]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
xfs: refactor cmp_key_with_cur routines to take advantage of cmp_int()
authorFedor Pchelkin <pchelkin@ispras.ru>
Mon, 6 Oct 2025 12:40:16 +0000 (14:40 +0200)
committerAndrey Albershteyn <aalbersh@kernel.org>
Mon, 13 Oct 2025 09:53:39 +0000 (11:53 +0200)
Source kernel commit: 734b871d6cf7d4f815bb1eff8c808289079701c2

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>
Signed-off-by: Andrey Albershteyn <aalbersh@kernel.org>
Reviewed-by: "Darrick J. Wong" <djwong@kernel.org>
libxfs/xfs_alloc_btree.c
libxfs/xfs_bmap_btree.c
libxfs/xfs_btree.h
libxfs/xfs_ialloc_btree.c
libxfs/xfs_refcount_btree.c
libxfs/xfs_rmap_btree.c
libxfs/xfs_rtrefcount_btree.c
libxfs/xfs_rtrmap_btree.c
repair/rcbag_btree.c

index c3c69a9de398db48d297743cab010249bb4a1dde..1604e1bb6251168a6cf88faeae87eee589f72471 100644 (file)
@@ -184,7 +184,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)
@@ -192,23 +192,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 19eab66fad26f8e2a6e11baf9ae4b47f10294c82..252da347b06e158f67b7e353f4ecf9406e5481d6 100644 (file)
@@ -368,13 +368,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 973ae62d39cf9eabe16de125846ce977b9dcb172..dab9b61bd2006f11fd48c604daf984fc3ab89a28 100644 (file)
@@ -264,13 +264,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 668c788dca796b2ea28f2fff0de93d4034ef5646..44d942e9d098e171a2cff4dfb6a3a4589d569520 100644 (file)
@@ -173,7 +173,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)
@@ -184,7 +184,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 ab207b9cc2d0b695b06bccae15e6a1124019b2fe..d7b9fccc3a0f5d5458ab57e0bddb9341e51d8c73 100644 (file)
@@ -242,34 +242,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 7fbbc6387c6e9e0e854bcf46b63ce7b66ad10c1f..77191f073a10bbce67f723e68ad3af95da18694b 100644 (file)
@@ -154,7 +154,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)
@@ -165,7 +165,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 0492cd55d5034f9a16481c6cedba9e0f72450c1a..633dca0333f4e71b35cb14023e72f71cc52750d1 100644 (file)
@@ -184,34 +184,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 8a50bd03a55360e725490802b6c2f9a5345b0231..86a28f2ab874fe51ebb4304b9e91e1c3650a8d9c 100644 (file)
@@ -46,30 +46,24 @@ 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)
 {
        struct rcbag_rec                *rec = (struct rcbag_rec *)&cur->bc_rec;
        const struct rcbag_key          *kp = (const struct rcbag_key *)key;
+       int                             d;
 
-       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;
+       d = cmp_int(kp->rbg_startblock, rec->rbg_startblock);
+       if (d)
+               return d;
 
-       if (kp->rbg_ino > rec->rbg_ino)
-               return 1;
-       if (kp->rbg_ino < rec->rbg_ino)
-               return -1;
+       d = cmp_int(kp->rbg_blockcount, rec->rbg_blockcount);
+       if (d)
+               return d;
 
-       return 0;
+       return cmp_int(kp->rbg_ino, rec->rbg_ino);
 }
 
 STATIC int