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 [-AVRNTM] [-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 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>
53 #include "../version.h"
56 static const char *ignored_types[] = {
66 static const char *really_wanted[] = {
73 #ifdef DEV_DSK_DEVICES
74 static const char *base_devices[] = {
93 static const char *base_devices[] = {
114 * Global variables for options
116 char *devices[MAX_DEVICES];
117 char *args[MAX_ARGS];
118 int num_devices, num_args;
127 int parallel_root = 0;
130 struct fs_info *filesys_info;
131 struct fsck_instance *instance_list;
132 const char *fsck_prefix_path = "/sbin:/sbin/fs.d:/sbin/fs:/etc/fs:/etc";
134 static int ignore(struct fs_info *);
138 extern char *strdup(const char *s);
141 static char *strdup(const char *s)
145 ret = malloc(strlen(s)+1);
152 static void free_instance(struct fsck_instance *i)
163 * Load the filesystem database from /etc/fstab
165 static void load_fs_info(NOARGS)
171 struct fs_info *fs_last = NULL;
176 /* Open the mount table. */
177 if ((mntfile = setmntent(MNTTAB, "r")) == NULL) {
182 while ((mp = getmntent(mntfile)) != NULL) {
183 fs = malloc(sizeof(struct fs_info));
184 memset(fs, 0, sizeof(struct fs_info));
185 fs->device = strdup(mp->mnt_fsname);
186 fs->mountpt = strdup(mp->mnt_dir);
187 fs->type = strdup(mp->mnt_type);
188 fs->opts = strdup(mp->mnt_opts);
189 fs->freq = mp->mnt_freq;
190 fs->passno = mp->mnt_passno;
201 (void) endmntent(mntfile);
204 fprintf(stderr, "\007\007\007"
205 "WARNING: Your /etc/fstab does not contain the fsck passno\n");
207 " field. I will kludge around things for you, but you\n");
209 " should fix your /etc/fstab file as soon as you can.\n\n");
211 for (fs = filesys_info; fs; fs = fs->next) {
217 #endif /* HAVE_MNTENT_H */
220 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
221 static struct fs_info *lookup(char *filesys)
225 /* No filesys name given. */
229 for (fs = filesys_info; fs; fs = fs->next) {
230 if (!strcmp(filesys, fs->device) ||
231 !strcmp(filesys, fs->mountpt))
238 /* Find fsck program for a given fs type. */
239 static char *find_fsck(char *type)
243 static char prog[256];
244 char *p = strdup(fsck_path);
247 /* Are we looking for a program or just a type? */
248 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
250 for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
251 sprintf(prog, tpl, s, type);
252 if (stat(prog, &st) == 0) break;
255 return(s ? prog : NULL);
259 * Execute a particular fsck program, and link it into the list of
260 * child processes we are waiting for.
262 static int execute(char *prog, char *device)
266 struct fsck_instance *inst;
269 argv[0] = strdup(prog);
272 for (i=0; i <num_args; i++)
273 argv[argc++] = strdup(args[i]);
275 argv[argc++] = strdup(device);
280 fprintf(stderr, "fsck: %s: not found\n", prog);
284 if (verbose || noexecute) {
286 for (i=0; i < argc; i++)
287 printf("%s ", argv[i]);
293 /* Fork and execute the correct program. */
294 if ((pid = fork()) < 0) {
297 } else if (pid == 0) {
298 (void) execv(s, argv);
302 inst = malloc(sizeof(struct fsck_instance));
305 memset(inst, 0, sizeof(struct fsck_instance));
307 inst->prog = strdup(prog);
308 inst->device = strdup(device);
309 inst->next = instance_list;
310 instance_list = inst;
316 * Wait for one child process to exit; when it does, unlink it from
317 * the list of executing child processes, and return it.
319 static struct fsck_instance *wait_one(NOARGS)
323 struct fsck_instance *inst, *prev;
332 if ((errno == EINTR) || (errno == EAGAIN))
334 if (errno == ECHILD) {
336 "%s: wait: No more child process?!?\n",
343 for (prev = 0, inst = instance_list;
345 prev = inst, inst = inst->next) {
346 if (inst->pid == pid)
350 printf("Unexpected child process %d, status = 0x%x\n",
354 if (WIFEXITED(status))
355 status = WEXITSTATUS(status);
356 else if (WIFSIGNALED(status)) {
357 sig = WTERMSIG(status);
359 status = EXIT_UNCORRECTED;
361 printf("Warning... %s for device %s exited "
363 inst->prog, inst->device, sig);
367 printf("%s %s: status is %x, should never happen.\n",
368 inst->prog, inst->device, status);
371 inst->exit_status = status;
373 prev->next = inst->next;
375 instance_list = inst->next;
380 * Wait until all executing child processes have exited; return the
381 * logical OR of all of their exit code values.
383 static int wait_all(NOARGS)
385 struct fsck_instance *inst;
386 int global_status = 0;
388 while (instance_list) {
392 global_status |= inst->exit_status;
395 return global_status;
399 * Run the fsck program on a particular device
401 * If the type is specified using -t, and it isn't prefixed with "no"
402 * (as in "noext2") and only one filesystem type is specified, then
403 * use that type regardless of what is specified in /etc/fstab.
405 * If the type isn't specified by the user, then use either the type
406 * specified in /etc/fstab, or DEFAULT_FSTYPE.
408 static void fsck_device(char *device)
410 const char *type = 0;
411 struct fs_info *fsent;
415 if (fstype && strncmp(fstype, "no", 2) && !strchr(fstype, ','))
418 if ((fsent = lookup(device))) {
419 device = fsent->device;
424 type = DEFAULT_FSTYPE;
426 sprintf(prog, "fsck.%s", type);
427 retval = execute(prog, device);
429 fprintf(stderr, "%s: Error %d while executing %s for %s\n",
430 progname, retval, prog, device);
434 /* See if filesystem type matches the list. */
435 static int fs_match(char *type, char *fs_type)
437 int ret = 0, negate = 0;
441 if (!fs_type) return(1);
443 if (strncmp(fs_type, "no", 2) == 0) {
447 strcpy(list, fs_type);
448 s = strtok(list, ",");
450 if (strcmp(s, type) == 0) {
454 s = strtok(NULL, ",");
456 return(negate ? !ret : ret);
460 /* Check if we should ignore this filesystem. */
461 static int ignore(struct fs_info *fs)
468 * If the pass number is 0, ignore it.
474 * If a specific fstype is specified, and it doesn't match,
477 if (!fs_match(fs->type, fstype)) return 1;
479 /* Noauto never matches. */
480 for (cp = strtok(fs->opts, ","); cp != NULL; cp = strtok(NULL, ",")) {
481 if (!strcmp(cp, "noauto"))
485 /* Are we ignoring this type? */
486 for(ip = ignored_types; *ip; ip++)
487 if (strcmp(fs->type, *ip) == 0) return(1);
489 /* Do we really really want to check this fs? */
490 for(ip = really_wanted; *ip; ip++)
491 if (strcmp(fs->type, *ip) == 0) {
496 /* See if the <fsck.fs> program is available. */
497 if (find_fsck(fs->type) == NULL) {
499 fprintf(stderr, "fsck: cannot check %s: fsck.%s not found\n",
500 fs->device, fs->type);
504 /* We can and want to check this file system type. */
509 * Return the "base device" given a particular device; this is used to
510 * assure that we only fsck one partition on a particular drive at any
511 * one time. Otherwise, the disk heads will be seeking all over the
514 static const char *base_device(char *device)
518 for (base = base_devices; *base; base++) {
519 if (!strncmp(*base, device, strlen(*base)))
526 * Returns TRUE if a partition on the same disk is already being
529 static int device_already_active(char *device)
531 struct fsck_instance *inst;
534 base = base_device(device);
536 for (inst = instance_list; inst; inst = inst->next) {
537 if (!strcmp(base, base_device(inst->device)))
544 /* Check all file systems, using the /etc/fstab table. */
545 static int check_all(NOARGS)
547 struct fs_info *fs = NULL;
548 struct fsck_instance *inst;
549 int status = EXIT_OK;
550 int not_done_yet = 1;
555 printf("Checking all file systems.\n");
558 * Find and check the root filesystem first.
560 if (!parallel_root) {
561 for (fs = filesys_info; fs; fs = fs->next) {
562 if (!strcmp(fs->mountpt, "/"))
565 if (fs && !skip_root && !ignore(fs)) {
566 fsck_device(fs->device);
567 fs->flags |= FLAG_DONE;
568 status |= wait_all();
569 if (status > EXIT_NONDESTRUCT)
573 if (fs) fs->flags |= FLAG_DONE;
576 * Mark filesystems that should be ignored as done.
578 for (fs = filesys_info; fs; fs = fs->next) {
580 fs->flags |= FLAG_DONE;
583 while (not_done_yet) {
587 for (fs = filesys_info; fs; fs = fs->next) {
588 if (fs->flags & FLAG_DONE)
591 * If the filesystem's pass number is higher
592 * than the current pass number, then we don't
595 if (fs->passno > passno) {
600 * If a filesystem on a particular device has
601 * already been spawned, then we need to defer
602 * this to another pass.
604 if (device_already_active(fs->device)) {
609 * Spawn off the fsck process
611 fsck_device(fs->device);
612 fs->flags |= FLAG_DONE;
616 break; /* Only do one filesystem at a time */
622 status |= inst->exit_status;
626 status |= wait_all();
628 printf("----------------------------------\n");
633 status |= wait_all();
637 static void usage(NOARGS)
640 "Usage: fsck [-AV] [-t fstype] [fs-options] filesys\n");
644 static void PRS(int argc, char *argv[])
650 int opts_for_fsck = 0;
660 for (i=1; i < argc; i++) {
665 if (num_devices >= MAX_DEVICES) {
666 fprintf(stderr, "%s: too many devices\n",
670 devices[num_devices++] = strdup(arg);
674 if (num_args >= MAX_ARGS) {
675 fprintf(stderr, "%s: too many arguments\n",
679 args[num_args++] = strdup(arg);
682 for (j=1; arg[j]; j++) {
684 options[++opt] = arg[j];
714 fstype = strdup(arg+j+1);
719 fstype = strdup(argv[i]);
728 options[++opt] = arg[j];
735 options[++opt] = '\0';
736 if (num_args >= MAX_ARGS) {
738 "%s: too many arguments\n",
742 args[num_args++] = strdup(options);
748 int main(int argc, char *argv[])
752 char *oldpath = getenv("PATH");
757 printf("Parallelizing fsck version %s (%s)\n",
758 E2FSPROGS_VERSION, E2FSPROGS_DATE);
760 /* Update our search path to include uncommon directories. */
762 fsck_path = malloc (strlen (fsck_prefix_path) + 1 +
763 strlen (oldpath) + 1);
764 strcpy (fsck_path, fsck_prefix_path);
765 strcat (fsck_path, ":");
766 strcat (fsck_path, oldpath);
768 fsck_path = strdup(fsck_prefix_path);
771 /* If -A was specified ("check all"), do that! */
775 for (i = 0 ; i < num_devices; i++) {
776 fsck_device(devices[i]);
778 struct fsck_instance *inst;
782 status |= inst->exit_status;
787 status |= wait_all();