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 * Usage: fsck [-ACVRNTM] [-s] [-t fstype] [fs-options] device
13 * Miquel van Smoorenburg (miquels@drinkel.ow.org) 20-Oct-1994:
14 * o Changed -t fstype to behave like with mount when -A (all file
15 * systems) or -M (like mount) is specified.
16 * o fsck looks if it can find the fsck.type program to decide
17 * if it should ignore the fs type. This way more fsck programs
18 * can be added without changing this front-end.
19 * o -R flag skip root file system.
21 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999 Theodore Ts'o.
24 * This file may be redistributed under the terms of the GNU Public
29 #include <sys/types.h>
31 #include <sys/signal.h>
57 #include "../version.h"
58 #include "nls-enable.h"
60 #include "blkid/blkid.h"
63 #define _PATH_MNTTAB "/etc/fstab"
66 static const char *ignored_types[] = {
76 static const char *really_wanted[] = {
84 #define BASE_MD "/dev/md"
87 * Global variables for options
89 char *devices[MAX_DEVICES];
91 int num_devices, num_args;
100 int parallel_root = 0;
102 int force_all_parallel = 0;
105 volatile int cancel_requested = 0;
109 struct fs_info *filesys_info = NULL, *filesys_last = NULL;
110 struct fsck_instance *instance_list;
111 const char *fsck_prefix_path = "/sbin:/sbin/fs.d:/sbin/fs:/etc/fs:/etc";
113 blkid_cache cache = NULL;
115 static char *string_copy(const char *s)
121 ret = malloc(strlen(s)+1);
127 static int ignore(struct fs_info *);
129 static char *skip_over_blank(char *cp)
131 while (*cp && isspace(*cp))
136 static char *skip_over_word(char *cp)
138 while (*cp && !isspace(*cp))
143 static void strip_line(char *line)
148 p = line + strlen(line) - 1;
149 if ((*p == '\n') || (*p == '\r'))
156 static char *parse_word(char **buf)
164 word = skip_over_blank(word);
165 next = skip_over_word(word);
172 static void parse_escape(char *word)
180 for (p = word, q = word; *p; p++, q++) {
199 for (i = 0; i < 3; i++, p++) {
202 ac = (ac * 8) + (*p - '0');
210 static void free_instance(struct fsck_instance *i)
217 free(i->base_device);
222 static struct fs_info *create_fs_device(char *device, char *mntpnt,
223 char *type, char *opts, int freq,
228 if (!(fs = malloc(sizeof(struct fs_info))))
231 fs->device = string_copy(device);
232 fs->mountpt = string_copy(mntpnt);
233 fs->type = string_copy(type);
234 fs->opts = string_copy(opts ? opts : "");
243 filesys_last->next = fs;
251 static int parse_fstab_line(char *line, struct fs_info **ret_fs)
253 char *dev, *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
259 if ((cp = strchr(line, '#')))
260 *cp = 0; /* Ignore everything after the comment char */
263 device = parse_word(&cp);
264 mntpnt = parse_word(&cp);
265 type = parse_word(&cp);
266 opts = parse_word(&cp);
267 freq = parse_word(&cp);
268 passno = parse_word(&cp);
271 return 0; /* Allow blank lines */
273 if (!mntpnt || !type)
276 parse_escape(device);
277 parse_escape(mntpnt);
281 parse_escape(passno);
283 dev = blkid_get_devname(cache, device, NULL);
287 if (strcmp(type, "auto") == 0 || (strchr(type, ',') != 0))
288 t = blkid_get_tag_value(cache, "TYPE", device);
292 fs = create_fs_device(device, mntpnt, type, opts,
293 freq ? atoi(freq) : -1,
294 passno ? atoi(passno) : -1);
307 * Load the filesystem database from /etc/fstab
309 static void load_fs_info(const char *filename)
317 if ((f = fopen(filename, "r")) == NULL) {
318 fprintf(stderr, _("WARNING: couldn't open %s: %s\n"),
319 filename, strerror(errno));
324 if (!fgets(buf, sizeof(buf), f))
326 buf[sizeof(buf)-1] = 0;
327 if (parse_fstab_line(buf, &fs) < 0) {
328 fprintf(stderr, _("WARNING: bad format "
329 "on line %d of %s\n"), lineno, filename);
343 fprintf(stderr, _("\007\007\007"
344 "WARNING: Your /etc/fstab does not contain the fsck passno\n"
345 " field. I will kludge around things for you, but you\n"
346 " should fix your /etc/fstab file as soon as you can.\n\n"));
348 for (fs = filesys_info; fs; fs = fs->next) {
354 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
355 static struct fs_info *lookup(char *filesys)
360 /* No filesys name given. */
364 for (fs = filesys_info; fs; fs = fs->next) {
365 if (!strcmp(filesys, fs->device) ||
366 (fs->mountpt && !strcmp(filesys, fs->mountpt)))
373 /* Find fsck program for a given fs type. */
374 static char *find_fsck(char *type)
378 static char prog[256];
379 char *p = string_copy(fsck_path);
382 /* Are we looking for a program or just a type? */
383 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
385 for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
386 sprintf(prog, tpl, s, type);
387 if (stat(prog, &st) == 0) break;
390 return(s ? prog : NULL);
393 static int progress_active(NOARGS)
395 struct fsck_instance *inst;
397 for (inst = instance_list; inst; inst = inst->next) {
398 if (inst->flags & FLAG_DONE)
400 if (inst->flags & FLAG_PROGRESS)
407 * Execute a particular fsck program, and link it into the list of
408 * child processes we are waiting for.
410 static int execute(const char *type, const char *device, const char *mntpt,
413 char *s, *argv[80], prog[80];
415 struct fsck_instance *inst, *p;
418 inst = malloc(sizeof(struct fsck_instance));
421 memset(inst, 0, sizeof(struct fsck_instance));
423 sprintf(prog, "fsck.%s", type);
424 argv[0] = string_copy(prog);
427 for (i=0; i <num_args; i++)
428 argv[argc++] = string_copy(args[i]);
430 if (progress & !progress_active()) {
431 if ((strcmp(type, "ext2") == 0) ||
432 (strcmp(type, "ext3") == 0)) {
433 argv[argc++] = string_copy("-C0");
434 inst->flags |= FLAG_PROGRESS;
438 argv[argc++] = string_copy(device);
443 fprintf(stderr, _("fsck: %s: not found\n"), prog);
447 if (verbose || noexecute) {
448 printf("[%s (%d) -- %s] ", s, num_running,
449 mntpt ? mntpt : device);
450 for (i=0; i < argc; i++)
451 printf("%s ", argv[i]);
455 /* Fork and execute the correct program. */
458 else if ((pid = fork()) < 0) {
461 } else if (pid == 0) {
464 (void) execv(s, argv);
469 for (i=0; i < argc; i++)
473 inst->prog = string_copy(prog);
474 inst->type = string_copy(type);
475 inst->device = string_copy(device);
476 inst->base_device = base_device(device);
477 inst->start_time = time(0);
481 * Find the end of the list, so we add the instance on at the end.
483 for (p = instance_list; p && p->next; p = p->next);
488 instance_list = inst;
494 * Send a signal to all outstanding fsck child processes
496 static int kill_all(int signum)
498 struct fsck_instance *inst;
501 for (inst = instance_list; inst; inst = inst->next) {
502 if (inst->flags & FLAG_DONE)
504 kill(inst->pid, signum);
511 * Wait for one child process to exit; when it does, unlink it from
512 * the list of executing child processes, and return it.
514 static struct fsck_instance *wait_one(int flags)
518 struct fsck_instance *inst, *inst2, *prev;
525 inst = instance_list;
528 while (inst->next && (random() & 1)) {
533 inst->exit_status = 0;
538 * gcc -Wall fails saving throw against stupidity
539 * (inst and prev are thought to be uninitialized variables)
544 pid = waitpid(-1, &status, flags);
545 if (cancel_requested && !kill_sent) {
549 if ((pid == 0) && (flags & WNOHANG))
552 if ((errno == EINTR) || (errno == EAGAIN))
554 if (errno == ECHILD) {
556 _("%s: wait: No more child process?!?\n"),
563 for (prev = 0, inst = instance_list;
565 prev = inst, inst = inst->next) {
566 if (inst->pid == pid)
571 if (WIFEXITED(status))
572 status = WEXITSTATUS(status);
573 else if (WIFSIGNALED(status)) {
574 sig = WTERMSIG(status);
576 status = EXIT_UNCORRECTED;
578 printf(_("Warning... %s for device %s exited "
579 "with signal %d.\n"),
580 inst->prog, inst->device, sig);
584 printf(_("%s %s: status is %x, should never happen.\n"),
585 inst->prog, inst->device, status);
588 inst->exit_status = status;
589 if (progress && (inst->flags & FLAG_PROGRESS) &&
590 !progress_active()) {
591 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
592 if (inst2->flags & FLAG_DONE)
594 if (strcmp(inst2->type, "ext2") &&
595 strcmp(inst2->type, "ext3"))
598 * If we've just started the fsck, wait a tiny
599 * bit before sending the kill, to give it
600 * time to set up the signal handler
602 if (inst2->start_time < time(0)+2) {
605 kill(inst2->pid, SIGUSR1);
609 kill(inst2->pid, SIGUSR1);
610 inst2->flags |= FLAG_PROGRESS;
616 prev->next = inst->next;
618 instance_list = inst->next;
620 printf(_("Finished with %s (exit status %d)\n"),
621 inst->device, inst->exit_status);
627 * Wait until all executing child processes have exited; return the
628 * logical OR of all of their exit code values.
630 static int wait_all(int flags)
632 struct fsck_instance *inst;
633 int global_status = 0;
635 while ((inst = wait_one(flags))) {
636 global_status |= inst->exit_status;
639 if (noexecute && (flags & WNOHANG) && !(random() % 3))
643 return global_status;
647 * Run the fsck program on a particular device
649 * If the type is specified using -t, and it isn't prefixed with "no"
650 * (as in "noext2") and only one filesystem type is specified, then
651 * use that type regardless of what is specified in /etc/fstab.
653 * If the type isn't specified by the user, then use either the type
654 * specified in /etc/fstab, or DEFAULT_FSTYPE.
656 static void fsck_device(struct fs_info *fs, int interactive)
658 const char *type = 0;
661 if (fstype && strncmp(fstype, "no", 2) &&
662 strncmp(fstype, "opts=", 5) && strncmp(fstype, "loop", 4) &&
663 !strchr(fstype, ','))
666 type = fs->type ? fs->type : DEFAULT_FSTYPE;
669 retval = execute(type, fs->device, fs->mountpt, interactive);
671 fprintf(stderr, _("%s: Error %d while executing fsck.%s "
672 "for %s\n"), progname, retval, type, fs->device);
679 * Deal with the fsck -t argument.
681 struct fs_type_compile {
687 #define FS_TYPE_NORMAL 0
688 #define FS_TYPE_OPT 1
689 #define FS_TYPE_NEGOPT 2
691 static const char *fs_type_syntax_error =
692 N_("Either all or none of the filesystem types passed to -t must be prefixed\n"
693 "with 'no' or '!'.\n");
695 static void compile_fs_type(char *fs_type, struct fs_type_compile *cmp)
699 int negate, first_negate = 1;
702 for (cp=fs_type; *cp; cp++) {
708 cmp->list = malloc(num * sizeof(char *));
709 cmp->type = malloc(num * sizeof(int));
710 if (!cmp->list || !cmp->type) {
711 fprintf(stderr, _("Couldn't allocate memory for "
712 "filesystem types\n"));
715 memset(cmp->list, 0, num * sizeof(char *));
716 memset(cmp->type, 0, num * sizeof(int));
722 list = string_copy(fs_type);
724 s = strtok(list, ",");
727 if (strncmp(s, "no", 2) == 0) {
730 } else if (*s == '!') {
734 if (strcmp(s, "loop") == 0)
735 /* loop is really short-hand for opts=loop */
736 goto loop_special_case;
737 else if (strncmp(s, "opts=", 5) == 0) {
740 cmp->type[num] = negate ? FS_TYPE_NEGOPT : FS_TYPE_OPT;
743 cmp->negate = negate;
746 if ((negate && !cmp->negate) ||
747 (!negate && cmp->negate)) {
748 fprintf(stderr, _(fs_type_syntax_error));
753 printf("Adding %s to list (type %d).\n", s, cmp->type[num]);
755 cmp->list[num++] = string_copy(s);
756 s = strtok(NULL, ",");
762 * This function returns true if a particular option appears in a
763 * comma-delimited options list
765 static int opt_in_list(char *opt, char *optlist)
771 list = string_copy(optlist);
773 s = strtok(list, ",");
775 if (strcmp(s, opt) == 0) {
779 s = strtok(NULL, ",");
785 /* See if the filesystem matches the criteria given by the -t option */
786 static int fs_match(struct fs_info *fs, struct fs_type_compile *cmp)
788 int n, ret = 0, checked_type = 0;
791 if (cmp->list == 0 || cmp->list[0] == 0)
794 for (n=0; (cp = cmp->list[n]); n++) {
795 switch (cmp->type[n]) {
798 if (strcmp(cp, fs->type) == 0) {
803 if (opt_in_list(cp, fs->opts))
807 if (!opt_in_list(cp, fs->opts))
812 if (checked_type == 0)
814 return (cmp->negate ? !ret : ret);
817 /* Check if we should ignore this filesystem. */
818 static int ignore(struct fs_info *fs)
824 * If the pass number is 0, ignore it.
830 * If a specific fstype is specified, and it doesn't match,
833 if (!fs_match(fs, &fs_type_compiled)) return 1;
835 /* Are we ignoring this type? */
836 for(ip = ignored_types; *ip; ip++)
837 if (strcmp(fs->type, *ip) == 0) return 1;
839 /* Do we really really want to check this fs? */
840 for(ip = really_wanted; *ip; ip++)
841 if (strcmp(fs->type, *ip) == 0) {
846 /* See if the <fsck.fs> program is available. */
847 if (find_fsck(fs->type) == NULL) {
849 fprintf(stderr, _("fsck: cannot check %s: fsck.%s not found\n"),
850 fs->device, fs->type);
854 /* We can and want to check this file system type. */
859 * Returns TRUE if a partition on the same disk is already being
862 static int device_already_active(char *device)
864 struct fsck_instance *inst;
867 if (force_all_parallel)
871 /* Don't check a soft raid disk with any other disk */
873 (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1) ||
874 !strncmp(device, BASE_MD, sizeof(BASE_MD)-1)))
878 base = base_device(device);
880 * If we don't know the base device, assume that the device is
881 * already active if there are any fsck instances running.
884 return (instance_list != 0);
885 for (inst = instance_list; inst; inst = inst->next) {
886 if (!inst->base_device || !strcmp(base, inst->base_device)) {
895 /* Check all file systems, using the /etc/fstab table. */
896 static int check_all(NOARGS)
898 struct fs_info *fs = NULL;
899 int status = EXIT_OK;
900 int not_done_yet = 1;
905 printf(_("Checking all file systems.\n"));
908 * Do an initial scan over the filesystem; mark filesystems
909 * which should be ignored as done, and resolve LABEL= and
910 * UUID= specifications to the real device.
912 for (fs = filesys_info; fs; fs = fs->next) {
914 fs->flags |= FLAG_DONE;
918 * Find and check the root filesystem.
920 if (!parallel_root) {
921 for (fs = filesys_info; fs; fs = fs->next) {
922 if (!strcmp(fs->mountpt, "/"))
926 if (!skip_root && !ignore(fs)) {
928 status |= wait_all(0);
929 if (status > EXIT_NONDESTRUCT)
932 fs->flags |= FLAG_DONE;
936 * This is for the bone-headed user who enters the root
937 * filesystem twice. Skip root will skep all root entries.
940 for (fs = filesys_info; fs; fs = fs->next)
941 if (!strcmp(fs->mountpt, "/"))
942 fs->flags |= FLAG_DONE;
944 while (not_done_yet) {
948 for (fs = filesys_info; fs; fs = fs->next) {
949 if (cancel_requested)
951 if (fs->flags & FLAG_DONE)
954 * If the filesystem's pass number is higher
955 * than the current pass number, then we don't
958 if (fs->passno > passno) {
963 * If a filesystem on a particular device has
964 * already been spawned, then we need to defer
965 * this to another pass.
967 if (device_already_active(fs->device)) {
972 * Spawn off the fsck process
974 fsck_device(fs, serialize);
975 fs->flags |= FLAG_DONE;
978 * Only do one filesystem at a time, or if we
979 * have a limit on the number of fsck's extant
980 * at one time, apply that limit.
983 (max_running && (num_running >= max_running))) {
988 if (cancel_requested)
991 printf(_("--waiting-- (pass %d)\n"), passno);
992 status |= wait_all(pass_done ? 0 : WNOHANG);
995 printf("----------------------------------\n");
1000 if (cancel_requested && !kill_sent) {
1004 status |= wait_all(0);
1008 static void usage(NOARGS)
1011 _("Usage: fsck [-ACNPRTV] [-t fstype] [fs-options] [filesys ...]\n"));
1015 #ifdef HAVE_SIGNAL_H
1016 static void signal_cancel(int sig)
1022 static void PRS(int argc, char *argv[])
1025 char *arg, *dev, *tmp = 0;
1028 int opts_for_fsck = 0;
1029 #ifdef HAVE_SIGNAL_H
1030 struct sigaction sa;
1033 * Set up signal action
1035 memset(&sa, 0, sizeof(struct sigaction));
1036 sa.sa_handler = signal_cancel;
1037 sigaction(SIGINT, &sa, 0);
1038 sigaction(SIGTERM, &sa, 0);
1047 for (i=1; i < argc; i++) {
1051 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
1052 if (num_devices >= MAX_DEVICES) {
1053 fprintf(stderr, _("%s: too many devices\n"),
1057 dev = blkid_get_devname(cache, arg, NULL);
1058 if (!dev && strchr(arg, '=')) {
1060 * Check to see if we failed because
1061 * /proc/partitions isn't found.
1063 if (access("/proc/partitions", R_OK) < 0) {
1064 fprintf(stderr, "Couldn't open /proc/partitions: %s\n",
1066 fprintf(stderr, "Is /proc mounted?\n");
1070 * Check to see if this is because
1071 * we're not running as root
1075 "Must be root to scan for matching filesystems: %s\n", arg);
1078 "Couldn't find matching filesystem: %s\n", arg);
1081 devices[num_devices++] = dev ? dev : string_copy(arg);
1084 if (arg[0] != '-' || opts_for_fsck) {
1085 if (num_args >= MAX_ARGS) {
1086 fprintf(stderr, _("%s: too many arguments\n"),
1090 args[num_args++] = string_copy(arg);
1093 for (j=1; arg[j]; j++) {
1094 if (opts_for_fsck) {
1095 options[++opt] = arg[j];
1131 else if ((i+1) < argc)
1135 fstype = string_copy(tmp);
1136 compile_fs_type(fstype, &fs_type_compiled);
1145 options[++opt] = arg[j];
1152 options[++opt] = '\0';
1153 if (num_args >= MAX_ARGS) {
1155 _("%s: too many arguments\n"),
1159 args[num_args++] = string_copy(options);
1163 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1164 force_all_parallel++;
1165 if ((tmp = getenv("FSCK_MAX_INST")))
1166 max_running = atoi(tmp);
1169 int main(int argc, char *argv[])
1172 int interactive = 0;
1173 char *type, *oldpath = getenv("PATH");
1178 setlocale(LC_MESSAGES, "");
1179 setlocale(LC_CTYPE, "");
1180 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1181 textdomain(NLS_CAT_NAME);
1183 blkid_get_cache(&cache, NULL);
1187 printf("fsck %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
1189 fstab = getenv("FSTAB_FILE");
1191 fstab = _PATH_MNTTAB;
1192 load_fs_info(fstab);
1194 /* Update our search path to include uncommon directories. */
1196 fsck_path = malloc (strlen (fsck_prefix_path) + 1 +
1197 strlen (oldpath) + 1);
1198 strcpy (fsck_path, fsck_prefix_path);
1199 strcat (fsck_path, ":");
1200 strcat (fsck_path, oldpath);
1202 fsck_path = string_copy(fsck_prefix_path);
1205 if ((num_devices == 1) || (serialize))
1208 /* If -A was specified ("check all"), do that! */
1212 if (num_devices == 0) {
1217 for (i = 0 ; i < num_devices; i++) {
1218 if (cancel_requested) {
1225 fs = lookup(devices[i]);
1227 type = blkid_get_tag_value(cache, "TYPE", devices[i]);
1229 fprintf(stderr, _("Could not determine "
1230 "filesystem type for %s\n"),
1234 fs = create_fs_device(devices[i], 0, type, 0, -1, -1);
1239 fsck_device(fs, interactive);
1241 (max_running && (num_running >= max_running))) {
1242 struct fsck_instance *inst;
1246 status |= inst->exit_status;
1247 free_instance(inst);
1250 printf("----------------------------------\n");
1253 status |= wait_all(0);
1255 blkid_put_cache(cache);