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