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