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 parse_escape(char *word)
161 for (p = word, q = word; *p; p++, q++) {
180 for (i = 0; i < 3; i++, p++) {
183 ac = (ac * 8) + (*p - '0');
191 static void free_instance(struct fsck_instance *i)
198 free(i->base_device);
203 static int parse_fstab_line(char *line, struct fs_info **ret_fs)
205 char *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
210 if ((cp = strchr(line, '#')))
211 *cp = 0; /* Ignore everything after the comment char */
214 device = parse_word(&cp);
215 mntpnt = parse_word(&cp);
216 type = parse_word(&cp);
217 opts = parse_word(&cp);
218 freq = parse_word(&cp);
219 passno = parse_word(&cp);
221 parse_escape(device);
222 parse_escape(mntpnt);
226 parse_escape(passno);
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)
256 char *dev = interpret_spec(spec);
262 * Check to see if this was because /proc/partitions isn't
265 if (access("/proc/partitions", R_OK) < 0) {
266 fprintf(stderr, "Couldn't open /proc/partitions: %s\n",
268 fprintf(stderr, "Is /proc mounted?\n");
272 * Check to see if this is because we're not running as root
276 "Must be root to scan for matching filesystems: %s\n",
279 fprintf(stderr, "Couldn't find matching filesystem: %s\n",
285 * Interpret filesystem auto type if necessary
287 static void interpret_type(struct fs_info *fs)
291 if (strcmp(fs->type, "auto") == 0 ||
292 (strchr(fs->type, ',') != 0)) {
293 if (fs && strchr(fs->device, '='))
294 fs->device = interpret_device(fs->device);
295 type = identify_fs(fs->device, fs->type);
298 fs->type = string_copy(type);
300 fprintf(stderr, _("Could not determine "
301 "filesystem type for %s\n"),
308 * Load the filesystem database from /etc/fstab
310 static void load_fs_info(const char *filename)
316 struct fs_info *fs, *fs_last = NULL;
319 if ((f = fopen(filename, "r")) == NULL) {
320 fprintf(stderr, _("WARNING: couldn't open %s: %s\n"),
321 filename, strerror(errno));
326 if (!fgets(buf, sizeof(buf), f))
328 buf[sizeof(buf)-1] = 0;
329 if (parse_fstab_line(buf, &fs) < 0) {
330 fprintf(stderr, _("WARNING: bad format "
331 "on line %d of %s\n"), lineno, filename);
350 fprintf(stderr, _("\007\007\007"
351 "WARNING: Your /etc/fstab does not contain the fsck passno\n"
352 " field. I will kludge around things for you, but you\n"
353 " should fix your /etc/fstab file as soon as you can.\n\n"));
355 for (fs = filesys_info; fs; fs = fs->next) {
361 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
362 static struct fs_info *lookup(char *filesys)
367 /* No filesys name given. */
371 for (fs = filesys_info; fs; fs = fs->next) {
372 if (strchr(fs->device, '='))
374 if (!strcmp(filesys, fs->device) ||
375 !strcmp(filesys, fs->mountpt))
378 if (fs && strchr(fs->device, '='))
379 fs->device = interpret_device(fs->device);
381 if (fs || !try_again)
384 for (fs = filesys_info; fs; fs = fs->next) {
385 fs->device = interpret_device(fs->device);
386 if (!strcmp(filesys, fs->device) ||
387 !strcmp(filesys, fs->mountpt))
394 /* Find fsck program for a given fs type. */
395 static char *find_fsck(char *type)
399 static char prog[256];
400 char *p = string_copy(fsck_path);
403 /* Are we looking for a program or just a type? */
404 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
406 for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
407 sprintf(prog, tpl, s, type);
408 if (stat(prog, &st) == 0) break;
411 return(s ? prog : NULL);
414 static int progress_active(NOARGS)
416 struct fsck_instance *inst;
418 for (inst = instance_list; inst; inst = inst->next) {
419 if (inst->flags & FLAG_DONE)
421 if (inst->flags & FLAG_PROGRESS)
428 * Execute a particular fsck program, and link it into the list of
429 * child processes we are waiting for.
431 static int execute(const char *type, char *device, char *mntpt,
434 char *s, *argv[80], prog[80];
436 struct fsck_instance *inst, *p;
439 inst = malloc(sizeof(struct fsck_instance));
442 memset(inst, 0, sizeof(struct fsck_instance));
444 sprintf(prog, "fsck.%s", type);
445 argv[0] = string_copy(prog);
448 for (i=0; i <num_args; i++)
449 argv[argc++] = string_copy(args[i]);
451 if (progress & !progress_active()) {
452 if ((strcmp(type, "ext2") == 0) ||
453 (strcmp(type, "ext3") == 0)) {
454 argv[argc++] = string_copy("-C0");
455 inst->flags |= FLAG_PROGRESS;
459 argv[argc++] = string_copy(device);
464 fprintf(stderr, _("fsck: %s: not found\n"), prog);
468 if (verbose || noexecute) {
469 printf("[%s (%d) -- %s] ", s, num_running,
470 mntpt ? mntpt : device);
471 for (i=0; i < argc; i++)
472 printf("%s ", argv[i]);
476 /* Fork and execute the correct program. */
479 else if ((pid = fork()) < 0) {
482 } else if (pid == 0) {
485 (void) execv(s, argv);
490 for (i=0; i < argc; i++)
494 inst->prog = string_copy(prog);
495 inst->type = string_copy(type);
496 inst->device = string_copy(device);
497 inst->base_device = base_device(device);
498 inst->start_time = time(0);
502 * Find the end of the list, so we add the instance on at the end.
504 for (p = instance_list; p && p->next; p = p->next);
509 instance_list = inst;
515 * Send a signal to all outstanding fsck child processes
517 static int kill_all(int signum)
519 struct fsck_instance *inst;
522 for (inst = instance_list; inst; inst = inst->next) {
523 if (inst->flags & FLAG_DONE)
525 kill(inst->pid, signum);
532 * Wait for one child process to exit; when it does, unlink it from
533 * the list of executing child processes, and return it.
535 static struct fsck_instance *wait_one(int flags)
539 struct fsck_instance *inst, *inst2, *prev;
546 inst = instance_list;
549 while (inst->next && (random() & 1)) {
554 inst->exit_status = 0;
559 * gcc -Wall fails saving throw against stupidity
560 * (inst and prev are thought to be uninitialized variables)
565 pid = waitpid(-1, &status, flags);
566 if (cancel_requested && !kill_sent) {
570 if ((pid == 0) && (flags & WNOHANG))
573 if ((errno == EINTR) || (errno == EAGAIN))
575 if (errno == ECHILD) {
577 _("%s: wait: No more child process?!?\n"),
584 for (prev = 0, inst = instance_list;
586 prev = inst, inst = inst->next) {
587 if (inst->pid == pid)
592 if (WIFEXITED(status))
593 status = WEXITSTATUS(status);
594 else if (WIFSIGNALED(status)) {
595 sig = WTERMSIG(status);
597 status = EXIT_UNCORRECTED;
599 printf(_("Warning... %s for device %s exited "
600 "with signal %d.\n"),
601 inst->prog, inst->device, sig);
605 printf(_("%s %s: status is %x, should never happen.\n"),
606 inst->prog, inst->device, status);
609 inst->exit_status = status;
610 if (progress && (inst->flags & FLAG_PROGRESS) &&
611 !progress_active()) {
612 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
613 if (inst2->flags & FLAG_DONE)
615 if (strcmp(inst2->type, "ext2") &&
616 strcmp(inst2->type, "ext3"))
619 * If we've just started the fsck, wait a tiny
620 * bit before sending the kill, to give it
621 * time to set up the signal handler
623 if (inst2->start_time < time(0)+2) {
626 kill(inst2->pid, SIGUSR1);
630 kill(inst2->pid, SIGUSR1);
631 inst2->flags |= FLAG_PROGRESS;
637 prev->next = inst->next;
639 instance_list = inst->next;
641 printf(_("Finished with %s (exit status %d)\n"),
642 inst->device, inst->exit_status);
648 * Wait until all executing child processes have exited; return the
649 * logical OR of all of their exit code values.
651 static int wait_all(int flags)
653 struct fsck_instance *inst;
654 int global_status = 0;
656 while ((inst = wait_one(flags))) {
657 global_status |= inst->exit_status;
660 if (noexecute && (flags & WNOHANG) && !(random() % 3))
664 return global_status;
668 * Run the fsck program on a particular device
670 * If the type is specified using -t, and it isn't prefixed with "no"
671 * (as in "noext2") and only one filesystem type is specified, then
672 * use that type regardless of what is specified in /etc/fstab.
674 * If the type isn't specified by the user, then use either the type
675 * specified in /etc/fstab, or DEFAULT_FSTYPE.
677 static void fsck_device(char *device, int interactive)
679 const char *type = 0;
680 struct fs_info *fsent;
683 if (fstype && strncmp(fstype, "no", 2) &&
684 strncmp(fstype, "opts=", 5) && strncmp(fstype, "loop", 4) &&
685 !strchr(fstype, ','))
688 if ((fsent = lookup(device))) {
689 device = fsent->device;
690 interpret_type(fsent);
695 type = DEFAULT_FSTYPE;
698 retval = execute(type, device, fsent ? fsent->mountpt : 0,
701 fprintf(stderr, _("%s: Error %d while executing fsck.%s "
702 "for %s\n"), progname, retval, type, device);
709 * Deal with the fsck -t argument.
711 struct fs_type_compile {
717 #define FS_TYPE_NORMAL 0
718 #define FS_TYPE_OPT 1
719 #define FS_TYPE_NEGOPT 2
721 static const char *fs_type_syntax_error =
722 N_("Either all or none of the filesystem types passed to -t must be prefixed\n"
723 "with 'no' or '!'.\n");
725 static void compile_fs_type(char *fs_type, struct fs_type_compile *cmp)
729 int negate, first_negate = 1;
732 for (cp=fs_type; *cp; cp++) {
738 cmp->list = malloc(num * sizeof(char *));
739 cmp->type = malloc(num * sizeof(int));
740 if (!cmp->list || !cmp->type) {
741 fprintf(stderr, _("Couldn't allocate memory for "
742 "filesystem types\n"));
745 memset(cmp->list, 0, num * sizeof(char *));
746 memset(cmp->type, 0, num * sizeof(int));
752 list = string_copy(fs_type);
754 s = strtok(list, ",");
757 if (strncmp(s, "no", 2) == 0) {
760 } else if (*s == '!') {
764 if (strcmp(s, "loop") == 0)
765 /* loop is really short-hand for opts=loop */
766 goto loop_special_case;
767 else if (strncmp(s, "opts=", 5) == 0) {
770 cmp->type[num] = negate ? FS_TYPE_NEGOPT : FS_TYPE_OPT;
773 cmp->negate = negate;
776 if ((negate && !cmp->negate) ||
777 (!negate && cmp->negate)) {
778 fprintf(stderr, _(fs_type_syntax_error));
783 printf("Adding %s to list (type %d).\n", s, cmp->type[num]);
785 cmp->list[num++] = string_copy(s);
786 s = strtok(NULL, ",");
792 * This function returns true if a particular option appears in a
793 * comma-delimited options list
795 static int opt_in_list(char *opt, char *optlist)
801 list = string_copy(optlist);
803 s = strtok(list, ",");
805 if (strcmp(s, opt) == 0) {
809 s = strtok(NULL, ",");
815 /* See if the filesystem matches the criteria given by the -t option */
816 static int fs_match(struct fs_info *fs, struct fs_type_compile *cmp)
818 int n, ret = 0, checked_type = 0;
821 if (cmp->list == 0 || cmp->list[0] == 0)
824 for (n=0; (cp = cmp->list[n]); n++) {
825 switch (cmp->type[n]) {
828 if (strcmp(cp, fs->type) == 0) {
833 if (opt_in_list(cp, fs->opts))
837 if (!opt_in_list(cp, fs->opts))
842 if (checked_type == 0)
844 return (cmp->negate ? !ret : ret);
847 /* Check if we should ignore this filesystem. */
848 static int ignore(struct fs_info *fs)
854 * If the pass number is 0, ignore it.
862 * If a specific fstype is specified, and it doesn't match,
865 if (!fs_match(fs, &fs_type_compiled)) return 1;
867 /* Are we ignoring this type? */
868 for(ip = ignored_types; *ip; ip++)
869 if (strcmp(fs->type, *ip) == 0) return 1;
871 /* Do we really really want to check this fs? */
872 for(ip = really_wanted; *ip; ip++)
873 if (strcmp(fs->type, *ip) == 0) {
878 /* See if the <fsck.fs> program is available. */
879 if (find_fsck(fs->type) == NULL) {
881 fprintf(stderr, _("fsck: cannot check %s: fsck.%s not found\n"),
882 fs->device, fs->type);
886 /* We can and want to check this file system type. */
891 * Returns TRUE if a partition on the same disk is already being
894 static int device_already_active(char *device)
896 struct fsck_instance *inst;
899 if (force_all_parallel)
903 /* Don't check a soft raid disk with any other disk */
905 (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1) ||
906 !strncmp(device, BASE_MD, sizeof(BASE_MD)-1)))
910 base = base_device(device);
912 * If we don't know the base device, assume that the device is
913 * already active if there are any fsck instances running.
916 return (instance_list != 0);
917 for (inst = instance_list; inst; inst = inst->next) {
918 if (!inst->base_device || !strcmp(base, inst->base_device)) {
927 /* Check all file systems, using the /etc/fstab table. */
928 static int check_all(NOARGS)
930 struct fs_info *fs = NULL;
931 int status = EXIT_OK;
932 int not_done_yet = 1;
937 printf(_("Checking all file systems.\n"));
940 * Do an initial scan over the filesystem; mark filesystems
941 * which should be ignored as done, and resolve LABEL= and
942 * UUID= specifications to the real device.
944 for (fs = filesys_info; fs; fs = fs->next) {
946 fs->flags |= FLAG_DONE;
948 fs->device = interpret_device(fs->device);
952 * Find and check the root filesystem.
954 if (!parallel_root) {
955 for (fs = filesys_info; fs; fs = fs->next) {
956 if (!strcmp(fs->mountpt, "/"))
960 if (!skip_root && !ignore(fs)) {
961 fsck_device(fs->device, 1);
962 status |= wait_all(0);
963 if (status > EXIT_NONDESTRUCT)
966 fs->flags |= FLAG_DONE;
970 * This is for the bone-headed user who enters the root
971 * filesystem twice. Skip root will skep all root entries.
974 for (fs = filesys_info; fs; fs = fs->next)
975 if (!strcmp(fs->mountpt, "/"))
976 fs->flags |= FLAG_DONE;
978 while (not_done_yet) {
982 for (fs = filesys_info; fs; fs = fs->next) {
983 if (cancel_requested)
985 if (fs->flags & FLAG_DONE)
988 * If the filesystem's pass number is higher
989 * than the current pass number, then we don't
992 if (fs->passno > passno) {
997 * If a filesystem on a particular device has
998 * already been spawned, then we need to defer
999 * this to another pass.
1001 if (device_already_active(fs->device)) {
1006 * Spawn off the fsck process
1008 fsck_device(fs->device, serialize);
1009 fs->flags |= FLAG_DONE;
1012 * Only do one filesystem at a time, or if we
1013 * have a limit on the number of fsck's extant
1014 * at one time, apply that limit.
1017 (max_running && (num_running >= max_running))) {
1022 if (cancel_requested)
1025 printf(_("--waiting-- (pass %d)\n"), passno);
1026 status |= wait_all(pass_done ? 0 : WNOHANG);
1029 printf("----------------------------------\n");
1034 if (cancel_requested && !kill_sent) {
1038 status |= wait_all(0);
1042 static void usage(NOARGS)
1045 _("Usage: fsck [-ACNPRTV] [-t fstype] [fs-options] [filesys ...]\n"));
1049 #ifdef HAVE_SIGNAL_H
1050 static void signal_cancel(int sig)
1056 static void PRS(int argc, char *argv[])
1062 int opts_for_fsck = 0;
1063 #ifdef HAVE_SIGNAL_H
1064 struct sigaction sa;
1067 * Set up signal action
1069 memset(&sa, 0, sizeof(struct sigaction));
1070 sa.sa_handler = signal_cancel;
1071 sigaction(SIGINT, &sa, 0);
1072 sigaction(SIGTERM, &sa, 0);
1081 for (i=1; i < argc; i++) {
1085 if ((arg[0] == '/' && !opts_for_fsck) ||
1086 (strncmp(arg, "LABEL=", 6) == 0) ||
1087 (strncmp(arg, "UUID=", 5) == 0)) {
1088 if (num_devices >= MAX_DEVICES) {
1089 fprintf(stderr, _("%s: too many devices\n"),
1093 devices[num_devices++] =
1094 interpret_device(string_copy(arg));
1097 if (arg[0] != '-' || opts_for_fsck) {
1098 if (num_args >= MAX_ARGS) {
1099 fprintf(stderr, _("%s: too many arguments\n"),
1103 args[num_args++] = string_copy(arg);
1106 for (j=1; arg[j]; j++) {
1107 if (opts_for_fsck) {
1108 options[++opt] = arg[j];
1144 else if ((i+1) < argc)
1148 fstype = string_copy(tmp);
1149 compile_fs_type(fstype, &fs_type_compiled);
1158 options[++opt] = arg[j];
1165 options[++opt] = '\0';
1166 if (num_args >= MAX_ARGS) {
1168 _("%s: too many arguments\n"),
1172 args[num_args++] = string_copy(options);
1176 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1177 force_all_parallel++;
1178 if ((tmp = getenv("FSCK_MAX_INST")))
1179 max_running = atoi(tmp);
1182 int main(int argc, char *argv[])
1186 int interactive = 0;
1187 char *oldpath = getenv("PATH");
1191 setlocale(LC_MESSAGES, "");
1192 setlocale(LC_CTYPE, "");
1193 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1194 textdomain(NLS_CAT_NAME);
1199 printf("fsck %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
1201 fstab = getenv("FSTAB_FILE");
1203 fstab = _PATH_MNTTAB;
1204 load_fs_info(fstab);
1206 /* Update our search path to include uncommon directories. */
1208 fsck_path = malloc (strlen (fsck_prefix_path) + 1 +
1209 strlen (oldpath) + 1);
1210 strcpy (fsck_path, fsck_prefix_path);
1211 strcat (fsck_path, ":");
1212 strcat (fsck_path, oldpath);
1214 fsck_path = string_copy(fsck_prefix_path);
1217 if ((num_devices == 1) || (serialize))
1220 /* If -A was specified ("check all"), do that! */
1224 if (num_devices == 0) {
1229 for (i = 0 ; i < num_devices; i++) {
1230 if (cancel_requested) {
1237 fsck_device(devices[i], interactive);
1239 (max_running && (num_running >= max_running))) {
1240 struct fsck_instance *inst;
1244 status |= inst->exit_status;
1245 free_instance(inst);
1248 printf("----------------------------------\n");
1251 status |= wait_all(0);