1 /* vi: set sw=4 ts=4: */
3 * pfsck --- A generic, parallelizing front-end for the fsck program.
4 * It will automatically try to run fsck programs in parallel if the
5 * devices are on separate spindles. It is based on the same ideas as
6 * the generic front end for fsck by David Engel and Fred van Kempen,
7 * but it has been completely rewritten from scratch to support
10 * Written by Theodore Ts'o, <tytso@mit.edu>
12 * Miquel van Smoorenburg (miquels@drinkel.ow.org) 20-Oct-1994:
13 * o Changed -t fstype to behave like with mount when -A (all file
14 * systems) or -M (like mount) is specified.
15 * o fsck looks if it can find the fsck.type program to decide
16 * if it should ignore the fs type. This way more fsck programs
17 * can be added without changing this front-end.
18 * o -R flag skip root file system.
20 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
21 * 2001, 2002, 2003, 2004, 2005 by Theodore Ts'o.
23 * Licensed under GPLv2, see file LICENSE in this tarball for details.
26 #include <sys/types.h>
42 #include "blkid/blkid.h"
49 #define _PATH_MNTTAB "/etc/fstab"
56 #ifndef DEFAULT_FSTYPE
57 #define DEFAULT_FSTYPE "ext2"
60 #define MAX_DEVICES 32
64 * Internal structure for mount tabel entries.
79 #define FLAG_PROGRESS 2
82 * Structure to allow exit codes to be stored
84 struct fsck_instance {
93 struct fsck_instance *next;
99 * Return the "base device" given a particular device; this is used to
100 * assure that we only fsck one partition on a particular drive at any
101 * one time. Otherwise, the disk heads will be seeking all over the
102 * place. If the base device cannot be determined, return NULL.
104 * The base_device() function returns an allocated string which must
110 #ifdef CONFIG_FEATURE_DEVFS
112 * Required for the uber-silly devfs /dev/ide/host1/bus2/target3/lun3
115 static const char *const devfs_hier[] = {
116 "host", "bus", "target", "lun", 0
120 static char *base_device(const char *device)
123 #ifdef CONFIG_FEATURE_DEVFS
124 const char *const *hier;
129 cp = str = xstrdup(device);
131 /* Skip over /dev/; if it's not present, give up. */
132 if (strncmp(cp, "/dev/", 5) != 0)
137 * For md devices, we treat them all as if they were all
138 * on one disk, since we don't know how to parallelize them.
140 if (cp[0] == 'm' && cp[1] == 'd') {
145 /* Handle DAC 960 devices */
146 if (strncmp(cp, "rd/", 3) == 0) {
148 if (cp[0] != 'c' || cp[2] != 'd' ||
149 !isdigit(cp[1]) || !isdigit(cp[3]))
155 /* Now let's handle /dev/hd* and /dev/sd* devices.... */
156 if ((cp[0] == 'h' || cp[0] == 's') && (cp[1] == 'd')) {
158 /* If there's a single number after /dev/hd, skip it */
161 /* What follows must be an alpha char, or give up */
168 #ifdef CONFIG_FEATURE_DEVFS
169 /* Now let's handle devfs (ugh) names */
171 if (strncmp(cp, "ide/", 4) == 0)
173 if (strncmp(cp, "scsi/", 5) == 0)
178 * Now we proceed down the expected devfs hierarchy.
179 * i.e., .../host1/bus2/target3/lun4/...
180 * If we don't find the expected token, followed by
181 * some number of digits at each level, abort.
183 for (hier = devfs_hier; *hier; hier++) {
185 if (strncmp(cp, *hier, len) != 0)
188 while (*cp != '/' && *cp != 0) {
199 /* Now handle devfs /dev/disc or /dev/disk names */
201 if (strncmp(cp, "discs/", 6) == 0)
203 else if (strncmp(cp, "disks/", 6) == 0)
207 if (strncmp(cp, disk, 4) != 0)
210 while (*cp != '/' && *cp != 0) {
226 static const char *const ignored_types[] = {
238 static const char *const really_wanted[] = {
249 #define BASE_MD "/dev/md"
252 * Global variables for options
254 static char *devices[MAX_DEVICES];
255 static char *args[MAX_ARGS];
256 static int num_devices, num_args;
260 static int noexecute;
261 static int serialize;
262 static int skip_root;
263 static int like_mount;
265 static int parallel_root;
267 static int progress_fd;
268 static int force_all_parallel;
269 static int num_running;
270 static int max_running;
271 static volatile int cancel_requested;
272 static int kill_sent;
274 static struct fs_info *filesys_info, *filesys_last;
275 static struct fsck_instance *instance_list;
276 static char *fsck_path;
277 static blkid_cache cache;
279 static char *string_copy(const char *s)
289 static int string_to_int(const char *s)
294 l = strtol(s, &p, 0);
295 if (*p || l == LONG_MIN || l == LONG_MAX || l < 0 || l > INT_MAX)
301 static char *skip_over_blank(char *cp)
303 while (*cp && isspace(*cp))
308 static char *skip_over_word(char *cp)
310 while (*cp && !isspace(*cp))
315 static void strip_line(char *line)
320 p = line + strlen(line) - 1;
321 if ((*p == '\n') || (*p == '\r'))
328 static char *parse_word(char **buf)
336 word = skip_over_blank(word);
337 next = skip_over_word(word);
344 static void parse_escape(char *word)
352 for (p = q = word; *p; q++) {
357 *q = bb_process_escape_sequence(&p);
363 static void free_instance(struct fsck_instance *i)
370 free(i->base_device);
374 static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
375 const char *type, const char *opts,
376 int freq, int passno)
380 if (!(fs = malloc(sizeof(struct fs_info))))
383 fs->device = string_copy(device);
384 fs->mountpt = string_copy(mntpnt);
385 fs->type = string_copy(type);
386 fs->opts = string_copy(opts ? opts : "");
395 filesys_last->next = fs;
403 static int parse_fstab_line(char *line, struct fs_info **ret_fs)
405 char *dev, *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
410 if ((cp = strchr(line, '#')))
411 *cp = 0; /* Ignore everything after the comment char */
414 device = parse_word(&cp);
415 mntpnt = parse_word(&cp);
416 type = parse_word(&cp);
417 opts = parse_word(&cp);
418 freq = parse_word(&cp);
419 passno = parse_word(&cp);
422 return 0; /* Allow blank lines */
424 if (!mntpnt || !type)
427 parse_escape(device);
428 parse_escape(mntpnt);
432 parse_escape(passno);
434 dev = blkid_get_devname(cache, device, NULL);
438 if (strchr(type, ','))
441 fs = create_fs_device(device, mntpnt, type ? type : "auto", opts,
442 freq ? atoi(freq) : -1,
443 passno ? atoi(passno) : -1);
453 static void interpret_type(struct fs_info *fs)
457 if (strcmp(fs->type, "auto") != 0)
459 t = blkid_get_tag_value(cache, "TYPE", fs->device);
467 * Load the filesystem database from /etc/fstab
469 static void load_fs_info(const char *filename)
477 if ((f = fopen_or_warn(filename, "r")) == NULL) {
482 if (!fgets(buf, sizeof(buf), f))
484 buf[sizeof(buf)-1] = 0;
485 if (parse_fstab_line(buf, &fs) < 0) {
486 bb_error_msg("WARNING: bad format "
487 "on line %d of %s\n", lineno, filename);
502 "WARNING: Your /etc/fstab does not contain the fsck passno\n"
503 " field. I will kludge around things for you, but you\n"
504 " should fix your /etc/fstab file as soon as you can.\n\n", stderr);
506 for (fs = filesys_info; fs; fs = fs->next) {
512 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
513 static struct fs_info *lookup(char *filesys)
517 /* No filesys name given. */
521 for (fs = filesys_info; fs; fs = fs->next) {
522 if (!strcmp(filesys, fs->device) ||
523 (fs->mountpt && !strcmp(filesys, fs->mountpt)))
530 /* Find fsck program for a given fs type. */
531 static char *find_fsck(char *type)
535 char *p = string_copy(fsck_path);
538 /* Are we looking for a program or just a type? */
539 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
541 for (s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
542 s = xasprintf(tpl, s, type);
543 if (stat(s, &st) == 0) break;
550 static int progress_active(void)
552 struct fsck_instance *inst;
554 for (inst = instance_list; inst; inst = inst->next) {
555 if (inst->flags & FLAG_DONE)
557 if (inst->flags & FLAG_PROGRESS)
564 * Execute a particular fsck program, and link it into the list of
565 * child processes we are waiting for.
567 static int execute(const char *type, const char *device, const char *mntpt,
573 struct fsck_instance *inst, *p;
576 inst = malloc(sizeof(struct fsck_instance));
579 memset(inst, 0, sizeof(struct fsck_instance));
581 prog = xasprintf("fsck.%s", type);
585 for (i=0; i <num_args; i++)
586 argv[argc++] = string_copy(args[i]);
588 if (progress && !progress_active()) {
589 if ((strcmp(type, "ext2") == 0) ||
590 (strcmp(type, "ext3") == 0)) {
592 snprintf(tmp, 80, "-C%d", progress_fd);
593 argv[argc++] = string_copy(tmp);
594 inst->flags |= FLAG_PROGRESS;
598 argv[argc++] = string_copy(device);
603 bb_error_msg("%s: not found", prog);
607 if (verbose || noexecute) {
608 printf("[%s (%d) -- %s] ", s, num_running,
609 mntpt ? mntpt : device);
610 for (i=0; i < argc; i++)
611 printf("%s ", argv[i]);
615 /* Fork and execute the correct program. */
618 else if ((pid = fork()) < 0) {
621 } else if (pid == 0) {
624 (void) execv(s, argv);
625 bb_simple_perror_msg_and_die(argv[0]);
628 for (i = 1; i < argc; i++)
634 inst->type = string_copy(type);
635 inst->device = string_copy(device);
636 inst->base_device = base_device(device);
637 inst->start_time = time(0);
641 * Find the end of the list, so we add the instance on at the end.
643 for (p = instance_list; p && p->next; p = p->next);
648 instance_list = inst;
654 * Send a signal to all outstanding fsck child processes
656 static int kill_all(int signum)
658 struct fsck_instance *inst;
661 for (inst = instance_list; inst; inst = inst->next) {
662 if (inst->flags & FLAG_DONE)
664 kill(inst->pid, signum);
671 * Wait for one child process to exit; when it does, unlink it from
672 * the list of executing child processes, and return it.
674 static struct fsck_instance *wait_one(int flags)
678 struct fsck_instance *inst, *inst2, *prev;
685 inst = instance_list;
688 while (inst->next && (random() & 1)) {
693 inst->exit_status = 0;
698 * gcc -Wall fails saving throw against stupidity
699 * (inst and prev are thought to be uninitialized variables)
704 pid = waitpid(-1, &status, flags);
705 if (cancel_requested && !kill_sent) {
709 if ((pid == 0) && (flags & WNOHANG))
712 if ((errno == EINTR) || (errno == EAGAIN))
714 if (errno == ECHILD) {
715 bb_error_msg("wait: no more child process?!?");
721 for (prev = 0, inst = instance_list;
723 prev = inst, inst = inst->next) {
724 if (inst->pid == pid)
729 if (WIFEXITED(status))
730 status = WEXITSTATUS(status);
731 else if (WIFSIGNALED(status)) {
732 sig = WTERMSIG(status);
734 status = EXIT_UNCORRECTED;
736 printf("Warning... %s for device %s exited "
738 inst->prog, inst->device, sig);
742 printf("%s %s: status is %x, should never happen.\n",
743 inst->prog, inst->device, status);
746 inst->exit_status = status;
747 if (progress && (inst->flags & FLAG_PROGRESS) &&
748 !progress_active()) {
749 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
750 if (inst2->flags & FLAG_DONE)
752 if (strcmp(inst2->type, "ext2") &&
753 strcmp(inst2->type, "ext3"))
756 * If we've just started the fsck, wait a tiny
757 * bit before sending the kill, to give it
758 * time to set up the signal handler
760 if (inst2->start_time < time(0)+2) {
763 kill(inst2->pid, SIGUSR1);
767 kill(inst2->pid, SIGUSR1);
768 inst2->flags |= FLAG_PROGRESS;
774 prev->next = inst->next;
776 instance_list = inst->next;
778 printf("Finished with %s (exit status %d)\n",
779 inst->device, inst->exit_status);
784 #define FLAG_WAIT_ALL 0
785 #define FLAG_WAIT_ATLEAST_ONE 1
787 * Wait until all executing child processes have exited; return the
788 * logical OR of all of their exit code values.
790 static int wait_many(int flags)
792 struct fsck_instance *inst;
793 int global_status = 0;
796 while ((inst = wait_one(wait_flags))) {
797 global_status |= inst->exit_status;
800 if (noexecute && (flags & WNOHANG) && !(random() % 3))
803 if (flags & FLAG_WAIT_ATLEAST_ONE)
804 wait_flags = WNOHANG;
806 return global_status;
810 * Run the fsck program on a particular device
812 * If the type is specified using -t, and it isn't prefixed with "no"
813 * (as in "noext2") and only one filesystem type is specified, then
814 * use that type regardless of what is specified in /etc/fstab.
816 * If the type isn't specified by the user, then use either the type
817 * specified in /etc/fstab, or DEFAULT_FSTYPE.
819 static void fsck_device(struct fs_info *fs, int interactive)
826 if (strcmp(fs->type, "auto") != 0)
828 else if (fstype && strncmp(fstype, "no", 2) &&
829 strncmp(fstype, "opts=", 5) && strncmp(fstype, "loop", 4) &&
830 !strchr(fstype, ','))
833 type = DEFAULT_FSTYPE;
836 retval = execute(type, fs->device, fs->mountpt, interactive);
838 bb_error_msg("error %d while executing fsck.%s for %s",
839 retval, type, fs->device);
846 * Deal with the fsck -t argument.
848 struct fs_type_compile {
854 #define FS_TYPE_NORMAL 0
855 #define FS_TYPE_OPT 1
856 #define FS_TYPE_NEGOPT 2
858 static const char fs_type_syntax_error[] =
859 "Either all or none of the filesystem types passed to -t must be prefixed\n"
862 static void compile_fs_type(char *fs_type, struct fs_type_compile *cmp)
866 int negate, first_negate = 1;
869 for (cp=fs_type; *cp; cp++) {
875 cmp->list = xzalloc(num * sizeof(char *));
876 cmp->type = xzalloc(num * sizeof(int));
882 list = string_copy(fs_type);
884 s = strtok(list, ",");
887 if (strncmp(s, "no", 2) == 0) {
890 } else if (*s == '!') {
894 if (strcmp(s, "loop") == 0)
895 /* loop is really short-hand for opts=loop */
896 goto loop_special_case;
897 else if (strncmp(s, "opts=", 5) == 0) {
900 cmp->type[num] = negate ? FS_TYPE_NEGOPT : FS_TYPE_OPT;
903 cmp->negate = negate;
906 if ((negate && !cmp->negate) ||
907 (!negate && cmp->negate)) {
908 bb_error_msg_and_die("%s", fs_type_syntax_error);
911 cmp->list[num++] = string_copy(s);
912 s = strtok(NULL, ",");
918 * This function returns true if a particular option appears in a
919 * comma-delimited options list
921 static int opt_in_list(char *opt, char *optlist)
927 list = string_copy(optlist);
929 s = strtok(list, ",");
931 if (strcmp(s, opt) == 0) {
935 s = strtok(NULL, ",");
941 /* See if the filesystem matches the criteria given by the -t option */
942 static int fs_match(struct fs_info *fs, struct fs_type_compile *cmp)
944 int n, ret = 0, checked_type = 0;
947 if (cmp->list == 0 || cmp->list[0] == 0)
950 for (n=0; (cp = cmp->list[n]); n++) {
951 switch (cmp->type[n]) {
954 if (strcmp(cp, fs->type) == 0) {
959 if (opt_in_list(cp, fs->opts))
963 if (!opt_in_list(cp, fs->opts))
968 if (checked_type == 0)
970 return (cmp->negate ? !ret : ret);
973 /* Check if we should ignore this filesystem. */
974 static int ignore(struct fs_info *fs)
980 * If the pass number is 0, ignore it.
988 * If a specific fstype is specified, and it doesn't match,
991 if (!fs_match(fs, &fs_type_compiled)) return 1;
993 /* Are we ignoring this type? */
994 if (index_in_str_array(ignored_types, fs->type) >= 0)
997 /* Do we really really want to check this fs? */
998 wanted = index_in_str_array(really_wanted, fs->type) >= 0;
1000 /* See if the <fsck.fs> program is available. */
1001 s = find_fsck(fs->type);
1004 bb_error_msg("can't check %s: fsck.%s not found",
1005 fs->device, fs->type);
1010 /* We can and want to check this file system type. */
1015 * Returns TRUE if a partition on the same disk is already being
1018 static int device_already_active(char *device)
1020 struct fsck_instance *inst;
1023 if (force_all_parallel)
1027 /* Don't check a soft raid disk with any other disk */
1028 if (instance_list &&
1029 (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1) ||
1030 !strncmp(device, BASE_MD, sizeof(BASE_MD)-1)))
1034 base = base_device(device);
1036 * If we don't know the base device, assume that the device is
1037 * already active if there are any fsck instances running.
1040 return (instance_list != 0);
1041 for (inst = instance_list; inst; inst = inst->next) {
1042 if (!inst->base_device || !strcmp(base, inst->base_device)) {
1051 /* Check all file systems, using the /etc/fstab table. */
1052 static int check_all(void)
1054 struct fs_info *fs = NULL;
1055 int status = EXIT_OK;
1056 int not_done_yet = 1;
1061 fputs("Checking all file systems.\n", stdout);
1064 * Do an initial scan over the filesystem; mark filesystems
1065 * which should be ignored as done, and resolve any "auto"
1066 * filesystem types (done as a side-effect of calling ignore()).
1068 for (fs = filesys_info; fs; fs = fs->next) {
1070 fs->flags |= FLAG_DONE;
1074 * Find and check the root filesystem.
1076 if (!parallel_root) {
1077 for (fs = filesys_info; fs; fs = fs->next) {
1078 if (LONE_CHAR(fs->mountpt, '/'))
1082 if (!skip_root && !ignore(fs)) {
1084 status |= wait_many(FLAG_WAIT_ALL);
1085 if (status > EXIT_NONDESTRUCT)
1088 fs->flags |= FLAG_DONE;
1092 * This is for the bone-headed user who enters the root
1093 * filesystem twice. Skip root will skep all root entries.
1096 for (fs = filesys_info; fs; fs = fs->next)
1097 if (LONE_CHAR(fs->mountpt, '/'))
1098 fs->flags |= FLAG_DONE;
1100 while (not_done_yet) {
1104 for (fs = filesys_info; fs; fs = fs->next) {
1105 if (cancel_requested)
1107 if (fs->flags & FLAG_DONE)
1110 * If the filesystem's pass number is higher
1111 * than the current pass number, then we don't
1114 if (fs->passno > passno) {
1119 * If a filesystem on a particular device has
1120 * already been spawned, then we need to defer
1121 * this to another pass.
1123 if (device_already_active(fs->device)) {
1128 * Spawn off the fsck process
1130 fsck_device(fs, serialize);
1131 fs->flags |= FLAG_DONE;
1134 * Only do one filesystem at a time, or if we
1135 * have a limit on the number of fsck's extant
1136 * at one time, apply that limit.
1139 (max_running && (num_running >= max_running))) {
1144 if (cancel_requested)
1147 printf("--waiting-- (pass %d)\n", passno);
1148 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
1149 FLAG_WAIT_ATLEAST_ONE);
1152 printf("----------------------------------\n");
1157 if (cancel_requested && !kill_sent) {
1161 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
1165 static void signal_cancel(int sig FSCK_ATTR((unused)))
1170 static void PRS(int argc, char **argv)
1173 char *arg, *dev, *tmp = 0;
1176 int opts_for_fsck = 0;
1177 struct sigaction sa;
1180 * Set up signal action
1182 memset(&sa, 0, sizeof(struct sigaction));
1183 sa.sa_handler = signal_cancel;
1184 sigaction(SIGINT, &sa, 0);
1185 sigaction(SIGTERM, &sa, 0);
1191 for (i=1; i < argc; i++) {
1195 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
1196 if (num_devices >= MAX_DEVICES) {
1197 bb_error_msg_and_die("too many devices");
1199 dev = blkid_get_devname(cache, arg, NULL);
1200 if (!dev && strchr(arg, '=')) {
1202 * Check to see if we failed because
1203 * /proc/partitions isn't found.
1205 if (access("/proc/partitions", R_OK) < 0) {
1206 bb_perror_msg_and_die("can't open /proc/partitions "
1207 "(is /proc mounted?)");
1210 * Check to see if this is because
1211 * we're not running as root
1214 bb_error_msg_and_die(
1215 "must be root to scan for matching filesystems: %s\n", arg);
1217 bb_error_msg_and_die(
1218 "can't find matching filesystem: %s", arg);
1220 devices[num_devices++] = dev ? dev : string_copy(arg);
1223 if (arg[0] != '-' || opts_for_fsck) {
1224 if (num_args >= MAX_ARGS) {
1225 bb_error_msg_and_die("too many arguments");
1227 args[num_args++] = string_copy(arg);
1230 for (j=1; arg[j]; j++) {
1231 if (opts_for_fsck) {
1232 options[++opt] = arg[j];
1242 progress_fd = string_to_int(arg+j+1);
1243 if (progress_fd < 0)
1247 } else if ((i+1) < argc
1248 && argv[i+1][0] != '-') {
1249 progress_fd = string_to_int(argv[i]);
1250 if (progress_fd < 0)
1285 else if ((i+1) < argc)
1289 fstype = string_copy(tmp);
1290 compile_fs_type(fstype, &fs_type_compiled);
1299 options[++opt] = arg[j];
1306 options[++opt] = '\0';
1307 if (num_args >= MAX_ARGS) {
1308 bb_error_msg("too many arguments");
1310 args[num_args++] = string_copy(options);
1314 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1315 force_all_parallel++;
1316 if ((tmp = getenv("FSCK_MAX_INST")))
1317 max_running = atoi(tmp);
1320 int fsck_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1321 int fsck_main(int argc, char **argv)
1324 int interactive = 0;
1328 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
1329 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
1331 blkid_get_cache(&cache, NULL);
1335 printf("fsck %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
1337 fstab = getenv("FSTAB_FILE");
1339 fstab = _PATH_MNTTAB;
1340 load_fs_info(fstab);
1342 fsck_path = e2fs_set_sbin_path();
1344 if ((num_devices == 1) || (serialize))
1347 /* If -A was specified ("check all"), do that! */
1351 if (num_devices == 0) {
1356 for (i = 0; i < num_devices; i++) {
1357 if (cancel_requested) {
1364 fs = lookup(devices[i]);
1366 fs = create_fs_device(devices[i], 0, "auto",
1371 fsck_device(fs, interactive);
1373 (max_running && (num_running >= max_running))) {
1374 struct fsck_instance *inst;
1378 status |= inst->exit_status;
1379 free_instance(inst);
1382 printf("----------------------------------\n");
1385 status |= wait_many(FLAG_WAIT_ALL);
1386 blkid_put_cache(cache);