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