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 :-).
95 #define BLKGETSIZE _IO(0x12,96) /* return device size */
100 MINIX1_LINK_MAX = 250,
101 MINIX2_LINK_MAX = 65530,
102 MINIX_I_MAP_SLOTS = 8,
103 MINIX_Z_MAP_SLOTS = 64,
104 MINIX_V1 = 0x0001, /* original minix fs */
105 MINIX_V2 = 0x0002, /* minix V2 fs */
107 MINIX_NAME_MAX = 255, /* # chars in a file name */
110 #if !ENABLE_FEATURE_MINIX2
111 enum { version2 = 0 };
114 enum { MAX_DEPTH = 32 };
118 #if ENABLE_FEATURE_MINIX2
121 smallint repair, automatic, verbose, list, show, warn_mode, force;
122 smallint changed; /* is filesystem modified? */
123 smallint errors_uncorrected; /* flag if some error was not corrected */
124 smallint termios_set;
128 int directory, regular, blockdev, chardev, links, symlinks, total;
134 unsigned char *inode_count;
135 unsigned char *zone_count;
139 char *name_component[MAX_DEPTH+1];
142 struct termios sv_termios;
143 char super_block_buffer[BLOCK_SIZE];
144 char add_zone_ind_blk[BLOCK_SIZE];
145 char add_zone_dind_blk[BLOCK_SIZE];
146 USE_FEATURE_MINIX2(char add_zone_tind_blk[BLOCK_SIZE];)
147 char check_file_blk[BLOCK_SIZE];
150 char current_name[MAX_DEPTH * MINIX_NAME_MAX];
153 #define G (*ptr_to_globals)
154 #define dev_fd (G.dev_fd )
155 #if ENABLE_FEATURE_MINIX2
156 #define version2 (G.version2 )
158 #define repair (G.repair )
159 #define automatic (G.automatic )
160 #define verbose (G.verbose )
161 #define list (G.list )
162 #define show (G.show )
163 #define warn_mode (G.warn_mode )
164 #define force (G.force )
165 #define changed (G.changed )
166 #define errors_uncorrected (G.errors_uncorrected )
167 #define termios_set (G.termios_set )
168 #define dirsize (G.dirsize )
169 #define namelen (G.namelen )
170 #define device_name (G.device_name )
171 #define directory (G.directory )
172 #define regular (G.regular )
173 #define blockdev (G.blockdev )
174 #define chardev (G.chardev )
175 #define links (G.links )
176 #define symlinks (G.symlinks )
177 #define total (G.total )
178 #define inode_buffer (G.inode_buffer )
179 #define inode_map (G.inode_map )
180 #define zone_map (G.zone_map )
181 #define inode_count (G.inode_count )
182 #define zone_count (G.zone_count )
183 #define name_depth (G.name_depth )
184 #define name_component (G.name_component )
185 #define sv_termios (G.sv_termios )
186 #define super_block_buffer (G.super_block_buffer )
187 #define add_zone_ind_blk (G.add_zone_ind_blk )
188 #define add_zone_dind_blk (G.add_zone_dind_blk )
189 #define add_zone_tind_blk (G.add_zone_tind_blk )
190 #define check_file_blk (G.check_file_blk )
191 #define current_name (G.current_name )
192 #define INIT_G() do { \
195 current_name[0] = '/'; \
196 /*current_name[1] = '\0';*/ \
197 name_component[0] = ¤t_name[0]; \
200 #define Inode1 (((struct minix1_inode *) inode_buffer)-1)
201 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
203 #define Super (*(struct minix_super_block *)(super_block_buffer))
205 #if ENABLE_FEATURE_MINIX2
206 # define ZONES ((unsigned)(version2 ? Super.s_zones : Super.s_nzones))
208 # define ZONES ((unsigned)(Super.s_nzones))
210 #define INODES ((unsigned)Super.s_ninodes)
211 #define IMAPS ((unsigned)Super.s_imap_blocks)
212 #define ZMAPS ((unsigned)Super.s_zmap_blocks)
213 #define FIRSTZONE ((unsigned)Super.s_firstdatazone)
214 #define ZONESIZE ((unsigned)Super.s_log_zone_size)
215 #define MAXSIZE ((unsigned)Super.s_max_size)
216 #define MAGIC (Super.s_magic)
218 /* gcc likes this more (code is smaller) than macro variant */
219 static ALWAYS_INLINE unsigned div_roundup(unsigned size, unsigned n)
221 return (size + n-1) / n;
224 #if ENABLE_FEATURE_MINIX2
225 #define INODE_BLOCKS div_roundup(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
226 : MINIX1_INODES_PER_BLOCK))
228 #define INODE_BLOCKS div_roundup(INODES, MINIX1_INODES_PER_BLOCK)
231 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
232 #define NORM_FIRSTZONE (2 + IMAPS + ZMAPS + INODE_BLOCKS)
234 /* Before you ask "where they come from?": */
235 /* setbit/clrbit are supplied by sys/param.h */
237 static int minix_bit(const char *a, unsigned i)
239 return (a[i >> 3] & (1<<(i & 7)));
242 static void minix_setbit(char *a, unsigned i)
247 static void minix_clrbit(char *a, unsigned i)
253 /* Note: do not assume 0/1, it is 0/nonzero */
254 #define zone_in_use(x) (minix_bit(zone_map,(x)-FIRSTZONE+1))
255 #define inode_in_use(x) (minix_bit(inode_map,(x)))
257 #define mark_inode(x) (minix_setbit(inode_map,(x)))
258 #define unmark_inode(x) (minix_clrbit(inode_map,(x)))
260 #define mark_zone(x) (minix_setbit(zone_map,(x)-FIRSTZONE+1))
261 #define unmark_zone(x) (minix_clrbit(zone_map,(x)-FIRSTZONE+1))
264 static void recursive_check(unsigned ino);
265 #if ENABLE_FEATURE_MINIX2
266 static void recursive_check2(unsigned ino);
269 static void die(const char *str) ATTRIBUTE_NORETURN;
270 static void die(const char *str)
273 tcsetattr(0, TCSANOW, &sv_termios);
274 bb_error_msg_and_die("%s", str);
277 static void push_filename(const char *name)
281 // [0] [1] [2] <-name_component[i]
282 if (name_depth < MAX_DEPTH) {
284 char *p = name_component[name_depth];
286 len = sprintf(p, "%.*s", namelen, name);
287 name_component[name_depth + 1] = p + len;
292 static void pop_filename(void)
295 if (name_depth < MAX_DEPTH) {
296 *name_component[name_depth] = '\0';
298 current_name[0] = '/';
299 current_name[1] = '\0';
304 static int ask(const char *string, int def)
310 errors_uncorrected = 1;
316 errors_uncorrected = 1;
319 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
325 errors_uncorrected = 1;
332 } else if (c == 'N') {
335 } else if (c == ' ' || c == '\n')
342 errors_uncorrected = 1;
348 * Make certain that we aren't checking a filesystem that is on a
349 * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
350 * 1994 Theodore Ts'o. Also licensed under GPL.
352 static void check_mount(void)
359 f = setmntent(MOUNTED, "r");
362 while ((mnt = getmntent(f)) != NULL)
363 if (strcmp(device_name, mnt->mnt_fsname) == 0)
370 * If the root is mounted read-only, then /etc/mtab is
371 * probably not correct; so we won't issue a warning based on
374 fd = open(MOUNTED, O_RDWR);
375 if (fd < 0 && errno == EROFS)
379 printf("%s is mounted. ", device_name);
381 if (isatty(0) && isatty(1))
382 cont = ask("Do you really want to continue", 0);
384 printf("Check aborted\n");
390 * check_zone_nr checks to see that *nr is a valid zone nr. If it
391 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
392 * if an error was corrected, and returns the zone (0 for no zone
393 * or a bad zone-number).
395 static int check_zone_nr2(uint32_t *nr, smallint *corrected)
402 else if (*nr >= ZONES)
406 printf("Zone nr %s in file '%s'. ", msg, current_name);
407 if (ask("Remove block", 1)) {
414 static int check_zone_nr(uint16_t *nr, smallint *corrected)
417 int r = check_zone_nr2(&nr32, corrected);
418 *nr = (uint16_t)nr32;
423 * read-block reads block nr into the buffer at addr.
425 static void read_block(unsigned nr, char *addr)
428 memset(addr, 0, BLOCK_SIZE);
431 if (BLOCK_SIZE * nr != lseek(dev_fd, BLOCK_SIZE * nr, SEEK_SET)) {
432 printf("%s: cannot seek to block in file '%s'\n",
433 bb_msg_read_error, current_name);
434 errors_uncorrected = 1;
435 memset(addr, 0, BLOCK_SIZE);
436 } else if (BLOCK_SIZE != read(dev_fd, addr, BLOCK_SIZE)) {
437 printf("%s: bad block in file '%s'\n",
438 bb_msg_read_error, current_name);
439 errors_uncorrected = 1;
440 memset(addr, 0, BLOCK_SIZE);
445 * write_block writes block nr to disk.
447 static void write_block(unsigned nr, char *addr)
451 if (nr < FIRSTZONE || nr >= ZONES) {
452 printf("Internal error: trying to write bad block\n"
453 "Write request ignored\n");
454 errors_uncorrected = 1;
457 if (BLOCK_SIZE * nr != lseek(dev_fd, BLOCK_SIZE * nr, SEEK_SET))
458 die("seek failed in write_block");
459 if (BLOCK_SIZE != write(dev_fd, addr, BLOCK_SIZE)) {
460 printf("%s: bad block in file '%s'\n",
461 bb_msg_write_error, 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 minix1_inode *inode, unsigned blknr)
473 uint16_t ind[BLOCK_SIZE >> 1];
474 uint16_t dind[BLOCK_SIZE >> 1];
479 return check_zone_nr(inode->i_zone + blknr, &changed);
482 block = check_zone_nr(inode->i_zone + 7, &changed);
483 read_block(block, (char *) ind);
485 result = check_zone_nr(blknr + ind, &blk_chg);
487 write_block(block, (char *) ind);
491 block = check_zone_nr(inode->i_zone + 8, &changed);
492 read_block(block, (char *) dind);
494 result = check_zone_nr(dind + (blknr / 512), &blk_chg);
496 write_block(block, (char *) dind);
498 read_block(block, (char *) ind);
500 result = check_zone_nr(ind + (blknr % 512), &blk_chg);
502 write_block(block, (char *) ind);
506 #if ENABLE_FEATURE_MINIX2
507 static int map_block2(struct minix2_inode *inode, unsigned blknr)
509 uint32_t ind[BLOCK_SIZE >> 2];
510 uint32_t dind[BLOCK_SIZE >> 2];
511 uint32_t tind[BLOCK_SIZE >> 2];
516 return check_zone_nr2(inode->i_zone + blknr, &changed);
519 block = check_zone_nr2(inode->i_zone + 7, &changed);
520 read_block(block, (char *) ind);
522 result = check_zone_nr2(blknr + ind, &blk_chg);
524 write_block(block, (char *) ind);
528 if (blknr >= 256 * 256) {
529 block = check_zone_nr2(inode->i_zone + 8, &changed);
530 read_block(block, (char *) dind);
532 result = check_zone_nr2(dind + blknr / 256, &blk_chg);
534 write_block(block, (char *) dind);
536 read_block(block, (char *) ind);
538 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
540 write_block(block, (char *) ind);
544 block = check_zone_nr2(inode->i_zone + 9, &changed);
545 read_block(block, (char *) tind);
547 result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
549 write_block(block, (char *) tind);
551 read_block(block, (char *) dind);
553 result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
555 write_block(block, (char *) dind);
557 read_block(block, (char *) ind);
559 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
561 write_block(block, (char *) ind);
566 static void write_super_block(void)
569 * Set the state of the filesystem based on whether or not there
570 * are uncorrected errors. The filesystem valid flag is
571 * unconditionally set if we get this far.
573 Super.s_state |= MINIX_VALID_FS | MINIX_ERROR_FS;
574 if (!errors_uncorrected)
575 Super.s_state &= ~MINIX_ERROR_FS;
577 if (BLOCK_SIZE != lseek(dev_fd, BLOCK_SIZE, SEEK_SET))
578 die("seek failed in write_super_block");
579 if (BLOCK_SIZE != write(dev_fd, super_block_buffer, BLOCK_SIZE))
580 die("cannot write super-block");
583 static void write_tables(void)
587 if (IMAPS * BLOCK_SIZE != write(dev_fd, inode_map, IMAPS * BLOCK_SIZE))
588 die("cannot write inode map");
589 if (ZMAPS * BLOCK_SIZE != write(dev_fd, zone_map, ZMAPS * BLOCK_SIZE))
590 die("cannot write zone map");
591 if (INODE_BUFFER_SIZE != write(dev_fd, inode_buffer, INODE_BUFFER_SIZE))
592 die("cannot write inodes");
595 static void get_dirsize(void)
598 char blk[BLOCK_SIZE];
601 #if ENABLE_FEATURE_MINIX2
603 block = Inode2[MINIX_ROOT_INO].i_zone[0];
606 block = Inode1[MINIX_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(dev_fd, BLOCK_SIZE, SEEK_SET))
622 if (BLOCK_SIZE != read(dev_fd, super_block_buffer, BLOCK_SIZE))
623 die("cannot read super block");
624 /* already initialized to:
629 if (MAGIC == MINIX1_SUPER_MAGIC) {
630 } else if (MAGIC == MINIX1_SUPER_MAGIC2) {
633 #if ENABLE_FEATURE_MINIX2
634 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
636 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
642 die("bad magic number in super-block");
643 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
644 die("only 1k blocks/zones supported");
645 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
646 die("bad s_imap_blocks field in super-block");
647 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
648 die("bad s_zmap_blocks field in super-block");
651 static void read_tables(void)
653 inode_map = xzalloc(IMAPS * BLOCK_SIZE);
654 zone_map = xzalloc(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(dev_fd, inode_map, IMAPS * BLOCK_SIZE))
659 die("cannot read inode map");
660 if (ZMAPS * BLOCK_SIZE != read(dev_fd, zone_map, ZMAPS * BLOCK_SIZE))
661 die("cannot read zone map");
662 if (INODE_BUFFER_SIZE != read(dev_fd, inode_buffer, INODE_BUFFER_SIZE))
663 die("cannot read inodes");
664 if (NORM_FIRSTZONE != FIRSTZONE) {
665 printf("warning: firstzone!=norm_firstzone\n");
666 errors_uncorrected = 1;
672 "Firstdatazone=%u (%u)\n"
675 "Filesystem state=%u\n"
679 FIRSTZONE, NORM_FIRSTZONE,
680 BLOCK_SIZE << ZONESIZE,
687 static struct minix1_inode *get_inode(unsigned nr)
689 struct minix1_inode *inode;
691 if (!nr || nr > INODES)
695 if (!inode_count[nr]) {
696 if (!inode_in_use(nr)) {
697 printf("Inode %d is marked as 'unused', but it is used "
698 "for file '%s'\n", nr, current_name);
700 if (ask("Mark as 'in use'", 1))
703 errors_uncorrected = 1;
706 if (S_ISDIR(inode->i_mode))
708 else if (S_ISREG(inode->i_mode))
710 else if (S_ISCHR(inode->i_mode))
712 else if (S_ISBLK(inode->i_mode))
714 else if (S_ISLNK(inode->i_mode))
716 else if (S_ISSOCK(inode->i_mode));
717 else if (S_ISFIFO(inode->i_mode));
719 printf("%s has mode %05o\n", current_name, inode->i_mode);
724 if (!++inode_count[nr]) {
725 printf("Warning: inode count too big\n");
727 errors_uncorrected = 1;
732 #if ENABLE_FEATURE_MINIX2
733 static struct minix2_inode *get_inode2(unsigned nr)
735 struct minix2_inode *inode;
737 if (!nr || nr > INODES)
741 if (!inode_count[nr]) {
742 if (!inode_in_use(nr)) {
743 printf("Inode %d is marked as 'unused', but it is used "
744 "for file '%s'\n", nr, current_name);
746 if (ask("Mark as 'in use'", 1))
749 errors_uncorrected = 1;
752 if (S_ISDIR(inode->i_mode))
754 else if (S_ISREG(inode->i_mode))
756 else if (S_ISCHR(inode->i_mode))
758 else if (S_ISBLK(inode->i_mode))
760 else if (S_ISLNK(inode->i_mode))
762 else if (S_ISSOCK(inode->i_mode));
763 else if (S_ISFIFO(inode->i_mode));
765 printf("%s has mode %05o\n", current_name, inode->i_mode);
769 if (!++inode_count[nr]) {
770 printf("Warning: inode count too big\n");
772 errors_uncorrected = 1;
778 static void check_root(void)
780 struct minix1_inode *inode = Inode1 + MINIX_ROOT_INO;
782 if (!inode || !S_ISDIR(inode->i_mode))
783 die("root inode isn't a directory");
786 #if ENABLE_FEATURE_MINIX2
787 static void check_root2(void)
789 struct minix2_inode *inode = Inode2 + MINIX_ROOT_INO;
791 if (!inode || !S_ISDIR(inode->i_mode))
792 die("root inode isn't a directory");
795 void check_root2(void);
798 static int add_zone(uint16_t *znr, smallint *corrected)
804 block = check_zone_nr(znr, corrected);
807 if (zone_count[block]) {
808 printf("Already used block is reused in file '%s'. ",
810 if (ask("Clear", 1)) {
817 if (!zone_in_use(block)) {
818 printf("Block %d in file '%s' is marked as 'unused'. ",
819 block, current_name);
820 if (ask("Correct", 1))
823 if (!++zone_count[block])
828 #if ENABLE_FEATURE_MINIX2
829 static int add_zone2(uint32_t *znr, smallint *corrected)
835 block = check_zone_nr2(znr, corrected);
838 if (zone_count[block]) {
839 printf("Already used block is reused in file '%s'. ",
841 if (ask("Clear", 1)) {
848 if (!zone_in_use(block)) {
849 printf("Block %d in file '%s' is marked as 'unused'. ",
850 block, current_name);
851 if (ask("Correct", 1))
854 if (!++zone_count[block])
860 static void add_zone_ind(uint16_t *znr, smallint *corrected)
864 smallint chg_blk = 0;
866 block = add_zone(znr, corrected);
869 read_block(block, add_zone_ind_blk);
870 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
871 add_zone(i + (uint16_t *) add_zone_ind_blk, &chg_blk);
873 write_block(block, add_zone_ind_blk);
876 #if ENABLE_FEATURE_MINIX2
877 static void add_zone_ind2(uint32_t *znr, smallint *corrected)
881 smallint chg_blk = 0;
883 block = add_zone2(znr, corrected);
886 read_block(block, add_zone_ind_blk);
887 for (i = 0; i < BLOCK_SIZE >> 2; i++)
888 add_zone2(i + (uint32_t *) add_zone_ind_blk, &chg_blk);
890 write_block(block, add_zone_ind_blk);
894 static void add_zone_dind(uint16_t *znr, smallint *corrected)
898 smallint chg_blk = 0;
900 block = add_zone(znr, corrected);
903 read_block(block, add_zone_dind_blk);
904 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
905 add_zone_ind(i + (uint16_t *) add_zone_dind_blk, &chg_blk);
907 write_block(block, add_zone_dind_blk);
910 #if ENABLE_FEATURE_MINIX2
911 static void add_zone_dind2(uint32_t *znr, smallint *corrected)
915 smallint chg_blk = 0;
917 block = add_zone2(znr, corrected);
920 read_block(block, add_zone_dind_blk);
921 for (i = 0; i < BLOCK_SIZE >> 2; i++)
922 add_zone_ind2(i + (uint32_t *) add_zone_dind_blk, &chg_blk);
924 write_block(block, add_zone_dind_blk);
927 static void add_zone_tind2(uint32_t *znr, smallint *corrected)
931 smallint chg_blk = 0;
933 block = add_zone2(znr, corrected);
936 read_block(block, add_zone_tind_blk);
937 for (i = 0; i < BLOCK_SIZE >> 2; i++)
938 add_zone_dind2(i + (uint32_t *) add_zone_tind_blk, &chg_blk);
940 write_block(block, add_zone_tind_blk);
944 static void check_zones(unsigned i)
946 struct minix1_inode *inode;
948 if (!i || i > INODES)
950 if (inode_count[i] > 1) /* have we counted this file already? */
953 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
954 !S_ISLNK(inode->i_mode)) return;
955 for (i = 0; i < 7; i++)
956 add_zone(i + inode->i_zone, &changed);
957 add_zone_ind(7 + inode->i_zone, &changed);
958 add_zone_dind(8 + inode->i_zone, &changed);
961 #if ENABLE_FEATURE_MINIX2
962 static void check_zones2(unsigned i)
964 struct minix2_inode *inode;
966 if (!i || i > INODES)
968 if (inode_count[i] > 1) /* have we counted this file already? */
971 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
972 && !S_ISLNK(inode->i_mode))
974 for (i = 0; i < 7; i++)
975 add_zone2(i + inode->i_zone, &changed);
976 add_zone_ind2(7 + inode->i_zone, &changed);
977 add_zone_dind2(8 + inode->i_zone, &changed);
978 add_zone_tind2(9 + inode->i_zone, &changed);
982 static void check_file(struct minix1_inode *dir, unsigned offset)
984 struct minix1_inode *inode;
989 block = map_block(dir, offset / BLOCK_SIZE);
990 read_block(block, check_file_blk);
991 name = check_file_blk + (offset % BLOCK_SIZE) + 2;
992 ino = *(uint16_t *) (name - 2);
994 printf("%s contains a bad inode number for file '%.*s'. ",
995 current_name, namelen, name);
996 if (ask("Remove", 1)) {
997 *(uint16_t *) (name - 2) = 0;
998 write_block(block, check_file_blk);
1002 push_filename(name);
1003 inode = get_inode(ino);
1006 if (inode && LONE_CHAR(name, '.'))
1008 printf("%s: bad directory: '.' isn't first\n", current_name);
1009 errors_uncorrected = 1;
1011 if (offset == dirsize) {
1012 if (inode && strcmp("..", name) == 0)
1014 printf("%s: bad directory: '..' isn't second\n", current_name);
1015 errors_uncorrected = 1;
1019 push_filename(name);
1022 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1023 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
1026 if (inode && S_ISDIR(inode->i_mode))
1027 recursive_check(ino);
1031 #if ENABLE_FEATURE_MINIX2
1032 static void check_file2(struct minix2_inode *dir, unsigned offset)
1034 struct minix2_inode *inode;
1039 block = map_block2(dir, offset / BLOCK_SIZE);
1040 read_block(block, check_file_blk);
1041 name = check_file_blk + (offset % BLOCK_SIZE) + 2;
1042 ino = *(uint16_t *) (name - 2);
1044 printf("%s contains a bad inode number for file '%.*s'. ",
1045 current_name, namelen, name);
1046 if (ask("Remove", 1)) {
1047 *(uint16_t *) (name - 2) = 0;
1048 write_block(block, check_file_blk);
1052 push_filename(name);
1053 inode = get_inode2(ino);
1056 if (inode && LONE_CHAR(name, '.'))
1058 printf("%s: bad directory: '.' isn't first\n", current_name);
1059 errors_uncorrected = 1;
1061 if (offset == dirsize) {
1062 if (inode && strcmp("..", name) == 0)
1064 printf("%s: bad directory: '..' isn't second\n", current_name);
1065 errors_uncorrected = 1;
1069 push_filename(name);
1072 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1073 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
1076 if (inode && S_ISDIR(inode->i_mode))
1077 recursive_check2(ino);
1082 static void recursive_check(unsigned ino)
1084 struct minix1_inode *dir;
1088 if (!S_ISDIR(dir->i_mode))
1089 die("internal error");
1090 if (dir->i_size < 2 * dirsize) {
1091 printf("%s: bad directory: size<32", current_name);
1092 errors_uncorrected = 1;
1094 for (offset = 0; offset < dir->i_size; offset += dirsize)
1095 check_file(dir, offset);
1098 #if ENABLE_FEATURE_MINIX2
1099 static void recursive_check2(unsigned ino)
1101 struct minix2_inode *dir;
1105 if (!S_ISDIR(dir->i_mode))
1106 die("internal error");
1107 if (dir->i_size < 2 * dirsize) {
1108 printf("%s: bad directory: size<32", current_name);
1109 errors_uncorrected = 1;
1111 for (offset = 0; offset < dir->i_size; offset += dirsize)
1112 check_file2(dir, offset);
1116 static int bad_zone(int i)
1118 char buffer[BLOCK_SIZE];
1120 if (BLOCK_SIZE * i != lseek(dev_fd, BLOCK_SIZE * i, SEEK_SET))
1121 die("seek failed in bad_zone");
1122 return (BLOCK_SIZE != read(dev_fd, buffer, BLOCK_SIZE));
1125 static void check_counts(void)
1129 for (i = 1; i <= INODES; i++) {
1130 if (warn_mode && Inode1[i].i_mode && !inode_in_use(i)) {
1131 printf("Inode %d has non-zero mode. ", i);
1132 if (ask("Clear", 1)) {
1133 Inode1[i].i_mode = 0;
1137 if (!inode_count[i]) {
1138 if (!inode_in_use(i))
1140 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
1141 if (ask("Clear", 1))
1145 if (!inode_in_use(i)) {
1146 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
1150 if (Inode1[i].i_nlinks != inode_count[i]) {
1151 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
1152 i, Inode1[i].i_mode, Inode1[i].i_nlinks,
1154 if (ask("Set i_nlinks to count", 1)) {
1155 Inode1[i].i_nlinks = inode_count[i];
1160 for (i = FIRSTZONE; i < ZONES; i++) {
1161 if ((zone_in_use(i) != 0) == zone_count[i])
1163 if (!zone_count[i]) {
1166 printf("Zone %d is marked 'in use', but no file uses it. ", i);
1167 if (ask("Unmark", 1))
1171 printf("Zone %d: %sin use, counted=%d\n",
1172 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1176 #if ENABLE_FEATURE_MINIX2
1177 static void check_counts2(void)
1181 for (i = 1; i <= INODES; i++) {
1182 if (warn_mode && Inode2[i].i_mode && !inode_in_use(i)) {
1183 printf("Inode %d has non-zero mode. ", i);
1184 if (ask("Clear", 1)) {
1185 Inode2[i].i_mode = 0;
1189 if (!inode_count[i]) {
1190 if (!inode_in_use(i))
1192 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
1193 if (ask("Clear", 1))
1197 if (!inode_in_use(i)) {
1198 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
1202 if (Inode2[i].i_nlinks != inode_count[i]) {
1203 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
1204 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1206 if (ask("Set i_nlinks to count", 1)) {
1207 Inode2[i].i_nlinks = inode_count[i];
1212 for (i = FIRSTZONE; i < ZONES; i++) {
1213 if ((zone_in_use(i) != 0) == zone_count[i])
1215 if (!zone_count[i]) {
1218 printf("Zone %d is marked 'in use', but no file uses it. ", i);
1219 if (ask("Unmark", 1))
1223 printf("Zone %d: %sin use, counted=%d\n",
1224 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1229 static void check(void)
1231 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1232 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1233 check_zones(MINIX_ROOT_INO);
1234 recursive_check(MINIX_ROOT_INO);
1238 #if ENABLE_FEATURE_MINIX2
1239 static void check2(void)
1241 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1242 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1243 check_zones2(MINIX_ROOT_INO);
1244 recursive_check2(MINIX_ROOT_INO);
1251 int fsck_minix_main(int argc, char **argv);
1252 int fsck_minix_main(int argc, char **argv)
1257 xfunc_error_retval = 8;
1261 if (INODE_SIZE1 * MINIX1_INODES_PER_BLOCK != BLOCK_SIZE)
1262 die("bad inode size");
1263 #if ENABLE_FEATURE_MINIX2
1264 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
1265 die("bad v2 inode size");
1267 while (--argc != 0) {
1269 if (argv[0][0] != '-') {
1272 device_name = argv[0];
1274 while (*++argv[0]) {
1275 switch (argv[0][0]) {
1308 check_mount(); /* trying to check a mounted filesystem? */
1309 if (repair && !automatic) {
1310 if (!isatty(0) || !isatty(1))
1311 die("need terminal for interactive repairs");
1313 dev_fd = xopen(device_name, repair ? O_RDWR : O_RDONLY);
1315 /*sync(); paranoia? */
1319 * Determine whether or not we should continue with the checking.
1320 * This is based on the status of the filesystem valid and error
1321 * flags and whether or not the -f switch was specified on the
1324 printf("%s: %s\n", applet_name, bb_banner);
1326 if (!(Super.s_state & MINIX_ERROR_FS)
1327 && (Super.s_state & MINIX_VALID_FS) && !force
1330 printf("%s is clean, check is skipped\n", device_name);
1333 printf("Forcing filesystem check on %s\n", device_name);
1335 printf("Filesystem on %s is dirty, needs checking\n",
1340 if (repair && !automatic) {
1341 tcgetattr(0, &sv_termios);
1343 tmp.c_lflag &= ~(ICANON | ECHO);
1344 tcsetattr(0, TCSANOW, &tmp);
1359 for (i = 1, free_cnt = 0; i <= INODES; i++)
1360 if (!inode_in_use(i))
1362 printf("\n%6u inodes used (%u%%)\n", (INODES - free_cnt),
1363 100 * (INODES - free_cnt) / INODES);
1364 for (i = FIRSTZONE, free_cnt = 0; i < ZONES; i++)
1365 if (!zone_in_use(i))
1367 printf("%6u zones used (%u%%)\n\n"
1368 "%6u regular files\n"
1370 "%6u character device files\n"
1371 "%6u block device files\n"
1373 "%6u symbolic links\n"
1376 (ZONES - free_cnt), 100 * (ZONES - free_cnt) / ZONES,
1377 regular, directory, chardev, blockdev,
1378 links - 2 * directory + 1, symlinks,
1379 total - 2 * directory + 1);
1383 printf("FILE SYSTEM HAS BEEN CHANGED\n");
1386 write_super_block();
1388 if (repair && !automatic)
1389 tcsetattr(0, TCSANOW, &sv_termios);
1393 if (errors_uncorrected)