blob: 8f3cba14ada32a6f6071c38eaf9770a22f84b339 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0+
Darrick J. Wong84d42ea2018-05-14 06:34:36 -07002/*
3 * Copyright (C) 2018 Oracle. All Rights Reserved.
Darrick J. Wong84d42ea2018-05-14 06:34:36 -07004 * Author: Darrick J. Wong <darrick.wong@oracle.com>
Darrick J. Wong84d42ea2018-05-14 06:34:36 -07005 */
6#include "xfs.h"
7#include "xfs_fs.h"
8#include "xfs_shared.h"
9#include "xfs_format.h"
10#include "xfs_trans_resv.h"
11#include "xfs_mount.h"
Darrick J. Wong84d42ea2018-05-14 06:34:36 -070012#include "xfs_btree.h"
Darrick J. Wong84d42ea2018-05-14 06:34:36 -070013#include "xfs_log_format.h"
14#include "xfs_trans.h"
15#include "xfs_sb.h"
16#include "xfs_inode.h"
Darrick J. Wong84d42ea2018-05-14 06:34:36 -070017#include "xfs_alloc.h"
18#include "xfs_alloc_btree.h"
19#include "xfs_ialloc.h"
20#include "xfs_ialloc_btree.h"
21#include "xfs_rmap.h"
22#include "xfs_rmap_btree.h"
Darrick J. Wong84d42ea2018-05-14 06:34:36 -070023#include "xfs_refcount_btree.h"
24#include "xfs_extent_busy.h"
Dave Chinner9bbafc712021-06-02 10:48:24 +100025#include "xfs_ag.h"
Darrick J. Wong84d42ea2018-05-14 06:34:36 -070026#include "xfs_ag_resv.h"
Darrick J. Wong7e85bc62018-05-29 22:18:11 -070027#include "xfs_quota.h"
Darrick J. Wong84d42ea2018-05-14 06:34:36 -070028#include "scrub/scrub.h"
29#include "scrub/common.h"
30#include "scrub/trace.h"
31#include "scrub/repair.h"
Darrick J. Wongbc270b52018-07-29 22:37:09 -070032#include "scrub/bitmap.h"
Darrick J. Wong84d42ea2018-05-14 06:34:36 -070033
34/*
35 * Attempt to repair some metadata, if the metadata is corrupt and userspace
36 * told us to fix it. This function returns -EAGAIN to mean "re-run scrub",
37 * and will set *fixed to true if it thinks it repaired anything.
38 */
39int
Darrick J. Wongb5e21962018-07-19 12:29:11 -070040xrep_attempt(
Darrick J. Wong160b5a72019-04-16 08:22:00 -070041 struct xfs_scrub *sc)
Darrick J. Wong84d42ea2018-05-14 06:34:36 -070042{
Darrick J. Wong032d91f2018-07-19 12:29:12 -070043 int error = 0;
Darrick J. Wong84d42ea2018-05-14 06:34:36 -070044
Darrick J. Wong026f57e2021-04-07 17:59:39 -070045 trace_xrep_attempt(XFS_I(file_inode(sc->file)), sc->sm, error);
Darrick J. Wong84d42ea2018-05-14 06:34:36 -070046
Darrick J. Wongc517b3a2018-07-19 12:29:11 -070047 xchk_ag_btcur_free(&sc->sa);
Darrick J. Wong84d42ea2018-05-14 06:34:36 -070048
49 /* Repair whatever's broken. */
50 ASSERT(sc->ops->repair);
51 error = sc->ops->repair(sc);
Darrick J. Wong026f57e2021-04-07 17:59:39 -070052 trace_xrep_done(XFS_I(file_inode(sc->file)), sc->sm, error);
Darrick J. Wong84d42ea2018-05-14 06:34:36 -070053 switch (error) {
54 case 0:
55 /*
56 * Repair succeeded. Commit the fixes and perform a second
57 * scrub so that we can tell userspace if we fixed the problem.
58 */
59 sc->sm->sm_flags &= ~XFS_SCRUB_FLAGS_OUT;
Darrick J. Wong160b5a72019-04-16 08:22:00 -070060 sc->flags |= XREP_ALREADY_FIXED;
Darrick J. Wong84d42ea2018-05-14 06:34:36 -070061 return -EAGAIN;
62 case -EDEADLOCK:
63 case -EAGAIN:
64 /* Tell the caller to try again having grabbed all the locks. */
Darrick J. Wongf8c2a222019-04-16 08:21:59 -070065 if (!(sc->flags & XCHK_TRY_HARDER)) {
66 sc->flags |= XCHK_TRY_HARDER;
Darrick J. Wong84d42ea2018-05-14 06:34:36 -070067 return -EAGAIN;
68 }
69 /*
70 * We tried harder but still couldn't grab all the resources
71 * we needed to fix it. The corruption has not been fixed,
72 * so report back to userspace.
73 */
74 return -EFSCORRUPTED;
75 default:
76 return error;
77 }
78}
79
80/*
81 * Complain about unfixable problems in the filesystem. We don't log
82 * corruptions when IFLAG_REPAIR wasn't set on the assumption that the driver
83 * program is xfs_scrub, which will call back with IFLAG_REPAIR set if the
84 * administrator isn't running xfs_scrub in no-repairs mode.
85 *
86 * Use this helper function because _ratelimited silently declares a static
87 * structure to track rate limiting information.
88 */
89void
Darrick J. Wongb5e21962018-07-19 12:29:11 -070090xrep_failure(
Darrick J. Wong032d91f2018-07-19 12:29:12 -070091 struct xfs_mount *mp)
Darrick J. Wong84d42ea2018-05-14 06:34:36 -070092{
93 xfs_alert_ratelimited(mp,
94"Corruption not fixed during online repair. Unmount and run xfs_repair.");
95}
96
97/*
98 * Repair probe -- userspace uses this to probe if we're willing to repair a
99 * given mountpoint.
100 */
101int
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700102xrep_probe(
Darrick J. Wong1d8a7482018-07-19 12:29:12 -0700103 struct xfs_scrub *sc)
Darrick J. Wong84d42ea2018-05-14 06:34:36 -0700104{
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700105 int error = 0;
Darrick J. Wong84d42ea2018-05-14 06:34:36 -0700106
Darrick J. Wongc517b3a2018-07-19 12:29:11 -0700107 if (xchk_should_terminate(sc, &error))
Darrick J. Wong84d42ea2018-05-14 06:34:36 -0700108 return error;
109
110 return 0;
111}
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700112
113/*
114 * Roll a transaction, keeping the AG headers locked and reinitializing
115 * the btree cursors.
116 */
117int
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700118xrep_roll_ag_trans(
Darrick J. Wong1d8a7482018-07-19 12:29:12 -0700119 struct xfs_scrub *sc)
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700120{
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700121 int error;
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700122
123 /* Keep the AG header buffers locked so we can keep going. */
Darrick J. Wongf9ed6de2018-08-09 22:42:53 -0700124 if (sc->sa.agi_bp)
125 xfs_trans_bhold(sc->tp, sc->sa.agi_bp);
126 if (sc->sa.agf_bp)
127 xfs_trans_bhold(sc->tp, sc->sa.agf_bp);
128 if (sc->sa.agfl_bp)
129 xfs_trans_bhold(sc->tp, sc->sa.agfl_bp);
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700130
Darrick J. Wongf60be902019-04-24 10:39:49 -0700131 /*
132 * Roll the transaction. We still own the buffer and the buffer lock
133 * regardless of whether or not the roll succeeds. If the roll fails,
134 * the buffers will be released during teardown on our way out of the
135 * kernel. If it succeeds, we join them to the new transaction and
136 * move on.
137 */
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700138 error = xfs_trans_roll(&sc->tp);
139 if (error)
Darrick J. Wongf60be902019-04-24 10:39:49 -0700140 return error;
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700141
142 /* Join AG headers to the new transaction. */
Darrick J. Wongf9ed6de2018-08-09 22:42:53 -0700143 if (sc->sa.agi_bp)
144 xfs_trans_bjoin(sc->tp, sc->sa.agi_bp);
145 if (sc->sa.agf_bp)
146 xfs_trans_bjoin(sc->tp, sc->sa.agf_bp);
147 if (sc->sa.agfl_bp)
148 xfs_trans_bjoin(sc->tp, sc->sa.agfl_bp);
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700149
150 return 0;
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700151}
152
153/*
154 * Does the given AG have enough space to rebuild a btree? Neither AG
155 * reservation can be critical, and we must have enough space (factoring
156 * in AG reservations) to construct a whole btree.
157 */
158bool
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700159xrep_ag_has_space(
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700160 struct xfs_perag *pag,
161 xfs_extlen_t nr_blocks,
162 enum xfs_ag_resv_type type)
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700163{
164 return !xfs_ag_resv_critical(pag, XFS_AG_RESV_RMAPBT) &&
165 !xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA) &&
166 pag->pagf_freeblks > xfs_ag_resv_needed(pag, type) + nr_blocks;
167}
168
169/*
170 * Figure out how many blocks to reserve for an AG repair. We calculate the
171 * worst case estimate for the number of blocks we'd need to rebuild one of
172 * any type of per-AG btree.
173 */
174xfs_extlen_t
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700175xrep_calc_ag_resblks(
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700176 struct xfs_scrub *sc)
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700177{
178 struct xfs_mount *mp = sc->mp;
179 struct xfs_scrub_metadata *sm = sc->sm;
180 struct xfs_perag *pag;
181 struct xfs_buf *bp;
Darrick J. Wong1fc25f52018-08-10 17:55:57 -0700182 xfs_agino_t icount = NULLAGINO;
183 xfs_extlen_t aglen = NULLAGBLOCK;
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700184 xfs_extlen_t usedlen;
185 xfs_extlen_t freelen;
186 xfs_extlen_t bnobt_sz;
187 xfs_extlen_t inobt_sz;
188 xfs_extlen_t rmapbt_sz;
189 xfs_extlen_t refcbt_sz;
190 int error;
191
192 if (!(sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR))
193 return 0;
194
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700195 pag = xfs_perag_get(mp, sm->sm_agno);
Darrick J. Wong1fc25f52018-08-10 17:55:57 -0700196 if (pag->pagi_init) {
197 /* Use in-core icount if possible. */
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700198 icount = pag->pagi_count;
Darrick J. Wong1fc25f52018-08-10 17:55:57 -0700199 } else {
200 /* Try to get the actual counters from disk. */
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700201 error = xfs_ialloc_read_agi(mp, NULL, sm->sm_agno, &bp);
Darrick J. Wong1fc25f52018-08-10 17:55:57 -0700202 if (!error) {
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700203 icount = pag->pagi_count;
204 xfs_buf_relse(bp);
205 }
206 }
207
208 /* Now grab the block counters from the AGF. */
209 error = xfs_alloc_read_agf(mp, NULL, sm->sm_agno, 0, &bp);
Darrick J. Wong1aa26702021-03-22 09:51:51 -0700210 if (error) {
211 aglen = xfs_ag_block_count(mp, sm->sm_agno);
212 freelen = aglen;
213 usedlen = aglen;
214 } else {
Christoph Hellwig9798f612020-03-10 08:57:29 -0700215 struct xfs_agf *agf = bp->b_addr;
216
217 aglen = be32_to_cpu(agf->agf_length);
218 freelen = be32_to_cpu(agf->agf_freeblks);
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700219 usedlen = aglen - freelen;
220 xfs_buf_relse(bp);
221 }
222 xfs_perag_put(pag);
223
Darrick J. Wong1fc25f52018-08-10 17:55:57 -0700224 /* If the icount is impossible, make some worst-case assumptions. */
225 if (icount == NULLAGINO ||
226 !xfs_verify_agino(mp, sm->sm_agno, icount)) {
227 xfs_agino_t first, last;
228
229 xfs_agino_range(mp, sm->sm_agno, &first, &last);
230 icount = last - first + 1;
231 }
232
233 /* If the block counts are impossible, make worst-case assumptions. */
234 if (aglen == NULLAGBLOCK ||
235 aglen != xfs_ag_block_count(mp, sm->sm_agno) ||
236 freelen >= aglen) {
237 aglen = xfs_ag_block_count(mp, sm->sm_agno);
238 freelen = aglen;
239 usedlen = aglen;
240 }
241
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700242 trace_xrep_calc_ag_resblks(mp, sm->sm_agno, icount, aglen,
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700243 freelen, usedlen);
244
245 /*
246 * Figure out how many blocks we'd need worst case to rebuild
247 * each type of btree. Note that we can only rebuild the
248 * bnobt/cntbt or inobt/finobt as pairs.
249 */
250 bnobt_sz = 2 * xfs_allocbt_calc_size(mp, freelen);
Dave Chinner38c26bf2021-08-18 18:46:37 -0700251 if (xfs_has_sparseinodes(mp))
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700252 inobt_sz = xfs_iallocbt_calc_size(mp, icount /
253 XFS_INODES_PER_HOLEMASK_BIT);
254 else
255 inobt_sz = xfs_iallocbt_calc_size(mp, icount /
256 XFS_INODES_PER_CHUNK);
Dave Chinner38c26bf2021-08-18 18:46:37 -0700257 if (xfs_has_finobt(mp))
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700258 inobt_sz *= 2;
Dave Chinner38c26bf2021-08-18 18:46:37 -0700259 if (xfs_has_reflink(mp))
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700260 refcbt_sz = xfs_refcountbt_calc_size(mp, usedlen);
261 else
262 refcbt_sz = 0;
Dave Chinner38c26bf2021-08-18 18:46:37 -0700263 if (xfs_has_rmapbt(mp)) {
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700264 /*
265 * Guess how many blocks we need to rebuild the rmapbt.
266 * For non-reflink filesystems we can't have more records than
267 * used blocks. However, with reflink it's possible to have
268 * more than one rmap record per AG block. We don't know how
269 * many rmaps there could be in the AG, so we start off with
270 * what we hope is an generous over-estimation.
271 */
Dave Chinner38c26bf2021-08-18 18:46:37 -0700272 if (xfs_has_reflink(mp))
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700273 rmapbt_sz = xfs_rmapbt_calc_size(mp,
274 (unsigned long long)aglen * 2);
275 else
276 rmapbt_sz = xfs_rmapbt_calc_size(mp, usedlen);
277 } else {
278 rmapbt_sz = 0;
279 }
280
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700281 trace_xrep_calc_ag_resblks_btsize(mp, sm->sm_agno, bnobt_sz,
Darrick J. Wong0a9633f2018-05-29 22:18:08 -0700282 inobt_sz, rmapbt_sz, refcbt_sz);
283
284 return max(max(bnobt_sz, inobt_sz), max(rmapbt_sz, refcbt_sz));
285}
Darrick J. Wong73d6b422018-05-29 22:18:09 -0700286
287/* Allocate a block in an AG. */
288int
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700289xrep_alloc_ag_block(
Darrick J. Wong66e32372018-12-12 08:46:23 -0800290 struct xfs_scrub *sc,
291 const struct xfs_owner_info *oinfo,
292 xfs_fsblock_t *fsbno,
293 enum xfs_ag_resv_type resv)
Darrick J. Wong73d6b422018-05-29 22:18:09 -0700294{
Darrick J. Wong66e32372018-12-12 08:46:23 -0800295 struct xfs_alloc_arg args = {0};
296 xfs_agblock_t bno;
297 int error;
Darrick J. Wong73d6b422018-05-29 22:18:09 -0700298
299 switch (resv) {
300 case XFS_AG_RESV_AGFL:
301 case XFS_AG_RESV_RMAPBT:
302 error = xfs_alloc_get_freelist(sc->tp, sc->sa.agf_bp, &bno, 1);
303 if (error)
304 return error;
305 if (bno == NULLAGBLOCK)
306 return -ENOSPC;
Dave Chinner45d06622021-06-02 10:48:24 +1000307 xfs_extent_busy_reuse(sc->mp, sc->sa.pag, bno,
Darrick J. Wong73d6b422018-05-29 22:18:09 -0700308 1, false);
Darrick J. Wong54406762021-08-09 16:31:14 -0700309 *fsbno = XFS_AGB_TO_FSB(sc->mp, sc->sa.pag->pag_agno, bno);
Darrick J. Wong73d6b422018-05-29 22:18:09 -0700310 if (resv == XFS_AG_RESV_RMAPBT)
Darrick J. Wong54406762021-08-09 16:31:14 -0700311 xfs_ag_resv_rmapbt_alloc(sc->mp, sc->sa.pag->pag_agno);
Darrick J. Wong73d6b422018-05-29 22:18:09 -0700312 return 0;
313 default:
314 break;
315 }
316
317 args.tp = sc->tp;
318 args.mp = sc->mp;
319 args.oinfo = *oinfo;
Darrick J. Wong54406762021-08-09 16:31:14 -0700320 args.fsbno = XFS_AGB_TO_FSB(args.mp, sc->sa.pag->pag_agno, 0);
Darrick J. Wong73d6b422018-05-29 22:18:09 -0700321 args.minlen = 1;
322 args.maxlen = 1;
323 args.prod = 1;
324 args.type = XFS_ALLOCTYPE_THIS_AG;
325 args.resv = resv;
326
327 error = xfs_alloc_vextent(&args);
328 if (error)
329 return error;
330 if (args.fsbno == NULLFSBLOCK)
331 return -ENOSPC;
332 ASSERT(args.len == 1);
333 *fsbno = args.fsbno;
334
335 return 0;
336}
337
338/* Initialize a new AG btree root block with zero entries. */
339int
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700340xrep_init_btblock(
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700341 struct xfs_scrub *sc,
Darrick J. Wong73d6b422018-05-29 22:18:09 -0700342 xfs_fsblock_t fsb,
343 struct xfs_buf **bpp,
344 xfs_btnum_t btnum,
345 const struct xfs_buf_ops *ops)
346{
347 struct xfs_trans *tp = sc->tp;
348 struct xfs_mount *mp = sc->mp;
349 struct xfs_buf *bp;
Darrick J. Wongce924642020-01-23 17:01:18 -0800350 int error;
Darrick J. Wong73d6b422018-05-29 22:18:09 -0700351
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700352 trace_xrep_init_btblock(mp, XFS_FSB_TO_AGNO(mp, fsb),
Darrick J. Wong73d6b422018-05-29 22:18:09 -0700353 XFS_FSB_TO_AGBNO(mp, fsb), btnum);
354
Darrick J. Wong54406762021-08-09 16:31:14 -0700355 ASSERT(XFS_FSB_TO_AGNO(mp, fsb) == sc->sa.pag->pag_agno);
Darrick J. Wongce924642020-01-23 17:01:18 -0800356 error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
357 XFS_FSB_TO_DADDR(mp, fsb), XFS_FSB_TO_BB(mp, 1), 0,
358 &bp);
359 if (error)
360 return error;
Darrick J. Wong73d6b422018-05-29 22:18:09 -0700361 xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
Darrick J. Wong54406762021-08-09 16:31:14 -0700362 xfs_btree_init_block(mp, bp, btnum, 0, 0, sc->sa.pag->pag_agno);
Darrick J. Wong73d6b422018-05-29 22:18:09 -0700363 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_BTREE_BUF);
Eric Sandeen7f313ed2019-08-27 17:35:12 -0700364 xfs_trans_log_buf(tp, bp, 0, BBTOB(bp->b_length) - 1);
Darrick J. Wong73d6b422018-05-29 22:18:09 -0700365 bp->b_ops = ops;
366 *bpp = bp;
367
368 return 0;
369}
Darrick J. Wong64a39d82018-05-29 22:18:09 -0700370
371/*
372 * Reconstructing per-AG Btrees
373 *
374 * When a space btree is corrupt, we don't bother trying to fix it. Instead,
375 * we scan secondary space metadata to derive the records that should be in
376 * the damaged btree, initialize a fresh btree root, and insert the records.
377 * Note that for rebuilding the rmapbt we scan all the primary data to
378 * generate the new records.
379 *
380 * However, that leaves the matter of removing all the metadata describing the
381 * old broken structure. For primary metadata we use the rmap data to collect
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700382 * every extent with a matching rmap owner (bitmap); we then iterate all other
Darrick J. Wong64a39d82018-05-29 22:18:09 -0700383 * metadata structures with the same rmap owner to collect the extents that
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700384 * cannot be removed (sublist). We then subtract sublist from bitmap to
Darrick J. Wong64a39d82018-05-29 22:18:09 -0700385 * derive the blocks that were used by the old btree. These blocks can be
386 * reaped.
387 *
388 * For rmapbt reconstructions we must use different tactics for extent
389 * collection. First we iterate all primary metadata (this excludes the old
390 * rmapbt, obviously) to generate new rmap records. The gaps in the rmap
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700391 * records are collected as bitmap. The bnobt records are collected as
392 * sublist. As with the other btrees we subtract sublist from bitmap, and the
Darrick J. Wong64a39d82018-05-29 22:18:09 -0700393 * result (since the rmapbt lives in the free space) are the blocks from the
394 * old rmapbt.
Darrick J. Wong64a39d82018-05-29 22:18:09 -0700395 *
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700396 * Disposal of Blocks from Old per-AG Btrees
397 *
398 * Now that we've constructed a new btree to replace the damaged one, we want
399 * to dispose of the blocks that (we think) the old btree was using.
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700400 * Previously, we used the rmapbt to collect the extents (bitmap) with the
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700401 * rmap owner corresponding to the tree we rebuilt, collected extents for any
402 * blocks with the same rmap owner that are owned by another data structure
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700403 * (sublist), and subtracted sublist from bitmap. In theory the extents
404 * remaining in bitmap are the old btree's blocks.
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700405 *
406 * Unfortunately, it's possible that the btree was crosslinked with other
407 * blocks on disk. The rmap data can tell us if there are multiple owners, so
408 * if the rmapbt says there is an owner of this block other than @oinfo, then
409 * the block is crosslinked. Remove the reverse mapping and continue.
410 *
411 * If there is one rmap record, we can free the block, which removes the
412 * reverse mapping but doesn't add the block to the free space. Our repair
413 * strategy is to hope the other metadata objects crosslinked on this block
414 * will be rebuilt (atop different blocks), thereby removing all the cross
415 * links.
416 *
417 * If there are no rmap records at all, we also free the block. If the btree
418 * being rebuilt lives in the free space (bnobt/cntbt/rmapbt) then there isn't
419 * supposed to be a rmap record and everything is ok. For other btrees there
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700420 * had to have been an rmap entry for the block to have ended up on @bitmap,
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700421 * so if it's gone now there's something wrong and the fs will shut down.
422 *
423 * Note: If there are multiple rmap records with only the same rmap owner as
424 * the btree we're trying to rebuild and the block is indeed owned by another
425 * data structure with the same rmap owner, then the block will be in sublist
426 * and therefore doesn't need disposal. If there are multiple rmap records
427 * with only the same rmap owner but the block is not owned by something with
428 * the same rmap owner, the block will be freed.
429 *
430 * The caller is responsible for locking the AG headers for the entire rebuild
431 * operation so that nothing else can sneak in and change the AG state while
432 * we're not looking. We also assume that the caller already invalidated any
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700433 * buffers associated with @bitmap.
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700434 */
435
436/*
437 * Invalidate buffers for per-AG btree blocks we're dumping. This function
438 * is not intended for use with file data repairs; we have bunmapi for that.
439 */
440int
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700441xrep_invalidate_blocks(
Darrick J. Wong1d8a7482018-07-19 12:29:12 -0700442 struct xfs_scrub *sc,
Darrick J. Wong00b10d42020-03-16 17:13:05 -0700443 struct xbitmap *bitmap)
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700444{
Darrick J. Wong00b10d42020-03-16 17:13:05 -0700445 struct xbitmap_range *bmr;
446 struct xbitmap_range *n;
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700447 struct xfs_buf *bp;
448 xfs_fsblock_t fsbno;
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700449
450 /*
451 * For each block in each extent, see if there's an incore buffer for
452 * exactly that block; if so, invalidate it. The buffer cache only
453 * lets us look for one buffer at a time, so we have to look one block
454 * at a time. Avoid invalidating AG headers and post-EOFS blocks
455 * because we never own those; and if we can't TRYLOCK the buffer we
456 * assume it's owned by someone else.
457 */
Darrick J. Wong00b10d42020-03-16 17:13:05 -0700458 for_each_xbitmap_block(fsbno, bmr, n, bitmap) {
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700459 /* Skip AG headers and post-EOFS blocks */
460 if (!xfs_verify_fsbno(sc->mp, fsbno))
461 continue;
462 bp = xfs_buf_incore(sc->mp->m_ddev_targp,
463 XFS_FSB_TO_DADDR(sc->mp, fsbno),
464 XFS_FSB_TO_BB(sc->mp, 1), XBF_TRYLOCK);
465 if (bp) {
466 xfs_trans_bjoin(sc->tp, bp);
467 xfs_trans_binval(sc->tp, bp);
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700468 }
469 }
470
471 return 0;
472}
473
474/* Ensure the freelist is the correct size. */
475int
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700476xrep_fix_freelist(
Darrick J. Wong1d8a7482018-07-19 12:29:12 -0700477 struct xfs_scrub *sc,
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700478 bool can_shrink)
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700479{
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700480 struct xfs_alloc_arg args = {0};
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700481
482 args.mp = sc->mp;
483 args.tp = sc->tp;
Darrick J. Wong54406762021-08-09 16:31:14 -0700484 args.agno = sc->sa.pag->pag_agno;
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700485 args.alignment = 1;
486 args.pag = sc->sa.pag;
487
488 return xfs_alloc_fix_freelist(&args,
489 can_shrink ? 0 : XFS_ALLOC_FLAG_NOSHRINK);
490}
491
492/*
493 * Put a block back on the AGFL.
494 */
495STATIC int
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700496xrep_put_freelist(
Darrick J. Wong1d8a7482018-07-19 12:29:12 -0700497 struct xfs_scrub *sc,
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700498 xfs_agblock_t agbno)
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700499{
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700500 int error;
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700501
502 /* Make sure there's space on the freelist. */
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700503 error = xrep_fix_freelist(sc, true);
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700504 if (error)
505 return error;
506
507 /*
508 * Since we're "freeing" a lost block onto the AGFL, we have to
509 * create an rmap for the block prior to merging it or else other
510 * parts will break.
511 */
Dave Chinnerfa9c3c12021-06-02 10:48:24 +1000512 error = xfs_rmap_alloc(sc->tp, sc->sa.agf_bp, sc->sa.pag, agbno, 1,
Darrick J. Wong7280fed2018-12-12 08:46:23 -0800513 &XFS_RMAP_OINFO_AG);
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700514 if (error)
515 return error;
516
517 /* Put the block on the AGFL. */
518 error = xfs_alloc_put_freelist(sc->tp, sc->sa.agf_bp, sc->sa.agfl_bp,
519 agbno, 0);
520 if (error)
521 return error;
Dave Chinner45d06622021-06-02 10:48:24 +1000522 xfs_extent_busy_insert(sc->tp, sc->sa.pag, agbno, 1,
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700523 XFS_EXTENT_BUSY_SKIP_DISCARD);
524
525 return 0;
526}
527
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700528/* Dispose of a single block. */
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700529STATIC int
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700530xrep_reap_block(
Darrick J. Wong66e32372018-12-12 08:46:23 -0800531 struct xfs_scrub *sc,
532 xfs_fsblock_t fsbno,
533 const struct xfs_owner_info *oinfo,
534 enum xfs_ag_resv_type resv)
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700535{
Darrick J. Wong66e32372018-12-12 08:46:23 -0800536 struct xfs_btree_cur *cur;
537 struct xfs_buf *agf_bp = NULL;
538 xfs_agnumber_t agno;
539 xfs_agblock_t agbno;
540 bool has_other_rmap;
541 int error;
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700542
543 agno = XFS_FSB_TO_AGNO(sc->mp, fsbno);
544 agbno = XFS_FSB_TO_AGBNO(sc->mp, fsbno);
545
546 /*
547 * If we are repairing per-inode metadata, we need to read in the AGF
548 * buffer. Otherwise, we're repairing a per-AG structure, so reuse
549 * the AGF buffer that the setup functions already grabbed.
550 */
551 if (sc->ip) {
552 error = xfs_alloc_read_agf(sc->mp, sc->tp, agno, 0, &agf_bp);
553 if (error)
554 return error;
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700555 } else {
556 agf_bp = sc->sa.agf_bp;
557 }
Dave Chinnerfa9c3c12021-06-02 10:48:24 +1000558 cur = xfs_rmapbt_init_cursor(sc->mp, sc->tp, agf_bp, sc->sa.pag);
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700559
560 /* Can we find any other rmappings? */
561 error = xfs_rmap_has_other_keys(cur, agbno, 1, oinfo, &has_other_rmap);
Darrick J. Wongef97ef22018-07-19 12:29:10 -0700562 xfs_btree_del_cursor(cur, error);
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700563 if (error)
Darrick J. Wongef97ef22018-07-19 12:29:10 -0700564 goto out_free;
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700565
566 /*
567 * If there are other rmappings, this block is cross linked and must
568 * not be freed. Remove the reverse mapping and move on. Otherwise,
569 * we were the only owner of the block, so free the extent, which will
570 * also remove the rmap.
571 *
572 * XXX: XFS doesn't support detecting the case where a single block
573 * metadata structure is crosslinked with a multi-block structure
574 * because the buffer cache doesn't detect aliasing problems, so we
575 * can't fix 100% of crosslinking problems (yet). The verifiers will
576 * blow on writeout, the filesystem will shut down, and the admin gets
577 * to run xfs_repair.
578 */
579 if (has_other_rmap)
Dave Chinnerfa9c3c12021-06-02 10:48:24 +1000580 error = xfs_rmap_free(sc->tp, agf_bp, sc->sa.pag, agbno,
581 1, oinfo);
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700582 else if (resv == XFS_AG_RESV_AGFL)
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700583 error = xrep_put_freelist(sc, agbno);
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700584 else
585 error = xfs_free_extent(sc->tp, fsbno, 1, oinfo, resv);
586 if (agf_bp != sc->sa.agf_bp)
587 xfs_trans_brelse(sc->tp, agf_bp);
588 if (error)
589 return error;
590
591 if (sc->ip)
592 return xfs_trans_roll_inode(&sc->tp, sc->ip);
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700593 return xrep_roll_ag_trans(sc);
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700594
Darrick J. Wongef97ef22018-07-19 12:29:10 -0700595out_free:
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700596 if (agf_bp != sc->sa.agf_bp)
597 xfs_trans_brelse(sc->tp, agf_bp);
598 return error;
599}
600
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700601/* Dispose of every block of every extent in the bitmap. */
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700602int
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700603xrep_reap_extents(
Darrick J. Wong66e32372018-12-12 08:46:23 -0800604 struct xfs_scrub *sc,
Darrick J. Wong00b10d42020-03-16 17:13:05 -0700605 struct xbitmap *bitmap,
Darrick J. Wong66e32372018-12-12 08:46:23 -0800606 const struct xfs_owner_info *oinfo,
607 enum xfs_ag_resv_type type)
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700608{
Darrick J. Wong00b10d42020-03-16 17:13:05 -0700609 struct xbitmap_range *bmr;
610 struct xbitmap_range *n;
Darrick J. Wong66e32372018-12-12 08:46:23 -0800611 xfs_fsblock_t fsbno;
612 int error = 0;
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700613
Dave Chinner38c26bf2021-08-18 18:46:37 -0700614 ASSERT(xfs_has_rmapbt(sc->mp));
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700615
Darrick J. Wong00b10d42020-03-16 17:13:05 -0700616 for_each_xbitmap_block(fsbno, bmr, n, bitmap) {
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700617 ASSERT(sc->ip != NULL ||
Darrick J. Wong54406762021-08-09 16:31:14 -0700618 XFS_FSB_TO_AGNO(sc->mp, fsbno) == sc->sa.pag->pag_agno);
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700619 trace_xrep_dispose_btree_extent(sc->mp,
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700620 XFS_FSB_TO_AGNO(sc->mp, fsbno),
621 XFS_FSB_TO_AGBNO(sc->mp, fsbno), 1);
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700622
Darrick J. Wong86d969b2018-07-30 11:18:13 -0700623 error = xrep_reap_block(sc, fsbno, oinfo, type);
624 if (error)
Darrick J. Wong37a65472020-03-16 17:12:34 -0700625 break;
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700626 }
627
Darrick J. Wong12c6510e2018-05-29 22:18:10 -0700628 return error;
629}
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700630
631/*
632 * Finding per-AG Btree Roots for AGF/AGI Reconstruction
633 *
634 * If the AGF or AGI become slightly corrupted, it may be necessary to rebuild
635 * the AG headers by using the rmap data to rummage through the AG looking for
636 * btree roots. This is not guaranteed to work if the AG is heavily damaged
637 * or the rmap data are corrupt.
638 *
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700639 * Callers of xrep_find_ag_btree_roots must lock the AGF and AGFL
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700640 * buffers if the AGF is being rebuilt; or the AGF and AGI buffers if the
641 * AGI is being rebuilt. It must maintain these locks until it's safe for
642 * other threads to change the btrees' shapes. The caller provides
643 * information about the btrees to look for by passing in an array of
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700644 * xrep_find_ag_btree with the (rmap owner, buf_ops, magic) fields set.
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700645 * The (root, height) fields will be set on return if anything is found. The
646 * last element of the array should have a NULL buf_ops to mark the end of the
647 * array.
648 *
649 * For every rmapbt record matching any of the rmap owners in btree_info,
650 * read each block referenced by the rmap record. If the block is a btree
651 * block from this filesystem matching any of the magic numbers and has a
652 * level higher than what we've already seen, remember the block and the
653 * height of the tree required to have such a block. When the call completes,
654 * we return the highest block we've found for each btree description; those
655 * should be the roots.
656 */
657
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700658struct xrep_findroot {
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700659 struct xfs_scrub *sc;
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700660 struct xfs_buf *agfl_bp;
661 struct xfs_agf *agf;
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700662 struct xrep_find_ag_btree *btree_info;
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700663};
664
665/* See if our block is in the AGFL. */
666STATIC int
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700667xrep_findroot_agfl_walk(
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700668 struct xfs_mount *mp,
669 xfs_agblock_t bno,
670 void *priv)
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700671{
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700672 xfs_agblock_t *agbno = priv;
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700673
Darrick J. Wonge7ee96d2019-08-28 14:37:57 -0700674 return (*agbno == bno) ? -ECANCELED : 0;
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700675}
676
677/* Does this block match the btree information passed in? */
678STATIC int
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700679xrep_findroot_block(
680 struct xrep_findroot *ri,
681 struct xrep_find_ag_btree *fab,
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700682 uint64_t owner,
683 xfs_agblock_t agbno,
Darrick J. Wong1002ff42018-10-18 17:20:26 +1100684 bool *done_with_block)
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700685{
686 struct xfs_mount *mp = ri->sc->mp;
687 struct xfs_buf *bp;
688 struct xfs_btree_block *btblock;
689 xfs_daddr_t daddr;
Darrick J. Wong1002ff42018-10-18 17:20:26 +1100690 int block_level;
Darrick J. Wong38b62382018-10-18 17:20:35 +1100691 int error = 0;
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700692
Darrick J. Wong54406762021-08-09 16:31:14 -0700693 daddr = XFS_AGB_TO_DADDR(mp, ri->sc->sa.pag->pag_agno, agbno);
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700694
695 /*
696 * Blocks in the AGFL have stale contents that might just happen to
697 * have a matching magic and uuid. We don't want to pull these blocks
698 * in as part of a tree root, so we have to filter out the AGFL stuff
699 * here. If the AGFL looks insane we'll just refuse to repair.
700 */
701 if (owner == XFS_RMAP_OWN_AG) {
702 error = xfs_agfl_walk(mp, ri->agf, ri->agfl_bp,
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700703 xrep_findroot_agfl_walk, &agbno);
Darrick J. Wonge7ee96d2019-08-28 14:37:57 -0700704 if (error == -ECANCELED)
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700705 return 0;
706 if (error)
707 return error;
708 }
709
Darrick J. Wong38b62382018-10-18 17:20:35 +1100710 /*
711 * Read the buffer into memory so that we can see if it's a match for
712 * our btree type. We have no clue if it is beforehand, and we want to
713 * avoid xfs_trans_read_buf's behavior of dumping the DONE state (which
714 * will cause needless disk reads in subsequent calls to this function)
715 * and logging metadata verifier failures.
716 *
717 * Therefore, pass in NULL buffer ops. If the buffer was already in
718 * memory from some other caller it will already have b_ops assigned.
719 * If it was in memory from a previous unsuccessful findroot_block
720 * call, the buffer won't have b_ops but it should be clean and ready
721 * for us to try to verify if the read call succeeds. The same applies
722 * if the buffer wasn't in memory at all.
723 *
724 * Note: If we never match a btree type with this buffer, it will be
725 * left in memory with NULL b_ops. This shouldn't be a problem unless
726 * the buffer gets written.
727 */
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700728 error = xfs_trans_read_buf(mp, ri->sc->tp, mp->m_ddev_targp, daddr,
729 mp->m_bsize, 0, &bp, NULL);
730 if (error)
731 return error;
732
Darrick J. Wong38b62382018-10-18 17:20:35 +1100733 /* Ensure the block magic matches the btree type we're looking for. */
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700734 btblock = XFS_BUF_TO_BLOCK(bp);
Darrick J. Wong9228d752019-02-06 10:20:54 -0800735 ASSERT(fab->buf_ops->magic[1] != 0);
736 if (btblock->bb_magic != fab->buf_ops->magic[1])
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700737 goto out;
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700738
Darrick J. Wong38b62382018-10-18 17:20:35 +1100739 /*
740 * If the buffer already has ops applied and they're not the ones for
741 * this btree type, we know this block doesn't match the btree and we
742 * can bail out.
743 *
744 * If the buffer ops match ours, someone else has already validated
745 * the block for us, so we can move on to checking if this is a root
746 * block candidate.
747 *
748 * If the buffer does not have ops, nobody has successfully validated
749 * the contents and the buffer cannot be dirty. If the magic, uuid,
750 * and structure match this btree type then we'll move on to checking
751 * if it's a root block candidate. If there is no match, bail out.
752 */
753 if (bp->b_ops) {
754 if (bp->b_ops != fab->buf_ops)
755 goto out;
756 } else {
757 ASSERT(!xfs_trans_buf_is_dirty(bp));
758 if (!uuid_equal(&btblock->bb_u.s.bb_uuid,
759 &mp->m_sb.sb_meta_uuid))
760 goto out;
Darrick J. Wongadd46b32019-02-03 14:03:59 -0800761 /*
762 * Read verifiers can reference b_ops, so we set the pointer
763 * here. If the verifier fails we'll reset the buffer state
764 * to what it was before we touched the buffer.
765 */
766 bp->b_ops = fab->buf_ops;
Darrick J. Wong38b62382018-10-18 17:20:35 +1100767 fab->buf_ops->verify_read(bp);
768 if (bp->b_error) {
Darrick J. Wongadd46b32019-02-03 14:03:59 -0800769 bp->b_ops = NULL;
Darrick J. Wong38b62382018-10-18 17:20:35 +1100770 bp->b_error = 0;
771 goto out;
772 }
773
774 /*
775 * Some read verifiers will (re)set b_ops, so we must be
Darrick J. Wongadd46b32019-02-03 14:03:59 -0800776 * careful not to change b_ops after running the verifier.
Darrick J. Wong38b62382018-10-18 17:20:35 +1100777 */
Darrick J. Wong38b62382018-10-18 17:20:35 +1100778 }
Darrick J. Wong1002ff42018-10-18 17:20:26 +1100779
780 /*
781 * This block passes the magic/uuid and verifier tests for this btree
782 * type. We don't need the caller to try the other tree types.
783 */
784 *done_with_block = true;
785
786 /*
787 * Compare this btree block's level to the height of the current
788 * candidate root block.
789 *
790 * If the level matches the root we found previously, throw away both
791 * blocks because there can't be two candidate roots.
792 *
793 * If level is lower in the tree than the root we found previously,
794 * ignore this block.
795 */
796 block_level = xfs_btree_get_level(btblock);
797 if (block_level + 1 == fab->height) {
798 fab->root = NULLAGBLOCK;
799 goto out;
800 } else if (block_level < fab->height) {
801 goto out;
802 }
803
804 /*
805 * This is the highest block in the tree that we've found so far.
806 * Update the btree height to reflect what we've learned from this
807 * block.
808 */
809 fab->height = block_level + 1;
810
811 /*
812 * If this block doesn't have sibling pointers, then it's the new root
813 * block candidate. Otherwise, the root will be found farther up the
814 * tree.
815 */
816 if (btblock->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK) &&
817 btblock->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK))
818 fab->root = agbno;
819 else
820 fab->root = NULLAGBLOCK;
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700821
Darrick J. Wong54406762021-08-09 16:31:14 -0700822 trace_xrep_findroot_block(mp, ri->sc->sa.pag->pag_agno, agbno,
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700823 be32_to_cpu(btblock->bb_magic), fab->height - 1);
824out:
825 xfs_trans_brelse(ri->sc->tp, bp);
826 return error;
827}
828
829/*
830 * Do any of the blocks in this rmap record match one of the btrees we're
831 * looking for?
832 */
833STATIC int
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700834xrep_findroot_rmap(
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700835 struct xfs_btree_cur *cur,
Darrick J. Wong159eb692021-08-10 17:02:16 -0700836 const struct xfs_rmap_irec *rec,
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700837 void *priv)
838{
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700839 struct xrep_findroot *ri = priv;
840 struct xrep_find_ag_btree *fab;
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700841 xfs_agblock_t b;
Darrick J. Wong1002ff42018-10-18 17:20:26 +1100842 bool done;
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700843 int error = 0;
844
845 /* Ignore anything that isn't AG metadata. */
846 if (!XFS_RMAP_NON_INODE_OWNER(rec->rm_owner))
847 return 0;
848
849 /* Otherwise scan each block + btree type. */
850 for (b = 0; b < rec->rm_blockcount; b++) {
Darrick J. Wong1002ff42018-10-18 17:20:26 +1100851 done = false;
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700852 for (fab = ri->btree_info; fab->buf_ops; fab++) {
853 if (rec->rm_owner != fab->rmap_owner)
854 continue;
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700855 error = xrep_findroot_block(ri, fab,
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700856 rec->rm_owner, rec->rm_startblock + b,
Darrick J. Wong1002ff42018-10-18 17:20:26 +1100857 &done);
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700858 if (error)
859 return error;
Darrick J. Wong1002ff42018-10-18 17:20:26 +1100860 if (done)
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700861 break;
862 }
863 }
864
865 return 0;
866}
867
868/* Find the roots of the per-AG btrees described in btree_info. */
869int
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700870xrep_find_ag_btree_roots(
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700871 struct xfs_scrub *sc,
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700872 struct xfs_buf *agf_bp,
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700873 struct xrep_find_ag_btree *btree_info,
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700874 struct xfs_buf *agfl_bp)
875{
876 struct xfs_mount *mp = sc->mp;
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700877 struct xrep_findroot ri;
878 struct xrep_find_ag_btree *fab;
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700879 struct xfs_btree_cur *cur;
880 int error;
881
882 ASSERT(xfs_buf_islocked(agf_bp));
883 ASSERT(agfl_bp == NULL || xfs_buf_islocked(agfl_bp));
884
885 ri.sc = sc;
886 ri.btree_info = btree_info;
Christoph Hellwig9798f612020-03-10 08:57:29 -0700887 ri.agf = agf_bp->b_addr;
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700888 ri.agfl_bp = agfl_bp;
889 for (fab = btree_info; fab->buf_ops; fab++) {
890 ASSERT(agfl_bp || fab->rmap_owner != XFS_RMAP_OWN_AG);
891 ASSERT(XFS_RMAP_NON_INODE_OWNER(fab->rmap_owner));
892 fab->root = NULLAGBLOCK;
893 fab->height = 0;
894 }
895
Dave Chinnerfa9c3c12021-06-02 10:48:24 +1000896 cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.pag);
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700897 error = xfs_rmap_query_all(cur, xrep_findroot_rmap, &ri);
Darrick J. Wong0b04b6b82018-07-19 12:26:31 -0700898 xfs_btree_del_cursor(cur, error);
Darrick J. Wong04a2b7b2018-05-29 22:18:10 -0700899
900 return error;
901}
Darrick J. Wong7e85bc62018-05-29 22:18:11 -0700902
903/* Force a quotacheck the next time we mount. */
904void
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700905xrep_force_quotacheck(
Darrick J. Wong1d8a7482018-07-19 12:29:12 -0700906 struct xfs_scrub *sc,
Darrick J. Wong1a7ed272020-07-15 17:53:43 -0700907 xfs_dqtype_t type)
Darrick J. Wong7e85bc62018-05-29 22:18:11 -0700908{
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700909 uint flag;
Darrick J. Wong7e85bc62018-05-29 22:18:11 -0700910
Darrick J. Wong1a7ed272020-07-15 17:53:43 -0700911 flag = xfs_quota_chkd_flag(type);
Darrick J. Wong7e85bc62018-05-29 22:18:11 -0700912 if (!(flag & sc->mp->m_qflags))
913 return;
914
915 sc->mp->m_qflags &= ~flag;
916 spin_lock(&sc->mp->m_sb_lock);
917 sc->mp->m_sb.sb_qflags &= ~flag;
918 spin_unlock(&sc->mp->m_sb_lock);
919 xfs_log_sb(sc->tp);
920}
921
922/*
923 * Attach dquots to this inode, or schedule quotacheck to fix them.
924 *
925 * This function ensures that the appropriate dquots are attached to an inode.
926 * We cannot allow the dquot code to allocate an on-disk dquot block here
927 * because we're already in transaction context with the inode locked. The
928 * on-disk dquot should already exist anyway. If the quota code signals
929 * corruption or missing quota information, schedule quotacheck, which will
930 * repair corruptions in the quota metadata.
931 */
932int
Darrick J. Wongb5e21962018-07-19 12:29:11 -0700933xrep_ino_dqattach(
Darrick J. Wong1d8a7482018-07-19 12:29:12 -0700934 struct xfs_scrub *sc)
Darrick J. Wong7e85bc62018-05-29 22:18:11 -0700935{
Darrick J. Wong032d91f2018-07-19 12:29:12 -0700936 int error;
Darrick J. Wong7e85bc62018-05-29 22:18:11 -0700937
938 error = xfs_qm_dqattach_locked(sc->ip, false);
939 switch (error) {
940 case -EFSBADCRC:
941 case -EFSCORRUPTED:
942 case -ENOENT:
943 xfs_err_ratelimited(sc->mp,
944"inode %llu repair encountered quota error %d, quotacheck forced.",
945 (unsigned long long)sc->ip->i_ino, error);
946 if (XFS_IS_UQUOTA_ON(sc->mp) && !sc->ip->i_udquot)
Darrick J. Wong8cd49012020-07-15 17:42:36 -0700947 xrep_force_quotacheck(sc, XFS_DQTYPE_USER);
Darrick J. Wong7e85bc62018-05-29 22:18:11 -0700948 if (XFS_IS_GQUOTA_ON(sc->mp) && !sc->ip->i_gdquot)
Darrick J. Wong8cd49012020-07-15 17:42:36 -0700949 xrep_force_quotacheck(sc, XFS_DQTYPE_GROUP);
Darrick J. Wong7e85bc62018-05-29 22:18:11 -0700950 if (XFS_IS_PQUOTA_ON(sc->mp) && !sc->ip->i_pdquot)
Darrick J. Wong8cd49012020-07-15 17:42:36 -0700951 xrep_force_quotacheck(sc, XFS_DQTYPE_PROJ);
Gustavo A. R. Silva53004ee2021-04-20 17:54:36 -0500952 fallthrough;
Darrick J. Wong7e85bc62018-05-29 22:18:11 -0700953 case -ESRCH:
954 error = 0;
955 break;
956 default:
957 break;
958 }
959
960 return error;
961}