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