diff options
author | Kent Overstreet <kent.overstreet@gmail.com> | 2021-08-07 18:19:33 -0400 |
---|---|---|
committer | Kent Overstreet <kent.overstreet@linux.dev> | 2023-10-22 17:09:10 -0400 |
commit | c8476a4eb29972d756df33c3eb85c5792f798748 (patch) | |
tree | c4763f1ab72da7bd819418b492a2fbea0f9f9e13 /fs/bcachefs | |
parent | d2c50773946dfc21021c18c03aa35f1ccb47669a (diff) |
bcachefs: Minor btree iter refactoring
This makes the flow control in bch2_btree_iter_peek() and
bch2_btree_iter_peek_prev() a bit cleaner.
Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
Diffstat (limited to 'fs/bcachefs')
-rw-r--r-- | fs/bcachefs/btree_iter.c | 59 |
1 files changed, 20 insertions, 39 deletions
diff --git a/fs/bcachefs/btree_iter.c b/fs/bcachefs/btree_iter.c index fa19ef141252..f38f231fb296 100644 --- a/fs/bcachefs/btree_iter.c +++ b/fs/bcachefs/btree_iter.c @@ -1696,37 +1696,6 @@ inline bool bch2_btree_iter_rewind(struct btree_iter *iter) return ret; } -static inline bool btree_iter_set_pos_to_next_leaf(struct btree_iter *iter) -{ - struct bpos next_pos = iter->l[0].b->key.k.p; - bool ret = bpos_cmp(next_pos, SPOS_MAX) != 0; - - /* - * Typically, we don't want to modify iter->pos here, since that - * indicates where we searched from - unless we got to the end of the - * btree, in that case we want iter->pos to reflect that: - */ - if (ret) - btree_iter_set_search_pos(iter, bpos_successor(next_pos)); - else - bch2_btree_iter_set_pos(iter, SPOS_MAX); - - return ret; -} - -static inline bool btree_iter_set_pos_to_prev_leaf(struct btree_iter *iter) -{ - struct bpos next_pos = iter->l[0].b->data->min_key; - bool ret = bpos_cmp(next_pos, POS_MIN) != 0; - - if (ret) - btree_iter_set_search_pos(iter, bpos_predecessor(next_pos)); - else - bch2_btree_iter_set_pos(iter, POS_MIN); - - return ret; -} - static noinline struct bkey_i *__btree_trans_peek_updates(struct btree_iter *iter) { struct btree_insert_entry *i; @@ -1767,10 +1736,10 @@ struct bkey_s_c bch2_btree_iter_peek(struct btree_iter *iter) EBUG_ON(btree_iter_type(iter) != BTREE_ITER_KEYS); bch2_btree_iter_verify(iter); bch2_btree_iter_verify_entry_exit(iter); -start: - btree_iter_set_search_pos(iter, search_key); while (1) { + btree_iter_set_search_pos(iter, search_key); + ret = btree_iter_traverse(iter); if (unlikely(ret)) return bkey_s_c_err(ret); @@ -1791,14 +1760,20 @@ start: if (likely(k.k)) { if (bkey_deleted(k.k)) { search_key = bkey_successor(iter, k.k->p); - goto start; + continue; } break; } - if (!btree_iter_set_pos_to_next_leaf(iter)) - return bkey_s_c_null; + if (unlikely(!bpos_cmp(iter->l[0].b->key.k.p, SPOS_MAX))) { + bch2_btree_iter_set_pos(iter, SPOS_MAX); + k = bkey_s_c_null; + goto out; + } + + /* Advance to next leaf node: */ + search_key = bpos_successor(iter->l[0].b->key.k.p); } /* @@ -1810,6 +1785,7 @@ start: else if (bkey_cmp(bkey_start_pos(k.k), iter->pos) > 0) iter->pos = bkey_start_pos(k.k); +out: bch2_btree_iter_verify_entry_exit(iter); bch2_btree_iter_verify(iter); iter->should_be_locked = true; @@ -1834,6 +1810,7 @@ struct bkey_s_c bch2_btree_iter_next(struct btree_iter *iter) */ struct bkey_s_c bch2_btree_iter_peek_prev(struct btree_iter *iter) { + struct bpos search_key = iter->pos; struct btree_iter_level *l = &iter->l[0]; struct bkey_s_c k; int ret; @@ -1843,9 +1820,9 @@ struct bkey_s_c bch2_btree_iter_peek_prev(struct btree_iter *iter) bch2_btree_iter_verify(iter); bch2_btree_iter_verify_entry_exit(iter); - btree_iter_set_search_pos(iter, iter->pos); - while (1) { + btree_iter_set_search_pos(iter, search_key); + ret = btree_iter_traverse(iter); if (unlikely(ret)) { k = bkey_s_c_err(ret); @@ -1862,10 +1839,14 @@ struct bkey_s_c bch2_btree_iter_peek_prev(struct btree_iter *iter) if (likely(k.k)) break; - if (!btree_iter_set_pos_to_prev_leaf(iter)) { + if (unlikely(!bpos_cmp(iter->l[0].b->data->min_key, POS_MIN))) { + bch2_btree_iter_set_pos(iter, POS_MIN); k = bkey_s_c_null; goto no_key; } + + /* Advance to previous leaf node: */ + search_key = bpos_predecessor(iter->l[0].b->data->min_key); } EBUG_ON(bkey_cmp(bkey_start_pos(k.k), iter->pos) > 0); |