]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
xfs: replace xfs_btree_has_record with a general keyspace scanner
authorDarrick J. Wong <djwong@kernel.org>
Wed, 31 May 2023 09:07:21 +0000 (11:07 +0200)
committerCarlos Maiolino <cem@kernel.org>
Fri, 9 Jun 2023 08:27:50 +0000 (10:27 +0200)
Source kernel commit: 6abc7aef85b1f42cb39a3149f4ab64ca255e41e6

The current implementation of xfs_btree_has_record returns true if it
finds /any/ record within the given range.  Unfortunately, that's not
sufficient for scrub.  We want to be able to tell if a range of keyspace
for a btree is devoid of records, is totally mapped to records, or is
somewhere in between.  By forcing this to be a boolean, we conflated
sparseness and fullness, which caused scrub to return incorrect results.
Fix the API so that we can tell the caller which of those three is the
current state.

Signed-off-by: Darrick J. Wong <djwong@kernel.org>
Reviewed-by: Dave Chinner <dchinner@redhat.com>
Signed-off-by: Carlos Maiolino <cem@kernel.org>
14 files changed:
libxfs/xfs_alloc.c
libxfs/xfs_alloc.h
libxfs/xfs_alloc_btree.c
libxfs/xfs_bmap_btree.c
libxfs/xfs_btree.c
libxfs/xfs_btree.h
libxfs/xfs_ialloc_btree.c
libxfs/xfs_refcount.c
libxfs/xfs_refcount.h
libxfs/xfs_refcount_btree.c
libxfs/xfs_rmap.c
libxfs/xfs_rmap.h
libxfs/xfs_rmap_btree.c
libxfs/xfs_types.h

index eeac7e8ae162823b6de7040022b8d5ac1c7cf1b4..f696494afe5a43cbba7b66c0de7960903e87a284 100644 (file)
@@ -3741,13 +3741,16 @@ xfs_alloc_query_all(
        return xfs_btree_query_all(cur, xfs_alloc_query_range_helper, &query);
 }
 
-/* Is there a record covering a given extent? */
+/*
+ * Scan part of the keyspace of the free space and tell us if the area has no
+ * records, is fully mapped by records, or is partially filled.
+ */
 int
-xfs_alloc_has_record(
+xfs_alloc_has_records(
        struct xfs_btree_cur    *cur,
        xfs_agblock_t           bno,
        xfs_extlen_t            len,
-       bool                    *exists)
+       enum xbtree_recpacking  *outcome)
 {
        union xfs_btree_irec    low;
        union xfs_btree_irec    high;
@@ -3757,7 +3760,7 @@ xfs_alloc_has_record(
        memset(&high, 0xFF, sizeof(high));
        high.a.ar_startblock = bno + len - 1;
 
-       return xfs_btree_has_record(cur, &low, &high, exists);
+       return xfs_btree_has_records(cur, &low, &high, outcome);
 }
 
 /*
index 56bd05900b3549b0d4105196af7c87e19f4c79b1..5dbb25546d0b1ec9f269da26b16fe90787d429c1 100644 (file)
@@ -213,8 +213,8 @@ int xfs_alloc_query_range(struct xfs_btree_cur *cur,
 int xfs_alloc_query_all(struct xfs_btree_cur *cur, xfs_alloc_query_range_fn fn,
                void *priv);
 
-int xfs_alloc_has_record(struct xfs_btree_cur *cur, xfs_agblock_t bno,
-               xfs_extlen_t len, bool *exist);
+int xfs_alloc_has_records(struct xfs_btree_cur *cur, xfs_agblock_t bno,
+               xfs_extlen_t len, enum xbtree_recpacking *outcome);
 
 typedef int (*xfs_agfl_walk_fn)(struct xfs_mount *mp, xfs_agblock_t bno,
                void *priv);
index 99844b78afe143ce53c04ea67f2ae22f33ccbef7..9c4521ffc91f0017ed5ac650c396df28d73dc02a 100644 (file)
@@ -421,6 +421,16 @@ xfs_cntbt_recs_inorder(
                 be32_to_cpu(r2->alloc.ar_startblock));
 }
 
+STATIC enum xbtree_key_contig
+xfs_allocbt_keys_contiguous(
+       struct xfs_btree_cur            *cur,
+       const union xfs_btree_key       *key1,
+       const union xfs_btree_key       *key2)
+{
+       return xbtree_key_contig(be32_to_cpu(key1->alloc.ar_startblock),
+                                be32_to_cpu(key2->alloc.ar_startblock));
+}
+
 static const struct xfs_btree_ops xfs_bnobt_ops = {
        .rec_len                = sizeof(xfs_alloc_rec_t),
        .key_len                = sizeof(xfs_alloc_key_t),
@@ -441,6 +451,7 @@ static const struct xfs_btree_ops xfs_bnobt_ops = {
        .diff_two_keys          = xfs_bnobt_diff_two_keys,
        .keys_inorder           = xfs_bnobt_keys_inorder,
        .recs_inorder           = xfs_bnobt_recs_inorder,
+       .keys_contiguous        = xfs_allocbt_keys_contiguous,
 };
 
 static const struct xfs_btree_ops xfs_cntbt_ops = {
@@ -463,6 +474,7 @@ static const struct xfs_btree_ops xfs_cntbt_ops = {
        .diff_two_keys          = xfs_cntbt_diff_two_keys,
        .keys_inorder           = xfs_cntbt_keys_inorder,
        .recs_inorder           = xfs_cntbt_recs_inorder,
+       .keys_contiguous        = NULL, /* not needed right now */
 };
 
 /* Allocate most of a new allocation btree cursor. */
