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 * superblock 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 Mickiewicz <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 superblock 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 */
98 struct BUG_bad_inode_size {
99 char BUG_bad_inode1_size[(INODE_SIZE1 * MINIX1_INODES_PER_BLOCK != BLOCK_SIZE) ? -1 : 1];
100 #if ENABLE_FEATURE_MINIX2
101 char BUG_bad_inode2_size[(INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE) ? -1 : 1];
107 MINIX1_LINK_MAX = 250,
108 MINIX2_LINK_MAX = 65530,
109 MINIX_I_MAP_SLOTS = 8,
110 MINIX_Z_MAP_SLOTS = 64,
111 MINIX_V1 = 0x0001, /* original minix fs */
112 MINIX_V2 = 0x0002, /* minix V2 fs */
114 MINIX_NAME_MAX = 255, /* # chars in a file name */
118 #if !ENABLE_FEATURE_MINIX2
119 enum { version2 = 0 };
122 enum { MAX_DEPTH = 32 };
127 #if ENABLE_FEATURE_MINIX2
130 smallint changed; /* is filesystem modified? */
131 smallint errors_uncorrected; /* flag if some error was not corrected */
132 smallint termios_set;
135 const char *device_name;
136 int directory, regular, blockdev, chardev, links, symlinks, total;
142 unsigned char *inode_count;
143 unsigned char *zone_count;
147 char *name_component[MAX_DEPTH+1];
150 struct termios sv_termios;
151 char superblock_buffer[BLOCK_SIZE];
152 char add_zone_ind_blk[BLOCK_SIZE];
153 char add_zone_dind_blk[BLOCK_SIZE];
154 USE_FEATURE_MINIX2(char add_zone_tind_blk[BLOCK_SIZE];)
155 char check_file_blk[BLOCK_SIZE];
158 char current_name[MAX_DEPTH * MINIX_NAME_MAX];
161 #define G (*ptr_to_globals)
162 #if ENABLE_FEATURE_MINIX2
163 #define version2 (G.version2 )
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 superblock_buffer (G.superblock_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 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
196 current_name[0] = '/'; \
197 /*current_name[1] = '\0';*/ \
198 name_component[0] = ¤t_name[0]; \
202 #define OPTION_STR "larvsmf"
212 #define OPT_list (option_mask32 & OPT_l)
213 #define OPT_automatic (option_mask32 & OPT_a)
214 #define OPT_repair (option_mask32 & OPT_r)
215 #define OPT_verbose (option_mask32 & OPT_v)
216 #define OPT_show (option_mask32 & OPT_s)
217 #define OPT_warn_mode (option_mask32 & OPT_w)
218 #define OPT_force (option_mask32 & OPT_f)
219 /* non-automatic repairs requested? */
220 #define OPT_manual ((option_mask32 & (OPT_a|OPT_r)) == OPT_r)
223 #define Inode1 (((struct minix1_inode *) inode_buffer)-1)
224 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
226 #define Super (*(struct minix_superblock *)(superblock_buffer))
228 #if ENABLE_FEATURE_MINIX2
229 # define ZONES ((unsigned)(version2 ? Super.s_zones : Super.s_nzones))
231 # define ZONES ((unsigned)(Super.s_nzones))
233 #define INODES ((unsigned)Super.s_ninodes)
234 #define IMAPS ((unsigned)Super.s_imap_blocks)
235 #define ZMAPS ((unsigned)Super.s_zmap_blocks)
236 #define FIRSTZONE ((unsigned)Super.s_firstdatazone)
237 #define ZONESIZE ((unsigned)Super.s_log_zone_size)
238 #define MAXSIZE ((unsigned)Super.s_max_size)
239 #define MAGIC (Super.s_magic)
241 /* gcc likes this more (code is smaller) than macro variant */
242 static ALWAYS_INLINE unsigned div_roundup(unsigned size, unsigned n)
244 return (size + n-1) / n;
247 #if !ENABLE_FEATURE_MINIX2
248 #define INODE_BLOCKS div_roundup(INODES, MINIX1_INODES_PER_BLOCK)
250 #define INODE_BLOCKS div_roundup(INODES, \
251 (version2 ? MINIX2_INODES_PER_BLOCK : MINIX1_INODES_PER_BLOCK))
254 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
255 #define NORM_FIRSTZONE (2 + IMAPS + ZMAPS + INODE_BLOCKS)
257 /* Before you ask "where they come from?": */
258 /* setbit/clrbit are supplied by sys/param.h */
260 static int minix_bit(const char *a, unsigned i)
262 return (a[i >> 3] & (1<<(i & 7)));
265 static void minix_setbit(char *a, unsigned i)
270 static void minix_clrbit(char *a, unsigned i)
276 /* Note: do not assume 0/1, it is 0/nonzero */
277 #define zone_in_use(x) (minix_bit(zone_map,(x)-FIRSTZONE+1))
278 #define inode_in_use(x) (minix_bit(inode_map,(x)))
280 #define mark_inode(x) (minix_setbit(inode_map,(x)))
281 #define unmark_inode(x) (minix_clrbit(inode_map,(x)))
283 #define mark_zone(x) (minix_setbit(zone_map,(x)-FIRSTZONE+1))
284 #define unmark_zone(x) (minix_clrbit(zone_map,(x)-FIRSTZONE+1))
287 static void recursive_check(unsigned ino);
288 #if ENABLE_FEATURE_MINIX2
289 static void recursive_check2(unsigned ino);
292 static void die(const char *str) NORETURN;
293 static void die(const char *str)
296 tcsetattr(0, TCSANOW, &sv_termios);
297 bb_error_msg_and_die("%s", str);
300 static void push_filename(const char *name)
304 // [0] [1] [2] <-name_component[i]
305 if (name_depth < MAX_DEPTH) {
307 char *p = name_component[name_depth];
309 len = sprintf(p, "%.*s", namelen, name);
310 name_component[name_depth + 1] = p + len;
315 static void pop_filename(void)
318 if (name_depth < MAX_DEPTH) {
319 *name_component[name_depth] = '\0';
321 current_name[0] = '/';
322 current_name[1] = '\0';
327 static int ask(const char *string, int def)
333 errors_uncorrected = 1;
339 errors_uncorrected = 1;
342 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
348 errors_uncorrected = 1;
355 } else if (c == 'N') {
358 } else if (c == ' ' || c == '\n')
365 errors_uncorrected = 1;
371 * Make certain that we aren't checking a filesystem that is on a
372 * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
373 * 1994 Theodore Ts'o. Also licensed under GPL.
375 static void check_mount(void)
381 //XXX:FIXME use find_mount_point()
382 f = setmntent(MOUNTED, "r");
385 while ((mnt = getmntent(f)) != NULL)
386 if (strcmp(device_name, mnt->mnt_fsname) == 0)
393 * If the root is mounted read-only, then /etc/mtab is
394 * probably not correct; so we won't issue a warning based on
397 fd = open(MOUNTED, O_RDWR);
398 if (fd < 0 && errno == EROFS)
402 printf("%s is mounted. ", device_name);
404 if (isatty(0) && isatty(1))
405 cont = ask("Do you really want to continue", 0);
407 printf("Check aborted\n");
413 * check_zone_nr checks to see that *nr is a valid zone nr. If it
414 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
415 * if an error was corrected, and returns the zone (0 for no zone
416 * or a bad zone-number).
418 static int check_zone_nr2(uint32_t *nr, smallint *corrected)
425 else if (*nr >= ZONES)
429 printf("Zone nr %s in file '%s'. ", msg, current_name);
430 if (ask("Remove block", 1)) {
437 static int check_zone_nr(uint16_t *nr, smallint *corrected)
440 int r = check_zone_nr2(&nr32, corrected);
441 *nr = (uint16_t)nr32;
446 * read-block reads block nr into the buffer at addr.
448 static void read_block(unsigned nr, void *addr)
451 memset(addr, 0, BLOCK_SIZE);
454 xlseek(dev_fd, BLOCK_SIZE * nr, SEEK_SET);
455 if (BLOCK_SIZE != full_read(dev_fd, addr, BLOCK_SIZE)) {
456 printf("%s: bad block %u in file '%s'\n",
457 bb_msg_read_error, nr, current_name);
458 errors_uncorrected = 1;
459 memset(addr, 0, BLOCK_SIZE);
464 * write_block writes block nr to disk.
466 static void write_block(unsigned nr, void *addr)
470 if (nr < FIRSTZONE || nr >= ZONES) {
471 printf("Internal error: trying to write bad block\n"
472 "Write request ignored\n");
473 errors_uncorrected = 1;
476 xlseek(dev_fd, BLOCK_SIZE * nr, SEEK_SET);
477 if (BLOCK_SIZE != full_write(dev_fd, addr, BLOCK_SIZE)) {
478 printf("%s: bad block %u in file '%s'\n",
479 bb_msg_write_error, nr, current_name);
480 errors_uncorrected = 1;
485 * map_block calculates the absolute block nr of a block in a file.
486 * It sets 'changed' if the inode has needed changing, and re-writes
487 * any indirect blocks with errors.
489 static int map_block(struct minix1_inode *inode, unsigned blknr)
491 uint16_t ind[BLOCK_SIZE >> 1];
496 return check_zone_nr(inode->i_zone + blknr, &changed);
499 block = check_zone_nr(inode->i_zone + 7, &changed);
503 block = check_zone_nr(inode->i_zone + 8, &changed);
504 read_block(block, ind); /* double indirect */
506 result = check_zone_nr(&ind[blknr / 512], &blk_chg);
508 write_block(block, ind);
511 read_block(block, ind);
513 result = check_zone_nr(&ind[blknr % 512], &blk_chg);
515 write_block(block, ind);
519 #if ENABLE_FEATURE_MINIX2
520 static int map_block2(struct minix2_inode *inode, unsigned blknr)
522 uint32_t ind[BLOCK_SIZE >> 2];
527 return check_zone_nr2(inode->i_zone + blknr, &changed);
530 block = check_zone_nr2(inode->i_zone + 7, &changed);
534 if (blknr < 256 * 256) {
535 block = check_zone_nr2(inode->i_zone + 8, &changed);
539 block = check_zone_nr2(inode->i_zone + 9, &changed);
540 read_block(block, ind); /* triple indirect */
542 result = check_zone_nr2(&ind[blknr / (256 * 256)], &blk_chg);
544 write_block(block, ind);
547 read_block(block, ind); /* double indirect */
549 result = check_zone_nr2(&ind[(blknr / 256) % 256], &blk_chg);
551 write_block(block, ind);
554 read_block(block, ind);
556 result = check_zone_nr2(&ind[blknr % 256], &blk_chg);
558 write_block(block, ind);
563 static void write_superblock(void)
566 * Set the state of the filesystem based on whether or not there
567 * are uncorrected errors. The filesystem valid flag is
568 * unconditionally set if we get this far.
570 Super.s_state |= MINIX_VALID_FS | MINIX_ERROR_FS;
571 if (!errors_uncorrected)
572 Super.s_state &= ~MINIX_ERROR_FS;
574 xlseek(dev_fd, BLOCK_SIZE, SEEK_SET);
575 if (BLOCK_SIZE != full_write(dev_fd, superblock_buffer, BLOCK_SIZE))
576 die("cannot write superblock");
579 static void write_tables(void)
583 if (IMAPS * BLOCK_SIZE != write(dev_fd, inode_map, IMAPS * BLOCK_SIZE))
584 die("cannot write inode map");
585 if (ZMAPS * BLOCK_SIZE != write(dev_fd, zone_map, ZMAPS * BLOCK_SIZE))
586 die("cannot write zone map");
587 if (INODE_BUFFER_SIZE != write(dev_fd, inode_buffer, INODE_BUFFER_SIZE))
588 die("cannot write inodes");
591 static void get_dirsize(void)
594 char blk[BLOCK_SIZE];
597 #if ENABLE_FEATURE_MINIX2
599 block = Inode2[MINIX_ROOT_INO].i_zone[0];
602 block = Inode1[MINIX_ROOT_INO].i_zone[0];
603 read_block(block, blk);
604 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
605 if (strcmp(blk + size + 2, "..") == 0) {
614 static void read_superblock(void)
616 xlseek(dev_fd, BLOCK_SIZE, SEEK_SET);
617 if (BLOCK_SIZE != full_read(dev_fd, superblock_buffer, BLOCK_SIZE))
618 die("cannot read superblock");
619 /* already initialized to:
624 if (MAGIC == MINIX1_SUPER_MAGIC) {
625 } else if (MAGIC == MINIX1_SUPER_MAGIC2) {
628 #if ENABLE_FEATURE_MINIX2
629 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
631 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
637 die("bad magic number in superblock");
638 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
639 die("only 1k blocks/zones supported");
640 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
641 die("bad s_imap_blocks field in superblock");
642 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
643 die("bad s_zmap_blocks field in superblock");
646 static void read_tables(void)
648 inode_map = xzalloc(IMAPS * BLOCK_SIZE);
649 zone_map = xzalloc(ZMAPS * BLOCK_SIZE);
650 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
651 inode_count = xmalloc(INODES + 1);
652 zone_count = xmalloc(ZONES);
653 if (IMAPS * BLOCK_SIZE != read(dev_fd, inode_map, IMAPS * BLOCK_SIZE))
654 die("cannot read inode map");
655 if (ZMAPS * BLOCK_SIZE != read(dev_fd, zone_map, ZMAPS * BLOCK_SIZE))
656 die("cannot read zone map");
657 if (INODE_BUFFER_SIZE != read(dev_fd, inode_buffer, INODE_BUFFER_SIZE))
658 die("cannot read inodes");
659 if (NORM_FIRSTZONE != FIRSTZONE) {
660 printf("warning: firstzone!=norm_firstzone\n");
661 errors_uncorrected = 1;
667 "Firstdatazone=%u (%u)\n"
670 "Filesystem state=%u\n"
674 FIRSTZONE, NORM_FIRSTZONE,
675 BLOCK_SIZE << ZONESIZE,
682 static void get_inode_common(unsigned nr, uint16_t i_mode)
685 if (!inode_count[nr]) {
686 if (!inode_in_use(nr)) {
687 printf("Inode %d is marked as 'unused', but it is used "
688 "for file '%s'\n", nr, current_name);
690 if (ask("Mark as 'in use'", 1))
693 errors_uncorrected = 1;
698 else if (S_ISREG(i_mode))
700 else if (S_ISCHR(i_mode))
702 else if (S_ISBLK(i_mode))
704 else if (S_ISLNK(i_mode))
706 else if (S_ISSOCK(i_mode));
707 else if (S_ISFIFO(i_mode));
709 printf("%s has mode %05o\n", current_name, i_mode);
713 if (!++inode_count[nr]) {
714 printf("Warning: inode count too big\n");
716 errors_uncorrected = 1;
720 static struct minix1_inode *get_inode(unsigned nr)
722 struct minix1_inode *inode;
724 if (!nr || nr > INODES)
727 get_inode_common(nr, inode->i_mode);
731 #if ENABLE_FEATURE_MINIX2
732 static struct minix2_inode *get_inode2(unsigned nr)
734 struct minix2_inode *inode;
736 if (!nr || nr > INODES)
739 get_inode_common(nr, inode->i_mode);
744 static void check_root(void)
746 struct minix1_inode *inode = Inode1 + MINIX_ROOT_INO;
748 if (!inode || !S_ISDIR(inode->i_mode))
749 die("root inode isn't a directory");
752 #if ENABLE_FEATURE_MINIX2
753 static void check_root2(void)
755 struct minix2_inode *inode = Inode2 + MINIX_ROOT_INO;
757 if (!inode || !S_ISDIR(inode->i_mode))
758 die("root inode isn't a directory");
761 void check_root2(void);
764 static int add_zone_common(int block, smallint *corrected)
768 if (zone_count[block]) {
769 printf("Already used block is reused in file '%s'. ",
771 if (ask("Clear", 1)) {
774 return -1; /* "please zero out *znr" */
777 if (!zone_in_use(block)) {
778 printf("Block %d in file '%s' is marked as 'unused'. ",
779 block, current_name);
780 if (ask("Correct", 1))
783 if (!++zone_count[block])
788 static int add_zone(uint16_t *znr, smallint *corrected)
792 block = check_zone_nr(znr, corrected);
793 block = add_zone_common(block, corrected);
801 #if ENABLE_FEATURE_MINIX2
802 static int add_zone2(uint32_t *znr, smallint *corrected)
806 block = check_zone_nr2(znr, corrected);
807 block = add_zone_common(block, corrected);
816 static void add_zone_ind(uint16_t *znr, smallint *corrected)
820 smallint chg_blk = 0;
822 block = add_zone(znr, corrected);
825 read_block(block, add_zone_ind_blk);
826 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
827 add_zone(i + (uint16_t *) add_zone_ind_blk, &chg_blk);
829 write_block(block, add_zone_ind_blk);
832 #if ENABLE_FEATURE_MINIX2
833 static void add_zone_ind2(uint32_t *znr, smallint *corrected)
837 smallint chg_blk = 0;
839 block = add_zone2(znr, corrected);
842 read_block(block, add_zone_ind_blk);
843 for (i = 0; i < BLOCK_SIZE >> 2; i++)
844 add_zone2(i + (uint32_t *) add_zone_ind_blk, &chg_blk);
846 write_block(block, add_zone_ind_blk);
850 static void add_zone_dind(uint16_t *znr, smallint *corrected)
854 smallint chg_blk = 0;
856 block = add_zone(znr, corrected);
859 read_block(block, add_zone_dind_blk);
860 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
861 add_zone_ind(i + (uint16_t *) add_zone_dind_blk, &chg_blk);
863 write_block(block, add_zone_dind_blk);
866 #if ENABLE_FEATURE_MINIX2
867 static void add_zone_dind2(uint32_t *znr, smallint *corrected)
871 smallint chg_blk = 0;
873 block = add_zone2(znr, corrected);
876 read_block(block, add_zone_dind_blk);
877 for (i = 0; i < BLOCK_SIZE >> 2; i++)
878 add_zone_ind2(i + (uint32_t *) add_zone_dind_blk, &chg_blk);
880 write_block(block, add_zone_dind_blk);
883 static void add_zone_tind2(uint32_t *znr, smallint *corrected)
887 smallint chg_blk = 0;
889 block = add_zone2(znr, corrected);
892 read_block(block, add_zone_tind_blk);
893 for (i = 0; i < BLOCK_SIZE >> 2; i++)
894 add_zone_dind2(i + (uint32_t *) add_zone_tind_blk, &chg_blk);
896 write_block(block, add_zone_tind_blk);
900 static void check_zones(unsigned i)
902 struct minix1_inode *inode;
904 if (!i || i > INODES)
906 if (inode_count[i] > 1) /* have we counted this file already? */
909 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
910 !S_ISLNK(inode->i_mode)) return;
911 for (i = 0; i < 7; i++)
912 add_zone(i + inode->i_zone, &changed);
913 add_zone_ind(7 + inode->i_zone, &changed);
914 add_zone_dind(8 + inode->i_zone, &changed);
917 #if ENABLE_FEATURE_MINIX2
918 static void check_zones2(unsigned i)
920 struct minix2_inode *inode;
922 if (!i || i > INODES)
924 if (inode_count[i] > 1) /* have we counted this file already? */
927 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
928 && !S_ISLNK(inode->i_mode))
930 for (i = 0; i < 7; i++)
931 add_zone2(i + inode->i_zone, &changed);
932 add_zone_ind2(7 + inode->i_zone, &changed);
933 add_zone_dind2(8 + inode->i_zone, &changed);
934 add_zone_tind2(9 + inode->i_zone, &changed);
938 static void check_file(struct minix1_inode *dir, unsigned offset)
940 struct minix1_inode *inode;
945 block = map_block(dir, offset / BLOCK_SIZE);
946 read_block(block, check_file_blk);
947 name = check_file_blk + (offset % BLOCK_SIZE) + 2;
948 ino = *(uint16_t *) (name - 2);
950 printf("%s contains a bad inode number for file '%.*s'. ",
951 current_name, namelen, name);
952 if (ask("Remove", 1)) {
953 *(uint16_t *) (name - 2) = 0;
954 write_block(block, check_file_blk);
959 inode = get_inode(ino);
962 if (inode && LONE_CHAR(name, '.'))
964 printf("%s: bad directory: '.' isn't first\n", current_name);
965 errors_uncorrected = 1;
967 if (offset == dirsize) {
968 if (inode && strcmp("..", name) == 0)
970 printf("%s: bad directory: '..' isn't second\n", current_name);
971 errors_uncorrected = 1;
978 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
979 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
982 if (inode && S_ISDIR(inode->i_mode))
983 recursive_check(ino);
987 #if ENABLE_FEATURE_MINIX2
988 static void check_file2(struct minix2_inode *dir, unsigned offset)
990 struct minix2_inode *inode;
995 block = map_block2(dir, offset / BLOCK_SIZE);
996 read_block(block, check_file_blk);
997 name = check_file_blk + (offset % BLOCK_SIZE) + 2;
998 ino = *(uint16_t *) (name - 2);
1000 printf("%s contains a bad inode number for file '%.*s'. ",
1001 current_name, namelen, name);
1002 if (ask("Remove", 1)) {
1003 *(uint16_t *) (name - 2) = 0;
1004 write_block(block, check_file_blk);
1008 push_filename(name);
1009 inode = get_inode2(ino);
1012 if (inode && LONE_CHAR(name, '.'))
1014 printf("%s: bad directory: '.' isn't first\n", current_name);
1015 errors_uncorrected = 1;
1017 if (offset == dirsize) {
1018 if (inode && strcmp("..", name) == 0)
1020 printf("%s: bad directory: '..' isn't second\n", current_name);
1021 errors_uncorrected = 1;
1025 push_filename(name);
1028 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1029 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
1032 if (inode && S_ISDIR(inode->i_mode))
1033 recursive_check2(ino);
1038 static void recursive_check(unsigned ino)
1040 struct minix1_inode *dir;
1044 if (!S_ISDIR(dir->i_mode))
1045 die("internal error");
1046 if (dir->i_size < 2 * dirsize) {
1047 printf("%s: bad directory: size<32", current_name);
1048 errors_uncorrected = 1;
1050 for (offset = 0; offset < dir->i_size; offset += dirsize)
1051 check_file(dir, offset);
1054 #if ENABLE_FEATURE_MINIX2
1055 static void recursive_check2(unsigned ino)
1057 struct minix2_inode *dir;
1061 if (!S_ISDIR(dir->i_mode))
1062 die("internal error");
1063 if (dir->i_size < 2 * dirsize) {
1064 printf("%s: bad directory: size<32", current_name);
1065 errors_uncorrected = 1;
1067 for (offset = 0; offset < dir->i_size; offset += dirsize)
1068 check_file2(dir, offset);
1072 static int bad_zone(int i)
1074 char buffer[BLOCK_SIZE];
1076 xlseek(dev_fd, BLOCK_SIZE * i, SEEK_SET);
1077 return (BLOCK_SIZE != full_read(dev_fd, buffer, BLOCK_SIZE));
1080 static void check_counts(void)
1084 for (i = 1; i <= INODES; i++) {
1085 if (OPT_warn_mode && Inode1[i].i_mode && !inode_in_use(i)) {
1086 printf("Inode %d has non-zero mode. ", i);
1087 if (ask("Clear", 1)) {
1088 Inode1[i].i_mode = 0;
1092 if (!inode_count[i]) {
1093 if (!inode_in_use(i))
1095 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
1096 if (ask("Clear", 1))
1100 if (!inode_in_use(i)) {
1101 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
1105 if (Inode1[i].i_nlinks != inode_count[i]) {
1106 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
1107 i, Inode1[i].i_mode, Inode1[i].i_nlinks,
1109 if (ask("Set i_nlinks to count", 1)) {
1110 Inode1[i].i_nlinks = inode_count[i];
1115 for (i = FIRSTZONE; i < ZONES; i++) {
1116 if ((zone_in_use(i) != 0) == zone_count[i])
1118 if (!zone_count[i]) {
1121 printf("Zone %d is marked 'in use', but no file uses it. ", i);
1122 if (ask("Unmark", 1))
1126 printf("Zone %d: %sin use, counted=%d\n",
1127 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1131 #if ENABLE_FEATURE_MINIX2
1132 static void check_counts2(void)
1136 for (i = 1; i <= INODES; i++) {
1137 if (OPT_warn_mode && Inode2[i].i_mode && !inode_in_use(i)) {
1138 printf("Inode %d has non-zero mode. ", i);
1139 if (ask("Clear", 1)) {
1140 Inode2[i].i_mode = 0;
1144 if (!inode_count[i]) {
1145 if (!inode_in_use(i))
1147 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
1148 if (ask("Clear", 1))
1152 if (!inode_in_use(i)) {
1153 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
1157 if (Inode2[i].i_nlinks != inode_count[i]) {
1158 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
1159 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1161 if (ask("Set i_nlinks to count", 1)) {
1162 Inode2[i].i_nlinks = inode_count[i];
1167 for (i = FIRSTZONE; i < ZONES; i++) {
1168 if ((zone_in_use(i) != 0) == zone_count[i])
1170 if (!zone_count[i]) {
1173 printf("Zone %d is marked 'in use', but no file uses it. ", i);
1174 if (ask("Unmark", 1))
1178 printf("Zone %d: %sin use, counted=%d\n",
1179 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1184 static void check(void)
1186 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1187 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1188 check_zones(MINIX_ROOT_INO);
1189 recursive_check(MINIX_ROOT_INO);
1193 #if ENABLE_FEATURE_MINIX2
1194 static void check2(void)
1196 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1197 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1198 check_zones2(MINIX_ROOT_INO);
1199 recursive_check2(MINIX_ROOT_INO);
1206 int fsck_minix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1207 int fsck_minix_main(int argc UNUSED_PARAM, char **argv)
1212 xfunc_error_retval = 8;
1216 opt_complementary = "=1:ar"; /* one argument; -a assumes -r */
1217 getopt32(argv, OPTION_STR);
1219 device_name = argv[0];
1221 check_mount(); /* trying to check a mounted filesystem? */
1223 if (!isatty(0) || !isatty(1))
1224 die("need terminal for interactive repairs");
1226 xmove_fd(xopen(device_name, OPT_repair ? O_RDWR : O_RDONLY), dev_fd);
1228 /*sync(); paranoia? */
1232 * Determine whether or not we should continue with the checking.
1233 * This is based on the status of the filesystem valid and error
1234 * flags and whether or not the -f switch was specified on the
1237 printf("%s: %s\n", applet_name, bb_banner);
1239 if (!(Super.s_state & MINIX_ERROR_FS)
1240 && (Super.s_state & MINIX_VALID_FS) && !OPT_force
1243 printf("%s is clean, check is skipped\n", device_name);
1245 } else if (OPT_force)
1246 printf("Forcing filesystem check on %s\n", device_name);
1247 else if (OPT_repair)
1248 printf("Filesystem on %s is dirty, needs checking\n",
1254 tcgetattr(0, &sv_termios);
1256 tmp.c_lflag &= ~(ICANON | ECHO);
1257 tcsetattr(0, TCSANOW, &tmp);
1272 for (i = 1, free_cnt = 0; i <= INODES; i++)
1273 if (!inode_in_use(i))
1275 printf("\n%6u inodes used (%u%%)\n", (INODES - free_cnt),
1276 100 * (INODES - free_cnt) / INODES);
1277 for (i = FIRSTZONE, free_cnt = 0; i < ZONES; i++)
1278 if (!zone_in_use(i))
1280 printf("%6u zones used (%u%%)\n\n"
1281 "%6u regular files\n"
1283 "%6u character device files\n"
1284 "%6u block device files\n"
1286 "%6u symbolic links\n"
1289 (ZONES - free_cnt), 100 * (ZONES - free_cnt) / ZONES,
1290 regular, directory, chardev, blockdev,
1291 links - 2 * directory + 1, symlinks,
1292 total - 2 * directory + 1);
1296 printf("FILE SYSTEM HAS BEEN CHANGED\n");
1298 } else if (OPT_repair)
1302 tcsetattr(0, TCSANOW, &sv_termios);
1306 if (errors_uncorrected)