unix.c revision d37066a9fa14ff3c0e7f4ea95e07204fce95f41a
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 <time.h> 20#ifdef HAVE_SIGNAL_H 21#include <signal.h> 22#endif 23#ifdef HAVE_GETOPT_H 24#include <getopt.h> 25#else 26extern char *optarg; 27extern int optind; 28#endif 29#include <unistd.h> 30#ifdef HAVE_ERRNO_H 31#include <errno.h> 32#endif 33#ifdef HAVE_MNTENT_H 34#include <mntent.h> 35#endif 36#include <sys/ioctl.h> 37#include <malloc.h> 38 39#include "et/com_err.h" 40#include "e2fsck.h" 41#include "problem.h" 42#include "../version.h" 43 44/* Command line options */ 45static int blocksize = 0; 46static int swapfs = 0; 47static int normalize_swapfs = 0; 48static int cflag = 0; /* check disk */ 49static int show_version_only = 0; 50static int verbose = 0; 51 52static int replace_bad_blocks = 0; 53static char *bad_blocks_file = 0; 54 55static int possible_block_sizes[] = { 1024, 2048, 4096, 8192, 0}; 56 57static int root_filesystem = 0; 58static int read_only_root = 0; 59 60e2fsck_t e2fsck_global_ctx; /* Try your very best not to use this! */ 61 62static void usage(e2fsck_t ctx) 63{ 64 fprintf(stderr, 65 _("Usage: %s [-panyrcdfvstFSV] [-b superblock] [-B blocksize]\n" 66 "\t\t[-I inode_buffer_blocks] [-P process_inode_size]\n" 67 "\t\t[-l|-L bad_blocks_file] [-C fd] [-j ext-journal] device\n"), 68 ctx->program_name); 69 70 fprintf(stderr, _("\nEmergency help:\n" 71 " -p Automatic repair (no questions)\n" 72 " -n Make no changes to the filesystem\n" 73 " -y Assume \"yes\" to all questions\n" 74 " -c Check for bad blocks\n" 75 " -f Force checking even if filesystem is marked clean\n")); 76 fprintf(stderr, _("" 77 " -v Be verbose\n" 78 " -b superblock Use alternative superblock\n" 79 " -B blocksize Force blocksize when looking for superblock\n" 80 " -j external-journal Set location of the external journal\n" 81 " -l bad_blocks_file Add to badblocks list\n" 82 " -L bad_blocks_file Set badblocks list\n" 83 )); 84 85 exit(FSCK_USAGE); 86} 87 88static void show_stats(e2fsck_t ctx) 89{ 90 ext2_filsys fs = ctx->fs; 91 int inodes, inodes_used, blocks, blocks_used; 92 int dir_links; 93 int num_files, num_links; 94 int frag_percent; 95 96 dir_links = 2 * ctx->fs_directory_count - 1; 97 num_files = ctx->fs_total_count - dir_links; 98 num_links = ctx->fs_links_count - dir_links; 99 inodes = fs->super->s_inodes_count; 100 inodes_used = (fs->super->s_inodes_count - 101 fs->super->s_free_inodes_count); 102 blocks = fs->super->s_blocks_count; 103 blocks_used = (fs->super->s_blocks_count - 104 fs->super->s_free_blocks_count); 105 106 frag_percent = (10000 * ctx->fs_fragmented) / inodes_used; 107 frag_percent = (frag_percent + 5) / 10; 108 109 if (!verbose) { 110 printf(_("%s: %d/%d files (%0d.%d%% non-contiguous), %d/%d blocks\n"), 111 ctx->device_name, inodes_used, inodes, 112 frag_percent / 10, frag_percent % 10, 113 blocks_used, blocks); 114 return; 115 } 116 /* 117 * This is a bit ugly. But I think there will nearly always be more 118 * than one "thing" to report about, so I won't try writing complex 119 * code to handle one/two/many forms of all words. 120 * Some languages (Italian, at least) never uses the plural form 121 * of foreign words, so in real life this could not be a problem. 122 * md@linux.it - 2000-1-15 123 */ 124#ifdef ENABLE_NLS 125 printf (_("\n%8d inodes used (%d%%)\n"), inodes_used, 126 (inodes_used != 1), 100 * inodes_used / inodes); 127 printf (_("%8d non-contiguous inodes (%0d.%d%%)\n"), 128 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10); 129 printf (_(" # of inodes with ind/dind/tind blocks: %d/%d/%d\n"), 130 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count); 131 printf (_("%8d blocks used (%d%%)\n" 132 "%8d bad blocks\n"), blocks_used, 133 (int) ((long long) 100 * blocks_used / blocks), 134 ctx->fs_badblocks_count); 135 printf(_("%8d large files\n"), ctx->large_files); 136 printf (_("\n%8d regular files\n" 137 "%8d directories\n" 138 "%8d character device files\n" 139 "%8d block device files\n" 140 "%8d fifos\n" 141 "%8d links\n" 142 "%8d symbolic links (%d fast symbolic links)\n" 143 "%8d sockets\n" 144 "--------\n" 145 "%8d files\n"), 146 ctx->fs_regular_count, 147 ctx->fs_directory_count, 148 ctx->fs_chardev_count, 149 ctx->fs_blockdev_count, 150 ctx->fs_fifo_count, 151 ctx->fs_links_count - dir_links, 152 ctx->fs_symlinks_count, 153 ctx->fs_fast_symlinks_count, 154 ctx->fs_sockets_count, 155 ctx->fs_total_count - dir_links); 156#else 157 printf ("\n%8d inode%s used (%d%%)\n", inodes_used, 158 (inodes_used != 1) ? "s" : "", 159 100 * inodes_used / inodes); 160 printf ("%8d non-contiguous inodes (%0d.%d%%)\n", 161 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10); 162 printf (" # of inodes with ind/dind/tind blocks: %d/%d/%d\n", 163 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count); 164 printf ("%8d block%s used (%d%%)\n" 165 "%8d bad block%s\n", blocks_used, 166 (blocks_used != 1) ? "s" : "", 167 100 * blocks_used / blocks, ctx->fs_badblocks_count, 168 ctx->fs_badblocks_count != 1 ? "s" : ""); 169 printf(_("%8d large file%s\n"), ctx->large_files, 170 (ctx->large_files != 1) ? "s" : ""); 171 printf ("\n%8d regular file%s\n" 172 "%8d director%s\n" 173 "%8d character device file%s\n" 174 "%8d block device file%s\n" 175 "%8d fifo%s\n" 176 "%8d link%s\n" 177 "%8d symbolic link%s (%d fast symbolic link%s)\n" 178 "%8d socket%s\n" 179 "--------\n" 180 "%8d file%s\n", 181 ctx->fs_regular_count, 182 (ctx->fs_regular_count != 1) ? "s" : "", 183 ctx->fs_directory_count, 184 (ctx->fs_directory_count != 1) ? "ies" : "y", 185 ctx->fs_chardev_count, 186 (ctx->fs_chardev_count != 1) ? "s" : "", 187 ctx->fs_blockdev_count, 188 (ctx->fs_blockdev_count != 1) ? "s" : "", 189 ctx->fs_fifo_count, 190 (ctx->fs_fifo_count != 1) ? "s" : "", 191 ctx->fs_links_count - dir_links, 192 ((ctx->fs_links_count - dir_links) != 1) ? "s" : "", 193 ctx->fs_symlinks_count, 194 (ctx->fs_symlinks_count != 1) ? "s" : "", 195 ctx->fs_fast_symlinks_count, 196 (ctx->fs_fast_symlinks_count != 1) ? "s" : "", 197 ctx->fs_sockets_count, (ctx->fs_sockets_count != 1) ? "s" : "", 198 ctx->fs_total_count - dir_links, 199 ((ctx->fs_total_count - dir_links) != 1) ? "s" : ""); 200#endif 201} 202 203static void check_mount(e2fsck_t ctx) 204{ 205 errcode_t retval; 206 int mount_flags, cont; 207 208 retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags); 209 if (retval) { 210 com_err("ext2fs_check_if_mount", retval, 211 _("while determining whether %s is mounted."), 212 ctx->filesystem_name); 213 return; 214 } 215 216 /* 217 * If the filesystem isn't mounted, or it's the root filesystem 218 * and it's mounted read-only, then everything's fine. 219 */ 220 if ((!(mount_flags & EXT2_MF_MOUNTED)) || 221 ((mount_flags & EXT2_MF_ISROOT) && 222 (mount_flags & EXT2_MF_READONLY))) 223 return; 224 225 if (ctx->options & E2F_OPT_READONLY) { 226 printf(_("Warning! %s is mounted.\n"), ctx->filesystem_name); 227 return; 228 } 229 230 printf(_("%s is mounted. "), ctx->filesystem_name); 231 if (!isatty(0) || !isatty(1)) 232 fatal_error(ctx, _("Cannot continue, aborting.\n\n")); 233 printf(_("\n\n\007\007\007\007WARNING!!! " 234 "Running e2fsck on a mounted filesystem may cause\n" 235 "SEVERE filesystem damage.\007\007\007\n\n")); 236 cont = ask_yn(_("Do you really want to continue"), -1); 237 if (!cont) { 238 printf (_("check aborted.\n")); 239 exit (0); 240 } 241 return; 242} 243 244/* 245 * This routine checks to see if a filesystem can be skipped; if so, 246 * it will exit with E2FSCK_OK. Under some conditions it will print a 247 * message explaining why a check is being forced. 248 */ 249static void check_if_skip(e2fsck_t ctx) 250{ 251 ext2_filsys fs = ctx->fs; 252 const char *reason = NULL; 253 unsigned int reason_arg = 0; 254 255 if ((ctx->options & E2F_OPT_FORCE) || bad_blocks_file || 256 cflag || swapfs) 257 return; 258 259 if (fs->super->s_state & EXT2_ERROR_FS) 260 reason = _(" contains a file system with errors"); 261 else if ((fs->super->s_state & EXT2_VALID_FS) == 0) 262 reason = _(" was not cleanly unmounted"); 263 else if ((fs->super->s_max_mnt_count > 0) && 264 (fs->super->s_mnt_count >= 265 (unsigned) fs->super->s_max_mnt_count)) { 266 reason = _(" has been mounted %u times without being checked"); 267 reason_arg = fs->super->s_mnt_count; 268 } else if (fs->super->s_checkinterval && 269 time(0) >= (fs->super->s_lastcheck + 270 fs->super->s_checkinterval)) { 271 reason = _(" has gone %u days without being checked"); 272 reason_arg = (time(0) - fs->super->s_lastcheck)/(3600*24); 273 } 274 if (reason) { 275 fputs(ctx->device_name, stdout); 276 printf(reason, reason_arg); 277 fputs(_(", check forced.\n"), stdout); 278 return; 279 } 280 printf(_("%s: clean, %d/%d files, %d/%d blocks\n"), ctx->device_name, 281 fs->super->s_inodes_count - fs->super->s_free_inodes_count, 282 fs->super->s_inodes_count, 283 fs->super->s_blocks_count - fs->super->s_free_blocks_count, 284 fs->super->s_blocks_count); 285 ext2fs_close(fs); 286 ctx->fs = NULL; 287 e2fsck_free_context(ctx); 288 exit(FSCK_OK); 289} 290 291/* 292 * For completion notice 293 */ 294struct percent_tbl { 295 int max_pass; 296 int table[32]; 297}; 298struct percent_tbl e2fsck_tbl = { 299 5, { 0, 70, 90, 92, 95, 100 } 300}; 301static char bar[] = 302 "===============================================================" 303 "==============================================================="; 304static char spaces[] = 305 " " 306 " "; 307 308static float calc_percent(struct percent_tbl *tbl, int pass, int curr, 309 int max) 310{ 311 float percent; 312 313 if (pass <= 0) 314 return 0.0; 315 if (pass > tbl->max_pass || max == 0) 316 return 100.0; 317 percent = ((float) curr) / ((float) max); 318 return ((percent * (tbl->table[pass] - tbl->table[pass-1])) 319 + tbl->table[pass-1]); 320} 321 322extern void e2fsck_clear_progbar(e2fsck_t ctx) 323{ 324 if (!(ctx->flags & E2F_FLAG_PROG_BAR)) 325 return; 326 327 printf("%s\r", spaces + (sizeof(spaces) - 80)); 328 ctx->flags &= ~E2F_FLAG_PROG_BAR; 329} 330 331static int e2fsck_update_progress(e2fsck_t ctx, int pass, 332 unsigned long cur, unsigned long max) 333{ 334 static const char spinner[] = "\\|/-"; 335 char buf[80]; 336 int i; 337 float percent; 338 int tick; 339 struct timeval tv; 340 static int dpywidth = 0; 341 342 if (pass == 0) 343 return 0; 344 345 if (ctx->progress_fd) { 346 sprintf(buf, "%d %lu %lu\n", pass, cur, max); 347 write(ctx->progress_fd, buf, strlen(buf)); 348 } else { 349 if (ctx->flags & E2F_FLAG_PROG_SUPPRESS) 350 return 0; 351 if (dpywidth == 0) { 352 dpywidth = 66 - strlen(ctx->device_name); 353 dpywidth = 8 * (dpywidth / 8); 354 } 355 /* 356 * Calculate the new progress position. If the 357 * percentage hasn't changed, then we skip out right 358 * away. 359 */ 360 percent = calc_percent(&e2fsck_tbl, pass, cur, max); 361 if (ctx->progress_last_percent == (int) 10 * percent) 362 return 0; 363 ctx->progress_last_percent = (int) 10 * percent; 364 365 /* 366 * If we've already updated the spinner once within 367 * the last 1/8th of a second, no point doing it 368 * again. 369 */ 370 gettimeofday(&tv, NULL); 371 tick = (tv.tv_sec << 3) + (tv.tv_usec / (1000000 / 8)); 372 if ((tick == ctx->progress_last_time) && 373 (cur != max) && (cur != 0)) 374 return 0; 375 ctx->progress_last_time = tick; 376 377 /* 378 * Advance the spinner, and note that the progress bar 379 * will be on the screen 380 */ 381 ctx->progress_pos = (ctx->progress_pos+1) & 3; 382 ctx->flags |= E2F_FLAG_PROG_BAR; 383 384 i = ((percent * dpywidth) + 50) / 100; 385 printf("%s: |%s%s", ctx->device_name, 386 bar + (sizeof(bar) - (i+1)), 387 spaces + (sizeof(spaces) - (dpywidth - i + 1))); 388 if (percent == 100.0) 389 fputc('|', stdout); 390 else 391 fputc(spinner[ctx->progress_pos & 3], stdout); 392 printf(" %4.1f%% \r", percent); 393 if (percent == 100.0) 394 e2fsck_clear_progbar(ctx); 395 fflush(stdout); 396 } 397 return 0; 398} 399 400#define PATH_SET "PATH=/sbin" 401 402static void reserve_stdio_fds(void) 403{ 404 int fd; 405 406 while (1) { 407 fd = open("/dev/null", O_RDWR); 408 if (fd > 2) 409 break; 410 if (fd < 0) { 411 fprintf(stderr, _("ERROR: Couldn't open " 412 "/dev/null (%s)\n"), 413 strerror(errno)); 414 break; 415 } 416 } 417 close(fd); 418} 419 420#ifdef HAVE_SIGNAL_H 421static void signal_progress_on(int sig) 422{ 423 e2fsck_t ctx = e2fsck_global_ctx; 424 425 if (!ctx) 426 return; 427 428 ctx->progress = e2fsck_update_progress; 429 ctx->progress_fd = 0; 430} 431 432static void signal_progress_off(int sig) 433{ 434 e2fsck_t ctx = e2fsck_global_ctx; 435 436 if (!ctx) 437 return; 438 439 e2fsck_clear_progbar(ctx); 440 ctx->progress = 0; 441} 442#endif 443 444static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx) 445{ 446 int flush = 0; 447 int c, fd; 448#ifdef MTRACE 449 extern void *mallwatch; 450#endif 451 e2fsck_t ctx; 452 errcode_t retval; 453#ifdef HAVE_SIGNAL_H 454 struct sigaction sa; 455#endif 456 457 retval = e2fsck_allocate_context(&ctx); 458 if (retval) 459 return retval; 460 461 *ret_ctx = ctx; 462 463 setbuf(stdout, NULL); 464 setbuf(stderr, NULL); 465 initialize_ext2_error_table(); 466 467 if (argc && *argv) 468 ctx->program_name = *argv; 469 else 470 ctx->program_name = "e2fsck"; 471 while ((c = getopt (argc, argv, "panyrcC:B:dfvtFVM:b:I:j:P:l:L:N:Ss")) != EOF) 472 switch (c) { 473 case 'C': 474 ctx->progress = e2fsck_update_progress; 475 ctx->progress_fd = atoi(optarg); 476 if (!ctx->progress_fd) 477 break; 478 /* Validate the file descriptor to avoid disasters */ 479 fd = dup(ctx->progress_fd); 480 if (fd < 0) { 481 fprintf(stderr, 482 _("Error validating file descriptor %d: %s\n"), 483 ctx->progress_fd, 484 error_message(errno)); 485 fatal_error(ctx, 486 _("Invalid completion information file descriptor")); 487 } else 488 close(fd); 489 break; 490 case 'p': 491 case 'a': 492 ctx->options |= E2F_OPT_PREEN; 493 ctx->options &= ~(E2F_OPT_YES|E2F_OPT_NO); 494 break; 495 case 'n': 496 ctx->options |= E2F_OPT_NO; 497 ctx->options &= ~(E2F_OPT_YES|E2F_OPT_PREEN); 498 break; 499 case 'y': 500 ctx->options |= E2F_OPT_YES; 501 ctx->options &= ~(E2F_OPT_PREEN|E2F_OPT_NO); 502 break; 503 case 't': 504#ifdef RESOURCE_TRACK 505 if (ctx->options & E2F_OPT_TIME) 506 ctx->options |= E2F_OPT_TIME2; 507 else 508 ctx->options |= E2F_OPT_TIME; 509#else 510 fprintf(stderr, _("The -t option is not " 511 "supported on this version of e2fsck.\n")); 512#endif 513 break; 514 case 'c': 515 cflag++; 516 ctx->options |= E2F_OPT_CHECKBLOCKS; 517 break; 518 case 'r': 519 /* What we do by default, anyway! */ 520 break; 521 case 'b': 522 ctx->use_superblock = atoi(optarg); 523 ctx->flags |= E2F_FLAG_SB_SPECIFIED; 524 break; 525 case 'B': 526 blocksize = atoi(optarg); 527 break; 528 case 'I': 529 ctx->inode_buffer_blocks = atoi(optarg); 530 break; 531 case 'j': 532 ctx->journal_name = optarg; 533 break; 534 case 'P': 535 ctx->process_inode_size = atoi(optarg); 536 break; 537 case 'L': 538 replace_bad_blocks++; 539 case 'l': 540 bad_blocks_file = (char *) malloc(strlen(optarg)+1); 541 if (!bad_blocks_file) 542 fatal_error(ctx, 543 "Couldn't malloc bad_blocks_file"); 544 strcpy(bad_blocks_file, optarg); 545 break; 546 case 'd': 547 ctx->options |= E2F_OPT_DEBUG; 548 break; 549 case 'f': 550 ctx->options |= E2F_OPT_FORCE; 551 break; 552 case 'F': 553 flush = 1; 554 break; 555 case 'v': 556 verbose = 1; 557 break; 558 case 'V': 559 show_version_only = 1; 560 break; 561#ifdef MTRACE 562 case 'M': 563 mallwatch = (void *) strtol(optarg, NULL, 0); 564 break; 565#endif 566 case 'N': 567 ctx->device_name = optarg; 568 break; 569#ifdef ENABLE_SWAPFS 570 case 's': 571 normalize_swapfs = 1; 572 case 'S': 573 swapfs = 1; 574 break; 575#else 576 case 's': 577 case 'S': 578 fprintf(stderr, _("Byte-swapping filesystems " 579 "not compiled in this version " 580 "of e2fsck\n")); 581 exit(1); 582#endif 583 default: 584 usage(ctx); 585 } 586 if (show_version_only) 587 return 0; 588 if (optind != argc - 1) 589 usage(ctx); 590 if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file && 591 !cflag && !swapfs) 592 ctx->options |= E2F_OPT_READONLY; 593 ctx->filesystem_name = argv[optind]; 594 if (flush) { 595 fd = open(ctx->filesystem_name, O_RDONLY, 0); 596 if (fd < 0) { 597 com_err("open", errno, 598 _("while opening %s for flushing"), 599 ctx->filesystem_name); 600 fatal_error(ctx, 0); 601 } 602 if ((retval = ext2fs_sync_device(fd, 1))) { 603 com_err("ext2fs_sync_device", retval, 604 _("while trying to flush %s"), 605 ctx->filesystem_name); 606 fatal_error(ctx, 0); 607 } 608 close(fd); 609 } 610#ifdef ENABLE_SWAPFS 611 if (swapfs) { 612 if (cflag || bad_blocks_file) { 613 fprintf(stderr, _("Incompatible options not " 614 "allowed when byte-swapping.\n")); 615 exit(FSCK_USAGE); 616 } 617 } 618#endif 619#ifdef HAVE_SIGNAL_H 620 /* 621 * Set up signal action 622 */ 623 memset(&sa, 0, sizeof(struct sigaction)); 624#ifdef SA_RESTART 625 sa.sa_flags = SA_RESTART; 626#endif 627 e2fsck_global_ctx = ctx; 628 sa.sa_handler = signal_progress_on; 629 sigaction(SIGUSR1, &sa, 0); 630 sa.sa_handler = signal_progress_off; 631 sigaction(SIGUSR2, &sa, 0); 632#endif 633 634 /* Update our PATH to include /sbin if we need to run badblocks */ 635 if (cflag) { 636 char *oldpath = getenv("PATH"); 637 if (oldpath) { 638 char *newpath; 639 640 newpath = (char *) malloc(sizeof (PATH_SET) + 1 + 641 strlen (oldpath)); 642 if (!newpath) 643 fatal_error(ctx, "Couldn't malloc() newpath"); 644 strcpy (newpath, PATH_SET); 645 strcat (newpath, ":"); 646 strcat (newpath, oldpath); 647 putenv (newpath); 648 } else 649 putenv (PATH_SET); 650 } 651 return 0; 652} 653 654static const char *my_ver_string = E2FSPROGS_VERSION; 655static const char *my_ver_date = E2FSPROGS_DATE; 656 657int main (int argc, char *argv[]) 658{ 659 errcode_t retval = 0; 660 int exit_value = FSCK_OK; 661 int i; 662 ext2_filsys fs = 0; 663 io_manager io_ptr; 664 struct ext2_super_block *sb; 665 const char *lib_ver_date; 666 int my_ver, lib_ver; 667 e2fsck_t ctx; 668 struct problem_context pctx; 669 int flags, run_result; 670 671 clear_problem_context(&pctx); 672#ifdef MTRACE 673 mtrace(); 674#endif 675#ifdef MCHECK 676 mcheck(0); 677#endif 678#ifdef ENABLE_NLS 679 setlocale(LC_MESSAGES, ""); 680 bindtextdomain(NLS_CAT_NAME, LOCALEDIR); 681 textdomain(NLS_CAT_NAME); 682#endif 683 my_ver = ext2fs_parse_version_string(my_ver_string); 684 lib_ver = ext2fs_get_library_version(0, &lib_ver_date); 685 if (my_ver > lib_ver) { 686 fprintf( stderr, _("Error: ext2fs library version " 687 "out of date!\n")); 688 show_version_only++; 689 } 690 691 retval = PRS(argc, argv, &ctx); 692 if (retval) { 693 com_err("e2fsck", retval, 694 _("while trying to initialize program")); 695 exit(FSCK_ERROR); 696 } 697 reserve_stdio_fds(); 698 699#ifdef RESOURCE_TRACK 700 init_resource_track(&ctx->global_rtrack); 701#endif 702 703 if (!(ctx->options & E2F_OPT_PREEN) || show_version_only) 704 fprintf (stderr, "e2fsck %s (%s)\n", my_ver_string, 705 my_ver_date); 706 707 if (show_version_only) { 708 fprintf(stderr, _("\tUsing %s, %s\n"), 709 error_message(EXT2_ET_BASE), lib_ver_date); 710 exit(FSCK_OK); 711 } 712 713 check_mount(ctx); 714 715 if (!(ctx->options & E2F_OPT_PREEN) && 716 !(ctx->options & E2F_OPT_NO) && 717 !(ctx->options & E2F_OPT_YES)) { 718 if (!isatty (0) || !isatty (1)) 719 fatal_error(ctx, 720 _("need terminal for interactive repairs")); 721 } 722 ctx->superblock = ctx->use_superblock; 723restart: 724#if 1 725 io_ptr = unix_io_manager; 726#else 727 io_ptr = test_io_manager; 728 test_io_backing_manager = unix_io_manager; 729#endif 730 flags = 0; 731 if ((ctx->options & E2F_OPT_READONLY) == 0) 732 flags |= EXT2_FLAG_RW; 733 734 if (ctx->superblock && blocksize) { 735 retval = ext2fs_open(ctx->filesystem_name, flags, 736 ctx->superblock, blocksize, io_ptr, &fs); 737 } else if (ctx->superblock) { 738 for (i=0; possible_block_sizes[i]; i++) { 739 retval = ext2fs_open(ctx->filesystem_name, flags, 740 ctx->superblock, 741 possible_block_sizes[i], 742 io_ptr, &fs); 743 if (!retval) 744 break; 745 } 746 } else 747 retval = ext2fs_open(ctx->filesystem_name, flags, 748 0, 0, io_ptr, &fs); 749 if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) && 750 !(ctx->flags & E2F_FLAG_SB_SPECIFIED) && 751 ((retval == EXT2_ET_BAD_MAGIC) || 752 ((retval == 0) && ext2fs_check_desc(fs)))) { 753 if (!fs || (fs->group_desc_count > 1)) { 754 printf(_("%s trying backup blocks...\n"), 755 retval ? _("Couldn't find ext2 superblock,") : 756 _("Group descriptors look bad...")); 757 ctx->superblock = get_backup_sb(fs); 758 if (fs) 759 ext2fs_close(fs); 760 goto restart; 761 } 762 } 763 if (retval) { 764 com_err(ctx->program_name, retval, _("while trying to open %s"), 765 ctx->filesystem_name); 766 if (retval == EXT2_ET_REV_TOO_HIGH) { 767 printf(_("The filesystem revision is apparently " 768 "too high for this version of e2fsck.\n" 769 "(Or the filesystem superblock " 770 "is corrupt)\n\n")); 771 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx); 772 } else if (retval == EXT2_ET_SHORT_READ) 773 printf(_("Could this be a zero-length partition?\n")); 774 else if ((retval == EPERM) || (retval == EACCES)) 775 printf(_("You must have %s access to the " 776 "filesystem or be root\n"), 777 (ctx->options & E2F_OPT_READONLY) ? 778 "r/o" : "r/w"); 779 else if (retval == ENXIO) 780 printf(_("Possibly non-existent or swap device?\n")); 781#ifdef EROFS 782 else if (retval == EROFS) 783 printf(_("Disk write-protected; use the -n option " 784 "to do a read-only\n" 785 "check of the device.\n")); 786#endif 787 else 788 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx); 789 fatal_error(ctx, 0); 790 } 791 ctx->fs = fs; 792 fs->priv_data = ctx; 793 sb = fs->super; 794 if (sb->s_rev_level > E2FSCK_CURRENT_REV) { 795 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH, 796 _("while trying to open %s"), 797 ctx->filesystem_name); 798 get_newer: 799 fatal_error(ctx, _("Get a newer version of e2fsck!")); 800 } 801 802 /* 803 * Set the device name, which is used whenever we print error 804 * or informational messages to the user. 805 */ 806 if (ctx->device_name == 0 && 807 (sb->s_volume_name[0] != 0)) { 808 char *cp = malloc(sizeof(sb->s_volume_name)+1); 809 if (cp) { 810 strncpy(cp, sb->s_volume_name, 811 sizeof(sb->s_volume_name)); 812 cp[sizeof(sb->s_volume_name)] = 0; 813 ctx->device_name = cp; 814 } 815 } 816 if (ctx->device_name == 0) 817 ctx->device_name = ctx->filesystem_name; 818 819 /* 820 * Make sure the ext3 superblock fields are consistent. 821 */ 822 retval = e2fsck_check_ext3_journal(ctx); 823 if (retval) { 824 com_err(ctx->program_name, retval, 825 _("while checking ext3 journal for %s"), 826 ctx->device_name); 827 ext2fs_close(ctx->fs); 828 fatal_error(ctx, 0); 829 } 830 831 /* 832 * Check to see if we need to do ext3-style recovery. If so, 833 * do it, and then restart the fsck. 834 */ 835 if (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) { 836 if (ctx->options & E2F_OPT_READONLY) { 837 printf(_("Warning: skipping journal recovery " 838 "because doing a read-only filesystem " 839 "check.\n")); 840 io_channel_flush(ctx->fs->io); 841 } else { 842 retval = e2fsck_run_ext3_journal(ctx); 843 if (retval) { 844 com_err(ctx->program_name, retval, 845 _("while recovering ext3 journal of %s"), 846 ctx->device_name); 847 fatal_error(ctx, 0); 848 } 849 ext2fs_close(ctx->fs); 850 ctx->fs = 0; 851 goto restart; 852 } 853 } 854 855 /* 856 * Check for compatibility with the feature sets. We need to 857 * be more stringent than ext2fs_open(). 858 */ 859 if ((sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) || 860 (sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) { 861 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE, 862 "(%s)", ctx->device_name); 863 goto get_newer; 864 } 865 if (sb->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) { 866 com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE, 867 "(%s)", ctx->device_name); 868 goto get_newer; 869 } 870#ifdef ENABLE_COMPRESSION 871 if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION) 872 com_err(ctx->program_name, 0, 873 _("Warning: compression support is experimental.\n")); 874#endif 875 876 /* 877 * If the user specified a specific superblock, presumably the 878 * master superblock has been trashed. So we mark the 879 * superblock as dirty, so it can be written out. 880 */ 881 if (ctx->superblock && 882 !(ctx->options & E2F_OPT_READONLY)) 883 ext2fs_mark_super_dirty(fs); 884 885 /* 886 * Don't overwrite the backup superblock and block 887 * descriptors, until we're sure the filesystem is OK.... 888 */ 889 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY; 890 891 ehandler_init(fs->io); 892 893 if (ctx->superblock) 894 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0); 895 check_super_block(ctx); 896 if (ctx->flags & E2F_FLAG_SIGNAL_MASK) 897 fatal_error(ctx, 0); 898 check_if_skip(ctx); 899 if (bad_blocks_file) 900 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks); 901 else if (cflag) 902 test_disk(ctx); 903 if (ctx->flags & E2F_FLAG_SIGNAL_MASK) 904 fatal_error(ctx, 0); 905#ifdef ENABLE_SWAPFS 906 if (normalize_swapfs) { 907 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) == 908 ext2fs_native_flag()) { 909 fprintf(stderr, _("%s: Filesystem byte order " 910 "already normalized.\n"), ctx->device_name); 911 fatal_error(ctx, 0); 912 } 913 } 914 if (swapfs) { 915 swap_filesys(ctx); 916 if (ctx->flags & E2F_FLAG_SIGNAL_MASK) 917 fatal_error(ctx, 0); 918 } 919#endif 920 921 /* 922 * Mark the system as valid, 'til proven otherwise 923 */ 924 ext2fs_mark_valid(fs); 925 926 retval = ext2fs_read_bb_inode(fs, &fs->badblocks); 927 if (retval) { 928 com_err(ctx->program_name, retval, 929 _("while reading bad blocks inode")); 930 preenhalt(ctx); 931 printf(_("This doesn't bode well," 932 " but we'll try to go on...\n")); 933 } 934 935 run_result = e2fsck_run(ctx); 936 e2fsck_clear_progbar(ctx); 937 if (run_result == E2F_FLAG_RESTART) { 938 printf(_("Restarting e2fsck from the beginning...\n")); 939 retval = e2fsck_reset_context(ctx); 940 if (retval) { 941 com_err(ctx->program_name, retval, 942 _("while resetting context")); 943 fatal_error(ctx, 0); 944 } 945 ext2fs_close(fs); 946 goto restart; 947 } 948 if (run_result & E2F_FLAG_SIGNAL_MASK) 949 fatal_error(ctx, 0); 950 if (run_result & E2F_FLAG_CANCEL) 951 ext2fs_unmark_valid(fs); 952 953#ifdef MTRACE 954 mtrace_print("Cleanup"); 955#endif 956 if (ext2fs_test_changed(fs)) { 957 exit_value = FSCK_NONDESTRUCT; 958 if (!(ctx->options & E2F_OPT_PREEN)) 959 printf(_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"), 960 ctx->device_name); 961 if (root_filesystem && !read_only_root) { 962 printf(_("%s: ***** REBOOT LINUX *****\n"), 963 ctx->device_name); 964 exit_value = FSCK_REBOOT; 965 } 966 } 967 if (ext2fs_test_valid(fs)) 968 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; 969 else { 970 printf(_("\n%s: ********** WARNING: Filesystem still has " 971 "errors **********\n\n"), ctx->device_name); 972 exit_value = FSCK_UNCORRECTED; 973 } 974 if (!(ctx->options & E2F_OPT_READONLY)) { 975 if (ext2fs_test_valid(fs)) { 976 if (!(sb->s_state & EXT2_VALID_FS)) 977 exit_value = FSCK_NONDESTRUCT; 978 sb->s_state = EXT2_VALID_FS; 979 } else 980 sb->s_state &= ~EXT2_VALID_FS; 981 sb->s_mnt_count = 0; 982 sb->s_lastcheck = time(NULL); 983 ext2fs_mark_super_dirty(fs); 984 } 985 show_stats(ctx); 986 987 e2fsck_write_bitmaps(ctx); 988 989 ext2fs_close(fs); 990 ctx->fs = NULL; 991 e2fsck_free_context(ctx); 992 993#ifdef RESOURCE_TRACK 994 if (ctx->options & E2F_OPT_TIME) 995 print_resource_track(NULL, &ctx->global_rtrack); 996#endif 997 998 return exit_value; 999} 1000