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>
55 #include "../version.h"
56 #include "nls-enable.h"
58 #include "blkid/blkid.h"
61 #define _PATH_MNTTAB "/etc/fstab"
64 static const char *ignored_types[] = {
74 static const char *really_wanted[] = {
82 #define BASE_MD "/dev/md"
85 * Global variables for options
87 char *devices[MAX_DEVICES];
89 int num_devices, num_args;
98 int parallel_root = 0;
100 int force_all_parallel = 0;
103 volatile int cancel_requested = 0;
107 struct fs_info *filesys_info = NULL, *filesys_last = NULL;
108 struct fsck_instance *instance_list;
109 const char *fsck_prefix_path = "/sbin:/sbin/fs.d:/sbin/fs:/etc/fs:/etc";
111 blkid_cache cache = NULL;
113 static char *string_copy(const char *s)
119 ret = malloc(strlen(s)+1);
125 static int ignore(struct fs_info *);
127 static char *skip_over_blank(char *cp)
129 while (*cp && isspace(*cp))
134 static char *skip_over_word(char *cp)
136 while (*cp && !isspace(*cp))
141 static void strip_line(char *line)
146 p = line + strlen(line) - 1;
147 if ((*p == '\n') || (*p == '\r'))
154 static char *parse_word(char **buf)
162 word = skip_over_blank(word);
163 next = skip_over_word(word);
170 static void parse_escape(char *word)
178 for (p = word, q = word; *p; p++, q++) {
197 for (i = 0; i < 3; i++, p++) {
200 ac = (ac * 8) + (*p - '0');
208 static void free_instance(struct fsck_instance *i)
215 free(i->base_device);
220 static struct fs_info *create_fs_device(char *device, char *mntpnt,
221 char *type, char *opts, int freq,
226 if (!(fs = malloc(sizeof(struct fs_info))))
229 fs->device = string_copy(device);
230 fs->mountpt = string_copy(mntpnt);
231 fs->type = string_copy(type);
232 fs->opts = string_copy(opts ? opts : "");
241 filesys_last->next = fs;
249 static int parse_fstab_line(char *line, struct fs_info **ret_fs)
251 char *dev, *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
257 if ((cp = strchr(line, '#')))
258 *cp = 0; /* Ignore everything after the comment char */
261 device = parse_word(&cp);
262 mntpnt = parse_word(&cp);
263 type = parse_word(&cp);
264 opts = parse_word(&cp);
265 freq = parse_word(&cp);
266 passno = parse_word(&cp);
269 return 0; /* Allow blank lines */
271 if (!mntpnt || !type)
274 parse_escape(device);
275 parse_escape(mntpnt);
279 parse_escape(passno);
281 dev = blkid_get_devname(cache, device, NULL);
285 if (strcmp(type, "auto") == 0 || (strchr(type, ',') != 0))
286 t = blkid_get_tag_value(cache, "TYPE", device);
290 fs = create_fs_device(device, mntpnt, type, opts,
291 freq ? atoi(freq) : -1,
292 passno ? atoi(passno) : -1);
305 * Load the filesystem database from /etc/fstab
307 static void load_fs_info(const char *filename)
315 if ((f = fopen(filename, "r")) == NULL) {
316 fprintf(stderr, _("WARNING: couldn't open %s: %s\n"),
317 filename, strerror(errno));
322 if (!fgets(buf, sizeof(buf), f))
324 buf[sizeof(buf)-1] = 0;
325 if (parse_fstab_line(buf, &fs) < 0) {
326 fprintf(stderr, _("WARNING: bad format "
327 "on line %d of %s\n"), lineno, filename);
341 fprintf(stderr, _("\007\007\007"
342 "WARNING: Your /etc/fstab does not contain the fsck passno\n"
343 " field. I will kludge around things for you, but you\n"
344 " should fix your /etc/fstab file as soon as you can.\n\n"));
346 for (fs = filesys_info; fs; fs = fs->next) {
352 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
353 static struct fs_info *lookup(char *filesys)
358 /* No filesys name given. */
362 for (fs = filesys_info; fs; fs = fs->next) {
363 if (!strcmp(filesys, fs->device) ||
364 (fs->mountpt && !strcmp(filesys, fs->mountpt)))
371 /* Find fsck program for a given fs type. */
372 static char *find_fsck(char *type)
376 static char prog[256];
377 char *p = string_copy(fsck_path);
380 /* Are we looking for a program or just a type? */
381 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
383 for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
384 sprintf(prog, tpl, s, type);
385 if (stat(prog, &st) == 0) break;
388 return(s ? prog : NULL);
391 static int progress_active(NOARGS)
393 struct fsck_instance *inst;
395 for (inst = instance_list; inst; inst = inst->next) {
396 if (inst->flags & FLAG_DONE)
398 if (inst->flags & FLAG_PROGRESS)
405 * Execute a particular fsck program, and link it into the list of
406 * child processes we are waiting for.
408 static int execute(const char *type, const char *device, const char *mntpt,
411 char *s, *argv[80], prog[80];
413 struct fsck_instance *inst, *p;
416 inst = malloc(sizeof(struct fsck_instance));
419 memset(inst, 0, sizeof(struct fsck_instance));
421 sprintf(prog, "fsck.%s", type);
422 argv[0] = string_copy(prog);
425 for (i=0; i <num_args; i++)
426 argv[argc++] = string_copy(args[i]);
428 if (progress & !progress_active()) {
429 if ((strcmp(type, "ext2") == 0) ||
430 (strcmp(type, "ext3") == 0)) {
431 argv[argc++] = string_copy("-C0");
432 inst->flags |= FLAG_PROGRESS;
436 argv[argc++] = string_copy(device);
441 fprintf(stderr, _("fsck: %s: not found\n"), prog);
445 if (verbose || noexecute) {
446 printf("[%s (%d) -- %s] ", s, num_running,
447 mntpt ? mntpt : device);
448 for (i=0; i < argc; i++)
449 printf("%s ", argv[i]);
453 /* Fork and execute the correct program. */
456 else if ((pid = fork()) < 0) {
459 } else if (pid == 0) {
462 (void) execv(s, argv);
467 for (i=0; i < argc; i++)
471 inst->prog = string_copy(prog);
472 inst->type = string_copy(type);
473 inst->device = string_copy(device);
474 inst->base_device = base_device(device);
475 inst->start_time = time(0);
479 * Find the end of the list, so we add the instance on at the end.
481 for (p = instance_list; p && p->next; p = p->next);
486 instance_list = inst;
492 * Send a signal to all outstanding fsck child processes
494 static int kill_all(int signum)
496 struct fsck_instance *inst;
499 for (inst = instance_list; inst; inst = inst->next) {
500 if (inst->flags & FLAG_DONE)
502 kill(inst->pid, signum);
509 * Wait for one child process to exit; when it does, unlink it from
510 * the list of executing child processes, and return it.
512 static struct fsck_instance *wait_one(int flags)
516 struct fsck_instance *inst, *inst2, *prev;
523 inst = instance_list;
526 while (inst->next && (random() & 1)) {
531 inst->exit_status = 0;
536 * gcc -Wall fails saving throw against stupidity
537 * (inst and prev are thought to be uninitialized variables)
542 pid = waitpid(-1, &status, flags);
543 if (cancel_requested && !kill_sent) {
547 if ((pid == 0) && (flags & WNOHANG))
550 if ((errno == EINTR) || (errno == EAGAIN))
552 if (errno == ECHILD) {
554 _("%s: wait: No more child process?!?\n"),
561 for (prev = 0, inst = instance_list;
563 prev = inst, inst = inst->next) {
564 if (inst->pid == pid)
569 if (WIFEXITED(status))
570 status = WEXITSTATUS(status);
571 else if (WIFSIGNALED(status)) {
572 sig = WTERMSIG(status);
574 status = EXIT_UNCORRECTED;
576 printf(_("Warning... %s for device %s exited "
577 "with signal %d.\n"),
578 inst->prog, inst->device, sig);
582 printf(_("%s %s: status is %x, should never happen.\n"),
583 inst->prog, inst->device, status);
586 inst->exit_status = status;
587 if (progress && (inst->flags & FLAG_PROGRESS) &&
588 !progress_active()) {
589 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
590 if (inst2->flags & FLAG_DONE)
592 if (strcmp(inst2->type, "ext2") &&
593 strcmp(inst2->type, "ext3"))
596 * If we've just started the fsck, wait a tiny
597 * bit before sending the kill, to give it
598 * time to set up the signal handler
600 if (inst2->start_time < time(0)+2) {
603 kill(inst2->pid, SIGUSR1);
607 kill(inst2->pid, SIGUSR1);
608 inst2->flags |= FLAG_PROGRESS;
614 prev->next = inst->next;
616 instance_list = inst->next;
618 printf(_("Finished with %s (exit status %d)\n"),
619 inst->device, inst->exit_status);
625 * Wait until all executing child processes have exited; return the
626 * logical OR of all of their exit code values.
628 static int wait_all(int flags)
630 struct fsck_instance *inst;
631 int global_status = 0;
633 while ((inst = wait_one(flags))) {
634 global_status |= inst->exit_status;
637 if (noexecute && (flags & WNOHANG) && !(random() % 3))
641 return global_status;
645 * Run the fsck program on a particular device
647 * If the type is specified using -t, and it isn't prefixed with "no"
648 * (as in "noext2") and only one filesystem type is specified, then
649 * use that type regardless of what is specified in /etc/fstab.
651 * If the type isn't specified by the user, then use either the type
652 * specified in /etc/fstab, or DEFAULT_FSTYPE.
654 static void fsck_device(struct fs_info *fs, int interactive)
656 const char *type = 0;
659 if (fstype && strncmp(fstype, "no", 2) &&
660 strncmp(fstype, "opts=", 5) && strncmp(fstype, "loop", 4) &&
661 !strchr(fstype, ','))
664 type = fs->type ? fs->type : DEFAULT_FSTYPE;
667 retval = execute(type, fs->device, fs->mountpt, interactive);
669 fprintf(stderr, _("%s: Error %d while executing fsck.%s "
670 "for %s\n"), progname, retval, type, fs->device);
677 * Deal with the fsck -t argument.
679 struct fs_type_compile {
685 #define FS_TYPE_NORMAL 0
686 #define FS_TYPE_OPT 1
687 #define FS_TYPE_NEGOPT 2
689 static const char *fs_type_syntax_error =
690 N_("Either all or none of the filesystem types passed to -t must be prefixed\n"
691 "with 'no' or '!'.\n");
693 static void compile_fs_type(char *fs_type, struct fs_type_compile *cmp)
697 int negate, first_negate = 1;
700 for (cp=fs_type; *cp; cp++) {
706 cmp->list = malloc(num * sizeof(char *));
707 cmp->type = malloc(num * sizeof(int));
708 if (!cmp->list || !cmp->type) {
709 fprintf(stderr, _("Couldn't allocate memory for "
710 "filesystem types\n"));
713 memset(cmp->list, 0, num * sizeof(char *));
714 memset(cmp->type, 0, num * sizeof(int));
720 list = string_copy(fs_type);
722 s = strtok(list, ",");
725 if (strncmp(s, "no", 2) == 0) {
728 } else if (*s == '!') {
732 if (strcmp(s, "loop") == 0)
733 /* loop is really short-hand for opts=loop */
734 goto loop_special_case;
735 else if (strncmp(s, "opts=", 5) == 0) {
738 cmp->type[num] = negate ? FS_TYPE_NEGOPT : FS_TYPE_OPT;
741 cmp->negate = negate;
744 if ((negate && !cmp->negate) ||
745 (!negate && cmp->negate)) {
746 fprintf(stderr, _(fs_type_syntax_error));
751 printf("Adding %s to list (type %d).\n", s, cmp->type[num]);
753 cmp->list[num++] = string_copy(s);
754 s = strtok(NULL, ",");
760 * This function returns true if a particular option appears in a
761 * comma-delimited options list
763 static int opt_in_list(char *opt, char *optlist)
769 list = string_copy(optlist);
771 s = strtok(list, ",");
773 if (strcmp(s, opt) == 0) {
777 s = strtok(NULL, ",");
783 /* See if the filesystem matches the criteria given by the -t option */
784 static int fs_match(struct fs_info *fs, struct fs_type_compile *cmp)
786 int n, ret = 0, checked_type = 0;
789 if (cmp->list == 0 || cmp->list[0] == 0)
792 for (n=0; (cp = cmp->list[n]); n++) {
793 switch (cmp->type[n]) {
796 if (strcmp(cp, fs->type) == 0) {
801 if (opt_in_list(cp, fs->opts))
805 if (!opt_in_list(cp, fs->opts))
810 if (checked_type == 0)
812 return (cmp->negate ? !ret : ret);
815 /* Check if we should ignore this filesystem. */
816 static int ignore(struct fs_info *fs)
822 * If the pass number is 0, ignore it.
828 * If a specific fstype is specified, and it doesn't match,
831 if (!fs_match(fs, &fs_type_compiled)) return 1;
833 /* Are we ignoring this type? */
834 for(ip = ignored_types; *ip; ip++)
835 if (strcmp(fs->type, *ip) == 0) return 1;
837 /* Do we really really want to check this fs? */
838 for(ip = really_wanted; *ip; ip++)
839 if (strcmp(fs->type, *ip) == 0) {
844 /* See if the <fsck.fs> program is available. */
845 if (find_fsck(fs->type) == NULL) {
847 fprintf(stderr, _("fsck: cannot check %s: fsck.%s not found\n"),
848 fs->device, fs->type);
852 /* We can and want to check this file system type. */
857 * Returns TRUE if a partition on the same disk is already being
860 static int device_already_active(char *device)
862 struct fsck_instance *inst;
865 if (force_all_parallel)
869 /* Don't check a soft raid disk with any other disk */
871 (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1) ||
872 !strncmp(device, BASE_MD, sizeof(BASE_MD)-1)))
876 base = base_device(device);
878 * If we don't know the base device, assume that the device is
879 * already active if there are any fsck instances running.
882 return (instance_list != 0);
883 for (inst = instance_list; inst; inst = inst->next) {
884 if (!inst->base_device || !strcmp(base, inst->base_device)) {
893 /* Check all file systems, using the /etc/fstab table. */
894 static int check_all(NOARGS)
896 struct fs_info *fs = NULL;
897 int status = EXIT_OK;
898 int not_done_yet = 1;
903 printf(_("Checking all file systems.\n"));
906 * Do an initial scan over the filesystem; mark filesystems
907 * which should be ignored as done, and resolve LABEL= and
908 * UUID= specifications to the real device.
910 for (fs = filesys_info; fs; fs = fs->next) {
912 fs->flags |= FLAG_DONE;
916 * Find and check the root filesystem.
918 if (!parallel_root) {
919 for (fs = filesys_info; fs; fs = fs->next) {
920 if (!strcmp(fs->mountpt, "/"))
924 if (!skip_root && !ignore(fs)) {
926 status |= wait_all(0);
927 if (status > EXIT_NONDESTRUCT)
930 fs->flags |= FLAG_DONE;
934 * This is for the bone-headed user who enters the root
935 * filesystem twice. Skip root will skep all root entries.
938 for (fs = filesys_info; fs; fs = fs->next)
939 if (!strcmp(fs->mountpt, "/"))
940 fs->flags |= FLAG_DONE;
942 while (not_done_yet) {
946 for (fs = filesys_info; fs; fs = fs->next) {
947 if (cancel_requested)
949 if (fs->flags & FLAG_DONE)
952 * If the filesystem's pass number is higher
953 * than the current pass number, then we don't
956 if (fs->passno > passno) {
961 * If a filesystem on a particular device has
962 * already been spawned, then we need to defer
963 * this to another pass.
965 if (device_already_active(fs->device)) {
970 * Spawn off the fsck process
972 fsck_device(fs, serialize);
973 fs->flags |= FLAG_DONE;
976 * Only do one filesystem at a time, or if we
977 * have a limit on the number of fsck's extant
978 * at one time, apply that limit.
981 (max_running && (num_running >= max_running))) {
986 if (cancel_requested)
989 printf(_("--waiting-- (pass %d)\n"), passno);
990 status |= wait_all(pass_done ? 0 : WNOHANG);
993 printf("----------------------------------\n");
998 if (cancel_requested && !kill_sent) {
1002 status |= wait_all(0);
1006 static void usage(NOARGS)
1009 _("Usage: fsck [-ACNPRTV] [-t fstype] [fs-options] [filesys ...]\n"));
1013 #ifdef HAVE_SIGNAL_H
1014 static void signal_cancel(int sig)
1020 static void PRS(int argc, char *argv[])
1023 char *arg, *dev, *tmp = 0;
1026 int opts_for_fsck = 0;
1027 #ifdef HAVE_SIGNAL_H
1028 struct sigaction sa;
1031 * Set up signal action
1033 memset(&sa, 0, sizeof(struct sigaction));
1034 sa.sa_handler = signal_cancel;
1035 sigaction(SIGINT, &sa, 0);
1036 sigaction(SIGTERM, &sa, 0);
1045 for (i=1; i < argc; i++) {
1049 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
1050 if (num_devices >= MAX_DEVICES) {
1051 fprintf(stderr, _("%s: too many devices\n"),
1055 dev = blkid_get_devname(cache, arg, NULL);
1056 if (!dev && strchr(arg, '=')) {
1058 * Check to see if we failed because
1059 * /proc/partitions isn't found.
1061 if (access("/proc/partitions", R_OK) < 0) {
1062 fprintf(stderr, "Couldn't open /proc/partitions: %s\n",
1064 fprintf(stderr, "Is /proc mounted?\n");
1068 * Check to see if this is because
1069 * we're not running as root
1073 "Must be root to scan for matching filesystems: %s\n", arg);
1076 "Couldn't find matching filesystem: %s\n", arg);
1079 devices[num_devices++] = dev ? dev : string_copy(arg);
1082 if (arg[0] != '-' || opts_for_fsck) {
1083 if (num_args >= MAX_ARGS) {
1084 fprintf(stderr, _("%s: too many arguments\n"),
1088 args[num_args++] = string_copy(arg);
1091 for (j=1; arg[j]; j++) {
1092 if (opts_for_fsck) {
1093 options[++opt] = arg[j];
1129 else if ((i+1) < argc)
1133 fstype = string_copy(tmp);
1134 compile_fs_type(fstype, &fs_type_compiled);
1143 options[++opt] = arg[j];
1150 options[++opt] = '\0';
1151 if (num_args >= MAX_ARGS) {
1153 _("%s: too many arguments\n"),
1157 args[num_args++] = string_copy(options);
1161 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1162 force_all_parallel++;
1163 if ((tmp = getenv("FSCK_MAX_INST")))
1164 max_running = atoi(tmp);
1167 int main(int argc, char *argv[])
1170 int interactive = 0;
1171 char *type, *oldpath = getenv("PATH");
1176 setlocale(LC_MESSAGES, "");
1177 setlocale(LC_CTYPE, "");
1178 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1179 textdomain(NLS_CAT_NAME);
1181 blkid_get_cache(&cache, NULL);
1185 printf("fsck %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
1187 fstab = getenv("FSTAB_FILE");
1189 fstab = _PATH_MNTTAB;
1190 load_fs_info(fstab);
1192 /* Update our search path to include uncommon directories. */
1194 fsck_path = malloc (strlen (fsck_prefix_path) + 1 +
1195 strlen (oldpath) + 1);
1196 strcpy (fsck_path, fsck_prefix_path);
1197 strcat (fsck_path, ":");
1198 strcat (fsck_path, oldpath);
1200 fsck_path = string_copy(fsck_prefix_path);
1203 if ((num_devices == 1) || (serialize))
1206 /* If -A was specified ("check all"), do that! */
1210 if (num_devices == 0) {
1215 for (i = 0 ; i < num_devices; i++) {
1216 if (cancel_requested) {
1223 fs = lookup(devices[i]);
1225 type = blkid_get_tag_value(cache, "TYPE", devices[i]);
1227 fprintf(stderr, _("Could not determine "
1228 "filesystem type for %s\n"),
1232 fs = create_fs_device(devices[i], 0, type, 0, -1, -1);
1237 fsck_device(fs, interactive);
1239 (max_running && (num_running >= max_running))) {
1240 struct fsck_instance *inst;
1244 status |= inst->exit_status;
1245 free_instance(inst);
1248 printf("----------------------------------\n");
1251 status |= wait_all(0);
1253 blkid_put_cache(cache);