Whamcloud - gitweb
Fix up Andreas's 8k blocksize changes to fix a number of bugs,
[tools/e2fsprogs.git] / misc / fsck.c
index 17ac42d..8c4499e 100644 (file)
@@ -98,6 +98,8 @@ int notitle = 0;
 int parallel_root = 0;
 int progress = 0;
 int force_all_parallel = 0;
+int num_running = 0;
+int max_running = 0;
 char *progname;
 char *fstype = NULL;
 struct fs_info *filesys_info;
@@ -106,16 +108,6 @@ const char *fsck_prefix_path = "/sbin:/sbin/fs.d:/sbin/fs:/etc/fs:/etc";
 char *fsck_path = 0;
 static int ignore(struct fs_info *);
 
-char *string_copy(const char *s)
-{
-       char    *ret;
-
-       ret = malloc(strlen(s)+1);
-       if (ret)
-               strcpy(ret, s);
-       return ret;
-}
-
 static char *skip_over_blank(char *cp)
 {
        while (*cp && isspace(*cp))
@@ -217,18 +209,11 @@ static int parse_fstab_line(char *line, struct fs_info **ret_fs)
  */
 static char *interpret_device(char *spec)
 {
-       char *dev = NULL;
-       
-       if (!strncmp(spec, "UUID=", 5))
-               dev = get_spec_by_uuid(spec+5);
-       else if (!strncmp(spec, "LABEL=", 6))
-               dev = get_spec_by_volume_label(spec+6);
-       else
-               return spec;
-       if (dev) {
-               free(spec);
-               return (dev);
-       }
+       char *dev = interpret_spec(spec);
+
+       if (dev)
+               return dev;
+
        /*
         * Check to see if this was because /proc/partitions isn't
         * found.
@@ -243,15 +228,39 @@ static char *interpret_device(char *spec)
         * Check to see if this is because we're not running as root
         */
        if (geteuid())
-               fprintf(stderr, "Must be root to scan for matching "
-                       "filesystems: %s\n", spec);
+               fprintf(stderr,
+                       "Must be root to scan for matching filesystems: %s\n",
+                       spec);
        else
-               fprintf(stderr, "Couldn't find matching filesystem: %s\n", 
+               fprintf(stderr, "Couldn't find matching filesystem: %s\n",
                        spec);
        exit(EXIT_ERROR);
 }
 
 /*
+ * Interpret filesystem auto type if necessary
+ */
+static void interpret_type(struct fs_info *fs)
+{
+       const char      *type;
+       
+       if (strcmp(fs->type, "auto") == 0 ||
+           (strchr(fs->type, ',') != 0)) {
+               if (fs && strchr(fs->device, '='))
+                       fs->device = interpret_device(fs->device);
+               type = identify_fs(fs->device, fs->type);
+               if (type) {
+                       free(fs->type);
+                       fs->type = string_copy(type);
+               } else
+                       fprintf(stderr, _("Could not determine "
+                                         "filesystem type for %s\n"),
+                               fs->device);
+       }
+}
+
+
+/*
  * Load the filesystem database from /etc/fstab
  */
 static void load_fs_info(const char *filename)
@@ -413,7 +422,8 @@ static int execute(const char *type, char *device, char *mntpt,
        }
 
        if (verbose || noexecute) {
-               printf("[%s -- %s] ", s, mntpt ? mntpt : device);
+               printf("[%s (%d) -- %s] ", s, num_running,
+                      mntpt ? mntpt : device);
                for (i=0; i < argc; i++)
                        printf("%s ", argv[i]);
                printf("\n");
@@ -461,7 +471,7 @@ static int execute(const char *type, char *device, char *mntpt,
  * Wait for one child process to exit; when it does, unlink it from
  * the list of executing child processes, and return it.
  */
-static struct fsck_instance *wait_one(NOARGS)
+static struct fsck_instance *wait_one(int flags)
 {
        int     status;
        int     sig;
@@ -473,9 +483,15 @@ static struct fsck_instance *wait_one(NOARGS)
 
        if (noexecute) {
                inst = instance_list;
-               instance_list = inst->next;
+               prev = 0;
+#ifdef RANDOM_DEBUG
+               while (inst->next && (random() & 1)) {
+                       prev = inst;
+                       inst = inst->next;
+               }
+#endif
                inst->exit_status = 0;
-               return(inst);
+               goto ret_inst;
        }
 
        /*
@@ -485,7 +501,9 @@ static struct fsck_instance *wait_one(NOARGS)
        inst = prev = NULL;
        
        do {
-               pid = wait(&status);
+               pid = waitpid(-1, &status, flags);
+               if ((pid == 0) && (flags & WNOHANG))
+                       return NULL;
                if (pid < 0) {
                        if ((errno == EINTR) || (errno == EAGAIN))
                                continue;
@@ -524,10 +542,6 @@ static struct fsck_instance *wait_one(NOARGS)
                status = EXIT_ERROR;
        }
        inst->exit_status = status;
-       if (prev)
-               prev->next = inst->next;
-       else
-               instance_list = inst->next;
        if (progress && (inst->flags & FLAG_PROGRESS) &&
            !progress_active()) {
                for (inst2 = instance_list; inst2; inst2 = inst2->next) {
@@ -553,6 +567,15 @@ static struct fsck_instance *wait_one(NOARGS)
                        break;
                }
        }
+ret_inst:
+       if (prev)
+               prev->next = inst->next;
+       else
+               instance_list = inst->next;
+       if (verbose > 1)
+               printf(_("Finished with %s (exit status %d)\n"),
+                      inst->device, inst->exit_status);
+       num_running--;
        return inst;
 }
 
@@ -560,17 +583,18 @@ static struct fsck_instance *wait_one(NOARGS)
  * Wait until all executing child processes have exited; return the
  * logical OR of all of their exit code values.
  */
-static int wait_all(NOARGS)
+static int wait_all(int flags)
 {
        struct fsck_instance *inst;
        int     global_status = 0;
 
-       while (instance_list) {
-               inst = wait_one();
-               if (!inst)
-                       break;
+       while ((inst = wait_one(flags))) {
                global_status |= inst->exit_status;
                free_instance(inst);
+#ifdef RANDOM_DEBUG
+               if (noexecute && (flags & WNOHANG) && !(random() % 3))
+                       break;
+#endif
        }
        return global_status;
 }
@@ -598,17 +622,20 @@ static void fsck_device(char *device, int interactive)
 
        if ((fsent = lookup(device))) {
                device = fsent->device;
+               interpret_type(fsent);
                if (!type)
                        type = fsent->type;
        }
        if (!type)
                type = DEFAULT_FSTYPE;
 
+       num_running++;
        retval = execute(type, device, fsent ? fsent->mountpt : 0,
                         interactive);
        if (retval) {
                fprintf(stderr, _("%s: Error %d while executing fsck.%s "
                        "for %s\n"), progname, retval, type, device);
+               num_running--;
        }
 }
 
@@ -729,7 +756,7 @@ static int fs_match(struct fs_info *fs, struct fs_type_compile *cmp)
        if (cmp->list == 0 || cmp->list[0] == 0)
                return 1;
 
-       for (n=0; cp = cmp->list[n]; n++) {
+       for (n=0; (cp = cmp->list[n]); n++) {
                switch (cmp->type[n]) {
                case FS_TYPE_NORMAL:
                        checked_type++;
@@ -764,6 +791,8 @@ static int ignore(struct fs_info *fs)
        if (fs->passno == 0)
                return 1;
 
+       interpret_type(fs);
+
        /*
         * If a specific fstype is specified, and it doesn't match,
         * ignore it.
@@ -851,7 +880,8 @@ static int check_all(NOARGS)
        for (fs = filesys_info; fs; fs = fs->next) {
                if (ignore(fs))
                        fs->flags |= FLAG_DONE;
-               fs->device = interpret_device(fs->device);
+               else
+                       fs->device = interpret_device(fs->device);
        }
                
        /*
@@ -865,7 +895,7 @@ static int check_all(NOARGS)
                if (fs) {
                        if (!skip_root && !ignore(fs)) {
                                fsck_device(fs->device, 1);
-                               status |= wait_all();
+                               status |= wait_all(0);
                                if (status > EXIT_NONDESTRUCT)
                                        return status;
                        }
@@ -904,27 +934,28 @@ static int check_all(NOARGS)
                        fsck_device(fs->device, serialize);
                        fs->flags |= FLAG_DONE;
 
-                       if (serialize) {
+                       /*
+                        * Only do one filesystem at a time, or if we
+                        * have a limit on the number of fsck's extant
+                        * at one time, apply that limit.
+                        */
+                       if (serialize ||
+                           (max_running && (num_running >= max_running))) {
                                pass_done = 0;
-                               break; /* Only do one filesystem at a time */
+                               break;
                        }
                }
                if (verbose > 1)
                        printf(_("--waiting-- (pass %d)\n"), passno);
-               inst = wait_one();
-               if (inst) {
-                       status |= inst->exit_status;
-                       free_instance(inst);
-               }
+               status |= wait_all(pass_done ? 0 : WNOHANG);
                if (pass_done) {
-                       status |= wait_all();
                        if (verbose > 1) 
                                printf("----------------------------------\n");
                        passno++;
                } else
                        not_done_yet++;
        }
-       status |= wait_all();
+       status |= wait_all(0);
        return status;
 }
 
@@ -938,7 +969,7 @@ static void usage(NOARGS)
 static void PRS(int argc, char *argv[])
 {
        int     i, j;
-       char    *arg;
+       char    *arg, *tmp;
        char    options[128];
        int     opt = 0;
        int     opts_for_fsck = 0;
@@ -1008,19 +1039,17 @@ static void PRS(int argc, char *argv[])
                                serialize++;
                                break;
                        case 't':
-                               if (arg[j+1]) {
-                                       fstype = string_copy(arg+j+1);
-                                       compile_fs_type(fstype, &fs_type_compiled);
-                                       goto next_arg;
-                               }
-                               if ((i+1) < argc) {
-                                       i++;
-                                       fstype = string_copy(argv[i]);
-                                       compile_fs_type(fstype, &fs_type_compiled);
-                                       goto next_arg;
-                               }
-                               usage();
-                               break;
+                               if (fstype)
+                                       usage();
+                               if (arg[j+1])
+                                       tmp = arg+j+1;
+                               else if ((i+1) < argc)
+                                       tmp = argv[++i];
+                               else
+                                       usage();
+                               fstype = string_copy(tmp);
+                               compile_fs_type(fstype, &fs_type_compiled);
+                               goto next_arg;
                        case '-':
                                opts_for_fsck++;
                                break;
@@ -1048,6 +1077,8 @@ static void PRS(int argc, char *argv[])
        }
        if (getenv("FSCK_FORCE_ALL_PARALLEL"))
                force_all_parallel++;
+       if ((tmp = getenv("FSCK_MAX_INST")))
+           max_running = atoi(tmp);
 }
 
 int main(int argc, char *argv[])
@@ -1060,14 +1091,14 @@ int main(int argc, char *argv[])
 
 #ifdef ENABLE_NLS
        setlocale(LC_MESSAGES, "");
+       setlocale(LC_CTYPE, "");
        bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
        textdomain(NLS_CAT_NAME);
 #endif
        PRS(argc, argv);
 
        if (!notitle)
-               printf(_("Parallelizing fsck version %s (%s)\n"),
-                       E2FSPROGS_VERSION, E2FSPROGS_DATE);
+               printf("fsck %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
 
        fstab = getenv("FSTAB_FILE");
        if (!fstab)
@@ -1092,19 +1123,23 @@ int main(int argc, char *argv[])
        if (doall)
                return check_all();
 
+       if (num_devices == 0) {
+               fprintf(stderr, _("\nNo devices specified to be checked!\n"));
+               exit(EXIT_ERROR);
+       }
        for (i = 0 ; i < num_devices; i++) {
                fsck_device(devices[i], interactive);
-               if (serialize) {
+               if (serialize || (num_running >= max_running)) {
                        struct fsck_instance *inst;
 
-                       inst = wait_one();
+                       inst = wait_one(0);
                        if (inst) {
                                status |= inst->exit_status;
                                free_instance(inst);
                        }
                }
        }
-       status |= wait_all();
+       status |= wait_all(0);
        free(fsck_path);
        return status;
 }