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>
42 #ifdef HAVE_SYS_TYPES_H
43 #include <sys/types.h>
49 #include "et/com_err.h"
52 #include "../version.h"
54 /* Command line options */
56 static int normalize_swapfs;
57 static int cflag; /* check disk */
58 static int show_version_only;
61 static int replace_bad_blocks;
62 static int keep_bad_blocks;
63 static char *bad_blocks_file;
65 e2fsck_t e2fsck_global_ctx; /* Try your very best not to use this! */
67 #ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jfs-debug */
68 int journal_enable_debug = -1;
71 static void usage(e2fsck_t ctx)
74 _("Usage: %s [-panyrcdfvstDFSV] [-b superblock] [-B blocksize]\n"
75 "\t\t[-I inode_buffer_blocks] [-P process_inode_size]\n"
76 "\t\t[-l|-L bad_blocks_file] [-C fd] [-j ext-journal]\n"
77 "\t\t[-E extended-options] device\n"),
80 fprintf(stderr, _("\nEmergency help:\n"
81 " -p Automatic repair (no questions)\n"
82 " -n Make no changes to the filesystem\n"
83 " -y Assume \"yes\" to all questions\n"
84 " -c Check for bad blocks and add them to the badblock list\n"
85 " -f Force checking even if filesystem is marked clean\n"));
88 " -b superblock Use alternative superblock\n"
89 " -B blocksize Force blocksize when looking for superblock\n"
90 " -j external-journal Set location of the external journal\n"
91 " -l bad_blocks_file Add to badblocks list\n"
92 " -L bad_blocks_file Set badblocks list\n"
98 static void show_stats(e2fsck_t ctx)
100 ext2_filsys fs = ctx->fs;
101 int inodes, inodes_used, blocks, blocks_used;
103 int num_files, num_links;
106 dir_links = 2 * ctx->fs_directory_count - 1;
107 num_files = ctx->fs_total_count - dir_links;
108 num_links = ctx->fs_links_count - dir_links;
109 inodes = fs->super->s_inodes_count;
110 inodes_used = (fs->super->s_inodes_count -
111 fs->super->s_free_inodes_count);
112 blocks = fs->super->s_blocks_count;
113 blocks_used = (fs->super->s_blocks_count -
114 fs->super->s_free_blocks_count);
116 frag_percent = (10000 * ctx->fs_fragmented) / inodes_used;
117 frag_percent = (frag_percent + 5) / 10;
120 printf(_("%s: %d/%d files (%0d.%d%% non-contiguous), %d/%d blocks\n"),
121 ctx->device_name, inodes_used, inodes,
122 frag_percent / 10, frag_percent % 10,
123 blocks_used, blocks);
126 printf (P_("\n%8d inode used (%d%%)\n", "\n%8d inodes used (%d%%)\n",
127 inodes_used), inodes_used, 100 * inodes_used / inodes);
128 printf (P_("%8d non-contiguous inode (%0d.%d%%)\n",
129 "%8d non-contiguous inodes (%0d.%d%%)\n",
131 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
132 printf (_(" # of inodes with ind/dind/tind blocks: %d/%d/%d\n"),
133 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
134 printf (P_("%8d block used (%d%%)\n", "%8d blocks used (%d%%)\n",
136 blocks_used, (int) ((long long) 100 * blocks_used / blocks));
137 printf (P_("%8d bad block\n", "%8d bad blocks\n",
138 ctx->fs_badblocks_count), ctx->fs_badblocks_count);
139 printf (P_("%8d large file\n", "%8d large files\n",
140 ctx->large_files), ctx->large_files);
141 printf (P_("\n%8d regular file\n", "\n%8d regular files\n",
142 ctx->fs_regular_count), ctx->fs_regular_count);
143 printf (P_("%8d directory\n", "%8d directories\n",
144 ctx->fs_directory_count), ctx->fs_directory_count);
145 printf (P_("%8d character device file\n",
146 "%8d character device files\n", ctx->fs_chardev_count),
147 ctx->fs_chardev_count);
148 printf (P_("%8d block device file\n", "%8d block device files\n",
149 ctx->fs_blockdev_count), ctx->fs_blockdev_count);
150 printf (P_("%8d fifo\n", "%8d fifos\n", ctx->fs_fifo_count),
152 printf (P_("%8d link\n", "%8d links\n",
153 ctx->fs_links_count - dir_links),
154 ctx->fs_links_count - dir_links);
155 printf (P_("%8d symbolic link", "%8d symbolic links",
156 ctx->fs_symlinks_count), ctx->fs_symlinks_count);
157 printf (P_(" (%d fast symbolic link)\n", " (%d fast symbolic links)\n",
158 ctx->fs_fast_symlinks_count), ctx->fs_fast_symlinks_count);
159 printf (P_("%8d socket\n", "%8d sockets\n", ctx->fs_sockets_count),
160 ctx->fs_sockets_count);
161 printf ("--------\n");
162 printf (P_("%8d file\n", "%8d files\n",
163 ctx->fs_total_count - dir_links),
164 ctx->fs_total_count - dir_links);
167 static void check_mount(e2fsck_t ctx)
172 retval = ext2fs_check_if_mounted(ctx->filesystem_name,
175 com_err("ext2fs_check_if_mount", retval,
176 _("while determining whether %s is mounted."),
177 ctx->filesystem_name);
182 * If the filesystem isn't mounted, or it's the root filesystem
183 * and it's mounted read-only, then everything's fine.
185 if ((!(ctx->mount_flags & EXT2_MF_MOUNTED)) ||
186 ((ctx->mount_flags & EXT2_MF_ISROOT) &&
187 (ctx->mount_flags & EXT2_MF_READONLY)))
190 if (ctx->options & E2F_OPT_READONLY) {
191 printf(_("Warning! %s is mounted.\n"), ctx->filesystem_name);
195 printf(_("%s is mounted. "), ctx->filesystem_name);
196 if (!ctx->interactive)
197 fatal_error(ctx, _("Cannot continue, aborting.\n\n"));
198 printf(_("\n\n\007\007\007\007WARNING!!! "
199 "Running e2fsck on a mounted filesystem may cause\n"
200 "SEVERE filesystem damage.\007\007\007\n\n"));
201 cont = ask_yn(_("Do you really want to continue"), -1);
203 printf (_("check aborted.\n"));
209 static int is_on_batt(void)
213 char tmp[80], tmp2[80], fname[80];
217 f = fopen("/proc/apm", "r");
219 if (fscanf(f, "%s %s %s %x", tmp, tmp, tmp, &acflag) != 4)
222 return (acflag != 1);
224 d = opendir("/proc/acpi/ac_adapter");
226 while ((de=readdir(d)) != NULL) {
227 if (!strncmp(".", de->d_name, 1))
229 snprintf(fname, 80, "/proc/acpi/ac_adapter/%s/state",
231 f = fopen(fname, "r");
234 if (fscanf(f, "%s %s", tmp2, tmp) != 2)
237 if (strncmp(tmp, "off-line", 8) == 0) {
248 * This routine checks to see if a filesystem can be skipped; if so,
249 * it will exit with E2FSCK_OK. Under some conditions it will print a
250 * message explaining why a check is being forced.
252 static void check_if_skip(e2fsck_t ctx)
254 ext2_filsys fs = ctx->fs;
255 const char *reason = NULL;
256 unsigned int reason_arg = 0;
258 int batt = is_on_batt();
260 if ((ctx->options & E2F_OPT_FORCE) || bad_blocks_file ||
264 if ((fs->super->s_state & EXT2_ERROR_FS) ||
265 !ext2fs_test_valid(fs))
266 reason = _(" contains a file system with errors");
267 else if ((fs->super->s_state & EXT2_VALID_FS) == 0)
268 reason = _(" was not cleanly unmounted");
269 else if ((fs->super->s_max_mnt_count > 0) &&
270 (fs->super->s_mnt_count >=
271 (unsigned) fs->super->s_max_mnt_count)) {
272 reason = _(" has been mounted %u times without being checked");
273 reason_arg = fs->super->s_mnt_count;
274 if (batt && (fs->super->s_mnt_count <
275 (unsigned) fs->super->s_max_mnt_count*2))
277 } else if (fs->super->s_checkinterval &&
278 ((ctx->now - fs->super->s_lastcheck) >=
279 fs->super->s_checkinterval)) {
280 reason = _(" has gone %u days without being checked");
281 reason_arg = (ctx->now - fs->super->s_lastcheck)/(3600*24);
282 if (batt && ((ctx->now - fs->super->s_lastcheck) <
283 fs->super->s_checkinterval*2))
287 fputs(ctx->device_name, stdout);
288 printf(reason, reason_arg);
289 fputs(_(", check forced.\n"), stdout);
292 printf(_("%s: clean, %d/%d files, %d/%d blocks"), ctx->device_name,
293 fs->super->s_inodes_count - fs->super->s_free_inodes_count,
294 fs->super->s_inodes_count,
295 fs->super->s_blocks_count - fs->super->s_free_blocks_count,
296 fs->super->s_blocks_count);
298 if (fs->super->s_max_mnt_count > 0) {
299 next_check = fs->super->s_max_mnt_count - fs->super->s_mnt_count;
303 if (fs->super->s_checkinterval &&
304 ((ctx->now - fs->super->s_lastcheck) >= fs->super->s_checkinterval))
306 if (next_check <= 5) {
308 fputs(_(" (check after next mount)"), stdout);
310 printf(_(" (check in %ld mounts)"), next_check);
315 e2fsck_free_context(ctx);
320 * For completion notice
326 struct percent_tbl e2fsck_tbl = {
327 5, { 0, 70, 90, 92, 95, 100 }
329 static char bar[128], spaces[128];
331 static float calc_percent(struct percent_tbl *tbl, int pass, int curr,
338 if (pass > tbl->max_pass || max == 0)
340 percent = ((float) curr) / ((float) max);
341 return ((percent * (tbl->table[pass] - tbl->table[pass-1]))
342 + tbl->table[pass-1]);
345 extern void e2fsck_clear_progbar(e2fsck_t ctx)
347 if (!(ctx->flags & E2F_FLAG_PROG_BAR))
350 printf("%s%s\r%s", ctx->start_meta, spaces + (sizeof(spaces) - 80),
353 ctx->flags &= ~E2F_FLAG_PROG_BAR;
356 int e2fsck_simple_progress(e2fsck_t ctx, const char *label, float percent,
359 static const char spinner[] = "\\|/-";
366 if (ctx->flags & E2F_FLAG_PROG_SUPPRESS)
370 * Calculate the new progress position. If the
371 * percentage hasn't changed, then we skip out right
374 fixed_percent = (int) ((10 * percent) + 0.5);
375 if (ctx->progress_last_percent == fixed_percent)
377 ctx->progress_last_percent = fixed_percent;
380 * If we've already updated the spinner once within
381 * the last 1/8th of a second, no point doing it
384 gettimeofday(&tv, NULL);
385 tick = (tv.tv_sec << 3) + (tv.tv_usec / (1000000 / 8));
386 if ((tick == ctx->progress_last_time) &&
387 (fixed_percent != 0) && (fixed_percent != 1000))
389 ctx->progress_last_time = tick;
392 * Advance the spinner, and note that the progress bar
393 * will be on the screen
395 ctx->progress_pos = (ctx->progress_pos+1) & 3;
396 ctx->flags |= E2F_FLAG_PROG_BAR;
398 dpywidth = 66 - strlen(label);
399 dpywidth = 8 * (dpywidth / 8);
403 i = ((percent * dpywidth) + 50) / 100;
404 printf("%s%s: |%s%s", ctx->start_meta, label,
405 bar + (sizeof(bar) - (i+1)),
406 spaces + (sizeof(spaces) - (dpywidth - i + 1)));
407 if (fixed_percent == 1000)
410 fputc(spinner[ctx->progress_pos & 3], stdout);
411 printf(" %4.1f%% ", percent);
413 printf("%u\r", dpynum);
415 fputs(" \r", stdout);
416 fputs(ctx->stop_meta, stdout);
418 if (fixed_percent == 1000)
419 e2fsck_clear_progbar(ctx);
425 static int e2fsck_update_progress(e2fsck_t ctx, int pass,
426 unsigned long cur, unsigned long max)
434 if (ctx->progress_fd) {
435 sprintf(buf, "%d %lu %lu\n", pass, cur, max);
436 write(ctx->progress_fd, buf, strlen(buf));
438 percent = calc_percent(&e2fsck_tbl, pass, cur, max);
439 e2fsck_simple_progress(ctx, ctx->device_name,
445 #define PATH_SET "PATH=/sbin"
447 static void reserve_stdio_fds(void)
452 fd = open("/dev/null", O_RDWR);
456 fprintf(stderr, _("ERROR: Couldn't open "
466 static void signal_progress_on(int sig EXT2FS_ATTR((unused)))
468 e2fsck_t ctx = e2fsck_global_ctx;
473 ctx->progress = e2fsck_update_progress;
474 ctx->progress_fd = 0;
477 static void signal_progress_off(int sig EXT2FS_ATTR((unused)))
479 e2fsck_t ctx = e2fsck_global_ctx;
484 e2fsck_clear_progbar(ctx);
488 static void signal_cancel(int sig EXT2FS_ATTR((unused)))
490 e2fsck_t ctx = e2fsck_global_ctx;
495 ctx->flags |= E2F_FLAG_CANCEL;
499 static void parse_extended_opts(e2fsck_t ctx, const char *opts)
501 char *buf, *token, *next, *p, *arg;
503 int extended_usage = 0;
505 buf = string_copy(ctx, opts, 0);
506 for (token = buf; token && *token; token = next) {
507 p = strchr(token, ',');
513 arg = strchr(token, '=');
518 if (strcmp(token, "ea_ver") == 0) {
523 ea_ver = strtoul(arg, &p, 0);
525 ((ea_ver != 1) && (ea_ver != 2))) {
527 _("Invalid EA version.\n"));
531 ctx->ext_attr_ver = ea_ver;
535 if (extended_usage) {
536 fprintf(stderr, _("Extended options are separated by commas, "
537 "and may take an argument which\n"
538 "is set off by an equals ('=') sign. "
539 "Valid raid options are:\n"
540 "\tea_ver=<ea_version (1 or 2)\n\n"));
546 static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
551 extern void *mallwatch;
558 char *extended_opts = 0;
560 retval = e2fsck_allocate_context(&ctx);
566 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
567 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
568 if (isatty(0) && isatty(1)) {
569 ctx->interactive = 1;
571 ctx->start_meta[0] = '\001';
572 ctx->stop_meta[0] = '\002';
574 memset(bar, '=', sizeof(bar)-1);
575 memset(spaces, ' ', sizeof(spaces)-1);
576 initialize_ext2_error_table();
577 blkid_get_cache(&ctx->blkid, NULL);
580 ctx->program_name = *argv;
582 ctx->program_name = "e2fsck";
583 while ((c = getopt (argc, argv, "panyrcC:B:dE:fvtFVM:b:I:j:P:l:L:N:SsDk")) != EOF)
586 ctx->progress = e2fsck_update_progress;
587 ctx->progress_fd = atoi(optarg);
588 if (!ctx->progress_fd)
590 /* Validate the file descriptor to avoid disasters */
591 fd = dup(ctx->progress_fd);
594 _("Error validating file descriptor %d: %s\n"),
596 error_message(errno));
598 _("Invalid completion information file descriptor"));
603 ctx->options |= E2F_OPT_COMPRESS_DIRS;
606 extended_opts = optarg;
610 if (ctx->options & (E2F_OPT_YES|E2F_OPT_NO)) {
613 _("Only one the options -p/-a, -n or -y may be specified."));
615 ctx->options |= E2F_OPT_PREEN;
618 if (ctx->options & (E2F_OPT_YES|E2F_OPT_PREEN))
620 ctx->options |= E2F_OPT_NO;
623 if (ctx->options & (E2F_OPT_PREEN|E2F_OPT_NO))
625 ctx->options |= E2F_OPT_YES;
628 #ifdef RESOURCE_TRACK
629 if (ctx->options & E2F_OPT_TIME)
630 ctx->options |= E2F_OPT_TIME2;
632 ctx->options |= E2F_OPT_TIME;
634 fprintf(stderr, _("The -t option is not "
635 "supported on this version of e2fsck.\n"));
640 ctx->options |= E2F_OPT_WRITECHECK;
641 ctx->options |= E2F_OPT_CHECKBLOCKS;
644 /* What we do by default, anyway! */
647 ctx->use_superblock = atoi(optarg);
648 ctx->flags |= E2F_FLAG_SB_SPECIFIED;
651 ctx->blocksize = atoi(optarg);
654 ctx->inode_buffer_blocks = atoi(optarg);
657 ctx->journal_name = string_copy(ctx, optarg, 0);
660 ctx->process_inode_size = atoi(optarg);
663 replace_bad_blocks++;
665 bad_blocks_file = string_copy(ctx, optarg, 0);
668 ctx->options |= E2F_OPT_DEBUG;
671 ctx->options |= E2F_OPT_FORCE;
680 show_version_only = 1;
684 mallwatch = (void *) strtol(optarg, NULL, 0);
688 ctx->device_name = optarg;
692 normalize_swapfs = 1;
699 fprintf(stderr, _("Byte-swapping filesystems "
700 "not compiled in this version "
710 if (show_version_only)
712 if (optind != argc - 1)
714 if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file &&
715 !cflag && !swapfs && !(ctx->options & E2F_OPT_COMPRESS_DIRS))
716 ctx->options |= E2F_OPT_READONLY;
717 ctx->io_options = strchr(argv[optind], '?');
719 *ctx->io_options++ = 0;
720 ctx->filesystem_name = blkid_get_devname(ctx->blkid, argv[optind], 0);
721 if (!ctx->filesystem_name) {
722 com_err(ctx->program_name, 0, _("Unable to resolve '%s'"),
727 parse_extended_opts(ctx, extended_opts);
730 fd = open(ctx->filesystem_name, O_RDONLY, 0);
732 com_err("open", errno,
733 _("while opening %s for flushing"),
734 ctx->filesystem_name);
737 if ((retval = ext2fs_sync_device(fd, 1))) {
738 com_err("ext2fs_sync_device", retval,
739 _("while trying to flush %s"),
740 ctx->filesystem_name);
747 if (cflag || bad_blocks_file) {
748 fprintf(stderr, _("Incompatible options not "
749 "allowed when byte-swapping.\n"));
754 if (cflag && bad_blocks_file) {
755 fprintf(stderr, _("The -c and the -l/-L options may "
756 "not be both used at the same time.\n"));
761 * Set up signal action
763 memset(&sa, 0, sizeof(struct sigaction));
764 sa.sa_handler = signal_cancel;
765 sigaction(SIGINT, &sa, 0);
766 sigaction(SIGTERM, &sa, 0);
768 sa.sa_flags = SA_RESTART;
770 e2fsck_global_ctx = ctx;
771 sa.sa_handler = signal_progress_on;
772 sigaction(SIGUSR1, &sa, 0);
773 sa.sa_handler = signal_progress_off;
774 sigaction(SIGUSR2, &sa, 0);
777 /* Update our PATH to include /sbin if we need to run badblocks */
779 char *oldpath = getenv("PATH");
783 newpath = (char *) malloc(sizeof (PATH_SET) + 1 +
786 fatal_error(ctx, "Couldn't malloc() newpath");
787 strcpy (newpath, PATH_SET);
788 strcat (newpath, ":");
789 strcat (newpath, oldpath);
794 #ifdef CONFIG_JBD_DEBUG
795 if (getenv("E2FSCK_JBD_DEBUG"))
796 journal_enable_debug = atoi(getenv("E2FSCK_JBD_DEBUG"));
801 static const char *my_ver_string = E2FSPROGS_VERSION;
802 static const char *my_ver_date = E2FSPROGS_DATE;
804 int main (int argc, char *argv[])
806 errcode_t retval = 0;
807 int exit_value = FSCK_OK;
810 struct ext2_super_block *sb;
811 const char *lib_ver_date;
814 struct problem_context pctx;
815 int flags, run_result;
817 clear_problem_context(&pctx);
825 setlocale(LC_MESSAGES, "");
826 setlocale(LC_CTYPE, "");
827 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
828 textdomain(NLS_CAT_NAME);
830 my_ver = ext2fs_parse_version_string(my_ver_string);
831 lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
832 if (my_ver > lib_ver) {
833 fprintf( stderr, _("Error: ext2fs library version "
838 retval = PRS(argc, argv, &ctx);
840 com_err("e2fsck", retval,
841 _("while trying to initialize program"));
846 #ifdef RESOURCE_TRACK
847 init_resource_track(&ctx->global_rtrack);
850 if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
851 fprintf(stderr, "e2fsck %s (%s)\n", my_ver_string,
854 if (show_version_only) {
855 fprintf(stderr, _("\tUsing %s, %s\n"),
856 error_message(EXT2_ET_BASE), lib_ver_date);
862 if (!(ctx->options & E2F_OPT_PREEN) &&
863 !(ctx->options & E2F_OPT_NO) &&
864 !(ctx->options & E2F_OPT_YES)) {
865 if (!ctx->interactive)
867 _("need terminal for interactive repairs"));
869 ctx->superblock = ctx->use_superblock;
871 #ifdef CONFIG_TESTIO_DEBUG
872 io_ptr = test_io_manager;
873 test_io_backing_manager = unix_io_manager;
875 io_ptr = unix_io_manager;
878 if ((ctx->options & E2F_OPT_READONLY) == 0)
879 flags |= EXT2_FLAG_RW;
881 if (ctx->superblock && ctx->blocksize) {
882 retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options,
883 flags, ctx->superblock, ctx->blocksize,
885 } else if (ctx->superblock) {
887 for (blocksize = EXT2_MIN_BLOCK_SIZE;
888 blocksize <= EXT2_MAX_BLOCK_SIZE; blocksize *= 2) {
889 retval = ext2fs_open2(ctx->filesystem_name,
890 ctx->io_options, flags,
891 ctx->superblock, blocksize,
897 retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options,
898 flags, 0, 0, io_ptr, &fs);
899 if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
900 !(ctx->flags & E2F_FLAG_SB_SPECIFIED) &&
901 ((retval == EXT2_ET_BAD_MAGIC) ||
902 ((retval == 0) && ext2fs_check_desc(fs)))) {
903 if (!fs || (fs->group_desc_count > 1)) {
904 printf(_("%s trying backup blocks...\n"),
905 retval ? _("Couldn't find ext2 superblock,") :
906 _("Group descriptors look bad..."));
907 get_backup_sb(ctx, fs, ctx->filesystem_name, io_ptr);
914 com_err(ctx->program_name, retval, _("while trying to open %s"),
915 ctx->filesystem_name);
916 if (retval == EXT2_ET_REV_TOO_HIGH) {
917 printf(_("The filesystem revision is apparently "
918 "too high for this version of e2fsck.\n"
919 "(Or the filesystem superblock "
921 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
922 } else if (retval == EXT2_ET_SHORT_READ)
923 printf(_("Could this be a zero-length partition?\n"));
924 else if ((retval == EPERM) || (retval == EACCES))
925 printf(_("You must have %s access to the "
926 "filesystem or be root\n"),
927 (ctx->options & E2F_OPT_READONLY) ?
929 else if (retval == ENXIO)
930 printf(_("Possibly non-existent or swap device?\n"));
932 else if (retval == EROFS)
933 printf(_("Disk write-protected; use the -n option "
934 "to do a read-only\n"
935 "check of the device.\n"));
938 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
944 if (sb->s_rev_level > E2FSCK_CURRENT_REV) {
945 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
946 _("while trying to open %s"),
947 ctx->filesystem_name);
949 fatal_error(ctx, _("Get a newer version of e2fsck!"));
953 * Set the device name, which is used whenever we print error
954 * or informational messages to the user.
956 if (ctx->device_name == 0 &&
957 (sb->s_volume_name[0] != 0)) {
958 ctx->device_name = string_copy(ctx, sb->s_volume_name,
959 sizeof(sb->s_volume_name));
961 if (ctx->device_name == 0)
962 ctx->device_name = ctx->filesystem_name;
965 * Make sure the ext3 superblock fields are consistent.
967 retval = e2fsck_check_ext3_journal(ctx);
969 com_err(ctx->program_name, retval,
970 _("while checking ext3 journal for %s"),
976 * Check to see if we need to do ext3-style recovery. If so,
977 * do it, and then restart the fsck.
979 if (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) {
980 if (ctx->options & E2F_OPT_READONLY) {
981 printf(_("Warning: skipping journal recovery "
982 "because doing a read-only filesystem "
984 io_channel_flush(ctx->fs->io);
986 if (ctx->flags & E2F_FLAG_RESTARTED) {
988 * Whoops, we attempted to run the
989 * journal twice. This should never
990 * happen, unless the hardware or
991 * device driver is being bogus.
993 com_err(ctx->program_name, 0,
994 _("unable to set superblock flags on %s\n"), ctx->device_name);
997 retval = e2fsck_run_ext3_journal(ctx);
999 com_err(ctx->program_name, retval,
1000 _("while recovering ext3 journal of %s"),
1002 fatal_error(ctx, 0);
1004 ext2fs_close(ctx->fs);
1006 ctx->flags |= E2F_FLAG_RESTARTED;
1012 * Check for compatibility with the feature sets. We need to
1013 * be more stringent than ext2fs_open().
1015 if ((sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
1016 (sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) {
1017 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
1018 "(%s)", ctx->device_name);
1021 if (sb->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
1022 com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE,
1023 "(%s)", ctx->device_name);
1026 #ifdef ENABLE_COMPRESSION
1027 if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION)
1028 com_err(ctx->program_name, 0,
1029 _("Warning: compression support is experimental.\n"));
1031 #ifndef ENABLE_HTREE
1032 if (sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) {
1033 com_err(ctx->program_name, 0,
1034 _("E2fsck not compiled with HTREE support,\n\t"
1035 "but filesystem %s has HTREE directories.\n"),
1042 * If the user specified a specific superblock, presumably the
1043 * master superblock has been trashed. So we mark the
1044 * superblock as dirty, so it can be written out.
1046 if (ctx->superblock &&
1047 !(ctx->options & E2F_OPT_READONLY))
1048 ext2fs_mark_super_dirty(fs);
1051 * We only update the master superblock because (a) paranoia;
1052 * we don't want to corrupt the backup superblocks, and (b) we
1053 * don't need to update the mount count and last checked
1054 * fields in the backup superblock (the kernel doesn't
1055 * update the backup superblocks anyway).
1057 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1059 ehandler_init(fs->io);
1061 if (ctx->superblock)
1062 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
1063 ext2fs_mark_valid(fs);
1064 check_super_block(ctx);
1065 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1066 fatal_error(ctx, 0);
1068 if (bad_blocks_file)
1069 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
1071 read_bad_blocks_file(ctx, 0, !keep_bad_blocks); /* Test disk */
1072 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1073 fatal_error(ctx, 0);
1074 #ifdef ENABLE_SWAPFS
1075 if (normalize_swapfs) {
1076 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ==
1077 ext2fs_native_flag()) {
1078 fprintf(stderr, _("%s: Filesystem byte order "
1079 "already normalized.\n"), ctx->device_name);
1080 fatal_error(ctx, 0);
1085 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1086 fatal_error(ctx, 0);
1091 * Mark the system as valid, 'til proven otherwise
1093 ext2fs_mark_valid(fs);
1095 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
1097 com_err(ctx->program_name, retval,
1098 _("while reading bad blocks inode"));
1100 printf(_("This doesn't bode well,"
1101 " but we'll try to go on...\n"));
1104 run_result = e2fsck_run(ctx);
1105 e2fsck_clear_progbar(ctx);
1106 if (run_result == E2F_FLAG_RESTART) {
1107 printf(_("Restarting e2fsck from the beginning...\n"));
1108 retval = e2fsck_reset_context(ctx);
1110 com_err(ctx->program_name, retval,
1111 _("while resetting context"));
1112 fatal_error(ctx, 0);
1117 if (run_result & E2F_FLAG_CANCEL) {
1118 printf(_("%s: e2fsck canceled.\n"), ctx->device_name ?
1119 ctx->device_name : ctx->filesystem_name);
1120 exit_value |= FSCK_CANCELED;
1122 if (run_result & E2F_FLAG_ABORT)
1123 fatal_error(ctx, _("aborted"));
1126 mtrace_print("Cleanup");
1128 if (ext2fs_test_changed(fs)) {
1129 exit_value |= FSCK_NONDESTRUCT;
1130 if (!(ctx->options & E2F_OPT_PREEN))
1131 printf(_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
1133 if (ctx->mount_flags & EXT2_MF_ISROOT) {
1134 printf(_("%s: ***** REBOOT LINUX *****\n"),
1136 exit_value |= FSCK_REBOOT;
1139 if (!ext2fs_test_valid(fs)) {
1140 printf(_("\n%s: ********** WARNING: Filesystem still has "
1141 "errors **********\n\n"), ctx->device_name);
1142 exit_value |= FSCK_UNCORRECTED;
1143 exit_value &= ~FSCK_NONDESTRUCT;
1145 if (exit_value & FSCK_CANCELED)
1146 exit_value &= ~FSCK_NONDESTRUCT;
1149 if (!(ctx->options & E2F_OPT_READONLY)) {
1150 if (ext2fs_test_valid(fs)) {
1151 if (!(sb->s_state & EXT2_VALID_FS))
1152 exit_value |= FSCK_NONDESTRUCT;
1153 sb->s_state = EXT2_VALID_FS;
1155 sb->s_state &= ~EXT2_VALID_FS;
1156 sb->s_mnt_count = 0;
1157 sb->s_lastcheck = ctx->now;
1158 ext2fs_mark_super_dirty(fs);
1162 e2fsck_write_bitmaps(ctx);
1166 free(ctx->filesystem_name);
1167 free(ctx->journal_name);
1168 e2fsck_free_context(ctx);
1170 #ifdef RESOURCE_TRACK
1171 if (ctx->options & E2F_OPT_TIME)
1172 print_resource_track(NULL, &ctx->global_rtrack);