3 #include "btree_cache.h"
4 #include "btree_iter.h"
5 #include "btree_key_cache.h"
6 #include "btree_locking.h"
7 #include "btree_update.h"
11 #include "journal_reclaim.h"
13 #include <linux/sched/mm.h>
14 #include <trace/events/bcachefs.h>
16 static inline bool btree_uses_pcpu_readers(enum btree_id id)
18 return id == BTREE_ID_subvolumes;
21 static struct kmem_cache *bch2_key_cache;
23 static int bch2_btree_key_cache_cmp_fn(struct rhashtable_compare_arg *arg,
26 const struct bkey_cached *ck = obj;
27 const struct bkey_cached_key *key = arg->key;
29 return cmp_int(ck->key.btree_id, key->btree_id) ?:
30 bpos_cmp(ck->key.pos, key->pos);
33 static const struct rhashtable_params bch2_btree_key_cache_params = {
34 .head_offset = offsetof(struct bkey_cached, hash),
35 .key_offset = offsetof(struct bkey_cached, key),
36 .key_len = sizeof(struct bkey_cached_key),
37 .obj_cmpfn = bch2_btree_key_cache_cmp_fn,
41 inline struct bkey_cached *
42 bch2_btree_key_cache_find(struct bch_fs *c, enum btree_id btree_id, struct bpos pos)
44 struct bkey_cached_key key = {
49 return rhashtable_lookup_fast(&c->btree_key_cache.table, &key,
50 bch2_btree_key_cache_params);
53 static bool bkey_cached_lock_for_evict(struct bkey_cached *ck)
55 if (!six_trylock_intent(&ck->c.lock))
58 if (!six_trylock_write(&ck->c.lock)) {
59 six_unlock_intent(&ck->c.lock);
63 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
64 six_unlock_write(&ck->c.lock);
65 six_unlock_intent(&ck->c.lock);
72 static void bkey_cached_evict(struct btree_key_cache *c,
73 struct bkey_cached *ck)
75 BUG_ON(rhashtable_remove_fast(&c->table, &ck->hash,
76 bch2_btree_key_cache_params));
77 memset(&ck->key, ~0, sizeof(ck->key));
79 atomic_long_dec(&c->nr_keys);
82 static void bkey_cached_free(struct btree_key_cache *bc,
83 struct bkey_cached *ck)
85 struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
87 BUG_ON(test_bit(BKEY_CACHED_DIRTY, &ck->flags));
89 ck->btree_trans_barrier_seq =
90 start_poll_synchronize_srcu(&c->btree_trans_barrier);
92 if (ck->c.lock.readers)
93 list_move_tail(&ck->list, &bc->freed_pcpu);
95 list_move_tail(&ck->list, &bc->freed_nonpcpu);
96 atomic_long_inc(&bc->nr_freed);
102 six_unlock_write(&ck->c.lock);
103 six_unlock_intent(&ck->c.lock);
106 static void bkey_cached_move_to_freelist(struct btree_key_cache *bc,
107 struct bkey_cached *ck)
109 struct btree_key_cache_freelist *f;
112 BUG_ON(test_bit(BKEY_CACHED_DIRTY, &ck->flags));
114 if (!ck->c.lock.readers) {
117 f = this_cpu_ptr(bc->pcpu_freed);
119 if (f->nr < ARRAY_SIZE(f->objs)) {
120 f->objs[f->nr++] = ck;
126 mutex_lock(&bc->lock);
128 f = this_cpu_ptr(bc->pcpu_freed);
130 while (f->nr > ARRAY_SIZE(f->objs) / 2) {
131 struct bkey_cached *ck2 = f->objs[--f->nr];
133 list_move_tail(&ck2->list, &bc->freed_nonpcpu);
137 list_move_tail(&ck->list, &bc->freed_nonpcpu);
138 mutex_unlock(&bc->lock);
141 mutex_lock(&bc->lock);
142 list_move_tail(&ck->list, &bc->freed_nonpcpu);
143 mutex_unlock(&bc->lock);
146 mutex_lock(&bc->lock);
147 list_move_tail(&ck->list, &bc->freed_pcpu);
148 mutex_unlock(&bc->lock);
152 static void bkey_cached_free_fast(struct btree_key_cache *bc,
153 struct bkey_cached *ck)
155 struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
157 ck->btree_trans_barrier_seq =
158 start_poll_synchronize_srcu(&c->btree_trans_barrier);
160 list_del_init(&ck->list);
161 atomic_long_inc(&bc->nr_freed);
167 bkey_cached_move_to_freelist(bc, ck);
169 six_unlock_write(&ck->c.lock);
170 six_unlock_intent(&ck->c.lock);
173 static struct bkey_cached *
174 bkey_cached_alloc(struct btree_trans *trans, struct btree_path *path)
176 struct bch_fs *c = trans->c;
177 struct btree_key_cache *bc = &c->btree_key_cache;
178 struct bkey_cached *ck = NULL;
179 struct btree_key_cache_freelist *f;
180 bool pcpu_readers = btree_uses_pcpu_readers(path->btree_id);
185 f = this_cpu_ptr(bc->pcpu_freed);
187 ck = f->objs[--f->nr];
191 mutex_lock(&bc->lock);
193 f = this_cpu_ptr(bc->pcpu_freed);
195 while (!list_empty(&bc->freed_nonpcpu) &&
196 f->nr < ARRAY_SIZE(f->objs) / 2) {
197 ck = list_last_entry(&bc->freed_nonpcpu, struct bkey_cached, list);
198 list_del_init(&ck->list);
199 f->objs[f->nr++] = ck;
202 ck = f->nr ? f->objs[--f->nr] : NULL;
204 mutex_unlock(&bc->lock);
207 mutex_lock(&bc->lock);
208 if (!list_empty(&bc->freed_nonpcpu)) {
209 ck = list_last_entry(&bc->freed_nonpcpu, struct bkey_cached, list);
210 list_del_init(&ck->list);
212 mutex_unlock(&bc->lock);
215 mutex_lock(&bc->lock);
216 if (!list_empty(&bc->freed_pcpu)) {
217 ck = list_last_entry(&bc->freed_pcpu, struct bkey_cached, list);
218 list_del_init(&ck->list);
220 mutex_unlock(&bc->lock);
226 ret = btree_node_lock_nopath(trans, &ck->c, SIX_LOCK_intent);
228 bkey_cached_move_to_freelist(bc, ck);
232 path->l[0].b = (void *) ck;
233 path->l[0].lock_seq = ck->c.lock.state.seq;
234 mark_btree_node_locked(trans, path, 0, SIX_LOCK_intent);
236 ret = bch2_btree_node_lock_write(trans, path, &ck->c);
238 btree_node_unlock(trans, path, 0);
239 bkey_cached_move_to_freelist(bc, ck);
246 /* GFP_NOFS because we're holding btree locks: */
247 ck = kmem_cache_alloc(bch2_key_cache, GFP_NOFS|__GFP_ZERO);
249 INIT_LIST_HEAD(&ck->list);
250 __six_lock_init(&ck->c.lock, "b->c.lock", &bch2_btree_node_lock_key);
252 six_lock_pcpu_alloc(&ck->c.lock);
255 BUG_ON(!six_trylock_intent(&ck->c.lock));
256 BUG_ON(!six_trylock_write(&ck->c.lock));
263 static struct bkey_cached *
264 bkey_cached_reuse(struct btree_key_cache *c)
266 struct bucket_table *tbl;
267 struct rhash_head *pos;
268 struct bkey_cached *ck;
271 mutex_lock(&c->lock);
273 tbl = rht_dereference_rcu(c->table.tbl, &c->table);
274 for (i = 0; i < tbl->size; i++)
275 rht_for_each_entry_rcu(ck, pos, tbl, i, hash) {
276 if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags) &&
277 bkey_cached_lock_for_evict(ck)) {
278 bkey_cached_evict(c, ck);
285 mutex_unlock(&c->lock);
289 static struct bkey_cached *
290 btree_key_cache_create(struct btree_trans *trans, struct btree_path *path)
292 struct bch_fs *c = trans->c;
293 struct btree_key_cache *bc = &c->btree_key_cache;
294 struct bkey_cached *ck;
297 ck = bkey_cached_alloc(trans, path);
298 if (unlikely(IS_ERR(ck)))
302 ck = bkey_cached_reuse(bc);
304 bch_err(c, "error allocating memory for key cache item, btree %s",
305 bch2_btree_ids[path->btree_id]);
306 return ERR_PTR(-ENOMEM);
309 mark_btree_node_locked(trans, path, 0, SIX_LOCK_intent);
312 if (path->btree_id == BTREE_ID_subvolumes)
313 six_lock_pcpu_alloc(&ck->c.lock);
317 ck->c.btree_id = path->btree_id;
318 ck->key.btree_id = path->btree_id;
319 ck->key.pos = path->pos;
321 ck->flags = 1U << BKEY_CACHED_ACCESSED;
323 if (unlikely(rhashtable_lookup_insert_fast(&bc->table,
325 bch2_btree_key_cache_params))) {
326 /* We raced with another fill: */
328 if (likely(was_new)) {
329 six_unlock_write(&ck->c.lock);
330 six_unlock_intent(&ck->c.lock);
331 mark_btree_node_locked(trans, path, 0, BTREE_NODE_UNLOCKED);
334 bkey_cached_free_fast(bc, ck);
340 atomic_long_inc(&bc->nr_keys);
342 six_unlock_write(&ck->c.lock);
347 static int btree_key_cache_fill(struct btree_trans *trans,
348 struct btree_path *ck_path,
349 struct bkey_cached *ck)
351 struct btree_path *path;
353 unsigned new_u64s = 0;
354 struct bkey_i *new_k = NULL;
358 path = bch2_path_get(trans, ck->key.btree_id,
359 ck->key.pos, 0, 0, 0, _THIS_IP_);
360 ret = bch2_btree_path_traverse(trans, path, 0);
364 k = bch2_btree_path_peek_slot(path, &u);
366 if (!bch2_btree_node_relock(trans, ck_path, 0)) {
367 trace_and_count(trans->c, trans_restart_relock_key_cache_fill, trans, _THIS_IP_, ck_path);
368 ret = btree_trans_restart(trans, BCH_ERR_transaction_restart_key_cache_raced);
373 * bch2_varint_decode can read past the end of the buffer by at
374 * most 7 bytes (it won't be used):
376 new_u64s = k.k->u64s + 1;
379 * Allocate some extra space so that the transaction commit path is less
380 * likely to have to reallocate, since that requires a transaction
383 new_u64s = min(256U, (new_u64s * 3) / 2);
385 if (new_u64s > ck->u64s) {
386 new_u64s = roundup_pow_of_two(new_u64s);
387 new_k = kmalloc(new_u64s * sizeof(u64), GFP_NOFS);
389 bch_err(trans->c, "error allocating memory for key cache key, btree %s u64s %u",
390 bch2_btree_ids[ck->key.btree_id], new_u64s);
396 ret = bch2_btree_node_lock_write(trans, ck_path, &ck_path->l[0].b->c);
408 bkey_reassemble(ck->k, k);
410 bch2_btree_node_unlock_write(trans, ck_path, ck_path->l[0].b);
412 /* We're not likely to need this iterator again: */
413 path->preserve = false;
415 bch2_path_put(trans, path, 0);
420 bch2_btree_path_traverse_cached_slowpath(struct btree_trans *trans, struct btree_path *path,
423 struct bch_fs *c = trans->c;
424 struct bkey_cached *ck;
431 if (bch2_btree_node_relock(trans, path, 0)) {
432 ck = (void *) path->l[0].b;
436 ck = bch2_btree_key_cache_find(c, path->btree_id, path->pos);
438 ck = btree_key_cache_create(trans, path);
439 ret = PTR_ERR_OR_ZERO(ck);
445 mark_btree_node_locked(trans, path, 0, SIX_LOCK_intent);
446 path->locks_want = 1;
448 enum six_lock_type lock_want = __btree_lock_want(path, 0);
450 ret = btree_node_lock(trans, path, (void *) ck, 0,
451 lock_want, _THIS_IP_);
452 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
457 if (ck->key.btree_id != path->btree_id ||
458 bpos_cmp(ck->key.pos, path->pos)) {
459 six_unlock_type(&ck->c.lock, lock_want);
463 mark_btree_node_locked(trans, path, 0, lock_want);
466 path->l[0].lock_seq = ck->c.lock.state.seq;
467 path->l[0].b = (void *) ck;
471 * Using the underscore version because we haven't set
472 * path->uptodate yet:
474 if (!path->locks_want &&
475 !__bch2_btree_path_upgrade(trans, path, 1)) {
476 trace_and_count(trans->c, trans_restart_key_cache_upgrade, trans, _THIS_IP_);
477 ret = btree_trans_restart(trans, BCH_ERR_transaction_restart_key_cache_upgrade);
481 ret = btree_key_cache_fill(trans, path, ck);
486 if (!test_bit(BKEY_CACHED_ACCESSED, &ck->flags))
487 set_bit(BKEY_CACHED_ACCESSED, &ck->flags);
489 path->uptodate = BTREE_ITER_UPTODATE;
491 BUG_ON(btree_node_locked_type(path, 0) != btree_lock_want(path, 0));
495 if (!bch2_err_matches(ret, BCH_ERR_transaction_restart)) {
496 btree_node_unlock(trans, path, 0);
497 path->l[0].b = ERR_PTR(ret);
502 int bch2_btree_path_traverse_cached(struct btree_trans *trans, struct btree_path *path,
505 struct bch_fs *c = trans->c;
506 struct bkey_cached *ck;
509 EBUG_ON(path->level);
513 if (bch2_btree_node_relock(trans, path, 0)) {
514 ck = (void *) path->l[0].b;
518 ck = bch2_btree_key_cache_find(c, path->btree_id, path->pos);
520 return bch2_btree_path_traverse_cached_slowpath(trans, path, flags);
522 enum six_lock_type lock_want = __btree_lock_want(path, 0);
524 ret = btree_node_lock(trans, path, (void *) ck, 0,
525 lock_want, _THIS_IP_);
526 EBUG_ON(ret && !bch2_err_matches(ret, BCH_ERR_transaction_restart));
531 if (ck->key.btree_id != path->btree_id ||
532 bpos_cmp(ck->key.pos, path->pos)) {
533 six_unlock_type(&ck->c.lock, lock_want);
537 mark_btree_node_locked(trans, path, 0, lock_want);
540 path->l[0].lock_seq = ck->c.lock.state.seq;
541 path->l[0].b = (void *) ck;
544 return bch2_btree_path_traverse_cached_slowpath(trans, path, flags);
546 if (!test_bit(BKEY_CACHED_ACCESSED, &ck->flags))
547 set_bit(BKEY_CACHED_ACCESSED, &ck->flags);
549 path->uptodate = BTREE_ITER_UPTODATE;
551 EBUG_ON(btree_node_locked_type(path, 0) != btree_lock_want(path, 0));
556 static int btree_key_cache_flush_pos(struct btree_trans *trans,
557 struct bkey_cached_key key,
559 unsigned commit_flags,
562 struct bch_fs *c = trans->c;
563 struct journal *j = &c->journal;
564 struct btree_iter c_iter, b_iter;
565 struct bkey_cached *ck = NULL;
568 bch2_trans_iter_init(trans, &b_iter, key.btree_id, key.pos,
571 BTREE_ITER_ALL_SNAPSHOTS);
572 bch2_trans_iter_init(trans, &c_iter, key.btree_id, key.pos,
575 b_iter.flags &= ~BTREE_ITER_WITH_KEY_CACHE;
577 ret = bch2_btree_iter_traverse(&c_iter);
581 ck = (void *) c_iter.path->l[0].b;
585 if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
593 if (journal_seq && ck->journal.seq != journal_seq)
597 * Since journal reclaim depends on us making progress here, and the
598 * allocator/copygc depend on journal reclaim making progress, we need
599 * to be using alloc reserves:
601 ret = bch2_btree_iter_traverse(&b_iter) ?:
602 bch2_trans_update(trans, &b_iter, ck->k,
603 BTREE_UPDATE_KEY_CACHE_RECLAIM|
604 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE|
605 BTREE_TRIGGER_NORUN) ?:
606 bch2_trans_commit(trans, NULL, NULL,
607 BTREE_INSERT_NOCHECK_RW|
609 BTREE_INSERT_USE_RESERVE|
610 (ck->journal.seq == journal_last_seq(j)
611 ? JOURNAL_WATERMARK_reserved
615 bch2_fs_fatal_err_on(ret &&
616 !bch2_err_matches(ret, BCH_ERR_transaction_restart) &&
617 !bch2_err_matches(ret, BCH_ERR_journal_reclaim_would_deadlock) &&
618 !bch2_journal_error(j), c,
619 "error flushing key cache: %s", bch2_err_str(ret));
623 bch2_journal_pin_drop(j, &ck->journal);
624 bch2_journal_preres_put(j, &ck->res);
626 BUG_ON(!btree_node_locked(c_iter.path, 0));
629 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
630 clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
631 atomic_long_dec(&c->btree_key_cache.nr_dirty);
634 struct btree_path *path2;
636 trans_for_each_path(trans, path2)
637 if (path2 != c_iter.path)
638 __bch2_btree_path_unlock(trans, path2);
640 bch2_btree_node_lock_write_nofail(trans, c_iter.path, &ck->c);
642 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
643 clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
644 atomic_long_dec(&c->btree_key_cache.nr_dirty);
647 mark_btree_node_locked_noreset(c_iter.path, 0, BTREE_NODE_UNLOCKED);
648 bkey_cached_evict(&c->btree_key_cache, ck);
649 bkey_cached_free_fast(&c->btree_key_cache, ck);
652 bch2_trans_iter_exit(trans, &b_iter);
653 bch2_trans_iter_exit(trans, &c_iter);
657 int bch2_btree_key_cache_journal_flush(struct journal *j,
658 struct journal_entry_pin *pin, u64 seq)
660 struct bch_fs *c = container_of(j, struct bch_fs, journal);
661 struct bkey_cached *ck =
662 container_of(pin, struct bkey_cached, journal);
663 struct bkey_cached_key key;
664 struct btree_trans trans;
665 int srcu_idx = srcu_read_lock(&c->btree_trans_barrier);
668 bch2_trans_init(&trans, c, 0, 0);
670 btree_node_lock_nopath_nofail(&trans, &ck->c, SIX_LOCK_read);
673 if (ck->journal.seq != seq ||
674 !test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
675 six_unlock_read(&ck->c.lock);
678 six_unlock_read(&ck->c.lock);
680 ret = commit_do(&trans, NULL, NULL, 0,
681 btree_key_cache_flush_pos(&trans, key, seq,
682 BTREE_INSERT_JOURNAL_RECLAIM, false));
684 srcu_read_unlock(&c->btree_trans_barrier, srcu_idx);
686 bch2_trans_exit(&trans);
691 * Flush and evict a key from the key cache:
693 int bch2_btree_key_cache_flush(struct btree_trans *trans,
694 enum btree_id id, struct bpos pos)
696 struct bch_fs *c = trans->c;
697 struct bkey_cached_key key = { id, pos };
699 /* Fastpath - assume it won't be found: */
700 if (!bch2_btree_key_cache_find(c, id, pos))
703 return btree_key_cache_flush_pos(trans, key, 0, 0, true);
706 bool bch2_btree_insert_key_cached(struct btree_trans *trans,
707 struct btree_path *path,
708 struct bkey_i *insert)
710 struct bch_fs *c = trans->c;
711 struct bkey_cached *ck = (void *) path->l[0].b;
712 bool kick_reclaim = false;
714 BUG_ON(insert->u64s > ck->u64s);
716 if (likely(!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY))) {
719 BUG_ON(jset_u64s(insert->u64s) > trans->journal_preres.u64s);
721 difference = jset_u64s(insert->u64s) - ck->res.u64s;
722 if (difference > 0) {
723 trans->journal_preres.u64s -= difference;
724 ck->res.u64s += difference;
728 bkey_copy(ck->k, insert);
731 if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
732 set_bit(BKEY_CACHED_DIRTY, &ck->flags);
733 atomic_long_inc(&c->btree_key_cache.nr_dirty);
735 if (bch2_nr_btree_keys_need_flush(c))
739 bch2_journal_pin_update(&c->journal, trans->journal_res.seq,
740 &ck->journal, bch2_btree_key_cache_journal_flush);
743 journal_reclaim_kick(&c->journal);
747 void bch2_btree_key_cache_drop(struct btree_trans *trans,
748 struct btree_path *path)
750 struct bch_fs *c = trans->c;
751 struct bkey_cached *ck = (void *) path->l[0].b;
756 * We just did an update to the btree, bypassing the key cache: the key
757 * cache key is now stale and must be dropped, even if dirty:
759 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
760 clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
761 atomic_long_dec(&c->btree_key_cache.nr_dirty);
762 bch2_journal_pin_drop(&c->journal, &ck->journal);
768 static unsigned long bch2_btree_key_cache_scan(struct shrinker *shrink,
769 struct shrink_control *sc)
771 struct bch_fs *c = container_of(shrink, struct bch_fs,
772 btree_key_cache.shrink);
773 struct btree_key_cache *bc = &c->btree_key_cache;
774 struct bucket_table *tbl;
775 struct bkey_cached *ck, *t;
776 size_t scanned = 0, freed = 0, nr = sc->nr_to_scan;
777 unsigned start, flags;
780 mutex_lock(&bc->lock);
781 srcu_idx = srcu_read_lock(&c->btree_trans_barrier);
782 flags = memalloc_nofs_save();
785 * Newest freed entries are at the end of the list - once we hit one
786 * that's too new to be freed, we can bail out:
788 list_for_each_entry_safe(ck, t, &bc->freed_nonpcpu, list) {
789 if (!poll_state_synchronize_srcu(&c->btree_trans_barrier,
790 ck->btree_trans_barrier_seq))
794 six_lock_pcpu_free(&ck->c.lock);
795 kmem_cache_free(bch2_key_cache, ck);
796 atomic_long_dec(&bc->nr_freed);
804 list_for_each_entry_safe(ck, t, &bc->freed_pcpu, list) {
805 if (!poll_state_synchronize_srcu(&c->btree_trans_barrier,
806 ck->btree_trans_barrier_seq))
810 six_lock_pcpu_free(&ck->c.lock);
811 kmem_cache_free(bch2_key_cache, ck);
812 atomic_long_dec(&bc->nr_freed);
821 tbl = rht_dereference_rcu(bc->table.tbl, &bc->table);
822 if (bc->shrink_iter >= tbl->size)
824 start = bc->shrink_iter;
827 struct rhash_head *pos, *next;
829 pos = rht_ptr_rcu(rht_bucket(tbl, bc->shrink_iter));
831 while (!rht_is_a_nulls(pos)) {
832 next = rht_dereference_bucket_rcu(pos->next, tbl, bc->shrink_iter);
833 ck = container_of(pos, struct bkey_cached, hash);
835 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags))
838 if (test_bit(BKEY_CACHED_ACCESSED, &ck->flags))
839 clear_bit(BKEY_CACHED_ACCESSED, &ck->flags);
840 else if (bkey_cached_lock_for_evict(ck)) {
841 bkey_cached_evict(bc, ck);
842 bkey_cached_free(bc, ck);
853 if (bc->shrink_iter >= tbl->size)
855 } while (scanned < nr && bc->shrink_iter != start);
859 memalloc_nofs_restore(flags);
860 srcu_read_unlock(&c->btree_trans_barrier, srcu_idx);
861 mutex_unlock(&bc->lock);
866 static unsigned long bch2_btree_key_cache_count(struct shrinker *shrink,
867 struct shrink_control *sc)
869 struct bch_fs *c = container_of(shrink, struct bch_fs,
870 btree_key_cache.shrink);
871 struct btree_key_cache *bc = &c->btree_key_cache;
872 long nr = atomic_long_read(&bc->nr_keys) -
873 atomic_long_read(&bc->nr_dirty);
878 void bch2_fs_btree_key_cache_exit(struct btree_key_cache *bc)
880 struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
881 struct bucket_table *tbl;
882 struct bkey_cached *ck, *n;
883 struct rhash_head *pos;
889 if (bc->shrink.list.next)
890 unregister_shrinker(&bc->shrink);
892 mutex_lock(&bc->lock);
895 tbl = rht_dereference_rcu(bc->table.tbl, &bc->table);
897 for (i = 0; i < tbl->size; i++)
898 rht_for_each_entry_rcu(ck, pos, tbl, i, hash) {
899 bkey_cached_evict(bc, ck);
900 list_add(&ck->list, &bc->freed_nonpcpu);
905 for_each_possible_cpu(cpu) {
906 struct btree_key_cache_freelist *f =
907 per_cpu_ptr(bc->pcpu_freed, cpu);
909 for (i = 0; i < f->nr; i++) {
911 list_add(&ck->list, &bc->freed_nonpcpu);
916 list_splice(&bc->freed_pcpu, &bc->freed_nonpcpu);
918 list_for_each_entry_safe(ck, n, &bc->freed_nonpcpu, list) {
921 bch2_journal_pin_drop(&c->journal, &ck->journal);
922 bch2_journal_preres_put(&c->journal, &ck->res);
926 six_lock_pcpu_free(&ck->c.lock);
927 kmem_cache_free(bch2_key_cache, ck);
930 if (atomic_long_read(&bc->nr_dirty) &&
931 !bch2_journal_error(&c->journal) &&
932 test_bit(BCH_FS_WAS_RW, &c->flags))
933 panic("btree key cache shutdown error: nr_dirty nonzero (%li)\n",
934 atomic_long_read(&bc->nr_dirty));
936 if (atomic_long_read(&bc->nr_keys))
937 panic("btree key cache shutdown error: nr_keys nonzero (%li)\n",
938 atomic_long_read(&bc->nr_keys));
940 mutex_unlock(&bc->lock);
942 if (bc->table_init_done)
943 rhashtable_destroy(&bc->table);
945 free_percpu(bc->pcpu_freed);
948 void bch2_fs_btree_key_cache_init_early(struct btree_key_cache *c)
950 mutex_init(&c->lock);
951 INIT_LIST_HEAD(&c->freed_pcpu);
952 INIT_LIST_HEAD(&c->freed_nonpcpu);
955 static void bch2_btree_key_cache_shrinker_to_text(struct printbuf *out, struct shrinker *shrink)
957 struct btree_key_cache *bc =
958 container_of(shrink, struct btree_key_cache, shrink);
960 bch2_btree_key_cache_to_text(out, bc);
963 int bch2_fs_btree_key_cache_init(struct btree_key_cache *bc)
965 struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
969 bc->pcpu_freed = alloc_percpu(struct btree_key_cache_freelist);
974 ret = rhashtable_init(&bc->table, &bch2_btree_key_cache_params);
978 bc->table_init_done = true;
980 bc->shrink.seeks = 1;
981 bc->shrink.count_objects = bch2_btree_key_cache_count;
982 bc->shrink.scan_objects = bch2_btree_key_cache_scan;
983 bc->shrink.to_text = bch2_btree_key_cache_shrinker_to_text;
984 return register_shrinker(&bc->shrink, "%s/btree_key_cache", c->name);
987 void bch2_btree_key_cache_to_text(struct printbuf *out, struct btree_key_cache *c)
989 prt_printf(out, "nr_freed:\t%zu\n", atomic_long_read(&c->nr_freed));
990 prt_printf(out, "nr_keys:\t%lu\n", atomic_long_read(&c->nr_keys));
991 prt_printf(out, "nr_dirty:\t%lu\n", atomic_long_read(&c->nr_dirty));
994 void bch2_btree_key_cache_exit(void)
997 kmem_cache_destroy(bch2_key_cache);
1000 int __init bch2_btree_key_cache_init(void)
1002 bch2_key_cache = KMEM_CACHE(bkey_cached, 0);
1003 if (!bch2_key_cache)