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[] = {
81 #ifdef DEV_DSK_DEVICES
82 static const char *base_devices[] = {
101 static const char *base_devices[] = {
122 * Global variables for options
124 char *devices[MAX_DEVICES];
125 char *args[MAX_ARGS];
126 int num_devices, num_args;
135 int parallel_root = 0;
137 int force_all_parallel = 0;
140 struct fs_info *filesys_info;
141 struct fsck_instance *instance_list;
142 const char *fsck_prefix_path = "/sbin:/sbin/fs.d:/sbin/fs:/etc/fs:/etc";
144 static int ignore(struct fs_info *);
146 static char *string_copy(const char *s)
150 ret = malloc(strlen(s)+1);
156 static char *skip_over_blank(char *cp)
158 while (*cp && isspace(*cp))
163 static char *skip_over_word(char *cp)
165 while (*cp && !isspace(*cp))
170 static void strip_line(char *line)
175 p = line + strlen(line) - 1;
176 if ((*p == '\n') || (*p == '\r'))
183 static char *parse_word(char **buf)
191 word = skip_over_blank(word);
192 next = skip_over_word(word);
199 static void free_instance(struct fsck_instance *i)
209 static int parse_fstab_line(char *line, struct fs_info **ret_fs)
211 char *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
216 if ((cp = strchr(line, '#')))
217 *cp = 0; /* Ignore everything after the comment char */
220 device = parse_word(&cp);
221 mntpnt = parse_word(&cp);
222 type = parse_word(&cp);
223 opts = parse_word(&cp);
224 freq = parse_word(&cp);
225 passno = parse_word(&cp);
228 return 0; /* Allow blank lines */
230 if (!mntpnt || !type)
233 if (!(fs = malloc(sizeof(struct fs_info))))
236 fs->device = string_copy(device);
237 fs->mountpt = string_copy(mntpnt);
238 fs->type = string_copy(type);
239 fs->opts = string_copy(opts ? opts : "");
240 fs->freq = freq ? atoi(freq) : -1;
241 fs->passno = passno ? atoi(passno) : -1;
251 * Interpret the device name if necessary
253 static char *interpret_device(char *spec)
257 if (!strncmp(spec, "UUID=", 5))
258 dev = get_spec_by_uuid(spec+5);
259 else if (!strncmp(spec, "LABEL=", 6))
260 dev = get_spec_by_volume_label(spec+6);
268 * Check to see if this was because /proc/partitions isn't
271 if (access("/proc/partitions", R_OK) < 0) {
272 fprintf(stderr, "Couldn't open /proc/partitions: %s\n",
274 fprintf(stderr, "Is /proc mounted?\n");
278 * Check to see if this is because we're not running as root
281 fprintf(stderr, "Must be root to scan for matching "
282 "filesystems: %s\n", spec);
284 fprintf(stderr, "Couldn't find matching filesystem: %s\n",
290 * Load the filesystem database from /etc/fstab
292 static void load_fs_info(const char *filename)
298 struct fs_info *fs, *fs_last = NULL;
301 if ((f = fopen(filename, "r")) == NULL) {
302 fprintf(stderr, _("WARNING: couldn't open %s: %s\n"),
303 filename, strerror(errno));
308 if (!fgets(buf, sizeof(buf), f))
310 buf[sizeof(buf)-1] = 0;
311 if (parse_fstab_line(buf, &fs) < 0) {
312 fprintf(stderr, _("WARNING: bad format "
313 "on line %d of %s\n"), lineno, filename);
332 fprintf(stderr, _("\007\007\007"
333 "WARNING: Your /etc/fstab does not contain the fsck passno\n"
334 " field. I will kludge around things for you, but you\n"
335 " should fix your /etc/fstab file as soon as you can.\n\n"));
337 for (fs = filesys_info; fs; fs = fs->next) {
343 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
344 static struct fs_info *lookup(char *filesys)
349 /* No filesys name given. */
353 for (fs = filesys_info; fs; fs = fs->next) {
354 if (strchr(fs->device, '='))
356 if (!strcmp(filesys, fs->device) ||
357 !strcmp(filesys, fs->mountpt))
361 if (fs || !try_again)
364 for (fs = filesys_info; fs; fs = fs->next) {
365 fs->device = interpret_device(fs->device);
366 if (!strcmp(filesys, fs->device) ||
367 !strcmp(filesys, fs->mountpt))
374 /* Find fsck program for a given fs type. */
375 static char *find_fsck(char *type)
379 static char prog[256];
380 char *p = string_copy(fsck_path);
383 /* Are we looking for a program or just a type? */
384 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
386 for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
387 sprintf(prog, tpl, s, type);
388 if (stat(prog, &st) == 0) break;
391 return(s ? prog : NULL);
394 static int progress_active(NOARGS)
396 struct fsck_instance *inst;
398 for (inst = instance_list; inst; inst = inst->next) {
399 if (inst->flags & FLAG_DONE)
401 if (inst->flags & FLAG_PROGRESS)
408 * Execute a particular fsck program, and link it into the list of
409 * child processes we are waiting for.
411 static int execute(const char *type, char *device, char *mntpt,
414 char *s, *argv[80], prog[80];
416 struct fsck_instance *inst, *p;
419 inst = malloc(sizeof(struct fsck_instance));
422 memset(inst, 0, sizeof(struct fsck_instance));
424 sprintf(prog, "fsck.%s", type);
425 argv[0] = string_copy(prog);
428 for (i=0; i <num_args; i++)
429 argv[argc++] = string_copy(args[i]);
431 if (progress & !progress_active()) {
432 if (strcmp(type, "ext2") == 0) {
433 argv[argc++] = string_copy("-C0");
434 inst->flags |= FLAG_PROGRESS;
438 argv[argc++] = string_copy(device);
443 fprintf(stderr, _("fsck: %s: not found\n"), prog);
447 if (verbose || noexecute) {
448 printf("[%s -- %s] ", s, mntpt ? mntpt : device);
449 for (i=0; i < argc; i++)
450 printf("%s ", argv[i]);
454 /* Fork and execute the correct program. */
457 else if ((pid = fork()) < 0) {
460 } else if (pid == 0) {
463 (void) execv(s, argv);
468 for (i=0; i < argc; i++)
472 inst->prog = string_copy(prog);
473 inst->type = string_copy(type);
474 inst->device = string_copy(device);
475 inst->start_time = time(0);
479 * Find the end of the list, so we add the instance on at the end.
481 for (p = instance_list; p && p->next; p = p->next);
486 instance_list = inst;
492 * Wait for one child process to exit; when it does, unlink it from
493 * the list of executing child processes, and return it.
495 static struct fsck_instance *wait_one(NOARGS)
499 struct fsck_instance *inst, *inst2, *prev;
506 inst = instance_list;
507 instance_list = inst->next;
508 inst->exit_status = 0;
513 * gcc -Wall fails saving throw against stupidity
514 * (inst and prev are thought to be uninitialized variables)
521 if ((errno == EINTR) || (errno == EAGAIN))
523 if (errno == ECHILD) {
525 _("%s: wait: No more child process?!?\n"),
532 for (prev = 0, inst = instance_list;
534 prev = inst, inst = inst->next) {
535 if (inst->pid == pid)
540 if (WIFEXITED(status))
541 status = WEXITSTATUS(status);
542 else if (WIFSIGNALED(status)) {
543 sig = WTERMSIG(status);
545 status = EXIT_UNCORRECTED;
547 printf(_("Warning... %s for device %s exited "
548 "with signal %d.\n"),
549 inst->prog, inst->device, sig);
553 printf(_("%s %s: status is %x, should never happen.\n"),
554 inst->prog, inst->device, status);
557 inst->exit_status = status;
559 prev->next = inst->next;
561 instance_list = inst->next;
562 if (progress && (inst->flags & FLAG_PROGRESS) &&
563 !progress_active()) {
564 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
565 if (inst2->flags & FLAG_DONE)
567 if (strcmp(inst2->type, "ext2"))
570 * If we've just started the fsck, wait a tiny
571 * bit before sending the kill, to give it
572 * time to set up the signal handler
574 if (inst2->start_time < time(0)+2) {
577 kill(inst2->pid, SIGUSR1);
581 kill(inst2->pid, SIGUSR1);
589 * Wait until all executing child processes have exited; return the
590 * logical OR of all of their exit code values.
592 static int wait_all(NOARGS)
594 struct fsck_instance *inst;
595 int global_status = 0;
597 while (instance_list) {
601 global_status |= inst->exit_status;
604 return global_status;
608 * Run the fsck program on a particular device
610 * If the type is specified using -t, and it isn't prefixed with "no"
611 * (as in "noext2") and only one filesystem type is specified, then
612 * use that type regardless of what is specified in /etc/fstab.
614 * If the type isn't specified by the user, then use either the type
615 * specified in /etc/fstab, or DEFAULT_FSTYPE.
617 static void fsck_device(char *device, int interactive)
619 const char *type = 0;
620 struct fs_info *fsent;
623 if (fstype && strncmp(fstype, "no", 2) && !strchr(fstype, ','))
626 if ((fsent = lookup(device))) {
627 device = fsent->device;
632 type = DEFAULT_FSTYPE;
634 retval = execute(type, device, fsent ? fsent->mountpt : 0,
637 fprintf(stderr, _("%s: Error %d while executing fsck.%s "
638 "for %s\n"), progname, retval, type, device);
642 /* See if filesystem type matches the list. */
643 static int fs_match(char *type, char *fs_type)
645 int ret = 0, negate = 0;
649 if (!fs_type) return(1);
651 if (strncmp(fs_type, "no", 2) == 0) {
655 strcpy(list, fs_type);
656 s = strtok(list, ",");
658 if (strcmp(s, type) == 0) {
662 s = strtok(NULL, ",");
664 return(negate ? !ret : ret);
668 /* Check if we should ignore this filesystem. */
669 static int ignore(struct fs_info *fs)
675 * If the pass number is 0, ignore it.
681 * If a specific fstype is specified, and it doesn't match,
684 if (!fs_match(fs->type, fstype)) return 1;
686 /* Are we ignoring this type? */
687 for(ip = ignored_types; *ip; ip++)
688 if (strcmp(fs->type, *ip) == 0) return(1);
690 /* Do we really really want to check this fs? */
691 for(ip = really_wanted; *ip; ip++)
692 if (strcmp(fs->type, *ip) == 0) {
697 /* See if the <fsck.fs> program is available. */
698 if (find_fsck(fs->type) == NULL) {
700 fprintf(stderr, _("fsck: cannot check %s: fsck.%s not found\n"),
701 fs->device, fs->type);
705 /* We can and want to check this file system type. */
710 * Return the "base device" given a particular device; this is used to
711 * assure that we only fsck one partition on a particular drive at any
712 * one time. Otherwise, the disk heads will be seeking all over the
715 static const char *base_device(char *device)
719 for (base = base_devices; *base; base++) {
720 if (!strncmp(*base, device, strlen(*base)))
727 * Returns TRUE if a partition on the same disk is already being
730 static int device_already_active(char *device)
732 struct fsck_instance *inst;
733 const char *base = base_device(device);
735 if (force_all_parallel)
738 for (inst = instance_list; inst; inst = inst->next) {
739 if (!strcmp(base, base_device(inst->device)))
745 /* Check all file systems, using the /etc/fstab table. */
746 static int check_all(NOARGS)
748 struct fs_info *fs = NULL;
749 struct fsck_instance *inst;
750 int status = EXIT_OK;
751 int not_done_yet = 1;
756 printf(_("Checking all file systems.\n"));
759 * Find and check the root filesystem first.
761 if (!parallel_root) {
762 for (fs = filesys_info; fs; fs = fs->next) {
763 if (!strcmp(fs->mountpt, "/"))
766 if (fs && !skip_root && !ignore(fs)) {
767 fs->device = interpret_device(fs->device);
768 fsck_device(fs->device, 1);
769 fs->flags |= FLAG_DONE;
770 status |= wait_all();
771 if (status > EXIT_NONDESTRUCT)
775 if (fs) fs->flags |= FLAG_DONE;
778 * Mark filesystems that should be ignored as done.
780 for (fs = filesys_info; fs; fs = fs->next) {
782 fs->flags |= FLAG_DONE;
785 while (not_done_yet) {
789 for (fs = filesys_info; fs; fs = fs->next) {
790 if (fs->flags & FLAG_DONE)
793 * If the filesystem's pass number is higher
794 * than the current pass number, then we don't
797 if (fs->passno > passno) {
802 * If a filesystem on a particular device has
803 * already been spawned, then we need to defer
804 * this to another pass.
806 if (device_already_active(fs->device)) {
811 * Spawn off the fsck process
813 fs->device = interpret_device(fs->device);
814 fsck_device(fs->device, serialize);
815 fs->flags |= FLAG_DONE;
819 break; /* Only do one filesystem at a time */
823 printf(_("--waiting-- (pass %d)\n"), passno);
826 status |= inst->exit_status;
830 status |= wait_all();
832 printf("----------------------------------\n");
837 status |= wait_all();
841 static void usage(NOARGS)
844 _("Usage: fsck [-ACNPRTV] [-t fstype] [fs-options] filesys\n"));
848 static void PRS(int argc, char *argv[])
854 int opts_for_fsck = 0;
862 for (i=1; i < argc; i++) {
866 if ((arg[0] == '/' && !opts_for_fsck) ||
867 (strncmp(arg, "LABEL=", 6) == 0) ||
868 (strncmp(arg, "UUID=", 5) == 0)) {
869 if (num_devices >= MAX_DEVICES) {
870 fprintf(stderr, _("%s: too many devices\n"),
874 devices[num_devices++] =
875 interpret_device(string_copy(arg));
878 if (arg[0] != '-' || opts_for_fsck) {
879 if (num_args >= MAX_ARGS) {
880 fprintf(stderr, _("%s: too many arguments\n"),
884 args[num_args++] = string_copy(arg);
887 for (j=1; arg[j]; j++) {
889 options[++opt] = arg[j];
922 fstype = string_copy(arg+j+1);
927 fstype = string_copy(argv[i]);
936 options[++opt] = arg[j];
943 options[++opt] = '\0';
944 if (num_args >= MAX_ARGS) {
946 _("%s: too many arguments\n"),
950 args[num_args++] = string_copy(options);
954 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
955 force_all_parallel++;
958 int main(int argc, char *argv[])
963 char *oldpath = getenv("PATH");
967 setlocale(LC_MESSAGES, "");
968 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
969 textdomain(NLS_CAT_NAME);
974 printf(_("Parallelizing fsck version %s (%s)\n"),
975 E2FSPROGS_VERSION, E2FSPROGS_DATE);
977 fstab = getenv("FSTAB_FILE");
979 fstab = _PATH_MNTTAB;
982 /* Update our search path to include uncommon directories. */
984 fsck_path = malloc (strlen (fsck_prefix_path) + 1 +
985 strlen (oldpath) + 1);
986 strcpy (fsck_path, fsck_prefix_path);
987 strcat (fsck_path, ":");
988 strcat (fsck_path, oldpath);
990 fsck_path = string_copy(fsck_prefix_path);
993 if ((num_devices == 1) || (serialize))
996 /* If -A was specified ("check all"), do that! */
1000 for (i = 0 ; i < num_devices; i++) {
1001 fsck_device(devices[i], interactive);
1003 struct fsck_instance *inst;
1007 status |= inst->exit_status;
1008 free_instance(inst);
1012 status |= wait_all();