]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - scrub/phase1.c
libfrog: refactor online geometry queries
[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 #include "repair.h"
29 #include "fsgeom.h"
30
31 /* Phase 1: Find filesystem geometry (and clean up after) */
32
33 /* Shut down the filesystem. */
34 void
35 xfs_shutdown_fs(
36 struct scrub_ctx *ctx)
37 {
38 int flag;
39
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);
44 }
45
46 /* Clean up the XFS-specific state data. */
47 bool
48 xfs_cleanup_fs(
49 struct scrub_ctx *ctx)
50 {
51 int error;
52
53 xfs_action_lists_free(&ctx->action_lists);
54 if (ctx->fshandle)
55 free_handle(ctx->fshandle, ctx->fshandle_len);
56 if (ctx->rtdev)
57 disk_close(ctx->rtdev);
58 if (ctx->logdev)
59 disk_close(ctx->logdev);
60 if (ctx->datadev)
61 disk_close(ctx->datadev);
62 fshandle_destroy();
63 if (ctx->mnt_fd >= 0) {
64 error = close(ctx->mnt_fd);
65 if (error)
66 str_errno(ctx, _("closing mountpoint fd"));
67 }
68 fs_table_destroy();
69
70 return true;
71 }
72
73 /*
74 * Bind to the mountpoint, read the XFS geometry, bind to the block devices.
75 * Anything we've already built will be cleaned up by xfs_cleanup_fs.
76 */
77 bool
78 xfs_setup_fs(
79 struct scrub_ctx *ctx)
80 {
81 int error;
82
83 /*
84 * Open the directory with O_NOATIME. For mountpoints owned
85 * by root, this should be sufficient to ensure that we have
86 * CAP_SYS_ADMIN, which we probably need to do anything fancy
87 * with the (XFS driver) kernel.
88 */
89 ctx->mnt_fd = open(ctx->mntpoint, O_RDONLY | O_NOATIME | O_DIRECTORY);
90 if (ctx->mnt_fd < 0) {
91 if (errno == EPERM)
92 str_info(ctx, ctx->mntpoint,
93 _("Must be root to run scrub."));
94 else
95 str_errno(ctx, ctx->mntpoint);
96 return false;
97 }
98
99 error = fstat(ctx->mnt_fd, &ctx->mnt_sb);
100 if (error) {
101 str_errno(ctx, ctx->mntpoint);
102 return false;
103 }
104 error = fstatvfs(ctx->mnt_fd, &ctx->mnt_sv);
105 if (error) {
106 str_errno(ctx, ctx->mntpoint);
107 return false;
108 }
109 error = fstatfs(ctx->mnt_fd, &ctx->mnt_sf);
110 if (error) {
111 str_errno(ctx, ctx->mntpoint);
112 return false;
113 }
114
115 if (!platform_test_xfs_fd(ctx->mnt_fd)) {
116 str_info(ctx, ctx->mntpoint,
117 _("Does not appear to be an XFS filesystem!"));
118 return false;
119 }
120
121 /*
122 * Flush everything out to disk before we start checking.
123 * This seems to reduce the incidence of stale file handle
124 * errors when we open things by handle.
125 */
126 error = syncfs(ctx->mnt_fd);
127 if (error) {
128 str_errno(ctx, ctx->mntpoint);
129 return false;
130 }
131
132 /* Retrieve XFS geometry. */
133 error = xfrog_geometry(ctx->mnt_fd, &ctx->geo);
134 if (error) {
135 str_liberror(ctx, error, _("Retrieving XFS geometry"));
136 return false;
137 }
138
139 if (!xfs_action_lists_alloc(ctx->geo.agcount, &ctx->action_lists)) {
140 str_error(ctx, ctx->mntpoint, _("Not enough memory."));
141 return false;
142 }
143
144 ctx->agblklog = log2_roundup(ctx->geo.agblocks);
145 ctx->blocklog = highbit32(ctx->geo.blocksize);
146 ctx->inodelog = highbit32(ctx->geo.inodesize);
147 ctx->inopblog = ctx->blocklog - ctx->inodelog;
148
149 error = path_to_fshandle(ctx->mntpoint, &ctx->fshandle,
150 &ctx->fshandle_len);
151 if (error) {
152 str_errno(ctx, _("getting fshandle"));
153 return false;
154 }
155
156 /* Do we have kernel-assisted metadata scrubbing? */
157 if (!xfs_can_scrub_fs_metadata(ctx) || !xfs_can_scrub_inode(ctx) ||
158 !xfs_can_scrub_bmap(ctx) || !xfs_can_scrub_dir(ctx) ||
159 !xfs_can_scrub_attr(ctx) || !xfs_can_scrub_symlink(ctx) ||
160 !xfs_can_scrub_parent(ctx)) {
161 str_info(ctx, ctx->mntpoint,
162 _("Kernel metadata scrubbing facility is not available."));
163 return false;
164 }
165
166 /* Do we need kernel-assisted metadata repair? */
167 if (ctx->mode != SCRUB_MODE_DRY_RUN && !xfs_can_repair(ctx)) {
168 str_info(ctx, ctx->mntpoint,
169 _("Kernel metadata repair facility is not available. Use -n to scrub."));
170 return false;
171 }
172
173 /* Did we find the log and rt devices, if they're present? */
174 if (ctx->geo.logstart == 0 && ctx->fsinfo.fs_log == NULL) {
175 str_info(ctx, ctx->mntpoint,
176 _("Unable to find log device path."));
177 return false;
178 }
179 if (ctx->geo.rtblocks && ctx->fsinfo.fs_rt == NULL) {
180 str_info(ctx, ctx->mntpoint,
181 _("Unable to find realtime device path."));
182 return false;
183 }
184
185 /* Open the raw devices. */
186 ctx->datadev = disk_open(ctx->fsinfo.fs_name);
187 if (error) {
188 str_errno(ctx, ctx->fsinfo.fs_name);
189 return false;
190 }
191
192 ctx->nr_io_threads = disk_heads(ctx->datadev);
193 if (verbose) {
194 fprintf(stdout, _("%s: using %d threads to scrub.\n"),
195 ctx->mntpoint, scrub_nproc(ctx));
196 fflush(stdout);
197 }
198
199 if (ctx->fsinfo.fs_log) {
200 ctx->logdev = disk_open(ctx->fsinfo.fs_log);
201 if (error) {
202 str_errno(ctx, ctx->fsinfo.fs_name);
203 return false;
204 }
205 }
206 if (ctx->fsinfo.fs_rt) {
207 ctx->rtdev = disk_open(ctx->fsinfo.fs_rt);
208 if (error) {
209 str_errno(ctx, ctx->fsinfo.fs_name);
210 return false;
211 }
212 }
213
214 /*
215 * Everything's set up, which means any failures recorded after
216 * this point are most probably corruption errors (as opposed to
217 * purely setup errors).
218 */
219 log_info(ctx, _("Invoking online scrub."), ctx);
220 ctx->scrub_setup_succeeded = true;
221 return true;
222 }