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 rudimentary 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).
95 #include "exitcodes.h"
96 #include "minix_programs.h"
98 #include "pathnames.h"
100 #include "ismounted.h"
102 #include "closestream.h"
104 #include "strutils.h"
107 #define YESNO_LENGTH 64
109 /* Global variables used in minix_programs.h inline functions */
111 char *super_block_buffer
;
113 static char *inode_buffer
;
115 #define Inode (((struct minix_inode *) inode_buffer) - 1)
116 #define Inode2 (((struct minix2_inode *) inode_buffer) - 1)
118 static char *device_name
;
119 static int device_fd
;
120 static int repair
, automatic
, verbose
, list
, show
, warn_mode
, force
;
121 static int directory
, regular
, blockdev
, chardev
, links
, symlinks
, total
;
123 static int changed
; /* flags if the filesystem has been changed */
124 static int errors_uncorrected
; /* flag if some error was not corrected */
125 static size_t dirsize
= 16;
126 static size_t namelen
= 14;
127 static struct termios termios
;
128 static volatile sig_atomic_t termios_set
;
132 static int name_depth
;
133 static char name_list
[MAX_DEPTH
][MINIX_NAME_MAX
+ 1];
135 /* Copy of the previous, just for error reporting - see get_current_name. This
136 * is a waste of 12kB or so. */
137 static char current_name
[MAX_DEPTH
* (MINIX_NAME_MAX
+ 1) + 1];
139 static unsigned char *inode_count
= NULL
;
140 static unsigned char *zone_count
= NULL
;
142 static void recursive_check(unsigned int ino
);
143 static void recursive_check2(unsigned int ino
);
145 static char *inode_map
;
146 static char *zone_map
;
148 #define inode_in_use(x) (isset(inode_map,(x)) != 0)
149 #define zone_in_use(x) (isset(zone_map,(x)-get_first_zone()+1) != 0)
151 #define mark_inode(x) (setbit(inode_map,(x)),changed=1)
152 #define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
154 #define mark_zone(x) (setbit(zone_map,(x)-get_first_zone()+1),changed=1)
155 #define unmark_zone(x) (clrbit(zone_map,(x)-get_first_zone()+1),changed=1)
160 tcsetattr(STDIN_FILENO
, TCSANOW
, &termios
);
165 /* We received a fatal signal. Reset the terminal. Also reset the
166 * signal handler and re-send the signal, so that the parent process
167 * knows which signal actually caused our death. */
168 signal(sig
, SIG_DFL
);
173 static void __attribute__((__noreturn__
))
179 static void __attribute__((__noreturn__
))
182 fputs(USAGE_HEADER
, out
);
183 fprintf(out
, _(" %s [options] <device>\n"), program_invocation_short_name
);
184 fputs(USAGE_SEPARATOR
, out
);
185 fputs(_("Check the consistency of a Minix filesystem.\n"), out
);
186 fputs(USAGE_OPTIONS
, out
);
187 fputs(_(" -l, --list list all filenames\n"), out
);
188 fputs(_(" -a, --auto automatic repair\n"), out
);
189 fputs(_(" -r, --repair interactive repair\n"), out
);
190 fputs(_(" -v, --verbose be verbose\n"), out
);
191 fputs(_(" -s, --super output super-block information\n"), out
);
192 fputs(_(" -m, --uncleared activate mode not cleared warnings\n"), out
);
193 fputs(_(" -f, --force force check\n"), out
);
194 fputs(USAGE_SEPARATOR
, out
);
195 printf(USAGE_HELP_OPTIONS(18));
196 printf(USAGE_MAN_TAIL("fsck.minix(8)"));
200 static void die(const char *fmt
, ...)
201 __attribute__ ((__format__(__printf__
, 1, 2)));
204 die(const char *fmt
, ...) {
207 fprintf(stderr
, UTIL_LINUX_VERSION
);
209 vfprintf(stderr
, fmt
, ap
);
212 leave(FSCK_EX_ERROR
);
215 /* This simply goes through the file-name data and prints out the current file. */
217 get_current_name(void) {
222 while (i
< name_depth
) {
235 ask(const char *string
, int def
) {
237 char input
[YESNO_LENGTH
];
241 errors_uncorrected
= 1;
247 errors_uncorrected
= 1;
250 /* TRANSLATORS: these yes no questions uses rpmatch(), and should be
252 printf(def
? _("%s (y/n)? ") : _("%s (n/y)? "), string
);
254 ignore_result( fgets(input
, YESNO_LENGTH
, stdin
) );
255 resp
= rpmatch(input
);
257 case RPMATCH_INVALID
:
272 errors_uncorrected
= 1;
277 /* Make certain that we aren't checking a filesystem that is on a mounted
278 * partition. Code adapted from e2fsck, Copyright (C) 1993, 1994 Theodore
279 * Ts'o. Also licensed under GPL. */
284 if (!is_mounted(device_name
))
287 printf(_("%s is mounted. "), device_name
);
288 if (isatty(STDIN_FILENO
) && isatty(STDOUT_FILENO
))
289 cont
= ask(_("Do you really want to continue"), 0);
293 printf(_("check aborted.\n"));
299 static int is_valid_zone_nr(unsigned short nr
)
301 if (nr
< get_first_zone())
303 if (nr
>= get_nzones())
308 /* check_zone_nr checks to see that *nr is a valid zone nr. If it isn't, it
309 * will possibly be repaired. Check_zone_nr sets *corrected if an error was
310 * corrected, and returns the zone (0 for no zone or a bad zone-number). */
312 check_zone_nr(unsigned short *nr
, int *corrected
) {
316 if (*nr
< get_first_zone()) {
318 printf(_("Zone nr < FIRSTZONE in file `%s'."), current_name
);
319 } else if (*nr
>= get_nzones()) {
321 printf(_("Zone nr >= ZONES in file `%s'."), current_name
);
325 if (ask(_("Remove block"), 1)) {
333 check_zone_nr2(unsigned int *nr
, int *corrected
) {
337 if (*nr
< get_first_zone()) {
339 printf(_("Zone nr < FIRSTZONE in file `%s'."), current_name
);
340 } else if (*nr
>= get_nzones()) {
342 printf(_("Zone nr >= ZONES in file `%s'."), current_name
);
346 if (ask(_("Remove block"), 1)) {
353 /* read-block reads block nr into the buffer at addr. */
355 read_block(unsigned int nr
, char *addr
) {
357 memset(addr
, 0, MINIX_BLOCK_SIZE
);
360 if (MINIX_BLOCK_SIZE
* nr
!= lseek(device_fd
, MINIX_BLOCK_SIZE
* nr
, SEEK_SET
)) {
362 printf(_("Read error: unable to seek to block in file '%s'\n"),
364 memset(addr
, 0, MINIX_BLOCK_SIZE
);
365 errors_uncorrected
= 1;
366 } else if (MINIX_BLOCK_SIZE
!= read(device_fd
, addr
, MINIX_BLOCK_SIZE
)) {
368 printf(_("Read error: bad block in file '%s'\n"), current_name
);
369 memset(addr
, 0, MINIX_BLOCK_SIZE
);
370 errors_uncorrected
= 1;
374 /* write_block writes block nr to disk. */
376 write_block(unsigned int nr
, char *addr
) {
379 if (nr
< get_first_zone() || nr
>= get_nzones()) {
380 printf(_("Internal error: trying to write bad block\n"
381 "Write request ignored\n"));
382 errors_uncorrected
= 1;
385 if (MINIX_BLOCK_SIZE
* nr
!= lseek(device_fd
, MINIX_BLOCK_SIZE
* nr
, SEEK_SET
))
386 die(_("seek failed in write_block"));
387 if (MINIX_BLOCK_SIZE
!= write(device_fd
, addr
, MINIX_BLOCK_SIZE
)) {
389 printf(_("Write error: bad block in file '%s'\n"),
391 errors_uncorrected
= 1;
395 /* map-block calculates the absolute block nr of a block in a file. It sets
396 * 'changed' if the inode has needed changing, and re-writes any indirect
397 * blocks with errors. */
399 map_block(struct minix_inode
*inode
, unsigned int blknr
) {
400 unsigned short ind
[MINIX_BLOCK_SIZE
>> 1];
401 unsigned short dind
[MINIX_BLOCK_SIZE
>> 1];
402 int blk_chg
, block
, result
;
406 return check_zone_nr(inode
->i_zone
+ blknr
, &changed
);
409 block
= check_zone_nr(inode
->i_zone
+ 7, &changed
);
410 read_block(block
, (char *)ind
);
412 result
= check_zone_nr(blknr
+ ind
, &blk_chg
);
414 write_block(block
, (char *)ind
);
418 block
= check_zone_nr(inode
->i_zone
+ 8, &changed
);
419 read_block(block
, (char *)dind
);
422 if (ARRAY_SIZE(dind
) <= range
) {
423 printf(_("Warning: block out of range\n"));
426 result
= check_zone_nr(dind
+ range
, &blk_chg
);
428 write_block(block
, (char *)dind
);
430 read_block(block
, (char *)ind
);
432 result
= check_zone_nr(ind
+ (blknr
% 512), &blk_chg
);
434 write_block(block
, (char *)ind
);
439 map_block2(struct minix2_inode
*inode
, unsigned int blknr
) {
440 unsigned int ind
[MINIX_BLOCK_SIZE
>> 2];
441 unsigned int dind
[MINIX_BLOCK_SIZE
>> 2];
442 unsigned int tind
[MINIX_BLOCK_SIZE
>> 2];
443 int blk_chg
, block
, result
;
446 return check_zone_nr2(inode
->i_zone
+ blknr
, &changed
);
449 block
= check_zone_nr2(inode
->i_zone
+ 7, &changed
);
450 read_block(block
, (char *)ind
);
452 result
= check_zone_nr2(blknr
+ ind
, &blk_chg
);
454 write_block(block
, (char *)ind
);
458 if (blknr
< 256 * 256) {
459 block
= check_zone_nr2(inode
->i_zone
+ 8, &changed
);
460 read_block(block
, (char *)dind
);
462 result
= check_zone_nr2(dind
+ blknr
/ 256, &blk_chg
);
464 write_block(block
, (char *)dind
);
466 read_block(block
, (char *)ind
);
468 result
= check_zone_nr2(ind
+ blknr
% 256, &blk_chg
);
470 write_block(block
, (char *)ind
);
474 block
= check_zone_nr2(inode
->i_zone
+ 9, &changed
);
475 read_block(block
, (char *)tind
);
477 result
= check_zone_nr2(tind
+ blknr
/ (256 * 256), &blk_chg
);
479 write_block(block
, (char *)tind
);
481 read_block(block
, (char *)dind
);
483 result
= check_zone_nr2(dind
+ (blknr
/ 256) % 256, &blk_chg
);
485 write_block(block
, (char *)dind
);
487 read_block(block
, (char *)ind
);
489 result
= check_zone_nr2(ind
+ blknr
% 256, &blk_chg
);
491 write_block(block
, (char *)ind
);
496 write_super_block(void) {
497 /* v3 super block does not track state */
500 /* Set the state of the filesystem based on whether or not there are
501 * uncorrected errors. The filesystem valid flag is unconditionally
502 * set if we get this far. */
503 Super
.s_state
|= MINIX_VALID_FS
;
504 if (errors_uncorrected
)
505 Super
.s_state
|= MINIX_ERROR_FS
;
507 Super
.s_state
&= ~MINIX_ERROR_FS
;
509 if (MINIX_BLOCK_SIZE
!= lseek(device_fd
, MINIX_BLOCK_SIZE
, SEEK_SET
))
510 die(_("seek failed in write_super_block"));
511 if (MINIX_BLOCK_SIZE
!= write(device_fd
, super_block_buffer
, MINIX_BLOCK_SIZE
))
512 die(_("unable to write super-block"));
517 unsigned long buffsz
= get_inode_buffer_size();
518 unsigned long imaps
= get_nimaps();
519 unsigned long zmaps
= get_nzmaps();
523 if (write_all(device_fd
, inode_map
, imaps
* MINIX_BLOCK_SIZE
))
524 die(_("Unable to write inode map"));
526 if (write_all(device_fd
, zone_map
, zmaps
* MINIX_BLOCK_SIZE
))
527 die(_("Unable to write zone map"));
529 if (write_all(device_fd
, inode_buffer
, buffsz
))
530 die(_("Unable to write inodes"));
536 char blk
[MINIX_BLOCK_SIZE
];
539 if (fs_version
== 2 || fs_version
== 3)
540 block
= Inode2
[ROOT_INO
].i_zone
[0];
542 block
= Inode
[ROOT_INO
].i_zone
[0];
543 read_block(block
, blk
);
545 for (size
= 16; size
< MINIX_BLOCK_SIZE
; size
<<= 1) {
546 if (strcmp(blk
+ size
+ 2, "..") == 0) {
556 read_superblock(void) {
557 if (MINIX_BLOCK_SIZE
!= lseek(device_fd
, MINIX_BLOCK_SIZE
, SEEK_SET
))
558 die(_("seek failed"));
560 super_block_buffer
= calloc(1, MINIX_BLOCK_SIZE
);
561 if (!super_block_buffer
)
562 die(_("unable to alloc buffer for superblock"));
564 if (MINIX_BLOCK_SIZE
!= read(device_fd
, super_block_buffer
, MINIX_BLOCK_SIZE
))
565 die(_("unable to read super block"));
566 if (Super
.s_magic
== MINIX_SUPER_MAGIC
) {
570 } else if (Super
.s_magic
== MINIX_SUPER_MAGIC2
) {
574 } else if (Super
.s_magic
== MINIX2_SUPER_MAGIC
) {
578 } else if (Super
.s_magic
== MINIX2_SUPER_MAGIC2
) {
582 } else if (Super3
.s_magic
== MINIX3_SUPER_MAGIC
) {
587 die(_("bad magic number in super-block"));
588 if (get_zone_size() != 0 || MINIX_BLOCK_SIZE
!= 1024)
589 die(_("Only 1k blocks/zones supported"));
590 if (get_ninodes() == 0 || get_ninodes() == UINT32_MAX
)
591 die(_("bad s_ninodes field in super-block"));
592 if (get_nimaps() * MINIX_BLOCK_SIZE
* 8 < get_ninodes() + 1)
593 die(_("bad s_imap_blocks field in super-block"));
594 if (get_first_zone() > (off_t
) get_nzones())
595 die(_("bad s_firstdatazone field in super-block"));
596 if (get_nzmaps() * MINIX_BLOCK_SIZE
* 8 <
597 get_nzones() - get_first_zone() + 1)
598 die(_("bad s_zmap_blocks field in super-block"));
603 unsigned long inodes
= get_ninodes();
604 size_t buffsz
= get_inode_buffer_size();
605 off_t norm_first_zone
= first_zone_data();
606 off_t first_zone
= get_first_zone();
607 unsigned long zones
= get_nzones();
608 unsigned long imaps
= get_nimaps();
609 unsigned long zmaps
= get_nzmaps();
612 inode_map
= malloc(imaps
* MINIX_BLOCK_SIZE
);
614 die(_("Unable to allocate buffer for inode map"));
615 zone_map
= malloc(zmaps
* MINIX_BLOCK_SIZE
);
617 die(_("Unable to allocate buffer for zone map"));
618 inode_buffer
= malloc(buffsz
);
620 die(_("Unable to allocate buffer for inodes"));
621 inode_count
= calloc(1, inodes
+ 1);
623 die(_("Unable to allocate buffer for inode count"));
624 zone_count
= calloc(1, zones
);
626 die(_("Unable to allocate buffer for zone count"));
628 rc
= read(device_fd
, inode_map
, imaps
* MINIX_BLOCK_SIZE
);
629 if (rc
< 0 || imaps
* MINIX_BLOCK_SIZE
!= (size_t) rc
)
630 die(_("Unable to read inode map"));
632 rc
= read(device_fd
, zone_map
, zmaps
* MINIX_BLOCK_SIZE
);
633 if (rc
< 0 || zmaps
* MINIX_BLOCK_SIZE
!= (size_t) rc
)
634 die(_("Unable to read zone map"));
636 rc
= read(device_fd
, inode_buffer
, buffsz
);
637 if (rc
< 0 || buffsz
!= (size_t) rc
)
638 die(_("Unable to read inodes"));
639 if (norm_first_zone
!= first_zone
) {
640 printf(_("Warning: Firstzone != Norm_firstzone\n"));
641 errors_uncorrected
= 1;
645 printf(_("%ld inodes\n"), inodes
);
646 printf(_("%ld blocks\n"), zones
);
647 printf(_("Firstdatazone=%jd (%jd)\n"),
648 (intmax_t)first_zone
, (intmax_t)norm_first_zone
);
649 printf(_("Zonesize=%d\n"), MINIX_BLOCK_SIZE
<< get_zone_size());
650 printf(_("Maxsize=%zu\n"), get_max_size());
652 printf(_("Filesystem state=%d\n"), Super
.s_state
);
653 printf(_("namelen=%zd\n\n"), namelen
);
657 static struct minix_inode
*
658 get_inode(unsigned int nr
) {
659 struct minix_inode
*inode
;
661 if (!nr
|| nr
> get_ninodes())
665 if (!inode_count
[nr
]) {
666 if (!inode_in_use(nr
)) {
668 printf(_("Inode %d marked unused, "
669 "but used for file '%s'\n"), nr
, current_name
);
671 if (ask(_("Mark in use"), 1))
674 errors_uncorrected
= 1;
677 if (S_ISDIR(inode
->i_mode
))
679 else if (S_ISREG(inode
->i_mode
))
681 else if (S_ISCHR(inode
->i_mode
))
683 else if (S_ISBLK(inode
->i_mode
))
685 else if (S_ISLNK(inode
->i_mode
))
687 else if (S_ISSOCK(inode
->i_mode
))
689 else if (S_ISFIFO(inode
->i_mode
))
693 printf(_("The file `%s' has mode %05o\n"),
694 current_name
, inode
->i_mode
);
699 if (!++inode_count
[nr
]) {
700 printf(_("Warning: inode count too big.\n"));
702 errors_uncorrected
= 1;
707 static struct minix2_inode
*
708 get_inode2(unsigned int nr
) {
709 struct minix2_inode
*inode
;
711 if (!nr
|| nr
> get_ninodes())
715 if (!inode_count
[nr
]) {
716 if (!inode_in_use(nr
)) {
718 printf(_("Inode %d marked unused, "
719 "but used for file '%s'\n"), nr
, current_name
);
721 if (ask(_("Mark in use"), 1))
724 errors_uncorrected
= 1;
727 if (S_ISDIR(inode
->i_mode
))
729 else if (S_ISREG(inode
->i_mode
))
731 else if (S_ISCHR(inode
->i_mode
))
733 else if (S_ISBLK(inode
->i_mode
))
735 else if (S_ISLNK(inode
->i_mode
))
737 else if (S_ISSOCK(inode
->i_mode
)) ;
738 else if (S_ISFIFO(inode
->i_mode
)) ;
741 printf(_("The file `%s' has mode %05o\n"),
742 current_name
, inode
->i_mode
);
746 if (!++inode_count
[nr
]) {
747 printf(_("Warning: inode count too big.\n"));
749 errors_uncorrected
= 1;
756 struct minix_inode
*inode
= Inode
+ ROOT_INO
;
758 if (!inode
|| !S_ISDIR(inode
->i_mode
))
759 die(_("root inode isn't a directory"));
764 struct minix2_inode
*inode
= Inode2
+ ROOT_INO
;
766 if (!inode
|| !S_ISDIR(inode
->i_mode
))
767 die(_("root inode isn't a directory"));
771 add_zone(unsigned short *znr
, int *corrected
) {
774 block
= check_zone_nr(znr
, corrected
);
777 if (zone_count
[block
]) {
779 printf(_("Block has been used before. Now in file `%s'."),
781 if (ask(_("Clear"), 1)) {
789 if (!zone_in_use(block
)) {
791 printf(_("Block %d in file `%s' is marked not in use."),
792 block
, current_name
);
793 if (ask(_("Correct"), 1))
796 if (!++zone_count
[block
])
802 add_zone2(unsigned int *znr
, int *corrected
) {
805 block
= check_zone_nr2(znr
, corrected
);
808 if (zone_count
[block
]) {
810 printf(_("Block has been used before. Now in file `%s'."),
812 if (ask(_("Clear"), 1)) {
820 if (!zone_in_use(block
)) {
822 printf(_("Block %d in file `%s' is marked not in use."),
823 block
, current_name
);
824 if (ask(_("Correct"), 1))
827 if (!++zone_count
[block
])
833 add_zone_ind(unsigned short *znr
, int *corrected
) {
834 static char blk
[MINIX_BLOCK_SIZE
];
838 block
= add_zone(znr
, corrected
);
841 read_block(block
, blk
);
842 for (i
= 0; i
< (MINIX_BLOCK_SIZE
>> 1); i
++)
843 add_zone(i
+ (unsigned short *)blk
, &chg_blk
);
845 write_block(block
, blk
);
849 add_zone_ind2(unsigned int *znr
, int *corrected
) {
850 static char blk
[MINIX_BLOCK_SIZE
];
854 block
= add_zone2(znr
, corrected
);
857 read_block(block
, blk
);
858 for (i
= 0; i
< MINIX_BLOCK_SIZE
>> 2; i
++)
859 add_zone2(i
+ (unsigned int *)blk
, &chg_blk
);
861 write_block(block
, blk
);
865 add_zone_dind(unsigned short *znr
, int *corrected
) {
866 static char blk
[MINIX_BLOCK_SIZE
];
870 block
= add_zone(znr
, corrected
);
873 read_block(block
, blk
);
874 for (i
= 0; i
< (MINIX_BLOCK_SIZE
>> 1); i
++)
875 add_zone_ind(i
+ (unsigned short *)blk
, &blk_chg
);
877 write_block(block
, blk
);
881 add_zone_dind2(unsigned int *znr
, int *corrected
) {
882 static char blk
[MINIX_BLOCK_SIZE
];
886 block
= add_zone2(znr
, corrected
);
889 read_block(block
, blk
);
890 for (i
= 0; i
< MINIX_BLOCK_SIZE
>> 2; i
++)
891 add_zone_ind2(i
+ (unsigned int *)blk
, &blk_chg
);
893 write_block(block
, blk
);
897 add_zone_tind2(unsigned int *znr
, int *corrected
) {
898 static char blk
[MINIX_BLOCK_SIZE
];
902 block
= add_zone2(znr
, corrected
);
905 read_block(block
, blk
);
906 for (i
= 0; i
< MINIX_BLOCK_SIZE
>> 2; i
++)
907 add_zone_dind2(i
+ (unsigned int *)blk
, &blk_chg
);
909 write_block(block
, blk
);
913 check_zones(unsigned int i
) {
914 struct minix_inode
*inode
;
916 if (!i
|| i
> get_ninodes())
918 if (inode_count
[i
] > 1) /* have we counted this file already? */
921 if (!S_ISDIR(inode
->i_mode
) && !S_ISREG(inode
->i_mode
) &&
922 !S_ISLNK(inode
->i_mode
))
924 for (i
= 0; i
< 7; i
++)
925 add_zone(i
+ inode
->i_zone
, &changed
);
926 add_zone_ind(7 + inode
->i_zone
, &changed
);
927 add_zone_dind(8 + inode
->i_zone
, &changed
);
931 check_zones2(unsigned int i
) {
932 struct minix2_inode
*inode
;
934 if (!i
|| i
> get_ninodes())
936 if (inode_count
[i
] > 1) /* have we counted this file already? */
939 if (!S_ISDIR(inode
->i_mode
) && !S_ISREG(inode
->i_mode
)
940 && !S_ISLNK(inode
->i_mode
))
942 for (i
= 0; i
< 7; i
++)
943 add_zone2(i
+ inode
->i_zone
, &changed
);
944 add_zone_ind2(7 + inode
->i_zone
, &changed
);
945 add_zone_dind2(8 + inode
->i_zone
, &changed
);
946 add_zone_tind2(9 + inode
->i_zone
, &changed
);
950 check_file(struct minix_inode
*dir
, unsigned int offset
) {
951 static char blk
[MINIX_BLOCK_SIZE
+ 2];
952 struct minix_inode
*inode
;
957 block
= map_block(dir
, offset
/ MINIX_BLOCK_SIZE
);
958 read_block(block
, blk
);
959 name
= blk
+ (offset
% MINIX_BLOCK_SIZE
) + 2;
960 ino
= *(unsigned short *)(name
- 2);
961 if (ino
> get_ninodes()) {
963 printf(_("The directory '%s' contains a bad inode number "
964 "for file '%.*s'."), current_name
, (int)namelen
, name
);
965 if (ask(_(" Remove"), 1)) {
966 *(unsigned short *)(name
- 2) = 0;
967 write_block(block
, blk
);
971 if (name_depth
< MAX_DEPTH
)
972 xstrncpy(name_list
[name_depth
], name
, namelen
);
976 inode
= get_inode(ino
);
979 if (!inode
|| strcmp(".", name
) != 0) {
981 printf(_("%s: bad directory: '.' isn't first\n"),
983 errors_uncorrected
= 1;
987 if (offset
== dirsize
) {
988 if (!inode
|| strcmp("..", name
) != 0) {
990 printf(_("%s: bad directory: '..' isn't second\n"),
992 errors_uncorrected
= 1;
998 if (name_depth
< MAX_DEPTH
)
999 xstrncpy(name_list
[name_depth
], name
, namelen
);
1005 printf("%6d %07o %3d ", ino
,
1006 inode
->i_mode
, inode
->i_nlinks
);
1008 printf("%s", current_name
);
1009 if (S_ISDIR(inode
->i_mode
))
1015 if (inode
&& S_ISDIR(inode
->i_mode
))
1016 recursive_check(ino
);
1021 check_file2(struct minix2_inode
*dir
, unsigned int offset
) {
1022 static char blk
[MINIX_BLOCK_SIZE
+ 4];
1023 struct minix2_inode
*inode
;
1027 const int version_offset
= fs_version
== 3 ? 4 : 2;
1029 block
= map_block2(dir
, offset
/ MINIX_BLOCK_SIZE
);
1030 read_block(block
, blk
);
1031 name
= blk
+ (offset
% MINIX_BLOCK_SIZE
) + version_offset
;
1032 ino
= version_offset
== 4 ? *(uint32_t *)(name
- version_offset
)
1033 : *(uint16_t *)(name
- version_offset
);
1034 if (ino
> get_ninodes()) {
1036 printf(_("The directory '%s' contains a bad inode number "
1037 "for file '%.*s'."), current_name
, (int)namelen
, name
);
1038 if (ask(_(" Remove"), 1)) {
1039 memset(name
- version_offset
, 0, version_offset
);
1040 write_block(block
, blk
);
1044 if (name_depth
< MAX_DEPTH
)
1045 xstrncpy(name_list
[name_depth
], name
, namelen
);
1049 inode
= get_inode2(ino
);
1052 if (!inode
|| strcmp(".", name
) != 0) {
1054 printf(_("%s: bad directory: '.' isn't first\n"),
1056 errors_uncorrected
= 1;
1060 if (offset
== dirsize
) {
1061 if (!inode
|| strcmp("..", name
) != 0) {
1063 printf(_("%s: bad directory: '..' isn't second\n"),
1065 errors_uncorrected
= 1;
1074 printf("%6ju %07o %3d ", (uintmax_t)ino
, inode
->i_mode
,
1077 printf("%s", current_name
);
1078 if (S_ISDIR(inode
->i_mode
))
1084 if (inode
&& S_ISDIR(inode
->i_mode
))
1085 recursive_check2(ino
);
1090 recursive_check(unsigned int ino
) {
1091 struct minix_inode
*dir
;
1095 if (!S_ISDIR(dir
->i_mode
))
1096 die(_("internal error"));
1097 if (dir
->i_size
< 2 * dirsize
) {
1099 printf(_("%s: bad directory: size < 32"), current_name
);
1100 errors_uncorrected
= 1;
1103 if ((!repair
|| automatic
) && !is_valid_zone_nr(*dir
->i_zone
)) {
1105 printf(_("%s: bad directory: invalid i_zone, use --repair to fix\n"), current_name
);
1108 for (offset
= 0; offset
< dir
->i_size
; offset
+= dirsize
)
1109 check_file(dir
, offset
);
1113 recursive_check2(unsigned int ino
) {
1114 struct minix2_inode
*dir
;
1118 if (!S_ISDIR(dir
->i_mode
))
1119 die(_("internal error"));
1120 if (dir
->i_size
< 2 * dirsize
) {
1122 printf(_("%s: bad directory: size < 32"), current_name
);
1123 errors_uncorrected
= 1;
1125 for (offset
= 0; offset
< dir
->i_size
; offset
+= dirsize
)
1126 check_file2(dir
, offset
);
1133 if (MINIX_BLOCK_SIZE
* i
!= lseek(device_fd
, MINIX_BLOCK_SIZE
* i
, SEEK_SET
))
1134 die(_("seek failed in bad_zone"));
1135 return (MINIX_BLOCK_SIZE
!= read(device_fd
, buffer
, MINIX_BLOCK_SIZE
));
1139 check_counts(void) {
1142 for (i
= 1; i
<= get_ninodes(); i
++) {
1143 if (!inode_in_use(i
) && Inode
[i
].i_mode
&& warn_mode
) {
1144 printf(_("Inode %lu mode not cleared."), i
);
1145 if (ask(_("Clear"), 1)) {
1146 Inode
[i
].i_mode
= 0;
1150 if (!inode_count
[i
]) {
1151 if (!inode_in_use(i
))
1153 printf(_("Inode %lu not used, marked used in the bitmap."), i
);
1154 if (ask(_("Clear"), 1))
1158 if (!inode_in_use(i
)) {
1159 printf(_("Inode %lu used, marked unused in the bitmap."), i
);
1160 if (ask(_("Set"), 1))
1163 if (Inode
[i
].i_nlinks
!= inode_count
[i
]) {
1164 printf(_("Inode %lu (mode = %07o), i_nlinks=%d, counted=%d."),
1165 i
, Inode
[i
].i_mode
, Inode
[i
].i_nlinks
,
1167 if (ask(_("Set i_nlinks to count"), 1)) {
1168 Inode
[i
].i_nlinks
= inode_count
[i
];
1173 for (i
= get_first_zone(); i
< get_nzones(); i
++) {
1174 if (zone_in_use(i
) == zone_count
[i
])
1176 if (!zone_count
[i
]) {
1179 printf(_("Zone %lu: marked in use, no file uses it."),
1181 if (ask(_("Unmark"), 1))
1186 printf(_("Zone %lu: in use, counted=%d\n"),
1189 printf(_("Zone %lu: not in use, counted=%d\n"),
1195 check_counts2(void) {
1198 for (i
= 1; i
<= get_ninodes(); i
++) {
1199 if (!inode_in_use(i
) && Inode2
[i
].i_mode
&& warn_mode
) {
1200 printf(_("Inode %lu mode not cleared."), i
);
1201 if (ask(_("Clear"), 1)) {
1202 Inode2
[i
].i_mode
= 0;
1206 if (!inode_count
[i
]) {
1207 if (!inode_in_use(i
))
1209 printf(_("Inode %lu not used, marked used in the bitmap."), i
);
1210 if (ask(_("Clear"), 1))
1214 if (!inode_in_use(i
)) {
1215 printf(_("Inode %lu used, marked unused in the bitmap."), i
);
1216 if (ask(_("Set"), 1))
1219 if (Inode2
[i
].i_nlinks
!= inode_count
[i
]) {
1220 printf(_("Inode %lu (mode = %07o), i_nlinks=%d, counted=%d."),
1221 i
, Inode2
[i
].i_mode
, Inode2
[i
].i_nlinks
,
1223 if (ask(_("Set i_nlinks to count"), 1)) {
1224 Inode2
[i
].i_nlinks
= inode_count
[i
];
1229 for (i
= get_first_zone(); i
< get_nzones(); i
++) {
1230 if (zone_in_use(i
) == zone_count
[i
])
1232 if (!zone_count
[i
]) {
1235 printf(_("Zone %lu: marked in use, no file uses it."),
1237 if (ask(_("Unmark"), 1))
1242 printf(_("Zone %lu: in use, counted=%d\n"),
1245 printf(_("Zone %lu: not in use, counted=%d\n"),
1252 memset(inode_count
, 0, (get_ninodes() + 1) * sizeof(*inode_count
));
1253 memset(zone_count
, 0, get_nzones() * sizeof(*zone_count
));
1254 check_zones(ROOT_INO
);
1255 recursive_check(ROOT_INO
);
1261 memset(inode_count
, 0, (get_ninodes() + 1) * sizeof(*inode_count
));
1262 memset(zone_count
, 0, get_nzones() * sizeof(*zone_count
));
1263 check_zones2(ROOT_INO
);
1264 recursive_check2(ROOT_INO
);
1269 main(int argc
, char **argv
) {
1272 int retcode
= FSCK_EX_OK
;
1274 static const struct option longopts
[] = {
1275 {"list", no_argument
, NULL
, 'l'},
1276 {"auto", no_argument
, NULL
, 'a'},
1277 {"repair", no_argument
, NULL
, 'r'},
1278 {"verbose", no_argument
, NULL
, 'v'},
1279 {"super", no_argument
, NULL
, 's'},
1280 {"uncleared", no_argument
, NULL
, 'm'},
1281 {"force", no_argument
, NULL
, 'f'},
1282 {"version", no_argument
, NULL
, 'V'},
1283 {"help", no_argument
, NULL
, 'h'},
1287 setlocale(LC_ALL
, "");
1288 bindtextdomain(PACKAGE
, LOCALEDIR
);
1289 textdomain(PACKAGE
);
1290 close_stdout_atexit();
1292 strutils_set_exitcode(FSCK_EX_USAGE
);
1294 if (INODE_SIZE
* MINIX_INODES_PER_BLOCK
!= MINIX_BLOCK_SIZE
)
1295 die(_("bad inode size"));
1296 if (INODE2_SIZE
* MINIX2_INODES_PER_BLOCK
!= MINIX_BLOCK_SIZE
)
1297 die(_("bad v2 inode size"));
1299 while ((i
= getopt_long(argc
, argv
, "larvsmfVh", longopts
, NULL
)) != -1)
1325 print_version(FSCK_EX_OK
);
1329 errtryhelp(FSCK_EX_USAGE
);
1334 device_name
= argv
[0];
1336 warnx(_("no device specified"));
1337 errtryhelp(FSCK_EX_USAGE
);
1339 check_mount(); /* trying to check a mounted filesystem? */
1340 if (repair
&& !automatic
&& (!isatty(STDIN_FILENO
) || !isatty(STDOUT_FILENO
)))
1341 die(_("need terminal for interactive repairs"));
1343 device_fd
= open(device_name
, repair
? O_RDWR
: O_RDONLY
);
1345 die(_("cannot open %s: %s"), device_name
, strerror(errno
));
1346 for (count
= 0; count
< 3; count
++)
1350 /* Determine whether or not we should continue with the checking. This
1351 * is based on the status of the filesystem valid and error flags and
1352 * whether or not the -f switch was specified on the command line. */
1353 if (fs_version
< 3 && !(Super
.s_state
& MINIX_ERROR_FS
) &&
1354 (Super
.s_state
& MINIX_VALID_FS
) && !force
) {
1356 printf(_("%s is clean, no check.\n"), device_name
);
1361 printf(_("Forcing filesystem check on %s.\n"), device_name
);
1363 printf(_("Filesystem on %s is dirty, needs checking.\n"),
1368 /* Restore the terminal state on fatal signals. We don't do this for
1369 * SIGALRM, SIGUSR1 or SIGUSR2. */
1370 signal(SIGINT
, fatalsig
);
1371 signal(SIGQUIT
, fatalsig
);
1372 signal(SIGTERM
, fatalsig
);
1374 if (repair
&& !automatic
) {
1375 tcgetattr(STDIN_FILENO
, &termios
);
1377 tmp
.c_lflag
&= ~(ICANON
| ECHO
);
1378 tcsetattr(STDIN_FILENO
, TCSANOW
, &tmp
);
1382 if (fs_version
== 2 || fs_version
== 3) {
1390 unsigned long inode
, free
;
1392 for (inode
= 1, free
= 0; inode
<= get_ninodes(); inode
++)
1393 if (!inode_in_use(inode
))
1395 printf(_("\n%6ld inodes used (%ld%%)\n"),
1396 (get_ninodes() - free
),
1397 100 * (get_ninodes() - free
) / get_ninodes());
1398 for (inode
= get_first_zone(), free
= 0; inode
< get_nzones(); inode
++)
1399 if (!zone_in_use(inode
))
1401 printf(_("%6ld zones used (%ld%%)\n"), (get_nzones() - free
),
1402 100 * (get_nzones() - free
) / get_nzones());
1403 printf(_("\n%6d regular files\n"
1405 "%6d character device files\n"
1406 "%6d block device files\n"
1408 "%6d symbolic links\n"
1411 regular
, directory
, chardev
, blockdev
,
1412 links
- 2 * directory
+ 1, symlinks
,
1413 total
- 2 * directory
+ 1);
1417 printf(_("----------------------------\n"
1418 "FILE SYSTEM HAS BEEN CHANGED\n"
1419 "----------------------------\n"));
1420 for (count
= 0; count
< 3; count
++)
1423 write_super_block();
1425 if (repair
&& !automatic
)
1426 tcsetattr(STDIN_FILENO
, TCSANOW
, &termios
);
1428 if (close_fd(device_fd
) != 0)
1429 err(FSCK_EX_ERROR
, _("write failed"));
1432 if (errors_uncorrected
)