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