Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 2 | /* |
| 3 | * fsck.c - a file system consistency checker for Linux. |
| 4 | * |
Rob Landley | cc9962d | 2006-09-14 05:27:28 +0000 | [diff] [blame] | 5 | * (C) 1991, 1992 Linus Torvalds. |
| 6 | * |
| 7 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | /* |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 11 | * 09.11.91 - made the first rudimentary functions |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 12 | * |
| 13 | * 10.11.91 - updated, does checking, no repairs yet. |
| 14 | * Sent out to the mailing-list for testing. |
| 15 | * |
| 16 | * 14.11.91 - Testing seems to have gone well. Added some |
| 17 | * correction-code, and changed some functions. |
| 18 | * |
| 19 | * 15.11.91 - More correction code. Hopefully it notices most |
| 20 | * cases now, and tries to do something about them. |
| 21 | * |
| 22 | * 16.11.91 - More corrections (thanks to Mika Jalava). Most |
| 23 | * things seem to work now. Yeah, sure. |
| 24 | * |
| 25 | * |
| 26 | * 19.04.92 - Had to start over again from this old version, as a |
| 27 | * kernel bug ate my enhanced fsck in february. |
| 28 | * |
| 29 | * 28.02.93 - added support for different directory entry sizes.. |
| 30 | * |
| 31 | * Sat Mar 6 18:59:42 1993, faith@cs.unc.edu: Output namelen with |
Bernhard Reutner-Fischer | e1e5174 | 2008-07-21 13:33:22 +0000 | [diff] [blame] | 32 | * superblock information |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 33 | * |
| 34 | * Sat Oct 9 11:17:11 1993, faith@cs.unc.edu: make exit status conform |
| 35 | * to that required by fsutil |
| 36 | * |
| 37 | * Mon Jan 3 11:06:52 1994 - Dr. Wettstein (greg%wind.uucp@plains.nodak.edu) |
| 38 | * Added support for file system valid flag. Also |
| 39 | * added program_version variable and output of |
| 40 | * program name and version number when program |
| 41 | * is executed. |
| 42 | * |
| 43 | * 30.10.94 - added support for v2 filesystem |
| 44 | * (Andreas Schwab, schwab@issan.informatik.uni-dortmund.de) |
| 45 | * |
| 46 | * 10.12.94 - added test to prevent checking of mounted fs adapted |
| 47 | * from Theodore Ts'o's (tytso@athena.mit.edu) e2fsck |
| 48 | * program. (Daniel Quinlan, quinlan@yggdrasil.com) |
| 49 | * |
| 50 | * 01.07.96 - Fixed the v2 fs stuff to use the right #defines and such |
| 51 | * for modern libcs (janl@math.uio.no, Nicolai Langfeldt) |
| 52 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 53 | * 02.07.96 - Added C bit fiddling routines from rmk@ecs.soton.ac.uk |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 54 | * (Russell King). He made them for ARM. It would seem |
| 55 | * that the ARM is powerful enough to do this in C whereas |
| 56 | * i386 and m64k must use assembly to get it fast >:-) |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 57 | * This should make minix fsck system-independent. |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 58 | * (janl@math.uio.no, Nicolai Langfeldt) |
| 59 | * |
| 60 | * 04.11.96 - Added minor fixes from Andreas Schwab to avoid compiler |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 61 | * warnings. Added mc68k bitops from |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 62 | * Joerg Dorchain <dorchain@mpi-sb.mpg.de>. |
| 63 | * |
| 64 | * 06.11.96 - Added v2 code submitted by Joerg Dorchain, but written by |
| 65 | * Andreas Schwab. |
| 66 | * |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 67 | * 1999-02-22 Arkadiusz Mickiewicz <misiek@misiek.eu.org> |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 68 | * - added Native Language Support |
| 69 | * |
| 70 | * |
| 71 | * I've had no time to add comments - hopefully the function names |
| 72 | * are comments enough. As with all file system checkers, this assumes |
| 73 | * the file system is quiescent - don't use it on a mounted device |
| 74 | * unless you can be sure nobody is writing to it (and remember that the |
| 75 | * kernel can write to it when it searches for files). |
| 76 | * |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 77 | * Usage: fsck [-larvsm] device |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 78 | * -l for a listing of all the filenames |
| 79 | * -a for automatic repairs (not implemented) |
| 80 | * -r for repairs (interactive) (not implemented) |
| 81 | * -v for verbose (tells how many files) |
Bernhard Reutner-Fischer | e1e5174 | 2008-07-21 13:33:22 +0000 | [diff] [blame] | 82 | * -s for superblock info |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 83 | * -m for minix-like "mode not cleared" warnings |
| 84 | * -f force filesystem check even if filesystem marked as valid |
| 85 | * |
| 86 | * The device may be a block device or a image of one, but this isn't |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 87 | * enforced (but it's not much fun on a character device :-). |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 88 | */ |
| 89 | |
Rob Landley | cc9962d | 2006-09-14 05:27:28 +0000 | [diff] [blame] | 90 | #include <mntent.h> |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 91 | #include "libbb.h" |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 92 | #include "minix.h" |
Eric Andersen | 85c5520 | 2000-07-10 22:46:55 +0000 | [diff] [blame] | 93 | |
Eric Andersen | 85c5520 | 2000-07-10 22:46:55 +0000 | [diff] [blame] | 94 | #ifndef BLKGETSIZE |
| 95 | #define BLKGETSIZE _IO(0x12,96) /* return device size */ |
| 96 | #endif |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 97 | |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 98 | struct BUG_bad_inode_size { |
| 99 | char BUG_bad_inode1_size[(INODE_SIZE1 * MINIX1_INODES_PER_BLOCK != BLOCK_SIZE) ? -1 : 1]; |
| 100 | #if ENABLE_FEATURE_MINIX2 |
| 101 | char BUG_bad_inode2_size[(INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE) ? -1 : 1]; |
| 102 | #endif |
| 103 | }; |
| 104 | |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 105 | enum { |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 106 | #ifdef UNUSED |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 107 | MINIX1_LINK_MAX = 250, |
| 108 | MINIX2_LINK_MAX = 65530, |
| 109 | MINIX_I_MAP_SLOTS = 8, |
| 110 | MINIX_Z_MAP_SLOTS = 64, |
| 111 | MINIX_V1 = 0x0001, /* original minix fs */ |
| 112 | MINIX_V2 = 0x0002, /* minix V2 fs */ |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 113 | #endif |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 114 | MINIX_NAME_MAX = 255, /* # chars in a file name */ |
| 115 | }; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 116 | |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 117 | |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 118 | #if !ENABLE_FEATURE_MINIX2 |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 119 | enum { version2 = 0 }; |
| 120 | #endif |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 121 | |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 122 | enum { MAX_DEPTH = 32 }; |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 123 | |
Denis Vlasenko | e06f61d | 2008-06-25 10:00:58 +0000 | [diff] [blame] | 124 | enum { dev_fd = 3 }; |
| 125 | |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 126 | struct globals { |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 127 | #if ENABLE_FEATURE_MINIX2 |
| 128 | smallint version2; |
| 129 | #endif |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 130 | smallint changed; /* is filesystem modified? */ |
| 131 | smallint errors_uncorrected; /* flag if some error was not corrected */ |
| 132 | smallint termios_set; |
| 133 | smallint dirsize; |
| 134 | smallint namelen; |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 135 | const char *device_name; |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 136 | int directory, regular, blockdev, chardev, links, symlinks, total; |
| 137 | char *inode_buffer; |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 138 | |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 139 | char *inode_map; |
| 140 | char *zone_map; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 141 | |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 142 | unsigned char *inode_count; |
| 143 | unsigned char *zone_count; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 144 | |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 145 | /* File-name data */ |
| 146 | int name_depth; |
| 147 | char *name_component[MAX_DEPTH+1]; |
Denis Vlasenko | fad03bc | 2007-03-13 22:31:28 +0000 | [diff] [blame] | 148 | |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 149 | /* Bigger stuff */ |
| 150 | struct termios sv_termios; |
Bernhard Reutner-Fischer | e1e5174 | 2008-07-21 13:33:22 +0000 | [diff] [blame] | 151 | char superblock_buffer[BLOCK_SIZE]; |
Denis Vlasenko | fad03bc | 2007-03-13 22:31:28 +0000 | [diff] [blame] | 152 | char add_zone_ind_blk[BLOCK_SIZE]; |
| 153 | char add_zone_dind_blk[BLOCK_SIZE]; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame^] | 154 | IF_FEATURE_MINIX2(char add_zone_tind_blk[BLOCK_SIZE];) |
Denis Vlasenko | fad03bc | 2007-03-13 22:31:28 +0000 | [diff] [blame] | 155 | char check_file_blk[BLOCK_SIZE]; |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 156 | |
| 157 | /* File-name data */ |
| 158 | char current_name[MAX_DEPTH * MINIX_NAME_MAX]; |
| 159 | }; |
| 160 | |
| 161 | #define G (*ptr_to_globals) |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 162 | #if ENABLE_FEATURE_MINIX2 |
| 163 | #define version2 (G.version2 ) |
| 164 | #endif |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 165 | #define changed (G.changed ) |
| 166 | #define errors_uncorrected (G.errors_uncorrected ) |
| 167 | #define termios_set (G.termios_set ) |
| 168 | #define dirsize (G.dirsize ) |
| 169 | #define namelen (G.namelen ) |
| 170 | #define device_name (G.device_name ) |
| 171 | #define directory (G.directory ) |
| 172 | #define regular (G.regular ) |
| 173 | #define blockdev (G.blockdev ) |
| 174 | #define chardev (G.chardev ) |
| 175 | #define links (G.links ) |
| 176 | #define symlinks (G.symlinks ) |
| 177 | #define total (G.total ) |
| 178 | #define inode_buffer (G.inode_buffer ) |
| 179 | #define inode_map (G.inode_map ) |
| 180 | #define zone_map (G.zone_map ) |
| 181 | #define inode_count (G.inode_count ) |
| 182 | #define zone_count (G.zone_count ) |
| 183 | #define name_depth (G.name_depth ) |
| 184 | #define name_component (G.name_component ) |
| 185 | #define sv_termios (G.sv_termios ) |
Bernhard Reutner-Fischer | e1e5174 | 2008-07-21 13:33:22 +0000 | [diff] [blame] | 186 | #define superblock_buffer (G.superblock_buffer ) |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 187 | #define add_zone_ind_blk (G.add_zone_ind_blk ) |
| 188 | #define add_zone_dind_blk (G.add_zone_dind_blk ) |
| 189 | #define add_zone_tind_blk (G.add_zone_tind_blk ) |
| 190 | #define check_file_blk (G.check_file_blk ) |
| 191 | #define current_name (G.current_name ) |
| 192 | #define INIT_G() do { \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 193 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 194 | dirsize = 16; \ |
| 195 | namelen = 14; \ |
| 196 | current_name[0] = '/'; \ |
| 197 | /*current_name[1] = '\0';*/ \ |
| 198 | name_component[0] = ¤t_name[0]; \ |
| 199 | } while (0) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 200 | |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 201 | |
| 202 | #define OPTION_STR "larvsmf" |
| 203 | enum { |
| 204 | OPT_l = (1 << 0), |
| 205 | OPT_a = (1 << 1), |
| 206 | OPT_r = (1 << 2), |
| 207 | OPT_v = (1 << 3), |
| 208 | OPT_s = (1 << 4), |
| 209 | OPT_w = (1 << 5), |
| 210 | OPT_f = (1 << 6), |
| 211 | }; |
| 212 | #define OPT_list (option_mask32 & OPT_l) |
| 213 | #define OPT_automatic (option_mask32 & OPT_a) |
| 214 | #define OPT_repair (option_mask32 & OPT_r) |
| 215 | #define OPT_verbose (option_mask32 & OPT_v) |
| 216 | #define OPT_show (option_mask32 & OPT_s) |
| 217 | #define OPT_warn_mode (option_mask32 & OPT_w) |
| 218 | #define OPT_force (option_mask32 & OPT_f) |
| 219 | /* non-automatic repairs requested? */ |
| 220 | #define OPT_manual ((option_mask32 & (OPT_a|OPT_r)) == OPT_r) |
| 221 | |
| 222 | |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 223 | #define Inode1 (((struct minix1_inode *) inode_buffer)-1) |
| 224 | #define Inode2 (((struct minix2_inode *) inode_buffer)-1) |
| 225 | |
Bernhard Reutner-Fischer | e1e5174 | 2008-07-21 13:33:22 +0000 | [diff] [blame] | 226 | #define Super (*(struct minix_superblock *)(superblock_buffer)) |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 227 | |
| 228 | #if ENABLE_FEATURE_MINIX2 |
| 229 | # define ZONES ((unsigned)(version2 ? Super.s_zones : Super.s_nzones)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 230 | #else |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 231 | # define ZONES ((unsigned)(Super.s_nzones)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 232 | #endif |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 233 | #define INODES ((unsigned)Super.s_ninodes) |
| 234 | #define IMAPS ((unsigned)Super.s_imap_blocks) |
| 235 | #define ZMAPS ((unsigned)Super.s_zmap_blocks) |
| 236 | #define FIRSTZONE ((unsigned)Super.s_firstdatazone) |
| 237 | #define ZONESIZE ((unsigned)Super.s_log_zone_size) |
| 238 | #define MAXSIZE ((unsigned)Super.s_max_size) |
| 239 | #define MAGIC (Super.s_magic) |
| 240 | |
| 241 | /* gcc likes this more (code is smaller) than macro variant */ |
Denis Vlasenko | 3ad5d0c | 2007-06-12 20:54:54 +0000 | [diff] [blame] | 242 | static ALWAYS_INLINE unsigned div_roundup(unsigned size, unsigned n) |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 243 | { |
Denis Vlasenko | 150f402 | 2007-01-13 21:06:21 +0000 | [diff] [blame] | 244 | return (size + n-1) / n; |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 247 | #if !ENABLE_FEATURE_MINIX2 |
| 248 | #define INODE_BLOCKS div_roundup(INODES, MINIX1_INODES_PER_BLOCK) |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 249 | #else |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 250 | #define INODE_BLOCKS div_roundup(INODES, \ |
| 251 | (version2 ? MINIX2_INODES_PER_BLOCK : MINIX1_INODES_PER_BLOCK)) |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 252 | #endif |
| 253 | |
Denis Vlasenko | 7049ff8 | 2008-06-25 09:53:17 +0000 | [diff] [blame] | 254 | #define INODE_BUFFER_SIZE (INODE_BLOCKS * BLOCK_SIZE) |
| 255 | #define NORM_FIRSTZONE (2 + IMAPS + ZMAPS + INODE_BLOCKS) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 256 | |
Denis Vlasenko | cc24419 | 2007-02-06 00:37:40 +0000 | [diff] [blame] | 257 | /* Before you ask "where they come from?": */ |
| 258 | /* setbit/clrbit are supplied by sys/param.h */ |
| 259 | |
| 260 | static int minix_bit(const char *a, unsigned i) |
Eric Andersen | 56f3e35 | 2000-09-19 21:13:55 +0000 | [diff] [blame] | 261 | { |
Denis Vlasenko | dc485c9 | 2007-02-06 00:36:53 +0000 | [diff] [blame] | 262 | return (a[i >> 3] & (1<<(i & 7))); |
Eric Andersen | 56f3e35 | 2000-09-19 21:13:55 +0000 | [diff] [blame] | 263 | } |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 264 | |
Denis Vlasenko | cc24419 | 2007-02-06 00:37:40 +0000 | [diff] [blame] | 265 | static void minix_setbit(char *a, unsigned i) |
| 266 | { |
| 267 | setbit(a, i); |
| 268 | changed = 1; |
| 269 | } |
| 270 | static void minix_clrbit(char *a, unsigned i) |
| 271 | { |
| 272 | clrbit(a, i); |
| 273 | changed = 1; |
| 274 | } |
Denis Vlasenko | dc485c9 | 2007-02-06 00:36:53 +0000 | [diff] [blame] | 275 | |
| 276 | /* Note: do not assume 0/1, it is 0/nonzero */ |
Denis Vlasenko | cc24419 | 2007-02-06 00:37:40 +0000 | [diff] [blame] | 277 | #define zone_in_use(x) (minix_bit(zone_map,(x)-FIRSTZONE+1)) |
| 278 | #define inode_in_use(x) (minix_bit(inode_map,(x))) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 279 | |
Denis Vlasenko | cc24419 | 2007-02-06 00:37:40 +0000 | [diff] [blame] | 280 | #define mark_inode(x) (minix_setbit(inode_map,(x))) |
| 281 | #define unmark_inode(x) (minix_clrbit(inode_map,(x))) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 282 | |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 283 | #define mark_zone(x) (minix_setbit(zone_map,(x)-FIRSTZONE+1)) |
| 284 | #define unmark_zone(x) (minix_clrbit(zone_map,(x)-FIRSTZONE+1)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 285 | |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 286 | |
Denis Vlasenko | 73464ce | 2007-01-03 00:45:05 +0000 | [diff] [blame] | 287 | static void recursive_check(unsigned ino); |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 288 | #if ENABLE_FEATURE_MINIX2 |
Denis Vlasenko | 73464ce | 2007-01-03 00:45:05 +0000 | [diff] [blame] | 289 | static void recursive_check2(unsigned ino); |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 290 | #endif |
| 291 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 292 | static void die(const char *str) NORETURN; |
Denis Vlasenko | 75fa4eb | 2007-01-03 01:36:42 +0000 | [diff] [blame] | 293 | static void die(const char *str) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 294 | { |
| 295 | if (termios_set) |
Denis Vlasenko | 202ac50 | 2008-11-05 13:20:58 +0000 | [diff] [blame] | 296 | tcsetattr_stdin_TCSANOW(&sv_termios); |
Denis Vlasenko | 75fa4eb | 2007-01-03 01:36:42 +0000 | [diff] [blame] | 297 | bb_error_msg_and_die("%s", str); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Rob Landley | 3ea66a8 | 2006-04-29 20:08:40 +0000 | [diff] [blame] | 300 | static void push_filename(const char *name) |
| 301 | { |
| 302 | // /dir/dir/dir/file |
| 303 | // ^ ^ ^ |
| 304 | // [0] [1] [2] <-name_component[i] |
| 305 | if (name_depth < MAX_DEPTH) { |
| 306 | int len; |
| 307 | char *p = name_component[name_depth]; |
| 308 | *p++ = '/'; |
| 309 | len = sprintf(p, "%.*s", namelen, name); |
| 310 | name_component[name_depth + 1] = p + len; |
| 311 | } |
| 312 | name_depth++; |
| 313 | } |
| 314 | |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 315 | static void pop_filename(void) |
| 316 | { |
Rob Landley | 3ea66a8 | 2006-04-29 20:08:40 +0000 | [diff] [blame] | 317 | name_depth--; |
| 318 | if (name_depth < MAX_DEPTH) { |
| 319 | *name_component[name_depth] = '\0'; |
| 320 | if (!name_depth) { |
| 321 | current_name[0] = '/'; |
| 322 | current_name[1] = '\0'; |
| 323 | } |
| 324 | } |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 327 | static int ask(const char *string, int def) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 328 | { |
| 329 | int c; |
| 330 | |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 331 | if (!OPT_repair) { |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 332 | bb_putchar('\n'); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 333 | errors_uncorrected = 1; |
| 334 | return 0; |
| 335 | } |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 336 | if (OPT_automatic) { |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 337 | bb_putchar('\n'); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 338 | if (!def) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 339 | errors_uncorrected = 1; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 340 | return def; |
| 341 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 342 | printf(def ? "%s (y/n)? " : "%s (n/y)? ", string); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 343 | for (;;) { |
| 344 | fflush(stdout); |
Denis Vlasenko | 6dd392a | 2007-01-03 00:41:53 +0000 | [diff] [blame] | 345 | c = getchar(); |
| 346 | if (c == EOF) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 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; |
Bernhard Reutner-Fischer | cb83abd | 2008-05-19 08:40:26 +0000 | [diff] [blame] | 381 | //XXX:FIXME use find_mount_point() |
Denis Vlasenko | 6dd392a | 2007-01-03 00:41:53 +0000 | [diff] [blame] | 382 | f = setmntent(MOUNTED, "r"); |
| 383 | if (f == NULL) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 384 | return; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 385 | while ((mnt = getmntent(f)) != NULL) |
| 386 | if (strcmp(device_name, mnt->mnt_fsname) == 0) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 387 | break; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 388 | endmntent(f); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 389 | if (!mnt) |
| 390 | return; |
| 391 | |
| 392 | /* |
| 393 | * If the root is mounted read-only, then /etc/mtab is |
| 394 | * probably not correct; so we won't issue a warning based on |
| 395 | * it. |
| 396 | */ |
| 397 | fd = open(MOUNTED, O_RDWR); |
| 398 | if (fd < 0 && errno == EROFS) |
| 399 | return; |
Denis Vlasenko | 6dd392a | 2007-01-03 00:41:53 +0000 | [diff] [blame] | 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"); |
Bernhard Reutner-Fischer | cb83abd | 2008-05-19 08:40:26 +0000 | [diff] [blame] | 408 | exit(EXIT_SUCCESS); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 409 | } |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | /* |
| 413 | * check_zone_nr checks to see that *nr is a valid zone nr. If it |
| 414 | * isn't, it will possibly be repaired. Check_zone_nr sets *corrected |
| 415 | * if an error was corrected, and returns the zone (0 for no zone |
| 416 | * or a bad zone-number). |
| 417 | */ |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 418 | static int check_zone_nr2(uint32_t *nr, smallint *corrected) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 419 | { |
Rob Landley | 3ea66a8 | 2006-04-29 20:08:40 +0000 | [diff] [blame] | 420 | const char *msg; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 421 | if (!*nr) |
| 422 | return 0; |
| 423 | if (*nr < FIRSTZONE) |
Rob Landley | 3ea66a8 | 2006-04-29 20:08:40 +0000 | [diff] [blame] | 424 | msg = "< FIRSTZONE"; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 425 | else if (*nr >= ZONES) |
Rob Landley | 3ea66a8 | 2006-04-29 20:08:40 +0000 | [diff] [blame] | 426 | msg = ">= ZONES"; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 427 | else |
| 428 | return *nr; |
Rob Landley | fd8409f | 2006-06-13 15:31:39 +0000 | [diff] [blame] | 429 | printf("Zone nr %s in file '%s'. ", msg, current_name); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 430 | if (ask("Remove block", 1)) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 431 | *nr = 0; |
| 432 | *corrected = 1; |
| 433 | } |
| 434 | return 0; |
| 435 | } |
| 436 | |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 437 | static int check_zone_nr(uint16_t *nr, smallint *corrected) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 438 | { |
Rob Landley | 8a62549 | 2006-04-29 20:05:17 +0000 | [diff] [blame] | 439 | uint32_t nr32 = *nr; |
| 440 | int r = check_zone_nr2(&nr32, corrected); |
| 441 | *nr = (uint16_t)nr32; |
| 442 | return r; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 443 | } |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 444 | |
| 445 | /* |
| 446 | * read-block reads block nr into the buffer at addr. |
| 447 | */ |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 448 | static void read_block(unsigned nr, void *addr) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 449 | { |
| 450 | if (!nr) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 451 | memset(addr, 0, BLOCK_SIZE); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 452 | return; |
| 453 | } |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 454 | xlseek(dev_fd, BLOCK_SIZE * nr, SEEK_SET); |
| 455 | if (BLOCK_SIZE != full_read(dev_fd, addr, BLOCK_SIZE)) { |
| 456 | printf("%s: bad block %u in file '%s'\n", |
| 457 | bb_msg_read_error, nr, 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 | } |
| 461 | } |
| 462 | |
| 463 | /* |
| 464 | * write_block writes block nr to disk. |
| 465 | */ |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 466 | static void write_block(unsigned nr, void *addr) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 467 | { |
| 468 | if (!nr) |
| 469 | return; |
| 470 | if (nr < FIRSTZONE || nr >= ZONES) { |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 471 | printf("Internal error: trying to write bad block\n" |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 472 | "Write request ignored\n"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 473 | errors_uncorrected = 1; |
| 474 | return; |
| 475 | } |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 476 | xlseek(dev_fd, BLOCK_SIZE * nr, SEEK_SET); |
| 477 | if (BLOCK_SIZE != full_write(dev_fd, addr, BLOCK_SIZE)) { |
| 478 | printf("%s: bad block %u in file '%s'\n", |
| 479 | bb_msg_write_error, nr, current_name); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 480 | errors_uncorrected = 1; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | /* |
Rob Landley | 8a62549 | 2006-04-29 20:05:17 +0000 | [diff] [blame] | 485 | * 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] | 486 | * It sets 'changed' if the inode has needed changing, and re-writes |
| 487 | * any indirect blocks with errors. |
| 488 | */ |
Denis Vlasenko | 73464ce | 2007-01-03 00:45:05 +0000 | [diff] [blame] | 489 | static int map_block(struct minix1_inode *inode, unsigned blknr) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 490 | { |
Rob Landley | 8a62549 | 2006-04-29 20:05:17 +0000 | [diff] [blame] | 491 | uint16_t ind[BLOCK_SIZE >> 1]; |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 492 | int block, result; |
| 493 | smallint blk_chg; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 494 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 495 | if (blknr < 7) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 496 | return check_zone_nr(inode->i_zone + blknr, &changed); |
| 497 | blknr -= 7; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 498 | if (blknr < 512) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 499 | block = check_zone_nr(inode->i_zone + 7, &changed); |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 500 | goto common; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 501 | } |
| 502 | blknr -= 512; |
| 503 | block = check_zone_nr(inode->i_zone + 8, &changed); |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 504 | read_block(block, ind); /* double indirect */ |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 505 | blk_chg = 0; |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 506 | result = check_zone_nr(&ind[blknr / 512], &blk_chg); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 507 | if (blk_chg) |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 508 | write_block(block, ind); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 509 | block = result; |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 510 | common: |
| 511 | read_block(block, ind); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 512 | blk_chg = 0; |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 513 | result = check_zone_nr(&ind[blknr % 512], &blk_chg); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 514 | if (blk_chg) |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 515 | write_block(block, ind); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 516 | return result; |
| 517 | } |
| 518 | |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 519 | #if ENABLE_FEATURE_MINIX2 |
Denis Vlasenko | 73464ce | 2007-01-03 00:45:05 +0000 | [diff] [blame] | 520 | static int map_block2(struct minix2_inode *inode, unsigned blknr) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 521 | { |
Rob Landley | 8a62549 | 2006-04-29 20:05:17 +0000 | [diff] [blame] | 522 | uint32_t ind[BLOCK_SIZE >> 2]; |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 523 | int block, result; |
| 524 | smallint blk_chg; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 525 | |
| 526 | if (blknr < 7) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 527 | return check_zone_nr2(inode->i_zone + blknr, &changed); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 528 | blknr -= 7; |
| 529 | if (blknr < 256) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 530 | block = check_zone_nr2(inode->i_zone + 7, &changed); |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 531 | goto common2; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 532 | } |
| 533 | blknr -= 256; |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 534 | if (blknr < 256 * 256) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 535 | block = check_zone_nr2(inode->i_zone + 8, &changed); |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 536 | goto common1; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 537 | } |
| 538 | blknr -= 256 * 256; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 539 | block = check_zone_nr2(inode->i_zone + 9, &changed); |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 540 | read_block(block, ind); /* triple indirect */ |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 541 | blk_chg = 0; |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 542 | result = check_zone_nr2(&ind[blknr / (256 * 256)], &blk_chg); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 543 | if (blk_chg) |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 544 | write_block(block, ind); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 545 | block = result; |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 546 | common1: |
| 547 | read_block(block, ind); /* double indirect */ |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 548 | blk_chg = 0; |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 549 | result = check_zone_nr2(&ind[(blknr / 256) % 256], &blk_chg); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 550 | if (blk_chg) |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 551 | write_block(block, ind); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 552 | block = result; |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 553 | common2: |
| 554 | read_block(block, ind); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 555 | blk_chg = 0; |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 556 | result = check_zone_nr2(&ind[blknr % 256], &blk_chg); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 557 | if (blk_chg) |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 558 | write_block(block, ind); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 559 | return result; |
| 560 | } |
| 561 | #endif |
| 562 | |
Bernhard Reutner-Fischer | e1e5174 | 2008-07-21 13:33:22 +0000 | [diff] [blame] | 563 | static void write_superblock(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 564 | { |
| 565 | /* |
| 566 | * Set the state of the filesystem based on whether or not there |
| 567 | * are uncorrected errors. The filesystem valid flag is |
| 568 | * unconditionally set if we get this far. |
| 569 | */ |
Denis Vlasenko | 6dd392a | 2007-01-03 00:41:53 +0000 | [diff] [blame] | 570 | Super.s_state |= MINIX_VALID_FS | MINIX_ERROR_FS; |
| 571 | if (!errors_uncorrected) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 572 | Super.s_state &= ~MINIX_ERROR_FS; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 573 | |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 574 | xlseek(dev_fd, BLOCK_SIZE, SEEK_SET); |
Bernhard Reutner-Fischer | e1e5174 | 2008-07-21 13:33:22 +0000 | [diff] [blame] | 575 | if (BLOCK_SIZE != full_write(dev_fd, superblock_buffer, BLOCK_SIZE)) |
| 576 | die("cannot write superblock"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 579 | static void write_tables(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 580 | { |
Bernhard Reutner-Fischer | e1e5174 | 2008-07-21 13:33:22 +0000 | [diff] [blame] | 581 | write_superblock(); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 582 | |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 583 | if (IMAPS * BLOCK_SIZE != write(dev_fd, inode_map, IMAPS * BLOCK_SIZE)) |
Denis Vlasenko | 6dd392a | 2007-01-03 00:41:53 +0000 | [diff] [blame] | 584 | die("cannot write inode map"); |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 585 | if (ZMAPS * BLOCK_SIZE != write(dev_fd, zone_map, ZMAPS * BLOCK_SIZE)) |
Denis Vlasenko | 6dd392a | 2007-01-03 00:41:53 +0000 | [diff] [blame] | 586 | die("cannot write zone map"); |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 587 | if (INODE_BUFFER_SIZE != write(dev_fd, inode_buffer, INODE_BUFFER_SIZE)) |
Denis Vlasenko | 6dd392a | 2007-01-03 00:41:53 +0000 | [diff] [blame] | 588 | die("cannot write inodes"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 591 | static void get_dirsize(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 592 | { |
| 593 | int block; |
| 594 | char blk[BLOCK_SIZE]; |
| 595 | int size; |
| 596 | |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 597 | #if ENABLE_FEATURE_MINIX2 |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 598 | if (version2) |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 599 | block = Inode2[MINIX_ROOT_INO].i_zone[0]; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 600 | else |
| 601 | #endif |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 602 | block = Inode1[MINIX_ROOT_INO].i_zone[0]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 603 | read_block(block, blk); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 604 | for (size = 16; size < BLOCK_SIZE; size <<= 1) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 605 | if (strcmp(blk + size + 2, "..") == 0) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 606 | dirsize = size; |
| 607 | namelen = size - 2; |
| 608 | return; |
| 609 | } |
| 610 | } |
| 611 | /* use defaults */ |
| 612 | } |
| 613 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 614 | static void read_superblock(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 615 | { |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 616 | xlseek(dev_fd, BLOCK_SIZE, SEEK_SET); |
Bernhard Reutner-Fischer | e1e5174 | 2008-07-21 13:33:22 +0000 | [diff] [blame] | 617 | if (BLOCK_SIZE != full_read(dev_fd, superblock_buffer, BLOCK_SIZE)) |
| 618 | die("cannot read superblock"); |
Rob Landley | 3ea66a8 | 2006-04-29 20:08:40 +0000 | [diff] [blame] | 619 | /* already initialized to: |
Rob Landley | 8a62549 | 2006-04-29 20:05:17 +0000 | [diff] [blame] | 620 | namelen = 14; |
| 621 | dirsize = 16; |
| 622 | version2 = 0; |
Rob Landley | 3ea66a8 | 2006-04-29 20:08:40 +0000 | [diff] [blame] | 623 | */ |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 624 | if (MAGIC == MINIX1_SUPER_MAGIC) { |
| 625 | } else if (MAGIC == MINIX1_SUPER_MAGIC2) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 626 | namelen = 30; |
| 627 | dirsize = 32; |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 628 | #if ENABLE_FEATURE_MINIX2 |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 629 | } else if (MAGIC == MINIX2_SUPER_MAGIC) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 630 | version2 = 1; |
| 631 | } else if (MAGIC == MINIX2_SUPER_MAGIC2) { |
| 632 | namelen = 30; |
| 633 | dirsize = 32; |
| 634 | version2 = 1; |
| 635 | #endif |
| 636 | } else |
Bernhard Reutner-Fischer | e1e5174 | 2008-07-21 13:33:22 +0000 | [diff] [blame] | 637 | die("bad magic number in superblock"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 638 | if (ZONESIZE != 0 || BLOCK_SIZE != 1024) |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 639 | die("only 1k blocks/zones supported"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 640 | if (IMAPS * BLOCK_SIZE * 8 < INODES + 1) |
Bernhard Reutner-Fischer | e1e5174 | 2008-07-21 13:33:22 +0000 | [diff] [blame] | 641 | die("bad s_imap_blocks field in superblock"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 642 | if (ZMAPS * BLOCK_SIZE * 8 < ZONES - FIRSTZONE + 1) |
Bernhard Reutner-Fischer | e1e5174 | 2008-07-21 13:33:22 +0000 | [diff] [blame] | 643 | die("bad s_zmap_blocks field in superblock"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 646 | static void read_tables(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 647 | { |
Rob Landley | 9a1a9de | 2006-06-13 15:12:14 +0000 | [diff] [blame] | 648 | inode_map = xzalloc(IMAPS * BLOCK_SIZE); |
| 649 | zone_map = xzalloc(ZMAPS * BLOCK_SIZE); |
Erik Andersen | 0d068a2 | 2000-03-21 22:32:57 +0000 | [diff] [blame] | 650 | inode_buffer = xmalloc(INODE_BUFFER_SIZE); |
| 651 | inode_count = xmalloc(INODES + 1); |
| 652 | zone_count = xmalloc(ZONES); |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 653 | if (IMAPS * BLOCK_SIZE != read(dev_fd, inode_map, IMAPS * BLOCK_SIZE)) |
Denis Vlasenko | 6dd392a | 2007-01-03 00:41:53 +0000 | [diff] [blame] | 654 | die("cannot read inode map"); |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 655 | if (ZMAPS * BLOCK_SIZE != read(dev_fd, zone_map, ZMAPS * BLOCK_SIZE)) |
Denis Vlasenko | 6dd392a | 2007-01-03 00:41:53 +0000 | [diff] [blame] | 656 | die("cannot read zone map"); |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 657 | if (INODE_BUFFER_SIZE != read(dev_fd, inode_buffer, INODE_BUFFER_SIZE)) |
Denis Vlasenko | 6dd392a | 2007-01-03 00:41:53 +0000 | [diff] [blame] | 658 | die("cannot read inodes"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 659 | if (NORM_FIRSTZONE != FIRSTZONE) { |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 660 | printf("warning: firstzone!=norm_firstzone\n"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 661 | errors_uncorrected = 1; |
| 662 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 663 | get_dirsize(); |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 664 | if (OPT_show) { |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 665 | printf("%u inodes\n" |
| 666 | "%u blocks\n" |
| 667 | "Firstdatazone=%u (%u)\n" |
| 668 | "Zonesize=%u\n" |
| 669 | "Maxsize=%u\n" |
| 670 | "Filesystem state=%u\n" |
| 671 | "namelen=%u\n\n", |
Rob Landley | 8a62549 | 2006-04-29 20:05:17 +0000 | [diff] [blame] | 672 | INODES, |
| 673 | ZONES, |
| 674 | FIRSTZONE, NORM_FIRSTZONE, |
| 675 | BLOCK_SIZE << ZONESIZE, |
| 676 | MAXSIZE, |
| 677 | Super.s_state, |
| 678 | namelen); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 679 | } |
| 680 | } |
| 681 | |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 682 | static void get_inode_common(unsigned nr, uint16_t i_mode) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 683 | { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 684 | total++; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 685 | if (!inode_count[nr]) { |
| 686 | if (!inode_in_use(nr)) { |
Denis Vlasenko | 73464ce | 2007-01-03 00:45:05 +0000 | [diff] [blame] | 687 | printf("Inode %d is marked as 'unused', but it is used " |
Rob Landley | fd8409f | 2006-06-13 15:31:39 +0000 | [diff] [blame] | 688 | "for file '%s'\n", nr, current_name); |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 689 | if (OPT_repair) { |
Rob Landley | fd8409f | 2006-06-13 15:31:39 +0000 | [diff] [blame] | 690 | if (ask("Mark as 'in use'", 1)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 691 | mark_inode(nr); |
Denis Vlasenko | cc24419 | 2007-02-06 00:37:40 +0000 | [diff] [blame] | 692 | else |
| 693 | errors_uncorrected = 1; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 694 | } |
| 695 | } |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 696 | if (S_ISDIR(i_mode)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 697 | directory++; |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 698 | else if (S_ISREG(i_mode)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 699 | regular++; |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 700 | else if (S_ISCHR(i_mode)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 701 | chardev++; |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 702 | else if (S_ISBLK(i_mode)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 703 | blockdev++; |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 704 | else if (S_ISLNK(i_mode)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 705 | symlinks++; |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 706 | else if (S_ISSOCK(i_mode)); |
| 707 | else if (S_ISFIFO(i_mode)); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 708 | else { |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 709 | printf("%s has mode %05o\n", current_name, i_mode); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 710 | } |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 711 | } else |
| 712 | links++; |
| 713 | if (!++inode_count[nr]) { |
Rob Landley | 8a62549 | 2006-04-29 20:05:17 +0000 | [diff] [blame] | 714 | printf("Warning: inode count too big\n"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 715 | inode_count[nr]--; |
| 716 | errors_uncorrected = 1; |
| 717 | } |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | static struct minix1_inode *get_inode(unsigned nr) |
| 721 | { |
| 722 | struct minix1_inode *inode; |
| 723 | |
| 724 | if (!nr || nr > INODES) |
| 725 | return NULL; |
| 726 | inode = Inode1 + nr; |
| 727 | get_inode_common(nr, inode->i_mode); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 728 | return inode; |
| 729 | } |
| 730 | |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 731 | #if ENABLE_FEATURE_MINIX2 |
Denis Vlasenko | 73464ce | 2007-01-03 00:45:05 +0000 | [diff] [blame] | 732 | static struct minix2_inode *get_inode2(unsigned nr) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 733 | { |
| 734 | struct minix2_inode *inode; |
| 735 | |
| 736 | if (!nr || nr > INODES) |
| 737 | return NULL; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 738 | inode = Inode2 + nr; |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 739 | get_inode_common(nr, inode->i_mode); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 740 | return inode; |
| 741 | } |
| 742 | #endif |
| 743 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 744 | static void check_root(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 745 | { |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 746 | struct minix1_inode *inode = Inode1 + MINIX_ROOT_INO; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 747 | |
| 748 | if (!inode || !S_ISDIR(inode->i_mode)) |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 749 | die("root inode isn't a directory"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 752 | #if ENABLE_FEATURE_MINIX2 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 753 | static void check_root2(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 754 | { |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 755 | struct minix2_inode *inode = Inode2 + MINIX_ROOT_INO; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 756 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 757 | if (!inode || !S_ISDIR(inode->i_mode)) |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 758 | die("root inode isn't a directory"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 759 | } |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 760 | #else |
| 761 | void check_root2(void); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 762 | #endif |
| 763 | |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 764 | static int add_zone_common(int block, smallint *corrected) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 765 | { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 766 | if (!block) |
| 767 | return 0; |
| 768 | if (zone_count[block]) { |
Rob Landley | fd8409f | 2006-06-13 15:31:39 +0000 | [diff] [blame] | 769 | printf("Already used block is reused in file '%s'. ", |
Rob Landley | 3ea66a8 | 2006-04-29 20:08:40 +0000 | [diff] [blame] | 770 | current_name); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 771 | if (ask("Clear", 1)) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 772 | block = 0; |
| 773 | *corrected = 1; |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 774 | return -1; /* "please zero out *znr" */ |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 775 | } |
| 776 | } |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 777 | if (!zone_in_use(block)) { |
Rob Landley | fd8409f | 2006-06-13 15:31:39 +0000 | [diff] [blame] | 778 | printf("Block %d in file '%s' is marked as 'unused'. ", |
Rob Landley | 3ea66a8 | 2006-04-29 20:08:40 +0000 | [diff] [blame] | 779 | block, current_name); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 780 | if (ask("Correct", 1)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 781 | mark_zone(block); |
| 782 | } |
| 783 | if (!++zone_count[block]) |
| 784 | zone_count[block]--; |
| 785 | return block; |
| 786 | } |
| 787 | |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 788 | static int add_zone(uint16_t *znr, smallint *corrected) |
| 789 | { |
| 790 | int block; |
| 791 | |
| 792 | block = check_zone_nr(znr, corrected); |
| 793 | block = add_zone_common(block, corrected); |
| 794 | if (block == -1) { |
| 795 | *znr = 0; |
| 796 | block = 0; |
| 797 | } |
| 798 | return block; |
| 799 | } |
| 800 | |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 801 | #if ENABLE_FEATURE_MINIX2 |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 802 | static int add_zone2(uint32_t *znr, smallint *corrected) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 803 | { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 804 | int block; |
| 805 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 806 | block = check_zone_nr2(znr, corrected); |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 807 | block = add_zone_common(block, corrected); |
| 808 | if (block == -1) { |
| 809 | *znr = 0; |
| 810 | block = 0; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 811 | } |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 812 | return block; |
| 813 | } |
| 814 | #endif |
| 815 | |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 816 | static void add_zone_ind(uint16_t *znr, smallint *corrected) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 817 | { |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 818 | int i; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 819 | int block; |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 820 | smallint chg_blk = 0; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 821 | |
| 822 | block = add_zone(znr, corrected); |
| 823 | if (!block) |
| 824 | return; |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 825 | read_block(block, add_zone_ind_blk); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 826 | for (i = 0; i < (BLOCK_SIZE >> 1); i++) |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 827 | add_zone(i + (uint16_t *) add_zone_ind_blk, &chg_blk); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 828 | if (chg_blk) |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 829 | write_block(block, add_zone_ind_blk); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 832 | #if ENABLE_FEATURE_MINIX2 |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 833 | static void add_zone_ind2(uint32_t *znr, smallint *corrected) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 834 | { |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 835 | int i; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 836 | int block; |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 837 | smallint chg_blk = 0; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 838 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 839 | block = add_zone2(znr, corrected); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 840 | if (!block) |
| 841 | return; |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 842 | read_block(block, add_zone_ind_blk); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 843 | for (i = 0; i < BLOCK_SIZE >> 2; i++) |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 844 | add_zone2(i + (uint32_t *) add_zone_ind_blk, &chg_blk); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 845 | if (chg_blk) |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 846 | write_block(block, add_zone_ind_blk); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 847 | } |
| 848 | #endif |
| 849 | |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 850 | static void add_zone_dind(uint16_t *znr, smallint *corrected) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 851 | { |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 852 | int i; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 853 | int block; |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 854 | smallint chg_blk = 0; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 855 | |
| 856 | block = add_zone(znr, corrected); |
| 857 | if (!block) |
| 858 | return; |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 859 | read_block(block, add_zone_dind_blk); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 860 | for (i = 0; i < (BLOCK_SIZE >> 1); i++) |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 861 | add_zone_ind(i + (uint16_t *) add_zone_dind_blk, &chg_blk); |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 862 | if (chg_blk) |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 863 | write_block(block, add_zone_dind_blk); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 864 | } |
| 865 | |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 866 | #if ENABLE_FEATURE_MINIX2 |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 867 | static void add_zone_dind2(uint32_t *znr, smallint *corrected) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 868 | { |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 869 | int i; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 870 | int block; |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 871 | smallint chg_blk = 0; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 872 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 873 | block = add_zone2(znr, corrected); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 874 | if (!block) |
| 875 | return; |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 876 | read_block(block, add_zone_dind_blk); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 877 | for (i = 0; i < BLOCK_SIZE >> 2; i++) |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 878 | add_zone_ind2(i + (uint32_t *) add_zone_dind_blk, &chg_blk); |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 879 | if (chg_blk) |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 880 | write_block(block, add_zone_dind_blk); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 881 | } |
| 882 | |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 883 | static void add_zone_tind2(uint32_t *znr, smallint *corrected) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 884 | { |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 885 | int i; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 886 | int block; |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 887 | smallint chg_blk = 0; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 888 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 889 | block = add_zone2(znr, corrected); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 890 | if (!block) |
| 891 | return; |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 892 | read_block(block, add_zone_tind_blk); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 893 | for (i = 0; i < BLOCK_SIZE >> 2; i++) |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 894 | add_zone_dind2(i + (uint32_t *) add_zone_tind_blk, &chg_blk); |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 895 | if (chg_blk) |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 896 | write_block(block, add_zone_tind_blk); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 897 | } |
| 898 | #endif |
| 899 | |
Denis Vlasenko | 73464ce | 2007-01-03 00:45:05 +0000 | [diff] [blame] | 900 | static void check_zones(unsigned i) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 901 | { |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 902 | struct minix1_inode *inode; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 903 | |
| 904 | if (!i || i > INODES) |
| 905 | return; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 906 | if (inode_count[i] > 1) /* have we counted this file already? */ |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 907 | return; |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 908 | inode = Inode1 + i; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 909 | if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) && |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 910 | !S_ISLNK(inode->i_mode)) return; |
| 911 | for (i = 0; i < 7; i++) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 912 | add_zone(i + inode->i_zone, &changed); |
| 913 | add_zone_ind(7 + inode->i_zone, &changed); |
| 914 | add_zone_dind(8 + inode->i_zone, &changed); |
| 915 | } |
| 916 | |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 917 | #if ENABLE_FEATURE_MINIX2 |
Denis Vlasenko | 73464ce | 2007-01-03 00:45:05 +0000 | [diff] [blame] | 918 | static void check_zones2(unsigned i) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 919 | { |
| 920 | struct minix2_inode *inode; |
| 921 | |
| 922 | if (!i || i > INODES) |
| 923 | return; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 924 | if (inode_count[i] > 1) /* have we counted this file already? */ |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 925 | return; |
| 926 | inode = Inode2 + i; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 927 | if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) |
| 928 | && !S_ISLNK(inode->i_mode)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 929 | return; |
| 930 | for (i = 0; i < 7; i++) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 931 | add_zone2(i + inode->i_zone, &changed); |
| 932 | add_zone_ind2(7 + inode->i_zone, &changed); |
| 933 | add_zone_dind2(8 + inode->i_zone, &changed); |
| 934 | add_zone_tind2(9 + inode->i_zone, &changed); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 935 | } |
| 936 | #endif |
| 937 | |
Denis Vlasenko | 73464ce | 2007-01-03 00:45:05 +0000 | [diff] [blame] | 938 | static void check_file(struct minix1_inode *dir, unsigned offset) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 939 | { |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 940 | struct minix1_inode *inode; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 941 | int ino; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 942 | char *name; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 943 | int block; |
| 944 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 945 | block = map_block(dir, offset / BLOCK_SIZE); |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 946 | read_block(block, check_file_blk); |
| 947 | name = check_file_blk + (offset % BLOCK_SIZE) + 2; |
Rob Landley | 8a62549 | 2006-04-29 20:05:17 +0000 | [diff] [blame] | 948 | ino = *(uint16_t *) (name - 2); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 949 | if (ino > INODES) { |
Rob Landley | fd8409f | 2006-06-13 15:31:39 +0000 | [diff] [blame] | 950 | printf("%s contains a bad inode number for file '%.*s'. ", |
Rob Landley | 3ea66a8 | 2006-04-29 20:08:40 +0000 | [diff] [blame] | 951 | current_name, namelen, name); |
Rob Landley | fd8409f | 2006-06-13 15:31:39 +0000 | [diff] [blame] | 952 | if (ask("Remove", 1)) { |
Rob Landley | 8a62549 | 2006-04-29 20:05:17 +0000 | [diff] [blame] | 953 | *(uint16_t *) (name - 2) = 0; |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 954 | write_block(block, check_file_blk); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 955 | } |
| 956 | ino = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 957 | } |
Rob Landley | 3ea66a8 | 2006-04-29 20:08:40 +0000 | [diff] [blame] | 958 | push_filename(name); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 959 | inode = get_inode(ino); |
Rob Landley | 3ea66a8 | 2006-04-29 20:08:40 +0000 | [diff] [blame] | 960 | pop_filename(); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 961 | if (!offset) { |
Denis Vlasenko | 6dd392a | 2007-01-03 00:41:53 +0000 | [diff] [blame] | 962 | if (inode && LONE_CHAR(name, '.')) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 963 | return; |
Denis Vlasenko | 6dd392a | 2007-01-03 00:41:53 +0000 | [diff] [blame] | 964 | printf("%s: bad directory: '.' isn't first\n", current_name); |
| 965 | errors_uncorrected = 1; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 966 | } |
| 967 | if (offset == dirsize) { |
Denis Vlasenko | 6dd392a | 2007-01-03 00:41:53 +0000 | [diff] [blame] | 968 | if (inode && strcmp("..", name) == 0) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 969 | return; |
Denis Vlasenko | 6dd392a | 2007-01-03 00:41:53 +0000 | [diff] [blame] | 970 | printf("%s: bad directory: '..' isn't second\n", current_name); |
| 971 | errors_uncorrected = 1; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 972 | } |
| 973 | if (!inode) |
| 974 | return; |
Rob Landley | 3ea66a8 | 2006-04-29 20:08:40 +0000 | [diff] [blame] | 975 | push_filename(name); |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 976 | if (OPT_list) { |
| 977 | if (OPT_verbose) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 978 | printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks); |
Rob Landley | 3ea66a8 | 2006-04-29 20:08:40 +0000 | [diff] [blame] | 979 | printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : ""); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 980 | } |
| 981 | check_zones(ino); |
| 982 | if (inode && S_ISDIR(inode->i_mode)) |
| 983 | recursive_check(ino); |
Rob Landley | 3ea66a8 | 2006-04-29 20:08:40 +0000 | [diff] [blame] | 984 | pop_filename(); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 985 | } |
| 986 | |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 987 | #if ENABLE_FEATURE_MINIX2 |
Denis Vlasenko | 73464ce | 2007-01-03 00:45:05 +0000 | [diff] [blame] | 988 | static void check_file2(struct minix2_inode *dir, unsigned offset) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 989 | { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 990 | struct minix2_inode *inode; |
| 991 | int ino; |
| 992 | char *name; |
| 993 | int block; |
| 994 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 995 | block = map_block2(dir, offset / BLOCK_SIZE); |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 996 | read_block(block, check_file_blk); |
| 997 | name = check_file_blk + (offset % BLOCK_SIZE) + 2; |
Rob Landley | 8a62549 | 2006-04-29 20:05:17 +0000 | [diff] [blame] | 998 | ino = *(uint16_t *) (name - 2); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 999 | if (ino > INODES) { |
Rob Landley | fd8409f | 2006-06-13 15:31:39 +0000 | [diff] [blame] | 1000 | printf("%s contains a bad inode number for file '%.*s'. ", |
Rob Landley | 3ea66a8 | 2006-04-29 20:08:40 +0000 | [diff] [blame] | 1001 | current_name, namelen, name); |
Rob Landley | fd8409f | 2006-06-13 15:31:39 +0000 | [diff] [blame] | 1002 | if (ask("Remove", 1)) { |
Rob Landley | 8a62549 | 2006-04-29 20:05:17 +0000 | [diff] [blame] | 1003 | *(uint16_t *) (name - 2) = 0; |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 1004 | write_block(block, check_file_blk); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1005 | } |
| 1006 | ino = 0; |
| 1007 | } |
Rob Landley | 3ea66a8 | 2006-04-29 20:08:40 +0000 | [diff] [blame] | 1008 | push_filename(name); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1009 | inode = get_inode2(ino); |
Rob Landley | 3ea66a8 | 2006-04-29 20:08:40 +0000 | [diff] [blame] | 1010 | pop_filename(); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1011 | if (!offset) { |
Denis Vlasenko | 6dd392a | 2007-01-03 00:41:53 +0000 | [diff] [blame] | 1012 | if (inode && LONE_CHAR(name, '.')) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1013 | return; |
Denis Vlasenko | 6dd392a | 2007-01-03 00:41:53 +0000 | [diff] [blame] | 1014 | printf("%s: bad directory: '.' isn't first\n", current_name); |
| 1015 | errors_uncorrected = 1; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1016 | } |
| 1017 | if (offset == dirsize) { |
Denis Vlasenko | 6dd392a | 2007-01-03 00:41:53 +0000 | [diff] [blame] | 1018 | if (inode && strcmp("..", name) == 0) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1019 | return; |
Denis Vlasenko | 6dd392a | 2007-01-03 00:41:53 +0000 | [diff] [blame] | 1020 | printf("%s: bad directory: '..' isn't second\n", current_name); |
| 1021 | errors_uncorrected = 1; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1022 | } |
| 1023 | if (!inode) |
| 1024 | return; |
Rob Landley | 3ea66a8 | 2006-04-29 20:08:40 +0000 | [diff] [blame] | 1025 | push_filename(name); |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 1026 | if (OPT_list) { |
| 1027 | if (OPT_verbose) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1028 | printf("%6d %07o %3d ", ino, inode->i_mode, inode->i_nlinks); |
Rob Landley | 3ea66a8 | 2006-04-29 20:08:40 +0000 | [diff] [blame] | 1029 | printf("%s%s\n", current_name, S_ISDIR(inode->i_mode) ? ":" : ""); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1030 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1031 | check_zones2(ino); |
| 1032 | if (inode && S_ISDIR(inode->i_mode)) |
| 1033 | recursive_check2(ino); |
Rob Landley | 3ea66a8 | 2006-04-29 20:08:40 +0000 | [diff] [blame] | 1034 | pop_filename(); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1035 | } |
| 1036 | #endif |
| 1037 | |
Denis Vlasenko | 73464ce | 2007-01-03 00:45:05 +0000 | [diff] [blame] | 1038 | static void recursive_check(unsigned ino) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1039 | { |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 1040 | struct minix1_inode *dir; |
Denis Vlasenko | 73464ce | 2007-01-03 00:45:05 +0000 | [diff] [blame] | 1041 | unsigned offset; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1042 | |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 1043 | dir = Inode1 + ino; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1044 | if (!S_ISDIR(dir->i_mode)) |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 1045 | die("internal error"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1046 | if (dir->i_size < 2 * dirsize) { |
Rob Landley | 3ea66a8 | 2006-04-29 20:08:40 +0000 | [diff] [blame] | 1047 | printf("%s: bad directory: size<32", current_name); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1048 | errors_uncorrected = 1; |
| 1049 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1050 | for (offset = 0; offset < dir->i_size; offset += dirsize) |
| 1051 | check_file(dir, offset); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 1054 | #if ENABLE_FEATURE_MINIX2 |
Denis Vlasenko | 73464ce | 2007-01-03 00:45:05 +0000 | [diff] [blame] | 1055 | static void recursive_check2(unsigned ino) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1056 | { |
| 1057 | struct minix2_inode *dir; |
Denis Vlasenko | 73464ce | 2007-01-03 00:45:05 +0000 | [diff] [blame] | 1058 | unsigned offset; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1059 | |
| 1060 | dir = Inode2 + ino; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1061 | if (!S_ISDIR(dir->i_mode)) |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 1062 | die("internal error"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1063 | if (dir->i_size < 2 * dirsize) { |
Rob Landley | 3ea66a8 | 2006-04-29 20:08:40 +0000 | [diff] [blame] | 1064 | printf("%s: bad directory: size<32", current_name); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1065 | errors_uncorrected = 1; |
| 1066 | } |
| 1067 | for (offset = 0; offset < dir->i_size; offset += dirsize) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1068 | check_file2(dir, offset); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1069 | } |
| 1070 | #endif |
| 1071 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 1072 | static int bad_zone(int i) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1073 | { |
Denis Vlasenko | 6dd392a | 2007-01-03 00:41:53 +0000 | [diff] [blame] | 1074 | char buffer[BLOCK_SIZE]; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1075 | |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 1076 | xlseek(dev_fd, BLOCK_SIZE * i, SEEK_SET); |
| 1077 | return (BLOCK_SIZE != full_read(dev_fd, buffer, BLOCK_SIZE)); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 1080 | static void check_counts(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1081 | { |
| 1082 | int i; |
| 1083 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1084 | for (i = 1; i <= INODES; i++) { |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 1085 | if (OPT_warn_mode && Inode1[i].i_mode && !inode_in_use(i)) { |
Denis Vlasenko | 73464ce | 2007-01-03 00:45:05 +0000 | [diff] [blame] | 1086 | printf("Inode %d has non-zero mode. ", i); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1087 | if (ask("Clear", 1)) { |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 1088 | Inode1[i].i_mode = 0; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1089 | changed = 1; |
| 1090 | } |
| 1091 | } |
| 1092 | if (!inode_count[i]) { |
| 1093 | if (!inode_in_use(i)) |
| 1094 | continue; |
Rob Landley | fd8409f | 2006-06-13 15:31:39 +0000 | [diff] [blame] | 1095 | printf("Unused inode %d is marked as 'used' in the bitmap. ", i); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1096 | if (ask("Clear", 1)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1097 | unmark_inode(i); |
| 1098 | continue; |
| 1099 | } |
| 1100 | if (!inode_in_use(i)) { |
Denis Vlasenko | 73464ce | 2007-01-03 00:45:05 +0000 | [diff] [blame] | 1101 | 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] | 1102 | if (ask("Set", 1)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1103 | mark_inode(i); |
| 1104 | } |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 1105 | if (Inode1[i].i_nlinks != inode_count[i]) { |
Denis Vlasenko | 73464ce | 2007-01-03 00:45:05 +0000 | [diff] [blame] | 1106 | printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ", |
Denis Vlasenko | dc485c9 | 2007-02-06 00:36:53 +0000 | [diff] [blame] | 1107 | i, Inode1[i].i_mode, Inode1[i].i_nlinks, |
| 1108 | inode_count[i]); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1109 | if (ask("Set i_nlinks to count", 1)) { |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 1110 | Inode1[i].i_nlinks = inode_count[i]; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1111 | changed = 1; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1112 | } |
| 1113 | } |
| 1114 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1115 | for (i = FIRSTZONE; i < ZONES; i++) { |
Denis Vlasenko | dc485c9 | 2007-02-06 00:36:53 +0000 | [diff] [blame] | 1116 | if ((zone_in_use(i) != 0) == zone_count[i]) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1117 | continue; |
| 1118 | if (!zone_count[i]) { |
| 1119 | if (bad_zone(i)) |
| 1120 | continue; |
Rob Landley | fd8409f | 2006-06-13 15:31:39 +0000 | [diff] [blame] | 1121 | 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] | 1122 | if (ask("Unmark", 1)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1123 | unmark_zone(i); |
| 1124 | continue; |
| 1125 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1126 | printf("Zone %d: %sin use, counted=%d\n", |
| 1127 | i, zone_in_use(i) ? "" : "not ", zone_count[i]); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1128 | } |
| 1129 | } |
| 1130 | |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 1131 | #if ENABLE_FEATURE_MINIX2 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1132 | static void check_counts2(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1133 | { |
| 1134 | int i; |
| 1135 | |
| 1136 | for (i = 1; i <= INODES; i++) { |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 1137 | if (OPT_warn_mode && Inode2[i].i_mode && !inode_in_use(i)) { |
Denis Vlasenko | 73464ce | 2007-01-03 00:45:05 +0000 | [diff] [blame] | 1138 | printf("Inode %d has non-zero mode. ", i); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1139 | if (ask("Clear", 1)) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1140 | Inode2[i].i_mode = 0; |
| 1141 | changed = 1; |
| 1142 | } |
| 1143 | } |
| 1144 | if (!inode_count[i]) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1145 | if (!inode_in_use(i)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1146 | continue; |
Rob Landley | fd8409f | 2006-06-13 15:31:39 +0000 | [diff] [blame] | 1147 | printf("Unused inode %d is marked as 'used' in the bitmap. ", i); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1148 | if (ask("Clear", 1)) |
| 1149 | unmark_inode(i); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1150 | continue; |
| 1151 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1152 | if (!inode_in_use(i)) { |
Denis Vlasenko | 73464ce | 2007-01-03 00:45:05 +0000 | [diff] [blame] | 1153 | 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] | 1154 | if (ask("Set", 1)) |
| 1155 | mark_inode(i); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1156 | } |
| 1157 | if (Inode2[i].i_nlinks != inode_count[i]) { |
Rob Landley | fd8409f | 2006-06-13 15:31:39 +0000 | [diff] [blame] | 1158 | printf("Inode %d (mode=%07o), i_nlinks=%d, counted=%d. ", |
Denis Vlasenko | dc485c9 | 2007-02-06 00:36:53 +0000 | [diff] [blame] | 1159 | i, Inode2[i].i_mode, Inode2[i].i_nlinks, |
| 1160 | inode_count[i]); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1161 | if (ask("Set i_nlinks to count", 1)) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1162 | Inode2[i].i_nlinks = inode_count[i]; |
| 1163 | changed = 1; |
| 1164 | } |
| 1165 | } |
| 1166 | } |
| 1167 | for (i = FIRSTZONE; i < ZONES; i++) { |
Denis Vlasenko | dc485c9 | 2007-02-06 00:36:53 +0000 | [diff] [blame] | 1168 | if ((zone_in_use(i) != 0) == zone_count[i]) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1169 | continue; |
| 1170 | if (!zone_count[i]) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1171 | if (bad_zone(i)) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1172 | continue; |
Rob Landley | fd8409f | 2006-06-13 15:31:39 +0000 | [diff] [blame] | 1173 | 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] | 1174 | if (ask("Unmark", 1)) |
| 1175 | unmark_zone(i); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1176 | continue; |
| 1177 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1178 | printf("Zone %d: %sin use, counted=%d\n", |
| 1179 | i, zone_in_use(i) ? "" : "not ", zone_count[i]); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1180 | } |
| 1181 | } |
| 1182 | #endif |
| 1183 | |
Eric Andersen | e674eb7 | 1999-10-19 20:52:57 +0000 | [diff] [blame] | 1184 | static void check(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1185 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1186 | memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count)); |
| 1187 | memset(zone_count, 0, ZONES * sizeof(*zone_count)); |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 1188 | check_zones(MINIX_ROOT_INO); |
| 1189 | recursive_check(MINIX_ROOT_INO); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1190 | check_counts(); |
| 1191 | } |
| 1192 | |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 1193 | #if ENABLE_FEATURE_MINIX2 |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1194 | static void check2(void) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1195 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1196 | memset(inode_count, 0, (INODES + 1) * sizeof(*inode_count)); |
| 1197 | memset(zone_count, 0, ZONES * sizeof(*zone_count)); |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 1198 | check_zones2(MINIX_ROOT_INO); |
| 1199 | recursive_check2(MINIX_ROOT_INO); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1200 | check_counts2(); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1201 | } |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 1202 | #else |
| 1203 | void check2(void); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1204 | #endif |
| 1205 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 1206 | int fsck_minix_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 1207 | int fsck_minix_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1208 | { |
| 1209 | struct termios tmp; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1210 | int retcode = 0; |
| 1211 | |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 1212 | xfunc_error_retval = 8; |
Denis Vlasenko | 6dd392a | 2007-01-03 00:41:53 +0000 | [diff] [blame] | 1213 | |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 1214 | INIT_G(); |
John Beppu | c1dc5d9 | 2000-02-11 12:43:20 +0000 | [diff] [blame] | 1215 | |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 1216 | opt_complementary = "=1:ar"; /* one argument; -a assumes -r */ |
| 1217 | getopt32(argv, OPTION_STR); |
| 1218 | argv += optind; |
| 1219 | device_name = argv[0]; |
Denis Vlasenko | 6dd392a | 2007-01-03 00:41:53 +0000 | [diff] [blame] | 1220 | |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 1221 | check_mount(); /* trying to check a mounted filesystem? */ |
| 1222 | if (OPT_manual) { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1223 | if (!isatty(0) || !isatty(1)) |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 1224 | die("need terminal for interactive repairs"); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1225 | } |
Denis Vlasenko | e06f61d | 2008-06-25 10:00:58 +0000 | [diff] [blame] | 1226 | xmove_fd(xopen(device_name, OPT_repair ? O_RDWR : O_RDONLY), dev_fd); |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 1227 | |
Denis Vlasenko | 6dd392a | 2007-01-03 00:41:53 +0000 | [diff] [blame] | 1228 | /*sync(); paranoia? */ |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1229 | read_superblock(); |
| 1230 | |
| 1231 | /* |
| 1232 | * Determine whether or not we should continue with the checking. |
| 1233 | * This is based on the status of the filesystem valid and error |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1234 | * flags and whether or not the -f switch was specified on the |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1235 | * command line. |
| 1236 | */ |
Denis Vlasenko | ca525b4 | 2007-06-13 12:27:17 +0000 | [diff] [blame] | 1237 | printf("%s: %s\n", applet_name, bb_banner); |
Denis Vlasenko | 6dd392a | 2007-01-03 00:41:53 +0000 | [diff] [blame] | 1238 | |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 1239 | if (!(Super.s_state & MINIX_ERROR_FS) |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 1240 | && (Super.s_state & MINIX_VALID_FS) && !OPT_force |
Denis Vlasenko | 24cfe8f | 2007-01-03 00:39:15 +0000 | [diff] [blame] | 1241 | ) { |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 1242 | if (OPT_repair) |
Rob Landley | fd8409f | 2006-06-13 15:31:39 +0000 | [diff] [blame] | 1243 | printf("%s is clean, check is skipped\n", device_name); |
Denis Vlasenko | 6dd392a | 2007-01-03 00:41:53 +0000 | [diff] [blame] | 1244 | return 0; |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 1245 | } else if (OPT_force) |
Rob Landley | 8a62549 | 2006-04-29 20:05:17 +0000 | [diff] [blame] | 1246 | printf("Forcing filesystem check on %s\n", device_name); |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 1247 | else if (OPT_repair) |
Rob Landley | 8a62549 | 2006-04-29 20:05:17 +0000 | [diff] [blame] | 1248 | printf("Filesystem on %s is dirty, needs checking\n", |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1249 | device_name); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1250 | |
| 1251 | read_tables(); |
| 1252 | |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 1253 | if (OPT_manual) { |
Denis Vlasenko | a669131 | 2007-06-13 12:28:21 +0000 | [diff] [blame] | 1254 | tcgetattr(0, &sv_termios); |
| 1255 | tmp = sv_termios; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1256 | tmp.c_lflag &= ~(ICANON | ECHO); |
Denis Vlasenko | 202ac50 | 2008-11-05 13:20:58 +0000 | [diff] [blame] | 1257 | tcsetattr_stdin_TCSANOW(&tmp); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1258 | termios_set = 1; |
| 1259 | } |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 1260 | |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1261 | if (version2) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1262 | check_root2(); |
| 1263 | check2(); |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 1264 | } else { |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1265 | check_root(); |
| 1266 | check(); |
| 1267 | } |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 1268 | |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 1269 | if (OPT_verbose) { |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 1270 | int i, free_cnt; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1271 | |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 1272 | for (i = 1, free_cnt = 0; i <= INODES; i++) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1273 | if (!inode_in_use(i)) |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 1274 | free_cnt++; |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 1275 | printf("\n%6u inodes used (%u%%)\n", (INODES - free_cnt), |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 1276 | 100 * (INODES - free_cnt) / INODES); |
| 1277 | for (i = FIRSTZONE, free_cnt = 0; i < ZONES; i++) |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1278 | if (!zone_in_use(i)) |
Eric Andersen | 1ca20a7 | 2001-03-21 07:34:27 +0000 | [diff] [blame] | 1279 | free_cnt++; |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 1280 | printf("%6u zones used (%u%%)\n\n" |
| 1281 | "%6u regular files\n" |
| 1282 | "%6u directories\n" |
| 1283 | "%6u character device files\n" |
| 1284 | "%6u block device files\n" |
| 1285 | "%6u links\n" |
| 1286 | "%6u symbolic links\n" |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1287 | "------\n" |
Denis Vlasenko | aa95959 | 2007-01-03 00:43:19 +0000 | [diff] [blame] | 1288 | "%6u files\n", |
Rob Landley | fd8409f | 2006-06-13 15:31:39 +0000 | [diff] [blame] | 1289 | (ZONES - free_cnt), 100 * (ZONES - free_cnt) / ZONES, |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1290 | regular, directory, chardev, blockdev, |
| 1291 | links - 2 * directory + 1, symlinks, |
| 1292 | total - 2 * directory + 1); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1293 | } |
| 1294 | if (changed) { |
| 1295 | write_tables(); |
Rob Landley | fd8409f | 2006-06-13 15:31:39 +0000 | [diff] [blame] | 1296 | printf("FILE SYSTEM HAS BEEN CHANGED\n"); |
Rob Landley | 8a62549 | 2006-04-29 20:05:17 +0000 | [diff] [blame] | 1297 | sync(); |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 1298 | } else if (OPT_repair) |
Bernhard Reutner-Fischer | e1e5174 | 2008-07-21 13:33:22 +0000 | [diff] [blame] | 1299 | write_superblock(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1300 | |
Denis Vlasenko | 9f153f6 | 2008-03-17 08:32:26 +0000 | [diff] [blame] | 1301 | if (OPT_manual) |
Denis Vlasenko | 202ac50 | 2008-11-05 13:20:58 +0000 | [diff] [blame] | 1302 | tcsetattr_stdin_TCSANOW(&sv_termios); |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1303 | |
| 1304 | if (changed) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1305 | retcode += 3; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1306 | if (errors_uncorrected) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1307 | retcode += 4; |
Eric Andersen | e494fdd | 1999-10-19 20:23:03 +0000 | [diff] [blame] | 1308 | return retcode; |
| 1309 | } |