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