bcachefs: __bch2_insert_snapshot_whiteouts() refactoring

Now uses bch2_get_snapshot_overwrites(), and much shorter.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
This commit is contained in:
Kent Overstreet 2025-05-28 15:20:20 -04:00
parent 801cb2bd6c
commit cb6f5d0dec
2 changed files with 30 additions and 52 deletions

View File

@ -123,76 +123,44 @@ static int need_whiteout_for_snapshot(struct btree_trans *trans,
}
int __bch2_insert_snapshot_whiteouts(struct btree_trans *trans,
enum btree_id id,
struct bpos old_pos,
struct bpos new_pos)
enum btree_id btree, struct bpos pos,
snapshot_id_list *s)
{
struct bch_fs *c = trans->c;
struct btree_iter old_iter, new_iter = {};
struct bkey_s_c old_k, new_k;
snapshot_id_list s;
struct bkey_i *update;
int ret = 0;
if (!bch2_snapshot_has_children(c, old_pos.snapshot))
return 0;
darray_for_each(*s, id) {
pos.snapshot = *id;
darray_init(&s);
bch2_trans_iter_init(trans, &old_iter, id, old_pos,
BTREE_ITER_not_extents|
BTREE_ITER_all_snapshots);
while ((old_k = bch2_btree_iter_prev(trans, &old_iter)).k &&
!(ret = bkey_err(old_k)) &&
bkey_eq(old_pos, old_k.k->p)) {
struct bpos whiteout_pos =
SPOS(new_pos.inode, new_pos.offset, old_k.k->p.snapshot);
if (!bch2_snapshot_is_ancestor(c, old_k.k->p.snapshot, old_pos.snapshot) ||
snapshot_list_has_ancestor(c, &s, old_k.k->p.snapshot))
continue;
new_k = bch2_bkey_get_iter(trans, &new_iter, id, whiteout_pos,
BTREE_ITER_not_extents|
BTREE_ITER_intent);
ret = bkey_err(new_k);
struct btree_iter iter;
struct bkey_s_c k = bch2_bkey_get_iter(trans, &iter, btree, pos,
BTREE_ITER_not_extents|
BTREE_ITER_intent);
ret = bkey_err(k);
if (ret)
break;
if (new_k.k->type == KEY_TYPE_deleted) {
update = bch2_trans_kmalloc(trans, sizeof(struct bkey_i));
if (k.k->type == KEY_TYPE_deleted) {
struct bkey_i *update = bch2_trans_kmalloc(trans, sizeof(struct bkey_i));
ret = PTR_ERR_OR_ZERO(update);
if (ret)
if (ret) {
bch2_trans_iter_exit(trans, &iter);
break;
}
bkey_init(&update->k);
update->k.p = whiteout_pos;
update->k.p = pos;
update->k.type = KEY_TYPE_whiteout;
ret = bch2_trans_update(trans, &new_iter, update,
ret = bch2_trans_update(trans, &iter, update,
BTREE_UPDATE_internal_snapshot_node);
}
bch2_trans_iter_exit(trans, &new_iter);
bch2_trans_iter_exit(trans, &iter);
ret = snapshot_list_add(c, &s, old_k.k->p.snapshot);
if (ret)
break;
}
bch2_trans_iter_exit(trans, &new_iter);
bch2_trans_iter_exit(trans, &old_iter);
snapshot_id_list s2;
ret = bch2_get_snapshot_overwrites(trans, id, old_pos, &s2);
if (ret) {
darray_exit(&s);
return ret;
}
BUG_ON(s.nr != s2.nr);
BUG_ON(memcmp(s.data, s2.data, sizeof(s.data[0]) * s.nr));
darray_exit(&s2);
darray_exit(&s);
darray_exit(s);
return ret;
}

View File

@ -4,6 +4,7 @@
#include "btree_iter.h"
#include "journal.h"
#include "snapshot.h"
struct bch_fs;
struct btree;
@ -74,7 +75,7 @@ static inline int bch2_btree_delete_at_buffered(struct btree_trans *trans,
}
int __bch2_insert_snapshot_whiteouts(struct btree_trans *, enum btree_id,
struct bpos, struct bpos);
struct bpos, snapshot_id_list *);
/*
* For use when splitting extents in existing snapshots:
@ -88,11 +89,20 @@ static inline int bch2_insert_snapshot_whiteouts(struct btree_trans *trans,
struct bpos old_pos,
struct bpos new_pos)
{
BUG_ON(old_pos.snapshot != new_pos.snapshot);
if (!btree_type_has_snapshots(btree) ||
bkey_eq(old_pos, new_pos))
return 0;
return __bch2_insert_snapshot_whiteouts(trans, btree, old_pos, new_pos);
snapshot_id_list s;
int ret = bch2_get_snapshot_overwrites(trans, btree, old_pos, &s);
if (ret)
return ret;
return s.nr
? __bch2_insert_snapshot_whiteouts(trans, btree, new_pos, &s)
: 0;
}
int bch2_trans_update_extent_overwrite(struct btree_trans *, struct btree_iter *,