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