Commit 89333156 authored by Kent Overstreet's avatar Kent Overstreet Committed by Kent Overstreet

bcachefs: Convert bch2_dev_usrdata_drop() to for_each_btree_key2()

The new for_each_btree_key2() macro handles transaction retries,
allowing us to avoid nested transactions - which we want to avoid since
they're tricky to do completely correctly and upcoming assertions are
going to be checking for that.
Signed-off-by: default avatarKent Overstreet <kent.overstreet@linux.dev>
parent d04801a0
...@@ -650,6 +650,11 @@ static inline bool btree_type_has_snapshots(enum btree_id id) ...@@ -650,6 +650,11 @@ static inline bool btree_type_has_snapshots(enum btree_id id)
return (1 << id) & BTREE_ID_HAS_SNAPSHOTS; return (1 << id) & BTREE_ID_HAS_SNAPSHOTS;
} }
static inline bool btree_type_has_ptrs(enum btree_id id)
{
return (1 << id) & BTREE_ID_HAS_PTRS;
}
static inline bool btree_node_type_needs_gc(enum btree_node_type type) static inline bool btree_node_type_needs_gc(enum btree_node_type type)
{ {
return BTREE_NODE_TYPE_HAS_TRIGGERS & (1U << type); return BTREE_NODE_TYPE_HAS_TRIGGERS & (1U << type);
......
...@@ -35,43 +35,36 @@ static int drop_dev_ptrs(struct bch_fs *c, struct bkey_s k, ...@@ -35,43 +35,36 @@ static int drop_dev_ptrs(struct bch_fs *c, struct bkey_s k,
return 0; return 0;
} }
static int __bch2_dev_usrdata_drop(struct bch_fs *c, unsigned dev_idx, int flags, static int bch2_dev_usrdata_drop_key(struct btree_trans *trans,
enum btree_id btree_id) struct btree_iter *iter,
struct bkey_s_c k,
unsigned dev_idx,
int flags)
{ {
struct btree_trans trans; struct bch_fs *c = trans->c;
struct btree_iter iter; struct bkey_i *n;
struct bkey_s_c k; int ret;
struct bkey_buf sk;
int ret = 0;
bch2_bkey_buf_init(&sk);
bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
bch2_trans_iter_init(&trans, &iter, btree_id, POS_MIN, if (!bch2_bkey_has_device(k, dev_idx))
BTREE_ITER_PREFETCH| return 0;
BTREE_ITER_ALL_SNAPSHOTS);
while ((bch2_trans_begin(&trans), n = bch2_trans_kmalloc(trans, bkey_bytes(k.k));
(k = bch2_btree_iter_peek(&iter)).k) && ret = PTR_ERR_OR_ZERO(n);
!(ret = bkey_err(k))) { if (ret)
if (!bch2_bkey_has_device(k, dev_idx)) { return ret;
bch2_btree_iter_advance(&iter);
continue;
}
bch2_bkey_buf_reassemble(&sk, c, k); bkey_reassemble(n, k);
ret = drop_dev_ptrs(c, bkey_i_to_s(sk.k), ret = drop_dev_ptrs(c, bkey_i_to_s(n), dev_idx, flags, false);
dev_idx, flags, false);
if (ret) if (ret)
break; return ret;
/* /*
* If the new extent no longer has any pointers, bch2_extent_normalize() * If the new extent no longer has any pointers, bch2_extent_normalize()
* will do the appropriate thing with it (turning it into a * will do the appropriate thing with it (turning it into a
* KEY_TYPE_error key, or just a discard if it was a cached extent) * KEY_TYPE_error key, or just a discard if it was a cached extent)
*/ */
bch2_extent_normalize(c, bkey_i_to_s(sk.k)); bch2_extent_normalize(c, bkey_i_to_s(n));
/* /*
* Since we're not inserting through an extent iterator * Since we're not inserting through an extent iterator
...@@ -79,41 +72,39 @@ static int __bch2_dev_usrdata_drop(struct bch_fs *c, unsigned dev_idx, int flags ...@@ -79,41 +72,39 @@ static int __bch2_dev_usrdata_drop(struct bch_fs *c, unsigned dev_idx, int flags
* we aren't using the extent overwrite path to delete, we're * we aren't using the extent overwrite path to delete, we're
* just using the normal key deletion path: * just using the normal key deletion path:
*/ */
if (bkey_deleted(&sk.k->k)) if (bkey_deleted(&n->k))
sk.k->k.size = 0; n->k.size = 0;
ret = bch2_btree_iter_traverse(&iter) ?: return bch2_trans_update(trans, iter, n, BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
bch2_trans_update(&trans, &iter, sk.k, }
BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE) ?:
bch2_trans_commit(&trans, NULL, NULL,
BTREE_INSERT_NOFAIL);
/* static int bch2_dev_usrdata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
* don't want to leave ret == -EINTR, since if we raced and {
* something else overwrote the key we could spuriously return struct btree_trans trans;
* -EINTR below: struct btree_iter iter;
*/ struct bkey_s_c k;
if (ret == -EINTR) enum btree_id id;
ret = 0; int ret = 0;
bch2_trans_init(&trans, c, BTREE_ITER_MAX, 0);
for (id = 0; id < BTREE_ID_NR; id++) {
if (!btree_type_has_ptrs(id))
continue;
ret = for_each_btree_key_commit(&trans, iter, id, POS_MIN,
BTREE_ITER_PREFETCH|BTREE_ITER_ALL_SNAPSHOTS, k,
NULL, NULL, BTREE_INSERT_NOFAIL,
bch2_dev_usrdata_drop_key(&trans, &iter, k, dev_idx, flags));
if (ret) if (ret)
break; break;
} }
bch2_trans_iter_exit(&trans, &iter);
bch2_trans_exit(&trans); bch2_trans_exit(&trans);
bch2_bkey_buf_exit(&sk, c);
BUG_ON(ret == -EINTR);
return ret; return ret;
} }
static int bch2_dev_usrdata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
{
return __bch2_dev_usrdata_drop(c, dev_idx, flags, BTREE_ID_extents) ?:
__bch2_dev_usrdata_drop(c, dev_idx, flags, BTREE_ID_reflink);
}
static int bch2_dev_metadata_drop(struct bch_fs *c, unsigned dev_idx, int flags) static int bch2_dev_metadata_drop(struct bch_fs *c, unsigned dev_idx, int flags)
{ {
struct btree_trans trans; struct btree_trans trans;
......
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