2 * fsck.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@misiek.eu.org>
66 * - added Native Language Support
69 * I've had no time to add comments - hopefully the function names
70 * are comments enough. As with all file system checkers, this assumes
71 * the file system is quiescent - don't use it on a mounted device
72 * unless you can be sure nobody is writing to it (and remember that the
73 * kernel can write to it when it searches for files).
75 * Usuage: fsck [-larvsm] device
76 * -l for a listing of all the filenames
77 * -a for automatic repairs (not implemented)
78 * -r for repairs (interactive) (not implemented)
79 * -v for verbose (tells how many files)
80 * -s for super-block info
81 * -m for minix-like "mode not cleared" warnings
82 * -f force filesystem check even if filesystem marked as valid
84 * The device may be a block device or a image of one, but this isn't
85 * enforced (but it's not much fun on a character device :-).
100 #include <linux/minix_fs.h>
101 #include "../version.h"
104 #ifdef MINIX2_SUPER_MAGIC2
105 #define HAVE_MINIX2 1
114 #define UPPER(size,n) ((size+((n)-1))/(n))
115 #define INODE_SIZE (sizeof(struct minix_inode))
117 #define INODE_SIZE2 (sizeof(struct minix2_inode))
118 #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
119 : MINIX_INODES_PER_BLOCK))
121 #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
123 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
125 #define BITS_PER_BLOCK (BLOCK_SIZE<<3)
127 static char * program_name = "fsck.minix";
128 static char * program_version = "1.2 - 11/11/96";
129 static char * device_name = NULL;
131 static int repair=0, automatic=0, verbose=0, list=0, show=0, warn_mode=0,
133 static int directory=0, regular=0, blockdev=0, chardev=0, links=0,
136 static int changed = 0; /* flags if the filesystem has been changed */
137 static int errors_uncorrected = 0; /* flag if some error was not corrected */
138 static int dirsize = 16;
139 static int namelen = 14;
140 static int version2 = 0;
141 static struct termios termios;
142 static int termios_set = 0;
146 static int name_depth = 0;
147 static char name_list[MAX_DEPTH][NAME_MAX+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)
156 #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
158 #define ZONES ((unsigned long)(Super.s_nzones))
160 #define IMAPS ((unsigned long)Super.s_imap_blocks)
161 #define ZMAPS ((unsigned long)Super.s_zmap_blocks)
162 #define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
163 #define ZONESIZE ((unsigned long)Super.s_log_zone_size)
164 #define MAXSIZE ((unsigned long)Super.s_max_size)
165 #define MAGIC (Super.s_magic)
166 #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
168 static char *inode_map;
169 static char *zone_map;
171 static unsigned char * inode_count = NULL;
172 static unsigned char * zone_count = NULL;
174 void recursive_check(unsigned int ino);
175 void recursive_check2(unsigned int ino);
179 #define inode_in_use(x) (bit(inode_map,(x)))
180 #define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
182 #define mark_inode(x) (setbit(inode_map,(x)),changed=1)
183 #define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
185 #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1)
186 #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1)
188 void leave(int) __attribute__ ((noreturn));
189 void leave(int status)
192 tcsetattr(0, TCSANOW, &termios);
198 _("Usage: %s [-larvsmf] /dev/name\n"),
203 void die(const char *str) {
204 fprintf(stderr, "%s: %s\n", program_name, str);
209 * This simply goes through the file-name data and prints out the
212 void print_current_name(void)
217 printf("/%.*s",namelen,name_list[i++]);
222 int ask(const char * string, int def)
228 errors_uncorrected = 1;
234 errors_uncorrected = 1;
237 printf(def?"%s (y/n)? ":"%s (n/y)? ",string);
240 if ((c=getchar())==EOF) {
242 errors_uncorrected = 1;
249 } else if (c == 'N') {
252 } else if (c == ' ' || c == '\n')
259 errors_uncorrected = 1;
265 * Make certain that we aren't checking a filesystem that is on a
266 * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
267 * 1994 Theodore Ts'o. Also licensed under GPL.
269 static void check_mount(void)
276 if ((f = setmntent (MOUNTED, "r")) == NULL)
278 while ((mnt = getmntent (f)) != NULL)
279 if (strcmp (device_name, mnt->mnt_fsname) == 0)
286 * If the root is mounted read-only, then /etc/mtab is
287 * probably not correct; so we won't issue a warning based on
290 fd = open(MOUNTED, O_RDWR);
291 if (fd < 0 && errno == EROFS)
296 printf (_("%s is mounted. "), device_name);
297 if (isatty(0) && isatty(1))
298 cont = ask(_("Do you really want to continue"), 0);
302 printf (_("check aborted.\n"));
309 * check_zone_nr checks to see that *nr is a valid zone nr. If it
310 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
311 * if an error was corrected, and returns the zone (0 for no zone
312 * or a bad zone-number).
314 int check_zone_nr(unsigned short * nr, int * corrected)
319 printf(_("Zone nr < FIRSTZONE in file `"));
320 else if (*nr >= ZONES)
321 printf(_("Zone nr >= ZONES in file `"));
324 print_current_name();
326 if (ask(_("Remove block"),1)) {
334 int check_zone_nr2 (unsigned int *nr, int *corrected)
339 printf (_("Zone nr < FIRSTZONE in file `"));
340 else if (*nr >= ZONES)
341 printf (_("Zone nr >= ZONES in file `"));
344 print_current_name ();
346 if (ask (_("Remove block"), 1)) {
355 * read-block reads block nr into the buffer at addr.
357 void read_block(unsigned int nr, char * addr)
360 memset(addr,0,BLOCK_SIZE);
363 if (BLOCK_SIZE*nr != lseek(IN, BLOCK_SIZE*nr, SEEK_SET)) {
364 printf(_("Read error: unable to seek to block in file '"));
365 print_current_name();
367 memset(addr,0,BLOCK_SIZE);
368 errors_uncorrected = 1;
369 } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) {
370 printf(_("Read error: bad block in file '"));
371 print_current_name();
373 memset(addr,0,BLOCK_SIZE);
374 errors_uncorrected = 1;
379 * write_block writes block nr to disk.
381 void write_block(unsigned int nr, char * addr)
385 if (nr < FIRSTZONE || nr >= ZONES) {
386 printf(_("Internal error: trying to write bad block\n"
387 "Write request ignored\n"));
388 errors_uncorrected = 1;
391 if (BLOCK_SIZE*nr != lseek(IN, BLOCK_SIZE*nr, SEEK_SET))
392 die(_("seek failed in write_block"));
393 if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) {
394 printf(_("Write error: bad block in file '"));
395 print_current_name();
397 errors_uncorrected = 1;
402 * map-block calculates the absolute block nr of a block in a file.
403 * It sets 'changed' if the inode has needed changing, and re-writes
404 * any indirect blocks with errors.
406 int map_block(struct minix_inode * inode, unsigned int blknr)
408 unsigned short ind[BLOCK_SIZE>>1];
409 unsigned short dind[BLOCK_SIZE>>1];
410 int blk_chg, block, result;
413 return check_zone_nr(inode->i_zone + blknr, &changed);
416 block = check_zone_nr(inode->i_zone + 7, &changed);
417 read_block(block, (char *) ind);
419 result = check_zone_nr(blknr + ind, &blk_chg);
421 write_block(block, (char *) ind);
425 block = check_zone_nr(inode->i_zone + 8, &changed);
426 read_block(block, (char *) dind);
428 result = check_zone_nr(dind + (blknr/512), &blk_chg);
430 write_block(block, (char *) dind);
432 read_block(block, (char *) ind);
434 result = check_zone_nr(ind + (blknr%512), &blk_chg);
436 write_block(block, (char *) ind);
441 int map_block2 (struct minix2_inode *inode, unsigned int blknr)
443 unsigned int ind[BLOCK_SIZE >> 2];
444 unsigned int dind[BLOCK_SIZE >> 2];
445 unsigned int tind[BLOCK_SIZE >> 2];
446 int blk_chg, block, result;
449 return check_zone_nr2 (inode->i_zone + blknr, &changed);
452 block = check_zone_nr2 (inode->i_zone + 7, &changed);
453 read_block (block, (char *) ind);
455 result = check_zone_nr2 (blknr + ind, &blk_chg);
457 write_block (block, (char *) ind);
461 if (blknr >= 256 * 256) {
462 block = check_zone_nr2 (inode->i_zone + 8, &changed);
463 read_block (block, (char *) dind);
465 result = check_zone_nr2 (dind + blknr / 256, &blk_chg);
467 write_block (block, (char *) dind);
469 read_block (block, (char *) ind);
471 result = check_zone_nr2 (ind + blknr % 256, &blk_chg);
473 write_block (block, (char *) ind);
477 block = check_zone_nr2 (inode->i_zone + 9, &changed);
478 read_block (block, (char *) tind);
480 result = check_zone_nr2 (tind + blknr / (256 * 256), &blk_chg);
482 write_block (block, (char *) tind);
484 read_block (block, (char *) dind);
486 result = check_zone_nr2 (dind + (blknr / 256) % 256, &blk_chg);
488 write_block (block, (char *) dind);
490 read_block (block, (char *) ind);
492 result = check_zone_nr2 (ind + blknr % 256, &blk_chg);
494 write_block (block, (char *) ind);
499 void write_super_block(void)
502 * Set the state of the filesystem based on whether or not there
503 * are uncorrected errors. The filesystem valid flag is
504 * unconditionally set if we get this far.
506 Super.s_state |= MINIX_VALID_FS;
507 if ( errors_uncorrected )
508 Super.s_state |= MINIX_ERROR_FS;
510 Super.s_state &= ~MINIX_ERROR_FS;
512 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
513 die(_("seek failed in write_super_block"));
514 if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE))
515 die(_("unable to write super-block"));
520 void write_tables(void)
524 if (IMAPS*BLOCK_SIZE != write(IN,inode_map,IMAPS*BLOCK_SIZE))
525 die(_("Unable to write inode map"));
526 if (ZMAPS*BLOCK_SIZE != write(IN,zone_map,ZMAPS*BLOCK_SIZE))
527 die(_("Unable to write zone map"));
528 if (INODE_BUFFER_SIZE != write(IN,inode_buffer,INODE_BUFFER_SIZE))
529 die(_("Unable to write inodes"));
532 void get_dirsize (void)
535 char blk[BLOCK_SIZE];
540 block = Inode2[ROOT_INO].i_zone[0];
543 block = Inode[ROOT_INO].i_zone[0];
544 read_block (block, blk);
545 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
546 if (strcmp (blk + size + 2, "..") == 0) {
555 void read_superblock(void)
557 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
558 die(_("seek failed"));
559 if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
560 die(_("unable to read super block"));
561 if (MAGIC == MINIX_SUPER_MAGIC) {
565 } else if (MAGIC == MINIX_SUPER_MAGIC2) {
570 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
574 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
580 die(_("bad magic number in super-block"));
581 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
582 die(_("Only 1k blocks/zones supported"));
583 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
584 die(_("bad s_imap_blocks field in super-block"));
585 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
586 die(_("bad s_zmap_blocks field in super-block"));
589 void read_tables(void)
591 inode_map = malloc(IMAPS * BLOCK_SIZE);
593 die(_("Unable to allocate buffer for inode map"));
594 zone_map = malloc(ZMAPS * BLOCK_SIZE);
596 die("Unable to allocate buffer for zone map");
597 memset(inode_map,0,sizeof(inode_map));
598 memset(zone_map,0,sizeof(zone_map));
599 inode_buffer = malloc(INODE_BUFFER_SIZE);
601 die(_("Unable to allocate buffer for inodes"));
602 inode_count = malloc(INODES + 1);
604 die(_("Unable to allocate buffer for inode count"));
605 zone_count = malloc(ZONES);
607 die(_("Unable to allocate buffer for zone count"));
608 if (IMAPS*BLOCK_SIZE != read(IN,inode_map,IMAPS*BLOCK_SIZE))
609 die(_("Unable to read inode map"));
610 if (ZMAPS*BLOCK_SIZE != read(IN,zone_map,ZMAPS*BLOCK_SIZE))
611 die(_("Unable to read zone map"));
612 if (INODE_BUFFER_SIZE != read(IN,inode_buffer,INODE_BUFFER_SIZE))
613 die(_("Unable to read inodes"));
614 if (NORM_FIRSTZONE != FIRSTZONE) {
615 printf(_("Warning: Firstzone != Norm_firstzone\n"));
616 errors_uncorrected = 1;
620 printf(_("%ld inodes\n"),INODES);
621 printf(_("%ld blocks\n"),ZONES);
622 printf(_("Firstdatazone=%ld (%ld)\n"),FIRSTZONE,NORM_FIRSTZONE);
623 printf(_("Zonesize=%d\n"),BLOCK_SIZE<<ZONESIZE);
624 printf(_("Maxsize=%ld\n"),MAXSIZE);
625 printf(_("Filesystem state=%d\n"), Super.s_state);
626 printf(_("namelen=%d\n\n"),namelen);
630 struct minix_inode * get_inode(unsigned int nr)
632 struct minix_inode * inode;
634 if (!nr || nr > INODES)
638 if (!inode_count[nr]) {
639 if (!inode_in_use(nr)) {
640 printf(_("Inode %d marked not used, but used for file '"),
642 print_current_name();
645 if (ask(_("Mark in use"),1))
648 errors_uncorrected = 1;
651 if (S_ISDIR(inode->i_mode))
653 else if (S_ISREG(inode->i_mode))
655 else if (S_ISCHR(inode->i_mode))
657 else if (S_ISBLK(inode->i_mode))
659 else if (S_ISLNK(inode->i_mode))
661 else if (S_ISSOCK(inode->i_mode))
663 else if (S_ISFIFO(inode->i_mode))
666 print_current_name();
667 printf(_(" has mode %05o\n"),inode->i_mode);
672 if (!++inode_count[nr]) {
673 printf(_("Warning: inode count too big.\n"));
675 errors_uncorrected = 1;
681 struct minix2_inode *
682 get_inode2 (unsigned int nr)
684 struct minix2_inode *inode;
686 if (!nr || nr > INODES)
690 if (!inode_count[nr]) {
691 if (!inode_in_use (nr)) {
692 printf (_("Inode %d marked not used, but used for file '"), nr);
693 print_current_name ();
696 if (ask (_("Mark in use"), 1))
699 errors_uncorrected = 1;
702 if (S_ISDIR (inode->i_mode))
704 else if (S_ISREG (inode->i_mode))
706 else if (S_ISCHR (inode->i_mode))
708 else if (S_ISBLK (inode->i_mode))
710 else if (S_ISLNK (inode->i_mode))
712 else if (S_ISSOCK (inode->i_mode));
713 else if (S_ISFIFO (inode->i_mode));
715 print_current_name ();
716 printf (_(" has mode %05o\n"), inode->i_mode);
720 if (!++inode_count[nr]) {
721 printf (_("Warning: inode count too big.\n"));
723 errors_uncorrected = 1;
729 void check_root(void)
731 struct minix_inode * inode = Inode + ROOT_INO;
733 if (!inode || !S_ISDIR(inode->i_mode))
734 die(_("root inode isn't a directory"));
738 void check_root2 (void)
740 struct minix2_inode *inode = Inode2 + ROOT_INO;
742 if (!inode || !S_ISDIR (inode->i_mode))
743 die ("root inode isn't a directory");
747 static int add_zone(unsigned short * znr, int * corrected)
753 block = check_zone_nr(znr, corrected);
756 if (zone_count[block]) {
757 printf(_("Block has been used before. Now in file `"));
758 print_current_name();
760 if (ask(_("Clear"),1)) {
768 if (!zone_in_use(block)) {
769 printf(_("Block %d in file `"),block);
770 print_current_name();
771 printf(_("' is marked not in use."));
772 if (ask(_("Correct"),1))
775 if (!++zone_count[block])
781 static int add_zone2 (unsigned int *znr, int *corrected)
787 block = check_zone_nr2 (znr, corrected);
790 if (zone_count[block]) {
791 printf (_("Block has been used before. Now in file `"));
792 print_current_name ();
794 if (ask (_("Clear"), 1)) {
802 if (!zone_in_use (block)) {
803 printf (_("Block %d in file `"), block);
804 print_current_name ();
805 printf (_("' is marked not in use."));
806 if (ask (_("Correct"), 1))
809 if (!++zone_count[block])
815 static void add_zone_ind(unsigned short * znr, int * corrected)
817 static char blk[BLOCK_SIZE];
821 block = add_zone(znr, corrected);
824 read_block(block, blk);
825 for (i=0 ; i < (BLOCK_SIZE>>1) ; i++)
826 add_zone(i + (unsigned short *) blk, &chg_blk);
828 write_block(block, blk);
833 add_zone_ind2 (unsigned int *znr, int *corrected)
835 static char blk[BLOCK_SIZE];
839 block = add_zone2 (znr, corrected);
842 read_block (block, blk);
843 for (i = 0; i < BLOCK_SIZE >> 2; i++)
844 add_zone2 (i + (unsigned int *) blk, &chg_blk);
846 write_block (block, blk);
850 static void add_zone_dind(unsigned short * znr, int * corrected)
852 static char blk[BLOCK_SIZE];
856 block = add_zone(znr, corrected);
859 read_block(block, blk);
860 for (i=0 ; i < (BLOCK_SIZE>>1) ; i++)
861 add_zone_ind(i + (unsigned short *) blk, &blk_chg);
863 write_block(block, blk);
868 add_zone_dind2 (unsigned int *znr, int *corrected)
870 static char blk[BLOCK_SIZE];
874 block = add_zone2 (znr, corrected);
877 read_block (block, blk);
878 for (i = 0; i < BLOCK_SIZE >> 2; i++)
879 add_zone_ind2 (i + (unsigned int *) blk, &blk_chg);
881 write_block (block, blk);
885 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 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))
914 for (i=0 ; i<7 ; i++)
915 add_zone(i + inode->i_zone, &changed);
916 add_zone_ind(7 + inode->i_zone, &changed);
917 add_zone_dind(8 + inode->i_zone, &changed);
922 check_zones2 (unsigned int i)
924 struct minix2_inode *inode;
926 if (!i || i > INODES)
928 if (inode_count[i] > 1) /* have we counted this file already? */
931 if (!S_ISDIR (inode->i_mode) && !S_ISREG (inode->i_mode)
932 && !S_ISLNK (inode->i_mode))
934 for (i = 0; i < 7; i++)
935 add_zone2 (i + inode->i_zone, &changed);
936 add_zone_ind2 (7 + inode->i_zone, &changed);
937 add_zone_dind2 (8 + inode->i_zone, &changed);
938 add_zone_tind2 (9 + inode->i_zone, &changed);
942 void check_file(struct minix_inode * dir, unsigned int offset)
944 static char blk[BLOCK_SIZE];
945 struct minix_inode * inode;
950 block = map_block(dir,offset/BLOCK_SIZE);
951 read_block(block, blk);
952 name = blk + (offset % BLOCK_SIZE) + 2;
953 ino = * (unsigned short *) (name-2);
955 print_current_name();
956 printf(_(" contains a bad inode number for file '"));
957 printf("%.*s'.",namelen,name);
958 if (ask(_(" Remove"),1)) {
959 *(unsigned short *)(name-2) = 0;
960 write_block(block, blk);
964 if (name_depth < MAX_DEPTH)
965 strncpy (name_list[name_depth], name, namelen);
967 inode = get_inode(ino);
970 if (!inode || strcmp(".",name)) {
971 print_current_name();
972 printf(_(": bad directory: '.' isn't first\n"));
973 errors_uncorrected = 1;
976 if (offset == dirsize) {
977 if (!inode || strcmp("..",name)) {
978 print_current_name();
979 printf(_(": bad directory: '..' isn't second\n"));
980 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);
1006 check_file2 (struct minix2_inode *dir, unsigned int offset)
1008 static char blk[BLOCK_SIZE];
1009 struct minix2_inode *inode;
1014 block = map_block2 (dir, offset / BLOCK_SIZE);
1015 read_block (block, blk);
1016 name = blk + (offset % BLOCK_SIZE) + 2;
1017 ino = *(unsigned short *) (name - 2);
1019 print_current_name ();
1020 printf (_(" contains a bad inode number for file '"));
1021 printf ("%.*s'.", namelen, name);
1022 if (ask (_(" Remove"), 1)) {
1023 *(unsigned short *) (name - 2) = 0;
1024 write_block (block, blk);
1028 if (name_depth < MAX_DEPTH)
1029 strncpy (name_list[name_depth], name, namelen);
1031 inode = get_inode2 (ino);
1034 if (!inode || strcmp (".", name)) {
1035 print_current_name ();
1036 printf (_(": bad directory: '.' isn't first\n"));
1037 errors_uncorrected = 1;
1041 if (offset == dirsize) {
1042 if (!inode || strcmp ("..", name)) {
1043 print_current_name ();
1044 printf (_(": bad directory: '..' isn't second\n"));
1045 errors_uncorrected = 1;
1054 printf ("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1055 print_current_name ();
1056 if (S_ISDIR (inode->i_mode))
1062 if (inode && S_ISDIR (inode->i_mode))
1063 recursive_check2 (ino);
1069 void recursive_check(unsigned int ino)
1071 struct minix_inode * dir;
1072 unsigned int offset;
1075 if (!S_ISDIR(dir->i_mode))
1076 die(_("internal error"));
1077 if (dir->i_size < 2 * dirsize) {
1078 print_current_name();
1079 printf(_(": bad directory: size<32"));
1080 errors_uncorrected = 1;
1082 for (offset = 0 ; offset < dir->i_size ; offset += dirsize)
1083 check_file(dir,offset);
1088 recursive_check2 (unsigned int ino)
1090 struct minix2_inode *dir;
1091 unsigned int offset;
1094 if (!S_ISDIR (dir->i_mode))
1095 die ("internal error");
1096 if (dir->i_size < 2 * dirsize) {
1097 print_current_name ();
1098 printf (_(": bad directory: size < 32"));
1099 errors_uncorrected = 1;
1101 for (offset = 0; offset < dir->i_size; offset += dirsize)
1102 check_file2 (dir, offset);
1110 if (BLOCK_SIZE*i != lseek(IN, BLOCK_SIZE*i, SEEK_SET))
1111 die(_("seek failed in bad_zone"));
1112 return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE));
1115 void check_counts(void)
1119 for (i=1 ; i <= INODES ; i++) {
1120 if (!inode_in_use(i) && Inode[i].i_mode && warn_mode) {
1121 printf(_("Inode %d mode not cleared."),i);
1122 if (ask(_("Clear"),1)) {
1123 Inode[i].i_mode = 0;
1127 if (!inode_count[i]) {
1128 if (!inode_in_use(i))
1130 printf(_("Inode %d not used, marked used in the bitmap."),i);
1131 if (ask(_("Clear"),1))
1135 if (!inode_in_use(i)) {
1136 printf(_("Inode %d used, marked unused in the bitmap."),
1141 if (Inode[i].i_nlinks != inode_count[i]) {
1142 printf(_("Inode %d (mode = %07o), i_nlinks=%d, counted=%d."),
1143 i,Inode[i].i_mode,Inode[i].i_nlinks,inode_count[i]);
1144 if (ask(_("Set i_nlinks to count"),1)) {
1145 Inode[i].i_nlinks=inode_count[i];
1150 for (i=FIRSTZONE ; i < ZONES ; i++) {
1151 if (zone_in_use(i) == zone_count[i])
1153 if (!zone_count[i]) {
1156 printf(_("Zone %d: marked in use, no file uses it."),i);
1157 if (ask(_("Unmark"),1))
1161 printf(_("Zone %d: %sin use, counted=%d\n"),
1162 i,zone_in_use(i)?"":_("not "),zone_count[i]);
1168 check_counts2 (void)
1172 for (i = 1; i <= INODES; i++) {
1173 if (!inode_in_use (i) && Inode2[i].i_mode && warn_mode) {
1174 printf (_("Inode %d mode not cleared."), i);
1175 if (ask (_("Clear"), 1)) {
1176 Inode2[i].i_mode = 0;
1180 if (!inode_count[i]) {
1181 if (!inode_in_use (i))
1183 printf (_("Inode %d not used, marked used in the bitmap."), i);
1184 if (ask (_("Clear"), 1))
1188 if (!inode_in_use (i)) {
1189 printf (_("Inode %d used, marked unused in the bitmap."), i);
1190 if (ask (_("Set"), 1))
1193 if (Inode2[i].i_nlinks != inode_count[i]) {
1194 printf (_("Inode %d (mode = %07o), i_nlinks=%d, counted=%d."),
1195 i, Inode2[i].i_mode, Inode2[i].i_nlinks, inode_count[i]);
1196 if (ask (_("Set i_nlinks to count"), 1)) {
1197 Inode2[i].i_nlinks = inode_count[i];
1202 for (i = FIRSTZONE; i < ZONES; i++) {
1203 if (zone_in_use (i) == zone_count[i])
1205 if (!zone_count[i]) {
1208 printf (_("Zone %d: marked in use, no file uses it."), i);
1209 if (ask (_("Unmark"), 1))
1213 printf (_("Zone %d: %sin use, counted=%d\n"),
1214 i, zone_in_use (i) ? "" : _("not "), zone_count[i]);
1221 memset(inode_count,0,(INODES + 1) * sizeof(*inode_count));
1222 memset(zone_count,0,ZONES*sizeof(*zone_count));
1223 check_zones(ROOT_INO);
1224 recursive_check(ROOT_INO);
1232 memset (inode_count, 0, (INODES + 1) * sizeof (*inode_count));
1233 memset (zone_count, 0, ZONES * sizeof (*zone_count));
1234 check_zones2 (ROOT_INO);
1235 recursive_check2 (ROOT_INO);
1241 fsck_minix_main(int argc, char ** argv)
1247 setlocale(LC_ALL, "");
1248 bindtextdomain(PACKAGE, LOCALEDIR);
1249 textdomain(PACKAGE);
1253 program_name = *argv;
1254 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
1255 die(_("bad inode size"));
1257 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
1258 die(_("bad v2 inode size"));
1260 while (argc-- > 1) {
1262 if (argv[0][0] != '-') {
1266 device_name = argv[0];
1267 } else while (*++argv[0])
1268 switch (argv[0][0]) {
1269 case 'l': list=1; break;
1270 case 'a': automatic=1; repair=1; break;
1271 case 'r': automatic=0; repair=1; break;
1272 case 'v': verbose=1; break;
1273 case 's': show=1; break;
1274 case 'm': warn_mode=1; break;
1275 case 'f': force=1; break;
1281 check_mount(); /* trying to check a mounted filesystem? */
1282 if (repair && !automatic) {
1283 if (!isatty(0) || !isatty(1))
1284 die(_("need terminal for interactive repairs"));
1286 IN = open(device_name,repair?O_RDWR:O_RDONLY);
1288 die(_("unable to open '%s'"));
1289 for (count=0 ; count<3 ; count++)
1294 * Determine whether or not we should continue with the checking.
1295 * This is based on the status of the filesystem valid and error
1296 * flags and whether or not the -f switch was specified on the
1299 printf("%s, %s / %s\n", program_name, program_version,
1300 util_linux_version);
1301 if ( !(Super.s_state & MINIX_ERROR_FS) &&
1302 (Super.s_state & MINIX_VALID_FS) &&
1305 printf(_("%s is clean, no check.\n"), device_name);
1309 printf(_("Forcing filesystem check on %s.\n"), device_name);
1311 printf(_("Filesystem on %s is dirty, needs checking.\n"),\
1316 if (repair && !automatic) {
1317 tcgetattr(0,&termios);
1319 tmp.c_lflag &= ~(ICANON|ECHO);
1320 tcsetattr(0,TCSANOW,&tmp);
1337 for (i=1,free=0 ; i <= INODES ; i++)
1338 if (!inode_in_use(i))
1340 printf(_("\n%6ld inodes used (%ld%%)\n"),(INODES-free),
1341 100*(INODES-free)/INODES);
1342 for (i=FIRSTZONE,free=0 ; i < ZONES ; i++)
1343 if (!zone_in_use(i))
1345 printf(_("%6ld zones used (%ld%%)\n"),(ZONES-free),
1346 100*(ZONES-free)/ZONES);
1347 printf(_("\n%6d regular files\n"
1349 "%6d character device files\n"
1350 "%6d block device files\n"
1352 "%6d symbolic links\n"
1355 regular,directory,chardev,blockdev,
1356 links-2*directory+1,symlinks,total-2*directory+1);
1360 printf(_( "----------------------------\n"
1361 "FILE SYSTEM HAS BEEN CHANGED\n"
1362 "----------------------------\n"));
1363 for (count=0 ; count<3 ; count++)
1367 write_super_block();
1369 if (repair && !automatic)
1370 tcsetattr(0,TCSANOW,&termios);
1374 if (errors_uncorrected)