2 * unix.c - The unix-specific code for e2fsck
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
12 #define _XOPEN_SOURCE 600 /* for inclusion of sa_handler in Solaris */
36 #ifdef HAVE_SYS_IOCTL_H
37 #include <sys/ioctl.h>
42 #ifdef HAVE_SYS_TYPES_H
43 #include <sys/types.h>
51 #include "et/com_err.h"
53 #include "uuid/uuid.h"
54 #include "support/plausible.h"
55 #include "support/devname.h"
59 #include "../version.h"
61 /* Command line options */
62 static int cflag; /* check disk */
63 static int show_version_only;
66 static int replace_bad_blocks;
67 static int keep_bad_blocks;
68 static char *bad_blocks_file;
70 e2fsck_t e2fsck_global_ctx; /* Try your very best not to use this! */
72 #ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jbd-debug */
73 int journal_enable_debug = -1;
76 static void usage(e2fsck_t ctx)
79 _("Usage: %s [-pamnyrcdfktvDFV] [-b superblock] [-B blocksize]\n"
80 "\t\t[-l|-L bad_blocks_file] [-C fd] [-j external_journal]\n"
81 "\t\t[-E extended-options] [-z undo_file] device\n"),
82 ctx->program_name ? ctx->program_name : "e2fsck");
84 fprintf(stderr, "%s", _("\nEmergency help:\n"
85 " -p Automatic repair (no questions)\n"
86 " -m multiple threads to speedup fsck\n"
87 " -n Make no changes to the filesystem\n"
88 " -y Assume \"yes\" to all questions\n"
89 " -c Check for bad blocks and add them to the badblock list\n"
90 " -f Force checking even if filesystem is marked clean\n"));
91 fprintf(stderr, "%s", _(""
93 " -b superblock Use alternative superblock\n"
94 " -B blocksize Force blocksize when looking for superblock\n"
95 " -j external_journal Set location of the external journal\n"
96 " -l bad_blocks_file Add to badblocks list\n"
97 " -L bad_blocks_file Set badblocks list\n"
98 " -z undo_file Create an undo file\n"
104 static void show_stats(e2fsck_t ctx)
106 ext2_filsys fs = ctx->fs;
107 ext2_ino_t inodes, inodes_used;
108 blk64_t blocks, blocks_used;
109 unsigned int dir_links;
110 unsigned int num_files, num_links;
112 int frag_percent_file = 0, frag_percent_dir = 0, frag_percent_total = 0;
113 int i, j, printed = 0;
115 dir_links = 2 * ctx->fs_directory_count - 1;
116 num_files = ctx->fs_total_count - dir_links;
117 num_links = ctx->fs_links_count - dir_links;
118 inodes = fs->super->s_inodes_count;
119 inodes_used = (fs->super->s_inodes_count -
120 fs->super->s_free_inodes_count);
121 blocks = ext2fs_blocks_count(fs->super);
122 blocks_used = (ext2fs_blocks_count(fs->super) -
123 ext2fs_free_blocks_count(fs->super));
125 if (inodes_used > 0) {
126 frag_percent_file = (10000 * ctx->fs_fragmented) / inodes_used;
127 frag_percent_file = (frag_percent_file + 5) / 10;
129 frag_percent_dir = (10000 * ctx->fs_fragmented_dir) / inodes_used;
130 frag_percent_dir = (frag_percent_dir + 5) / 10;
132 frag_percent_total = ((10000 * (ctx->fs_fragmented +
133 ctx->fs_fragmented_dir))
135 frag_percent_total = (frag_percent_total + 5) / 10;
139 log_out(ctx, _("%s: %u/%u files (%0d.%d%% non-contiguous), "
140 "%llu/%llu blocks\n"),
141 ctx->device_name, inodes_used, inodes,
142 frag_percent_total / 10, frag_percent_total % 10,
143 (unsigned long long) blocks_used,
144 (unsigned long long) blocks);
147 profile_get_boolean(ctx->profile, "options", "report_features", 0, 0,
150 log_out(ctx, "\nFilesystem features:");
151 mask = &ctx->fs->super->s_feature_compat;
152 for (i = 0; i < 3; i++, mask++) {
153 for (j = 0, m = 1; j < 32; j++, m <<= 1) {
156 e2p_feature2string(i, m));
162 log_out(ctx, " (none)");
166 log_out(ctx, P_("\n%12u inode used (%2.2f%%, out of %u)\n",
167 "\n%12u inodes used (%2.2f%%, out of %u)\n",
168 inodes_used), inodes_used,
169 100.0 * inodes_used / inodes, inodes);
170 log_out(ctx, P_("%12u non-contiguous file (%0d.%d%%)\n",
171 "%12u non-contiguous files (%0d.%d%%)\n",
173 ctx->fs_fragmented, frag_percent_file / 10,
174 frag_percent_file % 10);
175 log_out(ctx, P_("%12u non-contiguous directory (%0d.%d%%)\n",
176 "%12u non-contiguous directories (%0d.%d%%)\n",
177 ctx->fs_fragmented_dir),
178 ctx->fs_fragmented_dir, frag_percent_dir / 10,
179 frag_percent_dir % 10);
180 log_out(ctx, _(" # of inodes with ind/dind/tind blocks: "
182 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
184 for (j=MAX_EXTENT_DEPTH_COUNT-1; j >=0; j--)
185 if (ctx->extent_depth_count[j])
188 log_out(ctx, "%s", _(" Extent depth histogram: "));
189 for (i=0; i < j; i++) {
192 log_out(ctx, "%u", ctx->extent_depth_count[i]);
197 log_out(ctx, P_("%12llu block used (%2.2f%%, out of %llu)\n",
198 "%12llu blocks used (%2.2f%%, out of %llu)\n",
200 (unsigned long long) blocks_used, 100.0 * blocks_used / blocks,
201 (unsigned long long) blocks);
202 log_out(ctx, P_("%12u bad block\n", "%12u bad blocks\n",
203 ctx->fs_badblocks_count), ctx->fs_badblocks_count);
204 log_out(ctx, P_("%12u large file\n", "%12u large files\n",
205 ctx->large_files), ctx->large_files);
206 log_out(ctx, P_("\n%12u regular file\n", "\n%12u regular files\n",
207 ctx->fs_regular_count), ctx->fs_regular_count);
208 log_out(ctx, P_("%12u directory\n", "%12u directories\n",
209 ctx->fs_directory_count), ctx->fs_directory_count);
210 log_out(ctx, P_("%12u character device file\n",
211 "%12u character device files\n", ctx->fs_chardev_count),
212 ctx->fs_chardev_count);
213 log_out(ctx, P_("%12u block device file\n", "%12u block device files\n",
214 ctx->fs_blockdev_count), ctx->fs_blockdev_count);
215 log_out(ctx, P_("%12u fifo\n", "%12u fifos\n", ctx->fs_fifo_count),
217 log_out(ctx, P_("%12u link\n", "%12u links\n", num_links),
218 ctx->fs_links_count - dir_links);
219 log_out(ctx, P_("%12u symbolic link", "%12u symbolic links",
220 ctx->fs_symlinks_count), ctx->fs_symlinks_count);
221 log_out(ctx, P_(" (%u fast symbolic link)\n",
222 " (%u fast symbolic links)\n",
223 ctx->fs_fast_symlinks_count),
224 ctx->fs_fast_symlinks_count);
225 log_out(ctx, P_("%12u socket\n", "%12u sockets\n",
226 ctx->fs_sockets_count),
227 ctx->fs_sockets_count);
228 log_out(ctx, "------------\n");
229 log_out(ctx, P_("%12u file\n", "%12u files\n", num_files),
233 static void check_mount(e2fsck_t ctx)
238 retval = ext2fs_check_if_mounted(ctx->filesystem_name,
241 com_err("ext2fs_check_if_mount", retval,
242 _("while determining whether %s is mounted."),
243 ctx->filesystem_name);
248 * If the filesystem isn't mounted, or it's the root
249 * filesystem and it's mounted read-only, and we're not doing
250 * a read/write check, then everything's fine.
252 if ((!(ctx->mount_flags & (EXT2_MF_MOUNTED | EXT2_MF_BUSY))) ||
253 ((ctx->mount_flags & EXT2_MF_ISROOT) &&
254 (ctx->mount_flags & EXT2_MF_READONLY) &&
255 !(ctx->options & E2F_OPT_WRITECHECK)))
258 if (((ctx->options & E2F_OPT_READONLY) ||
259 ((ctx->options & E2F_OPT_FORCE) &&
260 (ctx->mount_flags & EXT2_MF_READONLY))) &&
261 !(ctx->options & E2F_OPT_WRITECHECK)) {
262 if (ctx->mount_flags & EXT2_MF_MOUNTED)
263 log_out(ctx, _("Warning! %s is mounted.\n"),
264 ctx->filesystem_name);
266 log_out(ctx, _("Warning! %s is in use.\n"),
267 ctx->filesystem_name);
271 if (ctx->mount_flags & EXT2_MF_MOUNTED)
272 log_out(ctx, _("%s is mounted.\n"), ctx->filesystem_name);
274 log_out(ctx, _("%s is in use.\n"), ctx->filesystem_name);
275 if (!ctx->interactive || ctx->mount_flags & EXT2_MF_BUSY)
276 fatal_error(ctx, _("Cannot continue, aborting.\n\n"));
277 puts("\007\007\007\007");
278 log_out(ctx, "%s", _("\n\nWARNING!!! "
279 "The filesystem is mounted. "
280 "If you continue you ***WILL***\n"
281 "cause ***SEVERE*** filesystem damage.\n\n"));
282 puts("\007\007\007");
283 cont = ask_yn(ctx, _("Do you really want to continue"), 0);
285 printf("%s", _("check aborted.\n"));
291 static int is_on_batt(void)
295 char tmp[80], tmp2[80], fname[NAME_MAX+30];
299 f = fopen("/sys/class/power_supply/AC/online", "r");
301 if (fscanf(f, "%u\n", &acflag) == 1) {
307 f = fopen("/proc/apm", "r");
309 if (fscanf(f, "%79s %79s %79s %x", tmp, tmp, tmp, &acflag) != 4)
312 return (acflag != 1);
314 d = opendir("/proc/acpi/ac_adapter");
316 while ((de=readdir(d)) != NULL) {
317 if (!strncmp(".", de->d_name, 1))
319 snprintf(fname, sizeof(fname),
320 "/proc/acpi/ac_adapter/%s/state",
322 f = fopen(fname, "r");
325 if (fscanf(f, "%79s %79s", tmp2, tmp) != 2)
328 if (strncmp(tmp, "off-line", 8) == 0) {
339 * This routine checks to see if a filesystem can be skipped; if so,
340 * it will exit with E2FSCK_OK. Under some conditions it will print a
341 * message explaining why a check is being forced.
343 static void check_if_skip(e2fsck_t ctx)
345 ext2_filsys fs = ctx->fs;
346 struct problem_context pctx;
347 const char *reason = NULL;
348 unsigned int reason_arg = 0;
350 int batt = is_on_batt();
351 int defer_check_on_battery;
352 int broken_system_clock;
355 if (ctx->flags & E2F_FLAG_PROBLEMS_FIXED)
358 profile_get_boolean(ctx->profile, "options", "broken_system_clock",
359 0, 0, &broken_system_clock);
360 if (ctx->flags & E2F_FLAG_TIME_INSANE)
361 broken_system_clock = 1;
362 profile_get_boolean(ctx->profile, "options",
363 "defer_check_on_battery", 0, 1,
364 &defer_check_on_battery);
365 if (!defer_check_on_battery)
368 if ((ctx->options & E2F_OPT_FORCE) || bad_blocks_file || cflag)
371 if (ctx->options & E2F_OPT_JOURNAL_ONLY)
374 lastcheck = fs->super->s_lastcheck;
375 if (lastcheck > ctx->now)
376 lastcheck -= ctx->time_fudge;
377 if ((fs->super->s_state & EXT2_ERROR_FS) ||
378 !ext2fs_test_valid(fs))
379 reason = _(" contains a file system with errors");
380 else if ((fs->super->s_state & EXT2_VALID_FS) == 0)
381 reason = _(" was not cleanly unmounted");
382 else if (check_backup_super_block(ctx))
383 reason = _(" primary superblock features different from backup");
384 else if ((fs->super->s_max_mnt_count > 0) &&
385 (fs->super->s_mnt_count >=
386 (unsigned) fs->super->s_max_mnt_count)) {
387 reason = _(" has been mounted %u times without being checked");
388 reason_arg = fs->super->s_mnt_count;
389 if (batt && (fs->super->s_mnt_count <
390 (unsigned) fs->super->s_max_mnt_count*2))
392 } else if (!broken_system_clock && fs->super->s_checkinterval &&
393 (ctx->now < lastcheck)) {
394 reason = _(" has filesystem last checked time in the future");
397 } else if (!broken_system_clock && fs->super->s_checkinterval &&
398 ((ctx->now - lastcheck) >=
399 ((time_t) fs->super->s_checkinterval))) {
400 reason = _(" has gone %u days without being checked");
401 reason_arg = (ctx->now - fs->super->s_lastcheck)/(3600*24);
402 if (batt && ((ctx->now - fs->super->s_lastcheck) <
403 fs->super->s_checkinterval*2))
405 } else if (broken_system_clock && fs->super->s_checkinterval) {
406 log_out(ctx, "%s: ", ctx->device_name);
408 _("ignoring check interval, broken_system_clock set\n"));
412 log_out(ctx, "%s", ctx->device_name);
413 log_out(ctx, reason, reason_arg);
414 log_out(ctx, "%s", _(", check forced.\n"));
419 * Update the global counts from the block group counts. This
420 * is needed since modern kernels don't update the global
421 * counts so as to avoid locking the entire file system. So
422 * if the filesystem is not unmounted cleanly, the global
423 * counts may not be accurate. Update them here if we can,
424 * for the benefit of users who might examine the file system
425 * using dumpe2fs. (This is for cosmetic reasons only.)
427 clear_problem_context(&pctx);
428 pctx.ino = fs->super->s_free_inodes_count;
429 pctx.ino2 = ctx->free_inodes;
430 if ((pctx.ino != pctx.ino2) &&
431 !(ctx->options & E2F_OPT_READONLY) &&
432 fix_problem(ctx, PR_0_FREE_INODE_COUNT, &pctx)) {
433 fs->super->s_free_inodes_count = ctx->free_inodes;
434 ext2fs_mark_super_dirty(fs);
436 clear_problem_context(&pctx);
437 pctx.blk = ext2fs_free_blocks_count(fs->super);
438 pctx.blk2 = ctx->free_blocks;
439 if ((pctx.blk != pctx.blk2) &&
440 !(ctx->options & E2F_OPT_READONLY) &&
441 fix_problem(ctx, PR_0_FREE_BLOCK_COUNT, &pctx)) {
442 ext2fs_free_blocks_count_set(fs->super, ctx->free_blocks);
443 ext2fs_mark_super_dirty(fs);
446 /* Print the summary message when we're skipping a full check */
447 log_out(ctx, _("%s: clean, %u/%u files, %llu/%llu blocks"),
449 fs->super->s_inodes_count - fs->super->s_free_inodes_count,
450 fs->super->s_inodes_count,
451 (unsigned long long) ext2fs_blocks_count(fs->super) -
452 ext2fs_free_blocks_count(fs->super),
453 (unsigned long long) ext2fs_blocks_count(fs->super));
455 if (fs->super->s_max_mnt_count > 0) {
456 next_check = fs->super->s_max_mnt_count - fs->super->s_mnt_count;
460 if (!broken_system_clock && fs->super->s_checkinterval &&
461 ((ctx->now - fs->super->s_lastcheck) >= fs->super->s_checkinterval))
463 if (next_check <= 5) {
464 if (next_check == 1) {
467 _(" (check deferred; on battery)"));
470 _(" (check after next mount)"));
472 log_out(ctx, _(" (check in %ld mounts)"),
477 ext2fs_close_free(&ctx->fs);
478 e2fsck_free_context(ctx);
483 * For completion notice
489 static struct percent_tbl e2fsck_tbl = {
490 5, { 0, 70, 90, 92, 95, 100 }
492 static char bar[128], spaces[128];
494 static float calc_percent(struct percent_tbl *tbl, int pass, int curr,
501 if (pass > tbl->max_pass || max == 0)
503 percent = ((float) curr) / ((float) max);
504 return ((percent * (tbl->table[pass] - tbl->table[pass-1]))
505 + tbl->table[pass-1]);
508 void e2fsck_clear_progbar(e2fsck_t ctx)
510 if (!(ctx->flags & E2F_FLAG_PROG_BAR))
513 printf("%s%s\r%s", ctx->start_meta, spaces + (sizeof(spaces) - 80),
516 ctx->flags &= ~E2F_FLAG_PROG_BAR;
519 int e2fsck_simple_progress(e2fsck_t ctx, const char *label, float percent,
522 static const char spinner[] = "\\|/-";
529 if (ctx->flags & E2F_FLAG_PROG_SUPPRESS)
533 * Calculate the new progress position. If the
534 * percentage hasn't changed, then we skip out right
537 fixed_percent = (int) ((10 * percent) + 0.5);
538 if (ctx->progress_last_percent == fixed_percent)
540 ctx->progress_last_percent = fixed_percent;
543 * If we've already updated the spinner once within
544 * the last 1/8th of a second, no point doing it
547 gettimeofday(&tv, NULL);
548 tick = (tv.tv_sec << 3) + (tv.tv_usec / (1000000 / 8));
549 if ((tick == ctx->progress_last_time) &&
550 (fixed_percent != 0) && (fixed_percent != 1000))
552 ctx->progress_last_time = tick;
555 * Advance the spinner, and note that the progress bar
556 * will be on the screen
558 ctx->progress_pos = (ctx->progress_pos+1) & 3;
559 ctx->flags |= E2F_FLAG_PROG_BAR;
561 dpywidth = 66 - strlen(label);
562 dpywidth = 8 * (dpywidth / 8);
566 i = ((percent * dpywidth) + 50) / 100;
567 printf("%s%s: |%s%s", ctx->start_meta, label,
568 bar + (sizeof(bar) - (i+1)),
569 spaces + (sizeof(spaces) - (dpywidth - i + 1)));
570 if (fixed_percent == 1000)
573 fputc(spinner[ctx->progress_pos & 3], stdout);
574 printf(" %4.1f%% ", percent);
576 printf("%u\r", dpynum);
578 fputs(" \r", stdout);
579 fputs(ctx->stop_meta, stdout);
581 if (fixed_percent == 1000)
582 e2fsck_clear_progbar(ctx);
588 static int e2fsck_update_progress(e2fsck_t ctx, int pass,
589 unsigned long cur, unsigned long max)
597 if (ctx->progress_fd) {
598 snprintf(buf, sizeof(buf), "%d %lu %lu %s\n",
599 pass, cur, max, ctx->device_name);
600 write_all(ctx->progress_fd, buf, strlen(buf));
602 percent = calc_percent(&e2fsck_tbl, pass, cur, max);
603 e2fsck_simple_progress(ctx, ctx->device_name,
609 #define PATH_SET "PATH=/sbin"
612 * Make sure 0,1,2 file descriptors are open, so that we don't open
613 * the filesystem using the same file descriptor as stdout or stderr.
615 static void reserve_stdio_fds(void)
620 fd = open("/dev/null", O_RDWR);
622 fprintf(stderr, _("ERROR: Couldn't open "
632 static void signal_progress_on(int sig EXT2FS_ATTR((unused)))
634 e2fsck_t ctx = e2fsck_global_ctx;
639 ctx->progress = e2fsck_update_progress;
642 static void signal_progress_off(int sig EXT2FS_ATTR((unused)))
644 e2fsck_t ctx = e2fsck_global_ctx;
649 e2fsck_clear_progbar(ctx);
653 static void signal_cancel(int sig EXT2FS_ATTR((unused)))
655 e2fsck_t ctx = e2fsck_global_ctx;
660 ctx->flags |= E2F_FLAG_CANCEL;
664 static void parse_extended_opts(e2fsck_t ctx, const char *opts)
666 char *buf, *token, *next, *p, *arg;
668 int extended_usage = 0;
669 unsigned long long reada_kb;
671 buf = string_copy(ctx, opts, 0);
672 for (token = buf; token && *token; token = next) {
673 p = strchr(token, ',');
679 arg = strchr(token, '=');
684 if (strcmp(token, "ea_ver") == 0) {
689 ea_ver = strtoul(arg, &p, 0);
691 ((ea_ver != 1) && (ea_ver != 2))) {
692 fprintf(stderr, "%s",
693 _("Invalid EA version.\n"));
697 ctx->ext_attr_ver = ea_ver;
698 } else if (strcmp(token, "readahead_kb") == 0) {
703 reada_kb = strtoull(arg, &p, 0);
705 fprintf(stderr, "%s",
706 _("Invalid readahead buffer size.\n"));
710 ctx->readahead_kb = reada_kb;
711 } else if (strcmp(token, "fragcheck") == 0) {
712 ctx->options |= E2F_OPT_FRAGCHECK;
714 } else if (strcmp(token, "journal_only") == 0) {
719 ctx->options |= E2F_OPT_JOURNAL_ONLY;
720 } else if (strcmp(token, "discard") == 0) {
721 ctx->options |= E2F_OPT_DISCARD;
723 } else if (strcmp(token, "nodiscard") == 0) {
724 ctx->options &= ~E2F_OPT_DISCARD;
726 } else if (strcmp(token, "optimize_extents") == 0) {
727 ctx->options &= ~E2F_OPT_NOOPT_EXTENTS;
729 } else if (strcmp(token, "no_optimize_extents") == 0) {
730 ctx->options |= E2F_OPT_NOOPT_EXTENTS;
732 } else if (strcmp(token, "inode_count_fullmap") == 0) {
733 ctx->options |= E2F_OPT_ICOUNT_FULLMAP;
735 } else if (strcmp(token, "no_inode_count_fullmap") == 0) {
736 ctx->options &= ~E2F_OPT_ICOUNT_FULLMAP;
738 } else if (strcmp(token, "log_filename") == 0) {
742 ctx->log_fn = string_copy(ctx, arg, 0);
744 } else if (strcmp(token, "problem_log") == 0) {
748 ctx->problem_log_fn = string_copy(ctx, arg, 0);
750 } else if (strcmp(token, "bmap2extent") == 0) {
751 ctx->options |= E2F_OPT_CONVERT_BMAP;
753 } else if (strcmp(token, "fixes_only") == 0) {
754 ctx->options |= E2F_OPT_FIXES_ONLY;
756 } else if (strcmp(token, "unshare_blocks") == 0) {
757 ctx->options |= E2F_OPT_UNSHARE_BLOCKS;
758 ctx->options |= E2F_OPT_FORCE;
760 } else if (strcmp(token, "check_encoding") == 0) {
761 ctx->options |= E2F_OPT_CHECK_ENCODING;
763 #ifdef CONFIG_DEVELOPER_FEATURES
764 } else if (strcmp(token, "clear_all_uninit_bits") == 0) {
765 ctx->options |= E2F_OPT_CLEAR_UNINIT;
769 fprintf(stderr, _("Unknown extended option: %s\n"),
776 if (extended_usage) {
777 fputs(_("\nExtended options are separated by commas, "
778 "and may take an argument which\n"
779 "is set off by an equals ('=') sign. "
780 "Valid extended options are:\n\n"), stderr);
781 fputs(_("\tea_ver=<ea_version (1 or 2)>\n"), stderr);
782 fputs("\tfragcheck\n", stderr);
783 fputs("\tjournal_only\n", stderr);
784 fputs("\tdiscard\n", stderr);
785 fputs("\tnodiscard\n", stderr);
786 fputs("\toptimize_extents\n", stderr);
787 fputs("\tno_optimize_extents\n", stderr);
788 fputs("\tinode_count_fullmap\n", stderr);
789 fputs("\tno_inode_count_fullmap\n", stderr);
790 fputs(_("\treadahead_kb=<buffer size>\n"), stderr);
791 fputs("\tbmap2extent\n", stderr);
792 fputs("\tunshare_blocks\n", stderr);
793 fputs("\tfixes_only\n", stderr);
794 fputs("\tcheck_encoding\n", stderr);
800 static void syntax_err_report(const char *filename, long err, int line_num)
803 _("Syntax error in e2fsck config file (%s, line #%d)\n\t%s\n"),
804 filename, line_num, error_message(err));
808 static const char *config_fn[] = { ROOT_SYSCONFDIR "/e2fsck.conf", 0 };
810 static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
815 extern void *mallwatch;
822 char *extended_opts = 0;
824 int res; /* result of sscanf */
825 #ifdef CONFIG_JBD_DEBUG
828 unsigned long long phys_mem_kb, blk;
830 retval = e2fsck_allocate_context(&ctx);
835 e2fsck_global_ctx = ctx;
837 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
838 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
839 if (getenv("E2FSCK_FORCE_INTERACTIVE") || (isatty(0) && isatty(1))) {
840 ctx->interactive = 1;
842 ctx->start_meta[0] = '\001';
843 ctx->stop_meta[0] = '\002';
845 memset(bar, '=', sizeof(bar)-1);
846 memset(spaces, ' ', sizeof(spaces)-1);
847 add_error_table(&et_ext2_error_table);
848 add_error_table(&et_prof_error_table);
849 blkid_get_cache(&ctx->blkid, NULL);
852 ctx->program_name = *argv;
856 phys_mem_kb = get_memory_size() / 1024;
857 ctx->readahead_kb = ~0ULL;
859 while ((c = getopt(argc, argv, "pamnyrcC:B:dE:fvtFVM:b:I:j:P:l:L:N:SsDkz:")) != EOF)
862 ctx->progress = e2fsck_update_progress;
863 res = sscanf(optarg, "%d", &ctx->progress_fd);
867 if (ctx->progress_fd < 0) {
869 ctx->progress_fd = ctx->progress_fd * -1;
871 if (!ctx->progress_fd)
873 /* Validate the file descriptor to avoid disasters */
874 fd = dup(ctx->progress_fd);
877 _("Error validating file descriptor %d: %s\n"),
879 error_message(errno));
881 _("Invalid completion information file descriptor"));
886 ctx->options |= E2F_OPT_COMPRESS_DIRS;
889 extended_opts = optarg;
893 if (ctx->options & (E2F_OPT_YES|E2F_OPT_NO)) {
896 _("Only one of the options -p/-a, -n or -y may be specified."));
898 ctx->options |= E2F_OPT_PREEN;
901 ctx->options |= E2F_OPT_MULTITHREAD;
904 if (ctx->options & (E2F_OPT_YES|E2F_OPT_PREEN))
906 ctx->options |= E2F_OPT_NO;
909 if (ctx->options & (E2F_OPT_PREEN|E2F_OPT_NO))
911 ctx->options |= E2F_OPT_YES;
914 #ifdef RESOURCE_TRACK
915 if (ctx->options & E2F_OPT_TIME)
916 ctx->options |= E2F_OPT_TIME2;
918 ctx->options |= E2F_OPT_TIME;
920 fprintf(stderr, _("The -t option is not "
921 "supported on this version of e2fsck.\n"));
926 ctx->options |= E2F_OPT_WRITECHECK;
927 ctx->options |= E2F_OPT_CHECKBLOCKS;
930 /* What we do by default, anyway! */
933 res = sscanf(optarg, "%llu", &blk);
934 ctx->use_superblock = blk;
937 ctx->flags |= E2F_FLAG_SB_SPECIFIED;
940 ctx->blocksize = atoi(optarg);
943 res = sscanf(optarg, "%d", &ctx->inode_buffer_blocks);
948 ctx->journal_name = get_devname(ctx->blkid,
950 if (!ctx->journal_name) {
951 com_err(ctx->program_name, 0,
952 _("Unable to resolve '%s'"),
958 res = sscanf(optarg, "%d", &ctx->process_inode_size);
963 replace_bad_blocks++;
967 free(bad_blocks_file);
968 bad_blocks_file = string_copy(ctx, optarg, 0);
971 ctx->options |= E2F_OPT_DEBUG;
974 ctx->options |= E2F_OPT_FORCE;
983 show_version_only = 1;
987 mallwatch = (void *) strtol(optarg, NULL, 0);
991 ctx->device_name = string_copy(ctx, optarg, 0);
997 ctx->undo_file = optarg;
1002 if (show_version_only)
1004 if (optind != argc - 1)
1006 if ((ctx->options & E2F_OPT_NO) &&
1007 (ctx->options & E2F_OPT_COMPRESS_DIRS)) {
1008 com_err(ctx->program_name, 0, "%s",
1009 _("The -n and -D options are incompatible."));
1010 fatal_error(ctx, 0);
1012 if ((ctx->options & E2F_OPT_NO) && cflag) {
1013 com_err(ctx->program_name, 0, "%s",
1014 _("The -n and -c options are incompatible."));
1015 fatal_error(ctx, 0);
1017 if ((ctx->options & E2F_OPT_NO) && bad_blocks_file) {
1018 com_err(ctx->program_name, 0, "%s",
1019 _("The -n and -l/-L options are incompatible."));
1020 fatal_error(ctx, 0);
1022 if (ctx->options & E2F_OPT_MULTITHREAD) {
1023 if ((ctx->options & (E2F_OPT_YES|E2F_OPT_NO|E2F_OPT_PREEN)) == 0) {
1024 com_err(ctx->program_name, 0, "%s",
1025 _("The -m option should be used together with one of -p/-y/-n options."));
1026 fatal_error(ctx, 0);
1028 if (ctx->progress) {
1029 com_err(ctx->program_name, 0, "%s",
1030 _("Only one of the options -C or -m may be specified."));
1031 fatal_error(ctx, 0);
1034 if (ctx->options & E2F_OPT_NO)
1035 ctx->options |= E2F_OPT_READONLY;
1037 ctx->io_options = strchr(argv[optind], '?');
1038 if (ctx->io_options)
1039 *ctx->io_options++ = 0;
1040 ctx->filesystem_name = get_devname(ctx->blkid, argv[optind], 0);
1041 if (!ctx->filesystem_name) {
1042 com_err(ctx->program_name, 0, _("Unable to resolve '%s'"),
1044 fatal_error(ctx, 0);
1047 parse_extended_opts(ctx, extended_opts);
1049 /* Complain about mutually exclusive rebuilding activities */
1050 if (getenv("E2FSCK_FIXES_ONLY"))
1051 ctx->options |= E2F_OPT_FIXES_ONLY;
1052 if ((ctx->options & E2F_OPT_COMPRESS_DIRS) &&
1053 (ctx->options & E2F_OPT_FIXES_ONLY)) {
1054 com_err(ctx->program_name, 0, "%s",
1055 _("The -D and -E fixes_only options are incompatible."));
1056 fatal_error(ctx, 0);
1058 if ((ctx->options & E2F_OPT_CONVERT_BMAP) &&
1059 (ctx->options & E2F_OPT_FIXES_ONLY)) {
1060 com_err(ctx->program_name, 0, "%s",
1061 _("The -E bmap2extent and fixes_only options are incompatible."));
1062 fatal_error(ctx, 0);
1065 if ((cp = getenv("E2FSCK_CONFIG")) != NULL)
1067 profile_set_syntax_err_cb(syntax_err_report);
1068 profile_init(config_fn, &ctx->profile);
1070 profile_get_boolean(ctx->profile, "options", "report_time", 0, 0,
1073 ctx->options |= E2F_OPT_TIME | E2F_OPT_TIME2;
1074 profile_get_boolean(ctx->profile, "options", "report_verbose", 0, 0,
1079 profile_get_boolean(ctx->profile, "options", "no_optimize_extents",
1082 ctx->options |= E2F_OPT_NOOPT_EXTENTS;
1084 profile_get_boolean(ctx->profile, "options", "inode_count_fullmap",
1087 ctx->options |= E2F_OPT_ICOUNT_FULLMAP;
1089 if (ctx->readahead_kb == ~0ULL) {
1090 profile_get_integer(ctx->profile, "options",
1091 "readahead_mem_pct", 0, -1, &c);
1092 if (c >= 0 && c <= 100)
1093 ctx->readahead_kb = phys_mem_kb * c / 100;
1094 profile_get_integer(ctx->profile, "options",
1095 "readahead_kb", 0, -1, &c);
1097 ctx->readahead_kb = c;
1098 if (ctx->readahead_kb != ~0ULL &&
1099 ctx->readahead_kb > phys_mem_kb)
1100 ctx->readahead_kb = phys_mem_kb;
1103 /* Turn off discard in read-only mode */
1104 if ((ctx->options & E2F_OPT_NO) &&
1105 (ctx->options & E2F_OPT_DISCARD))
1106 ctx->options &= ~E2F_OPT_DISCARD;
1109 fd = open(ctx->filesystem_name, O_RDONLY, 0);
1111 com_err("open", errno,
1112 _("while opening %s for flushing"),
1113 ctx->filesystem_name);
1114 fatal_error(ctx, 0);
1116 if ((retval = ext2fs_sync_device(fd, 1))) {
1117 com_err("ext2fs_sync_device", retval,
1118 _("while trying to flush %s"),
1119 ctx->filesystem_name);
1120 fatal_error(ctx, 0);
1124 if (cflag && bad_blocks_file) {
1125 fprintf(stderr, "%s", _("The -c and the -l/-L options may not "
1126 "be both used at the same time.\n"));
1129 #ifdef HAVE_SIGNAL_H
1131 * Set up signal action
1133 memset(&sa, 0, sizeof(struct sigaction));
1134 sa.sa_handler = signal_cancel;
1135 sigaction(SIGINT, &sa, 0);
1136 sigaction(SIGTERM, &sa, 0);
1138 sa.sa_flags = SA_RESTART;
1140 if ((ctx->options & E2F_OPT_MULTITHREAD) == 0) {
1141 sa.sa_handler = signal_progress_on;
1142 sigaction(SIGUSR1, &sa, 0);
1143 sa.sa_handler = signal_progress_off;
1144 sigaction(SIGUSR2, &sa, 0);
1148 /* Update our PATH to include /sbin if we need to run badblocks */
1150 char *oldpath = getenv("PATH");
1152 int len = sizeof(PATH_SET) + 1;
1155 len += strlen(oldpath);
1157 newpath = malloc(len);
1159 fatal_error(ctx, "Couldn't malloc() newpath");
1160 strcpy(newpath, PATH_SET);
1163 strcat(newpath, ":");
1164 strcat(newpath, oldpath);
1168 #ifdef CONFIG_JBD_DEBUG
1169 jbd_debug = getenv("E2FSCK_JBD_DEBUG");
1171 res = sscanf(jbd_debug, "%d", &journal_enable_debug);
1174 _("E2FSCK_JBD_DEBUG \"%s\" not an integer\n\n"),
1183 fprintf(stderr, _("\nInvalid non-numeric argument to -%c (\"%s\")\n\n"),
1188 static errcode_t try_open_fs(e2fsck_t ctx, int flags, io_manager io_ptr,
1189 ext2_filsys *ret_fs)
1195 if (ctx->superblock) {
1196 unsigned long blocksize = ctx->blocksize;
1199 for (blocksize = EXT2_MIN_BLOCK_SIZE;
1200 blocksize <= EXT2_MAX_BLOCK_SIZE; blocksize *= 2) {
1202 retval = ext2fs_open2(ctx->filesystem_name,
1203 ctx->io_options, flags,
1204 ctx->superblock, blocksize,
1205 unix_io_manager, ret_fs);
1207 ext2fs_free(*ret_fs);
1217 retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options,
1218 flags, ctx->superblock, blocksize,
1221 retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options,
1222 flags, 0, 0, io_ptr, ret_fs);
1225 (*ret_fs)->priv_data = ctx;
1226 e2fsck_set_bitmap_type(*ret_fs, EXT2FS_BMAP64_RBTREE,
1232 static const char *my_ver_string = E2FSPROGS_VERSION;
1233 static const char *my_ver_date = E2FSPROGS_DATE;
1235 static errcode_t e2fsck_check_mmp(ext2_filsys fs, e2fsck_t ctx)
1237 struct mmp_struct *mmp_s;
1238 unsigned int mmp_check_interval;
1239 errcode_t retval = 0;
1240 struct problem_context pctx;
1241 unsigned int wait_time = 0;
1243 clear_problem_context(&pctx);
1244 if (fs->mmp_buf == NULL) {
1245 retval = ext2fs_get_mem(fs->blocksize, &fs->mmp_buf);
1250 retval = ext2fs_mmp_read(fs, fs->super->s_mmp_block, fs->mmp_buf);
1254 mmp_s = fs->mmp_buf;
1256 mmp_check_interval = fs->super->s_mmp_update_interval;
1257 if (mmp_check_interval < EXT4_MMP_MIN_CHECK_INTERVAL)
1258 mmp_check_interval = EXT4_MMP_MIN_CHECK_INTERVAL;
1261 * If check_interval in MMP block is larger, use that instead of
1262 * check_interval from the superblock.
1264 if (mmp_s->mmp_check_interval > mmp_check_interval)
1265 mmp_check_interval = mmp_s->mmp_check_interval;
1267 wait_time = mmp_check_interval * 2 + 1;
1269 if (mmp_s->mmp_seq == EXT4_MMP_SEQ_CLEAN)
1271 else if (mmp_s->mmp_seq == EXT4_MMP_SEQ_FSCK)
1272 retval = EXT2_ET_MMP_FSCK_ON;
1273 else if (mmp_s->mmp_seq > EXT4_MMP_SEQ_MAX)
1274 retval = EXT2_ET_MMP_UNKNOWN_SEQ;
1279 /* Print warning if e2fsck will wait for more than 20 secs. */
1280 if (verbose || wait_time > EXT4_MMP_MIN_CHECK_INTERVAL * 4) {
1281 log_out(ctx, _("MMP interval is %u seconds and total wait "
1282 "time is %u seconds. Please wait...\n"),
1283 mmp_check_interval, wait_time * 2);
1290 if (retval == EXT2_ET_MMP_BAD_BLOCK) {
1291 if (fix_problem(ctx, PR_0_MMP_INVALID_BLK, &pctx)) {
1292 fs->super->s_mmp_block = 0;
1293 ext2fs_mark_super_dirty(fs);
1296 } else if (retval == EXT2_ET_MMP_FAILED) {
1297 com_err(ctx->program_name, retval, "%s",
1298 _("while checking MMP block"));
1299 dump_mmp_msg(fs->mmp_buf, NULL);
1300 } else if (retval == EXT2_ET_MMP_FSCK_ON ||
1301 retval == EXT2_ET_MMP_UNKNOWN_SEQ) {
1302 com_err(ctx->program_name, retval, "%s",
1303 _("while checking MMP block"));
1304 dump_mmp_msg(fs->mmp_buf,
1305 _("If you are sure the filesystem is not "
1306 "in use on any node, run:\n"
1307 "'tune2fs -f -E clear_mmp %s'\n"),
1309 } else if (retval == EXT2_ET_MMP_MAGIC_INVALID) {
1310 if (fix_problem(ctx, PR_0_MMP_INVALID_MAGIC, &pctx)) {
1311 ext2fs_mmp_clear(fs);
1314 } else if (retval == EXT2_ET_MMP_CSUM_INVALID) {
1315 if (fix_problem(ctx, PR_0_MMP_CSUM_INVALID, &pctx)) {
1316 ext2fs_mmp_clear(fs);
1320 com_err(ctx->program_name, retval, "%s",
1321 _("while reading MMP block"));
1325 static int e2fsck_setup_tdb(e2fsck_t ctx, io_manager *io_ptr)
1327 errcode_t retval = ENOMEM;
1328 char *tdb_dir = NULL, *tdb_file = NULL;
1329 char *dev_name, *tmp_name;
1330 int free_tdb_dir = 0;
1332 /* (re)open a specific undo file */
1333 if (ctx->undo_file && ctx->undo_file[0] != 0) {
1334 retval = set_undo_io_backing_manager(*io_ptr);
1337 *io_ptr = undo_io_manager;
1338 retval = set_undo_io_backup_file(ctx->undo_file);
1341 printf(_("Overwriting existing filesystem; this can be undone "
1342 "using the command:\n"
1343 " e2undo %s %s\n\n"),
1344 ctx->undo_file, ctx->filesystem_name);
1349 * Configuration via a conf file would be
1352 tdb_dir = getenv("E2FSPROGS_UNDO_DIR");
1354 profile_get_string(ctx->profile, "defaults",
1355 "undo_dir", 0, "/var/lib/e2fsprogs",
1360 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
1361 access(tdb_dir, W_OK)) {
1367 tmp_name = strdup(ctx->filesystem_name);
1370 dev_name = basename(tmp_name);
1371 tdb_file = malloc(strlen(tdb_dir) + 8 + strlen(dev_name) + 7 + 1);
1376 sprintf(tdb_file, "%s/e2fsck-%s.e2undo", tdb_dir, dev_name);
1379 if ((unlink(tdb_file) < 0) && (errno != ENOENT)) {
1381 com_err(ctx->program_name, retval,
1382 _("while trying to delete %s"), tdb_file);
1386 retval = set_undo_io_backing_manager(*io_ptr);
1389 *io_ptr = undo_io_manager;
1390 retval = set_undo_io_backup_file(tdb_file);
1393 printf(_("Overwriting existing filesystem; this can be undone "
1394 "using the command:\n"
1395 " e2undo %s %s\n\n"), tdb_file, ctx->filesystem_name);
1407 com_err(ctx->program_name, retval, "%s",
1408 _("while trying to setup undo file\n"));
1412 int main (int argc, char *argv[])
1414 errcode_t retval = 0, retval2 = 0, orig_retval = 0;
1415 int exit_value = FSCK_OK;
1418 struct ext2_super_block *sb;
1419 const char *lib_ver_date;
1420 int my_ver, lib_ver;
1422 blk64_t orig_superblock = ~(blk64_t)0;
1423 struct problem_context pctx;
1424 int flags, run_result, was_changed;
1426 int sysval, sys_page_size = 4096;
1430 enum quota_type qtype;
1431 struct ext2fs_journal_params jparams;
1433 clear_problem_context(&pctx);
1442 setlocale(LC_MESSAGES, "");
1443 setlocale(LC_CTYPE, "");
1444 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1445 textdomain(NLS_CAT_NAME);
1446 set_com_err_gettext(gettext);
1448 my_ver = ext2fs_parse_version_string(my_ver_string);
1449 lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
1450 if (my_ver > lib_ver) {
1451 fprintf( stderr, "%s",
1452 _("Error: ext2fs library version out of date!\n"));
1453 show_version_only++;
1456 retval = PRS(argc, argv, &ctx);
1458 com_err("e2fsck", retval, "%s",
1459 _("while trying to initialize program"));
1462 reserve_stdio_fds();
1464 set_up_logging(ctx);
1468 fputs("E2fsck run: ", ctx->logf);
1469 for (i = 0; i < argc; i++) {
1471 fputc(' ', ctx->logf);
1472 fputs(argv[i], ctx->logf);
1474 fputc('\n', ctx->logf);
1476 if (ctx->problem_logf) {
1479 fputs("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n",
1481 fprintf(ctx->problem_logf, "<problem_log time=\"%lu\">\n",
1482 (unsigned long) ctx->now);
1483 fprintf(ctx->problem_logf, "<invocation prog=\"%s\"",
1485 for (i = 1; i < argc; i++)
1486 fprintf(ctx->problem_logf, " arg%d=\"%s\"", i, argv[i]);
1487 fputs("/>\n", ctx->problem_logf);
1490 init_resource_track(&ctx->global_rtrack, NULL);
1491 if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
1492 log_err(ctx, "e2fsck %s (%s)\n", my_ver_string,
1495 if (show_version_only) {
1496 log_err(ctx, _("\tUsing %s, %s\n"),
1497 error_message(EXT2_ET_BASE), lib_ver_date);
1503 if (!(ctx->options & E2F_OPT_PREEN) &&
1504 !(ctx->options & E2F_OPT_NO) &&
1505 !(ctx->options & E2F_OPT_YES)) {
1506 if (!ctx->interactive)
1508 _("need terminal for interactive repairs"));
1510 ctx->superblock = ctx->use_superblock;
1512 flags = EXT2_FLAG_SKIP_MMP | EXT2_FLAG_THREADS;
1514 #ifdef CONFIG_TESTIO_DEBUG
1515 if (getenv("TEST_IO_FLAGS") || getenv("TEST_IO_BLOCK")) {
1516 io_ptr = test_io_manager;
1517 test_io_backing_manager = unix_io_manager;
1520 io_ptr = unix_io_manager;
1521 flags |= EXT2_FLAG_NOFREE_ON_ERROR;
1522 profile_get_boolean(ctx->profile, "options", "old_bitmaps", 0, 0,
1525 flags |= EXT2_FLAG_64BITS;
1526 if ((ctx->options & E2F_OPT_READONLY) == 0) {
1527 flags |= EXT2_FLAG_RW;
1528 if (!(ctx->mount_flags & EXT2_MF_ISROOT &&
1529 ctx->mount_flags & EXT2_MF_READONLY))
1530 flags |= EXT2_FLAG_EXCLUSIVE;
1531 if ((ctx->mount_flags & EXT2_MF_READONLY) &&
1532 (ctx->options & E2F_OPT_FORCE))
1533 flags &= ~EXT2_FLAG_EXCLUSIVE;
1536 if (ctx->undo_file) {
1537 retval = e2fsck_setup_tdb(ctx, &io_ptr);
1542 ctx->openfs_flags = flags;
1543 retval = try_open_fs(ctx, flags, io_ptr, &fs);
1545 if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
1546 !(ctx->flags & E2F_FLAG_SB_SPECIFIED) &&
1547 ((retval == EXT2_ET_BAD_MAGIC) ||
1548 (retval == EXT2_ET_SB_CSUM_INVALID) ||
1549 (retval == EXT2_ET_CORRUPT_SUPERBLOCK) ||
1550 ((retval == 0) && (retval2 = ext2fs_check_desc(fs))))) {
1552 pctx.errcode = retval;
1553 fix_problem(ctx, PR_0_OPEN_FAILED, &pctx);
1556 pctx.errcode = retval2;
1557 fix_problem(ctx, PR_0_CHECK_DESC_FAILED, &pctx);
1560 if (retval2 == ENOMEM || retval2 == EXT2_ET_NO_MEMORY) {
1564 if (fs->flags & EXT2_FLAG_NOFREE_ON_ERROR) {
1568 if (!fs || (fs->group_desc_count > 1)) {
1569 log_out(ctx, _("%s: %s trying backup blocks...\n"),
1571 retval ? _("Superblock invalid,") :
1572 _("Group descriptors look bad..."));
1573 orig_superblock = ctx->superblock;
1574 get_backup_sb(ctx, fs, ctx->filesystem_name, io_ptr);
1576 ext2fs_close_free(&fs);
1577 orig_retval = retval;
1578 retval = try_open_fs(ctx, flags, io_ptr, &fs);
1579 if ((orig_retval == 0) && retval != 0) {
1581 ext2fs_close_free(&fs);
1582 log_out(ctx, _("%s: %s while using the "
1585 error_message(retval));
1586 log_out(ctx, _("%s: going back to original "
1589 ctx->superblock = orig_superblock;
1590 retval = try_open_fs(ctx, flags, io_ptr, &fs);
1594 if (((retval == EXT2_ET_UNSUPP_FEATURE) ||
1595 (retval == EXT2_ET_RO_UNSUPP_FEATURE)) &&
1598 features[0] = (sb->s_feature_compat &
1599 ~EXT2_LIB_FEATURE_COMPAT_SUPP);
1600 features[1] = (sb->s_feature_incompat &
1601 ~EXT2_LIB_FEATURE_INCOMPAT_SUPP);
1602 features[2] = (sb->s_feature_ro_compat &
1603 ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP);
1604 if (features[0] || features[1] || features[2])
1605 goto print_unsupp_features;
1610 retval = orig_retval;
1611 com_err(ctx->program_name, retval, _("while trying to open %s"),
1612 ctx->filesystem_name);
1613 if (retval == EXT2_ET_REV_TOO_HIGH) {
1615 _("The filesystem revision is apparently "
1616 "too high for this version of e2fsck.\n"
1617 "(Or the filesystem superblock "
1618 "is corrupt)\n\n"));
1619 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
1620 } else if (retval == EXT2_ET_SHORT_READ)
1622 _("Could this be a zero-length partition?\n"));
1623 else if ((retval == EPERM) || (retval == EACCES))
1624 log_out(ctx, _("You must have %s access to the "
1625 "filesystem or be root\n"),
1626 (ctx->options & E2F_OPT_READONLY) ?
1628 else if (retval == ENXIO)
1630 _("Possibly non-existent or swap device?\n"));
1631 else if (retval == EBUSY)
1632 log_out(ctx, "%s", _("Filesystem mounted or opened "
1633 "exclusively by another program?\n"));
1634 else if (retval == ENOENT)
1636 _("Possibly non-existent device?\n"));
1638 else if (retval == EROFS)
1639 log_out(ctx, "%s", _("Disk write-protected; use the "
1640 "-n option to do a read-only\n"
1641 "check of the device.\n"));
1645 * Let's try once more will less consistency checking
1646 * so that we are able to recover from more errors
1647 * (e.g. some tool messing up some value in the sb).
1649 if (((retval == EXT2_ET_CORRUPT_SUPERBLOCK) ||
1650 (retval == EXT2_ET_BAD_DESC_SIZE)) &&
1651 !(flags & EXT2_FLAG_IGNORE_SB_ERRORS)) {
1653 ext2fs_close_free(&fs);
1654 log_out(ctx, _("%s: Trying to load superblock "
1655 "despite errors...\n"),
1657 flags |= EXT2_FLAG_IGNORE_SB_ERRORS;
1659 * If we tried backup sb, revert to the
1662 if (orig_superblock != ~(blk64_t)0)
1663 ctx->superblock = orig_superblock;
1666 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
1667 if (retval == EXT2_ET_BAD_MAGIC)
1668 check_plausibility(ctx->filesystem_name,
1669 CHECK_FS_EXIST, NULL);
1671 fatal_error(ctx, 0);
1674 * We only update the master superblock because (a) paranoia;
1675 * we don't want to corrupt the backup superblocks, and (b) we
1676 * don't need to update the mount count and last checked
1677 * fields in the backup superblock (the kernel doesn't update
1678 * the backup superblocks anyway). With newer versions of the
1679 * library this flag is set by ext2fs_open2(), but we set this
1680 * here just to be sure. (No, we don't support e2fsck running
1681 * with some other libext2fs than the one that it was shipped
1682 * with, but just in case....)
1684 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1686 if (!(ctx->flags & E2F_FLAG_GOT_DEVSIZE)) {
1687 __u32 blocksize = EXT2_BLOCK_SIZE(fs->super);
1688 int need_restart = 0;
1690 pctx.errcode = ext2fs_get_device_size2(ctx->filesystem_name,
1694 * The floppy driver refuses to allow anyone else to
1695 * open the device if has been opened with O_EXCL;
1696 * this is unlike other block device drivers in Linux.
1697 * To handle this, we close the filesystem and then
1698 * reopen the filesystem after we get the device size.
1700 if (pctx.errcode == EBUSY) {
1701 ext2fs_close_free(&fs);
1704 ext2fs_get_device_size2(ctx->filesystem_name,
1708 if (pctx.errcode == EXT2_ET_UNIMPLEMENTED)
1709 ctx->num_blocks = 0;
1710 else if (pctx.errcode) {
1711 fix_problem(ctx, PR_0_GETSIZE_ERROR, &pctx);
1712 ctx->flags |= E2F_FLAG_ABORT;
1713 fatal_error(ctx, 0);
1715 ctx->flags |= E2F_FLAG_GOT_DEVSIZE;
1724 if (sb->s_rev_level > E2FSCK_CURRENT_REV) {
1725 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
1726 _("while trying to open %s"),
1727 ctx->filesystem_name);
1729 fatal_error(ctx, _("Get a newer version of e2fsck!"));
1733 * Set the device name, which is used whenever we print error
1734 * or informational messages to the user.
1736 if (ctx->device_name == 0 && sb->s_volume_name[0])
1737 ctx->device_name = string_copy(ctx, (char *) sb->s_volume_name,
1738 sizeof(sb->s_volume_name));
1740 if (ctx->device_name == 0)
1741 ctx->device_name = string_copy(ctx, ctx->filesystem_name, 0);
1742 for (cp = ctx->device_name; *cp; cp++)
1743 if (isspace(*cp) || *cp == ':')
1746 if (ctx->problem_logf) {
1748 fprintf(ctx->problem_logf, "<filesystem dev=\"%s\"",
1749 ctx->filesystem_name);
1750 if (!uuid_is_null(sb->s_uuid)) {
1753 uuid_unparse(sb->s_uuid, buf);
1754 fprintf(ctx->problem_logf, " uuid=\"%s\"", buf);
1756 if (sb->s_volume_name[0])
1757 fprintf(ctx->problem_logf, " label=\"%.*s\"",
1758 EXT2_LEN_STR(sb->s_volume_name));
1760 fputs("/>\n", ctx->problem_logf);
1763 ehandler_init(fs->io);
1765 if (ext2fs_has_feature_mmp(fs->super) &&
1766 (flags & EXT2_FLAG_SKIP_MMP)) {
1767 if (e2fsck_check_mmp(fs, ctx))
1768 fatal_error(ctx, 0);
1771 * Restart in order to reopen fs but this time start mmp.
1773 ext2fs_close_free(&ctx->fs);
1774 flags &= ~EXT2_FLAG_SKIP_MMP;
1779 fprintf(ctx->logf, "Filesystem UUID: %s\n",
1780 e2p_uuid2str(sb->s_uuid));
1783 * Make sure the ext3 superblock fields are consistent.
1785 if ((ctx->mount_flags & (EXT2_MF_MOUNTED | EXT2_MF_BUSY)) == 0) {
1786 retval = e2fsck_check_ext3_journal(ctx);
1788 com_err(ctx->program_name, retval,
1789 _("while checking journal for %s"),
1792 _("Cannot proceed with file system check"));
1797 * Check to see if we need to do ext3-style recovery. If so,
1798 * do it, and then restart the fsck.
1800 if (ext2fs_has_feature_journal_needs_recovery(sb)) {
1801 if (ctx->options & E2F_OPT_READONLY) {
1803 _("Warning: skipping journal recovery because "
1804 "doing a read-only filesystem check.\n"));
1805 io_channel_flush(ctx->fs->io);
1807 if (ctx->flags & E2F_FLAG_RESTARTED) {
1809 * Whoops, we attempted to run the
1810 * journal twice. This should never
1811 * happen, unless the hardware or
1812 * device driver is being bogus.
1814 com_err(ctx->program_name, 0,
1815 _("unable to set superblock flags "
1816 "on %s\n"), ctx->device_name);
1817 fatal_error(ctx, 0);
1819 retval = e2fsck_run_ext3_journal(ctx);
1820 if (retval == EFSBADCRC) {
1821 log_out(ctx, _("Journal checksum error "
1824 } else if (retval == EFSCORRUPTED) {
1825 log_out(ctx, _("Journal corrupted in %s\n"),
1827 } else if (retval) {
1828 com_err(ctx->program_name, retval,
1829 _("while recovering journal of %s"),
1832 ext2fs_close_free(&ctx->fs);
1833 ctx->flags |= E2F_FLAG_RESTARTED;
1839 * Check for compatibility with the feature sets. We need to
1840 * be more stringent than ext2fs_open().
1842 features[0] = sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP;
1843 features[1] = sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP;
1844 features[2] = (sb->s_feature_ro_compat &
1845 ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP);
1846 print_unsupp_features:
1847 if (features[0] || features[1] || features[2]) {
1849 __u32 *mask = features, m;
1851 log_err(ctx, _("%s has unsupported feature(s):"),
1852 ctx->filesystem_name);
1854 for (i=0; i <3; i++,mask++) {
1855 for (j=0,m=1; j < 32; j++, m<<=1) {
1858 e2p_feature2string(i, m));
1865 if (ext2fs_has_feature_casefold(sb) && !fs->encoding) {
1866 log_err(ctx, _("%s has unsupported encoding: %0x\n"),
1867 ctx->filesystem_name, sb->s_encoding);
1872 * If the user specified a specific superblock, presumably the
1873 * master superblock has been trashed. So we mark the
1874 * superblock as dirty, so it can be written out.
1876 if (ctx->superblock &&
1877 !(ctx->options & E2F_OPT_READONLY))
1878 ext2fs_mark_super_dirty(fs);
1881 * Calculate the number of filesystem blocks per pagesize. If
1882 * fs->blocksize > page_size, set the number of blocks per
1883 * pagesize to 1 to avoid division by zero errors.
1886 sysval = sysconf(_SC_PAGESIZE);
1888 sys_page_size = sysval;
1889 #endif /* _SC_PAGESIZE */
1890 ctx->blocks_per_page = sys_page_size / fs->blocksize;
1891 if (ctx->blocks_per_page == 0)
1892 ctx->blocks_per_page = 1;
1894 if (ctx->superblock)
1895 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
1896 ext2fs_mark_valid(fs);
1897 check_super_block(ctx);
1898 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1899 fatal_error(ctx, 0);
1901 check_resize_inode(ctx);
1902 if (bad_blocks_file)
1903 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
1905 read_bad_blocks_file(ctx, 0, !keep_bad_blocks); /* Test disk */
1906 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1907 fatal_error(ctx, 0);
1910 * Mark the system as valid, 'til proven otherwise
1912 ext2fs_mark_valid(fs);
1914 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
1916 log_out(ctx, _("%s: %s while reading bad blocks inode\n"),
1917 ctx->program_name, error_message(retval));
1919 log_out(ctx, "%s", _("This doesn't bode well, "
1920 "but we'll try to go on...\n"));
1924 * Save the journal size in megabytes.
1925 * Try and use the journal size from the backup else let e2fsck
1926 * find the default journal size. If fast commit feature is enabled,
1927 * it is not clear how many of the journal blocks were fast commit
1928 * blocks. So, ignore the size of journal found in backup.
1930 * TODO: Add a new backup type that captures fast commit info as
1933 if (sb->s_jnl_backup_type == EXT3_JNL_BACKUP_BLOCKS &&
1934 !ext2fs_has_feature_fast_commit(sb))
1935 journal_size = (sb->s_jnl_blocks[15] << (32 - 20)) |
1936 (sb->s_jnl_blocks[16] >> 20);
1940 if (ext2fs_has_feature_quota(sb)) {
1941 /* Quotas were enabled. Do quota accounting during fsck. */
1942 clear_problem_context(&pctx);
1943 pctx.errcode = quota_init_context(&ctx->qctx, ctx->fs, 0);
1945 fix_problem(ctx, PR_0_QUOTA_INIT_CTX, &pctx);
1946 fatal_error(ctx, 0);
1950 run_result = e2fsck_run(ctx);
1951 e2fsck_clear_progbar(ctx);
1953 if (!ctx->invalid_bitmaps &&
1954 (ctx->flags & E2F_FLAG_JOURNAL_INODE)) {
1955 if (fix_problem(ctx, PR_6_RECREATE_JOURNAL, &pctx)) {
1956 if (journal_size < 1024) {
1957 ext2fs_get_journal_params(&jparams, fs);
1959 jparams.num_journal_blocks = journal_size;
1960 jparams.num_fc_blocks = 0;
1962 log_out(ctx, _("Creating journal (%d blocks): "),
1963 jparams.num_journal_blocks);
1965 retval = ext2fs_add_journal_inode3(fs, &jparams, ~0ULL, 0);
1967 log_out(ctx, "%s: while trying to create "
1968 "journal\n", error_message(retval));
1971 log_out(ctx, "%s", _(" Done.\n"));
1973 _("\n*** journal has been regenerated ***\n"));
1978 if (run_result & E2F_FLAG_ABORT) {
1979 fatal_error(ctx, _("aborted"));
1980 } else if (run_result & E2F_FLAG_CANCEL) {
1981 log_out(ctx, _("%s: e2fsck canceled.\n"), ctx->device_name ?
1982 ctx->device_name : ctx->filesystem_name);
1983 exit_value |= FSCK_CANCELED;
1984 } else if (ctx->qctx && !ctx->invalid_bitmaps) {
1987 for (qtype = 0; qtype < MAXQUOTAS; qtype++) {
1988 if (*quota_sb_inump(sb, qtype) == 0)
1992 retval = quota_compare_and_update(ctx->qctx, qtype,
1994 if ((retval || needs_writeout) &&
1995 fix_problem(ctx, PR_6_UPDATE_QUOTAS, &pctx)) {
1996 pctx.errcode = quota_write_inode(ctx->qctx,
1999 (void) fix_problem(ctx,
2000 PR_6_WRITE_QUOTAS, &pctx);
2003 quota_release_context(&ctx->qctx);
2006 if (run_result == E2F_FLAG_RESTART) {
2008 _("Restarting e2fsck from the beginning...\n"));
2009 retval = e2fsck_reset_context(ctx);
2011 com_err(ctx->program_name, retval, "%s",
2012 _("while resetting context"));
2013 fatal_error(ctx, 0);
2015 ext2fs_close_free(&ctx->fs);
2020 mtrace_print("Cleanup");
2022 was_changed = ext2fs_test_changed(fs);
2023 if (!(ctx->flags & E2F_FLAG_RUN_RETURN) &&
2024 !(ctx->options & E2F_OPT_READONLY)) {
2025 if (ext2fs_test_valid(fs)) {
2026 if (!(sb->s_state & EXT2_VALID_FS))
2027 exit_value |= FSCK_NONDESTRUCT;
2028 sb->s_state = EXT2_VALID_FS;
2029 if (check_backup_super_block(ctx))
2030 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
2032 sb->s_state &= ~EXT2_VALID_FS;
2033 if (!(ctx->flags & E2F_FLAG_TIME_INSANE))
2034 sb->s_lastcheck = ctx->now;
2035 sb->s_mnt_count = 0;
2036 memset(((char *) sb) + EXT4_S_ERR_START, 0, EXT4_S_ERR_LEN);
2037 pctx.errcode = ext2fs_set_gdt_csum(ctx->fs);
2039 fix_problem(ctx, PR_6_SET_BG_CHECKSUM, &pctx);
2040 ext2fs_mark_super_dirty(fs);
2043 if (ext2fs_has_feature_shared_blocks(ctx->fs->super) &&
2044 (ctx->options & E2F_OPT_UNSHARE_BLOCKS) &&
2045 (ctx->options & E2F_OPT_NO))
2046 /* Don't try to write or flush I/O, we just wanted to know whether or
2047 * not there were enough free blocks to undo deduplication.
2051 if (!(ctx->options & E2F_OPT_READONLY)) {
2052 e2fsck_write_bitmaps(ctx);
2053 if (fs->flags & EXT2_FLAG_DIRTY) {
2054 pctx.errcode = ext2fs_flush(ctx->fs);
2056 fix_problem(ctx, PR_6_FLUSH_FILESYSTEM, &pctx);
2058 pctx.errcode = io_channel_flush(ctx->fs->io);
2060 fix_problem(ctx, PR_6_IO_FLUSH, &pctx);
2064 int fs_fixed = (ctx->flags & E2F_FLAG_PROBLEMS_FIXED);
2067 exit_value |= FSCK_NONDESTRUCT;
2068 if (!(ctx->options & E2F_OPT_PREEN)) {
2069 #if 0 /* Do this later; it breaks too many tests' golden outputs */
2070 log_out(ctx, fs_fixed ?
2071 _("\n%s: ***** FILE SYSTEM ERRORS "
2072 "CORRECTED *****\n") :
2073 _("%s: File system was modified.\n"),
2077 _("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
2081 if (ctx->mount_flags & EXT2_MF_ISROOT) {
2082 log_out(ctx, _("%s: ***** REBOOT SYSTEM *****\n"),
2084 exit_value |= FSCK_REBOOT;
2089 if (!ext2fs_test_valid(fs) ||
2090 ((exit_value & FSCK_CANCELED) &&
2091 (sb->s_state & EXT2_ERROR_FS))) {
2092 log_out(ctx, _("\n%s: ********** WARNING: Filesystem still has "
2093 "errors **********\n\n"), ctx->device_name);
2094 exit_value |= FSCK_UNCORRECTED;
2095 exit_value &= ~FSCK_NONDESTRUCT;
2097 if (exit_value & FSCK_CANCELED) {
2098 int allow_cancellation;
2100 profile_get_boolean(ctx->profile, "options",
2101 "allow_cancellation", 0, 0,
2102 &allow_cancellation);
2103 exit_value &= ~FSCK_NONDESTRUCT;
2104 if (allow_cancellation && ext2fs_test_valid(fs) &&
2105 (sb->s_state & EXT2_VALID_FS) &&
2106 !(sb->s_state & EXT2_ERROR_FS))
2111 print_resource_track(ctx, NULL, &ctx->global_rtrack, ctx->fs->io);
2113 ext2fs_close_free(&ctx->fs);
2114 free(ctx->journal_name);
2117 fprintf(ctx->logf, "Exit status: %d\n", exit_value);
2118 e2fsck_free_context(ctx);
2119 remove_error_table(&et_ext2_error_table);
2120 remove_error_table(&et_prof_error_table);