2 * fsck.minix.c - a file system consistency checker for Linux.
4 * (C) 1991, 1992 Linus Torvalds. This file may be redistributed
5 * as per the GNU copyleft.
9 * 09.11.91 - made the first rudimetary functions
11 * 10.11.91 - updated, does checking, no repairs yet.
12 * Sent out to the mailing-list for testing.
14 * 14.11.91 - Testing seems to have gone well. Added some
15 * correction-code, and changed some functions.
17 * 15.11.91 - More correction code. Hopefully it notices most
18 * cases now, and tries to do something about them.
20 * 16.11.91 - More corrections (thanks to Mika Jalava). Most
21 * things seem to work now. Yeah, sure.
24 * 19.04.92 - Had to start over again from this old version, as a
25 * kernel bug ate my enhanced fsck in february.
27 * 28.02.93 - added support for different directory entry sizes..
29 * Sat Mar 6 18:59:42 1993, faith@cs.unc.edu: Output namelen with
30 * super-block information
32 * Sat Oct 9 11:17:11 1993, faith@cs.unc.edu: make exit status conform
33 * to that required by fsutil
35 * Mon Jan 3 11:06:52 1994 - Dr. Wettstein (greg%wind.uucp@plains.nodak.edu)
36 * Added support for file system valid flag. Also
37 * added program_version variable and output of
38 * program name and version number when program
41 * 30.10.94 - added support for v2 filesystem
42 * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de)
44 * 10.12.94 - added test to prevent checking of mounted fs adapted
45 * from Theodore Ts'o's (tytso@athena.mit.edu) e2fsck
46 * program. (Daniel Quinlan, quinlan@yggdrasil.com)
48 * 01.07.96 - Fixed the v2 fs stuff to use the right #defines and such
49 * for modern libcs (janl@math.uio.no, Nicolai Langfeldt)
51 * 02.07.96 - Added C bit fiddling routines from rmk@ecs.soton.ac.uk
52 * (Russell King). He made them for ARM. It would seem
53 * that the ARM is powerful enough to do this in C whereas
54 * i386 and m64k must use assembly to get it fast >:-)
55 * This should make minix fsck systemindependent.
56 * (janl@math.uio.no, Nicolai Langfeldt)
58 * 04.11.96 - Added minor fixes from Andreas Schwab to avoid compiler
59 * warnings. Added mc68k bitops from
60 * Joerg Dorchain <dorchain@mpi-sb.mpg.de>.
62 * 06.11.96 - Added v2 code submitted by Joerg Dorchain, but written by
65 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
66 * - added Native Language Support
68 * 2008-04-06 James Youngman <jay@gnu.org>
69 * - Issue better error message if we fail to open the device.
70 * - Restore terminal state if we get a fatal signal.
73 * I've had no time to add comments - hopefully the function names
74 * are comments enough. As with all file system checkers, this assumes
75 * the file system is quiescent - don't use it on a mounted device
76 * unless you can be sure nobody is writing to it (and remember that the
77 * kernel can write to it when it searches for files).
79 * Usuage: fsck [-larvsm] device
80 * -l for a listing of all the filenames
81 * -a for automatic repairs (not implemented)
82 * -r for repairs (interactive) (not implemented)
83 * -v for verbose (tells how many files)
84 * -s for super-block info
85 * -m for minix-like "mode not cleared" warnings
86 * -f force filesystem check even if filesystem marked as valid
88 * The device may be a block device or a image of one, but this isn't
89 * enforced (but it's not much fun on a character device :-).
102 #include <sys/stat.h>
107 #include "pathnames.h"
116 #define UPPER(size,n) ((size+((n)-1))/(n))
117 #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))
121 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
123 #define BITS_PER_BLOCK (BLOCK_SIZE<<3)
125 static char * program_name = "fsck.minix";
126 static char * device_name = NULL;
128 static int repair=0, automatic=0, verbose=0, list=0, show=0, warn_mode=0,
130 static int directory=0, regular=0, blockdev=0, chardev=0, links=0,
133 static int changed = 0; /* flags if the filesystem has been changed */
134 static int errors_uncorrected = 0; /* flag if some error was not corrected */
135 static int dirsize = 16;
136 static int namelen = 14;
137 static int version2 = 0;
138 static struct termios termios;
139 static volatile sig_atomic_t termios_set = 0;
143 static int name_depth = 0;
144 static char name_list[MAX_DEPTH][NAME_MAX+1];
145 /* Copy of the previous, just for error reporting - see get_current_name */
146 /* This is a waste of 12kB or so. */
147 static char current_name[MAX_DEPTH*(NAME_MAX+1)+1];
149 static char * inode_buffer = NULL;
150 #define Inode (((struct minix_inode *) inode_buffer)-1)
151 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
152 static char super_block_buffer[BLOCK_SIZE];
153 #define Super (*(struct minix_super_block *)super_block_buffer)
154 #define INODES ((unsigned long)Super.s_ninodes)
155 #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
156 #define IMAPS ((unsigned long)Super.s_imap_blocks)
157 #define ZMAPS ((unsigned long)Super.s_zmap_blocks)
158 #define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
159 #define ZONESIZE ((unsigned long)Super.s_log_zone_size)
160 #define MAXSIZE ((unsigned long)Super.s_max_size)
161 #define MAGIC (Super.s_magic)
162 #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
164 static char *inode_map;
165 static char *zone_map;
167 static unsigned char * inode_count = NULL;
168 static unsigned char * zone_count = NULL;
170 static void recursive_check(unsigned int ino);
171 static void recursive_check2(unsigned int ino);
173 #define inode_in_use(x) (isset(inode_map,(x)) != 0)
174 #define zone_in_use(x) (isset(zone_map,(x)-FIRSTZONE+1) != 0)
176 #define mark_inode(x) (setbit(inode_map,(x)),changed=1)
177 #define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
179 #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1)
180 #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1)
185 tcsetattr(0, TCSANOW, &termios);
191 /* We received a fatal signal. Reset the terminal.
192 * Also reset the signal handler and re-send the signal,
193 * so that the parent process knows which signal actually
196 signal(sig, SIG_DFL);
210 _("Usage: %s [-larvsmf] /dev/name\n"),
215 static void die(const char *fmt, ...)
216 __attribute__ ((__format__ (__printf__, 1, 2)));
219 die(const char *fmt, ...) {
222 fprintf(stderr, "%s: ", program_name);
224 vfprintf(stderr, fmt, ap);
231 * This simply goes through the file-name data and prints out the
235 get_current_name(void) {
240 while (i < name_depth) {
253 ask(const char * string, int def) {
258 errors_uncorrected = 1;
264 errors_uncorrected = 1;
267 printf(def?"%s (y/n)? ":"%s (n/y)? ",string);
270 if ((c=getchar())==EOF) {
272 errors_uncorrected = 1;
279 } else if (c == 'N') {
282 } else if (c == ' ' || c == '\n')
289 errors_uncorrected = 1;
295 * Make certain that we aren't checking a filesystem that is on a
296 * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
297 * 1994 Theodore Ts'o. Also licensed under GPL.
306 if ((f = setmntent (_PATH_MOUNTED, "r")) == NULL)
308 while ((mnt = getmntent (f)) != NULL)
309 if (strcmp (device_name, mnt->mnt_fsname) == 0)
316 * If the root is mounted read-only, then /etc/mtab is
317 * probably not correct; so we won't issue a warning based on
320 fd = open(_PATH_MOUNTED, O_RDWR);
321 if (fd < 0 && errno == EROFS)
326 printf (_("%s is mounted. "), device_name);
327 if (isatty(0) && isatty(1))
328 cont = ask(_("Do you really want to continue"), 0);
332 printf (_("check aborted.\n"));
339 * check_zone_nr checks to see that *nr is a valid zone nr. If it
340 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
341 * if an error was corrected, and returns the zone (0 for no zone
342 * or a bad zone-number).
345 check_zone_nr(unsigned short * nr, int * corrected) {
349 if (*nr < FIRSTZONE) {
351 printf(_("Zone nr < FIRSTZONE in file `%s'."),
353 } else if (*nr >= ZONES) {
355 printf(_("Zone nr >= ZONES in file `%s'."),
360 if (ask(_("Remove block"),1)) {
368 check_zone_nr2 (unsigned int *nr, int *corrected) {
372 if (*nr < FIRSTZONE) {
374 printf (_("Zone nr < FIRSTZONE in file `%s'."),
376 } else if (*nr >= ZONES) {
378 printf (_("Zone nr >= ZONES in file `%s'."),
383 if (ask (_("Remove block"), 1)) {
391 * read-block reads block nr into the buffer at addr.
394 read_block(unsigned int nr, char * addr) {
396 memset(addr,0,BLOCK_SIZE);
399 if (BLOCK_SIZE*nr != lseek(IN, BLOCK_SIZE*nr, SEEK_SET)) {
401 printf(_("Read error: unable to seek to block in file '%s'\n"),
403 memset(addr,0,BLOCK_SIZE);
404 errors_uncorrected = 1;
405 } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) {
407 printf(_("Read error: bad block in file '%s'\n"),
409 memset(addr,0,BLOCK_SIZE);
410 errors_uncorrected = 1;
415 * write_block writes block nr to disk.
418 write_block(unsigned int nr, char * addr) {
421 if (nr < FIRSTZONE || nr >= ZONES) {
422 printf(_("Internal error: trying to write bad block\n"
423 "Write request ignored\n"));
424 errors_uncorrected = 1;
427 if (BLOCK_SIZE*nr != lseek(IN, BLOCK_SIZE*nr, SEEK_SET))
428 die(_("seek failed in write_block"));
429 if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) {
431 printf(_("Write error: bad block in file '%s'\n"),
433 errors_uncorrected = 1;
438 * map-block calculates the absolute block nr of a block in a file.
439 * It sets 'changed' if the inode has needed changing, and re-writes
440 * any indirect blocks with errors.
443 map_block(struct minix_inode * inode, unsigned int blknr) {
444 unsigned short ind[BLOCK_SIZE>>1];
445 unsigned short dind[BLOCK_SIZE>>1];
446 int blk_chg, block, result;
449 return check_zone_nr(inode->i_zone + blknr, &changed);
452 block = check_zone_nr(inode->i_zone + 7, &changed);
453 read_block(block, (char *) ind);
455 result = check_zone_nr(blknr + ind, &blk_chg);
457 write_block(block, (char *) ind);
461 block = check_zone_nr(inode->i_zone + 8, &changed);
462 read_block(block, (char *) dind);
464 result = check_zone_nr(dind + (blknr/512), &blk_chg);
466 write_block(block, (char *) dind);
468 read_block(block, (char *) ind);
470 result = check_zone_nr(ind + (blknr%512), &blk_chg);
472 write_block(block, (char *) ind);
477 map_block2 (struct minix2_inode *inode, unsigned int blknr) {
478 unsigned int ind[BLOCK_SIZE >> 2];
479 unsigned int dind[BLOCK_SIZE >> 2];
480 unsigned int tind[BLOCK_SIZE >> 2];
481 int blk_chg, block, result;
484 return check_zone_nr2 (inode->i_zone + blknr, &changed);
487 block = check_zone_nr2 (inode->i_zone + 7, &changed);
488 read_block (block, (char *) ind);
490 result = check_zone_nr2 (blknr + ind, &blk_chg);
492 write_block (block, (char *) ind);
496 if (blknr >= 256 * 256) {
497 block = check_zone_nr2 (inode->i_zone + 8, &changed);
498 read_block (block, (char *) dind);
500 result = check_zone_nr2 (dind + blknr / 256, &blk_chg);
502 write_block (block, (char *) dind);
504 read_block (block, (char *) ind);
506 result = check_zone_nr2 (ind + blknr % 256, &blk_chg);
508 write_block (block, (char *) ind);
512 block = check_zone_nr2 (inode->i_zone + 9, &changed);
513 read_block (block, (char *) tind);
515 result = check_zone_nr2 (tind + blknr / (256 * 256), &blk_chg);
517 write_block (block, (char *) tind);
519 read_block (block, (char *) dind);
521 result = check_zone_nr2 (dind + (blknr / 256) % 256, &blk_chg);
523 write_block (block, (char *) dind);
525 read_block (block, (char *) ind);
527 result = check_zone_nr2 (ind + blknr % 256, &blk_chg);
529 write_block (block, (char *) ind);
534 write_super_block(void) {
536 * Set the state of the filesystem based on whether or not there
537 * are uncorrected errors. The filesystem valid flag is
538 * unconditionally set if we get this far.
540 Super.s_state |= MINIX_VALID_FS;
541 if ( errors_uncorrected )
542 Super.s_state |= MINIX_ERROR_FS;
544 Super.s_state &= ~MINIX_ERROR_FS;
546 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
547 die(_("seek failed in write_super_block"));
548 if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE))
549 die(_("unable to write super-block"));
558 if (IMAPS*BLOCK_SIZE != write(IN,inode_map,IMAPS*BLOCK_SIZE))
559 die(_("Unable to write inode map"));
560 if (ZMAPS*BLOCK_SIZE != write(IN,zone_map,ZMAPS*BLOCK_SIZE))
561 die(_("Unable to write zone map"));
562 if (INODE_BUFFER_SIZE != write(IN,inode_buffer,INODE_BUFFER_SIZE))
563 die(_("Unable to write inodes"));
569 char blk[BLOCK_SIZE];
573 block = Inode2[ROOT_INO].i_zone[0];
575 block = Inode[ROOT_INO].i_zone[0];
576 read_block (block, blk);
577 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
578 if (strcmp (blk + size + 2, "..") == 0) {
588 read_superblock(void) {
589 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
590 die(_("seek failed"));
591 if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
592 die(_("unable to read super block"));
593 if (MAGIC == MINIX_SUPER_MAGIC) {
597 } else if (MAGIC == MINIX_SUPER_MAGIC2) {
601 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
605 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
610 die(_("bad magic number in super-block"));
611 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
612 die(_("Only 1k blocks/zones supported"));
613 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
614 die(_("bad s_imap_blocks field in super-block"));
615 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
616 die(_("bad s_zmap_blocks field in super-block"));
621 inode_map = malloc(IMAPS * BLOCK_SIZE);
623 die(_("Unable to allocate buffer for inode map"));
624 zone_map = malloc(ZMAPS * BLOCK_SIZE);
626 die(_("Unable to allocate buffer for zone map"));
627 memset(inode_map,0,sizeof(inode_map));
628 memset(zone_map,0,sizeof(zone_map));
629 inode_buffer = malloc(INODE_BUFFER_SIZE);
631 die(_("Unable to allocate buffer for inodes"));
632 inode_count = malloc(INODES + 1);
634 die(_("Unable to allocate buffer for inode count"));
635 zone_count = malloc(ZONES);
637 die(_("Unable to allocate buffer for zone count"));
638 if (IMAPS*BLOCK_SIZE != read(IN,inode_map,IMAPS*BLOCK_SIZE))
639 die(_("Unable to read inode map"));
640 if (ZMAPS*BLOCK_SIZE != read(IN,zone_map,ZMAPS*BLOCK_SIZE))
641 die(_("Unable to read zone map"));
642 if (INODE_BUFFER_SIZE != read(IN,inode_buffer,INODE_BUFFER_SIZE))
643 die(_("Unable to read inodes"));
644 if (NORM_FIRSTZONE != FIRSTZONE) {
645 printf(_("Warning: Firstzone != Norm_firstzone\n"));
646 errors_uncorrected = 1;
650 printf(_("%ld inodes\n"),INODES);
651 printf(_("%ld blocks\n"),ZONES);
652 printf(_("Firstdatazone=%ld (%ld)\n"),FIRSTZONE,NORM_FIRSTZONE);
653 printf(_("Zonesize=%d\n"),BLOCK_SIZE<<ZONESIZE);
654 printf(_("Maxsize=%ld\n"),MAXSIZE);
655 printf(_("Filesystem state=%d\n"), Super.s_state);
656 printf(_("namelen=%d\n\n"),namelen);
660 static struct minix_inode *
661 get_inode(unsigned int nr) {
662 struct minix_inode * inode;
664 if (!nr || nr > INODES)
668 if (!inode_count[nr]) {
669 if (!inode_in_use(nr)) {
671 printf(_("Inode %d marked unused, "
672 "but used for file '%s'\n"),
675 if (ask(_("Mark in use"),1))
678 errors_uncorrected = 1;
681 if (S_ISDIR(inode->i_mode))
683 else if (S_ISREG(inode->i_mode))
685 else if (S_ISCHR(inode->i_mode))
687 else if (S_ISBLK(inode->i_mode))
689 else if (S_ISLNK(inode->i_mode))
691 else if (S_ISSOCK(inode->i_mode))
693 else if (S_ISFIFO(inode->i_mode))
697 printf(_("The file `%s' has mode %05o\n"),
698 current_name, inode->i_mode);
703 if (!++inode_count[nr]) {
704 printf(_("Warning: inode count too big.\n"));
706 errors_uncorrected = 1;
711 static struct minix2_inode *
712 get_inode2 (unsigned int nr) {
713 struct minix2_inode *inode;
715 if (!nr || nr > INODES)
719 if (!inode_count[nr]) {
720 if (!inode_in_use (nr)) {
722 printf (_("Inode %d marked unused, "
723 "but used for file '%s'\n"),
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));
746 printf (_("The file `%s' has mode %05o\n"),
747 current_name, inode->i_mode);
751 if (!++inode_count[nr]) {
752 printf (_("Warning: inode count too big.\n"));
754 errors_uncorrected = 1;
761 struct minix_inode * inode = Inode + ROOT_INO;
763 if (!inode || !S_ISDIR(inode->i_mode))
764 die(_("root inode isn't a directory"));
769 struct minix2_inode *inode = Inode2 + ROOT_INO;
771 if (!inode || !S_ISDIR (inode->i_mode))
772 die(_("root inode isn't a directory"));
776 add_zone(unsigned short * znr, int * corrected) {
781 block = check_zone_nr(znr, corrected);
784 if (zone_count[block]) {
786 printf(_("Block has been used before. Now in file `%s'."),
788 if (ask(_("Clear"),1)) {
796 if (!zone_in_use(block)) {
798 printf(_("Block %d in file `%s' is marked not in use."),
799 block, current_name);
800 if (ask(_("Correct"),1))
803 if (!++zone_count[block])
809 add_zone2 (unsigned int *znr, int *corrected) {
814 block = check_zone_nr2 (znr, corrected);
817 if (zone_count[block]) {
819 printf (_("Block has been used before. Now in file `%s'."),
821 if (ask (_("Clear"), 1)) {
829 if (!zone_in_use (block)) {
831 printf (_("Block %d in file `%s' is marked not in use."),
832 block, current_name);
833 if (ask (_("Correct"), 1))
836 if (!++zone_count[block])
842 add_zone_ind(unsigned short * znr, int * corrected) {
843 static char blk[BLOCK_SIZE];
847 block = add_zone(znr, corrected);
850 read_block(block, blk);
851 for (i=0 ; i < (BLOCK_SIZE>>1) ; i++)
852 add_zone(i + (unsigned short *) blk, &chg_blk);
854 write_block(block, blk);
858 add_zone_ind2 (unsigned int *znr, int *corrected) {
859 static char blk[BLOCK_SIZE];
863 block = add_zone2 (znr, corrected);
866 read_block (block, blk);
867 for (i = 0; i < BLOCK_SIZE >> 2; i++)
868 add_zone2 (i + (unsigned int *) blk, &chg_blk);
870 write_block (block, blk);
874 add_zone_dind(unsigned short * znr, int * corrected) {
875 static char blk[BLOCK_SIZE];
879 block = add_zone(znr, corrected);
882 read_block(block, blk);
883 for (i=0 ; i < (BLOCK_SIZE>>1) ; i++)
884 add_zone_ind(i + (unsigned short *) blk, &blk_chg);
886 write_block(block, blk);
890 add_zone_dind2 (unsigned int *znr, int *corrected) {
891 static char blk[BLOCK_SIZE];
895 block = add_zone2 (znr, corrected);
898 read_block (block, blk);
899 for (i = 0; i < BLOCK_SIZE >> 2; i++)
900 add_zone_ind2 (i + (unsigned int *) blk, &blk_chg);
902 write_block (block, blk);
906 add_zone_tind2 (unsigned int *znr, int *corrected) {
907 static char blk[BLOCK_SIZE];
911 block = add_zone2 (znr, corrected);
914 read_block (block, blk);
915 for (i = 0; i < BLOCK_SIZE >> 2; i++)
916 add_zone_dind2 (i + (unsigned int *) blk, &blk_chg);
918 write_block (block, blk);
922 check_zones(unsigned int i) {
923 struct minix_inode * inode;
925 if (!i || i > INODES)
927 if (inode_count[i] > 1) /* have we counted this file already? */
930 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
931 !S_ISLNK(inode->i_mode))
933 for (i=0 ; i<7 ; i++)
934 add_zone(i + inode->i_zone, &changed);
935 add_zone_ind(7 + inode->i_zone, &changed);
936 add_zone_dind(8 + inode->i_zone, &changed);
940 check_zones2 (unsigned int i) {
941 struct minix2_inode *inode;
943 if (!i || i > INODES)
945 if (inode_count[i] > 1) /* have we counted this file already? */
948 if (!S_ISDIR (inode->i_mode) && !S_ISREG (inode->i_mode)
949 && !S_ISLNK (inode->i_mode))
951 for (i = 0; i < 7; i++)
952 add_zone2 (i + inode->i_zone, &changed);
953 add_zone_ind2 (7 + inode->i_zone, &changed);
954 add_zone_dind2 (8 + inode->i_zone, &changed);
955 add_zone_tind2 (9 + inode->i_zone, &changed);
959 check_file(struct minix_inode * dir, unsigned int offset) {
960 static char blk[BLOCK_SIZE];
961 struct minix_inode * inode;
966 block = map_block(dir,offset/BLOCK_SIZE);
967 read_block(block, blk);
968 name = blk + (offset % BLOCK_SIZE) + 2;
969 ino = * (unsigned short *) (name-2);
972 printf(_("The directory '%s' contains a bad inode number "
974 current_name, namelen, name);
975 if (ask(_(" Remove"),1)) {
976 *(unsigned short *)(name-2) = 0;
977 write_block(block, blk);
981 if (name_depth < MAX_DEPTH)
982 strncpy (name_list[name_depth], name, namelen);
984 inode = get_inode(ino);
987 if (!inode || strcmp(".",name)) {
989 printf(_("%s: bad directory: '.' isn't first\n"),
991 errors_uncorrected = 1;
994 if (offset == dirsize) {
995 if (!inode || strcmp("..",name)) {
997 printf(_("%s: bad directory: '..' isn't second\n"),
999 errors_uncorrected = 1;
1004 if (name_depth < MAX_DEPTH)
1005 strncpy(name_list[name_depth], name, namelen);
1009 printf("%6d %07o %3d ", ino,
1010 inode->i_mode, inode->i_nlinks);
1012 printf("%s", current_name);
1013 if (S_ISDIR(inode->i_mode))
1019 if (inode && S_ISDIR(inode->i_mode))
1020 recursive_check(ino);
1026 check_file2 (struct minix2_inode *dir, unsigned int offset) {
1027 static char blk[BLOCK_SIZE];
1028 struct minix2_inode *inode;
1033 block = map_block2 (dir, offset / BLOCK_SIZE);
1034 read_block (block, blk);
1035 name = blk + (offset % BLOCK_SIZE) + 2;
1036 ino = *(unsigned short *) (name - 2);
1039 printf(_("The directory '%s' contains a bad inode number "
1040 "for file '%.*s'."),
1041 current_name, namelen, name);
1042 if (ask (_(" Remove"), 1)) {
1043 *(unsigned short *) (name - 2) = 0;
1044 write_block (block, blk);
1048 if (name_depth < MAX_DEPTH)
1049 strncpy (name_list[name_depth], name, namelen);
1051 inode = get_inode2 (ino);
1054 if (!inode || strcmp (".", name)) {
1055 get_current_name ();
1056 printf (_("%s: bad directory: '.' isn't first\n"),
1058 errors_uncorrected = 1;
1062 if (offset == dirsize) {
1063 if (!inode || strcmp ("..", name)) {
1064 get_current_name ();
1065 printf (_("%s: bad directory: '..' isn't second\n"),
1067 errors_uncorrected = 1;
1076 printf ("%6d %07o %3d ", ino, inode->i_mode,
1078 get_current_name ();
1079 printf("%s", current_name);
1080 if (S_ISDIR (inode->i_mode))
1086 if (inode && S_ISDIR (inode->i_mode))
1087 recursive_check2 (ino);
1093 recursive_check(unsigned int ino) {
1094 struct minix_inode * dir;
1095 unsigned int offset;
1098 if (!S_ISDIR(dir->i_mode))
1099 die(_("internal error"));
1100 if (dir->i_size < 2 * dirsize) {
1102 printf(_("%s: bad directory: size < 32"),
1104 errors_uncorrected = 1;
1106 for (offset = 0 ; offset < dir->i_size ; offset += dirsize)
1107 check_file(dir,offset);
1111 recursive_check2 (unsigned int ino) {
1112 struct minix2_inode *dir;
1113 unsigned int offset;
1116 if (!S_ISDIR (dir->i_mode))
1117 die(_("internal error"));
1118 if (dir->i_size < 2 * dirsize) {
1119 get_current_name ();
1120 printf (_("%s: bad directory: size < 32"),
1122 errors_uncorrected = 1;
1124 for (offset = 0; offset < dir->i_size; offset += dirsize)
1125 check_file2 (dir, offset);
1132 if (BLOCK_SIZE*i != lseek(IN, BLOCK_SIZE*i, SEEK_SET))
1133 die(_("seek failed in bad_zone"));
1134 return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE));
1138 check_counts(void) {
1141 for (i=1 ; i <= INODES ; i++) {
1142 if (!inode_in_use(i) && Inode[i].i_mode && warn_mode) {
1143 printf(_("Inode %d mode not cleared."),i);
1144 if (ask(_("Clear"),1)) {
1145 Inode[i].i_mode = 0;
1149 if (!inode_count[i]) {
1150 if (!inode_in_use(i))
1152 printf(_("Inode %d not used, marked used in the bitmap."),i);
1153 if (ask(_("Clear"),1))
1157 if (!inode_in_use(i)) {
1158 printf(_("Inode %d used, marked unused in the bitmap."),
1160 if (ask(_("Set"),1))
1163 if (Inode[i].i_nlinks != inode_count[i]) {
1164 printf(_("Inode %d (mode = %07o), i_nlinks=%d, counted=%d."),
1165 i,Inode[i].i_mode,Inode[i].i_nlinks,inode_count[i]);
1166 if (ask(_("Set i_nlinks to count"),1)) {
1167 Inode[i].i_nlinks=inode_count[i];
1172 for (i=FIRSTZONE ; i < ZONES ; i++) {
1173 if (zone_in_use(i) == zone_count[i])
1175 if (!zone_count[i]) {
1178 printf(_("Zone %d: marked in use, no file uses it."),i);
1179 if (ask(_("Unmark"),1))
1184 printf(_("Zone %d: in use, counted=%d\n"),
1187 printf(_("Zone %d: not in use, counted=%d\n"),
1193 check_counts2 (void) {
1196 for (i = 1; i <= INODES; i++) {
1197 if (!inode_in_use (i) && Inode2[i].i_mode && warn_mode) {
1198 printf (_("Inode %d mode not cleared."), i);
1199 if (ask (_("Clear"), 1)) {
1200 Inode2[i].i_mode = 0;
1204 if (!inode_count[i]) {
1205 if (!inode_in_use (i))
1207 printf (_("Inode %d not used, marked used in the bitmap."), i);
1208 if (ask (_("Clear"), 1))
1212 if (!inode_in_use (i)) {
1213 printf (_("Inode %d used, marked unused in the bitmap."), i);
1214 if (ask (_("Set"), 1))
1217 if (Inode2[i].i_nlinks != inode_count[i]) {
1218 printf (_("Inode %d (mode = %07o), i_nlinks=%d, counted=%d."),
1219 i, Inode2[i].i_mode, Inode2[i].i_nlinks, inode_count[i]);
1220 if (ask (_("Set i_nlinks to count"), 1)) {
1221 Inode2[i].i_nlinks = inode_count[i];
1226 for (i = FIRSTZONE; i < ZONES; i++) {
1227 if (zone_in_use (i) == zone_count[i])
1229 if (!zone_count[i]) {
1232 printf (_("Zone %d: marked in use, no file uses it."),
1234 if (ask (_("Unmark"), 1))
1238 if (zone_in_use (i))
1239 printf (_("Zone %d: in use, counted=%d\n"),
1242 printf (_("Zone %d: not in use, counted=%d\n"),
1249 memset(inode_count,0,(INODES + 1) * sizeof(*inode_count));
1250 memset(zone_count,0,ZONES*sizeof(*zone_count));
1251 check_zones(ROOT_INO);
1252 recursive_check(ROOT_INO);
1258 memset (inode_count, 0, (INODES + 1) * sizeof (*inode_count));
1259 memset (zone_count, 0, ZONES * sizeof (*zone_count));
1260 check_zones2 (ROOT_INO);
1261 recursive_check2 (ROOT_INO);
1266 main(int argc, char ** argv) {
1272 program_name = (argc && *argv) ? argv[0] : "fsck.minix";
1273 if ((p = strrchr(program_name, '/')) != NULL)
1276 setlocale(LC_ALL, "");
1277 bindtextdomain(PACKAGE, LOCALEDIR);
1278 textdomain(PACKAGE);
1281 (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version"))) {
1282 printf(_("%s (%s)\n"), program_name, PACKAGE_STRING);
1286 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
1287 die(_("bad inode size"));
1288 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
1289 die(_("bad v2 inode size"));
1291 while (argc-- > 1) {
1293 if (argv[0][0] != '-') {
1297 device_name = argv[0];
1298 } else while (*++argv[0])
1299 switch (argv[0][0]) {
1300 case 'l': list=1; break;
1301 case 'a': automatic=1; repair=1; break;
1302 case 'r': automatic=0; repair=1; break;
1303 case 'v': verbose=1; break;
1304 case 's': show=1; break;
1305 case 'm': warn_mode=1; break;
1306 case 'f': force=1; break;
1312 check_mount(); /* trying to check a mounted filesystem? */
1313 if (repair && !automatic) {
1314 if (!isatty(0) || !isatty(1))
1315 die(_("need terminal for interactive repairs"));
1317 IN = open(device_name,repair?O_RDWR:O_RDONLY);
1319 die(_("unable to open '%s': %s"), device_name, strerror(errno));
1320 for (count=0 ; count<3 ; count++)
1325 * Determine whether or not we should continue with the checking.
1326 * This is based on the status of the filesystem valid and error
1327 * flags and whether or not the -f switch was specified on the
1330 if ( !(Super.s_state & MINIX_ERROR_FS) &&
1331 (Super.s_state & MINIX_VALID_FS) &&
1334 printf(_("%s is clean, no check.\n"), device_name);
1338 printf(_("Forcing filesystem check on %s.\n"), device_name);
1340 printf(_("Filesystem on %s is dirty, needs checking.\n"),\
1345 /* Restore the terminal state on fatal signals.
1346 * We don't do this for SIGALRM, SIGUSR1 or SIGUSR2.
1348 signal(SIGINT, fatalsig);
1349 signal(SIGQUIT, fatalsig);
1350 signal(SIGTERM, fatalsig);
1352 if (repair && !automatic) {
1353 tcgetattr(0,&termios);
1355 tmp.c_lflag &= ~(ICANON|ECHO);
1356 tcsetattr(0,TCSANOW,&tmp);
1370 for (i=1,free=0 ; i <= INODES ; i++)
1371 if (!inode_in_use(i))
1373 printf(_("\n%6ld inodes used (%ld%%)\n"),(INODES-free),
1374 100*(INODES-free)/INODES);
1375 for (i=FIRSTZONE,free=0 ; i < ZONES ; i++)
1376 if (!zone_in_use(i))
1378 printf(_("%6ld zones used (%ld%%)\n"),(ZONES-free),
1379 100*(ZONES-free)/ZONES);
1380 printf(_("\n%6d regular files\n"
1382 "%6d character device files\n"
1383 "%6d block device files\n"
1385 "%6d symbolic links\n"
1388 regular,directory,chardev,blockdev,
1389 links-2*directory+1,symlinks,total-2*directory+1);
1393 printf(_( "----------------------------\n"
1394 "FILE SYSTEM HAS BEEN CHANGED\n"
1395 "----------------------------\n"));
1396 for (count=0 ; count<3 ; count++)
1400 write_super_block();
1402 if (repair && !automatic)
1403 tcsetattr(0,TCSANOW,&termios);
1407 if (errors_uncorrected)