xfs: cleanup key comparing routines
v2 cover xfs: cleanup key comparing routines
Key comparing routines are currently opencoded with extra casts and subtractions which is error prone and can be replaced with a neat cmp_int() helper which is now in a generic header file.
Started from:
https://lore.kernel.org/linux-xfs/[email protected]/T/#u
Thanks Darrick for suggestion!
v1: https://lore.kernel.org/linux-xfs/[email protected]/T/#u
v2: tune 6/6 patch to rename the "diff_two_ptrs" part
Fedor Pchelkin (6):
xfs: rename diff_two_keys routines
xfs: rename key_diff routines
xfs: refactor cmp_two_keys routines to take advantage of cmp_int()
xfs: refactor cmp_key_with_cur routines to take advantage of cmp_int()
xfs: use a proper variable name and type for storing a comparison
result
xfs: refactor xfs_btree_diff_two_ptrs() to take advantage of cmp_int()
fs/xfs/libxfs/xfs_alloc_btree.c | 52 +++++++++------------ fs/xfs/libxfs/xfs_bmap_btree.c | 32 +++++-------- fs/xfs/libxfs/xfs_btree.c | 33 +++++++------- fs/xfs/libxfs/xfs_btree.h | 41 +++++++++-------- fs/xfs/libxfs/xfs_ialloc_btree.c | 24 +++++----- fs/xfs/libxfs/xfs_refcount_btree.c | 18 ++++---- fs/xfs/libxfs/xfs_rmap_btree.c | 67 ++++++++++------------------ fs/xfs/libxfs/xfs_rtrefcount_btree.c | 18 ++++---- fs/xfs/libxfs/xfs_rtrmap_btree.c | 67 ++++++++++------------------ fs/xfs/scrub/btree.c | 2 +- fs/xfs/scrub/rcbag_btree.c | 38 +++++----------- 11 files changed, 158 insertions(+), 234 deletions(-)
Signature · 1 line
2.50.0
PATCH v2 1/6 xfs: rename diff_two_keys routines
One may think that diff_two_keys routines are used to compute the actual difference between the arguments but they return a result of a three-way-comparison of the passed operands. So it looks more appropriate to denote them as cmp_two_keys.
Found by Linux Verification Center (linuxtesting.org).
---
v2: add R-b
fs/xfs/libxfs/xfs_alloc_btree.c | 8 ++++---- fs/xfs/libxfs/xfs_bmap_btree.c | 4 ++-- fs/xfs/libxfs/xfs_btree.c | 2 +- fs/xfs/libxfs/xfs_btree.h | 26 +++++++++++++------------- fs/xfs/libxfs/xfs_ialloc_btree.c | 6 +++--- fs/xfs/libxfs/xfs_refcount_btree.c | 4 ++-- fs/xfs/libxfs/xfs_rmap_btree.c | 6 +++--- fs/xfs/libxfs/xfs_rtrefcount_btree.c | 4 ++-- fs/xfs/libxfs/xfs_rtrmap_btree.c | 6 +++--- fs/xfs/scrub/rcbag_btree.c | 4 ++-- 10 files changed, 35 insertions(+), 35 deletions(-)
fs/xfs/libxfs/xfs_alloc_btree.c +4 -4
@@ -214,7 +214,7 @@ xfs_cntbt_key_diff( } STATIC int64_t -xfs_bnobt_diff_two_keys( +xfs_bnobt_cmp_two_keys( struct xfs_btree_cur *cur, const union xfs_btree_key *k1, const union xfs_btree_key *k2,
@@ -227,7 +227,7 @@ xfs_bnobt_diff_two_keys( } STATIC int64_t -xfs_cntbt_diff_two_keys( +xfs_cntbt_cmp_two_keys( struct xfs_btree_cur *cur, const union xfs_btree_key *k1, const union xfs_btree_key *k2,
@@ -440,7 +440,7 @@ const struct xfs_btree_ops xfs_bnobt_ops = { .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur, .key_diff = xfs_bnobt_key_diff, .buf_ops = &xfs_bnobt_buf_ops, - .diff_two_keys = xfs_bnobt_diff_two_keys, + .cmp_two_keys = xfs_bnobt_cmp_two_keys, .keys_inorder = xfs_bnobt_keys_inorder, .recs_inorder = xfs_bnobt_recs_inorder, .keys_contiguous = xfs_allocbt_keys_contiguous,
@@ -470,7 +470,7 @@ const struct xfs_btree_ops xfs_cntbt_ops = { .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur, .key_diff = xfs_cntbt_key_diff, .buf_ops = &xfs_cntbt_buf_ops, - .diff_two_keys = xfs_cntbt_diff_two_keys, + .cmp_two_keys = xfs_cntbt_cmp_two_keys, .keys_inorder = xfs_cntbt_keys_inorder, .recs_inorder = xfs_cntbt_recs_inorder, .keys_contiguous = NULL, /* not needed right now */
fs/xfs/libxfs/xfs_bmap_btree.c +2 -2
@@ -379,7 +379,7 @@ xfs_bmbt_key_diff( } STATIC int64_t -xfs_bmbt_diff_two_keys( +xfs_bmbt_cmp_two_keys( struct xfs_btree_cur *cur, const union xfs_btree_key *k1, const union xfs_btree_key *k2,
@@ -648,7 +648,7 @@ const struct xfs_btree_ops xfs_bmbt_ops = { .init_high_key_from_rec = xfs_bmbt_init_high_key_from_rec, .init_rec_from_cur = xfs_bmbt_init_rec_from_cur, .key_diff = xfs_bmbt_key_diff, - .diff_two_keys = xfs_bmbt_diff_two_keys, + .cmp_two_keys = xfs_bmbt_cmp_two_keys, .buf_ops = &xfs_bmbt_buf_ops, .keys_inorder = xfs_bmbt_keys_inorder, .recs_inorder = xfs_bmbt_recs_inorder,
fs/xfs/libxfs/xfs_btree.c +1 -1
@@ -5058,7 +5058,7 @@ xfs_btree_simple_query_range(int error;ASSERT (cur->bc_ops->init_high_key_from_rec); -ASSERT (cur->bc_ops->diff_two_keys); +ASSERT (cur->bc_ops->cmp_two_keys);/* * Find the leftmost record. The btree cursor must be set
fs/xfs/libxfs/xfs_btree.h +13 -13
@@ -176,15 +176,15 @@ struct xfs_btree_ops { const union xfs_btree_key *key);/* -* Difference between key2 and key1 -- positive if key1 > key2, -* negative if key1 < key2, and zero if equal. If the @mask parameter -* is non NULL, each key field to be used in the comparison must -* contain a nonzero value. +* Compare key1 and key2 -- positive if key1 > key2, negative if +* key1 < key2, and zero if equal. If the @mask parameter is non NULL, +* each key field to be used in the comparison must contain a nonzero +* value. */ -int64_t (*diff_two_keys)(struct xfs_btree_cur *cur, -const union xfs_btree_key *key1, -const union xfs_btree_key *key2, -const union xfs_btree_key *mask); +int64_t (*cmp_two_keys)(struct xfs_btree_cur *cur, +const union xfs_btree_key *key1, +const union xfs_btree_key *key2, +const union xfs_btree_key *mask);const struct xfs_buf_ops *buf_ops;
@@ -546,7 +546,7 @@ xfs_btree_keycmp_lt(const union xfs_btree_key *key1, const union xfs_btree_key *key2) { - return cur->bc_ops->diff_two_keys(cur, key1, key2, NULL) < 0; + return cur->bc_ops->cmp_two_keys(cur, key1, key2, NULL) < 0; } static inline bool
@@ -555,7 +555,7 @@ xfs_btree_keycmp_gt(const union xfs_btree_key *key1, const union xfs_btree_key *key2) { - return cur->bc_ops->diff_two_keys(cur, key1, key2, NULL) > 0; + return cur->bc_ops->cmp_two_keys(cur, key1, key2, NULL) > 0; } static inline bool
@@ -564,7 +564,7 @@ xfs_btree_keycmp_eq(const union xfs_btree_key *key1, const union xfs_btree_key *key2) { - return cur->bc_ops->diff_two_keys(cur, key1, key2, NULL) == 0; + return cur->bc_ops->cmp_two_keys(cur, key1, key2, NULL) == 0; } static inline bool
@@ -602,7 +602,7 @@ xfs_btree_masked_keycmp_lt(const union xfs_btree_key *key2, const union xfs_btree_key *mask) { - return cur->bc_ops->diff_two_keys(cur, key1, key2, mask) < 0; + return cur->bc_ops->cmp_two_keys(cur, key1, key2, mask) < 0; } static inline bool
@@ -612,7 +612,7 @@ xfs_btree_masked_keycmp_gt(const union xfs_btree_key *key2, const union xfs_btree_key *mask) { - return cur->bc_ops->diff_two_keys(cur, key1, key2, mask) > 0; + return cur->bc_ops->cmp_two_keys(cur, key1, key2, mask) > 0; } static inline bool
fs/xfs/libxfs/xfs_ialloc_btree.c +3 -3
@@ -275,7 +275,7 @@ xfs_inobt_key_diff( } STATIC int64_t -xfs_inobt_diff_two_keys( +xfs_inobt_cmp_two_keys( struct xfs_btree_cur *cur, const union xfs_btree_key *k1, const union xfs_btree_key *k2,
@@ -432,7 +432,7 @@ const struct xfs_btree_ops xfs_inobt_ops = { .init_ptr_from_cur = xfs_inobt_init_ptr_from_cur, .key_diff = xfs_inobt_key_diff, .buf_ops = &xfs_inobt_buf_ops, - .diff_two_keys = xfs_inobt_diff_two_keys, + .cmp_two_keys = xfs_inobt_cmp_two_keys, .keys_inorder = xfs_inobt_keys_inorder, .recs_inorder = xfs_inobt_recs_inorder, .keys_contiguous = xfs_inobt_keys_contiguous,
@@ -462,7 +462,7 @@ const struct xfs_btree_ops xfs_finobt_ops = { .init_ptr_from_cur = xfs_finobt_init_ptr_from_cur, .key_diff = xfs_inobt_key_diff, .buf_ops = &xfs_finobt_buf_ops, - .diff_two_keys = xfs_inobt_diff_two_keys, + .cmp_two_keys = xfs_inobt_cmp_two_keys, .keys_inorder = xfs_inobt_keys_inorder, .recs_inorder = xfs_inobt_recs_inorder, .keys_contiguous = xfs_inobt_keys_contiguous,
fs/xfs/libxfs/xfs_refcount_btree.c +2 -2
@@ -189,7 +189,7 @@ xfs_refcountbt_key_diff( } STATIC int64_t -xfs_refcountbt_diff_two_keys( +xfs_refcountbt_cmp_two_keys( struct xfs_btree_cur *cur, const union xfs_btree_key *k1, const union xfs_btree_key *k2,
@@ -341,7 +341,7 @@ const struct xfs_btree_ops xfs_refcountbt_ops = { .init_ptr_from_cur = xfs_refcountbt_init_ptr_from_cur, .key_diff = xfs_refcountbt_key_diff, .buf_ops = &xfs_refcountbt_buf_ops, - .diff_two_keys = xfs_refcountbt_diff_two_keys, + .cmp_two_keys = xfs_refcountbt_cmp_two_keys, .keys_inorder = xfs_refcountbt_keys_inorder, .recs_inorder = xfs_refcountbt_recs_inorder, .keys_contiguous = xfs_refcountbt_keys_contiguous,
fs/xfs/libxfs/xfs_rmap_btree.c +3 -3
@@ -274,7 +274,7 @@ xfs_rmapbt_key_diff( } STATIC int64_t -xfs_rmapbt_diff_two_keys( +xfs_rmapbt_cmp_two_keys( struct xfs_btree_cur *cur, const union xfs_btree_key *k1, const union xfs_btree_key *k2,
@@ -517,7 +517,7 @@ const struct xfs_btree_ops xfs_rmapbt_ops = { .init_ptr_from_cur = xfs_rmapbt_init_ptr_from_cur, .key_diff = xfs_rmapbt_key_diff, .buf_ops = &xfs_rmapbt_buf_ops, - .diff_two_keys = xfs_rmapbt_diff_two_keys, + .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,
@@ -634,7 +634,7 @@ const struct xfs_btree_ops xfs_rmapbt_mem_ops = { .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, + .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,
fs/xfs/libxfs/xfs_rtrefcount_btree.c +2 -2
@@ -171,7 +171,7 @@ xfs_rtrefcountbt_key_diff( } STATIC int64_t -xfs_rtrefcountbt_diff_two_keys( +xfs_rtrefcountbt_cmp_two_keys( struct xfs_btree_cur *cur, const union xfs_btree_key *k1, const union xfs_btree_key *k2,
@@ -389,7 +389,7 @@ const struct xfs_btree_ops xfs_rtrefcountbt_ops = { .init_ptr_from_cur = xfs_rtrefcountbt_init_ptr_from_cur, .key_diff = xfs_rtrefcountbt_key_diff, .buf_ops = &xfs_rtrefcountbt_buf_ops, - .diff_two_keys = xfs_rtrefcountbt_diff_two_keys, + .cmp_two_keys = xfs_rtrefcountbt_cmp_two_keys, .keys_inorder = xfs_rtrefcountbt_keys_inorder, .recs_inorder = xfs_rtrefcountbt_recs_inorder, .keys_contiguous = xfs_rtrefcountbt_keys_contiguous,
fs/xfs/libxfs/xfs_rtrmap_btree.c +3 -3
@@ -216,7 +216,7 @@ xfs_rtrmapbt_key_diff( } STATIC int64_t -xfs_rtrmapbt_diff_two_keys( +xfs_rtrmapbt_cmp_two_keys( struct xfs_btree_cur *cur, const union xfs_btree_key *k1, const union xfs_btree_key *k2,
@@ -513,7 +513,7 @@ const struct xfs_btree_ops xfs_rtrmapbt_ops = { .init_ptr_from_cur = xfs_rtrmapbt_init_ptr_from_cur, .key_diff = xfs_rtrmapbt_key_diff, .buf_ops = &xfs_rtrmapbt_buf_ops, - .diff_two_keys = xfs_rtrmapbt_diff_two_keys, + .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,
@@ -622,7 +622,7 @@ const struct xfs_btree_ops xfs_rtrmapbt_mem_ops = { .init_ptr_from_cur = xfbtree_init_ptr_from_cur, .key_diff = xfs_rtrmapbt_key_diff, .buf_ops = &xfs_rtrmapbt_mem_buf_ops, - .diff_two_keys = xfs_rtrmapbt_diff_two_keys, + .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,
fs/xfs/scrub/rcbag_btree.c +2 -2
@@ -69,7 +69,7 @@ rcbagbt_key_diff( } STATIC int64_t -rcbagbt_diff_two_keys( +rcbagbt_cmp_two_keys( struct xfs_btree_cur *cur, const union xfs_btree_key *k1, const union xfs_btree_key *k2,
@@ -203,7 +203,7 @@ static const struct xfs_btree_ops rcbagbt_mem_ops = { .init_ptr_from_cur = xfbtree_init_ptr_from_cur, .key_diff = rcbagbt_key_diff, .buf_ops = &rcbagbt_mem_buf_ops, - .diff_two_keys = rcbagbt_diff_two_keys, + .cmp_two_keys = rcbagbt_cmp_two_keys, .keys_inorder = rcbagbt_keys_inorder, .recs_inorder = rcbagbt_recs_inorder, };
Signature · 1 line
2.50.0
PATCH v2 2/6 xfs: rename key_diff routines
In reply to Fedor Pchelkinkey_diff routines compare a key value with a cursor value. Make the naming to be a bit more self-descriptive.
Found by Linux Verification Center (linuxtesting.org).
---
v2: add R-b
fs/xfs/libxfs/xfs_alloc_btree.c | 8 ++++---- fs/xfs/libxfs/xfs_bmap_btree.c | 4 ++-- fs/xfs/libxfs/xfs_btree.c | 2 +- fs/xfs/libxfs/xfs_btree.h | 9 ++++++--- fs/xfs/libxfs/xfs_ialloc_btree.c | 6 +++--- fs/xfs/libxfs/xfs_refcount_btree.c | 4 ++-- fs/xfs/libxfs/xfs_rmap_btree.c | 6 +++--- fs/xfs/libxfs/xfs_rtrefcount_btree.c | 4 ++-- fs/xfs/libxfs/xfs_rtrmap_btree.c | 6 +++--- fs/xfs/scrub/rcbag_btree.c | 4 ++-- 10 files changed, 28 insertions(+), 25 deletions(-)
fs/xfs/libxfs/xfs_alloc_btree.c +4 -4
@@ -187,7 +187,7 @@ xfs_allocbt_init_ptr_from_cur( } STATIC int64_t -xfs_bnobt_key_diff ( +xfs_bnobt_cmp_key_with_cur (struct xfs_btree_cur *cur,const union xfs_btree_key *key) {
@@ -198,7 +198,7 @@ xfs_bnobt_key_diff( } STATIC int64_t -xfs_cntbt_key_diff ( +xfs_cntbt_cmp_key_with_cur (struct xfs_btree_cur *cur,const union xfs_btree_key *key) {
@@ -438,7 +438,7 @@ const struct xfs_btree_ops xfs_bnobt_ops = { .init_high_key_from_rec = xfs_bnobt_init_high_key_from_rec, .init_rec_from_cur = xfs_allocbt_init_rec_from_cur, .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur, - .key_diff = xfs_bnobt_key_diff, + .cmp_key_with_cur = xfs_bnobt_cmp_key_with_cur, .buf_ops = &xfs_bnobt_buf_ops, .cmp_two_keys = xfs_bnobt_cmp_two_keys, .keys_inorder = xfs_bnobt_keys_inorder,
@@ -468,7 +468,7 @@ const struct xfs_btree_ops xfs_cntbt_ops = { .init_high_key_from_rec = xfs_cntbt_init_high_key_from_rec, .init_rec_from_cur = xfs_allocbt_init_rec_from_cur, .init_ptr_from_cur = xfs_allocbt_init_ptr_from_cur, - .key_diff = xfs_cntbt_key_diff, + .cmp_key_with_cur = xfs_cntbt_cmp_key_with_cur, .buf_ops = &xfs_cntbt_buf_ops, .cmp_two_keys = xfs_cntbt_cmp_two_keys, .keys_inorder = xfs_cntbt_keys_inorder,
fs/xfs/libxfs/xfs_bmap_btree.c +2 -2
@@ -370,7 +370,7 @@ xfs_bmbt_init_rec_from_cur( } STATIC int64_t -xfs_bmbt_key_diff ( +xfs_bmbt_cmp_key_with_cur (struct xfs_btree_cur *cur,const union xfs_btree_key *key) {
@@ -647,7 +647,7 @@ const struct xfs_btree_ops xfs_bmbt_ops = { .init_key_from_rec = xfs_bmbt_init_key_from_rec, .init_high_key_from_rec = xfs_bmbt_init_high_key_from_rec, .init_rec_from_cur = xfs_bmbt_init_rec_from_cur, - .key_diff = xfs_bmbt_key_diff, + .cmp_key_with_cur = xfs_bmbt_cmp_key_with_cur, .cmp_two_keys = xfs_bmbt_cmp_two_keys, .buf_ops = &xfs_bmbt_buf_ops, .keys_inorder = xfs_bmbt_keys_inorder,
fs/xfs/libxfs/xfs_btree.c +1 -1
@@ -2070,7 +2070,7 @@ xfs_btree_lookup(* - greater than, move left * - equal, we're done */ - diff = cur->bc_ops->key_diff (cur, kp); + diff = cur->bc_ops->cmp_key_with_cur (cur, kp);if (diff <0 ) low = keyno +1 ;else if (diff >0 )
fs/xfs/libxfs/xfs_btree.h +6 -3
@@ -171,9 +171,12 @@ struct xfs_btree_ops {void (*init_high_key_from_rec)(union xfs_btree_key *key,const union xfs_btree_rec *rec); -/* difference between key value and cursor value */ -int64_t (*key_diff)(struct xfs_btree_cur *cur, -const union xfs_btree_key *key); +/* +* Compare key value and cursor value -- positive if key > cur, +* negative if key < cur, and zero if equal. +*/ +int64_t (*cmp_key_with_cur)(struct xfs_btree_cur *cur, +const union xfs_btree_key *key);/* * Compare key1 and key2 -- positive if key1 > key2, negative if
fs/xfs/libxfs/xfs_ialloc_btree.c +3 -3
@@ -266,7 +266,7 @@ xfs_finobt_init_ptr_from_cur( } STATIC int64_t -xfs_inobt_key_diff ( +xfs_inobt_cmp_key_with_cur (struct xfs_btree_cur *cur,const union xfs_btree_key *key) {
@@ -430,7 +430,7 @@ const struct xfs_btree_ops xfs_inobt_ops = { .init_high_key_from_rec = xfs_inobt_init_high_key_from_rec, .init_rec_from_cur = xfs_inobt_init_rec_from_cur, .init_ptr_from_cur = xfs_inobt_init_ptr_from_cur, - .key_diff = xfs_inobt_key_diff, + .cmp_key_with_cur = xfs_inobt_cmp_key_with_cur, .buf_ops = &xfs_inobt_buf_ops, .cmp_two_keys = xfs_inobt_cmp_two_keys, .keys_inorder = xfs_inobt_keys_inorder,
@@ -460,7 +460,7 @@ const struct xfs_btree_ops xfs_finobt_ops = { .init_high_key_from_rec = xfs_inobt_init_high_key_from_rec, .init_rec_from_cur = xfs_inobt_init_rec_from_cur, .init_ptr_from_cur = xfs_finobt_init_ptr_from_cur, - .key_diff = xfs_inobt_key_diff, + .cmp_key_with_cur = xfs_inobt_cmp_key_with_cur, .buf_ops = &xfs_finobt_buf_ops, .cmp_two_keys = xfs_inobt_cmp_two_keys, .keys_inorder = xfs_inobt_keys_inorder,
fs/xfs/libxfs/xfs_refcount_btree.c +2 -2
@@ -175,7 +175,7 @@ xfs_refcountbt_init_ptr_from_cur( } STATIC int64_t -xfs_refcountbt_key_diff ( +xfs_refcountbt_cmp_key_with_cur (struct xfs_btree_cur *cur,const union xfs_btree_key *key) {
@@ -339,7 +339,7 @@ const struct xfs_btree_ops xfs_refcountbt_ops = { .init_high_key_from_rec = xfs_refcountbt_init_high_key_from_rec, .init_rec_from_cur = xfs_refcountbt_init_rec_from_cur, .init_ptr_from_cur = xfs_refcountbt_init_ptr_from_cur, - .key_diff = xfs_refcountbt_key_diff, + .cmp_key_with_cur = xfs_refcountbt_cmp_key_with_cur, .buf_ops = &xfs_refcountbt_buf_ops, .cmp_two_keys = xfs_refcountbt_cmp_two_keys, .keys_inorder = xfs_refcountbt_keys_inorder,
fs/xfs/libxfs/xfs_rmap_btree.c +3 -3
@@ -244,7 +244,7 @@ static inline uint64_t offset_keymask(uint64_t offset) } STATIC int64_t -xfs_rmapbt_key_diff ( +xfs_rmapbt_cmp_key_with_cur (struct xfs_btree_cur *cur,const union xfs_btree_key *key) {
@@ -515,7 +515,7 @@ const struct xfs_btree_ops xfs_rmapbt_ops = { .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_rmapbt_init_ptr_from_cur, - .key_diff = xfs_rmapbt_key_diff, + .cmp_key_with_cur = xfs_rmapbt_cmp_key_with_cur, .buf_ops = &xfs_rmapbt_buf_ops, .cmp_two_keys = xfs_rmapbt_cmp_two_keys, .keys_inorder = xfs_rmapbt_keys_inorder,
@@ -632,7 +632,7 @@ const struct xfs_btree_ops xfs_rmapbt_mem_ops = { .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, + .cmp_key_with_cur = xfs_rmapbt_cmp_key_with_cur, .buf_ops = &xfs_rmapbt_mem_buf_ops, .cmp_two_keys = xfs_rmapbt_cmp_two_keys, .keys_inorder = xfs_rmapbt_keys_inorder,
fs/xfs/libxfs/xfs_rtrefcount_btree.c +2 -2
@@ -157,7 +157,7 @@ xfs_rtrefcountbt_init_ptr_from_cur( } STATIC int64_t -xfs_rtrefcountbt_key_diff ( +xfs_rtrefcountbt_cmp_key_with_cur (struct xfs_btree_cur *cur,const union xfs_btree_key *key) {
@@ -387,7 +387,7 @@ const struct xfs_btree_ops xfs_rtrefcountbt_ops = { .init_high_key_from_rec = xfs_rtrefcountbt_init_high_key_from_rec, .init_rec_from_cur = xfs_rtrefcountbt_init_rec_from_cur, .init_ptr_from_cur = xfs_rtrefcountbt_init_ptr_from_cur, - .key_diff = xfs_rtrefcountbt_key_diff, + .cmp_key_with_cur = xfs_rtrefcountbt_cmp_key_with_cur, .buf_ops = &xfs_rtrefcountbt_buf_ops, .cmp_two_keys = xfs_rtrefcountbt_cmp_two_keys, .keys_inorder = xfs_rtrefcountbt_keys_inorder,
fs/xfs/libxfs/xfs_rtrmap_btree.c +3 -3
@@ -186,7 +186,7 @@ static inline uint64_t offset_keymask(uint64_t offset) } STATIC int64_t -xfs_rtrmapbt_key_diff ( +xfs_rtrmapbt_cmp_key_with_cur (struct xfs_btree_cur *cur,const union xfs_btree_key *key) {
@@ -511,7 +511,7 @@ const struct xfs_btree_ops xfs_rtrmapbt_ops = { .init_high_key_from_rec = xfs_rtrmapbt_init_high_key_from_rec, .init_rec_from_cur = xfs_rtrmapbt_init_rec_from_cur, .init_ptr_from_cur = xfs_rtrmapbt_init_ptr_from_cur, - .key_diff = xfs_rtrmapbt_key_diff, + .cmp_key_with_cur = xfs_rtrmapbt_cmp_key_with_cur, .buf_ops = &xfs_rtrmapbt_buf_ops, .cmp_two_keys = xfs_rtrmapbt_cmp_two_keys, .keys_inorder = xfs_rtrmapbt_keys_inorder,
@@ -620,7 +620,7 @@ const struct xfs_btree_ops xfs_rtrmapbt_mem_ops = { .init_high_key_from_rec = xfs_rtrmapbt_init_high_key_from_rec, .init_rec_from_cur = xfs_rtrmapbt_init_rec_from_cur, .init_ptr_from_cur = xfbtree_init_ptr_from_cur, - .key_diff = xfs_rtrmapbt_key_diff, + .cmp_key_with_cur = xfs_rtrmapbt_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,
fs/xfs/scrub/rcbag_btree.c +2 -2
@@ -48,7 +48,7 @@ rcbagbt_init_rec_from_cur( } STATIC int64_t -rcbagbt_key_diff ( +rcbagbt_cmp_key_with_cur (struct xfs_btree_cur *cur,const union xfs_btree_key *key) {
@@ -201,7 +201,7 @@ static const struct xfs_btree_ops rcbagbt_mem_ops = { .init_key_from_rec = rcbagbt_init_key_from_rec, .init_rec_from_cur = rcbagbt_init_rec_from_cur, .init_ptr_from_cur = xfbtree_init_ptr_from_cur, - .key_diff = rcbagbt_key_diff, + .cmp_key_with_cur = rcbagbt_cmp_key_with_cur, .buf_ops = &rcbagbt_mem_buf_ops, .cmp_two_keys = rcbagbt_cmp_two_keys, .keys_inorder = rcbagbt_keys_inorder,
Signature · 1 line
2.50.0
PATCH v2 3/6 xfs: refactor cmp_two_keys routines to take advantage of cmp_int()
In reply to Fedor PchelkinThe net value of these functions is to determine the result of a three-way-comparison between operands of the same type.
Simplify the code using cmp_int() to eliminate potential errors with opencoded casts and subtractions. This also means we can change the return value type of cmp_two_keys routines from int64_t to int and make the interface a bit clearer.
Found by Linux Verification Center (linuxtesting.org).
---
v2: add R-b
fs/xfs/libxfs/xfs_alloc_btree.c | 21 ++++++++------------ fs/xfs/libxfs/xfs_bmap_btree.c | 18 +++-------------- fs/xfs/libxfs/xfs_btree.h | 2 +- fs/xfs/libxfs/xfs_ialloc_btree.c | 6 +++--- fs/xfs/libxfs/xfs_refcount_btree.c | 6 +++--- fs/xfs/libxfs/xfs_rmap_btree.c | 29 ++++++++++++---------------- fs/xfs/libxfs/xfs_rtrefcount_btree.c | 6 +++--- fs/xfs/libxfs/xfs_rtrmap_btree.c | 29 ++++++++++++---------------- fs/xfs/scrub/rcbag_btree.c | 15 +++----------- 9 files changed, 48 insertions(+), 84 deletions(-)
fs/xfs/libxfs/xfs_alloc_btree.c +8 -13
@@ -213,7 +213,7 @@ xfs_cntbt_cmp_key_with_cur(return (int64_t )be32_to_cpu (kp->ar_startblock) - rec->ar_startblock; } -STATIC int64_t +STATIC int xfs_bnobt_cmp_two_keys( struct xfs_btree_cur *cur, const union xfs_btree_key *k1,
@@ -222,29 +222,24 @@ xfs_bnobt_cmp_two_keys( {ASSERT (!mask || mask->alloc.ar_startblock); -return (int64_t )be32_to_cpu (k1->alloc.ar_startblock) - -be32_to_cpu (k2->alloc.ar_startblock); +return cmp_int (be32_to_cpu (k1->alloc.ar_startblock), +be32_to_cpu (k2->alloc.ar_startblock)); } -STATIC int64_t +STATIC intxfs_cntbt_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) { -int64_t diff; -ASSERT (!mask || (mask->alloc.ar_blockcount && mask->alloc.ar_startblock)); - diff =be32_to_cpu (k1->alloc.ar_blockcount) - -be32_to_cpu (k2->alloc.ar_blockcount); -if (diff) -return diff; - -return be32_to_cpu (k1->alloc.ar_startblock) - -be32_to_cpu (k2->alloc.ar_startblock); +return cmp_int (be32_to_cpu (k1->alloc.ar_blockcount), +be32_to_cpu (k2->alloc.ar_blockcount)) ?: +cmp_int (be32_to_cpu (k1->alloc.ar_startblock), +be32_to_cpu (k2->alloc.ar_startblock)); } static xfs_failaddr_t
fs/xfs/libxfs/xfs_bmap_btree.c +3 -15
@@ -378,29 +378,17 @@ xfs_bmbt_cmp_key_with_cur( cur->bc_rec.b.br_startoff; } -STATIC int64_t +STATIC intxfs_bmbt_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) { -uint64_t a =be64_to_cpu (k1->bmbt.br_startoff); -uint64_t b =be64_to_cpu (k2->bmbt.br_startoff); -ASSERT (!mask || mask->bmbt.br_startoff); -/* -* Note: This routine previously casted a and b to int64 and subtracted -* them to generate a result. This lead to problems if b was the -* "maximum" key value (all ones) being signed incorrectly, hence this -* somewhat less efficient version. -*/ -if (a > b) -return 1 ; -if (b > a) -return -1 ; -return 0 ; +return cmp_int (be64_to_cpu (k1->bmbt.br_startoff), +be64_to_cpu (k2->bmbt.br_startoff)); } static xfs_failaddr_t
fs/xfs/libxfs/xfs_btree.h +1 -1
@@ -184,7 +184,7 @@ struct xfs_btree_ops {* each key field to be used in the comparison must contain a nonzero * value. */ -int64_t (*cmp_two_keys)(struct xfs_btree_cur *cur, +int (*cmp_two_keys)(struct xfs_btree_cur *cur,const union xfs_btree_key *key1,const union xfs_btree_key *key2,const union xfs_btree_key *mask);
fs/xfs/libxfs/xfs_ialloc_btree.c +3 -3
@@ -274,7 +274,7 @@ xfs_inobt_cmp_key_with_cur( cur->bc_rec.i.ir_startino; } -STATIC int64_t +STATIC int xfs_inobt_cmp_two_keys( struct xfs_btree_cur *cur, const union xfs_btree_key *k1,
@@ -283,8 +283,8 @@ xfs_inobt_cmp_two_keys( {ASSERT (!mask || mask->inobt.ir_startino); -return (int64_t )be32_to_cpu (k1->inobt.ir_startino) - -be32_to_cpu (k2->inobt.ir_startino); +return cmp_int (be32_to_cpu (k1->inobt.ir_startino), +be32_to_cpu (k2->inobt.ir_startino)); } static xfs_failaddr_t
fs/xfs/libxfs/xfs_refcount_btree.c +3 -3
@@ -188,7 +188,7 @@ xfs_refcountbt_cmp_key_with_cur(return (int64_t )be32_to_cpu (kp->rc_startblock) - start; } -STATIC int64_t +STATIC int xfs_refcountbt_cmp_two_keys( struct xfs_btree_cur *cur, const union xfs_btree_key *k1,
@@ -197,8 +197,8 @@ xfs_refcountbt_cmp_two_keys( {ASSERT (!mask || mask->refc.rc_startblock); -return (int64_t )be32_to_cpu (k1->refc.rc_startblock) - -be32_to_cpu (k2->refc.rc_startblock); +return cmp_int (be32_to_cpu (k1->refc.rc_startblock), +be32_to_cpu (k2->refc.rc_startblock)); }STATIC xfs_failaddr_t
fs/xfs/libxfs/xfs_rmap_btree.c +12 -17
@@ -273,7 +273,7 @@ xfs_rmapbt_cmp_key_with_cur(return 0 ; } -STATIC int64_t +STATIC int xfs_rmapbt_cmp_two_keys( struct xfs_btree_cur *cur, const union xfs_btree_key *k1,
@@ -282,36 +282,31 @@ xfs_rmapbt_cmp_two_keys( {const struct xfs_rmap_key *kp1 = &k1->rmap;const struct xfs_rmap_key *kp2 = &k2->rmap; -int64_t d; -__u64 x, y; +int d;/* Doesn't make sense to mask off the physical space part */ ASSERT (!mask || mask->rmap.rm_startblock); - d = (int64_t )be32_to_cpu (kp1->rm_startblock) - -be32_to_cpu (kp2->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) { - x =be64_to_cpu (kp1->rm_owner); - y =be64_to_cpu (kp2->rm_owner); -if (x > y) -return 1 ; -else if (y > x) -return -1 ; + 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); - x =offset_keymask (be64_to_cpu (kp1->rm_offset)); - y =offset_keymask (be64_to_cpu (kp2->rm_offset)); -if (x > y) -return 1 ; -else if (y > x) -return -1 ; + 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 ;
fs/xfs/libxfs/xfs_rtrefcount_btree.c +3 -3
@@ -170,7 +170,7 @@ xfs_rtrefcountbt_cmp_key_with_cur(return (int64_t )be32_to_cpu (kp->rc_startblock) - start; } -STATIC int64_t +STATIC int xfs_rtrefcountbt_cmp_two_keys( struct xfs_btree_cur *cur, const union xfs_btree_key *k1,
@@ -179,8 +179,8 @@ xfs_rtrefcountbt_cmp_two_keys( {ASSERT (!mask || mask->refc.rc_startblock); -return (int64_t )be32_to_cpu (k1->refc.rc_startblock) - -be32_to_cpu (k2->refc.rc_startblock); +return cmp_int (be32_to_cpu (k1->refc.rc_startblock), +be32_to_cpu (k2->refc.rc_startblock)); } static xfs_failaddr_t
fs/xfs/libxfs/xfs_rtrmap_btree.c +12 -17
@@ -215,7 +215,7 @@ xfs_rtrmapbt_cmp_key_with_cur(return 0 ; } -STATIC int64_t +STATIC int xfs_rtrmapbt_cmp_two_keys( struct xfs_btree_cur *cur, const union xfs_btree_key *k1,
@@ -224,36 +224,31 @@ xfs_rtrmapbt_cmp_two_keys( {const struct xfs_rmap_key *kp1 = &k1->rmap;const struct xfs_rmap_key *kp2 = &k2->rmap; -int64_t d; -__u64 x, y; +int d;/* Doesn't make sense to mask off the physical space part */ ASSERT (!mask || mask->rmap.rm_startblock); - d = (int64_t )be32_to_cpu (kp1->rm_startblock) - -be32_to_cpu (kp2->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) { - x =be64_to_cpu (kp1->rm_owner); - y =be64_to_cpu (kp2->rm_owner); -if (x > y) -return 1 ; -else if (y > x) -return -1 ; + 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); - x =offset_keymask (be64_to_cpu (kp1->rm_offset)); - y =offset_keymask (be64_to_cpu (kp2->rm_offset)); -if (x > y) -return 1 ; -else if (y > x) -return -1 ; + 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 ;
fs/xfs/scrub/rcbag_btree.c +3 -12
@@ -68,7 +68,7 @@ rcbagbt_cmp_key_with_cur(return 0 ; } -STATIC int64_t +STATIC int rcbagbt_cmp_two_keys( struct xfs_btree_cur *cur, const union xfs_btree_key *k1,
@@ -80,17 +80,8 @@ rcbagbt_cmp_two_keys(ASSERT (mask ==NULL ); -if (kp1->rbg_startblock > kp2->rbg_startblock) -return 1 ; -if (kp1->rbg_startblock < kp2->rbg_startblock) -return -1 ; - -if (kp1->rbg_blockcount > kp2->rbg_blockcount) -return 1 ; -if (kp1->rbg_blockcount < kp2->rbg_blockcount) -return -1 ; - -return 0 ; +return cmp_int (kp1->rbg_startblock, kp2->rbg_startblock) ?: +cmp_int (kp1->rbg_blockcount, kp2->rbg_blockcount); }STATIC int
Signature · 1 line
2.50.0
PATCH v2 4/6 xfs: refactor cmp_key_with_cur routines to take advantage of cmp_int()
In reply to Fedor PchelkinThe net value of these functions is to determine the result of a three-way-comparison between operands of the same type.
Simplify the code using cmp_int() to eliminate potential errors with opencoded casts and subtractions. This also means we can change the return value type of cmp_key_with_cur routines from int64_t to int and make the interface a bit clearer.
Found by Linux Verification Center (linuxtesting.org).
---
v2: add R-b
fs/xfs/libxfs/xfs_alloc_btree.c | 15 ++++++--------- fs/xfs/libxfs/xfs_bmap_btree.c | 6 +++--- fs/xfs/libxfs/xfs_btree.h | 2 +- fs/xfs/libxfs/xfs_ialloc_btree.c | 6 +++--- fs/xfs/libxfs/xfs_refcount_btree.c | 4 ++-- fs/xfs/libxfs/xfs_rmap_btree.c | 26 +++++--------------------- fs/xfs/libxfs/xfs_rtrefcount_btree.c | 4 ++-- fs/xfs/libxfs/xfs_rtrmap_btree.c | 26 +++++--------------------- fs/xfs/scrub/rcbag_btree.c | 15 +++------------ 9 files changed, 30 insertions(+), 74 deletions(-)
fs/xfs/libxfs/xfs_alloc_btree.c +6 -9
@@ -186,7 +186,7 @@ xfs_allocbt_init_ptr_from_cur( ptr->s = agf->agf_cnt_root; } -STATIC int64_t +STATIC intxfs_bnobt_cmp_key_with_cur (struct xfs_btree_cur *cur,const union xfs_btree_key *key)
@@ -194,23 +194,20 @@ xfs_bnobt_cmp_key_with_cur(struct xfs_alloc_rec_incore *rec = &cur->bc_rec.a;const struct xfs_alloc_rec *kp = &key->alloc; -return (int64_t )be32_to_cpu (kp->ar_startblock) - rec->ar_startblock; +return cmp_int (be32_to_cpu (kp->ar_startblock), + rec->ar_startblock); } -STATIC int64_t +STATIC intxfs_cntbt_cmp_key_with_cur (struct xfs_btree_cur *cur,const union xfs_btree_key *key) {struct xfs_alloc_rec_incore *rec = &cur->bc_rec.a;const struct xfs_alloc_rec *kp = &key->alloc; -int64_t diff; - - diff = (int64_t )be32_to_cpu (kp->ar_blockcount) - rec->ar_blockcount; -if (diff) -return diff; -return (int64_t )be32_to_cpu (kp->ar_startblock) - rec->ar_startblock; +return cmp_int (be32_to_cpu (kp->ar_blockcount), rec->ar_blockcount) ?: +cmp_int (be32_to_cpu (kp->ar_startblock), rec->ar_startblock); }STATIC int
fs/xfs/libxfs/xfs_bmap_btree.c +3 -3
@@ -369,13 +369,13 @@ xfs_bmbt_init_rec_from_cur(xfs_bmbt_disk_set_all (&rec->bmbt, &cur->bc_rec.b); } -STATIC int64_t +STATIC intxfs_bmbt_cmp_key_with_cur (struct xfs_btree_cur *cur,const union xfs_btree_key *key) { -return (int64_t )be64_to_cpu (key->bmbt.br_startoff) - - cur->bc_rec.b.br_startoff; +return cmp_int (be64_to_cpu (key->bmbt.br_startoff), + cur->bc_rec.b.br_startoff); }STATIC int
fs/xfs/libxfs/xfs_btree.h +1 -1
@@ -175,7 +175,7 @@ struct xfs_btree_ops {* Compare key value and cursor value -- positive if key > cur, * negative if key < cur, and zero if equal. */ -int64_t (*cmp_key_with_cur)(struct xfs_btree_cur *cur, +int (*cmp_key_with_cur)(struct xfs_btree_cur *cur,const union xfs_btree_key *key);/*
fs/xfs/libxfs/xfs_ialloc_btree.c +3 -3
@@ -265,13 +265,13 @@ xfs_finobt_init_ptr_from_cur( ptr->s = agi->agi_free_root; } -STATIC int64_t +STATIC intxfs_inobt_cmp_key_with_cur (struct xfs_btree_cur *cur,const union xfs_btree_key *key) { -return (int64_t )be32_to_cpu (key->inobt.ir_startino) - - cur->bc_rec.i.ir_startino; +return cmp_int (be32_to_cpu (key->inobt.ir_startino), + cur->bc_rec.i.ir_startino); }STATIC int
fs/xfs/libxfs/xfs_refcount_btree.c +2 -2
@@ -174,7 +174,7 @@ xfs_refcountbt_init_ptr_from_cur( ptr->s = agf->agf_refcount_root; } -STATIC int64_t +STATIC intxfs_refcountbt_cmp_key_with_cur (struct xfs_btree_cur *cur,const union xfs_btree_key *key)
@@ -185,7 +185,7 @@ xfs_refcountbt_cmp_key_with_cur( start =xfs_refcount_encode_startblock (irec->rc_startblock, irec->rc_domain); -return (int64_t )be32_to_cpu (kp->rc_startblock) - start; +return cmp_int (be32_to_cpu (kp->rc_startblock), start); }STATIC int
fs/xfs/libxfs/xfs_rmap_btree.c +5 -21
@@ -243,34 +243,18 @@ static inline uint64_t offset_keymask(uint64_t offset)return offset & ~XFS_RMAP_OFF_UNWRITTEN ; } -STATIC int64_t +STATIC intxfs_rmapbt_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; -__u64 x, y; -int64_t d; - - d = (int64_t )be32_to_cpu (kp->rm_startblock) - rec->rm_startblock; -if (d) -return d; - x =be64_to_cpu (kp->rm_owner); - y = rec->rm_owner; -if (x > y) -return 1 ; -else if (y > x) -return -1 ; - - x =offset_keymask (be64_to_cpu (kp->rm_offset)); - y =offset_keymask (xfs_rmap_irec_offset_pack (rec)); -if (x > y) -return 1 ; -else if (y > x) -return -1 ; -return 0 ; +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
fs/xfs/libxfs/xfs_rtrefcount_btree.c +2 -2
@@ -156,7 +156,7 @@ xfs_rtrefcountbt_init_ptr_from_cur( ptr->l =0 ; } -STATIC int64_t +STATIC intxfs_rtrefcountbt_cmp_key_with_cur (struct xfs_btree_cur *cur,const union xfs_btree_key *key)
@@ -167,7 +167,7 @@ xfs_rtrefcountbt_cmp_key_with_cur( start =xfs_refcount_encode_startblock (irec->rc_startblock, irec->rc_domain); -return (int64_t )be32_to_cpu (kp->rc_startblock) - start; +return cmp_int (be32_to_cpu (kp->rc_startblock), start); }STATIC int
fs/xfs/libxfs/xfs_rtrmap_btree.c +5 -21
@@ -185,34 +185,18 @@ static inline uint64_t offset_keymask(uint64_t offset)return offset & ~XFS_RMAP_OFF_UNWRITTEN ; } -STATIC int64_t +STATIC intxfs_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; -__u64 x, y; -int64_t d; - - d = (int64_t )be32_to_cpu (kp->rm_startblock) - rec->rm_startblock; -if (d) -return d; - x =be64_to_cpu (kp->rm_owner); - y = rec->rm_owner; -if (x > y) -return 1 ; -else if (y > x) -return -1 ; - - x =offset_keymask (be64_to_cpu (kp->rm_offset)); - y =offset_keymask (xfs_rmap_irec_offset_pack (rec)); -if (x > y) -return 1 ; -else if (y > x) -return -1 ; -return 0 ; +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
fs/xfs/scrub/rcbag_btree.c +3 -12
@@ -47,7 +47,7 @@ rcbagbt_init_rec_from_cur( bag_rec->rbg_refcount = bag_irec->rbg_refcount; } -STATIC int64_t +STATIC intrcbagbt_cmp_key_with_cur (struct xfs_btree_cur *cur,const union xfs_btree_key *key)
@@ -55,17 +55,8 @@ rcbagbt_cmp_key_with_cur(struct rcbag_rec *rec = (struct rcbag_rec *)&cur->bc_rec;const struct rcbag_key *kp = (const struct rcbag_key *)key; -if (kp->rbg_startblock > rec->rbg_startblock) -return 1 ; -if (kp->rbg_startblock < rec->rbg_startblock) -return -1 ; - -if (kp->rbg_blockcount > rec->rbg_blockcount) -return 1 ; -if (kp->rbg_blockcount < rec->rbg_blockcount) -return -1 ; - -return 0 ; +return cmp_int (kp->rbg_startblock, rec->rbg_startblock) ?: +cmp_int (kp->rbg_blockcount, rec->rbg_blockcount); }STATIC int
Signature · 1 line
2.50.0
PATCH v2 5/6 xfs: use a proper variable name and type for storing a comparison result
In reply to Fedor PchelkinPerhaps that's just my silly imagination but 'diff' doesn't look good for the name of a variable to hold a result of a three-way-comparison (-1, 0, 1) which is what ->cmp_key_with_cur() does. It implies to contain an actual difference between the two integer variables but that's not true anymore after recent refactoring.
Declaring it as int64_t is also misleading now. Plain integer type is more than enough.
Found by Linux Verification Center (linuxtesting.org).
---
v2: add R-b
fs/xfs/libxfs/xfs_btree.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-)
fs/xfs/libxfs/xfs_btree.c +12 -11
@@ -1985,7 +1985,7 @@ xfs_btree_lookup( int *stat) /* success/failure */ {struct xfs_btree_block *block;/* current btree block */ -int64_t diff;/* difference for the current key */ +int cmp_r;/* current key comparison result */ int error;/* error return value */ int keyno;/* current key number */ int level;/* level in the btree */
@@ -2013,13 +2013,13 @@ xfs_btree_lookup(* on the lookup record, then follow the corresponding block * pointer down to the next level. */ -for (level = cur->bc_nlevels -1 , diff =1 ; level >=0 ; level--) { +for (level = cur->bc_nlevels -1 , cmp_r =1 ; level >=0 ; level--) {/* Get the block we need to do the lookup on. */ error =xfs_btree_lookup_get_block (cur, level, pp, &block);if (error) goto error0; -if (diff ==0 ) { +if (cmp_r ==0 ) {/* * If we already had a key match at a higher level, we * know we need to use the first entry in this block.
@@ -2065,15 +2065,16 @@ xfs_btree_lookup( keyno, block, &key);/* -* Compute difference to get next direction: +* Compute comparison result to get next +* direction: * - less than, move right * - greater than, move left * - equal, we're done */ - diff = cur->bc_ops->cmp_key_with_cur (cur, kp); -if (diff <0 ) + cmp_r = cur->bc_ops->cmp_key_with_cur (cur, kp); +if (cmp_r <0 ) low = keyno +1 ; -else if (diff >0 ) +else if (cmp_r >0 ) high = keyno -1 ;else break ;
@@ -2089,7 +2090,7 @@ xfs_btree_lookup(* If we moved left, need the previous key number, * unless there isn't one. */ -if (diff >0 && --keyno <1 ) +if (cmp_r >0 && --keyno <1 ) keyno =1 ; pp =xfs_btree_ptr_addr (cur, keyno, block);
@@ -2102,7 +2103,7 @@ xfs_btree_lookup( }/* Done with the search. See if we need to adjust the results. */ -if (dir !=XFS_LOOKUP_LE && diff <0 ) { +if (dir !=XFS_LOOKUP_LE && cmp_r <0 ) { keyno++;/* * If ge search and we went off the end of the block, but it's
@@ -2125,14 +2126,14 @@ xfs_btree_lookup( *stat =1 ;return 0 ; } - }else if (dir ==XFS_LOOKUP_LE && diff >0 ) + }else if (dir ==XFS_LOOKUP_LE && cmp_r >0 ) keyno--; cur->bc_levels[0 ].ptr = keyno;/* Return if we succeeded or not. */ if (keyno ==0 || keyno >xfs_btree_get_numrecs (block)) *stat =0 ; -else if (dir !=XFS_LOOKUP_EQ || diff ==0 ) +else if (dir !=XFS_LOOKUP_EQ || cmp_r ==0 ) *stat =1 ;else *stat =0 ;
Signature · 1 line
2.50.0
PATCH v2 6/6 xfs: refactor xfs_btree_diff_two_ptrs() to take advantage of cmp_int()
In reply to Fedor PchelkinUse cmp_int() to yield the result of a three-way-comparison instead of performing subtractions with extra casts. Thus also rename the function to make its name clearer in purpose.
Found by Linux Verification Center (linuxtesting.org).
---
v2: rename the "diff_two_ptrs" part (Darrick)
fs/xfs/libxfs/xfs_btree.c | 8 ++++---- fs/xfs/libxfs/xfs_btree.h | 6 +++--- fs/xfs/scrub/btree.c | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-)
fs/xfs/libxfs/xfs_btree.c +4 -4
@@ -5353,15 +5353,15 @@ xfs_btree_count_blocks( }/* Compare two btree pointers. */ -int64_t -xfs_btree_diff_two_ptrs ( +int +xfs_btree_cmp_two_ptrs (struct xfs_btree_cur *cur,const union xfs_btree_ptr *a,const union xfs_btree_ptr *b) {if (cur->bc_ops->ptr_len ==XFS_BTREE_LONG_PTR_LEN ) -return (int64_t )be64_to_cpu (a->l) -be64_to_cpu (b->l); -return (int64_t )be32_to_cpu (a->s) -be32_to_cpu (b->s); +return cmp_int (be64_to_cpu (a->l),be64_to_cpu (b->l)); +return cmp_int (be32_to_cpu (a->s),be32_to_cpu (b->s)); }struct xfs_btree_has_records {
fs/xfs/libxfs/xfs_btree.h +3 -3
@@ -519,9 +519,9 @@ struct xfs_btree_block *xfs_btree_get_block(struct xfs_btree_cur *cur, int level, struct xfs_buf **bpp);bool xfs_btree_ptr_is_null (struct xfs_btree_cur *cur,const union xfs_btree_ptr *ptr); -int64_t xfs_btree_diff_two_ptrs (struct xfs_btree_cur *cur, -const union xfs_btree_ptr *a, -const union xfs_btree_ptr *b); +int xfs_btree_cmp_two_ptrs (struct xfs_btree_cur *cur, +const union xfs_btree_ptr *a, +const union xfs_btree_ptr *b);void xfs_btree_get_sibling (struct xfs_btree_cur *cur,struct xfs_btree_block *block,union xfs_btree_ptr *ptr,int lr);
fs/xfs/scrub/btree.c +1 -1
@@ -306,7 +306,7 @@ xchk_btree_block_check_sibling(if (pbp)xchk_buffer_recheck (bs->sc, pbp); -if (xfs_btree_diff_two_ptrs (cur, pp, sibling)) +if (xfs_btree_cmp_two_ptrs (cur, pp, sibling))xchk_btree_set_corrupt (bs->sc, cur, level); out:xfs_btree_del_cursor (ncur,XFS_BTREE_ERROR );
Signature · 1 line
2.50.0
Re: [PATCH v2 6/6] xfs: refactor xfs_btree_diff_two_ptrs() to take advantage of cmp_int()
Quoted 10 lines · Fedor Pchelkin
Use
cmp_int()to yield the result of a three-way-comparison instead of performing subtractions with extra casts. Thus also rename the function to make its name clearer in purpose.Found by Linux Verification Center (linuxtesting.org).
Signed-off-by Fedor Pchelkin <[email protected]>---
v2: rename the "
diff_two_ptrs" part (Darrick)
Looks good now, thanks for the cmp_int cleanups!
--D
Quoted 64 lines
fs/xfs/libxfs/xfs_btree.c | 8 ++++---- fs/xfs/libxfs/xfs_btree.h | 6 +++--- fs/xfs/scrub/btree.c | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-)fs/xfs/libxfs/xfs_btree.c +4
-4@@ -5353,15 +5353,15 @@ xfs_btree_count_blocks( }/* Compare two btree pointers. */ -int64_t -xfs_btree_diff_two_ptrs ( +int +xfs_btree_cmp_two_ptrs (struct xfs_btree_cur *cur,const union xfs_btree_ptr *a,const union xfs_btree_ptr *b) {if (cur->bc_ops->ptr_len ==XFS_BTREE_LONG_PTR_LEN ) -return (int64_t )be64_to_cpu (a->l) -be64_to_cpu (b->l); -return (int64_t )be32_to_cpu (a->s) -be32_to_cpu (b->s); +return cmp_int (be64_to_cpu (a->l),be64_to_cpu (b->l)); +return cmp_int (be32_to_cpu (a->s),be32_to_cpu (b->s)); }struct xfs_btree_has_records {fs/xfs/libxfs/xfs_btree.h +3
-3@@ -519,9 +519,9 @@ struct xfs_btree_block *xfs_btree_get_block(struct xfs_btree_cur *cur, int level, struct xfs_buf **bpp);bool xfs_btree_ptr_is_null (struct xfs_btree_cur *cur,const union xfs_btree_ptr *ptr); -int64_t xfs_btree_diff_two_ptrs (struct xfs_btree_cur *cur, -const union xfs_btree_ptr *a, -const union xfs_btree_ptr *b); +int xfs_btree_cmp_two_ptrs (struct xfs_btree_cur *cur, +const union xfs_btree_ptr *a, +const union xfs_btree_ptr *b);void xfs_btree_get_sibling (struct xfs_btree_cur *cur,struct xfs_btree_block *block,union xfs_btree_ptr *ptr,int lr);fs/xfs/scrub/btree.c +1
-1@@ -306,7 +306,7 @@ xchk_btree_block_check_sibling(if (pbp)xchk_buffer_recheck (bs->sc, pbp); -if (xfs_btree_diff_two_ptrs (cur, pp, sibling)) +if (xfs_btree_cmp_two_ptrs (cur, pp, sibling))xchk_btree_set_corrupt (bs->sc, cur, level); out:xfs_btree_del_cursor (ncur,XFS_BTREE_ERROR );Signature · 1 line
2.50.0
Quoted 8 lines · Fedor Pchelkin
Key comparing routines are currently opencoded with extra casts and subtractions which is error prone and can be replaced with a neat
cmp_int()helper which is now in a generic header file.Started from:
https://lore.kernel.org/linux-xfs/[email protected]/T/#u[...]
Applied to for-next, thanks!
[1/6] xfs: rename diff_two_keys routines
commit: c4c6aee6ba87fdfbaeed1966f81298e3f3913f3c
[2/6] xfs: rename key_diff routines
commit: fb7eff8c9f1b7ac46b8e376f9da19d8996cd0262
[3/6] xfs: refactor cmp_two_keys routines to take advantage of cmp_int()
commit: ff48e83c9dcd8c4f6b92b7d695ebe599c0a0e57d
[4/6] xfs: refactor cmp_key_with_cur routines to take advantage of cmp_int()
commit: 786b3b2e16600a642ddf15bcaf94694cfc6b3250
[5/6] xfs: use a proper variable name and type for storing a comparison result
commit: a0b2b28e1cc1c3c95bd889eec455bf8e68def61d
[6/6] xfs: refactor xfs_btree_diff_two_ptrs() to take advantage of cmp_int()
commit: 10e4f0aebdadc94930e82ac13ef1e01abb0bda03Best regards,
Signature · 1 line
Carlos Maiolino <[email protected]>