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.
23 * Licensed under GPLv2, see file LICENSE in this tarball for details.
26 /* All filesystem specific hooks have been removed.
27 * If filesystem cannot be determined, we will execute
28 * "fsck.auto". Currently this also happens if you specify
29 * UUID=xxx or LABEL=xxx as an object to check.
30 * Detection code for that is also probably has to be in fsck.auto.
32 * In other words, this is _really_ is just a driver program which
33 * spawns actual fsck.something for each filesystem to check.
34 * It doesn't guess filesystem types from on-disk format.
39 /* "progress indicator" code is somewhat buggy and ext[23] specific.
40 * We should be filesystem agnostic. IOW: there should be a well-defined
41 * API for fsck.something, NOT ad-hoc hacks in generic fsck. */
42 #define DO_PROGRESS_INDICATOR 0
45 #define EXIT_NONDESTRUCT 1
46 #define EXIT_DESTRUCT 2
47 #define EXIT_UNCORRECTED 4
50 #define FSCK_CANCELED 32 /* Aborted with a signal or ^C */
53 * Internal structure for mount table entries.
67 #define FLAG_PROGRESS 2
69 * Structure to allow exit codes to be stored
71 struct fsck_instance {
72 struct fsck_instance *next;
75 #if DO_PROGRESS_INDICATOR
80 char *base_device; /* /dev/hda for /dev/hdaN etc */
83 static const char ignored_types[] ALIGN1 =
94 static const char really_wanted[] ALIGN1 =
104 #define BASE_MD "/dev/md"
106 static char **devices;
108 static int num_devices;
112 #define FS_TYPE_FLAG_NORMAL 0
113 #define FS_TYPE_FLAG_OPT 1
114 #define FS_TYPE_FLAG_NEGOPT 2
115 static char **fs_type_list;
116 static uint8_t *fs_type_flag;
117 static smallint fs_type_negated;
119 static volatile smallint cancel_requested;
120 static smallint doall;
121 static smallint noexecute;
122 static smallint serialize;
123 static smallint skip_root;
124 /* static smallint like_mount; */
125 static smallint notitle;
126 static smallint parallel_root;
127 static smallint force_all_parallel;
129 #if DO_PROGRESS_INDICATOR
130 static smallint progress;
131 static int progress_fd;
134 static int num_running;
135 static int max_running;
137 static struct fs_info *filesys_info;
138 static struct fs_info *filesys_last;
139 static struct fsck_instance *instance_list;
142 * Return the "base device" given a particular device; this is used to
143 * assure that we only fsck one partition on a particular drive at any
144 * one time. Otherwise, the disk heads will be seeking all over the
145 * place. If the base device cannot be determined, return NULL.
147 * The base_device() function returns an allocated string which must
150 #if ENABLE_FEATURE_DEVFS
152 * Required for the uber-silly devfs /dev/ide/host1/bus2/target3/lun3
155 static const char *const devfs_hier[] = {
156 "host", "bus", "target", "lun", NULL
160 static char *base_device(const char *device)
163 #if ENABLE_FEATURE_DEVFS
164 const char *const *hier;
168 cp = str = xstrdup(device);
170 /* Skip over /dev/; if it's not present, give up. */
171 if (strncmp(cp, "/dev/", 5) != 0)
176 * For md devices, we treat them all as if they were all
177 * on one disk, since we don't know how to parallelize them.
179 if (cp[0] == 'm' && cp[1] == 'd') {
184 /* Handle DAC 960 devices */
185 if (strncmp(cp, "rd/", 3) == 0) {
187 if (cp[0] != 'c' || !isdigit(cp[1])
188 || cp[2] != 'd' || !isdigit(cp[3]))
194 /* Now let's handle /dev/hd* and /dev/sd* devices.... */
195 if ((cp[0] == 'h' || cp[0] == 's') && cp[1] == 'd') {
197 /* If there's a single number after /dev/hd, skip it */
200 /* What follows must be an alpha char, or give up */
207 #if ENABLE_FEATURE_DEVFS
208 /* Now let's handle devfs (ugh) names */
210 if (strncmp(cp, "ide/", 4) == 0)
212 if (strncmp(cp, "scsi/", 5) == 0)
217 * Now we proceed down the expected devfs hierarchy.
218 * i.e., .../host1/bus2/target3/lun4/...
219 * If we don't find the expected token, followed by
220 * some number of digits at each level, abort.
222 for (hier = devfs_hier; *hier; hier++) {
224 if (strncmp(cp, *hier, len) != 0)
227 while (*cp != '/' && *cp != 0) {
238 /* Now handle devfs /dev/disc or /dev/disk names */
240 if (strncmp(cp, "discs/", 6) == 0)
242 else if (strncmp(cp, "disks/", 6) == 0)
246 if (strncmp(cp, disk, 4) != 0)
249 while (*cp != '/' && *cp != 0) {
263 static void free_instance(struct fsck_instance *p)
267 free(p->base_device);
271 static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
272 const char *type, const char *opts,
277 fs = xzalloc(sizeof(*fs));
278 fs->device = xstrdup(device);
279 fs->mountpt = xstrdup(mntpnt);
280 if (strchr(type, ','))
281 type = (char *)"auto";
282 fs->type = xstrdup(type);
283 fs->opts = xstrdup(opts ? opts : "");
284 fs->passno = passno < 0 ? 1 : passno;
286 /*fs->next = NULL; */
291 filesys_last->next = fs;
297 /* Load the filesystem database from /etc/fstab */
298 static void load_fs_info(const char *filename)
304 fstab = setmntent(filename, "r");
306 bb_perror_msg("cannot read %s", filename);
310 // Loop through entries
311 while (getmntent_r(fstab, &mte, bb_common_bufsiz1, COMMON_BUFSIZE)) {
312 //bb_info_msg("CREATE[%s][%s][%s][%s][%d]", mte.mnt_fsname, mte.mnt_dir,
313 // mte.mnt_type, mte.mnt_opts,
315 fs = create_fs_device(mte.mnt_fsname, mte.mnt_dir,
316 mte.mnt_type, mte.mnt_opts,
322 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
323 static struct fs_info *lookup(char *filesys)
327 for (fs = filesys_info; fs; fs = fs->next) {
328 if (strcmp(filesys, fs->device) == 0
329 || (fs->mountpt && strcmp(filesys, fs->mountpt) == 0)
337 #if DO_PROGRESS_INDICATOR
338 static int progress_active(void)
340 struct fsck_instance *inst;
342 for (inst = instance_list; inst; inst = inst->next) {
343 if (inst->flags & FLAG_DONE)
345 if (inst->flags & FLAG_PROGRESS)
354 * Send a signal to all outstanding fsck child processes
356 static void kill_all_if_cancel_requested(void)
358 static smallint kill_sent;
360 struct fsck_instance *inst;
362 if (!cancel_requested || kill_sent)
365 for (inst = instance_list; inst; inst = inst->next) {
366 if (inst->flags & FLAG_DONE)
368 kill(inst->pid, SIGTERM);
374 * Wait for one child process to exit; when it does, unlink it from
375 * the list of executing child processes, free, and return its exit status.
376 * If there is no exited child, return -1.
378 static int wait_one(int flags)
382 struct fsck_instance *inst, *prev;
387 /* if (noexecute) { already returned -1; } */
390 pid = waitpid(-1, &status, flags);
391 kill_all_if_cancel_requested();
392 if (pid == 0) /* flags == WNOHANG and no children exited */
397 if (errno == ECHILD) { /* paranoia */
398 bb_error_msg("wait: no more children");
401 bb_perror_msg("wait");
405 inst = instance_list;
407 if (inst->pid == pid)
415 if (WIFEXITED(status))
416 status = WEXITSTATUS(status);
417 else if (WIFSIGNALED(status)) {
418 sig = WTERMSIG(status);
419 status = EXIT_UNCORRECTED;
421 printf("Warning: %s %s terminated "
423 inst->prog, inst->device, sig);
427 printf("%s %s: status is %x, should never happen\n",
428 inst->prog, inst->device, status);
432 #if DO_PROGRESS_INDICATOR
433 if (progress && (inst->flags & FLAG_PROGRESS) && !progress_active()) {
434 struct fsck_instance *inst2;
435 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
436 if (inst2->flags & FLAG_DONE)
438 if (strcmp(inst2->type, "ext2") != 0
439 && strcmp(inst2->type, "ext3") != 0
443 /* ext[23], we will send USR1
444 * (request to start displaying progress bar)
446 * If we've just started the fsck, wait a tiny
447 * bit before sending the kill, to give it
448 * time to set up the signal handler
450 if (inst2->start_time >= time(NULL) - 1)
452 kill(inst2->pid, SIGUSR1);
453 inst2->flags |= FLAG_PROGRESS;
460 prev->next = inst->next;
462 instance_list = inst->next;
464 printf("Finished with %s (exit status %d)\n",
465 inst->device, status);
473 * Wait until all executing child processes have exited; return the
474 * logical OR of all of their exit code values.
476 #define FLAG_WAIT_ALL 0
477 #define FLAG_WAIT_ATLEAST_ONE WNOHANG
478 static int wait_many(int flags)
481 int global_status = 0;
484 while ((exit_status = wait_one(wait_flags)) != -1) {
485 global_status |= exit_status;
488 return global_status;
492 * Execute a particular fsck program, and link it into the list of
493 * child processes we are waiting for.
495 static void execute(const char *type, const char *device,
496 const char *mntpt /*, int interactive */)
498 char *argv[num_args + 4]; /* see count below: */
501 struct fsck_instance *inst;
504 argv[0] = xasprintf("fsck.%s", type); /* 1 */
505 for (i = 0; i < num_args; i++)
506 argv[i+1] = args[i]; /* num_args */
509 #if DO_PROGRESS_INDICATOR
510 if (progress && !progress_active()) {
511 if (strcmp(type, "ext2") == 0
512 || strcmp(type, "ext3") == 0
514 argv[argc++] = xasprintf("-C%d", progress_fd); /* 1 */
515 inst->flags |= FLAG_PROGRESS;
520 argv[argc++] = (char*)device; /* 1 */
521 argv[argc] = NULL; /* 1 */
523 if (verbose || noexecute) {
524 printf("[%s (%d) -- %s]", argv[0], num_running,
525 mntpt ? mntpt : device);
526 for (i = 0; i < argc; i++)
527 printf(" %s", argv[i]);
531 /* Fork and execute the correct program. */
536 bb_simple_perror_msg(argv[0]);
539 #if DO_PROGRESS_INDICATOR
540 free(argv[num_args + 1]);
543 /* No child, so don't record an instance */
549 inst = xzalloc(sizeof(*inst));
551 inst->prog = argv[0];
552 inst->device = xstrdup(device);
553 inst->base_device = base_device(device);
554 #if DO_PROGRESS_INDICATOR
555 inst->start_time = time(NULL);
558 /* Add to the list of running fsck's.
559 * (was adding to the end, but adding to the front is simpler...) */
560 inst->next = instance_list;
561 instance_list = inst;
565 * Run the fsck program on a particular device
567 * If the type is specified using -t, and it isn't prefixed with "no"
568 * (as in "noext2") and only one filesystem type is specified, then
569 * use that type regardless of what is specified in /etc/fstab.
571 * If the type isn't specified by the user, then use either the type
572 * specified in /etc/fstab, or "auto".
574 static void fsck_device(struct fs_info *fs /*, int interactive */)
578 if (strcmp(fs->type, "auto") != 0) {
581 bb_info_msg("using filesystem type '%s' %s",
584 && (fstype[0] != 'n' || fstype[1] != 'o') /* != "no" */
585 && strncmp(fstype, "opts=", 5) != 0
586 && strncmp(fstype, "loop", 4) != 0
587 && !strchr(fstype, ',')
591 bb_info_msg("using filesystem type '%s' %s",
596 bb_info_msg("using filesystem type '%s' %s",
601 execute(type, fs->device, fs->mountpt /*, interactive */);
605 * Returns TRUE if a partition on the same disk is already being
608 static int device_already_active(char *device)
610 struct fsck_instance *inst;
613 if (force_all_parallel)
617 /* Don't check a soft raid disk with any other disk */
619 && (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1)
620 || !strncmp(device, BASE_MD, sizeof(BASE_MD)-1))
626 base = base_device(device);
628 * If we don't know the base device, assume that the device is
629 * already active if there are any fsck instances running.
632 return (instance_list != NULL);
634 for (inst = instance_list; inst; inst = inst->next) {
635 if (!inst->base_device || !strcmp(base, inst->base_device)) {
646 * This function returns true if a particular option appears in a
647 * comma-delimited options list
649 static int opt_in_list(char *opt, char *optlist)
660 s = strstr(s + 1, opt);
663 /* neither "opt.." nor "xxx,opt.."? */
664 if (s != optlist && s[-1] != ',')
666 /* neither "..opt" nor "..opt,xxx"? */
667 if (s[len] != '\0' && s[len] != ',')
673 /* See if the filesystem matches the criteria given by the -t option */
674 static int fs_match(struct fs_info *fs)
676 int n, ret, checked_type;
686 cp = fs_type_list[n];
689 switch (fs_type_flag[n]) {
690 case FS_TYPE_FLAG_NORMAL:
692 if (strcmp(cp, fs->type) == 0)
695 case FS_TYPE_FLAG_NEGOPT:
696 if (opt_in_list(cp, fs->opts))
699 case FS_TYPE_FLAG_OPT:
700 if (!opt_in_list(cp, fs->opts))
706 if (checked_type == 0)
709 return (fs_type_negated ? !ret : ret);
712 /* Check if we should ignore this filesystem. */
713 static int ignore(struct fs_info *fs)
716 * If the pass number is 0, ignore it.
722 * If a specific fstype is specified, and it doesn't match,
728 /* Are we ignoring this type? */
729 if (index_in_strings(ignored_types, fs->type) >= 0)
732 /* We can and want to check this file system type. */
736 /* Check all file systems, using the /etc/fstab table. */
737 static int check_all(void)
740 int status = EXIT_OK;
741 smallint not_done_yet;
746 puts("Checking all filesystems");
749 * Do an initial scan over the filesystem; mark filesystems
750 * which should be ignored as done, and resolve any "auto"
751 * filesystem types (done as a side-effect of calling ignore()).
753 for (fs = filesys_info; fs; fs = fs->next)
755 fs->flags |= FLAG_DONE;
758 * Find and check the root filesystem.
760 if (!parallel_root) {
761 for (fs = filesys_info; fs; fs = fs->next) {
762 if (LONE_CHAR(fs->mountpt, '/')) {
763 if (!skip_root && !ignore(fs)) {
764 fsck_device(fs /*, 1*/);
765 status |= wait_many(FLAG_WAIT_ALL);
766 if (status > EXIT_NONDESTRUCT)
769 fs->flags |= FLAG_DONE;
775 * This is for the bone-headed user who has root
776 * filesystem listed twice.
777 * "Skip root" will skip _all_ root entries.
780 for (fs = filesys_info; fs; fs = fs->next)
781 if (LONE_CHAR(fs->mountpt, '/'))
782 fs->flags |= FLAG_DONE;
786 while (not_done_yet) {
790 for (fs = filesys_info; fs; fs = fs->next) {
791 if (cancel_requested)
793 if (fs->flags & FLAG_DONE)
796 * If the filesystem's pass number is higher
797 * than the current pass number, then we didn't
800 if (fs->passno > passno) {
805 * If a filesystem on a particular device has
806 * already been spawned, then we need to defer
807 * this to another pass.
809 if (device_already_active(fs->device)) {
814 * Spawn off the fsck process
816 fsck_device(fs /*, serialize*/);
817 fs->flags |= FLAG_DONE;
820 * Only do one filesystem at a time, or if we
821 * have a limit on the number of fsck's extant
822 * at one time, apply that limit.
825 || (max_running && (num_running >= max_running))
831 if (cancel_requested)
834 printf("--waiting-- (pass %d)\n", passno);
835 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
836 FLAG_WAIT_ATLEAST_ONE);
839 puts("----------------------------------");
844 kill_all_if_cancel_requested();
845 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
850 * Deal with the fsck -t argument.
851 * Huh, for mount "-t novfat,nfs" means "neither vfat nor nfs"!
852 * Why here we require "-t novfat,nonfs" ??
854 static void compile_fs_type(char *fs_type)
861 while ((s = strchr(s, ','))) {
866 fs_type_list = xzalloc(num * sizeof(fs_type_list[0]));
867 fs_type_flag = xzalloc(num * sizeof(fs_type_flag[0]));
868 fs_type_negated = -1; /* not yet known is it negated or not */
876 if (s[0] == 'n' && s[1] == 'o') { /* "no.." */
879 } else if (s[0] == '!') {
884 if (strcmp(s, "loop") == 0)
885 /* loop is really short-hand for opts=loop */
886 goto loop_special_case;
887 if (strncmp(s, "opts=", 5) == 0) {
890 fs_type_flag[num] = negate ? FS_TYPE_FLAG_NEGOPT : FS_TYPE_FLAG_OPT;
892 if (fs_type_negated == -1)
893 fs_type_negated = negate;
894 if (fs_type_negated != negate)
895 bb_error_msg_and_die(
896 "either all or none of the filesystem types passed to -t must be prefixed "
899 comma = strchr(s, ',');
900 fs_type_list[num++] = comma ? xstrndup(s, comma-s) : xstrdup(s);
907 static void parse_args(char **argv)
913 int opts_for_fsck = 0;
915 /* in bss, so already zeroed
918 instance_list = NULL;
921 for (i = 1; argv[i]; i++) {
924 /* "/dev/blk" or "/path" or "UUID=xxx" or "LABEL=xxx" */
925 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
926 // FIXME: must check that arg is a blkdev, or resolve
927 // "/path", "UUID=xxx" or "LABEL=xxx" into block device name
928 // ("UUID=xxx"/"LABEL=xxx" can probably shifted to fsck.auto duties)
929 devices = xrealloc_vector(devices, 2, num_devices);
930 devices[num_devices++] = xstrdup(arg);
934 if (arg[0] != '-' || opts_for_fsck) {
935 args = xrealloc_vector(args, 2, num_args);
936 args[num_args++] = xstrdup(arg);
940 if (LONE_CHAR(arg + 1, '-')) { /* "--" ? */
947 for (j = 1; arg[j]; j++) {
952 #if DO_PROGRESS_INDICATOR
955 if (arg[++j]) { /* -Cn */
956 progress_fd = xatoi_u(&arg[j]);
960 if (!argv[++i]) bb_show_usage();
961 progress_fd = xatoi_u(argv[i]);
994 fstype = xstrdup(tmp);
995 compile_fs_type(fstype);
1002 /* one extra for '\0' */
1003 options = xrealloc(options, optpos + 2);
1004 options[optpos] = arg[j];
1011 options[optpos + 1] = '\0';
1012 args = xrealloc_vector(args, 2, num_args);
1013 args[num_args++] = options;
1016 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1017 force_all_parallel = 1;
1018 tmp = getenv("FSCK_MAX_INST");
1020 max_running = xatoi(tmp);
1023 static void signal_cancel(int sig UNUSED_PARAM)
1025 cancel_requested = 1;
1028 int fsck_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1029 int fsck_main(int argc UNUSED_PARAM, char **argv)
1032 /*int interactive;*/
1036 /* we want wait() to be interruptible */
1037 signal_no_SA_RESTART_empty_mask(SIGINT, signal_cancel);
1038 signal_no_SA_RESTART_empty_mask(SIGTERM, signal_cancel);
1040 setbuf(stdout, NULL);
1045 puts("fsck (busybox "BB_VER", "BB_BT")");
1047 /* Even plain "fsck /dev/hda1" needs fstab to get fs type,
1048 * so we are scanning it anyway */
1049 fstab = getenv("FSTAB_FILE");
1051 fstab = "/etc/fstab";
1052 load_fs_info(fstab);
1054 /*interactive = (num_devices == 1) | serialize;*/
1056 if (num_devices == 0)
1057 /*interactive =*/ serialize = doall = 1;
1062 for (i = 0; i < num_devices; i++) {
1063 if (cancel_requested) {
1064 kill_all_if_cancel_requested();
1068 fs = lookup(devices[i]);
1070 fs = create_fs_device(devices[i], "", "auto", NULL, -1);
1071 fsck_device(fs /*, interactive */);
1074 || (max_running && (num_running >= max_running))
1076 int exit_status = wait_one(0);
1077 if (exit_status >= 0)
1078 status |= exit_status;
1080 puts("----------------------------------");
1083 status |= wait_many(FLAG_WAIT_ALL);