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>
44 #include "blkid/blkid.h"
51 #define _PATH_MNTTAB "/etc/fstab"
58 #ifndef DEFAULT_FSTYPE
59 #define DEFAULT_FSTYPE "ext2"
62 #define MAX_DEVICES 32
66 * Internal structure for mount tabel entries.
81 #define FLAG_PROGRESS 2
84 * Structure to allow exit codes to be stored
86 struct fsck_instance {
95 struct fsck_instance *next;
101 * Return the "base device" given a particular device; this is used to
102 * assure that we only fsck one partition on a particular drive at any
103 * one time. Otherwise, the disk heads will be seeking all over the
104 * place. If the base device can not be determined, return NULL.
106 * The base_device() function returns an allocated string which must
112 #ifdef CONFIG_FEATURE_DEVFS
114 * Required for the uber-silly devfs /dev/ide/host1/bus2/target3/lun3
117 static const char * const devfs_hier[] = {
118 "host", "bus", "target", "lun", 0
122 static char *base_device(const char *device)
125 #ifdef CONFIG_FEATURE_DEVFS
126 const char * const *hier;
131 cp = str = bb_xstrdup(device);
133 /* Skip over /dev/; if it's not present, give up. */
134 if (strncmp(cp, "/dev/", 5) != 0)
138 #if 0 /* this is for old stuff no one uses anymore ? */
139 /* Skip over /dev/dsk/... */
140 if (strncmp(cp, "dsk/", 4) == 0)
145 * For md devices, we treat them all as if they were all
146 * on one disk, since we don't know how to parallelize them.
148 if (cp[0] == 'm' && cp[1] == 'd') {
153 /* Handle DAC 960 devices */
154 if (strncmp(cp, "rd/", 3) == 0) {
156 if (cp[0] != 'c' || cp[2] != 'd' ||
157 !isdigit(cp[1]) || !isdigit(cp[3]))
163 /* Now let's handle /dev/hd* and /dev/sd* devices.... */
164 if ((cp[0] == 'h' || cp[0] == 's') && (cp[1] == 'd')) {
166 /* If there's a single number after /dev/hd, skip it */
169 /* What follows must be an alpha char, or give up */
176 #ifdef CONFIG_FEATURE_DEVFS
177 /* Now let's handle devfs (ugh) names */
179 if (strncmp(cp, "ide/", 4) == 0)
181 if (strncmp(cp, "scsi/", 5) == 0)
186 * Now we proceed down the expected devfs hierarchy.
187 * i.e., .../host1/bus2/target3/lun4/...
188 * If we don't find the expected token, followed by
189 * some number of digits at each level, abort.
191 for (hier = devfs_hier; *hier; hier++) {
193 if (strncmp(cp, *hier, len) != 0)
196 while (*cp != '/' && *cp != 0) {
207 /* Now handle devfs /dev/disc or /dev/disk names */
209 if (strncmp(cp, "discs/", 6) == 0)
211 else if (strncmp(cp, "disks/", 6) == 0)
215 if (strncmp(cp, disk, 4) != 0)
218 while (*cp != '/' && *cp != 0) {
234 static const char * const ignored_types[] = {
246 static const char * const really_wanted[] = {
257 #define BASE_MD "/dev/md"
260 * Global variables for options
262 static char *devices[MAX_DEVICES];
263 static char *args[MAX_ARGS];
264 static int num_devices, num_args;
268 static int noexecute;
269 static int serialize;
270 static int skip_root;
271 static int like_mount;
273 static int parallel_root;
275 static int progress_fd;
276 static int force_all_parallel;
277 static int num_running;
278 static int max_running;
279 static volatile int cancel_requested;
280 static int kill_sent;
282 static struct fs_info *filesys_info, *filesys_last;
283 static struct fsck_instance *instance_list;
284 static char *fsck_path;
285 static blkid_cache cache;
287 static char *string_copy(const char *s)
297 static int string_to_int(const char *s)
302 l = strtol(s, &p, 0);
303 if (*p || l == LONG_MIN || l == LONG_MAX || l < 0 || l > INT_MAX)
309 static char *skip_over_blank(char *cp)
311 while (*cp && isspace(*cp))
316 static char *skip_over_word(char *cp)
318 while (*cp && !isspace(*cp))
323 static void strip_line(char *line)
328 p = line + strlen(line) - 1;
329 if ((*p == '\n') || (*p == '\r'))
336 static char *parse_word(char **buf)
344 word = skip_over_blank(word);
345 next = skip_over_word(word);
352 static void parse_escape(char *word)
360 for (p = q = word; *p; q++) {
365 *q = bb_process_escape_sequence(&p);
371 static void free_instance(struct fsck_instance *i)
378 free(i->base_device);
383 static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
384 const char *type, const char *opts,
385 int freq, int passno)
389 if (!(fs = malloc(sizeof(struct fs_info))))
392 fs->device = string_copy(device);
393 fs->mountpt = string_copy(mntpnt);
394 fs->type = string_copy(type);
395 fs->opts = string_copy(opts ? opts : "");
404 filesys_last->next = fs;
412 static int parse_fstab_line(char *line, struct fs_info **ret_fs)
414 char *dev, *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
419 if ((cp = strchr(line, '#')))
420 *cp = 0; /* Ignore everything after the comment char */
423 device = parse_word(&cp);
424 mntpnt = parse_word(&cp);
425 type = parse_word(&cp);
426 opts = parse_word(&cp);
427 freq = parse_word(&cp);
428 passno = parse_word(&cp);
431 return 0; /* Allow blank lines */
433 if (!mntpnt || !type)
436 parse_escape(device);
437 parse_escape(mntpnt);
441 parse_escape(passno);
443 dev = blkid_get_devname(cache, device, NULL);
447 if (strchr(type, ','))
450 fs = create_fs_device(device, mntpnt, type ? type : "auto", opts,
451 freq ? atoi(freq) : -1,
452 passno ? atoi(passno) : -1);
462 static void interpret_type(struct fs_info *fs)
466 if (strcmp(fs->type, "auto") != 0)
468 t = blkid_get_tag_value(cache, "TYPE", fs->device);
476 * Load the filesystem database from /etc/fstab
478 static void load_fs_info(const char *filename)
486 if ((f = fopen(filename, "r")) == NULL) {
487 bb_perror_msg("WARNING: couldn't open %s", filename);
492 if (!fgets(buf, sizeof(buf), f))
494 buf[sizeof(buf)-1] = 0;
495 if (parse_fstab_line(buf, &fs) < 0) {
496 bb_error_msg("WARNING: bad format "
497 "on line %d of %s\n", lineno, filename);
512 "WARNING: Your /etc/fstab does not contain the fsck passno\n"
513 " field. I will kludge around things for you, but you\n"
514 " should fix your /etc/fstab file as soon as you can.\n\n", stderr);
516 for (fs = filesys_info; fs; fs = fs->next) {
522 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
523 static struct fs_info *lookup(char *filesys)
527 /* No filesys name given. */
531 for (fs = filesys_info; fs; fs = fs->next) {
532 if (!strcmp(filesys, fs->device) ||
533 (fs->mountpt && !strcmp(filesys, fs->mountpt)))
540 /* Find fsck program for a given fs type. */
541 static char *find_fsck(char *type)
545 char *p = string_copy(fsck_path);
548 /* Are we looking for a program or just a type? */
549 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
551 for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
552 s = bb_xasprintf(tpl, s, type);
553 if (stat(s, &st) == 0) break;
560 static int progress_active(void)
562 struct fsck_instance *inst;
564 for (inst = instance_list; inst; inst = inst->next) {
565 if (inst->flags & FLAG_DONE)
567 if (inst->flags & FLAG_PROGRESS)
574 * Execute a particular fsck program, and link it into the list of
575 * child processes we are waiting for.
577 static int execute(const char *type, const char *device, const char *mntpt,
583 struct fsck_instance *inst, *p;
586 inst = malloc(sizeof(struct fsck_instance));
589 memset(inst, 0, sizeof(struct fsck_instance));
591 prog = bb_xasprintf("fsck.%s", type);
595 for (i=0; i <num_args; i++)
596 argv[argc++] = string_copy(args[i]);
598 if (progress && !progress_active()) {
599 if ((strcmp(type, "ext2") == 0) ||
600 (strcmp(type, "ext3") == 0)) {
602 snprintf(tmp, 80, "-C%d", progress_fd);
603 argv[argc++] = string_copy(tmp);
604 inst->flags |= FLAG_PROGRESS;
608 argv[argc++] = string_copy(device);
613 bb_error_msg("%s: not found", prog);
617 if (verbose || noexecute) {
618 printf("[%s (%d) -- %s] ", s, num_running,
619 mntpt ? mntpt : device);
620 for (i=0; i < argc; i++)
621 printf("%s ", argv[i]);
625 /* Fork and execute the correct program. */
628 else if ((pid = fork()) < 0) {
631 } else if (pid == 0) {
634 (void) execv(s, argv);
635 bb_perror_msg_and_die("%s", argv[0]);
638 for (i = 1; i < argc; i++)
644 inst->type = string_copy(type);
645 inst->device = string_copy(device);
646 inst->base_device = base_device(device);
647 inst->start_time = time(0);
651 * Find the end of the list, so we add the instance on at the end.
653 for (p = instance_list; p && p->next; p = p->next);
658 instance_list = inst;
664 * Send a signal to all outstanding fsck child processes
666 static int kill_all(int signum)
668 struct fsck_instance *inst;
671 for (inst = instance_list; inst; inst = inst->next) {
672 if (inst->flags & FLAG_DONE)
674 kill(inst->pid, signum);
681 * Wait for one child process to exit; when it does, unlink it from
682 * the list of executing child processes, and return it.
684 static struct fsck_instance *wait_one(int flags)
688 struct fsck_instance *inst, *inst2, *prev;
695 inst = instance_list;
698 while (inst->next && (random() & 1)) {
703 inst->exit_status = 0;
708 * gcc -Wall fails saving throw against stupidity
709 * (inst and prev are thought to be uninitialized variables)
714 pid = waitpid(-1, &status, flags);
715 if (cancel_requested && !kill_sent) {
719 if ((pid == 0) && (flags & WNOHANG))
722 if ((errno == EINTR) || (errno == EAGAIN))
724 if (errno == ECHILD) {
725 bb_error_msg("wait: No more child process?!?");
731 for (prev = 0, inst = instance_list;
733 prev = inst, inst = inst->next) {
734 if (inst->pid == pid)
739 if (WIFEXITED(status))
740 status = WEXITSTATUS(status);
741 else if (WIFSIGNALED(status)) {
742 sig = WTERMSIG(status);
744 status = EXIT_UNCORRECTED;
746 printf("Warning... %s for device %s exited "
748 inst->prog, inst->device, sig);
752 printf("%s %s: status is %x, should never happen.\n",
753 inst->prog, inst->device, status);
756 inst->exit_status = status;
757 if (progress && (inst->flags & FLAG_PROGRESS) &&
758 !progress_active()) {
759 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
760 if (inst2->flags & FLAG_DONE)
762 if (strcmp(inst2->type, "ext2") &&
763 strcmp(inst2->type, "ext3"))
766 * If we've just started the fsck, wait a tiny
767 * bit before sending the kill, to give it
768 * time to set up the signal handler
770 if (inst2->start_time < time(0)+2) {
773 kill(inst2->pid, SIGUSR1);
777 kill(inst2->pid, SIGUSR1);
778 inst2->flags |= FLAG_PROGRESS;
784 prev->next = inst->next;
786 instance_list = inst->next;
788 printf("Finished with %s (exit status %d)\n",
789 inst->device, inst->exit_status);
794 #define FLAG_WAIT_ALL 0
795 #define FLAG_WAIT_ATLEAST_ONE 1
797 * Wait until all executing child processes have exited; return the
798 * logical OR of all of their exit code values.
800 static int wait_many(int flags)
802 struct fsck_instance *inst;
803 int global_status = 0;
806 while ((inst = wait_one(wait_flags))) {
807 global_status |= inst->exit_status;
810 if (noexecute && (flags & WNOHANG) && !(random() % 3))
813 if (flags & FLAG_WAIT_ATLEAST_ONE)
814 wait_flags = WNOHANG;
816 return global_status;
820 * Run the fsck program on a particular device
822 * If the type is specified using -t, and it isn't prefixed with "no"
823 * (as in "noext2") and only one filesystem type is specified, then
824 * use that type regardless of what is specified in /etc/fstab.
826 * If the type isn't specified by the user, then use either the type
827 * specified in /etc/fstab, or DEFAULT_FSTYPE.
829 static void fsck_device(struct fs_info *fs, int interactive)
836 if (strcmp(fs->type, "auto") != 0)
838 else if (fstype && strncmp(fstype, "no", 2) &&
839 strncmp(fstype, "opts=", 5) && strncmp(fstype, "loop", 4) &&
840 !strchr(fstype, ','))
843 type = DEFAULT_FSTYPE;
846 retval = execute(type, fs->device, fs->mountpt, interactive);
848 bb_error_msg("Error %d while executing fsck.%s for %s",
849 retval, type, fs->device);
856 * Deal with the fsck -t argument.
858 struct fs_type_compile {
864 #define FS_TYPE_NORMAL 0
865 #define FS_TYPE_OPT 1
866 #define FS_TYPE_NEGOPT 2
868 static const char fs_type_syntax_error[] =
869 "Either all or none of the filesystem types passed to -t must be prefixed\n"
872 static void compile_fs_type(char *fs_type, struct fs_type_compile *cmp)
876 int negate, first_negate = 1;
879 for (cp=fs_type; *cp; cp++) {
885 cmp->list = xcalloc(num, sizeof(char *));
886 cmp->type = xcalloc(num, sizeof(int));
892 list = string_copy(fs_type);
894 s = strtok(list, ",");
897 if (strncmp(s, "no", 2) == 0) {
900 } else if (*s == '!') {
904 if (strcmp(s, "loop") == 0)
905 /* loop is really short-hand for opts=loop */
906 goto loop_special_case;
907 else if (strncmp(s, "opts=", 5) == 0) {
910 cmp->type[num] = negate ? FS_TYPE_NEGOPT : FS_TYPE_OPT;
913 cmp->negate = negate;
916 if ((negate && !cmp->negate) ||
917 (!negate && cmp->negate)) {
918 bb_error_msg_and_die("%s", fs_type_syntax_error);
922 printf("Adding %s to list (type %d).\n", s, cmp->type[num]);
924 cmp->list[num++] = string_copy(s);
925 s = strtok(NULL, ",");
931 * This function returns true if a particular option appears in a
932 * comma-delimited options list
934 static int opt_in_list(char *opt, char *optlist)
940 list = string_copy(optlist);
942 s = strtok(list, ",");
944 if (strcmp(s, opt) == 0) {
948 s = strtok(NULL, ",");
954 /* See if the filesystem matches the criteria given by the -t option */
955 static int fs_match(struct fs_info *fs, struct fs_type_compile *cmp)
957 int n, ret = 0, checked_type = 0;
960 if (cmp->list == 0 || cmp->list[0] == 0)
963 for (n=0; (cp = cmp->list[n]); n++) {
964 switch (cmp->type[n]) {
967 if (strcmp(cp, fs->type) == 0) {
972 if (opt_in_list(cp, fs->opts))
976 if (!opt_in_list(cp, fs->opts))
981 if (checked_type == 0)
983 return (cmp->negate ? !ret : ret);
986 /* Check if we should ignore this filesystem. */
987 static int ignore(struct fs_info *fs)
993 * If the pass number is 0, ignore it.
1001 * If a specific fstype is specified, and it doesn't match,
1004 if (!fs_match(fs, &fs_type_compiled)) return 1;
1006 /* Are we ignoring this type? */
1007 if(compare_string_array(ignored_types, fs->type) >= 0)
1010 /* Do we really really want to check this fs? */
1011 wanted = compare_string_array(really_wanted, fs->type) >= 0;
1013 /* See if the <fsck.fs> program is available. */
1014 s = find_fsck(fs->type);
1017 bb_error_msg("cannot check %s: fsck.%s not found",
1018 fs->device, fs->type);
1023 /* We can and want to check this file system type. */
1028 * Returns TRUE if a partition on the same disk is already being
1031 static int device_already_active(char *device)
1033 struct fsck_instance *inst;
1036 if (force_all_parallel)
1040 /* Don't check a soft raid disk with any other disk */
1041 if (instance_list &&
1042 (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1) ||
1043 !strncmp(device, BASE_MD, sizeof(BASE_MD)-1)))
1047 base = base_device(device);
1049 * If we don't know the base device, assume that the device is
1050 * already active if there are any fsck instances running.
1053 return (instance_list != 0);
1054 for (inst = instance_list; inst; inst = inst->next) {
1055 if (!inst->base_device || !strcmp(base, inst->base_device)) {
1064 /* Check all file systems, using the /etc/fstab table. */
1065 static int check_all(void)
1067 struct fs_info *fs = NULL;
1068 int status = EXIT_OK;
1069 int not_done_yet = 1;
1074 fputs("Checking all file systems.\n", stdout);
1077 * Do an initial scan over the filesystem; mark filesystems
1078 * which should be ignored as done, and resolve any "auto"
1079 * filesystem types (done as a side-effect of calling ignore()).
1081 for (fs = filesys_info; fs; fs = fs->next) {
1083 fs->flags |= FLAG_DONE;
1087 * Find and check the root filesystem.
1089 if (!parallel_root) {
1090 for (fs = filesys_info; fs; fs = fs->next) {
1091 if (!strcmp(fs->mountpt, "/"))
1095 if (!skip_root && !ignore(fs)) {
1097 status |= wait_many(FLAG_WAIT_ALL);
1098 if (status > EXIT_NONDESTRUCT)
1101 fs->flags |= FLAG_DONE;
1105 * This is for the bone-headed user who enters the root
1106 * filesystem twice. Skip root will skep all root entries.
1109 for (fs = filesys_info; fs; fs = fs->next)
1110 if (!strcmp(fs->mountpt, "/"))
1111 fs->flags |= FLAG_DONE;
1113 while (not_done_yet) {
1117 for (fs = filesys_info; fs; fs = fs->next) {
1118 if (cancel_requested)
1120 if (fs->flags & FLAG_DONE)
1123 * If the filesystem's pass number is higher
1124 * than the current pass number, then we don't
1127 if (fs->passno > passno) {
1132 * If a filesystem on a particular device has
1133 * already been spawned, then we need to defer
1134 * this to another pass.
1136 if (device_already_active(fs->device)) {
1141 * Spawn off the fsck process
1143 fsck_device(fs, serialize);
1144 fs->flags |= FLAG_DONE;
1147 * Only do one filesystem at a time, or if we
1148 * have a limit on the number of fsck's extant
1149 * at one time, apply that limit.
1152 (max_running && (num_running >= max_running))) {
1157 if (cancel_requested)
1160 printf("--waiting-- (pass %d)\n", passno);
1161 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
1162 FLAG_WAIT_ATLEAST_ONE);
1165 printf("----------------------------------\n");
1170 if (cancel_requested && !kill_sent) {
1174 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
1178 static void signal_cancel(int sig FSCK_ATTR((unused)))
1183 static void PRS(int argc, char *argv[])
1186 char *arg, *dev, *tmp = 0;
1189 int opts_for_fsck = 0;
1190 struct sigaction sa;
1193 * Set up signal action
1195 memset(&sa, 0, sizeof(struct sigaction));
1196 sa.sa_handler = signal_cancel;
1197 sigaction(SIGINT, &sa, 0);
1198 sigaction(SIGTERM, &sa, 0);
1204 for (i=1; i < argc; i++) {
1208 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
1209 if (num_devices >= MAX_DEVICES) {
1210 bb_error_msg_and_die("too many devices");
1212 dev = blkid_get_devname(cache, arg, NULL);
1213 if (!dev && strchr(arg, '=')) {
1215 * Check to see if we failed because
1216 * /proc/partitions isn't found.
1218 if (access("/proc/partitions", R_OK) < 0) {
1219 bb_error_msg_and_die("Couldn't open /proc/partitions: %m\n"
1220 "Is /proc mounted?");
1223 * Check to see if this is because
1224 * we're not running as root
1227 bb_error_msg_and_die(
1228 "Must be root to scan for matching filesystems: %s\n", arg);
1230 bb_error_msg_and_die(
1231 "Couldn't find matching filesystem: %s", arg);
1233 devices[num_devices++] = dev ? dev : string_copy(arg);
1236 if (arg[0] != '-' || opts_for_fsck) {
1237 if (num_args >= MAX_ARGS) {
1238 bb_error_msg_and_die("too many arguments");
1240 args[num_args++] = string_copy(arg);
1243 for (j=1; arg[j]; j++) {
1244 if (opts_for_fsck) {
1245 options[++opt] = arg[j];
1255 progress_fd = string_to_int(arg+j+1);
1256 if (progress_fd < 0)
1260 } else if ((i+1) < argc &&
1261 !strncmp(argv[i+1], "-", 1) == 0) {
1262 progress_fd = string_to_int(argv[i]);
1263 if (progress_fd < 0)
1298 else if ((i+1) < argc)
1302 fstype = string_copy(tmp);
1303 compile_fs_type(fstype, &fs_type_compiled);
1312 options[++opt] = arg[j];
1319 options[++opt] = '\0';
1320 if (num_args >= MAX_ARGS) {
1321 bb_error_msg("too many arguments");
1323 args[num_args++] = string_copy(options);
1327 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1328 force_all_parallel++;
1329 if ((tmp = getenv("FSCK_MAX_INST")))
1330 max_running = atoi(tmp);
1333 int fsck_main(int argc, char *argv[])
1336 int interactive = 0;
1340 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
1341 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
1343 blkid_get_cache(&cache, NULL);
1347 printf("fsck %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
1349 fstab = getenv("FSTAB_FILE");
1351 fstab = _PATH_MNTTAB;
1352 load_fs_info(fstab);
1354 fsck_path = e2fs_set_sbin_path();
1356 if ((num_devices == 1) || (serialize))
1359 /* If -A was specified ("check all"), do that! */
1363 if (num_devices == 0) {
1368 for (i = 0 ; i < num_devices; i++) {
1369 if (cancel_requested) {
1376 fs = lookup(devices[i]);
1378 fs = create_fs_device(devices[i], 0, "auto",
1383 fsck_device(fs, interactive);
1385 (max_running && (num_running >= max_running))) {
1386 struct fsck_instance *inst;
1390 status |= inst->exit_status;
1391 free_instance(inst);
1394 printf("----------------------------------\n");
1397 status |= wait_many(FLAG_WAIT_ALL);
1398 blkid_put_cache(cache);