1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2018-2024 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <djwong@kernel.org>
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"
30 #include "xfs_errortag.h"
32 /* Phase 1: Find filesystem geometry (and clean up after) */
34 /* Shut down the filesystem. */
37 struct scrub_ctx
*ctx
)
41 flag
= XFS_FSOP_GOING_FLAGS_LOGFLUSH
;
42 str_info(ctx
, ctx
->mntpoint
, _("Shutting down filesystem!"));
43 if (ioctl(ctx
->mnt
.fd
, XFS_IOC_GOINGDOWN
, &flag
))
44 str_errno(ctx
, ctx
->mntpoint
);
47 /* Clean up the XFS-specific state data. */
50 struct scrub_ctx
*ctx
)
54 action_lists_free(&ctx
->action_lists
);
56 free_handle(ctx
->fshandle
, ctx
->fshandle_len
);
58 disk_close(ctx
->rtdev
);
60 disk_close(ctx
->logdev
);
62 disk_close(ctx
->datadev
);
64 error
= -xfd_close(&ctx
->mnt
);
66 str_liberror(ctx
, error
, _("closing mountpoint fd"));
72 /* Decide if we're using FORCE_REBUILD or injecting FORCE_REPAIR. */
75 struct scrub_ctx
*ctx
)
77 struct xfs_error_injection inject
= {
79 .errtag
= XFS_ERRTAG_FORCE_SCRUB_REPAIR
,
83 use_force_rebuild
= can_force_rebuild(ctx
);
84 if (use_force_rebuild
)
87 error
= ioctl(ctx
->mnt
.fd
, XFS_IOC_ERROR_INJECTION
, &inject
);
89 str_errno(ctx
, _("force_repair"));
94 * Bind to the mountpoint, read the XFS geometry, bind to the block devices.
95 * Anything we've already built will be cleaned up by scrub_cleanup.
99 struct scrub_ctx
*ctx
)
104 * Open the directory with O_NOATIME. For mountpoints owned
105 * by root, this should be sufficient to ensure that we have
106 * CAP_SYS_ADMIN, which we probably need to do anything fancy
107 * with the (XFS driver) kernel.
109 error
= -xfd_open(&ctx
->mnt
, ctx
->mntpoint
,
110 O_RDONLY
| O_NOATIME
| O_DIRECTORY
);
113 str_error(ctx
, ctx
->mntpoint
,
114 _("Must be root to run scrub."));
115 else if (error
== ENOTTY
)
116 str_error(ctx
, ctx
->mntpoint
,
117 _("Not an XFS filesystem."));
119 str_liberror(ctx
, error
, ctx
->mntpoint
);
123 error
= fstat(ctx
->mnt
.fd
, &ctx
->mnt_sb
);
125 str_errno(ctx
, ctx
->mntpoint
);
128 error
= fstatvfs(ctx
->mnt
.fd
, &ctx
->mnt_sv
);
130 str_errno(ctx
, ctx
->mntpoint
);
133 error
= fstatfs(ctx
->mnt
.fd
, &ctx
->mnt_sf
);
135 str_errno(ctx
, ctx
->mntpoint
);
140 * Flush everything out to disk before we start checking.
141 * This seems to reduce the incidence of stale file handle
142 * errors when we open things by handle.
144 error
= syncfs(ctx
->mnt
.fd
);
146 str_errno(ctx
, ctx
->mntpoint
);
150 error
= action_lists_alloc(ctx
->mnt
.fsgeom
.agcount
,
153 str_liberror(ctx
, error
, _("allocating action lists"));
157 error
= path_to_fshandle(ctx
->mntpoint
, &ctx
->fshandle
,
160 str_errno(ctx
, _("getting fshandle"));
164 /* Do we have kernel-assisted metadata scrubbing? */
165 if (!can_scrub_fs_metadata(ctx
) || !can_scrub_inode(ctx
) ||
166 !can_scrub_bmap(ctx
) || !can_scrub_dir(ctx
) ||
167 !can_scrub_attr(ctx
) || !can_scrub_symlink(ctx
) ||
168 !can_scrub_parent(ctx
)) {
169 str_error(ctx
, ctx
->mntpoint
,
170 _("Kernel metadata scrubbing facility is not available."));
174 /* Do we need kernel-assisted metadata repair? */
175 if (ctx
->mode
!= SCRUB_MODE_DRY_RUN
&& !xfs_can_repair(ctx
)) {
176 str_error(ctx
, ctx
->mntpoint
,
177 _("Kernel metadata repair facility is not available. Use -n to scrub."));
181 if (debug_tweak_on("XFS_SCRUB_FORCE_REPAIR")) {
182 error
= enable_force_repair(ctx
);
187 /* Did we find the log and rt devices, if they're present? */
188 if (ctx
->mnt
.fsgeom
.logstart
== 0 && ctx
->fsinfo
.fs_log
== NULL
) {
189 str_error(ctx
, ctx
->mntpoint
,
190 _("Unable to find log device path."));
193 if (ctx
->mnt
.fsgeom
.rtblocks
&& ctx
->fsinfo
.fs_rt
== NULL
) {
194 str_error(ctx
, ctx
->mntpoint
,
195 _("Unable to find realtime device path."));
199 /* Open the raw devices. */
200 ctx
->datadev
= disk_open(ctx
->fsinfo
.fs_name
);
202 str_error(ctx
, ctx
->mntpoint
, _("Unable to open data device."));
206 ctx
->nr_io_threads
= disk_heads(ctx
->datadev
);
208 fprintf(stdout
, _("%s: using %d threads to scrub.\n"),
209 ctx
->mntpoint
, scrub_nproc(ctx
));
213 if (ctx
->fsinfo
.fs_log
) {
214 ctx
->logdev
= disk_open(ctx
->fsinfo
.fs_log
);
216 str_error(ctx
, ctx
->mntpoint
,
217 _("Unable to open external log device."));
221 if (ctx
->fsinfo
.fs_rt
) {
222 ctx
->rtdev
= disk_open(ctx
->fsinfo
.fs_rt
);
224 str_error(ctx
, ctx
->mntpoint
,
225 _("Unable to open realtime device."));
231 * Everything's set up, which means any failures recorded after
232 * this point are most probably corruption errors (as opposed to
233 * purely setup errors).
235 log_info(ctx
, _("Invoking online scrub."), ctx
);
236 ctx
->scrub_setup_succeeded
= true;