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 #define BASE_MD "/dev/md"
85 * Global variables for options
87 char *devices[MAX_DEVICES];
89 int num_devices, num_args;
98 int parallel_root = 0;
100 int force_all_parallel = 0;
103 struct fs_info *filesys_info;
104 struct fsck_instance *instance_list;
105 const char *fsck_prefix_path = "/sbin:/sbin/fs.d:/sbin/fs:/etc/fs:/etc";
107 static int ignore(struct fs_info *);
109 char *string_copy(const char *s)
113 ret = malloc(strlen(s)+1);
119 static char *skip_over_blank(char *cp)
121 while (*cp && isspace(*cp))
126 static char *skip_over_word(char *cp)
128 while (*cp && !isspace(*cp))
133 static void strip_line(char *line)
138 p = line + strlen(line) - 1;
139 if ((*p == '\n') || (*p == '\r'))
146 static char *parse_word(char **buf)
154 word = skip_over_blank(word);
155 next = skip_over_word(word);
162 static void free_instance(struct fsck_instance *i)
169 free(i->base_device);
174 static int parse_fstab_line(char *line, struct fs_info **ret_fs)
176 char *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
181 if ((cp = strchr(line, '#')))
182 *cp = 0; /* Ignore everything after the comment char */
185 device = parse_word(&cp);
186 mntpnt = parse_word(&cp);
187 type = parse_word(&cp);
188 opts = parse_word(&cp);
189 freq = parse_word(&cp);
190 passno = parse_word(&cp);
193 return 0; /* Allow blank lines */
195 if (!mntpnt || !type)
198 if (!(fs = malloc(sizeof(struct fs_info))))
201 fs->device = string_copy(device);
202 fs->mountpt = string_copy(mntpnt);
203 fs->type = string_copy(type);
204 fs->opts = string_copy(opts ? opts : "");
205 fs->freq = freq ? atoi(freq) : -1;
206 fs->passno = passno ? atoi(passno) : -1;
216 * Interpret the device name if necessary
218 static char *interpret_device(char *spec)
222 if (!strncmp(spec, "UUID=", 5))
223 dev = get_spec_by_uuid(spec+5);
224 else if (!strncmp(spec, "LABEL=", 6))
225 dev = get_spec_by_volume_label(spec+6);
233 * Check to see if this was because /proc/partitions isn't
236 if (access("/proc/partitions", R_OK) < 0) {
237 fprintf(stderr, "Couldn't open /proc/partitions: %s\n",
239 fprintf(stderr, "Is /proc mounted?\n");
243 * Check to see if this is because we're not running as root
246 fprintf(stderr, "Must be root to scan for matching "
247 "filesystems: %s\n", spec);
249 fprintf(stderr, "Couldn't find matching filesystem: %s\n",
255 * Load the filesystem database from /etc/fstab
257 static void load_fs_info(const char *filename)
263 struct fs_info *fs, *fs_last = NULL;
266 if ((f = fopen(filename, "r")) == NULL) {
267 fprintf(stderr, _("WARNING: couldn't open %s: %s\n"),
268 filename, strerror(errno));
273 if (!fgets(buf, sizeof(buf), f))
275 buf[sizeof(buf)-1] = 0;
276 if (parse_fstab_line(buf, &fs) < 0) {
277 fprintf(stderr, _("WARNING: bad format "
278 "on line %d of %s\n"), lineno, filename);
297 fprintf(stderr, _("\007\007\007"
298 "WARNING: Your /etc/fstab does not contain the fsck passno\n"
299 " field. I will kludge around things for you, but you\n"
300 " should fix your /etc/fstab file as soon as you can.\n\n"));
302 for (fs = filesys_info; fs; fs = fs->next) {
308 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
309 static struct fs_info *lookup(char *filesys)
314 /* No filesys name given. */
318 for (fs = filesys_info; fs; fs = fs->next) {
319 if (strchr(fs->device, '='))
321 if (!strcmp(filesys, fs->device) ||
322 !strcmp(filesys, fs->mountpt))
325 if (fs && strchr(fs->device, '='))
326 fs->device = interpret_device(fs->device);
328 if (fs || !try_again)
331 for (fs = filesys_info; fs; fs = fs->next) {
332 fs->device = interpret_device(fs->device);
333 if (!strcmp(filesys, fs->device) ||
334 !strcmp(filesys, fs->mountpt))
341 /* Find fsck program for a given fs type. */
342 static char *find_fsck(char *type)
346 static char prog[256];
347 char *p = string_copy(fsck_path);
350 /* Are we looking for a program or just a type? */
351 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
353 for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
354 sprintf(prog, tpl, s, type);
355 if (stat(prog, &st) == 0) break;
358 return(s ? prog : NULL);
361 static int progress_active(NOARGS)
363 struct fsck_instance *inst;
365 for (inst = instance_list; inst; inst = inst->next) {
366 if (inst->flags & FLAG_DONE)
368 if (inst->flags & FLAG_PROGRESS)
375 * Execute a particular fsck program, and link it into the list of
376 * child processes we are waiting for.
378 static int execute(const char *type, char *device, char *mntpt,
381 char *s, *argv[80], prog[80];
383 struct fsck_instance *inst, *p;
386 inst = malloc(sizeof(struct fsck_instance));
389 memset(inst, 0, sizeof(struct fsck_instance));
391 sprintf(prog, "fsck.%s", type);
392 argv[0] = string_copy(prog);
395 for (i=0; i <num_args; i++)
396 argv[argc++] = string_copy(args[i]);
398 if (progress & !progress_active()) {
399 if ((strcmp(type, "ext2") == 0) ||
400 (strcmp(type, "ext3") == 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->base_device = base_device(device);
444 inst->start_time = time(0);
448 * Find the end of the list, so we add the instance on at the end.
450 for (p = instance_list; p && p->next; p = p->next);
455 instance_list = inst;
461 * Wait for one child process to exit; when it does, unlink it from
462 * the list of executing child processes, and return it.
464 static struct fsck_instance *wait_one(NOARGS)
468 struct fsck_instance *inst, *inst2, *prev;
475 inst = instance_list;
476 instance_list = inst->next;
477 inst->exit_status = 0;
482 * gcc -Wall fails saving throw against stupidity
483 * (inst and prev are thought to be uninitialized variables)
490 if ((errno == EINTR) || (errno == EAGAIN))
492 if (errno == ECHILD) {
494 _("%s: wait: No more child process?!?\n"),
501 for (prev = 0, inst = instance_list;
503 prev = inst, inst = inst->next) {
504 if (inst->pid == pid)
509 if (WIFEXITED(status))
510 status = WEXITSTATUS(status);
511 else if (WIFSIGNALED(status)) {
512 sig = WTERMSIG(status);
514 status = EXIT_UNCORRECTED;
516 printf(_("Warning... %s for device %s exited "
517 "with signal %d.\n"),
518 inst->prog, inst->device, sig);
522 printf(_("%s %s: status is %x, should never happen.\n"),
523 inst->prog, inst->device, status);
526 inst->exit_status = status;
528 prev->next = inst->next;
530 instance_list = inst->next;
531 if (progress && (inst->flags & FLAG_PROGRESS) &&
532 !progress_active()) {
533 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
534 if (inst2->flags & FLAG_DONE)
536 if (strcmp(inst2->type, "ext2") &&
537 strcmp(inst2->type, "ext3"))
540 * If we've just started the fsck, wait a tiny
541 * bit before sending the kill, to give it
542 * time to set up the signal handler
544 if (inst2->start_time < time(0)+2) {
547 kill(inst2->pid, SIGUSR1);
551 kill(inst2->pid, SIGUSR1);
552 inst2->flags |= FLAG_PROGRESS;
560 * Wait until all executing child processes have exited; return the
561 * logical OR of all of their exit code values.
563 static int wait_all(NOARGS)
565 struct fsck_instance *inst;
566 int global_status = 0;
568 while (instance_list) {
572 global_status |= inst->exit_status;
575 return global_status;
579 * Run the fsck program on a particular device
581 * If the type is specified using -t, and it isn't prefixed with "no"
582 * (as in "noext2") and only one filesystem type is specified, then
583 * use that type regardless of what is specified in /etc/fstab.
585 * If the type isn't specified by the user, then use either the type
586 * specified in /etc/fstab, or DEFAULT_FSTYPE.
588 static void fsck_device(char *device, int interactive)
590 const char *type = 0;
591 struct fs_info *fsent;
594 if (fstype && strncmp(fstype, "no", 2) && !strchr(fstype, ','))
597 if ((fsent = lookup(device))) {
598 device = fsent->device;
603 type = DEFAULT_FSTYPE;
605 retval = execute(type, device, fsent ? fsent->mountpt : 0,
608 fprintf(stderr, _("%s: Error %d while executing fsck.%s "
609 "for %s\n"), progname, retval, type, device);
613 /* See if filesystem type matches the list. */
614 static int fs_match(char *type, char *fs_type)
616 int ret = 0, negate = 0;
620 if (!fs_type) return(1);
622 if (strncmp(fs_type, "no", 2) == 0) {
626 strcpy(list, fs_type);
627 s = strtok(list, ",");
629 if (strcmp(s, type) == 0) {
633 s = strtok(NULL, ",");
635 return(negate ? !ret : ret);
639 /* Check if we should ignore this filesystem. */
640 static int ignore(struct fs_info *fs)
646 * If the pass number is 0, ignore it.
652 * If a specific fstype is specified, and it doesn't match,
655 if (!fs_match(fs->type, fstype)) return 1;
657 /* Are we ignoring this type? */
658 for(ip = ignored_types; *ip; ip++)
659 if (strcmp(fs->type, *ip) == 0) return 1;
661 /* Do we really really want to check this fs? */
662 for(ip = really_wanted; *ip; ip++)
663 if (strcmp(fs->type, *ip) == 0) {
668 /* See if the <fsck.fs> program is available. */
669 if (find_fsck(fs->type) == NULL) {
671 fprintf(stderr, _("fsck: cannot check %s: fsck.%s not found\n"),
672 fs->device, fs->type);
676 /* We can and want to check this file system type. */
681 * Returns TRUE if a partition on the same disk is already being
684 static int device_already_active(char *device)
686 struct fsck_instance *inst;
689 if (force_all_parallel)
693 /* Don't check a soft raid disk with any other disk */
695 (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1) ||
696 !strncmp(device, BASE_MD, sizeof(BASE_MD)-1)))
700 base = base_device(device);
703 for (inst = instance_list; inst; inst = inst->next) {
704 if (!strcmp(base, inst->base_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 * Do an initial scan over the filesystem; mark filesystems
728 * which should be ignored as done, and resolve LABEL= and
729 * UUID= specifications to the real device.
731 for (fs = filesys_info; fs; fs = fs->next) {
733 fs->flags |= FLAG_DONE;
734 fs->device = interpret_device(fs->device);
738 * Find and check the root filesystem.
740 if (!parallel_root) {
741 for (fs = filesys_info; fs; fs = fs->next) {
742 if (!strcmp(fs->mountpt, "/"))
746 if (!skip_root && !ignore(fs)) {
747 fsck_device(fs->device, 1);
748 status |= wait_all();
749 if (status > EXIT_NONDESTRUCT)
752 fs->flags |= FLAG_DONE;
756 while (not_done_yet) {
760 for (fs = filesys_info; fs; fs = fs->next) {
761 if (fs->flags & FLAG_DONE)
764 * If the filesystem's pass number is higher
765 * than the current pass number, then we don't
768 if (fs->passno > passno) {
773 * If a filesystem on a particular device has
774 * already been spawned, then we need to defer
775 * this to another pass.
777 if (device_already_active(fs->device)) {
782 * Spawn off the fsck process
784 fsck_device(fs->device, serialize);
785 fs->flags |= FLAG_DONE;
789 break; /* Only do one filesystem at a time */
793 printf(_("--waiting-- (pass %d)\n"), passno);
796 status |= inst->exit_status;
800 status |= wait_all();
802 printf("----------------------------------\n");
807 status |= wait_all();
811 static void usage(NOARGS)
814 _("Usage: fsck [-ACNPRTV] [-t fstype] [fs-options] filesys\n"));
818 static void PRS(int argc, char *argv[])
824 int opts_for_fsck = 0;
832 for (i=1; i < argc; i++) {
836 if ((arg[0] == '/' && !opts_for_fsck) ||
837 (strncmp(arg, "LABEL=", 6) == 0) ||
838 (strncmp(arg, "UUID=", 5) == 0)) {
839 if (num_devices >= MAX_DEVICES) {
840 fprintf(stderr, _("%s: too many devices\n"),
844 devices[num_devices++] =
845 interpret_device(string_copy(arg));
848 if (arg[0] != '-' || opts_for_fsck) {
849 if (num_args >= MAX_ARGS) {
850 fprintf(stderr, _("%s: too many arguments\n"),
854 args[num_args++] = string_copy(arg);
857 for (j=1; arg[j]; j++) {
859 options[++opt] = arg[j];
892 fstype = string_copy(arg+j+1);
897 fstype = string_copy(argv[i]);
906 options[++opt] = arg[j];
913 options[++opt] = '\0';
914 if (num_args >= MAX_ARGS) {
916 _("%s: too many arguments\n"),
920 args[num_args++] = string_copy(options);
924 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
925 force_all_parallel++;
928 int main(int argc, char *argv[])
933 char *oldpath = getenv("PATH");
937 setlocale(LC_MESSAGES, "");
938 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
939 textdomain(NLS_CAT_NAME);
944 printf(_("Parallelizing fsck version %s (%s)\n"),
945 E2FSPROGS_VERSION, E2FSPROGS_DATE);
947 fstab = getenv("FSTAB_FILE");
949 fstab = _PATH_MNTTAB;
952 /* Update our search path to include uncommon directories. */
954 fsck_path = malloc (strlen (fsck_prefix_path) + 1 +
955 strlen (oldpath) + 1);
956 strcpy (fsck_path, fsck_prefix_path);
957 strcat (fsck_path, ":");
958 strcat (fsck_path, oldpath);
960 fsck_path = string_copy(fsck_prefix_path);
963 if ((num_devices == 1) || (serialize))
966 /* If -A was specified ("check all"), do that! */
970 for (i = 0 ; i < num_devices; i++) {
971 fsck_device(devices[i], interactive);
973 struct fsck_instance *inst;
977 status |= inst->exit_status;
982 status |= wait_all();