index beecf8fcbe4de67660ef77fe0c0397eefa4aca94..1e33d3ea1be3778159aa9e9369e8a110594a2c59 100644 (file)
@@ -498,6 +498,16 @@ xfs_bmbt_recs_inorder(
                xfs_bmbt_disk_get_startoff(&r2->bmbt);
 }
 
+STATIC enum xbtree_key_contig
+xfs_bmbt_keys_contiguous(
+       struct xfs_btree_cur            *cur,
+       const union xfs_btree_key       *key1,
+       const union xfs_btree_key       *key2)
+{
+       return xbtree_key_contig(be64_to_cpu(key1->bmbt.br_startoff),
+                                be64_to_cpu(key2->bmbt.br_startoff));
+}
+
 static const struct xfs_btree_ops xfs_bmbt_ops = {
        .rec_len                = sizeof(xfs_bmbt_rec_t),
        .key_len                = sizeof(xfs_bmbt_key_t),
@@ -518,6 +528,7 @@ static const struct xfs_btree_ops xfs_bmbt_ops = {
        .buf_ops                = &xfs_bmbt_buf_ops,
        .keys_inorder           = xfs_bmbt_keys_inorder,
        .recs_inorder           = xfs_bmbt_recs_inorder,
+       .keys_contiguous        = xfs_bmbt_keys_contiguous,
 };
 
 /*
index 0e5edd2e5950381acd0ef47195cbbe6a9bc7122b..83acb9f3a4ba4b9d8248c68d9cf10c6ec3732006 100644 (file)
@@ -5022,34 +5022,116 @@ xfs_btree_diff_two_ptrs(
        return (int64_t)be32_to_cpu(a->s) - be32_to_cpu(b->s);
 }
 
-/* If there's an extent, we're done. */
+struct xfs_btree_has_records {
+       /* Keys for the start and end of the range we want to know about. */
+       union xfs_btree_key             start_key;
+       union xfs_btree_key             end_key;
+
+       /* Highest record key we've seen so far. */
+       union xfs_btree_key             high_key;
+
+       enum xbtree_recpacking          outcome;
+};
+
 STATIC int
-xfs_btree_has_record_helper(
+xfs_btree_has_records_helper(
        struct xfs_btree_cur            *cur,
        const union xfs_btree_rec       *rec,
        void                            *priv)
 {
-       return -ECANCELED;
+       union xfs_btree_key             rec_key;
+       union xfs_btree_key             rec_high_key;
+       struct xfs_btree_has_records    *info = priv;
+       enum xbtree_key_contig          key_contig;
+
+       cur->bc_ops->init_key_from_rec(&rec_key, rec);
+
+       if (info->outcome == XBTREE_RECPACKING_EMPTY) {
+               info->outcome = XBTREE_RECPACKING_SPARSE;
+
+               /*
+                * If the first record we find does not overlap the start key,
+                * then there is a hole at the start of the search range.
+                * Classify this as sparse and stop immediately.
+                */
+               if (xfs_btree_keycmp_lt(cur, &info->start_key, &rec_key))
+                       return -ECANCELED;
+       } else {
+               /*
+                * If a subsequent record does not overlap with the any record
+                * we've seen so far, there is a hole in the middle of the
+                * search range.  Classify this as sparse and stop.
+                * If the keys overlap and this btree does not allow overlap,
+                * signal corruption.
+                */
+               key_contig = cur->bc_ops->keys_contiguous(cur, &info->high_key,
+                                       &rec_key);
+               if (key_contig == XBTREE_KEY_OVERLAP &&
+                               !(cur->bc_flags & XFS_BTREE_OVERLAPPING))
+                       return -EFSCORRUPTED;
+               if (key_contig == XBTREE_KEY_GAP)
+                       return -ECANCELED;
+       }
+
+       /*
+        * If high_key(rec) is larger than any other high key we've seen,
+        * remember it for later.
+        */
+       cur->bc_ops->init_high_key_from_rec(&rec_high_key, rec);
+       if (xfs_btree_keycmp_gt(cur, &rec_high_key, &info->high_key))
+               info->high_key = rec_high_key; /* struct copy */
+
+       return 0;
 }
 
-/* Is there a record covering a given range of keys? */
+/*
+ * Scan part of the keyspace of a btree and tell us if that keyspace does not
+ * map to any records; is fully mapped to records; or is partially mapped to
+ * records.  This is the btree record equivalent to determining if a file is
+ * sparse.
+ */
 int
-xfs_btree_has_record(
+xfs_btree_has_records(
        struct xfs_btree_cur            *cur,
        const union xfs_btree_irec      *low,
        const union xfs_btree_irec      *high,
-       bool                            *exists)
+       enum xbtree_recpacking          *outcome)
 {
+       struct xfs_btree_has_records    info = {
+               .outcome                = XBTREE_RECPACKING_EMPTY,
+       };
        int                             error;
 
-       error = xfs_btree_query_range(cur, low, high,
-                       &xfs_btree_has_record_helper, NULL);
-       if (error == -ECANCELED) {
-               *exists = true;
-               return 0;
+       /* Not all btrees support this operation. */
+       if (!cur->bc_ops->keys_contiguous) {
+               ASSERT(0);
+               return -EOPNOTSUPP;
        }
-       *exists = false;
-       return error;
+
+       xfs_btree_key_from_irec(cur, &info.start_key, low);
+       xfs_btree_key_from_irec(cur, &info.end_key, high);
+
+       error = xfs_btree_query_range(cur, low, high,
+                       xfs_btree_has_records_helper, &info);
+       if (error == -ECANCELED)
+               goto out;
+       if (error)
+               return error;
+
+       if (info.outcome == XBTREE_RECPACKING_EMPTY)
+               goto out;
+
+       /*
+        * If the largest high_key(rec) we saw during the walk is greater than
+        * the end of the search range, classify this as full.  Otherwise,
+        * there is a hole at the end of the search range.
+        */
+       if (xfs_btree_keycmp_ge(cur, &info.high_key, &info.end_key))
+               info.outcome = XBTREE_RECPACKING_FULL;
+
+out:
+       *outcome = info.outcome;
+       return 0;
 }
 
 /* Are there more records in this btree? */
index f5aa4b893ee775a96fe360be22725a4ff73303e1..66431f351bb2d621a898460f162d7062c7ed8177 100644 (file)
@@ -90,6 +90,27 @@ uint32_t xfs_btree_magic(int crc, xfs_btnum_t btnum);
 #define XFS_BTREE_STATS_ADD(cur, stat, val)    \
        XFS_STATS_ADD_OFF((cur)->bc_mp, (cur)->bc_statoff + __XBTS_ ## stat, val)
 
+enum xbtree_key_contig {
+       XBTREE_KEY_GAP = 0,
+       XBTREE_KEY_CONTIGUOUS,
+       XBTREE_KEY_OVERLAP,
+};
+
+/*
+ * Decide if these two numeric btree key fields are contiguous, overlapping,
+ * or if there's a gap between them.  @x should be the field from the high
+ * key and @y should be the field from the low key.
+ */
+static inline enum xbtree_key_contig xbtree_key_contig(uint64_t x, uint64_t y)
+{
+       x++;
+       if (x < y)
+               return XBTREE_KEY_GAP;
+       if (x == y)
+               return XBTREE_KEY_CONTIGUOUS;
+       return XBTREE_KEY_OVERLAP;
+}
+
 struct xfs_btree_ops {
        /* size of the key and record structures */
        size_t  key_len;
@@ -157,6 +178,19 @@ struct xfs_btree_ops {
        int     (*recs_inorder)(struct xfs_btree_cur *cur,
                                const union xfs_btree_rec *r1,
                                const union xfs_btree_rec *r2);
+
+       /*
+        * Are these two btree keys immediately adjacent?
+        *
+        * Given two btree keys @key1 and @key2, decide if it is impossible for
+        * there to be a third btree key K satisfying the relationship
+        * @key1 < K < @key2.  To determine if two btree records are
+        * immediately adjacent, @key1 should be the high key of the first
+        * record and @key2 should be the low key of the second record.
+        */
+       enum xbtree_key_contig (*keys_contiguous)(struct xfs_btree_cur *cur,
+                              const union xfs_btree_key *key1,
+                              const union xfs_btree_key *key2);
 };
 
 /*
@@ -540,9 +574,15 @@ void xfs_btree_get_keys(struct xfs_btree_cur *cur,
                struct xfs_btree_block *block, union xfs_btree_key *key);
 union xfs_btree_key *xfs_btree_high_key_from_key(struct xfs_btree_cur *cur,
                union xfs_btree_key *key);
-int xfs_btree_has_record(struct xfs_btree_cur *cur,
+typedef bool (*xfs_btree_key_gap_fn)(struct xfs_btree_cur *cur,
+               const union xfs_btree_key *key1,
+               const union xfs_btree_key *key2);
+
+int xfs_btree_has_records(struct xfs_btree_cur *cur,
                const union xfs_btree_irec *low,
-               const union xfs_btree_irec *high, bool *exists);
+               const union xfs_btree_irec *high,
+               enum xbtree_recpacking *outcome);
+
 bool xfs_btree_has_more_records(struct xfs_btree_cur *cur);
 struct xfs_ifork *xfs_btree_ifork_ptr(struct xfs_btree_cur *cur);
 
index b399717723afbdeffd6fa9134a75eaffb794880f..f258a8f56aa82b1a5f439e4b1988b17528a91081 100644 (file)
@@ -382,6 +382,16 @@ xfs_inobt_recs_inorder(
                be32_to_cpu(r2->inobt.ir_startino);
 }
 
+STATIC enum xbtree_key_contig
+xfs_inobt_keys_contiguous(
+       struct xfs_btree_cur            *cur,
+       const union xfs_btree_key       *key1,
+       const union xfs_btree_key       *key2)
+{
+       return xbtree_key_contig(be32_to_cpu(key1->inobt.ir_startino),
+                                be32_to_cpu(key2->inobt.ir_startino));
+}
+
 static const struct xfs_btree_ops xfs_inobt_ops = {
        .rec_len                = sizeof(xfs_inobt_rec_t),
        .key_len                = sizeof(xfs_inobt_key_t),
@@ -401,6 +411,7 @@ static const struct xfs_btree_ops xfs_inobt_ops = {
        .diff_two_keys          = xfs_inobt_diff_two_keys,
        .keys_inorder           = xfs_inobt_keys_inorder,
        .recs_inorder           = xfs_inobt_recs_inorder,
+       .keys_contiguous        = xfs_inobt_keys_contiguous,
 };
 
 static const struct xfs_btree_ops xfs_finobt_ops = {
@@ -422,6 +433,7 @@ static const struct xfs_btree_ops xfs_finobt_ops = {
        .diff_two_keys          = xfs_inobt_diff_two_keys,
        .keys_inorder           = xfs_inobt_keys_inorder,
        .recs_inorder           = xfs_inobt_recs_inorder,
+       .keys_contiguous        = xfs_inobt_keys_contiguous,
 };
 
 /*
index c83074d2ec838ce6129d00bff863fc401f0f44f6..d7d2f792fc04f73327d3cf666ff38ab0bf08a467 100644 (file)
@@ -1997,14 +1997,17 @@ out_free:
        return error;
 }
 
-/* Is there a record covering a given extent? */
+/*
+ * Scan part of the keyspace of the refcount records and tell us if the area
+ * has no records, is fully mapped by records, or is partially filled.
+ */
 int
-xfs_refcount_has_record(
+xfs_refcount_has_records(
        struct xfs_btree_cur    *cur,
        enum xfs_refc_domain    domain,
        xfs_agblock_t           bno,
        xfs_extlen_t            len,
-       bool                    *exists)
+       enum xbtree_recpacking  *outcome)
 {
        union xfs_btree_irec    low;
        union xfs_btree_irec    high;
@@ -2015,7 +2018,7 @@ xfs_refcount_has_record(
        high.rc.rc_startblock = bno + len - 1;
        low.rc.rc_domain = high.rc.rc_domain = domain;
 
-       return xfs_btree_has_record(cur, &low, &high, exists);
+       return xfs_btree_has_records(cur, &low, &high, outcome);
 }
 
 int __init
index fc0b58d4c379e63e1c7337ebbb404937b5b8eaeb..783cd89ca1951c634c36efad3cf8168dd4c88923 100644 (file)
@@ -111,9 +111,9 @@ extern int xfs_refcount_recover_cow_leftovers(struct xfs_mount *mp,
  */
 #define XFS_REFCOUNT_ITEM_OVERHEAD     32
 
-extern int xfs_refcount_has_record(struct xfs_btree_cur *cur,
+extern int xfs_refcount_has_records(struct xfs_btree_cur *cur,
                enum xfs_refc_domain domain, xfs_agblock_t bno,
-               xfs_extlen_t len, bool *exists);
+               xfs_extlen_t len, enum xbtree_recpacking *outcome);
 union xfs_btree_rec;
 extern void xfs_refcount_btrec_to_irec(const union xfs_btree_rec *rec,
                struct xfs_refcount_irec *irec);
index bb9e3b2fe4359af930803ab11e2148ba9115db58..5da721f3e345098686349e3400c24b213ad194b6 100644 (file)
@@ -299,6 +299,16 @@ xfs_refcountbt_recs_inorder(
                be32_to_cpu(r2->refc.rc_startblock);
 }
 
+STATIC enum xbtree_key_contig
+xfs_refcountbt_keys_contiguous(
+       struct xfs_btree_cur            *cur,
+       const union xfs_btree_key       *key1,
+       const union xfs_btree_key       *key2)
+{
+       return xbtree_key_contig(be32_to_cpu(key1->refc.rc_startblock),
+                                be32_to_cpu(key2->refc.rc_startblock));
+}
+
 static const struct xfs_btree_ops xfs_refcountbt_ops = {
        .rec_len                = sizeof(struct xfs_refcount_rec),
        .key_len                = sizeof(struct xfs_refcount_key),
@@ -318,6 +328,7 @@ static const struct xfs_btree_ops xfs_refcountbt_ops = {
        .diff_two_keys          = xfs_refcountbt_diff_two_keys,
        .keys_inorder           = xfs_refcountbt_keys_inorder,
        .recs_inorder           = xfs_refcountbt_recs_inorder,
+       .keys_contiguous        = xfs_refcountbt_keys_contiguous,
 };
 
 /*
index 2a05cdba8e9549958ef59e738f3e4d4d80cc5729..af30ec51f91663c2f6e944f159df54f74709dfff 100644 (file)
@@ -2708,13 +2708,17 @@ xfs_rmap_compare(
                return 0;
 }
 
-/* Is there a record covering a given extent? */
+/*
+ * Scan the physical storage part of the keyspace of the reverse mapping index
+ * and tell us if the area has no records, is fully mapped by records, or is
+ * partially filled.
+ */
 int
-xfs_rmap_has_record(
+xfs_rmap_has_records(
        struct xfs_btree_cur    *cur,
        xfs_agblock_t           bno,
        xfs_extlen_t            len,
-       bool                    *exists)
+       enum xbtree_recpacking  *outcome)
 {
        union xfs_btree_irec    low;
        union xfs_btree_irec    high;
@@ -2724,7 +2728,7 @@ xfs_rmap_has_record(
        memset(&high, 0xFF, sizeof(high));
        high.r.rm_startblock = bno + len - 1;
 
-       return xfs_btree_has_record(cur, &low, &high, exists);
+       return xfs_btree_has_records(cur, &low, &high, outcome);
 }
 
 /*
index 7fb298bcc15f4492dcdbd73ccb6a89c76681cf73..4cbe50cf522ef3a0247b4a6357db18af4eaa4b1e 100644 (file)
@@ -198,8 +198,8 @@ xfs_failaddr_t xfs_rmap_btrec_to_irec(const union xfs_btree_rec *rec,
 xfs_failaddr_t xfs_rmap_check_irec(struct xfs_btree_cur *cur,
                const struct xfs_rmap_irec *irec);
 
-int xfs_rmap_has_record(struct xfs_btree_cur *cur, xfs_agblock_t bno,
-               xfs_extlen_t len, bool *exists);
+int xfs_rmap_has_records(struct xfs_btree_cur *cur, xfs_agblock_t bno,
+               xfs_extlen_t len, enum xbtree_recpacking *outcome);
 int xfs_rmap_record_exists(struct xfs_btree_cur *cur, xfs_agblock_t bno,
                xfs_extlen_t len, const struct xfs_owner_info *oinfo,
                bool *has_rmap);
index d79ca165fd50b8cca759e2c387dd3c48e9467b08..03e7e3136f97e059137535d28ef3e2f3da827505 100644 (file)
@@ -442,6 +442,21 @@ xfs_rmapbt_recs_inorder(
        return 0;
 }
 
+STATIC enum xbtree_key_contig
+xfs_rmapbt_keys_contiguous(
+       struct xfs_btree_cur            *cur,
+       const union xfs_btree_key       *key1,
+       const union xfs_btree_key       *key2)
+{
+       /*
+        * We only support checking contiguity of the physical space component.
+        * If any callers ever need more specificity than that, they'll have to
+        * implement it here.
+        */
+       return xbtree_key_contig(be32_to_cpu(key1->rmap.rm_startblock),
+                                be32_to_cpu(key2->rmap.rm_startblock));
+}
+
 static const struct xfs_btree_ops xfs_rmapbt_ops = {
        .rec_len                = sizeof(struct xfs_rmap_rec),
        .key_len                = 2 * sizeof(struct xfs_rmap_key),
@@ -461,6 +476,7 @@ static const struct xfs_btree_ops xfs_rmapbt_ops = {
        .diff_two_keys          = xfs_rmapbt_diff_two_keys,
        .keys_inorder           = xfs_rmapbt_keys_inorder,
        .recs_inorder           = xfs_rmapbt_recs_inorder,
+       .keys_contiguous        = xfs_rmapbt_keys_contiguous,
 };
 
 static struct xfs_btree_cur *
index 5ebdda7e10780579592cdb741bf158c0be97a0d2..851220021484177e969ec4b08ae96582d708abcc 100644 (file)
@@ -204,6 +204,18 @@ enum xfs_ag_resv_type {
        XFS_AG_RESV_RMAPBT,
 };
 
+/* Results of scanning a btree keyspace to check occupancy. */
+enum xbtree_recpacking {
+       /* None of the keyspace maps to records. */
+       XBTREE_RECPACKING_EMPTY = 0,
+
+       /* Some, but not all, of the keyspace maps to records. */
+       XBTREE_RECPACKING_SPARSE,
+
+       /* The entire keyspace maps to records. */
+       XBTREE_RECPACKING_FULL,
+};
+
 /*
  * Type verifier functions
  */