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.
24 * This file may be redistributed under the terms of the GNU Public
29 /* All filesystem specific hooks have been removed.
30 * If filesystem cannot be determined, we will execute
31 * "fsck.auto". Currently this also happens if you specify
32 * UUID=xxx or LABEL=xxx as an object to check.
33 * Detection code for that is also probably has to be in fsck.auto.
35 * In other words, this is _really_ is just a driver program which
36 * spawns actual fsck.something for each filesystem to check.
37 * It doesn't guess filesystem types from on-disk format.
43 #define EXIT_NONDESTRUCT 1
44 #define EXIT_DESTRUCT 2
45 #define EXIT_UNCORRECTED 4
48 #define FSCK_CANCELED 32 /* Aborted with a signal or ^C */
51 * Internal structure for mount table entries.
66 #define FLAG_PROGRESS 2
68 * Structure to allow exit codes to be stored
70 struct fsck_instance {
71 struct fsck_instance *next;
79 char *base_device; /* /dev/hda for /dev/hdaN etc */
82 static const char ignored_types[] ALIGN1 =
93 static const char really_wanted[] ALIGN1 =
103 #define BASE_MD "/dev/md"
105 static char **devices;
107 static int num_devices;
111 #define FS_TYPE_FLAG_NORMAL 0
112 #define FS_TYPE_FLAG_OPT 1
113 #define FS_TYPE_FLAG_NEGOPT 2
114 static char **fs_type_list;
115 static uint8_t *fs_type_flag;
116 static smallint fs_type_negated;
118 static volatile smallint cancel_requested;
119 static smallint doall;
120 static smallint noexecute;
121 static smallint serialize;
122 static smallint skip_root;
123 /* static smallint like_mount; */
124 static smallint notitle;
125 static smallint parallel_root;
126 static smallint force_all_parallel;
128 /* "progress indicator" code is somewhat buggy and ext[23] specific.
129 * We should be filesystem agnostic. IOW: there should be a well-defined
130 * API for fsck.something, NOT ad-hoc hacks in generic fsck. */
131 #define DO_PROGRESS_INDICATOR 0
132 #if DO_PROGRESS_INDICATOR
133 static smallint progress;
134 static int progress_fd;
137 static int num_running;
138 static int max_running;
140 static struct fs_info *filesys_info;
141 static struct fs_info *filesys_last;
142 static struct fsck_instance *instance_list;
145 * Return the "base device" given a particular device; this is used to
146 * assure that we only fsck one partition on a particular drive at any
147 * one time. Otherwise, the disk heads will be seeking all over the
148 * place. If the base device cannot be determined, return NULL.
150 * The base_device() function returns an allocated string which must
153 #if ENABLE_FEATURE_DEVFS
155 * Required for the uber-silly devfs /dev/ide/host1/bus2/target3/lun3
158 static const char *const devfs_hier[] = {
159 "host", "bus", "target", "lun", NULL
163 static char *base_device(const char *device)
166 #if ENABLE_FEATURE_DEVFS
167 const char *const *hier;
171 cp = str = xstrdup(device);
173 /* Skip over /dev/; if it's not present, give up. */
174 if (strncmp(cp, "/dev/", 5) != 0)
179 * For md devices, we treat them all as if they were all
180 * on one disk, since we don't know how to parallelize them.
182 if (cp[0] == 'm' && cp[1] == 'd') {
187 /* Handle DAC 960 devices */
188 if (strncmp(cp, "rd/", 3) == 0) {
190 if (cp[0] != 'c' || !isdigit(cp[1])
191 || cp[2] != 'd' || !isdigit(cp[3]))
197 /* Now let's handle /dev/hd* and /dev/sd* devices.... */
198 if ((cp[0] == 'h' || cp[0] == 's') && cp[1] == 'd') {
200 /* If there's a single number after /dev/hd, skip it */
203 /* What follows must be an alpha char, or give up */
210 #if ENABLE_FEATURE_DEVFS
211 /* Now let's handle devfs (ugh) names */
213 if (strncmp(cp, "ide/", 4) == 0)
215 if (strncmp(cp, "scsi/", 5) == 0)
220 * Now we proceed down the expected devfs hierarchy.
221 * i.e., .../host1/bus2/target3/lun4/...
222 * If we don't find the expected token, followed by
223 * some number of digits at each level, abort.
225 for (hier = devfs_hier; *hier; hier++) {
227 if (strncmp(cp, *hier, len) != 0)
230 while (*cp != '/' && *cp != 0) {
241 /* Now handle devfs /dev/disc or /dev/disk names */
243 if (strncmp(cp, "discs/", 6) == 0)
245 else if (strncmp(cp, "disks/", 6) == 0)
249 if (strncmp(cp, disk, 4) != 0)
252 while (*cp != '/' && *cp != 0) {
266 static void free_instance(struct fsck_instance *p)
270 free(p->base_device);
274 static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
275 const char *type, const char *opts,
276 int freq, int passno)
280 fs = xzalloc(sizeof(*fs));
281 fs->device = xstrdup(device);
282 fs->mountpt = xstrdup(mntpnt);
283 fs->type = xstrdup(type);
284 fs->opts = xstrdup(opts ? opts : "");
288 /*fs->next = NULL; */
293 filesys_last->next = fs;
299 static void strip_line(char *line)
301 char *p = line + strlen(line) - 1;
304 if (*p != '\n' && *p != '\r')
310 static char *parse_word(char **buf)
318 word = skip_whitespace(word);
319 next = skip_non_whitespace(word);
326 static void parse_escape(char *word)
334 for (p = q = word; *p; q++) {
339 *q = bb_process_escape_sequence(&p);
345 static int parse_fstab_line(char *line, struct fs_info **ret_fs)
347 char *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
352 *strchrnul(line, '#') = '\0'; /* Ignore everything after comment */
355 device = parse_word(&cp);
356 if (!device) return 0; /* Allow blank lines */
357 mntpnt = parse_word(&cp);
358 type = parse_word(&cp);
359 opts = parse_word(&cp);
360 freq = parse_word(&cp);
361 passno = parse_word(&cp);
363 if (!mntpnt || !type)
366 parse_escape(device);
367 parse_escape(mntpnt);
371 parse_escape(passno);
373 if (strchr(type, ','))
376 fs = create_fs_device(device, mntpnt, type ? type : "auto", opts,
377 freq ? atoi(freq) : -1,
378 passno ? atoi(passno) : -1);
383 /* Load the filesystem database from /etc/fstab */
384 static void load_fs_info(const char *filename)
391 f = fopen_or_warn(filename, "r");
397 char *buf = xmalloc_getline(f);
399 r = parse_fstab_line(buf, &fs);
403 bb_error_msg("WARNING: bad format "
404 "on line %d of %s", lineno, filename);
418 "WARNING: Your /etc/fstab does not contain the fsck passno field.\n"
419 "I will kludge around things for you, but you should fix\n"
420 "your /etc/fstab file as soon as you can.\n\n", stderr);
421 for (fs = filesys_info; fs; fs = fs->next) {
427 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
428 static struct fs_info *lookup(char *filesys)
432 for (fs = filesys_info; fs; fs = fs->next) {
433 if (strcmp(filesys, fs->device) == 0
434 || (fs->mountpt && strcmp(filesys, fs->mountpt) == 0)
442 #if DO_PROGRESS_INDICATOR
443 static int progress_active(void)
445 struct fsck_instance *inst;
447 for (inst = instance_list; inst; inst = inst->next) {
448 if (inst->flags & FLAG_DONE)
450 if (inst->flags & FLAG_PROGRESS)
459 * Send a signal to all outstanding fsck child processes
461 static void kill_all_if_cancel_requested(void)
463 static smallint kill_sent;
465 struct fsck_instance *inst;
467 if (!cancel_requested || kill_sent)
470 for (inst = instance_list; inst; inst = inst->next) {
471 if (inst->flags & FLAG_DONE)
473 kill(inst->pid, SIGTERM);
479 * Wait for one child process to exit; when it does, unlink it from
480 * the list of executing child processes, and return it.
482 static struct fsck_instance *wait_one(int flags)
486 struct fsck_instance *inst, *prev;
493 inst = instance_list;
496 while (inst->next && (random() & 1)) {
501 inst->exit_status = 0;
505 inst = prev = NULL; /* for gcc */
507 pid = waitpid(-1, &status, flags);
508 kill_all_if_cancel_requested();
509 if (pid == 0 && (flags & WNOHANG))
512 if (errno == EINTR || errno == EAGAIN)
514 if (errno == ECHILD) {
515 bb_error_msg("wait: no more child process?!?");
518 bb_perror_msg("wait");
522 inst = instance_list;
524 if (inst->pid == pid)
531 if (WIFEXITED(status))
532 status = WEXITSTATUS(status);
533 else if (WIFSIGNALED(status)) {
534 sig = WTERMSIG(status);
535 status = EXIT_UNCORRECTED;
537 printf("Warning... %s %s exited "
539 inst->prog, inst->device, sig);
543 printf("%s %s: status is %x, should never happen\n",
544 inst->prog, inst->device, status);
547 inst->exit_status = status;
549 #if DO_PROGRESS_INDICATOR
550 if (progress && (inst->flags & FLAG_PROGRESS) && !progress_active()) {
551 struct fsck_instance *inst2;
552 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
553 if (inst2->flags & FLAG_DONE)
555 if (strcmp(inst2->type, "ext2") != 0
556 && strcmp(inst2->type, "ext3") != 0
560 /* ext[23], we will send USR1
561 * (request to start displaying progress bar)
563 * If we've just started the fsck, wait a tiny
564 * bit before sending the kill, to give it
565 * time to set up the signal handler
567 if (inst2->start_time >= time(NULL) - 1)
569 kill(inst2->pid, SIGUSR1);
570 inst2->flags |= FLAG_PROGRESS;
578 prev->next = inst->next;
580 instance_list = inst->next;
582 printf("Finished with %s (exit status %d)\n",
583 inst->device, inst->exit_status);
588 #define FLAG_WAIT_ALL 0
589 #define FLAG_WAIT_ATLEAST_ONE 1
591 * Wait until all executing child processes have exited; return the
592 * logical OR of all of their exit code values.
594 static int wait_many(int flags)
596 struct fsck_instance *inst;
597 int global_status = 0;
600 while ((inst = wait_one(wait_flags))) {
601 global_status |= inst->exit_status;
604 if (noexecute && (flags & WNOHANG) && !(random() % 3))
607 if (flags & FLAG_WAIT_ATLEAST_ONE)
608 wait_flags = WNOHANG;
610 return global_status;
614 * Execute a particular fsck program, and link it into the list of
615 * child processes we are waiting for.
617 static void execute(const char *type, const char *device, const char *mntpt,
620 char *argv[num_args + 4]; /* see count below: */
623 struct fsck_instance *inst;
626 inst = xzalloc(sizeof(*inst));
628 argv[0] = xasprintf("fsck.%s", type); /* 1 */
629 for (i = 0; i < num_args; i++)
630 argv[i+1] = args[i]; /* num_args */
633 #if DO_PROGRESS_INDICATOR
634 if (progress && !progress_active()) {
635 if (strcmp(type, "ext2") == 0
636 || strcmp(type, "ext3") == 0
638 argv[argc++] = xasprintf("-C%d", progress_fd); /* 1 */
639 inst->flags |= FLAG_PROGRESS;
644 argv[argc++] = xstrdup(device); /* 1 */
645 argv[argc] = NULL; /* 1 */
647 if (verbose || noexecute) {
648 printf("[%s (%d) -- %s]", argv[0], num_running,
649 mntpt ? mntpt : device);
650 for (i = 0; i < argc; i++)
651 printf(" %s", argv[i]);
655 /* Fork and execute the correct program. */
660 bb_simple_perror_msg(argv[0]);
663 for (i = num_args+1; i < argc; i++)
667 inst->prog = argv[0];
668 inst->type = xstrdup(type);
669 inst->device = xstrdup(device);
670 inst->base_device = base_device(device);
671 inst->start_time = time(NULL);
673 /* Add to the list of running fsck's.
674 * (was adding to the end, but adding to the front is simpler...) */
675 inst->next = instance_list;
676 instance_list = inst;
680 * Run the fsck program on a particular device
682 * If the type is specified using -t, and it isn't prefixed with "no"
683 * (as in "noext2") and only one filesystem type is specified, then
684 * use that type regardless of what is specified in /etc/fstab.
686 * If the type isn't specified by the user, then use either the type
687 * specified in /etc/fstab, or "auto".
689 static void fsck_device(struct fs_info *fs, int interactive)
693 if (strcmp(fs->type, "auto") != 0) {
696 bb_info_msg("using filesystem type '%s' %s",
699 && (fstype[0] != 'n' || fstype[1] != 'o') /* != "no" */
700 && strncmp(fstype, "opts=", 5) != 0
701 && strncmp(fstype, "loop", 4) != 0
702 && !strchr(fstype, ',')
706 bb_info_msg("using filesystem type '%s' %s",
711 bb_info_msg("using filesystem type '%s' %s",
716 execute(type, fs->device, fs->mountpt, interactive);
720 * Returns TRUE if a partition on the same disk is already being
723 static int device_already_active(char *device)
725 struct fsck_instance *inst;
728 if (force_all_parallel)
732 /* Don't check a soft raid disk with any other disk */
734 && (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1)
735 || !strncmp(device, BASE_MD, sizeof(BASE_MD)-1))
741 base = base_device(device);
743 * If we don't know the base device, assume that the device is
744 * already active if there are any fsck instances running.
747 return (instance_list != NULL);
749 for (inst = instance_list; inst; inst = inst->next) {
750 if (!inst->base_device || !strcmp(base, inst->base_device)) {
761 * This function returns true if a particular option appears in a
762 * comma-delimited options list
764 static int opt_in_list(char *opt, char *optlist)
775 s = strstr(s + 1, opt);
778 /* neither "opt.." nor "xxx,opt.."? */
779 if (s != optlist && s[-1] != ',')
781 /* neither "..opt" nor "..opt,xxx"? */
782 if (s[len] != '\0' && s[len] != ',')
788 /* See if the filesystem matches the criteria given by the -t option */
789 static int fs_match(struct fs_info *fs)
791 int n, ret, checked_type;
801 cp = fs_type_list[n];
804 switch (fs_type_flag[n]) {
805 case FS_TYPE_FLAG_NORMAL:
807 if (strcmp(cp, fs->type) == 0)
810 case FS_TYPE_FLAG_NEGOPT:
811 if (opt_in_list(cp, fs->opts))
814 case FS_TYPE_FLAG_OPT:
815 if (!opt_in_list(cp, fs->opts))
821 if (checked_type == 0)
824 return (fs_type_negated ? !ret : ret);
827 /* Check if we should ignore this filesystem. */
828 static int ignore(struct fs_info *fs)
831 * If the pass number is 0, ignore it.
837 * If a specific fstype is specified, and it doesn't match,
843 /* Are we ignoring this type? */
844 if (index_in_strings(ignored_types, fs->type) >= 0)
847 /* We can and want to check this file system type. */
851 /* Check all file systems, using the /etc/fstab table. */
852 static int check_all(void)
855 int status = EXIT_OK;
856 smallint not_done_yet;
861 puts("Checking all filesystems");
864 * Do an initial scan over the filesystem; mark filesystems
865 * which should be ignored as done, and resolve any "auto"
866 * filesystem types (done as a side-effect of calling ignore()).
868 for (fs = filesys_info; fs; fs = fs->next) {
870 fs->flags |= FLAG_DONE;
874 * Find and check the root filesystem.
876 if (!parallel_root) {
877 for (fs = filesys_info; fs; fs = fs->next) {
878 if (LONE_CHAR(fs->mountpt, '/'))
882 if (!skip_root && !ignore(fs)) {
884 status |= wait_many(FLAG_WAIT_ALL);
885 if (status > EXIT_NONDESTRUCT)
888 fs->flags |= FLAG_DONE;
892 * This is for the bone-headed user who enters the root
893 * filesystem twice. Skip root will skip all root entries.
896 for (fs = filesys_info; fs; fs = fs->next)
897 if (LONE_CHAR(fs->mountpt, '/'))
898 fs->flags |= FLAG_DONE;
902 while (not_done_yet) {
906 for (fs = filesys_info; fs; fs = fs->next) {
907 if (cancel_requested)
909 if (fs->flags & FLAG_DONE)
912 * If the filesystem's pass number is higher
913 * than the current pass number, then we don't
916 if (fs->passno > passno) {
921 * If a filesystem on a particular device has
922 * already been spawned, then we need to defer
923 * this to another pass.
925 if (device_already_active(fs->device)) {
930 * Spawn off the fsck process
932 fsck_device(fs, serialize);
933 fs->flags |= FLAG_DONE;
936 * Only do one filesystem at a time, or if we
937 * have a limit on the number of fsck's extant
938 * at one time, apply that limit.
941 || (max_running && (num_running >= max_running))
947 if (cancel_requested)
950 printf("--waiting-- (pass %d)\n", passno);
951 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
952 FLAG_WAIT_ATLEAST_ONE);
955 puts("----------------------------------");
960 kill_all_if_cancel_requested();
961 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
966 * Deal with the fsck -t argument.
967 * Huh, for mount "-t novfat,nfs" means "neither vfat nor nfs"!
968 * Why here we require "-t novfat,nonfs" ??
970 static void compile_fs_type(char *fs_type)
978 while ((s = strchr(s, ','))) {
984 fs_type_list = xzalloc(num * sizeof(fs_type_list[0]));
985 fs_type_flag = xzalloc(num * sizeof(fs_type_flag[0]));
986 fs_type_negated = -1; /* not yet known is it negated or not */
997 if (s[0] == 'n' && s[1] == 'o') { /* "no.." */
1000 } else if (s[0] == '!') {
1005 if (strcmp(s, "loop") == 0)
1006 /* loop is really short-hand for opts=loop */
1007 goto loop_special_case;
1008 if (strncmp(s, "opts=", 5) == 0) {
1011 fs_type_flag[num] = negate ? FS_TYPE_FLAG_NEGOPT : FS_TYPE_FLAG_OPT;
1013 if (fs_type_negated == -1)
1014 fs_type_negated = negate;
1015 if (fs_type_negated != negate)
1016 bb_error_msg_and_die(
1017 "either all or none of the filesystem types passed to -t must be prefixed "
1018 "with 'no' or '!'");
1020 comma = strchr(s, ',');
1021 fs_type_list[num++] = comma ? xstrndup(s, comma-s) : xstrdup(s);
1028 static void parse_args(int argc, char **argv)
1032 char *options = NULL;
1034 int opts_for_fsck = 0;
1036 /* in bss, so already zeroed
1039 instance_list = NULL;
1042 /* TODO: getopt32 */
1043 for (i = 1; i < argc; i++) {
1046 /* "/dev/blk" or "/path" or "UUID=xxx" or "LABEL=xxx" */
1047 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
1048 // FIXME: must check that arg is a blkdev, or resolve
1049 // "/path", "UUID=xxx" or "LABEL=xxx" into block device name
1050 // ("UUID=xxx"/"LABEL=xxx" can probably shifted to fsck.auto duties)
1051 devices = xrealloc(devices, (num_devices+1) * sizeof(devices[0]));
1052 devices[num_devices++] = xstrdup(arg);
1056 if (arg[0] != '-' || opts_for_fsck) {
1057 args = xrealloc(args, (num_args+1) * sizeof(args[0]));
1058 args[num_args++] = xstrdup(arg);
1062 for (j = 1; arg[j]; j++) {
1063 if (opts_for_fsck) {
1065 /* one extra for '\0' */
1066 options = xrealloc(options, optpos + 2);
1067 options[optpos] = arg[j];
1074 #if DO_PROGRESS_INDICATOR
1077 if (arg[++j]) { /* -Cn */
1078 progress_fd = xatoi_u(&arg[j]);
1082 progress_fd = xatoi_u(argv[++i]);
1111 else if (++i < argc)
1115 fstype = xstrdup(tmp);
1116 compile_fs_type(fstype);
1126 /* one extra for '\0' */
1127 options = xrealloc(options, optpos + 2);
1128 options[optpos] = arg[j];
1135 options[optpos + 1] = '\0';
1136 args = xrealloc(args, (num_args+1) * sizeof(args[0]));
1137 args[num_args++] = options;
1142 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1143 force_all_parallel = 1;
1144 tmp = getenv("FSCK_MAX_INST");
1146 max_running = xatoi(tmp);
1149 static void signal_cancel(int sig ATTRIBUTE_UNUSED)
1151 cancel_requested = 1;
1154 int fsck_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1155 int fsck_main(int argc, char **argv)
1161 struct sigaction sa;
1163 memset(&sa, 0, sizeof(sa));
1164 sa.sa_handler = signal_cancel;
1165 sigaction(SIGINT, &sa, 0);
1166 sigaction(SIGTERM, &sa, 0);
1168 setbuf(stdout, NULL);
1170 parse_args(argc, argv);
1173 puts("fsck (busybox "BB_VER", "BB_BT")");
1175 /* Even plain "fsck /dev/hda1" needs fstab to get fs type,
1176 * so we are scanning it anyway */
1177 fstab = getenv("FSTAB_FILE");
1179 fstab = "/etc/fstab";
1180 load_fs_info(fstab);
1182 interactive = (num_devices == 1) | serialize;
1184 /* If -A was specified ("check all"), do that! */
1188 if (num_devices == 0) {
1194 for (i = 0; i < num_devices; i++) {
1195 if (cancel_requested) {
1196 kill_all_if_cancel_requested();
1200 fs = lookup(devices[i]);
1202 fs = create_fs_device(devices[i], 0, "auto", 0, -1, -1);
1203 fsck_device(fs, interactive);
1206 || (max_running && (num_running >= max_running))
1208 struct fsck_instance *inst;
1212 status |= inst->exit_status;
1213 free_instance(inst);
1216 puts("----------------------------------");
1219 status |= wait_many(FLAG_WAIT_ALL);