1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
10 #include <sys/resource.h>
11 #include <sys/statvfs.h>
16 #include "libfrog/util.h"
17 #include "libfrog/workqueue.h"
19 #include "libfrog/paths.h"
22 #include "libfrog/avl64.h"
24 #include "xfs_scrub.h"
29 #include "libfrog/fsgeom.h"
31 /* Phase 1: Find filesystem geometry (and clean up after) */
33 /* Shut down the filesystem. */
36 struct scrub_ctx
*ctx
)
40 flag
= XFS_FSOP_GOING_FLAGS_LOGFLUSH
;
41 str_info(ctx
, ctx
->mntpoint
, _("Shutting down filesystem!"));
42 if (ioctl(ctx
->mnt
.fd
, XFS_IOC_GOINGDOWN
, &flag
))
43 str_errno(ctx
, ctx
->mntpoint
);
46 /* Clean up the XFS-specific state data. */
49 struct scrub_ctx
*ctx
)
53 xfs_action_lists_free(&ctx
->action_lists
);
55 free_handle(ctx
->fshandle
, ctx
->fshandle_len
);
57 disk_close(ctx
->rtdev
);
59 disk_close(ctx
->logdev
);
61 disk_close(ctx
->datadev
);
63 error
= xfd_close(&ctx
->mnt
);
65 str_liberror(ctx
, error
, _("closing mountpoint fd"));
72 * Bind to the mountpoint, read the XFS geometry, bind to the block devices.
73 * Anything we've already built will be cleaned up by xfs_cleanup_fs.
77 struct scrub_ctx
*ctx
)
82 * Open the directory with O_NOATIME. For mountpoints owned
83 * by root, this should be sufficient to ensure that we have
84 * CAP_SYS_ADMIN, which we probably need to do anything fancy
85 * with the (XFS driver) kernel.
87 error
= xfd_open(&ctx
->mnt
, ctx
->mntpoint
,
88 O_RDONLY
| O_NOATIME
| O_DIRECTORY
);
91 str_info(ctx
, ctx
->mntpoint
,
92 _("Must be root to run scrub."));
93 else if (error
== ENOTTY
)
94 str_error(ctx
, ctx
->mntpoint
,
95 _("Not an XFS filesystem."));
97 str_liberror(ctx
, error
, ctx
->mntpoint
);
101 error
= fstat(ctx
->mnt
.fd
, &ctx
->mnt_sb
);
103 str_errno(ctx
, ctx
->mntpoint
);
106 error
= fstatvfs(ctx
->mnt
.fd
, &ctx
->mnt_sv
);
108 str_errno(ctx
, ctx
->mntpoint
);
111 error
= fstatfs(ctx
->mnt
.fd
, &ctx
->mnt_sf
);
113 str_errno(ctx
, ctx
->mntpoint
);
118 * Flush everything out to disk before we start checking.
119 * This seems to reduce the incidence of stale file handle
120 * errors when we open things by handle.
122 error
= syncfs(ctx
->mnt
.fd
);
124 str_errno(ctx
, ctx
->mntpoint
);
128 if (!xfs_action_lists_alloc(ctx
->mnt
.fsgeom
.agcount
,
129 &ctx
->action_lists
)) {
130 str_error(ctx
, ctx
->mntpoint
, _("Not enough memory."));
134 error
= path_to_fshandle(ctx
->mntpoint
, &ctx
->fshandle
,
137 str_errno(ctx
, _("getting fshandle"));
141 /* Do we have kernel-assisted metadata scrubbing? */
142 if (!xfs_can_scrub_fs_metadata(ctx
) || !xfs_can_scrub_inode(ctx
) ||
143 !xfs_can_scrub_bmap(ctx
) || !xfs_can_scrub_dir(ctx
) ||
144 !xfs_can_scrub_attr(ctx
) || !xfs_can_scrub_symlink(ctx
) ||
145 !xfs_can_scrub_parent(ctx
)) {
146 str_info(ctx
, ctx
->mntpoint
,
147 _("Kernel metadata scrubbing facility is not available."));
151 /* Do we need kernel-assisted metadata repair? */
152 if (ctx
->mode
!= SCRUB_MODE_DRY_RUN
&& !xfs_can_repair(ctx
)) {
153 str_info(ctx
, ctx
->mntpoint
,
154 _("Kernel metadata repair facility is not available. Use -n to scrub."));
158 /* Did we find the log and rt devices, if they're present? */
159 if (ctx
->mnt
.fsgeom
.logstart
== 0 && ctx
->fsinfo
.fs_log
== NULL
) {
160 str_info(ctx
, ctx
->mntpoint
,
161 _("Unable to find log device path."));
164 if (ctx
->mnt
.fsgeom
.rtblocks
&& ctx
->fsinfo
.fs_rt
== NULL
) {
165 str_info(ctx
, ctx
->mntpoint
,
166 _("Unable to find realtime device path."));
170 /* Open the raw devices. */
171 ctx
->datadev
= disk_open(ctx
->fsinfo
.fs_name
);
173 str_errno(ctx
, ctx
->fsinfo
.fs_name
);
177 ctx
->nr_io_threads
= disk_heads(ctx
->datadev
);
179 fprintf(stdout
, _("%s: using %d threads to scrub.\n"),
180 ctx
->mntpoint
, scrub_nproc(ctx
));
184 if (ctx
->fsinfo
.fs_log
) {
185 ctx
->logdev
= disk_open(ctx
->fsinfo
.fs_log
);
187 str_errno(ctx
, ctx
->fsinfo
.fs_name
);
191 if (ctx
->fsinfo
.fs_rt
) {
192 ctx
->rtdev
= disk_open(ctx
->fsinfo
.fs_rt
);
194 str_errno(ctx
, ctx
->fsinfo
.fs_name
);
200 * Everything's set up, which means any failures recorded after
201 * this point are most probably corruption errors (as opposed to
202 * purely setup errors).
204 log_info(ctx
, _("Invoking online scrub."), ctx
);
205 ctx
->scrub_setup_succeeded
= true;