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 */
199 #ifdef MINIX2_SUPER_MAGIC2
200 #define HAVE_MINIX2 1
209 #define UPPER(size,n) ((size+((n)-1))/(n))
210 #define INODE_SIZE (sizeof(struct minix_inode))
212 #define INODE_SIZE2 (sizeof(struct minix2_inode))
213 #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
214 : MINIX_INODES_PER_BLOCK))
216 #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
218 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
220 #define BITS_PER_BLOCK (BLOCK_SIZE<<3)
222 static char *program_name = "fsck.minix";
223 static char *program_version = "1.2 - 11/11/96";
224 static char *device_name = NULL;
226 static int repair = 0, automatic = 0, verbose = 0, list = 0, show =
227 0, warn_mode = 0, force = 0;
228 static int directory = 0, regular = 0, blockdev = 0, chardev = 0, links =
229 0, symlinks = 0, total = 0;
231 static int changed = 0; /* flags if the filesystem has been changed */
232 static int errors_uncorrected = 0; /* flag if some error was not corrected */
233 static int dirsize = 16;
234 static int namelen = 14;
235 static int version2 = 0;
236 static struct termios termios;
237 static int termios_set = 0;
241 static int name_depth = 0;
242 // static char name_list[MAX_DEPTH][BUFSIZ + 1];
243 static char **name_list = NULL;
245 static char *inode_buffer = NULL;
247 #define Inode (((struct minix_inode *) inode_buffer)-1)
248 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
249 static char super_block_buffer[BLOCK_SIZE];
251 #define Super (*(struct minix_super_block *)super_block_buffer)
252 #define INODES ((unsigned long)Super.s_ninodes)
254 #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
256 #define ZONES ((unsigned long)(Super.s_nzones))
258 #define IMAPS ((unsigned long)Super.s_imap_blocks)
259 #define ZMAPS ((unsigned long)Super.s_zmap_blocks)
260 #define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
261 #define ZONESIZE ((unsigned long)Super.s_log_zone_size)
262 #define MAXSIZE ((unsigned long)Super.s_max_size)
263 #define MAGIC (Super.s_magic)
264 #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
266 static char *inode_map;
267 static char *zone_map;
269 static unsigned char *inode_count = NULL;
270 static unsigned char *zone_count = NULL;
272 static void recursive_check(unsigned int ino);
273 static void recursive_check2(unsigned int ino);
275 #define inode_in_use(x) (isset(inode_map,(x)))
276 #define zone_in_use(x) (isset(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 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
296 fprintf(stderr, "Usage: %s [-larvsmf] /dev/name\n", program_name);
297 #ifndef BB_FEATURE_TRIVIAL_HELP
299 "\nPerforms a consistency check for MINIX filesystems.\n\n");
300 fprintf(stderr, "Options:\n");
301 fprintf(stderr, "\t-l\tLists all filenames\n");
302 fprintf(stderr, "\t-r\tPerform interactive repairs\n");
303 fprintf(stderr, "\t-a\tPerform automatic repairs\n");
304 fprintf(stderr, "\t-v\tverbose\n");
305 fprintf(stderr, "\t-s\tOutputs super-block information\n");
307 "\t-m\tActivates MINIX-like \"mode not cleared\" warnings\n");
308 fprintf(stderr, "\t-f\tForce file system check.\n\n");
313 static void die(const char *str)
315 fprintf(stderr, "%s: %s\n", program_name, str);
320 * This simply goes through the file-name data and prints out the
323 static void print_current_name(void)
327 while (i < name_depth)
328 printf("/%.*s", namelen, name_list[i++]);
333 static int ask(const char *string, int def)
339 errors_uncorrected = 1;
345 errors_uncorrected = 1;
348 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
351 if ((c = getchar()) == EOF) {
353 errors_uncorrected = 1;
360 } else if (c == 'N') {
363 } else if (c == ' ' || c == '\n')
370 errors_uncorrected = 1;
376 * Make certain that we aren't checking a filesystem that is on a
377 * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
378 * 1994 Theodore Ts'o. Also licensed under GPL.
380 static void check_mount(void)
387 if ((f = setmntent(MOUNTED, "r")) == NULL)
389 while ((mnt = getmntent(f)) != NULL)
390 if (strcmp(device_name, mnt->mnt_fsname) == 0)
397 * If the root is mounted read-only, then /etc/mtab is
398 * probably not correct; so we won't issue a warning based on
401 fd = open(MOUNTED, O_RDWR);
402 if (fd < 0 && errno == EROFS)
407 printf("%s is mounted. ", device_name);
408 if (isatty(0) && isatty(1))
409 cont = ask("Do you really want to continue", 0);
413 printf("check aborted.\n");
420 * check_zone_nr checks to see that *nr is a valid zone nr. If it
421 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
422 * if an error was corrected, and returns the zone (0 for no zone
423 * or a bad zone-number).
425 static int check_zone_nr(unsigned short *nr, int *corrected)
430 printf("Zone nr < FIRSTZONE in file `");
431 else if (*nr >= ZONES)
432 printf("Zone nr >= ZONES in file `");
435 print_current_name();
437 if (ask("Remove block", 1)) {
445 static int check_zone_nr2(unsigned int *nr, int *corrected)
450 printf("Zone nr < FIRSTZONE in file `");
451 else if (*nr >= ZONES)
452 printf("Zone nr >= ZONES in file `");
455 print_current_name();
457 if (ask("Remove block", 1)) {
466 * read-block reads block nr into the buffer at addr.
468 static void read_block(unsigned int nr, char *addr)
471 memset(addr, 0, BLOCK_SIZE);
474 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) {
475 printf("Read error: unable to seek to block in file '");
476 print_current_name();
478 memset(addr, 0, BLOCK_SIZE);
479 errors_uncorrected = 1;
480 } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) {
481 printf("Read error: bad block in file '");
482 print_current_name();
484 memset(addr, 0, BLOCK_SIZE);
485 errors_uncorrected = 1;
490 * write_block writes block nr to disk.
492 static void write_block(unsigned int nr, char *addr)
496 if (nr < FIRSTZONE || nr >= ZONES) {
497 printf("Internal error: trying to write bad block\n"
498 "Write request ignored\n");
499 errors_uncorrected = 1;
502 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET))
503 die("seek failed in write_block");
504 if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) {
505 printf("Write error: bad block in file '");
506 print_current_name();
508 errors_uncorrected = 1;
513 * map-block calculates the absolute block nr of a block in a file.
514 * It sets 'changed' if the inode has needed changing, and re-writes
515 * any indirect blocks with errors.
517 static int map_block(struct minix_inode *inode, unsigned int blknr)
519 unsigned short ind[BLOCK_SIZE >> 1];
520 unsigned short dind[BLOCK_SIZE >> 1];
521 int blk_chg, block, result;
524 return check_zone_nr(inode->i_zone + blknr, &changed);
527 block = check_zone_nr(inode->i_zone + 7, &changed);
528 read_block(block, (char *) ind);
530 result = check_zone_nr(blknr + ind, &blk_chg);
532 write_block(block, (char *) ind);
536 block = check_zone_nr(inode->i_zone + 8, &changed);
537 read_block(block, (char *) dind);
539 result = check_zone_nr(dind + (blknr / 512), &blk_chg);
541 write_block(block, (char *) dind);
543 read_block(block, (char *) ind);
545 result = check_zone_nr(ind + (blknr % 512), &blk_chg);
547 write_block(block, (char *) ind);
552 static int map_block2(struct minix2_inode *inode, unsigned int blknr)
554 unsigned int ind[BLOCK_SIZE >> 2];
555 unsigned int dind[BLOCK_SIZE >> 2];
556 unsigned int tind[BLOCK_SIZE >> 2];
557 int blk_chg, block, result;
560 return check_zone_nr2(inode->i_zone + blknr, &changed);
563 block = check_zone_nr2(inode->i_zone + 7, &changed);
564 read_block(block, (char *) ind);
566 result = check_zone_nr2(blknr + ind, &blk_chg);
568 write_block(block, (char *) ind);
572 if (blknr >= 256 * 256) {
573 block = check_zone_nr2(inode->i_zone + 8, &changed);
574 read_block(block, (char *) dind);
576 result = check_zone_nr2(dind + blknr / 256, &blk_chg);
578 write_block(block, (char *) dind);
580 read_block(block, (char *) ind);
582 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
584 write_block(block, (char *) ind);
588 block = check_zone_nr2(inode->i_zone + 9, &changed);
589 read_block(block, (char *) tind);
591 result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
593 write_block(block, (char *) tind);
595 read_block(block, (char *) dind);
597 result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
599 write_block(block, (char *) dind);
601 read_block(block, (char *) ind);
603 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
605 write_block(block, (char *) ind);
610 static void write_super_block(void)
613 * Set the state of the filesystem based on whether or not there
614 * are uncorrected errors. The filesystem valid flag is
615 * unconditionally set if we get this far.
617 Super.s_state |= MINIX_VALID_FS;
618 if (errors_uncorrected)
619 Super.s_state |= MINIX_ERROR_FS;
621 Super.s_state &= ~MINIX_ERROR_FS;
623 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
624 die("seek failed in write_super_block");
625 if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE))
626 die("unable to write super-block");
631 static void write_tables(void)
635 if (IMAPS * BLOCK_SIZE != write(IN, inode_map, IMAPS * BLOCK_SIZE))
636 die("Unable to write inode map");
637 if (ZMAPS * BLOCK_SIZE != write(IN, zone_map, ZMAPS * BLOCK_SIZE))
638 die("Unable to write zone map");
639 if (INODE_BUFFER_SIZE != write(IN, inode_buffer, INODE_BUFFER_SIZE))
640 die("Unable to write inodes");
643 static void get_dirsize(void)
646 char blk[BLOCK_SIZE];
651 block = Inode2[ROOT_INO].i_zone[0];
654 block = Inode[ROOT_INO].i_zone[0];
655 read_block(block, blk);
656 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
657 if (strcmp(blk + size + 2, "..") == 0) {
666 static void read_superblock(void)
668 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
670 if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
671 die("unable to read super block");
672 if (MAGIC == MINIX_SUPER_MAGIC) {
676 } else if (MAGIC == MINIX_SUPER_MAGIC2) {
681 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
685 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
691 die("bad magic number in super-block");
692 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
693 die("Only 1k blocks/zones supported");
694 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
695 die("bad s_imap_blocks field in super-block");
696 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
697 die("bad s_zmap_blocks field in super-block");
700 static void read_tables(void)
702 inode_map = xmalloc(IMAPS * BLOCK_SIZE);
703 zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
704 memset(inode_map, 0, sizeof(inode_map));
705 memset(zone_map, 0, sizeof(zone_map));
706 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
707 inode_count = xmalloc(INODES + 1);
708 zone_count = xmalloc(ZONES);
709 if (IMAPS * BLOCK_SIZE != read(IN, inode_map, IMAPS * BLOCK_SIZE))
710 die("Unable to read inode map");
711 if (ZMAPS * BLOCK_SIZE != read(IN, zone_map, ZMAPS * BLOCK_SIZE))
712 die("Unable to read zone map");
713 if (INODE_BUFFER_SIZE != read(IN, inode_buffer, INODE_BUFFER_SIZE))
714 die("Unable to read inodes");
715 if (NORM_FIRSTZONE != FIRSTZONE) {
716 printf("Warning: Firstzone != Norm_firstzone\n");
717 errors_uncorrected = 1;
721 printf("%ld inodes\n", INODES);
722 printf("%ld blocks\n", ZONES);
723 printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
724 printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
725 printf("Maxsize=%ld\n", MAXSIZE);
726 printf("Filesystem state=%d\n", Super.s_state);
727 printf("namelen=%d\n\n", namelen);
731 struct minix_inode *get_inode(unsigned int nr)
733 struct minix_inode *inode;
735 if (!nr || nr > INODES)
739 if (!inode_count[nr]) {
740 if (!inode_in_use(nr)) {
741 printf("Inode %d marked not used, but used for file '", nr);
742 print_current_name();
745 if (ask("Mark in use", 1))
748 errors_uncorrected = 1;
751 if (S_ISDIR(inode->i_mode))
753 else if (S_ISREG(inode->i_mode))
755 else if (S_ISCHR(inode->i_mode))
757 else if (S_ISBLK(inode->i_mode))
759 else if (S_ISLNK(inode->i_mode))
761 else if (S_ISSOCK(inode->i_mode));
762 else if (S_ISFIFO(inode->i_mode));
764 print_current_name();
765 printf(" has mode %05o\n", inode->i_mode);
770 if (!++inode_count[nr]) {
771 printf("Warning: inode count too big.\n");
773 errors_uncorrected = 1;
779 struct minix2_inode *get_inode2(unsigned int nr)
781 struct minix2_inode *inode;
783 if (!nr || nr > INODES)
787 if (!inode_count[nr]) {
788 if (!inode_in_use(nr)) {
789 printf("Inode %d marked not used, but used for file '", nr);
790 print_current_name();
793 if (ask("Mark in use", 1))
796 errors_uncorrected = 1;
799 if (S_ISDIR(inode->i_mode))
801 else if (S_ISREG(inode->i_mode))
803 else if (S_ISCHR(inode->i_mode))
805 else if (S_ISBLK(inode->i_mode))
807 else if (S_ISLNK(inode->i_mode))
809 else if (S_ISSOCK(inode->i_mode));
810 else if (S_ISFIFO(inode->i_mode));
812 print_current_name();
813 printf(" has mode %05o\n", inode->i_mode);
817 if (!++inode_count[nr]) {
818 printf("Warning: inode count too big.\n");
820 errors_uncorrected = 1;
826 static void check_root(void)
828 struct minix_inode *inode = Inode + ROOT_INO;
830 if (!inode || !S_ISDIR(inode->i_mode))
831 die("root inode isn't a directory");
835 static void check_root2(void)
837 struct minix2_inode *inode = Inode2 + ROOT_INO;
839 if (!inode || !S_ISDIR(inode->i_mode))
840 die("root inode isn't a directory");
844 static int add_zone(unsigned short *znr, int *corrected)
850 block = check_zone_nr(znr, corrected);
853 if (zone_count[block]) {
854 printf("Block has been used before. Now in file `");
855 print_current_name();
857 if (ask("Clear", 1)) {
865 if (!zone_in_use(block)) {
866 printf("Block %d in file `", block);
867 print_current_name();
868 printf("' is marked not in use.");
869 if (ask("Correct", 1))
872 if (!++zone_count[block])
878 static int add_zone2(unsigned int *znr, int *corrected)
884 block = check_zone_nr2(znr, corrected);
887 if (zone_count[block]) {
888 printf("Block has been used before. Now in file `");
889 print_current_name();
891 if (ask("Clear", 1)) {
899 if (!zone_in_use(block)) {
900 printf("Block %d in file `", block);
901 print_current_name();
902 printf("' is marked not in use.");
903 if (ask("Correct", 1))
906 if (!++zone_count[block])
912 static void add_zone_ind(unsigned short *znr, int *corrected)
914 static char blk[BLOCK_SIZE];
918 block = add_zone(znr, corrected);
921 read_block(block, blk);
922 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
923 add_zone(i + (unsigned short *) blk, &chg_blk);
925 write_block(block, blk);
929 static void add_zone_ind2(unsigned int *znr, int *corrected)
931 static char blk[BLOCK_SIZE];
935 block = add_zone2(znr, corrected);
938 read_block(block, blk);
939 for (i = 0; i < BLOCK_SIZE >> 2; i++)
940 add_zone2(i + (unsigned int *) blk, &chg_blk);
942 write_block(block, blk);
946 static void add_zone_dind(unsigned short *znr, int *corrected)
948 static char blk[BLOCK_SIZE];
952 block = add_zone(znr, corrected);
955 read_block(block, blk);
956 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
957 add_zone_ind(i + (unsigned short *) blk, &blk_chg);
959 write_block(block, blk);
963 static void add_zone_dind2(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_ind2(i + (unsigned int *) blk, &blk_chg);
976 write_block(block, blk);
979 static void add_zone_tind2(unsigned int *znr, int *corrected)
981 static char blk[BLOCK_SIZE];
985 block = add_zone2(znr, corrected);
988 read_block(block, blk);
989 for (i = 0; i < BLOCK_SIZE >> 2; i++)
990 add_zone_dind2(i + (unsigned int *) blk, &blk_chg);
992 write_block(block, blk);
996 static void check_zones(unsigned int i)
998 struct minix_inode *inode;
1000 if (!i || i > INODES)
1002 if (inode_count[i] > 1) /* have we counted this file already? */
1005 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
1006 !S_ISLNK(inode->i_mode)) return;
1007 for (i = 0; i < 7; i++)
1008 add_zone(i + inode->i_zone, &changed);
1009 add_zone_ind(7 + inode->i_zone, &changed);
1010 add_zone_dind(8 + inode->i_zone, &changed);
1014 static void check_zones2(unsigned int i)
1016 struct minix2_inode *inode;
1018 if (!i || i > INODES)
1020 if (inode_count[i] > 1) /* have we counted this file already? */
1023 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
1024 && !S_ISLNK(inode->i_mode))
1026 for (i = 0; i < 7; i++)
1027 add_zone2(i + inode->i_zone, &changed);
1028 add_zone_ind2(7 + inode->i_zone, &changed);
1029 add_zone_dind2(8 + inode->i_zone, &changed);
1030 add_zone_tind2(9 + inode->i_zone, &changed);
1034 static void check_file(struct minix_inode *dir, unsigned int offset)
1036 static char blk[BLOCK_SIZE];
1037 struct minix_inode *inode;
1042 block = map_block(dir, offset / BLOCK_SIZE);
1043 read_block(block, blk);
1044 name = blk + (offset % BLOCK_SIZE) + 2;
1045 ino = *(unsigned short *) (name - 2);
1047 print_current_name();
1048 printf(" contains a bad inode number for file '");
1049 printf("%.*s'.", namelen, name);
1050 if (ask(" Remove", 1)) {
1051 *(unsigned short *) (name - 2) = 0;
1052 write_block(block, blk);
1056 if (name_depth < MAX_DEPTH)
1057 strncpy(name_list[name_depth], name, namelen);
1059 inode = get_inode(ino);
1062 if (!inode || strcmp(".", name)) {
1063 print_current_name();
1064 printf(": bad directory: '.' isn't first\n");
1065 errors_uncorrected = 1;
1069 if (offset == dirsize) {
1070 if (!inode || strcmp("..", name)) {
1071 print_current_name();
1072 printf(": bad directory: '..' isn't second\n");
1073 errors_uncorrected = 1;
1079 if (name_depth < MAX_DEPTH)
1080 strncpy(name_list[name_depth], name, namelen);
1084 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1085 print_current_name();
1086 if (S_ISDIR(inode->i_mode))
1092 if (inode && S_ISDIR(inode->i_mode))
1093 recursive_check(ino);
1099 static void check_file2(struct minix2_inode *dir, unsigned int offset)
1101 static char blk[BLOCK_SIZE];
1102 struct minix2_inode *inode;
1107 block = map_block2(dir, offset / BLOCK_SIZE);
1108 read_block(block, blk);
1109 name = blk + (offset % BLOCK_SIZE) + 2;
1110 ino = *(unsigned short *) (name - 2);
1112 print_current_name();
1113 printf(" contains a bad inode number for file '");
1114 printf("%.*s'.", namelen, name);
1115 if (ask(" Remove", 1)) {
1116 *(unsigned short *) (name - 2) = 0;
1117 write_block(block, blk);
1121 if (name_depth < MAX_DEPTH)
1122 strncpy(name_list[name_depth], name, namelen);
1124 inode = get_inode2(ino);
1127 if (!inode || strcmp(".", name)) {
1128 print_current_name();
1129 printf(": bad directory: '.' isn't first\n");
1130 errors_uncorrected = 1;
1134 if (offset == dirsize) {
1135 if (!inode || strcmp("..", name)) {
1136 print_current_name();
1137 printf(": bad directory: '..' isn't second\n");
1138 errors_uncorrected = 1;
1147 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1148 print_current_name();
1149 if (S_ISDIR(inode->i_mode))
1155 if (inode && S_ISDIR(inode->i_mode))
1156 recursive_check2(ino);
1162 static void recursive_check(unsigned int ino)
1164 struct minix_inode *dir;
1165 unsigned int offset;
1168 if (!S_ISDIR(dir->i_mode))
1169 die("internal error");
1170 if (dir->i_size < 2 * dirsize) {
1171 print_current_name();
1172 printf(": bad directory: size<32");
1173 errors_uncorrected = 1;
1175 for (offset = 0; offset < dir->i_size; offset += dirsize)
1176 check_file(dir, offset);
1180 static void recursive_check2(unsigned int ino)
1182 struct minix2_inode *dir;
1183 unsigned int offset;
1186 if (!S_ISDIR(dir->i_mode))
1187 die("internal error");
1188 if (dir->i_size < 2 * dirsize) {
1189 print_current_name();
1190 printf(": bad directory: size < 32");
1191 errors_uncorrected = 1;
1193 for (offset = 0; offset < dir->i_size; offset += dirsize)
1194 check_file2(dir, offset);
1198 static int bad_zone(int i)
1202 if (BLOCK_SIZE * i != lseek(IN, BLOCK_SIZE * i, SEEK_SET))
1203 die("seek failed in bad_zone");
1204 return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE));
1207 static void check_counts(void)
1211 for (i = 1; i <= INODES; i++) {
1212 if (!inode_in_use(i) && Inode[i].i_mode && warn_mode) {
1213 printf("Inode %d mode not cleared.", i);
1214 if (ask("Clear", 1)) {
1215 Inode[i].i_mode = 0;
1219 if (!inode_count[i]) {
1220 if (!inode_in_use(i))
1222 printf("Inode %d not used, marked used in the bitmap.", i);
1223 if (ask("Clear", 1))
1227 if (!inode_in_use(i)) {
1228 printf("Inode %d used, marked unused in the bitmap.", i);
1232 if (Inode[i].i_nlinks != inode_count[i]) {
1233 printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
1234 i, Inode[i].i_mode, Inode[i].i_nlinks, inode_count[i]);
1235 if (ask("Set i_nlinks to count", 1)) {
1236 Inode[i].i_nlinks = inode_count[i];
1241 for (i = FIRSTZONE; i < ZONES; i++) {
1242 if (zone_in_use(i) == zone_count[i])
1244 if (!zone_count[i]) {
1247 printf("Zone %d: marked in use, no file uses it.", i);
1248 if (ask("Unmark", 1))
1252 printf("Zone %d: %sin use, counted=%d\n",
1253 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1258 static void check_counts2(void)
1262 for (i = 1; i <= INODES; i++) {
1263 if (!inode_in_use(i) && Inode2[i].i_mode && warn_mode) {
1264 printf("Inode %d mode not cleared.", i);
1265 if (ask("Clear", 1)) {
1266 Inode2[i].i_mode = 0;
1270 if (!inode_count[i]) {
1271 if (!inode_in_use(i))
1273 printf("Inode %d not used, marked used in the bitmap.", i);
1274 if (ask("Clear", 1))
1278 if (!inode_in_use(i)) {
1279 printf("Inode %d used, marked unused in the bitmap.", i);
1283 if (Inode2[i].i_nlinks != inode_count[i]) {
1284 printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
1285 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1287 if (ask("Set i_nlinks to count", 1)) {
1288 Inode2[i].i_nlinks = inode_count[i];
1293 for (i = FIRSTZONE; i < ZONES; i++) {
1294 if (zone_in_use(i) == zone_count[i])
1296 if (!zone_count[i]) {
1299 printf("Zone %d: marked in use, no file uses it.", i);
1300 if (ask("Unmark", 1))
1304 printf("Zone %d: %sin use, counted=%d\n",
1305 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1310 static void check(void)
1312 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1313 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1314 check_zones(ROOT_INO);
1315 recursive_check(ROOT_INO);
1320 static void check2(void)
1322 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1323 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1324 check_zones2(ROOT_INO);
1325 recursive_check2(ROOT_INO);
1330 /* Wed Feb 9 15:17:06 MST 2000 */
1331 /* dynamically allocate name_list (instead of making it static) */
1332 static void alloc_name_list(void)
1336 name_list = xmalloc(sizeof(char *) * MAX_DEPTH);
1337 for (i = 0; i < MAX_DEPTH; i++)
1338 name_list[i] = xmalloc(sizeof(char) * BUFSIZ + 1);
1342 /* execute this atexit() to deallocate name_list[] */
1343 /* piptigger was here */
1344 static void free_name_list(void)
1349 for (i = 0; i < MAX_DEPTH; i++) {
1359 extern int fsck_minix_main(int argc, char **argv)
1366 /* Don't bother to free memory. Exit does
1367 * that automagically, so we can save a few bytes */
1368 //atexit(free_name_list);
1371 program_name = *argv;
1372 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
1373 die("bad inode size");
1375 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
1376 die("bad v2 inode size");
1378 while (argc-- > 1) {
1380 if (argv[0][0] != '-') {
1384 device_name = argv[0];
1387 switch (argv[0][0]) {
1417 check_mount(); /* trying to check a mounted filesystem? */
1418 if (repair && !automatic) {
1419 if (!isatty(0) || !isatty(1))
1420 die("need terminal for interactive repairs");
1422 IN = open(device_name, repair ? O_RDWR : O_RDONLY);
1424 die("unable to open '%s'");
1425 for (count = 0; count < 3; count++)
1430 * Determine whether or not we should continue with the checking.
1431 * This is based on the status of the filesystem valid and error
1432 * flags and whether or not the -f switch was specified on the
1435 printf("%s, %s\n", program_name, program_version);
1436 if (!(Super.s_state & MINIX_ERROR_FS) &&
1437 (Super.s_state & MINIX_VALID_FS) && !force) {
1439 printf("%s is clean, no check.\n", device_name);
1442 printf("Forcing filesystem check on %s.\n", device_name);
1444 printf("Filesystem on %s is dirty, needs checking.\n",
1449 if (repair && !automatic) {
1450 tcgetattr(0, &termios);
1452 tmp.c_lflag &= ~(ICANON | ECHO);
1453 tcsetattr(0, TCSANOW, &tmp);
1469 for (i = 1, free = 0; i <= INODES; i++)
1470 if (!inode_in_use(i))
1472 printf("\n%6ld inodes used (%ld%%)\n", (INODES - free),
1473 100 * (INODES - free) / INODES);
1474 for (i = FIRSTZONE, free = 0; i < ZONES; i++)
1475 if (!zone_in_use(i))
1477 printf("%6ld zones used (%ld%%)\n", (ZONES - free),
1478 100 * (ZONES - free) / ZONES);
1479 printf("\n%6d regular files\n"
1481 "%6d character device files\n"
1482 "%6d block device files\n"
1484 "%6d symbolic links\n"
1487 regular, directory, chardev, blockdev,
1488 links - 2 * directory + 1, symlinks,
1489 total - 2 * directory + 1);
1493 printf("----------------------------\n"
1494 "FILE SYSTEM HAS BEEN CHANGED\n"
1495 "----------------------------\n");
1496 for (count = 0; count < 3; count++)
1499 write_super_block();
1501 if (repair && !automatic)
1502 tcsetattr(0, TCSANOW, &termios);
1506 if (errors_uncorrected)