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