]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - scrub/inodes.c
libfrog: fix workqueue error communication problems
[thirdparty/xfsprogs-dev.git] / scrub / inodes.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 <stdint.h>
8 #include <stdlib.h>
9 #include <pthread.h>
10 #include <sys/statvfs.h>
11 #include "platform_defs.h"
12 #include "xfs_arch.h"
13 #include "xfs_format.h"
14 #include "handle.h"
15 #include "libfrog/paths.h"
16 #include "libfrog/workqueue.h"
17 #include "xfs_scrub.h"
18 #include "common.h"
19 #include "inodes.h"
20 #include "libfrog/fsgeom.h"
21 #include "libfrog/bulkstat.h"
22
23 /*
24 * Iterate a range of inodes.
25 *
26 * This is a little more involved than repeatedly asking BULKSTAT for a
27 * buffer's worth of stat data for some number of inodes. We want to scan as
28 * many of the inodes that the inobt thinks there are, including the ones that
29 * are broken, but if we ask for n inodes starting at x, it'll skip the bad
30 * ones and fill from beyond the range (x + n).
31 *
32 * Therefore, we ask INUMBERS to return one inobt chunk's worth of inode
33 * bitmap information. Then we try to BULKSTAT only the inodes that were
34 * present in that chunk, and compare what we got against what INUMBERS said
35 * was there. If there's a mismatch, we know that we have an inode that fails
36 * the verifiers but we can inject the bulkstat information to force the scrub
37 * code to deal with the broken inodes.
38 *
39 * If the iteration function returns ESTALE, that means that the inode has
40 * been deleted and possibly recreated since the BULKSTAT call. We wil
41 * refresh the stat information and try again up to 30 times before reporting
42 * the staleness as an error.
43 */
44
45 /*
46 * Did we get exactly the inodes we expected? If not, load them one at a
47 * time (or fake it) into the bulkstat data.
48 */
49 static void
50 xfs_iterate_inodes_range_check(
51 struct scrub_ctx *ctx,
52 struct xfs_inumbers *inumbers,
53 struct xfs_bulkstat *bstat)
54 {
55 struct xfs_bulkstat *bs;
56 int i;
57 int error;
58
59 for (i = 0, bs = bstat; i < XFS_INODES_PER_CHUNK; i++) {
60 if (!(inumbers->xi_allocmask & (1ULL << i)))
61 continue;
62 if (bs->bs_ino == inumbers->xi_startino + i) {
63 bs++;
64 continue;
65 }
66
67 /* Load the one inode. */
68 error = xfrog_bulkstat_single(&ctx->mnt,
69 inumbers->xi_startino + i, 0, bs);
70 if (error || bs->bs_ino != inumbers->xi_startino + i) {
71 memset(bs, 0, sizeof(struct xfs_bulkstat));
72 bs->bs_ino = inumbers->xi_startino + i;
73 bs->bs_blksize = ctx->mnt_sv.f_frsize;
74 }
75 bs++;
76 }
77 }
78
79 /*
80 * Call into the filesystem for inode/bulkstat information and call our
81 * iterator function. We'll try to fill the bulkstat information in batches,
82 * but we also can detect iget failures.
83 */
84 static bool
85 xfs_iterate_inodes_ag(
86 struct scrub_ctx *ctx,
87 const char *descr,
88 void *fshandle,
89 uint32_t agno,
90 xfs_inode_iter_fn fn,
91 void *arg)
92 {
93 struct xfs_handle handle;
94 struct xfs_inumbers_req *ireq;
95 struct xfs_bulkstat_req *breq;
96 char idescr[DESCR_BUFSZ];
97 struct xfs_bulkstat *bs;
98 struct xfs_inumbers *inumbers;
99 bool moveon = true;
100 int i;
101 int error;
102 int stale_count = 0;
103
104 memcpy(&handle.ha_fsid, fshandle, sizeof(handle.ha_fsid));
105 handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
106 sizeof(handle.ha_fid.fid_len);
107 handle.ha_fid.fid_pad = 0;
108
109 breq = xfrog_bulkstat_alloc_req(XFS_INODES_PER_CHUNK, 0);
110 if (!breq) {
111 str_info(ctx, descr, _("Insufficient memory; giving up."));
112 return false;
113 }
114
115 ireq = xfrog_inumbers_alloc_req(1, 0);
116 if (!ireq) {
117 str_info(ctx, descr, _("Insufficient memory; giving up."));
118 free(breq);
119 return false;
120 }
121 inumbers = &ireq->inumbers[0];
122 xfrog_inumbers_set_ag(ireq, agno);
123
124 /* Find the inode chunk & alloc mask */
125 error = xfrog_inumbers(&ctx->mnt, ireq);
126 while (!error && ireq->hdr.ocount > 0) {
127 /*
128 * We can have totally empty inode chunks on filesystems where
129 * there are more than 64 inodes per block. Skip these.
130 */
131 if (inumbers->xi_alloccount == 0)
132 goto igrp_retry;
133
134 breq->hdr.ino = inumbers->xi_startino;
135 breq->hdr.icount = inumbers->xi_alloccount;
136 error = xfrog_bulkstat(&ctx->mnt, breq);
137 if (error) {
138 char errbuf[DESCR_BUFSZ];
139
140 str_info(ctx, descr, "%s", strerror_r(error,
141 errbuf, DESCR_BUFSZ));
142 }
143
144 xfs_iterate_inodes_range_check(ctx, inumbers, breq->bulkstat);
145
146 /* Iterate all the inodes. */
147 for (i = 0, bs = breq->bulkstat;
148 i < inumbers->xi_alloccount;
149 i++, bs++) {
150 handle.ha_fid.fid_ino = bs->bs_ino;
151 handle.ha_fid.fid_gen = bs->bs_gen;
152 error = fn(ctx, &handle, bs, arg);
153 switch (error) {
154 case 0:
155 break;
156 case ESTALE:
157 stale_count++;
158 if (stale_count < 30) {
159 ireq->hdr.ino = inumbers->xi_startino;
160 goto igrp_retry;
161 }
162 snprintf(idescr, DESCR_BUFSZ, "inode %"PRIu64,
163 (uint64_t)bs->bs_ino);
164 str_info(ctx, idescr,
165 _("Changed too many times during scan; giving up."));
166 break;
167 case XFS_ITERATE_INODES_ABORT:
168 error = 0;
169 /* fall thru */
170 default:
171 moveon = false;
172 errno = error;
173 goto err;
174 }
175 if (xfs_scrub_excessive_errors(ctx)) {
176 moveon = false;
177 goto out;
178 }
179 }
180
181 stale_count = 0;
182 igrp_retry:
183 error = xfrog_inumbers(&ctx->mnt, ireq);
184 }
185
186 err:
187 if (error) {
188 str_liberror(ctx, error, descr);
189 moveon = false;
190 }
191 out:
192 free(ireq);
193 free(breq);
194 return moveon;
195 }
196
197 /* BULKSTAT wrapper routines. */
198 struct xfs_scan_inodes {
199 xfs_inode_iter_fn fn;
200 void *arg;
201 bool moveon;
202 };
203
204 /* Scan all the inodes in an AG. */
205 static void
206 xfs_scan_ag_inodes(
207 struct workqueue *wq,
208 xfs_agnumber_t agno,
209 void *arg)
210 {
211 struct xfs_scan_inodes *si = arg;
212 struct scrub_ctx *ctx = (struct scrub_ctx *)wq->wq_ctx;
213 char descr[DESCR_BUFSZ];
214 bool moveon;
215
216 snprintf(descr, DESCR_BUFSZ, _("dev %d:%d AG %u inodes"),
217 major(ctx->fsinfo.fs_datadev),
218 minor(ctx->fsinfo.fs_datadev),
219 agno);
220
221 moveon = xfs_iterate_inodes_ag(ctx, descr, ctx->fshandle, agno,
222 si->fn, si->arg);
223 if (!moveon)
224 si->moveon = false;
225 }
226
227 /* Scan all the inodes in a filesystem. */
228 bool
229 xfs_scan_all_inodes(
230 struct scrub_ctx *ctx,
231 xfs_inode_iter_fn fn,
232 void *arg)
233 {
234 struct xfs_scan_inodes si;
235 xfs_agnumber_t agno;
236 struct workqueue wq;
237 int ret;
238
239 si.moveon = true;
240 si.fn = fn;
241 si.arg = arg;
242
243 ret = workqueue_create(&wq, (struct xfs_mount *)ctx,
244 scrub_nproc_workqueue(ctx));
245 if (ret) {
246 str_liberror(ctx, ret, _("creating bulkstat workqueue"));
247 return false;
248 }
249
250 for (agno = 0; agno < ctx->mnt.fsgeom.agcount; agno++) {
251 ret = workqueue_add(&wq, xfs_scan_ag_inodes, agno, &si);
252 if (ret) {
253 si.moveon = false;
254 str_liberror(ctx, ret, _("queueing bulkstat work"));
255 break;
256 }
257 }
258
259 workqueue_destroy(&wq);
260
261 return si.moveon;
262 }
263
264 /*
265 * Open a file by handle, or return a negative error code.
266 */
267 int
268 xfs_open_handle(
269 struct xfs_handle *handle)
270 {
271 return open_by_fshandle(handle, sizeof(*handle),
272 O_RDONLY | O_NOATIME | O_NOFOLLOW | O_NOCTTY);
273 }