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.
22 * Copyright (C) 2009 Karel Zak <kzak@redhat.com>
24 * This file may be redistributed under the terms of the GNU Public
28 #define _XOPEN_SOURCE 600 /* for inclusion of sa_handler in Solaris */
30 #include <sys/types.h>
32 #include <sys/signal.h>
50 #include "pathnames.h"
51 #include "ismounted.h"
55 static const char *ignored_types[] = {
67 static const char *really_wanted[] = {
80 #define BASE_MD "/dev/md"
83 * Global variables for options
85 char *devices[MAX_DEVICES];
87 int num_devices, num_args;
94 int ignore_mounted = 0;
96 int parallel_root = 0;
99 int force_all_parallel = 0;
102 volatile int cancel_requested = 0;
106 struct fs_info *filesys_info = NULL, *filesys_last = NULL;
107 struct fsck_instance *instance_list;
108 const char *fsck_prefix_path = "/sbin:/sbin/fs.d:/sbin/fs:/etc/fs:/etc";
111 static char *string_copy(const char *s)
117 ret = malloc(strlen(s)+1);
123 static int string_to_int(const char *s)
128 l = strtol(s, &p, 0);
129 if (*p || l == LONG_MIN || l == LONG_MAX || l < 0 || l > INT_MAX)
135 static int ignore(struct fs_info *);
137 static char *skip_over_blank(char *cp)
139 while (*cp && isspace(*cp))
144 static char *skip_over_word(char *cp)
146 while (*cp && !isspace(*cp))
151 static void strip_line(char *line)
156 p = line + strlen(line) - 1;
157 if ((*p == '\n') || (*p == '\r'))
164 static char *parse_word(char **buf)
172 word = skip_over_blank(word);
173 next = skip_over_word(word);
180 static void parse_escape(char *word)
188 for (p = word, q = word; *p; p++, q++) {
207 for (i = 0; i < 3; i++, p++) {
210 ac = (ac * 8) + (*p - '0');
218 static void free_instance(struct fsck_instance *i)
222 free(i->base_device);
227 static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
228 const char *type, const char *opts,
229 int freq, int passno)
233 if (!(fs = malloc(sizeof(struct fs_info))))
236 fs->device = string_copy(device);
237 fs->mountpt = string_copy(mntpnt);
238 fs->type = string_copy(type);
239 fs->opts = string_copy(opts ? opts : "");
248 filesys_last->next = fs;
256 static int parse_fstab_line(char *line, struct fs_info **ret_fs)
258 char *dev, *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
265 device = parse_word(&cp);
266 if (!device || *device == '#')
267 return 0; /* Ignore blank lines and comments */
268 mntpnt = parse_word(&cp);
269 type = parse_word(&cp);
270 opts = parse_word(&cp);
271 freq = parse_word(&cp);
272 passno = parse_word(&cp);
274 if (!mntpnt || !type)
277 parse_escape(device);
278 parse_escape(mntpnt);
282 parse_escape(passno);
284 dev = fsprobe_get_devname_by_spec(device);
288 if (strchr(type, ','))
291 fs = create_fs_device(device, mntpnt, type ? type : "auto", opts,
292 freq ? atoi(freq) : -1,
293 passno ? atoi(passno) : -1);
302 static void interpret_type(struct fs_info *fs)
306 if (strcmp(fs->type, "auto") != 0)
308 t = fsprobe_get_fstype_by_devname(fs->device);
316 * Load the filesystem database from /etc/fstab
318 static void load_fs_info(const char *filename)
326 if ((f = fopen(filename, "r")) == NULL) {
327 fprintf(stderr, _("WARNING: couldn't open %s: %s\n"),
328 filename, strerror(errno));
333 if (!fgets(buf, sizeof(buf), f))
335 buf[sizeof(buf)-1] = 0;
336 if (parse_fstab_line(buf, &fs) < 0) {
337 fprintf(stderr, _("WARNING: bad format "
338 "on line %d of %s\n"), lineno, filename);
351 if (old_fstab && filesys_info) {
353 "WARNING: Your /etc/fstab does not contain the fsck passno\n"
354 " field. I will kludge around things for you, but you\n"
355 " should fix your /etc/fstab file as soon as you can.\n\n"), stderr);
357 for (fs = filesys_info; fs; fs = fs->next) {
363 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
364 static struct fs_info *lookup(char *filesys)
368 /* No filesys name given. */
372 for (fs = filesys_info; fs; fs = fs->next) {
373 if (!strcmp(filesys, fs->device) ||
374 (fs->mountpt && !strcmp(filesys, fs->mountpt)))
381 /* Find fsck program for a given fs type. */
382 static char *find_fsck(char *type)
386 static char prog[256];
387 char *p = string_copy(fsck_path);
390 /* Are we looking for a program or just a type? */
391 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
393 for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
394 sprintf(prog, tpl, s, type);
395 if (stat(prog, &st) == 0) break;
398 return(s ? prog : NULL);
401 static int progress_active(NOARGS)
403 struct fsck_instance *inst;
405 for (inst = instance_list; inst; inst = inst->next) {
406 if (inst->flags & FLAG_DONE)
408 if (inst->flags & FLAG_PROGRESS)
415 * Execute a particular fsck program, and link it into the list of
416 * child processes we are waiting for.
418 static int execute(const char *type, const char *device, const char *mntpt,
421 char *s, *argv[80], prog[80];
423 struct fsck_instance *inst, *p;
426 inst = malloc(sizeof(struct fsck_instance));
429 memset(inst, 0, sizeof(struct fsck_instance));
431 sprintf(prog, "fsck.%s", type);
432 argv[0] = string_copy(prog);
435 for (i=0; i <num_args; i++)
436 argv[argc++] = string_copy(args[i]);
439 if ((strcmp(type, "ext2") == 0) ||
440 (strcmp(type, "ext3") == 0) ||
441 (strcmp(type, "ext4") == 0) ||
442 (strcmp(type, "ext4dev") == 0)) {
446 if (!progress_active()) {
447 snprintf(tmp, 80, "-C%d", progress_fd);
448 inst->flags |= FLAG_PROGRESS;
449 } else if (progress_fd)
450 snprintf(tmp, 80, "-C%d", progress_fd * -1);
452 argv[argc++] = string_copy(tmp);
456 argv[argc++] = string_copy(device);
461 fprintf(stderr, _("fsck: %s: not found\n"), prog);
466 if (verbose || noexecute) {
467 printf("[%s (%d) -- %s] ", s, num_running,
468 mntpt ? mntpt : device);
469 for (i=0; i < argc; i++)
470 printf("%s ", argv[i]);
474 /* Fork and execute the correct program. */
477 else if ((pid = fork()) < 0) {
481 } else if (pid == 0) {
484 (void) execv(s, argv);
490 for (i=0; i < argc; i++)
494 inst->prog = string_copy(prog);
495 inst->type = string_copy(type);
496 inst->device = string_copy(device);
497 inst->base_device = base_device(device);
498 inst->start_time = time(0);
502 * Find the end of the list, so we add the instance on at the end.
504 for (p = instance_list; p && p->next; p = p->next);
509 instance_list = inst;
515 * Send a signal to all outstanding fsck child processes
517 static int kill_all(int signum)
519 struct fsck_instance *inst;
522 for (inst = instance_list; inst; inst = inst->next) {
523 if (inst->flags & FLAG_DONE)
525 kill(inst->pid, signum);
532 * Wait for one child process to exit; when it does, unlink it from
533 * the list of executing child processes, and return it.
535 static struct fsck_instance *wait_one(int flags)
539 struct fsck_instance *inst, *inst2, *prev;
546 inst = instance_list;
549 while (inst->next && (random() & 1)) {
554 inst->exit_status = 0;
559 * gcc -Wall fails saving throw against stupidity
560 * (inst and prev are thought to be uninitialized variables)
565 pid = waitpid(-1, &status, flags);
566 if (cancel_requested && !kill_sent) {
570 if ((pid == 0) && (flags & WNOHANG))
573 if ((errno == EINTR) || (errno == EAGAIN))
575 if (errno == ECHILD) {
577 _("%s: wait: No more child process?!?\n"),
584 for (prev = 0, inst = instance_list;
586 prev = inst, inst = inst->next) {
587 if (inst->pid == pid)
592 if (WIFEXITED(status))
593 status = WEXITSTATUS(status);
594 else if (WIFSIGNALED(status)) {
595 sig = WTERMSIG(status);
597 status = EXIT_UNCORRECTED;
599 printf(_("Warning... %s for device %s exited "
600 "with signal %d.\n"),
601 inst->prog, inst->device, sig);
605 printf(_("%s %s: status is %x, should never happen.\n"),
606 inst->prog, inst->device, status);
609 inst->exit_status = status;
610 inst->flags |= FLAG_DONE;
611 if (progress && (inst->flags & FLAG_PROGRESS) &&
612 !progress_active()) {
613 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
614 if (inst2->flags & FLAG_DONE)
616 if (strcmp(inst2->type, "ext2") &&
617 strcmp(inst2->type, "ext3") &&
618 strcmp(inst2->type, "ext4") &&
619 strcmp(inst2->type, "ext4dev"))
622 * If we've just started the fsck, wait a tiny
623 * bit before sending the kill, to give it
624 * time to set up the signal handler
626 if (inst2->start_time < time(0)+2) {
629 kill(inst2->pid, SIGUSR1);
633 kill(inst2->pid, SIGUSR1);
634 inst2->flags |= FLAG_PROGRESS;
640 prev->next = inst->next;
642 instance_list = inst->next;
644 printf(_("Finished with %s (exit status %d)\n"),
645 inst->device, inst->exit_status);
650 #define FLAG_WAIT_ALL 0
651 #define FLAG_WAIT_ATLEAST_ONE 1
653 * Wait until all executing child processes have exited; return the
654 * logical OR of all of their exit code values.
656 static int wait_many(int flags)
658 struct fsck_instance *inst;
659 int global_status = 0;
662 while ((inst = wait_one(wait_flags))) {
663 global_status |= inst->exit_status;
666 if (noexecute && (flags & WNOHANG) && !(random() % 3))
669 if (flags & FLAG_WAIT_ATLEAST_ONE)
670 wait_flags = WNOHANG;
672 return global_status;
676 * Run the fsck program on a particular device
678 * If the type is specified using -t, and it isn't prefixed with "no"
679 * (as in "noext2") and only one filesystem type is specified, then
680 * use that type regardless of what is specified in /etc/fstab.
682 * If the type isn't specified by the user, then use either the type
683 * specified in /etc/fstab, or DEFAULT_FSTYPE.
685 static void fsck_device(struct fs_info *fs, int interactive)
692 if (strcmp(fs->type, "auto") != 0)
694 else if (fstype && strncmp(fstype, "no", 2) &&
695 strncmp(fstype, "opts=", 5) && strncmp(fstype, "loop", 4) &&
696 !strchr(fstype, ','))
699 type = DEFAULT_FSTYPE;
702 retval = execute(type, fs->device, fs->mountpt, interactive);
704 fprintf(stderr, _("%s: Error %d while executing fsck.%s "
705 "for %s\n"), progname, retval, type, fs->device);
712 * Deal with the fsck -t argument.
714 struct fs_type_compile {
720 #define FS_TYPE_NORMAL 0
721 #define FS_TYPE_OPT 1
722 #define FS_TYPE_NEGOPT 2
724 static const char *fs_type_syntax_error =
725 N_("Either all or none of the filesystem types passed to -t must be prefixed\n"
726 "with 'no' or '!'.\n");
728 static void compile_fs_type(char *fs_type, struct fs_type_compile *cmp)
732 int negate, first_negate = 1;
735 for (cp=fs_type; *cp; cp++) {
741 cmp->list = malloc(num * sizeof(char *));
742 cmp->type = malloc(num * sizeof(int));
743 if (!cmp->list || !cmp->type) {
744 fputs(_("Couldn't allocate memory for filesystem types\n"),
748 memset(cmp->list, 0, num * sizeof(char *));
749 memset(cmp->type, 0, num * sizeof(int));
755 list = string_copy(fs_type);
757 s = strtok(list, ",");
760 if (strncmp(s, "no", 2) == 0) {
763 } else if (*s == '!') {
767 if (strcmp(s, "loop") == 0)
768 /* loop is really short-hand for opts=loop */
769 goto loop_special_case;
770 else if (strncmp(s, "opts=", 5) == 0) {
773 cmp->type[num] = negate ? FS_TYPE_NEGOPT : FS_TYPE_OPT;
776 cmp->negate = negate;
779 if ((negate && !cmp->negate) ||
780 (!negate && cmp->negate)) {
781 fputs(_(fs_type_syntax_error), stderr);
786 printf("Adding %s to list (type %d).\n", s, cmp->type[num]);
788 cmp->list[num++] = string_copy(s);
789 s = strtok(NULL, ",");
795 * This function returns true if a particular option appears in a
796 * comma-delimited options list
798 static int opt_in_list(const char *opt, char *optlist)
804 list = string_copy(optlist);
806 s = strtok(list, ",");
808 if (strcmp(s, opt) == 0) {
812 s = strtok(NULL, ",");
818 /* See if the filesystem matches the criteria given by the -t option */
819 static int fs_match(struct fs_info *fs, struct fs_type_compile *cmp)
821 int n, ret = 0, checked_type = 0;
824 if (cmp->list == 0 || cmp->list[0] == 0)
827 for (n=0; (cp = cmp->list[n]); n++) {
828 switch (cmp->type[n]) {
831 if (strcmp(cp, fs->type) == 0) {
836 if (opt_in_list(cp, fs->opts))
840 if (!opt_in_list(cp, fs->opts))
845 if (checked_type == 0)
847 return (cmp->negate ? !ret : ret);
850 /* Check if we should ignore this filesystem. */
851 static int ignore(struct fs_info *fs)
857 * If the pass number is 0, ignore it.
863 * If this is a bind mount, ignore it.
865 if (opt_in_list("bind", fs->opts)) {
867 _("%s: skipping bad line in /etc/fstab: bind mount with nonzero fsck pass number\n"),
875 * If a specific fstype is specified, and it doesn't match,
878 if (!fs_match(fs, &fs_type_compiled)) return 1;
880 /* Are we ignoring this type? */
881 for(ip = ignored_types; *ip; ip++)
882 if (strcmp(fs->type, *ip) == 0) return 1;
884 /* Do we really really want to check this fs? */
885 for(ip = really_wanted; *ip; ip++)
886 if (strcmp(fs->type, *ip) == 0) {
891 /* See if the <fsck.fs> program is available. */
892 if (find_fsck(fs->type) == NULL) {
894 fprintf(stderr, _("fsck: cannot check %s: fsck.%s not found\n"),
895 fs->device, fs->type);
899 /* We can and want to check this file system type. */
904 * Returns TRUE if a partition on the same disk is already being
907 static int device_already_active(char *device)
909 struct fsck_instance *inst;
912 if (force_all_parallel)
916 /* Don't check a soft raid disk with any other disk */
918 (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1) ||
919 !strncmp(device, BASE_MD, sizeof(BASE_MD)-1)))
923 base = base_device(device);
925 * If we don't know the base device, assume that the device is
926 * already active if there are any fsck instances running.
929 return (instance_list != 0);
930 for (inst = instance_list; inst; inst = inst->next) {
931 if (!inst->base_device || !strcmp(base, inst->base_device)) {
940 /* Check all file systems, using the /etc/fstab table. */
941 static int check_all(NOARGS)
943 struct fs_info *fs = NULL;
944 int status = EXIT_OK;
945 int not_done_yet = 1;
950 fputs(_("Checking all file systems.\n"), stdout);
953 * Do an initial scan over the filesystem; mark filesystems
954 * which should be ignored as done, and resolve any "auto"
955 * filesystem types (done as a side-effect of calling ignore()).
957 for (fs = filesys_info; fs; fs = fs->next) {
959 fs->flags |= FLAG_DONE;
963 * Find and check the root filesystem.
965 if (!parallel_root) {
966 for (fs = filesys_info; fs; fs = fs->next) {
967 if (!strcmp(fs->mountpt, "/"))
971 if (!skip_root && !ignore(fs) &&
972 !(ignore_mounted && is_mounted(fs->device))) {
974 status |= wait_many(FLAG_WAIT_ALL);
975 if (status > EXIT_NONDESTRUCT)
978 fs->flags |= FLAG_DONE;
982 * This is for the bone-headed user who enters the root
983 * filesystem twice. Skip root will skep all root entries.
986 for (fs = filesys_info; fs; fs = fs->next)
987 if (!strcmp(fs->mountpt, "/"))
988 fs->flags |= FLAG_DONE;
990 while (not_done_yet) {
994 for (fs = filesys_info; fs; fs = fs->next) {
995 if (cancel_requested)
997 if (fs->flags & FLAG_DONE)
1000 * If the filesystem's pass number is higher
1001 * than the current pass number, then we don't
1004 if (fs->passno > passno) {
1008 if (ignore_mounted && is_mounted(fs->device)) {
1009 fs->flags |= FLAG_DONE;
1013 * If a filesystem on a particular device has
1014 * already been spawned, then we need to defer
1015 * this to another pass.
1017 if (device_already_active(fs->device)) {
1022 * Spawn off the fsck process
1024 fsck_device(fs, serialize);
1025 fs->flags |= FLAG_DONE;
1028 * Only do one filesystem at a time, or if we
1029 * have a limit on the number of fsck's extant
1030 * at one time, apply that limit.
1033 (max_running && (num_running >= max_running))) {
1038 if (cancel_requested)
1041 printf(_("--waiting-- (pass %d)\n"), passno);
1042 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
1043 FLAG_WAIT_ATLEAST_ONE);
1046 printf("----------------------------------\n");
1051 if (cancel_requested && !kill_sent) {
1055 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
1059 static void usage(NOARGS)
1061 fputs(_("Usage: fsck [-AMNPRTV] [ -C [ fd ] ] [-t fstype] [fs-options] [filesys ...]\n"), stderr);
1065 static void signal_cancel(int sig FSCK_ATTR((unused)))
1070 static void PRS(int argc, char *argv[])
1073 char *arg, *dev, *tmp = 0;
1076 int opts_for_fsck = 0;
1077 struct sigaction sa;
1080 * Set up signal action
1082 memset(&sa, 0, sizeof(struct sigaction));
1083 sa.sa_handler = signal_cancel;
1084 sigaction(SIGINT, &sa, 0);
1085 sigaction(SIGTERM, &sa, 0);
1093 for (i=1; i < argc; i++) {
1097 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
1098 if (num_devices >= MAX_DEVICES) {
1099 fprintf(stderr, _("%s: too many devices\n"),
1103 dev = fsprobe_get_devname_by_spec(arg);
1104 if (!dev && strchr(arg, '=')) {
1106 * Check to see if we failed because
1107 * /proc/partitions isn't found.
1109 if (access(_PATH_PROC_PARTITIONS, R_OK) < 0) {
1110 fprintf(stderr, _("Couldn't open %s: %s\n"),
1111 _PATH_PROC_PARTITIONS, strerror(errno));
1112 fprintf(stderr, _("Is /proc mounted?\n"));
1116 * Check to see if this is because
1117 * we're not running as root
1121 _("Must be root to scan for matching filesystems: %s\n"), arg);
1124 _("Couldn't find matching filesystem: %s\n"), arg);
1127 devices[num_devices++] = dev ? dev : string_copy(arg);
1130 if (arg[0] != '-' || opts_for_fsck) {
1131 if (num_args >= MAX_ARGS) {
1132 fprintf(stderr, _("%s: too many arguments\n"),
1136 args[num_args++] = string_copy(arg);
1139 for (j=1; arg[j]; j++) {
1140 if (opts_for_fsck) {
1141 options[++opt] = arg[j];
1151 progress_fd = string_to_int(arg+j+1);
1152 if (progress_fd < 0)
1156 } else if ((i+1) < argc &&
1157 !strncmp(argv[i+1], "-", 1) == 0) {
1158 progress_fd = string_to_int(argv[i]);
1159 if (progress_fd < 0)
1194 else if ((i+1) < argc)
1198 fstype = string_copy(tmp);
1199 compile_fs_type(fstype, &fs_type_compiled);
1208 options[++opt] = arg[j];
1215 options[++opt] = '\0';
1216 if (num_args >= MAX_ARGS) {
1218 _("%s: too many arguments\n"),
1222 args[num_args++] = string_copy(options);
1226 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1227 force_all_parallel++;
1228 if ((tmp = getenv("FSCK_MAX_INST")))
1229 max_running = atoi(tmp);
1232 int main(int argc, char *argv[])
1235 int interactive = 0;
1236 char *oldpath = getenv("PATH");
1240 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
1241 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
1243 setlocale(LC_MESSAGES, "");
1244 setlocale(LC_CTYPE, "");
1245 bindtextdomain(PACKAGE, LOCALEDIR);
1246 textdomain(PACKAGE);
1252 printf(_("fsck from %s\n"), PACKAGE_STRING);
1254 fstab = getenv("FSTAB_FILE");
1256 fstab = _PATH_MNTTAB;
1257 load_fs_info(fstab);
1259 /* Update our search path to include uncommon directories. */
1261 fsck_path = malloc (strlen (fsck_prefix_path) + 1 +
1262 strlen (oldpath) + 1);
1264 fprintf(stderr, _("%s: Unable to allocate memory for fsck_path\n"), progname);
1267 strcpy (fsck_path, fsck_prefix_path);
1268 strcat (fsck_path, ":");
1269 strcat (fsck_path, oldpath);
1271 fsck_path = string_copy(fsck_prefix_path);
1274 if ((num_devices == 1) || (serialize))
1277 /* If -A was specified ("check all"), do that! */
1281 if (num_devices == 0) {
1286 for (i = 0 ; i < num_devices; i++) {
1287 if (cancel_requested) {
1294 fs = lookup(devices[i]);
1296 fs = create_fs_device(devices[i], 0, "auto",
1301 if (ignore_mounted && is_mounted(fs->device))
1303 fsck_device(fs, interactive);
1305 (max_running && (num_running >= max_running))) {
1306 struct fsck_instance *inst;
1310 status |= inst->exit_status;
1311 free_instance(inst);
1314 printf("----------------------------------\n");
1317 status |= wait_many(FLAG_WAIT_ALL);