]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - scrub/phase1.c
xfsprogs: convert to SPDX license tags
[thirdparty/xfsprogs-dev.git] / scrub / phase1.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2018 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
5 */
6 #include "xfs.h"
7 #include <unistd.h>
8 #include <sys/types.h>
9 #include <sys/time.h>
10 #include <sys/resource.h>
11 #include <sys/statvfs.h>
12 #include <fcntl.h>
13 #include <dirent.h>
14 #include <stdint.h>
15 #include <pthread.h>
16 #include "libfrog.h"
17 #include "workqueue.h"
18 #include "input.h"
19 #include "path.h"
20 #include "handle.h"
21 #include "bitops.h"
22 #include "avl64.h"
23 #include "list.h"
24 #include "xfs_scrub.h"
25 #include "common.h"
26 #include "disk.h"
27 #include "scrub.h"
28
29 /* Phase 1: Find filesystem geometry (and clean up after) */
30
31 /* Shut down the filesystem. */
32 void
33 xfs_shutdown_fs(
34 struct scrub_ctx *ctx)
35 {
36 int flag;
37
38 flag = XFS_FSOP_GOING_FLAGS_LOGFLUSH;
39 str_info(ctx, ctx->mntpoint, _("Shutting down filesystem!"));
40 if (ioctl(ctx->mnt_fd, XFS_IOC_GOINGDOWN, &flag))
41 str_errno(ctx, ctx->mntpoint);
42 }
43
44 /* Clean up the XFS-specific state data. */
45 bool
46 xfs_cleanup_fs(
47 struct scrub_ctx *ctx)
48 {
49 int error;
50
51 if (ctx->fshandle)
52 free_handle(ctx->fshandle, ctx->fshandle_len);
53 if (ctx->rtdev)
54 disk_close(ctx->rtdev);
55 if (ctx->logdev)
56 disk_close(ctx->logdev);
57 if (ctx->datadev)
58 disk_close(ctx->datadev);
59 fshandle_destroy();
60 error = close(ctx->mnt_fd);
61 if (error)
62 str_errno(ctx, _("closing mountpoint fd"));
63 fs_table_destroy();
64
65 return true;
66 }
67
68 /*
69 * Bind to the mountpoint, read the XFS geometry, bind to the block devices.
70 * Anything we've already built will be cleaned up by xfs_cleanup_fs.
71 */
72 bool
73 xfs_setup_fs(
74 struct scrub_ctx *ctx)
75 {
76 int error;
77
78 /*
79 * Open the directory with O_NOATIME. For mountpoints owned
80 * by root, this should be sufficient to ensure that we have
81 * CAP_SYS_ADMIN, which we probably need to do anything fancy
82 * with the (XFS driver) kernel.
83 */
84 ctx->mnt_fd = open(ctx->mntpoint, O_RDONLY | O_NOATIME | O_DIRECTORY);
85 if (ctx->mnt_fd < 0) {
86 if (errno == EPERM)
87 str_info(ctx, ctx->mntpoint,
88 _("Must be root to run scrub."));
89 else
90 str_errno(ctx, ctx->mntpoint);
91 return false;
92 }
93
94 error = fstat(ctx->mnt_fd, &ctx->mnt_sb);
95 if (error) {
96 str_errno(ctx, ctx->mntpoint);
97 return false;
98 }
99 error = fstatvfs(ctx->mnt_fd, &ctx->mnt_sv);
100 if (error) {
101 str_errno(ctx, ctx->mntpoint);
102 return false;
103 }
104 error = fstatfs(ctx->mnt_fd, &ctx->mnt_sf);
105 if (error) {
106 str_errno(ctx, ctx->mntpoint);
107 return false;
108 }
109
110 ctx->nr_io_threads = nproc;
111 if (verbose) {
112 fprintf(stdout, _("%s: using %d threads to scrub.\n"),
113 ctx->mntpoint, scrub_nproc(ctx));
114 fflush(stdout);
115 }
116
117 if (!platform_test_xfs_fd(ctx->mnt_fd)) {
118 str_info(ctx, ctx->mntpoint,
119 _("Does not appear to be an XFS filesystem!"));
120 return false;
121 }
122
123 /*
124 * Flush everything out to disk before we start checking.
125 * This seems to reduce the incidence of stale file handle
126 * errors when we open things by handle.
127 */
128 error = syncfs(ctx->mnt_fd);
129 if (error) {
130 str_errno(ctx, ctx->mntpoint);
131 return false;
132 }
133
134 /* Retrieve XFS geometry. */
135 error = ioctl(ctx->mnt_fd, XFS_IOC_FSGEOMETRY, &ctx->geo);
136 if (error) {
137 str_errno(ctx, ctx->mntpoint);
138 return false;
139 }
140
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;
145
146 error = path_to_fshandle(ctx->mntpoint, &ctx->fshandle,
147 &ctx->fshandle_len);
148 if (error) {
149 str_errno(ctx, _("getting fshandle"));
150 return false;
151 }
152
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."));
160 return false;
161 }
162
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."));
167 return false;
168 }
169
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."));
174 return false;
175 }
176 if (ctx->geo.rtblocks && ctx->fsinfo.fs_rt == NULL) {
177 str_info(ctx, ctx->mntpoint,
178 _("Unable to find realtime device path."));
179 return false;
180 }
181
182 /* Open the raw devices. */
183 ctx->datadev = disk_open(ctx->fsinfo.fs_name);
184 if (error) {
185 str_errno(ctx, ctx->fsinfo.fs_name);
186 return false;
187 }
188
189 if (ctx->fsinfo.fs_log) {
190 ctx->logdev = disk_open(ctx->fsinfo.fs_log);
191 if (error) {
192 str_errno(ctx, ctx->fsinfo.fs_name);
193 return false;
194 }
195 }
196 if (ctx->fsinfo.fs_rt) {
197 ctx->rtdev = disk_open(ctx->fsinfo.fs_rt);
198 if (error) {
199 str_errno(ctx, ctx->fsinfo.fs_name);
200 return false;
201 }
202 }
203
204 /*
205 * Everything's set up, which means any failures recorded after
206 * this point are most probably corruption errors (as opposed to
207 * purely setup errors).
208 */
209 log_info(ctx, _("Invoking online scrub."), ctx);
210 ctx->scrub_setup_succeeded = true;
211 return true;
212 }