diff options
author | Kent Overstreet <kent.overstreet@gmail.com> | 2021-03-04 16:26:19 -0500 |
---|---|---|
committer | Kent Overstreet <kent.overstreet@linux.dev> | 2023-10-22 17:08:55 -0400 |
commit | 61a19ce4255abd1133d4e7cd64a6cfa40d1f37fa (patch) | |
tree | 2440c784ca3b4d2c7ad6c1b41bdd17083475d017 /fs/bcachefs/bkey.h | |
parent | f020bfcdb058e4542a4682557e046a750dc71660 (diff) | |
download | lwn-61a19ce4255abd1133d4e7cd64a6cfa40d1f37fa.tar.gz lwn-61a19ce4255abd1133d4e7cd64a6cfa40d1f37fa.zip |
bcachefs: Fix bpos_diff()
Previously, bpos_diff() did not handle borrows correctly. Minor thing
considering how it was used, but worth fixing.
Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
Diffstat (limited to 'fs/bcachefs/bkey.h')
-rw-r--r-- | fs/bcachefs/bkey.h | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/fs/bcachefs/bkey.h b/fs/bcachefs/bkey.h index 25cb5e985109..77d9d871adfb 100644 --- a/fs/bcachefs/bkey.h +++ b/fs/bcachefs/bkey.h @@ -183,6 +183,37 @@ static inline struct bpos bpos_max(struct bpos l, struct bpos r) return bkey_cmp(l, r) > 0 ? l : r; } +#define sbb(a, b, borrow) \ +do { \ + typeof(a) d1, d2; \ + \ + d1 = a - borrow; \ + borrow = d1 > a; \ + \ + d2 = d1 - b; \ + borrow += d2 > d1; \ + a = d2; \ +} while (0) + +/* returns a - b: */ +static inline struct bpos bpos_sub(struct bpos a, struct bpos b) +{ + int borrow = 0; + + sbb(a.snapshot, b.snapshot, borrow); + sbb(a.offset, b.offset, borrow); + sbb(a.inode, b.inode, borrow); + return a; +} + +static inline struct bpos bpos_diff(struct bpos l, struct bpos r) +{ + if (bkey_cmp(l, r) > 0) + swap(l, r); + + return bpos_sub(r, l); +} + void bch2_bpos_swab(struct bpos *); void bch2_bkey_swab_key(const struct bkey_format *, struct bkey_packed *); |