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>
45 #include "blkid/blkid.h"
52 #define _PATH_MNTTAB "/etc/fstab"
59 #ifndef DEFAULT_FSTYPE
60 #define DEFAULT_FSTYPE "ext2"
63 #define MAX_DEVICES 32
67 * Internal structure for mount tabel entries.
82 #define FLAG_PROGRESS 2
85 * Structure to allow exit codes to be stored
87 struct fsck_instance {
96 struct fsck_instance *next;
102 * Return the "base device" given a particular device; this is used to
103 * assure that we only fsck one partition on a particular drive at any
104 * one time. Otherwise, the disk heads will be seeking all over the
105 * place. If the base device can not be determined, return NULL.
107 * The base_device() function returns an allocated string which must
113 #ifdef CONFIG_FEATURE_DEVFS
115 * Required for the uber-silly devfs /dev/ide/host1/bus2/target3/lun3
118 static const char * const devfs_hier[] = {
119 "host", "bus", "target", "lun", 0
123 static char *base_device(const char *device)
126 #ifdef CONFIG_FEATURE_DEVFS
127 const char * const *hier;
132 cp = str = bb_xstrdup(device);
134 /* Skip over /dev/; if it's not present, give up. */
135 if (strncmp(cp, "/dev/", 5) != 0)
139 #if 0 /* this is for old stuff no one uses anymore ? */
140 /* Skip over /dev/dsk/... */
141 if (strncmp(cp, "dsk/", 4) == 0)
146 * For md devices, we treat them all as if they were all
147 * on one disk, since we don't know how to parallelize them.
149 if (cp[0] == 'm' && cp[1] == 'd') {
154 /* Handle DAC 960 devices */
155 if (strncmp(cp, "rd/", 3) == 0) {
157 if (cp[0] != 'c' || cp[2] != 'd' ||
158 !isdigit(cp[1]) || !isdigit(cp[3]))
164 /* Now let's handle /dev/hd* and /dev/sd* devices.... */
165 if ((cp[0] == 'h' || cp[0] == 's') && (cp[1] == 'd')) {
167 /* If there's a single number after /dev/hd, skip it */
170 /* What follows must be an alpha char, or give up */
177 #ifdef CONFIG_FEATURE_DEVFS
178 /* Now let's handle devfs (ugh) names */
180 if (strncmp(cp, "ide/", 4) == 0)
182 if (strncmp(cp, "scsi/", 5) == 0)
187 * Now we proceed down the expected devfs hierarchy.
188 * i.e., .../host1/bus2/target3/lun4/...
189 * If we don't find the expected token, followed by
190 * some number of digits at each level, abort.
192 for (hier = devfs_hier; *hier; hier++) {
194 if (strncmp(cp, *hier, len) != 0)
197 while (*cp != '/' && *cp != 0) {
208 /* Now handle devfs /dev/disc or /dev/disk names */
210 if (strncmp(cp, "discs/", 6) == 0)
212 else if (strncmp(cp, "disks/", 6) == 0)
216 if (strncmp(cp, disk, 4) != 0)
219 while (*cp != '/' && *cp != 0) {
235 static const char * const ignored_types[] = {
247 static const char * const really_wanted[] = {
258 #define BASE_MD "/dev/md"
261 * Global variables for options
263 static char *devices[MAX_DEVICES];
264 static char *args[MAX_ARGS];
265 static int num_devices, num_args;
269 static int noexecute;
270 static int serialize;
271 static int skip_root;
272 static int like_mount;
274 static int parallel_root;
276 static int progress_fd;
277 static int force_all_parallel;
278 static int num_running;
279 static int max_running;
280 static volatile int cancel_requested;
281 static int kill_sent;
283 static struct fs_info *filesys_info, *filesys_last;
284 static struct fsck_instance *instance_list;
285 static char *fsck_path;
286 static blkid_cache cache;
288 static char *string_copy(const char *s)
298 static int string_to_int(const char *s)
303 l = strtol(s, &p, 0);
304 if (*p || l == LONG_MIN || l == LONG_MAX || l < 0 || l > INT_MAX)
310 static char *skip_over_blank(char *cp)
312 while (*cp && isspace(*cp))
317 static char *skip_over_word(char *cp)
319 while (*cp && !isspace(*cp))
324 static void strip_line(char *line)
329 p = line + strlen(line) - 1;
330 if ((*p == '\n') || (*p == '\r'))
337 static char *parse_word(char **buf)
345 word = skip_over_blank(word);
346 next = skip_over_word(word);
353 static void parse_escape(char *word)
361 for (p = q = word; *p; q++) {
366 *q = bb_process_escape_sequence(&p);
372 static void free_instance(struct fsck_instance *i)
379 free(i->base_device);
384 static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
385 const char *type, const char *opts,
386 int freq, int passno)
390 if (!(fs = malloc(sizeof(struct fs_info))))
393 fs->device = string_copy(device);
394 fs->mountpt = string_copy(mntpnt);
395 fs->type = string_copy(type);
396 fs->opts = string_copy(opts ? opts : "");
405 filesys_last->next = fs;
413 static int parse_fstab_line(char *line, struct fs_info **ret_fs)
415 char *dev, *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
420 if ((cp = strchr(line, '#')))
421 *cp = 0; /* Ignore everything after the comment char */
424 device = parse_word(&cp);
425 mntpnt = parse_word(&cp);
426 type = parse_word(&cp);
427 opts = parse_word(&cp);
428 freq = parse_word(&cp);
429 passno = parse_word(&cp);
432 return 0; /* Allow blank lines */
434 if (!mntpnt || !type)
437 parse_escape(device);
438 parse_escape(mntpnt);
442 parse_escape(passno);
444 dev = blkid_get_devname(cache, device, NULL);
448 if (strchr(type, ','))
451 fs = create_fs_device(device, mntpnt, type ? type : "auto", opts,
452 freq ? atoi(freq) : -1,
453 passno ? atoi(passno) : -1);
463 static void interpret_type(struct fs_info *fs)
467 if (strcmp(fs->type, "auto") != 0)
469 t = blkid_get_tag_value(cache, "TYPE", fs->device);
477 * Load the filesystem database from /etc/fstab
479 static void load_fs_info(const char *filename)
487 if ((f = fopen(filename, "r")) == NULL) {
488 bb_perror_msg("WARNING: couldn't open %s", filename);
493 if (!fgets(buf, sizeof(buf), f))
495 buf[sizeof(buf)-1] = 0;
496 if (parse_fstab_line(buf, &fs) < 0) {
497 bb_error_msg("WARNING: bad format "
498 "on line %d of %s\n", lineno, filename);
513 "WARNING: Your /etc/fstab does not contain the fsck passno\n"
514 " field. I will kludge around things for you, but you\n"
515 " should fix your /etc/fstab file as soon as you can.\n\n", stderr);
517 for (fs = filesys_info; fs; fs = fs->next) {
523 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
524 static struct fs_info *lookup(char *filesys)
528 /* No filesys name given. */
532 for (fs = filesys_info; fs; fs = fs->next) {
533 if (!strcmp(filesys, fs->device) ||
534 (fs->mountpt && !strcmp(filesys, fs->mountpt)))
541 /* Find fsck program for a given fs type. */
542 static char *find_fsck(char *type)
546 char *p = string_copy(fsck_path);
549 /* Are we looking for a program or just a type? */
550 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
552 for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
553 s = bb_xasprintf(tpl, s, type);
554 if (stat(s, &st) == 0) break;
561 static int progress_active(void)
563 struct fsck_instance *inst;
565 for (inst = instance_list; inst; inst = inst->next) {
566 if (inst->flags & FLAG_DONE)
568 if (inst->flags & FLAG_PROGRESS)
575 * Execute a particular fsck program, and link it into the list of
576 * child processes we are waiting for.
578 static int execute(const char *type, const char *device, const char *mntpt,
584 struct fsck_instance *inst, *p;
587 inst = malloc(sizeof(struct fsck_instance));
590 memset(inst, 0, sizeof(struct fsck_instance));
592 prog = bb_xasprintf("fsck.%s", type);
596 for (i=0; i <num_args; i++)
597 argv[argc++] = string_copy(args[i]);
599 if (progress && !progress_active()) {
600 if ((strcmp(type, "ext2") == 0) ||
601 (strcmp(type, "ext3") == 0)) {
603 snprintf(tmp, 80, "-C%d", progress_fd);
604 argv[argc++] = string_copy(tmp);
605 inst->flags |= FLAG_PROGRESS;
609 argv[argc++] = string_copy(device);
614 bb_error_msg("%s: not found", prog);
618 if (verbose || noexecute) {
619 printf("[%s (%d) -- %s] ", s, num_running,
620 mntpt ? mntpt : device);
621 for (i=0; i < argc; i++)
622 printf("%s ", argv[i]);
626 /* Fork and execute the correct program. */
629 else if ((pid = fork()) < 0) {
632 } else if (pid == 0) {
635 (void) execv(s, argv);
636 bb_perror_msg_and_die("%s", argv[0]);
639 for (i = 1; i < argc; i++)
645 inst->type = string_copy(type);
646 inst->device = string_copy(device);
647 inst->base_device = base_device(device);
648 inst->start_time = time(0);
652 * Find the end of the list, so we add the instance on at the end.
654 for (p = instance_list; p && p->next; p = p->next);
659 instance_list = inst;
665 * Send a signal to all outstanding fsck child processes
667 static int kill_all(int signum)
669 struct fsck_instance *inst;
672 for (inst = instance_list; inst; inst = inst->next) {
673 if (inst->flags & FLAG_DONE)
675 kill(inst->pid, signum);
682 * Wait for one child process to exit; when it does, unlink it from
683 * the list of executing child processes, and return it.
685 static struct fsck_instance *wait_one(int flags)
689 struct fsck_instance *inst, *inst2, *prev;
696 inst = instance_list;
699 while (inst->next && (random() & 1)) {
704 inst->exit_status = 0;
709 * gcc -Wall fails saving throw against stupidity
710 * (inst and prev are thought to be uninitialized variables)
715 pid = waitpid(-1, &status, flags);
716 if (cancel_requested && !kill_sent) {
720 if ((pid == 0) && (flags & WNOHANG))
723 if ((errno == EINTR) || (errno == EAGAIN))
725 if (errno == ECHILD) {
726 bb_error_msg("wait: No more child process?!?");
732 for (prev = 0, inst = instance_list;
734 prev = inst, inst = inst->next) {
735 if (inst->pid == pid)
740 if (WIFEXITED(status))
741 status = WEXITSTATUS(status);
742 else if (WIFSIGNALED(status)) {
743 sig = WTERMSIG(status);
745 status = EXIT_UNCORRECTED;
747 printf("Warning... %s for device %s exited "
749 inst->prog, inst->device, sig);
753 printf("%s %s: status is %x, should never happen.\n",
754 inst->prog, inst->device, status);
757 inst->exit_status = status;
758 if (progress && (inst->flags & FLAG_PROGRESS) &&
759 !progress_active()) {
760 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
761 if (inst2->flags & FLAG_DONE)
763 if (strcmp(inst2->type, "ext2") &&
764 strcmp(inst2->type, "ext3"))
767 * If we've just started the fsck, wait a tiny
768 * bit before sending the kill, to give it
769 * time to set up the signal handler
771 if (inst2->start_time < time(0)+2) {
774 kill(inst2->pid, SIGUSR1);
778 kill(inst2->pid, SIGUSR1);
779 inst2->flags |= FLAG_PROGRESS;
785 prev->next = inst->next;
787 instance_list = inst->next;
789 printf("Finished with %s (exit status %d)\n",
790 inst->device, inst->exit_status);
795 #define FLAG_WAIT_ALL 0
796 #define FLAG_WAIT_ATLEAST_ONE 1
798 * Wait until all executing child processes have exited; return the
799 * logical OR of all of their exit code values.
801 static int wait_many(int flags)
803 struct fsck_instance *inst;
804 int global_status = 0;
807 while ((inst = wait_one(wait_flags))) {
808 global_status |= inst->exit_status;
811 if (noexecute && (flags & WNOHANG) && !(random() % 3))
814 if (flags & FLAG_WAIT_ATLEAST_ONE)
815 wait_flags = WNOHANG;
817 return global_status;
821 * Run the fsck program on a particular device
823 * If the type is specified using -t, and it isn't prefixed with "no"
824 * (as in "noext2") and only one filesystem type is specified, then
825 * use that type regardless of what is specified in /etc/fstab.
827 * If the type isn't specified by the user, then use either the type
828 * specified in /etc/fstab, or DEFAULT_FSTYPE.
830 static void fsck_device(struct fs_info *fs, int interactive)
837 if (strcmp(fs->type, "auto") != 0)
839 else if (fstype && strncmp(fstype, "no", 2) &&
840 strncmp(fstype, "opts=", 5) && strncmp(fstype, "loop", 4) &&
841 !strchr(fstype, ','))
844 type = DEFAULT_FSTYPE;
847 retval = execute(type, fs->device, fs->mountpt, interactive);
849 bb_error_msg("Error %d while executing fsck.%s for %s",
850 retval, type, fs->device);
857 * Deal with the fsck -t argument.
859 struct fs_type_compile {
865 #define FS_TYPE_NORMAL 0
866 #define FS_TYPE_OPT 1
867 #define FS_TYPE_NEGOPT 2
869 static const char fs_type_syntax_error[] =
870 "Either all or none of the filesystem types passed to -t must be prefixed\n"
873 static void compile_fs_type(char *fs_type, struct fs_type_compile *cmp)
877 int negate, first_negate = 1;
880 for (cp=fs_type; *cp; cp++) {
886 cmp->list = xcalloc(num, sizeof(char *));
887 cmp->type = xcalloc(num, sizeof(int));
893 list = string_copy(fs_type);
895 s = strtok(list, ",");
898 if (strncmp(s, "no", 2) == 0) {
901 } else if (*s == '!') {
905 if (strcmp(s, "loop") == 0)
906 /* loop is really short-hand for opts=loop */
907 goto loop_special_case;
908 else if (strncmp(s, "opts=", 5) == 0) {
911 cmp->type[num] = negate ? FS_TYPE_NEGOPT : FS_TYPE_OPT;
914 cmp->negate = negate;
917 if ((negate && !cmp->negate) ||
918 (!negate && cmp->negate)) {
919 bb_error_msg_and_die("%s", fs_type_syntax_error);
923 printf("Adding %s to list (type %d).\n", s, cmp->type[num]);
925 cmp->list[num++] = string_copy(s);
926 s = strtok(NULL, ",");
932 * This function returns true if a particular option appears in a
933 * comma-delimited options list
935 static int opt_in_list(char *opt, char *optlist)
941 list = string_copy(optlist);
943 s = strtok(list, ",");
945 if (strcmp(s, opt) == 0) {
949 s = strtok(NULL, ",");
955 /* See if the filesystem matches the criteria given by the -t option */
956 static int fs_match(struct fs_info *fs, struct fs_type_compile *cmp)
958 int n, ret = 0, checked_type = 0;
961 if (cmp->list == 0 || cmp->list[0] == 0)
964 for (n=0; (cp = cmp->list[n]); n++) {
965 switch (cmp->type[n]) {
968 if (strcmp(cp, fs->type) == 0) {
973 if (opt_in_list(cp, fs->opts))
977 if (!opt_in_list(cp, fs->opts))
982 if (checked_type == 0)
984 return (cmp->negate ? !ret : ret);
987 /* Check if we should ignore this filesystem. */
988 static int ignore(struct fs_info *fs)
994 * If the pass number is 0, ignore it.
1002 * If a specific fstype is specified, and it doesn't match,
1005 if (!fs_match(fs, &fs_type_compiled)) return 1;
1007 /* Are we ignoring this type? */
1008 if(compare_string_array(ignored_types, fs->type) >= 0)
1011 /* Do we really really want to check this fs? */
1012 wanted = compare_string_array(really_wanted, fs->type) >= 0;
1014 /* See if the <fsck.fs> program is available. */
1015 s = find_fsck(fs->type);
1018 bb_error_msg("cannot check %s: fsck.%s not found",
1019 fs->device, fs->type);
1024 /* We can and want to check this file system type. */
1029 * Returns TRUE if a partition on the same disk is already being
1032 static int device_already_active(char *device)
1034 struct fsck_instance *inst;
1037 if (force_all_parallel)
1041 /* Don't check a soft raid disk with any other disk */
1042 if (instance_list &&
1043 (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1) ||
1044 !strncmp(device, BASE_MD, sizeof(BASE_MD)-1)))
1048 base = base_device(device);
1050 * If we don't know the base device, assume that the device is
1051 * already active if there are any fsck instances running.
1054 return (instance_list != 0);
1055 for (inst = instance_list; inst; inst = inst->next) {
1056 if (!inst->base_device || !strcmp(base, inst->base_device)) {
1065 /* Check all file systems, using the /etc/fstab table. */
1066 static int check_all(void)
1068 struct fs_info *fs = NULL;
1069 int status = EXIT_OK;
1070 int not_done_yet = 1;
1075 fputs("Checking all file systems.\n", stdout);
1078 * Do an initial scan over the filesystem; mark filesystems
1079 * which should be ignored as done, and resolve any "auto"
1080 * filesystem types (done as a side-effect of calling ignore()).
1082 for (fs = filesys_info; fs; fs = fs->next) {
1084 fs->flags |= FLAG_DONE;
1088 * Find and check the root filesystem.
1090 if (!parallel_root) {
1091 for (fs = filesys_info; fs; fs = fs->next) {
1092 if (!strcmp(fs->mountpt, "/"))
1096 if (!skip_root && !ignore(fs)) {
1098 status |= wait_many(FLAG_WAIT_ALL);
1099 if (status > EXIT_NONDESTRUCT)
1102 fs->flags |= FLAG_DONE;
1106 * This is for the bone-headed user who enters the root
1107 * filesystem twice. Skip root will skep all root entries.
1110 for (fs = filesys_info; fs; fs = fs->next)
1111 if (!strcmp(fs->mountpt, "/"))
1112 fs->flags |= FLAG_DONE;
1114 while (not_done_yet) {
1118 for (fs = filesys_info; fs; fs = fs->next) {
1119 if (cancel_requested)
1121 if (fs->flags & FLAG_DONE)
1124 * If the filesystem's pass number is higher
1125 * than the current pass number, then we don't
1128 if (fs->passno > passno) {
1133 * If a filesystem on a particular device has
1134 * already been spawned, then we need to defer
1135 * this to another pass.
1137 if (device_already_active(fs->device)) {
1142 * Spawn off the fsck process
1144 fsck_device(fs, serialize);
1145 fs->flags |= FLAG_DONE;
1148 * Only do one filesystem at a time, or if we
1149 * have a limit on the number of fsck's extant
1150 * at one time, apply that limit.
1153 (max_running && (num_running >= max_running))) {
1158 if (cancel_requested)
1161 printf("--waiting-- (pass %d)\n", passno);
1162 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
1163 FLAG_WAIT_ATLEAST_ONE);
1166 printf("----------------------------------\n");
1171 if (cancel_requested && !kill_sent) {
1175 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
1180 static void usage(void)
1182 fputs("Usage: fsck [-ANPRTV] [ -C [ fd ] ] [-t fstype] [fs-options] [filesys ...]\n", stderr);
1187 static void signal_cancel(int sig FSCK_ATTR((unused)))
1192 static void PRS(int argc, char *argv[])
1195 char *arg, *dev, *tmp = 0;
1198 int opts_for_fsck = 0;
1199 struct sigaction sa;
1202 * Set up signal action
1204 memset(&sa, 0, sizeof(struct sigaction));
1205 sa.sa_handler = signal_cancel;
1206 sigaction(SIGINT, &sa, 0);
1207 sigaction(SIGTERM, &sa, 0);
1213 for (i=1; i < argc; i++) {
1217 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
1218 if (num_devices >= MAX_DEVICES) {
1219 bb_error_msg_and_die("too many devices");
1221 dev = blkid_get_devname(cache, arg, NULL);
1222 if (!dev && strchr(arg, '=')) {
1224 * Check to see if we failed because
1225 * /proc/partitions isn't found.
1227 if (access("/proc/partitions", R_OK) < 0) {
1228 bb_error_msg_and_die("Couldn't open /proc/partitions: %m\n"
1229 "Is /proc mounted?");
1232 * Check to see if this is because
1233 * we're not running as root
1236 bb_error_msg_and_die(
1237 "Must be root to scan for matching filesystems: %s\n", arg);
1239 bb_error_msg_and_die(
1240 "Couldn't find matching filesystem: %s", arg);
1242 devices[num_devices++] = dev ? dev : string_copy(arg);
1245 if (arg[0] != '-' || opts_for_fsck) {
1246 if (num_args >= MAX_ARGS) {
1247 bb_error_msg_and_die("too many arguments");
1249 args[num_args++] = string_copy(arg);
1252 for (j=1; arg[j]; j++) {
1253 if (opts_for_fsck) {
1254 options[++opt] = arg[j];
1264 progress_fd = string_to_int(arg+j+1);
1265 if (progress_fd < 0)
1269 } else if ((i+1) < argc &&
1270 !strncmp(argv[i+1], "-", 1) == 0) {
1271 progress_fd = string_to_int(argv[i]);
1272 if (progress_fd < 0)
1307 else if ((i+1) < argc)
1311 fstype = string_copy(tmp);
1312 compile_fs_type(fstype, &fs_type_compiled);
1321 options[++opt] = arg[j];
1328 options[++opt] = '\0';
1329 if (num_args >= MAX_ARGS) {
1330 bb_error_msg("too many arguments");
1332 args[num_args++] = string_copy(options);
1336 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1337 force_all_parallel++;
1338 if ((tmp = getenv("FSCK_MAX_INST")))
1339 max_running = atoi(tmp);
1342 int fsck_main(int argc, char *argv[])
1345 int interactive = 0;
1349 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
1350 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
1352 blkid_get_cache(&cache, NULL);
1356 printf("fsck %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
1358 fstab = getenv("FSTAB_FILE");
1360 fstab = _PATH_MNTTAB;
1361 load_fs_info(fstab);
1363 fsck_path = e2fs_set_sbin_path();
1365 if ((num_devices == 1) || (serialize))
1368 /* If -A was specified ("check all"), do that! */
1372 if (num_devices == 0) {
1377 for (i = 0 ; i < num_devices; i++) {
1378 if (cancel_requested) {
1385 fs = lookup(devices[i]);
1387 fs = create_fs_device(devices[i], 0, "auto",
1392 fsck_device(fs, interactive);
1394 (max_running && (num_running >= max_running))) {
1395 struct fsck_instance *inst;
1399 status |= inst->exit_status;
1400 free_instance(inst);
1403 printf("----------------------------------\n");
1406 status |= wait_many(FLAG_WAIT_ALL);
1407 blkid_put_cache(cache);