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 :-).
99 #include <sys/param.h>
102 typedef unsigned char u8;
103 typedef unsigned short u16;
104 typedef unsigned int u32;
107 #define MINIX_ROOT_INO 1
108 #define MINIX_LINK_MAX 250
109 #define MINIX2_LINK_MAX 65530
111 #define MINIX_I_MAP_SLOTS 8
112 #define MINIX_Z_MAP_SLOTS 64
113 #define MINIX_SUPER_MAGIC 0x137F /* original minix fs */
114 #define MINIX_SUPER_MAGIC2 0x138F /* minix fs, 30 char names */
115 #define MINIX2_SUPER_MAGIC 0x2468 /* minix V2 fs */
116 #define MINIX2_SUPER_MAGIC2 0x2478 /* minix V2 fs, 30 char names */
117 #define MINIX_VALID_FS 0x0001 /* Clean fs. */
118 #define MINIX_ERROR_FS 0x0002 /* fs has errors. */
120 #define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
121 #define MINIX2_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix2_inode)))
123 #define MINIX_V1 0x0001 /* original minix fs */
124 #define MINIX_V2 0x0002 /* minix V2 fs */
126 #define INODE_VERSION(inode) inode->i_sb->u.minix_sb.s_version
129 * This is the original minix inode layout on disk.
130 * Note the 8-bit gid and atime and ctime.
143 * The new minix inode has all the time entries, as well as
144 * long block numbers and a third indirect block (7+1+1+1
145 * instead of 7+1+1). Also, some previously 8-bit values are
146 * now 16-bit. The inode is now 64 bytes instead of 32.
148 struct minix2_inode {
161 * minix super-block data on disk
163 struct minix_super_block {
176 struct minix_dir_entry {
181 #define BLOCK_SIZE_BITS 10
182 #define BLOCK_SIZE (1<<BLOCK_SIZE_BITS)
184 #define NAME_MAX 255 /* # chars in a file name */
186 #define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
188 #define MINIX_VALID_FS 0x0001 /* Clean fs. */
189 #define MINIX_ERROR_FS 0x0002 /* fs has errors. */
191 #define MINIX_SUPER_MAGIC 0x137F /* original minix fs */
192 #define MINIX_SUPER_MAGIC2 0x138F /* minix fs, 30 char names */
195 #define BLKGETSIZE _IO(0x12,96) /* return device size */
204 #define UPPER(size,n) ((size+((n)-1))/(n))
205 #define INODE_SIZE (sizeof(struct minix_inode))
206 #ifdef BB_FEATURE_MINIX2
207 #define INODE_SIZE2 (sizeof(struct minix2_inode))
208 #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
209 : MINIX_INODES_PER_BLOCK))
211 #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
213 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
215 #define BITS_PER_BLOCK (BLOCK_SIZE<<3)
217 static char *program_version = "1.2 - 11/11/96";
218 static char *device_name = NULL;
220 static int repair = 0, automatic = 0, verbose = 0, list = 0, show =
221 0, warn_mode = 0, force = 0;
222 static int directory = 0, regular = 0, blockdev = 0, chardev = 0, links =
223 0, symlinks = 0, total = 0;
225 static int changed = 0; /* flags if the filesystem has been changed */
226 static int errors_uncorrected = 0; /* flag if some error was not corrected */
227 static int dirsize = 16;
228 static int namelen = 14;
229 static int version2 = 0;
230 static struct termios termios;
231 static int termios_set = 0;
235 static int name_depth = 0;
236 // static char name_list[MAX_DEPTH][BUFSIZ + 1];
237 static char **name_list = NULL;
239 static char *inode_buffer = NULL;
241 #define Inode (((struct minix_inode *) inode_buffer)-1)
242 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
243 static char super_block_buffer[BLOCK_SIZE];
245 #define Super (*(struct minix_super_block *)super_block_buffer)
246 #define INODES ((unsigned long)Super.s_ninodes)
247 #ifdef BB_FEATURE_MINIX2
248 #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
250 #define ZONES ((unsigned long)(Super.s_nzones))
252 #define IMAPS ((unsigned long)Super.s_imap_blocks)
253 #define ZMAPS ((unsigned long)Super.s_zmap_blocks)
254 #define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
255 #define ZONESIZE ((unsigned long)Super.s_log_zone_size)
256 #define MAXSIZE ((unsigned long)Super.s_max_size)
257 #define MAGIC (Super.s_magic)
258 #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
260 static char *inode_map;
261 static char *zone_map;
263 static unsigned char *inode_count = NULL;
264 static unsigned char *zone_count = NULL;
266 static void recursive_check(unsigned int ino);
267 #ifdef BB_FEATURE_MINIX2
268 static void recursive_check2(unsigned int ino);
271 #define inode_in_use(x) (isset(inode_map,(x)))
272 #define zone_in_use(x) (isset(zone_map,(x)-FIRSTZONE+1))
274 #define mark_inode(x) (setbit(inode_map,(x)),changed=1)
275 #define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
277 #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1)
278 #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1)
280 static void leave(int) __attribute__ ((noreturn));
281 static void leave(int status)
284 tcsetattr(0, TCSANOW, &termios);
288 static void show_usage(void)
290 usage(fsck_minix_usage);
293 static void die(const char *str)
295 errorMsg("%s\n", str);
300 * This simply goes through the file-name data and prints out the
303 static void print_current_name(void)
307 while (i < name_depth)
308 printf("/%.*s", namelen, name_list[i++]);
313 static int ask(const char *string, int def)
319 errors_uncorrected = 1;
325 errors_uncorrected = 1;
328 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
331 if ((c = getchar()) == EOF) {
333 errors_uncorrected = 1;
340 } else if (c == 'N') {
343 } else if (c == ' ' || c == '\n')
350 errors_uncorrected = 1;
356 * Make certain that we aren't checking a filesystem that is on a
357 * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
358 * 1994 Theodore Ts'o. Also licensed under GPL.
360 static void check_mount(void)
367 if ((f = setmntent(MOUNTED, "r")) == NULL)
369 while ((mnt = getmntent(f)) != NULL)
370 if (strcmp(device_name, mnt->mnt_fsname) == 0)
377 * If the root is mounted read-only, then /etc/mtab is
378 * probably not correct; so we won't issue a warning based on
381 fd = open(MOUNTED, O_RDWR);
382 if (fd < 0 && errno == EROFS)
387 printf("%s is mounted. ", device_name);
388 if (isatty(0) && isatty(1))
389 cont = ask("Do you really want to continue", 0);
393 printf("check aborted.\n");
400 * check_zone_nr checks to see that *nr is a valid zone nr. If it
401 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
402 * if an error was corrected, and returns the zone (0 for no zone
403 * or a bad zone-number).
405 static int check_zone_nr(unsigned short *nr, int *corrected)
410 printf("Zone nr < FIRSTZONE in file `");
411 else if (*nr >= ZONES)
412 printf("Zone nr >= ZONES in file `");
415 print_current_name();
417 if (ask("Remove block", 1)) {
424 #ifdef BB_FEATURE_MINIX2
425 static int check_zone_nr2(unsigned int *nr, int *corrected)
430 printf("Zone nr < FIRSTZONE in file `");
431 else if (*nr >= ZONES)
432 printf("Zone nr >= ZONES in file `");
435 print_current_name();
437 if (ask("Remove block", 1)) {
446 * read-block reads block nr into the buffer at addr.
448 static void read_block(unsigned int nr, char *addr)
451 memset(addr, 0, BLOCK_SIZE);
454 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) {
455 printf("Read error: unable to seek to block in file '");
456 print_current_name();
458 memset(addr, 0, BLOCK_SIZE);
459 errors_uncorrected = 1;
460 } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) {
461 printf("Read error: bad block in file '");
462 print_current_name();
464 memset(addr, 0, BLOCK_SIZE);
465 errors_uncorrected = 1;
470 * write_block writes block nr to disk.
472 static void write_block(unsigned int nr, char *addr)
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;
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 '");
486 print_current_name();
488 errors_uncorrected = 1;
493 * map-block calculates the absolute block nr of a block in a file.
494 * It sets 'changed' if the inode has needed changing, and re-writes
495 * any indirect blocks with errors.
497 static int map_block(struct minix_inode *inode, unsigned int blknr)
499 unsigned short ind[BLOCK_SIZE >> 1];
500 unsigned short dind[BLOCK_SIZE >> 1];
501 int blk_chg, block, result;
504 return check_zone_nr(inode->i_zone + blknr, &changed);
507 block = check_zone_nr(inode->i_zone + 7, &changed);
508 read_block(block, (char *) ind);
510 result = check_zone_nr(blknr + ind, &blk_chg);
512 write_block(block, (char *) ind);
516 block = check_zone_nr(inode->i_zone + 8, &changed);
517 read_block(block, (char *) dind);
519 result = check_zone_nr(dind + (blknr / 512), &blk_chg);
521 write_block(block, (char *) dind);
523 read_block(block, (char *) ind);
525 result = check_zone_nr(ind + (blknr % 512), &blk_chg);
527 write_block(block, (char *) ind);
531 #ifdef BB_FEATURE_MINIX2
532 static int map_block2(struct minix2_inode *inode, unsigned int blknr)
534 unsigned int ind[BLOCK_SIZE >> 2];
535 unsigned int dind[BLOCK_SIZE >> 2];
536 unsigned int tind[BLOCK_SIZE >> 2];
537 int blk_chg, block, result;
540 return check_zone_nr2(inode->i_zone + blknr, &changed);
543 block = check_zone_nr2(inode->i_zone + 7, &changed);
544 read_block(block, (char *) ind);
546 result = check_zone_nr2(blknr + ind, &blk_chg);
548 write_block(block, (char *) ind);
552 if (blknr >= 256 * 256) {
553 block = check_zone_nr2(inode->i_zone + 8, &changed);
554 read_block(block, (char *) dind);
556 result = check_zone_nr2(dind + blknr / 256, &blk_chg);
558 write_block(block, (char *) dind);
560 read_block(block, (char *) ind);
562 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
564 write_block(block, (char *) ind);
568 block = check_zone_nr2(inode->i_zone + 9, &changed);
569 read_block(block, (char *) tind);
571 result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
573 write_block(block, (char *) tind);
575 read_block(block, (char *) dind);
577 result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
579 write_block(block, (char *) dind);
581 read_block(block, (char *) ind);
583 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
585 write_block(block, (char *) ind);
590 static void write_super_block(void)
593 * Set the state of the filesystem based on whether or not there
594 * are uncorrected errors. The filesystem valid flag is
595 * unconditionally set if we get this far.
597 Super.s_state |= MINIX_VALID_FS;
598 if (errors_uncorrected)
599 Super.s_state |= MINIX_ERROR_FS;
601 Super.s_state &= ~MINIX_ERROR_FS;
603 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
604 die("seek failed in write_super_block");
605 if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE))
606 die("unable to write super-block");
611 static void write_tables(void)
615 if (IMAPS * BLOCK_SIZE != write(IN, inode_map, IMAPS * BLOCK_SIZE))
616 die("Unable to write inode map");
617 if (ZMAPS * BLOCK_SIZE != write(IN, zone_map, ZMAPS * BLOCK_SIZE))
618 die("Unable to write zone map");
619 if (INODE_BUFFER_SIZE != write(IN, inode_buffer, INODE_BUFFER_SIZE))
620 die("Unable to write inodes");
623 static void get_dirsize(void)
626 char blk[BLOCK_SIZE];
629 #ifdef BB_FEATURE_MINIX2
631 block = Inode2[ROOT_INO].i_zone[0];
634 block = Inode[ROOT_INO].i_zone[0];
635 read_block(block, blk);
636 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
637 if (strcmp(blk + size + 2, "..") == 0) {
646 static void read_superblock(void)
648 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
650 if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
651 die("unable to read super block");
652 if (MAGIC == MINIX_SUPER_MAGIC) {
656 } else if (MAGIC == MINIX_SUPER_MAGIC2) {
660 #ifdef BB_FEATURE_MINIX2
661 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
665 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
671 die("bad magic number in super-block");
672 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
673 die("Only 1k blocks/zones supported");
674 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
675 die("bad s_imap_blocks field in super-block");
676 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
677 die("bad s_zmap_blocks field in super-block");
680 static void read_tables(void)
682 inode_map = xmalloc(IMAPS * BLOCK_SIZE);
683 zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
684 memset(inode_map, 0, sizeof(inode_map));
685 memset(zone_map, 0, sizeof(zone_map));
686 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
687 inode_count = xmalloc(INODES + 1);
688 zone_count = xmalloc(ZONES);
689 if (IMAPS * BLOCK_SIZE != read(IN, inode_map, IMAPS * BLOCK_SIZE))
690 die("Unable to read inode map");
691 if (ZMAPS * BLOCK_SIZE != read(IN, zone_map, ZMAPS * BLOCK_SIZE))
692 die("Unable to read zone map");
693 if (INODE_BUFFER_SIZE != read(IN, inode_buffer, INODE_BUFFER_SIZE))
694 die("Unable to read inodes");
695 if (NORM_FIRSTZONE != FIRSTZONE) {
696 printf("Warning: Firstzone != Norm_firstzone\n");
697 errors_uncorrected = 1;
701 printf("%ld inodes\n", INODES);
702 printf("%ld blocks\n", ZONES);
703 printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
704 printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
705 printf("Maxsize=%ld\n", MAXSIZE);
706 printf("Filesystem state=%d\n", Super.s_state);
707 printf("namelen=%d\n\n", namelen);
711 struct minix_inode *get_inode(unsigned int nr)
713 struct minix_inode *inode;
715 if (!nr || nr > INODES)
719 if (!inode_count[nr]) {
720 if (!inode_in_use(nr)) {
721 printf("Inode %d marked not used, but used for file '", nr);
722 print_current_name();
725 if (ask("Mark in use", 1))
728 errors_uncorrected = 1;
731 if (S_ISDIR(inode->i_mode))
733 else if (S_ISREG(inode->i_mode))
735 else if (S_ISCHR(inode->i_mode))
737 else if (S_ISBLK(inode->i_mode))
739 else if (S_ISLNK(inode->i_mode))
741 else if (S_ISSOCK(inode->i_mode));
742 else if (S_ISFIFO(inode->i_mode));
744 print_current_name();
745 printf(" has mode %05o\n", inode->i_mode);
750 if (!++inode_count[nr]) {
751 printf("Warning: inode count too big.\n");
753 errors_uncorrected = 1;
758 #ifdef BB_FEATURE_MINIX2
759 struct minix2_inode *get_inode2(unsigned int nr)
761 struct minix2_inode *inode;
763 if (!nr || nr > INODES)
767 if (!inode_count[nr]) {
768 if (!inode_in_use(nr)) {
769 printf("Inode %d marked not used, but used for file '", nr);
770 print_current_name();
773 if (ask("Mark in use", 1))
776 errors_uncorrected = 1;
779 if (S_ISDIR(inode->i_mode))
781 else if (S_ISREG(inode->i_mode))
783 else if (S_ISCHR(inode->i_mode))
785 else if (S_ISBLK(inode->i_mode))
787 else if (S_ISLNK(inode->i_mode))
789 else if (S_ISSOCK(inode->i_mode));
790 else if (S_ISFIFO(inode->i_mode));
792 print_current_name();
793 printf(" has mode %05o\n", inode->i_mode);
797 if (!++inode_count[nr]) {
798 printf("Warning: inode count too big.\n");
800 errors_uncorrected = 1;
806 static void check_root(void)
808 struct minix_inode *inode = Inode + ROOT_INO;
810 if (!inode || !S_ISDIR(inode->i_mode))
811 die("root inode isn't a directory");
814 #ifdef BB_FEATURE_MINIX2
815 static void check_root2(void)
817 struct minix2_inode *inode = Inode2 + ROOT_INO;
819 if (!inode || !S_ISDIR(inode->i_mode))
820 die("root inode isn't a directory");
824 static int add_zone(unsigned short *znr, int *corrected)
830 block = check_zone_nr(znr, corrected);
833 if (zone_count[block]) {
834 printf("Block has been used before. Now in file `");
835 print_current_name();
837 if (ask("Clear", 1)) {
845 if (!zone_in_use(block)) {
846 printf("Block %d in file `", block);
847 print_current_name();
848 printf("' is marked not in use.");
849 if (ask("Correct", 1))
852 if (!++zone_count[block])
857 #ifdef BB_FEATURE_MINIX2
858 static int add_zone2(unsigned int *znr, int *corrected)
864 block = check_zone_nr2(znr, corrected);
867 if (zone_count[block]) {
868 printf("Block has been used before. Now in file `");
869 print_current_name();
871 if (ask("Clear", 1)) {
879 if (!zone_in_use(block)) {
880 printf("Block %d in file `", block);
881 print_current_name();
882 printf("' is marked not in use.");
883 if (ask("Correct", 1))
886 if (!++zone_count[block])
892 static void add_zone_ind(unsigned short *znr, int *corrected)
894 static char blk[BLOCK_SIZE];
898 block = add_zone(znr, corrected);
901 read_block(block, blk);
902 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
903 add_zone(i + (unsigned short *) blk, &chg_blk);
905 write_block(block, blk);
908 #ifdef BB_FEATURE_MINIX2
909 static void add_zone_ind2(unsigned int *znr, int *corrected)
911 static char blk[BLOCK_SIZE];
915 block = add_zone2(znr, corrected);
918 read_block(block, blk);
919 for (i = 0; i < BLOCK_SIZE >> 2; i++)
920 add_zone2(i + (unsigned int *) blk, &chg_blk);
922 write_block(block, blk);
926 static void add_zone_dind(unsigned short *znr, int *corrected)
928 static char blk[BLOCK_SIZE];
932 block = add_zone(znr, corrected);
935 read_block(block, blk);
936 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
937 add_zone_ind(i + (unsigned short *) blk, &blk_chg);
939 write_block(block, blk);
942 #ifdef BB_FEATURE_MINIX2
943 static void add_zone_dind2(unsigned int *znr, int *corrected)
945 static char blk[BLOCK_SIZE];
949 block = add_zone2(znr, corrected);
952 read_block(block, blk);
953 for (i = 0; i < BLOCK_SIZE >> 2; i++)
954 add_zone_ind2(i + (unsigned int *) blk, &blk_chg);
956 write_block(block, blk);
959 static void add_zone_tind2(unsigned int *znr, int *corrected)
961 static char blk[BLOCK_SIZE];
965 block = add_zone2(znr, corrected);
968 read_block(block, blk);
969 for (i = 0; i < BLOCK_SIZE >> 2; i++)
970 add_zone_dind2(i + (unsigned int *) blk, &blk_chg);
972 write_block(block, blk);
976 static void check_zones(unsigned int i)
978 struct minix_inode *inode;
980 if (!i || i > INODES)
982 if (inode_count[i] > 1) /* have we counted this file already? */
985 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
986 !S_ISLNK(inode->i_mode)) return;
987 for (i = 0; i < 7; i++)
988 add_zone(i + inode->i_zone, &changed);
989 add_zone_ind(7 + inode->i_zone, &changed);
990 add_zone_dind(8 + inode->i_zone, &changed);
993 #ifdef BB_FEATURE_MINIX2
994 static void check_zones2(unsigned int i)
996 struct minix2_inode *inode;
998 if (!i || i > INODES)
1000 if (inode_count[i] > 1) /* have we counted this file already? */
1003 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
1004 && !S_ISLNK(inode->i_mode))
1006 for (i = 0; i < 7; i++)
1007 add_zone2(i + inode->i_zone, &changed);
1008 add_zone_ind2(7 + inode->i_zone, &changed);
1009 add_zone_dind2(8 + inode->i_zone, &changed);
1010 add_zone_tind2(9 + inode->i_zone, &changed);
1014 static void check_file(struct minix_inode *dir, unsigned int offset)
1016 static char blk[BLOCK_SIZE];
1017 struct minix_inode *inode;
1022 block = map_block(dir, offset / BLOCK_SIZE);
1023 read_block(block, blk);
1024 name = blk + (offset % BLOCK_SIZE) + 2;
1025 ino = *(unsigned short *) (name - 2);
1027 print_current_name();
1028 printf(" contains a bad inode number for file '");
1029 printf("%.*s'.", namelen, name);
1030 if (ask(" Remove", 1)) {
1031 *(unsigned short *) (name - 2) = 0;
1032 write_block(block, blk);
1036 if (name_depth < MAX_DEPTH)
1037 strncpy(name_list[name_depth], name, namelen);
1039 inode = get_inode(ino);
1042 if (!inode || strcmp(".", name)) {
1043 print_current_name();
1044 printf(": bad directory: '.' isn't first\n");
1045 errors_uncorrected = 1;
1049 if (offset == dirsize) {
1050 if (!inode || strcmp("..", name)) {
1051 print_current_name();
1052 printf(": bad directory: '..' isn't second\n");
1053 errors_uncorrected = 1;
1059 if (name_depth < MAX_DEPTH)
1060 strncpy(name_list[name_depth], name, namelen);
1064 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1065 print_current_name();
1066 if (S_ISDIR(inode->i_mode))
1072 if (inode && S_ISDIR(inode->i_mode))
1073 recursive_check(ino);
1078 #ifdef BB_FEATURE_MINIX2
1079 static void check_file2(struct minix2_inode *dir, unsigned int offset)
1081 static char blk[BLOCK_SIZE];
1082 struct minix2_inode *inode;
1087 block = map_block2(dir, offset / BLOCK_SIZE);
1088 read_block(block, blk);
1089 name = blk + (offset % BLOCK_SIZE) + 2;
1090 ino = *(unsigned short *) (name - 2);
1092 print_current_name();
1093 printf(" contains a bad inode number for file '");
1094 printf("%.*s'.", namelen, name);
1095 if (ask(" Remove", 1)) {
1096 *(unsigned short *) (name - 2) = 0;
1097 write_block(block, blk);
1101 if (name_depth < MAX_DEPTH)
1102 strncpy(name_list[name_depth], name, namelen);
1104 inode = get_inode2(ino);
1107 if (!inode || strcmp(".", name)) {
1108 print_current_name();
1109 printf(": bad directory: '.' isn't first\n");
1110 errors_uncorrected = 1;
1114 if (offset == dirsize) {
1115 if (!inode || strcmp("..", name)) {
1116 print_current_name();
1117 printf(": bad directory: '..' isn't second\n");
1118 errors_uncorrected = 1;
1127 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1128 print_current_name();
1129 if (S_ISDIR(inode->i_mode))
1135 if (inode && S_ISDIR(inode->i_mode))
1136 recursive_check2(ino);
1142 static void recursive_check(unsigned int ino)
1144 struct minix_inode *dir;
1145 unsigned int offset;
1148 if (!S_ISDIR(dir->i_mode))
1149 die("internal error");
1150 if (dir->i_size < 2 * dirsize) {
1151 print_current_name();
1152 printf(": bad directory: size<32");
1153 errors_uncorrected = 1;
1155 for (offset = 0; offset < dir->i_size; offset += dirsize)
1156 check_file(dir, offset);
1159 #ifdef BB_FEATURE_MINIX2
1160 static void recursive_check2(unsigned int ino)
1162 struct minix2_inode *dir;
1163 unsigned int offset;
1166 if (!S_ISDIR(dir->i_mode))
1167 die("internal error");
1168 if (dir->i_size < 2 * dirsize) {
1169 print_current_name();
1170 printf(": bad directory: size < 32");
1171 errors_uncorrected = 1;
1173 for (offset = 0; offset < dir->i_size; offset += dirsize)
1174 check_file2(dir, offset);
1178 static int bad_zone(int i)
1182 if (BLOCK_SIZE * i != lseek(IN, BLOCK_SIZE * i, SEEK_SET))
1183 die("seek failed in bad_zone");
1184 return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE));
1187 static void check_counts(void)
1191 for (i = 1; i <= INODES; i++) {
1192 if (!inode_in_use(i) && Inode[i].i_mode && warn_mode) {
1193 printf("Inode %d mode not cleared.", i);
1194 if (ask("Clear", 1)) {
1195 Inode[i].i_mode = 0;
1199 if (!inode_count[i]) {
1200 if (!inode_in_use(i))
1202 printf("Inode %d not used, marked used in the bitmap.", i);
1203 if (ask("Clear", 1))
1207 if (!inode_in_use(i)) {
1208 printf("Inode %d used, marked unused in the bitmap.", i);
1212 if (Inode[i].i_nlinks != inode_count[i]) {
1213 printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
1214 i, Inode[i].i_mode, Inode[i].i_nlinks, inode_count[i]);
1215 if (ask("Set i_nlinks to count", 1)) {
1216 Inode[i].i_nlinks = inode_count[i];
1221 for (i = FIRSTZONE; i < ZONES; i++) {
1222 if (zone_in_use(i) == zone_count[i])
1224 if (!zone_count[i]) {
1227 printf("Zone %d: marked in use, no file uses it.", i);
1228 if (ask("Unmark", 1))
1232 printf("Zone %d: %sin use, counted=%d\n",
1233 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1237 #ifdef BB_FEATURE_MINIX2
1238 static void check_counts2(void)
1242 for (i = 1; i <= INODES; i++) {
1243 if (!inode_in_use(i) && Inode2[i].i_mode && warn_mode) {
1244 printf("Inode %d mode not cleared.", i);
1245 if (ask("Clear", 1)) {
1246 Inode2[i].i_mode = 0;
1250 if (!inode_count[i]) {
1251 if (!inode_in_use(i))
1253 printf("Inode %d not used, marked used in the bitmap.", i);
1254 if (ask("Clear", 1))
1258 if (!inode_in_use(i)) {
1259 printf("Inode %d used, marked unused in the bitmap.", i);
1263 if (Inode2[i].i_nlinks != inode_count[i]) {
1264 printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
1265 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1267 if (ask("Set i_nlinks to count", 1)) {
1268 Inode2[i].i_nlinks = inode_count[i];
1273 for (i = FIRSTZONE; i < ZONES; i++) {
1274 if (zone_in_use(i) == zone_count[i])
1276 if (!zone_count[i]) {
1279 printf("Zone %d: marked in use, no file uses it.", i);
1280 if (ask("Unmark", 1))
1284 printf("Zone %d: %sin use, counted=%d\n",
1285 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1290 static void check(void)
1292 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1293 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1294 check_zones(ROOT_INO);
1295 recursive_check(ROOT_INO);
1299 #ifdef BB_FEATURE_MINIX2
1300 static void check2(void)
1302 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1303 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1304 check_zones2(ROOT_INO);
1305 recursive_check2(ROOT_INO);
1310 /* Wed Feb 9 15:17:06 MST 2000 */
1311 /* dynamically allocate name_list (instead of making it static) */
1312 static void alloc_name_list(void)
1316 name_list = xmalloc(sizeof(char *) * MAX_DEPTH);
1317 for (i = 0; i < MAX_DEPTH; i++)
1318 name_list[i] = xmalloc(sizeof(char) * BUFSIZ + 1);
1321 #ifdef BB_FEATURE_CLEAN_UP
1322 /* execute this atexit() to deallocate name_list[] */
1323 /* piptigger was here */
1324 static void free_name_list(void)
1329 for (i = 0; i < MAX_DEPTH; i++) {
1339 extern int fsck_minix_main(int argc, char **argv)
1346 #ifdef BB_FEATURE_CLEAN_UP
1347 /* Don't bother to free memory. Exit does
1348 * that automagically, so we can save a few bytes */
1349 atexit(free_name_list);
1352 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
1353 die("bad inode size");
1354 #ifdef BB_FEATURE_MINIX2
1355 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
1356 die("bad v2 inode size");
1358 while (argc-- > 1) {
1360 if (argv[0][0] != '-') {
1364 device_name = argv[0];
1367 switch (argv[0][0]) {
1397 check_mount(); /* trying to check a mounted filesystem? */
1398 if (repair && !automatic) {
1399 if (!isatty(0) || !isatty(1))
1400 die("need terminal for interactive repairs");
1402 IN = open(device_name, repair ? O_RDWR : O_RDONLY);
1404 die("unable to open '%s'");
1405 for (count = 0; count < 3; count++)
1410 * Determine whether or not we should continue with the checking.
1411 * This is based on the status of the filesystem valid and error
1412 * flags and whether or not the -f switch was specified on the
1415 printf("%s, %s\n", applet_name, program_version);
1416 if (!(Super.s_state & MINIX_ERROR_FS) &&
1417 (Super.s_state & MINIX_VALID_FS) && !force) {
1419 printf("%s is clean, no check.\n", device_name);
1422 printf("Forcing filesystem check on %s.\n", device_name);
1424 printf("Filesystem on %s is dirty, needs checking.\n",
1429 if (repair && !automatic) {
1430 tcgetattr(0, &termios);
1432 tmp.c_lflag &= ~(ICANON | ECHO);
1433 tcsetattr(0, TCSANOW, &tmp);
1436 #ifdef BB_FEATURE_MINIX2
1449 for (i = 1, free = 0; i <= INODES; i++)
1450 if (!inode_in_use(i))
1452 printf("\n%6ld inodes used (%ld%%)\n", (INODES - free),
1453 100 * (INODES - free) / INODES);
1454 for (i = FIRSTZONE, free = 0; i < ZONES; i++)
1455 if (!zone_in_use(i))
1457 printf("%6ld zones used (%ld%%)\n", (ZONES - free),
1458 100 * (ZONES - free) / ZONES);
1459 printf("\n%6d regular files\n"
1461 "%6d character device files\n"
1462 "%6d block device files\n"
1464 "%6d symbolic links\n"
1467 regular, directory, chardev, blockdev,
1468 links - 2 * directory + 1, symlinks,
1469 total - 2 * directory + 1);
1473 printf("----------------------------\n"
1474 "FILE SYSTEM HAS BEEN CHANGED\n"
1475 "----------------------------\n");
1476 for (count = 0; count < 3; count++)
1479 write_super_block();
1481 if (repair && !automatic)
1482 tcsetattr(0, TCSANOW, &termios);
1486 if (errors_uncorrected)