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 "fileutils.h"
56 #include "monotonic.h"
59 #define XALLOC_EXIT_CODE FSCK_EX_ERROR
62 #define CLOSE_EXIT_CODE FSCK_EX_ERROR
63 #include "closestream.h"
65 #ifndef DEFAULT_FSTYPE
66 # define DEFAULT_FSTYPE "ext2"
69 #define MAX_DEVICES 32
72 #define FSCK_RUNTIME_DIRNAME "/run/fsck"
74 static const char *ignored_types
[] = {
81 static const char *really_wanted
[] = {
92 * Internal structure for mount table entries.
98 unsigned int stacked
:1,
104 * Structure to allow exit codes to be stored
106 struct fsck_instance
{
108 int flags
; /* FLAG_{DONE|PROGRESS} */
110 int lock
; /* flock()ed lockpath file descriptor or -1 */
111 char *lockpath
; /* /run/fsck/<diskname>.lock or NULL */
114 struct timeval start_time
;
115 struct timeval end_time
;
119 struct rusage rusage
;
120 struct libmnt_fs
*fs
;
121 struct fsck_instance
*next
;
125 #define FLAG_PROGRESS 2
128 * Global variables for options
130 static char *devices
[MAX_DEVICES
];
131 static char *args
[MAX_ARGS
];
132 static int num_devices
, num_args
;
137 static int noexecute
;
138 static int serialize
;
139 static int skip_root
;
140 static int ignore_mounted
;
142 static int parallel_root
;
144 static int progress_fd
;
145 static int force_all_parallel
;
146 static int report_stats
;
147 static FILE *report_stats_file
;
149 static int num_running
;
150 static int max_running
;
152 static volatile int cancel_requested
;
153 static int kill_sent
;
155 static struct fsck_instance
*instance_list
;
157 #define FSCK_DEFAULT_PATH "/sbin"
158 static char *fsck_path
;
161 /* parsed fstab and mtab */
162 static struct libmnt_table
*fstab
, *mtab
;
163 static struct libmnt_cache
*mntcache
;
165 static int count_slaves(dev_t disk
);
167 static int string_to_int(const char *s
)
173 l
= strtol(s
, &p
, 0);
174 if (errno
|| *p
|| l
== LONG_MIN
|| l
== LONG_MAX
|| l
< 0 || l
> INT_MAX
)
180 /* Do we really really want to check this fs? */
181 static int fs_check_required(const char *type
)
185 for(i
= 0; i
< ARRAY_SIZE(really_wanted
); i
++) {
186 if (strcmp(type
, really_wanted
[i
]) == 0)
193 static int is_mounted(struct libmnt_fs
*fs
)
198 src
= mnt_fs_get_source(fs
);
202 mntcache
= mnt_new_cache();
204 mtab
= mnt_new_table();
206 err(FSCK_EX_ERROR
, ("failed to initialize libmount table"));
207 mnt_table_set_cache(mtab
, mntcache
);
208 mnt_table_parse_mtab(mtab
, NULL
);
211 rc
= mnt_table_find_source(mtab
, src
, MNT_ITER_BACKWARD
) ? 1 : 0;
214 printf(_("%s is mounted\n"), src
);
216 printf(_("%s is not mounted\n"), src
);
221 static int ignore(struct libmnt_fs
*);
223 static struct fsck_fs_data
*fs_create_data(struct libmnt_fs
*fs
)
225 struct fsck_fs_data
*data
= mnt_fs_get_userdata(fs
);
228 data
= xcalloc(1, sizeof(*data
));
229 mnt_fs_set_userdata(fs
, data
);
235 * fs from fstab might contains real device name as well as symlink,
236 * LABEL or UUID, this function returns canonicalized result.
238 static const char *fs_get_device(struct libmnt_fs
*fs
)
240 struct fsck_fs_data
*data
= mnt_fs_get_userdata(fs
);
242 if (!data
|| !data
->eval_device
) {
243 const char *spec
= mnt_fs_get_source(fs
);
246 data
= fs_create_data(fs
);
248 data
->eval_device
= 1;
249 data
->device
= mnt_resolve_spec(spec
, mnt_table_get_cache(fstab
));
251 data
->device
= xstrdup(spec
);
257 static dev_t
fs_get_disk(struct libmnt_fs
*fs
, int check
)
259 struct fsck_fs_data
*data
;
263 data
= mnt_fs_get_userdata(fs
);
264 if (data
&& data
->disk
)
270 if (mnt_fs_is_netfs(fs
) || mnt_fs_is_pseudofs(fs
))
273 device
= fs_get_device(fs
);
277 data
= fs_create_data(fs
);
279 if (!stat(device
, &st
) &&
280 !blkid_devno_to_wholedisk(st
.st_rdev
, NULL
, 0, &data
->disk
)) {
283 data
->stacked
= count_slaves(data
->disk
) > 0 ? 1 : 0;
289 static int fs_is_stacked(struct libmnt_fs
*fs
)
291 struct fsck_fs_data
*data
= mnt_fs_get_userdata(fs
);
292 return data
? data
->stacked
: 0;
295 static int fs_is_done(struct libmnt_fs
*fs
)
297 struct fsck_fs_data
*data
= mnt_fs_get_userdata(fs
);
298 return data
? data
->done
: 0;
301 static void fs_set_done(struct libmnt_fs
*fs
)
303 struct fsck_fs_data
*data
= fs_create_data(fs
);
309 static int is_irrotational_disk(dev_t disk
)
316 rc
= snprintf(path
, sizeof(path
),
317 "/sys/dev/block/%d:%d/queue/rotational",
318 major(disk
), minor(disk
));
320 if (rc
< 0 || (unsigned int) rc
>= sizeof(path
))
323 f
= fopen(path
, "r");
327 rc
= fscanf(f
, "%d", &x
);
330 warn(_("cannot read %s"), path
);
332 warnx(_("parse error: %s"), path
);
336 return rc
== 1 ? !x
: 0;
339 static void lock_disk(struct fsck_instance
*inst
)
341 dev_t disk
= fs_get_disk(inst
->fs
, 1);
342 char *diskpath
= NULL
, *diskname
;
346 if (!disk
|| is_irrotational_disk(disk
))
349 diskpath
= blkid_devno_to_devname(disk
);
353 if (access(FSCK_RUNTIME_DIRNAME
, F_OK
) != 0) {
354 int rc
= mkdir(FSCK_RUNTIME_DIRNAME
,
356 S_IRUSR
|S_IRGRP
|S_IROTH
|
357 S_IXUSR
|S_IXGRP
|S_IXOTH
);
358 if (rc
&& errno
!= EEXIST
) {
359 warn(_("cannot create directory %s"),
360 FSCK_RUNTIME_DIRNAME
);
365 diskname
= stripoff_last_component(diskpath
);
369 xasprintf(&inst
->lockpath
, FSCK_RUNTIME_DIRNAME
"/%s.lock", diskname
);
372 printf(_("Locking disk by %s ... "), inst
->lockpath
);
374 inst
->lock
= open(inst
->lockpath
, O_RDONLY
|O_CREAT
|O_CLOEXEC
,
375 S_IWUSR
|S_IRUSR
|S_IRGRP
|S_IROTH
);
376 if (inst
->lock
>= 0) {
379 /* inform users that we're waiting on the lock */
381 (rc
= flock(inst
->lock
, LOCK_EX
| LOCK_NB
)) != 0 &&
382 errno
== EWOULDBLOCK
)
383 printf(_("(waiting) "));
385 if (rc
!= 0 && flock(inst
->lock
, LOCK_EX
) != 0) {
386 close(inst
->lock
); /* failed */
392 /* TRANSLATORS: These are followups to "Locking disk...". */
393 printf("%s.\n", inst
->lock
>= 0 ? _("succeeded") : _("failed"));
397 if (inst
->lock
< 0) {
398 free(inst
->lockpath
);
399 inst
->lockpath
= NULL
;
404 static void unlock_disk(struct fsck_instance
*inst
)
410 printf(_("Unlocking %s.\n"), inst
->lockpath
);
412 close(inst
->lock
); /* unlock */
414 free(inst
->lockpath
);
417 inst
->lockpath
= NULL
;
420 static void free_instance(struct fsck_instance
*i
)
430 static struct libmnt_fs
*add_dummy_fs(const char *device
)
432 struct libmnt_fs
*fs
= mnt_new_fs();
434 if (fs
&& mnt_fs_set_source(fs
, device
) == 0 &&
435 mnt_table_add_fs(fstab
, fs
) == 0) {
441 err(FSCK_EX_ERROR
, _("failed to setup description for %s"), device
);
444 static void fs_interpret_type(struct libmnt_fs
*fs
)
447 const char *type
= mnt_fs_get_fstype(fs
);
449 if (type
&& strcmp(type
, "auto") != 0)
452 mnt_fs_set_fstype(fs
, NULL
);
454 device
= fs_get_device(fs
);
458 struct libmnt_cache
*cache
= mnt_table_get_cache(fstab
);
460 tp
= mnt_get_fstype(device
, &ambi
, cache
);
462 mnt_fs_set_fstype(fs
, tp
);
468 static int parser_errcb(struct libmnt_table
*tb
__attribute__ ((__unused__
)),
469 const char *filename
, int line
)
471 warnx(_("%s: parse error at line %d -- ignored"), filename
, line
);
476 * Load the filesystem database from /etc/fstab
478 static void load_fs_info(void)
482 fstab
= mnt_new_table();
484 err(FSCK_EX_ERROR
, ("failed to initialize libmount table"));
486 mnt_table_set_parser_errcb(fstab
, parser_errcb
);
487 mnt_table_set_cache(fstab
, mntcache
);
492 * Let's follow libmount defaults if $FSTAB_FILE is not specified
494 path
= getenv("FSTAB_FILE");
496 if (mnt_table_parse_fstab(fstab
, path
)) {
498 path
= mnt_get_fstab_path();
500 /* don't print error when there is no fstab at all */
501 if (access(path
, F_OK
) == 0) {
503 warn(_("%s: failed to parse fstab"), path
);
505 warnx(_("%s: failed to parse fstab"), path
);
511 * Lookup filesys in /etc/fstab and return the corresponding entry.
512 * The @path has to be real path (no TAG) by mnt_resolve_spec().
514 static struct libmnt_fs
*lookup(char *path
)
516 struct libmnt_fs
*fs
;
521 fs
= mnt_table_find_srcpath(fstab
, path
, MNT_ITER_FORWARD
);
524 * Maybe mountpoint has been specified on fsck command line.
525 * Yeah, crazy feature...
527 * Note that mnt_table_find_target() may canonicalize paths in
528 * the fstab to support symlinks. This is really unwanted,
529 * because readlink() on mountpoints may trigger automounts.
531 * So, disable the cache and compare the paths as strings
532 * without care about symlinks...
534 mnt_table_set_cache(fstab
, NULL
);
535 fs
= mnt_table_find_target(fstab
, path
, MNT_ITER_FORWARD
);
536 mnt_table_set_cache(fstab
, mntcache
);
541 /* Find fsck program for a given fs type. */
542 static int find_fsck(const char *type
, char **progpath
)
547 char *p
= xstrdup(fsck_path
);
550 /* Are we looking for a program or just a type? */
551 tpl
= (strncmp(type
, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
553 for(s
= strtok(p
, ":"); s
; s
= strtok(NULL
, ":")) {
554 xasprintf(&prog
, tpl
, s
, type
);
555 if (access(prog
, X_OK
) == 0)
572 static int progress_active(void)
574 struct fsck_instance
*inst
;
576 for (inst
= instance_list
; inst
; inst
= inst
->next
) {
577 if (inst
->flags
& FLAG_DONE
)
579 if (inst
->flags
& FLAG_PROGRESS
)
586 * Process run statistics for finished fsck instances.
588 * If report_stats is 0, do nothing, otherwise print a selection of
589 * interesting rusage statistics as well as elapsed wallclock time.
591 static void print_stats(struct fsck_instance
*inst
)
593 struct timeval delta
;
595 if (!inst
|| !report_stats
|| noexecute
)
598 timersub(&inst
->end_time
, &inst
->start_time
, &delta
);
600 if (report_stats_file
)
601 fprintf(report_stats_file
, "%s %d %ld"
602 " %"PRId64
".%06"PRId64
603 " %"PRId64
".%06"PRId64
604 " %"PRId64
".%06"PRId64
"\n",
605 fs_get_device(inst
->fs
),
607 inst
->rusage
.ru_maxrss
,
608 (int64_t)delta
.tv_sec
, (int64_t)delta
.tv_usec
,
609 (int64_t)inst
->rusage
.ru_utime
.tv_sec
,
610 (int64_t)inst
->rusage
.ru_utime
.tv_usec
,
611 (int64_t)inst
->rusage
.ru_stime
.tv_sec
,
612 (int64_t)inst
->rusage
.ru_stime
.tv_usec
);
614 fprintf(stdout
, "%s: status %d, rss %ld, "
615 "real %"PRId64
".%06"PRId64
", "
616 "user %"PRId64
".%06"PRId64
", "
617 "sys %"PRId64
".%06"PRId64
"\n",
618 fs_get_device(inst
->fs
),
620 inst
->rusage
.ru_maxrss
,
621 (int64_t)delta
.tv_sec
, (int64_t)delta
.tv_usec
,
622 (int64_t)inst
->rusage
.ru_utime
.tv_sec
,
623 (int64_t)inst
->rusage
.ru_utime
.tv_usec
,
624 (int64_t)inst
->rusage
.ru_stime
.tv_sec
,
625 (int64_t)inst
->rusage
.ru_stime
.tv_usec
);
629 * Execute a particular fsck program, and link it into the list of
630 * child processes we are waiting for.
632 static int execute(const char *progname
, const char *progpath
,
633 const char *type
, struct libmnt_fs
*fs
, int interactive
)
637 struct fsck_instance
*inst
, *p
;
640 inst
= xcalloc(1, sizeof(*inst
));
642 argv
[0] = xstrdup(progname
);
645 for (i
=0; i
<num_args
; i
++)
646 argv
[argc
++] = xstrdup(args
[i
]);
649 ((strcmp(type
, "ext2") == 0) ||
650 (strcmp(type
, "ext3") == 0) ||
651 (strcmp(type
, "ext4") == 0) ||
652 (strcmp(type
, "ext4dev") == 0))) {
656 if (!progress_active()) {
657 snprintf(tmp
, 80, "-C%d", progress_fd
);
658 inst
->flags
|= FLAG_PROGRESS
;
659 } else if (progress_fd
)
660 snprintf(tmp
, 80, "-C%d", progress_fd
* -1);
662 argv
[argc
++] = xstrdup(tmp
);
665 argv
[argc
++] = xstrdup(fs_get_device(fs
));
668 if (verbose
|| noexecute
) {
669 const char *tgt
= mnt_fs_get_target(fs
);
672 tgt
= fs_get_device(fs
);
673 printf("[%s (%d) -- %s] ", progpath
, num_running
, tgt
);
674 for (i
=0; i
< argc
; i
++)
675 printf("%s ", argv
[i
]);
686 /* Fork and execute the correct program. */
689 else if ((pid
= fork()) < 0) {
690 warn(_("fork failed"));
693 } else if (pid
== 0) {
696 execv(progpath
, argv
);
697 err(FSCK_EX_ERROR
, _("%s: execute failed"), progpath
);
700 for (i
=0; i
< argc
; i
++)
704 inst
->prog
= xstrdup(progname
);
705 inst
->type
= xstrdup(type
);
706 gettime_monotonic(&inst
->start_time
);
710 * Find the end of the list, so we add the instance on at the end.
712 for (p
= instance_list
; p
&& p
->next
; p
= p
->next
);
717 instance_list
= inst
;
723 * Send a signal to all outstanding fsck child processes
725 static int kill_all(int signum
)
727 struct fsck_instance
*inst
;
730 for (inst
= instance_list
; inst
; inst
= inst
->next
) {
731 if (inst
->flags
& FLAG_DONE
)
735 kill(inst
->pid
, signum
);
742 * Wait for one child process to exit; when it does, unlink it from
743 * the list of executing child processes, and return it.
745 static struct fsck_instance
*wait_one(int flags
)
749 struct fsck_instance
*inst
, *inst2
, *prev
;
751 struct rusage rusage
;
757 inst
= instance_list
;
760 while (inst
->next
&& (random() & 1)) {
765 inst
->exit_status
= 0;
770 * gcc -Wall fails saving throw against stupidity
771 * (inst and prev are thought to be uninitialized variables)
776 pid
= wait4(-1, &status
, flags
, &rusage
);
777 if (cancel_requested
&& !kill_sent
) {
781 if ((pid
== 0) && (flags
& WNOHANG
))
784 if ((errno
== EINTR
) || (errno
== EAGAIN
))
786 if (errno
== ECHILD
) {
787 warnx(_("wait: no more child process?!?"));
790 warn(_("waitpid failed"));
793 for (prev
= NULL
, inst
= instance_list
;
795 prev
= inst
, inst
= inst
->next
) {
796 if (inst
->pid
== pid
)
801 if (WIFEXITED(status
))
802 status
= WEXITSTATUS(status
);
803 else if (WIFSIGNALED(status
)) {
804 sig
= WTERMSIG(status
);
806 status
= FSCK_EX_UNCORRECTED
;
808 warnx(_("Warning... %s for device %s exited "
810 inst
->prog
, fs_get_device(inst
->fs
), sig
);
811 status
= FSCK_EX_ERROR
;
814 warnx(_("%s %s: status is %x, should never happen."),
815 inst
->prog
, fs_get_device(inst
->fs
), status
);
816 status
= FSCK_EX_ERROR
;
819 inst
->exit_status
= status
;
820 inst
->flags
|= FLAG_DONE
;
821 gettime_monotonic(&inst
->end_time
);
822 memcpy(&inst
->rusage
, &rusage
, sizeof(struct rusage
));
824 if (progress
&& (inst
->flags
& FLAG_PROGRESS
) &&
825 !progress_active()) {
826 for (inst2
= instance_list
; inst2
; inst2
= inst2
->next
) {
827 if (inst2
->flags
& FLAG_DONE
)
829 if (strcmp(inst2
->type
, "ext2") != 0 &&
830 strcmp(inst2
->type
, "ext3") &&
831 strcmp(inst2
->type
, "ext4") != 0 &&
832 strcmp(inst2
->type
, "ext4dev") != 0)
835 * If we've just started the fsck, wait a tiny
836 * bit before sending the kill, to give it
837 * time to set up the signal handler
839 if (inst2
->start_time
.tv_sec
< time(NULL
) + 2) {
842 kill(inst2
->pid
, SIGUSR1
);
846 kill(inst2
->pid
, SIGUSR1
);
847 inst2
->flags
|= FLAG_PROGRESS
;
853 prev
->next
= inst
->next
;
855 instance_list
= inst
->next
;
860 printf(_("Finished with %s (exit status %d)\n"),
861 fs_get_device(inst
->fs
), inst
->exit_status
);
866 #define FLAG_WAIT_ALL 0
867 #define FLAG_WAIT_ATLEAST_ONE 1
869 * Wait until all executing child processes have exited; return the
870 * logical OR of all of their exit code values.
872 static int wait_many(int flags
)
874 struct fsck_instance
*inst
;
875 int global_status
= 0;
878 while ((inst
= wait_one(wait_flags
))) {
879 global_status
|= inst
->exit_status
;
882 if (noexecute
&& (flags
& WNOHANG
) && !(random() % 3))
885 if (flags
& FLAG_WAIT_ATLEAST_ONE
)
886 wait_flags
= WNOHANG
;
888 return global_status
;
892 * Run the fsck program on a particular device
894 * If the type is specified using -t, and it isn't prefixed with "no"
895 * (as in "noext2") and only one filesystem type is specified, then
896 * use that type regardless of what is specified in /etc/fstab.
898 * If the type isn't specified by the user, then use either the type
899 * specified in /etc/fstab, or DEFAULT_FSTYPE.
901 static int fsck_device(struct libmnt_fs
*fs
, int interactive
)
903 char *progname
, *progpath
;
907 fs_interpret_type(fs
);
909 type
= mnt_fs_get_fstype(fs
);
911 if (type
&& strcmp(type
, "auto") != 0)
913 else if (fstype
&& strncmp(fstype
, "no", 2) != 0 &&
914 strncmp(fstype
, "opts=", 5) != 0 && strncmp(fstype
, "loop", 4) != 0 &&
915 !strchr(fstype
, ','))
918 type
= DEFAULT_FSTYPE
;
920 xasprintf(&progname
, "fsck.%s", type
);
922 if (!find_fsck(progname
, &progpath
)) {
924 if (fs_check_required(type
)) {
932 retval
= execute(progname
, progpath
, type
, fs
, interactive
);
941 warnx(_("error %d (%s) while executing fsck.%s for %s"),
942 retval
, strerror(errno
), type
, fs_get_device(fs
));
943 return FSCK_EX_ERROR
;
948 * Deal with the fsck -t argument.
950 static struct fs_type_compile
{
956 #define FS_TYPE_NORMAL 0
957 #define FS_TYPE_OPT 1
958 #define FS_TYPE_NEGOPT 2
960 static void compile_fs_type(char *fs_type
, struct fs_type_compile
*cmp
)
964 int negate
, first_negate
= 1;
967 for (cp
=fs_type
; *cp
; cp
++) {
973 cmp
->list
= xcalloc(num
, sizeof(char *));
974 cmp
->type
= xcalloc(num
, sizeof(int));
980 list
= xstrdup(fs_type
);
982 s
= strtok(list
, ",");
985 if (strncmp(s
, "no", 2) == 0) {
988 } else if (*s
== '!') {
992 if (strcmp(s
, "loop") == 0)
993 /* loop is really short-hand for opts=loop */
994 goto loop_special_case
;
995 else if (strncmp(s
, "opts=", 5) == 0) {
998 cmp
->type
[num
] = negate
? FS_TYPE_NEGOPT
: FS_TYPE_OPT
;
1001 cmp
->negate
= negate
;
1004 if ((negate
&& !cmp
->negate
) ||
1005 (!negate
&& cmp
->negate
)) {
1007 _("Either all or none of the filesystem types passed to -t must be prefixed\n"
1008 "with 'no' or '!'."));
1012 cmp
->list
[num
++] = xstrdup(s
);
1013 s
= strtok(NULL
, ",");
1019 * This function returns true if a particular option appears in a
1020 * comma-delimited options list
1022 static int opt_in_list(const char *opt
, const char *optlist
)
1028 list
= xstrdup(optlist
);
1030 s
= strtok(list
, ",");
1032 if (strcmp(s
, opt
) == 0) {
1036 s
= strtok(NULL
, ",");
1042 /* See if the filesystem matches the criteria given by the -t option */
1043 static int fs_match(struct libmnt_fs
*fs
, struct fs_type_compile
*cmp
)
1045 int n
, ret
= 0, checked_type
= 0;
1048 if (cmp
->list
== NULL
|| cmp
->list
[0] == NULL
)
1051 for (n
=0; (cp
= cmp
->list
[n
]); n
++) {
1052 switch (cmp
->type
[n
]) {
1053 case FS_TYPE_NORMAL
:
1055 const char *type
= mnt_fs_get_fstype(fs
);
1058 if (type
&& strcmp(cp
, type
) == 0)
1062 case FS_TYPE_NEGOPT
:
1063 if (opt_in_list(cp
, mnt_fs_get_options(fs
)))
1067 if (!opt_in_list(cp
, mnt_fs_get_options(fs
)))
1072 if (checked_type
== 0)
1074 return (cmp
->negate
? !ret
: ret
);
1078 * Check if a device exists
1080 static int device_exists(const char *device
)
1084 if (stat(device
, &st
) == -1)
1086 if (!S_ISBLK(st
.st_mode
))
1091 static int fs_ignored_type(struct libmnt_fs
*fs
)
1093 const char **ip
, *type
;
1095 if (!mnt_fs_is_regularfs(fs
))
1098 type
= mnt_fs_get_fstype(fs
);
1100 for(ip
= ignored_types
; type
&& *ip
; ip
++) {
1101 if (strcmp(type
, *ip
) == 0)
1108 /* Check if we should ignore this filesystem. */
1109 static int ignore(struct libmnt_fs
*fs
)
1114 * If the pass number is 0, ignore it.
1116 if (mnt_fs_get_passno(fs
) == 0)
1120 * If this is a bind mount, ignore it.
1122 if (opt_in_list("bind", mnt_fs_get_options(fs
))) {
1123 warnx(_("%s: skipping bad line in /etc/fstab: "
1124 "bind mount with nonzero fsck pass number"),
1125 mnt_fs_get_target(fs
));
1130 * ignore devices that don't exist and have the "nofail" mount option
1132 if (!device_exists(fs_get_device(fs
))) {
1133 if (opt_in_list("nofail", mnt_fs_get_options(fs
))) {
1135 printf(_("%s: skipping nonexistent device\n"),
1140 printf(_("%s: nonexistent device (\"nofail\" fstab "
1141 "option may be used to skip this device)\n"),
1145 fs_interpret_type(fs
);
1148 * If a specific fstype is specified, and it doesn't match,
1151 if (!fs_match(fs
, &fs_type_compiled
))
1154 type
= mnt_fs_get_fstype(fs
);
1157 printf(_("%s: skipping unknown filesystem type\n"),
1162 /* Are we ignoring this type? */
1163 if (fs_ignored_type(fs
))
1168 /* See if the <fsck.fs> program is available. */
1169 if (!find_fsck(type
, NULL
)) {
1170 if (fs_check_required(type
))
1171 warnx(_("cannot check %s: fsck.%s not found"),
1172 fs_get_device(fs
), type
);
1176 /* We can and want to check this file system type. */
1180 static int count_slaves(dev_t disk
)
1184 char dirname
[PATH_MAX
];
1187 snprintf(dirname
, sizeof(dirname
),
1188 "/sys/dev/block/%u:%u/slaves/",
1189 major(disk
), minor(disk
));
1191 if (!(dir
= opendir(dirname
)))
1194 while ((dp
= readdir(dir
)) != NULL
) {
1195 #ifdef _DIRENT_HAVE_D_TYPE
1196 if (dp
->d_type
!= DT_UNKNOWN
&& dp
->d_type
!= DT_LNK
)
1199 if (dp
->d_name
[0] == '.' &&
1200 ((dp
->d_name
[1] == 0) ||
1201 ((dp
->d_name
[1] == '.') && (dp
->d_name
[2] == 0))))
1212 * Returns TRUE if a partition on the same disk is already being
1215 static int disk_already_active(struct libmnt_fs
*fs
)
1217 struct fsck_instance
*inst
;
1220 if (force_all_parallel
)
1223 if (instance_list
&& fs_is_stacked(instance_list
->fs
))
1224 /* any instance for a stacked device is already running */
1227 disk
= fs_get_disk(fs
, 1);
1230 * If we don't know the base device, assume that the device is
1231 * already active if there are any fsck instances running.
1233 * Don't check a stacked device with any other disk too.
1235 if (!disk
|| fs_is_stacked(fs
))
1236 return (instance_list
!= NULL
);
1238 for (inst
= instance_list
; inst
; inst
= inst
->next
) {
1239 dev_t idisk
= fs_get_disk(inst
->fs
, 0);
1241 if (!idisk
|| disk
== idisk
)
1248 /* Check all file systems, using the /etc/fstab table. */
1249 static int check_all(void)
1251 int not_done_yet
= 1;
1254 int status
= FSCK_EX_OK
;
1256 struct libmnt_fs
*fs
;
1257 struct libmnt_iter
*itr
= mnt_new_iter(MNT_ITER_FORWARD
);
1260 err(FSCK_EX_ERROR
, _("failed to allocate iterator"));
1263 * Do an initial scan over the filesystem; mark filesystems
1264 * which should be ignored as done, and resolve any "auto"
1265 * filesystem types (done as a side-effect of calling ignore()).
1267 while (mnt_table_next_fs(fstab
, itr
, &fs
) == 0) {
1275 fputs(_("Checking all file systems.\n"), stdout
);
1278 * Find and check the root filesystem.
1280 if (!parallel_root
) {
1281 fs
= mnt_table_find_target(fstab
, "/", MNT_ITER_FORWARD
);
1285 !(ignore_mounted
&& is_mounted(fs
))) {
1286 status
|= fsck_device(fs
, 1);
1287 status
|= wait_many(FLAG_WAIT_ALL
);
1288 if (status
> FSCK_EX_NONDESTRUCT
) {
1298 * This is for the bone-headed user who enters the root
1299 * filesystem twice. Skip root will skip all root entries.
1302 mnt_reset_iter(itr
, MNT_ITER_FORWARD
);
1304 while(mnt_table_next_fs(fstab
, itr
, &fs
) == 0) {
1305 const char *tgt
= mnt_fs_get_target(fs
);
1307 if (tgt
&& strcmp(tgt
, "/") == 0)
1312 while (not_done_yet
) {
1316 mnt_reset_iter(itr
, MNT_ITER_FORWARD
);
1318 while(mnt_table_next_fs(fstab
, itr
, &fs
) == 0) {
1320 if (cancel_requested
)
1325 * If the filesystem's pass number is higher
1326 * than the current pass number, then we don't
1329 if (mnt_fs_get_passno(fs
) > passno
) {
1333 if (ignore_mounted
&& is_mounted(fs
)) {
1338 * If a filesystem on a particular device has
1339 * already been spawned, then we need to defer
1340 * this to another pass.
1342 if (disk_already_active(fs
)) {
1347 * Spawn off the fsck process
1349 status
|= fsck_device(fs
, serialize
);
1353 * Only do one filesystem at a time, or if we
1354 * have a limit on the number of fsck's extant
1355 * at one time, apply that limit.
1358 (max_running
&& (num_running
>= max_running
))) {
1363 if (cancel_requested
)
1366 printf(_("--waiting-- (pass %d)\n"), passno
);
1368 status
|= wait_many(pass_done
? FLAG_WAIT_ALL
:
1369 FLAG_WAIT_ATLEAST_ONE
);
1372 printf("----------------------------------\n");
1378 if (cancel_requested
&& !kill_sent
) {
1383 status
|= wait_many(FLAG_WAIT_ATLEAST_ONE
);
1388 static void __attribute__((__noreturn__
)) usage(void)
1391 fputs(USAGE_HEADER
, out
);
1392 fprintf(out
, _(" %s [options] -- [fs-options] [<filesystem> ...]\n"),
1393 program_invocation_short_name
);
1395 fputs(USAGE_SEPARATOR
, out
);
1396 fputs(_("Check and repair a Linux filesystem.\n"), out
);
1398 fputs(USAGE_OPTIONS
, out
);
1399 fputs(_(" -A check all filesystems\n"), out
);
1400 fputs(_(" -C [<fd>] display progress bar; file descriptor is for GUIs\n"), out
);
1401 fputs(_(" -l lock the device to guarantee exclusive access\n"), out
);
1402 fputs(_(" -M do not check mounted filesystems\n"), out
);
1403 fputs(_(" -N do not execute, just show what would be done\n"), out
);
1404 fputs(_(" -P check filesystems in parallel, including root\n"), out
);
1405 fputs(_(" -R skip root filesystem; useful only with '-A'\n"), out
);
1406 fputs(_(" -r [<fd>] report statistics for each device checked;\n"
1407 " file descriptor is for GUIs\n"), out
);
1408 fputs(_(" -s serialize the checking operations\n"), out
);
1409 fputs(_(" -T do not show the title on startup\n"), out
);
1410 fputs(_(" -t <type> specify filesystem types to be checked;\n"
1411 " <type> is allowed to be a comma-separated list\n"), out
);
1412 fputs(_(" -V explain what is being done\n"), out
);
1414 fputs(USAGE_SEPARATOR
, out
);
1415 printf( " -?, --help %s\n", USAGE_OPTSTR_HELP
);
1416 printf( " --version %s\n", USAGE_OPTSTR_VERSION
);
1417 fputs(USAGE_SEPARATOR
, out
);
1418 fputs(_("See the specific fsck.* commands for available fs-options."), out
);
1419 printf(USAGE_MAN_TAIL("fsck(8)"));
1423 static void signal_cancel(int sig
__attribute__((__unused__
)))
1428 static void parse_argv(int argc
, char *argv
[])
1431 char *arg
, *dev
, *tmp
= NULL
;
1434 int opts_for_fsck
= 0;
1435 struct sigaction sa
;
1436 int report_stats_fd
= -1;
1439 * Set up signal action
1441 memset(&sa
, 0, sizeof(struct sigaction
));
1442 sa
.sa_handler
= signal_cancel
;
1443 sigaction(SIGINT
, &sa
, NULL
);
1444 sigaction(SIGTERM
, &sa
, NULL
);
1448 instance_list
= NULL
;
1450 for (i
=1; i
< argc
; i
++) {
1455 /* the only two longopts to satisfy UL standards */
1456 if (!opts_for_fsck
&& !strcmp(arg
, "--help"))
1458 if (!opts_for_fsck
&& !strcmp(arg
, "--version"))
1459 print_version(FSCK_EX_OK
);
1461 if ((arg
[0] == '/' && !opts_for_fsck
) || strchr(arg
, '=')) {
1462 if (num_devices
>= MAX_DEVICES
)
1463 errx(FSCK_EX_ERROR
, _("too many devices"));
1465 dev
= mnt_resolve_spec(arg
, mntcache
);
1467 if (!dev
&& strchr(arg
, '=')) {
1469 * Check to see if we failed because
1470 * /proc/partitions isn't found.
1472 if (access(_PATH_PROC_PARTITIONS
, R_OK
) < 0) {
1473 warn(_("cannot open %s"),
1474 _PATH_PROC_PARTITIONS
);
1475 errx(FSCK_EX_ERROR
, _("Is /proc mounted?"));
1478 * Check to see if this is because
1479 * we're not running as root
1483 _("must be root to scan for matching filesystems: %s"),
1487 _("couldn't find matching filesystem: %s"),
1490 devices
[num_devices
++] = dev
? dev
: xstrdup(arg
);
1493 if (arg
[0] != '-' || opts_for_fsck
) {
1494 if (num_args
>= MAX_ARGS
)
1495 errx(FSCK_EX_ERROR
, _("too many arguments"));
1496 args
[num_args
++] = xstrdup(arg
);
1499 for (j
=1; arg
[j
]; j
++) {
1500 if (opts_for_fsck
) {
1501 options
[++opt
] = arg
[j
];
1510 if (arg
[j
+1]) { /* -C<fd> */
1511 progress_fd
= string_to_int(arg
+j
+1);
1512 if (progress_fd
< 0)
1516 } else if (i
+1 < argc
&& *argv
[i
+1] != '-') { /* -C <fd> */
1517 progress_fd
= string_to_int(argv
[i
+1]);
1518 if (progress_fd
< 0)
1549 if (arg
[j
+1]) { /* -r<fd> */
1550 report_stats_fd
= strtou32_or_err(arg
+j
+1, _("invalid argument of -r"));
1552 } else if (i
+1 < argc
&& *argv
[i
+1] >= '0' && *argv
[i
+1] <= '9') { /* -r <fd> */
1553 report_stats_fd
= strtou32_or_err(argv
[i
+1], _("invalid argument of -r"));
1565 _("option '%s' may be specified only once"), "-t");
1568 else if ((i
+1) < argc
)
1572 _("option '%s' requires an argument"), "-t");
1573 fstype
= xstrdup(tmp
);
1574 compile_fs_type(fstype
, &fs_type_compiled
);
1583 options
[++opt
] = arg
[j
];
1590 options
[++opt
] = '\0';
1591 if (num_args
>= MAX_ARGS
)
1592 errx(FSCK_EX_ERROR
, _("too many arguments"));
1593 args
[num_args
++] = xstrdup(options
);
1598 /* Validate the report stats file descriptor to avoid disasters */
1599 if (report_stats_fd
>= 0) {
1600 report_stats_file
= fdopen(report_stats_fd
, "w");
1601 if (!report_stats_file
)
1603 _("invalid argument of -r: %d"),
1607 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1608 force_all_parallel
++;
1609 if (ul_strtos32(getenv("FSCK_MAX_INST"), &max_running
, 10) != 0)
1613 int main(int argc
, char *argv
[])
1616 int interactive
= 0;
1617 struct libmnt_fs
*fs
;
1618 const char *path
= getenv("PATH");
1620 setvbuf(stdout
, NULL
, _IONBF
, BUFSIZ
);
1621 setvbuf(stderr
, NULL
, _IONBF
, BUFSIZ
);
1623 setlocale(LC_MESSAGES
, "");
1624 setlocale(LC_CTYPE
, "");
1625 bindtextdomain(PACKAGE
, LOCALEDIR
);
1626 textdomain(PACKAGE
);
1627 close_stdout_atexit();
1629 strutils_set_exitcode(FSCK_EX_USAGE
);
1630 mnt_init_debug(0); /* init libmount debug mask */
1631 mntcache
= mnt_new_cache(); /* no fatal error if failed */
1633 parse_argv(argc
, argv
);
1636 printf(UTIL_LINUX_VERSION
);
1638 signal(SIGCHLD
, SIG_DFL
); /* clear any inherited settings */
1642 fsck_path
= xstrdup(path
&& *path
? path
: FSCK_DEFAULT_PATH
);
1644 if ((num_devices
== 1) || (serialize
))
1647 if (lockdisk
&& (doall
|| num_devices
> 1)) {
1648 warnx(_("the -l option can be used with one "
1649 "device only -- ignore"));
1653 /* If -A was specified ("check all"), do that! */
1657 if (num_devices
== 0) {
1662 for (i
= 0 ; i
< num_devices
; i
++) {
1663 if (cancel_requested
) {
1670 fs
= lookup(devices
[i
]);
1672 fs
= add_dummy_fs(devices
[i
]);
1673 else if (fs_ignored_type(fs
))
1675 if (ignore_mounted
&& is_mounted(fs
))
1677 status
|= fsck_device(fs
, interactive
);
1679 (max_running
&& (num_running
>= max_running
))) {
1680 struct fsck_instance
*inst
;
1684 status
|= inst
->exit_status
;
1685 free_instance(inst
);
1688 printf("----------------------------------\n");
1691 status
|= wait_many(FLAG_WAIT_ALL
);
1693 mnt_unref_cache(mntcache
);
1694 mnt_unref_table(fstab
);
1695 mnt_unref_table(mtab
);