3 * SPDX-License-Identifier: GPL-2.0-or-later
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * fsck --- A generic, parallelizing front-end for the fsck program.
11 * It will automatically try to run fsck programs in parallel if the
12 * devices are on separate spindles. It is based on the same ideas as
13 * the generic front end for fsck by David Engel and Fred van Kempen,
14 * but it has been completely rewritten from scratch to support
17 * Written by Theodore Ts'o, <tytso@mit.edu>
19 * Miquel van Smoorenburg (miquels@drinkel.ow.org) 20-Oct-1994:
20 * o Changed -t fstype to behave like with mount when -A (all file
21 * systems) or -M (like mount) is specified.
22 * o fsck looks if it can find the fsck.type program to decide
23 * if it should ignore the fs type. This way more fsck programs
24 * can be added without changing this front-end.
25 * o -R flag skip root file system.
27 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
28 * 2001, 2002, 2003, 2004, 2005 by Theodore Ts'o.
30 * Copyright (C) 2009-2014 Karel Zak <kzak@redhat.com>
32 #define _XOPEN_SOURCE 600 /* for inclusion of sa_handler in Solaris */
34 #include <sys/types.h>
50 #include <sys/resource.h>
56 #include "pathnames.h"
57 #include "exitcodes.h"
59 #include "fileutils.h"
60 #include "monotonic.h"
63 #define XALLOC_EXIT_CODE FSCK_EX_ERROR
66 #define CLOSE_EXIT_CODE FSCK_EX_ERROR
67 #include "closestream.h"
69 #ifndef DEFAULT_FSTYPE
70 # define DEFAULT_FSTYPE "ext2"
73 #define MAX_DEVICES 32
76 #define FSCK_RUNTIME_DIRNAME "/run/fsck"
78 static const char *ignored_types
[] = {
85 static const char *really_wanted
[] = {
96 * Internal structure for mount table entries.
102 unsigned int stacked
:1,
108 * Structure to allow exit codes to be stored
110 struct fsck_instance
{
112 int flags
; /* FLAG_{DONE|PROGRESS} */
114 int lock
; /* flock()ed lockpath file descriptor or -1 */
115 char *lockpath
; /* /run/fsck/<diskname>.lock or NULL */
118 struct timeval start_time
;
119 struct timeval end_time
;
123 struct rusage rusage
;
124 struct libmnt_fs
*fs
;
125 struct fsck_instance
*next
;
129 #define FLAG_PROGRESS 2
132 * Global variables for options
134 static char *devices
[MAX_DEVICES
];
135 static char *args
[MAX_ARGS
];
136 static int num_devices
, num_args
;
141 static int noexecute
;
142 static int serialize
;
143 static int skip_root
;
144 static int ignore_mounted
;
146 static int parallel_root
;
148 static int progress_fd
;
149 static int force_all_parallel
;
150 static int report_stats
;
151 static FILE *report_stats_file
;
153 static int num_running
;
154 static int max_running
;
156 static volatile sig_atomic_t cancel_requested
;
157 static int kill_sent
;
159 static struct fsck_instance
*instance_list
;
161 #define FSCK_DEFAULT_PATH "/sbin"
162 static char *fsck_path
;
165 /* parsed fstab and mtab */
166 static struct libmnt_table
*fstab
, *mtab
;
167 static struct libmnt_cache
*mntcache
;
169 static int count_slaves(dev_t disk
);
171 static int string_to_int(const char *s
)
177 l
= strtol(s
, &p
, 0);
178 if (errno
|| *p
|| l
== LONG_MIN
|| l
== LONG_MAX
|| l
< 0 || l
> INT_MAX
)
184 /* Do we really really want to check this fs? */
185 static int fs_check_required(const char *type
)
189 for(i
= 0; i
< ARRAY_SIZE(really_wanted
); i
++) {
190 if (strcmp(type
, really_wanted
[i
]) == 0)
197 static int is_mounted(struct libmnt_fs
*fs
)
202 src
= mnt_fs_get_source(fs
);
206 mntcache
= mnt_new_cache();
208 mtab
= mnt_new_table();
210 err(FSCK_EX_ERROR
, ("failed to initialize libmount table"));
211 mnt_table_set_cache(mtab
, mntcache
);
212 mnt_table_parse_mtab(mtab
, NULL
);
215 rc
= mnt_table_find_source(mtab
, src
, MNT_ITER_BACKWARD
) ? 1 : 0;
218 printf(_("%s is mounted\n"), src
);
220 printf(_("%s is not mounted\n"), src
);
225 static int ignore(struct libmnt_fs
*);
227 static struct fsck_fs_data
*fs_create_data(struct libmnt_fs
*fs
)
229 struct fsck_fs_data
*data
= mnt_fs_get_userdata(fs
);
232 data
= xcalloc(1, sizeof(*data
));
233 mnt_fs_set_userdata(fs
, data
);
239 * fs from fstab might contains real device name as well as symlink,
240 * LABEL or UUID, this function returns canonicalized result.
242 static const char *fs_get_device(struct libmnt_fs
*fs
)
244 struct fsck_fs_data
*data
= mnt_fs_get_userdata(fs
);
246 if (!data
|| !data
->eval_device
) {
247 const char *spec
= mnt_fs_get_source(fs
);
250 data
= fs_create_data(fs
);
252 data
->eval_device
= 1;
253 data
->device
= mnt_resolve_spec(spec
, mnt_table_get_cache(fstab
));
255 data
->device
= xstrdup(spec
);
261 static dev_t
fs_get_disk(struct libmnt_fs
*fs
, int check
)
263 struct fsck_fs_data
*data
;
267 data
= mnt_fs_get_userdata(fs
);
268 if (data
&& data
->disk
)
274 if (mnt_fs_is_netfs(fs
) || mnt_fs_is_pseudofs(fs
))
277 device
= fs_get_device(fs
);
281 data
= fs_create_data(fs
);
283 if (!stat(device
, &st
) &&
284 !blkid_devno_to_wholedisk(st
.st_rdev
, NULL
, 0, &data
->disk
)) {
287 data
->stacked
= count_slaves(data
->disk
) > 0 ? 1 : 0;
293 static int fs_is_stacked(struct libmnt_fs
*fs
)
295 struct fsck_fs_data
*data
= mnt_fs_get_userdata(fs
);
296 return data
? data
->stacked
: 0;
299 static int fs_is_done(struct libmnt_fs
*fs
)
301 struct fsck_fs_data
*data
= mnt_fs_get_userdata(fs
);
302 return data
? data
->done
: 0;
305 static void fs_set_done(struct libmnt_fs
*fs
)
307 struct fsck_fs_data
*data
= fs_create_data(fs
);
313 static int is_irrotational_disk(dev_t disk
)
320 rc
= snprintf(path
, sizeof(path
),
321 "/sys/dev/block/%d:%d/queue/rotational",
322 major(disk
), minor(disk
));
324 if (rc
< 0 || (unsigned int) rc
>= sizeof(path
))
327 f
= fopen(path
, "r");
331 rc
= fscanf(f
, "%d", &x
);
334 warn(_("cannot read %s"), path
);
336 warnx(_("parse error: %s"), path
);
340 return rc
== 1 ? !x
: 0;
343 static void lock_disk(struct fsck_instance
*inst
)
345 dev_t disk
= fs_get_disk(inst
->fs
, 1);
346 char *diskpath
= NULL
, *diskname
;
350 if (!disk
|| is_irrotational_disk(disk
))
353 diskpath
= blkid_devno_to_devname(disk
);
357 if (access(FSCK_RUNTIME_DIRNAME
, F_OK
) != 0) {
358 int rc
= mkdir(FSCK_RUNTIME_DIRNAME
,
360 S_IRUSR
|S_IRGRP
|S_IROTH
|
361 S_IXUSR
|S_IXGRP
|S_IXOTH
);
362 if (rc
&& errno
!= EEXIST
) {
363 warn(_("cannot create directory %s"),
364 FSCK_RUNTIME_DIRNAME
);
369 diskname
= stripoff_last_component(diskpath
);
373 xasprintf(&inst
->lockpath
, FSCK_RUNTIME_DIRNAME
"/%s.lock", diskname
);
376 printf(_("Locking disk by %s ... "), inst
->lockpath
);
378 inst
->lock
= open(inst
->lockpath
, O_RDONLY
|O_CREAT
|O_CLOEXEC
,
379 S_IWUSR
|S_IRUSR
|S_IRGRP
|S_IROTH
);
380 if (inst
->lock
>= 0) {
383 /* inform users that we're waiting on the lock */
385 (rc
= flock(inst
->lock
, LOCK_EX
| LOCK_NB
)) != 0 &&
386 errno
== EWOULDBLOCK
)
387 printf(_("(waiting) "));
389 if (rc
!= 0 && flock(inst
->lock
, LOCK_EX
) != 0) {
390 close(inst
->lock
); /* failed */
396 /* TRANSLATORS: These are followups to "Locking disk...". */
397 printf("%s.\n", inst
->lock
>= 0 ? _("succeeded") : _("failed"));
401 if (inst
->lock
< 0) {
402 free(inst
->lockpath
);
403 inst
->lockpath
= NULL
;
408 static void unlock_disk(struct fsck_instance
*inst
)
414 printf(_("Unlocking %s.\n"), inst
->lockpath
);
416 close(inst
->lock
); /* unlock */
418 free(inst
->lockpath
);
421 inst
->lockpath
= NULL
;
424 static void free_instance(struct fsck_instance
*i
)
434 static struct libmnt_fs
*add_dummy_fs(const char *device
)
436 struct libmnt_fs
*fs
= mnt_new_fs();
438 if (fs
&& mnt_fs_set_source(fs
, device
) == 0 &&
439 mnt_table_add_fs(fstab
, fs
) == 0) {
445 err(FSCK_EX_ERROR
, _("failed to setup description for %s"), device
);
448 static void fs_interpret_type(struct libmnt_fs
*fs
)
451 const char *type
= mnt_fs_get_fstype(fs
);
453 if (type
&& strcmp(type
, "auto") != 0)
456 mnt_fs_set_fstype(fs
, NULL
);
458 device
= fs_get_device(fs
);
462 struct libmnt_cache
*cache
= mnt_table_get_cache(fstab
);
464 tp
= mnt_get_fstype(device
, &ambi
, cache
);
466 mnt_fs_set_fstype(fs
, tp
);
472 static int parser_errcb(struct libmnt_table
*tb
__attribute__ ((__unused__
)),
473 const char *filename
, int line
)
475 warnx(_("%s: parse error at line %d -- ignored"), filename
, line
);
480 * Load the filesystem database from /etc/fstab
482 static void load_fs_info(void)
486 fstab
= mnt_new_table();
488 err(FSCK_EX_ERROR
, ("failed to initialize libmount table"));
490 mnt_table_set_parser_errcb(fstab
, parser_errcb
);
491 mnt_table_set_cache(fstab
, mntcache
);
496 * Let's follow libmount defaults if $FSTAB_FILE is not specified
498 path
= getenv("FSTAB_FILE");
500 if (mnt_table_parse_fstab(fstab
, path
)) {
502 path
= mnt_get_fstab_path();
504 /* don't print error when there is no fstab at all */
505 if (access(path
, F_OK
) == 0) {
507 warn(_("%s: failed to parse fstab"), path
);
509 warnx(_("%s: failed to parse fstab"), path
);
515 * Lookup filesys in /etc/fstab and return the corresponding entry.
516 * The @path has to be real path (no TAG) by mnt_resolve_spec().
518 static struct libmnt_fs
*lookup(char *path
)
520 struct libmnt_fs
*fs
;
525 fs
= mnt_table_find_srcpath(fstab
, path
, MNT_ITER_FORWARD
);
528 * Maybe mountpoint has been specified on fsck command line.
529 * Yeah, crazy feature...
531 * Note that mnt_table_find_target() may canonicalize paths in
532 * the fstab to support symlinks. This is really unwanted,
533 * because readlink() on mountpoints may trigger automounts.
535 * So, disable the cache and compare the paths as strings
536 * without care about symlinks...
538 mnt_table_set_cache(fstab
, NULL
);
539 fs
= mnt_table_find_target(fstab
, path
, MNT_ITER_FORWARD
);
540 mnt_table_set_cache(fstab
, mntcache
);
545 /* Find fsck program for a given fs type. */
546 static int find_fsck(const char *type
, char **progpath
)
551 char *p
= xstrdup(fsck_path
);
554 /* Are we looking for a program or just a type? */
555 tpl
= (strncmp(type
, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
557 for(s
= strtok(p
, ":"); s
; s
= strtok(NULL
, ":")) {
558 xasprintf(&prog
, tpl
, s
, type
);
559 if (access(prog
, X_OK
) == 0)
576 static int progress_active(void)
578 struct fsck_instance
*inst
;
580 for (inst
= instance_list
; inst
; inst
= inst
->next
) {
581 if (inst
->flags
& FLAG_DONE
)
583 if (inst
->flags
& FLAG_PROGRESS
)
590 * Process run statistics for finished fsck instances.
592 * If report_stats is 0, do nothing, otherwise print a selection of
593 * interesting rusage statistics as well as elapsed wallclock time.
595 static void print_stats(struct fsck_instance
*inst
)
597 struct timeval delta
= { 0 };
599 if (!inst
|| !report_stats
|| noexecute
)
602 timersub(&inst
->end_time
, &inst
->start_time
, &delta
);
604 if (report_stats_file
)
605 fprintf(report_stats_file
, "%s %d %ld"
606 " %"PRId64
".%06"PRId64
607 " %"PRId64
".%06"PRId64
608 " %"PRId64
".%06"PRId64
"\n",
609 fs_get_device(inst
->fs
),
611 inst
->rusage
.ru_maxrss
,
612 (int64_t)delta
.tv_sec
, (int64_t)delta
.tv_usec
,
613 (int64_t)inst
->rusage
.ru_utime
.tv_sec
,
614 (int64_t)inst
->rusage
.ru_utime
.tv_usec
,
615 (int64_t)inst
->rusage
.ru_stime
.tv_sec
,
616 (int64_t)inst
->rusage
.ru_stime
.tv_usec
);
618 fprintf(stdout
, "%s: status %d, rss %ld, "
619 "real %"PRId64
".%06"PRId64
", "
620 "user %"PRId64
".%06"PRId64
", "
621 "sys %"PRId64
".%06"PRId64
"\n",
622 fs_get_device(inst
->fs
),
624 inst
->rusage
.ru_maxrss
,
625 (int64_t)delta
.tv_sec
, (int64_t)delta
.tv_usec
,
626 (int64_t)inst
->rusage
.ru_utime
.tv_sec
,
627 (int64_t)inst
->rusage
.ru_utime
.tv_usec
,
628 (int64_t)inst
->rusage
.ru_stime
.tv_sec
,
629 (int64_t)inst
->rusage
.ru_stime
.tv_usec
);
633 * Execute a particular fsck program, and link it into the list of
634 * child processes we are waiting for.
636 static int execute(const char *progname
, const char *progpath
,
637 const char *type
, struct libmnt_fs
*fs
, int interactive
)
641 struct fsck_instance
*inst
, *p
;
644 inst
= xcalloc(1, sizeof(*inst
));
646 argv
[0] = xstrdup(progname
);
649 for (i
=0; i
<num_args
; i
++)
650 argv
[argc
++] = xstrdup(args
[i
]);
653 ((strcmp(type
, "ext2") == 0) ||
654 (strcmp(type
, "ext3") == 0) ||
655 (strcmp(type
, "ext4") == 0) ||
656 (strcmp(type
, "ext4dev") == 0))) {
660 if (!progress_active()) {
661 snprintf(tmp
, 80, "-C%d", progress_fd
);
662 inst
->flags
|= FLAG_PROGRESS
;
663 } else if (progress_fd
)
664 snprintf(tmp
, 80, "-C%d", progress_fd
* -1);
666 argv
[argc
++] = xstrdup(tmp
);
669 argv
[argc
++] = xstrdup(fs_get_device(fs
));
672 if (verbose
|| noexecute
) {
673 const char *tgt
= mnt_fs_get_target(fs
);
676 tgt
= fs_get_device(fs
);
677 printf("[%s (%d) -- %s] ", progpath
, num_running
, tgt
);
678 for (i
=0; i
< argc
; i
++)
679 printf("%s ", argv
[i
]);
690 /* Fork and execute the correct program. */
693 else if ((pid
= fork()) < 0) {
694 warn(_("fork failed"));
697 } else if (pid
== 0) {
700 execv(progpath
, argv
);
701 err(FSCK_EX_ERROR
, _("%s: execute failed"), progpath
);
704 for (i
=0; i
< argc
; i
++)
708 inst
->prog
= xstrdup(progname
);
709 inst
->type
= xstrdup(type
);
710 gettime_monotonic(&inst
->start_time
);
714 * Find the end of the list, so we add the instance on at the end.
716 for (p
= instance_list
; p
&& p
->next
; p
= p
->next
);
721 instance_list
= inst
;
727 * Send a signal to all outstanding fsck child processes
729 static int kill_all(int signum
)
731 struct fsck_instance
*inst
;
734 for (inst
= instance_list
; inst
; inst
= inst
->next
) {
735 if (inst
->flags
& FLAG_DONE
)
739 kill(inst
->pid
, signum
);
746 * Wait for one child process to exit; when it does, unlink it from
747 * the list of executing child processes, and return it.
749 static struct fsck_instance
*wait_one(int flags
)
753 struct fsck_instance
*inst
, *inst2
, *prev
;
755 struct rusage rusage
;
761 inst
= instance_list
;
764 while (inst
->next
&& (random() & 1)) {
769 inst
->exit_status
= 0;
774 * gcc -Wall fails saving throw against stupidity
775 * (inst and prev are thought to be uninitialized variables)
780 pid
= wait4(-1, &status
, flags
, &rusage
);
781 if (cancel_requested
&& !kill_sent
) {
785 if ((pid
== 0) && (flags
& WNOHANG
))
788 if ((errno
== EINTR
) || (errno
== EAGAIN
))
790 if (errno
== ECHILD
) {
791 warnx(_("wait: no more child process?!?"));
794 warn(_("waitpid failed"));
797 for (prev
= NULL
, inst
= instance_list
;
799 prev
= inst
, inst
= inst
->next
) {
800 if (inst
->pid
== pid
)
805 if (WIFEXITED(status
))
806 status
= WEXITSTATUS(status
);
807 else if (WIFSIGNALED(status
)) {
808 sig
= WTERMSIG(status
);
810 status
= FSCK_EX_UNCORRECTED
;
812 warnx(_("Warning... %s for device %s exited "
814 inst
->prog
, fs_get_device(inst
->fs
), sig
);
815 status
= FSCK_EX_ERROR
;
818 warnx(_("%s %s: status is %x, should never happen."),
819 inst
->prog
, fs_get_device(inst
->fs
), status
);
820 status
= FSCK_EX_ERROR
;
823 inst
->exit_status
= status
;
824 inst
->flags
|= FLAG_DONE
;
825 gettime_monotonic(&inst
->end_time
);
826 memcpy(&inst
->rusage
, &rusage
, sizeof(struct rusage
));
828 if (progress
&& (inst
->flags
& FLAG_PROGRESS
) &&
829 !progress_active()) {
830 for (inst2
= instance_list
; inst2
; inst2
= inst2
->next
) {
831 if (inst2
->flags
& FLAG_DONE
)
833 if (strcmp(inst2
->type
, "ext2") != 0 &&
834 strcmp(inst2
->type
, "ext3") &&
835 strcmp(inst2
->type
, "ext4") != 0 &&
836 strcmp(inst2
->type
, "ext4dev") != 0)
839 * If we've just started the fsck, wait a tiny
840 * bit before sending the kill, to give it
841 * time to set up the signal handler
843 if (inst2
->start_time
.tv_sec
< time(NULL
) + 2) {
846 kill(inst2
->pid
, SIGUSR1
);
850 kill(inst2
->pid
, SIGUSR1
);
851 inst2
->flags
|= FLAG_PROGRESS
;
857 prev
->next
= inst
->next
;
859 instance_list
= inst
->next
;
864 printf(_("Finished with %s (exit status %d)\n"),
865 fs_get_device(inst
->fs
), inst
->exit_status
);
870 #define FLAG_WAIT_ALL 0
871 #define FLAG_WAIT_ATLEAST_ONE 1
873 * Wait until all executing child processes have exited; return the
874 * logical OR of all of their exit code values.
876 static int wait_many(int flags
)
878 struct fsck_instance
*inst
;
879 int global_status
= 0;
882 while ((inst
= wait_one(wait_flags
))) {
883 global_status
|= inst
->exit_status
;
886 if (noexecute
&& (flags
& WNOHANG
) && !(random() % 3))
889 if (flags
& FLAG_WAIT_ATLEAST_ONE
)
890 wait_flags
= WNOHANG
;
892 return global_status
;
896 * Run the fsck program on a particular device
898 * If the type is specified using -t, and it isn't prefixed with "no"
899 * (as in "noext2") and only one filesystem type is specified, then
900 * use that type regardless of what is specified in /etc/fstab.
902 * If the type isn't specified by the user, then use either the type
903 * specified in /etc/fstab, or DEFAULT_FSTYPE.
905 static int fsck_device(struct libmnt_fs
*fs
, int interactive
, int warn_notfound
)
907 char *progname
, *progpath
;
911 fs_interpret_type(fs
);
913 type
= mnt_fs_get_fstype(fs
);
915 if (type
&& strcmp(type
, "auto") != 0)
917 else if (fstype
&& strncmp(fstype
, "no", 2) != 0 &&
918 strncmp(fstype
, "opts=", 5) != 0 && strncmp(fstype
, "loop", 4) != 0 &&
919 !strchr(fstype
, ','))
922 type
= DEFAULT_FSTYPE
;
924 xasprintf(&progname
, "fsck.%s", type
);
926 if (!find_fsck(progname
, &progpath
)) {
928 if (fs_check_required(type
)) {
933 warnx(_("fsck.%s not found; ignore %s"), type
,
939 retval
= execute(progname
, progpath
, type
, fs
, interactive
);
948 warnx(_("error %d (%s) while executing fsck.%s for %s"),
949 retval
, strerror(errno
), type
, fs_get_device(fs
));
950 return FSCK_EX_ERROR
;
955 * Deal with the fsck -t argument.
957 static struct fs_type_compile
{
963 #define FS_TYPE_NORMAL 0
964 #define FS_TYPE_OPT 1
965 #define FS_TYPE_NEGOPT 2
967 static void compile_fs_type(char *fs_type
, struct fs_type_compile
*cmp
)
971 int negate
, first_negate
= 1;
974 for (cp
=fs_type
; *cp
; cp
++) {
980 cmp
->list
= xcalloc(num
, sizeof(char *));
981 cmp
->type
= xcalloc(num
, sizeof(int));
987 list
= xstrdup(fs_type
);
989 s
= strtok(list
, ",");
992 if (strncmp(s
, "no", 2) == 0) {
995 } else if (*s
== '!') {
999 if (strcmp(s
, "loop") == 0)
1000 /* loop is really short-hand for opts=loop */
1001 goto loop_special_case
;
1002 else if (strncmp(s
, "opts=", 5) == 0) {
1005 cmp
->type
[num
] = negate
? FS_TYPE_NEGOPT
: FS_TYPE_OPT
;
1008 cmp
->negate
= negate
;
1011 if ((negate
&& !cmp
->negate
) ||
1012 (!negate
&& cmp
->negate
)) {
1014 _("Either all or none of the filesystem types passed to -t must be prefixed\n"
1015 "with 'no' or '!'."));
1019 cmp
->list
[num
++] = xstrdup(s
);
1020 s
= strtok(NULL
, ",");
1026 * This function returns true if a particular option appears in a
1027 * comma-delimited options list
1029 static int opt_in_list(const char *opt
, const char *optlist
)
1035 list
= xstrdup(optlist
);
1037 s
= strtok(list
, ",");
1039 if (strcmp(s
, opt
) == 0) {
1043 s
= strtok(NULL
, ",");
1049 /* See if the filesystem matches the criteria given by the -t option */
1050 static int fs_match(struct libmnt_fs
*fs
, struct fs_type_compile
*cmp
)
1052 int n
, ret
= 0, checked_type
= 0;
1055 if (cmp
->list
== NULL
|| cmp
->list
[0] == NULL
)
1058 for (n
=0; (cp
= cmp
->list
[n
]); n
++) {
1059 switch (cmp
->type
[n
]) {
1060 case FS_TYPE_NORMAL
:
1062 const char *type
= mnt_fs_get_fstype(fs
);
1065 if (type
&& strcmp(cp
, type
) == 0)
1069 case FS_TYPE_NEGOPT
:
1070 if (opt_in_list(cp
, mnt_fs_get_options(fs
)))
1074 if (!opt_in_list(cp
, mnt_fs_get_options(fs
)))
1079 if (checked_type
== 0)
1081 return (cmp
->negate
? !ret
: ret
);
1085 * Check if a device exists
1087 static int device_exists(const char *device
)
1091 if (stat(device
, &st
) == -1)
1093 if (!S_ISBLK(st
.st_mode
))
1098 static int fs_ignored_type(struct libmnt_fs
*fs
)
1100 const char **ip
, *type
;
1102 if (!mnt_fs_is_regularfs(fs
))
1105 type
= mnt_fs_get_fstype(fs
);
1107 for(ip
= ignored_types
; type
&& *ip
; ip
++) {
1108 if (strcmp(type
, *ip
) == 0)
1115 /* Check if we should ignore this filesystem. */
1116 static int ignore(struct libmnt_fs
*fs
)
1121 * If the pass number is 0, ignore it.
1123 if (mnt_fs_get_passno(fs
) == 0)
1127 * If this is a bind mount, ignore it.
1129 if (opt_in_list("bind", mnt_fs_get_options(fs
))) {
1130 warnx(_("%s: skipping bad line in /etc/fstab: "
1131 "bind mount with nonzero fsck pass number"),
1132 mnt_fs_get_target(fs
));
1137 * ignore devices that don't exist and have the "nofail" mount option
1139 if (!device_exists(fs_get_device(fs
))) {
1140 if (opt_in_list("nofail", mnt_fs_get_options(fs
))) {
1142 printf(_("%s: skipping nonexistent device\n"),
1147 printf(_("%s: nonexistent device (\"nofail\" fstab "
1148 "option may be used to skip this device)\n"),
1152 fs_interpret_type(fs
);
1155 * If a specific fstype is specified, and it doesn't match,
1158 if (!fs_match(fs
, &fs_type_compiled
))
1161 type
= mnt_fs_get_fstype(fs
);
1164 printf(_("%s: skipping unknown filesystem type\n"),
1169 /* Are we ignoring this type? */
1170 if (fs_ignored_type(fs
))
1175 /* See if the <fsck.fs> program is available. */
1176 if (!find_fsck(type
, NULL
)) {
1177 if (fs_check_required(type
))
1178 warnx(_("cannot check %s: fsck.%s not found"),
1179 fs_get_device(fs
), type
);
1183 /* We can and want to check this file system type. */
1187 static int count_slaves(dev_t disk
)
1191 char dirname
[PATH_MAX
];
1194 snprintf(dirname
, sizeof(dirname
),
1195 "/sys/dev/block/%u:%u/slaves/",
1196 major(disk
), minor(disk
));
1198 if (!(dir
= opendir(dirname
)))
1201 while ((dp
= readdir(dir
)) != NULL
) {
1202 #ifdef _DIRENT_HAVE_D_TYPE
1203 if (dp
->d_type
!= DT_UNKNOWN
&& dp
->d_type
!= DT_LNK
)
1206 if (dp
->d_name
[0] == '.' &&
1207 ((dp
->d_name
[1] == 0) ||
1208 ((dp
->d_name
[1] == '.') && (dp
->d_name
[2] == 0))))
1219 * Returns TRUE if a partition on the same disk is already being
1222 static int disk_already_active(struct libmnt_fs
*fs
)
1224 struct fsck_instance
*inst
;
1227 if (force_all_parallel
)
1230 if (instance_list
&& fs_is_stacked(instance_list
->fs
))
1231 /* any instance for a stacked device is already running */
1234 disk
= fs_get_disk(fs
, 1);
1237 * If we don't know the base device, assume that the device is
1238 * already active if there are any fsck instances running.
1240 * Don't check a stacked device with any other disk too.
1242 if (!disk
|| fs_is_stacked(fs
))
1243 return (instance_list
!= NULL
);
1245 for (inst
= instance_list
; inst
; inst
= inst
->next
) {
1246 dev_t idisk
= fs_get_disk(inst
->fs
, 0);
1248 if (!idisk
|| disk
== idisk
)
1255 /* Check all file systems, using the /etc/fstab table. */
1256 static int check_all(void)
1258 int not_done_yet
= 1;
1261 int status
= FSCK_EX_OK
;
1263 struct libmnt_fs
*fs
;
1264 struct libmnt_iter
*itr
= mnt_new_iter(MNT_ITER_FORWARD
);
1267 err(FSCK_EX_ERROR
, _("failed to allocate iterator"));
1270 * Do an initial scan over the filesystem; mark filesystems
1271 * which should be ignored as done, and resolve any "auto"
1272 * filesystem types (done as a side-effect of calling ignore()).
1274 while (mnt_table_next_fs(fstab
, itr
, &fs
) == 0) {
1282 fputs(_("Checking all file systems.\n"), stdout
);
1285 * Find and check the root filesystem.
1287 if (!parallel_root
) {
1288 fs
= mnt_table_find_target(fstab
, "/", MNT_ITER_FORWARD
);
1292 !(ignore_mounted
&& is_mounted(fs
))) {
1293 status
|= fsck_device(fs
, 1, 0);
1294 status
|= wait_many(FLAG_WAIT_ALL
);
1295 if (status
> FSCK_EX_NONDESTRUCT
) {
1305 * This is for the bone-headed user who enters the root
1306 * filesystem twice. Skip root will skip all root entries.
1309 mnt_reset_iter(itr
, MNT_ITER_FORWARD
);
1311 while(mnt_table_next_fs(fstab
, itr
, &fs
) == 0) {
1312 const char *tgt
= mnt_fs_get_target(fs
);
1314 if (tgt
&& strcmp(tgt
, "/") == 0)
1319 while (not_done_yet
) {
1323 mnt_reset_iter(itr
, MNT_ITER_FORWARD
);
1325 while(mnt_table_next_fs(fstab
, itr
, &fs
) == 0) {
1327 if (cancel_requested
)
1332 * If the filesystem's pass number is higher
1333 * than the current pass number, then we don't
1336 if (mnt_fs_get_passno(fs
) > passno
) {
1340 if (ignore_mounted
&& is_mounted(fs
)) {
1345 * If a filesystem on a particular device has
1346 * already been spawned, then we need to defer
1347 * this to another pass.
1349 if (disk_already_active(fs
)) {
1354 * Spawn off the fsck process
1356 status
|= fsck_device(fs
, serialize
, 0);
1360 * Only do one filesystem at a time, or if we
1361 * have a limit on the number of fsck's extant
1362 * at one time, apply that limit.
1365 (max_running
&& (num_running
>= max_running
))) {
1370 if (cancel_requested
)
1373 printf(_("--waiting-- (pass %d)\n"), passno
);
1375 status
|= wait_many(pass_done
? FLAG_WAIT_ALL
:
1376 FLAG_WAIT_ATLEAST_ONE
);
1379 printf("----------------------------------\n");
1385 if (cancel_requested
&& !kill_sent
) {
1390 status
|= wait_many(FLAG_WAIT_ATLEAST_ONE
);
1395 static void __attribute__((__noreturn__
)) usage(void)
1398 fputs(USAGE_HEADER
, out
);
1399 fprintf(out
, _(" %s [options] -- [fs-options] [<filesystem> ...]\n"),
1400 program_invocation_short_name
);
1402 fputs(USAGE_SEPARATOR
, out
);
1403 fputs(_("Check and repair a Linux filesystem.\n"), out
);
1405 fputs(USAGE_OPTIONS
, out
);
1406 fputs(_(" -A check all filesystems\n"), out
);
1407 fputs(_(" -C [<fd>] display progress bar; file descriptor is for GUIs\n"), out
);
1408 fputs(_(" -l lock the device to guarantee exclusive access\n"), out
);
1409 fputs(_(" -M do not check mounted filesystems\n"), out
);
1410 fputs(_(" -N do not execute, just show what would be done\n"), out
);
1411 fputs(_(" -P check filesystems in parallel, including root\n"), out
);
1412 fputs(_(" -R skip root filesystem; useful only with '-A'\n"), out
);
1413 fputs(_(" -r [<fd>] report statistics for each device checked;\n"
1414 " file descriptor is for GUIs\n"), out
);
1415 fputs(_(" -s serialize the checking operations\n"), out
);
1416 fputs(_(" -T do not show the title on startup\n"), out
);
1417 fputs(_(" -t <type> specify filesystem types to be checked;\n"
1418 " <type> is allowed to be a comma-separated list\n"), out
);
1419 fputs(_(" -V explain what is being done\n"), out
);
1421 fputs(USAGE_SEPARATOR
, out
);
1422 fprintf(out
, " -?, --help %s\n", USAGE_OPTSTR_HELP
);
1423 fprintf(out
, " --version %s\n", USAGE_OPTSTR_VERSION
);
1424 fputs(USAGE_SEPARATOR
, out
);
1425 fputs(_("See the specific fsck.* commands for available fs-options."), out
);
1426 fprintf(out
, USAGE_MAN_TAIL("fsck(8)"));
1430 static void signal_cancel(int sig
__attribute__((__unused__
)))
1432 cancel_requested
= 1;
1435 static void parse_argv(int argc
, char *argv
[])
1438 char *arg
, *dev
, *tmp
= NULL
;
1441 int opts_for_fsck
= 0;
1442 struct sigaction sa
;
1443 int report_stats_fd
= -1;
1446 * Set up signal action
1448 memset(&sa
, 0, sizeof(struct sigaction
));
1449 sa
.sa_handler
= signal_cancel
;
1450 sigaction(SIGINT
, &sa
, NULL
);
1451 sigaction(SIGTERM
, &sa
, NULL
);
1455 instance_list
= NULL
;
1457 for (i
=1; i
< argc
; i
++) {
1462 /* the only two longopts to satisfy UL standards */
1463 if (!opts_for_fsck
&& !strcmp(arg
, "--help"))
1465 if (!opts_for_fsck
&& !strcmp(arg
, "--version"))
1466 print_version(FSCK_EX_OK
);
1468 if ((arg
[0] == '/' && !opts_for_fsck
) || strchr(arg
, '=')) {
1469 if (num_devices
>= MAX_DEVICES
)
1470 errx(FSCK_EX_ERROR
, _("too many devices"));
1472 dev
= mnt_resolve_spec(arg
, mntcache
);
1474 if (!dev
&& strchr(arg
, '=')) {
1476 * Check to see if we failed because
1477 * /proc/partitions isn't found.
1479 if (access(_PATH_PROC_PARTITIONS
, R_OK
) < 0) {
1480 warn(_("cannot open %s"),
1481 _PATH_PROC_PARTITIONS
);
1482 errx(FSCK_EX_ERROR
, _("Is /proc mounted?"));
1485 * Check to see if this is because
1486 * we're not running as root
1490 _("must be root to scan for matching filesystems: %s"),
1494 _("couldn't find matching filesystem: %s"),
1497 devices
[num_devices
++] = dev
? dev
: xstrdup(arg
);
1500 if (arg
[0] != '-' || opts_for_fsck
) {
1501 if (num_args
>= MAX_ARGS
)
1502 errx(FSCK_EX_ERROR
, _("too many arguments"));
1503 args
[num_args
++] = xstrdup(arg
);
1506 for (j
=1; arg
[j
]; j
++) {
1507 if (opts_for_fsck
) {
1508 options
[++opt
] = arg
[j
];
1517 if (arg
[j
+1]) { /* -C<fd> */
1518 progress_fd
= string_to_int(arg
+j
+1);
1519 if (progress_fd
< 0)
1523 } else if (i
+1 < argc
&& *argv
[i
+1] != '-') { /* -C <fd> */
1524 progress_fd
= string_to_int(argv
[i
+1]);
1525 if (progress_fd
< 0)
1556 if (arg
[j
+1]) { /* -r<fd> */
1557 report_stats_fd
= strtou32_or_err(arg
+j
+1, _("invalid argument of -r"));
1559 } else if (i
+1 < argc
&& *argv
[i
+1] >= '0' && *argv
[i
+1] <= '9') { /* -r <fd> */
1560 report_stats_fd
= strtou32_or_err(argv
[i
+1], _("invalid argument of -r"));
1572 _("option '%s' may be specified only once"), "-t");
1575 else if ((i
+1) < argc
)
1579 _("option '%s' requires an argument"), "-t");
1580 fstype
= xstrdup(tmp
);
1581 compile_fs_type(fstype
, &fs_type_compiled
);
1590 options
[++opt
] = arg
[j
];
1597 options
[++opt
] = '\0';
1598 if (num_args
>= MAX_ARGS
)
1599 errx(FSCK_EX_ERROR
, _("too many arguments"));
1600 args
[num_args
++] = xstrdup(options
);
1605 /* Validate the report stats file descriptor to avoid disasters */
1606 if (report_stats_fd
>= 0) {
1607 report_stats_file
= fdopen(report_stats_fd
, "w");
1608 if (!report_stats_file
)
1610 _("invalid argument of -r: %d"),
1614 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1615 force_all_parallel
++;
1616 if (ul_strtos32(getenv("FSCK_MAX_INST"), &max_running
, 10) != 0)
1620 int main(int argc
, char *argv
[])
1623 int interactive
= 0;
1624 struct libmnt_fs
*fs
;
1625 const char *path
= getenv("PATH");
1627 setvbuf(stdout
, NULL
, _IONBF
, BUFSIZ
);
1628 setvbuf(stderr
, NULL
, _IONBF
, BUFSIZ
);
1630 setlocale(LC_MESSAGES
, "");
1631 setlocale(LC_CTYPE
, "");
1632 bindtextdomain(PACKAGE
, LOCALEDIR
);
1633 textdomain(PACKAGE
);
1634 close_stdout_atexit();
1636 strutils_set_exitcode(FSCK_EX_USAGE
);
1637 mnt_init_debug(0); /* init libmount debug mask */
1638 mntcache
= mnt_new_cache(); /* no fatal error if failed */
1641 /* Force libblkid to accept also filesystems with bad
1642 * checksums. This feature is helpful for "fsck /dev/foo," but
1643 * if it evaluates LABEL/UUIDs from fstab, then libmount may
1644 * use cached data from udevd and udev accepts only properly
1645 * detected filesystems.
1647 mnt_cache_set_sbprobe(mntcache
, BLKID_SUBLKS_BADCSUM
);
1650 parse_argv(argc
, argv
);
1653 printf(UTIL_LINUX_VERSION
);
1655 signal(SIGCHLD
, SIG_DFL
); /* clear any inherited settings */
1659 fsck_path
= xstrdup(path
&& *path
? path
: FSCK_DEFAULT_PATH
);
1661 if ((num_devices
== 1) || (serialize
))
1664 if (lockdisk
&& (doall
|| num_devices
> 1)) {
1665 warnx(_("the -l option can be used with one "
1666 "device only -- ignore"));
1670 /* If -A was specified ("check all"), do that! */
1674 if (num_devices
== 0) {
1679 for (i
= 0 ; i
< num_devices
; i
++) {
1680 if (cancel_requested
) {
1687 fs
= lookup(devices
[i
]);
1689 fs
= add_dummy_fs(devices
[i
]);
1690 else if (fs_ignored_type(fs
))
1692 if (ignore_mounted
&& is_mounted(fs
))
1694 status
|= fsck_device(fs
, interactive
, interactive
);
1696 (max_running
&& (num_running
>= max_running
))) {
1697 struct fsck_instance
*inst
;
1701 status
|= inst
->exit_status
;
1702 free_instance(inst
);
1705 printf("----------------------------------\n");
1708 status
|= wait_many(FLAG_WAIT_ALL
);
1710 mnt_unref_cache(mntcache
);
1711 mnt_unref_table(fstab
);
1712 mnt_unref_table(mtab
);