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>
101 static const int MINIX_ROOT_INO = 1;
102 static const int MINIX_LINK_MAX = 250;
103 static const int MINIX2_LINK_MAX = 65530;
105 static const int MINIX_I_MAP_SLOTS = 8;
106 static const int MINIX_Z_MAP_SLOTS = 64;
107 static const int MINIX_SUPER_MAGIC = 0x137F; /* original minix fs */
108 static const int MINIX_SUPER_MAGIC2 = 0x138F; /* minix fs, 30 char names */
109 static const int MINIX2_SUPER_MAGIC = 0x2468; /* minix V2 fs */
110 static const int MINIX2_SUPER_MAGIC2 = 0x2478; /* minix V2 fs, 30 char names */
111 static const int MINIX_VALID_FS = 0x0001; /* Clean fs. */
112 static const int MINIX_ERROR_FS = 0x0002; /* fs has errors. */
114 #define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
115 #define MINIX2_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix2_inode)))
117 static const int MINIX_V1 = 0x0001; /* original minix fs */
118 static const int MINIX_V2 = 0x0002; /* minix V2 fs */
120 #define INODE_VERSION(inode) inode->i_sb->u.minix_sb.s_version
123 * This is the original minix inode layout on disk.
124 * Note the 8-bit gid and atime and ctime.
137 * The new minix inode has all the time entries, as well as
138 * long block numbers and a third indirect block (7+1+1+1
139 * instead of 7+1+1). Also, some previously 8-bit values are
140 * now 16-bit. The inode is now 64 bytes instead of 32.
142 struct minix2_inode {
155 * minix super-block data on disk
157 struct minix_super_block {
160 uint16_t s_imap_blocks;
161 uint16_t s_zmap_blocks;
162 uint16_t s_firstdatazone;
163 uint16_t s_log_zone_size;
170 struct minix_dir_entry {
175 #define BLOCK_SIZE_BITS 10
176 #define BLOCK_SIZE (1<<BLOCK_SIZE_BITS)
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 static const int 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 = NULL;
208 static int repair = 0, automatic = 0, verbose = 0, list = 0, show =
209 0, warn_mode = 0, force = 0;
210 static int directory = 0, regular = 0, blockdev = 0, chardev = 0, links =
211 0, symlinks = 0, total = 0;
213 static int changed = 0; /* flags if the filesystem has been changed */
214 static int errors_uncorrected = 0; /* flag if some error was not corrected */
215 static int dirsize = 16;
216 static int namelen = 14;
217 static int version2 = 0;
218 static struct termios termios;
219 static int termios_set = 0;
222 static const int MAX_DEPTH = 32;
223 static int name_depth = 0;
224 // static char name_list[MAX_DEPTH][BUFSIZ + 1];
225 static char **name_list = NULL;
227 static char *inode_buffer = NULL;
229 #define Inode (((struct minix_inode *) inode_buffer)-1)
230 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
231 static char super_block_buffer[BLOCK_SIZE];
233 #define Super (*(struct minix_super_block *)super_block_buffer)
234 #define INODES ((unsigned long)Super.s_ninodes)
235 #ifdef CONFIG_FEATURE_MINIX2
236 #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
238 #define ZONES ((unsigned long)(Super.s_nzones))
240 #define IMAPS ((unsigned long)Super.s_imap_blocks)
241 #define ZMAPS ((unsigned long)Super.s_zmap_blocks)
242 #define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
243 #define ZONESIZE ((unsigned long)Super.s_log_zone_size)
244 #define MAXSIZE ((unsigned long)Super.s_max_size)
245 #define MAGIC (Super.s_magic)
246 #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
248 static char *inode_map;
249 static char *zone_map;
251 static unsigned char *inode_count = NULL;
252 static unsigned char *zone_count = NULL;
254 static void recursive_check(unsigned int ino);
255 #ifdef CONFIG_FEATURE_MINIX2
256 static void recursive_check2(unsigned int ino);
259 static inline int bit(char * a,unsigned int i)
261 return (a[i >> 3] & (1<<(i & 7))) != 0;
263 #define inode_in_use(x) (bit(inode_map,(x)))
264 #define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
266 #define mark_inode(x) (setbit(inode_map,(x)),changed=1)
267 #define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
269 #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1)
270 #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1)
272 static void leave(int) __attribute__ ((noreturn));
273 static void leave(int status)
276 tcsetattr(0, TCSANOW, &termios);
280 static void die(const char *str)
282 bb_error_msg("%s", str);
287 * This simply goes through the file-name data and prints out the
290 static void print_current_name(void)
294 while (i < name_depth)
295 printf("/%.*s", namelen, name_list[i++]);
300 static int ask(const char *string, int def)
306 errors_uncorrected = 1;
312 errors_uncorrected = 1;
315 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
318 if ((c = getchar()) == EOF) {
320 errors_uncorrected = 1;
327 } else if (c == 'N') {
330 } else if (c == ' ' || c == '\n')
337 errors_uncorrected = 1;
343 * Make certain that we aren't checking a filesystem that is on a
344 * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
345 * 1994 Theodore Ts'o. Also licensed under GPL.
347 static void check_mount(void)
354 if ((f = setmntent(MOUNTED, "r")) == NULL)
356 while ((mnt = getmntent(f)) != NULL)
357 if (strcmp(device_name, mnt->mnt_fsname) == 0)
364 * If the root is mounted read-only, then /etc/mtab is
365 * probably not correct; so we won't issue a warning based on
368 fd = open(MOUNTED, O_RDWR);
369 if (fd < 0 && errno == EROFS)
374 printf("%s is mounted. ", device_name);
375 if (isatty(0) && isatty(1))
376 cont = ask("Do you really want to continue", 0);
380 printf("check aborted.\n");
387 * check_zone_nr checks to see that *nr is a valid zone nr. If it
388 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
389 * if an error was corrected, and returns the zone (0 for no zone
390 * or a bad zone-number).
392 static int check_zone_nr(unsigned short *nr, int *corrected)
397 printf("Zone nr < FIRSTZONE in file `");
398 else if (*nr >= ZONES)
399 printf("Zone nr >= ZONES in file `");
402 print_current_name();
404 if (ask("Remove block", 1)) {
411 #ifdef CONFIG_FEATURE_MINIX2
412 static int check_zone_nr2(unsigned int *nr, int *corrected)
417 printf("Zone nr < FIRSTZONE in file `");
418 else if (*nr >= ZONES)
419 printf("Zone nr >= ZONES in file `");
422 print_current_name();
424 if (ask("Remove block", 1)) {
433 * read-block reads block nr into the buffer at addr.
435 static void read_block(unsigned int nr, char *addr)
438 memset(addr, 0, BLOCK_SIZE);
441 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) {
442 printf("Read error: unable to seek to block in file '");
443 print_current_name();
445 memset(addr, 0, BLOCK_SIZE);
446 errors_uncorrected = 1;
447 } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) {
448 printf("Read error: bad block in file '");
449 print_current_name();
451 memset(addr, 0, BLOCK_SIZE);
452 errors_uncorrected = 1;
457 * write_block writes block nr to disk.
459 static void write_block(unsigned int nr, char *addr)
463 if (nr < FIRSTZONE || nr >= ZONES) {
464 printf("Internal error: trying to write bad block\n"
465 "Write request ignored\n");
466 errors_uncorrected = 1;
469 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET))
470 die("seek failed in write_block");
471 if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) {
472 printf("Write error: bad block in file '");
473 print_current_name();
475 errors_uncorrected = 1;
480 * map-block calculates the absolute block nr of a block in a file.
481 * It sets 'changed' if the inode has needed changing, and re-writes
482 * any indirect blocks with errors.
484 static int map_block(struct minix_inode *inode, unsigned int blknr)
486 unsigned short ind[BLOCK_SIZE >> 1];
487 unsigned short dind[BLOCK_SIZE >> 1];
488 int blk_chg, block, result;
491 return check_zone_nr(inode->i_zone + blknr, &changed);
494 block = check_zone_nr(inode->i_zone + 7, &changed);
495 read_block(block, (char *) ind);
497 result = check_zone_nr(blknr + ind, &blk_chg);
499 write_block(block, (char *) ind);
503 block = check_zone_nr(inode->i_zone + 8, &changed);
504 read_block(block, (char *) dind);
506 result = check_zone_nr(dind + (blknr / 512), &blk_chg);
508 write_block(block, (char *) dind);
510 read_block(block, (char *) ind);
512 result = check_zone_nr(ind + (blknr % 512), &blk_chg);
514 write_block(block, (char *) ind);
518 #ifdef CONFIG_FEATURE_MINIX2
519 static int map_block2(struct minix2_inode *inode, unsigned int blknr)
521 unsigned int ind[BLOCK_SIZE >> 2];
522 unsigned int dind[BLOCK_SIZE >> 2];
523 unsigned int tind[BLOCK_SIZE >> 2];
524 int blk_chg, block, result;
527 return check_zone_nr2(inode->i_zone + blknr, &changed);
530 block = check_zone_nr2(inode->i_zone + 7, &changed);
531 read_block(block, (char *) ind);
533 result = check_zone_nr2(blknr + ind, &blk_chg);
535 write_block(block, (char *) ind);
539 if (blknr >= 256 * 256) {
540 block = check_zone_nr2(inode->i_zone + 8, &changed);
541 read_block(block, (char *) dind);
543 result = check_zone_nr2(dind + blknr / 256, &blk_chg);
545 write_block(block, (char *) dind);
547 read_block(block, (char *) ind);
549 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
551 write_block(block, (char *) ind);
555 block = check_zone_nr2(inode->i_zone + 9, &changed);
556 read_block(block, (char *) tind);
558 result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
560 write_block(block, (char *) tind);
562 read_block(block, (char *) dind);
564 result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
566 write_block(block, (char *) dind);
568 read_block(block, (char *) ind);
570 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
572 write_block(block, (char *) ind);
577 static void write_super_block(void)
580 * Set the state of the filesystem based on whether or not there
581 * are uncorrected errors. The filesystem valid flag is
582 * unconditionally set if we get this far.
584 Super.s_state |= MINIX_VALID_FS;
585 if (errors_uncorrected)
586 Super.s_state |= MINIX_ERROR_FS;
588 Super.s_state &= ~MINIX_ERROR_FS;
590 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
591 die("seek failed in write_super_block");
592 if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE))
593 die("unable to write super-block");
598 static void write_tables(void)
602 if (IMAPS * BLOCK_SIZE != write(IN, inode_map, IMAPS * BLOCK_SIZE))
603 die("Unable to write inode map");
604 if (ZMAPS * BLOCK_SIZE != write(IN, zone_map, ZMAPS * BLOCK_SIZE))
605 die("Unable to write zone map");
606 if (INODE_BUFFER_SIZE != write(IN, inode_buffer, INODE_BUFFER_SIZE))
607 die("Unable to write inodes");
610 static void get_dirsize(void)
613 char blk[BLOCK_SIZE];
616 #ifdef CONFIG_FEATURE_MINIX2
618 block = Inode2[ROOT_INO].i_zone[0];
621 block = Inode[ROOT_INO].i_zone[0];
622 read_block(block, blk);
623 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
624 if (strcmp(blk + size + 2, "..") == 0) {
633 static void read_superblock(void)
635 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
637 if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
638 die("unable to read super block");
639 if (MAGIC == MINIX_SUPER_MAGIC) {
643 } else if (MAGIC == MINIX_SUPER_MAGIC2) {
647 #ifdef CONFIG_FEATURE_MINIX2
648 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
652 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
658 die("bad magic number in super-block");
659 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
660 die("Only 1k blocks/zones supported");
661 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
662 die("bad s_imap_blocks field in super-block");
663 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
664 die("bad s_zmap_blocks field in super-block");
667 static void read_tables(void)
669 inode_map = xmalloc(IMAPS * BLOCK_SIZE);
670 zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
671 memset(inode_map, 0, sizeof(inode_map));
672 memset(zone_map, 0, sizeof(zone_map));
673 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
674 inode_count = xmalloc(INODES + 1);
675 zone_count = xmalloc(ZONES);
676 if (IMAPS * BLOCK_SIZE != read(IN, inode_map, IMAPS * BLOCK_SIZE))
677 die("Unable to read inode map");
678 if (ZMAPS * BLOCK_SIZE != read(IN, zone_map, ZMAPS * BLOCK_SIZE))
679 die("Unable to read zone map");
680 if (INODE_BUFFER_SIZE != read(IN, inode_buffer, INODE_BUFFER_SIZE))
681 die("Unable to read inodes");
682 if (NORM_FIRSTZONE != FIRSTZONE) {
683 printf("Warning: Firstzone != Norm_firstzone\n");
684 errors_uncorrected = 1;
688 printf("%ld inodes\n", INODES);
689 printf("%ld blocks\n", ZONES);
690 printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
691 printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
692 printf("Maxsize=%ld\n", MAXSIZE);
693 printf("Filesystem state=%d\n", Super.s_state);
694 printf("namelen=%d\n\n", namelen);
698 static struct minix_inode *get_inode(unsigned int nr)
700 struct minix_inode *inode;
702 if (!nr || nr > INODES)
706 if (!inode_count[nr]) {
707 if (!inode_in_use(nr)) {
708 printf("Inode %d marked not used, but used for file '", nr);
709 print_current_name();
712 if (ask("Mark in use", 1))
715 errors_uncorrected = 1;
718 if (S_ISDIR(inode->i_mode))
720 else if (S_ISREG(inode->i_mode))
722 else if (S_ISCHR(inode->i_mode))
724 else if (S_ISBLK(inode->i_mode))
726 else if (S_ISLNK(inode->i_mode))
728 else if (S_ISSOCK(inode->i_mode));
729 else if (S_ISFIFO(inode->i_mode));
731 print_current_name();
732 printf(" has mode %05o\n", inode->i_mode);
737 if (!++inode_count[nr]) {
738 printf("Warning: inode count too big.\n");
740 errors_uncorrected = 1;
745 #ifdef CONFIG_FEATURE_MINIX2
746 static struct minix2_inode *get_inode2(unsigned int nr)
748 struct minix2_inode *inode;
750 if (!nr || nr > INODES)
754 if (!inode_count[nr]) {
755 if (!inode_in_use(nr)) {
756 printf("Inode %d marked not used, but used for file '", nr);
757 print_current_name();
760 if (ask("Mark in use", 1))
763 errors_uncorrected = 1;
766 if (S_ISDIR(inode->i_mode))
768 else if (S_ISREG(inode->i_mode))
770 else if (S_ISCHR(inode->i_mode))
772 else if (S_ISBLK(inode->i_mode))
774 else if (S_ISLNK(inode->i_mode))
776 else if (S_ISSOCK(inode->i_mode));
777 else if (S_ISFIFO(inode->i_mode));
779 print_current_name();
780 printf(" has mode %05o\n", inode->i_mode);
784 if (!++inode_count[nr]) {
785 printf("Warning: inode count too big.\n");
787 errors_uncorrected = 1;
793 static void check_root(void)
795 struct minix_inode *inode = Inode + ROOT_INO;
797 if (!inode || !S_ISDIR(inode->i_mode))
798 die("root inode isn't a directory");
801 #ifdef CONFIG_FEATURE_MINIX2
802 static void check_root2(void)
804 struct minix2_inode *inode = Inode2 + ROOT_INO;
806 if (!inode || !S_ISDIR(inode->i_mode))
807 die("root inode isn't a directory");
811 static int add_zone(unsigned short *znr, int *corrected)
817 block = check_zone_nr(znr, corrected);
820 if (zone_count[block]) {
821 printf("Block has been used before. Now in file `");
822 print_current_name();
824 if (ask("Clear", 1)) {
832 if (!zone_in_use(block)) {
833 printf("Block %d in file `", block);
834 print_current_name();
835 printf("' is marked not in use.");
836 if (ask("Correct", 1))
839 if (!++zone_count[block])
844 #ifdef CONFIG_FEATURE_MINIX2
845 static int add_zone2(unsigned int *znr, int *corrected)
851 block = check_zone_nr2(znr, corrected);
854 if (zone_count[block]) {
855 printf("Block has been used before. Now in file `");
856 print_current_name();
858 if (ask("Clear", 1)) {
866 if (!zone_in_use(block)) {
867 printf("Block %d in file `", block);
868 print_current_name();
869 printf("' is marked not in use.");
870 if (ask("Correct", 1))
873 if (!++zone_count[block])
879 static void add_zone_ind(unsigned short *znr, int *corrected)
881 static char blk[BLOCK_SIZE];
885 block = add_zone(znr, corrected);
888 read_block(block, blk);
889 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
890 add_zone(i + (unsigned short *) blk, &chg_blk);
892 write_block(block, blk);
895 #ifdef CONFIG_FEATURE_MINIX2
896 static void add_zone_ind2(unsigned int *znr, int *corrected)
898 static char blk[BLOCK_SIZE];
902 block = add_zone2(znr, corrected);
905 read_block(block, blk);
906 for (i = 0; i < BLOCK_SIZE >> 2; i++)
907 add_zone2(i + (unsigned int *) blk, &chg_blk);
909 write_block(block, blk);
913 static void add_zone_dind(unsigned short *znr, int *corrected)
915 static char blk[BLOCK_SIZE];
919 block = add_zone(znr, corrected);
922 read_block(block, blk);
923 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
924 add_zone_ind(i + (unsigned short *) blk, &blk_chg);
926 write_block(block, blk);
929 #ifdef CONFIG_FEATURE_MINIX2
930 static void add_zone_dind2(unsigned int *znr, int *corrected)
932 static char blk[BLOCK_SIZE];
936 block = add_zone2(znr, corrected);
939 read_block(block, blk);
940 for (i = 0; i < BLOCK_SIZE >> 2; i++)
941 add_zone_ind2(i + (unsigned int *) blk, &blk_chg);
943 write_block(block, blk);
946 static void add_zone_tind2(unsigned int *znr, int *corrected)
948 static char blk[BLOCK_SIZE];
952 block = add_zone2(znr, corrected);
955 read_block(block, blk);
956 for (i = 0; i < BLOCK_SIZE >> 2; i++)
957 add_zone_dind2(i + (unsigned int *) blk, &blk_chg);
959 write_block(block, blk);
963 static void check_zones(unsigned int i)
965 struct minix_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)) return;
974 for (i = 0; i < 7; i++)
975 add_zone(i + inode->i_zone, &changed);
976 add_zone_ind(7 + inode->i_zone, &changed);
977 add_zone_dind(8 + inode->i_zone, &changed);
980 #ifdef CONFIG_FEATURE_MINIX2
981 static void check_zones2(unsigned int i)
983 struct minix2_inode *inode;
985 if (!i || i > INODES)
987 if (inode_count[i] > 1) /* have we counted this file already? */
990 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
991 && !S_ISLNK(inode->i_mode))
993 for (i = 0; i < 7; i++)
994 add_zone2(i + inode->i_zone, &changed);
995 add_zone_ind2(7 + inode->i_zone, &changed);
996 add_zone_dind2(8 + inode->i_zone, &changed);
997 add_zone_tind2(9 + inode->i_zone, &changed);
1001 static void check_file(struct minix_inode *dir, unsigned int offset)
1003 static char blk[BLOCK_SIZE];
1004 struct minix_inode *inode;
1009 block = map_block(dir, offset / BLOCK_SIZE);
1010 read_block(block, blk);
1011 name = blk + (offset % BLOCK_SIZE) + 2;
1012 ino = *(unsigned short *) (name - 2);
1014 print_current_name();
1015 printf(" contains a bad inode number for file '");
1016 printf("%.*s'.", namelen, name);
1017 if (ask(" Remove", 1)) {
1018 *(unsigned short *) (name - 2) = 0;
1019 write_block(block, blk);
1023 if (name_depth < MAX_DEPTH)
1024 strncpy(name_list[name_depth], name, namelen);
1026 inode = get_inode(ino);
1029 if (!inode || strcmp(".", name)) {
1030 print_current_name();
1031 printf(": bad directory: '.' isn't first\n");
1032 errors_uncorrected = 1;
1036 if (offset == dirsize) {
1037 if (!inode || strcmp("..", name)) {
1038 print_current_name();
1039 printf(": bad directory: '..' isn't second\n");
1040 errors_uncorrected = 1;
1046 if (name_depth < MAX_DEPTH)
1047 strncpy(name_list[name_depth], name, namelen);
1051 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1052 print_current_name();
1053 if (S_ISDIR(inode->i_mode))
1059 if (inode && S_ISDIR(inode->i_mode))
1060 recursive_check(ino);
1065 #ifdef CONFIG_FEATURE_MINIX2
1066 static void check_file2(struct minix2_inode *dir, unsigned int offset)
1068 static char blk[BLOCK_SIZE];
1069 struct minix2_inode *inode;
1074 block = map_block2(dir, offset / BLOCK_SIZE);
1075 read_block(block, blk);
1076 name = blk + (offset % BLOCK_SIZE) + 2;
1077 ino = *(unsigned short *) (name - 2);
1079 print_current_name();
1080 printf(" contains a bad inode number for file '");
1081 printf("%.*s'.", namelen, name);
1082 if (ask(" Remove", 1)) {
1083 *(unsigned short *) (name - 2) = 0;
1084 write_block(block, blk);
1088 if (name_depth < MAX_DEPTH)
1089 strncpy(name_list[name_depth], name, namelen);
1091 inode = get_inode2(ino);
1094 if (!inode || strcmp(".", name)) {
1095 print_current_name();
1096 printf(": bad directory: '.' isn't first\n");
1097 errors_uncorrected = 1;
1101 if (offset == dirsize) {
1102 if (!inode || strcmp("..", name)) {
1103 print_current_name();
1104 printf(": bad directory: '..' isn't second\n");
1105 errors_uncorrected = 1;
1114 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1115 print_current_name();
1116 if (S_ISDIR(inode->i_mode))
1122 if (inode && S_ISDIR(inode->i_mode))
1123 recursive_check2(ino);
1129 static void recursive_check(unsigned int ino)
1131 struct minix_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 print_current_name();
1139 printf(": bad directory: size<32");
1140 errors_uncorrected = 1;
1142 for (offset = 0; offset < dir->i_size; offset += dirsize)
1143 check_file(dir, offset);
1146 #ifdef CONFIG_FEATURE_MINIX2
1147 static void recursive_check2(unsigned int ino)
1149 struct minix2_inode *dir;
1150 unsigned int offset;
1153 if (!S_ISDIR(dir->i_mode))
1154 die("internal error");
1155 if (dir->i_size < 2 * dirsize) {
1156 print_current_name();
1157 printf(": bad directory: size < 32");
1158 errors_uncorrected = 1;
1160 for (offset = 0; offset < dir->i_size; offset += dirsize)
1161 check_file2(dir, offset);
1165 static int bad_zone(int i)
1169 if (BLOCK_SIZE * i != lseek(IN, BLOCK_SIZE * i, SEEK_SET))
1170 die("seek failed in bad_zone");
1171 return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE));
1174 static void check_counts(void)
1178 for (i = 1; i <= INODES; i++) {
1179 if (!inode_in_use(i) && Inode[i].i_mode && warn_mode) {
1180 printf("Inode %d mode not cleared.", i);
1181 if (ask("Clear", 1)) {
1182 Inode[i].i_mode = 0;
1186 if (!inode_count[i]) {
1187 if (!inode_in_use(i))
1189 printf("Inode %d not used, marked used in the bitmap.", i);
1190 if (ask("Clear", 1))
1194 if (!inode_in_use(i)) {
1195 printf("Inode %d used, marked unused in the bitmap.", i);
1199 if (Inode[i].i_nlinks != inode_count[i]) {
1200 printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
1201 i, Inode[i].i_mode, Inode[i].i_nlinks, inode_count[i]);
1202 if (ask("Set i_nlinks to count", 1)) {
1203 Inode[i].i_nlinks = inode_count[i];
1208 for (i = FIRSTZONE; i < ZONES; i++) {
1209 if (zone_in_use(i) == zone_count[i])
1211 if (!zone_count[i]) {
1214 printf("Zone %d: marked in use, no file uses it.", i);
1215 if (ask("Unmark", 1))
1219 printf("Zone %d: %sin use, counted=%d\n",
1220 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1224 #ifdef CONFIG_FEATURE_MINIX2
1225 static void check_counts2(void)
1229 for (i = 1; i <= INODES; i++) {
1230 if (!inode_in_use(i) && Inode2[i].i_mode && warn_mode) {
1231 printf("Inode %d mode not cleared.", i);
1232 if (ask("Clear", 1)) {
1233 Inode2[i].i_mode = 0;
1237 if (!inode_count[i]) {
1238 if (!inode_in_use(i))
1240 printf("Inode %d not used, marked used in the bitmap.", i);
1241 if (ask("Clear", 1))
1245 if (!inode_in_use(i)) {
1246 printf("Inode %d used, marked unused in the bitmap.", i);
1250 if (Inode2[i].i_nlinks != inode_count[i]) {
1251 printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
1252 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1254 if (ask("Set i_nlinks to count", 1)) {
1255 Inode2[i].i_nlinks = inode_count[i];
1260 for (i = FIRSTZONE; i < ZONES; i++) {
1261 if (zone_in_use(i) == zone_count[i])
1263 if (!zone_count[i]) {
1266 printf("Zone %d: marked in use, no file uses it.", i);
1267 if (ask("Unmark", 1))
1271 printf("Zone %d: %sin use, counted=%d\n",
1272 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1277 static void check(void)
1279 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1280 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1281 check_zones(ROOT_INO);
1282 recursive_check(ROOT_INO);
1286 #ifdef CONFIG_FEATURE_MINIX2
1287 static void check2(void)
1289 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1290 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1291 check_zones2(ROOT_INO);
1292 recursive_check2(ROOT_INO);
1297 /* Wed Feb 9 15:17:06 MST 2000 */
1298 /* dynamically allocate name_list (instead of making it static) */
1299 static void alloc_name_list(void)
1303 name_list = xmalloc(sizeof(char *) * MAX_DEPTH);
1304 for (i = 0; i < MAX_DEPTH; i++)
1305 name_list[i] = xmalloc(sizeof(char) * BUFSIZ + 1);
1308 #ifdef CONFIG_FEATURE_CLEAN_UP
1309 /* execute this atexit() to deallocate name_list[] */
1310 /* piptigger was here */
1311 static void free_name_list(void)
1316 for (i = 0; i < MAX_DEPTH; i++) {
1324 extern int fsck_minix_main(int argc, char **argv)
1331 #ifdef CONFIG_FEATURE_CLEAN_UP
1332 /* Don't bother to free memory. Exit does
1333 * that automagically, so we can save a few bytes */
1334 atexit(free_name_list);
1337 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
1338 die("bad inode size");
1339 #ifdef CONFIG_FEATURE_MINIX2
1340 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
1341 die("bad v2 inode size");
1343 while (argc-- > 1) {
1345 if (argv[0][0] != '-') {
1349 device_name = argv[0];
1352 switch (argv[0][0]) {
1382 check_mount(); /* trying to check a mounted filesystem? */
1383 if (repair && !automatic) {
1384 if (!isatty(0) || !isatty(1))
1385 die("need terminal for interactive repairs");
1387 IN = open(device_name, repair ? O_RDWR : O_RDONLY);
1389 fprintf(stderr,"unable to open device '%s'.\n",device_name);
1392 for (count = 0; count < 3; count++)
1397 * Determine whether or not we should continue with the checking.
1398 * This is based on the status of the filesystem valid and error
1399 * flags and whether or not the -f switch was specified on the
1402 printf("%s, %s\n", bb_applet_name, program_version);
1403 if (!(Super.s_state & MINIX_ERROR_FS) &&
1404 (Super.s_state & MINIX_VALID_FS) && !force) {
1406 printf("%s is clean, no check.\n", device_name);
1409 printf("Forcing filesystem check on %s.\n", device_name);
1411 printf("Filesystem on %s is dirty, needs checking.\n",
1416 if (repair && !automatic) {
1417 tcgetattr(0, &termios);
1419 tmp.c_lflag &= ~(ICANON | ECHO);
1420 tcsetattr(0, TCSANOW, &tmp);
1423 #ifdef CONFIG_FEATURE_MINIX2
1436 for (i = 1, free_cnt = 0; i <= INODES; i++)
1437 if (!inode_in_use(i))
1439 printf("\n%6ld inodes used (%ld%%)\n", (INODES - free_cnt),
1440 100 * (INODES - free_cnt) / INODES);
1441 for (i = FIRSTZONE, free_cnt = 0; i < ZONES; i++)
1442 if (!zone_in_use(i))
1444 printf("%6ld zones used (%ld%%)\n", (ZONES - free_cnt),
1445 100 * (ZONES - free_cnt) / ZONES);
1446 printf("\n%6d regular files\n"
1448 "%6d character device files\n"
1449 "%6d block device files\n"
1451 "%6d symbolic links\n"
1454 regular, directory, chardev, blockdev,
1455 links - 2 * directory + 1, symlinks,
1456 total - 2 * directory + 1);
1460 printf("----------------------------\n"
1461 "FILE SYSTEM HAS BEEN CHANGED\n"
1462 "----------------------------\n");
1463 for (count = 0; count < 3; count++)
1466 write_super_block();
1468 if (repair && !automatic)
1469 tcsetattr(0, TCSANOW, &termios);
1473 if (errors_uncorrected)