Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 2 | /* |
| 3 | * fsck.c - a file system consistency checker for Linux. |
| 4 | * |
| 5 | * (C) 1991, 1992 Linus Torvalds. This file may be redistributed |
| 6 | * as per the GNU copyleft. |
| 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * 09.11.91 - made the first rudimetary functions |
| 11 | * |
| 12 | * 10.11.91 - updated, does checking, no repairs yet. |
| 13 | * Sent out to the mailing-list for testing. |
| 14 | * |
| 15 | * 14.11.91 - Testing seems to have gone well. Added some |
| 16 | * correction-code, and changed some functions. |
| 17 | * |
| 18 | * 15.11.91 - More correction code. Hopefully it notices most |
| 19 | * cases now, and tries to do something about them. |
| 20 | * |
| 21 | * 16.11.91 - More corrections (thanks to Mika Jalava). Most |
| 22 | * things seem to work now. Yeah, sure. |
| 23 | * |
| 24 | * |
| 25 | * 19.04.92 - Had to start over again from this old version, as a |
| 26 | * kernel bug ate my enhanced fsck in february. |
| 27 | * |
| 28 | * 28.02.93 - added support for different directory entry sizes.. |
| 29 | * |
| 30 | * Sat Mar 6 18:59:42 1993, faith@cs.unc.edu: Output namelen with |
| 31 | * super-block information |
| 32 | * |
| 33 | * Sat Oct 9 11:17:11 1993, faith@cs.unc.edu: make exit status conform |
| 34 | * to that required by fsutil |
| 35 | * |
| 36 | * Mon Jan 3 11:06:52 1994 - Dr. Wettstein (greg%wind.uucp@plains.nodak.edu) |
| 37 | * Added support for file system valid flag. Also |
| 38 | * added program_version variable and output of |
| 39 | * program name and version number when program |
| 40 | * is executed. |
| 41 | * |
| 42 | * 30.10.94 - added support for v2 filesystem |
| 43 | * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de) |
| 44 | * |
| 45 | * 10.12.94 - added test to prevent checking of mounted fs adapted |
| 46 | * from Theodore Ts'o's (tytso@athena.mit.edu) e2fsck |
| 47 | * program. (Daniel Quinlan, quinlan@yggdrasil.com) |
| 48 | * |
| 49 | * 01.07.96 - Fixed the v2 fs stuff to use the right #defines and such |
| 50 | * for modern libcs (janl@math.uio.no, Nicolai Langfeldt) |
| 51 | * |
| 52 | * 02.07.96 - Added C bit fiddling routines from rmk@ecs.soton.ac.uk |
| 53 | * (Russell King). He made them for ARM. It would seem |
| 54 | * that the ARM is powerful enough to do this in C whereas |
| 55 | * i386 and m64k must use assembly to get it fast >:-) |
| 56 | * This should make minix fsck systemindependent. |
| 57 | * (janl@math.uio.no, Nicolai Langfeldt) |
| 58 | * |
| 59 | * 04.11.96 - Added minor fixes from Andreas Schwab to avoid compiler |
| 60 | * warnings. Added mc68k bitops from |
| 61 | * Joerg Dorchain <dorchain@mpi-sb.mpg.de>. |
| 62 | * |
| 63 | * 06.11.96 - Added v2 code submitted by Joerg Dorchain, but written by |
| 64 | * Andreas Schwab. |
| 65 | * |
| 66 | * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org> |
| 67 | * - added Native Language Support |
| 68 | * |
| 69 | * |
| 70 | * I've had no time to add comments - hopefully the function names |
| 71 | * are comments enough. As with all file system checkers, this assumes |
| 72 | * the file system is quiescent - don't use it on a mounted device |
| 73 | * unless you can be sure nobody is writing to it (and remember that the |
| 74 | * kernel can write to it when it searches for files). |
| 75 | * |
| 76 | * Usuage: fsck [-larvsm] device |
| 77 | * -l for a listing of all the filenames |
| 78 | * -a for automatic repairs (not implemented) |
| 79 | * -r for repairs (interactive) (not implemented) |
| 80 | * -v for verbose (tells how many files) |
| 81 | * -s for super-block info |
| 82 | * -m for minix-like "mode not cleared" warnings |
| 83 | * -f force filesystem check even if filesystem marked as valid |
| 84 | * |
| 85 | * The device may be a block device or a image of one, but this isn't |
| 86 | * enforced (but it's not much fun on a character device :-). |
| 87 | */ |
| 88 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 89 | #include "internal.h" |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 90 | #include <stdio.h> |
| 91 | #include <errno.h> |
| 92 | #include <unistd.h> |
| 93 | #include <string.h> |
| 94 | #include <fcntl.h> |
| 95 | #include <ctype.h> |
| 96 | #include <stdlib.h> |
| 97 | #include <termios.h> |
| 98 | #include <mntent.h> |
| 99 | #include <sys/stat.h> |
Erik Andersen | 8ea7d8c | 2000-05-20 00:40:08 +0000 | [diff] [blame] | 100 | #include <sys/param.h> |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 101 | |
| 102 | #include <linux/fs.h> |
| 103 | #include <linux/minix_fs.h> |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 104 | |
| 105 | #ifdef MINIX2_SUPER_MAGIC2 |
| 106 | #define HAVE_MINIX2 1 |
| 107 | #endif |
| 108 | |
| 109 | #ifndef __linux__ |
| 110 | #define volatile |
| 111 | #endif |
| 112 | |
| 113 | #define ROOT_INO 1 |
| 114 | |
| 115 | #define UPPER(size,n) ((size+((n)-1))/(n)) |
| 116 | #define INODE_SIZE (sizeof(struct minix_inode)) |
| 117 | #ifdef HAVE_MINIX2 |
| 118 | #define INODE_SIZE2 (sizeof(struct minix2_inode)) |
| 119 | #define INODE_BLOCKS UPPER(INODES, (version2 ? MINIX2_INODES_PER_BLOCK \ |
| 120 | : MINIX_INODES_PER_BLOCK)) |
| 121 | #else |
| 122 | #define INODE_BLOCKS UPPER(INODES, (MINIX_INODES_PER_BLOCK)) |
| 123 | #endif |
| 124 | #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE) |
| 125 | |
| 126 | #define BITS_PER_BLOCK (BLOCK_SIZE<<3) |
| 127 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 128 | static char *program_name = "fsck.minix"; |
| 129 | static char *program_version = "1.2 - 11/11/96"; |
| 130 | static char *device_name = NULL; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 131 | static int IN; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 132 | static int repair = 0, automatic = 0, verbose = 0, list = 0, show = |
| 133 | 0, warn_mode = 0, force = 0; |
| 134 | static int directory = 0, regular = 0, blockdev = 0, chardev = 0, links = |
| 135 | 0, symlinks = 0, total = 0; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 136 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 137 | static int changed = 0; /* flags if the filesystem has been changed */ |
| 138 | static int errors_uncorrected = 0; /* flag if some error was not corrected */ |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 139 | static int dirsize = 16; |
| 140 | static int namelen = 14; |
| 141 | static int version2 = 0; |
| 142 | static struct termios termios; |
| 143 | static int termios_set = 0; |
| 144 | |
| 145 | /* File-name data */ |
John Beppu | c1dc5d9 | 2000-02-11 12:43:20 +0000 | [diff] [blame] | 146 | #define MAX_DEPTH 32 |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 147 | static int name_depth = 0; |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 148 | // static char name_list[MAX_DEPTH][BUFSIZ + 1]; |
John Beppu | 5e1b2ca | 2000-02-11 12:52:55 +0000 | [diff] [blame] | 149 | static char **name_list = NULL; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 150 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 151 | static char *inode_buffer = NULL; |
| 152 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 153 | #define Inode (((struct minix_inode *) inode_buffer)-1) |
| 154 | #define Inode2 (((struct minix2_inode *) inode_buffer)-1) |
| 155 | static char super_block_buffer[BLOCK_SIZE]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 156 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 157 | #define Super (*(struct minix_super_block *)super_block_buffer) |
| 158 | #define INODES ((unsigned long)Super.s_ninodes) |
| 159 | #ifdef HAVE_MINIX2 |
| 160 | #define ZONES ((unsigned long)(version2 ? Super.s_zones : Super.s_nzones)) |
| 161 | #else |
| 162 | #define ZONES ((unsigned long)(Super.s_nzones)) |
| 163 | #endif |
| 164 | #define IMAPS ((unsigned long)Super.s_imap_blocks) |
| 165 | #define ZMAPS ((unsigned long)Super.s_zmap_blocks) |
| 166 | #define FIRSTZONE ((unsigned long)Super.s_firstdatazone) |
| 167 | #define ZONESIZE ((unsigned long)Super.s_log_zone_size) |
| 168 | #define MAXSIZE ((unsigned long)Super.s_max_size) |
| 169 | #define MAGIC (Super.s_magic) |
| 170 | #define NORM_FIRSTZONE (2+IMAPS+ZMAPS+INODE_BLOCKS) |
| 171 | |
| 172 | static char *inode_map; |
| 173 | static char *zone_map; |
| 174 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 175 | static unsigned char *inode_count = NULL; |
| 176 | static unsigned char *zone_count = NULL; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 177 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 178 | static void recursive_check(unsigned int ino); |
| 179 | static void recursive_check2(unsigned int ino); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 180 | |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 181 | #define inode_in_use(x) (isset(inode_map,(x))) |
| 182 | #define zone_in_use(x) (isset(zone_map,(x)-FIRSTZONE+1)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 183 | |
| 184 | #define mark_inode(x) (setbit(inode_map,(x)),changed=1) |
| 185 | #define unmark_inode(x) (clrbit(inode_map,(x)),changed=1) |
| 186 | |
| 187 | #define mark_zone(x) (setbit(zone_map,(x)-FIRSTZONE+1),changed=1) |
| 188 | #define unmark_zone(x) (clrbit(zone_map,(x)-FIRSTZONE+1),changed=1) |
| 189 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 190 | static void leave(int) __attribute__ ((noreturn)); |
| 191 | static void leave(int status) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 192 | { |
| 193 | if (termios_set) |
| 194 | tcsetattr(0, TCSANOW, &termios); |
| 195 | exit(status); |
| 196 | } |
| 197 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 198 | static void show_usage(void) |
| 199 | { |
| 200 | fprintf(stderr, "BusyBox v%s (%s) multi-call binary -- GPL2\n\n", |
| 201 | BB_VER, BB_BT); |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 202 | fprintf(stderr, "Usage: %s [-larvsmf] /dev/name\n", program_name); |
| 203 | #ifndef BB_FEATURE_TRIVIAL_HELP |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 204 | fprintf(stderr, |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 205 | "\nPerforms a consistency check for MINIX filesystems.\n\n"); |
Eric Andersen | 2086e3d | 2000-07-04 22:17:01 +0000 | [diff] [blame^] | 206 | fprintf(stderr, "Options:\n"); |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 207 | fprintf(stderr, "\t-l\tLists all filenames\n"); |
| 208 | fprintf(stderr, "\t-r\tPerform interactive repairs\n"); |
| 209 | fprintf(stderr, "\t-a\tPerform automatic repairs\n"); |
| 210 | fprintf(stderr, "\t-v\tverbose\n"); |
| 211 | fprintf(stderr, "\t-s\tOutputs super-block information\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 212 | fprintf(stderr, |
| 213 | "\t-m\tActivates MINIX-like \"mode not cleared\" warnings\n"); |
Eric Andersen | d73dc5b | 1999-11-10 23:13:02 +0000 | [diff] [blame] | 214 | fprintf(stderr, "\t-f\tForce file system check.\n\n"); |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 215 | #endif |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 216 | leave(16); |
| 217 | } |
| 218 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 219 | static void die(const char *str) |
| 220 | { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 221 | fprintf(stderr, "%s: %s\n", program_name, str); |
| 222 | leave(8); |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * This simply goes through the file-name data and prints out the |
| 227 | * current file. |
| 228 | */ |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 229 | static void print_current_name(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 230 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 231 | int i = 0; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 232 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 233 | while (i < name_depth) |
| 234 | printf("/%.*s", namelen, name_list[i++]); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 235 | if (i == 0) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 236 | printf("/"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 239 | static int ask(const char *string, int def) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 240 | { |
| 241 | int c; |
| 242 | |
| 243 | if (!repair) { |
| 244 | printf("\n"); |
| 245 | errors_uncorrected = 1; |
| 246 | return 0; |
| 247 | } |
| 248 | if (automatic) { |
| 249 | printf("\n"); |
| 250 | if (!def) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 251 | errors_uncorrected = 1; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 252 | return def; |
| 253 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 254 | printf(def ? "%s (y/n)? " : "%s (n/y)? ", string); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 255 | for (;;) { |
| 256 | fflush(stdout); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 257 | if ((c = getchar()) == EOF) { |
| 258 | if (!def) |
| 259 | errors_uncorrected = 1; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 260 | return def; |
| 261 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 262 | c = toupper(c); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 263 | if (c == 'Y') { |
| 264 | def = 1; |
| 265 | break; |
| 266 | } else if (c == 'N') { |
| 267 | def = 0; |
| 268 | break; |
| 269 | } else if (c == ' ' || c == '\n') |
| 270 | break; |
| 271 | } |
| 272 | if (def) |
| 273 | printf("y\n"); |
| 274 | else { |
| 275 | printf("n\n"); |
| 276 | errors_uncorrected = 1; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 277 | } |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 278 | return def; |
| 279 | } |
| 280 | |
| 281 | /* |
| 282 | * Make certain that we aren't checking a filesystem that is on a |
| 283 | * mounted partition. Code adapted from e2fsck, Copyright (C) 1993, |
| 284 | * 1994 Theodore Ts'o. Also licensed under GPL. |
| 285 | */ |
| 286 | static void check_mount(void) |
| 287 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 288 | FILE *f; |
| 289 | struct mntent *mnt; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 290 | int cont; |
| 291 | int fd; |
| 292 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 293 | if ((f = setmntent(MOUNTED, "r")) == NULL) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 294 | return; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 295 | while ((mnt = getmntent(f)) != NULL) |
| 296 | if (strcmp(device_name, mnt->mnt_fsname) == 0) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 297 | break; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 298 | endmntent(f); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 299 | if (!mnt) |
| 300 | return; |
| 301 | |
| 302 | /* |
| 303 | * If the root is mounted read-only, then /etc/mtab is |
| 304 | * probably not correct; so we won't issue a warning based on |
| 305 | * it. |
| 306 | */ |
| 307 | fd = open(MOUNTED, O_RDWR); |
| 308 | if (fd < 0 && errno == EROFS) |
| 309 | return; |
| 310 | else |
| 311 | close(fd); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 312 | |
| 313 | printf("%s is mounted. ", device_name); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 314 | if (isatty(0) && isatty(1)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 315 | cont = ask("Do you really want to continue", 0); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 316 | else |
| 317 | cont = 0; |
| 318 | if (!cont) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 319 | printf("check aborted.\n"); |
| 320 | exit(0); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 321 | } |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | /* |
| 326 | * check_zone_nr checks to see that *nr is a valid zone nr. If it |
| 327 | * isn't, it will possibly be repaired. Check_zone_nr sets *corrected |
| 328 | * if an error was corrected, and returns the zone (0 for no zone |
| 329 | * or a bad zone-number). |
| 330 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 331 | static int check_zone_nr(unsigned short *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("'."); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +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 | |
| 350 | #ifdef HAVE_MINIX2 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 351 | static int check_zone_nr2(unsigned int *nr, int *corrected) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 352 | { |
| 353 | if (!*nr) |
| 354 | return 0; |
| 355 | if (*nr < FIRSTZONE) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 356 | printf("Zone nr < FIRSTZONE in file `"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 357 | else if (*nr >= ZONES) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 358 | printf("Zone nr >= ZONES in file `"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 359 | else |
| 360 | return *nr; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 361 | print_current_name(); |
| 362 | printf("'."); |
| 363 | if (ask("Remove block", 1)) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 364 | *nr = 0; |
| 365 | *corrected = 1; |
| 366 | } |
| 367 | return 0; |
| 368 | } |
| 369 | #endif |
| 370 | |
| 371 | /* |
| 372 | * read-block reads block nr into the buffer at addr. |
| 373 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 374 | static void read_block(unsigned int nr, char *addr) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 375 | { |
| 376 | if (!nr) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 377 | memset(addr, 0, BLOCK_SIZE); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 378 | return; |
| 379 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 380 | if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) { |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 381 | printf("Read error: unable to seek to block in file '"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 382 | print_current_name(); |
| 383 | printf("'\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 384 | memset(addr, 0, BLOCK_SIZE); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 385 | errors_uncorrected = 1; |
| 386 | } else if (BLOCK_SIZE != read(IN, addr, BLOCK_SIZE)) { |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 387 | printf("Read error: bad block in file '"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 388 | print_current_name(); |
| 389 | printf("'\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 390 | memset(addr, 0, BLOCK_SIZE); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 391 | errors_uncorrected = 1; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | /* |
| 396 | * write_block writes block nr to disk. |
| 397 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 398 | static void write_block(unsigned int nr, char *addr) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 399 | { |
| 400 | if (!nr) |
| 401 | return; |
| 402 | if (nr < FIRSTZONE || nr >= ZONES) { |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 403 | printf("Internal error: trying to write bad block\n" |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 404 | "Write request ignored\n"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 405 | errors_uncorrected = 1; |
| 406 | return; |
| 407 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 408 | if (BLOCK_SIZE * nr != lseek(IN, BLOCK_SIZE * nr, SEEK_SET)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 409 | die("seek failed in write_block"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 410 | if (BLOCK_SIZE != write(IN, addr, BLOCK_SIZE)) { |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 411 | printf("Write error: bad block in file '"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 412 | print_current_name(); |
| 413 | printf("'\n"); |
| 414 | errors_uncorrected = 1; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | /* |
| 419 | * map-block calculates the absolute block nr of a block in a file. |
| 420 | * It sets 'changed' if the inode has needed changing, and re-writes |
| 421 | * any indirect blocks with errors. |
| 422 | */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 423 | static int map_block(struct minix_inode *inode, unsigned int blknr) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 424 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 425 | unsigned short ind[BLOCK_SIZE >> 1]; |
| 426 | unsigned short dind[BLOCK_SIZE >> 1]; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 427 | int blk_chg, block, result; |
| 428 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 429 | if (blknr < 7) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 430 | return check_zone_nr(inode->i_zone + blknr, &changed); |
| 431 | blknr -= 7; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 432 | if (blknr < 512) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 433 | block = check_zone_nr(inode->i_zone + 7, &changed); |
| 434 | read_block(block, (char *) ind); |
| 435 | blk_chg = 0; |
| 436 | result = check_zone_nr(blknr + ind, &blk_chg); |
| 437 | if (blk_chg) |
| 438 | write_block(block, (char *) ind); |
| 439 | return result; |
| 440 | } |
| 441 | blknr -= 512; |
| 442 | block = check_zone_nr(inode->i_zone + 8, &changed); |
| 443 | read_block(block, (char *) dind); |
| 444 | blk_chg = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 445 | result = check_zone_nr(dind + (blknr / 512), &blk_chg); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 446 | if (blk_chg) |
| 447 | write_block(block, (char *) dind); |
| 448 | block = result; |
| 449 | read_block(block, (char *) ind); |
| 450 | blk_chg = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 451 | result = check_zone_nr(ind + (blknr % 512), &blk_chg); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 452 | if (blk_chg) |
| 453 | write_block(block, (char *) ind); |
| 454 | return result; |
| 455 | } |
| 456 | |
| 457 | #ifdef HAVE_MINIX2 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 458 | static int map_block2(struct minix2_inode *inode, unsigned int blknr) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 459 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 460 | unsigned int ind[BLOCK_SIZE >> 2]; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 461 | unsigned int dind[BLOCK_SIZE >> 2]; |
| 462 | unsigned int tind[BLOCK_SIZE >> 2]; |
| 463 | int blk_chg, block, result; |
| 464 | |
| 465 | if (blknr < 7) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 466 | return check_zone_nr2(inode->i_zone + blknr, &changed); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 467 | blknr -= 7; |
| 468 | if (blknr < 256) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 469 | block = check_zone_nr2(inode->i_zone + 7, &changed); |
| 470 | read_block(block, (char *) ind); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 471 | blk_chg = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 472 | result = check_zone_nr2(blknr + ind, &blk_chg); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 473 | if (blk_chg) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 474 | write_block(block, (char *) ind); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 475 | return result; |
| 476 | } |
| 477 | blknr -= 256; |
| 478 | if (blknr >= 256 * 256) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 479 | block = check_zone_nr2(inode->i_zone + 8, &changed); |
| 480 | read_block(block, (char *) dind); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 481 | blk_chg = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 482 | result = check_zone_nr2(dind + blknr / 256, &blk_chg); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 483 | if (blk_chg) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 484 | write_block(block, (char *) dind); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 485 | block = result; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 486 | read_block(block, (char *) ind); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 487 | blk_chg = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 488 | result = check_zone_nr2(ind + blknr % 256, &blk_chg); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 489 | if (blk_chg) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 490 | write_block(block, (char *) ind); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 491 | return result; |
| 492 | } |
| 493 | blknr -= 256 * 256; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 494 | block = check_zone_nr2(inode->i_zone + 9, &changed); |
| 495 | read_block(block, (char *) tind); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 496 | blk_chg = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 497 | result = check_zone_nr2(tind + blknr / (256 * 256), &blk_chg); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 498 | if (blk_chg) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 499 | write_block(block, (char *) tind); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 500 | block = result; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 501 | read_block(block, (char *) dind); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 502 | blk_chg = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 503 | result = check_zone_nr2(dind + (blknr / 256) % 256, &blk_chg); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 504 | if (blk_chg) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 505 | write_block(block, (char *) dind); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 506 | block = result; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 507 | read_block(block, (char *) ind); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 508 | blk_chg = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 509 | result = check_zone_nr2(ind + blknr % 256, &blk_chg); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 510 | if (blk_chg) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 511 | write_block(block, (char *) ind); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 512 | return result; |
| 513 | } |
| 514 | #endif |
| 515 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 516 | static void write_super_block(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 517 | { |
| 518 | /* |
| 519 | * Set the state of the filesystem based on whether or not there |
| 520 | * are uncorrected errors. The filesystem valid flag is |
| 521 | * unconditionally set if we get this far. |
| 522 | */ |
| 523 | Super.s_state |= MINIX_VALID_FS; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 524 | if (errors_uncorrected) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 525 | Super.s_state |= MINIX_ERROR_FS; |
| 526 | else |
| 527 | Super.s_state &= ~MINIX_ERROR_FS; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 528 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 529 | if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 530 | die("seek failed in write_super_block"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 531 | if (BLOCK_SIZE != write(IN, super_block_buffer, BLOCK_SIZE)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 532 | die("unable to write super-block"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 533 | |
| 534 | return; |
| 535 | } |
| 536 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 537 | static void write_tables(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 538 | { |
| 539 | write_super_block(); |
| 540 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 541 | if (IMAPS * BLOCK_SIZE != write(IN, inode_map, IMAPS * BLOCK_SIZE)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 542 | die("Unable to write inode map"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 543 | if (ZMAPS * BLOCK_SIZE != write(IN, zone_map, ZMAPS * BLOCK_SIZE)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 544 | die("Unable to write zone map"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 545 | if (INODE_BUFFER_SIZE != write(IN, inode_buffer, INODE_BUFFER_SIZE)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 546 | die("Unable to write inodes"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 549 | static void get_dirsize(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 550 | { |
| 551 | int block; |
| 552 | char blk[BLOCK_SIZE]; |
| 553 | int size; |
| 554 | |
| 555 | #if HAVE_MINIX2 |
| 556 | if (version2) |
| 557 | block = Inode2[ROOT_INO].i_zone[0]; |
| 558 | else |
| 559 | #endif |
| 560 | block = Inode[ROOT_INO].i_zone[0]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 561 | read_block(block, blk); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 562 | for (size = 16; size < BLOCK_SIZE; size <<= 1) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 563 | if (strcmp(blk + size + 2, "..") == 0) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 564 | dirsize = size; |
| 565 | namelen = size - 2; |
| 566 | return; |
| 567 | } |
| 568 | } |
| 569 | /* use defaults */ |
| 570 | } |
| 571 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 572 | static void read_superblock(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 573 | { |
| 574 | if (BLOCK_SIZE != lseek(IN, BLOCK_SIZE, SEEK_SET)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 575 | die("seek failed"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 576 | if (BLOCK_SIZE != read(IN, super_block_buffer, BLOCK_SIZE)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 577 | die("unable to read super block"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 578 | if (MAGIC == MINIX_SUPER_MAGIC) { |
| 579 | namelen = 14; |
| 580 | dirsize = 16; |
| 581 | version2 = 0; |
| 582 | } else if (MAGIC == MINIX_SUPER_MAGIC2) { |
| 583 | namelen = 30; |
| 584 | dirsize = 32; |
| 585 | version2 = 0; |
| 586 | #ifdef HAVE_MINIX2 |
| 587 | } else if (MAGIC == MINIX2_SUPER_MAGIC) { |
| 588 | namelen = 14; |
| 589 | dirsize = 16; |
| 590 | version2 = 1; |
| 591 | } else if (MAGIC == MINIX2_SUPER_MAGIC2) { |
| 592 | namelen = 30; |
| 593 | dirsize = 32; |
| 594 | version2 = 1; |
| 595 | #endif |
| 596 | } else |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 597 | die("bad magic number in super-block"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 598 | if (ZONESIZE != 0 || BLOCK_SIZE != 1024) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 599 | die("Only 1k blocks/zones supported"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 600 | if (IMAPS * BLOCK_SIZE * 8 < INODES + 1) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 601 | die("bad s_imap_blocks field in super-block"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 602 | if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 603 | die("bad s_zmap_blocks field in super-block"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 606 | static void read_tables(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 607 | { |
Erik Andersen | 0d068a2 | 2000-03-21 22:32:57 +0000 | [diff] [blame] | 608 | inode_map = xmalloc(IMAPS * BLOCK_SIZE); |
| 609 | zone_map = xmalloc(ZMAPS * BLOCK_SIZE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 610 | memset(inode_map, 0, sizeof(inode_map)); |
| 611 | memset(zone_map, 0, sizeof(zone_map)); |
Erik Andersen | 0d068a2 | 2000-03-21 22:32:57 +0000 | [diff] [blame] | 612 | inode_buffer = xmalloc(INODE_BUFFER_SIZE); |
| 613 | inode_count = xmalloc(INODES + 1); |
| 614 | zone_count = xmalloc(ZONES); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 615 | if (IMAPS * BLOCK_SIZE != read(IN, inode_map, IMAPS * BLOCK_SIZE)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 616 | die("Unable to read inode map"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 617 | if (ZMAPS * BLOCK_SIZE != read(IN, zone_map, ZMAPS * BLOCK_SIZE)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 618 | die("Unable to read zone map"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 619 | if (INODE_BUFFER_SIZE != read(IN, inode_buffer, INODE_BUFFER_SIZE)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 620 | die("Unable to read inodes"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 621 | if (NORM_FIRSTZONE != FIRSTZONE) { |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 622 | printf("Warning: Firstzone != Norm_firstzone\n"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 623 | errors_uncorrected = 1; |
| 624 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 625 | get_dirsize(); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 626 | if (show) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 627 | printf("%ld inodes\n", INODES); |
| 628 | printf("%ld blocks\n", ZONES); |
| 629 | printf("Firstdatazone=%ld (%ld)\n", FIRSTZONE, NORM_FIRSTZONE); |
| 630 | printf("Zonesize=%d\n", BLOCK_SIZE << ZONESIZE); |
| 631 | printf("Maxsize=%ld\n", MAXSIZE); |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 632 | printf("Filesystem state=%d\n", Super.s_state); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 633 | printf("namelen=%d\n\n", namelen); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 637 | struct minix_inode *get_inode(unsigned int nr) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 638 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 639 | struct minix_inode *inode; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 640 | |
| 641 | if (!nr || nr > INODES) |
| 642 | return NULL; |
| 643 | total++; |
| 644 | inode = Inode + nr; |
| 645 | if (!inode_count[nr]) { |
| 646 | if (!inode_in_use(nr)) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 647 | printf("Inode %d marked not used, but used for file '", nr); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 648 | print_current_name(); |
| 649 | printf("'\n"); |
| 650 | if (repair) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 651 | if (ask("Mark in use", 1)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 652 | mark_inode(nr); |
| 653 | } else { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 654 | errors_uncorrected = 1; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | if (S_ISDIR(inode->i_mode)) |
| 658 | directory++; |
| 659 | else if (S_ISREG(inode->i_mode)) |
| 660 | regular++; |
| 661 | else if (S_ISCHR(inode->i_mode)) |
| 662 | chardev++; |
| 663 | else if (S_ISBLK(inode->i_mode)) |
| 664 | blockdev++; |
| 665 | else if (S_ISLNK(inode->i_mode)) |
| 666 | symlinks++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 667 | else if (S_ISSOCK(inode->i_mode)); |
| 668 | else if (S_ISFIFO(inode->i_mode)); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 669 | else { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 670 | print_current_name(); |
| 671 | printf(" has mode %05o\n", inode->i_mode); |
| 672 | } |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 673 | |
| 674 | } else |
| 675 | links++; |
| 676 | if (!++inode_count[nr]) { |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 677 | printf("Warning: inode count too big.\n"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 678 | inode_count[nr]--; |
| 679 | errors_uncorrected = 1; |
| 680 | } |
| 681 | return inode; |
| 682 | } |
| 683 | |
| 684 | #ifdef HAVE_MINIX2 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 685 | struct minix2_inode *get_inode2(unsigned int nr) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 686 | { |
| 687 | struct minix2_inode *inode; |
| 688 | |
| 689 | if (!nr || nr > INODES) |
| 690 | return NULL; |
| 691 | total++; |
| 692 | inode = Inode2 + nr; |
| 693 | if (!inode_count[nr]) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 694 | if (!inode_in_use(nr)) { |
| 695 | printf("Inode %d marked not used, but used for file '", nr); |
| 696 | print_current_name(); |
| 697 | printf("'\n"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 698 | if (repair) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 699 | if (ask("Mark in use", 1)) |
| 700 | mark_inode(nr); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 701 | else |
| 702 | errors_uncorrected = 1; |
| 703 | } |
| 704 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 705 | if (S_ISDIR(inode->i_mode)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 706 | directory++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 707 | else if (S_ISREG(inode->i_mode)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 708 | regular++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 709 | else if (S_ISCHR(inode->i_mode)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 710 | chardev++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 711 | else if (S_ISBLK(inode->i_mode)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 712 | blockdev++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 713 | else if (S_ISLNK(inode->i_mode)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 714 | symlinks++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 715 | else if (S_ISSOCK(inode->i_mode)); |
| 716 | else if (S_ISFIFO(inode->i_mode)); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 717 | else { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 718 | print_current_name(); |
| 719 | printf(" has mode %05o\n", inode->i_mode); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 720 | } |
| 721 | } else |
| 722 | links++; |
| 723 | if (!++inode_count[nr]) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 724 | printf("Warning: inode count too big.\n"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 725 | inode_count[nr]--; |
| 726 | errors_uncorrected = 1; |
| 727 | } |
| 728 | return inode; |
| 729 | } |
| 730 | #endif |
| 731 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 732 | static void check_root(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 733 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 734 | struct minix_inode *inode = Inode + ROOT_INO; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 735 | |
| 736 | if (!inode || !S_ISDIR(inode->i_mode)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 737 | die("root inode isn't a directory"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | #ifdef HAVE_MINIX2 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 741 | static void check_root2(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 742 | { |
| 743 | struct minix2_inode *inode = Inode2 + ROOT_INO; |
| 744 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 745 | if (!inode || !S_ISDIR(inode->i_mode)) |
| 746 | die("root inode isn't a directory"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 747 | } |
| 748 | #endif |
| 749 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 750 | static int add_zone(unsigned short *znr, int *corrected) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 751 | { |
| 752 | int result; |
| 753 | int block; |
| 754 | |
| 755 | result = 0; |
| 756 | block = check_zone_nr(znr, corrected); |
| 757 | if (!block) |
| 758 | return 0; |
| 759 | if (zone_count[block]) { |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 760 | printf("Block has been used before. Now in file `"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 761 | print_current_name(); |
| 762 | printf("'."); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 763 | if (ask("Clear", 1)) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 764 | *znr = 0; |
| 765 | block = 0; |
| 766 | *corrected = 1; |
| 767 | } |
| 768 | } |
| 769 | if (!block) |
| 770 | return 0; |
| 771 | if (!zone_in_use(block)) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 772 | printf("Block %d in file `", block); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 773 | print_current_name(); |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 774 | printf("' is marked not in use."); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 775 | if (ask("Correct", 1)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 776 | mark_zone(block); |
| 777 | } |
| 778 | if (!++zone_count[block]) |
| 779 | zone_count[block]--; |
| 780 | return block; |
| 781 | } |
| 782 | |
| 783 | #ifdef HAVE_MINIX2 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 784 | static int add_zone2(unsigned int *znr, int *corrected) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 785 | { |
| 786 | int result; |
| 787 | int block; |
| 788 | |
| 789 | result = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 790 | block = check_zone_nr2(znr, corrected); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 791 | if (!block) |
| 792 | return 0; |
| 793 | if (zone_count[block]) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 794 | printf("Block has been used before. Now in file `"); |
| 795 | print_current_name(); |
| 796 | printf("'."); |
| 797 | if (ask("Clear", 1)) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 798 | *znr = 0; |
| 799 | block = 0; |
| 800 | *corrected = 1; |
| 801 | } |
| 802 | } |
| 803 | if (!block) |
| 804 | return 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 805 | if (!zone_in_use(block)) { |
| 806 | printf("Block %d in file `", block); |
| 807 | print_current_name(); |
| 808 | printf("' is marked not in use."); |
| 809 | if (ask("Correct", 1)) |
| 810 | mark_zone(block); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 811 | } |
| 812 | if (!++zone_count[block]) |
| 813 | zone_count[block]--; |
| 814 | return block; |
| 815 | } |
| 816 | #endif |
| 817 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 818 | static void add_zone_ind(unsigned short *znr, int *corrected) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 819 | { |
| 820 | static char blk[BLOCK_SIZE]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 821 | int i, chg_blk = 0; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 822 | int block; |
| 823 | |
| 824 | block = add_zone(znr, corrected); |
| 825 | if (!block) |
| 826 | return; |
| 827 | read_block(block, blk); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 828 | for (i = 0; i < (BLOCK_SIZE >> 1); i++) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 829 | add_zone(i + (unsigned short *) blk, &chg_blk); |
| 830 | if (chg_blk) |
| 831 | write_block(block, blk); |
| 832 | } |
| 833 | |
| 834 | #ifdef HAVE_MINIX2 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 835 | static void add_zone_ind2(unsigned int *znr, int *corrected) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 836 | { |
| 837 | static char blk[BLOCK_SIZE]; |
| 838 | int i, chg_blk = 0; |
| 839 | int block; |
| 840 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 841 | block = add_zone2(znr, corrected); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 842 | if (!block) |
| 843 | return; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 844 | read_block(block, blk); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 845 | for (i = 0; i < BLOCK_SIZE >> 2; i++) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 846 | add_zone2(i + (unsigned int *) blk, &chg_blk); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 847 | if (chg_blk) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 848 | write_block(block, blk); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 849 | } |
| 850 | #endif |
| 851 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 852 | static void add_zone_dind(unsigned short *znr, int *corrected) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 853 | { |
| 854 | static char blk[BLOCK_SIZE]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 855 | int i, blk_chg = 0; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 856 | int block; |
| 857 | |
| 858 | block = add_zone(znr, corrected); |
| 859 | if (!block) |
| 860 | return; |
| 861 | read_block(block, blk); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 862 | for (i = 0; i < (BLOCK_SIZE >> 1); i++) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 863 | add_zone_ind(i + (unsigned short *) blk, &blk_chg); |
| 864 | if (blk_chg) |
| 865 | write_block(block, blk); |
| 866 | } |
| 867 | |
| 868 | #ifdef HAVE_MINIX2 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 869 | static void add_zone_dind2(unsigned int *znr, int *corrected) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 870 | { |
| 871 | static char blk[BLOCK_SIZE]; |
| 872 | int i, blk_chg = 0; |
| 873 | int block; |
| 874 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 875 | block = add_zone2(znr, corrected); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 876 | if (!block) |
| 877 | return; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 878 | read_block(block, blk); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 879 | for (i = 0; i < BLOCK_SIZE >> 2; i++) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 880 | add_zone_ind2(i + (unsigned int *) blk, &blk_chg); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 881 | if (blk_chg) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 882 | write_block(block, blk); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 885 | static void add_zone_tind2(unsigned int *znr, int *corrected) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 886 | { |
| 887 | static char blk[BLOCK_SIZE]; |
| 888 | int i, blk_chg = 0; |
| 889 | int block; |
| 890 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 891 | block = add_zone2(znr, corrected); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 892 | if (!block) |
| 893 | return; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 894 | read_block(block, blk); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 895 | for (i = 0; i < BLOCK_SIZE >> 2; i++) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 896 | add_zone_dind2(i + (unsigned int *) blk, &blk_chg); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 897 | if (blk_chg) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 898 | write_block(block, blk); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 899 | } |
| 900 | #endif |
| 901 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 902 | static void check_zones(unsigned int i) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 903 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 904 | struct minix_inode *inode; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 905 | |
| 906 | if (!i || i > INODES) |
| 907 | return; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 908 | if (inode_count[i] > 1) /* have we counted this file already? */ |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 909 | return; |
| 910 | inode = Inode + i; |
| 911 | if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) && |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 912 | !S_ISLNK(inode->i_mode)) return; |
| 913 | for (i = 0; i < 7; i++) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 914 | add_zone(i + inode->i_zone, &changed); |
| 915 | add_zone_ind(7 + inode->i_zone, &changed); |
| 916 | add_zone_dind(8 + inode->i_zone, &changed); |
| 917 | } |
| 918 | |
| 919 | #ifdef HAVE_MINIX2 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 920 | static void check_zones2(unsigned int i) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 921 | { |
| 922 | struct minix2_inode *inode; |
| 923 | |
| 924 | if (!i || i > INODES) |
| 925 | return; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 926 | if (inode_count[i] > 1) /* have we counted this file already? */ |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 927 | return; |
| 928 | inode = Inode2 + i; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 929 | if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) |
| 930 | && !S_ISLNK(inode->i_mode)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 931 | return; |
| 932 | for (i = 0; i < 7; i++) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 933 | add_zone2(i + inode->i_zone, &changed); |
| 934 | add_zone_ind2(7 + inode->i_zone, &changed); |
| 935 | add_zone_dind2(8 + inode->i_zone, &changed); |
| 936 | add_zone_tind2(9 + inode->i_zone, &changed); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 937 | } |
| 938 | #endif |
| 939 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 940 | static void check_file(struct minix_inode *dir, unsigned int offset) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 941 | { |
| 942 | static char blk[BLOCK_SIZE]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 943 | struct minix_inode *inode; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 944 | int ino; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 945 | char *name; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 946 | int block; |
| 947 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 948 | block = map_block(dir, offset / BLOCK_SIZE); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 949 | read_block(block, blk); |
| 950 | name = blk + (offset % BLOCK_SIZE) + 2; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 951 | ino = *(unsigned short *) (name - 2); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 952 | if (ino > INODES) { |
| 953 | print_current_name(); |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 954 | printf(" contains a bad inode number for file '"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 955 | printf("%.*s'.", namelen, name); |
| 956 | if (ask(" Remove", 1)) { |
| 957 | *(unsigned short *) (name - 2) = 0; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 958 | write_block(block, blk); |
| 959 | } |
| 960 | ino = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 961 | } |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 962 | if (name_depth < MAX_DEPTH) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 963 | strncpy(name_list[name_depth], name, namelen); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 964 | name_depth++; |
| 965 | inode = get_inode(ino); |
| 966 | name_depth--; |
| 967 | if (!offset) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 968 | if (!inode || strcmp(".", name)) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 969 | print_current_name(); |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 970 | printf(": bad directory: '.' isn't first\n"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 971 | errors_uncorrected = 1; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 972 | } else |
| 973 | return; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 974 | } |
| 975 | if (offset == dirsize) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 976 | if (!inode || strcmp("..", name)) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 977 | print_current_name(); |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 978 | printf(": bad directory: '..' isn't second\n"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 979 | errors_uncorrected = 1; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 980 | } else |
| 981 | return; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 982 | } |
| 983 | if (!inode) |
| 984 | return; |
| 985 | if (name_depth < MAX_DEPTH) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 986 | strncpy(name_list[name_depth], name, namelen); |
| 987 | name_depth++; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 988 | if (list) { |
| 989 | if (verbose) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 990 | printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 991 | print_current_name(); |
| 992 | if (S_ISDIR(inode->i_mode)) |
| 993 | printf(":\n"); |
| 994 | else |
| 995 | printf("\n"); |
| 996 | } |
| 997 | check_zones(ino); |
| 998 | if (inode && S_ISDIR(inode->i_mode)) |
| 999 | recursive_check(ino); |
| 1000 | name_depth--; |
| 1001 | return; |
| 1002 | } |
| 1003 | |
| 1004 | #ifdef HAVE_MINIX2 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1005 | static void check_file2(struct minix2_inode *dir, unsigned int offset) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1006 | { |
| 1007 | static char blk[BLOCK_SIZE]; |
| 1008 | struct minix2_inode *inode; |
| 1009 | int ino; |
| 1010 | char *name; |
| 1011 | int block; |
| 1012 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1013 | block = map_block2(dir, offset / BLOCK_SIZE); |
| 1014 | read_block(block, blk); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1015 | name = blk + (offset % BLOCK_SIZE) + 2; |
| 1016 | ino = *(unsigned short *) (name - 2); |
| 1017 | if (ino > INODES) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1018 | print_current_name(); |
| 1019 | printf(" contains a bad inode number for file '"); |
| 1020 | printf("%.*s'.", namelen, name); |
| 1021 | if (ask(" Remove", 1)) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1022 | *(unsigned short *) (name - 2) = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1023 | write_block(block, blk); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1024 | } |
| 1025 | ino = 0; |
| 1026 | } |
| 1027 | if (name_depth < MAX_DEPTH) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1028 | strncpy(name_list[name_depth], name, namelen); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1029 | name_depth++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1030 | inode = get_inode2(ino); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1031 | name_depth--; |
| 1032 | if (!offset) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1033 | if (!inode || strcmp(".", name)) { |
| 1034 | print_current_name(); |
| 1035 | printf(": bad directory: '.' isn't first\n"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1036 | errors_uncorrected = 1; |
| 1037 | } else |
| 1038 | return; |
| 1039 | } |
| 1040 | if (offset == dirsize) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1041 | if (!inode || strcmp("..", name)) { |
| 1042 | print_current_name(); |
| 1043 | printf(": bad directory: '..' isn't second\n"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1044 | errors_uncorrected = 1; |
| 1045 | } else |
| 1046 | return; |
| 1047 | } |
| 1048 | if (!inode) |
| 1049 | return; |
| 1050 | name_depth++; |
| 1051 | if (list) { |
| 1052 | if (verbose) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1053 | printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks); |
| 1054 | print_current_name(); |
| 1055 | if (S_ISDIR(inode->i_mode)) |
| 1056 | printf(":\n"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1057 | else |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1058 | printf("\n"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1059 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1060 | check_zones2(ino); |
| 1061 | if (inode && S_ISDIR(inode->i_mode)) |
| 1062 | recursive_check2(ino); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1063 | name_depth--; |
| 1064 | return; |
| 1065 | } |
| 1066 | #endif |
| 1067 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 1068 | static void recursive_check(unsigned int ino) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1069 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1070 | struct minix_inode *dir; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1071 | unsigned int offset; |
| 1072 | |
| 1073 | dir = Inode + ino; |
| 1074 | if (!S_ISDIR(dir->i_mode)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 1075 | die("internal error"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1076 | if (dir->i_size < 2 * dirsize) { |
| 1077 | print_current_name(); |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 1078 | printf(": bad directory: size<32"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1079 | errors_uncorrected = 1; |
| 1080 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1081 | for (offset = 0; offset < dir->i_size; offset += dirsize) |
| 1082 | check_file(dir, offset); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | #ifdef HAVE_MINIX2 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1086 | static void recursive_check2(unsigned int ino) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1087 | { |
| 1088 | struct minix2_inode *dir; |
| 1089 | unsigned int offset; |
| 1090 | |
| 1091 | dir = Inode2 + ino; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1092 | if (!S_ISDIR(dir->i_mode)) |
| 1093 | die("internal error"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1094 | if (dir->i_size < 2 * dirsize) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1095 | print_current_name(); |
| 1096 | printf(": bad directory: size < 32"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1097 | errors_uncorrected = 1; |
| 1098 | } |
| 1099 | for (offset = 0; offset < dir->i_size; offset += dirsize) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1100 | check_file2(dir, offset); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1101 | } |
| 1102 | #endif |
| 1103 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 1104 | static int bad_zone(int i) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1105 | { |
| 1106 | char buffer[1024]; |
| 1107 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1108 | if (BLOCK_SIZE * i != lseek(IN, BLOCK_SIZE * i, SEEK_SET)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 1109 | die("seek failed in bad_zone"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1110 | return (BLOCK_SIZE != read(IN, buffer, BLOCK_SIZE)); |
| 1111 | } |
| 1112 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 1113 | static void check_counts(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1114 | { |
| 1115 | int i; |
| 1116 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1117 | for (i = 1; i <= INODES; i++) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1118 | if (!inode_in_use(i) && Inode[i].i_mode && warn_mode) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1119 | printf("Inode %d mode not cleared.", i); |
| 1120 | if (ask("Clear", 1)) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1121 | Inode[i].i_mode = 0; |
| 1122 | changed = 1; |
| 1123 | } |
| 1124 | } |
| 1125 | if (!inode_count[i]) { |
| 1126 | if (!inode_in_use(i)) |
| 1127 | continue; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1128 | printf("Inode %d not used, marked used in the bitmap.", i); |
| 1129 | if (ask("Clear", 1)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1130 | unmark_inode(i); |
| 1131 | continue; |
| 1132 | } |
| 1133 | if (!inode_in_use(i)) { |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 1134 | printf("Inode %d used, marked unused in the bitmap.", i); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1135 | if (ask("Set", 1)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1136 | mark_inode(i); |
| 1137 | } |
| 1138 | if (Inode[i].i_nlinks != inode_count[i]) { |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 1139 | printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.", |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1140 | i, Inode[i].i_mode, Inode[i].i_nlinks, inode_count[i]); |
| 1141 | if (ask("Set i_nlinks to count", 1)) { |
| 1142 | Inode[i].i_nlinks = inode_count[i]; |
| 1143 | changed = 1; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1144 | } |
| 1145 | } |
| 1146 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1147 | for (i = FIRSTZONE; i < ZONES; i++) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1148 | if (zone_in_use(i) == zone_count[i]) |
| 1149 | continue; |
| 1150 | if (!zone_count[i]) { |
| 1151 | if (bad_zone(i)) |
| 1152 | continue; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1153 | printf("Zone %d: marked in use, no file uses it.", i); |
| 1154 | if (ask("Unmark", 1)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1155 | unmark_zone(i); |
| 1156 | continue; |
| 1157 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1158 | printf("Zone %d: %sin use, counted=%d\n", |
| 1159 | i, zone_in_use(i) ? "" : "not ", zone_count[i]); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | #ifdef HAVE_MINIX2 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1164 | static void check_counts2(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1165 | { |
| 1166 | int i; |
| 1167 | |
| 1168 | for (i = 1; i <= INODES; i++) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1169 | if (!inode_in_use(i) && Inode2[i].i_mode && warn_mode) { |
| 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]) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1177 | if (!inode_in_use(i)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1178 | continue; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1179 | printf("Inode %d not used, marked used in the bitmap.", i); |
| 1180 | if (ask("Clear", 1)) |
| 1181 | unmark_inode(i); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1182 | continue; |
| 1183 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1184 | if (!inode_in_use(i)) { |
| 1185 | printf("Inode %d used, marked unused in the bitmap.", i); |
| 1186 | if (ask("Set", 1)) |
| 1187 | mark_inode(i); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1188 | } |
| 1189 | if (Inode2[i].i_nlinks != inode_count[i]) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1190 | printf("Inode %d (mode = %07o), i_nlinks=%d, counted=%d.", |
| 1191 | i, Inode2[i].i_mode, Inode2[i].i_nlinks, |
| 1192 | inode_count[i]); |
| 1193 | if (ask("Set i_nlinks to count", 1)) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1194 | Inode2[i].i_nlinks = inode_count[i]; |
| 1195 | changed = 1; |
| 1196 | } |
| 1197 | } |
| 1198 | } |
| 1199 | for (i = FIRSTZONE; i < ZONES; i++) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1200 | if (zone_in_use(i) == zone_count[i]) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1201 | continue; |
| 1202 | if (!zone_count[i]) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1203 | if (bad_zone(i)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1204 | continue; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1205 | printf("Zone %d: marked in use, no file uses it.", i); |
| 1206 | if (ask("Unmark", 1)) |
| 1207 | unmark_zone(i); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1208 | continue; |
| 1209 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1210 | printf("Zone %d: %sin use, counted=%d\n", |
| 1211 | i, zone_in_use(i) ? "" : "not ", zone_count[i]); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1212 | } |
| 1213 | } |
| 1214 | #endif |
| 1215 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 1216 | static void check(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1217 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1218 | memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count)); |
| 1219 | memset(zone_count, 0, ZONES * sizeof(*zone_count)); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1220 | check_zones(ROOT_INO); |
| 1221 | recursive_check(ROOT_INO); |
| 1222 | check_counts(); |
| 1223 | } |
| 1224 | |
| 1225 | #ifdef HAVE_MINIX2 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1226 | static void check2(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1227 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 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(); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1233 | } |
| 1234 | #endif |
| 1235 | |
John Beppu | c1dc5d9 | 2000-02-11 12:43:20 +0000 | [diff] [blame] | 1236 | /* Wed Feb 9 15:17:06 MST 2000 */ |
| 1237 | /* dynamically allocate name_list (instead of making it static) */ |
| 1238 | static void alloc_name_list(void) |
| 1239 | { |
| 1240 | int i; |
| 1241 | |
Erik Andersen | 0d068a2 | 2000-03-21 22:32:57 +0000 | [diff] [blame] | 1242 | name_list = xmalloc(sizeof(char *) * MAX_DEPTH); |
| 1243 | for (i = 0; i < MAX_DEPTH; i++) |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 1244 | name_list[i] = xmalloc(sizeof(char) * BUFSIZ + 1); |
John Beppu | c1dc5d9 | 2000-02-11 12:43:20 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 1247 | #if 0 |
John Beppu | 5e1b2ca | 2000-02-11 12:52:55 +0000 | [diff] [blame] | 1248 | /* execute this atexit() to deallocate name_list[] */ |
| 1249 | /* piptigger was here */ |
John Beppu | c1dc5d9 | 2000-02-11 12:43:20 +0000 | [diff] [blame] | 1250 | static void free_name_list(void) |
| 1251 | { |
John Beppu | 5e1b2ca | 2000-02-11 12:52:55 +0000 | [diff] [blame] | 1252 | int i; |
| 1253 | |
| 1254 | if (name_list) { |
| 1255 | for (i = 0; i < MAX_DEPTH; i++) { |
| 1256 | if (name_list[i]) { |
| 1257 | free(name_list[i]); |
| 1258 | } |
| 1259 | } |
| 1260 | free(name_list); |
| 1261 | } |
John Beppu | c1dc5d9 | 2000-02-11 12:43:20 +0000 | [diff] [blame] | 1262 | } |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 1263 | #endif |
John Beppu | c1dc5d9 | 2000-02-11 12:43:20 +0000 | [diff] [blame] | 1264 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1265 | extern int fsck_minix_main(int argc, char **argv) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1266 | { |
| 1267 | struct termios tmp; |
| 1268 | int count; |
| 1269 | int retcode = 0; |
| 1270 | |
John Beppu | c1dc5d9 | 2000-02-11 12:43:20 +0000 | [diff] [blame] | 1271 | alloc_name_list(); |
Erik Andersen | 298854f | 2000-03-23 01:09:18 +0000 | [diff] [blame] | 1272 | /* Don't bother to free memory. Exit does |
| 1273 | * that automagically, so we can save a few bytes */ |
| 1274 | //atexit(free_name_list); |
John Beppu | c1dc5d9 | 2000-02-11 12:43:20 +0000 | [diff] [blame] | 1275 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1276 | if (argc && *argv) |
| 1277 | program_name = *argv; |
| 1278 | if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 1279 | die("bad inode size"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1280 | #ifdef HAVE_MINIX2 |
| 1281 | if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 1282 | die("bad v2 inode size"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1283 | #endif |
| 1284 | while (argc-- > 1) { |
| 1285 | argv++; |
| 1286 | if (argv[0][0] != '-') { |
| 1287 | if (device_name) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 1288 | show_usage(); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1289 | else |
| 1290 | device_name = argv[0]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1291 | } else |
| 1292 | while (*++argv[0]) |
| 1293 | switch (argv[0][0]) { |
| 1294 | case 'l': |
| 1295 | list = 1; |
| 1296 | break; |
| 1297 | case 'a': |
| 1298 | automatic = 1; |
| 1299 | repair = 1; |
| 1300 | break; |
| 1301 | case 'r': |
| 1302 | automatic = 0; |
| 1303 | repair = 1; |
| 1304 | break; |
| 1305 | case 'v': |
| 1306 | verbose = 1; |
| 1307 | break; |
| 1308 | case 's': |
| 1309 | show = 1; |
| 1310 | break; |
| 1311 | case 'm': |
| 1312 | warn_mode = 1; |
| 1313 | break; |
| 1314 | case 'f': |
| 1315 | force = 1; |
| 1316 | break; |
| 1317 | default: |
| 1318 | show_usage(); |
| 1319 | } |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1320 | } |
| 1321 | if (!device_name) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 1322 | show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1323 | check_mount(); /* trying to check a mounted filesystem? */ |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1324 | if (repair && !automatic) { |
| 1325 | if (!isatty(0) || !isatty(1)) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 1326 | die("need terminal for interactive repairs"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1327 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1328 | IN = open(device_name, repair ? O_RDWR : O_RDONLY); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1329 | if (IN < 0) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 1330 | die("unable to open '%s'"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1331 | for (count = 0; count < 3; count++) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1332 | sync(); |
| 1333 | read_superblock(); |
| 1334 | |
| 1335 | /* |
| 1336 | * Determine whether or not we should continue with the checking. |
| 1337 | * This is based on the status of the filesystem valid and error |
| 1338 | * flags and whether or not the -f switch was specified on the |
| 1339 | * command line. |
| 1340 | */ |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 1341 | printf("%s, %s\n", program_name, program_version); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1342 | if (!(Super.s_state & MINIX_ERROR_FS) && |
| 1343 | (Super.s_state & MINIX_VALID_FS) && !force) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1344 | if (repair) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 1345 | printf("%s is clean, no check.\n", device_name); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1346 | return retcode; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1347 | } else if (force) |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 1348 | printf("Forcing filesystem check on %s.\n", device_name); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1349 | else if (repair) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1350 | printf("Filesystem on %s is dirty, needs checking.\n", |
| 1351 | device_name); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1352 | |
| 1353 | read_tables(); |
| 1354 | |
| 1355 | if (repair && !automatic) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1356 | tcgetattr(0, &termios); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1357 | tmp = termios; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1358 | tmp.c_lflag &= ~(ICANON | ECHO); |
| 1359 | tcsetattr(0, TCSANOW, &tmp); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1360 | termios_set = 1; |
| 1361 | } |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1362 | #if HAVE_MINIX2 |
| 1363 | if (version2) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1364 | check_root2(); |
| 1365 | check2(); |
| 1366 | } else |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1367 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1368 | { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1369 | check_root(); |
| 1370 | check(); |
| 1371 | } |
| 1372 | if (verbose) { |
| 1373 | int i, free; |
| 1374 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1375 | for (i = 1, free = 0; i <= INODES; i++) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1376 | if (!inode_in_use(i)) |
| 1377 | free++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1378 | printf("\n%6ld inodes used (%ld%%)\n", (INODES - free), |
| 1379 | 100 * (INODES - free) / INODES); |
| 1380 | for (i = FIRSTZONE, free = 0; i < ZONES; i++) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1381 | if (!zone_in_use(i)) |
| 1382 | free++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1383 | printf("%6ld zones used (%ld%%)\n", (ZONES - free), |
| 1384 | 100 * (ZONES - free) / ZONES); |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 1385 | printf("\n%6d regular files\n" |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1386 | "%6d directories\n" |
| 1387 | "%6d character device files\n" |
| 1388 | "%6d block device files\n" |
| 1389 | "%6d links\n" |
| 1390 | "%6d symbolic links\n" |
| 1391 | "------\n" |
| 1392 | "%6d files\n", |
| 1393 | regular, directory, chardev, blockdev, |
| 1394 | links - 2 * directory + 1, symlinks, |
| 1395 | total - 2 * directory + 1); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1396 | } |
| 1397 | if (changed) { |
| 1398 | write_tables(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1399 | printf("----------------------------\n" |
| 1400 | "FILE SYSTEM HAS BEEN CHANGED\n" |
| 1401 | "----------------------------\n"); |
| 1402 | for (count = 0; count < 3; count++) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1403 | sync(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1404 | } else if (repair) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1405 | write_super_block(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1406 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1407 | if (repair && !automatic) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1408 | tcsetattr(0, TCSANOW, &termios); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1409 | |
| 1410 | if (changed) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1411 | retcode += 3; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1412 | if (errors_uncorrected) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1413 | retcode += 4; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1414 | return retcode; |
| 1415 | } |