1 // SPDX-License-Identifier: GPL-2.0
5 #include "btree_cache.h"
7 #include "btree_iter.h"
8 #include "btree_locking.h"
13 #include <linux/prefetch.h>
14 #include <linux/sched/mm.h>
15 #include <trace/events/bcachefs.h>
17 const char * const bch2_btree_node_flags[] = {
24 void bch2_recalc_btree_reserve(struct bch_fs *c)
26 unsigned i, reserve = 16;
28 if (!c->btree_roots[0].b)
31 for (i = 0; i < BTREE_ID_NR; i++)
32 if (c->btree_roots[i].b)
33 reserve += min_t(unsigned, 1,
34 c->btree_roots[i].b->c.level) * 8;
36 c->btree_cache.reserve = reserve;
39 static inline unsigned btree_cache_can_free(struct btree_cache *bc)
41 return max_t(int, 0, bc->used - bc->reserve);
44 static void btree_node_to_freedlist(struct btree_cache *bc, struct btree *b)
46 if (b->c.lock.readers)
47 list_move(&b->list, &bc->freed_pcpu);
49 list_move(&b->list, &bc->freed_nonpcpu);
52 static void btree_node_data_free(struct bch_fs *c, struct btree *b)
54 struct btree_cache *bc = &c->btree_cache;
56 EBUG_ON(btree_node_write_in_flight(b));
58 kvpfree(b->data, btree_bytes(c));
63 munmap(b->aux_data, btree_aux_data_bytes(b));
69 btree_node_to_freedlist(bc, b);
72 static int bch2_btree_cache_cmp_fn(struct rhashtable_compare_arg *arg,
75 const struct btree *b = obj;
76 const u64 *v = arg->key;
78 return b->hash_val == *v ? 0 : 1;
81 static const struct rhashtable_params bch_btree_cache_params = {
82 .head_offset = offsetof(struct btree, hash),
83 .key_offset = offsetof(struct btree, hash_val),
84 .key_len = sizeof(u64),
85 .obj_cmpfn = bch2_btree_cache_cmp_fn,
88 static int btree_node_data_alloc(struct bch_fs *c, struct btree *b, gfp_t gfp)
90 BUG_ON(b->data || b->aux_data);
92 b->data = kvpmalloc(btree_bytes(c), gfp);
96 b->aux_data = vmalloc_exec(btree_aux_data_bytes(b), gfp);
98 b->aux_data = mmap(NULL, btree_aux_data_bytes(b),
99 PROT_READ|PROT_WRITE|PROT_EXEC,
100 MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
101 if (b->aux_data == MAP_FAILED)
105 kvpfree(b->data, btree_bytes(c));
113 static struct btree *__btree_node_mem_alloc(struct bch_fs *c, gfp_t gfp)
115 struct btree *b = kzalloc(sizeof(struct btree), gfp);
119 bkey_btree_ptr_init(&b->key);
120 __six_lock_init(&b->c.lock, "b->c.lock", &bch2_btree_node_lock_key);
121 #ifdef CONFIG_DEBUG_LOCK_ALLOC
122 lockdep_set_no_check_recursion(&b->c.lock.dep_map);
124 INIT_LIST_HEAD(&b->list);
125 INIT_LIST_HEAD(&b->write_blocked);
126 b->byte_order = ilog2(btree_bytes(c));
130 struct btree *__bch2_btree_node_mem_alloc(struct bch_fs *c)
132 struct btree_cache *bc = &c->btree_cache;
133 struct btree *b = __btree_node_mem_alloc(c, GFP_KERNEL);
137 if (btree_node_data_alloc(c, b, GFP_KERNEL)) {
143 list_add(&b->list, &bc->freeable);
147 /* Btree in memory cache - hash table */
149 void bch2_btree_node_hash_remove(struct btree_cache *bc, struct btree *b)
151 int ret = rhashtable_remove_fast(&bc->table, &b->hash, bch_btree_cache_params);
154 /* Cause future lookups for this node to fail: */
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_and_count(c, btree_cache_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 freed = 0;
286 unsigned long touched = 0;
288 unsigned long ret = SHRINK_STOP;
289 bool trigger_writes = atomic_read(&bc->dirty) + nr >=
292 if (bch2_btree_shrinker_disabled)
295 mutex_lock(&bc->lock);
296 flags = memalloc_nofs_save();
299 * It's _really_ critical that we don't free too many btree nodes - we
300 * have to always leave ourselves a reserve. The reserve is how we
301 * guarantee that allocating memory for a new btree node can always
302 * succeed, so that inserting keys into the btree can always succeed and
303 * IO can always make forward progress:
305 can_free = btree_cache_can_free(bc);
306 nr = min_t(unsigned long, nr, can_free);
309 list_for_each_entry_safe(b, t, &bc->freeable, list) {
311 * Leave a few nodes on the freeable list, so that a btree split
312 * won't have to hit the system allocator:
322 if (!btree_node_reclaim(c, b)) {
323 btree_node_data_free(c, b);
324 six_unlock_write(&b->c.lock);
325 six_unlock_intent(&b->c.lock);
330 list_for_each_entry_safe(b, t, &bc->live, list) {
333 if (btree_node_accessed(b)) {
334 clear_btree_node_accessed(b);
335 } else if (!btree_node_reclaim(c, b)) {
337 btree_node_data_free(c, b);
339 bch2_btree_node_hash_remove(bc, b);
340 six_unlock_write(&b->c.lock);
341 six_unlock_intent(&b->c.lock);
345 } else if (trigger_writes &&
346 btree_node_dirty(b) &&
347 !btree_node_will_make_reachable(b) &&
348 !btree_node_write_blocked(b) &&
349 six_trylock_read(&b->c.lock)) {
350 list_move(&bc->live, &b->list);
351 mutex_unlock(&bc->lock);
352 __bch2_btree_node_write(c, b, 0);
353 six_unlock_read(&b->c.lock);
356 mutex_lock(&bc->lock);
364 mutex_unlock(&bc->lock);
367 memalloc_nofs_restore(flags);
368 trace_and_count(c, btree_cache_scan, sc->nr_to_scan, can_free, ret);
372 static unsigned long bch2_btree_cache_count(struct shrinker *shrink,
373 struct shrink_control *sc)
375 struct bch_fs *c = container_of(shrink, struct bch_fs,
377 struct btree_cache *bc = &c->btree_cache;
379 if (bch2_btree_shrinker_disabled)
382 return btree_cache_can_free(bc);
385 static void bch2_btree_cache_shrinker_to_text(struct printbuf *out, struct shrinker *shrink)
387 struct bch_fs *c = container_of(shrink, struct bch_fs,
390 bch2_btree_cache_to_text(out, c);
393 void bch2_fs_btree_cache_exit(struct bch_fs *c)
395 struct btree_cache *bc = &c->btree_cache;
399 if (bc->shrink.list.next)
400 unregister_shrinker(&bc->shrink);
402 /* vfree() can allocate memory: */
403 flags = memalloc_nofs_save();
404 mutex_lock(&bc->lock);
407 list_move(&c->verify_data->list, &bc->live);
409 kvpfree(c->verify_ondisk, btree_bytes(c));
411 for (i = 0; i < BTREE_ID_NR; i++)
412 if (c->btree_roots[i].b)
413 list_add(&c->btree_roots[i].b->list, &bc->live);
415 list_splice(&bc->freeable, &bc->live);
417 while (!list_empty(&bc->live)) {
418 b = list_first_entry(&bc->live, struct btree, list);
420 BUG_ON(btree_node_read_in_flight(b) ||
421 btree_node_write_in_flight(b));
423 if (btree_node_dirty(b))
424 bch2_btree_complete_write(c, b, btree_current_write(b));
425 clear_btree_node_dirty_acct(c, b);
427 btree_node_data_free(c, b);
430 BUG_ON(atomic_read(&c->btree_cache.dirty));
432 list_splice(&bc->freed_pcpu, &bc->freed_nonpcpu);
434 while (!list_empty(&bc->freed_nonpcpu)) {
435 b = list_first_entry(&bc->freed_nonpcpu, struct btree, list);
437 six_lock_pcpu_free(&b->c.lock);
441 mutex_unlock(&bc->lock);
442 memalloc_nofs_restore(flags);
444 if (bc->table_init_done)
445 rhashtable_destroy(&bc->table);
448 int bch2_fs_btree_cache_init(struct bch_fs *c)
450 struct btree_cache *bc = &c->btree_cache;
454 pr_verbose_init(c->opts, "");
456 ret = rhashtable_init(&bc->table, &bch_btree_cache_params);
460 bc->table_init_done = true;
462 bch2_recalc_btree_reserve(c);
464 for (i = 0; i < bc->reserve; i++)
465 if (!__bch2_btree_node_mem_alloc(c)) {
470 list_splice_init(&bc->live, &bc->freeable);
472 mutex_init(&c->verify_lock);
474 bc->shrink.count_objects = bch2_btree_cache_count;
475 bc->shrink.scan_objects = bch2_btree_cache_scan;
476 bc->shrink.to_text = bch2_btree_cache_shrinker_to_text;
477 bc->shrink.seeks = 4;
478 ret = register_shrinker(&bc->shrink);
480 pr_verbose_init(c->opts, "ret %i", ret);
484 void bch2_fs_btree_cache_init_early(struct btree_cache *bc)
486 mutex_init(&bc->lock);
487 INIT_LIST_HEAD(&bc->live);
488 INIT_LIST_HEAD(&bc->freeable);
489 INIT_LIST_HEAD(&bc->freed_pcpu);
490 INIT_LIST_HEAD(&bc->freed_nonpcpu);
494 * We can only have one thread cannibalizing other cached btree nodes at a time,
495 * or we'll deadlock. We use an open coded mutex to ensure that, which a
496 * cannibalize_bucket() will take. This means every time we unlock the root of
497 * the btree, we need to release this lock if we have it held.
499 void bch2_btree_cache_cannibalize_unlock(struct bch_fs *c)
501 struct btree_cache *bc = &c->btree_cache;
503 if (bc->alloc_lock == current) {
504 trace_and_count(c, btree_cache_cannibalize_unlock, c);
505 bc->alloc_lock = NULL;
506 closure_wake_up(&bc->alloc_wait);
510 int bch2_btree_cache_cannibalize_lock(struct bch_fs *c, struct closure *cl)
512 struct btree_cache *bc = &c->btree_cache;
513 struct task_struct *old;
515 old = cmpxchg(&bc->alloc_lock, NULL, current);
516 if (old == NULL || old == current)
520 trace_and_count(c, btree_cache_cannibalize_lock_fail, c);
524 closure_wait(&bc->alloc_wait, cl);
526 /* Try again, after adding ourselves to waitlist */
527 old = cmpxchg(&bc->alloc_lock, NULL, current);
528 if (old == NULL || old == current) {
530 closure_wake_up(&bc->alloc_wait);
534 trace_and_count(c, btree_cache_cannibalize_lock_fail, c);
538 trace_and_count(c, btree_cache_cannibalize_lock, c);
542 static struct btree *btree_node_cannibalize(struct bch_fs *c)
544 struct btree_cache *bc = &c->btree_cache;
547 list_for_each_entry_reverse(b, &bc->live, list)
548 if (!btree_node_reclaim(c, b))
552 list_for_each_entry_reverse(b, &bc->live, list)
553 if (!btree_node_write_and_reclaim(c, b))
557 * Rare case: all nodes were intent-locked.
560 WARN_ONCE(1, "btree cache cannibalize failed\n");
565 struct btree *bch2_btree_node_mem_alloc(struct bch_fs *c, bool pcpu_read_locks)
567 struct btree_cache *bc = &c->btree_cache;
568 struct list_head *freed = pcpu_read_locks
570 : &bc->freed_nonpcpu;
571 struct btree *b, *b2;
572 u64 start_time = local_clock();
575 flags = memalloc_nofs_save();
576 mutex_lock(&bc->lock);
579 * We never free struct btree itself, just the memory that holds the on
580 * disk node. Check the freed list before allocating a new one:
582 list_for_each_entry(b, freed, list)
583 if (!btree_node_reclaim(c, b)) {
584 list_del_init(&b->list);
588 b = __btree_node_mem_alloc(c, __GFP_NOWARN);
590 mutex_unlock(&bc->lock);
591 b = __btree_node_mem_alloc(c, GFP_KERNEL);
594 mutex_lock(&bc->lock);
598 six_lock_pcpu_alloc(&b->c.lock);
600 BUG_ON(!six_trylock_intent(&b->c.lock));
601 BUG_ON(!six_trylock_write(&b->c.lock));
605 * btree_free() doesn't free memory; it sticks the node on the end of
606 * the list. Check if there's any freed nodes there:
608 list_for_each_entry(b2, &bc->freeable, list)
609 if (!btree_node_reclaim(c, b2)) {
610 swap(b->data, b2->data);
611 swap(b->aux_data, b2->aux_data);
612 btree_node_to_freedlist(bc, b2);
613 six_unlock_write(&b2->c.lock);
614 six_unlock_intent(&b2->c.lock);
618 mutex_unlock(&bc->lock);
620 if (btree_node_data_alloc(c, b, __GFP_NOWARN|GFP_KERNEL))
623 mutex_lock(&bc->lock);
626 mutex_unlock(&bc->lock);
628 BUG_ON(btree_node_hashed(b));
629 BUG_ON(btree_node_dirty(b));
630 BUG_ON(btree_node_write_in_flight(b));
637 b->whiteout_u64s = 0;
638 bch2_btree_keys_init(b);
639 set_btree_node_accessed(b);
641 bch2_time_stats_update(&c->times[BCH_TIME_btree_node_mem_alloc],
644 memalloc_nofs_restore(flags);
647 mutex_lock(&bc->lock);
649 /* Try to cannibalize another cached btree node: */
650 if (bc->alloc_lock == current) {
651 b2 = btree_node_cannibalize(c);
652 bch2_btree_node_hash_remove(bc, b2);
655 swap(b->data, b2->data);
656 swap(b->aux_data, b2->aux_data);
657 btree_node_to_freedlist(bc, b2);
658 six_unlock_write(&b2->c.lock);
659 six_unlock_intent(&b2->c.lock);
662 list_del_init(&b->list);
665 mutex_unlock(&bc->lock);
667 trace_and_count(c, btree_cache_cannibalize, c);
671 mutex_unlock(&bc->lock);
672 memalloc_nofs_restore(flags);
673 return ERR_PTR(-ENOMEM);
676 /* Slowpath, don't want it inlined into btree_iter_traverse() */
677 static noinline struct btree *bch2_btree_node_fill(struct bch_fs *c,
678 struct btree_trans *trans,
679 struct btree_path *path,
680 const struct bkey_i *k,
681 enum btree_id btree_id,
683 enum six_lock_type lock_type,
686 struct btree_cache *bc = &c->btree_cache;
690 BUG_ON(level + 1 >= BTREE_MAX_DEPTH);
692 * Parent node must be locked, else we could read in a btree node that's
695 if (trans && !bch2_btree_node_relock(trans, path, level + 1)) {
696 trace_and_count(c, trans_restart_relock_parent_for_fill, trans, _THIS_IP_, path);
697 return ERR_PTR(btree_trans_restart(trans, BCH_ERR_transaction_restart_fill_relock));
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_and_count(c, trans_restart_memory_allocation_failure, trans, _THIS_IP_, path);
705 return ERR_PTR(btree_trans_restart(trans, BCH_ERR_transaction_restart_fill_mem_alloc_fail));
711 bkey_copy(&b->key, k);
712 if (bch2_btree_node_hash_insert(bc, b, level, btree_id)) {
713 /* raced with another fill: */
715 /* mark as unhashed... */
718 mutex_lock(&bc->lock);
719 list_add(&b->list, &bc->freeable);
720 mutex_unlock(&bc->lock);
722 six_unlock_write(&b->c.lock);
723 six_unlock_intent(&b->c.lock);
727 set_btree_node_read_in_flight(b);
729 six_unlock_write(&b->c.lock);
730 seq = b->c.lock.state.seq;
731 six_unlock_intent(&b->c.lock);
733 /* Unlock before doing IO: */
735 bch2_trans_unlock(trans);
737 bch2_btree_node_read(c, b, sync);
743 int ret = bch2_trans_relock(trans) ?:
744 bch2_btree_path_relock_intent(trans, path);
746 BUG_ON(!trans->restarted);
751 if (!six_relock_type(&b->c.lock, lock_type, seq)) {
753 trace_and_count(c, trans_restart_relock_after_fill, trans, _THIS_IP_, path);
754 return ERR_PTR(btree_trans_restart(trans, BCH_ERR_transaction_restart_relock_after_fill));
760 static noinline void btree_bad_header(struct bch_fs *c, struct btree *b)
762 struct printbuf buf = PRINTBUF;
764 if (!test_bit(BCH_FS_INITIAL_GC_DONE, &c->flags))
768 "btree node header doesn't match ptr\n"
769 "btree %s level %u\n"
771 bch2_btree_ids[b->c.btree_id], b->c.level);
772 bch2_bkey_val_to_text(&buf, c, bkey_i_to_s_c(&b->key));
774 prt_printf(&buf, "\nheader: btree %s level %llu\n"
776 bch2_btree_ids[BTREE_NODE_ID(b->data)],
777 BTREE_NODE_LEVEL(b->data));
778 bch2_bpos_to_text(&buf, b->data->min_key);
780 prt_printf(&buf, "\nmax ");
781 bch2_bpos_to_text(&buf, b->data->max_key);
783 bch2_fs_inconsistent(c, "%s", buf.buf);
787 static inline void btree_check_header(struct bch_fs *c, struct btree *b)
789 if (b->c.btree_id != BTREE_NODE_ID(b->data) ||
790 b->c.level != BTREE_NODE_LEVEL(b->data) ||
791 bpos_cmp(b->data->max_key, b->key.k.p) ||
792 (b->key.k.type == KEY_TYPE_btree_ptr_v2 &&
793 bpos_cmp(b->data->min_key,
794 bkey_i_to_btree_ptr_v2(&b->key)->v.min_key)))
795 btree_bad_header(c, b);
799 * bch_btree_node_get - find a btree node in the cache and lock it, reading it
800 * in from disk if necessary.
802 * If IO is necessary and running under generic_make_request, returns -EAGAIN.
804 * The btree node will have either a read or a write lock held, depending on
805 * the @write parameter.
807 struct btree *bch2_btree_node_get(struct btree_trans *trans, struct btree_path *path,
808 const struct bkey_i *k, unsigned level,
809 enum six_lock_type lock_type,
810 unsigned long trace_ip)
812 struct bch_fs *c = trans->c;
813 struct btree_cache *bc = &c->btree_cache;
818 EBUG_ON(level >= BTREE_MAX_DEPTH);
820 b = btree_node_mem_ptr(k);
823 * Check b->hash_val _before_ calling btree_node_lock() - this might not
824 * be the node we want anymore, and trying to lock the wrong node could
825 * cause an unneccessary transaction restart:
827 if (likely(c->opts.btree_node_mem_ptr_optimization &&
829 b->hash_val == btree_ptr_hash_val(k)))
832 b = btree_cache_find(bc, k);
835 * We must have the parent locked to call bch2_btree_node_fill(),
836 * else we could read in a btree node from disk that's been
839 b = bch2_btree_node_fill(c, trans, path, k, path->btree_id,
840 level, lock_type, true);
842 /* We raced and found the btree node in the cache */
851 * There's a potential deadlock with splits and insertions into
852 * interior nodes we have to avoid:
854 * The other thread might be holding an intent lock on the node
855 * we want, and they want to update its parent node so they're
856 * going to upgrade their intent lock on the parent node to a
859 * But if we're holding a read lock on the parent, and we're
860 * trying to get the intent lock they're holding, we deadlock.
862 * So to avoid this we drop the read locks on parent nodes when
863 * we're starting to take intent locks - and handle the race.
865 * The race is that they might be about to free the node we
866 * want, and dropping our read lock on the parent node lets them
867 * update the parent marking the node we want as freed, and then
870 * To guard against this, btree nodes are evicted from the cache
871 * when they're freed - and b->hash_val is zeroed out, which we
872 * check for after we lock the node.
874 * Then, bch2_btree_node_relock() on the parent will fail - because
875 * the parent was modified, when the pointer to the node we want
876 * was removed - and we'll bail out:
878 if (btree_node_read_locked(path, level + 1))
879 btree_node_unlock(trans, path, level + 1);
881 ret = btree_node_lock(trans, path, &b->c, level, lock_type, trace_ip);
882 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
887 if (unlikely(b->hash_val != btree_ptr_hash_val(k) ||
888 b->c.level != level ||
890 six_unlock_type(&b->c.lock, lock_type);
891 if (bch2_btree_node_relock(trans, path, level + 1))
894 trace_and_count(c, trans_restart_btree_node_reused, trans, trace_ip, path);
895 return ERR_PTR(btree_trans_restart(trans, BCH_ERR_transaction_restart_lock_node_reused));
899 if (unlikely(btree_node_read_in_flight(b))) {
900 u32 seq = b->c.lock.state.seq;
902 six_unlock_type(&b->c.lock, lock_type);
903 bch2_trans_unlock(trans);
905 bch2_btree_node_wait_on_read(b);
908 * should_be_locked is not set on this path yet, so we need to
909 * relock it specifically:
912 int ret = bch2_trans_relock(trans) ?:
913 bch2_btree_path_relock_intent(trans, path);
915 BUG_ON(!trans->restarted);
920 if (!six_relock_type(&b->c.lock, lock_type, seq))
924 prefetch(b->aux_data);
926 for_each_bset(b, t) {
927 void *p = (u64 *) b->aux_data + t->aux_data_offset;
929 prefetch(p + L1_CACHE_BYTES * 0);
930 prefetch(p + L1_CACHE_BYTES * 1);
931 prefetch(p + L1_CACHE_BYTES * 2);
934 /* avoid atomic set bit if it's not needed: */
935 if (!btree_node_accessed(b))
936 set_btree_node_accessed(b);
938 if (unlikely(btree_node_read_error(b))) {
939 six_unlock_type(&b->c.lock, lock_type);
940 return ERR_PTR(-EIO);
943 EBUG_ON(b->c.btree_id != path->btree_id);
944 EBUG_ON(BTREE_NODE_LEVEL(b->data) != level);
945 btree_check_header(c, b);
950 struct btree *bch2_btree_node_get_noiter(struct btree_trans *trans,
951 const struct bkey_i *k,
952 enum btree_id btree_id,
956 struct bch_fs *c = trans->c;
957 struct btree_cache *bc = &c->btree_cache;
962 EBUG_ON(level >= BTREE_MAX_DEPTH);
964 if (c->opts.btree_node_mem_ptr_optimization) {
965 b = btree_node_mem_ptr(k);
970 b = btree_cache_find(bc, k);
975 b = bch2_btree_node_fill(c, NULL, NULL, k, btree_id,
976 level, SIX_LOCK_read, true);
978 /* We raced and found the btree node in the cache */
983 !bch2_btree_cache_cannibalize_lock(c, NULL))
990 ret = btree_node_lock_nopath(trans, &b->c, SIX_LOCK_read);
991 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
996 if (unlikely(b->hash_val != btree_ptr_hash_val(k) ||
997 b->c.btree_id != btree_id ||
998 b->c.level != level)) {
999 six_unlock_read(&b->c.lock);
1004 /* XXX: waiting on IO with btree locks held: */
1005 __bch2_btree_node_wait_on_read(b);
1007 prefetch(b->aux_data);
1009 for_each_bset(b, t) {
1010 void *p = (u64 *) b->aux_data + t->aux_data_offset;
1012 prefetch(p + L1_CACHE_BYTES * 0);
1013 prefetch(p + L1_CACHE_BYTES * 1);
1014 prefetch(p + L1_CACHE_BYTES * 2);
1017 /* avoid atomic set bit if it's not needed: */
1018 if (!btree_node_accessed(b))
1019 set_btree_node_accessed(b);
1021 if (unlikely(btree_node_read_error(b))) {
1022 six_unlock_read(&b->c.lock);
1027 EBUG_ON(b->c.btree_id != btree_id);
1028 EBUG_ON(BTREE_NODE_LEVEL(b->data) != level);
1029 btree_check_header(c, b);
1031 bch2_btree_cache_cannibalize_unlock(c);
1035 int bch2_btree_node_prefetch(struct bch_fs *c,
1036 struct btree_trans *trans,
1037 struct btree_path *path,
1038 const struct bkey_i *k,
1039 enum btree_id btree_id, unsigned level)
1041 struct btree_cache *bc = &c->btree_cache;
1044 BUG_ON(trans && !btree_node_locked(path, level + 1));
1045 BUG_ON(level >= BTREE_MAX_DEPTH);
1047 b = btree_cache_find(bc, k);
1051 b = bch2_btree_node_fill(c, trans, path, k, btree_id,
1052 level, SIX_LOCK_read, false);
1053 return PTR_ERR_OR_ZERO(b);
1056 void bch2_btree_node_evict(struct btree_trans *trans, const struct bkey_i *k)
1058 struct bch_fs *c = trans->c;
1059 struct btree_cache *bc = &c->btree_cache;
1062 b = btree_cache_find(bc, k);
1066 /* not allowed to wait on io with btree locks held: */
1068 /* XXX we're called from btree_gc which will be holding other btree
1071 __bch2_btree_node_wait_on_read(b);
1072 __bch2_btree_node_wait_on_write(b);
1074 btree_node_lock_nopath_nofail(trans, &b->c, SIX_LOCK_intent);
1075 btree_node_lock_nopath_nofail(trans, &b->c, SIX_LOCK_write);
1077 if (btree_node_dirty(b)) {
1078 __bch2_btree_node_write(c, b, 0);
1079 six_unlock_write(&b->c.lock);
1080 six_unlock_intent(&b->c.lock);
1084 BUG_ON(btree_node_dirty(b));
1086 mutex_lock(&bc->lock);
1087 btree_node_data_free(c, b);
1088 bch2_btree_node_hash_remove(bc, b);
1089 mutex_unlock(&bc->lock);
1091 six_unlock_write(&b->c.lock);
1092 six_unlock_intent(&b->c.lock);
1095 void bch2_btree_node_to_text(struct printbuf *out, struct bch_fs *c,
1098 const struct bkey_format *f = &b->format;
1099 struct bset_stats stats;
1101 memset(&stats, 0, sizeof(stats));
1103 bch2_btree_keys_stats(b, &stats);
1105 prt_printf(out, "l %u ", b->c.level);
1106 bch2_bpos_to_text(out, b->data->min_key);
1107 prt_printf(out, " - ");
1108 bch2_bpos_to_text(out, b->data->max_key);
1109 prt_printf(out, ":\n"
1111 bch2_val_to_text(out, c, bkey_i_to_s_c(&b->key));
1113 prt_printf(out, "\n"
1114 " format: u64s %u fields %u %u %u %u %u\n"
1115 " unpack fn len: %u\n"
1116 " bytes used %zu/%zu (%zu%% full)\n"
1117 " sib u64s: %u, %u (merge threshold %u)\n"
1118 " nr packed keys %u\n"
1119 " nr unpacked keys %u\n"
1121 " failed unpacked %zu\n",
1123 f->bits_per_field[0],
1124 f->bits_per_field[1],
1125 f->bits_per_field[2],
1126 f->bits_per_field[3],
1127 f->bits_per_field[4],
1129 b->nr.live_u64s * sizeof(u64),
1130 btree_bytes(c) - sizeof(struct btree_node),
1131 b->nr.live_u64s * 100 / btree_max_u64s(c),
1134 c->btree_foreground_merge_threshold,
1136 b->nr.unpacked_keys,
1141 void bch2_btree_cache_to_text(struct printbuf *out, struct bch_fs *c)
1143 prt_printf(out, "nr nodes:\t\t%u\n", c->btree_cache.used);
1144 prt_printf(out, "nr dirty:\t\t%u\n", atomic_read(&c->btree_cache.dirty));
1145 prt_printf(out, "cannibalize lock:\t%p\n", c->btree_cache.alloc_lock);