2 * fsck --- A generic, parallelizing front-end for the fsck program.
3 * It will automatically try to run fsck programs in parallel if the
4 * devices are on separate spindles. It is based on the same ideas as
5 * the generic front end for fsck by David Engel and Fred van Kempen,
6 * but it has been completely rewritten from scratch to support
9 * Written by Theodore Ts'o, <tytso@mit.edu>
11 * Miquel van Smoorenburg (miquels@drinkel.ow.org) 20-Oct-1994:
12 * o Changed -t fstype to behave like with mount when -A (all file
13 * systems) or -M (like mount) is specified.
14 * o fsck looks if it can find the fsck.type program to decide
15 * if it should ignore the fs type. This way more fsck programs
16 * can be added without changing this front-end.
17 * o -R flag skip root file system.
19 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
20 * 2001, 2002, 2003, 2004, 2005 by Theodore Ts'o.
22 * Copyright (C) 2009-2014 Karel Zak <kzak@redhat.com>
24 * This file may be redistributed under the terms of the GNU Public
28 #define _XOPEN_SOURCE 600 /* for inclusion of sa_handler in Solaris */
30 #include <sys/types.h>
46 #include <sys/resource.h>
52 #include "pathnames.h"
53 #include "exitcodes.h"
55 #include "closestream.h"
56 #include "fileutils.h"
57 #include "monotonic.h"
59 #define XALLOC_EXIT_CODE FSCK_EX_ERROR
62 #ifndef DEFAULT_FSTYPE
63 # define DEFAULT_FSTYPE "ext2"
66 #define MAX_DEVICES 32
69 #define FSCK_RUNTIME_DIRNAME "/run/fsck"
71 static const char *ignored_types
[] = {
78 static const char *really_wanted
[] = {
89 * Internal structure for mount tabel entries.
95 unsigned int stacked
:1,
101 * Structure to allow exit codes to be stored
103 struct fsck_instance
{
105 int flags
; /* FLAG_{DONE|PROGRESS} */
107 int lock
; /* flock()ed lockpath file descriptor or -1 */
108 char *lockpath
; /* /run/fsck/<diskname>.lock or NULL */
111 struct timeval start_time
;
112 struct timeval end_time
;
116 struct rusage rusage
;
117 struct libmnt_fs
*fs
;
118 struct fsck_instance
*next
;
122 #define FLAG_PROGRESS 2
125 * Global variables for options
127 static char *devices
[MAX_DEVICES
];
128 static char *args
[MAX_ARGS
];
129 static int num_devices
, num_args
;
134 static int noexecute
;
135 static int serialize
;
136 static int skip_root
;
137 static int ignore_mounted
;
139 static int parallel_root
;
141 static int progress_fd
;
142 static int force_all_parallel
;
143 static int report_stats
;
145 static int num_running
;
146 static int max_running
;
148 static volatile int cancel_requested
;
149 static int kill_sent
;
151 static struct fsck_instance
*instance_list
;
153 static const char fsck_prefix_path
[] = FS_SEARCH_PATH
;
154 static char *fsck_path
;
156 /* parsed fstab and mtab */
157 static struct libmnt_table
*fstab
, *mtab
;
158 static struct libmnt_cache
*mntcache
;
160 static int count_slaves(dev_t disk
);
162 static int string_to_int(const char *s
)
167 l
= strtol(s
, &p
, 0);
168 if (*p
|| l
== LONG_MIN
|| l
== LONG_MAX
|| l
< 0 || l
> INT_MAX
)
174 /* Do we really really want to check this fs? */
175 static int fs_check_required(const char *type
)
179 for(i
= 0; i
< ARRAY_SIZE(really_wanted
); i
++) {
180 if (strcmp(type
, really_wanted
[i
]) == 0)
187 static int is_mounted(struct libmnt_fs
*fs
)
192 src
= mnt_fs_get_source(fs
);
196 mntcache
= mnt_new_cache();
198 mtab
= mnt_new_table();
200 err(FSCK_EX_ERROR
, ("failed to initialize libmount table"));
201 mnt_table_set_cache(mtab
, mntcache
);
202 mnt_table_parse_mtab(mtab
, NULL
);
205 rc
= mnt_table_find_source(mtab
, src
, MNT_ITER_BACKWARD
) ? 1 : 0;
208 printf(_("%s is mounted\n"), src
);
210 printf(_("%s is not mounted\n"), src
);
215 static int ignore(struct libmnt_fs
*);
217 static struct fsck_fs_data
*fs_create_data(struct libmnt_fs
*fs
)
219 struct fsck_fs_data
*data
= mnt_fs_get_userdata(fs
);
222 data
= xcalloc(1, sizeof(*data
));
223 mnt_fs_set_userdata(fs
, data
);
229 * fs from fstab might contains real device name as well as symlink,
230 * LABEL or UUID, this function returns canonicalized result.
232 static const char *fs_get_device(struct libmnt_fs
*fs
)
234 struct fsck_fs_data
*data
= mnt_fs_get_userdata(fs
);
236 if (!data
|| !data
->eval_device
) {
237 const char *spec
= mnt_fs_get_source(fs
);
240 data
= fs_create_data(fs
);
242 data
->eval_device
= 1;
243 data
->device
= mnt_resolve_spec(spec
, mnt_table_get_cache(fstab
));
245 data
->device
= xstrdup(spec
);
251 static dev_t
fs_get_disk(struct libmnt_fs
*fs
, int check
)
253 struct fsck_fs_data
*data
;
257 data
= mnt_fs_get_userdata(fs
);
258 if (data
&& data
->disk
)
264 if (mnt_fs_is_netfs(fs
) || mnt_fs_is_pseudofs(fs
))
267 device
= fs_get_device(fs
);
271 data
= fs_create_data(fs
);
273 if (!stat(device
, &st
) &&
274 !blkid_devno_to_wholedisk(st
.st_rdev
, NULL
, 0, &data
->disk
)) {
277 data
->stacked
= count_slaves(data
->disk
) > 0 ? 1 : 0;
283 static int fs_is_stacked(struct libmnt_fs
*fs
)
285 struct fsck_fs_data
*data
= mnt_fs_get_userdata(fs
);
286 return data
? data
->stacked
: 0;
289 static int fs_is_done(struct libmnt_fs
*fs
)
291 struct fsck_fs_data
*data
= mnt_fs_get_userdata(fs
);
292 return data
? data
->done
: 0;
295 static void fs_set_done(struct libmnt_fs
*fs
)
297 struct fsck_fs_data
*data
= fs_create_data(fs
);
303 static int is_irrotational_disk(dev_t disk
)
310 rc
= snprintf(path
, sizeof(path
),
311 "/sys/dev/block/%d:%d/queue/rotational",
312 major(disk
), minor(disk
));
314 if (rc
< 0 || (unsigned int) (rc
+ 1) > sizeof(path
))
317 f
= fopen(path
, "r");
321 rc
= fscanf(f
, "%d", &x
);
324 warn(_("cannot read %s"), path
);
326 warnx(_("parse error: %s"), path
);
330 return rc
== 1 ? !x
: 0;
333 static void lock_disk(struct fsck_instance
*inst
)
335 dev_t disk
= fs_get_disk(inst
->fs
, 1);
336 char *diskpath
= NULL
, *diskname
;
340 if (!disk
|| is_irrotational_disk(disk
))
343 diskpath
= blkid_devno_to_devname(disk
);
347 if (access(FSCK_RUNTIME_DIRNAME
, F_OK
) != 0) {
348 int rc
= mkdir(FSCK_RUNTIME_DIRNAME
,
350 S_IRUSR
|S_IRGRP
|S_IROTH
|
351 S_IXUSR
|S_IXGRP
|S_IXOTH
);
352 if (rc
&& errno
!= EEXIST
) {
353 warn(_("cannot create directory %s"),
354 FSCK_RUNTIME_DIRNAME
);
359 diskname
= stripoff_last_component(diskpath
);
363 xasprintf(&inst
->lockpath
, FSCK_RUNTIME_DIRNAME
"/%s.lock", diskname
);
366 printf(_("Locking disk by %s ... "), inst
->lockpath
);
368 inst
->lock
= open(inst
->lockpath
, O_RDONLY
|O_CREAT
|O_CLOEXEC
,
369 S_IWUSR
|S_IRUSR
|S_IRGRP
|S_IROTH
);
370 if (inst
->lock
>= 0) {
373 /* inform users that we're waiting on the lock */
375 (rc
= flock(inst
->lock
, LOCK_EX
| LOCK_NB
)) != 0 &&
376 errno
== EWOULDBLOCK
)
377 printf(_("(waiting) "));
379 if (rc
!= 0 && flock(inst
->lock
, LOCK_EX
) != 0) {
380 close(inst
->lock
); /* failed */
386 /* TRANSLATORS: These are followups to "Locking disk...". */
387 printf("%s.\n", inst
->lock
>= 0 ? _("succeeded") : _("failed"));
391 if (inst
->lock
< 0) {
392 free(inst
->lockpath
);
393 inst
->lockpath
= NULL
;
399 static void unlock_disk(struct fsck_instance
*inst
)
405 printf(_("Unlocking %s.\n"), inst
->lockpath
);
407 close(inst
->lock
); /* unlock */
408 unlink(inst
->lockpath
);
410 free(inst
->lockpath
);
413 inst
->lockpath
= NULL
;
416 static void free_instance(struct fsck_instance
*i
)
427 static struct libmnt_fs
*add_dummy_fs(const char *device
)
429 struct libmnt_fs
*fs
= mnt_new_fs();
431 if (fs
&& mnt_fs_set_source(fs
, device
) == 0 &&
432 mnt_table_add_fs(fstab
, fs
) == 0) {
438 err(FSCK_EX_ERROR
, _("failed to setup description for %s"), device
);
441 static void fs_interpret_type(struct libmnt_fs
*fs
)
444 const char *type
= mnt_fs_get_fstype(fs
);
446 if (type
&& strcmp(type
, "auto") != 0)
449 mnt_fs_set_fstype(fs
, NULL
);
451 device
= fs_get_device(fs
);
455 struct libmnt_cache
*cache
= mnt_table_get_cache(fstab
);
457 tp
= mnt_get_fstype(device
, &ambi
, cache
);
459 mnt_fs_set_fstype(fs
, tp
);
465 static int parser_errcb(struct libmnt_table
*tb
__attribute__ ((__unused__
)),
466 const char *filename
, int line
)
468 warnx(_("%s: parse error at line %d -- ignore"), filename
, line
);
473 * Load the filesystem database from /etc/fstab
475 static void load_fs_info(void)
479 fstab
= mnt_new_table();
481 err(FSCK_EX_ERROR
, ("failed to initialize libmount table"));
483 mnt_table_set_parser_errcb(fstab
, parser_errcb
);
484 mnt_table_set_cache(fstab
, mntcache
);
489 * Let's follow libmount defauls if $FSTAB_FILE is not specified
491 path
= getenv("FSTAB_FILE");
493 if (mnt_table_parse_fstab(fstab
, path
)) {
495 path
= mnt_get_fstab_path();
497 /* don't print error when there is no fstab at all */
498 if (access(path
, F_OK
) == 0) {
500 warn(_("%s: failed to parse fstab"), path
);
502 warnx(_("%s: failed to parse fstab"), path
);
508 * Lookup filesys in /etc/fstab and return the corresponding entry.
509 * The @path has to be real path (no TAG) by mnt_resolve_spec().
511 static struct libmnt_fs
*lookup(char *path
)
513 struct libmnt_fs
*fs
;
518 fs
= mnt_table_find_srcpath(fstab
, path
, MNT_ITER_FORWARD
);
521 * Maybe mountpoint has been specified on fsck command line.
522 * Yeah, crazy feature...
524 * Note that mnt_table_find_target() may canonicalize paths in
525 * the fstab to support symlinks. This is really unwanted,
526 * because readlink() on mountpoints may trigger automounts.
528 * So, disable the cache and compare the paths as strings
529 * without care about symlinks...
531 mnt_table_set_cache(fstab
, NULL
);
532 fs
= mnt_table_find_target(fstab
, path
, MNT_ITER_FORWARD
);
533 mnt_table_set_cache(fstab
, mntcache
);
538 /* Find fsck program for a given fs type. */
539 static char *find_fsck(const char *type
)
543 static char prog
[256];
544 char *p
= xstrdup(fsck_path
);
547 /* Are we looking for a program or just a type? */
548 tpl
= (strncmp(type
, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
550 for(s
= strtok(p
, ":"); s
; s
= strtok(NULL
, ":")) {
551 sprintf(prog
, tpl
, s
, type
);
552 if (stat(prog
, &st
) == 0)
557 return(s
? prog
: NULL
);
560 static int progress_active(void)
562 struct fsck_instance
*inst
;
564 for (inst
= instance_list
; inst
; inst
= inst
->next
) {
565 if (inst
->flags
& FLAG_DONE
)
567 if (inst
->flags
& FLAG_PROGRESS
)
574 * Process run statistics for finished fsck instances.
576 * If report_stats is 0, do nothing, otherwise print a selection of
577 * interesting rusage statistics as well as elapsed wallclock time.
579 static void print_stats(struct fsck_instance
*inst
)
581 struct timeval delta
;
583 if (!inst
|| !report_stats
|| noexecute
)
586 timersub(&inst
->end_time
, &inst
->start_time
, &delta
);
588 fprintf(stdout
, "%s: status %d, rss %ld, "
589 "real %ld.%06ld, user %d.%06d, sys %d.%06d\n",
590 fs_get_device(inst
->fs
),
592 inst
->rusage
.ru_maxrss
,
593 delta
.tv_sec
, delta
.tv_usec
,
594 (int)inst
->rusage
.ru_utime
.tv_sec
,
595 (int)inst
->rusage
.ru_utime
.tv_usec
,
596 (int)inst
->rusage
.ru_stime
.tv_sec
,
597 (int)inst
->rusage
.ru_stime
.tv_usec
);
601 * Execute a particular fsck program, and link it into the list of
602 * child processes we are waiting for.
604 static int execute(const char *progname
, const char *progpath
,
605 const char *type
, struct libmnt_fs
*fs
, int interactive
)
609 struct fsck_instance
*inst
, *p
;
612 inst
= xcalloc(1, sizeof(*inst
));
614 argv
[0] = xstrdup(progname
);
617 for (i
=0; i
<num_args
; i
++)
618 argv
[argc
++] = xstrdup(args
[i
]);
621 if ((strcmp(type
, "ext2") == 0) ||
622 (strcmp(type
, "ext3") == 0) ||
623 (strcmp(type
, "ext4") == 0) ||
624 (strcmp(type
, "ext4dev") == 0)) {
628 if (!progress_active()) {
629 snprintf(tmp
, 80, "-C%d", progress_fd
);
630 inst
->flags
|= FLAG_PROGRESS
;
631 } else if (progress_fd
)
632 snprintf(tmp
, 80, "-C%d", progress_fd
* -1);
634 argv
[argc
++] = xstrdup(tmp
);
638 argv
[argc
++] = xstrdup(fs_get_device(fs
));
641 if (verbose
|| noexecute
) {
642 const char *tgt
= mnt_fs_get_target(fs
);
645 tgt
= fs_get_device(fs
);
646 printf("[%s (%d) -- %s] ", progpath
, num_running
, tgt
);
647 for (i
=0; i
< argc
; i
++)
648 printf("%s ", argv
[i
]);
659 /* Fork and execute the correct program. */
662 else if ((pid
= fork()) < 0) {
663 warn(_("fork failed"));
666 } else if (pid
== 0) {
669 execv(progpath
, argv
);
670 err(FSCK_EX_ERROR
, _("%s: execute failed"), progpath
);
673 for (i
=0; i
< argc
; i
++)
677 inst
->prog
= xstrdup(progname
);
678 inst
->type
= xstrdup(type
);
679 gettime_monotonic(&inst
->start_time
);
683 * Find the end of the list, so we add the instance on at the end.
685 for (p
= instance_list
; p
&& p
->next
; p
= p
->next
);
690 instance_list
= inst
;
696 * Send a signal to all outstanding fsck child processes
698 static int kill_all(int signum
)
700 struct fsck_instance
*inst
;
703 for (inst
= instance_list
; inst
; inst
= inst
->next
) {
704 if (inst
->flags
& FLAG_DONE
)
706 kill(inst
->pid
, signum
);
713 * Wait for one child process to exit; when it does, unlink it from
714 * the list of executing child processes, and return it.
716 static struct fsck_instance
*wait_one(int flags
)
720 struct fsck_instance
*inst
, *inst2
, *prev
;
722 struct rusage rusage
;
728 inst
= instance_list
;
731 while (inst
->next
&& (random() & 1)) {
736 inst
->exit_status
= 0;
741 * gcc -Wall fails saving throw against stupidity
742 * (inst and prev are thought to be uninitialized variables)
747 pid
= wait4(-1, &status
, flags
, &rusage
);
748 if (cancel_requested
&& !kill_sent
) {
752 if ((pid
== 0) && (flags
& WNOHANG
))
755 if ((errno
== EINTR
) || (errno
== EAGAIN
))
757 if (errno
== ECHILD
) {
758 warnx(_("wait: no more child process?!?"));
761 warn(_("waitpid failed"));
764 for (prev
= 0, inst
= instance_list
;
766 prev
= inst
, inst
= inst
->next
) {
767 if (inst
->pid
== pid
)
772 if (WIFEXITED(status
))
773 status
= WEXITSTATUS(status
);
774 else if (WIFSIGNALED(status
)) {
775 sig
= WTERMSIG(status
);
777 status
= FSCK_EX_UNCORRECTED
;
779 warnx(_("Warning... %s for device %s exited "
781 inst
->prog
, fs_get_device(inst
->fs
), sig
);
782 status
= FSCK_EX_ERROR
;
785 warnx(_("%s %s: status is %x, should never happen."),
786 inst
->prog
, fs_get_device(inst
->fs
), status
);
787 status
= FSCK_EX_ERROR
;
790 inst
->exit_status
= status
;
791 inst
->flags
|= FLAG_DONE
;
792 gettime_monotonic(&inst
->end_time
);
793 memcpy(&inst
->rusage
, &rusage
, sizeof(struct rusage
));
795 if (progress
&& (inst
->flags
& FLAG_PROGRESS
) &&
796 !progress_active()) {
797 for (inst2
= instance_list
; inst2
; inst2
= inst2
->next
) {
798 if (inst2
->flags
& FLAG_DONE
)
800 if (strcmp(inst2
->type
, "ext2") &&
801 strcmp(inst2
->type
, "ext3") &&
802 strcmp(inst2
->type
, "ext4") &&
803 strcmp(inst2
->type
, "ext4dev"))
806 * If we've just started the fsck, wait a tiny
807 * bit before sending the kill, to give it
808 * time to set up the signal handler
810 if (inst2
->start_time
.tv_sec
< time(0) + 2) {
813 kill(inst2
->pid
, SIGUSR1
);
817 kill(inst2
->pid
, SIGUSR1
);
818 inst2
->flags
|= FLAG_PROGRESS
;
824 prev
->next
= inst
->next
;
826 instance_list
= inst
->next
;
831 printf(_("Finished with %s (exit status %d)\n"),
832 fs_get_device(inst
->fs
), inst
->exit_status
);
837 #define FLAG_WAIT_ALL 0
838 #define FLAG_WAIT_ATLEAST_ONE 1
840 * Wait until all executing child processes have exited; return the
841 * logical OR of all of their exit code values.
843 static int wait_many(int flags
)
845 struct fsck_instance
*inst
;
846 int global_status
= 0;
849 while ((inst
= wait_one(wait_flags
))) {
850 global_status
|= inst
->exit_status
;
853 if (noexecute
&& (flags
& WNOHANG
) && !(random() % 3))
856 if (flags
& FLAG_WAIT_ATLEAST_ONE
)
857 wait_flags
= WNOHANG
;
859 return global_status
;
863 * Run the fsck program on a particular device
865 * If the type is specified using -t, and it isn't prefixed with "no"
866 * (as in "noext2") and only one filesystem type is specified, then
867 * use that type regardless of what is specified in /etc/fstab.
869 * If the type isn't specified by the user, then use either the type
870 * specified in /etc/fstab, or DEFAULT_FSTYPE.
872 static int fsck_device(struct libmnt_fs
*fs
, int interactive
)
874 char progname
[80], *progpath
;
878 fs_interpret_type(fs
);
880 type
= mnt_fs_get_fstype(fs
);
882 if (type
&& strcmp(type
, "auto") != 0)
884 else if (fstype
&& strncmp(fstype
, "no", 2) &&
885 strncmp(fstype
, "opts=", 5) && strncmp(fstype
, "loop", 4) &&
886 !strchr(fstype
, ','))
889 type
= DEFAULT_FSTYPE
;
891 sprintf(progname
, "fsck.%s", type
);
892 progpath
= find_fsck(progname
);
893 if (progpath
== NULL
) {
894 if (fs_check_required(type
)) {
902 retval
= execute(progname
, progpath
, type
, fs
, interactive
);
909 warnx(_("error %d (%m) while executing fsck.%s for %s"),
910 retval
, type
, fs_get_device(fs
));
911 return FSCK_EX_ERROR
;
916 * Deal with the fsck -t argument.
918 struct fs_type_compile
{
924 #define FS_TYPE_NORMAL 0
925 #define FS_TYPE_OPT 1
926 #define FS_TYPE_NEGOPT 2
928 static void compile_fs_type(char *fs_type
, struct fs_type_compile
*cmp
)
932 int negate
, first_negate
= 1;
935 for (cp
=fs_type
; *cp
; cp
++) {
941 cmp
->list
= xcalloc(num
, sizeof(char *));
942 cmp
->type
= xcalloc(num
, sizeof(int));
948 list
= xstrdup(fs_type
);
950 s
= strtok(list
, ",");
953 if (strncmp(s
, "no", 2) == 0) {
956 } else if (*s
== '!') {
960 if (strcmp(s
, "loop") == 0)
961 /* loop is really short-hand for opts=loop */
962 goto loop_special_case
;
963 else if (strncmp(s
, "opts=", 5) == 0) {
966 cmp
->type
[num
] = negate
? FS_TYPE_NEGOPT
: FS_TYPE_OPT
;
969 cmp
->negate
= negate
;
972 if ((negate
&& !cmp
->negate
) ||
973 (!negate
&& cmp
->negate
)) {
975 _("Either all or none of the filesystem types passed to -t must be prefixed\n"
976 "with 'no' or '!'."));
980 cmp
->list
[num
++] = xstrdup(s
);
981 s
= strtok(NULL
, ",");
987 * This function returns true if a particular option appears in a
988 * comma-delimited options list
990 static int opt_in_list(const char *opt
, const char *optlist
)
996 list
= xstrdup(optlist
);
998 s
= strtok(list
, ",");
1000 if (strcmp(s
, opt
) == 0) {
1004 s
= strtok(NULL
, ",");
1010 /* See if the filesystem matches the criteria given by the -t option */
1011 static int fs_match(struct libmnt_fs
*fs
, struct fs_type_compile
*cmp
)
1013 int n
, ret
= 0, checked_type
= 0;
1016 if (cmp
->list
== 0 || cmp
->list
[0] == 0)
1019 for (n
=0; (cp
= cmp
->list
[n
]); n
++) {
1020 switch (cmp
->type
[n
]) {
1021 case FS_TYPE_NORMAL
:
1023 const char *type
= mnt_fs_get_fstype(fs
);
1026 if (type
&& strcmp(cp
, type
) == 0)
1030 case FS_TYPE_NEGOPT
:
1031 if (opt_in_list(cp
, mnt_fs_get_options(fs
)))
1035 if (!opt_in_list(cp
, mnt_fs_get_options(fs
)))
1040 if (checked_type
== 0)
1042 return (cmp
->negate
? !ret
: ret
);
1046 * Check if a device exists
1048 static int device_exists(const char *device
)
1052 if (stat(device
, &st
) == -1)
1054 if (!S_ISBLK(st
.st_mode
))
1059 static int fs_ignored_type(struct libmnt_fs
*fs
)
1061 const char **ip
, *type
;
1063 if (mnt_fs_is_netfs(fs
) || mnt_fs_is_pseudofs(fs
) || mnt_fs_is_swaparea(fs
))
1066 type
= mnt_fs_get_fstype(fs
);
1068 for(ip
= ignored_types
; type
&& *ip
; ip
++) {
1069 if (strcmp(type
, *ip
) == 0)
1076 /* Check if we should ignore this filesystem. */
1077 static int ignore(struct libmnt_fs
*fs
)
1082 * If the pass number is 0, ignore it.
1084 if (mnt_fs_get_passno(fs
) == 0)
1088 * If this is a bind mount, ignore it.
1090 if (opt_in_list("bind", mnt_fs_get_options(fs
))) {
1091 warnx(_("%s: skipping bad line in /etc/fstab: "
1092 "bind mount with nonzero fsck pass number"),
1093 mnt_fs_get_target(fs
));
1098 * ignore devices that don't exist and have the "nofail" mount option
1100 if (!device_exists(fs_get_device(fs
))) {
1101 if (opt_in_list("nofail", mnt_fs_get_options(fs
))) {
1103 printf(_("%s: skipping nonexistent device\n"),
1108 printf(_("%s: nonexistent device (\"nofail\" fstab "
1109 "option may be used to skip this device)\n"),
1113 fs_interpret_type(fs
);
1116 * If a specific fstype is specified, and it doesn't match,
1119 if (!fs_match(fs
, &fs_type_compiled
))
1122 type
= mnt_fs_get_fstype(fs
);
1125 printf(_("%s: skipping unknown filesystem type\n"),
1130 /* Are we ignoring this type? */
1131 if (fs_ignored_type(fs
))
1136 /* See if the <fsck.fs> program is available. */
1137 if (find_fsck(type
) == NULL
) {
1138 if (fs_check_required(type
))
1139 warnx(_("cannot check %s: fsck.%s not found"),
1140 fs_get_device(fs
), type
);
1144 /* We can and want to check this file system type. */
1148 static int count_slaves(dev_t disk
)
1152 char dirname
[PATH_MAX
];
1155 snprintf(dirname
, sizeof(dirname
),
1156 "/sys/dev/block/%u:%u/slaves/",
1157 major(disk
), minor(disk
));
1159 if (!(dir
= opendir(dirname
)))
1162 while ((dp
= readdir(dir
)) != 0) {
1163 #ifdef _DIRENT_HAVE_D_TYPE
1164 if (dp
->d_type
!= DT_UNKNOWN
&& dp
->d_type
!= DT_LNK
)
1167 if (dp
->d_name
[0] == '.' &&
1168 ((dp
->d_name
[1] == 0) ||
1169 ((dp
->d_name
[1] == '.') && (dp
->d_name
[2] == 0))))
1180 * Returns TRUE if a partition on the same disk is already being
1183 static int disk_already_active(struct libmnt_fs
*fs
)
1185 struct fsck_instance
*inst
;
1188 if (force_all_parallel
)
1191 if (instance_list
&& fs_is_stacked(instance_list
->fs
))
1192 /* any instance for a stacked device is already running */
1195 disk
= fs_get_disk(fs
, 1);
1198 * If we don't know the base device, assume that the device is
1199 * already active if there are any fsck instances running.
1201 * Don't check a stacked device with any other disk too.
1203 if (!disk
|| fs_is_stacked(fs
))
1204 return (instance_list
!= 0);
1206 for (inst
= instance_list
; inst
; inst
= inst
->next
) {
1207 dev_t idisk
= fs_get_disk(inst
->fs
, 0);
1209 if (!idisk
|| disk
== idisk
)
1216 /* Check all file systems, using the /etc/fstab table. */
1217 static int check_all(void)
1219 int not_done_yet
= 1;
1222 int status
= FSCK_EX_OK
;
1224 struct libmnt_fs
*fs
;
1225 struct libmnt_iter
*itr
= mnt_new_iter(MNT_ITER_FORWARD
);
1228 err(FSCK_EX_ERROR
, _("failed to allocate iterator"));
1231 * Do an initial scan over the filesystem; mark filesystems
1232 * which should be ignored as done, and resolve any "auto"
1233 * filesystem types (done as a side-effect of calling ignore()).
1235 while (mnt_table_next_fs(fstab
, itr
, &fs
) == 0) {
1243 fputs(_("Checking all file systems.\n"), stdout
);
1246 * Find and check the root filesystem.
1248 if (!parallel_root
) {
1249 fs
= mnt_table_find_target(fstab
, "/", MNT_ITER_FORWARD
);
1253 !(ignore_mounted
&& is_mounted(fs
))) {
1254 status
|= fsck_device(fs
, 1);
1255 status
|= wait_many(FLAG_WAIT_ALL
);
1256 if (status
> FSCK_EX_NONDESTRUCT
) {
1266 * This is for the bone-headed user who enters the root
1267 * filesystem twice. Skip root will skep all root entries.
1270 mnt_reset_iter(itr
, MNT_ITER_FORWARD
);
1272 while(mnt_table_next_fs(fstab
, itr
, &fs
) == 0) {
1273 const char *tgt
= mnt_fs_get_target(fs
);
1275 if (tgt
&& strcmp(tgt
, "/") == 0)
1280 while (not_done_yet
) {
1284 mnt_reset_iter(itr
, MNT_ITER_FORWARD
);
1286 while(mnt_table_next_fs(fstab
, itr
, &fs
) == 0) {
1288 if (cancel_requested
)
1293 * If the filesystem's pass number is higher
1294 * than the current pass number, then we don't
1297 if (mnt_fs_get_passno(fs
) > passno
) {
1301 if (ignore_mounted
&& is_mounted(fs
)) {
1306 * If a filesystem on a particular device has
1307 * already been spawned, then we need to defer
1308 * this to another pass.
1310 if (disk_already_active(fs
)) {
1315 * Spawn off the fsck process
1317 status
|= fsck_device(fs
, serialize
);
1321 * Only do one filesystem at a time, or if we
1322 * have a limit on the number of fsck's extant
1323 * at one time, apply that limit.
1326 (max_running
&& (num_running
>= max_running
))) {
1331 if (cancel_requested
)
1334 printf(_("--waiting-- (pass %d)\n"), passno
);
1336 status
|= wait_many(pass_done
? FLAG_WAIT_ALL
:
1337 FLAG_WAIT_ATLEAST_ONE
);
1340 printf("----------------------------------\n");
1346 if (cancel_requested
&& !kill_sent
) {
1351 status
|= wait_many(FLAG_WAIT_ATLEAST_ONE
);
1356 static void __attribute__((__noreturn__
)) usage(FILE *out
)
1358 fputs(USAGE_HEADER
, out
);
1359 fprintf(out
, _(" %s [options] -- [fs-options] [<filesystem> ...]\n"),
1360 program_invocation_short_name
);
1362 fputs(USAGE_SEPARATOR
, out
);
1363 fputs(_("Check and repair a Linux filesystem.\n"), out
);
1365 fputs(USAGE_OPTIONS
, out
);
1366 fputs(_(" -A check all filesystems\n"), out
);
1367 fputs(_(" -C [<fd>] display progress bar; file descriptor is for GUIs\n"), out
);
1368 fputs(_(" -l lock the device to guarantee exclusive access\n"), out
);
1369 fputs(_(" -M do not check mounted filesystems\n"), out
);
1370 fputs(_(" -N do not execute, just show what would be done\n"), out
);
1371 fputs(_(" -P check filesystems in parallel, including root\n"), out
);
1372 fputs(_(" -R skip root filesystem; useful only with '-A'\n"), out
);
1373 fputs(_(" -r report statistics for each device checked\n"), out
);
1374 fputs(_(" -s serialize the checking operations\n"), out
);
1375 fputs(_(" -T do not show the title on startup\n"), out
);
1376 fputs(_(" -t <type> specify filesystem types to be checked;\n"
1377 " <type> is allowed to be a comma-separated list\n"), out
);
1378 fputs(_(" -V explain what is being done\n"), out
);
1379 fputs(_(" -? display this help and exit\n"), out
);
1381 fputs(USAGE_SEPARATOR
, out
);
1382 fputs(_("See the specific fsck.* commands for available fs-options."), out
);
1383 fprintf(out
, USAGE_MAN_TAIL("fsck(8)"));
1385 exit(out
== stderr
? FSCK_EX_USAGE
: FSCK_EX_OK
);
1388 static void signal_cancel(int sig
__attribute__((__unused__
)))
1393 static void parse_argv(int argc
, char *argv
[])
1396 char *arg
, *dev
, *tmp
= 0;
1399 int opts_for_fsck
= 0;
1400 struct sigaction sa
;
1403 * Set up signal action
1405 memset(&sa
, 0, sizeof(struct sigaction
));
1406 sa
.sa_handler
= signal_cancel
;
1407 sigaction(SIGINT
, &sa
, 0);
1408 sigaction(SIGTERM
, &sa
, 0);
1414 for (i
=1; i
< argc
; i
++) {
1418 if ((arg
[0] == '/' && !opts_for_fsck
) || strchr(arg
, '=')) {
1419 if (num_devices
>= MAX_DEVICES
)
1420 errx(FSCK_EX_ERROR
, _("too many devices"));
1422 dev
= mnt_resolve_spec(arg
, mntcache
);
1424 if (!dev
&& strchr(arg
, '=')) {
1426 * Check to see if we failed because
1427 * /proc/partitions isn't found.
1429 if (access(_PATH_PROC_PARTITIONS
, R_OK
) < 0) {
1430 warn(_("cannot open %s"),
1431 _PATH_PROC_PARTITIONS
);
1432 errx(FSCK_EX_ERROR
, _("Is /proc mounted?"));
1435 * Check to see if this is because
1436 * we're not running as root
1440 _("must be root to scan for matching filesystems: %s"),
1444 _("couldn't find matching filesystem: %s"),
1447 devices
[num_devices
++] = dev
? dev
: xstrdup(arg
);
1450 if (arg
[0] != '-' || opts_for_fsck
) {
1451 if (num_args
>= MAX_ARGS
)
1452 errx(FSCK_EX_ERROR
, _("too many arguments"));
1453 args
[num_args
++] = xstrdup(arg
);
1456 for (j
=1; arg
[j
]; j
++) {
1457 if (opts_for_fsck
) {
1458 options
[++opt
] = arg
[j
];
1467 if (arg
[j
+1]) { /* -C<fd> */
1468 progress_fd
= string_to_int(arg
+j
+1);
1469 if (progress_fd
< 0)
1473 } else if (i
+1 < argc
&& *argv
[i
+1] != '-') { /* -C <fd> */
1474 progress_fd
= string_to_int(argv
[i
+1]);
1475 if (progress_fd
< 0)
1516 else if ((i
+1) < argc
)
1520 fstype
= xstrdup(tmp
);
1521 compile_fs_type(fstype
, &fs_type_compiled
);
1530 options
[++opt
] = arg
[j
];
1537 options
[++opt
] = '\0';
1538 if (num_args
>= MAX_ARGS
)
1539 errx(FSCK_EX_ERROR
, _("too many arguments"));
1540 args
[num_args
++] = xstrdup(options
);
1544 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1545 force_all_parallel
++;
1546 if ((tmp
= getenv("FSCK_MAX_INST")))
1547 max_running
= atoi(tmp
);
1550 int main(int argc
, char *argv
[])
1553 int interactive
= 0;
1554 char *oldpath
= getenv("PATH");
1555 struct libmnt_fs
*fs
;
1557 setvbuf(stdout
, NULL
, _IONBF
, BUFSIZ
);
1558 setvbuf(stderr
, NULL
, _IONBF
, BUFSIZ
);
1560 setlocale(LC_MESSAGES
, "");
1561 setlocale(LC_CTYPE
, "");
1562 bindtextdomain(PACKAGE
, LOCALEDIR
);
1563 textdomain(PACKAGE
);
1564 atexit(close_stdout
);
1566 mnt_init_debug(0); /* init libmount debug mask */
1567 mntcache
= mnt_new_cache(); /* no fatal error if failed */
1569 parse_argv(argc
, argv
);
1572 printf(UTIL_LINUX_VERSION
);
1576 /* Update our search path to include uncommon directories. */
1578 fsck_path
= xmalloc (strlen (fsck_prefix_path
) + 1 +
1579 strlen (oldpath
) + 1);
1580 strcpy (fsck_path
, fsck_prefix_path
);
1581 strcat (fsck_path
, ":");
1582 strcat (fsck_path
, oldpath
);
1584 fsck_path
= xstrdup(fsck_prefix_path
);
1587 if ((num_devices
== 1) || (serialize
))
1590 if (lockdisk
&& (doall
|| num_devices
> 1)) {
1591 warnx(_("the -l option can be used with one "
1592 "device only -- ignore"));
1596 /* If -A was specified ("check all"), do that! */
1600 if (num_devices
== 0) {
1605 for (i
= 0 ; i
< num_devices
; i
++) {
1606 if (cancel_requested
) {
1613 fs
= lookup(devices
[i
]);
1615 fs
= add_dummy_fs(devices
[i
]);
1616 else if (fs_ignored_type(fs
))
1618 if (ignore_mounted
&& is_mounted(fs
))
1620 status
|= fsck_device(fs
, interactive
);
1622 (max_running
&& (num_running
>= max_running
))) {
1623 struct fsck_instance
*inst
;
1627 status
|= inst
->exit_status
;
1628 free_instance(inst
);
1631 printf("----------------------------------\n");
1634 status
|= wait_many(FLAG_WAIT_ALL
);
1636 mnt_unref_cache(mntcache
);
1637 mnt_unref_table(fstab
);
1638 mnt_unref_table(mtab
);