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, 2012 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"
57 #define XALLOC_EXIT_CODE FSCK_EX_ERROR
60 #ifndef DEFAULT_FSTYPE
61 # define DEFAULT_FSTYPE "ext2"
64 #define MAX_DEVICES 32
67 static const char *ignored_types
[] = {
74 static const char *really_wanted
[] = {
88 * Internal structure for mount tabel entries.
94 unsigned int stacked
:1,
100 * Structure to allow exit codes to be stored
102 struct fsck_instance
{
104 int flags
; /* FLAG_{DONE|PROGRESS} */
105 int lock
; /* flock()ed whole disk file descriptor or -1 */
107 struct timeval start_time
;
108 struct timeval end_time
;
112 struct rusage rusage
;
113 struct libmnt_fs
*fs
;
114 struct fsck_instance
*next
;
118 #define FLAG_PROGRESS 2
121 * Global variables for options
123 static char *devices
[MAX_DEVICES
];
124 static char *args
[MAX_ARGS
];
125 static int num_devices
, num_args
;
130 static int noexecute
;
131 static int serialize
;
132 static int skip_root
;
133 static int ignore_mounted
;
135 static int parallel_root
;
137 static int progress_fd
;
138 static int force_all_parallel
;
139 static int report_stats
;
141 static int num_running
;
142 static int max_running
;
144 static volatile int cancel_requested
;
145 static int kill_sent
;
147 static struct fsck_instance
*instance_list
;
149 static const char fsck_prefix_path
[] = FS_SEARCH_PATH
;
150 static char *fsck_path
;
152 /* parsed fstab and mtab */
153 static struct libmnt_table
*fstab
, *mtab
;
154 static struct libmnt_cache
*mntcache
;
156 static int count_slaves(dev_t disk
);
158 static int string_to_int(const char *s
)
163 l
= strtol(s
, &p
, 0);
164 if (*p
|| l
== LONG_MIN
|| l
== LONG_MAX
|| l
< 0 || l
> INT_MAX
)
170 static int is_mounted(struct libmnt_fs
*fs
)
175 mtab
= mnt_new_table();
177 err(FSCK_EX_ERROR
, ("failed to initialize libmount table"));
179 mntcache
= mnt_new_cache();
180 mnt_table_set_cache(mtab
, mntcache
);
181 mnt_table_parse_mtab(mtab
, NULL
);
184 rc
= mnt_table_is_fs_mounted(mtab
, fs
);
187 printf(_("%s is mounted\n"), mnt_fs_get_target(fs
));
189 printf(_("%s is not mounted\n"), mnt_fs_get_target(fs
));
194 static int ignore(struct libmnt_fs
*);
196 static struct fsck_fs_data
*fs_create_data(struct libmnt_fs
*fs
)
198 struct fsck_fs_data
*data
= mnt_fs_get_userdata(fs
);
201 data
= xcalloc(1, sizeof(*data
));
202 mnt_fs_set_userdata(fs
, data
);
208 * fs from fstab might contains real device name as well as symlink,
209 * LABEL or UUID, this function returns canonicalized result.
211 static const char *fs_get_device(struct libmnt_fs
*fs
)
213 struct fsck_fs_data
*data
= mnt_fs_get_userdata(fs
);
215 if (!data
|| !data
->eval_device
) {
216 const char *spec
= mnt_fs_get_source(fs
);
219 data
= fs_create_data(fs
);
221 data
->eval_device
= 1;
222 data
->device
= mnt_resolve_spec(spec
, mnt_table_get_cache(fstab
));
224 data
->device
= xstrdup(spec
);
230 static dev_t
fs_get_disk(struct libmnt_fs
*fs
, int check
)
232 struct fsck_fs_data
*data
;
236 data
= mnt_fs_get_userdata(fs
);
237 if (data
&& data
->disk
)
243 if (mnt_fs_is_netfs(fs
) || mnt_fs_is_pseudofs(fs
))
246 device
= fs_get_device(fs
);
250 data
= fs_create_data(fs
);
252 if (!stat(device
, &st
) &&
253 !blkid_devno_to_wholedisk(st
.st_rdev
, NULL
, 0, &data
->disk
)) {
256 data
->stacked
= count_slaves(data
->disk
) > 0 ? 1 : 0;
262 static int fs_is_stacked(struct libmnt_fs
*fs
)
264 struct fsck_fs_data
*data
= mnt_fs_get_userdata(fs
);
265 return data
? data
->stacked
: 0;
268 static int fs_is_done(struct libmnt_fs
*fs
)
270 struct fsck_fs_data
*data
= mnt_fs_get_userdata(fs
);
271 return data
? data
->done
: 0;
274 static void fs_set_done(struct libmnt_fs
*fs
)
276 struct fsck_fs_data
*data
= fs_create_data(fs
);
282 static int is_irrotational_disk(dev_t disk
)
289 rc
= snprintf(path
, sizeof(path
),
290 "/sys/dev/block/%d:%d/queue/rotational",
291 major(disk
), minor(disk
));
293 if (rc
< 0 || (unsigned int) (rc
+ 1) > sizeof(path
))
296 f
= fopen(path
, "r");
300 rc
= fscanf(f
, "%d", &x
);
303 warn(_("failed to read: %s"), path
);
305 warnx(_("parse error: %s"), path
);
309 return rc
== 1 ? !x
: 0;
312 static void lock_disk(struct fsck_instance
*inst
)
314 dev_t disk
= fs_get_disk(inst
->fs
, 1);
317 if (!disk
|| is_irrotational_disk(disk
))
320 diskname
= blkid_devno_to_devname(disk
);
325 printf(_("Locking disk %s ... "), diskname
);
327 inst
->lock
= open(diskname
, O_CLOEXEC
| O_RDONLY
);
328 if (inst
->lock
>= 0) {
331 /* inform users that we're waiting on the lock */
333 (rc
= flock(inst
->lock
, LOCK_EX
| LOCK_NB
)) != 0 &&
334 errno
== EWOULDBLOCK
)
335 printf(_("(waiting) "));
337 if (rc
!= 0 && flock(inst
->lock
, LOCK_EX
) != 0) {
338 close(inst
->lock
); /* failed */
344 /* TRANSLATORS: These are followups to "Locking disk...". */
345 printf("%s.\n", inst
->lock
>= 0 ? _("succeeded") : _("failed"));
351 static void unlock_disk(struct fsck_instance
*inst
)
353 if (inst
->lock
>= 0) {
354 /* explicitly unlock, don't rely on close(), maybe some library
355 * (e.g. liblkid) has still open the device.
357 flock(inst
->lock
, LOCK_UN
);
364 static void free_instance(struct fsck_instance
*i
)
373 static struct libmnt_fs
*add_dummy_fs(const char *device
)
375 struct libmnt_fs
*fs
= mnt_new_fs();
377 if (fs
&& mnt_fs_set_source(fs
, device
) == 0 &&
378 mnt_table_add_fs(fstab
, fs
) == 0)
382 err(FSCK_EX_ERROR
, _("failed to setup description for %s"), device
);
385 static void fs_interpret_type(struct libmnt_fs
*fs
)
388 const char *type
= mnt_fs_get_fstype(fs
);
390 if (type
&& strcmp(type
, "auto") != 0)
393 mnt_fs_set_fstype(fs
, NULL
);
395 device
= fs_get_device(fs
);
399 type
= mnt_get_fstype(device
, &ambi
, mnt_table_get_cache(fstab
));
401 mnt_fs_set_fstype(fs
, type
);
405 static int parser_errcb(struct libmnt_table
*tb
__attribute__ ((__unused__
)),
406 const char *filename
, int line
)
408 warnx(_("%s: parse error at line %d -- ignore"), filename
, line
);
413 * Load the filesystem database from /etc/fstab
415 static void load_fs_info(void)
419 fstab
= mnt_new_table();
421 err(FSCK_EX_ERROR
, ("failed to initialize libmount table"));
423 mnt_table_set_parser_errcb(fstab
, parser_errcb
);
424 mnt_table_set_cache(fstab
, mntcache
);
429 * Let's follow libmount defauls if $FSTAB_FILE is not specified
431 path
= getenv("FSTAB_FILE");
433 if (mnt_table_parse_fstab(fstab
, path
)) {
435 path
= mnt_get_fstab_path();
437 warn(_("%s: failed to parse fstab"), path
);
439 warnx(_("%s: failed to parse fstab"), path
);
444 * Lookup filesys in /etc/fstab and return the corresponding entry.
445 * The @path has to be real path (no TAG) by mnt_resolve_spec().
447 static struct libmnt_fs
*lookup(char *path
)
449 struct libmnt_fs
*fs
;
454 fs
= mnt_table_find_srcpath(fstab
, path
, MNT_ITER_FORWARD
);
456 fs
= mnt_table_find_target(fstab
, path
, MNT_ITER_FORWARD
);
461 /* Find fsck program for a given fs type. */
462 static char *find_fsck(const char *type
)
466 static char prog
[256];
467 char *p
= xstrdup(fsck_path
);
470 /* Are we looking for a program or just a type? */
471 tpl
= (strncmp(type
, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
473 for(s
= strtok(p
, ":"); s
; s
= strtok(NULL
, ":")) {
474 sprintf(prog
, tpl
, s
, type
);
475 if (stat(prog
, &st
) == 0)
480 return(s
? prog
: NULL
);
483 static int progress_active(void)
485 struct fsck_instance
*inst
;
487 for (inst
= instance_list
; inst
; inst
= inst
->next
) {
488 if (inst
->flags
& FLAG_DONE
)
490 if (inst
->flags
& FLAG_PROGRESS
)
497 * Process run statistics for finished fsck instances.
499 * If report_stats is 0, do nothing, otherwise print a selection of
500 * interesting rusage statistics as well as elapsed wallclock time.
502 static void print_stats(struct fsck_instance
*inst
)
506 if (!inst
|| !report_stats
|| noexecute
)
509 time_diff
= (inst
->end_time
.tv_sec
- inst
->start_time
.tv_sec
)
510 + (inst
->end_time
.tv_usec
- inst
->start_time
.tv_usec
) / 1E6
;
512 fprintf(stdout
, "%s: status %d, rss %ld, "
513 "real %f, user %d.%06d, sys %d.%06d\n",
514 fs_get_device(inst
->fs
),
516 inst
->rusage
.ru_maxrss
,
518 (int)inst
->rusage
.ru_utime
.tv_sec
,
519 (int)inst
->rusage
.ru_utime
.tv_usec
,
520 (int)inst
->rusage
.ru_stime
.tv_sec
,
521 (int)inst
->rusage
.ru_stime
.tv_usec
);
525 * Execute a particular fsck program, and link it into the list of
526 * child processes we are waiting for.
528 static int execute(const char *type
, struct libmnt_fs
*fs
, int interactive
)
530 char *s
, *argv
[80], prog
[80];
532 struct fsck_instance
*inst
, *p
;
535 inst
= xcalloc(1, sizeof(*inst
));
537 sprintf(prog
, "fsck.%s", type
);
538 argv
[0] = xstrdup(prog
);
541 for (i
=0; i
<num_args
; i
++)
542 argv
[argc
++] = xstrdup(args
[i
]);
545 if ((strcmp(type
, "ext2") == 0) ||
546 (strcmp(type
, "ext3") == 0) ||
547 (strcmp(type
, "ext4") == 0) ||
548 (strcmp(type
, "ext4dev") == 0)) {
552 if (!progress_active()) {
553 snprintf(tmp
, 80, "-C%d", progress_fd
);
554 inst
->flags
|= FLAG_PROGRESS
;
555 } else if (progress_fd
)
556 snprintf(tmp
, 80, "-C%d", progress_fd
* -1);
558 argv
[argc
++] = xstrdup(tmp
);
562 argv
[argc
++] = xstrdup(fs_get_device(fs
));
567 warnx(_("%s: not found"), prog
);
572 if (verbose
|| noexecute
) {
573 const char *tgt
= mnt_fs_get_target(fs
);
576 tgt
= fs_get_device(fs
);
577 printf("[%s (%d) -- %s] ", s
, num_running
, tgt
);
578 for (i
=0; i
< argc
; i
++)
579 printf("%s ", argv
[i
]);
589 /* Fork and execute the correct program. */
592 else if ((pid
= fork()) < 0) {
593 warn(_("fork failed"));
596 } else if (pid
== 0) {
600 err(FSCK_EX_ERROR
, _("%s: execute failed"), s
);
603 for (i
=0; i
< argc
; i
++)
607 inst
->prog
= xstrdup(prog
);
608 inst
->type
= xstrdup(type
);
609 gettimeofday(&inst
->start_time
, NULL
);
613 * Find the end of the list, so we add the instance on at the end.
615 for (p
= instance_list
; p
&& p
->next
; p
= p
->next
);
620 instance_list
= inst
;
626 * Send a signal to all outstanding fsck child processes
628 static int kill_all(int signum
)
630 struct fsck_instance
*inst
;
633 for (inst
= instance_list
; inst
; inst
= inst
->next
) {
634 if (inst
->flags
& FLAG_DONE
)
636 kill(inst
->pid
, signum
);
643 * Wait for one child process to exit; when it does, unlink it from
644 * the list of executing child processes, and return it.
646 static struct fsck_instance
*wait_one(int flags
)
650 struct fsck_instance
*inst
, *inst2
, *prev
;
652 struct rusage rusage
;
658 inst
= instance_list
;
661 while (inst
->next
&& (random() & 1)) {
666 inst
->exit_status
= 0;
671 * gcc -Wall fails saving throw against stupidity
672 * (inst and prev are thought to be uninitialized variables)
677 pid
= wait4(-1, &status
, flags
, &rusage
);
678 if (cancel_requested
&& !kill_sent
) {
682 if ((pid
== 0) && (flags
& WNOHANG
))
685 if ((errno
== EINTR
) || (errno
== EAGAIN
))
687 if (errno
== ECHILD
) {
688 warnx(_("wait: no more child process?!?"));
691 warn(_("waidpid failed"));
694 for (prev
= 0, inst
= instance_list
;
696 prev
= inst
, inst
= inst
->next
) {
697 if (inst
->pid
== pid
)
702 if (WIFEXITED(status
))
703 status
= WEXITSTATUS(status
);
704 else if (WIFSIGNALED(status
)) {
705 sig
= WTERMSIG(status
);
707 status
= FSCK_EX_UNCORRECTED
;
709 warnx(_("Warning... %s for device %s exited "
711 inst
->prog
, fs_get_device(inst
->fs
), sig
);
712 status
= FSCK_EX_ERROR
;
715 warnx(_("%s %s: status is %x, should never happen."),
716 inst
->prog
, fs_get_device(inst
->fs
), status
);
717 status
= FSCK_EX_ERROR
;
720 inst
->exit_status
= status
;
721 inst
->flags
|= FLAG_DONE
;
722 gettimeofday(&inst
->end_time
, NULL
);
723 memcpy(&inst
->rusage
, &rusage
, sizeof(struct rusage
));
725 if (progress
&& (inst
->flags
& FLAG_PROGRESS
) &&
726 !progress_active()) {
727 for (inst2
= instance_list
; inst2
; inst2
= inst2
->next
) {
728 if (inst2
->flags
& FLAG_DONE
)
730 if (strcmp(inst2
->type
, "ext2") &&
731 strcmp(inst2
->type
, "ext3") &&
732 strcmp(inst2
->type
, "ext4") &&
733 strcmp(inst2
->type
, "ext4dev"))
736 * If we've just started the fsck, wait a tiny
737 * bit before sending the kill, to give it
738 * time to set up the signal handler
740 if (inst2
->start_time
.tv_sec
< time(0) + 2) {
743 kill(inst2
->pid
, SIGUSR1
);
747 kill(inst2
->pid
, SIGUSR1
);
748 inst2
->flags
|= FLAG_PROGRESS
;
754 prev
->next
= inst
->next
;
756 instance_list
= inst
->next
;
761 printf(_("Finished with %s (exit status %d)\n"),
762 fs_get_device(inst
->fs
), inst
->exit_status
);
767 #define FLAG_WAIT_ALL 0
768 #define FLAG_WAIT_ATLEAST_ONE 1
770 * Wait until all executing child processes have exited; return the
771 * logical OR of all of their exit code values.
773 static int wait_many(int flags
)
775 struct fsck_instance
*inst
;
776 int global_status
= 0;
779 while ((inst
= wait_one(wait_flags
))) {
780 global_status
|= inst
->exit_status
;
783 if (noexecute
&& (flags
& WNOHANG
) && !(random() % 3))
786 if (flags
& FLAG_WAIT_ATLEAST_ONE
)
787 wait_flags
= WNOHANG
;
789 return global_status
;
793 * Run the fsck program on a particular device
795 * If the type is specified using -t, and it isn't prefixed with "no"
796 * (as in "noext2") and only one filesystem type is specified, then
797 * use that type regardless of what is specified in /etc/fstab.
799 * If the type isn't specified by the user, then use either the type
800 * specified in /etc/fstab, or DEFAULT_FSTYPE.
802 static int fsck_device(struct libmnt_fs
*fs
, int interactive
)
807 fs_interpret_type(fs
);
809 type
= mnt_fs_get_fstype(fs
);
811 if (type
&& strcmp(type
, "auto") != 0)
813 else if (fstype
&& strncmp(fstype
, "no", 2) &&
814 strncmp(fstype
, "opts=", 5) && strncmp(fstype
, "loop", 4) &&
815 !strchr(fstype
, ','))
818 type
= DEFAULT_FSTYPE
;
821 retval
= execute(type
, fs
, interactive
);
823 warnx(_("error %d while executing fsck.%s for %s"),
824 retval
, type
, fs_get_device(fs
));
826 return FSCK_EX_ERROR
;
833 * Deal with the fsck -t argument.
835 struct fs_type_compile
{
841 #define FS_TYPE_NORMAL 0
842 #define FS_TYPE_OPT 1
843 #define FS_TYPE_NEGOPT 2
845 static void compile_fs_type(char *fs_type
, struct fs_type_compile
*cmp
)
849 int negate
, first_negate
= 1;
852 for (cp
=fs_type
; *cp
; cp
++) {
858 cmp
->list
= xcalloc(num
, sizeof(char *));
859 cmp
->type
= xcalloc(num
, sizeof(int));
865 list
= xstrdup(fs_type
);
867 s
= strtok(list
, ",");
870 if (strncmp(s
, "no", 2) == 0) {
873 } else if (*s
== '!') {
877 if (strcmp(s
, "loop") == 0)
878 /* loop is really short-hand for opts=loop */
879 goto loop_special_case
;
880 else if (strncmp(s
, "opts=", 5) == 0) {
883 cmp
->type
[num
] = negate
? FS_TYPE_NEGOPT
: FS_TYPE_OPT
;
886 cmp
->negate
= negate
;
889 if ((negate
&& !cmp
->negate
) ||
890 (!negate
&& cmp
->negate
)) {
892 _("Either all or none of the filesystem types passed to -t must be prefixed\n"
893 "with 'no' or '!'."));
897 cmp
->list
[num
++] = xstrdup(s
);
898 s
= strtok(NULL
, ",");
904 * This function returns true if a particular option appears in a
905 * comma-delimited options list
907 static int opt_in_list(const char *opt
, const char *optlist
)
913 list
= xstrdup(optlist
);
915 s
= strtok(list
, ",");
917 if (strcmp(s
, opt
) == 0) {
921 s
= strtok(NULL
, ",");
927 /* See if the filesystem matches the criteria given by the -t option */
928 static int fs_match(struct libmnt_fs
*fs
, struct fs_type_compile
*cmp
)
930 int n
, ret
= 0, checked_type
= 0;
933 if (cmp
->list
== 0 || cmp
->list
[0] == 0)
936 for (n
=0; (cp
= cmp
->list
[n
]); n
++) {
937 switch (cmp
->type
[n
]) {
940 const char *type
= mnt_fs_get_fstype(fs
);
943 if (type
&& strcmp(cp
, type
) == 0)
948 if (opt_in_list(cp
, mnt_fs_get_options(fs
)))
952 if (!opt_in_list(cp
, mnt_fs_get_options(fs
)))
957 if (checked_type
== 0)
959 return (cmp
->negate
? !ret
: ret
);
963 * Check if a device exists
965 static int device_exists(const char *device
)
969 if (stat(device
, &st
) == -1)
971 if (!S_ISBLK(st
.st_mode
))
976 static int fs_ignored_type(struct libmnt_fs
*fs
)
978 const char **ip
, *type
;
980 if (mnt_fs_is_netfs(fs
) || mnt_fs_is_pseudofs(fs
) || mnt_fs_is_swaparea(fs
))
983 type
= mnt_fs_get_fstype(fs
);
985 for(ip
= ignored_types
; type
&& *ip
; ip
++) {
986 if (strcmp(type
, *ip
) == 0)
993 /* Check if we should ignore this filesystem. */
994 static int ignore(struct libmnt_fs
*fs
)
996 const char **ip
, *type
;
1000 * If the pass number is 0, ignore it.
1002 if (mnt_fs_get_passno(fs
) == 0)
1006 * If this is a bind mount, ignore it.
1008 if (opt_in_list("bind", mnt_fs_get_options(fs
))) {
1009 warnx(_("%s: skipping bad line in /etc/fstab: "
1010 "bind mount with nonzero fsck pass number"),
1011 mnt_fs_get_target(fs
));
1016 * ignore devices that don't exist and have the "nofail" mount option
1018 if (!device_exists(fs_get_device(fs
))) {
1019 if (opt_in_list("nofail", mnt_fs_get_options(fs
))) {
1021 printf(_("%s: skipping nonexistent device\n"),
1026 printf(_("%s: nonexistent device (\"nofail\" fstab "
1027 "option may be used to skip this device)\n"),
1031 fs_interpret_type(fs
);
1034 * If a specific fstype is specified, and it doesn't match,
1037 if (!fs_match(fs
, &fs_type_compiled
))
1040 type
= mnt_fs_get_fstype(fs
);
1043 printf(_("%s: skipping unknown filesystem type\n"),
1048 /* Are we ignoring this type? */
1049 if (fs_ignored_type(fs
))
1052 /* Do we really really want to check this fs? */
1053 for(ip
= really_wanted
; *ip
; ip
++)
1054 if (strcmp(type
, *ip
) == 0) {
1059 /* See if the <fsck.fs> program is available. */
1060 if (find_fsck(type
) == NULL
) {
1062 warnx(_("cannot check %s: fsck.%s not found"),
1063 fs_get_device(fs
), type
);
1067 /* We can and want to check this file system type. */
1071 static int count_slaves(dev_t disk
)
1075 char dirname
[PATH_MAX
];
1078 snprintf(dirname
, sizeof(dirname
),
1079 "/sys/dev/block/%u:%u/slaves/",
1080 major(disk
), minor(disk
));
1082 if (!(dir
= opendir(dirname
)))
1085 while ((dp
= readdir(dir
)) != 0) {
1086 #ifdef _DIRENT_HAVE_D_TYPE
1087 if (dp
->d_type
!= DT_UNKNOWN
&& dp
->d_type
!= DT_LNK
)
1090 if (dp
->d_name
[0] == '.' &&
1091 ((dp
->d_name
[1] == 0) ||
1092 ((dp
->d_name
[1] == '.') && (dp
->d_name
[2] == 0))))
1103 * Returns TRUE if a partition on the same disk is already being
1106 static int disk_already_active(struct libmnt_fs
*fs
)
1108 struct fsck_instance
*inst
;
1111 if (force_all_parallel
)
1114 if (instance_list
&& fs_is_stacked(instance_list
->fs
))
1115 /* any instance for a stacked device is already running */
1118 disk
= fs_get_disk(fs
, 1);
1121 * If we don't know the base device, assume that the device is
1122 * already active if there are any fsck instances running.
1124 * Don't check a stacked device with any other disk too.
1126 if (!disk
|| fs_is_stacked(fs
))
1127 return (instance_list
!= 0);
1129 for (inst
= instance_list
; inst
; inst
= inst
->next
) {
1130 dev_t idisk
= fs_get_disk(inst
->fs
, 0);
1132 if (!idisk
|| disk
== idisk
)
1139 /* Check all file systems, using the /etc/fstab table. */
1140 static int check_all(void)
1142 int not_done_yet
= 1;
1145 int status
= FSCK_EX_OK
;
1147 struct libmnt_fs
*fs
;
1148 struct libmnt_iter
*itr
= mnt_new_iter(MNT_ITER_FORWARD
);
1151 err(FSCK_EX_ERROR
, _("failed to allocate iterator"));
1154 * Do an initial scan over the filesystem; mark filesystems
1155 * which should be ignored as done, and resolve any "auto"
1156 * filesystem types (done as a side-effect of calling ignore()).
1158 while (mnt_table_next_fs(fstab
, itr
, &fs
) == 0) {
1166 fputs(_("Checking all file systems.\n"), stdout
);
1169 * Find and check the root filesystem.
1171 if (!parallel_root
) {
1172 fs
= mnt_table_find_target(fstab
, "/", MNT_ITER_FORWARD
);
1176 !(ignore_mounted
&& is_mounted(fs
))) {
1177 status
|= fsck_device(fs
, 1);
1178 status
|= wait_many(FLAG_WAIT_ALL
);
1179 if (status
> FSCK_EX_NONDESTRUCT
) {
1189 * This is for the bone-headed user who enters the root
1190 * filesystem twice. Skip root will skep all root entries.
1193 mnt_reset_iter(itr
, MNT_ITER_FORWARD
);
1195 while(mnt_table_next_fs(fstab
, itr
, &fs
) == 0) {
1196 const char *tgt
= mnt_fs_get_target(fs
);
1198 if (tgt
&& strcmp(tgt
, "/") == 0)
1203 while (not_done_yet
) {
1207 mnt_reset_iter(itr
, MNT_ITER_FORWARD
);
1209 while(mnt_table_next_fs(fstab
, itr
, &fs
) == 0) {
1211 if (cancel_requested
)
1216 * If the filesystem's pass number is higher
1217 * than the current pass number, then we don't
1220 if (mnt_fs_get_passno(fs
) > passno
) {
1224 if (ignore_mounted
&& is_mounted(fs
)) {
1229 * If a filesystem on a particular device has
1230 * already been spawned, then we need to defer
1231 * this to another pass.
1233 if (disk_already_active(fs
)) {
1238 * Spawn off the fsck process
1240 status
|= fsck_device(fs
, serialize
);
1244 * Only do one filesystem at a time, or if we
1245 * have a limit on the number of fsck's extant
1246 * at one time, apply that limit.
1249 (max_running
&& (num_running
>= max_running
))) {
1254 if (cancel_requested
)
1257 printf(_("--waiting-- (pass %d)\n"), passno
);
1259 status
|= wait_many(pass_done
? FLAG_WAIT_ALL
:
1260 FLAG_WAIT_ATLEAST_ONE
);
1263 printf("----------------------------------\n");
1269 if (cancel_requested
&& !kill_sent
) {
1274 status
|= wait_many(FLAG_WAIT_ATLEAST_ONE
);
1279 static void __attribute__((__noreturn__
)) usage(void)
1281 printf(_("\nUsage:\n"
1282 " %s [fsck-options] [fs-options] [filesys ...]\n"),
1283 program_invocation_short_name
);
1285 puts(_( "\nOptions:\n"
1286 " -A check all filesystems\n"
1287 " -R skip root filesystem; useful only with `-A'\n"
1288 " -M do not check mounted filesystems\n"
1289 " -t <type> specify filesystem types to be checked;\n"
1290 " type is allowed to be comma-separated list\n"
1291 " -P check filesystems in parallel, including root\n"
1292 " -r report statistics for each device fsck\n"
1293 " -s serialize fsck operations\n"
1294 " -l lock the device using flock()\n"
1295 " -N do not execute, just show what would be done\n"
1296 " -T do not show the title on startup\n"
1297 " -C <fd> display progress bar; file descriptor is for GUIs\n"
1298 " -V explain what is being done\n"
1299 " -? display this help and exit\n\n"
1300 "See fsck.* commands for fs-options."));
1302 exit(FSCK_EX_USAGE
);
1305 static void signal_cancel(int sig
__attribute__((__unused__
)))
1310 static void parse_argv(int argc
, char *argv
[])
1313 char *arg
, *dev
, *tmp
= 0;
1316 int opts_for_fsck
= 0;
1317 struct sigaction sa
;
1320 * Set up signal action
1322 memset(&sa
, 0, sizeof(struct sigaction
));
1323 sa
.sa_handler
= signal_cancel
;
1324 sigaction(SIGINT
, &sa
, 0);
1325 sigaction(SIGTERM
, &sa
, 0);
1331 for (i
=1; i
< argc
; i
++) {
1335 if ((arg
[0] == '/' && !opts_for_fsck
) || strchr(arg
, '=')) {
1336 if (num_devices
>= MAX_DEVICES
)
1337 errx(FSCK_EX_ERROR
, _("too many devices"));
1339 dev
= mnt_resolve_spec(arg
, mntcache
);
1341 if (!dev
&& strchr(arg
, '=')) {
1343 * Check to see if we failed because
1344 * /proc/partitions isn't found.
1346 if (access(_PATH_PROC_PARTITIONS
, R_OK
) < 0) {
1347 warn(_("cannot open %s"),
1348 _PATH_PROC_PARTITIONS
);
1349 errx(FSCK_EX_ERROR
, _("Is /proc mounted?"));
1352 * Check to see if this is because
1353 * we're not running as root
1357 _("must be root to scan for matching filesystems: %s"),
1361 _("couldn't find matching filesystem: %s"),
1364 devices
[num_devices
++] = dev
? dev
: xstrdup(arg
);
1367 if (arg
[0] != '-' || opts_for_fsck
) {
1368 if (num_args
>= MAX_ARGS
)
1369 errx(FSCK_EX_ERROR
, _("too many arguments"));
1370 args
[num_args
++] = xstrdup(arg
);
1373 for (j
=1; arg
[j
]; j
++) {
1374 if (opts_for_fsck
) {
1375 options
[++opt
] = arg
[j
];
1385 progress_fd
= string_to_int(arg
+j
+1);
1386 if (progress_fd
< 0)
1390 } else if ((i
+1) < argc
&&
1391 !strncmp(argv
[i
+1], "-", 1) == 0) {
1392 progress_fd
= string_to_int(argv
[i
]);
1393 if (progress_fd
< 0)
1434 else if ((i
+1) < argc
)
1438 fstype
= xstrdup(tmp
);
1439 compile_fs_type(fstype
, &fs_type_compiled
);
1448 options
[++opt
] = arg
[j
];
1455 options
[++opt
] = '\0';
1456 if (num_args
>= MAX_ARGS
)
1457 errx(FSCK_EX_ERROR
, _("too many arguments"));
1458 args
[num_args
++] = xstrdup(options
);
1462 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1463 force_all_parallel
++;
1464 if ((tmp
= getenv("FSCK_MAX_INST")))
1465 max_running
= atoi(tmp
);
1468 int main(int argc
, char *argv
[])
1471 int interactive
= 0;
1472 char *oldpath
= getenv("PATH");
1473 struct libmnt_fs
*fs
;
1475 setvbuf(stdout
, NULL
, _IONBF
, BUFSIZ
);
1476 setvbuf(stderr
, NULL
, _IONBF
, BUFSIZ
);
1478 setlocale(LC_MESSAGES
, "");
1479 setlocale(LC_CTYPE
, "");
1480 bindtextdomain(PACKAGE
, LOCALEDIR
);
1481 textdomain(PACKAGE
);
1482 atexit(close_stdout
);
1484 mnt_init_debug(0); /* init libmount debug mask */
1485 mntcache
= mnt_new_cache(); /* no fatal error if failed */
1487 parse_argv(argc
, argv
);
1490 printf(_("%s from %s\n"), program_invocation_short_name
, PACKAGE_STRING
);
1494 /* Update our search path to include uncommon directories. */
1496 fsck_path
= xmalloc (strlen (fsck_prefix_path
) + 1 +
1497 strlen (oldpath
) + 1);
1498 strcpy (fsck_path
, fsck_prefix_path
);
1499 strcat (fsck_path
, ":");
1500 strcat (fsck_path
, oldpath
);
1502 fsck_path
= xstrdup(fsck_prefix_path
);
1505 if ((num_devices
== 1) || (serialize
))
1508 if (lockdisk
&& (doall
|| num_devices
> 1)) {
1509 warnx(_("the -l option can be used with one "
1510 "device only -- ignore"));
1514 /* If -A was specified ("check all"), do that! */
1518 if (num_devices
== 0) {
1523 for (i
= 0 ; i
< num_devices
; i
++) {
1524 if (cancel_requested
) {
1531 fs
= lookup(devices
[i
]);
1533 fs
= add_dummy_fs(devices
[i
]);
1534 else if (fs_ignored_type(fs
))
1537 if (ignore_mounted
&& is_mounted(fs
))
1539 status
|= fsck_device(fs
, interactive
);
1541 (max_running
&& (num_running
>= max_running
))) {
1542 struct fsck_instance
*inst
;
1546 status
|= inst
->exit_status
;
1547 free_instance(inst
);
1550 printf("----------------------------------\n");
1553 status
|= wait_many(FLAG_WAIT_ALL
);
1555 mnt_free_cache(mntcache
);
1556 mnt_free_table(fstab
);
1557 mnt_free_table(mtab
);