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