]>
git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - misc/fsck.c
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 Theodore Ts'o. This file may be
22 * redistributed under the terms of the GNU Public License.
25 #include <sys/types.h>
27 #include <sys/signal.h>
52 #include "../version.h"
55 static const char *ignored_types
[] = {
65 static const char *really_wanted
[] = {
72 #ifdef DEV_DSK_DEVICES
73 static const char *base_devices
[] = {
92 static const char *base_devices
[] = {
113 * Global variables for options
115 char *devices
[MAX_DEVICES
];
116 char *args
[MAX_ARGS
];
117 int num_devices
, num_args
;
128 struct fs_info
*filesys_info
;
129 struct fsck_instance
*instance_list
;
130 const char *fsck_prefix_path
= "/sbin:/sbin/fs.d:/sbin/fs:/etc/fs:/etc";
132 static int ignore(struct fs_info
*);
136 extern char *strdup(const char *s
);
139 static char *strdup(const char *s
)
143 ret
= malloc(strlen(s
)+1);
150 static void free_instance(struct fsck_instance
*i
)
161 * Load the filesystem database from /etc/fstab
163 static void load_fs_info(NOARGS
)
169 struct fs_info
*fs_last
= NULL
;
174 /* Open the mount table. */
175 if ((mntfile
= setmntent(MNTTAB
, "r")) == NULL
) {
180 while ((mp
= getmntent(mntfile
)) != NULL
) {
181 fs
= malloc(sizeof(struct fs_info
));
182 memset(fs
, 0, sizeof(struct fs_info
));
183 fs
->device
= strdup(mp
->mnt_fsname
);
184 fs
->mountpt
= strdup(mp
->mnt_dir
);
185 fs
->type
= strdup(mp
->mnt_type
);
186 fs
->opts
= strdup(mp
->mnt_opts
);
187 fs
->freq
= mp
->mnt_freq
;
188 fs
->passno
= mp
->mnt_passno
;
199 (void) endmntent(mntfile
);
202 fprintf(stderr
, "\007\007\007"
203 "WARNING: Your /etc/fstab does not contain the fsck passno\n");
205 " field. I will kludge around things for you, but you\n");
207 " should fix your /etc/fstab file as soon as you can.\n\n");
209 for (fs
= filesys_info
; fs
; fs
= fs
->next
) {
215 #endif /* HAVE_MNTENT_H */
218 /* Lookup filesys in /etc/fstab and return the corresponding entry. */
219 static struct fs_info
*lookup(char *filesys
)
223 /* No filesys name given. */
227 for (fs
= filesys_info
; fs
; fs
= fs
->next
) {
228 if (!strcmp(filesys
, fs
->device
) ||
229 !strcmp(filesys
, fs
->mountpt
))
236 /* Find fsck program for a given fs type. */
237 static char *find_fsck(char *type
)
241 static char prog
[256];
242 char *p
= strdup(fsck_path
);
245 /* Are we looking for a program or just a type? */
246 tpl
= (strncmp(type
, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
248 for(s
= strtok(p
, ":"); s
; s
= strtok(NULL
, ":")) {
249 sprintf(prog
, tpl
, s
, type
);
250 if (stat(prog
, &st
) == 0) break;
253 return(s
? prog
: NULL
);
257 * Execute a particular fsck program, and link it into the list of
258 * child processes we are waiting for.
260 static int execute(char *prog
, char *device
)
264 struct fsck_instance
*inst
;
267 argv
[0] = strdup(prog
);
270 for (i
=0; i
<num_args
; i
++)
271 argv
[argc
++] = strdup(args
[i
]);
273 argv
[argc
++] = strdup(device
);
278 fprintf(stderr
, "fsck: %s: not found\n", prog
);
282 if (verbose
|| noexecute
) {
284 for (i
=0; i
< argc
; i
++)
285 printf("%s ", argv
[i
]);
291 /* Fork and execute the correct program. */
292 if ((pid
= fork()) < 0) {
295 } else if (pid
== 0) {
296 (void) execv(s
, argv
);
300 inst
= malloc(sizeof(struct fsck_instance
));
303 memset(inst
, 0, sizeof(struct fsck_instance
));
305 inst
->prog
= strdup(prog
);
306 inst
->device
= strdup(device
);
307 inst
->next
= instance_list
;
308 instance_list
= inst
;
314 * Wait for one child process to exit; when it does, unlink it from
315 * the list of executing child processes, and return it.
317 static struct fsck_instance
*wait_one(NOARGS
)
321 struct fsck_instance
*inst
, *prev
;
330 if ((errno
== EINTR
) || (errno
== EAGAIN
))
332 if (errno
== ECHILD
) {
334 "%s: wait: No more child process?!?\n",
341 for (prev
= 0, inst
= instance_list
;
343 prev
= inst
, inst
= inst
->next
) {
344 if (inst
->pid
== pid
)
348 printf("Unexpected child process %d, status = 0x%x\n",
352 if (WIFEXITED(status
))
353 status
= WEXITSTATUS(status
);
354 else if (WIFSIGNALED(status
)) {
355 sig
= WTERMSIG(status
);
357 status
= EXIT_UNCORRECTED
;
359 printf("Warning... %s for device %s exited "
361 inst
->prog
, inst
->device
, sig
);
365 printf("%s %s: status is %x, should never happen.\n",
366 inst
->prog
, inst
->device
, status
);
369 inst
->exit_status
= status
;
371 prev
->next
= inst
->next
;
373 instance_list
= inst
->next
;
378 * Wait until all executing child processes have exited; return the
379 * logical OR of all of their exit code values.
381 static int wait_all(NOARGS
)
383 struct fsck_instance
*inst
;
384 int global_status
= 0;
386 while (instance_list
) {
390 global_status
|= inst
->exit_status
;
393 return global_status
;
397 * Run the fsck program on a particular device
399 * If the type is specified using -t, and it isn't prefixed with "no"
400 * (as in "noext2") and only one filesystem type is specified, then
401 * use that type regardless of what is specified in /etc/fstab.
403 * If the type isn't specified by the user, then use either the type
404 * specified in /etc/fstab, or DEFAULT_FSTYPE.
406 static void fsck_device(char *device
)
408 const char *type
= 0;
409 struct fs_info
*fsent
;
413 if (fstype
&& strncmp(fstype
, "no", 2) && !strchr(fstype
, ','))
416 if ((fsent
= lookup(device
))) {
417 device
= fsent
->device
;
422 type
= DEFAULT_FSTYPE
;
424 sprintf(prog
, "fsck.%s", type
);
425 retval
= execute(prog
, device
);
427 fprintf(stderr
, "%s: Error %d while executing %s for %s\n",
428 progname
, retval
, prog
, device
);
432 /* See if filesystem type matches the list. */
433 static int fs_match(char *type
, char *fs_type
)
435 int ret
= 0, negate
= 0;
439 if (!fs_type
) return(1);
441 if (strncmp(fs_type
, "no", 2) == 0) {
445 strcpy(list
, fs_type
);
446 s
= strtok(list
, ",");
448 if (strcmp(s
, type
) == 0) {
452 s
= strtok(NULL
, ",");
454 return(negate
? !ret
: ret
);
458 /* Check if we should ignore this filesystem. */
459 static int ignore(struct fs_info
*fs
)
466 * If the pass number is 0, ignore it.
472 * If a specific fstype is specified, and it doesn't match,
475 if (!fs_match(fs
->type
, fstype
)) return 1;
477 /* Noauto never matches. */
478 for (cp
= strtok(fs
->opts
, ","); cp
!= NULL
; cp
= strtok(NULL
, ",")) {
479 if (!strcmp(cp
, "noauto"))
483 /* Are we ignoring this type? */
484 for(ip
= ignored_types
; *ip
; ip
++)
485 if (strcmp(fs
->type
, *ip
) == 0) return(1);
487 /* Do we really really want to check this fs? */
488 for(ip
= really_wanted
; *ip
; ip
++)
489 if (strcmp(fs
->type
, *ip
) == 0) {
494 /* See if the <fsck.fs> program is available. */
495 if (find_fsck(fs
->type
) == NULL
) {
497 fprintf(stderr
, "fsck: cannot check %s: fsck.%s not found\n",
498 fs
->device
, fs
->type
);
502 /* We can and want to check this file system type. */
507 * Return the "base device" given a particular device; this is used to
508 * assure that we only fsck one partition on a particular drive at any
509 * one time. Otherwise, the disk heads will be seeking all over the
512 static const char *base_device(char *device
)
516 for (base
= base_devices
; *base
; base
++) {
517 if (!strncmp(*base
, device
, strlen(*base
)))
524 * Returns TRUE if a partition on the same disk is already being
527 static int device_already_active(char *device
)
529 struct fsck_instance
*inst
;
532 base
= base_device(device
);
534 for (inst
= instance_list
; inst
; inst
= inst
->next
) {
535 if (!strcmp(base
, base_device(inst
->device
)))
542 /* Check all file systems, using the /etc/fstab table. */
543 static int check_all(NOARGS
)
546 struct fsck_instance
*inst
;
547 int status
= EXIT_OK
;
548 int not_done_yet
= 1;
553 printf("Checking all file systems.\n");
556 * Find and check the root filesystem first.
558 for (fs
= filesys_info
; fs
; fs
= fs
->next
) {
559 if (!strcmp(fs
->mountpt
, "/"))
562 if (fs
&& !skip_root
&& !ignore(fs
)) {
563 fsck_device(fs
->device
);
564 fs
->flags
|= FLAG_DONE
;
565 status
|= wait_all();
566 if (status
> EXIT_NONDESTRUCT
)
569 if (fs
) fs
->flags
|= FLAG_DONE
;
572 * Mark filesystems that should be ignored as done.
574 for (fs
= filesys_info
; fs
; fs
= fs
->next
) {
576 fs
->flags
|= FLAG_DONE
;
579 while (not_done_yet
) {
583 for (fs
= filesys_info
; fs
; fs
= fs
->next
) {
584 if (fs
->flags
& FLAG_DONE
)
587 * If the filesystem's pass number is higher
588 * than the current pass number, then we don't
591 if (fs
->passno
> passno
) {
596 * If a filesystem on a particular device has
597 * already been spawned, then we need to defer
598 * this to another pass.
600 if (device_already_active(fs
->device
)) {
605 * Spawn off the fsck process
607 fsck_device(fs
->device
);
608 fs
->flags
|= FLAG_DONE
;
611 break; /* Only do one filesystem at a time */
615 status
|= inst
->exit_status
;
619 status
|= wait_all();
621 printf("----------------------------------\n");
626 status
|= wait_all();
630 static void usage(NOARGS
)
633 "Usage: fsck [-AV] [-t fstype] [fs-options] filesys\n");
637 static void PRS(int argc
, char *argv
[])
643 int opts_for_fsck
= 0;
653 for (i
=1; i
< argc
; i
++) {
658 if (num_devices
>= MAX_DEVICES
) {
659 fprintf(stderr
, "%s: too many devices\n",
663 devices
[num_devices
++] = strdup(arg
);
667 if (num_args
>= MAX_ARGS
) {
668 fprintf(stderr
, "%s: too many arguments\n",
672 args
[num_args
++] = strdup(arg
);
675 for (j
=1; arg
[j
]; j
++) {
677 options
[++opt
] = arg
[j
];
704 fstype
= strdup(arg
+j
+1);
709 fstype
= strdup(argv
[i
]);
718 options
[++opt
] = arg
[j
];
725 options
[++opt
] = '\0';
726 if (num_args
>= MAX_ARGS
) {
728 "%s: too many arguments\n",
732 args
[num_args
++] = strdup(options
);
738 int main(int argc
, char *argv
[])
742 char *oldpath
= getenv("PATH");
747 printf("Parallelizing fsck version %s (%s)\n",
748 E2FSPROGS_VERSION
, E2FSPROGS_DATE
);
750 /* Update our search path to include uncommon directories. */
752 fsck_path
= malloc (strlen (fsck_prefix_path
) + 1 +
753 strlen (oldpath
) + 1);
754 strcpy (fsck_path
, fsck_prefix_path
);
755 strcat (fsck_path
, ":");
756 strcat (fsck_path
, oldpath
);
758 fsck_path
= strdup(oldpath
);
761 /* If -A was specified ("check all"), do that! */
765 for (i
= 0 ; i
< num_devices
; i
++) {
766 fsck_device(devices
[i
]);
768 struct fsck_instance
*inst
;
772 status
|= inst
->exit_status
;
777 status
|= wait_all();