bcachefs: Fix bch2_get_key_or_hole()
authorKent Overstreet <kent.overstreet@linux.dev>
Tue, 28 Mar 2023 23:37:25 +0000 (19:37 -0400)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 22 Oct 2023 21:09:58 +0000 (17:09 -0400)
This fixes an off by one error, due to confusing closed vs. half open
intervals.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/alloc_background.c

index aea6caa791ea9112e31e09a8f40d183767a99d0a..e5cbb4bce1ee3c3338a0e2c96ad9d82646e39f7e 100644 (file)
@@ -962,10 +962,17 @@ struct bkey_s_c bch2_get_key_or_hole(struct btree_iter *iter, struct bpos end, s
                struct bpos next;
 
                bch2_trans_copy_iter(&iter2, iter);
-               k = bch2_btree_iter_peek_upto(&iter2,
-                               bkey_min(bkey_min(end,
-                                                 iter->path->l[0].b->key.k.p),
-                                                 POS(iter->pos.inode, iter->pos.offset + U32_MAX - 1)));
+
+               if (!bpos_eq(iter->path->l[0].b->key.k.p, SPOS_MAX))
+                       end = bkey_min(end, bpos_nosnap_successor(iter->path->l[0].b->key.k.p));
+
+               end = bkey_min(end, POS(iter->pos.inode, iter->pos.offset + U32_MAX - 1));
+
+               /*
+                * btree node min/max is a closed interval, upto takes a half
+                * open interval:
+                */
+               k = bch2_btree_iter_peek_upto(&iter2, end);
                next = iter2.pos;
                bch2_trans_iter_exit(iter->trans, &iter2);