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