Whamcloud - gitweb
Fix up Andreas's 8k blocksize changes to fix a number of bugs,
[tools/e2fsprogs.git] / misc / fsck.c
index 8084351..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 *);
 
-static 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.
@@ -237,21 +222,45 @@ static char *interpret_device(char *spec)
                fprintf(stderr, "Couldn't open /proc/partitions: %s\n",
                        strerror(errno));
                fprintf(stderr, "Is /proc mounted?\n");
-               exit(1);
+               exit(EXIT_ERROR);
        }
        /*
         * 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(1);
+       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;
 }
@@ -591,50 +615,169 @@ static void fsck_device(char *device, int interactive)
        struct fs_info *fsent;
        int retval;
 
-       if (fstype && strncmp(fstype, "no", 2) && !strchr(fstype, ','))
+       if (fstype && strncmp(fstype, "no", 2) &&
+           strncmp(fstype, "opts=", 5) && strncmp(fstype, "loop", 4) && 
+           !strchr(fstype, ','))
                type = fstype;
 
        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--;
        }
 }
 
-/* See if filesystem type matches the list. */
-static int fs_match(char *type, char *fs_type)
+
+/*
+ * Deal with the fsck -t argument.
+ */
+struct fs_type_compile {
+       char **list;
+       int *type;
+       int  negate;
+} fs_type_compiled;
+
+#define FS_TYPE_NORMAL 0
+#define FS_TYPE_OPT    1
+#define FS_TYPE_NEGOPT 2
+
+static const char *fs_type_syntax_error =
+N_("Either all or none of the filesystem types passed to -t must be prefixed\n"
+   "with 'no' or '!'.\n");
+
+static void compile_fs_type(char *fs_type, struct fs_type_compile *cmp)
 {
-  int ret = 0, negate = 0;
-  char list[128];
-  char *s;
+       char    *cp, *list, *s;
+       int     num = 2;
+       int     negate, first_negate = 1;
+
+       if (fs_type) {
+               for (cp=fs_type; *cp; cp++) {
+                       if (*cp == ',')
+                               num++;
+               }
+       }
 
-  if (!fs_type) return(1);
+       cmp->list = malloc(num * sizeof(char *));
+       cmp->type = malloc(num * sizeof(int));
+       if (!cmp->list || !cmp->type) {
+               fprintf(stderr, _("Couldn't allocate memory for "
+                                 "filesystem types\n"));
+               exit(EXIT_ERROR);
+       }
+       memset(cmp->list, 0, num * sizeof(char *));
+       memset(cmp->type, 0, num * sizeof(int));
+       cmp->negate = 0;
 
-  if (strncmp(fs_type, "no", 2) == 0) {
-       fs_type += 2;
-       negate = 1;
-  }
-  strcpy(list, fs_type);
-  s = strtok(list, ",");
-  while(s) {
-       if (strcmp(s, type) == 0) {
-               ret = 1;
-               break;
-       }
-       s = strtok(NULL, ",");
-  }
-  return(negate ? !ret : ret);
+       if (!fs_type)
+               return;
+       
+       list = string_copy(fs_type);
+       num = 0;
+       s = strtok(list, ",");
+       while(s) {
+               negate = 0;
+               if (strncmp(s, "no", 2) == 0) {
+                       s += 2;
+                       negate = 1;
+               } else if (*s == '!') {
+                       s++;
+                       negate = 1;
+               }
+               if (strcmp(s, "loop") == 0)
+                       /* loop is really short-hand for opts=loop */
+                       goto loop_special_case;
+               else if (strncmp(s, "opts=", 5) == 0) {
+                       s += 5;
+               loop_special_case:
+                       cmp->type[num] = negate ? FS_TYPE_NEGOPT : FS_TYPE_OPT;
+               } else {
+                       if (first_negate) {
+                               cmp->negate = negate;
+                               first_negate = 0;
+                       }
+                       if ((negate && !cmp->negate) ||
+                           (!negate && cmp->negate)) {
+                               fprintf(stderr, _(fs_type_syntax_error));
+                               exit(EXIT_USAGE);
+                       }
+               }
+#if 0
+               printf("Adding %s to list (type %d).\n", s, cmp->type[num]);
+#endif
+               cmp->list[num++] = string_copy(s);
+               s = strtok(NULL, ",");
+       }
+       free(list);
 }
 
