xref: /openbmc/linux/fs/xfs/scrub/repair.c (revision 81fbc5f9308033ea9a2b22d80ad6431a1ef224ff)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2018-2023 Oracle.  All Rights Reserved.
4  * Author: Darrick J. Wong <djwong@kernel.org>
5  */
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"
12 #include "xfs_btree.h"
13 #include "xfs_log_format.h"
14 #include "xfs_trans.h"
15 #include "xfs_sb.h"
16 #include "xfs_inode.h"
17 #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"
23 #include "xfs_refcount_btree.h"
24 #include "xfs_extent_busy.h"
25 #include "xfs_ag.h"
26 #include "xfs_ag_resv.h"
27 #include "xfs_quota.h"
28 #include "xfs_qm.h"
29 #include "xfs_defer.h"
30 #include "scrub/scrub.h"
31 #include "scrub/common.h"
32 #include "scrub/trace.h"
33 #include "scrub/repair.h"
34 #include "scrub/bitmap.h"
35 
36 /*
37  * Attempt to repair some metadata, if the metadata is corrupt and userspace
38  * told us to fix it.  This function returns -EAGAIN to mean "re-run scrub",
39  * and will set *fixed to true if it thinks it repaired anything.
40  */
41 int
42 xrep_attempt(
43 	struct xfs_scrub	*sc)
44 {
45 	int			error = 0;
46 
47 	trace_xrep_attempt(XFS_I(file_inode(sc->file)), sc->sm, error);
48 
49 	xchk_ag_btcur_free(&sc->sa);
50 
51 	/* Repair whatever's broken. */
52 	ASSERT(sc->ops->repair);
53 	error = sc->ops->repair(sc);
54 	trace_xrep_done(XFS_I(file_inode(sc->file)), sc->sm, error);
55 	switch (error) {
56 	case 0:
57 		/*
58 		 * Repair succeeded.  Commit the fixes and perform a second
59 		 * scrub so that we can tell userspace if we fixed the problem.
60 		 */
61 		sc->sm->sm_flags &= ~XFS_SCRUB_FLAGS_OUT;
62 		sc->flags |= XREP_ALREADY_FIXED;
63 		return -EAGAIN;
64 	case -ECHRNG:
65 		sc->flags |= XCHK_NEED_DRAIN;
66 		return -EAGAIN;
67 	case -EDEADLOCK:
68 		/* Tell the caller to try again having grabbed all the locks. */
69 		if (!(sc->flags & XCHK_TRY_HARDER)) {
70 			sc->flags |= XCHK_TRY_HARDER;
71 			return -EAGAIN;
72 		}
73 		/*
74 		 * We tried harder but still couldn't grab all the resources
75 		 * we needed to fix it.  The corruption has not been fixed,
76 		 * so exit to userspace with the scan's output flags unchanged.
77 		 */
78 		return 0;
79 	default:
80 		/*
81 		 * EAGAIN tells the caller to re-scrub, so we cannot return
82 		 * that here.
83 		 */
84 		ASSERT(error != -EAGAIN);
85 		return error;
86 	}
87 }
88 
89 /*
90  * Complain about unfixable problems in the filesystem.  We don't log
91  * corruptions when IFLAG_REPAIR wasn't set on the assumption that the driver
92  * program is xfs_scrub, which will call back with IFLAG_REPAIR set if the
93  * administrator isn't running xfs_scrub in no-repairs mode.
94  *
95  * Use this helper function because _ratelimited silently declares a static
96  * structure to track rate limiting information.
97  */
98 void
99 xrep_failure(
100 	struct xfs_mount	*mp)
101 {
102 	xfs_alert_ratelimited(mp,
103 "Corruption not fixed during online repair.  Unmount and run xfs_repair.");
104 }
105 
106 /*
107  * Repair probe -- userspace uses this to probe if we're willing to repair a
108  * given mountpoint.
109  */
110 int
111 xrep_probe(
112 	struct xfs_scrub	*sc)
113 {
114 	int			error = 0;
115 
116 	if (xchk_should_terminate(sc, &error))
117 		return error;
118 
119 	return 0;
120 }
121 
122 /*
123  * Roll a transaction, keeping the AG headers locked and reinitializing
124  * the btree cursors.
125  */
126 int
127 xrep_roll_ag_trans(
128 	struct xfs_scrub	*sc)
129 {
130 	int			error;
131 
132 	/*
133 	 * Keep the AG header buffers locked while we roll the transaction.
134 	 * Ensure that both AG buffers are dirty and held when we roll the
135 	 * transaction so that they move forward in the log without losing the
136 	 * bli (and hence the bli type) when the transaction commits.
137 	 *
138 	 * Normal code would never hold clean buffers across a roll, but repair
139 	 * needs both buffers to maintain a total lock on the AG.
140 	 */
141 	if (sc->sa.agi_bp) {
142 		xfs_ialloc_log_agi(sc->tp, sc->sa.agi_bp, XFS_AGI_MAGICNUM);
143 		xfs_trans_bhold(sc->tp, sc->sa.agi_bp);
144 	}
145 
146 	if (sc->sa.agf_bp) {
147 		xfs_alloc_log_agf(sc->tp, sc->sa.agf_bp, XFS_AGF_MAGICNUM);
148 		xfs_trans_bhold(sc->tp, sc->sa.agf_bp);
149 	}
150 
151 	/*
152 	 * Roll the transaction.  We still hold the AG header buffers locked
153 	 * regardless of whether or not that succeeds.  On failure, the buffers
154 	 * will be released during teardown on our way out of the kernel.  If
155 	 * successful, join the buffers to the new transaction and move on.
156 	 */
157 	error = xfs_trans_roll(&sc->tp);
158 	if (error)
159 		return error;
160 
161 	/* Join the AG headers to the new transaction. */
162 	if (sc->sa.agi_bp)
163 		xfs_trans_bjoin(sc->tp, sc->sa.agi_bp);
164 	if (sc->sa.agf_bp)
165 		xfs_trans_bjoin(sc->tp, sc->sa.agf_bp);
166 
167 	return 0;
168 }
169 
170 /* Finish all deferred work attached to the repair transaction. */
171 int
172 xrep_defer_finish(
173 	struct xfs_scrub	*sc)
174 {
175 	int			error;
176 
177 	/*
178 	 * Keep the AG header buffers locked while we complete deferred work
179 	 * items.  Ensure that both AG buffers are dirty and held when we roll
180 	 * the transaction so that they move forward in the log without losing
181 	 * the bli (and hence the bli type) when the transaction commits.
182 	 *
183 	 * Normal code would never hold clean buffers across a roll, but repair
184 	 * needs both buffers to maintain a total lock on the AG.
185 	 */
186 	if (sc->sa.agi_bp) {
187 		xfs_ialloc_log_agi(sc->tp, sc->sa.agi_bp, XFS_AGI_MAGICNUM);
188 		xfs_trans_bhold(sc->tp, sc->sa.agi_bp);
189 	}
190 
191 	if (sc->sa.agf_bp) {
192 		xfs_alloc_log_agf(sc->tp, sc->sa.agf_bp, XFS_AGF_MAGICNUM);
193 		xfs_trans_bhold(sc->tp, sc->sa.agf_bp);
194 	}
195 
196 	/*
197 	 * Finish all deferred work items.  We still hold the AG header buffers
198 	 * locked regardless of whether or not that succeeds.  On failure, the
199 	 * buffers will be released during teardown on our way out of the
200 	 * kernel.  If successful, join the buffers to the new transaction
201 	 * and move on.
202 	 */
203 	error = xfs_defer_finish(&sc->tp);
204 	if (error)
205 		return error;
206 
207 	/*
208 	 * Release the hold that we set above because defer_finish won't do
209 	 * that for us.  The defer roll code redirties held buffers after each
210 	 * roll, so the AG header buffers should be ready for logging.
211 	 */
212 	if (sc->sa.agi_bp)
213 		xfs_trans_bhold_release(sc->tp, sc->sa.agi_bp);
214 	if (sc->sa.agf_bp)
215 		xfs_trans_bhold_release(sc->tp, sc->sa.agf_bp);
216 
217 	return 0;
218 }
219 
220 /*
221  * Does the given AG have enough space to rebuild a btree?  Neither AG
222  * reservation can be critical, and we must have enough space (factoring
223  * in AG reservations) to construct a whole btree.
224  */
225 bool
226 xrep_ag_has_space(
227 	struct xfs_perag	*pag,
228 	xfs_extlen_t		nr_blocks,
229 	enum xfs_ag_resv_type	type)
230 {
231 	return  !xfs_ag_resv_critical(pag, XFS_AG_RESV_RMAPBT) &&
232 		!xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA) &&
233 		pag->pagf_freeblks > xfs_ag_resv_needed(pag, type) + nr_blocks;
234 }
235 
236 /*
237  * Figure out how many blocks to reserve for an AG repair.  We calculate the
238  * worst case estimate for the number of blocks we'd need to rebuild one of
239  * any type of per-AG btree.
240  */
241 xfs_extlen_t
242 xrep_calc_ag_resblks(
243 	struct xfs_scrub		*sc)
244 {
245 	struct xfs_mount		*mp = sc->mp;
246 	struct xfs_scrub_metadata	*sm = sc->sm;
247 	struct xfs_perag		*pag;
248 	struct xfs_buf			*bp;
249 	xfs_agino_t			icount = NULLAGINO;
250 	xfs_extlen_t			aglen = NULLAGBLOCK;
251 	xfs_extlen_t			usedlen;
252 	xfs_extlen_t			freelen;
253 	xfs_extlen_t			bnobt_sz;
254 	xfs_extlen_t			inobt_sz;
255 	xfs_extlen_t			rmapbt_sz;
256 	xfs_extlen_t			refcbt_sz;
257 	int				error;
258 
259 	if (!(sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR))
260 		return 0;
261 
262 	pag = xfs_perag_get(mp, sm->sm_agno);
263 	if (xfs_perag_initialised_agi(pag)) {
264 		/* Use in-core icount if possible. */
265 		icount = pag->pagi_count;
266 	} else {
267 		/* Try to get the actual counters from disk. */
268 		error = xfs_ialloc_read_agi(pag, NULL, &bp);
269 		if (!error) {
270 			icount = pag->pagi_count;
271 			xfs_buf_relse(bp);
272 		}
273 	}
274 
275 	/* Now grab the block counters from the AGF. */
276 	error = xfs_alloc_read_agf(pag, NULL, 0, &bp);
277 	if (error) {
278 		aglen = pag->block_count;
279 		freelen = aglen;
280 		usedlen = aglen;
281 	} else {
282 		struct xfs_agf	*agf = bp->b_addr;
283 
284 		aglen = be32_to_cpu(agf->agf_length);
285 		freelen = be32_to_cpu(agf->agf_freeblks);
286 		usedlen = aglen - freelen;
287 		xfs_buf_relse(bp);
288 	}
289 
290 	/* If the icount is impossible, make some worst-case assumptions. */
291 	if (icount == NULLAGINO ||
292 	    !xfs_verify_agino(pag, icount)) {
293 		icount = pag->agino_max - pag->agino_min + 1;
294 	}
295 
296 	/* If the block counts are impossible, make worst-case assumptions. */
297 	if (aglen == NULLAGBLOCK ||
298 	    aglen != pag->block_count ||
299 	    freelen >= aglen) {
300 		aglen = pag->block_count;
301 		freelen = aglen;
302 		usedlen = aglen;
303 	}
304 	xfs_perag_put(pag);
305 
306 	trace_xrep_calc_ag_resblks(mp, sm->sm_agno, icount, aglen,
307 			freelen, usedlen);
308 
309 	/*
310 	 * Figure out how many blocks we'd need worst case to rebuild
311 	 * each type of btree.  Note that we can only rebuild the
312 	 * bnobt/cntbt or inobt/finobt as pairs.
313 	 */
314 	bnobt_sz = 2 * xfs_allocbt_calc_size(mp, freelen);
315 	if (xfs_has_sparseinodes(mp))
316 		inobt_sz = xfs_iallocbt_calc_size(mp, icount /
317 				XFS_INODES_PER_HOLEMASK_BIT);
318 	else
319 		inobt_sz = xfs_iallocbt_calc_size(mp, icount /
320 				XFS_INODES_PER_CHUNK);
321 	if (xfs_has_finobt(mp))
322 		inobt_sz *= 2;
323 	if (xfs_has_reflink(mp))
324 		refcbt_sz = xfs_refcountbt_calc_size(mp, usedlen);
325 	else
326 		refcbt_sz = 0;
327 	if (xfs_has_rmapbt(mp)) {
328 		/*
329 		 * Guess how many blocks we need to rebuild the rmapbt.
330 		 * For non-reflink filesystems we can't have more records than
331 		 * used blocks.  However, with reflink it's possible to have
332 		 * more than one rmap record per AG block.  We don't know how
333 		 * many rmaps there could be in the AG, so we start off with
334 		 * what we hope is an generous over-estimation.
335 		 */
336 		if (xfs_has_reflink(mp))
337 			rmapbt_sz = xfs_rmapbt_calc_size(mp,
338 					(unsigned long long)aglen * 2);
339 		else
340 			rmapbt_sz = xfs_rmapbt_calc_size(mp, usedlen);
341 	} else {
342 		rmapbt_sz = 0;
343 	}
344 
345 	trace_xrep_calc_ag_resblks_btsize(mp, sm->sm_agno, bnobt_sz,
346 			inobt_sz, rmapbt_sz, refcbt_sz);
347 
348 	return max(max(bnobt_sz, inobt_sz), max(rmapbt_sz, refcbt_sz));
349 }
350 
351 /*
352  * Reconstructing per-AG Btrees
353  *
354  * When a space btree is corrupt, we don't bother trying to fix it.  Instead,
355  * we scan secondary space metadata to derive the records that should be in
356  * the damaged btree, initialize a fresh btree root, and insert the records.
357  * Note that for rebuilding the rmapbt we scan all the primary data to
358  * generate the new records.
359  *
360  * However, that leaves the matter of removing all the metadata describing the
361  * old broken structure.  For primary metadata we use the rmap data to collect
362  * every extent with a matching rmap owner (bitmap); we then iterate all other
363  * metadata structures with the same rmap owner to collect the extents that
364  * cannot be removed (sublist).  We then subtract sublist from bitmap to
365  * derive the blocks that were used by the old btree.  These blocks can be
366  * reaped.
367  *
368  * For rmapbt reconstructions we must use different tactics for extent
369  * collection.  First we iterate all primary metadata (this excludes the old
370  * rmapbt, obviously) to generate new rmap records.  The gaps in the rmap
371  * records are collected as bitmap.  The bnobt records are collected as
372  * sublist.  As with the other btrees we subtract sublist from bitmap, and the
373  * result (since the rmapbt lives in the free space) are the blocks from the
374  * old rmapbt.
375  */
376 
377 /* Ensure the freelist is the correct size. */
378 int
379 xrep_fix_freelist(
380 	struct xfs_scrub	*sc,
381 	bool			can_shrink)
382 {
383 	struct xfs_alloc_arg	args = {0};
384 
385 	args.mp = sc->mp;
386 	args.tp = sc->tp;
387 	args.agno = sc->sa.pag->pag_agno;
388 	args.alignment = 1;
389 	args.pag = sc->sa.pag;
390 
391 	return xfs_alloc_fix_freelist(&args,
392 			can_shrink ? 0 : XFS_ALLOC_FLAG_NOSHRINK);
393 }
394 
395 /*
396  * Finding per-AG Btree Roots for AGF/AGI Reconstruction
397  *
398  * If the AGF or AGI become slightly corrupted, it may be necessary to rebuild
399  * the AG headers by using the rmap data to rummage through the AG looking for
400  * btree roots.  This is not guaranteed to work if the AG is heavily damaged
401  * or the rmap data are corrupt.
402  *
403  * Callers of xrep_find_ag_btree_roots must lock the AGF and AGFL
404  * buffers if the AGF is being rebuilt; or the AGF and AGI buffers if the
405  * AGI is being rebuilt.  It must maintain these locks until it's safe for
406  * other threads to change the btrees' shapes.  The caller provides
407  * information about the btrees to look for by passing in an array of
408  * xrep_find_ag_btree with the (rmap owner, buf_ops, magic) fields set.
409  * The (root, height) fields will be set on return if anything is found.  The
410  * last element of the array should have a NULL buf_ops to mark the end of the
411  * array.
412  *
413  * For every rmapbt record matching any of the rmap owners in btree_info,
414  * read each block referenced by the rmap record.  If the block is a btree
415  * block from this filesystem matching any of the magic numbers and has a
416  * level higher than what we've already seen, remember the block and the
417  * height of the tree required to have such a block.  When the call completes,
418  * we return the highest block we've found for each btree description; those
419  * should be the roots.
420  */
421 
422 struct xrep_findroot {
423 	struct xfs_scrub		*sc;
424 	struct xfs_buf			*agfl_bp;
425 	struct xfs_agf			*agf;
426 	struct xrep_find_ag_btree	*btree_info;
427 };
428 
429 /* See if our block is in the AGFL. */
430 STATIC int
431 xrep_findroot_agfl_walk(
432 	struct xfs_mount	*mp,
433 	xfs_agblock_t		bno,
434 	void			*priv)
435 {
436 	xfs_agblock_t		*agbno = priv;
437 
438 	return (*agbno == bno) ? -ECANCELED : 0;
439 }
440 
441 /* Does this block match the btree information passed in? */
442 STATIC int
443 xrep_findroot_block(
444 	struct xrep_findroot		*ri,
445 	struct xrep_find_ag_btree	*fab,
446 	uint64_t			owner,
447 	xfs_agblock_t			agbno,
448 	bool				*done_with_block)
449 {
450 	struct xfs_mount		*mp = ri->sc->mp;
451 	struct xfs_buf			*bp;
452 	struct xfs_btree_block		*btblock;
453 	xfs_daddr_t			daddr;
454 	int				block_level;
455 	int				error = 0;
456 
457 	daddr = XFS_AGB_TO_DADDR(mp, ri->sc->sa.pag->pag_agno, agbno);
458 
459 	/*
460 	 * Blocks in the AGFL have stale contents that might just happen to
461 	 * have a matching magic and uuid.  We don't want to pull these blocks
462 	 * in as part of a tree root, so we have to filter out the AGFL stuff
463 	 * here.  If the AGFL looks insane we'll just refuse to repair.
464 	 */
465 	if (owner == XFS_RMAP_OWN_AG) {
466 		error = xfs_agfl_walk(mp, ri->agf, ri->agfl_bp,
467 				xrep_findroot_agfl_walk, &agbno);
468 		if (error == -ECANCELED)
469 			return 0;
470 		if (error)
471 			return error;
472 	}
473 
474 	/*
475 	 * Read the buffer into memory so that we can see if it's a match for
476 	 * our btree type.  We have no clue if it is beforehand, and we want to
477 	 * avoid xfs_trans_read_buf's behavior of dumping the DONE state (which
478 	 * will cause needless disk reads in subsequent calls to this function)
479 	 * and logging metadata verifier failures.
480 	 *
481 	 * Therefore, pass in NULL buffer ops.  If the buffer was already in
482 	 * memory from some other caller it will already have b_ops assigned.
483 	 * If it was in memory from a previous unsuccessful findroot_block
484 	 * call, the buffer won't have b_ops but it should be clean and ready
485 	 * for us to try to verify if the read call succeeds.  The same applies
486 	 * if the buffer wasn't in memory at all.
487 	 *
488 	 * Note: If we never match a btree type with this buffer, it will be
489 	 * left in memory with NULL b_ops.  This shouldn't be a problem unless
490 	 * the buffer gets written.
491 	 */
492 	error = xfs_trans_read_buf(mp, ri->sc->tp, mp->m_ddev_targp, daddr,
493 			mp->m_bsize, 0, &bp, NULL);
494 	if (error)
495 		return error;
496 
497 	/* Ensure the block magic matches the btree type we're looking for. */
498 	btblock = XFS_BUF_TO_BLOCK(bp);
499 	ASSERT(fab->buf_ops->magic[1] != 0);
500 	if (btblock->bb_magic != fab->buf_ops->magic[1])
501 		goto out;
502 
503 	/*
504 	 * If the buffer already has ops applied and they're not the ones for
505 	 * this btree type, we know this block doesn't match the btree and we
506 	 * can bail out.
507 	 *
508 	 * If the buffer ops match ours, someone else has already validated
509 	 * the block for us, so we can move on to checking if this is a root
510 	 * block candidate.
511 	 *
512 	 * If the buffer does not have ops, nobody has successfully validated
513 	 * the contents and the buffer cannot be dirty.  If the magic, uuid,
514 	 * and structure match this btree type then we'll move on to checking
515 	 * if it's a root block candidate.  If there is no match, bail out.
516 	 */
517 	if (bp->b_ops) {
518 		if (bp->b_ops != fab->buf_ops)
519 			goto out;
520 	} else {
521 		ASSERT(!xfs_trans_buf_is_dirty(bp));
522 		if (!uuid_equal(&btblock->bb_u.s.bb_uuid,
523 				&mp->m_sb.sb_meta_uuid))
524 			goto out;
525 		/*
526 		 * Read verifiers can reference b_ops, so we set the pointer
527 		 * here.  If the verifier fails we'll reset the buffer state
528 		 * to what it was before we touched the buffer.
529 		 */
530 		bp->b_ops = fab->buf_ops;
531 		fab->buf_ops->verify_read(bp);
532 		if (bp->b_error) {
533 			bp->b_ops = NULL;
534 			bp->b_error = 0;
535 			goto out;
536 		}
537 
538 		/*
539 		 * Some read verifiers will (re)set b_ops, so we must be
540 		 * careful not to change b_ops after running the verifier.
541 		 */
542 	}
543 
544 	/*
545 	 * This block passes the magic/uuid and verifier tests for this btree
546 	 * type.  We don't need the caller to try the other tree types.
547 	 */
548 	*done_with_block = true;
549 
550 	/*
551 	 * Compare this btree block's level to the height of the current
552 	 * candidate root block.
553 	 *
554 	 * If the level matches the root we found previously, throw away both
555 	 * blocks because there can't be two candidate roots.
556 	 *
557 	 * If level is lower in the tree than the root we found previously,
558 	 * ignore this block.
559 	 */
560 	block_level = xfs_btree_get_level(btblock);
561 	if (block_level + 1 == fab->height) {
562 		fab->root = NULLAGBLOCK;
563 		goto out;
564 	} else if (block_level < fab->height) {
565 		goto out;
566 	}
567 
568 	/*
569 	 * This is the highest block in the tree that we've found so far.
570 	 * Update the btree height to reflect what we've learned from this
571 	 * block.
572 	 */
573 	fab->height = block_level + 1;
574 
575 	/*
576 	 * If this block doesn't have sibling pointers, then it's the new root
577 	 * block candidate.  Otherwise, the root will be found farther up the
578 	 * tree.
579 	 */
580 	if (btblock->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK) &&
581 	    btblock->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK))
582 		fab->root = agbno;
583 	else
584 		fab->root = NULLAGBLOCK;
585 
586 	trace_xrep_findroot_block(mp, ri->sc->sa.pag->pag_agno, agbno,
587 			be32_to_cpu(btblock->bb_magic), fab->height - 1);
588 out:
589 	xfs_trans_brelse(ri->sc->tp, bp);
590 	return error;
591 }
592 
593 /*
594  * Do any of the blocks in this rmap record match one of the btrees we're
595  * looking for?
596  */
597 STATIC int
598 xrep_findroot_rmap(
599 	struct xfs_btree_cur		*cur,
600 	const struct xfs_rmap_irec	*rec,
601 	void				*priv)
602 {
603 	struct xrep_findroot		*ri = priv;
604 	struct xrep_find_ag_btree	*fab;
605 	xfs_agblock_t			b;
606 	bool				done;
607 	int				error = 0;
608 
609 	/* Ignore anything that isn't AG metadata. */
610 	if (!XFS_RMAP_NON_INODE_OWNER(rec->rm_owner))
611 		return 0;
612 
613 	/* Otherwise scan each block + btree type. */
614 	for (b = 0; b < rec->rm_blockcount; b++) {
615 		done = false;
616 		for (fab = ri->btree_info; fab->buf_ops; fab++) {
617 			if (rec->rm_owner != fab->rmap_owner)
618 				continue;
619 			error = xrep_findroot_block(ri, fab,
620 					rec->rm_owner, rec->rm_startblock + b,
621 					&done);
622 			if (error)
623 				return error;
624 			if (done)
625 				break;
626 		}
627 	}
628 
629 	return 0;
630 }
631 
632 /* Find the roots of the per-AG btrees described in btree_info. */
633 int
634 xrep_find_ag_btree_roots(
635 	struct xfs_scrub		*sc,
636 	struct xfs_buf			*agf_bp,
637 	struct xrep_find_ag_btree	*btree_info,
638 	struct xfs_buf			*agfl_bp)
639 {
640 	struct xfs_mount		*mp = sc->mp;
641 	struct xrep_findroot		ri;
642 	struct xrep_find_ag_btree	*fab;
643 	struct xfs_btree_cur		*cur;
644 	int				error;
645 
646 	ASSERT(xfs_buf_islocked(agf_bp));
647 	ASSERT(agfl_bp == NULL || xfs_buf_islocked(agfl_bp));
648 
649 	ri.sc = sc;
650 	ri.btree_info = btree_info;
651 	ri.agf = agf_bp->b_addr;
652 	ri.agfl_bp = agfl_bp;
653 	for (fab = btree_info; fab->buf_ops; fab++) {
654 		ASSERT(agfl_bp || fab->rmap_owner != XFS_RMAP_OWN_AG);
655 		ASSERT(XFS_RMAP_NON_INODE_OWNER(fab->rmap_owner));
656 		fab->root = NULLAGBLOCK;
657 		fab->height = 0;
658 	}
659 
660 	cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.pag);
661 	error = xfs_rmap_query_all(cur, xrep_findroot_rmap, &ri);
662 	xfs_btree_del_cursor(cur, error);
663 
664 	return error;
665 }
666 
667 /* Force a quotacheck the next time we mount. */
668 void
669 xrep_force_quotacheck(
670 	struct xfs_scrub	*sc,
671 	xfs_dqtype_t		type)
672 {
673 	uint			flag;
674 
675 	flag = xfs_quota_chkd_flag(type);
676 	if (!(flag & sc->mp->m_qflags))
677 		return;
678 
679 	mutex_lock(&sc->mp->m_quotainfo->qi_quotaofflock);
680 	sc->mp->m_qflags &= ~flag;
681 	spin_lock(&sc->mp->m_sb_lock);
682 	sc->mp->m_sb.sb_qflags &= ~flag;
683 	spin_unlock(&sc->mp->m_sb_lock);
684 	xfs_log_sb(sc->tp);
685 	mutex_unlock(&sc->mp->m_quotainfo->qi_quotaofflock);
686 }
687 
688 /*
689  * Attach dquots to this inode, or schedule quotacheck to fix them.
690  *
691  * This function ensures that the appropriate dquots are attached to an inode.
692  * We cannot allow the dquot code to allocate an on-disk dquot block here
693  * because we're already in transaction context with the inode locked.  The
694  * on-disk dquot should already exist anyway.  If the quota code signals
695  * corruption or missing quota information, schedule quotacheck, which will
696  * repair corruptions in the quota metadata.
697  */
698 int
699 xrep_ino_dqattach(
700 	struct xfs_scrub	*sc)
701 {
702 	int			error;
703 
704 	error = xfs_qm_dqattach_locked(sc->ip, false);
705 	switch (error) {
706 	case -EFSBADCRC:
707 	case -EFSCORRUPTED:
708 	case -ENOENT:
709 		xfs_err_ratelimited(sc->mp,
710 "inode %llu repair encountered quota error %d, quotacheck forced.",
711 				(unsigned long long)sc->ip->i_ino, error);
712 		if (XFS_IS_UQUOTA_ON(sc->mp) && !sc->ip->i_udquot)
713 			xrep_force_quotacheck(sc, XFS_DQTYPE_USER);
714 		if (XFS_IS_GQUOTA_ON(sc->mp) && !sc->ip->i_gdquot)
715 			xrep_force_quotacheck(sc, XFS_DQTYPE_GROUP);
716 		if (XFS_IS_PQUOTA_ON(sc->mp) && !sc->ip->i_pdquot)
717 			xrep_force_quotacheck(sc, XFS_DQTYPE_PROJ);
718 		fallthrough;
719 	case -ESRCH:
720 		error = 0;
721 		break;
722 	default:
723 		break;
724 	}
725 
726 	return error;
727 }
728