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