xfs: dedupe realtime rmap btree code
cover xfs: dedupe realtime rmap btree code
xfs_rmap_btree.c and xfs_rtrmap_btree.c have a large amount of duplicated code, differing only in function names which add "rt" in the latter case. This can all be shared to reduce the cut & paste.
Tested with an auto xfstests run as well as the rmap group with rtdev & rmap enabled.
xfs_rmap_btree.c | 16 +-- xfs_rmap_btree.h | 26 ++++++ xfs_rtrmap_btree.c | 230 +++------------------------------------------------ 3 files changed, 51 insertions(+), 221 deletions(-)
[root@fedora-rawhide xfstests-dev]# ./check -g rmap
FSTYP -- xfs (non-debug)
PLATFORM -- Linux/x86_64 fedora-rawhide 7.3.0-rc3+ #330 SMPPREEMPT_DYNAMIC Fri Sep 18 10:23:43 CDT 2026MKFS_OPTIONS -- -f -rrtdev=/dev/loop0 -m rmapbt=1,reflink=1 /dev/vdb2 MOUNT_OPTIONS -- -o context=system_u:object_r:root_t:s0 -ortdev=/dev/loop0 /dev/vdb2 /mnt/scratch
generic/365 2s ... 2s xfs/114 3s ... 2s xfs/233 1s ... 1s xfs/234 5s ... 5s xfs/235 1s ... 1s xfs/236 6s ... 6s xfs/271 2s ... 2s xfs/272 2s ... 2s xfs/273 4s ... 5s xfs/274 3s ... 3s xfs/275 [not run] This test requires a valid $SCRATCH_LOGDEV xfs/276 3s ... 3s xfs/277 2s ... 2s xfs/310 0s ... 1s xfs/317 [not run] XFS error injection requires CONFIG_XFS_DEBUG xfs/331 2s ... 2s xfs/332 1s ... 1s xfs/334 1s ... 1s xfs/335 4s ... 3s xfs/336 5s ... 5s xfs/337 5s ... 5s xfs/338 2s ... 2s xfs/339 2s ... 2s xfs/340 2s ... 2s xfs/341 1s ... 1s xfs/342 2s ... 2s xfs/343 2s ... 2s xfs/450 1s ... 2s
Ran: generic/365 xfs/114 xfs/233 xfs/234 xfs/235 xfs/236 xfs/271 xfs/272 xfs/273 xfs/274 xfs/275 xfs/276 xfs/277 xfs/310 xfs/317 xfs/331 xfs/332 xfs/334 xfs/335 xfs/336 xfs/337 xfs/338 xfs/339 xfs/340 xfs/341 xfs/342 xfs/343 xfs/450
Not run: xfs/275 xfs/317
Passed all 28 tests
PATCH 1/2 xfs: export several rmap btree key/record ops
The AG rmap btree and the realtime rmap btree have several identical btree key and record ops.
Export the AG variants of these ops so that they can be shared with the realtime code in the next patch, to eliminate this copied code.
--- fs/xfs/libxfs/xfs_rmap_btree.c | 16 ++++++++-------- fs/xfs/libxfs/xfs_rmap_btree.h | 26 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 8 deletions(-)
fs/xfs/libxfs/xfs_rmap_btree.c +8 -8
@@ -170,7 +170,7 @@ static inline __be64 ondisk_rec_offset_to_key(const union xfs_btree_rec *rec)return rec->rmap.rm_offset & ~cpu_to_be64 (XFS_RMAP_OFF_UNWRITTEN ); } -STATIC void +void xfs_rmapbt_init_key_from_rec (union xfs_btree_key *key,const union xfs_btree_rec *rec)
@@ -187,7 +187,7 @@ xfs_rmapbt_init_key_from_rec(* the startblock for all records, and if the record is for a data/attr * fork mapping, we add blockcount-1 to the offset too. */ -STATIC void +void xfs_rmapbt_init_high_key_from_rec (union xfs_btree_key *key,const union xfs_btree_rec *rec)
@@ -209,7 +209,7 @@ xfs_rmapbt_init_high_key_from_rec( key->rmap.rm_offset =cpu_to_be64 (off); } -STATIC void +void xfs_rmapbt_init_rec_from_cur (struct xfs_btree_cur *cur,union xfs_btree_rec *rec)
@@ -243,7 +243,7 @@ static inline uint64_t offset_keymask(uint64_t offset)return offset & ~XFS_RMAP_OFF_UNWRITTEN ; } -STATIC int +int xfs_rmapbt_cmp_key_with_cur (struct xfs_btree_cur *cur,const union xfs_btree_key *key)
@@ -257,7 +257,7 @@ xfs_rmapbt_cmp_key_with_cur( offset_keymask(xfs_rmap_irec_offset_pack(rec))); } -STATIC int +int xfs_rmapbt_cmp_two_keys( struct xfs_btree_cur *cur, const union xfs_btree_key *k1,
@@ -390,7 +390,7 @@ const struct xfs_buf_ops xfs_rmapbt_buf_ops = { .verify_struct = xfs_rmapbt_verify, }; -STATIC int +int xfs_rmapbt_keys_inorder( struct xfs_btree_cur *cur, const union xfs_btree_key *k1,
@@ -420,7 +420,7 @@ xfs_rmapbt_keys_inorder(return 0 ; } -STATIC int +int xfs_rmapbt_recs_inorder( struct xfs_btree_cur *cur, const union xfs_btree_rec *r1,
@@ -450,7 +450,7 @@ xfs_rmapbt_recs_inorder(return 0 ; } -STATIC enum xbtree_key_contig +enum xbtree_key_contig xfs_rmapbt_keys_contiguous( struct xfs_btree_cur *cur, const union xfs_btree_key *key1,
fs/xfs/libxfs/xfs_rmap_btree.h +26 -0
@@ -11,6 +11,8 @@ struct xfs_btree_cur;struct xfs_mount ;struct xbtree_afakeroot ;struct xfbtree ; +union xfs_btree_key ; +union xfs_btree_rec ;/* rmaps only exist on crc enabled filesystems */ #define XFS_RMAP_BLOCK_LEN XFS_BTREE_SBLOCK_CRC_LEN
@@ -69,4 +71,28 @@ struct xfs_btree_cur *xfs_rmapbt_mem_cursor(struct xfs_perag *pag,int xfs_rmapbt_mem_init (struct xfs_mount *mp,struct xfbtree *xfbtree,struct xfs_buftarg *btp,xfs_agnumber_t agno); +/* +* Key and record btree ops. The rmap on-disk key/record format is identical +* for the AG rmap btree and the realtime rmap btree, so these are shared by +* both (see xfs_rtrmap_btree.c). +*/ +void xfs_rmapbt_init_key_from_rec (union xfs_btree_key *key, +const union xfs_btree_rec *rec); +void xfs_rmapbt_init_high_key_from_rec (union xfs_btree_key *key, +const union xfs_btree_rec *rec); +void xfs_rmapbt_init_rec_from_cur (struct xfs_btree_cur *cur, +union xfs_btree_rec *rec); +int xfs_rmapbt_cmp_key_with_cur (struct xfs_btree_cur *cur, +const union xfs_btree_key *key); +int xfs_rmapbt_cmp_two_keys (struct xfs_btree_cur *cur, +const union xfs_btree_key *k1,const union xfs_btree_key *k2, +const union xfs_btree_key *mask); +int xfs_rmapbt_keys_inorder (struct xfs_btree_cur *cur, +const union xfs_btree_key *k1,const union xfs_btree_key *k2); +int xfs_rmapbt_recs_inorder (struct xfs_btree_cur *cur, +const union xfs_btree_rec *r1,const union xfs_btree_rec *r2); +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, +const union xfs_btree_key *mask); +#endif /* __XFS_RMAP_BTREE_H__ */
Signature · 1 line
2.55.0
PATCH 2/2 xfs: use the shared rmap btree ops for the rt rmap btree
In reply to Eric SandeenNow that the (identical but for naming) AG variants of these ops are exported, use them for realtime as well, to eliminate copied code.
--- fs/xfs/libxfs/xfs_rtrmap_btree.c | 230 +++---------------------------- 1 file changed, 17 insertions(+), 213 deletions(-)
fs/xfs/libxfs/xfs_rtrmap_btree.c +17 -213
@@ -20,6 +20,7 @@#include "xfs_btree_staging.h" #include "xfs_metafile.h" #include "xfs_rmap.h" +#include "xfs_rmap_btree.h" #include "xfs_rtrmap_btree.h" #include "xfs_trace.h" #include "xfs_cksum.h"
@@ -113,60 +114,6 @@ xfs_rtrmapbt_get_dmaxrecs(return xfs_rtrmapbt_droot_maxrecs (cur->bc_ino.forksize, level ==0 ); } -/* -* Convert the ondisk record's offset field into the ondisk key's offset field. -* Fork and bmbt are significant parts of the rmap record key, but written -* status is merely a record attribute. -*/ -static inline __be64 ondisk_rec_offset_to_key (const union xfs_btree_rec *rec) -{ -return rec->rmap.rm_offset & ~cpu_to_be64 (XFS_RMAP_OFF_UNWRITTEN ); -} - -STATIC void -xfs_rtrmapbt_init_key_from_rec ( -union xfs_btree_key *key, -const union xfs_btree_rec *rec) -{ - key->rmap.rm_startblock = rec->rmap.rm_startblock; - key->rmap.rm_owner = rec->rmap.rm_owner; - key->rmap.rm_offset =ondisk_rec_offset_to_key (rec); -} - -STATIC void -xfs_rtrmapbt_init_high_key_from_rec ( -union xfs_btree_key *key, -const union xfs_btree_rec *rec) -{ -uint64_t off; -int adj; - - adj =be32_to_cpu (rec->rmap.rm_blockcount) -1 ; - - key->rmap.rm_startblock = rec->rmap.rm_startblock; -be32_add_cpu (&key->rmap.rm_startblock, adj); - key->rmap.rm_owner = rec->rmap.rm_owner; - key->rmap.rm_offset =ondisk_rec_offset_to_key (rec); -if (XFS_RMAP_NON_INODE_OWNER (be64_to_cpu (rec->rmap.rm_owner)) || -XFS_RMAP_IS_BMBT_BLOCK (be64_to_cpu (rec->rmap.rm_offset))) -return ; - off =be64_to_cpu (key->rmap.rm_offset); - off = (XFS_RMAP_OFF (off) + adj) | (off & ~XFS_RMAP_OFF_MASK ); - key->rmap.rm_offset =cpu_to_be64 (off); -} - -STATIC void -xfs_rtrmapbt_init_rec_from_cur ( -struct xfs_btree_cur *cur, -union xfs_btree_rec *rec) -{ - rec->rmap.rm_startblock =cpu_to_be32 (cur->bc_rec.r.rm_startblock); - rec->rmap.rm_blockcount =cpu_to_be32 (cur->bc_rec.r.rm_blockcount); - rec->rmap.rm_owner =cpu_to_be64 (cur->bc_rec.r.rm_owner); - rec->rmap.rm_offset =cpu_to_be64 ( -xfs_rmap_irec_offset_pack (&cur->bc_rec.r)); -} - STATIC void xfs_rtrmapbt_init_ptr_from_cur( struct xfs_btree_cur *cur,
@@ -175,69 +122,6 @@ xfs_rtrmapbt_init_ptr_from_cur( ptr->l =0 ; } -/* -* Mask the appropriate parts of the ondisk key field for a key comparison. -* Fork and bmbt are significant parts of the rmap record key, but written -* status is merely a record attribute. -*/ -static inline uint64_t offset_keymask (uint64_t offset) -{ -return offset & ~XFS_RMAP_OFF_UNWRITTEN ; -} - -STATIC int -xfs_rtrmapbt_cmp_key_with_cur ( -struct xfs_btree_cur *cur, -const union xfs_btree_key *key) -{ -struct xfs_rmap_irec *rec = &cur->bc_rec.r; -const struct xfs_rmap_key *kp = &key->rmap; - -return cmp_int (be32_to_cpu (kp->rm_startblock), rec->rm_startblock) ?: -cmp_int (be64_to_cpu (kp->rm_owner), rec->rm_owner) ?: -cmp_int (offset_keymask (be64_to_cpu (kp->rm_offset)), -offset_keymask (xfs_rmap_irec_offset_pack (rec))); -} - -STATIC int -xfs_rtrmapbt_cmp_two_keys ( -struct xfs_btree_cur *cur, -const union xfs_btree_key *k1, -const union xfs_btree_key *k2, -const union xfs_btree_key *mask) -{ -const struct xfs_rmap_key *kp1 = &k1->rmap; -const struct xfs_rmap_key *kp2 = &k2->rmap; -int d; - -/* Doesn't make sense to mask off the physical space part */ -ASSERT (!mask || mask->rmap.rm_startblock); - - d =cmp_int (be32_to_cpu (kp1->rm_startblock), -be32_to_cpu (kp2->rm_startblock)); -if (d) -return d; - -if (!mask || mask->rmap.rm_owner) { - d =cmp_int (be64_to_cpu (kp1->rm_owner), -be64_to_cpu (kp2->rm_owner)); -if (d) -return d; - } - -if (!mask || mask->rmap.rm_offset) { -/* Doesn't make sense to allow offset but not owner */ -ASSERT (!mask || mask->rmap.rm_owner); - - d =cmp_int (offset_keymask (be64_to_cpu (kp1->rm_offset)), -offset_keymask (be64_to_cpu (kp2->rm_offset))); -if (d) -return d; - } - -return 0 ; -} -static xfs_failaddr_t xfs_rtrmapbt_verify (struct xfs_buf *bp)
@@ -304,86 +188,6 @@ const struct xfs_buf_ops xfs_rtrmapbt_buf_ops = { .verify_struct = xfs_rtrmapbt_verify, }; -STATIC int -xfs_rtrmapbt_keys_inorder ( -struct xfs_btree_cur *cur, -const union xfs_btree_key *k1, -const union xfs_btree_key *k2) -{ -uint32_t x; -uint32_t y; -uint64_t a; -uint64_t b; - - x =be32_to_cpu (k1->rmap.rm_startblock); - y =be32_to_cpu (k2->rmap.rm_startblock); -if (x < y) -return 1 ; -else if (x > y) -return 0 ; - a =be64_to_cpu (k1->rmap.rm_owner); - b =be64_to_cpu (k2->rmap.rm_owner); -if (a < b) -return 1 ; -else if (a > b) -return 0 ; - a =offset_keymask (be64_to_cpu (k1->rmap.rm_offset)); - b =offset_keymask (be64_to_cpu (k2->rmap.rm_offset)); -if (a <= b) -return 1 ; -return 0 ; -} - -STATIC int -xfs_rtrmapbt_recs_inorder ( -struct xfs_btree_cur *cur, -const union xfs_btree_rec *r1, -const union xfs_btree_rec *r2) -{ -uint32_t x; -uint32_t y; -uint64_t a; -uint64_t b; - - x =be32_to_cpu (r1->rmap.rm_startblock); - y =be32_to_cpu (r2->rmap.rm_startblock); -if (x < y) -return 1 ; -else if (x > y) -return 0 ; - a =be64_to_cpu (r1->rmap.rm_owner); - b =be64_to_cpu (r2->rmap.rm_owner); -if (a < b) -return 1 ; -else if (a > b) -return 0 ; - a =offset_keymask (be64_to_cpu (r1->rmap.rm_offset)); - b =offset_keymask (be64_to_cpu (r2->rmap.rm_offset)); -if (a <= b) -return 1 ; -return 0 ; -} - -STATIC enum xbtree_key_contig -xfs_rtrmapbt_keys_contiguous ( -struct xfs_btree_cur *cur, -const union xfs_btree_key *key1, -const union xfs_btree_key *key2, -const union xfs_btree_key *mask) -{ -ASSERT (!mask || mask->rmap.rm_startblock); - -/* -* 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. -*/ -ASSERT (!mask || (!mask->rmap.rm_owner && !mask->rmap.rm_offset)); - -return xbtree_key_contig (be32_to_cpu (key1->rmap.rm_startblock), -be32_to_cpu (key2->rmap.rm_startblock)); -} - static inline void xfs_rtrmapbt_move_ptrs( struct xfs_mount *mp,
@@ -486,16 +290,16 @@ const struct xfs_btree_ops xfs_rtrmapbt_ops = { .get_minrecs = xfs_rtrmapbt_get_minrecs, .get_maxrecs = xfs_rtrmapbt_get_maxrecs, .get_dmaxrecs = xfs_rtrmapbt_get_dmaxrecs, - .init_key_from_rec = xfs_rtrmapbt_init_key_from_rec, - .init_high_key_from_rec = xfs_rtrmapbt_init_high_key_from_rec, - .init_rec_from_cur = xfs_rtrmapbt_init_rec_from_cur, + .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 = xfs_rtrmapbt_init_ptr_from_cur, - .cmp_key_with_cur = xfs_rtrmapbt_cmp_key_with_cur, + .cmp_key_with_cur = xfs_rmapbt_cmp_key_with_cur, .buf_ops = &xfs_rtrmapbt_buf_ops, - .cmp_two_keys = xfs_rtrmapbt_cmp_two_keys, - .keys_inorder = xfs_rtrmapbt_keys_inorder, - .recs_inorder = xfs_rtrmapbt_recs_inorder, - .keys_contiguous = xfs_rtrmapbt_keys_contiguous, + .cmp_two_keys = xfs_rmapbt_cmp_two_keys, + .keys_inorder = xfs_rmapbt_keys_inorder, + .recs_inorder = xfs_rmapbt_recs_inorder, + .keys_contiguous = xfs_rmapbt_keys_contiguous, .broot_realloc = xfs_rtrmapbt_broot_realloc, };
@@ -595,16 +399,16 @@ const struct xfs_btree_ops xfs_rtrmapbt_mem_ops = { .free_block = xfbtree_free_block, .get_minrecs = xfbtree_get_minrecs, .get_maxrecs = xfbtree_get_maxrecs, - .init_key_from_rec = xfs_rtrmapbt_init_key_from_rec, - .init_high_key_from_rec = xfs_rtrmapbt_init_high_key_from_rec, - .init_rec_from_cur = xfs_rtrmapbt_init_rec_from_cur, + .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, - .cmp_key_with_cur = xfs_rtrmapbt_cmp_key_with_cur, + .cmp_key_with_cur = xfs_rmapbt_cmp_key_with_cur, .buf_ops = &xfs_rtrmapbt_mem_buf_ops, - .cmp_two_keys = xfs_rtrmapbt_cmp_two_keys, - .keys_inorder = xfs_rtrmapbt_keys_inorder, - .recs_inorder = xfs_rtrmapbt_recs_inorder, - .keys_contiguous = xfs_rtrmapbt_keys_contiguous, + .cmp_two_keys = xfs_rmapbt_cmp_two_keys, + .keys_inorder = xfs_rmapbt_keys_inorder, + .recs_inorder = xfs_rmapbt_recs_inorder, + .keys_contiguous = xfs_rmapbt_keys_contiguous, };/* Create a cursor for an in-memory btree. */
Signature · 1 line
2.55.0
Quoted 11 lines · Eric Sandeen
xfs_rmap_btree.candxfs_rtrmap_btree.chave a large amount of duplicated code, differing only in function names which add "rt" in the latter case. This can all be shared to reduce the cut & paste.Tested with an auto xfstests run as well as the rmap group with rtdev & rmap enabled.
xfs_rmap_btree.c | 16 +-- xfs_rmap_btree.h | 26 ++++++ xfs_rtrmap_btree.c | 230 +++------------------------------------------------ 3 files changed, 51 insertions(+), 221 deletions(-)
In general this looks good to me, though I'll add that the same sort of code deduplication could likely be done to the refcount btree code too. ;)
--D
Quoted 47 lines
[root@fedora-rawhide xfstests-dev]# ./check -g rmap
FSTYP -- xfs (non-debug)
PLATFORM -- Linux/x86_64 fedora-rawhide 7.3.0-rc3+ #330 SMPPREEMPT_DYNAMICFri Sep 18 10:23:43 CDT 2026MKFS_OPTIONS-- -f -rrtdev=/dev/loop0 -m rmapbt=1,reflink=1/dev/vdb2MOUNT_OPTIONS-- -o context=system_u:object_r:root_t:s0 -ortdev=/dev/loop0/dev/vdb2/mnt/scratchgeneric/365 2s ... 2s xfs/114 3s ... 2s xfs/233 1s ... 1s xfs/234 5s ... 5s xfs/235 1s ... 1s xfs/236 6s ... 6s xfs/271 2s ... 2s xfs/272 2s ... 2s xfs/273 4s ... 5s xfs/274 3s ... 3s xfs/275 [not run] This test requires a valid $SCRATCH_LOGDEV xfs/276 3s ... 3s xfs/277 2s ... 2s xfs/310 0s ... 1s xfs/317 [not run] XFS error injection requires CONFIG_XFS_DEBUG xfs/331 2s ... 2s xfs/332 1s ... 1s xfs/334 1s ... 1s xfs/335 4s ... 3s xfs/336 5s ... 5s xfs/337 5s ... 5s xfs/338 2s ... 2s xfs/339 2s ... 2s xfs/340 2s ... 2s xfs/341 1s ... 1s xfs/342 2s ... 2s xfs/343 2s ... 2s xfs/450 1s ... 2sRan: generic/365 xfs/114 xfs/233 xfs/234 xfs/235 xfs/236 xfs/271 xfs/272 xfs/273 xfs/274 xfs/275 xfs/276 xfs/277 xfs/310 xfs/317 xfs/331 xfs/332 xfs/334 xfs/335 xfs/336 xfs/337 xfs/338 xfs/339 xfs/340 xfs/341 xfs/342 xfs/343 xfs/450
Not run: xfs/275 xfs/317
Passed all 28 tests
Christoph Hellwig
Reviewed-byQuoted 2 lines · Darrick J. Wong
In general this looks good to me, though I'll add that the same sort of code deduplication could likely be done to the refcount btree code too.
I thought the same while reading over the patches. Also the exported in patch 1 threw me off, but it didn't actually export anything.
I'd be tempted to just merge the two patches as that's easier to follow, but otherwise this looks good to me:
Quoted 11 lines · Christoph Hellwig
Quoted 2 lines · Darrick J. Wong
In general this looks good to me, though I'll add that the same sort of code deduplication could likely be done to the refcount btree code too.
I thought the same while reading over the patches. Also the exported in patch 1 threw me off, but it didn't actually export anything.
I'd be tempted to just merge the two patches as that's easier to follow, but otherwise this looks good to me:
Reviewed-by Christoph Hellwig <[email protected]>
Oh, ok - I can merge them if folks prefer. (I kinda thought a prep patch / use patch was simpler to follow but I really don't care either way.)
And yeah, you're both right about the refcount code, I should have thought of that. It's much shorter functions and my duplicate code scan was looking for bigger chunks, but I didn't fully engage my brain.
Happy to send V2 with both, and merge each into a single patch if that's what people want.
-Eric
Quoted 15 lines · Eric Sandeen
Quoted 11 lines · Christoph Hellwig
Quoted 2 lines · Darrick J. Wong
In general this looks good to me, though I'll add that the same sort of code deduplication could likely be done to the refcount btree code too.
I thought the same while reading over the patches. Also the exported in patch 1 threw me off, but it didn't actually export anything.
I'd be tempted to just merge the two patches as that's easier to follow, but otherwise this looks good to me:
Reviewed-by Christoph Hellwig <[email protected]>Oh, ok - I can merge them if folks prefer. (I kinda thought a prep patch / use patch was simpler to follow but I really don't care either way.)
Quoted 6 lines
And yeah, you're both right about the refcount code, I should have thought of that. It's much shorter functions and my duplicate code scan was looking for bigger chunks, but I didn't fully engage my brain.
Happy to send V2 with both, and merge each into a single patch if that's what people want.
This sounds like a good plan for me :)
For the code in these patches though:
Quoted 3 lines
-Eric