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