2 * unix.c - The unix-specific code for e2fsck
4 * Copyright (C) 1993, 1994, 1995, 1996, 1997 Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Public
12 #define _XOPEN_SOURCE 600 /* for inclusion of sa_handler in Solaris */
38 #ifdef HAVE_SYS_IOCTL_H
39 #include <sys/ioctl.h>
44 #ifdef HAVE_SYS_TYPES_H
45 #include <sys/types.h>
51 #include "et/com_err.h"
55 #include "../version.h"
57 /* Command line options */
58 static int cflag; /* check disk */
59 static int show_version_only;
62 static int replace_bad_blocks;
63 static int keep_bad_blocks;
64 static char *bad_blocks_file;
66 e2fsck_t e2fsck_global_ctx; /* Try your very best not to use this! */
68 #ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jfs-debug */
69 int journal_enable_debug = -1;
72 static void usage(e2fsck_t ctx)
75 _("Usage: %s [-panyrcdfvtDFV] [-b superblock] [-B blocksize]\n"
76 "\t\t[-I inode_buffer_blocks] [-P process_inode_size]\n"
77 "\t\t[-l|-L bad_blocks_file] [-C fd] [-j external_journal]\n"
78 "\t\t[-E extended-options] device\n"),
81 fprintf(stderr, _("\nEmergency help:\n"
82 " -p Automatic repair (no questions)\n"
83 " -n Make no changes to the filesystem\n"
84 " -y Assume \"yes\" to all questions\n"
85 " -c Check for bad blocks and add them to the badblock list\n"
86 " -f Force checking even if filesystem is marked clean\n"));
89 " -b superblock Use alternative superblock\n"
90 " -B blocksize Force blocksize when looking for superblock\n"
91 " -j external_journal Set location of the external journal\n"
92 " -l bad_blocks_file Add to badblocks list\n"
93 " -L bad_blocks_file Set badblocks list\n"
99 static void show_stats(e2fsck_t ctx)
101 ext2_filsys fs = ctx->fs;
102 ext2_ino_t inodes, inodes_used;
103 blk_t blocks, blocks_used;
105 int num_files, num_links;
109 dir_links = 2 * ctx->fs_directory_count - 1;
110 num_files = ctx->fs_total_count - dir_links;
111 num_links = ctx->fs_links_count - dir_links;
112 inodes = fs->super->s_inodes_count;
113 inodes_used = (fs->super->s_inodes_count -
114 fs->super->s_free_inodes_count);
115 blocks = fs->super->s_blocks_count;
116 blocks_used = (fs->super->s_blocks_count -
117 fs->super->s_free_blocks_count);
119 frag_percent = (10000 * ctx->fs_fragmented) / inodes_used;
120 frag_percent = (frag_percent + 5) / 10;
123 printf(_("%s: %u/%u files (%0d.%d%% non-contiguous), %u/%u blocks\n"),
124 ctx->device_name, inodes_used, inodes,
125 frag_percent / 10, frag_percent % 10,
126 blocks_used, blocks);
129 printf (P_("\n%8u inode used (%2.2f%%)\n", "\n%8u inodes used (%2.2f%%)\n",
130 inodes_used), inodes_used, 100.0 * inodes_used / inodes);
131 printf (P_("%8u non-contiguous inode (%0d.%d%%)\n",
132 "%8u non-contiguous inodes (%0d.%d%%)\n",
134 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
135 printf (_(" # of inodes with ind/dind/tind blocks: %u/%u/%u\n"),
136 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
138 for (j=MAX_EXTENT_DEPTH_COUNT-1; j >=0; j--)
139 if (ctx->extent_depth_count[j])
142 printf (_(" Extent depth histogram: "));
143 for (i=0; i < j; i++) {
146 printf("%u", ctx->extent_depth_count[i]);
151 printf (P_("%8u block used (%2.2f%%)\n", "%8u blocks used (%2.2f%%)\n",
152 blocks_used), blocks_used, 100.0 * blocks_used / blocks);
153 printf (P_("%8u bad block\n", "%8u bad blocks\n",
154 ctx->fs_badblocks_count), ctx->fs_badblocks_count);
155 printf (P_("%8u large file\n", "%8u large files\n",
156 ctx->large_files), ctx->large_files);
157 printf (P_("\n%8u regular file\n", "\n%8u regular files\n",
158 ctx->fs_regular_count), ctx->fs_regular_count);
159 printf (P_("%8u directory\n", "%8u directories\n",
160 ctx->fs_directory_count), ctx->fs_directory_count);
161 printf (P_("%8u character device file\n",
162 "%8u character device files\n", ctx->fs_chardev_count),
163 ctx->fs_chardev_count);
164 printf (P_("%8u block device file\n", "%8u block device files\n",
165 ctx->fs_blockdev_count), ctx->fs_blockdev_count);
166 printf (P_("%8u fifo\n", "%8u fifos\n", ctx->fs_fifo_count),
168 printf (P_("%8u link\n", "%8u links\n",
169 ctx->fs_links_count - dir_links),
170 ctx->fs_links_count - dir_links);
171 printf (P_("%8u symbolic link", "%8u symbolic links",
172 ctx->fs_symlinks_count), ctx->fs_symlinks_count);
173 printf (P_(" (%u fast symbolic link)\n", " (%u fast symbolic links)\n",
174 ctx->fs_fast_symlinks_count), ctx->fs_fast_symlinks_count);
175 printf (P_("%8u socket\n", "%8u sockets\n", ctx->fs_sockets_count),
176 ctx->fs_sockets_count);
177 printf ("--------\n");
178 printf (P_("%8u file\n", "%8u files\n",
179 ctx->fs_total_count - dir_links),
180 ctx->fs_total_count - dir_links);
183 static void check_mount(e2fsck_t ctx)
188 retval = ext2fs_check_if_mounted(ctx->filesystem_name,
191 com_err("ext2fs_check_if_mount", retval,
192 _("while determining whether %s is mounted."),
193 ctx->filesystem_name);
198 * If the filesystem isn't mounted, or it's the root
199 * filesystem and it's mounted read-only, and we're not doing
200 * a read/write check, then everything's fine.
202 if ((!(ctx->mount_flags & EXT2_MF_MOUNTED)) ||
203 ((ctx->mount_flags & EXT2_MF_ISROOT) &&
204 (ctx->mount_flags & EXT2_MF_READONLY) &&
205 !(ctx->options & E2F_OPT_WRITECHECK)))
208 if ((ctx->options & E2F_OPT_READONLY) &&
209 !(ctx->options & E2F_OPT_WRITECHECK)) {
210 printf(_("Warning! %s is mounted.\n"), ctx->filesystem_name);
214 printf(_("%s is mounted. "), ctx->filesystem_name);
215 if (!ctx->interactive)
216 fatal_error(ctx, _("Cannot continue, aborting.\n\n"));
217 printf(_("\n\n\007\007\007\007WARNING!!! "
218 "Running e2fsck on a mounted filesystem may cause\n"
219 "SEVERE filesystem damage.\007\007\007\n\n"));
220 cont = ask_yn(_("Do you really want to continue"), -1);
222 printf (_("check aborted.\n"));
228 static int is_on_batt(void)
232 char tmp[80], tmp2[80], fname[80];
236 f = fopen("/proc/apm", "r");
238 if (fscanf(f, "%s %s %s %x", tmp, tmp, tmp, &acflag) != 4)
241 return (acflag != 1);
243 d = opendir("/proc/acpi/ac_adapter");
245 while ((de=readdir(d)) != NULL) {
246 if (!strncmp(".", de->d_name, 1))
248 snprintf(fname, 80, "/proc/acpi/ac_adapter/%s/state",
250 f = fopen(fname, "r");
253 if (fscanf(f, "%s %s", tmp2, tmp) != 2)
256 if (strncmp(tmp, "off-line", 8) == 0) {
267 * This routine checks to see if a filesystem can be skipped; if so,
268 * it will exit with E2FSCK_OK. Under some conditions it will print a
269 * message explaining why a check is being forced.
271 static void check_if_skip(e2fsck_t ctx)
273 ext2_filsys fs = ctx->fs;
274 const char *reason = NULL;
275 unsigned int reason_arg = 0;
277 int batt = is_on_batt();
278 int defer_check_on_battery;
281 profile_get_boolean(ctx->profile, "options",
282 "defer_check_on_battery", 0, 1,
283 &defer_check_on_battery);
284 if (!defer_check_on_battery)
287 if ((ctx->options & E2F_OPT_FORCE) || bad_blocks_file || cflag)
290 lastcheck = fs->super->s_lastcheck;
291 if (lastcheck > ctx->now)
292 lastcheck -= ctx->time_fudge;
293 if ((fs->super->s_state & EXT2_ERROR_FS) ||
294 !ext2fs_test_valid(fs))
295 reason = _(" contains a file system with errors");
296 else if ((fs->super->s_state & EXT2_VALID_FS) == 0)
297 reason = _(" was not cleanly unmounted");
298 else if (check_backup_super_block(ctx))
299 reason = _(" primary superblock features different from backup");
300 else if ((fs->super->s_max_mnt_count > 0) &&
301 (fs->super->s_mnt_count >=
302 (unsigned) fs->super->s_max_mnt_count)) {
303 reason = _(" has been mounted %u times without being checked");
304 reason_arg = fs->super->s_mnt_count;
305 if (batt && (fs->super->s_mnt_count <
306 (unsigned) fs->super->s_max_mnt_count*2))
308 } else if (fs->super->s_checkinterval && (ctx->now < lastcheck)) {
309 reason = _(" has filesystem last checked time in the future");
312 } else if (fs->super->s_checkinterval &&
313 ((ctx->now - lastcheck) >=
314 ((time_t) fs->super->s_checkinterval))) {
315 reason = _(" has gone %u days without being checked");
316 reason_arg = (ctx->now - fs->super->s_lastcheck)/(3600*24);
317 if (batt && ((ctx->now - fs->super->s_lastcheck) <
318 fs->super->s_checkinterval*2))
322 fputs(ctx->device_name, stdout);
323 printf(reason, reason_arg);
324 fputs(_(", check forced.\n"), stdout);
327 printf(_("%s: clean, %u/%u files, %u/%u blocks"), ctx->device_name,
328 fs->super->s_inodes_count - fs->super->s_free_inodes_count,
329 fs->super->s_inodes_count,
330 fs->super->s_blocks_count - fs->super->s_free_blocks_count,
331 fs->super->s_blocks_count);
333 if (fs->super->s_max_mnt_count > 0) {
334 next_check = fs->super->s_max_mnt_count - fs->super->s_mnt_count;
338 if (fs->super->s_checkinterval &&
339 ((ctx->now - fs->super->s_lastcheck) >= fs->super->s_checkinterval))
341 if (next_check <= 5) {
342 if (next_check == 1) {
344 fputs(_(" (check deferred; on battery)"),
347 fputs(_(" (check after next mount)"), stdout);
349 printf(_(" (check in %ld mounts)"), next_check);
354 e2fsck_free_context(ctx);
359 * For completion notice
365 struct percent_tbl e2fsck_tbl = {
366 5, { 0, 70, 90, 92, 95, 100 }
368 static char bar[128], spaces[128];
370 static float calc_percent(struct percent_tbl *tbl, int pass, int curr,
377 if (pass > tbl->max_pass || max == 0)
379 percent = ((float) curr) / ((float) max);
380 return ((percent * (tbl->table[pass] - tbl->table[pass-1]))
381 + tbl->table[pass-1]);
384 extern void e2fsck_clear_progbar(e2fsck_t ctx)
386 if (!(ctx->flags & E2F_FLAG_PROG_BAR))
389 printf("%s%s\r%s", ctx->start_meta, spaces + (sizeof(spaces) - 80),
392 ctx->flags &= ~E2F_FLAG_PROG_BAR;
395 int e2fsck_simple_progress(e2fsck_t ctx, const char *label, float percent,
398 static const char spinner[] = "\\|/-";
405 if (ctx->flags & E2F_FLAG_PROG_SUPPRESS)
409 * Calculate the new progress position. If the
410 * percentage hasn't changed, then we skip out right
413 fixed_percent = (int) ((10 * percent) + 0.5);
414 if (ctx->progress_last_percent == fixed_percent)
416 ctx->progress_last_percent = fixed_percent;
419 * If we've already updated the spinner once within
420 * the last 1/8th of a second, no point doing it
423 gettimeofday(&tv, NULL);
424 tick = (tv.tv_sec << 3) + (tv.tv_usec / (1000000 / 8));
425 if ((tick == ctx->progress_last_time) &&
426 (fixed_percent != 0) && (fixed_percent != 1000))
428 ctx->progress_last_time = tick;
431 * Advance the spinner, and note that the progress bar
432 * will be on the screen
434 ctx->progress_pos = (ctx->progress_pos+1) & 3;
435 ctx->flags |= E2F_FLAG_PROG_BAR;
437 dpywidth = 66 - strlen(label);
438 dpywidth = 8 * (dpywidth / 8);
442 i = ((percent * dpywidth) + 50) / 100;
443 printf("%s%s: |%s%s", ctx->start_meta, label,
444 bar + (sizeof(bar) - (i+1)),
445 spaces + (sizeof(spaces) - (dpywidth - i + 1)));
446 if (fixed_percent == 1000)
449 fputc(spinner[ctx->progress_pos & 3], stdout);
450 printf(" %4.1f%% ", percent);
452 printf("%u\r", dpynum);
454 fputs(" \r", stdout);
455 fputs(ctx->stop_meta, stdout);
457 if (fixed_percent == 1000)
458 e2fsck_clear_progbar(ctx);
464 static int e2fsck_update_progress(e2fsck_t ctx, int pass,
465 unsigned long cur, unsigned long max)
473 if (ctx->progress_fd) {
474 snprintf(buf, sizeof(buf), "%d %lu %lu %s\n",
475 pass, cur, max, ctx->device_name);
476 write(ctx->progress_fd, buf, strlen(buf));
478 percent = calc_percent(&e2fsck_tbl, pass, cur, max);
479 e2fsck_simple_progress(ctx, ctx->device_name,
485 #define PATH_SET "PATH=/sbin"
487 static void reserve_stdio_fds(void)
492 fd = open("/dev/null", O_RDWR);
496 fprintf(stderr, _("ERROR: Couldn't open "
506 static void signal_progress_on(int sig EXT2FS_ATTR((unused)))
508 e2fsck_t ctx = e2fsck_global_ctx;
513 ctx->progress = e2fsck_update_progress;
516 static void signal_progress_off(int sig EXT2FS_ATTR((unused)))
518 e2fsck_t ctx = e2fsck_global_ctx;
523 e2fsck_clear_progbar(ctx);
527 static void signal_cancel(int sig EXT2FS_ATTR((unused)))
529 e2fsck_t ctx = e2fsck_global_ctx;
534 ctx->flags |= E2F_FLAG_CANCEL;
538 static void parse_extended_opts(e2fsck_t ctx, const char *opts)
540 char *buf, *token, *next, *p, *arg;
542 int extended_usage = 0;
544 buf = string_copy(ctx, opts, 0);
545 for (token = buf; token && *token; token = next) {
546 p = strchr(token, ',');
552 arg = strchr(token, '=');
557 if (strcmp(token, "ea_ver") == 0) {
562 ea_ver = strtoul(arg, &p, 0);
564 ((ea_ver != 1) && (ea_ver != 2))) {
566 _("Invalid EA version.\n"));
570 ctx->ext_attr_ver = ea_ver;
571 } else if (strcmp(token, "fragcheck") == 0) {
572 ctx->options |= E2F_OPT_FRAGCHECK;
575 fprintf(stderr, _("Unknown extended option: %s\n"),
582 if (extended_usage) {
583 fputs(("\nExtended options are separated by commas, "
584 "and may take an argument which\n"
585 "is set off by an equals ('=') sign. "
586 "Valid extended options are:\n"), stderr);
587 fputs(("\tea_ver=<ea_version (1 or 2)>\n"), stderr);
588 fputs(("\tfragcheck\n"), stderr);
594 static void syntax_err_report(const char *filename, long err, int line_num)
597 _("Syntax error in e2fsck config file (%s, line #%d)\n\t%s\n"),
598 filename, line_num, error_message(err));
602 static const char *config_fn[] = { ROOT_SYSCONFDIR "/e2fsck.conf", 0 };
604 static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
609 extern void *mallwatch;
616 char *extended_opts = 0;
618 int res; /* result of sscanf */
619 #ifdef CONFIG_JBD_DEBUG
623 retval = e2fsck_allocate_context(&ctx);
629 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
630 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
631 if (isatty(0) && isatty(1)) {
632 ctx->interactive = 1;
634 ctx->start_meta[0] = '\001';
635 ctx->stop_meta[0] = '\002';
637 memset(bar, '=', sizeof(bar)-1);
638 memset(spaces, ' ', sizeof(spaces)-1);
639 add_error_table(&et_ext2_error_table);
640 add_error_table(&et_prof_error_table);
641 blkid_get_cache(&ctx->blkid, NULL);
644 ctx->program_name = *argv;
646 ctx->program_name = "e2fsck";
647 while ((c = getopt (argc, argv, "panyrcC:B:dE:fvtFVM:b:I:j:P:l:L:N:SsDk")) != EOF)
650 ctx->progress = e2fsck_update_progress;
651 res = sscanf(optarg, "%d", &ctx->progress_fd);
655 if (ctx->progress_fd < 0) {
657 ctx->progress_fd = ctx->progress_fd * -1;
659 if (!ctx->progress_fd)
661 /* Validate the file descriptor to avoid disasters */
662 fd = dup(ctx->progress_fd);
665 _("Error validating file descriptor %d: %s\n"),
667 error_message(errno));
669 _("Invalid completion information file descriptor"));
674 ctx->options |= E2F_OPT_COMPRESS_DIRS;
677 extended_opts = optarg;
681 if (ctx->options & (E2F_OPT_YES|E2F_OPT_NO)) {
684 _("Only one of the options -p/-a, -n or -y may be specified."));
686 ctx->options |= E2F_OPT_PREEN;
689 if (ctx->options & (E2F_OPT_YES|E2F_OPT_PREEN))
691 ctx->options |= E2F_OPT_NO;
694 if (ctx->options & (E2F_OPT_PREEN|E2F_OPT_NO))
696 ctx->options |= E2F_OPT_YES;
699 #ifdef RESOURCE_TRACK
700 if (ctx->options & E2F_OPT_TIME)
701 ctx->options |= E2F_OPT_TIME2;
703 ctx->options |= E2F_OPT_TIME;
705 fprintf(stderr, _("The -t option is not "
706 "supported on this version of e2fsck.\n"));
711 ctx->options |= E2F_OPT_WRITECHECK;
712 ctx->options |= E2F_OPT_CHECKBLOCKS;
715 /* What we do by default, anyway! */
718 res = sscanf(optarg, "%u", &ctx->use_superblock);
721 ctx->flags |= E2F_FLAG_SB_SPECIFIED;
724 ctx->blocksize = atoi(optarg);
727 res = sscanf(optarg, "%d", &ctx->inode_buffer_blocks);
732 ctx->journal_name = string_copy(ctx, optarg, 0);
735 res = sscanf(optarg, "%d", &ctx->process_inode_size);
740 replace_bad_blocks++;
742 bad_blocks_file = string_copy(ctx, optarg, 0);
745 ctx->options |= E2F_OPT_DEBUG;
748 ctx->options |= E2F_OPT_FORCE;
757 show_version_only = 1;
761 mallwatch = (void *) strtol(optarg, NULL, 0);
765 ctx->device_name = string_copy(ctx, optarg, 0);
773 if (show_version_only)
775 if (optind != argc - 1)
777 if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file &&
778 !cflag && !(ctx->options & E2F_OPT_COMPRESS_DIRS))
779 ctx->options |= E2F_OPT_READONLY;
781 ctx->io_options = strchr(argv[optind], '?');
783 *ctx->io_options++ = 0;
784 ctx->filesystem_name = blkid_get_devname(ctx->blkid, argv[optind], 0);
785 if (!ctx->filesystem_name) {
786 com_err(ctx->program_name, 0, _("Unable to resolve '%s'"),
790 ctx->filesystem_name = string_copy(ctx, ctx->filesystem_name, 0);
792 parse_extended_opts(ctx, extended_opts);
794 if ((cp = getenv("E2FSCK_CONFIG")) != NULL)
796 profile_set_syntax_err_cb(syntax_err_report);
797 profile_init(config_fn, &ctx->profile);
800 fd = open(ctx->filesystem_name, O_RDONLY, 0);
802 com_err("open", errno,
803 _("while opening %s for flushing"),
804 ctx->filesystem_name);
807 if ((retval = ext2fs_sync_device(fd, 1))) {
808 com_err("ext2fs_sync_device", retval,
809 _("while trying to flush %s"),
810 ctx->filesystem_name);
815 if (cflag && bad_blocks_file) {
816 fprintf(stderr, _("The -c and the -l/-L options may "
817 "not be both used at the same time.\n"));
822 * Set up signal action
824 memset(&sa, 0, sizeof(struct sigaction));
825 sa.sa_handler = signal_cancel;
826 sigaction(SIGINT, &sa, 0);
827 sigaction(SIGTERM, &sa, 0);
829 sa.sa_flags = SA_RESTART;
831 e2fsck_global_ctx = ctx;
832 sa.sa_handler = signal_progress_on;
833 sigaction(SIGUSR1, &sa, 0);
834 sa.sa_handler = signal_progress_off;
835 sigaction(SIGUSR2, &sa, 0);
838 /* Update our PATH to include /sbin if we need to run badblocks */
840 char *oldpath = getenv("PATH");
842 int len = sizeof(PATH_SET) + 1;
845 len += strlen(oldpath);
847 newpath = malloc(len);
849 fatal_error(ctx, "Couldn't malloc() newpath");
850 strcpy(newpath, PATH_SET);
853 strcat(newpath, ":");
854 strcat(newpath, oldpath);
858 #ifdef CONFIG_JBD_DEBUG
859 jbd_debug = getenv("E2FSCK_JBD_DEBUG");
861 res = sscanf(jbd_debug, "%d", &journal_enable_debug);
864 _("E2FSCK_JBD_DEBUG \"%s\" not an integer\n\n"),
873 fprintf(stderr, _("\nInvalid non-numeric argument to -%c (\"%s\")\n\n"),
878 static const char *my_ver_string = E2FSPROGS_VERSION;
879 static const char *my_ver_date = E2FSPROGS_DATE;
881 int main (int argc, char *argv[])
883 errcode_t retval = 0, orig_retval = 0;
884 int exit_value = FSCK_OK;
887 struct ext2_super_block *sb;
888 const char *lib_ver_date;
891 struct problem_context pctx;
892 int flags, run_result;
894 int sysval, sys_page_size = 4096;
898 clear_problem_context(&pctx);
906 setlocale(LC_MESSAGES, "");
907 setlocale(LC_CTYPE, "");
908 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
909 textdomain(NLS_CAT_NAME);
911 my_ver = ext2fs_parse_version_string(my_ver_string);
912 lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
913 if (my_ver > lib_ver) {
914 fprintf( stderr, _("Error: ext2fs library version "
919 retval = PRS(argc, argv, &ctx);
921 com_err("e2fsck", retval,
922 _("while trying to initialize program"));
927 #ifdef RESOURCE_TRACK
928 init_resource_track(&ctx->global_rtrack, NULL);
931 if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
932 fprintf(stderr, "e2fsck %s (%s)\n", my_ver_string,
935 if (show_version_only) {
936 fprintf(stderr, _("\tUsing %s, %s\n"),
937 error_message(EXT2_ET_BASE), lib_ver_date);
943 if (!(ctx->options & E2F_OPT_PREEN) &&
944 !(ctx->options & E2F_OPT_NO) &&
945 !(ctx->options & E2F_OPT_YES)) {
946 if (!ctx->interactive)
948 _("need terminal for interactive repairs"));
950 ctx->superblock = ctx->use_superblock;
952 #ifdef CONFIG_TESTIO_DEBUG
953 io_ptr = test_io_manager;
954 test_io_backing_manager = unix_io_manager;
956 io_ptr = unix_io_manager;
958 flags = EXT2_FLAG_NOFREE_ON_ERROR;
959 if ((ctx->options & E2F_OPT_READONLY) == 0)
960 flags |= EXT2_FLAG_RW;
961 if ((ctx->mount_flags & EXT2_MF_MOUNTED) == 0)
962 flags |= EXT2_FLAG_EXCLUSIVE;
964 if (ctx->superblock && ctx->blocksize) {
965 retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options,
966 flags, ctx->superblock, ctx->blocksize,
968 } else if (ctx->superblock) {
970 for (blocksize = EXT2_MIN_BLOCK_SIZE;
971 blocksize <= EXT2_MAX_BLOCK_SIZE; blocksize *= 2) {
972 retval = ext2fs_open2(ctx->filesystem_name,
973 ctx->io_options, flags,
974 ctx->superblock, blocksize,
980 retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options,
981 flags, 0, 0, io_ptr, &fs);
982 if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
983 !(ctx->flags & E2F_FLAG_SB_SPECIFIED) &&
984 ((retval == EXT2_ET_BAD_MAGIC) ||
985 (retval == EXT2_ET_CORRUPT_SUPERBLOCK) ||
986 ((retval == 0) && ext2fs_check_desc(fs)))) {
987 if (fs->flags & EXT2_FLAG_NOFREE_ON_ERROR) {
991 if (!fs || (fs->group_desc_count > 1)) {
992 printf(_("%s: %s trying backup blocks...\n"),
994 retval ? _("Superblock invalid,") :
995 _("Group descriptors look bad..."));
996 get_backup_sb(ctx, fs, ctx->filesystem_name, io_ptr);
999 orig_retval = retval;
1003 if (((retval == EXT2_ET_UNSUPP_FEATURE) ||
1004 (retval == EXT2_ET_RO_UNSUPP_FEATURE)) &&
1007 features[0] = (sb->s_feature_compat &
1008 ~EXT2_LIB_FEATURE_COMPAT_SUPP);
1009 features[1] = (sb->s_feature_incompat &
1010 ~EXT2_LIB_FEATURE_INCOMPAT_SUPP);
1011 features[2] = (sb->s_feature_ro_compat &
1012 ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP);
1013 if (features[0] || features[1] || features[2])
1014 goto print_unsupp_features;
1018 retval = orig_retval;
1019 com_err(ctx->program_name, retval, _("while trying to open %s"),
1020 ctx->filesystem_name);
1021 if (retval == EXT2_ET_REV_TOO_HIGH) {
1022 printf(_("The filesystem revision is apparently "
1023 "too high for this version of e2fsck.\n"
1024 "(Or the filesystem superblock "
1025 "is corrupt)\n\n"));
1026 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
1027 } else if (retval == EXT2_ET_SHORT_READ)
1028 printf(_("Could this be a zero-length partition?\n"));
1029 else if ((retval == EPERM) || (retval == EACCES))
1030 printf(_("You must have %s access to the "
1031 "filesystem or be root\n"),
1032 (ctx->options & E2F_OPT_READONLY) ?
1034 else if (retval == ENXIO)
1035 printf(_("Possibly non-existent or swap device?\n"));
1036 else if (retval == EBUSY)
1037 printf(_("Filesystem mounted or opened exclusively "
1038 "by another program?\n"));
1040 else if (retval == EROFS)
1041 printf(_("Disk write-protected; use the -n option "
1042 "to do a read-only\n"
1043 "check of the device.\n"));
1046 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
1047 fatal_error(ctx, 0);
1050 * We only update the master superblock because (a) paranoia;
1051 * we don't want to corrupt the backup superblocks, and (b) we
1052 * don't need to update the mount count and last checked
1053 * fields in the backup superblock (the kernel doesn't update
1054 * the backup superblocks anyway). With newer versions of the
1055 * library this flag is set by ext2fs_open2(), but we set this
1056 * here just to be sure. (No, we don't support e2fsck running
1057 * with some other libext2fs than the one that it was shipped
1058 * with, but just in case....)
1060 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
1062 if (!(ctx->flags & E2F_FLAG_GOT_DEVSIZE)) {
1063 __u32 blocksize = EXT2_BLOCK_SIZE(fs->super);
1064 int need_restart = 0;
1066 pctx.errcode = ext2fs_get_device_size(ctx->filesystem_name,
1070 * The floppy driver refuses to allow anyone else to
1071 * open the device if has been opened with O_EXCL;
1072 * this is unlike other block device drivers in Linux.
1073 * To handle this, we close the filesystem and then
1074 * reopen the filesystem after we get the device size.
1076 if (pctx.errcode == EBUSY) {
1080 ext2fs_get_device_size(ctx->filesystem_name,
1084 if (pctx.errcode == EXT2_ET_UNIMPLEMENTED)
1085 ctx->num_blocks = 0;
1086 else if (pctx.errcode) {
1087 fix_problem(ctx, PR_0_GETSIZE_ERROR, &pctx);
1088 ctx->flags |= E2F_FLAG_ABORT;
1089 fatal_error(ctx, 0);
1091 ctx->flags |= E2F_FLAG_GOT_DEVSIZE;
1097 fs->priv_data = ctx;
1100 if (sb->s_rev_level > E2FSCK_CURRENT_REV) {
1101 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
1102 _("while trying to open %s"),
1103 ctx->filesystem_name);
1105 fatal_error(ctx, _("Get a newer version of e2fsck!"));
1109 * Set the device name, which is used whenever we print error
1110 * or informational messages to the user.
1112 if (ctx->device_name == 0 &&
1113 (sb->s_volume_name[0] != 0)) {
1114 ctx->device_name = string_copy(ctx, sb->s_volume_name,
1115 sizeof(sb->s_volume_name));
1117 if (ctx->device_name == 0)
1118 ctx->device_name = string_copy(ctx, ctx->filesystem_name, 0);
1119 for (cp = ctx->device_name; *cp; cp++)
1120 if (isspace(*cp) || *cp == ':')
1124 * Make sure the ext3 superblock fields are consistent.
1126 retval = e2fsck_check_ext3_journal(ctx);
1128 com_err(ctx->program_name, retval,
1129 _("while checking ext3 journal for %s"),
1131 fatal_error(ctx, 0);
1135 * Check to see if we need to do ext3-style recovery. If so,
1136 * do it, and then restart the fsck.
1138 if (sb->s_feature_incompat & EXT3_FEATURE_INCOMPAT_RECOVER) {
1139 if (ctx->options & E2F_OPT_READONLY) {
1140 printf(_("Warning: skipping journal recovery "
1141 "because doing a read-only filesystem "
1143 io_channel_flush(ctx->fs->io);
1145 if (ctx->flags & E2F_FLAG_RESTARTED) {
1147 * Whoops, we attempted to run the
1148 * journal twice. This should never
1149 * happen, unless the hardware or
1150 * device driver is being bogus.
1152 com_err(ctx->program_name, 0,
1153 _("unable to set superblock flags on %s\n"), ctx->device_name);
1154 fatal_error(ctx, 0);
1156 retval = e2fsck_run_ext3_journal(ctx);
1158 com_err(ctx->program_name, retval,
1159 _("while recovering ext3 journal of %s"),
1161 fatal_error(ctx, 0);
1163 ext2fs_close(ctx->fs);
1165 ctx->flags |= E2F_FLAG_RESTARTED;
1171 * Check for compatibility with the feature sets. We need to
1172 * be more stringent than ext2fs_open().
1174 features[0] = sb->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP;
1175 features[1] = sb->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP;
1176 features[2] = (sb->s_feature_ro_compat &
1177 ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP);
1178 print_unsupp_features:
1179 if (features[0] || features[1] || features[2]) {
1181 __u32 *mask = features, m;
1183 fprintf(stderr, _("%s has unsupported feature(s):"),
1184 ctx->filesystem_name);
1186 for (i=0; i <3; i++,mask++) {
1187 for (j=0,m=1; j < 32; j++, m<<=1) {
1189 fprintf(stderr, " %s",
1190 e2p_feature2string(i, m));
1196 #ifdef ENABLE_COMPRESSION
1197 if (sb->s_feature_incompat & EXT2_FEATURE_INCOMPAT_COMPRESSION)
1198 com_err(ctx->program_name, 0,
1199 _("Warning: compression support is experimental.\n"));
1201 #ifndef ENABLE_HTREE
1202 if (sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) {
1203 com_err(ctx->program_name, 0,
1204 _("E2fsck not compiled with HTREE support,\n\t"
1205 "but filesystem %s has HTREE directories.\n"),
1212 * If the user specified a specific superblock, presumably the
1213 * master superblock has been trashed. So we mark the
1214 * superblock as dirty, so it can be written out.
1216 if (ctx->superblock &&
1217 !(ctx->options & E2F_OPT_READONLY))
1218 ext2fs_mark_super_dirty(fs);
1221 * Calculate the number of filesystem blocks per pagesize. If
1222 * fs->blocksize > page_size, set the number of blocks per
1223 * pagesize to 1 to avoid division by zero errors.
1226 sysval = sysconf(_SC_PAGESIZE);
1228 sys_page_size = sysval;
1229 #endif /* _SC_PAGESIZE */
1230 ctx->blocks_per_page = sys_page_size / fs->blocksize;
1231 if (ctx->blocks_per_page == 0)
1232 ctx->blocks_per_page = 1;
1234 ehandler_init(fs->io);
1236 if (ctx->superblock)
1237 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
1238 ext2fs_mark_valid(fs);
1239 check_super_block(ctx);
1240 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1241 fatal_error(ctx, 0);
1243 if (bad_blocks_file)
1244 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
1246 read_bad_blocks_file(ctx, 0, !keep_bad_blocks); /* Test disk */
1247 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
1248 fatal_error(ctx, 0);
1251 * Mark the system as valid, 'til proven otherwise
1253 ext2fs_mark_valid(fs);
1255 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
1257 com_err(ctx->program_name, retval,
1258 _("while reading bad blocks inode"));
1260 printf(_("This doesn't bode well,"
1261 " but we'll try to go on...\n"));
1265 * Save the journal size in megabytes.
1266 * Try and use the journal size from the backup else let e2fsck
1267 * find the default journal size.
1269 if (sb->s_jnl_backup_type == EXT3_JNL_BACKUP_BLOCKS)
1270 journal_size = sb->s_jnl_blocks[16] >> 20;
1274 run_result = e2fsck_run(ctx);
1275 e2fsck_clear_progbar(ctx);
1277 if (ctx->flags & E2F_FLAG_JOURNAL_INODE) {
1278 if (fix_problem(ctx, PR_6_RECREATE_JOURNAL, &pctx)) {
1279 if (journal_size < 1024)
1280 journal_size = ext2fs_default_journal_size(fs->super->s_blocks_count);
1281 if (journal_size < 0) {
1282 fs->super->s_feature_compat &=
1283 ~EXT3_FEATURE_COMPAT_HAS_JOURNAL;
1284 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1285 com_err(ctx->program_name, 0,
1286 _("Couldn't determine journal size"));
1289 printf(_("Creating journal (%d blocks): "),
1292 retval = ext2fs_add_journal_inode(fs,
1295 com_err("Error ", retval,
1296 _("\n\twhile trying to create journal"));
1299 printf(_(" Done.\n"));
1300 printf(_("\n*** journal has been re-created - "
1301 "filesystem is now ext3 again ***\n"));
1306 if (run_result == E2F_FLAG_RESTART) {
1307 printf(_("Restarting e2fsck from the beginning...\n"));
1308 retval = e2fsck_reset_context(ctx);
1310 com_err(ctx->program_name, retval,
1311 _("while resetting context"));
1312 fatal_error(ctx, 0);
1317 if (run_result & E2F_FLAG_CANCEL) {
1318 printf(_("%s: e2fsck canceled.\n"), ctx->device_name ?
1319 ctx->device_name : ctx->filesystem_name);
1320 exit_value |= FSCK_CANCELED;
1322 if (run_result & E2F_FLAG_ABORT)
1323 fatal_error(ctx, _("aborted"));
1324 if (check_backup_super_block(ctx)) {
1325 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
1326 ext2fs_mark_super_dirty(fs);
1330 mtrace_print("Cleanup");
1332 if (ext2fs_test_changed(fs)) {
1333 exit_value |= FSCK_NONDESTRUCT;
1334 if (!(ctx->options & E2F_OPT_PREEN))
1335 printf(_("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n"),
1337 if (ctx->mount_flags & EXT2_MF_ISROOT) {
1338 printf(_("%s: ***** REBOOT LINUX *****\n"),
1340 exit_value |= FSCK_REBOOT;
1343 if (!ext2fs_test_valid(fs) ||
1344 ((exit_value & FSCK_CANCELED) &&
1345 (sb->s_state & EXT2_ERROR_FS))) {
1346 printf(_("\n%s: ********** WARNING: Filesystem still has "
1347 "errors **********\n\n"), ctx->device_name);
1348 exit_value |= FSCK_UNCORRECTED;
1349 exit_value &= ~FSCK_NONDESTRUCT;
1351 if (exit_value & FSCK_CANCELED) {
1352 int allow_cancellation;
1354 profile_get_boolean(ctx->profile, "options",
1355 "allow_cancellation", 0, 0,
1356 &allow_cancellation);
1357 exit_value &= ~FSCK_NONDESTRUCT;
1358 if (allow_cancellation && ext2fs_test_valid(fs) &&
1359 (sb->s_state & EXT2_VALID_FS) &&
1360 !(sb->s_state & EXT2_ERROR_FS))
1364 if (!(ctx->options & E2F_OPT_READONLY)) {
1365 if (ext2fs_test_valid(fs)) {
1366 if (!(sb->s_state & EXT2_VALID_FS))
1367 exit_value |= FSCK_NONDESTRUCT;
1368 sb->s_state = EXT2_VALID_FS;
1370 sb->s_state &= ~EXT2_VALID_FS;
1371 sb->s_mnt_count = 0;
1372 sb->s_lastcheck = ctx->now;
1373 ext2fs_mark_super_dirty(fs);
1377 if (sb->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM &&
1378 !(ctx->options & E2F_OPT_READONLY)) {
1379 retval = ext2fs_set_gdt_csum(ctx->fs);
1381 com_err(ctx->program_name, retval,
1382 _("while setting block group checksum info"));
1383 fatal_error(ctx, 0);
1387 e2fsck_write_bitmaps(ctx);
1388 #ifdef RESOURCE_TRACK
1389 io_channel_flush(ctx->fs->io);
1390 if (ctx->options & E2F_OPT_TIME)
1391 print_resource_track(NULL, &ctx->global_rtrack, ctx->fs->io);
1395 free(ctx->journal_name);
1397 e2fsck_free_context(ctx);
1398 remove_error_table(&et_ext2_error_table);
1399 remove_error_table(&et_prof_error_table);