Whamcloud - gitweb
ChangeLog, tune2fs.c:
[tools/e2fsprogs.git] / misc / fsck.c
1 /*
2  * pfsck --- A generic, parallelizing front-end for the fsck program.
3  * It will automatically try to run fsck programs in parallel if the
4  * devices are on separate spindles.  It is based on the same ideas as
5  * the generic front end for fsck by David Engel and Fred van Kempen,
6  * but it has been completely rewritten from scratch to support
7  * parallel execution.
8  *
9  * Written by Theodore Ts'o, <tytso@mit.edu>
10  * 
11  * Usage:       fsck [-ACVRNTM] [-s] [-t fstype] [fs-options] device
12  * 
13  * Miquel van Smoorenburg (miquels@drinkel.ow.org) 20-Oct-1994:
14  *   o Changed -t fstype to behave like with mount when -A (all file
15  *     systems) or -M (like mount) is specified.
16  *   o fsck looks if it can find the fsck.type program to decide
17  *     if it should ignore the fs type. This way more fsck programs
18  *     can be added without changing this front-end.
19  *   o -R flag skip root file system.
20  *
21  * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999 Theodore Ts'o.
22  *
23  * %Begin-Header%
24  * This file may be redistributed under the terms of the GNU Public
25  * License.
26  * %End-Header%
27  */
28
29 #include <sys/types.h>
30 #include <sys/wait.h>
31 #include <sys/signal.h>
32 #include <sys/stat.h>
33 #include <limits.h>
34 #include <stdio.h>
35 #include <ctype.h>
36 #include <string.h>
37 #include <time.h>
38 #if HAVE_STDLIB_H
39 #include <stdlib.h>
40 #endif
41 #if HAVE_ERRNO_H
42 #include <errno.h>
43 #endif
44 #if HAVE_PATHS_H
45 #include <paths.h>
46 #endif
47 #if HAVE_UNISTD_H
48 #include <unistd.h>
49 #endif
50 #if HAVE_ERRNO_H
51 #include <errno.h>
52 #endif
53 #include <malloc.h>
54
55 #include "../version.h"
56 #include "fsck.h"
57 #include "get_device_by_label.h"
58
59 #ifndef _PATH_MNTTAB
60 #define _PATH_MNTTAB    "/etc/fstab"
61 #endif
62
63 static const char *ignored_types[] = {
64         "ignore",
65         "iso9660",
66         "nfs",
67         "proc",
68         "sw",
69         "swap",
70         NULL
71 };
72
73 static const char *really_wanted[] = {
74         "minix",
75         "ext2",
76         "xiafs",
77         NULL
78 };
79
80 #ifdef DEV_DSK_DEVICES
81 static const char *base_devices[] = {
82         "/dev/dsk/hda",
83         "/dev/dsk/hdb",
84         "/dev/dsk/hdc",
85         "/dev/dsk/hdd",
86         "/dev/dsk/hd1a",
87         "/dev/dsk/hd1b",
88         "/dev/dsk/hd1c",
89         "/dev/dsk/hd1d",
90         "/dev/dsk/sda",
91         "/dev/dsk/sdb",
92         "/dev/dsk/sdc",
93         "/dev/dsk/sdd",
94         "/dev/dsk/sde",
95         "/dev/dsk/sdf",
96         "/dev/dsk/sdg",
97         NULL
98 };
99 #else
100 static const char *base_devices[] = {
101         "/dev/hda",
102         "/dev/hdb",
103         "/dev/hdc",
104         "/dev/hdd",
105         "/dev/hd1a",
106         "/dev/hd1b",
107         "/dev/hd1c",
108         "/dev/hd1d",
109         "/dev/sda",
110         "/dev/sdb",
111         "/dev/sdc",
112         "/dev/sdd",
113         "/dev/sde",
114         "/dev/sdf",
115         "/dev/sdg",
116         NULL
117 };
118 #endif
119
120 /*
121  * Global variables for options
122  */
123 char *devices[MAX_DEVICES];
124 char *args[MAX_ARGS];
125 int num_devices, num_args;
126
127 int verbose = 0;
128 int doall = 0;
129 int noexecute = 0;
130 int serialize = 0;
131 int skip_root = 0;
132 int like_mount = 0;
133 int notitle = 0;
134 int parallel_root = 0;
135 int progress = 0;
136 int force_all_parallel = 0;
137 char *progname;
138 char *fstype = NULL;
139 struct fs_info *filesys_info;
140 struct fsck_instance *instance_list;
141 const char *fsck_prefix_path = "/sbin:/sbin/fs.d:/sbin/fs:/etc/fs:/etc";
142 char *fsck_path = 0;
143 static int ignore(struct fs_info *);
144
145 static char *string_copy(const char *s)
146 {
147         char    *ret;
148
149         ret = malloc(strlen(s)+1);
150         if (ret)
151                 strcpy(ret, s);
152         return ret;
153 }
154
155 static char *skip_over_blank(char *cp)
156 {
157         while (*cp && isspace(*cp))
158                 cp++;
159         return cp;
160 }
161
162 static char *skip_over_word(char *cp)
163 {
164         while (*cp && !isspace(*cp))
165                 cp++;
166         return cp;
167 }
168
169 static void strip_line(char *line)
170 {
171         char    *p;
172
173         while (*line) {
174                 p = line + strlen(line) - 1;
175                 if ((*p == '\n') || (*p == '\r'))
176                         *p = 0;
177                 else
178                         break;
179         }
180 }
181
182 static char *parse_word(char **buf)
183 {
184         char *word, *next;
185
186         word = *buf;
187         if (*word == 0)
188                 return 0;
189
190         word = skip_over_blank(word);
191         next = skip_over_word(word);
192         if (*next)
193                 *next++ = 0;
194         *buf = next;
195         return word;
196 }
197
198 static void free_instance(struct fsck_instance *i)
199 {
200         if (i->prog)
201                 free(i->prog);
202         if (i->device)
203                 free(i->device);
204         free(i);
205         return;
206 }
207
208 static int parse_fstab_line(char *line, struct fs_info **ret_fs)
209 {
210         char    *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
211         struct fs_info *fs;
212
213         *ret_fs = 0;
214         strip_line(line);
215         if ((cp = strchr(line, '#')))
216                 *cp = 0;        /* Ignore everything after the comment char */
217         cp = line;
218
219         device = parse_word(&cp);
220         mntpnt = parse_word(&cp);
221         type = parse_word(&cp);
222         opts = parse_word(&cp);
223         freq = parse_word(&cp);
224         passno = parse_word(&cp);
225
226         if (!device)
227                 return 0;       /* Allow blank lines */
228         
229         if (!mntpnt || !type)
230                 return -1;
231         
232         if (!(fs = malloc(sizeof(struct fs_info))))
233                 return -1;
234
235         fs->device = string_copy(device);
236         fs->mountpt = string_copy(mntpnt);
237         fs->type = string_copy(type);
238         fs->opts = string_copy(opts ? opts : "");
239         fs->freq = freq ? atoi(freq) : -1;
240         fs->passno = passno ? atoi(passno) : -1;
241         fs->flags = 0;
242         fs->next = NULL;
243
244         *ret_fs = fs;
245
246         return 0;
247 }
248
249 /*
250  * Interpret the device name if necessary 
251  */
252 static char *interpret_device(char *spec)
253 {
254         char *dev = NULL;
255         
256         if (!strncmp(spec, "UUID=", 5))
257                 dev = get_spec_by_uuid(spec+5);
258         else if (!strncmp(spec, "LABEL=", 6))
259                 dev = get_spec_by_volume_label(spec+6);
260         if (dev) {
261                 free(spec);
262                 spec = dev;
263         }
264         return spec;
265 }
266
267 /*
268  * Load the filesystem database from /etc/fstab
269  */
270 static void load_fs_info(const char *filename)
271 {
272         FILE    *f;
273         char    buf[1024];
274         int     lineno = 0;
275         int     old_fstab = 1;
276         struct fs_info *fs, *fs_last = NULL;
277
278         filesys_info = NULL;
279         if ((f = fopen(filename, "r")) == NULL) {
280                 fprintf(stderr, "WARNING: couldn't open %s: %s\n",
281                         filename, strerror(errno));
282                 return;
283         }
284         while (!feof(f)) {
285                 lineno++;
286                 if (!fgets(buf, sizeof(buf), f))
287                         break;
288                 buf[sizeof(buf)-1] = 0;
289                 if (parse_fstab_line(buf, &fs) < 0) {
290                         fprintf(stderr, "WARNING: bad format "
291                                 "on line %d of %s\n", lineno, filename);
292                         continue;
293                 }
294                 if (!fs)
295                         continue;
296                 fs->device = interpret_device(fs->device);
297                 if (!filesys_info)
298                         filesys_info = fs;
299                 else
300                         fs_last->next = fs;
301                 fs_last = fs;
302                 if (fs->passno < 0)
303                         fs->passno = 0;
304                 else
305                         old_fstab = 0;
306         }
307         
308         fclose(f);
309         
310         if (old_fstab) {
311                 fprintf(stderr, "\007\007\007"
312         "WARNING: Your /etc/fstab does not contain the fsck passno\n");
313                 fprintf(stderr,
314         "       field.  I will kludge around things for you, but you\n");
315                 fprintf(stderr,
316         "       should fix your /etc/fstab file as soon as you can.\n\n");
317                 
318                 for (fs = filesys_info; fs; fs = fs->next) {
319                         fs->passno = 1;
320                 }
321         }
322 }
323         
324 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
325 static struct fs_info *lookup(char *filesys)
326 {
327         struct fs_info *fs;
328
329         /* No filesys name given. */
330         if (filesys == NULL)
331                 return NULL;
332
333         for (fs = filesys_info; fs; fs = fs->next) {
334                 if (!strcmp(filesys, fs->device) ||
335                     !strcmp(filesys, fs->mountpt))
336                         break;
337         }
338
339         return fs;
340 }
341
342 /* Find fsck program for a given fs type. */
343 static char *find_fsck(char *type)
344 {
345   char *s;
346   const char *tpl;
347   static char prog[256];
348   char *p = string_copy(fsck_path);
349   struct stat st;
350
351   /* Are we looking for a program or just a type? */
352   tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
353
354   for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
355         sprintf(prog, tpl, s, type);
356         if (stat(prog, &st) == 0) break;
357   }
358   free(p);
359   return(s ? prog : NULL);
360 }
361
362 static int progress_active(NOARGS)
363 {
364         struct fsck_instance *inst;
365
366         for (inst = instance_list; inst; inst = inst->next) {
367                 if (inst->flags & FLAG_DONE)
368                         continue;
369                 if (inst->flags & FLAG_PROGRESS)
370                         return 1;
371         }
372         return 0;
373 }
374
375 /*
376  * Execute a particular fsck program, and link it into the list of
377  * child processes we are waiting for.
378  */
379 static int execute(const char *type, char *device, char *mntpt,
380                    int interactive)
381 {
382         char *s, *argv[80], prog[80];
383         int  argc, i;
384         struct fsck_instance *inst, *p;
385         pid_t   pid;
386
387         inst = malloc(sizeof(struct fsck_instance));
388         if (!inst)
389                 return ENOMEM;
390         memset(inst, 0, sizeof(struct fsck_instance));
391
392         sprintf(prog, "fsck.%s", type);
393         argv[0] = string_copy(prog);
394         argc = 1;
395         
396         for (i=0; i <num_args; i++)
397                 argv[argc++] = string_copy(args[i]);
398
399         if (progress & !progress_active()) {
400                 if (strcmp(type, "ext2") == 0) {
401                         argv[argc++] = string_copy("-C0");
402                         inst->flags |= FLAG_PROGRESS;
403                 }
404         }
405
406         argv[argc++] = string_copy(device);
407         argv[argc] = 0;
408
409         s = find_fsck(prog);
410         if (s == NULL) {
411                 fprintf(stderr, "fsck: %s: not found\n", prog);
412                 return ENOENT;
413         }
414
415         if (verbose || noexecute) {
416                 printf("[%s -- %s] ", s, mntpt ? mntpt : device);
417                 for (i=0; i < argc; i++)
418                         printf("%s ", argv[i]);
419                 printf("\n");
420         }
421         
422         /* Fork and execute the correct program. */
423         if (noexecute)
424                 pid = -1;
425         else if ((pid = fork()) < 0) {
426                 perror("fork");
427                 return errno;
428         } else if (pid == 0) {
429                 if (!interactive)
430                         close(0);
431                 (void) execv(s, argv);
432                 perror(argv[0]);
433                 exit(EXIT_ERROR);
434         }
435
436         for (i=0; i < argc; i++)
437                 free(argv[i]);
438         
439         inst->pid = pid;
440         inst->prog = string_copy(prog);
441         inst->type = string_copy(type);
442         inst->device = string_copy(device);
443         inst->start_time = time(0);
444         inst->next = NULL;
445
446         /*
447          * Find the end of the list, so we add the instance on at the end.
448          */
449         for (p = instance_list; p && p->next; p = p->next);
450
451         if (p)
452                 p->next = inst;
453         else
454                 instance_list = inst;
455         
456         return 0;
457 }
458
459 /*
460  * Wait for one child process to exit; when it does, unlink it from
461  * the list of executing child processes, and return it.
462  */
463 static struct fsck_instance *wait_one(NOARGS)
464 {
465         int     status;
466         int     sig;
467         struct fsck_instance *inst, *inst2, *prev;
468         pid_t   pid;
469
470         if (!instance_list)
471                 return NULL;
472
473         if (noexecute) {
474                 inst = instance_list;
475                 instance_list = inst->next;
476                 inst->exit_status = 0;
477                 return(inst);
478         }
479
480         /*
481          * gcc -Wall fails saving throw against stupidity
482          * (inst and prev are thought to be uninitialized variables)
483          */
484         inst = prev = NULL;
485         
486         do {
487                 pid = wait(&status);
488                 if (pid < 0) {
489                         if ((errno == EINTR) || (errno == EAGAIN))
490                                 continue;
491                         if (errno == ECHILD) {
492                                 fprintf(stderr,
493                                         "%s: wait: No more child process?!?\n",
494                                         progname);
495                                 return NULL;
496                         }
497                         perror("wait");
498                         continue;
499                 }
500                 for (prev = 0, inst = instance_list;
501                      inst;
502                      prev = inst, inst = inst->next) {
503                         if (inst->pid == pid)
504                                 break;
505                 }
506         } while (!inst);
507
508         if (WIFEXITED(status)) 
509                 status = WEXITSTATUS(status);
510         else if (WIFSIGNALED(status)) {
511                 sig = WTERMSIG(status);
512                 if (sig == SIGINT) {
513                         status = EXIT_UNCORRECTED;
514                 } else {
515                         printf("Warning... %s for device %s exited "
516                                "with signal %d.\n",
517                                inst->prog, inst->device, sig);
518                         status = EXIT_ERROR;
519                 }
520         } else {
521                 printf("%s %s: status is %x, should never happen.\n",
522                        inst->prog, inst->device, status);
523                 status = EXIT_ERROR;
524         }
525         inst->exit_status = status;
526         if (prev)
527                 prev->next = inst->next;
528         else
529                 instance_list = inst->next;
530         if (progress && (inst->flags & FLAG_PROGRESS) &&
531             !progress_active()) {
532                 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
533                         if (inst2->flags & FLAG_DONE)
534                                 continue;
535                         if (strcmp(inst2->type, "ext2"))
536                                 continue;
537                         /*
538                          * If we've just started the fsck, wait a tiny
539                          * bit before sending the kill, to give it
540                          * time to set up the signal handler
541                          */
542                         if (inst2->start_time < time(0)+2) {
543                                 if (fork() == 0) {
544                                         sleep(1);
545                                         kill(inst2->pid, SIGUSR1);
546                                         exit(0);
547                                 }
548                         } else
549                                 kill(inst2->pid, SIGUSR1);
550                         break;
551                 }
552         }
553         return inst;
554 }
555
556 /*
557  * Wait until all executing child processes have exited; return the
558  * logical OR of all of their exit code values.
559  */
560 static int wait_all(NOARGS)
561 {
562         struct fsck_instance *inst;
563         int     global_status = 0;
564
565         while (instance_list) {
566                 inst = wait_one();
567                 if (!inst)
568                         break;
569                 global_status |= inst->exit_status;
570                 free_instance(inst);
571         }
572         return global_status;
573 }
574
575 /*
576  * Run the fsck program on a particular device
577  * 
578  * If the type is specified using -t, and it isn't prefixed with "no"
579  * (as in "noext2") and only one filesystem type is specified, then
580  * use that type regardless of what is specified in /etc/fstab.
581  * 
582  * If the type isn't specified by the user, then use either the type
583  * specified in /etc/fstab, or DEFAULT_FSTYPE.
584  */
585 static void fsck_device(char *device, int interactive)
586 {
587         const char *type = 0;
588         struct fs_info *fsent;
589         int retval;
590
591         if (fstype && strncmp(fstype, "no", 2) && !strchr(fstype, ','))
592                 type = fstype;
593
594         if ((fsent = lookup(device))) {
595                 device = fsent->device;
596                 if (!type)
597                         type = fsent->type;
598         }
599         if (!type)
600                 type = DEFAULT_FSTYPE;
601
602         retval = execute(type, device, fsent ? fsent->mountpt : 0,
603                          interactive);
604         if (retval) {
605                 fprintf(stderr, "%s: Error %d while executing fsck.%s "
606                         "for %s\n", progname, retval, type, device);
607         }
608 }
609
610 /* See if filesystem type matches the list. */
611 static int fs_match(char *type, char *fs_type)
612 {
613   int ret = 0, negate = 0;
614   char list[128];
615   char *s;
616
617   if (!fs_type) return(1);
618
619   if (strncmp(fs_type, "no", 2) == 0) {
620         fs_type += 2;
621         negate = 1;
622   }
623   strcpy(list, fs_type);
624   s = strtok(list, ",");
625   while(s) {
626         if (strcmp(s, type) == 0) {
627                 ret = 1;
628                 break;
629         }
630         s = strtok(NULL, ",");
631   }
632   return(negate ? !ret : ret);
633 }
634
635
636 /* Check if we should ignore this filesystem. */
637 static int ignore(struct fs_info *fs)
638 {
639         const char **ip;
640         int wanted = 0;
641
642         /*
643          * If the pass number is 0, ignore it.
644          */
645         if (fs->passno == 0)
646                 return 1;
647
648         /*
649          * If a specific fstype is specified, and it doesn't match,
650          * ignore it.
651          */
652         if (!fs_match(fs->type, fstype)) return 1;
653         
654         /* Are we ignoring this type? */
655         for(ip = ignored_types; *ip; ip++)
656                 if (strcmp(fs->type, *ip) == 0) return(1);
657
658         /* Do we really really want to check this fs? */
659         for(ip = really_wanted; *ip; ip++)
660                 if (strcmp(fs->type, *ip) == 0) {
661                         wanted = 1;
662                         break;
663                 }
664
665         /* See if the <fsck.fs> program is available. */
666         if (find_fsck(fs->type) == NULL) {
667                 if (wanted)
668                         fprintf(stderr, "fsck: cannot check %s: fsck.%s not found\n",
669                                 fs->device, fs->type);
670                 return(1);
671         }
672
673         /* We can and want to check this file system type. */
674         return 0;
675 }
676
677 /*
678  * Return the "base device" given a particular device; this is used to
679  * assure that we only fsck one partition on a particular drive at any
680  * one time.  Otherwise, the disk heads will be seeking all over the
681  * place.
682  */
683 static const char *base_device(char *device)
684 {
685         const char **base;
686
687         for (base = base_devices; *base; base++) {
688                 if (!strncmp(*base, device, strlen(*base)))
689                         return *base;
690         }
691         return device;
692 }
693
694 /*
695  * Returns TRUE if a partition on the same disk is already being
696  * checked.
697  */
698 static int device_already_active(char *device)
699 {
700         struct fsck_instance *inst;
701         const char *base = base_device(device);
702
703         if (force_all_parallel)
704                 return 0;
705
706         for (inst = instance_list; inst; inst = inst->next) {
707                 if (!strcmp(base, base_device(inst->device)))
708                         return 1;
709         }
710         return 0;
711 }
712
713 /* Check all file systems, using the /etc/fstab table. */
714 static int check_all(NOARGS)
715 {
716         struct fs_info *fs = NULL;
717         struct fsck_instance *inst;
718         int status = EXIT_OK;
719         int not_done_yet = 1;
720         int passno = 1;
721         int pass_done;
722
723         if (verbose)
724                 printf("Checking all file systems.\n");
725
726         /*
727          * Find and check the root filesystem first.
728          */
729         if (!parallel_root) {
730                 for (fs = filesys_info; fs; fs = fs->next) {
731                         if (!strcmp(fs->mountpt, "/"))
732                                 break;
733                 }
734                 if (fs && !skip_root && !ignore(fs)) {
735                         fsck_device(fs->device, 1);
736                         fs->flags |= FLAG_DONE;
737                         status |= wait_all();
738                         if (status > EXIT_NONDESTRUCT)
739                                 return status;
740                 }
741         }
742         if (fs) fs->flags |= FLAG_DONE;
743
744         /*
745          * Mark filesystems that should be ignored as done.
746          */
747         for (fs = filesys_info; fs; fs = fs->next) {
748                 if (ignore(fs))
749                         fs->flags |= FLAG_DONE;
750         }
751                 
752         while (not_done_yet) {
753                 not_done_yet = 0;
754                 pass_done = 1;
755
756                 for (fs = filesys_info; fs; fs = fs->next) {
757                         if (fs->flags & FLAG_DONE)
758                                 continue;
759                         /*
760                          * If the filesystem's pass number is higher
761                          * than the current pass number, then we don't
762                          * do it yet.
763                          */
764                         if (fs->passno > passno) {
765                                 not_done_yet++;
766                                 continue;
767                         }
768                         /*
769                          * If a filesystem on a particular device has
770                          * already been spawned, then we need to defer
771                          * this to another pass.
772                          */
773                         if (device_already_active(fs->device)) {
774                                 pass_done = 0;
775                                 continue;
776                         }
777                         /*
778                          * Spawn off the fsck process
779                          */
780                         fsck_device(fs->device, serialize);
781                         fs->flags |= FLAG_DONE;
782
783                         if (serialize) {
784                                 pass_done = 0;
785                                 break; /* Only do one filesystem at a time */
786                         }
787                 }
788                 if (verbose > 1)
789                         printf("--waiting-- (pass %d)\n", passno);
790                 inst = wait_one();
791                 if (inst) {
792                         status |= inst->exit_status;
793                         free_instance(inst);
794                 }
795                 if (pass_done) {
796                         status |= wait_all();
797                         if (verbose > 1) 
798                                 printf("----------------------------------\n");
799                         passno++;
800                 } else
801                         not_done_yet++;
802         }
803         status |= wait_all();
804         return status;
805 }
806
807 static void usage(NOARGS)
808 {
809         fprintf(stderr,
810                 "Usage: fsck [-ACNPRTV] [-t fstype] [fs-options] filesys\n");
811         exit(EXIT_USAGE);
812 }
813
814 static void PRS(int argc, char *argv[])
815 {
816         int     i, j;
817         char    *arg;
818         char    options[128];
819         int     opt = 0;
820         int     opts_for_fsck = 0;
821         
822         num_devices = 0;
823         num_args = 0;
824         instance_list = 0;
825
826         progname = argv[0];
827
828         for (i=1; i < argc; i++) {
829                 arg = argv[i];
830                 if (!arg)
831                         continue;
832                 if ((arg[0] == '/' && !opts_for_fsck) ||
833                     (strncmp(arg, "LABEL=", 6) == 0) ||
834                     (strncmp(arg, "UUID=", 5) == 0)) {
835                         if (num_devices >= MAX_DEVICES) {
836                                 fprintf(stderr, "%s: too many devices\n",
837                                         progname);
838                                 exit(1);
839                         }
840                         devices[num_devices++] =
841                                 interpret_device(string_copy(arg));
842                         continue;
843                 }
844                 if (arg[0] != '-' || opts_for_fsck) {
845                         if (num_args >= MAX_ARGS) {
846                                 fprintf(stderr, "%s: too many arguments\n",
847                                         progname);
848                                 exit(1);
849                         }
850                         args[num_args++] = string_copy(arg);
851                         continue;
852                 }
853                 for (j=1; arg[j]; j++) {
854                         if (opts_for_fsck) {
855                                 options[++opt] = arg[j];
856                                 continue;
857                         }
858                         switch (arg[j]) {
859                         case 'A':
860                                 doall++;
861                                 break;
862                         case 'C':
863                                 progress++;
864                                 break;
865                         case 'V':
866                                 verbose++;
867                                 break;
868                         case 'N':
869                                 noexecute++;
870                                 break;
871                         case 'R':
872                                 skip_root++;
873                                 break;
874                         case 'T':
875                                 notitle++;
876                                 break;
877                         case 'M':
878                                 like_mount++;
879                                 break;
880                         case 'P':
881                                 parallel_root++;
882                                 break;
883                         case 's':
884                                 serialize++;
885                                 break;
886                         case 't':
887                                 if (arg[j+1]) {
888                                         fstype = string_copy(arg+j+1);
889                                         goto next_arg;
890                                 }
891                                 if ((i+1) < argc) {
892                                         i++;
893                                         fstype = string_copy(argv[i]);
894                                         goto next_arg;
895                                 }
896                                 usage();
897                                 break;
898                         case '-':
899                                 opts_for_fsck++;
900                                 break;
901                         default:
902                                 options[++opt] = arg[j];
903                                 break;
904                         }
905                 }
906         next_arg:
907                 if (opt) {
908                         options[0] = '-';
909                         options[++opt] = '\0';
910                         if (num_args >= MAX_ARGS) {
911                                 fprintf(stderr,
912                                         "%s: too many arguments\n",
913                                         progname);
914                                 exit(1);
915                         }
916                         args[num_args++] = string_copy(options);
917                         opt = 0;
918                 }
919         }
920         if (getenv("FSCK_FORCE_ALL_PARALLEL"))
921                 force_all_parallel++;
922 }
923
924 int main(int argc, char *argv[])
925 {
926         int i;
927         int status = 0;
928         int interactive = 0;
929         char *oldpath = getenv("PATH");
930         const char *fstab;
931
932         PRS(argc, argv);
933
934         if (!notitle)
935                 printf("Parallelizing fsck version %s (%s)\n",
936                         E2FSPROGS_VERSION, E2FSPROGS_DATE);
937
938         fstab = getenv("FSTAB_FILE");
939         if (!fstab)
940                 fstab = _PATH_MNTTAB;
941         load_fs_info(fstab);
942
943         /* Update our search path to include uncommon directories. */
944         if (oldpath) {
945                 fsck_path = malloc (strlen (fsck_prefix_path) + 1 +
946                                     strlen (oldpath) + 1);
947                 strcpy (fsck_path, fsck_prefix_path);
948                 strcat (fsck_path, ":");
949                 strcat (fsck_path, oldpath);
950         } else {
951                 fsck_path = string_copy(fsck_prefix_path);
952         }
953         
954         if ((num_devices == 1) || (serialize))
955                 interactive = 1;
956
957         /* If -A was specified ("check all"), do that! */
958         if (doall)
959                 return check_all();
960
961         for (i = 0 ; i < num_devices; i++) {
962                 fsck_device(devices[i], interactive);
963                 if (serialize) {
964                         struct fsck_instance *inst;
965
966                         inst = wait_one();
967                         if (inst) {
968                                 status |= inst->exit_status;
969                                 free_instance(inst);
970                         }
971                 }
972         }
973         status |= wait_all();
974         free(fsck_path);
975         return status;
976 }