]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
xfs: refactor cmp_key_with_cur routines to take advantage of cmp_int()
authorFedor Pchelkin <pchelkin@ispras.ru>
Wed, 2 Jul 2025 09:39:31 +0000 (12:39 +0300)
committerCarlos Maiolino <cem@kernel.org>
Thu, 24 Jul 2025 15:30:13 +0000 (17:30 +0200)
The 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).

Suggested-by: Darrick J. Wong <djwong@kernel.org>
Signed-off-by: Fedor Pchelkin <pchelkin@ispras.ru>
Reviewed-by: Darrick J. Wong <djwong@kernel.org>
Signed-off-by: Carlos Maiolino <cem@kernel.org>
fs/xfs/libxfs/xfs_alloc_btree.c
fs/xfs/libxfs/xfs_bmap_btree.c
fs/xfs/libxfs/xfs_btree.h
fs/xfs/libxfs/xfs_ialloc_btree.c
fs/xfs/libxfs/xfs_refcount_btree.c
fs/xfs/libxfs/xfs_rmap_btree.c
fs/xfs/libxfs/xfs_rtrefcount_btree.c
fs/xfs/libxfs/xfs_rtrmap_btree.c
fs/xfs/scrub/rcbag_btree.c

index f371f1b32cfbe9ebe558dfb3c08d4ed071fdbeee..fa1f03c1331e1ca46bc47b4db92550bcf730a760 100644 (file)
@@ -186,7 +186,7 @@ xfs_allocbt_init_ptr_from_cur(
                ptr->s = agf->agf_cnt_root;
 }
 
-STATIC int64_t
+STATIC int
 xfs_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 int
 xfs_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
index bfe67e5d4d112bf949a87a9f0775fbffd4aaa825..188feac04b6031f49900fd21636c852194140c8d 100644 (file)
@@ -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 int
 xfs_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
index fecd9f0b93984acc41c3273c3ccd5d1a7e55b329..1bf20d509ac99fd609c24f2a388eff679e034db2 100644 (file)
@@ -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);
 
        /*
index ab9fce20b083876dd5dda9948fe60967d5f20537..100afdd66cdd5743d69bc5bbd5cc234e0ed1ec3d 100644 (file)
@@ -265,13 +265,13 @@ xfs_finobt_init_ptr_from_cur(
        ptr->s = agi->agi_free_root;
 }
 
-STATIC int64_t
+STATIC int
 xfs_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
index 1c3996b11563d3eb545b78f01594fb8cfa39817e..06da3ca14727a162267040169ff123a2051f1fd6 100644 (file)
@@ -174,7 +174,7 @@ xfs_refcountbt_init_ptr_from_cur(
        ptr->s = agf->agf_refcount_root;
 }
 
-STATIC int64_t
+STATIC int
 xfs_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
index 3cccdb0d04182a3a2a4ae3c9369f9c84f68e17b1..bf16aee50d732d62a4c28489ff8fdcf63b1b7d34 100644 (file)
@@ -243,34 +243,18 @@ static inline uint64_t offset_keymask(uint64_t offset)
        return offset & ~XFS_RMAP_OFF_UNWRITTEN;
 }
 
-STATIC int64_t
+STATIC int
 xfs_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
index d9f79ae579c6950b8f2a48f19f9233f4a7655017..ac11e94b42ae3d1e38a94780a85a8f425a289c62 100644 (file)
@@ -156,7 +156,7 @@ xfs_rtrefcountbt_init_ptr_from_cur(
        ptr->l = 0;
 }
 
-STATIC int64_t
+STATIC int
 xfs_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
index 231a189ea2fe6889883ef083b97a2e26cf782a00..55f903165769578fee140de3ecd21f97fd6f8e5a 100644 (file)
@@ -185,34 +185,18 @@ static inline uint64_t offset_keymask(uint64_t offset)
        return offset & ~XFS_RMAP_OFF_UNWRITTEN;
 }
 
-STATIC int64_t
+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;
-       __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
index 46598817b239185710b66b6033d546162f04d7f9..9a4ef823c5a7f388ef9b3bd269d66d4c5e8f4a79 100644 (file)
@@ -47,7 +47,7 @@ rcbagbt_init_rec_from_cur(
        bag_rec->rbg_refcount = bag_irec->rbg_refcount;
 }
 
-STATIC int64_t
+STATIC int
 rcbagbt_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