Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * e2fsck.c - a consistency checker for the new extended file system. |
| 3 | * |
| 4 | * Copyright (C) 1993, 1994 Theodore Ts'o. This file may be |
| 5 | * redistributed under the terms of the GNU Public License. |
| 6 | */ |
| 7 | |
| 8 | /* Usage: e2fsck [-dfpnsvy] device |
| 9 | * -d -- debugging this program |
| 10 | * -f -- check the fs even if it is marked valid |
| 11 | * -p -- "preen" the filesystem |
| 12 | * -n -- open the filesystem r/o mode; never try to fix problems |
| 13 | * -v -- verbose (tells how many files) |
| 14 | * -y -- always answer yes to questions |
| 15 | * |
| 16 | * The device may be a block device or a image of one, but this isn't |
| 17 | * enforced (but it's not much fun on a character device :-). |
| 18 | */ |
| 19 | |
| 20 | #include <string.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <ctype.h> |
| 23 | #include <termios.h> |
| 24 | #include <time.h> |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 25 | #ifdef HAVE_GETOPT_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 26 | #include <getopt.h> |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 27 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 28 | #include <unistd.h> |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 29 | #ifdef HAVE_ERRNO_H |
| 30 | #include <errno.h> |
| 31 | #endif |
| 32 | #ifdef HAVE_MNTENT_H |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 33 | #include <mntent.h> |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 34 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 35 | #include <sys/ioctl.h> |
| 36 | #include <malloc.h> |
| 37 | |
| 38 | #include "et/com_err.h" |
| 39 | #include "e2fsck.h" |
| 40 | #include "../version.h" |
| 41 | |
| 42 | extern int isatty(int); |
| 43 | |
| 44 | const char * program_name = "e2fsck"; |
| 45 | const char * device_name = NULL; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 46 | const char * filesystem_name = NULL; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 47 | |
| 48 | /* Command line options */ |
| 49 | int nflag = 0; |
| 50 | int yflag = 0; |
| 51 | int tflag = 0; /* Do timing */ |
| 52 | int cflag = 0; /* check disk */ |
| 53 | int preen = 0; |
| 54 | int rwflag = 1; |
| 55 | int inode_buffer_blocks = 0; |
| 56 | blk_t superblock; |
| 57 | int blocksize = 0; |
| 58 | int verbose = 0; |
| 59 | int list = 0; |
| 60 | int debug = 0; |
| 61 | int force = 0; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 62 | int invalid_bitmaps = 0; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 63 | static int show_version_only = 0; |
| 64 | |
| 65 | static int replace_bad_blocks = 0; |
| 66 | static char *bad_blocks_file = 0; |
| 67 | |
| 68 | static int possible_block_sizes[] = { 1024, 2048, 4096, 8192, 0}; |
| 69 | |
| 70 | struct resource_track global_rtrack; |
| 71 | |
| 72 | static int root_filesystem = 0; |
| 73 | static int read_only_root = 0; |
| 74 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 75 | int *invalid_inode_bitmap; |
| 76 | int *invalid_block_bitmap; |
| 77 | int *invalid_inode_table; |
| 78 | int restart_e2fsck = 0; |
| 79 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 80 | static void usage(NOARGS) |
| 81 | { |
| 82 | fprintf(stderr, |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 83 | "Usage: %s [-panyrcdfvtFV] [-b superblock] [-B blocksize]\n" |
| 84 | "\t\t[-I inode_buffer_blocks] [-P process_inode_size]\n" |
| 85 | "\t\t[-l|-L bad_blocks_file] device\n", program_name); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 86 | exit(FSCK_USAGE); |
| 87 | } |
| 88 | |
| 89 | static void show_stats(ext2_filsys fs) |
| 90 | { |
| 91 | int inodes, inodes_used, blocks, blocks_used; |
| 92 | int dir_links; |
| 93 | int num_files, num_links; |
| 94 | |
| 95 | dir_links = 2 * fs_directory_count - 1; |
| 96 | num_files = fs_total_count - dir_links; |
| 97 | num_links = fs_links_count - dir_links; |
| 98 | inodes = fs->super->s_inodes_count; |
| 99 | inodes_used = (fs->super->s_inodes_count - |
| 100 | fs->super->s_free_inodes_count); |
| 101 | blocks = fs->super->s_blocks_count; |
| 102 | blocks_used = (fs->super->s_blocks_count - |
| 103 | fs->super->s_free_blocks_count); |
| 104 | |
| 105 | if (!verbose) { |
Theodore Ts'o | 74becf3 | 1997-04-26 14:37:06 +0000 | [diff] [blame^] | 106 | printf("%s: %d/%d files (%3.1f%% non-contiguous), %d/%d blocks\n", |
| 107 | device_name, inodes_used, inodes, |
| 108 | 100.00 * fs_fragmented / inodes_used, |
| 109 | blocks_used, blocks); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 110 | return; |
| 111 | } |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 112 | printf ("\n%8d inode%s used (%d%%)\n", inodes_used, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 113 | (inodes_used != 1) ? "s" : "", |
| 114 | 100 * inodes_used / inodes); |
Theodore Ts'o | 74becf3 | 1997-04-26 14:37:06 +0000 | [diff] [blame^] | 115 | printf ("%8d non-contiguous inodes (%3.1f%%)\n", |
| 116 | fs_fragmented, 100.00 * fs_fragmented / inodes_used); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 117 | printf (" # of inodes with ind/dind/tind blocks: %d/%d/%d\n", |
| 118 | fs_ind_count, fs_dind_count, fs_tind_count); |
| 119 | printf ("%8d block%s used (%d%%)\n" |
| 120 | "%8d bad block%s\n", blocks_used, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 121 | (blocks_used != 1) ? "s" : "", |
| 122 | 100 * blocks_used / blocks, fs_badblocks_count, |
| 123 | fs_badblocks_count != 1 ? "s" : ""); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 124 | printf ("\n%8d regular file%s\n" |
| 125 | "%8d director%s\n" |
| 126 | "%8d character device file%s\n" |
| 127 | "%8d block device file%s\n" |
| 128 | "%8d fifo%s\n" |
| 129 | "%8d link%s\n" |
| 130 | "%8d symbolic link%s (%d fast symbolic link%s)\n" |
| 131 | "%8d socket%s\n" |
| 132 | "--------\n" |
| 133 | "%8d file%s\n", |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 134 | fs_regular_count, (fs_regular_count != 1) ? "s" : "", |
| 135 | fs_directory_count, (fs_directory_count != 1) ? "ies" : "y", |
| 136 | fs_chardev_count, (fs_chardev_count != 1) ? "s" : "", |
| 137 | fs_blockdev_count, (fs_blockdev_count != 1) ? "s" : "", |
| 138 | fs_fifo_count, (fs_fifo_count != 1) ? "s" : "", |
| 139 | fs_links_count - dir_links, |
| 140 | ((fs_links_count - dir_links) != 1) ? "s" : "", |
| 141 | fs_symlinks_count, (fs_symlinks_count != 1) ? "s" : "", |
| 142 | fs_fast_symlinks_count, (fs_fast_symlinks_count != 1) ? "s" : "", |
| 143 | fs_sockets_count, (fs_sockets_count != 1) ? "s" : "", |
| 144 | fs_total_count - dir_links, |
| 145 | ((fs_total_count - dir_links) != 1) ? "s" : ""); |
| 146 | } |
| 147 | |
| 148 | static void check_mount(NOARGS) |
| 149 | { |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 150 | errcode_t retval; |
Theodore Ts'o | 297f47a | 1997-04-26 14:25:20 +0000 | [diff] [blame] | 151 | int mount_flags, cont, fd; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 152 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 153 | retval = ext2fs_check_if_mounted(filesystem_name, &mount_flags); |
| 154 | if (retval) { |
| 155 | com_err("ext2fs_check_if_mount", retval, |
| 156 | "while determining whether %s is mounted.", |
| 157 | filesystem_name); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 158 | return; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 159 | } |
| 160 | if (!(mount_flags & EXT2_MF_MOUNTED)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 161 | return; |
Theodore Ts'o | 297f47a | 1997-04-26 14:25:20 +0000 | [diff] [blame] | 162 | |
Theodore Ts'o | 74becf3 | 1997-04-26 14:37:06 +0000 | [diff] [blame^] | 163 | #if (defined(__linux__) && defined(HAVE_MNTENT_H)) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 164 | /* |
| 165 | * If the root is mounted read-only, then /etc/mtab is |
| 166 | * probably not correct; so we won't issue a warning based on |
| 167 | * it. |
| 168 | */ |
Theodore Ts'o | 297f47a | 1997-04-26 14:25:20 +0000 | [diff] [blame] | 169 | fd = open(MOUNTED, O_RDWR); |
| 170 | if (fd < 0) { |
| 171 | if (errno == EROFS) |
| 172 | return; |
| 173 | } else |
| 174 | close(fd); |
| 175 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 176 | |
| 177 | if (!rwflag) { |
| 178 | printf("Warning! %s is mounted.\n", device_name); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | printf ("%s is mounted. ", device_name); |
| 183 | if (isatty (0) && isatty (1)) |
| 184 | cont = ask_yn("Do you really want to continue", -1); |
| 185 | else |
| 186 | cont = 0; |
| 187 | if (!cont) { |
| 188 | printf ("check aborted.\n"); |
| 189 | exit (0); |
| 190 | } |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | static void sync_disks(NOARGS) |
| 195 | { |
| 196 | sync(); |
| 197 | sync(); |
| 198 | sleep(1); |
| 199 | sync(); |
| 200 | } |
| 201 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 202 | #define MIN_CHECK 1 |
| 203 | #define MAX_CHECK 2 |
| 204 | |
| 205 | static const char *corrupt_msg = "\nThe filesystem superblock is corrupt. " |
| 206 | "Try running e2fsck with an alternate\n" |
| 207 | "superblock using the -b option. " |
| 208 | "(8193 is commonly an alternate superblock;\n" |
| 209 | "Hence, 'e2fsck -b 8193 <device>' may recover the filesystem.)\n\n"; |
| 210 | |
| 211 | static void check_super_value(const char *descr, unsigned long value, |
| 212 | int flags, unsigned long min, unsigned long max) |
| 213 | { |
| 214 | if (((flags & MIN_CHECK) && (value < min)) || |
| 215 | ((flags & MAX_CHECK) && (value > max))) { |
| 216 | printf("Corruption found in superblock. (%s = %lu).\n", |
| 217 | descr, value); |
| 218 | printf(corrupt_msg); |
| 219 | fatal_error(0); |
| 220 | } |
| 221 | } |
| 222 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 223 | static void check_super_block(ext2_filsys fs) |
| 224 | { |
| 225 | blk_t first_block, last_block; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 226 | struct ext2_super_block *s = fs->super; |
| 227 | blk_t blocks_per_group = fs->super->s_blocks_per_group; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 228 | int i; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 229 | blk_t should_be; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 230 | errcode_t retval; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 231 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 232 | /* |
| 233 | * Verify the super block constants... |
| 234 | */ |
| 235 | check_super_value("inodes_count", s->s_inodes_count, |
| 236 | MIN_CHECK, 1, 0); |
| 237 | check_super_value("blocks_count", s->s_blocks_count, |
| 238 | MIN_CHECK, 1, 0); |
| 239 | check_super_value("first_data_block", s->s_first_data_block, |
| 240 | MAX_CHECK, 0, s->s_blocks_count); |
| 241 | check_super_value("log_frag_size", s->s_log_frag_size, |
| 242 | MAX_CHECK, 0, 2); |
| 243 | check_super_value("log_block_size", s->s_log_block_size, |
| 244 | MIN_CHECK | MAX_CHECK, s->s_log_frag_size, |
| 245 | 2); |
| 246 | check_super_value("frags_per_group", s->s_frags_per_group, |
| 247 | MIN_CHECK | MAX_CHECK, 1, 8 * EXT2_BLOCK_SIZE(s)); |
| 248 | check_super_value("blocks_per_group", s->s_blocks_per_group, |
| 249 | MIN_CHECK | MAX_CHECK, 1, 8 * EXT2_BLOCK_SIZE(s)); |
| 250 | check_super_value("inodes_per_group", s->s_inodes_per_group, |
| 251 | MIN_CHECK, 1, 0); |
| 252 | check_super_value("r_blocks_count", s->s_r_blocks_count, |
| 253 | MAX_CHECK, 0, s->s_blocks_count); |
| 254 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 255 | retval = ext2fs_get_device_size(filesystem_name, EXT2_BLOCK_SIZE(s), |
| 256 | &should_be); |
| 257 | if (retval) { |
| 258 | com_err("ext2fs_get_device_size", retval, |
| 259 | "while trying to check physical size of filesystem"); |
| 260 | fatal_error(0); |
| 261 | } |
| 262 | if (should_be < s->s_blocks_count) { |
| 263 | printf("The filesystem size (according to the superblock) is %d blocks\n", s->s_blocks_count); |
| 264 | printf("The physical size of the device is %d blocks\n", |
| 265 | should_be); |
| 266 | printf("Either the superblock or the partition table is likely to be corrupt!\n"); |
| 267 | preenhalt(fs); |
| 268 | if (ask("Abort", 1)) |
| 269 | fatal_error(0); |
| 270 | } |
| 271 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 272 | if (s->s_log_block_size != s->s_log_frag_size) { |
| 273 | printf("Superblock block_size = %d, fragsize = %d.\n", |
| 274 | EXT2_BLOCK_SIZE(s), EXT2_FRAG_SIZE(s)); |
| 275 | printf("This version of e2fsck does not support fragment " |
| 276 | "sizes different\n" |
| 277 | "from the block size.\n"); |
| 278 | fatal_error(0); |
| 279 | } |
| 280 | |
| 281 | should_be = s->s_frags_per_group / |
| 282 | (s->s_log_block_size - s->s_log_frag_size + 1); |
| 283 | if (s->s_blocks_per_group != should_be) { |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 284 | printf("Superblock blocks_per_group = %u, should " |
| 285 | "have been %u\n", s->s_blocks_per_group, |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 286 | should_be); |
| 287 | printf(corrupt_msg); |
| 288 | } |
| 289 | |
| 290 | should_be = (s->s_log_block_size == 0) ? 1 : 0; |
| 291 | if (s->s_first_data_block != should_be) { |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 292 | printf("Superblock first_data_block = %u, should " |
| 293 | "have been %u\n", s->s_first_data_block, |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 294 | should_be); |
| 295 | printf(corrupt_msg); |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * Verify the group descriptors.... |
| 300 | */ |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 301 | first_block = fs->super->s_first_data_block; |
| 302 | last_block = first_block + blocks_per_group; |
| 303 | |
| 304 | for (i = 0; i < fs->group_desc_count; i++) { |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 305 | if (i == fs->group_desc_count - 1) |
| 306 | last_block = fs->super->s_blocks_count; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 307 | if ((fs->group_desc[i].bg_block_bitmap < first_block) || |
| 308 | (fs->group_desc[i].bg_block_bitmap >= last_block)) { |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 309 | printf("Block bitmap for group %d is not in group. " |
| 310 | "(block %u)\n", |
| 311 | i, fs->group_desc[i].bg_block_bitmap); |
| 312 | preenhalt(fs); |
| 313 | if (!ask("Relocate", 1)) { |
| 314 | fatal_error("Block bitmap not in group"); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 315 | } |
| 316 | fs->group_desc[i].bg_block_bitmap = 0; |
| 317 | invalid_block_bitmap[i]++; |
| 318 | invalid_bitmaps++; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 319 | } |
| 320 | if ((fs->group_desc[i].bg_inode_bitmap < first_block) || |
| 321 | (fs->group_desc[i].bg_inode_bitmap >= last_block)) { |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 322 | printf("Inode bitmap group %d not in group. " |
| 323 | "(block %u)\n", |
| 324 | i, fs->group_desc[i].bg_inode_bitmap); |
| 325 | preenhalt(fs); |
| 326 | if (!ask("Relocate", 1)) { |
| 327 | fatal_error("Inode bitmap not in group"); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 328 | } |
| 329 | fs->group_desc[i].bg_inode_bitmap = 0; |
| 330 | invalid_inode_bitmap[i]++; |
| 331 | invalid_bitmaps++; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 332 | } |
| 333 | if ((fs->group_desc[i].bg_inode_table < first_block) || |
| 334 | ((fs->group_desc[i].bg_inode_table + |
| 335 | fs->inode_blocks_per_group - 1) >= last_block)) { |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 336 | printf("Inode table for group %d not in group. " |
| 337 | "(block %u)\n", |
| 338 | i, fs->group_desc[i].bg_inode_table); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 339 | printf("WARNING: SEVERE DATA LOSS POSSIBLE.\n"); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 340 | preenhalt(fs); |
| 341 | if (!ask("Relocate", 1)) { |
| 342 | fatal_error("Inode table not in group"); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 343 | } |
| 344 | fs->group_desc[i].bg_inode_table = 0; |
| 345 | invalid_inode_table[i]++; |
| 346 | invalid_bitmaps++; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 347 | } |
| 348 | first_block += fs->super->s_blocks_per_group; |
| 349 | last_block += fs->super->s_blocks_per_group; |
| 350 | } |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | /* |
| 355 | * This routine checks to see if a filesystem can be skipped; if so, |
| 356 | * it will exit with E2FSCK_OK. Under some conditions it will print a |
| 357 | * message explaining why a check is being forced. |
| 358 | */ |
| 359 | static void check_if_skip(ext2_filsys fs) |
| 360 | { |
| 361 | const char *reason = NULL; |
| 362 | |
| 363 | if (force || bad_blocks_file || cflag) |
| 364 | return; |
| 365 | |
| 366 | if (fs->super->s_state & EXT2_ERROR_FS) |
| 367 | reason = "contains a file system with errors"; |
| 368 | else if (fs->super->s_mnt_count >= |
| 369 | (unsigned) fs->super->s_max_mnt_count) |
| 370 | reason = "has reached maximal mount count"; |
| 371 | else if (fs->super->s_checkinterval && |
| 372 | time(0) >= (fs->super->s_lastcheck + |
| 373 | fs->super->s_checkinterval)) |
| 374 | reason = "has gone too long without being checked"; |
| 375 | if (reason) { |
| 376 | printf("%s %s, check forced.\n", device_name, reason); |
| 377 | return; |
| 378 | } |
| 379 | if (fs->super->s_state & EXT2_VALID_FS) { |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 380 | printf("%s: clean, %d/%d files, %d/%d blocks\n", device_name, |
| 381 | fs->super->s_inodes_count - fs->super->s_free_inodes_count, |
| 382 | fs->super->s_inodes_count, |
| 383 | fs->super->s_blocks_count - fs->super->s_free_blocks_count, |
| 384 | fs->super->s_blocks_count); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 385 | exit(FSCK_OK); |
| 386 | } |
| 387 | } |
| 388 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 389 | #define PATH_SET "PATH=/sbin" |
| 390 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 391 | static void PRS(int argc, char *argv[]) |
| 392 | { |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 393 | int flush = 0; |
| 394 | char c; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 395 | #ifdef MTRACE |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 396 | extern void *mallwatch; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 397 | #endif |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 398 | char *oldpath = getenv("PATH"); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 399 | |
| 400 | /* Update our PATH to include /sbin */ |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 401 | if (oldpath) { |
| 402 | char *newpath; |
| 403 | |
| 404 | newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath)); |
| 405 | if (!newpath) |
| 406 | fatal_error("Couldn't malloc() newpath"); |
| 407 | strcpy (newpath, PATH_SET); |
| 408 | strcat (newpath, ":"); |
| 409 | strcat (newpath, oldpath); |
| 410 | putenv (newpath); |
| 411 | } else |
| 412 | putenv (PATH_SET); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 413 | |
| 414 | setbuf(stdout, NULL); |
| 415 | setbuf(stderr, NULL); |
| 416 | initialize_ext2_error_table(); |
| 417 | |
| 418 | if (argc && *argv) |
| 419 | program_name = *argv; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 420 | while ((c = getopt (argc, argv, "panyrcB:dfvtFVM:b:I:P:l:L:N:")) != EOF) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 421 | switch (c) { |
| 422 | case 'p': |
| 423 | case 'a': |
| 424 | preen = 1; |
| 425 | yflag = nflag = 0; |
| 426 | break; |
| 427 | case 'n': |
| 428 | nflag = 1; |
| 429 | preen = yflag = 0; |
| 430 | break; |
| 431 | case 'y': |
| 432 | yflag = 1; |
| 433 | preen = nflag = 0; |
| 434 | break; |
| 435 | case 't': |
| 436 | tflag++; |
| 437 | break; |
| 438 | case 'c': |
| 439 | cflag++; |
| 440 | break; |
| 441 | case 'r': |
| 442 | /* What we do by default, anyway! */ |
| 443 | break; |
| 444 | case 'b': |
| 445 | superblock = atoi(optarg); |
| 446 | break; |
| 447 | case 'B': |
| 448 | blocksize = atoi(optarg); |
| 449 | break; |
| 450 | case 'I': |
| 451 | inode_buffer_blocks = atoi(optarg); |
| 452 | break; |
| 453 | case 'P': |
| 454 | process_inode_size = atoi(optarg); |
| 455 | break; |
| 456 | case 'L': |
| 457 | replace_bad_blocks++; |
| 458 | case 'l': |
| 459 | bad_blocks_file = malloc(strlen(optarg)+1); |
| 460 | if (!bad_blocks_file) |
| 461 | fatal_error("Couldn't malloc bad_blocks_file"); |
| 462 | strcpy(bad_blocks_file, optarg); |
| 463 | break; |
| 464 | case 'd': |
| 465 | debug = 1; |
| 466 | break; |
| 467 | case 'f': |
| 468 | force = 1; |
| 469 | break; |
| 470 | case 'F': |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 471 | #ifdef BLKFLSBUF |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 472 | flush = 1; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 473 | #else |
| 474 | fatal_error ("-F not supported"); |
| 475 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 476 | break; |
| 477 | case 'v': |
| 478 | verbose = 1; |
| 479 | break; |
| 480 | case 'V': |
| 481 | show_version_only = 1; |
| 482 | break; |
| 483 | #ifdef MTRACE |
| 484 | case 'M': |
| 485 | mallwatch = (void *) strtol(optarg, NULL, 0); |
| 486 | break; |
| 487 | #endif |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 488 | case 'N': |
| 489 | device_name = optarg; |
| 490 | break; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 491 | default: |
| 492 | usage (); |
| 493 | } |
| 494 | if (show_version_only) |
| 495 | return; |
| 496 | if (optind != argc - 1) |
| 497 | usage (); |
| 498 | if (nflag && !bad_blocks_file && !cflag) |
| 499 | rwflag = 0; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 500 | filesystem_name = argv[optind]; |
| 501 | if (device_name == 0) |
| 502 | device_name = filesystem_name; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 503 | if (flush) { |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 504 | #ifdef BLKFLSBUF |
| 505 | int fd = open(filesystem_name, O_RDONLY, 0); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 506 | |
| 507 | if (fd < 0) { |
| 508 | com_err("open", errno, "while opening %s for flushing", |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 509 | filesystem_name); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 510 | exit(FSCK_ERROR); |
| 511 | } |
| 512 | if (ioctl(fd, BLKFLSBUF, 0) < 0) { |
| 513 | com_err("BLKFLSBUF", errno, "while trying to flush %s", |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 514 | filesystem_name); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 515 | exit(FSCK_ERROR); |
| 516 | } |
| 517 | close(fd); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 518 | #else |
| 519 | fatal_error ("BLKFLSBUF not supported"); |
| 520 | #endif /* BLKFLSBUF */ |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 521 | } |
| 522 | } |
| 523 | |
| 524 | int main (int argc, char *argv[]) |
| 525 | { |
| 526 | errcode_t retval = 0; |
| 527 | int exit_value = FSCK_OK; |
| 528 | int i; |
| 529 | ext2_filsys fs; |
| 530 | |
| 531 | #ifdef MTRACE |
| 532 | mtrace(); |
| 533 | #endif |
| 534 | #ifdef MCHECK |
| 535 | mcheck(0); |
| 536 | #endif |
| 537 | |
| 538 | init_resource_track(&global_rtrack); |
| 539 | |
| 540 | PRS(argc, argv); |
| 541 | |
| 542 | if (!preen) |
| 543 | fprintf (stderr, "e2fsck %s, %s for EXT2 FS %s, %s\n", |
| 544 | E2FSPROGS_VERSION, E2FSPROGS_DATE, |
| 545 | EXT2FS_VERSION, EXT2FS_DATE); |
| 546 | |
| 547 | if (show_version_only) |
| 548 | exit(0); |
| 549 | |
| 550 | check_mount(); |
| 551 | |
| 552 | if (!preen && !nflag && !yflag) { |
| 553 | if (!isatty (0) || !isatty (1)) |
| 554 | die ("need terminal for interactive repairs"); |
| 555 | } |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 556 | restart: |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 557 | sync_disks(); |
| 558 | if (superblock && blocksize) { |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 559 | retval = ext2fs_open(filesystem_name, |
| 560 | rwflag ? EXT2_FLAG_RW : 0, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 561 | superblock, blocksize, unix_io_manager, |
| 562 | &fs); |
| 563 | } else if (superblock) { |
| 564 | for (i=0; possible_block_sizes[i]; i++) { |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 565 | retval = ext2fs_open(filesystem_name, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 566 | rwflag ? EXT2_FLAG_RW : 0, |
| 567 | superblock, |
| 568 | possible_block_sizes[i], |
| 569 | unix_io_manager, &fs); |
| 570 | if (!retval) |
| 571 | break; |
| 572 | } |
| 573 | } else |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 574 | retval = ext2fs_open(filesystem_name, |
| 575 | rwflag ? EXT2_FLAG_RW : 0, |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 576 | 0, 0, unix_io_manager, &fs); |
| 577 | if (retval) { |
| 578 | com_err(program_name, retval, "while trying to open %s", |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 579 | filesystem_name); |
| 580 | switch (retval) { |
| 581 | case EXT2_ET_REV_TOO_HIGH: |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 582 | printf ("Get a newer version of e2fsck!\n"); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 583 | break; |
| 584 | case EXT2_ET_SHORT_READ: |
| 585 | printf ("Could this be a zero-length partition?\n"); |
| 586 | break; |
| 587 | case EPERM: |
| 588 | case EACCES: |
| 589 | printf("You must have %s access to the " |
| 590 | "filesystem or be root\n", |
| 591 | rwflag ? "r/w" : "r/o"); |
| 592 | break; |
| 593 | case ENXIO: |
| 594 | printf("Possibly non-existent or swap device?\n"); |
| 595 | break; |
| 596 | default: |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 597 | printf(corrupt_msg); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 598 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 599 | fatal_error(0); |
| 600 | } |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 601 | |
| 602 | #ifdef EXT2_CURRENT_REV |
| 603 | if (fs->super->s_rev_level > E2FSCK_CURRENT_REV) { |
| 604 | com_err(program_name, retval, "while trying to open %s", |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 605 | filesystem_name); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 606 | printf ("Get a newer version of e2fsck!\n"); |
| 607 | fatal_error(0); |
| 608 | } |
| 609 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 610 | /* |
| 611 | * If the user specified a specific superblock, presumably the |
| 612 | * master superblock has been trashed. So we mark the |
| 613 | * superblock as dirty, so it can be written out. |
| 614 | */ |
| 615 | if (superblock && rwflag) |
| 616 | ext2fs_mark_super_dirty(fs); |
| 617 | |
| 618 | ehandler_init(fs->io); |
| 619 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 620 | invalid_inode_bitmap = allocate_memory(sizeof(int) * |
| 621 | fs->group_desc_count, |
| 622 | "invalid_inode_bitmap"); |
| 623 | invalid_block_bitmap = allocate_memory(sizeof(int) * |
| 624 | fs->group_desc_count, |
| 625 | "invalid_block_bitmap"); |
| 626 | invalid_inode_table = allocate_memory(sizeof(int) * |
| 627 | fs->group_desc_count, |
| 628 | "invalid_inode_table"); |
| 629 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 630 | check_super_block(fs); |
| 631 | check_if_skip(fs); |
| 632 | if (bad_blocks_file) |
| 633 | read_bad_blocks_file(fs, bad_blocks_file, replace_bad_blocks); |
| 634 | else if (cflag) |
| 635 | test_disk(fs); |
| 636 | |
| 637 | /* |
| 638 | * Mark the system as valid, 'til proven otherwise |
| 639 | */ |
| 640 | ext2fs_mark_valid(fs); |
| 641 | |
| 642 | pass1(fs); |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame] | 643 | free(invalid_inode_bitmap); |
| 644 | free(invalid_block_bitmap); |
| 645 | free(invalid_inode_table); |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 646 | if (restart_e2fsck) { |
| 647 | ext2fs_close(fs); |
| 648 | printf("Restarting e2fsck from the beginning...\n"); |
| 649 | restart_e2fsck = 0; |
| 650 | goto restart; |
| 651 | } |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 652 | pass2(fs); |
| 653 | pass3(fs); |
| 654 | pass4(fs); |
| 655 | pass5(fs); |
| 656 | |
| 657 | #ifdef MTRACE |
| 658 | mtrace_print("Cleanup"); |
| 659 | #endif |
| 660 | if (ext2fs_test_changed(fs)) { |
| 661 | exit_value = FSCK_NONDESTRUCT; |
| 662 | if (!preen) |
| 663 | printf("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n", |
| 664 | device_name); |
| 665 | if (root_filesystem && !read_only_root) { |
| 666 | printf("%s: ***** REBOOT LINUX *****\n", device_name); |
| 667 | exit_value = FSCK_REBOOT; |
| 668 | } |
| 669 | } |
| 670 | if (!ext2fs_test_valid(fs)) |
| 671 | exit_value = FSCK_UNCORRECTED; |
| 672 | if (rwflag) { |
| 673 | if (ext2fs_test_valid(fs)) |
| 674 | fs->super->s_state = EXT2_VALID_FS; |
| 675 | else |
| 676 | fs->super->s_state &= ~EXT2_VALID_FS; |
| 677 | fs->super->s_mnt_count = 0; |
| 678 | fs->super->s_lastcheck = time(NULL); |
| 679 | ext2fs_mark_super_dirty(fs); |
| 680 | } |
| 681 | show_stats(fs); |
| 682 | |
| 683 | write_bitmaps(fs); |
| 684 | ext2fs_close(fs); |
| 685 | sync_disks(); |
| 686 | |
| 687 | if (tflag) |
| 688 | print_resource_track(&global_rtrack); |
| 689 | |
| 690 | return exit_value; |
| 691 | } |