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