Commit 3b583adf authored by Fedor Pchelkin's avatar Fedor Pchelkin Committed by Carlos Maiolino
Browse files

xfs: refactor cmp_two_keys routines to take advantage of cmp_int()



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_two_keys routines from int64_t to int and make the
interface a bit clearer.

Found by Linux Verification Center (linuxtesting.org).

Suggested-by: default avatarDarrick J. Wong <djwong@kernel.org>
Signed-off-by: default avatarFedor Pchelkin <pchelkin@ispras.ru>
Reviewed-by: default avatarDarrick J. Wong <djwong@kernel.org>
Signed-off-by: default avatarCarlos Maiolino <cem@kernel.org>
parent 82b63ee1
Loading
Loading
Loading
Loading
+8 −13
Original line number Diff line number Diff line
@@ -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 int
xfs_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
+3 −15
Original line number Diff line number Diff line
@@ -378,29 +378,17 @@ xfs_bmbt_cmp_key_with_cur(
				      cur->bc_rec.b.br_startoff;
}

STATIC int64_t
STATIC int
xfs_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
+1 −1
Original line number Diff line number Diff line
@@ -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);
+3 −3
Original line number Diff line number Diff line
@@ -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
+3 −3
Original line number Diff line number Diff line
@@ -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
Loading