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;
211 static int repair, automatic, verbose, list, show, warn_mode, force;
212 static int directory, regular, blockdev, chardev, links, symlinks, total;
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;
219 static struct termios termios;
220 static int termios_set;
223 enum { MAX_DEPTH = 32 };
224 static int name_depth;
225 // static char name_list[MAX_DEPTH][BUFSIZ + 1];
226 static char **name_list;
228 static char *inode_buffer;
230 #define Inode (((struct minix_inode *) inode_buffer)-1)
231 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
232 static char super_block_buffer[BLOCK_SIZE];
234 #define Super (*(struct minix_super_block *)super_block_buffer)
235 #define INODES ((unsigned long)Super.s_ninodes)
236 #ifdef CONFIG_FEATURE_MINIX2
237 #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
239 #define ZONES ((unsigned long)(Super.s_nzones))
241 #define IMAPS ((unsigned long)Super.s_imap_blocks)
242 #define ZMAPS ((unsigned long)Super.s_zmap_blocks)
243 #define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
244 #define ZONESIZE ((unsigned long)Super.s_log_zone_size)
245 #define MAXSIZE ((unsigned long)Super.s_max_size)
246 #define MAGIC (Super.s_magic)
247 #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
249 static char *inode_map;
250 static char *zone_map;
252 static unsigned char *inode_count;
253 static unsigned char *zone_count;
255 static void recursive_check(unsigned int ino);
256 #ifdef CONFIG_FEATURE_MINIX2
257 static void recursive_check2(unsigned int ino);
260 static inline int bit(char * a,unsigned int i)
262 return (a[i >> 3] & (1<<(i & 7))) != 0;
264 #define inode_in_use(x) (bit(inode_map,(x)))
265 #define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
267 #define mark_inode(x) (setbit(inode_map,(x)),changed=1)
268 #define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
270 #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1)
271 #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1)
273 static void leave(int) ATTRIBUTE_NORETURN;
274 static void leave(int status)
277 tcsetattr(0, TCSANOW, &termios);
281 static void die(const char *str)
283 bb_error_msg("%s", str);
288 * This simply goes through the file-name data and prints out the
291 static void print_current_name(void)
295 while (i < name_depth)
296 printf("/%.*s", namelen, name_list[i++]);
301 static int ask(const char *string, int def)
307 errors_uncorrected = 1;
313 errors_uncorrected = 1;
316 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
319 if ((c = getchar()) == EOF) {
321 errors_uncorrected = 1;
328 } else if (c == 'N') {
331 } else if (c == ' ' || c == '\n')
338 errors_uncorrected = 1;
344 * Make certain that we aren't checking a filesystem that is on a
345 * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
346 * 1994 Theodore Ts'o. Also licensed under GPL.
348 static void check_mount(void)
355 if ((f = setmntent(MOUNTED, "r")) == NULL)
357 while ((mnt = getmntent(f)) != NULL)
358 if (strcmp(device_name, mnt->mnt_fsname) == 0)
365 * If the root is mounted read-only, then /etc/mtab is
366 * probably not correct; so we won't issue a warning based on
369 fd = open(MOUNTED, O_RDWR);
370 if (fd < 0 && errno == EROFS)
375 printf("%s is mounted. ", device_name);
377 if (isatty(0) && isatty(1))
378 cont = ask("Do you really want to continue", 0);
380 printf("Check aborted\n");
387 * check_zone_nr checks to see that *nr is a valid zone nr. If it
388 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
389 * if an error was corrected, and returns the zone (0 for no zone
390 * or a bad zone-number).
392 static int check_zone_nr2(uint32_t *nr, int *corrected)
397 printf("Zone nr < FIRSTZONE in file `");
398 else if (*nr >= ZONES)
399 printf("Zone nr >= ZONES in file `");
402 print_current_name();
404 if (ask("Remove block", 1)) {
411 static int check_zone_nr(uint16_t *nr, int *corrected)
414 int r = check_zone_nr2(&nr32, corrected);
415 *nr = (uint16_t)nr32;
420 * read-block reads block nr into the buffer at addr.
422 static void read_block(unsigned int nr, char *addr)
425 memset(addr, 0, BLOCK_SIZE);
428 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) {
429 printf("Read error: unable to seek to block in file '");
430 print_current_name();
432 errors_uncorrected = 1;
433 memset(addr, 0, BLOCK_SIZE);
434 } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) {
435 printf("Read error: bad block in file '");
436 print_current_name();
438 errors_uncorrected = 1;
439 memset(addr, 0, BLOCK_SIZE);
444 * write_block writes block nr to disk.
446 static void write_block(unsigned int nr, char *addr)
450 if (nr < FIRSTZONE || nr >= ZONES) {
451 printf("Internal error: trying to write bad block\n"
452 "Write request ignored\n");
453 errors_uncorrected = 1;
456 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET))
457 die("seek failed in write_block");
458 if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) {
459 printf("Write error: bad block in file '");
460 print_current_name();
462 errors_uncorrected = 1;
467 * map_block calculates the absolute block nr of a block in a file.
468 * It sets 'changed' if the inode has needed changing, and re-writes
469 * any indirect blocks with errors.
471 static int map_block(struct minix_inode *inode, unsigned int blknr)
473 uint16_t ind[BLOCK_SIZE >> 1];
474 uint16_t dind[BLOCK_SIZE >> 1];
475 int blk_chg, block, result;
478 return check_zone_nr(inode->i_zone + blknr, &changed);
481 block = check_zone_nr(inode->i_zone + 7, &changed);
482 read_block(block, (char *) ind);
484 result = check_zone_nr(blknr + ind, &blk_chg);
486 write_block(block, (char *) ind);
490 block = check_zone_nr(inode->i_zone + 8, &changed);
491 read_block(block, (char *) dind);
493 result = check_zone_nr(dind + (blknr / 512), &blk_chg);
495 write_block(block, (char *) dind);
497 read_block(block, (char *) ind);
499 result = check_zone_nr(ind + (blknr % 512), &blk_chg);
501 write_block(block, (char *) ind);
505 #ifdef CONFIG_FEATURE_MINIX2
506 static int map_block2(struct minix2_inode *inode, unsigned int blknr)
508 uint32_t ind[BLOCK_SIZE >> 2];
509 uint32_t dind[BLOCK_SIZE >> 2];
510 uint32_t tind[BLOCK_SIZE >> 2];
511 int blk_chg, block, result;
514 return check_zone_nr2(inode->i_zone + blknr, &changed);
517 block = check_zone_nr2(inode->i_zone + 7, &changed);
518 read_block(block, (char *) ind);
520 result = check_zone_nr2(blknr + ind, &blk_chg);
522 write_block(block, (char *) ind);
526 if (blknr >= 256 * 256) {
527 block = check_zone_nr2(inode->i_zone + 8, &changed);
528 read_block(block, (char *) dind);
530 result = check_zone_nr2(dind + blknr / 256, &blk_chg);
532 write_block(block, (char *) dind);
534 read_block(block, (char *) ind);
536 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
538 write_block(block, (char *) ind);
542 block = check_zone_nr2(inode->i_zone + 9, &changed);
543 read_block(block, (char *) tind);
545 result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
547 write_block(block, (char *) tind);
549 read_block(block, (char *) dind);
551 result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
553 write_block(block, (char *) dind);
555 read_block(block, (char *) ind);
557 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
559 write_block(block, (char *) ind);
564 static void write_super_block(void)
567 * Set the state of the filesystem based on whether or not there
568 * are uncorrected errors. The filesystem valid flag is
569 * unconditionally set if we get this far.
571 Super.s_state |= MINIX_VALID_FS;
572 if (errors_uncorrected)
573 Super.s_state |= MINIX_ERROR_FS;
575 Super.s_state &= ~MINIX_ERROR_FS;
577 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
578 die("seek failed in write_super_block");
579 if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE))
580 die("unable to write super-block");
583 static void write_tables(void)
587 if (IMAPS * BLOCK_SIZE != write(IN, inode_map, IMAPS * BLOCK_SIZE))
588 die("Unable to write inode map");
589 if (ZMAPS * BLOCK_SIZE != write(IN, zone_map, ZMAPS * BLOCK_SIZE))
590 die("Unable to write zone map");
591 if (INODE_BUFFER_SIZE != write(IN, inode_buffer, INODE_BUFFER_SIZE))
592 die("Unable to write inodes");
595 static void get_dirsize(void)
598 char blk[BLOCK_SIZE];
601 #ifdef CONFIG_FEATURE_MINIX2
603 block = Inode2[ROOT_INO].i_zone[0];
606 block = Inode[ROOT_INO].i_zone[0];
607 read_block(block, blk);
608 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
609 if (strcmp(blk + size + 2, "..") == 0) {
618 static void read_superblock(void)
620 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
622 if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
623 die("unable to read super block");
627 if (MAGIC == MINIX_SUPER_MAGIC) {
628 } else if (MAGIC == MINIX_SUPER_MAGIC2) {
631 #ifdef CONFIG_FEATURE_MINIX2
632 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
634 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
640 die("bad magic number in super-block");
641 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
642 die("Only 1k blocks/zones supported");
643 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
644 die("bad s_imap_blocks field in super-block");
645 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
646 die("bad s_zmap_blocks field in super-block");
649 static void read_tables(void)
651 inode_map = xmalloc(IMAPS * BLOCK_SIZE);
652 zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
653 memset(inode_map, 0, IMAPS * BLOCK_SIZE);
654 memset(zone_map, 0, ZMAPS * BLOCK_SIZE);
655 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
656 inode_count = xmalloc(INODES + 1);
657 zone_count = xmalloc(ZONES);
658 if (IMAPS * BLOCK_SIZE != read(IN, inode_map, IMAPS * BLOCK_SIZE))
659 die("Unable to read inode map");
660 if (ZMAPS * BLOCK_SIZE != read(IN, zone_map, ZMAPS * BLOCK_SIZE))
661 die("Unable to read zone map");
662 if (INODE_BUFFER_SIZE != read(IN, inode_buffer, INODE_BUFFER_SIZE))
663 die("Unable to read inodes");
664 if (NORM_FIRSTZONE != FIRSTZONE) {
665 printf("Warning: Firstzone != Norm_firstzone\n");
666 errors_uncorrected = 1;
670 printf("%ld inodes\n"
672 "Firstdatazone=%ld (%ld)\n"
675 "Filesystem state=%d\n"
679 FIRSTZONE, NORM_FIRSTZONE,
680 BLOCK_SIZE << ZONESIZE,
687 static struct minix_inode *get_inode(unsigned int nr)
689 struct minix_inode *inode;
691 if (!nr || nr > INODES)
695 if (!inode_count[nr]) {
696 if (!inode_in_use(nr)) {
697 printf("Inode %d marked not used, but used for file '", nr);
698 print_current_name();
701 if (ask("Mark in use", 1))
704 errors_uncorrected = 1;
707 if (S_ISDIR(inode->i_mode))
709 else if (S_ISREG(inode->i_mode))
711 else if (S_ISCHR(inode->i_mode))
713 else if (S_ISBLK(inode->i_mode))
715 else if (S_ISLNK(inode->i_mode))
717 else if (S_ISSOCK(inode->i_mode));
718 else if (S_ISFIFO(inode->i_mode));
720 print_current_name();
721 printf(" has mode %05o\n", inode->i_mode);
726 if (!++inode_count[nr]) {
727 printf("Warning: inode count too big\n");
729 errors_uncorrected = 1;
734 #ifdef CONFIG_FEATURE_MINIX2
735 static struct minix2_inode *get_inode2(unsigned int nr)
737 struct minix2_inode *inode;
739 if (!nr || nr > INODES)
743 if (!inode_count[nr]) {
744 if (!inode_in_use(nr)) {
745 printf("Inode %d marked not used, but used for file '", nr);
746 print_current_name();
749 if (ask("Mark in use", 1))
752 errors_uncorrected = 1;
755 if (S_ISDIR(inode->i_mode))
757 else if (S_ISREG(inode->i_mode))
759 else if (S_ISCHR(inode->i_mode))
761 else if (S_ISBLK(inode->i_mode))
763 else if (S_ISLNK(inode->i_mode))
765 else if (S_ISSOCK(inode->i_mode));
766 else if (S_ISFIFO(inode->i_mode));
768 print_current_name();
769 printf(" has mode %05o\n", inode->i_mode);
773 if (!++inode_count[nr]) {
774 printf("Warning: inode count too big\n");
776 errors_uncorrected = 1;
782 static void check_root(void)
784 struct minix_inode *inode = Inode + ROOT_INO;
786 if (!inode || !S_ISDIR(inode->i_mode))
787 die("root inode isn't a directory");
790 #ifdef CONFIG_FEATURE_MINIX2
791 static void check_root2(void)
793 struct minix2_inode *inode = Inode2 + ROOT_INO;
795 if (!inode || !S_ISDIR(inode->i_mode))
796 die("root inode isn't a directory");
800 static int add_zone(uint16_t *znr, int *corrected)
806 block = check_zone_nr(znr, corrected);
809 if (zone_count[block]) {
810 printf("Block has been used before. Now in file `");
811 print_current_name();
813 if (ask("Clear", 1)) {
820 if (!zone_in_use(block)) {
821 printf("Block %d in file `", block);
822 print_current_name();
823 printf("' is marked not in use.");
824 if (ask("Correct", 1))
827 if (!++zone_count[block])
832 #ifdef CONFIG_FEATURE_MINIX2
833 static int add_zone2(uint32_t *znr, int *corrected)
839 block = check_zone_nr2(znr, corrected);
842 if (zone_count[block]) {
843 printf("Block has been used before. Now in file `");
844 print_current_name();
846 if (ask("Clear", 1)) {
853 if (!zone_in_use(block)) {
854 printf("Block %d in file `", block);
855 print_current_name();
856 printf("' is marked not in use.");
857 if (ask("Correct", 1))
860 if (!++zone_count[block])
866 static void add_zone_ind(uint16_t *znr, int *corrected)
868 static char blk[BLOCK_SIZE];
872 block = add_zone(znr, corrected);
875 read_block(block, blk);
876 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
877 add_zone(i + (uint16_t *) blk, &chg_blk);
879 write_block(block, blk);
882 #ifdef CONFIG_FEATURE_MINIX2
883 static void add_zone_ind2(uint32_t *znr, int *corrected)
885 static char blk[BLOCK_SIZE];
889 block = add_zone2(znr, corrected);
892 read_block(block, blk);
893 for (i = 0; i < BLOCK_SIZE >> 2; i++)
894 add_zone2(i + (uint32_t *) blk, &chg_blk);
896 write_block(block, blk);
900 static void add_zone_dind(uint16_t *znr, int *corrected)
902 static char blk[BLOCK_SIZE];
906 block = add_zone(znr, corrected);
909 read_block(block, blk);
910 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
911 add_zone_ind(i + (uint16_t *) blk, &blk_chg);
913 write_block(block, blk);
916 #ifdef CONFIG_FEATURE_MINIX2
917 static void add_zone_dind2(uint32_t *znr, int *corrected)
919 static char blk[BLOCK_SIZE];
923 block = add_zone2(znr, corrected);
926 read_block(block, blk);
927 for (i = 0; i < BLOCK_SIZE >> 2; i++)
928 add_zone_ind2(i + (uint32_t *) blk, &blk_chg);
930 write_block(block, blk);
933 static void add_zone_tind2(uint32_t *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_dind2(i + (uint32_t *) blk, &blk_chg);
946 write_block(block, blk);
950 static void check_zones(unsigned int i)
952 struct minix_inode *inode;
954 if (!i || i > INODES)
956 if (inode_count[i] > 1) /* have we counted this file already? */
959 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
960 !S_ISLNK(inode->i_mode)) return;
961 for (i = 0; i < 7; i++)
962 add_zone(i + inode->i_zone, &changed);
963 add_zone_ind(7 + inode->i_zone, &changed);
964 add_zone_dind(8 + inode->i_zone, &changed);
967 #ifdef CONFIG_FEATURE_MINIX2
968 static void check_zones2(unsigned int i)
970 struct minix2_inode *inode;
972 if (!i || i > INODES)
974 if (inode_count[i] > 1) /* have we counted this file already? */
977 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
978 && !S_ISLNK(inode->i_mode))
980 for (i = 0; i < 7; i++)
981 add_zone2(i + inode->i_zone, &changed);
982 add_zone_ind2(7 + inode->i_zone, &changed);
983 add_zone_dind2(8 + inode->i_zone, &changed);
984 add_zone_tind2(9 + inode->i_zone, &changed);
988 static void check_file(struct minix_inode *dir, unsigned int offset)
990 static char blk[BLOCK_SIZE];
991 struct minix_inode *inode;
996 block = map_block(dir, offset / BLOCK_SIZE);
997 read_block(block, blk);
998 name = blk + (offset % BLOCK_SIZE) + 2;
999 ino = *(uint16_t *) (name - 2);
1001 print_current_name();
1002 printf(" contains a bad inode number for file '");
1003 printf("%.*s'.", namelen, name);
1004 if (ask(" Remove", 1)) {
1005 *(uint16_t *) (name - 2) = 0;
1006 write_block(block, blk);
1010 if (name_depth < MAX_DEPTH)
1011 strncpy(name_list[name_depth], name, namelen);
1013 inode = get_inode(ino);
1016 if (!inode || strcmp(".", name)) {
1017 print_current_name();
1018 printf(": bad directory: '.' isn't first\n");
1019 errors_uncorrected = 1;
1023 if (offset == dirsize) {
1024 if (!inode || strcmp("..", name)) {
1025 print_current_name();
1026 printf(": bad directory: '..' isn't second\n");
1027 errors_uncorrected = 1;
1033 if (name_depth < MAX_DEPTH)
1034 strncpy(name_list[name_depth], name, namelen);
1038 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1039 print_current_name();
1040 if (S_ISDIR(inode->i_mode))
1046 if (inode && S_ISDIR(inode->i_mode))
1047 recursive_check(ino);
1052 #ifdef CONFIG_FEATURE_MINIX2
1053 static void check_file2(struct minix2_inode *dir, unsigned int offset)
1055 static char blk[BLOCK_SIZE];
1056 struct minix2_inode *inode;
1061 block = map_block2(dir, offset / BLOCK_SIZE);
1062 read_block(block, blk);
1063 name = blk + (offset % BLOCK_SIZE) + 2;
1064 ino = *(uint16_t *) (name - 2);
1066 print_current_name();
1067 printf(" contains a bad inode number for file '");
1068 printf("%.*s'.", namelen, name);
1069 if (ask(" Remove", 1)) {
1070 *(uint16_t *) (name - 2) = 0;
1071 write_block(block, blk);
1075 if (name_depth < MAX_DEPTH)
1076 strncpy(name_list[name_depth], name, namelen);
1078 inode = get_inode2(ino);
1081 if (!inode || strcmp(".", name)) {
1082 print_current_name();
1083 printf(": bad directory: '.' isn't first\n");
1084 errors_uncorrected = 1;
1088 if (offset == dirsize) {
1089 if (!inode || strcmp("..", name)) {
1090 print_current_name();
1091 printf(": bad directory: '..' isn't second\n");
1092 errors_uncorrected = 1;
1101 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1102 print_current_name();
1103 if (S_ISDIR(inode->i_mode))
1109 if (inode && S_ISDIR(inode->i_mode))
1110 recursive_check2(ino);
1116 static void recursive_check(unsigned int ino)
1118 struct minix_inode *dir;
1119 unsigned int offset;
1122 if (!S_ISDIR(dir->i_mode))
1123 die("internal error");
1124 if (dir->i_size < 2 * dirsize) {
1125 print_current_name();
1126 printf(": bad directory: size<32");
1127 errors_uncorrected = 1;
1129 for (offset = 0; offset < dir->i_size; offset += dirsize)
1130 check_file(dir, offset);
1133 #ifdef CONFIG_FEATURE_MINIX2
1134 static void recursive_check2(unsigned int ino)
1136 struct minix2_inode *dir;
1137 unsigned int offset;
1140 if (!S_ISDIR(dir->i_mode))
1141 die("internal error");
1142 if (dir->i_size < 2 * dirsize) {
1143 print_current_name();
1144 printf(": bad directory: size < 32");
1145 errors_uncorrected = 1;
1147 for (offset = 0; offset < dir->i_size; offset += dirsize)
1148 check_file2(dir, offset);
1152 static int bad_zone(int i)
1156 if (BLOCK_SIZE * i != lseek(IN, BLOCK_SIZE * i, SEEK_SET))
1157 die("seek failed in bad_zone");
1158 return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE));
1161 static void check_counts(void)
1165 for (i = 1; i <= INODES; i++) {
1166 if (warn_mode && Inode[i].i_mode && !inode_in_use(i)) {
1167 printf("Inode %d mode not cleared.", i);
1168 if (ask("Clear", 1)) {
1169 Inode[i].i_mode = 0;
1173 if (!inode_count[i]) {
1174 if (!inode_in_use(i))
1176 printf("Inode %d not used, marked used in the bitmap.", i);
1177 if (ask("Clear", 1))
1181 if (!inode_in_use(i)) {
1182 printf("Inode %d used, marked unused in the bitmap.", i);
1186 if (Inode[i].i_nlinks != inode_count[i]) {
1187 printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
1188 i, Inode[i].i_mode, Inode[i].i_nlinks, inode_count[i]);
1189 if (ask("Set i_nlinks to count", 1)) {
1190 Inode[i].i_nlinks = inode_count[i];
1195 for (i = FIRSTZONE; i < ZONES; i++) {
1196 if (zone_in_use(i) == zone_count[i])
1198 if (!zone_count[i]) {
1201 printf("Zone %d: marked in use, no file uses it.", i);
1202 if (ask("Unmark", 1))
1206 printf("Zone %d: %sin use, counted=%d\n",
1207 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1211 #ifdef CONFIG_FEATURE_MINIX2
1212 static void check_counts2(void)
1216 for (i = 1; i <= INODES; i++) {
1217 if (warn_mode && Inode2[i].i_mode && !inode_in_use(i)) {
1218 printf("Inode %d mode not cleared.", i);
1219 if (ask("Clear", 1)) {
1220 Inode2[i].i_mode = 0;
1224 if (!inode_count[i]) {
1225 if (!inode_in_use(i))
1227 printf("Inode %d not used, marked used in the bitmap.", i);
1228 if (ask("Clear", 1))
1232 if (!inode_in_use(i)) {
1233 printf("Inode %d used, marked unused in the bitmap.", i);
1237 if (Inode2[i].i_nlinks != inode_count[i]) {
1238 printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
1239 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1241 if (ask("Set i_nlinks to count", 1)) {
1242 Inode2[i].i_nlinks = inode_count[i];
1247 for (i = FIRSTZONE; i < ZONES; i++) {
1248 if (zone_in_use(i) == zone_count[i])
1250 if (!zone_count[i]) {
1253 printf("Zone %d: marked in use, no file uses it.", i);
1254 if (ask("Unmark", 1))
1258 printf("Zone %d: %sin use, counted=%d\n",
1259 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1264 static void check(void)
1266 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1267 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1268 check_zones(ROOT_INO);
1269 recursive_check(ROOT_INO);
1273 #ifdef CONFIG_FEATURE_MINIX2
1274 static void check2(void)
1276 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1277 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1278 check_zones2(ROOT_INO);
1279 recursive_check2(ROOT_INO);
1284 /* Wed Feb 9 15:17:06 MST 2000 */
1285 /* dynamically allocate name_list (instead of making it static) */
1286 static void alloc_name_list(void)
1290 name_list = xmalloc(sizeof(char *) * MAX_DEPTH);
1291 for (i = 0; i < MAX_DEPTH; i++)
1292 name_list[i] = xmalloc(sizeof(char) * (BUFSIZ + 1));
1295 #ifdef CONFIG_FEATURE_CLEAN_UP
1296 /* execute this atexit() to deallocate name_list[] */
1297 /* piptigger was here */
1298 static void free_name_list(void)
1303 for (i = 0; i < MAX_DEPTH; i++) {
1311 int fsck_minix_main(int argc, char **argv)
1317 #ifdef CONFIG_FEATURE_CLEAN_UP
1318 /* Don't bother to free memory. Exit does
1319 * that automagically, so we can save a few bytes */
1320 atexit(free_name_list);
1323 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
1324 die("bad inode size");
1325 #ifdef CONFIG_FEATURE_MINIX2
1326 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
1327 die("bad v2 inode size");
1329 while (argc-- > 1) {
1331 if (argv[0][0] != '-') {
1335 device_name = argv[0];
1338 switch (argv[0][0]) {
1368 check_mount(); /* trying to check a mounted filesystem? */
1369 if (repair && !automatic) {
1370 if (!isatty(0) || !isatty(1))
1371 die("need terminal for interactive repairs");
1373 IN = open(device_name, repair ? O_RDWR : O_RDONLY);
1375 fprintf(stderr,"unable to open device '%s'\n",device_name);
1378 sync(); /* paranoia? */
1382 * Determine whether or not we should continue with the checking.
1383 * This is based on the status of the filesystem valid and error
1384 * flags and whether or not the -f switch was specified on the
1387 printf("%s, %s\n", bb_applet_name, program_version);
1388 if (!(Super.s_state & MINIX_ERROR_FS) &&
1389 (Super.s_state & MINIX_VALID_FS) && !force) {
1391 printf("%s is clean, no check\n", device_name);
1394 printf("Forcing filesystem check on %s\n", device_name);
1396 printf("Filesystem on %s is dirty, needs checking\n",
1401 if (repair && !automatic) {
1402 tcgetattr(0, &termios);
1404 tmp.c_lflag &= ~(ICANON | ECHO);
1405 tcsetattr(0, TCSANOW, &tmp);
1408 #ifdef CONFIG_FEATURE_MINIX2
1421 for (i = 1, free_cnt = 0; i <= INODES; i++)
1422 if (!inode_in_use(i))
1424 printf("\n%6ld inodes used (%ld%%)\n", (INODES - free_cnt),
1425 100 * (INODES - free_cnt) / INODES);
1426 for (i = FIRSTZONE, free_cnt = 0; i < ZONES; i++)
1427 if (!zone_in_use(i))
1429 printf("%6ld zones used (%ld%%)\n", (ZONES - free_cnt),
1430 100 * (ZONES - free_cnt) / ZONES);
1431 printf("\n%6d regular files\n"
1433 "%6d character device files\n"
1434 "%6d block device files\n"
1436 "%6d symbolic links\n"
1439 regular, directory, chardev, blockdev,
1440 links - 2 * directory + 1, symlinks,
1441 total - 2 * directory + 1);
1445 printf("----------------------------\n"
1446 "FILE SYSTEM HAS BEEN CHANGED\n"
1447 "----------------------------\n");
1450 write_super_block();
1452 if (repair && !automatic)
1453 tcsetattr(0, TCSANOW, &termios);
1457 if (errors_uncorrected)