Commit f2a53270 authored by Kent Overstreet's avatar Kent Overstreet

bcachefs: Fix insert_snapshot_whiteouts()

 - We were failing to set the key type on the whiteouts it was creating,
   oops.

 - Also, we need to create whiteouts when generating front splits, not
   just back splits.
Signed-off-by: default avatarKent Overstreet <kent.overstreet@linux.dev>
parent 19d65219
...@@ -21,9 +21,10 @@ static int insert_snapshot_whiteouts(struct btree_trans *trans, ...@@ -21,9 +21,10 @@ static int insert_snapshot_whiteouts(struct btree_trans *trans,
struct bpos new_pos) struct bpos new_pos)
{ {
struct bch_fs *c = trans->c; struct bch_fs *c = trans->c;
struct btree_iter iter; struct btree_iter iter, iter2;
struct bkey_s_c k; struct bkey_s_c k, k2;
snapshot_id_list s; snapshot_id_list s;
struct bkey_i *update;
int ret; int ret;
if (!btree_type_has_snapshots(id)) if (!btree_type_has_snapshots(id))
...@@ -31,10 +32,7 @@ static int insert_snapshot_whiteouts(struct btree_trans *trans, ...@@ -31,10 +32,7 @@ static int insert_snapshot_whiteouts(struct btree_trans *trans,
darray_init(&s); darray_init(&s);
if (bkey_eq(old_pos, new_pos)) if (!bch2_snapshot_has_children(c, old_pos.snapshot))
return 0;
if (!snapshot_t(c, old_pos.snapshot)->children[0])
return 0; return 0;
bch2_trans_iter_init(trans, &iter, id, old_pos, bch2_trans_iter_init(trans, &iter, id, old_pos,
...@@ -46,27 +44,39 @@ static int insert_snapshot_whiteouts(struct btree_trans *trans, ...@@ -46,27 +44,39 @@ static int insert_snapshot_whiteouts(struct btree_trans *trans,
if (ret) if (ret)
break; break;
if (!k.k)
break;
if (!bkey_eq(old_pos, k.k->p)) if (!bkey_eq(old_pos, k.k->p))
break; break;
if (bch2_snapshot_is_ancestor(c, k.k->p.snapshot, old_pos.snapshot)) { if (bch2_snapshot_is_ancestor(c, k.k->p.snapshot, old_pos.snapshot) &&
struct bkey_i *update; !snapshot_list_has_ancestor(c, &s, k.k->p.snapshot)) {
struct bpos whiteout_pos = new_pos;
if (snapshot_list_has_ancestor(c, &s, k.k->p.snapshot)) whiteout_pos.snapshot = k.k->p.snapshot;
continue;
update = bch2_trans_kmalloc(trans, sizeof(struct bkey_i)); bch2_trans_iter_init(trans, &iter2, id, whiteout_pos,
BTREE_ITER_NOT_EXTENTS|
BTREE_ITER_INTENT);
k2 = bch2_btree_iter_peek_slot(&iter2);
ret = bkey_err(k2);
ret = PTR_ERR_OR_ZERO(update); if (!ret && k2.k->type == KEY_TYPE_deleted) {
if (ret) update = bch2_trans_kmalloc(trans, sizeof(struct bkey_i));
break; ret = PTR_ERR_OR_ZERO(update);
if (ret)
break;
bkey_init(&update->k); bkey_init(&update->k);
update->k.p = new_pos; update->k.p = whiteout_pos;
update->k.p.snapshot = k.k->p.snapshot; update->k.type = KEY_TYPE_whiteout;
ret = bch2_trans_update(trans, &iter2, update,
BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
}
bch2_trans_iter_exit(trans, &iter2);
ret = bch2_btree_insert_nonextent(trans, id, update,
BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
if (ret) if (ret)
break; break;
...@@ -222,9 +232,21 @@ static int __bch2_data_update_index_update(struct btree_trans *trans, ...@@ -222,9 +232,21 @@ static int __bch2_data_update_index_update(struct btree_trans *trans,
next_pos = insert->k.p; next_pos = insert->k.p;
ret = insert_snapshot_whiteouts(trans, m->btree_id, if (!bkey_eq(bkey_start_pos(&insert->k), bkey_start_pos(k.k))) {
k.k->p, insert->k.p) ?: ret = insert_snapshot_whiteouts(trans, m->btree_id, k.k->p,
bch2_trans_update(trans, &iter, insert, 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_trans_update(trans, &iter, insert,
BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?: BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
bch2_trans_commit(trans, &op->res, bch2_trans_commit(trans, &op->res,
NULL, NULL,
......
...@@ -68,6 +68,13 @@ static inline bool bch2_snapshot_is_ancestor(struct bch_fs *c, u32 id, u32 ances ...@@ -68,6 +68,13 @@ static inline bool bch2_snapshot_is_ancestor(struct bch_fs *c, u32 id, u32 ances
return id == ancestor; return id == ancestor;
} }
static inline bool bch2_snapshot_has_children(struct bch_fs *c, u32 id)
{
struct snapshot_t *t = snapshot_t(c, id);
return (t->children[0]|t->children[1]) != 0;
}
static inline bool snapshot_list_has_id(snapshot_id_list *s, u32 id) static inline bool snapshot_list_has_id(snapshot_id_list *s, u32 id)
{ {
u32 *i; u32 *i;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment