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 rudimentary 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 system-independent.
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 * 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
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 :-).
98 #include <sys/param.h>
101 #define BLOCK_SIZE_BITS 10
102 #define BLOCK_SIZE (1<<BLOCK_SIZE_BITS)
105 * This is the original minix inode layout on disk.
106 * Note the 8-bit gid and atime and ctime.
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.
124 struct minix2_inode {
138 MINIX_LINK_MAX = 250,
139 MINIX2_LINK_MAX = 65530,
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. */
150 MINIX_INODES_PER_BLOCK = ((BLOCK_SIZE)/(sizeof (struct minix_inode))),
151 MINIX2_INODES_PER_BLOCK = ((BLOCK_SIZE)/(sizeof (struct minix2_inode))),
153 MINIX_V1 = 0x0001, /* original minix fs */
154 MINIX_V2 = 0x0002 /* minix V2 fs */
157 #define INODE_VERSION(inode) inode->i_sb->u.minix_sb.s_version
160 * minix super-block data on disk
162 struct minix_super_block {
165 uint16_t s_imap_blocks;
166 uint16_t s_zmap_blocks;
167 uint16_t s_firstdatazone;
168 uint16_t s_log_zone_size;
175 struct minix_dir_entry {
181 #define NAME_MAX 255 /* # chars in a file name */
183 #define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
186 #define BLKGETSIZE _IO(0x12,96) /* return device size */
193 enum { ROOT_INO = 1 };
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))
202 #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
204 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
206 #define BITS_PER_BLOCK (BLOCK_SIZE<<3)
208 static char *program_version = "1.2 - 11/11/96";
209 static char *device_name = NULL;
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;
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;
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;
230 static char *inode_buffer = NULL;
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];
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))
241 #define ZONES ((unsigned long)(Super.s_nzones))
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)
251 static char *inode_map;
252 static char *zone_map;
254 static unsigned char *inode_count = NULL;
255 static unsigned char *zone_count = NULL;
257 static void recursive_check(unsigned int ino);
258 #ifdef CONFIG_FEATURE_MINIX2
259 static void recursive_check2(unsigned int ino);
262 static inline int bit(char * a,unsigned int i)
264 return (a[i >> 3] & (1<<(i & 7))) != 0;
266 #define inode_in_use(x) (bit(inode_map,(x)))
267 #define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
269 #define mark_inode(x) (setbit(inode_map,(x)),changed=1)
270 #define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
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)
275 static void leave(int) ATTRIBUTE_NORETURN;
276 static void leave(int status)
279 tcsetattr(0, TCSANOW, &termios);
283 static void die(const char *str)
285 bb_error_msg("%s", str);
290 * This simply goes through the file-name data and prints out the
293 static void print_current_name(void)
297 while (i < name_depth)
298 printf("/%.*s", namelen, name_list[i++]);
303 static int ask(const char *string, int def)
309 errors_uncorrected = 1;
315 errors_uncorrected = 1;
318 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
321 if ((c = getchar()) == EOF) {
323 errors_uncorrected = 1;
330 } else if (c == 'N') {
333 } else if (c == ' ' || c == '\n')
340 errors_uncorrected = 1;
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.
350 static void check_mount(void)
357 if ((f = setmntent(MOUNTED, "r")) == NULL)
359 while ((mnt = getmntent(f)) != NULL)
360 if (strcmp(device_name, mnt->mnt_fsname) == 0)
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
371 fd = open(MOUNTED, O_RDWR);
372 if (fd < 0 && errno == EROFS)
377 printf("%s is mounted. ", device_name);
378 if (isatty(0) && isatty(1))
379 cont = ask("Do you really want to continue", 0);
383 printf("check aborted.\n");
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).
395 static int check_zone_nr(unsigned short *nr, int *corrected)
400 printf("Zone nr < FIRSTZONE in file `");
401 else if (*nr >= ZONES)
402 printf("Zone nr >= ZONES in file `");
405 print_current_name();
407 if (ask("Remove block", 1)) {
414 #ifdef CONFIG_FEATURE_MINIX2
415 static int check_zone_nr2(unsigned int *nr, int *corrected)
420 printf("Zone nr < FIRSTZONE in file `");
421 else if (*nr >= ZONES)
422 printf("Zone nr >= ZONES in file `");
425 print_current_name();
427 if (ask("Remove block", 1)) {
436 * read-block reads block nr into the buffer at addr.
438 static void read_block(unsigned int nr, char *addr)
441 memset(addr, 0, BLOCK_SIZE);
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();
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();
454 memset(addr, 0, BLOCK_SIZE);
455 errors_uncorrected = 1;
460 * write_block writes block nr to disk.
462 static void write_block(unsigned int nr, char *addr)
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;
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();
478 errors_uncorrected = 1;
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.
487 static int map_block(struct minix_inode *inode, unsigned int blknr)
489 unsigned short ind[BLOCK_SIZE >> 1];
490 unsigned short dind[BLOCK_SIZE >> 1];
491 int blk_chg, block, result;
494 return check_zone_nr(inode->i_zone + blknr, &changed);
497 block = check_zone_nr(inode->i_zone + 7, &changed);
498 read_block(block, (char *) ind);
500 result = check_zone_nr(blknr + ind, &blk_chg);
502 write_block(block, (char *) ind);
506 block = check_zone_nr(inode->i_zone + 8, &changed);
507 read_block(block, (char *) dind);
509 result = check_zone_nr(dind + (blknr / 512), &blk_chg);
511 write_block(block, (char *) dind);
513 read_block(block, (char *) ind);
515 result = check_zone_nr(ind + (blknr % 512), &blk_chg);
517 write_block(block, (char *) ind);
521 #ifdef CONFIG_FEATURE_MINIX2
522 static int map_block2(struct minix2_inode *inode, unsigned int blknr)
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;
530 return check_zone_nr2(inode->i_zone + blknr, &changed);
533 block = check_zone_nr2(inode->i_zone + 7, &changed);
534 read_block(block, (char *) ind);
536 result = check_zone_nr2(blknr + ind, &blk_chg);
538 write_block(block, (char *) ind);
542 if (blknr >= 256 * 256) {
543 block = check_zone_nr2(inode->i_zone + 8, &changed);
544 read_block(block, (char *) dind);
546 result = check_zone_nr2(dind + blknr / 256, &blk_chg);
548 write_block(block, (char *) dind);
550 read_block(block, (char *) ind);
552 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
554 write_block(block, (char *) ind);
558 block = check_zone_nr2(inode->i_zone + 9, &changed);
559 read_block(block, (char *) tind);
561 result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
563 write_block(block, (char *) tind);
565 read_block(block, (char *) dind);
567 result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
569 write_block(block, (char *) dind);
571 read_block(block, (char *) ind);
573 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
575 write_block(block, (char *) ind);
580 static void write_super_block(void)
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.
587 Super.s_state |= MINIX_VALID_FS;
588 if (errors_uncorrected)
589 Super.s_state |= MINIX_ERROR_FS;
591 Super.s_state &= ~MINIX_ERROR_FS;
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");
601 static void write_tables(void)
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");
613 static void get_dirsize(void)
616 char blk[BLOCK_SIZE];
619 #ifdef CONFIG_FEATURE_MINIX2
621 block = Inode2[ROOT_INO].i_zone[0];
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) {
636 static void read_superblock(void)
638 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
640 if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
641 die("unable to read super block");
642 if (MAGIC == MINIX_SUPER_MAGIC) {
646 } else if (MAGIC == MINIX_SUPER_MAGIC2) {
650 #ifdef CONFIG_FEATURE_MINIX2
651 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
655 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
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");
670 static void read_tables(void)
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;
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);
701 static struct minix_inode *get_inode(unsigned int nr)
703 struct minix_inode *inode;
705 if (!nr || nr > INODES)
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();
715 if (ask("Mark in use", 1))
718 errors_uncorrected = 1;
721 if (S_ISDIR(inode->i_mode))
723 else if (S_ISREG(inode->i_mode))
725 else if (S_ISCHR(inode->i_mode))
727 else if (S_ISBLK(inode->i_mode))
729 else if (S_ISLNK(inode->i_mode))
731 else if (S_ISSOCK(inode->i_mode));
732 else if (S_ISFIFO(inode->i_mode));
734 print_current_name();
735 printf(" has mode %05o\n", inode->i_mode);
740 if (!++inode_count[nr]) {
741 printf("Warning: inode count too big.\n");
743 errors_uncorrected = 1;
748 #ifdef CONFIG_FEATURE_MINIX2
749 static struct minix2_inode *get_inode2(unsigned int nr)
751 struct minix2_inode *inode;
753 if (!nr || nr > INODES)
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();
763 if (ask("Mark in use", 1))
766 errors_uncorrected = 1;
769 if (S_ISDIR(inode->i_mode))
771 else if (S_ISREG(inode->i_mode))
773 else if (S_ISCHR(inode->i_mode))
775 else if (S_ISBLK(inode->i_mode))
777 else if (S_ISLNK(inode->i_mode))
779 else if (S_ISSOCK(inode->i_mode));
780 else if (S_ISFIFO(inode->i_mode));
782 print_current_name();
783 printf(" has mode %05o\n", inode->i_mode);
787 if (!++inode_count[nr]) {
788 printf("Warning: inode count too big.\n");
790 errors_uncorrected = 1;
796 static void check_root(void)
798 struct minix_inode *inode = Inode + ROOT_INO;
800 if (!inode || !S_ISDIR(inode->i_mode))
801 die("root inode isn't a directory");
804 #ifdef CONFIG_FEATURE_MINIX2
805 static void check_root2(void)
807 struct minix2_inode *inode = Inode2 + ROOT_INO;
809 if (!inode || !S_ISDIR(inode->i_mode))
810 die("root inode isn't a directory");
814 static int add_zone(unsigned short *znr, int *corrected)
820 block = check_zone_nr(znr, corrected);
823 if (zone_count[block]) {
824 printf("Block has been used before. Now in file `");
825 print_current_name();
827 if (ask("Clear", 1)) {
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))
842 if (!++zone_count[block])
847 #ifdef CONFIG_FEATURE_MINIX2
848 static int add_zone2(unsigned int *znr, int *corrected)
854 block = check_zone_nr2(znr, corrected);
857 if (zone_count[block]) {
858 printf("Block has been used before. Now in file `");
859 print_current_name();
861 if (ask("Clear", 1)) {
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))
876 if (!++zone_count[block])
882 static void add_zone_ind(unsigned short *znr, int *corrected)
884 static char blk[BLOCK_SIZE];
888 block = add_zone(znr, corrected);
891 read_block(block, blk);
892 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
893 add_zone(i + (unsigned short *) blk, &chg_blk);
895 write_block(block, blk);
898 #ifdef CONFIG_FEATURE_MINIX2
899 static void add_zone_ind2(unsigned int *znr, int *corrected)
901 static char blk[BLOCK_SIZE];
905 block = add_zone2(znr, corrected);
908 read_block(block, blk);
909 for (i = 0; i < BLOCK_SIZE >> 2; i++)
910 add_zone2(i + (unsigned int *) blk, &chg_blk);
912 write_block(block, blk);
916 static void add_zone_dind(unsigned short *znr, int *corrected)
918 static char blk[BLOCK_SIZE];
922 block = add_zone(znr, corrected);
925 read_block(block, blk);
926 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
927 add_zone_ind(i + (unsigned short *) blk, &blk_chg);
929 write_block(block, blk);
932 #ifdef CONFIG_FEATURE_MINIX2
933 static void add_zone_dind2(unsigned int *znr, int *corrected)
935 static char blk[BLOCK_SIZE];
939 block = add_zone2(znr, corrected);
942 read_block(block, blk);
943 for (i = 0; i < BLOCK_SIZE >> 2; i++)
944 add_zone_ind2(i + (unsigned int *) blk, &blk_chg);
946 write_block(block, blk);
949 static void add_zone_tind2(unsigned int *znr, int *corrected)
951 static char blk[BLOCK_SIZE];
955 block = add_zone2(znr, corrected);
958 read_block(block, blk);
959 for (i = 0; i < BLOCK_SIZE >> 2; i++)
960 add_zone_dind2(i + (unsigned int *) blk, &blk_chg);
962 write_block(block, blk);
966 static void check_zones(unsigned int i)
968 struct minix_inode *inode;
970 if (!i || i > INODES)
972 if (inode_count[i] > 1) /* have we counted this file already? */
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);
983 #ifdef CONFIG_FEATURE_MINIX2
984 static void check_zones2(unsigned int i)
986 struct minix2_inode *inode;
988 if (!i || i > INODES)
990 if (inode_count[i] > 1) /* have we counted this file already? */
993 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
994 && !S_ISLNK(inode->i_mode))
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);
1004 static void check_file(struct minix_inode *dir, unsigned int offset)
1006 static char blk[BLOCK_SIZE];
1007 struct minix_inode *inode;
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);
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);
1026 if (name_depth < MAX_DEPTH)
1027 strncpy(name_list[name_depth], name, namelen);
1029 inode = get_inode(ino);
1032 if (!inode || strcmp(".", name)) {
1033 print_current_name();
1034 printf(": bad directory: '.' isn't first\n");
1035 errors_uncorrected = 1;
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;
1049 if (name_depth < MAX_DEPTH)
1050 strncpy(name_list[name_depth], name, namelen);
1054 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1055 print_current_name();
1056 if (S_ISDIR(inode->i_mode))
1062 if (inode && S_ISDIR(inode->i_mode))
1063 recursive_check(ino);
1068 #ifdef CONFIG_FEATURE_MINIX2
1069 static void check_file2(struct minix2_inode *dir, unsigned int offset)
1071 static char blk[BLOCK_SIZE];
1072 struct minix2_inode *inode;
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);
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);
1091 if (name_depth < MAX_DEPTH)
1092 strncpy(name_list[name_depth], name, namelen);
1094 inode = get_inode2(ino);
1097 if (!inode || strcmp(".", name)) {
1098 print_current_name();
1099 printf(": bad directory: '.' isn't first\n");
1100 errors_uncorrected = 1;
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;
1117 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1118 print_current_name();
1119 if (S_ISDIR(inode->i_mode))
1125 if (inode && S_ISDIR(inode->i_mode))
1126 recursive_check2(ino);
1132 static void recursive_check(unsigned int ino)
1134 struct minix_inode *dir;
1135 unsigned int offset;
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;
1145 for (offset = 0; offset < dir->i_size; offset += dirsize)
1146 check_file(dir, offset);
1149 #ifdef CONFIG_FEATURE_MINIX2
1150 static void recursive_check2(unsigned int ino)
1152 struct minix2_inode *dir;
1153 unsigned int offset;
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;
1163 for (offset = 0; offset < dir->i_size; offset += dirsize)
1164 check_file2(dir, offset);
1168 static int bad_zone(int i)
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));
1177 static void check_counts(void)
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;
1189 if (!inode_count[i]) {
1190 if (!inode_in_use(i))
1192 printf("Inode %d not used, marked used in the bitmap.", i);
1193 if (ask("Clear", 1))
1197 if (!inode_in_use(i)) {
1198 printf("Inode %d used, marked unused in the bitmap.", i);
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];
1211 for (i = FIRSTZONE; i < ZONES; i++) {
1212 if (zone_in_use(i) == zone_count[i])
1214 if (!zone_count[i]) {
1217 printf("Zone %d: marked in use, no file uses it.", i);
1218 if (ask("Unmark", 1))
1222 printf("Zone %d: %sin use, counted=%d\n",
1223 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1227 #ifdef CONFIG_FEATURE_MINIX2
1228 static void check_counts2(void)
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;
1240 if (!inode_count[i]) {
1241 if (!inode_in_use(i))
1243 printf("Inode %d not used, marked used in the bitmap.", i);
1244 if (ask("Clear", 1))
1248 if (!inode_in_use(i)) {
1249 printf("Inode %d used, marked unused in the bitmap.", i);
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,
1257 if (ask("Set i_nlinks to count", 1)) {
1258 Inode2[i].i_nlinks = inode_count[i];
1263 for (i = FIRSTZONE; i < ZONES; i++) {
1264 if (zone_in_use(i) == zone_count[i])
1266 if (!zone_count[i]) {
1269 printf("Zone %d: marked in use, no file uses it.", i);
1270 if (ask("Unmark", 1))
1274 printf("Zone %d: %sin use, counted=%d\n",
1275 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1280 static void check(void)
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);
1289 #ifdef CONFIG_FEATURE_MINIX2
1290 static void check2(void)
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);
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)
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);
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)
1319 for (i = 0; i < MAX_DEPTH; i++) {
1327 int fsck_minix_main(int argc, char **argv)
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);
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");
1346 while (argc-- > 1) {
1348 if (argv[0][0] != '-') {
1352 device_name = argv[0];
1355 switch (argv[0][0]) {
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");
1390 IN = open(device_name, repair ? O_RDWR : O_RDONLY);
1392 fprintf(stderr,"unable to open device '%s'.\n",device_name);
1395 for (count = 0; count < 3; count++)
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
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) {
1409 printf("%s is clean, no check.\n", device_name);
1412 printf("Forcing filesystem check on %s.\n", device_name);
1414 printf("Filesystem on %s is dirty, needs checking.\n",
1419 if (repair && !automatic) {
1420 tcgetattr(0, &termios);
1422 tmp.c_lflag &= ~(ICANON | ECHO);
1423 tcsetattr(0, TCSANOW, &tmp);
1426 #ifdef CONFIG_FEATURE_MINIX2
1439 for (i = 1, free_cnt = 0; i <= INODES; i++)
1440 if (!inode_in_use(i))
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))
1447 printf("%6ld zones used (%ld%%)\n", (ZONES - free_cnt),
1448 100 * (ZONES - free_cnt) / ZONES);
1449 printf("\n%6d regular files\n"
1451 "%6d character device files\n"
1452 "%6d block device files\n"
1454 "%6d symbolic links\n"
1457 regular, directory, chardev, blockdev,
1458 links - 2 * directory + 1, symlinks,
1459 total - 2 * directory + 1);
1463 printf("----------------------------\n"
1464 "FILE SYSTEM HAS BEEN CHANGED\n"
1465 "----------------------------\n");
1466 for (count = 0; count < 3; count++)
1469 write_super_block();
1471 if (repair && !automatic)
1472 tcsetattr(0, TCSANOW, &termios);
1476 if (errors_uncorrected)