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 struct lock_class_key bch2_btree_node_lock_key;
18 void bch2_recalc_btree_reserve(struct bch_fs *c)
20 unsigned i, reserve = 16;
22 if (!c->btree_roots[0].b)
25 for (i = 0; i < BTREE_ID_NR; i++)
26 if (c->btree_roots[i].b)
27 reserve += min_t(unsigned, 1,
28 c->btree_roots[i].b->c.level) * 8;
30 c->btree_cache.reserve = reserve;
33 static inline unsigned btree_cache_can_free(struct btree_cache *bc)
35 return max_t(int, 0, bc->used - bc->reserve);
38 static void btree_node_data_free(struct bch_fs *c, struct btree *b)
40 struct btree_cache *bc = &c->btree_cache;
42 EBUG_ON(btree_node_write_in_flight(b));
44 kvpfree(b->data, btree_bytes(c));
49 munmap(b->aux_data, btree_aux_data_bytes(b));
54 list_move(&b->list, &bc->freed);
57 static int bch2_btree_cache_cmp_fn(struct rhashtable_compare_arg *arg,
60 const struct btree *b = obj;
61 const u64 *v = arg->key;
63 return b->hash_val == *v ? 0 : 1;
66 static const struct rhashtable_params bch_btree_cache_params = {
67 .head_offset = offsetof(struct btree, hash),
68 .key_offset = offsetof(struct btree, hash_val),
69 .key_len = sizeof(u64),
70 .obj_cmpfn = bch2_btree_cache_cmp_fn,
73 static int btree_node_data_alloc(struct bch_fs *c, struct btree *b, gfp_t gfp)
75 BUG_ON(b->data || b->aux_data);
77 b->data = kvpmalloc(btree_bytes(c), gfp);
81 b->aux_data = vmalloc_exec(btree_aux_data_bytes(b), gfp);
83 b->aux_data = mmap(NULL, btree_aux_data_bytes(b),
84 PROT_READ|PROT_WRITE|PROT_EXEC,
85 MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
88 kvpfree(b->data, btree_bytes(c));
96 static struct btree *__btree_node_mem_alloc(struct bch_fs *c)
98 struct btree *b = kzalloc(sizeof(struct btree), GFP_KERNEL);
102 bkey_btree_ptr_init(&b->key);
103 __six_lock_init(&b->c.lock, "b->c.lock", &bch2_btree_node_lock_key);
104 INIT_LIST_HEAD(&b->list);
105 INIT_LIST_HEAD(&b->write_blocked);
106 b->byte_order = ilog2(btree_bytes(c));
110 struct btree *__bch2_btree_node_mem_alloc(struct bch_fs *c)
112 struct btree_cache *bc = &c->btree_cache;
113 struct btree *b = __btree_node_mem_alloc(c);
117 if (btree_node_data_alloc(c, b, GFP_KERNEL)) {
123 list_add(&b->list, &bc->freeable);
127 /* Btree in memory cache - hash table */
129 void bch2_btree_node_hash_remove(struct btree_cache *bc, struct btree *b)
131 rhashtable_remove_fast(&bc->table, &b->hash, bch_btree_cache_params);
133 /* Cause future lookups for this node to fail: */
136 six_lock_wakeup_all(&b->c.lock);
139 int __bch2_btree_node_hash_insert(struct btree_cache *bc, struct btree *b)
142 b->hash_val = btree_ptr_hash_val(&b->key);
144 return rhashtable_lookup_insert_fast(&bc->table, &b->hash,
145 bch_btree_cache_params);
148 int bch2_btree_node_hash_insert(struct btree_cache *bc, struct btree *b,
149 unsigned level, enum btree_id id)
157 six_lock_pcpu_alloc(&b->c.lock);
159 six_lock_pcpu_free_rcu(&b->c.lock);
161 mutex_lock(&bc->lock);
162 ret = __bch2_btree_node_hash_insert(bc, b);
164 list_add(&b->list, &bc->live);
165 mutex_unlock(&bc->lock);
171 static inline struct btree *btree_cache_find(struct btree_cache *bc,
172 const struct bkey_i *k)
174 u64 v = btree_ptr_hash_val(k);
176 return rhashtable_lookup_fast(&bc->table, &v, bch_btree_cache_params);
180 * this version is for btree nodes that have already been freed (we're not
181 * reaping a real btree node)
183 static int __btree_node_reclaim(struct bch_fs *c, struct btree *b, bool flush)
185 struct btree_cache *bc = &c->btree_cache;
188 lockdep_assert_held(&bc->lock);
190 if (b->flags & ((1U << BTREE_NODE_dirty)|
191 (1U << BTREE_NODE_read_in_flight)|
192 (1U << BTREE_NODE_write_in_flight))) {
196 /* XXX: waiting on IO with btree cache lock held */
197 bch2_btree_node_wait_on_read(b);
198 bch2_btree_node_wait_on_write(b);
201 if (!six_trylock_intent(&b->c.lock))
204 if (!six_trylock_write(&b->c.lock))
205 goto out_unlock_intent;
207 /* recheck under lock */
208 if (b->flags & ((1U << BTREE_NODE_read_in_flight)|
209 (1U << BTREE_NODE_write_in_flight))) {
212 six_unlock_write(&b->c.lock);
213 six_unlock_intent(&b->c.lock);
217 if (btree_node_noevict(b))
220 if (!btree_node_may_write(b))
223 if (btree_node_dirty(b)) {
225 test_bit(BCH_FS_HOLD_BTREE_WRITES, &c->flags))
228 * Using the underscore version because we don't want to compact
229 * bsets after the write, since this node is about to be evicted
230 * - unless btree verify mode is enabled, since it runs out of
231 * the post write cleanup:
233 if (bch2_verify_btree_ondisk)
234 bch2_btree_node_write(c, b, SIX_LOCK_intent);
236 __bch2_btree_node_write(c, b, false);
238 six_unlock_write(&b->c.lock);
239 six_unlock_intent(&b->c.lock);
243 if (b->hash_val && !ret)
244 trace_btree_node_reap(c, b);
247 six_unlock_write(&b->c.lock);
249 six_unlock_intent(&b->c.lock);
254 static int btree_node_reclaim(struct bch_fs *c, struct btree *b)
256 return __btree_node_reclaim(c, b, false);
259 static int btree_node_write_and_reclaim(struct bch_fs *c, struct btree *b)
261 return __btree_node_reclaim(c, b, true);
264 static unsigned long bch2_btree_cache_scan(struct shrinker *shrink,
265 struct shrink_control *sc)
267 struct bch_fs *c = container_of(shrink, struct bch_fs,
269 struct btree_cache *bc = &c->btree_cache;
271 unsigned long nr = sc->nr_to_scan;
272 unsigned long can_free;
273 unsigned long touched = 0;
274 unsigned long freed = 0;
277 if (bch2_btree_shrinker_disabled)
280 /* Return -1 if we can't do anything right now */
281 if (sc->gfp_mask & __GFP_FS)
282 mutex_lock(&bc->lock);
283 else if (!mutex_trylock(&bc->lock))
286 flags = memalloc_nofs_save();
289 * It's _really_ critical that we don't free too many btree nodes - we
290 * have to always leave ourselves a reserve. The reserve is how we
291 * guarantee that allocating memory for a new btree node can always
292 * succeed, so that inserting keys into the btree can always succeed and
293 * IO can always make forward progress:
295 nr /= btree_pages(c);
296 can_free = btree_cache_can_free(bc);
297 nr = min_t(unsigned long, nr, can_free);
300 list_for_each_entry_safe(b, t, &bc->freeable, list) {
307 !btree_node_reclaim(c, b)) {
308 btree_node_data_free(c, b);
309 six_unlock_write(&b->c.lock);
310 six_unlock_intent(&b->c.lock);
315 list_for_each_entry_safe(b, t, &bc->live, list) {
320 if (&t->list != &bc->live)
321 list_move_tail(&bc->live, &t->list);
325 if (!btree_node_accessed(b) &&
326 !btree_node_reclaim(c, b)) {
327 /* can't call bch2_btree_node_hash_remove under lock */
329 if (&t->list != &bc->live)
330 list_move_tail(&bc->live, &t->list);
332 btree_node_data_free(c, b);
333 mutex_unlock(&bc->lock);
335 bch2_btree_node_hash_remove(bc, b);
336 six_unlock_write(&b->c.lock);
337 six_unlock_intent(&b->c.lock);
342 if (sc->gfp_mask & __GFP_FS)
343 mutex_lock(&bc->lock);
344 else if (!mutex_trylock(&bc->lock))
348 clear_btree_node_accessed(b);
351 mutex_unlock(&bc->lock);
353 memalloc_nofs_restore(flags);
354 return (unsigned long) freed * btree_pages(c);
357 static unsigned long bch2_btree_cache_count(struct shrinker *shrink,
358 struct shrink_control *sc)
360 struct bch_fs *c = container_of(shrink, struct bch_fs,
362 struct btree_cache *bc = &c->btree_cache;
364 if (bch2_btree_shrinker_disabled)
367 return btree_cache_can_free(bc) * btree_pages(c);
370 void bch2_fs_btree_cache_exit(struct bch_fs *c)
372 struct btree_cache *bc = &c->btree_cache;
376 if (bc->shrink.list.next)
377 unregister_shrinker(&bc->shrink);
379 /* vfree() can allocate memory: */
380 flags = memalloc_nofs_save();
381 mutex_lock(&bc->lock);
384 list_move(&c->verify_data->list, &bc->live);
386 kvpfree(c->verify_ondisk, btree_bytes(c));
388 for (i = 0; i < BTREE_ID_NR; i++)
389 if (c->btree_roots[i].b)
390 list_add(&c->btree_roots[i].b->list, &bc->live);
392 list_splice(&bc->freeable, &bc->live);
394 while (!list_empty(&bc->live)) {
395 b = list_first_entry(&bc->live, struct btree, list);
397 BUG_ON(btree_node_read_in_flight(b) ||
398 btree_node_write_in_flight(b));
400 if (btree_node_dirty(b))
401 bch2_btree_complete_write(c, b, btree_current_write(b));
402 clear_btree_node_dirty(c, b);
404 btree_node_data_free(c, b);
407 BUG_ON(atomic_read(&c->btree_cache.dirty));
409 while (!list_empty(&bc->freed)) {
410 b = list_first_entry(&bc->freed, struct btree, list);
412 six_lock_pcpu_free(&b->c.lock);
416 mutex_unlock(&bc->lock);
417 memalloc_nofs_restore(flags);
419 if (bc->table_init_done)
420 rhashtable_destroy(&bc->table);
423 int bch2_fs_btree_cache_init(struct bch_fs *c)
425 struct btree_cache *bc = &c->btree_cache;
429 pr_verbose_init(c->opts, "");
431 ret = rhashtable_init(&bc->table, &bch_btree_cache_params);
435 bc->table_init_done = true;
437 bch2_recalc_btree_reserve(c);
439 for (i = 0; i < bc->reserve; i++)
440 if (!__bch2_btree_node_mem_alloc(c)) {
445 list_splice_init(&bc->live, &bc->freeable);
447 mutex_init(&c->verify_lock);
449 bc->shrink.count_objects = bch2_btree_cache_count;
450 bc->shrink.scan_objects = bch2_btree_cache_scan;
451 bc->shrink.seeks = 4;
452 bc->shrink.batch = btree_pages(c) * 2;
453 ret = register_shrinker(&bc->shrink);
455 pr_verbose_init(c->opts, "ret %i", ret);
459 void bch2_fs_btree_cache_init_early(struct btree_cache *bc)
461 mutex_init(&bc->lock);
462 INIT_LIST_HEAD(&bc->live);
463 INIT_LIST_HEAD(&bc->freeable);
464 INIT_LIST_HEAD(&bc->freed);
468 * We can only have one thread cannibalizing other cached btree nodes at a time,
469 * or we'll deadlock. We use an open coded mutex to ensure that, which a
470 * cannibalize_bucket() will take. This means every time we unlock the root of
471 * the btree, we need to release this lock if we have it held.
473 void bch2_btree_cache_cannibalize_unlock(struct bch_fs *c)
475 struct btree_cache *bc = &c->btree_cache;
477 if (bc->alloc_lock == current) {
478 trace_btree_node_cannibalize_unlock(c);
479 bc->alloc_lock = NULL;
480 closure_wake_up(&bc->alloc_wait);
484 int bch2_btree_cache_cannibalize_lock(struct bch_fs *c, struct closure *cl)
486 struct btree_cache *bc = &c->btree_cache;
487 struct task_struct *old;
489 old = cmpxchg(&bc->alloc_lock, NULL, current);
490 if (old == NULL || old == current)
494 trace_btree_node_cannibalize_lock_fail(c);
498 closure_wait(&bc->alloc_wait, cl);
500 /* Try again, after adding ourselves to waitlist */
501 old = cmpxchg(&bc->alloc_lock, NULL, current);
502 if (old == NULL || old == current) {
504 closure_wake_up(&bc->alloc_wait);
508 trace_btree_node_cannibalize_lock_fail(c);
512 trace_btree_node_cannibalize_lock(c);
516 static struct btree *btree_node_cannibalize(struct bch_fs *c)
518 struct btree_cache *bc = &c->btree_cache;
521 list_for_each_entry_reverse(b, &bc->live, list)
522 if (!btree_node_reclaim(c, b))
526 list_for_each_entry_reverse(b, &bc->live, list)
527 if (!btree_node_write_and_reclaim(c, b))
531 * Rare case: all nodes were intent-locked.
534 WARN_ONCE(1, "btree cache cannibalize failed\n");
539 struct btree *bch2_btree_node_mem_alloc(struct bch_fs *c)
541 struct btree_cache *bc = &c->btree_cache;
543 u64 start_time = local_clock();
546 flags = memalloc_nofs_save();
547 mutex_lock(&bc->lock);
550 * btree_free() doesn't free memory; it sticks the node on the end of
551 * the list. Check if there's any freed nodes there:
553 list_for_each_entry(b, &bc->freeable, list)
554 if (!btree_node_reclaim(c, b))
558 * We never free struct btree itself, just the memory that holds the on
559 * disk node. Check the freed list before allocating a new one:
561 list_for_each_entry(b, &bc->freed, list)
562 if (!btree_node_reclaim(c, b))
568 list_del_init(&b->list);
569 mutex_unlock(&bc->lock);
572 b = __btree_node_mem_alloc(c);
576 BUG_ON(!six_trylock_intent(&b->c.lock));
577 BUG_ON(!six_trylock_write(&b->c.lock));
581 if (btree_node_data_alloc(c, b, __GFP_NOWARN|GFP_KERNEL))
584 mutex_lock(&bc->lock);
586 mutex_unlock(&bc->lock);
589 BUG_ON(btree_node_hashed(b));
590 BUG_ON(btree_node_dirty(b));
591 BUG_ON(btree_node_write_in_flight(b));
598 b->whiteout_u64s = 0;
599 bch2_btree_keys_init(b);
600 set_btree_node_accessed(b);
602 bch2_time_stats_update(&c->times[BCH_TIME_btree_node_mem_alloc],
605 memalloc_nofs_restore(flags);
608 mutex_lock(&bc->lock);
611 list_add(&b->list, &bc->freed);
612 six_unlock_write(&b->c.lock);
613 six_unlock_intent(&b->c.lock);
616 /* Try to cannibalize another cached btree node: */
617 if (bc->alloc_lock == current) {
618 b = btree_node_cannibalize(c);
619 list_del_init(&b->list);
620 mutex_unlock(&bc->lock);
622 bch2_btree_node_hash_remove(bc, b);
624 trace_btree_node_cannibalize(c);
628 mutex_unlock(&bc->lock);
629 memalloc_nofs_restore(flags);
630 return ERR_PTR(-ENOMEM);
633 /* Slowpath, don't want it inlined into btree_iter_traverse() */
634 static noinline struct btree *bch2_btree_node_fill(struct bch_fs *c,
635 struct btree_iter *iter,
636 const struct bkey_i *k,
637 enum btree_id btree_id,
639 enum six_lock_type lock_type,
642 struct btree_cache *bc = &c->btree_cache;
646 BUG_ON(level + 1 >= BTREE_MAX_DEPTH);
648 * Parent node must be locked, else we could read in a btree node that's
651 if (iter && !bch2_btree_node_relock(iter, level + 1))
652 return ERR_PTR(-EINTR);
654 b = bch2_btree_node_mem_alloc(c);
658 bkey_copy(&b->key, k);
659 if (bch2_btree_node_hash_insert(bc, b, level, btree_id)) {
660 /* raced with another fill: */
662 /* mark as unhashed... */
665 mutex_lock(&bc->lock);
666 list_add(&b->list, &bc->freeable);
667 mutex_unlock(&bc->lock);
669 six_unlock_write(&b->c.lock);
670 six_unlock_intent(&b->c.lock);
674 set_btree_node_read_in_flight(b);
676 six_unlock_write(&b->c.lock);
677 seq = b->c.lock.state.seq;
678 six_unlock_intent(&b->c.lock);
680 /* Unlock before doing IO: */
682 bch2_trans_unlock(iter->trans);
684 bch2_btree_node_read(c, b, sync);
690 * XXX: this will probably always fail because btree_iter_relock()
691 * currently fails for iterators that aren't pointed at a valid btree
695 (!bch2_trans_relock(iter->trans) ||
696 !bch2_btree_iter_relock(iter, _THIS_IP_)))
697 return ERR_PTR(-EINTR);
699 if (!six_relock_type(&b->c.lock, lock_type, seq))
700 return ERR_PTR(-EINTR);
705 static int lock_node_check_fn(struct six_lock *lock, void *p)
707 struct btree *b = container_of(lock, struct btree, c.lock);
708 const struct bkey_i *k = p;
710 return b->hash_val == btree_ptr_hash_val(k) ? 0 : -1;
713 static noinline void btree_bad_header(struct bch_fs *c, struct btree *b)
715 char buf1[200], buf2[100], buf3[100];
717 if (!test_bit(BCH_FS_INITIAL_GC_DONE, &c->flags))
720 bch2_bkey_val_to_text(&PBUF(buf1), c, bkey_i_to_s_c(&b->key));
721 bch2_bpos_to_text(&PBUF(buf3), b->data->max_key);
723 bch2_fs_inconsistent(c, "btree node header doesn't match ptr\n"
724 "btree %s level %u\n"
726 "header: btree %s level %llu\n"
728 bch2_btree_ids[b->c.btree_id], b->c.level,
730 bch2_btree_ids[BTREE_NODE_ID(b->data)],
731 BTREE_NODE_LEVEL(b->data),
735 static inline void btree_check_header(struct bch_fs *c, struct btree *b)
737 if (b->c.btree_id != BTREE_NODE_ID(b->data) ||
738 b->c.level != BTREE_NODE_LEVEL(b->data) ||
739 bpos_cmp(b->data->max_key, b->key.k.p) ||
740 (b->key.k.type == KEY_TYPE_btree_ptr_v2 &&
741 bpos_cmp(b->data->min_key,
742 bkey_i_to_btree_ptr_v2(&b->key)->v.min_key)))
743 btree_bad_header(c, b);
747 * bch_btree_node_get - find a btree node in the cache and lock it, reading it
748 * in from disk if necessary.
750 * If IO is necessary and running under generic_make_request, returns -EAGAIN.
752 * The btree node will have either a read or a write lock held, depending on
753 * the @write parameter.
755 struct btree *bch2_btree_node_get(struct bch_fs *c, struct btree_iter *iter,
756 const struct bkey_i *k, unsigned level,
757 enum six_lock_type lock_type,
758 unsigned long trace_ip)
760 struct btree_cache *bc = &c->btree_cache;
764 EBUG_ON(level >= BTREE_MAX_DEPTH);
766 b = btree_node_mem_ptr(k);
770 b = btree_cache_find(bc, k);
773 * We must have the parent locked to call bch2_btree_node_fill(),
774 * else we could read in a btree node from disk that's been
777 b = bch2_btree_node_fill(c, iter, k, iter->btree_id,
778 level, lock_type, true);
780 /* We raced and found the btree node in the cache */
789 * There's a potential deadlock with splits and insertions into
790 * interior nodes we have to avoid:
792 * The other thread might be holding an intent lock on the node
793 * we want, and they want to update its parent node so they're
794 * going to upgrade their intent lock on the parent node to a
797 * But if we're holding a read lock on the parent, and we're
798 * trying to get the intent lock they're holding, we deadlock.
800 * So to avoid this we drop the read locks on parent nodes when
801 * we're starting to take intent locks - and handle the race.
803 * The race is that they might be about to free the node we
804 * want, and dropping our read lock on the parent node lets them
805 * update the parent marking the node we want as freed, and then
808 * To guard against this, btree nodes are evicted from the cache
809 * when they're freed - and b->hash_val is zeroed out, which we
810 * check for after we lock the node.
812 * Then, bch2_btree_node_relock() on the parent will fail - because
813 * the parent was modified, when the pointer to the node we want
814 * was removed - and we'll bail out:
816 if (btree_node_read_locked(iter, level + 1))
817 btree_node_unlock(iter, level + 1);
819 if (!btree_node_lock(b, k->k.p, level, iter, lock_type,
820 lock_node_check_fn, (void *) k, trace_ip)) {
821 if (b->hash_val != btree_ptr_hash_val(k))
823 return ERR_PTR(-EINTR);
826 if (unlikely(b->hash_val != btree_ptr_hash_val(k) ||
827 b->c.level != level ||
829 six_unlock_type(&b->c.lock, lock_type);
830 if (bch2_btree_node_relock(iter, level + 1))
833 trace_trans_restart_btree_node_reused(iter->trans->ip,
837 return ERR_PTR(-EINTR);
841 if (unlikely(btree_node_read_in_flight(b))) {
842 u32 seq = b->c.lock.state.seq;
844 six_unlock_type(&b->c.lock, lock_type);
845 bch2_trans_unlock(iter->trans);
847 bch2_btree_node_wait_on_read(b);
850 * XXX: check if this always fails - btree_iter_relock()
851 * currently fails for iterators that aren't pointed at a valid
855 (!bch2_trans_relock(iter->trans) ||
856 !bch2_btree_iter_relock(iter, _THIS_IP_)))
857 return ERR_PTR(-EINTR);
859 if (!six_relock_type(&b->c.lock, lock_type, seq))
863 prefetch(b->aux_data);
865 for_each_bset(b, t) {
866 void *p = (u64 *) b->aux_data + t->aux_data_offset;
868 prefetch(p + L1_CACHE_BYTES * 0);
869 prefetch(p + L1_CACHE_BYTES * 1);
870 prefetch(p + L1_CACHE_BYTES * 2);
873 /* avoid atomic set bit if it's not needed: */
874 if (!btree_node_accessed(b))
875 set_btree_node_accessed(b);
877 if (unlikely(btree_node_read_error(b))) {
878 six_unlock_type(&b->c.lock, lock_type);
879 return ERR_PTR(-EIO);
882 EBUG_ON(b->c.btree_id != iter->btree_id);
883 EBUG_ON(BTREE_NODE_LEVEL(b->data) != level);
884 btree_check_header(c, b);
889 struct btree *bch2_btree_node_get_noiter(struct bch_fs *c,
890 const struct bkey_i *k,
891 enum btree_id btree_id,
895 struct btree_cache *bc = &c->btree_cache;
900 EBUG_ON(level >= BTREE_MAX_DEPTH);
902 b = btree_node_mem_ptr(k);
906 b = btree_cache_find(bc, k);
911 b = bch2_btree_node_fill(c, NULL, k, btree_id,
912 level, SIX_LOCK_read, true);
914 /* We raced and found the btree node in the cache */
919 !bch2_btree_cache_cannibalize_lock(c, NULL))
926 ret = six_lock_read(&b->c.lock, lock_node_check_fn, (void *) k);
930 if (unlikely(b->hash_val != btree_ptr_hash_val(k) ||
931 b->c.btree_id != btree_id ||
932 b->c.level != level)) {
933 six_unlock_read(&b->c.lock);
938 /* XXX: waiting on IO with btree locks held: */
939 __bch2_btree_node_wait_on_read(b);
941 prefetch(b->aux_data);
943 for_each_bset(b, t) {
944 void *p = (u64 *) b->aux_data + t->aux_data_offset;
946 prefetch(p + L1_CACHE_BYTES * 0);
947 prefetch(p + L1_CACHE_BYTES * 1);
948 prefetch(p + L1_CACHE_BYTES * 2);
951 /* avoid atomic set bit if it's not needed: */
952 if (!btree_node_accessed(b))
953 set_btree_node_accessed(b);
955 if (unlikely(btree_node_read_error(b))) {
956 six_unlock_read(&b->c.lock);
961 EBUG_ON(b->c.btree_id != btree_id);
962 EBUG_ON(BTREE_NODE_LEVEL(b->data) != level);
963 btree_check_header(c, b);
965 bch2_btree_cache_cannibalize_unlock(c);
969 void bch2_btree_node_prefetch(struct bch_fs *c, struct btree_iter *iter,
970 const struct bkey_i *k,
971 enum btree_id btree_id, unsigned level)
973 struct btree_cache *bc = &c->btree_cache;
976 BUG_ON(iter && !btree_node_locked(iter, level + 1));
977 BUG_ON(level >= BTREE_MAX_DEPTH);
979 b = btree_cache_find(bc, k);
983 bch2_btree_node_fill(c, iter, k, btree_id, level, SIX_LOCK_read, false);
986 void bch2_btree_node_evict(struct bch_fs *c, const struct bkey_i *k)
988 struct btree_cache *bc = &c->btree_cache;
991 b = btree_cache_find(bc, k);
995 /* not allowed to wait on io with btree locks held: */
997 /* XXX we're called from btree_gc which will be holding other btree
1000 __bch2_btree_node_wait_on_read(b);
1001 __bch2_btree_node_wait_on_write(b);
1003 six_lock_intent(&b->c.lock, NULL, NULL);
1004 six_lock_write(&b->c.lock, NULL, NULL);
1006 if (btree_node_dirty(b)) {
1007 __bch2_btree_node_write(c, b, false);
1008 six_unlock_write(&b->c.lock);
1009 six_unlock_intent(&b->c.lock);
1013 BUG_ON(btree_node_dirty(b));
1015 mutex_lock(&bc->lock);
1016 btree_node_data_free(c, b);
1017 bch2_btree_node_hash_remove(bc, b);
1018 mutex_unlock(&bc->lock);
1020 six_unlock_write(&b->c.lock);
1021 six_unlock_intent(&b->c.lock);
1024 void bch2_btree_node_to_text(struct printbuf *out, struct bch_fs *c,
1027 const struct bkey_format *f = &b->format;
1028 struct bset_stats stats;
1030 memset(&stats, 0, sizeof(stats));
1032 bch2_btree_keys_stats(b, &stats);
1034 pr_buf(out, "l %u ", b->c.level);
1035 bch2_bpos_to_text(out, b->data->min_key);
1037 bch2_bpos_to_text(out, b->data->max_key);
1040 bch2_val_to_text(out, c, bkey_i_to_s_c(&b->key));
1043 " format: u64s %u fields %u %u %u %u %u\n"
1044 " unpack fn len: %u\n"
1045 " bytes used %zu/%zu (%zu%% full)\n"
1046 " sib u64s: %u, %u (merge threshold %u)\n"
1047 " nr packed keys %u\n"
1048 " nr unpacked keys %u\n"
1050 " failed unpacked %zu\n",
1052 f->bits_per_field[0],
1053 f->bits_per_field[1],
1054 f->bits_per_field[2],
1055 f->bits_per_field[3],
1056 f->bits_per_field[4],
1058 b->nr.live_u64s * sizeof(u64),
1059 btree_bytes(c) - sizeof(struct btree_node),
1060 b->nr.live_u64s * 100 / btree_max_u64s(c),
1063 c->btree_foreground_merge_threshold,
1065 b->nr.unpacked_keys,
1070 void bch2_btree_cache_to_text(struct printbuf *out, struct bch_fs *c)
1072 pr_buf(out, "nr nodes:\t\t%u\n", c->btree_cache.used);
1073 pr_buf(out, "nr dirty:\t\t%u\n", atomic_read(&c->btree_cache.dirty));
1074 pr_buf(out, "cannibalize lock:\t%p\n", c->btree_cache.alloc_lock);