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