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
36 #ifdef HAVE_SYS_IOCTL_H
37 #include <sys/ioctl.h>
43 #include "et/com_err.h"
46 #include "../version.h"
48 /* Command line options */
50 static int normalize_swapfs;
51 static int cflag; /* check disk */
52 static int show_version_only;
55 static int replace_bad_blocks;
56 static int keep_bad_blocks;
57 static char *bad_blocks_file;
59 e2fsck_t e2fsck_global_ctx; /* Try your very best not to use this! */
61 #ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jfs-debug */
62 int journal_enable_debug = -1;
65 static void usage(e2fsck_t ctx)
68 _("Usage: %s [-panyrcdfvstDFSV] [-b superblock] [-B blocksize]\n"
69 "\t\t[-I inode_buffer_blocks] [-P process_inode_size]\n"
70 "\t\t[-l|-L bad_blocks_file] [-C fd] [-j ext-journal]\n"
71 "\t\t[-E extended-options] device\n"),
74 fprintf(stderr, _("\nEmergency help:\n"
75 " -p Automatic repair (no questions)\n"
76 " -n Make no changes to the filesystem\n"
77 " -y Assume \"yes\" to all questions\n"
78 " -c Check for bad blocks and add them to the badblock list\n"
79 " -f Force checking even if filesystem is marked clean\n"));
82 " -b superblock Use alternative superblock\n"
83 " -B blocksize Force blocksize when looking for superblock\n"
84 " -j external-journal Set location of the external journal\n"
85 " -l bad_blocks_file Add to badblocks list\n"
86 " -L bad_blocks_file Set badblocks list\n"
92 static void show_stats(e2fsck_t ctx)
94 ext2_filsys fs = ctx->fs;
95 int inodes, inodes_used, blocks, blocks_used;
97 int num_files, num_links;
100 dir_links = 2 * ctx->fs_directory_count - 1;
101 num_files = ctx->fs_total_count - dir_links;
102 num_links = ctx->fs_links_count - dir_links;
103 inodes = fs->super->s_inodes_count;
104 inodes_used = (fs->super->s_inodes_count -
105 fs->super->s_free_inodes_count);
106 blocks = fs->super->s_blocks_count;
107 blocks_used = (fs->super->s_blocks_count -
108 fs->super->s_free_blocks_count);
110 frag_percent = (10000 * ctx->fs_fragmented) / inodes_used;
111 frag_percent = (frag_percent + 5) / 10;
114 printf(_("%s: %d/%d files (%0d.%d%% non-contiguous), %d/%d blocks\n"),
115 ctx->device_name, inodes_used, inodes,
116 frag_percent / 10, frag_percent % 10,
117 blocks_used, blocks);
120 printf (P_("\n%8d inode used (%d%%)\n", "\n%8d inodes used (%d%%)\n",
121 inodes_used), inodes_used, 100 * inodes_used / inodes);
122 printf (P_("%8d non-contiguous inode (%0d.%d%%)\n",
123 "%8d non-contiguous inodes (%0d.%d%%)\n",
125 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
126 printf (_(" # of inodes with ind/dind/tind blocks: %d/%d/%d\n"),
127 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
128 printf (P_("%8d block used (%d%%)\n", "%8d blocks used (%d%%)\n",
130 blocks_used, (int) ((long long) 100 * blocks_used / blocks));
131 printf (P_("%8d bad block\n", "%8d bad blocks\n",
132 ctx->fs_badblocks_count), ctx->fs_badblocks_count);
133 printf (P_("%8d large file\n", "%8d large files\n",
134 ctx->large_files), ctx->large_files);
135 printf (P_("\n%8d regular file\n", "\n%8d regular files\n",
136 ctx->fs_regular_count), ctx->fs_regular_count);
137 printf (P_("%8d directory\n", "%8d directories\n",
138 ctx->fs_directory_count), ctx->fs_directory_count);
139 printf (P_("%8d character device file\n",
140 "%8d character device files\n", ctx->fs_chardev_count),
141 ctx->fs_chardev_count);
142 printf (P_("%8d block device file\n", "%8d block device files\n",
143 ctx->fs_blockdev_count), ctx->fs_blockdev_count);
144 printf (P_("%8d fifo\n", "%8d fifos\n", ctx->fs_fifo_count),
146 printf (P_("%8d link\n", "%8d links\n",
147 ctx->fs_links_count - dir_links),
148 ctx->fs_links_count - dir_links);
149 printf (P_("%8d symbolic link", "%8d symbolic links",
150 ctx->fs_symlinks_count), ctx->fs_symlinks_count);
151 printf (P_(" (%d fast symbolic link)\n", " (%d fast symbolic links)\n",
152 ctx->fs_fast_symlinks_count), ctx->fs_fast_symlinks_count);
153 printf (P_("%8d socket\n", "%8d sockets\n", ctx->fs_sockets_count),
154 ctx->fs_sockets_count);
155 printf ("--------\n");
156 printf (P_("%8d file\n", "%8d files\n",
157 ctx->fs_total_count - dir_links),
158 ctx->fs_total_count - dir_links);
161 static void check_mount(e2fsck_t ctx)
166 retval = ext2fs_check_if_mounted(ctx->filesystem_name,
169 com_err("ext2fs_check_if_mount", retval,
170 _("while determining whether %s is mounted."),
171 ctx->filesystem_name);
176 * If the filesystem isn't mounted, or it's the root filesystem
177 * and it's mounted read-only, then everything's fine.
179 if ((!(ctx->mount_flags & EXT2_MF_MOUNTED)) ||
180 ((ctx->mount_flags & EXT2_MF_ISROOT) &&
181 (ctx->mount_flags & EXT2_MF_READONLY)))
184 if (ctx->options & E2F_OPT_READONLY) {
185 printf(_("Warning! %s is mounted.\n"), ctx->filesystem_name);
189 printf(_("%s is mounted. "), ctx->filesystem_name);
190 if (!ctx->interactive)
191 fatal_error(ctx, _("Cannot continue, aborting.\n\n"));
192 printf(_("\n\n\007\007\007\007WARNING!!! "
193 "Running e2fsck on a mounted filesystem may cause\n"
194 "SEVERE filesystem damage.\007\007\007\n\n"));
195 cont = ask_yn(_("Do you really want to continue"), -1);
197 printf (_("check aborted.\n"));
203 static int is_on_batt(void)
206 char tmp[80], tmp2[80];
209 f = fopen("/proc/apm", "r");
211 if (fscanf(f, "%s %s %s %x", tmp, tmp, tmp, &acflag) != 4)
214 return (acflag != 1);
216 f = fopen("/proc/acpi/ac_adapter/AC/state", "r");
218 if (fscanf(f, "%s %s", tmp2, tmp) != 2)
221 if (strncmp(tmp, "off-line", 8) == 0)
228 * This routine checks to see if a filesystem can be skipped; if so,
229 * it will exit with E2FSCK_OK. Under some conditions it will print a
230 * message explaining why a check is being forced.
232 static void check_if_skip(e2fsck_t ctx)
234 ext2_filsys fs = ctx->fs;
235 const char *reason = NULL;
236 unsigned int reason_arg = 0;
238 int batt = is_on_batt();
239 time_t now = time(0);
241 if ((ctx->options & E2F_OPT_FORCE) || bad_blocks_file ||
245 if (fs->super->s_state & EXT2_ERROR_FS)
246 reason = _(" contains a file system with errors");
247 else if ((fs->super->s_state & EXT2_VALID_FS) == 0)
248 reason = _(" was not cleanly unmounted");
249 else if ((fs->super->s_max_mnt_count > 0) &&
250 (fs->super->s_mnt_count >=
251 (unsigned) fs->super->s_max_mnt_count)) {
252 reason = _(" has been mounted %u times without being checked");
253 reason_arg = fs->super->s_mnt_count;
254 if (batt && (fs->super->s_mnt_count <
255 (unsigned) fs->super->s_max_mnt_count*2))
257 } else if (fs->super->s_checkinterval &&
258 ((now - fs->super->s_lastcheck) >=
259 fs->super->s_checkinterval)) {
260 reason = _(" has gone %u days without being checked");
261 reason_arg = (now - fs->super->s_lastcheck)/(3600*24);
262 if (batt && ((now - fs->super->s_lastcheck) <
263 fs->super->s_checkinterval*2))
267 fputs(ctx->device_name, stdout);
268 printf(reason, reason_arg);
269 fputs(_(", check forced.\n"), stdout);
272 printf(_("%s: clean, %d/%d files, %d/%d blocks"), ctx->device_name,
273 fs->super->s_inodes_count - fs->super->s_free_inodes_count,
274 fs->super->s_inodes_count,
275 fs->super->s_blocks_count - fs->super->s_free_blocks_count,
276 fs->super->s_blocks_count);
278 if (fs->super->s_max_mnt_count > 0) {
279 next_check = fs->super->s_max_mnt_count - fs->super->s_mnt_count;
283 if ((now - fs->super->s_lastcheck) >= fs->super->s_checkinterval)
285 if (next_check <= 5) {
287 fputs(_(" (check after next mount)"), stdout);
289 printf(_(" (check in %ld mounts)"), next_check);
294 e2fsck_free_context(ctx);
299 * For completion notice
305 struct percent_tbl e2fsck_tbl = {
306 5, { 0, 70, 90, 92, 95, 100 }
308 static char bar[128], spaces[128];
310 static float calc_percent(struct percent_tbl *tbl, int pass, int curr,
317 if (pass > tbl->max_pass || max == 0)
319 percent = ((float) curr) / ((float) max);
320 return ((percent * (tbl->table[pass] - tbl->table[pass-1]))
321 + tbl->table[pass-1]);
324 extern void e2fsck_clear_progbar(e2fsck_t ctx)
326 if (!(ctx->flags & E2F_FLAG_PROG_BAR))
329 printf("%s%s\r%s", ctx->start_meta, spaces + (sizeof(spaces) - 80),
332 ctx->flags &= ~E2F_FLAG_PROG_BAR;
335 int e2fsck_simple_progress(e2fsck_t ctx, const char *label, float percent,
338 static const char spinner[] = "\\|/-";
344 if (ctx->flags & E2F_FLAG_PROG_SUPPRESS)
348 * Calculate the new progress position. If the
349 * percentage hasn't changed, then we skip out right
352 if (ctx->progress_last_percent == (int) 10 * percent)
354 ctx->progress_last_percent = (int) 10 * percent;
357 * If we've already updated the spinner once within
358 * the last 1/8th of a second, no point doing it
361 gettimeofday(&tv, NULL);
362 tick = (tv.tv_sec << 3) + (tv.tv_usec / (1000000 / 8));
363 if ((tick == ctx->progress_last_time) &&
364 (percent != 0.0) && (percent != 100.0))
366 ctx->progress_last_time = tick;
369 * Advance the spinner, and note that the progress bar
370 * will be on the screen
372 ctx->progress_pos = (ctx->progress_pos+1) & 3;
373 ctx->flags |= E2F_FLAG_PROG_BAR;
375 dpywidth = 66 - strlen(label);
376 dpywidth = 8 * (dpywidth / 8);
380 i = ((percent * dpywidth) + 50) / 100;
381 printf("%s%s: |%s%s", ctx->start_meta, label,
382 bar + (sizeof(bar) - (i+1)),
383 spaces + (sizeof(spaces) - (dpywidth - i + 1)));
384 if (percent == 100.0)
387 fputc(spinner[ctx->progress_pos & 3], stdout);
389 printf(" %4.1f%% %u\r%s", percent, dpynum, ctx->stop_meta);
391 printf(" %4.1f%% \r%s", percent, ctx->stop_meta);
393 if (percent == 100.0)
394 e2fsck_clear_progbar(ctx);
400 static int e2fsck_update_progress(e2fsck_t ctx, int pass,
401 unsigned long cur, unsigned long max)
409 if (ctx->progress_fd) {
410 sprintf(buf, "%d %lu %lu\n", pass, cur, max);
411 write(ctx->progress_fd, buf, strlen(buf));
413 percent = calc_percent(&e2fsck_tbl, pass, cur, max);
414 e2fsck_simple_progress(ctx, ctx->device_name,
420 #define PATH_SET "PATH=/sbin"
422 static void reserve_stdio_fds(void)
427 fd = open("/dev/null", O_RDWR);
431 fprintf(stderr, _("ERROR: Couldn't open "
441 static void signal_progress_on(int sig EXT2FS_ATTR((unused)))
443 e2fsck_t ctx = e2fsck_global_ctx;
448 ctx->progress = e2fsck_update_progress;
449 ctx->progress_fd = 0;
452 static void signal_progress_off(int sig EXT2FS_ATTR((unused)))
454 e2fsck_t ctx = e2fsck_global_ctx;
459 e2fsck_clear_progbar(ctx);
463 static void signal_cancel(int sig EXT2FS_ATTR((unused)))
465 e2fsck_t ctx = e2fsck_global_ctx;
470 ctx->flags |= E2F_FLAG_CANCEL;
474 static void parse_extended_opts(e2fsck_t ctx, const char *opts)
476 char *buf, *token, *next, *p, *arg;
478 int extended_usage = 0;
480 buf = string_copy(ctx, opts, 0);
481 for (token = buf; token && *token; token = next) {
482 p = strchr(token, ',');
488 arg = strchr(token, '=');
493 if (strcmp(token, "ea_ver") == 0) {
498 ea_ver = strtoul(arg, &p, 0);
500 ((ea_ver != 1) && (ea_ver != 2))) {
502 _("Invalid EA version.\n"));
506 ctx->ext_attr_ver = ea_ver;
510 if (extended_usage) {
511 fprintf(stderr, _("Extended options are separated by commas, "
512 "and may take an argument which\n"
513 "is set off by an equals ('=') sign. "
514 "Valid raid options are:\n"
515 "\tea_ver=<ea_version (1 or 2)\n\n"));
521 static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
526 extern void *mallwatch;
533 char *extended_opts = 0;
535 retval = e2fsck_allocate_context(&ctx);
541 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
542 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
543 if (isatty(0) && isatty(1)) {
544 ctx->interactive = 1;
546 ctx->start_meta[0] = '\001';
547 ctx->stop_meta[0] = '\002';
549 memset(bar, '=', sizeof(bar)-1);
550 memset(spaces, ' ', sizeof(spaces)-1);
551 initialize_ext2_error_table();
552 blkid_get_cache(&ctx->blkid, NULL);
555 ctx->program_name = *argv;
557 ctx->program_name = "e2fsck";
558 while ((c = getopt (argc, argv, "panyrcC:B:dE:fvtFVM:b:I:j:P:l:L:N:SsDk")) != EOF)
561 ctx->progress = e2fsck_update_progress;
562 ctx->progress_fd = atoi(optarg);
563 if (!ctx->progress_fd)
565 /* Validate the file descriptor to avoid disasters */
566 fd = dup(ctx->progress_fd);
569 _("Error validating file descriptor %d: %s\n"),
571 error_message(errno));
573 _("Invalid completion information file descriptor"));
578 ctx->options |= E2F_OPT_COMPRESS_DIRS;
581 extended_opts = optarg;
585 if (ctx->options & (E2F_OPT_YES|E2F_OPT_NO)) {
588 _("Only one the options -p/-a, -n or -y may be specified."));
590 ctx->options |= E2F_OPT_PREEN;
593 if (ctx->options & (E2F_OPT_YES|E2F_OPT_PREEN))
595 ctx->options |= E2F_OPT_NO;
598 if (ctx->options & (E2F_OPT_PREEN|E2F_OPT_NO))
600 ctx->options |= E2F_OPT_YES;
603 #ifdef RESOURCE_TRACK
604 if (ctx->options & E2F_OPT_TIME)
605 ctx->options |= E2F_OPT_TIME2;
607 ctx->options |= E2F_OPT_TIME;
609 fprintf(stderr, _("The -t option is not "
610 "supported on this version of e2fsck.\n"));
615 ctx->options |= E2F_OPT_WRITECHECK;
616 ctx->options |= E2F_OPT_CHECKBLOCKS;
619 /* What we do by default, anyway! */
622 ctx->use_superblock = atoi(optarg);
623 ctx->flags |= E2F_FLAG_SB_SPECIFIED;
626 ctx->blocksize = atoi(optarg);
629 ctx->inode_buffer_blocks = atoi(optarg);
632 ctx->journal_name = string_copy(ctx, optarg, 0);
635 ctx->process_inode_size = atoi(optarg);
638 replace_bad_blocks++;
640 bad_blocks_file = string_copy(ctx, optarg, 0);
643 ctx->options |= E2F_OPT_DEBUG;
646 ctx->options |= E2F_OPT_FORCE;
655 show_version_only = 1;
659 mallwatch = (void *) strtol(optarg, NULL, 0);
663 ctx->device_name = optarg;
667 normalize_swapfs = 1;
674 fprintf(stderr, _("Byte-swapping filesystems "
675 "not compiled in this version "
685 if (show_version_only)
687 if (optind != argc - 1)
689 if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file &&
690 !cflag && !swapfs && !(ctx->options & E2F_OPT_COMPRESS_DIRS))
691 ctx->options |= E2F_OPT_READONLY;
692 ctx->filesystem_name = blkid_get_devname(ctx->blkid, argv[optind], 0);
693 if (!ctx->filesystem_name) {
694 com_err(ctx->program_name, 0, _("Unable to resolve '%s'"),
699 parse_extended_opts(ctx, extended_opts);
702 fd = open(ctx->filesystem_name, O_RDONLY, 0);
704 com_err("open", errno,
705 _("while opening %s for flushing"),
706 ctx->filesystem_name);
709 if ((retval = ext2fs_sync_device(fd, 1))) {
710 com_err("ext2fs_sync_device", retval,
711 _("while trying to flush %s"),
712 ctx->filesystem_name);
719 if (cflag || bad_blocks_file) {
720 fprintf(stderr, _("Incompatible options not "
721 "allowed when byte-swapping.\n"));
726 if (cflag && bad_blocks_file) {
727 fprintf(stderr, _("The -c and the -l/-L options may "
728 "not be both used at the same time.\n"));
733 * Set up signal action
735 memset(&sa, 0, sizeof(struct sigaction));
736 sa.sa_handler = signal_cancel;
737 sigaction(SIGINT, &sa, 0);
738 sigaction(SIGTERM, &sa, 0);
740 sa.sa_flags = SA_RESTART;
742 e2fsck_global_ctx = ctx;
743 sa.sa_handler = signal_progress_on;
744 sigaction(SIGUSR1, &sa, 0);
745 sa.sa_handler = signal_progress_off;
746 sigaction(SIGUSR2, &sa, 0);
749 /* Update our PATH to include /sbin if we need to run badblocks */
751 char *oldpath = getenv("PATH");
755 newpath = (char *) malloc(sizeof (PATH_SET) + 1 +
758 fatal_error(ctx, "Couldn't malloc() newpath");
759 strcpy (newpath, PATH_SET);
760 strcat (newpath, ":");
761 strcat (newpath, oldpath);
766 #ifdef CONFIG_JBD_DEBUG
767 if (getenv("E2FSCK_JBD_DEBUG"))
768 journal_enable_debug = atoi(getenv("E2FSCK_JBD_DEBUG"));
773 static const char *my_ver_string = E2FSPROGS_VERSION;
774 static const char *my_ver_date = E2FSPROGS_DATE;
776 int main (int argc, char *argv[])
778 errcode_t retval = 0;
779 int exit_value = FSCK_OK;
782 struct ext2_super_block *sb;
783 const char *lib_ver_date;
786 struct problem_context pctx;
787 int flags, run_result;
789 clear_problem_context(&pctx);
797 setlocale(LC_MESSAGES, "");
798 setlocale(LC_CTYPE, "");
799 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
800 textdomain(NLS_CAT_NAME);
802 my_ver = ext2fs_parse_version_string(my_ver_string);
803 lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
804 if (my_ver > lib_ver) {
805 fprintf( stderr, _("Error: ext2fs library version "
810 retval = PRS(argc, argv, &ctx);
812 com_err("e2fsck", retval,
813 _("while trying to initialize program"));
818 #ifdef RESOURCE_TRACK
819 init_resource_track(&ctx->global_rtrack);
822 if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
823 fprintf(stderr, "e2fsck %s (%s)\n", my_ver_string,
826 if (show_version_only) {
827 fprintf(stderr, _("\tUsing %s, %s\n"),
828 error_message(EXT2_ET_BASE), lib_ver_date);
834 if (!(ctx->options & E2F_OPT_PREEN) &&
835 !(ctx->options & E2F_OPT_NO) &&
836 !(ctx->options & E2F_OPT_YES)) {
837 if (!ctx->interactive)
839 _("need terminal for interactive repairs"));
841 ctx->superblock = ctx->use_superblock;
843 #ifdef CONFIG_TESTIO_DEBUG
844 io_ptr = test_io_manager;
845 test_io_backing_manager = unix_io_manager;
847 io_ptr = unix_io_manager;
850 if ((ctx->options & E2F_OPT_READONLY) == 0)
851 flags |= EXT2_FLAG_RW;
853 if (ctx->superblock && ctx->blocksize) {
854 retval = ext2fs_open(ctx->filesystem_name, flags,
855 ctx->superblock, ctx->blocksize,
857 } else if (ctx->superblock) {
859 for (blocksize = EXT2_MIN_BLOCK_SIZE;
860 blocksize <= EXT2_MAX_BLOCK_SIZE; blocksize *= 2) {
861 retval = ext2fs_open(ctx->filesystem_name, flags,
862 ctx->superblock, blocksize,
868 retval = ext2fs_open(ctx->filesystem_name, flags,
870 if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
871 !(ctx->flags & E2F_FLAG_SB_SPECIFIED) &&
872 ((retval == EXT2_ET_BAD_MAGIC) ||
873 ((retval == 0) && ext2fs_check_desc(fs)))) {
874 if (!fs || (fs->group_desc_count > 1)) {
875 printf(_("%s trying backup blocks...\n"),
876 retval ? _("Couldn't find ext2 superblock,") :
877 _("Group descriptors look bad..."));
878 get_backup_sb(ctx, fs, ctx->filesystem_name, io_ptr);
885 com_err(ctx->program_name, retval, _("while trying to open %s"),
886 ctx->filesystem_name);
887 if (retval == EXT2_ET_REV_TOO_HIGH) {
888 printf(_("The filesystem revision is apparently "
889 "too high for this version of e2fsck.\n"
890 "(Or the filesystem superblock "
892 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
893 } else if (retval == EXT2_ET_SHORT_READ)
894 printf(_("Could this be a zero-length partition?\n"));
895 else if ((retval == EPERM) || (retval == EACCES))
896 printf(_("You must have %s access to the "
897 "filesystem or be root\n"),
898 (ctx->options & E2F_OPT_READONLY) ?
900 else if (retval == ENXIO)
901 printf(_("Possibly non-existent or swap device?\n"));
903 else if (retval == EROFS)
904 printf(_("Disk write-protected; use the -n option "
905 "to do a read-only\n"
906 "check of the device.\n"));
909 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
915 if (sb->s_rev_level > E2FSCK_CURRENT_REV) {
916 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
917 _("while trying to open %s"),
918 ctx->filesystem_name);
920 fatal_error(ctx, _("Get a newer version of e2fsck!"));
924 * Set the device name, which is used whenever we print error
925 * or informational messages to the user.
927 if (ctx->device_name == 0 &&
928 (sb->s_volume_name[0] != 0)) {
929 ctx->device_name = string_copy(ctx, sb->s_volume_name,
930 sizeof(sb->s_volume_name));
932 if (ctx->device_name == 0)
933 ctx->device_name = ctx->filesystem_name;
936 * Make sure the ext3 superblock fields are consistent.
938 retval = e2fsck_check_ext3_journal(ctx);
940 com_err(ctx->program_name, retval,
941 _("while checking ext3 journal for %s"),
947 * Check to see if we need to do ext3-style recovery. If so,
948 * do it, and then restart the fsck.
950 if (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) {
951 if (ctx->options & E2F_OPT_READONLY) {
952 printf(_("Warning: skipping journal recovery "
953 "because doing a read-only filesystem "
955 io_channel_flush(ctx->fs->io);
957 if (ctx->flags & E2F_FLAG_RESTARTED) {
959 * Whoops, we attempted to run the
960 * journal twice. This should never
961 * happen, unless the hardware or
962 * device driver is being bogus.
964 com_err(ctx->program_name, 0,
965 _("unable to set superblock flags on %s\n"), ctx->device_name);
968 retval = e2fsck_run_ext3_journal(ctx);
970 com_err(ctx->program_name, retval,
971 _("while recovering ext3 journal of %s"),
975 ext2fs_close(ctx->fs);
977 ctx->flags |= E2F_FLAG_RESTARTED;
983 * Check for compatibility with the feature sets. We need to
984 * be more stringent than ext2fs_open().
986 if ((sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
987 (sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) {
988 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
989 "(%s)", ctx->device_name);
992 if (sb->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
993 com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE,
994 "(%s)", ctx->device_name);
997 #ifdef ENABLE_COMPRESSION
998 if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION)
999 com_err(ctx->program_name, 0,
1000 _("Warning: compression support is experimental.\n"));
1002 #ifndef ENABLE_HTREE
1003 if (sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) {
1004 com_err(ctx->program_name, 0,
1005 _("E2fsck not compiled with HTREE support,\n\t"
1006 "but filesystem %s has HTREE directories.\n"),
1013 * If the user specified a specific superblock, presumably the
1014 * master superblock has been trashed. So we mark the
1015 * superblock as dirty, so it can be written out.
1017 if (ctx->superblock &&
1018 !(ctx->options & E2F_OPT_READONLY))
1019 ext2fs_mark_super_dirty(fs);
1022 * We only update the master superblock because (a) paranoia;
1023 * we don't want to corrupt the backup superblocks, and (b) we
1024 * don't need to update the mount count and last checked
1025 * fields in the backup superblock (the kernel doesn't
1026 * update the backup superblocks anyway).
1028 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1030 ehandler_init(fs->io);
1032 if (ctx->superblock)
1033 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
1034 check_super_block(ctx);
1035 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1036 fatal_error(ctx, 0);
1038 if (bad_blocks_file)
1039 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
1041 read_bad_blocks_file(ctx, 0, !keep_bad_blocks); /* Test disk */
1042 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1043 fatal_error(ctx, 0);
1044 #ifdef ENABLE_SWAPFS
1045 if (normalize_swapfs) {
1046 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ==
1047 ext2fs_native_flag()) {
1048 fprintf(stderr, _("%s: Filesystem byte order "
1049 "already normalized.\n"), ctx->device_name);
1050 fatal_error(ctx, 0);
1055 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1056 fatal_error(ctx, 0);
1061 * Mark the system as valid, 'til proven otherwise
1063 ext2fs_mark_valid(fs);
1065 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
1067 com_err(ctx->program_name, retval,
1068 _("while reading bad blocks inode"));
1070 printf(_("This doesn't bode well,"
1071 " but we'll try to go on...\n"));
1074 run_result = e2fsck_run(ctx);
1075 e2fsck_clear_progbar(ctx);
1076 if (run_result == E2F_FLAG_RESTART) {
1077 printf(_("Restarting e2fsck from the beginning...\n"));
1078 retval = e2fsck_reset_context(ctx);
1080 com_err(ctx->program_name, retval,
1081 _("while resetting context"));
1082 fatal_error(ctx, 0);
1087 if (run_result & E2F_FLAG_CANCEL) {
1088 printf(_("%s: e2fsck canceled.\n"), ctx->device_name ?
1089 ctx->device_name : ctx->filesystem_name);
1090 exit_value |= FSCK_CANCELED;
1092 if (run_result & E2F_FLAG_ABORT)
1093 fatal_error(ctx, _("aborted"));
1096 mtrace_print("Cleanup");
1098 if (ext2fs_test_changed(fs)) {
1099 exit_value |= FSCK_NONDESTRUCT;
1100 if (!(ctx->options & E2F_OPT_PREEN))
1101 printf(_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
1103 if (ctx->mount_flags & EXT2_MF_ISROOT) {
1104 printf(_("%s: ***** REBOOT LINUX *****\n"),
1106 exit_value |= FSCK_REBOOT;
1109 if (!ext2fs_test_valid(fs)) {
1110 printf(_("\n%s: ********** WARNING: Filesystem still has "
1111 "errors **********\n\n"), ctx->device_name);
1112 exit_value |= FSCK_UNCORRECTED;
1113 exit_value &= ~FSCK_NONDESTRUCT;
1115 if (exit_value & FSCK_CANCELED)
1116 exit_value &= ~FSCK_NONDESTRUCT;
1119 if (!(ctx->options & E2F_OPT_READONLY)) {
1120 if (ext2fs_test_valid(fs)) {
1121 if (!(sb->s_state & EXT2_VALID_FS))
1122 exit_value |= FSCK_NONDESTRUCT;
1123 sb->s_state = EXT2_VALID_FS;
1125 sb->s_state &= ~EXT2_VALID_FS;
1126 sb->s_mnt_count = 0;
1127 sb->s_lastcheck = time(NULL);
1128 ext2fs_mark_super_dirty(fs);
1132 e2fsck_write_bitmaps(ctx);
1136 free(ctx->filesystem_name);
1137 free(ctx->journal_name);
1138 e2fsck_free_context(ctx);
1140 #ifdef RESOURCE_TRACK
1141 if (ctx->options & E2F_OPT_TIME)
1142 print_resource_track(NULL, &ctx->global_rtrack);