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
31 #include <sys/ioctl.h>
34 #include "et/com_err.h"
37 #include "../version.h"
39 extern int isatty(int);
41 /* Command line options */
42 static int blocksize = 0;
43 static int swapfs = 0;
44 static int normalize_swapfs = 0;
45 static int cflag = 0; /* check disk */
46 static int show_version_only = 0;
48 static int verbose = 0;
50 static int replace_bad_blocks = 0;
51 static char *bad_blocks_file = 0;
53 static int possible_block_sizes[] = { 1024, 2048, 4096, 8192, 0};
55 static int root_filesystem = 0;
56 static int read_only_root = 0;
58 static void usage(e2fsck_t ctx)
61 "Usage: %s [-panyrcdfvstFSV] [-b superblock] [-B blocksize]\n"
62 "\t\t[-I inode_buffer_blocks] [-P process_inode_size]\n"
63 "\t\t[-l|-L bad_blocks_file] device\n", ctx->program_name);
67 static void show_stats(e2fsck_t ctx)
69 ext2_filsys fs = ctx->fs;
70 int inodes, inodes_used, blocks, blocks_used;
72 int num_files, num_links;
75 dir_links = 2 * ctx->fs_directory_count - 1;
76 num_files = ctx->fs_total_count - dir_links;
77 num_links = ctx->fs_links_count - dir_links;
78 inodes = fs->super->s_inodes_count;
79 inodes_used = (fs->super->s_inodes_count -
80 fs->super->s_free_inodes_count);
81 blocks = fs->super->s_blocks_count;
82 blocks_used = (fs->super->s_blocks_count -
83 fs->super->s_free_blocks_count);
85 frag_percent = (10000 * ctx->fs_fragmented) / inodes_used;
86 frag_percent = (frag_percent + 5) / 10;
89 printf("%s: %d/%d files (%0d.%d%% non-contiguous), %d/%d blocks\n",
90 ctx->device_name, inodes_used, inodes,
91 frag_percent / 10, frag_percent % 10,
95 printf ("\n%8d inode%s used (%d%%)\n", inodes_used,
96 (inodes_used != 1) ? "s" : "",
97 100 * inodes_used / inodes);
98 printf ("%8d non-contiguous inodes (%0d.%d%%)\n",
99 ctx->fs_fragmented, frag_percent / 10, frag_percent % 10);
100 printf (" # of inodes with ind/dind/tind blocks: %d/%d/%d\n",
101 ctx->fs_ind_count, ctx->fs_dind_count, ctx->fs_tind_count);
102 printf ("%8d block%s used (%d%%)\n"
103 "%8d bad block%s\n", blocks_used,
104 (blocks_used != 1) ? "s" : "",
105 100 * blocks_used / blocks, ctx->fs_badblocks_count,
106 ctx->fs_badblocks_count != 1 ? "s" : "");
107 printf ("\n%8d regular file%s\n"
109 "%8d character device file%s\n"
110 "%8d block device file%s\n"
113 "%8d symbolic link%s (%d fast symbolic link%s)\n"
117 ctx->fs_regular_count,
118 (ctx->fs_regular_count != 1) ? "s" : "",
119 ctx->fs_directory_count,
120 (ctx->fs_directory_count != 1) ? "ies" : "y",
121 ctx->fs_chardev_count,
122 (ctx->fs_chardev_count != 1) ? "s" : "",
123 ctx->fs_blockdev_count,
124 (ctx->fs_blockdev_count != 1) ? "s" : "",
126 (ctx->fs_fifo_count != 1) ? "s" : "",
127 ctx->fs_links_count - dir_links,
128 ((ctx->fs_links_count - dir_links) != 1) ? "s" : "",
129 ctx->fs_symlinks_count,
130 (ctx->fs_symlinks_count != 1) ? "s" : "",
131 ctx->fs_fast_symlinks_count,
132 (ctx->fs_fast_symlinks_count != 1) ? "s" : "",
133 ctx->fs_sockets_count, (ctx->fs_sockets_count != 1) ? "s" : "",
134 ctx->fs_total_count - dir_links,
135 ((ctx->fs_total_count - dir_links) != 1) ? "s" : "");
138 static void check_mount(e2fsck_t ctx)
141 int mount_flags, cont, fd;
143 retval = ext2fs_check_if_mounted(ctx->filesystem_name, &mount_flags);
145 com_err("ext2fs_check_if_mount", retval,
146 "while determining whether %s is mounted.",
147 ctx->filesystem_name);
150 if (!(mount_flags & EXT2_MF_MOUNTED))
153 #if (defined(__linux__) && defined(HAVE_MNTENT_H))
155 * If the root is mounted read-only, then /etc/mtab is
156 * probably not correct; so we won't issue a warning based on
159 fd = open(MOUNTED, O_RDWR);
167 if (ctx->options & E2F_OPT_READONLY) {
168 printf("Warning! %s is mounted.\n", ctx->device_name);
172 printf("%s is mounted.\n\n", ctx->device_name);
173 printf("\007\007\007\007WARNING!!! Running e2fsck on a mounted filesystem "
174 "may cause\nSEVERE filesystem damage.\007\007\007\n\n");
175 if (isatty (0) && isatty (1))
176 cont = ask_yn("Do you really want to continue", -1);
180 printf ("check aborted.\n");
187 * This routine checks to see if a filesystem can be skipped; if so,
188 * it will exit with E2FSCK_OK. Under some conditions it will print a
189 * message explaining why a check is being forced.
191 static void check_if_skip(e2fsck_t ctx)
193 ext2_filsys fs = ctx->fs;
194 const char *reason = NULL;
196 if (force || bad_blocks_file || cflag || swapfs)
199 if (fs->super->s_state & EXT2_ERROR_FS)
200 reason = "contains a file system with errors";
201 else if ((fs->super->s_state & EXT2_VALID_FS) == 0)
202 reason = "was not cleanly unmounted";
203 else if (fs->super->s_mnt_count >=
204 (unsigned) fs->super->s_max_mnt_count)
205 reason = "has reached maximal mount count";
206 else if (fs->super->s_checkinterval &&
207 time(0) >= (fs->super->s_lastcheck +
208 fs->super->s_checkinterval))
209 reason = "has gone too long without being checked";
211 printf("%s %s, check forced.\n", ctx->device_name, reason);
214 printf("%s: clean, %d/%d files, %d/%d blocks\n", ctx->device_name,
215 fs->super->s_inodes_count - fs->super->s_free_inodes_count,
216 fs->super->s_inodes_count,
217 fs->super->s_blocks_count - fs->super->s_free_blocks_count,
218 fs->super->s_blocks_count);
224 * For completion notice
226 static int e2fsck_update_progress(e2fsck_t ctx, int pass,
227 unsigned long cur, unsigned long max)
229 const char spinner[] = "\\|/-";
235 if (ctx->progress_fd) {
236 sprintf(buf, "%d %lu %lu\n", pass, cur, max);
237 write(ctx->progress_fd, buf, strlen(buf));
239 ctx->progress_pos = (ctx->progress_pos+1) & 3;
240 fputc(spinner[ctx->progress_pos], stdout);
247 #define PATH_SET "PATH=/sbin"
249 static void reserve_stdio_fds(NOARGS)
254 fd = open("/dev/null", O_RDWR);
258 fprintf(stderr, "ERROR: Couldn't open /dev/null (%s)\n",
266 static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
271 extern void *mallwatch;
273 char *oldpath = getenv("PATH");
277 retval = e2fsck_allocate_context(&ctx);
283 /* Update our PATH to include /sbin */
287 newpath = (char *) malloc(sizeof (PATH_SET) + 1 +
290 fatal_error(ctx, "Couldn't malloc() newpath");
291 strcpy (newpath, PATH_SET);
292 strcat (newpath, ":");
293 strcat (newpath, oldpath);
298 setbuf(stdout, NULL);
299 setbuf(stderr, NULL);
300 initialize_ext2_error_table();
303 ctx->program_name = *argv;
305 ctx->program_name = "e2fsck";
306 while ((c = getopt (argc, argv, "panyrcC:B:dfvtFVM:b:I:P:l:L:N:Ss")) != EOF)
309 ctx->progress = e2fsck_update_progress;
310 ctx->progress_fd = atoi(optarg);
314 ctx->options |= E2F_OPT_PREEN;
315 ctx->options &= ~(E2F_OPT_YES|E2F_OPT_NO);
318 ctx->options |= E2F_OPT_NO;
319 ctx->options &= ~(E2F_OPT_YES|E2F_OPT_PREEN);
322 ctx->options |= E2F_OPT_YES;
323 ctx->options &= ~(E2F_OPT_PREEN|E2F_OPT_NO);
326 #ifdef RESOURCE_TRACK
327 if (ctx->options & E2F_OPT_TIME)
328 ctx->options |= E2F_OPT_TIME2;
330 ctx->options |= E2F_OPT_TIME;
332 fprintf(stderr, "The -t option is not "
333 "supported on this version of e2fsck.\n");
338 ctx->options |= E2F_OPT_CHECKBLOCKS;
341 /* What we do by default, anyway! */
344 ctx->use_superblock = atoi(optarg);
347 blocksize = atoi(optarg);
350 ctx->inode_buffer_blocks = atoi(optarg);
353 ctx->process_inode_size = atoi(optarg);
356 replace_bad_blocks++;
358 bad_blocks_file = (char *) malloc(strlen(optarg)+1);
359 if (!bad_blocks_file)
361 "Couldn't malloc bad_blocks_file");
362 strcpy(bad_blocks_file, optarg);
365 ctx->options |= E2F_OPT_DEBUG;
374 fatal_error(ctx, "-F not supported");
381 show_version_only = 1;
385 mallwatch = (void *) strtol(optarg, NULL, 0);
389 ctx->device_name = optarg;
392 normalize_swapfs = 1;
399 if (show_version_only)
401 if (optind != argc - 1)
403 if ((ctx->options & E2F_OPT_NO) && !bad_blocks_file &&
405 ctx->options |= E2F_OPT_READONLY;
406 ctx->filesystem_name = argv[optind];
407 if (ctx->device_name == 0)
408 ctx->device_name = ctx->filesystem_name;
411 int fd = open(ctx->filesystem_name, O_RDONLY, 0);
414 com_err("open", errno, "while opening %s for flushing",
415 ctx->filesystem_name);
418 if (ioctl(fd, BLKFLSBUF, 0) < 0) {
419 com_err("BLKFLSBUF", errno, "while trying to flush %s",
420 ctx->filesystem_name);
425 fatal_error(ctx, "BLKFLSBUF not supported");
426 #endif /* BLKFLSBUF */
429 if (cflag || bad_blocks_file) {
430 fprintf(stderr, "Incompatible options not "
431 "allowed when byte-swapping.\n");
438 static const char *my_ver_string = E2FSPROGS_VERSION;
439 static const char *my_ver_date = E2FSPROGS_DATE;
441 int main (int argc, char *argv[])
443 errcode_t retval = 0;
444 int exit_value = FSCK_OK;
449 const char *lib_ver_date;
452 struct problem_context pctx;
453 int flags, run_result;
455 clear_problem_context(&pctx);
462 my_ver = ext2fs_parse_version_string(my_ver_string);
463 lib_ver = ext2fs_get_library_version(0, &lib_ver_date);
464 if (my_ver > lib_ver) {
465 fprintf( stderr, "Error: ext2fs library version "
470 retval = PRS(argc, argv, &ctx);
472 com_err("e2fsck", retval,
473 "while trying to initialize program");
478 #ifdef RESOURCE_TRACK
479 init_resource_track(&ctx->global_rtrack);
482 if (!(ctx->options & E2F_OPT_PREEN) || show_version_only)
483 fprintf (stderr, "e2fsck %s, %s for EXT2 FS %s, %s\n",
484 my_ver_string, my_ver_date, EXT2FS_VERSION,
487 if (show_version_only) {
488 fprintf(stderr, "\tUsing %s, %s\n",
489 error_message(EXT2_ET_BASE), lib_ver_date);
495 if (!(ctx->options & E2F_OPT_PREEN) &&
496 !(ctx->options & E2F_OPT_NO) &&
497 !(ctx->options & E2F_OPT_YES)) {
498 if (!isatty (0) || !isatty (1))
500 "need terminal for interactive repairs");
502 ctx->superblock = ctx->use_superblock;
505 io_ptr = unix_io_manager;
507 io_ptr = test_io_manager;
508 test_io_backing_manager = unix_io_manager;
510 flags = (ctx->options & E2F_OPT_READONLY) ? 0 : EXT2_FLAG_RW;
511 if (ctx->superblock && blocksize) {
512 retval = ext2fs_open(ctx->filesystem_name, flags,
513 ctx->superblock, blocksize, io_ptr, &fs);
514 } else if (ctx->superblock) {
515 for (i=0; possible_block_sizes[i]; i++) {
516 retval = ext2fs_open(ctx->filesystem_name, flags,
518 possible_block_sizes[i],
524 retval = ext2fs_open(ctx->filesystem_name, flags,
526 if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
527 ((retval == EXT2_ET_BAD_MAGIC) ||
528 ((retval == 0) && ext2fs_check_desc(fs)))) {
529 if (!fs || (fs->group_desc_count > 1)) {
530 printf("%s trying backup blocks...\n",
531 retval ? "Couldn't find ext2 superblock," :
532 "Group descriptors look bad...");
533 ctx->superblock = get_backup_sb(fs);
540 com_err(ctx->program_name, retval, "while trying to open %s",
541 ctx->filesystem_name);
542 if (retval == EXT2_ET_REV_TOO_HIGH) {
543 printf("The filesystem revision is apparently "
544 "too high for this version of e2fsck.\n"
545 "(Or the filesystem superblock "
547 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
548 } else if (retval == EXT2_ET_SHORT_READ)
549 printf("Could this be a zero-length partition?\n");
550 else if ((retval == EPERM) || (retval == EACCES))
551 printf("You must have %s access to the "
552 "filesystem or be root\n",
553 (ctx->options & E2F_OPT_READONLY) ?
555 else if (retval == ENXIO)
556 printf("Possibly non-existent or swap device?\n");
558 else if (retval == EROFS)
559 printf("Disk write-protected; use the -n option"
560 "to do a read-only\n"
561 "check of the device.\n");
564 fix_problem(ctx, PR_0_SB_CORRUPT, &pctx);
569 #ifdef EXT2_CURRENT_REV
570 if (fs->super->s_rev_level > E2FSCK_CURRENT_REV) {
571 com_err(ctx->program_name, EXT2_ET_REV_TOO_HIGH,
572 "while trying to open %s",
573 ctx->filesystem_name);
575 fatal_error(ctx, "Get a newer version of e2fsck!");
579 * Check for compatibility with the feature sets. We need to
580 * be more stringent than ext2fs_open().
582 s = (struct ext2fs_sb *) fs->super;
583 if ((s->s_feature_compat & ~EXT2_LIB_FEATURE_COMPAT_SUPP) ||
584 (s->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP)) {
585 com_err(ctx->program_name, EXT2_ET_UNSUPP_FEATURE,
586 "(%s)", ctx->filesystem_name);
589 if (s->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
590 com_err(ctx->program_name, EXT2_ET_RO_UNSUPP_FEATURE,
591 "(%s)", ctx->filesystem_name);
596 * If the user specified a specific superblock, presumably the
597 * master superblock has been trashed. So we mark the
598 * superblock as dirty, so it can be written out.
600 if (ctx->superblock &&
601 !(ctx->options & E2F_OPT_READONLY))
602 ext2fs_mark_super_dirty(fs);
605 * Don't overwrite the backup superblock and block
606 * descriptors, until we're sure the filesystem is OK....
608 fs->flags |= EXT2_FLAG_MASTER_SB_ONLY;
610 ehandler_init(fs->io);
613 set_latch_flags(PR_LATCH_RELOC, PRL_LATCHED, 0);
614 check_super_block(ctx);
615 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
619 read_bad_blocks_file(ctx, bad_blocks_file, replace_bad_blocks);
622 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
625 if (normalize_swapfs) {
626 if ((fs->flags & EXT2_FLAG_SWAP_BYTES) ==
627 ext2fs_native_flag()) {
628 fprintf(stderr, "%s: Filesystem byte order "
629 "already normalized.\n", ctx->device_name);
635 if (ctx->flags & E2F_FLAG_SIGNAL_MASK)
640 * Mark the system as valid, 'til proven otherwise
642 ext2fs_mark_valid(fs);
644 retval = ext2fs_read_bb_inode(fs, &fs->badblocks);
646 com_err(ctx->program_name, retval,
647 "while reading bad blocks inode");
649 printf("This doesn't bode well, but we'll try to go on...\n");
652 run_result = e2fsck_run(ctx);
653 if (run_result == E2F_FLAG_RESTART) {
654 printf("Restarting e2fsck from the beginning...\n");
655 retval = e2fsck_reset_context(ctx);
657 com_err(ctx->program_name, retval,
658 "while resetting context");
664 if (run_result & E2F_FLAG_SIGNAL_MASK)
666 if (run_result & E2F_FLAG_CANCEL)
667 ext2fs_unmark_valid(fs);
670 mtrace_print("Cleanup");
672 if (ext2fs_test_changed(fs)) {
673 exit_value = FSCK_NONDESTRUCT;
674 if (!(ctx->options & E2F_OPT_PREEN))
675 printf("\n%s: ***** FILE SYSTEM WAS MODIFIED *****\n",
677 if (root_filesystem && !read_only_root) {
678 printf("%s: ***** REBOOT LINUX *****\n",
680 exit_value = FSCK_REBOOT;
683 if (ext2fs_test_valid(fs))
684 fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
686 exit_value = FSCK_UNCORRECTED;
687 if (!(ctx->options & E2F_OPT_READONLY)) {
688 if (ext2fs_test_valid(fs)) {
689 if (!(fs->super->s_state & EXT2_VALID_FS))
690 exit_value = FSCK_NONDESTRUCT;
691 fs->super->s_state = EXT2_VALID_FS;
693 fs->super->s_state &= ~EXT2_VALID_FS;
694 fs->super->s_mnt_count = 0;
695 fs->super->s_lastcheck = time(NULL);
696 ext2fs_mark_super_dirty(fs);
700 e2fsck_write_bitmaps(ctx);
702 #ifdef RESOURCE_TRACK
703 if (ctx->options & E2F_OPT_TIME)
704 print_resource_track(NULL, &ctx->global_rtrack);
707 e2fsck_free_context(ctx);