1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90#include <mntent.h>
91#include "libbb.h"
92#include "minix.h"
93
94#ifndef BLKGETSIZE
95#define BLKGETSIZE _IO(0x12,96)
96#endif
97
98struct BUG_bad_inode_size {
99 char BUG_bad_inode1_size[(INODE_SIZE1 * MINIX1_INODES_PER_BLOCK != BLOCK_SIZE) ? -1 : 1];
100#if ENABLE_FEATURE_MINIX2
101 char BUG_bad_inode2_size[(INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE) ? -1 : 1];
102#endif
103};
104
105enum {
106#ifdef UNUSED
107 MINIX1_LINK_MAX = 250,
108 MINIX2_LINK_MAX = 65530,
109 MINIX_I_MAP_SLOTS = 8,
110 MINIX_Z_MAP_SLOTS = 64,
111 MINIX_V1 = 0x0001,
112 MINIX_V2 = 0x0002,
113#endif
114 MINIX_NAME_MAX = 255,
115};
116
117
118#if !ENABLE_FEATURE_MINIX2
119enum { version2 = 0 };
120#endif
121
122enum { MAX_DEPTH = 32 };
123
124enum { dev_fd = 3 };
125
126struct globals {
127#if ENABLE_FEATURE_MINIX2
128 smallint version2;
129#endif
130 smallint changed;
131 smallint errors_uncorrected;
132 smallint termios_set;
133 smallint dirsize;
134 smallint namelen;
135 const char *device_name;
136 int directory, regular, blockdev, chardev, links, symlinks, total;
137 char *inode_buffer;
138
139 char *inode_map;
140 char *zone_map;
141
142 unsigned char *inode_count;
143 unsigned char *zone_count;
144
145
146 int name_depth;
147 char *name_component[MAX_DEPTH+1];
148
149
150 struct termios sv_termios;
151 char superblock_buffer[BLOCK_SIZE];
152 char add_zone_ind_blk[BLOCK_SIZE];
153 char add_zone_dind_blk[BLOCK_SIZE];
154 USE_FEATURE_MINIX2(char add_zone_tind_blk[BLOCK_SIZE];)
155 char check_file_blk[BLOCK_SIZE];
156
157
158 char current_name[MAX_DEPTH * MINIX_NAME_MAX];
159};
160
161#define G (*ptr_to_globals)
162#if ENABLE_FEATURE_MINIX2
163#define version2 (G.version2 )
164#endif
165#define changed (G.changed )
166#define errors_uncorrected (G.errors_uncorrected )
167#define termios_set (G.termios_set )
168#define dirsize (G.dirsize )
169#define namelen (G.namelen )
170#define device_name (G.device_name )
171#define directory (G.directory )
172#define regular (G.regular )
173#define blockdev (G.blockdev )
174#define chardev (G.chardev )
175#define links (G.links )
176#define symlinks (G.symlinks )
177#define total (G.total )
178#define inode_buffer (G.inode_buffer )
179#define inode_map (G.inode_map )
180#define zone_map (G.zone_map )
181#define inode_count (G.inode_count )
182#define zone_count (G.zone_count )
183#define name_depth (G.name_depth )
184#define name_component (G.name_component )
185#define sv_termios (G.sv_termios )
186#define superblock_buffer (G.superblock_buffer )
187#define add_zone_ind_blk (G.add_zone_ind_blk )
188#define add_zone_dind_blk (G.add_zone_dind_blk )
189#define add_zone_tind_blk (G.add_zone_tind_blk )
190#define check_file_blk (G.check_file_blk )
191#define current_name (G.current_name )
192#define INIT_G() do { \
193 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
194 dirsize = 16; \
195 namelen = 14; \
196 current_name[0] = '/'; \
197 \
198 name_component[0] = ¤t_name[0]; \
199} while (0)
200
201
202#define OPTION_STR "larvsmf"
203enum {
204 OPT_l = (1 << 0),
205 OPT_a = (1 << 1),
206 OPT_r = (1 << 2),
207 OPT_v = (1 << 3),
208 OPT_s = (1 << 4),
209 OPT_w = (1 << 5),
210 OPT_f = (1 << 6),
211};
212#define OPT_list (option_mask32 & OPT_l)
213#define OPT_automatic (option_mask32 & OPT_a)
214#define OPT_repair (option_mask32 & OPT_r)
215#define OPT_verbose (option_mask32 & OPT_v)
216#define OPT_show (option_mask32 & OPT_s)
217#define OPT_warn_mode (option_mask32 & OPT_w)
218#define OPT_force (option_mask32 & OPT_f)
219
220#define OPT_manual ((option_mask32 & (OPT_a|OPT_r)) == OPT_r)
221
222
223#define Inode1 (((struct minix1_inode *) inode_buffer)-1)
224#define Inode2 (((struct minix2_inode *) inode_buffer)-1)
225
226#define Super (*(struct minix_superblock *)(superblock_buffer))
227
228#if ENABLE_FEATURE_MINIX2
229# define ZONES ((unsigned)(version2 ? Super.s_zones : Super.s_nzones))
230#else
231# define ZONES ((unsigned)(Super.s_nzones))
232#endif
233#define INODES ((unsigned)Super.s_ninodes)
234#define IMAPS ((unsigned)Super.s_imap_blocks)
235#define ZMAPS ((unsigned)Super.s_zmap_blocks)
236#define FIRSTZONE ((unsigned)Super.s_firstdatazone)
237#define ZONESIZE ((unsigned)Super.s_log_zone_size)
238#define MAXSIZE ((unsigned)Super.s_max_size)
239#define MAGIC (Super.s_magic)
240
241
242static ALWAYS_INLINE unsigned div_roundup(unsigned size, unsigned n)
243{
244 return (size + n-1) / n;
245}
246
247#if !ENABLE_FEATURE_MINIX2
248#define INODE_BLOCKS div_roundup(INODES, MINIX1_INODES_PER_BLOCK)
249#else
250#define INODE_BLOCKS div_roundup(INODES, \
251 (version2 ? MINIX2_INODES_PER_BLOCK : MINIX1_INODES_PER_BLOCK))
252#endif
253
254#define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE)
255#define NORM_FIRSTZONE (2 + IMAPS + ZMAPS + INODE_BLOCKS)
256
257
258
259
260static int minix_bit(const char *a, unsigned i)
261{
262 return (a[i >> 3] & (1<<(i & 7)));
263}
264
265static void minix_setbit(char *a, unsigned i)
266{
267 setbit(a, i);
268 changed = 1;
269}
270static void minix_clrbit(char *a, unsigned i)
271{
272 clrbit(a, i);
273 changed = 1;
274}
275
276
277#define zone_in_use(x) (minix_bit(zone_map,(x)-FIRSTZONE+1))
278#define inode_in_use(x) (minix_bit(inode_map,(x)))
279
280#define mark_inode(x) (minix_setbit(inode_map,(x)))
281#define unmark_inode(x) (minix_clrbit(inode_map,(x)))
282
283#define mark_zone(x) (minix_setbit(zone_map,(x)-FIRSTZONE+1))
284#define unmark_zone(x) (minix_clrbit(zone_map,(x)-FIRSTZONE+1))
285
286
287static void recursive_check(unsigned ino);
288#if ENABLE_FEATURE_MINIX2
289static void recursive_check2(unsigned ino);
290#endif
291
292static void die(const char *str) NORETURN;
293static void die(const char *str)
294{
295 if (termios_set)
296 tcsetattr_stdin_TCSANOW(&sv_termios);
297 bb_error_msg_and_die("%s", str);
298}
299
300static void push_filename(const char *name)
301{
302
303
304
305 if (name_depth < MAX_DEPTH) {
306 int len;
307 char *p = name_component[name_depth];
308 *p++ = '/';
309 len = sprintf(p, "%.*s", namelen, name);
310 name_component[name_depth + 1] = p + len;
311 }
312 name_depth++;
313}
314
315static void pop_filename(void)
316{
317 name_depth--;
318 if (name_depth < MAX_DEPTH) {
319 *name_component[name_depth] = '\0';
320 if (!name_depth) {
321 current_name[0] = '/';
322 current_name[1] = '\0';
323 }
324 }
325}
326
327static int ask(const char *string, int def)
328{
329 int c;
330
331 if (!OPT_repair) {
332 bb_putchar('\n');
333 errors_uncorrected = 1;
334 return 0;
335 }
336 if (OPT_automatic) {
337 bb_putchar('\n');
338 if (!def)
339 errors_uncorrected = 1;
340 return def;
341 }
342 printf(def ? "%s (y/n)? " : "%s (n/y)? ", string);
343 for (;;) {
344 fflush(stdout);
345 c = getchar();
346 if (c == EOF) {
347 if (!def)
348 errors_uncorrected = 1;
349 return def;
350 }
351 c = toupper(c);
352 if (c == 'Y') {
353 def = 1;
354 break;
355 } else if (c == 'N') {
356 def = 0;
357 break;
358 } else if (c == ' ' || c == '\n')
359 break;
360 }
361 if (def)
362 printf("y\n");
363 else {
364 printf("n\n");
365 errors_uncorrected = 1;
366 }
367 return def;
368}
369
370
371
372
373
374
375static void check_mount(void)
376{
377 FILE *f;
378 struct mntent *mnt;
379 int cont;
380 int fd;
381
382 f = setmntent(MOUNTED, "r");
383 if (f == NULL)
384 return;
385 while ((mnt = getmntent(f)) != NULL)
386 if (strcmp(device_name, mnt->mnt_fsname) == 0)
387 break;
388 endmntent(f);
389 if (!mnt)
390 return;
391
392
393
394
395
396
397 fd = open(MOUNTED, O_RDWR);
398 if (fd < 0 && errno == EROFS)
399 return;
400 close(fd);
401
402 printf("%s is mounted. ", device_name);
403 cont = 0;
404 if (isatty(0) && isatty(1))
405 cont = ask("Do you really want to continue", 0);
406 if (!cont) {
407 printf("Check aborted\n");
408 exit(EXIT_SUCCESS);
409 }
410}
411
412
413
414
415
416
417
418static int check_zone_nr2(uint32_t *nr, smallint *corrected)
419{
420 const char *msg;
421 if (!*nr)
422 return 0;
423 if (*nr < FIRSTZONE)
424 msg = "< FIRSTZONE";
425 else if (*nr >= ZONES)
426 msg = ">= ZONES";
427 else
428 return *nr;
429 printf("Zone nr %s in file '%s'. ", msg, current_name);
430 if (ask("Remove block", 1)) {
431 *nr = 0;
432 *corrected = 1;
433 }
434 return 0;
435}
436
437static int check_zone_nr(uint16_t *nr, smallint *corrected)
438{
439 uint32_t nr32 = *nr;
440 int r = check_zone_nr2(&nr32, corrected);
441 *nr = (uint16_t)nr32;
442 return r;
443}
444
445
446
447
448static void read_block(unsigned nr, void *addr)
449{
450 if (!nr) {
451 memset(addr, 0, BLOCK_SIZE);
452 return;
453 }
454 xlseek(dev_fd, BLOCK_SIZE * nr, SEEK_SET);
455 if (BLOCK_SIZE != full_read(dev_fd, addr, BLOCK_SIZE)) {
456 printf("%s: bad block %u in file '%s'\n",
457 bb_msg_read_error, nr, current_name);
458 errors_uncorrected = 1;
459 memset(addr, 0, BLOCK_SIZE);
460 }
461}
462
463
464
465
466static void write_block(unsigned nr, void *addr)
467{
468 if (!nr)
469 return;
470 if (nr < FIRSTZONE || nr >= ZONES) {
471 printf("Internal error: trying to write bad block\n"
472 "Write request ignored\n");
473 errors_uncorrected = 1;
474 return;
475 }
476 xlseek(dev_fd, BLOCK_SIZE * nr, SEEK_SET);
477 if (BLOCK_SIZE != full_write(dev_fd, addr, BLOCK_SIZE)) {
478 printf("%s: bad block %u in file '%s'\n",
479 bb_msg_write_error, nr, current_name);
480 errors_uncorrected = 1;
481 }
482}
483
484
485
486
487
488
489static int map_block(struct minix1_inode *inode, unsigned blknr)
490{
491 uint16_t ind[BLOCK_SIZE >> 1];
492 int block, result;
493 smallint blk_chg;
494
495 if (blknr < 7)
496 return check_zone_nr(inode->i_zone + blknr, &changed);
497 blknr -= 7;
498 if (blknr < 512) {
499 block = check_zone_nr(inode->i_zone + 7, &changed);
500 goto common;
501 }
502 blknr -= 512;
503 block = check_zone_nr(inode->i_zone + 8, &changed);
504 read_block(block, ind);
505 blk_chg = 0;
506 result = check_zone_nr(&ind[blknr / 512], &blk_chg);
507 if (blk_chg)
508 write_block(block, ind);
509 block = result;
510 common:
511 read_block(block, ind);
512 blk_chg = 0;
513 result = check_zone_nr(&ind[blknr % 512], &blk_chg);
514 if (blk_chg)
515 write_block(block, ind);
516 return result;
517}
518
519#if ENABLE_FEATURE_MINIX2
520static int map_block2(struct minix2_inode *inode, unsigned blknr)
521{
522 uint32_t ind[BLOCK_SIZE >> 2];
523 int block, result;
524 smallint blk_chg;
525
526 if (blknr < 7)
527 return check_zone_nr2(inode->i_zone + blknr, &changed);
528 blknr -= 7;
529 if (blknr < 256) {
530 block = check_zone_nr2(inode->i_zone + 7, &changed);
531 goto common2;
532 }
533 blknr -= 256;
534 if (blknr < 256 * 256) {
535 block = check_zone_nr2(inode->i_zone + 8, &changed);
536 goto common1;
537 }
538 blknr -= 256 * 256;
539 block = check_zone_nr2(inode->i_zone + 9, &changed);
540 read_block(block, ind);
541 blk_chg = 0;
542 result = check_zone_nr2(&ind[blknr / (256 * 256)], &blk_chg);
543 if (blk_chg)
544 write_block(block, ind);
545 block = result;
546 common1:
547 read_block(block, ind);
548 blk_chg = 0;
549 result = check_zone_nr2(&ind[(blknr / 256) % 256], &blk_chg);
550 if (blk_chg)
551 write_block(block, ind);
552 block = result;
553 common2:
554 read_block(block, ind);
555 blk_chg = 0;
556 result = check_zone_nr2(&ind[blknr % 256], &blk_chg);
557 if (blk_chg)
558 write_block(block, ind);
559 return result;
560}
561#endif
562
563static void write_superblock(void)
564{
565
566
567
568
569
570 Super.s_state |= MINIX_VALID_FS | MINIX_ERROR_FS;
571 if (!errors_uncorrected)
572 Super.s_state &= ~MINIX_ERROR_FS;
573
574 xlseek(dev_fd, BLOCK_SIZE, SEEK_SET);
575 if (BLOCK_SIZE != full_write(dev_fd, superblock_buffer, BLOCK_SIZE))
576 die("cannot write superblock");
577}
578
579static void write_tables(void)
580{
581 write_superblock();
582
583 if (IMAPS * BLOCK_SIZE != write(dev_fd, inode_map, IMAPS * BLOCK_SIZE))
584 die("cannot write inode map");
585 if (ZMAPS * BLOCK_SIZE != write(dev_fd, zone_map, ZMAPS * BLOCK_SIZE))
586 die("cannot write zone map");
587 if (INODE_BUFFER_SIZE != write(dev_fd, inode_buffer, INODE_BUFFER_SIZE))
588 die("cannot write inodes");
589}
590
591static void get_dirsize(void)
592{
593 int block;
594 char blk[BLOCK_SIZE];
595 int size;
596
597#if ENABLE_FEATURE_MINIX2
598 if (version2)
599 block = Inode2[MINIX_ROOT_INO].i_zone[0];
600 else
601#endif
602 block = Inode1[MINIX_ROOT_INO].i_zone[0];
603 read_block(block, blk);
604 for (size = 16; size < BLOCK_SIZE; size <<= 1) {
605 if (strcmp(blk + size + 2, "..") == 0) {
606 dirsize = size;
607 namelen = size - 2;
608 return;
609 }
610 }
611
612}
613
614static void read_superblock(void)
615{
616 xlseek(dev_fd, BLOCK_SIZE, SEEK_SET);
617 if (BLOCK_SIZE != full_read(dev_fd, superblock_buffer, BLOCK_SIZE))
618 die("cannot read superblock");
619
620
621
622
623
624 if (MAGIC == MINIX1_SUPER_MAGIC) {
625 } else if (MAGIC == MINIX1_SUPER_MAGIC2) {
626 namelen = 30;
627 dirsize = 32;
628#if ENABLE_FEATURE_MINIX2
629 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
630 version2 = 1;
631 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
632 namelen = 30;
633 dirsize = 32;
634 version2 = 1;
635#endif
636 } else
637 die("bad magic number in superblock");
638 if (ZONESIZE != 0 || BLOCK_SIZE != 1024)
639 die("only 1k blocks/zones supported");
640 if (IMAPS * BLOCK_SIZE * 8 < INODES + 1)
641 die("bad s_imap_blocks field in superblock");
642 if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1)
643 die("bad s_zmap_blocks field in superblock");
644}
645
646static void read_tables(void)
647{
648 inode_map = xzalloc(IMAPS * BLOCK_SIZE);
649 zone_map = xzalloc(ZMAPS * BLOCK_SIZE);
650 inode_buffer = xmalloc(INODE_BUFFER_SIZE);
651 inode_count = xmalloc(INODES + 1);
652 zone_count = xmalloc(ZONES);
653 if (IMAPS * BLOCK_SIZE != read(dev_fd, inode_map, IMAPS * BLOCK_SIZE))
654 die("cannot read inode map");
655 if (ZMAPS * BLOCK_SIZE != read(dev_fd, zone_map, ZMAPS * BLOCK_SIZE))
656 die("cannot read zone map");
657 if (INODE_BUFFER_SIZE != read(dev_fd, inode_buffer, INODE_BUFFER_SIZE))
658 die("cannot read inodes");
659 if (NORM_FIRSTZONE != FIRSTZONE) {
660 printf("warning: firstzone!=norm_firstzone\n");
661 errors_uncorrected = 1;
662 }
663 get_dirsize();
664 if (OPT_show) {
665 printf("%u inodes\n"
666 "%u blocks\n"
667 "Firstdatazone=%u (%u)\n"
668 "Zonesize=%u\n"
669 "Maxsize=%u\n"
670 "Filesystem state=%u\n"
671 "namelen=%u\n\n",
672 INODES,
673 ZONES,
674 FIRSTZONE, NORM_FIRSTZONE,
675 BLOCK_SIZE << ZONESIZE,
676 MAXSIZE,
677 Super.s_state,
678 namelen);
679 }
680}
681
682static void get_inode_common(unsigned nr, uint16_t i_mode)
683{
684 total++;
685 if (!inode_count[nr]) {
686 if (!inode_in_use(nr)) {
687 printf("Inode %d is marked as 'unused', but it is used "
688 "for file '%s'\n", nr, current_name);
689 if (OPT_repair) {
690 if (ask("Mark as 'in use'", 1))
691 mark_inode(nr);
692 else
693 errors_uncorrected = 1;
694 }
695 }
696 if (S_ISDIR(i_mode))
697 directory++;
698 else if (S_ISREG(i_mode))
699 regular++;
700 else if (S_ISCHR(i_mode))
701 chardev++;
702 else if (S_ISBLK(i_mode))
703 blockdev++;
704 else if (S_ISLNK(i_mode))
705 symlinks++;
706 else if (S_ISSOCK(i_mode));
707 else if (S_ISFIFO(i_mode));
708 else {
709 printf("%s has mode %05o\n", current_name, i_mode);
710 }
711 } else
712 links++;
713 if (!++inode_count[nr]) {
714 printf("Warning: inode count too big\n");
715 inode_count[nr]--;
716 errors_uncorrected = 1;
717 }
718}
719
720static struct minix1_inode *get_inode(unsigned nr)
721{
722 struct minix1_inode *inode;
723
724 if (!nr || nr > INODES)
725 return NULL;
726 inode = Inode1 + nr;
727 get_inode_common(nr, inode->i_mode);
728 return inode;
729}
730
731#if ENABLE_FEATURE_MINIX2
732static struct minix2_inode *get_inode2(unsigned nr)
733{
734 struct minix2_inode *inode;
735
736 if (!nr || nr > INODES)
737 return NULL;
738 inode = Inode2 + nr;
739 get_inode_common(nr, inode->i_mode);
740 return inode;
741}
742#endif
743
744static void check_root(void)
745{
746 struct minix1_inode *inode = Inode1 + MINIX_ROOT_INO;
747
748 if (!inode || !S_ISDIR(inode->i_mode))
749 die("root inode isn't a directory");
750}
751
752#if ENABLE_FEATURE_MINIX2
753static void check_root2(void)
754{
755 struct minix2_inode *inode = Inode2 + MINIX_ROOT_INO;
756
757 if (!inode || !S_ISDIR(inode->i_mode))
758 die("root inode isn't a directory");
759}
760#else
761void check_root2(void);
762#endif
763
764static int add_zone_common(int block, smallint *corrected)
765{
766 if (!block)
767 return 0;
768 if (zone_count[block]) {
769 printf("Already used block is reused in file '%s'. ",
770 current_name);
771 if (ask("Clear", 1)) {
772 block = 0;
773 *corrected = 1;
774 return -1;
775 }
776 }
777 if (!zone_in_use(block)) {
778 printf("Block %d in file '%s' is marked as 'unused'. ",
779 block, current_name);
780 if (ask("Correct", 1))
781 mark_zone(block);
782 }
783 if (!++zone_count[block])
784 zone_count[block]--;
785 return block;
786}
787
788static int add_zone(uint16_t *znr, smallint *corrected)
789{
790 int block;
791
792 block = check_zone_nr(znr, corrected);
793 block = add_zone_common(block, corrected);
794 if (block == -1) {
795 *znr = 0;
796 block = 0;
797 }
798 return block;
799}
800
801#if ENABLE_FEATURE_MINIX2
802static int add_zone2(uint32_t *znr, smallint *corrected)
803{
804 int block;
805
806 block = check_zone_nr2(znr, corrected);
807 block = add_zone_common(block, corrected);
808 if (block == -1) {
809 *znr = 0;
810 block = 0;
811 }
812 return block;
813}
814#endif
815
816static void add_zone_ind(uint16_t *znr, smallint *corrected)
817{
818 int i;
819 int block;
820 smallint chg_blk = 0;
821
822 block = add_zone(znr, corrected);
823 if (!block)
824 return;
825 read_block(block, add_zone_ind_blk);
826 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
827 add_zone(i + (uint16_t *) add_zone_ind_blk, &chg_blk);
828 if (chg_blk)
829 write_block(block, add_zone_ind_blk);
830}
831
832#if ENABLE_FEATURE_MINIX2
833static void add_zone_ind2(uint32_t *znr, smallint *corrected)
834{
835 int i;
836 int block;
837 smallint chg_blk = 0;
838
839 block = add_zone2(znr, corrected);
840 if (!block)
841 return;
842 read_block(block, add_zone_ind_blk);
843 for (i = 0; i < BLOCK_SIZE >> 2; i++)
844 add_zone2(i + (uint32_t *) add_zone_ind_blk, &chg_blk);
845 if (chg_blk)
846 write_block(block, add_zone_ind_blk);
847}
848#endif
849
850static void add_zone_dind(uint16_t *znr, smallint *corrected)
851{
852 int i;
853 int block;
854 smallint chg_blk = 0;
855
856 block = add_zone(znr, corrected);
857 if (!block)
858 return;
859 read_block(block, add_zone_dind_blk);
860 for (i = 0; i < (BLOCK_SIZE >> 1); i++)
861 add_zone_ind(i + (uint16_t *) add_zone_dind_blk, &chg_blk);
862 if (chg_blk)
863 write_block(block, add_zone_dind_blk);
864}
865
866#if ENABLE_FEATURE_MINIX2
867static void add_zone_dind2(uint32_t *znr, smallint *corrected)
868{
869 int i;
870 int block;
871 smallint chg_blk = 0;
872
873 block = add_zone2(znr, corrected);
874 if (!block)
875 return;
876 read_block(block, add_zone_dind_blk);
877 for (i = 0; i < BLOCK_SIZE >> 2; i++)
878 add_zone_ind2(i + (uint32_t *) add_zone_dind_blk, &chg_blk);
879 if (chg_blk)
880 write_block(block, add_zone_dind_blk);
881}
882
883static void add_zone_tind2(uint32_t *znr, smallint *corrected)
884{
885 int i;
886 int block;
887 smallint chg_blk = 0;
888
889 block = add_zone2(znr, corrected);
890 if (!block)
891 return;
892 read_block(block, add_zone_tind_blk);
893 for (i = 0; i < BLOCK_SIZE >> 2; i++)
894 add_zone_dind2(i + (uint32_t *) add_zone_tind_blk, &chg_blk);
895 if (chg_blk)
896 write_block(block, add_zone_tind_blk);
897}
898#endif
899
900static void check_zones(unsigned i)
901{
902 struct minix1_inode *inode;
903
904 if (!i || i > INODES)
905 return;
906 if (inode_count[i] > 1)
907 return;
908 inode = Inode1 + i;
909 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
910 !S_ISLNK(inode->i_mode)) return;
911 for (i = 0; i < 7; i++)
912 add_zone(i + inode->i_zone, &changed);
913 add_zone_ind(7 + inode->i_zone, &changed);
914 add_zone_dind(8 + inode->i_zone, &changed);
915}
916
917#if ENABLE_FEATURE_MINIX2
918static void check_zones2(unsigned i)
919{
920 struct minix2_inode *inode;
921
922 if (!i || i > INODES)
923 return;
924 if (inode_count[i] > 1)
925 return;
926 inode = Inode2 + i;
927 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
928 && !S_ISLNK(inode->i_mode))
929 return;
930 for (i = 0; i < 7; i++)
931 add_zone2(i + inode->i_zone, &changed);
932 add_zone_ind2(7 + inode->i_zone, &changed);
933 add_zone_dind2(8 + inode->i_zone, &changed);
934 add_zone_tind2(9 + inode->i_zone, &changed);
935}
936#endif
937
938static void check_file(struct minix1_inode *dir, unsigned offset)
939{
940 struct minix1_inode *inode;
941 int ino;
942 char *name;
943 int block;
944
945 block = map_block(dir, offset / BLOCK_SIZE);
946 read_block(block, check_file_blk);
947 name = check_file_blk + (offset % BLOCK_SIZE) + 2;
948 ino = *(uint16_t *) (name - 2);
949 if (ino > INODES) {
950 printf("%s contains a bad inode number for file '%.*s'. ",
951 current_name, namelen, name);
952 if (ask("Remove", 1)) {
953 *(uint16_t *) (name - 2) = 0;
954 write_block(block, check_file_blk);
955 }
956 ino = 0;
957 }
958 push_filename(name);
959 inode = get_inode(ino);
960 pop_filename();
961 if (!offset) {
962 if (inode && LONE_CHAR(name, '.'))
963 return;
964 printf("%s: bad directory: '.' isn't first\n", current_name);
965 errors_uncorrected = 1;
966 }
967 if (offset == dirsize) {
968 if (inode && strcmp("..", name) == 0)
969 return;
970 printf("%s: bad directory: '..' isn't second\n", current_name);
971 errors_uncorrected = 1;
972 }
973 if (!inode)
974 return;
975 push_filename(name);
976 if (OPT_list) {
977 if (OPT_verbose)
978 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
979 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
980 }
981 check_zones(ino);
982 if (inode && S_ISDIR(inode->i_mode))
983 recursive_check(ino);
984 pop_filename();
985}
986
987#if ENABLE_FEATURE_MINIX2
988static void check_file2(struct minix2_inode *dir, unsigned offset)
989{
990 struct minix2_inode *inode;
991 int ino;
992 char *name;
993 int block;
994
995 block = map_block2(dir, offset / BLOCK_SIZE);
996 read_block(block, check_file_blk);
997 name = check_file_blk + (offset % BLOCK_SIZE) + 2;
998 ino = *(uint16_t *) (name - 2);
999 if (ino > INODES) {
1000 printf("%s contains a bad inode number for file '%.*s'. ",
1001 current_name, namelen, name);
1002 if (ask("Remove", 1)) {
1003 *(uint16_t *) (name - 2) = 0;
1004 write_block(block, check_file_blk);
1005 }
1006 ino = 0;
1007 }
1008 push_filename(name);
1009 inode = get_inode2(ino);
1010 pop_filename();
1011 if (!offset) {
1012 if (inode && LONE_CHAR(name, '.'))
1013 return;
1014 printf("%s: bad directory: '.' isn't first\n", current_name);
1015 errors_uncorrected = 1;
1016 }
1017 if (offset == dirsize) {
1018 if (inode && strcmp("..", name) == 0)
1019 return;
1020 printf("%s: bad directory: '..' isn't second\n", current_name);
1021 errors_uncorrected = 1;
1022 }
1023 if (!inode)
1024 return;
1025 push_filename(name);
1026 if (OPT_list) {
1027 if (OPT_verbose)
1028 printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks);
1029 printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : "");
1030 }
1031 check_zones2(ino);
1032 if (inode && S_ISDIR(inode->i_mode))
1033 recursive_check2(ino);
1034 pop_filename();
1035}
1036#endif
1037
1038static void recursive_check(unsigned ino)
1039{
1040 struct minix1_inode *dir;
1041 unsigned offset;
1042
1043 dir = Inode1 + ino;
1044 if (!S_ISDIR(dir->i_mode))
1045 die("internal error");
1046 if (dir->i_size < 2 * dirsize) {
1047 printf("%s: bad directory: size<32", current_name);
1048 errors_uncorrected = 1;
1049 }
1050 for (offset = 0; offset < dir->i_size; offset += dirsize)
1051 check_file(dir, offset);
1052}
1053
1054#if ENABLE_FEATURE_MINIX2
1055static void recursive_check2(unsigned ino)
1056{
1057 struct minix2_inode *dir;
1058 unsigned offset;
1059
1060 dir = Inode2 + ino;
1061 if (!S_ISDIR(dir->i_mode))
1062 die("internal error");
1063 if (dir->i_size < 2 * dirsize) {
1064 printf("%s: bad directory: size<32", current_name);
1065 errors_uncorrected = 1;
1066 }
1067 for (offset = 0; offset < dir->i_size; offset += dirsize)
1068 check_file2(dir, offset);
1069}
1070#endif
1071
1072static int bad_zone(int i)
1073{
1074 char buffer[BLOCK_SIZE];
1075
1076 xlseek(dev_fd, BLOCK_SIZE * i, SEEK_SET);
1077 return (BLOCK_SIZE != full_read(dev_fd, buffer, BLOCK_SIZE));
1078}
1079
1080static void check_counts(void)
1081{
1082 int i;
1083
1084 for (i = 1; i <= INODES; i++) {
1085 if (OPT_warn_mode && Inode1[i].i_mode && !inode_in_use(i)) {
1086 printf("Inode %d has non-zero mode. ", i);
1087 if (ask("Clear", 1)) {
1088 Inode1[i].i_mode = 0;
1089 changed = 1;
1090 }
1091 }
1092 if (!inode_count[i]) {
1093 if (!inode_in_use(i))
1094 continue;
1095 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
1096 if (ask("Clear", 1))
1097 unmark_inode(i);
1098 continue;
1099 }
1100 if (!inode_in_use(i)) {
1101 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
1102 if (ask("Set", 1))
1103 mark_inode(i);
1104 }
1105 if (Inode1[i].i_nlinks != inode_count[i]) {
1106 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
1107 i, Inode1[i].i_mode, Inode1[i].i_nlinks,
1108 inode_count[i]);
1109 if (ask("Set i_nlinks to count", 1)) {
1110 Inode1[i].i_nlinks = inode_count[i];
1111 changed = 1;
1112 }
1113 }
1114 }
1115 for (i = FIRSTZONE; i < ZONES; i++) {
1116 if ((zone_in_use(i) != 0) == zone_count[i])
1117 continue;
1118 if (!zone_count[i]) {
1119 if (bad_zone(i))
1120 continue;
1121 printf("Zone %d is marked 'in use', but no file uses it. ", i);
1122 if (ask("Unmark", 1))
1123 unmark_zone(i);
1124 continue;
1125 }
1126 printf("Zone %d: %sin use, counted=%d\n",
1127 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1128 }
1129}
1130
1131#if ENABLE_FEATURE_MINIX2
1132static void check_counts2(void)
1133{
1134 int i;
1135
1136 for (i = 1; i <= INODES; i++) {
1137 if (OPT_warn_mode && Inode2[i].i_mode && !inode_in_use(i)) {
1138 printf("Inode %d has non-zero mode. ", i);
1139 if (ask("Clear", 1)) {
1140 Inode2[i].i_mode = 0;
1141 changed = 1;
1142 }
1143 }
1144 if (!inode_count[i]) {
1145 if (!inode_in_use(i))
1146 continue;
1147 printf("Unused inode %d is marked as 'used' in the bitmap. ", i);
1148 if (ask("Clear", 1))
1149 unmark_inode(i);
1150 continue;
1151 }
1152 if (!inode_in_use(i)) {
1153 printf("Inode %d is used, but marked as 'unused' in the bitmap. ", i);
1154 if (ask("Set", 1))
1155 mark_inode(i);
1156 }
1157 if (Inode2[i].i_nlinks != inode_count[i]) {
1158 printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ",
1159 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1160 inode_count[i]);
1161 if (ask("Set i_nlinks to count", 1)) {
1162 Inode2[i].i_nlinks = inode_count[i];
1163 changed = 1;
1164 }
1165 }
1166 }
1167 for (i = FIRSTZONE; i < ZONES; i++) {
1168 if ((zone_in_use(i) != 0) == zone_count[i])
1169 continue;
1170 if (!zone_count[i]) {
1171 if (bad_zone(i))
1172 continue;
1173 printf("Zone %d is marked 'in use', but no file uses it. ", i);
1174 if (ask("Unmark", 1))
1175 unmark_zone(i);
1176 continue;
1177 }
1178 printf("Zone %d: %sin use, counted=%d\n",
1179 i, zone_in_use(i) ? "" : "not ", zone_count[i]);
1180 }
1181}
1182#endif
1183
1184static void check(void)
1185{
1186 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1187 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1188 check_zones(MINIX_ROOT_INO);
1189 recursive_check(MINIX_ROOT_INO);
1190 check_counts();
1191}
1192
1193#if ENABLE_FEATURE_MINIX2
1194static void check2(void)
1195{
1196 memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count));
1197 memset(zone_count, 0, ZONES * sizeof(*zone_count));
1198 check_zones2(MINIX_ROOT_INO);
1199 recursive_check2(MINIX_ROOT_INO);
1200 check_counts2();
1201}
1202#else
1203void check2(void);
1204#endif
1205
1206int fsck_minix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1207int fsck_minix_main(int argc UNUSED_PARAM, char **argv)
1208{
1209 struct termios tmp;
1210 int retcode = 0;
1211
1212 xfunc_error_retval = 8;
1213
1214 INIT_G();
1215
1216 opt_complementary = "=1:ar";
1217 getopt32(argv, OPTION_STR);
1218 argv += optind;
1219 device_name = argv[0];
1220
1221 check_mount();
1222 if (OPT_manual) {
1223 if (!isatty(0) || !isatty(1))
1224 die("need terminal for interactive repairs");
1225 }
1226 xmove_fd(xopen(device_name, OPT_repair ? O_RDWR : O_RDONLY), dev_fd);
1227
1228
1229 read_superblock();
1230
1231
1232
1233
1234
1235
1236
1237 printf("%s: %s\n", applet_name, bb_banner);
1238
1239 if (!(Super.s_state & MINIX_ERROR_FS)
1240 && (Super.s_state & MINIX_VALID_FS) && !OPT_force
1241 ) {
1242 if (OPT_repair)
1243 printf("%s is clean, check is skipped\n", device_name);
1244 return 0;
1245 } else if (OPT_force)
1246 printf("Forcing filesystem check on %s\n", device_name);
1247 else if (OPT_repair)
1248 printf("Filesystem on %s is dirty, needs checking\n",
1249 device_name);
1250
1251 read_tables();
1252
1253 if (OPT_manual) {
1254 tcgetattr(0, &sv_termios);
1255 tmp = sv_termios;
1256 tmp.c_lflag &= ~(ICANON | ECHO);
1257 tcsetattr_stdin_TCSANOW(&tmp);
1258 termios_set = 1;
1259 }
1260
1261 if (version2) {
1262 check_root2();
1263 check2();
1264 } else {
1265 check_root();
1266 check();
1267 }
1268
1269 if (OPT_verbose) {
1270 int i, free_cnt;
1271
1272 for (i = 1, free_cnt = 0; i <= INODES; i++)
1273 if (!inode_in_use(i))
1274 free_cnt++;
1275 printf("\n%6u inodes used (%u%%)\n", (INODES - free_cnt),
1276 100 * (INODES - free_cnt) / INODES);
1277 for (i = FIRSTZONE, free_cnt = 0; i < ZONES; i++)
1278 if (!zone_in_use(i))
1279 free_cnt++;
1280 printf("%6u zones used (%u%%)\n\n"
1281 "%6u regular files\n"
1282 "%6u directories\n"
1283 "%6u character device files\n"
1284 "%6u block device files\n"
1285 "%6u links\n"
1286 "%6u symbolic links\n"
1287 "------\n"
1288 "%6u files\n",
1289 (ZONES - free_cnt), 100 * (ZONES - free_cnt) / ZONES,
1290 regular, directory, chardev, blockdev,
1291 links - 2 * directory + 1, symlinks,
1292 total - 2 * directory + 1);
1293 }
1294 if (changed) {
1295 write_tables();
1296 printf("FILE SYSTEM HAS BEEN CHANGED\n");
1297 sync();
1298 } else if (OPT_repair)
1299 write_superblock();
1300
1301 if (OPT_manual)
1302 tcsetattr_stdin_TCSANOW(&sv_termios);
1303
1304 if (changed)
1305 retcode += 3;
1306 if (errors_uncorrected)
1307 retcode += 4;
1308 return retcode;
1309}
1310