1 /* vi: set sw=4 ts=4: */
3 * fsck --- 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.
24 * This file may be redistributed under the terms of the GNU Public
29 /* All filesystem specific hooks have been removed.
30 * If filesystem cannot be determined, we will execute
31 * "fsck.auto". Currently this also happens if you specify
32 * UUID=xxx or LABEL=xxx as an object to check.
33 * Detection code for that is also probably has to be in fsck.auto.
35 * In other words, this is _really_ is just a driver program which
36 * spawns actual fsck.something for each filesystem to check.
37 * It doesn't guess filesystem types from on-disk format.
42 /* "progress indicator" code is somewhat buggy and ext[23] specific.
43 * We should be filesystem agnostic. IOW: there should be a well-defined
44 * API for fsck.something, NOT ad-hoc hacks in generic fsck. */
45 #define DO_PROGRESS_INDICATOR 0
48 #define EXIT_NONDESTRUCT 1
49 #define EXIT_DESTRUCT 2
50 #define EXIT_UNCORRECTED 4
53 #define FSCK_CANCELED 32 /* Aborted with a signal or ^C */
56 * Internal structure for mount table entries.
70 #define FLAG_PROGRESS 2
72 * Structure to allow exit codes to be stored
74 struct fsck_instance {
75 struct fsck_instance *next;
78 #if DO_PROGRESS_INDICATOR
83 char *base_device; /* /dev/hda for /dev/hdaN etc */
86 static const char ignored_types[] ALIGN1 =
97 static const char really_wanted[] ALIGN1 =
107 #define BASE_MD "/dev/md"
109 static char **devices;
111 static int num_devices;
115 #define FS_TYPE_FLAG_NORMAL 0
116 #define FS_TYPE_FLAG_OPT 1
117 #define FS_TYPE_FLAG_NEGOPT 2
118 static char **fs_type_list;
119 static uint8_t *fs_type_flag;
120 static smallint fs_type_negated;
122 static volatile smallint cancel_requested;
123 static smallint doall;
124 static smallint noexecute;
125 static smallint serialize;
126 static smallint skip_root;
127 /* static smallint like_mount; */
128 static smallint notitle;
129 static smallint parallel_root;
130 static smallint force_all_parallel;
132 #if DO_PROGRESS_INDICATOR
133 static smallint progress;
134 static int progress_fd;
137 static int num_running;
138 static int max_running;
140 static struct fs_info *filesys_info;
141 static struct fs_info *filesys_last;
142 static struct fsck_instance *instance_list;
145 * Return the "base device" given a particular device; this is used to
146 * assure that we only fsck one partition on a particular drive at any
147 * one time. Otherwise, the disk heads will be seeking all over the
148 * place. If the base device cannot be determined, return NULL.
150 * The base_device() function returns an allocated string which must
153 #if ENABLE_FEATURE_DEVFS
155 * Required for the uber-silly devfs /dev/ide/host1/bus2/target3/lun3
158 static const char *const devfs_hier[] = {
159 "host", "bus", "target", "lun", NULL
163 static char *base_device(const char *device)
166 #if ENABLE_FEATURE_DEVFS
167 const char *const *hier;
171 cp = str = xstrdup(device);
173 /* Skip over /dev/; if it's not present, give up. */
174 if (strncmp(cp, "/dev/", 5) != 0)
179 * For md devices, we treat them all as if they were all
180 * on one disk, since we don't know how to parallelize them.
182 if (cp[0] == 'm' && cp[1] == 'd') {
187 /* Handle DAC 960 devices */
188 if (strncmp(cp, "rd/", 3) == 0) {
190 if (cp[0] != 'c' || !isdigit(cp[1])
191 || cp[2] != 'd' || !isdigit(cp[3]))
197 /* Now let's handle /dev/hd* and /dev/sd* devices.... */
198 if ((cp[0] == 'h' || cp[0] == 's') && cp[1] == 'd') {
200 /* If there's a single number after /dev/hd, skip it */
203 /* What follows must be an alpha char, or give up */
210 #if ENABLE_FEATURE_DEVFS
211 /* Now let's handle devfs (ugh) names */
213 if (strncmp(cp, "ide/", 4) == 0)
215 if (strncmp(cp, "scsi/", 5) == 0)
220 * Now we proceed down the expected devfs hierarchy.
221 * i.e., .../host1/bus2/target3/lun4/...
222 * If we don't find the expected token, followed by
223 * some number of digits at each level, abort.
225 for (hier = devfs_hier; *hier; hier++) {
227 if (strncmp(cp, *hier, len) != 0)
230 while (*cp != '/' && *cp != 0) {
241 /* Now handle devfs /dev/disc or /dev/disk names */
243 if (strncmp(cp, "discs/", 6) == 0)
245 else if (strncmp(cp, "disks/", 6) == 0)
249 if (strncmp(cp, disk, 4) != 0)
252 while (*cp != '/' && *cp != 0) {
266 static void free_instance(struct fsck_instance *p)
270 free(p->base_device);
274 static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
275 const char *type, const char *opts,
280 fs = xzalloc(sizeof(*fs));
281 fs->device = xstrdup(device);
282 fs->mountpt = xstrdup(mntpnt);
283 if (strchr(type, ','))
284 type = (char *)"auto";
285 fs->type = xstrdup(type);
286 fs->opts = xstrdup(opts ? opts : "");
287 fs->passno = passno < 0 ? 1 : passno;
289 /*fs->next = NULL; */
294 filesys_last->next = fs;
300 /* Load the filesystem database from /etc/fstab */
301 static void load_fs_info(const char *filename)
307 fstab = setmntent(filename, "r");
309 bb_perror_msg("cannot read %s", filename);
313 // Loop through entries
314 while (getmntent_r(fstab, &mte, bb_common_bufsiz1, COMMON_BUFSIZE)) {
315 //bb_info_msg("CREATE[%s][%s][%s][%s][%d]", mte.mnt_fsname, mte.mnt_dir,
316 // mte.mnt_type, mte.mnt_opts,
318 fs = create_fs_device(mte.mnt_fsname, mte.mnt_dir,
319 mte.mnt_type, mte.mnt_opts,
325 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
326 static struct fs_info *lookup(char *filesys)
330 for (fs = filesys_info; fs; fs = fs->next) {
331 if (strcmp(filesys, fs->device) == 0
332 || (fs->mountpt && strcmp(filesys, fs->mountpt) == 0)
340 #if DO_PROGRESS_INDICATOR
341 static int progress_active(void)
343 struct fsck_instance *inst;
345 for (inst = instance_list; inst; inst = inst->next) {
346 if (inst->flags & FLAG_DONE)
348 if (inst->flags & FLAG_PROGRESS)
357 * Send a signal to all outstanding fsck child processes
359 static void kill_all_if_cancel_requested(void)
361 static smallint kill_sent;
363 struct fsck_instance *inst;
365 if (!cancel_requested || kill_sent)
368 for (inst = instance_list; inst; inst = inst->next) {
369 if (inst->flags & FLAG_DONE)
371 kill(inst->pid, SIGTERM);
377 * Wait for one child process to exit; when it does, unlink it from
378 * the list of executing child processes, free, and return its exit status.
379 * If there is no exited child, return -1.
381 static int wait_one(int flags)
385 struct fsck_instance *inst, *prev;
390 /* if (noexecute) { already returned -1; } */
393 pid = waitpid(-1, &status, flags);
394 kill_all_if_cancel_requested();
395 if (pid == 0) /* flags == WNOHANG and no children exited */
400 if (errno == ECHILD) { /* paranoia */
401 bb_error_msg("wait: no more children");
404 bb_perror_msg("wait");
408 inst = instance_list;
410 if (inst->pid == pid)
418 if (WIFEXITED(status))
419 status = WEXITSTATUS(status);
420 else if (WIFSIGNALED(status)) {
421 sig = WTERMSIG(status);
422 status = EXIT_UNCORRECTED;
424 printf("Warning: %s %s terminated "
426 inst->prog, inst->device, sig);
430 printf("%s %s: status is %x, should never happen\n",
431 inst->prog, inst->device, status);
435 #if DO_PROGRESS_INDICATOR
436 if (progress && (inst->flags & FLAG_PROGRESS) && !progress_active()) {
437 struct fsck_instance *inst2;
438 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
439 if (inst2->flags & FLAG_DONE)
441 if (strcmp(inst2->type, "ext2") != 0
442 && strcmp(inst2->type, "ext3") != 0
446 /* ext[23], we will send USR1
447 * (request to start displaying progress bar)
449 * If we've just started the fsck, wait a tiny
450 * bit before sending the kill, to give it
451 * time to set up the signal handler
453 if (inst2->start_time >= time(NULL) - 1)
455 kill(inst2->pid, SIGUSR1);
456 inst2->flags |= FLAG_PROGRESS;
463 prev->next = inst->next;
465 instance_list = inst->next;
467 printf("Finished with %s (exit status %d)\n",
468 inst->device, status);
476 * Wait until all executing child processes have exited; return the
477 * logical OR of all of their exit code values.
479 #define FLAG_WAIT_ALL 0
480 #define FLAG_WAIT_ATLEAST_ONE WNOHANG
481 static int wait_many(int flags)
484 int global_status = 0;
487 while ((exit_status = wait_one(wait_flags)) != -1) {
488 global_status |= exit_status;
491 return global_status;
495 * Execute a particular fsck program, and link it into the list of
496 * child processes we are waiting for.
498 static void execute(const char *type, const char *device,
499 const char *mntpt /*, int interactive */)
501 char *argv[num_args + 4]; /* see count below: */
504 struct fsck_instance *inst;
507 argv[0] = xasprintf("fsck.%s", type); /* 1 */
508 for (i = 0; i < num_args; i++)
509 argv[i+1] = args[i]; /* num_args */
512 #if DO_PROGRESS_INDICATOR
513 if (progress && !progress_active()) {
514 if (strcmp(type, "ext2") == 0
515 || strcmp(type, "ext3") == 0
517 argv[argc++] = xasprintf("-C%d", progress_fd); /* 1 */
518 inst->flags |= FLAG_PROGRESS;
523 argv[argc++] = (char*)device; /* 1 */
524 argv[argc] = NULL; /* 1 */
526 if (verbose || noexecute) {
527 printf("[%s (%d) -- %s]", argv[0], num_running,
528 mntpt ? mntpt : device);
529 for (i = 0; i < argc; i++)
530 printf(" %s", argv[i]);
534 /* Fork and execute the correct program. */
539 bb_simple_perror_msg(argv[0]);
542 #if DO_PROGRESS_INDICATOR
543 free(argv[num_args + 1]);
546 /* No child, so don't record an instance */
552 inst = xzalloc(sizeof(*inst));
554 inst->prog = argv[0];
555 inst->device = xstrdup(device);
556 inst->base_device = base_device(device);
557 #if DO_PROGRESS_INDICATOR
558 inst->start_time = time(NULL);
561 /* Add to the list of running fsck's.
562 * (was adding to the end, but adding to the front is simpler...) */
563 inst->next = instance_list;
564 instance_list = inst;
568 * Run the fsck program on a particular device
570 * If the type is specified using -t, and it isn't prefixed with "no"
571 * (as in "noext2") and only one filesystem type is specified, then
572 * use that type regardless of what is specified in /etc/fstab.
574 * If the type isn't specified by the user, then use either the type
575 * specified in /etc/fstab, or "auto".
577 static void fsck_device(struct fs_info *fs /*, int interactive */)
581 if (strcmp(fs->type, "auto") != 0) {
584 bb_info_msg("using filesystem type '%s' %s",
587 && (fstype[0] != 'n' || fstype[1] != 'o') /* != "no" */
588 && strncmp(fstype, "opts=", 5) != 0
589 && strncmp(fstype, "loop", 4) != 0
590 && !strchr(fstype, ',')
594 bb_info_msg("using filesystem type '%s' %s",
599 bb_info_msg("using filesystem type '%s' %s",
604 execute(type, fs->device, fs->mountpt /*, interactive */);
608 * Returns TRUE if a partition on the same disk is already being
611 static int device_already_active(char *device)
613 struct fsck_instance *inst;
616 if (force_all_parallel)
620 /* Don't check a soft raid disk with any other disk */
622 && (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1)
623 || !strncmp(device, BASE_MD, sizeof(BASE_MD)-1))
629 base = base_device(device);
631 * If we don't know the base device, assume that the device is
632 * already active if there are any fsck instances running.
635 return (instance_list != NULL);
637 for (inst = instance_list; inst; inst = inst->next) {
638 if (!inst->base_device || !strcmp(base, inst->base_device)) {
649 * This function returns true if a particular option appears in a
650 * comma-delimited options list
652 static int opt_in_list(char *opt, char *optlist)
663 s = strstr(s + 1, opt);
666 /* neither "opt.." nor "xxx,opt.."? */
667 if (s != optlist && s[-1] != ',')
669 /* neither "..opt" nor "..opt,xxx"? */
670 if (s[len] != '\0' && s[len] != ',')
676 /* See if the filesystem matches the criteria given by the -t option */
677 static int fs_match(struct fs_info *fs)
679 int n, ret, checked_type;
689 cp = fs_type_list[n];
692 switch (fs_type_flag[n]) {
693 case FS_TYPE_FLAG_NORMAL:
695 if (strcmp(cp, fs->type) == 0)
698 case FS_TYPE_FLAG_NEGOPT:
699 if (opt_in_list(cp, fs->opts))
702 case FS_TYPE_FLAG_OPT:
703 if (!opt_in_list(cp, fs->opts))
709 if (checked_type == 0)
712 return (fs_type_negated ? !ret : ret);
715 /* Check if we should ignore this filesystem. */
716 static int ignore(struct fs_info *fs)
719 * If the pass number is 0, ignore it.
725 * If a specific fstype is specified, and it doesn't match,
731 /* Are we ignoring this type? */
732 if (index_in_strings(ignored_types, fs->type) >= 0)
735 /* We can and want to check this file system type. */
739 /* Check all file systems, using the /etc/fstab table. */
740 static int check_all(void)
743 int status = EXIT_OK;
744 smallint not_done_yet;
749 puts("Checking all filesystems");
752 * Do an initial scan over the filesystem; mark filesystems
753 * which should be ignored as done, and resolve any "auto"
754 * filesystem types (done as a side-effect of calling ignore()).
756 for (fs = filesys_info; fs; fs = fs->next)
758 fs->flags |= FLAG_DONE;
761 * Find and check the root filesystem.
763 if (!parallel_root) {
764 for (fs = filesys_info; fs; fs = fs->next) {
765 if (LONE_CHAR(fs->mountpt, '/')) {
766 if (!skip_root && !ignore(fs)) {
767 fsck_device(fs /*, 1*/);
768 status |= wait_many(FLAG_WAIT_ALL);
769 if (status > EXIT_NONDESTRUCT)
772 fs->flags |= FLAG_DONE;
778 * This is for the bone-headed user who has root
779 * filesystem listed twice.
780 * "Skip root" will skip _all_ root entries.
783 for (fs = filesys_info; fs; fs = fs->next)
784 if (LONE_CHAR(fs->mountpt, '/'))
785 fs->flags |= FLAG_DONE;
789 while (not_done_yet) {
793 for (fs = filesys_info; fs; fs = fs->next) {
794 if (cancel_requested)
796 if (fs->flags & FLAG_DONE)
799 * If the filesystem's pass number is higher
800 * than the current pass number, then we didn't
803 if (fs->passno > passno) {
808 * If a filesystem on a particular device has
809 * already been spawned, then we need to defer
810 * this to another pass.
812 if (device_already_active(fs->device)) {
817 * Spawn off the fsck process
819 fsck_device(fs /*, serialize*/);
820 fs->flags |= FLAG_DONE;
823 * Only do one filesystem at a time, or if we
824 * have a limit on the number of fsck's extant
825 * at one time, apply that limit.
828 || (max_running && (num_running >= max_running))
834 if (cancel_requested)
837 printf("--waiting-- (pass %d)\n", passno);
838 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
839 FLAG_WAIT_ATLEAST_ONE);
842 puts("----------------------------------");
847 kill_all_if_cancel_requested();
848 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
853 * Deal with the fsck -t argument.
854 * Huh, for mount "-t novfat,nfs" means "neither vfat nor nfs"!
855 * Why here we require "-t novfat,nonfs" ??
857 static void compile_fs_type(char *fs_type)
864 while ((s = strchr(s, ','))) {
869 fs_type_list = xzalloc(num * sizeof(fs_type_list[0]));
870 fs_type_flag = xzalloc(num * sizeof(fs_type_flag[0]));
871 fs_type_negated = -1; /* not yet known is it negated or not */
879 if (s[0] == 'n' && s[1] == 'o') { /* "no.." */
882 } else if (s[0] == '!') {
887 if (strcmp(s, "loop") == 0)
888 /* loop is really short-hand for opts=loop */
889 goto loop_special_case;
890 if (strncmp(s, "opts=", 5) == 0) {
893 fs_type_flag[num] = negate ? FS_TYPE_FLAG_NEGOPT : FS_TYPE_FLAG_OPT;
895 if (fs_type_negated == -1)
896 fs_type_negated = negate;
897 if (fs_type_negated != negate)
898 bb_error_msg_and_die(
899 "either all or none of the filesystem types passed to -t must be prefixed "
902 comma = strchr(s, ',');
903 fs_type_list[num++] = comma ? xstrndup(s, comma-s) : xstrdup(s);
910 static void parse_args(char **argv)
916 int opts_for_fsck = 0;
918 /* in bss, so already zeroed
921 instance_list = NULL;
924 for (i = 1; argv[i]; i++) {
927 /* "/dev/blk" or "/path" or "UUID=xxx" or "LABEL=xxx" */
928 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
929 // FIXME: must check that arg is a blkdev, or resolve
930 // "/path", "UUID=xxx" or "LABEL=xxx" into block device name
931 // ("UUID=xxx"/"LABEL=xxx" can probably shifted to fsck.auto duties)
932 devices = xrealloc_vector(devices, 2, num_devices);
933 devices[num_devices++] = xstrdup(arg);
937 if (arg[0] != '-' || opts_for_fsck) {
938 args = xrealloc_vector(args, 2, num_args);
939 args[num_args++] = xstrdup(arg);
943 if (LONE_CHAR(arg + 1, '-')) { /* "--" ? */
950 for (j = 1; arg[j]; j++) {
955 #if DO_PROGRESS_INDICATOR
958 if (arg[++j]) { /* -Cn */
959 progress_fd = xatoi_u(&arg[j]);
963 if (!argv[++i]) bb_show_usage();
964 progress_fd = xatoi_u(argv[i]);
997 fstype = xstrdup(tmp);
998 compile_fs_type(fstype);
1005 /* one extra for '\0' */
1006 options = xrealloc(options, optpos + 2);
1007 options[optpos] = arg[j];
1014 options[optpos + 1] = '\0';
1015 args = xrealloc_vector(args, 2, num_args);
1016 args[num_args++] = options;
1019 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1020 force_all_parallel = 1;
1021 tmp = getenv("FSCK_MAX_INST");
1023 max_running = xatoi(tmp);
1026 static void signal_cancel(int sig UNUSED_PARAM)
1028 cancel_requested = 1;
1031 int fsck_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1032 int fsck_main(int argc UNUSED_PARAM, char **argv)
1035 /*int interactive;*/
1039 /* we want wait() to be interruptible */
1040 signal_no_SA_RESTART_empty_mask(SIGINT, signal_cancel);
1041 signal_no_SA_RESTART_empty_mask(SIGTERM, signal_cancel);
1043 setbuf(stdout, NULL);
1048 puts("fsck (busybox "BB_VER", "BB_BT")");
1050 /* Even plain "fsck /dev/hda1" needs fstab to get fs type,
1051 * so we are scanning it anyway */
1052 fstab = getenv("FSTAB_FILE");
1054 fstab = "/etc/fstab";
1055 load_fs_info(fstab);
1057 /*interactive = (num_devices == 1) | serialize;*/
1059 if (num_devices == 0)
1060 /*interactive =*/ serialize = doall = 1;
1065 for (i = 0; i < num_devices; i++) {
1066 if (cancel_requested) {
1067 kill_all_if_cancel_requested();
1071 fs = lookup(devices[i]);
1073 fs = create_fs_device(devices[i], "", "auto", NULL, -1);
1074 fsck_device(fs /*, interactive */);
1077 || (max_running && (num_running >= max_running))
1079 int exit_status = wait_one(0);
1080 if (exit_status >= 0)
1081 status |= exit_status;
1083 puts("----------------------------------");
1086 status |= wait_many(FLAG_WAIT_ALL);