From ad520141b155786800261cc7e02ec02f0afe2643 Mon Sep 17 00:00:00 2001 From: Kent Overstreet Date: Sat, 27 May 2023 23:19:13 -0400 Subject: bcachefs: Fix corruption with writeable snapshots When partially overwriting an extent in an older snapshot, the existing extent has to be split. If the existing extent was overwritten in a different (sibling) snapshot, we have to ensure that the split won't be visible in the sibling snapshot. data_update.c already has code for this, bch2_insert_snapshot_writeouts() - we just need to move it into btree_update_leaf.c and change bch2_trans_update_extent() to use it as well. Signed-off-by: Kent Overstreet --- fs/bcachefs/data_update.c | 94 +++-------------------------------------------- 1 file changed, 6 insertions(+), 88 deletions(-) (limited to 'fs/bcachefs/data_update.c') diff --git a/fs/bcachefs/data_update.c b/fs/bcachefs/data_update.c index ae7e60d6e583..c89ee14f8b6b 100644 --- a/fs/bcachefs/data_update.c +++ b/fs/bcachefs/data_update.c @@ -16,81 +16,6 @@ #include "subvolume.h" #include "trace.h" -static int insert_snapshot_whiteouts(struct btree_trans *trans, - enum btree_id id, - struct bpos old_pos, - struct bpos new_pos) -{ - struct bch_fs *c = trans->c; - struct btree_iter iter, iter2; - struct bkey_s_c k, k2; - snapshot_id_list s; - struct bkey_i *update; - int ret; - - if (!btree_type_has_snapshots(id)) - return 0; - - darray_init(&s); - - if (!bch2_snapshot_has_children(c, old_pos.snapshot)) - return 0; - - bch2_trans_iter_init(trans, &iter, id, old_pos, - BTREE_ITER_NOT_EXTENTS| - BTREE_ITER_ALL_SNAPSHOTS); - while (1) { - k = bch2_btree_iter_prev(&iter); - ret = bkey_err(k); - if (ret) - break; - - if (!k.k) - break; - - if (!bkey_eq(old_pos, k.k->p)) - break; - - if (bch2_snapshot_is_ancestor(c, k.k->p.snapshot, old_pos.snapshot) && - !snapshot_list_has_ancestor(c, &s, k.k->p.snapshot)) { - struct bpos whiteout_pos = new_pos; - - whiteout_pos.snapshot = k.k->p.snapshot; - - k2 = bch2_bkey_get_iter(trans, &iter2, id, whiteout_pos, - BTREE_ITER_NOT_EXTENTS| - BTREE_ITER_INTENT); - ret = bkey_err(k2); - - if (!ret && k2.k->type == KEY_TYPE_deleted) { - update = bch2_trans_kmalloc(trans, sizeof(struct bkey_i)); - ret = PTR_ERR_OR_ZERO(update); - if (ret) - break; - - bkey_init(&update->k); - update->k.p = whiteout_pos; - update->k.type = KEY_TYPE_whiteout; - - ret = bch2_trans_update(trans, &iter2, update, - BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE); - } - bch2_trans_iter_exit(trans, &iter2); - - if (ret) - break; - - ret = snapshot_list_add(c, &s, k.k->p.snapshot); - if (ret) - break; - } - } - bch2_trans_iter_exit(trans, &iter); - darray_exit(&s); - - return ret; -} - static void trace_move_extent_finish2(struct bch_fs *c, struct bkey_s_c k) { if (trace_move_extent_finish_enabled()) { @@ -327,19 +252,12 @@ restart_drop_extra_replicas: next_pos = insert->k.p; - if (!bkey_eq(bkey_start_pos(&insert->k), bkey_start_pos(k.k))) { - ret = insert_snapshot_whiteouts(trans, m->btree_id, k.k->p, - bkey_start_pos(&insert->k)); - if (ret) - goto err; - } - - if (!bkey_eq(insert->k.p, k.k->p)) { - ret = insert_snapshot_whiteouts(trans, m->btree_id, - k.k->p, insert->k.p); - if (ret) - goto err; - } + ret = bch2_insert_snapshot_whiteouts(trans, m->btree_id, + k.k->p, bkey_start_pos(&insert->k)) ?: + bch2_insert_snapshot_whiteouts(trans, m->btree_id, + k.k->p, insert->k.p); + if (ret) + goto err; ret = bch2_trans_update(trans, &iter, insert, BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?: -- cgit v1.2.3