Whamcloud - gitweb
e2fsck: misc cleanups for pfsck
[tools/e2fsprogs.git] / e2fsck / unix.c
index f267bae..d33a21c 100644 (file)
@@ -52,6 +52,7 @@ extern int optind;
 #include "e2p/e2p.h"
 #include "uuid/uuid.h"
 #include "support/plausible.h"
+#include "support/devname.h"
 #include "e2fsck.h"
 #include "problem.h"
 #include "jfs_user.h"
@@ -75,13 +76,16 @@ int journal_enable_debug = -1;
 static void usage(e2fsck_t ctx)
 {
        fprintf(stderr,
-               _("Usage: %s [-panyrcdfktvDFV] [-b superblock] [-B blocksize]\n"
+               _("Usage: %s [-pamnyrcdfktvDFV] [-b superblock] [-B blocksize]\n"
                "\t\t[-l|-L bad_blocks_file] [-C fd] [-j external_journal]\n"
                "\t\t[-E extended-options] [-z undo_file] device\n"),
-               ctx->program_name);
+               ctx->program_name ? ctx->program_name : "e2fsck");
 
        fprintf(stderr, "%s", _("\nEmergency help:\n"
                " -p                   Automatic repair (no questions)\n"
+#ifdef HAVE_PTHREAD
+               " -m                   multiple threads to speedup fsck\n"
+#endif
                " -n                   Make no changes to the filesystem\n"
                " -y                   Assume \"yes\" to all questions\n"
                " -c                   Check for bad blocks and add them to the badblock list\n"
@@ -823,6 +827,10 @@ static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
 #ifdef CONFIG_JBD_DEBUG
        char            *jbd_debug;
 #endif
+#ifdef HAVE_PTHREAD
+       char            *pm;
+       unsigned long   thread_num;
+#endif
        unsigned long long phys_mem_kb, blk;
 
        retval = e2fsck_allocate_context(&ctx);
@@ -849,11 +857,16 @@ static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
        if (argc && *argv)
                ctx->program_name = *argv;
        else
-               ctx->program_name = "e2fsck";
+               usage(NULL);
 
        phys_mem_kb = get_memory_size() / 1024;
        ctx->readahead_kb = ~0ULL;
+
+#ifdef HAVE_PTHREAD
+       while ((c = getopt(argc, argv, "pam:nyrcC:B:dE:fvtFVM:b:I:j:P:l:L:N:SsDkz:")) != EOF)
+#else
        while ((c = getopt(argc, argv, "panyrcC:B:dE:fvtFVM:b:I:j:P:l:L:N:SsDkz:")) != EOF)
+#endif
                switch (c) {
                case 'C':
                        ctx->progress = e2fsck_update_progress;
@@ -894,6 +907,22 @@ static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
                        }
                        ctx->options |= E2F_OPT_PREEN;
                        break;
+#ifdef HAVE_PTHREAD
+               case 'm':
+                       thread_num = strtoul(optarg, &pm, 0);
+                       if (*pm)
+                               fatal_error(ctx,
+                                       _("Invalid multiple thread num.\n"));
+                       if (thread_num > E2FSCK_MAX_THREADS) {
+                               fprintf(stderr,
+                                       _("threads %lu too large (max %u)\n"),
+                                       thread_num, E2FSCK_MAX_THREADS);
+                               fatal_error(ctx, 0);
+                       }
+                       ctx->options |= E2F_OPT_MULTITHREAD;
+                       ctx->pfs_num_threads = thread_num;
+                       break;
+#endif
                case 'n':
                        if (ctx->options & (E2F_OPT_YES|E2F_OPT_PREEN))
                                goto conflict_opt;
@@ -939,8 +968,8 @@ static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
                                goto sscanf_err;
                        break;
                case 'j':
-                       ctx->journal_name = blkid_get_devname(ctx->blkid,
-                                                             optarg, NULL);
+                       ctx->journal_name = get_devname(ctx->blkid,
+                                                       optarg, NULL);
                        if (!ctx->journal_name) {
                                com_err(ctx->program_name, 0,
                                        _("Unable to resolve '%s'"),
@@ -1013,13 +1042,27 @@ static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
                        _("The -n and -l/-L options are incompatible."));
                fatal_error(ctx, 0);
        }
+#ifdef HAVE_PTHREAD
+       if (ctx->options & E2F_OPT_MULTITHREAD) {
+               if ((ctx->options & (E2F_OPT_YES|E2F_OPT_NO|E2F_OPT_PREEN)) == 0) {
+                       com_err(ctx->program_name, 0, "%s",
+                               _("The -m option should be used together with one of -p/-y/-n options."));
+                       fatal_error(ctx, 0);
+               }
+               if (ctx->progress) {
+                       com_err(ctx->program_name, 0, "%s",
+                               _("Only one of the options -C or -m may be specified."));
+                       fatal_error(ctx, 0);
+               }
+       }
+#endif
        if (ctx->options & E2F_OPT_NO)
                ctx->options |= E2F_OPT_READONLY;
 
        ctx->io_options = strchr(argv[optind], '?');
        if (ctx->io_options)
                *ctx->io_options++ = 0;
-       ctx->filesystem_name = blkid_get_devname(ctx->blkid, argv[optind], 0);
+       ctx->filesystem_name = get_devname(ctx->blkid, argv[optind], 0);
        if (!ctx->filesystem_name) {
                com_err(ctx->program_name, 0, _("Unable to resolve '%s'"),
                        argv[optind]);
@@ -1119,10 +1162,12 @@ static errcode_t PRS(int argc, char *argv[], e2fsck_t *ret_ctx)
 #ifdef SA_RESTART
        sa.sa_flags = SA_RESTART;
 #endif
-       sa.sa_handler = signal_progress_on;
-       sigaction(SIGUSR1, &sa, 0);
-       sa.sa_handler = signal_progress_off;
-       sigaction(SIGUSR2, &sa, 0);
+       if ((ctx->options & E2F_OPT_MULTITHREAD) == 0) {
+               sa.sa_handler = signal_progress_on;
+               sigaction(SIGUSR1, &sa, 0);
+               sa.sa_handler = signal_progress_off;
+               sigaction(SIGUSR2, &sa, 0);
+       }
 #endif
 
        /* Update our PATH to include /sbin if we need to run badblocks  */
@@ -1171,25 +1216,32 @@ static errcode_t try_open_fs(e2fsck_t ctx, int flags, io_manager io_ptr,
        errcode_t retval;
 
        *ret_fs = NULL;
-       if (ctx->superblock && ctx->blocksize) {
-               retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options,
-                                     flags, ctx->superblock, ctx->blocksize,
-                                     io_ptr, ret_fs);
-       } else if (ctx->superblock) {
-               int blocksize;
-               for (blocksize = EXT2_MIN_BLOCK_SIZE;
-                    blocksize <= EXT2_MAX_BLOCK_SIZE; blocksize *= 2) {
-                       if (*ret_fs) {
-                               ext2fs_free(*ret_fs);
-                               *ret_fs = NULL;
+
+       if (ctx->superblock) {
+               unsigned long blocksize = ctx->blocksize;
+
+               if (!blocksize) {
+                       for (blocksize = EXT2_MIN_BLOCK_SIZE;
+                            blocksize <= EXT2_MAX_BLOCK_SIZE; blocksize *= 2) {
+
+                               retval = ext2fs_open2(ctx->filesystem_name,
+                                                     ctx->io_options, flags,
+                                                     ctx->superblock, blocksize,
+                                                     unix_io_manager, ret_fs);
+                               if (*ret_fs) {
+                                       ext2fs_free(*ret_fs);
+                                       *ret_fs = NULL;
+                               }
+                               if (!retval)
+                                       break;
                        }
-                       retval = ext2fs_open2(ctx->filesystem_name,
-                                             ctx->io_options, flags,
-                                             ctx->superblock, blocksize,
-                                             io_ptr, ret_fs);
-                       if (!retval)
-                               break;
+                       if (retval)
+                               return retval;
                }
+
+               retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options,
+                                     flags, ctx->superblock, blocksize,
+                                     io_ptr, ret_fs);
        } else
                retval = ext2fs_open2(ctx->filesystem_name, ctx->io_options,
                                      flags, 0, 0, io_ptr, ret_fs);
@@ -1434,6 +1486,7 @@ int main (int argc, char *argv[])
        }
        reserve_stdio_fds();
 
+       ctx->global_ctx = NULL;
        set_up_logging(ctx);
        if (ctx->logf) {
                int i;
@@ -1513,6 +1566,7 @@ restart:
        }
 
        ctx->openfs_flags = flags;
+       ctx->io_manager = io_ptr;
        retval = try_open_fs(ctx, flags, io_ptr, &fs);
 
        if (!ctx->superblock && !(ctx->options & E2F_OPT_PREEN) &&
@@ -1619,7 +1673,8 @@ failure:
                         * so that we are able to recover from more errors
                         * (e.g. some tool messing up some value in the sb).
                         */
-                       if ((retval == EXT2_ET_CORRUPT_SUPERBLOCK) &&
+                       if (((retval == EXT2_ET_CORRUPT_SUPERBLOCK) ||
+                            (retval == EXT2_ET_BAD_DESC_SIZE)) &&
                            !(flags & EXT2_FLAG_IGNORE_SB_ERRORS)) {
                                if (fs)
                                        ext2fs_close_free(&fs);