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 :-).
100 #include <sys/param.h>
103 typedef unsigned char u8;
104 typedef unsigned short u16;
105 typedef unsigned int u32;
108 #define MINIX_ROOT_INO 1
109 #define MINIX_LINK_MAX 250
110 #define MINIX2_LINK_MAX 65530
112 #define MINIX_I_MAP_SLOTS 8
113 #define MINIX_Z_MAP_SLOTS 64
114 #define MINIX_SUPER_MAGIC 0x137F /* original minix fs */
115 #define MINIX_SUPER_MAGIC2 0x138F /* minix fs, 30 char names */
116 #define MINIX2_SUPER_MAGIC 0x2468 /* minix V2 fs */
117 #define MINIX2_SUPER_MAGIC2 0x2478 /* minix V2 fs, 30 char names */
118 #define MINIX_VALID_FS 0x0001 /* Clean fs. */
119 #define MINIX_ERROR_FS 0x0002 /* fs has errors. */
121 #define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
122 #define MINIX2_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix2_inode)))
124 #define MINIX_V1 0x0001 /* original minix fs */
125 #define MINIX_V2 0x0002 /* minix V2 fs */
127 #define INODE_VERSION(inode) inode->i_sb->u.minix_sb.s_version
130 * This is the original minix inode layout on disk.
131 * Note the 8-bit gid and atime and ctime.
144 * The new minix inode has all the time entries, as well as
145 * long block numbers and a third indirect block (7+1+1+1
146 * instead of 7+1+1). Also, some previously 8-bit values are
147 * now 16-bit. The inode is now 64 bytes instead of 32.
149 struct minix2_inode {
162 * minix super-block data on disk
164 struct minix_super_block {
177 struct minix_dir_entry {
182 #define BLOCK_SIZE_BITS 10
183 #define BLOCK_SIZE (1<<BLOCK_SIZE_BITS)
185 #define NAME_MAX 255 /* # chars in a file name */
187 #define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
189 #define MINIX_VALID_FS 0x0001 /* Clean fs. */
190 #define MINIX_ERROR_FS 0x0002 /* fs has errors. */
192 #define MINIX_SUPER_MAGIC 0x137F /* original minix fs */
193 #define MINIX_SUPER_MAGIC2 0x138F /* minix fs, 30 char names */
196 #define BLKGETSIZE _IO(0x12,96) /* return device size */
205 #define UPPER(size,n) ((size+((n)-1))/(n))
206 #define INODE_SIZE (sizeof(struct minix_inode))
207 #ifdef BB_FEATURE_MINIX2
208 #define INODE_SIZE2 (sizeof(struct minix2_inode))
209 #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
210 : MINIX_INODES_PER_BLOCK))
212 #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
214 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
216 #define BITS_PER_BLOCK (BLOCK_SIZE<<3)
218 static char *program_version = "1.2 - 11/11/96";
219 static char *device_name = NULL;
221 static int repair = 0, automatic = 0, verbose = 0, list = 0, show =
222 0, warn_mode = 0, force = 0;
223 static int directory = 0, regular = 0, blockdev = 0, chardev = 0, links =
224 0, symlinks = 0, total = 0;
226 static int changed = 0; /* flags if the filesystem has been changed */
227 static int errors_uncorrected = 0; /* flag if some error was not corrected */
228 static int dirsize = 16;
229 static int namelen = 14;
230 static int version2 = 0;
231 static struct termios termios;
232 static int termios_set = 0;
236 static int name_depth = 0;
237 // static char name_list[MAX_DEPTH][BUFSIZ + 1];
238 static char **name_list = NULL;
240 static char *inode_buffer = NULL;
242 #define Inode (((struct minix_inode *) inode_buffer)-1)
243 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
244 static char super_block_buffer[BLOCK_SIZE];
246 #define Super (*(struct minix_super_block *)super_block_buffer)
247 #define INODES ((unsigned long)Super.s_ninodes)
248 #ifdef BB_FEATURE_MINIX2
249 #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
251 #define ZONES ((unsigned long)(Super.s_nzones))
253 #define IMAPS ((unsigned long)Super.s_imap_blocks)
254 #define ZMAPS ((unsigned long)Super.s_zmap_blocks)
255 #define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
256 #define ZONESIZE ((unsigned long)Super.s_log_zone_size)
257 #define MAXSIZE ((unsigned long)Super.s_max_size)
258 #define MAGIC (Super.s_magic)
259 #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
261 static char *inode_map;
262 static char *zone_map;
264 static unsigned char *inode_count = NULL;
265 static unsigned char *zone_count = NULL;
267 static void recursive_check(unsigned int ino);
268 #ifdef BB_FEATURE_MINIX2
269 static void recursive_check2(unsigned int ino);
272 #define inode_in_use(x) (isset(inode_map,(x)))
273 #define zone_in_use(x) (isset(zone_map,(x)-FIRSTZONE+1))
275 #define mark_inode(x) (setbit(inode_map,(x)),changed=1)
276 #define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
278 #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1)
279 #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1)
281 static void leave(int) __attribute__ ((noreturn));
282 static void leave(int status)
285 tcsetattr(0, TCSANOW, &termios);
289 static void show_usage(void)
291 usage(fsck_minix_usage);
294 static void die(const char *str)
296 errorMsg("%s\n", str);
301 * This simply goes through the file-name data and prints out the
304 static void print_current_name(void)
308 while (i < name_depth)
309 printf("/%.*s", namelen, name_list[i++]);
314 static int ask(const char *string, int def)
320 errors_uncorrected = 1;
326 errors_uncorrected = 1;
329 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
332 if ((c = getchar()) == EOF) {
334 errors_uncorrected = 1;
341 } else if (c == 'N') {
344 } else if (c == ' ' || c == '\n')
351 errors_uncorrected = 1;
357 * Make certain that we aren't checking a filesystem that is on a
358 * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
359 * 1994 Theodore Ts'o. Also licensed under GPL.
361 static void check_mount(void)
368 if ((f = setmntent(MOUNTED, "r")) == NULL)
370 while ((mnt = getmntent(f)) != NULL)
371 if (strcmp(device_name, mnt->mnt_fsname) == 0)
378 * If the root is mounted read-only, then /etc/mtab is
379 * probably not correct; so we won't issue a warning based on
382 fd = open(MOUNTED, O_RDWR);
383 if (fd < 0 && errno == EROFS)
388 printf("%s is mounted. ", device_name);
389 if (isatty(0) && isatty(1))
390 cont = ask("Do you really want to continue", 0);
394 printf("check aborted.\n");
401 * check_zone_nr checks to see that *nr is a valid zone nr. If it
402 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
403 * if an error was corrected, and returns the zone (0 for no zone
404 * or a bad zone-number).
406 static int check_zone_nr(unsigned short *nr, int *corrected)
411 printf("Zone nr < FIRSTZONE in file `");
412 else if (*nr >= ZONES)
413 printf("Zone nr >= ZONES in file `");
416 print_current_name();
418 if (ask("Remove block", 1)) {
425 #ifdef BB_FEATURE_MINIX2
426 static int check_zone_nr2(unsigned int *nr, int *corrected)
431 printf("Zone nr < FIRSTZONE in file `");
432 else if (*nr >= ZONES)
433 printf("Zone nr >= ZONES in file `");
436 print_current_name();
438 if (ask("Remove block", 1)) {
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("Read error: unable to seek to block in file '");
457 print_current_name();
459 memset(addr, 0, BLOCK_SIZE);
460 errors_uncorrected = 1;
461 } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) {
462 printf("Read error: bad block in file '");
463 print_current_name();
465 memset(addr, 0, BLOCK_SIZE);
466 errors_uncorrected = 1;
471 * write_block writes block nr to disk.
473 static void write_block(unsigned int nr, char *addr)
477 if (nr < FIRSTZONE || nr >= ZONES) {
478 printf("Internal error: trying to write bad block\n"
479 "Write request ignored\n");
480 errors_uncorrected = 1;
483 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET))
484 die("seek failed in write_block");
485 if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) {
486 printf("Write error: bad block in file '");
487 print_current_name();
489 errors_uncorrected = 1;
494 * map-block calculates the absolute block nr of a block in a file.
495 * It sets 'changed' if the inode has needed changing, and re-writes
496 * any indirect blocks with errors.
498 static int map_block(struct minix_inode *inode, unsigned int blknr)
500 unsigned short ind[BLOCK_SIZE >> 1];
501 unsigned short dind[BLOCK_SIZE >> 1];
502 int blk_chg, block, result;
505 return check_zone_nr(inode->i_zone + blknr, &changed);
508 block = check_zone_nr(inode->i_zone + 7, &changed);
509 read_block(block, (char *) ind);
511 result = check_zone_nr(blknr + ind, &blk_chg);
513 write_block(block, (char *) ind);
517 block = check_zone_nr(inode->i_zone + 8, &changed);
518 read_block(block, (char *) dind);
520 result = check_zone_nr(dind + (blknr / 512), &blk_chg);
522 write_block(block, (char *) dind);
524 read_block(block, (char *) ind);
526 result = check_zone_nr(ind + (blknr % 512), &blk_chg);
528 write_block(block, (char *) ind);
532 #ifdef BB_FEATURE_MINIX2
533 static int map_block2(struct minix2_inode *inode, unsigned int blknr)
535 unsigned int ind[BLOCK_SIZE >> 2];
536 unsigned int dind[BLOCK_SIZE >> 2];
537 unsigned int tind[BLOCK_SIZE >> 2];
538 int blk_chg, block, result;
541 return check_zone_nr2(inode->i_zone + blknr, &changed);
544 block = check_zone_nr2(inode->i_zone + 7, &changed);
545 read_block(block, (char *) ind);
547 result = check_zone_nr2(blknr + ind, &blk_chg);
549 write_block(block, (char *) ind);
553 if (blknr >= 256 * 256) {
554 block = check_zone_nr2(inode->i_zone + 8, &changed);
555 read_block(block, (char *) dind);
557 result = check_zone_nr2(dind + blknr / 256, &blk_chg);
559 write_block(block, (char *) dind);
561 read_block(block, (char *) ind);
563 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
565 write_block(block, (char *) ind);
569 block = check_zone_nr2(inode->i_zone + 9, &changed);
570 read_block(block, (char *) tind);
572 result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
574 write_block(block, (char *) tind);
576 read_block(block, (char *) dind);
578 result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
580 write_block(block, (char *) dind);
582 read_block(block, (char *) ind);
584 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
586 write_block(block, (char *) ind);
591 static void write_super_block(void)
594 * Set the state of the filesystem based on whether or not there
595 * are uncorrected errors. The filesystem valid flag is
596 * unconditionally set if we get this far.
598 Super.s_state |= MINIX_VALID_FS;
599 if (errors_uncorrected)
600 Super.s_state |= MINIX_ERROR_FS;
602 Super.s_state &= ~MINIX_ERROR_FS;
604 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
605 die("seek failed in write_super_block");
606 if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE))
607 die("unable to write super-block");
612 static void write_tables(void)
616 if (IMAPS * BLOCK_SIZE != write(IN, inode_map, IMAPS * BLOCK_SIZE))
617 die("Unable to write inode map");
618 if (ZMAPS * BLOCK_SIZE != write(IN, zone_map, ZMAPS * BLOCK_SIZE))
619 die("Unable to write zone map");
620 if (INODE_BUFFER_SIZE != write(IN, inode_buffer, INODE_BUFFER_SIZE))
621 die("Unable to write inodes");
624 static void get_dirsize(void)
627 char blk[BLOCK_SIZE];
630 #ifdef BB_FEATURE_MINIX2
632 block = Inode2[ROOT_INO].i_zone[0];
635 block = Inode[ROOT_INO].i_zone[0];
636 read_block(block, blk);
637 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
638 if (strcmp(blk + size + 2, "..") == 0) {
647 static void read_superblock(void)
649 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
651 if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
652 die("unable to read super block");
653 if (MAGIC == MINIX_SUPER_MAGIC) {
657 } else if (MAGIC == MINIX_SUPER_MAGIC2) {
661 #ifdef BB_FEATURE_MINIX2
662 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
666 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
672 die("bad magic number in super-block");
673 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
674 die("Only 1k blocks/zones supported");
675 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
676 die("bad s_imap_blocks field in super-block");
677 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
678 die("bad s_zmap_blocks field in super-block");
681 static void read_tables(void)
683 inode_map = xmalloc(IMAPS * BLOCK_SIZE);
684 zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
685 memset(inode_map, 0, sizeof(inode_map));
686 memset(zone_map, 0, sizeof(zone_map));
687 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
688 inode_count = xmalloc(INODES + 1);
689 zone_count = xmalloc(ZONES);
690 if (IMAPS * BLOCK_SIZE != read(IN, inode_map, IMAPS * BLOCK_SIZE))
691 die("Unable to read inode map");
692 if (ZMAPS * BLOCK_SIZE != read(IN, zone_map, ZMAPS * BLOCK_SIZE))
693 die("Unable to read zone map");
694 if (INODE_BUFFER_SIZE != read(IN, inode_buffer, INODE_BUFFER_SIZE))
695 die("Unable to read inodes");
696 if (NORM_FIRSTZONE != FIRSTZONE) {
697 printf("Warning: Firstzone != Norm_firstzone\n");
698 errors_uncorrected = 1;
702 printf("%ld inodes\n", INODES);
703 printf("%ld blocks\n", ZONES);
704 printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
705 printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
706 printf("Maxsize=%ld\n", MAXSIZE);
707 printf("Filesystem state=%d\n", Super.s_state);
708 printf("namelen=%d\n\n", namelen);
712 struct minix_inode *get_inode(unsigned int nr)
714 struct minix_inode *inode;
716 if (!nr || nr > INODES)
720 if (!inode_count[nr]) {
721 if (!inode_in_use(nr)) {
722 printf("Inode %d marked not used, but used for file '", nr);
723 print_current_name();
726 if (ask("Mark in use", 1))
729 errors_uncorrected = 1;
732 if (S_ISDIR(inode->i_mode))
734 else if (S_ISREG(inode->i_mode))
736 else if (S_ISCHR(inode->i_mode))
738 else if (S_ISBLK(inode->i_mode))
740 else if (S_ISLNK(inode->i_mode))
742 else if (S_ISSOCK(inode->i_mode));
743 else if (S_ISFIFO(inode->i_mode));
745 print_current_name();
746 printf(" has mode %05o\n", inode->i_mode);
751 if (!++inode_count[nr]) {
752 printf("Warning: inode count too big.\n");
754 errors_uncorrected = 1;
759 #ifdef BB_FEATURE_MINIX2
760 struct minix2_inode *get_inode2(unsigned int nr)
762 struct minix2_inode *inode;
764 if (!nr || nr > INODES)
768 if (!inode_count[nr]) {
769 if (!inode_in_use(nr)) {
770 printf("Inode %d marked not used, but used for file '", nr);
771 print_current_name();
774 if (ask("Mark in use", 1))
777 errors_uncorrected = 1;
780 if (S_ISDIR(inode->i_mode))
782 else if (S_ISREG(inode->i_mode))
784 else if (S_ISCHR(inode->i_mode))
786 else if (S_ISBLK(inode->i_mode))
788 else if (S_ISLNK(inode->i_mode))
790 else if (S_ISSOCK(inode->i_mode));
791 else if (S_ISFIFO(inode->i_mode));
793 print_current_name();
794 printf(" has mode %05o\n", inode->i_mode);
798 if (!++inode_count[nr]) {
799 printf("Warning: inode count too big.\n");
801 errors_uncorrected = 1;
807 static void check_root(void)
809 struct minix_inode *inode = Inode + ROOT_INO;
811 if (!inode || !S_ISDIR(inode->i_mode))
812 die("root inode isn't a directory");
815 #ifdef BB_FEATURE_MINIX2
816 static void check_root2(void)
818 struct minix2_inode *inode = Inode2 + ROOT_INO;
820 if (!inode || !S_ISDIR(inode->i_mode))
821 die("root inode isn't a directory");
825 static int add_zone(unsigned short *znr, int *corrected)
831 block = check_zone_nr(znr, corrected);
834 if (zone_count[block]) {
835 printf("Block has been used before. Now in file `");
836 print_current_name();
838 if (ask("Clear", 1)) {
846 if (!zone_in_use(block)) {
847 printf("Block %d in file `", block);
848 print_current_name();
849 printf("' is marked not in use.");
850 if (ask("Correct", 1))
853 if (!++zone_count[block])
858 #ifdef BB_FEATURE_MINIX2
859 static int add_zone2(unsigned int *znr, int *corrected)
865 block = check_zone_nr2(znr, corrected);
868 if (zone_count[block]) {
869 printf("Block has been used before. Now in file `");
870 print_current_name();
872 if (ask("Clear", 1)) {
880 if (!zone_in_use(block)) {
881 printf("Block %d in file `", block);
882 print_current_name();
883 printf("' is marked not in use.");
884 if (ask("Correct", 1))
887 if (!++zone_count[block])
893 static void add_zone_ind(unsigned short *znr, int *corrected)
895 static char blk[BLOCK_SIZE];
899 block = add_zone(znr, corrected);
902 read_block(block, blk);
903 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
904 add_zone(i + (unsigned short *) blk, &chg_blk);
906 write_block(block, blk);
909 #ifdef BB_FEATURE_MINIX2
910 static void add_zone_ind2(unsigned int *znr, int *corrected)
912 static char blk[BLOCK_SIZE];
916 block = add_zone2(znr, corrected);
919 read_block(block, blk);
920 for (i = 0; i < BLOCK_SIZE >> 2; i++)
921 add_zone2(i + (unsigned int *) blk, &chg_blk);
923 write_block(block, blk);
927 static void add_zone_dind(unsigned short *znr, int *corrected)
929 static char blk[BLOCK_SIZE];
933 block = add_zone(znr, corrected);
936 read_block(block, blk);
937 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
938 add_zone_ind(i + (unsigned short *) blk, &blk_chg);
940 write_block(block, blk);
943 #ifdef BB_FEATURE_MINIX2
944 static void add_zone_dind2(unsigned int *znr, int *corrected)
946 static char blk[BLOCK_SIZE];
950 block = add_zone2(znr, corrected);
953 read_block(block, blk);
954 for (i = 0; i < BLOCK_SIZE >> 2; i++)
955 add_zone_ind2(i + (unsigned int *) blk, &blk_chg);
957 write_block(block, blk);
960 static void add_zone_tind2(unsigned int *znr, int *corrected)
962 static char blk[BLOCK_SIZE];
966 block = add_zone2(znr, corrected);
969 read_block(block, blk);
970 for (i = 0; i < BLOCK_SIZE >> 2; i++)
971 add_zone_dind2(i + (unsigned int *) blk, &blk_chg);
973 write_block(block, blk);
977 static void check_zones(unsigned int i)
979 struct minix_inode *inode;
981 if (!i || i > INODES)
983 if (inode_count[i] > 1) /* have we counted this file already? */
986 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
987 !S_ISLNK(inode->i_mode)) return;
988 for (i = 0; i < 7; i++)
989 add_zone(i + inode->i_zone, &changed);
990 add_zone_ind(7 + inode->i_zone, &changed);
991 add_zone_dind(8 + inode->i_zone, &changed);
994 #ifdef BB_FEATURE_MINIX2
995 static void check_zones2(unsigned int i)
997 struct minix2_inode *inode;
999 if (!i || i > INODES)
1001 if (inode_count[i] > 1) /* have we counted this file already? */
1004 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
1005 && !S_ISLNK(inode->i_mode))
1007 for (i = 0; i < 7; i++)
1008 add_zone2(i + inode->i_zone, &changed);
1009 add_zone_ind2(7 + inode->i_zone, &changed);
1010 add_zone_dind2(8 + inode->i_zone, &changed);
1011 add_zone_tind2(9 + inode->i_zone, &changed);
1015 static void check_file(struct minix_inode *dir, unsigned int offset)
1017 static char blk[BLOCK_SIZE];
1018 struct minix_inode *inode;
1023 block = map_block(dir, offset / BLOCK_SIZE);
1024 read_block(block, blk);
1025 name = blk + (offset % BLOCK_SIZE) + 2;
1026 ino = *(unsigned short *) (name - 2);
1028 print_current_name();
1029 printf(" contains a bad inode number for file '");
1030 printf("%.*s'.", namelen, name);
1031 if (ask(" Remove", 1)) {
1032 *(unsigned short *) (name - 2) = 0;
1033 write_block(block, blk);
1037 if (name_depth < MAX_DEPTH)
1038 strncpy(name_list[name_depth], name, namelen);
1040 inode = get_inode(ino);
1043 if (!inode || strcmp(".", name)) {
1044 print_current_name();
1045 printf(": bad directory: '.' isn't first\n");
1046 errors_uncorrected = 1;
1050 if (offset == dirsize) {
1051 if (!inode || strcmp("..", name)) {
1052 print_current_name();
1053 printf(": bad directory: '..' isn't second\n");
1054 errors_uncorrected = 1;
1060 if (name_depth < MAX_DEPTH)
1061 strncpy(name_list[name_depth], name, namelen);
1065 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1066 print_current_name();
1067 if (S_ISDIR(inode->i_mode))
1073 if (inode && S_ISDIR(inode->i_mode))
1074 recursive_check(ino);
1079 #ifdef BB_FEATURE_MINIX2
1080 static void check_file2(struct minix2_inode *dir, unsigned int offset)
1082 static char blk[BLOCK_SIZE];
1083 struct minix2_inode *inode;
1088 block = map_block2(dir, offset / BLOCK_SIZE);
1089 read_block(block, blk);
1090 name = blk + (offset % BLOCK_SIZE) + 2;
1091 ino = *(unsigned short *) (name - 2);
1093 print_current_name();
1094 printf(" contains a bad inode number for file '");
1095 printf("%.*s'.", namelen, name);
1096 if (ask(" Remove", 1)) {
1097 *(unsigned short *) (name - 2) = 0;
1098 write_block(block, blk);
1102 if (name_depth < MAX_DEPTH)
1103 strncpy(name_list[name_depth], name, namelen);
1105 inode = get_inode2(ino);
1108 if (!inode || strcmp(".", name)) {
1109 print_current_name();
1110 printf(": bad directory: '.' isn't first\n");
1111 errors_uncorrected = 1;
1115 if (offset == dirsize) {
1116 if (!inode || strcmp("..", name)) {
1117 print_current_name();
1118 printf(": bad directory: '..' isn't second\n");
1119 errors_uncorrected = 1;
1128 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1129 print_current_name();
1130 if (S_ISDIR(inode->i_mode))
1136 if (inode && S_ISDIR(inode->i_mode))
1137 recursive_check2(ino);
1143 static void recursive_check(unsigned int ino)
1145 struct minix_inode *dir;
1146 unsigned int offset;
1149 if (!S_ISDIR(dir->i_mode))
1150 die("internal error");
1151 if (dir->i_size < 2 * dirsize) {
1152 print_current_name();
1153 printf(": bad directory: size<32");
1154 errors_uncorrected = 1;
1156 for (offset = 0; offset < dir->i_size; offset += dirsize)
1157 check_file(dir, offset);
1160 #ifdef BB_FEATURE_MINIX2
1161 static void recursive_check2(unsigned int ino)
1163 struct minix2_inode *dir;
1164 unsigned int offset;
1167 if (!S_ISDIR(dir->i_mode))
1168 die("internal error");
1169 if (dir->i_size < 2 * dirsize) {
1170 print_current_name();
1171 printf(": bad directory: size < 32");
1172 errors_uncorrected = 1;
1174 for (offset = 0; offset < dir->i_size; offset += dirsize)
1175 check_file2(dir, offset);
1179 static int bad_zone(int i)
1183 if (BLOCK_SIZE * i != lseek(IN, BLOCK_SIZE * i, SEEK_SET))
1184 die("seek failed in bad_zone");
1185 return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE));
1188 static void check_counts(void)
1192 for (i = 1; i <= INODES; i++) {
1193 if (!inode_in_use(i) && Inode[i].i_mode && warn_mode) {
1194 printf("Inode %d mode not cleared.", i);
1195 if (ask("Clear", 1)) {
1196 Inode[i].i_mode = 0;
1200 if (!inode_count[i]) {
1201 if (!inode_in_use(i))
1203 printf("Inode %d not used, marked used in the bitmap.", i);
1204 if (ask("Clear", 1))
1208 if (!inode_in_use(i)) {
1209 printf("Inode %d used, marked unused in the bitmap.", i);
1213 if (Inode[i].i_nlinks != inode_count[i]) {
1214 printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
1215 i, Inode[i].i_mode, Inode[i].i_nlinks, inode_count[i]);
1216 if (ask("Set i_nlinks to count", 1)) {
1217 Inode[i].i_nlinks = inode_count[i];
1222 for (i = FIRSTZONE; i < ZONES; i++) {
1223 if (zone_in_use(i) == zone_count[i])
1225 if (!zone_count[i]) {
1228 printf("Zone %d: marked in use, no file uses it.", i);
1229 if (ask("Unmark", 1))
1233 printf("Zone %d: %sin use, counted=%d\n",
1234 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1238 #ifdef BB_FEATURE_MINIX2
1239 static void check_counts2(void)
1243 for (i = 1; i <= INODES; i++) {
1244 if (!inode_in_use(i) && Inode2[i].i_mode && warn_mode) {
1245 printf("Inode %d mode not cleared.", i);
1246 if (ask("Clear", 1)) {
1247 Inode2[i].i_mode = 0;
1251 if (!inode_count[i]) {
1252 if (!inode_in_use(i))
1254 printf("Inode %d not used, marked used in the bitmap.", i);
1255 if (ask("Clear", 1))
1259 if (!inode_in_use(i)) {
1260 printf("Inode %d used, marked unused in the bitmap.", i);
1264 if (Inode2[i].i_nlinks != inode_count[i]) {
1265 printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
1266 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1268 if (ask("Set i_nlinks to count", 1)) {
1269 Inode2[i].i_nlinks = inode_count[i];
1274 for (i = FIRSTZONE; i < ZONES; i++) {
1275 if (zone_in_use(i) == zone_count[i])
1277 if (!zone_count[i]) {
1280 printf("Zone %d: marked in use, no file uses it.", i);
1281 if (ask("Unmark", 1))
1285 printf("Zone %d: %sin use, counted=%d\n",
1286 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1291 static void check(void)
1293 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1294 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1295 check_zones(ROOT_INO);
1296 recursive_check(ROOT_INO);
1300 #ifdef BB_FEATURE_MINIX2
1301 static void check2(void)
1303 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1304 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1305 check_zones2(ROOT_INO);
1306 recursive_check2(ROOT_INO);
1311 /* Wed Feb 9 15:17:06 MST 2000 */
1312 /* dynamically allocate name_list (instead of making it static) */
1313 static void alloc_name_list(void)
1317 name_list = xmalloc(sizeof(char *) * MAX_DEPTH);
1318 for (i = 0; i < MAX_DEPTH; i++)
1319 name_list[i] = xmalloc(sizeof(char) * BUFSIZ + 1);
1322 #ifdef BB_FEATURE_CLEAN_UP
1323 /* execute this atexit() to deallocate name_list[] */
1324 /* piptigger was here */
1325 static void free_name_list(void)
1330 for (i = 0; i < MAX_DEPTH; i++) {
1340 extern int fsck_minix_main(int argc, char **argv)
1347 #ifdef BB_FEATURE_CLEAN_UP
1348 /* Don't bother to free memory. Exit does
1349 * that automagically, so we can save a few bytes */
1350 atexit(free_name_list);
1353 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
1354 die("bad inode size");
1355 #ifdef BB_FEATURE_MINIX2
1356 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
1357 die("bad v2 inode size");
1359 while (argc-- > 1) {
1361 if (argv[0][0] != '-') {
1365 device_name = argv[0];
1368 switch (argv[0][0]) {
1398 check_mount(); /* trying to check a mounted filesystem? */
1399 if (repair && !automatic) {
1400 if (!isatty(0) || !isatty(1))
1401 die("need terminal for interactive repairs");
1403 IN = open(device_name, repair ? O_RDWR : O_RDONLY);
1405 die("unable to open '%s'");
1406 for (count = 0; count < 3; count++)
1411 * Determine whether or not we should continue with the checking.
1412 * This is based on the status of the filesystem valid and error
1413 * flags and whether or not the -f switch was specified on the
1416 printf("%s, %s\n", applet_name, program_version);
1417 if (!(Super.s_state & MINIX_ERROR_FS) &&
1418 (Super.s_state & MINIX_VALID_FS) && !force) {
1420 printf("%s is clean, no check.\n", device_name);
1423 printf("Forcing filesystem check on %s.\n", device_name);
1425 printf("Filesystem on %s is dirty, needs checking.\n",
1430 if (repair && !automatic) {
1431 tcgetattr(0, &termios);
1433 tmp.c_lflag &= ~(ICANON | ECHO);
1434 tcsetattr(0, TCSANOW, &tmp);
1437 #ifdef BB_FEATURE_MINIX2
1450 for (i = 1, free = 0; i <= INODES; i++)
1451 if (!inode_in_use(i))
1453 printf("\n%6ld inodes used (%ld%%)\n", (INODES - free),
1454 100 * (INODES - free) / INODES);
1455 for (i = FIRSTZONE, free = 0; i < ZONES; i++)
1456 if (!zone_in_use(i))
1458 printf("%6ld zones used (%ld%%)\n", (ZONES - free),
1459 100 * (ZONES - free) / ZONES);
1460 printf("\n%6d regular files\n"
1462 "%6d character device files\n"
1463 "%6d block device files\n"
1465 "%6d symbolic links\n"
1468 regular, directory, chardev, blockdev,
1469 links - 2 * directory + 1, symlinks,
1470 total - 2 * directory + 1);
1474 printf("----------------------------\n"
1475 "FILE SYSTEM HAS BEEN CHANGED\n"
1476 "----------------------------\n");
1477 for (count = 0; count < 3; count++)
1480 write_super_block();
1482 if (repair && !automatic)
1483 tcsetattr(0, TCSANOW, &termios);
1487 if (errors_uncorrected)