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 source tree.
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 * superblock 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 Mickiewicz <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 superblock 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 :-).
90 //usage:#define fsck_minix_trivial_usage
91 //usage: "[-larvsmf] BLOCKDEV"
92 //usage:#define fsck_minix_full_usage "\n\n"
93 //usage: "Check MINIX filesystem\n"
94 //usage: "\n -l List all filenames"
95 //usage: "\n -r Perform interactive repairs"
96 //usage: "\n -a Perform automatic repairs"
97 //usage: "\n -v Verbose"
98 //usage: "\n -s Output superblock information"
99 //usage: "\n -m Show \"mode not cleared\" warnings"
100 //usage: "\n -f Force file system check"
107 #define BLKGETSIZE _IO(0x12,96) /* return device size */
110 struct BUG_bad_inode_size
{
111 char BUG_bad_inode1_size
[(INODE_SIZE1
* MINIX1_INODES_PER_BLOCK
!= BLOCK_SIZE
) ? -1 : 1];
112 #if ENABLE_FEATURE_MINIX2
113 char BUG_bad_inode2_size
[(INODE_SIZE2
* MINIX2_INODES_PER_BLOCK
!= BLOCK_SIZE
) ? -1 : 1];
119 MINIX1_LINK_MAX
= 250,
120 MINIX2_LINK_MAX
= 65530,
121 MINIX_I_MAP_SLOTS
= 8,
122 MINIX_Z_MAP_SLOTS
= 64,
123 MINIX_V1
= 0x0001, /* original minix fs */
124 MINIX_V2
= 0x0002, /* minix V2 fs */
126 MINIX_NAME_MAX
= 255, /* # chars in a file name */
130 #if !ENABLE_FEATURE_MINIX2
131 enum { version2
= 0 };
134 enum { MAX_DEPTH
= 32 };
139 #if ENABLE_FEATURE_MINIX2
142 smallint changed
; /* is filesystem modified? */
143 smallint errors_uncorrected
; /* flag if some error was not corrected */
144 smallint termios_set
;
147 const char *device_name
;
148 int directory
, regular
, blockdev
, chardev
, links
, symlinks
, total
;
154 unsigned char *inode_count
;
155 unsigned char *zone_count
;
159 char *name_component
[MAX_DEPTH
+1];
162 struct termios sv_termios
;
163 char superblock_buffer
[BLOCK_SIZE
];
164 char add_zone_ind_blk
[BLOCK_SIZE
];
165 char add_zone_dind_blk
[BLOCK_SIZE
];
166 IF_FEATURE_MINIX2(char add_zone_tind_blk
[BLOCK_SIZE
];)
167 char check_file_blk
[BLOCK_SIZE
];
170 char current_name
[MAX_DEPTH
* MINIX_NAME_MAX
];
172 #define G (*ptr_to_globals)
173 #if ENABLE_FEATURE_MINIX2
174 #define version2 (G.version2 )
176 #define changed (G.changed )
177 #define errors_uncorrected (G.errors_uncorrected )
178 #define termios_set (G.termios_set )
179 #define dirsize (G.dirsize )
180 #define namelen (G.namelen )
181 #define device_name (G.device_name )
182 #define directory (G.directory )
183 #define regular (G.regular )
184 #define blockdev (G.blockdev )
185 #define chardev (G.chardev )
186 #define links (G.links )
187 #define symlinks (G.symlinks )
188 #define total (G.total )
189 #define inode_buffer (G.inode_buffer )
190 #define inode_map (G.inode_map )
191 #define zone_map (G.zone_map )
192 #define inode_count (G.inode_count )
193 #define zone_count (G.zone_count )
194 #define name_depth (G.name_depth )
195 #define name_component (G.name_component )
196 #define sv_termios (G.sv_termios )
197 #define superblock_buffer (G.superblock_buffer )
198 #define add_zone_ind_blk (G.add_zone_ind_blk )
199 #define add_zone_dind_blk (G.add_zone_dind_blk )
200 #define add_zone_tind_blk (G.add_zone_tind_blk )
201 #define check_file_blk (G.check_file_blk )
202 #define current_name (G.current_name )
203 #define INIT_G() do { \
204 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
207 current_name[0] = '/'; \
208 /*current_name[1] = '\0';*/ \
209 name_component[0] = ¤t_name[0]; \
213 #define OPTION_STR "larvsmf"
223 #define OPT_list (option_mask32 & OPT_l)
224 #define OPT_automatic (option_mask32 & OPT_a)
225 #define OPT_repair (option_mask32 & OPT_r)
226 #define OPT_verbose (option_mask32 & OPT_v)
227 #define OPT_show (option_mask32 & OPT_s)
228 #define OPT_warn_mode (option_mask32 & OPT_w)
229 #define OPT_force (option_mask32 & OPT_f)
230 /* non-automatic repairs requested? */
231 #define OPT_manual ((option_mask32 & (OPT_a|OPT_r)) == OPT_r)
234 #define Inode1 (((struct minix1_inode *) inode_buffer)-1)
235 #define Inode2 (((struct minix2_inode *) inode_buffer)-1)
237 #define Super (*(struct minix_superblock *)(superblock_buffer))
239 #if ENABLE_FEATURE_MINIX2
240 # define ZONES ((unsigned)(version2 ? Super.s_zones : Super.s_nzones))
242 # define ZONES ((unsigned)(Super.s_nzones))
244 #define INODES ((unsigned)Super.s_ninodes)
245 #define IMAPS ((unsigned)Super.s_imap_blocks)
246 #define ZMAPS ((unsigned)Super.s_zmap_blocks)
247 #define FIRSTZONE ((unsigned)Super.s_firstdatazone)
248 #define ZONESIZE ((unsigned)Super.s_log_zone_size)
249 #define MAXSIZE ((unsigned)Super.s_max_size)
250 #define MAGIC (Super.s_magic)
252 /* gcc likes this more (code is smaller) than macro variant */
253 static ALWAYS_INLINE
unsigned div_roundup(unsigned size
, unsigned n
)
255 return (size
+ n
-1) / n
;
258 #if !ENABLE_FEATURE_MINIX2
259 #define INODE_BLOCKS div_roundup(INODES, MINIX1_INODES_PER_BLOCK)
261 #define INODE_BLOCKS div_roundup(INODES, \
262 (version2 ? MINIX2_INODES_PER_BLOCK : MINIX1_INODES_PER_BLOCK))
265 #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
266 #define NORM_FIRSTZONE (2 + IMAPS + ZMAPS + INODE_BLOCKS)
268 /* Before you ask "where they come from?": */
269 /* setbit/clrbit are supplied by sys/param.h */
271 static int minix_bit(const char *a
, unsigned i
)
273 return (a
[i
>> 3] & (1<<(i
& 7)));
276 static void minix_setbit(char *a
, unsigned i
)
281 static void minix_clrbit(char *a
, unsigned i
)
287 /* Note: do not assume 0/1, it is 0/nonzero */
288 #define zone_in_use(x) (minix_bit(zone_map,(x)-FIRSTZONE+1))
289 #define inode_in_use(x) (minix_bit(inode_map,(x)))
291 #define mark_inode(x) (minix_setbit(inode_map,(x)))
292 #define unmark_inode(x) (minix_clrbit(inode_map,(x)))
294 #define mark_zone(x) (minix_setbit(zone_map,(x)-FIRSTZONE+1))
295 #define unmark_zone(x) (minix_clrbit(zone_map,(x)-FIRSTZONE+1))
298 static void recursive_check(unsigned ino
);
299 #if ENABLE_FEATURE_MINIX2
300 static void recursive_check2(unsigned ino
);
303 static void die(const char *str
) NORETURN
;
304 static void die(const char *str
)
307 tcsetattr_stdin_TCSANOW(&sv_termios
);
308 bb_error_msg_and_die("%s", str
);
311 static void push_filename(const char *name
)
315 // [0] [1] [2] <-name_component[i]
316 if (name_depth
< MAX_DEPTH
) {
318 char *p
= name_component
[name_depth
];
320 len
= sprintf(p
, "%.*s", namelen
, name
);
321 name_component
[name_depth
+ 1] = p
+ len
;
326 static void pop_filename(void)
329 if (name_depth
< MAX_DEPTH
) {
330 *name_component
[name_depth
] = '\0';
332 current_name
[0] = '/';
333 current_name
[1] = '\0';
338 static int ask(const char *string
, int def
)
344 errors_uncorrected
= 1;
350 errors_uncorrected
= 1;
353 printf(def
? "%s (y/n)? " : "%s (n/y)? ", string
);
359 errors_uncorrected
= 1;
364 c
|= 0x20; /* tolower */
378 errors_uncorrected
= 1;
384 * Make certain that we aren't checking a filesystem that is on a
385 * mounted partition. Code adapted from e2fsck, Copyright (C) 1993,
386 * 1994 Theodore Ts'o. Also licensed under GPL.
388 static void check_mount(void)
390 if (find_mount_point(device_name
, 0)) {
392 #if ENABLE_FEATURE_MTAB_SUPPORT
394 * If the root is mounted read-only, then /etc/mtab is
395 * probably not correct; so we won't issue a warning based on
398 int fd
= open(bb_path_mtab_file
, O_RDWR
);
400 if (fd
< 0 && errno
== EROFS
)
404 printf("%s is mounted. ", device_name
);
406 if (isatty(0) && isatty(1))
407 cont
= ask("Do you really want to continue", 0);
409 printf("Check aborted\n");
416 * check_zone_nr checks to see that *nr is a valid zone nr. If it
417 * isn't, it will possibly be repaired. Check_zone_nr sets *corrected
418 * if an error was corrected, and returns the zone (0 for no zone
419 * or a bad zone-number).
421 static int check_zone_nr2(uint32_t *nr
, smallint
*corrected
)
428 else if (*nr
>= ZONES
)
432 printf("Zone nr %s in file '%s'. ", msg
, current_name
);
433 if (ask("Remove block", 1)) {
440 static int check_zone_nr(uint16_t *nr
, smallint
*corrected
)
443 int r
= check_zone_nr2(&nr32
, corrected
);
444 *nr
= (uint16_t)nr32
;
449 * read-block reads block nr into the buffer at addr.
451 static void read_block(unsigned nr
, void *addr
)
454 memset(addr
, 0, BLOCK_SIZE
);
457 xlseek(dev_fd
, BLOCK_SIZE
* nr
, SEEK_SET
);
458 if (BLOCK_SIZE
!= full_read(dev_fd
, addr
, BLOCK_SIZE
)) {
459 printf("%s: bad block %u in file '%s'\n",
460 bb_msg_read_error
, nr
, current_name
);
461 errors_uncorrected
= 1;
462 memset(addr
, 0, BLOCK_SIZE
);
467 * write_block writes block nr to disk.
469 static void write_block(unsigned nr
, void *addr
)
473 if (nr
< FIRSTZONE
|| nr
>= ZONES
) {
474 printf("Internal error: trying to write bad block\n"
475 "Write request ignored\n");
476 errors_uncorrected
= 1;
479 xlseek(dev_fd
, BLOCK_SIZE
* nr
, SEEK_SET
);
480 if (BLOCK_SIZE
!= full_write(dev_fd
, addr
, BLOCK_SIZE
)) {
481 printf("%s: bad block %u in file '%s'\n",
482 bb_msg_write_error
, nr
, current_name
);
483 errors_uncorrected
= 1;
488 * map_block calculates the absolute block nr of a block in a file.
489 * It sets 'changed' if the inode has needed changing, and re-writes
490 * any indirect blocks with errors.
492 static int map_block(struct minix1_inode
*inode
, unsigned blknr
)
494 uint16_t ind
[BLOCK_SIZE
>> 1];
499 return check_zone_nr(inode
->i_zone
+ blknr
, &changed
);
502 block
= check_zone_nr(inode
->i_zone
+ 7, &changed
);
506 block
= check_zone_nr(inode
->i_zone
+ 8, &changed
);
507 read_block(block
, ind
); /* double indirect */
509 result
= check_zone_nr(&ind
[blknr
/ 512], &blk_chg
);
511 write_block(block
, ind
);
514 read_block(block
, ind
);
516 result
= check_zone_nr(&ind
[blknr
% 512], &blk_chg
);
518 write_block(block
, ind
);
522 #if ENABLE_FEATURE_MINIX2
523 static int map_block2(struct minix2_inode
*inode
, unsigned blknr
)
525 uint32_t ind
[BLOCK_SIZE
>> 2];
530 return check_zone_nr2(inode
->i_zone
+ blknr
, &changed
);
533 block
= check_zone_nr2(inode
->i_zone
+ 7, &changed
);
537 if (blknr
< 256 * 256) {
538 block
= check_zone_nr2(inode
->i_zone
+ 8, &changed
);
542 block
= check_zone_nr2(inode
->i_zone
+ 9, &changed
);
543 read_block(block
, ind
); /* triple indirect */
545 result
= check_zone_nr2(&ind
[blknr
/ (256 * 256)], &blk_chg
);
547 write_block(block
, ind
);
550 read_block(block
, ind
); /* double indirect */
552 result
= check_zone_nr2(&ind
[(blknr
/ 256) % 256], &blk_chg
);
554 write_block(block
, ind
);
557 read_block(block
, ind
);
559 result
= check_zone_nr2(&ind
[blknr
% 256], &blk_chg
);
561 write_block(block
, ind
);
566 static void write_superblock(void)
569 * Set the state of the filesystem based on whether or not there
570 * are uncorrected errors. The filesystem valid flag is
571 * unconditionally set if we get this far.
573 Super
.s_state
|= MINIX_VALID_FS
| MINIX_ERROR_FS
;
574 if (!errors_uncorrected
)
575 Super
.s_state
&= ~MINIX_ERROR_FS
;
577 xlseek(dev_fd
, BLOCK_SIZE
, SEEK_SET
);
578 if (BLOCK_SIZE
!= full_write(dev_fd
, superblock_buffer
, BLOCK_SIZE
))
579 die("can't write superblock");
582 static void write_tables(void)
586 if (IMAPS
* BLOCK_SIZE
!= write(dev_fd
, inode_map
, IMAPS
* BLOCK_SIZE
))
587 die("can't write inode map");
588 if (ZMAPS
* BLOCK_SIZE
!= write(dev_fd
, zone_map
, ZMAPS
* BLOCK_SIZE
))
589 die("can't write zone map");
590 if (INODE_BUFFER_SIZE
!= write(dev_fd
, inode_buffer
, INODE_BUFFER_SIZE
))
591 die("can't write inodes");
594 static void get_dirsize(void)
597 char blk
[BLOCK_SIZE
];
600 #if ENABLE_FEATURE_MINIX2
602 block
= Inode2
[MINIX_ROOT_INO
].i_zone
[0];
605 block
= Inode1
[MINIX_ROOT_INO
].i_zone
[0];
606 read_block(block
, blk
);
607 for (size
= 16; size
< BLOCK_SIZE
; size
<<= 1) {
608 if (strcmp(blk
+ size
+ 2, "..") == 0) {
617 static void read_superblock(void)
619 xlseek(dev_fd
, BLOCK_SIZE
, SEEK_SET
);
620 if (BLOCK_SIZE
!= full_read(dev_fd
, superblock_buffer
, BLOCK_SIZE
))
621 die("can't read superblock");
622 /* already initialized to:
627 if (MAGIC
== MINIX1_SUPER_MAGIC
) {
628 } else if (MAGIC
== MINIX1_SUPER_MAGIC2
) {
631 #if ENABLE_FEATURE_MINIX2
632 } else if (MAGIC
== MINIX2_SUPER_MAGIC
) {
634 } else if (MAGIC
== MINIX2_SUPER_MAGIC2
) {
640 die("bad magic number in superblock");
641 if (ZONESIZE
!= 0 || BLOCK_SIZE
!= 1024)
642 die("only 1k blocks/zones supported");
643 if (IMAPS
* BLOCK_SIZE
* 8 < INODES
+ 1)
644 die("bad s_imap_blocks field in superblock");
645 if (ZMAPS
* BLOCK_SIZE
* 8 < ZONES
- FIRSTZONE
+ 1)
646 die("bad s_zmap_blocks field in superblock");
649 static void read_tables(void)
651 inode_map
= xzalloc(IMAPS
* BLOCK_SIZE
);
652 zone_map
= xzalloc(ZMAPS
* BLOCK_SIZE
);
653 inode_buffer
= xmalloc(INODE_BUFFER_SIZE
);
654 inode_count
= xmalloc(INODES
+ 1);
655 zone_count
= xmalloc(ZONES
);
656 if (IMAPS
* BLOCK_SIZE
!= read(dev_fd
, inode_map
, IMAPS
* BLOCK_SIZE
))
657 die("can't read inode map");
658 if (ZMAPS
* BLOCK_SIZE
!= read(dev_fd
, zone_map
, ZMAPS
* BLOCK_SIZE
))
659 die("can't read zone map");
660 if (INODE_BUFFER_SIZE
!= read(dev_fd
, inode_buffer
, INODE_BUFFER_SIZE
))
661 die("can't read inodes");
662 if (NORM_FIRSTZONE
!= FIRSTZONE
) {
663 printf("warning: firstzone!=norm_firstzone\n");
664 errors_uncorrected
= 1;
670 "Firstdatazone=%u (%u)\n"
673 "Filesystem state=%u\n"
677 FIRSTZONE
, NORM_FIRSTZONE
,
678 BLOCK_SIZE
<< ZONESIZE
,
685 static void get_inode_common(unsigned nr
, uint16_t i_mode
)
688 if (!inode_count
[nr
]) {
689 if (!inode_in_use(nr
)) {
690 printf("Inode %d is marked as 'unused', but it is used "
691 "for file '%s'\n", nr
, current_name
);
693 if (ask("Mark as 'in use'", 1))
696 errors_uncorrected
= 1;
701 else if (S_ISREG(i_mode
))
703 else if (S_ISCHR(i_mode
))
705 else if (S_ISBLK(i_mode
))
707 else if (S_ISLNK(i_mode
))
709 else if (S_ISSOCK(i_mode
));
710 else if (S_ISFIFO(i_mode
));
712 printf("%s has mode %05o\n", current_name
, i_mode
);
716 if (!++inode_count
[nr
]) {
717 printf("Warning: inode count too big\n");
719 errors_uncorrected
= 1;
723 static struct minix1_inode
*get_inode(unsigned nr
)
725 struct minix1_inode
*inode
;
727 if (!nr
|| nr
> INODES
)
730 get_inode_common(nr
, inode
->i_mode
);
734 #if ENABLE_FEATURE_MINIX2
735 static struct minix2_inode
*get_inode2(unsigned nr
)
737 struct minix2_inode
*inode
;
739 if (!nr
|| nr
> INODES
)
742 get_inode_common(nr
, inode
->i_mode
);
747 static void check_root(void)
749 struct minix1_inode
*inode
= Inode1
+ MINIX_ROOT_INO
;
751 if (!inode
|| !S_ISDIR(inode
->i_mode
))
752 die("root inode isn't a directory");
755 #if ENABLE_FEATURE_MINIX2
756 static void check_root2(void)
758 struct minix2_inode
*inode
= Inode2
+ MINIX_ROOT_INO
;
760 if (!inode
|| !S_ISDIR(inode
->i_mode
))
761 die("root inode isn't a directory");
764 void check_root2(void);
767 static int add_zone_common(int block
, smallint
*corrected
)
771 if (zone_count
[block
]) {
772 printf("Already used block is reused in file '%s'. ",
774 if (ask("Clear", 1)) {
777 return -1; /* "please zero out *znr" */
780 if (!zone_in_use(block
)) {
781 printf("Block %d in file '%s' is marked as 'unused'. ",
782 block
, current_name
);
783 if (ask("Correct", 1))
786 if (!++zone_count
[block
])
791 static int add_zone(uint16_t *znr
, smallint
*corrected
)
795 block
= check_zone_nr(znr
, corrected
);
796 block
= add_zone_common(block
, corrected
);
804 #if ENABLE_FEATURE_MINIX2
805 static int add_zone2(uint32_t *znr
, smallint
*corrected
)
809 block
= check_zone_nr2(znr
, corrected
);
810 block
= add_zone_common(block
, corrected
);
819 static void add_zone_ind(uint16_t *znr
, smallint
*corrected
)
823 smallint chg_blk
= 0;
825 block
= add_zone(znr
, corrected
);
828 read_block(block
, add_zone_ind_blk
);
829 for (i
= 0; i
< (BLOCK_SIZE
>> 1); i
++)
830 add_zone(i
+ (uint16_t *) add_zone_ind_blk
, &chg_blk
);
832 write_block(block
, add_zone_ind_blk
);
835 #if ENABLE_FEATURE_MINIX2
836 static void add_zone_ind2(uint32_t *znr
, smallint
*corrected
)
840 smallint chg_blk
= 0;
842 block
= add_zone2(znr
, corrected
);
845 read_block(block
, add_zone_ind_blk
);
846 for (i
= 0; i
< BLOCK_SIZE
>> 2; i
++)
847 add_zone2(i
+ (uint32_t *) add_zone_ind_blk
, &chg_blk
);
849 write_block(block
, add_zone_ind_blk
);
853 static void add_zone_dind(uint16_t *znr
, smallint
*corrected
)
857 smallint chg_blk
= 0;
859 block
= add_zone(znr
, corrected
);
862 read_block(block
, add_zone_dind_blk
);
863 for (i
= 0; i
< (BLOCK_SIZE
>> 1); i
++)
864 add_zone_ind(i
+ (uint16_t *) add_zone_dind_blk
, &chg_blk
);
866 write_block(block
, add_zone_dind_blk
);
869 #if ENABLE_FEATURE_MINIX2
870 static void add_zone_dind2(uint32_t *znr
, smallint
*corrected
)
874 smallint chg_blk
= 0;
876 block
= add_zone2(znr
, corrected
);
879 read_block(block
, add_zone_dind_blk
);
880 for (i
= 0; i
< BLOCK_SIZE
>> 2; i
++)
881 add_zone_ind2(i
+ (uint32_t *) add_zone_dind_blk
, &chg_blk
);
883 write_block(block
, add_zone_dind_blk
);
886 static void add_zone_tind2(uint32_t *znr
, smallint
*corrected
)
890 smallint chg_blk
= 0;
892 block
= add_zone2(znr
, corrected
);
895 read_block(block
, add_zone_tind_blk
);
896 for (i
= 0; i
< BLOCK_SIZE
>> 2; i
++)
897 add_zone_dind2(i
+ (uint32_t *) add_zone_tind_blk
, &chg_blk
);
899 write_block(block
, add_zone_tind_blk
);
903 static void check_zones(unsigned i
)
905 struct minix1_inode
*inode
;
907 if (!i
|| i
> INODES
)
909 if (inode_count
[i
] > 1) /* have we counted this file already? */
912 if (!S_ISDIR(inode
->i_mode
)
913 && !S_ISREG(inode
->i_mode
)
914 && !S_ISLNK(inode
->i_mode
)
918 for (i
= 0; i
< 7; i
++)
919 add_zone(i
+ inode
->i_zone
, &changed
);
920 add_zone_ind(7 + inode
->i_zone
, &changed
);
921 add_zone_dind(8 + inode
->i_zone
, &changed
);
924 #if ENABLE_FEATURE_MINIX2
925 static void check_zones2(unsigned i
)
927 struct minix2_inode
*inode
;
929 if (!i
|| i
> INODES
)
931 if (inode_count
[i
] > 1) /* have we counted this file already? */
934 if (!S_ISDIR(inode
->i_mode
) && !S_ISREG(inode
->i_mode
)
935 && !S_ISLNK(inode
->i_mode
))
937 for (i
= 0; i
< 7; i
++)
938 add_zone2(i
+ inode
->i_zone
, &changed
);
939 add_zone_ind2(7 + inode
->i_zone
, &changed
);
940 add_zone_dind2(8 + inode
->i_zone
, &changed
);
941 add_zone_tind2(9 + inode
->i_zone
, &changed
);
945 static void check_file(struct minix1_inode
*dir
, unsigned offset
)
947 struct minix1_inode
*inode
;
952 block
= map_block(dir
, offset
/ BLOCK_SIZE
);
953 read_block(block
, check_file_blk
);
954 name
= check_file_blk
+ (offset
% BLOCK_SIZE
) + 2;
955 ino
= *(uint16_t *) (name
- 2);
957 printf("%s contains a bad inode number for file '%.*s'. ",
958 current_name
, namelen
, name
);
959 if (ask("Remove", 1)) {
960 *(uint16_t *) (name
- 2) = 0;
961 write_block(block
, check_file_blk
);
966 inode
= get_inode(ino
);
969 if (inode
&& LONE_CHAR(name
, '.'))
971 printf("%s: bad directory: '.' isn't first\n", current_name
);
972 errors_uncorrected
= 1;
974 if (offset
== dirsize
) {
975 if (inode
&& strcmp("..", name
) == 0)
977 printf("%s: bad directory: '..' isn't second\n", current_name
);
978 errors_uncorrected
= 1;
985 printf("%6d %07o %3d ", ino
, inode
->i_mode
, inode
->i_nlinks
);
986 printf("%s%s\n", current_name
, S_ISDIR(inode
->i_mode
) ? ":" : "");
989 if (inode
&& S_ISDIR(inode
->i_mode
))
990 recursive_check(ino
);
994 #if ENABLE_FEATURE_MINIX2
995 static void check_file2(struct minix2_inode
*dir
, unsigned offset
)
997 struct minix2_inode
*inode
;
1002 block
= map_block2(dir
, offset
/ BLOCK_SIZE
);
1003 read_block(block
, check_file_blk
);
1004 name
= check_file_blk
+ (offset
% BLOCK_SIZE
) + 2;
1005 ino
= *(uint16_t *) (name
- 2);
1007 printf("%s contains a bad inode number for file '%.*s'. ",
1008 current_name
, namelen
, name
);
1009 if (ask("Remove", 1)) {
1010 *(uint16_t *) (name
- 2) = 0;
1011 write_block(block
, check_file_blk
);
1015 push_filename(name
);
1016 inode
= get_inode2(ino
);
1019 if (inode
&& LONE_CHAR(name
, '.'))
1021 printf("%s: bad directory: '.' isn't first\n", current_name
);
1022 errors_uncorrected
= 1;
1024 if (offset
== dirsize
) {
1025 if (inode
&& strcmp("..", name
) == 0)
1027 printf("%s: bad directory: '..' isn't second\n", current_name
);
1028 errors_uncorrected
= 1;
1032 push_filename(name
);
1035 printf("%6d %07o %3d ", ino
, inode
->i_mode
, inode
->i_nlinks
);
1036 printf("%s%s\n", current_name
, S_ISDIR(inode
->i_mode
) ? ":" : "");
1039 if (inode
&& S_ISDIR(inode
->i_mode
))
1040 recursive_check2(ino
);
1045 static void recursive_check(unsigned ino
)
1047 struct minix1_inode
*dir
;
1051 if (!S_ISDIR(dir
->i_mode
))
1052 die("internal error");
1053 if (dir
->i_size
< 2 * dirsize
) {
1054 printf("%s: bad directory: size<32", current_name
);
1055 errors_uncorrected
= 1;
1057 for (offset
= 0; offset
< dir
->i_size
; offset
+= dirsize
)
1058 check_file(dir
, offset
);
1061 #if ENABLE_FEATURE_MINIX2
1062 static void recursive_check2(unsigned ino
)
1064 struct minix2_inode
*dir
;
1068 if (!S_ISDIR(dir
->i_mode
))
1069 die("internal error");
1070 if (dir
->i_size
< 2 * dirsize
) {
1071 printf("%s: bad directory: size<32", current_name
);
1072 errors_uncorrected
= 1;
1074 for (offset
= 0; offset
< dir
->i_size
; offset
+= dirsize
)
1075 check_file2(dir
, offset
);
1079 static int bad_zone(int i
)
1081 char buffer
[BLOCK_SIZE
];
1083 xlseek(dev_fd
, BLOCK_SIZE
* i
, SEEK_SET
);
1084 return (BLOCK_SIZE
!= full_read(dev_fd
, buffer
, BLOCK_SIZE
));
1087 static void check_counts(void)
1091 for (i
= 1; i
<= INODES
; i
++) {
1092 if (OPT_warn_mode
&& Inode1
[i
].i_mode
&& !inode_in_use(i
)) {
1093 printf("Inode %d has non-zero mode. ", i
);
1094 if (ask("Clear", 1)) {
1095 Inode1
[i
].i_mode
= 0;
1099 if (!inode_count
[i
]) {
1100 if (!inode_in_use(i
))
1102 printf("Unused inode %d is marked as 'used' in the bitmap. ", i
);
1103 if (ask("Clear", 1))
1107 if (!inode_in_use(i
)) {
1108 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i
);
1112 if (Inode1
[i
].i_nlinks
!= inode_count
[i
]) {
1113 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
1114 i
, Inode1
[i
].i_mode
, Inode1
[i
].i_nlinks
,
1116 if (ask("Set i_nlinks to count", 1)) {
1117 Inode1
[i
].i_nlinks
= inode_count
[i
];
1122 for (i
= FIRSTZONE
; i
< ZONES
; i
++) {
1123 if ((zone_in_use(i
) != 0) == zone_count
[i
])
1125 if (!zone_count
[i
]) {
1128 printf("Zone %d is marked 'in use', but no file uses it. ", i
);
1129 if (ask("Unmark", 1))
1133 printf("Zone %d: %sin use, counted=%d\n",
1134 i
, zone_in_use(i
) ? "" : "not ", zone_count
[i
]);
1138 #if ENABLE_FEATURE_MINIX2
1139 static void check_counts2(void)
1143 for (i
= 1; i
<= INODES
; i
++) {
1144 if (OPT_warn_mode
&& Inode2
[i
].i_mode
&& !inode_in_use(i
)) {
1145 printf("Inode %d has non-zero mode. ", i
);
1146 if (ask("Clear", 1)) {
1147 Inode2
[i
].i_mode
= 0;
1151 if (!inode_count
[i
]) {
1152 if (!inode_in_use(i
))
1154 printf("Unused inode %d is marked as 'used' in the bitmap. ", i
);
1155 if (ask("Clear", 1))
1159 if (!inode_in_use(i
)) {
1160 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i
);
1164 if (Inode2
[i
].i_nlinks
!= inode_count
[i
]) {
1165 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
1166 i
, Inode2
[i
].i_mode
, Inode2
[i
].i_nlinks
,
1168 if (ask("Set i_nlinks to count", 1)) {
1169 Inode2
[i
].i_nlinks
= inode_count
[i
];
1174 for (i
= FIRSTZONE
; i
< ZONES
; i
++) {
1175 if ((zone_in_use(i
) != 0) == zone_count
[i
])
1177 if (!zone_count
[i
]) {
1180 printf("Zone %d is marked 'in use', but no file uses it. ", i
);
1181 if (ask("Unmark", 1))
1185 printf("Zone %d: %sin use, counted=%d\n",
1186 i
, zone_in_use(i
) ? "" : "not ", zone_count
[i
]);
1191 static void check(void)
1193 memset(inode_count
, 0, (INODES
+ 1) * sizeof(*inode_count
));
1194 memset(zone_count
, 0, ZONES
* sizeof(*zone_count
));
1195 check_zones(MINIX_ROOT_INO
);
1196 recursive_check(MINIX_ROOT_INO
);
1200 #if ENABLE_FEATURE_MINIX2
1201 static void check2(void)
1203 memset(inode_count
, 0, (INODES
+ 1) * sizeof(*inode_count
));
1204 memset(zone_count
, 0, ZONES
* sizeof(*zone_count
));
1205 check_zones2(MINIX_ROOT_INO
);
1206 recursive_check2(MINIX_ROOT_INO
);
1213 int fsck_minix_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
1214 int fsck_minix_main(int argc UNUSED_PARAM
, char **argv
)
1219 xfunc_error_retval
= 8;
1223 opt_complementary
= "=1:ar"; /* one argument; -a assumes -r */
1224 getopt32(argv
, OPTION_STR
);
1226 device_name
= argv
[0];
1228 check_mount(); /* trying to check a mounted filesystem? */
1230 if (!isatty(0) || !isatty(1))
1231 die("need terminal for interactive repairs");
1233 xmove_fd(xopen(device_name
, OPT_repair
? O_RDWR
: O_RDONLY
), dev_fd
);
1235 /*sync(); paranoia? */
1239 * Determine whether or not we should continue with the checking.
1240 * This is based on the status of the filesystem valid and error
1241 * flags and whether or not the -f switch was specified on the
1244 printf("%s: %s\n", applet_name
, bb_banner
);
1246 if (!(Super
.s_state
& MINIX_ERROR_FS
)
1247 && (Super
.s_state
& MINIX_VALID_FS
) && !OPT_force
1250 printf("%s is clean, check is skipped\n", device_name
);
1252 } else if (OPT_force
)
1253 printf("Forcing filesystem check on %s\n", device_name
);
1254 else if (OPT_repair
)
1255 printf("Filesystem on %s is dirty, needs checking\n",
1261 tcgetattr(0, &sv_termios
);
1263 tmp
.c_lflag
&= ~(ICANON
| ECHO
);
1264 tcsetattr_stdin_TCSANOW(&tmp
);
1279 for (i
= 1, free_cnt
= 0; i
<= INODES
; i
++)
1280 if (!inode_in_use(i
))
1282 printf("\n%6u inodes used (%u%%)\n", (INODES
- free_cnt
),
1283 100 * (INODES
- free_cnt
) / INODES
);
1284 for (i
= FIRSTZONE
, free_cnt
= 0; i
< ZONES
; i
++)
1285 if (!zone_in_use(i
))
1287 printf("%6u zones used (%u%%)\n\n"
1288 "%6u regular files\n"
1290 "%6u character device files\n"
1291 "%6u block device files\n"
1293 "%6u symbolic links\n"
1296 (ZONES
- free_cnt
), 100 * (ZONES
- free_cnt
) / ZONES
,
1297 regular
, directory
, chardev
, blockdev
,
1298 links
- 2 * directory
+ 1, symlinks
,
1299 total
- 2 * directory
+ 1);
1303 printf("FILE SYSTEM HAS BEEN CHANGED\n");
1305 } else if (OPT_repair
)
1309 tcsetattr_stdin_TCSANOW(&sv_termios
);
1313 if (errors_uncorrected
)