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.
40 //config: fsck is used to check and optionally repair one or more filesystems.
41 //config: In actuality, fsck is simply a front-end for the various file system
42 //config: checkers (fsck.fstype) available under Linux.
44 //applet:IF_FSCK(APPLET(fsck, BB_DIR_SBIN, BB_SUID_DROP))
46 //kbuild:lib-$(CONFIG_FSCK) += fsck.o
48 //usage:#define fsck_trivial_usage
49 //usage: "[-ANPRTV] [-t FSTYPE] [FS_OPTS] [BLOCKDEV]..."
50 //usage:#define fsck_full_usage "\n\n"
51 //usage: "Check and repair filesystems\n"
52 //usage: "\n -A Walk /etc/fstab and check all filesystems"
53 //usage: "\n -N Don't execute, just show what would be done"
54 //usage: "\n -P With -A, check filesystems in parallel"
55 //usage: "\n -R With -A, skip the root filesystem"
56 //usage: "\n -T Don't show title on startup"
57 //usage: "\n -V Verbose"
58 //DO_PROGRESS_INDICATOR is off:
59 ////usage: "\n -C FD Write status information to specified file descriptor"
60 //usage: "\n -t TYPE List of filesystem types to check"
64 /* "progress indicator" code is somewhat buggy and ext[23] specific.
65 * We should be filesystem agnostic. IOW: there should be a well-defined
66 * API for fsck.something, NOT ad-hoc hacks in generic fsck. */
67 #define DO_PROGRESS_INDICATOR 0
69 /* fsck 1.41.4 (27-Jan-2009) manpage says:
71 * 1 - File system errors corrected
72 * 2 - System should be rebooted
73 * 4 - File system errors left uncorrected
74 * 8 - Operational error
75 * 16 - Usage or syntax error
76 * 32 - Fsck canceled by user request
77 * 128 - Shared library error
80 #define EXIT_NONDESTRUCT 1
81 #define EXIT_DESTRUCT 2
82 #define EXIT_UNCORRECTED 4
85 #define FSCK_CANCELED 32 /* Aborted with a signal or ^C */
88 * Internal structure for mount table entries.
101 #define FLAG_PROGRESS 2
103 * Structure to allow exit codes to be stored
105 struct fsck_instance {
106 struct fsck_instance *next;
109 #if DO_PROGRESS_INDICATOR
114 char *base_device; /* /dev/hda for /dev/hdaN etc */
117 static const char ignored_types[] ALIGN1 =
128 static const char really_wanted[] ALIGN1 =
138 #define BASE_MD "/dev/md"
145 #define FS_TYPE_FLAG_NORMAL 0
146 #define FS_TYPE_FLAG_OPT 1
147 #define FS_TYPE_FLAG_NEGOPT 2
149 uint8_t *fs_type_flag;
150 smallint fs_type_negated;
155 /* smallint like_mount; */
156 smallint parallel_root;
157 smallint force_all_parallel;
160 #if DO_PROGRESS_INDICATOR
168 struct fs_info *filesys_info;
169 struct fs_info *filesys_last;
170 struct fsck_instance *instance_list;
172 #define G (*(struct globals*)&bb_common_bufsiz1)
173 #define INIT_G() do { \
174 BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
178 * Return the "base device" given a particular device; this is used to
179 * assure that we only fsck one partition on a particular drive at any
180 * one time. Otherwise, the disk heads will be seeking all over the
181 * place. If the base device cannot be determined, return NULL.
183 * The base_device() function returns an allocated string which must
186 #if ENABLE_FEATURE_DEVFS
188 * Required for the uber-silly devfs /dev/ide/host1/bus2/target3/lun3
191 static const char *const devfs_hier[] = {
192 "host", "bus", "target", "lun", NULL
196 static char *base_device(const char *device)
199 #if ENABLE_FEATURE_DEVFS
200 const char *const *hier;
204 str = xstrdup(device);
206 /* Skip over "/dev/"; if it's not present, give up */
207 cp = skip_dev_pfx(str);
212 * For md devices, we treat them all as if they were all
213 * on one disk, since we don't know how to parallelize them.
215 if (cp[0] == 'm' && cp[1] == 'd') {
220 /* Handle DAC 960 devices */
221 if (is_prefixed_with(cp, "rd/")) {
223 if (cp[0] != 'c' || !isdigit(cp[1])
224 || cp[2] != 'd' || !isdigit(cp[3]))
230 /* Now let's handle /dev/hd* and /dev/sd* devices.... */
231 if ((cp[0] == 'h' || cp[0] == 's') && cp[1] == 'd') {
233 /* If there's a single number after /dev/hd, skip it */
236 /* What follows must be an alpha char, or give up */
243 #if ENABLE_FEATURE_DEVFS
244 /* Now let's handle devfs (ugh) names */
246 if (is_prefixed_with(cp, "ide/"))
248 if (is_prefixed_with(cp, "scsi/"))
253 * Now we proceed down the expected devfs hierarchy.
254 * i.e., .../host1/bus2/target3/lun4/...
255 * If we don't find the expected token, followed by
256 * some number of digits at each level, abort.
258 for (hier = devfs_hier; *hier; hier++) {
259 cp = is_prefixed_with(cp, *hier);
262 while (*cp != '/' && *cp != '\0') {
267 //FIXME: what if *cp = '\0' now? cp++ moves past it!!!
274 /* Now handle devfs /dev/disc or /dev/disk names */
276 if (is_prefixed_with(cp, "discs/"))
278 else if (is_prefixed_with(cp, "disks/"))
282 cp = is_prefixed_with(cp, disk);
285 while (*cp != '/' && *cp != '\0') {
299 static void free_instance(struct fsck_instance *p)
303 free(p->base_device);
307 static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
308 const char *type, const char *opts,
313 fs = xzalloc(sizeof(*fs));
314 fs->device = xstrdup(device);
315 fs->mountpt = xstrdup(mntpnt);
316 if (strchr(type, ','))
317 type = (char *)"auto";
318 fs->type = xstrdup(type);
319 fs->opts = xstrdup(opts ? opts : "");
320 fs->passno = passno < 0 ? 1 : passno;
322 /*fs->next = NULL; */
327 G.filesys_last->next = fs;
333 /* Load the filesystem database from /etc/fstab */
334 static void load_fs_info(const char *filename)
340 fstab = setmntent(filename, "r");
342 bb_perror_msg("can't read '%s'", filename);
346 // Loop through entries
347 while (getmntent_r(fstab, &mte, buf, sizeof(buf))) {
348 //bb_info_msg("CREATE[%s][%s][%s][%s][%d]", mte.mnt_fsname, mte.mnt_dir,
349 // mte.mnt_type, mte.mnt_opts,
351 create_fs_device(mte.mnt_fsname, mte.mnt_dir,
352 mte.mnt_type, mte.mnt_opts,
358 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
359 static struct fs_info *lookup(char *filesys)
363 for (fs = G.filesys_info; fs; fs = fs->next) {
364 if (strcmp(filesys, fs->device) == 0
365 || (fs->mountpt && strcmp(filesys, fs->mountpt) == 0)
373 #if DO_PROGRESS_INDICATOR
374 static int progress_active(void)
376 struct fsck_instance *inst;
378 for (inst = G.instance_list; inst; inst = inst->next) {
379 if (inst->flags & FLAG_DONE)
381 if (inst->flags & FLAG_PROGRESS)
390 * Send a signal to all outstanding fsck child processes
392 static void kill_all_if_got_signal(void)
394 struct fsck_instance *inst;
396 if (!bb_got_signal || G.kill_sent)
399 for (inst = G.instance_list; inst; inst = inst->next) {
400 if (inst->flags & FLAG_DONE)
402 kill(inst->pid, SIGTERM);
408 * Wait for one child process to exit; when it does, unlink it from
409 * the list of executing child processes, free, and return its exit status.
410 * If there is no exited child, return -1.
412 static int wait_one(int flags)
416 struct fsck_instance *inst, *prev;
419 if (!G.instance_list)
421 /* if (G.noexecute) { already returned -1; } */
424 pid = waitpid(-1, &status, flags);
425 kill_all_if_got_signal();
426 if (pid == 0) /* flags == WNOHANG and no children exited */
431 if (errno == ECHILD) { /* paranoia */
432 bb_error_msg("wait: no more children");
435 bb_perror_msg("wait");
439 inst = G.instance_list;
441 if (inst->pid == pid)
449 status = WEXITSTATUS(status);
450 if (WIFSIGNALED(status)) {
451 sig = WTERMSIG(status);
452 status = EXIT_UNCORRECTED;
454 printf("Warning: %s %s terminated "
456 inst->prog, inst->device, sig);
461 #if DO_PROGRESS_INDICATOR
462 if (progress && (inst->flags & FLAG_PROGRESS) && !progress_active()) {
463 struct fsck_instance *inst2;
464 for (inst2 = G.instance_list; inst2; inst2 = inst2->next) {
465 if (inst2->flags & FLAG_DONE)
467 if (strcmp(inst2->type, "ext2") != 0
468 && strcmp(inst2->type, "ext3") != 0
472 /* ext[23], we will send USR1
473 * (request to start displaying progress bar)
475 * If we've just started the fsck, wait a tiny
476 * bit before sending the kill, to give it
477 * time to set up the signal handler
479 if (inst2->start_time >= time(NULL) - 1)
481 kill(inst2->pid, SIGUSR1);
482 inst2->flags |= FLAG_PROGRESS;
489 prev->next = inst->next;
491 G.instance_list = inst->next;
493 printf("Finished with %s (exit status %d)\n",
494 inst->device, status);
502 * Wait until all executing child processes have exited; return the
503 * logical OR of all of their exit code values.
505 #define FLAG_WAIT_ALL 0
506 #define FLAG_WAIT_ATLEAST_ONE WNOHANG
507 static int wait_many(int flags)
510 int global_status = 0;
513 while ((exit_status = wait_one(wait_flags)) != -1) {
514 global_status |= exit_status;
517 return global_status;
521 * Execute a particular fsck program, and link it into the list of
522 * child processes we are waiting for.
524 static void execute(const char *type, const char *device,
525 const char *mntpt /*, int interactive */)
528 struct fsck_instance *inst;
531 G.args[0] = xasprintf("fsck.%s", type);
533 #if DO_PROGRESS_INDICATOR
534 if (progress && !progress_active()) {
535 if (strcmp(type, "ext2") == 0
536 || strcmp(type, "ext3") == 0
538 G.args[XXX] = xasprintf("-C%d", progress_fd); /* 1 */
539 inst->flags |= FLAG_PROGRESS;
544 G.args[G.num_args - 2] = (char*)device;
545 /* G.args[G.num_args - 1] = NULL; - already is */
547 if (G.verbose || G.noexecute) {
548 printf("[%s (%d) -- %s]", G.args[0], G.num_running,
549 mntpt ? mntpt : device);
550 for (i = 0; G.args[i]; i++)
551 printf(" %s", G.args[i]);
555 /* Fork and execute the correct program. */
560 bb_simple_perror_msg(G.args[0]);
563 #if DO_PROGRESS_INDICATOR
567 /* No child, so don't record an instance */
573 inst = xzalloc(sizeof(*inst));
575 inst->prog = G.args[0];
576 inst->device = xstrdup(device);
577 inst->base_device = base_device(device);
578 #if DO_PROGRESS_INDICATOR
579 inst->start_time = time(NULL);
582 /* Add to the list of running fsck's.
583 * (was adding to the end, but adding to the front is simpler...) */
584 inst->next = G.instance_list;
585 G.instance_list = inst;
589 * Run the fsck program on a particular device
591 * If the type is specified using -t, and it isn't prefixed with "no"
592 * (as in "noext2") and only one filesystem type is specified, then
593 * use that type regardless of what is specified in /etc/fstab.
595 * If the type isn't specified by the user, then use either the type
596 * specified in /etc/fstab, or "auto".
598 static void fsck_device(struct fs_info *fs /*, int interactive */)
602 if (strcmp(fs->type, "auto") != 0) {
605 bb_info_msg("using filesystem type '%s' %s",
608 && (G.fstype[0] != 'n' || G.fstype[1] != 'o') /* != "no" */
609 && !is_prefixed_with(G.fstype, "opts=")
610 && !is_prefixed_with(G.fstype, "loop")
611 && !strchr(G.fstype, ',')
615 bb_info_msg("using filesystem type '%s' %s",
620 bb_info_msg("using filesystem type '%s' %s",
625 execute(type, fs->device, fs->mountpt /*, interactive */);
629 * Returns TRUE if a partition on the same disk is already being
632 static int device_already_active(char *device)
634 struct fsck_instance *inst;
637 if (G.force_all_parallel)
641 /* Don't check a soft raid disk with any other disk */
643 && (is_prefixed_with(G.instance_list->device, BASE_MD)
644 || is_prefixed_with(device, BASE_MD))
650 base = base_device(device);
652 * If we don't know the base device, assume that the device is
653 * already active if there are any fsck instances running.
656 return (G.instance_list != NULL);
658 for (inst = G.instance_list; inst; inst = inst->next) {
659 if (!inst->base_device || !strcmp(base, inst->base_device)) {
670 * This function returns true if a particular option appears in a
671 * comma-delimited options list
673 static int opt_in_list(char *opt, char *optlist)
684 s = strstr(s + 1, opt);
687 /* neither "opt.." nor "xxx,opt.."? */
688 if (s != optlist && s[-1] != ',')
690 /* neither "..opt" nor "..opt,xxx"? */
691 if (s[len] != '\0' && s[len] != ',')
697 /* See if the filesystem matches the criteria given by the -t option */
698 static int fs_match(struct fs_info *fs)
700 int n, ret, checked_type;
710 cp = G.fs_type_list[n];
713 switch (G.fs_type_flag[n]) {
714 case FS_TYPE_FLAG_NORMAL:
716 if (strcmp(cp, fs->type) == 0)
719 case FS_TYPE_FLAG_NEGOPT:
720 if (opt_in_list(cp, fs->opts))
723 case FS_TYPE_FLAG_OPT:
724 if (!opt_in_list(cp, fs->opts))
730 if (checked_type == 0)
733 return (G.fs_type_negated ? !ret : ret);
736 /* Check if we should ignore this filesystem. */
737 static int ignore(struct fs_info *fs)
740 * If the pass number is 0, ignore it.
746 * If a specific fstype is specified, and it doesn't match,
752 /* Are we ignoring this type? */
753 if (index_in_strings(ignored_types, fs->type) >= 0)
756 /* We can and want to check this file system type. */
760 /* Check all file systems, using the /etc/fstab table. */
761 static int check_all(void)
764 int status = EXIT_OK;
765 smallint not_done_yet;
770 puts("Checking all filesystems");
773 * Do an initial scan over the filesystem; mark filesystems
774 * which should be ignored as done, and resolve any "auto"
775 * filesystem types (done as a side-effect of calling ignore()).
777 for (fs = G.filesys_info; fs; fs = fs->next)
779 fs->flags |= FLAG_DONE;
782 * Find and check the root filesystem.
784 if (!G.parallel_root) {
785 for (fs = G.filesys_info; fs; fs = fs->next) {
786 if (LONE_CHAR(fs->mountpt, '/')) {
787 if (!G.skip_root && !ignore(fs)) {
788 fsck_device(fs /*, 1*/);
789 status |= wait_many(FLAG_WAIT_ALL);
790 if (status > EXIT_NONDESTRUCT)
793 fs->flags |= FLAG_DONE;
799 * This is for the bone-headed user who has root
800 * filesystem listed twice.
801 * "Skip root" will skip _all_ root entries.
804 for (fs = G.filesys_info; fs; fs = fs->next)
805 if (LONE_CHAR(fs->mountpt, '/'))
806 fs->flags |= FLAG_DONE;
810 while (not_done_yet) {
814 for (fs = G.filesys_info; fs; fs = fs->next) {
817 if (fs->flags & FLAG_DONE)
820 * If the filesystem's pass number is higher
821 * than the current pass number, then we didn't
824 if (fs->passno > passno) {
829 * If a filesystem on a particular device has
830 * already been spawned, then we need to defer
831 * this to another pass.
833 if (device_already_active(fs->device)) {
838 * Spawn off the fsck process
840 fsck_device(fs /*, G.serialize*/);
841 fs->flags |= FLAG_DONE;
844 * Only do one filesystem at a time, or if we
845 * have a limit on the number of fsck's extant
846 * at one time, apply that limit.
849 || (G.num_running >= G.max_running)
858 printf("--waiting-- (pass %d)\n", passno);
859 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
860 FLAG_WAIT_ATLEAST_ONE);
863 puts("----------------------------------");
868 kill_all_if_got_signal();
869 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
874 * Deal with the fsck -t argument.
875 * Huh, for mount "-t novfat,nfs" means "neither vfat nor nfs"!
876 * Why here we require "-t novfat,nonfs" ??
878 static void compile_fs_type(char *fs_type)
885 while ((s = strchr(s, ','))) {
890 G.fs_type_list = xzalloc(num * sizeof(G.fs_type_list[0]));
891 G.fs_type_flag = xzalloc(num * sizeof(G.fs_type_flag[0]));
892 G.fs_type_negated = -1; /* not yet known is it negated or not */
900 if (s[0] == 'n' && s[1] == 'o') { /* "no.." */
903 } else if (s[0] == '!') {
908 if (strcmp(s, "loop") == 0)
909 /* loop is really short-hand for opts=loop */
910 goto loop_special_case;
911 if (is_prefixed_with(s, "opts=")) {
914 G.fs_type_flag[num] = negate ? FS_TYPE_FLAG_NEGOPT : FS_TYPE_FLAG_OPT;
916 if (G.fs_type_negated == -1)
917 G.fs_type_negated = negate;
918 if (G.fs_type_negated != negate)
919 bb_error_msg_and_die(
920 "either all or none of the filesystem types passed to -t must be prefixed "
923 comma = strchrnul(s, ',');
924 G.fs_type_list[num++] = xstrndup(s, comma-s);
931 static char **new_args(void)
933 G.args = xrealloc_vector(G.args, 2, G.num_args);
934 return &G.args[G.num_args++];
937 int fsck_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
938 int fsck_main(int argc UNUSED_PARAM, char **argv)
947 smallint opts_for_fsck;
953 /* we want wait() to be interruptible */
954 signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
955 signal_no_SA_RESTART_empty_mask(SIGTERM, record_signo);
957 setbuf(stdout, NULL);
959 opts_for_fsck = doall = notitle = 0;
962 new_args(); /* G.args[0] = NULL, will be replaced by fsck.<type> */
963 /* G.instance_list = NULL; - in bss, so already zeroed */
971 /* "/dev/blk" or "/path" or "UUID=xxx" or "LABEL=xxx" */
972 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
973 // FIXME: must check that arg is a blkdev, or resolve
974 // "/path", "UUID=xxx" or "LABEL=xxx" into block device name
975 // ("UUID=xxx"/"LABEL=xxx" can probably shifted to fsck.auto duties)
976 devices = xrealloc_vector(devices, 2, num_devices);
977 devices[num_devices++] = arg;
981 if (arg[0] != '-' || opts_for_fsck) {
986 if (LONE_CHAR(arg + 1, '-')) { /* "--" ? */
993 for (j = 1; arg[j]; j++) {
998 #if DO_PROGRESS_INDICATOR
1001 if (arg[++j]) { /* -Cn */
1002 progress_fd = xatoi_positive(&arg[j]);
1008 progress_fd = xatoi_positive(*argv);
1027 G.parallel_root = 1;
1041 G.fstype = xstrdup(tmp);
1042 compile_fs_type(G.fstype);
1049 /* one extra for '\0' */
1050 options = xrealloc(options, optpos + 2);
1051 options[optpos] = arg[j];
1058 options[optpos + 1] = '\0';
1059 *new_args() = options;
1062 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1063 G.force_all_parallel = 1;
1064 tmp = getenv("FSCK_MAX_INST");
1065 G.max_running = INT_MAX;
1067 G.max_running = xatoi(tmp);
1068 new_args(); /* G.args[G.num_args - 2] will be replaced by <device> */
1069 new_args(); /* G.args[G.num_args - 1] is the last, NULL element */
1072 puts("fsck (busybox "BB_VER", "BB_BT")");
1074 /* Even plain "fsck /dev/hda1" needs fstab to get fs type,
1075 * so we are scanning it anyway */
1076 fstab = getenv("FSTAB_FILE");
1078 fstab = "/etc/fstab";
1079 load_fs_info(fstab);
1081 /*interactive = (num_devices == 1) | G.serialize;*/
1083 if (num_devices == 0)
1084 /*interactive =*/ G.serialize = doall = 1;
1089 for (i = 0; i < num_devices; i++) {
1090 if (bb_got_signal) {
1091 kill_all_if_got_signal();
1095 fs = lookup(devices[i]);
1097 fs = create_fs_device(devices[i], "", "auto", NULL, -1);
1098 fsck_device(fs /*, interactive */);
1101 || (G.num_running >= G.max_running)
1103 int exit_status = wait_one(0);
1104 if (exit_status >= 0)
1105 status |= exit_status;
1107 puts("----------------------------------");
1110 status |= wait_many(FLAG_WAIT_ALL);