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 *);
136 static char *string_copy(const char *s)
140 ret = malloc(strlen(s)+1);
146 static void free_instance(struct fsck_instance *i)
157 * Load the filesystem database from /etc/fstab
159 static void load_fs_info(NOARGS)
165 struct fs_info *fs_last = NULL;
170 /* Open the mount table. */
171 if ((mntfile = setmntent(MNTTAB, "r")) == NULL) {
176 while ((mp = getmntent(mntfile)) != NULL) {
177 fs = malloc(sizeof(struct fs_info));
178 memset(fs, 0, sizeof(struct fs_info));
179 fs->device = string_copy(mp->mnt_fsname);
180 fs->mountpt = string_copy(mp->mnt_dir);
181 fs->type = string_copy(mp->mnt_type);
182 fs->opts = string_copy(mp->mnt_opts);
183 fs->freq = mp->mnt_freq;
184 fs->passno = mp->mnt_passno;
195 (void) endmntent(mntfile);
198 fprintf(stderr, "\007\007\007"
199 "WARNING: Your /etc/fstab does not contain the fsck passno\n");
201 " field. I will kludge around things for you, but you\n");
203 " should fix your /etc/fstab file as soon as you can.\n\n");
205 for (fs = filesys_info; fs; fs = fs->next) {
211 #endif /* HAVE_MNTENT_H */
214 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
215 static struct fs_info *lookup(char *filesys)
219 /* No filesys name given. */
223 for (fs = filesys_info; fs; fs = fs->next) {
224 if (!strcmp(filesys, fs->device) ||
225 !strcmp(filesys, fs->mountpt))
232 /* Find fsck program for a given fs type. */
233 static char *find_fsck(char *type)
237 static char prog[256];
238 char *p = string_copy(fsck_path);
241 /* Are we looking for a program or just a type? */
242 tpl = (strncmp(type, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
244 for(s = strtok(p, ":"); s; s = strtok(NULL, ":")) {
245 sprintf(prog, tpl, s, type);
246 if (stat(prog, &st) == 0) break;
249 return(s ? prog : NULL);
253 * Execute a particular fsck program, and link it into the list of
254 * child processes we are waiting for.
256 static int execute(char *prog, char *device)
260 struct fsck_instance *inst;
263 argv[0] = string_copy(prog);
266 for (i=0; i <num_args; i++)
267 argv[argc++] = string_copy(args[i]);
269 argv[argc++] = string_copy(device);
274 fprintf(stderr, "fsck: %s: not found\n", prog);
278 if (verbose || noexecute) {
280 for (i=0; i < argc; i++)
281 printf("%s ", argv[i]);
287 /* Fork and execute the correct program. */
288 if ((pid = fork()) < 0) {
291 } else if (pid == 0) {
292 (void) execv(s, argv);
296 inst = malloc(sizeof(struct fsck_instance));
299 memset(inst, 0, sizeof(struct fsck_instance));
301 inst->prog = string_copy(prog);
302 inst->device = string_copy(device);
303 inst->next = instance_list;
304 instance_list = inst;
310 * Wait for one child process to exit; when it does, unlink it from
311 * the list of executing child processes, and return it.
313 static struct fsck_instance *wait_one(NOARGS)
317 struct fsck_instance *inst, *prev;
326 if ((errno == EINTR) || (errno == EAGAIN))
328 if (errno == ECHILD) {
330 "%s: wait: No more child process?!?\n",
337 for (prev = 0, inst = instance_list;
339 prev = inst, inst = inst->next) {
340 if (inst->pid == pid)
344 printf("Unexpected child process %d, status = 0x%x\n",
348 if (WIFEXITED(status))
349 status = WEXITSTATUS(status);
350 else if (WIFSIGNALED(status)) {
351 sig = WTERMSIG(status);
353 status = EXIT_UNCORRECTED;
355 printf("Warning... %s for device %s exited "
357 inst->prog, inst->device, sig);
361 printf("%s %s: status is %x, should never happen.\n",
362 inst->prog, inst->device, status);
365 inst->exit_status = status;
367 prev->next = inst->next;
369 instance_list = inst->next;
374 * Wait until all executing child processes have exited; return the
375 * logical OR of all of their exit code values.
377 static int wait_all(NOARGS)
379 struct fsck_instance *inst;
380 int global_status = 0;
382 while (instance_list) {
386 global_status |= inst->exit_status;
389 return global_status;
393 * Run the fsck program on a particular device
395 * If the type is specified using -t, and it isn't prefixed with "no"
396 * (as in "noext2") and only one filesystem type is specified, then
397 * use that type regardless of what is specified in /etc/fstab.
399 * If the type isn't specified by the user, then use either the type
400 * specified in /etc/fstab, or DEFAULT_FSTYPE.
402 static void fsck_device(char *device)
404 const char *type = 0;
405 struct fs_info *fsent;
409 if (fstype && strncmp(fstype, "no", 2) && !strchr(fstype, ','))
412 if ((fsent = lookup(device))) {
413 device = fsent->device;
418 type = DEFAULT_FSTYPE;
420 sprintf(prog, "fsck.%s", type);
421 retval = execute(prog, device);
423 fprintf(stderr, "%s: Error %d while executing %s for %s\n",
424 progname, retval, prog, device);
428 /* See if filesystem type matches the list. */
429 static int fs_match(char *type, char *fs_type)
431 int ret = 0, negate = 0;
435 if (!fs_type) return(1);
437 if (strncmp(fs_type, "no", 2) == 0) {
441 strcpy(list, fs_type);
442 s = strtok(list, ",");
444 if (strcmp(s, type) == 0) {
448 s = strtok(NULL, ",");
450 return(negate ? !ret : ret);
454 /* Check if we should ignore this filesystem. */
455 static int ignore(struct fs_info *fs)
461 * If the pass number is 0, ignore it.
467 * If a specific fstype is specified, and it doesn't match,
470 if (!fs_match(fs->type, fstype)) return 1;
472 /* Are we ignoring this type? */
473 for(ip = ignored_types; *ip; ip++)
474 if (strcmp(fs->type, *ip) == 0) return(1);
476 /* Do we really really want to check this fs? */
477 for(ip = really_wanted; *ip; ip++)
478 if (strcmp(fs->type, *ip) == 0) {
483 /* See if the <fsck.fs> program is available. */
484 if (find_fsck(fs->type) == NULL) {
486 fprintf(stderr, "fsck: cannot check %s: fsck.%s not found\n",
487 fs->device, fs->type);
491 /* We can and want to check this file system type. */
496 * Return the "base device" given a particular device; this is used to
497 * assure that we only fsck one partition on a particular drive at any
498 * one time. Otherwise, the disk heads will be seeking all over the
501 static const char *base_device(char *device)
505 for (base = base_devices; *base; base++) {
506 if (!strncmp(*base, device, strlen(*base)))
513 * Returns TRUE if a partition on the same disk is already being
516 static int device_already_active(char *device)
518 struct fsck_instance *inst;
521 base = base_device(device);
523 for (inst = instance_list; inst; inst = inst->next) {
524 if (!strcmp(base, base_device(inst->device)))
531 /* Check all file systems, using the /etc/fstab table. */
532 static int check_all(NOARGS)
534 struct fs_info *fs = NULL;
535 struct fsck_instance *inst;
536 int status = EXIT_OK;
537 int not_done_yet = 1;
542 printf("Checking all file systems.\n");
545 * Find and check the root filesystem first.
547 if (!parallel_root) {
548 for (fs = filesys_info; fs; fs = fs->next) {
549 if (!strcmp(fs->mountpt, "/"))
552 if (fs && !skip_root && !ignore(fs)) {
553 fsck_device(fs->device);
554 fs->flags |= FLAG_DONE;
555 status |= wait_all();
556 if (status > EXIT_NONDESTRUCT)
560 if (fs) fs->flags |= FLAG_DONE;
563 * Mark filesystems that should be ignored as done.
565 for (fs = filesys_info; fs; fs = fs->next) {
567 fs->flags |= FLAG_DONE;
570 while (not_done_yet) {
574 for (fs = filesys_info; fs; fs = fs->next) {
575 if (fs->flags & FLAG_DONE)
578 * If the filesystem's pass number is higher
579 * than the current pass number, then we don't
582 if (fs->passno > passno) {
587 * If a filesystem on a particular device has
588 * already been spawned, then we need to defer
589 * this to another pass.
591 if (device_already_active(fs->device)) {
596 * Spawn off the fsck process
598 fsck_device(fs->device);
599 fs->flags |= FLAG_DONE;
603 break; /* Only do one filesystem at a time */
609 status |= inst->exit_status;
613 status |= wait_all();
615 printf("----------------------------------\n");
620 status |= wait_all();
624 static void usage(NOARGS)
627 "Usage: fsck [-AV] [-t fstype] [fs-options] filesys\n");
631 static void PRS(int argc, char *argv[])
637 int opts_for_fsck = 0;
647 for (i=1; i < argc; i++) {
652 if (num_devices >= MAX_DEVICES) {
653 fprintf(stderr, "%s: too many devices\n",
657 devices[num_devices++] = string_copy(arg);
661 if (num_args >= MAX_ARGS) {
662 fprintf(stderr, "%s: too many arguments\n",
666 args[num_args++] = string_copy(arg);
669 for (j=1; arg[j]; j++) {
671 options[++opt] = arg[j];
701 fstype = string_copy(arg+j+1);
706 fstype = string_copy(argv[i]);
715 options[++opt] = arg[j];
722 options[++opt] = '\0';
723 if (num_args >= MAX_ARGS) {
725 "%s: too many arguments\n",
729 args[num_args++] = string_copy(options);
735 int main(int argc, char *argv[])
739 char *oldpath = getenv("PATH");
744 printf("Parallelizing fsck version %s (%s)\n",
745 E2FSPROGS_VERSION, E2FSPROGS_DATE);
747 /* Update our search path to include uncommon directories. */
749 fsck_path = malloc (strlen (fsck_prefix_path) + 1 +
750 strlen (oldpath) + 1);
751 strcpy (fsck_path, fsck_prefix_path);
752 strcat (fsck_path, ":");
753 strcat (fsck_path, oldpath);
755 fsck_path = string_copy(fsck_prefix_path);
758 /* If -A was specified ("check all"), do that! */
762 for (i = 0 ; i < num_devices; i++) {
763 fsck_device(devices[i]);
765 struct fsck_instance *inst;
769 status |= inst->exit_status;
774 status |= wait_all();