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