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 source tree.
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.
37 //usage:#define fsck_trivial_usage
38 //usage: "[-ANPRTV] [-C FD] [-t FSTYPE] [FS_OPTS] [BLOCKDEV]..."
39 //usage:#define fsck_full_usage "\n\n"
40 //usage: "Check and repair filesystems\n"
42 //usage: "\n -A Walk /etc/fstab and check all filesystems"
43 //usage: "\n -N Don't execute, just show what would be done"
44 //usage: "\n -P With -A, check filesystems in parallel"
45 //usage: "\n -R With -A, skip the root filesystem"
46 //usage: "\n -T Don't show title on startup"
47 //usage: "\n -V Verbose"
48 //usage: "\n -C n Write status information to specified filedescriptor"
49 //usage: "\n -t TYPE List of filesystem types to check"
53 /* "progress indicator" code is somewhat buggy and ext[23] specific.
54 * We should be filesystem agnostic. IOW: there should be a well-defined
55 * API for fsck.something, NOT ad-hoc hacks in generic fsck. */
56 #define DO_PROGRESS_INDICATOR 0
58 /* fsck 1.41.4 (27-Jan-2009) manpage says:
60 * 1 - File system errors corrected
61 * 2 - System should be rebooted
62 * 4 - File system errors left uncorrected
63 * 8 - Operational error
64 * 16 - Usage or syntax error
65 * 32 - Fsck canceled by user request
66 * 128 - Shared library error
69 #define EXIT_NONDESTRUCT 1
70 #define EXIT_DESTRUCT 2
71 #define EXIT_UNCORRECTED 4
74 #define FSCK_CANCELED 32 /* Aborted with a signal or ^C */
77 * Internal structure for mount table entries.
90 #define FLAG_PROGRESS 2
92 * Structure to allow exit codes to be stored
94 struct fsck_instance {
95 struct fsck_instance *next;
98 #if DO_PROGRESS_INDICATOR
103 char *base_device; /* /dev/hda for /dev/hdaN etc */
106 static const char ignored_types[] ALIGN1 =
117 static const char really_wanted[] ALIGN1 =
127 #define BASE_MD "/dev/md"
133 #define FS_TYPE_FLAG_NORMAL 0
134 #define FS_TYPE_FLAG_OPT 1
135 #define FS_TYPE_FLAG_NEGOPT 2
136 static char **fs_type_list;
137 static uint8_t *fs_type_flag;
138 static smallint fs_type_negated;
140 static smallint noexecute;
141 static smallint serialize;
142 static smallint skip_root;
143 /* static smallint like_mount; */
144 static smallint parallel_root;
145 static smallint force_all_parallel;
147 #if DO_PROGRESS_INDICATOR
148 static smallint progress;
149 static int progress_fd;
152 static int num_running;
153 static int max_running;
155 static struct fs_info *filesys_info;
156 static struct fs_info *filesys_last;
157 static struct fsck_instance *instance_list;
160 * Return the "base device" given a particular device; this is used to
161 * assure that we only fsck one partition on a particular drive at any
162 * one time. Otherwise, the disk heads will be seeking all over the
163 * place. If the base device cannot be determined, return NULL.
165 * The base_device() function returns an allocated string which must
168 #if ENABLE_FEATURE_DEVFS
170 * Required for the uber-silly devfs /dev/ide/host1/bus2/target3/lun3
173 static const char *const devfs_hier[] = {
174 "host", "bus", "target", "lun", NULL
178 static char *base_device(const char *device)
181 #if ENABLE_FEATURE_DEVFS
182 const char *const *hier;
186 str = xstrdup(device);
188 /* Skip over "/dev/"; if it's not present, give up */
189 cp = skip_dev_pfx(str);
194 * For md devices, we treat them all as if they were all
195 * on one disk, since we don't know how to parallelize them.
197 if (cp[0] == 'm' && cp[1] == 'd') {
202 /* Handle DAC 960 devices */
203 if (strncmp(cp, "rd/", 3) == 0) {
205 if (cp[0] != 'c' || !isdigit(cp[1])
206 || cp[2] != 'd' || !isdigit(cp[3]))
212 /* Now let's handle /dev/hd* and /dev/sd* devices.... */
213 if ((cp[0] == 'h' || cp[0] == 's') && cp[1] == 'd') {
215 /* If there's a single number after /dev/hd, skip it */
218 /* What follows must be an alpha char, or give up */
225 #if ENABLE_FEATURE_DEVFS
226 /* Now let's handle devfs (ugh) names */
228 if (strncmp(cp, "ide/", 4) == 0)
230 if (strncmp(cp, "scsi/", 5) == 0)
235 * Now we proceed down the expected devfs hierarchy.
236 * i.e., .../host1/bus2/target3/lun4/...
237 * If we don't find the expected token, followed by
238 * some number of digits at each level, abort.
240 for (hier = devfs_hier; *hier; hier++) {
242 if (strncmp(cp, *hier, len) != 0)
245 while (*cp != '/' && *cp != 0) {
256 /* Now handle devfs /dev/disc or /dev/disk names */
258 if (strncmp(cp, "discs/", 6) == 0)
260 else if (strncmp(cp, "disks/", 6) == 0)
264 if (strncmp(cp, disk, 4) != 0)
267 while (*cp != '/' && *cp != 0) {
281 static void free_instance(struct fsck_instance *p)
285 free(p->base_device);
289 static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
290 const char *type, const char *opts,
295 fs = xzalloc(sizeof(*fs));
296 fs->device = xstrdup(device);
297 fs->mountpt = xstrdup(mntpnt);
298 if (strchr(type, ','))
299 type = (char *)"auto";
300 fs->type = xstrdup(type);
301 fs->opts = xstrdup(opts ? opts : "");
302 fs->passno = passno < 0 ? 1 : passno;
304 /*fs->next = NULL; */
309 filesys_last->next = fs;
315 /* Load the filesystem database from /etc/fstab */
316 static void load_fs_info(const char *filename)
322 fstab = setmntent(filename, "r");
324 bb_perror_msg("can't read '%s'", filename);
328 // Loop through entries
329 while (getmntent_r(fstab, &mte, bb_common_bufsiz1, COMMON_BUFSIZE)) {
330 //bb_info_msg("CREATE[%s][%s][%s][%s][%d]", mte.mnt_fsname, mte.mnt_dir,
331 // mte.mnt_type, mte.mnt_opts,
333 fs = create_fs_device(mte.mnt_fsname, mte.mnt_dir,
334 mte.mnt_type, mte.mnt_opts,
340 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
341 static struct fs_info *lookup(char *filesys)
345 for (fs = filesys_info; fs; fs = fs->next) {
346 if (strcmp(filesys, fs->device) == 0
347 || (fs->mountpt && strcmp(filesys, fs->mountpt) == 0)
355 #if DO_PROGRESS_INDICATOR
356 static int progress_active(void)
358 struct fsck_instance *inst;
360 for (inst = instance_list; inst; inst = inst->next) {
361 if (inst->flags & FLAG_DONE)
363 if (inst->flags & FLAG_PROGRESS)
372 * Send a signal to all outstanding fsck child processes
374 static void kill_all_if_got_signal(void)
376 static smallint kill_sent;
378 struct fsck_instance *inst;
380 if (!bb_got_signal || kill_sent)
383 for (inst = instance_list; inst; inst = inst->next) {
384 if (inst->flags & FLAG_DONE)
386 kill(inst->pid, SIGTERM);
392 * Wait for one child process to exit; when it does, unlink it from
393 * the list of executing child processes, free, and return its exit status.
394 * If there is no exited child, return -1.
396 static int wait_one(int flags)
400 struct fsck_instance *inst, *prev;
405 /* if (noexecute) { already returned -1; } */
408 pid = waitpid(-1, &status, flags);
409 kill_all_if_got_signal();
410 if (pid == 0) /* flags == WNOHANG and no children exited */
415 if (errno == ECHILD) { /* paranoia */
416 bb_error_msg("wait: no more children");
419 bb_perror_msg("wait");
423 inst = instance_list;
425 if (inst->pid == pid)
433 if (WIFEXITED(status))
434 status = WEXITSTATUS(status);
435 else if (WIFSIGNALED(status)) {
436 sig = WTERMSIG(status);
437 status = EXIT_UNCORRECTED;
439 printf("Warning: %s %s terminated "
441 inst->prog, inst->device, sig);
445 printf("%s %s: status is %x, should never happen\n",
446 inst->prog, inst->device, status);
450 #if DO_PROGRESS_INDICATOR
451 if (progress && (inst->flags & FLAG_PROGRESS) && !progress_active()) {
452 struct fsck_instance *inst2;
453 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
454 if (inst2->flags & FLAG_DONE)
456 if (strcmp(inst2->type, "ext2") != 0
457 && strcmp(inst2->type, "ext3") != 0
461 /* ext[23], we will send USR1
462 * (request to start displaying progress bar)
464 * If we've just started the fsck, wait a tiny
465 * bit before sending the kill, to give it
466 * time to set up the signal handler
468 if (inst2->start_time >= time(NULL) - 1)
470 kill(inst2->pid, SIGUSR1);
471 inst2->flags |= FLAG_PROGRESS;
478 prev->next = inst->next;
480 instance_list = inst->next;
482 printf("Finished with %s (exit status %d)\n",
483 inst->device, status);
491 * Wait until all executing child processes have exited; return the
492 * logical OR of all of their exit code values.
494 #define FLAG_WAIT_ALL 0
495 #define FLAG_WAIT_ATLEAST_ONE WNOHANG
496 static int wait_many(int flags)
499 int global_status = 0;
502 while ((exit_status = wait_one(wait_flags)) != -1) {
503 global_status |= exit_status;
506 return global_status;
510 * Execute a particular fsck program, and link it into the list of
511 * child processes we are waiting for.
513 static void execute(const char *type, const char *device,
514 const char *mntpt /*, int interactive */)
517 struct fsck_instance *inst;
520 args[0] = xasprintf("fsck.%s", type);
522 #if DO_PROGRESS_INDICATOR
523 if (progress && !progress_active()) {
524 if (strcmp(type, "ext2") == 0
525 || strcmp(type, "ext3") == 0
527 args[XXX] = xasprintf("-C%d", progress_fd); /* 1 */
528 inst->flags |= FLAG_PROGRESS;
533 args[num_args - 2] = (char*)device;
534 /* args[num_args - 1] = NULL; - already is */
536 if (verbose || noexecute) {
537 printf("[%s (%d) -- %s]", args[0], num_running,
538 mntpt ? mntpt : device);
539 for (i = 0; args[i]; i++)
540 printf(" %s", args[i]);
544 /* Fork and execute the correct program. */
549 bb_simple_perror_msg(args[0]);
552 #if DO_PROGRESS_INDICATOR
556 /* No child, so don't record an instance */
562 inst = xzalloc(sizeof(*inst));
564 inst->prog = args[0];
565 inst->device = xstrdup(device);
566 inst->base_device = base_device(device);
567 #if DO_PROGRESS_INDICATOR
568 inst->start_time = time(NULL);
571 /* Add to the list of running fsck's.
572 * (was adding to the end, but adding to the front is simpler...) */
573 inst->next = instance_list;
574 instance_list = inst;
578 * Run the fsck program on a particular device
580 * If the type is specified using -t, and it isn't prefixed with "no"
581 * (as in "noext2") and only one filesystem type is specified, then
582 * use that type regardless of what is specified in /etc/fstab.
584 * If the type isn't specified by the user, then use either the type
585 * specified in /etc/fstab, or "auto".
587 static void fsck_device(struct fs_info *fs /*, int interactive */)
591 if (strcmp(fs->type, "auto") != 0) {
594 bb_info_msg("using filesystem type '%s' %s",
597 && (fstype[0] != 'n' || fstype[1] != 'o') /* != "no" */
598 && strncmp(fstype, "opts=", 5) != 0
599 && strncmp(fstype, "loop", 4) != 0
600 && !strchr(fstype, ',')
604 bb_info_msg("using filesystem type '%s' %s",
609 bb_info_msg("using filesystem type '%s' %s",
614 execute(type, fs->device, fs->mountpt /*, interactive */);
618 * Returns TRUE if a partition on the same disk is already being
621 static int device_already_active(char *device)
623 struct fsck_instance *inst;
626 if (force_all_parallel)
630 /* Don't check a soft raid disk with any other disk */
632 && (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1)
633 || !strncmp(device, BASE_MD, sizeof(BASE_MD)-1))
639 base = base_device(device);
641 * If we don't know the base device, assume that the device is
642 * already active if there are any fsck instances running.
645 return (instance_list != NULL);
647 for (inst = instance_list; inst; inst = inst->next) {
648 if (!inst->base_device || !strcmp(base, inst->base_device)) {
659 * This function returns true if a particular option appears in a
660 * comma-delimited options list
662 static int opt_in_list(char *opt, char *optlist)
673 s = strstr(s + 1, opt);
676 /* neither "opt.." nor "xxx,opt.."? */
677 if (s != optlist && s[-1] != ',')
679 /* neither "..opt" nor "..opt,xxx"? */
680 if (s[len] != '\0' && s[len] != ',')
686 /* See if the filesystem matches the criteria given by the -t option */
687 static int fs_match(struct fs_info *fs)
689 int n, ret, checked_type;
699 cp = fs_type_list[n];
702 switch (fs_type_flag[n]) {
703 case FS_TYPE_FLAG_NORMAL:
705 if (strcmp(cp, fs->type) == 0)
708 case FS_TYPE_FLAG_NEGOPT:
709 if (opt_in_list(cp, fs->opts))
712 case FS_TYPE_FLAG_OPT:
713 if (!opt_in_list(cp, fs->opts))
719 if (checked_type == 0)
722 return (fs_type_negated ? !ret : ret);
725 /* Check if we should ignore this filesystem. */
726 static int ignore(struct fs_info *fs)
729 * If the pass number is 0, ignore it.
735 * If a specific fstype is specified, and it doesn't match,
741 /* Are we ignoring this type? */
742 if (index_in_strings(ignored_types, fs->type) >= 0)
745 /* We can and want to check this file system type. */
749 /* Check all file systems, using the /etc/fstab table. */
750 static int check_all(void)
753 int status = EXIT_OK;
754 smallint not_done_yet;
759 puts("Checking all filesystems");
762 * Do an initial scan over the filesystem; mark filesystems
763 * which should be ignored as done, and resolve any "auto"
764 * filesystem types (done as a side-effect of calling ignore()).
766 for (fs = filesys_info; fs; fs = fs->next)
768 fs->flags |= FLAG_DONE;
771 * Find and check the root filesystem.
773 if (!parallel_root) {
774 for (fs = filesys_info; fs; fs = fs->next) {
775 if (LONE_CHAR(fs->mountpt, '/')) {
776 if (!skip_root && !ignore(fs)) {
777 fsck_device(fs /*, 1*/);
778 status |= wait_many(FLAG_WAIT_ALL);
779 if (status > EXIT_NONDESTRUCT)
782 fs->flags |= FLAG_DONE;
788 * This is for the bone-headed user who has root
789 * filesystem listed twice.
790 * "Skip root" will skip _all_ root entries.
793 for (fs = filesys_info; fs; fs = fs->next)
794 if (LONE_CHAR(fs->mountpt, '/'))
795 fs->flags |= FLAG_DONE;
799 while (not_done_yet) {
803 for (fs = filesys_info; fs; fs = fs->next) {
806 if (fs->flags & FLAG_DONE)
809 * If the filesystem's pass number is higher
810 * than the current pass number, then we didn't
813 if (fs->passno > passno) {
818 * If a filesystem on a particular device has
819 * already been spawned, then we need to defer
820 * this to another pass.
822 if (device_already_active(fs->device)) {
827 * Spawn off the fsck process
829 fsck_device(fs /*, serialize*/);
830 fs->flags |= FLAG_DONE;
833 * Only do one filesystem at a time, or if we
834 * have a limit on the number of fsck's extant
835 * at one time, apply that limit.
838 || (max_running && (num_running >= max_running))
847 printf("--waiting-- (pass %d)\n", passno);
848 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
849 FLAG_WAIT_ATLEAST_ONE);
852 puts("----------------------------------");
857 kill_all_if_got_signal();
858 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
863 * Deal with the fsck -t argument.
864 * Huh, for mount "-t novfat,nfs" means "neither vfat nor nfs"!
865 * Why here we require "-t novfat,nonfs" ??
867 static void compile_fs_type(char *fs_type)
874 while ((s = strchr(s, ','))) {
879 fs_type_list = xzalloc(num * sizeof(fs_type_list[0]));
880 fs_type_flag = xzalloc(num * sizeof(fs_type_flag[0]));
881 fs_type_negated = -1; /* not yet known is it negated or not */
889 if (s[0] == 'n' && s[1] == 'o') { /* "no.." */
892 } else if (s[0] == '!') {
897 if (strcmp(s, "loop") == 0)
898 /* loop is really short-hand for opts=loop */
899 goto loop_special_case;
900 if (strncmp(s, "opts=", 5) == 0) {
903 fs_type_flag[num] = negate ? FS_TYPE_FLAG_NEGOPT : FS_TYPE_FLAG_OPT;
905 if (fs_type_negated == -1)
906 fs_type_negated = negate;
907 if (fs_type_negated != negate)
908 bb_error_msg_and_die(
909 "either all or none of the filesystem types passed to -t must be prefixed "
912 comma = strchr(s, ',');
913 fs_type_list[num++] = comma ? xstrndup(s, comma-s) : xstrdup(s);
920 static char **new_args(void)
922 args = xrealloc_vector(args, 2, num_args);
923 return &args[num_args++];
926 int fsck_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
927 int fsck_main(int argc UNUSED_PARAM, char **argv)
936 smallint opts_for_fsck;
940 /* we want wait() to be interruptible */
941 signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
942 signal_no_SA_RESTART_empty_mask(SIGTERM, record_signo);
944 setbuf(stdout, NULL);
946 opts_for_fsck = doall = notitle = 0;
949 new_args(); /* args[0] = NULL, will be replaced by fsck.<type> */
950 /* instance_list = NULL; - in bss, so already zeroed */
958 /* "/dev/blk" or "/path" or "UUID=xxx" or "LABEL=xxx" */
959 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
960 // FIXME: must check that arg is a blkdev, or resolve
961 // "/path", "UUID=xxx" or "LABEL=xxx" into block device name
962 // ("UUID=xxx"/"LABEL=xxx" can probably shifted to fsck.auto duties)
963 devices = xrealloc_vector(devices, 2, num_devices);
964 devices[num_devices++] = arg;
968 if (arg[0] != '-' || opts_for_fsck) {
973 if (LONE_CHAR(arg + 1, '-')) { /* "--" ? */
980 for (j = 1; arg[j]; j++) {
985 #if DO_PROGRESS_INDICATOR
988 if (arg[++j]) { /* -Cn */
989 progress_fd = xatoi_positive(&arg[j]);
995 progress_fd = xatoi_positive(*argv);
1028 fstype = xstrdup(tmp);
1029 compile_fs_type(fstype);
1036 /* one extra for '\0' */
1037 options = xrealloc(options, optpos + 2);
1038 options[optpos] = arg[j];
1045 options[optpos + 1] = '\0';
1046 *new_args() = options;
1049 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1050 force_all_parallel = 1;
1051 tmp = getenv("FSCK_MAX_INST");
1053 max_running = xatoi(tmp);
1054 new_args(); /* args[num_args - 2] will be replaced by <device> */
1055 new_args(); /* args[num_args - 1] is the last, NULL element */
1058 puts("fsck (busybox "BB_VER", "BB_BT")");
1060 /* Even plain "fsck /dev/hda1" needs fstab to get fs type,
1061 * so we are scanning it anyway */
1062 fstab = getenv("FSTAB_FILE");
1064 fstab = "/etc/fstab";
1065 load_fs_info(fstab);
1067 /*interactive = (num_devices == 1) | serialize;*/
1069 if (num_devices == 0)
1070 /*interactive =*/ serialize = doall = 1;
1075 for (i = 0; i < num_devices; i++) {
1076 if (bb_got_signal) {
1077 kill_all_if_got_signal();
1081 fs = lookup(devices[i]);
1083 fs = create_fs_device(devices[i], "", "auto", NULL, -1);
1084 fsck_device(fs /*, interactive */);
1087 || (max_running && (num_running >= max_running))
1089 int exit_status = wait_one(0);
1090 if (exit_status >= 0)
1091 status |= exit_status;
1093 puts("----------------------------------");
1096 status |= wait_many(FLAG_WAIT_ALL);