xref: /OK3568_Linux_fs/kernel/fs/xfs/scrub/refcount.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2017 Oracle.  All Rights Reserved.
4*4882a593Smuzhiyun  * Author: Darrick J. Wong <darrick.wong@oracle.com>
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun #include "xfs.h"
7*4882a593Smuzhiyun #include "xfs_fs.h"
8*4882a593Smuzhiyun #include "xfs_shared.h"
9*4882a593Smuzhiyun #include "xfs_format.h"
10*4882a593Smuzhiyun #include "xfs_btree.h"
11*4882a593Smuzhiyun #include "xfs_rmap.h"
12*4882a593Smuzhiyun #include "xfs_refcount.h"
13*4882a593Smuzhiyun #include "scrub/scrub.h"
14*4882a593Smuzhiyun #include "scrub/common.h"
15*4882a593Smuzhiyun #include "scrub/btree.h"
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun /*
18*4882a593Smuzhiyun  * Set us up to scrub reference count btrees.
19*4882a593Smuzhiyun  */
20*4882a593Smuzhiyun int
xchk_setup_ag_refcountbt(struct xfs_scrub * sc,struct xfs_inode * ip)21*4882a593Smuzhiyun xchk_setup_ag_refcountbt(
22*4882a593Smuzhiyun 	struct xfs_scrub	*sc,
23*4882a593Smuzhiyun 	struct xfs_inode	*ip)
24*4882a593Smuzhiyun {
25*4882a593Smuzhiyun 	return xchk_setup_ag_btree(sc, ip, false);
26*4882a593Smuzhiyun }
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun /* Reference count btree scrubber. */
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun /*
31*4882a593Smuzhiyun  * Confirming Reference Counts via Reverse Mappings
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  * We want to count the reverse mappings overlapping a refcount record
34*4882a593Smuzhiyun  * (bno, len, refcount), allowing for the possibility that some of the
35*4882a593Smuzhiyun  * overlap may come from smaller adjoining reverse mappings, while some
36*4882a593Smuzhiyun  * comes from single extents which overlap the range entirely.  The
37*4882a593Smuzhiyun  * outer loop is as follows:
38*4882a593Smuzhiyun  *
39*4882a593Smuzhiyun  * 1. For all reverse mappings overlapping the refcount extent,
40*4882a593Smuzhiyun  *    a. If a given rmap completely overlaps, mark it as seen.
41*4882a593Smuzhiyun  *    b. Otherwise, record the fragment (in agbno order) for later
42*4882a593Smuzhiyun  *       processing.
43*4882a593Smuzhiyun  *
44*4882a593Smuzhiyun  * Once we've seen all the rmaps, we know that for all blocks in the
45*4882a593Smuzhiyun  * refcount record we want to find $refcount owners and we've already
46*4882a593Smuzhiyun  * visited $seen extents that overlap all the blocks.  Therefore, we
47*4882a593Smuzhiyun  * need to find ($refcount - $seen) owners for every block in the
48*4882a593Smuzhiyun  * extent; call that quantity $target_nr.  Proceed as follows:
49*4882a593Smuzhiyun  *
50*4882a593Smuzhiyun  * 2. Pull the first $target_nr fragments from the list; all of them
51*4882a593Smuzhiyun  *    should start at or before the start of the extent.
52*4882a593Smuzhiyun  *    Call this subset of fragments the working set.
53*4882a593Smuzhiyun  * 3. Until there are no more unprocessed fragments,
54*4882a593Smuzhiyun  *    a. Find the shortest fragments in the set and remove them.
55*4882a593Smuzhiyun  *    b. Note the block number of the end of these fragments.
56*4882a593Smuzhiyun  *    c. Pull the same number of fragments from the list.  All of these
57*4882a593Smuzhiyun  *       fragments should start at the block number recorded in the
58*4882a593Smuzhiyun  *       previous step.
59*4882a593Smuzhiyun  *    d. Put those fragments in the set.
60*4882a593Smuzhiyun  * 4. Check that there are $target_nr fragments remaining in the list,
61*4882a593Smuzhiyun  *    and that they all end at or beyond the end of the refcount extent.
62*4882a593Smuzhiyun  *
63*4882a593Smuzhiyun  * If the refcount is correct, all the check conditions in the algorithm
64*4882a593Smuzhiyun  * should always hold true.  If not, the refcount is incorrect.
65*4882a593Smuzhiyun  */
66*4882a593Smuzhiyun struct xchk_refcnt_frag {
67*4882a593Smuzhiyun 	struct list_head	list;
68*4882a593Smuzhiyun 	struct xfs_rmap_irec	rm;
69*4882a593Smuzhiyun };
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun struct xchk_refcnt_check {
72*4882a593Smuzhiyun 	struct xfs_scrub	*sc;
73*4882a593Smuzhiyun 	struct list_head	fragments;
74*4882a593Smuzhiyun 
75*4882a593Smuzhiyun 	/* refcount extent we're examining */
76*4882a593Smuzhiyun 	xfs_agblock_t		bno;
77*4882a593Smuzhiyun 	xfs_extlen_t		len;
78*4882a593Smuzhiyun 	xfs_nlink_t		refcount;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	/* number of owners seen */
81*4882a593Smuzhiyun 	xfs_nlink_t		seen;
82*4882a593Smuzhiyun };
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun /*
85*4882a593Smuzhiyun  * Decide if the given rmap is large enough that we can redeem it
86*4882a593Smuzhiyun  * towards refcount verification now, or if it's a fragment, in
87*4882a593Smuzhiyun  * which case we'll hang onto it in the hopes that we'll later
88*4882a593Smuzhiyun  * discover that we've collected exactly the correct number of
89*4882a593Smuzhiyun  * fragments as the refcountbt says we should have.
90*4882a593Smuzhiyun  */
91*4882a593Smuzhiyun STATIC int
xchk_refcountbt_rmap_check(struct xfs_btree_cur * cur,struct xfs_rmap_irec * rec,void * priv)92*4882a593Smuzhiyun xchk_refcountbt_rmap_check(
93*4882a593Smuzhiyun 	struct xfs_btree_cur		*cur,
94*4882a593Smuzhiyun 	struct xfs_rmap_irec		*rec,
95*4882a593Smuzhiyun 	void				*priv)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun 	struct xchk_refcnt_check	*refchk = priv;
98*4882a593Smuzhiyun 	struct xchk_refcnt_frag		*frag;
99*4882a593Smuzhiyun 	xfs_agblock_t			rm_last;
100*4882a593Smuzhiyun 	xfs_agblock_t			rc_last;
101*4882a593Smuzhiyun 	int				error = 0;
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	if (xchk_should_terminate(refchk->sc, &error))
104*4882a593Smuzhiyun 		return error;
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	rm_last = rec->rm_startblock + rec->rm_blockcount - 1;
107*4882a593Smuzhiyun 	rc_last = refchk->bno + refchk->len - 1;
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	/* Confirm that a single-owner refc extent is a CoW stage. */
110*4882a593Smuzhiyun 	if (refchk->refcount == 1 && rec->rm_owner != XFS_RMAP_OWN_COW) {
111*4882a593Smuzhiyun 		xchk_btree_xref_set_corrupt(refchk->sc, cur, 0);
112*4882a593Smuzhiyun 		return 0;
113*4882a593Smuzhiyun 	}
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	if (rec->rm_startblock <= refchk->bno && rm_last >= rc_last) {
116*4882a593Smuzhiyun 		/*
117*4882a593Smuzhiyun 		 * The rmap overlaps the refcount record, so we can confirm
118*4882a593Smuzhiyun 		 * one refcount owner seen.
119*4882a593Smuzhiyun 		 */
120*4882a593Smuzhiyun 		refchk->seen++;
121*4882a593Smuzhiyun 	} else {
122*4882a593Smuzhiyun 		/*
123*4882a593Smuzhiyun 		 * This rmap covers only part of the refcount record, so
124*4882a593Smuzhiyun 		 * save the fragment for later processing.  If the rmapbt
125*4882a593Smuzhiyun 		 * is healthy each rmap_irec we see will be in agbno order
126*4882a593Smuzhiyun 		 * so we don't need insertion sort here.
127*4882a593Smuzhiyun 		 */
128*4882a593Smuzhiyun 		frag = kmem_alloc(sizeof(struct xchk_refcnt_frag),
129*4882a593Smuzhiyun 				KM_MAYFAIL);
130*4882a593Smuzhiyun 		if (!frag)
131*4882a593Smuzhiyun 			return -ENOMEM;
132*4882a593Smuzhiyun 		memcpy(&frag->rm, rec, sizeof(frag->rm));
133*4882a593Smuzhiyun 		list_add_tail(&frag->list, &refchk->fragments);
134*4882a593Smuzhiyun 	}
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	return 0;
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun /*
140*4882a593Smuzhiyun  * Given a bunch of rmap fragments, iterate through them, keeping
141*4882a593Smuzhiyun  * a running tally of the refcount.  If this ever deviates from
142*4882a593Smuzhiyun  * what we expect (which is the refcountbt's refcount minus the
143*4882a593Smuzhiyun  * number of extents that totally covered the refcountbt extent),
144*4882a593Smuzhiyun  * we have a refcountbt error.
145*4882a593Smuzhiyun  */
146*4882a593Smuzhiyun STATIC void
xchk_refcountbt_process_rmap_fragments(struct xchk_refcnt_check * refchk)147*4882a593Smuzhiyun xchk_refcountbt_process_rmap_fragments(
148*4882a593Smuzhiyun 	struct xchk_refcnt_check	*refchk)
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun 	struct list_head		worklist;
151*4882a593Smuzhiyun 	struct xchk_refcnt_frag		*frag;
152*4882a593Smuzhiyun 	struct xchk_refcnt_frag		*n;
153*4882a593Smuzhiyun 	xfs_agblock_t			bno;
154*4882a593Smuzhiyun 	xfs_agblock_t			rbno;
155*4882a593Smuzhiyun 	xfs_agblock_t			next_rbno;
156*4882a593Smuzhiyun 	xfs_nlink_t			nr;
157*4882a593Smuzhiyun 	xfs_nlink_t			target_nr;
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	target_nr = refchk->refcount - refchk->seen;
160*4882a593Smuzhiyun 	if (target_nr == 0)
161*4882a593Smuzhiyun 		return;
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	/*
164*4882a593Smuzhiyun 	 * There are (refchk->rc.rc_refcount - refchk->nr refcount)
165*4882a593Smuzhiyun 	 * references we haven't found yet.  Pull that many off the
166*4882a593Smuzhiyun 	 * fragment list and figure out where the smallest rmap ends
167*4882a593Smuzhiyun 	 * (and therefore the next rmap should start).  All the rmaps
168*4882a593Smuzhiyun 	 * we pull off should start at or before the beginning of the
169*4882a593Smuzhiyun 	 * refcount record's range.
170*4882a593Smuzhiyun 	 */
171*4882a593Smuzhiyun 	INIT_LIST_HEAD(&worklist);
172*4882a593Smuzhiyun 	rbno = NULLAGBLOCK;
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	/* Make sure the fragments actually /are/ in agbno order. */
175*4882a593Smuzhiyun 	bno = 0;
176*4882a593Smuzhiyun 	list_for_each_entry(frag, &refchk->fragments, list) {
177*4882a593Smuzhiyun 		if (frag->rm.rm_startblock < bno)
178*4882a593Smuzhiyun 			goto done;
179*4882a593Smuzhiyun 		bno = frag->rm.rm_startblock;
180*4882a593Smuzhiyun 	}
181*4882a593Smuzhiyun 
182*4882a593Smuzhiyun 	/*
183*4882a593Smuzhiyun 	 * Find all the rmaps that start at or before the refc extent,
184*4882a593Smuzhiyun 	 * and put them on the worklist.
185*4882a593Smuzhiyun 	 */
186*4882a593Smuzhiyun 	nr = 0;
187*4882a593Smuzhiyun 	list_for_each_entry_safe(frag, n, &refchk->fragments, list) {
188*4882a593Smuzhiyun 		if (frag->rm.rm_startblock > refchk->bno || nr > target_nr)
189*4882a593Smuzhiyun 			break;
190*4882a593Smuzhiyun 		bno = frag->rm.rm_startblock + frag->rm.rm_blockcount;
191*4882a593Smuzhiyun 		if (bno < rbno)
192*4882a593Smuzhiyun 			rbno = bno;
193*4882a593Smuzhiyun 		list_move_tail(&frag->list, &worklist);
194*4882a593Smuzhiyun 		nr++;
195*4882a593Smuzhiyun 	}
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	/*
198*4882a593Smuzhiyun 	 * We should have found exactly $target_nr rmap fragments starting
199*4882a593Smuzhiyun 	 * at or before the refcount extent.
200*4882a593Smuzhiyun 	 */
201*4882a593Smuzhiyun 	if (nr != target_nr)
202*4882a593Smuzhiyun 		goto done;
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	while (!list_empty(&refchk->fragments)) {
205*4882a593Smuzhiyun 		/* Discard any fragments ending at rbno from the worklist. */
206*4882a593Smuzhiyun 		nr = 0;
207*4882a593Smuzhiyun 		next_rbno = NULLAGBLOCK;
208*4882a593Smuzhiyun 		list_for_each_entry_safe(frag, n, &worklist, list) {
209*4882a593Smuzhiyun 			bno = frag->rm.rm_startblock + frag->rm.rm_blockcount;
210*4882a593Smuzhiyun 			if (bno != rbno) {
211*4882a593Smuzhiyun 				if (bno < next_rbno)
212*4882a593Smuzhiyun 					next_rbno = bno;
213*4882a593Smuzhiyun 				continue;
214*4882a593Smuzhiyun 			}
215*4882a593Smuzhiyun 			list_del(&frag->list);
216*4882a593Smuzhiyun 			kmem_free(frag);
217*4882a593Smuzhiyun 			nr++;
218*4882a593Smuzhiyun 		}
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 		/* Try to add nr rmaps starting at rbno to the worklist. */
221*4882a593Smuzhiyun 		list_for_each_entry_safe(frag, n, &refchk->fragments, list) {
222*4882a593Smuzhiyun 			bno = frag->rm.rm_startblock + frag->rm.rm_blockcount;
223*4882a593Smuzhiyun 			if (frag->rm.rm_startblock != rbno)
224*4882a593Smuzhiyun 				goto done;
225*4882a593Smuzhiyun 			list_move_tail(&frag->list, &worklist);
226*4882a593Smuzhiyun 			if (next_rbno > bno)
227*4882a593Smuzhiyun 				next_rbno = bno;
228*4882a593Smuzhiyun 			nr--;
229*4882a593Smuzhiyun 			if (nr == 0)
230*4882a593Smuzhiyun 				break;
231*4882a593Smuzhiyun 		}
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 		/*
234*4882a593Smuzhiyun 		 * If we get here and nr > 0, this means that we added fewer
235*4882a593Smuzhiyun 		 * items to the worklist than we discarded because the fragment
236*4882a593Smuzhiyun 		 * list ran out of items.  Therefore, we cannot maintain the
237*4882a593Smuzhiyun 		 * required refcount.  Something is wrong, so we're done.
238*4882a593Smuzhiyun 		 */
239*4882a593Smuzhiyun 		if (nr)
240*4882a593Smuzhiyun 			goto done;
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 		rbno = next_rbno;
243*4882a593Smuzhiyun 	}
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	/*
246*4882a593Smuzhiyun 	 * Make sure the last extent we processed ends at or beyond
247*4882a593Smuzhiyun 	 * the end of the refcount extent.
248*4882a593Smuzhiyun 	 */
249*4882a593Smuzhiyun 	if (rbno < refchk->bno + refchk->len)
250*4882a593Smuzhiyun 		goto done;
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun 	/* Actually record us having seen the remaining refcount. */
253*4882a593Smuzhiyun 	refchk->seen = refchk->refcount;
254*4882a593Smuzhiyun done:
255*4882a593Smuzhiyun 	/* Delete fragments and work list. */
256*4882a593Smuzhiyun 	list_for_each_entry_safe(frag, n, &worklist, list) {
257*4882a593Smuzhiyun 		list_del(&frag->list);
258*4882a593Smuzhiyun 		kmem_free(frag);
259*4882a593Smuzhiyun 	}
260*4882a593Smuzhiyun 	list_for_each_entry_safe(frag, n, &refchk->fragments, list) {
261*4882a593Smuzhiyun 		list_del(&frag->list);
262*4882a593Smuzhiyun 		kmem_free(frag);
263*4882a593Smuzhiyun 	}
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun /* Use the rmap entries covering this extent to verify the refcount. */
267*4882a593Smuzhiyun STATIC void
xchk_refcountbt_xref_rmap(struct xfs_scrub * sc,xfs_agblock_t bno,xfs_extlen_t len,xfs_nlink_t refcount)268*4882a593Smuzhiyun xchk_refcountbt_xref_rmap(
269*4882a593Smuzhiyun 	struct xfs_scrub		*sc,
270*4882a593Smuzhiyun 	xfs_agblock_t			bno,
271*4882a593Smuzhiyun 	xfs_extlen_t			len,
272*4882a593Smuzhiyun 	xfs_nlink_t			refcount)
273*4882a593Smuzhiyun {
274*4882a593Smuzhiyun 	struct xchk_refcnt_check	refchk = {
275*4882a593Smuzhiyun 		.sc = sc,
276*4882a593Smuzhiyun 		.bno = bno,
277*4882a593Smuzhiyun 		.len = len,
278*4882a593Smuzhiyun 		.refcount = refcount,
279*4882a593Smuzhiyun 		.seen = 0,
280*4882a593Smuzhiyun 	};
281*4882a593Smuzhiyun 	struct xfs_rmap_irec		low;
282*4882a593Smuzhiyun 	struct xfs_rmap_irec		high;
283*4882a593Smuzhiyun 	struct xchk_refcnt_frag		*frag;
284*4882a593Smuzhiyun 	struct xchk_refcnt_frag		*n;
285*4882a593Smuzhiyun 	int				error;
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
288*4882a593Smuzhiyun 		return;
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	/* Cross-reference with the rmapbt to confirm the refcount. */
291*4882a593Smuzhiyun 	memset(&low, 0, sizeof(low));
292*4882a593Smuzhiyun 	low.rm_startblock = bno;
293*4882a593Smuzhiyun 	memset(&high, 0xFF, sizeof(high));
294*4882a593Smuzhiyun 	high.rm_startblock = bno + len - 1;
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	INIT_LIST_HEAD(&refchk.fragments);
297*4882a593Smuzhiyun 	error = xfs_rmap_query_range(sc->sa.rmap_cur, &low, &high,
298*4882a593Smuzhiyun 			&xchk_refcountbt_rmap_check, &refchk);
299*4882a593Smuzhiyun 	if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
300*4882a593Smuzhiyun 		goto out_free;
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	xchk_refcountbt_process_rmap_fragments(&refchk);
303*4882a593Smuzhiyun 	if (refcount != refchk.seen)
304*4882a593Smuzhiyun 		xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun out_free:
307*4882a593Smuzhiyun 	list_for_each_entry_safe(frag, n, &refchk.fragments, list) {
308*4882a593Smuzhiyun 		list_del(&frag->list);
309*4882a593Smuzhiyun 		kmem_free(frag);
310*4882a593Smuzhiyun 	}
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun /* Cross-reference with the other btrees. */
314*4882a593Smuzhiyun STATIC void
xchk_refcountbt_xref(struct xfs_scrub * sc,xfs_agblock_t agbno,xfs_extlen_t len,xfs_nlink_t refcount)315*4882a593Smuzhiyun xchk_refcountbt_xref(
316*4882a593Smuzhiyun 	struct xfs_scrub	*sc,
317*4882a593Smuzhiyun 	xfs_agblock_t		agbno,
318*4882a593Smuzhiyun 	xfs_extlen_t		len,
319*4882a593Smuzhiyun 	xfs_nlink_t		refcount)
320*4882a593Smuzhiyun {
321*4882a593Smuzhiyun 	if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
322*4882a593Smuzhiyun 		return;
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	xchk_xref_is_used_space(sc, agbno, len);
325*4882a593Smuzhiyun 	xchk_xref_is_not_inode_chunk(sc, agbno, len);
326*4882a593Smuzhiyun 	xchk_refcountbt_xref_rmap(sc, agbno, len, refcount);
327*4882a593Smuzhiyun }
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun /* Scrub a refcountbt record. */
330*4882a593Smuzhiyun STATIC int
xchk_refcountbt_rec(struct xchk_btree * bs,union xfs_btree_rec * rec)331*4882a593Smuzhiyun xchk_refcountbt_rec(
332*4882a593Smuzhiyun 	struct xchk_btree	*bs,
333*4882a593Smuzhiyun 	union xfs_btree_rec	*rec)
334*4882a593Smuzhiyun {
335*4882a593Smuzhiyun 	struct xfs_mount	*mp = bs->cur->bc_mp;
336*4882a593Smuzhiyun 	xfs_agblock_t		*cow_blocks = bs->private;
337*4882a593Smuzhiyun 	xfs_agnumber_t		agno = bs->cur->bc_ag.agno;
338*4882a593Smuzhiyun 	xfs_agblock_t		bno;
339*4882a593Smuzhiyun 	xfs_extlen_t		len;
340*4882a593Smuzhiyun 	xfs_nlink_t		refcount;
341*4882a593Smuzhiyun 	bool			has_cowflag;
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun 	bno = be32_to_cpu(rec->refc.rc_startblock);
344*4882a593Smuzhiyun 	len = be32_to_cpu(rec->refc.rc_blockcount);
345*4882a593Smuzhiyun 	refcount = be32_to_cpu(rec->refc.rc_refcount);
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	/* Only CoW records can have refcount == 1. */
348*4882a593Smuzhiyun 	has_cowflag = (bno & XFS_REFC_COW_START);
349*4882a593Smuzhiyun 	if ((refcount == 1 && !has_cowflag) || (refcount != 1 && has_cowflag))
350*4882a593Smuzhiyun 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
351*4882a593Smuzhiyun 	if (has_cowflag)
352*4882a593Smuzhiyun 		(*cow_blocks) += len;
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 	/* Check the extent. */
355*4882a593Smuzhiyun 	bno &= ~XFS_REFC_COW_START;
356*4882a593Smuzhiyun 	if (bno + len <= bno ||
357*4882a593Smuzhiyun 	    !xfs_verify_agbno(mp, agno, bno) ||
358*4882a593Smuzhiyun 	    !xfs_verify_agbno(mp, agno, bno + len - 1))
359*4882a593Smuzhiyun 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 	if (refcount == 0)
362*4882a593Smuzhiyun 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 	xchk_refcountbt_xref(bs->sc, bno, len, refcount);
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	return 0;
367*4882a593Smuzhiyun }
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun /* Make sure we have as many refc blocks as the rmap says. */
370*4882a593Smuzhiyun STATIC void
xchk_refcount_xref_rmap(struct xfs_scrub * sc,xfs_filblks_t cow_blocks)371*4882a593Smuzhiyun xchk_refcount_xref_rmap(
372*4882a593Smuzhiyun 	struct xfs_scrub	*sc,
373*4882a593Smuzhiyun 	xfs_filblks_t		cow_blocks)
374*4882a593Smuzhiyun {
375*4882a593Smuzhiyun 	xfs_extlen_t		refcbt_blocks = 0;
376*4882a593Smuzhiyun 	xfs_filblks_t		blocks;
377*4882a593Smuzhiyun 	int			error;
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 	if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
380*4882a593Smuzhiyun 		return;
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 	/* Check that we saw as many refcbt blocks as the rmap knows about. */
383*4882a593Smuzhiyun 	error = xfs_btree_count_blocks(sc->sa.refc_cur, &refcbt_blocks);
384*4882a593Smuzhiyun 	if (!xchk_btree_process_error(sc, sc->sa.refc_cur, 0, &error))
385*4882a593Smuzhiyun 		return;
386*4882a593Smuzhiyun 	error = xchk_count_rmap_ownedby_ag(sc, sc->sa.rmap_cur,
387*4882a593Smuzhiyun 			&XFS_RMAP_OINFO_REFC, &blocks);
388*4882a593Smuzhiyun 	if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
389*4882a593Smuzhiyun 		return;
390*4882a593Smuzhiyun 	if (blocks != refcbt_blocks)
391*4882a593Smuzhiyun 		xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun 	/* Check that we saw as many cow blocks as the rmap knows about. */
394*4882a593Smuzhiyun 	error = xchk_count_rmap_ownedby_ag(sc, sc->sa.rmap_cur,
395*4882a593Smuzhiyun 			&XFS_RMAP_OINFO_COW, &blocks);
396*4882a593Smuzhiyun 	if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
397*4882a593Smuzhiyun 		return;
398*4882a593Smuzhiyun 	if (blocks != cow_blocks)
399*4882a593Smuzhiyun 		xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
400*4882a593Smuzhiyun }
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun /* Scrub the refcount btree for some AG. */
403*4882a593Smuzhiyun int
xchk_refcountbt(struct xfs_scrub * sc)404*4882a593Smuzhiyun xchk_refcountbt(
405*4882a593Smuzhiyun 	struct xfs_scrub	*sc)
406*4882a593Smuzhiyun {
407*4882a593Smuzhiyun 	xfs_agblock_t		cow_blocks = 0;
408*4882a593Smuzhiyun 	int			error;
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun 	error = xchk_btree(sc, sc->sa.refc_cur, xchk_refcountbt_rec,
411*4882a593Smuzhiyun 			&XFS_RMAP_OINFO_REFC, &cow_blocks);
412*4882a593Smuzhiyun 	if (error)
413*4882a593Smuzhiyun 		return error;
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun 	xchk_refcount_xref_rmap(sc, cow_blocks);
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	return 0;
418*4882a593Smuzhiyun }
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun /* xref check that a cow staging extent is marked in the refcountbt. */
421*4882a593Smuzhiyun void
xchk_xref_is_cow_staging(struct xfs_scrub * sc,xfs_agblock_t agbno,xfs_extlen_t len)422*4882a593Smuzhiyun xchk_xref_is_cow_staging(
423*4882a593Smuzhiyun 	struct xfs_scrub		*sc,
424*4882a593Smuzhiyun 	xfs_agblock_t			agbno,
425*4882a593Smuzhiyun 	xfs_extlen_t			len)
426*4882a593Smuzhiyun {
427*4882a593Smuzhiyun 	struct xfs_refcount_irec	rc;
428*4882a593Smuzhiyun 	bool				has_cowflag;
429*4882a593Smuzhiyun 	int				has_refcount;
430*4882a593Smuzhiyun 	int				error;
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 	if (!sc->sa.refc_cur || xchk_skip_xref(sc->sm))
433*4882a593Smuzhiyun 		return;
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun 	/* Find the CoW staging extent. */
436*4882a593Smuzhiyun 	error = xfs_refcount_lookup_le(sc->sa.refc_cur,
437*4882a593Smuzhiyun 			agbno + XFS_REFC_COW_START, &has_refcount);
438*4882a593Smuzhiyun 	if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur))
439*4882a593Smuzhiyun 		return;
440*4882a593Smuzhiyun 	if (!has_refcount) {
441*4882a593Smuzhiyun 		xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0);
442*4882a593Smuzhiyun 		return;
443*4882a593Smuzhiyun 	}
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun 	error = xfs_refcount_get_rec(sc->sa.refc_cur, &rc, &has_refcount);
446*4882a593Smuzhiyun 	if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur))
447*4882a593Smuzhiyun 		return;
448*4882a593Smuzhiyun 	if (!has_refcount) {
449*4882a593Smuzhiyun 		xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0);
450*4882a593Smuzhiyun 		return;
451*4882a593Smuzhiyun 	}
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun 	/* CoW flag must be set, refcount must be 1. */
454*4882a593Smuzhiyun 	has_cowflag = (rc.rc_startblock & XFS_REFC_COW_START);
455*4882a593Smuzhiyun 	if (!has_cowflag || rc.rc_refcount != 1)
456*4882a593Smuzhiyun 		xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0);
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun 	/* Must be at least as long as what was passed in */
459*4882a593Smuzhiyun 	if (rc.rc_blockcount < len)
460*4882a593Smuzhiyun 		xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0);
461*4882a593Smuzhiyun }
462*4882a593Smuzhiyun 
463*4882a593Smuzhiyun /*
464*4882a593Smuzhiyun  * xref check that the extent is not shared.  Only file data blocks
465*4882a593Smuzhiyun  * can have multiple owners.
466*4882a593Smuzhiyun  */
467*4882a593Smuzhiyun void
xchk_xref_is_not_shared(struct xfs_scrub * sc,xfs_agblock_t agbno,xfs_extlen_t len)468*4882a593Smuzhiyun xchk_xref_is_not_shared(
469*4882a593Smuzhiyun 	struct xfs_scrub	*sc,
470*4882a593Smuzhiyun 	xfs_agblock_t		agbno,
471*4882a593Smuzhiyun 	xfs_extlen_t		len)
472*4882a593Smuzhiyun {
473*4882a593Smuzhiyun 	bool			shared;
474*4882a593Smuzhiyun 	int			error;
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun 	if (!sc->sa.refc_cur || xchk_skip_xref(sc->sm))
477*4882a593Smuzhiyun 		return;
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun 	error = xfs_refcount_has_record(sc->sa.refc_cur, agbno, len, &shared);
480*4882a593Smuzhiyun 	if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur))
481*4882a593Smuzhiyun 		return;
482*4882a593Smuzhiyun 	if (shared)
483*4882a593Smuzhiyun 		xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0);
484*4882a593Smuzhiyun }
485