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 rudimetary 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 systemindependent.
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 * Usuage: 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 :-).
99 #include <sys/param.h>
102 typedef unsigned char u8;
103 typedef unsigned short u16;
104 typedef unsigned int u32;
107 #define MINIX_ROOT_INO 1
108 #define MINIX_LINK_MAX 250
109 #define MINIX2_LINK_MAX 65530
111 #define MINIX_I_MAP_SLOTS 8
112 #define MINIX_Z_MAP_SLOTS 64
113 #define MINIX_SUPER_MAGIC 0x137F /* original minix fs */
114 #define MINIX_SUPER_MAGIC2 0x138F /* minix fs, 30 char names */
115 #define MINIX2_SUPER_MAGIC 0x2468 /* minix V2 fs */
116 #define MINIX2_SUPER_MAGIC2 0x2478 /* minix V2 fs, 30 char names */
117 #define MINIX_VALID_FS 0x0001 /* Clean fs. */
118 #define MINIX_ERROR_FS 0x0002 /* fs has errors. */
120 #define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
121 #define MINIX2_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix2_inode)))
123 #define MINIX_V1 0x0001 /* original minix fs */
124 #define MINIX_V2 0x0002 /* minix V2 fs */
126 #define INODE_VERSION(inode) inode->i_sb->u.minix_sb.s_version
129 * This is the original minix inode layout on disk.
130 * Note the 8-bit gid and atime and ctime.
143 * The new minix inode has all the time entries, as well as
144 * long block numbers and a third indirect block (7+1+1+1
145 * instead of 7+1+1). Also, some previously 8-bit values are
146 * now 16-bit. The inode is now 64 bytes instead of 32.
148 struct minix2_inode {
161 * minix super-block data on disk
163 struct minix_super_block {
176 struct minix_dir_entry {
181 #define BLOCK_SIZE_BITS 10
182 #define BLOCK_SIZE (1<<BLOCK_SIZE_BITS)
184 #define NAME_MAX 255 /* # chars in a file name */
186 #define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
188 #define MINIX_VALID_FS 0x0001 /* Clean fs. */
189 #define MINIX_ERROR_FS 0x0002 /* fs has errors. */
191 #define MINIX_SUPER_MAGIC 0x137F /* original minix fs */
192 #define MINIX_SUPER_MAGIC2 0x138F /* minix fs, 30 char names */
195 #define BLKGETSIZE _IO(0x12,96) /* return device size */
204 #define UPPER(size,n) ((size+((n)-1))/(n))
205 #define INODE_SIZE (sizeof(struct minix_inode))
206 #ifdef BB_FEATURE_MINIX2
207 #define INODE_SIZE2 (sizeof(struct minix2_inode))
208 #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
209 : MINIX_INODES_PER_BLOCK))
211 #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
213 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
215 #define BITS_PER_BLOCK (BLOCK_SIZE<<3)
217 static char *program_version = "1.2 - 11/11/96";
218 static char *device_name = NULL;
220 static int repair = 0, automatic = 0, verbose = 0, list = 0, show =
221 0, warn_mode = 0, force = 0;
222 static int directory = 0, regular = 0, blockdev = 0, chardev = 0, links =
223 0, symlinks = 0, total = 0;
225 static int changed = 0; /* flags if the filesystem has been changed */
226 static int errors_uncorrected = 0; /* flag if some error was not corrected */
227 static int dirsize = 16;
228 static int namelen = 14;
229 static int version2 = 0;
230 static struct termios termios;
231 static int termios_set = 0;
235 static int name_depth = 0;
236 // static char name_list[MAX_DEPTH][BUFSIZ + 1];
237 static char **name_list = NULL;
239 static char *inode_buffer = NULL;
241 #define Inode (((struct minix_inode *) inode_buffer)-1)
242 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
243 static char super_block_buffer[BLOCK_SIZE];
245 #define Super (*(struct minix_super_block *)super_block_buffer)
246 #define INODES ((unsigned long)Super.s_ninodes)
247 #ifdef BB_FEATURE_MINIX2
248 #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
250 #define ZONES ((unsigned long)(Super.s_nzones))
252 #define IMAPS ((unsigned long)Super.s_imap_blocks)
253 #define ZMAPS ((unsigned long)Super.s_zmap_blocks)
254 #define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
255 #define ZONESIZE ((unsigned long)Super.s_log_zone_size)
256 #define MAXSIZE ((unsigned long)Super.s_max_size)
257 #define MAGIC (Super.s_magic)
258 #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
260 static char *inode_map;
261 static char *zone_map;
263 static unsigned char *inode_count = NULL;
264 static unsigned char *zone_count = NULL;
266 static void recursive_check(unsigned int ino);
267 #ifdef BB_FEATURE_MINIX2
268 static void recursive_check2(unsigned int ino);
271 static inline int bit(char * a,unsigned int i)
273 return (a[i >> 3] & (1<<(i & 7))) != 0;
275 #define inode_in_use(x) (bit(inode_map,(x)))
276 #define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
278 #define mark_inode(x) (setbit(inode_map,(x)),changed=1)
279 #define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
281 #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1)
282 #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1)
284 static void leave(int) __attribute__ ((noreturn));
285 static void leave(int status)
288 tcsetattr(0, TCSANOW, &termios);
292 static void show_usage(void)
294 usage(fsck_minix_usage);
297 static void die(const char *str)
299 error_msg("%s\n", str);
304 * This simply goes through the file-name data and prints out the
307 static void print_current_name(void)
311 while (i < name_depth)
312 printf("/%.*s", namelen, name_list[i++]);
317 static int ask(const char *string, int def)
323 errors_uncorrected = 1;
329 errors_uncorrected = 1;
332 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
335 if ((c = getchar()) == EOF) {
337 errors_uncorrected = 1;
344 } else if (c == 'N') {
347 } else if (c == ' ' || c == '\n')
354 errors_uncorrected = 1;
360 * Make certain that we aren't checking a filesystem that is on a
361 * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
362 * 1994 Theodore Ts'o. Also licensed under GPL.
364 static void check_mount(void)
371 if ((f = setmntent(MOUNTED, "r")) == NULL)
373 while ((mnt = getmntent(f)) != NULL)
374 if (strcmp(device_name, mnt->mnt_fsname) == 0)
381 * If the root is mounted read-only, then /etc/mtab is
382 * probably not correct; so we won't issue a warning based on
385 fd = open(MOUNTED, O_RDWR);
386 if (fd < 0 && errno == EROFS)
391 printf("%s is mounted. ", device_name);
392 if (isatty(0) && isatty(1))
393 cont = ask("Do you really want to continue", 0);
397 printf("check aborted.\n");
404 * check_zone_nr checks to see that *nr is a valid zone nr. If it
405 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
406 * if an error was corrected, and returns the zone (0 for no zone
407 * or a bad zone-number).
409 static int check_zone_nr(unsigned short *nr, int *corrected)
414 printf("Zone nr < FIRSTZONE in file `");
415 else if (*nr >= ZONES)
416 printf("Zone nr >= ZONES in file `");
419 print_current_name();
421 if (ask("Remove block", 1)) {
428 #ifdef BB_FEATURE_MINIX2
429 static int check_zone_nr2(unsigned int *nr, int *corrected)
434 printf("Zone nr < FIRSTZONE in file `");
435 else if (*nr >= ZONES)
436 printf("Zone nr >= ZONES in file `");
439 print_current_name();
441 if (ask("Remove block", 1)) {
450 * read-block reads block nr into the buffer at addr.
452 static void read_block(unsigned int nr, char *addr)
455 memset(addr, 0, BLOCK_SIZE);
458 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) {
459 printf("Read error: unable to seek to block in file '");
460 print_current_name();
462 memset(addr, 0, BLOCK_SIZE);
463 errors_uncorrected = 1;
464 } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) {
465 printf("Read error: bad block in file '");
466 print_current_name();
468 memset(addr, 0, BLOCK_SIZE);
469 errors_uncorrected = 1;
474 * write_block writes block nr to disk.
476 static void write_block(unsigned int nr, char *addr)
480 if (nr < FIRSTZONE || nr >= ZONES) {
481 printf("Internal error: trying to write bad block\n"
482 "Write request ignored\n");
483 errors_uncorrected = 1;
486 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET))
487 die("seek failed in write_block");
488 if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) {
489 printf("Write error: bad block in file '");
490 print_current_name();
492 errors_uncorrected = 1;
497 * map-block calculates the absolute block nr of a block in a file.
498 * It sets 'changed' if the inode has needed changing, and re-writes
499 * any indirect blocks with errors.
501 static int map_block(struct minix_inode *inode, unsigned int blknr)
503 unsigned short ind[BLOCK_SIZE >> 1];
504 unsigned short dind[BLOCK_SIZE >> 1];
505 int blk_chg, block, result;
508 return check_zone_nr(inode->i_zone + blknr, &changed);
511 block = check_zone_nr(inode->i_zone + 7, &changed);
512 read_block(block, (char *) ind);
514 result = check_zone_nr(blknr + ind, &blk_chg);
516 write_block(block, (char *) ind);
520 block = check_zone_nr(inode->i_zone + 8, &changed);
521 read_block(block, (char *) dind);
523 result = check_zone_nr(dind + (blknr / 512), &blk_chg);
525 write_block(block, (char *) dind);
527 read_block(block, (char *) ind);
529 result = check_zone_nr(ind + (blknr % 512), &blk_chg);
531 write_block(block, (char *) ind);
535 #ifdef BB_FEATURE_MINIX2
536 static int map_block2(struct minix2_inode *inode, unsigned int blknr)
538 unsigned int ind[BLOCK_SIZE >> 2];
539 unsigned int dind[BLOCK_SIZE >> 2];
540 unsigned int tind[BLOCK_SIZE >> 2];
541 int blk_chg, block, result;
544 return check_zone_nr2(inode->i_zone + blknr, &changed);
547 block = check_zone_nr2(inode->i_zone + 7, &changed);
548 read_block(block, (char *) ind);
550 result = check_zone_nr2(blknr + ind, &blk_chg);
552 write_block(block, (char *) ind);
556 if (blknr >= 256 * 256) {
557 block = check_zone_nr2(inode->i_zone + 8, &changed);
558 read_block(block, (char *) dind);
560 result = check_zone_nr2(dind + blknr / 256, &blk_chg);
562 write_block(block, (char *) dind);
564 read_block(block, (char *) ind);
566 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
568 write_block(block, (char *) ind);
572 block = check_zone_nr2(inode->i_zone + 9, &changed);
573 read_block(block, (char *) tind);
575 result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
577 write_block(block, (char *) tind);
579 read_block(block, (char *) dind);
581 result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
583 write_block(block, (char *) dind);
585 read_block(block, (char *) ind);
587 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
589 write_block(block, (char *) ind);
594 static void write_super_block(void)
597 * Set the state of the filesystem based on whether or not there
598 * are uncorrected errors. The filesystem valid flag is
599 * unconditionally set if we get this far.
601 Super.s_state |= MINIX_VALID_FS;
602 if (errors_uncorrected)
603 Super.s_state |= MINIX_ERROR_FS;
605 Super.s_state &= ~MINIX_ERROR_FS;
607 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
608 die("seek failed in write_super_block");
609 if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE))
610 die("unable to write super-block");
615 static void write_tables(void)
619 if (IMAPS * BLOCK_SIZE != write(IN, inode_map, IMAPS * BLOCK_SIZE))
620 die("Unable to write inode map");
621 if (ZMAPS * BLOCK_SIZE != write(IN, zone_map, ZMAPS * BLOCK_SIZE))
622 die("Unable to write zone map");
623 if (INODE_BUFFER_SIZE != write(IN, inode_buffer, INODE_BUFFER_SIZE))
624 die("Unable to write inodes");
627 static void get_dirsize(void)
630 char blk[BLOCK_SIZE];
633 #ifdef BB_FEATURE_MINIX2
635 block = Inode2[ROOT_INO].i_zone[0];
638 block = Inode[ROOT_INO].i_zone[0];
639 read_block(block, blk);
640 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
641 if (strcmp(blk + size + 2, "..") == 0) {
650 static void read_superblock(void)
652 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
654 if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
655 die("unable to read super block");
656 if (MAGIC == MINIX_SUPER_MAGIC) {
660 } else if (MAGIC == MINIX_SUPER_MAGIC2) {
664 #ifdef BB_FEATURE_MINIX2
665 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
669 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
675 die("bad magic number in super-block");
676 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
677 die("Only 1k blocks/zones supported");
678 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
679 die("bad s_imap_blocks field in super-block");
680 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
681 die("bad s_zmap_blocks field in super-block");
684 static void read_tables(void)
686 inode_map = xmalloc(IMAPS * BLOCK_SIZE);
687 zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
688 memset(inode_map, 0, sizeof(inode_map));
689 memset(zone_map, 0, sizeof(zone_map));
690 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
691 inode_count = xmalloc(INODES + 1);
692 zone_count = xmalloc(ZONES);
693 if (IMAPS * BLOCK_SIZE != read(IN, inode_map, IMAPS * BLOCK_SIZE))
694 die("Unable to read inode map");
695 if (ZMAPS * BLOCK_SIZE != read(IN, zone_map, ZMAPS * BLOCK_SIZE))
696 die("Unable to read zone map");
697 if (INODE_BUFFER_SIZE != read(IN, inode_buffer, INODE_BUFFER_SIZE))
698 die("Unable to read inodes");
699 if (NORM_FIRSTZONE != FIRSTZONE) {
700 printf("Warning: Firstzone != Norm_firstzone\n");
701 errors_uncorrected = 1;
705 printf("%ld inodes\n", INODES);
706 printf("%ld blocks\n", ZONES);
707 printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
708 printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
709 printf("Maxsize=%ld\n", MAXSIZE);
710 printf("Filesystem state=%d\n", Super.s_state);
711 printf("namelen=%d\n\n", namelen);
715 struct minix_inode *get_inode(unsigned int nr)
717 struct minix_inode *inode;
719 if (!nr || nr > INODES)
723 if (!inode_count[nr]) {
724 if (!inode_in_use(nr)) {
725 printf("Inode %d marked not used, but used for file '", nr);
726 print_current_name();
729 if (ask("Mark in use", 1))
732 errors_uncorrected = 1;
735 if (S_ISDIR(inode->i_mode))
737 else if (S_ISREG(inode->i_mode))
739 else if (S_ISCHR(inode->i_mode))
741 else if (S_ISBLK(inode->i_mode))
743 else if (S_ISLNK(inode->i_mode))
745 else if (S_ISSOCK(inode->i_mode));
746 else if (S_ISFIFO(inode->i_mode));
748 print_current_name();
749 printf(" has mode %05o\n", inode->i_mode);
754 if (!++inode_count[nr]) {
755 printf("Warning: inode count too big.\n");
757 errors_uncorrected = 1;
762 #ifdef BB_FEATURE_MINIX2
763 struct minix2_inode *get_inode2(unsigned int nr)
765 struct minix2_inode *inode;
767 if (!nr || nr > INODES)
771 if (!inode_count[nr]) {
772 if (!inode_in_use(nr)) {
773 printf("Inode %d marked not used, but used for file '", nr);
774 print_current_name();
777 if (ask("Mark in use", 1))
780 errors_uncorrected = 1;
783 if (S_ISDIR(inode->i_mode))
785 else if (S_ISREG(inode->i_mode))
787 else if (S_ISCHR(inode->i_mode))
789 else if (S_ISBLK(inode->i_mode))
791 else if (S_ISLNK(inode->i_mode))
793 else if (S_ISSOCK(inode->i_mode));
794 else if (S_ISFIFO(inode->i_mode));
796 print_current_name();
797 printf(" has mode %05o\n", inode->i_mode);
801 if (!++inode_count[nr]) {
802 printf("Warning: inode count too big.\n");
804 errors_uncorrected = 1;
810 static void check_root(void)
812 struct minix_inode *inode = Inode + ROOT_INO;
814 if (!inode || !S_ISDIR(inode->i_mode))
815 die("root inode isn't a directory");
818 #ifdef BB_FEATURE_MINIX2
819 static void check_root2(void)
821 struct minix2_inode *inode = Inode2 + ROOT_INO;
823 if (!inode || !S_ISDIR(inode->i_mode))
824 die("root inode isn't a directory");
828 static int add_zone(unsigned short *znr, int *corrected)
834 block = check_zone_nr(znr, corrected);
837 if (zone_count[block]) {
838 printf("Block has been used before. Now in file `");
839 print_current_name();
841 if (ask("Clear", 1)) {
849 if (!zone_in_use(block)) {
850 printf("Block %d in file `", block);
851 print_current_name();
852 printf("' is marked not in use.");
853 if (ask("Correct", 1))
856 if (!++zone_count[block])
861 #ifdef BB_FEATURE_MINIX2
862 static int add_zone2(unsigned int *znr, int *corrected)
868 block = check_zone_nr2(znr, corrected);
871 if (zone_count[block]) {
872 printf("Block has been used before. Now in file `");
873 print_current_name();
875 if (ask("Clear", 1)) {
883 if (!zone_in_use(block)) {
884 printf("Block %d in file `", block);
885 print_current_name();
886 printf("' is marked not in use.");
887 if (ask("Correct", 1))
890 if (!++zone_count[block])
896 static void add_zone_ind(unsigned short *znr, int *corrected)
898 static char blk[BLOCK_SIZE];
902 block = add_zone(znr, corrected);
905 read_block(block, blk);
906 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
907 add_zone(i + (unsigned short *) blk, &chg_blk);
909 write_block(block, blk);
912 #ifdef BB_FEATURE_MINIX2
913 static void add_zone_ind2(unsigned int *znr, int *corrected)
915 static char blk[BLOCK_SIZE];
919 block = add_zone2(znr, corrected);
922 read_block(block, blk);
923 for (i = 0; i < BLOCK_SIZE >> 2; i++)
924 add_zone2(i + (unsigned int *) blk, &chg_blk);
926 write_block(block, blk);
930 static void add_zone_dind(unsigned short *znr, int *corrected)
932 static char blk[BLOCK_SIZE];
936 block = add_zone(znr, corrected);
939 read_block(block, blk);
940 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
941 add_zone_ind(i + (unsigned short *) blk, &blk_chg);
943 write_block(block, blk);
946 #ifdef BB_FEATURE_MINIX2
947 static void add_zone_dind2(unsigned int *znr, int *corrected)
949 static char blk[BLOCK_SIZE];
953 block = add_zone2(znr, corrected);
956 read_block(block, blk);
957 for (i = 0; i < BLOCK_SIZE >> 2; i++)
958 add_zone_ind2(i + (unsigned int *) blk, &blk_chg);
960 write_block(block, blk);
963 static void add_zone_tind2(unsigned int *znr, int *corrected)
965 static char blk[BLOCK_SIZE];
969 block = add_zone2(znr, corrected);
972 read_block(block, blk);
973 for (i = 0; i < BLOCK_SIZE >> 2; i++)
974 add_zone_dind2(i + (unsigned int *) blk, &blk_chg);
976 write_block(block, blk);
980 static void check_zones(unsigned int i)
982 struct minix_inode *inode;
984 if (!i || i > INODES)
986 if (inode_count[i] > 1) /* have we counted this file already? */
989 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
990 !S_ISLNK(inode->i_mode)) return;
991 for (i = 0; i < 7; i++)
992 add_zone(i + inode->i_zone, &changed);
993 add_zone_ind(7 + inode->i_zone, &changed);
994 add_zone_dind(8 + inode->i_zone, &changed);
997 #ifdef BB_FEATURE_MINIX2
998 static void check_zones2(unsigned int i)
1000 struct minix2_inode *inode;
1002 if (!i || i > INODES)
1004 if (inode_count[i] > 1) /* have we counted this file already? */
1007 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
1008 && !S_ISLNK(inode->i_mode))
1010 for (i = 0; i < 7; i++)
1011 add_zone2(i + inode->i_zone, &changed);
1012 add_zone_ind2(7 + inode->i_zone, &changed);
1013 add_zone_dind2(8 + inode->i_zone, &changed);
1014 add_zone_tind2(9 + inode->i_zone, &changed);
1018 static void check_file(struct minix_inode *dir, unsigned int offset)
1020 static char blk[BLOCK_SIZE];
1021 struct minix_inode *inode;
1026 block = map_block(dir, offset / BLOCK_SIZE);
1027 read_block(block, blk);
1028 name = blk + (offset % BLOCK_SIZE) + 2;
1029 ino = *(unsigned short *) (name - 2);
1031 print_current_name();
1032 printf(" contains a bad inode number for file '");
1033 printf("%.*s'.", namelen, name);
1034 if (ask(" Remove", 1)) {
1035 *(unsigned short *) (name - 2) = 0;
1036 write_block(block, blk);
1040 if (name_depth < MAX_DEPTH)
1041 strncpy(name_list[name_depth], name, namelen);
1043 inode = get_inode(ino);
1046 if (!inode || strcmp(".", name)) {
1047 print_current_name();
1048 printf(": bad directory: '.' isn't first\n");
1049 errors_uncorrected = 1;
1053 if (offset == dirsize) {
1054 if (!inode || strcmp("..", name)) {
1055 print_current_name();
1056 printf(": bad directory: '..' isn't second\n");
1057 errors_uncorrected = 1;
1063 if (name_depth < MAX_DEPTH)
1064 strncpy(name_list[name_depth], name, namelen);
1068 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1069 print_current_name();
1070 if (S_ISDIR(inode->i_mode))
1076 if (inode && S_ISDIR(inode->i_mode))
1077 recursive_check(ino);
1082 #ifdef BB_FEATURE_MINIX2
1083 static void check_file2(struct minix2_inode *dir, unsigned int offset)
1085 static char blk[BLOCK_SIZE];
1086 struct minix2_inode *inode;
1091 block = map_block2(dir, offset / BLOCK_SIZE);
1092 read_block(block, blk);
1093 name = blk + (offset % BLOCK_SIZE) + 2;
1094 ino = *(unsigned short *) (name - 2);
1096 print_current_name();
1097 printf(" contains a bad inode number for file '");
1098 printf("%.*s'.", namelen, name);
1099 if (ask(" Remove", 1)) {
1100 *(unsigned short *) (name - 2) = 0;
1101 write_block(block, blk);
1105 if (name_depth < MAX_DEPTH)
1106 strncpy(name_list[name_depth], name, namelen);
1108 inode = get_inode2(ino);
1111 if (!inode || strcmp(".", name)) {
1112 print_current_name();
1113 printf(": bad directory: '.' isn't first\n");
1114 errors_uncorrected = 1;
1118 if (offset == dirsize) {
1119 if (!inode || strcmp("..", name)) {
1120 print_current_name();
1121 printf(": bad directory: '..' isn't second\n");
1122 errors_uncorrected = 1;
1131 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1132 print_current_name();
1133 if (S_ISDIR(inode->i_mode))
1139 if (inode && S_ISDIR(inode->i_mode))
1140 recursive_check2(ino);
1146 static void recursive_check(unsigned int ino)
1148 struct minix_inode *dir;
1149 unsigned int offset;
1152 if (!S_ISDIR(dir->i_mode))
1153 die("internal error");
1154 if (dir->i_size < 2 * dirsize) {
1155 print_current_name();
1156 printf(": bad directory: size<32");
1157 errors_uncorrected = 1;
1159 for (offset = 0; offset < dir->i_size; offset += dirsize)
1160 check_file(dir, offset);
1163 #ifdef BB_FEATURE_MINIX2
1164 static void recursive_check2(unsigned int ino)
1166 struct minix2_inode *dir;
1167 unsigned int offset;
1170 if (!S_ISDIR(dir->i_mode))
1171 die("internal error");
1172 if (dir->i_size < 2 * dirsize) {
1173 print_current_name();
1174 printf(": bad directory: size < 32");
1175 errors_uncorrected = 1;
1177 for (offset = 0; offset < dir->i_size; offset += dirsize)
1178 check_file2(dir, offset);
1182 static int bad_zone(int i)
1186 if (BLOCK_SIZE * i != lseek(IN, BLOCK_SIZE * i, SEEK_SET))
1187 die("seek failed in bad_zone");
1188 return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE));
1191 static void check_counts(void)
1195 for (i = 1; i <= INODES; i++) {
1196 if (!inode_in_use(i) && Inode[i].i_mode && warn_mode) {
1197 printf("Inode %d mode not cleared.", i);
1198 if (ask("Clear", 1)) {
1199 Inode[i].i_mode = 0;
1203 if (!inode_count[i]) {
1204 if (!inode_in_use(i))
1206 printf("Inode %d not used, marked used in the bitmap.", i);
1207 if (ask("Clear", 1))
1211 if (!inode_in_use(i)) {
1212 printf("Inode %d used, marked unused in the bitmap.", i);
1216 if (Inode[i].i_nlinks != inode_count[i]) {
1217 printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
1218 i, Inode[i].i_mode, Inode[i].i_nlinks, inode_count[i]);
1219 if (ask("Set i_nlinks to count", 1)) {
1220 Inode[i].i_nlinks = inode_count[i];
1225 for (i = FIRSTZONE; i < ZONES; i++) {
1226 if (zone_in_use(i) == zone_count[i])
1228 if (!zone_count[i]) {
1231 printf("Zone %d: marked in use, no file uses it.", i);
1232 if (ask("Unmark", 1))
1236 printf("Zone %d: %sin use, counted=%d\n",
1237 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1241 #ifdef BB_FEATURE_MINIX2
1242 static void check_counts2(void)
1246 for (i = 1; i <= INODES; i++) {
1247 if (!inode_in_use(i) && Inode2[i].i_mode && warn_mode) {
1248 printf("Inode %d mode not cleared.", i);
1249 if (ask("Clear", 1)) {
1250 Inode2[i].i_mode = 0;
1254 if (!inode_count[i]) {
1255 if (!inode_in_use(i))
1257 printf("Inode %d not used, marked used in the bitmap.", i);
1258 if (ask("Clear", 1))
1262 if (!inode_in_use(i)) {
1263 printf("Inode %d used, marked unused in the bitmap.", i);
1267 if (Inode2[i].i_nlinks != inode_count[i]) {
1268 printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
1269 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1271 if (ask("Set i_nlinks to count", 1)) {
1272 Inode2[i].i_nlinks = inode_count[i];
1277 for (i = FIRSTZONE; i < ZONES; i++) {
1278 if (zone_in_use(i) == zone_count[i])
1280 if (!zone_count[i]) {
1283 printf("Zone %d: marked in use, no file uses it.", i);
1284 if (ask("Unmark", 1))
1288 printf("Zone %d: %sin use, counted=%d\n",
1289 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1294 static void check(void)
1296 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1297 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1298 check_zones(ROOT_INO);
1299 recursive_check(ROOT_INO);
1303 #ifdef BB_FEATURE_MINIX2
1304 static void check2(void)
1306 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1307 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1308 check_zones2(ROOT_INO);
1309 recursive_check2(ROOT_INO);
1314 /* Wed Feb 9 15:17:06 MST 2000 */
1315 /* dynamically allocate name_list (instead of making it static) */
1316 static void alloc_name_list(void)
1320 name_list = xmalloc(sizeof(char *) * MAX_DEPTH);
1321 for (i = 0; i < MAX_DEPTH; i++)
1322 name_list[i] = xmalloc(sizeof(char) * BUFSIZ + 1);
1325 #ifdef BB_FEATURE_CLEAN_UP
1326 /* execute this atexit() to deallocate name_list[] */
1327 /* piptigger was here */
1328 static void free_name_list(void)
1333 for (i = 0; i < MAX_DEPTH; i++) {
1343 extern int fsck_minix_main(int argc, char **argv)
1350 #ifdef BB_FEATURE_CLEAN_UP
1351 /* Don't bother to free memory. Exit does
1352 * that automagically, so we can save a few bytes */
1353 atexit(free_name_list);
1356 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
1357 die("bad inode size");
1358 #ifdef BB_FEATURE_MINIX2
1359 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
1360 die("bad v2 inode size");
1362 while (argc-- > 1) {
1364 if (argv[0][0] != '-') {
1368 device_name = argv[0];
1371 switch (argv[0][0]) {
1401 check_mount(); /* trying to check a mounted filesystem? */
1402 if (repair && !automatic) {
1403 if (!isatty(0) || !isatty(1))
1404 die("need terminal for interactive repairs");
1406 IN = open(device_name, repair ? O_RDWR : O_RDONLY);
1408 die("unable to open '%s'");
1409 for (count = 0; count < 3; count++)
1414 * Determine whether or not we should continue with the checking.
1415 * This is based on the status of the filesystem valid and error
1416 * flags and whether or not the -f switch was specified on the
1419 printf("%s, %s\n", applet_name, program_version);
1420 if (!(Super.s_state & MINIX_ERROR_FS) &&
1421 (Super.s_state & MINIX_VALID_FS) && !force) {
1423 printf("%s is clean, no check.\n", device_name);
1426 printf("Forcing filesystem check on %s.\n", device_name);
1428 printf("Filesystem on %s is dirty, needs checking.\n",
1433 if (repair && !automatic) {
1434 tcgetattr(0, &termios);
1436 tmp.c_lflag &= ~(ICANON | ECHO);
1437 tcsetattr(0, TCSANOW, &tmp);
1440 #ifdef BB_FEATURE_MINIX2
1453 for (i = 1, free = 0; i <= INODES; i++)
1454 if (!inode_in_use(i))
1456 printf("\n%6ld inodes used (%ld%%)\n", (INODES - free),
1457 100 * (INODES - free) / INODES);
1458 for (i = FIRSTZONE, free = 0; i < ZONES; i++)
1459 if (!zone_in_use(i))
1461 printf("%6ld zones used (%ld%%)\n", (ZONES - free),
1462 100 * (ZONES - free) / ZONES);
1463 printf("\n%6d regular files\n"
1465 "%6d character device files\n"
1466 "%6d block device files\n"
1468 "%6d symbolic links\n"
1471 regular, directory, chardev, blockdev,
1472 links - 2 * directory + 1, symlinks,
1473 total - 2 * directory + 1);
1477 printf("----------------------------\n"
1478 "FILE SYSTEM HAS BEEN CHANGED\n"
1479 "----------------------------\n");
1480 for (count = 0; count < 3; count++)
1483 write_super_block();
1485 if (repair && !automatic)
1486 tcsetattr(0, TCSANOW, &termios);
1490 if (errors_uncorrected)