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[] = {
78 static const char *really_wanted[] = {
89 #define BASE_MD "/dev/md"
92 * Global variables for options
94 char *devices[MAX_DEVICES];
96 int num_devices, num_args;
105 int parallel_root = 0;
107 int force_all_parallel = 0;
110 volatile int cancel_requested = 0;
114 struct fs_info *filesys_info = NULL, *filesys_last = NULL;
115 struct fsck_instance *instance_list;
116 const char *fsck_prefix_path = "/sbin:/sbin/fs.d:/sbin/fs:/etc/fs:/etc";
118 blkid_cache cache = NULL;
120 static char *string_copy(const char *s)
126 ret = malloc(strlen(s)+1);
132 static int ignore(struct fs_info *);
134 static char *skip_over_blank(char *cp)
136 while (*cp && isspace(*cp))
141 static char *skip_over_word(char *cp)
143 while (*cp && !isspace(*cp))
148 static void strip_line(char *line)
153 p = line + strlen(line) - 1;
154 if ((*p == '\n') || (*p == '\r'))
161 static char *parse_word(char **buf)
169 word = skip_over_blank(word);
170 next = skip_over_word(word);
177 static void parse_escape(char *word)
185 for (p = word, q = word; *p; p++, q++) {
204 for (i = 0; i < 3; i++, p++) {
207 ac = (ac * 8) + (*p - '0');
215 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;
263 if ((cp = strchr(line, '#')))
264 *cp = 0; /* Ignore everything after the comment char */
267 device = parse_word(&cp);
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);
275 return 0; /* Allow blank lines */
277 if (!mntpnt || !type)
280 parse_escape(device);
281 parse_escape(mntpnt);
285 parse_escape(passno);
287 dev = blkid_get_devname(cache, device, NULL);
291 if (strchr(type, ','))
294 fs = create_fs_device(device, mntpnt, type ? type : "auto", opts,
295 freq ? atoi(freq) : -1,
296 passno ? atoi(passno) : -1);
306 static void interpret_type(struct fs_info *fs)
310 if (strcmp(fs->type, "auto") != 0)
312 t = blkid_get_tag_value(cache, "TYPE", fs->device);
320 * Load the filesystem database from /etc/fstab
322 static void load_fs_info(const char *filename)
330 if ((f = fopen(filename, "r")) == NULL) {
331 fprintf(stderr, _("WARNING: couldn't open %s: %s\n"),
332 filename, strerror(errno));
337 if (!fgets(buf, sizeof(buf), f))
339 buf[sizeof(buf)-1] = 0;
340 if (parse_fstab_line(buf, &fs) < 0) {
341 fprintf(stderr, _("WARNING: bad format "
342 "on line %d of %s\n"), lineno, filename);
356 fprintf(stderr, _("\007\007\007"
357 "WARNING: Your /etc/fstab does not contain the fsck passno\n"
358 " field. I will kludge around things for you, but you\n"
359 " should fix your /etc/fstab file as soon as you can.\n\n"));
361 for (fs = filesys_info; fs; fs = fs->next) {
367 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
368 static struct fs_info *lookup(char *filesys)
372 /* No filesys name given. */
376 for (fs = filesys_info; fs; fs = fs->next) {
377 if (!strcmp(filesys, fs->device) ||
378 (fs->mountpt && !strcmp(filesys, fs->mountpt)))
385 /* Find fsck program for a given fs type. */
386 static char *find_fsck(char *type)
390 static char prog[256];
391 char *p = string_copy(fsck_path);
394 /* Are we looking for a program or just a type? */
395 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
397 for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
398 sprintf(prog, tpl, s, type);
399 if (stat(prog, &st) == 0) break;
402 return(s ? prog : NULL);
405 static int progress_active(NOARGS)
407 struct fsck_instance *inst;
409 for (inst = instance_list; inst; inst = inst->next) {
410 if (inst->flags & FLAG_DONE)
412 if (inst->flags & FLAG_PROGRESS)
419 * Execute a particular fsck program, and link it into the list of
420 * child processes we are waiting for.
422 static int execute(const char *type, const char *device, const char *mntpt,
425 char *s, *argv[80], prog[80];
427 struct fsck_instance *inst, *p;
430 inst = malloc(sizeof(struct fsck_instance));
433 memset(inst, 0, sizeof(struct fsck_instance));
435 sprintf(prog, "fsck.%s", type);
436 argv[0] = string_copy(prog);
439 for (i=0; i <num_args; i++)
440 argv[argc++] = string_copy(args[i]);
442 if (progress & !progress_active()) {
443 if ((strcmp(type, "ext2") == 0) ||
444 (strcmp(type, "ext3") == 0)) {
445 argv[argc++] = string_copy("-C0");
446 inst->flags |= FLAG_PROGRESS;
450 argv[argc++] = string_copy(device);
455 fprintf(stderr, _("fsck: %s: not found\n"), prog);
459 if (verbose || noexecute) {
460 printf("[%s (%d) -- %s] ", s, num_running,
461 mntpt ? mntpt : device);
462 for (i=0; i < argc; i++)
463 printf("%s ", argv[i]);
467 /* Fork and execute the correct program. */
470 else if ((pid = fork()) < 0) {
473 } else if (pid == 0) {
476 (void) execv(s, argv);
481 for (i=0; i < argc; i++)
485 inst->prog = string_copy(prog);
486 inst->type = string_copy(type);
487 inst->device = string_copy(device);
488 inst->base_device = base_device(device);
489 inst->start_time = time(0);
493 * Find the end of the list, so we add the instance on at the end.
495 for (p = instance_list; p && p->next; p = p->next);
500 instance_list = inst;
506 * Send a signal to all outstanding fsck child processes
508 static int kill_all(int signum)
510 struct fsck_instance *inst;
513 for (inst = instance_list; inst; inst = inst->next) {
514 if (inst->flags & FLAG_DONE)
516 kill(inst->pid, signum);
523 * Wait for one child process to exit; when it does, unlink it from
524 * the list of executing child processes, and return it.
526 static struct fsck_instance *wait_one(int flags)
530 struct fsck_instance *inst, *inst2, *prev;
537 inst = instance_list;
540 while (inst->next && (random() & 1)) {
545 inst->exit_status = 0;
550 * gcc -Wall fails saving throw against stupidity
551 * (inst and prev are thought to be uninitialized variables)
556 pid = waitpid(-1, &status, flags);
557 if (cancel_requested && !kill_sent) {
561 if ((pid == 0) && (flags & WNOHANG))
564 if ((errno == EINTR) || (errno == EAGAIN))
566 if (errno == ECHILD) {
568 _("%s: wait: No more child process?!?\n"),
575 for (prev = 0, inst = instance_list;
577 prev = inst, inst = inst->next) {
578 if (inst->pid == pid)
583 if (WIFEXITED(status))
584 status = WEXITSTATUS(status);
585 else if (WIFSIGNALED(status)) {
586 sig = WTERMSIG(status);
588 status = EXIT_UNCORRECTED;
590 printf(_("Warning... %s for device %s exited "
591 "with signal %d.\n"),
592 inst->prog, inst->device, sig);
596 printf(_("%s %s: status is %x, should never happen.\n"),
597 inst->prog, inst->device, status);
600 inst->exit_status = status;
601 if (progress && (inst->flags & FLAG_PROGRESS) &&
602 !progress_active()) {
603 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
604 if (inst2->flags & FLAG_DONE)
606 if (strcmp(inst2->type, "ext2") &&
607 strcmp(inst2->type, "ext3"))
610 * If we've just started the fsck, wait a tiny
611 * bit before sending the kill, to give it
612 * time to set up the signal handler
614 if (inst2->start_time < time(0)+2) {
617 kill(inst2->pid, SIGUSR1);
621 kill(inst2->pid, SIGUSR1);
622 inst2->flags |= FLAG_PROGRESS;
628 prev->next = inst->next;
630 instance_list = inst->next;
632 printf(_("Finished with %s (exit status %d)\n"),
633 inst->device, inst->exit_status);
638 #define FLAG_WAIT_ALL 0
639 #define FLAG_WAIT_ATLEAST_ONE 1
641 * Wait until all executing child processes have exited; return the
642 * logical OR of all of their exit code values.
644 static int wait_many(int flags)
646 struct fsck_instance *inst;
647 int global_status = 0;
650 while ((inst = wait_one(wait_flags))) {
651 global_status |= inst->exit_status;
654 if (noexecute && (flags & WNOHANG) && !(random() % 3))
657 if (flags & FLAG_WAIT_ATLEAST_ONE)
658 wait_flags = WNOHANG;
660 return global_status;
664 * Run the fsck program on a particular device
666 * If the type is specified using -t, and it isn't prefixed with "no"
667 * (as in "noext2") and only one filesystem type is specified, then
668 * use that type regardless of what is specified in /etc/fstab.
670 * If the type isn't specified by the user, then use either the type
671 * specified in /etc/fstab, or DEFAULT_FSTYPE.
673 static void fsck_device(struct fs_info *fs, int interactive)
680 if (strcmp(fs->type, "auto") != 0)
682 else if (fstype && strncmp(fstype, "no", 2) &&
683 strncmp(fstype, "opts=", 5) && strncmp(fstype, "loop", 4) &&
684 !strchr(fstype, ','))
687 type = DEFAULT_FSTYPE;
690 retval = execute(type, fs->device, fs->mountpt, interactive);
692 fprintf(stderr, _("%s: Error %d while executing fsck.%s "
693 "for %s\n"), progname, retval, type, fs->device);
700 * Deal with the fsck -t argument.
702 struct fs_type_compile {
708 #define FS_TYPE_NORMAL 0
709 #define FS_TYPE_OPT 1
710 #define FS_TYPE_NEGOPT 2
712 static const char *fs_type_syntax_error =
713 N_("Either all or none of the filesystem types passed to -t must be prefixed\n"
714 "with 'no' or '!'.\n");
716 static void compile_fs_type(char *fs_type, struct fs_type_compile *cmp)
720 int negate, first_negate = 1;
723 for (cp=fs_type; *cp; cp++) {
729 cmp->list = malloc(num * sizeof(char *));
730 cmp->type = malloc(num * sizeof(int));
731 if (!cmp->list || !cmp->type) {
732 fprintf(stderr, _("Couldn't allocate memory for "
733 "filesystem types\n"));
736 memset(cmp->list, 0, num * sizeof(char *));
737 memset(cmp->type, 0, num * sizeof(int));
743 list = string_copy(fs_type);
745 s = strtok(list, ",");
748 if (strncmp(s, "no", 2) == 0) {
751 } else if (*s == '!') {
755 if (strcmp(s, "loop") == 0)
756 /* loop is really short-hand for opts=loop */
757 goto loop_special_case;
758 else if (strncmp(s, "opts=", 5) == 0) {
761 cmp->type[num] = negate ? FS_TYPE_NEGOPT : FS_TYPE_OPT;
764 cmp->negate = negate;
767 if ((negate && !cmp->negate) ||
768 (!negate && cmp->negate)) {
769 fprintf(stderr, _(fs_type_syntax_error));
774 printf("Adding %s to list (type %d).\n", s, cmp->type[num]);
776 cmp->list[num++] = string_copy(s);
777 s = strtok(NULL, ",");
783 * This function returns true if a particular option appears in a
784 * comma-delimited options list
786 static int opt_in_list(char *opt, char *optlist)
792 list = string_copy(optlist);
794 s = strtok(list, ",");
796 if (strcmp(s, opt) == 0) {
800 s = strtok(NULL, ",");
806 /* See if the filesystem matches the criteria given by the -t option */
807 static int fs_match(struct fs_info *fs, struct fs_type_compile *cmp)
809 int n, ret = 0, checked_type = 0;
812 if (cmp->list == 0 || cmp->list[0] == 0)
815 for (n=0; (cp = cmp->list[n]); n++) {
816 switch (cmp->type[n]) {
819 if (strcmp(cp, fs->type) == 0) {
824 if (opt_in_list(cp, fs->opts))
828 if (!opt_in_list(cp, fs->opts))
833 if (checked_type == 0)
835 return (cmp->negate ? !ret : ret);
838 /* Check if we should ignore this filesystem. */
839 static int ignore(struct fs_info *fs)
845 * If the pass number is 0, ignore it.
853 * If a specific fstype is specified, and it doesn't match,
856 if (!fs_match(fs, &fs_type_compiled)) return 1;
858 /* Are we ignoring this type? */
859 for(ip = ignored_types; *ip; ip++)
860 if (strcmp(fs->type, *ip) == 0) return 1;
862 /* Do we really really want to check this fs? */
863 for(ip = really_wanted; *ip; ip++)
864 if (strcmp(fs->type, *ip) == 0) {
869 /* See if the <fsck.fs> program is available. */
870 if (find_fsck(fs->type) == NULL) {
872 fprintf(stderr, _("fsck: cannot check %s: fsck.%s not found\n"),
873 fs->device, fs->type);
877 /* We can and want to check this file system type. */
882 * Returns TRUE if a partition on the same disk is already being
885 static int device_already_active(char *device)
887 struct fsck_instance *inst;
890 if (force_all_parallel)
894 /* Don't check a soft raid disk with any other disk */
896 (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1) ||
897 !strncmp(device, BASE_MD, sizeof(BASE_MD)-1)))
901 base = base_device(device);
903 * If we don't know the base device, assume that the device is
904 * already active if there are any fsck instances running.
907 return (instance_list != 0);
908 for (inst = instance_list; inst; inst = inst->next) {
909 if (!inst->base_device || !strcmp(base, inst->base_device)) {
918 /* Check all file systems, using the /etc/fstab table. */
919 static int check_all(NOARGS)
921 struct fs_info *fs = NULL;
922 int status = EXIT_OK;
923 int not_done_yet = 1;
928 printf(_("Checking all file systems.\n"));
931 * Do an initial scan over the filesystem; mark filesystems
932 * which should be ignored as done, and resolve any "auto"
933 * filesystem types (done as a side-effect of calling ignore()).
935 for (fs = filesys_info; fs; fs = fs->next) {
937 fs->flags |= FLAG_DONE;
941 * Find and check the root filesystem.
943 if (!parallel_root) {
944 for (fs = filesys_info; fs; fs = fs->next) {
945 if (!strcmp(fs->mountpt, "/"))
949 if (!skip_root && !ignore(fs)) {
951 status |= wait_many(FLAG_WAIT_ALL);
952 if (status > EXIT_NONDESTRUCT)
955 fs->flags |= FLAG_DONE;
959 * This is for the bone-headed user who enters the root
960 * filesystem twice. Skip root will skep all root entries.
963 for (fs = filesys_info; fs; fs = fs->next)
964 if (!strcmp(fs->mountpt, "/"))
965 fs->flags |= FLAG_DONE;
967 while (not_done_yet) {
971 for (fs = filesys_info; fs; fs = fs->next) {
972 if (cancel_requested)
974 if (fs->flags & FLAG_DONE)
977 * If the filesystem's pass number is higher
978 * than the current pass number, then we don't
981 if (fs->passno > passno) {
986 * If a filesystem on a particular device has
987 * already been spawned, then we need to defer
988 * this to another pass.
990 if (device_already_active(fs->device)) {
995 * Spawn off the fsck process
997 fsck_device(fs, serialize);
998 fs->flags |= FLAG_DONE;
1001 * Only do one filesystem at a time, or if we
1002 * have a limit on the number of fsck's extant
1003 * at one time, apply that limit.
1006 (max_running && (num_running >= max_running))) {
1011 if (cancel_requested)
1014 printf(_("--waiting-- (pass %d)\n"), passno);
1015 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
1016 FLAG_WAIT_ATLEAST_ONE);
1019 printf("----------------------------------\n");
1024 if (cancel_requested && !kill_sent) {
1028 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
1032 static void usage(NOARGS)
1035 _("Usage: fsck [-ACNPRTV] [-t fstype] [fs-options] [filesys ...]\n"));
1039 #ifdef HAVE_SIGNAL_H
1040 static void signal_cancel(int sig)
1046 static void PRS(int argc, char *argv[])
1049 char *arg, *dev, *tmp = 0;
1052 int opts_for_fsck = 0;
1053 #ifdef HAVE_SIGNAL_H
1054 struct sigaction sa;
1057 * Set up signal action
1059 memset(&sa, 0, sizeof(struct sigaction));
1060 sa.sa_handler = signal_cancel;
1061 sigaction(SIGINT, &sa, 0);
1062 sigaction(SIGTERM, &sa, 0);
1071 for (i=1; i < argc; i++) {
1075 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
1076 if (num_devices >= MAX_DEVICES) {
1077 fprintf(stderr, _("%s: too many devices\n"),
1081 dev = blkid_get_devname(cache, arg, NULL);
1082 if (!dev && strchr(arg, '=')) {
1084 * Check to see if we failed because
1085 * /proc/partitions isn't found.
1087 if (access("/proc/partitions", R_OK) < 0) {
1088 fprintf(stderr, "Couldn't open /proc/partitions: %s\n",
1090 fprintf(stderr, "Is /proc mounted?\n");
1094 * Check to see if this is because
1095 * we're not running as root
1099 "Must be root to scan for matching filesystems: %s\n", arg);
1102 "Couldn't find matching filesystem: %s\n", arg);
1105 devices[num_devices++] = dev ? dev : string_copy(arg);
1108 if (arg[0] != '-' || opts_for_fsck) {
1109 if (num_args >= MAX_ARGS) {
1110 fprintf(stderr, _("%s: too many arguments\n"),
1114 args[num_args++] = string_copy(arg);
1117 for (j=1; arg[j]; j++) {
1118 if (opts_for_fsck) {
1119 options[++opt] = arg[j];
1155 else if ((i+1) < argc)
1159 fstype = string_copy(tmp);
1160 compile_fs_type(fstype, &fs_type_compiled);
1169 options[++opt] = arg[j];
1176 options[++opt] = '\0';
1177 if (num_args >= MAX_ARGS) {
1179 _("%s: too many arguments\n"),
1183 args[num_args++] = string_copy(options);
1187 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1188 force_all_parallel++;
1189 if ((tmp = getenv("FSCK_MAX_INST")))
1190 max_running = atoi(tmp);
1193 int main(int argc, char *argv[])
1196 int interactive = 0;
1197 char *oldpath = getenv("PATH");
1201 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
1202 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
1205 setlocale(LC_MESSAGES, "");
1206 setlocale(LC_CTYPE, "");
1207 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1208 textdomain(NLS_CAT_NAME);
1210 blkid_get_cache(&cache, NULL);
1214 printf("fsck %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
1216 fstab = getenv("FSTAB_FILE");
1218 fstab = _PATH_MNTTAB;
1219 load_fs_info(fstab);
1221 /* Update our search path to include uncommon directories. */
1223 fsck_path = malloc (strlen (fsck_prefix_path) + 1 +
1224 strlen (oldpath) + 1);
1225 strcpy (fsck_path, fsck_prefix_path);
1226 strcat (fsck_path, ":");
1227 strcat (fsck_path, oldpath);
1229 fsck_path = string_copy(fsck_prefix_path);
1232 if ((num_devices == 1) || (serialize))
1235 /* If -A was specified ("check all"), do that! */
1239 if (num_devices == 0) {
1244 for (i = 0 ; i < num_devices; i++) {
1245 if (cancel_requested) {
1252 fs = lookup(devices[i]);
1254 fs = create_fs_device(devices[i], 0, "auto",
1259 fsck_device(fs, interactive);
1261 (max_running && (num_running >= max_running))) {
1262 struct fsck_instance *inst;
1266 status |= inst->exit_status;
1267 free_instance(inst);
1270 printf("----------------------------------\n");
1273 status |= wait_many(FLAG_WAIT_ALL);
1275 blkid_put_cache(cache);