Commit 1f7fdc0a authored by Kent Overstreet's avatar Kent Overstreet Committed by Kent Overstreet

bcachefs: btree_iter_live()

New helper to clean things up a bit - also, improve iter->flags
handling.
Signed-off-by: default avatarKent Overstreet <kent.overstreet@gmail.com>
Signed-off-by: default avatarKent Overstreet <kent.overstreet@linux.dev>
parent 6333bd2f
...@@ -1701,7 +1701,8 @@ struct bkey_s_c bch2_btree_iter_peek_with_updates(struct btree_iter *iter) ...@@ -1701,7 +1701,8 @@ struct bkey_s_c bch2_btree_iter_peek_with_updates(struct btree_iter *iter)
k = __bch2_btree_iter_peek_with_updates(iter); k = __bch2_btree_iter_peek_with_updates(iter);
if (k.k && bkey_deleted(k.k)) { if (k.k && bkey_deleted(k.k)) {
bch2_btree_iter_advance_pos(iter); if (!bch2_btree_iter_advance_pos(iter))
return bkey_s_c_null;
continue; continue;
} }
...@@ -2008,7 +2009,7 @@ static void btree_trans_iter_alloc_fail(struct btree_trans *trans) ...@@ -2008,7 +2009,7 @@ static void btree_trans_iter_alloc_fail(struct btree_trans *trans)
bch2_btree_ids[iter->btree_id], bch2_btree_ids[iter->btree_id],
iter->pos.inode, iter->pos.inode,
iter->pos.offset, iter->pos.offset,
(trans->iters_live & (1ULL << iter->idx)) ? " live" : "", btree_iter_live(trans, iter) ? " live" : "",
(trans->iters_touched & (1ULL << iter->idx)) ? " touched" : "", (trans->iters_touched & (1ULL << iter->idx)) ? " touched" : "",
iter->flags & BTREE_ITER_KEEP_UNTIL_COMMIT ? " keep" : "", iter->flags & BTREE_ITER_KEEP_UNTIL_COMMIT ? " keep" : "",
(void *) iter->ip_allocated); (void *) iter->ip_allocated);
...@@ -2089,31 +2090,20 @@ static struct btree_iter *__btree_trans_get_iter(struct btree_trans *trans, ...@@ -2089,31 +2090,20 @@ static struct btree_iter *__btree_trans_get_iter(struct btree_trans *trans,
if (!best) { if (!best) {
iter = btree_trans_iter_alloc(trans); iter = btree_trans_iter_alloc(trans);
bch2_btree_iter_init(trans, iter, btree_id, pos, flags); bch2_btree_iter_init(trans, iter, btree_id, pos, flags);
} else if ((trans->iters_live & (1ULL << best->idx)) || } else if (btree_iter_keep(trans, best)) {
(best->flags & BTREE_ITER_KEEP_UNTIL_COMMIT)) {
iter = btree_trans_iter_alloc(trans); iter = btree_trans_iter_alloc(trans);
btree_iter_copy(iter, best); btree_iter_copy(iter, best);
} else { } else {
iter = best; iter = best;
} }
iter->flags &= ~BTREE_ITER_KEEP_UNTIL_COMMIT; flags |= iter->flags & BTREE_ITER_ERROR;
iter->flags &= ~BTREE_ITER_USER_FLAGS; iter->flags = flags;
iter->flags |= flags & BTREE_ITER_USER_FLAGS;
if (iter->flags & BTREE_ITER_INTENT) { if (!(iter->flags & BTREE_ITER_INTENT))
if (!iter->locks_want) {
__bch2_btree_iter_unlock(iter);
iter->locks_want = 1;
}
} else
bch2_btree_iter_downgrade(iter); bch2_btree_iter_downgrade(iter);
else if (!iter->locks_want)
BUG_ON(iter->btree_id != btree_id); __bch2_btree_iter_upgrade_nounlock(iter, 1);
BUG_ON((iter->flags ^ flags) & BTREE_ITER_TYPE);
BUG_ON(iter->flags & BTREE_ITER_KEEP_UNTIL_COMMIT);
BUG_ON(iter->flags & BTREE_ITER_SET_POS_AFTER_COMMIT);
BUG_ON(trans->iters_live & (1ULL << iter->idx));
trans->iters_live |= 1ULL << iter->idx; trans->iters_live |= 1ULL << iter->idx;
trans->iters_touched |= 1ULL << iter->idx; trans->iters_touched |= 1ULL << iter->idx;
......
...@@ -289,6 +289,17 @@ struct btree_iter *bch2_trans_get_node_iter(struct btree_trans *, ...@@ -289,6 +289,17 @@ struct btree_iter *bch2_trans_get_node_iter(struct btree_trans *,
enum btree_id, struct bpos, enum btree_id, struct bpos,
unsigned, unsigned, unsigned); unsigned, unsigned, unsigned);
static inline bool btree_iter_live(struct btree_trans *trans, struct btree_iter *iter)
{
return (trans->iters_live & (1ULL << iter->idx)) != 0;
}
static inline bool btree_iter_keep(struct btree_trans *trans, struct btree_iter *iter)
{
return btree_iter_live(trans, iter) ||
(iter->flags & BTREE_ITER_KEEP_UNTIL_COMMIT);
}
#define TRANS_RESET_NOTRAVERSE (1 << 0) #define TRANS_RESET_NOTRAVERSE (1 << 0)
void bch2_trans_reset(struct btree_trans *, unsigned); void bch2_trans_reset(struct btree_trans *, unsigned);
......
...@@ -216,13 +216,6 @@ enum btree_iter_type { ...@@ -216,13 +216,6 @@ enum btree_iter_type {
#define BTREE_ITER_CACHED_NOFILL (1 << 9) #define BTREE_ITER_CACHED_NOFILL (1 << 9)
#define BTREE_ITER_CACHED_NOCREATE (1 << 10) #define BTREE_ITER_CACHED_NOCREATE (1 << 10)
#define BTREE_ITER_USER_FLAGS \
(BTREE_ITER_SLOTS \
|BTREE_ITER_INTENT \
|BTREE_ITER_PREFETCH \
|BTREE_ITER_CACHED_NOFILL \
|BTREE_ITER_CACHED_NOCREATE)
enum btree_iter_uptodate { enum btree_iter_uptodate {
BTREE_ITER_UPTODATE = 0, BTREE_ITER_UPTODATE = 0,
BTREE_ITER_NEED_PEEK = 1, BTREE_ITER_NEED_PEEK = 1,
......
...@@ -512,8 +512,7 @@ static inline int do_bch2_trans_commit(struct btree_trans *trans, ...@@ -512,8 +512,7 @@ static inline int do_bch2_trans_commit(struct btree_trans *trans,
*/ */
trans_for_each_iter(trans, iter) { trans_for_each_iter(trans, iter) {
if (iter->nodes_locked != iter->nodes_intent_locked) { if (iter->nodes_locked != iter->nodes_intent_locked) {
if ((iter->flags & BTREE_ITER_KEEP_UNTIL_COMMIT) || if (btree_iter_keep(trans, iter)) {
(trans->iters_live & (1ULL << iter->idx))) {
if (!bch2_btree_iter_upgrade(iter, 1)) { if (!bch2_btree_iter_upgrade(iter, 1)) {
trace_trans_restart_upgrade(trans->ip); trace_trans_restart_upgrade(trans->ip);
return -EINTR; return -EINTR;
...@@ -945,7 +944,7 @@ int __bch2_trans_commit(struct btree_trans *trans) ...@@ -945,7 +944,7 @@ int __bch2_trans_commit(struct btree_trans *trans)
goto err; goto err;
trans_for_each_iter(trans, iter) trans_for_each_iter(trans, iter)
if ((trans->iters_live & (1ULL << iter->idx)) && if (btree_iter_live(trans, iter) &&
(iter->flags & BTREE_ITER_SET_POS_AFTER_COMMIT)) (iter->flags & BTREE_ITER_SET_POS_AFTER_COMMIT))
bch2_btree_iter_set_pos(iter, iter->pos_after_commit); bch2_btree_iter_set_pos(iter, iter->pos_after_commit);
out: out:
...@@ -1049,7 +1048,7 @@ int bch2_trans_update(struct btree_trans *trans, struct btree_iter *iter, ...@@ -1049,7 +1048,7 @@ int bch2_trans_update(struct btree_trans *trans, struct btree_iter *iter,
* the iterator pos if some other code is using it, so we may * the iterator pos if some other code is using it, so we may
* need to clone it: * need to clone it:
*/ */
if (trans->iters_live & (1ULL << i->iter->idx)) { if (btree_iter_live(trans, i->iter)) {
i->iter = bch2_trans_copy_iter(trans, i->iter); i->iter = bch2_trans_copy_iter(trans, i->iter);
i->iter->flags |= BTREE_ITER_KEEP_UNTIL_COMMIT; i->iter->flags |= BTREE_ITER_KEEP_UNTIL_COMMIT;
......
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