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 static const int MINIX_ROOT_INO = 1;
108 static const int MINIX_LINK_MAX = 250;
109 static const int MINIX2_LINK_MAX = 65530;
111 static const int MINIX_I_MAP_SLOTS = 8;
112 static const int MINIX_Z_MAP_SLOTS = 64;
113 static const int MINIX_SUPER_MAGIC = 0x137F; /* original minix fs */
114 static const int MINIX_SUPER_MAGIC2 = 0x138F; /* minix fs, 30 char names */
115 static const int MINIX2_SUPER_MAGIC = 0x2468; /* minix V2 fs */
116 static const int MINIX2_SUPER_MAGIC2 = 0x2478; /* minix V2 fs, 30 char names */
117 static const int MINIX_VALID_FS = 0x0001; /* Clean fs. */
118 static const int 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 static const int MINIX_V1 = 0x0001; /* original minix fs */
124 static const int 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)))
189 #define BLKGETSIZE _IO(0x12,96) /* return device size */
196 static const int ROOT_INO = 1;
198 #define UPPER(size,n) ((size+((n)-1))/(n))
199 #define INODE_SIZE (sizeof(struct minix_inode))
200 #ifdef BB_FEATURE_MINIX2
201 #define INODE_SIZE2 (sizeof(struct minix2_inode))
202 #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
203 : MINIX_INODES_PER_BLOCK))
205 #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
207 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
209 #define BITS_PER_BLOCK (BLOCK_SIZE<<3)
211 static char *program_version = "1.2 - 11/11/96";
212 static char *device_name = NULL;
214 static int repair = 0, automatic = 0, verbose = 0, list = 0, show =
215 0, warn_mode = 0, force = 0;
216 static int directory = 0, regular = 0, blockdev = 0, chardev = 0, links =
217 0, symlinks = 0, total = 0;
219 static int changed = 0; /* flags if the filesystem has been changed */
220 static int errors_uncorrected = 0; /* flag if some error was not corrected */
221 static int dirsize = 16;
222 static int namelen = 14;
223 static int version2 = 0;
224 static struct termios termios;
225 static int termios_set = 0;
228 static const int MAX_DEPTH = 32;
229 static int name_depth = 0;
230 // static char name_list[MAX_DEPTH][BUFSIZ + 1];
231 static char **name_list = NULL;
233 static char *inode_buffer = NULL;
235 #define Inode (((struct minix_inode *) inode_buffer)-1)
236 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
237 static char super_block_buffer[BLOCK_SIZE];
239 #define Super (*(struct minix_super_block *)super_block_buffer)
240 #define INODES ((unsigned long)Super.s_ninodes)
241 #ifdef BB_FEATURE_MINIX2
242 #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
244 #define ZONES ((unsigned long)(Super.s_nzones))
246 #define IMAPS ((unsigned long)Super.s_imap_blocks)
247 #define ZMAPS ((unsigned long)Super.s_zmap_blocks)
248 #define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
249 #define ZONESIZE ((unsigned long)Super.s_log_zone_size)
250 #define MAXSIZE ((unsigned long)Super.s_max_size)
251 #define MAGIC (Super.s_magic)
252 #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
254 static char *inode_map;
255 static char *zone_map;
257 static unsigned char *inode_count = NULL;
258 static unsigned char *zone_count = NULL;
260 static void recursive_check(unsigned int ino);
261 #ifdef BB_FEATURE_MINIX2
262 static void recursive_check2(unsigned int ino);
265 static inline int bit(char * a,unsigned int i)
267 return (a[i >> 3] & (1<<(i & 7))) != 0;
269 #define inode_in_use(x) (bit(inode_map,(x)))
270 #define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
272 #define mark_inode(x) (setbit(inode_map,(x)),changed=1)
273 #define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
275 #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1)
276 #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1)
278 static void leave(int) __attribute__ ((noreturn));
279 static void leave(int status)
282 tcsetattr(0, TCSANOW, &termios);
286 static void show_usage(void)
288 usage(fsck_minix_usage);
291 static void die(const char *str)
293 error_msg("%s\n", str);
298 * This simply goes through the file-name data and prints out the
301 static void print_current_name(void)
305 while (i < name_depth)
306 printf("/%.*s", namelen, name_list[i++]);
311 static int ask(const char *string, int def)
317 errors_uncorrected = 1;
323 errors_uncorrected = 1;
326 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
329 if ((c = getchar()) == EOF) {
331 errors_uncorrected = 1;
338 } else if (c == 'N') {
341 } else if (c == ' ' || c == '\n')
348 errors_uncorrected = 1;
354 * Make certain that we aren't checking a filesystem that is on a
355 * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
356 * 1994 Theodore Ts'o. Also licensed under GPL.
358 static void check_mount(void)
365 if ((f = setmntent(MOUNTED, "r")) == NULL)
367 while ((mnt = getmntent(f)) != NULL)
368 if (strcmp(device_name, mnt->mnt_fsname) == 0)
375 * If the root is mounted read-only, then /etc/mtab is
376 * probably not correct; so we won't issue a warning based on
379 fd = open(MOUNTED, O_RDWR);
380 if (fd < 0 && errno == EROFS)
385 printf("%s is mounted. ", device_name);
386 if (isatty(0) && isatty(1))
387 cont = ask("Do you really want to continue", 0);
391 printf("check aborted.\n");
398 * check_zone_nr checks to see that *nr is a valid zone nr. If it
399 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
400 * if an error was corrected, and returns the zone (0 for no zone
401 * or a bad zone-number).
403 static int check_zone_nr(unsigned short *nr, int *corrected)
408 printf("Zone nr < FIRSTZONE in file `");
409 else if (*nr >= ZONES)
410 printf("Zone nr >= ZONES in file `");
413 print_current_name();
415 if (ask("Remove block", 1)) {
422 #ifdef BB_FEATURE_MINIX2
423 static int check_zone_nr2(unsigned int *nr, int *corrected)
428 printf("Zone nr < FIRSTZONE in file `");
429 else if (*nr >= ZONES)
430 printf("Zone nr >= ZONES in file `");
433 print_current_name();
435 if (ask("Remove block", 1)) {
444 * read-block reads block nr into the buffer at addr.
446 static void read_block(unsigned int nr, char *addr)
449 memset(addr, 0, BLOCK_SIZE);
452 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) {
453 printf("Read error: unable to seek to block in file '");
454 print_current_name();
456 memset(addr, 0, BLOCK_SIZE);
457 errors_uncorrected = 1;
458 } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) {
459 printf("Read error: bad block in file '");
460 print_current_name();
462 memset(addr, 0, BLOCK_SIZE);
463 errors_uncorrected = 1;
468 * write_block writes block nr to disk.
470 static void write_block(unsigned int nr, char *addr)
474 if (nr < FIRSTZONE || nr >= ZONES) {
475 printf("Internal error: trying to write bad block\n"
476 "Write request ignored\n");
477 errors_uncorrected = 1;
480 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET))
481 die("seek failed in write_block");
482 if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) {
483 printf("Write error: bad block in file '");
484 print_current_name();
486 errors_uncorrected = 1;
491 * map-block calculates the absolute block nr of a block in a file.
492 * It sets 'changed' if the inode has needed changing, and re-writes
493 * any indirect blocks with errors.
495 static int map_block(struct minix_inode *inode, unsigned int blknr)
497 unsigned short ind[BLOCK_SIZE >> 1];
498 unsigned short dind[BLOCK_SIZE >> 1];
499 int blk_chg, block, result;
502 return check_zone_nr(inode->i_zone + blknr, &changed);
505 block = check_zone_nr(inode->i_zone + 7, &changed);
506 read_block(block, (char *) ind);
508 result = check_zone_nr(blknr + ind, &blk_chg);
510 write_block(block, (char *) ind);
514 block = check_zone_nr(inode->i_zone + 8, &changed);
515 read_block(block, (char *) dind);
517 result = check_zone_nr(dind + (blknr / 512), &blk_chg);
519 write_block(block, (char *) dind);
521 read_block(block, (char *) ind);
523 result = check_zone_nr(ind + (blknr % 512), &blk_chg);
525 write_block(block, (char *) ind);
529 #ifdef BB_FEATURE_MINIX2
530 static int map_block2(struct minix2_inode *inode, unsigned int blknr)
532 unsigned int ind[BLOCK_SIZE >> 2];
533 unsigned int dind[BLOCK_SIZE >> 2];
534 unsigned int tind[BLOCK_SIZE >> 2];
535 int blk_chg, block, result;
538 return check_zone_nr2(inode->i_zone + blknr, &changed);
541 block = check_zone_nr2(inode->i_zone + 7, &changed);
542 read_block(block, (char *) ind);
544 result = check_zone_nr2(blknr + ind, &blk_chg);
546 write_block(block, (char *) ind);
550 if (blknr >= 256 * 256) {
551 block = check_zone_nr2(inode->i_zone + 8, &changed);
552 read_block(block, (char *) dind);
554 result = check_zone_nr2(dind + blknr / 256, &blk_chg);
556 write_block(block, (char *) dind);
558 read_block(block, (char *) ind);
560 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
562 write_block(block, (char *) ind);
566 block = check_zone_nr2(inode->i_zone + 9, &changed);
567 read_block(block, (char *) tind);
569 result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
571 write_block(block, (char *) tind);
573 read_block(block, (char *) dind);
575 result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
577 write_block(block, (char *) dind);
579 read_block(block, (char *) ind);
581 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
583 write_block(block, (char *) ind);
588 static void write_super_block(void)
591 * Set the state of the filesystem based on whether or not there
592 * are uncorrected errors. The filesystem valid flag is
593 * unconditionally set if we get this far.
595 Super.s_state |= MINIX_VALID_FS;
596 if (errors_uncorrected)
597 Super.s_state |= MINIX_ERROR_FS;
599 Super.s_state &= ~MINIX_ERROR_FS;
601 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
602 die("seek failed in write_super_block");
603 if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE))
604 die("unable to write super-block");
609 static void write_tables(void)
613 if (IMAPS * BLOCK_SIZE != write(IN, inode_map, IMAPS * BLOCK_SIZE))
614 die("Unable to write inode map");
615 if (ZMAPS * BLOCK_SIZE != write(IN, zone_map, ZMAPS * BLOCK_SIZE))
616 die("Unable to write zone map");
617 if (INODE_BUFFER_SIZE != write(IN, inode_buffer, INODE_BUFFER_SIZE))
618 die("Unable to write inodes");
621 static void get_dirsize(void)
624 char blk[BLOCK_SIZE];
627 #ifdef BB_FEATURE_MINIX2
629 block = Inode2[ROOT_INO].i_zone[0];
632 block = Inode[ROOT_INO].i_zone[0];
633 read_block(block, blk);
634 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
635 if (strcmp(blk + size + 2, "..") == 0) {
644 static void read_superblock(void)
646 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
648 if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
649 die("unable to read super block");
650 if (MAGIC == MINIX_SUPER_MAGIC) {
654 } else if (MAGIC == MINIX_SUPER_MAGIC2) {
658 #ifdef BB_FEATURE_MINIX2
659 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
663 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
669 die("bad magic number in super-block");
670 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
671 die("Only 1k blocks/zones supported");
672 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
673 die("bad s_imap_blocks field in super-block");
674 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
675 die("bad s_zmap_blocks field in super-block");
678 static void read_tables(void)
680 inode_map = xmalloc(IMAPS * BLOCK_SIZE);
681 zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
682 memset(inode_map, 0, sizeof(inode_map));
683 memset(zone_map, 0, sizeof(zone_map));
684 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
685 inode_count = xmalloc(INODES + 1);
686 zone_count = xmalloc(ZONES);
687 if (IMAPS * BLOCK_SIZE != read(IN, inode_map, IMAPS * BLOCK_SIZE))
688 die("Unable to read inode map");
689 if (ZMAPS * BLOCK_SIZE != read(IN, zone_map, ZMAPS * BLOCK_SIZE))
690 die("Unable to read zone map");
691 if (INODE_BUFFER_SIZE != read(IN, inode_buffer, INODE_BUFFER_SIZE))
692 die("Unable to read inodes");
693 if (NORM_FIRSTZONE != FIRSTZONE) {
694 printf("Warning: Firstzone != Norm_firstzone\n");
695 errors_uncorrected = 1;
699 printf("%ld inodes\n", INODES);
700 printf("%ld blocks\n", ZONES);
701 printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
702 printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
703 printf("Maxsize=%ld\n", MAXSIZE);
704 printf("Filesystem state=%d\n", Super.s_state);
705 printf("namelen=%d\n\n", namelen);
709 struct minix_inode *get_inode(unsigned int nr)
711 struct minix_inode *inode;
713 if (!nr || nr > INODES)
717 if (!inode_count[nr]) {
718 if (!inode_in_use(nr)) {
719 printf("Inode %d marked not used, but used for file '", nr);
720 print_current_name();
723 if (ask("Mark in use", 1))
726 errors_uncorrected = 1;
729 if (S_ISDIR(inode->i_mode))
731 else if (S_ISREG(inode->i_mode))
733 else if (S_ISCHR(inode->i_mode))
735 else if (S_ISBLK(inode->i_mode))
737 else if (S_ISLNK(inode->i_mode))
739 else if (S_ISSOCK(inode->i_mode));
740 else if (S_ISFIFO(inode->i_mode));
742 print_current_name();
743 printf(" has mode %05o\n", inode->i_mode);
748 if (!++inode_count[nr]) {
749 printf("Warning: inode count too big.\n");
751 errors_uncorrected = 1;
756 #ifdef BB_FEATURE_MINIX2
757 struct minix2_inode *get_inode2(unsigned int nr)
759 struct minix2_inode *inode;
761 if (!nr || nr > INODES)
765 if (!inode_count[nr]) {
766 if (!inode_in_use(nr)) {
767 printf("Inode %d marked not used, but used for file '", nr);
768 print_current_name();
771 if (ask("Mark in use", 1))
774 errors_uncorrected = 1;
777 if (S_ISDIR(inode->i_mode))
779 else if (S_ISREG(inode->i_mode))
781 else if (S_ISCHR(inode->i_mode))
783 else if (S_ISBLK(inode->i_mode))
785 else if (S_ISLNK(inode->i_mode))
787 else if (S_ISSOCK(inode->i_mode));
788 else if (S_ISFIFO(inode->i_mode));
790 print_current_name();
791 printf(" has mode %05o\n", inode->i_mode);
795 if (!++inode_count[nr]) {
796 printf("Warning: inode count too big.\n");
798 errors_uncorrected = 1;
804 static void check_root(void)
806 struct minix_inode *inode = Inode + ROOT_INO;
808 if (!inode || !S_ISDIR(inode->i_mode))
809 die("root inode isn't a directory");
812 #ifdef BB_FEATURE_MINIX2
813 static void check_root2(void)
815 struct minix2_inode *inode = Inode2 + ROOT_INO;
817 if (!inode || !S_ISDIR(inode->i_mode))
818 die("root inode isn't a directory");
822 static int add_zone(unsigned short *znr, int *corrected)
828 block = check_zone_nr(znr, corrected);
831 if (zone_count[block]) {
832 printf("Block has been used before. Now in file `");
833 print_current_name();
835 if (ask("Clear", 1)) {
843 if (!zone_in_use(block)) {
844 printf("Block %d in file `", block);
845 print_current_name();
846 printf("' is marked not in use.");
847 if (ask("Correct", 1))
850 if (!++zone_count[block])
855 #ifdef BB_FEATURE_MINIX2
856 static int add_zone2(unsigned int *znr, int *corrected)
862 block = check_zone_nr2(znr, corrected);
865 if (zone_count[block]) {
866 printf("Block has been used before. Now in file `");
867 print_current_name();
869 if (ask("Clear", 1)) {
877 if (!zone_in_use(block)) {
878 printf("Block %d in file `", block);
879 print_current_name();
880 printf("' is marked not in use.");
881 if (ask("Correct", 1))
884 if (!++zone_count[block])
890 static void add_zone_ind(unsigned short *znr, int *corrected)
892 static char blk[BLOCK_SIZE];
896 block = add_zone(znr, corrected);
899 read_block(block, blk);
900 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
901 add_zone(i + (unsigned short *) blk, &chg_blk);
903 write_block(block, blk);
906 #ifdef BB_FEATURE_MINIX2
907 static void add_zone_ind2(unsigned int *znr, int *corrected)
909 static char blk[BLOCK_SIZE];
913 block = add_zone2(znr, corrected);
916 read_block(block, blk);
917 for (i = 0; i < BLOCK_SIZE >> 2; i++)
918 add_zone2(i + (unsigned int *) blk, &chg_blk);
920 write_block(block, blk);
924 static void add_zone_dind(unsigned short *znr, int *corrected)
926 static char blk[BLOCK_SIZE];
930 block = add_zone(znr, corrected);
933 read_block(block, blk);
934 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
935 add_zone_ind(i + (unsigned short *) blk, &blk_chg);
937 write_block(block, blk);
940 #ifdef BB_FEATURE_MINIX2
941 static void add_zone_dind2(unsigned int *znr, int *corrected)
943 static char blk[BLOCK_SIZE];
947 block = add_zone2(znr, corrected);
950 read_block(block, blk);
951 for (i = 0; i < BLOCK_SIZE >> 2; i++)
952 add_zone_ind2(i + (unsigned int *) blk, &blk_chg);
954 write_block(block, blk);
957 static void add_zone_tind2(unsigned int *znr, int *corrected)
959 static char blk[BLOCK_SIZE];
963 block = add_zone2(znr, corrected);
966 read_block(block, blk);
967 for (i = 0; i < BLOCK_SIZE >> 2; i++)
968 add_zone_dind2(i + (unsigned int *) blk, &blk_chg);
970 write_block(block, blk);
974 static void check_zones(unsigned int i)
976 struct minix_inode *inode;
978 if (!i || i > INODES)
980 if (inode_count[i] > 1) /* have we counted this file already? */
983 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
984 !S_ISLNK(inode->i_mode)) return;
985 for (i = 0; i < 7; i++)
986 add_zone(i + inode->i_zone, &changed);
987 add_zone_ind(7 + inode->i_zone, &changed);
988 add_zone_dind(8 + inode->i_zone, &changed);
991 #ifdef BB_FEATURE_MINIX2
992 static void check_zones2(unsigned int i)
994 struct minix2_inode *inode;
996 if (!i || i > INODES)
998 if (inode_count[i] > 1) /* have we counted this file already? */
1001 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
1002 && !S_ISLNK(inode->i_mode))
1004 for (i = 0; i < 7; i++)
1005 add_zone2(i + inode->i_zone, &changed);
1006 add_zone_ind2(7 + inode->i_zone, &changed);
1007 add_zone_dind2(8 + inode->i_zone, &changed);
1008 add_zone_tind2(9 + inode->i_zone, &changed);
1012 static void check_file(struct minix_inode *dir, unsigned int offset)
1014 static char blk[BLOCK_SIZE];
1015 struct minix_inode *inode;
1020 block = map_block(dir, offset / BLOCK_SIZE);
1021 read_block(block, blk);
1022 name = blk + (offset % BLOCK_SIZE) + 2;
1023 ino = *(unsigned short *) (name - 2);
1025 print_current_name();
1026 printf(" contains a bad inode number for file '");
1027 printf("%.*s'.", namelen, name);
1028 if (ask(" Remove", 1)) {
1029 *(unsigned short *) (name - 2) = 0;
1030 write_block(block, blk);
1034 if (name_depth < MAX_DEPTH)
1035 strncpy(name_list[name_depth], name, namelen);
1037 inode = get_inode(ino);
1040 if (!inode || strcmp(".", name)) {
1041 print_current_name();
1042 printf(": bad directory: '.' isn't first\n");
1043 errors_uncorrected = 1;
1047 if (offset == dirsize) {
1048 if (!inode || strcmp("..", name)) {
1049 print_current_name();
1050 printf(": bad directory: '..' isn't second\n");
1051 errors_uncorrected = 1;
1057 if (name_depth < MAX_DEPTH)
1058 strncpy(name_list[name_depth], name, namelen);
1062 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1063 print_current_name();
1064 if (S_ISDIR(inode->i_mode))
1070 if (inode && S_ISDIR(inode->i_mode))
1071 recursive_check(ino);
1076 #ifdef BB_FEATURE_MINIX2
1077 static void check_file2(struct minix2_inode *dir, unsigned int offset)
1079 static char blk[BLOCK_SIZE];
1080 struct minix2_inode *inode;
1085 block = map_block2(dir, offset / BLOCK_SIZE);
1086 read_block(block, blk);
1087 name = blk + (offset % BLOCK_SIZE) + 2;
1088 ino = *(unsigned short *) (name - 2);
1090 print_current_name();
1091 printf(" contains a bad inode number for file '");
1092 printf("%.*s'.", namelen, name);
1093 if (ask(" Remove", 1)) {
1094 *(unsigned short *) (name - 2) = 0;
1095 write_block(block, blk);
1099 if (name_depth < MAX_DEPTH)
1100 strncpy(name_list[name_depth], name, namelen);
1102 inode = get_inode2(ino);
1105 if (!inode || strcmp(".", name)) {
1106 print_current_name();
1107 printf(": bad directory: '.' isn't first\n");
1108 errors_uncorrected = 1;
1112 if (offset == dirsize) {
1113 if (!inode || strcmp("..", name)) {
1114 print_current_name();
1115 printf(": bad directory: '..' isn't second\n");
1116 errors_uncorrected = 1;
1125 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1126 print_current_name();
1127 if (S_ISDIR(inode->i_mode))
1133 if (inode && S_ISDIR(inode->i_mode))
1134 recursive_check2(ino);
1140 static void recursive_check(unsigned int ino)
1142 struct minix_inode *dir;
1143 unsigned int offset;
1146 if (!S_ISDIR(dir->i_mode))
1147 die("internal error");
1148 if (dir->i_size < 2 * dirsize) {
1149 print_current_name();
1150 printf(": bad directory: size<32");
1151 errors_uncorrected = 1;
1153 for (offset = 0; offset < dir->i_size; offset += dirsize)
1154 check_file(dir, offset);
1157 #ifdef BB_FEATURE_MINIX2
1158 static void recursive_check2(unsigned int ino)
1160 struct minix2_inode *dir;
1161 unsigned int offset;
1164 if (!S_ISDIR(dir->i_mode))
1165 die("internal error");
1166 if (dir->i_size < 2 * dirsize) {
1167 print_current_name();
1168 printf(": bad directory: size < 32");
1169 errors_uncorrected = 1;
1171 for (offset = 0; offset < dir->i_size; offset += dirsize)
1172 check_file2(dir, offset);
1176 static int bad_zone(int i)
1180 if (BLOCK_SIZE * i != lseek(IN, BLOCK_SIZE * i, SEEK_SET))
1181 die("seek failed in bad_zone");
1182 return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE));
1185 static void check_counts(void)
1189 for (i = 1; i <= INODES; i++) {
1190 if (!inode_in_use(i) && Inode[i].i_mode && warn_mode) {
1191 printf("Inode %d mode not cleared.", i);
1192 if (ask("Clear", 1)) {
1193 Inode[i].i_mode = 0;
1197 if (!inode_count[i]) {
1198 if (!inode_in_use(i))
1200 printf("Inode %d not used, marked used in the bitmap.", i);
1201 if (ask("Clear", 1))
1205 if (!inode_in_use(i)) {
1206 printf("Inode %d used, marked unused in the bitmap.", i);
1210 if (Inode[i].i_nlinks != inode_count[i]) {
1211 printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
1212 i, Inode[i].i_mode, Inode[i].i_nlinks, inode_count[i]);
1213 if (ask("Set i_nlinks to count", 1)) {
1214 Inode[i].i_nlinks = inode_count[i];
1219 for (i = FIRSTZONE; i < ZONES; i++) {
1220 if (zone_in_use(i) == zone_count[i])
1222 if (!zone_count[i]) {
1225 printf("Zone %d: marked in use, no file uses it.", i);
1226 if (ask("Unmark", 1))
1230 printf("Zone %d: %sin use, counted=%d\n",
1231 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1235 #ifdef BB_FEATURE_MINIX2
1236 static void check_counts2(void)
1240 for (i = 1; i <= INODES; i++) {
1241 if (!inode_in_use(i) && Inode2[i].i_mode && warn_mode) {
1242 printf("Inode %d mode not cleared.", i);
1243 if (ask("Clear", 1)) {
1244 Inode2[i].i_mode = 0;
1248 if (!inode_count[i]) {
1249 if (!inode_in_use(i))
1251 printf("Inode %d not used, marked used in the bitmap.", i);
1252 if (ask("Clear", 1))
1256 if (!inode_in_use(i)) {
1257 printf("Inode %d used, marked unused in the bitmap.", i);
1261 if (Inode2[i].i_nlinks != inode_count[i]) {
1262 printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
1263 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1265 if (ask("Set i_nlinks to count", 1)) {
1266 Inode2[i].i_nlinks = inode_count[i];
1271 for (i = FIRSTZONE; i < ZONES; i++) {
1272 if (zone_in_use(i) == zone_count[i])
1274 if (!zone_count[i]) {
1277 printf("Zone %d: marked in use, no file uses it.", i);
1278 if (ask("Unmark", 1))
1282 printf("Zone %d: %sin use, counted=%d\n",
1283 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1288 static void check(void)
1290 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1291 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1292 check_zones(ROOT_INO);
1293 recursive_check(ROOT_INO);
1297 #ifdef BB_FEATURE_MINIX2
1298 static void check2(void)
1300 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1301 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1302 check_zones2(ROOT_INO);
1303 recursive_check2(ROOT_INO);
1308 /* Wed Feb 9 15:17:06 MST 2000 */
1309 /* dynamically allocate name_list (instead of making it static) */
1310 static void alloc_name_list(void)
1314 name_list = xmalloc(sizeof(char *) * MAX_DEPTH);
1315 for (i = 0; i < MAX_DEPTH; i++)
1316 name_list[i] = xmalloc(sizeof(char) * BUFSIZ + 1);
1319 #ifdef BB_FEATURE_CLEAN_UP
1320 /* execute this atexit() to deallocate name_list[] */
1321 /* piptigger was here */
1322 static void free_name_list(void)
1327 for (i = 0; i < MAX_DEPTH; i++) {
1337 extern int fsck_minix_main(int argc, char **argv)
1344 #ifdef BB_FEATURE_CLEAN_UP
1345 /* Don't bother to free memory. Exit does
1346 * that automagically, so we can save a few bytes */
1347 atexit(free_name_list);
1350 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
1351 die("bad inode size");
1352 #ifdef BB_FEATURE_MINIX2
1353 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
1354 die("bad v2 inode size");
1356 while (argc-- > 1) {
1358 if (argv[0][0] != '-') {
1362 device_name = argv[0];
1365 switch (argv[0][0]) {
1395 check_mount(); /* trying to check a mounted filesystem? */
1396 if (repair && !automatic) {
1397 if (!isatty(0) || !isatty(1))
1398 die("need terminal for interactive repairs");
1400 IN = open(device_name, repair ? O_RDWR : O_RDONLY);
1402 fprintf(stderr,"unable to open device '%s'.\n",device_name);
1405 for (count = 0; count < 3; count++)
1410 * Determine whether or not we should continue with the checking.
1411 * This is based on the status of the filesystem valid and error
1412 * flags and whether or not the -f switch was specified on the
1415 printf("%s, %s\n", applet_name, program_version);
1416 if (!(Super.s_state & MINIX_ERROR_FS) &&
1417 (Super.s_state & MINIX_VALID_FS) && !force) {
1419 printf("%s is clean, no check.\n", device_name);
1422 printf("Forcing filesystem check on %s.\n", device_name);
1424 printf("Filesystem on %s is dirty, needs checking.\n",
1429 if (repair && !automatic) {
1430 tcgetattr(0, &termios);
1432 tmp.c_lflag &= ~(ICANON | ECHO);
1433 tcsetattr(0, TCSANOW, &tmp);
1436 #ifdef BB_FEATURE_MINIX2
1449 for (i = 1, free = 0; i <= INODES; i++)
1450 if (!inode_in_use(i))
1452 printf("\n%6ld inodes used (%ld%%)\n", (INODES - free),
1453 100 * (INODES - free) / INODES);
1454 for (i = FIRSTZONE, free = 0; i < ZONES; i++)
1455 if (!zone_in_use(i))
1457 printf("%6ld zones used (%ld%%)\n", (ZONES - free),
1458 100 * (ZONES - free) / ZONES);
1459 printf("\n%6d regular files\n"
1461 "%6d character device files\n"
1462 "%6d block device files\n"
1464 "%6d symbolic links\n"
1467 regular, directory, chardev, blockdev,
1468 links - 2 * directory + 1, symlinks,
1469 total - 2 * directory + 1);
1473 printf("----------------------------\n"
1474 "FILE SYSTEM HAS BEEN CHANGED\n"
1475 "----------------------------\n");
1476 for (count = 0; count < 3; count++)
1479 write_super_block();
1481 if (repair && !automatic)
1482 tcsetattr(0, TCSANOW, &termios);
1486 if (errors_uncorrected)