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 struct btree_cache *bc = &c->btree_cache;
40 EBUG_ON(btree_node_write_in_flight(b));
42 kvpfree(b->data, btree_bytes(c));
47 munmap(b->aux_data, btree_aux_data_bytes(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 b->aux_data = mmap(NULL, btree_aux_data_bytes(b),
82 PROT_READ|PROT_WRITE|PROT_EXEC,
83 MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
86 kvpfree(b->data, btree_bytes(c));
94 static struct btree *__btree_node_mem_alloc(struct bch_fs *c)
96 struct btree *b = kzalloc(sizeof(struct btree), GFP_KERNEL);
100 bkey_btree_ptr_init(&b->key);
101 six_lock_init(&b->c.lock);
102 INIT_LIST_HEAD(&b->list);
103 INIT_LIST_HEAD(&b->write_blocked);
104 b->byte_order = ilog2(btree_bytes(c));
108 struct btree *__bch2_btree_node_mem_alloc(struct bch_fs *c)
110 struct btree_cache *bc = &c->btree_cache;
111 struct btree *b = __btree_node_mem_alloc(c);
115 if (btree_node_data_alloc(c, b, GFP_KERNEL)) {
121 list_add(&b->list, &bc->freeable);
125 /* Btree in memory cache - hash table */
127 void bch2_btree_node_hash_remove(struct btree_cache *bc, struct btree *b)
129 rhashtable_remove_fast(&bc->table, &b->hash, bch_btree_cache_params);
131 /* Cause future lookups for this node to fail: */
134 six_lock_wakeup_all(&b->c.lock);
137 int __bch2_btree_node_hash_insert(struct btree_cache *bc, struct btree *b)
140 b->hash_val = btree_ptr_hash_val(&b->key);
142 return rhashtable_lookup_insert_fast(&bc->table, &b->hash,
143 bch_btree_cache_params);
146 int bch2_btree_node_hash_insert(struct btree_cache *bc, struct btree *b,
147 unsigned level, enum btree_id id)
155 six_lock_pcpu_alloc(&b->c.lock);
157 six_lock_pcpu_free_rcu(&b->c.lock);
159 mutex_lock(&bc->lock);
160 ret = __bch2_btree_node_hash_insert(bc, b);
162 list_add(&b->list, &bc->live);
163 mutex_unlock(&bc->lock);
169 static inline struct btree *btree_cache_find(struct btree_cache *bc,
170 const struct bkey_i *k)
172 u64 v = btree_ptr_hash_val(k);
174 return rhashtable_lookup_fast(&bc->table, &v, bch_btree_cache_params);
178 * this version is for btree nodes that have already been freed (we're not
179 * reaping a real btree node)
181 static int __btree_node_reclaim(struct bch_fs *c, struct btree *b, bool flush)
183 struct btree_cache *bc = &c->btree_cache;
186 lockdep_assert_held(&bc->lock);
188 if (!six_trylock_intent(&b->c.lock))
191 if (!six_trylock_write(&b->c.lock))
192 goto out_unlock_intent;
194 if (btree_node_noevict(b))
197 if (!btree_node_may_write(b))
200 if (btree_node_dirty(b) &&
201 test_bit(BCH_FS_HOLD_BTREE_WRITES, &c->flags))
204 if (btree_node_dirty(b) ||
205 btree_node_write_in_flight(b) ||
206 btree_node_read_in_flight(b)) {
210 wait_on_bit_io(&b->flags, BTREE_NODE_read_in_flight,
211 TASK_UNINTERRUPTIBLE);
214 * Using the underscore version because we don't want to compact
215 * bsets after the write, since this node is about to be evicted
216 * - unless btree verify mode is enabled, since it runs out of
217 * the post write cleanup:
219 if (bch2_verify_btree_ondisk)
220 bch2_btree_node_write(c, b, SIX_LOCK_intent);
222 __bch2_btree_node_write(c, b);
224 /* wait for any in flight btree write */
225 btree_node_wait_on_io(b);
228 if (b->hash_val && !ret)
229 trace_btree_node_reap(c, b);
232 six_unlock_write(&b->c.lock);
234 six_unlock_intent(&b->c.lock);
239 static int btree_node_reclaim(struct bch_fs *c, struct btree *b)
241 return __btree_node_reclaim(c, b, false);
244 static int btree_node_write_and_reclaim(struct bch_fs *c, struct btree *b)
246 return __btree_node_reclaim(c, b, true);
249 static unsigned long bch2_btree_cache_scan(struct shrinker *shrink,
250 struct shrink_control *sc)
252 struct bch_fs *c = container_of(shrink, struct bch_fs,
254 struct btree_cache *bc = &c->btree_cache;
256 unsigned long nr = sc->nr_to_scan;
257 unsigned long can_free;
258 unsigned long touched = 0;
259 unsigned long freed = 0;
262 if (bch2_btree_shrinker_disabled)
265 /* Return -1 if we can't do anything right now */
266 if (sc->gfp_mask & __GFP_FS)
267 mutex_lock(&bc->lock);
268 else if (!mutex_trylock(&bc->lock))
271 flags = memalloc_nofs_save();
274 * It's _really_ critical that we don't free too many btree nodes - we
275 * have to always leave ourselves a reserve. The reserve is how we
276 * guarantee that allocating memory for a new btree node can always
277 * succeed, so that inserting keys into the btree can always succeed and
278 * IO can always make forward progress:
280 nr /= btree_pages(c);
281 can_free = btree_cache_can_free(bc);
282 nr = min_t(unsigned long, nr, can_free);
285 list_for_each_entry_safe(b, t, &bc->freeable, list) {
292 !btree_node_reclaim(c, b)) {
293 btree_node_data_free(c, b);
294 six_unlock_write(&b->c.lock);
295 six_unlock_intent(&b->c.lock);
300 list_for_each_entry_safe(b, t, &bc->live, list) {
305 if (&t->list != &bc->live)
306 list_move_tail(&bc->live, &t->list);
310 if (!btree_node_accessed(b) &&
311 !btree_node_reclaim(c, b)) {
312 /* can't call bch2_btree_node_hash_remove under lock */
314 if (&t->list != &bc->live)
315 list_move_tail(&bc->live, &t->list);
317 btree_node_data_free(c, b);
318 mutex_unlock(&bc->lock);
320 bch2_btree_node_hash_remove(bc, b);
321 six_unlock_write(&b->c.lock);
322 six_unlock_intent(&b->c.lock);
327 if (sc->gfp_mask & __GFP_FS)
328 mutex_lock(&bc->lock);
329 else if (!mutex_trylock(&bc->lock))
333 clear_btree_node_accessed(b);
336 mutex_unlock(&bc->lock);
338 memalloc_nofs_restore(flags);
339 return (unsigned long) freed * btree_pages(c);
342 static unsigned long bch2_btree_cache_count(struct shrinker *shrink,
343 struct shrink_control *sc)
345 struct bch_fs *c = container_of(shrink, struct bch_fs,
347 struct btree_cache *bc = &c->btree_cache;
349 if (bch2_btree_shrinker_disabled)
352 return btree_cache_can_free(bc) * btree_pages(c);
355 void bch2_fs_btree_cache_exit(struct bch_fs *c)
357 struct btree_cache *bc = &c->btree_cache;
361 if (bc->shrink.list.next)
362 unregister_shrinker(&bc->shrink);
364 /* vfree() can allocate memory: */
365 flags = memalloc_nofs_save();
366 mutex_lock(&bc->lock);
369 list_move(&c->verify_data->list, &bc->live);
371 kvpfree(c->verify_ondisk, btree_bytes(c));
373 for (i = 0; i < BTREE_ID_NR; i++)
374 if (c->btree_roots[i].b)
375 list_add(&c->btree_roots[i].b->list, &bc->live);
377 list_splice(&bc->freeable, &bc->live);
379 while (!list_empty(&bc->live)) {
380 b = list_first_entry(&bc->live, struct btree, list);
382 BUG_ON(btree_node_read_in_flight(b) ||
383 btree_node_write_in_flight(b));
385 if (btree_node_dirty(b))
386 bch2_btree_complete_write(c, b, btree_current_write(b));
387 clear_btree_node_dirty(c, b);
389 btree_node_data_free(c, b);
392 BUG_ON(atomic_read(&c->btree_cache.dirty));
394 while (!list_empty(&bc->freed)) {
395 b = list_first_entry(&bc->freed, struct btree, list);
397 six_lock_pcpu_free(&b->c.lock);
401 mutex_unlock(&bc->lock);
402 memalloc_nofs_restore(flags);
404 if (bc->table_init_done)
405 rhashtable_destroy(&bc->table);
408 int bch2_fs_btree_cache_init(struct bch_fs *c)
410 struct btree_cache *bc = &c->btree_cache;
414 pr_verbose_init(c->opts, "");
416 ret = rhashtable_init(&bc->table, &bch_btree_cache_params);
420 bc->table_init_done = true;
422 bch2_recalc_btree_reserve(c);
424 for (i = 0; i < bc->reserve; i++)
425 if (!__bch2_btree_node_mem_alloc(c)) {
430 list_splice_init(&bc->live, &bc->freeable);
432 mutex_init(&c->verify_lock);
434 bc->shrink.count_objects = bch2_btree_cache_count;
435 bc->shrink.scan_objects = bch2_btree_cache_scan;
436 bc->shrink.seeks = 4;
437 bc->shrink.batch = btree_pages(c) * 2;
438 ret = register_shrinker(&bc->shrink);
440 pr_verbose_init(c->opts, "ret %i", ret);
444 void bch2_fs_btree_cache_init_early(struct btree_cache *bc)
446 mutex_init(&bc->lock);
447 INIT_LIST_HEAD(&bc->live);
448 INIT_LIST_HEAD(&bc->freeable);
449 INIT_LIST_HEAD(&bc->freed);
453 * We can only have one thread cannibalizing other cached btree nodes at a time,
454 * or we'll deadlock. We use an open coded mutex to ensure that, which a
455 * cannibalize_bucket() will take. This means every time we unlock the root of
456 * the btree, we need to release this lock if we have it held.
458 void bch2_btree_cache_cannibalize_unlock(struct bch_fs *c)
460 struct btree_cache *bc = &c->btree_cache;
462 if (bc->alloc_lock == current) {
463 trace_btree_node_cannibalize_unlock(c);
464 bc->alloc_lock = NULL;
465 closure_wake_up(&bc->alloc_wait);
469 int bch2_btree_cache_cannibalize_lock(struct bch_fs *c, struct closure *cl)
471 struct btree_cache *bc = &c->btree_cache;
472 struct task_struct *old;
474 old = cmpxchg(&bc->alloc_lock, NULL, current);
475 if (old == NULL || old == current)
479 trace_btree_node_cannibalize_lock_fail(c);
483 closure_wait(&bc->alloc_wait, cl);
485 /* Try again, after adding ourselves to waitlist */
486 old = cmpxchg(&bc->alloc_lock, NULL, current);
487 if (old == NULL || old == current) {
489 closure_wake_up(&bc->alloc_wait);
493 trace_btree_node_cannibalize_lock_fail(c);
497 trace_btree_node_cannibalize_lock(c);
501 static struct btree *btree_node_cannibalize(struct bch_fs *c)
503 struct btree_cache *bc = &c->btree_cache;
506 list_for_each_entry_reverse(b, &bc->live, list)
507 if (!btree_node_reclaim(c, b))
511 list_for_each_entry_reverse(b, &bc->live, list)
512 if (!btree_node_write_and_reclaim(c, b))
516 * Rare case: all nodes were intent-locked.
519 WARN_ONCE(1, "btree cache cannibalize failed\n");
524 struct btree *bch2_btree_node_mem_alloc(struct bch_fs *c)
526 struct btree_cache *bc = &c->btree_cache;
528 u64 start_time = local_clock();
531 flags = memalloc_nofs_save();
532 mutex_lock(&bc->lock);
535 * btree_free() doesn't free memory; it sticks the node on the end of
536 * the list. Check if there's any freed nodes there:
538 list_for_each_entry(b, &bc->freeable, list)
539 if (!btree_node_reclaim(c, b))
543 * We never free struct btree itself, just the memory that holds the on
544 * disk node. Check the freed list before allocating a new one:
546 list_for_each_entry(b, &bc->freed, list)
547 if (!btree_node_reclaim(c, b))
553 list_del_init(&b->list);
554 mutex_unlock(&bc->lock);
557 b = __btree_node_mem_alloc(c);
561 BUG_ON(!six_trylock_intent(&b->c.lock));
562 BUG_ON(!six_trylock_write(&b->c.lock));
566 if (btree_node_data_alloc(c, b, __GFP_NOWARN|GFP_KERNEL))
569 mutex_lock(&bc->lock);
571 mutex_unlock(&bc->lock);
574 BUG_ON(btree_node_hashed(b));
575 BUG_ON(btree_node_write_in_flight(b));
582 b->whiteout_u64s = 0;
583 bch2_btree_keys_init(b);
584 set_btree_node_accessed(b);
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;
688 static noinline void btree_bad_header(struct bch_fs *c, struct btree *b)
690 char buf1[100], buf2[100], buf3[100], buf4[100];
692 if (!test_bit(BCH_FS_INITIAL_GC_DONE, &c->flags))
695 bch2_bpos_to_text(&PBUF(buf1), b->key.k.type == KEY_TYPE_btree_ptr_v2
696 ? bkey_i_to_btree_ptr_v2(&b->key)->v.min_key
698 bch2_bpos_to_text(&PBUF(buf2), b->data->min_key);
700 bch2_bpos_to_text(&PBUF(buf3), b->key.k.p);
701 bch2_bpos_to_text(&PBUF(buf4), b->data->max_key);
702 bch2_fs_inconsistent(c, "btree node header doesn't match ptr\n"
703 "btree: ptr %u header %llu\n"
704 "level: ptr %u header %llu\n"
705 "min ptr %s node header %s\n"
706 "max ptr %s node header %s",
707 b->c.btree_id, BTREE_NODE_ID(b->data),
708 b->c.level, BTREE_NODE_LEVEL(b->data),
709 buf1, buf2, buf3, buf4);
712 static inline void btree_check_header(struct bch_fs *c, struct btree *b)
714 if (b->c.btree_id != BTREE_NODE_ID(b->data) ||
715 b->c.level != BTREE_NODE_LEVEL(b->data) ||
716 bpos_cmp(b->data->max_key, b->key.k.p) ||
717 (b->key.k.type == KEY_TYPE_btree_ptr_v2 &&
718 bpos_cmp(b->data->min_key,
719 bkey_i_to_btree_ptr_v2(&b->key)->v.min_key)))
720 btree_bad_header(c, b);
724 * bch_btree_node_get - find a btree node in the cache and lock it, reading it
725 * in from disk if necessary.
727 * If IO is necessary and running under generic_make_request, returns -EAGAIN.
729 * The btree node will have either a read or a write lock held, depending on
730 * the @write parameter.
732 struct btree *bch2_btree_node_get(struct bch_fs *c, struct btree_iter *iter,
733 const struct bkey_i *k, unsigned level,
734 enum six_lock_type lock_type,
735 unsigned long trace_ip)
737 struct btree_cache *bc = &c->btree_cache;
741 EBUG_ON(level >= BTREE_MAX_DEPTH);
743 b = btree_node_mem_ptr(k);
747 b = btree_cache_find(bc, k);
750 * We must have the parent locked to call bch2_btree_node_fill(),
751 * else we could read in a btree node from disk that's been
754 b = bch2_btree_node_fill(c, iter, k, iter->btree_id,
755 level, lock_type, true);
757 /* We raced and found the btree node in the cache */
766 * There's a potential deadlock with splits and insertions into
767 * interior nodes we have to avoid:
769 * The other thread might be holding an intent lock on the node
770 * we want, and they want to update its parent node so they're
771 * going to upgrade their intent lock on the parent node to a
774 * But if we're holding a read lock on the parent, and we're
775 * trying to get the intent lock they're holding, we deadlock.
777 * So to avoid this we drop the read locks on parent nodes when
778 * we're starting to take intent locks - and handle the race.
780 * The race is that they might be about to free the node we
781 * want, and dropping our read lock on the parent node lets them
782 * update the parent marking the node we want as freed, and then
785 * To guard against this, btree nodes are evicted from the cache
786 * when they're freed - and b->hash_val is zeroed out, which we
787 * check for after we lock the node.
789 * Then, bch2_btree_node_relock() on the parent will fail - because
790 * the parent was modified, when the pointer to the node we want
791 * was removed - and we'll bail out:
793 if (btree_node_read_locked(iter, level + 1))
794 btree_node_unlock(iter, level + 1);
796 if (!btree_node_lock(b, k->k.p, level, iter, lock_type,
797 lock_node_check_fn, (void *) k, trace_ip)) {
798 if (b->hash_val != btree_ptr_hash_val(k))
800 return ERR_PTR(-EINTR);
803 if (unlikely(b->hash_val != btree_ptr_hash_val(k) ||
804 b->c.level != level ||
806 six_unlock_type(&b->c.lock, lock_type);
807 if (bch2_btree_node_relock(iter, level + 1))
810 trace_trans_restart_btree_node_reused(iter->trans->ip,
814 return ERR_PTR(-EINTR);
818 /* XXX: waiting on IO with btree locks held: */
819 wait_on_bit_io(&b->flags, BTREE_NODE_read_in_flight,
820 TASK_UNINTERRUPTIBLE);
822 prefetch(b->aux_data);
824 for_each_bset(b, t) {
825 void *p = (u64 *) b->aux_data + t->aux_data_offset;
827 prefetch(p + L1_CACHE_BYTES * 0);
828 prefetch(p + L1_CACHE_BYTES * 1);
829 prefetch(p + L1_CACHE_BYTES * 2);
832 /* avoid atomic set bit if it's not needed: */
833 if (!btree_node_accessed(b))
834 set_btree_node_accessed(b);
836 if (unlikely(btree_node_read_error(b))) {
837 six_unlock_type(&b->c.lock, lock_type);
838 return ERR_PTR(-EIO);
841 EBUG_ON(b->c.btree_id != iter->btree_id);
842 EBUG_ON(BTREE_NODE_LEVEL(b->data) != level);
843 btree_check_header(c, b);
848 struct btree *bch2_btree_node_get_noiter(struct bch_fs *c,
849 const struct bkey_i *k,
850 enum btree_id btree_id,
854 struct btree_cache *bc = &c->btree_cache;
859 EBUG_ON(level >= BTREE_MAX_DEPTH);
861 b = btree_node_mem_ptr(k);
865 b = btree_cache_find(bc, k);
870 b = bch2_btree_node_fill(c, NULL, k, btree_id,
871 level, SIX_LOCK_read, true);
873 /* We raced and found the btree node in the cache */
878 !bch2_btree_cache_cannibalize_lock(c, NULL))
885 ret = six_lock_read(&b->c.lock, lock_node_check_fn, (void *) k);
889 if (unlikely(b->hash_val != btree_ptr_hash_val(k) ||
890 b->c.btree_id != btree_id ||
891 b->c.level != level)) {
892 six_unlock_read(&b->c.lock);
897 /* XXX: waiting on IO with btree locks held: */
898 wait_on_bit_io(&b->flags, BTREE_NODE_read_in_flight,
899 TASK_UNINTERRUPTIBLE);
901 prefetch(b->aux_data);
903 for_each_bset(b, t) {
904 void *p = (u64 *) b->aux_data + t->aux_data_offset;
906 prefetch(p + L1_CACHE_BYTES * 0);
907 prefetch(p + L1_CACHE_BYTES * 1);
908 prefetch(p + L1_CACHE_BYTES * 2);
911 /* avoid atomic set bit if it's not needed: */
912 if (!btree_node_accessed(b))
913 set_btree_node_accessed(b);
915 if (unlikely(btree_node_read_error(b))) {
916 six_unlock_read(&b->c.lock);
921 EBUG_ON(b->c.btree_id != btree_id);
922 EBUG_ON(BTREE_NODE_LEVEL(b->data) != level);
923 btree_check_header(c, b);
925 bch2_btree_cache_cannibalize_unlock(c);
929 void bch2_btree_node_prefetch(struct bch_fs *c, struct btree_iter *iter,
930 const struct bkey_i *k,
931 enum btree_id btree_id, unsigned level)
933 struct btree_cache *bc = &c->btree_cache;
936 BUG_ON(iter && !btree_node_locked(iter, level + 1));
937 BUG_ON(level >= BTREE_MAX_DEPTH);
939 b = btree_cache_find(bc, k);
943 bch2_btree_node_fill(c, iter, k, btree_id, level, SIX_LOCK_read, false);
946 void bch2_btree_node_evict(struct bch_fs *c, const struct bkey_i *k)
948 struct btree_cache *bc = &c->btree_cache;
951 b = btree_cache_find(bc, k);
955 six_lock_intent(&b->c.lock, NULL, NULL);
956 six_lock_write(&b->c.lock, NULL, NULL);
958 wait_on_bit_io(&b->flags, BTREE_NODE_read_in_flight,
959 TASK_UNINTERRUPTIBLE);
960 __bch2_btree_node_write(c, b);
962 /* wait for any in flight btree write */
963 btree_node_wait_on_io(b);
965 BUG_ON(btree_node_dirty(b));
967 mutex_lock(&bc->lock);
968 btree_node_data_free(c, b);
969 bch2_btree_node_hash_remove(bc, b);
970 mutex_unlock(&bc->lock);
972 six_unlock_write(&b->c.lock);
973 six_unlock_intent(&b->c.lock);
976 void bch2_btree_node_to_text(struct printbuf *out, struct bch_fs *c,
979 const struct bkey_format *f = &b->format;
980 struct bset_stats stats;
982 memset(&stats, 0, sizeof(stats));
984 bch2_btree_keys_stats(b, &stats);
986 pr_buf(out, "l %u ", b->c.level);
987 bch2_bpos_to_text(out, b->data->min_key);
989 bch2_bpos_to_text(out, b->data->max_key);
992 bch2_val_to_text(out, c, bkey_i_to_s_c(&b->key));
995 " format: u64s %u fields %u %u %u %u %u\n"
996 " unpack fn len: %u\n"
997 " bytes used %zu/%zu (%zu%% full)\n"
998 " sib u64s: %u, %u (merge threshold %u)\n"
999 " nr packed keys %u\n"
1000 " nr unpacked keys %u\n"
1002 " failed unpacked %zu\n",
1004 f->bits_per_field[0],
1005 f->bits_per_field[1],
1006 f->bits_per_field[2],
1007 f->bits_per_field[3],
1008 f->bits_per_field[4],
1010 b->nr.live_u64s * sizeof(u64),
1011 btree_bytes(c) - sizeof(struct btree_node),
1012 b->nr.live_u64s * 100 / btree_max_u64s(c),
1015 c->btree_foreground_merge_threshold,
1017 b->nr.unpacked_keys,
1022 void bch2_btree_cache_to_text(struct printbuf *out, struct bch_fs *c)
1024 pr_buf(out, "nr nodes:\t\t%u\n", c->btree_cache.used);
1025 pr_buf(out, "nr dirty:\t\t%u\n", atomic_read(&c->btree_cache.dirty));
1026 pr_buf(out, "cannibalize lock:\t%p\n", c->btree_cache.alloc_lock);