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 #define _XOPEN_SOURCE 600 /* for inclusion of sa_handler in Solaris */
30 #include <sys/types.h>
32 #include <sys/signal.h>
61 #include "../version.h"
62 #include "nls-enable.h"
64 #include "blkid/blkid.h"
67 #define _PATH_MNTTAB "/etc/fstab"
70 static const char *ignored_types[] = {
82 static const char *really_wanted[] = {
95 #define BASE_MD "/dev/md"
98 * Global variables for options
100 char *devices[MAX_DEVICES];
101 char *args[MAX_ARGS];
102 int num_devices, num_args;
109 int ignore_mounted = 0;
111 int parallel_root = 0;
114 int force_all_parallel = 0;
117 volatile int cancel_requested = 0;
121 struct fs_info *filesys_info = NULL, *filesys_last = NULL;
122 struct fsck_instance *instance_list;
123 const char *fsck_prefix_path = "/sbin:/sbin/fs.d:/sbin/fs:/etc/fs:/etc";
125 blkid_cache cache = NULL;
127 static char *string_copy(const char *s)
133 ret = malloc(strlen(s)+1);
139 static int string_to_int(const char *s)
144 l = strtol(s, &p, 0);
145 if (*p || l == LONG_MIN || l == LONG_MAX || l < 0 || l > INT_MAX)
151 static int ignore(struct fs_info *);
153 static char *skip_over_blank(char *cp)
155 while (*cp && isspace(*cp))
160 static char *skip_over_word(char *cp)
162 while (*cp && !isspace(*cp))
167 static void strip_line(char *line)
172 p = line + strlen(line) - 1;
173 if ((*p == '\n') || (*p == '\r'))
180 static char *parse_word(char **buf)
188 word = skip_over_blank(word);
189 next = skip_over_word(word);
196 static void parse_escape(char *word)
204 for (p = word, q = word; *p; p++, q++) {
223 for (i = 0; i < 3; i++, p++) {
226 ac = (ac * 8) + (*p - '0');
234 static void free_instance(struct fsck_instance *i)
238 free(i->base_device);
243 static struct fs_info *create_fs_device(const char *device, const char *mntpnt,
244 const char *type, const char *opts,
245 int freq, int passno)
249 if (!(fs = malloc(sizeof(struct fs_info))))
252 fs->device = string_copy(device);
253 fs->mountpt = string_copy(mntpnt);
254 fs->type = string_copy(type);
255 fs->opts = string_copy(opts ? opts : "");
264 filesys_last->next = fs;
272 static int parse_fstab_line(char *line, struct fs_info **ret_fs)
274 char *dev, *device, *mntpnt, *type, *opts, *freq, *passno, *cp;
281 device = parse_word(&cp);
282 if (!device || *device == '#')
283 return 0; /* Ignore blank lines and comments */
284 mntpnt = parse_word(&cp);
285 type = parse_word(&cp);
286 opts = parse_word(&cp);
287 freq = parse_word(&cp);
288 passno = parse_word(&cp);
290 if (!mntpnt || !type)
293 parse_escape(device);
294 parse_escape(mntpnt);
298 parse_escape(passno);
300 dev = blkid_get_devname(cache, device, NULL);
304 if (strchr(type, ','))
307 fs = create_fs_device(device, mntpnt, type ? type : "auto", opts,
308 freq ? atoi(freq) : -1,
309 passno ? atoi(passno) : -1);
318 static void interpret_type(struct fs_info *fs)
322 if (strcmp(fs->type, "auto") != 0)
324 t = blkid_get_tag_value(cache, "TYPE", fs->device);
332 * Load the filesystem database from /etc/fstab
334 static void load_fs_info(const char *filename)
342 if ((f = fopen(filename, "r")) == NULL) {
343 fprintf(stderr, _("WARNING: couldn't open %s: %s\n"),
344 filename, strerror(errno));
349 if (!fgets(buf, sizeof(buf), f))
351 buf[sizeof(buf)-1] = 0;
352 if (parse_fstab_line(buf, &fs) < 0) {
353 fprintf(stderr, _("WARNING: bad format "
354 "on line %d of %s\n"), lineno, filename);
367 if (old_fstab && filesys_info) {
368 fputs(_("\007\007\007"
369 "WARNING: Your /etc/fstab does not contain the fsck passno\n"
370 " field. I will kludge around things for you, but you\n"
371 " should fix your /etc/fstab file as soon as you can.\n\n"), stderr);
373 for (fs = filesys_info; fs; fs = fs->next) {
379 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
380 static struct fs_info *lookup(char *filesys)
384 /* No filesys name given. */
388 for (fs = filesys_info; fs; fs = fs->next) {
389 if (!strcmp(filesys, fs->device) ||
390 (fs->mountpt && !strcmp(filesys, fs->mountpt)))
397 /* Find fsck program for a given fs type. */
398 static char *find_fsck(char *type)
402 static char prog[256];
403 char *p = string_copy(fsck_path);
406 /* Are we looking for a program or just a type? */
407 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
409 for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
410 sprintf(prog, tpl, s, type);
411 if (stat(prog, &st) == 0) break;
414 return(s ? prog : NULL);
417 static int progress_active(NOARGS)
419 struct fsck_instance *inst;
421 for (inst = instance_list; inst; inst = inst->next) {
422 if (inst->flags & FLAG_DONE)
424 if (inst->flags & FLAG_PROGRESS)
431 * Execute a particular fsck program, and link it into the list of
432 * child processes we are waiting for.
434 static int execute(const char *type, const char *device, const char *mntpt,
437 char *s, *argv[80], prog[80];
439 struct fsck_instance *inst, *p;
442 inst = malloc(sizeof(struct fsck_instance));
445 memset(inst, 0, sizeof(struct fsck_instance));
447 sprintf(prog, "fsck.%s", type);
448 argv[0] = string_copy(prog);
451 for (i=0; i <num_args; i++)
452 argv[argc++] = string_copy(args[i]);
455 if ((strcmp(type, "ext2") == 0) ||
456 (strcmp(type, "ext3") == 0) ||
457 (strcmp(type, "ext4") == 0) ||
458 (strcmp(type, "ext4dev") == 0)) {
462 if (!progress_active()) {
463 snprintf(tmp, 80, "-C%d", progress_fd);
464 inst->flags |= FLAG_PROGRESS;
465 } else if (progress_fd)
466 snprintf(tmp, 80, "-C%d", progress_fd * -1);
468 argv[argc++] = string_copy(tmp);
472 argv[argc++] = string_copy(device);
477 fprintf(stderr, _("fsck: %s: not found\n"), prog);
482 if (verbose || noexecute) {
483 printf("[%s (%d) -- %s] ", s, num_running,
484 mntpt ? mntpt : device);
485 for (i=0; i < argc; i++)
486 printf("%s ", argv[i]);
490 /* Fork and execute the correct program. */
493 else if ((pid = fork()) < 0) {
497 } else if (pid == 0) {
500 (void) execv(s, argv);
506 for (i=0; i < argc; i++)
510 inst->prog = string_copy(prog);
511 inst->type = string_copy(type);
512 inst->device = string_copy(device);
513 inst->base_device = base_device(device);
514 inst->start_time = time(0);
518 * Find the end of the list, so we add the instance on at the end.
520 for (p = instance_list; p && p->next; p = p->next);
525 instance_list = inst;
531 * Send a signal to all outstanding fsck child processes
533 static int kill_all(int signum)
535 struct fsck_instance *inst;
538 for (inst = instance_list; inst; inst = inst->next) {
539 if (inst->flags & FLAG_DONE)
541 kill(inst->pid, signum);
548 * Wait for one child process to exit; when it does, unlink it from
549 * the list of executing child processes, and return it.
551 static struct fsck_instance *wait_one(int flags)
555 struct fsck_instance *inst, *inst2, *prev;
562 inst = instance_list;
565 while (inst->next && (random() & 1)) {
570 inst->exit_status = 0;
575 * gcc -Wall fails saving throw against stupidity
576 * (inst and prev are thought to be uninitialized variables)
581 pid = waitpid(-1, &status, flags);
582 if (cancel_requested && !kill_sent) {
586 if ((pid == 0) && (flags & WNOHANG))
589 if ((errno == EINTR) || (errno == EAGAIN))
591 if (errno == ECHILD) {
593 _("%s: wait: No more child process?!?\n"),
600 for (prev = 0, inst = instance_list;
602 prev = inst, inst = inst->next) {
603 if (inst->pid == pid)
608 if (WIFEXITED(status))
609 status = WEXITSTATUS(status);
610 else if (WIFSIGNALED(status)) {
611 sig = WTERMSIG(status);
613 status = EXIT_UNCORRECTED;
615 printf(_("Warning... %s for device %s exited "
616 "with signal %d.\n"),
617 inst->prog, inst->device, sig);
621 printf(_("%s %s: status is %x, should never happen.\n"),
622 inst->prog, inst->device, status);
625 inst->exit_status = status;
626 inst->flags |= FLAG_DONE;
627 if (progress && (inst->flags & FLAG_PROGRESS) &&
628 !progress_active()) {
629 for (inst2 = instance_list; inst2; inst2 = inst2->next) {
630 if (inst2->flags & FLAG_DONE)
632 if (strcmp(inst2->type, "ext2") &&
633 strcmp(inst2->type, "ext3") &&
634 strcmp(inst2->type, "ext4") &&
635 strcmp(inst2->type, "ext4dev"))
638 * If we've just started the fsck, wait a tiny
639 * bit before sending the kill, to give it
640 * time to set up the signal handler
642 if (inst2->start_time < time(0)+2) {
645 kill(inst2->pid, SIGUSR1);
649 kill(inst2->pid, SIGUSR1);
650 inst2->flags |= FLAG_PROGRESS;
656 prev->next = inst->next;
658 instance_list = inst->next;
660 printf(_("Finished with %s (exit status %d)\n"),
661 inst->device, inst->exit_status);
666 #define FLAG_WAIT_ALL 0
667 #define FLAG_WAIT_ATLEAST_ONE 1
669 * Wait until all executing child processes have exited; return the
670 * logical OR of all of their exit code values.
672 static int wait_many(int flags)
674 struct fsck_instance *inst;
675 int global_status = 0;
678 while ((inst = wait_one(wait_flags))) {
679 global_status |= inst->exit_status;
682 if (noexecute && (flags & WNOHANG) && !(random() % 3))
685 if (flags & FLAG_WAIT_ATLEAST_ONE)
686 wait_flags = WNOHANG;
688 return global_status;
692 * Run the fsck program on a particular device
694 * If the type is specified using -t, and it isn't prefixed with "no"
695 * (as in "noext2") and only one filesystem type is specified, then
696 * use that type regardless of what is specified in /etc/fstab.
698 * If the type isn't specified by the user, then use either the type
699 * specified in /etc/fstab, or DEFAULT_FSTYPE.
701 static void fsck_device(struct fs_info *fs, int interactive)
708 if (strcmp(fs->type, "auto") != 0)
710 else if (fstype && strncmp(fstype, "no", 2) &&
711 strncmp(fstype, "opts=", 5) && strncmp(fstype, "loop", 4) &&
712 !strchr(fstype, ','))
715 type = DEFAULT_FSTYPE;
718 retval = execute(type, fs->device, fs->mountpt, interactive);
720 fprintf(stderr, _("%s: Error %d while executing fsck.%s "
721 "for %s\n"), progname, retval, type, fs->device);
728 * Deal with the fsck -t argument.
730 struct fs_type_compile {
736 #define FS_TYPE_NORMAL 0
737 #define FS_TYPE_OPT 1
738 #define FS_TYPE_NEGOPT 2
740 static const char *fs_type_syntax_error =
741 N_("Either all or none of the filesystem types passed to -t must be prefixed\n"
742 "with 'no' or '!'.\n");
744 static void compile_fs_type(char *fs_type, struct fs_type_compile *cmp)
748 int negate, first_negate = 1;
751 for (cp=fs_type; *cp; cp++) {
757 cmp->list = malloc(num * sizeof(char *));
758 cmp->type = malloc(num * sizeof(int));
759 if (!cmp->list || !cmp->type) {
760 fputs(_("Couldn't allocate memory for filesystem types\n"),
764 memset(cmp->list, 0, num * sizeof(char *));
765 memset(cmp->type, 0, num * sizeof(int));
771 list = string_copy(fs_type);
773 s = strtok(list, ",");
776 if (strncmp(s, "no", 2) == 0) {
779 } else if (*s == '!') {
783 if (strcmp(s, "loop") == 0)
784 /* loop is really short-hand for opts=loop */
785 goto loop_special_case;
786 else if (strncmp(s, "opts=", 5) == 0) {
789 cmp->type[num] = negate ? FS_TYPE_NEGOPT : FS_TYPE_OPT;
792 cmp->negate = negate;
795 if ((negate && !cmp->negate) ||
796 (!negate && cmp->negate)) {
797 fputs(_(fs_type_syntax_error), stderr);
802 printf("Adding %s to list (type %d).\n", s, cmp->type[num]);
804 cmp->list[num++] = string_copy(s);
805 s = strtok(NULL, ",");
811 * This function returns true if a particular option appears in a
812 * comma-delimited options list
814 static int opt_in_list(const char *opt, char *optlist)
820 list = string_copy(optlist);
822 s = strtok(list, ",");
824 if (strcmp(s, opt) == 0) {
828 s = strtok(NULL, ",");
834 /* See if the filesystem matches the criteria given by the -t option */
835 static int fs_match(struct fs_info *fs, struct fs_type_compile *cmp)
837 int n, ret = 0, checked_type = 0;
840 if (cmp->list == 0 || cmp->list[0] == 0)
843 for (n=0; (cp = cmp->list[n]); n++) {
844 switch (cmp->type[n]) {
847 if (strcmp(cp, fs->type) == 0) {
852 if (opt_in_list(cp, fs->opts))
856 if (!opt_in_list(cp, fs->opts))
861 if (checked_type == 0)
863 return (cmp->negate ? !ret : ret);
866 /* Check if we should ignore this filesystem. */
867 static int ignore(struct fs_info *fs)
873 * If the pass number is 0, ignore it.
879 * If this is a bind mount, ignore it.
881 if (opt_in_list("bind", fs->opts)) {
883 _("%s: skipping bad line in /etc/fstab: bind mount with nonzero fsck pass number\n"),
891 * If a specific fstype is specified, and it doesn't match,
894 if (!fs_match(fs, &fs_type_compiled)) return 1;
896 /* Are we ignoring this type? */
897 for(ip = ignored_types; *ip; ip++)
898 if (strcmp(fs->type, *ip) == 0) return 1;
900 /* Do we really really want to check this fs? */
901 for(ip = really_wanted; *ip; ip++)
902 if (strcmp(fs->type, *ip) == 0) {
907 /* See if the <fsck.fs> program is available. */
908 if (find_fsck(fs->type) == NULL) {
910 fprintf(stderr, _("fsck: cannot check %s: fsck.%s not found\n"),
911 fs->device, fs->type);
915 /* We can and want to check this file system type. */
920 * Returns TRUE if a partition on the same disk is already being
923 static int device_already_active(char *device)
925 struct fsck_instance *inst;
928 if (force_all_parallel)
932 /* Don't check a soft raid disk with any other disk */
934 (!strncmp(instance_list->device, BASE_MD, sizeof(BASE_MD)-1) ||
935 !strncmp(device, BASE_MD, sizeof(BASE_MD)-1)))
939 base = base_device(device);
941 * If we don't know the base device, assume that the device is
942 * already active if there are any fsck instances running.
945 return (instance_list != 0);
946 for (inst = instance_list; inst; inst = inst->next) {
947 if (!inst->base_device || !strcmp(base, inst->base_device)) {
956 /* Check all file systems, using the /etc/fstab table. */
957 static int check_all(NOARGS)
959 struct fs_info *fs = NULL;
960 int status = EXIT_OK;
961 int not_done_yet = 1;
966 fputs(_("Checking all file systems.\n"), stdout);
969 * Do an initial scan over the filesystem; mark filesystems
970 * which should be ignored as done, and resolve any "auto"
971 * filesystem types (done as a side-effect of calling ignore()).
973 for (fs = filesys_info; fs; fs = fs->next) {
975 fs->flags |= FLAG_DONE;
979 * Find and check the root filesystem.
981 if (!parallel_root) {
982 for (fs = filesys_info; fs; fs = fs->next) {
983 if (!strcmp(fs->mountpt, "/"))
987 if (!skip_root && !ignore(fs) &&
988 !(ignore_mounted && is_mounted(fs->device))) {
990 status |= wait_many(FLAG_WAIT_ALL);
991 if (status > EXIT_NONDESTRUCT)
994 fs->flags |= FLAG_DONE;
998 * This is for the bone-headed user who enters the root
999 * filesystem twice. Skip root will skep all root entries.
1002 for (fs = filesys_info; fs; fs = fs->next)
1003 if (!strcmp(fs->mountpt, "/"))
1004 fs->flags |= FLAG_DONE;
1006 while (not_done_yet) {
1010 for (fs = filesys_info; fs; fs = fs->next) {
1011 if (cancel_requested)
1013 if (fs->flags & FLAG_DONE)
1016 * If the filesystem's pass number is higher
1017 * than the current pass number, then we don't
1020 if (fs->passno > passno) {
1024 if (ignore_mounted && is_mounted(fs->device)) {
1025 fs->flags |= FLAG_DONE;
1029 * If a filesystem on a particular device has
1030 * already been spawned, then we need to defer
1031 * this to another pass.
1033 if (device_already_active(fs->device)) {
1038 * Spawn off the fsck process
1040 fsck_device(fs, serialize);
1041 fs->flags |= FLAG_DONE;
1044 * Only do one filesystem at a time, or if we
1045 * have a limit on the number of fsck's extant
1046 * at one time, apply that limit.
1049 (max_running && (num_running >= max_running))) {
1054 if (cancel_requested)
1057 printf(_("--waiting-- (pass %d)\n"), passno);
1058 status |= wait_many(pass_done ? FLAG_WAIT_ALL :
1059 FLAG_WAIT_ATLEAST_ONE);
1062 printf("----------------------------------\n");
1067 if (cancel_requested && !kill_sent) {
1071 status |= wait_many(FLAG_WAIT_ATLEAST_ONE);
1075 static void usage(NOARGS)
1077 fputs(_("Usage: fsck [-AMNPRTV] [ -C [ fd ] ] [-t fstype] [fs-options] [filesys ...]\n"), stderr);
1081 #ifdef HAVE_SIGNAL_H
1082 static void signal_cancel(int sig FSCK_ATTR((unused)))
1088 static void PRS(int argc, char *argv[])
1091 char *arg, *dev, *tmp = 0;
1094 int opts_for_fsck = 0;
1095 #ifdef HAVE_SIGNAL_H
1096 struct sigaction sa;
1099 * Set up signal action
1101 memset(&sa, 0, sizeof(struct sigaction));
1102 sa.sa_handler = signal_cancel;
1103 sigaction(SIGINT, &sa, 0);
1104 sigaction(SIGTERM, &sa, 0);
1113 for (i=1; i < argc; i++) {
1117 if ((arg[0] == '/' && !opts_for_fsck) || strchr(arg, '=')) {
1118 if (num_devices >= MAX_DEVICES) {
1119 fprintf(stderr, _("%s: too many devices\n"),
1123 dev = blkid_get_devname(cache, arg, NULL);
1124 if (!dev && strchr(arg, '=')) {
1126 * Check to see if we failed because
1127 * /proc/partitions isn't found.
1129 if (access("/proc/partitions", R_OK) < 0) {
1130 fprintf(stderr, "Couldn't open /proc/partitions: %s\n",
1132 fprintf(stderr, "Is /proc mounted?\n");
1136 * Check to see if this is because
1137 * we're not running as root
1141 "Must be root to scan for matching filesystems: %s\n", arg);
1144 "Couldn't find matching filesystem: %s\n", arg);
1147 devices[num_devices++] = dev ? dev : string_copy(arg);
1150 if (arg[0] != '-' || opts_for_fsck) {
1151 if (num_args >= MAX_ARGS) {
1152 fprintf(stderr, _("%s: too many arguments\n"),
1156 args[num_args++] = string_copy(arg);
1159 for (j=1; arg[j]; j++) {
1160 if (opts_for_fsck) {
1161 options[++opt] = arg[j];
1171 progress_fd = string_to_int(arg+j+1);
1172 if (progress_fd < 0)
1176 } else if ((i+1) < argc &&
1177 !strncmp(argv[i+1], "-", 1) == 0) {
1178 progress_fd = string_to_int(argv[i]);
1179 if (progress_fd < 0)
1214 else if ((i+1) < argc)
1218 fstype = string_copy(tmp);
1219 compile_fs_type(fstype, &fs_type_compiled);
1228 options[++opt] = arg[j];
1235 options[++opt] = '\0';
1236 if (num_args >= MAX_ARGS) {
1238 _("%s: too many arguments\n"),
1242 args[num_args++] = string_copy(options);
1246 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1247 force_all_parallel++;
1248 if ((tmp = getenv("FSCK_MAX_INST")))
1249 max_running = atoi(tmp);
1252 int main(int argc, char *argv[])
1255 int interactive = 0;
1256 char *oldpath = getenv("PATH");
1260 setvbuf(stdout, NULL, _IONBF, BUFSIZ);
1261 setvbuf(stderr, NULL, _IONBF, BUFSIZ);
1264 setlocale(LC_MESSAGES, "");
1265 setlocale(LC_CTYPE, "");
1266 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
1267 textdomain(NLS_CAT_NAME);
1269 blkid_get_cache(&cache, NULL);
1273 printf("fsck %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE);
1275 fstab = getenv("FSTAB_FILE");
1277 fstab = _PATH_MNTTAB;
1278 load_fs_info(fstab);
1280 /* Update our search path to include uncommon directories. */
1282 fsck_path = malloc (strlen (fsck_prefix_path) + 1 +
1283 strlen (oldpath) + 1);
1285 fprintf(stderr, "%s: Unable to allocate memory for fsck_path\n", progname);
1288 strcpy (fsck_path, fsck_prefix_path);
1289 strcat (fsck_path, ":");
1290 strcat (fsck_path, oldpath);
1292 fsck_path = string_copy(fsck_prefix_path);
1295 if ((num_devices == 1) || (serialize))
1298 /* If -A was specified ("check all"), do that! */
1302 if (num_devices == 0) {
1307 for (i = 0 ; i < num_devices; i++) {
1308 if (cancel_requested) {
1315 fs = lookup(devices[i]);
1317 fs = create_fs_device(devices[i], 0, "auto",
1322 if (ignore_mounted && is_mounted(fs->device))
1324 fsck_device(fs, interactive);
1326 (max_running && (num_running >= max_running))) {
1327 struct fsck_instance *inst;
1331 status |= inst->exit_status;
1332 free_instance(inst);
1335 printf("----------------------------------\n");
1338 status |= wait_many(FLAG_WAIT_ALL);
1340 blkid_put_cache(cache);