Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * unix.c - The unix-specific code for e2fsck |
| 3 | * |
| 4 | * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o. |
| 5 | * |
| 6 | * %Begin-Header% |
| 7 | * This file may be redistributed under the terms of the GNU Public |
| 8 | * License. |
| 9 | * %End-Header% |
| 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #ifdef HAVE_STDLIB_H |
| 14 | #include <stdlib.h> |
| 15 | #endif |
| 16 | #include <string.h> |
| 17 | #include <fcntl.h> |
| 18 | #include <ctype.h> |
| 19 | #include <termios.h> |
| 20 | #include <time.h> |
| 21 | #ifdef HAVE_GETOPT_H |
| 22 | #include <getopt.h> |
| 23 | #endif |
| 24 | #include <unistd.h> |
| 25 | #ifdef HAVE_ERRNO_H |
| 26 | #include <errno.h> |
| 27 | #endif |
| 28 | #ifdef HAVE_MNTENT_H |
| 29 | #include <mntent.h> |
| 30 | #endif |
| 31 | #include <sys/ioctl.h> |
| 32 | #include <malloc.h> |
| 33 | |
| 34 | #include "et/com_err.h" |
| 35 | #include "e2fsck.h" |
| 36 | #include "problem.h" |
| 37 | #include "../version.h" |
| 38 | |
| 39 | extern int isatty(int); |
| 40 | |
| 41 | /* Command line options */ |
| 42 | static int blocksize = 0; |
| 43 | static int swapfs = 0; |
| 44 | static int normalize_swapfs = 0; |
| 45 | static int cflag = 0; /* check disk */ |
| 46 | static int show_version_only = 0; |
| 47 | static int force = 0; |
| 48 | static int verbose = 0; |
| 49 | |
| 50 | static int replace_bad_blocks = 0; |
| 51 | static char *bad_blocks_file = 0; |
| 52 | |
| 53 | static int possible_block_sizes[] = { 1024, 2048, 4096, 8192, 0}; |
| 54 | |
| 55 | static int root_filesystem = 0; |
| 56 | static int read_only_root = 0; |
| 57 | |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 58 | static void usage(e2fsck_t ctx) |
| 59 | { |
| 60 | fprintf(stderr, |
| 61 | "Usage: %s [-panyrcdfvstFSV] [-b superblock] [-B blocksize]\n" |
| 62 | "\t\t[-I inode_buffer_blocks] [-P process_inode_size]\n" |
| 63 | "\t\t[-l|-L bad_blocks_file] device\n", ctx->program_name); |
| 64 | exit(FSCK_USAGE); |
| 65 | } |
| 66 | |
| 67 | static void show_stats(e2fsck_t ctx) |
| 68 | { |
| 69 | ext2_filsys fs = ctx->fs; |
| 70 | int inodes, inodes_used, blocks, blocks_used; |
| 71 | int dir_links; |
| 72 | int num_files, num_links; |
| 73 | int frag_percent; |
| 74 | |
| 75 | dir_links = 2 * ctx->fs_directory_count - 1; |
| 76 | num_files = ctx->fs_total_count - dir_links; |
| 77 | num_links = ctx->fs_links_count - dir_links; |
| 78 | inodes = fs->super->s_inodes_count; |
| 79 | inodes_used = (fs->super->s_inodes_count - |
| 80 | fs->super->s_free_inodes_count); |
| 81 | blocks = fs->super->s_blocks_count; |
| 82 | blocks_used = (fs->super->s_blocks_count - |
| 83 | fs->super->s_free_blocks_count); |
| 84 | |
| 85 | frag_percent = (10000 * ctx->fs_fragmented) / inodes_used; |
| 86 | frag_percent = (frag_percent + 5) / 10; |
| 87 | |
| 88 | if (!verbose) { |
| 89 | printf("%s: %d/%d files (%0d.%d%% non-contiguous), %d/%d blocks\n", |
| 90 | ctx->device_name, inodes_used, inodes, |
| 91 | frag_percent / 10, frag_percent % 10, |
| 92 | blocks_used, blocks); |
| 93 | return; |
| 94 | } |
| 95 | printf ("\n%8d inode%s used (%d%%)\n", inodes_used, |
| 96 | (inodes_used != 1) ? "s" : "", |
| 97 | 100 * inodes_used / inodes); |
| 98 | printf ("%8d non-contiguous inodes (%0d.%d%%)\n", |
| 99 | ctx->fs_fragmented, frag_percent / 10, frag_percent % 10); |
| 100 | printf (" # of inodes with ind/dind/tind blocks: %d/%d/%d\n", |
| 101 | ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count); |
| 102 | printf ("%8d block%s used (%d%%)\n" |
| 103 | "%8d bad block%s\n", blocks_used, |
| 104 | (blocks_used != 1) ? "s" : "", |
| 105 | 100 * blocks_used / blocks, ctx->fs_badblocks_count, |
| 106 | ctx->fs_badblocks_count != 1 ? "s" : ""); |
| 107 | printf ("\n%8d regular file%s\n" |
| 108 | "%8d director%s\n" |
| 109 | "%8d character device file%s\n" |
| 110 | "%8d block device file%s\n" |
| 111 | "%8d fifo%s\n" |
| 112 | "%8d link%s\n" |
| 113 | "%8d symbolic link%s (%d fast symbolic link%s)\n" |
| 114 | "%8d socket%s\n" |
| 115 | "--------\n" |
| 116 | "%8d file%s\n", |
| 117 | ctx->fs_regular_count, |
| 118 | (ctx->fs_regular_count != 1) ? "s" : "", |
| 119 | ctx->fs_directory_count, |
| 120 | (ctx->fs_directory_count != 1) ? "ies" : "y", |
| 121 | ctx->fs_chardev_count, |
| 122 | (ctx->fs_chardev_count != 1) ? "s" : "", |
| 123 | ctx->fs_blockdev_count, |
| 124 | (ctx->fs_blockdev_count != 1) ? "s" : "", |
| 125 | ctx->fs_fifo_count, |
| 126 | (ctx->fs_fifo_count != 1) ? "s" : "", |
| 127 | ctx->fs_links_count - dir_links, |
| 128 | ((ctx->fs_links_count - dir_links) != 1) ? "s" : "", |
| 129 | ctx->fs_symlinks_count, |
| 130 | (ctx->fs_symlinks_count != 1) ? "s" : "", |
| 131 | ctx->fs_fast_symlinks_count, |
| 132 | (ctx->fs_fast_symlinks_count != 1) ? "s" : "", |
| 133 | ctx->fs_sockets_count, (ctx->fs_sockets_count != 1) ? "s" : "", |
| 134 | ctx->fs_total_count - dir_links, |
| 135 | ((ctx->fs_total_count - dir_links) != 1) ? "s" : ""); |
| 136 | } |
| 137 | |
| 138 | static void check_mount(e2fsck_t ctx) |
| 139 | { |
| 140 | errcode_t retval; |
| 141 | int mount_flags, cont, fd; |
| 142 | |
| 143 | retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags); |
| 144 | if (retval) { |
| 145 | com_err("ext2fs_check_if_mount", retval, |
| 146 | "while determining whether %s is mounted.", |
| 147 | ctx->filesystem_name); |
| 148 | return; |
| 149 | } |
| 150 | if (!(mount_flags & EXT2_MF_MOUNTED)) |
| 151 | return; |
| 152 | |
| 153 | #if (defined(__linux__) && defined(HAVE_MNTENT_H)) |
| 154 | /* |
| 155 | * If the root is mounted read-only, then /etc/mtab is |
| 156 | * probably not correct; so we won't issue a warning based on |
| 157 | * it. |
| 158 | */ |
| 159 | fd = open(MOUNTED, O_RDWR); |
| 160 | if (fd < 0) { |
| 161 | if (errno == EROFS) |
| 162 | return; |
| 163 | } else |
| 164 | close(fd); |
| 165 | #endif |
| 166 | |
| 167 | if (ctx->options & E2F_OPT_READONLY) { |
| 168 | printf("Warning! %s is mounted.\n", ctx->device_name); |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | printf("%s is mounted.\n\n", ctx->device_name); |
| 173 | printf("\a\a\a\aWARNING!!! Running e2fsck on a mounted filesystem " |
| 174 | "may cause\nSEVERE filesystem damage.\a\a\a\n\n"); |
| 175 | if (isatty (0) && isatty (1)) |
| 176 | cont = ask_yn("Do you really want to continue", -1); |
| 177 | else |
| 178 | cont = 0; |
| 179 | if (!cont) { |
| 180 | printf ("check aborted.\n"); |
| 181 | exit (0); |
| 182 | } |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | static void sync_disks(NOARGS) |
| 187 | { |
| 188 | sync(); |
| 189 | sync(); |
| 190 | sleep(1); |
| 191 | sync(); |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * This routine checks to see if a filesystem can be skipped; if so, |
| 196 | * it will exit with E2FSCK_OK. Under some conditions it will print a |
| 197 | * message explaining why a check is being forced. |
| 198 | */ |
| 199 | static void check_if_skip(e2fsck_t ctx) |
| 200 | { |
| 201 | ext2_filsys fs = ctx->fs; |
| 202 | const char *reason = NULL; |
| 203 | |
| 204 | if (force || bad_blocks_file || cflag || swapfs) |
| 205 | return; |
| 206 | |
| 207 | if (fs->super->s_state & EXT2_ERROR_FS) |
| 208 | reason = "contains a file system with errors"; |
| 209 | else if (fs->super->s_mnt_count >= |
| 210 | (unsigned) fs->super->s_max_mnt_count) |
| 211 | reason = "has reached maximal mount count"; |
| 212 | else if (fs->super->s_checkinterval && |
| 213 | time(0) >= (fs->super->s_lastcheck + |
| 214 | fs->super->s_checkinterval)) |
| 215 | reason = "has gone too long without being checked"; |
| 216 | else if ((fs->super->s_state & EXT2_VALID_FS) == 0) |
| 217 | reason = "was not cleanly unmounted"; |
| 218 | if (reason) { |
| 219 | printf("%s %s, check forced.\n", ctx->device_name, reason); |
| 220 | return; |
| 221 | } |
| 222 | printf("%s: clean, %d/%d files, %d/%d blocks\n", ctx->device_name, |
| 223 | fs->super->s_inodes_count - fs->super->s_free_inodes_count, |
| 224 | fs->super->s_inodes_count, |
| 225 | fs->super->s_blocks_count - fs->super->s_free_blocks_count, |
| 226 | fs->super->s_blocks_count); |
| 227 | ext2fs_close(fs); |
| 228 | exit(FSCK_OK); |
| 229 | } |
| 230 | |
| 231 | |
| 232 | #define PATH_SET "PATH=/sbin" |
| 233 | |
| 234 | static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx) |
| 235 | { |
| 236 | int flush = 0; |
Theodore Ts'o | 519149f | 1997-10-25 03:49:49 +0000 | [diff] [blame] | 237 | int c; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 238 | #ifdef MTRACE |
| 239 | extern void *mallwatch; |
| 240 | #endif |
| 241 | char *oldpath = getenv("PATH"); |
| 242 | e2fsck_t ctx; |
| 243 | errcode_t retval; |
| 244 | |
| 245 | retval = e2fsck_allocate_context(&ctx); |
| 246 | if (retval) |
| 247 | return retval; |
| 248 | |
| 249 | *ret_ctx = ctx; |
| 250 | |
| 251 | /* Update our PATH to include /sbin */ |
| 252 | if (oldpath) { |
| 253 | char *newpath; |
| 254 | |
| 255 | newpath = malloc(sizeof (PATH_SET) + 1 + strlen (oldpath)); |
| 256 | if (!newpath) |
| 257 | fatal_error("Couldn't malloc() newpath"); |
| 258 | strcpy (newpath, PATH_SET); |
| 259 | strcat (newpath, ":"); |
| 260 | strcat (newpath, oldpath); |
| 261 | putenv (newpath); |
| 262 | } else |
| 263 | putenv (PATH_SET); |
| 264 | |
| 265 | setbuf(stdout, NULL); |
| 266 | setbuf(stderr, NULL); |
| 267 | initialize_ext2_error_table(); |
| 268 | |
| 269 | if (argc && *argv) |
| 270 | ctx->program_name = *argv; |
| 271 | else |
| 272 | ctx->program_name = "e2fsck"; |
| 273 | while ((c = getopt (argc, argv, "panyrcB:dfvtFVM:b:I:P:l:L:N:Ss")) != EOF) |
| 274 | switch (c) { |
| 275 | case 'p': |
| 276 | case 'a': |
| 277 | ctx->options |= E2F_OPT_PREEN; |
| 278 | ctx->options &= ~(E2F_OPT_YES|E2F_OPT_NO); |
| 279 | break; |
| 280 | case 'n': |
| 281 | ctx->options |= E2F_OPT_NO; |
| 282 | ctx->options &= ~(E2F_OPT_YES|E2F_OPT_PREEN); |
| 283 | break; |
| 284 | case 'y': |
| 285 | ctx->options |= E2F_OPT_YES; |
| 286 | ctx->options &= ~(E2F_OPT_PREEN|E2F_OPT_NO); |
| 287 | break; |
| 288 | case 't': |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 289 | #ifdef RESOURCE_TRACK |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 290 | if (ctx->options & E2F_OPT_TIME) |
| 291 | ctx->options |= E2F_OPT_TIME2; |
| 292 | else |
| 293 | ctx->options |= E2F_OPT_TIME; |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 294 | #else |
| 295 | fprintf(stderr, "The -t option is not " |
| 296 | "supported on this version of e2fsck.\n"); |
| 297 | #endif |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 298 | break; |
| 299 | case 'c': |
| 300 | cflag++; |
| 301 | ctx->options |= E2F_OPT_CHECKBLOCKS; |
| 302 | break; |
| 303 | case 'r': |
| 304 | /* What we do by default, anyway! */ |
| 305 | break; |
| 306 | case 'b': |
| 307 | ctx->use_superblock = atoi(optarg); |
| 308 | break; |
| 309 | case 'B': |
| 310 | blocksize = atoi(optarg); |
| 311 | break; |
| 312 | case 'I': |
| 313 | ctx->inode_buffer_blocks = atoi(optarg); |
| 314 | break; |
| 315 | case 'P': |
| 316 | ctx->process_inode_size = atoi(optarg); |
| 317 | break; |
| 318 | case 'L': |
| 319 | replace_bad_blocks++; |
| 320 | case 'l': |
| 321 | bad_blocks_file = malloc(strlen(optarg)+1); |
| 322 | if (!bad_blocks_file) |
| 323 | fatal_error("Couldn't malloc bad_blocks_file"); |
| 324 | strcpy(bad_blocks_file, optarg); |
| 325 | break; |
| 326 | case 'd': |
| 327 | ctx->options |= E2F_OPT_DEBUG; |
| 328 | break; |
| 329 | case 'f': |
| 330 | force = 1; |
| 331 | break; |
| 332 | case 'F': |
| 333 | #ifdef BLKFLSBUF |
| 334 | flush = 1; |
| 335 | #else |
| 336 | fatal_error ("-F not supported"); |
| 337 | #endif |
| 338 | break; |
| 339 | case 'v': |
| 340 | verbose = 1; |
| 341 | break; |
| 342 | case 'V': |
| 343 | show_version_only = 1; |
| 344 | break; |
| 345 | #ifdef MTRACE |
| 346 | case 'M': |
| 347 | mallwatch = (void *) strtol(optarg, NULL, 0); |
| 348 | break; |
| 349 | #endif |
| 350 | case 'N': |
| 351 | ctx->device_name = optarg; |
| 352 | break; |
| 353 | case 's': |
| 354 | normalize_swapfs = 1; |
| 355 | case 'S': |
| 356 | swapfs = 1; |
| 357 | break; |
| 358 | default: |
| 359 | usage(ctx); |
| 360 | } |
| 361 | if (show_version_only) |
| 362 | return 0; |
| 363 | if (optind != argc - 1) |
| 364 | usage(ctx); |
| 365 | if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file && |
| 366 | !cflag && !swapfs) |
| 367 | ctx->options |= E2F_OPT_READONLY; |
| 368 | ctx->filesystem_name = argv[optind]; |
| 369 | if (ctx->device_name == 0) |
| 370 | ctx->device_name = ctx->filesystem_name; |
| 371 | if (flush) { |
| 372 | #ifdef BLKFLSBUF |
| 373 | int fd = open(ctx->filesystem_name, O_RDONLY, 0); |
| 374 | |
| 375 | if (fd < 0) { |
| 376 | com_err("open", errno, "while opening %s for flushing", |
| 377 | ctx->filesystem_name); |
| 378 | exit(FSCK_ERROR); |
| 379 | } |
| 380 | if (ioctl(fd, BLKFLSBUF, 0) < 0) { |
| 381 | com_err("BLKFLSBUF", errno, "while trying to flush %s", |
| 382 | ctx->filesystem_name); |
| 383 | exit(FSCK_ERROR); |
| 384 | } |
| 385 | close(fd); |
| 386 | #else |
| 387 | fatal_error ("BLKFLSBUF not supported"); |
| 388 | #endif /* BLKFLSBUF */ |
| 389 | } |
| 390 | if (swapfs) { |
| 391 | if (cflag || bad_blocks_file) { |
| 392 | fprintf(stderr, "Incompatible options not " |
| 393 | "allowed when byte-swapping.\n"); |
| 394 | fatal_error(0); |
| 395 | } |
| 396 | } |
| 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | static const char *my_ver_string = E2FSPROGS_VERSION; |
| 401 | static const char *my_ver_date = E2FSPROGS_DATE; |
| 402 | |
| 403 | int main (int argc, char *argv[]) |
| 404 | { |
| 405 | errcode_t retval = 0; |
| 406 | int exit_value = FSCK_OK; |
| 407 | int i; |
| 408 | ext2_filsys fs = 0; |
| 409 | io_manager io_ptr; |
| 410 | struct ext2fs_sb *s; |
| 411 | const char *lib_ver_date; |
| 412 | int my_ver, lib_ver; |
| 413 | e2fsck_t ctx; |
| 414 | struct problem_context pctx; |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 415 | int flags, run_result; |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 416 | |
| 417 | clear_problem_context(&pctx); |
| 418 | #ifdef MTRACE |
| 419 | mtrace(); |
| 420 | #endif |
| 421 | #ifdef MCHECK |
| 422 | mcheck(0); |
| 423 | #endif |
| 424 | my_ver = ext2fs_parse_version_string(my_ver_string); |
| 425 | lib_ver = ext2fs_get_library_version(0, &lib_ver_date); |
| 426 | if (my_ver > lib_ver) { |
| 427 | fprintf( stderr, "Error: ext2fs library version " |
| 428 | "out of date!\n"); |
| 429 | show_version_only++; |
| 430 | } |
| 431 | |
| 432 | retval = PRS(argc, argv, &ctx); |
| 433 | if (retval) { |
| 434 | com_err("e2fsck", retval, |
| 435 | "while trying to initialize program"); |
| 436 | exit(1); |
| 437 | } |
| 438 | |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 439 | #ifdef RESOURCE_TRACK |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 440 | init_resource_track(&ctx->global_rtrack); |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 441 | #endif |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 442 | |
| 443 | if (!(ctx->options & E2F_OPT_PREEN) || show_version_only) |
| 444 | fprintf (stderr, "e2fsck %s, %s for EXT2 FS %s, %s\n", |
| 445 | my_ver_string, my_ver_date, EXT2FS_VERSION, |
| 446 | EXT2FS_DATE); |
| 447 | |
| 448 | if (show_version_only) { |
| 449 | fprintf(stderr, "\tUsing %s, %s\n", |
| 450 | error_message(EXT2_ET_BASE), lib_ver_date); |
| 451 | exit(0); |
| 452 | } |
| 453 | |
| 454 | check_mount(ctx); |
| 455 | |
| 456 | if (!(ctx->options & E2F_OPT_PREEN) && |
| 457 | !(ctx->options & E2F_OPT_NO) && |
| 458 | !(ctx->options & E2F_OPT_YES)) { |
| 459 | if (!isatty (0) || !isatty (1)) |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 460 | fatal_error("need terminal for interactive repairs"); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 461 | } |
| 462 | ctx->superblock = ctx->use_superblock; |
| 463 | restart: |
| 464 | #if 1 |
| 465 | io_ptr = unix_io_manager; |
| 466 | #else |
| 467 | io_ptr = test_io_manager; |
| 468 | test_io_backing_manager = unix_io_manager; |
| 469 | #endif |
| 470 | sync_disks(); |
| 471 | flags = (ctx->options & E2F_OPT_READONLY) ? 0 : EXT2_FLAG_RW; |
| 472 | if (ctx->superblock && blocksize) { |
| 473 | retval = ext2fs_open(ctx->filesystem_name, flags, |
| 474 | ctx->superblock, blocksize, io_ptr, &fs); |
| 475 | } else if (ctx->superblock) { |
| 476 | for (i=0; possible_block_sizes[i]; i++) { |
| 477 | retval = ext2fs_open(ctx->filesystem_name, flags, |
| 478 | ctx->superblock, |
| 479 | possible_block_sizes[i], |
| 480 | io_ptr, &fs); |
| 481 | if (!retval) |
| 482 | break; |
| 483 | } |
| 484 | } else |
| 485 | retval = ext2fs_open(ctx->filesystem_name, flags, |
| 486 | 0, 0, io_ptr, &fs); |
| 487 | if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) && |
| 488 | ((retval == EXT2_ET_BAD_MAGIC) || |
| 489 | ((retval == 0) && ext2fs_check_desc(fs)))) { |
| 490 | if (!fs || (fs->group_desc_count > 1)) { |
| 491 | printf("%s trying backup blocks...\n", |
| 492 | retval ? "Couldn't find ext2 superblock," : |
| 493 | "Group descriptors look bad..."); |
| 494 | ctx->superblock = get_backup_sb(fs); |
| 495 | if (fs) |
| 496 | ext2fs_close(fs); |
| 497 | goto restart; |
| 498 | } |
| 499 | } |
| 500 | if (retval) { |
| 501 | com_err(ctx->program_name, retval, "while trying to open %s", |
| 502 | ctx->filesystem_name); |
| 503 | if (retval == EXT2_ET_REV_TOO_HIGH) |
| 504 | printf ("Get a newer version of e2fsck!\n"); |
| 505 | else if (retval == EXT2_ET_SHORT_READ) |
| 506 | printf ("Could this be a zero-length partition?\n"); |
| 507 | else if ((retval == EPERM) || (retval == EACCES)) |
| 508 | printf("You must have %s access to the " |
| 509 | "filesystem or be root\n", |
| 510 | (ctx->options & E2F_OPT_READONLY) ? |
| 511 | "r/o" : "r/w"); |
| 512 | else if (retval == ENXIO) |
| 513 | printf("Possibly non-existent or swap device?\n"); |
Theodore Ts'o | 6822754 | 1997-11-04 04:25:22 +0000 | [diff] [blame^] | 514 | #ifdef EROFS |
| 515 | else if (retval == EROFS) |
| 516 | printf("Disk write-protected; use the -n option" |
| 517 | "to do a read-only\n" |
| 518 | "check of the device.\n"); |
| 519 | |
| 520 | #endif |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 521 | else |
| 522 | fix_problem(ctx, PR_0_SB_CORRUPT, &pctx); |
| 523 | fatal_error(0); |
| 524 | } |
| 525 | ctx->fs = fs; |
| 526 | fs->private = ctx; |
| 527 | #ifdef EXT2_CURRENT_REV |
| 528 | if (fs->super->s_rev_level > E2FSCK_CURRENT_REV) { |
| 529 | com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH, |
| 530 | "while trying to open %s", |
| 531 | ctx->filesystem_name); |
| 532 | goto get_newer; |
| 533 | } |
| 534 | #endif |
| 535 | /* |
| 536 | * Check for compatibility with the feature sets. We need to |
| 537 | * be more stringent than ext2fs_open(). |
| 538 | */ |
| 539 | s = (struct ext2fs_sb *) fs->super; |
| 540 | if ((s->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) || |
| 541 | (s->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) { |
| 542 | com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE, |
| 543 | "(%s)", ctx->filesystem_name); |
| 544 | get_newer: |
| 545 | printf ("Get a newer version of e2fsck!\n"); |
| 546 | fatal_error(0); |
| 547 | } |
| 548 | if (s->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) { |
| 549 | com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE, |
| 550 | "(%s)", ctx->filesystem_name); |
| 551 | goto get_newer; |
| 552 | } |
| 553 | |
| 554 | /* |
| 555 | * If the user specified a specific superblock, presumably the |
| 556 | * master superblock has been trashed. So we mark the |
| 557 | * superblock as dirty, so it can be written out. |
| 558 | */ |
| 559 | if (ctx->superblock && |
| 560 | !(ctx->options & E2F_OPT_READONLY)) |
| 561 | ext2fs_mark_super_dirty(fs); |
| 562 | |
| 563 | /* |
| 564 | * Don't overwrite the backup superblock and block |
| 565 | * descriptors, until we're sure the filesystem is OK.... |
| 566 | */ |
| 567 | fs->flags |= EXT2_FLAG_MASTER_SB_ONLY; |
| 568 | |
| 569 | ehandler_init(fs->io); |
| 570 | |
| 571 | if (ctx->superblock) |
| 572 | set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0); |
| 573 | check_super_block(ctx); |
| 574 | check_if_skip(ctx); |
| 575 | if (bad_blocks_file) |
| 576 | read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks); |
| 577 | else if (cflag) |
| 578 | test_disk(ctx); |
| 579 | |
| 580 | if (normalize_swapfs) { |
| 581 | if ((fs->flags & EXT2_FLAG_SWAP_BYTES) == |
| 582 | ext2fs_native_flag()) { |
| 583 | fprintf(stderr, "%s: Filesystem byte order " |
| 584 | "already normalized.\n", ctx->device_name); |
| 585 | fatal_error(0); |
| 586 | } |
| 587 | } |
| 588 | if (swapfs) |
| 589 | swap_filesys(ctx); |
| 590 | |
| 591 | /* |
| 592 | * Mark the system as valid, 'til proven otherwise |
| 593 | */ |
| 594 | ext2fs_mark_valid(fs); |
| 595 | |
| 596 | retval = ext2fs_read_bb_inode(fs, &fs->badblocks); |
| 597 | if (retval) { |
| 598 | com_err(ctx->program_name, retval, |
| 599 | "while reading bad blocks inode"); |
| 600 | preenhalt(ctx); |
| 601 | printf("This doesn't bode well, but we'll try to go on...\n"); |
| 602 | } |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 603 | |
| 604 | run_result = e2fsck_run(ctx); |
| 605 | if (run_result == E2F_FLAG_RESTART) { |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 606 | ext2fs_close(fs); |
| 607 | printf("Restarting e2fsck from the beginning...\n"); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 608 | retval = e2fsck_reset_context(ctx); |
| 609 | if (retval) { |
| 610 | com_err(ctx->program_name, retval, |
| 611 | "while resetting context"); |
| 612 | exit(1); |
| 613 | } |
| 614 | goto restart; |
| 615 | } |
Theodore Ts'o | 08b2130 | 1997-11-03 19:42:40 +0000 | [diff] [blame] | 616 | if (run_result & E2F_FLAG_ABORT) |
| 617 | exit(FSCK_ERROR); |
| 618 | if (run_result & E2F_FLAG_CANCEL) |
| 619 | ext2fs_unmark_valid(fs); |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 620 | |
| 621 | #ifdef MTRACE |
| 622 | mtrace_print("Cleanup"); |
| 623 | #endif |
| 624 | if (ext2fs_test_changed(fs)) { |
| 625 | exit_value = FSCK_NONDESTRUCT; |
| 626 | if (!(ctx->options & E2F_OPT_PREEN)) |
| 627 | printf("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n", |
| 628 | ctx->device_name); |
| 629 | if (root_filesystem && !read_only_root) { |
| 630 | printf("%s: ***** REBOOT LINUX *****\n", |
| 631 | ctx->device_name); |
| 632 | exit_value = FSCK_REBOOT; |
| 633 | } |
| 634 | } |
| 635 | if (ext2fs_test_valid(fs)) |
| 636 | fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; |
| 637 | else |
| 638 | exit_value = FSCK_UNCORRECTED; |
| 639 | if (!(ctx->options & E2F_OPT_READONLY)) { |
| 640 | if (ext2fs_test_valid(fs)) { |
| 641 | if (!(fs->super->s_state & EXT2_VALID_FS)) |
| 642 | exit_value = FSCK_NONDESTRUCT; |
| 643 | fs->super->s_state = EXT2_VALID_FS; |
| 644 | } else |
| 645 | fs->super->s_state &= ~EXT2_VALID_FS; |
| 646 | fs->super->s_mnt_count = 0; |
| 647 | fs->super->s_lastcheck = time(NULL); |
| 648 | ext2fs_mark_super_dirty(fs); |
| 649 | } |
| 650 | show_stats(ctx); |
| 651 | |
| 652 | write_bitmaps(ctx); |
| 653 | ext2fs_close(fs); |
| 654 | sync_disks(); |
| 655 | |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 656 | #ifdef RESOURCE_TRACK |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 657 | if (ctx->options & E2F_OPT_TIME) |
| 658 | print_resource_track(NULL, &ctx->global_rtrack); |
Theodore Ts'o | 8bf191e | 1997-10-20 01:38:32 +0000 | [diff] [blame] | 659 | #endif |
Theodore Ts'o | 1b6bf17 | 1997-10-03 17:48:10 +0000 | [diff] [blame] | 660 | |
| 661 | e2fsck_free_context(ctx); |
| 662 | |
| 663 | return exit_value; |
| 664 | } |