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