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