1 /* vi: set sw=4 ts=4: */
3 * fsck.c - a file system consistency checker for Linux.
5 * (C) 1991, 1992 Linus Torvalds. This file may be redistributed
6 * as per the GNU copyleft.
10 * 09.11.91 - made the first rudimentary functions
12 * 10.11.91 - updated, does checking, no repairs yet.
13 * Sent out to the mailing-list for testing.
15 * 14.11.91 - Testing seems to have gone well. Added some
16 * correction-code, and changed some functions.
18 * 15.11.91 - More correction code. Hopefully it notices most
19 * cases now, and tries to do something about them.
21 * 16.11.91 - More corrections (thanks to Mika Jalava). Most
22 * things seem to work now. Yeah, sure.
25 * 19.04.92 - Had to start over again from this old version, as a
26 * kernel bug ate my enhanced fsck in february.
28 * 28.02.93 - added support for different directory entry sizes..
30 * Sat Mar 6 18:59:42 1993, faith@cs.unc.edu: Output namelen with
31 * super-block information
33 * Sat Oct 9 11:17:11 1993, faith@cs.unc.edu: make exit status conform
34 * to that required by fsutil
36 * Mon Jan 3 11:06:52 1994 - Dr. Wettstein (greg%wind.uucp@plains.nodak.edu)
37 * Added support for file system valid flag. Also
38 * added program_version variable and output of
39 * program name and version number when program
42 * 30.10.94 - added support for v2 filesystem
43 * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de)
45 * 10.12.94 - added test to prevent checking of mounted fs adapted
46 * from Theodore Ts'o's (tytso@athena.mit.edu) e2fsck
47 * program. (Daniel Quinlan, quinlan@yggdrasil.com)
49 * 01.07.96 - Fixed the v2 fs stuff to use the right #defines and such
50 * for modern libcs (janl@math.uio.no, Nicolai Langfeldt)
52 * 02.07.96 - Added C bit fiddling routines from rmk@ecs.soton.ac.uk
53 * (Russell King). He made them for ARM. It would seem
54 * that the ARM is powerful enough to do this in C whereas
55 * i386 and m64k must use assembly to get it fast >:-)
56 * This should make minix fsck system-independent.
57 * (janl@math.uio.no, Nicolai Langfeldt)
59 * 04.11.96 - Added minor fixes from Andreas Schwab to avoid compiler
60 * warnings. Added mc68k bitops from
61 * Joerg Dorchain <dorchain@mpi-sb.mpg.de>.
63 * 06.11.96 - Added v2 code submitted by Joerg Dorchain, but written by
66 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
67 * - added Native Language Support
70 * I've had no time to add comments - hopefully the function names
71 * are comments enough. As with all file system checkers, this assumes
72 * the file system is quiescent - don't use it on a mounted device
73 * unless you can be sure nobody is writing to it (and remember that the
74 * kernel can write to it when it searches for files).
76 * Usage: fsck [-larvsm] device
77 * -l for a listing of all the filenames
78 * -a for automatic repairs (not implemented)
79 * -r for repairs (interactive) (not implemented)
80 * -v for verbose (tells how many files)
81 * -s for super-block info
82 * -m for minix-like "mode not cleared" warnings
83 * -f force filesystem check even if filesystem marked as valid
85 * The device may be a block device or a image of one, but this isn't
86 * enforced (but it's not much fun on a character device :-).
98 #include <sys/param.h>
102 * This is the original minix inode layout on disk.
103 * Note the 8-bit gid and atime and ctime.
116 * The new minix inode has all the time entries, as well as
117 * long block numbers and a third indirect block (7+1+1+1
118 * instead of 7+1+1). Also, some previously 8-bit values are
119 * now 16-bit. The inode is now 64 bytes instead of 32.
121 struct minix2_inode {
135 MINIX_LINK_MAX = 250,
136 MINIX2_LINK_MAX = 65530,
138 MINIX_I_MAP_SLOTS = 8,
139 MINIX_Z_MAP_SLOTS = 64,
140 MINIX_SUPER_MAGIC = 0x137F, /* original minix fs */
141 MINIX_SUPER_MAGIC2 = 0x138F, /* minix fs, 30 char names */
142 MINIX2_SUPER_MAGIC = 0x2468, /* minix V2 fs */
143 MINIX2_SUPER_MAGIC2 = 0x2478, /* minix V2 fs, 30 char names */
144 MINIX_VALID_FS = 0x0001, /* Clean fs. */
145 MINIX_ERROR_FS = 0x0002, /* fs has errors. */
147 MINIX_INODES_PER_BLOCK = ((BLOCK_SIZE)/(sizeof (struct minix_inode))),
148 MINIX2_INODES_PER_BLOCK = ((BLOCK_SIZE)/(sizeof (struct minix2_inode))),
150 MINIX_V1 = 0x0001, /* original minix fs */
151 MINIX_V2 = 0x0002 /* minix V2 fs */
154 #define INODE_VERSION(inode) inode->i_sb->u.minix_sb.s_version
157 * minix super-block data on disk
159 struct minix_super_block {
162 uint16_t s_imap_blocks;
163 uint16_t s_zmap_blocks;
164 uint16_t s_firstdatazone;
165 uint16_t s_log_zone_size;
172 struct minix_dir_entry {
178 #define NAME_MAX 255 /* # chars in a file name */
180 #define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
183 #define BLKGETSIZE _IO(0x12,96) /* return device size */
190 enum { ROOT_INO = 1 };
192 #define UPPER(size,n) ((size+((n)-1))/(n))
193 #define INODE_SIZE (sizeof(struct minix_inode))
194 #ifdef CONFIG_FEATURE_MINIX2
195 #define INODE_SIZE2 (sizeof(struct minix2_inode))
196 #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
197 : MINIX_INODES_PER_BLOCK))
199 #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
201 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
203 #define BITS_PER_BLOCK (BLOCK_SIZE<<3)
205 static char *program_version = "1.2 - 11/11/96";
206 static char *device_name;
208 static int repair, automatic, verbose, list, show, warn_mode, force;
209 static int directory, regular, blockdev, chardev, links, symlinks, total;
211 static int changed; /* flags if the filesystem has been changed */
212 static int errors_uncorrected; /* flag if some error was not corrected */
213 static int dirsize = 16;
214 static int namelen = 14;
215 static struct termios termios;
216 static int termios_set;
218 static char *inode_buffer;
219 #define Inode (((struct minix_inode *) inode_buffer)-1)
220 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
221 static char super_block_buffer[BLOCK_SIZE];
223 #define Super (*(struct minix_super_block *)super_block_buffer)
224 #define INODES ((unsigned long)Super.s_ninodes)
225 #ifdef CONFIG_FEATURE_MINIX2
227 #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
229 #define ZONES ((unsigned long)(Super.s_nzones))
231 #define IMAPS ((unsigned long)Super.s_imap_blocks)
232 #define ZMAPS ((unsigned long)Super.s_zmap_blocks)
233 #define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
234 #define ZONESIZE ((unsigned long)Super.s_log_zone_size)
235 #define MAXSIZE ((unsigned long)Super.s_max_size)
236 #define MAGIC (Super.s_magic)
237 #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
239 static char *inode_map;
240 static char *zone_map;
242 static unsigned char *inode_count;
243 static unsigned char *zone_count;
245 static void recursive_check(unsigned int ino);
246 #ifdef CONFIG_FEATURE_MINIX2
247 static void recursive_check2(unsigned int ino);
250 static inline int bit(char *a, unsigned int i)
252 return (a[i >> 3] & (1<<(i & 7))) != 0;
254 #define inode_in_use(x) (bit(inode_map,(x)))
255 #define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
257 #define mark_inode(x) (setbit(inode_map,(x)),changed=1)
258 #define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
260 #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1)
261 #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1)
263 static void leave(int) ATTRIBUTE_NORETURN;
264 static void leave(int status)
267 tcsetattr(0, TCSANOW, &termios);
271 static void die(const char *str)
273 bb_error_msg("%s", str);
278 enum { MAX_DEPTH = 32 };
279 static int name_depth;
280 static char *current_name;
281 static char *name_component[MAX_DEPTH+1];
283 /* Wed Feb 9 15:17:06 MST 2000 */
284 /* dynamically allocate name_list (instead of making it static) */
285 static inline void alloc_current_name(void)
287 current_name = xmalloc(MAX_DEPTH * (BUFSIZ + 1));
288 current_name[0] = '/';
289 current_name[1] = '\0';
290 name_component[0] = ¤t_name[0];
293 #ifdef CONFIG_FEATURE_CLEAN_UP
294 /* execute this atexit() to deallocate name_list[] */
295 /* piptigger was here */
296 static inline void free_current_name(void)
302 static void push_filename(const char *name)
306 // [0] [1] [2] <-name_component[i]
307 if (name_depth < MAX_DEPTH) {
309 char *p = name_component[name_depth];
311 len = sprintf(p, "%.*s", namelen, name);
312 name_component[name_depth + 1] = p + len;
317 static void pop_filename(void) {
319 if (name_depth < MAX_DEPTH) {
320 *name_component[name_depth] = '\0';
322 current_name[0] = '/';
323 current_name[1] = '\0';
328 static int ask(const char *string, int def)
334 errors_uncorrected = 1;
340 errors_uncorrected = 1;
343 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
346 if ((c = getchar()) == EOF) {
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)
382 if ((f = setmntent(MOUNTED, "r")) == NULL)
384 while ((mnt = getmntent(f)) != NULL)
385 if (strcmp(device_name, mnt->mnt_fsname) == 0)
392 * If the root is mounted read-only, then /etc/mtab is
393 * probably not correct; so we won't issue a warning based on
396 fd = open(MOUNTED, O_RDWR);
397 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");
414 * check_zone_nr checks to see that *nr is a valid zone nr. If it
415 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
416 * if an error was corrected, and returns the zone (0 for no zone
417 * or a bad zone-number).
419 static int check_zone_nr2(uint32_t *nr, int *corrected)
426 else if (*nr >= ZONES)
430 printf("Zone nr %s in file '%s'. ", msg, current_name);
431 if (ask("Remove block", 1)) {
438 static int check_zone_nr(uint16_t *nr, int *corrected)
441 int r = check_zone_nr2(&nr32, corrected);
442 *nr = (uint16_t)nr32;
447 * read-block reads block nr into the buffer at addr.
449 static void read_block(unsigned int nr, char *addr)
452 memset(addr, 0, BLOCK_SIZE);
455 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) {
456 printf("%s: unable to seek to block in file '%s'\n",
457 bb_msg_read_error, current_name);
458 errors_uncorrected = 1;
459 memset(addr, 0, BLOCK_SIZE);
460 } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) {
461 printf("%s: bad block in file '%s'\n",
462 bb_msg_read_error, current_name);
463 errors_uncorrected = 1;
464 memset(addr, 0, BLOCK_SIZE);
469 * write_block writes block nr to disk.
471 static void write_block(unsigned int nr, char *addr)
475 if (nr < FIRSTZONE || nr >= ZONES) {
476 printf("Internal error: trying to write bad block\n"
477 "Write request ignored\n");
478 errors_uncorrected = 1;
481 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET))
482 die("Seek failed in write_block");
483 if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) {
484 printf("%s: bad block in file '%s'\n",
485 bb_msg_write_error, current_name);
486 errors_uncorrected = 1;
491 * map_block calculates the absolute block nr of a block in a file.
492 * It sets 'changed' if the inode has needed changing, and re-writes
493 * any indirect blocks with errors.
495 static int map_block(struct minix_inode *inode, unsigned int blknr)
497 uint16_t ind[BLOCK_SIZE >> 1];
498 uint16_t dind[BLOCK_SIZE >> 1];
499 int blk_chg, block, result;
502 return check_zone_nr(inode->i_zone + blknr, &changed);
505 block = check_zone_nr(inode->i_zone + 7, &changed);
506 read_block(block, (char *) ind);
508 result = check_zone_nr(blknr + ind, &blk_chg);
510 write_block(block, (char *) ind);
514 block = check_zone_nr(inode->i_zone + 8, &changed);
515 read_block(block, (char *) dind);
517 result = check_zone_nr(dind + (blknr / 512), &blk_chg);
519 write_block(block, (char *) dind);
521 read_block(block, (char *) ind);
523 result = check_zone_nr(ind + (blknr % 512), &blk_chg);
525 write_block(block, (char *) ind);
529 #ifdef CONFIG_FEATURE_MINIX2
530 static int map_block2(struct minix2_inode *inode, unsigned int blknr)
532 uint32_t ind[BLOCK_SIZE >> 2];
533 uint32_t dind[BLOCK_SIZE >> 2];
534 uint32_t tind[BLOCK_SIZE >> 2];
535 int blk_chg, block, result;
538 return check_zone_nr2(inode->i_zone + blknr, &changed);
541 block = check_zone_nr2(inode->i_zone + 7, &changed);
542 read_block(block, (char *) ind);
544 result = check_zone_nr2(blknr + ind, &blk_chg);
546 write_block(block, (char *) ind);
550 if (blknr >= 256 * 256) {
551 block = check_zone_nr2(inode->i_zone + 8, &changed);
552 read_block(block, (char *) dind);
554 result = check_zone_nr2(dind + blknr / 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);
566 block = check_zone_nr2(inode->i_zone + 9, &changed);
567 read_block(block, (char *) tind);
569 result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
571 write_block(block, (char *) tind);
573 read_block(block, (char *) dind);
575 result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
577 write_block(block, (char *) dind);
579 read_block(block, (char *) ind);
581 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
583 write_block(block, (char *) ind);
588 static void write_super_block(void)
591 * Set the state of the filesystem based on whether or not there
592 * are uncorrected errors. The filesystem valid flag is
593 * unconditionally set if we get this far.
595 Super.s_state |= MINIX_VALID_FS;
596 if (errors_uncorrected)
597 Super.s_state |= MINIX_ERROR_FS;
599 Super.s_state &= ~MINIX_ERROR_FS;
601 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
602 die("Seek failed in write_super_block");
603 if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE))
604 die("Unable to write super-block");
607 static void write_tables(void)
611 if (IMAPS * BLOCK_SIZE != write(IN, inode_map, IMAPS * BLOCK_SIZE))
612 die("Unable to write inode map");
613 if (ZMAPS * BLOCK_SIZE != write(IN, zone_map, ZMAPS * BLOCK_SIZE))
614 die("Unable to write zone map");
615 if (INODE_BUFFER_SIZE != write(IN, inode_buffer, INODE_BUFFER_SIZE))
616 die("Unable to write inodes");
619 static void get_dirsize(void)
622 char blk[BLOCK_SIZE];
625 #ifdef CONFIG_FEATURE_MINIX2
627 block = Inode2[ROOT_INO].i_zone[0];
630 block = Inode[ROOT_INO].i_zone[0];
631 read_block(block, blk);
632 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
633 if (strcmp(blk + size + 2, "..") == 0) {
642 static void read_superblock(void)
644 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
646 if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
647 die("Unable to read super block");
648 /* already initialized to:
653 if (MAGIC == MINIX_SUPER_MAGIC) {
654 } else if (MAGIC == MINIX_SUPER_MAGIC2) {
657 #ifdef CONFIG_FEATURE_MINIX2
658 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
660 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
666 die("Bad magic number in super-block");
667 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
668 die("Only 1k blocks/zones supported");
669 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
670 die("Bad s_imap_blocks field in super-block");
671 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
672 die("Bad s_zmap_blocks field in super-block");
675 static void read_tables(void)
677 inode_map = xzalloc(IMAPS * BLOCK_SIZE);
678 zone_map = xzalloc(ZMAPS * BLOCK_SIZE);
679 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
680 inode_count = xmalloc(INODES + 1);
681 zone_count = xmalloc(ZONES);
682 if (IMAPS * BLOCK_SIZE != read(IN, inode_map, IMAPS * BLOCK_SIZE))
683 die("Unable to read inode map");
684 if (ZMAPS * BLOCK_SIZE != read(IN, zone_map, ZMAPS * BLOCK_SIZE))
685 die("Unable to read zone map");
686 if (INODE_BUFFER_SIZE != read(IN, inode_buffer, INODE_BUFFER_SIZE))
687 die("Unable to read inodes");
688 if (NORM_FIRSTZONE != FIRSTZONE) {
689 printf("Warning: Firstzone!=Norm_firstzone\n");
690 errors_uncorrected = 1;
694 printf("%ld inodes\n"
696 "Firstdatazone=%ld (%ld)\n"
699 "Filesystem state=%d\n"
703 FIRSTZONE, NORM_FIRSTZONE,
704 BLOCK_SIZE << ZONESIZE,
711 static struct minix_inode *get_inode(unsigned int nr)
713 struct minix_inode *inode;
715 if (!nr || nr > INODES)
719 if (!inode_count[nr]) {
720 if (!inode_in_use(nr)) {
721 printf("Inode %d is marked as 'unused', but it is used "
722 "for file '%s'\n", nr, current_name);
724 if (ask("Mark as 'in use'", 1))
727 errors_uncorrected = 1;
730 if (S_ISDIR(inode->i_mode))
732 else if (S_ISREG(inode->i_mode))
734 else if (S_ISCHR(inode->i_mode))
736 else if (S_ISBLK(inode->i_mode))
738 else if (S_ISLNK(inode->i_mode))
740 else if (S_ISSOCK(inode->i_mode));
741 else if (S_ISFIFO(inode->i_mode));
743 printf("%s has mode %05o\n", current_name, inode->i_mode);
748 if (!++inode_count[nr]) {
749 printf("Warning: inode count too big\n");
751 errors_uncorrected = 1;
756 #ifdef CONFIG_FEATURE_MINIX2
757 static struct minix2_inode *get_inode2(unsigned int nr)
759 struct minix2_inode *inode;
761 if (!nr || nr > INODES)
765 if (!inode_count[nr]) {
766 if (!inode_in_use(nr)) {
767 printf("Inode %d is marked as 'unused', but it is used "
768 "for file '%s'\n", nr, current_name);
770 if (ask("Mark as 'in use'", 1))
773 errors_uncorrected = 1;
776 if (S_ISDIR(inode->i_mode))
778 else if (S_ISREG(inode->i_mode))
780 else if (S_ISCHR(inode->i_mode))
782 else if (S_ISBLK(inode->i_mode))
784 else if (S_ISLNK(inode->i_mode))
786 else if (S_ISSOCK(inode->i_mode));
787 else if (S_ISFIFO(inode->i_mode));
789 printf("%s has mode %05o\n", current_name, inode->i_mode);
793 if (!++inode_count[nr]) {
794 printf("Warning: inode count too big\n");
796 errors_uncorrected = 1;
802 static void check_root(void)
804 struct minix_inode *inode = Inode + ROOT_INO;
806 if (!inode || !S_ISDIR(inode->i_mode))
807 die("Root inode isn't a directory");
810 #ifdef CONFIG_FEATURE_MINIX2
811 static void check_root2(void)
813 struct minix2_inode *inode = Inode2 + ROOT_INO;
815 if (!inode || !S_ISDIR(inode->i_mode))
816 die("Root inode isn't a directory");
820 static int add_zone(uint16_t *znr, int *corrected)
826 block = check_zone_nr(znr, corrected);
829 if (zone_count[block]) {
830 printf("Already used block is reused in file '%s'. ",
832 if (ask("Clear", 1)) {
839 if (!zone_in_use(block)) {
840 printf("Block %d in file '%s' is marked as 'unused'. ",
841 block, current_name);
842 if (ask("Correct", 1))
845 if (!++zone_count[block])
850 #ifdef CONFIG_FEATURE_MINIX2
851 static int add_zone2(uint32_t *znr, int *corrected)
857 block = check_zone_nr2(znr, corrected);
860 if (zone_count[block]) {
861 printf("Already used block is reused in file '%s'. ",
863 if (ask("Clear", 1)) {
870 if (!zone_in_use(block)) {
871 printf("Block %d in file '%s' is marked as 'unused'. ",
872 block, current_name);
873 if (ask("Correct", 1))
876 if (!++zone_count[block])
882 static void add_zone_ind(uint16_t *znr, int *corrected)
884 static char blk[BLOCK_SIZE];
888 block = add_zone(znr, corrected);
891 read_block(block, blk);
892 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
893 add_zone(i + (uint16_t *) blk, &chg_blk);
895 write_block(block, blk);
898 #ifdef CONFIG_FEATURE_MINIX2
899 static void add_zone_ind2(uint32_t *znr, int *corrected)
901 static char blk[BLOCK_SIZE];
905 block = add_zone2(znr, corrected);
908 read_block(block, blk);
909 for (i = 0; i < BLOCK_SIZE >> 2; i++)
910 add_zone2(i + (uint32_t *) blk, &chg_blk);
912 write_block(block, blk);
916 static void add_zone_dind(uint16_t *znr, int *corrected)
918 static char blk[BLOCK_SIZE];
922 block = add_zone(znr, corrected);
925 read_block(block, blk);
926 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
927 add_zone_ind(i + (uint16_t *) blk, &blk_chg);
929 write_block(block, blk);
932 #ifdef CONFIG_FEATURE_MINIX2
933 static void add_zone_dind2(uint32_t *znr, int *corrected)
935 static char blk[BLOCK_SIZE];
939 block = add_zone2(znr, corrected);
942 read_block(block, blk);
943 for (i = 0; i < BLOCK_SIZE >> 2; i++)
944 add_zone_ind2(i + (uint32_t *) blk, &blk_chg);
946 write_block(block, blk);
949 static void add_zone_tind2(uint32_t *znr, int *corrected)
951 static char blk[BLOCK_SIZE];
955 block = add_zone2(znr, corrected);
958 read_block(block, blk);
959 for (i = 0; i < BLOCK_SIZE >> 2; i++)
960 add_zone_dind2(i + (uint32_t *) blk, &blk_chg);
962 write_block(block, blk);
966 static void check_zones(unsigned int i)
968 struct minix_inode *inode;
970 if (!i || i > INODES)
972 if (inode_count[i] > 1) /* have we counted this file already? */
975 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
976 !S_ISLNK(inode->i_mode)) return;
977 for (i = 0; i < 7; i++)
978 add_zone(i + inode->i_zone, &changed);
979 add_zone_ind(7 + inode->i_zone, &changed);
980 add_zone_dind(8 + inode->i_zone, &changed);
983 #ifdef CONFIG_FEATURE_MINIX2
984 static void check_zones2(unsigned int i)
986 struct minix2_inode *inode;
988 if (!i || i > INODES)
990 if (inode_count[i] > 1) /* have we counted this file already? */
993 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
994 && !S_ISLNK(inode->i_mode))
996 for (i = 0; i < 7; i++)
997 add_zone2(i + inode->i_zone, &changed);
998 add_zone_ind2(7 + inode->i_zone, &changed);
999 add_zone_dind2(8 + inode->i_zone, &changed);
1000 add_zone_tind2(9 + inode->i_zone, &changed);
1004 static void check_file(struct minix_inode *dir, unsigned int offset)
1006 static char blk[BLOCK_SIZE];
1007 struct minix_inode *inode;
1012 block = map_block(dir, offset / BLOCK_SIZE);
1013 read_block(block, blk);
1014 name = blk + (offset % BLOCK_SIZE) + 2;
1015 ino = *(uint16_t *) (name - 2);
1017 printf("%s contains a bad inode number for file '%.*s'. ",
1018 current_name, namelen, name);
1019 if (ask("Remove", 1)) {
1020 *(uint16_t *) (name - 2) = 0;
1021 write_block(block, blk);
1025 push_filename(name);
1026 inode = get_inode(ino);
1029 if (!inode || strcmp(".", name)) {
1030 printf("%s: bad directory: '.' isn't first\n", current_name);
1031 errors_uncorrected = 1;
1035 if (offset == dirsize) {
1036 if (!inode || strcmp("..", name)) {
1037 printf("%s: bad directory: '..' isn't second\n", current_name);
1038 errors_uncorrected = 1;
1044 push_filename(name);
1047 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1048 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
1051 if (inode && S_ISDIR(inode->i_mode))
1052 recursive_check(ino);
1057 #ifdef CONFIG_FEATURE_MINIX2
1058 static void check_file2(struct minix2_inode *dir, unsigned int offset)
1060 static char blk[BLOCK_SIZE];
1061 struct minix2_inode *inode;
1066 block = map_block2(dir, offset / BLOCK_SIZE);
1067 read_block(block, blk);
1068 name = blk + (offset % BLOCK_SIZE) + 2;
1069 ino = *(uint16_t *) (name - 2);
1071 printf("%s contains a bad inode number for file '%.*s'. ",
1072 current_name, namelen, name);
1073 if (ask("Remove", 1)) {
1074 *(uint16_t *) (name - 2) = 0;
1075 write_block(block, blk);
1079 push_filename(name);
1080 inode = get_inode2(ino);
1083 if (!inode || strcmp(".", name)) {
1084 printf("%s: bad directory: '.' isn't first\n", current_name);
1085 errors_uncorrected = 1;
1089 if (offset == dirsize) {
1090 if (!inode || strcmp("..", name)) {
1091 printf("%s: bad directory: '..' isn't second\n", current_name);
1092 errors_uncorrected = 1;
1098 push_filename(name);
1101 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1102 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
1105 if (inode && S_ISDIR(inode->i_mode))
1106 recursive_check2(ino);
1112 static void recursive_check(unsigned int ino)
1114 struct minix_inode *dir;
1115 unsigned int offset;
1118 if (!S_ISDIR(dir->i_mode))
1119 die("Internal error");
1120 if (dir->i_size < 2 * dirsize) {
1121 printf("%s: bad directory: size<32", current_name);
1122 errors_uncorrected = 1;
1124 for (offset = 0; offset < dir->i_size; offset += dirsize)
1125 check_file(dir, offset);
1128 #ifdef CONFIG_FEATURE_MINIX2
1129 static void recursive_check2(unsigned int ino)
1131 struct minix2_inode *dir;
1132 unsigned int offset;
1135 if (!S_ISDIR(dir->i_mode))
1136 die("Internal error");
1137 if (dir->i_size < 2 * dirsize) {
1138 printf("%s: bad directory: size<32", current_name);
1139 errors_uncorrected = 1;
1141 for (offset = 0; offset < dir->i_size; offset += dirsize)
1142 check_file2(dir, offset);
1146 static int bad_zone(int i)
1150 if (BLOCK_SIZE * i != lseek(IN, BLOCK_SIZE * i, SEEK_SET))
1151 die("Seek failed in bad_zone");
1152 return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE));
1155 static void check_counts(void)
1159 for (i = 1; i <= INODES; i++) {
1160 if (warn_mode && Inode[i].i_mode && !inode_in_use(i)) {
1161 printf("Inode %d has non-zero mode. ", i);
1162 if (ask("Clear", 1)) {
1163 Inode[i].i_mode = 0;
1167 if (!inode_count[i]) {
1168 if (!inode_in_use(i))
1170 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
1171 if (ask("Clear", 1))
1175 if (!inode_in_use(i)) {
1176 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
1180 if (Inode[i].i_nlinks != inode_count[i]) {
1181 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
1182 i, Inode[i].i_mode, Inode[i].i_nlinks, inode_count[i]);
1183 if (ask("Set i_nlinks to count", 1)) {
1184 Inode[i].i_nlinks = inode_count[i];
1189 for (i = FIRSTZONE; i < ZONES; i++) {
1190 if (zone_in_use(i) == zone_count[i])
1192 if (!zone_count[i]) {
1195 printf("Zone %d is marked 'in use', but no file uses it. ", i);
1196 if (ask("Unmark", 1))
1200 printf("Zone %d: %sin use, counted=%d\n",
1201 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1205 #ifdef CONFIG_FEATURE_MINIX2
1206 static void check_counts2(void)
1210 for (i = 1; i <= INODES; i++) {
1211 if (warn_mode && Inode2[i].i_mode && !inode_in_use(i)) {
1212 printf("Inode %d has non-zero mode. ", i);
1213 if (ask("Clear", 1)) {
1214 Inode2[i].i_mode = 0;
1218 if (!inode_count[i]) {
1219 if (!inode_in_use(i))
1221 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
1222 if (ask("Clear", 1))
1226 if (!inode_in_use(i)) {
1227 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
1231 if (Inode2[i].i_nlinks != inode_count[i]) {
1232 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
1233 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1235 if (ask("Set i_nlinks to count", 1)) {
1236 Inode2[i].i_nlinks = inode_count[i];
1241 for (i = FIRSTZONE; i < ZONES; i++) {
1242 if (zone_in_use(i) == zone_count[i])
1244 if (!zone_count[i]) {
1247 printf("Zone %d is marked 'in use', but no file uses it. ", i);
1248 if (ask("Unmark", 1))
1252 printf("Zone %d: %sin use, counted=%d\n",
1253 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1258 static void check(void)
1260 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1261 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1262 check_zones(ROOT_INO);
1263 recursive_check(ROOT_INO);
1267 #ifdef CONFIG_FEATURE_MINIX2
1268 static void check2(void)
1270 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1271 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1272 check_zones2(ROOT_INO);
1273 recursive_check2(ROOT_INO);
1278 int fsck_minix_main(int argc, char **argv)
1283 alloc_current_name();
1284 #ifdef CONFIG_FEATURE_CLEAN_UP
1285 /* Don't bother to free memory. Exit does
1286 * that automagically, so we can save a few bytes */
1287 atexit(free_current_name);
1290 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
1291 die("Bad inode size");
1292 #ifdef CONFIG_FEATURE_MINIX2
1293 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
1294 die("Bad v2 inode size");
1296 while (argc-- > 1) {
1298 if (argv[0][0] != '-') {
1302 device_name = argv[0];
1305 switch (argv[0][0]) {
1335 check_mount(); /* trying to check a mounted filesystem? */
1336 if (repair && !automatic) {
1337 if (!isatty(0) || !isatty(1))
1338 die("Need terminal for interactive repairs");
1340 IN = open(device_name, repair ? O_RDWR : O_RDONLY);
1342 printf("Unable to open device '%s'\n", device_name);
1345 sync(); /* paranoia? */
1349 * Determine whether or not we should continue with the checking.
1350 * This is based on the status of the filesystem valid and error
1351 * flags and whether or not the -f switch was specified on the
1354 printf("%s, %s\n", bb_applet_name, program_version);
1355 if (!(Super.s_state & MINIX_ERROR_FS) &&
1356 (Super.s_state & MINIX_VALID_FS) && !force) {
1358 printf("%s is clean, check is skipped\n", device_name);
1361 printf("Forcing filesystem check on %s\n", device_name);
1363 printf("Filesystem on %s is dirty, needs checking\n",
1368 if (repair && !automatic) {
1369 tcgetattr(0, &termios);
1371 tmp.c_lflag &= ~(ICANON | ECHO);
1372 tcsetattr(0, TCSANOW, &tmp);
1375 #ifdef CONFIG_FEATURE_MINIX2
1388 for (i = 1, free_cnt = 0; i <= INODES; i++)
1389 if (!inode_in_use(i))
1391 printf("\n%6ld inodes used (%ld%%)\n", (INODES - free_cnt),
1392 100 * (INODES - free_cnt) / INODES);
1393 for (i = FIRSTZONE, free_cnt = 0; i < ZONES; i++)
1394 if (!zone_in_use(i))
1396 printf("%6ld zones used (%ld%%)\n\n"
1397 "%6d regular files\n"
1399 "%6d character device files\n"
1400 "%6d block device files\n"
1402 "%6d symbolic links\n"
1405 (ZONES - free_cnt), 100 * (ZONES - free_cnt) / ZONES,
1406 regular, directory, chardev, blockdev,
1407 links - 2 * directory + 1, symlinks,
1408 total - 2 * directory + 1);
1412 printf("FILE SYSTEM HAS BEEN CHANGED\n");
1415 write_super_block();
1417 if (repair && !automatic)
1418 tcsetattr(0, TCSANOW, &termios);
1422 if (errors_uncorrected)