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 int ret = rhashtable_remove_fast(&bc->table, &b->hash, bch_btree_cache_params);
134 /* Cause future lookups for this node to fail: */
137 six_lock_wakeup_all(&b->c.lock);
140 int __bch2_btree_node_hash_insert(struct btree_cache *bc, struct btree *b)
143 b->hash_val = btree_ptr_hash_val(&b->key);
145 return rhashtable_lookup_insert_fast(&bc->table, &b->hash,
146 bch_btree_cache_params);
149 int bch2_btree_node_hash_insert(struct btree_cache *bc, struct btree *b,
150 unsigned level, enum btree_id id)
158 six_lock_pcpu_alloc(&b->c.lock);
160 six_lock_pcpu_free_rcu(&b->c.lock);
162 mutex_lock(&bc->lock);
163 ret = __bch2_btree_node_hash_insert(bc, b);
165 list_add(&b->list, &bc->live);
166 mutex_unlock(&bc->lock);
172 static inline struct btree *btree_cache_find(struct btree_cache *bc,
173 const struct bkey_i *k)
175 u64 v = btree_ptr_hash_val(k);
177 return rhashtable_lookup_fast(&bc->table, &v, bch_btree_cache_params);
181 * this version is for btree nodes that have already been freed (we're not
182 * reaping a real btree node)
184 static int __btree_node_reclaim(struct bch_fs *c, struct btree *b, bool flush)
186 struct btree_cache *bc = &c->btree_cache;
189 lockdep_assert_held(&bc->lock);
191 if (b->flags & ((1U << BTREE_NODE_dirty)|
192 (1U << BTREE_NODE_read_in_flight)|
193 (1U << BTREE_NODE_write_in_flight))) {
197 /* XXX: waiting on IO with btree cache lock held */
198 bch2_btree_node_wait_on_read(b);
199 bch2_btree_node_wait_on_write(b);
202 if (!six_trylock_intent(&b->c.lock))
205 if (!six_trylock_write(&b->c.lock))
206 goto out_unlock_intent;
208 /* recheck under lock */
209 if (b->flags & ((1U << BTREE_NODE_read_in_flight)|
210 (1U << BTREE_NODE_write_in_flight))) {
213 six_unlock_write(&b->c.lock);
214 six_unlock_intent(&b->c.lock);
218 if (btree_node_noevict(b))
221 if (!btree_node_may_write(b))
224 if (btree_node_dirty(b)) {
226 test_bit(BCH_FS_HOLD_BTREE_WRITES, &c->flags))
229 * Using the underscore version because we don't want to compact
230 * bsets after the write, since this node is about to be evicted
231 * - unless btree verify mode is enabled, since it runs out of
232 * the post write cleanup:
234 if (bch2_verify_btree_ondisk)
235 bch2_btree_node_write(c, b, SIX_LOCK_intent);
237 __bch2_btree_node_write(c, b, false);
239 six_unlock_write(&b->c.lock);
240 six_unlock_intent(&b->c.lock);
244 if (b->hash_val && !ret)
245 trace_btree_node_reap(c, b);
248 six_unlock_write(&b->c.lock);
250 six_unlock_intent(&b->c.lock);
255 static int btree_node_reclaim(struct bch_fs *c, struct btree *b)
257 return __btree_node_reclaim(c, b, false);
260 static int btree_node_write_and_reclaim(struct bch_fs *c, struct btree *b)
262 return __btree_node_reclaim(c, b, true);
265 static unsigned long bch2_btree_cache_scan(struct shrinker *shrink,
266 struct shrink_control *sc)
268 struct bch_fs *c = container_of(shrink, struct bch_fs,
270 struct btree_cache *bc = &c->btree_cache;
272 unsigned long nr = sc->nr_to_scan;
273 unsigned long can_free;
274 unsigned long touched = 0;
275 unsigned long freed = 0;
277 unsigned long ret = SHRINK_STOP;
279 if (bch2_btree_shrinker_disabled)
282 /* Return -1 if we can't do anything right now */
283 if (sc->gfp_mask & __GFP_FS)
284 mutex_lock(&bc->lock);
285 else if (!mutex_trylock(&bc->lock))
288 flags = memalloc_nofs_save();
291 * It's _really_ critical that we don't free too many btree nodes - we
292 * have to always leave ourselves a reserve. The reserve is how we
293 * guarantee that allocating memory for a new btree node can always
294 * succeed, so that inserting keys into the btree can always succeed and
295 * IO can always make forward progress:
297 nr /= btree_pages(c);
298 can_free = btree_cache_can_free(bc);
299 nr = min_t(unsigned long, nr, can_free);
302 list_for_each_entry_safe(b, t, &bc->freeable, list) {
304 * Leave a few nodes on the freeable list, so that a btree split
305 * won't have to hit the system allocator:
315 if (!btree_node_reclaim(c, b)) {
316 btree_node_data_free(c, b);
317 six_unlock_write(&b->c.lock);
318 six_unlock_intent(&b->c.lock);
323 list_for_each_entry_safe(b, t, &bc->live, list) {
328 if (&t->list != &bc->live)
329 list_move_tail(&bc->live, &t->list);
333 if (!btree_node_accessed(b) &&
334 !btree_node_reclaim(c, b)) {
335 /* can't call bch2_btree_node_hash_remove under lock */
337 if (&t->list != &bc->live)
338 list_move_tail(&bc->live, &t->list);
340 btree_node_data_free(c, b);
341 mutex_unlock(&bc->lock);
343 bch2_btree_node_hash_remove(bc, b);
344 six_unlock_write(&b->c.lock);
345 six_unlock_intent(&b->c.lock);
350 if (sc->gfp_mask & __GFP_FS)
351 mutex_lock(&bc->lock);
352 else if (!mutex_trylock(&bc->lock))
356 clear_btree_node_accessed(b);
359 mutex_unlock(&bc->lock);
361 ret = (unsigned long) freed * btree_pages(c);
362 memalloc_nofs_restore(flags);
364 trace_btree_cache_scan(sc->nr_to_scan,
365 sc->nr_to_scan / btree_pages(c),
366 btree_cache_can_free(bc),
371 static unsigned long bch2_btree_cache_count(struct shrinker *shrink,
372 struct shrink_control *sc)
374 struct bch_fs *c = container_of(shrink, struct bch_fs,
376 struct btree_cache *bc = &c->btree_cache;
378 if (bch2_btree_shrinker_disabled)
381 return btree_cache_can_free(bc) * btree_pages(c);
384 void bch2_fs_btree_cache_exit(struct bch_fs *c)
386 struct btree_cache *bc = &c->btree_cache;
390 if (bc->shrink.list.next)
391 unregister_shrinker(&bc->shrink);
393 /* vfree() can allocate memory: */
394 flags = memalloc_nofs_save();
395 mutex_lock(&bc->lock);
398 list_move(&c->verify_data->list, &bc->live);
400 kvpfree(c->verify_ondisk, btree_bytes(c));
402 for (i = 0; i < BTREE_ID_NR; i++)
403 if (c->btree_roots[i].b)
404 list_add(&c->btree_roots[i].b->list, &bc->live);
406 list_splice(&bc->freeable, &bc->live);
408 while (!list_empty(&bc->live)) {
409 b = list_first_entry(&bc->live, struct btree, list);
411 BUG_ON(btree_node_read_in_flight(b) ||
412 btree_node_write_in_flight(b));
414 if (btree_node_dirty(b))
415 bch2_btree_complete_write(c, b, btree_current_write(b));
416 clear_btree_node_dirty(c, b);
418 btree_node_data_free(c, b);
421 BUG_ON(atomic_read(&c->btree_cache.dirty));
423 while (!list_empty(&bc->freed)) {
424 b = list_first_entry(&bc->freed, struct btree, list);
426 six_lock_pcpu_free(&b->c.lock);
430 mutex_unlock(&bc->lock);
431 memalloc_nofs_restore(flags);
433 if (bc->table_init_done)
434 rhashtable_destroy(&bc->table);
437 int bch2_fs_btree_cache_init(struct bch_fs *c)
439 struct btree_cache *bc = &c->btree_cache;
443 pr_verbose_init(c->opts, "");
445 ret = rhashtable_init(&bc->table, &bch_btree_cache_params);
449 bc->table_init_done = true;
451 bch2_recalc_btree_reserve(c);
453 for (i = 0; i < bc->reserve; i++)
454 if (!__bch2_btree_node_mem_alloc(c)) {
459 list_splice_init(&bc->live, &bc->freeable);
461 mutex_init(&c->verify_lock);
463 bc->shrink.count_objects = bch2_btree_cache_count;
464 bc->shrink.scan_objects = bch2_btree_cache_scan;
465 bc->shrink.seeks = 4;
466 bc->shrink.batch = btree_pages(c) * 2;
467 ret = register_shrinker(&bc->shrink);
469 pr_verbose_init(c->opts, "ret %i", ret);
473 void bch2_fs_btree_cache_init_early(struct btree_cache *bc)
475 mutex_init(&bc->lock);
476 INIT_LIST_HEAD(&bc->live);
477 INIT_LIST_HEAD(&bc->freeable);
478 INIT_LIST_HEAD(&bc->freed);
482 * We can only have one thread cannibalizing other cached btree nodes at a time,
483 * or we'll deadlock. We use an open coded mutex to ensure that, which a
484 * cannibalize_bucket() will take. This means every time we unlock the root of
485 * the btree, we need to release this lock if we have it held.
487 void bch2_btree_cache_cannibalize_unlock(struct bch_fs *c)
489 struct btree_cache *bc = &c->btree_cache;
491 if (bc->alloc_lock == current) {
492 trace_btree_node_cannibalize_unlock(c);
493 bc->alloc_lock = NULL;
494 closure_wake_up(&bc->alloc_wait);
498 int bch2_btree_cache_cannibalize_lock(struct bch_fs *c, struct closure *cl)
500 struct btree_cache *bc = &c->btree_cache;
501 struct task_struct *old;
503 old = cmpxchg(&bc->alloc_lock, NULL, current);
504 if (old == NULL || old == current)
508 trace_btree_node_cannibalize_lock_fail(c);
512 closure_wait(&bc->alloc_wait, cl);
514 /* Try again, after adding ourselves to waitlist */
515 old = cmpxchg(&bc->alloc_lock, NULL, current);
516 if (old == NULL || old == current) {
518 closure_wake_up(&bc->alloc_wait);
522 trace_btree_node_cannibalize_lock_fail(c);
526 trace_btree_node_cannibalize_lock(c);
530 static struct btree *btree_node_cannibalize(struct bch_fs *c)
532 struct btree_cache *bc = &c->btree_cache;
535 list_for_each_entry_reverse(b, &bc->live, list)
536 if (!btree_node_reclaim(c, b))
540 list_for_each_entry_reverse(b, &bc->live, list)
541 if (!btree_node_write_and_reclaim(c, b))
545 * Rare case: all nodes were intent-locked.
548 WARN_ONCE(1, "btree cache cannibalize failed\n");
553 struct btree *bch2_btree_node_mem_alloc(struct bch_fs *c)
555 struct btree_cache *bc = &c->btree_cache;
557 u64 start_time = local_clock();
560 flags = memalloc_nofs_save();
561 mutex_lock(&bc->lock);
564 * btree_free() doesn't free memory; it sticks the node on the end of
565 * the list. Check if there's any freed nodes there:
567 list_for_each_entry(b, &bc->freeable, list)
568 if (!btree_node_reclaim(c, b))
572 * We never free struct btree itself, just the memory that holds the on
573 * disk node. Check the freed list before allocating a new one:
575 list_for_each_entry(b, &bc->freed, list)
576 if (!btree_node_reclaim(c, b))
582 list_del_init(&b->list);
583 mutex_unlock(&bc->lock);
586 b = __btree_node_mem_alloc(c);
590 BUG_ON(!six_trylock_intent(&b->c.lock));
591 BUG_ON(!six_trylock_write(&b->c.lock));
595 if (btree_node_data_alloc(c, b, __GFP_NOWARN|GFP_KERNEL))
598 mutex_lock(&bc->lock);
600 mutex_unlock(&bc->lock);
603 BUG_ON(btree_node_hashed(b));
604 BUG_ON(btree_node_dirty(b));
605 BUG_ON(btree_node_write_in_flight(b));
612 b->whiteout_u64s = 0;
613 bch2_btree_keys_init(b);
614 set_btree_node_accessed(b);
616 bch2_time_stats_update(&c->times[BCH_TIME_btree_node_mem_alloc],
619 memalloc_nofs_restore(flags);
622 mutex_lock(&bc->lock);
625 list_add(&b->list, &bc->freed);
626 six_unlock_write(&b->c.lock);
627 six_unlock_intent(&b->c.lock);
630 /* Try to cannibalize another cached btree node: */
631 if (bc->alloc_lock == current) {
632 b = btree_node_cannibalize(c);
633 list_del_init(&b->list);
634 mutex_unlock(&bc->lock);
636 bch2_btree_node_hash_remove(bc, b);
638 trace_btree_node_cannibalize(c);
642 mutex_unlock(&bc->lock);
643 memalloc_nofs_restore(flags);
644 return ERR_PTR(-ENOMEM);
647 /* Slowpath, don't want it inlined into btree_iter_traverse() */
648 static noinline struct btree *bch2_btree_node_fill(struct bch_fs *c,
649 struct btree_trans *trans,
650 struct btree_path *path,
651 const struct bkey_i *k,
652 enum btree_id btree_id,
654 enum six_lock_type lock_type,
657 struct btree_cache *bc = &c->btree_cache;
661 BUG_ON(level + 1 >= BTREE_MAX_DEPTH);
663 * Parent node must be locked, else we could read in a btree node that's
666 if (trans && !bch2_btree_node_relock(trans, path, level + 1)) {
667 btree_trans_restart(trans);
668 return ERR_PTR(-EINTR);
671 b = bch2_btree_node_mem_alloc(c);
675 bkey_copy(&b->key, k);
676 if (bch2_btree_node_hash_insert(bc, b, level, btree_id)) {
677 /* raced with another fill: */
679 /* mark as unhashed... */
682 mutex_lock(&bc->lock);
683 list_add(&b->list, &bc->freeable);
684 mutex_unlock(&bc->lock);
686 six_unlock_write(&b->c.lock);
687 six_unlock_intent(&b->c.lock);
691 set_btree_node_read_in_flight(b);
693 six_unlock_write(&b->c.lock);
694 seq = b->c.lock.state.seq;
695 six_unlock_intent(&b->c.lock);
697 /* Unlock before doing IO: */
699 bch2_trans_unlock(trans);
701 bch2_btree_node_read(c, b, sync);
707 (!bch2_trans_relock(trans) ||
708 !bch2_btree_path_relock_intent(trans, path))) {
709 BUG_ON(!trans->restarted);
710 return ERR_PTR(-EINTR);
713 if (!six_relock_type(&b->c.lock, lock_type, seq)) {
714 btree_trans_restart(trans);
715 return ERR_PTR(-EINTR);
721 static int lock_node_check_fn(struct six_lock *lock, void *p)
723 struct btree *b = container_of(lock, struct btree, c.lock);
724 const struct bkey_i *k = p;
726 return b->hash_val == btree_ptr_hash_val(k) ? 0 : -1;
729 static noinline void btree_bad_header(struct bch_fs *c, struct btree *b)
731 char buf1[200], buf2[100], buf3[100];
733 if (!test_bit(BCH_FS_INITIAL_GC_DONE, &c->flags))
736 bch2_bkey_val_to_text(&PBUF(buf1), c, bkey_i_to_s_c(&b->key));
737 bch2_bpos_to_text(&PBUF(buf2), b->data->min_key);
738 bch2_bpos_to_text(&PBUF(buf3), b->data->max_key);
740 bch2_fs_inconsistent(c, "btree node header doesn't match ptr\n"
741 "btree %s level %u\n"
743 "header: btree %s level %llu\n"
745 bch2_btree_ids[b->c.btree_id], b->c.level,
747 bch2_btree_ids[BTREE_NODE_ID(b->data)],
748 BTREE_NODE_LEVEL(b->data),
752 static inline void btree_check_header(struct bch_fs *c, struct btree *b)
754 if (b->c.btree_id != BTREE_NODE_ID(b->data) ||
755 b->c.level != BTREE_NODE_LEVEL(b->data) ||
756 bpos_cmp(b->data->max_key, b->key.k.p) ||
757 (b->key.k.type == KEY_TYPE_btree_ptr_v2 &&
758 bpos_cmp(b->data->min_key,
759 bkey_i_to_btree_ptr_v2(&b->key)->v.min_key)))
760 btree_bad_header(c, b);
764 * bch_btree_node_get - find a btree node in the cache and lock it, reading it
765 * in from disk if necessary.
767 * If IO is necessary and running under generic_make_request, returns -EAGAIN.
769 * The btree node will have either a read or a write lock held, depending on
770 * the @write parameter.
772 struct btree *bch2_btree_node_get(struct btree_trans *trans, struct btree_path *path,
773 const struct bkey_i *k, unsigned level,
774 enum six_lock_type lock_type,
775 unsigned long trace_ip)
777 struct bch_fs *c = trans->c;
778 struct btree_cache *bc = &c->btree_cache;
782 EBUG_ON(level >= BTREE_MAX_DEPTH);
784 b = btree_node_mem_ptr(k);
787 * Check b->hash_val _before_ calling btree_node_lock() - this might not
788 * be the node we want anymore, and trying to lock the wrong node could
789 * cause an unneccessary transaction restart:
791 if (likely(c->opts.btree_node_mem_ptr_optimization &&
793 b->hash_val == btree_ptr_hash_val(k)))
796 b = btree_cache_find(bc, k);
799 * We must have the parent locked to call bch2_btree_node_fill(),
800 * else we could read in a btree node from disk that's been
803 b = bch2_btree_node_fill(c, trans, path, k, path->btree_id,
804 level, lock_type, true);
806 /* We raced and found the btree node in the cache */
815 * There's a potential deadlock with splits and insertions into
816 * interior nodes we have to avoid:
818 * The other thread might be holding an intent lock on the node
819 * we want, and they want to update its parent node so they're
820 * going to upgrade their intent lock on the parent node to a
823 * But if we're holding a read lock on the parent, and we're
824 * trying to get the intent lock they're holding, we deadlock.
826 * So to avoid this we drop the read locks on parent nodes when
827 * we're starting to take intent locks - and handle the race.
829 * The race is that they might be about to free the node we
830 * want, and dropping our read lock on the parent node lets them
831 * update the parent marking the node we want as freed, and then
834 * To guard against this, btree nodes are evicted from the cache
835 * when they're freed - and b->hash_val is zeroed out, which we
836 * check for after we lock the node.
838 * Then, bch2_btree_node_relock() on the parent will fail - because
839 * the parent was modified, when the pointer to the node we want
840 * was removed - and we'll bail out:
842 if (btree_node_read_locked(path, level + 1))
843 btree_node_unlock(path, level + 1);
845 if (!btree_node_lock(trans, path, b, k->k.p, level, lock_type,
846 lock_node_check_fn, (void *) k, trace_ip)) {
847 if (!trans->restarted)
849 return ERR_PTR(-EINTR);
852 if (unlikely(b->hash_val != btree_ptr_hash_val(k) ||
853 b->c.level != level ||
855 six_unlock_type(&b->c.lock, lock_type);
856 if (bch2_btree_node_relock(trans, path, level + 1))
859 trace_trans_restart_btree_node_reused(trans->ip,
863 btree_trans_restart(trans);
864 return ERR_PTR(-EINTR);
868 if (unlikely(btree_node_read_in_flight(b))) {
869 u32 seq = b->c.lock.state.seq;
871 six_unlock_type(&b->c.lock, lock_type);
872 bch2_trans_unlock(trans);
874 bch2_btree_node_wait_on_read(b);
877 * should_be_locked is not set on this path yet, so we need to
878 * relock it specifically:
881 (!bch2_trans_relock(trans) ||
882 !bch2_btree_path_relock_intent(trans, path))) {
883 BUG_ON(!trans->restarted);
884 return ERR_PTR(-EINTR);
887 if (!six_relock_type(&b->c.lock, lock_type, seq))
891 prefetch(b->aux_data);
893 for_each_bset(b, t) {
894 void *p = (u64 *) b->aux_data + t->aux_data_offset;
896 prefetch(p + L1_CACHE_BYTES * 0);
897 prefetch(p + L1_CACHE_BYTES * 1);
898 prefetch(p + L1_CACHE_BYTES * 2);
901 /* avoid atomic set bit if it's not needed: */
902 if (!btree_node_accessed(b))
903 set_btree_node_accessed(b);
905 if (unlikely(btree_node_read_error(b))) {
906 six_unlock_type(&b->c.lock, lock_type);
907 return ERR_PTR(-EIO);
910 EBUG_ON(b->c.btree_id != path->btree_id);
911 EBUG_ON(BTREE_NODE_LEVEL(b->data) != level);
912 btree_check_header(c, b);
917 struct btree *bch2_btree_node_get_noiter(struct bch_fs *c,
918 const struct bkey_i *k,
919 enum btree_id btree_id,
923 struct btree_cache *bc = &c->btree_cache;
928 EBUG_ON(level >= BTREE_MAX_DEPTH);
930 if (c->opts.btree_node_mem_ptr_optimization) {
931 b = btree_node_mem_ptr(k);
936 b = btree_cache_find(bc, k);
941 b = bch2_btree_node_fill(c, NULL, NULL, k, btree_id,
942 level, SIX_LOCK_read, true);
944 /* We raced and found the btree node in the cache */
949 !bch2_btree_cache_cannibalize_lock(c, NULL))
956 ret = six_lock_read(&b->c.lock, lock_node_check_fn, (void *) k);
960 if (unlikely(b->hash_val != btree_ptr_hash_val(k) ||
961 b->c.btree_id != btree_id ||
962 b->c.level != level)) {
963 six_unlock_read(&b->c.lock);
968 /* XXX: waiting on IO with btree locks held: */
969 __bch2_btree_node_wait_on_read(b);
971 prefetch(b->aux_data);
973 for_each_bset(b, t) {
974 void *p = (u64 *) b->aux_data + t->aux_data_offset;
976 prefetch(p + L1_CACHE_BYTES * 0);
977 prefetch(p + L1_CACHE_BYTES * 1);
978 prefetch(p + L1_CACHE_BYTES * 2);
981 /* avoid atomic set bit if it's not needed: */
982 if (!btree_node_accessed(b))
983 set_btree_node_accessed(b);
985 if (unlikely(btree_node_read_error(b))) {
986 six_unlock_read(&b->c.lock);
991 EBUG_ON(b->c.btree_id != btree_id);
992 EBUG_ON(BTREE_NODE_LEVEL(b->data) != level);
993 btree_check_header(c, b);
995 bch2_btree_cache_cannibalize_unlock(c);
999 int bch2_btree_node_prefetch(struct bch_fs *c,
1000 struct btree_trans *trans,
1001 struct btree_path *path,
1002 const struct bkey_i *k,
1003 enum btree_id btree_id, unsigned level)
1005 struct btree_cache *bc = &c->btree_cache;
1008 BUG_ON(trans && !btree_node_locked(path, level + 1));
1009 BUG_ON(level >= BTREE_MAX_DEPTH);
1011 b = btree_cache_find(bc, k);
1015 b = bch2_btree_node_fill(c, trans, path, k, btree_id,
1016 level, SIX_LOCK_read, false);
1017 return PTR_ERR_OR_ZERO(b);
1020 void bch2_btree_node_evict(struct bch_fs *c, const struct bkey_i *k)
1022 struct btree_cache *bc = &c->btree_cache;
1025 b = btree_cache_find(bc, k);
1029 /* not allowed to wait on io with btree locks held: */
1031 /* XXX we're called from btree_gc which will be holding other btree
1034 __bch2_btree_node_wait_on_read(b);
1035 __bch2_btree_node_wait_on_write(b);
1037 six_lock_intent(&b->c.lock, NULL, NULL);
1038 six_lock_write(&b->c.lock, NULL, NULL);
1040 if (btree_node_dirty(b)) {
1041 __bch2_btree_node_write(c, b, false);
1042 six_unlock_write(&b->c.lock);
1043 six_unlock_intent(&b->c.lock);
1047 BUG_ON(btree_node_dirty(b));
1049 mutex_lock(&bc->lock);
1050 btree_node_data_free(c, b);
1051 bch2_btree_node_hash_remove(bc, b);
1052 mutex_unlock(&bc->lock);
1054 six_unlock_write(&b->c.lock);
1055 six_unlock_intent(&b->c.lock);
1058 void bch2_btree_node_to_text(struct printbuf *out, struct bch_fs *c,
1061 const struct bkey_format *f = &b->format;
1062 struct bset_stats stats;
1064 memset(&stats, 0, sizeof(stats));
1066 bch2_btree_keys_stats(b, &stats);
1068 pr_buf(out, "l %u ", b->c.level);
1069 bch2_bpos_to_text(out, b->data->min_key);
1071 bch2_bpos_to_text(out, b->data->max_key);
1074 bch2_val_to_text(out, c, bkey_i_to_s_c(&b->key));
1077 " format: u64s %u fields %u %u %u %u %u\n"
1078 " unpack fn len: %u\n"
1079 " bytes used %zu/%zu (%zu%% full)\n"
1080 " sib u64s: %u, %u (merge threshold %u)\n"
1081 " nr packed keys %u\n"
1082 " nr unpacked keys %u\n"
1084 " failed unpacked %zu\n",
1086 f->bits_per_field[0],
1087 f->bits_per_field[1],
1088 f->bits_per_field[2],
1089 f->bits_per_field[3],
1090 f->bits_per_field[4],
1092 b->nr.live_u64s * sizeof(u64),
1093 btree_bytes(c) - sizeof(struct btree_node),
1094 b->nr.live_u64s * 100 / btree_max_u64s(c),
1097 c->btree_foreground_merge_threshold,
1099 b->nr.unpacked_keys,
1104 void bch2_btree_cache_to_text(struct printbuf *out, struct bch_fs *c)
1106 pr_buf(out, "nr nodes:\t\t%u\n", c->btree_cache.used);
1107 pr_buf(out, "nr dirty:\t\t%u\n", atomic_read(&c->btree_cache.dirty));
1108 pr_buf(out, "cannibalize lock:\t%p\n", c->btree_cache.alloc_lock);