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
)
172 l
= strtol(s
, &p
, 0);
173 if (*p
|| l
== LONG_MIN
|| l
== LONG_MAX
|| l
< 0 || l
> INT_MAX
)
179 /* Do we really really want to check this fs? */
180 static int fs_check_required(const char *type
)
184 for(i
= 0; i
< ARRAY_SIZE(really_wanted
); i
++) {
185 if (strcmp(type
, really_wanted
[i
]) == 0)
192 static int is_mounted(struct libmnt_fs
*fs
)
197 src
= mnt_fs_get_source(fs
);
201 mntcache
= mnt_new_cache();
203 mtab
= mnt_new_table();
205 err(FSCK_EX_ERROR
, ("failed to initialize libmount table"));
206 mnt_table_set_cache(mtab
, mntcache
);
207 mnt_table_parse_mtab(mtab
, NULL
);
210 rc
= mnt_table_find_source(mtab
, src
, MNT_ITER_BACKWARD
) ? 1 : 0;
213 printf(_("%s is mounted\n"), src
);
215 printf(_("%s is not mounted\n"), src
);
220 static int ignore(struct libmnt_fs
*);
222 static struct fsck_fs_data
*fs_create_data(struct libmnt_fs
*fs
)
224 struct fsck_fs_data
*data
= mnt_fs_get_userdata(fs
);
227 data
= xcalloc(1, sizeof(*data
));
228 mnt_fs_set_userdata(fs
, data
);
234 * fs from fstab might contains real device name as well as symlink,
235 * LABEL or UUID, this function returns canonicalized result.
237 static const char *fs_get_device(struct libmnt_fs
*fs
)
239 struct fsck_fs_data
*data
= mnt_fs_get_userdata(fs
);
241 if (!data
|| !data
->eval_device
) {
242 const char *spec
= mnt_fs_get_source(fs
);
245 data
= fs_create_data(fs
);
247 data
->eval_device
= 1;
248 data
->device
= mnt_resolve_spec(spec
, mnt_table_get_cache(fstab
));
250 data
->device
= xstrdup(spec
);
256 static dev_t
fs_get_disk(struct libmnt_fs
*fs
, int check
)
258 struct fsck_fs_data
*data
;
262 data
= mnt_fs_get_userdata(fs
);
263 if (data
&& data
->disk
)
269 if (mnt_fs_is_netfs(fs
) || mnt_fs_is_pseudofs(fs
))
272 device
= fs_get_device(fs
);
276 data
= fs_create_data(fs
);
278 if (!stat(device
, &st
) &&
279 !blkid_devno_to_wholedisk(st
.st_rdev
, NULL
, 0, &data
->disk
)) {
282 data
->stacked
= count_slaves(data
->disk
) > 0 ? 1 : 0;
288 static int fs_is_stacked(struct libmnt_fs
*fs
)
290 struct fsck_fs_data
*data
= mnt_fs_get_userdata(fs
);
291 return data
? data
->stacked
: 0;
294 static int fs_is_done(struct libmnt_fs
*fs
)
296 struct fsck_fs_data
*data
= mnt_fs_get_userdata(fs
);
297 return data
? data
->done
: 0;
300 static void fs_set_done(struct libmnt_fs
*fs
)
302 struct fsck_fs_data
*data
= fs_create_data(fs
);
308 static int is_irrotational_disk(dev_t disk
)
315 rc
= snprintf(path
, sizeof(path
),
316 "/sys/dev/block/%d:%d/queue/rotational",
317 major(disk
), minor(disk
));
319 if (rc
< 0 || (unsigned int) rc
>= sizeof(path
))
322 f
= fopen(path
, "r");
326 rc
= fscanf(f
, "%d", &x
);
329 warn(_("cannot read %s"), path
);
331 warnx(_("parse error: %s"), path
);
335 return rc
== 1 ? !x
: 0;
338 static void lock_disk(struct fsck_instance
*inst
)
340 dev_t disk
= fs_get_disk(inst
->fs
, 1);
341 char *diskpath
= NULL
, *diskname
;
345 if (!disk
|| is_irrotational_disk(disk
))
348 diskpath
= blkid_devno_to_devname(disk
);
352 if (access(FSCK_RUNTIME_DIRNAME
, F_OK
) != 0) {
353 int rc
= mkdir(FSCK_RUNTIME_DIRNAME
,
355 S_IRUSR
|S_IRGRP
|S_IROTH
|
356 S_IXUSR
|S_IXGRP
|S_IXOTH
);
357 if (rc
&& errno
!= EEXIST
) {
358 warn(_("cannot create directory %s"),
359 FSCK_RUNTIME_DIRNAME
);
364 diskname
= stripoff_last_component(diskpath
);
368 xasprintf(&inst
->lockpath
, FSCK_RUNTIME_DIRNAME
"/%s.lock", diskname
);
371 printf(_("Locking disk by %s ... "), inst
->lockpath
);
373 inst
->lock
= open(inst
->lockpath
, O_RDONLY
|O_CREAT
|O_CLOEXEC
,
374 S_IWUSR
|S_IRUSR
|S_IRGRP
|S_IROTH
);
375 if (inst
->lock
>= 0) {
378 /* inform users that we're waiting on the lock */
380 (rc
= flock(inst
->lock
, LOCK_EX
| LOCK_NB
)) != 0 &&
381 errno
== EWOULDBLOCK
)
382 printf(_("(waiting) "));
384 if (rc
!= 0 && flock(inst
->lock
, LOCK_EX
) != 0) {
385 close(inst
->lock
); /* failed */
391 /* TRANSLATORS: These are followups to "Locking disk...". */
392 printf("%s.\n", inst
->lock
>= 0 ? _("succeeded") : _("failed"));
396 if (inst
->lock
< 0) {
397 free(inst
->lockpath
);
398 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
)
431 static struct libmnt_fs
*add_dummy_fs(const char *device
)
433 struct libmnt_fs
*fs
= mnt_new_fs();
435 if (fs
&& mnt_fs_set_source(fs
, device
) == 0 &&
436 mnt_table_add_fs(fstab
, fs
) == 0) {
442 err(FSCK_EX_ERROR
, _("failed to setup description for %s"), device
);
445 static void fs_interpret_type(struct libmnt_fs
*fs
)
448 const char *type
= mnt_fs_get_fstype(fs
);
450 if (type
&& strcmp(type
, "auto") != 0)
453 mnt_fs_set_fstype(fs
, NULL
);
455 device
= fs_get_device(fs
);
459 struct libmnt_cache
*cache
= mnt_table_get_cache(fstab
);
461 tp
= mnt_get_fstype(device
, &ambi
, cache
);
463 mnt_fs_set_fstype(fs
, tp
);
469 static int parser_errcb(struct libmnt_table
*tb
__attribute__ ((__unused__
)),
470 const char *filename
, int line
)
472 warnx(_("%s: parse error at line %d -- ignored"), filename
, line
);
477 * Load the filesystem database from /etc/fstab
479 static void load_fs_info(void)
483 fstab
= mnt_new_table();
485 err(FSCK_EX_ERROR
, ("failed to initialize libmount table"));
487 mnt_table_set_parser_errcb(fstab
, parser_errcb
);
488 mnt_table_set_cache(fstab
, mntcache
);
493 * Let's follow libmount defaults if $FSTAB_FILE is not specified
495 path
= getenv("FSTAB_FILE");
497 if (mnt_table_parse_fstab(fstab
, path
)) {
499 path
= mnt_get_fstab_path();
501 /* don't print error when there is no fstab at all */
502 if (access(path
, F_OK
) == 0) {
504 warn(_("%s: failed to parse fstab"), path
);
506 warnx(_("%s: failed to parse fstab"), path
);
512 * Lookup filesys in /etc/fstab and return the corresponding entry.
513 * The @path has to be real path (no TAG) by mnt_resolve_spec().
515 static struct libmnt_fs
*lookup(char *path
)
517 struct libmnt_fs
*fs
;
522 fs
= mnt_table_find_srcpath(fstab
, path
, MNT_ITER_FORWARD
);
525 * Maybe mountpoint has been specified on fsck command line.
526 * Yeah, crazy feature...
528 * Note that mnt_table_find_target() may canonicalize paths in
529 * the fstab to support symlinks. This is really unwanted,
530 * because readlink() on mountpoints may trigger automounts.
532 * So, disable the cache and compare the paths as strings
533 * without care about symlinks...
535 mnt_table_set_cache(fstab
, NULL
);
536 fs
= mnt_table_find_target(fstab
, path
, MNT_ITER_FORWARD
);
537 mnt_table_set_cache(fstab
, mntcache
);
542 /* Find fsck program for a given fs type. */
543 static char *find_fsck(const char *type
)
547 static char prog
[256];
548 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 sprintf(prog
, tpl
, s
, type
);
555 if (access(prog
, X_OK
) == 0)
560 return(s
? prog
: NULL
);
563 static int progress_active(void)
565 struct fsck_instance
*inst
;
567 for (inst
= instance_list
; inst
; inst
= inst
->next
) {
568 if (inst
->flags
& FLAG_DONE
)
570 if (inst
->flags
& FLAG_PROGRESS
)
577 * Process run statistics for finished fsck instances.
579 * If report_stats is 0, do nothing, otherwise print a selection of
580 * interesting rusage statistics as well as elapsed wallclock time.
582 static void print_stats(struct fsck_instance
*inst
)
584 struct timeval delta
;
586 if (!inst
|| !report_stats
|| noexecute
)
589 timersub(&inst
->end_time
, &inst
->start_time
, &delta
);
591 if (report_stats_file
)
592 fprintf(report_stats_file
, "%s %d %ld "
593 "%ld.%06ld %ld.%06ld %ld.%06ld\n",
594 fs_get_device(inst
->fs
),
596 inst
->rusage
.ru_maxrss
,
597 (long)delta
.tv_sec
, (long)delta
.tv_usec
,
598 (long)inst
->rusage
.ru_utime
.tv_sec
,
599 (long)inst
->rusage
.ru_utime
.tv_usec
,
600 (long)inst
->rusage
.ru_stime
.tv_sec
,
601 (long)inst
->rusage
.ru_stime
.tv_usec
);
603 fprintf(stdout
, "%s: status %d, rss %ld, "
604 "real %ld.%06ld, user %ld.%06ld, sys %ld.%06ld\n",
605 fs_get_device(inst
->fs
),
607 inst
->rusage
.ru_maxrss
,
608 (long)delta
.tv_sec
, (long)delta
.tv_usec
,
609 (long)inst
->rusage
.ru_utime
.tv_sec
,
610 (long)inst
->rusage
.ru_utime
.tv_usec
,
611 (long)inst
->rusage
.ru_stime
.tv_sec
,
612 (long)inst
->rusage
.ru_stime
.tv_usec
);
616 * Execute a particular fsck program, and link it into the list of
617 * child processes we are waiting for.
619 static int execute(const char *progname
, const char *progpath
,
620 const char *type
, struct libmnt_fs
*fs
, int interactive
)
624 struct fsck_instance
*inst
, *p
;
627 inst
= xcalloc(1, sizeof(*inst
));
629 argv
[0] = xstrdup(progname
);
632 for (i
=0; i
<num_args
; i
++)
633 argv
[argc
++] = xstrdup(args
[i
]);
636 ((strcmp(type
, "ext2") == 0) ||
637 (strcmp(type
, "ext3") == 0) ||
638 (strcmp(type
, "ext4") == 0) ||
639 (strcmp(type
, "ext4dev") == 0))) {
643 if (!progress_active()) {
644 snprintf(tmp
, 80, "-C%d", progress_fd
);
645 inst
->flags
|= FLAG_PROGRESS
;
646 } else if (progress_fd
)
647 snprintf(tmp
, 80, "-C%d", progress_fd
* -1);
649 argv
[argc
++] = xstrdup(tmp
);
652 argv
[argc
++] = xstrdup(fs_get_device(fs
));
655 if (verbose
|| noexecute
) {
656 const char *tgt
= mnt_fs_get_target(fs
);
659 tgt
= fs_get_device(fs
);
660 printf("[%s (%d) -- %s] ", progpath
, num_running
, tgt
);
661 for (i
=0; i
< argc
; i
++)
662 printf("%s ", argv
[i
]);
673 /* Fork and execute the correct program. */
676 else if ((pid
= fork()) < 0) {
677 warn(_("fork failed"));
680 } else if (pid
== 0) {
683 execv(progpath
, argv
);
684 err(FSCK_EX_ERROR
, _("%s: execute failed"), progpath
);
687 for (i
=0; i
< argc
; i
++)
691 inst
->prog
= xstrdup(progname
);
692 inst
->type
= xstrdup(type
);
693 gettime_monotonic(&inst
->start_time
);
697 * Find the end of the list, so we add the instance on at the end.
699 for (p
= instance_list
; p
&& p
->next
; p
= p
->next
);
704 instance_list
= inst
;
710 * Send a signal to all outstanding fsck child processes
712 static int kill_all(int signum
)
714 struct fsck_instance
*inst
;
717 for (inst
= instance_list
; inst
; inst
= inst
->next
) {
718 if (inst
->flags
& FLAG_DONE
)
720 kill(inst
->pid
, signum
);
727 * Wait for one child process to exit; when it does, unlink it from
728 * the list of executing child processes, and return it.
730 static struct fsck_instance
*wait_one(int flags
)
734 struct fsck_instance
*inst
, *inst2
, *prev
;
736 struct rusage rusage
;
742 inst
= instance_list
;
745 while (inst
->next
&& (random() & 1)) {
750 inst
->exit_status
= 0;
755 * gcc -Wall fails saving throw against stupidity
756 * (inst and prev are thought to be uninitialized variables)
761 pid
= wait4(-1, &status
, flags
, &rusage
);
762 if (cancel_requested
&& !kill_sent
) {
766 if ((pid
== 0) && (flags
& WNOHANG
))
769 if ((errno
== EINTR
) || (errno
== EAGAIN
))
771 if (errno
== ECHILD
) {
772 warnx(_("wait: no more child process?!?"));
775 warn(_("waitpid failed"));
778 for (prev
= NULL
, inst
= instance_list
;
780 prev
= inst
, inst
= inst
->next
) {
781 if (inst
->pid
== pid
)
786 if (WIFEXITED(status
))
787 status
= WEXITSTATUS(status
);
788 else if (WIFSIGNALED(status
)) {
789 sig
= WTERMSIG(status
);
791 status
= FSCK_EX_UNCORRECTED
;
793 warnx(_("Warning... %s for device %s exited "
795 inst
->prog
, fs_get_device(inst
->fs
), sig
);
796 status
= FSCK_EX_ERROR
;
799 warnx(_("%s %s: status is %x, should never happen."),
800 inst
->prog
, fs_get_device(inst
->fs
), status
);
801 status
= FSCK_EX_ERROR
;
804 inst
->exit_status
= status
;
805 inst
->flags
|= FLAG_DONE
;
806 gettime_monotonic(&inst
->end_time
);
807 memcpy(&inst
->rusage
, &rusage
, sizeof(struct rusage
));
809 if (progress
&& (inst
->flags
& FLAG_PROGRESS
) &&
810 !progress_active()) {
811 for (inst2
= instance_list
; inst2
; inst2
= inst2
->next
) {
812 if (inst2
->flags
& FLAG_DONE
)
814 if (strcmp(inst2
->type
, "ext2") &&
815 strcmp(inst2
->type
, "ext3") &&
816 strcmp(inst2
->type
, "ext4") &&
817 strcmp(inst2
->type
, "ext4dev"))
820 * If we've just started the fsck, wait a tiny
821 * bit before sending the kill, to give it
822 * time to set up the signal handler
824 if (inst2
->start_time
.tv_sec
< time(NULL
) + 2) {
827 kill(inst2
->pid
, SIGUSR1
);
831 kill(inst2
->pid
, SIGUSR1
);
832 inst2
->flags
|= FLAG_PROGRESS
;
838 prev
->next
= inst
->next
;
840 instance_list
= inst
->next
;
845 printf(_("Finished with %s (exit status %d)\n"),
846 fs_get_device(inst
->fs
), inst
->exit_status
);
851 #define FLAG_WAIT_ALL 0
852 #define FLAG_WAIT_ATLEAST_ONE 1
854 * Wait until all executing child processes have exited; return the
855 * logical OR of all of their exit code values.
857 static int wait_many(int flags
)
859 struct fsck_instance
*inst
;
860 int global_status
= 0;
863 while ((inst
= wait_one(wait_flags
))) {
864 global_status
|= inst
->exit_status
;
867 if (noexecute
&& (flags
& WNOHANG
) && !(random() % 3))
870 if (flags
& FLAG_WAIT_ATLEAST_ONE
)
871 wait_flags
= WNOHANG
;
873 return global_status
;
877 * Run the fsck program on a particular device
879 * If the type is specified using -t, and it isn't prefixed with "no"
880 * (as in "noext2") and only one filesystem type is specified, then
881 * use that type regardless of what is specified in /etc/fstab.
883 * If the type isn't specified by the user, then use either the type
884 * specified in /etc/fstab, or DEFAULT_FSTYPE.
886 static int fsck_device(struct libmnt_fs
*fs
, int interactive
)
888 char progname
[80], *progpath
;
892 fs_interpret_type(fs
);
894 type
= mnt_fs_get_fstype(fs
);
896 if (type
&& strcmp(type
, "auto") != 0)
898 else if (fstype
&& strncmp(fstype
, "no", 2) &&
899 strncmp(fstype
, "opts=", 5) && strncmp(fstype
, "loop", 4) &&
900 !strchr(fstype
, ','))
903 type
= DEFAULT_FSTYPE
;
905 sprintf(progname
, "fsck.%s", type
);
906 progpath
= find_fsck(progname
);
907 if (progpath
== NULL
) {
908 if (fs_check_required(type
)) {
916 retval
= execute(progname
, progpath
, type
, fs
, interactive
);
923 warnx(_("error %d (%m) while executing fsck.%s for %s"),
924 retval
, type
, fs_get_device(fs
));
925 return FSCK_EX_ERROR
;
930 * Deal with the fsck -t argument.
932 static struct fs_type_compile
{
938 #define FS_TYPE_NORMAL 0
939 #define FS_TYPE_OPT 1
940 #define FS_TYPE_NEGOPT 2
942 static void compile_fs_type(char *fs_type
, struct fs_type_compile
*cmp
)
946 int negate
, first_negate
= 1;
949 for (cp
=fs_type
; *cp
; cp
++) {
955 cmp
->list
= xcalloc(num
, sizeof(char *));
956 cmp
->type
= xcalloc(num
, sizeof(int));
962 list
= xstrdup(fs_type
);
964 s
= strtok(list
, ",");
967 if (strncmp(s
, "no", 2) == 0) {
970 } else if (*s
== '!') {
974 if (strcmp(s
, "loop") == 0)
975 /* loop is really short-hand for opts=loop */
976 goto loop_special_case
;
977 else if (strncmp(s
, "opts=", 5) == 0) {
980 cmp
->type
[num
] = negate
? FS_TYPE_NEGOPT
: FS_TYPE_OPT
;
983 cmp
->negate
= negate
;
986 if ((negate
&& !cmp
->negate
) ||
987 (!negate
&& cmp
->negate
)) {
989 _("Either all or none of the filesystem types passed to -t must be prefixed\n"
990 "with 'no' or '!'."));
994 cmp
->list
[num
++] = xstrdup(s
);
995 s
= strtok(NULL
, ",");
1001 * This function returns true if a particular option appears in a
1002 * comma-delimited options list
1004 static int opt_in_list(const char *opt
, const char *optlist
)
1010 list
= xstrdup(optlist
);
1012 s
= strtok(list
, ",");
1014 if (strcmp(s
, opt
) == 0) {
1018 s
= strtok(NULL
, ",");
1024 /* See if the filesystem matches the criteria given by the -t option */
1025 static int fs_match(struct libmnt_fs
*fs
, struct fs_type_compile
*cmp
)
1027 int n
, ret
= 0, checked_type
= 0;
1030 if (cmp
->list
== NULL
|| cmp
->list
[0] == NULL
)
1033 for (n
=0; (cp
= cmp
->list
[n
]); n
++) {
1034 switch (cmp
->type
[n
]) {
1035 case FS_TYPE_NORMAL
:
1037 const char *type
= mnt_fs_get_fstype(fs
);
1040 if (type
&& strcmp(cp
, type
) == 0)
1044 case FS_TYPE_NEGOPT
:
1045 if (opt_in_list(cp
, mnt_fs_get_options(fs
)))
1049 if (!opt_in_list(cp
, mnt_fs_get_options(fs
)))
1054 if (checked_type
== 0)
1056 return (cmp
->negate
? !ret
: ret
);
1060 * Check if a device exists
1062 static int device_exists(const char *device
)
1066 if (stat(device
, &st
) == -1)
1068 if (!S_ISBLK(st
.st_mode
))
1073 static int fs_ignored_type(struct libmnt_fs
*fs
)
1075 const char **ip
, *type
;
1077 if (mnt_fs_is_netfs(fs
) || mnt_fs_is_pseudofs(fs
) || mnt_fs_is_swaparea(fs
))
1080 type
= mnt_fs_get_fstype(fs
);
1082 for(ip
= ignored_types
; type
&& *ip
; ip
++) {
1083 if (strcmp(type
, *ip
) == 0)
1090 /* Check if we should ignore this filesystem. */
1091 static int ignore(struct libmnt_fs
*fs
)
1096 * If the pass number is 0, ignore it.
1098 if (mnt_fs_get_passno(fs
) == 0)
1102 * If this is a bind mount, ignore it.
1104 if (opt_in_list("bind", mnt_fs_get_options(fs
))) {
1105 warnx(_("%s: skipping bad line in /etc/fstab: "
1106 "bind mount with nonzero fsck pass number"),
1107 mnt_fs_get_target(fs
));
1112 * ignore devices that don't exist and have the "nofail" mount option
1114 if (!device_exists(fs_get_device(fs
))) {
1115 if (opt_in_list("nofail", mnt_fs_get_options(fs
))) {
1117 printf(_("%s: skipping nonexistent device\n"),
1122 printf(_("%s: nonexistent device (\"nofail\" fstab "
1123 "option may be used to skip this device)\n"),
1127 fs_interpret_type(fs
);
1130 * If a specific fstype is specified, and it doesn't match,
1133 if (!fs_match(fs
, &fs_type_compiled
))
1136 type
= mnt_fs_get_fstype(fs
);
1139 printf(_("%s: skipping unknown filesystem type\n"),
1144 /* Are we ignoring this type? */
1145 if (fs_ignored_type(fs
))
1150 /* See if the <fsck.fs> program is available. */
1151 if (find_fsck(type
) == NULL
) {
1152 if (fs_check_required(type
))
1153 warnx(_("cannot check %s: fsck.%s not found"),
1154 fs_get_device(fs
), type
);
1158 /* We can and want to check this file system type. */
1162 static int count_slaves(dev_t disk
)
1166 char dirname
[PATH_MAX
];
1169 snprintf(dirname
, sizeof(dirname
),
1170 "/sys/dev/block/%u:%u/slaves/",
1171 major(disk
), minor(disk
));
1173 if (!(dir
= opendir(dirname
)))
1176 while ((dp
= readdir(dir
)) != NULL
) {
1177 #ifdef _DIRENT_HAVE_D_TYPE
1178 if (dp
->d_type
!= DT_UNKNOWN
&& dp
->d_type
!= DT_LNK
)
1181 if (dp
->d_name
[0] == '.' &&
1182 ((dp
->d_name
[1] == 0) ||
1183 ((dp
->d_name
[1] == '.') && (dp
->d_name
[2] == 0))))
1194 * Returns TRUE if a partition on the same disk is already being
1197 static int disk_already_active(struct libmnt_fs
*fs
)
1199 struct fsck_instance
*inst
;
1202 if (force_all_parallel
)
1205 if (instance_list
&& fs_is_stacked(instance_list
->fs
))
1206 /* any instance for a stacked device is already running */
1209 disk
= fs_get_disk(fs
, 1);
1212 * If we don't know the base device, assume that the device is
1213 * already active if there are any fsck instances running.
1215 * Don't check a stacked device with any other disk too.
1217 if (!disk
|| fs_is_stacked(fs
))
1218 return (instance_list
!= NULL
);
1220 for (inst
= instance_list
; inst
; inst
= inst
->next
) {
1221 dev_t idisk
= fs_get_disk(inst
->fs
, 0);
1223 if (!idisk
|| disk
== idisk
)
1230 /* Check all file systems, using the /etc/fstab table. */
1231 static int check_all(void)
1233 int not_done_yet
= 1;
1236 int status
= FSCK_EX_OK
;
1238 struct libmnt_fs
*fs
;
1239 struct libmnt_iter
*itr
= mnt_new_iter(MNT_ITER_FORWARD
);
1242 err(FSCK_EX_ERROR
, _("failed to allocate iterator"));
1245 * Do an initial scan over the filesystem; mark filesystems
1246 * which should be ignored as done, and resolve any "auto"
1247 * filesystem types (done as a side-effect of calling ignore()).
1249 while (mnt_table_next_fs(fstab
, itr
, &fs
) == 0) {
1257 fputs(_("Checking all file systems.\n"), stdout
);
1260 * Find and check the root filesystem.
1262 if (!parallel_root
) {
1263 fs
= mnt_table_find_target(fstab
, "/", MNT_ITER_FORWARD
);
1267 !(ignore_mounted
&& is_mounted(fs
))) {
1268 status
|= fsck_device(fs
, 1);
1269 status
|= wait_many(FLAG_WAIT_ALL
);
1270 if (status
> FSCK_EX_NONDESTRUCT
) {
1280 * This is for the bone-headed user who enters the root
1281 * filesystem twice. Skip root will skip all root entries.
1284 mnt_reset_iter(itr
, MNT_ITER_FORWARD
);
1286 while(mnt_table_next_fs(fstab
, itr
, &fs
) == 0) {
1287 const char *tgt
= mnt_fs_get_target(fs
);
1289 if (tgt
&& strcmp(tgt
, "/") == 0)
1294 while (not_done_yet
) {
1298 mnt_reset_iter(itr
, MNT_ITER_FORWARD
);
1300 while(mnt_table_next_fs(fstab
, itr
, &fs
) == 0) {
1302 if (cancel_requested
)
1307 * If the filesystem's pass number is higher
1308 * than the current pass number, then we don't
1311 if (mnt_fs_get_passno(fs
) > passno
) {
1315 if (ignore_mounted
&& is_mounted(fs
)) {
1320 * If a filesystem on a particular device has
1321 * already been spawned, then we need to defer
1322 * this to another pass.
1324 if (disk_already_active(fs
)) {
1329 * Spawn off the fsck process
1331 status
|= fsck_device(fs
, serialize
);
1335 * Only do one filesystem at a time, or if we
1336 * have a limit on the number of fsck's extant
1337 * at one time, apply that limit.
1340 (max_running
&& (num_running
>= max_running
))) {
1345 if (cancel_requested
)
1348 printf(_("--waiting-- (pass %d)\n"), passno
);
1350 status
|= wait_many(pass_done
? FLAG_WAIT_ALL
:
1351 FLAG_WAIT_ATLEAST_ONE
);
1354 printf("----------------------------------\n");
1360 if (cancel_requested
&& !kill_sent
) {
1365 status
|= wait_many(FLAG_WAIT_ATLEAST_ONE
);
1370 static void __attribute__((__noreturn__
)) usage(void)
1373 fputs(USAGE_HEADER
, out
);
1374 fprintf(out
, _(" %s [options] -- [fs-options] [<filesystem> ...]\n"),
1375 program_invocation_short_name
);
1377 fputs(USAGE_SEPARATOR
, out
);
1378 fputs(_("Check and repair a Linux filesystem.\n"), out
);
1380 fputs(USAGE_OPTIONS
, out
);
1381 fputs(_(" -A check all filesystems\n"), out
);
1382 fputs(_(" -C [<fd>] display progress bar; file descriptor is for GUIs\n"), out
);
1383 fputs(_(" -l lock the device to guarantee exclusive access\n"), out
);
1384 fputs(_(" -M do not check mounted filesystems\n"), out
);
1385 fputs(_(" -N do not execute, just show what would be done\n"), out
);
1386 fputs(_(" -P check filesystems in parallel, including root\n"), out
);
1387 fputs(_(" -R skip root filesystem; useful only with '-A'\n"), out
);
1388 fputs(_(" -r [<fd>] report statistics for each device checked;\n"
1389 " file descriptor is for GUIs\n"), out
);
1390 fputs(_(" -s serialize the checking operations\n"), out
);
1391 fputs(_(" -T do not show the title on startup\n"), out
);
1392 fputs(_(" -t <type> specify filesystem types to be checked;\n"
1393 " <type> is allowed to be a comma-separated list\n"), out
);
1394 fputs(_(" -V explain what is being done\n"), out
);
1396 fputs(USAGE_SEPARATOR
, out
);
1397 printf( " -?, --help %s\n", USAGE_OPTSTR_HELP
);
1398 printf( " --version %s\n", USAGE_OPTSTR_VERSION
);
1399 fputs(USAGE_SEPARATOR
, out
);
1400 fputs(_("See the specific fsck.* commands for available fs-options."), out
);
1401 fprintf(out
, USAGE_MAN_TAIL("fsck(8)"));
1405 static void signal_cancel(int sig
__attribute__((__unused__
)))
1410 static void parse_argv(int argc
, char *argv
[])
1413 char *arg
, *dev
, *tmp
= NULL
;
1416 int opts_for_fsck
= 0;
1417 struct sigaction sa
;
1418 int report_stats_fd
= -1;
1421 * Set up signal action
1423 memset(&sa
, 0, sizeof(struct sigaction
));
1424 sa
.sa_handler
= signal_cancel
;
1425 sigaction(SIGINT
, &sa
, NULL
);
1426 sigaction(SIGTERM
, &sa
, NULL
);
1430 instance_list
= NULL
;
1432 for (i
=1; i
< argc
; i
++) {
1437 /* the only two longopts to satisfy UL standards */
1438 if (!opts_for_fsck
&& !strcmp(arg
, "--help"))
1440 if (!opts_for_fsck
&& !strcmp(arg
, "--version")) {
1441 printf(UTIL_LINUX_VERSION
);
1445 if ((arg
[0] == '/' && !opts_for_fsck
) || strchr(arg
, '=')) {
1446 if (num_devices
>= MAX_DEVICES
)
1447 errx(FSCK_EX_ERROR
, _("too many devices"));
1449 dev
= mnt_resolve_spec(arg
, mntcache
);
1451 if (!dev
&& strchr(arg
, '=')) {
1453 * Check to see if we failed because
1454 * /proc/partitions isn't found.
1456 if (access(_PATH_PROC_PARTITIONS
, R_OK
) < 0) {
1457 warn(_("cannot open %s"),
1458 _PATH_PROC_PARTITIONS
);
1459 errx(FSCK_EX_ERROR
, _("Is /proc mounted?"));
1462 * Check to see if this is because
1463 * we're not running as root
1467 _("must be root to scan for matching filesystems: %s"),
1471 _("couldn't find matching filesystem: %s"),
1474 devices
[num_devices
++] = dev
? dev
: xstrdup(arg
);
1477 if (arg
[0] != '-' || opts_for_fsck
) {
1478 if (num_args
>= MAX_ARGS
)
1479 errx(FSCK_EX_ERROR
, _("too many arguments"));
1480 args
[num_args
++] = xstrdup(arg
);
1483 for (j
=1; arg
[j
]; j
++) {
1484 if (opts_for_fsck
) {
1485 options
[++opt
] = arg
[j
];
1494 if (arg
[j
+1]) { /* -C<fd> */
1495 progress_fd
= string_to_int(arg
+j
+1);
1496 if (progress_fd
< 0)
1500 } else if (i
+1 < argc
&& *argv
[i
+1] != '-') { /* -C <fd> */
1501 progress_fd
= string_to_int(argv
[i
+1]);
1502 if (progress_fd
< 0)
1533 if (arg
[j
+1]) { /* -r<fd> */
1534 report_stats_fd
= strtou32_or_err(arg
+j
+1, _("invalid argument of -r"));
1536 } else if (i
+1 < argc
&& *argv
[i
+1] >= '0' && *argv
[i
+1] <= '9') { /* -r <fd> */
1537 report_stats_fd
= strtou32_or_err(argv
[i
+1], _("invalid argument of -r"));
1549 _("option '%s' may be specified only once"), "-t");
1552 else if ((i
+1) < argc
)
1556 _("option '%s' requires an argument"), "-t");
1557 fstype
= xstrdup(tmp
);
1558 compile_fs_type(fstype
, &fs_type_compiled
);
1567 options
[++opt
] = arg
[j
];
1574 options
[++opt
] = '\0';
1575 if (num_args
>= MAX_ARGS
)
1576 errx(FSCK_EX_ERROR
, _("too many arguments"));
1577 args
[num_args
++] = xstrdup(options
);
1582 /* Validate the report stats file descriptor to avoid disasters */
1583 if (report_stats_fd
>= 0) {
1584 report_stats_file
= fdopen(report_stats_fd
, "w");
1585 if (!report_stats_file
)
1587 _("invalid argument of -r: %d"),
1591 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1592 force_all_parallel
++;
1593 if ((tmp
= getenv("FSCK_MAX_INST")))
1594 max_running
= atoi(tmp
);
1597 int main(int argc
, char *argv
[])
1600 int interactive
= 0;
1601 struct libmnt_fs
*fs
;
1602 const char *path
= getenv("PATH");
1604 setvbuf(stdout
, NULL
, _IONBF
, BUFSIZ
);
1605 setvbuf(stderr
, NULL
, _IONBF
, BUFSIZ
);
1607 setlocale(LC_MESSAGES
, "");
1608 setlocale(LC_CTYPE
, "");
1609 bindtextdomain(PACKAGE
, LOCALEDIR
);
1610 textdomain(PACKAGE
);
1611 atexit(close_stdout
);
1613 strutils_set_exitcode(FSCK_EX_USAGE
);
1614 mnt_init_debug(0); /* init libmount debug mask */
1615 mntcache
= mnt_new_cache(); /* no fatal error if failed */
1617 parse_argv(argc
, argv
);
1620 printf(UTIL_LINUX_VERSION
);
1624 fsck_path
= xstrdup(path
&& *path
? path
: FSCK_DEFAULT_PATH
);
1626 if ((num_devices
== 1) || (serialize
))
1629 if (lockdisk
&& (doall
|| num_devices
> 1)) {
1630 warnx(_("the -l option can be used with one "
1631 "device only -- ignore"));
1635 /* If -A was specified ("check all"), do that! */
1639 if (num_devices
== 0) {
1644 for (i
= 0 ; i
< num_devices
; i
++) {
1645 if (cancel_requested
) {
1652 fs
= lookup(devices
[i
]);
1654 fs
= add_dummy_fs(devices
[i
]);
1655 else if (fs_ignored_type(fs
))
1657 if (ignore_mounted
&& is_mounted(fs
))
1659 status
|= fsck_device(fs
, interactive
);
1661 (max_running
&& (num_running
>= max_running
))) {
1662 struct fsck_instance
*inst
;
1666 status
|= inst
->exit_status
;
1667 free_instance(inst
);
1670 printf("----------------------------------\n");
1673 status
|= wait_many(FLAG_WAIT_ALL
);
1675 mnt_unref_cache(mntcache
);
1676 mnt_unref_table(fstab
);
1677 mnt_unref_table(mtab
);