Some #include updates.
[oweals/busybox.git] / fsck_minix.c
1 /* vi: set sw=4 ts=4: */
2 /*
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
89 #include "internal.h"
90 #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/param.h>
100
101  
102  typedef unsigned char u8;
103 typedef unsigned short u16;
104 typedef unsigned int u32;
105
106
107 #define MINIX_ROOT_INO 1
108 #define MINIX_LINK_MAX  250
109 #define MINIX2_LINK_MAX 65530
110
111 #define MINIX_I_MAP_SLOTS       8
112 #define MINIX_Z_MAP_SLOTS       64
113 #define MINIX_SUPER_MAGIC       0x137F          /* original minix fs */
114 #define MINIX_SUPER_MAGIC2      0x138F          /* minix fs, 30 char names */
115 #define MINIX2_SUPER_MAGIC      0x2468          /* minix V2 fs */
116 #define MINIX2_SUPER_MAGIC2     0x2478          /* minix V2 fs, 30 char names */
117 #define MINIX_VALID_FS          0x0001          /* Clean fs. */
118 #define MINIX_ERROR_FS          0x0002          /* fs has errors. */
119
120 #define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
121 #define MINIX2_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix2_inode)))
122
123 #define MINIX_V1                0x0001          /* original minix fs */
124 #define MINIX_V2                0x0002          /* minix V2 fs */
125
126 #define INODE_VERSION(inode)    inode->i_sb->u.minix_sb.s_version
127
128 /*
129  * This is the original minix inode layout on disk.
130  * Note the 8-bit gid and atime and ctime.
131  */
132 struct minix_inode {
133         u16 i_mode;
134         u16 i_uid;
135         u32 i_size;
136         u32 i_time;
137         u8  i_gid;
138         u8  i_nlinks;
139         u16 i_zone[9];
140 };
141
142 /*
143  * The new minix inode has all the time entries, as well as
144  * long block numbers and a third indirect block (7+1+1+1
145  * instead of 7+1+1). Also, some previously 8-bit values are
146  * now 16-bit. The inode is now 64 bytes instead of 32.
147  */
148 struct minix2_inode {
149         u16 i_mode;
150         u16 i_nlinks;
151         u16 i_uid;
152         u16 i_gid;
153         u32 i_size;
154         u32 i_atime;
155         u32 i_mtime;
156         u32 i_ctime;
157         u32 i_zone[10];
158 };
159
160 /*
161  * minix super-block data on disk
162  */
163 struct minix_super_block {
164         u16 s_ninodes;
165         u16 s_nzones;
166         u16 s_imap_blocks;
167         u16 s_zmap_blocks;
168         u16 s_firstdatazone;
169         u16 s_log_zone_size;
170         u32 s_max_size;
171         u16 s_magic;
172         u16 s_state;
173         u32 s_zones;
174 };
175
176 struct minix_dir_entry {
177         u16 inode;
178         char name[0];
179 };
180
181 #define BLOCK_SIZE_BITS 10
182 #define BLOCK_SIZE (1<<BLOCK_SIZE_BITS)
183
184 #define NAME_MAX         255   /* # chars in a file name */
185
186 #define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
187
188 #define MINIX_VALID_FS               0x0001          /* Clean fs. */
189 #define MINIX_ERROR_FS               0x0002          /* fs has errors. */
190
191 #define MINIX_SUPER_MAGIC    0x137F          /* original minix fs */
192 #define MINIX_SUPER_MAGIC2   0x138F          /* minix fs, 30 char names */
193
194 #ifndef BLKGETSIZE
195 #define BLKGETSIZE _IO(0x12,96)    /* return device size */
196 #endif
197
198 #ifndef __linux__
199 #define volatile
200 #endif
201
202 #define ROOT_INO 1
203
204 #define UPPER(size,n) ((size+((n)-1))/(n))
205 #define INODE_SIZE (sizeof(struct minix_inode))
206 #ifdef BB_FEATURE_MINIX2
207 #define INODE_SIZE2 (sizeof(struct minix2_inode))
208 #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
209                                     : MINIX_INODES_PER_BLOCK))
210 #else
211 #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
212 #endif
213 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
214
215 #define BITS_PER_BLOCK (BLOCK_SIZE<<3)
216
217 static char *program_version = "1.2 - 11/11/96";
218 static char *device_name = NULL;
219 static int IN;
220 static int repair = 0, automatic = 0, verbose = 0, list = 0, show =
221         0, warn_mode = 0, force = 0;
222 static int directory = 0, regular = 0, blockdev = 0, chardev = 0, links =
223         0, symlinks = 0, total = 0;
224
225 static int changed = 0;                 /* flags if the filesystem has been changed */
226 static int errors_uncorrected = 0;      /* flag if some error was not corrected */
227 static int dirsize = 16;
228 static int namelen = 14;
229 static int version2 = 0;
230 static struct termios termios;
231 static int termios_set = 0;
232
233 /* File-name data */
234 #define MAX_DEPTH 32
235 static int name_depth = 0;
236 // static char name_list[MAX_DEPTH][BUFSIZ + 1];
237 static char **name_list = NULL;
238
239 static char *inode_buffer = NULL;
240
241 #define Inode (((struct minix_inode *) inode_buffer)-1)
242 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
243 static char super_block_buffer[BLOCK_SIZE];
244
245 #define Super (*(struct minix_super_block *)super_block_buffer)
246 #define INODES ((unsigned long)Super.s_ninodes)
247 #ifdef BB_FEATURE_MINIX2
248 #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
249 #else
250 #define ZONES ((unsigned long)(Super.s_nzones))
251 #endif
252 #define IMAPS ((unsigned long)Super.s_imap_blocks)
253 #define ZMAPS ((unsigned long)Super.s_zmap_blocks)
254 #define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
255 #define ZONESIZE ((unsigned long)Super.s_log_zone_size)
256 #define MAXSIZE ((unsigned long)Super.s_max_size)
257 #define MAGIC (Super.s_magic)
258 #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
259
260 static char *inode_map;
261 static char *zone_map;
262
263 static unsigned char *inode_count = NULL;
264 static unsigned char *zone_count = NULL;
265
266 static void recursive_check(unsigned int ino);
267 #ifdef BB_FEATURE_MINIX2
268 static void recursive_check2(unsigned int ino);
269 #endif
270
271 #define inode_in_use(x) (isset(inode_map,(x)))
272 #define zone_in_use(x) (isset(zone_map,(x)-FIRSTZONE+1))
273
274 #define mark_inode(x) (setbit(inode_map,(x)),changed=1)
275 #define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
276
277 #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1)
278 #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1)
279
280 static void leave(int) __attribute__ ((noreturn));
281 static void leave(int status)
282 {
283         if (termios_set)
284                 tcsetattr(0, TCSANOW, &termios);
285         exit(status);
286 }
287
288 static void show_usage(void)
289 {
290         usage(fsck_minix_usage);
291 }
292
293 static void die(const char *str)
294 {
295         errorMsg("%s\n", str);
296         leave(8);
297 }
298
299 /*
300  * This simply goes through the file-name data and prints out the
301  * current file.
302  */
303 static void print_current_name(void)
304 {
305         int i = 0;
306
307         while (i < name_depth)
308                 printf("/%.*s", namelen, name_list[i++]);
309         if (i == 0)
310                 printf("/");
311 }
312
313 static int ask(const char *string, int def)
314 {
315         int c;
316
317         if (!repair) {
318                 printf("\n");
319                 errors_uncorrected = 1;
320                 return 0;
321         }
322         if (automatic) {
323                 printf("\n");
324                 if (!def)
325                         errors_uncorrected = 1;
326                 return def;
327         }
328         printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
329         for (;;) {
330                 fflush(stdout);
331                 if ((c = getchar()) == EOF) {
332                         if (!def)
333                                 errors_uncorrected = 1;
334                         return def;
335                 }
336                 c = toupper(c);
337                 if (c == 'Y') {
338                         def = 1;
339                         break;
340                 } else if (c == 'N') {
341                         def = 0;
342                         break;
343                 } else if (c == ' ' || c == '\n')
344                         break;
345         }
346         if (def)
347                 printf("y\n");
348         else {
349                 printf("n\n");
350                 errors_uncorrected = 1;
351         }
352         return def;
353 }
354
355 /*
356  * Make certain that we aren't checking a filesystem that is on a
357  * mounted partition.  Code adapted from e2fsck, Copyright (C) 1993,
358  * 1994 Theodore Ts'o.  Also licensed under GPL.
359  */
360 static void check_mount(void)
361 {
362         FILE *f;
363         struct mntent *mnt;
364         int cont;
365         int fd;
366
367         if ((f = setmntent(MOUNTED, "r")) == NULL)
368                 return;
369         while ((mnt = getmntent(f)) != NULL)
370                 if (strcmp(device_name, mnt->mnt_fsname) == 0)
371                         break;
372         endmntent(f);
373         if (!mnt)
374                 return;
375
376         /*
377          * If the root is mounted read-only, then /etc/mtab is
378          * probably not correct; so we won't issue a warning based on
379          * it.
380          */
381         fd = open(MOUNTED, O_RDWR);
382         if (fd < 0 && errno == EROFS)
383                 return;
384         else
385                 close(fd);
386
387         printf("%s is mounted.   ", device_name);
388         if (isatty(0) && isatty(1))
389                 cont = ask("Do you really want to continue", 0);
390         else
391                 cont = 0;
392         if (!cont) {
393                 printf("check aborted.\n");
394                 exit(0);
395         }
396         return;
397 }
398
399 /*
400  * check_zone_nr checks to see that *nr is a valid zone nr. If it
401  * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
402  * if an error was corrected, and returns the zone (0 for no zone
403  * or a bad zone-number).
404  */
405 static int check_zone_nr(unsigned short *nr, int *corrected)
406 {
407         if (!*nr)
408                 return 0;
409         if (*nr < FIRSTZONE)
410                 printf("Zone nr < FIRSTZONE in file `");
411         else if (*nr >= ZONES)
412                 printf("Zone nr >= ZONES in file `");
413         else
414                 return *nr;
415         print_current_name();
416         printf("'.");
417         if (ask("Remove block", 1)) {
418                 *nr = 0;
419                 *corrected = 1;
420         }
421         return 0;
422 }
423
424 #ifdef BB_FEATURE_MINIX2
425 static int check_zone_nr2(unsigned int *nr, int *corrected)
426 {
427         if (!*nr)
428                 return 0;
429         if (*nr < FIRSTZONE)
430                 printf("Zone nr < FIRSTZONE in file `");
431         else if (*nr >= ZONES)
432                 printf("Zone nr >= ZONES in file `");
433         else
434                 return *nr;
435         print_current_name();
436         printf("'.");
437         if (ask("Remove block", 1)) {
438                 *nr = 0;
439                 *corrected = 1;
440         }
441         return 0;
442 }
443 #endif
444
445 /*
446  * read-block reads block nr into the buffer at addr.
447  */
448 static void read_block(unsigned int nr, char *addr)
449 {
450         if (!nr) {
451                 memset(addr, 0, BLOCK_SIZE);
452                 return;
453         }
454         if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) {
455                 printf("Read error: unable to seek to block in file '");
456                 print_current_name();
457                 printf("'\n");
458                 memset(addr, 0, BLOCK_SIZE);
459                 errors_uncorrected = 1;
460         } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) {
461                 printf("Read error: bad block in file '");
462                 print_current_name();
463                 printf("'\n");
464                 memset(addr, 0, BLOCK_SIZE);
465                 errors_uncorrected = 1;
466         }
467 }
468
469 /*
470  * write_block writes block nr to disk.
471  */
472 static void write_block(unsigned int nr, char *addr)
473 {
474         if (!nr)
475                 return;
476         if (nr < FIRSTZONE || nr >= ZONES) {
477                 printf("Internal error: trying to write bad block\n"
478                            "Write request ignored\n");
479                 errors_uncorrected = 1;
480                 return;
481         }
482         if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET))
483                 die("seek failed in write_block");
484         if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) {
485                 printf("Write error: bad block in file '");
486                 print_current_name();
487                 printf("'\n");
488                 errors_uncorrected = 1;
489         }
490 }
491
492 /*
493  * map-block calculates the absolute block nr of a block in a file.
494  * It sets 'changed' if the inode has needed changing, and re-writes
495  * any indirect blocks with errors.
496  */
497 static int map_block(struct minix_inode *inode, unsigned int blknr)
498 {
499         unsigned short ind[BLOCK_SIZE >> 1];
500         unsigned short dind[BLOCK_SIZE >> 1];
501         int blk_chg, block, result;
502
503         if (blknr < 7)
504                 return check_zone_nr(inode->i_zone + blknr, &changed);
505         blknr -= 7;
506         if (blknr < 512) {
507                 block = check_zone_nr(inode->i_zone + 7, &changed);
508                 read_block(block, (char *) ind);
509                 blk_chg = 0;
510                 result = check_zone_nr(blknr + ind, &blk_chg);
511                 if (blk_chg)
512                         write_block(block, (char *) ind);
513                 return result;
514         }
515         blknr -= 512;
516         block = check_zone_nr(inode->i_zone + 8, &changed);
517         read_block(block, (char *) dind);
518         blk_chg = 0;
519         result = check_zone_nr(dind + (blknr / 512), &blk_chg);
520         if (blk_chg)
521                 write_block(block, (char *) dind);
522         block = result;
523         read_block(block, (char *) ind);
524         blk_chg = 0;
525         result = check_zone_nr(ind + (blknr % 512), &blk_chg);
526         if (blk_chg)
527                 write_block(block, (char *) ind);
528         return result;
529 }
530
531 #ifdef BB_FEATURE_MINIX2
532 static int map_block2(struct minix2_inode *inode, unsigned int blknr)
533 {
534         unsigned int ind[BLOCK_SIZE >> 2];
535         unsigned int dind[BLOCK_SIZE >> 2];
536         unsigned int tind[BLOCK_SIZE >> 2];
537         int blk_chg, block, result;
538
539         if (blknr < 7)
540                 return check_zone_nr2(inode->i_zone + blknr, &changed);
541         blknr -= 7;
542         if (blknr < 256) {
543                 block = check_zone_nr2(inode->i_zone + 7, &changed);
544                 read_block(block, (char *) ind);
545                 blk_chg = 0;
546                 result = check_zone_nr2(blknr + ind, &blk_chg);
547                 if (blk_chg)
548                         write_block(block, (char *) ind);
549                 return result;
550         }
551         blknr -= 256;
552         if (blknr >= 256 * 256) {
553                 block = check_zone_nr2(inode->i_zone + 8, &changed);
554                 read_block(block, (char *) dind);
555                 blk_chg = 0;
556                 result = check_zone_nr2(dind + blknr / 256, &blk_chg);
557                 if (blk_chg)
558                         write_block(block, (char *) dind);
559                 block = result;
560                 read_block(block, (char *) ind);
561                 blk_chg = 0;
562                 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
563                 if (blk_chg)
564                         write_block(block, (char *) ind);
565                 return result;
566         }
567         blknr -= 256 * 256;
568         block = check_zone_nr2(inode->i_zone + 9, &changed);
569         read_block(block, (char *) tind);
570         blk_chg = 0;
571         result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
572         if (blk_chg)
573                 write_block(block, (char *) tind);
574         block = result;
575         read_block(block, (char *) dind);
576         blk_chg = 0;
577         result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
578         if (blk_chg)
579                 write_block(block, (char *) dind);
580         block = result;
581         read_block(block, (char *) ind);
582         blk_chg = 0;
583         result = check_zone_nr2(ind + blknr % 256, &blk_chg);
584         if (blk_chg)
585                 write_block(block, (char *) ind);
586         return result;
587 }
588 #endif
589
590 static void write_super_block(void)
591 {
592         /*
593          * Set the state of the filesystem based on whether or not there
594          * are uncorrected errors.  The filesystem valid flag is
595          * unconditionally set if we get this far.
596          */
597         Super.s_state |= MINIX_VALID_FS;
598         if (errors_uncorrected)
599                 Super.s_state |= MINIX_ERROR_FS;
600         else
601                 Super.s_state &= ~MINIX_ERROR_FS;
602
603         if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
604                 die("seek failed in write_super_block");
605         if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE))
606                 die("unable to write super-block");
607
608         return;
609 }
610
611 static void write_tables(void)
612 {
613         write_super_block();
614
615         if (IMAPS * BLOCK_SIZE != write(IN, inode_map, IMAPS * BLOCK_SIZE))
616                 die("Unable to write inode map");
617         if (ZMAPS * BLOCK_SIZE != write(IN, zone_map, ZMAPS * BLOCK_SIZE))
618                 die("Unable to write zone map");
619         if (INODE_BUFFER_SIZE != write(IN, inode_buffer, INODE_BUFFER_SIZE))
620                 die("Unable to write inodes");
621 }
622
623 static void get_dirsize(void)
624 {
625         int block;
626         char blk[BLOCK_SIZE];
627         int size;
628
629 #ifdef BB_FEATURE_MINIX2
630         if (version2)
631                 block = Inode2[ROOT_INO].i_zone[0];
632         else
633 #endif
634                 block = Inode[ROOT_INO].i_zone[0];
635         read_block(block, blk);
636         for (size = 16; size < BLOCK_SIZE; size <<= 1) {
637                 if (strcmp(blk + size + 2, "..") == 0) {
638                         dirsize = size;
639                         namelen = size - 2;
640                         return;
641                 }
642         }
643         /* use defaults */
644 }
645
646 static void read_superblock(void)
647 {
648         if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
649                 die("seek failed");
650         if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
651                 die("unable to read super block");
652         if (MAGIC == MINIX_SUPER_MAGIC) {
653                 namelen = 14;
654                 dirsize = 16;
655                 version2 = 0;
656         } else if (MAGIC == MINIX_SUPER_MAGIC2) {
657                 namelen = 30;
658                 dirsize = 32;
659                 version2 = 0;
660 #ifdef BB_FEATURE_MINIX2
661         } else if (MAGIC == MINIX2_SUPER_MAGIC) {
662                 namelen = 14;
663                 dirsize = 16;
664                 version2 = 1;
665         } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
666                 namelen = 30;
667                 dirsize = 32;
668                 version2 = 1;
669 #endif
670         } else
671                 die("bad magic number in super-block");
672         if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
673                 die("Only 1k blocks/zones supported");
674         if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
675                 die("bad s_imap_blocks field in super-block");
676         if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
677                 die("bad s_zmap_blocks field in super-block");
678 }
679
680 static void read_tables(void)
681 {
682         inode_map = xmalloc(IMAPS * BLOCK_SIZE);
683         zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
684         memset(inode_map, 0, sizeof(inode_map));
685         memset(zone_map, 0, sizeof(zone_map));
686         inode_buffer = xmalloc(INODE_BUFFER_SIZE);
687         inode_count = xmalloc(INODES + 1);
688         zone_count = xmalloc(ZONES);
689         if (IMAPS * BLOCK_SIZE != read(IN, inode_map, IMAPS * BLOCK_SIZE))
690                 die("Unable to read inode map");
691         if (ZMAPS * BLOCK_SIZE != read(IN, zone_map, ZMAPS * BLOCK_SIZE))
692                 die("Unable to read zone map");
693         if (INODE_BUFFER_SIZE != read(IN, inode_buffer, INODE_BUFFER_SIZE))
694                 die("Unable to read inodes");
695         if (NORM_FIRSTZONE != FIRSTZONE) {
696                 printf("Warning: Firstzone != Norm_firstzone\n");
697                 errors_uncorrected = 1;
698         }
699         get_dirsize();
700         if (show) {
701                 printf("%ld inodes\n", INODES);
702                 printf("%ld blocks\n", ZONES);
703                 printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
704                 printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
705                 printf("Maxsize=%ld\n", MAXSIZE);
706                 printf("Filesystem state=%d\n", Super.s_state);
707                 printf("namelen=%d\n\n", namelen);
708         }
709 }
710
711 struct minix_inode *get_inode(unsigned int nr)
712 {
713         struct minix_inode *inode;
714
715         if (!nr || nr > INODES)
716                 return NULL;
717         total++;
718         inode = Inode + nr;
719         if (!inode_count[nr]) {
720                 if (!inode_in_use(nr)) {
721                         printf("Inode %d marked not used, but used for file '", nr);
722                         print_current_name();
723                         printf("'\n");
724                         if (repair) {
725                                 if (ask("Mark in use", 1))
726                                         mark_inode(nr);
727                         } else {
728                                 errors_uncorrected = 1;
729                         }
730                 }
731                 if (S_ISDIR(inode->i_mode))
732                         directory++;
733                 else if (S_ISREG(inode->i_mode))
734                         regular++;
735                 else if (S_ISCHR(inode->i_mode))
736                         chardev++;
737                 else if (S_ISBLK(inode->i_mode))
738                         blockdev++;
739                 else if (S_ISLNK(inode->i_mode))
740                         symlinks++;
741                 else if (S_ISSOCK(inode->i_mode));
742                 else if (S_ISFIFO(inode->i_mode));
743                 else {
744                         print_current_name();
745                         printf(" has mode %05o\n", inode->i_mode);
746                 }
747
748         } else
749                 links++;
750         if (!++inode_count[nr]) {
751                 printf("Warning: inode count too big.\n");
752                 inode_count[nr]--;
753                 errors_uncorrected = 1;
754         }
755         return inode;
756 }
757
758 #ifdef BB_FEATURE_MINIX2
759 struct minix2_inode *get_inode2(unsigned int nr)
760 {
761         struct minix2_inode *inode;
762
763         if (!nr || nr > INODES)
764                 return NULL;
765         total++;
766         inode = Inode2 + nr;
767         if (!inode_count[nr]) {
768                 if (!inode_in_use(nr)) {
769                         printf("Inode %d marked not used, but used for file '", nr);
770                         print_current_name();
771                         printf("'\n");
772                         if (repair) {
773                                 if (ask("Mark in use", 1))
774                                         mark_inode(nr);
775                                 else
776                                         errors_uncorrected = 1;
777                         }
778                 }
779                 if (S_ISDIR(inode->i_mode))
780                         directory++;
781                 else if (S_ISREG(inode->i_mode))
782                         regular++;
783                 else if (S_ISCHR(inode->i_mode))
784                         chardev++;
785                 else if (S_ISBLK(inode->i_mode))
786                         blockdev++;
787                 else if (S_ISLNK(inode->i_mode))
788                         symlinks++;
789                 else if (S_ISSOCK(inode->i_mode));
790                 else if (S_ISFIFO(inode->i_mode));
791                 else {
792                         print_current_name();
793                         printf(" has mode %05o\n", inode->i_mode);
794                 }
795         } else
796                 links++;
797         if (!++inode_count[nr]) {
798                 printf("Warning: inode count too big.\n");
799                 inode_count[nr]--;
800                 errors_uncorrected = 1;
801         }
802         return inode;
803 }
804 #endif
805
806 static void check_root(void)
807 {
808         struct minix_inode *inode = Inode + ROOT_INO;
809
810         if (!inode || !S_ISDIR(inode->i_mode))
811                 die("root inode isn't a directory");
812 }
813
814 #ifdef BB_FEATURE_MINIX2
815 static void check_root2(void)
816 {
817         struct minix2_inode *inode = Inode2 + ROOT_INO;
818
819         if (!inode || !S_ISDIR(inode->i_mode))
820                 die("root inode isn't a directory");
821 }
822 #endif
823
824 static int add_zone(unsigned short *znr, int *corrected)
825 {
826         int result;
827         int block;
828
829         result = 0;
830         block = check_zone_nr(znr, corrected);
831         if (!block)
832                 return 0;
833         if (zone_count[block]) {
834                 printf("Block has been used before. Now in file `");
835                 print_current_name();
836                 printf("'.");
837                 if (ask("Clear", 1)) {
838                         *znr = 0;
839                         block = 0;
840                         *corrected = 1;
841                 }
842         }
843         if (!block)
844                 return 0;
845         if (!zone_in_use(block)) {
846                 printf("Block %d in file `", block);
847                 print_current_name();
848                 printf("' is marked not in use.");
849                 if (ask("Correct", 1))
850                         mark_zone(block);
851         }
852         if (!++zone_count[block])
853                 zone_count[block]--;
854         return block;
855 }
856
857 #ifdef BB_FEATURE_MINIX2
858 static int add_zone2(unsigned int *znr, int *corrected)
859 {
860         int result;
861         int block;
862
863         result = 0;
864         block = check_zone_nr2(znr, corrected);
865         if (!block)
866                 return 0;
867         if (zone_count[block]) {
868                 printf("Block has been used before. Now in file `");
869                 print_current_name();
870                 printf("'.");
871                 if (ask("Clear", 1)) {
872                         *znr = 0;
873                         block = 0;
874                         *corrected = 1;
875                 }
876         }
877         if (!block)
878                 return 0;
879         if (!zone_in_use(block)) {
880                 printf("Block %d in file `", block);
881                 print_current_name();
882                 printf("' is marked not in use.");
883                 if (ask("Correct", 1))
884                         mark_zone(block);
885         }
886         if (!++zone_count[block])
887                 zone_count[block]--;
888         return block;
889 }
890 #endif
891
892 static void add_zone_ind(unsigned short *znr, int *corrected)
893 {
894         static char blk[BLOCK_SIZE];
895         int i, chg_blk = 0;
896         int block;
897
898         block = add_zone(znr, corrected);
899         if (!block)
900                 return;
901         read_block(block, blk);
902         for (i = 0; i < (BLOCK_SIZE >> 1); i++)
903                 add_zone(i + (unsigned short *) blk, &chg_blk);
904         if (chg_blk)
905                 write_block(block, blk);
906 }
907
908 #ifdef BB_FEATURE_MINIX2
909 static void add_zone_ind2(unsigned int *znr, int *corrected)
910 {
911         static char blk[BLOCK_SIZE];
912         int i, chg_blk = 0;
913         int block;
914
915         block = add_zone2(znr, corrected);
916         if (!block)
917                 return;
918         read_block(block, blk);
919         for (i = 0; i < BLOCK_SIZE >> 2; i++)
920                 add_zone2(i + (unsigned int *) blk, &chg_blk);
921         if (chg_blk)
922                 write_block(block, blk);
923 }
924 #endif
925
926 static void add_zone_dind(unsigned short *znr, int *corrected)
927 {
928         static char blk[BLOCK_SIZE];
929         int i, blk_chg = 0;
930         int block;
931
932         block = add_zone(znr, corrected);
933         if (!block)
934                 return;
935         read_block(block, blk);
936         for (i = 0; i < (BLOCK_SIZE >> 1); i++)
937                 add_zone_ind(i + (unsigned short *) blk, &blk_chg);
938         if (blk_chg)
939                 write_block(block, blk);
940 }
941
942 #ifdef BB_FEATURE_MINIX2
943 static void add_zone_dind2(unsigned int *znr, int *corrected)
944 {
945         static char blk[BLOCK_SIZE];
946         int i, blk_chg = 0;
947         int block;
948
949         block = add_zone2(znr, corrected);
950         if (!block)
951                 return;
952         read_block(block, blk);
953         for (i = 0; i < BLOCK_SIZE >> 2; i++)
954                 add_zone_ind2(i + (unsigned int *) blk, &blk_chg);
955         if (blk_chg)
956                 write_block(block, blk);
957 }
958
959 static void add_zone_tind2(unsigned int *znr, int *corrected)
960 {
961         static char blk[BLOCK_SIZE];
962         int i, blk_chg = 0;
963         int block;
964
965         block = add_zone2(znr, corrected);
966         if (!block)
967                 return;
968         read_block(block, blk);
969         for (i = 0; i < BLOCK_SIZE >> 2; i++)
970                 add_zone_dind2(i + (unsigned int *) blk, &blk_chg);
971         if (blk_chg)
972                 write_block(block, blk);
973 }
974 #endif
975
976 static void check_zones(unsigned int i)
977 {
978         struct minix_inode *inode;
979
980         if (!i || i > INODES)
981                 return;
982         if (inode_count[i] > 1)         /* have we counted this file already? */
983                 return;
984         inode = Inode + i;
985         if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
986                 !S_ISLNK(inode->i_mode)) return;
987         for (i = 0; i < 7; i++)
988                 add_zone(i + inode->i_zone, &changed);
989         add_zone_ind(7 + inode->i_zone, &changed);
990         add_zone_dind(8 + inode->i_zone, &changed);
991 }
992
993 #ifdef BB_FEATURE_MINIX2
994 static void check_zones2(unsigned int i)
995 {
996         struct minix2_inode *inode;
997
998         if (!i || i > INODES)
999                 return;
1000         if (inode_count[i] > 1)         /* have we counted this file already? */
1001                 return;
1002         inode = Inode2 + i;
1003         if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
1004                 && !S_ISLNK(inode->i_mode))
1005                 return;
1006         for (i = 0; i < 7; i++)
1007                 add_zone2(i + inode->i_zone, &changed);
1008         add_zone_ind2(7 + inode->i_zone, &changed);
1009         add_zone_dind2(8 + inode->i_zone, &changed);
1010         add_zone_tind2(9 + inode->i_zone, &changed);
1011 }
1012 #endif
1013
1014 static void check_file(struct minix_inode *dir, unsigned int offset)
1015 {
1016         static char blk[BLOCK_SIZE];
1017         struct minix_inode *inode;
1018         int ino;
1019         char *name;
1020         int block;
1021
1022         block = map_block(dir, offset / BLOCK_SIZE);
1023         read_block(block, blk);
1024         name = blk + (offset % BLOCK_SIZE) + 2;
1025         ino = *(unsigned short *) (name - 2);
1026         if (ino > INODES) {
1027                 print_current_name();
1028                 printf(" contains a bad inode number for file '");
1029                 printf("%.*s'.", namelen, name);
1030                 if (ask(" Remove", 1)) {
1031                         *(unsigned short *) (name - 2) = 0;
1032                         write_block(block, blk);
1033                 }
1034                 ino = 0;
1035         }
1036         if (name_depth < MAX_DEPTH)
1037                 strncpy(name_list[name_depth], name, namelen);
1038         name_depth++;
1039         inode = get_inode(ino);
1040         name_depth--;
1041         if (!offset) {
1042                 if (!inode || strcmp(".", name)) {
1043                         print_current_name();
1044                         printf(": bad directory: '.' isn't first\n");
1045                         errors_uncorrected = 1;
1046                 } else
1047                         return;
1048         }
1049         if (offset == dirsize) {
1050                 if (!inode || strcmp("..", name)) {
1051                         print_current_name();
1052                         printf(": bad directory: '..' isn't second\n");
1053                         errors_uncorrected = 1;
1054                 } else
1055                         return;
1056         }
1057         if (!inode)
1058                 return;
1059         if (name_depth < MAX_DEPTH)
1060                 strncpy(name_list[name_depth], name, namelen);
1061         name_depth++;
1062         if (list) {
1063                 if (verbose)
1064                         printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1065                 print_current_name();
1066                 if (S_ISDIR(inode->i_mode))
1067                         printf(":\n");
1068                 else
1069                         printf("\n");
1070         }
1071         check_zones(ino);
1072         if (inode && S_ISDIR(inode->i_mode))
1073                 recursive_check(ino);
1074         name_depth--;
1075         return;
1076 }
1077
1078 #ifdef BB_FEATURE_MINIX2
1079 static void check_file2(struct minix2_inode *dir, unsigned int offset)
1080 {
1081         static char blk[BLOCK_SIZE];
1082         struct minix2_inode *inode;
1083         int ino;
1084         char *name;
1085         int block;
1086
1087         block = map_block2(dir, offset / BLOCK_SIZE);
1088         read_block(block, blk);
1089         name = blk + (offset % BLOCK_SIZE) + 2;
1090         ino = *(unsigned short *) (name - 2);
1091         if (ino > INODES) {
1092                 print_current_name();
1093                 printf(" contains a bad inode number for file '");
1094                 printf("%.*s'.", namelen, name);
1095                 if (ask(" Remove", 1)) {
1096                         *(unsigned short *) (name - 2) = 0;
1097                         write_block(block, blk);
1098                 }
1099                 ino = 0;
1100         }
1101         if (name_depth < MAX_DEPTH)
1102                 strncpy(name_list[name_depth], name, namelen);
1103         name_depth++;
1104         inode = get_inode2(ino);
1105         name_depth--;
1106         if (!offset) {
1107                 if (!inode || strcmp(".", name)) {
1108                         print_current_name();
1109                         printf(": bad directory: '.' isn't first\n");
1110                         errors_uncorrected = 1;
1111                 } else
1112                         return;
1113         }
1114         if (offset == dirsize) {
1115                 if (!inode || strcmp("..", name)) {
1116                         print_current_name();
1117                         printf(": bad directory: '..' isn't second\n");
1118                         errors_uncorrected = 1;
1119                 } else
1120                         return;
1121         }
1122         if (!inode)
1123                 return;
1124         name_depth++;
1125         if (list) {
1126                 if (verbose)
1127                         printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1128                 print_current_name();
1129                 if (S_ISDIR(inode->i_mode))
1130                         printf(":\n");
1131                 else
1132                         printf("\n");
1133         }
1134         check_zones2(ino);
1135         if (inode && S_ISDIR(inode->i_mode))
1136                 recursive_check2(ino);
1137         name_depth--;
1138         return;
1139 }
1140 #endif
1141
1142 static void recursive_check(unsigned int ino)
1143 {
1144         struct minix_inode *dir;
1145         unsigned int offset;
1146
1147         dir = Inode + ino;
1148         if (!S_ISDIR(dir->i_mode))
1149                 die("internal error");
1150         if (dir->i_size < 2 * dirsize) {
1151                 print_current_name();
1152                 printf(": bad directory: size<32");
1153                 errors_uncorrected = 1;
1154         }
1155         for (offset = 0; offset < dir->i_size; offset += dirsize)
1156                 check_file(dir, offset);
1157 }
1158
1159 #ifdef BB_FEATURE_MINIX2
1160 static void recursive_check2(unsigned int ino)
1161 {
1162         struct minix2_inode *dir;
1163         unsigned int offset;
1164
1165         dir = Inode2 + ino;
1166         if (!S_ISDIR(dir->i_mode))
1167                 die("internal error");
1168         if (dir->i_size < 2 * dirsize) {
1169                 print_current_name();
1170                 printf(": bad directory: size < 32");
1171                 errors_uncorrected = 1;
1172         }
1173         for (offset = 0; offset < dir->i_size; offset += dirsize)
1174                 check_file2(dir, offset);
1175 }
1176 #endif
1177
1178 static int bad_zone(int i)
1179 {
1180         char buffer[1024];
1181
1182         if (BLOCK_SIZE * i != lseek(IN, BLOCK_SIZE * i, SEEK_SET))
1183                 die("seek failed in bad_zone");
1184         return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE));
1185 }
1186
1187 static void check_counts(void)
1188 {
1189         int i;
1190
1191         for (i = 1; i <= INODES; i++) {
1192                 if (!inode_in_use(i) && Inode[i].i_mode && warn_mode) {
1193                         printf("Inode %d mode not cleared.", i);
1194                         if (ask("Clear", 1)) {
1195                                 Inode[i].i_mode = 0;
1196                                 changed = 1;
1197                         }
1198                 }
1199                 if (!inode_count[i]) {
1200                         if (!inode_in_use(i))
1201                                 continue;
1202                         printf("Inode %d not used, marked used in the bitmap.", i);
1203                         if (ask("Clear", 1))
1204                                 unmark_inode(i);
1205                         continue;
1206                 }
1207                 if (!inode_in_use(i)) {
1208                         printf("Inode %d used, marked unused in the bitmap.", i);
1209                         if (ask("Set", 1))
1210                                 mark_inode(i);
1211                 }
1212                 if (Inode[i].i_nlinks != inode_count[i]) {
1213                         printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
1214                                    i, Inode[i].i_mode, Inode[i].i_nlinks, inode_count[i]);
1215                         if (ask("Set i_nlinks to count", 1)) {
1216                                 Inode[i].i_nlinks = inode_count[i];
1217                                 changed = 1;
1218                         }
1219                 }
1220         }
1221         for (i = FIRSTZONE; i < ZONES; i++) {
1222                 if (zone_in_use(i) == zone_count[i])
1223                         continue;
1224                 if (!zone_count[i]) {
1225                         if (bad_zone(i))
1226                                 continue;
1227                         printf("Zone %d: marked in use, no file uses it.", i);
1228                         if (ask("Unmark", 1))
1229                                 unmark_zone(i);
1230                         continue;
1231                 }
1232                 printf("Zone %d: %sin use, counted=%d\n",
1233                            i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1234         }
1235 }
1236
1237 #ifdef BB_FEATURE_MINIX2
1238 static void check_counts2(void)
1239 {
1240         int i;
1241
1242         for (i = 1; i <= INODES; i++) {
1243                 if (!inode_in_use(i) && Inode2[i].i_mode && warn_mode) {
1244                         printf("Inode %d mode not cleared.", i);
1245                         if (ask("Clear", 1)) {
1246                                 Inode2[i].i_mode = 0;
1247                                 changed = 1;
1248                         }
1249                 }
1250                 if (!inode_count[i]) {
1251                         if (!inode_in_use(i))
1252                                 continue;
1253                         printf("Inode %d not used, marked used in the bitmap.", i);
1254                         if (ask("Clear", 1))
1255                                 unmark_inode(i);
1256                         continue;
1257                 }
1258                 if (!inode_in_use(i)) {
1259                         printf("Inode %d used, marked unused in the bitmap.", i);
1260                         if (ask("Set", 1))
1261                                 mark_inode(i);
1262                 }
1263                 if (Inode2[i].i_nlinks != inode_count[i]) {
1264                         printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
1265                                    i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1266                                    inode_count[i]);
1267                         if (ask("Set i_nlinks to count", 1)) {
1268                                 Inode2[i].i_nlinks = inode_count[i];
1269                                 changed = 1;
1270                         }
1271                 }
1272         }
1273         for (i = FIRSTZONE; i < ZONES; i++) {
1274                 if (zone_in_use(i) == zone_count[i])
1275                         continue;
1276                 if (!zone_count[i]) {
1277                         if (bad_zone(i))
1278                                 continue;
1279                         printf("Zone %d: marked in use, no file uses it.", i);
1280                         if (ask("Unmark", 1))
1281                                 unmark_zone(i);
1282                         continue;
1283                 }
1284                 printf("Zone %d: %sin use, counted=%d\n",
1285                            i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1286         }
1287 }
1288 #endif
1289
1290 static void check(void)
1291 {
1292         memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1293         memset(zone_count, 0, ZONES * sizeof(*zone_count));
1294         check_zones(ROOT_INO);
1295         recursive_check(ROOT_INO);
1296         check_counts();
1297 }
1298
1299 #ifdef BB_FEATURE_MINIX2
1300 static void check2(void)
1301 {
1302         memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1303         memset(zone_count, 0, ZONES * sizeof(*zone_count));
1304         check_zones2(ROOT_INO);
1305         recursive_check2(ROOT_INO);
1306         check_counts2();
1307 }
1308 #endif
1309
1310 /* Wed Feb  9 15:17:06 MST 2000 */
1311 /* dynamically allocate name_list (instead of making it static) */
1312 static void alloc_name_list(void)
1313 {
1314         int i;
1315
1316         name_list = xmalloc(sizeof(char *) * MAX_DEPTH);
1317         for (i = 0; i < MAX_DEPTH; i++)
1318                 name_list[i] = xmalloc(sizeof(char) * BUFSIZ + 1);
1319 }
1320
1321 #ifdef BB_FEATURE_CLEAN_UP
1322 /* execute this atexit() to deallocate name_list[] */
1323 /* piptigger was here */
1324 static void free_name_list(void)
1325 {
1326         int i;
1327
1328         if (name_list) { 
1329                 for (i = 0; i < MAX_DEPTH; i++) {
1330                         if (name_list[i]) {
1331                                 free(name_list[i]);
1332                         }
1333                 }
1334                 free(name_list);
1335         }
1336 }
1337 #endif
1338
1339 extern int fsck_minix_main(int argc, char **argv)
1340 {
1341         struct termios tmp;
1342         int count;
1343         int retcode = 0;
1344
1345         alloc_name_list();
1346 #ifdef BB_FEATURE_CLEAN_UP
1347         /* Don't bother to free memory.  Exit does
1348          * that automagically, so we can save a few bytes */
1349         atexit(free_name_list);
1350 #endif
1351
1352         if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
1353                 die("bad inode size");
1354 #ifdef BB_FEATURE_MINIX2
1355         if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
1356                 die("bad v2 inode size");
1357 #endif
1358         while (argc-- > 1) {
1359                 argv++;
1360                 if (argv[0][0] != '-') {
1361                         if (device_name)
1362                                 show_usage();
1363                         else
1364                                 device_name = argv[0];
1365                 } else
1366                         while (*++argv[0])
1367                                 switch (argv[0][0]) {
1368                                 case 'l':
1369                                         list = 1;
1370                                         break;
1371                                 case 'a':
1372                                         automatic = 1;
1373                                         repair = 1;
1374                                         break;
1375                                 case 'r':
1376                                         automatic = 0;
1377                                         repair = 1;
1378                                         break;
1379                                 case 'v':
1380                                         verbose = 1;
1381                                         break;
1382                                 case 's':
1383                                         show = 1;
1384                                         break;
1385                                 case 'm':
1386                                         warn_mode = 1;
1387                                         break;
1388                                 case 'f':
1389                                         force = 1;
1390                                         break;
1391                                 default:
1392                                         show_usage();
1393                                 }
1394         }
1395         if (!device_name)
1396                 show_usage();
1397         check_mount();                          /* trying to check a mounted filesystem? */
1398         if (repair && !automatic) {
1399                 if (!isatty(0) || !isatty(1))
1400                         die("need terminal for interactive repairs");
1401         }
1402         IN = open(device_name, repair ? O_RDWR : O_RDONLY);
1403         if (IN < 0)
1404                 die("unable to open '%s'");
1405         for (count = 0; count < 3; count++)
1406                 sync();
1407         read_superblock();
1408
1409         /*
1410          * Determine whether or not we should continue with the checking.
1411          * This is based on the status of the filesystem valid and error
1412          * flags and whether or not the -f switch was specified on the 
1413          * command line.
1414          */
1415         printf("%s, %s\n", applet_name, program_version);
1416         if (!(Super.s_state & MINIX_ERROR_FS) &&
1417                 (Super.s_state & MINIX_VALID_FS) && !force) {
1418                 if (repair)
1419                         printf("%s is clean, no check.\n", device_name);
1420                 return retcode;
1421         } else if (force)
1422                 printf("Forcing filesystem check on %s.\n", device_name);
1423         else if (repair)
1424                 printf("Filesystem on %s is dirty, needs checking.\n",
1425                            device_name);
1426
1427         read_tables();
1428
1429         if (repair && !automatic) {
1430                 tcgetattr(0, &termios);
1431                 tmp = termios;
1432                 tmp.c_lflag &= ~(ICANON | ECHO);
1433                 tcsetattr(0, TCSANOW, &tmp);
1434                 termios_set = 1;
1435         }
1436 #ifdef BB_FEATURE_MINIX2
1437         if (version2) {
1438                 check_root2();
1439                 check2();
1440         } else
1441 #endif
1442         {
1443                 check_root();
1444                 check();
1445         }
1446         if (verbose) {
1447                 int i, free;
1448
1449                 for (i = 1, free = 0; i <= INODES; i++)
1450                         if (!inode_in_use(i))
1451                                 free++;
1452                 printf("\n%6ld inodes used (%ld%%)\n", (INODES - free),
1453                            100 * (INODES - free) / INODES);
1454                 for (i = FIRSTZONE, free = 0; i < ZONES; i++)
1455                         if (!zone_in_use(i))
1456                                 free++;
1457                 printf("%6ld zones used (%ld%%)\n", (ZONES - free),
1458                            100 * (ZONES - free) / ZONES);
1459                 printf("\n%6d regular files\n"
1460                            "%6d directories\n"
1461                            "%6d character device files\n"
1462                            "%6d block device files\n"
1463                            "%6d links\n"
1464                            "%6d symbolic links\n"
1465                            "------\n"
1466                            "%6d files\n",
1467                            regular, directory, chardev, blockdev,
1468                            links - 2 * directory + 1, symlinks,
1469                            total - 2 * directory + 1);
1470         }
1471         if (changed) {
1472                 write_tables();
1473                 printf("----------------------------\n"
1474                            "FILE SYSTEM HAS BEEN CHANGED\n"
1475                            "----------------------------\n");
1476                 for (count = 0; count < 3; count++)
1477                         sync();
1478         } else if (repair)
1479                 write_super_block();
1480
1481         if (repair && !automatic)
1482                 tcsetattr(0, TCSANOW, &termios);
1483
1484         if (changed)
1485                 retcode += 3;
1486         if (errors_uncorrected)
1487                 retcode += 4;
1488         return retcode;
1489 }