2 * fsck --- 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.
22 * Copyright (C) 2009-2014 Karel Zak <kzak@redhat.com>
24 * 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>
46 #include <sys/resource.h>
52 #include "pathnames.h"
53 #include "exitcodes.h"
55 #include "closestream.h"
56 #include "fileutils.h"
58 #define XALLOC_EXIT_CODE FSCK_EX_ERROR
61 #ifndef DEFAULT_FSTYPE
62 # define DEFAULT_FSTYPE "ext2"
65 #define MAX_DEVICES 32
68 #define FSCK_RUNTIME_DIRNAME "/run/fsck"
70 static const char *ignored_types
[] = {
77 static const char *really_wanted
[] = {
89 * Internal structure for mount tabel entries.
95 unsigned int stacked
:1,
101 * Structure to allow exit codes to be stored
103 struct fsck_instance
{
105 int flags
; /* FLAG_{DONE|PROGRESS} */
107 int lock
; /* flock()ed lockpath file descriptor or -1 */
108 char *lockpath
; /* /run/fsck/<diskname>.lock or NULL */
111 struct timeval start_time
;
112 struct timeval end_time
;
116 struct rusage rusage
;
117 struct libmnt_fs
*fs
;
118 struct fsck_instance
*next
;
122 #define FLAG_PROGRESS 2
125 * Global variables for options
127 static char *devices
[MAX_DEVICES
];
128 static char *args
[MAX_ARGS
];
129 static int num_devices
, num_args
;
134 static int noexecute
;
135 static int serialize
;
136 static int skip_root
;
137 static int ignore_mounted
;
139 static int parallel_root
;
141 static int progress_fd
;
142 static int force_all_parallel
;
143 static int report_stats
;
145 static int num_running
;
146 static int max_running
;
148 static volatile int cancel_requested
;
149 static int kill_sent
;
151 static struct fsck_instance
*instance_list
;
153 static const char fsck_prefix_path
[] = FS_SEARCH_PATH
;
154 static char *fsck_path
;
156 /* parsed fstab and mtab */
157 static struct libmnt_table
*fstab
, *mtab
;
158 static struct libmnt_cache
*mntcache
;
160 static int count_slaves(dev_t disk
);
162 static int string_to_int(const char *s
)
167 l
= strtol(s
, &p
, 0);
168 if (*p
|| l
== LONG_MIN
|| l
== LONG_MAX
|| l
< 0 || l
> INT_MAX
)
174 /* Do we really really want to check this fs? */
175 static int fs_check_required(const char *type
)
179 for(i
= 0; i
< ARRAY_SIZE(really_wanted
); i
++) {
180 if (strcmp(type
, really_wanted
[i
]) == 0)
187 static int is_mounted(struct libmnt_fs
*fs
)
192 src
= mnt_fs_get_source(fs
);
196 mntcache
= mnt_new_cache();
198 mtab
= mnt_new_table();
200 err(FSCK_EX_ERROR
, ("failed to initialize libmount table"));
201 mnt_table_set_cache(mtab
, mntcache
);
202 mnt_table_parse_mtab(mtab
, NULL
);
205 rc
= mnt_table_find_source(mtab
, src
, MNT_ITER_BACKWARD
) ? 1 : 0;
208 printf(_("%s is mounted\n"), src
);
210 printf(_("%s is not mounted\n"), src
);
215 static int ignore(struct libmnt_fs
*);
217 static struct fsck_fs_data
*fs_create_data(struct libmnt_fs
*fs
)
219 struct fsck_fs_data
*data
= mnt_fs_get_userdata(fs
);
222 data
= xcalloc(1, sizeof(*data
));
223 mnt_fs_set_userdata(fs
, data
);
229 * fs from fstab might contains real device name as well as symlink,
230 * LABEL or UUID, this function returns canonicalized result.
232 static const char *fs_get_device(struct libmnt_fs
*fs
)
234 struct fsck_fs_data
*data
= mnt_fs_get_userdata(fs
);
236 if (!data
|| !data
->eval_device
) {
237 const char *spec
= mnt_fs_get_source(fs
);
240 data
= fs_create_data(fs
);
242 data
->eval_device
= 1;
243 data
->device
= mnt_resolve_spec(spec
, mnt_table_get_cache(fstab
));
245 data
->device
= xstrdup(spec
);
251 static dev_t
fs_get_disk(struct libmnt_fs
*fs
, int check
)
253 struct fsck_fs_data
*data
;
257 data
= mnt_fs_get_userdata(fs
);
258 if (data
&& data
->disk
)
264 if (mnt_fs_is_netfs(fs
) || mnt_fs_is_pseudofs(fs
))
267 device
= fs_get_device(fs
);
271 data
= fs_create_data(fs
);
273 if (!stat(device
, &st
) &&
274 !blkid_devno_to_wholedisk(st
.st_rdev
, NULL
, 0, &data
->disk
)) {
277 data
->stacked
= count_slaves(data
->disk
) > 0 ? 1 : 0;
283 static int fs_is_stacked(struct libmnt_fs
*fs
)
285 struct fsck_fs_data
*data
= mnt_fs_get_userdata(fs
);
286 return data
? data
->stacked
: 0;
289 static int fs_is_done(struct libmnt_fs
*fs
)
291 struct fsck_fs_data
*data
= mnt_fs_get_userdata(fs
);
292 return data
? data
->done
: 0;
295 static void fs_set_done(struct libmnt_fs
*fs
)
297 struct fsck_fs_data
*data
= fs_create_data(fs
);
303 static int is_irrotational_disk(dev_t disk
)
310 rc
= snprintf(path
, sizeof(path
),
311 "/sys/dev/block/%d:%d/queue/rotational",
312 major(disk
), minor(disk
));
314 if (rc
< 0 || (unsigned int) (rc
+ 1) > sizeof(path
))
317 f
= fopen(path
, "r");
321 rc
= fscanf(f
, "%d", &x
);
324 warn(_("cannot read %s"), path
);
326 warnx(_("parse error: %s"), path
);
330 return rc
== 1 ? !x
: 0;
333 static void lock_disk(struct fsck_instance
*inst
)
335 dev_t disk
= fs_get_disk(inst
->fs
, 1);
336 char *diskpath
= NULL
, *diskname
;
340 if (!disk
|| is_irrotational_disk(disk
))
343 diskpath
= blkid_devno_to_devname(disk
);
347 if (access(FSCK_RUNTIME_DIRNAME
, F_OK
) != 0) {
348 int rc
= mkdir(FSCK_RUNTIME_DIRNAME
,
350 S_IRUSR
|S_IRGRP
|S_IROTH
|
351 S_IXUSR
|S_IXGRP
|S_IXOTH
);
352 if (rc
&& errno
!= EEXIST
) {
353 warn(_("cannot create directory %s"),
354 FSCK_RUNTIME_DIRNAME
);
359 diskname
= stripoff_last_component(diskpath
);
363 xasprintf(&inst
->lockpath
, FSCK_RUNTIME_DIRNAME
"/%s.lock", diskname
);
366 printf(_("Locking disk by %s ... "), inst
->lockpath
);
368 inst
->lock
= open(inst
->lockpath
, O_RDONLY
|O_CREAT
|O_CLOEXEC
,
369 S_IWUSR
|S_IRUSR
|S_IRGRP
|S_IROTH
);
370 if (inst
->lock
>= 0) {
373 /* inform users that we're waiting on the lock */
375 (rc
= flock(inst
->lock
, LOCK_EX
| LOCK_NB
)) != 0 &&
376 errno
== EWOULDBLOCK
)
377 printf(_("(waiting) "));
379 if (rc
!= 0 && flock(inst
->lock
, LOCK_EX
) != 0) {
380 close(inst
->lock
); /* failed */
386 /* TRANSLATORS: These are followups to "Locking disk...". */
387 printf("%s.\n", inst
->lock
>= 0 ? _("succeeded") : _("failed"));
391 if (inst
->lock
< 0) {
392 free(inst
->lockpath
);
393 inst
->lockpath
= NULL
;
399 static void unlock_disk(struct fsck_instance
*inst
)
405 printf(_("Unlocking %s.\n"), inst
->lockpath
);
407 close(inst
->lock
); /* unlock */
408 unlink(inst
->lockpath
);
410 free(inst
->lockpath
);
413 inst
->lockpath
= NULL
;
416 static void free_instance(struct fsck_instance
*i
)
427 static struct libmnt_fs
*add_dummy_fs(const char *device
)
429 struct libmnt_fs
*fs
= mnt_new_fs();
431 if (fs
&& mnt_fs_set_source(fs
, device
) == 0 &&
432 mnt_table_add_fs(fstab
, fs
) == 0) {
438 err(FSCK_EX_ERROR
, _("failed to setup description for %s"), device
);
441 static void fs_interpret_type(struct libmnt_fs
*fs
)
444 const char *type
= mnt_fs_get_fstype(fs
);
446 if (type
&& strcmp(type
, "auto") != 0)
449 mnt_fs_set_fstype(fs
, NULL
);
451 device
= fs_get_device(fs
);
455 type
= mnt_get_fstype(device
, &ambi
, mnt_table_get_cache(fstab
));
457 mnt_fs_set_fstype(fs
, type
);
461 static int parser_errcb(struct libmnt_table
*tb
__attribute__ ((__unused__
)),
462 const char *filename
, int line
)
464 warnx(_("%s: parse error at line %d -- ignore"), filename
, line
);
469 * Load the filesystem database from /etc/fstab
471 static void load_fs_info(void)
475 fstab
= mnt_new_table();
477 err(FSCK_EX_ERROR
, ("failed to initialize libmount table"));
479 mnt_table_set_parser_errcb(fstab
, parser_errcb
);
480 mnt_table_set_cache(fstab
, mntcache
);
485 * Let's follow libmount defauls if $FSTAB_FILE is not specified
487 path
= getenv("FSTAB_FILE");
489 if (mnt_table_parse_fstab(fstab
, path
)) {
491 path
= mnt_get_fstab_path();
493 /* don't print error when there is no fstab at all */
494 if (access(path
, F_OK
) == 0) {
496 warn(_("%s: failed to parse fstab"), path
);
498 warnx(_("%s: failed to parse fstab"), path
);
504 * Lookup filesys in /etc/fstab and return the corresponding entry.
505 * The @path has to be real path (no TAG) by mnt_resolve_spec().
507 static struct libmnt_fs
*lookup(char *path
)
509 struct libmnt_fs
*fs
;
514 fs
= mnt_table_find_srcpath(fstab
, path
, MNT_ITER_FORWARD
);
517 * Maybe mountpoint has been specified on fsck command line.
518 * Yeah, crazy feature...
520 * Note that mnt_table_find_target() may canonicalize paths in
521 * the fstab to support symlinks. This is really unwanted,
522 * because readlink() on mountpoints may trigger automounts.
524 * So, disable the cache and compare the paths as strings
525 * without care about symlinks...
527 mnt_table_set_cache(fstab
, NULL
);
528 fs
= mnt_table_find_target(fstab
, path
, MNT_ITER_FORWARD
);
529 mnt_table_set_cache(fstab
, mntcache
);
534 /* Find fsck program for a given fs type. */
535 static char *find_fsck(const char *type
)
539 static char prog
[256];
540 char *p
= xstrdup(fsck_path
);
543 /* Are we looking for a program or just a type? */
544 tpl
= (strncmp(type
, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
546 for(s
= strtok(p
, ":"); s
; s
= strtok(NULL
, ":")) {
547 sprintf(prog
, tpl
, s
, type
);
548 if (stat(prog
, &st
) == 0)
553 return(s
? prog
: NULL
);
556 static int progress_active(void)
558 struct fsck_instance
*inst
;
560 for (inst
= instance_list
; inst
; inst
= inst
->next
) {
561 if (inst
->flags
& FLAG_DONE
)
563 if (inst
->flags
& FLAG_PROGRESS
)
570 * Process run statistics for finished fsck instances.
572 * If report_stats is 0, do nothing, otherwise print a selection of
573 * interesting rusage statistics as well as elapsed wallclock time.
575 static void print_stats(struct fsck_instance
*inst
)
579 if (!inst
|| !report_stats
|| noexecute
)
582 time_diff
= (inst
->end_time
.tv_sec
- inst
->start_time
.tv_sec
)
583 + (inst
->end_time
.tv_usec
- inst
->start_time
.tv_usec
) / 1E6
;
585 fprintf(stdout
, "%s: status %d, rss %ld, "
586 "real %f, user %d.%06d, sys %d.%06d\n",
587 fs_get_device(inst
->fs
),
589 inst
->rusage
.ru_maxrss
,
591 (int)inst
->rusage
.ru_utime
.tv_sec
,
592 (int)inst
->rusage
.ru_utime
.tv_usec
,
593 (int)inst
->rusage
.ru_stime
.tv_sec
,
594 (int)inst
->rusage
.ru_stime
.tv_usec
);
598 * Execute a particular fsck program, and link it into the list of
599 * child processes we are waiting for.
601 static int execute(const char *progname
, const char *progpath
,
602 const char *type
, struct libmnt_fs
*fs
, int interactive
)
606 struct fsck_instance
*inst
, *p
;
609 inst
= xcalloc(1, sizeof(*inst
));
611 argv
[0] = xstrdup(progname
);
614 for (i
=0; i
<num_args
; i
++)
615 argv
[argc
++] = xstrdup(args
[i
]);
618 if ((strcmp(type
, "ext2") == 0) ||
619 (strcmp(type
, "ext3") == 0) ||
620 (strcmp(type
, "ext4") == 0) ||
621 (strcmp(type
, "ext4dev") == 0)) {
625 if (!progress_active()) {
626 snprintf(tmp
, 80, "-C%d", progress_fd
);
627 inst
->flags
|= FLAG_PROGRESS
;
628 } else if (progress_fd
)
629 snprintf(tmp
, 80, "-C%d", progress_fd
* -1);
631 argv
[argc
++] = xstrdup(tmp
);
635 argv
[argc
++] = xstrdup(fs_get_device(fs
));
638 if (verbose
|| noexecute
) {
639 const char *tgt
= mnt_fs_get_target(fs
);
642 tgt
= fs_get_device(fs
);
643 printf("[%s (%d) -- %s] ", progpath
, num_running
, tgt
);
644 for (i
=0; i
< argc
; i
++)
645 printf("%s ", argv
[i
]);
656 /* Fork and execute the correct program. */
659 else if ((pid
= fork()) < 0) {
660 warn(_("fork failed"));
663 } else if (pid
== 0) {
666 execv(progpath
, argv
);
667 err(FSCK_EX_ERROR
, _("%s: execute failed"), progpath
);
670 for (i
=0; i
< argc
; i
++)
674 inst
->prog
= xstrdup(progname
);
675 inst
->type
= xstrdup(type
);
676 gettimeofday(&inst
->start_time
, NULL
);
680 * Find the end of the list, so we add the instance on at the end.
682 for (p
= instance_list
; p
&& p
->next
; p
= p
->next
);
687 instance_list
= inst
;
693 * Send a signal to all outstanding fsck child processes
695 static int kill_all(int signum
)
697 struct fsck_instance
*inst
;
700 for (inst
= instance_list
; inst
; inst
= inst
->next
) {
701 if (inst
->flags
& FLAG_DONE
)
703 kill(inst
->pid
, signum
);
710 * Wait for one child process to exit; when it does, unlink it from
711 * the list of executing child processes, and return it.
713 static struct fsck_instance
*wait_one(int flags
)
717 struct fsck_instance
*inst
, *inst2
, *prev
;
719 struct rusage rusage
;
725 inst
= instance_list
;
728 while (inst
->next
&& (random() & 1)) {
733 inst
->exit_status
= 0;
738 * gcc -Wall fails saving throw against stupidity
739 * (inst and prev are thought to be uninitialized variables)
744 pid
= wait4(-1, &status
, flags
, &rusage
);
745 if (cancel_requested
&& !kill_sent
) {
749 if ((pid
== 0) && (flags
& WNOHANG
))
752 if ((errno
== EINTR
) || (errno
== EAGAIN
))
754 if (errno
== ECHILD
) {
755 warnx(_("wait: no more child process?!?"));
758 warn(_("waitpid failed"));
761 for (prev
= 0, inst
= instance_list
;
763 prev
= inst
, inst
= inst
->next
) {
764 if (inst
->pid
== pid
)
769 if (WIFEXITED(status
))
770 status
= WEXITSTATUS(status
);
771 else if (WIFSIGNALED(status
)) {
772 sig
= WTERMSIG(status
);
774 status
= FSCK_EX_UNCORRECTED
;
776 warnx(_("Warning... %s for device %s exited "
778 inst
->prog
, fs_get_device(inst
->fs
), sig
);
779 status
= FSCK_EX_ERROR
;
782 warnx(_("%s %s: status is %x, should never happen."),
783 inst
->prog
, fs_get_device(inst
->fs
), status
);
784 status
= FSCK_EX_ERROR
;
787 inst
->exit_status
= status
;
788 inst
->flags
|= FLAG_DONE
;
789 gettimeofday(&inst
->end_time
, NULL
);
790 memcpy(&inst
->rusage
, &rusage
, sizeof(struct rusage
));
792 if (progress
&& (inst
->flags
& FLAG_PROGRESS
) &&
793 !progress_active()) {
794 for (inst2
= instance_list
; inst2
; inst2
= inst2
->next
) {
795 if (inst2
->flags
& FLAG_DONE
)
797 if (strcmp(inst2
->type
, "ext2") &&
798 strcmp(inst2
->type
, "ext3") &&
799 strcmp(inst2
->type
, "ext4") &&
800 strcmp(inst2
->type
, "ext4dev"))
803 * If we've just started the fsck, wait a tiny
804 * bit before sending the kill, to give it
805 * time to set up the signal handler
807 if (inst2
->start_time
.tv_sec
< time(0) + 2) {
810 kill(inst2
->pid
, SIGUSR1
);
814 kill(inst2
->pid
, SIGUSR1
);
815 inst2
->flags
|= FLAG_PROGRESS
;
821 prev
->next
= inst
->next
;
823 instance_list
= inst
->next
;
828 printf(_("Finished with %s (exit status %d)\n"),
829 fs_get_device(inst
->fs
), inst
->exit_status
);
834 #define FLAG_WAIT_ALL 0
835 #define FLAG_WAIT_ATLEAST_ONE 1
837 * Wait until all executing child processes have exited; return the
838 * logical OR of all of their exit code values.
840 static int wait_many(int flags
)
842 struct fsck_instance
*inst
;
843 int global_status
= 0;
846 while ((inst
= wait_one(wait_flags
))) {
847 global_status
|= inst
->exit_status
;
850 if (noexecute
&& (flags
& WNOHANG
) && !(random() % 3))
853 if (flags
& FLAG_WAIT_ATLEAST_ONE
)
854 wait_flags
= WNOHANG
;
856 return global_status
;
860 * Run the fsck program on a particular device
862 * If the type is specified using -t, and it isn't prefixed with "no"
863 * (as in "noext2") and only one filesystem type is specified, then
864 * use that type regardless of what is specified in /etc/fstab.
866 * If the type isn't specified by the user, then use either the type
867 * specified in /etc/fstab, or DEFAULT_FSTYPE.
869 static int fsck_device(struct libmnt_fs
*fs
, int interactive
)
871 char progname
[80], *progpath
;
875 fs_interpret_type(fs
);
877 type
= mnt_fs_get_fstype(fs
);
879 if (type
&& strcmp(type
, "auto") != 0)
881 else if (fstype
&& strncmp(fstype
, "no", 2) &&
882 strncmp(fstype
, "opts=", 5) && strncmp(fstype
, "loop", 4) &&
883 !strchr(fstype
, ','))
886 type
= DEFAULT_FSTYPE
;
888 sprintf(progname
, "fsck.%s", type
);
889 progpath
= find_fsck(progname
);
890 if (progpath
== NULL
) {
891 if (fs_check_required(type
)) {
899 retval
= execute(progname
, progpath
, type
, fs
, interactive
);
906 warnx(_("error %d (%m) while executing fsck.%s for %s"),
907 retval
, type
, fs_get_device(fs
));
908 return FSCK_EX_ERROR
;
913 * Deal with the fsck -t argument.
915 struct fs_type_compile
{
921 #define FS_TYPE_NORMAL 0
922 #define FS_TYPE_OPT 1
923 #define FS_TYPE_NEGOPT 2
925 static void compile_fs_type(char *fs_type
, struct fs_type_compile
*cmp
)
929 int negate
, first_negate
= 1;
932 for (cp
=fs_type
; *cp
; cp
++) {
938 cmp
->list
= xcalloc(num
, sizeof(char *));
939 cmp
->type
= xcalloc(num
, sizeof(int));
945 list
= xstrdup(fs_type
);
947 s
= strtok(list
, ",");
950 if (strncmp(s
, "no", 2) == 0) {
953 } else if (*s
== '!') {
957 if (strcmp(s
, "loop") == 0)
958 /* loop is really short-hand for opts=loop */
959 goto loop_special_case
;
960 else if (strncmp(s
, "opts=", 5) == 0) {
963 cmp
->type
[num
] = negate
? FS_TYPE_NEGOPT
: FS_TYPE_OPT
;
966 cmp
->negate
= negate
;
969 if ((negate
&& !cmp
->negate
) ||
970 (!negate
&& cmp
->negate
)) {
972 _("Either all or none of the filesystem types passed to -t must be prefixed\n"
973 "with 'no' or '!'."));
977 cmp
->list
[num
++] = xstrdup(s
);
978 s
= strtok(NULL
, ",");
984 * This function returns true if a particular option appears in a
985 * comma-delimited options list
987 static int opt_in_list(const char *opt
, const char *optlist
)
993 list
= xstrdup(optlist
);
995 s
= strtok(list
, ",");
997 if (strcmp(s
, opt
) == 0) {
1001 s
= strtok(NULL
, ",");
1007 /* See if the filesystem matches the criteria given by the -t option */
1008 static int fs_match(struct libmnt_fs
*fs
, struct fs_type_compile
*cmp
)
1010 int n
, ret
= 0, checked_type
= 0;
1013 if (cmp
->list
== 0 || cmp
->list
[0] == 0)
1016 for (n
=0; (cp
= cmp
->list
[n
]); n
++) {
1017 switch (cmp
->type
[n
]) {
1018 case FS_TYPE_NORMAL
:
1020 const char *type
= mnt_fs_get_fstype(fs
);
1023 if (type
&& strcmp(cp
, type
) == 0)
1027 case FS_TYPE_NEGOPT
:
1028 if (opt_in_list(cp
, mnt_fs_get_options(fs
)))
1032 if (!opt_in_list(cp
, mnt_fs_get_options(fs
)))
1037 if (checked_type
== 0)
1039 return (cmp
->negate
? !ret
: ret
);
1043 * Check if a device exists
1045 static int device_exists(const char *device
)
1049 if (stat(device
, &st
) == -1)
1051 if (!S_ISBLK(st
.st_mode
))
1056 static int fs_ignored_type(struct libmnt_fs
*fs
)
1058 const char **ip
, *type
;
1060 if (mnt_fs_is_netfs(fs
) || mnt_fs_is_pseudofs(fs
) || mnt_fs_is_swaparea(fs
))
1063 type
= mnt_fs_get_fstype(fs
);
1065 for(ip
= ignored_types
; type
&& *ip
; ip
++) {
1066 if (strcmp(type
, *ip
) == 0)
1073 /* Check if we should ignore this filesystem. */
1074 static int ignore(struct libmnt_fs
*fs
)
1079 * If the pass number is 0, ignore it.
1081 if (mnt_fs_get_passno(fs
) == 0)
1085 * If this is a bind mount, ignore it.
1087 if (opt_in_list("bind", mnt_fs_get_options(fs
))) {
1088 warnx(_("%s: skipping bad line in /etc/fstab: "
1089 "bind mount with nonzero fsck pass number"),
1090 mnt_fs_get_target(fs
));
1095 * ignore devices that don't exist and have the "nofail" mount option
1097 if (!device_exists(fs_get_device(fs
))) {
1098 if (opt_in_list("nofail", mnt_fs_get_options(fs
))) {
1100 printf(_("%s: skipping nonexistent device\n"),
1105 printf(_("%s: nonexistent device (\"nofail\" fstab "
1106 "option may be used to skip this device)\n"),
1110 fs_interpret_type(fs
);
1113 * If a specific fstype is specified, and it doesn't match,
1116 if (!fs_match(fs
, &fs_type_compiled
))
1119 type
= mnt_fs_get_fstype(fs
);
1122 printf(_("%s: skipping unknown filesystem type\n"),
1127 /* Are we ignoring this type? */
1128 if (fs_ignored_type(fs
))
1133 /* See if the <fsck.fs> program is available. */
1134 if (find_fsck(type
) == NULL
) {
1135 if (fs_check_required(type
))
1136 warnx(_("cannot check %s: fsck.%s not found"),
1137 fs_get_device(fs
), type
);
1141 /* We can and want to check this file system type. */
1145 static int count_slaves(dev_t disk
)
1149 char dirname
[PATH_MAX
];
1152 snprintf(dirname
, sizeof(dirname
),
1153 "/sys/dev/block/%u:%u/slaves/",
1154 major(disk
), minor(disk
));
1156 if (!(dir
= opendir(dirname
)))
1159 while ((dp
= readdir(dir
)) != 0) {
1160 #ifdef _DIRENT_HAVE_D_TYPE
1161 if (dp
->d_type
!= DT_UNKNOWN
&& dp
->d_type
!= DT_LNK
)
1164 if (dp
->d_name
[0] == '.' &&
1165 ((dp
->d_name
[1] == 0) ||
1166 ((dp
->d_name
[1] == '.') && (dp
->d_name
[2] == 0))))
1177 * Returns TRUE if a partition on the same disk is already being
1180 static int disk_already_active(struct libmnt_fs
*fs
)
1182 struct fsck_instance
*inst
;
1185 if (force_all_parallel
)
1188 if (instance_list
&& fs_is_stacked(instance_list
->fs
))
1189 /* any instance for a stacked device is already running */
1192 disk
= fs_get_disk(fs
, 1);
1195 * If we don't know the base device, assume that the device is
1196 * already active if there are any fsck instances running.
1198 * Don't check a stacked device with any other disk too.
1200 if (!disk
|| fs_is_stacked(fs
))
1201 return (instance_list
!= 0);
1203 for (inst
= instance_list
; inst
; inst
= inst
->next
) {
1204 dev_t idisk
= fs_get_disk(inst
->fs
, 0);
1206 if (!idisk
|| disk
== idisk
)
1213 /* Check all file systems, using the /etc/fstab table. */
1214 static int check_all(void)
1216 int not_done_yet
= 1;
1219 int status
= FSCK_EX_OK
;
1221 struct libmnt_fs
*fs
;
1222 struct libmnt_iter
*itr
= mnt_new_iter(MNT_ITER_FORWARD
);
1225 err(FSCK_EX_ERROR
, _("failed to allocate iterator"));
1228 * Do an initial scan over the filesystem; mark filesystems
1229 * which should be ignored as done, and resolve any "auto"
1230 * filesystem types (done as a side-effect of calling ignore()).
1232 while (mnt_table_next_fs(fstab
, itr
, &fs
) == 0) {
1240 fputs(_("Checking all file systems.\n"), stdout
);
1243 * Find and check the root filesystem.
1245 if (!parallel_root
) {
1246 fs
= mnt_table_find_target(fstab
, "/", MNT_ITER_FORWARD
);
1250 !(ignore_mounted
&& is_mounted(fs
))) {
1251 status
|= fsck_device(fs
, 1);
1252 status
|= wait_many(FLAG_WAIT_ALL
);
1253 if (status
> FSCK_EX_NONDESTRUCT
) {
1263 * This is for the bone-headed user who enters the root
1264 * filesystem twice. Skip root will skep all root entries.
1267 mnt_reset_iter(itr
, MNT_ITER_FORWARD
);
1269 while(mnt_table_next_fs(fstab
, itr
, &fs
) == 0) {
1270 const char *tgt
= mnt_fs_get_target(fs
);
1272 if (tgt
&& strcmp(tgt
, "/") == 0)
1277 while (not_done_yet
) {
1281 mnt_reset_iter(itr
, MNT_ITER_FORWARD
);
1283 while(mnt_table_next_fs(fstab
, itr
, &fs
) == 0) {
1285 if (cancel_requested
)
1290 * If the filesystem's pass number is higher
1291 * than the current pass number, then we don't
1294 if (mnt_fs_get_passno(fs
) > passno
) {
1298 if (ignore_mounted
&& is_mounted(fs
)) {
1303 * If a filesystem on a particular device has
1304 * already been spawned, then we need to defer
1305 * this to another pass.
1307 if (disk_already_active(fs
)) {
1312 * Spawn off the fsck process
1314 status
|= fsck_device(fs
, serialize
);
1318 * Only do one filesystem at a time, or if we
1319 * have a limit on the number of fsck's extant
1320 * at one time, apply that limit.
1323 (max_running
&& (num_running
>= max_running
))) {
1328 if (cancel_requested
)
1331 printf(_("--waiting-- (pass %d)\n"), passno
);
1333 status
|= wait_many(pass_done
? FLAG_WAIT_ALL
:
1334 FLAG_WAIT_ATLEAST_ONE
);
1337 printf("----------------------------------\n");
1343 if (cancel_requested
&& !kill_sent
) {
1348 status
|= wait_many(FLAG_WAIT_ATLEAST_ONE
);
1353 static void __attribute__((__noreturn__
)) usage(FILE *out
)
1355 fputs(USAGE_HEADER
, out
);
1356 fprintf(out
, _(" %s [options] -- [fs-options] [<filesystem> ...]\n"),
1357 program_invocation_short_name
);
1359 fputs(USAGE_OPTIONS
, out
);
1360 fputs(_(" -A check all filesystems\n"), out
);
1361 fputs(_(" -C [<fd>] display progress bar; file descriptor is for GUIs\n"), out
);
1362 fputs(_(" -l lock the device to guarantee exclusive access\n"), out
);
1363 fputs(_(" -M do not check mounted filesystems\n"), out
);
1364 fputs(_(" -N do not execute, just show what would be done\n"), out
);
1365 fputs(_(" -P check filesystems in parallel, including root\n"), out
);
1366 fputs(_(" -R skip root filesystem; useful only with '-A'\n"), out
);
1367 fputs(_(" -r report statistics for each device checked\n"), out
);
1368 fputs(_(" -s serialize the checking operations\n"), out
);
1369 fputs(_(" -T do not show the title on startup\n"), out
);
1370 fputs(_(" -t <type> specify filesystem types to be checked;\n"
1371 " <type> is allowed to be a comma-separated list\n"), out
);
1372 fputs(_(" -V explain what is being done\n"), out
);
1373 fputs(_(" -? display this help and exit\n"), out
);
1375 fputs(USAGE_SEPARATOR
, out
);
1376 fputs(_("See the specific fsck.* commands for available fs-options."), out
);
1377 fprintf(out
, USAGE_MAN_TAIL("fsck(8)"));
1379 exit(out
== stderr
? FSCK_EX_USAGE
: FSCK_EX_OK
);
1382 static void signal_cancel(int sig
__attribute__((__unused__
)))
1387 static void parse_argv(int argc
, char *argv
[])
1390 char *arg
, *dev
, *tmp
= 0;
1393 int opts_for_fsck
= 0;
1394 struct sigaction sa
;
1397 * Set up signal action
1399 memset(&sa
, 0, sizeof(struct sigaction
));
1400 sa
.sa_handler
= signal_cancel
;
1401 sigaction(SIGINT
, &sa
, 0);
1402 sigaction(SIGTERM
, &sa
, 0);
1408 for (i
=1; i
< argc
; i
++) {
1412 if ((arg
[0] == '/' && !opts_for_fsck
) || strchr(arg
, '=')) {
1413 if (num_devices
>= MAX_DEVICES
)
1414 errx(FSCK_EX_ERROR
, _("too many devices"));
1416 dev
= mnt_resolve_spec(arg
, mntcache
);
1418 if (!dev
&& strchr(arg
, '=')) {
1420 * Check to see if we failed because
1421 * /proc/partitions isn't found.
1423 if (access(_PATH_PROC_PARTITIONS
, R_OK
) < 0) {
1424 warn(_("cannot open %s"),
1425 _PATH_PROC_PARTITIONS
);
1426 errx(FSCK_EX_ERROR
, _("Is /proc mounted?"));
1429 * Check to see if this is because
1430 * we're not running as root
1434 _("must be root to scan for matching filesystems: %s"),
1438 _("couldn't find matching filesystem: %s"),
1441 devices
[num_devices
++] = dev
? dev
: xstrdup(arg
);
1444 if (arg
[0] != '-' || opts_for_fsck
) {
1445 if (num_args
>= MAX_ARGS
)
1446 errx(FSCK_EX_ERROR
, _("too many arguments"));
1447 args
[num_args
++] = xstrdup(arg
);
1450 for (j
=1; arg
[j
]; j
++) {
1451 if (opts_for_fsck
) {
1452 options
[++opt
] = arg
[j
];
1462 progress_fd
= string_to_int(arg
+j
+1);
1463 if (progress_fd
< 0)
1467 } else if ((i
+1) < argc
&&
1468 !strncmp(argv
[i
+1], "-", 1) == 0) {
1469 progress_fd
= string_to_int(argv
[i
]);
1470 if (progress_fd
< 0)
1511 else if ((i
+1) < argc
)
1515 fstype
= xstrdup(tmp
);
1516 compile_fs_type(fstype
, &fs_type_compiled
);
1525 options
[++opt
] = arg
[j
];
1532 options
[++opt
] = '\0';
1533 if (num_args
>= MAX_ARGS
)
1534 errx(FSCK_EX_ERROR
, _("too many arguments"));
1535 args
[num_args
++] = xstrdup(options
);
1539 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1540 force_all_parallel
++;
1541 if ((tmp
= getenv("FSCK_MAX_INST")))
1542 max_running
= atoi(tmp
);
1545 int main(int argc
, char *argv
[])
1548 int interactive
= 0;
1549 char *oldpath
= getenv("PATH");
1550 struct libmnt_fs
*fs
;
1552 setvbuf(stdout
, NULL
, _IONBF
, BUFSIZ
);
1553 setvbuf(stderr
, NULL
, _IONBF
, BUFSIZ
);
1555 setlocale(LC_MESSAGES
, "");
1556 setlocale(LC_CTYPE
, "");
1557 bindtextdomain(PACKAGE
, LOCALEDIR
);
1558 textdomain(PACKAGE
);
1559 atexit(close_stdout
);
1561 mnt_init_debug(0); /* init libmount debug mask */
1562 mntcache
= mnt_new_cache(); /* no fatal error if failed */
1564 parse_argv(argc
, argv
);
1567 printf(UTIL_LINUX_VERSION
);
1571 /* Update our search path to include uncommon directories. */
1573 fsck_path
= xmalloc (strlen (fsck_prefix_path
) + 1 +
1574 strlen (oldpath
) + 1);
1575 strcpy (fsck_path
, fsck_prefix_path
);
1576 strcat (fsck_path
, ":");
1577 strcat (fsck_path
, oldpath
);
1579 fsck_path
= xstrdup(fsck_prefix_path
);
1582 if ((num_devices
== 1) || (serialize
))
1585 if (lockdisk
&& (doall
|| num_devices
> 1)) {
1586 warnx(_("the -l option can be used with one "
1587 "device only -- ignore"));
1591 /* If -A was specified ("check all"), do that! */
1595 if (num_devices
== 0) {
1600 for (i
= 0 ; i
< num_devices
; i
++) {
1601 if (cancel_requested
) {
1608 fs
= lookup(devices
[i
]);
1610 fs
= add_dummy_fs(devices
[i
]);
1611 else if (fs_ignored_type(fs
))
1613 if (ignore_mounted
&& is_mounted(fs
))
1615 status
|= fsck_device(fs
, interactive
);
1617 (max_running
&& (num_running
>= max_running
))) {
1618 struct fsck_instance
*inst
;
1622 status
|= inst
->exit_status
;
1623 free_instance(inst
);
1626 printf("----------------------------------\n");
1629 status
|= wait_many(FLAG_WAIT_ALL
);
1631 mnt_unref_cache(mntcache
);
1632 mnt_unref_table(fstab
);
1633 mnt_unref_table(mtab
);