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 { \
193 PTR_TO_GLOBALS = xzalloc(sizeof(G)); \
196 current_name[0] = '/'; \
197 /*current_name[1] = '\0';*/ \
198 name_component[0] = ¤t_name[0]; \
201 #define Inode1 (((struct minix1_inode *) inode_buffer)-1)
202 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
204 #define Super (*(struct minix_super_block *)(super_block_buffer))
206 #if ENABLE_FEATURE_MINIX2
207 # define ZONES ((unsigned)(version2 ? Super.s_zones : Super.s_nzones))
209 # define ZONES ((unsigned)(Super.s_nzones))
211 #define INODES ((unsigned)Super.s_ninodes)
212 #define IMAPS ((unsigned)Super.s_imap_blocks)
213 #define ZMAPS ((unsigned)Super.s_zmap_blocks)
214 #define FIRSTZONE ((unsigned)Super.s_firstdatazone)
215 #define ZONESIZE ((unsigned)Super.s_log_zone_size)
216 #define MAXSIZE ((unsigned)Super.s_max_size)
217 #define MAGIC (Super.s_magic)
219 /* gcc likes this more (code is smaller) than macro variant */
220 static ALWAYS_INLINE unsigned div_roundup(unsigned size, unsigned n)
222 return (size + n-1) / n;
225 #if ENABLE_FEATURE_MINIX2
226 #define INODE_BLOCKS div_roundup(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
227 : MINIX1_INODES_PER_BLOCK))
229 #define INODE_BLOCKS div_roundup(INODES, MINIX1_INODES_PER_BLOCK)
232 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
233 #define NORM_FIRSTZONE (2 + IMAPS + ZMAPS + INODE_BLOCKS)
235 /* Before you ask "where they come from?": */
236 /* setbit/clrbit are supplied by sys/param.h */
238 static int minix_bit(const char *a, unsigned i)
240 return (a[i >> 3] & (1<<(i & 7)));
243 static void minix_setbit(char *a, unsigned i)
248 static void minix_clrbit(char *a, unsigned i)
254 /* Note: do not assume 0/1, it is 0/nonzero */
255 #define zone_in_use(x) (minix_bit(zone_map,(x)-FIRSTZONE+1))
256 #define inode_in_use(x) (minix_bit(inode_map,(x)))
258 #define mark_inode(x) (minix_setbit(inode_map,(x)))
259 #define unmark_inode(x) (minix_clrbit(inode_map,(x)))
261 #define mark_zone(x) (minix_setbit(zone_map,(x)-FIRSTZONE+1))
262 #define unmark_zone(x) (minix_clrbit(zone_map,(x)-FIRSTZONE+1))
265 static void recursive_check(unsigned ino);
266 #if ENABLE_FEATURE_MINIX2
267 static void recursive_check2(unsigned ino);
270 static void die(const char *str) ATTRIBUTE_NORETURN;
271 static void die(const char *str)
274 tcsetattr(0, TCSANOW, &sv_termios);
275 bb_error_msg_and_die("%s", str);
278 static void push_filename(const char *name)
282 // [0] [1] [2] <-name_component[i]
283 if (name_depth < MAX_DEPTH) {
285 char *p = name_component[name_depth];
287 len = sprintf(p, "%.*s", namelen, name);
288 name_component[name_depth + 1] = p + len;
293 static void pop_filename(void)
296 if (name_depth < MAX_DEPTH) {
297 *name_component[name_depth] = '\0';
299 current_name[0] = '/';
300 current_name[1] = '\0';
305 static int ask(const char *string, int def)
311 errors_uncorrected = 1;
317 errors_uncorrected = 1;
320 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
326 errors_uncorrected = 1;
333 } else if (c == 'N') {
336 } else if (c == ' ' || c == '\n')
343 errors_uncorrected = 1;
349 * Make certain that we aren't checking a filesystem that is on a
350 * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
351 * 1994 Theodore Ts'o. Also licensed under GPL.
353 static void check_mount(void)
360 f = setmntent(MOUNTED, "r");
363 while ((mnt = getmntent(f)) != NULL)
364 if (strcmp(device_name, mnt->mnt_fsname) == 0)
371 * If the root is mounted read-only, then /etc/mtab is
372 * probably not correct; so we won't issue a warning based on
375 fd = open(MOUNTED, O_RDWR);
376 if (fd < 0 && errno == EROFS)
380 printf("%s is mounted. ", device_name);
382 if (isatty(0) && isatty(1))
383 cont = ask("Do you really want to continue", 0);
385 printf("Check aborted\n");
391 * check_zone_nr checks to see that *nr is a valid zone nr. If it
392 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
393 * if an error was corrected, and returns the zone (0 for no zone
394 * or a bad zone-number).
396 static int check_zone_nr2(uint32_t *nr, smallint *corrected)
403 else if (*nr >= ZONES)
407 printf("Zone nr %s in file '%s'. ", msg, current_name);
408 if (ask("Remove block", 1)) {
415 static int check_zone_nr(uint16_t *nr, smallint *corrected)
418 int r = check_zone_nr2(&nr32, corrected);
419 *nr = (uint16_t)nr32;
424 * read-block reads block nr into the buffer at addr.
426 static void read_block(unsigned nr, char *addr)
429 memset(addr, 0, BLOCK_SIZE);
432 if (BLOCK_SIZE * nr != lseek(dev_fd, BLOCK_SIZE * nr, SEEK_SET)) {
433 printf("%s: cannot seek to block in file '%s'\n",
434 bb_msg_read_error, current_name);
435 errors_uncorrected = 1;
436 memset(addr, 0, BLOCK_SIZE);
437 } else if (BLOCK_SIZE != read(dev_fd, addr, BLOCK_SIZE)) {
438 printf("%s: bad block in file '%s'\n",
439 bb_msg_read_error, current_name);
440 errors_uncorrected = 1;
441 memset(addr, 0, BLOCK_SIZE);
446 * write_block writes block nr to disk.
448 static void write_block(unsigned nr, char *addr)
452 if (nr < FIRSTZONE || nr >= ZONES) {
453 printf("Internal error: trying to write bad block\n"
454 "Write request ignored\n");
455 errors_uncorrected = 1;
458 if (BLOCK_SIZE * nr != lseek(dev_fd, BLOCK_SIZE * nr, SEEK_SET))
459 die("seek failed in write_block");
460 if (BLOCK_SIZE != write(dev_fd, addr, BLOCK_SIZE)) {
461 printf("%s: bad block in file '%s'\n",
462 bb_msg_write_error, current_name);
463 errors_uncorrected = 1;
468 * map_block calculates the absolute block nr of a block in a file.
469 * It sets 'changed' if the inode has needed changing, and re-writes
470 * any indirect blocks with errors.
472 static int map_block(struct minix1_inode *inode, unsigned blknr)
474 uint16_t ind[BLOCK_SIZE >> 1];
475 uint16_t dind[BLOCK_SIZE >> 1];
480 return check_zone_nr(inode->i_zone + blknr, &changed);
483 block = check_zone_nr(inode->i_zone + 7, &changed);
484 read_block(block, (char *) ind);
486 result = check_zone_nr(blknr + ind, &blk_chg);
488 write_block(block, (char *) ind);
492 block = check_zone_nr(inode->i_zone + 8, &changed);
493 read_block(block, (char *) dind);
495 result = check_zone_nr(dind + (blknr / 512), &blk_chg);
497 write_block(block, (char *) dind);
499 read_block(block, (char *) ind);
501 result = check_zone_nr(ind + (blknr % 512), &blk_chg);
503 write_block(block, (char *) ind);
507 #if ENABLE_FEATURE_MINIX2
508 static int map_block2(struct minix2_inode *inode, unsigned blknr)
510 uint32_t ind[BLOCK_SIZE >> 2];
511 uint32_t dind[BLOCK_SIZE >> 2];
512 uint32_t tind[BLOCK_SIZE >> 2];
517 return check_zone_nr2(inode->i_zone + blknr, &changed);
520 block = check_zone_nr2(inode->i_zone + 7, &changed);
521 read_block(block, (char *) ind);
523 result = check_zone_nr2(blknr + ind, &blk_chg);
525 write_block(block, (char *) ind);
529 if (blknr >= 256 * 256) {
530 block = check_zone_nr2(inode->i_zone + 8, &changed);
531 read_block(block, (char *) dind);
533 result = check_zone_nr2(dind + blknr / 256, &blk_chg);
535 write_block(block, (char *) dind);
537 read_block(block, (char *) ind);
539 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
541 write_block(block, (char *) ind);
545 block = check_zone_nr2(inode->i_zone + 9, &changed);
546 read_block(block, (char *) tind);
548 result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
550 write_block(block, (char *) tind);
552 read_block(block, (char *) dind);
554 result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
556 write_block(block, (char *) dind);
558 read_block(block, (char *) ind);
560 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
562 write_block(block, (char *) ind);
567 static void write_super_block(void)
570 * Set the state of the filesystem based on whether or not there
571 * are uncorrected errors. The filesystem valid flag is
572 * unconditionally set if we get this far.
574 Super.s_state |= MINIX_VALID_FS | MINIX_ERROR_FS;
575 if (!errors_uncorrected)
576 Super.s_state &= ~MINIX_ERROR_FS;
578 if (BLOCK_SIZE != lseek(dev_fd, BLOCK_SIZE, SEEK_SET))
579 die("seek failed in write_super_block");
580 if (BLOCK_SIZE != write(dev_fd, super_block_buffer, BLOCK_SIZE))
581 die("cannot write super-block");
584 static void write_tables(void)
588 if (IMAPS * BLOCK_SIZE != write(dev_fd, inode_map, IMAPS * BLOCK_SIZE))
589 die("cannot write inode map");
590 if (ZMAPS * BLOCK_SIZE != write(dev_fd, zone_map, ZMAPS * BLOCK_SIZE))
591 die("cannot write zone map");
592 if (INODE_BUFFER_SIZE != write(dev_fd, inode_buffer, INODE_BUFFER_SIZE))
593 die("cannot write inodes");
596 static void get_dirsize(void)
599 char blk[BLOCK_SIZE];
602 #if ENABLE_FEATURE_MINIX2
604 block = Inode2[MINIX_ROOT_INO].i_zone[0];
607 block = Inode1[MINIX_ROOT_INO].i_zone[0];
608 read_block(block, blk);
609 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
610 if (strcmp(blk + size + 2, "..") == 0) {
619 static void read_superblock(void)
621 if (BLOCK_SIZE != lseek(dev_fd, BLOCK_SIZE, SEEK_SET))
623 if (BLOCK_SIZE != read(dev_fd, super_block_buffer, BLOCK_SIZE))
624 die("cannot read super block");
625 /* already initialized to:
630 if (MAGIC == MINIX1_SUPER_MAGIC) {
631 } else if (MAGIC == MINIX1_SUPER_MAGIC2) {
634 #if ENABLE_FEATURE_MINIX2
635 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
637 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
643 die("bad magic number in super-block");
644 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
645 die("only 1k blocks/zones supported");
646 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
647 die("bad s_imap_blocks field in super-block");
648 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
649 die("bad s_zmap_blocks field in super-block");
652 static void read_tables(void)
654 inode_map = xzalloc(IMAPS * BLOCK_SIZE);
655 zone_map = xzalloc(ZMAPS * BLOCK_SIZE);
656 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
657 inode_count = xmalloc(INODES + 1);
658 zone_count = xmalloc(ZONES);
659 if (IMAPS * BLOCK_SIZE != read(dev_fd, inode_map, IMAPS * BLOCK_SIZE))
660 die("cannot read inode map");
661 if (ZMAPS * BLOCK_SIZE != read(dev_fd, zone_map, ZMAPS * BLOCK_SIZE))
662 die("cannot read zone map");
663 if (INODE_BUFFER_SIZE != read(dev_fd, inode_buffer, INODE_BUFFER_SIZE))
664 die("cannot read inodes");
665 if (NORM_FIRSTZONE != FIRSTZONE) {
666 printf("warning: firstzone!=norm_firstzone\n");
667 errors_uncorrected = 1;
673 "Firstdatazone=%u (%u)\n"
676 "Filesystem state=%u\n"
680 FIRSTZONE, NORM_FIRSTZONE,
681 BLOCK_SIZE << ZONESIZE,
688 static struct minix1_inode *get_inode(unsigned nr)
690 struct minix1_inode *inode;
692 if (!nr || nr > INODES)
696 if (!inode_count[nr]) {
697 if (!inode_in_use(nr)) {
698 printf("Inode %d is marked as 'unused', but it is used "
699 "for file '%s'\n", nr, current_name);
701 if (ask("Mark as 'in use'", 1))
704 errors_uncorrected = 1;
707 if (S_ISDIR(inode->i_mode))
709 else if (S_ISREG(inode->i_mode))
711 else if (S_ISCHR(inode->i_mode))
713 else if (S_ISBLK(inode->i_mode))
715 else if (S_ISLNK(inode->i_mode))
717 else if (S_ISSOCK(inode->i_mode));
718 else if (S_ISFIFO(inode->i_mode));
720 printf("%s has mode %05o\n", current_name, inode->i_mode);
725 if (!++inode_count[nr]) {
726 printf("Warning: inode count too big\n");
728 errors_uncorrected = 1;
733 #if ENABLE_FEATURE_MINIX2
734 static struct minix2_inode *get_inode2(unsigned nr)
736 struct minix2_inode *inode;
738 if (!nr || nr > INODES)
742 if (!inode_count[nr]) {
743 if (!inode_in_use(nr)) {
744 printf("Inode %d is marked as 'unused', but it is used "
745 "for file '%s'\n", nr, current_name);
747 if (ask("Mark as 'in use'", 1))
750 errors_uncorrected = 1;
753 if (S_ISDIR(inode->i_mode))
755 else if (S_ISREG(inode->i_mode))
757 else if (S_ISCHR(inode->i_mode))
759 else if (S_ISBLK(inode->i_mode))
761 else if (S_ISLNK(inode->i_mode))
763 else if (S_ISSOCK(inode->i_mode));
764 else if (S_ISFIFO(inode->i_mode));
766 printf("%s has mode %05o\n", current_name, inode->i_mode);
770 if (!++inode_count[nr]) {
771 printf("Warning: inode count too big\n");
773 errors_uncorrected = 1;
779 static void check_root(void)
781 struct minix1_inode *inode = Inode1 + MINIX_ROOT_INO;
783 if (!inode || !S_ISDIR(inode->i_mode))
784 die("root inode isn't a directory");
787 #if ENABLE_FEATURE_MINIX2
788 static void check_root2(void)
790 struct minix2_inode *inode = Inode2 + MINIX_ROOT_INO;
792 if (!inode || !S_ISDIR(inode->i_mode))
793 die("root inode isn't a directory");
796 void check_root2(void);
799 static int add_zone(uint16_t *znr, smallint *corrected)
805 block = check_zone_nr(znr, corrected);
808 if (zone_count[block]) {
809 printf("Already used block is reused in file '%s'. ",
811 if (ask("Clear", 1)) {
818 if (!zone_in_use(block)) {
819 printf("Block %d in file '%s' is marked as 'unused'. ",
820 block, current_name);
821 if (ask("Correct", 1))
824 if (!++zone_count[block])
829 #if ENABLE_FEATURE_MINIX2
830 static int add_zone2(uint32_t *znr, smallint *corrected)
836 block = check_zone_nr2(znr, corrected);
839 if (zone_count[block]) {
840 printf("Already used block is reused in file '%s'. ",
842 if (ask("Clear", 1)) {
849 if (!zone_in_use(block)) {
850 printf("Block %d in file '%s' is marked as 'unused'. ",
851 block, current_name);
852 if (ask("Correct", 1))
855 if (!++zone_count[block])
861 static void add_zone_ind(uint16_t *znr, smallint *corrected)
865 smallint chg_blk = 0;
867 block = add_zone(znr, corrected);
870 read_block(block, add_zone_ind_blk);
871 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
872 add_zone(i + (uint16_t *) add_zone_ind_blk, &chg_blk);
874 write_block(block, add_zone_ind_blk);
877 #if ENABLE_FEATURE_MINIX2
878 static void add_zone_ind2(uint32_t *znr, smallint *corrected)
882 smallint chg_blk = 0;
884 block = add_zone2(znr, corrected);
887 read_block(block, add_zone_ind_blk);
888 for (i = 0; i < BLOCK_SIZE >> 2; i++)
889 add_zone2(i + (uint32_t *) add_zone_ind_blk, &chg_blk);
891 write_block(block, add_zone_ind_blk);
895 static void add_zone_dind(uint16_t *znr, smallint *corrected)
899 smallint chg_blk = 0;
901 block = add_zone(znr, corrected);
904 read_block(block, add_zone_dind_blk);
905 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
906 add_zone_ind(i + (uint16_t *) add_zone_dind_blk, &chg_blk);
908 write_block(block, add_zone_dind_blk);
911 #if ENABLE_FEATURE_MINIX2
912 static void add_zone_dind2(uint32_t *znr, smallint *corrected)
916 smallint chg_blk = 0;
918 block = add_zone2(znr, corrected);
921 read_block(block, add_zone_dind_blk);
922 for (i = 0; i < BLOCK_SIZE >> 2; i++)
923 add_zone_ind2(i + (uint32_t *) add_zone_dind_blk, &chg_blk);
925 write_block(block, add_zone_dind_blk);
928 static void add_zone_tind2(uint32_t *znr, smallint *corrected)
932 smallint chg_blk = 0;
934 block = add_zone2(znr, corrected);
937 read_block(block, add_zone_tind_blk);
938 for (i = 0; i < BLOCK_SIZE >> 2; i++)
939 add_zone_dind2(i + (uint32_t *) add_zone_tind_blk, &chg_blk);
941 write_block(block, add_zone_tind_blk);
945 static void check_zones(unsigned i)
947 struct minix1_inode *inode;
949 if (!i || i > INODES)
951 if (inode_count[i] > 1) /* have we counted this file already? */
954 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
955 !S_ISLNK(inode->i_mode)) return;
956 for (i = 0; i < 7; i++)
957 add_zone(i + inode->i_zone, &changed);
958 add_zone_ind(7 + inode->i_zone, &changed);
959 add_zone_dind(8 + inode->i_zone, &changed);
962 #if ENABLE_FEATURE_MINIX2
963 static void check_zones2(unsigned i)
965 struct minix2_inode *inode;
967 if (!i || i > INODES)
969 if (inode_count[i] > 1) /* have we counted this file already? */
972 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
973 && !S_ISLNK(inode->i_mode))
975 for (i = 0; i < 7; i++)
976 add_zone2(i + inode->i_zone, &changed);
977 add_zone_ind2(7 + inode->i_zone, &changed);
978 add_zone_dind2(8 + inode->i_zone, &changed);
979 add_zone_tind2(9 + inode->i_zone, &changed);
983 static void check_file(struct minix1_inode *dir, unsigned offset)
985 struct minix1_inode *inode;
990 block = map_block(dir, offset / BLOCK_SIZE);
991 read_block(block, check_file_blk);
992 name = check_file_blk + (offset % BLOCK_SIZE) + 2;
993 ino = *(uint16_t *) (name - 2);
995 printf("%s contains a bad inode number for file '%.*s'. ",
996 current_name, namelen, name);
997 if (ask("Remove", 1)) {
998 *(uint16_t *) (name - 2) = 0;
999 write_block(block, check_file_blk);
1003 push_filename(name);
1004 inode = get_inode(ino);
1007 if (inode && LONE_CHAR(name, '.'))
1009 printf("%s: bad directory: '.' isn't first\n", current_name);
1010 errors_uncorrected = 1;
1012 if (offset == dirsize) {
1013 if (inode && strcmp("..", name) == 0)
1015 printf("%s: bad directory: '..' isn't second\n", current_name);
1016 errors_uncorrected = 1;
1020 push_filename(name);
1023 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1024 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
1027 if (inode && S_ISDIR(inode->i_mode))
1028 recursive_check(ino);
1032 #if ENABLE_FEATURE_MINIX2
1033 static void check_file2(struct minix2_inode *dir, unsigned offset)
1035 struct minix2_inode *inode;
1040 block = map_block2(dir, offset / BLOCK_SIZE);
1041 read_block(block, check_file_blk);
1042 name = check_file_blk + (offset % BLOCK_SIZE) + 2;
1043 ino = *(uint16_t *) (name - 2);
1045 printf("%s contains a bad inode number for file '%.*s'. ",
1046 current_name, namelen, name);
1047 if (ask("Remove", 1)) {
1048 *(uint16_t *) (name - 2) = 0;
1049 write_block(block, check_file_blk);
1053 push_filename(name);
1054 inode = get_inode2(ino);
1057 if (inode && LONE_CHAR(name, '.'))
1059 printf("%s: bad directory: '.' isn't first\n", current_name);
1060 errors_uncorrected = 1;
1062 if (offset == dirsize) {
1063 if (inode && strcmp("..", name) == 0)
1065 printf("%s: bad directory: '..' isn't second\n", current_name);
1066 errors_uncorrected = 1;
1070 push_filename(name);
1073 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1074 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
1077 if (inode && S_ISDIR(inode->i_mode))
1078 recursive_check2(ino);
1083 static void recursive_check(unsigned ino)
1085 struct minix1_inode *dir;
1089 if (!S_ISDIR(dir->i_mode))
1090 die("internal error");
1091 if (dir->i_size < 2 * dirsize) {
1092 printf("%s: bad directory: size<32", current_name);
1093 errors_uncorrected = 1;
1095 for (offset = 0; offset < dir->i_size; offset += dirsize)
1096 check_file(dir, offset);
1099 #if ENABLE_FEATURE_MINIX2
1100 static void recursive_check2(unsigned ino)
1102 struct minix2_inode *dir;
1106 if (!S_ISDIR(dir->i_mode))
1107 die("internal error");
1108 if (dir->i_size < 2 * dirsize) {
1109 printf("%s: bad directory: size<32", current_name);
1110 errors_uncorrected = 1;
1112 for (offset = 0; offset < dir->i_size; offset += dirsize)
1113 check_file2(dir, offset);
1117 static int bad_zone(int i)
1119 char buffer[BLOCK_SIZE];
1121 if (BLOCK_SIZE * i != lseek(dev_fd, BLOCK_SIZE * i, SEEK_SET))
1122 die("seek failed in bad_zone");
1123 return (BLOCK_SIZE != read(dev_fd, buffer, BLOCK_SIZE));
1126 static void check_counts(void)
1130 for (i = 1; i <= INODES; i++) {
1131 if (warn_mode && Inode1[i].i_mode && !inode_in_use(i)) {
1132 printf("Inode %d has non-zero mode. ", i);
1133 if (ask("Clear", 1)) {
1134 Inode1[i].i_mode = 0;
1138 if (!inode_count[i]) {
1139 if (!inode_in_use(i))
1141 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
1142 if (ask("Clear", 1))
1146 if (!inode_in_use(i)) {
1147 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
1151 if (Inode1[i].i_nlinks != inode_count[i]) {
1152 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
1153 i, Inode1[i].i_mode, Inode1[i].i_nlinks,
1155 if (ask("Set i_nlinks to count", 1)) {
1156 Inode1[i].i_nlinks = inode_count[i];
1161 for (i = FIRSTZONE; i < ZONES; i++) {
1162 if ((zone_in_use(i) != 0) == zone_count[i])
1164 if (!zone_count[i]) {
1167 printf("Zone %d is marked 'in use', but no file uses it. ", i);
1168 if (ask("Unmark", 1))
1172 printf("Zone %d: %sin use, counted=%d\n",
1173 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1177 #if ENABLE_FEATURE_MINIX2
1178 static void check_counts2(void)
1182 for (i = 1; i <= INODES; i++) {
1183 if (warn_mode && Inode2[i].i_mode && !inode_in_use(i)) {
1184 printf("Inode %d has non-zero mode. ", i);
1185 if (ask("Clear", 1)) {
1186 Inode2[i].i_mode = 0;
1190 if (!inode_count[i]) {
1191 if (!inode_in_use(i))
1193 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
1194 if (ask("Clear", 1))
1198 if (!inode_in_use(i)) {
1199 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
1203 if (Inode2[i].i_nlinks != inode_count[i]) {
1204 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
1205 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1207 if (ask("Set i_nlinks to count", 1)) {
1208 Inode2[i].i_nlinks = inode_count[i];
1213 for (i = FIRSTZONE; i < ZONES; i++) {
1214 if ((zone_in_use(i) != 0) == zone_count[i])
1216 if (!zone_count[i]) {
1219 printf("Zone %d is marked 'in use', but no file uses it. ", i);
1220 if (ask("Unmark", 1))
1224 printf("Zone %d: %sin use, counted=%d\n",
1225 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1230 static void check(void)
1232 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1233 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1234 check_zones(MINIX_ROOT_INO);
1235 recursive_check(MINIX_ROOT_INO);
1239 #if ENABLE_FEATURE_MINIX2
1240 static void check2(void)
1242 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1243 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1244 check_zones2(MINIX_ROOT_INO);
1245 recursive_check2(MINIX_ROOT_INO);
1252 int fsck_minix_main(int argc, char **argv);
1253 int fsck_minix_main(int argc, char **argv)
1258 xfunc_error_retval = 8;
1262 if (INODE_SIZE1 * MINIX1_INODES_PER_BLOCK != BLOCK_SIZE)
1263 die("bad inode size");
1264 #if ENABLE_FEATURE_MINIX2
1265 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
1266 die("bad v2 inode size");
1268 while (--argc != 0) {
1270 if (argv[0][0] != '-') {
1273 device_name = argv[0];
1275 while (*++argv[0]) {
1276 switch (argv[0][0]) {
1309 check_mount(); /* trying to check a mounted filesystem? */
1310 if (repair && !automatic) {
1311 if (!isatty(0) || !isatty(1))
1312 die("need terminal for interactive repairs");
1314 dev_fd = xopen(device_name, repair ? O_RDWR : O_RDONLY);
1316 /*sync(); paranoia? */
1320 * Determine whether or not we should continue with the checking.
1321 * This is based on the status of the filesystem valid and error
1322 * flags and whether or not the -f switch was specified on the
1325 printf("%s: %s\n", applet_name, bb_banner);
1327 if (!(Super.s_state & MINIX_ERROR_FS)
1328 && (Super.s_state & MINIX_VALID_FS) && !force
1331 printf("%s is clean, check is skipped\n", device_name);
1334 printf("Forcing filesystem check on %s\n", device_name);
1336 printf("Filesystem on %s is dirty, needs checking\n",
1341 if (repair && !automatic) {
1342 tcgetattr(0, &sv_termios);
1344 tmp.c_lflag &= ~(ICANON | ECHO);
1345 tcsetattr(0, TCSANOW, &tmp);
1360 for (i = 1, free_cnt = 0; i <= INODES; i++)
1361 if (!inode_in_use(i))
1363 printf("\n%6u inodes used (%u%%)\n", (INODES - free_cnt),
1364 100 * (INODES - free_cnt) / INODES);
1365 for (i = FIRSTZONE, free_cnt = 0; i < ZONES; i++)
1366 if (!zone_in_use(i))
1368 printf("%6u zones used (%u%%)\n\n"
1369 "%6u regular files\n"
1371 "%6u character device files\n"
1372 "%6u block device files\n"
1374 "%6u symbolic links\n"
1377 (ZONES - free_cnt), 100 * (ZONES - free_cnt) / ZONES,
1378 regular, directory, chardev, blockdev,
1379 links - 2 * directory + 1, symlinks,
1380 total - 2 * directory + 1);
1384 printf("FILE SYSTEM HAS BEEN CHANGED\n");
1387 write_super_block();
1389 if (repair && !automatic)
1390 tcsetattr(0, TCSANOW, &sv_termios);
1394 if (errors_uncorrected)