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->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 void btree_node_data_alloc(struct bch_fs *c, struct btree *b, gfp_t gfp)
77 struct btree_cache *bc = &c->btree_cache;
79 b->data = kvpmalloc(btree_bytes(c), gfp);
83 if (bch2_btree_keys_alloc(b, btree_page_order(c), gfp))
87 list_move(&b->list, &bc->freeable);
90 kvpfree(b->data, btree_bytes(c));
92 list_move(&b->list, &bc->freed);
95 static struct btree *btree_node_mem_alloc(struct bch_fs *c, gfp_t gfp)
97 struct btree *b = kzalloc(sizeof(struct btree), gfp);
101 bkey_btree_ptr_init(&b->key);
102 six_lock_init(&b->lock);
103 INIT_LIST_HEAD(&b->list);
104 INIT_LIST_HEAD(&b->write_blocked);
106 btree_node_data_alloc(c, b, gfp);
107 return b->data ? b : NULL;
110 /* Btree in memory cache - hash table */
112 void bch2_btree_node_hash_remove(struct btree_cache *bc, struct btree *b)
114 rhashtable_remove_fast(&bc->table, &b->hash, bch_btree_cache_params);
116 /* Cause future lookups for this node to fail: */
120 int __bch2_btree_node_hash_insert(struct btree_cache *bc, struct btree *b)
123 b->hash_val = btree_ptr_hash_val(&b->key);
125 return rhashtable_lookup_insert_fast(&bc->table, &b->hash,
126 bch_btree_cache_params);
129 int bch2_btree_node_hash_insert(struct btree_cache *bc, struct btree *b,
130 unsigned level, enum btree_id id)
137 mutex_lock(&bc->lock);
138 ret = __bch2_btree_node_hash_insert(bc, b);
140 list_add(&b->list, &bc->live);
141 mutex_unlock(&bc->lock);
147 static inline struct btree *btree_cache_find(struct btree_cache *bc,
148 const struct bkey_i *k)
150 u64 v = btree_ptr_hash_val(k);
152 return rhashtable_lookup_fast(&bc->table, &v, bch_btree_cache_params);
156 * this version is for btree nodes that have already been freed (we're not
157 * reaping a real btree node)
159 static int __btree_node_reclaim(struct bch_fs *c, struct btree *b, bool flush)
161 struct btree_cache *bc = &c->btree_cache;
164 lockdep_assert_held(&bc->lock);
166 if (!six_trylock_intent(&b->lock))
169 if (!six_trylock_write(&b->lock))
170 goto out_unlock_intent;
172 if (btree_node_noevict(b))
175 if (!btree_node_may_write(b))
178 if (btree_node_dirty(b) &&
179 test_bit(BCH_FS_HOLD_BTREE_WRITES, &c->flags))
182 if (btree_node_dirty(b) ||
183 btree_node_write_in_flight(b) ||
184 btree_node_read_in_flight(b)) {
188 wait_on_bit_io(&b->flags, BTREE_NODE_read_in_flight,
189 TASK_UNINTERRUPTIBLE);
192 * Using the underscore version because we don't want to compact
193 * bsets after the write, since this node is about to be evicted
194 * - unless btree verify mode is enabled, since it runs out of
195 * the post write cleanup:
197 if (verify_btree_ondisk(c))
198 bch2_btree_node_write(c, b, SIX_LOCK_intent);
200 __bch2_btree_node_write(c, b, SIX_LOCK_read);
202 /* wait for any in flight btree write */
203 btree_node_wait_on_io(b);
206 if (b->hash_val && !ret)
207 trace_btree_node_reap(c, b);
210 six_unlock_write(&b->lock);
212 six_unlock_intent(&b->lock);
217 static int btree_node_reclaim(struct bch_fs *c, struct btree *b)
219 return __btree_node_reclaim(c, b, false);
222 static int btree_node_write_and_reclaim(struct bch_fs *c, struct btree *b)
224 return __btree_node_reclaim(c, b, true);
227 static unsigned long bch2_btree_cache_scan(struct shrinker *shrink,
228 struct shrink_control *sc)
230 struct bch_fs *c = container_of(shrink, struct bch_fs,
232 struct btree_cache *bc = &c->btree_cache;
234 unsigned long nr = sc->nr_to_scan;
235 unsigned long can_free;
236 unsigned long touched = 0;
237 unsigned long freed = 0;
240 if (btree_shrinker_disabled(c))
243 /* Return -1 if we can't do anything right now */
244 if (sc->gfp_mask & __GFP_IO)
245 mutex_lock(&bc->lock);
246 else if (!mutex_trylock(&bc->lock))
250 * It's _really_ critical that we don't free too many btree nodes - we
251 * have to always leave ourselves a reserve. The reserve is how we
252 * guarantee that allocating memory for a new btree node can always
253 * succeed, so that inserting keys into the btree can always succeed and
254 * IO can always make forward progress:
256 nr /= btree_pages(c);
257 can_free = btree_cache_can_free(bc);
258 nr = min_t(unsigned long, nr, can_free);
261 list_for_each_entry_safe(b, t, &bc->freeable, list) {
268 !btree_node_reclaim(c, b)) {
269 btree_node_data_free(c, b);
270 six_unlock_write(&b->lock);
271 six_unlock_intent(&b->lock);
276 list_for_each_entry_safe(b, t, &bc->live, list) {
281 if (&t->list != &bc->live)
282 list_move_tail(&bc->live, &t->list);
286 if (!btree_node_accessed(b) &&
287 !btree_node_reclaim(c, b)) {
288 /* can't call bch2_btree_node_hash_remove under lock */
290 if (&t->list != &bc->live)
291 list_move_tail(&bc->live, &t->list);
293 btree_node_data_free(c, b);
294 mutex_unlock(&bc->lock);
296 bch2_btree_node_hash_remove(bc, b);
297 six_unlock_write(&b->lock);
298 six_unlock_intent(&b->lock);
303 if (sc->gfp_mask & __GFP_IO)
304 mutex_lock(&bc->lock);
305 else if (!mutex_trylock(&bc->lock))
309 clear_btree_node_accessed(b);
312 mutex_unlock(&bc->lock);
314 return (unsigned long) freed * btree_pages(c);
317 static unsigned long bch2_btree_cache_count(struct shrinker *shrink,
318 struct shrink_control *sc)
320 struct bch_fs *c = container_of(shrink, struct bch_fs,
322 struct btree_cache *bc = &c->btree_cache;
324 if (btree_shrinker_disabled(c))
327 return btree_cache_can_free(bc) * btree_pages(c);
330 void bch2_fs_btree_cache_exit(struct bch_fs *c)
332 struct btree_cache *bc = &c->btree_cache;
336 if (bc->shrink.list.next)
337 unregister_shrinker(&bc->shrink);
339 mutex_lock(&bc->lock);
341 #ifdef CONFIG_BCACHEFS_DEBUG
343 list_move(&c->verify_data->list, &bc->live);
345 kvpfree(c->verify_ondisk, btree_bytes(c));
348 for (i = 0; i < BTREE_ID_NR; i++)
349 if (c->btree_roots[i].b)
350 list_add(&c->btree_roots[i].b->list, &bc->live);
352 list_splice(&bc->freeable, &bc->live);
354 while (!list_empty(&bc->live)) {
355 b = list_first_entry(&bc->live, struct btree, list);
357 BUG_ON(btree_node_read_in_flight(b) ||
358 btree_node_write_in_flight(b));
360 if (btree_node_dirty(b))
361 bch2_btree_complete_write(c, b, btree_current_write(b));
362 clear_btree_node_dirty(b);
364 btree_node_data_free(c, b);
367 while (!list_empty(&bc->freed)) {
368 b = list_first_entry(&bc->freed, struct btree, list);
373 mutex_unlock(&bc->lock);
375 if (bc->table_init_done)
376 rhashtable_destroy(&bc->table);
379 int bch2_fs_btree_cache_init(struct bch_fs *c)
381 struct btree_cache *bc = &c->btree_cache;
385 pr_verbose_init(c->opts, "");
387 ret = rhashtable_init(&bc->table, &bch_btree_cache_params);
391 bc->table_init_done = true;
393 bch2_recalc_btree_reserve(c);
395 for (i = 0; i < bc->reserve; i++)
396 if (!btree_node_mem_alloc(c, GFP_KERNEL)) {
401 list_splice_init(&bc->live, &bc->freeable);
403 #ifdef CONFIG_BCACHEFS_DEBUG
404 mutex_init(&c->verify_lock);
406 c->verify_ondisk = kvpmalloc(btree_bytes(c), GFP_KERNEL);
407 if (!c->verify_ondisk) {
412 c->verify_data = btree_node_mem_alloc(c, GFP_KERNEL);
413 if (!c->verify_data) {
418 list_del_init(&c->verify_data->list);
421 bc->shrink.count_objects = bch2_btree_cache_count;
422 bc->shrink.scan_objects = bch2_btree_cache_scan;
423 bc->shrink.seeks = 4;
424 bc->shrink.batch = btree_pages(c) * 2;
425 register_shrinker(&bc->shrink);
427 pr_verbose_init(c->opts, "ret %i", ret);
431 void bch2_fs_btree_cache_init_early(struct btree_cache *bc)
433 mutex_init(&bc->lock);
434 INIT_LIST_HEAD(&bc->live);
435 INIT_LIST_HEAD(&bc->freeable);
436 INIT_LIST_HEAD(&bc->freed);
440 * We can only have one thread cannibalizing other cached btree nodes at a time,
441 * or we'll deadlock. We use an open coded mutex to ensure that, which a
442 * cannibalize_bucket() will take. This means every time we unlock the root of
443 * the btree, we need to release this lock if we have it held.
445 void bch2_btree_cache_cannibalize_unlock(struct bch_fs *c)
447 struct btree_cache *bc = &c->btree_cache;
449 if (bc->alloc_lock == current) {
450 trace_btree_node_cannibalize_unlock(c);
451 bc->alloc_lock = NULL;
452 closure_wake_up(&bc->alloc_wait);
456 int bch2_btree_cache_cannibalize_lock(struct bch_fs *c, struct closure *cl)
458 struct btree_cache *bc = &c->btree_cache;
459 struct task_struct *old;
461 old = cmpxchg(&bc->alloc_lock, NULL, current);
462 if (old == NULL || old == current)
466 trace_btree_node_cannibalize_lock_fail(c);
470 closure_wait(&bc->alloc_wait, cl);
472 /* Try again, after adding ourselves to waitlist */
473 old = cmpxchg(&bc->alloc_lock, NULL, current);
474 if (old == NULL || old == current) {
476 closure_wake_up(&bc->alloc_wait);
480 trace_btree_node_cannibalize_lock_fail(c);
484 trace_btree_node_cannibalize_lock(c);
488 static struct btree *btree_node_cannibalize(struct bch_fs *c)
490 struct btree_cache *bc = &c->btree_cache;
493 list_for_each_entry_reverse(b, &bc->live, list)
494 if (!btree_node_reclaim(c, b))
498 list_for_each_entry_reverse(b, &bc->live, list)
499 if (!btree_node_write_and_reclaim(c, b))
503 * Rare case: all nodes were intent-locked.
506 WARN_ONCE(1, "btree cache cannibalize failed\n");
511 struct btree *bch2_btree_node_mem_alloc(struct bch_fs *c)
513 struct btree_cache *bc = &c->btree_cache;
515 u64 start_time = local_clock();
518 flags = memalloc_nofs_save();
519 mutex_lock(&bc->lock);
522 * btree_free() doesn't free memory; it sticks the node on the end of
523 * the list. Check if there's any freed nodes there:
525 list_for_each_entry(b, &bc->freeable, list)
526 if (!btree_node_reclaim(c, b))
530 * We never free struct btree itself, just the memory that holds the on
531 * disk node. Check the freed list before allocating a new one:
533 list_for_each_entry(b, &bc->freed, list)
534 if (!btree_node_reclaim(c, b)) {
535 btree_node_data_alloc(c, b, __GFP_NOWARN|GFP_NOIO);
539 six_unlock_write(&b->lock);
540 six_unlock_intent(&b->lock);
544 b = btree_node_mem_alloc(c, __GFP_NOWARN|GFP_NOIO);
548 BUG_ON(!six_trylock_intent(&b->lock));
549 BUG_ON(!six_trylock_write(&b->lock));
551 BUG_ON(btree_node_hashed(b));
552 BUG_ON(btree_node_write_in_flight(b));
554 list_del_init(&b->list);
555 mutex_unlock(&bc->lock);
562 b->whiteout_u64s = 0;
563 bch2_btree_keys_init(b, &c->expensive_debug_checks);
565 bch2_time_stats_update(&c->times[BCH_TIME_btree_node_mem_alloc],
568 memalloc_nofs_restore(flags);
571 /* Try to cannibalize another cached btree node: */
572 if (bc->alloc_lock == current) {
573 b = btree_node_cannibalize(c);
574 list_del_init(&b->list);
575 mutex_unlock(&bc->lock);
577 bch2_btree_node_hash_remove(bc, b);
579 trace_btree_node_cannibalize(c);
583 mutex_unlock(&bc->lock);
584 memalloc_nofs_restore(flags);
585 return ERR_PTR(-ENOMEM);
588 /* Slowpath, don't want it inlined into btree_iter_traverse() */
589 static noinline struct btree *bch2_btree_node_fill(struct bch_fs *c,
590 struct btree_iter *iter,
591 const struct bkey_i *k,
592 enum btree_id btree_id,
594 enum six_lock_type lock_type,
597 struct btree_cache *bc = &c->btree_cache;
600 BUG_ON(level + 1 >= BTREE_MAX_DEPTH);
602 * Parent node must be locked, else we could read in a btree node that's
605 if (iter && !bch2_btree_node_relock(iter, level + 1))
606 return ERR_PTR(-EINTR);
608 b = bch2_btree_node_mem_alloc(c);
612 bkey_copy(&b->key, k);
613 if (bch2_btree_node_hash_insert(bc, b, level, btree_id)) {
614 /* raced with another fill: */
616 /* mark as unhashed... */
619 mutex_lock(&bc->lock);
620 list_add(&b->list, &bc->freeable);
621 mutex_unlock(&bc->lock);
623 six_unlock_write(&b->lock);
624 six_unlock_intent(&b->lock);
629 * Unlock before doing IO:
631 * XXX: ideally should be dropping all btree node locks here
633 if (iter && btree_node_read_locked(iter, level + 1))
634 btree_node_unlock(iter, level + 1);
636 bch2_btree_node_read(c, b, sync);
638 six_unlock_write(&b->lock);
641 six_unlock_intent(&b->lock);
645 if (lock_type == SIX_LOCK_read)
646 six_lock_downgrade(&b->lock);
652 * bch_btree_node_get - find a btree node in the cache and lock it, reading it
653 * in from disk if necessary.
655 * If IO is necessary and running under generic_make_request, returns -EAGAIN.
657 * The btree node will have either a read or a write lock held, depending on
658 * the @write parameter.
660 struct btree *bch2_btree_node_get(struct bch_fs *c, struct btree_iter *iter,
661 const struct bkey_i *k, unsigned level,
662 enum six_lock_type lock_type)
664 struct btree_cache *bc = &c->btree_cache;
668 EBUG_ON(level >= BTREE_MAX_DEPTH);
670 b = btree_node_mem_ptr(k);
674 b = btree_cache_find(bc, k);
677 * We must have the parent locked to call bch2_btree_node_fill(),
678 * else we could read in a btree node from disk that's been
681 b = bch2_btree_node_fill(c, iter, k, iter->btree_id,
682 level, lock_type, true);
684 /* We raced and found the btree node in the cache */
693 * There's a potential deadlock with splits and insertions into
694 * interior nodes we have to avoid:
696 * The other thread might be holding an intent lock on the node
697 * we want, and they want to update its parent node so they're
698 * going to upgrade their intent lock on the parent node to a
701 * But if we're holding a read lock on the parent, and we're
702 * trying to get the intent lock they're holding, we deadlock.
704 * So to avoid this we drop the read locks on parent nodes when
705 * we're starting to take intent locks - and handle the race.
707 * The race is that they might be about to free the node we
708 * want, and dropping our read lock on the parent node lets them
709 * update the parent marking the node we want as freed, and then
712 * To guard against this, btree nodes are evicted from the cache
713 * when they're freed - and b->hash_val is zeroed out, which we
714 * check for after we lock the node.
716 * Then, bch2_btree_node_relock() on the parent will fail - because
717 * the parent was modified, when the pointer to the node we want
718 * was removed - and we'll bail out:
720 if (btree_node_read_locked(iter, level + 1))
721 btree_node_unlock(iter, level + 1);
723 if (!btree_node_lock(b, k->k.p, level, iter, lock_type))
724 return ERR_PTR(-EINTR);
726 if (unlikely(b->hash_val != btree_ptr_hash_val(k) ||
729 six_unlock_type(&b->lock, lock_type);
730 if (bch2_btree_node_relock(iter, level + 1))
733 trace_trans_restart_btree_node_reused(iter->trans->ip);
734 return ERR_PTR(-EINTR);
738 /* XXX: waiting on IO with btree locks held: */
739 wait_on_bit_io(&b->flags, BTREE_NODE_read_in_flight,
740 TASK_UNINTERRUPTIBLE);
742 prefetch(b->aux_data);
744 for_each_bset(b, t) {
745 void *p = (u64 *) b->aux_data + t->aux_data_offset;
747 prefetch(p + L1_CACHE_BYTES * 0);
748 prefetch(p + L1_CACHE_BYTES * 1);
749 prefetch(p + L1_CACHE_BYTES * 2);
752 /* avoid atomic set bit if it's not needed: */
753 if (!btree_node_accessed(b))
754 set_btree_node_accessed(b);
756 if (unlikely(btree_node_read_error(b))) {
757 six_unlock_type(&b->lock, lock_type);
758 return ERR_PTR(-EIO);
761 EBUG_ON(b->btree_id != iter->btree_id ||
762 BTREE_NODE_LEVEL(b->data) != level ||
763 bkey_cmp(b->data->max_key, k->k.p));
768 struct btree *bch2_btree_node_get_noiter(struct bch_fs *c,
769 const struct bkey_i *k,
770 enum btree_id btree_id,
773 struct btree_cache *bc = &c->btree_cache;
777 EBUG_ON(level >= BTREE_MAX_DEPTH);
779 b = btree_node_mem_ptr(k);
783 b = btree_cache_find(bc, k);
785 b = bch2_btree_node_fill(c, NULL, k, btree_id,
786 level, SIX_LOCK_read, true);
788 /* We raced and found the btree node in the cache */
796 six_lock_read(&b->lock);
798 if (unlikely(b->hash_val != btree_ptr_hash_val(k) ||
799 b->btree_id != btree_id ||
800 b->level != level)) {
801 six_unlock_read(&b->lock);
806 /* XXX: waiting on IO with btree locks held: */
807 wait_on_bit_io(&b->flags, BTREE_NODE_read_in_flight,
808 TASK_UNINTERRUPTIBLE);
810 prefetch(b->aux_data);
812 for_each_bset(b, t) {
813 void *p = (u64 *) b->aux_data + t->aux_data_offset;
815 prefetch(p + L1_CACHE_BYTES * 0);
816 prefetch(p + L1_CACHE_BYTES * 1);
817 prefetch(p + L1_CACHE_BYTES * 2);
820 /* avoid atomic set bit if it's not needed: */
821 if (!btree_node_accessed(b))
822 set_btree_node_accessed(b);
824 if (unlikely(btree_node_read_error(b))) {
825 six_unlock_read(&b->lock);
826 return ERR_PTR(-EIO);
829 EBUG_ON(b->btree_id != btree_id ||
830 BTREE_NODE_LEVEL(b->data) != level ||
831 bkey_cmp(b->data->max_key, k->k.p));
836 struct btree *bch2_btree_node_get_sibling(struct bch_fs *c,
837 struct btree_iter *iter,
839 enum btree_node_sibling sib)
841 struct btree_trans *trans = iter->trans;
842 struct btree *parent;
843 struct btree_node_iter node_iter;
844 struct bkey_packed *k;
846 struct btree *ret = NULL;
847 unsigned level = b->level;
849 parent = btree_iter_node(iter, level + 1);
854 * There's a corner case where a btree_iter might have a node locked
855 * that is just outside its current pos - when
856 * bch2_btree_iter_set_pos_same_leaf() gets to the end of the node.
858 * But the lock ordering checks in __bch2_btree_node_lock() go off of
859 * iter->pos, not the node's key: so if the iterator is marked as
860 * needing to be traversed, we risk deadlock if we don't bail out here:
862 if (iter->uptodate >= BTREE_ITER_NEED_TRAVERSE)
863 return ERR_PTR(-EINTR);
865 if (!bch2_btree_node_relock(iter, level + 1)) {
866 ret = ERR_PTR(-EINTR);
870 node_iter = iter->l[parent->level].iter;
872 k = bch2_btree_node_iter_peek_all(&node_iter, parent);
873 BUG_ON(bkey_cmp_left_packed(parent, k, &b->key.k.p));
875 k = sib == btree_prev_sib
876 ? bch2_btree_node_iter_prev(&node_iter, parent)
877 : (bch2_btree_node_iter_advance(&node_iter, parent),
878 bch2_btree_node_iter_peek(&node_iter, parent));
882 bch2_bkey_unpack(parent, &tmp.k, k);
884 ret = bch2_btree_node_get(c, iter, &tmp.k, level,
887 if (PTR_ERR_OR_ZERO(ret) == -EINTR && !trans->nounlock) {
888 struct btree_iter *linked;
890 if (!bch2_btree_node_relock(iter, level + 1))
894 * We might have got -EINTR because trylock failed, and we're
895 * holding other locks that would cause us to deadlock:
897 trans_for_each_iter(trans, linked)
898 if (btree_iter_cmp(iter, linked) < 0)
899 __bch2_btree_iter_unlock(linked);
901 if (sib == btree_prev_sib)
902 btree_node_unlock(iter, level);
904 ret = bch2_btree_node_get(c, iter, &tmp.k, level,
908 * before btree_iter_relock() calls btree_iter_verify_locks():
910 if (btree_lock_want(iter, level + 1) == BTREE_NODE_UNLOCKED)
911 btree_node_unlock(iter, level + 1);
913 if (!bch2_btree_node_relock(iter, level)) {
914 btree_iter_set_dirty(iter, BTREE_ITER_NEED_RELOCK);
917 six_unlock_intent(&ret->lock);
918 ret = ERR_PTR(-EINTR);
922 bch2_trans_relock(trans);
925 if (btree_lock_want(iter, level + 1) == BTREE_NODE_UNLOCKED)
926 btree_node_unlock(iter, level + 1);
928 if (PTR_ERR_OR_ZERO(ret) == -EINTR)
929 bch2_btree_iter_upgrade(iter, level + 2);
931 BUG_ON(!IS_ERR(ret) && !btree_node_locked(iter, level));
933 if (!IS_ERR_OR_NULL(ret)) {
934 struct btree *n1 = ret, *n2 = b;
936 if (sib != btree_prev_sib)
939 BUG_ON(bkey_cmp(bkey_successor(n1->key.k.p),
943 bch2_btree_trans_verify_locks(trans);
948 void bch2_btree_node_prefetch(struct bch_fs *c, struct btree_iter *iter,
949 const struct bkey_i *k, unsigned level)
951 struct btree_cache *bc = &c->btree_cache;
954 BUG_ON(!btree_node_locked(iter, level + 1));
955 BUG_ON(level >= BTREE_MAX_DEPTH);
957 b = btree_cache_find(bc, k);
961 bch2_btree_node_fill(c, iter, k, iter->btree_id,
962 level, SIX_LOCK_read, false);
965 void bch2_btree_node_to_text(struct printbuf *out, struct bch_fs *c,
968 const struct bkey_format *f = &b->format;
969 struct bset_stats stats;
971 memset(&stats, 0, sizeof(stats));
973 bch2_btree_keys_stats(b, &stats);
976 "l %u %llu:%llu - %llu:%llu:\n"
979 b->data->min_key.inode,
980 b->data->min_key.offset,
981 b->data->max_key.inode,
982 b->data->max_key.offset);
983 bch2_val_to_text(out, c, bkey_i_to_s_c(&b->key));
985 " format: u64s %u fields %u %u %u %u %u\n"
986 " unpack fn len: %u\n"
987 " bytes used %zu/%zu (%zu%% full)\n"
988 " sib u64s: %u, %u (merge threshold %zu)\n"
989 " nr packed keys %u\n"
990 " nr unpacked keys %u\n"
992 " failed unpacked %zu\n",
994 f->bits_per_field[0],
995 f->bits_per_field[1],
996 f->bits_per_field[2],
997 f->bits_per_field[3],
998 f->bits_per_field[4],
1000 b->nr.live_u64s * sizeof(u64),
1001 btree_bytes(c) - sizeof(struct btree_node),
1002 b->nr.live_u64s * 100 / btree_max_u64s(c),
1005 BTREE_FOREGROUND_MERGE_THRESHOLD(c),
1007 b->nr.unpacked_keys,