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