+/*
+ * This function returns true if a particular option appears in a
+ * comma-delimited options list
+ */
+static int opt_in_list(char *opt, char *optlist)
+{
+       char    *list, *s;
+
+       if (!optlist)
+               return 0;
+       list = string_copy(optlist);
+       
+       s = strtok(list, ",");
+       while(s) {
+               if (strcmp(s, opt) == 0) {
+                       free(list);
+                       return 1;
+               }
+               s = strtok(NULL, ",");
+       }
+        free(list);
+       return 0;
+}
+
+/* See if the filesystem matches the criteria given by the -t option */
+static int fs_match(struct fs_info *fs, struct fs_type_compile *cmp)
+{
+       int n, ret = 0, checked_type = 0;
+       char *cp;
+
+       if (cmp->list == 0 || cmp->list[0] == 0)
+               return 1;
+
+       for (n=0; (cp = cmp->list[n]); n++) {
+               switch (cmp->type[n]) {
+               case FS_TYPE_NORMAL:
+                       checked_type++;
+                       if (strcmp(cp, fs->type) == 0) {
+                               ret = 1;
+                       }
+                       break;
+               case FS_TYPE_NEGOPT:
+                       if (opt_in_list(cp, fs->opts))
+                               return 0;
+                       break;
+               case FS_TYPE_OPT:
+                       if (!opt_in_list(cp, fs->opts))
+                               return 0;
+                       break;
+               }
+       }
+       if (checked_type == 0)
+               return 1;
+       return (cmp->negate ? !ret : ret);
+}
 
 /* Check if we should ignore this filesystem. */
 static int ignore(struct fs_info *fs)
@@ -648,11 +791,13 @@ 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.
         */
-       if (!fs_match(fs->type, fstype)) return 1;
+       if (!fs_match(fs, &fs_type_compiled)) return 1;
        
        /* Are we ignoring this type? */
        for(ip = ignored_types; *ip; ip++)
@@ -698,10 +843,14 @@ static int device_already_active(char *device)
 #endif
 
        base = base_device(device);
-       if (!base)
-               return 0;
+       /*
+        * If we don't know the base device, assume that the device is
+        * already active if there are any fsck instances running.
+        */
+       if (!base) 
+               return (instance_list != 0);
        for (inst = instance_list; inst; inst = inst->next) {
-               if (!strcmp(base, inst->base_device)) {
+               if (!inst->base_device || !strcmp(base, inst->base_device)) {
                        free(base);
                        return 1;
                }
@@ -731,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);
        }
                
        /*
@@ -745,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;
                        }
@@ -784,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;
 }
 
@@ -818,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;
@@ -839,7 +990,7 @@ static void PRS(int argc, char *argv[])
                        if (num_devices >= MAX_DEVICES) {
                                fprintf(stderr, _("%s: too many devices\n"),
                                        progname);
-                               exit(1);
+                               exit(EXIT_ERROR);
                        }
                        devices[num_devices++] =
                                interpret_device(string_copy(arg));
@@ -849,7 +1000,7 @@ static void PRS(int argc, char *argv[])
                        if (num_args >= MAX_ARGS) {
                                fprintf(stderr, _("%s: too many arguments\n"),
                                        progname);
-                               exit(1);
+                               exit(EXIT_ERROR);
                        }
                        args[num_args++] = string_copy(arg);
                        continue;
@@ -888,20 +1039,23 @@ static void PRS(int argc, char *argv[])
                                serialize++;
                                break;
                        case 't':
-                               if (arg[j+1]) {
-                                       fstype = string_copy(arg+j+1);
-                                       goto next_arg;
-                               }
-                               if ((i+1) < argc) {
-                                       i++;
-                                       fstype = string_copy(argv[i]);
-                                       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;
+                       case '?':
+                               usage();
+                               break;
                        default:
                                options[++opt] = arg[j];
                                break;
@@ -915,7 +1069,7 @@ static void PRS(int argc, char *argv[])
                                fprintf(stderr,
                                        _("%s: too many arguments\n"),
                                        progname);
-                               exit(1);
+                               exit(EXIT_ERROR);
                        }
                        args[num_args++] = string_copy(options);
                        opt = 0;
@@ -923,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[])
@@ -935,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)
@@ -967,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;
 }