bcachefs: Fix a btree iter usage error
authorKent Overstreet <kent.overstreet@gmail.com>
Thu, 29 Nov 2018 08:24:06 +0000 (03:24 -0500)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 22 Oct 2023 21:08:12 +0000 (17:08 -0400)
previously, if the code traversed to the next btree node, that could
return an error (due to lock restarts) - which was not being checked
for.

fix is to rework it so it never iterates past the current leaf node, and
pops an assertion if it ever sees an error.

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

index 40d3f02d86d89b64351e865bc3a063cbf99ea588..e7d7c5fe6db7ff920a82dfd68873d1a09449eaa6 100644 (file)
@@ -242,9 +242,15 @@ static s64 sum_sector_overwrites(struct bkey_i *new, struct btree_iter *_iter,
        bch2_btree_iter_link(_iter, &iter);
        bch2_btree_iter_copy(&iter, _iter);
 
-       for_each_btree_key_continue(&iter, BTREE_ITER_SLOTS, old) {
-               if (bkey_cmp(new->k.p, bkey_start_pos(old.k)) <= 0)
-                       break;
+       old = bch2_btree_iter_peek_slot(&iter);
+
+       while (1) {
+               /*
+                * should not be possible to get an error here, since we're
+                * carefully not advancing past @new and thus whatever leaf node
+                * @_iter currently points to:
+                */
+               BUG_ON(btree_iter_err(old));
 
                if (allocating &&
                    !bch2_extent_is_fully_allocated(old))
@@ -256,6 +262,11 @@ static s64 sum_sector_overwrites(struct bkey_i *new, struct btree_iter *_iter,
                              bkey_start_offset(old.k))) *
                        (bkey_extent_is_allocation(&new->k) -
                         bkey_extent_is_allocation(old.k));
+
+               if (bkey_cmp(old.k->p, new->k.p) >= 0)
+                       break;
+
+               old = bch2_btree_iter_next_slot(&iter);
        }
 
        bch2_btree_iter_unlink(&iter);