From c7bb769c81cca29462b39ea4689991b9b4d786fb Mon Sep 17 00:00:00 2001 From: Kent Overstreet Date: Fri, 19 Feb 2021 20:44:55 -0500 Subject: [PATCH] bcachefs: __bch2_trans_get_iter() refactoring, BTREE_ITER_NOT_EXTENTS Signed-off-by: Kent Overstreet --- fs/bcachefs/btree_iter.c | 70 ++++++++++++--------------------- fs/bcachefs/btree_iter.h | 1 - fs/bcachefs/btree_types.h | 1 + fs/bcachefs/btree_update_leaf.c | 16 ++++---- fs/bcachefs/fsck.c | 3 +- fs/bcachefs/recovery.c | 2 +- 6 files changed, 38 insertions(+), 55 deletions(-) diff --git a/fs/bcachefs/btree_iter.c b/fs/bcachefs/btree_iter.c index 15bda5c92ad1..35480ba43621 100644 --- a/fs/bcachefs/btree_iter.c +++ b/fs/bcachefs/btree_iter.c @@ -1494,24 +1494,14 @@ static void btree_iter_set_search_pos(struct btree_iter *iter, struct bpos new_p bch2_btree_iter_verify(iter); } -void __bch2_btree_iter_set_pos(struct btree_iter *iter, struct bpos new_pos, - bool strictly_greater) +void bch2_btree_iter_set_pos(struct btree_iter *iter, struct bpos new_pos) { bkey_init(&iter->k); 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)); } -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) { struct bpos pos = iter->k.p; @@ -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, - struct btree_iter *iter, enum btree_id btree_id, - struct bpos pos, unsigned flags) + struct btree_iter *iter, enum btree_id btree_id) { struct bch_fs *c = trans->c; unsigned i; - if (btree_node_type_is_extents(btree_id) && - !(flags & BTREE_ITER_NODES)) - flags |= BTREE_ITER_IS_EXTENTS; - 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->btree_id = btree_id; iter->level = 0; iter->min_depth = 0; - iter->locks_want = flags & BTREE_ITER_INTENT ? 1 : 0; + iter->locks_want = 0; iter->nodes_locked = 0; iter->nodes_intent_locked = 0; for (i = 0; i < ARRAY_SIZE(iter->l); i++) @@ -2064,12 +2044,16 @@ static inline void btree_iter_copy(struct btree_iter *dst, dst->flags &= ~BTREE_ITER_SET_POS_AFTER_COMMIT; } -static struct btree_iter *__btree_trans_get_iter(struct btree_trans *trans, - unsigned btree_id, struct bpos pos, - unsigned flags) +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, *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) { if (btree_iter_type(iter) != (flags & BTREE_ITER_TYPE)) continue; @@ -2084,10 +2068,10 @@ static struct btree_iter *__btree_trans_get_iter(struct btree_trans *trans, best = iter; } - +alloc_iter: if (!best) { 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)) { iter = btree_trans_iter_alloc(trans); btree_iter_copy(iter, best); @@ -2095,7 +2079,14 @@ static struct btree_iter *__btree_trans_get_iter(struct btree_trans *trans, 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; if (!(iter->flags & BTREE_ITER_INTENT)) @@ -2103,24 +2094,11 @@ static struct btree_iter *__btree_trans_get_iter(struct btree_trans *trans, else if (!iter->locks_want) __bch2_btree_iter_upgrade_nounlock(iter, 1); - trans->iters_live |= 1ULL << iter->idx; - trans->iters_touched |= 1ULL << iter->idx; + bch2_btree_iter_set_pos(iter, pos); 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; -} - struct btree_iter *bch2_trans_get_node_iter(struct btree_trans *trans, enum btree_id btree_id, struct bpos pos, @@ -2129,8 +2107,10 @@ struct btree_iter *bch2_trans_get_node_iter(struct btree_trans *trans, unsigned flags) { struct btree_iter *iter = - __btree_trans_get_iter(trans, btree_id, pos, - flags|BTREE_ITER_NODES); + __bch2_trans_get_iter(trans, btree_id, pos, + BTREE_ITER_NODES| + BTREE_ITER_NOT_EXTENTS| + flags); unsigned i; BUG_ON(bkey_cmp(iter->pos, pos)); diff --git a/fs/bcachefs/btree_iter.h b/fs/bcachefs/btree_iter.h index 0ac8337eba98..bd0c429bd91a 100644 --- a/fs/bcachefs/btree_iter.h +++ b/fs/bcachefs/btree_iter.h @@ -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 *); -void __bch2_btree_iter_set_pos(struct btree_iter *, struct bpos, bool); void bch2_btree_iter_set_pos(struct btree_iter *, struct bpos); /* Sort order for locking btree iterators: */ diff --git a/fs/bcachefs/btree_types.h b/fs/bcachefs/btree_types.h index b12a4f9dd1d0..41fa5ff77e91 100644 --- a/fs/bcachefs/btree_types.h +++ b/fs/bcachefs/btree_types.h @@ -215,6 +215,7 @@ enum btree_iter_type { #define BTREE_ITER_SET_POS_AFTER_COMMIT (1 << 8) #define BTREE_ITER_CACHED_NOFILL (1 << 9) #define BTREE_ITER_CACHED_NOCREATE (1 << 10) +#define BTREE_ITER_NOT_EXTENTS (1 << 11) enum btree_iter_uptodate { BTREE_ITER_UPTODATE = 0, diff --git a/fs/bcachefs/btree_update_leaf.c b/fs/bcachefs/btree_update_leaf.c index 315e2e1e229d..bf2a2b0695b6 100644 --- a/fs/bcachefs/btree_update_leaf.c +++ b/fs/bcachefs/btree_update_leaf.c @@ -740,10 +740,9 @@ static int extent_update_to_keys(struct btree_trans *trans, if (ret) return ret; - n.iter = bch2_trans_copy_iter(trans, n.iter); - - n.iter->flags |= BTREE_ITER_INTENT; - __bch2_btree_iter_set_pos(n.iter, n.k->k.p, false); + n.iter = bch2_trans_get_iter(trans, n.iter->btree_id, n.k->k.p, + BTREE_ITER_INTENT| + BTREE_ITER_NOT_EXTENTS); n.is_extent = false; ret = __bch2_trans_update2(trans, n); @@ -777,7 +776,8 @@ static int extent_handle_overwrites(struct btree_trans *trans, bkey_reassemble(update, k); 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); bch2_trans_iter_put(trans, update_iter); if (ret) @@ -794,7 +794,8 @@ static int extent_handle_overwrites(struct btree_trans *trans, bkey_reassemble(update, k); 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); bch2_trans_iter_put(trans, update_iter); if (ret) @@ -811,7 +812,8 @@ static int extent_handle_overwrites(struct btree_trans *trans, update->k.type = KEY_TYPE_deleted; 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); bch2_trans_iter_put(trans, update_iter); if (ret) diff --git a/fs/bcachefs/fsck.c b/fs/bcachefs/fsck.c index ebc234b0b6fe..7f6b4ac48f3d 100644 --- a/fs/bcachefs/fsck.c +++ b/fs/bcachefs/fsck.c @@ -442,7 +442,8 @@ static int bch2_fix_overlapping_extent(struct btree_trans *trans, * We don't want to go through the * 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 diff --git a/fs/bcachefs/recovery.c b/fs/bcachefs/recovery.c index 11d4894b3d63..03a25dd5acc6 100644 --- a/fs/bcachefs/recovery.c +++ b/fs/bcachefs/recovery.c @@ -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 * 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) ?: bch2_trans_update(trans, iter, k, BTREE_TRIGGER_NORUN);