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