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 volatile int cancel_requested = 0;
107 struct fs_info *filesys_info;
108 struct fsck_instance *instance_list;
109 const char *fsck_prefix_path = "/sbin:/sbin/fs.d:/sbin/fs:/etc/fs:/etc";
111 static int ignore(struct fs_info *);
113 static char *skip_over_blank(char *cp)
115 while (*cp && isspace(*cp))
120 static char *skip_over_word(char *cp)
122 while (*cp && !isspace(*cp))
127 static void strip_line(char *line)
132 p = line + strlen(line) - 1;
133 if ((*p == '\n') || (*p == '\r'))
140 static char *parse_word(char **buf)
148 word = skip_over_blank(word);
149 next = skip_over_word(word);
156 static void free_instance(struct fsck_instance *i)
163 free(i->base_device);
168 static int parse_fstab_line(char *line, struct fs_info **ret_fs)
170 char *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
175 if ((cp = strchr(line, '#')))
176 *cp = 0; /* Ignore everything after the comment char */
179 device = parse_word(&cp);
180 mntpnt = parse_word(&cp);
181 type = parse_word(&cp);
182 opts = parse_word(&cp);
183 freq = parse_word(&cp);
184 passno = parse_word(&cp);
187 return 0; /* Allow blank lines */
189 if (!mntpnt || !type)
192 if (!(fs = malloc(sizeof(struct fs_info))))
195 fs->device = string_copy(device);
196 fs->mountpt = string_copy(mntpnt);
197 fs->type = string_copy(type);
198 fs->opts = string_copy(opts ? opts : "");
199 fs->freq = freq ? atoi(freq) : -1;
200 fs->passno = passno ? atoi(passno) : -1;
210 * Interpret the device name if necessary
212 static char *interpret_device(char *spec)
214 char *dev = interpret_spec(spec);
220 * Check to see if this was because /proc/partitions isn't
223 if (access("/proc/partitions", R_OK) < 0) {
224 fprintf(stderr, "Couldn't open /proc/partitions: %s\n",
226 fprintf(stderr, "Is /proc mounted?\n");
230 * Check to see if this is because we're not running as root
234 "Must be root to scan for matching filesystems: %s\n",
237 fprintf(stderr, "Couldn't find matching filesystem: %s\n",
243 * Interpret filesystem auto type if necessary
245 static void interpret_type(struct fs_info *fs)
249 if (strcmp(fs->type, "auto") == 0 ||
250 (strchr(fs->type, ',') != 0)) {
251 if (fs && strchr(fs->device, '='))
252 fs->device = interpret_device(fs->device);
253 type = identify_fs(fs->device, fs->type);
256 fs->type = string_copy(type);
258 fprintf(stderr, _("Could not determine "
259 "filesystem type for %s\n"),
266 * Load the filesystem database from /etc/fstab
268 static void load_fs_info(const char *filename)
274 struct fs_info *fs, *fs_last = NULL;
277 if ((f = fopen(filename, "r")) == NULL) {
278 fprintf(stderr, _("WARNING: couldn't open %s: %s\n"),
279 filename, strerror(errno));
284 if (!fgets(buf, sizeof(buf), f))
286 buf[sizeof(buf)-1] = 0;
287 if (parse_fstab_line(buf, &fs) < 0) {
288 fprintf(stderr, _("WARNING: bad format "
289 "on line %d of %s\n"), lineno, filename);
308 fprintf(stderr, _("\007\007\007"
309 "WARNING: Your /etc/fstab does not contain the fsck passno\n"
310 " field. I will kludge around things for you, but you\n"
311 " should fix your /etc/fstab file as soon as you can.\n\n"));
313 for (fs = filesys_info; fs; fs = fs->next) {
319 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
320 static struct fs_info *lookup(char *filesys)
325 /* No filesys name given. */
329 for (fs = filesys_info; fs; fs = fs->next) {
330 if (strchr(fs->device, '='))
332 if (!strcmp(filesys, fs->device) ||
333 !strcmp(filesys, fs->mountpt))
336 if (fs && strchr(fs->device, '='))
337 fs->device = interpret_device(fs->device);
339 if (fs || !try_again)
342 for (fs = filesys_info; fs; fs = fs->next) {
343 fs->device = interpret_device(fs->device);
344 if (!strcmp(filesys, fs->device) ||
345 !strcmp(filesys, fs->mountpt))
352 /* Find fsck program for a given fs type. */
353 static char *find_fsck(char *type)
357 static char prog[256];
358 char *p = string_copy(fsck_path);
361 /* Are we looking for a program or just a type? */
362 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
364 for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
365 sprintf(prog, tpl, s, type);
366 if (stat(prog, &st) == 0) break;
369 return(s ? prog : NULL);
372 static int progress_active(NOARGS)
374 struct fsck_instance *inst;
376 for (inst = instance_list; inst; inst = inst->next) {
377 if (inst->flags & FLAG_DONE)
379 if (inst->flags & FLAG_PROGRESS)
386 * Execute a particular fsck program, and link it into the list of
387 * child processes we are waiting for.
389 static int execute(const char *type, char *device, char *mntpt,
392 char *s, *argv[80], prog[80];
394 struct fsck_instance *inst, *p;
397 inst = malloc(sizeof(struct fsck_instance));
400 memset(inst, 0, sizeof(struct fsck_instance));
402 sprintf(prog, "fsck.%s", type);
403 argv[0] = string_copy(prog);
406 for (i=0; i <num_args; i++)
407 argv[argc++] = string_copy(args[i]);
409 if (progress & !progress_active()) {
410 if ((strcmp(type, "ext2") == 0) ||
411 (strcmp(type, "ext3") == 0)) {
412 argv[argc++] = string_copy("-C0");
413 inst->flags |= FLAG_PROGRESS;
417 argv[argc++] = string_copy(device);
422 fprintf(stderr, _("fsck: %s: not found\n"), prog);
426 if (verbose || noexecute) {
427 printf("[%s (%d) -- %s] ", s, num_running,
428 mntpt ? mntpt : device);
429 for (i=0; i < argc; i++)
430 printf("%s ", argv[i]);
434 /* Fork and execute the correct program. */
437 else if ((pid = fork()) < 0) {
440 } else if (pid == 0) {
443 (void) execv(s, argv);
448 for (i=0; i < argc; i++)
452 inst->prog = string_copy(prog);
453 inst->type = string_copy(type);
454 inst->device = string_copy(device);
455 inst->base_device = base_device(device);
456 inst->start_time = time(0);
460 * Find the end of the list, so we add the instance on at the end.
462 for (p = instance_list; p && p->next; p = p->next);
467 instance_list = inst;
473 * Send a signal to all outstanding fsck child processes
475 static int kill_all(int signum)
477 struct fsck_instance *inst;
480 for (inst = instance_list; inst; inst = inst->next) {
481 if (inst->flags & FLAG_DONE)
483 kill(inst->pid, signum);
490 * Wait for one child process to exit; when it does, unlink it from
491 * the list of executing child processes, and return it.
493 static struct fsck_instance *wait_one(int flags)
497 struct fsck_instance *inst, *inst2, *prev;
504 inst = instance_list;
507 while (inst->next && (random() & 1)) {
512 inst->exit_status = 0;
517 * gcc -Wall fails saving throw against stupidity
518 * (inst and prev are thought to be uninitialized variables)
523 pid = waitpid(-1, &status, flags);
524 if (cancel_requested && !kill_sent) {
528 if ((pid == 0) && (flags & WNOHANG))
531 if ((errno == EINTR) || (errno == EAGAIN))
533 if (errno == ECHILD) {
535 _("%s: wait: No more child process?!?\n"),
542 for (prev = 0, inst = instance_list;
544 prev = inst, inst = inst->next) {
545 if (inst->pid == pid)
550 if (WIFEXITED(status))
551 status = WEXITSTATUS(status);
552 else if (WIFSIGNALED(status)) {
553 sig = WTERMSIG(status);
555 status = EXIT_UNCORRECTED;
557 printf(_("Warning... %s for device %s exited "
558 "with signal %d.\n"),
559 inst->prog, inst->device, sig);
563 printf(_("%s %s: status is %x, should never happen.\n"),
564 inst->prog, inst->device, status);
567 inst->exit_status = status;
568 if (progress && (inst->flags & FLAG_PROGRESS) &&
569 !progress_active()) {
570 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
571 if (inst2->flags & FLAG_DONE)
573 if (strcmp(inst2->type, "ext2") &&
574 strcmp(inst2->type, "ext3"))
577 * If we've just started the fsck, wait a tiny
578 * bit before sending the kill, to give it
579 * time to set up the signal handler
581 if (inst2->start_time < time(0)+2) {
584 kill(inst2->pid, SIGUSR1);
588 kill(inst2->pid, SIGUSR1);
589 inst2->flags |= FLAG_PROGRESS;
595 prev->next = inst->next;
597 instance_list = inst->next;
599 printf(_("Finished with %s (exit status %d)\n"),
600 inst->device, inst->exit_status);
606 * Wait until all executing child processes have exited; return the
607 * logical OR of all of their exit code values.
609 static int wait_all(int flags)
611 struct fsck_instance *inst;
612 int global_status = 0;
614 while ((inst = wait_one(flags))) {
615 global_status |= inst->exit_status;
618 if (noexecute && (flags & WNOHANG) && !(random() % 3))
622 return global_status;
626 * Run the fsck program on a particular device
628 * If the type is specified using -t, and it isn't prefixed with "no"
629 * (as in "noext2") and only one filesystem type is specified, then
630 * use that type regardless of what is specified in /etc/fstab.
632 * If the type isn't specified by the user, then use either the type
633 * specified in /etc/fstab, or DEFAULT_FSTYPE.
635 static void fsck_device(char *device, int interactive)
637 const char *type = 0;
638 struct fs_info *fsent;
641 if (fstype && strncmp(fstype, "no", 2) &&
642 strncmp(fstype, "opts=", 5) && strncmp(fstype, "loop", 4) &&
643 !strchr(fstype, ','))
646 if ((fsent = lookup(device))) {
647 device = fsent->device;
648 interpret_type(fsent);
653 type = DEFAULT_FSTYPE;
656 retval = execute(type, device, fsent ? fsent->mountpt : 0,
659 fprintf(stderr, _("%s: Error %d while executing fsck.%s "
660 "for %s\n"), progname, retval, type, device);
667 * Deal with the fsck -t argument.
669 struct fs_type_compile {
675 #define FS_TYPE_NORMAL 0
676 #define FS_TYPE_OPT 1
677 #define FS_TYPE_NEGOPT 2
679 static const char *fs_type_syntax_error =
680 N_("Either all or none of the filesystem types passed to -t must be prefixed\n"
681 "with 'no' or '!'.\n");
683 static void compile_fs_type(char *fs_type, struct fs_type_compile *cmp)
687 int negate, first_negate = 1;
690 for (cp=fs_type; *cp; cp++) {
696 cmp->list = malloc(num * sizeof(char *));
697 cmp->type = malloc(num * sizeof(int));
698 if (!cmp->list || !cmp->type) {
699 fprintf(stderr, _("Couldn't allocate memory for "
700 "filesystem types\n"));
703 memset(cmp->list, 0, num * sizeof(char *));
704 memset(cmp->type, 0, num * sizeof(int));
710 list = string_copy(fs_type);
712 s = strtok(list, ",");
715 if (strncmp(s, "no", 2) == 0) {
718 } else if (*s == '!') {
722 if (strcmp(s, "loop") == 0)
723 /* loop is really short-hand for opts=loop */
724 goto loop_special_case;
725 else if (strncmp(s, "opts=", 5) == 0) {
728 cmp->type[num] = negate ? FS_TYPE_NEGOPT : FS_TYPE_OPT;
731 cmp->negate = negate;
734 if ((negate && !cmp->negate) ||
735 (!negate && cmp->negate)) {
736 fprintf(stderr, _(fs_type_syntax_error));
741 printf("Adding %s to list (type %d).\n", s, cmp->type[num]);
743 cmp->list[num++] = string_copy(s);
744 s = strtok(NULL, ",");
750 * This function returns true if a particular option appears in a
751 * comma-delimited options list
753 static int opt_in_list(char *opt, char *optlist)
759 list = string_copy(optlist);
761 s = strtok(list, ",");
763 if (strcmp(s, opt) == 0) {
767 s = strtok(NULL, ",");
773 /* See if the filesystem matches the criteria given by the -t option */
774 static int fs_match(struct fs_info *fs, struct fs_type_compile *cmp)
776 int n, ret = 0, checked_type = 0;
779 if (cmp->list == 0 || cmp->list[0] == 0)
782 for (n=0; (cp = cmp->list[n]); n++) {
783 switch (cmp->type[n]) {
786 if (strcmp(cp, fs->type) == 0) {
791 if (opt_in_list(cp, fs->opts))
795 if (!opt_in_list(cp, fs->opts))
800 if (checked_type == 0)
802 return (cmp->negate ? !ret : ret);
805 /* Check if we should ignore this filesystem. */
806 static int ignore(struct fs_info *fs)
812 * If the pass number is 0, ignore it.
820 * If a specific fstype is specified, and it doesn't match,
823 if (!fs_match(fs, &fs_type_compiled)) return 1;
825 /* Are we ignoring this type? */
826 for(ip = ignored_types; *ip; ip++)
827 if (strcmp(fs->type, *ip) == 0) return 1;
829 /* Do we really really want to check this fs? */
830 for(ip = really_wanted; *ip; ip++)
831 if (strcmp(fs->type, *ip) == 0) {
836 /* See if the <fsck.fs> program is available. */
837 if (find_fsck(fs->type) == NULL) {
839 fprintf(stderr, _("fsck: cannot check %s: fsck.%s not found\n"),
840 fs->device, fs->type);
844 /* We can and want to check this file system type. */
849 * Returns TRUE if a partition on the same disk is already being
852 static int device_already_active(char *device)
854 struct fsck_instance *inst;
857 if (force_all_parallel)
861 /* Don't check a soft raid disk with any other disk */
863 (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1) ||
864 !strncmp(device, BASE_MD, sizeof(BASE_MD)-1)))
868 base = base_device(device);
870 * If we don't know the base device, assume that the device is
871 * already active if there are any fsck instances running.
874 return (instance_list != 0);
875 for (inst = instance_list; inst; inst = inst->next) {
876 if (!inst->base_device || !strcmp(base, inst->base_device)) {
885 /* Check all file systems, using the /etc/fstab table. */
886 static int check_all(NOARGS)
888 struct fs_info *fs = NULL;
889 int status = EXIT_OK;
890 int not_done_yet = 1;
895 printf(_("Checking all file systems.\n"));
898 * Do an initial scan over the filesystem; mark filesystems
899 * which should be ignored as done, and resolve LABEL= and
900 * UUID= specifications to the real device.
902 for (fs = filesys_info; fs; fs = fs->next) {
904 fs->flags |= FLAG_DONE;
906 fs->device = interpret_device(fs->device);
910 * Find and check the root filesystem.
912 if (!parallel_root) {
913 for (fs = filesys_info; fs; fs = fs->next) {
914 if (!strcmp(fs->mountpt, "/"))
918 if (!skip_root && !ignore(fs)) {
919 fsck_device(fs->device, 1);
920 status |= wait_all(0);
921 if (status > EXIT_NONDESTRUCT)
924 fs->flags |= FLAG_DONE;
928 * This is for the bone-headed user who enters the root
929 * filesystem twice. Skip root will skep all root entries.
932 for (fs = filesys_info; fs; fs = fs->next)
933 if (!strcmp(fs->mountpt, "/"))
934 fs->flags |= FLAG_DONE;
936 while (not_done_yet) {
940 for (fs = filesys_info; fs; fs = fs->next) {
941 if (cancel_requested)
943 if (fs->flags & FLAG_DONE)
946 * If the filesystem's pass number is higher
947 * than the current pass number, then we don't
950 if (fs->passno > passno) {
955 * If a filesystem on a particular device has
956 * already been spawned, then we need to defer
957 * this to another pass.
959 if (device_already_active(fs->device)) {
964 * Spawn off the fsck process
966 fsck_device(fs->device, serialize);
967 fs->flags |= FLAG_DONE;
970 * Only do one filesystem at a time, or if we
971 * have a limit on the number of fsck's extant
972 * at one time, apply that limit.
975 (max_running && (num_running >= max_running))) {
980 if (cancel_requested)
983 printf(_("--waiting-- (pass %d)\n"), passno);
984 status |= wait_all(pass_done ? 0 : WNOHANG);
987 printf("----------------------------------\n");
992 if (cancel_requested && !kill_sent) {
996 status |= wait_all(0);
1000 static void usage(NOARGS)
1003 _("Usage: fsck [-ACNPRTV] [-t fstype] [fs-options] [filesys ...]\n"));
1007 #ifdef HAVE_SIGNAL_H
1008 static void signal_cancel(int sig)
1014 static void PRS(int argc, char *argv[])
1020 int opts_for_fsck = 0;
1021 #ifdef HAVE_SIGNAL_H
1022 struct sigaction sa;
1025 * Set up signal action
1027 memset(&sa, 0, sizeof(struct sigaction));
1028 sa.sa_handler = signal_cancel;
1029 sigaction(SIGINT, &sa, 0);
1030 sigaction(SIGTERM, &sa, 0);
1039 for (i=1; i < argc; i++) {
1043 if ((arg[0] == '/' && !opts_for_fsck) ||
1044 (strncmp(arg, "LABEL=", 6) == 0) ||
1045 (strncmp(arg, "UUID=", 5) == 0)) {
1046 if (num_devices >= MAX_DEVICES) {
1047 fprintf(stderr, _("%s: too many devices\n"),
1051 devices[num_devices++] =
1052 interpret_device(string_copy(arg));
1055 if (arg[0] != '-' || opts_for_fsck) {
1056 if (num_args >= MAX_ARGS) {
1057 fprintf(stderr, _("%s: too many arguments\n"),
1061 args[num_args++] = string_copy(arg);
1064 for (j=1; arg[j]; j++) {
1065 if (opts_for_fsck) {
1066 options[++opt] = arg[j];
1102 else if ((i+1) < argc)
1106 fstype = string_copy(tmp);
1107 compile_fs_type(fstype, &fs_type_compiled);
1116 options[++opt] = arg[j];
1123 options[++opt] = '\0';
1124 if (num_args >= MAX_ARGS) {
1126 _("%s: too many arguments\n"),
1130 args[num_args++] = string_copy(options);
1134 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1135 force_all_parallel++;
1136 if ((tmp = getenv("FSCK_MAX_INST")))
1137 max_running = atoi(tmp);
1140 int main(int argc, char *argv[])
1144 int interactive = 0;
1145 char *oldpath = getenv("PATH");
1149 setlocale(LC_MESSAGES, "");
1150 setlocale(LC_CTYPE, "");
1151 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1152 textdomain(NLS_CAT_NAME);
1157 printf("fsck %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
1159 fstab = getenv("FSTAB_FILE");
1161 fstab = _PATH_MNTTAB;
1162 load_fs_info(fstab);
1164 /* Update our search path to include uncommon directories. */
1166 fsck_path = malloc (strlen (fsck_prefix_path) + 1 +
1167 strlen (oldpath) + 1);
1168 strcpy (fsck_path, fsck_prefix_path);
1169 strcat (fsck_path, ":");
1170 strcat (fsck_path, oldpath);
1172 fsck_path = string_copy(fsck_prefix_path);
1175 if ((num_devices == 1) || (serialize))
1178 /* If -A was specified ("check all"), do that! */
1182 if (num_devices == 0) {
1187 for (i = 0 ; i < num_devices; i++) {
1188 if (cancel_requested) {
1195 fsck_device(devices[i], interactive);
1196 if (serialize || (num_running >= max_running)) {
1197 struct fsck_instance *inst;
1201 status |= inst->exit_status;
1202 free_instance(inst);
1206 status |= wait_all(0);