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>
17 #include "workqueue.h"
24 #include "xfs_scrub.h"
30 /* Phase 1: Find filesystem geometry (and clean up after) */
32 /* Shut down the filesystem. */
35 struct scrub_ctx
*ctx
)
39 flag
= XFS_FSOP_GOING_FLAGS_LOGFLUSH
;
40 str_info(ctx
, ctx
->mntpoint
, _("Shutting down filesystem!"));
41 if (ioctl(ctx
->mnt_fd
, XFS_IOC_GOINGDOWN
, &flag
))
42 str_errno(ctx
, ctx
->mntpoint
);
45 /* Clean up the XFS-specific state data. */
48 struct scrub_ctx
*ctx
)
52 xfs_action_lists_free(&ctx
->action_lists
);
54 free_handle(ctx
->fshandle
, ctx
->fshandle_len
);
56 disk_close(ctx
->rtdev
);
58 disk_close(ctx
->logdev
);
60 disk_close(ctx
->datadev
);
62 error
= close(ctx
->mnt_fd
);
64 str_errno(ctx
, _("closing mountpoint fd"));
71 * Bind to the mountpoint, read the XFS geometry, bind to the block devices.
72 * Anything we've already built will be cleaned up by xfs_cleanup_fs.
76 struct scrub_ctx
*ctx
)
81 * Open the directory with O_NOATIME. For mountpoints owned
82 * by root, this should be sufficient to ensure that we have
83 * CAP_SYS_ADMIN, which we probably need to do anything fancy
84 * with the (XFS driver) kernel.
86 ctx
->mnt_fd
= open(ctx
->mntpoint
, O_RDONLY
| O_NOATIME
| O_DIRECTORY
);
87 if (ctx
->mnt_fd
< 0) {
89 str_info(ctx
, ctx
->mntpoint
,
90 _("Must be root to run scrub."));
92 str_errno(ctx
, ctx
->mntpoint
);
96 error
= fstat(ctx
->mnt_fd
, &ctx
->mnt_sb
);
98 str_errno(ctx
, ctx
->mntpoint
);
101 error
= fstatvfs(ctx
->mnt_fd
, &ctx
->mnt_sv
);
103 str_errno(ctx
, ctx
->mntpoint
);
106 error
= fstatfs(ctx
->mnt_fd
, &ctx
->mnt_sf
);
108 str_errno(ctx
, ctx
->mntpoint
);
112 if (!platform_test_xfs_fd(ctx
->mnt_fd
)) {
113 str_info(ctx
, ctx
->mntpoint
,
114 _("Does not appear to be an XFS filesystem!"));
119 * Flush everything out to disk before we start checking.
120 * This seems to reduce the incidence of stale file handle
121 * errors when we open things by handle.
123 error
= syncfs(ctx
->mnt_fd
);
125 str_errno(ctx
, ctx
->mntpoint
);
129 /* Retrieve XFS geometry. */
130 error
= ioctl(ctx
->mnt_fd
, XFS_IOC_FSGEOMETRY
, &ctx
->geo
);
132 str_errno(ctx
, ctx
->mntpoint
);
136 if (!xfs_action_lists_alloc(ctx
->geo
.agcount
, &ctx
->action_lists
)) {
137 str_error(ctx
, ctx
->mntpoint
, _("Not enough memory."));
141 ctx
->agblklog
= log2_roundup(ctx
->geo
.agblocks
);
142 ctx
->blocklog
= highbit32(ctx
->geo
.blocksize
);
143 ctx
->inodelog
= highbit32(ctx
->geo
.inodesize
);
144 ctx
->inopblog
= ctx
->blocklog
- ctx
->inodelog
;
146 error
= path_to_fshandle(ctx
->mntpoint
, &ctx
->fshandle
,
149 str_errno(ctx
, _("getting fshandle"));
153 /* Do we have kernel-assisted metadata scrubbing? */
154 if (!xfs_can_scrub_fs_metadata(ctx
) || !xfs_can_scrub_inode(ctx
) ||
155 !xfs_can_scrub_bmap(ctx
) || !xfs_can_scrub_dir(ctx
) ||
156 !xfs_can_scrub_attr(ctx
) || !xfs_can_scrub_symlink(ctx
) ||
157 !xfs_can_scrub_parent(ctx
)) {
158 str_info(ctx
, ctx
->mntpoint
,
159 _("Kernel metadata scrubbing facility is not available."));
163 /* Do we need kernel-assisted metadata repair? */
164 if (ctx
->mode
!= SCRUB_MODE_DRY_RUN
&& !xfs_can_repair(ctx
)) {
165 str_info(ctx
, ctx
->mntpoint
,
166 _("Kernel metadata repair facility is not available. Use -n to scrub."));
170 /* Did we find the log and rt devices, if they're present? */
171 if (ctx
->geo
.logstart
== 0 && ctx
->fsinfo
.fs_log
== NULL
) {
172 str_info(ctx
, ctx
->mntpoint
,
173 _("Unable to find log device path."));
176 if (ctx
->geo
.rtblocks
&& ctx
->fsinfo
.fs_rt
== NULL
) {
177 str_info(ctx
, ctx
->mntpoint
,
178 _("Unable to find realtime device path."));
182 /* Open the raw devices. */
183 ctx
->datadev
= disk_open(ctx
->fsinfo
.fs_name
);
185 str_errno(ctx
, ctx
->fsinfo
.fs_name
);
189 ctx
->nr_io_threads
= disk_heads(ctx
->datadev
);
191 fprintf(stdout
, _("%s: using %d threads to scrub.\n"),
192 ctx
->mntpoint
, scrub_nproc(ctx
));
196 if (ctx
->fsinfo
.fs_log
) {
197 ctx
->logdev
= disk_open(ctx
->fsinfo
.fs_log
);
199 str_errno(ctx
, ctx
->fsinfo
.fs_name
);
203 if (ctx
->fsinfo
.fs_rt
) {
204 ctx
->rtdev
= disk_open(ctx
->fsinfo
.fs_rt
);
206 str_errno(ctx
, ctx
->fsinfo
.fs_name
);
212 * Everything's set up, which means any failures recorded after
213 * this point are most probably corruption errors (as opposed to
214 * purely setup errors).
216 log_info(ctx
, _("Invoking online scrub."), ctx
);
217 ctx
->scrub_setup_succeeded
= true;