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"
53 #define _PATH_MNTTAB "/etc/fstab"
60 #ifndef DEFAULT_FSTYPE
61 #define DEFAULT_FSTYPE "ext2"
64 #define MAX_DEVICES 32
68 * Internal structure for mount tabel entries.
83 #define FLAG_PROGRESS 2
86 * Structure to allow exit codes to be stored
88 struct fsck_instance {
97 struct fsck_instance *next;
103 * Return the "base device" given a particular device; this is used to
104 * assure that we only fsck one partition on a particular drive at any
105 * one time. Otherwise, the disk heads will be seeking all over the
106 * place. If the base device can not be determined, return NULL.
108 * The base_device() function returns an allocated string which must
114 #ifdef CONFIG_FEATURE_DEVFS
116 * Required for the uber-silly devfs /dev/ide/host1/bus2/target3/lun3
119 static const char * const devfs_hier[] = {
120 "host", "bus", "target", "lun", 0
124 static char *base_device(const char *device)
127 #ifdef CONFIG_FEATURE_DEVFS
128 const char * const *hier;
133 cp = str = bb_xstrdup(device);
135 /* Skip over /dev/; if it's not present, give up. */
136 if (strncmp(cp, "/dev/", 5) != 0)
140 #if 0 /* this is for old stuff no one uses anymore ? */
141 /* Skip over /dev/dsk/... */
142 if (strncmp(cp, "dsk/", 4) == 0)
147 * For md devices, we treat them all as if they were all
148 * on one disk, since we don't know how to parallelize them.
150 if (cp[0] == 'm' && cp[1] == 'd') {
155 /* Handle DAC 960 devices */
156 if (strncmp(cp, "rd/", 3) == 0) {
158 if (cp[0] != 'c' || cp[2] != 'd' ||
159 !isdigit(cp[1]) || !isdigit(cp[3]))
165 /* Now let's handle /dev/hd* and /dev/sd* devices.... */
166 if ((cp[0] == 'h' || cp[0] == 's') && (cp[1] == 'd')) {
168 /* If there's a single number after /dev/hd, skip it */
171 /* What follows must be an alpha char, or give up */
178 #ifdef CONFIG_FEATURE_DEVFS
179 /* Now let's handle devfs (ugh) names */
181 if (strncmp(cp, "ide/", 4) == 0)
183 if (strncmp(cp, "scsi/", 5) == 0)
188 * Now we proceed down the expected devfs hierarchy.
189 * i.e., .../host1/bus2/target3/lun4/...
190 * If we don't find the expected token, followed by
191 * some number of digits at each level, abort.
193 for (hier = devfs_hier; *hier; hier++) {
195 if (strncmp(cp, *hier, len) != 0)
198 while (*cp != '/' && *cp != 0) {
209 /* Now handle devfs /dev/disc or /dev/disk names */
211 if (strncmp(cp, "discs/", 6) == 0)
213 else if (strncmp(cp, "disks/", 6) == 0)
217 if (strncmp(cp, disk, 4) != 0)
220 while (*cp != '/' && *cp != 0) {
236 static const char * const ignored_types[] = {
248 static const char * const really_wanted[] = {
259 #define BASE_MD "/dev/md"
262 * Global variables for options
264 static char *devices[MAX_DEVICES];
265 static char *args[MAX_ARGS];
266 static int num_devices, num_args;
270 static int noexecute;
271 static int serialize;
272 static int skip_root;
273 static int like_mount;
275 static int parallel_root;
277 static int progress_fd;
278 static int force_all_parallel;
279 static int num_running;
280 static int max_running;
281 static volatile int cancel_requested;
282 static int kill_sent;
284 static struct fs_info *filesys_info, *filesys_last;
285 static struct fsck_instance *instance_list;
286 static char *fsck_path;
287 static blkid_cache cache;
289 static char *string_copy(const char *s)
299 static int string_to_int(const char *s)
304 l = strtol(s, &p, 0);
305 if (*p || l == LONG_MIN || l == LONG_MAX || l < 0 || l > INT_MAX)
311 static char *skip_over_blank(char *cp)
313 while (*cp && isspace(*cp))
318 static char *skip_over_word(char *cp)
320 while (*cp && !isspace(*cp))
325 static void strip_line(char *line)
330 p = line + strlen(line) - 1;
331 if ((*p == '\n') || (*p == '\r'))
338 static char *parse_word(char **buf)
346 word = skip_over_blank(word);
347 next = skip_over_word(word);
354 static void parse_escape(char *word)
362 for (p = q = word; *p; q++) {
367 *q = bb_process_escape_sequence(&p);
373 static void free_instance(struct fsck_instance *i)
380 free(i->base_device);
385 static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
386 const char *type, const char *opts,
387 int freq, int passno)
391 if (!(fs = malloc(sizeof(struct fs_info))))
394 fs->device = string_copy(device);
395 fs->mountpt = string_copy(mntpnt);
396 fs->type = string_copy(type);
397 fs->opts = string_copy(opts ? opts : "");
406 filesys_last->next = fs;
414 static int parse_fstab_line(char *line, struct fs_info **ret_fs)
416 char *dev, *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
421 if ((cp = strchr(line, '#')))
422 *cp = 0; /* Ignore everything after the comment char */
425 device = parse_word(&cp);
426 mntpnt = parse_word(&cp);
427 type = parse_word(&cp);
428 opts = parse_word(&cp);
429 freq = parse_word(&cp);
430 passno = parse_word(&cp);
433 return 0; /* Allow blank lines */
435 if (!mntpnt || !type)
438 parse_escape(device);
439 parse_escape(mntpnt);
443 parse_escape(passno);
445 dev = blkid_get_devname(cache, device, NULL);
449 if (strchr(type, ','))
452 fs = create_fs_device(device, mntpnt, type ? type : "auto", opts,
453 freq ? atoi(freq) : -1,
454 passno ? atoi(passno) : -1);
464 static void interpret_type(struct fs_info *fs)
468 if (strcmp(fs->type, "auto") != 0)
470 t = blkid_get_tag_value(cache, "TYPE", fs->device);
478 * Load the filesystem database from /etc/fstab
480 static void load_fs_info(const char *filename)
488 if ((f = fopen(filename, "r")) == NULL) {
489 bb_perror_msg("WARNING: couldn't open %s", filename);
494 if (!fgets(buf, sizeof(buf), f))
496 buf[sizeof(buf)-1] = 0;
497 if (parse_fstab_line(buf, &fs) < 0) {
498 bb_error_msg("WARNING: bad format "
499 "on line %d of %s\n", lineno, filename);
514 "WARNING: Your /etc/fstab does not contain the fsck passno\n"
515 " field. I will kludge around things for you, but you\n"
516 " should fix your /etc/fstab file as soon as you can.\n\n", stderr);
518 for (fs = filesys_info; fs; fs = fs->next) {
524 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
525 static struct fs_info *lookup(char *filesys)
529 /* No filesys name given. */
533 for (fs = filesys_info; fs; fs = fs->next) {
534 if (!strcmp(filesys, fs->device) ||
535 (fs->mountpt && !strcmp(filesys, fs->mountpt)))
542 /* Find fsck program for a given fs type. */
543 static char *find_fsck(char *type)
547 char *p = string_copy(fsck_path);
550 /* Are we looking for a program or just a type? */
551 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
553 for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
554 s = bb_xasprintf(tpl, s, type);
555 if (stat(s, &st) == 0) break;
562 static int progress_active(void)
564 struct fsck_instance *inst;
566 for (inst = instance_list; inst; inst = inst->next) {
567 if (inst->flags & FLAG_DONE)
569 if (inst->flags & FLAG_PROGRESS)
576 * Execute a particular fsck program, and link it into the list of
577 * child processes we are waiting for.
579 static int execute(const char *type, const char *device, const char *mntpt,
585 struct fsck_instance *inst, *p;
588 inst = malloc(sizeof(struct fsck_instance));
591 memset(inst, 0, sizeof(struct fsck_instance));
593 prog = bb_xasprintf("fsck.%s", type);
597 for (i=0; i <num_args; i++)
598 argv[argc++] = string_copy(args[i]);
600 if (progress && !progress_active()) {
601 if ((strcmp(type, "ext2") == 0) ||
602 (strcmp(type, "ext3") == 0)) {
604 snprintf(tmp, 80, "-C%d", progress_fd);
605 argv[argc++] = string_copy(tmp);
606 inst->flags |= FLAG_PROGRESS;
610 argv[argc++] = string_copy(device);
615 bb_error_msg("%s: not found", prog);
619 if (verbose || noexecute) {
620 printf("[%s (%d) -- %s] ", s, num_running,
621 mntpt ? mntpt : device);
622 for (i=0; i < argc; i++)
623 printf("%s ", argv[i]);
627 /* Fork and execute the correct program. */
630 else if ((pid = fork()) < 0) {
633 } else if (pid == 0) {
636 (void) execv(s, argv);
637 bb_perror_msg_and_die("%s", argv[0]);
640 for (i = 1; i < argc; i++)
646 inst->type = string_copy(type);
647 inst->device = string_copy(device);
648 inst->base_device = base_device(device);
649 inst->start_time = time(0);
653 * Find the end of the list, so we add the instance on at the end.
655 for (p = instance_list; p && p->next; p = p->next);
660 instance_list = inst;
666 * Send a signal to all outstanding fsck child processes
668 static int kill_all(int signum)
670 struct fsck_instance *inst;
673 for (inst = instance_list; inst; inst = inst->next) {
674 if (inst->flags & FLAG_DONE)
676 kill(inst->pid, signum);
683 * Wait for one child process to exit; when it does, unlink it from
684 * the list of executing child processes, and return it.
686 static struct fsck_instance *wait_one(int flags)
690 struct fsck_instance *inst, *inst2, *prev;
697 inst = instance_list;
700 while (inst->next && (random() & 1)) {
705 inst->exit_status = 0;
710 * gcc -Wall fails saving throw against stupidity
711 * (inst and prev are thought to be uninitialized variables)
716 pid = waitpid(-1, &status, flags);
717 if (cancel_requested && !kill_sent) {
721 if ((pid == 0) && (flags & WNOHANG))
724 if ((errno == EINTR) || (errno == EAGAIN))
726 if (errno == ECHILD) {
727 bb_error_msg("wait: No more child process?!?");
733 for (prev = 0, inst = instance_list;
735 prev = inst, inst = inst->next) {
736 if (inst->pid == pid)
741 if (WIFEXITED(status))
742 status = WEXITSTATUS(status);
743 else if (WIFSIGNALED(status)) {
744 sig = WTERMSIG(status);
746 status = EXIT_UNCORRECTED;
748 printf("Warning... %s for device %s exited "
750 inst->prog, inst->device, sig);
754 printf("%s %s: status is %x, should never happen.\n",
755 inst->prog, inst->device, status);
758 inst->exit_status = status;
759 if (progress && (inst->flags & FLAG_PROGRESS) &&
760 !progress_active()) {
761 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
762 if (inst2->flags & FLAG_DONE)
764 if (strcmp(inst2->type, "ext2") &&
765 strcmp(inst2->type, "ext3"))
768 * If we've just started the fsck, wait a tiny
769 * bit before sending the kill, to give it
770 * time to set up the signal handler
772 if (inst2->start_time < time(0)+2) {
775 kill(inst2->pid, SIGUSR1);
779 kill(inst2->pid, SIGUSR1);
780 inst2->flags |= FLAG_PROGRESS;
786 prev->next = inst->next;
788 instance_list = inst->next;
790 printf("Finished with %s (exit status %d)\n",
791 inst->device, inst->exit_status);
796 #define FLAG_WAIT_ALL 0
797 #define FLAG_WAIT_ATLEAST_ONE 1
799 * Wait until all executing child processes have exited; return the
800 * logical OR of all of their exit code values.
802 static int wait_many(int flags)
804 struct fsck_instance *inst;
805 int global_status = 0;
808 while ((inst = wait_one(wait_flags))) {
809 global_status |= inst->exit_status;
812 if (noexecute && (flags & WNOHANG) && !(random() % 3))
815 if (flags & FLAG_WAIT_ATLEAST_ONE)
816 wait_flags = WNOHANG;
818 return global_status;
822 * Run the fsck program on a particular device
824 * If the type is specified using -t, and it isn't prefixed with "no"
825 * (as in "noext2") and only one filesystem type is specified, then
826 * use that type regardless of what is specified in /etc/fstab.
828 * If the type isn't specified by the user, then use either the type
829 * specified in /etc/fstab, or DEFAULT_FSTYPE.
831 static void fsck_device(struct fs_info *fs, int interactive)
838 if (strcmp(fs->type, "auto") != 0)
840 else if (fstype && strncmp(fstype, "no", 2) &&
841 strncmp(fstype, "opts=", 5) && strncmp(fstype, "loop", 4) &&
842 !strchr(fstype, ','))
845 type = DEFAULT_FSTYPE;
848 retval = execute(type, fs->device, fs->mountpt, interactive);
850 bb_error_msg("Error %d while executing fsck.%s for %s",
851 retval, type, fs->device);
858 * Deal with the fsck -t argument.
860 struct fs_type_compile {
866 #define FS_TYPE_NORMAL 0
867 #define FS_TYPE_OPT 1
868 #define FS_TYPE_NEGOPT 2
870 static const char fs_type_syntax_error[] =
871 "Either all or none of the filesystem types passed to -t must be prefixed\n"
874 static void compile_fs_type(char *fs_type, struct fs_type_compile *cmp)
878 int negate, first_negate = 1;
881 for (cp=fs_type; *cp; cp++) {
887 cmp->list = xcalloc(num, sizeof(char *));
888 cmp->type = xcalloc(num, sizeof(int));
894 list = string_copy(fs_type);
896 s = strtok(list, ",");
899 if (strncmp(s, "no", 2) == 0) {
902 } else if (*s == '!') {
906 if (strcmp(s, "loop") == 0)
907 /* loop is really short-hand for opts=loop */
908 goto loop_special_case;
909 else if (strncmp(s, "opts=", 5) == 0) {
912 cmp->type[num] = negate ? FS_TYPE_NEGOPT : FS_TYPE_OPT;
915 cmp->negate = negate;
918 if ((negate && !cmp->negate) ||
919 (!negate && cmp->negate)) {
920 bb_error_msg_and_die("%s", fs_type_syntax_error);
924 printf("Adding %s to list (type %d).\n", s, cmp->type[num]);
926 cmp->list[num++] = string_copy(s);
927 s = strtok(NULL, ",");
933 * This function returns true if a particular option appears in a
934 * comma-delimited options list
936 static int opt_in_list(char *opt, char *optlist)
942 list = string_copy(optlist);
944 s = strtok(list, ",");
946 if (strcmp(s, opt) == 0) {
950 s = strtok(NULL, ",");
956 /* See if the filesystem matches the criteria given by the -t option */
957 static int fs_match(struct fs_info *fs, struct fs_type_compile *cmp)
959 int n, ret = 0, checked_type = 0;
962 if (cmp->list == 0 || cmp->list[0] == 0)
965 for (n=0; (cp = cmp->list[n]); n++) {
966 switch (cmp->type[n]) {
969 if (strcmp(cp, fs->type) == 0) {
974 if (opt_in_list(cp, fs->opts))
978 if (!opt_in_list(cp, fs->opts))
983 if (checked_type == 0)
985 return (cmp->negate ? !ret : ret);
988 /* Check if we should ignore this filesystem. */
989 static int ignore(struct fs_info *fs)
995 * If the pass number is 0, ignore it.
1003 * If a specific fstype is specified, and it doesn't match,
1006 if (!fs_match(fs, &fs_type_compiled)) return 1;
1008 /* Are we ignoring this type? */
1009 if(compare_string_array(ignored_types, fs->type) >= 0)
1012 /* Do we really really want to check this fs? */
1013 wanted = compare_string_array(really_wanted, fs->type) >= 0;
1015 /* See if the <fsck.fs> program is available. */
1016 s = find_fsck(fs->type);
1019 bb_error_msg("cannot check %s: fsck.%s not found",
1020 fs->device, fs->type);
1025 /* We can and want to check this file system type. */
1030 * Returns TRUE if a partition on the same disk is already being
1033 static int device_already_active(char *device)
1035 struct fsck_instance *inst;
1038 if (force_all_parallel)
1042 /* Don't check a soft raid disk with any other disk */
1043 if (instance_list &&
1044 (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1) ||
1045 !strncmp(device, BASE_MD, sizeof(BASE_MD)-1)))
1049 base = base_device(device);
1051 * If we don't know the base device, assume that the device is
1052 * already active if there are any fsck instances running.
1055 return (instance_list != 0);
1056 for (inst = instance_list; inst; inst = inst->next) {
1057 if (!inst->base_device || !strcmp(base, inst->base_device)) {
1066 /* Check all file systems, using the /etc/fstab table. */
1067 static int check_all(void)
1069 struct fs_info *fs = NULL;
1070 int status = EXIT_OK;
1071 int not_done_yet = 1;
1076 fputs("Checking all file systems.\n", stdout);
1079 * Do an initial scan over the filesystem; mark filesystems
1080 * which should be ignored as done, and resolve any "auto"
1081 * filesystem types (done as a side-effect of calling ignore()).
1083 for (fs = filesys_info; fs; fs = fs->next) {
1085 fs->flags |= FLAG_DONE;
1089 * Find and check the root filesystem.
1091 if (!parallel_root) {
1092 for (fs = filesys_info; fs; fs = fs->next) {
1093 if (!strcmp(fs->mountpt, "/"))
1097 if (!skip_root && !ignore(fs)) {
1099 status |= wait_many(FLAG_WAIT_ALL);
1100 if (status > EXIT_NONDESTRUCT)
1103 fs->flags |= FLAG_DONE;
1107 * This is for the bone-headed user who enters the root
1108 * filesystem twice. Skip root will skep all root entries.
1111 for (fs = filesys_info; fs; fs = fs->next)
1112 if (!strcmp(fs->mountpt, "/"))
1113 fs->flags |= FLAG_DONE;
1115 while (not_done_yet) {
1119 for (fs = filesys_info; fs; fs = fs->next) {
1120 if (cancel_requested)
1122 if (fs->flags & FLAG_DONE)
1125 * If the filesystem's pass number is higher
1126 * than the current pass number, then we don't
1129 if (fs->passno > passno) {
1134 * If a filesystem on a particular device has
1135 * already been spawned, then we need to defer
1136 * this to another pass.
1138 if (device_already_active(fs->device)) {
1143 * Spawn off the fsck process
1145 fsck_device(fs, serialize);
1146 fs->flags |= FLAG_DONE;
1149 * Only do one filesystem at a time, or if we
1150 * have a limit on the number of fsck's extant
1151 * at one time, apply that limit.
1154 (max_running && (num_running >= max_running))) {
1159 if (cancel_requested)
1162 printf("--waiting-- (pass %d)\n", passno);
1163 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
1164 FLAG_WAIT_ATLEAST_ONE);
1167 printf("----------------------------------\n");
1172 if (cancel_requested && !kill_sent) {
1176 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
1181 static void usage(void)
1183 fputs("Usage: fsck [-ANPRTV] [ -C [ fd ] ] [-t fstype] [fs-options] [filesys ...]\n", stderr);
1188 static void signal_cancel(int sig FSCK_ATTR((unused)))
1193 static void PRS(int argc, char *argv[])
1196 char *arg, *dev, *tmp = 0;
1199 int opts_for_fsck = 0;
1200 struct sigaction sa;
1203 * Set up signal action
1205 memset(&sa, 0, sizeof(struct sigaction));
1206 sa.sa_handler = signal_cancel;
1207 sigaction(SIGINT, &sa, 0);
1208 sigaction(SIGTERM, &sa, 0);
1214 for (i=1; i < argc; i++) {
1218 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
1219 if (num_devices >= MAX_DEVICES) {
1220 bb_error_msg_and_die("too many devices");
1222 dev = blkid_get_devname(cache, arg, NULL);
1223 if (!dev && strchr(arg, '=')) {
1225 * Check to see if we failed because
1226 * /proc/partitions isn't found.
1228 if (access("/proc/partitions", R_OK) < 0) {
1229 bb_error_msg_and_die("Couldn't open /proc/partitions: %m\n"
1230 "Is /proc mounted?");
1233 * Check to see if this is because
1234 * we're not running as root
1237 bb_error_msg_and_die(
1238 "Must be root to scan for matching filesystems: %s\n", arg);
1240 bb_error_msg_and_die(
1241 "Couldn't find matching filesystem: %s", arg);
1243 devices[num_devices++] = dev ? dev : string_copy(arg);
1246 if (arg[0] != '-' || opts_for_fsck) {
1247 if (num_args >= MAX_ARGS) {
1248 bb_error_msg_and_die("too many arguments");
1250 args[num_args++] = string_copy(arg);
1253 for (j=1; arg[j]; j++) {
1254 if (opts_for_fsck) {
1255 options[++opt] = arg[j];
1265 progress_fd = string_to_int(arg+j+1);
1266 if (progress_fd < 0)
1270 } else if ((i+1) < argc &&
1271 !strncmp(argv[i+1], "-", 1) == 0) {
1272 progress_fd = string_to_int(argv[i]);
1273 if (progress_fd < 0)
1308 else if ((i+1) < argc)
1312 fstype = string_copy(tmp);
1313 compile_fs_type(fstype, &fs_type_compiled);
1322 options[++opt] = arg[j];
1329 options[++opt] = '\0';
1330 if (num_args >= MAX_ARGS) {
1331 bb_error_msg("too many arguments");
1333 args[num_args++] = string_copy(options);
1337 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1338 force_all_parallel++;
1339 if ((tmp = getenv("FSCK_MAX_INST")))
1340 max_running = atoi(tmp);
1343 int fsck_main(int argc, char *argv[])
1346 int interactive = 0;
1350 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
1351 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
1353 blkid_get_cache(&cache, NULL);
1357 printf("fsck %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
1359 fstab = getenv("FSTAB_FILE");
1361 fstab = _PATH_MNTTAB;
1362 load_fs_info(fstab);
1364 fsck_path = e2fs_set_sbin_path();
1366 if ((num_devices == 1) || (serialize))
1369 /* If -A was specified ("check all"), do that! */
1373 if (num_devices == 0) {
1378 for (i = 0 ; i < num_devices; i++) {
1379 if (cancel_requested) {
1386 fs = lookup(devices[i]);
1388 fs = create_fs_device(devices[i], 0, "auto",
1393 fsck_device(fs, interactive);
1395 (max_running && (num_running >= max_running))) {
1396 struct fsck_instance *inst;
1400 status |= inst->exit_status;
1401 free_instance(inst);
1404 printf("----------------------------------\n");
1407 status |= wait_many(FLAG_WAIT_ALL);
1408 blkid_put_cache(cache);