1 /* vi: set sw=4 ts=4: */
3 * fsck.c - a file system consistency checker for Linux.
5 * (C) 1991, 1992 Linus Torvalds.
7 * Licensed under GPLv2, see file LICENSE in this tarball for details.
11 * 09.11.91 - made the first rudimentary functions
13 * 10.11.91 - updated, does checking, no repairs yet.
14 * Sent out to the mailing-list for testing.
16 * 14.11.91 - Testing seems to have gone well. Added some
17 * correction-code, and changed some functions.
19 * 15.11.91 - More correction code. Hopefully it notices most
20 * cases now, and tries to do something about them.
22 * 16.11.91 - More corrections (thanks to Mika Jalava). Most
23 * things seem to work now. Yeah, sure.
26 * 19.04.92 - Had to start over again from this old version, as a
27 * kernel bug ate my enhanced fsck in february.
29 * 28.02.93 - added support for different directory entry sizes..
31 * Sat Mar 6 18:59:42 1993, faith@cs.unc.edu: Output namelen with
32 * super-block information
34 * Sat Oct 9 11:17:11 1993, faith@cs.unc.edu: make exit status conform
35 * to that required by fsutil
37 * Mon Jan 3 11:06:52 1994 - Dr. Wettstein (greg%wind.uucp@plains.nodak.edu)
38 * Added support for file system valid flag. Also
39 * added program_version variable and output of
40 * program name and version number when program
43 * 30.10.94 - added support for v2 filesystem
44 * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de)
46 * 10.12.94 - added test to prevent checking of mounted fs adapted
47 * from Theodore Ts'o's (tytso@athena.mit.edu) e2fsck
48 * program. (Daniel Quinlan, quinlan@yggdrasil.com)
50 * 01.07.96 - Fixed the v2 fs stuff to use the right #defines and such
51 * for modern libcs (janl@math.uio.no, Nicolai Langfeldt)
53 * 02.07.96 - Added C bit fiddling routines from rmk@ecs.soton.ac.uk
54 * (Russell King). He made them for ARM. It would seem
55 * that the ARM is powerful enough to do this in C whereas
56 * i386 and m64k must use assembly to get it fast >:-)
57 * This should make minix fsck system-independent.
58 * (janl@math.uio.no, Nicolai Langfeldt)
60 * 04.11.96 - Added minor fixes from Andreas Schwab to avoid compiler
61 * warnings. Added mc68k bitops from
62 * Joerg Dorchain <dorchain@mpi-sb.mpg.de>.
64 * 06.11.96 - Added v2 code submitted by Joerg Dorchain, but written by
67 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
68 * - added Native Language Support
71 * I've had no time to add comments - hopefully the function names
72 * are comments enough. As with all file system checkers, this assumes
73 * the file system is quiescent - don't use it on a mounted device
74 * unless you can be sure nobody is writing to it (and remember that the
75 * kernel can write to it when it searches for files).
77 * Usage: fsck [-larvsm] device
78 * -l for a listing of all the filenames
79 * -a for automatic repairs (not implemented)
80 * -r for repairs (interactive) (not implemented)
81 * -v for verbose (tells how many files)
82 * -s for super-block info
83 * -m for minix-like "mode not cleared" warnings
84 * -f force filesystem check even if filesystem marked as valid
86 * The device may be a block device or a image of one, but this isn't
87 * enforced (but it's not much fun on a character device :-).
94 * This is the original minix inode layout on disk.
95 * Note the 8-bit gid and atime and ctime.
108 * The new minix inode has all the time entries, as well as
109 * long block numbers and a third indirect block (7+1+1+1
110 * instead of 7+1+1). Also, some previously 8-bit values are
111 * now 16-bit. The inode is now 64 bytes instead of 32.
113 struct minix2_inode {
127 MINIX_LINK_MAX = 250,
128 MINIX2_LINK_MAX = 65530,
130 MINIX_I_MAP_SLOTS = 8,
131 MINIX_Z_MAP_SLOTS = 64,
132 MINIX_SUPER_MAGIC = 0x137F, /* original minix fs */
133 MINIX_SUPER_MAGIC2 = 0x138F, /* minix fs, 30 char names */
134 MINIX2_SUPER_MAGIC = 0x2468, /* minix V2 fs */
135 MINIX2_SUPER_MAGIC2 = 0x2478, /* minix V2 fs, 30 char names */
136 MINIX_VALID_FS = 0x0001, /* Clean fs. */
137 MINIX_ERROR_FS = 0x0002, /* fs has errors. */
139 MINIX_INODES_PER_BLOCK = ((BLOCK_SIZE)/(sizeof (struct minix_inode))),
140 MINIX2_INODES_PER_BLOCK = ((BLOCK_SIZE)/(sizeof (struct minix2_inode))),
142 MINIX_V1 = 0x0001, /* original minix fs */
143 MINIX_V2 = 0x0002 /* minix V2 fs */
146 #define INODE_VERSION(inode) inode->i_sb->u.minix_sb.s_version
149 * minix super-block data on disk
151 struct minix_super_block {
154 uint16_t s_imap_blocks;
155 uint16_t s_zmap_blocks;
156 uint16_t s_firstdatazone;
157 uint16_t s_log_zone_size;
164 struct minix_dir_entry {
170 #define NAME_MAX 255 /* # chars in a file name */
172 #define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
175 #define BLKGETSIZE _IO(0x12,96) /* return device size */
182 enum { ROOT_INO = 1 };
184 #define UPPER(size,n) ((size+((n)-1))/(n))
185 #define INODE_SIZE (sizeof(struct minix_inode))
186 #ifdef CONFIG_FEATURE_MINIX2
187 #define INODE_SIZE2 (sizeof(struct minix2_inode))
188 #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \
189 : MINIX_INODES_PER_BLOCK))
191 #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
193 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
195 #define BITS_PER_BLOCK (BLOCK_SIZE<<3)
197 static char *program_version = "1.2 - 11/11/96";
198 static char *device_name;
200 static int repair, automatic, verbose, list, show, warn_mode, force;
201 static int directory, regular, blockdev, chardev, links, symlinks, total;
203 static int changed; /* flags if the filesystem has been changed */
204 static int errors_uncorrected; /* flag if some error was not corrected */
205 static int dirsize = 16;
206 static int namelen = 14;
207 static struct termios termios;
208 static int termios_set;
210 static char *inode_buffer;
211 #define Inode (((struct minix_inode *) inode_buffer)-1)
212 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
213 static char super_block_buffer[BLOCK_SIZE];
215 #define Super (*(struct minix_super_block *)super_block_buffer)
216 #define INODES ((unsigned long)Super.s_ninodes)
217 #ifdef CONFIG_FEATURE_MINIX2
219 #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
221 #define ZONES ((unsigned long)(Super.s_nzones))
223 #define IMAPS ((unsigned long)Super.s_imap_blocks)
224 #define ZMAPS ((unsigned long)Super.s_zmap_blocks)
225 #define FIRSTZONE ((unsigned long)Super.s_firstdatazone)
226 #define ZONESIZE ((unsigned long)Super.s_log_zone_size)
227 #define MAXSIZE ((unsigned long)Super.s_max_size)
228 #define MAGIC (Super.s_magic)
229 #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS)
231 static char *inode_map;
232 static char *zone_map;
234 static unsigned char *inode_count;
235 static unsigned char *zone_count;
237 static void recursive_check(unsigned int ino);
238 #ifdef CONFIG_FEATURE_MINIX2
239 static void recursive_check2(unsigned int ino);
242 static int bit(char *a, unsigned int i)
244 return (a[i >> 3] & (1<<(i & 7))) != 0;
246 #define inode_in_use(x) (bit(inode_map,(x)))
247 #define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
249 #define mark_inode(x) (setbit(inode_map,(x)),changed=1)
250 #define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
252 #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1)
253 #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1)
255 static void leave(int) ATTRIBUTE_NORETURN;
256 static void leave(int status)
259 tcsetattr(0, TCSANOW, &termios);
263 static void die(const char *str)
265 bb_error_msg("%s", str);
270 enum { MAX_DEPTH = 32 };
271 static int name_depth;
272 static char *current_name;
273 static char *name_component[MAX_DEPTH+1];
275 /* Wed Feb 9 15:17:06 MST 2000 */
276 /* dynamically allocate name_list (instead of making it static) */
277 static void alloc_current_name(void)
279 current_name = xmalloc(MAX_DEPTH * (BUFSIZ + 1));
280 current_name[0] = '/';
281 current_name[1] = '\0';
282 name_component[0] = ¤t_name[0];
285 #ifdef CONFIG_FEATURE_CLEAN_UP
286 /* execute this atexit() to deallocate name_list[] */
287 /* piptigger was here */
288 static void free_current_name(void)
294 static void push_filename(const char *name)
298 // [0] [1] [2] <-name_component[i]
299 if (name_depth < MAX_DEPTH) {
301 char *p = name_component[name_depth];
303 len = sprintf(p, "%.*s", namelen, name);
304 name_component[name_depth + 1] = p + len;
309 static void pop_filename(void) {
311 if (name_depth < MAX_DEPTH) {
312 *name_component[name_depth] = '\0';
314 current_name[0] = '/';
315 current_name[1] = '\0';
320 static int ask(const char *string, int def)
326 errors_uncorrected = 1;
332 errors_uncorrected = 1;
335 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
338 if ((c = getchar()) == EOF) {
340 errors_uncorrected = 1;
347 } else if (c == 'N') {
350 } else if (c == ' ' || c == '\n')
357 errors_uncorrected = 1;
363 * Make certain that we aren't checking a filesystem that is on a
364 * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
365 * 1994 Theodore Ts'o. Also licensed under GPL.
367 static void check_mount(void)
374 if ((f = setmntent(MOUNTED, "r")) == NULL)
376 while ((mnt = getmntent(f)) != NULL)
377 if (strcmp(device_name, mnt->mnt_fsname) == 0)
384 * If the root is mounted read-only, then /etc/mtab is
385 * probably not correct; so we won't issue a warning based on
388 fd = open(MOUNTED, O_RDWR);
389 if (fd < 0 && errno == EROFS)
394 printf("%s is mounted. ", device_name);
396 if (isatty(0) && isatty(1))
397 cont = ask("Do you really want to continue", 0);
399 printf("Check aborted\n");
406 * check_zone_nr checks to see that *nr is a valid zone nr. If it
407 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
408 * if an error was corrected, and returns the zone (0 for no zone
409 * or a bad zone-number).
411 static int check_zone_nr2(uint32_t *nr, int *corrected)
418 else if (*nr >= ZONES)
422 printf("Zone nr %s in file '%s'. ", msg, current_name);
423 if (ask("Remove block", 1)) {
430 static int check_zone_nr(uint16_t *nr, int *corrected)
433 int r = check_zone_nr2(&nr32, corrected);
434 *nr = (uint16_t)nr32;
439 * read-block reads block nr into the buffer at addr.
441 static void read_block(unsigned int nr, char *addr)
444 memset(addr, 0, BLOCK_SIZE);
447 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) {
448 printf("%s: unable to seek to block in file '%s'\n",
449 bb_msg_read_error, current_name);
450 errors_uncorrected = 1;
451 memset(addr, 0, BLOCK_SIZE);
452 } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) {
453 printf("%s: bad block in file '%s'\n",
454 bb_msg_read_error, current_name);
455 errors_uncorrected = 1;
456 memset(addr, 0, BLOCK_SIZE);
461 * write_block writes block nr to disk.
463 static void write_block(unsigned int nr, char *addr)
467 if (nr < FIRSTZONE || nr >= ZONES) {
468 printf("Internal error: trying to write bad block\n"
469 "Write request ignored\n");
470 errors_uncorrected = 1;
473 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET))
474 die("Seek failed in write_block");
475 if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) {
476 printf("%s: bad block in file '%s'\n",
477 bb_msg_write_error, current_name);
478 errors_uncorrected = 1;
483 * map_block calculates the absolute block nr of a block in a file.
484 * It sets 'changed' if the inode has needed changing, and re-writes
485 * any indirect blocks with errors.
487 static int map_block(struct minix_inode *inode, unsigned int blknr)
489 uint16_t ind[BLOCK_SIZE >> 1];
490 uint16_t dind[BLOCK_SIZE >> 1];
491 int blk_chg, block, result;
494 return check_zone_nr(inode->i_zone + blknr, &changed);
497 block = check_zone_nr(inode->i_zone + 7, &changed);
498 read_block(block, (char *) ind);
500 result = check_zone_nr(blknr + ind, &blk_chg);
502 write_block(block, (char *) ind);
506 block = check_zone_nr(inode->i_zone + 8, &changed);
507 read_block(block, (char *) dind);
509 result = check_zone_nr(dind + (blknr / 512), &blk_chg);
511 write_block(block, (char *) dind);
513 read_block(block, (char *) ind);
515 result = check_zone_nr(ind + (blknr % 512), &blk_chg);
517 write_block(block, (char *) ind);
521 #ifdef CONFIG_FEATURE_MINIX2
522 static int map_block2(struct minix2_inode *inode, unsigned int blknr)
524 uint32_t ind[BLOCK_SIZE >> 2];
525 uint32_t dind[BLOCK_SIZE >> 2];
526 uint32_t tind[BLOCK_SIZE >> 2];
527 int blk_chg, block, result;
530 return check_zone_nr2(inode->i_zone + blknr, &changed);
533 block = check_zone_nr2(inode->i_zone + 7, &changed);
534 read_block(block, (char *) ind);
536 result = check_zone_nr2(blknr + ind, &blk_chg);
538 write_block(block, (char *) ind);
542 if (blknr >= 256 * 256) {
543 block = check_zone_nr2(inode->i_zone + 8, &changed);
544 read_block(block, (char *) dind);
546 result = check_zone_nr2(dind + blknr / 256, &blk_chg);
548 write_block(block, (char *) dind);
550 read_block(block, (char *) ind);
552 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
554 write_block(block, (char *) ind);
558 block = check_zone_nr2(inode->i_zone + 9, &changed);
559 read_block(block, (char *) tind);
561 result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
563 write_block(block, (char *) tind);
565 read_block(block, (char *) dind);
567 result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
569 write_block(block, (char *) dind);
571 read_block(block, (char *) ind);
573 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
575 write_block(block, (char *) ind);
580 static void write_super_block(void)
583 * Set the state of the filesystem based on whether or not there
584 * are uncorrected errors. The filesystem valid flag is
585 * unconditionally set if we get this far.
587 Super.s_state |= MINIX_VALID_FS;
588 if (errors_uncorrected)
589 Super.s_state |= MINIX_ERROR_FS;
591 Super.s_state &= ~MINIX_ERROR_FS;
593 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
594 die("Seek failed in write_super_block");
595 if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE))
596 die("Unable to write super-block");
599 static void write_tables(void)
603 if (IMAPS * BLOCK_SIZE != write(IN, inode_map, IMAPS * BLOCK_SIZE))
604 die("Unable to write inode map");
605 if (ZMAPS * BLOCK_SIZE != write(IN, zone_map, ZMAPS * BLOCK_SIZE))
606 die("Unable to write zone map");
607 if (INODE_BUFFER_SIZE != write(IN, inode_buffer, INODE_BUFFER_SIZE))
608 die("Unable to write inodes");
611 static void get_dirsize(void)
614 char blk[BLOCK_SIZE];
617 #ifdef CONFIG_FEATURE_MINIX2
619 block = Inode2[ROOT_INO].i_zone[0];
622 block = Inode[ROOT_INO].i_zone[0];
623 read_block(block, blk);
624 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
625 if (strcmp(blk + size + 2, "..") == 0) {
634 static void read_superblock(void)
636 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
638 if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
639 die("Unable to read super block");
640 /* already initialized to:
645 if (MAGIC == MINIX_SUPER_MAGIC) {
646 } else if (MAGIC == MINIX_SUPER_MAGIC2) {
649 #ifdef CONFIG_FEATURE_MINIX2
650 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
652 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
658 die("Bad magic number in super-block");
659 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
660 die("Only 1k blocks/zones supported");
661 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
662 die("Bad s_imap_blocks field in super-block");
663 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
664 die("Bad s_zmap_blocks field in super-block");
667 static void read_tables(void)
669 inode_map = xzalloc(IMAPS * BLOCK_SIZE);
670 zone_map = xzalloc(ZMAPS * BLOCK_SIZE);
671 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
672 inode_count = xmalloc(INODES + 1);
673 zone_count = xmalloc(ZONES);
674 if (IMAPS * BLOCK_SIZE != read(IN, inode_map, IMAPS * BLOCK_SIZE))
675 die("Unable to read inode map");
676 if (ZMAPS * BLOCK_SIZE != read(IN, zone_map, ZMAPS * BLOCK_SIZE))
677 die("Unable to read zone map");
678 if (INODE_BUFFER_SIZE != read(IN, inode_buffer, INODE_BUFFER_SIZE))
679 die("Unable to read inodes");
680 if (NORM_FIRSTZONE != FIRSTZONE) {
681 printf("Warning: Firstzone!=Norm_firstzone\n");
682 errors_uncorrected = 1;
686 printf("%ld inodes\n"
688 "Firstdatazone=%ld (%ld)\n"
691 "Filesystem state=%d\n"
695 FIRSTZONE, NORM_FIRSTZONE,
696 BLOCK_SIZE << ZONESIZE,
703 static struct minix_inode *get_inode(unsigned int nr)
705 struct minix_inode *inode;
707 if (!nr || nr > INODES)
711 if (!inode_count[nr]) {
712 if (!inode_in_use(nr)) {
713 printf("Inode %d is marked as 'unused', but it is used "
714 "for file '%s'\n", nr, current_name);
716 if (ask("Mark as 'in use'", 1))
719 errors_uncorrected = 1;
722 if (S_ISDIR(inode->i_mode))
724 else if (S_ISREG(inode->i_mode))
726 else if (S_ISCHR(inode->i_mode))
728 else if (S_ISBLK(inode->i_mode))
730 else if (S_ISLNK(inode->i_mode))
732 else if (S_ISSOCK(inode->i_mode));
733 else if (S_ISFIFO(inode->i_mode));
735 printf("%s has mode %05o\n", current_name, inode->i_mode);
740 if (!++inode_count[nr]) {
741 printf("Warning: inode count too big\n");
743 errors_uncorrected = 1;
748 #ifdef CONFIG_FEATURE_MINIX2
749 static struct minix2_inode *get_inode2(unsigned int nr)
751 struct minix2_inode *inode;
753 if (!nr || nr > INODES)
757 if (!inode_count[nr]) {
758 if (!inode_in_use(nr)) {
759 printf("Inode %d is marked as 'unused', but it is used "
760 "for file '%s'\n", nr, current_name);
762 if (ask("Mark as 'in use'", 1))
765 errors_uncorrected = 1;
768 if (S_ISDIR(inode->i_mode))
770 else if (S_ISREG(inode->i_mode))
772 else if (S_ISCHR(inode->i_mode))
774 else if (S_ISBLK(inode->i_mode))
776 else if (S_ISLNK(inode->i_mode))
778 else if (S_ISSOCK(inode->i_mode));
779 else if (S_ISFIFO(inode->i_mode));
781 printf("%s has mode %05o\n", current_name, inode->i_mode);
785 if (!++inode_count[nr]) {
786 printf("Warning: inode count too big\n");
788 errors_uncorrected = 1;
794 static void check_root(void)
796 struct minix_inode *inode = Inode + ROOT_INO;
798 if (!inode || !S_ISDIR(inode->i_mode))
799 die("Root inode isn't a directory");
802 #ifdef CONFIG_FEATURE_MINIX2
803 static void check_root2(void)
805 struct minix2_inode *inode = Inode2 + ROOT_INO;
807 if (!inode || !S_ISDIR(inode->i_mode))
808 die("Root inode isn't a directory");
812 static int add_zone(uint16_t *znr, int *corrected)
818 block = check_zone_nr(znr, corrected);
821 if (zone_count[block]) {
822 printf("Already used block is reused in file '%s'. ",
824 if (ask("Clear", 1)) {
831 if (!zone_in_use(block)) {
832 printf("Block %d in file '%s' is marked as 'unused'. ",
833 block, current_name);
834 if (ask("Correct", 1))
837 if (!++zone_count[block])
842 #ifdef CONFIG_FEATURE_MINIX2
843 static int add_zone2(uint32_t *znr, int *corrected)
849 block = check_zone_nr2(znr, corrected);
852 if (zone_count[block]) {
853 printf("Already used block is reused in file '%s'. ",
855 if (ask("Clear", 1)) {
862 if (!zone_in_use(block)) {
863 printf("Block %d in file '%s' is marked as 'unused'. ",
864 block, current_name);
865 if (ask("Correct", 1))
868 if (!++zone_count[block])
874 static void add_zone_ind(uint16_t *znr, int *corrected)
876 static char blk[BLOCK_SIZE];
880 block = add_zone(znr, corrected);
883 read_block(block, blk);
884 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
885 add_zone(i + (uint16_t *) blk, &chg_blk);
887 write_block(block, blk);
890 #ifdef CONFIG_FEATURE_MINIX2
891 static void add_zone_ind2(uint32_t *znr, int *corrected)
893 static char blk[BLOCK_SIZE];
897 block = add_zone2(znr, corrected);
900 read_block(block, blk);
901 for (i = 0; i < BLOCK_SIZE >> 2; i++)
902 add_zone2(i + (uint32_t *) blk, &chg_blk);
904 write_block(block, blk);
908 static void add_zone_dind(uint16_t *znr, int *corrected)
910 static char blk[BLOCK_SIZE];
914 block = add_zone(znr, corrected);
917 read_block(block, blk);
918 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
919 add_zone_ind(i + (uint16_t *) blk, &blk_chg);
921 write_block(block, blk);
924 #ifdef CONFIG_FEATURE_MINIX2
925 static void add_zone_dind2(uint32_t *znr, int *corrected)
927 static char blk[BLOCK_SIZE];
931 block = add_zone2(znr, corrected);
934 read_block(block, blk);
935 for (i = 0; i < BLOCK_SIZE >> 2; i++)
936 add_zone_ind2(i + (uint32_t *) blk, &blk_chg);
938 write_block(block, blk);
941 static void add_zone_tind2(uint32_t *znr, int *corrected)
943 static char blk[BLOCK_SIZE];
947 block = add_zone2(znr, corrected);
950 read_block(block, blk);
951 for (i = 0; i < BLOCK_SIZE >> 2; i++)
952 add_zone_dind2(i + (uint32_t *) blk, &blk_chg);
954 write_block(block, blk);
958 static void check_zones(unsigned int i)
960 struct minix_inode *inode;
962 if (!i || i > INODES)
964 if (inode_count[i] > 1) /* have we counted this file already? */
967 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
968 !S_ISLNK(inode->i_mode)) return;
969 for (i = 0; i < 7; i++)
970 add_zone(i + inode->i_zone, &changed);
971 add_zone_ind(7 + inode->i_zone, &changed);
972 add_zone_dind(8 + inode->i_zone, &changed);
975 #ifdef CONFIG_FEATURE_MINIX2
976 static void check_zones2(unsigned int i)
978 struct minix2_inode *inode;
980 if (!i || i > INODES)
982 if (inode_count[i] > 1) /* have we counted this file already? */
985 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
986 && !S_ISLNK(inode->i_mode))
988 for (i = 0; i < 7; i++)
989 add_zone2(i + inode->i_zone, &changed);
990 add_zone_ind2(7 + inode->i_zone, &changed);
991 add_zone_dind2(8 + inode->i_zone, &changed);
992 add_zone_tind2(9 + inode->i_zone, &changed);
996 static void check_file(struct minix_inode *dir, unsigned int offset)
998 static char blk[BLOCK_SIZE];
999 struct minix_inode *inode;
1004 block = map_block(dir, offset / BLOCK_SIZE);
1005 read_block(block, blk);
1006 name = blk + (offset % BLOCK_SIZE) + 2;
1007 ino = *(uint16_t *) (name - 2);
1009 printf("%s contains a bad inode number for file '%.*s'. ",
1010 current_name, namelen, name);
1011 if (ask("Remove", 1)) {
1012 *(uint16_t *) (name - 2) = 0;
1013 write_block(block, blk);
1017 push_filename(name);
1018 inode = get_inode(ino);
1021 if (!inode || strcmp(".", name)) {
1022 printf("%s: bad directory: '.' isn't first\n", current_name);
1023 errors_uncorrected = 1;
1027 if (offset == dirsize) {
1028 if (!inode || strcmp("..", name)) {
1029 printf("%s: bad directory: '..' isn't second\n", current_name);
1030 errors_uncorrected = 1;
1036 push_filename(name);
1039 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1040 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
1043 if (inode && S_ISDIR(inode->i_mode))
1044 recursive_check(ino);
1049 #ifdef CONFIG_FEATURE_MINIX2
1050 static void check_file2(struct minix2_inode *dir, unsigned int offset)
1052 static char blk[BLOCK_SIZE];
1053 struct minix2_inode *inode;
1058 block = map_block2(dir, offset / BLOCK_SIZE);
1059 read_block(block, blk);
1060 name = blk + (offset % BLOCK_SIZE) + 2;
1061 ino = *(uint16_t *) (name - 2);
1063 printf("%s contains a bad inode number for file '%.*s'. ",
1064 current_name, namelen, name);
1065 if (ask("Remove", 1)) {
1066 *(uint16_t *) (name - 2) = 0;
1067 write_block(block, blk);
1071 push_filename(name);
1072 inode = get_inode2(ino);
1075 if (!inode || strcmp(".", name)) {
1076 printf("%s: bad directory: '.' isn't first\n", current_name);
1077 errors_uncorrected = 1;
1081 if (offset == dirsize) {
1082 if (!inode || strcmp("..", name)) {
1083 printf("%s: bad directory: '..' isn't second\n", current_name);
1084 errors_uncorrected = 1;
1090 push_filename(name);
1093 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1094 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
1097 if (inode && S_ISDIR(inode->i_mode))
1098 recursive_check2(ino);
1104 static void recursive_check(unsigned int ino)
1106 struct minix_inode *dir;
1107 unsigned int offset;
1110 if (!S_ISDIR(dir->i_mode))
1111 die("Internal error");
1112 if (dir->i_size < 2 * dirsize) {
1113 printf("%s: bad directory: size<32", current_name);
1114 errors_uncorrected = 1;
1116 for (offset = 0; offset < dir->i_size; offset += dirsize)
1117 check_file(dir, offset);
1120 #ifdef CONFIG_FEATURE_MINIX2
1121 static void recursive_check2(unsigned int ino)
1123 struct minix2_inode *dir;
1124 unsigned int offset;
1127 if (!S_ISDIR(dir->i_mode))
1128 die("Internal error");
1129 if (dir->i_size < 2 * dirsize) {
1130 printf("%s: bad directory: size<32", current_name);
1131 errors_uncorrected = 1;
1133 for (offset = 0; offset < dir->i_size; offset += dirsize)
1134 check_file2(dir, offset);
1138 static int bad_zone(int i)
1142 if (BLOCK_SIZE * i != lseek(IN, BLOCK_SIZE * i, SEEK_SET))
1143 die("Seek failed in bad_zone");
1144 return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE));
1147 static void check_counts(void)
1151 for (i = 1; i <= INODES; i++) {
1152 if (warn_mode && Inode[i].i_mode && !inode_in_use(i)) {
1153 printf("Inode %d has non-zero mode. ", i);
1154 if (ask("Clear", 1)) {
1155 Inode[i].i_mode = 0;
1159 if (!inode_count[i]) {
1160 if (!inode_in_use(i))
1162 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
1163 if (ask("Clear", 1))
1167 if (!inode_in_use(i)) {
1168 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
1172 if (Inode[i].i_nlinks != inode_count[i]) {
1173 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
1174 i, Inode[i].i_mode, Inode[i].i_nlinks, inode_count[i]);
1175 if (ask("Set i_nlinks to count", 1)) {
1176 Inode[i].i_nlinks = inode_count[i];
1181 for (i = FIRSTZONE; i < ZONES; i++) {
1182 if (zone_in_use(i) == zone_count[i])
1184 if (!zone_count[i]) {
1187 printf("Zone %d is marked 'in use', but no file uses it. ", i);
1188 if (ask("Unmark", 1))
1192 printf("Zone %d: %sin use, counted=%d\n",
1193 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1197 #ifdef CONFIG_FEATURE_MINIX2
1198 static void check_counts2(void)
1202 for (i = 1; i <= INODES; i++) {
1203 if (warn_mode && Inode2[i].i_mode && !inode_in_use(i)) {
1204 printf("Inode %d has non-zero mode. ", i);
1205 if (ask("Clear", 1)) {
1206 Inode2[i].i_mode = 0;
1210 if (!inode_count[i]) {
1211 if (!inode_in_use(i))
1213 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
1214 if (ask("Clear", 1))
1218 if (!inode_in_use(i)) {
1219 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
1223 if (Inode2[i].i_nlinks != inode_count[i]) {
1224 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
1225 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1227 if (ask("Set i_nlinks to count", 1)) {
1228 Inode2[i].i_nlinks = inode_count[i];
1233 for (i = FIRSTZONE; i < ZONES; i++) {
1234 if (zone_in_use(i) == zone_count[i])
1236 if (!zone_count[i]) {
1239 printf("Zone %d is marked 'in use', but no file uses it. ", i);
1240 if (ask("Unmark", 1))
1244 printf("Zone %d: %sin use, counted=%d\n",
1245 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1250 static void check(void)
1252 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1253 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1254 check_zones(ROOT_INO);
1255 recursive_check(ROOT_INO);
1259 #ifdef CONFIG_FEATURE_MINIX2
1260 static void check2(void)
1262 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1263 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1264 check_zones2(ROOT_INO);
1265 recursive_check2(ROOT_INO);
1270 int fsck_minix_main(int argc, char **argv)
1275 alloc_current_name();
1276 #ifdef CONFIG_FEATURE_CLEAN_UP
1277 /* Don't bother to free memory. Exit does
1278 * that automagically, so we can save a few bytes */
1279 atexit(free_current_name);
1282 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
1283 die("Bad inode size");
1284 #ifdef CONFIG_FEATURE_MINIX2
1285 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
1286 die("Bad v2 inode size");
1288 while (argc-- > 1) {
1290 if (argv[0][0] != '-') {
1294 device_name = argv[0];
1297 switch (argv[0][0]) {
1327 check_mount(); /* trying to check a mounted filesystem? */
1328 if (repair && !automatic) {
1329 if (!isatty(0) || !isatty(1))
1330 die("Need terminal for interactive repairs");
1332 IN = open(device_name, repair ? O_RDWR : O_RDONLY);
1334 printf("Unable to open device '%s'\n", device_name);
1337 sync(); /* paranoia? */
1341 * Determine whether or not we should continue with the checking.
1342 * This is based on the status of the filesystem valid and error
1343 * flags and whether or not the -f switch was specified on the
1346 printf("%s, %s\n", applet_name, program_version);
1347 if (!(Super.s_state & MINIX_ERROR_FS) &&
1348 (Super.s_state & MINIX_VALID_FS) && !force) {
1350 printf("%s is clean, check is skipped\n", device_name);
1353 printf("Forcing filesystem check on %s\n", device_name);
1355 printf("Filesystem on %s is dirty, needs checking\n",
1360 if (repair && !automatic) {
1361 tcgetattr(0, &termios);
1363 tmp.c_lflag &= ~(ICANON | ECHO);
1364 tcsetattr(0, TCSANOW, &tmp);
1367 #ifdef CONFIG_FEATURE_MINIX2
1380 for (i = 1, free_cnt = 0; i <= INODES; i++)
1381 if (!inode_in_use(i))
1383 printf("\n%6ld inodes used (%ld%%)\n", (INODES - free_cnt),
1384 100 * (INODES - free_cnt) / INODES);
1385 for (i = FIRSTZONE, free_cnt = 0; i < ZONES; i++)
1386 if (!zone_in_use(i))
1388 printf("%6ld zones used (%ld%%)\n\n"
1389 "%6d regular files\n"
1391 "%6d character device files\n"
1392 "%6d block device files\n"
1394 "%6d symbolic links\n"
1397 (ZONES - free_cnt), 100 * (ZONES - free_cnt) / ZONES,
1398 regular, directory, chardev, blockdev,
1399 links - 2 * directory + 1, symlinks,
1400 total - 2 * directory + 1);
1404 printf("FILE SYSTEM HAS BEEN CHANGED\n");
1407 write_super_block();
1409 if (repair && !automatic)
1410 tcsetattr(0, TCSANOW, &termios);
1414 if (errors_uncorrected)