Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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