]> git.ipfire.org Git - thirdparty/util-linux.git/blob - disk-utils/fsck.minix.c
fsck.minix: fix printf format warning
[thirdparty/util-linux.git] / disk-utils / fsck.minix.c
1 /*
2 * fsck.minix.c - a file system consistency checker for Linux.
3 *
4 * (C) 1991, 1992 Linus Torvalds. This file may be redistributed
5 * as per the GNU copyleft.
6 */
7
8 /*
9 * 09.11.91 - made the first rudimetary functions
10 *
11 * 10.11.91 - updated, does checking, no repairs yet.
12 * Sent out to the mailing-list for testing.
13 *
14 * 14.11.91 - Testing seems to have gone well. Added some
15 * correction-code, and changed some functions.
16 *
17 * 15.11.91 - More correction code. Hopefully it notices most
18 * cases now, and tries to do something about them.
19 *
20 * 16.11.91 - More corrections (thanks to Mika Jalava). Most
21 * things seem to work now. Yeah, sure.
22 *
23 *
24 * 19.04.92 - Had to start over again from this old version, as a
25 * kernel bug ate my enhanced fsck in february.
26 *
27 * 28.02.93 - added support for different directory entry sizes..
28 *
29 * Sat Mar 6 18:59:42 1993, faith@cs.unc.edu: Output namelen with
30 * super-block information
31 *
32 * Sat Oct 9 11:17:11 1993, faith@cs.unc.edu: make exit status conform
33 * to that required by fsutil
34 *
35 * Mon Jan 3 11:06:52 1994 - Dr. Wettstein (greg%wind.uucp@plains.nodak.edu)
36 * Added support for file system valid flag. Also
37 * added program_version variable and output of
38 * program name and version number when program
39 * is executed.
40 *
41 * 30.10.94 - added support for v2 filesystem
42 * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de)
43 *
44 * 10.12.94 - added test to prevent checking of mounted fs adapted
45 * from Theodore Ts'o's (tytso@athena.mit.edu) e2fsck
46 * program. (Daniel Quinlan, quinlan@yggdrasil.com)
47 *
48 * 01.07.96 - Fixed the v2 fs stuff to use the right #defines and such
49 * for modern libcs (janl@math.uio.no, Nicolai Langfeldt)
50 *
51 * 02.07.96 - Added C bit fiddling routines from rmk@ecs.soton.ac.uk
52 * (Russell King). He made them for ARM. It would seem
53 * that the ARM is powerful enough to do this in C whereas
54 * i386 and m64k must use assembly to get it fast >:-)
55 * This should make minix fsck systemindependent.
56 * (janl@math.uio.no, Nicolai Langfeldt)
57 *
58 * 04.11.96 - Added minor fixes from Andreas Schwab to avoid compiler
59 * warnings. Added mc68k bitops from
60 * Joerg Dorchain <dorchain@mpi-sb.mpg.de>.
61 *
62 * 06.11.96 - Added v2 code submitted by Joerg Dorchain, but written by
63 * Andreas Schwab.
64 *
65 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
66 * - added Native Language Support
67 *
68 * 2008-04-06 James Youngman <jay@gnu.org>
69 * - Issue better error message if we fail to open the device.
70 * - Restore terminal state if we get a fatal signal.
71 *
72 *
73 * I've had no time to add comments - hopefully the function names
74 * are comments enough. As with all file system checkers, this assumes
75 * the file system is quiescent - don't use it on a mounted device
76 * unless you can be sure nobody is writing to it (and remember that the
77 * kernel can write to it when it searches for files).
78 *
79 * Usuage: fsck [-larvsm] device
80 * -l for a listing of all the filenames
81 * -a for automatic repairs (not implemented)
82 * -r for repairs (interactive) (not implemented)
83 * -v for verbose (tells how many files)
84 * -s for super-block info
85 * -m for minix-like "mode not cleared" warnings
86 * -f force filesystem check even if filesystem marked as valid
87 *
88 * The device may be a block device or a image of one, but this isn't
89 * enforced (but it's not much fun on a character device :-).
90 */
91
92 #include <stdio.h>
93 #include <stdarg.h>
94 #include <errno.h>
95 #include <unistd.h>
96 #include <string.h>
97 #include <fcntl.h>
98 #include <ctype.h>
99 #include <stdlib.h>
100 #include <termios.h>
101 #include <mntent.h>
102 #include <sys/stat.h>
103 #include <signal.h>
104
105 #include "c.h"
106 #include "exitcodes.h"
107 #include "minix_programs.h"
108 #include "nls.h"
109 #include "pathnames.h"
110 #include "bitops.h"
111 #include "ismounted.h"
112 #include "writeall.h"
113
114 #define ROOT_INO 1
115 #define YESNO_LENGTH 64
116
117 /* Global variables used in minix_programs.h inline fuctions */
118 int fs_version = 1;
119 char *super_block_buffer;
120
121 static char *inode_buffer;
122
123 #define Inode (((struct minix_inode *) inode_buffer) - 1)
124 #define Inode2 (((struct minix2_inode *) inode_buffer) - 1)
125
126 static char *device_name;
127 static int IN;
128 static int repair, automatic, verbose, list, show, warn_mode, force;
129 static int directory, regular, blockdev, chardev, links, symlinks, total;
130
131 static int changed; /* flags if the filesystem has been changed */
132 static int errors_uncorrected; /* flag if some error was not corrected */
133 static size_t dirsize = 16;
134 static size_t namelen = 14;
135 static struct termios termios;
136 static volatile sig_atomic_t termios_set;
137
138 /* File-name data */
139 #define MAX_DEPTH 50
140 static int name_depth;
141 static char name_list[MAX_DEPTH][MINIX_NAME_MAX + 1];
142
143 /* Copy of the previous, just for error reporting - see get_current_name. This
144 * is a waste of 12kB or so. */
145 static char current_name[MAX_DEPTH * (MINIX_NAME_MAX + 1) + 1];
146
147 #define MAGIC (Super.s_magic)
148
149 static unsigned char *inode_count = NULL;
150 static unsigned char *zone_count = NULL;
151
152 static void recursive_check(unsigned int ino);
153 static void recursive_check2(unsigned int ino);
154
155 static char *inode_map;
156 static char *zone_map;
157
158 #define inode_in_use(x) (isset(inode_map,(x)) != 0)
159 #define zone_in_use(x) (isset(zone_map,(x)-get_first_zone()+1) != 0)
160
161 #define mark_inode(x) (setbit(inode_map,(x)),changed=1)
162 #define unmark_inode(x) (clrbit(inode_map,(x)),changed=1)
163
164 #define mark_zone(x) (setbit(zone_map,(x)-get_first_zone()+1),changed=1)
165 #define unmark_zone(x) (clrbit(zone_map,(x)-get_first_zone()+1),changed=1)
166
167 static void
168 reset(void) {
169 if (termios_set)
170 tcsetattr(0, TCSANOW, &termios);
171 }
172
173 static void
174 fatalsig(int sig) {
175 /* We received a fatal signal. Reset the terminal. Also reset the
176 * signal handler and re-send the signal, so that the parent process
177 * knows which signal actually caused our death. */
178 signal(sig, SIG_DFL);
179 reset();
180 raise(sig);
181 }
182
183 static void
184 leave(int status) {
185 reset();
186 exit(status);
187 }
188
189 static void
190 usage(void) {
191 fputs(USAGE_HEADER, stderr);
192 fprintf(stderr,
193 _(" %s [options] <device>\n"), program_invocation_short_name);
194 fputs(USAGE_OPTIONS, stderr);
195 fputs(_(" -l list all filenames\n"), stderr);
196 fputs(_(" -a automatic repair\n"), stderr);
197 fputs(_(" -r interactive repair\n"), stderr);
198 fputs(_(" -v be verbose\n"), stderr);
199 fputs(_(" -s output super-block information\n"), stderr);
200 fputs(_(" -m activate mode not cleared warnings\n"), stderr);
201 fputs(_(" -f force check\n"), stderr);
202 fputs(USAGE_SEPARATOR, stderr);
203 fputs(USAGE_VERSION, stderr);
204 fprintf(stderr, USAGE_MAN_TAIL("fsck.minix(8)"));
205 leave(FSCK_EX_USAGE);
206 }
207
208 static void die(const char *fmt, ...)
209 __attribute__ ((__format__(__printf__, 1, 2)));
210
211 static void
212 die(const char *fmt, ...) {
213 va_list ap;
214
215 fprintf(stderr, "%s: ", program_invocation_short_name);
216 va_start(ap, fmt);
217 vfprintf(stderr, fmt, ap);
218 va_end(ap);
219 fputc('\n', stderr);
220 leave(FSCK_EX_ERROR);
221 }
222
223 /* This simply goes through the file-name data and prints out the current file. */
224 static void
225 get_current_name(void) {
226 int i = 0, ct;
227 char *p, *q;
228
229 q = current_name;
230 while (i < name_depth) {
231 p = name_list[i++];
232 ct = namelen;
233 *q++ = '/';
234 while (ct-- && *p)
235 *q++ = *p++;
236 }
237 if (i == 0)
238 *q++ = '/';
239 *q = 0;
240 }
241
242 static int
243 ask(const char *string, int def) {
244 int resp;
245 char input[YESNO_LENGTH];
246
247 if (!repair) {
248 printf("\n");
249 errors_uncorrected = 1;
250 return 0;
251 }
252 if (automatic) {
253 printf("\n");
254 if (!def)
255 errors_uncorrected = 1;
256 return def;
257 }
258 /* TRANSLATORS: these yes no questions uses rpmatch(), and should be
259 * translated. */
260 printf(def ? _("%s (y/n)? ") : _("%s (n/y)? "), string);
261 fflush(stdout);
262 fgets(input, YESNO_LENGTH, stdin);
263 resp = rpmatch(input);
264 switch (resp) {
265 case -1:
266 /* def = def */
267 break;
268 case 0:
269 case 1:
270 def = resp;
271 break;
272 default:
273 /* rpmatch bug? */
274 abort();
275 }
276 if (def)
277 printf(_("y\n"));
278 else {
279 printf(_("n\n"));
280 errors_uncorrected = 1;
281 }
282 return def;
283 }
284
285 /* Make certain that we aren't checking a filesystem that is on a mounted
286 * partition. Code adapted from e2fsck, Copyright (C) 1993, 1994 Theodore
287 * Ts'o. Also licensed under GPL. */
288 static void
289 check_mount(void) {
290 int cont;
291
292 if (!is_mounted(device_name))
293 return;
294
295 printf(_("%s is mounted. "), device_name);
296 if (isatty(0) && isatty(1))
297 cont = ask(_("Do you really want to continue"), 0);
298 else
299 cont = 0;
300 if (!cont) {
301 printf(_("check aborted.\n"));
302 exit(FSCK_EX_OK);
303 }
304 return;
305 }
306
307 /* check_zone_nr checks to see that *nr is a valid zone nr. If it isn't, it
308 * will possibly be repaired. Check_zone_nr sets *corrected if an error was
309 * corrected, and returns the zone (0 for no zone or a bad zone-number). */
310 static int
311 check_zone_nr(unsigned short *nr, int *corrected) {
312 if (!*nr)
313 return 0;
314
315 if (*nr < get_first_zone()) {
316 get_current_name();
317 printf(_("Zone nr < FIRSTZONE in file `%s'."), current_name);
318 } else if (*nr >= get_nzones()) {
319 get_current_name();
320 printf(_("Zone nr >= ZONES in file `%s'."), current_name);
321 } else
322 return *nr;
323
324 if (ask(_("Remove block"), 1)) {
325 *nr = 0;
326 *corrected = 1;
327 }
328 return 0;
329 }
330
331 static int
332 check_zone_nr2(unsigned int *nr, int *corrected) {
333 if (!*nr)
334 return 0;
335
336 if (*nr < get_first_zone()) {
337 get_current_name();
338 printf(_("Zone nr < FIRSTZONE in file `%s'."), current_name);
339 } else if (*nr >= get_nzones()) {
340 get_current_name();
341 printf(_("Zone nr >= ZONES in file `%s'."), current_name);
342 } else
343 return *nr;
344
345 if (ask(_("Remove block"), 1)) {
346 *nr = 0;
347 *corrected = 1;
348 }
349 return 0;
350 }
351
352 /* read-block reads block nr into the buffer at addr. */
353 static void
354 read_block(unsigned int nr, char *addr) {
355 if (!nr) {
356 memset(addr, 0, MINIX_BLOCK_SIZE);
357 return;
358 }
359 if (MINIX_BLOCK_SIZE * nr != lseek(IN, MINIX_BLOCK_SIZE * nr, SEEK_SET)) {
360 get_current_name();
361 printf(_("Read error: unable to seek to block in file '%s'\n"),
362 current_name);
363 memset(addr, 0, MINIX_BLOCK_SIZE);
364 errors_uncorrected = 1;
365 } else if (MINIX_BLOCK_SIZE != read(IN, addr, MINIX_BLOCK_SIZE)) {
366 get_current_name();
367 printf(_("Read error: bad block in file '%s'\n"), current_name);
368 memset(addr, 0, MINIX_BLOCK_SIZE);
369 errors_uncorrected = 1;
370 }
371 }
372
373 /* write_block writes block nr to disk. */
374 static void
375 write_block(unsigned int nr, char *addr) {
376 if (!nr)
377 return;
378 if (nr < get_first_zone() || nr >= get_nzones()) {
379 printf(_("Internal error: trying to write bad block\n"
380 "Write request ignored\n"));
381 errors_uncorrected = 1;
382 return;
383 }
384 if (MINIX_BLOCK_SIZE * nr != lseek(IN, MINIX_BLOCK_SIZE * nr, SEEK_SET))
385 die(_("seek failed in write_block"));
386 if (MINIX_BLOCK_SIZE != write(IN, addr, MINIX_BLOCK_SIZE)) {
387 get_current_name();
388 printf(_("Write error: bad block in file '%s'\n"),
389 current_name);
390 errors_uncorrected = 1;
391 }
392 }
393
394 /* map-block calculates the absolute block nr of a block in a file. It sets
395 * 'changed' if the inode has needed changing, and re-writes any indirect
396 * blocks with errors. */
397 static int
398 map_block(struct minix_inode *inode, unsigned int blknr) {
399 unsigned short ind[MINIX_BLOCK_SIZE >> 1];
400 unsigned short dind[MINIX_BLOCK_SIZE >> 1];
401 int blk_chg, block, result;
402
403 if (blknr < 7)
404 return check_zone_nr(inode->i_zone + blknr, &changed);
405 blknr -= 7;
406 if (blknr < 512) {
407 block = check_zone_nr(inode->i_zone + 7, &changed);
408 read_block(block, (char *)ind);
409 blk_chg = 0;
410 result = check_zone_nr(blknr + ind, &blk_chg);
411 if (blk_chg)
412 write_block(block, (char *)ind);
413 return result;
414 }
415 blknr -= 512;
416 block = check_zone_nr(inode->i_zone + 8, &changed);
417 read_block(block, (char *)dind);
418 blk_chg = 0;
419 result = check_zone_nr(dind + (blknr / 512), &blk_chg);
420 if (blk_chg)
421 write_block(block, (char *)dind);
422 block = result;
423 read_block(block, (char *)ind);
424 blk_chg = 0;
425 result = check_zone_nr(ind + (blknr % 512), &blk_chg);
426 if (blk_chg)
427 write_block(block, (char *)ind);
428 return result;
429 }
430
431 static int
432 map_block2(struct minix2_inode *inode, unsigned int blknr) {
433 unsigned int ind[MINIX_BLOCK_SIZE >> 2];
434 unsigned int dind[MINIX_BLOCK_SIZE >> 2];
435 unsigned int tind[MINIX_BLOCK_SIZE >> 2];
436 int blk_chg, block, result;
437
438 if (blknr < 7)
439 return check_zone_nr2(inode->i_zone + blknr, &changed);
440 blknr -= 7;
441 if (blknr < 256) {
442 block = check_zone_nr2(inode->i_zone + 7, &changed);
443 read_block(block, (char *)ind);
444 blk_chg = 0;
445 result = check_zone_nr2(blknr + ind, &blk_chg);
446 if (blk_chg)
447 write_block(block, (char *)ind);
448 return result;
449 }
450 blknr -= 256;
451 if (blknr < 256 * 256) {
452 block = check_zone_nr2(inode->i_zone + 8, &changed);
453 read_block(block, (char *)dind);
454 blk_chg = 0;
455 result = check_zone_nr2(dind + blknr / 256, &blk_chg);
456 if (blk_chg)
457 write_block(block, (char *)dind);
458 block = result;
459 read_block(block, (char *)ind);
460 blk_chg = 0;
461 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
462 if (blk_chg)
463 write_block(block, (char *)ind);
464 return result;
465 }
466 blknr -= 256 * 256;
467 block = check_zone_nr2(inode->i_zone + 9, &changed);
468 read_block(block, (char *)tind);
469 blk_chg = 0;
470 result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg);
471 if (blk_chg)
472 write_block(block, (char *)tind);
473 block = result;
474 read_block(block, (char *)dind);
475 blk_chg = 0;
476 result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg);
477 if (blk_chg)
478 write_block(block, (char *)dind);
479 block = result;
480 read_block(block, (char *)ind);
481 blk_chg = 0;
482 result = check_zone_nr2(ind + blknr % 256, &blk_chg);
483 if (blk_chg)
484 write_block(block, (char *)ind);
485 return result;
486 }
487
488 static void
489 write_super_block(void) {
490 /* Set the state of the filesystem based on whether or not there are
491 * uncorrected errors. The filesystem valid flag is unconditionally
492 * set if we get this far. */
493 Super.s_state |= MINIX_VALID_FS;
494 if (errors_uncorrected)
495 Super.s_state |= MINIX_ERROR_FS;
496 else
497 Super.s_state &= ~MINIX_ERROR_FS;
498
499 if (MINIX_BLOCK_SIZE != lseek(IN, MINIX_BLOCK_SIZE, SEEK_SET))
500 die(_("seek failed in write_super_block"));
501 if (MINIX_BLOCK_SIZE != write(IN, super_block_buffer, MINIX_BLOCK_SIZE))
502 die(_("unable to write super-block"));
503 return;
504 }
505
506 static void
507 write_tables(void) {
508 write_super_block();
509 unsigned long buffsz = get_inode_buffer_size();
510 unsigned long imaps = get_nimaps();
511 unsigned long zmaps = get_nzmaps();
512
513 if (write_all(IN, inode_map, imaps * MINIX_BLOCK_SIZE))
514 die(_("Unable to write inode map"));
515
516 if (write_all(IN, zone_map, zmaps * MINIX_BLOCK_SIZE))
517 die(_("Unable to write zone map"));
518
519 if (write_all(IN, inode_buffer, buffsz))
520 die(_("Unable to write inodes"));
521 }
522
523 static void
524 get_dirsize(void) {
525 int block;
526 char blk[MINIX_BLOCK_SIZE];
527 size_t size;
528
529 if (fs_version == 2)
530 block = Inode2[ROOT_INO].i_zone[0];
531 else
532 block = Inode[ROOT_INO].i_zone[0];
533 read_block(block, blk);
534
535 for (size = 16; size < MINIX_BLOCK_SIZE; size <<= 1) {
536 if (strcmp(blk + size + 2, "..") == 0) {
537 dirsize = size;
538 namelen = size - 2;
539 return;
540 }
541 }
542 /* use defaults */
543 }
544
545 static void
546 read_superblock(void) {
547 if (MINIX_BLOCK_SIZE != lseek(IN, MINIX_BLOCK_SIZE, SEEK_SET))
548 die(_("seek failed"));
549
550 super_block_buffer = calloc(1, MINIX_BLOCK_SIZE);
551 if (!super_block_buffer)
552 die(_("unable to alloc buffer for superblock"));
553
554 if (MINIX_BLOCK_SIZE != read(IN, super_block_buffer, MINIX_BLOCK_SIZE))
555 die(_("unable to read super block"));
556 if (MAGIC == MINIX_SUPER_MAGIC) {
557 namelen = 14;
558 dirsize = 16;
559 fs_version = 1;
560 } else if (MAGIC == MINIX_SUPER_MAGIC2) {
561 namelen = 30;
562 dirsize = 32;
563 fs_version = 1;
564 } else if (MAGIC == MINIX2_SUPER_MAGIC) {
565 namelen = 14;
566 dirsize = 16;
567 fs_version = 2;
568 } else if (MAGIC == MINIX2_SUPER_MAGIC2) {
569 namelen = 30;
570 dirsize = 32;
571 fs_version = 2;
572 } else
573 die(_("bad magic number in super-block"));
574 if (get_zone_size() != 0 || MINIX_BLOCK_SIZE != 1024)
575 die(_("Only 1k blocks/zones supported"));
576 if (get_nimaps() * MINIX_BLOCK_SIZE * 8 < get_ninodes() + 1)
577 die(_("bad s_imap_blocks field in super-block"));
578 if (get_nzmaps() * MINIX_BLOCK_SIZE * 8 <
579 get_nzones() - get_first_zone() + 1)
580 die(_("bad s_zmap_blocks field in super-block"));
581 }
582
583 static void
584 read_tables(void) {
585 unsigned long inodes = get_ninodes();
586 unsigned long buffsz = get_inode_buffer_size();
587 unsigned long norm_first_zone = first_zone_data();
588 unsigned long first_zone = get_first_zone();
589 unsigned long zones = get_nzones();
590 unsigned long imaps = get_nimaps();
591 unsigned long zmaps = get_nzmaps();
592 ssize_t rc;
593
594 inode_map = malloc(imaps * MINIX_BLOCK_SIZE);
595 if (!inode_map)
596 die(_("Unable to allocate buffer for inode map"));
597 zone_map = malloc(zmaps * MINIX_BLOCK_SIZE);
598 if (!zone_map)
599 die(_("Unable to allocate buffer for zone map"));
600 inode_buffer = malloc(buffsz);
601 if (!inode_buffer)
602 die(_("Unable to allocate buffer for inodes"));
603 inode_count = calloc(1, inodes + 1);
604 if (!inode_count)
605 die(_("Unable to allocate buffer for inode count"));
606 zone_count = calloc(1, zones);
607 if (!zone_count)
608 die(_("Unable to allocate buffer for zone count"));
609
610 rc = read(IN, inode_map, imaps * MINIX_BLOCK_SIZE);
611 if (rc < 0 || imaps * MINIX_BLOCK_SIZE != (size_t) rc)
612 die(_("Unable to read inode map"));
613
614 rc = read(IN, zone_map, zmaps * MINIX_BLOCK_SIZE);
615 if (rc < 0 || zmaps * MINIX_BLOCK_SIZE != (size_t) rc)
616 die(_("Unable to read zone map"));
617
618 rc = read(IN, inode_buffer, buffsz);
619 if (rc < 0 || buffsz != (size_t) rc)
620 die(_("Unable to read inodes"));
621 if (norm_first_zone != first_zone) {
622 printf(_("Warning: Firstzone != Norm_firstzone\n"));
623 errors_uncorrected = 1;
624 }
625 get_dirsize();
626 if (show) {
627 printf(_("%ld inodes\n"), inodes);
628 printf(_("%ld blocks\n"), zones);
629 printf(_("Firstdatazone=%ld (%ld)\n"), first_zone, norm_first_zone);
630 printf(_("Zonesize=%d\n"), MINIX_BLOCK_SIZE << get_zone_size());
631 printf(_("Maxsize=%ld\n"), get_max_size());
632 printf(_("Filesystem state=%d\n"), Super.s_state);
633 printf(_("namelen=%zd\n\n"), namelen);
634 }
635 }
636
637 static struct minix_inode *
638 get_inode(unsigned int nr) {
639 struct minix_inode *inode;
640
641 if (!nr || nr > get_ninodes())
642 return NULL;
643 total++;
644 inode = Inode + nr;
645 if (!inode_count[nr]) {
646 if (!inode_in_use(nr)) {
647 get_current_name();
648 printf(_("Inode %d marked unused, "
649 "but used for file '%s'\n"), nr, current_name);
650 if (repair) {
651 if (ask(_("Mark in use"), 1))
652 mark_inode(nr);
653 } else {
654 errors_uncorrected = 1;
655 }
656 }
657 if (S_ISDIR(inode->i_mode))
658 directory++;
659 else if (S_ISREG(inode->i_mode))
660 regular++;
661 else if (S_ISCHR(inode->i_mode))
662 chardev++;
663 else if (S_ISBLK(inode->i_mode))
664 blockdev++;
665 else if (S_ISLNK(inode->i_mode))
666 symlinks++;
667 else if (S_ISSOCK(inode->i_mode))
668 ;
669 else if (S_ISFIFO(inode->i_mode))
670 ;
671 else {
672 get_current_name();
673 printf(_("The file `%s' has mode %05o\n"),
674 current_name, inode->i_mode);
675 }
676
677 } else
678 links++;
679 if (!++inode_count[nr]) {
680 printf(_("Warning: inode count too big.\n"));
681 inode_count[nr]--;
682 errors_uncorrected = 1;
683 }
684 return inode;
685 }
686
687 static struct minix2_inode *
688 get_inode2(unsigned int nr) {
689 struct minix2_inode *inode;
690
691 if (!nr || nr > get_ninodes())
692 return NULL;
693 total++;
694 inode = Inode2 + nr;
695 if (!inode_count[nr]) {
696 if (!inode_in_use(nr)) {
697 get_current_name();
698 printf(_("Inode %d marked unused, "
699 "but used for file '%s'\n"), nr, current_name);
700 if (repair) {
701 if (ask(_("Mark in use"), 1))
702 mark_inode(nr);
703 else
704 errors_uncorrected = 1;
705 }
706 }
707 if (S_ISDIR(inode->i_mode))
708 directory++;
709 else if (S_ISREG(inode->i_mode))
710 regular++;
711 else if (S_ISCHR(inode->i_mode))
712 chardev++;
713 else if (S_ISBLK(inode->i_mode))
714 blockdev++;
715 else if (S_ISLNK(inode->i_mode))
716 symlinks++;
717 else if (S_ISSOCK(inode->i_mode)) ;
718 else if (S_ISFIFO(inode->i_mode)) ;
719 else {
720 get_current_name();
721 printf(_("The file `%s' has mode %05o\n"),
722 current_name, inode->i_mode);
723 }
724 } else
725 links++;
726 if (!++inode_count[nr]) {
727 printf(_("Warning: inode count too big.\n"));
728 inode_count[nr]--;
729 errors_uncorrected = 1;
730 }
731 return inode;
732 }
733
734 static void
735 check_root(void) {
736 struct minix_inode *inode = Inode + ROOT_INO;
737
738 if (!inode || !S_ISDIR(inode->i_mode))
739 die(_("root inode isn't a directory"));
740 }
741
742 static void
743 check_root2(void) {
744 struct minix2_inode *inode = Inode2 + ROOT_INO;
745
746 if (!inode || !S_ISDIR(inode->i_mode))
747 die(_("root inode isn't a directory"));
748 }
749
750 static int
751 add_zone(unsigned short *znr, int *corrected) {
752 int block;
753
754 block = check_zone_nr(znr, corrected);
755 if (!block)
756 return 0;
757 if (zone_count[block]) {
758 get_current_name();
759 printf(_("Block has been used before. Now in file `%s'."),
760 current_name);
761 if (ask(_("Clear"), 1)) {
762 *znr = 0;
763 block = 0;
764 *corrected = 1;
765 }
766 }
767 if (!block)
768 return 0;
769 if (!zone_in_use(block)) {
770 get_current_name();
771 printf(_("Block %d in file `%s' is marked not in use."),
772 block, current_name);
773 if (ask(_("Correct"), 1))
774 mark_zone(block);
775 }
776 if (!++zone_count[block])
777 zone_count[block]--;
778 return block;
779 }
780
781 static int
782 add_zone2(unsigned int *znr, int *corrected) {
783 int block;
784
785 block = check_zone_nr2(znr, corrected);
786 if (!block)
787 return 0;
788 if (zone_count[block]) {
789 get_current_name();
790 printf(_("Block has been used before. Now in file `%s'."),
791 current_name);
792 if (ask(_("Clear"), 1)) {
793 *znr = 0;
794 block = 0;
795 *corrected = 1;
796 }
797 }
798 if (!block)
799 return 0;
800 if (!zone_in_use(block)) {
801 get_current_name();
802 printf(_("Block %d in file `%s' is marked not in use."),
803 block, current_name);
804 if (ask(_("Correct"), 1))
805 mark_zone(block);
806 }
807 if (!++zone_count[block])
808 zone_count[block]--;
809 return block;
810 }
811
812 static void
813 add_zone_ind(unsigned short *znr, int *corrected) {
814 static char blk[MINIX_BLOCK_SIZE];
815 int i, chg_blk = 0;
816 int block;
817
818 block = add_zone(znr, corrected);
819 if (!block)
820 return;
821 read_block(block, blk);
822 for (i = 0; i < (MINIX_BLOCK_SIZE >> 1); i++)
823 add_zone(i + (unsigned short *)blk, &chg_blk);
824 if (chg_blk)
825 write_block(block, blk);
826 }
827
828 static void
829 add_zone_ind2(unsigned int *znr, int *corrected) {
830 static char blk[MINIX_BLOCK_SIZE];
831 int i, chg_blk = 0;
832 int block;
833
834 block = add_zone2(znr, corrected);
835 if (!block)
836 return;
837 read_block(block, blk);
838 for (i = 0; i < MINIX_BLOCK_SIZE >> 2; i++)
839 add_zone2(i + (unsigned int *)blk, &chg_blk);
840 if (chg_blk)
841 write_block(block, blk);
842 }
843
844 static void
845 add_zone_dind(unsigned short *znr, int *corrected) {
846 static char blk[MINIX_BLOCK_SIZE];
847 int i, blk_chg = 0;
848 int block;
849
850 block = add_zone(znr, corrected);
851 if (!block)
852 return;
853 read_block(block, blk);
854 for (i = 0; i < (MINIX_BLOCK_SIZE >> 1); i++)
855 add_zone_ind(i + (unsigned short *)blk, &blk_chg);
856 if (blk_chg)
857 write_block(block, blk);
858 }
859
860 static void
861 add_zone_dind2(unsigned int *znr, int *corrected) {
862 static char blk[MINIX_BLOCK_SIZE];
863 int i, blk_chg = 0;
864 int block;
865
866 block = add_zone2(znr, corrected);
867 if (!block)
868 return;
869 read_block(block, blk);
870 for (i = 0; i < MINIX_BLOCK_SIZE >> 2; i++)
871 add_zone_ind2(i + (unsigned int *)blk, &blk_chg);
872 if (blk_chg)
873 write_block(block, blk);
874 }
875
876 static void
877 add_zone_tind2(unsigned int *znr, int *corrected) {
878 static char blk[MINIX_BLOCK_SIZE];
879 int i, blk_chg = 0;
880 int block;
881
882 block = add_zone2(znr, corrected);
883 if (!block)
884 return;
885 read_block(block, blk);
886 for (i = 0; i < MINIX_BLOCK_SIZE >> 2; i++)
887 add_zone_dind2(i + (unsigned int *)blk, &blk_chg);
888 if (blk_chg)
889 write_block(block, blk);
890 }
891
892 static void
893 check_zones(unsigned int i) {
894 struct minix_inode *inode;
895
896 if (!i || i > get_ninodes())
897 return;
898 if (inode_count[i] > 1) /* have we counted this file already? */
899 return;
900 inode = Inode + i;
901 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
902 !S_ISLNK(inode->i_mode))
903 return;
904 for (i = 0; i < 7; i++)
905 add_zone(i + inode->i_zone, &changed);
906 add_zone_ind(7 + inode->i_zone, &changed);
907 add_zone_dind(8 + inode->i_zone, &changed);
908 }
909
910 static void
911 check_zones2(unsigned int i) {
912 struct minix2_inode *inode;
913
914 if (!i || i > get_ninodes())
915 return;
916 if (inode_count[i] > 1) /* have we counted this file already? */
917 return;
918 inode = Inode2 + i;
919 if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode)
920 && !S_ISLNK(inode->i_mode))
921 return;
922 for (i = 0; i < 7; i++)
923 add_zone2(i + inode->i_zone, &changed);
924 add_zone_ind2(7 + inode->i_zone, &changed);
925 add_zone_dind2(8 + inode->i_zone, &changed);
926 add_zone_tind2(9 + inode->i_zone, &changed);
927 }
928
929 static void
930 check_file(struct minix_inode *dir, unsigned int offset) {
931 static char blk[MINIX_BLOCK_SIZE];
932 struct minix_inode *inode;
933 unsigned int ino;
934 char *name;
935 int block;
936
937 block = map_block(dir, offset / MINIX_BLOCK_SIZE);
938 read_block(block, blk);
939 name = blk + (offset % MINIX_BLOCK_SIZE) + 2;
940 ino = *(unsigned short *)(name - 2);
941 if (ino > get_ninodes()) {
942 get_current_name();
943 printf(_("The directory '%s' contains a bad inode number "
944 "for file '%.*s'."), current_name, (int)namelen, name);
945 if (ask(_(" Remove"), 1)) {
946 *(unsigned short *)(name - 2) = 0;
947 write_block(block, blk);
948 }
949 ino = 0;
950 }
951 if (name_depth < MAX_DEPTH)
952 strncpy(name_list[name_depth], name, namelen);
953 name_depth++;
954 inode = get_inode(ino);
955 name_depth--;
956 if (!offset) {
957 if (!inode || strcmp(".", name)) {
958 get_current_name();
959 printf(_("%s: bad directory: '.' isn't first\n"),
960 current_name);
961 errors_uncorrected = 1;
962 } else
963 return;
964 }
965 if (offset == dirsize) {
966 if (!inode || strcmp("..", name)) {
967 get_current_name();
968 printf(_("%s: bad directory: '..' isn't second\n"),
969 current_name);
970 errors_uncorrected = 1;
971 } else
972 return;
973 }
974 if (!inode)
975 return;
976 if (name_depth < MAX_DEPTH)
977 strncpy(name_list[name_depth], name, namelen);
978 name_depth++;
979 if (list) {
980 if (verbose)
981 printf("%6d %07o %3d ", ino,
982 inode->i_mode, inode->i_nlinks);
983 get_current_name();
984 printf("%s", current_name);
985 if (S_ISDIR(inode->i_mode))
986 printf(":\n");
987 else
988 printf("\n");
989 }
990 check_zones(ino);
991 if (inode && S_ISDIR(inode->i_mode))
992 recursive_check(ino);
993 name_depth--;
994 return;
995 }
996
997 static void
998 check_file2(struct minix2_inode *dir, unsigned int offset) {
999 static char blk[MINIX_BLOCK_SIZE];
1000 struct minix2_inode *inode;
1001 unsigned long ino;
1002 char *name;
1003 int block;
1004
1005 block = map_block2(dir, offset / MINIX_BLOCK_SIZE);
1006 read_block(block, blk);
1007 name = blk + (offset % MINIX_BLOCK_SIZE) + 2;
1008 ino = *(unsigned short *)(name - 2);
1009 if (ino > get_ninodes()) {
1010 get_current_name();
1011 printf(_("The directory '%s' contains a bad inode number "
1012 "for file '%.*s'."), current_name, (int)namelen, name);
1013 if (ask(_(" Remove"), 1)) {
1014 *(unsigned short *)(name - 2) = 0;
1015 write_block(block, blk);
1016 }
1017 ino = 0;
1018 }
1019 if (name_depth < MAX_DEPTH)
1020 strncpy(name_list[name_depth], name, namelen);
1021 name_depth++;
1022 inode = get_inode2(ino);
1023 name_depth--;
1024 if (!offset) {
1025 if (!inode || strcmp(".", name)) {
1026 get_current_name();
1027 printf(_("%s: bad directory: '.' isn't first\n"),
1028 current_name);
1029 errors_uncorrected = 1;
1030 } else
1031 return;
1032 }
1033 if (offset == dirsize) {
1034 if (!inode || strcmp("..", name)) {
1035 get_current_name();
1036 printf(_("%s: bad directory: '..' isn't second\n"),
1037 current_name);
1038 errors_uncorrected = 1;
1039 } else
1040 return;
1041 }
1042 if (!inode)
1043 return;
1044 name_depth++;
1045 if (list) {
1046 if (verbose)
1047 printf("%6zd %07o %3d ", ino, inode->i_mode,
1048 inode->i_nlinks);
1049 get_current_name();
1050 printf("%s", current_name);
1051 if (S_ISDIR(inode->i_mode))
1052 printf(":\n");
1053 else
1054 printf("\n");
1055 }
1056 check_zones2(ino);
1057 if (inode && S_ISDIR(inode->i_mode))
1058 recursive_check2(ino);
1059 name_depth--;
1060 return;
1061 }
1062
1063 static void
1064 recursive_check(unsigned int ino) {
1065 struct minix_inode *dir;
1066 unsigned int offset;
1067
1068 dir = Inode + ino;
1069 if (!S_ISDIR(dir->i_mode))
1070 die(_("internal error"));
1071 if (dir->i_size < 2 * dirsize) {
1072 get_current_name();
1073 printf(_("%s: bad directory: size < 32"), current_name);
1074 errors_uncorrected = 1;
1075 }
1076 for (offset = 0; offset < dir->i_size; offset += dirsize)
1077 check_file(dir, offset);
1078 }
1079
1080 static void
1081 recursive_check2(unsigned int ino) {
1082 struct minix2_inode *dir;
1083 unsigned int offset;
1084
1085 dir = Inode2 + ino;
1086 if (!S_ISDIR(dir->i_mode))
1087 die(_("internal error"));
1088 if (dir->i_size < 2 * dirsize) {
1089 get_current_name();
1090 printf(_("%s: bad directory: size < 32"), current_name);
1091 errors_uncorrected = 1;
1092 }
1093 for (offset = 0; offset < dir->i_size; offset += dirsize)
1094 check_file2(dir, offset);
1095 }
1096
1097 static int
1098 bad_zone(int i) {
1099 char buffer[1024];
1100
1101 if (MINIX_BLOCK_SIZE * i != lseek(IN, MINIX_BLOCK_SIZE * i, SEEK_SET))
1102 die(_("seek failed in bad_zone"));
1103 return (MINIX_BLOCK_SIZE != read(IN, buffer, MINIX_BLOCK_SIZE));
1104 }
1105
1106 static void
1107 check_counts(void) {
1108 unsigned long i;
1109
1110 for (i = 1; i <= get_ninodes(); i++) {
1111 if (!inode_in_use(i) && Inode[i].i_mode && warn_mode) {
1112 printf(_("Inode %lu mode not cleared."), i);
1113 if (ask(_("Clear"), 1)) {
1114 Inode[i].i_mode = 0;
1115 changed = 1;
1116 }
1117 }
1118 if (!inode_count[i]) {
1119 if (!inode_in_use(i))
1120 continue;
1121 printf(_("Inode %lu not used, marked used in the bitmap."), i);
1122 if (ask(_("Clear"), 1))
1123 unmark_inode(i);
1124 continue;
1125 }
1126 if (!inode_in_use(i)) {
1127 printf(_("Inode %lu used, marked unused in the bitmap."), i);
1128 if (ask(_("Set"), 1))
1129 mark_inode(i);
1130 }
1131 if (Inode[i].i_nlinks != inode_count[i]) {
1132 printf(_("Inode %lu (mode = %07o), i_nlinks=%d, counted=%d."),
1133 i, Inode[i].i_mode, Inode[i].i_nlinks,
1134 inode_count[i]);
1135 if (ask(_("Set i_nlinks to count"), 1)) {
1136 Inode[i].i_nlinks = inode_count[i];
1137 changed = 1;
1138 }
1139 }
1140 }
1141 for (i = get_first_zone(); i < get_nzones(); i++) {
1142 if (zone_in_use(i) == zone_count[i])
1143 continue;
1144 if (!zone_count[i]) {
1145 if (bad_zone(i))
1146 continue;
1147 printf(_("Zone %lu: marked in use, no file uses it."),
1148 i);
1149 if (ask(_("Unmark"), 1))
1150 unmark_zone(i);
1151 continue;
1152 }
1153 if (zone_in_use(i))
1154 printf(_("Zone %lu: in use, counted=%d\n"),
1155 i, zone_count[i]);
1156 else
1157 printf(_("Zone %lu: not in use, counted=%d\n"),
1158 i, zone_count[i]);
1159 }
1160 }
1161
1162 static void
1163 check_counts2(void) {
1164 unsigned long i;
1165
1166 for (i = 1; i <= get_ninodes(); i++) {
1167 if (!inode_in_use(i) && Inode2[i].i_mode && warn_mode) {
1168 printf(_("Inode %lu mode not cleared."), i);
1169 if (ask(_("Clear"), 1)) {
1170 Inode2[i].i_mode = 0;
1171 changed = 1;
1172 }
1173 }
1174 if (!inode_count[i]) {
1175 if (!inode_in_use(i))
1176 continue;
1177 printf(_("Inode %lu not used, marked used in the bitmap."), i);
1178 if (ask(_("Clear"), 1))
1179 unmark_inode(i);
1180 continue;
1181 }
1182 if (!inode_in_use(i)) {
1183 printf(_("Inode %lu used, marked unused in the bitmap."), i);
1184 if (ask(_("Set"), 1))
1185 mark_inode(i);
1186 }
1187 if (Inode2[i].i_nlinks != inode_count[i]) {
1188 printf(_("Inode %lu (mode = %07o), i_nlinks=%d, counted=%d."),
1189 i, Inode2[i].i_mode, Inode2[i].i_nlinks,
1190 inode_count[i]);
1191 if (ask(_("Set i_nlinks to count"), 1)) {
1192 Inode2[i].i_nlinks = inode_count[i];
1193 changed = 1;
1194 }
1195 }
1196 }
1197 for (i = get_first_zone(); i < get_nzones(); i++) {
1198 if (zone_in_use(i) == zone_count[i])
1199 continue;
1200 if (!zone_count[i]) {
1201 if (bad_zone(i))
1202 continue;
1203 printf(_("Zone %lu: marked in use, no file uses it."),
1204 i);
1205 if (ask(_("Unmark"), 1))
1206 unmark_zone(i);
1207 continue;
1208 }
1209 if (zone_in_use(i))
1210 printf(_("Zone %lu: in use, counted=%d\n"),
1211 i, zone_count[i]);
1212 else
1213 printf(_("Zone %lu: not in use, counted=%d\n"),
1214 i, zone_count[i]);
1215 }
1216 }
1217
1218 static void
1219 check(void) {
1220 memset(inode_count, 0, (get_ninodes() + 1) * sizeof(*inode_count));
1221 memset(zone_count, 0, get_nzones() * sizeof(*zone_count));
1222 check_zones(ROOT_INO);
1223 recursive_check(ROOT_INO);
1224 check_counts();
1225 }
1226
1227 static void
1228 check2(void) {
1229 memset(inode_count, 0, (get_ninodes() + 1) * sizeof(*inode_count));
1230 memset(zone_count, 0, get_nzones() * sizeof(*zone_count));
1231 check_zones2(ROOT_INO);
1232 recursive_check2(ROOT_INO);
1233 check_counts2();
1234 }
1235
1236 int
1237 main(int argc, char **argv) {
1238 struct termios tmp;
1239 int count;
1240 int retcode = 0;
1241
1242 setlocale(LC_ALL, "");
1243 bindtextdomain(PACKAGE, LOCALEDIR);
1244 textdomain(PACKAGE);
1245
1246 if (argc == 2 &&
1247 (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version"))) {
1248 printf(UTIL_LINUX_VERSION);
1249 exit(FSCK_EX_OK);
1250 }
1251
1252 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != MINIX_BLOCK_SIZE)
1253 die(_("bad inode size"));
1254 if (INODE2_SIZE * MINIX2_INODES_PER_BLOCK != MINIX_BLOCK_SIZE)
1255 die(_("bad v2 inode size"));
1256
1257 while (argc-- > 1) {
1258 argv++;
1259 if (argv[0][0] != '-') {
1260 if (device_name)
1261 usage();
1262 else
1263 device_name = argv[0];
1264 } else
1265 while (*++argv[0])
1266 switch (argv[0][0]) {
1267 case 'l':
1268 list = 1;
1269 break;
1270 case 'a':
1271 automatic = 1;
1272 repair = 1;
1273 break;
1274 case 'r':
1275 automatic = 0;
1276 repair = 1;
1277 break;
1278 case 'v':
1279 verbose = 1;
1280 break;
1281 case 's':
1282 show = 1;
1283 break;
1284 case 'm':
1285 warn_mode = 1;
1286 break;
1287 case 'f':
1288 force = 1;
1289 break;
1290 default:
1291 usage();
1292 }
1293 }
1294 if (!device_name)
1295 usage();
1296 check_mount(); /* trying to check a mounted filesystem? */
1297 if (repair && !automatic) {
1298 if (!isatty(0) || !isatty(1))
1299 die(_("need terminal for interactive repairs"));
1300 }
1301 IN = open(device_name, repair ? O_RDWR : O_RDONLY);
1302 if (IN < 0)
1303 die(_("unable to open '%s': %s"), device_name, strerror(errno));
1304 for (count = 0; count < 3; count++)
1305 sync();
1306 read_superblock();
1307
1308 /* Determine whether or not we should continue with the checking. This
1309 * is based on the status of the filesystem valid and error flags and
1310 * whether or not the -f switch was specified on the command line. */
1311 if (!(Super.s_state & MINIX_ERROR_FS) &&
1312 (Super.s_state & MINIX_VALID_FS) && !force) {
1313 if (repair)
1314 printf(_("%s is clean, no check.\n"), device_name);
1315 return retcode;
1316 } else if (force)
1317 printf(_("Forcing filesystem check on %s.\n"), device_name);
1318 else if (repair)
1319 printf(_("Filesystem on %s is dirty, needs checking.\n"),
1320 device_name);
1321
1322 read_tables();
1323
1324 /* Restore the terminal state on fatal signals. We don't do this for
1325 * SIGALRM, SIGUSR1 or SIGUSR2. */
1326 signal(SIGINT, fatalsig);
1327 signal(SIGQUIT, fatalsig);
1328 signal(SIGTERM, fatalsig);
1329
1330 if (repair && !automatic) {
1331 tcgetattr(0, &termios);
1332 tmp = termios;
1333 tmp.c_lflag &= ~(ICANON | ECHO);
1334 tcsetattr(0, TCSANOW, &tmp);
1335 termios_set = 1;
1336 }
1337
1338 if (fs_version == 2) {
1339 check_root2();
1340 check2();
1341 } else {
1342 check_root();
1343 check();
1344 }
1345 if (verbose) {
1346 unsigned long i, free;
1347
1348 for (i = 1, free = 0; i <= get_ninodes(); i++)
1349 if (!inode_in_use(i))
1350 free++;
1351 printf(_("\n%6ld inodes used (%ld%%)\n"),
1352 (get_ninodes() - free),
1353 100 * (get_ninodes() - free) / get_ninodes());
1354 for (i = get_first_zone(), free = 0; i < get_nzones(); i++)
1355 if (!zone_in_use(i))
1356 free++;
1357 printf(_("%6ld zones used (%ld%%)\n"), (get_nzones() - free),
1358 100 * (get_nzones() - free) / get_nzones());
1359 printf(_("\n%6d regular files\n"
1360 "%6d directories\n"
1361 "%6d character device files\n"
1362 "%6d block device files\n"
1363 "%6d links\n"
1364 "%6d symbolic links\n"
1365 "------\n"
1366 "%6d files\n"),
1367 regular, directory, chardev, blockdev,
1368 links - 2 * directory + 1, symlinks,
1369 total - 2 * directory + 1);
1370 }
1371 if (changed) {
1372 write_tables();
1373 printf(_("----------------------------\n"
1374 "FILE SYSTEM HAS BEEN CHANGED\n"
1375 "----------------------------\n"));
1376 for (count = 0; count < 3; count++)
1377 sync();
1378 } else if (repair)
1379 write_super_block();
1380
1381 if (repair && !automatic)
1382 tcsetattr(0, TCSANOW, &termios);
1383
1384 if (changed)
1385 retcode += 3;
1386 if (errors_uncorrected)
1387 retcode += 4;
1388 return retcode;
1389 }