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