summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDarrick J. Wong <djwong@kernel.org>2022-07-14 11:16:09 -0700
committerDarrick J. Wong <djwong@kernel.org>2022-10-14 14:17:25 -0700
commit116c1380fbc615a57d63900c9170477cf12fd40e (patch)
tree9701a87012e978225b3349a9f0556486eb782bfe
parent86f7fb31d47c99ea808a3c8288b044094e92ae4b (diff)
xfs: check reference counts of gaps between rt refcount records
If there's a gap between records in the rt refcount btree, we ought to cross-reference the gap with the rtrmap records to make sure that there aren't any overlapping records for a region that doesn't have any shared ownership. Signed-off-by: Darrick J. Wong <djwong@kernel.org>
-rw-r--r--fs/xfs/scrub/rtrefcount.c81
1 files changed, 80 insertions, 1 deletions
diff --git a/fs/xfs/scrub/rtrefcount.c b/fs/xfs/scrub/rtrefcount.c
index cbd5feddb904..2f6f248a25b9 100644
--- a/fs/xfs/scrub/rtrefcount.c
+++ b/fs/xfs/scrub/rtrefcount.c
@@ -15,6 +15,7 @@
#include "xfs_inode.h"
#include "xfs_rtbitmap.h"
#include "xfs_rtgroup.h"
+#include "xfs_rtalloc.h"
#include "scrub/scrub.h"
#include "scrub/common.h"
#include "scrub/btree.h"
@@ -352,8 +353,14 @@ struct xchk_rtrefcbt_records {
/* Previous refcount record. */
struct xfs_refcount_irec prev_rec;
+ /* The next rtgroup block where we aren't expecting shared extents. */
+ xfs_rgblock_t next_unshared_rgbno;
+
/* Number of CoW blocks we expect. */
xfs_extlen_t cow_blocks;
+
+ /* Was the last record a shared or CoW staging extent? */
+ enum xfs_rcext_domain prev_domain;
};
static inline bool
@@ -394,6 +401,53 @@ xchk_rtrefcountbt_check_mergeable(
memcpy(&rrc->prev_rec, irec, sizeof(struct xfs_refcount_irec));
}
+STATIC int
+xchk_rtrefcountbt_rmap_check_gap(
+ struct xfs_btree_cur *cur,
+ const struct xfs_rmap_irec *rec,
+ void *priv)
+{
+ xfs_rgblock_t *next_bno = priv;
+
+ if (*next_bno != NULLRGBLOCK && rec->rm_startblock < *next_bno)
+ return -ECANCELED;
+
+ *next_bno = rec->rm_startblock + rec->rm_blockcount;
+ return 0;
+}
+
+/*
+ * Make sure that a gap in the reference count records does not correspond to
+ * overlapping records (i.e. shared extents) in the reverse mappings.
+ */
+static inline void
+xchk_rtrefcountbt_xref_gaps(
+ struct xfs_scrub *sc,
+ struct xchk_rtrefcbt_records *rrc,
+ xfs_rtblock_t bno)
+{
+ struct xfs_rmap_irec low;
+ struct xfs_rmap_irec high;
+ xfs_rgblock_t next_bno = NULLRGBLOCK;
+ int error;
+
+ if (bno <= rrc->next_unshared_rgbno || !sc->sr.rmap_cur ||
+ xchk_skip_xref(sc->sm))
+ return;
+
+ memset(&low, 0, sizeof(low));
+ low.rm_startblock = rrc->next_unshared_rgbno;
+ memset(&high, 0xFF, sizeof(high));
+ high.rm_startblock = bno - 1;
+
+ error = xfs_rmap_query_range(sc->sr.rmap_cur, &low, &high,
+ xchk_rtrefcountbt_rmap_check_gap, &next_bno);
+ if (error == -ECANCELED)
+ xchk_btree_xref_set_corrupt(sc, sc->sr.rmap_cur, 0);
+ else
+ xchk_should_check_xref(sc, &error, &sc->sr.rmap_cur);
+}
+
/* Scrub a rtrefcountbt record. */
STATIC int
xchk_rtrefcountbt_rec(
@@ -434,9 +488,26 @@ xchk_rtrefcountbt_rec(
if (irec.rc_refcount == 0)
xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
+ /* Shared records always come before CoW records. */
+ if (irec.rc_domain == XFS_RCDOM_SHARED &&
+ rrc->prev_domain == XFS_RCDOM_COW)
+ xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
+ rrc->prev_domain = irec.rc_domain;
+
xchk_rtrefcountbt_check_mergeable(bs, rrc, &irec);
xchk_rtrefcountbt_xref(bs->sc, &irec);
+ /*
+ * If this is a record for a shared extent, check that all blocks
+ * between the previous record and this one have at most one reverse
+ * mapping.
+ */
+ if (irec.rc_domain == XFS_RCDOM_SHARED) {
+ xchk_rtrefcountbt_xref_gaps(bs->sc, rrc, irec.rc_startblock);
+ rrc->next_unshared_rgbno = irec.rc_startblock +
+ irec.rc_blockcount;
+ }
+
return 0;
}
@@ -481,7 +552,9 @@ xchk_rtrefcountbt(
{
struct xfs_owner_info btree_oinfo;
struct xchk_rtrefcbt_records rrc = {
- .cow_blocks = 0,
+ .cow_blocks = 0,
+ .next_unshared_rgbno = 0,
+ .prev_domain = XFS_RCDOM_SHARED,
};
int error;
@@ -496,6 +569,12 @@ xchk_rtrefcountbt(
if (error)
goto out_unlock;
+ /*
+ * Check that all blocks between the last refcount > 1 record and the
+ * end of the rt volume have at most one reverse mapping.
+ */
+ xchk_rtrefcountbt_xref_gaps(sc, &rrc, sc->mp->m_sb.sb_rblocks);
+
xchk_refcount_xref_rmap(sc, &btree_oinfo, rrc.cow_blocks);
out_unlock: