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"
57 #define _PATH_MNTTAB "/etc/fstab"
60 static const char *ignored_types[] = {
70 static const char *really_wanted[] = {
77 #ifdef DEV_DSK_DEVICES
78 static const char *base_devices[] = {
97 static const char *base_devices[] = {
118 * Global variables for options
120 char *devices[MAX_DEVICES];
121 char *args[MAX_ARGS];
122 int num_devices, num_args;
131 int parallel_root = 0;
134 struct fs_info *filesys_info;
135 struct fsck_instance *instance_list;
136 const char *fsck_prefix_path = "/sbin:/sbin/fs.d:/sbin/fs:/etc/fs:/etc";
138 static int ignore(struct fs_info *);
140 static char *string_copy(const char *s)
144 ret = malloc(strlen(s)+1);
150 static char *skip_over_blank(char *cp)
152 while (*cp && isspace(*cp))
157 static char *skip_over_word(char *cp)
159 while (*cp && !isspace(*cp))
164 static void strip_line(char *line)
169 p = line + strlen(line) - 1;
170 if ((*p == '\n') || (*p == '\r'))
177 static char *parse_word(char **buf)
185 word = skip_over_blank(word);
186 next = skip_over_word(word);
193 static void free_instance(struct fsck_instance *i)
203 struct fs_info *parse_fstab_line(char *line)
205 char *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
211 device = parse_word(&cp);
212 mntpnt = parse_word(&cp);
213 type = parse_word(&cp);
214 opts = parse_word(&cp);
215 freq = parse_word(&cp);
216 passno = parse_word(&cp);
218 if (!device || !mntpnt || !type)
221 if (!(fs = malloc(sizeof(struct fs_info))))
224 fs->device = string_copy(device);
225 fs->mountpt = string_copy(mntpnt);
226 fs->type = string_copy(type);
227 fs->opts = string_copy(opts ? opts : "");
228 fs->freq = freq ? atoi(freq) : -1;
229 fs->passno = passno ? atoi(passno) : -1;
237 * Load the filesystem database from /etc/fstab
239 static void load_fs_info(char *filename)
245 struct fs_info *fs, *fs_last = NULL;
248 if ((f = fopen(filename, "r")) == NULL) {
249 fprintf(stderr, "WARNING: couldn't open %s: %s\n",
250 filename, strerror(errno));
255 if (!fgets(buf, sizeof(buf), f))
257 buf[sizeof(buf)-1] = 0;
258 if ((fs = parse_fstab_line(buf)) == NULL) {
259 fprintf(stderr, "WARNING: bad format "
260 "on line %d of %s\n", lineno, filename);
275 fprintf(stderr, "\007\007\007"
276 "WARNING: Your /etc/fstab does not contain the fsck passno\n");
278 " field. I will kludge around things for you, but you\n");
280 " should fix your /etc/fstab file as soon as you can.\n\n");
282 for (fs = filesys_info; fs; fs = fs->next) {
288 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
289 static struct fs_info *lookup(char *filesys)
293 /* No filesys name given. */
297 for (fs = filesys_info; fs; fs = fs->next) {
298 if (!strcmp(filesys, fs->device) ||
299 !strcmp(filesys, fs->mountpt))
306 /* Find fsck program for a given fs type. */
307 static char *find_fsck(char *type)
311 static char prog[256];
312 char *p = string_copy(fsck_path);
315 /* Are we looking for a program or just a type? */
316 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
318 for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
319 sprintf(prog, tpl, s, type);
320 if (stat(prog, &st) == 0) break;
323 return(s ? prog : NULL);
327 * Execute a particular fsck program, and link it into the list of
328 * child processes we are waiting for.
330 static int execute(char *prog, char *device)
334 struct fsck_instance *inst;
337 argv[0] = string_copy(prog);
340 for (i=0; i <num_args; i++)
341 argv[argc++] = string_copy(args[i]);
343 argv[argc++] = string_copy(device);
348 fprintf(stderr, "fsck: %s: not found\n", prog);
352 if (verbose || noexecute) {
354 for (i=0; i < argc; i++)
355 printf("%s ", argv[i]);
361 /* Fork and execute the correct program. */
362 if ((pid = fork()) < 0) {
365 } else if (pid == 0) {
366 (void) execv(s, argv);
370 inst = malloc(sizeof(struct fsck_instance));
373 memset(inst, 0, sizeof(struct fsck_instance));
375 inst->prog = string_copy(prog);
376 inst->device = string_copy(device);
377 inst->next = instance_list;
378 instance_list = inst;
384 * Wait for one child process to exit; when it does, unlink it from
385 * the list of executing child processes, and return it.
387 static struct fsck_instance *wait_one(NOARGS)
391 struct fsck_instance *inst, *prev;
400 if ((errno == EINTR) || (errno == EAGAIN))
402 if (errno == ECHILD) {
404 "%s: wait: No more child process?!?\n",
411 for (prev = 0, inst = instance_list;
413 prev = inst, inst = inst->next) {
414 if (inst->pid == pid)
418 printf("Unexpected child process %d, status = 0x%x\n",
422 if (WIFEXITED(status))
423 status = WEXITSTATUS(status);
424 else if (WIFSIGNALED(status)) {
425 sig = WTERMSIG(status);
427 status = EXIT_UNCORRECTED;
429 printf("Warning... %s for device %s exited "
431 inst->prog, inst->device, sig);
435 printf("%s %s: status is %x, should never happen.\n",
436 inst->prog, inst->device, status);
439 inst->exit_status = status;
441 prev->next = inst->next;
443 instance_list = inst->next;
448 * Wait until all executing child processes have exited; return the
449 * logical OR of all of their exit code values.
451 static int wait_all(NOARGS)
453 struct fsck_instance *inst;
454 int global_status = 0;
456 while (instance_list) {
460 global_status |= inst->exit_status;
463 return global_status;
467 * Run the fsck program on a particular device
469 * If the type is specified using -t, and it isn't prefixed with "no"
470 * (as in "noext2") and only one filesystem type is specified, then
471 * use that type regardless of what is specified in /etc/fstab.
473 * If the type isn't specified by the user, then use either the type
474 * specified in /etc/fstab, or DEFAULT_FSTYPE.
476 static void fsck_device(char *device)
478 const char *type = 0;
479 struct fs_info *fsent;
483 if (fstype && strncmp(fstype, "no", 2) && !strchr(fstype, ','))
486 if ((fsent = lookup(device))) {
487 device = fsent->device;
492 type = DEFAULT_FSTYPE;
494 sprintf(prog, "fsck.%s", type);
495 retval = execute(prog, device);
497 fprintf(stderr, "%s: Error %d while executing %s for %s\n",
498 progname, retval, prog, device);
502 /* See if filesystem type matches the list. */
503 static int fs_match(char *type, char *fs_type)
505 int ret = 0, negate = 0;
509 if (!fs_type) return(1);
511 if (strncmp(fs_type, "no", 2) == 0) {
515 strcpy(list, fs_type);
516 s = strtok(list, ",");
518 if (strcmp(s, type) == 0) {
522 s = strtok(NULL, ",");
524 return(negate ? !ret : ret);
528 /* Check if we should ignore this filesystem. */
529 static int ignore(struct fs_info *fs)
535 * If the pass number is 0, ignore it.
541 * If a specific fstype is specified, and it doesn't match,
544 if (!fs_match(fs->type, fstype)) return 1;
546 /* Are we ignoring this type? */
547 for(ip = ignored_types; *ip; ip++)
548 if (strcmp(fs->type, *ip) == 0) return(1);
550 /* Do we really really want to check this fs? */
551 for(ip = really_wanted; *ip; ip++)
552 if (strcmp(fs->type, *ip) == 0) {
557 /* See if the <fsck.fs> program is available. */
558 if (find_fsck(fs->type) == NULL) {
560 fprintf(stderr, "fsck: cannot check %s: fsck.%s not found\n",
561 fs->device, fs->type);
565 /* We can and want to check this file system type. */
570 * Return the "base device" given a particular device; this is used to
571 * assure that we only fsck one partition on a particular drive at any
572 * one time. Otherwise, the disk heads will be seeking all over the
575 static const char *base_device(char *device)
579 for (base = base_devices; *base; base++) {
580 if (!strncmp(*base, device, strlen(*base)))
587 * Returns TRUE if a partition on the same disk is already being
590 static int device_already_active(char *device)
592 struct fsck_instance *inst;
595 base = base_device(device);
597 for (inst = instance_list; inst; inst = inst->next) {
598 if (!strcmp(base, base_device(inst->device)))
605 /* Check all file systems, using the /etc/fstab table. */
606 static int check_all(NOARGS)
608 struct fs_info *fs = NULL;
609 struct fsck_instance *inst;
610 int status = EXIT_OK;
611 int not_done_yet = 1;
616 printf("Checking all file systems.\n");
619 * Find and check the root filesystem first.
621 if (!parallel_root) {
622 for (fs = filesys_info; fs; fs = fs->next) {
623 if (!strcmp(fs->mountpt, "/"))
626 if (fs && !skip_root && !ignore(fs)) {
627 fsck_device(fs->device);
628 fs->flags |= FLAG_DONE;
629 status |= wait_all();
630 if (status > EXIT_NONDESTRUCT)
634 if (fs) fs->flags |= FLAG_DONE;
637 * Mark filesystems that should be ignored as done.
639 for (fs = filesys_info; fs; fs = fs->next) {
641 fs->flags |= FLAG_DONE;
644 while (not_done_yet) {
648 for (fs = filesys_info; fs; fs = fs->next) {
649 if (fs->flags & FLAG_DONE)
652 * If the filesystem's pass number is higher
653 * than the current pass number, then we don't
656 if (fs->passno > passno) {
661 * If a filesystem on a particular device has
662 * already been spawned, then we need to defer
663 * this to another pass.
665 if (device_already_active(fs->device)) {
670 * Spawn off the fsck process
672 fsck_device(fs->device);
673 fs->flags |= FLAG_DONE;
677 break; /* Only do one filesystem at a time */
683 status |= inst->exit_status;
687 status |= wait_all();
689 printf("----------------------------------\n");
694 status |= wait_all();
698 static void usage(NOARGS)
701 "Usage: fsck [-AV] [-t fstype] [fs-options] filesys\n");
705 static void PRS(int argc, char *argv[])
711 int opts_for_fsck = 0;
719 for (i=1; i < argc; i++) {
724 if (num_devices >= MAX_DEVICES) {
725 fprintf(stderr, "%s: too many devices\n",
729 devices[num_devices++] = string_copy(arg);
733 if (num_args >= MAX_ARGS) {
734 fprintf(stderr, "%s: too many arguments\n",
738 args[num_args++] = string_copy(arg);
741 for (j=1; arg[j]; j++) {
743 options[++opt] = arg[j];
773 fstype = string_copy(arg+j+1);
778 fstype = string_copy(argv[i]);
787 options[++opt] = arg[j];
794 options[++opt] = '\0';
795 if (num_args >= MAX_ARGS) {
797 "%s: too many arguments\n",
801 args[num_args++] = string_copy(options);
807 int main(int argc, char *argv[])
811 char *oldpath = getenv("PATH");
816 printf("Parallelizing fsck version %s (%s)\n",
817 E2FSPROGS_VERSION, E2FSPROGS_DATE);
819 load_fs_info(_PATH_MNTTAB);
821 /* Update our search path to include uncommon directories. */
823 fsck_path = malloc (strlen (fsck_prefix_path) + 1 +
824 strlen (oldpath) + 1);
825 strcpy (fsck_path, fsck_prefix_path);
826 strcat (fsck_path, ":");
827 strcat (fsck_path, oldpath);
829 fsck_path = string_copy(fsck_prefix_path);
832 /* If -A was specified ("check all"), do that! */
836 for (i = 0 ; i < num_devices; i++) {
837 fsck_device(devices[i]);
839 struct fsck_instance *inst;
843 status |= inst->exit_status;
848 status |= wait_all();