blob: 7d27566b8579caedb1d4e7451d8ca1308feac635 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersene494fdd1999-10-19 20:23:03 +00002/*
3 * fsck.c - a file system consistency checker for Linux.
4 *
5 * (C) 1991, 1992 Linus Torvalds. This file may be redistributed
6 * as per the GNU copyleft.
7 */
8
9/*
10 * 09.11.91 - made the first rudimetary functions
11 *
12 * 10.11.91 - updated, does checking, no repairs yet.
13 * Sent out to the mailing-list for testing.
14 *
15 * 14.11.91 - Testing seems to have gone well. Added some
16 * correction-code, and changed some functions.
17 *
18 * 15.11.91 - More correction code. Hopefully it notices most
19 * cases now, and tries to do something about them.
20 *
21 * 16.11.91 - More corrections (thanks to Mika Jalava). Most
22 * things seem to work now. Yeah, sure.
23 *
24 *
25 * 19.04.92 - Had to start over again from this old version, as a
26 * kernel bug ate my enhanced fsck in february.
27 *
28 * 28.02.93 - added support for different directory entry sizes..
29 *
30 * Sat Mar 6 18:59:42 1993, faith@cs.unc.edu: Output namelen with
31 * super-block information
32 *
33 * Sat Oct 9 11:17:11 1993, faith@cs.unc.edu: make exit status conform
34 * to that required by fsutil
35 *
36 * Mon Jan 3 11:06:52 1994 - Dr. Wettstein (greg%wind.uucp@plains.nodak.edu)
37 * Added support for file system valid flag. Also
38 * added program_version variable and output of
39 * program name and version number when program
40 * is executed.
41 *
42 * 30.10.94 - added support for v2 filesystem
43 * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de)
44 *
45 * 10.12.94 - added test to prevent checking of mounted fs adapted
46 * from Theodore Ts'o's (tytso@athena.mit.edu) e2fsck
47 * program. (Daniel Quinlan, quinlan@yggdrasil.com)
48 *
49 * 01.07.96 - Fixed the v2 fs stuff to use the right #defines and such
50 * for modern libcs (janl@math.uio.no, Nicolai Langfeldt)
51 *
52 * 02.07.96 - Added C bit fiddling routines from rmk@ecs.soton.ac.uk
53 * (Russell King). He made them for ARM. It would seem
54 * that the ARM is powerful enough to do this in C whereas
55 * i386 and m64k must use assembly to get it fast >:-)
56 * This should make minix fsck systemindependent.
57 * (janl@math.uio.no, Nicolai Langfeldt)
58 *
59 * 04.11.96 - Added minor fixes from Andreas Schwab to avoid compiler
60 * warnings. Added mc68k bitops from
61 * Joerg Dorchain <dorchain@mpi-sb.mpg.de>.
62 *
63 * 06.11.96 - Added v2 code submitted by Joerg Dorchain, but written by
64 * Andreas Schwab.
65 *
66 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
67 * - added Native Language Support
68 *
69 *
70 * I've had no time to add comments - hopefully the function names
71 * are comments enough. As with all file system checkers, this assumes
72 * the file system is quiescent - don't use it on a mounted device
73 * unless you can be sure nobody is writing to it (and remember that the
74 * kernel can write to it when it searches for files).
75 *
76 * Usuage: fsck [-larvsm] device
77 * -l for a listing of all the filenames
78 * -a for automatic repairs (not implemented)
79 * -r for repairs (interactive) (not implemented)
80 * -v for verbose (tells how many files)
81 * -s for super-block info
82 * -m for minix-like "mode not cleared" warnings
83 * -f force filesystem check even if filesystem marked as valid
84 *
85 * The device may be a block device or a image of one, but this isn't
86 * enforced (but it's not much fun on a character device :-).
87 */
88
Eric Andersene674eb71999-10-19 20:52:57 +000089#include "internal.h"
Eric Andersene494fdd1999-10-19 20:23:03 +000090#include <stdio.h>
91#include <errno.h>
92#include <unistd.h>
93#include <string.h>
94#include <fcntl.h>
95#include <ctype.h>
96#include <stdlib.h>
97#include <termios.h>
98#include <mntent.h>
99#include <sys/stat.h>
100
101#include <linux/fs.h>
102#include <linux/minix_fs.h>
Eric Andersene494fdd1999-10-19 20:23:03 +0000103
104#ifdef MINIX2_SUPER_MAGIC2
105#define HAVE_MINIX2 1
106#endif
107
108#ifndef __linux__
109#define volatile
110#endif
111
112#define ROOT_INO 1
113
114#define UPPER(size,n) ((size+((n)-1))/(n))
115#define INODE_SIZE (sizeof(struct minix_inode))
116#ifdef HAVE_MINIX2
117#define INODE_SIZE2 (sizeof(struct minix2_inode))
118#define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
119 : MINIX_INODES_PER_BLOCK))
120#else
121#define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
122#endif
123#define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
124
125#define BITS_PER_BLOCK (BLOCK_SIZE<<3)
126
Erik Andersene49d5ec2000-02-08 19:58:47 +0000127static char *program_name = "fsck.minix";
128static char *program_version = "1.2 - 11/11/96";
129static char *device_name = NULL;
Eric Andersene494fdd1999-10-19 20:23:03 +0000130static int IN;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000131static int repair = 0, automatic = 0, verbose = 0, list = 0, show =
132 0, warn_mode = 0, force = 0;
133static int directory = 0, regular = 0, blockdev = 0, chardev = 0, links =
134 0, symlinks = 0, total = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000135
Erik Andersene49d5ec2000-02-08 19:58:47 +0000136static int changed = 0; /* flags if the filesystem has been changed */
137static int errors_uncorrected = 0; /* flag if some error was not corrected */
Eric Andersene494fdd1999-10-19 20:23:03 +0000138static int dirsize = 16;
139static int namelen = 14;
140static int version2 = 0;
141static struct termios termios;
142static int termios_set = 0;
143
144/* File-name data */
John Beppuc1dc5d92000-02-11 12:43:20 +0000145#define MAX_DEPTH 32
Eric Andersene494fdd1999-10-19 20:23:03 +0000146static int name_depth = 0;
Erik Andersen4f3f7572000-04-28 00:18:56 +0000147// static char name_list[MAX_DEPTH][BUFSIZ + 1];
John Beppu5e1b2ca2000-02-11 12:52:55 +0000148static char **name_list = NULL;
Eric Andersene494fdd1999-10-19 20:23:03 +0000149
Erik Andersene49d5ec2000-02-08 19:58:47 +0000150static char *inode_buffer = NULL;
151
Eric Andersene494fdd1999-10-19 20:23:03 +0000152#define Inode (((struct minix_inode *) inode_buffer)-1)
153#define Inode2 (((struct minix2_inode *) inode_buffer)-1)
154static char super_block_buffer[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000155
Eric Andersene494fdd1999-10-19 20:23:03 +0000156#define Super (*(struct minix_super_block *)super_block_buffer)
157#define INODES ((unsigned long)Super.s_ninodes)
158#ifdef HAVE_MINIX2
159#define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
160#else
161#define ZONES ((unsigned long)(Super.s_nzones))
162#endif
163#define IMAPS ((unsigned long)Super.s_imap_blocks)
164#define ZMAPS ((unsigned long)Super.s_zmap_blocks)
165#define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
166#define ZONESIZE ((unsigned long)Super.s_log_zone_size)
167#define MAXSIZE ((unsigned long)Super.s_max_size)
168#define MAGIC (Super.s_magic)
169#define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
170
171static char *inode_map;
172static char *zone_map;
173
Erik Andersene49d5ec2000-02-08 19:58:47 +0000174static unsigned char *inode_count = NULL;
175static unsigned char *zone_count = NULL;
Eric Andersene494fdd1999-10-19 20:23:03 +0000176
Eric Andersene674eb71999-10-19 20:52:57 +0000177static void recursive_check(unsigned int ino);
178static void recursive_check2(unsigned int ino);
Eric Andersene494fdd1999-10-19 20:23:03 +0000179
Erik Andersen4f3f7572000-04-28 00:18:56 +0000180#define inode_in_use(x) (isset(inode_map,(x)))
181#define zone_in_use(x) (isset(zone_map,(x)-FIRSTZONE+1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000182
183#define mark_inode(x) (setbit(inode_map,(x)),changed=1)
184#define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
185
186#define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1)
187#define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1)
188
Eric Andersene674eb71999-10-19 20:52:57 +0000189static void leave(int) __attribute__ ((noreturn));
190static void leave(int status)
Eric Andersene494fdd1999-10-19 20:23:03 +0000191{
192 if (termios_set)
193 tcsetattr(0, TCSANOW, &termios);
194 exit(status);
195}
196
Erik Andersene49d5ec2000-02-08 19:58:47 +0000197static void show_usage(void)
198{
199 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
200 BB_VER, BB_BT);
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000201 fprintf(stderr, "Usage: %s [-larvsmf] /dev/name\n", program_name);
202#ifndef BB_FEATURE_TRIVIAL_HELP
Erik Andersene49d5ec2000-02-08 19:58:47 +0000203 fprintf(stderr,
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000204 "\nPerforms a consistency check for MINIX filesystems.\n\n");
Eric Andersend73dc5b1999-11-10 23:13:02 +0000205 fprintf(stderr, "OPTIONS:\n");
206 fprintf(stderr, "\t-l\tLists all filenames\n");
207 fprintf(stderr, "\t-r\tPerform interactive repairs\n");
208 fprintf(stderr, "\t-a\tPerform automatic repairs\n");
209 fprintf(stderr, "\t-v\tverbose\n");
210 fprintf(stderr, "\t-s\tOutputs super-block information\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000211 fprintf(stderr,
212 "\t-m\tActivates MINIX-like \"mode not cleared\" warnings\n");
Eric Andersend73dc5b1999-11-10 23:13:02 +0000213 fprintf(stderr, "\t-f\tForce file system check.\n\n");
Erik Andersen7ab9c7e2000-05-12 19:41:47 +0000214#endif
Eric Andersene494fdd1999-10-19 20:23:03 +0000215 leave(16);
216}
217
Erik Andersene49d5ec2000-02-08 19:58:47 +0000218static void die(const char *str)
219{
Eric Andersene494fdd1999-10-19 20:23:03 +0000220 fprintf(stderr, "%s: %s\n", program_name, str);
221 leave(8);
222}
223
224/*
225 * This simply goes through the file-name data and prints out the
226 * current file.
227 */
Eric Andersene674eb71999-10-19 20:52:57 +0000228static void print_current_name(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000229{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000230 int i = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000231
Erik Andersene49d5ec2000-02-08 19:58:47 +0000232 while (i < name_depth)
233 printf("/%.*s", namelen, name_list[i++]);
Eric Andersene494fdd1999-10-19 20:23:03 +0000234 if (i == 0)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000235 printf("/");
Eric Andersene494fdd1999-10-19 20:23:03 +0000236}
237
Erik Andersene49d5ec2000-02-08 19:58:47 +0000238static int ask(const char *string, int def)
Eric Andersene494fdd1999-10-19 20:23:03 +0000239{
240 int c;
241
242 if (!repair) {
243 printf("\n");
244 errors_uncorrected = 1;
245 return 0;
246 }
247 if (automatic) {
248 printf("\n");
249 if (!def)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000250 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000251 return def;
252 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000253 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
Eric Andersene494fdd1999-10-19 20:23:03 +0000254 for (;;) {
255 fflush(stdout);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000256 if ((c = getchar()) == EOF) {
257 if (!def)
258 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000259 return def;
260 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000261 c = toupper(c);
Eric Andersene494fdd1999-10-19 20:23:03 +0000262 if (c == 'Y') {
263 def = 1;
264 break;
265 } else if (c == 'N') {
266 def = 0;
267 break;
268 } else if (c == ' ' || c == '\n')
269 break;
270 }
271 if (def)
272 printf("y\n");
273 else {
274 printf("n\n");
275 errors_uncorrected = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000276 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000277 return def;
278}
279
280/*
281 * Make certain that we aren't checking a filesystem that is on a
282 * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
283 * 1994 Theodore Ts'o. Also licensed under GPL.
284 */
285static void check_mount(void)
286{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000287 FILE *f;
288 struct mntent *mnt;
Eric Andersene494fdd1999-10-19 20:23:03 +0000289 int cont;
290 int fd;
291
Erik Andersene49d5ec2000-02-08 19:58:47 +0000292 if ((f = setmntent(MOUNTED, "r")) == NULL)
Eric Andersene494fdd1999-10-19 20:23:03 +0000293 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000294 while ((mnt = getmntent(f)) != NULL)
295 if (strcmp(device_name, mnt->mnt_fsname) == 0)
Eric Andersene494fdd1999-10-19 20:23:03 +0000296 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000297 endmntent(f);
Eric Andersene494fdd1999-10-19 20:23:03 +0000298 if (!mnt)
299 return;
300
301 /*
302 * If the root is mounted read-only, then /etc/mtab is
303 * probably not correct; so we won't issue a warning based on
304 * it.
305 */
306 fd = open(MOUNTED, O_RDWR);
307 if (fd < 0 && errno == EROFS)
308 return;
309 else
310 close(fd);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000311
312 printf("%s is mounted. ", device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +0000313 if (isatty(0) && isatty(1))
Eric Andersene674eb71999-10-19 20:52:57 +0000314 cont = ask("Do you really want to continue", 0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000315 else
316 cont = 0;
317 if (!cont) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000318 printf("check aborted.\n");
319 exit(0);
Eric Andersene494fdd1999-10-19 20:23:03 +0000320 }
321 return;
322}
323
324/*
325 * check_zone_nr checks to see that *nr is a valid zone nr. If it
326 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
327 * if an error was corrected, and returns the zone (0 for no zone
328 * or a bad zone-number).
329 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000330static int check_zone_nr(unsigned short *nr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000331{
332 if (!*nr)
333 return 0;
334 if (*nr < FIRSTZONE)
Eric Andersene674eb71999-10-19 20:52:57 +0000335 printf("Zone nr < FIRSTZONE in file `");
Eric Andersene494fdd1999-10-19 20:23:03 +0000336 else if (*nr >= ZONES)
Eric Andersene674eb71999-10-19 20:52:57 +0000337 printf("Zone nr >= ZONES in file `");
Eric Andersene494fdd1999-10-19 20:23:03 +0000338 else
339 return *nr;
340 print_current_name();
341 printf("'.");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000342 if (ask("Remove block", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000343 *nr = 0;
344 *corrected = 1;
345 }
346 return 0;
347}
348
349#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000350static int check_zone_nr2(unsigned int *nr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000351{
352 if (!*nr)
353 return 0;
354 if (*nr < FIRSTZONE)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000355 printf("Zone nr < FIRSTZONE in file `");
Eric Andersene494fdd1999-10-19 20:23:03 +0000356 else if (*nr >= ZONES)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000357 printf("Zone nr >= ZONES in file `");
Eric Andersene494fdd1999-10-19 20:23:03 +0000358 else
359 return *nr;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000360 print_current_name();
361 printf("'.");
362 if (ask("Remove block", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000363 *nr = 0;
364 *corrected = 1;
365 }
366 return 0;
367}
368#endif
369
370/*
371 * read-block reads block nr into the buffer at addr.
372 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000373static void read_block(unsigned int nr, char *addr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000374{
375 if (!nr) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000376 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000377 return;
378 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000379 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) {
Eric Andersene674eb71999-10-19 20:52:57 +0000380 printf("Read error: unable to seek to block in file '");
Eric Andersene494fdd1999-10-19 20:23:03 +0000381 print_current_name();
382 printf("'\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000383 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000384 errors_uncorrected = 1;
385 } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) {
Eric Andersene674eb71999-10-19 20:52:57 +0000386 printf("Read error: bad block in file '");
Eric Andersene494fdd1999-10-19 20:23:03 +0000387 print_current_name();
388 printf("'\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000389 memset(addr, 0, BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000390 errors_uncorrected = 1;
391 }
392}
393
394/*
395 * write_block writes block nr to disk.
396 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000397static void write_block(unsigned int nr, char *addr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000398{
399 if (!nr)
400 return;
401 if (nr < FIRSTZONE || nr >= ZONES) {
Eric Andersene674eb71999-10-19 20:52:57 +0000402 printf("Internal error: trying to write bad block\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +0000403 "Write request ignored\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000404 errors_uncorrected = 1;
405 return;
406 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000407 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET))
Eric Andersene674eb71999-10-19 20:52:57 +0000408 die("seek failed in write_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000409 if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) {
Eric Andersene674eb71999-10-19 20:52:57 +0000410 printf("Write error: bad block in file '");
Eric Andersene494fdd1999-10-19 20:23:03 +0000411 print_current_name();
412 printf("'\n");
413 errors_uncorrected = 1;
414 }
415}
416
417/*
418 * map-block calculates the absolute block nr of a block in a file.
419 * It sets 'changed' if the inode has needed changing, and re-writes
420 * any indirect blocks with errors.
421 */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000422static int map_block(struct minix_inode *inode, unsigned int blknr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000423{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000424 unsigned short ind[BLOCK_SIZE >> 1];
425 unsigned short dind[BLOCK_SIZE >> 1];
Eric Andersene494fdd1999-10-19 20:23:03 +0000426 int blk_chg, block, result;
427
Erik Andersene49d5ec2000-02-08 19:58:47 +0000428 if (blknr < 7)
Eric Andersene494fdd1999-10-19 20:23:03 +0000429 return check_zone_nr(inode->i_zone + blknr, &changed);
430 blknr -= 7;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000431 if (blknr < 512) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000432 block = check_zone_nr(inode->i_zone + 7, &changed);
433 read_block(block, (char *) ind);
434 blk_chg = 0;
435 result = check_zone_nr(blknr + ind, &blk_chg);
436 if (blk_chg)
437 write_block(block, (char *) ind);
438 return result;
439 }
440 blknr -= 512;
441 block = check_zone_nr(inode->i_zone + 8, &changed);
442 read_block(block, (char *) dind);
443 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000444 result = check_zone_nr(dind + (blknr / 512), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000445 if (blk_chg)
446 write_block(block, (char *) dind);
447 block = result;
448 read_block(block, (char *) ind);
449 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000450 result = check_zone_nr(ind + (blknr % 512), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000451 if (blk_chg)
452 write_block(block, (char *) ind);
453 return result;
454}
455
456#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000457static int map_block2(struct minix2_inode *inode, unsigned int blknr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000458{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000459 unsigned int ind[BLOCK_SIZE >> 2];
Eric Andersene494fdd1999-10-19 20:23:03 +0000460 unsigned int dind[BLOCK_SIZE >> 2];
461 unsigned int tind[BLOCK_SIZE >> 2];
462 int blk_chg, block, result;
463
464 if (blknr < 7)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000465 return check_zone_nr2(inode->i_zone + blknr, &changed);
Eric Andersene494fdd1999-10-19 20:23:03 +0000466 blknr -= 7;
467 if (blknr < 256) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000468 block = check_zone_nr2(inode->i_zone + 7, &changed);
469 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000470 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000471 result = check_zone_nr2(blknr + ind, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000472 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000473 write_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000474 return result;
475 }
476 blknr -= 256;
477 if (blknr >= 256 * 256) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000478 block = check_zone_nr2(inode->i_zone + 8, &changed);
479 read_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000480 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000481 result = check_zone_nr2(dind + blknr / 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000482 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000483 write_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000484 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000485 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000486 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000487 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000488 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000489 write_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000490 return result;
491 }
492 blknr -= 256 * 256;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000493 block = check_zone_nr2(inode->i_zone + 9, &changed);
494 read_block(block, (char *) tind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000495 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000496 result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000497 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000498 write_block(block, (char *) tind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000499 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000500 read_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000501 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000502 result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000503 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000504 write_block(block, (char *) dind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000505 block = result;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000506 read_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000507 blk_chg = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000508 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000509 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000510 write_block(block, (char *) ind);
Eric Andersene494fdd1999-10-19 20:23:03 +0000511 return result;
512}
513#endif
514
Eric Andersene674eb71999-10-19 20:52:57 +0000515static void write_super_block(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000516{
517 /*
518 * Set the state of the filesystem based on whether or not there
519 * are uncorrected errors. The filesystem valid flag is
520 * unconditionally set if we get this far.
521 */
522 Super.s_state |= MINIX_VALID_FS;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000523 if (errors_uncorrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000524 Super.s_state |= MINIX_ERROR_FS;
525 else
526 Super.s_state &= ~MINIX_ERROR_FS;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000527
Eric Andersene494fdd1999-10-19 20:23:03 +0000528 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
Eric Andersene674eb71999-10-19 20:52:57 +0000529 die("seek failed in write_super_block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000530 if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000531 die("unable to write super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000532
533 return;
534}
535
Eric Andersene674eb71999-10-19 20:52:57 +0000536static void write_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000537{
538 write_super_block();
539
Erik Andersene49d5ec2000-02-08 19:58:47 +0000540 if (IMAPS * BLOCK_SIZE != write(IN, inode_map, IMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000541 die("Unable to write inode map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000542 if (ZMAPS * BLOCK_SIZE != write(IN, zone_map, ZMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000543 die("Unable to write zone map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000544 if (INODE_BUFFER_SIZE != write(IN, inode_buffer, INODE_BUFFER_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000545 die("Unable to write inodes");
Eric Andersene494fdd1999-10-19 20:23:03 +0000546}
547
Erik Andersene49d5ec2000-02-08 19:58:47 +0000548static void get_dirsize(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000549{
550 int block;
551 char blk[BLOCK_SIZE];
552 int size;
553
554#if HAVE_MINIX2
555 if (version2)
556 block = Inode2[ROOT_INO].i_zone[0];
557 else
558#endif
559 block = Inode[ROOT_INO].i_zone[0];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000560 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000561 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000562 if (strcmp(blk + size + 2, "..") == 0) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000563 dirsize = size;
564 namelen = size - 2;
565 return;
566 }
567 }
568 /* use defaults */
569}
570
Eric Andersene674eb71999-10-19 20:52:57 +0000571static void read_superblock(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000572{
573 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
Eric Andersene674eb71999-10-19 20:52:57 +0000574 die("seek failed");
Eric Andersene494fdd1999-10-19 20:23:03 +0000575 if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000576 die("unable to read super block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000577 if (MAGIC == MINIX_SUPER_MAGIC) {
578 namelen = 14;
579 dirsize = 16;
580 version2 = 0;
581 } else if (MAGIC == MINIX_SUPER_MAGIC2) {
582 namelen = 30;
583 dirsize = 32;
584 version2 = 0;
585#ifdef HAVE_MINIX2
586 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
587 namelen = 14;
588 dirsize = 16;
589 version2 = 1;
590 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
591 namelen = 30;
592 dirsize = 32;
593 version2 = 1;
594#endif
595 } else
Eric Andersene674eb71999-10-19 20:52:57 +0000596 die("bad magic number in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000597 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
Eric Andersene674eb71999-10-19 20:52:57 +0000598 die("Only 1k blocks/zones supported");
Eric Andersene494fdd1999-10-19 20:23:03 +0000599 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
Eric Andersene674eb71999-10-19 20:52:57 +0000600 die("bad s_imap_blocks field in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000601 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
Eric Andersene674eb71999-10-19 20:52:57 +0000602 die("bad s_zmap_blocks field in super-block");
Eric Andersene494fdd1999-10-19 20:23:03 +0000603}
604
Eric Andersene674eb71999-10-19 20:52:57 +0000605static void read_tables(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000606{
Erik Andersen0d068a22000-03-21 22:32:57 +0000607 inode_map = xmalloc(IMAPS * BLOCK_SIZE);
608 zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000609 memset(inode_map, 0, sizeof(inode_map));
610 memset(zone_map, 0, sizeof(zone_map));
Erik Andersen0d068a22000-03-21 22:32:57 +0000611 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
612 inode_count = xmalloc(INODES + 1);
613 zone_count = xmalloc(ZONES);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000614 if (IMAPS * BLOCK_SIZE != read(IN, inode_map, IMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000615 die("Unable to read inode map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000616 if (ZMAPS * BLOCK_SIZE != read(IN, zone_map, ZMAPS * BLOCK_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000617 die("Unable to read zone map");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000618 if (INODE_BUFFER_SIZE != read(IN, inode_buffer, INODE_BUFFER_SIZE))
Eric Andersene674eb71999-10-19 20:52:57 +0000619 die("Unable to read inodes");
Eric Andersene494fdd1999-10-19 20:23:03 +0000620 if (NORM_FIRSTZONE != FIRSTZONE) {
Eric Andersene674eb71999-10-19 20:52:57 +0000621 printf("Warning: Firstzone != Norm_firstzone\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000622 errors_uncorrected = 1;
623 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000624 get_dirsize();
Eric Andersene494fdd1999-10-19 20:23:03 +0000625 if (show) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000626 printf("%ld inodes\n", INODES);
627 printf("%ld blocks\n", ZONES);
628 printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
629 printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
630 printf("Maxsize=%ld\n", MAXSIZE);
Eric Andersene674eb71999-10-19 20:52:57 +0000631 printf("Filesystem state=%d\n", Super.s_state);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000632 printf("namelen=%d\n\n", namelen);
Eric Andersene494fdd1999-10-19 20:23:03 +0000633 }
634}
635
Erik Andersene49d5ec2000-02-08 19:58:47 +0000636struct minix_inode *get_inode(unsigned int nr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000637{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000638 struct minix_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000639
640 if (!nr || nr > INODES)
641 return NULL;
642 total++;
643 inode = Inode + nr;
644 if (!inode_count[nr]) {
645 if (!inode_in_use(nr)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000646 printf("Inode %d marked not used, but used for file '", nr);
Eric Andersene494fdd1999-10-19 20:23:03 +0000647 print_current_name();
648 printf("'\n");
649 if (repair) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000650 if (ask("Mark in use", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000651 mark_inode(nr);
652 } else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000653 errors_uncorrected = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +0000654 }
655 }
656 if (S_ISDIR(inode->i_mode))
657 directory++;
658 else if (S_ISREG(inode->i_mode))
659 regular++;
660 else if (S_ISCHR(inode->i_mode))
661 chardev++;
662 else if (S_ISBLK(inode->i_mode))
663 blockdev++;
664 else if (S_ISLNK(inode->i_mode))
665 symlinks++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000666 else if (S_ISSOCK(inode->i_mode));
667 else if (S_ISFIFO(inode->i_mode));
Eric Andersene494fdd1999-10-19 20:23:03 +0000668 else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000669 print_current_name();
670 printf(" has mode %05o\n", inode->i_mode);
671 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000672
673 } else
674 links++;
675 if (!++inode_count[nr]) {
Eric Andersene674eb71999-10-19 20:52:57 +0000676 printf("Warning: inode count too big.\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000677 inode_count[nr]--;
678 errors_uncorrected = 1;
679 }
680 return inode;
681}
682
683#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000684struct minix2_inode *get_inode2(unsigned int nr)
Eric Andersene494fdd1999-10-19 20:23:03 +0000685{
686 struct minix2_inode *inode;
687
688 if (!nr || nr > INODES)
689 return NULL;
690 total++;
691 inode = Inode2 + nr;
692 if (!inode_count[nr]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000693 if (!inode_in_use(nr)) {
694 printf("Inode %d marked not used, but used for file '", nr);
695 print_current_name();
696 printf("'\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000697 if (repair) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000698 if (ask("Mark in use", 1))
699 mark_inode(nr);
Eric Andersene494fdd1999-10-19 20:23:03 +0000700 else
701 errors_uncorrected = 1;
702 }
703 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000704 if (S_ISDIR(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000705 directory++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000706 else if (S_ISREG(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000707 regular++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000708 else if (S_ISCHR(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000709 chardev++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000710 else if (S_ISBLK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000711 blockdev++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000712 else if (S_ISLNK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000713 symlinks++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000714 else if (S_ISSOCK(inode->i_mode));
715 else if (S_ISFIFO(inode->i_mode));
Eric Andersene494fdd1999-10-19 20:23:03 +0000716 else {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000717 print_current_name();
718 printf(" has mode %05o\n", inode->i_mode);
Eric Andersene494fdd1999-10-19 20:23:03 +0000719 }
720 } else
721 links++;
722 if (!++inode_count[nr]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000723 printf("Warning: inode count too big.\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000724 inode_count[nr]--;
725 errors_uncorrected = 1;
726 }
727 return inode;
728}
729#endif
730
Eric Andersene674eb71999-10-19 20:52:57 +0000731static void check_root(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000732{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000733 struct minix_inode *inode = Inode + ROOT_INO;
Eric Andersene494fdd1999-10-19 20:23:03 +0000734
735 if (!inode || !S_ISDIR(inode->i_mode))
Eric Andersene674eb71999-10-19 20:52:57 +0000736 die("root inode isn't a directory");
Eric Andersene494fdd1999-10-19 20:23:03 +0000737}
738
739#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000740static void check_root2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +0000741{
742 struct minix2_inode *inode = Inode2 + ROOT_INO;
743
Erik Andersene49d5ec2000-02-08 19:58:47 +0000744 if (!inode || !S_ISDIR(inode->i_mode))
745 die("root inode isn't a directory");
Eric Andersene494fdd1999-10-19 20:23:03 +0000746}
747#endif
748
Erik Andersene49d5ec2000-02-08 19:58:47 +0000749static int add_zone(unsigned short *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000750{
751 int result;
752 int block;
753
754 result = 0;
755 block = check_zone_nr(znr, corrected);
756 if (!block)
757 return 0;
758 if (zone_count[block]) {
Eric Andersene674eb71999-10-19 20:52:57 +0000759 printf("Block has been used before. Now in file `");
Eric Andersene494fdd1999-10-19 20:23:03 +0000760 print_current_name();
761 printf("'.");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000762 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000763 *znr = 0;
764 block = 0;
765 *corrected = 1;
766 }
767 }
768 if (!block)
769 return 0;
770 if (!zone_in_use(block)) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000771 printf("Block %d in file `", block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000772 print_current_name();
Eric Andersene674eb71999-10-19 20:52:57 +0000773 printf("' is marked not in use.");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000774 if (ask("Correct", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +0000775 mark_zone(block);
776 }
777 if (!++zone_count[block])
778 zone_count[block]--;
779 return block;
780}
781
782#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000783static int add_zone2(unsigned int *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000784{
785 int result;
786 int block;
787
788 result = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000789 block = check_zone_nr2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000790 if (!block)
791 return 0;
792 if (zone_count[block]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000793 printf("Block has been used before. Now in file `");
794 print_current_name();
795 printf("'.");
796 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000797 *znr = 0;
798 block = 0;
799 *corrected = 1;
800 }
801 }
802 if (!block)
803 return 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000804 if (!zone_in_use(block)) {
805 printf("Block %d in file `", block);
806 print_current_name();
807 printf("' is marked not in use.");
808 if (ask("Correct", 1))
809 mark_zone(block);
Eric Andersene494fdd1999-10-19 20:23:03 +0000810 }
811 if (!++zone_count[block])
812 zone_count[block]--;
813 return block;
814}
815#endif
816
Erik Andersene49d5ec2000-02-08 19:58:47 +0000817static void add_zone_ind(unsigned short *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000818{
819 static char blk[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000820 int i, chg_blk = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000821 int block;
822
823 block = add_zone(znr, corrected);
824 if (!block)
825 return;
826 read_block(block, blk);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000827 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000828 add_zone(i + (unsigned short *) blk, &chg_blk);
829 if (chg_blk)
830 write_block(block, blk);
831}
832
833#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000834static void add_zone_ind2(unsigned int *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000835{
836 static char blk[BLOCK_SIZE];
837 int i, chg_blk = 0;
838 int block;
839
Erik Andersene49d5ec2000-02-08 19:58:47 +0000840 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000841 if (!block)
842 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000843 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000844 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000845 add_zone2(i + (unsigned int *) blk, &chg_blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000846 if (chg_blk)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000847 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000848}
849#endif
850
Erik Andersene49d5ec2000-02-08 19:58:47 +0000851static void add_zone_dind(unsigned short *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000852{
853 static char blk[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000854 int i, blk_chg = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000855 int block;
856
857 block = add_zone(znr, corrected);
858 if (!block)
859 return;
860 read_block(block, blk);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000861 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000862 add_zone_ind(i + (unsigned short *) blk, &blk_chg);
863 if (blk_chg)
864 write_block(block, blk);
865}
866
867#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000868static void add_zone_dind2(unsigned int *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000869{
870 static char blk[BLOCK_SIZE];
871 int i, blk_chg = 0;
872 int block;
873
Erik Andersene49d5ec2000-02-08 19:58:47 +0000874 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000875 if (!block)
876 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000877 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000878 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000879 add_zone_ind2(i + (unsigned int *) blk, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000880 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000881 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000882}
883
Erik Andersene49d5ec2000-02-08 19:58:47 +0000884static void add_zone_tind2(unsigned int *znr, int *corrected)
Eric Andersene494fdd1999-10-19 20:23:03 +0000885{
886 static char blk[BLOCK_SIZE];
887 int i, blk_chg = 0;
888 int block;
889
Erik Andersene49d5ec2000-02-08 19:58:47 +0000890 block = add_zone2(znr, corrected);
Eric Andersene494fdd1999-10-19 20:23:03 +0000891 if (!block)
892 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000893 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000894 for (i = 0; i < BLOCK_SIZE >> 2; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000895 add_zone_dind2(i + (unsigned int *) blk, &blk_chg);
Eric Andersene494fdd1999-10-19 20:23:03 +0000896 if (blk_chg)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000897 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +0000898}
899#endif
900
Eric Andersene674eb71999-10-19 20:52:57 +0000901static void check_zones(unsigned int i)
Eric Andersene494fdd1999-10-19 20:23:03 +0000902{
Erik Andersene49d5ec2000-02-08 19:58:47 +0000903 struct minix_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000904
905 if (!i || i > INODES)
906 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000907 if (inode_count[i] > 1) /* have we counted this file already? */
Eric Andersene494fdd1999-10-19 20:23:03 +0000908 return;
909 inode = Inode + i;
910 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
Erik Andersene49d5ec2000-02-08 19:58:47 +0000911 !S_ISLNK(inode->i_mode)) return;
912 for (i = 0; i < 7; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +0000913 add_zone(i + inode->i_zone, &changed);
914 add_zone_ind(7 + inode->i_zone, &changed);
915 add_zone_dind(8 + inode->i_zone, &changed);
916}
917
918#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +0000919static void check_zones2(unsigned int i)
Eric Andersene494fdd1999-10-19 20:23:03 +0000920{
921 struct minix2_inode *inode;
922
923 if (!i || i > INODES)
924 return;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000925 if (inode_count[i] > 1) /* have we counted this file already? */
Eric Andersene494fdd1999-10-19 20:23:03 +0000926 return;
927 inode = Inode2 + i;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000928 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
929 && !S_ISLNK(inode->i_mode))
Eric Andersene494fdd1999-10-19 20:23:03 +0000930 return;
931 for (i = 0; i < 7; i++)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000932 add_zone2(i + inode->i_zone, &changed);
933 add_zone_ind2(7 + inode->i_zone, &changed);
934 add_zone_dind2(8 + inode->i_zone, &changed);
935 add_zone_tind2(9 + inode->i_zone, &changed);
Eric Andersene494fdd1999-10-19 20:23:03 +0000936}
937#endif
938
Erik Andersene49d5ec2000-02-08 19:58:47 +0000939static void check_file(struct minix_inode *dir, unsigned int offset)
Eric Andersene494fdd1999-10-19 20:23:03 +0000940{
941 static char blk[BLOCK_SIZE];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000942 struct minix_inode *inode;
Eric Andersene494fdd1999-10-19 20:23:03 +0000943 int ino;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000944 char *name;
Eric Andersene494fdd1999-10-19 20:23:03 +0000945 int block;
946
Erik Andersene49d5ec2000-02-08 19:58:47 +0000947 block = map_block(dir, offset / BLOCK_SIZE);
Eric Andersene494fdd1999-10-19 20:23:03 +0000948 read_block(block, blk);
949 name = blk + (offset % BLOCK_SIZE) + 2;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000950 ino = *(unsigned short *) (name - 2);
Eric Andersene494fdd1999-10-19 20:23:03 +0000951 if (ino > INODES) {
952 print_current_name();
Eric Andersene674eb71999-10-19 20:52:57 +0000953 printf(" contains a bad inode number for file '");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000954 printf("%.*s'.", namelen, name);
955 if (ask(" Remove", 1)) {
956 *(unsigned short *) (name - 2) = 0;
Eric Andersene494fdd1999-10-19 20:23:03 +0000957 write_block(block, blk);
958 }
959 ino = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000960 }
Eric Andersene494fdd1999-10-19 20:23:03 +0000961 if (name_depth < MAX_DEPTH)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000962 strncpy(name_list[name_depth], name, namelen);
Eric Andersene494fdd1999-10-19 20:23:03 +0000963 name_depth++;
964 inode = get_inode(ino);
965 name_depth--;
966 if (!offset) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000967 if (!inode || strcmp(".", name)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000968 print_current_name();
Eric Andersene674eb71999-10-19 20:52:57 +0000969 printf(": bad directory: '.' isn't first\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000970 errors_uncorrected = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000971 } else
972 return;
Eric Andersene494fdd1999-10-19 20:23:03 +0000973 }
974 if (offset == dirsize) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000975 if (!inode || strcmp("..", name)) {
Eric Andersene494fdd1999-10-19 20:23:03 +0000976 print_current_name();
Eric Andersene674eb71999-10-19 20:52:57 +0000977 printf(": bad directory: '..' isn't second\n");
Eric Andersene494fdd1999-10-19 20:23:03 +0000978 errors_uncorrected = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000979 } else
980 return;
Eric Andersene494fdd1999-10-19 20:23:03 +0000981 }
982 if (!inode)
983 return;
984 if (name_depth < MAX_DEPTH)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000985 strncpy(name_list[name_depth], name, namelen);
986 name_depth++;
Eric Andersene494fdd1999-10-19 20:23:03 +0000987 if (list) {
988 if (verbose)
Erik Andersene49d5ec2000-02-08 19:58:47 +0000989 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
Eric Andersene494fdd1999-10-19 20:23:03 +0000990 print_current_name();
991 if (S_ISDIR(inode->i_mode))
992 printf(":\n");
993 else
994 printf("\n");
995 }
996 check_zones(ino);
997 if (inode && S_ISDIR(inode->i_mode))
998 recursive_check(ino);
999 name_depth--;
1000 return;
1001}
1002
1003#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001004static void check_file2(struct minix2_inode *dir, unsigned int offset)
Eric Andersene494fdd1999-10-19 20:23:03 +00001005{
1006 static char blk[BLOCK_SIZE];
1007 struct minix2_inode *inode;
1008 int ino;
1009 char *name;
1010 int block;
1011
Erik Andersene49d5ec2000-02-08 19:58:47 +00001012 block = map_block2(dir, offset / BLOCK_SIZE);
1013 read_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +00001014 name = blk + (offset % BLOCK_SIZE) + 2;
1015 ino = *(unsigned short *) (name - 2);
1016 if (ino > INODES) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001017 print_current_name();
1018 printf(" contains a bad inode number for file '");
1019 printf("%.*s'.", namelen, name);
1020 if (ask(" Remove", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001021 *(unsigned short *) (name - 2) = 0;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001022 write_block(block, blk);
Eric Andersene494fdd1999-10-19 20:23:03 +00001023 }
1024 ino = 0;
1025 }
1026 if (name_depth < MAX_DEPTH)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001027 strncpy(name_list[name_depth], name, namelen);
Eric Andersene494fdd1999-10-19 20:23:03 +00001028 name_depth++;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001029 inode = get_inode2(ino);
Eric Andersene494fdd1999-10-19 20:23:03 +00001030 name_depth--;
1031 if (!offset) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001032 if (!inode || strcmp(".", name)) {
1033 print_current_name();
1034 printf(": bad directory: '.' isn't first\n");
Eric Andersene494fdd1999-10-19 20:23:03 +00001035 errors_uncorrected = 1;
1036 } else
1037 return;
1038 }
1039 if (offset == dirsize) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001040 if (!inode || strcmp("..", name)) {
1041 print_current_name();
1042 printf(": bad directory: '..' isn't second\n");
Eric Andersene494fdd1999-10-19 20:23:03 +00001043 errors_uncorrected = 1;
1044 } else
1045 return;
1046 }
1047 if (!inode)
1048 return;
1049 name_depth++;
1050 if (list) {
1051 if (verbose)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001052 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1053 print_current_name();
1054 if (S_ISDIR(inode->i_mode))
1055 printf(":\n");
Eric Andersene494fdd1999-10-19 20:23:03 +00001056 else
Erik Andersene49d5ec2000-02-08 19:58:47 +00001057 printf("\n");
Eric Andersene494fdd1999-10-19 20:23:03 +00001058 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001059 check_zones2(ino);
1060 if (inode && S_ISDIR(inode->i_mode))
1061 recursive_check2(ino);
Eric Andersene494fdd1999-10-19 20:23:03 +00001062 name_depth--;
1063 return;
1064}
1065#endif
1066
Eric Andersene674eb71999-10-19 20:52:57 +00001067static void recursive_check(unsigned int ino)
Eric Andersene494fdd1999-10-19 20:23:03 +00001068{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001069 struct minix_inode *dir;
Eric Andersene494fdd1999-10-19 20:23:03 +00001070 unsigned int offset;
1071
1072 dir = Inode + ino;
1073 if (!S_ISDIR(dir->i_mode))
Eric Andersene674eb71999-10-19 20:52:57 +00001074 die("internal error");
Eric Andersene494fdd1999-10-19 20:23:03 +00001075 if (dir->i_size < 2 * dirsize) {
1076 print_current_name();
Eric Andersene674eb71999-10-19 20:52:57 +00001077 printf(": bad directory: size<32");
Eric Andersene494fdd1999-10-19 20:23:03 +00001078 errors_uncorrected = 1;
1079 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001080 for (offset = 0; offset < dir->i_size; offset += dirsize)
1081 check_file(dir, offset);
Eric Andersene494fdd1999-10-19 20:23:03 +00001082}
1083
1084#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001085static void recursive_check2(unsigned int ino)
Eric Andersene494fdd1999-10-19 20:23:03 +00001086{
1087 struct minix2_inode *dir;
1088 unsigned int offset;
1089
1090 dir = Inode2 + ino;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001091 if (!S_ISDIR(dir->i_mode))
1092 die("internal error");
Eric Andersene494fdd1999-10-19 20:23:03 +00001093 if (dir->i_size < 2 * dirsize) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001094 print_current_name();
1095 printf(": bad directory: size < 32");
Eric Andersene494fdd1999-10-19 20:23:03 +00001096 errors_uncorrected = 1;
1097 }
1098 for (offset = 0; offset < dir->i_size; offset += dirsize)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001099 check_file2(dir, offset);
Eric Andersene494fdd1999-10-19 20:23:03 +00001100}
1101#endif
1102
Eric Andersene674eb71999-10-19 20:52:57 +00001103static int bad_zone(int i)
Eric Andersene494fdd1999-10-19 20:23:03 +00001104{
1105 char buffer[1024];
1106
Erik Andersene49d5ec2000-02-08 19:58:47 +00001107 if (BLOCK_SIZE * i != lseek(IN, BLOCK_SIZE * i, SEEK_SET))
Eric Andersene674eb71999-10-19 20:52:57 +00001108 die("seek failed in bad_zone");
Eric Andersene494fdd1999-10-19 20:23:03 +00001109 return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE));
1110}
1111
Eric Andersene674eb71999-10-19 20:52:57 +00001112static void check_counts(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001113{
1114 int i;
1115
Erik Andersene49d5ec2000-02-08 19:58:47 +00001116 for (i = 1; i <= INODES; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001117 if (!inode_in_use(i) && Inode[i].i_mode && warn_mode) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001118 printf("Inode %d mode not cleared.", i);
1119 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001120 Inode[i].i_mode = 0;
1121 changed = 1;
1122 }
1123 }
1124 if (!inode_count[i]) {
1125 if (!inode_in_use(i))
1126 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001127 printf("Inode %d not used, marked used in the bitmap.", i);
1128 if (ask("Clear", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001129 unmark_inode(i);
1130 continue;
1131 }
1132 if (!inode_in_use(i)) {
Eric Andersene674eb71999-10-19 20:52:57 +00001133 printf("Inode %d used, marked unused in the bitmap.", i);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001134 if (ask("Set", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001135 mark_inode(i);
1136 }
1137 if (Inode[i].i_nlinks != inode_count[i]) {
Eric Andersene674eb71999-10-19 20:52:57 +00001138 printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
Erik Andersene49d5ec2000-02-08 19:58:47 +00001139 i, Inode[i].i_mode, Inode[i].i_nlinks, inode_count[i]);
1140 if (ask("Set i_nlinks to count", 1)) {
1141 Inode[i].i_nlinks = inode_count[i];
1142 changed = 1;
Eric Andersene494fdd1999-10-19 20:23:03 +00001143 }
1144 }
1145 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001146 for (i = FIRSTZONE; i < ZONES; i++) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001147 if (zone_in_use(i) == zone_count[i])
1148 continue;
1149 if (!zone_count[i]) {
1150 if (bad_zone(i))
1151 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001152 printf("Zone %d: marked in use, no file uses it.", i);
1153 if (ask("Unmark", 1))
Eric Andersene494fdd1999-10-19 20:23:03 +00001154 unmark_zone(i);
1155 continue;
1156 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001157 printf("Zone %d: %sin use, counted=%d\n",
1158 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
Eric Andersene494fdd1999-10-19 20:23:03 +00001159 }
1160}
1161
1162#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001163static void check_counts2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001164{
1165 int i;
1166
1167 for (i = 1; i <= INODES; i++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001168 if (!inode_in_use(i) && Inode2[i].i_mode && warn_mode) {
1169 printf("Inode %d mode not cleared.", i);
1170 if (ask("Clear", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001171 Inode2[i].i_mode = 0;
1172 changed = 1;
1173 }
1174 }
1175 if (!inode_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001176 if (!inode_in_use(i))
Eric Andersene494fdd1999-10-19 20:23:03 +00001177 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001178 printf("Inode %d not used, marked used in the bitmap.", i);
1179 if (ask("Clear", 1))
1180 unmark_inode(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001181 continue;
1182 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001183 if (!inode_in_use(i)) {
1184 printf("Inode %d used, marked unused in the bitmap.", i);
1185 if (ask("Set", 1))
1186 mark_inode(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001187 }
1188 if (Inode2[i].i_nlinks != inode_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001189 printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
1190 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1191 inode_count[i]);
1192 if (ask("Set i_nlinks to count", 1)) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001193 Inode2[i].i_nlinks = inode_count[i];
1194 changed = 1;
1195 }
1196 }
1197 }
1198 for (i = FIRSTZONE; i < ZONES; i++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001199 if (zone_in_use(i) == zone_count[i])
Eric Andersene494fdd1999-10-19 20:23:03 +00001200 continue;
1201 if (!zone_count[i]) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001202 if (bad_zone(i))
Eric Andersene494fdd1999-10-19 20:23:03 +00001203 continue;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001204 printf("Zone %d: marked in use, no file uses it.", i);
1205 if (ask("Unmark", 1))
1206 unmark_zone(i);
Eric Andersene494fdd1999-10-19 20:23:03 +00001207 continue;
1208 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001209 printf("Zone %d: %sin use, counted=%d\n",
1210 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
Eric Andersene494fdd1999-10-19 20:23:03 +00001211 }
1212}
1213#endif
1214
Eric Andersene674eb71999-10-19 20:52:57 +00001215static void check(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001216{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001217 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1218 memset(zone_count, 0, ZONES * sizeof(*zone_count));
Eric Andersene494fdd1999-10-19 20:23:03 +00001219 check_zones(ROOT_INO);
1220 recursive_check(ROOT_INO);
1221 check_counts();
1222}
1223
1224#ifdef HAVE_MINIX2
Erik Andersene49d5ec2000-02-08 19:58:47 +00001225static void check2(void)
Eric Andersene494fdd1999-10-19 20:23:03 +00001226{
Erik Andersene49d5ec2000-02-08 19:58:47 +00001227 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1228 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1229 check_zones2(ROOT_INO);
1230 recursive_check2(ROOT_INO);
1231 check_counts2();
Eric Andersene494fdd1999-10-19 20:23:03 +00001232}
1233#endif
1234
John Beppuc1dc5d92000-02-11 12:43:20 +00001235/* Wed Feb 9 15:17:06 MST 2000 */
1236/* dynamically allocate name_list (instead of making it static) */
1237static void alloc_name_list(void)
1238{
1239 int i;
1240
Erik Andersen0d068a22000-03-21 22:32:57 +00001241 name_list = xmalloc(sizeof(char *) * MAX_DEPTH);
1242 for (i = 0; i < MAX_DEPTH; i++)
Erik Andersen4f3f7572000-04-28 00:18:56 +00001243 name_list[i] = xmalloc(sizeof(char) * BUFSIZ + 1);
John Beppuc1dc5d92000-02-11 12:43:20 +00001244}
1245
Erik Andersen298854f2000-03-23 01:09:18 +00001246#if 0
John Beppu5e1b2ca2000-02-11 12:52:55 +00001247/* execute this atexit() to deallocate name_list[] */
1248/* piptigger was here */
John Beppuc1dc5d92000-02-11 12:43:20 +00001249static void free_name_list(void)
1250{
John Beppu5e1b2ca2000-02-11 12:52:55 +00001251 int i;
1252
1253 if (name_list) {
1254 for (i = 0; i < MAX_DEPTH; i++) {
1255 if (name_list[i]) {
1256 free(name_list[i]);
1257 }
1258 }
1259 free(name_list);
1260 }
John Beppuc1dc5d92000-02-11 12:43:20 +00001261}
Erik Andersen298854f2000-03-23 01:09:18 +00001262#endif
John Beppuc1dc5d92000-02-11 12:43:20 +00001263
Erik Andersene49d5ec2000-02-08 19:58:47 +00001264extern int fsck_minix_main(int argc, char **argv)
Eric Andersene494fdd1999-10-19 20:23:03 +00001265{
1266 struct termios tmp;
1267 int count;
1268 int retcode = 0;
1269
John Beppuc1dc5d92000-02-11 12:43:20 +00001270 alloc_name_list();
Erik Andersen298854f2000-03-23 01:09:18 +00001271 /* Don't bother to free memory. Exit does
1272 * that automagically, so we can save a few bytes */
1273 //atexit(free_name_list);
John Beppuc1dc5d92000-02-11 12:43:20 +00001274
Eric Andersene494fdd1999-10-19 20:23:03 +00001275 if (argc && *argv)
1276 program_name = *argv;
1277 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
Eric Andersene674eb71999-10-19 20:52:57 +00001278 die("bad inode size");
Eric Andersene494fdd1999-10-19 20:23:03 +00001279#ifdef HAVE_MINIX2
1280 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
Eric Andersene674eb71999-10-19 20:52:57 +00001281 die("bad v2 inode size");
Eric Andersene494fdd1999-10-19 20:23:03 +00001282#endif
1283 while (argc-- > 1) {
1284 argv++;
1285 if (argv[0][0] != '-') {
1286 if (device_name)
Eric Andersene674eb71999-10-19 20:52:57 +00001287 show_usage();
Eric Andersene494fdd1999-10-19 20:23:03 +00001288 else
1289 device_name = argv[0];
Erik Andersene49d5ec2000-02-08 19:58:47 +00001290 } else
1291 while (*++argv[0])
1292 switch (argv[0][0]) {
1293 case 'l':
1294 list = 1;
1295 break;
1296 case 'a':
1297 automatic = 1;
1298 repair = 1;
1299 break;
1300 case 'r':
1301 automatic = 0;
1302 repair = 1;
1303 break;
1304 case 'v':
1305 verbose = 1;
1306 break;
1307 case 's':
1308 show = 1;
1309 break;
1310 case 'm':
1311 warn_mode = 1;
1312 break;
1313 case 'f':
1314 force = 1;
1315 break;
1316 default:
1317 show_usage();
1318 }
Eric Andersene494fdd1999-10-19 20:23:03 +00001319 }
1320 if (!device_name)
Eric Andersene674eb71999-10-19 20:52:57 +00001321 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001322 check_mount(); /* trying to check a mounted filesystem? */
Eric Andersene494fdd1999-10-19 20:23:03 +00001323 if (repair && !automatic) {
1324 if (!isatty(0) || !isatty(1))
Eric Andersene674eb71999-10-19 20:52:57 +00001325 die("need terminal for interactive repairs");
Eric Andersene494fdd1999-10-19 20:23:03 +00001326 }
Erik Andersene49d5ec2000-02-08 19:58:47 +00001327 IN = open(device_name, repair ? O_RDWR : O_RDONLY);
Eric Andersene494fdd1999-10-19 20:23:03 +00001328 if (IN < 0)
Eric Andersene674eb71999-10-19 20:52:57 +00001329 die("unable to open '%s'");
Erik Andersene49d5ec2000-02-08 19:58:47 +00001330 for (count = 0; count < 3; count++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001331 sync();
1332 read_superblock();
1333
1334 /*
1335 * Determine whether or not we should continue with the checking.
1336 * This is based on the status of the filesystem valid and error
1337 * flags and whether or not the -f switch was specified on the
1338 * command line.
1339 */
Eric Andersene674eb71999-10-19 20:52:57 +00001340 printf("%s, %s\n", program_name, program_version);
Erik Andersene49d5ec2000-02-08 19:58:47 +00001341 if (!(Super.s_state & MINIX_ERROR_FS) &&
1342 (Super.s_state & MINIX_VALID_FS) && !force) {
Eric Andersene494fdd1999-10-19 20:23:03 +00001343 if (repair)
Eric Andersene674eb71999-10-19 20:52:57 +00001344 printf("%s is clean, no check.\n", device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001345 return retcode;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001346 } else if (force)
Eric Andersene674eb71999-10-19 20:52:57 +00001347 printf("Forcing filesystem check on %s.\n", device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001348 else if (repair)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001349 printf("Filesystem on %s is dirty, needs checking.\n",
1350 device_name);
Eric Andersene494fdd1999-10-19 20:23:03 +00001351
1352 read_tables();
1353
1354 if (repair && !automatic) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001355 tcgetattr(0, &termios);
Eric Andersene494fdd1999-10-19 20:23:03 +00001356 tmp = termios;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001357 tmp.c_lflag &= ~(ICANON | ECHO);
1358 tcsetattr(0, TCSANOW, &tmp);
Eric Andersene494fdd1999-10-19 20:23:03 +00001359 termios_set = 1;
1360 }
Eric Andersene494fdd1999-10-19 20:23:03 +00001361#if HAVE_MINIX2
1362 if (version2) {
Erik Andersene49d5ec2000-02-08 19:58:47 +00001363 check_root2();
1364 check2();
1365 } else
Eric Andersene494fdd1999-10-19 20:23:03 +00001366#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +00001367 {
Eric Andersene494fdd1999-10-19 20:23:03 +00001368 check_root();
1369 check();
1370 }
1371 if (verbose) {
1372 int i, free;
1373
Erik Andersene49d5ec2000-02-08 19:58:47 +00001374 for (i = 1, free = 0; i <= INODES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001375 if (!inode_in_use(i))
1376 free++;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001377 printf("\n%6ld inodes used (%ld%%)\n", (INODES - free),
1378 100 * (INODES - free) / INODES);
1379 for (i = FIRSTZONE, free = 0; i < ZONES; i++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001380 if (!zone_in_use(i))
1381 free++;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001382 printf("%6ld zones used (%ld%%)\n", (ZONES - free),
1383 100 * (ZONES - free) / ZONES);
Eric Andersene674eb71999-10-19 20:52:57 +00001384 printf("\n%6d regular files\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +00001385 "%6d directories\n"
1386 "%6d character device files\n"
1387 "%6d block device files\n"
1388 "%6d links\n"
1389 "%6d symbolic links\n"
1390 "------\n"
1391 "%6d files\n",
1392 regular, directory, chardev, blockdev,
1393 links - 2 * directory + 1, symlinks,
1394 total - 2 * directory + 1);
Eric Andersene494fdd1999-10-19 20:23:03 +00001395 }
1396 if (changed) {
1397 write_tables();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001398 printf("----------------------------\n"
1399 "FILE SYSTEM HAS BEEN CHANGED\n"
1400 "----------------------------\n");
1401 for (count = 0; count < 3; count++)
Eric Andersene494fdd1999-10-19 20:23:03 +00001402 sync();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001403 } else if (repair)
Eric Andersene494fdd1999-10-19 20:23:03 +00001404 write_super_block();
Erik Andersene49d5ec2000-02-08 19:58:47 +00001405
Eric Andersene494fdd1999-10-19 20:23:03 +00001406 if (repair && !automatic)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001407 tcsetattr(0, TCSANOW, &termios);
Eric Andersene494fdd1999-10-19 20:23:03 +00001408
1409 if (changed)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001410 retcode += 3;
Eric Andersene494fdd1999-10-19 20:23:03 +00001411 if (errors_uncorrected)
Erik Andersene49d5ec2000-02-08 19:58:47 +00001412 retcode += 4;
Eric Andersene494fdd1999-10-19 20:23:03 +00001413 return retcode;
1414}