summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDarrick J. Wong <djwong@kernel.org>2022-07-14 11:06:33 -0700
committerDarrick J. Wong <djwong@kernel.org>2022-10-14 14:16:47 -0700
commitd06f57a6fb0b65edb8a49b9bc2c83d1f37f1c839 (patch)
treeef25f1df05aac4c685d5137afa08329d5f08e5c6
parente55600ba69b97153d6410ccedd8c5f1049b701f2 (diff)
xfs: create a shadow rmap btree during rmap repair
Create an in-memory btree of rmap records instead of an array. This enables us to do live record collection instead of freezing the fs. Signed-off-by: Darrick J. Wong <djwong@kernel.org>
-rw-r--r--fs/xfs/libxfs/xfs_rmap.c28
-rw-r--r--fs/xfs/libxfs/xfs_rmap_btree.c157
-rw-r--r--fs/xfs/libxfs/xfs_rmap_btree.h13
-rw-r--r--fs/xfs/scrub/rmap_repair.c259
4 files changed, 376 insertions, 81 deletions
diff --git a/fs/xfs/libxfs/xfs_rmap.c b/fs/xfs/libxfs/xfs_rmap.c
index cf406ff57ce3..a2dfc1e107ab 100644
--- a/fs/xfs/libxfs/xfs_rmap.c
+++ b/fs/xfs/libxfs/xfs_rmap.c
@@ -241,7 +241,10 @@ xfs_rmap_get_rec(
if (irec->rm_blockcount == 0)
goto out_bad_rec;
- if (irec->rm_startblock <= XFS_AGFL_BLOCK(mp)) {
+ if (cur->bc_flags & XFS_BTREE_IN_MEMORY) {
+ if (!xfs_rmapbt_mem_verify_rec(cur, irec))
+ goto out_bad_rec;
+ } else if (irec->rm_startblock <= XFS_AGFL_BLOCK(mp)) {
if (irec->rm_owner != XFS_RMAP_OWN_FS)
goto out_bad_rec;
if (irec->rm_blockcount != XFS_AGFL_BLOCK(mp) + 1)
@@ -265,9 +268,13 @@ xfs_rmap_get_rec(
return 0;
out_bad_rec:
- xfs_warn(mp,
- "Reverse Mapping BTree record corruption in AG %d detected!",
- pag->pag_agno);
+ if (cur->bc_flags & XFS_BTREE_IN_MEMORY)
+ xfs_warn(mp,
+ "In-Memory Reverse Mapping BTree record corruption detected!");
+ else
+ xfs_warn(mp,
+ "Reverse Mapping BTree record corruption in AG %d detected!",
+ pag->pag_agno);
xfs_warn(mp,
"Owner 0x%llx, flags 0x%x, start block 0x%x block count 0x%x",
irec->rm_owner, irec->rm_flags, irec->rm_startblock,
@@ -2366,15 +2373,12 @@ xfs_rmap_map_raw(
{
struct xfs_owner_info oinfo;
- oinfo.oi_owner = rmap->rm_owner;
- oinfo.oi_offset = rmap->rm_offset;
- oinfo.oi_flags = 0;
- if (rmap->rm_flags & XFS_RMAP_ATTR_FORK)
- oinfo.oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
- if (rmap->rm_flags & XFS_RMAP_BMBT_BLOCK)
- oinfo.oi_flags |= XFS_OWNER_INFO_BMBT_BLOCK;
+ xfs_owner_info_pack(&oinfo, rmap->rm_owner, rmap->rm_offset,
+ rmap->rm_flags);
- if (rmap->rm_flags || XFS_RMAP_NON_INODE_OWNER(rmap->rm_owner))
+ if ((rmap->rm_flags & (XFS_RMAP_ATTR_FORK | XFS_RMAP_BMBT_BLOCK |
+ XFS_RMAP_UNWRITTEN)) ||
+ XFS_RMAP_NON_INODE_OWNER(rmap->rm_owner))
return xfs_rmap_map(cur, rmap->rm_startblock,
rmap->rm_blockcount,
rmap->rm_flags & XFS_RMAP_UNWRITTEN,
diff --git a/fs/xfs/libxfs/xfs_rmap_btree.c b/fs/xfs/libxfs/xfs_rmap_btree.c
index cb080479fec2..42f7fe5eeec0 100644
--- a/fs/xfs/libxfs/xfs_rmap_btree.c
+++ b/fs/xfs/libxfs/xfs_rmap_btree.c
@@ -21,6 +21,9 @@
#include "xfs_extent_busy.h"
#include "xfs_ag.h"
#include "xfs_ag_resv.h"
+#include "scrub/xfile.h"
+#include "scrub/xfbtree.h"
+#include "xfs_btree_mem.h"
static struct kmem_cache *xfs_rmapbt_cur_cache;
@@ -567,6 +570,160 @@ xfs_rmapbt_stage_cursor(
return cur;
}
+#ifdef CONFIG_XFS_IN_MEMORY_BTREE
+/*
+ * Validate an in-memory rmap btree block. Callers are allowed to generate an
+ * in-memory btree even if the ondisk feature is not enabled.
+ */
+static xfs_failaddr_t
+xfs_rmapbt_mem_verify(
+ struct xfs_buf *bp)
+{
+ struct xfs_mount *mp = bp->b_mount;
+ struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
+ xfs_failaddr_t fa;
+ unsigned int level;
+
+ if (!xfs_verify_magic(bp, block->bb_magic))
+ return __this_address;
+
+ fa = xfs_btree_sblock_v5hdr_verify(bp);
+ if (fa)
+ return fa;
+
+ level = be16_to_cpu(block->bb_level);
+ if (xfs_has_rmapbt(mp)) {
+ if (level >= mp->m_rmap_maxlevels)
+ return __this_address;
+ } else {
+ if (level >= xfs_rmapbt_maxlevels_ondisk())
+ return __this_address;
+ }
+
+ return xfbtree_sblock_verify(bp,
+ xfs_rmapbt_maxrecs(xfo_to_b(1), level == 0));
+}
+
+static void
+xfs_rmapbt_mem_rw_verify(
+ struct xfs_buf *bp)
+{
+ xfs_failaddr_t fa = xfs_rmapbt_mem_verify(bp);
+
+ if (fa)
+ xfs_verifier_error(bp, -EFSCORRUPTED, fa);
+}
+
+/* skip crc checks on in-memory btrees to save time */
+static const struct xfs_buf_ops xfs_rmapbt_mem_buf_ops = {
+ .name = "xfs_rmapbt_mem",
+ .magic = { 0, cpu_to_be32(XFS_RMAP_CRC_MAGIC) },
+ .verify_read = xfs_rmapbt_mem_rw_verify,
+ .verify_write = xfs_rmapbt_mem_rw_verify,
+ .verify_struct = xfs_rmapbt_mem_verify,
+};
+
+static const struct xfs_btree_ops xfs_rmapbt_mem_ops = {
+ .rec_len = sizeof(struct xfs_rmap_rec),
+ .key_len = 2 * sizeof(struct xfs_rmap_key),
+
+ .dup_cursor = xfbtree_dup_cursor,
+ .set_root = xfbtree_set_root,
+ .alloc_block = xfbtree_alloc_block,
+ .free_block = xfbtree_free_block,
+ .get_minrecs = xfbtree_get_minrecs,
+ .get_maxrecs = xfbtree_get_maxrecs,
+ .init_key_from_rec = xfs_rmapbt_init_key_from_rec,
+ .init_high_key_from_rec = xfs_rmapbt_init_high_key_from_rec,
+ .init_rec_from_cur = xfs_rmapbt_init_rec_from_cur,
+ .init_ptr_from_cur = xfbtree_init_ptr_from_cur,
+ .key_diff = xfs_rmapbt_key_diff,
+ .buf_ops = &xfs_rmapbt_mem_buf_ops,
+ .diff_two_keys = xfs_rmapbt_diff_two_keys,
+ .keys_inorder = xfs_rmapbt_keys_inorder,
+ .recs_inorder = xfs_rmapbt_recs_inorder,
+ .has_key_gap = xfs_rmapbt_has_key_gap,
+ .mask_key = xfs_rmapbt_mask_key,
+};
+
+/* Create a cursor for an in-memory btree. */
+struct xfs_btree_cur *
+xfs_rmapbt_mem_cursor(
+ struct xfs_mount *mp,
+ struct xfs_trans *tp,
+ struct xfs_buf *head_bp,
+ struct xfbtree *xfbtree)
+{
+ struct xfs_btree_cur *cur;
+
+ /* Overlapping btree; 2 keys per pointer. */
+ cur = xfs_btree_alloc_cursor(mp, tp, XFS_BTNUM_RMAP,
+ mp->m_rmap_maxlevels, xfs_rmapbt_cur_cache);
+ cur->bc_flags = XFS_BTREE_CRC_BLOCKS | XFS_BTREE_OVERLAPPING |
+ XFS_BTREE_IN_MEMORY;
+ cur->bc_statoff = XFS_STATS_CALC_INDEX(xs_rmap_2);
+ cur->bc_ops = &xfs_rmapbt_mem_ops;
+ cur->bc_mem.xfbtree = xfbtree;
+ cur->bc_mem.head_bp = head_bp;
+ cur->bc_nlevels = xfs_btree_mem_head_nlevels(head_bp);
+
+ return cur;
+}
+
+/* Create an in-memory rmap btree. */
+int
+xfs_rmapbt_mem_create(
+ struct xfs_mount *mp,
+ xfs_agnumber_t agno,
+ struct xfs_buftarg *target,
+ struct xfbtree **xfbtreep)
+{
+ struct xfbtree_config cfg = {
+ .btree_ops = &xfs_rmapbt_mem_ops,
+ .target = target,
+ .btnum = XFS_BTNUM_RMAP,
+ .owner = agno,
+ };
+
+ return xfbtree_create(mp, &cfg, xfbtreep);
+}
+
+/*
+ * Validate that a rmap record stored in an in-memory btree points somewhere
+ * within the ondisk filesystem.
+ */
+bool
+xfs_rmapbt_mem_verify_rec(
+ struct xfs_btree_cur *cur,
+ const struct xfs_rmap_irec *irec)
+{
+ struct xfs_mount *mp = cur->bc_mp;
+ struct xfs_perag *pag;
+ xfs_agnumber_t agno = cur->bc_mem.xfbtree->owner;
+ bool ret = false;
+
+ if (irec->rm_startblock <= XFS_AGFL_BLOCK(mp)) {
+ if (irec->rm_owner != XFS_RMAP_OWN_FS)
+ return false;
+ return irec->rm_blockcount == XFS_AGFL_BLOCK(mp) + 1;
+ }
+
+ /* check for valid extent range, including overflow */
+ pag = xfs_perag_get(mp, agno);
+ if (!xfs_verify_agbno(pag, irec->rm_startblock))
+ goto out;
+ if (irec->rm_startblock > irec->rm_startblock + irec->rm_blockcount)
+ goto out;
+ if (!xfs_verify_agbno(pag,
+ irec->rm_startblock + irec->rm_blockcount - 1))
+ goto out;
+ ret = true;
+out:
+ xfs_perag_put(pag);
+ return ret;
+}
+#endif /* CONFIG_XFS_IN_MEMORY_BTREE */
+
/*
* Install a new reverse mapping btree root. Caller is responsible for
* invalidating and freeing the old btree blocks.
diff --git a/fs/xfs/libxfs/xfs_rmap_btree.h b/fs/xfs/libxfs/xfs_rmap_btree.h
index 3244715dd111..fb2ddb571984 100644
--- a/fs/xfs/libxfs/xfs_rmap_btree.h
+++ b/fs/xfs/libxfs/xfs_rmap_btree.h
@@ -64,4 +64,17 @@ unsigned int xfs_rmapbt_maxlevels_ondisk(void);
int __init xfs_rmapbt_init_cur_cache(void);
void xfs_rmapbt_destroy_cur_cache(void);
+#ifdef CONFIG_XFS_IN_MEMORY_BTREE
+struct xfbtree;
+struct xfs_btree_cur *xfs_rmapbt_mem_cursor(struct xfs_mount *mp,
+ struct xfs_trans *tp, struct xfs_buf *head_bp,
+ struct xfbtree *xfbtree);
+int xfs_rmapbt_mem_create(struct xfs_mount *mp, xfs_agnumber_t agno,
+ struct xfs_buftarg *target, struct xfbtree **xfbtreep);
+bool xfs_rmapbt_mem_verify_rec(struct xfs_btree_cur *cur,
+ const struct xfs_rmap_irec *irec);
+#else
+# define xfs_rmapbt_mem_verify_rec(cur, irec) (false)
+#endif /* CONFIG_XFS_IN_MEMORY_BTREE */
+
#endif /* __XFS_RMAP_BTREE_H__ */
diff --git a/fs/xfs/scrub/rmap_repair.c b/fs/xfs/scrub/rmap_repair.c
index 833a652b7206..48b84d59c244 100644
--- a/fs/xfs/scrub/rmap_repair.c
+++ b/fs/xfs/scrub/rmap_repair.c
@@ -12,6 +12,7 @@
#include "xfs_defer.h"
#include "xfs_btree.h"
#include "xfs_btree_staging.h"
+#include "xfs_btree_mem.h"
#include "xfs_bit.h"
#include "xfs_log_format.h"
#include "xfs_trans.h"
@@ -36,11 +37,11 @@
#include "scrub/trace.h"
#include "scrub/repair.h"
#include "scrub/bitmap.h"
-#include "scrub/xfarray.h"
#include "scrub/xfile.h"
#include "scrub/iscan.h"
#include "scrub/newbt.h"
#include "scrub/reap.h"
+#include "scrub/xfbtree.h"
/*
* Reverse Mapping Btree Repair
@@ -125,37 +126,38 @@ int
xrep_setup_ag_rmapbt(
struct xfs_scrub *sc)
{
- /* For now this is a placeholder until we land other pieces. */
+ int error;
+
+ error = xfile_create(sc->mp, "rmapbt repair", 0, &sc->xfile);
+ if (error)
+ return error;
+
+ error = xfs_alloc_memory_buftarg(sc->mp, sc->xfile, &sc->xfile_buftarg);
+ if (error)
+ return error;
+
return 0;
}
-/*
- * Packed rmap record. The ATTR/BMBT/UNWRITTEN flags are hidden in the upper
- * bits of offset, just like the on-disk record.
- */
-struct xrep_rmap_extent {
- xfs_agblock_t startblock;
- xfs_extlen_t blockcount;
- uint64_t owner;
- uint64_t offset;
-} __packed;
-
/* Context for collecting rmaps */
struct xrep_rmap {
/* new rmapbt information */
struct xrep_newbt new_btree;
/* rmap records generated from primary metadata */
- struct xfarray *rmap_records;
+ struct xfbtree *rmap_btree;
struct xfs_scrub *sc;
- /* get_records()'s position in the rmap record array. */
- xfarray_idx_t array_cur;
+ /* in-memory btree cursor for the xfs_btree_bload iteration */
+ struct xfs_btree_cur *mcur;
/* inode scan cursor */
struct xchk_iscan iscan;
+ /* Number of non-freespace records found. */
+ unsigned long long nr_records;
+
/* bnobt/cntbt contribution to btreeblks */
xfs_agblock_t freesp_btblocks;
@@ -210,11 +212,6 @@ xrep_rmap_stash(
uint64_t offset,
unsigned int flags)
{
- struct xrep_rmap_extent rre = {
- .startblock = startblock,
- .blockcount = blockcount,
- .owner = owner,
- };
struct xfs_rmap_irec rmap = {
.rm_startblock = startblock,
.rm_blockcount = blockcount,
@@ -223,6 +220,8 @@ xrep_rmap_stash(
.rm_flags = flags,
};
struct xfs_scrub *sc = rr->sc;
+ struct xfs_btree_cur *mcur;
+ struct xfs_buf *mhead_bp;
int error = 0;
if (xchk_should_terminate(sc, &error))
@@ -230,8 +229,21 @@ xrep_rmap_stash(
trace_xrep_rmap_found(sc->mp, sc->sa.pag->pag_agno, &rmap);
- rre.offset = xfs_rmap_irec_offset_pack(&rmap);
- return xfarray_append(rr->rmap_records, &rre);
+ error = xfbtree_head_read_buf(rr->rmap_btree, sc->tp, &mhead_bp);
+ if (error)
+ return error;
+
+ mcur = xfs_rmapbt_mem_cursor(sc->mp, sc->tp, mhead_bp, rr->rmap_btree);
+ error = xfs_rmap_map_raw(mcur, &rmap);
+ xfs_btree_del_cursor(mcur, error);
+ if (error)
+ goto out_cancel;
+
+ return xfbtree_trans_commit(rr->rmap_btree, sc->tp);
+
+out_cancel:
+ xfbtree_trans_cancel(rr->rmap_btree, sc->tp);
+ return error;
}
struct xrep_rmap_stash_run {
@@ -768,6 +780,24 @@ xrep_rmap_find_log_rmaps(
sc->mp->m_sb.sb_logblocks, XFS_RMAP_OWN_LOG, 0, 0);
}
+/* Check and count all the records that we gathered. */
+STATIC int
+xrep_rmap_check_record(
+ struct xfs_btree_cur *cur,
+ const struct xfs_rmap_irec *rec,
+ void *priv)
+{
+ struct xrep_rmap *rr = priv;
+ int error;
+
+ error = xrep_rmap_check_mapping(rr->sc, rec);
+ if (error)
+ return error;
+
+ rr->nr_records++;
+ return 0;
+}
+
/*
* Generate all the reverse-mappings for this AG, a list of the old rmapbt
* blocks, and the new btreeblks count. Figure out if we have enough free
@@ -781,6 +811,8 @@ xrep_rmap_find_rmaps(
struct xfs_scrub *sc = rr->sc;
struct xchk_ag *sa = &sc->sa;
struct xfs_inode *ip;
+ struct xfs_buf *mhead_bp;
+ struct xfs_btree_cur *mcur;
int error;
/* Find all the per-AG metadata. */
@@ -847,7 +879,35 @@ end_agscan:
error = xchk_setup_fs(sc);
if (error)
return error;
- return xchk_perag_lock(sc);
+ error = xchk_perag_lock(sc);
+ if (error)
+ return error;
+
+ /*
+ * Now that we have everything locked again, we need to count the
+ * number of rmap records stashed in the btree. This should reflect
+ * all actively-owned space in the filesystem. At the same time, check
+ * all our records before we start building a new btree, which requires
+ * a bnobt cursor.
+ */
+ error = xfbtree_head_read_buf(rr->rmap_btree, NULL, &mhead_bp);
+ if (error)
+ return error;
+
+ mcur = xfs_rmapbt_mem_cursor(rr->sc->mp, NULL, mhead_bp,
+ rr->rmap_btree);
+ sc->sa.bno_cur = xfs_allocbt_init_cursor(sc->mp, sc->tp, sc->sa.agf_bp,
+ sc->sa.pag, XFS_BTNUM_BNO);
+
+ rr->nr_records = 0;
+ error = xfs_rmap_query_all(mcur, xrep_rmap_check_record, rr);
+
+ xfs_btree_del_cursor(sc->sa.bno_cur, error);
+ sc->sa.bno_cur = NULL;
+ xfs_btree_del_cursor(mcur, error);
+ xfs_buf_relse(mhead_bp);
+
+ return error;
}
/* Section (II): Reserving space for new rmapbt and setting free space bitmap */
@@ -880,7 +940,6 @@ STATIC int
xrep_rmap_try_reserve(
struct xrep_rmap *rr,
struct xfs_btree_cur *rmap_cur,
- uint64_t nr_records,
struct xagb_bitmap *freesp_blocks,
uint64_t *blocks_reserved,
bool *done)
@@ -967,7 +1026,7 @@ xrep_rmap_try_reserve(
/* Compute how many blocks we'll need for all the rmaps. */
error = xfs_btree_bload_compute_geometry(rmap_cur,
- &rr->new_btree.bload, nr_records + freesp_records);
+ &rr->new_btree.bload, rr->nr_records + freesp_records);
if (error)
return error;
@@ -986,16 +1045,13 @@ xrep_rmap_reserve_space(
struct xfs_btree_cur *rmap_cur)
{
struct xagb_bitmap freesp_blocks; /* AGBIT */
- uint64_t nr_records; /* NR */
uint64_t blocks_reserved = 0;
bool done = false;
int error;
- nr_records = xfarray_length(rr->rmap_records);
-
/* Compute how many blocks we'll need for the rmaps collected so far. */
error = xfs_btree_bload_compute_geometry(rmap_cur,
- &rr->new_btree.bload, nr_records);
+ &rr->new_btree.bload, rr->nr_records);
if (error)
return error;
@@ -1012,8 +1068,8 @@ xrep_rmap_reserve_space(
* Finish when we don't need more blocks.
*/
do {
- error = xrep_rmap_try_reserve(rr, rmap_cur, nr_records,
- &freesp_blocks, &blocks_reserved, &done);
+ error = xrep_rmap_try_reserve(rr, rmap_cur, &freesp_blocks,
+ &blocks_reserved, &done);
if (error)
goto out_bitmap;
} while (!done);
@@ -1075,29 +1131,25 @@ xrep_rmap_get_records(
unsigned int nr_wanted,
void *priv)
{
- struct xrep_rmap_extent rec;
- struct xfs_rmap_irec *irec = &cur->bc_rec.r;
struct xrep_rmap *rr = priv;
union xfs_btree_rec *block_rec;
unsigned int loaded;
int error;
for (loaded = 0; loaded < nr_wanted; loaded++, idx++) {
- error = xfarray_load_next(rr->rmap_records, &rr->array_cur,
- &rec);
- if (error)
- return error;
+ int stat = 0;
- irec->rm_startblock = rec.startblock;
- irec->rm_blockcount = rec.blockcount;
- irec->rm_owner = rec.owner;
- error = xfs_rmap_irec_offset_unpack(rec.offset, irec);
+ error = xfs_btree_increment(rr->mcur, 0, &stat);
if (error)
return error;
+ if (!stat)
+ return -EFSCORRUPTED;
- error = xrep_rmap_check_mapping(rr->sc, irec);
+ error = xfs_rmap_get_rec(rr->mcur, &cur->bc_rec.r, &stat);
if (error)
return error;
+ if (!stat)
+ return -EFSCORRUPTED;
block_rec = xfs_btree_rec_addr(cur, idx, block);
cur->bc_ops->init_rec_from_cur(cur, block_rec);
@@ -1161,6 +1213,29 @@ xrep_rmap_alloc_vextent(
return xfs_alloc_vextent(args);
}
+
+/* Count the records in this btree. */
+STATIC int
+xrep_rmap_count_records(
+ struct xfs_btree_cur *cur,
+ unsigned long long *nr)
+{
+ int running = 1;
+ int error;
+
+ *nr = 0;
+
+ error = xfs_btree_goto_left_edge(cur);
+ if (error)
+ return error;
+
+ while (running && !(error = xfs_btree_increment(cur, 0, &running))) {
+ if (running)
+ (*nr)++;
+ }
+
+ return error;
+}
/*
* Use the collected rmap information to stage a new rmap btree. If this is
* successful we'll return with the new btree root information logged to the
@@ -1175,6 +1250,7 @@ xrep_rmap_build_new_tree(
struct xfs_perag *pag = sc->sa.pag;
struct xfs_agf *agf = sc->sa.agf_bp->b_addr;
struct xfs_btree_cur *rmap_cur;
+ struct xfs_buf *mhead_bp;
xfs_fsblock_t fsbno;
int error;
@@ -1210,6 +1286,21 @@ xrep_rmap_build_new_tree(
goto err_cur;
/*
+ * Count the rmapbt records again, because the space reservation
+ * for the rmapbt itself probably added more records to the btree.
+ */
+ error = xfbtree_head_read_buf(rr->rmap_btree, NULL, &mhead_bp);
+ if (error)
+ goto err_cur;
+
+ rr->mcur = xfs_rmapbt_mem_cursor(rr->sc->mp, NULL, mhead_bp,
+ rr->rmap_btree);
+
+ error = xrep_rmap_count_records(rr->mcur, &rr->nr_records);
+ if (error)
+ goto err_mcur;
+
+ /*
* Due to btree slack factors, it's possible for a new btree to be one
* level taller than the old btree. Update the incore btree height so
* that we don't trip the verifiers when writing the new btree blocks
@@ -1218,13 +1309,16 @@ xrep_rmap_build_new_tree(
pag->pagf_alt_levels[XFS_BTNUM_RMAPi] =
rr->new_btree.bload.btree_height;
+ /*
+ * Move the cursor to the left edge of the tree so that the first
+ * increment in ->get_records positions us at the first record.
+ */
+ error = xfs_btree_goto_left_edge(rr->mcur);
+ if (error)
+ goto err_level;
+
/* Add all observed rmap records. */
- rr->array_cur = XFARRAY_CURSOR_INIT;
- sc->sa.bno_cur = xfs_allocbt_init_cursor(sc->mp, sc->tp, sc->sa.agf_bp,
- sc->sa.pag, XFS_BTNUM_BNO);
error = xfs_btree_bload(rmap_cur, &rr->new_btree.bload, rr);
- xfs_btree_del_cursor(sc->sa.bno_cur, error);
- sc->sa.bno_cur = NULL;
if (error)
goto err_level;
@@ -1234,6 +1328,15 @@ xrep_rmap_build_new_tree(
*/
xfs_rmapbt_commit_staged_btree(rmap_cur, sc->tp, sc->sa.agf_bp);
xfs_btree_del_cursor(rmap_cur, 0);
+ xfs_btree_del_cursor(rr->mcur, 0);
+ rr->mcur = NULL;
+ xfs_buf_relse(mhead_bp);
+
+ /*
+ * Now that we've written the new btree to disk, we don't need to keep
+ * updating the in-memory btree. Abort the scan to stop live updates.
+ */
+ xchk_iscan_abort(&rr->iscan);
/* Reset the AGF counters now that we've changed the btree shape. */
error = xrep_rmap_reset_counters(rr);
@@ -1249,6 +1352,9 @@ xrep_rmap_build_new_tree(
err_level:
pag->pagf_alt_levels[XFS_BTNUM_RMAPi] = 0;
+err_mcur:
+ xfs_btree_del_cursor(rr->mcur, error);
+ xfs_buf_relse(mhead_bp);
err_cur:
xfs_btree_del_cursor(rmap_cur, error);
err_newbt:
@@ -1276,6 +1382,28 @@ xrep_rmap_find_freesp(
rec->ar_blockcount);
}
+/* Record the free space we find, as part of cleaning out the btree. */
+STATIC int
+xrep_rmap_find_gaps(
+ struct xfs_btree_cur *cur,
+ const struct xfs_rmap_irec *rec,
+ void *priv)
+{
+ struct xrep_rmap_find_gaps *rfg = priv;
+ int error;
+
+ if (rec->rm_startblock > rfg->next_agbno) {
+ error = xagb_bitmap_set(&rfg->rmap_gaps, rfg->next_agbno,
+ rec->rm_startblock - rfg->next_agbno);
+ if (error)
+ return error;
+ }
+
+ rfg->next_agbno = max_t(xfs_agblock_t, rfg->next_agbno,
+ rec->rm_startblock + rec->rm_blockcount);
+ return 0;
+}
+
/*
* Reap the old rmapbt blocks. Now that the rmapbt is fully rebuilt, we make
* a list of gaps in the rmap records and a list of the extents mentioned in
@@ -1292,30 +1420,23 @@ xrep_rmap_remove_old_tree(
struct xfs_scrub *sc = rr->sc;
struct xfs_agf *agf = sc->sa.agf_bp->b_addr;
struct xfs_perag *pag = sc->sa.pag;
+ struct xfs_btree_cur *mcur;
+ struct xfs_buf *mhead_bp;
xfs_agblock_t agend;
- xfarray_idx_t array_cur;
int error;
xagb_bitmap_init(&rfg.rmap_gaps);
/* Compute free space from the new rmapbt. */
- foreach_xfarray_idx(rr->rmap_records, array_cur) {
- struct xrep_rmap_extent rec;
+ error = xfbtree_head_read_buf(rr->rmap_btree, NULL, &mhead_bp);
+ mcur = xfs_rmapbt_mem_cursor(rr->sc->mp, NULL, mhead_bp,
+ rr->rmap_btree);
- error = xfarray_load(rr->rmap_records, array_cur, &rec);
- if (error)
- goto out_bitmap;
-
- /* Record the free space we find. */
- if (rec.startblock > rfg.next_agbno) {
- error = xagb_bitmap_set(&rfg.rmap_gaps, rfg.next_agbno,
- rec.startblock - rfg.next_agbno);
- if (error)
- goto out_bitmap;
- }
- rfg.next_agbno = max_t(xfs_agblock_t, rfg.next_agbno,
- rec.startblock + rec.blockcount);
- }
+ error = xfs_rmap_query_all(mcur, xrep_rmap_find_gaps, &rfg);
+ xfs_btree_del_cursor(mcur, error);
+ xfs_buf_relse(mhead_bp);
+ if (error)
+ goto out_bitmap;
/* Insert a record for space between the last rmap and EOAG. */
agend = be32_to_cpu(agf->agf_length);
@@ -1377,9 +1498,9 @@ xrep_rmapbt(
return -ENOMEM;
rr->sc = sc;
- /* Set up some storage */
- error = xfarray_create(sc->mp, "rmap records", 0,
- sizeof(struct xrep_rmap_extent), &rr->rmap_records);
+ /* Set up in-memory rmap btree */
+ error = xfs_rmapbt_mem_create(sc->mp, sc->sa.pag->pag_agno,
+ sc->xfile_buftarg, &rr->rmap_btree);
if (error)
goto out_rr;
@@ -1404,7 +1525,7 @@ xrep_rmapbt(
out_records:
xchk_iscan_finish(&rr->iscan);
- xfarray_destroy(rr->rmap_records);
+ xfbtree_destroy(rr->rmap_btree);
out_rr:
kfree(rr);
return error;