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

bcachefs: __bch2_trans_get_iter() refactoring, BTREE_ITER_NOT_EXTENTS

Signed-off-by: default avatarKent Overstreet <kent.overstreet@linux.dev>
parent a045be5a
...@@ -1494,24 +1494,14 @@ static void btree_iter_set_search_pos(struct btree_iter *iter, struct bpos new_p ...@@ -1494,24 +1494,14 @@ static void btree_iter_set_search_pos(struct btree_iter *iter, struct bpos new_p
bch2_btree_iter_verify(iter); bch2_btree_iter_verify(iter);
} }
void __bch2_btree_iter_set_pos(struct btree_iter *iter, struct bpos new_pos, void bch2_btree_iter_set_pos(struct btree_iter *iter, struct bpos new_pos)
bool strictly_greater)
{ {
bkey_init(&iter->k); bkey_init(&iter->k);
iter->k.p = iter->pos = new_pos; iter->k.p = iter->pos = new_pos;
iter->flags &= ~BTREE_ITER_IS_EXTENTS;
iter->flags |= strictly_greater ? BTREE_ITER_IS_EXTENTS : 0;
btree_iter_set_search_pos(iter, btree_iter_search_key(iter)); btree_iter_set_search_pos(iter, btree_iter_search_key(iter));
} }
void bch2_btree_iter_set_pos(struct btree_iter *iter, struct bpos new_pos)
{
__bch2_btree_iter_set_pos(iter, new_pos,
(iter->flags & BTREE_ITER_IS_EXTENTS) != 0);
}
static inline bool bch2_btree_iter_advance_pos(struct btree_iter *iter) static inline bool bch2_btree_iter_advance_pos(struct btree_iter *iter)
{ {
struct bpos pos = iter->k.p; struct bpos pos = iter->k.p;
...@@ -1932,27 +1922,17 @@ struct bkey_s_c bch2_btree_iter_peek_cached(struct btree_iter *iter) ...@@ -1932,27 +1922,17 @@ struct bkey_s_c bch2_btree_iter_peek_cached(struct btree_iter *iter)
} }
static inline void bch2_btree_iter_init(struct btree_trans *trans, static inline void bch2_btree_iter_init(struct btree_trans *trans,
struct btree_iter *iter, enum btree_id btree_id, struct btree_iter *iter, enum btree_id btree_id)
struct bpos pos, unsigned flags)
{ {
struct bch_fs *c = trans->c; struct bch_fs *c = trans->c;
unsigned i; unsigned i;
if (btree_node_type_is_extents(btree_id) &&
!(flags & BTREE_ITER_NODES))
flags |= BTREE_ITER_IS_EXTENTS;
iter->trans = trans; iter->trans = trans;
iter->pos = pos;
bkey_init(&iter->k);
iter->k.p = pos;
iter->flags = flags;
iter->real_pos = btree_iter_search_key(iter);
iter->uptodate = BTREE_ITER_NEED_TRAVERSE; iter->uptodate = BTREE_ITER_NEED_TRAVERSE;
iter->btree_id = btree_id; iter->btree_id = btree_id;
iter->level = 0; iter->level = 0;
iter->min_depth = 0; iter->min_depth = 0;
iter->locks_want = flags & BTREE_ITER_INTENT ? 1 : 0; iter->locks_want = 0;
iter->nodes_locked = 0; iter->nodes_locked = 0;
iter->nodes_intent_locked = 0; iter->nodes_intent_locked = 0;
for (i = 0; i < ARRAY_SIZE(iter->l); i++) for (i = 0; i < ARRAY_SIZE(iter->l); i++)
...@@ -2064,12 +2044,16 @@ static inline void btree_iter_copy(struct btree_iter *dst, ...@@ -2064,12 +2044,16 @@ static inline void btree_iter_copy(struct btree_iter *dst,
dst->flags &= ~BTREE_ITER_SET_POS_AFTER_COMMIT; dst->flags &= ~BTREE_ITER_SET_POS_AFTER_COMMIT;
} }
static struct btree_iter *__btree_trans_get_iter(struct btree_trans *trans, struct btree_iter *__bch2_trans_get_iter(struct btree_trans *trans,
unsigned btree_id, struct bpos pos, enum btree_id btree_id, struct bpos pos,
unsigned flags) unsigned flags)
{ {
struct btree_iter *iter, *best = NULL; struct btree_iter *iter, *best = NULL;
/* We always want a fresh iterator for node iterators: */
if ((flags & BTREE_ITER_TYPE) == BTREE_ITER_NODES)
goto alloc_iter;
trans_for_each_iter(trans, iter) { trans_for_each_iter(trans, iter) {
if (btree_iter_type(iter) != (flags & BTREE_ITER_TYPE)) if (btree_iter_type(iter) != (flags & BTREE_ITER_TYPE))
continue; continue;
...@@ -2084,10 +2068,10 @@ static struct btree_iter *__btree_trans_get_iter(struct btree_trans *trans, ...@@ -2084,10 +2068,10 @@ static struct btree_iter *__btree_trans_get_iter(struct btree_trans *trans,
best = iter; best = iter;
} }
alloc_iter:
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);
} else if (btree_iter_keep(trans, best)) { } else if (btree_iter_keep(trans, best)) {
iter = btree_trans_iter_alloc(trans); iter = btree_trans_iter_alloc(trans);
btree_iter_copy(iter, best); btree_iter_copy(iter, best);
...@@ -2095,7 +2079,14 @@ static struct btree_iter *__btree_trans_get_iter(struct btree_trans *trans, ...@@ -2095,7 +2079,14 @@ static struct btree_iter *__btree_trans_get_iter(struct btree_trans *trans,
iter = best; iter = best;
} }
flags |= iter->flags & BTREE_ITER_ERROR; trans->iters_live |= 1ULL << iter->idx;
trans->iters_touched |= 1ULL << iter->idx;
if ((flags & BTREE_ITER_TYPE) != BTREE_ITER_NODES &&
btree_node_type_is_extents(btree_id) &&
!(flags & BTREE_ITER_NOT_EXTENTS))
flags |= BTREE_ITER_IS_EXTENTS;
iter->flags = flags; iter->flags = flags;
if (!(iter->flags & BTREE_ITER_INTENT)) if (!(iter->flags & BTREE_ITER_INTENT))
...@@ -2103,21 +2094,8 @@ static struct btree_iter *__btree_trans_get_iter(struct btree_trans *trans, ...@@ -2103,21 +2094,8 @@ static struct btree_iter *__btree_trans_get_iter(struct btree_trans *trans,
else if (!iter->locks_want) else if (!iter->locks_want)
__bch2_btree_iter_upgrade_nounlock(iter, 1); __bch2_btree_iter_upgrade_nounlock(iter, 1);
trans->iters_live |= 1ULL << iter->idx; bch2_btree_iter_set_pos(iter, pos);
trans->iters_touched |= 1ULL << iter->idx;
return iter;
}
struct btree_iter *__bch2_trans_get_iter(struct btree_trans *trans,
enum btree_id btree_id,
struct bpos pos, unsigned flags)
{
struct btree_iter *iter =
__btree_trans_get_iter(trans, btree_id, pos, flags);
__bch2_btree_iter_set_pos(iter, pos,
btree_node_type_is_extents(btree_id));
return iter; return iter;
} }
...@@ -2129,8 +2107,10 @@ struct btree_iter *bch2_trans_get_node_iter(struct btree_trans *trans, ...@@ -2129,8 +2107,10 @@ struct btree_iter *bch2_trans_get_node_iter(struct btree_trans *trans,
unsigned flags) unsigned flags)
{ {
struct btree_iter *iter = struct btree_iter *iter =
__btree_trans_get_iter(trans, btree_id, pos, __bch2_trans_get_iter(trans, btree_id, pos,
flags|BTREE_ITER_NODES); BTREE_ITER_NODES|
BTREE_ITER_NOT_EXTENTS|
flags);
unsigned i; unsigned i;
BUG_ON(bkey_cmp(iter->pos, pos)); BUG_ON(bkey_cmp(iter->pos, pos));
......
...@@ -175,7 +175,6 @@ struct bkey_s_c bch2_btree_iter_prev_slot(struct btree_iter *); ...@@ -175,7 +175,6 @@ struct bkey_s_c bch2_btree_iter_prev_slot(struct btree_iter *);
struct bkey_s_c bch2_btree_iter_peek_cached(struct btree_iter *); struct bkey_s_c bch2_btree_iter_peek_cached(struct btree_iter *);
void __bch2_btree_iter_set_pos(struct btree_iter *, struct bpos, bool);
void bch2_btree_iter_set_pos(struct btree_iter *, struct bpos); void bch2_btree_iter_set_pos(struct btree_iter *, struct bpos);
/* Sort order for locking btree iterators: */ /* Sort order for locking btree iterators: */
......
...@@ -215,6 +215,7 @@ enum btree_iter_type { ...@@ -215,6 +215,7 @@ enum btree_iter_type {
#define BTREE_ITER_SET_POS_AFTER_COMMIT (1 << 8) #define BTREE_ITER_SET_POS_AFTER_COMMIT (1 << 8)
#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_NOT_EXTENTS (1 << 11)
enum btree_iter_uptodate { enum btree_iter_uptodate {
BTREE_ITER_UPTODATE = 0, BTREE_ITER_UPTODATE = 0,
......
...@@ -740,10 +740,9 @@ static int extent_update_to_keys(struct btree_trans *trans, ...@@ -740,10 +740,9 @@ static int extent_update_to_keys(struct btree_trans *trans,
if (ret) if (ret)
return ret; return ret;
n.iter = bch2_trans_copy_iter(trans, n.iter); n.iter = bch2_trans_get_iter(trans, n.iter->btree_id, n.k->k.p,
BTREE_ITER_INTENT|
n.iter->flags |= BTREE_ITER_INTENT; BTREE_ITER_NOT_EXTENTS);
__bch2_btree_iter_set_pos(n.iter, n.k->k.p, false);
n.is_extent = false; n.is_extent = false;
ret = __bch2_trans_update2(trans, n); ret = __bch2_trans_update2(trans, n);
...@@ -777,7 +776,8 @@ static int extent_handle_overwrites(struct btree_trans *trans, ...@@ -777,7 +776,8 @@ static int extent_handle_overwrites(struct btree_trans *trans,
bkey_reassemble(update, k); bkey_reassemble(update, k);
bch2_cut_back(start, update); bch2_cut_back(start, update);
__bch2_btree_iter_set_pos(update_iter, update->k.p, false); update_iter->flags &= ~BTREE_ITER_IS_EXTENTS;
bch2_btree_iter_set_pos(update_iter, update->k.p);
ret = bch2_trans_update2(trans, update_iter, update); ret = bch2_trans_update2(trans, update_iter, update);
bch2_trans_iter_put(trans, update_iter); bch2_trans_iter_put(trans, update_iter);
if (ret) if (ret)
...@@ -794,7 +794,8 @@ static int extent_handle_overwrites(struct btree_trans *trans, ...@@ -794,7 +794,8 @@ static int extent_handle_overwrites(struct btree_trans *trans,
bkey_reassemble(update, k); bkey_reassemble(update, k);
bch2_cut_front(end, update); bch2_cut_front(end, update);
__bch2_btree_iter_set_pos(update_iter, update->k.p, false); update_iter->flags &= ~BTREE_ITER_IS_EXTENTS;
bch2_btree_iter_set_pos(update_iter, update->k.p);
ret = bch2_trans_update2(trans, update_iter, update); ret = bch2_trans_update2(trans, update_iter, update);
bch2_trans_iter_put(trans, update_iter); bch2_trans_iter_put(trans, update_iter);
if (ret) if (ret)
...@@ -811,7 +812,8 @@ static int extent_handle_overwrites(struct btree_trans *trans, ...@@ -811,7 +812,8 @@ static int extent_handle_overwrites(struct btree_trans *trans,
update->k.type = KEY_TYPE_deleted; update->k.type = KEY_TYPE_deleted;
update->k.size = 0; update->k.size = 0;
__bch2_btree_iter_set_pos(update_iter, update->k.p, false); update_iter->flags &= ~BTREE_ITER_IS_EXTENTS;
bch2_btree_iter_set_pos(update_iter, update->k.p);
ret = bch2_trans_update2(trans, update_iter, update); ret = bch2_trans_update2(trans, update_iter, update);
bch2_trans_iter_put(trans, update_iter); bch2_trans_iter_put(trans, update_iter);
if (ret) if (ret)
......
...@@ -442,7 +442,8 @@ static int bch2_fix_overlapping_extent(struct btree_trans *trans, ...@@ -442,7 +442,8 @@ static int bch2_fix_overlapping_extent(struct btree_trans *trans,
* We don't want to go through the * We don't want to go through the
* extent_handle_overwrites path: * extent_handle_overwrites path:
*/ */
__bch2_btree_iter_set_pos(u_iter, u->k.p, false); u_iter->flags &= ~BTREE_ITER_IS_EXTENTS;
bch2_btree_iter_set_pos(u_iter, u->k.p);
/* /*
* XXX: this is going to leave disk space * XXX: this is going to leave disk space
......
...@@ -529,7 +529,7 @@ static int __bch2_journal_replay_key(struct btree_trans *trans, ...@@ -529,7 +529,7 @@ static int __bch2_journal_replay_key(struct btree_trans *trans,
* want that here, journal replay is supposed to treat extents like * want that here, journal replay is supposed to treat extents like
* regular keys: * regular keys:
*/ */
__bch2_btree_iter_set_pos(iter, k->k.p, false); BUG_ON(iter->flags & BTREE_ITER_IS_EXTENTS);
ret = bch2_btree_iter_traverse(iter) ?: ret = bch2_btree_iter_traverse(iter) ?:
bch2_trans_update(trans, iter, k, BTREE_TRIGGER_NORUN); bch2_trans_update(trans, iter, k, BTREE_TRIGGER_NORUN);
......
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