xref: /openbmc/linux/fs/xfs/scrub/refcount.c (revision 466c525d6d35e69115852c004f405f0711b8f91a)
1739a2fe0SDarrick J. Wong // SPDX-License-Identifier: GPL-2.0-or-later
2edc09b52SDarrick J. Wong /*
3ecc73f8aSDarrick J. Wong  * Copyright (C) 2017-2023 Oracle.  All Rights Reserved.
4739a2fe0SDarrick J. Wong  * Author: Darrick J. Wong <djwong@kernel.org>
5edc09b52SDarrick J. Wong  */
6edc09b52SDarrick J. Wong #include "xfs.h"
7edc09b52SDarrick J. Wong #include "xfs_fs.h"
8edc09b52SDarrick J. Wong #include "xfs_shared.h"
9edc09b52SDarrick J. Wong #include "xfs_format.h"
10d5c88131SDarrick J. Wong #include "xfs_trans_resv.h"
11d5c88131SDarrick J. Wong #include "xfs_mount.h"
12edc09b52SDarrick J. Wong #include "xfs_btree.h"
13edc09b52SDarrick J. Wong #include "xfs_rmap.h"
14f6d5fc21SDarrick J. Wong #include "xfs_refcount.h"
15edc09b52SDarrick J. Wong #include "scrub/scrub.h"
16edc09b52SDarrick J. Wong #include "scrub/common.h"
17edc09b52SDarrick J. Wong #include "scrub/btree.h"
1890148903SDarrick J. Wong #include "scrub/trace.h"
1936029deeSDave Chinner #include "xfs_trans_resv.h"
2036029deeSDave Chinner #include "xfs_mount.h"
2150f02fe3SDave Chinner #include "xfs_ag.h"
22edc09b52SDarrick J. Wong 
23edc09b52SDarrick J. Wong /*
24edc09b52SDarrick J. Wong  * Set us up to scrub reference count btrees.
25edc09b52SDarrick J. Wong  */
26edc09b52SDarrick J. Wong int
27c517b3aaSDarrick J. Wong xchk_setup_ag_refcountbt(
28026f57ebSDarrick J. Wong 	struct xfs_scrub	*sc)
29edc09b52SDarrick J. Wong {
30*466c525dSDarrick J. Wong 	if (xchk_need_intent_drain(sc))
31*466c525dSDarrick J. Wong 		xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN);
32026f57ebSDarrick J. Wong 	return xchk_setup_ag_btree(sc, false);
33edc09b52SDarrick J. Wong }
34edc09b52SDarrick J. Wong 
35edc09b52SDarrick J. Wong /* Reference count btree scrubber. */
36edc09b52SDarrick J. Wong 
37dbde19daSDarrick J. Wong /*
38dbde19daSDarrick J. Wong  * Confirming Reference Counts via Reverse Mappings
39dbde19daSDarrick J. Wong  *
40dbde19daSDarrick J. Wong  * We want to count the reverse mappings overlapping a refcount record
41dbde19daSDarrick J. Wong  * (bno, len, refcount), allowing for the possibility that some of the
42dbde19daSDarrick J. Wong  * overlap may come from smaller adjoining reverse mappings, while some
43dbde19daSDarrick J. Wong  * comes from single extents which overlap the range entirely.  The
44dbde19daSDarrick J. Wong  * outer loop is as follows:
45dbde19daSDarrick J. Wong  *
46dbde19daSDarrick J. Wong  * 1. For all reverse mappings overlapping the refcount extent,
47dbde19daSDarrick J. Wong  *    a. If a given rmap completely overlaps, mark it as seen.
48dbde19daSDarrick J. Wong  *    b. Otherwise, record the fragment (in agbno order) for later
49dbde19daSDarrick J. Wong  *       processing.
50dbde19daSDarrick J. Wong  *
51dbde19daSDarrick J. Wong  * Once we've seen all the rmaps, we know that for all blocks in the
52dbde19daSDarrick J. Wong  * refcount record we want to find $refcount owners and we've already
53dbde19daSDarrick J. Wong  * visited $seen extents that overlap all the blocks.  Therefore, we
54dbde19daSDarrick J. Wong  * need to find ($refcount - $seen) owners for every block in the
55dbde19daSDarrick J. Wong  * extent; call that quantity $target_nr.  Proceed as follows:
56dbde19daSDarrick J. Wong  *
57dbde19daSDarrick J. Wong  * 2. Pull the first $target_nr fragments from the list; all of them
58dbde19daSDarrick J. Wong  *    should start at or before the start of the extent.
59dbde19daSDarrick J. Wong  *    Call this subset of fragments the working set.
60dbde19daSDarrick J. Wong  * 3. Until there are no more unprocessed fragments,
61dbde19daSDarrick J. Wong  *    a. Find the shortest fragments in the set and remove them.
62dbde19daSDarrick J. Wong  *    b. Note the block number of the end of these fragments.
63dbde19daSDarrick J. Wong  *    c. Pull the same number of fragments from the list.  All of these
64dbde19daSDarrick J. Wong  *       fragments should start at the block number recorded in the
65dbde19daSDarrick J. Wong  *       previous step.
66dbde19daSDarrick J. Wong  *    d. Put those fragments in the set.
67dbde19daSDarrick J. Wong  * 4. Check that there are $target_nr fragments remaining in the list,
68dbde19daSDarrick J. Wong  *    and that they all end at or beyond the end of the refcount extent.
69dbde19daSDarrick J. Wong  *
70dbde19daSDarrick J. Wong  * If the refcount is correct, all the check conditions in the algorithm
71dbde19daSDarrick J. Wong  * should always hold true.  If not, the refcount is incorrect.
72dbde19daSDarrick J. Wong  */
73c517b3aaSDarrick J. Wong struct xchk_refcnt_frag {
74dbde19daSDarrick J. Wong 	struct list_head	list;
75dbde19daSDarrick J. Wong 	struct xfs_rmap_irec	rm;
76dbde19daSDarrick J. Wong };
77dbde19daSDarrick J. Wong 
78c517b3aaSDarrick J. Wong struct xchk_refcnt_check {
791d8a748aSDarrick J. Wong 	struct xfs_scrub	*sc;
80dbde19daSDarrick J. Wong 	struct list_head	fragments;
81dbde19daSDarrick J. Wong 
82dbde19daSDarrick J. Wong 	/* refcount extent we're examining */
83dbde19daSDarrick J. Wong 	xfs_agblock_t		bno;
84dbde19daSDarrick J. Wong 	xfs_extlen_t		len;
85dbde19daSDarrick J. Wong 	xfs_nlink_t		refcount;
86dbde19daSDarrick J. Wong 
87dbde19daSDarrick J. Wong 	/* number of owners seen */
88dbde19daSDarrick J. Wong 	xfs_nlink_t		seen;
89dbde19daSDarrick J. Wong };
90dbde19daSDarrick J. Wong 
91dbde19daSDarrick J. Wong /*
92dbde19daSDarrick J. Wong  * Decide if the given rmap is large enough that we can redeem it
93dbde19daSDarrick J. Wong  * towards refcount verification now, or if it's a fragment, in
94dbde19daSDarrick J. Wong  * which case we'll hang onto it in the hopes that we'll later
95dbde19daSDarrick J. Wong  * discover that we've collected exactly the correct number of
96dbde19daSDarrick J. Wong  * fragments as the refcountbt says we should have.
97dbde19daSDarrick J. Wong  */
98dbde19daSDarrick J. Wong STATIC int
99c517b3aaSDarrick J. Wong xchk_refcountbt_rmap_check(
100dbde19daSDarrick J. Wong 	struct xfs_btree_cur		*cur,
101159eb69dSDarrick J. Wong 	const struct xfs_rmap_irec	*rec,
102dbde19daSDarrick J. Wong 	void				*priv)
103dbde19daSDarrick J. Wong {
104c517b3aaSDarrick J. Wong 	struct xchk_refcnt_check	*refchk = priv;
105c517b3aaSDarrick J. Wong 	struct xchk_refcnt_frag		*frag;
106dbde19daSDarrick J. Wong 	xfs_agblock_t			rm_last;
107dbde19daSDarrick J. Wong 	xfs_agblock_t			rc_last;
108dbde19daSDarrick J. Wong 	int				error = 0;
109dbde19daSDarrick J. Wong 
110c517b3aaSDarrick J. Wong 	if (xchk_should_terminate(refchk->sc, &error))
111dbde19daSDarrick J. Wong 		return error;
112dbde19daSDarrick J. Wong 
113dbde19daSDarrick J. Wong 	rm_last = rec->rm_startblock + rec->rm_blockcount - 1;
114dbde19daSDarrick J. Wong 	rc_last = refchk->bno + refchk->len - 1;
115dbde19daSDarrick J. Wong 
116dbde19daSDarrick J. Wong 	/* Confirm that a single-owner refc extent is a CoW stage. */
117dbde19daSDarrick J. Wong 	if (refchk->refcount == 1 && rec->rm_owner != XFS_RMAP_OWN_COW) {
118c517b3aaSDarrick J. Wong 		xchk_btree_xref_set_corrupt(refchk->sc, cur, 0);
119dbde19daSDarrick J. Wong 		return 0;
120dbde19daSDarrick J. Wong 	}
121dbde19daSDarrick J. Wong 
122dbde19daSDarrick J. Wong 	if (rec->rm_startblock <= refchk->bno && rm_last >= rc_last) {
123dbde19daSDarrick J. Wong 		/*
124dbde19daSDarrick J. Wong 		 * The rmap overlaps the refcount record, so we can confirm
125dbde19daSDarrick J. Wong 		 * one refcount owner seen.
126dbde19daSDarrick J. Wong 		 */
127dbde19daSDarrick J. Wong 		refchk->seen++;
128dbde19daSDarrick J. Wong 	} else {
129dbde19daSDarrick J. Wong 		/*
130dbde19daSDarrick J. Wong 		 * This rmap covers only part of the refcount record, so
131dbde19daSDarrick J. Wong 		 * save the fragment for later processing.  If the rmapbt
132dbde19daSDarrick J. Wong 		 * is healthy each rmap_irec we see will be in agbno order
133dbde19daSDarrick J. Wong 		 * so we don't need insertion sort here.
134dbde19daSDarrick J. Wong 		 */
135306195f3SDarrick J. Wong 		frag = kmalloc(sizeof(struct xchk_refcnt_frag),
136306195f3SDarrick J. Wong 				XCHK_GFP_FLAGS);
137dbde19daSDarrick J. Wong 		if (!frag)
138dbde19daSDarrick J. Wong 			return -ENOMEM;
139dbde19daSDarrick J. Wong 		memcpy(&frag->rm, rec, sizeof(frag->rm));
140dbde19daSDarrick J. Wong 		list_add_tail(&frag->list, &refchk->fragments);
141dbde19daSDarrick J. Wong 	}
142dbde19daSDarrick J. Wong 
143dbde19daSDarrick J. Wong 	return 0;
144dbde19daSDarrick J. Wong }
145dbde19daSDarrick J. Wong 
146dbde19daSDarrick J. Wong /*
147dbde19daSDarrick J. Wong  * Given a bunch of rmap fragments, iterate through them, keeping
148dbde19daSDarrick J. Wong  * a running tally of the refcount.  If this ever deviates from
149dbde19daSDarrick J. Wong  * what we expect (which is the refcountbt's refcount minus the
150dbde19daSDarrick J. Wong  * number of extents that totally covered the refcountbt extent),
151dbde19daSDarrick J. Wong  * we have a refcountbt error.
152dbde19daSDarrick J. Wong  */
153dbde19daSDarrick J. Wong STATIC void
154c517b3aaSDarrick J. Wong xchk_refcountbt_process_rmap_fragments(
155c517b3aaSDarrick J. Wong 	struct xchk_refcnt_check	*refchk)
156dbde19daSDarrick J. Wong {
157dbde19daSDarrick J. Wong 	struct list_head		worklist;
158c517b3aaSDarrick J. Wong 	struct xchk_refcnt_frag		*frag;
159c517b3aaSDarrick J. Wong 	struct xchk_refcnt_frag		*n;
160dbde19daSDarrick J. Wong 	xfs_agblock_t			bno;
161dbde19daSDarrick J. Wong 	xfs_agblock_t			rbno;
162dbde19daSDarrick J. Wong 	xfs_agblock_t			next_rbno;
163dbde19daSDarrick J. Wong 	xfs_nlink_t			nr;
164dbde19daSDarrick J. Wong 	xfs_nlink_t			target_nr;
165dbde19daSDarrick J. Wong 
166dbde19daSDarrick J. Wong 	target_nr = refchk->refcount - refchk->seen;
167dbde19daSDarrick J. Wong 	if (target_nr == 0)
168dbde19daSDarrick J. Wong 		return;
169dbde19daSDarrick J. Wong 
170dbde19daSDarrick J. Wong 	/*
171dbde19daSDarrick J. Wong 	 * There are (refchk->rc.rc_refcount - refchk->nr refcount)
172dbde19daSDarrick J. Wong 	 * references we haven't found yet.  Pull that many off the
173dbde19daSDarrick J. Wong 	 * fragment list and figure out where the smallest rmap ends
174dbde19daSDarrick J. Wong 	 * (and therefore the next rmap should start).  All the rmaps
175dbde19daSDarrick J. Wong 	 * we pull off should start at or before the beginning of the
176dbde19daSDarrick J. Wong 	 * refcount record's range.
177dbde19daSDarrick J. Wong 	 */
178dbde19daSDarrick J. Wong 	INIT_LIST_HEAD(&worklist);
179dbde19daSDarrick J. Wong 	rbno = NULLAGBLOCK;
180dbde19daSDarrick J. Wong 
181dbde19daSDarrick J. Wong 	/* Make sure the fragments actually /are/ in agbno order. */
182dbde19daSDarrick J. Wong 	bno = 0;
183dbde19daSDarrick J. Wong 	list_for_each_entry(frag, &refchk->fragments, list) {
184dbde19daSDarrick J. Wong 		if (frag->rm.rm_startblock < bno)
185dbde19daSDarrick J. Wong 			goto done;
186dbde19daSDarrick J. Wong 		bno = frag->rm.rm_startblock;
187dbde19daSDarrick J. Wong 	}
188dbde19daSDarrick J. Wong 
189dbde19daSDarrick J. Wong 	/*
190dbde19daSDarrick J. Wong 	 * Find all the rmaps that start at or before the refc extent,
191dbde19daSDarrick J. Wong 	 * and put them on the worklist.
192dbde19daSDarrick J. Wong 	 */
19354e9b09eSDarrick J. Wong 	nr = 0;
194dbde19daSDarrick J. Wong 	list_for_each_entry_safe(frag, n, &refchk->fragments, list) {
19554e9b09eSDarrick J. Wong 		if (frag->rm.rm_startblock > refchk->bno || nr > target_nr)
19654e9b09eSDarrick J. Wong 			break;
197dbde19daSDarrick J. Wong 		bno = frag->rm.rm_startblock + frag->rm.rm_blockcount;
198dbde19daSDarrick J. Wong 		if (bno < rbno)
199dbde19daSDarrick J. Wong 			rbno = bno;
200dbde19daSDarrick J. Wong 		list_move_tail(&frag->list, &worklist);
201dbde19daSDarrick J. Wong 		nr++;
202dbde19daSDarrick J. Wong 	}
203dbde19daSDarrick J. Wong 
204dbde19daSDarrick J. Wong 	/*
205dbde19daSDarrick J. Wong 	 * We should have found exactly $target_nr rmap fragments starting
206dbde19daSDarrick J. Wong 	 * at or before the refcount extent.
207dbde19daSDarrick J. Wong 	 */
208dbde19daSDarrick J. Wong 	if (nr != target_nr)
209dbde19daSDarrick J. Wong 		goto done;
210dbde19daSDarrick J. Wong 
211dbde19daSDarrick J. Wong 	while (!list_empty(&refchk->fragments)) {
212dbde19daSDarrick J. Wong 		/* Discard any fragments ending at rbno from the worklist. */
213dbde19daSDarrick J. Wong 		nr = 0;
214dbde19daSDarrick J. Wong 		next_rbno = NULLAGBLOCK;
215dbde19daSDarrick J. Wong 		list_for_each_entry_safe(frag, n, &worklist, list) {
216dbde19daSDarrick J. Wong 			bno = frag->rm.rm_startblock + frag->rm.rm_blockcount;
217dbde19daSDarrick J. Wong 			if (bno != rbno) {
218dbde19daSDarrick J. Wong 				if (bno < next_rbno)
219dbde19daSDarrick J. Wong 					next_rbno = bno;
220dbde19daSDarrick J. Wong 				continue;
221dbde19daSDarrick J. Wong 			}
222dbde19daSDarrick J. Wong 			list_del(&frag->list);
223306195f3SDarrick J. Wong 			kfree(frag);
224dbde19daSDarrick J. Wong 			nr++;
225dbde19daSDarrick J. Wong 		}
226dbde19daSDarrick J. Wong 
227dbde19daSDarrick J. Wong 		/* Try to add nr rmaps starting at rbno to the worklist. */
228dbde19daSDarrick J. Wong 		list_for_each_entry_safe(frag, n, &refchk->fragments, list) {
229dbde19daSDarrick J. Wong 			bno = frag->rm.rm_startblock + frag->rm.rm_blockcount;
230dbde19daSDarrick J. Wong 			if (frag->rm.rm_startblock != rbno)
231dbde19daSDarrick J. Wong 				goto done;
232dbde19daSDarrick J. Wong 			list_move_tail(&frag->list, &worklist);
233dbde19daSDarrick J. Wong 			if (next_rbno > bno)
234dbde19daSDarrick J. Wong 				next_rbno = bno;
235dbde19daSDarrick J. Wong 			nr--;
236dbde19daSDarrick J. Wong 			if (nr == 0)
237dbde19daSDarrick J. Wong 				break;
238dbde19daSDarrick J. Wong 		}
239dbde19daSDarrick J. Wong 
240dbde19daSDarrick J. Wong 		/*
241dbde19daSDarrick J. Wong 		 * If we get here and nr > 0, this means that we added fewer
242dbde19daSDarrick J. Wong 		 * items to the worklist than we discarded because the fragment
243dbde19daSDarrick J. Wong 		 * list ran out of items.  Therefore, we cannot maintain the
244dbde19daSDarrick J. Wong 		 * required refcount.  Something is wrong, so we're done.
245dbde19daSDarrick J. Wong 		 */
246dbde19daSDarrick J. Wong 		if (nr)
247dbde19daSDarrick J. Wong 			goto done;
248dbde19daSDarrick J. Wong 
249dbde19daSDarrick J. Wong 		rbno = next_rbno;
250dbde19daSDarrick J. Wong 	}
251dbde19daSDarrick J. Wong 
252dbde19daSDarrick J. Wong 	/*
253dbde19daSDarrick J. Wong 	 * Make sure the last extent we processed ends at or beyond
254dbde19daSDarrick J. Wong 	 * the end of the refcount extent.
255dbde19daSDarrick J. Wong 	 */
256dbde19daSDarrick J. Wong 	if (rbno < refchk->bno + refchk->len)
257dbde19daSDarrick J. Wong 		goto done;
258dbde19daSDarrick J. Wong 
259dbde19daSDarrick J. Wong 	/* Actually record us having seen the remaining refcount. */
260dbde19daSDarrick J. Wong 	refchk->seen = refchk->refcount;
261dbde19daSDarrick J. Wong done:
262dbde19daSDarrick J. Wong 	/* Delete fragments and work list. */
263dbde19daSDarrick J. Wong 	list_for_each_entry_safe(frag, n, &worklist, list) {
264dbde19daSDarrick J. Wong 		list_del(&frag->list);
265306195f3SDarrick J. Wong 		kfree(frag);
266dbde19daSDarrick J. Wong 	}
267dbde19daSDarrick J. Wong 	list_for_each_entry_safe(frag, n, &refchk->fragments, list) {
268dbde19daSDarrick J. Wong 		list_del(&frag->list);
269306195f3SDarrick J. Wong 		kfree(frag);
270dbde19daSDarrick J. Wong 	}
271dbde19daSDarrick J. Wong }
272dbde19daSDarrick J. Wong 
273dbde19daSDarrick J. Wong /* Use the rmap entries covering this extent to verify the refcount. */
274dbde19daSDarrick J. Wong STATIC void
275c517b3aaSDarrick J. Wong xchk_refcountbt_xref_rmap(
2761d8a748aSDarrick J. Wong 	struct xfs_scrub		*sc,
2775a8c345cSDarrick J. Wong 	const struct xfs_refcount_irec	*irec)
278dbde19daSDarrick J. Wong {
279c517b3aaSDarrick J. Wong 	struct xchk_refcnt_check	refchk = {
280dbde19daSDarrick J. Wong 		.sc			= sc,
2815a8c345cSDarrick J. Wong 		.bno			= irec->rc_startblock,
2825a8c345cSDarrick J. Wong 		.len			= irec->rc_blockcount,
2835a8c345cSDarrick J. Wong 		.refcount		= irec->rc_refcount,
284dbde19daSDarrick J. Wong 		.seen = 0,
285dbde19daSDarrick J. Wong 	};
286dbde19daSDarrick J. Wong 	struct xfs_rmap_irec		low;
287dbde19daSDarrick J. Wong 	struct xfs_rmap_irec		high;
288c517b3aaSDarrick J. Wong 	struct xchk_refcnt_frag		*frag;
289c517b3aaSDarrick J. Wong 	struct xchk_refcnt_frag		*n;
290dbde19daSDarrick J. Wong 	int				error;
291dbde19daSDarrick J. Wong 
292c517b3aaSDarrick J. Wong 	if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
293dbde19daSDarrick J. Wong 		return;
294dbde19daSDarrick J. Wong 
295dbde19daSDarrick J. Wong 	/* Cross-reference with the rmapbt to confirm the refcount. */
296dbde19daSDarrick J. Wong 	memset(&low, 0, sizeof(low));
2975a8c345cSDarrick J. Wong 	low.rm_startblock = irec->rc_startblock;
298dbde19daSDarrick J. Wong 	memset(&high, 0xFF, sizeof(high));
2995a8c345cSDarrick J. Wong 	high.rm_startblock = irec->rc_startblock + irec->rc_blockcount - 1;
300dbde19daSDarrick J. Wong 
301dbde19daSDarrick J. Wong 	INIT_LIST_HEAD(&refchk.fragments);
302dbde19daSDarrick J. Wong 	error = xfs_rmap_query_range(sc->sa.rmap_cur, &low, &high,
303c517b3aaSDarrick J. Wong 			&xchk_refcountbt_rmap_check, &refchk);
304c517b3aaSDarrick J. Wong 	if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
305dbde19daSDarrick J. Wong 		goto out_free;
306dbde19daSDarrick J. Wong 
307c517b3aaSDarrick J. Wong 	xchk_refcountbt_process_rmap_fragments(&refchk);
30890148903SDarrick J. Wong 	if (irec->rc_refcount != refchk.seen) {
30990148903SDarrick J. Wong 		trace_xchk_refcount_incorrect(sc->sa.pag, irec, refchk.seen);
310c517b3aaSDarrick J. Wong 		xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
31190148903SDarrick J. Wong 	}
312dbde19daSDarrick J. Wong 
313dbde19daSDarrick J. Wong out_free:
314dbde19daSDarrick J. Wong 	list_for_each_entry_safe(frag, n, &refchk.fragments, list) {
315dbde19daSDarrick J. Wong 		list_del(&frag->list);
316306195f3SDarrick J. Wong 		kfree(frag);
317dbde19daSDarrick J. Wong 	}
318dbde19daSDarrick J. Wong }
319dbde19daSDarrick J. Wong 
320166d7641SDarrick J. Wong /* Cross-reference with the other btrees. */
321166d7641SDarrick J. Wong STATIC void
322c517b3aaSDarrick J. Wong xchk_refcountbt_xref(
3231d8a748aSDarrick J. Wong 	struct xfs_scrub		*sc,
3245a8c345cSDarrick J. Wong 	const struct xfs_refcount_irec	*irec)
325166d7641SDarrick J. Wong {
326166d7641SDarrick J. Wong 	if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
327166d7641SDarrick J. Wong 		return;
32852dc4b44SDarrick J. Wong 
3295a8c345cSDarrick J. Wong 	xchk_xref_is_used_space(sc, irec->rc_startblock, irec->rc_blockcount);
3305a8c345cSDarrick J. Wong 	xchk_xref_is_not_inode_chunk(sc, irec->rc_startblock,
3315a8c345cSDarrick J. Wong 			irec->rc_blockcount);
3325a8c345cSDarrick J. Wong 	xchk_refcountbt_xref_rmap(sc, irec);
333166d7641SDarrick J. Wong }
334166d7641SDarrick J. Wong 
335edc09b52SDarrick J. Wong /* Scrub a refcountbt record. */
336edc09b52SDarrick J. Wong STATIC int
337c517b3aaSDarrick J. Wong xchk_refcountbt_rec(
338c517b3aaSDarrick J. Wong 	struct xchk_btree	*bs,
33922ece4e8SDarrick J. Wong 	const union xfs_btree_rec *rec)
340edc09b52SDarrick J. Wong {
3415a8c345cSDarrick J. Wong 	struct xfs_refcount_irec irec;
342dbde19daSDarrick J. Wong 	xfs_agblock_t		*cow_blocks = bs->private;
3430800169eSDave Chinner 	struct xfs_perag	*pag = bs->cur->bc_ag.pag;
344edc09b52SDarrick J. Wong 
3455a8c345cSDarrick J. Wong 	xfs_refcount_btrec_to_irec(rec, &irec);
346edc09b52SDarrick J. Wong 
347f492135dSDarrick J. Wong 	/* Check the domain and refcount are not incompatible. */
348f492135dSDarrick J. Wong 	if (!xfs_refcount_check_domain(&irec))
349c517b3aaSDarrick J. Wong 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
350f492135dSDarrick J. Wong 
351f492135dSDarrick J. Wong 	if (irec.rc_domain == XFS_REFC_DOMAIN_COW)
3525a8c345cSDarrick J. Wong 		(*cow_blocks) += irec.rc_blockcount;
353edc09b52SDarrick J. Wong 
354edc09b52SDarrick J. Wong 	/* Check the extent. */
3555a8c345cSDarrick J. Wong 	if (!xfs_verify_agbext(pag, irec.rc_startblock, irec.rc_blockcount))
356c517b3aaSDarrick J. Wong 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
357edc09b52SDarrick J. Wong 
3585a8c345cSDarrick J. Wong 	if (irec.rc_refcount == 0)
359c517b3aaSDarrick J. Wong 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
360edc09b52SDarrick J. Wong 
3615a8c345cSDarrick J. Wong 	xchk_refcountbt_xref(bs->sc, &irec);
362166d7641SDarrick J. Wong 
363d5cc14d9SAliasgar Surti 	return 0;
364edc09b52SDarrick J. Wong }
365edc09b52SDarrick J. Wong 
366dbde19daSDarrick J. Wong /* Make sure we have as many refc blocks as the rmap says. */
367dbde19daSDarrick J. Wong STATIC void
368c517b3aaSDarrick J. Wong xchk_refcount_xref_rmap(
3691d8a748aSDarrick J. Wong 	struct xfs_scrub	*sc,
370dbde19daSDarrick J. Wong 	xfs_filblks_t		cow_blocks)
371dbde19daSDarrick J. Wong {
372dbde19daSDarrick J. Wong 	xfs_extlen_t		refcbt_blocks = 0;
373dbde19daSDarrick J. Wong 	xfs_filblks_t		blocks;
374dbde19daSDarrick J. Wong 	int			error;
375dbde19daSDarrick J. Wong 
376c517b3aaSDarrick J. Wong 	if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
377dbde19daSDarrick J. Wong 		return;
378dbde19daSDarrick J. Wong 
379dbde19daSDarrick J. Wong 	/* Check that we saw as many refcbt blocks as the rmap knows about. */
380dbde19daSDarrick J. Wong 	error = xfs_btree_count_blocks(sc->sa.refc_cur, &refcbt_blocks);
381c517b3aaSDarrick J. Wong 	if (!xchk_btree_process_error(sc, sc->sa.refc_cur, 0, &error))
382dbde19daSDarrick J. Wong 		return;
3837280fedaSDarrick J. Wong 	error = xchk_count_rmap_ownedby_ag(sc, sc->sa.rmap_cur,
3847280fedaSDarrick J. Wong 			&XFS_RMAP_OINFO_REFC, &blocks);
385c517b3aaSDarrick J. Wong 	if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
386dbde19daSDarrick J. Wong 		return;
387dbde19daSDarrick J. Wong 	if (blocks != refcbt_blocks)
388c517b3aaSDarrick J. Wong 		xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
389dbde19daSDarrick J. Wong 
390dbde19daSDarrick J. Wong 	/* Check that we saw as many cow blocks as the rmap knows about. */
3917280fedaSDarrick J. Wong 	error = xchk_count_rmap_ownedby_ag(sc, sc->sa.rmap_cur,
3927280fedaSDarrick J. Wong 			&XFS_RMAP_OINFO_COW, &blocks);
393c517b3aaSDarrick J. Wong 	if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
394dbde19daSDarrick J. Wong 		return;
395dbde19daSDarrick J. Wong 	if (blocks != cow_blocks)
396c517b3aaSDarrick J. Wong 		xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
397dbde19daSDarrick J. Wong }
398dbde19daSDarrick J. Wong 
399edc09b52SDarrick J. Wong /* Scrub the refcount btree for some AG. */
400edc09b52SDarrick J. Wong int
401c517b3aaSDarrick J. Wong xchk_refcountbt(
4021d8a748aSDarrick J. Wong 	struct xfs_scrub	*sc)
403edc09b52SDarrick J. Wong {
404dbde19daSDarrick J. Wong 	xfs_agblock_t		cow_blocks = 0;
405dbde19daSDarrick J. Wong 	int			error;
406edc09b52SDarrick J. Wong 
407c517b3aaSDarrick J. Wong 	error = xchk_btree(sc, sc->sa.refc_cur, xchk_refcountbt_rec,
4087280fedaSDarrick J. Wong 			&XFS_RMAP_OINFO_REFC, &cow_blocks);
409dbde19daSDarrick J. Wong 	if (error)
410dbde19daSDarrick J. Wong 		return error;
411dbde19daSDarrick J. Wong 
41266e3237eSDarrick J. Wong 	xchk_refcount_xref_rmap(sc, cow_blocks);
413dbde19daSDarrick J. Wong 
414dbde19daSDarrick J. Wong 	return 0;
415edc09b52SDarrick J. Wong }
416f6d5fc21SDarrick J. Wong 
417f6d5fc21SDarrick J. Wong /* xref check that a cow staging extent is marked in the refcountbt. */
418f6d5fc21SDarrick J. Wong void
419c517b3aaSDarrick J. Wong xchk_xref_is_cow_staging(
4201d8a748aSDarrick J. Wong 	struct xfs_scrub		*sc,
421f6d5fc21SDarrick J. Wong 	xfs_agblock_t			agbno,
422f6d5fc21SDarrick J. Wong 	xfs_extlen_t			len)
423f6d5fc21SDarrick J. Wong {
424f6d5fc21SDarrick J. Wong 	struct xfs_refcount_irec	rc;
425f6d5fc21SDarrick J. Wong 	int				has_refcount;
426f6d5fc21SDarrick J. Wong 	int				error;
427f6d5fc21SDarrick J. Wong 
428c517b3aaSDarrick J. Wong 	if (!sc->sa.refc_cur || xchk_skip_xref(sc->sm))
429f6d5fc21SDarrick J. Wong 		return;
430f6d5fc21SDarrick J. Wong 
431f6d5fc21SDarrick J. Wong 	/* Find the CoW staging extent. */
4329a50ee4fSDarrick J. Wong 	error = xfs_refcount_lookup_le(sc->sa.refc_cur, XFS_REFC_DOMAIN_COW,
4339a50ee4fSDarrick J. Wong 			agbno, &has_refcount);
434c517b3aaSDarrick J. Wong 	if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur))
435f6d5fc21SDarrick J. Wong 		return;
436f6d5fc21SDarrick J. Wong 	if (!has_refcount) {
437c517b3aaSDarrick J. Wong 		xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0);
438f6d5fc21SDarrick J. Wong 		return;
439f6d5fc21SDarrick J. Wong 	}
440f6d5fc21SDarrick J. Wong 
441f6d5fc21SDarrick J. Wong 	error = xfs_refcount_get_rec(sc->sa.refc_cur, &rc, &has_refcount);
442c517b3aaSDarrick J. Wong 	if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur))
443f6d5fc21SDarrick J. Wong 		return;
444f6d5fc21SDarrick J. Wong 	if (!has_refcount) {
445c517b3aaSDarrick J. Wong 		xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0);
446f6d5fc21SDarrick J. Wong 		return;
447f6d5fc21SDarrick J. Wong 	}
448f6d5fc21SDarrick J. Wong 
449f62ac3e0SDarrick J. Wong 	/* CoW lookup returned a shared extent record? */
450f62ac3e0SDarrick J. Wong 	if (rc.rc_domain != XFS_REFC_DOMAIN_COW)
451c517b3aaSDarrick J. Wong 		xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0);
452f6d5fc21SDarrick J. Wong 
453f6d5fc21SDarrick J. Wong 	/* Must be at least as long as what was passed in */
454f6d5fc21SDarrick J. Wong 	if (rc.rc_blockcount < len)
455c517b3aaSDarrick J. Wong 		xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0);
456f6d5fc21SDarrick J. Wong }
457f6d5fc21SDarrick J. Wong 
458f6d5fc21SDarrick J. Wong /*
459f6d5fc21SDarrick J. Wong  * xref check that the extent is not shared.  Only file data blocks
460f6d5fc21SDarrick J. Wong  * can have multiple owners.
461f6d5fc21SDarrick J. Wong  */
462f6d5fc21SDarrick J. Wong void
463c517b3aaSDarrick J. Wong xchk_xref_is_not_shared(
4641d8a748aSDarrick J. Wong 	struct xfs_scrub	*sc,
465f6d5fc21SDarrick J. Wong 	xfs_agblock_t		agbno,
466f6d5fc21SDarrick J. Wong 	xfs_extlen_t		len)
467f6d5fc21SDarrick J. Wong {
468f6d5fc21SDarrick J. Wong 	bool			shared;
469f6d5fc21SDarrick J. Wong 	int			error;
470f6d5fc21SDarrick J. Wong 
471c517b3aaSDarrick J. Wong 	if (!sc->sa.refc_cur || xchk_skip_xref(sc->sm))
472f6d5fc21SDarrick J. Wong 		return;
473f6d5fc21SDarrick J. Wong 
4749a50ee4fSDarrick J. Wong 	error = xfs_refcount_has_record(sc->sa.refc_cur, XFS_REFC_DOMAIN_SHARED,
4759a50ee4fSDarrick J. Wong 			agbno, len, &shared);
476c517b3aaSDarrick J. Wong 	if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur))
477f6d5fc21SDarrick J. Wong 		return;
478f6d5fc21SDarrick J. Wong 	if (shared)
479c517b3aaSDarrick J. Wong 		xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0);
480f6d5fc21SDarrick J. Wong }
481