2 * pfsck --- A generic, parallelizing front-end for the fsck program.
3 * It will automatically try to run fsck programs in parallel if the
4 * devices are on separate spindles. It is based on the same ideas as
5 * the generic front end for fsck by David Engel and Fred van Kempen,
6 * but it has been completely rewritten from scratch to support
9 * Written by Theodore Ts'o, <tytso@mit.edu>
11 * Miquel van Smoorenburg (miquels@drinkel.ow.org) 20-Oct-1994:
12 * o Changed -t fstype to behave like with mount when -A (all file
13 * systems) or -M (like mount) is specified.
14 * o fsck looks if it can find the fsck.type program to decide
15 * if it should ignore the fs type. This way more fsck programs
16 * can be added without changing this front-end.
17 * o -R flag skip root file system.
19 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
20 * 2001, 2002, 2003, 2004, 2005 by Theodore Ts'o.
23 * This file may be redistributed under the terms of the GNU Public
28 #include <sys/types.h>
30 #include <sys/signal.h>
59 #include "../version.h"
60 #include "nls-enable.h"
62 #include "blkid/blkid.h"
65 #define _PATH_MNTTAB "/etc/fstab"
68 static const char *ignored_types[] = {
80 static const char *really_wanted[] = {
91 #define BASE_MD "/dev/md"
94 * Global variables for options
96 char *devices[MAX_DEVICES];
98 int num_devices, num_args;
107 int parallel_root = 0;
110 int force_all_parallel = 0;
113 volatile int cancel_requested = 0;
117 struct fs_info *filesys_info = NULL, *filesys_last = NULL;
118 struct fsck_instance *instance_list;
119 const char *fsck_prefix_path = "/sbin:/sbin/fs.d:/sbin/fs:/etc/fs:/etc";
121 blkid_cache cache = NULL;
123 static char *string_copy(const char *s)
129 ret = malloc(strlen(s)+1);
135 static int string_to_int(const char *s)
140 l = strtol(s, &p, 0);
141 if (*p || l == LONG_MIN || l == LONG_MAX || l < 0 || l > INT_MAX)
147 static int ignore(struct fs_info *);
149 static char *skip_over_blank(char *cp)
151 while (*cp && isspace(*cp))
156 static char *skip_over_word(char *cp)
158 while (*cp && !isspace(*cp))
163 static void strip_line(char *line)
168 p = line + strlen(line) - 1;
169 if ((*p == '\n') || (*p == '\r'))
176 static char *parse_word(char **buf)
184 word = skip_over_blank(word);
185 next = skip_over_word(word);
192 static void parse_escape(char *word)
200 for (p = word, q = word; *p; p++, q++) {
219 for (i = 0; i < 3; i++, p++) {
222 ac = (ac * 8) + (*p - '0');
230 static void free_instance(struct fsck_instance *i)
237 free(i->base_device);
242 static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
243 const char *type, const char *opts,
244 int freq, int passno)
248 if (!(fs = malloc(sizeof(struct fs_info))))
251 fs->device = string_copy(device);
252 fs->mountpt = string_copy(mntpnt);
253 fs->type = string_copy(type);
254 fs->opts = string_copy(opts ? opts : "");
263 filesys_last->next = fs;
271 static int parse_fstab_line(char *line, struct fs_info **ret_fs)
273 char *dev, *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
278 if ((cp = strchr(line, '#')))
279 *cp = 0; /* Ignore everything after the comment char */
282 device = parse_word(&cp);
283 mntpnt = parse_word(&cp);
284 type = parse_word(&cp);
285 opts = parse_word(&cp);
286 freq = parse_word(&cp);
287 passno = parse_word(&cp);
290 return 0; /* Allow blank lines */
292 if (!mntpnt || !type)
295 parse_escape(device);
296 parse_escape(mntpnt);
300 parse_escape(passno);
302 dev = blkid_get_devname(cache, device, NULL);
306 if (strchr(type, ','))
309 fs = create_fs_device(device, mntpnt, type ? type : "auto", opts,
310 freq ? atoi(freq) : -1,
311 passno ? atoi(passno) : -1);
321 static void interpret_type(struct fs_info *fs)
325 if (strcmp(fs->type, "auto") != 0)
327 t = blkid_get_tag_value(cache, "TYPE", fs->device);
335 * Load the filesystem database from /etc/fstab
337 static void load_fs_info(const char *filename)
345 if ((f = fopen(filename, "r")) == NULL) {
346 fprintf(stderr, _("WARNING: couldn't open %s: %s\n"),
347 filename, strerror(errno));
352 if (!fgets(buf, sizeof(buf), f))
354 buf[sizeof(buf)-1] = 0;
355 if (parse_fstab_line(buf, &fs) < 0) {
356 fprintf(stderr, _("WARNING: bad format "
357 "on line %d of %s\n"), lineno, filename);
371 fputs(_("\007\007\007"
372 "WARNING: Your /etc/fstab does not contain the fsck passno\n"
373 " field. I will kludge around things for you, but you\n"
374 " should fix your /etc/fstab file as soon as you can.\n\n"), stderr);
376 for (fs = filesys_info; fs; fs = fs->next) {
382 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
383 static struct fs_info *lookup(char *filesys)
387 /* No filesys name given. */
391 for (fs = filesys_info; fs; fs = fs->next) {
392 if (!strcmp(filesys, fs->device) ||
393 (fs->mountpt && !strcmp(filesys, fs->mountpt)))
400 /* Find fsck program for a given fs type. */
401 static char *find_fsck(char *type)
405 static char prog[256];
406 char *p = string_copy(fsck_path);
409 /* Are we looking for a program or just a type? */
410 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
412 for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
413 sprintf(prog, tpl, s, type);
414 if (stat(prog, &st) == 0) break;
417 return(s ? prog : NULL);
420 static int progress_active(NOARGS)
422 struct fsck_instance *inst;
424 for (inst = instance_list; inst; inst = inst->next) {
425 if (inst->flags & FLAG_DONE)
427 if (inst->flags & FLAG_PROGRESS)
434 * Execute a particular fsck program, and link it into the list of
435 * child processes we are waiting for.
437 static int execute(const char *type, const char *device, const char *mntpt,
440 char *s, *argv[80], prog[80];
442 struct fsck_instance *inst, *p;
445 inst = malloc(sizeof(struct fsck_instance));
448 memset(inst, 0, sizeof(struct fsck_instance));
450 sprintf(prog, "fsck.%s", type);
451 argv[0] = string_copy(prog);
454 for (i=0; i <num_args; i++)
455 argv[argc++] = string_copy(args[i]);
457 if (progress && !progress_active()) {
458 if ((strcmp(type, "ext2") == 0) ||
459 (strcmp(type, "ext3") == 0)) {
461 snprintf(tmp, 80, "-C%d", progress_fd);
462 argv[argc++] = string_copy(tmp);
463 inst->flags |= FLAG_PROGRESS;
467 argv[argc++] = string_copy(device);
472 fprintf(stderr, _("fsck: %s: not found\n"), prog);
476 if (verbose || noexecute) {
477 printf("[%s (%d) -- %s] ", s, num_running,
478 mntpt ? mntpt : device);
479 for (i=0; i < argc; i++)
480 printf("%s ", argv[i]);
484 /* Fork and execute the correct program. */
487 else if ((pid = fork()) < 0) {
490 } else if (pid == 0) {
493 (void) execv(s, argv);
498 for (i=0; i < argc; i++)
502 inst->prog = string_copy(prog);
503 inst->type = string_copy(type);
504 inst->device = string_copy(device);
505 inst->base_device = base_device(device);
506 inst->start_time = time(0);
510 * Find the end of the list, so we add the instance on at the end.
512 for (p = instance_list; p && p->next; p = p->next);
517 instance_list = inst;
523 * Send a signal to all outstanding fsck child processes
525 static int kill_all(int signum)
527 struct fsck_instance *inst;
530 for (inst = instance_list; inst; inst = inst->next) {
531 if (inst->flags & FLAG_DONE)
533 kill(inst->pid, signum);
540 * Wait for one child process to exit; when it does, unlink it from
541 * the list of executing child processes, and return it.
543 static struct fsck_instance *wait_one(int flags)
547 struct fsck_instance *inst, *inst2, *prev;
554 inst = instance_list;
557 while (inst->next && (random() & 1)) {
562 inst->exit_status = 0;
567 * gcc -Wall fails saving throw against stupidity
568 * (inst and prev are thought to be uninitialized variables)
573 pid = waitpid(-1, &status, flags);
574 if (cancel_requested && !kill_sent) {
578 if ((pid == 0) && (flags & WNOHANG))
581 if ((errno == EINTR) || (errno == EAGAIN))
583 if (errno == ECHILD) {
585 _("%s: wait: No more child process?!?\n"),
592 for (prev = 0, inst = instance_list;
594 prev = inst, inst = inst->next) {
595 if (inst->pid == pid)
600 if (WIFEXITED(status))
601 status = WEXITSTATUS(status);
602 else if (WIFSIGNALED(status)) {
603 sig = WTERMSIG(status);
605 status = EXIT_UNCORRECTED;
607 printf(_("Warning... %s for device %s exited "
608 "with signal %d.\n"),
609 inst->prog, inst->device, sig);
613 printf(_("%s %s: status is %x, should never happen.\n"),
614 inst->prog, inst->device, status);
617 inst->exit_status = status;
618 if (progress && (inst->flags & FLAG_PROGRESS) &&
619 !progress_active()) {
620 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
621 if (inst2->flags & FLAG_DONE)
623 if (strcmp(inst2->type, "ext2") &&
624 strcmp(inst2->type, "ext3"))
627 * If we've just started the fsck, wait a tiny
628 * bit before sending the kill, to give it
629 * time to set up the signal handler
631 if (inst2->start_time < time(0)+2) {
634 kill(inst2->pid, SIGUSR1);
638 kill(inst2->pid, SIGUSR1);
639 inst2->flags |= FLAG_PROGRESS;
645 prev->next = inst->next;
647 instance_list = inst->next;
649 printf(_("Finished with %s (exit status %d)\n"),
650 inst->device, inst->exit_status);
655 #define FLAG_WAIT_ALL 0
656 #define FLAG_WAIT_ATLEAST_ONE 1
658 * Wait until all executing child processes have exited; return the
659 * logical OR of all of their exit code values.
661 static int wait_many(int flags)
663 struct fsck_instance *inst;
664 int global_status = 0;
667 while ((inst = wait_one(wait_flags))) {
668 global_status |= inst->exit_status;
671 if (noexecute && (flags & WNOHANG) && !(random() % 3))
674 if (flags & FLAG_WAIT_ATLEAST_ONE)
675 wait_flags = WNOHANG;
677 return global_status;
681 * Run the fsck program on a particular device
683 * If the type is specified using -t, and it isn't prefixed with "no"
684 * (as in "noext2") and only one filesystem type is specified, then
685 * use that type regardless of what is specified in /etc/fstab.
687 * If the type isn't specified by the user, then use either the type
688 * specified in /etc/fstab, or DEFAULT_FSTYPE.
690 static void fsck_device(struct fs_info *fs, int interactive)
697 if (strcmp(fs->type, "auto") != 0)
699 else if (fstype && strncmp(fstype, "no", 2) &&
700 strncmp(fstype, "opts=", 5) && strncmp(fstype, "loop", 4) &&
701 !strchr(fstype, ','))
704 type = DEFAULT_FSTYPE;
707 retval = execute(type, fs->device, fs->mountpt, interactive);
709 fprintf(stderr, _("%s: Error %d while executing fsck.%s "
710 "for %s\n"), progname, retval, type, fs->device);
717 * Deal with the fsck -t argument.
719 struct fs_type_compile {
725 #define FS_TYPE_NORMAL 0
726 #define FS_TYPE_OPT 1
727 #define FS_TYPE_NEGOPT 2
729 static const char *fs_type_syntax_error =
730 N_("Either all or none of the filesystem types passed to -t must be prefixed\n"
731 "with 'no' or '!'.\n");
733 static void compile_fs_type(char *fs_type, struct fs_type_compile *cmp)
737 int negate, first_negate = 1;
740 for (cp=fs_type; *cp; cp++) {
746 cmp->list = malloc(num * sizeof(char *));
747 cmp->type = malloc(num * sizeof(int));
748 if (!cmp->list || !cmp->type) {
749 fputs(_("Couldn't allocate memory for filesystem types\n"),
753 memset(cmp->list, 0, num * sizeof(char *));
754 memset(cmp->type, 0, num * sizeof(int));
760 list = string_copy(fs_type);
762 s = strtok(list, ",");
765 if (strncmp(s, "no", 2) == 0) {
768 } else if (*s == '!') {
772 if (strcmp(s, "loop") == 0)
773 /* loop is really short-hand for opts=loop */
774 goto loop_special_case;
775 else if (strncmp(s, "opts=", 5) == 0) {
778 cmp->type[num] = negate ? FS_TYPE_NEGOPT : FS_TYPE_OPT;
781 cmp->negate = negate;
784 if ((negate && !cmp->negate) ||
785 (!negate && cmp->negate)) {
786 fputs(_(fs_type_syntax_error), stderr);
791 printf("Adding %s to list (type %d).\n", s, cmp->type[num]);
793 cmp->list[num++] = string_copy(s);
794 s = strtok(NULL, ",");
800 * This function returns true if a particular option appears in a
801 * comma-delimited options list
803 static int opt_in_list(char *opt, char *optlist)
809 list = string_copy(optlist);
811 s = strtok(list, ",");
813 if (strcmp(s, opt) == 0) {
817 s = strtok(NULL, ",");
823 /* See if the filesystem matches the criteria given by the -t option */
824 static int fs_match(struct fs_info *fs, struct fs_type_compile *cmp)
826 int n, ret = 0, checked_type = 0;
829 if (cmp->list == 0 || cmp->list[0] == 0)
832 for (n=0; (cp = cmp->list[n]); n++) {
833 switch (cmp->type[n]) {
836 if (strcmp(cp, fs->type) == 0) {
841 if (opt_in_list(cp, fs->opts))
845 if (!opt_in_list(cp, fs->opts))
850 if (checked_type == 0)
852 return (cmp->negate ? !ret : ret);
855 /* Check if we should ignore this filesystem. */
856 static int ignore(struct fs_info *fs)
862 * If the pass number is 0, ignore it.
870 * If a specific fstype is specified, and it doesn't match,
873 if (!fs_match(fs, &fs_type_compiled)) return 1;
875 /* Are we ignoring this type? */
876 for(ip = ignored_types; *ip; ip++)
877 if (strcmp(fs->type, *ip) == 0) return 1;
879 /* Do we really really want to check this fs? */
880 for(ip = really_wanted; *ip; ip++)
881 if (strcmp(fs->type, *ip) == 0) {
886 /* See if the <fsck.fs> program is available. */
887 if (find_fsck(fs->type) == NULL) {
889 fprintf(stderr, _("fsck: cannot check %s: fsck.%s not found\n"),
890 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)))
918 base = base_device(device);
920 * If we don't know the base device, assume that the device is
921 * already active if there are any fsck instances running.
924 return (instance_list != 0);
925 for (inst = instance_list; inst; inst = inst->next) {
926 if (!inst->base_device || !strcmp(base, inst->base_device)) {
935 /* Check all file systems, using the /etc/fstab table. */
936 static int check_all(NOARGS)
938 struct fs_info *fs = NULL;
939 int status = EXIT_OK;
940 int not_done_yet = 1;
945 fputs(_("Checking all file systems.\n"), stdout);
948 * Do an initial scan over the filesystem; mark filesystems
949 * which should be ignored as done, and resolve any "auto"
950 * filesystem types (done as a side-effect of calling ignore()).
952 for (fs = filesys_info; fs; fs = fs->next) {
954 fs->flags |= FLAG_DONE;
958 * Find and check the root filesystem.
960 if (!parallel_root) {
961 for (fs = filesys_info; fs; fs = fs->next) {
962 if (!strcmp(fs->mountpt, "/"))
966 if (!skip_root && !ignore(fs)) {
968 status |= wait_many(FLAG_WAIT_ALL);
969 if (status > EXIT_NONDESTRUCT)
972 fs->flags |= FLAG_DONE;
976 * This is for the bone-headed user who enters the root
977 * filesystem twice. Skip root will skep all root entries.
980 for (fs = filesys_info; fs; fs = fs->next)
981 if (!strcmp(fs->mountpt, "/"))
982 fs->flags |= FLAG_DONE;
984 while (not_done_yet) {
988 for (fs = filesys_info; fs; fs = fs->next) {
989 if (cancel_requested)
991 if (fs->flags & FLAG_DONE)
994 * If the filesystem's pass number is higher
995 * than the current pass number, then we don't
998 if (fs->passno > passno) {
1003 * If a filesystem on a particular device has
1004 * already been spawned, then we need to defer
1005 * this to another pass.
1007 if (device_already_active(fs->device)) {
1012 * Spawn off the fsck process
1014 fsck_device(fs, serialize);
1015 fs->flags |= FLAG_DONE;
1018 * Only do one filesystem at a time, or if we
1019 * have a limit on the number of fsck's extant
1020 * at one time, apply that limit.
1023 (max_running && (num_running >= max_running))) {
1028 if (cancel_requested)
1031 printf(_("--waiting-- (pass %d)\n"), passno);
1032 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
1033 FLAG_WAIT_ATLEAST_ONE);
1036 printf("----------------------------------\n");
1041 if (cancel_requested && !kill_sent) {
1045 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
1049 static void usage(NOARGS)
1051 fputs(_("Usage: fsck [-ANPRTV] [ -C [ fd ] ] [-t fstype] [fs-options] [filesys ...]\n"), stderr);
1055 #ifdef HAVE_SIGNAL_H
1056 static void signal_cancel(int sig FSCK_ATTR((unused)))
1062 static void PRS(int argc, char *argv[])
1065 char *arg, *dev, *tmp = 0;
1068 int opts_for_fsck = 0;
1069 #ifdef HAVE_SIGNAL_H
1070 struct sigaction sa;
1073 * Set up signal action
1075 memset(&sa, 0, sizeof(struct sigaction));
1076 sa.sa_handler = signal_cancel;
1077 sigaction(SIGINT, &sa, 0);
1078 sigaction(SIGTERM, &sa, 0);
1087 for (i=1; i < argc; i++) {
1091 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
1092 if (num_devices >= MAX_DEVICES) {
1093 fprintf(stderr, _("%s: too many devices\n"),
1097 dev = blkid_get_devname(cache, arg, NULL);
1098 if (!dev && strchr(arg, '=')) {
1100 * Check to see if we failed because
1101 * /proc/partitions isn't found.
1103 if (access("/proc/partitions", R_OK) < 0) {
1104 fprintf(stderr, "Couldn't open /proc/partitions: %s\n",
1106 fprintf(stderr, "Is /proc mounted?\n");
1110 * Check to see if this is because
1111 * we're not running as root
1115 "Must be root to scan for matching filesystems: %s\n", arg);
1118 "Couldn't find matching filesystem: %s\n", arg);
1121 devices[num_devices++] = dev ? dev : string_copy(arg);
1124 if (arg[0] != '-' || opts_for_fsck) {
1125 if (num_args >= MAX_ARGS) {
1126 fprintf(stderr, _("%s: too many arguments\n"),
1130 args[num_args++] = string_copy(arg);
1133 for (j=1; arg[j]; j++) {
1134 if (opts_for_fsck) {
1135 options[++opt] = arg[j];
1145 progress_fd = string_to_int(arg+j+1);
1146 if (progress_fd < 0)
1150 } else if ((i+1) < argc &&
1151 !strncmp(argv[i+1], "-", 1) == 0) {
1152 progress_fd = string_to_int(argv[i]);
1153 if (progress_fd < 0)
1188 else if ((i+1) < argc)
1192 fstype = string_copy(tmp);
1193 compile_fs_type(fstype, &fs_type_compiled);
1202 options[++opt] = arg[j];
1209 options[++opt] = '\0';
1210 if (num_args >= MAX_ARGS) {
1212 _("%s: too many arguments\n"),
1216 args[num_args++] = string_copy(options);
1220 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1221 force_all_parallel++;
1222 if ((tmp = getenv("FSCK_MAX_INST")))
1223 max_running = atoi(tmp);
1226 int main(int argc, char *argv[])
1229 int interactive = 0;
1230 char *oldpath = getenv("PATH");
1234 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
1235 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
1238 setlocale(LC_MESSAGES, "");
1239 setlocale(LC_CTYPE, "");
1240 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1241 textdomain(NLS_CAT_NAME);
1243 blkid_get_cache(&cache, NULL);
1247 printf("fsck %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
1249 fstab = getenv("FSTAB_FILE");
1251 fstab = _PATH_MNTTAB;
1252 load_fs_info(fstab);
1254 /* Update our search path to include uncommon directories. */
1256 fsck_path = malloc (strlen (fsck_prefix_path) + 1 +
1257 strlen (oldpath) + 1);
1259 fprintf(stderr, "%s: Unable to allocate memory for fsck_path\n", progname);
1262 strcpy (fsck_path, fsck_prefix_path);
1263 strcat (fsck_path, ":");
1264 strcat (fsck_path, oldpath);
1266 fsck_path = string_copy(fsck_prefix_path);
1269 if ((num_devices == 1) || (serialize))
1272 /* If -A was specified ("check all"), do that! */
1276 if (num_devices == 0) {
1281 for (i = 0 ; i < num_devices; i++) {
1282 if (cancel_requested) {
1289 fs = lookup(devices[i]);
1291 fs = create_fs_device(devices[i], 0, "auto",
1296 fsck_device(fs, interactive);
1298 (max_running && (num_running >= max_running))) {
1299 struct fsck_instance *inst;
1303 status |= inst->exit_status;
1304 free_instance(inst);
1307 printf("----------------------------------\n");
1310 status |= wait_many(FLAG_WAIT_ALL);
1312 blkid_put_cache(cache);