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
9 * Written by Theodore Ts'o, <tytso@mit.edu>
11 * Usage: fsck [-ACVRNTM] [-s] [-t fstype] [fs-options] device
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.
21 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999 Theodore Ts'o.
24 * This file may be redistributed under the terms of the GNU Public
29 #include <sys/types.h>
31 #include <sys/signal.h>
55 #include "../version.h"
57 #include "get_device_by_label.h"
60 #define _PATH_MNTTAB "/etc/fstab"
63 static const char *ignored_types[] = {
73 static const char *really_wanted[] = {
80 #ifdef DEV_DSK_DEVICES
81 static const char *base_devices[] = {
100 static const char *base_devices[] = {
121 * Global variables for options
123 char *devices[MAX_DEVICES];
124 char *args[MAX_ARGS];
125 int num_devices, num_args;
134 int parallel_root = 0;
136 int force_all_parallel = 0;
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";
143 static int ignore(struct fs_info *);
145 static char *string_copy(const char *s)
149 ret = malloc(strlen(s)+1);
155 static char *skip_over_blank(char *cp)
157 while (*cp && isspace(*cp))
162 static char *skip_over_word(char *cp)
164 while (*cp && !isspace(*cp))
169 static void strip_line(char *line)
174 p = line + strlen(line) - 1;
175 if ((*p == '\n') || (*p == '\r'))
182 static char *parse_word(char **buf)
190 word = skip_over_blank(word);
191 next = skip_over_word(word);
198 static void free_instance(struct fsck_instance *i)
208 static int parse_fstab_line(char *line, struct fs_info **ret_fs)
210 char *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
215 if ((cp = strchr(line, '#')))
216 *cp = 0; /* Ignore everything after the comment char */
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);
227 return 0; /* Allow blank lines */
229 if (!mntpnt || !type)
232 if (!(fs = malloc(sizeof(struct fs_info))))
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;
250 * Interpret the device name if necessary
252 static char *interpret_device(char *spec)
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);
268 * Load the filesystem database from /etc/fstab
270 static void load_fs_info(const char *filename)
276 struct fs_info *fs, *fs_last = NULL;
279 if ((f = fopen(filename, "r")) == NULL) {
280 fprintf(stderr, "WARNING: couldn't open %s: %s\n",
281 filename, strerror(errno));
286 if (!fgets(buf, sizeof(buf), f))
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);
296 fs->device = interpret_device(fs->device);
311 fprintf(stderr, "\007\007\007"
312 "WARNING: Your /etc/fstab does not contain the fsck passno\n");
314 " field. I will kludge around things for you, but you\n");
316 " should fix your /etc/fstab file as soon as you can.\n\n");
318 for (fs = filesys_info; fs; fs = fs->next) {
324 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
325 static struct fs_info *lookup(char *filesys)
329 /* No filesys name given. */
333 for (fs = filesys_info; fs; fs = fs->next) {
334 if (!strcmp(filesys, fs->device) ||
335 !strcmp(filesys, fs->mountpt))
342 /* Find fsck program for a given fs type. */
343 static char *find_fsck(char *type)
347 static char prog[256];
348 char *p = string_copy(fsck_path);
351 /* Are we looking for a program or just a type? */
352 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
354 for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
355 sprintf(prog, tpl, s, type);
356 if (stat(prog, &st) == 0) break;
359 return(s ? prog : NULL);
362 static int progress_active(NOARGS)
364 struct fsck_instance *inst;
366 for (inst = instance_list; inst; inst = inst->next) {
367 if (inst->flags & FLAG_DONE)
369 if (inst->flags & FLAG_PROGRESS)
376 * Execute a particular fsck program, and link it into the list of
377 * child processes we are waiting for.
379 static int execute(const char *type, char *device, char *mntpt,
382 char *s, *argv[80], prog[80];
384 struct fsck_instance *inst, *p;
387 inst = malloc(sizeof(struct fsck_instance));
390 memset(inst, 0, sizeof(struct fsck_instance));
392 sprintf(prog, "fsck.%s", type);
393 argv[0] = string_copy(prog);
396 for (i=0; i <num_args; i++)
397 argv[argc++] = string_copy(args[i]);
399 if (progress & !progress_active()) {
400 if (strcmp(type, "ext2") == 0) {
401 argv[argc++] = string_copy("-C0");
402 inst->flags |= FLAG_PROGRESS;
406 argv[argc++] = string_copy(device);
411 fprintf(stderr, "fsck: %s: not found\n", prog);
415 if (verbose || noexecute) {
416 printf("[%s -- %s] ", s, mntpt ? mntpt : device);
417 for (i=0; i < argc; i++)
418 printf("%s ", argv[i]);
422 /* Fork and execute the correct program. */
425 else if ((pid = fork()) < 0) {
428 } else if (pid == 0) {
431 (void) execv(s, argv);
436 for (i=0; i < argc; i++)
440 inst->prog = string_copy(prog);
441 inst->type = string_copy(type);
442 inst->device = string_copy(device);
443 inst->start_time = time(0);
447 * Find the end of the list, so we add the instance on at the end.
449 for (p = instance_list; p && p->next; p = p->next);
454 instance_list = inst;
460 * Wait for one child process to exit; when it does, unlink it from
461 * the list of executing child processes, and return it.
463 static struct fsck_instance *wait_one(NOARGS)
467 struct fsck_instance *inst, *inst2, *prev;
474 inst = instance_list;
475 instance_list = inst->next;
476 inst->exit_status = 0;
481 * gcc -Wall fails saving throw against stupidity
482 * (inst and prev are thought to be uninitialized variables)
489 if ((errno == EINTR) || (errno == EAGAIN))
491 if (errno == ECHILD) {
493 "%s: wait: No more child process?!?\n",
500 for (prev = 0, inst = instance_list;
502 prev = inst, inst = inst->next) {
503 if (inst->pid == pid)
508 if (WIFEXITED(status))
509 status = WEXITSTATUS(status);
510 else if (WIFSIGNALED(status)) {
511 sig = WTERMSIG(status);
513 status = EXIT_UNCORRECTED;
515 printf("Warning... %s for device %s exited "
517 inst->prog, inst->device, sig);
521 printf("%s %s: status is %x, should never happen.\n",
522 inst->prog, inst->device, status);
525 inst->exit_status = status;
527 prev->next = inst->next;
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)
535 if (strcmp(inst2->type, "ext2"))
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
542 if (inst2->start_time < time(0)+2) {
545 kill(inst2->pid, SIGUSR1);
549 kill(inst2->pid, SIGUSR1);
557 * Wait until all executing child processes have exited; return the
558 * logical OR of all of their exit code values.
560 static int wait_all(NOARGS)
562 struct fsck_instance *inst;
563 int global_status = 0;
565 while (instance_list) {
569 global_status |= inst->exit_status;
572 return global_status;
576 * Run the fsck program on a particular device
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.
582 * If the type isn't specified by the user, then use either the type
583 * specified in /etc/fstab, or DEFAULT_FSTYPE.
585 static void fsck_device(char *device, int interactive)
587 const char *type = 0;
588 struct fs_info *fsent;
591 if (fstype && strncmp(fstype, "no", 2) && !strchr(fstype, ','))
594 if ((fsent = lookup(device))) {
595 device = fsent->device;
600 type = DEFAULT_FSTYPE;
602 retval = execute(type, device, fsent ? fsent->mountpt : 0,
605 fprintf(stderr, "%s: Error %d while executing fsck.%s "
606 "for %s\n", progname, retval, type, device);
610 /* See if filesystem type matches the list. */
611 static int fs_match(char *type, char *fs_type)
613 int ret = 0, negate = 0;
617 if (!fs_type) return(1);
619 if (strncmp(fs_type, "no", 2) == 0) {
623 strcpy(list, fs_type);
624 s = strtok(list, ",");
626 if (strcmp(s, type) == 0) {
630 s = strtok(NULL, ",");
632 return(negate ? !ret : ret);
636 /* Check if we should ignore this filesystem. */
637 static int ignore(struct fs_info *fs)
643 * If the pass number is 0, ignore it.
649 * If a specific fstype is specified, and it doesn't match,
652 if (!fs_match(fs->type, fstype)) return 1;
654 /* Are we ignoring this type? */
655 for(ip = ignored_types; *ip; ip++)
656 if (strcmp(fs->type, *ip) == 0) return(1);
658 /* Do we really really want to check this fs? */
659 for(ip = really_wanted; *ip; ip++)
660 if (strcmp(fs->type, *ip) == 0) {
665 /* See if the <fsck.fs> program is available. */
666 if (find_fsck(fs->type) == NULL) {
668 fprintf(stderr, "fsck: cannot check %s: fsck.%s not found\n",
669 fs->device, fs->type);
673 /* We can and want to check this file system type. */
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
683 static const char *base_device(char *device)
687 for (base = base_devices; *base; base++) {
688 if (!strncmp(*base, device, strlen(*base)))
695 * Returns TRUE if a partition on the same disk is already being
698 static int device_already_active(char *device)
700 struct fsck_instance *inst;
701 const char *base = base_device(device);
703 if (force_all_parallel)
706 for (inst = instance_list; inst; inst = inst->next) {
707 if (!strcmp(base, base_device(inst->device)))
713 /* Check all file systems, using the /etc/fstab table. */
714 static int check_all(NOARGS)
716 struct fs_info *fs = NULL;
717 struct fsck_instance *inst;
718 int status = EXIT_OK;
719 int not_done_yet = 1;
724 printf("Checking all file systems.\n");
727 * Find and check the root filesystem first.
729 if (!parallel_root) {
730 for (fs = filesys_info; fs; fs = fs->next) {
731 if (!strcmp(fs->mountpt, "/"))
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)
742 if (fs) fs->flags |= FLAG_DONE;
745 * Mark filesystems that should be ignored as done.
747 for (fs = filesys_info; fs; fs = fs->next) {
749 fs->flags |= FLAG_DONE;
752 while (not_done_yet) {
756 for (fs = filesys_info; fs; fs = fs->next) {
757 if (fs->flags & FLAG_DONE)
760 * If the filesystem's pass number is higher
761 * than the current pass number, then we don't
764 if (fs->passno > passno) {
769 * If a filesystem on a particular device has
770 * already been spawned, then we need to defer
771 * this to another pass.
773 if (device_already_active(fs->device)) {
778 * Spawn off the fsck process
780 fsck_device(fs->device, serialize);
781 fs->flags |= FLAG_DONE;
785 break; /* Only do one filesystem at a time */
789 printf("--waiting-- (pass %d)\n", passno);
792 status |= inst->exit_status;
796 status |= wait_all();
798 printf("----------------------------------\n");
803 status |= wait_all();
807 static void usage(NOARGS)
810 "Usage: fsck [-ACNPRTV] [-t fstype] [fs-options] filesys\n");
814 static void PRS(int argc, char *argv[])
820 int opts_for_fsck = 0;
828 for (i=1; i < argc; i++) {
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",
840 devices[num_devices++] =
841 interpret_device(string_copy(arg));
844 if (arg[0] != '-' || opts_for_fsck) {
845 if (num_args >= MAX_ARGS) {
846 fprintf(stderr, "%s: too many arguments\n",
850 args[num_args++] = string_copy(arg);
853 for (j=1; arg[j]; j++) {
855 options[++opt] = arg[j];
888 fstype = string_copy(arg+j+1);
893 fstype = string_copy(argv[i]);
902 options[++opt] = arg[j];
909 options[++opt] = '\0';
910 if (num_args >= MAX_ARGS) {
912 "%s: too many arguments\n",
916 args[num_args++] = string_copy(options);
920 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
921 force_all_parallel++;
924 int main(int argc, char *argv[])
929 char *oldpath = getenv("PATH");
935 printf("Parallelizing fsck version %s (%s)\n",
936 E2FSPROGS_VERSION, E2FSPROGS_DATE);
938 fstab = getenv("FSTAB_FILE");
940 fstab = _PATH_MNTTAB;
943 /* Update our search path to include uncommon directories. */
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);
951 fsck_path = string_copy(fsck_prefix_path);
954 if ((num_devices == 1) || (serialize))
957 /* If -A was specified ("check all"), do that! */
961 for (i = 0 ; i < num_devices; i++) {
962 fsck_device(devices[i], interactive);
964 struct fsck_instance *inst;
968 status |= inst->exit_status;
973 status |= wait_all();