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