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 if (ctx
->mnt_fd
>= 0) {
63 error
= close(ctx
->mnt_fd
);
65 str_errno(ctx
, _("closing mountpoint fd"));
73 * Bind to the mountpoint, read the XFS geometry, bind to the block devices.
74 * Anything we've already built will be cleaned up by xfs_cleanup_fs.
78 struct scrub_ctx
*ctx
)
83 * Open the directory with O_NOATIME. For mountpoints owned
84 * by root, this should be sufficient to ensure that we have
85 * CAP_SYS_ADMIN, which we probably need to do anything fancy
86 * with the (XFS driver) kernel.
88 ctx
->mnt_fd
= open(ctx
->mntpoint
, O_RDONLY
| O_NOATIME
| O_DIRECTORY
);
89 if (ctx
->mnt_fd
< 0) {
91 str_info(ctx
, ctx
->mntpoint
,
92 _("Must be root to run scrub."));
94 str_errno(ctx
, ctx
->mntpoint
);
98 error
= fstat(ctx
->mnt_fd
, &ctx
->mnt_sb
);
100 str_errno(ctx
, ctx
->mntpoint
);
103 error
= fstatvfs(ctx
->mnt_fd
, &ctx
->mnt_sv
);
105 str_errno(ctx
, ctx
->mntpoint
);
108 error
= fstatfs(ctx
->mnt_fd
, &ctx
->mnt_sf
);
110 str_errno(ctx
, ctx
->mntpoint
);
114 if (!platform_test_xfs_fd(ctx
->mnt_fd
)) {
115 str_info(ctx
, ctx
->mntpoint
,
116 _("Does not appear to be an XFS filesystem!"));
121 * Flush everything out to disk before we start checking.
122 * This seems to reduce the incidence of stale file handle
123 * errors when we open things by handle.
125 error
= syncfs(ctx
->mnt_fd
);
127 str_errno(ctx
, ctx
->mntpoint
);
131 /* Retrieve XFS geometry. */
132 error
= ioctl(ctx
->mnt_fd
, XFS_IOC_FSGEOMETRY
, &ctx
->geo
);
134 str_errno(ctx
, ctx
->mntpoint
);
138 if (!xfs_action_lists_alloc(ctx
->geo
.agcount
, &ctx
->action_lists
)) {
139 str_error(ctx
, ctx
->mntpoint
, _("Not enough memory."));
143 ctx
->agblklog
= log2_roundup(ctx
->geo
.agblocks
);
144 ctx
->blocklog
= highbit32(ctx
->geo
.blocksize
);
145 ctx
->inodelog
= highbit32(ctx
->geo
.inodesize
);
146 ctx
->inopblog
= ctx
->blocklog
- ctx
->inodelog
;
148 error
= path_to_fshandle(ctx
->mntpoint
, &ctx
->fshandle
,
151 str_errno(ctx
, _("getting fshandle"));
155 /* Do we have kernel-assisted metadata scrubbing? */
156 if (!xfs_can_scrub_fs_metadata(ctx
) || !xfs_can_scrub_inode(ctx
) ||
157 !xfs_can_scrub_bmap(ctx
) || !xfs_can_scrub_dir(ctx
) ||
158 !xfs_can_scrub_attr(ctx
) || !xfs_can_scrub_symlink(ctx
) ||
159 !xfs_can_scrub_parent(ctx
)) {
160 str_info(ctx
, ctx
->mntpoint
,
161 _("Kernel metadata scrubbing facility is not available."));
165 /* Do we need kernel-assisted metadata repair? */
166 if (ctx
->mode
!= SCRUB_MODE_DRY_RUN
&& !xfs_can_repair(ctx
)) {
167 str_info(ctx
, ctx
->mntpoint
,
168 _("Kernel metadata repair facility is not available. Use -n to scrub."));
172 /* Did we find the log and rt devices, if they're present? */
173 if (ctx
->geo
.logstart
== 0 && ctx
->fsinfo
.fs_log
== NULL
) {
174 str_info(ctx
, ctx
->mntpoint
,
175 _("Unable to find log device path."));
178 if (ctx
->geo
.rtblocks
&& ctx
->fsinfo
.fs_rt
== NULL
) {
179 str_info(ctx
, ctx
->mntpoint
,
180 _("Unable to find realtime device path."));
184 /* Open the raw devices. */
185 ctx
->datadev
= disk_open(ctx
->fsinfo
.fs_name
);
187 str_errno(ctx
, ctx
->fsinfo
.fs_name
);
191 ctx
->nr_io_threads
= disk_heads(ctx
->datadev
);
193 fprintf(stdout
, _("%s: using %d threads to scrub.\n"),
194 ctx
->mntpoint
, scrub_nproc(ctx
));
198 if (ctx
->fsinfo
.fs_log
) {
199 ctx
->logdev
= disk_open(ctx
->fsinfo
.fs_log
);
201 str_errno(ctx
, ctx
->fsinfo
.fs_name
);
205 if (ctx
->fsinfo
.fs_rt
) {
206 ctx
->rtdev
= disk_open(ctx
->fsinfo
.fs_rt
);
208 str_errno(ctx
, ctx
->fsinfo
.fs_name
);
214 * Everything's set up, which means any failures recorded after
215 * this point are most probably corruption errors (as opposed to
216 * purely setup errors).
218 log_info(ctx
, _("Invoking online scrub."), ctx
);
219 ctx
->scrub_setup_succeeded
= true;