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;
278 if (bch2_btree_shrinker_disabled)
281 /* Return -1 if we can't do anything right now */
282 if (sc->gfp_mask & __GFP_FS)
283 mutex_lock(&bc->lock);
284 else if (!mutex_trylock(&bc->lock))
287 flags = memalloc_nofs_save();
290 * It's _really_ critical that we don't free too many btree nodes - we
291 * have to always leave ourselves a reserve. The reserve is how we
292 * guarantee that allocating memory for a new btree node can always
293 * succeed, so that inserting keys into the btree can always succeed and
294 * IO can always make forward progress:
296 nr /= btree_pages(c);
297 can_free = btree_cache_can_free(bc);
298 nr = min_t(unsigned long, nr, can_free);
301 list_for_each_entry_safe(b, t, &bc->freeable, list) {
308 !btree_node_reclaim(c, b)) {
309 btree_node_data_free(c, b);
310 six_unlock_write(&b->c.lock);
311 six_unlock_intent(&b->c.lock);
316 list_for_each_entry_safe(b, t, &bc->live, list) {
321 if (&t->list != &bc->live)
322 list_move_tail(&bc->live, &t->list);
326 if (!btree_node_accessed(b) &&
327 !btree_node_reclaim(c, b)) {
328 /* can't call bch2_btree_node_hash_remove under lock */
330 if (&t->list != &bc->live)
331 list_move_tail(&bc->live, &t->list);
333 btree_node_data_free(c, b);
334 mutex_unlock(&bc->lock);
336 bch2_btree_node_hash_remove(bc, b);
337 six_unlock_write(&b->c.lock);
338 six_unlock_intent(&b->c.lock);
343 if (sc->gfp_mask & __GFP_FS)
344 mutex_lock(&bc->lock);
345 else if (!mutex_trylock(&bc->lock))
349 clear_btree_node_accessed(b);
352 mutex_unlock(&bc->lock);
354 memalloc_nofs_restore(flags);
355 return (unsigned long) freed * btree_pages(c);
358 static unsigned long bch2_btree_cache_count(struct shrinker *shrink,
359 struct shrink_control *sc)
361 struct bch_fs *c = container_of(shrink, struct bch_fs,
363 struct btree_cache *bc = &c->btree_cache;
365 if (bch2_btree_shrinker_disabled)
368 return btree_cache_can_free(bc) * btree_pages(c);
371 void bch2_fs_btree_cache_exit(struct bch_fs *c)
373 struct btree_cache *bc = &c->btree_cache;
377 if (bc->shrink.list.next)
378 unregister_shrinker(&bc->shrink);
380 /* vfree() can allocate memory: */
381 flags = memalloc_nofs_save();
382 mutex_lock(&bc->lock);
385 list_move(&c->verify_data->list, &bc->live);
387 kvpfree(c->verify_ondisk, btree_bytes(c));
389 for (i = 0; i < BTREE_ID_NR; i++)
390 if (c->btree_roots[i].b)
391 list_add(&c->btree_roots[i].b->list, &bc->live);
393 list_splice(&bc->freeable, &bc->live);
395 while (!list_empty(&bc->live)) {
396 b = list_first_entry(&bc->live, struct btree, list);
398 BUG_ON(btree_node_read_in_flight(b) ||
399 btree_node_write_in_flight(b));
401 if (btree_node_dirty(b))
402 bch2_btree_complete_write(c, b, btree_current_write(b));
403 clear_btree_node_dirty(c, b);
405 btree_node_data_free(c, b);
408 BUG_ON(atomic_read(&c->btree_cache.dirty));
410 while (!list_empty(&bc->freed)) {
411 b = list_first_entry(&bc->freed, struct btree, list);
413 six_lock_pcpu_free(&b->c.lock);
417 mutex_unlock(&bc->lock);
418 memalloc_nofs_restore(flags);
420 if (bc->table_init_done)
421 rhashtable_destroy(&bc->table);
424 int bch2_fs_btree_cache_init(struct bch_fs *c)
426 struct btree_cache *bc = &c->btree_cache;
430 pr_verbose_init(c->opts, "");
432 ret = rhashtable_init(&bc->table, &bch_btree_cache_params);
436 bc->table_init_done = true;
438 bch2_recalc_btree_reserve(c);
440 for (i = 0; i < bc->reserve; i++)
441 if (!__bch2_btree_node_mem_alloc(c)) {
446 list_splice_init(&bc->live, &bc->freeable);
448 mutex_init(&c->verify_lock);
450 bc->shrink.count_objects = bch2_btree_cache_count;
451 bc->shrink.scan_objects = bch2_btree_cache_scan;
452 bc->shrink.seeks = 4;
453 bc->shrink.batch = btree_pages(c) * 2;
454 ret = register_shrinker(&bc->shrink);
456 pr_verbose_init(c->opts, "ret %i", ret);
460 void bch2_fs_btree_cache_init_early(struct btree_cache *bc)
462 mutex_init(&bc->lock);
463 INIT_LIST_HEAD(&bc->live);
464 INIT_LIST_HEAD(&bc->freeable);
465 INIT_LIST_HEAD(&bc->freed);
469 * We can only have one thread cannibalizing other cached btree nodes at a time,
470 * or we'll deadlock. We use an open coded mutex to ensure that, which a
471 * cannibalize_bucket() will take. This means every time we unlock the root of
472 * the btree, we need to release this lock if we have it held.
474 void bch2_btree_cache_cannibalize_unlock(struct bch_fs *c)
476 struct btree_cache *bc = &c->btree_cache;
478 if (bc->alloc_lock == current) {
479 trace_btree_node_cannibalize_unlock(c);
480 bc->alloc_lock = NULL;
481 closure_wake_up(&bc->alloc_wait);
485 int bch2_btree_cache_cannibalize_lock(struct bch_fs *c, struct closure *cl)
487 struct btree_cache *bc = &c->btree_cache;
488 struct task_struct *old;
490 old = cmpxchg(&bc->alloc_lock, NULL, current);
491 if (old == NULL || old == current)
495 trace_btree_node_cannibalize_lock_fail(c);
499 closure_wait(&bc->alloc_wait, cl);
501 /* Try again, after adding ourselves to waitlist */
502 old = cmpxchg(&bc->alloc_lock, NULL, current);
503 if (old == NULL || old == current) {
505 closure_wake_up(&bc->alloc_wait);
509 trace_btree_node_cannibalize_lock_fail(c);
513 trace_btree_node_cannibalize_lock(c);
517 static struct btree *btree_node_cannibalize(struct bch_fs *c)
519 struct btree_cache *bc = &c->btree_cache;
522 list_for_each_entry_reverse(b, &bc->live, list)
523 if (!btree_node_reclaim(c, b))
527 list_for_each_entry_reverse(b, &bc->live, list)
528 if (!btree_node_write_and_reclaim(c, b))
532 * Rare case: all nodes were intent-locked.
535 WARN_ONCE(1, "btree cache cannibalize failed\n");
540 struct btree *bch2_btree_node_mem_alloc(struct bch_fs *c)
542 struct btree_cache *bc = &c->btree_cache;
544 u64 start_time = local_clock();
547 flags = memalloc_nofs_save();
548 mutex_lock(&bc->lock);
551 * btree_free() doesn't free memory; it sticks the node on the end of
552 * the list. Check if there's any freed nodes there:
554 list_for_each_entry(b, &bc->freeable, list)
555 if (!btree_node_reclaim(c, b))
559 * We never free struct btree itself, just the memory that holds the on
560 * disk node. Check the freed list before allocating a new one:
562 list_for_each_entry(b, &bc->freed, list)
563 if (!btree_node_reclaim(c, b))
569 list_del_init(&b->list);
570 mutex_unlock(&bc->lock);
573 b = __btree_node_mem_alloc(c);
577 BUG_ON(!six_trylock_intent(&b->c.lock));
578 BUG_ON(!six_trylock_write(&b->c.lock));
582 if (btree_node_data_alloc(c, b, __GFP_NOWARN|GFP_KERNEL))
585 mutex_lock(&bc->lock);
587 mutex_unlock(&bc->lock);
590 BUG_ON(btree_node_hashed(b));
591 BUG_ON(btree_node_dirty(b));
592 BUG_ON(btree_node_write_in_flight(b));
599 b->whiteout_u64s = 0;
600 bch2_btree_keys_init(b);
601 set_btree_node_accessed(b);
603 bch2_time_stats_update(&c->times[BCH_TIME_btree_node_mem_alloc],
606 memalloc_nofs_restore(flags);
609 mutex_lock(&bc->lock);
612 list_add(&b->list, &bc->freed);
613 six_unlock_write(&b->c.lock);
614 six_unlock_intent(&b->c.lock);
617 /* Try to cannibalize another cached btree node: */
618 if (bc->alloc_lock == current) {
619 b = btree_node_cannibalize(c);
620 list_del_init(&b->list);
621 mutex_unlock(&bc->lock);
623 bch2_btree_node_hash_remove(bc, b);
625 trace_btree_node_cannibalize(c);
629 mutex_unlock(&bc->lock);
630 memalloc_nofs_restore(flags);
631 return ERR_PTR(-ENOMEM);
634 /* Slowpath, don't want it inlined into btree_iter_traverse() */
635 static noinline struct btree *bch2_btree_node_fill(struct bch_fs *c,
636 struct btree_trans *trans,
637 struct btree_path *path,
638 const struct bkey_i *k,
639 enum btree_id btree_id,
641 enum six_lock_type lock_type,
644 struct btree_cache *bc = &c->btree_cache;
648 BUG_ON(level + 1 >= BTREE_MAX_DEPTH);
650 * Parent node must be locked, else we could read in a btree node that's
653 if (trans && !bch2_btree_node_relock(trans, path, level + 1)) {
654 btree_trans_restart(trans);
655 return ERR_PTR(-EINTR);
658 b = bch2_btree_node_mem_alloc(c);
662 bkey_copy(&b->key, k);
663 if (bch2_btree_node_hash_insert(bc, b, level, btree_id)) {
664 /* raced with another fill: */
666 /* mark as unhashed... */
669 mutex_lock(&bc->lock);
670 list_add(&b->list, &bc->freeable);
671 mutex_unlock(&bc->lock);
673 six_unlock_write(&b->c.lock);
674 six_unlock_intent(&b->c.lock);
678 set_btree_node_read_in_flight(b);
680 six_unlock_write(&b->c.lock);
681 seq = b->c.lock.state.seq;
682 six_unlock_intent(&b->c.lock);
684 /* Unlock before doing IO: */
686 bch2_trans_unlock(trans);
688 bch2_btree_node_read(c, b, sync);
694 (!bch2_trans_relock(trans) ||
695 !bch2_btree_path_relock_intent(trans, path))) {
696 BUG_ON(!trans->restarted);
697 return ERR_PTR(-EINTR);
700 if (!six_relock_type(&b->c.lock, lock_type, seq)) {
701 btree_trans_restart(trans);
702 return ERR_PTR(-EINTR);
708 static int lock_node_check_fn(struct six_lock *lock, void *p)
710 struct btree *b = container_of(lock, struct btree, c.lock);
711 const struct bkey_i *k = p;
713 return b->hash_val == btree_ptr_hash_val(k) ? 0 : -1;
716 static noinline void btree_bad_header(struct bch_fs *c, struct btree *b)
718 char buf1[200], buf2[100], buf3[100];
720 if (!test_bit(BCH_FS_INITIAL_GC_DONE, &c->flags))
723 bch2_bkey_val_to_text(&PBUF(buf1), c, bkey_i_to_s_c(&b->key));
724 bch2_bpos_to_text(&PBUF(buf2), b->data->min_key);
725 bch2_bpos_to_text(&PBUF(buf3), b->data->max_key);
727 bch2_fs_inconsistent(c, "btree node header doesn't match ptr\n"
728 "btree %s level %u\n"
730 "header: btree %s level %llu\n"
732 bch2_btree_ids[b->c.btree_id], b->c.level,
734 bch2_btree_ids[BTREE_NODE_ID(b->data)],
735 BTREE_NODE_LEVEL(b->data),
739 static inline void btree_check_header(struct bch_fs *c, struct btree *b)
741 if (b->c.btree_id != BTREE_NODE_ID(b->data) ||
742 b->c.level != BTREE_NODE_LEVEL(b->data) ||
743 bpos_cmp(b->data->max_key, b->key.k.p) ||
744 (b->key.k.type == KEY_TYPE_btree_ptr_v2 &&
745 bpos_cmp(b->data->min_key,
746 bkey_i_to_btree_ptr_v2(&b->key)->v.min_key)))
747 btree_bad_header(c, b);
751 * bch_btree_node_get - find a btree node in the cache and lock it, reading it
752 * in from disk if necessary.
754 * If IO is necessary and running under generic_make_request, returns -EAGAIN.
756 * The btree node will have either a read or a write lock held, depending on
757 * the @write parameter.
759 struct btree *bch2_btree_node_get(struct btree_trans *trans, struct btree_path *path,
760 const struct bkey_i *k, unsigned level,
761 enum six_lock_type lock_type,
762 unsigned long trace_ip)
764 struct bch_fs *c = trans->c;
765 struct btree_cache *bc = &c->btree_cache;
769 EBUG_ON(level >= BTREE_MAX_DEPTH);
771 b = btree_node_mem_ptr(k);
774 * Check b->hash_val _before_ calling btree_node_lock() - this might not
775 * be the node we want anymore, and trying to lock the wrong node could
776 * cause an unneccessary transaction restart:
778 if (likely(c->opts.btree_node_mem_ptr_optimization &&
780 b->hash_val == btree_ptr_hash_val(k)))
783 b = btree_cache_find(bc, k);
786 * We must have the parent locked to call bch2_btree_node_fill(),
787 * else we could read in a btree node from disk that's been
790 b = bch2_btree_node_fill(c, trans, path, k, path->btree_id,
791 level, lock_type, true);
793 /* We raced and found the btree node in the cache */
802 * There's a potential deadlock with splits and insertions into
803 * interior nodes we have to avoid:
805 * The other thread might be holding an intent lock on the node
806 * we want, and they want to update its parent node so they're
807 * going to upgrade their intent lock on the parent node to a
810 * But if we're holding a read lock on the parent, and we're
811 * trying to get the intent lock they're holding, we deadlock.
813 * So to avoid this we drop the read locks on parent nodes when
814 * we're starting to take intent locks - and handle the race.
816 * The race is that they might be about to free the node we
817 * want, and dropping our read lock on the parent node lets them
818 * update the parent marking the node we want as freed, and then
821 * To guard against this, btree nodes are evicted from the cache
822 * when they're freed - and b->hash_val is zeroed out, which we
823 * check for after we lock the node.
825 * Then, bch2_btree_node_relock() on the parent will fail - because
826 * the parent was modified, when the pointer to the node we want
827 * was removed - and we'll bail out:
829 if (btree_node_read_locked(path, level + 1))
830 btree_node_unlock(path, level + 1);
832 if (!btree_node_lock(trans, path, b, k->k.p, level, lock_type,
833 lock_node_check_fn, (void *) k, trace_ip)) {
834 if (!trans->restarted)
836 return ERR_PTR(-EINTR);
839 if (unlikely(b->hash_val != btree_ptr_hash_val(k) ||
840 b->c.level != level ||
842 six_unlock_type(&b->c.lock, lock_type);
843 if (bch2_btree_node_relock(trans, path, level + 1))
846 trace_trans_restart_btree_node_reused(trans->ip,
850 btree_trans_restart(trans);
851 return ERR_PTR(-EINTR);
855 if (unlikely(btree_node_read_in_flight(b))) {
856 u32 seq = b->c.lock.state.seq;
858 six_unlock_type(&b->c.lock, lock_type);
859 bch2_trans_unlock(trans);
861 bch2_btree_node_wait_on_read(b);
864 * should_be_locked is not set on this path yet, so we need to
865 * relock it specifically:
868 (!bch2_trans_relock(trans) ||
869 !bch2_btree_path_relock_intent(trans, path))) {
870 BUG_ON(!trans->restarted);
871 return ERR_PTR(-EINTR);
874 if (!six_relock_type(&b->c.lock, lock_type, seq))
878 prefetch(b->aux_data);
880 for_each_bset(b, t) {
881 void *p = (u64 *) b->aux_data + t->aux_data_offset;
883 prefetch(p + L1_CACHE_BYTES * 0);
884 prefetch(p + L1_CACHE_BYTES * 1);
885 prefetch(p + L1_CACHE_BYTES * 2);
888 /* avoid atomic set bit if it's not needed: */
889 if (!btree_node_accessed(b))
890 set_btree_node_accessed(b);
892 if (unlikely(btree_node_read_error(b))) {
893 six_unlock_type(&b->c.lock, lock_type);
894 return ERR_PTR(-EIO);
897 EBUG_ON(b->c.btree_id != path->btree_id);
898 EBUG_ON(BTREE_NODE_LEVEL(b->data) != level);
899 btree_check_header(c, b);
904 struct btree *bch2_btree_node_get_noiter(struct bch_fs *c,
905 const struct bkey_i *k,
906 enum btree_id btree_id,
910 struct btree_cache *bc = &c->btree_cache;
915 EBUG_ON(level >= BTREE_MAX_DEPTH);
917 if (c->opts.btree_node_mem_ptr_optimization) {
918 b = btree_node_mem_ptr(k);
923 b = btree_cache_find(bc, k);
928 b = bch2_btree_node_fill(c, NULL, NULL, k, btree_id,
929 level, SIX_LOCK_read, true);
931 /* We raced and found the btree node in the cache */
936 !bch2_btree_cache_cannibalize_lock(c, NULL))
943 ret = six_lock_read(&b->c.lock, lock_node_check_fn, (void *) k);
947 if (unlikely(b->hash_val != btree_ptr_hash_val(k) ||
948 b->c.btree_id != btree_id ||
949 b->c.level != level)) {
950 six_unlock_read(&b->c.lock);
955 /* XXX: waiting on IO with btree locks held: */
956 __bch2_btree_node_wait_on_read(b);
958 prefetch(b->aux_data);
960 for_each_bset(b, t) {
961 void *p = (u64 *) b->aux_data + t->aux_data_offset;
963 prefetch(p + L1_CACHE_BYTES * 0);
964 prefetch(p + L1_CACHE_BYTES * 1);
965 prefetch(p + L1_CACHE_BYTES * 2);
968 /* avoid atomic set bit if it's not needed: */
969 if (!btree_node_accessed(b))
970 set_btree_node_accessed(b);
972 if (unlikely(btree_node_read_error(b))) {
973 six_unlock_read(&b->c.lock);
978 EBUG_ON(b->c.btree_id != btree_id);
979 EBUG_ON(BTREE_NODE_LEVEL(b->data) != level);
980 btree_check_header(c, b);
982 bch2_btree_cache_cannibalize_unlock(c);
986 int bch2_btree_node_prefetch(struct bch_fs *c,
987 struct btree_trans *trans,
988 struct btree_path *path,
989 const struct bkey_i *k,
990 enum btree_id btree_id, unsigned level)
992 struct btree_cache *bc = &c->btree_cache;
995 BUG_ON(trans && !btree_node_locked(path, level + 1));
996 BUG_ON(level >= BTREE_MAX_DEPTH);
998 b = btree_cache_find(bc, k);
1002 b = bch2_btree_node_fill(c, trans, path, k, btree_id,
1003 level, SIX_LOCK_read, false);
1004 return PTR_ERR_OR_ZERO(b);
1007 void bch2_btree_node_evict(struct bch_fs *c, const struct bkey_i *k)
1009 struct btree_cache *bc = &c->btree_cache;
1012 b = btree_cache_find(bc, k);
1016 /* not allowed to wait on io with btree locks held: */
1018 /* XXX we're called from btree_gc which will be holding other btree
1021 __bch2_btree_node_wait_on_read(b);
1022 __bch2_btree_node_wait_on_write(b);
1024 six_lock_intent(&b->c.lock, NULL, NULL);
1025 six_lock_write(&b->c.lock, NULL, NULL);
1027 if (btree_node_dirty(b)) {
1028 __bch2_btree_node_write(c, b, false);
1029 six_unlock_write(&b->c.lock);
1030 six_unlock_intent(&b->c.lock);
1034 BUG_ON(btree_node_dirty(b));
1036 mutex_lock(&bc->lock);
1037 btree_node_data_free(c, b);
1038 bch2_btree_node_hash_remove(bc, b);
1039 mutex_unlock(&bc->lock);
1041 six_unlock_write(&b->c.lock);
1042 six_unlock_intent(&b->c.lock);
1045 void bch2_btree_node_to_text(struct printbuf *out, struct bch_fs *c,
1048 const struct bkey_format *f = &b->format;
1049 struct bset_stats stats;
1051 memset(&stats, 0, sizeof(stats));
1053 bch2_btree_keys_stats(b, &stats);
1055 pr_buf(out, "l %u ", b->c.level);
1056 bch2_bpos_to_text(out, b->data->min_key);
1058 bch2_bpos_to_text(out, b->data->max_key);
1061 bch2_val_to_text(out, c, bkey_i_to_s_c(&b->key));
1064 " format: u64s %u fields %u %u %u %u %u\n"
1065 " unpack fn len: %u\n"
1066 " bytes used %zu/%zu (%zu%% full)\n"
1067 " sib u64s: %u, %u (merge threshold %u)\n"
1068 " nr packed keys %u\n"
1069 " nr unpacked keys %u\n"
1071 " failed unpacked %zu\n",
1073 f->bits_per_field[0],
1074 f->bits_per_field[1],
1075 f->bits_per_field[2],
1076 f->bits_per_field[3],
1077 f->bits_per_field[4],
1079 b->nr.live_u64s * sizeof(u64),
1080 btree_bytes(c) - sizeof(struct btree_node),
1081 b->nr.live_u64s * 100 / btree_max_u64s(c),
1084 c->btree_foreground_merge_threshold,
1086 b->nr.unpacked_keys,
1091 void bch2_btree_cache_to_text(struct printbuf *out, struct bch_fs *c)
1093 pr_buf(out, "nr nodes:\t\t%u\n", c->btree_cache.used);
1094 pr_buf(out, "nr dirty:\t\t%u\n", atomic_read(&c->btree_cache.dirty));
1095 pr_buf(out, "cannibalize lock:\t%p\n", c->btree_cache.alloc_lock);