1 /* vi: set sw=4 ts=4: */
3 * fsck.c - a file system consistency checker for Linux.
5 * (C) 1991, 1992 Linus Torvalds. This file may be redistributed
6 * as per the GNU copyleft.
10 * 09.11.91 - made the first rudimetary functions
12 * 10.11.91 - updated, does checking, no repairs yet.
13 * Sent out to the mailing-list for testing.
15 * 14.11.91 - Testing seems to have gone well. Added some
16 * correction-code, and changed some functions.
18 * 15.11.91 - More correction code. Hopefully it notices most
19 * cases now, and tries to do something about them.
21 * 16.11.91 - More corrections (thanks to Mika Jalava). Most
22 * things seem to work now. Yeah, sure.
25 * 19.04.92 - Had to start over again from this old version, as a
26 * kernel bug ate my enhanced fsck in february.
28 * 28.02.93 - added support for different directory entry sizes..
30 * Sat Mar 6 18:59:42 1993, faith@cs.unc.edu: Output namelen with
31 * super-block information
33 * Sat Oct 9 11:17:11 1993, faith@cs.unc.edu: make exit status conform
34 * to that required by fsutil
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
42 * 30.10.94 - added support for v2 filesystem
43 * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de)
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)
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)
52 * 02.07.96 - Added C bit fiddling routines from rmk@ecs.soton.ac.uk
53 * (Russell King). He made them for ARM. It would seem
54 * that the ARM is powerful enough to do this in C whereas
55 * i386 and m64k must use assembly to get it fast >:-)
56 * This should make minix fsck systemindependent.
57 * (janl@math.uio.no, Nicolai Langfeldt)
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>.
63 * 06.11.96 - Added v2 code submitted by Joerg Dorchain, but written by
66 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
67 * - added Native Language Support
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).
76 * Usuage: fsck [-larvsm] device
77 * -l for a listing of all the filenames
78 * -a for automatic repairs (not implemented)
79 * -r for repairs (interactive) (not implemented)
80 * -v for verbose (tells how many files)
81 * -s for super-block info
82 * -m for minix-like "mode not cleared" warnings
83 * -f force filesystem check even if filesystem marked as valid
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 :-).
100 #include <sys/param.h> /* for PATH_MAX */
102 #include <linux/fs.h>
103 #include <linux/minix_fs.h>
105 #ifdef MINIX2_SUPER_MAGIC2
106 #define HAVE_MINIX2 1
115 #define UPPER(size,n) ((size+((n)-1))/(n))
116 #define INODE_SIZE (sizeof(struct minix_inode))
118 #define INODE_SIZE2 (sizeof(struct minix2_inode))
119 #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
120 : MINIX_INODES_PER_BLOCK))
122 #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
124 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
126 #define BITS_PER_BLOCK (BLOCK_SIZE<<3)
128 static char *program_name = "fsck.minix";
129 static char *program_version = "1.2 - 11/11/96";
130 static char *device_name = NULL;
132 static int repair = 0, automatic = 0, verbose = 0, list = 0, show =
133 0, warn_mode = 0, force = 0;
134 static int directory = 0, regular = 0, blockdev = 0, chardev = 0, links =
135 0, symlinks = 0, total = 0;
137 static int changed = 0; /* flags if the filesystem has been changed */
138 static int errors_uncorrected = 0; /* flag if some error was not corrected */
139 static int dirsize = 16;
140 static int namelen = 14;
141 static int version2 = 0;
142 static struct termios termios;
143 static int termios_set = 0;
147 static int name_depth = 0;
148 static char name_list[MAX_DEPTH][PATH_MAX + 1];
150 static char *inode_buffer = NULL;
152 #define Inode (((struct minix_inode *) inode_buffer)-1)
153 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
154 static char super_block_buffer[BLOCK_SIZE];
156 #define Super (*(struct minix_super_block *)super_block_buffer)
157 #define INODES ((unsigned long)Super.s_ninodes)
159 #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
161 #define ZONES ((unsigned long)(Super.s_nzones))
163 #define IMAPS ((unsigned long)Super.s_imap_blocks)
164 #define ZMAPS ((unsigned long)Super.s_zmap_blocks)
165 #define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
166 #define ZONESIZE ((unsigned long)Super.s_log_zone_size)
167 #define MAXSIZE ((unsigned long)Super.s_max_size)
168 #define MAGIC (Super.s_magic)
169 #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
171 static char *inode_map;
172 static char *zone_map;
174 static unsigned char *inode_count = NULL;
175 static unsigned char *zone_count = NULL;
177 static void recursive_check(unsigned int ino);
178 static void recursive_check2(unsigned int ino);
180 #define inode_in_use(x) (bit(inode_map,(x)))
181 #define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
183 #define mark_inode(x) (setbit(inode_map,(x)),changed=1)
184 #define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
186 #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1)
187 #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1)
189 static void leave(int) __attribute__ ((noreturn));
190 static void leave(int status)
193 tcsetattr(0, TCSANOW, &termios);
197 static void show_usage(void)
199 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
201 fprintf(stderr, "Usage: %s [-larvsmf] /dev/name\n\n", program_name);
203 "Performs a consistency check for MINIX filesystems.\n\n");
204 fprintf(stderr, "OPTIONS:\n");
205 fprintf(stderr, "\t-l\tLists all filenames\n");
206 fprintf(stderr, "\t-r\tPerform interactive repairs\n");
207 fprintf(stderr, "\t-a\tPerform automatic repairs\n");
208 fprintf(stderr, "\t-v\tverbose\n");
209 fprintf(stderr, "\t-s\tOutputs super-block information\n");
211 "\t-m\tActivates MINIX-like \"mode not cleared\" warnings\n");
212 fprintf(stderr, "\t-f\tForce file system check.\n\n");
216 static void die(const char *str)
218 fprintf(stderr, "%s: %s\n", program_name, str);
223 * This simply goes through the file-name data and prints out the
226 static void print_current_name(void)
230 while (i < name_depth)
231 printf("/%.*s", namelen, name_list[i++]);
236 static int ask(const char *string, int def)
242 errors_uncorrected = 1;
248 errors_uncorrected = 1;
251 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
254 if ((c = getchar()) == EOF) {
256 errors_uncorrected = 1;
263 } else if (c == 'N') {
266 } else if (c == ' ' || c == '\n')
273 errors_uncorrected = 1;
279 * Make certain that we aren't checking a filesystem that is on a
280 * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
281 * 1994 Theodore Ts'o. Also licensed under GPL.
283 static void check_mount(void)
290 if ((f = setmntent(MOUNTED, "r")) == NULL)
292 while ((mnt = getmntent(f)) != NULL)
293 if (strcmp(device_name, mnt->mnt_fsname) == 0)
300 * If the root is mounted read-only, then /etc/mtab is
301 * probably not correct; so we won't issue a warning based on
304 fd = open(MOUNTED, O_RDWR);
305 if (fd < 0 && errno == EROFS)
310 printf("%s is mounted. ", device_name);
311 if (isatty(0) && isatty(1))
312 cont = ask("Do you really want to continue", 0);
316 printf("check aborted.\n");
323 * check_zone_nr checks to see that *nr is a valid zone nr. If it
324 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
325 * if an error was corrected, and returns the zone (0 for no zone
326 * or a bad zone-number).
328 static int check_zone_nr(unsigned short *nr, int *corrected)
333 printf("Zone nr < FIRSTZONE in file `");
334 else if (*nr >= ZONES)
335 printf("Zone nr >= ZONES in file `");
338 print_current_name();
340 if (ask("Remove block", 1)) {
348 static int check_zone_nr2(unsigned int *nr, int *corrected)
353 printf("Zone nr < FIRSTZONE in file `");
354 else if (*nr >= ZONES)
355 printf("Zone nr >= ZONES in file `");
358 print_current_name();
360 if (ask("Remove block", 1)) {
369 * read-block reads block nr into the buffer at addr.
371 static void read_block(unsigned int nr, char *addr)
374 memset(addr, 0, BLOCK_SIZE);
377 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) {
378 printf("Read error: unable to seek to block in file '");
379 print_current_name();
381 memset(addr, 0, BLOCK_SIZE);
382 errors_uncorrected = 1;
383 } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) {
384 printf("Read error: bad block in file '");
385 print_current_name();
387 memset(addr, 0, BLOCK_SIZE);
388 errors_uncorrected = 1;
393 * write_block writes block nr to disk.
395 static void write_block(unsigned int nr, char *addr)
399 if (nr < FIRSTZONE || nr >= ZONES) {
400 printf("Internal error: trying to write bad block\n"
401 "Write request ignored\n");
402 errors_uncorrected = 1;
405 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET))
406 die("seek failed in write_block");
407 if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) {
408 printf("Write error: bad block in file '");
409 print_current_name();
411 errors_uncorrected = 1;
416 * map-block calculates the absolute block nr of a block in a file.
417 * It sets 'changed' if the inode has needed changing, and re-writes
418 * any indirect blocks with errors.
420 static int map_block(struct minix_inode *inode, unsigned int blknr)
422 unsigned short ind[BLOCK_SIZE >> 1];
423 unsigned short dind[BLOCK_SIZE >> 1];
424 int blk_chg, block, result;
427 return check_zone_nr(inode->i_zone + blknr, &changed);
430 block = check_zone_nr(inode->i_zone + 7, &changed);
431 read_block(block, (char *) ind);
433 result = check_zone_nr(blknr + ind, &blk_chg);
435 write_block(block, (char *) ind);
439 block = check_zone_nr(inode->i_zone + 8, &changed);
440 read_block(block, (char *) dind);
442 result = check_zone_nr(dind + (blknr / 512), &blk_chg);
444 write_block(block, (char *) dind);
446 read_block(block, (char *) ind);
448 result = check_zone_nr(ind + (blknr % 512), &blk_chg);
450 write_block(block, (char *) ind);
455 static int map_block2(struct minix2_inode *inode, unsigned int blknr)
457 unsigned int ind[BLOCK_SIZE >> 2];
458 unsigned int dind[BLOCK_SIZE >> 2];
459 unsigned int tind[BLOCK_SIZE >> 2];
460 int blk_chg, block, result;
463 return check_zone_nr2(inode->i_zone + blknr, &changed);
466 block = check_zone_nr2(inode->i_zone + 7, &changed);
467 read_block(block, (char *) ind);
469 result = check_zone_nr2(blknr + ind, &blk_chg);
471 write_block(block, (char *) ind);
475 if (blknr >= 256 * 256) {
476 block = check_zone_nr2(inode->i_zone + 8, &changed);
477 read_block(block, (char *) dind);
479 result = check_zone_nr2(dind + blknr / 256, &blk_chg);
481 write_block(block, (char *) dind);
483 read_block(block, (char *) ind);
485 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
487 write_block(block, (char *) ind);
491 block = check_zone_nr2(inode->i_zone + 9, &changed);
492 read_block(block, (char *) tind);
494 result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
496 write_block(block, (char *) tind);
498 read_block(block, (char *) dind);
500 result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
502 write_block(block, (char *) dind);
504 read_block(block, (char *) ind);
506 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
508 write_block(block, (char *) ind);
513 static void write_super_block(void)
516 * Set the state of the filesystem based on whether or not there
517 * are uncorrected errors. The filesystem valid flag is
518 * unconditionally set if we get this far.
520 Super.s_state |= MINIX_VALID_FS;
521 if (errors_uncorrected)
522 Super.s_state |= MINIX_ERROR_FS;
524 Super.s_state &= ~MINIX_ERROR_FS;
526 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
527 die("seek failed in write_super_block");
528 if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE))
529 die("unable to write super-block");
534 static void write_tables(void)
538 if (IMAPS * BLOCK_SIZE != write(IN, inode_map, IMAPS * BLOCK_SIZE))
539 die("Unable to write inode map");
540 if (ZMAPS * BLOCK_SIZE != write(IN, zone_map, ZMAPS * BLOCK_SIZE))
541 die("Unable to write zone map");
542 if (INODE_BUFFER_SIZE != write(IN, inode_buffer, INODE_BUFFER_SIZE))
543 die("Unable to write inodes");
546 static void get_dirsize(void)
549 char blk[BLOCK_SIZE];
554 block = Inode2[ROOT_INO].i_zone[0];
557 block = Inode[ROOT_INO].i_zone[0];
558 read_block(block, blk);
559 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
560 if (strcmp(blk + size + 2, "..") == 0) {
569 static void read_superblock(void)
571 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
573 if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
574 die("unable to read super block");
575 if (MAGIC == MINIX_SUPER_MAGIC) {
579 } else if (MAGIC == MINIX_SUPER_MAGIC2) {
584 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
588 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
594 die("bad magic number in super-block");
595 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
596 die("Only 1k blocks/zones supported");
597 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
598 die("bad s_imap_blocks field in super-block");
599 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
600 die("bad s_zmap_blocks field in super-block");
603 static void read_tables(void)
605 inode_map = malloc(IMAPS * BLOCK_SIZE);
607 die("Unable to allocate buffer for inode map");
608 zone_map = malloc(ZMAPS * BLOCK_SIZE);
610 die("Unable to allocate buffer for zone map");
611 memset(inode_map, 0, sizeof(inode_map));
612 memset(zone_map, 0, sizeof(zone_map));
613 inode_buffer = malloc(INODE_BUFFER_SIZE);
615 die("Unable to allocate buffer for inodes");
616 inode_count = malloc(INODES + 1);
618 die("Unable to allocate buffer for inode count");
619 zone_count = malloc(ZONES);
621 die("Unable to allocate buffer for zone count");
622 if (IMAPS * BLOCK_SIZE != read(IN, inode_map, IMAPS * BLOCK_SIZE))
623 die("Unable to read inode map");
624 if (ZMAPS * BLOCK_SIZE != read(IN, zone_map, ZMAPS * BLOCK_SIZE))
625 die("Unable to read zone map");
626 if (INODE_BUFFER_SIZE != read(IN, inode_buffer, INODE_BUFFER_SIZE))
627 die("Unable to read inodes");
628 if (NORM_FIRSTZONE != FIRSTZONE) {
629 printf("Warning: Firstzone != Norm_firstzone\n");
630 errors_uncorrected = 1;
634 printf("%ld inodes\n", INODES);
635 printf("%ld blocks\n", ZONES);
636 printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
637 printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
638 printf("Maxsize=%ld\n", MAXSIZE);
639 printf("Filesystem state=%d\n", Super.s_state);
640 printf("namelen=%d\n\n", namelen);
644 struct minix_inode *get_inode(unsigned int nr)
646 struct minix_inode *inode;
648 if (!nr || nr > INODES)
652 if (!inode_count[nr]) {
653 if (!inode_in_use(nr)) {
654 printf("Inode %d marked not used, but used for file '", nr);
655 print_current_name();
658 if (ask("Mark in use", 1))
661 errors_uncorrected = 1;
664 if (S_ISDIR(inode->i_mode))
666 else if (S_ISREG(inode->i_mode))
668 else if (S_ISCHR(inode->i_mode))
670 else if (S_ISBLK(inode->i_mode))
672 else if (S_ISLNK(inode->i_mode))
674 else if (S_ISSOCK(inode->i_mode));
675 else if (S_ISFIFO(inode->i_mode));
677 print_current_name();
678 printf(" has mode %05o\n", inode->i_mode);
683 if (!++inode_count[nr]) {
684 printf("Warning: inode count too big.\n");
686 errors_uncorrected = 1;
692 struct minix2_inode *get_inode2(unsigned int nr)
694 struct minix2_inode *inode;
696 if (!nr || nr > INODES)
700 if (!inode_count[nr]) {
701 if (!inode_in_use(nr)) {
702 printf("Inode %d marked not used, but used for file '", nr);
703 print_current_name();
706 if (ask("Mark in use", 1))
709 errors_uncorrected = 1;
712 if (S_ISDIR(inode->i_mode))
714 else if (S_ISREG(inode->i_mode))
716 else if (S_ISCHR(inode->i_mode))
718 else if (S_ISBLK(inode->i_mode))
720 else if (S_ISLNK(inode->i_mode))
722 else if (S_ISSOCK(inode->i_mode));
723 else if (S_ISFIFO(inode->i_mode));
725 print_current_name();
726 printf(" has mode %05o\n", inode->i_mode);
730 if (!++inode_count[nr]) {
731 printf("Warning: inode count too big.\n");
733 errors_uncorrected = 1;
739 static void check_root(void)
741 struct minix_inode *inode = Inode + ROOT_INO;
743 if (!inode || !S_ISDIR(inode->i_mode))
744 die("root inode isn't a directory");
748 static void check_root2(void)
750 struct minix2_inode *inode = Inode2 + ROOT_INO;
752 if (!inode || !S_ISDIR(inode->i_mode))
753 die("root inode isn't a directory");
757 static int add_zone(unsigned short *znr, int *corrected)
763 block = check_zone_nr(znr, corrected);
766 if (zone_count[block]) {
767 printf("Block has been used before. Now in file `");
768 print_current_name();
770 if (ask("Clear", 1)) {
778 if (!zone_in_use(block)) {
779 printf("Block %d in file `", block);
780 print_current_name();
781 printf("' is marked not in use.");
782 if (ask("Correct", 1))
785 if (!++zone_count[block])
791 static int add_zone2(unsigned int *znr, int *corrected)
797 block = check_zone_nr2(znr, corrected);
800 if (zone_count[block]) {
801 printf("Block has been used before. Now in file `");
802 print_current_name();
804 if (ask("Clear", 1)) {
812 if (!zone_in_use(block)) {
813 printf("Block %d in file `", block);
814 print_current_name();
815 printf("' is marked not in use.");
816 if (ask("Correct", 1))
819 if (!++zone_count[block])
825 static void add_zone_ind(unsigned short *znr, int *corrected)
827 static char blk[BLOCK_SIZE];
831 block = add_zone(znr, corrected);
834 read_block(block, blk);
835 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
836 add_zone(i + (unsigned short *) blk, &chg_blk);
838 write_block(block, blk);
842 static void add_zone_ind2(unsigned int *znr, int *corrected)
844 static char blk[BLOCK_SIZE];
848 block = add_zone2(znr, corrected);
851 read_block(block, blk);
852 for (i = 0; i < BLOCK_SIZE >> 2; i++)
853 add_zone2(i + (unsigned int *) blk, &chg_blk);
855 write_block(block, blk);
859 static void add_zone_dind(unsigned short *znr, int *corrected)
861 static char blk[BLOCK_SIZE];
865 block = add_zone(znr, corrected);
868 read_block(block, blk);
869 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
870 add_zone_ind(i + (unsigned short *) blk, &blk_chg);
872 write_block(block, blk);
876 static void add_zone_dind2(unsigned int *znr, int *corrected)
878 static char blk[BLOCK_SIZE];
882 block = add_zone2(znr, corrected);
885 read_block(block, blk);
886 for (i = 0; i < BLOCK_SIZE >> 2; i++)
887 add_zone_ind2(i + (unsigned int *) blk, &blk_chg);
889 write_block(block, blk);
892 static void add_zone_tind2(unsigned int *znr, int *corrected)
894 static char blk[BLOCK_SIZE];
898 block = add_zone2(znr, corrected);
901 read_block(block, blk);
902 for (i = 0; i < BLOCK_SIZE >> 2; i++)
903 add_zone_dind2(i + (unsigned int *) blk, &blk_chg);
905 write_block(block, blk);
909 static void check_zones(unsigned int i)
911 struct minix_inode *inode;
913 if (!i || i > INODES)
915 if (inode_count[i] > 1) /* have we counted this file already? */
918 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
919 !S_ISLNK(inode->i_mode)) return;
920 for (i = 0; i < 7; i++)
921 add_zone(i + inode->i_zone, &changed);
922 add_zone_ind(7 + inode->i_zone, &changed);
923 add_zone_dind(8 + inode->i_zone, &changed);
927 static void check_zones2(unsigned int i)
929 struct minix2_inode *inode;
931 if (!i || i > INODES)
933 if (inode_count[i] > 1) /* have we counted this file already? */
936 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
937 && !S_ISLNK(inode->i_mode))
939 for (i = 0; i < 7; i++)
940 add_zone2(i + inode->i_zone, &changed);
941 add_zone_ind2(7 + inode->i_zone, &changed);
942 add_zone_dind2(8 + inode->i_zone, &changed);
943 add_zone_tind2(9 + inode->i_zone, &changed);
947 static void check_file(struct minix_inode *dir, unsigned int offset)
949 static char blk[BLOCK_SIZE];
950 struct minix_inode *inode;
955 block = map_block(dir, offset / BLOCK_SIZE);
956 read_block(block, blk);
957 name = blk + (offset % BLOCK_SIZE) + 2;
958 ino = *(unsigned short *) (name - 2);
960 print_current_name();
961 printf(" contains a bad inode number for file '");
962 printf("%.*s'.", namelen, name);
963 if (ask(" Remove", 1)) {
964 *(unsigned short *) (name - 2) = 0;
965 write_block(block, blk);
969 if (name_depth < MAX_DEPTH)
970 strncpy(name_list[name_depth], name, namelen);
972 inode = get_inode(ino);
975 if (!inode || strcmp(".", name)) {
976 print_current_name();
977 printf(": bad directory: '.' isn't first\n");
978 errors_uncorrected = 1;
982 if (offset == dirsize) {
983 if (!inode || strcmp("..", name)) {
984 print_current_name();
985 printf(": bad directory: '..' isn't second\n");
986 errors_uncorrected = 1;
992 if (name_depth < MAX_DEPTH)
993 strncpy(name_list[name_depth], name, namelen);
997 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
998 print_current_name();
999 if (S_ISDIR(inode->i_mode))
1005 if (inode && S_ISDIR(inode->i_mode))
1006 recursive_check(ino);
1012 static void check_file2(struct minix2_inode *dir, unsigned int offset)
1014 static char blk[BLOCK_SIZE];
1015 struct minix2_inode *inode;
1020 block = map_block2(dir, offset / BLOCK_SIZE);
1021 read_block(block, blk);
1022 name = blk + (offset % BLOCK_SIZE) + 2;
1023 ino = *(unsigned short *) (name - 2);
1025 print_current_name();
1026 printf(" contains a bad inode number for file '");
1027 printf("%.*s'.", namelen, name);
1028 if (ask(" Remove", 1)) {
1029 *(unsigned short *) (name - 2) = 0;
1030 write_block(block, blk);
1034 if (name_depth < MAX_DEPTH)
1035 strncpy(name_list[name_depth], name, namelen);
1037 inode = get_inode2(ino);
1040 if (!inode || strcmp(".", name)) {
1041 print_current_name();
1042 printf(": bad directory: '.' isn't first\n");
1043 errors_uncorrected = 1;
1047 if (offset == dirsize) {
1048 if (!inode || strcmp("..", name)) {
1049 print_current_name();
1050 printf(": bad directory: '..' isn't second\n");
1051 errors_uncorrected = 1;
1060 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1061 print_current_name();
1062 if (S_ISDIR(inode->i_mode))
1068 if (inode && S_ISDIR(inode->i_mode))
1069 recursive_check2(ino);
1075 static void recursive_check(unsigned int ino)
1077 struct minix_inode *dir;
1078 unsigned int offset;
1081 if (!S_ISDIR(dir->i_mode))
1082 die("internal error");
1083 if (dir->i_size < 2 * dirsize) {
1084 print_current_name();
1085 printf(": bad directory: size<32");
1086 errors_uncorrected = 1;
1088 for (offset = 0; offset < dir->i_size; offset += dirsize)
1089 check_file(dir, offset);
1093 static void recursive_check2(unsigned int ino)
1095 struct minix2_inode *dir;
1096 unsigned int offset;
1099 if (!S_ISDIR(dir->i_mode))
1100 die("internal error");
1101 if (dir->i_size < 2 * dirsize) {
1102 print_current_name();
1103 printf(": bad directory: size < 32");
1104 errors_uncorrected = 1;
1106 for (offset = 0; offset < dir->i_size; offset += dirsize)
1107 check_file2(dir, offset);
1111 static int bad_zone(int i)
1115 if (BLOCK_SIZE * i != lseek(IN, BLOCK_SIZE * i, SEEK_SET))
1116 die("seek failed in bad_zone");
1117 return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE));
1120 static void check_counts(void)
1124 for (i = 1; i <= INODES; i++) {
1125 if (!inode_in_use(i) && Inode[i].i_mode && warn_mode) {
1126 printf("Inode %d mode not cleared.", i);
1127 if (ask("Clear", 1)) {
1128 Inode[i].i_mode = 0;
1132 if (!inode_count[i]) {
1133 if (!inode_in_use(i))
1135 printf("Inode %d not used, marked used in the bitmap.", i);
1136 if (ask("Clear", 1))
1140 if (!inode_in_use(i)) {
1141 printf("Inode %d used, marked unused in the bitmap.", i);
1145 if (Inode[i].i_nlinks != inode_count[i]) {
1146 printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
1147 i, Inode[i].i_mode, Inode[i].i_nlinks, inode_count[i]);
1148 if (ask("Set i_nlinks to count", 1)) {
1149 Inode[i].i_nlinks = inode_count[i];
1154 for (i = FIRSTZONE; i < ZONES; i++) {
1155 if (zone_in_use(i) == zone_count[i])
1157 if (!zone_count[i]) {
1160 printf("Zone %d: marked in use, no file uses it.", i);
1161 if (ask("Unmark", 1))
1165 printf("Zone %d: %sin use, counted=%d\n",
1166 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1171 static void check_counts2(void)
1175 for (i = 1; i <= INODES; i++) {
1176 if (!inode_in_use(i) && Inode2[i].i_mode && warn_mode) {
1177 printf("Inode %d mode not cleared.", i);
1178 if (ask("Clear", 1)) {
1179 Inode2[i].i_mode = 0;
1183 if (!inode_count[i]) {
1184 if (!inode_in_use(i))
1186 printf("Inode %d not used, marked used in the bitmap.", i);
1187 if (ask("Clear", 1))
1191 if (!inode_in_use(i)) {
1192 printf("Inode %d used, marked unused in the bitmap.", i);
1196 if (Inode2[i].i_nlinks != inode_count[i]) {
1197 printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
1198 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1200 if (ask("Set i_nlinks to count", 1)) {
1201 Inode2[i].i_nlinks = inode_count[i];
1206 for (i = FIRSTZONE; i < ZONES; i++) {
1207 if (zone_in_use(i) == zone_count[i])
1209 if (!zone_count[i]) {
1212 printf("Zone %d: marked in use, no file uses it.", i);
1213 if (ask("Unmark", 1))
1217 printf("Zone %d: %sin use, counted=%d\n",
1218 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1223 static void check(void)
1225 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1226 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1227 check_zones(ROOT_INO);
1228 recursive_check(ROOT_INO);
1233 static void check2(void)
1235 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1236 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1237 check_zones2(ROOT_INO);
1238 recursive_check2(ROOT_INO);
1243 extern int fsck_minix_main(int argc, char **argv)
1250 program_name = *argv;
1251 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
1252 die("bad inode size");
1254 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
1255 die("bad v2 inode size");
1257 while (argc-- > 1) {
1259 if (argv[0][0] != '-') {
1263 device_name = argv[0];
1266 switch (argv[0][0]) {
1296 check_mount(); /* trying to check a mounted filesystem? */
1297 if (repair && !automatic) {
1298 if (!isatty(0) || !isatty(1))
1299 die("need terminal for interactive repairs");
1301 IN = open(device_name, repair ? O_RDWR : O_RDONLY);
1303 die("unable to open '%s'");
1304 for (count = 0; count < 3; count++)
1309 * Determine whether or not we should continue with the checking.
1310 * This is based on the status of the filesystem valid and error
1311 * flags and whether or not the -f switch was specified on the
1314 printf("%s, %s\n", program_name, program_version);
1315 if (!(Super.s_state & MINIX_ERROR_FS) &&
1316 (Super.s_state & MINIX_VALID_FS) && !force) {
1318 printf("%s is clean, no check.\n", device_name);
1321 printf("Forcing filesystem check on %s.\n", device_name);
1323 printf("Filesystem on %s is dirty, needs checking.\n",
1328 if (repair && !automatic) {
1329 tcgetattr(0, &termios);
1331 tmp.c_lflag &= ~(ICANON | ECHO);
1332 tcsetattr(0, TCSANOW, &tmp);
1348 for (i = 1, free = 0; i <= INODES; i++)
1349 if (!inode_in_use(i))
1351 printf("\n%6ld inodes used (%ld%%)\n", (INODES - free),
1352 100 * (INODES - free) / INODES);
1353 for (i = FIRSTZONE, free = 0; i < ZONES; i++)
1354 if (!zone_in_use(i))
1356 printf("%6ld zones used (%ld%%)\n", (ZONES - free),
1357 100 * (ZONES - free) / ZONES);
1358 printf("\n%6d regular files\n"
1360 "%6d character device files\n"
1361 "%6d block device files\n"
1363 "%6d symbolic links\n"
1366 regular, directory, chardev, blockdev,
1367 links - 2 * directory + 1, symlinks,
1368 total - 2 * directory + 1);
1372 printf("----------------------------\n"
1373 "FILE SYSTEM HAS BEEN CHANGED\n"
1374 "----------------------------\n");
1375 for (count = 0; count < 3; count++)
1378 write_super_block();
1380 if (repair && !automatic)
1381 tcsetattr(0, TCSANOW, &termios);
1385 if (errors_uncorrected)