1 /* vi: set sw=4 ts=4: */
3 * fsck.c - a file system consistency checker for Linux.
5 * (C) 1991, 1992 Linus Torvalds.
7 * Licensed under GPLv2, see file LICENSE in this tarball for details.
11 * 09.11.91 - made the first rudimentary functions
13 * 10.11.91 - updated, does checking, no repairs yet.
14 * Sent out to the mailing-list for testing.
16 * 14.11.91 - Testing seems to have gone well. Added some
17 * correction-code, and changed some functions.
19 * 15.11.91 - More correction code. Hopefully it notices most
20 * cases now, and tries to do something about them.
22 * 16.11.91 - More corrections (thanks to Mika Jalava). Most
23 * things seem to work now. Yeah, sure.
26 * 19.04.92 - Had to start over again from this old version, as a
27 * kernel bug ate my enhanced fsck in february.
29 * 28.02.93 - added support for different directory entry sizes..
31 * Sat Mar 6 18:59:42 1993, faith@cs.unc.edu: Output namelen with
32 * super-block information
34 * Sat Oct 9 11:17:11 1993, faith@cs.unc.edu: make exit status conform
35 * to that required by fsutil
37 * Mon Jan 3 11:06:52 1994 - Dr. Wettstein (greg%wind.uucp@plains.nodak.edu)
38 * Added support for file system valid flag. Also
39 * added program_version variable and output of
40 * program name and version number when program
43 * 30.10.94 - added support for v2 filesystem
44 * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de)
46 * 10.12.94 - added test to prevent checking of mounted fs adapted
47 * from Theodore Ts'o's (tytso@athena.mit.edu) e2fsck
48 * program. (Daniel Quinlan, quinlan@yggdrasil.com)
50 * 01.07.96 - Fixed the v2 fs stuff to use the right #defines and such
51 * for modern libcs (janl@math.uio.no, Nicolai Langfeldt)
53 * 02.07.96 - Added C bit fiddling routines from rmk@ecs.soton.ac.uk
54 * (Russell King). He made them for ARM. It would seem
55 * that the ARM is powerful enough to do this in C whereas
56 * i386 and m64k must use assembly to get it fast >:-)
57 * This should make minix fsck system-independent.
58 * (janl@math.uio.no, Nicolai Langfeldt)
60 * 04.11.96 - Added minor fixes from Andreas Schwab to avoid compiler
61 * warnings. Added mc68k bitops from
62 * Joerg Dorchain <dorchain@mpi-sb.mpg.de>.
64 * 06.11.96 - Added v2 code submitted by Joerg Dorchain, but written by
67 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
68 * - added Native Language Support
71 * I've had no time to add comments - hopefully the function names
72 * are comments enough. As with all file system checkers, this assumes
73 * the file system is quiescent - don't use it on a mounted device
74 * unless you can be sure nobody is writing to it (and remember that the
75 * kernel can write to it when it searches for files).
77 * Usage: fsck [-larvsm] device
78 * -l for a listing of all the filenames
79 * -a for automatic repairs (not implemented)
80 * -r for repairs (interactive) (not implemented)
81 * -v for verbose (tells how many files)
82 * -s for super-block info
83 * -m for minix-like "mode not cleared" warnings
84 * -f force filesystem check even if filesystem marked as valid
86 * The device may be a block device or a image of one, but this isn't
87 * enforced (but it's not much fun on a character device :-).
96 #define BLKGETSIZE _IO(0x12,96) /* return device size */
101 MINIX1_LINK_MAX = 250,
102 MINIX2_LINK_MAX = 65530,
103 MINIX_I_MAP_SLOTS = 8,
104 MINIX_Z_MAP_SLOTS = 64,
105 MINIX_V1 = 0x0001, /* original minix fs */
106 MINIX_V2 = 0x0002, /* minix V2 fs */
107 NAME_MAX = 255, /* # chars in a file name */
111 #if ENABLE_FEATURE_MINIX2
112 static smallint version2;
114 enum { version2 = 0 };
117 #define PROGRAM_VERSION "1.2 - 11/11/96"
118 static smallint repair, automatic, verbose, list, show, warn_mode, force;
119 static smallint changed; /* is filesystem modified? */
120 static smallint errors_uncorrected; /* flag if some error was not corrected */
122 static smallint termios_set;
123 static struct termios termios;
125 static char *device_name;
127 static int directory, regular, blockdev, chardev, links, symlinks, total;
130 static int dirsize = 16;
131 static int namelen = 14;
133 static char *inode_buffer;
136 char super_block_buffer[BLOCK_SIZE];
137 char add_zone_ind_blk[BLOCK_SIZE];
138 char add_zone_dind_blk[BLOCK_SIZE];
139 USE_FEATURE_MINIX2(char add_zone_tind_blk[BLOCK_SIZE];)
140 char check_file_blk[BLOCK_SIZE];
143 #define Inode1 (((struct minix1_inode *) inode_buffer)-1)
144 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
146 #define Super (*(struct minix_super_block *)(blockbuf->super_block_buffer))
148 #if ENABLE_FEATURE_MINIX2
149 # define ZONES ((unsigned)(version2 ? Super.s_zones : Super.s_nzones))
151 # define ZONES ((unsigned)(Super.s_nzones))
153 #define INODES ((unsigned)Super.s_ninodes)
154 #define IMAPS ((unsigned)Super.s_imap_blocks)
155 #define ZMAPS ((unsigned)Super.s_zmap_blocks)
156 #define FIRSTZONE ((unsigned)Super.s_firstdatazone)
157 #define ZONESIZE ((unsigned)Super.s_log_zone_size)
158 #define MAXSIZE ((unsigned)Super.s_max_size)
159 #define MAGIC (Super.s_magic)
161 /* gcc likes this more (code is smaller) than macro variant */
162 static ATTRIBUTE_ALWAYS_INLINE unsigned div_roundup(unsigned size, unsigned n)
164 return (size + n-1) / n;
167 #if ENABLE_FEATURE_MINIX2
168 #define INODE_BLOCKS div_roundup(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
169 : MINIX1_INODES_PER_BLOCK))
171 #define INODE_BLOCKS div_roundup(INODES, MINIX1_INODES_PER_BLOCK)
174 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
175 #define NORM_FIRSTZONE (2 + IMAPS + ZMAPS + INODE_BLOCKS)
177 static char *inode_map;
178 static char *zone_map;
180 static unsigned char *inode_count;
181 static unsigned char *zone_count;
183 /* Before you ask "where they come from?": */
184 /* setbit/clrbit are supplied by sys/param.h */
186 static int minix_bit(const char *a, unsigned i)
188 return (a[i >> 3] & (1<<(i & 7)));
191 static void minix_setbit(char *a, unsigned i)
196 static void minix_clrbit(char *a, unsigned i)
202 /* Note: do not assume 0/1, it is 0/nonzero */
203 #define zone_in_use(x) (minix_bit(zone_map,(x)-FIRSTZONE+1))
204 #define inode_in_use(x) (minix_bit(inode_map,(x)))
206 #define mark_inode(x) (minix_setbit(inode_map,(x)))
207 #define unmark_inode(x) (minix_clrbit(inode_map,(x)))
209 #define mark_zone(x) (minix_setbit(zone_map,(x)-FIRSTZONE+1))
210 #define unmark_zone(x) (minix_clrbit(zone_map,(x)-FIRSTZONE+1))
213 static void recursive_check(unsigned ino);
214 #if ENABLE_FEATURE_MINIX2
215 static void recursive_check2(unsigned ino);
218 static void die(const char *str) ATTRIBUTE_NORETURN;
219 static void die(const char *str)
222 tcsetattr(0, TCSANOW, &termios);
223 bb_error_msg_and_die("%s", str);
227 enum { MAX_DEPTH = 32 };
228 static int name_depth;
229 static char *current_name;
230 static char *name_component[MAX_DEPTH+1];
232 /* Wed Feb 9 15:17:06 MST 2000 */
233 /* dynamically allocate name_list (instead of making it static) */
234 static void alloc_current_name(void)
236 current_name = xmalloc(MAX_DEPTH * (BUFSIZ + 1));
237 current_name[0] = '/';
238 current_name[1] = '\0';
239 name_component[0] = ¤t_name[0];
242 #if ENABLE_FEATURE_CLEAN_UP
243 /* execute this atexit() to deallocate name_list[] */
244 /* piptigger was here */
245 static void free_current_name(void)
251 static void push_filename(const char *name)
255 // [0] [1] [2] <-name_component[i]
256 if (name_depth < MAX_DEPTH) {
258 char *p = name_component[name_depth];
260 len = sprintf(p, "%.*s", namelen, name);
261 name_component[name_depth + 1] = p + len;
266 static void pop_filename(void)
269 if (name_depth < MAX_DEPTH) {
270 *name_component[name_depth] = '\0';
272 current_name[0] = '/';
273 current_name[1] = '\0';
278 static int ask(const char *string, int def)
284 errors_uncorrected = 1;
290 errors_uncorrected = 1;
293 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
299 errors_uncorrected = 1;
306 } else if (c == 'N') {
309 } else if (c == ' ' || c == '\n')
316 errors_uncorrected = 1;
322 * Make certain that we aren't checking a filesystem that is on a
323 * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
324 * 1994 Theodore Ts'o. Also licensed under GPL.
326 static void check_mount(void)
333 f = setmntent(MOUNTED, "r");
336 while ((mnt = getmntent(f)) != NULL)
337 if (strcmp(device_name, mnt->mnt_fsname) == 0)
344 * If the root is mounted read-only, then /etc/mtab is
345 * probably not correct; so we won't issue a warning based on
348 fd = open(MOUNTED, O_RDWR);
349 if (fd < 0 && errno == EROFS)
353 printf("%s is mounted. ", device_name);
355 if (isatty(0) && isatty(1))
356 cont = ask("Do you really want to continue", 0);
358 printf("Check aborted\n");
364 * check_zone_nr checks to see that *nr is a valid zone nr. If it
365 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
366 * if an error was corrected, and returns the zone (0 for no zone
367 * or a bad zone-number).
369 static int check_zone_nr2(uint32_t *nr, smallint *corrected)
376 else if (*nr >= ZONES)
380 printf("Zone nr %s in file '%s'. ", msg, current_name);
381 if (ask("Remove block", 1)) {
388 static int check_zone_nr(uint16_t *nr, smallint *corrected)
391 int r = check_zone_nr2(&nr32, corrected);
392 *nr = (uint16_t)nr32;
397 * read-block reads block nr into the buffer at addr.
399 static void read_block(unsigned nr, char *addr)
402 memset(addr, 0, BLOCK_SIZE);
405 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) {
406 printf("%s: cannot seek to block in file '%s'\n",
407 bb_msg_read_error, current_name);
408 errors_uncorrected = 1;
409 memset(addr, 0, BLOCK_SIZE);
410 } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) {
411 printf("%s: bad block in file '%s'\n",
412 bb_msg_read_error, current_name);
413 errors_uncorrected = 1;
414 memset(addr, 0, BLOCK_SIZE);
419 * write_block writes block nr to disk.
421 static void write_block(unsigned nr, char *addr)
425 if (nr < FIRSTZONE || nr >= ZONES) {
426 printf("Internal error: trying to write bad block\n"
427 "Write request ignored\n");
428 errors_uncorrected = 1;
431 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET))
432 die("seek failed in write_block");
433 if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) {
434 printf("%s: bad block in file '%s'\n",
435 bb_msg_write_error, current_name);
436 errors_uncorrected = 1;
441 * map_block calculates the absolute block nr of a block in a file.
442 * It sets 'changed' if the inode has needed changing, and re-writes
443 * any indirect blocks with errors.
445 static int map_block(struct minix1_inode *inode, unsigned blknr)
447 uint16_t ind[BLOCK_SIZE >> 1];
448 uint16_t dind[BLOCK_SIZE >> 1];
453 return check_zone_nr(inode->i_zone + blknr, &changed);
456 block = check_zone_nr(inode->i_zone + 7, &changed);
457 read_block(block, (char *) ind);
459 result = check_zone_nr(blknr + ind, &blk_chg);
461 write_block(block, (char *) ind);
465 block = check_zone_nr(inode->i_zone + 8, &changed);
466 read_block(block, (char *) dind);
468 result = check_zone_nr(dind + (blknr / 512), &blk_chg);
470 write_block(block, (char *) dind);
472 read_block(block, (char *) ind);
474 result = check_zone_nr(ind + (blknr % 512), &blk_chg);
476 write_block(block, (char *) ind);
480 #if ENABLE_FEATURE_MINIX2
481 static int map_block2(struct minix2_inode *inode, unsigned blknr)
483 uint32_t ind[BLOCK_SIZE >> 2];
484 uint32_t dind[BLOCK_SIZE >> 2];
485 uint32_t tind[BLOCK_SIZE >> 2];
490 return check_zone_nr2(inode->i_zone + blknr, &changed);
493 block = check_zone_nr2(inode->i_zone + 7, &changed);
494 read_block(block, (char *) ind);
496 result = check_zone_nr2(blknr + ind, &blk_chg);
498 write_block(block, (char *) ind);
502 if (blknr >= 256 * 256) {
503 block = check_zone_nr2(inode->i_zone + 8, &changed);
504 read_block(block, (char *) dind);
506 result = check_zone_nr2(dind + blknr / 256, &blk_chg);
508 write_block(block, (char *) dind);
510 read_block(block, (char *) ind);
512 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
514 write_block(block, (char *) ind);
518 block = check_zone_nr2(inode->i_zone + 9, &changed);
519 read_block(block, (char *) tind);
521 result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
523 write_block(block, (char *) tind);
525 read_block(block, (char *) dind);
527 result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
529 write_block(block, (char *) dind);
531 read_block(block, (char *) ind);
533 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
535 write_block(block, (char *) ind);
540 static void write_super_block(void)
543 * Set the state of the filesystem based on whether or not there
544 * are uncorrected errors. The filesystem valid flag is
545 * unconditionally set if we get this far.
547 Super.s_state |= MINIX_VALID_FS | MINIX_ERROR_FS;
548 if (!errors_uncorrected)
549 Super.s_state &= ~MINIX_ERROR_FS;
551 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
552 die("seek failed in write_super_block");
553 if (BLOCK_SIZE != write(IN, blockbuf->super_block_buffer, BLOCK_SIZE))
554 die("cannot write super-block");
557 static void write_tables(void)
561 if (IMAPS * BLOCK_SIZE != write(IN, inode_map, IMAPS * BLOCK_SIZE))
562 die("cannot write inode map");
563 if (ZMAPS * BLOCK_SIZE != write(IN, zone_map, ZMAPS * BLOCK_SIZE))
564 die("cannot write zone map");
565 if (INODE_BUFFER_SIZE != write(IN, inode_buffer, INODE_BUFFER_SIZE))
566 die("cannot write inodes");
569 static void get_dirsize(void)
572 char blk[BLOCK_SIZE];
575 #if ENABLE_FEATURE_MINIX2
577 block = Inode2[MINIX_ROOT_INO].i_zone[0];
580 block = Inode1[MINIX_ROOT_INO].i_zone[0];
581 read_block(block, blk);
582 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
583 if (strcmp(blk + size + 2, "..") == 0) {
592 static void read_superblock(void)
594 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
596 if (BLOCK_SIZE != read(IN, blockbuf->super_block_buffer, BLOCK_SIZE))
597 die("cannot read super block");
598 /* already initialized to:
603 if (MAGIC == MINIX1_SUPER_MAGIC) {
604 } else if (MAGIC == MINIX1_SUPER_MAGIC2) {
607 #if ENABLE_FEATURE_MINIX2
608 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
610 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
616 die("bad magic number in super-block");
617 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
618 die("only 1k blocks/zones supported");
619 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
620 die("bad s_imap_blocks field in super-block");
621 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
622 die("bad s_zmap_blocks field in super-block");
625 static void read_tables(void)
627 inode_map = xzalloc(IMAPS * BLOCK_SIZE);
628 zone_map = xzalloc(ZMAPS * BLOCK_SIZE);
629 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
630 inode_count = xmalloc(INODES + 1);
631 zone_count = xmalloc(ZONES);
632 if (IMAPS * BLOCK_SIZE != read(IN, inode_map, IMAPS * BLOCK_SIZE))
633 die("cannot read inode map");
634 if (ZMAPS * BLOCK_SIZE != read(IN, zone_map, ZMAPS * BLOCK_SIZE))
635 die("cannot read zone map");
636 if (INODE_BUFFER_SIZE != read(IN, inode_buffer, INODE_BUFFER_SIZE))
637 die("cannot read inodes");
638 if (NORM_FIRSTZONE != FIRSTZONE) {
639 printf("warning: firstzone!=norm_firstzone\n");
640 errors_uncorrected = 1;
646 "Firstdatazone=%u (%u)\n"
649 "Filesystem state=%u\n"
653 FIRSTZONE, NORM_FIRSTZONE,
654 BLOCK_SIZE << ZONESIZE,
661 static struct minix1_inode *get_inode(unsigned nr)
663 struct minix1_inode *inode;
665 if (!nr || nr > INODES)
669 if (!inode_count[nr]) {
670 if (!inode_in_use(nr)) {
671 printf("Inode %d is marked as 'unused', but it is used "
672 "for file '%s'\n", nr, current_name);
674 if (ask("Mark as 'in use'", 1))
677 errors_uncorrected = 1;
680 if (S_ISDIR(inode->i_mode))
682 else if (S_ISREG(inode->i_mode))
684 else if (S_ISCHR(inode->i_mode))
686 else if (S_ISBLK(inode->i_mode))
688 else if (S_ISLNK(inode->i_mode))
690 else if (S_ISSOCK(inode->i_mode));
691 else if (S_ISFIFO(inode->i_mode));
693 printf("%s has mode %05o\n", current_name, inode->i_mode);
698 if (!++inode_count[nr]) {
699 printf("Warning: inode count too big\n");
701 errors_uncorrected = 1;
706 #if ENABLE_FEATURE_MINIX2
707 static struct minix2_inode *get_inode2(unsigned nr)
709 struct minix2_inode *inode;
711 if (!nr || nr > INODES)
715 if (!inode_count[nr]) {
716 if (!inode_in_use(nr)) {
717 printf("Inode %d is marked as 'unused', but it is used "
718 "for file '%s'\n", nr, current_name);
720 if (ask("Mark as 'in use'", 1))
723 errors_uncorrected = 1;
726 if (S_ISDIR(inode->i_mode))
728 else if (S_ISREG(inode->i_mode))
730 else if (S_ISCHR(inode->i_mode))
732 else if (S_ISBLK(inode->i_mode))
734 else if (S_ISLNK(inode->i_mode))
736 else if (S_ISSOCK(inode->i_mode));
737 else if (S_ISFIFO(inode->i_mode));
739 printf("%s has mode %05o\n", current_name, inode->i_mode);
743 if (!++inode_count[nr]) {
744 printf("Warning: inode count too big\n");
746 errors_uncorrected = 1;
752 static void check_root(void)
754 struct minix1_inode *inode = Inode1 + MINIX_ROOT_INO;
756 if (!inode || !S_ISDIR(inode->i_mode))
757 die("root inode isn't a directory");
760 #if ENABLE_FEATURE_MINIX2
761 static void check_root2(void)
763 struct minix2_inode *inode = Inode2 + MINIX_ROOT_INO;
765 if (!inode || !S_ISDIR(inode->i_mode))
766 die("root inode isn't a directory");
769 void check_root2(void);
772 static int add_zone(uint16_t *znr, smallint *corrected)
778 block = check_zone_nr(znr, corrected);
781 if (zone_count[block]) {
782 printf("Already used block is reused in file '%s'. ",
784 if (ask("Clear", 1)) {
791 if (!zone_in_use(block)) {
792 printf("Block %d in file '%s' is marked as 'unused'. ",
793 block, current_name);
794 if (ask("Correct", 1))
797 if (!++zone_count[block])
802 #if ENABLE_FEATURE_MINIX2
803 static int add_zone2(uint32_t *znr, smallint *corrected)
809 block = check_zone_nr2(znr, corrected);
812 if (zone_count[block]) {
813 printf("Already used block is reused in file '%s'. ",
815 if (ask("Clear", 1)) {
822 if (!zone_in_use(block)) {
823 printf("Block %d in file '%s' is marked as 'unused'. ",
824 block, current_name);
825 if (ask("Correct", 1))
828 if (!++zone_count[block])
834 static void add_zone_ind(uint16_t *znr, smallint *corrected)
836 #define blk (blockbuf->add_zone_ind_blk)
839 smallint chg_blk = 0;
841 block = add_zone(znr, corrected);
844 read_block(block, blk);
845 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
846 add_zone(i + (uint16_t *) blk, &chg_blk);
848 write_block(block, blk);
852 #if ENABLE_FEATURE_MINIX2
853 static void add_zone_ind2(uint32_t *znr, smallint *corrected)
855 #define blk (blockbuf->add_zone_ind_blk)
858 smallint chg_blk = 0;
860 block = add_zone2(znr, corrected);
863 read_block(block, blk);
864 for (i = 0; i < BLOCK_SIZE >> 2; i++)
865 add_zone2(i + (uint32_t *) blk, &chg_blk);
867 write_block(block, blk);
872 static void add_zone_dind(uint16_t *znr, smallint *corrected)
874 #define blk (blockbuf->add_zone_dind_blk)
877 smallint chg_blk = 0;
879 block = add_zone(znr, corrected);
882 read_block(block, blk);
883 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
884 add_zone_ind(i + (uint16_t *) blk, &chg_blk);
886 write_block(block, blk);
890 #if ENABLE_FEATURE_MINIX2
891 static void add_zone_dind2(uint32_t *znr, smallint *corrected)
893 #define blk (blockbuf->add_zone_dind_blk)
896 smallint chg_blk = 0;
898 block = add_zone2(znr, corrected);
901 read_block(block, blk);
902 for (i = 0; i < BLOCK_SIZE >> 2; i++)
903 add_zone_ind2(i + (uint32_t *) blk, &chg_blk);
905 write_block(block, blk);
909 static void add_zone_tind2(uint32_t *znr, smallint *corrected)
911 #define blk (blockbuf->add_zone_tind_blk)
914 smallint chg_blk = 0;
916 block = add_zone2(znr, corrected);
919 read_block(block, blk);
920 for (i = 0; i < BLOCK_SIZE >> 2; i++)
921 add_zone_dind2(i + (uint32_t *) blk, &chg_blk);
923 write_block(block, blk);
928 static void check_zones(unsigned i)
930 struct minix1_inode *inode;
932 if (!i || i > INODES)
934 if (inode_count[i] > 1) /* have we counted this file already? */
937 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
938 !S_ISLNK(inode->i_mode)) return;
939 for (i = 0; i < 7; i++)
940 add_zone(i + inode->i_zone, &changed);
941 add_zone_ind(7 + inode->i_zone, &changed);
942 add_zone_dind(8 + inode->i_zone, &changed);
945 #if ENABLE_FEATURE_MINIX2
946 static void check_zones2(unsigned i)
948 struct minix2_inode *inode;
950 if (!i || i > INODES)
952 if (inode_count[i] > 1) /* have we counted this file already? */
955 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
956 && !S_ISLNK(inode->i_mode))
958 for (i = 0; i < 7; i++)
959 add_zone2(i + inode->i_zone, &changed);
960 add_zone_ind2(7 + inode->i_zone, &changed);
961 add_zone_dind2(8 + inode->i_zone, &changed);
962 add_zone_tind2(9 + inode->i_zone, &changed);
966 static void check_file(struct minix1_inode *dir, unsigned offset)
968 #define blk (blockbuf->check_file_blk)
969 struct minix1_inode *inode;
974 block = map_block(dir, offset / BLOCK_SIZE);
975 read_block(block, blk);
976 name = blk + (offset % BLOCK_SIZE) + 2;
977 ino = *(uint16_t *) (name - 2);
979 printf("%s contains a bad inode number for file '%.*s'. ",
980 current_name, namelen, name);
981 if (ask("Remove", 1)) {
982 *(uint16_t *) (name - 2) = 0;
983 write_block(block, blk);
988 inode = get_inode(ino);
991 if (inode && LONE_CHAR(name, '.'))
993 printf("%s: bad directory: '.' isn't first\n", current_name);
994 errors_uncorrected = 1;
996 if (offset == dirsize) {
997 if (inode && strcmp("..", name) == 0)
999 printf("%s: bad directory: '..' isn't second\n", current_name);
1000 errors_uncorrected = 1;
1004 push_filename(name);
1007 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1008 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
1011 if (inode && S_ISDIR(inode->i_mode))
1012 recursive_check(ino);
1017 #if ENABLE_FEATURE_MINIX2
1018 static void check_file2(struct minix2_inode *dir, unsigned offset)
1020 #define blk (blockbuf->check_file_blk)
1021 struct minix2_inode *inode;
1026 block = map_block2(dir, offset / BLOCK_SIZE);
1027 read_block(block, blk);
1028 name = blk + (offset % BLOCK_SIZE) + 2;
1029 ino = *(uint16_t *) (name - 2);
1031 printf("%s contains a bad inode number for file '%.*s'. ",
1032 current_name, namelen, name);
1033 if (ask("Remove", 1)) {
1034 *(uint16_t *) (name - 2) = 0;
1035 write_block(block, blk);
1039 push_filename(name);
1040 inode = get_inode2(ino);
1043 if (inode && LONE_CHAR(name, '.'))
1045 printf("%s: bad directory: '.' isn't first\n", current_name);
1046 errors_uncorrected = 1;
1048 if (offset == dirsize) {
1049 if (inode && strcmp("..", name) == 0)
1051 printf("%s: bad directory: '..' isn't second\n", current_name);
1052 errors_uncorrected = 1;
1056 push_filename(name);
1059 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1060 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
1063 if (inode && S_ISDIR(inode->i_mode))
1064 recursive_check2(ino);
1070 static void recursive_check(unsigned ino)
1072 struct minix1_inode *dir;
1076 if (!S_ISDIR(dir->i_mode))
1077 die("internal error");
1078 if (dir->i_size < 2 * dirsize) {
1079 printf("%s: bad directory: size<32", current_name);
1080 errors_uncorrected = 1;
1082 for (offset = 0; offset < dir->i_size; offset += dirsize)
1083 check_file(dir, offset);
1086 #if ENABLE_FEATURE_MINIX2
1087 static void recursive_check2(unsigned ino)
1089 struct minix2_inode *dir;
1093 if (!S_ISDIR(dir->i_mode))
1094 die("internal error");
1095 if (dir->i_size < 2 * dirsize) {
1096 printf("%s: bad directory: size<32", current_name);
1097 errors_uncorrected = 1;
1099 for (offset = 0; offset < dir->i_size; offset += dirsize)
1100 check_file2(dir, offset);
1104 static int bad_zone(int i)
1106 char buffer[BLOCK_SIZE];
1108 if (BLOCK_SIZE * i != lseek(IN, BLOCK_SIZE * i, SEEK_SET))
1109 die("seek failed in bad_zone");
1110 return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE));
1113 static void check_counts(void)
1117 for (i = 1; i <= INODES; i++) {
1118 if (warn_mode && Inode1[i].i_mode && !inode_in_use(i)) {
1119 printf("Inode %d has non-zero mode. ", i);
1120 if (ask("Clear", 1)) {
1121 Inode1[i].i_mode = 0;
1125 if (!inode_count[i]) {
1126 if (!inode_in_use(i))
1128 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
1129 if (ask("Clear", 1))
1133 if (!inode_in_use(i)) {
1134 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
1138 if (Inode1[i].i_nlinks != inode_count[i]) {
1139 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
1140 i, Inode1[i].i_mode, Inode1[i].i_nlinks,
1142 if (ask("Set i_nlinks to count", 1)) {
1143 Inode1[i].i_nlinks = inode_count[i];
1148 for (i = FIRSTZONE; i < ZONES; i++) {
1149 if ((zone_in_use(i) != 0) == zone_count[i])
1151 if (!zone_count[i]) {
1154 printf("Zone %d is marked 'in use', but no file uses it. ", i);
1155 if (ask("Unmark", 1))
1159 printf("Zone %d: %sin use, counted=%d\n",
1160 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1164 #if ENABLE_FEATURE_MINIX2
1165 static void check_counts2(void)
1169 for (i = 1; i <= INODES; i++) {
1170 if (warn_mode && Inode2[i].i_mode && !inode_in_use(i)) {
1171 printf("Inode %d has non-zero mode. ", i);
1172 if (ask("Clear", 1)) {
1173 Inode2[i].i_mode = 0;
1177 if (!inode_count[i]) {
1178 if (!inode_in_use(i))
1180 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
1181 if (ask("Clear", 1))
1185 if (!inode_in_use(i)) {
1186 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
1190 if (Inode2[i].i_nlinks != inode_count[i]) {
1191 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
1192 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1194 if (ask("Set i_nlinks to count", 1)) {
1195 Inode2[i].i_nlinks = inode_count[i];
1200 for (i = FIRSTZONE; i < ZONES; i++) {
1201 if ((zone_in_use(i) != 0) == zone_count[i])
1203 if (!zone_count[i]) {
1206 printf("Zone %d is marked 'in use', but no file uses it. ", i);
1207 if (ask("Unmark", 1))
1211 printf("Zone %d: %sin use, counted=%d\n",
1212 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1217 static void check(void)
1219 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1220 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1221 check_zones(MINIX_ROOT_INO);
1222 recursive_check(MINIX_ROOT_INO);
1226 #if ENABLE_FEATURE_MINIX2
1227 static void check2(void)
1229 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1230 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1231 check_zones2(MINIX_ROOT_INO);
1232 recursive_check2(MINIX_ROOT_INO);
1239 int fsck_minix_main(int argc, char **argv);
1240 int fsck_minix_main(int argc, char **argv)
1245 xfunc_error_retval = 8;
1246 blockbuf = xzalloc(sizeof(*blockbuf));
1248 alloc_current_name();
1249 #if ENABLE_FEATURE_CLEAN_UP
1250 /* Don't bother to free memory. Exit does
1251 * that automagically, so we can save a few bytes */
1252 atexit(free_current_name);
1255 if (INODE_SIZE1 * MINIX1_INODES_PER_BLOCK != BLOCK_SIZE)
1256 die("bad inode size");
1257 #if ENABLE_FEATURE_MINIX2
1258 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
1259 die("bad v2 inode size");
1261 while (--argc != 0) {
1263 if (argv[0][0] != '-') {
1266 device_name = argv[0];
1268 while (*++argv[0]) {
1269 switch (argv[0][0]) {
1302 check_mount(); /* trying to check a mounted filesystem? */
1303 if (repair && !automatic) {
1304 if (!isatty(0) || !isatty(1))
1305 die("need terminal for interactive repairs");
1307 IN = xopen(device_name, repair ? O_RDWR : O_RDONLY);
1309 /*sync(); paranoia? */
1313 * Determine whether or not we should continue with the checking.
1314 * This is based on the status of the filesystem valid and error
1315 * flags and whether or not the -f switch was specified on the
1318 printf("%s, "PROGRAM_VERSION"\n", applet_name);
1320 if (!(Super.s_state & MINIX_ERROR_FS)
1321 && (Super.s_state & MINIX_VALID_FS) && !force
1324 printf("%s is clean, check is skipped\n", device_name);
1327 printf("Forcing filesystem check on %s\n", device_name);
1329 printf("Filesystem on %s is dirty, needs checking\n",
1334 if (repair && !automatic) {
1335 tcgetattr(0, &termios);
1337 tmp.c_lflag &= ~(ICANON | ECHO);
1338 tcsetattr(0, TCSANOW, &tmp);
1353 for (i = 1, free_cnt = 0; i <= INODES; i++)
1354 if (!inode_in_use(i))
1356 printf("\n%6u inodes used (%u%%)\n", (INODES - free_cnt),
1357 100 * (INODES - free_cnt) / INODES);
1358 for (i = FIRSTZONE, free_cnt = 0; i < ZONES; i++)
1359 if (!zone_in_use(i))
1361 printf("%6u zones used (%u%%)\n\n"
1362 "%6u regular files\n"
1364 "%6u character device files\n"
1365 "%6u block device files\n"
1367 "%6u symbolic links\n"
1370 (ZONES - free_cnt), 100 * (ZONES - free_cnt) / ZONES,
1371 regular, directory, chardev, blockdev,
1372 links - 2 * directory + 1, symlinks,
1373 total - 2 * directory + 1);
1377 printf("FILE SYSTEM HAS BEEN CHANGED\n");
1380 write_super_block();
1382 if (repair && !automatic)
1383 tcsetattr(0, TCSANOW, &termios);
1387 if (errors_uncorrected)