aboutsummaryrefslogtreecommitdiff
path: root/util-linux/fsck_minix.c
diff options
context:
space:
mode:
Diffstat (limited to 'util-linux/fsck_minix.c')
-rw-r--r--util-linux/fsck_minix.c1417
1 files changed, 1417 insertions, 0 deletions
diff --git a/util-linux/fsck_minix.c b/util-linux/fsck_minix.c
new file mode 100644
index 000000000..9c831bd59
--- /dev/null
+++ b/util-linux/fsck_minix.c
@@ -0,0 +1,1417 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * fsck.c - a file system consistency checker for Linux.
4 *
5 * (C) 1991, 1992 Linus Torvalds.
6 *
7 * Licensed under GPLv2, see file LICENSE in this tarball for details.
8 */
9
10/*
11 * 09.11.91 - made the first rudimentary functions
12 *
13 * 10.11.91 - updated, does checking, no repairs yet.
14 * Sent out to the mailing-list for testing.
15 *
16 * 14.11.91 - Testing seems to have gone well. Added some
17 * correction-code, and changed some functions.
18 *
19 * 15.11.91 - More correction code. Hopefully it notices most
20 * cases now, and tries to do something about them.
21 *
22 * 16.11.91 - More corrections (thanks to Mika Jalava). Most
23 * things seem to work now. Yeah, sure.
24 *
25 *
26 * 19.04.92 - Had to start over again from this old version, as a
27 * kernel bug ate my enhanced fsck in february.
28 *
29 * 28.02.93 - added support for different directory entry sizes..
30 *
31 * Sat Mar 6 18:59:42 1993, faith@cs.unc.edu: Output namelen with
32 * super-block information
33 *
34 * Sat Oct 9 11:17:11 1993, faith@cs.unc.edu: make exit status conform
35 * to that required by fsutil
36 *
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
41 * is executed.
42 *
43 * 30.10.94 - added support for v2 filesystem
44 * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de)
45 *
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)
49 *
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)
52 *
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)
59 *
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>.
63 *
64 * 06.11.96 - Added v2 code submitted by Joerg Dorchain, but written by
65 * Andreas Schwab.
66 *
67 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
68 * - added Native Language Support
69 *
70 *
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).
76 *
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
85 *
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 :-).
88 */
89
90#include "busybox.h"
91#include <mntent.h>
92
93/*
94 * This is the original minix inode layout on disk.
95 * Note the 8-bit gid and atime and ctime.
96 */
97struct minix_inode {
98 uint16_t i_mode;
99 uint16_t i_uid;
100 uint32_t i_size;
101 uint32_t i_time;
102 uint8_t i_gid;
103 uint8_t i_nlinks;
104 uint16_t i_zone[9];
105};
106
107/*
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.
112 */
113struct minix2_inode {
114 uint16_t i_mode;
115 uint16_t i_nlinks;
116 uint16_t i_uid;
117 uint16_t i_gid;
118 uint32_t i_size;
119 uint32_t i_atime;
120 uint32_t i_mtime;
121 uint32_t i_ctime;
122 uint32_t i_zone[10];
123};
124
125enum {
126 MINIX_ROOT_INO = 1,
127 MINIX_LINK_MAX = 250,
128 MINIX2_LINK_MAX = 65530,
129
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. */
138
139 MINIX_INODES_PER_BLOCK = ((BLOCK_SIZE)/(sizeof (struct minix_inode))),
140 MINIX2_INODES_PER_BLOCK = ((BLOCK_SIZE)/(sizeof (struct minix2_inode))),
141
142 MINIX_V1 = 0x0001, /* original minix fs */
143 MINIX_V2 = 0x0002 /* minix V2 fs */
144};
145
146#define INODE_VERSION(inode) inode->i_sb->u.minix_sb.s_version
147
148/*
149 * minix super-block data on disk
150 */
151struct minix_super_block {
152 uint16_t s_ninodes;
153 uint16_t s_nzones;
154 uint16_t s_imap_blocks;
155 uint16_t s_zmap_blocks;
156 uint16_t s_firstdatazone;
157 uint16_t s_log_zone_size;
158 uint32_t s_max_size;
159 uint16_t s_magic;
160 uint16_t s_state;
161 uint32_t s_zones;
162};
163
164struct minix_dir_entry {
165 uint16_t inode;
166 char name[0];
167};
168
169
170#define NAME_MAX 255 /* # chars in a file name */
171
172#define MINIX_INODES_PER_BLOCK ((BLOCK_SIZE)/(sizeof (struct minix_inode)))
173
174#ifndef BLKGETSIZE
175#define BLKGETSIZE _IO(0x12,96) /* return device size */
176#endif
177
178#ifndef __linux__
179#define volatile
180#endif
181
182enum { ROOT_INO = 1 };
183
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))
190#else
191#define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK))
192#endif
193#define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
194
195#define BITS_PER_BLOCK (BLOCK_SIZE<<3)
196
197static char *program_version = "1.2 - 11/11/96";
198static char *device_name;
199static int IN;
200static int repair, automatic, verbose, list, show, warn_mode, force;
201static int directory, regular, blockdev, chardev, links, symlinks, total;
202
203static int changed; /* flags if the filesystem has been changed */
204static int errors_uncorrected; /* flag if some error was not corrected */
205static int dirsize = 16;
206static int namelen = 14;
207static struct termios termios;
208static int termios_set;
209
210static char *inode_buffer;
211#define Inode (((struct minix_inode *) inode_buffer)-1)
212#define Inode2 (((struct minix2_inode *) inode_buffer)-1)
213static char super_block_buffer[BLOCK_SIZE];
214
215#define Super (*(struct minix_super_block *)super_block_buffer)
216#define INODES ((unsigned long)Super.s_ninodes)
217#ifdef CONFIG_FEATURE_MINIX2
218static int version2;
219#define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones))
220#else
221#define ZONES ((unsigned long)(Super.s_nzones))
222#endif
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)
230
231static char *inode_map;
232static char *zone_map;
233
234static unsigned char *inode_count;
235static unsigned char *zone_count;
236
237static void recursive_check(unsigned int ino);
238#ifdef CONFIG_FEATURE_MINIX2
239static void recursive_check2(unsigned int ino);
240#endif
241
242static int bit(char *a, unsigned int i)
243{
244 return (a[i >> 3] & (1<<(i & 7))) != 0;
245}
246#define inode_in_use(x) (bit(inode_map,(x)))
247#define zone_in_use(x) (bit(zone_map,(x)-FIRSTZONE+1))
248
249#define mark_inode(x) (setbit(inode_map,(x)),changed=1)
250#define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
251
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)
254
255static void leave(int) ATTRIBUTE_NORETURN;
256static void leave(int status)
257{
258 if (termios_set)
259 tcsetattr(0, TCSANOW, &termios);
260 exit(status);
261}
262
263static void die(const char *str)
264{
265 bb_error_msg("%s", str);
266 leave(8);
267}
268
269/* File-name data */
270enum { MAX_DEPTH = 32 };
271static int name_depth;
272static char *current_name;
273static char *name_component[MAX_DEPTH+1];
274
275/* Wed Feb 9 15:17:06 MST 2000 */
276/* dynamically allocate name_list (instead of making it static) */
277static void alloc_current_name(void)
278{
279 current_name = xmalloc(MAX_DEPTH * (BUFSIZ + 1));
280 current_name[0] = '/';
281 current_name[1] = '\0';
282 name_component[0] = &current_name[0];
283}
284
285#ifdef CONFIG_FEATURE_CLEAN_UP
286/* execute this atexit() to deallocate name_list[] */
287/* piptigger was here */
288static void free_current_name(void)
289{
290 free(current_name);
291}
292#endif
293
294static void push_filename(const char *name)
295{
296 // /dir/dir/dir/file
297 // ^ ^ ^
298 // [0] [1] [2] <-name_component[i]
299 if (name_depth < MAX_DEPTH) {
300 int len;
301 char *p = name_component[name_depth];
302 *p++ = '/';
303 len = sprintf(p, "%.*s", namelen, name);
304 name_component[name_depth + 1] = p + len;
305 }
306 name_depth++;
307}
308
309static void pop_filename(void) {
310 name_depth--;
311 if (name_depth < MAX_DEPTH) {
312 *name_component[name_depth] = '\0';
313 if (!name_depth) {
314 current_name[0] = '/';
315 current_name[1] = '\0';
316 }
317 }
318}
319
320static int ask(const char *string, int def)
321{
322 int c;
323
324 if (!repair) {
325 puts("");
326 errors_uncorrected = 1;
327 return 0;
328 }
329 if (automatic) {
330 puts("");
331 if (!def)
332 errors_uncorrected = 1;
333 return def;
334 }
335 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
336 for (;;) {
337 fflush(stdout);
338 if ((c = getchar()) == EOF) {
339 if (!def)
340 errors_uncorrected = 1;
341 return def;
342 }
343 c = toupper(c);
344 if (c == 'Y') {
345 def = 1;
346 break;
347 } else if (c == 'N') {
348 def = 0;
349 break;
350 } else if (c == ' ' || c == '\n')
351 break;
352 }
353 if (def)
354 printf("y\n");
355 else {
356 printf("n\n");
357 errors_uncorrected = 1;
358 }
359 return def;
360}
361
362/*
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.
366 */
367static void check_mount(void)
368{
369 FILE *f;
370 struct mntent *mnt;
371 int cont;
372 int fd;
373
374 if ((f = setmntent(MOUNTED, "r")) == NULL)
375 return;
376 while ((mnt = getmntent(f)) != NULL)
377 if (strcmp(device_name, mnt->mnt_fsname) == 0)
378 break;
379 endmntent(f);
380 if (!mnt)
381 return;
382
383 /*
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
386 * it.
387 */
388 fd = open(MOUNTED, O_RDWR);
389 if (fd < 0 && errno == EROFS)
390 return;
391 else
392 close(fd);
393
394 printf("%s is mounted. ", device_name);
395 cont = 0;
396 if (isatty(0) && isatty(1))
397 cont = ask("Do you really want to continue", 0);
398 if (!cont) {
399 printf("Check aborted\n");
400 exit(0);
401 }
402 return;
403}
404
405/*
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).
410 */
411static int check_zone_nr2(uint32_t *nr, int *corrected)
412{
413 const char *msg;
414 if (!*nr)
415 return 0;
416 if (*nr < FIRSTZONE)
417 msg = "< FIRSTZONE";
418 else if (*nr >= ZONES)
419 msg = ">= ZONES";
420 else
421 return *nr;
422 printf("Zone nr %s in file '%s'. ", msg, current_name);
423 if (ask("Remove block", 1)) {
424 *nr = 0;
425 *corrected = 1;
426 }
427 return 0;
428}
429
430static int check_zone_nr(uint16_t *nr, int *corrected)
431{
432 uint32_t nr32 = *nr;
433 int r = check_zone_nr2(&nr32, corrected);
434 *nr = (uint16_t)nr32;
435 return r;
436}
437
438/*
439 * read-block reads block nr into the buffer at addr.
440 */
441static void read_block(unsigned int nr, char *addr)
442{
443 if (!nr) {
444 memset(addr, 0, BLOCK_SIZE);
445 return;
446 }
447 if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) {
448 printf("%s: cannot 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);
457 }
458}
459
460/*
461 * write_block writes block nr to disk.
462 */
463static void write_block(unsigned int nr, char *addr)
464{
465 if (!nr)
466 return;
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;
471 return;
472 }
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;
479 }
480}
481
482/*
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.
486 */
487static int map_block(struct minix_inode *inode, unsigned int blknr)
488{
489 uint16_t ind[BLOCK_SIZE >> 1];
490 uint16_t dind[BLOCK_SIZE >> 1];
491 int blk_chg, block, result;
492
493 if (blknr < 7)
494 return check_zone_nr(inode->i_zone + blknr, &changed);
495 blknr -= 7;
496 if (blknr < 512) {
497 block = check_zone_nr(inode->i_zone + 7, &changed);
498 read_block(block, (char *) ind);
499 blk_chg = 0;
500 result = check_zone_nr(blknr + ind, &blk_chg);
501 if (blk_chg)
502 write_block(block, (char *) ind);
503 return result;
504 }
505 blknr -= 512;
506 block = check_zone_nr(inode->i_zone + 8, &changed);
507 read_block(block, (char *) dind);
508 blk_chg = 0;
509 result = check_zone_nr(dind + (blknr / 512), &blk_chg);
510 if (blk_chg)
511 write_block(block, (char *) dind);
512 block = result;
513 read_block(block, (char *) ind);
514 blk_chg = 0;
515 result = check_zone_nr(ind + (blknr % 512), &blk_chg);
516 if (blk_chg)
517 write_block(block, (char *) ind);
518 return result;
519}
520
521#ifdef CONFIG_FEATURE_MINIX2
522static int map_block2(struct minix2_inode *inode, unsigned int blknr)
523{
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;
528
529 if (blknr < 7)
530 return check_zone_nr2(inode->i_zone + blknr, &changed);
531 blknr -= 7;
532 if (blknr < 256) {
533 block = check_zone_nr2(inode->i_zone + 7, &changed);
534 read_block(block, (char *) ind);
535 blk_chg = 0;
536 result = check_zone_nr2(blknr + ind, &blk_chg);
537 if (blk_chg)
538 write_block(block, (char *) ind);
539 return result;
540 }
541 blknr -= 256;
542 if (blknr >= 256 * 256) {
543 block = check_zone_nr2(inode->i_zone + 8, &changed);
544 read_block(block, (char *) dind);
545 blk_chg = 0;
546 result = check_zone_nr2(dind + blknr / 256, &blk_chg);
547 if (blk_chg)
548 write_block(block, (char *) dind);
549 block = result;
550 read_block(block, (char *) ind);
551 blk_chg = 0;
552 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
553 if (blk_chg)
554 write_block(block, (char *) ind);
555 return result;
556 }
557 blknr -= 256 * 256;
558 block = check_zone_nr2(inode->i_zone + 9, &changed);
559 read_block(block, (char *) tind);
560 blk_chg = 0;
561 result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
562 if (blk_chg)
563 write_block(block, (char *) tind);
564 block = result;
565 read_block(block, (char *) dind);
566 blk_chg = 0;
567 result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
568 if (blk_chg)
569 write_block(block, (char *) dind);
570 block = result;
571 read_block(block, (char *) ind);
572 blk_chg = 0;
573 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
574 if (blk_chg)
575 write_block(block, (char *) ind);
576 return result;
577}
578#endif
579
580static void write_super_block(void)
581{
582 /*
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.
586 */
587 Super.s_state |= MINIX_VALID_FS;
588 if (errors_uncorrected)
589 Super.s_state |= MINIX_ERROR_FS;
590 else
591 Super.s_state &= ~MINIX_ERROR_FS;
592
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");
597}
598
599static void write_tables(void)
600{
601 write_super_block();
602
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");
609}
610
611static void get_dirsize(void)
612{
613 int block;
614 char blk[BLOCK_SIZE];
615 int size;
616
617#ifdef CONFIG_FEATURE_MINIX2
618 if (version2)
619 block = Inode2[ROOT_INO].i_zone[0];
620 else
621#endif
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) {
626 dirsize = size;
627 namelen = size - 2;
628 return;
629 }
630 }
631 /* use defaults */
632}
633
634static void read_superblock(void)
635{
636 if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET))
637 die("Seek failed");
638 if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE))
639 die("Unable to read super block");
640 /* already initialized to:
641 namelen = 14;
642 dirsize = 16;
643 version2 = 0;
644 */
645 if (MAGIC == MINIX_SUPER_MAGIC) {
646 } else if (MAGIC == MINIX_SUPER_MAGIC2) {
647 namelen = 30;
648 dirsize = 32;
649#ifdef CONFIG_FEATURE_MINIX2
650 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
651 version2 = 1;
652 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
653 namelen = 30;
654 dirsize = 32;
655 version2 = 1;
656#endif
657 } else
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");
665}
666
667static void read_tables(void)
668{
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;
683 }
684 get_dirsize();
685 if (show) {
686 printf("%ld inodes\n"
687 "%ld blocks\n"
688 "Firstdatazone=%ld (%ld)\n"
689 "Zonesize=%d\n"
690 "Maxsize=%ld\n"
691 "Filesystem state=%d\n"
692 "namelen=%d\n\n",
693 INODES,
694 ZONES,
695 FIRSTZONE, NORM_FIRSTZONE,
696 BLOCK_SIZE << ZONESIZE,
697 MAXSIZE,
698 Super.s_state,
699 namelen);
700 }
701}
702
703static struct minix_inode *get_inode(unsigned int nr)
704{
705 struct minix_inode *inode;
706
707 if (!nr || nr > INODES)
708 return NULL;
709 total++;
710 inode = Inode + nr;
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);
715 if (repair) {
716 if (ask("Mark as 'in use'", 1))
717 mark_inode(nr);
718 } else {
719 errors_uncorrected = 1;
720 }
721 }
722 if (S_ISDIR(inode->i_mode))
723 directory++;
724 else if (S_ISREG(inode->i_mode))
725 regular++;
726 else if (S_ISCHR(inode->i_mode))
727 chardev++;
728 else if (S_ISBLK(inode->i_mode))
729 blockdev++;
730 else if (S_ISLNK(inode->i_mode))
731 symlinks++;
732 else if (S_ISSOCK(inode->i_mode));
733 else if (S_ISFIFO(inode->i_mode));
734 else {
735 printf("%s has mode %05o\n", current_name, inode->i_mode);
736 }
737
738 } else
739 links++;
740 if (!++inode_count[nr]) {
741 printf("Warning: inode count too big\n");
742 inode_count[nr]--;
743 errors_uncorrected = 1;
744 }
745 return inode;
746}
747
748#ifdef CONFIG_FEATURE_MINIX2
749static struct minix2_inode *get_inode2(unsigned int nr)
750{
751 struct minix2_inode *inode;
752
753 if (!nr || nr > INODES)
754 return NULL;
755 total++;
756 inode = Inode2 + nr;
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);
761 if (repair) {
762 if (ask("Mark as 'in use'", 1))
763 mark_inode(nr);
764 else
765 errors_uncorrected = 1;
766 }
767 }
768 if (S_ISDIR(inode->i_mode))
769 directory++;
770 else if (S_ISREG(inode->i_mode))
771 regular++;
772 else if (S_ISCHR(inode->i_mode))
773 chardev++;
774 else if (S_ISBLK(inode->i_mode))
775 blockdev++;
776 else if (S_ISLNK(inode->i_mode))
777 symlinks++;
778 else if (S_ISSOCK(inode->i_mode));
779 else if (S_ISFIFO(inode->i_mode));
780 else {
781 printf("%s has mode %05o\n", current_name, inode->i_mode);
782 }
783 } else
784 links++;
785 if (!++inode_count[nr]) {
786 printf("Warning: inode count too big\n");
787 inode_count[nr]--;
788 errors_uncorrected = 1;
789 }
790 return inode;
791}
792#endif
793
794static void check_root(void)
795{
796 struct minix_inode *inode = Inode + ROOT_INO;
797
798 if (!inode || !S_ISDIR(inode->i_mode))
799 die("Root inode isn't a directory");
800}
801
802#ifdef CONFIG_FEATURE_MINIX2
803static void check_root2(void)
804{
805 struct minix2_inode *inode = Inode2 + ROOT_INO;
806
807 if (!inode || !S_ISDIR(inode->i_mode))
808 die("Root inode isn't a directory");
809}
810#endif
811
812static int add_zone(uint16_t *znr, int *corrected)
813{
814 int result;
815 int block;
816
817 result = 0;
818 block = check_zone_nr(znr, corrected);
819 if (!block)
820 return 0;
821 if (zone_count[block]) {
822 printf("Already used block is reused in file '%s'. ",
823 current_name);
824 if (ask("Clear", 1)) {
825 *znr = 0;
826 block = 0;
827 *corrected = 1;
828 return 0;
829 }
830 }
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))
835 mark_zone(block);
836 }
837 if (!++zone_count[block])
838 zone_count[block]--;
839 return block;
840}
841
842#ifdef CONFIG_FEATURE_MINIX2
843static int add_zone2(uint32_t *znr, int *corrected)
844{
845 int result;
846 int block;
847
848 result = 0;
849 block = check_zone_nr2(znr, corrected);
850 if (!block)
851 return 0;
852 if (zone_count[block]) {
853 printf("Already used block is reused in file '%s'. ",
854 current_name);
855 if (ask("Clear", 1)) {
856 *znr = 0;
857 block = 0;
858 *corrected = 1;
859 return 0;
860 }
861 }
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))
866 mark_zone(block);
867 }
868 if (!++zone_count[block])
869 zone_count[block]--;
870 return block;
871}
872#endif
873
874static void add_zone_ind(uint16_t *znr, int *corrected)
875{
876 static char blk[BLOCK_SIZE];
877 int i, chg_blk = 0;
878 int block;
879
880 block = add_zone(znr, corrected);
881 if (!block)
882 return;
883 read_block(block, blk);
884 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
885 add_zone(i + (uint16_t *) blk, &chg_blk);
886 if (chg_blk)
887 write_block(block, blk);
888}
889
890#ifdef CONFIG_FEATURE_MINIX2
891static void add_zone_ind2(uint32_t *znr, int *corrected)
892{
893 static char blk[BLOCK_SIZE];
894 int i, chg_blk = 0;
895 int block;
896
897 block = add_zone2(znr, corrected);
898 if (!block)
899 return;
900 read_block(block, blk);
901 for (i = 0; i < BLOCK_SIZE >> 2; i++)
902 add_zone2(i + (uint32_t *) blk, &chg_blk);
903 if (chg_blk)
904 write_block(block, blk);
905}
906#endif
907
908static void add_zone_dind(uint16_t *znr, int *corrected)
909{
910 static char blk[BLOCK_SIZE];
911 int i, blk_chg = 0;
912 int block;
913
914 block = add_zone(znr, corrected);
915 if (!block)
916 return;
917 read_block(block, blk);
918 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
919 add_zone_ind(i + (uint16_t *) blk, &blk_chg);
920 if (blk_chg)
921 write_block(block, blk);
922}
923
924#ifdef CONFIG_FEATURE_MINIX2
925static void add_zone_dind2(uint32_t *znr, int *corrected)
926{
927 static char blk[BLOCK_SIZE];
928 int i, blk_chg = 0;
929 int block;
930
931 block = add_zone2(znr, corrected);
932 if (!block)
933 return;
934 read_block(block, blk);
935 for (i = 0; i < BLOCK_SIZE >> 2; i++)
936 add_zone_ind2(i + (uint32_t *) blk, &blk_chg);
937 if (blk_chg)
938 write_block(block, blk);
939}
940
941static void add_zone_tind2(uint32_t *znr, int *corrected)
942{
943 static char blk[BLOCK_SIZE];
944 int i, blk_chg = 0;
945 int block;
946
947 block = add_zone2(znr, corrected);
948 if (!block)
949 return;
950 read_block(block, blk);
951 for (i = 0; i < BLOCK_SIZE >> 2; i++)
952 add_zone_dind2(i + (uint32_t *) blk, &blk_chg);
953 if (blk_chg)
954 write_block(block, blk);
955}
956#endif
957
958static void check_zones(unsigned int i)
959{
960 struct minix_inode *inode;
961
962 if (!i || i > INODES)
963 return;
964 if (inode_count[i] > 1) /* have we counted this file already? */
965 return;
966 inode = Inode + i;
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);
973}
974
975#ifdef CONFIG_FEATURE_MINIX2
976static void check_zones2(unsigned int i)
977{
978 struct minix2_inode *inode;
979
980 if (!i || i > INODES)
981 return;
982 if (inode_count[i] > 1) /* have we counted this file already? */
983 return;
984 inode = Inode2 + i;
985 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
986 && !S_ISLNK(inode->i_mode))
987 return;
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);
993}
994#endif
995
996static void check_file(struct minix_inode *dir, unsigned int offset)
997{
998 static char blk[BLOCK_SIZE];
999 struct minix_inode *inode;
1000 int ino;
1001 char *name;
1002 int block;
1003
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);
1008 if (ino > INODES) {
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);
1014 }
1015 ino = 0;
1016 }
1017 push_filename(name);
1018 inode = get_inode(ino);
1019 pop_filename();
1020 if (!offset) {
1021 if (!inode || strcmp(".", name)) {
1022 printf("%s: bad directory: '.' isn't first\n", current_name);
1023 errors_uncorrected = 1;
1024 } else
1025 return;
1026 }
1027 if (offset == dirsize) {
1028 if (!inode || strcmp("..", name)) {
1029 printf("%s: bad directory: '..' isn't second\n", current_name);
1030 errors_uncorrected = 1;
1031 } else
1032 return;
1033 }
1034 if (!inode)
1035 return;
1036 push_filename(name);
1037 if (list) {
1038 if (verbose)
1039 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1040 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
1041 }
1042 check_zones(ino);
1043 if (inode && S_ISDIR(inode->i_mode))
1044 recursive_check(ino);
1045 pop_filename();
1046 return;
1047}
1048
1049#ifdef CONFIG_FEATURE_MINIX2
1050static void check_file2(struct minix2_inode *dir, unsigned int offset)
1051{
1052 static char blk[BLOCK_SIZE];
1053 struct minix2_inode *inode;
1054 int ino;
1055 char *name;
1056 int block;
1057
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);
1062 if (ino > INODES) {
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);
1068 }
1069 ino = 0;
1070 }
1071 push_filename(name);
1072 inode = get_inode2(ino);
1073 pop_filename();
1074 if (!offset) {
1075 if (!inode || strcmp(".", name)) {
1076 printf("%s: bad directory: '.' isn't first\n", current_name);
1077 errors_uncorrected = 1;
1078 } else
1079 return;
1080 }
1081 if (offset == dirsize) {
1082 if (!inode || strcmp("..", name)) {
1083 printf("%s: bad directory: '..' isn't second\n", current_name);
1084 errors_uncorrected = 1;
1085 } else
1086 return;
1087 }
1088 if (!inode)
1089 return;
1090 push_filename(name);
1091 if (list) {
1092 if (verbose)
1093 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1094 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
1095 }
1096 check_zones2(ino);
1097 if (inode && S_ISDIR(inode->i_mode))
1098 recursive_check2(ino);
1099 pop_filename();
1100 return;
1101}
1102#endif
1103
1104static void recursive_check(unsigned int ino)
1105{
1106 struct minix_inode *dir;
1107 unsigned int offset;
1108
1109 dir = Inode + ino;
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;
1115 }
1116 for (offset = 0; offset < dir->i_size; offset += dirsize)
1117 check_file(dir, offset);
1118}
1119
1120#ifdef CONFIG_FEATURE_MINIX2
1121static void recursive_check2(unsigned int ino)
1122{
1123 struct minix2_inode *dir;
1124 unsigned int offset;
1125
1126 dir = Inode2 + ino;
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;
1132 }
1133 for (offset = 0; offset < dir->i_size; offset += dirsize)
1134 check_file2(dir, offset);
1135}
1136#endif
1137
1138static int bad_zone(int i)
1139{
1140 char buffer[1024];
1141
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));
1145}
1146
1147static void check_counts(void)
1148{
1149 int i;
1150
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;
1156 changed = 1;
1157 }
1158 }
1159 if (!inode_count[i]) {
1160 if (!inode_in_use(i))
1161 continue;
1162 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
1163 if (ask("Clear", 1))
1164 unmark_inode(i);
1165 continue;
1166 }
1167 if (!inode_in_use(i)) {
1168 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
1169 if (ask("Set", 1))
1170 mark_inode(i);
1171 }
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];
1177 changed = 1;
1178 }
1179 }
1180 }
1181 for (i = FIRSTZONE; i < ZONES; i++) {
1182 if (zone_in_use(i) == zone_count[i])
1183 continue;
1184 if (!zone_count[i]) {
1185 if (bad_zone(i))
1186 continue;
1187 printf("Zone %d is marked 'in use', but no file uses it. ", i);
1188 if (ask("Unmark", 1))
1189 unmark_zone(i);
1190 continue;
1191 }
1192 printf("Zone %d: %sin use, counted=%d\n",
1193 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1194 }
1195}
1196
1197#ifdef CONFIG_FEATURE_MINIX2
1198static void check_counts2(void)
1199{
1200 int i;
1201
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;
1207 changed = 1;
1208 }
1209 }
1210 if (!inode_count[i]) {
1211 if (!inode_in_use(i))
1212 continue;
1213 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
1214 if (ask("Clear", 1))
1215 unmark_inode(i);
1216 continue;
1217 }
1218 if (!inode_in_use(i)) {
1219 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
1220 if (ask("Set", 1))
1221 mark_inode(i);
1222 }
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,
1226 inode_count[i]);
1227 if (ask("Set i_nlinks to count", 1)) {
1228 Inode2[i].i_nlinks = inode_count[i];
1229 changed = 1;
1230 }
1231 }
1232 }
1233 for (i = FIRSTZONE; i < ZONES; i++) {
1234 if (zone_in_use(i) == zone_count[i])
1235 continue;
1236 if (!zone_count[i]) {
1237 if (bad_zone(i))
1238 continue;
1239 printf("Zone %d is marked 'in use', but no file uses it. ", i);
1240 if (ask("Unmark", 1))
1241 unmark_zone(i);
1242 continue;
1243 }
1244 printf("Zone %d: %sin use, counted=%d\n",
1245 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1246 }
1247}
1248#endif
1249
1250static void check(void)
1251{
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);
1256 check_counts();
1257}
1258
1259#ifdef CONFIG_FEATURE_MINIX2
1260static void check2(void)
1261{
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);
1266 check_counts2();
1267}
1268#endif
1269
1270int fsck_minix_main(int argc, char **argv)
1271{
1272 struct termios tmp;
1273 int retcode = 0;
1274
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);
1280#endif
1281
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");
1287#endif
1288 while (argc-- > 1) {
1289 argv++;
1290 if (argv[0][0] != '-') {
1291 if (device_name)
1292 bb_show_usage();
1293 else
1294 device_name = argv[0];
1295 } else
1296 while (*++argv[0])
1297 switch (argv[0][0]) {
1298 case 'l':
1299 list = 1;
1300 break;
1301 case 'a':
1302 automatic = 1;
1303 repair = 1;
1304 break;
1305 case 'r':
1306 automatic = 0;
1307 repair = 1;
1308 break;
1309 case 'v':
1310 verbose = 1;
1311 break;
1312 case 's':
1313 show = 1;
1314 break;
1315 case 'm':
1316 warn_mode = 1;
1317 break;
1318 case 'f':
1319 force = 1;
1320 break;
1321 default:
1322 bb_show_usage();
1323 }
1324 }
1325 if (!device_name)
1326 bb_show_usage();
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");
1331 }
1332 IN = open(device_name, repair ? O_RDWR : O_RDONLY);
1333 if (IN < 0){
1334 printf("Unable to open device '%s'\n", device_name);
1335 leave(8);
1336 }
1337 sync(); /* paranoia? */
1338 read_superblock();
1339
1340 /*
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
1344 * command line.
1345 */
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) {
1349 if (repair)
1350 printf("%s is clean, check is skipped\n", device_name);
1351 return retcode;
1352 } else if (force)
1353 printf("Forcing filesystem check on %s\n", device_name);
1354 else if (repair)
1355 printf("Filesystem on %s is dirty, needs checking\n",
1356 device_name);
1357
1358 read_tables();
1359
1360 if (repair && !automatic) {
1361 tcgetattr(0, &termios);
1362 tmp = termios;
1363 tmp.c_lflag &= ~(ICANON | ECHO);
1364 tcsetattr(0, TCSANOW, &tmp);
1365 termios_set = 1;
1366 }
1367#ifdef CONFIG_FEATURE_MINIX2
1368 if (version2) {
1369 check_root2();
1370 check2();
1371 } else
1372#endif
1373 {
1374 check_root();
1375 check();
1376 }
1377 if (verbose) {
1378 int i, free_cnt;
1379
1380 for (i = 1, free_cnt = 0; i <= INODES; i++)
1381 if (!inode_in_use(i))
1382 free_cnt++;
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))
1387 free_cnt++;
1388 printf("%6ld zones used (%ld%%)\n\n"
1389 "%6d regular files\n"
1390 "%6d directories\n"
1391 "%6d character device files\n"
1392 "%6d block device files\n"
1393 "%6d links\n"
1394 "%6d symbolic links\n"
1395 "------\n"
1396 "%6d files\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);
1401 }
1402 if (changed) {
1403 write_tables();
1404 printf("FILE SYSTEM HAS BEEN CHANGED\n");
1405 sync();
1406 } else if (repair)
1407 write_super_block();
1408
1409 if (repair && !automatic)
1410 tcsetattr(0, TCSANOW, &termios);
1411
1412 if (changed)
1413 retcode += 3;
1414 if (errors_uncorrected)
1415 retcode += 4;
1416 return retcode;
1417}