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>
102 #include <linux/fs.h>
103 #include <linux/minix_fs.h>
105 #ifdef MINIX2_SUPER_MAGIC2
106 #define HAVE_MINIX2 1
115 #define UPPER(size,n) ((size+((n)-1))/(n))
116 #define INODE_SIZE (sizeof(struct minix_inode))
118 #define INODE_SIZE2 (sizeof(struct minix2_inode))
119 #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
120 : MINIX_INODES_PER_BLOCK))
122 #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
124 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
126 #define BITS_PER_BLOCK (BLOCK_SIZE<<3)
128 static char *program_name = "fsck.minix";
129 static char *program_version = "1.2 - 11/11/96";
130 static char *device_name = NULL;
132 static int repair = 0, automatic = 0, verbose = 0, list = 0, show =
133 0, warn_mode = 0, force = 0;
134 static int directory = 0, regular = 0, blockdev = 0, chardev = 0, links =
135 0, symlinks = 0, total = 0;
137 static int changed = 0; /* flags if the filesystem has been changed */
138 static int errors_uncorrected = 0; /* flag if some error was not corrected */
139 static int dirsize = 16;
140 static int namelen = 14;
141 static int version2 = 0;
142 static struct termios termios;
143 static int termios_set = 0;
147 static int name_depth = 0;
148 // static char name_list[MAX_DEPTH][BUFSIZ + 1];
149 static char **name_list = NULL;
151 static char *inode_buffer = NULL;
153 #define Inode (((struct minix_inode *) inode_buffer)-1)
154 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
155 static char super_block_buffer[BLOCK_SIZE];
157 #define Super (*(struct minix_super_block *)super_block_buffer)
158 #define INODES ((unsigned long)Super.s_ninodes)
160 #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
162 #define ZONES ((unsigned long)(Super.s_nzones))
164 #define IMAPS ((unsigned long)Super.s_imap_blocks)
165 #define ZMAPS ((unsigned long)Super.s_zmap_blocks)
166 #define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
167 #define ZONESIZE ((unsigned long)Super.s_log_zone_size)
168 #define MAXSIZE ((unsigned long)Super.s_max_size)
169 #define MAGIC (Super.s_magic)
170 #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
172 static char *inode_map;
173 static char *zone_map;
175 static unsigned char *inode_count = NULL;
176 static unsigned char *zone_count = NULL;
178 static void recursive_check(unsigned int ino);
179 static void recursive_check2(unsigned int ino);
181 #define inode_in_use(x) (isset(inode_map,(x)))
182 #define zone_in_use(x) (isset(zone_map,(x)-FIRSTZONE+1))
184 #define mark_inode(x) (setbit(inode_map,(x)),changed=1)
185 #define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
187 #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1)
188 #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1)
190 static void leave(int) __attribute__ ((noreturn));
191 static void leave(int status)
194 tcsetattr(0, TCSANOW, &termios);
198 static void show_usage(void)
200 fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n",
202 fprintf(stderr, "Usage: %s [-larvsmf] /dev/name\n", program_name);
203 #ifndef BB_FEATURE_TRIVIAL_HELP
205 "\nPerforms a consistency check for MINIX filesystems.\n\n");
206 fprintf(stderr, "Options:\n");
207 fprintf(stderr, "\t-l\tLists all filenames\n");
208 fprintf(stderr, "\t-r\tPerform interactive repairs\n");
209 fprintf(stderr, "\t-a\tPerform automatic repairs\n");
210 fprintf(stderr, "\t-v\tverbose\n");
211 fprintf(stderr, "\t-s\tOutputs super-block information\n");
213 "\t-m\tActivates MINIX-like \"mode not cleared\" warnings\n");
214 fprintf(stderr, "\t-f\tForce file system check.\n\n");
219 static void die(const char *str)
221 fprintf(stderr, "%s: %s\n", program_name, str);
226 * This simply goes through the file-name data and prints out the
229 static void print_current_name(void)
233 while (i < name_depth)
234 printf("/%.*s", namelen, name_list[i++]);
239 static int ask(const char *string, int def)
245 errors_uncorrected = 1;
251 errors_uncorrected = 1;
254 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
257 if ((c = getchar()) == EOF) {
259 errors_uncorrected = 1;
266 } else if (c == 'N') {
269 } else if (c == ' ' || c == '\n')
276 errors_uncorrected = 1;
282 * Make certain that we aren't checking a filesystem that is on a
283 * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
284 * 1994 Theodore Ts'o. Also licensed under GPL.
286 static void check_mount(void)
293 if ((f = setmntent(MOUNTED, "r")) == NULL)
295 while ((mnt = getmntent(f)) != NULL)
296 if (strcmp(device_name, mnt->mnt_fsname) == 0)
303 * If the root is mounted read-only, then /etc/mtab is
304 * probably not correct; so we won't issue a warning based on
307 fd = open(MOUNTED, O_RDWR);
308 if (fd < 0 && errno == EROFS)
313 printf("%s is mounted. ", device_name);
314 if (isatty(0) && isatty(1))
315 cont = ask("Do you really want to continue", 0);
319 printf("check aborted.\n");
326 * check_zone_nr checks to see that *nr is a valid zone nr. If it
327 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
328 * if an error was corrected, and returns the zone (0 for no zone
329 * or a bad zone-number).
331 static int check_zone_nr(unsigned short *nr, int *corrected)
336 printf("Zone nr < FIRSTZONE in file `");
337 else if (*nr >= ZONES)
338 printf("Zone nr >= ZONES in file `");
341 print_current_name();
343 if (ask("Remove block", 1)) {
351 static int check_zone_nr2(unsigned int *nr, int *corrected)
356 printf("Zone nr < FIRSTZONE in file `");
357 else if (*nr >= ZONES)
358 printf("Zone nr >= ZONES in file `");
361 print_current_name();
363 if (ask("Remove block", 1)) {
372 * read-block reads block nr into the buffer at addr.
374 static void read_block(unsigned int nr, char *addr)
377 memset(addr, 0, BLOCK_SIZE);
380 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) {
381 printf("Read error: unable to seek to block in file '");
382 print_current_name();
384 memset(addr, 0, BLOCK_SIZE);
385 errors_uncorrected = 1;
386 } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) {
387 printf("Read error: bad block in file '");
388 print_current_name();
390 memset(addr, 0, BLOCK_SIZE);
391 errors_uncorrected = 1;
396 * write_block writes block nr to disk.
398 static void write_block(unsigned int nr, char *addr)
402 if (nr < FIRSTZONE || nr >= ZONES) {
403 printf("Internal error: trying to write bad block\n"
404 "Write request ignored\n");
405 errors_uncorrected = 1;
408 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET))
409 die("seek failed in write_block");
410 if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) {
411 printf("Write error: bad block in file '");
412 print_current_name();
414 errors_uncorrected = 1;
419 * map-block calculates the absolute block nr of a block in a file.
420 * It sets 'changed' if the inode has needed changing, and re-writes
421 * any indirect blocks with errors.
423 static int map_block(struct minix_inode *inode, unsigned int blknr)
425 unsigned short ind[BLOCK_SIZE >> 1];
426 unsigned short dind[BLOCK_SIZE >> 1];
427 int blk_chg, block, result;
430 return check_zone_nr(inode->i_zone + blknr, &changed);
433 block = check_zone_nr(inode->i_zone + 7, &changed);
434 read_block(block, (char *) ind);
436 result = check_zone_nr(blknr + ind, &blk_chg);
438 write_block(block, (char *) ind);
442 block = check_zone_nr(inode->i_zone + 8, &changed);
443 read_block(block, (char *) dind);
445 result = check_zone_nr(dind + (blknr / 512), &blk_chg);
447 write_block(block, (char *) dind);
449 read_block(block, (char *) ind);
451 result = check_zone_nr(ind + (blknr % 512), &blk_chg);
453 write_block(block, (char *) ind);
458 static int map_block2(struct minix2_inode *inode, unsigned int blknr)
460 unsigned int ind[BLOCK_SIZE >> 2];
461 unsigned int dind[BLOCK_SIZE >> 2];
462 unsigned int tind[BLOCK_SIZE >> 2];
463 int blk_chg, block, result;
466 return check_zone_nr2(inode->i_zone + blknr, &changed);
469 block = check_zone_nr2(inode->i_zone + 7, &changed);
470 read_block(block, (char *) ind);
472 result = check_zone_nr2(blknr + ind, &blk_chg);
474 write_block(block, (char *) ind);
478 if (blknr >= 256 * 256) {
479 block = check_zone_nr2(inode->i_zone + 8, &changed);
480 read_block(block, (char *) dind);
482 result = check_zone_nr2(dind + blknr / 256, &blk_chg);
484 write_block(block, (char *) dind);
486 read_block(block, (char *) ind);
488 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
490 write_block(block, (char *) ind);
494 block = check_zone_nr2(inode->i_zone + 9, &changed);
495 read_block(block, (char *) tind);
497 result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
499 write_block(block, (char *) tind);
501 read_block(block, (char *) dind);
503 result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
505 write_block(block, (char *) dind);
507 read_block(block, (char *) ind);
509 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
511 write_block(block, (char *) ind);
516 static void write_super_block(void)
519 * Set the state of the filesystem based on whether or not there
520 * are uncorrected errors. The filesystem valid flag is
521 * unconditionally set if we get this far.
523 Super.s_state |= MINIX_VALID_FS;
524 if (errors_uncorrected)
525 Super.s_state |= MINIX_ERROR_FS;
527 Super.s_state &= ~MINIX_ERROR_FS;
529 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
530 die("seek failed in write_super_block");
531 if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE))
532 die("unable to write super-block");
537 static void write_tables(void)
541 if (IMAPS * BLOCK_SIZE != write(IN, inode_map, IMAPS * BLOCK_SIZE))
542 die("Unable to write inode map");
543 if (ZMAPS * BLOCK_SIZE != write(IN, zone_map, ZMAPS * BLOCK_SIZE))
544 die("Unable to write zone map");
545 if (INODE_BUFFER_SIZE != write(IN, inode_buffer, INODE_BUFFER_SIZE))
546 die("Unable to write inodes");
549 static void get_dirsize(void)
552 char blk[BLOCK_SIZE];
557 block = Inode2[ROOT_INO].i_zone[0];
560 block = Inode[ROOT_INO].i_zone[0];
561 read_block(block, blk);
562 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
563 if (strcmp(blk + size + 2, "..") == 0) {
572 static void read_superblock(void)
574 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
576 if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
577 die("unable to read super block");
578 if (MAGIC == MINIX_SUPER_MAGIC) {
582 } else if (MAGIC == MINIX_SUPER_MAGIC2) {
587 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
591 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
597 die("bad magic number in super-block");
598 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
599 die("Only 1k blocks/zones supported");
600 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
601 die("bad s_imap_blocks field in super-block");
602 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
603 die("bad s_zmap_blocks field in super-block");
606 static void read_tables(void)
608 inode_map = xmalloc(IMAPS * BLOCK_SIZE);
609 zone_map = xmalloc(ZMAPS * BLOCK_SIZE);
610 memset(inode_map, 0, sizeof(inode_map));
611 memset(zone_map, 0, sizeof(zone_map));
612 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
613 inode_count = xmalloc(INODES + 1);
614 zone_count = xmalloc(ZONES);
615 if (IMAPS * BLOCK_SIZE != read(IN, inode_map, IMAPS * BLOCK_SIZE))
616 die("Unable to read inode map");
617 if (ZMAPS * BLOCK_SIZE != read(IN, zone_map, ZMAPS * BLOCK_SIZE))
618 die("Unable to read zone map");
619 if (INODE_BUFFER_SIZE != read(IN, inode_buffer, INODE_BUFFER_SIZE))
620 die("Unable to read inodes");
621 if (NORM_FIRSTZONE != FIRSTZONE) {
622 printf("Warning: Firstzone != Norm_firstzone\n");
623 errors_uncorrected = 1;
627 printf("%ld inodes\n", INODES);
628 printf("%ld blocks\n", ZONES);
629 printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE);
630 printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE);
631 printf("Maxsize=%ld\n", MAXSIZE);
632 printf("Filesystem state=%d\n", Super.s_state);
633 printf("namelen=%d\n\n", namelen);
637 struct minix_inode *get_inode(unsigned int nr)
639 struct minix_inode *inode;
641 if (!nr || nr > INODES)
645 if (!inode_count[nr]) {
646 if (!inode_in_use(nr)) {
647 printf("Inode %d marked not used, but used for file '", nr);
648 print_current_name();
651 if (ask("Mark in use", 1))
654 errors_uncorrected = 1;
657 if (S_ISDIR(inode->i_mode))
659 else if (S_ISREG(inode->i_mode))
661 else if (S_ISCHR(inode->i_mode))
663 else if (S_ISBLK(inode->i_mode))
665 else if (S_ISLNK(inode->i_mode))
667 else if (S_ISSOCK(inode->i_mode));
668 else if (S_ISFIFO(inode->i_mode));
670 print_current_name();
671 printf(" has mode %05o\n", inode->i_mode);
676 if (!++inode_count[nr]) {
677 printf("Warning: inode count too big.\n");
679 errors_uncorrected = 1;
685 struct minix2_inode *get_inode2(unsigned int nr)
687 struct minix2_inode *inode;
689 if (!nr || nr > INODES)
693 if (!inode_count[nr]) {
694 if (!inode_in_use(nr)) {
695 printf("Inode %d marked not used, but used for file '", nr);
696 print_current_name();
699 if (ask("Mark in use", 1))
702 errors_uncorrected = 1;
705 if (S_ISDIR(inode->i_mode))
707 else if (S_ISREG(inode->i_mode))
709 else if (S_ISCHR(inode->i_mode))
711 else if (S_ISBLK(inode->i_mode))
713 else if (S_ISLNK(inode->i_mode))
715 else if (S_ISSOCK(inode->i_mode));
716 else if (S_ISFIFO(inode->i_mode));
718 print_current_name();
719 printf(" has mode %05o\n", inode->i_mode);
723 if (!++inode_count[nr]) {
724 printf("Warning: inode count too big.\n");
726 errors_uncorrected = 1;
732 static void check_root(void)
734 struct minix_inode *inode = Inode + ROOT_INO;
736 if (!inode || !S_ISDIR(inode->i_mode))
737 die("root inode isn't a directory");
741 static void check_root2(void)
743 struct minix2_inode *inode = Inode2 + ROOT_INO;
745 if (!inode || !S_ISDIR(inode->i_mode))
746 die("root inode isn't a directory");
750 static int add_zone(unsigned short *znr, int *corrected)
756 block = check_zone_nr(znr, corrected);
759 if (zone_count[block]) {
760 printf("Block has been used before. Now in file `");
761 print_current_name();
763 if (ask("Clear", 1)) {
771 if (!zone_in_use(block)) {
772 printf("Block %d in file `", block);
773 print_current_name();
774 printf("' is marked not in use.");
775 if (ask("Correct", 1))
778 if (!++zone_count[block])
784 static int add_zone2(unsigned int *znr, int *corrected)
790 block = check_zone_nr2(znr, corrected);
793 if (zone_count[block]) {
794 printf("Block has been used before. Now in file `");
795 print_current_name();
797 if (ask("Clear", 1)) {
805 if (!zone_in_use(block)) {
806 printf("Block %d in file `", block);
807 print_current_name();
808 printf("' is marked not in use.");
809 if (ask("Correct", 1))
812 if (!++zone_count[block])
818 static void add_zone_ind(unsigned short *znr, int *corrected)
820 static char blk[BLOCK_SIZE];
824 block = add_zone(znr, corrected);
827 read_block(block, blk);
828 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
829 add_zone(i + (unsigned short *) blk, &chg_blk);
831 write_block(block, blk);
835 static void add_zone_ind2(unsigned int *znr, int *corrected)
837 static char blk[BLOCK_SIZE];
841 block = add_zone2(znr, corrected);
844 read_block(block, blk);
845 for (i = 0; i < BLOCK_SIZE >> 2; i++)
846 add_zone2(i + (unsigned int *) blk, &chg_blk);
848 write_block(block, blk);
852 static void add_zone_dind(unsigned short *znr, int *corrected)
854 static char blk[BLOCK_SIZE];
858 block = add_zone(znr, corrected);
861 read_block(block, blk);
862 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
863 add_zone_ind(i + (unsigned short *) blk, &blk_chg);
865 write_block(block, blk);
869 static void add_zone_dind2(unsigned int *znr, int *corrected)
871 static char blk[BLOCK_SIZE];
875 block = add_zone2(znr, corrected);
878 read_block(block, blk);
879 for (i = 0; i < BLOCK_SIZE >> 2; i++)
880 add_zone_ind2(i + (unsigned int *) blk, &blk_chg);
882 write_block(block, blk);
885 static void add_zone_tind2(unsigned int *znr, int *corrected)
887 static char blk[BLOCK_SIZE];
891 block = add_zone2(znr, corrected);
894 read_block(block, blk);
895 for (i = 0; i < BLOCK_SIZE >> 2; i++)
896 add_zone_dind2(i + (unsigned int *) blk, &blk_chg);
898 write_block(block, blk);
902 static void check_zones(unsigned int i)
904 struct minix_inode *inode;
906 if (!i || i > INODES)
908 if (inode_count[i] > 1) /* have we counted this file already? */
911 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
912 !S_ISLNK(inode->i_mode)) return;
913 for (i = 0; i < 7; i++)
914 add_zone(i + inode->i_zone, &changed);
915 add_zone_ind(7 + inode->i_zone, &changed);
916 add_zone_dind(8 + inode->i_zone, &changed);
920 static void check_zones2(unsigned int i)
922 struct minix2_inode *inode;
924 if (!i || i > INODES)
926 if (inode_count[i] > 1) /* have we counted this file already? */
929 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
930 && !S_ISLNK(inode->i_mode))
932 for (i = 0; i < 7; i++)
933 add_zone2(i + inode->i_zone, &changed);
934 add_zone_ind2(7 + inode->i_zone, &changed);
935 add_zone_dind2(8 + inode->i_zone, &changed);
936 add_zone_tind2(9 + inode->i_zone, &changed);
940 static void check_file(struct minix_inode *dir, unsigned int offset)
942 static char blk[BLOCK_SIZE];
943 struct minix_inode *inode;
948 block = map_block(dir, offset / BLOCK_SIZE);
949 read_block(block, blk);
950 name = blk + (offset % BLOCK_SIZE) + 2;
951 ino = *(unsigned short *) (name - 2);
953 print_current_name();
954 printf(" contains a bad inode number for file '");
955 printf("%.*s'.", namelen, name);
956 if (ask(" Remove", 1)) {
957 *(unsigned short *) (name - 2) = 0;
958 write_block(block, blk);
962 if (name_depth < MAX_DEPTH)
963 strncpy(name_list[name_depth], name, namelen);
965 inode = get_inode(ino);
968 if (!inode || strcmp(".", name)) {
969 print_current_name();
970 printf(": bad directory: '.' isn't first\n");
971 errors_uncorrected = 1;
975 if (offset == dirsize) {
976 if (!inode || strcmp("..", name)) {
977 print_current_name();
978 printf(": bad directory: '..' isn't second\n");
979 errors_uncorrected = 1;
985 if (name_depth < MAX_DEPTH)
986 strncpy(name_list[name_depth], name, namelen);
990 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
991 print_current_name();
992 if (S_ISDIR(inode->i_mode))
998 if (inode && S_ISDIR(inode->i_mode))
999 recursive_check(ino);
1005 static void check_file2(struct minix2_inode *dir, unsigned int offset)
1007 static char blk[BLOCK_SIZE];
1008 struct minix2_inode *inode;
1013 block = map_block2(dir, offset / BLOCK_SIZE);
1014 read_block(block, blk);
1015 name = blk + (offset % BLOCK_SIZE) + 2;
1016 ino = *(unsigned short *) (name - 2);
1018 print_current_name();
1019 printf(" contains a bad inode number for file '");
1020 printf("%.*s'.", namelen, name);
1021 if (ask(" Remove", 1)) {
1022 *(unsigned short *) (name - 2) = 0;
1023 write_block(block, blk);
1027 if (name_depth < MAX_DEPTH)
1028 strncpy(name_list[name_depth], name, namelen);
1030 inode = get_inode2(ino);
1033 if (!inode || strcmp(".", name)) {
1034 print_current_name();
1035 printf(": bad directory: '.' isn't first\n");
1036 errors_uncorrected = 1;
1040 if (offset == dirsize) {
1041 if (!inode || strcmp("..", name)) {
1042 print_current_name();
1043 printf(": bad directory: '..' isn't second\n");
1044 errors_uncorrected = 1;
1053 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1054 print_current_name();
1055 if (S_ISDIR(inode->i_mode))
1061 if (inode && S_ISDIR(inode->i_mode))
1062 recursive_check2(ino);
1068 static void recursive_check(unsigned int ino)
1070 struct minix_inode *dir;
1071 unsigned int offset;
1074 if (!S_ISDIR(dir->i_mode))
1075 die("internal error");
1076 if (dir->i_size < 2 * dirsize) {
1077 print_current_name();
1078 printf(": bad directory: size<32");
1079 errors_uncorrected = 1;
1081 for (offset = 0; offset < dir->i_size; offset += dirsize)
1082 check_file(dir, offset);
1086 static void recursive_check2(unsigned int ino)
1088 struct minix2_inode *dir;
1089 unsigned int offset;
1092 if (!S_ISDIR(dir->i_mode))
1093 die("internal error");
1094 if (dir->i_size < 2 * dirsize) {
1095 print_current_name();
1096 printf(": bad directory: size < 32");
1097 errors_uncorrected = 1;
1099 for (offset = 0; offset < dir->i_size; offset += dirsize)
1100 check_file2(dir, offset);
1104 static int bad_zone(int i)
1108 if (BLOCK_SIZE * i != lseek(IN, BLOCK_SIZE * i, SEEK_SET))
1109 die("seek failed in bad_zone");
1110 return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE));
1113 static void check_counts(void)
1117 for (i = 1; i <= INODES; i++) {
1118 if (!inode_in_use(i) && Inode[i].i_mode && warn_mode) {
1119 printf("Inode %d mode not cleared.", i);
1120 if (ask("Clear", 1)) {
1121 Inode[i].i_mode = 0;
1125 if (!inode_count[i]) {
1126 if (!inode_in_use(i))
1128 printf("Inode %d not used, marked used in the bitmap.", i);
1129 if (ask("Clear", 1))
1133 if (!inode_in_use(i)) {
1134 printf("Inode %d used, marked unused in the bitmap.", i);
1138 if (Inode[i].i_nlinks != inode_count[i]) {
1139 printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
1140 i, Inode[i].i_mode, Inode[i].i_nlinks, inode_count[i]);
1141 if (ask("Set i_nlinks to count", 1)) {
1142 Inode[i].i_nlinks = inode_count[i];
1147 for (i = FIRSTZONE; i < ZONES; i++) {
1148 if (zone_in_use(i) == zone_count[i])
1150 if (!zone_count[i]) {
1153 printf("Zone %d: marked in use, no file uses it.", i);
1154 if (ask("Unmark", 1))
1158 printf("Zone %d: %sin use, counted=%d\n",
1159 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1164 static void check_counts2(void)
1168 for (i = 1; i <= INODES; i++) {
1169 if (!inode_in_use(i) && Inode2[i].i_mode && warn_mode) {
1170 printf("Inode %d mode not cleared.", i);
1171 if (ask("Clear", 1)) {
1172 Inode2[i].i_mode = 0;
1176 if (!inode_count[i]) {
1177 if (!inode_in_use(i))
1179 printf("Inode %d not used, marked used in the bitmap.", i);
1180 if (ask("Clear", 1))
1184 if (!inode_in_use(i)) {
1185 printf("Inode %d used, marked unused in the bitmap.", i);
1189 if (Inode2[i].i_nlinks != inode_count[i]) {
1190 printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.",
1191 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1193 if (ask("Set i_nlinks to count", 1)) {
1194 Inode2[i].i_nlinks = inode_count[i];
1199 for (i = FIRSTZONE; i < ZONES; i++) {
1200 if (zone_in_use(i) == zone_count[i])
1202 if (!zone_count[i]) {
1205 printf("Zone %d: marked in use, no file uses it.", i);
1206 if (ask("Unmark", 1))
1210 printf("Zone %d: %sin use, counted=%d\n",
1211 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1216 static void check(void)
1218 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1219 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1220 check_zones(ROOT_INO);
1221 recursive_check(ROOT_INO);
1226 static void check2(void)
1228 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1229 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1230 check_zones2(ROOT_INO);
1231 recursive_check2(ROOT_INO);
1236 /* Wed Feb 9 15:17:06 MST 2000 */
1237 /* dynamically allocate name_list (instead of making it static) */
1238 static void alloc_name_list(void)
1242 name_list = xmalloc(sizeof(char *) * MAX_DEPTH);
1243 for (i = 0; i < MAX_DEPTH; i++)
1244 name_list[i] = xmalloc(sizeof(char) * BUFSIZ + 1);
1248 /* execute this atexit() to deallocate name_list[] */
1249 /* piptigger was here */
1250 static void free_name_list(void)
1255 for (i = 0; i < MAX_DEPTH; i++) {
1265 extern int fsck_minix_main(int argc, char **argv)
1272 /* Don't bother to free memory. Exit does
1273 * that automagically, so we can save a few bytes */
1274 //atexit(free_name_list);
1277 program_name = *argv;
1278 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
1279 die("bad inode size");
1281 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
1282 die("bad v2 inode size");
1284 while (argc-- > 1) {
1286 if (argv[0][0] != '-') {
1290 device_name = argv[0];
1293 switch (argv[0][0]) {
1323 check_mount(); /* trying to check a mounted filesystem? */
1324 if (repair && !automatic) {
1325 if (!isatty(0) || !isatty(1))
1326 die("need terminal for interactive repairs");
1328 IN = open(device_name, repair ? O_RDWR : O_RDONLY);
1330 die("unable to open '%s'");
1331 for (count = 0; count < 3; count++)
1336 * Determine whether or not we should continue with the checking.
1337 * This is based on the status of the filesystem valid and error
1338 * flags and whether or not the -f switch was specified on the
1341 printf("%s, %s\n", program_name, program_version);
1342 if (!(Super.s_state & MINIX_ERROR_FS) &&
1343 (Super.s_state & MINIX_VALID_FS) && !force) {
1345 printf("%s is clean, no check.\n", device_name);
1348 printf("Forcing filesystem check on %s.\n", device_name);
1350 printf("Filesystem on %s is dirty, needs checking.\n",
1355 if (repair && !automatic) {
1356 tcgetattr(0, &termios);
1358 tmp.c_lflag &= ~(ICANON | ECHO);
1359 tcsetattr(0, TCSANOW, &tmp);
1375 for (i = 1, free = 0; i <= INODES; i++)
1376 if (!inode_in_use(i))
1378 printf("\n%6ld inodes used (%ld%%)\n", (INODES - free),
1379 100 * (INODES - free) / INODES);
1380 for (i = FIRSTZONE, free = 0; i < ZONES; i++)
1381 if (!zone_in_use(i))
1383 printf("%6ld zones used (%ld%%)\n", (ZONES - free),
1384 100 * (ZONES - free) / ZONES);
1385 printf("\n%6d regular files\n"
1387 "%6d character device files\n"
1388 "%6d block device files\n"
1390 "%6d symbolic links\n"
1393 regular, directory, chardev, blockdev,
1394 links - 2 * directory + 1, symlinks,
1395 total - 2 * directory + 1);
1399 printf("----------------------------\n"
1400 "FILE SYSTEM HAS BEEN CHANGED\n"
1401 "----------------------------\n");
1402 for (count = 0; count < 3; count++)
1405 write_super_block();
1407 if (repair && !automatic)
1408 tcsetattr(0, TCSANOW, &termios);
1412 if (errors_uncorrected)