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