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