bcachefs: Fix race between btree writes and metadata drop
authorKent Overstreet <kent.overstreet@linux.dev>
Wed, 29 Nov 2023 00:26:23 +0000 (19:26 -0500)
committerKent Overstreet <kent.overstreet@linux.dev>
Wed, 29 Nov 2023 03:58:22 +0000 (22:58 -0500)
btree writes update the btree node key after every write, in order to
update sectors_written, and they also might need to drop pointers if one
of the writes failed in a replicated btree node.

But the btree node might also have had a pointer dropped while the write
was in flight, by bch2_dev_metadata_drop(), and thus there was a bug
where the btree node write would ovewrite the btree node's key with what
it had at the start of the write.

Fix this by dropping pointers not currently in the btree node key.

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

index d68fb01944b14ca1fa63d283c5c28ac8641789c2..6697417273aa14e7c5de09fbc1622c84eff24949 100644 (file)
@@ -2270,6 +2270,10 @@ int bch2_btree_node_update_key_get_iter(struct btree_trans *trans,
 
        BUG_ON(!btree_node_hashed(b));
 
+       struct bch_extent_ptr *ptr;
+       bch2_bkey_drop_ptrs(bkey_i_to_s(new_key), ptr,
+                           !bch2_bkey_has_device(bkey_i_to_s(&b->key), ptr->dev));
+
        ret = bch2_btree_node_update_key(trans, &iter, b, new_key,
                                         commit_flags, skip_triggers);
 out: