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 *const ignored_types[] = {
95 static const char *const really_wanted[] = {
107 #define BASE_MD "/dev/md"
109 static char **devices;
111 static int num_devices;
115 #define FS_TYPE_FLAG_NORMAL 0
116 #define FS_TYPE_FLAG_OPT 1
117 #define FS_TYPE_FLAG_NEGOPT 2
118 static char **fs_type_list;
119 static uint8_t *fs_type_flag;
120 static smallint fs_type_negated;
122 static volatile smallint cancel_requested;
123 static smallint doall;
124 static smallint noexecute;
125 static smallint serialize;
126 static smallint skip_root;
127 /* static smallint like_mount; */
128 static smallint notitle;
129 static smallint parallel_root;
130 static smallint force_all_parallel;
132 /* "progress indicator" code is somewhat buggy and ext[23] specific.
133 * We should be filesystem agnostic. IOW: there should be a well-defined
134 * API for fsck.something, NOT ad-hoc hacks in generic fsck. */
135 #define DO_PROGRESS_INDICATOR 0
136 #if DO_PROGRESS_INDICATOR
137 static smallint progress;
138 static int progress_fd;
141 static int num_running;
142 static int max_running;
144 static struct fs_info *filesys_info;
145 static struct fs_info *filesys_last;
146 static struct fsck_instance *instance_list;
149 * Return the "base device" given a particular device; this is used to
150 * assure that we only fsck one partition on a particular drive at any
151 * one time. Otherwise, the disk heads will be seeking all over the
152 * place. If the base device cannot be determined, return NULL.
154 * The base_device() function returns an allocated string which must
157 #if ENABLE_FEATURE_DEVFS
159 * Required for the uber-silly devfs /dev/ide/host1/bus2/target3/lun3
162 static const char *const devfs_hier[] = {
163 "host", "bus", "target", "lun", NULL
167 static char *base_device(const char *device)
170 #if ENABLE_FEATURE_DEVFS
171 const char *const *hier;
175 cp = str = xstrdup(device);
177 /* Skip over /dev/; if it's not present, give up. */
178 if (strncmp(cp, "/dev/", 5) != 0)
183 * For md devices, we treat them all as if they were all
184 * on one disk, since we don't know how to parallelize them.
186 if (cp[0] == 'm' && cp[1] == 'd') {
191 /* Handle DAC 960 devices */
192 if (strncmp(cp, "rd/", 3) == 0) {
194 if (cp[0] != 'c' || !isdigit(cp[1])
195 || cp[2] != 'd' || !isdigit(cp[3]))
201 /* Now let's handle /dev/hd* and /dev/sd* devices.... */
202 if ((cp[0] == 'h' || cp[0] == 's') && cp[1] == 'd') {
204 /* If there's a single number after /dev/hd, skip it */
207 /* What follows must be an alpha char, or give up */
214 #if ENABLE_FEATURE_DEVFS
215 /* Now let's handle devfs (ugh) names */
217 if (strncmp(cp, "ide/", 4) == 0)
219 if (strncmp(cp, "scsi/", 5) == 0)
224 * Now we proceed down the expected devfs hierarchy.
225 * i.e., .../host1/bus2/target3/lun4/...
226 * If we don't find the expected token, followed by
227 * some number of digits at each level, abort.
229 for (hier = devfs_hier; *hier; hier++) {
231 if (strncmp(cp, *hier, len) != 0)
234 while (*cp != '/' && *cp != 0) {
245 /* Now handle devfs /dev/disc or /dev/disk names */
247 if (strncmp(cp, "discs/", 6) == 0)
249 else if (strncmp(cp, "disks/", 6) == 0)
253 if (strncmp(cp, disk, 4) != 0)
256 while (*cp != '/' && *cp != 0) {
270 static void free_instance(struct fsck_instance *p)
274 free(p->base_device);
278 static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
279 const char *type, const char *opts,
280 int freq, int passno)
284 fs = xzalloc(sizeof(*fs));
285 fs->device = xstrdup(device);
286 fs->mountpt = xstrdup(mntpnt);
287 fs->type = xstrdup(type);
288 fs->opts = xstrdup(opts ? opts : "");
292 /*fs->next = NULL; */
297 filesys_last->next = fs;
303 static void strip_line(char *line)
305 char *p = line + strlen(line) - 1;
308 if (*p != '\n' && *p != '\r')
314 static char *parse_word(char **buf)
322 word = skip_whitespace(word);
323 next = skip_non_whitespace(word);
330 static void parse_escape(char *word)
338 for (p = q = word; *p; q++) {
343 *q = bb_process_escape_sequence(&p);
349 static int parse_fstab_line(char *line, struct fs_info **ret_fs)
351 char *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
356 cp = strchr(line, '#');
358 *cp = '\0'; /* Ignore everything after the comment char */
361 device = parse_word(&cp);
362 if (!device) return 0; /* Allow blank lines */
363 mntpnt = parse_word(&cp);
364 type = parse_word(&cp);
365 opts = parse_word(&cp);
366 freq = parse_word(&cp);
367 passno = parse_word(&cp);
369 if (!mntpnt || !type)
372 parse_escape(device);
373 parse_escape(mntpnt);
377 parse_escape(passno);
379 if (strchr(type, ','))
382 fs = create_fs_device(device, mntpnt, type ? type : "auto", opts,
383 freq ? atoi(freq) : -1,
384 passno ? atoi(passno) : -1);
389 /* Load the filesystem database from /etc/fstab */
390 static void load_fs_info(const char *filename)
397 f = fopen_or_warn(filename, "r");
399 /*bb_perror_msg("WARNING: cannot open %s", filename);*/
404 char *buf = xmalloc_getline(f);
406 r = parse_fstab_line(buf, &fs);
410 bb_error_msg("WARNING: bad format "
411 "on line %d of %s", lineno, filename);
425 "WARNING: Your /etc/fstab does not contain the fsck passno field.\n"
426 "I will kludge around things for you, but you should fix\n"
427 "your /etc/fstab file as soon as you can.\n\n", stderr);
428 for (fs = filesys_info; fs; fs = fs->next) {
434 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
435 static struct fs_info *lookup(char *filesys)
439 for (fs = filesys_info; fs; fs = fs->next) {
440 if (strcmp(filesys, fs->device) == 0
441 || (fs->mountpt && strcmp(filesys, fs->mountpt) == 0)
449 #if DO_PROGRESS_INDICATOR
450 static int progress_active(void)
452 struct fsck_instance *inst;
454 for (inst = instance_list; inst; inst = inst->next) {
455 if (inst->flags & FLAG_DONE)
457 if (inst->flags & FLAG_PROGRESS)
466 * Send a signal to all outstanding fsck child processes
468 static void kill_all_if_cancel_requested(void)
470 static smallint kill_sent;
472 struct fsck_instance *inst;
474 if (!cancel_requested || kill_sent)
477 for (inst = instance_list; inst; inst = inst->next) {
478 if (inst->flags & FLAG_DONE)
480 kill(inst->pid, SIGTERM);
486 * Wait for one child process to exit; when it does, unlink it from
487 * the list of executing child processes, and return it.
489 static struct fsck_instance *wait_one(int flags)
493 struct fsck_instance *inst, *prev;
500 inst = instance_list;
503 while (inst->next && (random() & 1)) {
508 inst->exit_status = 0;
512 inst = prev = NULL; /* for gcc */
514 pid = waitpid(-1, &status, flags);
515 kill_all_if_cancel_requested();
516 if (pid == 0 && (flags & WNOHANG))
519 if (errno == EINTR || errno == EAGAIN)
521 if (errno == ECHILD) {
522 bb_error_msg("wait: no more child process?!?");
525 bb_perror_msg("wait");
529 inst = instance_list;
531 if (inst->pid == pid)
538 if (WIFEXITED(status))
539 status = WEXITSTATUS(status);
540 else if (WIFSIGNALED(status)) {
541 sig = WTERMSIG(status);
542 status = EXIT_UNCORRECTED;
544 printf("Warning... %s %s exited "
546 inst->prog, inst->device, sig);
550 printf("%s %s: status is %x, should never happen\n",
551 inst->prog, inst->device, status);
554 inst->exit_status = status;
556 #if DO_PROGRESS_INDICATOR
557 if (progress && (inst->flags & FLAG_PROGRESS) && !progress_active()) {
558 struct fsck_instance *inst2;
559 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
560 if (inst2->flags & FLAG_DONE)
562 if (strcmp(inst2->type, "ext2") != 0
563 && strcmp(inst2->type, "ext3") != 0
567 /* ext[23], we will send USR1
568 * (request to start displaying progress bar)
570 * If we've just started the fsck, wait a tiny
571 * bit before sending the kill, to give it
572 * time to set up the signal handler
574 if (inst2->start_time >= time(NULL) - 1)
576 kill(inst2->pid, SIGUSR1);
577 inst2->flags |= FLAG_PROGRESS;
585 prev->next = inst->next;
587 instance_list = inst->next;
589 printf("Finished with %s (exit status %d)\n",
590 inst->device, inst->exit_status);
595 #define FLAG_WAIT_ALL 0
596 #define FLAG_WAIT_ATLEAST_ONE 1
598 * Wait until all executing child processes have exited; return the
599 * logical OR of all of their exit code values.
601 static int wait_many(int flags)
603 struct fsck_instance *inst;
604 int global_status = 0;
607 while ((inst = wait_one(wait_flags))) {
608 global_status |= inst->exit_status;
611 if (noexecute && (flags & WNOHANG) && !(random() % 3))
614 if (flags & FLAG_WAIT_ATLEAST_ONE)
615 wait_flags = WNOHANG;
617 return global_status;
621 * Execute a particular fsck program, and link it into the list of
622 * child processes we are waiting for.
624 static void execute(const char *type, const char *device, const char *mntpt,
627 char *argv[num_args + 4]; /* see count below: */
630 struct fsck_instance *inst;
633 inst = xzalloc(sizeof(*inst));
635 argv[0] = xasprintf("fsck.%s", type); /* 1 */
636 for (i = 0; i < num_args; i++)
637 argv[i+1] = args[i]; /* num_args */
640 #if DO_PROGRESS_INDICATOR
641 if (progress && !progress_active()) {
642 if (strcmp(type, "ext2") == 0
643 || strcmp(type, "ext3") == 0
645 argv[argc++] = xasprintf("-C%d", progress_fd); /* 1 */
646 inst->flags |= FLAG_PROGRESS;
651 argv[argc++] = xstrdup(device); /* 1 */
652 argv[argc] = NULL; /* 1 */
654 if (verbose || noexecute) {
655 printf("[%s (%d) -- %s]", argv[0], num_running,
656 mntpt ? mntpt : device);
657 for (i = 0; i < argc; i++)
658 printf(" %s", argv[i]);
662 /* Fork and execute the correct program. */
667 bb_perror_msg("%s", argv[0]);
670 for (i = num_args+1; i < argc; i++)
674 inst->prog = argv[0];
675 inst->type = xstrdup(type);
676 inst->device = xstrdup(device);
677 inst->base_device = base_device(device);
678 inst->start_time = time(NULL);
680 /* Add to the list of running fsck's.
681 * (was adding to the end, but adding to the front is simpler...) */
682 inst->next = instance_list;
683 instance_list = inst;
687 * Run the fsck program on a particular device
689 * If the type is specified using -t, and it isn't prefixed with "no"
690 * (as in "noext2") and only one filesystem type is specified, then
691 * use that type regardless of what is specified in /etc/fstab.
693 * If the type isn't specified by the user, then use either the type
694 * specified in /etc/fstab, or "auto".
696 static void fsck_device(struct fs_info *fs, int interactive)
700 if (strcmp(fs->type, "auto") != 0) {
703 bb_info_msg("using filesystem type '%s' %s",
706 && (fstype[0] != 'n' || fstype[1] != 'o') /* != "no" */
707 && strncmp(fstype, "opts=", 5) != 0
708 && strncmp(fstype, "loop", 4) != 0
709 && !strchr(fstype, ',')
713 bb_info_msg("using filesystem type '%s' %s",
718 bb_info_msg("using filesystem type '%s' %s",
723 execute(type, fs->device, fs->mountpt, interactive);
727 * Returns TRUE if a partition on the same disk is already being
730 static int device_already_active(char *device)
732 struct fsck_instance *inst;
735 if (force_all_parallel)
739 /* Don't check a soft raid disk with any other disk */
741 && (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1)
742 || !strncmp(device, BASE_MD, sizeof(BASE_MD)-1))
748 base = base_device(device);
750 * If we don't know the base device, assume that the device is
751 * already active if there are any fsck instances running.
754 return (instance_list != NULL);
756 for (inst = instance_list; inst; inst = inst->next) {
757 if (!inst->base_device || !strcmp(base, inst->base_device)) {
768 * This function returns true if a particular option appears in a
769 * comma-delimited options list
771 static int opt_in_list(char *opt, char *optlist)
782 s = strstr(s + 1, opt);
785 /* neither "opt.." nor "xxx,opt.."? */
786 if (s != optlist && s[-1] != ',')
788 /* neither "..opt" nor "..opt,xxx"? */
789 if (s[len] != '\0' && s[len] != ',')
795 /* See if the filesystem matches the criteria given by the -t option */
796 static int fs_match(struct fs_info *fs)
798 int n, ret, checked_type;
808 cp = fs_type_list[n];
811 switch (fs_type_flag[n]) {
812 case FS_TYPE_FLAG_NORMAL:
814 if (strcmp(cp, fs->type) == 0)
817 case FS_TYPE_FLAG_NEGOPT:
818 if (opt_in_list(cp, fs->opts))
821 case FS_TYPE_FLAG_OPT:
822 if (!opt_in_list(cp, fs->opts))
828 if (checked_type == 0)
831 return (fs_type_negated ? !ret : ret);
834 /* Check if we should ignore this filesystem. */
835 static int ignore(struct fs_info *fs)
838 * If the pass number is 0, ignore it.
844 * If a specific fstype is specified, and it doesn't match,
850 /* Are we ignoring this type? */
851 if (index_in_str_array(ignored_types, fs->type) >= 0)
854 /* We can and want to check this file system type. */
858 /* Check all file systems, using the /etc/fstab table. */
859 static int check_all(void)
862 int status = EXIT_OK;
863 smallint not_done_yet;
868 puts("Checking all filesystems");
871 * Do an initial scan over the filesystem; mark filesystems
872 * which should be ignored as done, and resolve any "auto"
873 * filesystem types (done as a side-effect of calling ignore()).
875 for (fs = filesys_info; fs; fs = fs->next) {
877 fs->flags |= FLAG_DONE;
881 * Find and check the root filesystem.
883 if (!parallel_root) {
884 for (fs = filesys_info; fs; fs = fs->next) {
885 if (LONE_CHAR(fs->mountpt, '/'))
889 if (!skip_root && !ignore(fs)) {
891 status |= wait_many(FLAG_WAIT_ALL);
892 if (status > EXIT_NONDESTRUCT)
895 fs->flags |= FLAG_DONE;
899 * This is for the bone-headed user who enters the root
900 * filesystem twice. Skip root will skip all root entries.
903 for (fs = filesys_info; fs; fs = fs->next)
904 if (LONE_CHAR(fs->mountpt, '/'))
905 fs->flags |= FLAG_DONE;
909 while (not_done_yet) {
913 for (fs = filesys_info; fs; fs = fs->next) {
914 if (cancel_requested)
916 if (fs->flags & FLAG_DONE)
919 * If the filesystem's pass number is higher
920 * than the current pass number, then we don't
923 if (fs->passno > passno) {
928 * If a filesystem on a particular device has
929 * already been spawned, then we need to defer
930 * this to another pass.
932 if (device_already_active(fs->device)) {
937 * Spawn off the fsck process
939 fsck_device(fs, serialize);
940 fs->flags |= FLAG_DONE;
943 * Only do one filesystem at a time, or if we
944 * have a limit on the number of fsck's extant
945 * at one time, apply that limit.
948 || (max_running && (num_running >= max_running))
954 if (cancel_requested)
957 printf("--waiting-- (pass %d)\n", passno);
958 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
959 FLAG_WAIT_ATLEAST_ONE);
962 puts("----------------------------------");
967 kill_all_if_cancel_requested();
968 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
973 * Deal with the fsck -t argument.
974 * Huh, for mount "-t novfat,nfs" means "neither vfat nor nfs"!
975 * Why here we require "-t novfat,nonfs" ??
977 static void compile_fs_type(char *fs_type)
985 while ((s = strchr(s, ','))) {
991 fs_type_list = xzalloc(num * sizeof(fs_type_list[0]));
992 fs_type_flag = xzalloc(num * sizeof(fs_type_flag[0]));
993 fs_type_negated = -1; /* not yet known is it negated or not */
1004 if (s[0] == 'n' && s[1] == 'o') { /* "no.." */
1007 } else if (s[0] == '!') {
1012 if (strcmp(s, "loop") == 0)
1013 /* loop is really short-hand for opts=loop */
1014 goto loop_special_case;
1015 if (strncmp(s, "opts=", 5) == 0) {
1018 fs_type_flag[num] = negate ? FS_TYPE_FLAG_NEGOPT : FS_TYPE_FLAG_OPT;
1020 if (fs_type_negated == -1)
1021 fs_type_negated = negate;
1022 if (fs_type_negated != negate)
1023 bb_error_msg_and_die(
1024 "either all or none of the filesystem types passed to -t must be prefixed "
1025 "with 'no' or '!'");
1027 comma = strchr(s, ',');
1028 fs_type_list[num++] = comma ? xstrndup(s, comma-s) : xstrdup(s);
1035 static void parse_args(int argc, char *argv[])
1039 char *options = NULL;
1041 int opts_for_fsck = 0;
1043 /* in bss, so already zeroed
1046 instance_list = NULL;
1049 /* TODO: getopt32 */
1050 for (i = 1; i < argc; i++) {
1053 /* "/dev/blk" or "/path" or "UUID=xxx" or "LABEL=xxx" */
1054 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
1055 // FIXME: must check that arg is a blkdev, or resolve
1056 // "/path", "UUID=xxx" or "LABEL=xxx" into block device name
1057 // ("UUID=xxx"/"LABEL=xxx" can probably shifted to fsck.auto duties)
1058 devices = xrealloc(devices, (num_devices+1) * sizeof(devices[0]));
1059 devices[num_devices++] = xstrdup(arg);
1063 if (arg[0] != '-' || opts_for_fsck) {
1064 args = xrealloc(args, (num_args+1) * sizeof(args[0]));
1065 args[num_args++] = xstrdup(arg);
1069 for (j = 1; arg[j]; j++) {
1070 if (opts_for_fsck) {
1072 /* one extra for '\0' */
1073 options = xrealloc(options, optpos + 2);
1074 options[optpos] = arg[j];
1081 #if DO_PROGRESS_INDICATOR
1084 if (arg[++j]) { /* -Cn */
1085 progress_fd = xatoi_u(&arg[j]);
1089 progress_fd = xatoi_u(argv[++i]);
1118 else if (++i < argc)
1122 fstype = xstrdup(tmp);
1123 compile_fs_type(fstype);
1133 /* one extra for '\0' */
1134 options = xrealloc(options, optpos + 2);
1135 options[optpos] = arg[j];
1142 options[optpos + 1] = '\0';
1143 args = xrealloc(args, (num_args+1) * sizeof(args[0]));
1144 args[num_args++] = options;
1149 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1150 force_all_parallel = 1;
1151 tmp = getenv("FSCK_MAX_INST");
1153 max_running = xatoi(tmp);
1156 static void signal_cancel(int sig ATTRIBUTE_UNUSED)
1158 cancel_requested = 1;
1161 int fsck_main(int argc, char *argv[]);
1162 int fsck_main(int argc, char *argv[])
1168 struct sigaction sa;
1170 memset(&sa, 0, sizeof(sa));
1171 sa.sa_handler = signal_cancel;
1172 sigaction(SIGINT, &sa, 0);
1173 sigaction(SIGTERM, &sa, 0);
1175 setbuf(stdout, NULL);
1177 parse_args(argc, argv);
1180 puts("fsck (busybox "BB_VER", "BB_BT")");
1182 /* Even plain "fsck /dev/hda1" needs fstab to get fs type,
1183 * so we are scanning it anyway */
1184 fstab = getenv("FSTAB_FILE");
1186 fstab = "/etc/fstab";
1187 load_fs_info(fstab);
1189 interactive = (num_devices == 1) | serialize;
1191 /* If -A was specified ("check all"), do that! */
1195 if (num_devices == 0) {
1201 for (i = 0; i < num_devices; i++) {
1202 if (cancel_requested) {
1203 kill_all_if_cancel_requested();
1207 fs = lookup(devices[i]);
1209 fs = create_fs_device(devices[i], 0, "auto", 0, -1, -1);
1210 fsck_device(fs, interactive);
1213 || (max_running && (num_running >= max_running))
1215 struct fsck_instance *inst;
1219 status |= inst->exit_status;
1220 free_instance(inst);
1223 puts("----------------------------------");
1226 status |= wait_many(FLAG_WAIT_ALL);