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 src
= mnt_fs_get_source(fs
);
179 mntcache
= mnt_new_cache();
181 mtab
= mnt_new_table();
183 err(FSCK_EX_ERROR
, ("failed to initialize libmount table"));
184 mnt_table_set_cache(mtab
, mntcache
);
185 mnt_table_parse_mtab(mtab
, NULL
);
188 rc
= mnt_table_find_source(mtab
, src
, MNT_ITER_BACKWARD
) ? 1 : 0;
191 printf(_("%s is mounted\n"), src
);
193 printf(_("%s is not mounted\n"), src
);
198 static int ignore(struct libmnt_fs
*);
200 static struct fsck_fs_data
*fs_create_data(struct libmnt_fs
*fs
)
202 struct fsck_fs_data
*data
= mnt_fs_get_userdata(fs
);
205 data
= xcalloc(1, sizeof(*data
));
206 mnt_fs_set_userdata(fs
, data
);
212 * fs from fstab might contains real device name as well as symlink,
213 * LABEL or UUID, this function returns canonicalized result.
215 static const char *fs_get_device(struct libmnt_fs
*fs
)
217 struct fsck_fs_data
*data
= mnt_fs_get_userdata(fs
);
219 if (!data
|| !data
->eval_device
) {
220 const char *spec
= mnt_fs_get_source(fs
);
223 data
= fs_create_data(fs
);
225 data
->eval_device
= 1;
226 data
->device
= mnt_resolve_spec(spec
, mnt_table_get_cache(fstab
));
228 data
->device
= xstrdup(spec
);
234 static dev_t
fs_get_disk(struct libmnt_fs
*fs
, int check
)
236 struct fsck_fs_data
*data
;
240 data
= mnt_fs_get_userdata(fs
);
241 if (data
&& data
->disk
)
247 if (mnt_fs_is_netfs(fs
) || mnt_fs_is_pseudofs(fs
))
250 device
= fs_get_device(fs
);
254 data
= fs_create_data(fs
);
256 if (!stat(device
, &st
) &&
257 !blkid_devno_to_wholedisk(st
.st_rdev
, NULL
, 0, &data
->disk
)) {
260 data
->stacked
= count_slaves(data
->disk
) > 0 ? 1 : 0;
266 static int fs_is_stacked(struct libmnt_fs
*fs
)
268 struct fsck_fs_data
*data
= mnt_fs_get_userdata(fs
);
269 return data
? data
->stacked
: 0;
272 static int fs_is_done(struct libmnt_fs
*fs
)
274 struct fsck_fs_data
*data
= mnt_fs_get_userdata(fs
);
275 return data
? data
->done
: 0;
278 static void fs_set_done(struct libmnt_fs
*fs
)
280 struct fsck_fs_data
*data
= fs_create_data(fs
);
286 static int is_irrotational_disk(dev_t disk
)
293 rc
= snprintf(path
, sizeof(path
),
294 "/sys/dev/block/%d:%d/queue/rotational",
295 major(disk
), minor(disk
));
297 if (rc
< 0 || (unsigned int) (rc
+ 1) > sizeof(path
))
300 f
= fopen(path
, "r");
304 rc
= fscanf(f
, "%d", &x
);
307 warn(_("cannot read %s"), path
);
309 warnx(_("parse error: %s"), path
);
313 return rc
== 1 ? !x
: 0;
316 static void lock_disk(struct fsck_instance
*inst
)
318 dev_t disk
= fs_get_disk(inst
->fs
, 1);
321 if (!disk
|| is_irrotational_disk(disk
))
324 diskname
= blkid_devno_to_devname(disk
);
329 printf(_("Locking disk %s ... "), diskname
);
331 inst
->lock
= open(diskname
, O_CLOEXEC
| O_RDONLY
);
332 if (inst
->lock
>= 0) {
335 /* inform users that we're waiting on the lock */
337 (rc
= flock(inst
->lock
, LOCK_EX
| LOCK_NB
)) != 0 &&
338 errno
== EWOULDBLOCK
)
339 printf(_("(waiting) "));
341 if (rc
!= 0 && flock(inst
->lock
, LOCK_EX
) != 0) {
342 close(inst
->lock
); /* failed */
348 /* TRANSLATORS: These are followups to "Locking disk...". */
349 printf("%s.\n", inst
->lock
>= 0 ? _("succeeded") : _("failed"));
355 static void unlock_disk(struct fsck_instance
*inst
)
357 if (inst
->lock
>= 0) {
358 /* explicitly unlock, don't rely on close(), maybe some library
359 * (e.g. liblkid) has still open the device.
361 flock(inst
->lock
, LOCK_UN
);
368 static void free_instance(struct fsck_instance
*i
)
378 static struct libmnt_fs
*add_dummy_fs(const char *device
)
380 struct libmnt_fs
*fs
= mnt_new_fs();
382 if (fs
&& mnt_fs_set_source(fs
, device
) == 0 &&
383 mnt_table_add_fs(fstab
, fs
) == 0) {
389 err(FSCK_EX_ERROR
, _("failed to setup description for %s"), device
);
392 static void fs_interpret_type(struct libmnt_fs
*fs
)
395 const char *type
= mnt_fs_get_fstype(fs
);
397 if (type
&& strcmp(type
, "auto") != 0)
400 mnt_fs_set_fstype(fs
, NULL
);
402 device
= fs_get_device(fs
);
406 type
= mnt_get_fstype(device
, &ambi
, mnt_table_get_cache(fstab
));
408 mnt_fs_set_fstype(fs
, type
);
412 static int parser_errcb(struct libmnt_table
*tb
__attribute__ ((__unused__
)),
413 const char *filename
, int line
)
415 warnx(_("%s: parse error at line %d -- ignore"), filename
, line
);
420 * Load the filesystem database from /etc/fstab
422 static void load_fs_info(void)
426 fstab
= mnt_new_table();
428 err(FSCK_EX_ERROR
, ("failed to initialize libmount table"));
430 mnt_table_set_parser_errcb(fstab
, parser_errcb
);
431 mnt_table_set_cache(fstab
, mntcache
);
436 * Let's follow libmount defauls if $FSTAB_FILE is not specified
438 path
= getenv("FSTAB_FILE");
440 if (mnt_table_parse_fstab(fstab
, path
)) {
442 path
= mnt_get_fstab_path();
444 /* don't print error when there is no fstab at all */
445 if (access(path
, F_OK
) == 0) {
447 warn(_("%s: failed to parse fstab"), path
);
449 warnx(_("%s: failed to parse fstab"), path
);
455 * Lookup filesys in /etc/fstab and return the corresponding entry.
456 * The @path has to be real path (no TAG) by mnt_resolve_spec().
458 static struct libmnt_fs
*lookup(char *path
)
460 struct libmnt_fs
*fs
;
465 fs
= mnt_table_find_srcpath(fstab
, path
, MNT_ITER_FORWARD
);
468 * Maybe mountpoint has been specified on fsck command line.
469 * Yeah, crazy feature...
471 * Note that mnt_table_find_target() may canonicalize paths in
472 * the fstab to support symlinks. This is really unwanted,
473 * because readlink() on mountpoints may trigger automounts.
475 * So, disable the cache and compare the paths as strings
476 * without care about symlinks...
478 mnt_table_set_cache(fstab
, NULL
);
479 fs
= mnt_table_find_target(fstab
, path
, MNT_ITER_FORWARD
);
480 mnt_table_set_cache(fstab
, mntcache
);
485 /* Find fsck program for a given fs type. */
486 static char *find_fsck(const char *type
)
490 static char prog
[256];
491 char *p
= xstrdup(fsck_path
);
494 /* Are we looking for a program or just a type? */
495 tpl
= (strncmp(type
, "fsck.", 5) ? "%s/fsck.%s" : "%s/%s");
497 for(s
= strtok(p
, ":"); s
; s
= strtok(NULL
, ":")) {
498 sprintf(prog
, tpl
, s
, type
);
499 if (stat(prog
, &st
) == 0)
504 return(s
? prog
: NULL
);
507 static int progress_active(void)
509 struct fsck_instance
*inst
;
511 for (inst
= instance_list
; inst
; inst
= inst
->next
) {
512 if (inst
->flags
& FLAG_DONE
)
514 if (inst
->flags
& FLAG_PROGRESS
)
521 * Process run statistics for finished fsck instances.
523 * If report_stats is 0, do nothing, otherwise print a selection of
524 * interesting rusage statistics as well as elapsed wallclock time.
526 static void print_stats(struct fsck_instance
*inst
)
530 if (!inst
|| !report_stats
|| noexecute
)
533 time_diff
= (inst
->end_time
.tv_sec
- inst
->start_time
.tv_sec
)
534 + (inst
->end_time
.tv_usec
- inst
->start_time
.tv_usec
) / 1E6
;
536 fprintf(stdout
, "%s: status %d, rss %ld, "
537 "real %f, user %d.%06d, sys %d.%06d\n",
538 fs_get_device(inst
->fs
),
540 inst
->rusage
.ru_maxrss
,
542 (int)inst
->rusage
.ru_utime
.tv_sec
,
543 (int)inst
->rusage
.ru_utime
.tv_usec
,
544 (int)inst
->rusage
.ru_stime
.tv_sec
,
545 (int)inst
->rusage
.ru_stime
.tv_usec
);
549 * Execute a particular fsck program, and link it into the list of
550 * child processes we are waiting for.
552 static int execute(const char *type
, struct libmnt_fs
*fs
, int interactive
)
554 char *s
, *argv
[80], prog
[80];
556 struct fsck_instance
*inst
, *p
;
559 inst
= xcalloc(1, sizeof(*inst
));
561 sprintf(prog
, "fsck.%s", type
);
562 argv
[0] = xstrdup(prog
);
565 for (i
=0; i
<num_args
; i
++)
566 argv
[argc
++] = xstrdup(args
[i
]);
569 if ((strcmp(type
, "ext2") == 0) ||
570 (strcmp(type
, "ext3") == 0) ||
571 (strcmp(type
, "ext4") == 0) ||
572 (strcmp(type
, "ext4dev") == 0)) {
576 if (!progress_active()) {
577 snprintf(tmp
, 80, "-C%d", progress_fd
);
578 inst
->flags
|= FLAG_PROGRESS
;
579 } else if (progress_fd
)
580 snprintf(tmp
, 80, "-C%d", progress_fd
* -1);
582 argv
[argc
++] = xstrdup(tmp
);
586 argv
[argc
++] = xstrdup(fs_get_device(fs
));
591 warnx(_("%s: not found"), prog
);
596 if (verbose
|| noexecute
) {
597 const char *tgt
= mnt_fs_get_target(fs
);
600 tgt
= fs_get_device(fs
);
601 printf("[%s (%d) -- %s] ", s
, num_running
, tgt
);
602 for (i
=0; i
< argc
; i
++)
603 printf("%s ", argv
[i
]);
614 /* Fork and execute the correct program. */
617 else if ((pid
= fork()) < 0) {
618 warn(_("fork failed"));
621 } else if (pid
== 0) {
625 err(FSCK_EX_ERROR
, _("%s: execute failed"), s
);
628 for (i
=0; i
< argc
; i
++)
632 inst
->prog
= xstrdup(prog
);
633 inst
->type
= xstrdup(type
);
634 gettimeofday(&inst
->start_time
, NULL
);
638 * Find the end of the list, so we add the instance on at the end.
640 for (p
= instance_list
; p
&& p
->next
; p
= p
->next
);
645 instance_list
= inst
;
651 * Send a signal to all outstanding fsck child processes
653 static int kill_all(int signum
)
655 struct fsck_instance
*inst
;
658 for (inst
= instance_list
; inst
; inst
= inst
->next
) {
659 if (inst
->flags
& FLAG_DONE
)
661 kill(inst
->pid
, signum
);
668 * Wait for one child process to exit; when it does, unlink it from
669 * the list of executing child processes, and return it.
671 static struct fsck_instance
*wait_one(int flags
)
675 struct fsck_instance
*inst
, *inst2
, *prev
;
677 struct rusage rusage
;
683 inst
= instance_list
;
686 while (inst
->next
&& (random() & 1)) {
691 inst
->exit_status
= 0;
696 * gcc -Wall fails saving throw against stupidity
697 * (inst and prev are thought to be uninitialized variables)
702 pid
= wait4(-1, &status
, flags
, &rusage
);
703 if (cancel_requested
&& !kill_sent
) {
707 if ((pid
== 0) && (flags
& WNOHANG
))
710 if ((errno
== EINTR
) || (errno
== EAGAIN
))
712 if (errno
== ECHILD
) {
713 warnx(_("wait: no more child process?!?"));
716 warn(_("waitpid failed"));
719 for (prev
= 0, inst
= instance_list
;
721 prev
= inst
, inst
= inst
->next
) {
722 if (inst
->pid
== pid
)
727 if (WIFEXITED(status
))
728 status
= WEXITSTATUS(status
);
729 else if (WIFSIGNALED(status
)) {
730 sig
= WTERMSIG(status
);
732 status
= FSCK_EX_UNCORRECTED
;
734 warnx(_("Warning... %s for device %s exited "
736 inst
->prog
, fs_get_device(inst
->fs
), sig
);
737 status
= FSCK_EX_ERROR
;
740 warnx(_("%s %s: status is %x, should never happen."),
741 inst
->prog
, fs_get_device(inst
->fs
), status
);
742 status
= FSCK_EX_ERROR
;
745 inst
->exit_status
= status
;
746 inst
->flags
|= FLAG_DONE
;
747 gettimeofday(&inst
->end_time
, NULL
);
748 memcpy(&inst
->rusage
, &rusage
, sizeof(struct rusage
));
750 if (progress
&& (inst
->flags
& FLAG_PROGRESS
) &&
751 !progress_active()) {
752 for (inst2
= instance_list
; inst2
; inst2
= inst2
->next
) {
753 if (inst2
->flags
& FLAG_DONE
)
755 if (strcmp(inst2
->type
, "ext2") &&
756 strcmp(inst2
->type
, "ext3") &&
757 strcmp(inst2
->type
, "ext4") &&
758 strcmp(inst2
->type
, "ext4dev"))
761 * If we've just started the fsck, wait a tiny
762 * bit before sending the kill, to give it
763 * time to set up the signal handler
765 if (inst2
->start_time
.tv_sec
< time(0) + 2) {
768 kill(inst2
->pid
, SIGUSR1
);
772 kill(inst2
->pid
, SIGUSR1
);
773 inst2
->flags
|= FLAG_PROGRESS
;
779 prev
->next
= inst
->next
;
781 instance_list
= inst
->next
;
786 printf(_("Finished with %s (exit status %d)\n"),
787 fs_get_device(inst
->fs
), inst
->exit_status
);
792 #define FLAG_WAIT_ALL 0
793 #define FLAG_WAIT_ATLEAST_ONE 1
795 * Wait until all executing child processes have exited; return the
796 * logical OR of all of their exit code values.
798 static int wait_many(int flags
)
800 struct fsck_instance
*inst
;
801 int global_status
= 0;
804 while ((inst
= wait_one(wait_flags
))) {
805 global_status
|= inst
->exit_status
;
808 if (noexecute
&& (flags
& WNOHANG
) && !(random() % 3))
811 if (flags
& FLAG_WAIT_ATLEAST_ONE
)
812 wait_flags
= WNOHANG
;
814 return global_status
;
818 * Run the fsck program on a particular device
820 * If the type is specified using -t, and it isn't prefixed with "no"
821 * (as in "noext2") and only one filesystem type is specified, then
822 * use that type regardless of what is specified in /etc/fstab.
824 * If the type isn't specified by the user, then use either the type
825 * specified in /etc/fstab, or DEFAULT_FSTYPE.
827 static int fsck_device(struct libmnt_fs
*fs
, int interactive
)
832 fs_interpret_type(fs
);
834 type
= mnt_fs_get_fstype(fs
);
836 if (type
&& strcmp(type
, "auto") != 0)
838 else if (fstype
&& strncmp(fstype
, "no", 2) &&
839 strncmp(fstype
, "opts=", 5) && strncmp(fstype
, "loop", 4) &&
840 !strchr(fstype
, ','))
843 type
= DEFAULT_FSTYPE
;
846 retval
= execute(type
, fs
, interactive
);
848 warnx(_("error %d while executing fsck.%s for %s"),
849 retval
, type
, fs_get_device(fs
));
851 return FSCK_EX_ERROR
;
858 * Deal with the fsck -t argument.
860 struct fs_type_compile
{
866 #define FS_TYPE_NORMAL 0
867 #define FS_TYPE_OPT 1
868 #define FS_TYPE_NEGOPT 2
870 static void compile_fs_type(char *fs_type
, struct fs_type_compile
*cmp
)
874 int negate
, first_negate
= 1;
877 for (cp
=fs_type
; *cp
; cp
++) {
883 cmp
->list
= xcalloc(num
, sizeof(char *));
884 cmp
->type
= xcalloc(num
, sizeof(int));
890 list
= xstrdup(fs_type
);
892 s
= strtok(list
, ",");
895 if (strncmp(s
, "no", 2) == 0) {
898 } else if (*s
== '!') {
902 if (strcmp(s
, "loop") == 0)
903 /* loop is really short-hand for opts=loop */
904 goto loop_special_case
;
905 else if (strncmp(s
, "opts=", 5) == 0) {
908 cmp
->type
[num
] = negate
? FS_TYPE_NEGOPT
: FS_TYPE_OPT
;
911 cmp
->negate
= negate
;
914 if ((negate
&& !cmp
->negate
) ||
915 (!negate
&& cmp
->negate
)) {
917 _("Either all or none of the filesystem types passed to -t must be prefixed\n"
918 "with 'no' or '!'."));
922 cmp
->list
[num
++] = xstrdup(s
);
923 s
= strtok(NULL
, ",");
929 * This function returns true if a particular option appears in a
930 * comma-delimited options list
932 static int opt_in_list(const char *opt
, const char *optlist
)
938 list
= xstrdup(optlist
);
940 s
= strtok(list
, ",");
942 if (strcmp(s
, opt
) == 0) {
946 s
= strtok(NULL
, ",");
952 /* See if the filesystem matches the criteria given by the -t option */
953 static int fs_match(struct libmnt_fs
*fs
, struct fs_type_compile
*cmp
)
955 int n
, ret
= 0, checked_type
= 0;
958 if (cmp
->list
== 0 || cmp
->list
[0] == 0)
961 for (n
=0; (cp
= cmp
->list
[n
]); n
++) {
962 switch (cmp
->type
[n
]) {
965 const char *type
= mnt_fs_get_fstype(fs
);
968 if (type
&& strcmp(cp
, type
) == 0)
973 if (opt_in_list(cp
, mnt_fs_get_options(fs
)))
977 if (!opt_in_list(cp
, mnt_fs_get_options(fs
)))
982 if (checked_type
== 0)
984 return (cmp
->negate
? !ret
: ret
);
988 * Check if a device exists
990 static int device_exists(const char *device
)
994 if (stat(device
, &st
) == -1)
996 if (!S_ISBLK(st
.st_mode
))
1001 static int fs_ignored_type(struct libmnt_fs
*fs
)
1003 const char **ip
, *type
;
1005 if (mnt_fs_is_netfs(fs
) || mnt_fs_is_pseudofs(fs
) || mnt_fs_is_swaparea(fs
))
1008 type
= mnt_fs_get_fstype(fs
);
1010 for(ip
= ignored_types
; type
&& *ip
; ip
++) {
1011 if (strcmp(type
, *ip
) == 0)
1018 /* Check if we should ignore this filesystem. */
1019 static int ignore(struct libmnt_fs
*fs
)
1021 const char **ip
, *type
;
1025 * If the pass number is 0, ignore it.
1027 if (mnt_fs_get_passno(fs
) == 0)
1031 * If this is a bind mount, ignore it.
1033 if (opt_in_list("bind", mnt_fs_get_options(fs
))) {
1034 warnx(_("%s: skipping bad line in /etc/fstab: "
1035 "bind mount with nonzero fsck pass number"),
1036 mnt_fs_get_target(fs
));
1041 * ignore devices that don't exist and have the "nofail" mount option
1043 if (!device_exists(fs_get_device(fs
))) {
1044 if (opt_in_list("nofail", mnt_fs_get_options(fs
))) {
1046 printf(_("%s: skipping nonexistent device\n"),
1051 printf(_("%s: nonexistent device (\"nofail\" fstab "
1052 "option may be used to skip this device)\n"),
1056 fs_interpret_type(fs
);
1059 * If a specific fstype is specified, and it doesn't match,
1062 if (!fs_match(fs
, &fs_type_compiled
))
1065 type
= mnt_fs_get_fstype(fs
);
1068 printf(_("%s: skipping unknown filesystem type\n"),
1073 /* Are we ignoring this type? */
1074 if (fs_ignored_type(fs
))
1077 /* Do we really really want to check this fs? */
1078 for(ip
= really_wanted
; *ip
; ip
++)
1079 if (strcmp(type
, *ip
) == 0) {
1084 /* See if the <fsck.fs> program is available. */
1085 if (find_fsck(type
) == NULL
) {
1087 warnx(_("cannot check %s: fsck.%s not found"),
1088 fs_get_device(fs
), type
);
1092 /* We can and want to check this file system type. */
1096 static int count_slaves(dev_t disk
)
1100 char dirname
[PATH_MAX
];
1103 snprintf(dirname
, sizeof(dirname
),
1104 "/sys/dev/block/%u:%u/slaves/",
1105 major(disk
), minor(disk
));
1107 if (!(dir
= opendir(dirname
)))
1110 while ((dp
= readdir(dir
)) != 0) {
1111 #ifdef _DIRENT_HAVE_D_TYPE
1112 if (dp
->d_type
!= DT_UNKNOWN
&& dp
->d_type
!= DT_LNK
)
1115 if (dp
->d_name
[0] == '.' &&
1116 ((dp
->d_name
[1] == 0) ||
1117 ((dp
->d_name
[1] == '.') && (dp
->d_name
[2] == 0))))
1128 * Returns TRUE if a partition on the same disk is already being
1131 static int disk_already_active(struct libmnt_fs
*fs
)
1133 struct fsck_instance
*inst
;
1136 if (force_all_parallel
)
1139 if (instance_list
&& fs_is_stacked(instance_list
->fs
))
1140 /* any instance for a stacked device is already running */
1143 disk
= fs_get_disk(fs
, 1);
1146 * If we don't know the base device, assume that the device is
1147 * already active if there are any fsck instances running.
1149 * Don't check a stacked device with any other disk too.
1151 if (!disk
|| fs_is_stacked(fs
))
1152 return (instance_list
!= 0);
1154 for (inst
= instance_list
; inst
; inst
= inst
->next
) {
1155 dev_t idisk
= fs_get_disk(inst
->fs
, 0);
1157 if (!idisk
|| disk
== idisk
)
1164 /* Check all file systems, using the /etc/fstab table. */
1165 static int check_all(void)
1167 int not_done_yet
= 1;
1170 int status
= FSCK_EX_OK
;
1172 struct libmnt_fs
*fs
;
1173 struct libmnt_iter
*itr
= mnt_new_iter(MNT_ITER_FORWARD
);
1176 err(FSCK_EX_ERROR
, _("failed to allocate iterator"));
1179 * Do an initial scan over the filesystem; mark filesystems
1180 * which should be ignored as done, and resolve any "auto"
1181 * filesystem types (done as a side-effect of calling ignore()).
1183 while (mnt_table_next_fs(fstab
, itr
, &fs
) == 0) {
1191 fputs(_("Checking all file systems.\n"), stdout
);
1194 * Find and check the root filesystem.
1196 if (!parallel_root
) {
1197 fs
= mnt_table_find_target(fstab
, "/", MNT_ITER_FORWARD
);
1201 !(ignore_mounted
&& is_mounted(fs
))) {
1202 status
|= fsck_device(fs
, 1);
1203 status
|= wait_many(FLAG_WAIT_ALL
);
1204 if (status
> FSCK_EX_NONDESTRUCT
) {
1214 * This is for the bone-headed user who enters the root
1215 * filesystem twice. Skip root will skep all root entries.
1218 mnt_reset_iter(itr
, MNT_ITER_FORWARD
);
1220 while(mnt_table_next_fs(fstab
, itr
, &fs
) == 0) {
1221 const char *tgt
= mnt_fs_get_target(fs
);
1223 if (tgt
&& strcmp(tgt
, "/") == 0)
1228 while (not_done_yet
) {
1232 mnt_reset_iter(itr
, MNT_ITER_FORWARD
);
1234 while(mnt_table_next_fs(fstab
, itr
, &fs
) == 0) {
1236 if (cancel_requested
)
1241 * If the filesystem's pass number is higher
1242 * than the current pass number, then we don't
1245 if (mnt_fs_get_passno(fs
) > passno
) {
1249 if (ignore_mounted
&& is_mounted(fs
)) {
1254 * If a filesystem on a particular device has
1255 * already been spawned, then we need to defer
1256 * this to another pass.
1258 if (disk_already_active(fs
)) {
1263 * Spawn off the fsck process
1265 status
|= fsck_device(fs
, serialize
);
1269 * Only do one filesystem at a time, or if we
1270 * have a limit on the number of fsck's extant
1271 * at one time, apply that limit.
1274 (max_running
&& (num_running
>= max_running
))) {
1279 if (cancel_requested
)
1282 printf(_("--waiting-- (pass %d)\n"), passno
);
1284 status
|= wait_many(pass_done
? FLAG_WAIT_ALL
:
1285 FLAG_WAIT_ATLEAST_ONE
);
1288 printf("----------------------------------\n");
1294 if (cancel_requested
&& !kill_sent
) {
1299 status
|= wait_many(FLAG_WAIT_ATLEAST_ONE
);
1304 static void __attribute__((__noreturn__
)) usage(FILE *out
)
1306 fputs(USAGE_HEADER
, out
);
1307 fprintf(out
, _(" %s [options] -- [fs-options] [<filesystem> ...]\n"),
1308 program_invocation_short_name
);
1310 fputs(USAGE_OPTIONS
, out
);
1311 fputs(_(" -A check all filesystems\n"), out
);
1312 fputs(_(" -C [<fd>] display progress bar; file descriptor is for GUIs\n"), out
);
1313 fputs(_(" -l lock the device to guarantee exclusive access\n"), out
);
1314 fputs(_(" -M do not check mounted filesystems\n"), out
);
1315 fputs(_(" -N do not execute, just show what would be done\n"), out
);
1316 fputs(_(" -P check filesystems in parallel, including root\n"), out
);
1317 fputs(_(" -R skip root filesystem; useful only with '-A'\n"), out
);
1318 fputs(_(" -r report statistics for each device checked\n"), out
);
1319 fputs(_(" -s serialize the checking operations\n"), out
);
1320 fputs(_(" -T do not show the title on startup\n"), out
);
1321 fputs(_(" -t <type> specify filesystem types to be checked;\n"
1322 " <type> is allowed to be a comma-separated list\n"), out
);
1323 fputs(_(" -V explain what is being done\n"), out
);
1324 fputs(_(" -? display this help and exit\n"), out
);
1326 fputs(USAGE_SEPARATOR
, out
);
1327 fputs(_("See the specific fsck.* commands for available fs-options."), out
);
1328 fprintf(out
, USAGE_MAN_TAIL("fsck(8)"));
1330 exit(out
== stderr
? FSCK_EX_USAGE
: FSCK_EX_OK
);
1333 static void signal_cancel(int sig
__attribute__((__unused__
)))
1338 static void parse_argv(int argc
, char *argv
[])
1341 char *arg
, *dev
, *tmp
= 0;
1344 int opts_for_fsck
= 0;
1345 struct sigaction sa
;
1348 * Set up signal action
1350 memset(&sa
, 0, sizeof(struct sigaction
));
1351 sa
.sa_handler
= signal_cancel
;
1352 sigaction(SIGINT
, &sa
, 0);
1353 sigaction(SIGTERM
, &sa
, 0);
1359 for (i
=1; i
< argc
; i
++) {
1363 if ((arg
[0] == '/' && !opts_for_fsck
) || strchr(arg
, '=')) {
1364 if (num_devices
>= MAX_DEVICES
)
1365 errx(FSCK_EX_ERROR
, _("too many devices"));
1367 dev
= mnt_resolve_spec(arg
, mntcache
);
1369 if (!dev
&& strchr(arg
, '=')) {
1371 * Check to see if we failed because
1372 * /proc/partitions isn't found.
1374 if (access(_PATH_PROC_PARTITIONS
, R_OK
) < 0) {
1375 warn(_("cannot open %s"),
1376 _PATH_PROC_PARTITIONS
);
1377 errx(FSCK_EX_ERROR
, _("Is /proc mounted?"));
1380 * Check to see if this is because
1381 * we're not running as root
1385 _("must be root to scan for matching filesystems: %s"),
1389 _("couldn't find matching filesystem: %s"),
1392 devices
[num_devices
++] = dev
? dev
: xstrdup(arg
);
1395 if (arg
[0] != '-' || opts_for_fsck
) {
1396 if (num_args
>= MAX_ARGS
)
1397 errx(FSCK_EX_ERROR
, _("too many arguments"));
1398 args
[num_args
++] = xstrdup(arg
);
1401 for (j
=1; arg
[j
]; j
++) {
1402 if (opts_for_fsck
) {
1403 options
[++opt
] = arg
[j
];
1413 progress_fd
= string_to_int(arg
+j
+1);
1414 if (progress_fd
< 0)
1418 } else if ((i
+1) < argc
&&
1419 !strncmp(argv
[i
+1], "-", 1) == 0) {
1420 progress_fd
= string_to_int(argv
[i
]);
1421 if (progress_fd
< 0)
1462 else if ((i
+1) < argc
)
1466 fstype
= xstrdup(tmp
);
1467 compile_fs_type(fstype
, &fs_type_compiled
);
1476 options
[++opt
] = arg
[j
];
1483 options
[++opt
] = '\0';
1484 if (num_args
>= MAX_ARGS
)
1485 errx(FSCK_EX_ERROR
, _("too many arguments"));
1486 args
[num_args
++] = xstrdup(options
);
1490 if (getenv("FSCK_FORCE_ALL_PARALLEL"))
1491 force_all_parallel
++;
1492 if ((tmp
= getenv("FSCK_MAX_INST")))
1493 max_running
= atoi(tmp
);
1496 int main(int argc
, char *argv
[])
1499 int interactive
= 0;
1500 char *oldpath
= getenv("PATH");
1501 struct libmnt_fs
*fs
;
1503 setvbuf(stdout
, NULL
, _IONBF
, BUFSIZ
);
1504 setvbuf(stderr
, NULL
, _IONBF
, BUFSIZ
);
1506 setlocale(LC_MESSAGES
, "");
1507 setlocale(LC_CTYPE
, "");
1508 bindtextdomain(PACKAGE
, LOCALEDIR
);
1509 textdomain(PACKAGE
);
1510 atexit(close_stdout
);
1512 mnt_init_debug(0); /* init libmount debug mask */
1513 mntcache
= mnt_new_cache(); /* no fatal error if failed */
1515 parse_argv(argc
, argv
);
1518 printf(UTIL_LINUX_VERSION
);
1522 /* Update our search path to include uncommon directories. */
1524 fsck_path
= xmalloc (strlen (fsck_prefix_path
) + 1 +
1525 strlen (oldpath
) + 1);
1526 strcpy (fsck_path
, fsck_prefix_path
);
1527 strcat (fsck_path
, ":");
1528 strcat (fsck_path
, oldpath
);
1530 fsck_path
= xstrdup(fsck_prefix_path
);
1533 if ((num_devices
== 1) || (serialize
))
1536 if (lockdisk
&& (doall
|| num_devices
> 1)) {
1537 warnx(_("the -l option can be used with one "
1538 "device only -- ignore"));
1542 /* If -A was specified ("check all"), do that! */
1546 if (num_devices
== 0) {
1551 for (i
= 0 ; i
< num_devices
; i
++) {
1552 if (cancel_requested
) {
1559 fs
= lookup(devices
[i
]);
1561 fs
= add_dummy_fs(devices
[i
]);
1562 else if (fs_ignored_type(fs
))
1565 if (ignore_mounted
&& is_mounted(fs
))
1567 status
|= fsck_device(fs
, interactive
);
1569 (max_running
&& (num_running
>= max_running
))) {
1570 struct fsck_instance
*inst
;
1574 status
|= inst
->exit_status
;
1575 free_instance(inst
);
1578 printf("----------------------------------\n");
1581 status
|= wait_many(FLAG_WAIT_ALL
);
1583 mnt_unref_cache(mntcache
);
1584 mnt_unref_table(fstab
);
1585 mnt_unref_table(mtab
);