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 #include <sys/ioctl.h>
41 #include "et/com_err.h"
44 #include "../version.h"
46 /* Command line options */
47 static int swapfs = 0;
48 static int normalize_swapfs = 0;
49 static int cflag = 0; /* check disk */
50 static int show_version_only = 0;
51 static int verbose = 0;
53 static int replace_bad_blocks = 0;
54 static char *bad_blocks_file = 0;
56 e2fsck_t e2fsck_global_ctx; /* Try your very best not to use this! */
58 #ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jfs-debug */
59 int journal_enable_debug = -1;
62 static void usage(e2fsck_t ctx)
65 _("Usage: %s [-panyrcdfvstDFSV] [-b superblock] [-B blocksize]\n"
66 "\t\t[-I inode_buffer_blocks] [-P process_inode_size]\n"
67 "\t\t[-l|-L bad_blocks_file] [-C fd] [-j ext-journal]\n"
68 "\t\t[-E extended-options] device\n"),
71 fprintf(stderr, _("\nEmergency help:\n"
72 " -p Automatic repair (no questions)\n"
73 " -n Make no changes to the filesystem\n"
74 " -y Assume \"yes\" to all questions\n"
75 " -c Check for bad blocks and add them to the badblock list\n"
76 " -f Force checking even if filesystem is marked clean\n"));
79 " -b superblock Use alternative superblock\n"
80 " -B blocksize Force blocksize when looking for superblock\n"
81 " -j external-journal Set location of the external journal\n"
82 " -l bad_blocks_file Add to badblocks list\n"
83 " -L bad_blocks_file Set badblocks list\n"
89 static void show_stats(e2fsck_t ctx)
91 ext2_filsys fs = ctx->fs;
92 int inodes, inodes_used, blocks, blocks_used;
94 int num_files, num_links;
97 dir_links = 2 * ctx->fs_directory_count - 1;
98 num_files = ctx->fs_total_count - dir_links;
99 num_links = ctx->fs_links_count - dir_links;
100 inodes = fs->super->s_inodes_count;
101 inodes_used = (fs->super->s_inodes_count -
102 fs->super->s_free_inodes_count);
103 blocks = fs->super->s_blocks_count;
104 blocks_used = (fs->super->s_blocks_count -
105 fs->super->s_free_blocks_count);
107 frag_percent = (10000 * ctx->fs_fragmented) / inodes_used;
108 frag_percent = (frag_percent + 5) / 10;
111 printf(_("%s: %d/%d files (%0d.%d%% non-contiguous), %d/%d blocks\n"),
112 ctx->device_name, inodes_used, inodes,
113 frag_percent / 10, frag_percent % 10,
114 blocks_used, blocks);
118 * This is a bit ugly. But I think there will nearly always be more
119 * than one "thing" to report about, so I won't try writing complex
120 * code to handle one/two/many forms of all words.
121 * Some languages (Italian, at least) never uses the plural form
122 * of foreign words, so in real life this could not be a problem.
123 * md@linux.it - 2000-1-15
126 printf (_("\n%8d inodes used (%d%%)\n"), inodes_used,
127 100 * inodes_used / inodes);
128 printf (_("%8d non-contiguous inodes (%0d.%d%%)\n"),
129 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
130 printf (_(" # of inodes with ind/dind/tind blocks: %d/%d/%d\n"),
131 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
132 printf (_("%8d blocks used (%d%%)\n"
133 "%8d bad blocks\n"), blocks_used,
134 (int) ((long long) 100 * blocks_used / blocks),
135 ctx->fs_badblocks_count);
136 printf(_("%8d large files\n"), ctx->large_files);
137 printf (_("\n%8d regular files\n"
139 "%8d character device files\n"
140 "%8d block device files\n"
143 "%8d symbolic links (%d fast symbolic links)\n"
147 ctx->fs_regular_count,
148 ctx->fs_directory_count,
149 ctx->fs_chardev_count,
150 ctx->fs_blockdev_count,
152 ctx->fs_links_count - dir_links,
153 ctx->fs_symlinks_count,
154 ctx->fs_fast_symlinks_count,
155 ctx->fs_sockets_count,
156 ctx->fs_total_count - dir_links);
158 printf ("\n%8d inode%s used (%d%%)\n", inodes_used,
159 (inodes_used != 1) ? "s" : "",
160 100 * inodes_used / inodes);
161 printf ("%8d non-contiguous inodes (%0d.%d%%)\n",
162 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
163 printf (" # of inodes with ind/dind/tind blocks: %d/%d/%d\n",
164 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
165 printf ("%8d block%s used (%d%%)\n"
166 "%8d bad block%s\n", blocks_used,
167 (blocks_used != 1) ? "s" : "",
168 100 * blocks_used / blocks, ctx->fs_badblocks_count,
169 ctx->fs_badblocks_count != 1 ? "s" : "");
170 printf(_("%8d large file%s\n"), ctx->large_files,
171 (ctx->large_files != 1) ? "s" : "");
172 printf ("\n%8d regular file%s\n"
174 "%8d character device file%s\n"
175 "%8d block device file%s\n"
178 "%8d symbolic link%s (%d fast symbolic link%s)\n"
182 ctx->fs_regular_count,
183 (ctx->fs_regular_count != 1) ? "s" : "",
184 ctx->fs_directory_count,
185 (ctx->fs_directory_count != 1) ? "ies" : "y",
186 ctx->fs_chardev_count,
187 (ctx->fs_chardev_count != 1) ? "s" : "",
188 ctx->fs_blockdev_count,
189 (ctx->fs_blockdev_count != 1) ? "s" : "",
191 (ctx->fs_fifo_count != 1) ? "s" : "",
192 ctx->fs_links_count - dir_links,
193 ((ctx->fs_links_count - dir_links) != 1) ? "s" : "",
194 ctx->fs_symlinks_count,
195 (ctx->fs_symlinks_count != 1) ? "s" : "",
196 ctx->fs_fast_symlinks_count,
197 (ctx->fs_fast_symlinks_count != 1) ? "s" : "",
198 ctx->fs_sockets_count, (ctx->fs_sockets_count != 1) ? "s" : "",
199 ctx->fs_total_count - dir_links,
200 ((ctx->fs_total_count - dir_links) != 1) ? "s" : "");
204 static void check_mount(e2fsck_t ctx)
209 retval = ext2fs_check_if_mounted(ctx->filesystem_name,
212 com_err("ext2fs_check_if_mount", retval,
213 _("while determining whether %s is mounted."),
214 ctx->filesystem_name);
219 * If the filesystem isn't mounted, or it's the root filesystem
220 * and it's mounted read-only, then everything's fine.
222 if ((!(ctx->mount_flags & EXT2_MF_MOUNTED)) ||
223 ((ctx->mount_flags & EXT2_MF_ISROOT) &&
224 (ctx->mount_flags & EXT2_MF_READONLY)))
227 if (ctx->options & E2F_OPT_READONLY) {
228 printf(_("Warning! %s is mounted.\n"), ctx->filesystem_name);
232 printf(_("%s is mounted. "), ctx->filesystem_name);
233 if (!isatty(0) || !isatty(1))
234 fatal_error(ctx, _("Cannot continue, aborting.\n\n"));
235 printf(_("\n\n\007\007\007\007WARNING!!! "
236 "Running e2fsck on a mounted filesystem may cause\n"
237 "SEVERE filesystem damage.\007\007\007\n\n"));
238 cont = ask_yn(_("Do you really want to continue"), -1);
240 printf (_("check aborted.\n"));
247 * This routine checks to see if a filesystem can be skipped; if so,
248 * it will exit with E2FSCK_OK. Under some conditions it will print a
249 * message explaining why a check is being forced.
251 static void check_if_skip(e2fsck_t ctx)
253 ext2_filsys fs = ctx->fs;
254 const char *reason = NULL;
255 unsigned int reason_arg = 0;
257 if ((ctx->options & E2F_OPT_FORCE) || bad_blocks_file ||
261 if (fs->super->s_state & EXT2_ERROR_FS)
262 reason = _(" contains a file system with errors");
263 else if ((fs->super->s_state & EXT2_VALID_FS) == 0)
264 reason = _(" was not cleanly unmounted");
265 else if ((fs->super->s_max_mnt_count > 0) &&
266 (fs->super->s_mnt_count >=
267 (unsigned) fs->super->s_max_mnt_count)) {
268 reason = _(" has been mounted %u times without being checked");
269 reason_arg = fs->super->s_mnt_count;
270 } else if (fs->super->s_checkinterval &&
271 time(0) >= (fs->super->s_lastcheck +
272 fs->super->s_checkinterval)) {
273 reason = _(" has gone %u days without being checked");
274 reason_arg = (time(0) - fs->super->s_lastcheck)/(3600*24);
277 fputs(ctx->device_name, stdout);
278 printf(reason, reason_arg);
279 fputs(_(", check forced.\n"), stdout);
282 printf(_("%s: clean, %d/%d files, %d/%d blocks\n"), ctx->device_name,
283 fs->super->s_inodes_count - fs->super->s_free_inodes_count,
284 fs->super->s_inodes_count,
285 fs->super->s_blocks_count - fs->super->s_free_blocks_count,
286 fs->super->s_blocks_count);
289 e2fsck_free_context(ctx);
294 * For completion notice
300 struct percent_tbl e2fsck_tbl = {
301 5, { 0, 70, 90, 92, 95, 100 }
304 "==============================================================="
305 "===============================================================";
306 static char spaces[] =
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\r", spaces + (sizeof(spaces) - 80));
330 ctx->flags &= ~E2F_FLAG_PROG_BAR;
333 int e2fsck_simple_progress(e2fsck_t ctx, char *label, float percent,
336 static const char spinner[] = "\\|/-";
343 if (ctx->flags & E2F_FLAG_PROG_SUPPRESS)
347 * Calculate the new progress position. If the
348 * percentage hasn't changed, then we skip out right
351 if (ctx->progress_last_percent == (int) 10 * percent)
353 ctx->progress_last_percent = (int) 10 * percent;
356 * If we've already updated the spinner once within
357 * the last 1/8th of a second, no point doing it
360 gettimeofday(&tv, NULL);
361 tick = (tv.tv_sec << 3) + (tv.tv_usec / (1000000 / 8));
362 if ((tick == ctx->progress_last_time) &&
363 (percent != 0.0) && (percent != 100.0))
365 ctx->progress_last_time = tick;
368 * Advance the spinner, and note that the progress bar
369 * will be on the screen
371 ctx->progress_pos = (ctx->progress_pos+1) & 3;
372 ctx->flags |= E2F_FLAG_PROG_BAR;
374 dpywidth = 66 - strlen(label);
375 dpywidth = 8 * (dpywidth / 8);
379 i = ((percent * dpywidth) + 50) / 100;
380 printf("%s: |%s%s", label, bar + (sizeof(bar) - (i+1)),
381 spaces + (sizeof(spaces) - (dpywidth - i + 1)));
382 if (percent == 100.0)
385 fputc(spinner[ctx->progress_pos & 3], stdout);
387 printf(" %4.1f%% %u\r", percent, dpynum);
389 printf(" %4.1f%% \r", percent);
391 if (percent == 100.0)
392 e2fsck_clear_progbar(ctx);
398 static int e2fsck_update_progress(e2fsck_t ctx, int pass,
399 unsigned long cur, unsigned long max)
401 static const char spinner[] = "\\|/-";
407 static int dpywidth = 0;
412 if (ctx->progress_fd) {
413 sprintf(buf, "%d %lu %lu\n", pass, cur, max);
414 write(ctx->progress_fd, buf, strlen(buf));
416 percent = calc_percent(&e2fsck_tbl, pass, cur, max);
417 e2fsck_simple_progress(ctx, ctx->device_name,
423 #define PATH_SET "PATH=/sbin"
425 static void reserve_stdio_fds(void)
430 fd = open("/dev/null", O_RDWR);
434 fprintf(stderr, _("ERROR: Couldn't open "
444 static void signal_progress_on(int sig)
446 e2fsck_t ctx = e2fsck_global_ctx;
451 ctx->progress = e2fsck_update_progress;
452 ctx->progress_fd = 0;
455 static void signal_progress_off(int sig)
457 e2fsck_t ctx = e2fsck_global_ctx;
462 e2fsck_clear_progbar(ctx);
466 static void signal_cancel(int sig)
468 e2fsck_t ctx = e2fsck_global_ctx;
473 ctx->flags |= E2F_FLAG_CANCEL;
477 static void parse_extended_opts(e2fsck_t ctx, const char *opts)
479 char *buf, *token, *next, *p, *arg;
481 int extended_usage = 0;
483 buf = string_copy(ctx, opts, 0);
484 for (token = buf; token && *token; token = next) {
485 p = strchr(token, ',');
491 arg = strchr(token, '=');
496 if (strcmp(token, "ea_ver") == 0) {
501 ea_ver = strtoul(arg, &p, 0);
503 ((ea_ver != 1) && (ea_ver != 2))) {
505 _("Invalid EA version.\n"));
509 ctx->ext_attr_ver = ea_ver;
513 if (extended_usage) {
514 fprintf(stderr, _("Extended options are separated by commas, "
515 "and may take an argument which\n"
516 "is set off by an equals ('=') sign. "
517 "Valid raid options are:\n"
518 "\tea_ver=<ea_version (1 or 2)\n\n"));
524 static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
529 extern void *mallwatch;
536 char *extended_opts = 0;
538 retval = e2fsck_allocate_context(&ctx);
544 setbuf(stdout, NULL);
545 setbuf(stderr, NULL);
546 initialize_ext2_error_table();
547 blkid_get_cache(&ctx->blkid, NULL);
550 ctx->program_name = *argv;
552 ctx->program_name = "e2fsck";
553 while ((c = getopt (argc, argv, "panyrcC:B:dE:fvtFVM:b:I:j:P:l:L:N:SsD")) != EOF)
556 ctx->progress = e2fsck_update_progress;
557 ctx->progress_fd = atoi(optarg);
558 if (!ctx->progress_fd)
560 /* Validate the file descriptor to avoid disasters */
561 fd = dup(ctx->progress_fd);
564 _("Error validating file descriptor %d: %s\n"),
566 error_message(errno));
568 _("Invalid completion information file descriptor"));
573 ctx->options |= E2F_OPT_COMPRESS_DIRS;
576 extended_opts = optarg;
580 if (ctx->options & (E2F_OPT_YES|E2F_OPT_NO)) {
583 _("Only one the options -p/-a, -n or -y may be specified."));
585 ctx->options |= E2F_OPT_PREEN;
588 if (ctx->options & (E2F_OPT_YES|E2F_OPT_PREEN))
590 ctx->options |= E2F_OPT_NO;
593 if (ctx->options & (E2F_OPT_PREEN|E2F_OPT_NO))
595 ctx->options |= E2F_OPT_YES;
598 #ifdef RESOURCE_TRACK
599 if (ctx->options & E2F_OPT_TIME)
600 ctx->options |= E2F_OPT_TIME2;
602 ctx->options |= E2F_OPT_TIME;
604 fprintf(stderr, _("The -t option is not "
605 "supported on this version of e2fsck.\n"));
610 ctx->options |= E2F_OPT_WRITECHECK;
611 ctx->options |= E2F_OPT_CHECKBLOCKS;
614 /* What we do by default, anyway! */
617 ctx->use_superblock = atoi(optarg);
618 ctx->flags |= E2F_FLAG_SB_SPECIFIED;
621 ctx->blocksize = atoi(optarg);
624 ctx->inode_buffer_blocks = atoi(optarg);
627 ctx->journal_name = string_copy(ctx, optarg, 0);
630 ctx->process_inode_size = atoi(optarg);
633 replace_bad_blocks++;
635 bad_blocks_file = string_copy(ctx, optarg, 0);
638 ctx->options |= E2F_OPT_DEBUG;
641 ctx->options |= E2F_OPT_FORCE;
650 show_version_only = 1;
654 mallwatch = (void *) strtol(optarg, NULL, 0);
658 ctx->device_name = optarg;
662 normalize_swapfs = 1;
669 fprintf(stderr, _("Byte-swapping filesystems "
670 "not compiled in this version "
677 if (show_version_only)
679 if (optind != argc - 1)
681 if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file &&
682 !cflag && !swapfs && !(ctx->options & E2F_OPT_COMPRESS_DIRS))
683 ctx->options |= E2F_OPT_READONLY;
684 ctx->filesystem_name = blkid_get_devname(ctx->blkid, argv[optind], 0);
686 parse_extended_opts(ctx, extended_opts);
689 fd = open(ctx->filesystem_name, O_RDONLY, 0);
691 com_err("open", errno,
692 _("while opening %s for flushing"),
693 ctx->filesystem_name);
696 if ((retval = ext2fs_sync_device(fd, 1))) {
697 com_err("ext2fs_sync_device", retval,
698 _("while trying to flush %s"),
699 ctx->filesystem_name);
706 if (cflag || bad_blocks_file) {
707 fprintf(stderr, _("Incompatible options not "
708 "allowed when byte-swapping.\n"));
713 if (cflag && bad_blocks_file) {
714 fprintf(stderr, _("The -c and the -l/-L options may "
715 "not be both used at the same time.\n"));
720 * Set up signal action
722 memset(&sa, 0, sizeof(struct sigaction));
723 sa.sa_handler = signal_cancel;
724 sigaction(SIGINT, &sa, 0);
725 sigaction(SIGTERM, &sa, 0);
727 sa.sa_flags = SA_RESTART;
729 e2fsck_global_ctx = ctx;
730 sa.sa_handler = signal_progress_on;
731 sigaction(SIGUSR1, &sa, 0);
732 sa.sa_handler = signal_progress_off;
733 sigaction(SIGUSR2, &sa, 0);
736 /* Update our PATH to include /sbin if we need to run badblocks */
738 char *oldpath = getenv("PATH");
742 newpath = (char *) malloc(sizeof (PATH_SET) + 1 +
745 fatal_error(ctx, "Couldn't malloc() newpath");
746 strcpy (newpath, PATH_SET);
747 strcat (newpath, ":");
748 strcat (newpath, oldpath);
753 #ifdef CONFIG_JBD_DEBUG
754 if (getenv("E2FSCK_JBD_DEBUG"))
755 journal_enable_debug = atoi(getenv("E2FSCK_JBD_DEBUG"));
760 static const char *my_ver_string = E2FSPROGS_VERSION;
761 static const char *my_ver_date = E2FSPROGS_DATE;
763 int main (int argc, char *argv[])
765 errcode_t retval = 0;
766 int exit_value = FSCK_OK;
769 struct ext2_super_block *sb;
770 const char *lib_ver_date;
773 struct problem_context pctx;
774 int flags, run_result;
776 clear_problem_context(&pctx);
784 setlocale(LC_MESSAGES, "");
785 setlocale(LC_CTYPE, "");
786 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
787 textdomain(NLS_CAT_NAME);
789 my_ver = ext2fs_parse_version_string(my_ver_string);
790 lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
791 if (my_ver > lib_ver) {
792 fprintf( stderr, _("Error: ext2fs library version "
797 retval = PRS(argc, argv, &ctx);
799 com_err("e2fsck", retval,
800 _("while trying to initialize program"));
805 #ifdef RESOURCE_TRACK
806 init_resource_track(&ctx->global_rtrack);
809 if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
810 fprintf (stderr, "e2fsck %s (%s)\n", my_ver_string,
813 if (show_version_only) {
814 fprintf(stderr, _("\tUsing %s, %s\n"),
815 error_message(EXT2_ET_BASE), lib_ver_date);
821 if (!(ctx->options & E2F_OPT_PREEN) &&
822 !(ctx->options & E2F_OPT_NO) &&
823 !(ctx->options & E2F_OPT_YES)) {
824 if (!isatty (0) || !isatty (1))
826 _("need terminal for interactive repairs"));
828 ctx->superblock = ctx->use_superblock;
831 io_ptr = unix_io_manager;
833 io_ptr = test_io_manager;
834 test_io_backing_manager = unix_io_manager;
837 if ((ctx->options & E2F_OPT_READONLY) == 0)
838 flags |= EXT2_FLAG_RW;
840 if (ctx->superblock && ctx->blocksize) {
841 retval = ext2fs_open(ctx->filesystem_name, flags,
842 ctx->superblock, ctx->blocksize,
844 } else if (ctx->superblock) {
846 for (blocksize = EXT2_MIN_BLOCK_SIZE;
847 blocksize <= EXT2_MAX_BLOCK_SIZE; blocksize *= 2) {
848 retval = ext2fs_open(ctx->filesystem_name, flags,
849 ctx->superblock, blocksize,
855 retval = ext2fs_open(ctx->filesystem_name, flags,
857 if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
858 !(ctx->flags & E2F_FLAG_SB_SPECIFIED) &&
859 ((retval == EXT2_ET_BAD_MAGIC) ||
860 ((retval == 0) && ext2fs_check_desc(fs)))) {
861 if (!fs || (fs->group_desc_count > 1)) {
862 printf(_("%s trying backup blocks...\n"),
863 retval ? _("Couldn't find ext2 superblock,") :
864 _("Group descriptors look bad..."));
865 get_backup_sb(ctx, fs, ctx->filesystem_name, io_ptr);
872 com_err(ctx->program_name, retval, _("while trying to open %s"),
873 ctx->filesystem_name);
874 if (retval == EXT2_ET_REV_TOO_HIGH) {
875 printf(_("The filesystem revision is apparently "
876 "too high for this version of e2fsck.\n"
877 "(Or the filesystem superblock "
879 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
880 } else if (retval == EXT2_ET_SHORT_READ)
881 printf(_("Could this be a zero-length partition?\n"));
882 else if ((retval == EPERM) || (retval == EACCES))
883 printf(_("You must have %s access to the "
884 "filesystem or be root\n"),
885 (ctx->options & E2F_OPT_READONLY) ?
887 else if (retval == ENXIO)
888 printf(_("Possibly non-existent or swap device?\n"));
890 else if (retval == EROFS)
891 printf(_("Disk write-protected; use the -n option "
892 "to do a read-only\n"
893 "check of the device.\n"));
896 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
902 if (sb->s_rev_level > E2FSCK_CURRENT_REV) {
903 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
904 _("while trying to open %s"),
905 ctx->filesystem_name);
907 fatal_error(ctx, _("Get a newer version of e2fsck!"));
911 * Set the device name, which is used whenever we print error
912 * or informational messages to the user.
914 if (ctx->device_name == 0 &&
915 (sb->s_volume_name[0] != 0)) {
916 ctx->device_name = string_copy(ctx, sb->s_volume_name,
917 sizeof(sb->s_volume_name));
919 if (ctx->device_name == 0)
920 ctx->device_name = ctx->filesystem_name;
923 * Make sure the ext3 superblock fields are consistent.
925 retval = e2fsck_check_ext3_journal(ctx);
927 com_err(ctx->program_name, retval,
928 _("while checking ext3 journal for %s"),
934 * Check to see if we need to do ext3-style recovery. If so,
935 * do it, and then restart the fsck.
937 if (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) {
938 if (ctx->options & E2F_OPT_READONLY) {
939 printf(_("Warning: skipping journal recovery "
940 "because doing a read-only filesystem "
942 io_channel_flush(ctx->fs->io);
944 if (ctx->flags & E2F_FLAG_RESTARTED) {
946 * Whoops, we attempted to run the
947 * journal twice. This should never
948 * happen, unless the hardware or
949 * device driver is being bogus.
951 com_err(ctx->program_name, 0,
952 _("unable to set superblock flags on %s\n"), ctx->device_name);
955 retval = e2fsck_run_ext3_journal(ctx);
957 com_err(ctx->program_name, retval,
958 _("while recovering ext3 journal of %s"),
962 ext2fs_close(ctx->fs);
964 ctx->flags |= E2F_FLAG_RESTARTED;
970 * Check for compatibility with the feature sets. We need to
971 * be more stringent than ext2fs_open().
973 if ((sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
974 (sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) {
975 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
976 "(%s)", ctx->device_name);
979 if (sb->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
980 com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE,
981 "(%s)", ctx->device_name);
984 #ifdef ENABLE_COMPRESSION
985 if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION)
986 com_err(ctx->program_name, 0,
987 _("Warning: compression support is experimental.\n"));
990 if (sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) {
991 com_err(ctx->program_name, 0,
992 _("E2fsck not compiled with HTREE support,\n\t"
993 "but filesystem %s has HTREE directories.\n"),
1000 * If the user specified a specific superblock, presumably the
1001 * master superblock has been trashed. So we mark the
1002 * superblock as dirty, so it can be written out.
1004 if (ctx->superblock &&
1005 !(ctx->options & E2F_OPT_READONLY))
1006 ext2fs_mark_super_dirty(fs);
1009 * We only update the master superblock because (a) paranoia;
1010 * we don't want to corrupt the backup superblocks, and (b) we
1011 * don't need to update the mount count and last checked
1012 * fields in the backup superblock (the kernel doesn't
1013 * update the backup superblocks anyway).
1015 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1017 ehandler_init(fs->io);
1019 if (ctx->superblock)
1020 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
1021 check_super_block(ctx);
1022 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1023 fatal_error(ctx, 0);
1025 if (bad_blocks_file)
1026 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
1029 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1030 fatal_error(ctx, 0);
1031 #ifdef ENABLE_SWAPFS
1032 if (normalize_swapfs) {
1033 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ==
1034 ext2fs_native_flag()) {
1035 fprintf(stderr, _("%s: Filesystem byte order "
1036 "already normalized.\n"), ctx->device_name);
1037 fatal_error(ctx, 0);
1042 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1043 fatal_error(ctx, 0);
1048 * Mark the system as valid, 'til proven otherwise
1050 ext2fs_mark_valid(fs);
1052 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
1054 com_err(ctx->program_name, retval,
1055 _("while reading bad blocks inode"));
1057 printf(_("This doesn't bode well,"
1058 " but we'll try to go on...\n"));
1061 run_result = e2fsck_run(ctx);
1062 e2fsck_clear_progbar(ctx);
1063 if (run_result == E2F_FLAG_RESTART) {
1064 printf(_("Restarting e2fsck from the beginning...\n"));
1065 retval = e2fsck_reset_context(ctx);
1067 com_err(ctx->program_name, retval,
1068 _("while resetting context"));
1069 fatal_error(ctx, 0);
1074 if (run_result & E2F_FLAG_CANCEL) {
1075 printf(_("%s: e2fsck canceled.\n"), ctx->device_name ?
1076 ctx->device_name : ctx->filesystem_name);
1077 exit_value |= FSCK_CANCELED;
1079 if (run_result & E2F_FLAG_ABORT)
1080 fatal_error(ctx, _("aborted"));
1083 mtrace_print("Cleanup");
1085 if (ext2fs_test_changed(fs)) {
1086 exit_value |= FSCK_NONDESTRUCT;
1087 if (!(ctx->options & E2F_OPT_PREEN))
1088 printf(_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
1090 if (ctx->mount_flags & EXT2_MF_ISROOT) {
1091 printf(_("%s: ***** REBOOT LINUX *****\n"),
1093 exit_value |= FSCK_REBOOT;
1096 if (!ext2fs_test_valid(fs)) {
1097 printf(_("\n%s: ********** WARNING: Filesystem still has "
1098 "errors **********\n\n"), ctx->device_name);
1099 exit_value |= FSCK_UNCORRECTED;
1100 exit_value &= ~FSCK_NONDESTRUCT;
1102 if (exit_value & FSCK_CANCELED)
1103 exit_value &= ~FSCK_NONDESTRUCT;
1106 if (!(ctx->options & E2F_OPT_READONLY)) {
1107 if (ext2fs_test_valid(fs)) {
1108 if (!(sb->s_state & EXT2_VALID_FS))
1109 exit_value |= FSCK_NONDESTRUCT;
1110 sb->s_state = EXT2_VALID_FS;
1112 sb->s_state &= ~EXT2_VALID_FS;
1113 sb->s_mnt_count = 0;
1114 sb->s_lastcheck = time(NULL);
1115 ext2fs_mark_super_dirty(fs);
1119 e2fsck_write_bitmaps(ctx);
1123 free(ctx->filesystem_name);
1124 free(ctx->journal_name);
1125 e2fsck_free_context(ctx);
1127 #ifdef RESOURCE_TRACK
1128 if (ctx->options & E2F_OPT_TIME)
1129 print_resource_track(NULL, &ctx->global_rtrack);