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
32 #define EXIT_NONDESTRUCT 1
33 #define EXIT_DESTRUCT 2
34 #define EXIT_UNCORRECTED 4
37 #define FSCK_CANCELED 32 /* Aborted with a signal or ^C */
39 #ifndef DEFAULT_FSTYPE
40 #define DEFAULT_FSTYPE "ext2"
44 * Internal structure for mount tabel entries.
59 #define FLAG_PROGRESS 2
61 * Structure to allow exit codes to be stored
63 struct fsck_instance {
71 char *base_device; /* /dev/hda for /dev/hdaN etc */
72 struct fsck_instance *next;
75 static const char *const ignored_types[] = {
88 static const char *const really_wanted[] = {
100 #define BASE_MD "/dev/md"
102 static volatile int cancel_requested;
104 static char **devices;
106 static int num_devices, num_args;
109 static int noexecute;
110 static int serialize;
111 static int skip_root;
112 static int like_mount;
114 static int parallel_root;
116 static int progress_fd;
117 static int force_all_parallel;
118 static int num_running;
119 static int max_running;
121 static struct fs_info *filesys_info;
122 static struct fs_info *filesys_last;
123 static struct fsck_instance *instance_list;
125 #define FS_TYPE_FLAG_NORMAL 0
126 #define FS_TYPE_FLAG_OPT 1
127 #define FS_TYPE_FLAG_NEGOPT 2
128 static char **fs_type_list;
129 static uint8_t *fs_type_flag;
130 static int fs_type_negated;
133 * Return the "base device" given a particular device; this is used to
134 * assure that we only fsck one partition on a particular drive at any
135 * one time. Otherwise, the disk heads will be seeking all over the
136 * place. If the base device cannot be determined, return NULL.
138 * The base_device() function returns an allocated string which must
141 #if ENABLE_FEATURE_DEVFS
143 * Required for the uber-silly devfs /dev/ide/host1/bus2/target3/lun3
146 static const char *const devfs_hier[] = {
147 "host", "bus", "target", "lun", NULL
151 static char *base_device(const char *device)
154 #if ENABLE_FEATURE_DEVFS
155 const char *const *hier;
159 cp = str = xstrdup(device);
161 /* Skip over /dev/; if it's not present, give up. */
162 if (strncmp(cp, "/dev/", 5) != 0)
167 * For md devices, we treat them all as if they were all
168 * on one disk, since we don't know how to parallelize them.
170 if (cp[0] == 'm' && cp[1] == 'd') {
175 /* Handle DAC 960 devices */
176 if (strncmp(cp, "rd/", 3) == 0) {
178 if (cp[0] != 'c' || !isdigit(cp[1])
179 || cp[2] != 'd' || !isdigit(cp[3]))
185 /* Now let's handle /dev/hd* and /dev/sd* devices.... */
186 if ((cp[0] == 'h' || cp[0] == 's') && cp[1] == 'd') {
188 /* If there's a single number after /dev/hd, skip it */
191 /* What follows must be an alpha char, or give up */
198 #if ENABLE_FEATURE_DEVFS
199 /* Now let's handle devfs (ugh) names */
201 if (strncmp(cp, "ide/", 4) == 0)
203 if (strncmp(cp, "scsi/", 5) == 0)
208 * Now we proceed down the expected devfs hierarchy.
209 * i.e., .../host1/bus2/target3/lun4/...
210 * If we don't find the expected token, followed by
211 * some number of digits at each level, abort.
213 for (hier = devfs_hier; *hier; hier++) {
215 if (strncmp(cp, *hier, len) != 0)
218 while (*cp != '/' && *cp != 0) {
229 /* Now handle devfs /dev/disc or /dev/disk names */
231 if (strncmp(cp, "discs/", 6) == 0)
233 else if (strncmp(cp, "disks/", 6) == 0)
237 if (strncmp(cp, disk, 4) != 0)
240 while (*cp != '/' && *cp != 0) {
254 static void free_instance(struct fsck_instance *p)
258 free(p->base_device);
262 static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
263 const char *type, const char *opts,
264 int freq, int passno)
268 fs = xzalloc(sizeof(*fs));
269 fs->device = xstrdup(device);
270 fs->mountpt = xstrdup(mntpnt);
271 fs->type = xstrdup(type);
272 fs->opts = xstrdup(opts ? opts : "");
276 /*fs->next = NULL; */
281 filesys_last->next = fs;
287 static void strip_line(char *line)
289 char *p = line + strlen(line) - 1;
292 if (*p != '\n' && *p != '\r')
298 static char *parse_word(char **buf)
306 word = skip_whitespace(word);
307 next = skip_non_whitespace(word);
314 static void parse_escape(char *word)
322 for (p = q = word; *p; q++) {
327 *q = bb_process_escape_sequence(&p);
333 static int parse_fstab_line(char *line, struct fs_info **ret_fs)
336 char *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
341 cp = strchr(line, '#');
343 *cp = '\0'; /* Ignore everything after the comment char */
346 device = parse_word(&cp);
347 if (!device) return 0; /* Allow blank lines */
348 mntpnt = parse_word(&cp);
349 type = parse_word(&cp);
350 opts = parse_word(&cp);
351 freq = parse_word(&cp);
352 passno = parse_word(&cp);
354 if (!mntpnt || !type)
357 parse_escape(device);
358 parse_escape(mntpnt);
362 parse_escape(passno);
365 dev = blkid_get_devname(cache, device, NULL);
369 if (strchr(type, ','))
372 fs = create_fs_device(device, mntpnt, type ? type : "auto", opts,
373 freq ? atoi(freq) : -1,
374 passno ? atoi(passno) : -1);
383 static void interpret_type(struct fs_info *fs)
387 if (strcmp(fs->type, "auto") != 0)
389 t = blkid_get_tag_value(cache, "TYPE", fs->device);
396 #define interpret_type(fs) ((void)0)
399 * Load the filesystem database from /etc/fstab
401 static void load_fs_info(const char *filename)
408 f = fopen_or_warn(filename, "r");
410 /*bb_perror_msg("WARNING: cannot open %s", filename);*/
415 char *buf = xmalloc_getline(f);
417 r = parse_fstab_line(buf, &fs);
421 bb_error_msg("WARNING: bad format "
422 "on line %d of %s\n", lineno, filename);
436 "WARNING: Your /etc/fstab does not contain the fsck passno field.\n"
437 "I will kludge around things for you, but you should fix\n"
438 "your /etc/fstab file as soon as you can.\n\n", stderr);
439 for (fs = filesys_info; fs; fs = fs->next) {
445 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
446 static struct fs_info *lookup(char *filesys)
450 for (fs = filesys_info; fs; fs = fs->next) {
451 if (strcmp(filesys, fs->device) == 0
452 || (fs->mountpt && strcmp(filesys, fs->mountpt) == 0)
460 static int progress_active(void)
462 struct fsck_instance *inst;
464 for (inst = instance_list; inst; inst = inst->next) {
465 if (inst->flags & FLAG_DONE)
467 if (inst->flags & FLAG_PROGRESS)
474 * Execute a particular fsck program, and link it into the list of
475 * child processes we are waiting for.
477 static int execute(const char *type, const char *device, const char *mntpt,
480 char *argv[num_args + 4]; /* see count below: */
483 struct fsck_instance *inst, *p;
486 inst = xzalloc(sizeof(*inst));
488 argv[0] = xasprintf("fsck.%s", type); /* 1 */
489 for (i = 0; i < num_args; i++)
490 argv[i+1] = args[i]; /* num_args */
493 if (progress && !progress_active()) {
494 if (strcmp(type, "ext2") == 0
495 || strcmp(type, "ext3") == 0
497 argv[argc++] = xasprintf("-C%d", progress_fd); /* 1 */
498 inst->flags |= FLAG_PROGRESS;
502 argv[argc++] = xstrdup(device); /* 1 */
503 argv[argc] = 0; /* 1 */
505 if (verbose || noexecute) {
506 printf("[%s (%d) -- %s]", argv[0], num_running,
507 mntpt ? mntpt : device);
508 for (i = 0; i < argc; i++)
509 printf(" %s", argv[i]);
513 /* Fork and execute the correct program. */
516 else if ((pid = fork()) < 0) {
517 bb_perror_msg("fork");
524 execvp(argv[0], argv);
525 bb_perror_msg_and_die("%s", argv[0]);
528 for (i = num_args+1; i < argc; i++)
532 inst->prog = argv[0];
533 inst->type = xstrdup(type);
534 inst->device = xstrdup(device);
535 inst->base_device = base_device(device);
536 inst->start_time = time(0);
540 * Find the end of the list, so we add the instance on at the end.
542 if (!instance_list) {
543 instance_list = inst;
554 * Send a signal to all outstanding fsck child processes
556 static void kill_all_if_cancel_requested(void)
558 static int kill_sent;
560 struct fsck_instance *inst;
562 if (!cancel_requested || kill_sent)
565 for (inst = instance_list; inst; inst = inst->next) {
566 if (inst->flags & FLAG_DONE)
568 kill(inst->pid, SIGTERM);
574 * Wait for one child process to exit; when it does, unlink it from
575 * the list of executing child processes, and return it.
577 static struct fsck_instance *wait_one(int flags)
581 struct fsck_instance *inst, *inst2, *prev;
588 inst = instance_list;
591 while (inst->next && (random() & 1)) {
596 inst->exit_status = 0;
601 * gcc -Wall fails saving throw against stupidity
602 * (inst and prev are thought to be uninitialized variables)
607 pid = waitpid(-1, &status, flags);
608 kill_all_if_cancel_requested();
609 if (pid == 0 && (flags & WNOHANG))
612 if (errno == EINTR || errno == EAGAIN)
614 if (errno == ECHILD) {
615 bb_error_msg("wait: no more child process?!?");
621 for (prev = 0, inst = instance_list;
623 prev = inst, inst = inst->next) {
624 if (inst->pid == pid)
629 if (WIFEXITED(status))
630 status = WEXITSTATUS(status);
631 else if (WIFSIGNALED(status)) {
632 sig = WTERMSIG(status);
634 status = EXIT_UNCORRECTED;
636 printf("Warning... %s for device %s exited "
638 inst->prog, inst->device, sig);
642 printf("%s %s: status is %x, should never happen.\n",
643 inst->prog, inst->device, status);
646 inst->exit_status = status;
647 if (progress && (inst->flags & FLAG_PROGRESS) &&
648 !progress_active()) {
649 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
650 if (inst2->flags & FLAG_DONE)
652 if (strcmp(inst2->type, "ext2") &&
653 strcmp(inst2->type, "ext3"))
656 * If we've just started the fsck, wait a tiny
657 * bit before sending the kill, to give it
658 * time to set up the signal handler
660 if (inst2->start_time < time(0)+2) {
663 kill(inst2->pid, SIGUSR1);
667 kill(inst2->pid, SIGUSR1);
668 inst2->flags |= FLAG_PROGRESS;
674 prev->next = inst->next;
676 instance_list = inst->next;
678 printf("Finished with %s (exit status %d)\n",
679 inst->device, inst->exit_status);
684 #define FLAG_WAIT_ALL 0
685 #define FLAG_WAIT_ATLEAST_ONE 1
687 * Wait until all executing child processes have exited; return the
688 * logical OR of all of their exit code values.
690 static int wait_many(int flags)
692 struct fsck_instance *inst;
693 int global_status = 0;
696 while ((inst = wait_one(wait_flags))) {
697 global_status |= inst->exit_status;
700 if (noexecute && (flags & WNOHANG) && !(random() % 3))
703 if (flags & FLAG_WAIT_ATLEAST_ONE)
704 wait_flags = WNOHANG;
706 return global_status;
710 * Run the fsck program on a particular device
712 * If the type is specified using -t, and it isn't prefixed with "no"
713 * (as in "noext2") and only one filesystem type is specified, then
714 * use that type regardless of what is specified in /etc/fstab.
716 * If the type isn't specified by the user, then use either the type
717 * specified in /etc/fstab, or DEFAULT_FSTYPE.
719 static void fsck_device(struct fs_info *fs, int interactive)
726 type = DEFAULT_FSTYPE;
727 if (strcmp(fs->type, "auto") != 0)
730 && (fstype[0] != 'n' || fstype[1] != 'o') /* != "no" */
731 && strncmp(fstype, "opts=", 5) != 0
732 && strncmp(fstype, "loop", 4) != 0
733 && !strchr(fstype, ',')
738 retval = execute(type, fs->device, fs->mountpt, interactive);
740 bb_error_msg("error %d while executing fsck.%s for %s",
741 retval, type, fs->device);
747 * Deal with the fsck -t argument.
749 static void compile_fs_type(char *fs_type)
757 while ((cp = strchr(cp, ','))) {
763 fs_type_list = xzalloc(num * sizeof(fs_type_list[0]));
764 fs_type_flag = xzalloc(num * sizeof(fs_type_flag[0]));
765 fs_type_negated = -1;
770 list = xstrdup(fs_type);
772 s = strtok(list, ",");
775 if (s[0] == 'n' && s[1] == 'o') { /* "no.." */
778 } else if (s[0] == '!') {
782 if (strcmp(s, "loop") == 0)
783 /* loop is really short-hand for opts=loop */
784 goto loop_special_case;
785 if (strncmp(s, "opts=", 5) == 0) {
788 fs_type_flag[num] = negate ? FS_TYPE_FLAG_NEGOPT : FS_TYPE_FLAG_OPT;
790 if (fs_type_negated == -1)
791 fs_type_negated = negate;
792 if (fs_type_negated != negate)
793 bb_error_msg_and_die(
794 "Either all or none of the filesystem types passed to -t must be prefixed\n"
795 "with 'no' or '!'.");
797 fs_type_list[num++] = xstrdup(s);
798 s = strtok(NULL, ",");
804 * This function returns true if a particular option appears in a
805 * comma-delimited options list
807 static int opt_in_list(char *opt, char *optlist)
813 list = xstrdup(optlist);
815 s = strtok(list, ",");
817 if (strcmp(s, opt) == 0) {
821 s = strtok(NULL, ",");
827 /* See if the filesystem matches the criteria given by the -t option */
828 static int fs_match(struct fs_info *fs)
830 int n, ret = 0, checked_type = 0;
836 for (n = 0; (cp = fs_type_list[n]); n++) {
837 switch (fs_type_flag[n]) {
838 case FS_TYPE_FLAG_NORMAL:
840 if (strcmp(cp, fs->type) == 0)
843 case FS_TYPE_FLAG_NEGOPT:
844 if (opt_in_list(cp, fs->opts))
847 case FS_TYPE_FLAG_OPT:
848 if (!opt_in_list(cp, fs->opts))
853 if (checked_type == 0)
856 return (fs_type_negated ? !ret : ret);
859 /* Check if we should ignore this filesystem. */
860 static int ignore(struct fs_info *fs)
863 * If the pass number is 0, ignore it.
871 * If a specific fstype is specified, and it doesn't match,
877 /* Are we ignoring this type? */
878 if (index_in_str_array(ignored_types, fs->type) >= 0)
881 /* Do we really really want to check this fs? */
882 wanted = index_in_str_array(really_wanted, fs->type) >= 0;
884 /* See if the <fsck.fs> program is available. */
885 s = find_fsck(fs->type);
888 bb_error_msg("cannot check %s: fsck.%s not found",
889 fs->device, fs->type);
894 /* We can and want to check this file system type. */
899 * Returns TRUE if a partition on the same disk is already being
902 static int device_already_active(char *device)
904 struct fsck_instance *inst;
907 if (force_all_parallel)
911 /* Don't check a soft raid disk with any other disk */
913 && (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1)
914 || !strncmp(device, BASE_MD, sizeof(BASE_MD)-1))
920 base = base_device(device);
922 * If we don't know the base device, assume that the device is
923 * already active if there are any fsck instances running.
926 return (instance_list != 0);
928 for (inst = instance_list; inst; inst = inst->next) {
929 if (!inst->base_device || !strcmp(base, inst->base_device)) {
939 /* Check all file systems, using the /etc/fstab table. */
940 static int check_all(void)
942 struct fs_info *fs = NULL;
943 int status = EXIT_OK;
944 int not_done_yet = 1;
949 puts("Checking all filesystems");
952 * Do an initial scan over the filesystem; mark filesystems
953 * which should be ignored as done, and resolve any "auto"
954 * filesystem types (done as a side-effect of calling ignore()).
956 for (fs = filesys_info; fs; fs = fs->next) {
958 fs->flags |= FLAG_DONE;
962 * Find and check the root filesystem.
964 if (!parallel_root) {
965 for (fs = filesys_info; fs; fs = fs->next) {
966 if (LONE_CHAR(fs->mountpt, '/'))
970 if (!skip_root && !ignore(fs)) {
972 status |= wait_many(FLAG_WAIT_ALL);
973 if (status > EXIT_NONDESTRUCT)
976 fs->flags |= FLAG_DONE;
980 * This is for the bone-headed user who enters the root
981 * filesystem twice. Skip root will skep all root entries.
984 for (fs = filesys_info; fs; fs = fs->next)
985 if (LONE_CHAR(fs->mountpt, '/'))
986 fs->flags |= FLAG_DONE;
988 while (not_done_yet) {
992 for (fs = filesys_info; fs; fs = fs->next) {
993 if (cancel_requested)
995 if (fs->flags & FLAG_DONE)
998 * If the filesystem's pass number is higher
999 * than the current pass number, then we don't
1002 if (fs->passno > passno) {
1007 * If a filesystem on a particular device has
1008 * already been spawned, then we need to defer
1009 * this to another pass.
1011 if (device_already_active(fs->device)) {
1016 * Spawn off the fsck process
1018 fsck_device(fs, serialize);
1019 fs->flags |= FLAG_DONE;
1022 * Only do one filesystem at a time, or if we
1023 * have a limit on the number of fsck's extant
1024 * at one time, apply that limit.
1027 || (max_running && (num_running >= max_running))
1033 if (cancel_requested)
1036 printf("--waiting-- (pass %d)\n", passno);
1037 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
1038 FLAG_WAIT_ATLEAST_ONE);
1041 puts("----------------------------------");
1046 kill_all_if_cancel_requested();
1047 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
1051 static void signal_cancel(int sig ATTRIBUTE_UNUSED)
1053 cancel_requested = 1;
1056 static int string_to_int(const char *s)
1060 if (!s) bb_show_usage();
1061 n = bb_strtou(s, NULL, 0);
1062 if (errno || n < 0) bb_show_usage();
1066 static void parse_args(int argc, char *argv[])
1070 char *options = NULL;
1072 int opts_for_fsck = 0;
1073 struct sigaction sa;
1076 * Set up signal action
1078 memset(&sa, 0, sizeof(sa));
1079 sa.sa_handler = signal_cancel;
1080 sigaction(SIGINT, &sa, 0);
1081 sigaction(SIGTERM, &sa, 0);
1087 /* TODO: getopt32 */
1088 for (i = 1; i < argc; i++) {
1090 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
1093 dev = blkid_get_devname(cache, arg, NULL);
1094 if (!dev && strchr(arg, '=')) {
1096 * Check to see if we failed because
1097 * /proc/partitions isn't found.
1099 if (access("/proc/partitions", R_OK) < 0) {
1100 bb_perror_msg_and_die(
1101 "cannot open /proc/partitions (is /proc mounted?)");
1104 * Check to see if this is because
1105 * we're not running as root
1108 bb_error_msg_and_die(
1109 "must be root to scan for matching filesystems: %s\n", arg);
1111 bb_error_msg_and_die(
1112 "cannot find matching filesystem: %s", arg);
1114 devices = xrealloc(devices, (num_devices+1) * sizeof(devices[0]));
1115 devices[num_devices++] = dev ? dev : xstrdup(arg);
1117 devices = xrealloc(devices, (num_devices+1) * sizeof(devices[0]));
1118 devices[num_devices++] = xstrdup(arg);
1121 if (arg[0] != '-' || opts_for_fsck) {
1122 args = xrealloc(args, (num_args+1) * sizeof(args[0]));
1123 args[num_args++] = xstrdup(arg);
1126 for (j = 1; arg[j]; j++) {
1127 if (opts_for_fsck) {
1129 /* one extra for '\0' */
1130 options = xrealloc(options, optpos + 2);
1131 options[optpos] = arg[j];
1140 if (arg[++j]) { /* -Cn */
1141 progress_fd = string_to_int(&arg[j]);
1145 progress_fd = string_to_int(argv[++i]);
1173 else if (++i < argc)
1177 fstype = xstrdup(tmp);
1178 compile_fs_type(fstype);
1188 /* one extra for '\0' */
1189 options = xrealloc(options, optpos + 2);
1190 options[optpos] = arg[j];
1197 options[optpos + 1] = '\0';
1198 args = xrealloc(args, (num_args+1) * sizeof(args[0]));
1199 args[num_args++] = options;
1204 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1205 force_all_parallel++;
1206 tmp = getenv("FSCK_MAX_INST");
1208 max_running = xatoi(tmp);
1211 int fsck_main(int argc, char *argv[])
1214 int interactive = 0;
1218 setbuf(stdout, NULL);
1219 /*setvbuf(stdout, NULL, _IONBF, BUFSIZ);*/
1220 /*setvbuf(stderr, NULL, _IONBF, BUFSIZ);*/
1222 /*blkid_get_cache(&cache, NULL);*/
1223 parse_args(argc, argv);
1226 puts("fsck (busybox "BB_VER", "BB_BT")");
1228 fstab = getenv("FSTAB_FILE");
1230 fstab = "/etc/fstab";
1231 load_fs_info(fstab);
1233 if (num_devices == 1 || serialize)
1236 /* If -A was specified ("check all"), do that! */
1240 if (num_devices == 0) {
1246 for (i = 0; i < num_devices; i++) {
1247 if (cancel_requested) {
1248 kill_all_if_cancel_requested();
1251 fs = lookup(devices[i]);
1253 fs = create_fs_device(devices[i], 0, "auto", 0, -1, -1);
1255 fsck_device(fs, interactive);
1257 || (max_running && (num_running >= max_running))
1259 struct fsck_instance *inst;
1263 status |= inst->exit_status;
1264 free_instance(inst);
1267 puts("----------------------------------");
1270 status |= wait_many(FLAG_WAIT_ALL);
1271 /*blkid_put_cache(cache);*/