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