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 external_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 __u32 inodes, inodes_used;
102 blk_t blocks, blocks_used;
104 int num_files, num_links;
107 dir_links = 2 * ctx->fs_directory_count - 1;
108 num_files = ctx->fs_total_count - dir_links;
109 num_links = ctx->fs_links_count - dir_links;
110 inodes = fs->super->s_inodes_count;
111 inodes_used = (fs->super->s_inodes_count -
112 fs->super->s_free_inodes_count);
113 blocks = fs->super->s_blocks_count;
114 blocks_used = (fs->super->s_blocks_count -
115 fs->super->s_free_blocks_count);
117 frag_percent = (10000 * ctx->fs_fragmented) / inodes_used;
118 frag_percent = (frag_percent + 5) / 10;
121 printf(_("%s: %u/%u files (%0d.%d%% non-contiguous), %u/%u blocks\n"),
122 ctx->device_name, inodes_used, inodes,
123 frag_percent / 10, frag_percent % 10,
124 blocks_used, blocks);
127 printf (P_("\n%8u inode used (%2.2f%%)\n", "\n%8u inodes used (%2.2f%%)\n",
128 inodes_used), inodes_used, 100.0 * inodes_used / inodes);
129 printf (P_("%8u non-contiguous inode (%0d.%d%%)\n",
130 "%8u non-contiguous inodes (%0d.%d%%)\n",
132 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
133 printf (_(" # of inodes with ind/dind/tind blocks: %u/%u/%u\n"),
134 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
135 printf (P_("%8u block used (%2.2f%%)\n", "%8u blocks used (%2.2f%%)\n",
136 blocks_used), blocks_used, 100.0 * blocks_used / blocks);
137 printf (P_("%8u bad block\n", "%8u bad blocks\n",
138 ctx->fs_badblocks_count), ctx->fs_badblocks_count);
139 printf (P_("%8u large file\n", "%8u large files\n",
140 ctx->large_files), ctx->large_files);
141 printf (P_("\n%8u regular file\n", "\n%8u regular files\n",
142 ctx->fs_regular_count), ctx->fs_regular_count);
143 printf (P_("%8u directory\n", "%8u directories\n",
144 ctx->fs_directory_count), ctx->fs_directory_count);
145 printf (P_("%8u character device file\n",
146 "%8u character device files\n", ctx->fs_chardev_count),
147 ctx->fs_chardev_count);
148 printf (P_("%8u block device file\n", "%8u block device files\n",
149 ctx->fs_blockdev_count), ctx->fs_blockdev_count);
150 printf (P_("%8u fifo\n", "%8u fifos\n", ctx->fs_fifo_count),
152 printf (P_("%8u link\n", "%8u links\n",
153 ctx->fs_links_count - dir_links),
154 ctx->fs_links_count - dir_links);
155 printf (P_("%8u symbolic link", "%8u symbolic links",
156 ctx->fs_symlinks_count), ctx->fs_symlinks_count);
157 printf (P_(" (%u fast symbolic link)\n", " (%u fast symbolic links)\n",
158 ctx->fs_fast_symlinks_count), ctx->fs_fast_symlinks_count);
159 printf (P_("%8u socket\n", "%8u sockets\n", ctx->fs_sockets_count),
160 ctx->fs_sockets_count);
161 printf ("--------\n");
162 printf (P_("%8u file\n", "%8u 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
183 * filesystem and it's mounted read-only, and we're not doing
184 * a read/write check, then everything's fine.
186 if ((!(ctx->mount_flags & EXT2_MF_MOUNTED)) ||
187 ((ctx->mount_flags & EXT2_MF_ISROOT) &&
188 (ctx->mount_flags & EXT2_MF_READONLY) &&
189 !(ctx->options & E2F_OPT_WRITECHECK)))
192 if ((ctx->options & E2F_OPT_READONLY) &&
193 !(ctx->options & E2F_OPT_WRITECHECK)) {
194 printf(_("Warning! %s is mounted.\n"), ctx->filesystem_name);
198 printf(_("%s is mounted. "), ctx->filesystem_name);
199 if (!ctx->interactive)
200 fatal_error(ctx, _("Cannot continue, aborting.\n\n"));
201 printf(_("\n\n\007\007\007\007WARNING!!! "
202 "Running e2fsck on a mounted filesystem may cause\n"
203 "SEVERE filesystem damage.\007\007\007\n\n"));
204 cont = ask_yn(_("Do you really want to continue"), -1);
206 printf (_("check aborted.\n"));
212 static int is_on_batt(void)
216 char tmp[80], tmp2[80], fname[80];
220 f = fopen("/proc/apm", "r");
222 if (fscanf(f, "%s %s %s %x", tmp, tmp, tmp, &acflag) != 4)
225 return (acflag != 1);
227 d = opendir("/proc/acpi/ac_adapter");
229 while ((de=readdir(d)) != NULL) {
230 if (!strncmp(".", de->d_name, 1))
232 snprintf(fname, 80, "/proc/acpi/ac_adapter/%s/state",
234 f = fopen(fname, "r");
237 if (fscanf(f, "%s %s", tmp2, tmp) != 2)
240 if (strncmp(tmp, "off-line", 8) == 0) {
251 * This routine checks to see if a filesystem can be skipped; if so,
252 * it will exit with E2FSCK_OK. Under some conditions it will print a
253 * message explaining why a check is being forced.
255 static void check_if_skip(e2fsck_t ctx)
257 ext2_filsys fs = ctx->fs;
258 const char *reason = NULL;
259 unsigned int reason_arg = 0;
261 int batt = is_on_batt();
262 int defer_check_on_battery;
264 profile_get_boolean(ctx->profile, "options",
265 "defer_check_on_battery", 0, 1,
266 &defer_check_on_battery);
267 if (!defer_check_on_battery)
270 if ((ctx->options & E2F_OPT_FORCE) || bad_blocks_file ||
274 if ((fs->super->s_state & EXT2_ERROR_FS) ||
275 !ext2fs_test_valid(fs))
276 reason = _(" contains a file system with errors");
277 else if ((fs->super->s_state & EXT2_VALID_FS) == 0)
278 reason = _(" was not cleanly unmounted");
279 else if ((fs->super->s_max_mnt_count > 0) &&
280 (fs->super->s_mnt_count >=
281 (unsigned) fs->super->s_max_mnt_count)) {
282 reason = _(" has been mounted %u times without being checked");
283 reason_arg = fs->super->s_mnt_count;
284 if (batt && (fs->super->s_mnt_count <
285 (unsigned) fs->super->s_max_mnt_count*2))
287 } else if (fs->super->s_checkinterval &&
288 ((ctx->now - fs->super->s_lastcheck) >=
289 fs->super->s_checkinterval)) {
290 reason = _(" has gone %u days without being checked");
291 reason_arg = (ctx->now - fs->super->s_lastcheck)/(3600*24);
292 if (batt && ((ctx->now - fs->super->s_lastcheck) <
293 fs->super->s_checkinterval*2))
297 fputs(ctx->device_name, stdout);
298 printf(reason, reason_arg);
299 fputs(_(", check forced.\n"), stdout);
302 printf(_("%s: clean, %u/%u files, %u/%u blocks"), ctx->device_name,
303 fs->super->s_inodes_count - fs->super->s_free_inodes_count,
304 fs->super->s_inodes_count,
305 fs->super->s_blocks_count - fs->super->s_free_blocks_count,
306 fs->super->s_blocks_count);
308 if (fs->super->s_max_mnt_count > 0) {
309 next_check = fs->super->s_max_mnt_count - fs->super->s_mnt_count;
313 if (fs->super->s_checkinterval &&
314 ((ctx->now - fs->super->s_lastcheck) >= fs->super->s_checkinterval))
316 if (next_check <= 5) {
317 if (next_check == 1) {
319 fputs(_(" (check deferred; on battery)"),
322 fputs(_(" (check after next mount)"), stdout);
324 printf(_(" (check in %ld mounts)"), next_check);
329 e2fsck_free_context(ctx);
334 * For completion notice
340 struct percent_tbl e2fsck_tbl = {
341 5, { 0, 70, 90, 92, 95, 100 }
343 static char bar[128], spaces[128];
345 static float calc_percent(struct percent_tbl *tbl, int pass, int curr,
352 if (pass > tbl->max_pass || max == 0)
354 percent = ((float) curr) / ((float) max);
355 return ((percent * (tbl->table[pass] - tbl->table[pass-1]))
356 + tbl->table[pass-1]);
359 extern void e2fsck_clear_progbar(e2fsck_t ctx)
361 if (!(ctx->flags & E2F_FLAG_PROG_BAR))
364 printf("%s%s\r%s", ctx->start_meta, spaces + (sizeof(spaces) - 80),
367 ctx->flags &= ~E2F_FLAG_PROG_BAR;
370 int e2fsck_simple_progress(e2fsck_t ctx, const char *label, float percent,
373 static const char spinner[] = "\\|/-";
380 if (ctx->flags & E2F_FLAG_PROG_SUPPRESS)
384 * Calculate the new progress position. If the
385 * percentage hasn't changed, then we skip out right
388 fixed_percent = (int) ((10 * percent) + 0.5);
389 if (ctx->progress_last_percent == fixed_percent)
391 ctx->progress_last_percent = fixed_percent;
394 * If we've already updated the spinner once within
395 * the last 1/8th of a second, no point doing it
398 gettimeofday(&tv, NULL);
399 tick = (tv.tv_sec << 3) + (tv.tv_usec / (1000000 / 8));
400 if ((tick == ctx->progress_last_time) &&
401 (fixed_percent != 0) && (fixed_percent != 1000))
403 ctx->progress_last_time = tick;
406 * Advance the spinner, and note that the progress bar
407 * will be on the screen
409 ctx->progress_pos = (ctx->progress_pos+1) & 3;
410 ctx->flags |= E2F_FLAG_PROG_BAR;
412 dpywidth = 66 - strlen(label);
413 dpywidth = 8 * (dpywidth / 8);
417 i = ((percent * dpywidth) + 50) / 100;
418 printf("%s%s: |%s%s", ctx->start_meta, label,
419 bar + (sizeof(bar) - (i+1)),
420 spaces + (sizeof(spaces) - (dpywidth - i + 1)));
421 if (fixed_percent == 1000)
424 fputc(spinner[ctx->progress_pos & 3], stdout);
425 printf(" %4.1f%% ", percent);
427 printf("%u\r", dpynum);
429 fputs(" \r", stdout);
430 fputs(ctx->stop_meta, stdout);
432 if (fixed_percent == 1000)
433 e2fsck_clear_progbar(ctx);
439 static int e2fsck_update_progress(e2fsck_t ctx, int pass,
440 unsigned long cur, unsigned long max)
448 if (ctx->progress_fd) {
449 sprintf(buf, "%d %lu %lu\n", pass, cur, max);
450 write(ctx->progress_fd, buf, strlen(buf));
452 percent = calc_percent(&e2fsck_tbl, pass, cur, max);
453 e2fsck_simple_progress(ctx, ctx->device_name,
459 #define PATH_SET "PATH=/sbin"
461 static void reserve_stdio_fds(void)
466 fd = open("/dev/null", O_RDWR);
470 fprintf(stderr, _("ERROR: Couldn't open "
480 static void signal_progress_on(int sig EXT2FS_ATTR((unused)))
482 e2fsck_t ctx = e2fsck_global_ctx;
487 ctx->progress = e2fsck_update_progress;
488 ctx->progress_fd = 0;
491 static void signal_progress_off(int sig EXT2FS_ATTR((unused)))
493 e2fsck_t ctx = e2fsck_global_ctx;
498 e2fsck_clear_progbar(ctx);
502 static void signal_cancel(int sig EXT2FS_ATTR((unused)))
504 e2fsck_t ctx = e2fsck_global_ctx;
509 ctx->flags |= E2F_FLAG_CANCEL;
513 static void parse_extended_opts(e2fsck_t ctx, const char *opts)
515 char *buf, *token, *next, *p, *arg;
517 int extended_usage = 0;
519 buf = string_copy(ctx, opts, 0);
520 for (token = buf; token && *token; token = next) {
521 p = strchr(token, ',');
527 arg = strchr(token, '=');
532 if (strcmp(token, "ea_ver") == 0) {
537 ea_ver = strtoul(arg, &p, 0);
539 ((ea_ver != 1) && (ea_ver != 2))) {
541 _("Invalid EA version.\n"));
545 ctx->ext_attr_ver = ea_ver;
547 fprintf(stderr, _("Unknown extended option: %s\n"),
552 if (extended_usage) {
553 fputs(("\nExtended options are separated by commas, "
554 "and may take an argument which\n"
555 "is set off by an equals ('=') sign. "
556 "Valid extended options are:\n"
557 "\tea_ver=<ea_version (1 or 2)>\n\n"), stderr);
562 static void syntax_err_report(const char *filename, long err, int line_num)
565 _("Syntax error in e2fsck config file (%s, line #%d)\n\t%s\n"),
566 filename, line_num, error_message(err));
570 static const char *config_fn[] = { ROOT_SYSCONFDIR "/e2fsck.conf", 0 };
572 static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
577 extern void *mallwatch;
584 char *extended_opts = 0;
587 retval = e2fsck_allocate_context(&ctx);
593 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
594 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
595 if (isatty(0) && isatty(1)) {
596 ctx->interactive = 1;
598 ctx->start_meta[0] = '\001';
599 ctx->stop_meta[0] = '\002';
601 memset(bar, '=', sizeof(bar)-1);
602 memset(spaces, ' ', sizeof(spaces)-1);
603 initialize_ext2_error_table();
604 initialize_prof_error_table();
605 blkid_get_cache(&ctx->blkid, NULL);
608 ctx->program_name = *argv;
610 ctx->program_name = "e2fsck";
611 while ((c = getopt (argc, argv, "panyrcC:B:dE:fvtFVM:b:I:j:P:l:L:N:SsDk")) != EOF)
614 ctx->progress = e2fsck_update_progress;
615 ctx->progress_fd = atoi(optarg);
616 if (!ctx->progress_fd)
618 /* Validate the file descriptor to avoid disasters */
619 fd = dup(ctx->progress_fd);
622 _("Error validating file descriptor %d: %s\n"),
624 error_message(errno));
626 _("Invalid completion information file descriptor"));
631 ctx->options |= E2F_OPT_COMPRESS_DIRS;
634 extended_opts = optarg;
638 if (ctx->options & (E2F_OPT_YES|E2F_OPT_NO)) {
641 _("Only one of the options -p/-a, -n or -y may be specified."));
643 ctx->options |= E2F_OPT_PREEN;
646 if (ctx->options & (E2F_OPT_YES|E2F_OPT_PREEN))
648 ctx->options |= E2F_OPT_NO;
651 if (ctx->options & (E2F_OPT_PREEN|E2F_OPT_NO))
653 ctx->options |= E2F_OPT_YES;
656 #ifdef RESOURCE_TRACK
657 if (ctx->options & E2F_OPT_TIME)
658 ctx->options |= E2F_OPT_TIME2;
660 ctx->options |= E2F_OPT_TIME;
662 fprintf(stderr, _("The -t option is not "
663 "supported on this version of e2fsck.\n"));
668 ctx->options |= E2F_OPT_WRITECHECK;
669 ctx->options |= E2F_OPT_CHECKBLOCKS;
672 /* What we do by default, anyway! */
675 ctx->use_superblock = atoi(optarg);
676 ctx->flags |= E2F_FLAG_SB_SPECIFIED;
679 ctx->blocksize = atoi(optarg);
682 ctx->inode_buffer_blocks = atoi(optarg);
685 ctx->journal_name = string_copy(ctx, optarg, 0);
688 ctx->process_inode_size = atoi(optarg);
691 replace_bad_blocks++;
693 bad_blocks_file = string_copy(ctx, optarg, 0);
696 ctx->options |= E2F_OPT_DEBUG;
699 ctx->options |= E2F_OPT_FORCE;
708 show_version_only = 1;
712 mallwatch = (void *) strtol(optarg, NULL, 0);
716 ctx->device_name = optarg;
720 normalize_swapfs = 1;
727 fprintf(stderr, _("Byte-swapping filesystems "
728 "not compiled in this version "
738 if (show_version_only)
740 if (optind != argc - 1)
742 if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file &&
743 !cflag && !swapfs && !(ctx->options & E2F_OPT_COMPRESS_DIRS))
744 ctx->options |= E2F_OPT_READONLY;
745 ctx->io_options = strchr(argv[optind], '?');
747 *ctx->io_options++ = 0;
748 ctx->filesystem_name = blkid_get_devname(ctx->blkid, argv[optind], 0);
749 if (!ctx->filesystem_name) {
750 com_err(ctx->program_name, 0, _("Unable to resolve '%s'"),
755 parse_extended_opts(ctx, extended_opts);
757 if ((cp = getenv("E2FSCK_CONFIG")) != NULL)
759 profile_set_syntax_err_cb(syntax_err_report);
760 profile_init(config_fn, &ctx->profile);
763 fd = open(ctx->filesystem_name, O_RDONLY, 0);
765 com_err("open", errno,
766 _("while opening %s for flushing"),
767 ctx->filesystem_name);
770 if ((retval = ext2fs_sync_device(fd, 1))) {
771 com_err("ext2fs_sync_device", retval,
772 _("while trying to flush %s"),
773 ctx->filesystem_name);
780 if (cflag || bad_blocks_file) {
781 fprintf(stderr, _("Incompatible options not "
782 "allowed when byte-swapping.\n"));
787 if (cflag && bad_blocks_file) {
788 fprintf(stderr, _("The -c and the -l/-L options may "
789 "not be both used at the same time.\n"));
794 * Set up signal action
796 memset(&sa, 0, sizeof(struct sigaction));
797 sa.sa_handler = signal_cancel;
798 sigaction(SIGINT, &sa, 0);
799 sigaction(SIGTERM, &sa, 0);
801 sa.sa_flags = SA_RESTART;
803 e2fsck_global_ctx = ctx;
804 sa.sa_handler = signal_progress_on;
805 sigaction(SIGUSR1, &sa, 0);
806 sa.sa_handler = signal_progress_off;
807 sigaction(SIGUSR2, &sa, 0);
810 /* Update our PATH to include /sbin if we need to run badblocks */
812 char *oldpath = getenv("PATH");
816 newpath = (char *) malloc(sizeof (PATH_SET) + 1 +
819 fatal_error(ctx, "Couldn't malloc() newpath");
820 strcpy (newpath, PATH_SET);
821 strcat (newpath, ":");
822 strcat (newpath, oldpath);
827 #ifdef CONFIG_JBD_DEBUG
828 if (getenv("E2FSCK_JBD_DEBUG"))
829 journal_enable_debug = atoi(getenv("E2FSCK_JBD_DEBUG"));
834 static const char *my_ver_string = E2FSPROGS_VERSION;
835 static const char *my_ver_date = E2FSPROGS_DATE;
837 int main (int argc, char *argv[])
839 errcode_t retval = 0;
840 int exit_value = FSCK_OK;
843 struct ext2_super_block *sb;
844 const char *lib_ver_date;
847 struct problem_context pctx;
848 int flags, run_result;
850 clear_problem_context(&pctx);
858 setlocale(LC_MESSAGES, "");
859 setlocale(LC_CTYPE, "");
860 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
861 textdomain(NLS_CAT_NAME);
863 my_ver = ext2fs_parse_version_string(my_ver_string);
864 lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
865 if (my_ver > lib_ver) {
866 fprintf( stderr, _("Error: ext2fs library version "
871 retval = PRS(argc, argv, &ctx);
873 com_err("e2fsck", retval,
874 _("while trying to initialize program"));
879 #ifdef RESOURCE_TRACK
880 init_resource_track(&ctx->global_rtrack);
883 if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
884 fprintf(stderr, "e2fsck %s (%s)\n", my_ver_string,
887 if (show_version_only) {
888 fprintf(stderr, _("\tUsing %s, %s\n"),
889 error_message(EXT2_ET_BASE), lib_ver_date);
895 if (!(ctx->options & E2F_OPT_PREEN) &&
896 !(ctx->options & E2F_OPT_NO) &&
897 !(ctx->options & E2F_OPT_YES)) {
898 if (!ctx->interactive)
900 _("need terminal for interactive repairs"));
902 ctx->superblock = ctx->use_superblock;
904 #ifdef CONFIG_TESTIO_DEBUG
905 io_ptr = test_io_manager;
906 test_io_backing_manager = unix_io_manager;
908 io_ptr = unix_io_manager;
911 if ((ctx->options & E2F_OPT_READONLY) == 0)
912 flags |= EXT2_FLAG_RW;
913 if ((ctx->mount_flags & EXT2_MF_MOUNTED) == 0)
914 flags |= EXT2_FLAG_EXCLUSIVE;
916 if (ctx->superblock && ctx->blocksize) {
917 retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options,
918 flags, ctx->superblock, ctx->blocksize,
920 } else if (ctx->superblock) {
922 for (blocksize = EXT2_MIN_BLOCK_SIZE;
923 blocksize <= EXT2_MAX_BLOCK_SIZE; blocksize *= 2) {
924 retval = ext2fs_open2(ctx->filesystem_name,
925 ctx->io_options, flags,
926 ctx->superblock, blocksize,
932 retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options,
933 flags, 0, 0, io_ptr, &fs);
934 if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
935 !(ctx->flags & E2F_FLAG_SB_SPECIFIED) &&
936 ((retval == EXT2_ET_BAD_MAGIC) ||
937 ((retval == 0) && ext2fs_check_desc(fs)))) {
938 if (!fs || (fs->group_desc_count > 1)) {
939 printf(_("%s trying backup blocks...\n"),
940 retval ? _("Couldn't find ext2 superblock,") :
941 _("Group descriptors look bad..."));
942 get_backup_sb(ctx, fs, ctx->filesystem_name, io_ptr);
949 com_err(ctx->program_name, retval, _("while trying to open %s"),
950 ctx->filesystem_name);
951 if (retval == EXT2_ET_REV_TOO_HIGH) {
952 printf(_("The filesystem revision is apparently "
953 "too high for this version of e2fsck.\n"
954 "(Or the filesystem superblock "
956 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
957 } else if (retval == EXT2_ET_SHORT_READ)
958 printf(_("Could this be a zero-length partition?\n"));
959 else if ((retval == EPERM) || (retval == EACCES))
960 printf(_("You must have %s access to the "
961 "filesystem or be root\n"),
962 (ctx->options & E2F_OPT_READONLY) ?
964 else if (retval == ENXIO)
965 printf(_("Possibly non-existent or swap device?\n"));
966 else if (retval == EBUSY)
967 printf(_("Filesystem mounted or opened exclusively "
968 "by another program?\n"));
970 else if (retval == EROFS)
971 printf(_("Disk write-protected; use the -n option "
972 "to do a read-only\n"
973 "check of the device.\n"));
976 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
983 if (sb->s_rev_level > E2FSCK_CURRENT_REV) {
984 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
985 _("while trying to open %s"),
986 ctx->filesystem_name);
988 fatal_error(ctx, _("Get a newer version of e2fsck!"));
992 * Set the device name, which is used whenever we print error
993 * or informational messages to the user.
995 if (ctx->device_name == 0 &&
996 (sb->s_volume_name[0] != 0)) {
997 ctx->device_name = string_copy(ctx, sb->s_volume_name,
998 sizeof(sb->s_volume_name));
1000 if (ctx->device_name == 0)
1001 ctx->device_name = ctx->filesystem_name;
1004 * Make sure the ext3 superblock fields are consistent.
1006 retval = e2fsck_check_ext3_journal(ctx);
1008 com_err(ctx->program_name, retval,
1009 _("while checking ext3 journal for %s"),
1011 fatal_error(ctx, 0);
1015 * Check to see if we need to do ext3-style recovery. If so,
1016 * do it, and then restart the fsck.
1018 if (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) {
1019 if (ctx->options & E2F_OPT_READONLY) {
1020 printf(_("Warning: skipping journal recovery "
1021 "because doing a read-only filesystem "
1023 io_channel_flush(ctx->fs->io);
1025 if (ctx->flags & E2F_FLAG_RESTARTED) {
1027 * Whoops, we attempted to run the
1028 * journal twice. This should never
1029 * happen, unless the hardware or
1030 * device driver is being bogus.
1032 com_err(ctx->program_name, 0,
1033 _("unable to set superblock flags on %s\n"), ctx->device_name);
1034 fatal_error(ctx, 0);
1036 retval = e2fsck_run_ext3_journal(ctx);
1038 com_err(ctx->program_name, retval,
1039 _("while recovering ext3 journal of %s"),
1041 fatal_error(ctx, 0);
1043 ext2fs_close(ctx->fs);
1045 ctx->flags |= E2F_FLAG_RESTARTED;
1051 * Check for compatibility with the feature sets. We need to
1052 * be more stringent than ext2fs_open().
1054 if ((sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
1055 (sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) {
1056 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
1057 "(%s)", ctx->device_name);
1060 if (sb->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
1061 com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE,
1062 "(%s)", ctx->device_name);
1065 #ifdef ENABLE_COMPRESSION
1066 if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION)
1067 com_err(ctx->program_name, 0,
1068 _("Warning: compression support is experimental.\n"));
1070 #ifndef ENABLE_HTREE
1071 if (sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) {
1072 com_err(ctx->program_name, 0,
1073 _("E2fsck not compiled with HTREE support,\n\t"
1074 "but filesystem %s has HTREE directories.\n"),
1081 * If the user specified a specific superblock, presumably the
1082 * master superblock has been trashed. So we mark the
1083 * superblock as dirty, so it can be written out.
1085 if (ctx->superblock &&
1086 !(ctx->options & E2F_OPT_READONLY))
1087 ext2fs_mark_super_dirty(fs);
1090 * We only update the master superblock because (a) paranoia;
1091 * we don't want to corrupt the backup superblocks, and (b) we
1092 * don't need to update the mount count and last checked
1093 * fields in the backup superblock (the kernel doesn't
1094 * update the backup superblocks anyway).
1096 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1098 ehandler_init(fs->io);
1100 if (ctx->superblock)
1101 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
1102 ext2fs_mark_valid(fs);
1103 check_super_block(ctx);
1104 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1105 fatal_error(ctx, 0);
1107 if (bad_blocks_file)
1108 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
1110 read_bad_blocks_file(ctx, 0, !keep_bad_blocks); /* Test disk */
1111 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1112 fatal_error(ctx, 0);
1113 #ifdef ENABLE_SWAPFS
1114 if (normalize_swapfs) {
1115 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ==
1116 ext2fs_native_flag()) {
1117 fprintf(stderr, _("%s: Filesystem byte order "
1118 "already normalized.\n"), ctx->device_name);
1119 fatal_error(ctx, 0);
1124 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1125 fatal_error(ctx, 0);
1130 * Mark the system as valid, 'til proven otherwise
1132 ext2fs_mark_valid(fs);
1134 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
1136 com_err(ctx->program_name, retval,
1137 _("while reading bad blocks inode"));
1139 printf(_("This doesn't bode well,"
1140 " but we'll try to go on...\n"));
1143 run_result = e2fsck_run(ctx);
1144 e2fsck_clear_progbar(ctx);
1145 if (run_result == E2F_FLAG_RESTART) {
1146 printf(_("Restarting e2fsck from the beginning...\n"));
1147 retval = e2fsck_reset_context(ctx);
1149 com_err(ctx->program_name, retval,
1150 _("while resetting context"));
1151 fatal_error(ctx, 0);
1156 if (run_result & E2F_FLAG_CANCEL) {
1157 printf(_("%s: e2fsck canceled.\n"), ctx->device_name ?
1158 ctx->device_name : ctx->filesystem_name);
1159 exit_value |= FSCK_CANCELED;
1161 if (run_result & E2F_FLAG_ABORT)
1162 fatal_error(ctx, _("aborted"));
1165 mtrace_print("Cleanup");
1167 if (ext2fs_test_changed(fs)) {
1168 exit_value |= FSCK_NONDESTRUCT;
1169 if (!(ctx->options & E2F_OPT_PREEN))
1170 printf(_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
1172 if (ctx->mount_flags & EXT2_MF_ISROOT) {
1173 printf(_("%s: ***** REBOOT LINUX *****\n"),
1175 exit_value |= FSCK_REBOOT;
1178 if (!ext2fs_test_valid(fs) ||
1179 ((exit_value & FSCK_CANCELED) &&
1180 (sb->s_state & EXT2_ERROR_FS))) {
1181 printf(_("\n%s: ********** WARNING: Filesystem still has "
1182 "errors **********\n\n"), ctx->device_name);
1183 exit_value |= FSCK_UNCORRECTED;
1184 exit_value &= ~FSCK_NONDESTRUCT;
1186 if (exit_value & FSCK_CANCELED) {
1187 int allow_cancellation;
1189 profile_get_boolean(ctx->profile, "options",
1190 "allow_cancellation", 0, 0,
1191 &allow_cancellation);
1192 exit_value &= ~FSCK_NONDESTRUCT;
1193 if (allow_cancellation && ext2fs_test_valid(fs) &&
1194 (sb->s_state & EXT2_VALID_FS) &&
1195 !(sb->s_state & EXT2_ERROR_FS))
1199 if (!(ctx->options & E2F_OPT_READONLY)) {
1200 if (ext2fs_test_valid(fs)) {
1201 if (!(sb->s_state & EXT2_VALID_FS))
1202 exit_value |= FSCK_NONDESTRUCT;
1203 sb->s_state = EXT2_VALID_FS;
1205 sb->s_state &= ~EXT2_VALID_FS;
1206 sb->s_mnt_count = 0;
1207 sb->s_lastcheck = ctx->now;
1208 ext2fs_mark_super_dirty(fs);
1212 e2fsck_write_bitmaps(ctx);
1216 free(ctx->filesystem_name);
1217 free(ctx->journal_name);
1219 #ifdef RESOURCE_TRACK
1220 if (ctx->options & E2F_OPT_TIME)
1221 print_resource_track(NULL, &ctx->global_rtrack);
1223 e2fsck_free_context(ctx);