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 * Miquel van Smoorenburg (miquels@drinkel.ow.org) 20-Oct-1994:
12 * o Changed -t fstype to behave like with mount when -A (all file
13 * systems) or -M (like mount) is specified.
14 * o fsck looks if it can find the fsck.type program to decide
15 * if it should ignore the fs type. This way more fsck programs
16 * can be added without changing this front-end.
17 * o -R flag skip root file system.
19 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
20 * 2001, 2002, 2003, 2004, 2005 by Theodore Ts'o.
23 * This file may be redistributed under the terms of the GNU Public
28 #include <sys/types.h>
30 #include <sys/signal.h>
46 #include "blkid/blkid.h"
51 #define _PATH_MNTTAB "/etc/fstab"
54 static const char * const ignored_types[] = {
66 static const char * const really_wanted[] = {
77 #define BASE_MD "/dev/md"
80 * Global variables for options
82 static char *devices[MAX_DEVICES];
83 static char *args[MAX_ARGS];
84 static int num_devices, num_args;
91 static int like_mount;
93 static int parallel_root;
95 static int progress_fd;
96 static int force_all_parallel;
97 static int num_running;
98 static int max_running;
99 static volatile int cancel_requested;
100 static int kill_sent;
102 static struct fs_info *filesys_info, *filesys_last;
103 static struct fsck_instance *instance_list;
104 static const char *fsck_prefix_path = "/sbin:/sbin/fs.d:/sbin/fs:/etc/fs:/etc";
105 static char *fsck_path;
106 static blkid_cache cache;
108 static char *string_copy(const char *s)
118 static int string_to_int(const char *s)
123 l = strtol(s, &p, 0);
124 if (*p || l == LONG_MIN || l == LONG_MAX || l < 0 || l > INT_MAX)
130 static char *skip_over_blank(char *cp)
132 while (*cp && isspace(*cp))
137 static char *skip_over_word(char *cp)
139 while (*cp && !isspace(*cp))
144 static void strip_line(char *line)
149 p = line + strlen(line) - 1;
150 if ((*p == '\n') || (*p == '\r'))
157 static char *parse_word(char **buf)
165 word = skip_over_blank(word);
166 next = skip_over_word(word);
173 static void parse_escape(char *word)
181 for (p = q = word; *p; q++) {
186 *q = bb_process_escape_sequence(&p);
192 static void free_instance(struct fsck_instance *i)
199 free(i->base_device);
204 static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
205 const char *type, const char *opts,
206 int freq, int passno)
210 if (!(fs = malloc(sizeof(struct fs_info))))
213 fs->device = string_copy(device);
214 fs->mountpt = string_copy(mntpnt);
215 fs->type = string_copy(type);
216 fs->opts = string_copy(opts ? opts : "");
225 filesys_last->next = fs;
233 static int parse_fstab_line(char *line, struct fs_info **ret_fs)
235 char *dev, *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
240 if ((cp = strchr(line, '#')))
241 *cp = 0; /* Ignore everything after the comment char */
244 device = parse_word(&cp);
245 mntpnt = parse_word(&cp);
246 type = parse_word(&cp);
247 opts = parse_word(&cp);
248 freq = parse_word(&cp);
249 passno = parse_word(&cp);
252 return 0; /* Allow blank lines */
254 if (!mntpnt || !type)
257 parse_escape(device);
258 parse_escape(mntpnt);
262 parse_escape(passno);
264 dev = blkid_get_devname(cache, device, NULL);
268 if (strchr(type, ','))
271 fs = create_fs_device(device, mntpnt, type ? type : "auto", opts,
272 freq ? atoi(freq) : -1,
273 passno ? atoi(passno) : -1);
283 static void interpret_type(struct fs_info *fs)
287 if (strcmp(fs->type, "auto") != 0)
289 t = blkid_get_tag_value(cache, "TYPE", fs->device);
297 * Load the filesystem database from /etc/fstab
299 static void load_fs_info(const char *filename)
307 if ((f = fopen(filename, "r")) == NULL) {
308 bb_perror_msg("WARNING: couldn't open %s: %m", filename);
313 if (!fgets(buf, sizeof(buf), f))
315 buf[sizeof(buf)-1] = 0;
316 if (parse_fstab_line(buf, &fs) < 0) {
317 bb_error_msg("WARNING: bad format "
318 "on line %d of %s\n", lineno, filename);
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", stderr);
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)
348 /* No filesys name given. */
352 for (fs = filesys_info; fs; fs = fs->next) {
353 if (!strcmp(filesys, fs->device) ||
354 (fs->mountpt && !strcmp(filesys, fs->mountpt)))
361 /* Find fsck program for a given fs type. */
362 static char *find_fsck(char *type)
366 char *p = string_copy(fsck_path);
369 /* Are we looking for a program or just a type? */
370 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
372 for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
373 s = bb_xasprintf(tpl, s, type);
374 if (stat(s, &st) == 0) break;
381 static int progress_active(void)
383 struct fsck_instance *inst;
385 for (inst = instance_list; inst; inst = inst->next) {
386 if (inst->flags & FLAG_DONE)
388 if (inst->flags & FLAG_PROGRESS)
395 * Execute a particular fsck program, and link it into the list of
396 * child processes we are waiting for.
398 static int execute(const char *type, const char *device, const char *mntpt,
404 struct fsck_instance *inst, *p;
407 inst = malloc(sizeof(struct fsck_instance));
410 memset(inst, 0, sizeof(struct fsck_instance));
412 prog = bb_xasprintf("fsck.%s", type);
416 for (i=0; i <num_args; i++)
417 argv[argc++] = string_copy(args[i]);
419 if (progress && !progress_active()) {
420 if ((strcmp(type, "ext2") == 0) ||
421 (strcmp(type, "ext3") == 0)) {
423 snprintf(tmp, 80, "-C%d", progress_fd);
424 argv[argc++] = string_copy(tmp);
425 inst->flags |= FLAG_PROGRESS;
429 argv[argc++] = string_copy(device);
434 bb_error_msg("%s: not found", prog);
438 if (verbose || noexecute) {
439 printf("[%s (%d) -- %s] ", s, num_running,
440 mntpt ? mntpt : device);
441 for (i=0; i < argc; i++)
442 printf("%s ", argv[i]);
446 /* Fork and execute the correct program. */
449 else if ((pid = fork()) < 0) {
452 } else if (pid == 0) {
455 (void) execv(s, argv);
456 bb_perror_msg_and_die("%s", argv[0]);
459 for (i = 1; i < argc; i++)
465 inst->type = string_copy(type);
466 inst->device = string_copy(device);
467 inst->base_device = base_device(device);
468 inst->start_time = time(0);
472 * Find the end of the list, so we add the instance on at the end.
474 for (p = instance_list; p && p->next; p = p->next);
479 instance_list = inst;
485 * Send a signal to all outstanding fsck child processes
487 static int kill_all(int signum)
489 struct fsck_instance *inst;
492 for (inst = instance_list; inst; inst = inst->next) {
493 if (inst->flags & FLAG_DONE)
495 kill(inst->pid, signum);
502 * Wait for one child process to exit; when it does, unlink it from
503 * the list of executing child processes, and return it.
505 static struct fsck_instance *wait_one(int flags)
509 struct fsck_instance *inst, *inst2, *prev;
516 inst = instance_list;
519 while (inst->next && (random() & 1)) {
524 inst->exit_status = 0;
529 * gcc -Wall fails saving throw against stupidity
530 * (inst and prev are thought to be uninitialized variables)
535 pid = waitpid(-1, &status, flags);
536 if (cancel_requested && !kill_sent) {
540 if ((pid == 0) && (flags & WNOHANG))
543 if ((errno == EINTR) || (errno == EAGAIN))
545 if (errno == ECHILD) {
546 bb_error_msg("wait: No more child process?!?");
552 for (prev = 0, inst = instance_list;
554 prev = inst, inst = inst->next) {
555 if (inst->pid == pid)
560 if (WIFEXITED(status))
561 status = WEXITSTATUS(status);
562 else if (WIFSIGNALED(status)) {
563 sig = WTERMSIG(status);
565 status = EXIT_UNCORRECTED;
567 printf("Warning... %s for device %s exited "
569 inst->prog, inst->device, sig);
573 printf("%s %s: status is %x, should never happen.\n",
574 inst->prog, inst->device, status);
577 inst->exit_status = status;
578 if (progress && (inst->flags & FLAG_PROGRESS) &&
579 !progress_active()) {
580 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
581 if (inst2->flags & FLAG_DONE)
583 if (strcmp(inst2->type, "ext2") &&
584 strcmp(inst2->type, "ext3"))
587 * If we've just started the fsck, wait a tiny
588 * bit before sending the kill, to give it
589 * time to set up the signal handler
591 if (inst2->start_time < time(0)+2) {
594 kill(inst2->pid, SIGUSR1);
598 kill(inst2->pid, SIGUSR1);
599 inst2->flags |= FLAG_PROGRESS;
605 prev->next = inst->next;
607 instance_list = inst->next;
609 printf("Finished with %s (exit status %d)\n",
610 inst->device, inst->exit_status);
615 #define FLAG_WAIT_ALL 0
616 #define FLAG_WAIT_ATLEAST_ONE 1
618 * Wait until all executing child processes have exited; return the
619 * logical OR of all of their exit code values.
621 static int wait_many(int flags)
623 struct fsck_instance *inst;
624 int global_status = 0;
627 while ((inst = wait_one(wait_flags))) {
628 global_status |= inst->exit_status;
631 if (noexecute && (flags & WNOHANG) && !(random() % 3))
634 if (flags & FLAG_WAIT_ATLEAST_ONE)
635 wait_flags = WNOHANG;
637 return global_status;
641 * Run the fsck program on a particular device
643 * If the type is specified using -t, and it isn't prefixed with "no"
644 * (as in "noext2") and only one filesystem type is specified, then
645 * use that type regardless of what is specified in /etc/fstab.
647 * If the type isn't specified by the user, then use either the type
648 * specified in /etc/fstab, or DEFAULT_FSTYPE.
650 static void fsck_device(struct fs_info *fs, int interactive)
657 if (strcmp(fs->type, "auto") != 0)
659 else if (fstype && strncmp(fstype, "no", 2) &&
660 strncmp(fstype, "opts=", 5) && strncmp(fstype, "loop", 4) &&
661 !strchr(fstype, ','))
664 type = DEFAULT_FSTYPE;
667 retval = execute(type, fs->device, fs->mountpt, interactive);
669 bb_error_msg("Error %d while executing fsck.%s for %s",
670 retval, type, fs->device);
677 * Deal with the fsck -t argument.
679 struct fs_type_compile {
685 #define FS_TYPE_NORMAL 0
686 #define FS_TYPE_OPT 1
687 #define FS_TYPE_NEGOPT 2
689 static const char *fs_type_syntax_error =
690 N_("Either all or none of the filesystem types passed to -t must be prefixed\n"
691 "with 'no' or '!'.\n");
693 static void compile_fs_type(char *fs_type, struct fs_type_compile *cmp)
697 int negate, first_negate = 1;
700 for (cp=fs_type; *cp; cp++) {
706 cmp->list = xcalloc(num, sizeof(char *));
707 cmp->type = xcalloc(num, sizeof(int));
713 list = string_copy(fs_type);
715 s = strtok(list, ",");
718 if (strncmp(s, "no", 2) == 0) {
721 } else if (*s == '!') {
725 if (strcmp(s, "loop") == 0)
726 /* loop is really short-hand for opts=loop */
727 goto loop_special_case;
728 else if (strncmp(s, "opts=", 5) == 0) {
731 cmp->type[num] = negate ? FS_TYPE_NEGOPT : FS_TYPE_OPT;
734 cmp->negate = negate;
737 if ((negate && !cmp->negate) ||
738 (!negate && cmp->negate)) {
739 bb_error_msg_and_die("%s", fs_type_syntax_error);
743 printf("Adding %s to list (type %d).\n", s, cmp->type[num]);
745 cmp->list[num++] = string_copy(s);
746 s = strtok(NULL, ",");
752 * This function returns true if a particular option appears in a
753 * comma-delimited options list
755 static int opt_in_list(char *opt, char *optlist)
761 list = string_copy(optlist);
763 s = strtok(list, ",");
765 if (strcmp(s, opt) == 0) {
769 s = strtok(NULL, ",");
775 /* See if the filesystem matches the criteria given by the -t option */
776 static int fs_match(struct fs_info *fs, struct fs_type_compile *cmp)
778 int n, ret = 0, checked_type = 0;
781 if (cmp->list == 0 || cmp->list[0] == 0)
784 for (n=0; (cp = cmp->list[n]); n++) {
785 switch (cmp->type[n]) {
788 if (strcmp(cp, fs->type) == 0) {
793 if (opt_in_list(cp, fs->opts))
797 if (!opt_in_list(cp, fs->opts))
802 if (checked_type == 0)
804 return (cmp->negate ? !ret : ret);
807 /* Check if we should ignore this filesystem. */
808 static int ignore(struct fs_info *fs)
810 const char * const *ip;
815 * If the pass number is 0, ignore it.
823 * If a specific fstype is specified, and it doesn't match,
826 if (!fs_match(fs, &fs_type_compiled)) return 1;
828 /* Are we ignoring this type? */
829 for(ip = ignored_types; *ip; ip++)
830 if (strcmp(fs->type, *ip) == 0) return 1;
832 /* Do we really really want to check this fs? */
833 for(ip = really_wanted; *ip; ip++)
834 if (strcmp(fs->type, *ip) == 0) {
839 /* See if the <fsck.fs> program is available. */
840 s = find_fsck(fs->type);
843 bb_error_msg("cannot check %s: fsck.%s not found",
844 fs->device, fs->type);
849 /* We can and want to check this file system type. */
854 * Returns TRUE if a partition on the same disk is already being
857 static int device_already_active(char *device)
859 struct fsck_instance *inst;
862 if (force_all_parallel)
866 /* Don't check a soft raid disk with any other disk */
868 (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1) ||
869 !strncmp(device, BASE_MD, sizeof(BASE_MD)-1)))
873 base = base_device(device);
875 * If we don't know the base device, assume that the device is
876 * already active if there are any fsck instances running.
879 return (instance_list != 0);
880 for (inst = instance_list; inst; inst = inst->next) {
881 if (!inst->base_device || !strcmp(base, inst->base_device)) {
890 /* Check all file systems, using the /etc/fstab table. */
891 static int check_all(void)
893 struct fs_info *fs = NULL;
894 int status = EXIT_OK;
895 int not_done_yet = 1;
900 fputs("Checking all file systems.\n", stdout);
903 * Do an initial scan over the filesystem; mark filesystems
904 * which should be ignored as done, and resolve any "auto"
905 * filesystem types (done as a side-effect of calling ignore()).
907 for (fs = filesys_info; fs; fs = fs->next) {
909 fs->flags |= FLAG_DONE;
913 * Find and check the root filesystem.
915 if (!parallel_root) {
916 for (fs = filesys_info; fs; fs = fs->next) {
917 if (!strcmp(fs->mountpt, "/"))
921 if (!skip_root && !ignore(fs)) {
923 status |= wait_many(FLAG_WAIT_ALL);
924 if (status > EXIT_NONDESTRUCT)
927 fs->flags |= FLAG_DONE;
931 * This is for the bone-headed user who enters the root
932 * filesystem twice. Skip root will skep all root entries.
935 for (fs = filesys_info; fs; fs = fs->next)
936 if (!strcmp(fs->mountpt, "/"))
937 fs->flags |= FLAG_DONE;
939 while (not_done_yet) {
943 for (fs = filesys_info; fs; fs = fs->next) {
944 if (cancel_requested)
946 if (fs->flags & FLAG_DONE)
949 * If the filesystem's pass number is higher
950 * than the current pass number, then we don't
953 if (fs->passno > passno) {
958 * If a filesystem on a particular device has
959 * already been spawned, then we need to defer
960 * this to another pass.
962 if (device_already_active(fs->device)) {
967 * Spawn off the fsck process
969 fsck_device(fs, serialize);
970 fs->flags |= FLAG_DONE;
973 * Only do one filesystem at a time, or if we
974 * have a limit on the number of fsck's extant
975 * at one time, apply that limit.
978 (max_running && (num_running >= max_running))) {
983 if (cancel_requested)
986 printf("--waiting-- (pass %d)\n", passno);
987 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
988 FLAG_WAIT_ATLEAST_ONE);
991 printf("----------------------------------\n");
996 if (cancel_requested && !kill_sent) {
1000 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
1005 static void usage(void)
1007 fputs("Usage: fsck [-ANPRTV] [ -C [ fd ] ] [-t fstype] [fs-options] [filesys ...]\n", stderr);
1012 static void signal_cancel(int sig FSCK_ATTR((unused)))
1017 static void PRS(int argc, char *argv[])
1020 char *arg, *dev, *tmp = 0;
1023 int opts_for_fsck = 0;
1024 struct sigaction sa;
1027 * Set up signal action
1029 memset(&sa, 0, sizeof(struct sigaction));
1030 sa.sa_handler = signal_cancel;
1031 sigaction(SIGINT, &sa, 0);
1032 sigaction(SIGTERM, &sa, 0);
1038 for (i=1; i < argc; i++) {
1042 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
1043 if (num_devices >= MAX_DEVICES) {
1044 bb_error_msg_and_die("too many devices");
1046 dev = blkid_get_devname(cache, arg, NULL);
1047 if (!dev && strchr(arg, '=')) {
1049 * Check to see if we failed because
1050 * /proc/partitions isn't found.
1052 if (access("/proc/partitions", R_OK) < 0) {
1053 bb_error_msg_and_die("Couldn't open /proc/partitions: %m\n"
1054 "Is /proc mounted?");
1057 * Check to see if this is because
1058 * we're not running as root
1061 bb_error_msg_and_die(
1062 "Must be root to scan for matching filesystems: %s\n", arg);
1064 bb_error_msg_and_die(
1065 "Couldn't find matching filesystem: %s", arg);
1067 devices[num_devices++] = dev ? dev : string_copy(arg);
1070 if (arg[0] != '-' || opts_for_fsck) {
1071 if (num_args >= MAX_ARGS) {
1072 bb_error_msg_and_die("too many arguments");
1074 args[num_args++] = string_copy(arg);
1077 for (j=1; arg[j]; j++) {
1078 if (opts_for_fsck) {
1079 options[++opt] = arg[j];
1089 progress_fd = string_to_int(arg+j+1);
1090 if (progress_fd < 0)
1094 } else if ((i+1) < argc &&
1095 !strncmp(argv[i+1], "-", 1) == 0) {
1096 progress_fd = string_to_int(argv[i]);
1097 if (progress_fd < 0)
1132 else if ((i+1) < argc)
1136 fstype = string_copy(tmp);
1137 compile_fs_type(fstype, &fs_type_compiled);
1146 options[++opt] = arg[j];
1153 options[++opt] = '\0';
1154 if (num_args >= MAX_ARGS) {
1155 bb_error_msg("too many arguments");
1157 args[num_args++] = string_copy(options);
1161 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1162 force_all_parallel++;
1163 if ((tmp = getenv("FSCK_MAX_INST")))
1164 max_running = atoi(tmp);
1167 int fsck_main(int argc, char *argv[])
1170 int interactive = 0;
1171 char *oldpath = getenv("PATH");
1175 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
1176 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
1178 blkid_get_cache(&cache, NULL);
1182 printf("fsck %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
1184 fstab = getenv("FSTAB_FILE");
1186 fstab = _PATH_MNTTAB;
1187 load_fs_info(fstab);
1189 /* Update our search path to include uncommon directories. */
1191 fsck_path = bb_xasprintf("%s:%s", fsck_prefix_path, oldpath);
1193 fsck_path = string_copy(fsck_prefix_path);
1196 if ((num_devices == 1) || (serialize))
1199 /* If -A was specified ("check all"), do that! */
1203 if (num_devices == 0) {
1208 for (i = 0 ; i < num_devices; i++) {
1209 if (cancel_requested) {
1216 fs = lookup(devices[i]);
1218 fs = create_fs_device(devices[i], 0, "auto",
1223 fsck_device(fs, interactive);
1225 (max_running && (num_running >= max_running))) {
1226 struct fsck_instance *inst;
1230 status |= inst->exit_status;
1231 free_instance(inst);
1234 printf("----------------------------------\n");
1237 status |= wait_many(FLAG_WAIT_ALL);
1238 if (ENABLE_FEATURE_CLEAN_UP)
1240 blkid_put_cache(cache);