Skip to content

Commit bd7e795

Browse files
author
Darrick J. Wong
committed
xfs: refactor ->diff_two_keys callsites
Create wrapper functions around ->diff_two_keys so that we don't have to remember what the return values mean, and adjust some of the code comments to reflect the longtime code behavior. We're going to introduce more uses of ->diff_two_keys in the next patch, so reduce the cognitive load for readers by doing this refactoring now. Suggested-by: Dave Chinner <david@fromorbit.com> Signed-off-by: Darrick J. Wong <djwong@kernel.org> Reviewed-by: Dave Chinner <dchinner@redhat.com>
1 parent ee5fe8f commit bd7e795

3 files changed

Lines changed: 91 additions & 45 deletions

File tree

fs/xfs/libxfs/xfs_btree.c

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2067,8 +2067,7 @@ xfs_btree_get_leaf_keys(
20672067
for (n = 2; n <= xfs_btree_get_numrecs(block); n++) {
20682068
rec = xfs_btree_rec_addr(cur, n, block);
20692069
cur->bc_ops->init_high_key_from_rec(&hkey, rec);
2070-
if (cur->bc_ops->diff_two_keys(cur, &hkey, &max_hkey)
2071-
> 0)
2070+
if (xfs_btree_keycmp_gt(cur, &hkey, &max_hkey))
20722071
max_hkey = hkey;
20732072
}
20742073

@@ -2096,7 +2095,7 @@ xfs_btree_get_node_keys(
20962095
max_hkey = xfs_btree_high_key_addr(cur, 1, block);
20972096
for (n = 2; n <= xfs_btree_get_numrecs(block); n++) {
20982097
hkey = xfs_btree_high_key_addr(cur, n, block);
2099-
if (cur->bc_ops->diff_two_keys(cur, hkey, max_hkey) > 0)
2098+
if (xfs_btree_keycmp_gt(cur, hkey, max_hkey))
21002099
max_hkey = hkey;
21012100
}
21022101

@@ -2183,8 +2182,8 @@ __xfs_btree_updkeys(
21832182
nlkey = xfs_btree_key_addr(cur, ptr, block);
21842183
nhkey = xfs_btree_high_key_addr(cur, ptr, block);
21852184
if (!force_all &&
2186-
!(cur->bc_ops->diff_two_keys(cur, nlkey, lkey) != 0 ||
2187-
cur->bc_ops->diff_two_keys(cur, nhkey, hkey) != 0))
2185+
xfs_btree_keycmp_eq(cur, nlkey, lkey) &&
2186+
xfs_btree_keycmp_eq(cur, nhkey, hkey))
21882187
break;
21892188
xfs_btree_copy_keys(cur, nlkey, lkey, 1);
21902189
xfs_btree_log_keys(cur, bp, ptr, ptr);
@@ -4716,7 +4715,6 @@ xfs_btree_simple_query_range(
47164715
{
47174716
union xfs_btree_rec *recp;
47184717
union xfs_btree_key rec_key;
4719-
int64_t diff;
47204718
int stat;
47214719
bool firstrec = true;
47224720
int error;
@@ -4746,20 +4744,17 @@ xfs_btree_simple_query_range(
47464744
if (error || !stat)
47474745
break;
47484746

4749-
/* Skip if high_key(rec) < low_key. */
4747+
/* Skip if low_key > high_key(rec). */
47504748
if (firstrec) {
47514749
cur->bc_ops->init_high_key_from_rec(&rec_key, recp);
47524750
firstrec = false;
4753-
diff = cur->bc_ops->diff_two_keys(cur, low_key,
4754-
&rec_key);
4755-
if (diff > 0)
4751+
if (xfs_btree_keycmp_gt(cur, low_key, &rec_key))
47564752
goto advloop;
47574753
}
47584754

4759-
/* Stop if high_key < low_key(rec). */
4755+
/* Stop if low_key(rec) > high_key. */
47604756
cur->bc_ops->init_key_from_rec(&rec_key, recp);
4761-
diff = cur->bc_ops->diff_two_keys(cur, &rec_key, high_key);
4762-
if (diff > 0)
4757+
if (xfs_btree_keycmp_gt(cur, &rec_key, high_key))
47634758
break;
47644759

47654760
/* Callback */
@@ -4813,8 +4808,6 @@ xfs_btree_overlapped_query_range(
48134808
union xfs_btree_key *hkp;
48144809
union xfs_btree_rec *recp;
48154810
struct xfs_btree_block *block;
4816-
int64_t ldiff;
4817-
int64_t hdiff;
48184811
int level;
48194812
struct xfs_buf *bp;
48204813
int i;
@@ -4854,25 +4847,23 @@ xfs_btree_overlapped_query_range(
48544847
block);
48554848

48564849
cur->bc_ops->init_high_key_from_rec(&rec_hkey, recp);
4857-
ldiff = cur->bc_ops->diff_two_keys(cur, &rec_hkey,
4858-
low_key);
4859-
48604850
cur->bc_ops->init_key_from_rec(&rec_key, recp);
4861-
hdiff = cur->bc_ops->diff_two_keys(cur, high_key,
4862-
&rec_key);
48634851

48644852
/*
4853+
* If (query's high key < record's low key), then there
4854+
* are no more interesting records in this block. Pop
4855+
* up to the leaf level to find more record blocks.
4856+
*
48654857
* If (record's high key >= query's low key) and
48664858
* (query's high key >= record's low key), then
48674859
* this record overlaps the query range; callback.
48684860
*/
4869-
if (ldiff >= 0 && hdiff >= 0) {
4861+
if (xfs_btree_keycmp_lt(cur, high_key, &rec_key))
4862+
goto pop_up;
4863+
if (xfs_btree_keycmp_ge(cur, &rec_hkey, low_key)) {
48704864
error = fn(cur, recp, priv);
48714865
if (error)
48724866
break;
4873-
} else if (hdiff < 0) {
4874-
/* Record is larger than high key; pop. */
4875-
goto pop_up;
48764867
}
48774868
cur->bc_levels[level].ptr++;
48784869
continue;
@@ -4884,15 +4875,18 @@ xfs_btree_overlapped_query_range(
48844875
block);
48854876
pp = xfs_btree_ptr_addr(cur, cur->bc_levels[level].ptr, block);
48864877

4887-
ldiff = cur->bc_ops->diff_two_keys(cur, hkp, low_key);
4888-
hdiff = cur->bc_ops->diff_two_keys(cur, high_key, lkp);
4889-
48904878
/*
4879+
* If (query's high key < pointer's low key), then there are no
4880+
* more interesting keys in this block. Pop up one leaf level
4881+
* to continue looking for records.
4882+
*
48914883
* If (pointer's high key >= query's low key) and
48924884
* (query's high key >= pointer's low key), then
48934885
* this record overlaps the query range; follow pointer.
48944886
*/
4895-
if (ldiff >= 0 && hdiff >= 0) {
4887+
if (xfs_btree_keycmp_lt(cur, high_key, lkp))
4888+
goto pop_up;
4889+
if (xfs_btree_keycmp_ge(cur, hkp, low_key)) {
48964890
level--;
48974891
error = xfs_btree_lookup_get_block(cur, level, pp,
48984892
&block);
@@ -4907,9 +4901,6 @@ xfs_btree_overlapped_query_range(
49074901
#endif
49084902
cur->bc_levels[level].ptr = 1;
49094903
continue;
4910-
} else if (hdiff < 0) {
4911-
/* The low key is larger than the upper range; pop. */
4912-
goto pop_up;
49134904
}
49144905
cur->bc_levels[level].ptr++;
49154906
}
@@ -4971,8 +4962,8 @@ xfs_btree_query_range(
49714962
xfs_btree_key_from_irec(cur, &high_key, high_rec);
49724963
xfs_btree_key_from_irec(cur, &low_key, low_rec);
49734964

4974-
/* Enforce low key < high key. */
4975-
if (cur->bc_ops->diff_two_keys(cur, &low_key, &high_key) > 0)
4965+
/* Enforce low key <= high key. */
4966+
if (!xfs_btree_keycmp_le(cur, &low_key, &high_key))
49764967
return -EINVAL;
49774968

49784969
if (!(cur->bc_flags & XFS_BTREE_OVERLAPPING))

fs/xfs/libxfs/xfs_btree.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,61 @@ int xfs_btree_has_record(struct xfs_btree_cur *cur,
546546
bool xfs_btree_has_more_records(struct xfs_btree_cur *cur);
547547
struct xfs_ifork *xfs_btree_ifork_ptr(struct xfs_btree_cur *cur);
548548

549+
/* Key comparison helpers */
550+
static inline bool
551+
xfs_btree_keycmp_lt(
552+
struct xfs_btree_cur *cur,
553+
const union xfs_btree_key *key1,
554+
const union xfs_btree_key *key2)
555+
{
556+
return cur->bc_ops->diff_two_keys(cur, key1, key2) < 0;
557+
}
558+
559+
static inline bool
560+
xfs_btree_keycmp_gt(
561+
struct xfs_btree_cur *cur,
562+
const union xfs_btree_key *key1,
563+
const union xfs_btree_key *key2)
564+
{
565+
return cur->bc_ops->diff_two_keys(cur, key1, key2) > 0;
566+
}
567+
568+
static inline bool
569+
xfs_btree_keycmp_eq(
570+
struct xfs_btree_cur *cur,
571+
const union xfs_btree_key *key1,
572+
const union xfs_btree_key *key2)
573+
{
574+
return cur->bc_ops->diff_two_keys(cur, key1, key2) == 0;
575+
}
576+
577+
static inline bool
578+
xfs_btree_keycmp_le(
579+
struct xfs_btree_cur *cur,
580+
const union xfs_btree_key *key1,
581+
const union xfs_btree_key *key2)
582+
{
583+
return !xfs_btree_keycmp_gt(cur, key1, key2);
584+
}
585+
586+
static inline bool
587+
xfs_btree_keycmp_ge(
588+
struct xfs_btree_cur *cur,
589+
const union xfs_btree_key *key1,
590+
const union xfs_btree_key *key2)
591+
{
592+
return !xfs_btree_keycmp_lt(cur, key1, key2);
593+
}
594+
595+
static inline bool
596+
xfs_btree_keycmp_ne(
597+
struct xfs_btree_cur *cur,
598+
const union xfs_btree_key *key1,
599+
const union xfs_btree_key *key2)
600+
{
601+
return !xfs_btree_keycmp_eq(cur, key1, key2);
602+
}
603+
549604
/* Does this cursor point to the last block in the given level? */
550605
static inline bool
551606
xfs_btree_islastblock(

fs/xfs/scrub/btree.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -161,20 +161,20 @@ xchk_btree_rec(
161161
if (cur->bc_nlevels == 1)
162162
return;
163163

164-
/* Is this at least as large as the parent low key? */
164+
/* Is low_key(rec) at least as large as the parent low key? */
165165
cur->bc_ops->init_key_from_rec(&key, rec);
166166
keyblock = xfs_btree_get_block(cur, 1, &bp);
167167
keyp = xfs_btree_key_addr(cur, cur->bc_levels[1].ptr, keyblock);
168-
if (cur->bc_ops->diff_two_keys(cur, &key, keyp) < 0)
168+
if (xfs_btree_keycmp_lt(cur, &key, keyp))
169169
xchk_btree_set_corrupt(bs->sc, cur, 1);
170170

171171
if (!(cur->bc_flags & XFS_BTREE_OVERLAPPING))
172172
return;
173173

174-
/* Is this no larger than the parent high key? */
174+
/* Is high_key(rec) no larger than the parent high key? */
175175
cur->bc_ops->init_high_key_from_rec(&hkey, rec);
176176
keyp = xfs_btree_high_key_addr(cur, cur->bc_levels[1].ptr, keyblock);
177-
if (cur->bc_ops->diff_two_keys(cur, keyp, &hkey) < 0)
177+
if (xfs_btree_keycmp_lt(cur, keyp, &hkey))
178178
xchk_btree_set_corrupt(bs->sc, cur, 1);
179179
}
180180

@@ -209,20 +209,20 @@ xchk_btree_key(
209209
if (level + 1 >= cur->bc_nlevels)
210210
return;
211211

212-
/* Is this at least as large as the parent low key? */
212+
/* Is this block's low key at least as large as the parent low key? */
213213
keyblock = xfs_btree_get_block(cur, level + 1, &bp);
214214
keyp = xfs_btree_key_addr(cur, cur->bc_levels[level + 1].ptr, keyblock);
215-
if (cur->bc_ops->diff_two_keys(cur, key, keyp) < 0)
215+
if (xfs_btree_keycmp_lt(cur, key, keyp))
216216
xchk_btree_set_corrupt(bs->sc, cur, level);
217217

218218
if (!(cur->bc_flags & XFS_BTREE_OVERLAPPING))
219219
return;
220220

221-
/* Is this no larger than the parent high key? */
221+
/* Is this block's high key no larger than the parent high key? */
222222
key = xfs_btree_high_key_addr(cur, cur->bc_levels[level].ptr, block);
223223
keyp = xfs_btree_high_key_addr(cur, cur->bc_levels[level + 1].ptr,
224224
keyblock);
225-
if (cur->bc_ops->diff_two_keys(cur, keyp, key) < 0)
225+
if (xfs_btree_keycmp_lt(cur, keyp, key))
226226
xchk_btree_set_corrupt(bs->sc, cur, level);
227227
}
228228

@@ -557,7 +557,7 @@ xchk_btree_block_check_keys(
557557
parent_block = xfs_btree_get_block(cur, level + 1, &bp);
558558
parent_low_key = xfs_btree_key_addr(cur, cur->bc_levels[level + 1].ptr,
559559
parent_block);
560-
if (cur->bc_ops->diff_two_keys(cur, &block_key, parent_low_key)) {
560+
if (xfs_btree_keycmp_ne(cur, &block_key, parent_low_key)) {
561561
xchk_btree_set_corrupt(bs->sc, bs->cur, level);
562562
return;
563563
}
@@ -569,7 +569,7 @@ xchk_btree_block_check_keys(
569569
parent_high_key = xfs_btree_high_key_addr(cur,
570570
cur->bc_levels[level + 1].ptr, parent_block);
571571
block_high_key = xfs_btree_high_key_from_key(cur, &block_key);
572-
if (cur->bc_ops->diff_two_keys(cur, block_high_key, parent_high_key))
572+
if (xfs_btree_keycmp_ne(cur, block_high_key, parent_high_key))
573573
xchk_btree_set_corrupt(bs->sc, bs->cur, level);
574574
}
575575

@@ -661,7 +661,7 @@ xchk_btree_block_keys(
661661
parent_keys = xfs_btree_key_addr(cur, cur->bc_levels[level + 1].ptr,
662662
parent_block);
663663

664-
if (cur->bc_ops->diff_two_keys(cur, &block_keys, parent_keys) != 0)
664+
if (xfs_btree_keycmp_ne(cur, &block_keys, parent_keys))
665665
xchk_btree_set_corrupt(bs->sc, cur, 1);
666666

667667
if (!(cur->bc_flags & XFS_BTREE_OVERLAPPING))
@@ -672,7 +672,7 @@ xchk_btree_block_keys(
672672
high_pk = xfs_btree_high_key_addr(cur, cur->bc_levels[level + 1].ptr,
673673
parent_block);
674674

675-
if (cur->bc_ops->diff_two_keys(cur, high_bk, high_pk) != 0)
675+
if (xfs_btree_keycmp_ne(cur, high_bk, high_pk))
676676
xchk_btree_set_corrupt(bs->sc, cur, 1);
677677
}
678678

0 commit comments

Comments
 (0)