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>
106 #include "exitcodes.h"
107 #include "minix_programs.h"
109 #include "pathnames.h"
111 #include "ismounted.h"
112 #include "writeall.h"
115 #define YESNO_LENGTH 64
117 /* Global variables used in minix_programs.h inline fuctions */
119 char *super_block_buffer
;
121 static char *inode_buffer
;
123 #define Inode (((struct minix_inode *) inode_buffer) - 1)
124 #define Inode2 (((struct minix2_inode *) inode_buffer) - 1)
126 static char *device_name
;
128 static int repair
, automatic
, verbose
, list
, show
, warn_mode
, force
;
129 static int directory
, regular
, blockdev
, chardev
, links
, symlinks
, total
;
131 static int changed
; /* flags if the filesystem has been changed */
132 static int errors_uncorrected
; /* flag if some error was not corrected */
133 static size_t dirsize
= 16;
134 static size_t namelen
= 14;
135 static struct termios termios
;
136 static volatile sig_atomic_t termios_set
;
140 static int name_depth
;
141 static char name_list
[MAX_DEPTH
][MINIX_NAME_MAX
+ 1];
143 /* Copy of the previous, just for error reporting - see get_current_name. This
144 * is a waste of 12kB or so. */
145 static char current_name
[MAX_DEPTH
* (MINIX_NAME_MAX
+ 1) + 1];
147 #define MAGIC (Super.s_magic)
149 static unsigned char *inode_count
= NULL
;
150 static unsigned char *zone_count
= NULL
;
152 static void recursive_check(unsigned int ino
);
153 static void recursive_check2(unsigned int ino
);
155 static char *inode_map
;
156 static char *zone_map
;
158 #define inode_in_use(x) (isset(inode_map,(x)) != 0)
159 #define zone_in_use(x) (isset(zone_map,(x)-get_first_zone()+1) != 0)
161 #define mark_inode(x) (setbit(inode_map,(x)),changed=1)
162 #define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
164 #define mark_zone(x) (setbit(zone_map,(x)-get_first_zone()+1),changed=1)
165 #define unmark_zone(x) (clrbit(zone_map,(x)-get_first_zone()+1),changed=1)
170 tcsetattr(0, TCSANOW
, &termios
);
175 /* We received a fatal signal. Reset the terminal. Also reset the
176 * signal handler and re-send the signal, so that the parent process
177 * knows which signal actually caused our death. */
178 signal(sig
, SIG_DFL
);
191 fputs(USAGE_HEADER
, stderr
);
193 _(" %s [options] <device>\n"), program_invocation_short_name
);
194 fputs(USAGE_OPTIONS
, stderr
);
195 fputs(_(" -l list all filenames\n"), stderr
);
196 fputs(_(" -a automatic repair\n"), stderr
);
197 fputs(_(" -r interactive repair\n"), stderr
);
198 fputs(_(" -v be verbose\n"), stderr
);
199 fputs(_(" -s output super-block information\n"), stderr
);
200 fputs(_(" -m activate mode not cleared warnings\n"), stderr
);
201 fputs(_(" -f force check\n"), stderr
);
202 fputs(USAGE_SEPARATOR
, stderr
);
203 fputs(USAGE_VERSION
, stderr
);
204 fprintf(stderr
, USAGE_MAN_TAIL("fsck.minix(8)"));
205 leave(FSCK_EX_USAGE
);
208 static void die(const char *fmt
, ...)
209 __attribute__ ((__format__(__printf__
, 1, 2)));
212 die(const char *fmt
, ...) {
215 fprintf(stderr
, "%s: ", program_invocation_short_name
);
217 vfprintf(stderr
, fmt
, ap
);
220 leave(FSCK_EX_ERROR
);
223 /* This simply goes through the file-name data and prints out the current file. */
225 get_current_name(void) {
230 while (i
< name_depth
) {
243 ask(const char *string
, int def
) {
245 char input
[YESNO_LENGTH
];
249 errors_uncorrected
= 1;
255 errors_uncorrected
= 1;
258 /* TRANSLATORS: these yes no questions uses rpmatch(), and should be
260 printf(def
? _("%s (y/n)? ") : _("%s (n/y)? "), string
);
262 fgets(input
, YESNO_LENGTH
, stdin
);
263 resp
= rpmatch(input
);
280 errors_uncorrected
= 1;
285 /* Make certain that we aren't checking a filesystem that is on a mounted
286 * partition. Code adapted from e2fsck, Copyright (C) 1993, 1994 Theodore
287 * Ts'o. Also licensed under GPL. */
292 if (!is_mounted(device_name
))
295 printf(_("%s is mounted. "), device_name
);
296 if (isatty(0) && isatty(1))
297 cont
= ask(_("Do you really want to continue"), 0);
301 printf(_("check aborted.\n"));
307 /* check_zone_nr checks to see that *nr is a valid zone nr. If it isn't, it
308 * will possibly be repaired. Check_zone_nr sets *corrected if an error was
309 * corrected, and returns the zone (0 for no zone or a bad zone-number). */
311 check_zone_nr(unsigned short *nr
, int *corrected
) {
315 if (*nr
< get_first_zone()) {
317 printf(_("Zone nr < FIRSTZONE in file `%s'."), current_name
);
318 } else if (*nr
>= get_nzones()) {
320 printf(_("Zone nr >= ZONES in file `%s'."), current_name
);
324 if (ask(_("Remove block"), 1)) {
332 check_zone_nr2(unsigned int *nr
, int *corrected
) {
336 if (*nr
< get_first_zone()) {
338 printf(_("Zone nr < FIRSTZONE in file `%s'."), current_name
);
339 } else if (*nr
>= get_nzones()) {
341 printf(_("Zone nr >= ZONES in file `%s'."), current_name
);
345 if (ask(_("Remove block"), 1)) {
352 /* read-block reads block nr into the buffer at addr. */
354 read_block(unsigned int nr
, char *addr
) {
356 memset(addr
, 0, MINIX_BLOCK_SIZE
);
359 if (MINIX_BLOCK_SIZE
* nr
!= lseek(IN
, MINIX_BLOCK_SIZE
* nr
, SEEK_SET
)) {
361 printf(_("Read error: unable to seek to block in file '%s'\n"),
363 memset(addr
, 0, MINIX_BLOCK_SIZE
);
364 errors_uncorrected
= 1;
365 } else if (MINIX_BLOCK_SIZE
!= read(IN
, addr
, MINIX_BLOCK_SIZE
)) {
367 printf(_("Read error: bad block in file '%s'\n"), current_name
);
368 memset(addr
, 0, MINIX_BLOCK_SIZE
);
369 errors_uncorrected
= 1;
373 /* write_block writes block nr to disk. */
375 write_block(unsigned int nr
, char *addr
) {
378 if (nr
< get_first_zone() || nr
>= get_nzones()) {
379 printf(_("Internal error: trying to write bad block\n"
380 "Write request ignored\n"));
381 errors_uncorrected
= 1;
384 if (MINIX_BLOCK_SIZE
* nr
!= lseek(IN
, MINIX_BLOCK_SIZE
* nr
, SEEK_SET
))
385 die(_("seek failed in write_block"));
386 if (MINIX_BLOCK_SIZE
!= write(IN
, addr
, MINIX_BLOCK_SIZE
)) {
388 printf(_("Write error: bad block in file '%s'\n"),
390 errors_uncorrected
= 1;
394 /* map-block calculates the absolute block nr of a block in a file. It sets
395 * 'changed' if the inode has needed changing, and re-writes any indirect
396 * blocks with errors. */
398 map_block(struct minix_inode
*inode
, unsigned int blknr
) {
399 unsigned short ind
[MINIX_BLOCK_SIZE
>> 1];
400 unsigned short dind
[MINIX_BLOCK_SIZE
>> 1];
401 int blk_chg
, block
, result
;
404 return check_zone_nr(inode
->i_zone
+ blknr
, &changed
);
407 block
= check_zone_nr(inode
->i_zone
+ 7, &changed
);
408 read_block(block
, (char *)ind
);
410 result
= check_zone_nr(blknr
+ ind
, &blk_chg
);
412 write_block(block
, (char *)ind
);
416 block
= check_zone_nr(inode
->i_zone
+ 8, &changed
);
417 read_block(block
, (char *)dind
);
419 result
= check_zone_nr(dind
+ (blknr
/ 512), &blk_chg
);
421 write_block(block
, (char *)dind
);
423 read_block(block
, (char *)ind
);
425 result
= check_zone_nr(ind
+ (blknr
% 512), &blk_chg
);
427 write_block(block
, (char *)ind
);
432 map_block2(struct minix2_inode
*inode
, unsigned int blknr
) {
433 unsigned int ind
[MINIX_BLOCK_SIZE
>> 2];
434 unsigned int dind
[MINIX_BLOCK_SIZE
>> 2];
435 unsigned int tind
[MINIX_BLOCK_SIZE
>> 2];
436 int blk_chg
, block
, result
;
439 return check_zone_nr2(inode
->i_zone
+ blknr
, &changed
);
442 block
= check_zone_nr2(inode
->i_zone
+ 7, &changed
);
443 read_block(block
, (char *)ind
);
445 result
= check_zone_nr2(blknr
+ ind
, &blk_chg
);
447 write_block(block
, (char *)ind
);
451 if (blknr
< 256 * 256) {
452 block
= check_zone_nr2(inode
->i_zone
+ 8, &changed
);
453 read_block(block
, (char *)dind
);
455 result
= check_zone_nr2(dind
+ blknr
/ 256, &blk_chg
);
457 write_block(block
, (char *)dind
);
459 read_block(block
, (char *)ind
);
461 result
= check_zone_nr2(ind
+ blknr
% 256, &blk_chg
);
463 write_block(block
, (char *)ind
);
467 block
= check_zone_nr2(inode
->i_zone
+ 9, &changed
);
468 read_block(block
, (char *)tind
);
470 result
= check_zone_nr2(tind
+ blknr
/ (256 * 256), &blk_chg
);
472 write_block(block
, (char *)tind
);
474 read_block(block
, (char *)dind
);
476 result
= check_zone_nr2(dind
+ (blknr
/ 256) % 256, &blk_chg
);
478 write_block(block
, (char *)dind
);
480 read_block(block
, (char *)ind
);
482 result
= check_zone_nr2(ind
+ blknr
% 256, &blk_chg
);
484 write_block(block
, (char *)ind
);
489 write_super_block(void) {
490 /* Set the state of the filesystem based on whether or not there are
491 * uncorrected errors. The filesystem valid flag is unconditionally
492 * set if we get this far. */
493 Super
.s_state
|= MINIX_VALID_FS
;
494 if (errors_uncorrected
)
495 Super
.s_state
|= MINIX_ERROR_FS
;
497 Super
.s_state
&= ~MINIX_ERROR_FS
;
499 if (MINIX_BLOCK_SIZE
!= lseek(IN
, MINIX_BLOCK_SIZE
, SEEK_SET
))
500 die(_("seek failed in write_super_block"));
501 if (MINIX_BLOCK_SIZE
!= write(IN
, super_block_buffer
, MINIX_BLOCK_SIZE
))
502 die(_("unable to write super-block"));
509 unsigned long buffsz
= get_inode_buffer_size();
510 unsigned long imaps
= get_nimaps();
511 unsigned long zmaps
= get_nzmaps();
513 if (write_all(IN
, inode_map
, imaps
* MINIX_BLOCK_SIZE
))
514 die(_("Unable to write inode map"));
516 if (write_all(IN
, zone_map
, zmaps
* MINIX_BLOCK_SIZE
))
517 die(_("Unable to write zone map"));
519 if (write_all(IN
, inode_buffer
, buffsz
))
520 die(_("Unable to write inodes"));
526 char blk
[MINIX_BLOCK_SIZE
];
530 block
= Inode2
[ROOT_INO
].i_zone
[0];
532 block
= Inode
[ROOT_INO
].i_zone
[0];
533 read_block(block
, blk
);
535 for (size
= 16; size
< MINIX_BLOCK_SIZE
; size
<<= 1) {
536 if (strcmp(blk
+ size
+ 2, "..") == 0) {
546 read_superblock(void) {
547 if (MINIX_BLOCK_SIZE
!= lseek(IN
, MINIX_BLOCK_SIZE
, SEEK_SET
))
548 die(_("seek failed"));
550 super_block_buffer
= calloc(1, MINIX_BLOCK_SIZE
);
551 if (!super_block_buffer
)
552 die(_("unable to alloc buffer for superblock"));
554 if (MINIX_BLOCK_SIZE
!= read(IN
, super_block_buffer
, MINIX_BLOCK_SIZE
))
555 die(_("unable to read super block"));
556 if (MAGIC
== MINIX_SUPER_MAGIC
) {
560 } else if (MAGIC
== MINIX_SUPER_MAGIC2
) {
564 } else if (MAGIC
== MINIX2_SUPER_MAGIC
) {
568 } else if (MAGIC
== MINIX2_SUPER_MAGIC2
) {
573 die(_("bad magic number in super-block"));
574 if (get_zone_size() != 0 || MINIX_BLOCK_SIZE
!= 1024)
575 die(_("Only 1k blocks/zones supported"));
576 if (get_nimaps() * MINIX_BLOCK_SIZE
* 8 < get_ninodes() + 1)
577 die(_("bad s_imap_blocks field in super-block"));
578 if (get_nzmaps() * MINIX_BLOCK_SIZE
* 8 <
579 get_nzones() - get_first_zone() + 1)
580 die(_("bad s_zmap_blocks field in super-block"));
585 unsigned long inodes
= get_ninodes();
586 unsigned long buffsz
= get_inode_buffer_size();
587 unsigned long norm_first_zone
= first_zone_data();
588 unsigned long first_zone
= get_first_zone();
589 unsigned long zones
= get_nzones();
590 unsigned long imaps
= get_nimaps();
591 unsigned long zmaps
= get_nzmaps();
594 inode_map
= malloc(imaps
* MINIX_BLOCK_SIZE
);
596 die(_("Unable to allocate buffer for inode map"));
597 zone_map
= malloc(zmaps
* MINIX_BLOCK_SIZE
);
599 die(_("Unable to allocate buffer for zone map"));
600 inode_buffer
= malloc(buffsz
);
602 die(_("Unable to allocate buffer for inodes"));
603 inode_count
= calloc(1, inodes
+ 1);
605 die(_("Unable to allocate buffer for inode count"));
606 zone_count
= calloc(1, zones
);
608 die(_("Unable to allocate buffer for zone count"));
610 rc
= read(IN
, inode_map
, imaps
* MINIX_BLOCK_SIZE
);
611 if (rc
< 0 || imaps
* MINIX_BLOCK_SIZE
!= (size_t) rc
)
612 die(_("Unable to read inode map"));
614 rc
= read(IN
, zone_map
, zmaps
* MINIX_BLOCK_SIZE
);
615 if (rc
< 0 || zmaps
* MINIX_BLOCK_SIZE
!= (size_t) rc
)
616 die(_("Unable to read zone map"));
618 rc
= read(IN
, inode_buffer
, buffsz
);
619 if (rc
< 0 || buffsz
!= (size_t) rc
)
620 die(_("Unable to read inodes"));
621 if (norm_first_zone
!= first_zone
) {
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"), first_zone
, norm_first_zone
);
630 printf(_("Zonesize=%d\n"), MINIX_BLOCK_SIZE
<< get_zone_size());
631 printf(_("Maxsize=%ld\n"), get_max_size());
632 printf(_("Filesystem state=%d\n"), Super
.s_state
);
633 printf(_("namelen=%zd\n\n"), namelen
);
637 static struct minix_inode
*
638 get_inode(unsigned int nr
) {
639 struct minix_inode
*inode
;
641 if (!nr
|| nr
> get_ninodes())
645 if (!inode_count
[nr
]) {
646 if (!inode_in_use(nr
)) {
648 printf(_("Inode %d marked unused, "
649 "but used for file '%s'\n"), nr
, 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
))
669 else if (S_ISFIFO(inode
->i_mode
))
673 printf(_("The file `%s' has mode %05o\n"),
674 current_name
, inode
->i_mode
);
679 if (!++inode_count
[nr
]) {
680 printf(_("Warning: inode count too big.\n"));
682 errors_uncorrected
= 1;
687 static struct minix2_inode
*
688 get_inode2(unsigned int nr
) {
689 struct minix2_inode
*inode
;
691 if (!nr
|| nr
> get_ninodes())
695 if (!inode_count
[nr
]) {
696 if (!inode_in_use(nr
)) {
698 printf(_("Inode %d marked unused, "
699 "but used for file '%s'\n"), nr
, current_name
);
701 if (ask(_("Mark in use"), 1))
704 errors_uncorrected
= 1;
707 if (S_ISDIR(inode
->i_mode
))
709 else if (S_ISREG(inode
->i_mode
))
711 else if (S_ISCHR(inode
->i_mode
))
713 else if (S_ISBLK(inode
->i_mode
))
715 else if (S_ISLNK(inode
->i_mode
))
717 else if (S_ISSOCK(inode
->i_mode
)) ;
718 else if (S_ISFIFO(inode
->i_mode
)) ;
721 printf(_("The file `%s' has mode %05o\n"),
722 current_name
, inode
->i_mode
);
726 if (!++inode_count
[nr
]) {
727 printf(_("Warning: inode count too big.\n"));
729 errors_uncorrected
= 1;
736 struct minix_inode
*inode
= Inode
+ ROOT_INO
;
738 if (!inode
|| !S_ISDIR(inode
->i_mode
))
739 die(_("root inode isn't a directory"));
744 struct minix2_inode
*inode
= Inode2
+ ROOT_INO
;
746 if (!inode
|| !S_ISDIR(inode
->i_mode
))
747 die(_("root inode isn't a directory"));
751 add_zone(unsigned short *znr
, int *corrected
) {
754 block
= check_zone_nr(znr
, corrected
);
757 if (zone_count
[block
]) {
759 printf(_("Block has been used before. Now in file `%s'."),
761 if (ask(_("Clear"), 1)) {
769 if (!zone_in_use(block
)) {
771 printf(_("Block %d in file `%s' is marked not in use."),
772 block
, current_name
);
773 if (ask(_("Correct"), 1))
776 if (!++zone_count
[block
])
782 add_zone2(unsigned int *znr
, int *corrected
) {
785 block
= check_zone_nr2(znr
, corrected
);
788 if (zone_count
[block
]) {
790 printf(_("Block has been used before. Now in file `%s'."),
792 if (ask(_("Clear"), 1)) {
800 if (!zone_in_use(block
)) {
802 printf(_("Block %d in file `%s' is marked not in use."),
803 block
, current_name
);
804 if (ask(_("Correct"), 1))
807 if (!++zone_count
[block
])
813 add_zone_ind(unsigned short *znr
, int *corrected
) {
814 static char blk
[MINIX_BLOCK_SIZE
];
818 block
= add_zone(znr
, corrected
);
821 read_block(block
, blk
);
822 for (i
= 0; i
< (MINIX_BLOCK_SIZE
>> 1); i
++)
823 add_zone(i
+ (unsigned short *)blk
, &chg_blk
);
825 write_block(block
, blk
);
829 add_zone_ind2(unsigned int *znr
, int *corrected
) {
830 static char blk
[MINIX_BLOCK_SIZE
];
834 block
= add_zone2(znr
, corrected
);
837 read_block(block
, blk
);
838 for (i
= 0; i
< MINIX_BLOCK_SIZE
>> 2; i
++)
839 add_zone2(i
+ (unsigned int *)blk
, &chg_blk
);
841 write_block(block
, blk
);
845 add_zone_dind(unsigned short *znr
, int *corrected
) {
846 static char blk
[MINIX_BLOCK_SIZE
];
850 block
= add_zone(znr
, corrected
);
853 read_block(block
, blk
);
854 for (i
= 0; i
< (MINIX_BLOCK_SIZE
>> 1); i
++)
855 add_zone_ind(i
+ (unsigned short *)blk
, &blk_chg
);
857 write_block(block
, blk
);
861 add_zone_dind2(unsigned int *znr
, int *corrected
) {
862 static char blk
[MINIX_BLOCK_SIZE
];
866 block
= add_zone2(znr
, corrected
);
869 read_block(block
, blk
);
870 for (i
= 0; i
< MINIX_BLOCK_SIZE
>> 2; i
++)
871 add_zone_ind2(i
+ (unsigned int *)blk
, &blk_chg
);
873 write_block(block
, blk
);
877 add_zone_tind2(unsigned int *znr
, int *corrected
) {
878 static char blk
[MINIX_BLOCK_SIZE
];
882 block
= add_zone2(znr
, corrected
);
885 read_block(block
, blk
);
886 for (i
= 0; i
< MINIX_BLOCK_SIZE
>> 2; i
++)
887 add_zone_dind2(i
+ (unsigned int *)blk
, &blk_chg
);
889 write_block(block
, blk
);
893 check_zones(unsigned int i
) {
894 struct minix_inode
*inode
;
896 if (!i
|| i
> get_ninodes())
898 if (inode_count
[i
] > 1) /* have we counted this file already? */
901 if (!S_ISDIR(inode
->i_mode
) && !S_ISREG(inode
->i_mode
) &&
902 !S_ISLNK(inode
->i_mode
))
904 for (i
= 0; i
< 7; i
++)
905 add_zone(i
+ inode
->i_zone
, &changed
);
906 add_zone_ind(7 + inode
->i_zone
, &changed
);
907 add_zone_dind(8 + inode
->i_zone
, &changed
);
911 check_zones2(unsigned int i
) {
912 struct minix2_inode
*inode
;
914 if (!i
|| i
> get_ninodes())
916 if (inode_count
[i
] > 1) /* have we counted this file already? */
919 if (!S_ISDIR(inode
->i_mode
) && !S_ISREG(inode
->i_mode
)
920 && !S_ISLNK(inode
->i_mode
))
922 for (i
= 0; i
< 7; i
++)
923 add_zone2(i
+ inode
->i_zone
, &changed
);
924 add_zone_ind2(7 + inode
->i_zone
, &changed
);
925 add_zone_dind2(8 + inode
->i_zone
, &changed
);
926 add_zone_tind2(9 + inode
->i_zone
, &changed
);
930 check_file(struct minix_inode
*dir
, unsigned int offset
) {
931 static char blk
[MINIX_BLOCK_SIZE
];
932 struct minix_inode
*inode
;
937 block
= map_block(dir
, offset
/ MINIX_BLOCK_SIZE
);
938 read_block(block
, blk
);
939 name
= blk
+ (offset
% MINIX_BLOCK_SIZE
) + 2;
940 ino
= *(unsigned short *)(name
- 2);
941 if (ino
> get_ninodes()) {
943 printf(_("The directory '%s' contains a bad inode number "
944 "for file '%.*s'."), current_name
, (int)namelen
, name
);
945 if (ask(_(" Remove"), 1)) {
946 *(unsigned short *)(name
- 2) = 0;
947 write_block(block
, blk
);
951 if (name_depth
< MAX_DEPTH
)
952 strncpy(name_list
[name_depth
], name
, namelen
);
954 inode
= get_inode(ino
);
957 if (!inode
|| strcmp(".", name
)) {
959 printf(_("%s: bad directory: '.' isn't first\n"),
961 errors_uncorrected
= 1;
965 if (offset
== dirsize
) {
966 if (!inode
|| strcmp("..", name
)) {
968 printf(_("%s: bad directory: '..' isn't second\n"),
970 errors_uncorrected
= 1;
976 if (name_depth
< MAX_DEPTH
)
977 strncpy(name_list
[name_depth
], name
, namelen
);
981 printf("%6d %07o %3d ", ino
,
982 inode
->i_mode
, inode
->i_nlinks
);
984 printf("%s", current_name
);
985 if (S_ISDIR(inode
->i_mode
))
991 if (inode
&& S_ISDIR(inode
->i_mode
))
992 recursive_check(ino
);
998 check_file2(struct minix2_inode
*dir
, unsigned int offset
) {
999 static char blk
[MINIX_BLOCK_SIZE
];
1000 struct minix2_inode
*inode
;
1005 block
= map_block2(dir
, offset
/ MINIX_BLOCK_SIZE
);
1006 read_block(block
, blk
);
1007 name
= blk
+ (offset
% MINIX_BLOCK_SIZE
) + 2;
1008 ino
= *(unsigned short *)(name
- 2);
1009 if (ino
> get_ninodes()) {
1011 printf(_("The directory '%s' contains a bad inode number "
1012 "for file '%.*s'."), current_name
, (int)namelen
, name
);
1013 if (ask(_(" Remove"), 1)) {
1014 *(unsigned short *)(name
- 2) = 0;
1015 write_block(block
, blk
);
1019 if (name_depth
< MAX_DEPTH
)
1020 strncpy(name_list
[name_depth
], name
, namelen
);
1022 inode
= get_inode2(ino
);
1025 if (!inode
|| strcmp(".", name
)) {
1027 printf(_("%s: bad directory: '.' isn't first\n"),
1029 errors_uncorrected
= 1;
1033 if (offset
== dirsize
) {
1034 if (!inode
|| strcmp("..", name
)) {
1036 printf(_("%s: bad directory: '..' isn't second\n"),
1038 errors_uncorrected
= 1;
1047 printf("%6zd %07o %3d ", ino
, inode
->i_mode
,
1050 printf("%s", current_name
);
1051 if (S_ISDIR(inode
->i_mode
))
1057 if (inode
&& S_ISDIR(inode
->i_mode
))
1058 recursive_check2(ino
);
1064 recursive_check(unsigned int ino
) {
1065 struct minix_inode
*dir
;
1066 unsigned int offset
;
1069 if (!S_ISDIR(dir
->i_mode
))
1070 die(_("internal error"));
1071 if (dir
->i_size
< 2 * dirsize
) {
1073 printf(_("%s: bad directory: size < 32"), current_name
);
1074 errors_uncorrected
= 1;
1076 for (offset
= 0; offset
< dir
->i_size
; offset
+= dirsize
)
1077 check_file(dir
, offset
);
1081 recursive_check2(unsigned int ino
) {
1082 struct minix2_inode
*dir
;
1083 unsigned int offset
;
1086 if (!S_ISDIR(dir
->i_mode
))
1087 die(_("internal error"));
1088 if (dir
->i_size
< 2 * dirsize
) {
1090 printf(_("%s: bad directory: size < 32"), current_name
);
1091 errors_uncorrected
= 1;
1093 for (offset
= 0; offset
< dir
->i_size
; offset
+= dirsize
)
1094 check_file2(dir
, offset
);
1101 if (MINIX_BLOCK_SIZE
* i
!= lseek(IN
, MINIX_BLOCK_SIZE
* i
, SEEK_SET
))
1102 die(_("seek failed in bad_zone"));
1103 return (MINIX_BLOCK_SIZE
!= read(IN
, buffer
, MINIX_BLOCK_SIZE
));
1107 check_counts(void) {
1110 for (i
= 1; i
<= get_ninodes(); i
++) {
1111 if (!inode_in_use(i
) && Inode
[i
].i_mode
&& warn_mode
) {
1112 printf(_("Inode %lu mode not cleared."), i
);
1113 if (ask(_("Clear"), 1)) {
1114 Inode
[i
].i_mode
= 0;
1118 if (!inode_count
[i
]) {
1119 if (!inode_in_use(i
))
1121 printf(_("Inode %lu not used, marked used in the bitmap."), i
);
1122 if (ask(_("Clear"), 1))
1126 if (!inode_in_use(i
)) {
1127 printf(_("Inode %lu used, marked unused in the bitmap."), i
);
1128 if (ask(_("Set"), 1))
1131 if (Inode
[i
].i_nlinks
!= inode_count
[i
]) {
1132 printf(_("Inode %lu (mode = %07o), i_nlinks=%d, counted=%d."),
1133 i
, Inode
[i
].i_mode
, Inode
[i
].i_nlinks
,
1135 if (ask(_("Set i_nlinks to count"), 1)) {
1136 Inode
[i
].i_nlinks
= inode_count
[i
];
1141 for (i
= get_first_zone(); i
< get_nzones(); i
++) {
1142 if (zone_in_use(i
) == zone_count
[i
])
1144 if (!zone_count
[i
]) {
1147 printf(_("Zone %lu: marked in use, no file uses it."),
1149 if (ask(_("Unmark"), 1))
1154 printf(_("Zone %lu: in use, counted=%d\n"),
1157 printf(_("Zone %lu: not in use, counted=%d\n"),
1163 check_counts2(void) {
1166 for (i
= 1; i
<= get_ninodes(); i
++) {
1167 if (!inode_in_use(i
) && Inode2
[i
].i_mode
&& warn_mode
) {
1168 printf(_("Inode %lu mode not cleared."), i
);
1169 if (ask(_("Clear"), 1)) {
1170 Inode2
[i
].i_mode
= 0;
1174 if (!inode_count
[i
]) {
1175 if (!inode_in_use(i
))
1177 printf(_("Inode %lu not used, marked used in the bitmap."), i
);
1178 if (ask(_("Clear"), 1))
1182 if (!inode_in_use(i
)) {
1183 printf(_("Inode %lu used, marked unused in the bitmap."), i
);
1184 if (ask(_("Set"), 1))
1187 if (Inode2
[i
].i_nlinks
!= inode_count
[i
]) {
1188 printf(_("Inode %lu (mode = %07o), i_nlinks=%d, counted=%d."),
1189 i
, Inode2
[i
].i_mode
, Inode2
[i
].i_nlinks
,
1191 if (ask(_("Set i_nlinks to count"), 1)) {
1192 Inode2
[i
].i_nlinks
= inode_count
[i
];
1197 for (i
= get_first_zone(); i
< get_nzones(); i
++) {
1198 if (zone_in_use(i
) == zone_count
[i
])
1200 if (!zone_count
[i
]) {
1203 printf(_("Zone %lu: marked in use, no file uses it."),
1205 if (ask(_("Unmark"), 1))
1210 printf(_("Zone %lu: in use, counted=%d\n"),
1213 printf(_("Zone %lu: not in use, counted=%d\n"),
1220 memset(inode_count
, 0, (get_ninodes() + 1) * sizeof(*inode_count
));
1221 memset(zone_count
, 0, get_nzones() * sizeof(*zone_count
));
1222 check_zones(ROOT_INO
);
1223 recursive_check(ROOT_INO
);
1229 memset(inode_count
, 0, (get_ninodes() + 1) * sizeof(*inode_count
));
1230 memset(zone_count
, 0, get_nzones() * sizeof(*zone_count
));
1231 check_zones2(ROOT_INO
);
1232 recursive_check2(ROOT_INO
);
1237 main(int argc
, char **argv
) {
1242 setlocale(LC_ALL
, "");
1243 bindtextdomain(PACKAGE
, LOCALEDIR
);
1244 textdomain(PACKAGE
);
1247 (!strcmp(argv
[1], "-V") || !strcmp(argv
[1], "--version"))) {
1248 printf(UTIL_LINUX_VERSION
);
1252 if (INODE_SIZE
* MINIX_INODES_PER_BLOCK
!= MINIX_BLOCK_SIZE
)
1253 die(_("bad inode size"));
1254 if (INODE2_SIZE
* MINIX2_INODES_PER_BLOCK
!= MINIX_BLOCK_SIZE
)
1255 die(_("bad v2 inode size"));
1257 while (argc
-- > 1) {
1259 if (argv
[0][0] != '-') {
1263 device_name
= argv
[0];
1266 switch (argv
[0][0]) {
1296 check_mount(); /* trying to check a mounted filesystem? */
1297 if (repair
&& !automatic
) {
1298 if (!isatty(0) || !isatty(1))
1299 die(_("need terminal for interactive repairs"));
1301 IN
= open(device_name
, repair
? O_RDWR
: O_RDONLY
);
1303 die(_("unable to open '%s': %s"), device_name
, strerror(errno
));
1304 for (count
= 0; count
< 3; count
++)
1308 /* Determine whether or not we should continue with the checking. This
1309 * is based on the status of the filesystem valid and error flags and
1310 * whether or not the -f switch was specified on the command line. */
1311 if (!(Super
.s_state
& MINIX_ERROR_FS
) &&
1312 (Super
.s_state
& MINIX_VALID_FS
) && !force
) {
1314 printf(_("%s is clean, no check.\n"), device_name
);
1317 printf(_("Forcing filesystem check on %s.\n"), device_name
);
1319 printf(_("Filesystem on %s is dirty, needs checking.\n"),
1324 /* Restore the terminal state on fatal signals. We don't do this for
1325 * SIGALRM, SIGUSR1 or SIGUSR2. */
1326 signal(SIGINT
, fatalsig
);
1327 signal(SIGQUIT
, fatalsig
);
1328 signal(SIGTERM
, fatalsig
);
1330 if (repair
&& !automatic
) {
1331 tcgetattr(0, &termios
);
1333 tmp
.c_lflag
&= ~(ICANON
| ECHO
);
1334 tcsetattr(0, TCSANOW
, &tmp
);
1338 if (fs_version
== 2) {
1346 unsigned long i
, free
;
1348 for (i
= 1, free
= 0; i
<= get_ninodes(); i
++)
1349 if (!inode_in_use(i
))
1351 printf(_("\n%6ld inodes used (%ld%%)\n"),
1352 (get_ninodes() - free
),
1353 100 * (get_ninodes() - free
) / get_ninodes());
1354 for (i
= get_first_zone(), free
= 0; i
< get_nzones(); i
++)
1355 if (!zone_in_use(i
))
1357 printf(_("%6ld zones used (%ld%%)\n"), (get_nzones() - free
),
1358 100 * (get_nzones() - free
) / get_nzones());
1359 printf(_("\n%6d regular files\n"
1361 "%6d character device files\n"
1362 "%6d block device files\n"
1364 "%6d symbolic links\n"
1367 regular
, directory
, chardev
, blockdev
,
1368 links
- 2 * directory
+ 1, symlinks
,
1369 total
- 2 * directory
+ 1);
1373 printf(_("----------------------------\n"
1374 "FILE SYSTEM HAS BEEN CHANGED\n"
1375 "----------------------------\n"));
1376 for (count
= 0; count
< 3; count
++)
1379 write_super_block();
1381 if (repair
&& !automatic
)
1382 tcsetattr(0, TCSANOW
, &termios
);
1386 if (errors_uncorrected
)