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 [-AVRNTM] [-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 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>
53 #include "../version.h"
55 #include "get_device_by_label.h"
58 #define _PATH_MNTTAB "/etc/fstab"
61 static const char *ignored_types[] = {
71 static const char *really_wanted[] = {
78 #ifdef DEV_DSK_DEVICES
79 static const char *base_devices[] = {
98 static const char *base_devices[] = {
119 * Global variables for options
121 char *devices[MAX_DEVICES];
122 char *args[MAX_ARGS];
123 int num_devices, num_args;
132 int parallel_root = 0;
135 struct fs_info *filesys_info;
136 struct fsck_instance *instance_list;
137 const char *fsck_prefix_path = "/sbin:/sbin/fs.d:/sbin/fs:/etc/fs:/etc";
139 static int ignore(struct fs_info *);
141 static char *string_copy(const char *s)
145 ret = malloc(strlen(s)+1);
151 static char *skip_over_blank(char *cp)
153 while (*cp && isspace(*cp))
158 static char *skip_over_word(char *cp)
160 while (*cp && !isspace(*cp))
165 static void strip_line(char *line)
170 p = line + strlen(line) - 1;
171 if ((*p == '\n') || (*p == '\r'))
178 static char *parse_word(char **buf)
186 word = skip_over_blank(word);
187 next = skip_over_word(word);
194 static void free_instance(struct fsck_instance *i)
204 int parse_fstab_line(char *line, struct fs_info **ret_fs)
206 char *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
211 if (cp = strchr(line, '#'))
212 *cp = 0; /* Ignore everything after the comment char */
215 device = parse_word(&cp);
216 mntpnt = parse_word(&cp);
217 type = parse_word(&cp);
218 opts = parse_word(&cp);
219 freq = parse_word(&cp);
220 passno = parse_word(&cp);
223 return 0; /* Allow blank lines */
225 if (!mntpnt || !type)
228 if (!(fs = malloc(sizeof(struct fs_info))))
231 fs->device = string_copy(device);
232 fs->mountpt = string_copy(mntpnt);
233 fs->type = string_copy(type);
234 fs->opts = string_copy(opts ? opts : "");
235 fs->freq = freq ? atoi(freq) : -1;
236 fs->passno = passno ? atoi(passno) : -1;
246 * Interpret the device name if necessary
248 static char *interpret_device(char *spec)
252 if (!strncmp(spec, "UUID=", 5))
253 dev = get_spec_by_uuid(spec+5);
254 else if (!strncmp(spec, "LABEL=", 6))
255 dev = get_spec_by_volume_label(spec+6);
264 * Load the filesystem database from /etc/fstab
266 static void load_fs_info(char *filename)
272 struct fs_info *fs, *fs_last = NULL;
275 if ((f = fopen(filename, "r")) == NULL) {
276 fprintf(stderr, "WARNING: couldn't open %s: %s\n",
277 filename, strerror(errno));
282 if (!fgets(buf, sizeof(buf), f))
284 buf[sizeof(buf)-1] = 0;
285 if (parse_fstab_line(buf, &fs) < 0) {
286 fprintf(stderr, "WARNING: bad format "
287 "on line %d of %s\n", lineno, filename);
292 fs->device = interpret_device(fs->device);
307 fprintf(stderr, "\007\007\007"
308 "WARNING: Your /etc/fstab does not contain the fsck passno\n");
310 " field. I will kludge around things for you, but you\n");
312 " should fix your /etc/fstab file as soon as you can.\n\n");
314 for (fs = filesys_info; fs; fs = fs->next) {
320 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
321 static struct fs_info *lookup(char *filesys)
325 /* No filesys name given. */
329 for (fs = filesys_info; fs; fs = fs->next) {
330 if (!strcmp(filesys, fs->device) ||
331 !strcmp(filesys, fs->mountpt))
338 /* Find fsck program for a given fs type. */
339 static char *find_fsck(char *type)
343 static char prog[256];
344 char *p = string_copy(fsck_path);
347 /* Are we looking for a program or just a type? */
348 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
350 for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
351 sprintf(prog, tpl, s, type);
352 if (stat(prog, &st) == 0) break;
355 return(s ? prog : NULL);
359 * Execute a particular fsck program, and link it into the list of
360 * child processes we are waiting for.
362 static int execute(char *prog, char *device, char *mntpt, int interactive)
366 struct fsck_instance *inst;
369 argv[0] = string_copy(prog);
372 for (i=0; i <num_args; i++)
373 argv[argc++] = string_copy(args[i]);
375 argv[argc++] = string_copy(device);
380 fprintf(stderr, "fsck: %s: not found\n", prog);
384 if (verbose || noexecute) {
385 printf("[%s -- %s] ", s, mntpt ? mntpt : device);
386 for (i=0; i < argc; i++)
387 printf("%s ", argv[i]);
391 /* Fork and execute the correct program. */
394 else if ((pid = fork()) < 0) {
397 } else if (pid == 0) {
400 (void) execv(s, argv);
404 inst = malloc(sizeof(struct fsck_instance));
407 memset(inst, 0, sizeof(struct fsck_instance));
409 inst->prog = string_copy(prog);
410 inst->device = string_copy(device);
411 inst->next = instance_list;
412 instance_list = inst;
418 * Wait for one child process to exit; when it does, unlink it from
419 * the list of executing child processes, and return it.
421 static struct fsck_instance *wait_one(NOARGS)
425 struct fsck_instance *inst, *prev;
432 inst = instance_list;
433 instance_list = inst->next;
434 inst->exit_status = 0;
441 if ((errno == EINTR) || (errno == EAGAIN))
443 if (errno == ECHILD) {
445 "%s: wait: No more child process?!?\n",
452 for (prev = 0, inst = instance_list;
454 prev = inst, inst = inst->next) {
455 if (inst->pid == pid)
459 printf("Unexpected child process %d, status = 0x%x\n",
463 if (WIFEXITED(status))
464 status = WEXITSTATUS(status);
465 else if (WIFSIGNALED(status)) {
466 sig = WTERMSIG(status);
468 status = EXIT_UNCORRECTED;
470 printf("Warning... %s for device %s exited "
472 inst->prog, inst->device, sig);
476 printf("%s %s: status is %x, should never happen.\n",
477 inst->prog, inst->device, status);
480 inst->exit_status = status;
482 prev->next = inst->next;
484 instance_list = inst->next;
489 * Wait until all executing child processes have exited; return the
490 * logical OR of all of their exit code values.
492 static int wait_all(NOARGS)
494 struct fsck_instance *inst;
495 int global_status = 0;
497 while (instance_list) {
501 global_status |= inst->exit_status;
504 return global_status;
508 * Run the fsck program on a particular device
510 * If the type is specified using -t, and it isn't prefixed with "no"
511 * (as in "noext2") and only one filesystem type is specified, then
512 * use that type regardless of what is specified in /etc/fstab.
514 * If the type isn't specified by the user, then use either the type
515 * specified in /etc/fstab, or DEFAULT_FSTYPE.
517 static void fsck_device(char *device, int interactive)
519 const char *type = 0;
520 struct fs_info *fsent;
524 if (fstype && strncmp(fstype, "no", 2) && !strchr(fstype, ','))
527 if ((fsent = lookup(device))) {
528 device = fsent->device;
533 type = DEFAULT_FSTYPE;
535 sprintf(prog, "fsck.%s", type);
536 retval = execute(prog, device, fsent ? fsent->mountpt : 0,
539 fprintf(stderr, "%s: Error %d while executing %s for %s\n",
540 progname, retval, prog, device);
544 /* See if filesystem type matches the list. */
545 static int fs_match(char *type, char *fs_type)
547 int ret = 0, negate = 0;
551 if (!fs_type) return(1);
553 if (strncmp(fs_type, "no", 2) == 0) {
557 strcpy(list, fs_type);
558 s = strtok(list, ",");
560 if (strcmp(s, type) == 0) {
564 s = strtok(NULL, ",");
566 return(negate ? !ret : ret);
570 /* Check if we should ignore this filesystem. */
571 static int ignore(struct fs_info *fs)
577 * If the pass number is 0, ignore it.
583 * If a specific fstype is specified, and it doesn't match,
586 if (!fs_match(fs->type, fstype)) return 1;
588 /* Are we ignoring this type? */
589 for(ip = ignored_types; *ip; ip++)
590 if (strcmp(fs->type, *ip) == 0) return(1);
592 /* Do we really really want to check this fs? */
593 for(ip = really_wanted; *ip; ip++)
594 if (strcmp(fs->type, *ip) == 0) {
599 /* See if the <fsck.fs> program is available. */
600 if (find_fsck(fs->type) == NULL) {
602 fprintf(stderr, "fsck: cannot check %s: fsck.%s not found\n",
603 fs->device, fs->type);
607 /* We can and want to check this file system type. */
612 * Return the "base device" given a particular device; this is used to
613 * assure that we only fsck one partition on a particular drive at any
614 * one time. Otherwise, the disk heads will be seeking all over the
617 static const char *base_device(char *device)
621 for (base = base_devices; *base; base++) {
622 if (!strncmp(*base, device, strlen(*base)))
629 * Returns TRUE if a partition on the same disk is already being
632 static int device_already_active(char *device)
634 struct fsck_instance *inst;
637 base = base_device(device);
639 for (inst = instance_list; inst; inst = inst->next) {
640 if (!strcmp(base, base_device(inst->device)))
647 /* Check all file systems, using the /etc/fstab table. */
648 static int check_all(NOARGS)
650 struct fs_info *fs = NULL;
651 struct fsck_instance *inst;
652 int status = EXIT_OK;
653 int not_done_yet = 1;
658 printf("Checking all file systems.\n");
661 * Find and check the root filesystem first.
663 if (!parallel_root) {
664 for (fs = filesys_info; fs; fs = fs->next) {
665 if (!strcmp(fs->mountpt, "/"))
668 if (fs && !skip_root && !ignore(fs)) {
669 fsck_device(fs->device, 0);
670 fs->flags |= FLAG_DONE;
671 status |= wait_all();
672 if (status > EXIT_NONDESTRUCT)
676 if (fs) fs->flags |= FLAG_DONE;
679 * Mark filesystems that should be ignored as done.
681 for (fs = filesys_info; fs; fs = fs->next) {
683 fs->flags |= FLAG_DONE;
686 while (not_done_yet) {
690 for (fs = filesys_info; fs; fs = fs->next) {
691 if (fs->flags & FLAG_DONE)
694 * If the filesystem's pass number is higher
695 * than the current pass number, then we don't
698 if (fs->passno > passno) {
703 * If a filesystem on a particular device has
704 * already been spawned, then we need to defer
705 * this to another pass.
707 if (device_already_active(fs->device)) {
712 * Spawn off the fsck process
714 fsck_device(fs->device, 0);
715 fs->flags |= FLAG_DONE;
719 break; /* Only do one filesystem at a time */
724 printf("--waiting-- (pass %d)\n", passno);
727 status |= inst->exit_status;
731 status |= wait_all();
733 printf("----------------------------------\n");
738 status |= wait_all();
742 static void usage(NOARGS)
745 "Usage: fsck [-AV] [-t fstype] [fs-options] filesys\n");
749 static void PRS(int argc, char *argv[])
755 int opts_for_fsck = 0;
763 for (i=1; i < argc; i++) {
767 if ((arg[0] == '/' && !opts_for_fsck) ||
768 (strncmp(arg, "LABEL=", 6) == 0) ||
769 (strncmp(arg, "UUID=", 5) == 0)) {
770 if (num_devices >= MAX_DEVICES) {
771 fprintf(stderr, "%s: too many devices\n",
775 devices[num_devices++] =
776 interpret_device(string_copy(arg));
779 if (arg[0] != '-' || opts_for_fsck) {
780 if (num_args >= MAX_ARGS) {
781 fprintf(stderr, "%s: too many arguments\n",
785 args[num_args++] = string_copy(arg);
788 for (j=1; arg[j]; j++) {
790 options[++opt] = arg[j];
820 fstype = string_copy(arg+j+1);
825 fstype = string_copy(argv[i]);
834 options[++opt] = arg[j];
841 options[++opt] = '\0';
842 if (num_args >= MAX_ARGS) {
844 "%s: too many arguments\n",
848 args[num_args++] = string_copy(options);
854 int main(int argc, char *argv[])
859 char *oldpath = getenv("PATH");
865 printf("Parallelizing fsck version %s (%s)\n",
866 E2FSPROGS_VERSION, E2FSPROGS_DATE);
868 fstab = getenv("FSTAB_FILE");
870 fstab = _PATH_MNTTAB;
873 /* Update our search path to include uncommon directories. */
875 fsck_path = malloc (strlen (fsck_prefix_path) + 1 +
876 strlen (oldpath) + 1);
877 strcpy (fsck_path, fsck_prefix_path);
878 strcat (fsck_path, ":");
879 strcat (fsck_path, oldpath);
881 fsck_path = string_copy(fsck_prefix_path);
884 /* If -A was specified ("check all"), do that! */
888 if ((num_devices == 1) || (serialize))
891 for (i = 0 ; i < num_devices; i++) {
892 fsck_device(devices[i], interactive);
894 struct fsck_instance *inst;
898 status |= inst->exit_status;
903 status |= wait_all();