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 const char * const bch2_btree_node_flags[] = {
25 void bch2_recalc_btree_reserve(struct bch_fs *c)
27 unsigned i, reserve = 16;
29 if (!c->btree_roots[0].b)
32 for (i = 0; i < BTREE_ID_NR; i++)
33 if (c->btree_roots[i].b)
34 reserve += min_t(unsigned, 1,
35 c->btree_roots[i].b->c.level) * 8;
37 c->btree_cache.reserve = reserve;
40 static inline unsigned btree_cache_can_free(struct btree_cache *bc)
42 return max_t(int, 0, bc->used - bc->reserve);
45 static void btree_node_to_freedlist(struct btree_cache *bc, struct btree *b)
47 if (b->c.lock.readers)
48 list_move(&b->list, &bc->freed_pcpu);
50 list_move(&b->list, &bc->freed_nonpcpu);
53 static void btree_node_data_free(struct bch_fs *c, struct btree *b)
55 struct btree_cache *bc = &c->btree_cache;
57 EBUG_ON(btree_node_write_in_flight(b));
59 kvpfree(b->data, btree_bytes(c));
64 munmap(b->aux_data, btree_aux_data_bytes(b));
70 btree_node_to_freedlist(bc, b);
73 static int bch2_btree_cache_cmp_fn(struct rhashtable_compare_arg *arg,
76 const struct btree *b = obj;
77 const u64 *v = arg->key;
79 return b->hash_val == *v ? 0 : 1;
82 static const struct rhashtable_params bch_btree_cache_params = {
83 .head_offset = offsetof(struct btree, hash),
84 .key_offset = offsetof(struct btree, hash_val),
85 .key_len = sizeof(u64),
86 .obj_cmpfn = bch2_btree_cache_cmp_fn,
89 static int btree_node_data_alloc(struct bch_fs *c, struct btree *b, gfp_t gfp)
91 BUG_ON(b->data || b->aux_data);
93 b->data = kvpmalloc(btree_bytes(c), gfp);
97 b->aux_data = vmalloc_exec(btree_aux_data_bytes(b), gfp);
99 b->aux_data = mmap(NULL, btree_aux_data_bytes(b),
100 PROT_READ|PROT_WRITE|PROT_EXEC,
101 MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
102 if (b->aux_data == MAP_FAILED)
106 kvpfree(b->data, btree_bytes(c));
114 static struct btree *__btree_node_mem_alloc(struct bch_fs *c)
116 struct btree *b = kzalloc(sizeof(struct btree), GFP_KERNEL);
120 bkey_btree_ptr_init(&b->key);
121 __six_lock_init(&b->c.lock, "b->c.lock", &bch2_btree_node_lock_key);
122 INIT_LIST_HEAD(&b->list);
123 INIT_LIST_HEAD(&b->write_blocked);
124 b->byte_order = ilog2(btree_bytes(c));
128 struct btree *__bch2_btree_node_mem_alloc(struct bch_fs *c)
130 struct btree_cache *bc = &c->btree_cache;
131 struct btree *b = __btree_node_mem_alloc(c);
135 if (btree_node_data_alloc(c, b, GFP_KERNEL)) {
141 list_add(&b->list, &bc->freeable);
145 /* Btree in memory cache - hash table */
147 void bch2_btree_node_hash_remove(struct btree_cache *bc, struct btree *b)
149 int ret = rhashtable_remove_fast(&bc->table, &b->hash, bch_btree_cache_params);
152 /* Cause future lookups for this node to fail: */
155 six_lock_wakeup_all(&b->c.lock);
158 int __bch2_btree_node_hash_insert(struct btree_cache *bc, struct btree *b)
161 b->hash_val = btree_ptr_hash_val(&b->key);
163 return rhashtable_lookup_insert_fast(&bc->table, &b->hash,
164 bch_btree_cache_params);
167 int bch2_btree_node_hash_insert(struct btree_cache *bc, struct btree *b,
168 unsigned level, enum btree_id id)
175 mutex_lock(&bc->lock);
176 ret = __bch2_btree_node_hash_insert(bc, b);
178 list_add(&b->list, &bc->live);
179 mutex_unlock(&bc->lock);
185 static inline struct btree *btree_cache_find(struct btree_cache *bc,
186 const struct bkey_i *k)
188 u64 v = btree_ptr_hash_val(k);
190 return rhashtable_lookup_fast(&bc->table, &v, bch_btree_cache_params);
194 * this version is for btree nodes that have already been freed (we're not
195 * reaping a real btree node)
197 static int __btree_node_reclaim(struct bch_fs *c, struct btree *b, bool flush)
199 struct btree_cache *bc = &c->btree_cache;
202 lockdep_assert_held(&bc->lock);
204 if (b->flags & ((1U << BTREE_NODE_dirty)|
205 (1U << BTREE_NODE_read_in_flight)|
206 (1U << BTREE_NODE_write_in_flight))) {
210 /* XXX: waiting on IO with btree cache lock held */
211 bch2_btree_node_wait_on_read(b);
212 bch2_btree_node_wait_on_write(b);
215 if (!six_trylock_intent(&b->c.lock))
218 if (!six_trylock_write(&b->c.lock))
219 goto out_unlock_intent;
221 /* recheck under lock */
222 if (b->flags & ((1U << BTREE_NODE_read_in_flight)|
223 (1U << BTREE_NODE_write_in_flight))) {
226 six_unlock_write(&b->c.lock);
227 six_unlock_intent(&b->c.lock);
231 if (btree_node_noevict(b) ||
232 btree_node_write_blocked(b) ||
233 btree_node_will_make_reachable(b))
236 if (btree_node_dirty(b)) {
240 * Using the underscore version because we don't want to compact
241 * bsets after the write, since this node is about to be evicted
242 * - unless btree verify mode is enabled, since it runs out of
243 * the post write cleanup:
245 if (bch2_verify_btree_ondisk)
246 bch2_btree_node_write(c, b, SIX_LOCK_intent, 0);
248 __bch2_btree_node_write(c, b, 0);
250 six_unlock_write(&b->c.lock);
251 six_unlock_intent(&b->c.lock);
255 if (b->hash_val && !ret)
256 trace_btree_node_reap(c, b);
259 six_unlock_write(&b->c.lock);
261 six_unlock_intent(&b->c.lock);
266 static int btree_node_reclaim(struct bch_fs *c, struct btree *b)
268 return __btree_node_reclaim(c, b, false);
271 static int btree_node_write_and_reclaim(struct bch_fs *c, struct btree *b)
273 return __btree_node_reclaim(c, b, true);
276 static unsigned long bch2_btree_cache_scan(struct shrinker *shrink,
277 struct shrink_control *sc)
279 struct bch_fs *c = container_of(shrink, struct bch_fs,
281 struct btree_cache *bc = &c->btree_cache;
283 unsigned long nr = sc->nr_to_scan;
284 unsigned long can_free = 0;
285 unsigned long touched = 0;
286 unsigned long freed = 0;
288 unsigned long ret = SHRINK_STOP;
290 if (bch2_btree_shrinker_disabled)
293 /* Return -1 if we can't do anything right now */
294 if (sc->gfp_mask & __GFP_FS)
295 mutex_lock(&bc->lock);
296 else if (!mutex_trylock(&bc->lock))
299 flags = memalloc_nofs_save();
302 * It's _really_ critical that we don't free too many btree nodes - we
303 * have to always leave ourselves a reserve. The reserve is how we
304 * guarantee that allocating memory for a new btree node can always
305 * succeed, so that inserting keys into the btree can always succeed and
306 * IO can always make forward progress:
308 can_free = btree_cache_can_free(bc);
309 nr = min_t(unsigned long, nr, can_free);
312 list_for_each_entry_safe(b, t, &bc->freeable, list) {
314 * Leave a few nodes on the freeable list, so that a btree split
315 * won't have to hit the system allocator:
325 if (!btree_node_reclaim(c, b)) {
326 btree_node_data_free(c, b);
327 six_unlock_write(&b->c.lock);
328 six_unlock_intent(&b->c.lock);
333 list_for_each_entry_safe(b, t, &bc->live, list) {
335 if (btree_node_accessed(b)) {
336 clear_btree_node_accessed(b);
340 if (!btree_node_reclaim(c, b)) {
341 /* can't call bch2_btree_node_hash_remove under lock */
343 if (&t->list != &bc->live)
344 list_move_tail(&bc->live, &t->list);
346 btree_node_data_free(c, b);
347 mutex_unlock(&bc->lock);
349 bch2_btree_node_hash_remove(bc, b);
350 six_unlock_write(&b->c.lock);
351 six_unlock_intent(&b->c.lock);
356 if (sc->gfp_mask & __GFP_FS)
357 mutex_lock(&bc->lock);
358 else if (!mutex_trylock(&bc->lock))
369 if (&t->list != &bc->live)
370 list_move_tail(&bc->live, &t->list);
375 mutex_unlock(&bc->lock);
378 memalloc_nofs_restore(flags);
380 trace_btree_cache_scan(sc->nr_to_scan, can_free, ret);
384 static unsigned long bch2_btree_cache_count(struct shrinker *shrink,
385 struct shrink_control *sc)
387 struct bch_fs *c = container_of(shrink, struct bch_fs,
389 struct btree_cache *bc = &c->btree_cache;
391 if (bch2_btree_shrinker_disabled)
394 return btree_cache_can_free(bc);
397 void bch2_fs_btree_cache_exit(struct bch_fs *c)
399 struct btree_cache *bc = &c->btree_cache;
403 if (bc->shrink.list.next)
404 unregister_shrinker(&bc->shrink);
406 /* vfree() can allocate memory: */
407 flags = memalloc_nofs_save();
408 mutex_lock(&bc->lock);
411 list_move(&c->verify_data->list, &bc->live);
413 kvpfree(c->verify_ondisk, btree_bytes(c));
415 for (i = 0; i < BTREE_ID_NR; i++)
416 if (c->btree_roots[i].b)
417 list_add(&c->btree_roots[i].b->list, &bc->live);
419 list_splice(&bc->freeable, &bc->live);
421 while (!list_empty(&bc->live)) {
422 b = list_first_entry(&bc->live, struct btree, list);
424 BUG_ON(btree_node_read_in_flight(b) ||
425 btree_node_write_in_flight(b));
427 if (btree_node_dirty(b))
428 bch2_btree_complete_write(c, b, btree_current_write(b));
429 clear_btree_node_dirty_acct(c, b);
431 btree_node_data_free(c, b);
434 BUG_ON(atomic_read(&c->btree_cache.dirty));
436 list_splice(&bc->freed_pcpu, &bc->freed_nonpcpu);
438 while (!list_empty(&bc->freed_nonpcpu)) {
439 b = list_first_entry(&bc->freed_nonpcpu, struct btree, list);
441 six_lock_pcpu_free(&b->c.lock);
445 mutex_unlock(&bc->lock);
446 memalloc_nofs_restore(flags);
448 if (bc->table_init_done)
449 rhashtable_destroy(&bc->table);
452 int bch2_fs_btree_cache_init(struct bch_fs *c)
454 struct btree_cache *bc = &c->btree_cache;
458 pr_verbose_init(c->opts, "");
460 ret = rhashtable_init(&bc->table, &bch_btree_cache_params);
464 bc->table_init_done = true;
466 bch2_recalc_btree_reserve(c);
468 for (i = 0; i < bc->reserve; i++)
469 if (!__bch2_btree_node_mem_alloc(c)) {
474 list_splice_init(&bc->live, &bc->freeable);
476 mutex_init(&c->verify_lock);
478 bc->shrink.count_objects = bch2_btree_cache_count;
479 bc->shrink.scan_objects = bch2_btree_cache_scan;
480 bc->shrink.seeks = 4;
481 ret = register_shrinker(&bc->shrink);
483 pr_verbose_init(c->opts, "ret %i", ret);
487 void bch2_fs_btree_cache_init_early(struct btree_cache *bc)
489 mutex_init(&bc->lock);
490 INIT_LIST_HEAD(&bc->live);
491 INIT_LIST_HEAD(&bc->freeable);
492 INIT_LIST_HEAD(&bc->freed_pcpu);
493 INIT_LIST_HEAD(&bc->freed_nonpcpu);
497 * We can only have one thread cannibalizing other cached btree nodes at a time,
498 * or we'll deadlock. We use an open coded mutex to ensure that, which a
499 * cannibalize_bucket() will take. This means every time we unlock the root of
500 * the btree, we need to release this lock if we have it held.
502 void bch2_btree_cache_cannibalize_unlock(struct bch_fs *c)
504 struct btree_cache *bc = &c->btree_cache;
506 if (bc->alloc_lock == current) {
507 trace_btree_node_cannibalize_unlock(c);
508 bc->alloc_lock = NULL;
509 closure_wake_up(&bc->alloc_wait);
513 int bch2_btree_cache_cannibalize_lock(struct bch_fs *c, struct closure *cl)
515 struct btree_cache *bc = &c->btree_cache;
516 struct task_struct *old;
518 old = cmpxchg(&bc->alloc_lock, NULL, current);
519 if (old == NULL || old == current)
523 trace_btree_node_cannibalize_lock_fail(c);
527 closure_wait(&bc->alloc_wait, cl);
529 /* Try again, after adding ourselves to waitlist */
530 old = cmpxchg(&bc->alloc_lock, NULL, current);
531 if (old == NULL || old == current) {
533 closure_wake_up(&bc->alloc_wait);
537 trace_btree_node_cannibalize_lock_fail(c);
541 trace_btree_node_cannibalize_lock(c);
545 static struct btree *btree_node_cannibalize(struct bch_fs *c)
547 struct btree_cache *bc = &c->btree_cache;
550 list_for_each_entry_reverse(b, &bc->live, list)
551 if (!btree_node_reclaim(c, b))
555 list_for_each_entry_reverse(b, &bc->live, list)
556 if (!btree_node_write_and_reclaim(c, b))
560 * Rare case: all nodes were intent-locked.
563 WARN_ONCE(1, "btree cache cannibalize failed\n");
568 struct btree *bch2_btree_node_mem_alloc(struct bch_fs *c, bool pcpu_read_locks)
570 struct btree_cache *bc = &c->btree_cache;
571 struct list_head *freed = pcpu_read_locks
573 : &bc->freed_nonpcpu;
574 struct btree *b, *b2;
575 u64 start_time = local_clock();
578 flags = memalloc_nofs_save();
579 mutex_lock(&bc->lock);
582 * We never free struct btree itself, just the memory that holds the on
583 * disk node. Check the freed list before allocating a new one:
585 list_for_each_entry(b, freed, list)
586 if (!btree_node_reclaim(c, b)) {
587 list_del_init(&b->list);
591 b = __btree_node_mem_alloc(c);
596 six_lock_pcpu_alloc(&b->c.lock);
598 BUG_ON(!six_trylock_intent(&b->c.lock));
599 BUG_ON(!six_trylock_write(&b->c.lock));
603 * btree_free() doesn't free memory; it sticks the node on the end of
604 * the list. Check if there's any freed nodes there:
606 list_for_each_entry(b2, &bc->freeable, list)
607 if (!btree_node_reclaim(c, b2)) {
608 swap(b->data, b2->data);
609 swap(b->aux_data, b2->aux_data);
610 btree_node_to_freedlist(bc, b2);
611 six_unlock_write(&b2->c.lock);
612 six_unlock_intent(&b2->c.lock);
616 mutex_unlock(&bc->lock);
618 if (btree_node_data_alloc(c, b, __GFP_NOWARN|GFP_KERNEL))
621 mutex_lock(&bc->lock);
624 mutex_unlock(&bc->lock);
626 BUG_ON(btree_node_hashed(b));
627 BUG_ON(btree_node_dirty(b));
628 BUG_ON(btree_node_write_in_flight(b));
635 b->whiteout_u64s = 0;
636 bch2_btree_keys_init(b);
637 set_btree_node_accessed(b);
639 bch2_time_stats_update(&c->times[BCH_TIME_btree_node_mem_alloc],
642 memalloc_nofs_restore(flags);
645 mutex_lock(&bc->lock);
647 /* Try to cannibalize another cached btree node: */
648 if (bc->alloc_lock == current) {
649 b2 = btree_node_cannibalize(c);
650 bch2_btree_node_hash_remove(bc, b2);
653 swap(b->data, b2->data);
654 swap(b->aux_data, b2->aux_data);
655 btree_node_to_freedlist(bc, b2);
656 six_unlock_write(&b2->c.lock);
657 six_unlock_intent(&b2->c.lock);
660 list_del_init(&b->list);
663 mutex_unlock(&bc->lock);
665 trace_btree_node_cannibalize(c);
669 mutex_unlock(&bc->lock);
670 memalloc_nofs_restore(flags);
671 return ERR_PTR(-ENOMEM);
674 /* Slowpath, don't want it inlined into btree_iter_traverse() */
675 static noinline struct btree *bch2_btree_node_fill(struct bch_fs *c,
676 struct btree_trans *trans,
677 struct btree_path *path,
678 const struct bkey_i *k,
679 enum btree_id btree_id,
681 enum six_lock_type lock_type,
684 struct btree_cache *bc = &c->btree_cache;
688 BUG_ON(level + 1 >= BTREE_MAX_DEPTH);
690 * Parent node must be locked, else we could read in a btree node that's
693 if (trans && !bch2_btree_node_relock(trans, path, level + 1)) {
694 trace_trans_restart_relock_parent_for_fill(trans->fn,
695 _THIS_IP_, btree_id, &path->pos);
696 btree_trans_restart(trans);
697 return ERR_PTR(-EINTR);
700 b = bch2_btree_node_mem_alloc(c, level != 0);
702 if (trans && b == ERR_PTR(-ENOMEM)) {
703 trans->memory_allocation_failure = true;
704 trace_trans_restart_memory_allocation_failure(trans->fn,
705 _THIS_IP_, btree_id, &path->pos);
706 btree_trans_restart(trans);
707 return ERR_PTR(-EINTR);
713 bkey_copy(&b->key, k);
714 if (bch2_btree_node_hash_insert(bc, b, level, btree_id)) {
715 /* raced with another fill: */
717 /* mark as unhashed... */
720 mutex_lock(&bc->lock);
721 list_add(&b->list, &bc->freeable);
722 mutex_unlock(&bc->lock);
724 six_unlock_write(&b->c.lock);
725 six_unlock_intent(&b->c.lock);
729 set_btree_node_read_in_flight(b);
731 six_unlock_write(&b->c.lock);
732 seq = b->c.lock.state.seq;
733 six_unlock_intent(&b->c.lock);
735 /* Unlock before doing IO: */
737 bch2_trans_unlock(trans);
739 bch2_btree_node_read(c, b, sync);
745 (!bch2_trans_relock(trans) ||
746 !bch2_btree_path_relock_intent(trans, path))) {
747 BUG_ON(!trans->restarted);
748 return ERR_PTR(-EINTR);
751 if (!six_relock_type(&b->c.lock, lock_type, seq)) {
752 trace_trans_restart_relock_after_fill(trans->fn, _THIS_IP_,
753 btree_id, &path->pos);
754 btree_trans_restart(trans);
755 return ERR_PTR(-EINTR);
761 static int lock_node_check_fn(struct six_lock *lock, void *p)
763 struct btree *b = container_of(lock, struct btree, c.lock);
764 const struct bkey_i *k = p;
766 return b->hash_val == btree_ptr_hash_val(k) ? 0 : -1;
769 static noinline void btree_bad_header(struct bch_fs *c, struct btree *b)
771 struct printbuf buf1 = PRINTBUF;
772 struct printbuf buf2 = PRINTBUF;
773 struct printbuf buf3 = PRINTBUF;
775 if (!test_bit(BCH_FS_INITIAL_GC_DONE, &c->flags))
778 bch2_bkey_val_to_text(&buf1, c, bkey_i_to_s_c(&b->key));
779 bch2_bpos_to_text(&buf2, b->data->min_key);
780 bch2_bpos_to_text(&buf3, b->data->max_key);
782 bch2_fs_inconsistent(c, "btree node header doesn't match ptr\n"
783 "btree %s level %u\n"
785 "header: btree %s level %llu\n"
787 bch2_btree_ids[b->c.btree_id], b->c.level,
789 bch2_btree_ids[BTREE_NODE_ID(b->data)],
790 BTREE_NODE_LEVEL(b->data),
793 printbuf_exit(&buf3);
794 printbuf_exit(&buf2);
795 printbuf_exit(&buf1);
798 static inline void btree_check_header(struct bch_fs *c, struct btree *b)
800 if (b->c.btree_id != BTREE_NODE_ID(b->data) ||
801 b->c.level != BTREE_NODE_LEVEL(b->data) ||
802 bpos_cmp(b->data->max_key, b->key.k.p) ||
803 (b->key.k.type == KEY_TYPE_btree_ptr_v2 &&
804 bpos_cmp(b->data->min_key,
805 bkey_i_to_btree_ptr_v2(&b->key)->v.min_key)))
806 btree_bad_header(c, b);
810 * bch_btree_node_get - find a btree node in the cache and lock it, reading it
811 * in from disk if necessary.
813 * If IO is necessary and running under generic_make_request, returns -EAGAIN.
815 * The btree node will have either a read or a write lock held, depending on
816 * the @write parameter.
818 struct btree *bch2_btree_node_get(struct btree_trans *trans, struct btree_path *path,
819 const struct bkey_i *k, unsigned level,
820 enum six_lock_type lock_type,
821 unsigned long trace_ip)
823 struct bch_fs *c = trans->c;
824 struct btree_cache *bc = &c->btree_cache;
828 EBUG_ON(level >= BTREE_MAX_DEPTH);
830 b = btree_node_mem_ptr(k);
833 * Check b->hash_val _before_ calling btree_node_lock() - this might not
834 * be the node we want anymore, and trying to lock the wrong node could
835 * cause an unneccessary transaction restart:
837 if (likely(c->opts.btree_node_mem_ptr_optimization &&
839 b->hash_val == btree_ptr_hash_val(k)))
842 b = btree_cache_find(bc, k);
845 * We must have the parent locked to call bch2_btree_node_fill(),
846 * else we could read in a btree node from disk that's been
849 b = bch2_btree_node_fill(c, trans, path, k, path->btree_id,
850 level, lock_type, true);
852 /* We raced and found the btree node in the cache */
861 * There's a potential deadlock with splits and insertions into
862 * interior nodes we have to avoid:
864 * The other thread might be holding an intent lock on the node
865 * we want, and they want to update its parent node so they're
866 * going to upgrade their intent lock on the parent node to a
869 * But if we're holding a read lock on the parent, and we're
870 * trying to get the intent lock they're holding, we deadlock.
872 * So to avoid this we drop the read locks on parent nodes when
873 * we're starting to take intent locks - and handle the race.
875 * The race is that they might be about to free the node we
876 * want, and dropping our read lock on the parent node lets them
877 * update the parent marking the node we want as freed, and then
880 * To guard against this, btree nodes are evicted from the cache
881 * when they're freed - and b->hash_val is zeroed out, which we
882 * check for after we lock the node.
884 * Then, bch2_btree_node_relock() on the parent will fail - because
885 * the parent was modified, when the pointer to the node we want
886 * was removed - and we'll bail out:
888 if (btree_node_read_locked(path, level + 1))
889 btree_node_unlock(path, level + 1);
891 if (!btree_node_lock(trans, path, b, k->k.p, level, lock_type,
892 lock_node_check_fn, (void *) k, trace_ip)) {
893 if (!trans->restarted)
895 return ERR_PTR(-EINTR);
898 if (unlikely(b->hash_val != btree_ptr_hash_val(k) ||
899 b->c.level != level ||
901 six_unlock_type(&b->c.lock, lock_type);
902 if (bch2_btree_node_relock(trans, path, level + 1))
905 trace_trans_restart_btree_node_reused(trans->fn,
909 btree_trans_restart(trans);
910 return ERR_PTR(-EINTR);
914 if (unlikely(btree_node_read_in_flight(b))) {
915 u32 seq = b->c.lock.state.seq;
917 six_unlock_type(&b->c.lock, lock_type);
918 bch2_trans_unlock(trans);
920 bch2_btree_node_wait_on_read(b);
923 * should_be_locked is not set on this path yet, so we need to
924 * relock it specifically:
927 (!bch2_trans_relock(trans) ||
928 !bch2_btree_path_relock_intent(trans, path))) {
929 BUG_ON(!trans->restarted);
930 return ERR_PTR(-EINTR);
933 if (!six_relock_type(&b->c.lock, lock_type, seq))
937 prefetch(b->aux_data);
939 for_each_bset(b, t) {
940 void *p = (u64 *) b->aux_data + t->aux_data_offset;
942 prefetch(p + L1_CACHE_BYTES * 0);
943 prefetch(p + L1_CACHE_BYTES * 1);
944 prefetch(p + L1_CACHE_BYTES * 2);
947 /* avoid atomic set bit if it's not needed: */
948 if (!btree_node_accessed(b))
949 set_btree_node_accessed(b);
951 if (unlikely(btree_node_read_error(b))) {
952 six_unlock_type(&b->c.lock, lock_type);
953 return ERR_PTR(-EIO);
956 EBUG_ON(b->c.btree_id != path->btree_id);
957 EBUG_ON(BTREE_NODE_LEVEL(b->data) != level);
958 btree_check_header(c, b);
963 struct btree *bch2_btree_node_get_noiter(struct bch_fs *c,
964 const struct bkey_i *k,
965 enum btree_id btree_id,
969 struct btree_cache *bc = &c->btree_cache;
974 EBUG_ON(level >= BTREE_MAX_DEPTH);
976 if (c->opts.btree_node_mem_ptr_optimization) {
977 b = btree_node_mem_ptr(k);
982 b = btree_cache_find(bc, k);
987 b = bch2_btree_node_fill(c, NULL, NULL, k, btree_id,
988 level, SIX_LOCK_read, true);
990 /* We raced and found the btree node in the cache */
995 !bch2_btree_cache_cannibalize_lock(c, NULL))
1002 ret = six_lock_read(&b->c.lock, lock_node_check_fn, (void *) k);
1006 if (unlikely(b->hash_val != btree_ptr_hash_val(k) ||
1007 b->c.btree_id != btree_id ||
1008 b->c.level != level)) {
1009 six_unlock_read(&b->c.lock);
1014 /* XXX: waiting on IO with btree locks held: */
1015 __bch2_btree_node_wait_on_read(b);
1017 prefetch(b->aux_data);
1019 for_each_bset(b, t) {
1020 void *p = (u64 *) b->aux_data + t->aux_data_offset;
1022 prefetch(p + L1_CACHE_BYTES * 0);
1023 prefetch(p + L1_CACHE_BYTES * 1);
1024 prefetch(p + L1_CACHE_BYTES * 2);
1027 /* avoid atomic set bit if it's not needed: */
1028 if (!btree_node_accessed(b))
1029 set_btree_node_accessed(b);
1031 if (unlikely(btree_node_read_error(b))) {
1032 six_unlock_read(&b->c.lock);
1037 EBUG_ON(b->c.btree_id != btree_id);
1038 EBUG_ON(BTREE_NODE_LEVEL(b->data) != level);
1039 btree_check_header(c, b);
1041 bch2_btree_cache_cannibalize_unlock(c);
1045 int bch2_btree_node_prefetch(struct bch_fs *c,
1046 struct btree_trans *trans,
1047 struct btree_path *path,
1048 const struct bkey_i *k,
1049 enum btree_id btree_id, unsigned level)
1051 struct btree_cache *bc = &c->btree_cache;
1054 BUG_ON(trans && !btree_node_locked(path, level + 1));
1055 BUG_ON(level >= BTREE_MAX_DEPTH);
1057 b = btree_cache_find(bc, k);
1061 b = bch2_btree_node_fill(c, trans, path, k, btree_id,
1062 level, SIX_LOCK_read, false);
1063 return PTR_ERR_OR_ZERO(b);
1066 void bch2_btree_node_evict(struct bch_fs *c, const struct bkey_i *k)
1068 struct btree_cache *bc = &c->btree_cache;
1071 b = btree_cache_find(bc, k);
1075 /* not allowed to wait on io with btree locks held: */
1077 /* XXX we're called from btree_gc which will be holding other btree
1080 __bch2_btree_node_wait_on_read(b);
1081 __bch2_btree_node_wait_on_write(b);
1083 six_lock_intent(&b->c.lock, NULL, NULL);
1084 six_lock_write(&b->c.lock, NULL, NULL);
1086 if (btree_node_dirty(b)) {
1087 __bch2_btree_node_write(c, b, 0);
1088 six_unlock_write(&b->c.lock);
1089 six_unlock_intent(&b->c.lock);
1093 BUG_ON(btree_node_dirty(b));
1095 mutex_lock(&bc->lock);
1096 btree_node_data_free(c, b);
1097 bch2_btree_node_hash_remove(bc, b);
1098 mutex_unlock(&bc->lock);
1100 six_unlock_write(&b->c.lock);
1101 six_unlock_intent(&b->c.lock);
1104 void bch2_btree_node_to_text(struct printbuf *out, struct bch_fs *c,
1107 const struct bkey_format *f = &b->format;
1108 struct bset_stats stats;
1110 memset(&stats, 0, sizeof(stats));
1112 bch2_btree_keys_stats(b, &stats);
1114 pr_buf(out, "l %u ", b->c.level);
1115 bch2_bpos_to_text(out, b->data->min_key);
1117 bch2_bpos_to_text(out, b->data->max_key);
1120 bch2_val_to_text(out, c, bkey_i_to_s_c(&b->key));
1123 " format: u64s %u fields %u %u %u %u %u\n"
1124 " unpack fn len: %u\n"
1125 " bytes used %zu/%zu (%zu%% full)\n"
1126 " sib u64s: %u, %u (merge threshold %u)\n"
1127 " nr packed keys %u\n"
1128 " nr unpacked keys %u\n"
1130 " failed unpacked %zu\n",
1132 f->bits_per_field[0],
1133 f->bits_per_field[1],
1134 f->bits_per_field[2],
1135 f->bits_per_field[3],
1136 f->bits_per_field[4],
1138 b->nr.live_u64s * sizeof(u64),
1139 btree_bytes(c) - sizeof(struct btree_node),
1140 b->nr.live_u64s * 100 / btree_max_u64s(c),
1143 c->btree_foreground_merge_threshold,
1145 b->nr.unpacked_keys,
1150 void bch2_btree_cache_to_text(struct printbuf *out, struct bch_fs *c)
1152 pr_buf(out, "nr nodes:\t\t%u\n", c->btree_cache.used);
1153 pr_buf(out, "nr dirty:\t\t%u\n", atomic_read(&c->btree_cache.dirty));
1154 pr_buf(out, "cannibalize lock:\t%p\n", c->btree_cache.alloc_lock);