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 //config: bool "fsck (6.7 kb)"
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"
63 #include "common_bufsiz.h"
65 /* "progress indicator" code is somewhat buggy and ext[23] specific.
66 * We should be filesystem agnostic. IOW: there should be a well-defined
67 * API for fsck.something, NOT ad-hoc hacks in generic fsck. */
68 #define DO_PROGRESS_INDICATOR 0
70 /* fsck 1.41.4 (27-Jan-2009) manpage says:
72 * 1 - File system errors corrected
73 * 2 - System should be rebooted
74 * 4 - File system errors left uncorrected
75 * 8 - Operational error
76 * 16 - Usage or syntax error
77 * 32 - Fsck canceled by user request
78 * 128 - Shared library error
81 #define EXIT_NONDESTRUCT 1
82 #define EXIT_DESTRUCT 2
83 #define EXIT_UNCORRECTED 4
86 #define FSCK_CANCELED 32 /* Aborted with a signal or ^C */
89 * Internal structure for mount table entries.
102 #define FLAG_PROGRESS 2
104 * Structure to allow exit codes to be stored
106 struct fsck_instance {
107 struct fsck_instance *next;
110 #if DO_PROGRESS_INDICATOR
115 char *base_device; /* /dev/hda for /dev/hdaN etc */
118 static const char ignored_types[] ALIGN1 =
129 static const char really_wanted[] ALIGN1 =
139 #define BASE_MD "/dev/md"
146 #define FS_TYPE_FLAG_NORMAL 0
147 #define FS_TYPE_FLAG_OPT 1
148 #define FS_TYPE_FLAG_NEGOPT 2
150 uint8_t *fs_type_flag;
151 smallint fs_type_negated;
156 /* smallint like_mount; */
157 smallint parallel_root;
158 smallint force_all_parallel;
161 #if DO_PROGRESS_INDICATOR
169 struct fs_info *filesys_info;
170 struct fs_info *filesys_last;
171 struct fsck_instance *instance_list;
173 #define G (*(struct globals*)bb_common_bufsiz1)
174 #define INIT_G() do { \
175 setup_common_bufsiz(); \
176 BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
180 * Return the "base device" given a particular device; this is used to
181 * assure that we only fsck one partition on a particular drive at any
182 * one time. Otherwise, the disk heads will be seeking all over the
183 * place. If the base device cannot be determined, return NULL.
185 * The base_device() function returns an allocated string which must
188 #if ENABLE_FEATURE_DEVFS
190 * Required for the uber-silly devfs /dev/ide/host1/bus2/target3/lun3
193 static const char *const devfs_hier[] = {
194 "host", "bus", "target", "lun", NULL
198 static char *base_device(const char *device)
201 #if ENABLE_FEATURE_DEVFS
202 const char *const *hier;
206 str = xstrdup(device);
208 /* Skip over "/dev/"; if it's not present, give up */
209 cp = skip_dev_pfx(str);
214 * For md devices, we treat them all as if they were all
215 * on one disk, since we don't know how to parallelize them.
217 if (cp[0] == 'm' && cp[1] == 'd') {
222 /* Handle DAC 960 devices */
223 if (is_prefixed_with(cp, "rd/")) {
225 if (cp[0] != 'c' || !isdigit(cp[1])
226 || cp[2] != 'd' || !isdigit(cp[3]))
232 /* Now let's handle /dev/hd* and /dev/sd* devices.... */
233 if ((cp[0] == 'h' || cp[0] == 's') && cp[1] == 'd') {
235 /* If there's a single number after /dev/hd, skip it */
238 /* What follows must be an alpha char, or give up */
245 #if ENABLE_FEATURE_DEVFS
246 /* Now let's handle devfs (ugh) names */
248 if (is_prefixed_with(cp, "ide/"))
250 if (is_prefixed_with(cp, "scsi/"))
255 * Now we proceed down the expected devfs hierarchy.
256 * i.e., .../host1/bus2/target3/lun4/...
257 * If we don't find the expected token, followed by
258 * some number of digits at each level, abort.
260 for (hier = devfs_hier; *hier; hier++) {
261 cp = is_prefixed_with(cp, *hier);
264 while (*cp != '/' && *cp != '\0') {
269 //FIXME: what if *cp = '\0' now? cp++ moves past it!!!
276 /* Now handle devfs /dev/disc or /dev/disk names */
278 if (is_prefixed_with(cp, "discs/"))
280 else if (is_prefixed_with(cp, "disks/"))
284 cp = is_prefixed_with(cp, disk);
287 while (*cp != '/' && *cp != '\0') {
301 static void free_instance(struct fsck_instance *p)
305 free(p->base_device);
309 static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
310 const char *type, const char *opts,
315 fs = xzalloc(sizeof(*fs));
316 fs->device = xstrdup(device);
317 fs->mountpt = xstrdup(mntpnt);
318 if (strchr(type, ','))
319 type = (char *)"auto";
320 fs->type = xstrdup(type);
321 fs->opts = xstrdup(opts ? opts : "");
322 fs->passno = passno < 0 ? 1 : passno;
324 /*fs->next = NULL; */
329 G.filesys_last->next = fs;
335 /* Load the filesystem database from /etc/fstab */
336 static void load_fs_info(const char *filename)
342 fstab = setmntent(filename, "r");
344 bb_perror_msg("can't read '%s'", filename);
348 // Loop through entries
349 while (getmntent_r(fstab, &mte, buf, sizeof(buf))) {
350 //bb_error_msg("CREATE[%s][%s][%s][%s][%d]", mte.mnt_fsname, mte.mnt_dir,
351 // mte.mnt_type, mte.mnt_opts,
353 create_fs_device(mte.mnt_fsname, mte.mnt_dir,
354 mte.mnt_type, mte.mnt_opts,
360 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
361 static struct fs_info *lookup(char *filesys)
365 for (fs = G.filesys_info; fs; fs = fs->next) {
366 if (strcmp(filesys, fs->device) == 0
367 || (fs->mountpt && strcmp(filesys, fs->mountpt) == 0)
375 #if DO_PROGRESS_INDICATOR
376 static int progress_active(void)
378 struct fsck_instance *inst;
380 for (inst = G.instance_list; inst; inst = inst->next) {
381 if (inst->flags & FLAG_DONE)
383 if (inst->flags & FLAG_PROGRESS)
392 * Send a signal to all outstanding fsck child processes
394 static void kill_all_if_got_signal(void)
396 struct fsck_instance *inst;
398 if (!bb_got_signal || G.kill_sent)
401 for (inst = G.instance_list; inst; inst = inst->next) {
402 if (inst->flags & FLAG_DONE)
404 kill(inst->pid, SIGTERM);
410 * Wait for one child process to exit; when it does, unlink it from
411 * the list of executing child processes, free, and return its exit status.
412 * If there is no exited child, return -1.
414 static int wait_one(int flags)
418 struct fsck_instance *inst, *prev;
421 if (!G.instance_list)
423 /* if (G.noexecute) { already returned -1; } */
426 pid = waitpid(-1, &status, flags);
427 kill_all_if_got_signal();
428 if (pid == 0) /* flags == WNOHANG and no children exited */
433 if (errno == ECHILD) { /* paranoia */
434 bb_error_msg("wait: no more children");
437 bb_perror_msg("wait");
441 inst = G.instance_list;
443 if (inst->pid == pid)
451 exitcode = WEXITSTATUS(status);
452 if (WIFSIGNALED(status)) {
454 sig = WTERMSIG(status);
455 exitcode = EXIT_UNCORRECTED;
457 printf("Warning: %s %s terminated "
459 inst->prog, inst->device, sig);
460 exitcode = EXIT_ERROR;
464 #if DO_PROGRESS_INDICATOR
465 if (progress && (inst->flags & FLAG_PROGRESS) && !progress_active()) {
466 struct fsck_instance *inst2;
467 for (inst2 = G.instance_list; inst2; inst2 = inst2->next) {
468 if (inst2->flags & FLAG_DONE)
470 if (strcmp(inst2->type, "ext2") != 0
471 && strcmp(inst2->type, "ext3") != 0
475 /* ext[23], we will send USR1
476 * (request to start displaying progress bar)
478 * If we've just started the fsck, wait a tiny
479 * bit before sending the kill, to give it
480 * time to set up the signal handler
482 if (inst2->start_time >= time(NULL) - 1)
484 kill(inst2->pid, SIGUSR1);
485 inst2->flags |= FLAG_PROGRESS;
492 prev->next = inst->next;
494 G.instance_list = inst->next;
496 printf("Finished with %s (exit status %u)\n",
497 inst->device, exitcode);
505 * Wait until all executing child processes have exited; return the
506 * logical OR of all of their exit code values.
508 #define FLAG_WAIT_ALL 0
509 #define FLAG_WAIT_ATLEAST_ONE WNOHANG
510 static int wait_many(int flags)
513 int global_status = 0;
516 while ((exit_status = wait_one(wait_flags)) != -1) {
517 global_status |= exit_status;
520 return global_status;
524 * Execute a particular fsck program, and link it into the list of
525 * child processes we are waiting for.
527 static void execute(const char *type, const char *device,
528 const char *mntpt /*, int interactive */)
531 struct fsck_instance *inst;
534 G.args[0] = xasprintf("fsck.%s", type);
536 #if DO_PROGRESS_INDICATOR
537 if (progress && !progress_active()) {
538 if (strcmp(type, "ext2") == 0
539 || strcmp(type, "ext3") == 0
541 G.args[XXX] = xasprintf("-C%d", progress_fd); /* 1 */
542 inst->flags |= FLAG_PROGRESS;
547 G.args[G.num_args - 2] = (char*)device;
548 /* G.args[G.num_args - 1] = NULL; - already is */
550 if (G.verbose || G.noexecute) {
551 printf("[%s (%d) -- %s]", G.args[0], G.num_running,
552 mntpt ? mntpt : device);
553 for (i = 0; G.args[i]; i++)
554 printf(" %s", G.args[i]);
558 /* Fork and execute the correct program. */
563 bb_simple_perror_msg(G.args[0]);
566 #if DO_PROGRESS_INDICATOR
570 /* No child, so don't record an instance */
576 inst = xzalloc(sizeof(*inst));
578 inst->prog = G.args[0];
579 inst->device = xstrdup(device);
580 inst->base_device = base_device(device);
581 #if DO_PROGRESS_INDICATOR
582 inst->start_time = time(NULL);
585 /* Add to the list of running fsck's.
586 * (was adding to the end, but adding to the front is simpler...) */
587 inst->next = G.instance_list;
588 G.instance_list = inst;
592 * Run the fsck program on a particular device
594 * If the type is specified using -t, and it isn't prefixed with "no"
595 * (as in "noext2") and only one filesystem type is specified, then
596 * use that type regardless of what is specified in /etc/fstab.
598 * If the type isn't specified by the user, then use either the type
599 * specified in /etc/fstab, or "auto".
601 static void fsck_device(struct fs_info *fs /*, int interactive */)
605 if (strcmp(fs->type, "auto") != 0) {
608 printf("using filesystem type '%s' %s\n",
611 && (G.fstype[0] != 'n' || G.fstype[1] != 'o') /* != "no" */
612 && !is_prefixed_with(G.fstype, "opts=")
613 && !is_prefixed_with(G.fstype, "loop")
614 && !strchr(G.fstype, ',')
618 printf("using filesystem type '%s' %s\n",
623 printf("using filesystem type '%s' %s\n",
628 execute(type, fs->device, fs->mountpt /*, interactive */);
632 * Returns TRUE if a partition on the same disk is already being
635 static int device_already_active(char *device)
637 struct fsck_instance *inst;
640 if (G.force_all_parallel)
644 /* Don't check a soft raid disk with any other disk */
646 && (is_prefixed_with(G.instance_list->device, BASE_MD)
647 || is_prefixed_with(device, BASE_MD))
653 base = base_device(device);
655 * If we don't know the base device, assume that the device is
656 * already active if there are any fsck instances running.
659 return (G.instance_list != NULL);
661 for (inst = G.instance_list; inst; inst = inst->next) {
662 if (!inst->base_device || strcmp(base, inst->base_device) == 0) {
673 * This function returns true if a particular option appears in a
674 * comma-delimited options list
676 static int opt_in_list(char *opt, char *optlist)
687 s = strstr(s + 1, opt);
690 /* neither "opt.." nor "xxx,opt.."? */
691 if (s != optlist && s[-1] != ',')
693 /* neither "..opt" nor "..opt,xxx"? */
694 if (s[len] != '\0' && s[len] != ',')
700 /* See if the filesystem matches the criteria given by the -t option */
701 static int fs_match(struct fs_info *fs)
703 int n, ret, checked_type;
713 cp = G.fs_type_list[n];
716 switch (G.fs_type_flag[n]) {
717 case FS_TYPE_FLAG_NORMAL:
719 if (strcmp(cp, fs->type) == 0)
722 case FS_TYPE_FLAG_NEGOPT:
723 if (opt_in_list(cp, fs->opts))
726 case FS_TYPE_FLAG_OPT:
727 if (!opt_in_list(cp, fs->opts))
733 if (checked_type == 0)
736 return (G.fs_type_negated ? !ret : ret);
739 /* Check if we should ignore this filesystem. */
740 static int ignore(struct fs_info *fs)
743 * If the pass number is 0, ignore it.
749 * If a specific fstype is specified, and it doesn't match,
755 /* Are we ignoring this type? */
756 if (index_in_strings(ignored_types, fs->type) >= 0)
759 /* We can and want to check this file system type. */
763 /* Check all file systems, using the /etc/fstab table. */
764 static int check_all(void)
767 int status = EXIT_OK;
768 smallint not_done_yet;
773 puts("Checking all filesystems");
776 * Do an initial scan over the filesystem; mark filesystems
777 * which should be ignored as done, and resolve any "auto"
778 * filesystem types (done as a side-effect of calling ignore()).
780 for (fs = G.filesys_info; fs; fs = fs->next)
782 fs->flags |= FLAG_DONE;
785 * Find and check the root filesystem.
787 if (!G.parallel_root) {
788 for (fs = G.filesys_info; fs; fs = fs->next) {
789 if (LONE_CHAR(fs->mountpt, '/')) {
790 if (!G.skip_root && !ignore(fs)) {
791 fsck_device(fs /*, 1*/);
792 status |= wait_many(FLAG_WAIT_ALL);
793 if (status > EXIT_NONDESTRUCT)
796 fs->flags |= FLAG_DONE;
802 * This is for the bone-headed user who has root
803 * filesystem listed twice.
804 * "Skip root" will skip _all_ root entries.
807 for (fs = G.filesys_info; fs; fs = fs->next)
808 if (LONE_CHAR(fs->mountpt, '/'))
809 fs->flags |= FLAG_DONE;
813 while (not_done_yet) {
817 for (fs = G.filesys_info; fs; fs = fs->next) {
820 if (fs->flags & FLAG_DONE)
823 * If the filesystem's pass number is higher
824 * than the current pass number, then we didn't
827 if (fs->passno > passno) {
832 * If a filesystem on a particular device has
833 * already been spawned, then we need to defer
834 * this to another pass.
836 if (device_already_active(fs->device)) {
841 * Spawn off the fsck process
843 fsck_device(fs /*, G.serialize*/);
844 fs->flags |= FLAG_DONE;
847 * Only do one filesystem at a time, or if we
848 * have a limit on the number of fsck's extant
849 * at one time, apply that limit.
852 || (G.num_running >= G.max_running)
861 printf("--waiting-- (pass %d)\n", passno);
862 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
863 FLAG_WAIT_ATLEAST_ONE);
866 puts("----------------------------------");
871 kill_all_if_got_signal();
872 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
877 * Deal with the fsck -t argument.
878 * Huh, for mount "-t novfat,nfs" means "neither vfat nor nfs"!
879 * Why here we require "-t novfat,nonfs" ??
881 static void compile_fs_type(char *fs_type)
888 while ((s = strchr(s, ','))) {
893 G.fs_type_list = xzalloc(num * sizeof(G.fs_type_list[0]));
894 G.fs_type_flag = xzalloc(num * sizeof(G.fs_type_flag[0]));
895 G.fs_type_negated = -1; /* not yet known is it negated or not */
903 if (s[0] == 'n' && s[1] == 'o') { /* "no.." */
906 } else if (s[0] == '!') {
911 if (strcmp(s, "loop") == 0)
912 /* loop is really short-hand for opts=loop */
913 goto loop_special_case;
914 if (is_prefixed_with(s, "opts=")) {
917 G.fs_type_flag[num] = negate ? FS_TYPE_FLAG_NEGOPT : FS_TYPE_FLAG_OPT;
919 if (G.fs_type_negated == -1)
920 G.fs_type_negated = negate;
921 if (G.fs_type_negated != negate)
922 bb_error_msg_and_die(
923 "either all or none of the filesystem types passed to -t must be prefixed "
926 comma = strchrnul(s, ',');
927 G.fs_type_list[num++] = xstrndup(s, comma-s);
934 static char **new_args(void)
936 G.args = xrealloc_vector(G.args, 2, G.num_args);
937 return &G.args[G.num_args++];
940 int fsck_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
941 int fsck_main(int argc UNUSED_PARAM, char **argv)
950 smallint opts_for_fsck;
956 /* we want wait() to be interruptible */
957 signal_no_SA_RESTART_empty_mask(SIGINT, record_signo);
958 signal_no_SA_RESTART_empty_mask(SIGTERM, record_signo);
960 setbuf(stdout, NULL);
962 opts_for_fsck = doall = notitle = 0;
965 new_args(); /* G.args[0] = NULL, will be replaced by fsck.<type> */
966 /* G.instance_list = NULL; - in bss, so already zeroed */
974 /* "/dev/blk" or "/path" or "UUID=xxx" or "LABEL=xxx" */
975 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
976 // FIXME: must check that arg is a blkdev, or resolve
977 // "/path", "UUID=xxx" or "LABEL=xxx" into block device name
978 // ("UUID=xxx"/"LABEL=xxx" can probably shifted to fsck.auto duties)
979 devices = xrealloc_vector(devices, 2, num_devices);
980 devices[num_devices++] = arg;
984 if (arg[0] != '-' || opts_for_fsck) {
989 if (LONE_CHAR(arg + 1, '-')) { /* "--" ? */
996 for (j = 1; arg[j]; j++) {
1001 #if DO_PROGRESS_INDICATOR
1004 if (arg[++j]) { /* -Cn */
1005 progress_fd = xatoi_positive(&arg[j]);
1011 progress_fd = xatoi_positive(*argv);
1030 G.parallel_root = 1;
1044 G.fstype = xstrdup(tmp);
1045 compile_fs_type(G.fstype);
1052 /* one extra for '\0' */
1053 options = xrealloc(options, optpos + 2);
1054 options[optpos] = arg[j];
1061 options[optpos + 1] = '\0';
1062 *new_args() = options;
1065 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1066 G.force_all_parallel = 1;
1067 tmp = getenv("FSCK_MAX_INST");
1068 G.max_running = INT_MAX;
1070 G.max_running = xatoi(tmp);
1071 new_args(); /* G.args[G.num_args - 2] will be replaced by <device> */
1072 new_args(); /* G.args[G.num_args - 1] is the last, NULL element */
1075 puts("fsck (busybox "BB_VER")");
1077 /* Even plain "fsck /dev/hda1" needs fstab to get fs type,
1078 * so we are scanning it anyway */
1079 fstab = getenv("FSTAB_FILE");
1081 fstab = "/etc/fstab";
1082 load_fs_info(fstab);
1084 /*interactive = (num_devices == 1) | G.serialize;*/
1086 if (num_devices == 0)
1087 /*interactive =*/ G.serialize = doall = 1;
1092 for (i = 0; i < num_devices; i++) {
1093 if (bb_got_signal) {
1094 kill_all_if_got_signal();
1098 fs = lookup(devices[i]);
1100 fs = create_fs_device(devices[i], "", "auto", NULL, -1);
1101 fsck_device(fs /*, interactive */);
1104 || (G.num_running >= G.max_running)
1106 int exit_status = wait_one(0);
1107 if (exit_status >= 0)
1108 status |= exit_status;
1110 puts("----------------------------------");
1113 status |= wait_many(FLAG_WAIT_ALL);