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"
56 #include "nls-enable.h"
58 #include "get_device_by_label.h"
61 #define _PATH_MNTTAB "/etc/fstab"
64 static const char *ignored_types[] = {
74 static const char *really_wanted[] = {
82 #ifdef DEV_DSK_DEVICES
83 static const char *base_devices[] = {
102 static const char *base_devices[] = {
123 * Global variables for options
125 char *devices[MAX_DEVICES];
126 char *args[MAX_ARGS];
127 int num_devices, num_args;
136 int parallel_root = 0;
138 int force_all_parallel = 0;
141 struct fs_info *filesys_info;
142 struct fsck_instance *instance_list;
143 const char *fsck_prefix_path = "/sbin:/sbin/fs.d:/sbin/fs:/etc/fs:/etc";
145 static int ignore(struct fs_info *);
147 static char *string_copy(const char *s)
151 ret = malloc(strlen(s)+1);
157 static char *skip_over_blank(char *cp)
159 while (*cp && isspace(*cp))
164 static char *skip_over_word(char *cp)
166 while (*cp && !isspace(*cp))
171 static void strip_line(char *line)
176 p = line + strlen(line) - 1;
177 if ((*p == '\n') || (*p == '\r'))
184 static char *parse_word(char **buf)
192 word = skip_over_blank(word);
193 next = skip_over_word(word);
200 static void free_instance(struct fsck_instance *i)
210 static int parse_fstab_line(char *line, struct fs_info **ret_fs)
212 char *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
217 if ((cp = strchr(line, '#')))
218 *cp = 0; /* Ignore everything after the comment char */
221 device = parse_word(&cp);
222 mntpnt = parse_word(&cp);
223 type = parse_word(&cp);
224 opts = parse_word(&cp);
225 freq = parse_word(&cp);
226 passno = parse_word(&cp);
229 return 0; /* Allow blank lines */
231 if (!mntpnt || !type)
234 if (!(fs = malloc(sizeof(struct fs_info))))
237 fs->device = string_copy(device);
238 fs->mountpt = string_copy(mntpnt);
239 fs->type = string_copy(type);
240 fs->opts = string_copy(opts ? opts : "");
241 fs->freq = freq ? atoi(freq) : -1;
242 fs->passno = passno ? atoi(passno) : -1;
252 * Interpret the device name if necessary
254 static char *interpret_device(char *spec)
258 if (!strncmp(spec, "UUID=", 5))
259 dev = get_spec_by_uuid(spec+5);
260 else if (!strncmp(spec, "LABEL=", 6))
261 dev = get_spec_by_volume_label(spec+6);
269 * Check to see if this was because /proc/partitions isn't
272 if (access("/proc/partitions", R_OK) < 0) {
273 fprintf(stderr, "Couldn't open /proc/partitions: %s\n",
275 fprintf(stderr, "Is /proc mounted?\n");
279 * Check to see if this is because we're not running as root
282 fprintf(stderr, "Must be root to scan for matching "
283 "filesystems: %s\n", spec);
285 fprintf(stderr, "Couldn't find matching filesystem: %s\n",
291 * Load the filesystem database from /etc/fstab
293 static void load_fs_info(const char *filename)
299 struct fs_info *fs, *fs_last = NULL;
302 if ((f = fopen(filename, "r")) == NULL) {
303 fprintf(stderr, _("WARNING: couldn't open %s: %s\n"),
304 filename, strerror(errno));
309 if (!fgets(buf, sizeof(buf), f))
311 buf[sizeof(buf)-1] = 0;
312 if (parse_fstab_line(buf, &fs) < 0) {
313 fprintf(stderr, _("WARNING: bad format "
314 "on line %d of %s\n"), lineno, filename);
333 fprintf(stderr, _("\007\007\007"
334 "WARNING: Your /etc/fstab does not contain the fsck passno\n"
335 " field. I will kludge around things for you, but you\n"
336 " should fix your /etc/fstab file as soon as you can.\n\n"));
338 for (fs = filesys_info; fs; fs = fs->next) {
344 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
345 static struct fs_info *lookup(char *filesys)
350 /* No filesys name given. */
354 for (fs = filesys_info; fs; fs = fs->next) {
355 if (strchr(fs->device, '='))
357 if (!strcmp(filesys, fs->device) ||
358 !strcmp(filesys, fs->mountpt))
361 if (fs && strchr(fs->device, '='))
362 fs->device = interpret_device(fs->device);
364 if (fs || !try_again)
367 for (fs = filesys_info; fs; fs = fs->next) {
368 fs->device = interpret_device(fs->device);
369 if (!strcmp(filesys, fs->device) ||
370 !strcmp(filesys, fs->mountpt))
377 /* Find fsck program for a given fs type. */
378 static char *find_fsck(char *type)
382 static char prog[256];
383 char *p = string_copy(fsck_path);
386 /* Are we looking for a program or just a type? */
387 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
389 for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
390 sprintf(prog, tpl, s, type);
391 if (stat(prog, &st) == 0) break;
394 return(s ? prog : NULL);
397 static int progress_active(NOARGS)
399 struct fsck_instance *inst;
401 for (inst = instance_list; inst; inst = inst->next) {
402 if (inst->flags & FLAG_DONE)
404 if (inst->flags & FLAG_PROGRESS)
411 * Execute a particular fsck program, and link it into the list of
412 * child processes we are waiting for.
414 static int execute(const char *type, char *device, char *mntpt,
417 char *s, *argv[80], prog[80];
419 struct fsck_instance *inst, *p;
422 inst = malloc(sizeof(struct fsck_instance));
425 memset(inst, 0, sizeof(struct fsck_instance));
427 sprintf(prog, "fsck.%s", type);
428 argv[0] = string_copy(prog);
431 for (i=0; i <num_args; i++)
432 argv[argc++] = string_copy(args[i]);
434 if (progress & !progress_active()) {
435 if ((strcmp(type, "ext2") == 0) ||
436 (strcmp(type, "ext3") == 0)) {
437 argv[argc++] = string_copy("-C0");
438 inst->flags |= FLAG_PROGRESS;
442 argv[argc++] = string_copy(device);
447 fprintf(stderr, _("fsck: %s: not found\n"), prog);
451 if (verbose || noexecute) {
452 printf("[%s -- %s] ", s, mntpt ? mntpt : device);
453 for (i=0; i < argc; i++)
454 printf("%s ", argv[i]);
458 /* Fork and execute the correct program. */
461 else if ((pid = fork()) < 0) {
464 } else if (pid == 0) {
467 (void) execv(s, argv);
472 for (i=0; i < argc; i++)
476 inst->prog = string_copy(prog);
477 inst->type = string_copy(type);
478 inst->device = string_copy(device);
479 inst->start_time = time(0);
483 * Find the end of the list, so we add the instance on at the end.
485 for (p = instance_list; p && p->next; p = p->next);
490 instance_list = inst;
496 * Wait for one child process to exit; when it does, unlink it from
497 * the list of executing child processes, and return it.
499 static struct fsck_instance *wait_one(NOARGS)
503 struct fsck_instance *inst, *inst2, *prev;
510 inst = instance_list;
511 instance_list = inst->next;
512 inst->exit_status = 0;
517 * gcc -Wall fails saving throw against stupidity
518 * (inst and prev are thought to be uninitialized variables)
525 if ((errno == EINTR) || (errno == EAGAIN))
527 if (errno == ECHILD) {
529 _("%s: wait: No more child process?!?\n"),
536 for (prev = 0, inst = instance_list;
538 prev = inst, inst = inst->next) {
539 if (inst->pid == pid)
544 if (WIFEXITED(status))
545 status = WEXITSTATUS(status);
546 else if (WIFSIGNALED(status)) {
547 sig = WTERMSIG(status);
549 status = EXIT_UNCORRECTED;
551 printf(_("Warning... %s for device %s exited "
552 "with signal %d.\n"),
553 inst->prog, inst->device, sig);
557 printf(_("%s %s: status is %x, should never happen.\n"),
558 inst->prog, inst->device, status);
561 inst->exit_status = status;
563 prev->next = inst->next;
565 instance_list = inst->next;
566 if (progress && (inst->flags & FLAG_PROGRESS) &&
567 !progress_active()) {
568 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
569 if (inst2->flags & FLAG_DONE)
571 if (strcmp(inst2->type, "ext2") &&
572 strcmp(inst2->type, "ext3"))
575 * If we've just started the fsck, wait a tiny
576 * bit before sending the kill, to give it
577 * time to set up the signal handler
579 if (inst2->start_time < time(0)+2) {
582 kill(inst2->pid, SIGUSR1);
586 kill(inst2->pid, SIGUSR1);
594 * Wait until all executing child processes have exited; return the
595 * logical OR of all of their exit code values.
597 static int wait_all(NOARGS)
599 struct fsck_instance *inst;
600 int global_status = 0;
602 while (instance_list) {
606 global_status |= inst->exit_status;
609 return global_status;
613 * Run the fsck program on a particular device
615 * If the type is specified using -t, and it isn't prefixed with "no"
616 * (as in "noext2") and only one filesystem type is specified, then
617 * use that type regardless of what is specified in /etc/fstab.
619 * If the type isn't specified by the user, then use either the type
620 * specified in /etc/fstab, or DEFAULT_FSTYPE.
622 static void fsck_device(char *device, int interactive)
624 const char *type = 0;
625 struct fs_info *fsent;
628 if (fstype && strncmp(fstype, "no", 2) && !strchr(fstype, ','))
631 if ((fsent = lookup(device))) {
632 device = fsent->device;
637 type = DEFAULT_FSTYPE;
639 retval = execute(type, device, fsent ? fsent->mountpt : 0,
642 fprintf(stderr, _("%s: Error %d while executing fsck.%s "
643 "for %s\n"), progname, retval, type, device);
647 /* See if filesystem type matches the list. */
648 static int fs_match(char *type, char *fs_type)
650 int ret = 0, negate = 0;
654 if (!fs_type) return(1);
656 if (strncmp(fs_type, "no", 2) == 0) {
660 strcpy(list, fs_type);
661 s = strtok(list, ",");
663 if (strcmp(s, type) == 0) {
667 s = strtok(NULL, ",");
669 return(negate ? !ret : ret);
673 /* Check if we should ignore this filesystem. */
674 static int ignore(struct fs_info *fs)
680 * If the pass number is 0, ignore it.
686 * If a specific fstype is specified, and it doesn't match,
689 if (!fs_match(fs->type, fstype)) return 1;
691 /* Are we ignoring this type? */
692 for(ip = ignored_types; *ip; ip++)
693 if (strcmp(fs->type, *ip) == 0) return(1);
695 /* Do we really really want to check this fs? */
696 for(ip = really_wanted; *ip; ip++)
697 if (strcmp(fs->type, *ip) == 0) {
702 /* See if the <fsck.fs> program is available. */
703 if (find_fsck(fs->type) == NULL) {
705 fprintf(stderr, _("fsck: cannot check %s: fsck.%s not found\n"),
706 fs->device, fs->type);
710 /* We can and want to check this file system type. */
715 * Return the "base device" given a particular device; this is used to
716 * assure that we only fsck one partition on a particular drive at any
717 * one time. Otherwise, the disk heads will be seeking all over the
720 static const char *base_device(char *device)
724 for (base = base_devices; *base; base++) {
725 if (!strncmp(*base, device, strlen(*base)))
732 * Returns TRUE if a partition on the same disk is already being
735 static int device_already_active(char *device)
737 struct fsck_instance *inst;
738 const char *base = base_device(device);
740 if (force_all_parallel)
743 for (inst = instance_list; inst; inst = inst->next) {
744 if (!strcmp(base, base_device(inst->device)))
750 /* Check all file systems, using the /etc/fstab table. */
751 static int check_all(NOARGS)
753 struct fs_info *fs = NULL;
754 struct fsck_instance *inst;
755 int status = EXIT_OK;
756 int not_done_yet = 1;
761 printf(_("Checking all file systems.\n"));
764 * Find and check the root filesystem first.
766 if (!parallel_root) {
767 for (fs = filesys_info; fs; fs = fs->next) {
768 if (!strcmp(fs->mountpt, "/"))
771 if (fs && !skip_root && !ignore(fs)) {
772 fs->device = interpret_device(fs->device);
773 fsck_device(fs->device, 1);
774 fs->flags |= FLAG_DONE;
775 status |= wait_all();
776 if (status > EXIT_NONDESTRUCT)
780 if (fs) fs->flags |= FLAG_DONE;
783 * Mark filesystems that should be ignored as done.
785 for (fs = filesys_info; fs; fs = fs->next) {
787 fs->flags |= FLAG_DONE;
790 while (not_done_yet) {
794 for (fs = filesys_info; fs; fs = fs->next) {
795 if (fs->flags & FLAG_DONE)
798 * If the filesystem's pass number is higher
799 * than the current pass number, then we don't
802 if (fs->passno > passno) {
807 * If a filesystem on a particular device has
808 * already been spawned, then we need to defer
809 * this to another pass.
811 if (device_already_active(fs->device)) {
816 * Spawn off the fsck process
818 fs->device = interpret_device(fs->device);
819 fsck_device(fs->device, serialize);
820 fs->flags |= FLAG_DONE;
824 break; /* Only do one filesystem at a time */
828 printf(_("--waiting-- (pass %d)\n"), passno);
831 status |= inst->exit_status;
835 status |= wait_all();
837 printf("----------------------------------\n");
842 status |= wait_all();
846 static void usage(NOARGS)
849 _("Usage: fsck [-ACNPRTV] [-t fstype] [fs-options] filesys\n"));
853 static void PRS(int argc, char *argv[])
859 int opts_for_fsck = 0;
867 for (i=1; i < argc; i++) {
871 if ((arg[0] == '/' && !opts_for_fsck) ||
872 (strncmp(arg, "LABEL=", 6) == 0) ||
873 (strncmp(arg, "UUID=", 5) == 0)) {
874 if (num_devices >= MAX_DEVICES) {
875 fprintf(stderr, _("%s: too many devices\n"),
879 devices[num_devices++] =
880 interpret_device(string_copy(arg));
883 if (arg[0] != '-' || opts_for_fsck) {
884 if (num_args >= MAX_ARGS) {
885 fprintf(stderr, _("%s: too many arguments\n"),
889 args[num_args++] = string_copy(arg);
892 for (j=1; arg[j]; j++) {
894 options[++opt] = arg[j];
927 fstype = string_copy(arg+j+1);
932 fstype = string_copy(argv[i]);
941 options[++opt] = arg[j];
948 options[++opt] = '\0';
949 if (num_args >= MAX_ARGS) {
951 _("%s: too many arguments\n"),
955 args[num_args++] = string_copy(options);
959 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
960 force_all_parallel++;
963 int main(int argc, char *argv[])
968 char *oldpath = getenv("PATH");
972 setlocale(LC_MESSAGES, "");
973 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
974 textdomain(NLS_CAT_NAME);
979 printf(_("Parallelizing fsck version %s (%s)\n"),
980 E2FSPROGS_VERSION, E2FSPROGS_DATE);
982 fstab = getenv("FSTAB_FILE");
984 fstab = _PATH_MNTTAB;
987 /* Update our search path to include uncommon directories. */
989 fsck_path = malloc (strlen (fsck_prefix_path) + 1 +
990 strlen (oldpath) + 1);
991 strcpy (fsck_path, fsck_prefix_path);
992 strcat (fsck_path, ":");
993 strcat (fsck_path, oldpath);
995 fsck_path = string_copy(fsck_prefix_path);
998 if ((num_devices == 1) || (serialize))
1001 /* If -A was specified ("check all"), do that! */
1005 for (i = 0 ; i < num_devices; i++) {
1006 fsck_device(devices[i], interactive);
1008 struct fsck_instance *inst;
1012 status |= inst->exit_status;
1013 free_instance(inst);
1017 status |= wait_all();