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"
58 #define STRTOXX_EXIT_CODE FSCK_EX_ERROR
61 #define XALLOC_EXIT_CODE FSCK_EX_ERROR
64 #define CLOSE_EXIT_CODE FSCK_EX_ERROR
65 #include "closestream.h"
67 #ifndef DEFAULT_FSTYPE
68 # define DEFAULT_FSTYPE "ext2"
71 #define MAX_DEVICES 32
74 #define FSCK_RUNTIME_DIRNAME "/run/fsck"
76 static const char *ignored_types
[] = {
83 static const char *really_wanted
[] = {
94 * Internal structure for mount table entries.
100 unsigned int stacked
:1,
106 * Structure to allow exit codes to be stored
108 struct fsck_instance
{
110 int flags
; /* FLAG_{DONE|PROGRESS} */
112 int lock
; /* flock()ed lockpath file descriptor or -1 */
113 char *lockpath
; /* /run/fsck/<diskname>.lock or NULL */
116 struct timeval start_time
;
117 struct timeval end_time
;
121 struct rusage rusage
;
122 struct libmnt_fs
*fs
;
123 struct fsck_instance
*next
;
127 #define FLAG_PROGRESS 2
130 * Global variables for options
132 static char *devices
[MAX_DEVICES
];
133 static char *args
[MAX_ARGS
];
134 static int num_devices
, num_args
;
139 static int noexecute
;
140 static int serialize
;
141 static int skip_root
;
142 static int ignore_mounted
;
144 static int parallel_root
;
146 static int progress_fd
;
147 static int force_all_parallel
;
148 static int report_stats
;
149 static FILE *report_stats_file
;
151 static int num_running
;
152 static int max_running
;
154 static volatile int cancel_requested
;
155 static int kill_sent
;
157 static struct fsck_instance
*instance_list
;
159 #define FSCK_DEFAULT_PATH "/sbin"
160 static char *fsck_path
;
163 /* parsed fstab and mtab */
164 static struct libmnt_table
*fstab
, *mtab
;
165 static struct libmnt_cache
*mntcache
;
167 static int count_slaves(dev_t disk
);
169 static int string_to_int(const char *s
)
174 l
= strtol(s
, &p
, 0);
175 if (*p
|| l
== LONG_MIN
|| l
== LONG_MAX
|| l
< 0 || l
> INT_MAX
)
181 /* Do we really really want to check this fs? */
182 static int fs_check_required(const char *type
)
186 for(i
= 0; i
< ARRAY_SIZE(really_wanted
); i
++) {
187 if (strcmp(type
, really_wanted
[i
]) == 0)
194 static int is_mounted(struct libmnt_fs
*fs
)
199 src
= mnt_fs_get_source(fs
);
203 mntcache
= mnt_new_cache();
205 mtab
= mnt_new_table();
207 err(FSCK_EX_ERROR
, ("failed to initialize libmount table"));
208 mnt_table_set_cache(mtab
, mntcache
);
209 mnt_table_parse_mtab(mtab
, NULL
);
212 rc
= mnt_table_find_source(mtab
, src
, MNT_ITER_BACKWARD
) ? 1 : 0;
215 printf(_("%s is mounted\n"), src
);
217 printf(_("%s is not mounted\n"), src
);
222 static int ignore(struct libmnt_fs
*);
224 static struct fsck_fs_data
*fs_create_data(struct libmnt_fs
*fs
)
226 struct fsck_fs_data
*data
= mnt_fs_get_userdata(fs
);
229 data
= xcalloc(1, sizeof(*data
));
230 mnt_fs_set_userdata(fs
, data
);
236 * fs from fstab might contains real device name as well as symlink,
237 * LABEL or UUID, this function returns canonicalized result.
239 static const char *fs_get_device(struct libmnt_fs
*fs
)
241 struct fsck_fs_data
*data
= mnt_fs_get_userdata(fs
);
243 if (!data
|| !data
->eval_device
) {
244 const char *spec
= mnt_fs_get_source(fs
);
247 data
= fs_create_data(fs
);
249 data
->eval_device
= 1;
250 data
->device
= mnt_resolve_spec(spec
, mnt_table_get_cache(fstab
));
252 data
->device
= xstrdup(spec
);
258 static dev_t
fs_get_disk(struct libmnt_fs
*fs
, int check
)
260 struct fsck_fs_data
*data
;
264 data
= mnt_fs_get_userdata(fs
);
265 if (data
&& data
->disk
)
271 if (mnt_fs_is_netfs(fs
) || mnt_fs_is_pseudofs(fs
))
274 device
= fs_get_device(fs
);
278 data
= fs_create_data(fs
);
280 if (!stat(device
, &st
) &&
281 !blkid_devno_to_wholedisk(st
.st_rdev
, NULL
, 0, &data
->disk
)) {
284 data
->stacked
= count_slaves(data
->disk
) > 0 ? 1 : 0;
290 static int fs_is_stacked(struct libmnt_fs
*fs
)
292 struct fsck_fs_data
*data
= mnt_fs_get_userdata(fs
);
293 return data
? data
->stacked
: 0;
296 static int fs_is_done(struct libmnt_fs
*fs
)
298 struct fsck_fs_data
*data
= mnt_fs_get_userdata(fs
);
299 return data
? data
->done
: 0;
302 static void fs_set_done(struct libmnt_fs
*fs
)
304 struct fsck_fs_data
*data
= fs_create_data(fs
);
310 static int is_irrotational_disk(dev_t disk
)
317 rc
= snprintf(path
, sizeof(path
),
318 "/sys/dev/block/%d:%d/queue/rotational",
319 major(disk
), minor(disk
));
321 if (rc
< 0 || (unsigned int) rc
>= sizeof(path
))
324 f
= fopen(path
, "r");
328 rc
= fscanf(f
, "%d", &x
);
331 warn(_("cannot read %s"), path
);
333 warnx(_("parse error: %s"), path
);
337 return rc
== 1 ? !x
: 0;
340 static void lock_disk(struct fsck_instance
*inst
)
342 dev_t disk
= fs_get_disk(inst
->fs
, 1);
343 char *diskpath
= NULL
, *diskname
;
347 if (!disk
|| is_irrotational_disk(disk
))
350 diskpath
= blkid_devno_to_devname(disk
);
354 if (access(FSCK_RUNTIME_DIRNAME
, F_OK
) != 0) {
355 int rc
= mkdir(FSCK_RUNTIME_DIRNAME
,
357 S_IRUSR
|S_IRGRP
|S_IROTH
|
358 S_IXUSR
|S_IXGRP
|S_IXOTH
);
359 if (rc
&& errno
!= EEXIST
) {
360 warn(_("cannot create directory %s"),
361 FSCK_RUNTIME_DIRNAME
);
366 diskname
= stripoff_last_component(diskpath
);
370 xasprintf(&inst
->lockpath
, FSCK_RUNTIME_DIRNAME
"/%s.lock", diskname
);
373 printf(_("Locking disk by %s ... "), inst
->lockpath
);
375 inst
->lock
= open(inst
->lockpath
, O_RDONLY
|O_CREAT
|O_CLOEXEC
,
376 S_IWUSR
|S_IRUSR
|S_IRGRP
|S_IROTH
);
377 if (inst
->lock
>= 0) {
380 /* inform users that we're waiting on the lock */
382 (rc
= flock(inst
->lock
, LOCK_EX
| LOCK_NB
)) != 0 &&
383 errno
== EWOULDBLOCK
)
384 printf(_("(waiting) "));
386 if (rc
!= 0 && flock(inst
->lock
, LOCK_EX
) != 0) {
387 close(inst
->lock
); /* failed */
393 /* TRANSLATORS: These are followups to "Locking disk...". */
394 printf("%s.\n", inst
->lock
>= 0 ? _("succeeded") : _("failed"));
398 if (inst
->lock
< 0) {
399 free(inst
->lockpath
);
400 inst
->lockpath
= NULL
;
406 static void unlock_disk(struct fsck_instance
*inst
)
412 printf(_("Unlocking %s.\n"), inst
->lockpath
);
414 close(inst
->lock
); /* unlock */
416 free(inst
->lockpath
);
419 inst
->lockpath
= NULL
;
422 static void free_instance(struct fsck_instance
*i
)
433 static struct libmnt_fs
*add_dummy_fs(const char *device
)
435 struct libmnt_fs
*fs
= mnt_new_fs();
437 if (fs
&& mnt_fs_set_source(fs
, device
) == 0 &&
438 mnt_table_add_fs(fstab
, fs
) == 0) {
444 err(FSCK_EX_ERROR
, _("failed to setup description for %s"), device
);
447 static void fs_interpret_type(struct libmnt_fs
*fs
)
450 const char *type
= mnt_fs_get_fstype(fs
);
452 if (type
&& strcmp(type
, "auto") != 0)
455 mnt_fs_set_fstype(fs
, NULL
);
457 device
= fs_get_device(fs
);
461 struct libmnt_cache
*cache
= mnt_table_get_cache(fstab
);
463 tp
= mnt_get_fstype(device
, &ambi
, cache
);
465 mnt_fs_set_fstype(fs
, tp
);
471 static int parser_errcb(struct libmnt_table
*tb
__attribute__ ((__unused__
)),
472 const char *filename
, int line
)
474 warnx(_("%s: parse error at line %d -- ignored"), filename
, line
);
479 * Load the filesystem database from /etc/fstab
481 static void load_fs_info(void)
485 fstab
= mnt_new_table();
487 err(FSCK_EX_ERROR
, ("failed to initialize libmount table"));
489 mnt_table_set_parser_errcb(fstab
, parser_errcb
);
490 mnt_table_set_cache(fstab
, mntcache
);
495 * Let's follow libmount defaults if $FSTAB_FILE is not specified
497 path
= getenv("FSTAB_FILE");
499 if (mnt_table_parse_fstab(fstab
, path
)) {
501 path
= mnt_get_fstab_path();
503 /* don't print error when there is no fstab at all */
504 if (access(path
, F_OK
) == 0) {
506 warn(_("%s: failed to parse fstab"), path
);
508 warnx(_("%s: failed to parse fstab"), path
);
514 * Lookup filesys in /etc/fstab and return the corresponding entry.
515 * The @path has to be real path (no TAG) by mnt_resolve_spec().
517 static struct libmnt_fs
*lookup(char *path
)
519 struct libmnt_fs
*fs
;
524 fs
= mnt_table_find_srcpath(fstab
, path
, MNT_ITER_FORWARD
);
527 * Maybe mountpoint has been specified on fsck command line.
528 * Yeah, crazy feature...
530 * Note that mnt_table_find_target() may canonicalize paths in
531 * the fstab to support symlinks. This is really unwanted,
532 * because readlink() on mountpoints may trigger automounts.
534 * So, disable the cache and compare the paths as strings
535 * without care about symlinks...
537 mnt_table_set_cache(fstab
, NULL
);
538 fs
= mnt_table_find_target(fstab
, path
, MNT_ITER_FORWARD
);
539 mnt_table_set_cache(fstab
, mntcache
);
544 /* Find fsck program for a given fs type. */
545 static char *find_fsck(const char *type
)
549 static char prog
[256];
550 char *p
= xstrdup(fsck_path
);
552 /* Are we looking for a program or just a type? */
553 tpl
= (strncmp(type
, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
555 for(s
= strtok(p
, ":"); s
; s
= strtok(NULL
, ":")) {
556 sprintf(prog
, tpl
, s
, type
);
557 if (access(prog
, X_OK
) == 0)
562 return(s
? prog
: NULL
);
565 static int progress_active(void)
567 struct fsck_instance
*inst
;
569 for (inst
= instance_list
; inst
; inst
= inst
->next
) {
570 if (inst
->flags
& FLAG_DONE
)
572 if (inst
->flags
& FLAG_PROGRESS
)
579 * Process run statistics for finished fsck instances.
581 * If report_stats is 0, do nothing, otherwise print a selection of
582 * interesting rusage statistics as well as elapsed wallclock time.
584 static void print_stats(struct fsck_instance
*inst
)
586 struct timeval delta
;
588 if (!inst
|| !report_stats
|| noexecute
)
591 timersub(&inst
->end_time
, &inst
->start_time
, &delta
);
593 if (report_stats_file
)
594 fprintf(report_stats_file
, "%s %d %ld "
595 "%ld.%06ld %ld.%06ld %ld.%06ld\n",
596 fs_get_device(inst
->fs
),
598 inst
->rusage
.ru_maxrss
,
599 (long)delta
.tv_sec
, (long)delta
.tv_usec
,
600 (long)inst
->rusage
.ru_utime
.tv_sec
,
601 (long)inst
->rusage
.ru_utime
.tv_usec
,
602 (long)inst
->rusage
.ru_stime
.tv_sec
,
603 (long)inst
->rusage
.ru_stime
.tv_usec
);
605 fprintf(stdout
, "%s: status %d, rss %ld, "
606 "real %ld.%06ld, user %ld.%06ld, sys %ld.%06ld\n",
607 fs_get_device(inst
->fs
),
609 inst
->rusage
.ru_maxrss
,
610 (long)delta
.tv_sec
, (long)delta
.tv_usec
,
611 (long)inst
->rusage
.ru_utime
.tv_sec
,
612 (long)inst
->rusage
.ru_utime
.tv_usec
,
613 (long)inst
->rusage
.ru_stime
.tv_sec
,
614 (long)inst
->rusage
.ru_stime
.tv_usec
);
618 * Execute a particular fsck program, and link it into the list of
619 * child processes we are waiting for.
621 static int execute(const char *progname
, const char *progpath
,
622 const char *type
, struct libmnt_fs
*fs
, int interactive
)
626 struct fsck_instance
*inst
, *p
;
629 inst
= xcalloc(1, sizeof(*inst
));
631 argv
[0] = xstrdup(progname
);
634 for (i
=0; i
<num_args
; i
++)
635 argv
[argc
++] = xstrdup(args
[i
]);
638 ((strcmp(type
, "ext2") == 0) ||
639 (strcmp(type
, "ext3") == 0) ||
640 (strcmp(type
, "ext4") == 0) ||
641 (strcmp(type
, "ext4dev") == 0))) {
645 if (!progress_active()) {
646 snprintf(tmp
, 80, "-C%d", progress_fd
);
647 inst
->flags
|= FLAG_PROGRESS
;
648 } else if (progress_fd
)
649 snprintf(tmp
, 80, "-C%d", progress_fd
* -1);
651 argv
[argc
++] = xstrdup(tmp
);
654 argv
[argc
++] = xstrdup(fs_get_device(fs
));
657 if (verbose
|| noexecute
) {
658 const char *tgt
= mnt_fs_get_target(fs
);
661 tgt
= fs_get_device(fs
);
662 printf("[%s (%d) -- %s] ", progpath
, num_running
, tgt
);
663 for (i
=0; i
< argc
; i
++)
664 printf("%s ", argv
[i
]);
675 /* Fork and execute the correct program. */
678 else if ((pid
= fork()) < 0) {
679 warn(_("fork failed"));
682 } else if (pid
== 0) {
685 execv(progpath
, argv
);
686 err(FSCK_EX_ERROR
, _("%s: execute failed"), progpath
);
689 for (i
=0; i
< argc
; i
++)
693 inst
->prog
= xstrdup(progname
);
694 inst
->type
= xstrdup(type
);
695 gettime_monotonic(&inst
->start_time
);
699 * Find the end of the list, so we add the instance on at the end.
701 for (p
= instance_list
; p
&& p
->next
; p
= p
->next
);
706 instance_list
= inst
;
712 * Send a signal to all outstanding fsck child processes
714 static int kill_all(int signum
)
716 struct fsck_instance
*inst
;
719 for (inst
= instance_list
; inst
; inst
= inst
->next
) {
720 if (inst
->flags
& FLAG_DONE
)
722 kill(inst
->pid
, signum
);
729 * Wait for one child process to exit; when it does, unlink it from
730 * the list of executing child processes, and return it.
732 static struct fsck_instance
*wait_one(int flags
)
736 struct fsck_instance
*inst
, *inst2
, *prev
;
738 struct rusage rusage
;
744 inst
= instance_list
;
747 while (inst
->next
&& (random() & 1)) {
752 inst
->exit_status
= 0;
757 * gcc -Wall fails saving throw against stupidity
758 * (inst and prev are thought to be uninitialized variables)
763 pid
= wait4(-1, &status
, flags
, &rusage
);
764 if (cancel_requested
&& !kill_sent
) {
768 if ((pid
== 0) && (flags
& WNOHANG
))
771 if ((errno
== EINTR
) || (errno
== EAGAIN
))
773 if (errno
== ECHILD
) {
774 warnx(_("wait: no more child process?!?"));
777 warn(_("waitpid failed"));
780 for (prev
= NULL
, inst
= instance_list
;
782 prev
= inst
, inst
= inst
->next
) {
783 if (inst
->pid
== pid
)
788 if (WIFEXITED(status
))
789 status
= WEXITSTATUS(status
);
790 else if (WIFSIGNALED(status
)) {
791 sig
= WTERMSIG(status
);
793 status
= FSCK_EX_UNCORRECTED
;
795 warnx(_("Warning... %s for device %s exited "
797 inst
->prog
, fs_get_device(inst
->fs
), sig
);
798 status
= FSCK_EX_ERROR
;
801 warnx(_("%s %s: status is %x, should never happen."),
802 inst
->prog
, fs_get_device(inst
->fs
), status
);
803 status
= FSCK_EX_ERROR
;
806 inst
->exit_status
= status
;
807 inst
->flags
|= FLAG_DONE
;
808 gettime_monotonic(&inst
->end_time
);
809 memcpy(&inst
->rusage
, &rusage
, sizeof(struct rusage
));
811 if (progress
&& (inst
->flags
& FLAG_PROGRESS
) &&
812 !progress_active()) {
813 for (inst2
= instance_list
; inst2
; inst2
= inst2
->next
) {
814 if (inst2
->flags
& FLAG_DONE
)
816 if (strcmp(inst2
->type
, "ext2") &&
817 strcmp(inst2
->type
, "ext3") &&
818 strcmp(inst2
->type
, "ext4") &&
819 strcmp(inst2
->type
, "ext4dev"))
822 * If we've just started the fsck, wait a tiny
823 * bit before sending the kill, to give it
824 * time to set up the signal handler
826 if (inst2
->start_time
.tv_sec
< time(NULL
) + 2) {
829 kill(inst2
->pid
, SIGUSR1
);
833 kill(inst2
->pid
, SIGUSR1
);
834 inst2
->flags
|= FLAG_PROGRESS
;
840 prev
->next
= inst
->next
;
842 instance_list
= inst
->next
;
847 printf(_("Finished with %s (exit status %d)\n"),
848 fs_get_device(inst
->fs
), inst
->exit_status
);
853 #define FLAG_WAIT_ALL 0
854 #define FLAG_WAIT_ATLEAST_ONE 1
856 * Wait until all executing child processes have exited; return the
857 * logical OR of all of their exit code values.
859 static int wait_many(int flags
)
861 struct fsck_instance
*inst
;
862 int global_status
= 0;
865 while ((inst
= wait_one(wait_flags
))) {
866 global_status
|= inst
->exit_status
;
869 if (noexecute
&& (flags
& WNOHANG
) && !(random() % 3))
872 if (flags
& FLAG_WAIT_ATLEAST_ONE
)
873 wait_flags
= WNOHANG
;
875 return global_status
;
879 * Run the fsck program on a particular device
881 * If the type is specified using -t, and it isn't prefixed with "no"
882 * (as in "noext2") and only one filesystem type is specified, then
883 * use that type regardless of what is specified in /etc/fstab.
885 * If the type isn't specified by the user, then use either the type
886 * specified in /etc/fstab, or DEFAULT_FSTYPE.
888 static int fsck_device(struct libmnt_fs
*fs
, int interactive
)
890 char progname
[80], *progpath
;
894 fs_interpret_type(fs
);
896 type
= mnt_fs_get_fstype(fs
);
898 if (type
&& strcmp(type
, "auto") != 0)
900 else if (fstype
&& strncmp(fstype
, "no", 2) &&
901 strncmp(fstype
, "opts=", 5) && strncmp(fstype
, "loop", 4) &&
902 !strchr(fstype
, ','))
905 type
= DEFAULT_FSTYPE
;
907 sprintf(progname
, "fsck.%s", type
);
908 progpath
= find_fsck(progname
);
909 if (progpath
== NULL
) {
910 if (fs_check_required(type
)) {
918 retval
= execute(progname
, progpath
, type
, fs
, interactive
);
925 warnx(_("error %d (%m) while executing fsck.%s for %s"),
926 retval
, type
, fs_get_device(fs
));
927 return FSCK_EX_ERROR
;
932 * Deal with the fsck -t argument.
934 static struct fs_type_compile
{
940 #define FS_TYPE_NORMAL 0
941 #define FS_TYPE_OPT 1
942 #define FS_TYPE_NEGOPT 2
944 static void compile_fs_type(char *fs_type
, struct fs_type_compile
*cmp
)
948 int negate
, first_negate
= 1;
951 for (cp
=fs_type
; *cp
; cp
++) {
957 cmp
->list
= xcalloc(num
, sizeof(char *));
958 cmp
->type
= xcalloc(num
, sizeof(int));
964 list
= xstrdup(fs_type
);
966 s
= strtok(list
, ",");
969 if (strncmp(s
, "no", 2) == 0) {
972 } else if (*s
== '!') {
976 if (strcmp(s
, "loop") == 0)
977 /* loop is really short-hand for opts=loop */
978 goto loop_special_case
;
979 else if (strncmp(s
, "opts=", 5) == 0) {
982 cmp
->type
[num
] = negate
? FS_TYPE_NEGOPT
: FS_TYPE_OPT
;
985 cmp
->negate
= negate
;
988 if ((negate
&& !cmp
->negate
) ||
989 (!negate
&& cmp
->negate
)) {
991 _("Either all or none of the filesystem types passed to -t must be prefixed\n"
992 "with 'no' or '!'."));
996 cmp
->list
[num
++] = xstrdup(s
);
997 s
= strtok(NULL
, ",");
1003 * This function returns true if a particular option appears in a
1004 * comma-delimited options list
1006 static int opt_in_list(const char *opt
, const char *optlist
)
1012 list
= xstrdup(optlist
);
1014 s
= strtok(list
, ",");
1016 if (strcmp(s
, opt
) == 0) {
1020 s
= strtok(NULL
, ",");
1026 /* See if the filesystem matches the criteria given by the -t option */
1027 static int fs_match(struct libmnt_fs
*fs
, struct fs_type_compile
*cmp
)
1029 int n
, ret
= 0, checked_type
= 0;
1032 if (cmp
->list
== NULL
|| cmp
->list
[0] == NULL
)
1035 for (n
=0; (cp
= cmp
->list
[n
]); n
++) {
1036 switch (cmp
->type
[n
]) {
1037 case FS_TYPE_NORMAL
:
1039 const char *type
= mnt_fs_get_fstype(fs
);
1042 if (type
&& strcmp(cp
, type
) == 0)
1046 case FS_TYPE_NEGOPT
:
1047 if (opt_in_list(cp
, mnt_fs_get_options(fs
)))
1051 if (!opt_in_list(cp
, mnt_fs_get_options(fs
)))
1056 if (checked_type
== 0)
1058 return (cmp
->negate
? !ret
: ret
);
1062 * Check if a device exists
1064 static int device_exists(const char *device
)
1068 if (stat(device
, &st
) == -1)
1070 if (!S_ISBLK(st
.st_mode
))
1075 static int fs_ignored_type(struct libmnt_fs
*fs
)
1077 const char **ip
, *type
;
1079 if (mnt_fs_is_netfs(fs
) || mnt_fs_is_pseudofs(fs
) || mnt_fs_is_swaparea(fs
))
1082 type
= mnt_fs_get_fstype(fs
);
1084 for(ip
= ignored_types
; type
&& *ip
; ip
++) {
1085 if (strcmp(type
, *ip
) == 0)
1092 /* Check if we should ignore this filesystem. */
1093 static int ignore(struct libmnt_fs
*fs
)
1098 * If the pass number is 0, ignore it.
1100 if (mnt_fs_get_passno(fs
) == 0)
1104 * If this is a bind mount, ignore it.
1106 if (opt_in_list("bind", mnt_fs_get_options(fs
))) {
1107 warnx(_("%s: skipping bad line in /etc/fstab: "
1108 "bind mount with nonzero fsck pass number"),
1109 mnt_fs_get_target(fs
));
1114 * ignore devices that don't exist and have the "nofail" mount option
1116 if (!device_exists(fs_get_device(fs
))) {
1117 if (opt_in_list("nofail", mnt_fs_get_options(fs
))) {
1119 printf(_("%s: skipping nonexistent device\n"),
1124 printf(_("%s: nonexistent device (\"nofail\" fstab "
1125 "option may be used to skip this device)\n"),
1129 fs_interpret_type(fs
);
1132 * If a specific fstype is specified, and it doesn't match,
1135 if (!fs_match(fs
, &fs_type_compiled
))
1138 type
= mnt_fs_get_fstype(fs
);
1141 printf(_("%s: skipping unknown filesystem type\n"),
1146 /* Are we ignoring this type? */
1147 if (fs_ignored_type(fs
))
1152 /* See if the <fsck.fs> program is available. */
1153 if (find_fsck(type
) == NULL
) {
1154 if (fs_check_required(type
))
1155 warnx(_("cannot check %s: fsck.%s not found"),
1156 fs_get_device(fs
), type
);
1160 /* We can and want to check this file system type. */
1164 static int count_slaves(dev_t disk
)
1168 char dirname
[PATH_MAX
];
1171 snprintf(dirname
, sizeof(dirname
),
1172 "/sys/dev/block/%u:%u/slaves/",
1173 major(disk
), minor(disk
));
1175 if (!(dir
= opendir(dirname
)))
1178 while ((dp
= readdir(dir
)) != NULL
) {
1179 #ifdef _DIRENT_HAVE_D_TYPE
1180 if (dp
->d_type
!= DT_UNKNOWN
&& dp
->d_type
!= DT_LNK
)
1183 if (dp
->d_name
[0] == '.' &&
1184 ((dp
->d_name
[1] == 0) ||
1185 ((dp
->d_name
[1] == '.') && (dp
->d_name
[2] == 0))))
1196 * Returns TRUE if a partition on the same disk is already being
1199 static int disk_already_active(struct libmnt_fs
*fs
)
1201 struct fsck_instance
*inst
;
1204 if (force_all_parallel
)
1207 if (instance_list
&& fs_is_stacked(instance_list
->fs
))
1208 /* any instance for a stacked device is already running */
1211 disk
= fs_get_disk(fs
, 1);
1214 * If we don't know the base device, assume that the device is
1215 * already active if there are any fsck instances running.
1217 * Don't check a stacked device with any other disk too.
1219 if (!disk
|| fs_is_stacked(fs
))
1220 return (instance_list
!= NULL
);
1222 for (inst
= instance_list
; inst
; inst
= inst
->next
) {
1223 dev_t idisk
= fs_get_disk(inst
->fs
, 0);
1225 if (!idisk
|| disk
== idisk
)
1232 /* Check all file systems, using the /etc/fstab table. */
1233 static int check_all(void)
1235 int not_done_yet
= 1;
1238 int status
= FSCK_EX_OK
;
1240 struct libmnt_fs
*fs
;
1241 struct libmnt_iter
*itr
= mnt_new_iter(MNT_ITER_FORWARD
);
1244 err(FSCK_EX_ERROR
, _("failed to allocate iterator"));
1247 * Do an initial scan over the filesystem; mark filesystems
1248 * which should be ignored as done, and resolve any "auto"
1249 * filesystem types (done as a side-effect of calling ignore()).
1251 while (mnt_table_next_fs(fstab
, itr
, &fs
) == 0) {
1259 fputs(_("Checking all file systems.\n"), stdout
);
1262 * Find and check the root filesystem.
1264 if (!parallel_root
) {
1265 fs
= mnt_table_find_target(fstab
, "/", MNT_ITER_FORWARD
);
1269 !(ignore_mounted
&& is_mounted(fs
))) {
1270 status
|= fsck_device(fs
, 1);
1271 status
|= wait_many(FLAG_WAIT_ALL
);
1272 if (status
> FSCK_EX_NONDESTRUCT
) {
1282 * This is for the bone-headed user who enters the root
1283 * filesystem twice. Skip root will skip all root entries.
1286 mnt_reset_iter(itr
, MNT_ITER_FORWARD
);
1288 while(mnt_table_next_fs(fstab
, itr
, &fs
) == 0) {
1289 const char *tgt
= mnt_fs_get_target(fs
);
1291 if (tgt
&& strcmp(tgt
, "/") == 0)
1296 while (not_done_yet
) {
1300 mnt_reset_iter(itr
, MNT_ITER_FORWARD
);
1302 while(mnt_table_next_fs(fstab
, itr
, &fs
) == 0) {
1304 if (cancel_requested
)
1309 * If the filesystem's pass number is higher
1310 * than the current pass number, then we don't
1313 if (mnt_fs_get_passno(fs
) > passno
) {
1317 if (ignore_mounted
&& is_mounted(fs
)) {
1322 * If a filesystem on a particular device has
1323 * already been spawned, then we need to defer
1324 * this to another pass.
1326 if (disk_already_active(fs
)) {
1331 * Spawn off the fsck process
1333 status
|= fsck_device(fs
, serialize
);
1337 * Only do one filesystem at a time, or if we
1338 * have a limit on the number of fsck's extant
1339 * at one time, apply that limit.
1342 (max_running
&& (num_running
>= max_running
))) {
1347 if (cancel_requested
)
1350 printf(_("--waiting-- (pass %d)\n"), passno
);
1352 status
|= wait_many(pass_done
? FLAG_WAIT_ALL
:
1353 FLAG_WAIT_ATLEAST_ONE
);
1356 printf("----------------------------------\n");
1362 if (cancel_requested
&& !kill_sent
) {
1367 status
|= wait_many(FLAG_WAIT_ATLEAST_ONE
);
1372 static void __attribute__((__noreturn__
)) usage(void)
1375 fputs(USAGE_HEADER
, out
);
1376 fprintf(out
, _(" %s [options] -- [fs-options] [<filesystem> ...]\n"),
1377 program_invocation_short_name
);
1379 fputs(USAGE_SEPARATOR
, out
);
1380 fputs(_("Check and repair a Linux filesystem.\n"), out
);
1382 fputs(USAGE_OPTIONS
, out
);
1383 fputs(_(" -A check all filesystems\n"), out
);
1384 fputs(_(" -C [<fd>] display progress bar; file descriptor is for GUIs\n"), out
);
1385 fputs(_(" -l lock the device to guarantee exclusive access\n"), out
);
1386 fputs(_(" -M do not check mounted filesystems\n"), out
);
1387 fputs(_(" -N do not execute, just show what would be done\n"), out
);
1388 fputs(_(" -P check filesystems in parallel, including root\n"), out
);
1389 fputs(_(" -R skip root filesystem; useful only with '-A'\n"), out
);
1390 fputs(_(" -r [<fd>] report statistics for each device checked;\n"
1391 " file descriptor is for GUIs\n"), out
);
1392 fputs(_(" -s serialize the checking operations\n"), out
);
1393 fputs(_(" -T do not show the title on startup\n"), out
);
1394 fputs(_(" -t <type> specify filesystem types to be checked;\n"
1395 " <type> is allowed to be a comma-separated list\n"), out
);
1396 fputs(_(" -V explain what is being done\n"), out
);
1398 fputs(USAGE_SEPARATOR
, out
);
1399 fputs(_(" -?, --help display this help and exit\n"), out
);
1400 fputs(_(" --version output version information and exit\n"), out
);
1401 fputs(USAGE_SEPARATOR
, out
);
1402 fputs(_("See the specific fsck.* commands for available fs-options."), out
);
1403 fprintf(out
, USAGE_MAN_TAIL("fsck(8)"));
1407 static void signal_cancel(int sig
__attribute__((__unused__
)))
1412 static void parse_argv(int argc
, char *argv
[])
1415 char *arg
, *dev
, *tmp
= NULL
;
1418 int opts_for_fsck
= 0;
1419 struct sigaction sa
;
1420 int report_stats_fd
= -1;
1423 * Set up signal action
1425 memset(&sa
, 0, sizeof(struct sigaction
));
1426 sa
.sa_handler
= signal_cancel
;
1427 sigaction(SIGINT
, &sa
, NULL
);
1428 sigaction(SIGTERM
, &sa
, NULL
);
1432 instance_list
= NULL
;
1434 for (i
=1; i
< argc
; i
++) {
1439 /* the only two longopts to satisfy UL standards */
1440 if (!opts_for_fsck
&& !strcmp(arg
, "--help"))
1442 if (!opts_for_fsck
&& !strcmp(arg
, "--version")) {
1443 printf(UTIL_LINUX_VERSION
);
1447 if ((arg
[0] == '/' && !opts_for_fsck
) || strchr(arg
, '=')) {
1448 if (num_devices
>= MAX_DEVICES
)
1449 errx(FSCK_EX_ERROR
, _("too many devices"));
1451 dev
= mnt_resolve_spec(arg
, mntcache
);
1453 if (!dev
&& strchr(arg
, '=')) {
1455 * Check to see if we failed because
1456 * /proc/partitions isn't found.
1458 if (access(_PATH_PROC_PARTITIONS
, R_OK
) < 0) {
1459 warn(_("cannot open %s"),
1460 _PATH_PROC_PARTITIONS
);
1461 errx(FSCK_EX_ERROR
, _("Is /proc mounted?"));
1464 * Check to see if this is because
1465 * we're not running as root
1469 _("must be root to scan for matching filesystems: %s"),
1473 _("couldn't find matching filesystem: %s"),
1476 devices
[num_devices
++] = dev
? dev
: xstrdup(arg
);
1479 if (arg
[0] != '-' || opts_for_fsck
) {
1480 if (num_args
>= MAX_ARGS
)
1481 errx(FSCK_EX_ERROR
, _("too many arguments"));
1482 args
[num_args
++] = xstrdup(arg
);
1485 for (j
=1; arg
[j
]; j
++) {
1486 if (opts_for_fsck
) {
1487 options
[++opt
] = arg
[j
];
1496 if (arg
[j
+1]) { /* -C<fd> */
1497 progress_fd
= string_to_int(arg
+j
+1);
1498 if (progress_fd
< 0)
1502 } else if (i
+1 < argc
&& *argv
[i
+1] != '-') { /* -C <fd> */
1503 progress_fd
= string_to_int(argv
[i
+1]);
1504 if (progress_fd
< 0)
1535 if (arg
[j
+1]) { /* -r<fd> */
1536 report_stats_fd
= strtou32_or_err(arg
+j
+1, _("invalid argument of -r"));
1538 } else if (i
+1 < argc
&& *argv
[i
+1] >= '0' && *argv
[i
+1] <= '9') { /* -r <fd> */
1539 report_stats_fd
= strtou32_or_err(argv
[i
+1], _("invalid argument of -r"));
1551 _("option '%s' may be specified only once"), "-t");
1554 else if ((i
+1) < argc
)
1558 _("option '%s' requires an argument"), "-t");
1559 fstype
= xstrdup(tmp
);
1560 compile_fs_type(fstype
, &fs_type_compiled
);
1569 options
[++opt
] = arg
[j
];
1576 options
[++opt
] = '\0';
1577 if (num_args
>= MAX_ARGS
)
1578 errx(FSCK_EX_ERROR
, _("too many arguments"));
1579 args
[num_args
++] = xstrdup(options
);
1584 /* Validate the report stats file descriptor to avoid disasters */
1585 if (report_stats_fd
>= 0) {
1586 report_stats_file
= fdopen(report_stats_fd
, "w");
1587 if (!report_stats_file
)
1589 _("invalid argument of -r: %d"),
1593 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1594 force_all_parallel
++;
1595 if ((tmp
= getenv("FSCK_MAX_INST")))
1596 max_running
= atoi(tmp
);
1599 int main(int argc
, char *argv
[])
1602 int interactive
= 0;
1603 struct libmnt_fs
*fs
;
1604 const char *path
= getenv("PATH");
1606 setvbuf(stdout
, NULL
, _IONBF
, BUFSIZ
);
1607 setvbuf(stderr
, NULL
, _IONBF
, BUFSIZ
);
1609 setlocale(LC_MESSAGES
, "");
1610 setlocale(LC_CTYPE
, "");
1611 bindtextdomain(PACKAGE
, LOCALEDIR
);
1612 textdomain(PACKAGE
);
1613 atexit(close_stdout
);
1615 mnt_init_debug(0); /* init libmount debug mask */
1616 mntcache
= mnt_new_cache(); /* no fatal error if failed */
1618 parse_argv(argc
, argv
);
1621 printf(UTIL_LINUX_VERSION
);
1625 fsck_path
= xstrdup(path
&& *path
? path
: FSCK_DEFAULT_PATH
);
1627 if ((num_devices
== 1) || (serialize
))
1630 if (lockdisk
&& (doall
|| num_devices
> 1)) {
1631 warnx(_("the -l option can be used with one "
1632 "device only -- ignore"));
1636 /* If -A was specified ("check all"), do that! */
1640 if (num_devices
== 0) {
1645 for (i
= 0 ; i
< num_devices
; i
++) {
1646 if (cancel_requested
) {
1653 fs
= lookup(devices
[i
]);
1655 fs
= add_dummy_fs(devices
[i
]);
1656 else if (fs_ignored_type(fs
))
1658 if (ignore_mounted
&& is_mounted(fs
))
1660 status
|= fsck_device(fs
, interactive
);
1662 (max_running
&& (num_running
>= max_running
))) {
1663 struct fsck_instance
*inst
;
1667 status
|= inst
->exit_status
;
1668 free_instance(inst
);
1671 printf("----------------------------------\n");
1674 status
|= wait_many(FLAG_WAIT_ALL
);
1676 mnt_unref_cache(mntcache
);
1677 mnt_unref_table(fstab
);
1678 mnt_unref_table(mtab
);