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) {
116 f = this_cpu_ptr(bc->pcpu_freed);
118 if (f->nr < ARRAY_SIZE(f->objs)) {
119 f->objs[f->nr++] = ck;
125 mutex_lock(&bc->lock);
127 f = this_cpu_ptr(bc->pcpu_freed);
129 while (f->nr > ARRAY_SIZE(f->objs) / 2) {
130 struct bkey_cached *ck2 = f->objs[--f->nr];
132 list_move_tail(&ck2->list, &bc->freed_nonpcpu);
136 list_move_tail(&ck->list, &bc->freed_nonpcpu);
137 mutex_unlock(&bc->lock);
140 mutex_lock(&bc->lock);
141 list_move_tail(&ck->list, &bc->freed_pcpu);
142 mutex_unlock(&bc->lock);
146 static void bkey_cached_free_fast(struct btree_key_cache *bc,
147 struct bkey_cached *ck)
149 struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
151 ck->btree_trans_barrier_seq =
152 start_poll_synchronize_srcu(&c->btree_trans_barrier);
154 list_del_init(&ck->list);
155 atomic_long_inc(&bc->nr_freed);
161 bkey_cached_move_to_freelist(bc, ck);
163 six_unlock_write(&ck->c.lock);
164 six_unlock_intent(&ck->c.lock);
167 static struct bkey_cached *
168 bkey_cached_alloc(struct btree_trans *trans, struct btree_path *path)
170 struct bch_fs *c = trans->c;
171 struct btree_key_cache *bc = &c->btree_key_cache;
172 struct bkey_cached *ck = NULL;
173 struct btree_key_cache_freelist *f;
174 bool pcpu_readers = btree_uses_pcpu_readers(path->btree_id);
178 f = this_cpu_ptr(bc->pcpu_freed);
180 ck = f->objs[--f->nr];
184 mutex_lock(&bc->lock);
186 f = this_cpu_ptr(bc->pcpu_freed);
188 while (!list_empty(&bc->freed_nonpcpu) &&
189 f->nr < ARRAY_SIZE(f->objs) / 2) {
190 ck = list_last_entry(&bc->freed_nonpcpu, struct bkey_cached, list);
191 list_del_init(&ck->list);
192 f->objs[f->nr++] = ck;
195 ck = f->nr ? f->objs[--f->nr] : NULL;
197 mutex_unlock(&bc->lock);
200 mutex_lock(&bc->lock);
201 if (!list_empty(&bc->freed_pcpu)) {
202 ck = list_last_entry(&bc->freed_pcpu, struct bkey_cached, list);
203 list_del_init(&ck->list);
205 mutex_unlock(&bc->lock);
211 ret = btree_node_lock_nopath(trans, &ck->c, SIX_LOCK_intent);
213 bkey_cached_move_to_freelist(bc, ck);
217 path->l[0].b = (void *) ck;
218 path->l[0].lock_seq = ck->c.lock.state.seq;
219 mark_btree_node_locked(trans, path, 0, SIX_LOCK_intent);
221 ret = bch2_btree_node_lock_write(trans, path, &ck->c);
223 btree_node_unlock(trans, path, 0);
224 bkey_cached_move_to_freelist(bc, ck);
231 ck = kmem_cache_alloc(bch2_key_cache, GFP_NOFS|__GFP_ZERO);
233 INIT_LIST_HEAD(&ck->list);
234 __six_lock_init(&ck->c.lock, "b->c.lock", &bch2_btree_node_lock_key);
236 six_lock_pcpu_alloc(&ck->c.lock);
239 BUG_ON(!six_trylock_intent(&ck->c.lock));
240 BUG_ON(!six_trylock_write(&ck->c.lock));
247 static struct bkey_cached *
248 bkey_cached_reuse(struct btree_key_cache *c)
250 struct bucket_table *tbl;
251 struct rhash_head *pos;
252 struct bkey_cached *ck;
256 tbl = rht_dereference_rcu(c->table.tbl, &c->table);
257 for (i = 0; i < tbl->size; i++)
258 rht_for_each_entry_rcu(ck, pos, tbl, i, hash) {
259 if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags) &&
260 bkey_cached_lock_for_evict(ck)) {
261 bkey_cached_evict(c, ck);
271 static struct bkey_cached *
272 btree_key_cache_create(struct btree_trans *trans, struct btree_path *path)
274 struct bch_fs *c = trans->c;
275 struct btree_key_cache *bc = &c->btree_key_cache;
276 struct bkey_cached *ck;
279 ck = bkey_cached_alloc(trans, path);
280 if (unlikely(IS_ERR(ck)))
284 ck = bkey_cached_reuse(bc);
286 bch_err(c, "error allocating memory for key cache item, btree %s",
287 bch2_btree_ids[path->btree_id]);
288 return ERR_PTR(-ENOMEM);
291 mark_btree_node_locked(trans, path, 0, SIX_LOCK_intent);
294 if (path->btree_id == BTREE_ID_subvolumes)
295 six_lock_pcpu_alloc(&ck->c.lock);
299 ck->c.btree_id = path->btree_id;
300 ck->key.btree_id = path->btree_id;
301 ck->key.pos = path->pos;
303 ck->flags = 1U << BKEY_CACHED_ACCESSED;
305 if (unlikely(rhashtable_lookup_insert_fast(&bc->table,
307 bch2_btree_key_cache_params))) {
308 /* We raced with another fill: */
310 if (likely(was_new)) {
311 six_unlock_write(&ck->c.lock);
312 six_unlock_intent(&ck->c.lock);
313 mark_btree_node_locked(trans, path, 0, BTREE_NODE_UNLOCKED);
316 bkey_cached_free_fast(bc, ck);
322 atomic_long_inc(&bc->nr_keys);
324 six_unlock_write(&ck->c.lock);
329 static int btree_key_cache_fill(struct btree_trans *trans,
330 struct btree_path *ck_path,
331 struct bkey_cached *ck)
333 struct btree_path *path;
335 unsigned new_u64s = 0;
336 struct bkey_i *new_k = NULL;
340 path = bch2_path_get(trans, ck->key.btree_id,
341 ck->key.pos, 0, 0, 0, _THIS_IP_);
342 ret = bch2_btree_path_traverse(trans, path, 0);
346 k = bch2_btree_path_peek_slot(path, &u);
348 if (!bch2_btree_node_relock(trans, ck_path, 0)) {
349 trace_and_count(trans->c, trans_restart_relock_key_cache_fill, trans, _THIS_IP_, ck_path);
350 ret = btree_trans_restart(trans, BCH_ERR_transaction_restart_key_cache_raced);
355 * bch2_varint_decode can read past the end of the buffer by at
356 * most 7 bytes (it won't be used):
358 new_u64s = k.k->u64s + 1;
361 * Allocate some extra space so that the transaction commit path is less
362 * likely to have to reallocate, since that requires a transaction
365 new_u64s = min(256U, (new_u64s * 3) / 2);
367 if (new_u64s > ck->u64s) {
368 new_u64s = roundup_pow_of_two(new_u64s);
369 new_k = kmalloc(new_u64s * sizeof(u64), GFP_NOFS);
371 bch_err(trans->c, "error allocating memory for key cache key, btree %s u64s %u",
372 bch2_btree_ids[ck->key.btree_id], new_u64s);
378 ret = bch2_btree_node_lock_write(trans, ck_path, &ck_path->l[0].b->c);
390 bkey_reassemble(ck->k, k);
392 bch2_btree_node_unlock_write(trans, ck_path, ck_path->l[0].b);
394 /* We're not likely to need this iterator again: */
395 path->preserve = false;
397 bch2_path_put(trans, path, 0);
401 static int bkey_cached_check_fn(struct six_lock *lock, void *p)
403 struct bkey_cached *ck = container_of(lock, struct bkey_cached, c.lock);
404 const struct btree_path *path = p;
406 if (ck->key.btree_id != path->btree_id &&
407 bpos_cmp(ck->key.pos, path->pos))
408 return BCH_ERR_lock_fail_node_reused;
413 int bch2_btree_path_traverse_cached(struct btree_trans *trans, struct btree_path *path,
416 struct bch_fs *c = trans->c;
417 struct bkey_cached *ck;
424 if (bch2_btree_node_relock(trans, path, 0)) {
425 ck = (void *) path->l[0].b;
429 ck = bch2_btree_key_cache_find(c, path->btree_id, path->pos);
431 ck = btree_key_cache_create(trans, path);
432 ret = PTR_ERR_OR_ZERO(ck);
438 mark_btree_node_locked(trans, path, 0, SIX_LOCK_intent);
439 path->locks_want = 1;
441 enum six_lock_type lock_want = __btree_lock_want(path, 0);
443 ret = btree_node_lock(trans, path, (void *) ck, path->pos, 0,
445 bkey_cached_check_fn, path, _THIS_IP_);
447 if (bch2_err_matches(ret, BCH_ERR_lock_fail_node_reused))
449 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
454 if (ck->key.btree_id != path->btree_id ||
455 bpos_cmp(ck->key.pos, path->pos)) {
456 six_unlock_type(&ck->c.lock, lock_want);
460 mark_btree_node_locked(trans, path, 0, lock_want);
463 path->l[0].lock_seq = ck->c.lock.state.seq;
464 path->l[0].b = (void *) ck;
468 * Using the underscore version because we haven't set
469 * path->uptodate yet:
471 if (!path->locks_want &&
472 !__bch2_btree_path_upgrade(trans, path, 1)) {
473 trace_and_count(trans->c, trans_restart_key_cache_upgrade, trans, _THIS_IP_);
474 ret = btree_trans_restart(trans, BCH_ERR_transaction_restart_key_cache_upgrade);
478 ret = btree_key_cache_fill(trans, path, ck);
483 if (!test_bit(BKEY_CACHED_ACCESSED, &ck->flags))
484 set_bit(BKEY_CACHED_ACCESSED, &ck->flags);
486 path->uptodate = BTREE_ITER_UPTODATE;
488 BUG_ON(btree_node_locked_type(path, 0) != btree_lock_want(path, 0));
492 if (!bch2_err_matches(ret, BCH_ERR_transaction_restart)) {
493 btree_node_unlock(trans, path, 0);
494 path->l[0].b = ERR_PTR(ret);
499 static int btree_key_cache_flush_pos(struct btree_trans *trans,
500 struct bkey_cached_key key,
502 unsigned commit_flags,
505 struct bch_fs *c = trans->c;
506 struct journal *j = &c->journal;
507 struct btree_iter c_iter, b_iter;
508 struct bkey_cached *ck = NULL;
511 bch2_trans_iter_init(trans, &b_iter, key.btree_id, key.pos,
514 BTREE_ITER_ALL_SNAPSHOTS);
515 bch2_trans_iter_init(trans, &c_iter, key.btree_id, key.pos,
518 b_iter.flags &= ~BTREE_ITER_WITH_KEY_CACHE;
520 ret = bch2_btree_iter_traverse(&c_iter);
524 ck = (void *) c_iter.path->l[0].b;
528 if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
536 if (journal_seq && ck->journal.seq != journal_seq)
540 * Since journal reclaim depends on us making progress here, and the
541 * allocator/copygc depend on journal reclaim making progress, we need
542 * to be using alloc reserves:
544 ret = bch2_btree_iter_traverse(&b_iter) ?:
545 bch2_trans_update(trans, &b_iter, ck->k,
546 BTREE_UPDATE_KEY_CACHE_RECLAIM|
547 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE|
548 BTREE_TRIGGER_NORUN) ?:
549 bch2_trans_commit(trans, NULL, NULL,
550 BTREE_INSERT_NOCHECK_RW|
552 BTREE_INSERT_USE_RESERVE|
553 (ck->journal.seq == journal_last_seq(j)
554 ? JOURNAL_WATERMARK_reserved
558 bch2_fs_fatal_err_on(ret &&
559 !bch2_err_matches(ret, BCH_ERR_transaction_restart) &&
560 !bch2_err_matches(ret, BCH_ERR_journal_reclaim_would_deadlock) &&
561 !bch2_journal_error(j), c,
562 "error flushing key cache: %s", bch2_err_str(ret));
566 bch2_journal_pin_drop(j, &ck->journal);
567 bch2_journal_preres_put(j, &ck->res);
569 BUG_ON(!btree_node_locked(c_iter.path, 0));
572 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
573 clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
574 atomic_long_dec(&c->btree_key_cache.nr_dirty);
577 struct btree_path *path2;
579 trans_for_each_path(trans, path2)
580 if (path2 != c_iter.path)
581 __bch2_btree_path_unlock(trans, path2);
583 bch2_btree_node_lock_write_nofail(trans, c_iter.path, &ck->c);
585 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
586 clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
587 atomic_long_dec(&c->btree_key_cache.nr_dirty);
590 mark_btree_node_locked_noreset(c_iter.path, 0, BTREE_NODE_UNLOCKED);
591 bkey_cached_evict(&c->btree_key_cache, ck);
592 bkey_cached_free_fast(&c->btree_key_cache, ck);
595 bch2_trans_iter_exit(trans, &b_iter);
596 bch2_trans_iter_exit(trans, &c_iter);
600 int bch2_btree_key_cache_journal_flush(struct journal *j,
601 struct journal_entry_pin *pin, u64 seq)
603 struct bch_fs *c = container_of(j, struct bch_fs, journal);
604 struct bkey_cached *ck =
605 container_of(pin, struct bkey_cached, journal);
606 struct bkey_cached_key key;
607 struct btree_trans trans;
608 int srcu_idx = srcu_read_lock(&c->btree_trans_barrier);
611 bch2_trans_init(&trans, c, 0, 0);
613 btree_node_lock_nopath_nofail(&trans, &ck->c, SIX_LOCK_read);
616 if (ck->journal.seq != seq ||
617 !test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
618 six_unlock_read(&ck->c.lock);
621 six_unlock_read(&ck->c.lock);
623 ret = commit_do(&trans, NULL, NULL, 0,
624 btree_key_cache_flush_pos(&trans, key, seq,
625 BTREE_INSERT_JOURNAL_RECLAIM, false));
627 srcu_read_unlock(&c->btree_trans_barrier, srcu_idx);
629 bch2_trans_exit(&trans);
634 * Flush and evict a key from the key cache:
636 int bch2_btree_key_cache_flush(struct btree_trans *trans,
637 enum btree_id id, struct bpos pos)
639 struct bch_fs *c = trans->c;
640 struct bkey_cached_key key = { id, pos };
642 /* Fastpath - assume it won't be found: */
643 if (!bch2_btree_key_cache_find(c, id, pos))
646 return btree_key_cache_flush_pos(trans, key, 0, 0, true);
649 bool bch2_btree_insert_key_cached(struct btree_trans *trans,
650 struct btree_path *path,
651 struct bkey_i *insert)
653 struct bch_fs *c = trans->c;
654 struct bkey_cached *ck = (void *) path->l[0].b;
655 bool kick_reclaim = false;
657 BUG_ON(insert->u64s > ck->u64s);
659 if (likely(!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY))) {
662 BUG_ON(jset_u64s(insert->u64s) > trans->journal_preres.u64s);
664 difference = jset_u64s(insert->u64s) - ck->res.u64s;
665 if (difference > 0) {
666 trans->journal_preres.u64s -= difference;
667 ck->res.u64s += difference;
671 bkey_copy(ck->k, insert);
674 if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
675 set_bit(BKEY_CACHED_DIRTY, &ck->flags);
676 atomic_long_inc(&c->btree_key_cache.nr_dirty);
678 if (bch2_nr_btree_keys_need_flush(c))
682 bch2_journal_pin_update(&c->journal, trans->journal_res.seq,
683 &ck->journal, bch2_btree_key_cache_journal_flush);
686 journal_reclaim_kick(&c->journal);
690 void bch2_btree_key_cache_drop(struct btree_trans *trans,
691 struct btree_path *path)
693 struct bch_fs *c = trans->c;
694 struct bkey_cached *ck = (void *) path->l[0].b;
699 * We just did an update to the btree, bypassing the key cache: the key
700 * cache key is now stale and must be dropped, even if dirty:
702 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
703 clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
704 atomic_long_dec(&c->btree_key_cache.nr_dirty);
705 bch2_journal_pin_drop(&c->journal, &ck->journal);
711 static unsigned long bch2_btree_key_cache_scan(struct shrinker *shrink,
712 struct shrink_control *sc)
714 struct bch_fs *c = container_of(shrink, struct bch_fs,
715 btree_key_cache.shrink);
716 struct btree_key_cache *bc = &c->btree_key_cache;
717 struct bucket_table *tbl;
718 struct bkey_cached *ck, *t;
719 size_t scanned = 0, freed = 0, nr = sc->nr_to_scan;
720 unsigned start, flags;
723 /* Return -1 if we can't do anything right now */
724 if (sc->gfp_mask & __GFP_FS)
725 mutex_lock(&bc->lock);
726 else if (!mutex_trylock(&bc->lock))
729 srcu_idx = srcu_read_lock(&c->btree_trans_barrier);
730 flags = memalloc_nofs_save();
733 * Newest freed entries are at the end of the list - once we hit one
734 * that's too new to be freed, we can bail out:
736 list_for_each_entry_safe(ck, t, &bc->freed_nonpcpu, list) {
737 if (!poll_state_synchronize_srcu(&c->btree_trans_barrier,
738 ck->btree_trans_barrier_seq))
742 six_lock_pcpu_free(&ck->c.lock);
743 kmem_cache_free(bch2_key_cache, ck);
744 atomic_long_dec(&bc->nr_freed);
752 list_for_each_entry_safe(ck, t, &bc->freed_pcpu, list) {
753 if (!poll_state_synchronize_srcu(&c->btree_trans_barrier,
754 ck->btree_trans_barrier_seq))
758 six_lock_pcpu_free(&ck->c.lock);
759 kmem_cache_free(bch2_key_cache, ck);
760 atomic_long_dec(&bc->nr_freed);
769 tbl = rht_dereference_rcu(bc->table.tbl, &bc->table);
770 if (bc->shrink_iter >= tbl->size)
772 start = bc->shrink_iter;
775 struct rhash_head *pos, *next;
777 pos = rht_ptr_rcu(rht_bucket(tbl, bc->shrink_iter));
779 while (!rht_is_a_nulls(pos)) {
780 next = rht_dereference_bucket_rcu(pos->next, tbl, bc->shrink_iter);
781 ck = container_of(pos, struct bkey_cached, hash);
783 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags))
786 if (test_bit(BKEY_CACHED_ACCESSED, &ck->flags))
787 clear_bit(BKEY_CACHED_ACCESSED, &ck->flags);
788 else if (bkey_cached_lock_for_evict(ck)) {
789 bkey_cached_evict(bc, ck);
790 bkey_cached_free(bc, ck);
801 if (bc->shrink_iter >= tbl->size)
803 } while (scanned < nr && bc->shrink_iter != start);
807 memalloc_nofs_restore(flags);
808 srcu_read_unlock(&c->btree_trans_barrier, srcu_idx);
809 mutex_unlock(&bc->lock);
814 static unsigned long bch2_btree_key_cache_count(struct shrinker *shrink,
815 struct shrink_control *sc)
817 struct bch_fs *c = container_of(shrink, struct bch_fs,
818 btree_key_cache.shrink);
819 struct btree_key_cache *bc = &c->btree_key_cache;
820 long nr = atomic_long_read(&bc->nr_keys) -
821 atomic_long_read(&bc->nr_dirty);
826 void bch2_fs_btree_key_cache_exit(struct btree_key_cache *bc)
828 struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
829 struct bucket_table *tbl;
830 struct bkey_cached *ck, *n;
831 struct rhash_head *pos;
835 if (bc->shrink.list.next)
836 unregister_shrinker(&bc->shrink);
838 mutex_lock(&bc->lock);
841 tbl = rht_dereference_rcu(bc->table.tbl, &bc->table);
843 for (i = 0; i < tbl->size; i++)
844 rht_for_each_entry_rcu(ck, pos, tbl, i, hash) {
845 bkey_cached_evict(bc, ck);
846 list_add(&ck->list, &bc->freed_nonpcpu);
850 for_each_possible_cpu(cpu) {
851 struct btree_key_cache_freelist *f =
852 per_cpu_ptr(bc->pcpu_freed, cpu);
854 for (i = 0; i < f->nr; i++) {
856 list_add(&ck->list, &bc->freed_nonpcpu);
860 list_splice(&bc->freed_pcpu, &bc->freed_nonpcpu);
862 list_for_each_entry_safe(ck, n, &bc->freed_nonpcpu, list) {
865 bch2_journal_pin_drop(&c->journal, &ck->journal);
866 bch2_journal_preres_put(&c->journal, &ck->res);
870 six_lock_pcpu_free(&ck->c.lock);
871 kmem_cache_free(bch2_key_cache, ck);
874 BUG_ON(atomic_long_read(&bc->nr_dirty) &&
875 !bch2_journal_error(&c->journal) &&
876 test_bit(BCH_FS_WAS_RW, &c->flags));
877 BUG_ON(atomic_long_read(&bc->nr_keys));
879 mutex_unlock(&bc->lock);
881 if (bc->table_init_done)
882 rhashtable_destroy(&bc->table);
884 free_percpu(bc->pcpu_freed);
887 void bch2_fs_btree_key_cache_init_early(struct btree_key_cache *c)
889 mutex_init(&c->lock);
890 INIT_LIST_HEAD(&c->freed_pcpu);
891 INIT_LIST_HEAD(&c->freed_nonpcpu);
894 static void bch2_btree_key_cache_shrinker_to_text(struct printbuf *out, struct shrinker *shrink)
896 struct btree_key_cache *bc =
897 container_of(shrink, struct btree_key_cache, shrink);
899 bch2_btree_key_cache_to_text(out, bc);
902 int bch2_fs_btree_key_cache_init(struct btree_key_cache *c)
906 c->pcpu_freed = alloc_percpu(struct btree_key_cache_freelist);
910 ret = rhashtable_init(&c->table, &bch2_btree_key_cache_params);
914 c->table_init_done = true;
917 c->shrink.count_objects = bch2_btree_key_cache_count;
918 c->shrink.scan_objects = bch2_btree_key_cache_scan;
919 c->shrink.to_text = bch2_btree_key_cache_shrinker_to_text;
920 return register_shrinker(&c->shrink);
923 void bch2_btree_key_cache_to_text(struct printbuf *out, struct btree_key_cache *c)
925 prt_printf(out, "nr_freed:\t%zu\n", atomic_long_read(&c->nr_freed));
926 prt_printf(out, "nr_keys:\t%lu\n", atomic_long_read(&c->nr_keys));
927 prt_printf(out, "nr_dirty:\t%lu\n", atomic_long_read(&c->nr_dirty));
930 void bch2_btree_key_cache_exit(void)
933 kmem_cache_destroy(bch2_key_cache);
936 int __init bch2_btree_key_cache_init(void)
938 bch2_key_cache = KMEM_CACHE(bkey_cached, 0);