1 // SPDX-License-Identifier: GPL-2.0
4 #include "btree_cache.h"
6 #include "btree_iter.h"
7 #include "btree_locking.h"
10 #include <linux/prefetch.h>
11 #include <linux/sched/mm.h>
12 #include <trace/events/bcachefs.h>
14 const char * const bch2_btree_ids[] = {
15 #define x(kwd, val, name) name,
21 void bch2_recalc_btree_reserve(struct bch_fs *c)
23 unsigned i, reserve = 16;
25 if (!c->btree_roots[0].b)
28 for (i = 0; i < BTREE_ID_NR; i++)
29 if (c->btree_roots[i].b)
30 reserve += min_t(unsigned, 1,
31 c->btree_roots[i].b->c.level) * 8;
33 c->btree_cache.reserve = reserve;
36 static inline unsigned btree_cache_can_free(struct btree_cache *bc)
38 return max_t(int, 0, bc->used - bc->reserve);
41 static void __btree_node_data_free(struct bch_fs *c, struct btree *b)
43 EBUG_ON(btree_node_write_in_flight(b));
45 kvpfree(b->data, btree_bytes(c));
47 bch2_btree_keys_free(b);
50 static void btree_node_data_free(struct bch_fs *c, struct btree *b)
52 struct btree_cache *bc = &c->btree_cache;
54 __btree_node_data_free(c, b);
56 list_move(&b->list, &bc->freed);
59 static int bch2_btree_cache_cmp_fn(struct rhashtable_compare_arg *arg,
62 const struct btree *b = obj;
63 const u64 *v = arg->key;
65 return b->hash_val == *v ? 0 : 1;
68 static const struct rhashtable_params bch_btree_cache_params = {
69 .head_offset = offsetof(struct btree, hash),
70 .key_offset = offsetof(struct btree, hash_val),
71 .key_len = sizeof(u64),
72 .obj_cmpfn = bch2_btree_cache_cmp_fn,
75 static int __btree_node_data_alloc(struct bch_fs *c, struct btree *b, gfp_t gfp)
77 BUG_ON(b->data || b->aux_data);
79 b->data = kvpmalloc(btree_bytes(c), gfp);
83 if (bch2_btree_keys_alloc(b, btree_page_order(c), gfp)) {
84 kvpfree(b->data, btree_bytes(c));
92 static void btree_node_data_alloc(struct bch_fs *c, struct btree *b, gfp_t gfp)
94 struct btree_cache *bc = &c->btree_cache;
96 if (!__btree_node_data_alloc(c, b, gfp)) {
98 list_move(&b->list, &bc->freeable);
100 list_move(&b->list, &bc->freed);
104 static struct btree *btree_node_mem_alloc(struct bch_fs *c, gfp_t gfp)
106 struct btree *b = kzalloc(sizeof(struct btree), gfp);
110 bkey_btree_ptr_init(&b->key);
111 six_lock_init(&b->c.lock);
112 INIT_LIST_HEAD(&b->list);
113 INIT_LIST_HEAD(&b->write_blocked);
115 btree_node_data_alloc(c, b, gfp);
116 return b->data ? b : NULL;
119 /* Btree in memory cache - hash table */
121 void bch2_btree_node_hash_remove(struct btree_cache *bc, struct btree *b)
123 rhashtable_remove_fast(&bc->table, &b->hash, bch_btree_cache_params);
125 /* Cause future lookups for this node to fail: */
129 int __bch2_btree_node_hash_insert(struct btree_cache *bc, struct btree *b)
132 b->hash_val = btree_ptr_hash_val(&b->key);
134 return rhashtable_lookup_insert_fast(&bc->table, &b->hash,
135 bch_btree_cache_params);
138 int bch2_btree_node_hash_insert(struct btree_cache *bc, struct btree *b,
139 unsigned level, enum btree_id id)
146 mutex_lock(&bc->lock);
147 ret = __bch2_btree_node_hash_insert(bc, b);
149 list_add(&b->list, &bc->live);
150 mutex_unlock(&bc->lock);
156 static inline struct btree *btree_cache_find(struct btree_cache *bc,
157 const struct bkey_i *k)
159 u64 v = btree_ptr_hash_val(k);
161 return rhashtable_lookup_fast(&bc->table, &v, bch_btree_cache_params);
165 * this version is for btree nodes that have already been freed (we're not
166 * reaping a real btree node)
168 static int __btree_node_reclaim(struct bch_fs *c, struct btree *b, bool flush)
170 struct btree_cache *bc = &c->btree_cache;
173 lockdep_assert_held(&bc->lock);
175 if (!six_trylock_intent(&b->c.lock))
178 if (!six_trylock_write(&b->c.lock))
179 goto out_unlock_intent;
181 if (btree_node_noevict(b))
184 if (!btree_node_may_write(b))
187 if (btree_node_dirty(b) &&
188 test_bit(BCH_FS_HOLD_BTREE_WRITES, &c->flags))
191 if (btree_node_dirty(b) ||
192 btree_node_write_in_flight(b) ||
193 btree_node_read_in_flight(b)) {
197 wait_on_bit_io(&b->flags, BTREE_NODE_read_in_flight,
198 TASK_UNINTERRUPTIBLE);
201 * Using the underscore version because we don't want to compact
202 * bsets after the write, since this node is about to be evicted
203 * - unless btree verify mode is enabled, since it runs out of
204 * the post write cleanup:
206 if (verify_btree_ondisk(c))
207 bch2_btree_node_write(c, b, SIX_LOCK_intent);
209 __bch2_btree_node_write(c, b, SIX_LOCK_read);
211 /* wait for any in flight btree write */
212 btree_node_wait_on_io(b);
215 if (b->hash_val && !ret)
216 trace_btree_node_reap(c, b);
219 six_unlock_write(&b->c.lock);
221 six_unlock_intent(&b->c.lock);
226 static int btree_node_reclaim(struct bch_fs *c, struct btree *b)
228 return __btree_node_reclaim(c, b, false);
231 static int btree_node_write_and_reclaim(struct bch_fs *c, struct btree *b)
233 return __btree_node_reclaim(c, b, true);
236 static unsigned long bch2_btree_cache_scan(struct shrinker *shrink,
237 struct shrink_control *sc)
239 struct bch_fs *c = container_of(shrink, struct bch_fs,
241 struct btree_cache *bc = &c->btree_cache;
243 unsigned long nr = sc->nr_to_scan;
244 unsigned long can_free;
245 unsigned long touched = 0;
246 unsigned long freed = 0;
249 if (btree_shrinker_disabled(c))
252 /* Return -1 if we can't do anything right now */
253 if (sc->gfp_mask & __GFP_FS)
254 mutex_lock(&bc->lock);
255 else if (!mutex_trylock(&bc->lock))
259 * It's _really_ critical that we don't free too many btree nodes - we
260 * have to always leave ourselves a reserve. The reserve is how we
261 * guarantee that allocating memory for a new btree node can always
262 * succeed, so that inserting keys into the btree can always succeed and
263 * IO can always make forward progress:
265 nr /= btree_pages(c);
266 can_free = btree_cache_can_free(bc);
267 nr = min_t(unsigned long, nr, can_free);
270 list_for_each_entry_safe(b, t, &bc->freeable, list) {
277 !btree_node_reclaim(c, b)) {
278 btree_node_data_free(c, b);
279 six_unlock_write(&b->c.lock);
280 six_unlock_intent(&b->c.lock);
285 list_for_each_entry_safe(b, t, &bc->live, list) {
290 if (&t->list != &bc->live)
291 list_move_tail(&bc->live, &t->list);
295 if (!btree_node_accessed(b) &&
296 !btree_node_reclaim(c, b)) {
297 /* can't call bch2_btree_node_hash_remove under lock */
299 if (&t->list != &bc->live)
300 list_move_tail(&bc->live, &t->list);
302 btree_node_data_free(c, b);
303 mutex_unlock(&bc->lock);
305 bch2_btree_node_hash_remove(bc, b);
306 six_unlock_write(&b->c.lock);
307 six_unlock_intent(&b->c.lock);
312 if (sc->gfp_mask & __GFP_IO)
313 mutex_lock(&bc->lock);
314 else if (!mutex_trylock(&bc->lock))
318 clear_btree_node_accessed(b);
321 mutex_unlock(&bc->lock);
323 return (unsigned long) freed * btree_pages(c);
326 static unsigned long bch2_btree_cache_count(struct shrinker *shrink,
327 struct shrink_control *sc)
329 struct bch_fs *c = container_of(shrink, struct bch_fs,
331 struct btree_cache *bc = &c->btree_cache;
333 if (btree_shrinker_disabled(c))
336 return btree_cache_can_free(bc) * btree_pages(c);
339 void bch2_fs_btree_cache_exit(struct bch_fs *c)
341 struct btree_cache *bc = &c->btree_cache;
345 if (bc->shrink.list.next)
346 unregister_shrinker(&bc->shrink);
348 mutex_lock(&bc->lock);
350 #ifdef CONFIG_BCACHEFS_DEBUG
352 list_move(&c->verify_data->list, &bc->live);
354 kvpfree(c->verify_ondisk, btree_bytes(c));
357 for (i = 0; i < BTREE_ID_NR; i++)
358 if (c->btree_roots[i].b)
359 list_add(&c->btree_roots[i].b->list, &bc->live);
361 list_splice(&bc->freeable, &bc->live);
363 while (!list_empty(&bc->live)) {
364 b = list_first_entry(&bc->live, struct btree, list);
366 BUG_ON(btree_node_read_in_flight(b) ||
367 btree_node_write_in_flight(b));
369 if (btree_node_dirty(b))
370 bch2_btree_complete_write(c, b, btree_current_write(b));
371 clear_btree_node_dirty(b);
373 btree_node_data_free(c, b);
376 while (!list_empty(&bc->freed)) {
377 b = list_first_entry(&bc->freed, struct btree, list);
382 mutex_unlock(&bc->lock);
384 if (bc->table_init_done)
385 rhashtable_destroy(&bc->table);
388 int bch2_fs_btree_cache_init(struct bch_fs *c)
390 struct btree_cache *bc = &c->btree_cache;
394 pr_verbose_init(c->opts, "");
396 ret = rhashtable_init(&bc->table, &bch_btree_cache_params);
400 bc->table_init_done = true;
402 bch2_recalc_btree_reserve(c);
404 for (i = 0; i < bc->reserve; i++)
405 if (!btree_node_mem_alloc(c, GFP_KERNEL)) {
410 list_splice_init(&bc->live, &bc->freeable);
412 #ifdef CONFIG_BCACHEFS_DEBUG
413 mutex_init(&c->verify_lock);
415 c->verify_ondisk = kvpmalloc(btree_bytes(c), GFP_KERNEL);
416 if (!c->verify_ondisk) {
421 c->verify_data = btree_node_mem_alloc(c, GFP_KERNEL);
422 if (!c->verify_data) {
427 list_del_init(&c->verify_data->list);
430 bc->shrink.count_objects = bch2_btree_cache_count;
431 bc->shrink.scan_objects = bch2_btree_cache_scan;
432 bc->shrink.seeks = 4;
433 bc->shrink.batch = btree_pages(c) * 2;
434 register_shrinker(&bc->shrink);
436 pr_verbose_init(c->opts, "ret %i", ret);
440 void bch2_fs_btree_cache_init_early(struct btree_cache *bc)
442 mutex_init(&bc->lock);
443 INIT_LIST_HEAD(&bc->live);
444 INIT_LIST_HEAD(&bc->freeable);
445 INIT_LIST_HEAD(&bc->freed);
449 * We can only have one thread cannibalizing other cached btree nodes at a time,
450 * or we'll deadlock. We use an open coded mutex to ensure that, which a
451 * cannibalize_bucket() will take. This means every time we unlock the root of
452 * the btree, we need to release this lock if we have it held.
454 void bch2_btree_cache_cannibalize_unlock(struct bch_fs *c)
456 struct btree_cache *bc = &c->btree_cache;
458 if (bc->alloc_lock == current) {
459 trace_btree_node_cannibalize_unlock(c);
460 bc->alloc_lock = NULL;
461 closure_wake_up(&bc->alloc_wait);
465 int bch2_btree_cache_cannibalize_lock(struct bch_fs *c, struct closure *cl)
467 struct btree_cache *bc = &c->btree_cache;
468 struct task_struct *old;
470 old = cmpxchg(&bc->alloc_lock, NULL, current);
471 if (old == NULL || old == current)
475 trace_btree_node_cannibalize_lock_fail(c);
479 closure_wait(&bc->alloc_wait, cl);
481 /* Try again, after adding ourselves to waitlist */
482 old = cmpxchg(&bc->alloc_lock, NULL, current);
483 if (old == NULL || old == current) {
485 closure_wake_up(&bc->alloc_wait);
489 trace_btree_node_cannibalize_lock_fail(c);
493 trace_btree_node_cannibalize_lock(c);
497 static struct btree *btree_node_cannibalize(struct bch_fs *c)
499 struct btree_cache *bc = &c->btree_cache;
502 list_for_each_entry_reverse(b, &bc->live, list)
503 if (!btree_node_reclaim(c, b))
507 list_for_each_entry_reverse(b, &bc->live, list)
508 if (!btree_node_write_and_reclaim(c, b))
512 * Rare case: all nodes were intent-locked.
515 WARN_ONCE(1, "btree cache cannibalize failed\n");
520 struct btree *bch2_btree_node_mem_alloc(struct bch_fs *c)
522 struct btree_cache *bc = &c->btree_cache;
524 u64 start_time = local_clock();
527 flags = memalloc_nofs_save();
528 mutex_lock(&bc->lock);
531 * btree_free() doesn't free memory; it sticks the node on the end of
532 * the list. Check if there's any freed nodes there:
534 list_for_each_entry(b, &bc->freeable, list)
535 if (!btree_node_reclaim(c, b))
539 * We never free struct btree itself, just the memory that holds the on
540 * disk node. Check the freed list before allocating a new one:
542 list_for_each_entry(b, &bc->freed, list)
543 if (!btree_node_reclaim(c, b))
549 list_del_init(&b->list);
550 mutex_unlock(&bc->lock);
553 b = kzalloc(sizeof(struct btree), GFP_KERNEL);
557 bkey_btree_ptr_init(&b->key);
558 six_lock_init(&b->c.lock);
559 INIT_LIST_HEAD(&b->list);
560 INIT_LIST_HEAD(&b->write_blocked);
562 BUG_ON(!six_trylock_intent(&b->c.lock));
563 BUG_ON(!six_trylock_write(&b->c.lock));
567 if (__btree_node_data_alloc(c, b, __GFP_NOWARN|GFP_KERNEL))
570 mutex_lock(&bc->lock);
572 mutex_unlock(&bc->lock);
575 BUG_ON(btree_node_hashed(b));
576 BUG_ON(btree_node_write_in_flight(b));
583 b->whiteout_u64s = 0;
584 bch2_btree_keys_init(b, &c->expensive_debug_checks);
586 bch2_time_stats_update(&c->times[BCH_TIME_btree_node_mem_alloc],
589 memalloc_nofs_restore(flags);
592 mutex_lock(&bc->lock);
595 list_add(&b->list, &bc->freed);
596 six_unlock_write(&b->c.lock);
597 six_unlock_intent(&b->c.lock);
600 /* Try to cannibalize another cached btree node: */
601 if (bc->alloc_lock == current) {
602 b = btree_node_cannibalize(c);
603 list_del_init(&b->list);
604 mutex_unlock(&bc->lock);
606 bch2_btree_node_hash_remove(bc, b);
608 trace_btree_node_cannibalize(c);
612 mutex_unlock(&bc->lock);
613 memalloc_nofs_restore(flags);
614 return ERR_PTR(-ENOMEM);
617 /* Slowpath, don't want it inlined into btree_iter_traverse() */
618 static noinline struct btree *bch2_btree_node_fill(struct bch_fs *c,
619 struct btree_iter *iter,
620 const struct bkey_i *k,
621 enum btree_id btree_id,
623 enum six_lock_type lock_type,
626 struct btree_cache *bc = &c->btree_cache;
629 BUG_ON(level + 1 >= BTREE_MAX_DEPTH);
631 * Parent node must be locked, else we could read in a btree node that's
634 if (iter && !bch2_btree_node_relock(iter, level + 1))
635 return ERR_PTR(-EINTR);
637 b = bch2_btree_node_mem_alloc(c);
641 bkey_copy(&b->key, k);
642 if (bch2_btree_node_hash_insert(bc, b, level, btree_id)) {
643 /* raced with another fill: */
645 /* mark as unhashed... */
648 mutex_lock(&bc->lock);
649 list_add(&b->list, &bc->freeable);
650 mutex_unlock(&bc->lock);
652 six_unlock_write(&b->c.lock);
653 six_unlock_intent(&b->c.lock);
658 * Unlock before doing IO:
660 * XXX: ideally should be dropping all btree node locks here
662 if (iter && btree_node_read_locked(iter, level + 1))
663 btree_node_unlock(iter, level + 1);
665 bch2_btree_node_read(c, b, sync);
667 six_unlock_write(&b->c.lock);
670 six_unlock_intent(&b->c.lock);
674 if (lock_type == SIX_LOCK_read)
675 six_lock_downgrade(&b->c.lock);
680 static int lock_node_check_fn(struct six_lock *lock, void *p)
682 struct btree *b = container_of(lock, struct btree, c.lock);
683 const struct bkey_i *k = p;
685 return b->hash_val == btree_ptr_hash_val(k) ? 0 : -1;
689 * bch_btree_node_get - find a btree node in the cache and lock it, reading it
690 * in from disk if necessary.
692 * If IO is necessary and running under generic_make_request, returns -EAGAIN.
694 * The btree node will have either a read or a write lock held, depending on
695 * the @write parameter.
697 struct btree *bch2_btree_node_get(struct bch_fs *c, struct btree_iter *iter,
698 const struct bkey_i *k, unsigned level,
699 enum six_lock_type lock_type)
701 struct btree_cache *bc = &c->btree_cache;
705 EBUG_ON(level >= BTREE_MAX_DEPTH);
707 b = btree_node_mem_ptr(k);
711 b = btree_cache_find(bc, k);
714 * We must have the parent locked to call bch2_btree_node_fill(),
715 * else we could read in a btree node from disk that's been
718 b = bch2_btree_node_fill(c, iter, k, iter->btree_id,
719 level, lock_type, true);
721 /* We raced and found the btree node in the cache */
730 * There's a potential deadlock with splits and insertions into
731 * interior nodes we have to avoid:
733 * The other thread might be holding an intent lock on the node
734 * we want, and they want to update its parent node so they're
735 * going to upgrade their intent lock on the parent node to a
738 * But if we're holding a read lock on the parent, and we're
739 * trying to get the intent lock they're holding, we deadlock.
741 * So to avoid this we drop the read locks on parent nodes when
742 * we're starting to take intent locks - and handle the race.
744 * The race is that they might be about to free the node we
745 * want, and dropping our read lock on the parent node lets them
746 * update the parent marking the node we want as freed, and then
749 * To guard against this, btree nodes are evicted from the cache
750 * when they're freed - and b->hash_val is zeroed out, which we
751 * check for after we lock the node.
753 * Then, bch2_btree_node_relock() on the parent will fail - because
754 * the parent was modified, when the pointer to the node we want
755 * was removed - and we'll bail out:
757 if (btree_node_read_locked(iter, level + 1))
758 btree_node_unlock(iter, level + 1);
760 if (!btree_node_lock(b, k->k.p, level, iter, lock_type,
761 lock_node_check_fn, (void *) k)) {
762 if (b->hash_val != btree_ptr_hash_val(k))
764 return ERR_PTR(-EINTR);
767 if (unlikely(b->hash_val != btree_ptr_hash_val(k) ||
768 b->c.level != level ||
770 six_unlock_type(&b->c.lock, lock_type);
771 if (bch2_btree_node_relock(iter, level + 1))
774 trace_trans_restart_btree_node_reused(iter->trans->ip);
775 return ERR_PTR(-EINTR);
779 /* XXX: waiting on IO with btree locks held: */
780 wait_on_bit_io(&b->flags, BTREE_NODE_read_in_flight,
781 TASK_UNINTERRUPTIBLE);
783 prefetch(b->aux_data);
785 for_each_bset(b, t) {
786 void *p = (u64 *) b->aux_data + t->aux_data_offset;
788 prefetch(p + L1_CACHE_BYTES * 0);
789 prefetch(p + L1_CACHE_BYTES * 1);
790 prefetch(p + L1_CACHE_BYTES * 2);
793 /* avoid atomic set bit if it's not needed: */
794 if (!btree_node_accessed(b))
795 set_btree_node_accessed(b);
797 if (unlikely(btree_node_read_error(b))) {
798 six_unlock_type(&b->c.lock, lock_type);
799 return ERR_PTR(-EIO);
802 EBUG_ON(b->c.btree_id != iter->btree_id ||
803 BTREE_NODE_LEVEL(b->data) != level ||
804 bkey_cmp(b->data->max_key, k->k.p));
809 struct btree *bch2_btree_node_get_noiter(struct bch_fs *c,
810 const struct bkey_i *k,
811 enum btree_id btree_id,
814 struct btree_cache *bc = &c->btree_cache;
819 EBUG_ON(level >= BTREE_MAX_DEPTH);
821 b = btree_node_mem_ptr(k);
825 b = btree_cache_find(bc, k);
827 b = bch2_btree_node_fill(c, NULL, k, btree_id,
828 level, SIX_LOCK_read, true);
830 /* We raced and found the btree node in the cache */
838 ret = six_lock_read(&b->c.lock, lock_node_check_fn, (void *) k);
842 if (unlikely(b->hash_val != btree_ptr_hash_val(k) ||
843 b->c.btree_id != btree_id ||
844 b->c.level != level)) {
845 six_unlock_read(&b->c.lock);
850 /* XXX: waiting on IO with btree locks held: */
851 wait_on_bit_io(&b->flags, BTREE_NODE_read_in_flight,
852 TASK_UNINTERRUPTIBLE);
854 prefetch(b->aux_data);
856 for_each_bset(b, t) {
857 void *p = (u64 *) b->aux_data + t->aux_data_offset;
859 prefetch(p + L1_CACHE_BYTES * 0);
860 prefetch(p + L1_CACHE_BYTES * 1);
861 prefetch(p + L1_CACHE_BYTES * 2);
864 /* avoid atomic set bit if it's not needed: */
865 if (!btree_node_accessed(b))
866 set_btree_node_accessed(b);
868 if (unlikely(btree_node_read_error(b))) {
869 six_unlock_read(&b->c.lock);
870 return ERR_PTR(-EIO);
873 EBUG_ON(b->c.btree_id != btree_id ||
874 BTREE_NODE_LEVEL(b->data) != level ||
875 bkey_cmp(b->data->max_key, k->k.p));
880 struct btree *bch2_btree_node_get_sibling(struct bch_fs *c,
881 struct btree_iter *iter,
883 enum btree_node_sibling sib)
885 struct btree_trans *trans = iter->trans;
886 struct btree *parent;
887 struct btree_node_iter node_iter;
888 struct bkey_packed *k;
890 struct btree *ret = NULL;
891 unsigned level = b->c.level;
893 parent = btree_iter_node(iter, level + 1);
898 * There's a corner case where a btree_iter might have a node locked
899 * that is just outside its current pos - when
900 * bch2_btree_iter_set_pos_same_leaf() gets to the end of the node.
902 * But the lock ordering checks in __bch2_btree_node_lock() go off of
903 * iter->pos, not the node's key: so if the iterator is marked as
904 * needing to be traversed, we risk deadlock if we don't bail out here:
906 if (iter->uptodate >= BTREE_ITER_NEED_TRAVERSE)
907 return ERR_PTR(-EINTR);
909 if (!bch2_btree_node_relock(iter, level + 1)) {
910 ret = ERR_PTR(-EINTR);
914 node_iter = iter->l[parent->c.level].iter;
916 k = bch2_btree_node_iter_peek_all(&node_iter, parent);
917 BUG_ON(bkey_cmp_left_packed(parent, k, &b->key.k.p));
919 k = sib == btree_prev_sib
920 ? bch2_btree_node_iter_prev(&node_iter, parent)
921 : (bch2_btree_node_iter_advance(&node_iter, parent),
922 bch2_btree_node_iter_peek(&node_iter, parent));
926 bch2_bkey_unpack(parent, &tmp.k, k);
928 ret = bch2_btree_node_get(c, iter, &tmp.k, level,
931 if (PTR_ERR_OR_ZERO(ret) == -EINTR && !trans->nounlock) {
932 struct btree_iter *linked;
934 if (!bch2_btree_node_relock(iter, level + 1))
938 * We might have got -EINTR because trylock failed, and we're
939 * holding other locks that would cause us to deadlock:
941 trans_for_each_iter(trans, linked)
942 if (btree_iter_cmp(iter, linked) < 0)
943 __bch2_btree_iter_unlock(linked);
945 if (sib == btree_prev_sib)
946 btree_node_unlock(iter, level);
948 ret = bch2_btree_node_get(c, iter, &tmp.k, level,
952 * before btree_iter_relock() calls btree_iter_verify_locks():
954 if (btree_lock_want(iter, level + 1) == BTREE_NODE_UNLOCKED)
955 btree_node_unlock(iter, level + 1);
957 if (!bch2_btree_node_relock(iter, level)) {
958 btree_iter_set_dirty(iter, BTREE_ITER_NEED_RELOCK);
961 six_unlock_intent(&ret->c.lock);
962 ret = ERR_PTR(-EINTR);
966 bch2_trans_relock(trans);
969 if (btree_lock_want(iter, level + 1) == BTREE_NODE_UNLOCKED)
970 btree_node_unlock(iter, level + 1);
972 if (PTR_ERR_OR_ZERO(ret) == -EINTR)
973 bch2_btree_iter_upgrade(iter, level + 2);
975 BUG_ON(!IS_ERR(ret) && !btree_node_locked(iter, level));
977 if (!IS_ERR_OR_NULL(ret)) {
978 struct btree *n1 = ret, *n2 = b;
980 if (sib != btree_prev_sib)
983 BUG_ON(bkey_cmp(bkey_successor(n1->key.k.p),
987 bch2_btree_trans_verify_locks(trans);
992 void bch2_btree_node_prefetch(struct bch_fs *c, struct btree_iter *iter,
993 const struct bkey_i *k, unsigned level)
995 struct btree_cache *bc = &c->btree_cache;
998 BUG_ON(!btree_node_locked(iter, level + 1));
999 BUG_ON(level >= BTREE_MAX_DEPTH);
1001 b = btree_cache_find(bc, k);
1005 bch2_btree_node_fill(c, iter, k, iter->btree_id,
1006 level, SIX_LOCK_read, false);
1009 void bch2_btree_node_to_text(struct printbuf *out, struct bch_fs *c,
1012 const struct bkey_format *f = &b->format;
1013 struct bset_stats stats;
1015 memset(&stats, 0, sizeof(stats));
1017 bch2_btree_keys_stats(b, &stats);
1020 "l %u %llu:%llu - %llu:%llu:\n"
1023 b->data->min_key.inode,
1024 b->data->min_key.offset,
1025 b->data->max_key.inode,
1026 b->data->max_key.offset);
1027 bch2_val_to_text(out, c, bkey_i_to_s_c(&b->key));
1029 " format: u64s %u fields %u %u %u %u %u\n"
1030 " unpack fn len: %u\n"
1031 " bytes used %zu/%zu (%zu%% full)\n"
1032 " sib u64s: %u, %u (merge threshold %zu)\n"
1033 " nr packed keys %u\n"
1034 " nr unpacked keys %u\n"
1036 " failed unpacked %zu\n",
1038 f->bits_per_field[0],
1039 f->bits_per_field[1],
1040 f->bits_per_field[2],
1041 f->bits_per_field[3],
1042 f->bits_per_field[4],
1044 b->nr.live_u64s * sizeof(u64),
1045 btree_bytes(c) - sizeof(struct btree_node),
1046 b->nr.live_u64s * 100 / btree_max_u64s(c),
1049 BTREE_FOREGROUND_MERGE_THRESHOLD(c),
1051 b->nr.unpacked_keys,