162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (C) 2017-2023 Oracle. All Rights Reserved. 462306a36Sopenharmony_ci * Author: Darrick J. Wong <djwong@kernel.org> 562306a36Sopenharmony_ci */ 662306a36Sopenharmony_ci#include "xfs.h" 762306a36Sopenharmony_ci#include "xfs_fs.h" 862306a36Sopenharmony_ci#include "xfs_shared.h" 962306a36Sopenharmony_ci#include "xfs_format.h" 1062306a36Sopenharmony_ci#include "xfs_trans_resv.h" 1162306a36Sopenharmony_ci#include "xfs_mount.h" 1262306a36Sopenharmony_ci#include "xfs_ag.h" 1362306a36Sopenharmony_ci#include "xfs_btree.h" 1462306a36Sopenharmony_ci#include "xfs_rmap.h" 1562306a36Sopenharmony_ci#include "xfs_refcount.h" 1662306a36Sopenharmony_ci#include "scrub/scrub.h" 1762306a36Sopenharmony_ci#include "scrub/common.h" 1862306a36Sopenharmony_ci#include "scrub/btree.h" 1962306a36Sopenharmony_ci#include "scrub/trace.h" 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_ci/* 2262306a36Sopenharmony_ci * Set us up to scrub reference count btrees. 2362306a36Sopenharmony_ci */ 2462306a36Sopenharmony_ciint 2562306a36Sopenharmony_cixchk_setup_ag_refcountbt( 2662306a36Sopenharmony_ci struct xfs_scrub *sc) 2762306a36Sopenharmony_ci{ 2862306a36Sopenharmony_ci if (xchk_need_intent_drain(sc)) 2962306a36Sopenharmony_ci xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN); 3062306a36Sopenharmony_ci return xchk_setup_ag_btree(sc, false); 3162306a36Sopenharmony_ci} 3262306a36Sopenharmony_ci 3362306a36Sopenharmony_ci/* Reference count btree scrubber. */ 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_ci/* 3662306a36Sopenharmony_ci * Confirming Reference Counts via Reverse Mappings 3762306a36Sopenharmony_ci * 3862306a36Sopenharmony_ci * We want to count the reverse mappings overlapping a refcount record 3962306a36Sopenharmony_ci * (bno, len, refcount), allowing for the possibility that some of the 4062306a36Sopenharmony_ci * overlap may come from smaller adjoining reverse mappings, while some 4162306a36Sopenharmony_ci * comes from single extents which overlap the range entirely. The 4262306a36Sopenharmony_ci * outer loop is as follows: 4362306a36Sopenharmony_ci * 4462306a36Sopenharmony_ci * 1. For all reverse mappings overlapping the refcount extent, 4562306a36Sopenharmony_ci * a. If a given rmap completely overlaps, mark it as seen. 4662306a36Sopenharmony_ci * b. Otherwise, record the fragment (in agbno order) for later 4762306a36Sopenharmony_ci * processing. 4862306a36Sopenharmony_ci * 4962306a36Sopenharmony_ci * Once we've seen all the rmaps, we know that for all blocks in the 5062306a36Sopenharmony_ci * refcount record we want to find $refcount owners and we've already 5162306a36Sopenharmony_ci * visited $seen extents that overlap all the blocks. Therefore, we 5262306a36Sopenharmony_ci * need to find ($refcount - $seen) owners for every block in the 5362306a36Sopenharmony_ci * extent; call that quantity $target_nr. Proceed as follows: 5462306a36Sopenharmony_ci * 5562306a36Sopenharmony_ci * 2. Pull the first $target_nr fragments from the list; all of them 5662306a36Sopenharmony_ci * should start at or before the start of the extent. 5762306a36Sopenharmony_ci * Call this subset of fragments the working set. 5862306a36Sopenharmony_ci * 3. Until there are no more unprocessed fragments, 5962306a36Sopenharmony_ci * a. Find the shortest fragments in the set and remove them. 6062306a36Sopenharmony_ci * b. Note the block number of the end of these fragments. 6162306a36Sopenharmony_ci * c. Pull the same number of fragments from the list. All of these 6262306a36Sopenharmony_ci * fragments should start at the block number recorded in the 6362306a36Sopenharmony_ci * previous step. 6462306a36Sopenharmony_ci * d. Put those fragments in the set. 6562306a36Sopenharmony_ci * 4. Check that there are $target_nr fragments remaining in the list, 6662306a36Sopenharmony_ci * and that they all end at or beyond the end of the refcount extent. 6762306a36Sopenharmony_ci * 6862306a36Sopenharmony_ci * If the refcount is correct, all the check conditions in the algorithm 6962306a36Sopenharmony_ci * should always hold true. If not, the refcount is incorrect. 7062306a36Sopenharmony_ci */ 7162306a36Sopenharmony_cistruct xchk_refcnt_frag { 7262306a36Sopenharmony_ci struct list_head list; 7362306a36Sopenharmony_ci struct xfs_rmap_irec rm; 7462306a36Sopenharmony_ci}; 7562306a36Sopenharmony_ci 7662306a36Sopenharmony_cistruct xchk_refcnt_check { 7762306a36Sopenharmony_ci struct xfs_scrub *sc; 7862306a36Sopenharmony_ci struct list_head fragments; 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_ci /* refcount extent we're examining */ 8162306a36Sopenharmony_ci xfs_agblock_t bno; 8262306a36Sopenharmony_ci xfs_extlen_t len; 8362306a36Sopenharmony_ci xfs_nlink_t refcount; 8462306a36Sopenharmony_ci 8562306a36Sopenharmony_ci /* number of owners seen */ 8662306a36Sopenharmony_ci xfs_nlink_t seen; 8762306a36Sopenharmony_ci}; 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_ci/* 9062306a36Sopenharmony_ci * Decide if the given rmap is large enough that we can redeem it 9162306a36Sopenharmony_ci * towards refcount verification now, or if it's a fragment, in 9262306a36Sopenharmony_ci * which case we'll hang onto it in the hopes that we'll later 9362306a36Sopenharmony_ci * discover that we've collected exactly the correct number of 9462306a36Sopenharmony_ci * fragments as the refcountbt says we should have. 9562306a36Sopenharmony_ci */ 9662306a36Sopenharmony_ciSTATIC int 9762306a36Sopenharmony_cixchk_refcountbt_rmap_check( 9862306a36Sopenharmony_ci struct xfs_btree_cur *cur, 9962306a36Sopenharmony_ci const struct xfs_rmap_irec *rec, 10062306a36Sopenharmony_ci void *priv) 10162306a36Sopenharmony_ci{ 10262306a36Sopenharmony_ci struct xchk_refcnt_check *refchk = priv; 10362306a36Sopenharmony_ci struct xchk_refcnt_frag *frag; 10462306a36Sopenharmony_ci xfs_agblock_t rm_last; 10562306a36Sopenharmony_ci xfs_agblock_t rc_last; 10662306a36Sopenharmony_ci int error = 0; 10762306a36Sopenharmony_ci 10862306a36Sopenharmony_ci if (xchk_should_terminate(refchk->sc, &error)) 10962306a36Sopenharmony_ci return error; 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ci rm_last = rec->rm_startblock + rec->rm_blockcount - 1; 11262306a36Sopenharmony_ci rc_last = refchk->bno + refchk->len - 1; 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_ci /* Confirm that a single-owner refc extent is a CoW stage. */ 11562306a36Sopenharmony_ci if (refchk->refcount == 1 && rec->rm_owner != XFS_RMAP_OWN_COW) { 11662306a36Sopenharmony_ci xchk_btree_xref_set_corrupt(refchk->sc, cur, 0); 11762306a36Sopenharmony_ci return 0; 11862306a36Sopenharmony_ci } 11962306a36Sopenharmony_ci 12062306a36Sopenharmony_ci if (rec->rm_startblock <= refchk->bno && rm_last >= rc_last) { 12162306a36Sopenharmony_ci /* 12262306a36Sopenharmony_ci * The rmap overlaps the refcount record, so we can confirm 12362306a36Sopenharmony_ci * one refcount owner seen. 12462306a36Sopenharmony_ci */ 12562306a36Sopenharmony_ci refchk->seen++; 12662306a36Sopenharmony_ci } else { 12762306a36Sopenharmony_ci /* 12862306a36Sopenharmony_ci * This rmap covers only part of the refcount record, so 12962306a36Sopenharmony_ci * save the fragment for later processing. If the rmapbt 13062306a36Sopenharmony_ci * is healthy each rmap_irec we see will be in agbno order 13162306a36Sopenharmony_ci * so we don't need insertion sort here. 13262306a36Sopenharmony_ci */ 13362306a36Sopenharmony_ci frag = kmalloc(sizeof(struct xchk_refcnt_frag), 13462306a36Sopenharmony_ci XCHK_GFP_FLAGS); 13562306a36Sopenharmony_ci if (!frag) 13662306a36Sopenharmony_ci return -ENOMEM; 13762306a36Sopenharmony_ci memcpy(&frag->rm, rec, sizeof(frag->rm)); 13862306a36Sopenharmony_ci list_add_tail(&frag->list, &refchk->fragments); 13962306a36Sopenharmony_ci } 14062306a36Sopenharmony_ci 14162306a36Sopenharmony_ci return 0; 14262306a36Sopenharmony_ci} 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_ci/* 14562306a36Sopenharmony_ci * Given a bunch of rmap fragments, iterate through them, keeping 14662306a36Sopenharmony_ci * a running tally of the refcount. If this ever deviates from 14762306a36Sopenharmony_ci * what we expect (which is the refcountbt's refcount minus the 14862306a36Sopenharmony_ci * number of extents that totally covered the refcountbt extent), 14962306a36Sopenharmony_ci * we have a refcountbt error. 15062306a36Sopenharmony_ci */ 15162306a36Sopenharmony_ciSTATIC void 15262306a36Sopenharmony_cixchk_refcountbt_process_rmap_fragments( 15362306a36Sopenharmony_ci struct xchk_refcnt_check *refchk) 15462306a36Sopenharmony_ci{ 15562306a36Sopenharmony_ci struct list_head worklist; 15662306a36Sopenharmony_ci struct xchk_refcnt_frag *frag; 15762306a36Sopenharmony_ci struct xchk_refcnt_frag *n; 15862306a36Sopenharmony_ci xfs_agblock_t bno; 15962306a36Sopenharmony_ci xfs_agblock_t rbno; 16062306a36Sopenharmony_ci xfs_agblock_t next_rbno; 16162306a36Sopenharmony_ci xfs_nlink_t nr; 16262306a36Sopenharmony_ci xfs_nlink_t target_nr; 16362306a36Sopenharmony_ci 16462306a36Sopenharmony_ci target_nr = refchk->refcount - refchk->seen; 16562306a36Sopenharmony_ci if (target_nr == 0) 16662306a36Sopenharmony_ci return; 16762306a36Sopenharmony_ci 16862306a36Sopenharmony_ci /* 16962306a36Sopenharmony_ci * There are (refchk->rc.rc_refcount - refchk->nr refcount) 17062306a36Sopenharmony_ci * references we haven't found yet. Pull that many off the 17162306a36Sopenharmony_ci * fragment list and figure out where the smallest rmap ends 17262306a36Sopenharmony_ci * (and therefore the next rmap should start). All the rmaps 17362306a36Sopenharmony_ci * we pull off should start at or before the beginning of the 17462306a36Sopenharmony_ci * refcount record's range. 17562306a36Sopenharmony_ci */ 17662306a36Sopenharmony_ci INIT_LIST_HEAD(&worklist); 17762306a36Sopenharmony_ci rbno = NULLAGBLOCK; 17862306a36Sopenharmony_ci 17962306a36Sopenharmony_ci /* Make sure the fragments actually /are/ in agbno order. */ 18062306a36Sopenharmony_ci bno = 0; 18162306a36Sopenharmony_ci list_for_each_entry(frag, &refchk->fragments, list) { 18262306a36Sopenharmony_ci if (frag->rm.rm_startblock < bno) 18362306a36Sopenharmony_ci goto done; 18462306a36Sopenharmony_ci bno = frag->rm.rm_startblock; 18562306a36Sopenharmony_ci } 18662306a36Sopenharmony_ci 18762306a36Sopenharmony_ci /* 18862306a36Sopenharmony_ci * Find all the rmaps that start at or before the refc extent, 18962306a36Sopenharmony_ci * and put them on the worklist. 19062306a36Sopenharmony_ci */ 19162306a36Sopenharmony_ci nr = 0; 19262306a36Sopenharmony_ci list_for_each_entry_safe(frag, n, &refchk->fragments, list) { 19362306a36Sopenharmony_ci if (frag->rm.rm_startblock > refchk->bno || nr > target_nr) 19462306a36Sopenharmony_ci break; 19562306a36Sopenharmony_ci bno = frag->rm.rm_startblock + frag->rm.rm_blockcount; 19662306a36Sopenharmony_ci if (bno < rbno) 19762306a36Sopenharmony_ci rbno = bno; 19862306a36Sopenharmony_ci list_move_tail(&frag->list, &worklist); 19962306a36Sopenharmony_ci nr++; 20062306a36Sopenharmony_ci } 20162306a36Sopenharmony_ci 20262306a36Sopenharmony_ci /* 20362306a36Sopenharmony_ci * We should have found exactly $target_nr rmap fragments starting 20462306a36Sopenharmony_ci * at or before the refcount extent. 20562306a36Sopenharmony_ci */ 20662306a36Sopenharmony_ci if (nr != target_nr) 20762306a36Sopenharmony_ci goto done; 20862306a36Sopenharmony_ci 20962306a36Sopenharmony_ci while (!list_empty(&refchk->fragments)) { 21062306a36Sopenharmony_ci /* Discard any fragments ending at rbno from the worklist. */ 21162306a36Sopenharmony_ci nr = 0; 21262306a36Sopenharmony_ci next_rbno = NULLAGBLOCK; 21362306a36Sopenharmony_ci list_for_each_entry_safe(frag, n, &worklist, list) { 21462306a36Sopenharmony_ci bno = frag->rm.rm_startblock + frag->rm.rm_blockcount; 21562306a36Sopenharmony_ci if (bno != rbno) { 21662306a36Sopenharmony_ci if (bno < next_rbno) 21762306a36Sopenharmony_ci next_rbno = bno; 21862306a36Sopenharmony_ci continue; 21962306a36Sopenharmony_ci } 22062306a36Sopenharmony_ci list_del(&frag->list); 22162306a36Sopenharmony_ci kfree(frag); 22262306a36Sopenharmony_ci nr++; 22362306a36Sopenharmony_ci } 22462306a36Sopenharmony_ci 22562306a36Sopenharmony_ci /* Try to add nr rmaps starting at rbno to the worklist. */ 22662306a36Sopenharmony_ci list_for_each_entry_safe(frag, n, &refchk->fragments, list) { 22762306a36Sopenharmony_ci bno = frag->rm.rm_startblock + frag->rm.rm_blockcount; 22862306a36Sopenharmony_ci if (frag->rm.rm_startblock != rbno) 22962306a36Sopenharmony_ci goto done; 23062306a36Sopenharmony_ci list_move_tail(&frag->list, &worklist); 23162306a36Sopenharmony_ci if (next_rbno > bno) 23262306a36Sopenharmony_ci next_rbno = bno; 23362306a36Sopenharmony_ci nr--; 23462306a36Sopenharmony_ci if (nr == 0) 23562306a36Sopenharmony_ci break; 23662306a36Sopenharmony_ci } 23762306a36Sopenharmony_ci 23862306a36Sopenharmony_ci /* 23962306a36Sopenharmony_ci * If we get here and nr > 0, this means that we added fewer 24062306a36Sopenharmony_ci * items to the worklist than we discarded because the fragment 24162306a36Sopenharmony_ci * list ran out of items. Therefore, we cannot maintain the 24262306a36Sopenharmony_ci * required refcount. Something is wrong, so we're done. 24362306a36Sopenharmony_ci */ 24462306a36Sopenharmony_ci if (nr) 24562306a36Sopenharmony_ci goto done; 24662306a36Sopenharmony_ci 24762306a36Sopenharmony_ci rbno = next_rbno; 24862306a36Sopenharmony_ci } 24962306a36Sopenharmony_ci 25062306a36Sopenharmony_ci /* 25162306a36Sopenharmony_ci * Make sure the last extent we processed ends at or beyond 25262306a36Sopenharmony_ci * the end of the refcount extent. 25362306a36Sopenharmony_ci */ 25462306a36Sopenharmony_ci if (rbno < refchk->bno + refchk->len) 25562306a36Sopenharmony_ci goto done; 25662306a36Sopenharmony_ci 25762306a36Sopenharmony_ci /* Actually record us having seen the remaining refcount. */ 25862306a36Sopenharmony_ci refchk->seen = refchk->refcount; 25962306a36Sopenharmony_cidone: 26062306a36Sopenharmony_ci /* Delete fragments and work list. */ 26162306a36Sopenharmony_ci list_for_each_entry_safe(frag, n, &worklist, list) { 26262306a36Sopenharmony_ci list_del(&frag->list); 26362306a36Sopenharmony_ci kfree(frag); 26462306a36Sopenharmony_ci } 26562306a36Sopenharmony_ci list_for_each_entry_safe(frag, n, &refchk->fragments, list) { 26662306a36Sopenharmony_ci list_del(&frag->list); 26762306a36Sopenharmony_ci kfree(frag); 26862306a36Sopenharmony_ci } 26962306a36Sopenharmony_ci} 27062306a36Sopenharmony_ci 27162306a36Sopenharmony_ci/* Use the rmap entries covering this extent to verify the refcount. */ 27262306a36Sopenharmony_ciSTATIC void 27362306a36Sopenharmony_cixchk_refcountbt_xref_rmap( 27462306a36Sopenharmony_ci struct xfs_scrub *sc, 27562306a36Sopenharmony_ci const struct xfs_refcount_irec *irec) 27662306a36Sopenharmony_ci{ 27762306a36Sopenharmony_ci struct xchk_refcnt_check refchk = { 27862306a36Sopenharmony_ci .sc = sc, 27962306a36Sopenharmony_ci .bno = irec->rc_startblock, 28062306a36Sopenharmony_ci .len = irec->rc_blockcount, 28162306a36Sopenharmony_ci .refcount = irec->rc_refcount, 28262306a36Sopenharmony_ci .seen = 0, 28362306a36Sopenharmony_ci }; 28462306a36Sopenharmony_ci struct xfs_rmap_irec low; 28562306a36Sopenharmony_ci struct xfs_rmap_irec high; 28662306a36Sopenharmony_ci struct xchk_refcnt_frag *frag; 28762306a36Sopenharmony_ci struct xchk_refcnt_frag *n; 28862306a36Sopenharmony_ci int error; 28962306a36Sopenharmony_ci 29062306a36Sopenharmony_ci if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm)) 29162306a36Sopenharmony_ci return; 29262306a36Sopenharmony_ci 29362306a36Sopenharmony_ci /* Cross-reference with the rmapbt to confirm the refcount. */ 29462306a36Sopenharmony_ci memset(&low, 0, sizeof(low)); 29562306a36Sopenharmony_ci low.rm_startblock = irec->rc_startblock; 29662306a36Sopenharmony_ci memset(&high, 0xFF, sizeof(high)); 29762306a36Sopenharmony_ci high.rm_startblock = irec->rc_startblock + irec->rc_blockcount - 1; 29862306a36Sopenharmony_ci 29962306a36Sopenharmony_ci INIT_LIST_HEAD(&refchk.fragments); 30062306a36Sopenharmony_ci error = xfs_rmap_query_range(sc->sa.rmap_cur, &low, &high, 30162306a36Sopenharmony_ci &xchk_refcountbt_rmap_check, &refchk); 30262306a36Sopenharmony_ci if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur)) 30362306a36Sopenharmony_ci goto out_free; 30462306a36Sopenharmony_ci 30562306a36Sopenharmony_ci xchk_refcountbt_process_rmap_fragments(&refchk); 30662306a36Sopenharmony_ci if (irec->rc_refcount != refchk.seen) { 30762306a36Sopenharmony_ci trace_xchk_refcount_incorrect(sc->sa.pag, irec, refchk.seen); 30862306a36Sopenharmony_ci xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0); 30962306a36Sopenharmony_ci } 31062306a36Sopenharmony_ci 31162306a36Sopenharmony_ciout_free: 31262306a36Sopenharmony_ci list_for_each_entry_safe(frag, n, &refchk.fragments, list) { 31362306a36Sopenharmony_ci list_del(&frag->list); 31462306a36Sopenharmony_ci kfree(frag); 31562306a36Sopenharmony_ci } 31662306a36Sopenharmony_ci} 31762306a36Sopenharmony_ci 31862306a36Sopenharmony_ci/* Cross-reference with the other btrees. */ 31962306a36Sopenharmony_ciSTATIC void 32062306a36Sopenharmony_cixchk_refcountbt_xref( 32162306a36Sopenharmony_ci struct xfs_scrub *sc, 32262306a36Sopenharmony_ci const struct xfs_refcount_irec *irec) 32362306a36Sopenharmony_ci{ 32462306a36Sopenharmony_ci if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) 32562306a36Sopenharmony_ci return; 32662306a36Sopenharmony_ci 32762306a36Sopenharmony_ci xchk_xref_is_used_space(sc, irec->rc_startblock, irec->rc_blockcount); 32862306a36Sopenharmony_ci xchk_xref_is_not_inode_chunk(sc, irec->rc_startblock, 32962306a36Sopenharmony_ci irec->rc_blockcount); 33062306a36Sopenharmony_ci xchk_refcountbt_xref_rmap(sc, irec); 33162306a36Sopenharmony_ci} 33262306a36Sopenharmony_ci 33362306a36Sopenharmony_cistruct xchk_refcbt_records { 33462306a36Sopenharmony_ci /* Previous refcount record. */ 33562306a36Sopenharmony_ci struct xfs_refcount_irec prev_rec; 33662306a36Sopenharmony_ci 33762306a36Sopenharmony_ci /* The next AG block where we aren't expecting shared extents. */ 33862306a36Sopenharmony_ci xfs_agblock_t next_unshared_agbno; 33962306a36Sopenharmony_ci 34062306a36Sopenharmony_ci /* Number of CoW blocks we expect. */ 34162306a36Sopenharmony_ci xfs_agblock_t cow_blocks; 34262306a36Sopenharmony_ci 34362306a36Sopenharmony_ci /* Was the last record a shared or CoW staging extent? */ 34462306a36Sopenharmony_ci enum xfs_refc_domain prev_domain; 34562306a36Sopenharmony_ci}; 34662306a36Sopenharmony_ci 34762306a36Sopenharmony_ciSTATIC int 34862306a36Sopenharmony_cixchk_refcountbt_rmap_check_gap( 34962306a36Sopenharmony_ci struct xfs_btree_cur *cur, 35062306a36Sopenharmony_ci const struct xfs_rmap_irec *rec, 35162306a36Sopenharmony_ci void *priv) 35262306a36Sopenharmony_ci{ 35362306a36Sopenharmony_ci xfs_agblock_t *next_bno = priv; 35462306a36Sopenharmony_ci 35562306a36Sopenharmony_ci if (*next_bno != NULLAGBLOCK && rec->rm_startblock < *next_bno) 35662306a36Sopenharmony_ci return -ECANCELED; 35762306a36Sopenharmony_ci 35862306a36Sopenharmony_ci *next_bno = rec->rm_startblock + rec->rm_blockcount; 35962306a36Sopenharmony_ci return 0; 36062306a36Sopenharmony_ci} 36162306a36Sopenharmony_ci 36262306a36Sopenharmony_ci/* 36362306a36Sopenharmony_ci * Make sure that a gap in the reference count records does not correspond to 36462306a36Sopenharmony_ci * overlapping records (i.e. shared extents) in the reverse mappings. 36562306a36Sopenharmony_ci */ 36662306a36Sopenharmony_cistatic inline void 36762306a36Sopenharmony_cixchk_refcountbt_xref_gaps( 36862306a36Sopenharmony_ci struct xfs_scrub *sc, 36962306a36Sopenharmony_ci struct xchk_refcbt_records *rrc, 37062306a36Sopenharmony_ci xfs_agblock_t bno) 37162306a36Sopenharmony_ci{ 37262306a36Sopenharmony_ci struct xfs_rmap_irec low; 37362306a36Sopenharmony_ci struct xfs_rmap_irec high; 37462306a36Sopenharmony_ci xfs_agblock_t next_bno = NULLAGBLOCK; 37562306a36Sopenharmony_ci int error; 37662306a36Sopenharmony_ci 37762306a36Sopenharmony_ci if (bno <= rrc->next_unshared_agbno || !sc->sa.rmap_cur || 37862306a36Sopenharmony_ci xchk_skip_xref(sc->sm)) 37962306a36Sopenharmony_ci return; 38062306a36Sopenharmony_ci 38162306a36Sopenharmony_ci memset(&low, 0, sizeof(low)); 38262306a36Sopenharmony_ci low.rm_startblock = rrc->next_unshared_agbno; 38362306a36Sopenharmony_ci memset(&high, 0xFF, sizeof(high)); 38462306a36Sopenharmony_ci high.rm_startblock = bno - 1; 38562306a36Sopenharmony_ci 38662306a36Sopenharmony_ci error = xfs_rmap_query_range(sc->sa.rmap_cur, &low, &high, 38762306a36Sopenharmony_ci xchk_refcountbt_rmap_check_gap, &next_bno); 38862306a36Sopenharmony_ci if (error == -ECANCELED) 38962306a36Sopenharmony_ci xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0); 39062306a36Sopenharmony_ci else 39162306a36Sopenharmony_ci xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur); 39262306a36Sopenharmony_ci} 39362306a36Sopenharmony_ci 39462306a36Sopenharmony_cistatic inline bool 39562306a36Sopenharmony_cixchk_refcount_mergeable( 39662306a36Sopenharmony_ci struct xchk_refcbt_records *rrc, 39762306a36Sopenharmony_ci const struct xfs_refcount_irec *r2) 39862306a36Sopenharmony_ci{ 39962306a36Sopenharmony_ci const struct xfs_refcount_irec *r1 = &rrc->prev_rec; 40062306a36Sopenharmony_ci 40162306a36Sopenharmony_ci /* Ignore if prev_rec is not yet initialized. */ 40262306a36Sopenharmony_ci if (r1->rc_blockcount > 0) 40362306a36Sopenharmony_ci return false; 40462306a36Sopenharmony_ci 40562306a36Sopenharmony_ci if (r1->rc_domain != r2->rc_domain) 40662306a36Sopenharmony_ci return false; 40762306a36Sopenharmony_ci if (r1->rc_startblock + r1->rc_blockcount != r2->rc_startblock) 40862306a36Sopenharmony_ci return false; 40962306a36Sopenharmony_ci if (r1->rc_refcount != r2->rc_refcount) 41062306a36Sopenharmony_ci return false; 41162306a36Sopenharmony_ci if ((unsigned long long)r1->rc_blockcount + r2->rc_blockcount > 41262306a36Sopenharmony_ci MAXREFCEXTLEN) 41362306a36Sopenharmony_ci return false; 41462306a36Sopenharmony_ci 41562306a36Sopenharmony_ci return true; 41662306a36Sopenharmony_ci} 41762306a36Sopenharmony_ci 41862306a36Sopenharmony_ci/* Flag failures for records that could be merged. */ 41962306a36Sopenharmony_ciSTATIC void 42062306a36Sopenharmony_cixchk_refcountbt_check_mergeable( 42162306a36Sopenharmony_ci struct xchk_btree *bs, 42262306a36Sopenharmony_ci struct xchk_refcbt_records *rrc, 42362306a36Sopenharmony_ci const struct xfs_refcount_irec *irec) 42462306a36Sopenharmony_ci{ 42562306a36Sopenharmony_ci if (bs->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) 42662306a36Sopenharmony_ci return; 42762306a36Sopenharmony_ci 42862306a36Sopenharmony_ci if (xchk_refcount_mergeable(rrc, irec)) 42962306a36Sopenharmony_ci xchk_btree_set_corrupt(bs->sc, bs->cur, 0); 43062306a36Sopenharmony_ci 43162306a36Sopenharmony_ci memcpy(&rrc->prev_rec, irec, sizeof(struct xfs_refcount_irec)); 43262306a36Sopenharmony_ci} 43362306a36Sopenharmony_ci 43462306a36Sopenharmony_ci/* Scrub a refcountbt record. */ 43562306a36Sopenharmony_ciSTATIC int 43662306a36Sopenharmony_cixchk_refcountbt_rec( 43762306a36Sopenharmony_ci struct xchk_btree *bs, 43862306a36Sopenharmony_ci const union xfs_btree_rec *rec) 43962306a36Sopenharmony_ci{ 44062306a36Sopenharmony_ci struct xfs_refcount_irec irec; 44162306a36Sopenharmony_ci struct xchk_refcbt_records *rrc = bs->private; 44262306a36Sopenharmony_ci 44362306a36Sopenharmony_ci xfs_refcount_btrec_to_irec(rec, &irec); 44462306a36Sopenharmony_ci if (xfs_refcount_check_irec(bs->cur, &irec) != NULL) { 44562306a36Sopenharmony_ci xchk_btree_set_corrupt(bs->sc, bs->cur, 0); 44662306a36Sopenharmony_ci return 0; 44762306a36Sopenharmony_ci } 44862306a36Sopenharmony_ci 44962306a36Sopenharmony_ci if (irec.rc_domain == XFS_REFC_DOMAIN_COW) 45062306a36Sopenharmony_ci rrc->cow_blocks += irec.rc_blockcount; 45162306a36Sopenharmony_ci 45262306a36Sopenharmony_ci /* Shared records always come before CoW records. */ 45362306a36Sopenharmony_ci if (irec.rc_domain == XFS_REFC_DOMAIN_SHARED && 45462306a36Sopenharmony_ci rrc->prev_domain == XFS_REFC_DOMAIN_COW) 45562306a36Sopenharmony_ci xchk_btree_set_corrupt(bs->sc, bs->cur, 0); 45662306a36Sopenharmony_ci rrc->prev_domain = irec.rc_domain; 45762306a36Sopenharmony_ci 45862306a36Sopenharmony_ci xchk_refcountbt_check_mergeable(bs, rrc, &irec); 45962306a36Sopenharmony_ci xchk_refcountbt_xref(bs->sc, &irec); 46062306a36Sopenharmony_ci 46162306a36Sopenharmony_ci /* 46262306a36Sopenharmony_ci * If this is a record for a shared extent, check that all blocks 46362306a36Sopenharmony_ci * between the previous record and this one have at most one reverse 46462306a36Sopenharmony_ci * mapping. 46562306a36Sopenharmony_ci */ 46662306a36Sopenharmony_ci if (irec.rc_domain == XFS_REFC_DOMAIN_SHARED) { 46762306a36Sopenharmony_ci xchk_refcountbt_xref_gaps(bs->sc, rrc, irec.rc_startblock); 46862306a36Sopenharmony_ci rrc->next_unshared_agbno = irec.rc_startblock + 46962306a36Sopenharmony_ci irec.rc_blockcount; 47062306a36Sopenharmony_ci } 47162306a36Sopenharmony_ci 47262306a36Sopenharmony_ci return 0; 47362306a36Sopenharmony_ci} 47462306a36Sopenharmony_ci 47562306a36Sopenharmony_ci/* Make sure we have as many refc blocks as the rmap says. */ 47662306a36Sopenharmony_ciSTATIC void 47762306a36Sopenharmony_cixchk_refcount_xref_rmap( 47862306a36Sopenharmony_ci struct xfs_scrub *sc, 47962306a36Sopenharmony_ci xfs_filblks_t cow_blocks) 48062306a36Sopenharmony_ci{ 48162306a36Sopenharmony_ci xfs_extlen_t refcbt_blocks = 0; 48262306a36Sopenharmony_ci xfs_filblks_t blocks; 48362306a36Sopenharmony_ci int error; 48462306a36Sopenharmony_ci 48562306a36Sopenharmony_ci if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm)) 48662306a36Sopenharmony_ci return; 48762306a36Sopenharmony_ci 48862306a36Sopenharmony_ci /* Check that we saw as many refcbt blocks as the rmap knows about. */ 48962306a36Sopenharmony_ci error = xfs_btree_count_blocks(sc->sa.refc_cur, &refcbt_blocks); 49062306a36Sopenharmony_ci if (!xchk_btree_process_error(sc, sc->sa.refc_cur, 0, &error)) 49162306a36Sopenharmony_ci return; 49262306a36Sopenharmony_ci error = xchk_count_rmap_ownedby_ag(sc, sc->sa.rmap_cur, 49362306a36Sopenharmony_ci &XFS_RMAP_OINFO_REFC, &blocks); 49462306a36Sopenharmony_ci if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur)) 49562306a36Sopenharmony_ci return; 49662306a36Sopenharmony_ci if (blocks != refcbt_blocks) 49762306a36Sopenharmony_ci xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0); 49862306a36Sopenharmony_ci 49962306a36Sopenharmony_ci /* Check that we saw as many cow blocks as the rmap knows about. */ 50062306a36Sopenharmony_ci error = xchk_count_rmap_ownedby_ag(sc, sc->sa.rmap_cur, 50162306a36Sopenharmony_ci &XFS_RMAP_OINFO_COW, &blocks); 50262306a36Sopenharmony_ci if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur)) 50362306a36Sopenharmony_ci return; 50462306a36Sopenharmony_ci if (blocks != cow_blocks) 50562306a36Sopenharmony_ci xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0); 50662306a36Sopenharmony_ci} 50762306a36Sopenharmony_ci 50862306a36Sopenharmony_ci/* Scrub the refcount btree for some AG. */ 50962306a36Sopenharmony_ciint 51062306a36Sopenharmony_cixchk_refcountbt( 51162306a36Sopenharmony_ci struct xfs_scrub *sc) 51262306a36Sopenharmony_ci{ 51362306a36Sopenharmony_ci struct xchk_refcbt_records rrc = { 51462306a36Sopenharmony_ci .cow_blocks = 0, 51562306a36Sopenharmony_ci .next_unshared_agbno = 0, 51662306a36Sopenharmony_ci .prev_domain = XFS_REFC_DOMAIN_SHARED, 51762306a36Sopenharmony_ci }; 51862306a36Sopenharmony_ci int error; 51962306a36Sopenharmony_ci 52062306a36Sopenharmony_ci error = xchk_btree(sc, sc->sa.refc_cur, xchk_refcountbt_rec, 52162306a36Sopenharmony_ci &XFS_RMAP_OINFO_REFC, &rrc); 52262306a36Sopenharmony_ci if (error) 52362306a36Sopenharmony_ci return error; 52462306a36Sopenharmony_ci 52562306a36Sopenharmony_ci /* 52662306a36Sopenharmony_ci * Check that all blocks between the last refcount > 1 record and the 52762306a36Sopenharmony_ci * end of the AG have at most one reverse mapping. 52862306a36Sopenharmony_ci */ 52962306a36Sopenharmony_ci xchk_refcountbt_xref_gaps(sc, &rrc, sc->mp->m_sb.sb_agblocks); 53062306a36Sopenharmony_ci 53162306a36Sopenharmony_ci xchk_refcount_xref_rmap(sc, rrc.cow_blocks); 53262306a36Sopenharmony_ci 53362306a36Sopenharmony_ci return 0; 53462306a36Sopenharmony_ci} 53562306a36Sopenharmony_ci 53662306a36Sopenharmony_ci/* xref check that a cow staging extent is marked in the refcountbt. */ 53762306a36Sopenharmony_civoid 53862306a36Sopenharmony_cixchk_xref_is_cow_staging( 53962306a36Sopenharmony_ci struct xfs_scrub *sc, 54062306a36Sopenharmony_ci xfs_agblock_t agbno, 54162306a36Sopenharmony_ci xfs_extlen_t len) 54262306a36Sopenharmony_ci{ 54362306a36Sopenharmony_ci struct xfs_refcount_irec rc; 54462306a36Sopenharmony_ci int has_refcount; 54562306a36Sopenharmony_ci int error; 54662306a36Sopenharmony_ci 54762306a36Sopenharmony_ci if (!sc->sa.refc_cur || xchk_skip_xref(sc->sm)) 54862306a36Sopenharmony_ci return; 54962306a36Sopenharmony_ci 55062306a36Sopenharmony_ci /* Find the CoW staging extent. */ 55162306a36Sopenharmony_ci error = xfs_refcount_lookup_le(sc->sa.refc_cur, XFS_REFC_DOMAIN_COW, 55262306a36Sopenharmony_ci agbno, &has_refcount); 55362306a36Sopenharmony_ci if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur)) 55462306a36Sopenharmony_ci return; 55562306a36Sopenharmony_ci if (!has_refcount) { 55662306a36Sopenharmony_ci xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0); 55762306a36Sopenharmony_ci return; 55862306a36Sopenharmony_ci } 55962306a36Sopenharmony_ci 56062306a36Sopenharmony_ci error = xfs_refcount_get_rec(sc->sa.refc_cur, &rc, &has_refcount); 56162306a36Sopenharmony_ci if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur)) 56262306a36Sopenharmony_ci return; 56362306a36Sopenharmony_ci if (!has_refcount) { 56462306a36Sopenharmony_ci xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0); 56562306a36Sopenharmony_ci return; 56662306a36Sopenharmony_ci } 56762306a36Sopenharmony_ci 56862306a36Sopenharmony_ci /* CoW lookup returned a shared extent record? */ 56962306a36Sopenharmony_ci if (rc.rc_domain != XFS_REFC_DOMAIN_COW) 57062306a36Sopenharmony_ci xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0); 57162306a36Sopenharmony_ci 57262306a36Sopenharmony_ci /* Must be at least as long as what was passed in */ 57362306a36Sopenharmony_ci if (rc.rc_blockcount < len) 57462306a36Sopenharmony_ci xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0); 57562306a36Sopenharmony_ci} 57662306a36Sopenharmony_ci 57762306a36Sopenharmony_ci/* 57862306a36Sopenharmony_ci * xref check that the extent is not shared. Only file data blocks 57962306a36Sopenharmony_ci * can have multiple owners. 58062306a36Sopenharmony_ci */ 58162306a36Sopenharmony_civoid 58262306a36Sopenharmony_cixchk_xref_is_not_shared( 58362306a36Sopenharmony_ci struct xfs_scrub *sc, 58462306a36Sopenharmony_ci xfs_agblock_t agbno, 58562306a36Sopenharmony_ci xfs_extlen_t len) 58662306a36Sopenharmony_ci{ 58762306a36Sopenharmony_ci enum xbtree_recpacking outcome; 58862306a36Sopenharmony_ci int error; 58962306a36Sopenharmony_ci 59062306a36Sopenharmony_ci if (!sc->sa.refc_cur || xchk_skip_xref(sc->sm)) 59162306a36Sopenharmony_ci return; 59262306a36Sopenharmony_ci 59362306a36Sopenharmony_ci error = xfs_refcount_has_records(sc->sa.refc_cur, 59462306a36Sopenharmony_ci XFS_REFC_DOMAIN_SHARED, agbno, len, &outcome); 59562306a36Sopenharmony_ci if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur)) 59662306a36Sopenharmony_ci return; 59762306a36Sopenharmony_ci if (outcome != XBTREE_RECPACKING_EMPTY) 59862306a36Sopenharmony_ci xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0); 59962306a36Sopenharmony_ci} 60062306a36Sopenharmony_ci 60162306a36Sopenharmony_ci/* xref check that the extent is not being used for CoW staging. */ 60262306a36Sopenharmony_civoid 60362306a36Sopenharmony_cixchk_xref_is_not_cow_staging( 60462306a36Sopenharmony_ci struct xfs_scrub *sc, 60562306a36Sopenharmony_ci xfs_agblock_t agbno, 60662306a36Sopenharmony_ci xfs_extlen_t len) 60762306a36Sopenharmony_ci{ 60862306a36Sopenharmony_ci enum xbtree_recpacking outcome; 60962306a36Sopenharmony_ci int error; 61062306a36Sopenharmony_ci 61162306a36Sopenharmony_ci if (!sc->sa.refc_cur || xchk_skip_xref(sc->sm)) 61262306a36Sopenharmony_ci return; 61362306a36Sopenharmony_ci 61462306a36Sopenharmony_ci error = xfs_refcount_has_records(sc->sa.refc_cur, XFS_REFC_DOMAIN_COW, 61562306a36Sopenharmony_ci agbno, len, &outcome); 61662306a36Sopenharmony_ci if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur)) 61762306a36Sopenharmony_ci return; 61862306a36Sopenharmony_ci if (outcome != XBTREE_RECPACKING_EMPTY) 61962306a36Sopenharmony_ci xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0); 62062306a36Sopenharmony_ci} 621