1 // SPDX-License-Identifier: GPL-2.0
5 #include "btree_cache.h"
7 #include "btree_iter.h"
8 #include "btree_locking.h"
12 #include <linux/prefetch.h>
13 #include <linux/sched/mm.h>
14 #include <trace/events/bcachefs.h>
16 void bch2_recalc_btree_reserve(struct bch_fs *c)
18 unsigned i, reserve = 16;
20 if (!c->btree_roots[0].b)
23 for (i = 0; i < BTREE_ID_NR; i++)
24 if (c->btree_roots[i].b)
25 reserve += min_t(unsigned, 1,
26 c->btree_roots[i].b->c.level) * 8;
28 c->btree_cache.reserve = reserve;
31 static inline unsigned btree_cache_can_free(struct btree_cache *bc)
33 return max_t(int, 0, bc->used - bc->reserve);
36 static void __btree_node_data_free(struct bch_fs *c, struct btree *b)
38 EBUG_ON(btree_node_write_in_flight(b));
40 kvpfree(b->data, btree_bytes(c));
46 static void btree_node_data_free(struct bch_fs *c, struct btree *b)
48 struct btree_cache *bc = &c->btree_cache;
50 __btree_node_data_free(c, b);
52 list_move(&b->list, &bc->freed);
55 static int bch2_btree_cache_cmp_fn(struct rhashtable_compare_arg *arg,
58 const struct btree *b = obj;
59 const u64 *v = arg->key;
61 return b->hash_val == *v ? 0 : 1;
64 static const struct rhashtable_params bch_btree_cache_params = {
65 .head_offset = offsetof(struct btree, hash),
66 .key_offset = offsetof(struct btree, hash_val),
67 .key_len = sizeof(u64),
68 .obj_cmpfn = bch2_btree_cache_cmp_fn,
71 static int btree_node_data_alloc(struct bch_fs *c, struct btree *b, gfp_t gfp)
73 BUG_ON(b->data || b->aux_data);
75 b->data = kvpmalloc(btree_bytes(c), gfp);
79 b->aux_data = vmalloc_exec(btree_aux_data_bytes(b), gfp);
81 kvpfree(b->data, btree_bytes(c));
89 static struct btree *__btree_node_mem_alloc(struct bch_fs *c)
91 struct btree *b = kzalloc(sizeof(struct btree), GFP_KERNEL);
95 bkey_btree_ptr_init(&b->key);
96 six_lock_init(&b->c.lock);
97 INIT_LIST_HEAD(&b->list);
98 INIT_LIST_HEAD(&b->write_blocked);
99 b->byte_order = ilog2(btree_bytes(c));
103 static struct btree *btree_node_mem_alloc(struct bch_fs *c)
105 struct btree_cache *bc = &c->btree_cache;
106 struct btree *b = __btree_node_mem_alloc(c);
110 if (btree_node_data_alloc(c, b, GFP_KERNEL)) {
116 list_add(&b->list, &bc->freeable);
120 /* Btree in memory cache - hash table */
122 void bch2_btree_node_hash_remove(struct btree_cache *bc, struct btree *b)
124 rhashtable_remove_fast(&bc->table, &b->hash, bch_btree_cache_params);
126 /* Cause future lookups for this node to fail: */
129 six_lock_wakeup_all(&b->c.lock);
132 int __bch2_btree_node_hash_insert(struct btree_cache *bc, struct btree *b)
135 b->hash_val = btree_ptr_hash_val(&b->key);
137 return rhashtable_lookup_insert_fast(&bc->table, &b->hash,
138 bch_btree_cache_params);
141 int bch2_btree_node_hash_insert(struct btree_cache *bc, struct btree *b,
142 unsigned level, enum btree_id id)
150 six_lock_pcpu_alloc(&b->c.lock);
152 six_lock_pcpu_free_rcu(&b->c.lock);
154 mutex_lock(&bc->lock);
155 ret = __bch2_btree_node_hash_insert(bc, b);
157 list_add(&b->list, &bc->live);
158 mutex_unlock(&bc->lock);
164 static inline struct btree *btree_cache_find(struct btree_cache *bc,
165 const struct bkey_i *k)
167 u64 v = btree_ptr_hash_val(k);
169 return rhashtable_lookup_fast(&bc->table, &v, bch_btree_cache_params);
173 * this version is for btree nodes that have already been freed (we're not
174 * reaping a real btree node)
176 static int __btree_node_reclaim(struct bch_fs *c, struct btree *b, bool flush)
178 struct btree_cache *bc = &c->btree_cache;
181 lockdep_assert_held(&bc->lock);
183 if (!six_trylock_intent(&b->c.lock))
186 if (!six_trylock_write(&b->c.lock))
187 goto out_unlock_intent;
189 if (btree_node_noevict(b))
192 if (!btree_node_may_write(b))
195 if (btree_node_dirty(b) &&
196 test_bit(BCH_FS_HOLD_BTREE_WRITES, &c->flags))
199 if (btree_node_dirty(b) ||
200 btree_node_write_in_flight(b) ||
201 btree_node_read_in_flight(b)) {
205 wait_on_bit_io(&b->flags, BTREE_NODE_read_in_flight,
206 TASK_UNINTERRUPTIBLE);
209 * Using the underscore version because we don't want to compact
210 * bsets after the write, since this node is about to be evicted
211 * - unless btree verify mode is enabled, since it runs out of
212 * the post write cleanup:
214 if (bch2_verify_btree_ondisk)
215 bch2_btree_node_write(c, b, SIX_LOCK_intent);
217 __bch2_btree_node_write(c, b, SIX_LOCK_read);
219 /* wait for any in flight btree write */
220 btree_node_wait_on_io(b);
223 if (b->hash_val && !ret)
224 trace_btree_node_reap(c, b);
227 six_unlock_write(&b->c.lock);
229 six_unlock_intent(&b->c.lock);
234 static int btree_node_reclaim(struct bch_fs *c, struct btree *b)
236 return __btree_node_reclaim(c, b, false);
239 static int btree_node_write_and_reclaim(struct bch_fs *c, struct btree *b)
241 return __btree_node_reclaim(c, b, true);
244 static unsigned long bch2_btree_cache_scan(struct shrinker *shrink,
245 struct shrink_control *sc)
247 struct bch_fs *c = container_of(shrink, struct bch_fs,
249 struct btree_cache *bc = &c->btree_cache;
251 unsigned long nr = sc->nr_to_scan;
252 unsigned long can_free;
253 unsigned long touched = 0;
254 unsigned long freed = 0;
257 if (bch2_btree_shrinker_disabled)
260 /* Return -1 if we can't do anything right now */
261 if (sc->gfp_mask & __GFP_FS)
262 mutex_lock(&bc->lock);
263 else if (!mutex_trylock(&bc->lock))
266 flags = memalloc_nofs_save();
269 * It's _really_ critical that we don't free too many btree nodes - we
270 * have to always leave ourselves a reserve. The reserve is how we
271 * guarantee that allocating memory for a new btree node can always
272 * succeed, so that inserting keys into the btree can always succeed and
273 * IO can always make forward progress:
275 nr /= btree_pages(c);
276 can_free = btree_cache_can_free(bc);
277 nr = min_t(unsigned long, nr, can_free);
280 list_for_each_entry_safe(b, t, &bc->freeable, list) {
287 !btree_node_reclaim(c, b)) {
288 btree_node_data_free(c, b);
289 six_unlock_write(&b->c.lock);
290 six_unlock_intent(&b->c.lock);
295 list_for_each_entry_safe(b, t, &bc->live, list) {
300 if (&t->list != &bc->live)
301 list_move_tail(&bc->live, &t->list);
305 if (!btree_node_accessed(b) &&
306 !btree_node_reclaim(c, b)) {
307 /* can't call bch2_btree_node_hash_remove under lock */
309 if (&t->list != &bc->live)
310 list_move_tail(&bc->live, &t->list);
312 btree_node_data_free(c, b);
313 mutex_unlock(&bc->lock);
315 bch2_btree_node_hash_remove(bc, b);
316 six_unlock_write(&b->c.lock);
317 six_unlock_intent(&b->c.lock);
322 if (sc->gfp_mask & __GFP_FS)
323 mutex_lock(&bc->lock);
324 else if (!mutex_trylock(&bc->lock))
328 clear_btree_node_accessed(b);
331 mutex_unlock(&bc->lock);
333 memalloc_nofs_restore(flags);
334 return (unsigned long) freed * btree_pages(c);
337 static unsigned long bch2_btree_cache_count(struct shrinker *shrink,
338 struct shrink_control *sc)
340 struct bch_fs *c = container_of(shrink, struct bch_fs,
342 struct btree_cache *bc = &c->btree_cache;
344 if (bch2_btree_shrinker_disabled)
347 return btree_cache_can_free(bc) * btree_pages(c);
350 void bch2_fs_btree_cache_exit(struct bch_fs *c)
352 struct btree_cache *bc = &c->btree_cache;
356 if (bc->shrink.list.next)
357 unregister_shrinker(&bc->shrink);
359 /* vfree() can allocate memory: */
360 flags = memalloc_nofs_save();
361 mutex_lock(&bc->lock);
363 #ifdef CONFIG_BCACHEFS_DEBUG
365 list_move(&c->verify_data->list, &bc->live);
367 kvpfree(c->verify_ondisk, btree_bytes(c));
370 for (i = 0; i < BTREE_ID_NR; i++)
371 if (c->btree_roots[i].b)
372 list_add(&c->btree_roots[i].b->list, &bc->live);
374 list_splice(&bc->freeable, &bc->live);
376 while (!list_empty(&bc->live)) {
377 b = list_first_entry(&bc->live, struct btree, list);
379 BUG_ON(btree_node_read_in_flight(b) ||
380 btree_node_write_in_flight(b));
382 if (btree_node_dirty(b))
383 bch2_btree_complete_write(c, b, btree_current_write(b));
384 clear_btree_node_dirty(c, b);
386 btree_node_data_free(c, b);
389 BUG_ON(atomic_read(&c->btree_cache.dirty));
391 while (!list_empty(&bc->freed)) {
392 b = list_first_entry(&bc->freed, struct btree, list);
394 six_lock_pcpu_free(&b->c.lock);
398 mutex_unlock(&bc->lock);
399 memalloc_nofs_restore(flags);
401 if (bc->table_init_done)
402 rhashtable_destroy(&bc->table);
405 int bch2_fs_btree_cache_init(struct bch_fs *c)
407 struct btree_cache *bc = &c->btree_cache;
411 pr_verbose_init(c->opts, "");
413 ret = rhashtable_init(&bc->table, &bch_btree_cache_params);
417 bc->table_init_done = true;
419 bch2_recalc_btree_reserve(c);
421 for (i = 0; i < bc->reserve; i++)
422 if (!btree_node_mem_alloc(c)) {
427 list_splice_init(&bc->live, &bc->freeable);
429 #ifdef CONFIG_BCACHEFS_DEBUG
430 mutex_init(&c->verify_lock);
432 c->verify_ondisk = kvpmalloc(btree_bytes(c), GFP_KERNEL);
433 if (!c->verify_ondisk) {
438 c->verify_data = btree_node_mem_alloc(c);
439 if (!c->verify_data) {
444 list_del_init(&c->verify_data->list);
447 bc->shrink.count_objects = bch2_btree_cache_count;
448 bc->shrink.scan_objects = bch2_btree_cache_scan;
449 bc->shrink.seeks = 4;
450 bc->shrink.batch = btree_pages(c) * 2;
451 ret = register_shrinker(&bc->shrink);
453 pr_verbose_init(c->opts, "ret %i", ret);
457 void bch2_fs_btree_cache_init_early(struct btree_cache *bc)
459 mutex_init(&bc->lock);
460 INIT_LIST_HEAD(&bc->live);
461 INIT_LIST_HEAD(&bc->freeable);
462 INIT_LIST_HEAD(&bc->freed);
466 * We can only have one thread cannibalizing other cached btree nodes at a time,
467 * or we'll deadlock. We use an open coded mutex to ensure that, which a
468 * cannibalize_bucket() will take. This means every time we unlock the root of
469 * the btree, we need to release this lock if we have it held.
471 void bch2_btree_cache_cannibalize_unlock(struct bch_fs *c)
473 struct btree_cache *bc = &c->btree_cache;
475 if (bc->alloc_lock == current) {
476 trace_btree_node_cannibalize_unlock(c);
477 bc->alloc_lock = NULL;
478 closure_wake_up(&bc->alloc_wait);
482 int bch2_btree_cache_cannibalize_lock(struct bch_fs *c, struct closure *cl)
484 struct btree_cache *bc = &c->btree_cache;
485 struct task_struct *old;
487 old = cmpxchg(&bc->alloc_lock, NULL, current);
488 if (old == NULL || old == current)
492 trace_btree_node_cannibalize_lock_fail(c);
496 closure_wait(&bc->alloc_wait, cl);
498 /* Try again, after adding ourselves to waitlist */
499 old = cmpxchg(&bc->alloc_lock, NULL, current);
500 if (old == NULL || old == current) {
502 closure_wake_up(&bc->alloc_wait);
506 trace_btree_node_cannibalize_lock_fail(c);
510 trace_btree_node_cannibalize_lock(c);
514 static struct btree *btree_node_cannibalize(struct bch_fs *c)
516 struct btree_cache *bc = &c->btree_cache;
519 list_for_each_entry_reverse(b, &bc->live, list)
520 if (!btree_node_reclaim(c, b))
524 list_for_each_entry_reverse(b, &bc->live, list)
525 if (!btree_node_write_and_reclaim(c, b))
529 * Rare case: all nodes were intent-locked.
532 WARN_ONCE(1, "btree cache cannibalize failed\n");
537 struct btree *bch2_btree_node_mem_alloc(struct bch_fs *c)
539 struct btree_cache *bc = &c->btree_cache;
541 u64 start_time = local_clock();
544 flags = memalloc_nofs_save();
545 mutex_lock(&bc->lock);
548 * btree_free() doesn't free memory; it sticks the node on the end of
549 * the list. Check if there's any freed nodes there:
551 list_for_each_entry(b, &bc->freeable, list)
552 if (!btree_node_reclaim(c, b))
556 * We never free struct btree itself, just the memory that holds the on
557 * disk node. Check the freed list before allocating a new one:
559 list_for_each_entry(b, &bc->freed, list)
560 if (!btree_node_reclaim(c, b))
566 list_del_init(&b->list);
567 mutex_unlock(&bc->lock);
570 b = __btree_node_mem_alloc(c);
574 BUG_ON(!six_trylock_intent(&b->c.lock));
575 BUG_ON(!six_trylock_write(&b->c.lock));
579 if (btree_node_data_alloc(c, b, __GFP_NOWARN|GFP_KERNEL))
582 mutex_lock(&bc->lock);
584 mutex_unlock(&bc->lock);
587 BUG_ON(btree_node_hashed(b));
588 BUG_ON(btree_node_write_in_flight(b));
595 b->whiteout_u64s = 0;
596 bch2_btree_keys_init(b);
598 bch2_time_stats_update(&c->times[BCH_TIME_btree_node_mem_alloc],
601 memalloc_nofs_restore(flags);
604 mutex_lock(&bc->lock);
607 list_add(&b->list, &bc->freed);
608 six_unlock_write(&b->c.lock);
609 six_unlock_intent(&b->c.lock);
612 /* Try to cannibalize another cached btree node: */
613 if (bc->alloc_lock == current) {
614 b = btree_node_cannibalize(c);
615 list_del_init(&b->list);
616 mutex_unlock(&bc->lock);
618 bch2_btree_node_hash_remove(bc, b);
620 trace_btree_node_cannibalize(c);
624 mutex_unlock(&bc->lock);
625 memalloc_nofs_restore(flags);
626 return ERR_PTR(-ENOMEM);
629 /* Slowpath, don't want it inlined into btree_iter_traverse() */
630 static noinline struct btree *bch2_btree_node_fill(struct bch_fs *c,
631 struct btree_iter *iter,
632 const struct bkey_i *k,
633 enum btree_id btree_id,
635 enum six_lock_type lock_type,
638 struct btree_cache *bc = &c->btree_cache;
641 BUG_ON(level + 1 >= BTREE_MAX_DEPTH);
643 * Parent node must be locked, else we could read in a btree node that's
646 if (iter && !bch2_btree_node_relock(iter, level + 1))
647 return ERR_PTR(-EINTR);
649 b = bch2_btree_node_mem_alloc(c);
653 bkey_copy(&b->key, k);
654 if (bch2_btree_node_hash_insert(bc, b, level, btree_id)) {
655 /* raced with another fill: */
657 /* mark as unhashed... */
660 mutex_lock(&bc->lock);
661 list_add(&b->list, &bc->freeable);
662 mutex_unlock(&bc->lock);
664 six_unlock_write(&b->c.lock);
665 six_unlock_intent(&b->c.lock);
670 * Unlock before doing IO:
672 * XXX: ideally should be dropping all btree node locks here
674 if (iter && btree_node_read_locked(iter, level + 1))
675 btree_node_unlock(iter, level + 1);
677 bch2_btree_node_read(c, b, sync);
679 six_unlock_write(&b->c.lock);
682 six_unlock_intent(&b->c.lock);
686 if (lock_type == SIX_LOCK_read)
687 six_lock_downgrade(&b->c.lock);
692 static int lock_node_check_fn(struct six_lock *lock, void *p)
694 struct btree *b = container_of(lock, struct btree, c.lock);
695 const struct bkey_i *k = p;
697 return b->hash_val == btree_ptr_hash_val(k) ? 0 : -1;
701 * bch_btree_node_get - find a btree node in the cache and lock it, reading it
702 * in from disk if necessary.
704 * If IO is necessary and running under generic_make_request, returns -EAGAIN.
706 * The btree node will have either a read or a write lock held, depending on
707 * the @write parameter.
709 struct btree *bch2_btree_node_get(struct bch_fs *c, struct btree_iter *iter,
710 const struct bkey_i *k, unsigned level,
711 enum six_lock_type lock_type,
712 unsigned long trace_ip)
714 struct btree_cache *bc = &c->btree_cache;
718 EBUG_ON(level >= BTREE_MAX_DEPTH);
720 b = btree_node_mem_ptr(k);
724 b = btree_cache_find(bc, k);
727 * We must have the parent locked to call bch2_btree_node_fill(),
728 * else we could read in a btree node from disk that's been
731 b = bch2_btree_node_fill(c, iter, k, iter->btree_id,
732 level, lock_type, true);
734 /* We raced and found the btree node in the cache */
743 * There's a potential deadlock with splits and insertions into
744 * interior nodes we have to avoid:
746 * The other thread might be holding an intent lock on the node
747 * we want, and they want to update its parent node so they're
748 * going to upgrade their intent lock on the parent node to a
751 * But if we're holding a read lock on the parent, and we're
752 * trying to get the intent lock they're holding, we deadlock.
754 * So to avoid this we drop the read locks on parent nodes when
755 * we're starting to take intent locks - and handle the race.
757 * The race is that they might be about to free the node we
758 * want, and dropping our read lock on the parent node lets them
759 * update the parent marking the node we want as freed, and then
762 * To guard against this, btree nodes are evicted from the cache
763 * when they're freed - and b->hash_val is zeroed out, which we
764 * check for after we lock the node.
766 * Then, bch2_btree_node_relock() on the parent will fail - because
767 * the parent was modified, when the pointer to the node we want
768 * was removed - and we'll bail out:
770 if (btree_node_read_locked(iter, level + 1))
771 btree_node_unlock(iter, level + 1);
773 if (!btree_node_lock(b, k->k.p, level, iter, lock_type,
774 lock_node_check_fn, (void *) k, trace_ip)) {
775 if (b->hash_val != btree_ptr_hash_val(k))
777 return ERR_PTR(-EINTR);
780 if (unlikely(b->hash_val != btree_ptr_hash_val(k) ||
781 b->c.level != level ||
783 six_unlock_type(&b->c.lock, lock_type);
784 if (bch2_btree_node_relock(iter, level + 1))
787 trace_trans_restart_btree_node_reused(iter->trans->ip);
788 return ERR_PTR(-EINTR);
792 /* XXX: waiting on IO with btree locks held: */
793 wait_on_bit_io(&b->flags, BTREE_NODE_read_in_flight,
794 TASK_UNINTERRUPTIBLE);
796 prefetch(b->aux_data);
798 for_each_bset(b, t) {
799 void *p = (u64 *) b->aux_data + t->aux_data_offset;
801 prefetch(p + L1_CACHE_BYTES * 0);
802 prefetch(p + L1_CACHE_BYTES * 1);
803 prefetch(p + L1_CACHE_BYTES * 2);
806 /* avoid atomic set bit if it's not needed: */
807 if (!btree_node_accessed(b))
808 set_btree_node_accessed(b);
810 if (unlikely(btree_node_read_error(b))) {
811 six_unlock_type(&b->c.lock, lock_type);
812 return ERR_PTR(-EIO);
815 EBUG_ON(b->c.btree_id != iter->btree_id);
816 EBUG_ON(BTREE_NODE_LEVEL(b->data) != level);
817 EBUG_ON(bpos_cmp(b->data->max_key, k->k.p));
818 EBUG_ON(b->key.k.type == KEY_TYPE_btree_ptr_v2 &&
819 bpos_cmp(b->data->min_key,
820 bkey_i_to_btree_ptr_v2(&b->key)->v.min_key));
825 struct btree *bch2_btree_node_get_noiter(struct bch_fs *c,
826 const struct bkey_i *k,
827 enum btree_id btree_id,
831 struct btree_cache *bc = &c->btree_cache;
836 EBUG_ON(level >= BTREE_MAX_DEPTH);
838 b = btree_node_mem_ptr(k);
842 b = btree_cache_find(bc, k);
847 b = bch2_btree_node_fill(c, NULL, k, btree_id,
848 level, SIX_LOCK_read, true);
850 /* We raced and found the btree node in the cache */
855 !bch2_btree_cache_cannibalize_lock(c, NULL))
862 ret = six_lock_read(&b->c.lock, lock_node_check_fn, (void *) k);
866 if (unlikely(b->hash_val != btree_ptr_hash_val(k) ||
867 b->c.btree_id != btree_id ||
868 b->c.level != level)) {
869 six_unlock_read(&b->c.lock);
874 /* XXX: waiting on IO with btree locks held: */
875 wait_on_bit_io(&b->flags, BTREE_NODE_read_in_flight,
876 TASK_UNINTERRUPTIBLE);
878 prefetch(b->aux_data);
880 for_each_bset(b, t) {
881 void *p = (u64 *) b->aux_data + t->aux_data_offset;
883 prefetch(p + L1_CACHE_BYTES * 0);
884 prefetch(p + L1_CACHE_BYTES * 1);
885 prefetch(p + L1_CACHE_BYTES * 2);
888 /* avoid atomic set bit if it's not needed: */
889 if (!btree_node_accessed(b))
890 set_btree_node_accessed(b);
892 if (unlikely(btree_node_read_error(b))) {
893 six_unlock_read(&b->c.lock);
898 EBUG_ON(b->c.btree_id != btree_id);
899 EBUG_ON(BTREE_NODE_LEVEL(b->data) != level);
900 EBUG_ON(bpos_cmp(b->data->max_key, k->k.p));
901 EBUG_ON(b->key.k.type == KEY_TYPE_btree_ptr_v2 &&
902 bpos_cmp(b->data->min_key,
903 bkey_i_to_btree_ptr_v2(&b->key)->v.min_key));
905 bch2_btree_cache_cannibalize_unlock(c);
909 struct btree *bch2_btree_node_get_sibling(struct bch_fs *c,
910 struct btree_iter *iter,
912 enum btree_node_sibling sib)
914 struct btree_trans *trans = iter->trans;
915 struct btree *parent;
916 struct btree_node_iter node_iter;
917 struct bkey_packed *k;
919 struct btree *ret = NULL;
920 unsigned level = b->c.level;
922 bch2_bkey_buf_init(&tmp);
924 parent = btree_iter_node(iter, level + 1);
929 * There's a corner case where a btree_iter might have a node locked
930 * that is just outside its current pos - when
931 * bch2_btree_iter_set_pos_same_leaf() gets to the end of the node.
933 * But the lock ordering checks in __bch2_btree_node_lock() go off of
934 * iter->pos, not the node's key: so if the iterator is marked as
935 * needing to be traversed, we risk deadlock if we don't bail out here:
937 if (iter->uptodate >= BTREE_ITER_NEED_TRAVERSE)
938 return ERR_PTR(-EINTR);
940 if (!bch2_btree_node_relock(iter, level + 1)) {
941 ret = ERR_PTR(-EINTR);
945 node_iter = iter->l[parent->c.level].iter;
947 k = bch2_btree_node_iter_peek_all(&node_iter, parent);
948 BUG_ON(bkey_cmp_left_packed(parent, k, &b->key.k.p));
950 k = sib == btree_prev_sib
951 ? bch2_btree_node_iter_prev(&node_iter, parent)
952 : (bch2_btree_node_iter_advance(&node_iter, parent),
953 bch2_btree_node_iter_peek(&node_iter, parent));
957 bch2_bkey_buf_unpack(&tmp, c, parent, k);
959 ret = bch2_btree_node_get(c, iter, tmp.k, level,
960 SIX_LOCK_intent, _THIS_IP_);
962 if (PTR_ERR_OR_ZERO(ret) == -EINTR && !trans->nounlock) {
963 struct btree_iter *linked;
965 if (!bch2_btree_node_relock(iter, level + 1))
969 * We might have got -EINTR because trylock failed, and we're
970 * holding other locks that would cause us to deadlock:
972 trans_for_each_iter(trans, linked)
973 if (btree_iter_lock_cmp(iter, linked) < 0)
974 __bch2_btree_iter_unlock(linked);
976 if (sib == btree_prev_sib)
977 btree_node_unlock(iter, level);
979 ret = bch2_btree_node_get(c, iter, tmp.k, level,
980 SIX_LOCK_intent, _THIS_IP_);
983 * before btree_iter_relock() calls btree_iter_verify_locks():
985 if (btree_lock_want(iter, level + 1) == BTREE_NODE_UNLOCKED)
986 btree_node_unlock(iter, level + 1);
988 if (!bch2_btree_node_relock(iter, level)) {
989 btree_iter_set_dirty(iter, BTREE_ITER_NEED_RELOCK);
992 six_unlock_intent(&ret->c.lock);
993 ret = ERR_PTR(-EINTR);
997 bch2_trans_relock(trans);
1000 if (btree_lock_want(iter, level + 1) == BTREE_NODE_UNLOCKED)
1001 btree_node_unlock(iter, level + 1);
1003 if (PTR_ERR_OR_ZERO(ret) == -EINTR)
1004 bch2_btree_iter_upgrade(iter, level + 2);
1006 BUG_ON(!IS_ERR(ret) && !btree_node_locked(iter, level));
1008 if (!IS_ERR_OR_NULL(ret)) {
1009 struct btree *n1 = ret, *n2 = b;
1011 if (sib != btree_prev_sib)
1014 if (bpos_cmp(bpos_successor(n1->key.k.p),
1015 n2->data->min_key)) {
1016 char buf1[200], buf2[200];
1018 bch2_bkey_val_to_text(&PBUF(buf1), c, bkey_i_to_s_c(&n1->key));
1019 bch2_bkey_val_to_text(&PBUF(buf2), c, bkey_i_to_s_c(&n2->key));
1021 bch2_fs_inconsistent(c, "btree topology error at btree %s level %u:\n"
1024 bch2_btree_ids[iter->btree_id], level,
1027 six_unlock_intent(&ret->c.lock);
1032 bch2_btree_trans_verify_locks(trans);
1034 bch2_bkey_buf_exit(&tmp, c);
1039 void bch2_btree_node_prefetch(struct bch_fs *c, struct btree_iter *iter,
1040 const struct bkey_i *k,
1041 enum btree_id btree_id, unsigned level)
1043 struct btree_cache *bc = &c->btree_cache;
1046 BUG_ON(iter && !btree_node_locked(iter, level + 1));
1047 BUG_ON(level >= BTREE_MAX_DEPTH);
1049 b = btree_cache_find(bc, k);
1053 bch2_btree_node_fill(c, iter, k, btree_id, level, SIX_LOCK_read, false);
1056 void bch2_btree_node_to_text(struct printbuf *out, struct bch_fs *c,
1059 const struct bkey_format *f = &b->format;
1060 struct bset_stats stats;
1062 memset(&stats, 0, sizeof(stats));
1064 bch2_btree_keys_stats(b, &stats);
1066 pr_buf(out, "l %u ", b->c.level);
1067 bch2_bpos_to_text(out, b->data->min_key);
1069 bch2_bpos_to_text(out, b->data->max_key);
1072 bch2_val_to_text(out, c, bkey_i_to_s_c(&b->key));
1075 " format: u64s %u fields %u %u %u %u %u\n"
1076 " unpack fn len: %u\n"
1077 " bytes used %zu/%zu (%zu%% full)\n"
1078 " sib u64s: %u, %u (merge threshold %zu)\n"
1079 " nr packed keys %u\n"
1080 " nr unpacked keys %u\n"
1082 " failed unpacked %zu\n",
1084 f->bits_per_field[0],
1085 f->bits_per_field[1],
1086 f->bits_per_field[2],
1087 f->bits_per_field[3],
1088 f->bits_per_field[4],
1090 b->nr.live_u64s * sizeof(u64),
1091 btree_bytes(c) - sizeof(struct btree_node),
1092 b->nr.live_u64s * 100 / btree_max_u64s(c),
1095 BTREE_FOREGROUND_MERGE_THRESHOLD(c),
1097 b->nr.unpacked_keys,
1102 void bch2_btree_cache_to_text(struct printbuf *out, struct bch_fs *c)
1104 pr_buf(out, "nr nodes:\t\t%u\n", c->btree_cache.used);
1105 pr_buf(out, "nr dirty:\t\t%u\n", atomic_read(&c->btree_cache.dirty));
1106 pr_buf(out, "cannibalize lock:\t%p\n", c->btree_cache.alloc_lock);