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;
135 static char super_block_buffer[BLOCK_SIZE];
137 #define Inode1 (((struct minix1_inode *) inode_buffer)-1)
138 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
140 #define Super (*(struct minix_super_block *)super_block_buffer)
142 #if ENABLE_FEATURE_MINIX2
143 # define ZONES ((unsigned)(version2 ? Super.s_zones : Super.s_nzones))
145 # define ZONES ((unsigned)(Super.s_nzones))
147 #define INODES ((unsigned)Super.s_ninodes)
148 #define IMAPS ((unsigned)Super.s_imap_blocks)
149 #define ZMAPS ((unsigned)Super.s_zmap_blocks)
150 #define FIRSTZONE ((unsigned)Super.s_firstdatazone)
151 #define ZONESIZE ((unsigned)Super.s_log_zone_size)
152 #define MAXSIZE ((unsigned)Super.s_max_size)
153 #define MAGIC (Super.s_magic)
155 /* gcc likes this more (code is smaller) than macro variant */
156 static ATTRIBUTE_ALWAYS_INLINE unsigned div_roundup(unsigned size, unsigned n)
158 return (size + n-1) / n;
161 #if ENABLE_FEATURE_MINIX2
162 #define INODE_BLOCKS div_roundup(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
163 : MINIX1_INODES_PER_BLOCK))
165 #define INODE_BLOCKS div_roundup(INODES, MINIX1_INODES_PER_BLOCK)
168 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
169 #define NORM_FIRSTZONE (2 + IMAPS + ZMAPS + INODE_BLOCKS)
171 static char *inode_map;
172 static char *zone_map;
174 static unsigned char *inode_count;
175 static unsigned char *zone_count;
177 static int bit(char *a, unsigned i)
179 return (a[i >> 3] & (1<<(i & 7))) != 0;
182 #define inode_in_use(x) (bit(inode_map,(x)))
183 #define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
185 #define mark_inode(x) (setbit(inode_map,(x)),changed=1)
186 #define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
188 #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1)
189 #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1)
192 static void recursive_check(unsigned ino);
193 #if ENABLE_FEATURE_MINIX2
194 static void recursive_check2(unsigned ino);
197 static void leave(int) ATTRIBUTE_NORETURN;
198 static void leave(int status)
201 tcsetattr(0, TCSANOW, &termios);
205 static void die(const char *str)
207 bb_error_msg("%s", str);
212 enum { MAX_DEPTH = 32 };
213 static int name_depth;
214 static char *current_name;
215 static char *name_component[MAX_DEPTH+1];
217 /* Wed Feb 9 15:17:06 MST 2000 */
218 /* dynamically allocate name_list (instead of making it static) */
219 static void alloc_current_name(void)
221 current_name = xmalloc(MAX_DEPTH * (BUFSIZ + 1));
222 current_name[0] = '/';
223 current_name[1] = '\0';
224 name_component[0] = ¤t_name[0];
227 #if ENABLE_FEATURE_CLEAN_UP
228 /* execute this atexit() to deallocate name_list[] */
229 /* piptigger was here */
230 static void free_current_name(void)
236 static void push_filename(const char *name)
240 // [0] [1] [2] <-name_component[i]
241 if (name_depth < MAX_DEPTH) {
243 char *p = name_component[name_depth];
245 len = sprintf(p, "%.*s", namelen, name);
246 name_component[name_depth + 1] = p + len;
251 static void pop_filename(void)
254 if (name_depth < MAX_DEPTH) {
255 *name_component[name_depth] = '\0';
257 current_name[0] = '/';
258 current_name[1] = '\0';
263 static int ask(const char *string, int def)
269 errors_uncorrected = 1;
275 errors_uncorrected = 1;
278 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
284 errors_uncorrected = 1;
291 } else if (c == 'N') {
294 } else if (c == ' ' || c == '\n')
301 errors_uncorrected = 1;
307 * Make certain that we aren't checking a filesystem that is on a
308 * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
309 * 1994 Theodore Ts'o. Also licensed under GPL.
311 static void check_mount(void)
318 f = setmntent(MOUNTED, "r");
321 while ((mnt = getmntent(f)) != NULL)
322 if (strcmp(device_name, mnt->mnt_fsname) == 0)
329 * If the root is mounted read-only, then /etc/mtab is
330 * probably not correct; so we won't issue a warning based on
333 fd = open(MOUNTED, O_RDWR);
334 if (fd < 0 && errno == EROFS)
338 printf("%s is mounted. ", device_name);
340 if (isatty(0) && isatty(1))
341 cont = ask("Do you really want to continue", 0);
343 printf("Check aborted\n");
349 * check_zone_nr checks to see that *nr is a valid zone nr. If it
350 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
351 * if an error was corrected, and returns the zone (0 for no zone
352 * or a bad zone-number).
354 static int check_zone_nr2(uint32_t *nr, smallint *corrected)
361 else if (*nr >= ZONES)
365 printf("Zone nr %s in file '%s'. ", msg, current_name);
366 if (ask("Remove block", 1)) {
373 static int check_zone_nr(uint16_t *nr, smallint *corrected)
376 int r = check_zone_nr2(&nr32, corrected);
377 *nr = (uint16_t)nr32;
382 * read-block reads block nr into the buffer at addr.
384 static void read_block(unsigned nr, char *addr)
387 memset(addr, 0, BLOCK_SIZE);
390 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) {
391 printf("%s: cannot seek to block in file '%s'\n",
392 bb_msg_read_error, current_name);
393 errors_uncorrected = 1;
394 memset(addr, 0, BLOCK_SIZE);
395 } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) {
396 printf("%s: bad block in file '%s'\n",
397 bb_msg_read_error, current_name);
398 errors_uncorrected = 1;
399 memset(addr, 0, BLOCK_SIZE);
404 * write_block writes block nr to disk.
406 static void write_block(unsigned nr, char *addr)
410 if (nr < FIRSTZONE || nr >= ZONES) {
411 printf("Internal error: trying to write bad block\n"
412 "Write request ignored\n");
413 errors_uncorrected = 1;
416 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET))
417 die("seek failed in write_block");
418 if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) {
419 printf("%s: bad block in file '%s'\n",
420 bb_msg_write_error, current_name);
421 errors_uncorrected = 1;
426 * map_block calculates the absolute block nr of a block in a file.
427 * It sets 'changed' if the inode has needed changing, and re-writes
428 * any indirect blocks with errors.
430 static int map_block(struct minix1_inode *inode, unsigned blknr)
432 uint16_t ind[BLOCK_SIZE >> 1];
433 uint16_t dind[BLOCK_SIZE >> 1];
438 return check_zone_nr(inode->i_zone + blknr, &changed);
441 block = check_zone_nr(inode->i_zone + 7, &changed);
442 read_block(block, (char *) ind);
444 result = check_zone_nr(blknr + ind, &blk_chg);
446 write_block(block, (char *) ind);
450 block = check_zone_nr(inode->i_zone + 8, &changed);
451 read_block(block, (char *) dind);
453 result = check_zone_nr(dind + (blknr / 512), &blk_chg);
455 write_block(block, (char *) dind);
457 read_block(block, (char *) ind);
459 result = check_zone_nr(ind + (blknr % 512), &blk_chg);
461 write_block(block, (char *) ind);
465 #if ENABLE_FEATURE_MINIX2
466 static int map_block2(struct minix2_inode *inode, unsigned blknr)
468 uint32_t ind[BLOCK_SIZE >> 2];
469 uint32_t dind[BLOCK_SIZE >> 2];
470 uint32_t tind[BLOCK_SIZE >> 2];
475 return check_zone_nr2(inode->i_zone + blknr, &changed);
478 block = check_zone_nr2(inode->i_zone + 7, &changed);
479 read_block(block, (char *) ind);
481 result = check_zone_nr2(blknr + ind, &blk_chg);
483 write_block(block, (char *) ind);
487 if (blknr >= 256 * 256) {
488 block = check_zone_nr2(inode->i_zone + 8, &changed);
489 read_block(block, (char *) dind);
491 result = check_zone_nr2(dind + blknr / 256, &blk_chg);
493 write_block(block, (char *) dind);
495 read_block(block, (char *) ind);
497 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
499 write_block(block, (char *) ind);
503 block = check_zone_nr2(inode->i_zone + 9, &changed);
504 read_block(block, (char *) tind);
506 result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
508 write_block(block, (char *) tind);
510 read_block(block, (char *) dind);
512 result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
514 write_block(block, (char *) dind);
516 read_block(block, (char *) ind);
518 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
520 write_block(block, (char *) ind);
525 static void write_super_block(void)
528 * Set the state of the filesystem based on whether or not there
529 * are uncorrected errors. The filesystem valid flag is
530 * unconditionally set if we get this far.
532 Super.s_state |= MINIX_VALID_FS | MINIX_ERROR_FS;
533 if (!errors_uncorrected)
534 Super.s_state &= ~MINIX_ERROR_FS;
536 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
537 die("seek failed in write_super_block");
538 if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE))
539 die("cannot write super-block");
542 static void write_tables(void)
546 if (IMAPS * BLOCK_SIZE != write(IN, inode_map, IMAPS * BLOCK_SIZE))
547 die("cannot write inode map");
548 if (ZMAPS * BLOCK_SIZE != write(IN, zone_map, ZMAPS * BLOCK_SIZE))
549 die("cannot write zone map");
550 if (INODE_BUFFER_SIZE != write(IN, inode_buffer, INODE_BUFFER_SIZE))
551 die("cannot write inodes");
554 static void get_dirsize(void)
557 char blk[BLOCK_SIZE];
560 #if ENABLE_FEATURE_MINIX2
562 block = Inode2[MINIX_ROOT_INO].i_zone[0];
565 block = Inode1[MINIX_ROOT_INO].i_zone[0];
566 read_block(block, blk);
567 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
568 if (strcmp(blk + size + 2, "..") == 0) {
577 static void read_superblock(void)
579 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
581 if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
582 die("cannot read super block");
583 /* already initialized to:
588 if (MAGIC == MINIX1_SUPER_MAGIC) {
589 } else if (MAGIC == MINIX1_SUPER_MAGIC2) {
592 #if ENABLE_FEATURE_MINIX2
593 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
595 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
601 die("bad magic number in super-block");
602 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
603 die("only 1k blocks/zones supported");
604 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
605 die("bad s_imap_blocks field in super-block");
606 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
607 die("bad s_zmap_blocks field in super-block");
610 static void read_tables(void)
612 inode_map = xzalloc(IMAPS * BLOCK_SIZE);
613 zone_map = xzalloc(ZMAPS * BLOCK_SIZE);
614 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
615 inode_count = xmalloc(INODES + 1);
616 zone_count = xmalloc(ZONES);
617 if (IMAPS * BLOCK_SIZE != read(IN, inode_map, IMAPS * BLOCK_SIZE))
618 die("cannot read inode map");
619 if (ZMAPS * BLOCK_SIZE != read(IN, zone_map, ZMAPS * BLOCK_SIZE))
620 die("cannot read zone map");
621 if (INODE_BUFFER_SIZE != read(IN, inode_buffer, INODE_BUFFER_SIZE))
622 die("cannot read inodes");
623 if (NORM_FIRSTZONE != FIRSTZONE) {
624 printf("warning: firstzone!=norm_firstzone\n");
625 errors_uncorrected = 1;
631 "Firstdatazone=%u (%u)\n"
634 "Filesystem state=%u\n"
638 FIRSTZONE, NORM_FIRSTZONE,
639 BLOCK_SIZE << ZONESIZE,
646 static struct minix1_inode *get_inode(unsigned nr)
648 struct minix1_inode *inode;
650 if (!nr || nr > INODES)
654 if (!inode_count[nr]) {
655 if (!inode_in_use(nr)) {
656 printf("Inode %d is marked as 'unused', but it is used "
657 "for file '%s'\n", nr, current_name);
659 if (ask("Mark as 'in use'", 1))
662 errors_uncorrected = 1;
665 if (S_ISDIR(inode->i_mode))
667 else if (S_ISREG(inode->i_mode))
669 else if (S_ISCHR(inode->i_mode))
671 else if (S_ISBLK(inode->i_mode))
673 else if (S_ISLNK(inode->i_mode))
675 else if (S_ISSOCK(inode->i_mode));
676 else if (S_ISFIFO(inode->i_mode));
678 printf("%s has mode %05o\n", current_name, inode->i_mode);
683 if (!++inode_count[nr]) {
684 printf("Warning: inode count too big\n");
686 errors_uncorrected = 1;
691 #if ENABLE_FEATURE_MINIX2
692 static struct minix2_inode *get_inode2(unsigned nr)
694 struct minix2_inode *inode;
696 if (!nr || nr > INODES)
700 if (!inode_count[nr]) {
701 if (!inode_in_use(nr)) {
702 printf("Inode %d is marked as 'unused', but it is used "
703 "for file '%s'\n", nr, current_name);
705 if (ask("Mark as 'in use'", 1))
708 errors_uncorrected = 1;
711 if (S_ISDIR(inode->i_mode))
713 else if (S_ISREG(inode->i_mode))
715 else if (S_ISCHR(inode->i_mode))
717 else if (S_ISBLK(inode->i_mode))
719 else if (S_ISLNK(inode->i_mode))
721 else if (S_ISSOCK(inode->i_mode));
722 else if (S_ISFIFO(inode->i_mode));
724 printf("%s has mode %05o\n", current_name, inode->i_mode);
728 if (!++inode_count[nr]) {
729 printf("Warning: inode count too big\n");
731 errors_uncorrected = 1;
737 static void check_root(void)
739 struct minix1_inode *inode = Inode1 + MINIX_ROOT_INO;
741 if (!inode || !S_ISDIR(inode->i_mode))
742 die("root inode isn't a directory");
745 #if ENABLE_FEATURE_MINIX2
746 static void check_root2(void)
748 struct minix2_inode *inode = Inode2 + MINIX_ROOT_INO;
750 if (!inode || !S_ISDIR(inode->i_mode))
751 die("root inode isn't a directory");
754 void check_root2(void);
757 static int add_zone(uint16_t *znr, smallint *corrected)
763 block = check_zone_nr(znr, corrected);
766 if (zone_count[block]) {
767 printf("Already used block is reused in file '%s'. ",
769 if (ask("Clear", 1)) {
776 if (!zone_in_use(block)) {
777 printf("Block %d in file '%s' is marked as 'unused'. ",
778 block, current_name);
779 if (ask("Correct", 1))
782 if (!++zone_count[block])
787 #if ENABLE_FEATURE_MINIX2
788 static int add_zone2(uint32_t *znr, smallint *corrected)
794 block = check_zone_nr2(znr, corrected);
797 if (zone_count[block]) {
798 printf("Already used block is reused in file '%s'. ",
800 if (ask("Clear", 1)) {
807 if (!zone_in_use(block)) {
808 printf("Block %d in file '%s' is marked as 'unused'. ",
809 block, current_name);
810 if (ask("Correct", 1))
813 if (!++zone_count[block])
819 static void add_zone_ind(uint16_t *znr, smallint *corrected)
821 static char blk[BLOCK_SIZE];
824 smallint chg_blk = 0;
826 block = add_zone(znr, corrected);
829 read_block(block, blk);
830 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
831 add_zone(i + (uint16_t *) blk, &chg_blk);
833 write_block(block, blk);
836 #if ENABLE_FEATURE_MINIX2
837 static void add_zone_ind2(uint32_t *znr, smallint *corrected)
839 static char blk[BLOCK_SIZE];
842 smallint chg_blk = 0;
844 block = add_zone2(znr, corrected);
847 read_block(block, blk);
848 for (i = 0; i < BLOCK_SIZE >> 2; i++)
849 add_zone2(i + (uint32_t *) blk, &chg_blk);
851 write_block(block, blk);
855 static void add_zone_dind(uint16_t *znr, smallint *corrected)
857 static char blk[BLOCK_SIZE];
860 smallint chg_blk = 0;
862 block = add_zone(znr, corrected);
865 read_block(block, blk);
866 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
867 add_zone_ind(i + (uint16_t *) blk, &chg_blk);
869 write_block(block, blk);
872 #if ENABLE_FEATURE_MINIX2
873 static void add_zone_dind2(uint32_t *znr, smallint *corrected)
875 static char blk[BLOCK_SIZE];
878 smallint chg_blk = 0;
880 block = add_zone2(znr, corrected);
883 read_block(block, blk);
884 for (i = 0; i < BLOCK_SIZE >> 2; i++)
885 add_zone_ind2(i + (uint32_t *) blk, &chg_blk);
887 write_block(block, blk);
890 static void add_zone_tind2(uint32_t *znr, smallint *corrected)
892 static char blk[BLOCK_SIZE];
895 smallint chg_blk = 0;
897 block = add_zone2(znr, corrected);
900 read_block(block, blk);
901 for (i = 0; i < BLOCK_SIZE >> 2; i++)
902 add_zone_dind2(i + (uint32_t *) blk, &chg_blk);
904 write_block(block, blk);
908 static void check_zones(unsigned i)
910 struct minix1_inode *inode;
912 if (!i || i > INODES)
914 if (inode_count[i] > 1) /* have we counted this file already? */
917 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
918 !S_ISLNK(inode->i_mode)) return;
919 for (i = 0; i < 7; i++)
920 add_zone(i + inode->i_zone, &changed);
921 add_zone_ind(7 + inode->i_zone, &changed);
922 add_zone_dind(8 + inode->i_zone, &changed);
925 #if ENABLE_FEATURE_MINIX2
926 static void check_zones2(unsigned i)
928 struct minix2_inode *inode;
930 if (!i || i > INODES)
932 if (inode_count[i] > 1) /* have we counted this file already? */
935 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
936 && !S_ISLNK(inode->i_mode))
938 for (i = 0; i < 7; i++)
939 add_zone2(i + inode->i_zone, &changed);
940 add_zone_ind2(7 + inode->i_zone, &changed);
941 add_zone_dind2(8 + inode->i_zone, &changed);
942 add_zone_tind2(9 + inode->i_zone, &changed);
946 static void check_file(struct minix1_inode *dir, unsigned offset)
948 static char blk[BLOCK_SIZE];
949 struct minix1_inode *inode;
954 block = map_block(dir, offset / BLOCK_SIZE);
955 read_block(block, blk);
956 name = blk + (offset % BLOCK_SIZE) + 2;
957 ino = *(uint16_t *) (name - 2);
959 printf("%s contains a bad inode number for file '%.*s'. ",
960 current_name, namelen, name);
961 if (ask("Remove", 1)) {
962 *(uint16_t *) (name - 2) = 0;
963 write_block(block, blk);
968 inode = get_inode(ino);
971 if (inode && LONE_CHAR(name, '.'))
973 printf("%s: bad directory: '.' isn't first\n", current_name);
974 errors_uncorrected = 1;
976 if (offset == dirsize) {
977 if (inode && strcmp("..", name) == 0)
979 printf("%s: bad directory: '..' isn't second\n", current_name);
980 errors_uncorrected = 1;
987 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
988 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
991 if (inode && S_ISDIR(inode->i_mode))
992 recursive_check(ino);
996 #if ENABLE_FEATURE_MINIX2
997 static void check_file2(struct minix2_inode *dir, unsigned offset)
999 static char blk[BLOCK_SIZE];
1000 struct minix2_inode *inode;
1005 block = map_block2(dir, offset / BLOCK_SIZE);
1006 read_block(block, blk);
1007 name = blk + (offset % BLOCK_SIZE) + 2;
1008 ino = *(uint16_t *) (name - 2);
1010 printf("%s contains a bad inode number for file '%.*s'. ",
1011 current_name, namelen, name);
1012 if (ask("Remove", 1)) {
1013 *(uint16_t *) (name - 2) = 0;
1014 write_block(block, blk);
1018 push_filename(name);
1019 inode = get_inode2(ino);
1022 if (inode && LONE_CHAR(name, '.'))
1024 printf("%s: bad directory: '.' isn't first\n", current_name);
1025 errors_uncorrected = 1;
1027 if (offset == dirsize) {
1028 if (inode && strcmp("..", name) == 0)
1030 printf("%s: bad directory: '..' isn't second\n", current_name);
1031 errors_uncorrected = 1;
1035 push_filename(name);
1038 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1039 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
1042 if (inode && S_ISDIR(inode->i_mode))
1043 recursive_check2(ino);
1048 static void recursive_check(unsigned ino)
1050 struct minix1_inode *dir;
1054 if (!S_ISDIR(dir->i_mode))
1055 die("internal error");
1056 if (dir->i_size < 2 * dirsize) {
1057 printf("%s: bad directory: size<32", current_name);
1058 errors_uncorrected = 1;
1060 for (offset = 0; offset < dir->i_size; offset += dirsize)
1061 check_file(dir, offset);
1064 #if ENABLE_FEATURE_MINIX2
1065 static void recursive_check2(unsigned ino)
1067 struct minix2_inode *dir;
1071 if (!S_ISDIR(dir->i_mode))
1072 die("internal error");
1073 if (dir->i_size < 2 * dirsize) {
1074 printf("%s: bad directory: size<32", current_name);
1075 errors_uncorrected = 1;
1077 for (offset = 0; offset < dir->i_size; offset += dirsize)
1078 check_file2(dir, offset);
1082 static int bad_zone(int i)
1084 char buffer[BLOCK_SIZE];
1086 if (BLOCK_SIZE * i != lseek(IN, BLOCK_SIZE * i, SEEK_SET))
1087 die("seek failed in bad_zone");
1088 return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE));
1091 static void check_counts(void)
1095 for (i = 1; i <= INODES; i++) {
1096 if (warn_mode && Inode1[i].i_mode && !inode_in_use(i)) {
1097 printf("Inode %d has non-zero mode. ", i);
1098 if (ask("Clear", 1)) {
1099 Inode1[i].i_mode = 0;
1103 if (!inode_count[i]) {
1104 if (!inode_in_use(i))
1106 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
1107 if (ask("Clear", 1))
1111 if (!inode_in_use(i)) {
1112 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
1116 if (Inode1[i].i_nlinks != inode_count[i]) {
1117 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
1118 i, Inode1[i].i_mode, Inode1[i].i_nlinks, inode_count[i]);
1119 if (ask("Set i_nlinks to count", 1)) {
1120 Inode1[i].i_nlinks = inode_count[i];
1125 for (i = FIRSTZONE; i < ZONES; i++) {
1126 if (zone_in_use(i) == zone_count[i])
1128 if (!zone_count[i]) {
1131 printf("Zone %d is marked 'in use', but no file uses it. ", i);
1132 if (ask("Unmark", 1))
1136 printf("Zone %d: %sin use, counted=%d\n",
1137 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1141 #if ENABLE_FEATURE_MINIX2
1142 static void check_counts2(void)
1146 for (i = 1; i <= INODES; i++) {
1147 if (warn_mode && Inode2[i].i_mode && !inode_in_use(i)) {
1148 printf("Inode %d has non-zero mode. ", i);
1149 if (ask("Clear", 1)) {
1150 Inode2[i].i_mode = 0;
1154 if (!inode_count[i]) {
1155 if (!inode_in_use(i))
1157 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
1158 if (ask("Clear", 1))
1162 if (!inode_in_use(i)) {
1163 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
1167 if (Inode2[i].i_nlinks != inode_count[i]) {
1168 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
1169 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1171 if (ask("Set i_nlinks to count", 1)) {
1172 Inode2[i].i_nlinks = inode_count[i];
1177 for (i = FIRSTZONE; i < ZONES; i++) {
1178 if (zone_in_use(i) == zone_count[i])
1180 if (!zone_count[i]) {
1183 printf("Zone %d is marked 'in use', but no file uses it. ", i);
1184 if (ask("Unmark", 1))
1188 printf("Zone %d: %sin use, counted=%d\n",
1189 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1194 static void check(void)
1196 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1197 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1198 check_zones(MINIX_ROOT_INO);
1199 recursive_check(MINIX_ROOT_INO);
1203 #if ENABLE_FEATURE_MINIX2
1204 static void check2(void)
1206 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1207 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1208 check_zones2(MINIX_ROOT_INO);
1209 recursive_check2(MINIX_ROOT_INO);
1216 int fsck_minix_main(int argc, char **argv)
1221 xfunc_error_retval = 8;
1223 alloc_current_name();
1224 #if ENABLE_FEATURE_CLEAN_UP
1225 /* Don't bother to free memory. Exit does
1226 * that automagically, so we can save a few bytes */
1227 atexit(free_current_name);
1230 if (INODE_SIZE1 * MINIX1_INODES_PER_BLOCK != BLOCK_SIZE)
1231 die("bad inode size");
1232 #if ENABLE_FEATURE_MINIX2
1233 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
1234 die("bad v2 inode size");
1236 while (--argc != 0) {
1238 if (argv[0][0] != '-') {
1241 device_name = argv[0];
1243 while (*++argv[0]) {
1244 switch (argv[0][0]) {
1277 check_mount(); /* trying to check a mounted filesystem? */
1278 if (repair && !automatic) {
1279 if (!isatty(0) || !isatty(1))
1280 die("need terminal for interactive repairs");
1282 IN = xopen(device_name, repair ? O_RDWR : O_RDONLY);
1284 /*sync(); paranoia? */
1288 * Determine whether or not we should continue with the checking.
1289 * This is based on the status of the filesystem valid and error
1290 * flags and whether or not the -f switch was specified on the
1293 printf("%s, "PROGRAM_VERSION"\n", applet_name);
1295 if (!(Super.s_state & MINIX_ERROR_FS)
1296 && (Super.s_state & MINIX_VALID_FS) && !force
1299 printf("%s is clean, check is skipped\n", device_name);
1302 printf("Forcing filesystem check on %s\n", device_name);
1304 printf("Filesystem on %s is dirty, needs checking\n",
1309 if (repair && !automatic) {
1310 tcgetattr(0, &termios);
1312 tmp.c_lflag &= ~(ICANON | ECHO);
1313 tcsetattr(0, TCSANOW, &tmp);
1328 for (i = 1, free_cnt = 0; i <= INODES; i++)
1329 if (!inode_in_use(i))
1331 printf("\n%6u inodes used (%u%%)\n", (INODES - free_cnt),
1332 100 * (INODES - free_cnt) / INODES);
1333 for (i = FIRSTZONE, free_cnt = 0; i < ZONES; i++)
1334 if (!zone_in_use(i))
1336 printf("%6u zones used (%u%%)\n\n"
1337 "%6u regular files\n"
1339 "%6u character device files\n"
1340 "%6u block device files\n"
1342 "%6u symbolic links\n"
1345 (ZONES - free_cnt), 100 * (ZONES - free_cnt) / ZONES,
1346 regular, directory, chardev, blockdev,
1347 links - 2 * directory + 1, symlinks,
1348 total - 2 * directory + 1);
1352 printf("FILE SYSTEM HAS BEEN CHANGED\n");
1355 write_super_block();
1357 if (repair && !automatic)
1358 tcsetattr(0, TCSANOW, &termios);
1362 if (errors_uncorrected)