1 /* vi: set sw=4 ts=4: */
3 * pfsck --- 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 #include <sys/types.h>
45 #include "blkid/blkid.h"
52 #define _PATH_MNTTAB "/etc/fstab"
59 #ifndef DEFAULT_FSTYPE
60 #define DEFAULT_FSTYPE "ext2"
63 #define MAX_DEVICES 32
67 * Internal structure for mount tabel entries.
82 #define FLAG_PROGRESS 2
85 * Structure to allow exit codes to be stored
87 struct fsck_instance {
96 struct fsck_instance *next;
102 * Return the "base device" given a particular device; this is used to
103 * assure that we only fsck one partition on a particular drive at any
104 * one time. Otherwise, the disk heads will be seeking all over the
105 * place. If the base device cannot be determined, return NULL.
107 * The base_device() function returns an allocated string which must
113 #ifdef CONFIG_FEATURE_DEVFS
115 * Required for the uber-silly devfs /dev/ide/host1/bus2/target3/lun3
118 static const char * const devfs_hier[] = {
119 "host", "bus", "target", "lun", 0
123 static char *base_device(const char *device)
126 #ifdef CONFIG_FEATURE_DEVFS
127 const char * const *hier;
132 cp = str = xstrdup(device);
134 /* Skip over /dev/; if it's not present, give up. */
135 if (strncmp(cp, "/dev/", 5) != 0)
140 * For md devices, we treat them all as if they were all
141 * on one disk, since we don't know how to parallelize them.
143 if (cp[0] == 'm' && cp[1] == 'd') {
148 /* Handle DAC 960 devices */
149 if (strncmp(cp, "rd/", 3) == 0) {
151 if (cp[0] != 'c' || cp[2] != 'd' ||
152 !isdigit(cp[1]) || !isdigit(cp[3]))
158 /* Now let's handle /dev/hd* and /dev/sd* devices.... */
159 if ((cp[0] == 'h' || cp[0] == 's') && (cp[1] == 'd')) {
161 /* If there's a single number after /dev/hd, skip it */
164 /* What follows must be an alpha char, or give up */
171 #ifdef CONFIG_FEATURE_DEVFS
172 /* Now let's handle devfs (ugh) names */
174 if (strncmp(cp, "ide/", 4) == 0)
176 if (strncmp(cp, "scsi/", 5) == 0)
181 * Now we proceed down the expected devfs hierarchy.
182 * i.e., .../host1/bus2/target3/lun4/...
183 * If we don't find the expected token, followed by
184 * some number of digits at each level, abort.
186 for (hier = devfs_hier; *hier; hier++) {
188 if (strncmp(cp, *hier, len) != 0)
191 while (*cp != '/' && *cp != 0) {
202 /* Now handle devfs /dev/disc or /dev/disk names */
204 if (strncmp(cp, "discs/", 6) == 0)
206 else if (strncmp(cp, "disks/", 6) == 0)
210 if (strncmp(cp, disk, 4) != 0)
213 while (*cp != '/' && *cp != 0) {
229 static const char * const ignored_types[] = {
241 static const char * const really_wanted[] = {
252 #define BASE_MD "/dev/md"
255 * Global variables for options
257 static char *devices[MAX_DEVICES];
258 static char *args[MAX_ARGS];
259 static int num_devices, num_args;
263 static int noexecute;
264 static int serialize;
265 static int skip_root;
266 static int like_mount;
268 static int parallel_root;
270 static int progress_fd;
271 static int force_all_parallel;
272 static int num_running;
273 static int max_running;
274 static volatile int cancel_requested;
275 static int kill_sent;
277 static struct fs_info *filesys_info, *filesys_last;
278 static struct fsck_instance *instance_list;
279 static char *fsck_path;
280 static blkid_cache cache;
282 static char *string_copy(const char *s)
292 static int string_to_int(const char *s)
297 l = strtol(s, &p, 0);
298 if (*p || l == LONG_MIN || l == LONG_MAX || l < 0 || l > INT_MAX)
304 static char *skip_over_blank(char *cp)
306 while (*cp && isspace(*cp))
311 static char *skip_over_word(char *cp)
313 while (*cp && !isspace(*cp))
318 static void strip_line(char *line)
323 p = line + strlen(line) - 1;
324 if ((*p == '\n') || (*p == '\r'))
331 static char *parse_word(char **buf)
339 word = skip_over_blank(word);
340 next = skip_over_word(word);
347 static void parse_escape(char *word)
355 for (p = q = word; *p; q++) {
360 *q = bb_process_escape_sequence(&p);
366 static void free_instance(struct fsck_instance *i)
373 free(i->base_device);
377 static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
378 const char *type, const char *opts,
379 int freq, int passno)
383 if (!(fs = malloc(sizeof(struct fs_info))))
386 fs->device = string_copy(device);
387 fs->mountpt = string_copy(mntpnt);
388 fs->type = string_copy(type);
389 fs->opts = string_copy(opts ? opts : "");
398 filesys_last->next = fs;
406 static int parse_fstab_line(char *line, struct fs_info **ret_fs)
408 char *dev, *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
413 if ((cp = strchr(line, '#')))
414 *cp = 0; /* Ignore everything after the comment char */
417 device = parse_word(&cp);
418 mntpnt = parse_word(&cp);
419 type = parse_word(&cp);
420 opts = parse_word(&cp);
421 freq = parse_word(&cp);
422 passno = parse_word(&cp);
425 return 0; /* Allow blank lines */
427 if (!mntpnt || !type)
430 parse_escape(device);
431 parse_escape(mntpnt);
435 parse_escape(passno);
437 dev = blkid_get_devname(cache, device, NULL);
441 if (strchr(type, ','))
444 fs = create_fs_device(device, mntpnt, type ? type : "auto", opts,
445 freq ? atoi(freq) : -1,
446 passno ? atoi(passno) : -1);
456 static void interpret_type(struct fs_info *fs)
460 if (strcmp(fs->type, "auto") != 0)
462 t = blkid_get_tag_value(cache, "TYPE", fs->device);
470 * Load the filesystem database from /etc/fstab
472 static void load_fs_info(const char *filename)
480 if ((f = fopen(filename, "r")) == NULL) {
481 bb_perror_msg("WARNING: cannot open %s", filename);
486 if (!fgets(buf, sizeof(buf), f))
488 buf[sizeof(buf)-1] = 0;
489 if (parse_fstab_line(buf, &fs) < 0) {
490 bb_error_msg("WARNING: bad format "
491 "on line %d of %s\n", lineno, filename);
506 "WARNING: Your /etc/fstab does not contain the fsck passno\n"
507 " field. I will kludge around things for you, but you\n"
508 " should fix your /etc/fstab file as soon as you can.\n\n", stderr);
510 for (fs = filesys_info; fs; fs = fs->next) {
516 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
517 static struct fs_info *lookup(char *filesys)
521 /* No filesys name given. */
525 for (fs = filesys_info; fs; fs = fs->next) {
526 if (!strcmp(filesys, fs->device) ||
527 (fs->mountpt && !strcmp(filesys, fs->mountpt)))
534 /* Find fsck program for a given fs type. */
535 static char *find_fsck(char *type)
539 char *p = string_copy(fsck_path);
542 /* Are we looking for a program or just a type? */
543 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
545 for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
546 s = xasprintf(tpl, s, type);
547 if (stat(s, &st) == 0) break;
554 static int progress_active(void)
556 struct fsck_instance *inst;
558 for (inst = instance_list; inst; inst = inst->next) {
559 if (inst->flags & FLAG_DONE)
561 if (inst->flags & FLAG_PROGRESS)
568 * Execute a particular fsck program, and link it into the list of
569 * child processes we are waiting for.
571 static int execute(const char *type, const char *device, const char *mntpt,
577 struct fsck_instance *inst, *p;
580 inst = malloc(sizeof(struct fsck_instance));
583 memset(inst, 0, sizeof(struct fsck_instance));
585 prog = xasprintf("fsck.%s", type);
589 for (i=0; i <num_args; i++)
590 argv[argc++] = string_copy(args[i]);
592 if (progress && !progress_active()) {
593 if ((strcmp(type, "ext2") == 0) ||
594 (strcmp(type, "ext3") == 0)) {
596 snprintf(tmp, 80, "-C%d", progress_fd);
597 argv[argc++] = string_copy(tmp);
598 inst->flags |= FLAG_PROGRESS;
602 argv[argc++] = string_copy(device);
607 bb_error_msg("%s: not found", prog);
611 if (verbose || noexecute) {
612 printf("[%s (%d) -- %s] ", s, num_running,
613 mntpt ? mntpt : device);
614 for (i=0; i < argc; i++)
615 printf("%s ", argv[i]);
619 /* Fork and execute the correct program. */
622 else if ((pid = fork()) < 0) {
625 } else if (pid == 0) {
628 (void) execv(s, argv);
629 bb_perror_msg_and_die("%s", argv[0]);
632 for (i = 1; i < argc; i++)
638 inst->type = string_copy(type);
639 inst->device = string_copy(device);
640 inst->base_device = base_device(device);
641 inst->start_time = time(0);
645 * Find the end of the list, so we add the instance on at the end.
647 for (p = instance_list; p && p->next; p = p->next);
652 instance_list = inst;
658 * Send a signal to all outstanding fsck child processes
660 static int kill_all(int signum)
662 struct fsck_instance *inst;
665 for (inst = instance_list; inst; inst = inst->next) {
666 if (inst->flags & FLAG_DONE)
668 kill(inst->pid, signum);
675 * Wait for one child process to exit; when it does, unlink it from
676 * the list of executing child processes, and return it.
678 static struct fsck_instance *wait_one(int flags)
682 struct fsck_instance *inst, *inst2, *prev;
689 inst = instance_list;
692 while (inst->next && (random() & 1)) {
697 inst->exit_status = 0;
702 * gcc -Wall fails saving throw against stupidity
703 * (inst and prev are thought to be uninitialized variables)
708 pid = waitpid(-1, &status, flags);
709 if (cancel_requested && !kill_sent) {
713 if ((pid == 0) && (flags & WNOHANG))
716 if ((errno == EINTR) || (errno == EAGAIN))
718 if (errno == ECHILD) {
719 bb_error_msg("wait: no more child process?!?");
725 for (prev = 0, inst = instance_list;
727 prev = inst, inst = inst->next) {
728 if (inst->pid == pid)
733 if (WIFEXITED(status))
734 status = WEXITSTATUS(status);
735 else if (WIFSIGNALED(status)) {
736 sig = WTERMSIG(status);
738 status = EXIT_UNCORRECTED;
740 printf("Warning... %s for device %s exited "
742 inst->prog, inst->device, sig);
746 printf("%s %s: status is %x, should never happen.\n",
747 inst->prog, inst->device, status);
750 inst->exit_status = status;
751 if (progress && (inst->flags & FLAG_PROGRESS) &&
752 !progress_active()) {
753 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
754 if (inst2->flags & FLAG_DONE)
756 if (strcmp(inst2->type, "ext2") &&
757 strcmp(inst2->type, "ext3"))
760 * If we've just started the fsck, wait a tiny
761 * bit before sending the kill, to give it
762 * time to set up the signal handler
764 if (inst2->start_time < time(0)+2) {
767 kill(inst2->pid, SIGUSR1);
771 kill(inst2->pid, SIGUSR1);
772 inst2->flags |= FLAG_PROGRESS;
778 prev->next = inst->next;
780 instance_list = inst->next;
782 printf("Finished with %s (exit status %d)\n",
783 inst->device, inst->exit_status);
788 #define FLAG_WAIT_ALL 0
789 #define FLAG_WAIT_ATLEAST_ONE 1
791 * Wait until all executing child processes have exited; return the
792 * logical OR of all of their exit code values.
794 static int wait_many(int flags)
796 struct fsck_instance *inst;
797 int global_status = 0;
800 while ((inst = wait_one(wait_flags))) {
801 global_status |= inst->exit_status;
804 if (noexecute && (flags & WNOHANG) && !(random() % 3))
807 if (flags & FLAG_WAIT_ATLEAST_ONE)
808 wait_flags = WNOHANG;
810 return global_status;
814 * Run the fsck program on a particular device
816 * If the type is specified using -t, and it isn't prefixed with "no"
817 * (as in "noext2") and only one filesystem type is specified, then
818 * use that type regardless of what is specified in /etc/fstab.
820 * If the type isn't specified by the user, then use either the type
821 * specified in /etc/fstab, or DEFAULT_FSTYPE.
823 static void fsck_device(struct fs_info *fs, int interactive)
830 if (strcmp(fs->type, "auto") != 0)
832 else if (fstype && strncmp(fstype, "no", 2) &&
833 strncmp(fstype, "opts=", 5) && strncmp(fstype, "loop", 4) &&
834 !strchr(fstype, ','))
837 type = DEFAULT_FSTYPE;
840 retval = execute(type, fs->device, fs->mountpt, interactive);
842 bb_error_msg("error %d while executing fsck.%s for %s",
843 retval, type, fs->device);
850 * Deal with the fsck -t argument.
852 struct fs_type_compile {
858 #define FS_TYPE_NORMAL 0
859 #define FS_TYPE_OPT 1
860 #define FS_TYPE_NEGOPT 2
862 static const char fs_type_syntax_error[] =
863 "Either all or none of the filesystem types passed to -t must be prefixed\n"
866 static void compile_fs_type(char *fs_type, struct fs_type_compile *cmp)
870 int negate, first_negate = 1;
873 for (cp=fs_type; *cp; cp++) {
879 cmp->list = xzalloc(num * sizeof(char *));
880 cmp->type = xzalloc(num * sizeof(int));
886 list = string_copy(fs_type);
888 s = strtok(list, ",");
891 if (strncmp(s, "no", 2) == 0) {
894 } else if (*s == '!') {
898 if (strcmp(s, "loop") == 0)
899 /* loop is really short-hand for opts=loop */
900 goto loop_special_case;
901 else if (strncmp(s, "opts=", 5) == 0) {
904 cmp->type[num] = negate ? FS_TYPE_NEGOPT : FS_TYPE_OPT;
907 cmp->negate = negate;
910 if ((negate && !cmp->negate) ||
911 (!negate && cmp->negate)) {
912 bb_error_msg_and_die("%s", fs_type_syntax_error);
915 cmp->list[num++] = string_copy(s);
916 s = strtok(NULL, ",");
922 * This function returns true if a particular option appears in a
923 * comma-delimited options list
925 static int opt_in_list(char *opt, char *optlist)
931 list = string_copy(optlist);
933 s = strtok(list, ",");
935 if (strcmp(s, opt) == 0) {
939 s = strtok(NULL, ",");
945 /* See if the filesystem matches the criteria given by the -t option */
946 static int fs_match(struct fs_info *fs, struct fs_type_compile *cmp)
948 int n, ret = 0, checked_type = 0;
951 if (cmp->list == 0 || cmp->list[0] == 0)
954 for (n=0; (cp = cmp->list[n]); n++) {
955 switch (cmp->type[n]) {
958 if (strcmp(cp, fs->type) == 0) {
963 if (opt_in_list(cp, fs->opts))
967 if (!opt_in_list(cp, fs->opts))
972 if (checked_type == 0)
974 return (cmp->negate ? !ret : ret);
977 /* Check if we should ignore this filesystem. */
978 static int ignore(struct fs_info *fs)
984 * If the pass number is 0, ignore it.
992 * If a specific fstype is specified, and it doesn't match,
995 if (!fs_match(fs, &fs_type_compiled)) return 1;
997 /* Are we ignoring this type? */
998 if (index_in_str_array(ignored_types, fs->type) >= 0)
1001 /* Do we really really want to check this fs? */
1002 wanted = index_in_str_array(really_wanted, fs->type) >= 0;
1004 /* See if the <fsck.fs> program is available. */
1005 s = find_fsck(fs->type);
1008 bb_error_msg("cannot check %s: fsck.%s not found",
1009 fs->device, fs->type);
1014 /* We can and want to check this file system type. */
1019 * Returns TRUE if a partition on the same disk is already being
1022 static int device_already_active(char *device)
1024 struct fsck_instance *inst;
1027 if (force_all_parallel)
1031 /* Don't check a soft raid disk with any other disk */
1032 if (instance_list &&
1033 (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1) ||
1034 !strncmp(device, BASE_MD, sizeof(BASE_MD)-1)))
1038 base = base_device(device);
1040 * If we don't know the base device, assume that the device is
1041 * already active if there are any fsck instances running.
1044 return (instance_list != 0);
1045 for (inst = instance_list; inst; inst = inst->next) {
1046 if (!inst->base_device || !strcmp(base, inst->base_device)) {
1055 /* Check all file systems, using the /etc/fstab table. */
1056 static int check_all(void)
1058 struct fs_info *fs = NULL;
1059 int status = EXIT_OK;
1060 int not_done_yet = 1;
1065 fputs("Checking all file systems.\n", stdout);
1068 * Do an initial scan over the filesystem; mark filesystems
1069 * which should be ignored as done, and resolve any "auto"
1070 * filesystem types (done as a side-effect of calling ignore()).
1072 for (fs = filesys_info; fs; fs = fs->next) {
1074 fs->flags |= FLAG_DONE;
1078 * Find and check the root filesystem.
1080 if (!parallel_root) {
1081 for (fs = filesys_info; fs; fs = fs->next) {
1082 if (LONE_CHAR(fs->mountpt, '/'))
1086 if (!skip_root && !ignore(fs)) {
1088 status |= wait_many(FLAG_WAIT_ALL);
1089 if (status > EXIT_NONDESTRUCT)
1092 fs->flags |= FLAG_DONE;
1096 * This is for the bone-headed user who enters the root
1097 * filesystem twice. Skip root will skep all root entries.
1100 for (fs = filesys_info; fs; fs = fs->next)
1101 if (LONE_CHAR(fs->mountpt, '/'))
1102 fs->flags |= FLAG_DONE;
1104 while (not_done_yet) {
1108 for (fs = filesys_info; fs; fs = fs->next) {
1109 if (cancel_requested)
1111 if (fs->flags & FLAG_DONE)
1114 * If the filesystem's pass number is higher
1115 * than the current pass number, then we don't
1118 if (fs->passno > passno) {
1123 * If a filesystem on a particular device has
1124 * already been spawned, then we need to defer
1125 * this to another pass.
1127 if (device_already_active(fs->device)) {
1132 * Spawn off the fsck process
1134 fsck_device(fs, serialize);
1135 fs->flags |= FLAG_DONE;
1138 * Only do one filesystem at a time, or if we
1139 * have a limit on the number of fsck's extant
1140 * at one time, apply that limit.
1143 (max_running && (num_running >= max_running))) {
1148 if (cancel_requested)
1151 printf("--waiting-- (pass %d)\n", passno);
1152 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
1153 FLAG_WAIT_ATLEAST_ONE);
1156 printf("----------------------------------\n");
1161 if (cancel_requested && !kill_sent) {
1165 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
1169 static void signal_cancel(int sig FSCK_ATTR((unused)))
1174 static void PRS(int argc, char *argv[])
1177 char *arg, *dev, *tmp = 0;
1180 int opts_for_fsck = 0;
1181 struct sigaction sa;
1184 * Set up signal action
1186 memset(&sa, 0, sizeof(struct sigaction));
1187 sa.sa_handler = signal_cancel;
1188 sigaction(SIGINT, &sa, 0);
1189 sigaction(SIGTERM, &sa, 0);
1195 for (i=1; i < argc; i++) {
1199 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
1200 if (num_devices >= MAX_DEVICES) {
1201 bb_error_msg_and_die("too many devices");
1203 dev = blkid_get_devname(cache, arg, NULL);
1204 if (!dev && strchr(arg, '=')) {
1206 * Check to see if we failed because
1207 * /proc/partitions isn't found.
1209 if (access("/proc/partitions", R_OK) < 0) {
1210 bb_perror_msg_and_die("cannot open /proc/partitions "
1211 "(is /proc mounted?)");
1214 * Check to see if this is because
1215 * we're not running as root
1218 bb_error_msg_and_die(
1219 "must be root to scan for matching filesystems: %s\n", arg);
1221 bb_error_msg_and_die(
1222 "cannot find matching filesystem: %s", arg);
1224 devices[num_devices++] = dev ? dev : string_copy(arg);
1227 if (arg[0] != '-' || opts_for_fsck) {
1228 if (num_args >= MAX_ARGS) {
1229 bb_error_msg_and_die("too many arguments");
1231 args[num_args++] = string_copy(arg);
1234 for (j=1; arg[j]; j++) {
1235 if (opts_for_fsck) {
1236 options[++opt] = arg[j];
1246 progress_fd = string_to_int(arg+j+1);
1247 if (progress_fd < 0)
1251 } else if ((i+1) < argc
1252 && argv[i+1][0] != '-') {
1253 progress_fd = string_to_int(argv[i]);
1254 if (progress_fd < 0)
1289 else if ((i+1) < argc)
1293 fstype = string_copy(tmp);
1294 compile_fs_type(fstype, &fs_type_compiled);
1303 options[++opt] = arg[j];
1310 options[++opt] = '\0';
1311 if (num_args >= MAX_ARGS) {
1312 bb_error_msg("too many arguments");
1314 args[num_args++] = string_copy(options);
1318 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1319 force_all_parallel++;
1320 if ((tmp = getenv("FSCK_MAX_INST")))
1321 max_running = atoi(tmp);
1324 int fsck_main(int argc, char *argv[]);
1325 int fsck_main(int argc, char *argv[])
1328 int interactive = 0;
1332 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
1333 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
1335 blkid_get_cache(&cache, NULL);
1339 printf("fsck %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
1341 fstab = getenv("FSTAB_FILE");
1343 fstab = _PATH_MNTTAB;
1344 load_fs_info(fstab);
1346 fsck_path = e2fs_set_sbin_path();
1348 if ((num_devices == 1) || (serialize))
1351 /* If -A was specified ("check all"), do that! */
1355 if (num_devices == 0) {
1360 for (i = 0 ; i < num_devices; i++) {
1361 if (cancel_requested) {
1368 fs = lookup(devices[i]);
1370 fs = create_fs_device(devices[i], 0, "auto",
1375 fsck_device(fs, interactive);
1377 (max_running && (num_running >= max_running))) {
1378 struct fsck_instance *inst;
1382 status |= inst->exit_status;
1383 free_instance(inst);
1386 printf("----------------------------------\n");
1389 status |= wait_many(FLAG_WAIT_ALL);
1390 blkid_put_cache(cache);