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
44 /* fsck 1.41.4 (27-Jan-2009) manpage says:
46 * 1 - File system errors corrected
47 * 2 - System should be rebooted
48 * 4 - File system errors left uncorrected
49 * 8 - Operational error
50 * 16 - Usage or syntax error
51 * 32 - Fsck canceled by user request
52 * 128 - Shared library error
55 #define EXIT_NONDESTRUCT 1
56 #define EXIT_DESTRUCT 2
57 #define EXIT_UNCORRECTED 4
60 #define FSCK_CANCELED 32 /* Aborted with a signal or ^C */
63 * Internal structure for mount table entries.
76 #define FLAG_PROGRESS 2
78 * Structure to allow exit codes to be stored
80 struct fsck_instance {
81 struct fsck_instance *next;
84 #if DO_PROGRESS_INDICATOR
89 char *base_device; /* /dev/hda for /dev/hdaN etc */
92 static const char ignored_types[] ALIGN1 =
103 static const char really_wanted[] ALIGN1 =
113 #define BASE_MD "/dev/md"
119 #define FS_TYPE_FLAG_NORMAL 0
120 #define FS_TYPE_FLAG_OPT 1
121 #define FS_TYPE_FLAG_NEGOPT 2
122 static char **fs_type_list;
123 static uint8_t *fs_type_flag;
124 static smallint fs_type_negated;
126 static smallint noexecute;
127 static smallint serialize;
128 static smallint skip_root;
129 /* static smallint like_mount; */
130 static smallint parallel_root;
131 static smallint force_all_parallel;
133 #if DO_PROGRESS_INDICATOR
134 static smallint progress;
135 static int progress_fd;
138 static int num_running;
139 static int max_running;
141 static struct fs_info *filesys_info;
142 static struct fs_info *filesys_last;
143 static struct fsck_instance *instance_list;
146 * Return the "base device" given a particular device; this is used to
147 * assure that we only fsck one partition on a particular drive at any
148 * one time. Otherwise, the disk heads will be seeking all over the
149 * place. If the base device cannot be determined, return NULL.
151 * The base_device() function returns an allocated string which must
154 #if ENABLE_FEATURE_DEVFS
156 * Required for the uber-silly devfs /dev/ide/host1/bus2/target3/lun3
159 static const char *const devfs_hier[] = {
160 "host", "bus", "target", "lun", NULL
164 static char *base_device(const char *device)
167 #if ENABLE_FEATURE_DEVFS
168 const char *const *hier;
172 cp = str = xstrdup(device);
174 /* Skip over /dev/; if it's not present, give up. */
175 if (strncmp(cp, "/dev/", 5) != 0)
180 * For md devices, we treat them all as if they were all
181 * on one disk, since we don't know how to parallelize them.
183 if (cp[0] == 'm' && cp[1] == 'd') {
188 /* Handle DAC 960 devices */
189 if (strncmp(cp, "rd/", 3) == 0) {
191 if (cp[0] != 'c' || !isdigit(cp[1])
192 || cp[2] != 'd' || !isdigit(cp[3]))
198 /* Now let's handle /dev/hd* and /dev/sd* devices.... */
199 if ((cp[0] == 'h' || cp[0] == 's') && cp[1] == 'd') {
201 /* If there's a single number after /dev/hd, skip it */
204 /* What follows must be an alpha char, or give up */
211 #if ENABLE_FEATURE_DEVFS
212 /* Now let's handle devfs (ugh) names */
214 if (strncmp(cp, "ide/", 4) == 0)
216 if (strncmp(cp, "scsi/", 5) == 0)
221 * Now we proceed down the expected devfs hierarchy.
222 * i.e., .../host1/bus2/target3/lun4/...
223 * If we don't find the expected token, followed by
224 * some number of digits at each level, abort.
226 for (hier = devfs_hier; *hier; hier++) {
228 if (strncmp(cp, *hier, len) != 0)
231 while (*cp != '/' && *cp != 0) {
242 /* Now handle devfs /dev/disc or /dev/disk names */
244 if (strncmp(cp, "discs/", 6) == 0)
246 else if (strncmp(cp, "disks/", 6) == 0)
250 if (strncmp(cp, disk, 4) != 0)
253 while (*cp != '/' && *cp != 0) {
267 static void free_instance(struct fsck_instance *p)
271 free(p->base_device);
275 static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
276 const char *type, const char *opts,
281 fs = xzalloc(sizeof(*fs));
282 fs->device = xstrdup(device);
283 fs->mountpt = xstrdup(mntpnt);
284 if (strchr(type, ','))
285 type = (char *)"auto";
286 fs->type = xstrdup(type);
287 fs->opts = xstrdup(opts ? opts : "");
288 fs->passno = passno < 0 ? 1 : passno;
290 /*fs->next = NULL; */
295 filesys_last->next = fs;
301 /* Load the filesystem database from /etc/fstab */
302 static void load_fs_info(const char *filename)
308 fstab = setmntent(filename, "r");
310 bb_perror_msg("can't read %s", filename);
314 // Loop through entries
315 while (getmntent_r(fstab, &mte, bb_common_bufsiz1, COMMON_BUFSIZE)) {
316 //bb_info_msg("CREATE[%s][%s][%s][%s][%d]", mte.mnt_fsname, mte.mnt_dir,
317 // mte.mnt_type, mte.mnt_opts,
319 fs = create_fs_device(mte.mnt_fsname, mte.mnt_dir,
320 mte.mnt_type, mte.mnt_opts,
326 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
327 static struct fs_info *lookup(char *filesys)
331 for (fs = filesys_info; fs; fs = fs->next) {
332 if (strcmp(filesys, fs->device) == 0
333 || (fs->mountpt && strcmp(filesys, fs->mountpt) == 0)
341 #if DO_PROGRESS_INDICATOR
342 static int progress_active(void)
344 struct fsck_instance *inst;
346 for (inst = instance_list; inst; inst = inst->next) {
347 if (inst->flags & FLAG_DONE)
349 if (inst->flags & FLAG_PROGRESS)
358 * Send a signal to all outstanding fsck child processes
360 static void kill_all_if_got_signal(void)
362 static smallint kill_sent;
364 struct fsck_instance *inst;
366 if (!bb_got_signal || kill_sent)
369 for (inst = instance_list; inst; inst = inst->next) {
370 if (inst->flags & FLAG_DONE)
372 kill(inst->pid, SIGTERM);
378 * Wait for one child process to exit; when it does, unlink it from
379 * the list of executing child processes, free, and return its exit status.
380 * If there is no exited child, return -1.
382 static int wait_one(int flags)
386 struct fsck_instance *inst, *prev;
391 /* if (noexecute) { already returned -1; } */
394 pid = waitpid(-1, &status, flags);
395 kill_all_if_got_signal();
396 if (pid == 0) /* flags == WNOHANG and no children exited */
401 if (errno == ECHILD) { /* paranoia */
402 bb_error_msg("wait: no more children");
405 bb_perror_msg("wait");
409 inst = instance_list;
411 if (inst->pid == pid)
419 if (WIFEXITED(status))
420 status = WEXITSTATUS(status);
421 else if (WIFSIGNALED(status)) {
422 sig = WTERMSIG(status);
423 status = EXIT_UNCORRECTED;
425 printf("Warning: %s %s terminated "
427 inst->prog, inst->device, sig);
431 printf("%s %s: status is %x, should never happen\n",
432 inst->prog, inst->device, status);
436 #if DO_PROGRESS_INDICATOR
437 if (progress && (inst->flags & FLAG_PROGRESS) && !progress_active()) {
438 struct fsck_instance *inst2;
439 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
440 if (inst2->flags & FLAG_DONE)
442 if (strcmp(inst2->type, "ext2") != 0
443 && strcmp(inst2->type, "ext3") != 0
447 /* ext[23], we will send USR1
448 * (request to start displaying progress bar)
450 * If we've just started the fsck, wait a tiny
451 * bit before sending the kill, to give it
452 * time to set up the signal handler
454 if (inst2->start_time >= time(NULL) - 1)
456 kill(inst2->pid, SIGUSR1);
457 inst2->flags |= FLAG_PROGRESS;
464 prev->next = inst->next;
466 instance_list = inst->next;
468 printf("Finished with %s (exit status %d)\n",
469 inst->device, status);
477 * Wait until all executing child processes have exited; return the
478 * logical OR of all of their exit code values.
480 #define FLAG_WAIT_ALL 0
481 #define FLAG_WAIT_ATLEAST_ONE WNOHANG
482 static int wait_many(int flags)
485 int global_status = 0;
488 while ((exit_status = wait_one(wait_flags)) != -1) {
489 global_status |= exit_status;
492 return global_status;
496 * Execute a particular fsck program, and link it into the list of
497 * child processes we are waiting for.
499 static void execute(const char *type, const char *device,
500 const char *mntpt /*, int interactive */)
503 struct fsck_instance *inst;
506 args[0] = xasprintf("fsck.%s", type);
508 #if DO_PROGRESS_INDICATOR
509 if (progress && !progress_active()) {
510 if (strcmp(type, "ext2") == 0
511 || strcmp(type, "ext3") == 0
513 args[XXX] = xasprintf("-C%d", progress_fd); /* 1 */
514 inst->flags |= FLAG_PROGRESS;
519 args[num_args - 2] = (char*)device;
520 /* args[num_args - 1] = NULL; - already is */
522 if (verbose || noexecute) {
523 printf("[%s (%d) -- %s]", args[0], num_running,
524 mntpt ? mntpt : device);
525 for (i = 0; args[i]; i++)
526 printf(" %s", args[i]);
530 /* Fork and execute the correct program. */
535 bb_simple_perror_msg(args[0]);
538 #if DO_PROGRESS_INDICATOR
542 /* No child, so don't record an instance */
548 inst = xzalloc(sizeof(*inst));
550 inst->prog = args[0];
551 inst->device = xstrdup(device);
552 inst->base_device = base_device(device);
553 #if DO_PROGRESS_INDICATOR
554 inst->start_time = time(NULL);
557 /* Add to the list of running fsck's.
558 * (was adding to the end, but adding to the front is simpler...) */
559 inst->next = instance_list;
560 instance_list = inst;
564 * Run the fsck program on a particular device
566 * If the type is specified using -t, and it isn't prefixed with "no"
567 * (as in "noext2") and only one filesystem type is specified, then
568 * use that type regardless of what is specified in /etc/fstab.
570 * If the type isn't specified by the user, then use either the type
571 * specified in /etc/fstab, or "auto".
573 static void fsck_device(struct fs_info *fs /*, int interactive */)
577 if (strcmp(fs->type, "auto") != 0) {
580 bb_info_msg("using filesystem type '%s' %s",
583 && (fstype[0] != 'n' || fstype[1] != 'o') /* != "no" */
584 && strncmp(fstype, "opts=", 5) != 0
585 && strncmp(fstype, "loop", 4) != 0
586 && !strchr(fstype, ',')
590 bb_info_msg("using filesystem type '%s' %s",
595 bb_info_msg("using filesystem type '%s' %s",
600 execute(type, fs->device, fs->mountpt /*, interactive */);
604 * Returns TRUE if a partition on the same disk is already being
607 static int device_already_active(char *device)
609 struct fsck_instance *inst;
612 if (force_all_parallel)
616 /* Don't check a soft raid disk with any other disk */
618 && (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1)
619 || !strncmp(device, BASE_MD, sizeof(BASE_MD)-1))
625 base = base_device(device);
627 * If we don't know the base device, assume that the device is
628 * already active if there are any fsck instances running.
631 return (instance_list != NULL);
633 for (inst = instance_list; inst; inst = inst->next) {
634 if (!inst->base_device || !strcmp(base, inst->base_device)) {
645 * This function returns true if a particular option appears in a
646 * comma-delimited options list
648 static int opt_in_list(char *opt, char *optlist)
659 s = strstr(s + 1, opt);
662 /* neither "opt.." nor "xxx,opt.."? */
663 if (s != optlist && s[-1] != ',')
665 /* neither "..opt" nor "..opt,xxx"? */
666 if (s[len] != '\0' && s[len] != ',')
672 /* See if the filesystem matches the criteria given by the -t option */
673 static int fs_match(struct fs_info *fs)
675 int n, ret, checked_type;
685 cp = fs_type_list[n];
688 switch (fs_type_flag[n]) {
689 case FS_TYPE_FLAG_NORMAL:
691 if (strcmp(cp, fs->type) == 0)
694 case FS_TYPE_FLAG_NEGOPT:
695 if (opt_in_list(cp, fs->opts))
698 case FS_TYPE_FLAG_OPT:
699 if (!opt_in_list(cp, fs->opts))
705 if (checked_type == 0)
708 return (fs_type_negated ? !ret : ret);
711 /* Check if we should ignore this filesystem. */
712 static int ignore(struct fs_info *fs)
715 * If the pass number is 0, ignore it.
721 * If a specific fstype is specified, and it doesn't match,
727 /* Are we ignoring this type? */
728 if (index_in_strings(ignored_types, fs->type) >= 0)
731 /* We can and want to check this file system type. */
735 /* Check all file systems, using the /etc/fstab table. */
736 static int check_all(void)
739 int status = EXIT_OK;
740 smallint not_done_yet;
745 puts("Checking all filesystems");
748 * Do an initial scan over the filesystem; mark filesystems
749 * which should be ignored as done, and resolve any "auto"
750 * filesystem types (done as a side-effect of calling ignore()).
752 for (fs = filesys_info; fs; fs = fs->next)
754 fs->flags |= FLAG_DONE;
757 * Find and check the root filesystem.
759 if (!parallel_root) {
760 for (fs = filesys_info; fs; fs = fs->next) {
761 if (LONE_CHAR(fs->mountpt, '/')) {
762 if (!skip_root && !ignore(fs)) {
763 fsck_device(fs /*, 1*/);
764 status |= wait_many(FLAG_WAIT_ALL);
765 if (status > EXIT_NONDESTRUCT)
768 fs->flags |= FLAG_DONE;
774 * This is for the bone-headed user who has root
775 * filesystem listed twice.
776 * "Skip root" will skip _all_ root entries.
779 for (fs = filesys_info; fs; fs = fs->next)
780 if (LONE_CHAR(fs->mountpt, '/'))
781 fs->flags |= FLAG_DONE;
785 while (not_done_yet) {
789 for (fs = filesys_info; fs; fs = fs->next) {
792 if (fs->flags & FLAG_DONE)
795 * If the filesystem's pass number is higher
796 * than the current pass number, then we didn't
799 if (fs->passno > passno) {
804 * If a filesystem on a particular device has
805 * already been spawned, then we need to defer
806 * this to another pass.
808 if (device_already_active(fs->device)) {
813 * Spawn off the fsck process
815 fsck_device(fs /*, serialize*/);
816 fs->flags |= FLAG_DONE;
819 * Only do one filesystem at a time, or if we
820 * have a limit on the number of fsck's extant
821 * at one time, apply that limit.
824 || (max_running && (num_running >= max_running))
833 printf("--waiting-- (pass %d)\n", passno);
834 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
835 FLAG_WAIT_ATLEAST_ONE);
838 puts("----------------------------------");
843 kill_all_if_got_signal();
844 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
849 * Deal with the fsck -t argument.
850 * Huh, for mount "-t novfat,nfs" means "neither vfat nor nfs"!
851 * Why here we require "-t novfat,nonfs" ??
853 static void compile_fs_type(char *fs_type)
860 while ((s = strchr(s, ','))) {
865 fs_type_list = xzalloc(num * sizeof(fs_type_list[0]));
866 fs_type_flag = xzalloc(num * sizeof(fs_type_flag[0]));
867 fs_type_negated = -1; /* not yet known is it negated or not */
875 if (s[0] == 'n' && s[1] == 'o') { /* "no.." */
878 } else if (s[0] == '!') {
883 if (strcmp(s, "loop") == 0)
884 /* loop is really short-hand for opts=loop */
885 goto loop_special_case;
886 if (strncmp(s, "opts=", 5) == 0) {
889 fs_type_flag[num] = negate ? FS_TYPE_FLAG_NEGOPT : FS_TYPE_FLAG_OPT;
891 if (fs_type_negated == -1)
892 fs_type_negated = negate;
893 if (fs_type_negated != negate)
894 bb_error_msg_and_die(
895 "either all or none of the filesystem types passed to -t must be prefixed "
898 comma = strchr(s, ',');
899 fs_type_list[num++] = comma ? xstrndup(s, comma-s) : xstrdup(s);
906 static char **new_args(void)
908 args = xrealloc_vector(args, 2, num_args);
909 return &args[num_args++];
912 int fsck_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
913 int fsck_main(int argc UNUSED_PARAM, char **argv)
922 smallint opts_for_fsck;
926 /* we want wait() to be interruptible */
927 signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
928 signal_no_SA_RESTART_empty_mask(SIGTERM, record_signo);
930 setbuf(stdout, NULL);
932 opts_for_fsck = doall = notitle = 0;
935 new_args(); /* args[0] = NULL, will be replaced by fsck.<type> */
936 /* instance_list = NULL; - in bss, so already zeroed */
944 /* "/dev/blk" or "/path" or "UUID=xxx" or "LABEL=xxx" */
945 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
946 // FIXME: must check that arg is a blkdev, or resolve
947 // "/path", "UUID=xxx" or "LABEL=xxx" into block device name
948 // ("UUID=xxx"/"LABEL=xxx" can probably shifted to fsck.auto duties)
949 devices = xrealloc_vector(devices, 2, num_devices);
950 devices[num_devices++] = arg;
954 if (arg[0] != '-' || opts_for_fsck) {
959 if (LONE_CHAR(arg + 1, '-')) { /* "--" ? */
966 for (j = 1; arg[j]; j++) {
971 #if DO_PROGRESS_INDICATOR
974 if (arg[++j]) { /* -Cn */
975 progress_fd = xatoi_u(&arg[j]);
981 progress_fd = xatoi_u(*argv);
1014 fstype = xstrdup(tmp);
1015 compile_fs_type(fstype);
1022 /* one extra for '\0' */
1023 options = xrealloc(options, optpos + 2);
1024 options[optpos] = arg[j];
1031 options[optpos + 1] = '\0';
1032 *new_args() = options;
1035 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1036 force_all_parallel = 1;
1037 tmp = getenv("FSCK_MAX_INST");
1039 max_running = xatoi(tmp);
1040 new_args(); /* args[num_args - 2] will be replaced by <device> */
1041 new_args(); /* args[num_args - 1] is the last, NULL element */
1044 puts("fsck (busybox "BB_VER", "BB_BT")");
1046 /* Even plain "fsck /dev/hda1" needs fstab to get fs type,
1047 * so we are scanning it anyway */
1048 fstab = getenv("FSTAB_FILE");
1050 fstab = "/etc/fstab";
1051 load_fs_info(fstab);
1053 /*interactive = (num_devices == 1) | serialize;*/
1055 if (num_devices == 0)
1056 /*interactive =*/ serialize = doall = 1;
1061 for (i = 0; i < num_devices; i++) {
1062 if (bb_got_signal) {
1063 kill_all_if_got_signal();
1067 fs = lookup(devices[i]);
1069 fs = create_fs_device(devices[i], "", "auto", NULL, -1);
1070 fsck_device(fs /*, interactive */);
1073 || (max_running && (num_running >= max_running))
1075 int exit_status = wait_one(0);
1076 if (exit_status >= 0)
1077 status |= exit_status;
1079 puts("----------------------------------");
1082 status |= wait_many(FLAG_WAIT_ALL);