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 btree_trans_restart(iter->trans);
653 return ERR_PTR(-EINTR);
656 b = bch2_btree_node_mem_alloc(c);
660 bkey_copy(&b->key, k);
661 if (bch2_btree_node_hash_insert(bc, b, level, btree_id)) {
662 /* raced with another fill: */
664 /* mark as unhashed... */
667 mutex_lock(&bc->lock);
668 list_add(&b->list, &bc->freeable);
669 mutex_unlock(&bc->lock);
671 six_unlock_write(&b->c.lock);
672 six_unlock_intent(&b->c.lock);
676 set_btree_node_read_in_flight(b);
678 six_unlock_write(&b->c.lock);
679 seq = b->c.lock.state.seq;
680 six_unlock_intent(&b->c.lock);
682 /* Unlock before doing IO: */
684 bch2_trans_unlock(iter->trans);
686 bch2_btree_node_read(c, b, sync);
692 (!bch2_trans_relock(iter->trans) ||
693 !bch2_btree_iter_relock_intent(iter))) {
694 BUG_ON(!iter->trans->restarted);
695 return ERR_PTR(-EINTR);
698 if (!six_relock_type(&b->c.lock, lock_type, seq)) {
699 btree_trans_restart(iter->trans);
700 return ERR_PTR(-EINTR);
706 static int lock_node_check_fn(struct six_lock *lock, void *p)
708 struct btree *b = container_of(lock, struct btree, c.lock);
709 const struct bkey_i *k = p;
711 return b->hash_val == btree_ptr_hash_val(k) ? 0 : -1;
714 static noinline void btree_bad_header(struct bch_fs *c, struct btree *b)
716 char buf1[200], buf2[100], buf3[100];
718 if (!test_bit(BCH_FS_INITIAL_GC_DONE, &c->flags))
721 bch2_bkey_val_to_text(&PBUF(buf1), c, bkey_i_to_s_c(&b->key));
722 bch2_bpos_to_text(&PBUF(buf2), b->data->min_key);
723 bch2_bpos_to_text(&PBUF(buf3), b->data->max_key);
725 bch2_fs_inconsistent(c, "btree node header doesn't match ptr\n"
726 "btree %s level %u\n"
728 "header: btree %s level %llu\n"
730 bch2_btree_ids[b->c.btree_id], b->c.level,
732 bch2_btree_ids[BTREE_NODE_ID(b->data)],
733 BTREE_NODE_LEVEL(b->data),
737 static inline void btree_check_header(struct bch_fs *c, struct btree *b)
739 if (b->c.btree_id != BTREE_NODE_ID(b->data) ||
740 b->c.level != BTREE_NODE_LEVEL(b->data) ||
741 bpos_cmp(b->data->max_key, b->key.k.p) ||
742 (b->key.k.type == KEY_TYPE_btree_ptr_v2 &&
743 bpos_cmp(b->data->min_key,
744 bkey_i_to_btree_ptr_v2(&b->key)->v.min_key)))
745 btree_bad_header(c, b);
749 * bch_btree_node_get - find a btree node in the cache and lock it, reading it
750 * in from disk if necessary.
752 * If IO is necessary and running under generic_make_request, returns -EAGAIN.
754 * The btree node will have either a read or a write lock held, depending on
755 * the @write parameter.
757 struct btree *bch2_btree_node_get(struct btree_trans *trans, struct btree_iter *iter,
758 const struct bkey_i *k, unsigned level,
759 enum six_lock_type lock_type,
760 unsigned long trace_ip)
762 struct bch_fs *c = trans->c;
763 struct btree_cache *bc = &c->btree_cache;
767 EBUG_ON(level >= BTREE_MAX_DEPTH);
769 if (c->opts.btree_node_mem_ptr_optimization) {
770 b = btree_node_mem_ptr(k);
775 b = btree_cache_find(bc, k);
778 * We must have the parent locked to call bch2_btree_node_fill(),
779 * else we could read in a btree node from disk that's been
782 b = bch2_btree_node_fill(c, iter, k, iter->btree_id,
783 level, lock_type, true);
785 /* We raced and found the btree node in the cache */
794 * There's a potential deadlock with splits and insertions into
795 * interior nodes we have to avoid:
797 * The other thread might be holding an intent lock on the node
798 * we want, and they want to update its parent node so they're
799 * going to upgrade their intent lock on the parent node to a
802 * But if we're holding a read lock on the parent, and we're
803 * trying to get the intent lock they're holding, we deadlock.
805 * So to avoid this we drop the read locks on parent nodes when
806 * we're starting to take intent locks - and handle the race.
808 * The race is that they might be about to free the node we
809 * want, and dropping our read lock on the parent node lets them
810 * update the parent marking the node we want as freed, and then
813 * To guard against this, btree nodes are evicted from the cache
814 * when they're freed - and b->hash_val is zeroed out, which we
815 * check for after we lock the node.
817 * Then, bch2_btree_node_relock() on the parent will fail - because
818 * the parent was modified, when the pointer to the node we want
819 * was removed - and we'll bail out:
821 if (btree_node_read_locked(iter, level + 1))
822 btree_node_unlock(iter, level + 1);
824 if (!btree_node_lock(b, k->k.p, level, iter, lock_type,
825 lock_node_check_fn, (void *) k, trace_ip)) {
826 if (!trans->restarted)
828 return ERR_PTR(-EINTR);
831 if (unlikely(b->hash_val != btree_ptr_hash_val(k) ||
832 b->c.level != level ||
834 six_unlock_type(&b->c.lock, lock_type);
835 if (bch2_btree_node_relock(iter, level + 1))
838 trace_trans_restart_btree_node_reused(trans->ip,
842 btree_trans_restart(trans);
843 return ERR_PTR(-EINTR);
847 if (unlikely(btree_node_read_in_flight(b))) {
848 u32 seq = b->c.lock.state.seq;
850 six_unlock_type(&b->c.lock, lock_type);
851 bch2_trans_unlock(trans);
853 bch2_btree_node_wait_on_read(b);
856 * should_be_locked is not set on this iterator yet, so we need
857 * to relock it specifically:
860 (!bch2_trans_relock(trans) ||
861 !bch2_btree_iter_relock_intent(iter))) {
862 BUG_ON(!trans->restarted);
863 return ERR_PTR(-EINTR);
866 if (!six_relock_type(&b->c.lock, lock_type, seq))
870 prefetch(b->aux_data);
872 for_each_bset(b, t) {
873 void *p = (u64 *) b->aux_data + t->aux_data_offset;
875 prefetch(p + L1_CACHE_BYTES * 0);
876 prefetch(p + L1_CACHE_BYTES * 1);
877 prefetch(p + L1_CACHE_BYTES * 2);
880 /* avoid atomic set bit if it's not needed: */
881 if (!btree_node_accessed(b))
882 set_btree_node_accessed(b);
884 if (unlikely(btree_node_read_error(b))) {
885 six_unlock_type(&b->c.lock, lock_type);
886 return ERR_PTR(-EIO);
889 EBUG_ON(b->c.btree_id != iter->btree_id);
890 EBUG_ON(BTREE_NODE_LEVEL(b->data) != level);
891 btree_check_header(c, b);
896 struct btree *bch2_btree_node_get_noiter(struct bch_fs *c,
897 const struct bkey_i *k,
898 enum btree_id btree_id,
902 struct btree_cache *bc = &c->btree_cache;
907 EBUG_ON(level >= BTREE_MAX_DEPTH);
909 if (c->opts.btree_node_mem_ptr_optimization) {
910 b = btree_node_mem_ptr(k);
915 b = btree_cache_find(bc, k);
920 b = bch2_btree_node_fill(c, NULL, k, btree_id,
921 level, SIX_LOCK_read, true);
923 /* We raced and found the btree node in the cache */
928 !bch2_btree_cache_cannibalize_lock(c, NULL))
935 ret = six_lock_read(&b->c.lock, lock_node_check_fn, (void *) k);
939 if (unlikely(b->hash_val != btree_ptr_hash_val(k) ||
940 b->c.btree_id != btree_id ||
941 b->c.level != level)) {
942 six_unlock_read(&b->c.lock);
947 /* XXX: waiting on IO with btree locks held: */
948 __bch2_btree_node_wait_on_read(b);
950 prefetch(b->aux_data);
952 for_each_bset(b, t) {
953 void *p = (u64 *) b->aux_data + t->aux_data_offset;
955 prefetch(p + L1_CACHE_BYTES * 0);
956 prefetch(p + L1_CACHE_BYTES * 1);
957 prefetch(p + L1_CACHE_BYTES * 2);
960 /* avoid atomic set bit if it's not needed: */
961 if (!btree_node_accessed(b))
962 set_btree_node_accessed(b);
964 if (unlikely(btree_node_read_error(b))) {
965 six_unlock_read(&b->c.lock);
970 EBUG_ON(b->c.btree_id != btree_id);
971 EBUG_ON(BTREE_NODE_LEVEL(b->data) != level);
972 btree_check_header(c, b);
974 bch2_btree_cache_cannibalize_unlock(c);
978 int bch2_btree_node_prefetch(struct bch_fs *c, struct btree_iter *iter,
979 const struct bkey_i *k,
980 enum btree_id btree_id, unsigned level)
982 struct btree_cache *bc = &c->btree_cache;
985 BUG_ON(iter && !btree_node_locked(iter, level + 1));
986 BUG_ON(level >= BTREE_MAX_DEPTH);
988 b = btree_cache_find(bc, k);
992 b = bch2_btree_node_fill(c, iter, k, btree_id, level, SIX_LOCK_read, false);
993 return PTR_ERR_OR_ZERO(b);
996 void bch2_btree_node_evict(struct bch_fs *c, const struct bkey_i *k)
998 struct btree_cache *bc = &c->btree_cache;
1001 b = btree_cache_find(bc, k);
1005 /* not allowed to wait on io with btree locks held: */
1007 /* XXX we're called from btree_gc which will be holding other btree
1010 __bch2_btree_node_wait_on_read(b);
1011 __bch2_btree_node_wait_on_write(b);
1013 six_lock_intent(&b->c.lock, NULL, NULL);
1014 six_lock_write(&b->c.lock, NULL, NULL);
1016 if (btree_node_dirty(b)) {
1017 __bch2_btree_node_write(c, b, false);
1018 six_unlock_write(&b->c.lock);
1019 six_unlock_intent(&b->c.lock);
1023 BUG_ON(btree_node_dirty(b));
1025 mutex_lock(&bc->lock);
1026 btree_node_data_free(c, b);
1027 bch2_btree_node_hash_remove(bc, b);
1028 mutex_unlock(&bc->lock);
1030 six_unlock_write(&b->c.lock);
1031 six_unlock_intent(&b->c.lock);
1034 void bch2_btree_node_to_text(struct printbuf *out, struct bch_fs *c,
1037 const struct bkey_format *f = &b->format;
1038 struct bset_stats stats;
1040 memset(&stats, 0, sizeof(stats));
1042 bch2_btree_keys_stats(b, &stats);
1044 pr_buf(out, "l %u ", b->c.level);
1045 bch2_bpos_to_text(out, b->data->min_key);
1047 bch2_bpos_to_text(out, b->data->max_key);
1050 bch2_val_to_text(out, c, bkey_i_to_s_c(&b->key));
1053 " format: u64s %u fields %u %u %u %u %u\n"
1054 " unpack fn len: %u\n"
1055 " bytes used %zu/%zu (%zu%% full)\n"
1056 " sib u64s: %u, %u (merge threshold %u)\n"
1057 " nr packed keys %u\n"
1058 " nr unpacked keys %u\n"
1060 " failed unpacked %zu\n",
1062 f->bits_per_field[0],
1063 f->bits_per_field[1],
1064 f->bits_per_field[2],
1065 f->bits_per_field[3],
1066 f->bits_per_field[4],
1068 b->nr.live_u64s * sizeof(u64),
1069 btree_bytes(c) - sizeof(struct btree_node),
1070 b->nr.live_u64s * 100 / btree_max_u64s(c),
1073 c->btree_foreground_merge_threshold,
1075 b->nr.unpacked_keys,
1080 void bch2_btree_cache_to_text(struct printbuf *out, struct bch_fs *c)
1082 pr_buf(out, "nr nodes:\t\t%u\n", c->btree_cache.used);
1083 pr_buf(out, "nr dirty:\t\t%u\n", atomic_read(&c->btree_cache.dirty));
1084 pr_buf(out, "cannibalize lock:\t%p\n", c->btree_cache.alloc_lock);