]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_key_cache.c
Update bcachefs sources to a8b3ce7599 fixup! bcachefs: Eliminate more PAGE_SIZE uses
[bcachefs-tools-debian] / libbcachefs / btree_key_cache.c
1
2 #include "bcachefs.h"
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"
8 #include "error.h"
9 #include "journal.h"
10 #include "journal_reclaim.h"
11
12 #include <linux/sched/mm.h>
13 #include <trace/events/bcachefs.h>
14
15 static struct kmem_cache *bch2_key_cache;
16
17 static int bch2_btree_key_cache_cmp_fn(struct rhashtable_compare_arg *arg,
18                                        const void *obj)
19 {
20         const struct bkey_cached *ck = obj;
21         const struct bkey_cached_key *key = arg->key;
22
23         return cmp_int(ck->key.btree_id, key->btree_id) ?:
24                 bpos_cmp(ck->key.pos, key->pos);
25 }
26
27 static const struct rhashtable_params bch2_btree_key_cache_params = {
28         .head_offset    = offsetof(struct bkey_cached, hash),
29         .key_offset     = offsetof(struct bkey_cached, key),
30         .key_len        = sizeof(struct bkey_cached_key),
31         .obj_cmpfn      = bch2_btree_key_cache_cmp_fn,
32 };
33
34 __flatten
35 inline struct bkey_cached *
36 bch2_btree_key_cache_find(struct bch_fs *c, enum btree_id btree_id, struct bpos pos)
37 {
38         struct bkey_cached_key key = {
39                 .btree_id       = btree_id,
40                 .pos            = pos,
41         };
42
43         return rhashtable_lookup_fast(&c->btree_key_cache.table, &key,
44                                       bch2_btree_key_cache_params);
45 }
46
47 static bool bkey_cached_lock_for_evict(struct bkey_cached *ck)
48 {
49         if (!six_trylock_intent(&ck->c.lock))
50                 return false;
51
52         if (!six_trylock_write(&ck->c.lock)) {
53                 six_unlock_intent(&ck->c.lock);
54                 return false;
55         }
56
57         if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
58                 six_unlock_write(&ck->c.lock);
59                 six_unlock_intent(&ck->c.lock);
60                 return false;
61         }
62
63         return true;
64 }
65
66 static void bkey_cached_evict(struct btree_key_cache *c,
67                               struct bkey_cached *ck)
68 {
69         BUG_ON(rhashtable_remove_fast(&c->table, &ck->hash,
70                                       bch2_btree_key_cache_params));
71         memset(&ck->key, ~0, sizeof(ck->key));
72
73         atomic_long_dec(&c->nr_keys);
74 }
75
76 static void bkey_cached_free(struct btree_key_cache *bc,
77                              struct bkey_cached *ck)
78 {
79         struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
80
81         BUG_ON(test_bit(BKEY_CACHED_DIRTY, &ck->flags));
82
83         ck->btree_trans_barrier_seq =
84                 start_poll_synchronize_srcu(&c->btree_trans_barrier);
85
86         list_move_tail(&ck->list, &bc->freed);
87         bc->nr_freed++;
88
89         kfree(ck->k);
90         ck->k           = NULL;
91         ck->u64s        = 0;
92
93         six_unlock_write(&ck->c.lock);
94         six_unlock_intent(&ck->c.lock);
95 }
96
97 static struct bkey_cached *
98 bkey_cached_alloc(struct btree_key_cache *c)
99 {
100         struct bkey_cached *ck;
101
102         ck = kmem_cache_alloc(bch2_key_cache, GFP_NOFS|__GFP_ZERO);
103         if (likely(ck)) {
104                 INIT_LIST_HEAD(&ck->list);
105                 six_lock_init(&ck->c.lock);
106                 BUG_ON(!six_trylock_intent(&ck->c.lock));
107                 BUG_ON(!six_trylock_write(&ck->c.lock));
108                 return ck;
109         }
110
111         return NULL;
112 }
113
114 static struct bkey_cached *
115 bkey_cached_reuse(struct btree_key_cache *c)
116 {
117         struct bucket_table *tbl;
118         struct rhash_head *pos;
119         struct bkey_cached *ck;
120         unsigned i;
121
122         mutex_lock(&c->lock);
123         list_for_each_entry_reverse(ck, &c->freed, list)
124                 if (bkey_cached_lock_for_evict(ck)) {
125                         c->nr_freed--;
126                         list_del(&ck->list);
127                         mutex_unlock(&c->lock);
128                         return ck;
129                 }
130         mutex_unlock(&c->lock);
131
132         rcu_read_lock();
133         tbl = rht_dereference_rcu(c->table.tbl, &c->table);
134         for (i = 0; i < tbl->size; i++)
135                 rht_for_each_entry_rcu(ck, pos, tbl, i, hash) {
136                         if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags) &&
137                             bkey_cached_lock_for_evict(ck)) {
138                                 bkey_cached_evict(c, ck);
139                                 rcu_read_unlock();
140                                 return ck;
141                         }
142                 }
143         rcu_read_unlock();
144
145         return NULL;
146 }
147
148 static struct bkey_cached *
149 btree_key_cache_create(struct btree_key_cache *c,
150                        enum btree_id btree_id,
151                        struct bpos pos)
152 {
153         struct bkey_cached *ck;
154         bool was_new = true;
155
156         ck = bkey_cached_alloc(c);
157
158         if (unlikely(!ck)) {
159                 ck = bkey_cached_reuse(c);
160                 if (unlikely(!ck))
161                         return ERR_PTR(-ENOMEM);
162
163                 was_new = false;
164         }
165
166         ck->c.level             = 0;
167         ck->c.btree_id          = btree_id;
168         ck->key.btree_id        = btree_id;
169         ck->key.pos             = pos;
170         ck->valid               = false;
171         ck->flags               = 1U << BKEY_CACHED_ACCESSED;
172
173         if (unlikely(rhashtable_lookup_insert_fast(&c->table,
174                                           &ck->hash,
175                                           bch2_btree_key_cache_params))) {
176                 /* We raced with another fill: */
177
178                 if (likely(was_new)) {
179                         six_unlock_write(&ck->c.lock);
180                         six_unlock_intent(&ck->c.lock);
181                         kfree(ck);
182                 } else {
183                         mutex_lock(&c->lock);
184                         bkey_cached_free(c, ck);
185                         mutex_unlock(&c->lock);
186                 }
187
188                 return NULL;
189         }
190
191         atomic_long_inc(&c->nr_keys);
192
193         six_unlock_write(&ck->c.lock);
194
195         return ck;
196 }
197
198 static int btree_key_cache_fill(struct btree_trans *trans,
199                                 struct btree_iter *ck_iter,
200                                 struct bkey_cached *ck)
201 {
202         struct btree_iter *iter;
203         struct bkey_s_c k;
204         unsigned new_u64s = 0;
205         struct bkey_i *new_k = NULL;
206         int ret;
207
208         iter = bch2_trans_get_iter(trans, ck->key.btree_id,
209                                    ck->key.pos, BTREE_ITER_SLOTS);
210         k = bch2_btree_iter_peek_slot(iter);
211         ret = bkey_err(k);
212         if (ret)
213                 goto err;
214
215         if (!bch2_btree_node_relock(ck_iter, 0)) {
216                 trace_transaction_restart_ip(trans->ip, _THIS_IP_);
217                 ret = -EINTR;
218                 goto err;
219         }
220
221         if (k.k->u64s > ck->u64s) {
222                 new_u64s = roundup_pow_of_two(k.k->u64s);
223                 new_k = kmalloc(new_u64s * sizeof(u64), GFP_NOFS);
224                 if (!new_k) {
225                         ret = -ENOMEM;
226                         goto err;
227                 }
228         }
229
230         bch2_btree_node_lock_write(ck_iter->l[0].b, ck_iter);
231         if (new_k) {
232                 kfree(ck->k);
233                 ck->u64s = new_u64s;
234                 ck->k = new_k;
235         }
236
237         bkey_reassemble(ck->k, k);
238         ck->valid = true;
239         bch2_btree_node_unlock_write(ck_iter->l[0].b, ck_iter);
240
241         /* We're not likely to need this iterator again: */
242         set_btree_iter_dontneed(trans, iter);
243 err:
244         bch2_trans_iter_put(trans, iter);
245         return ret;
246 }
247
248 static int bkey_cached_check_fn(struct six_lock *lock, void *p)
249 {
250         struct bkey_cached *ck = container_of(lock, struct bkey_cached, c.lock);
251         const struct btree_iter *iter = p;
252
253         return ck->key.btree_id == iter->btree_id &&
254                 !bpos_cmp(ck->key.pos, iter->pos) ? 0 : -1;
255 }
256
257 __flatten
258 int bch2_btree_iter_traverse_cached(struct btree_iter *iter)
259 {
260         struct btree_trans *trans = iter->trans;
261         struct bch_fs *c = trans->c;
262         struct bkey_cached *ck;
263         int ret = 0;
264
265         BUG_ON(iter->level);
266
267         if (btree_node_locked(iter, 0)) {
268                 ck = (void *) iter->l[0].b;
269                 goto fill;
270         }
271 retry:
272         ck = bch2_btree_key_cache_find(c, iter->btree_id, iter->pos);
273         if (!ck) {
274                 if (iter->flags & BTREE_ITER_CACHED_NOCREATE) {
275                         iter->l[0].b = NULL;
276                         return 0;
277                 }
278
279                 ck = btree_key_cache_create(&c->btree_key_cache,
280                                             iter->btree_id, iter->pos);
281                 ret = PTR_ERR_OR_ZERO(ck);
282                 if (ret)
283                         goto err;
284                 if (!ck)
285                         goto retry;
286
287                 mark_btree_node_locked(iter, 0, SIX_LOCK_intent);
288                 iter->locks_want = 1;
289         } else {
290                 enum six_lock_type lock_want = __btree_lock_want(iter, 0);
291
292                 if (!btree_node_lock((void *) ck, iter->pos, 0, iter, lock_want,
293                                      bkey_cached_check_fn, iter, _THIS_IP_)) {
294                         if (ck->key.btree_id != iter->btree_id ||
295                             bpos_cmp(ck->key.pos, iter->pos)) {
296                                 goto retry;
297                         }
298
299                         trace_transaction_restart_ip(trans->ip, _THIS_IP_);
300                         ret = -EINTR;
301                         goto err;
302                 }
303
304                 if (ck->key.btree_id != iter->btree_id ||
305                     bpos_cmp(ck->key.pos, iter->pos)) {
306                         six_unlock_type(&ck->c.lock, lock_want);
307                         goto retry;
308                 }
309
310                 mark_btree_node_locked(iter, 0, lock_want);
311         }
312
313         iter->l[0].lock_seq     = ck->c.lock.state.seq;
314         iter->l[0].b            = (void *) ck;
315 fill:
316         if (!ck->valid && !(iter->flags & BTREE_ITER_CACHED_NOFILL)) {
317                 if (!btree_node_intent_locked(iter, 0))
318                         bch2_btree_iter_upgrade(iter, 1);
319                 if (!btree_node_intent_locked(iter, 0)) {
320                         trace_transaction_restart_ip(trans->ip, _THIS_IP_);
321                         ret = -EINTR;
322                         goto err;
323                 }
324
325                 ret = btree_key_cache_fill(trans, iter, ck);
326                 if (ret)
327                         goto err;
328         }
329
330         if (!test_bit(BKEY_CACHED_ACCESSED, &ck->flags))
331                 set_bit(BKEY_CACHED_ACCESSED, &ck->flags);
332
333         iter->uptodate = BTREE_ITER_NEED_PEEK;
334
335         if (!(iter->flags & BTREE_ITER_INTENT))
336                 bch2_btree_iter_downgrade(iter);
337         else if (!iter->locks_want) {
338                 if (!__bch2_btree_iter_upgrade(iter, 1))
339                         ret = -EINTR;
340         }
341
342         return ret;
343 err:
344         if (ret != -EINTR) {
345                 btree_node_unlock(iter, 0);
346                 iter->flags |= BTREE_ITER_ERROR;
347                 iter->l[0].b = BTREE_ITER_NO_NODE_ERROR;
348         }
349         return ret;
350 }
351
352 static int btree_key_cache_flush_pos(struct btree_trans *trans,
353                                      struct bkey_cached_key key,
354                                      u64 journal_seq,
355                                      unsigned commit_flags,
356                                      bool evict)
357 {
358         struct bch_fs *c = trans->c;
359         struct journal *j = &c->journal;
360         struct btree_iter *c_iter = NULL, *b_iter = NULL;
361         struct bkey_cached *ck = NULL;
362         int ret;
363
364         b_iter = bch2_trans_get_iter(trans, key.btree_id, key.pos,
365                                      BTREE_ITER_SLOTS|
366                                      BTREE_ITER_INTENT);
367         c_iter = bch2_trans_get_iter(trans, key.btree_id, key.pos,
368                                      BTREE_ITER_CACHED|
369                                      BTREE_ITER_CACHED_NOFILL|
370                                      BTREE_ITER_CACHED_NOCREATE|
371                                      BTREE_ITER_INTENT);
372 retry:
373         ret = bch2_btree_iter_traverse(c_iter);
374         if (ret)
375                 goto err;
376
377         ck = (void *) c_iter->l[0].b;
378         if (!ck ||
379             (journal_seq && ck->journal.seq != journal_seq))
380                 goto out;
381
382         if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
383                 if (!evict)
384                         goto out;
385                 goto evict;
386         }
387
388         ret   = bch2_btree_iter_traverse(b_iter) ?:
389                 bch2_trans_update(trans, b_iter, ck->k, BTREE_TRIGGER_NORUN) ?:
390                 bch2_trans_commit(trans, NULL, NULL,
391                                   BTREE_INSERT_NOUNLOCK|
392                                   BTREE_INSERT_NOCHECK_RW|
393                                   BTREE_INSERT_NOFAIL|
394                                   (ck->journal.seq == journal_last_seq(j)
395                                    ? BTREE_INSERT_JOURNAL_RESERVED
396                                    : 0)|
397                                   commit_flags);
398 err:
399         if (ret == -EINTR)
400                 goto retry;
401
402         if (ret == -EAGAIN)
403                 goto out;
404
405         if (ret) {
406                 bch2_fs_fatal_err_on(!bch2_journal_error(j), c,
407                         "error flushing key cache: %i", ret);
408                 goto out;
409         }
410
411         bch2_journal_pin_drop(j, &ck->journal);
412         bch2_journal_preres_put(j, &ck->res);
413
414         BUG_ON(!btree_node_locked(c_iter, 0));
415
416         if (!evict) {
417                 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
418                         clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
419                         atomic_long_dec(&c->btree_key_cache.nr_dirty);
420                 }
421         } else {
422 evict:
423                 BUG_ON(!btree_node_intent_locked(c_iter, 0));
424
425                 mark_btree_node_unlocked(c_iter, 0);
426                 c_iter->l[0].b = NULL;
427
428                 six_lock_write(&ck->c.lock, NULL, NULL);
429
430                 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
431                         clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
432                         atomic_long_dec(&c->btree_key_cache.nr_dirty);
433                 }
434
435                 bkey_cached_evict(&c->btree_key_cache, ck);
436
437                 mutex_lock(&c->btree_key_cache.lock);
438                 bkey_cached_free(&c->btree_key_cache, ck);
439                 mutex_unlock(&c->btree_key_cache.lock);
440         }
441 out:
442         bch2_trans_iter_put(trans, b_iter);
443         bch2_trans_iter_put(trans, c_iter);
444         return ret;
445 }
446
447 int bch2_btree_key_cache_journal_flush(struct journal *j,
448                                 struct journal_entry_pin *pin, u64 seq)
449 {
450         struct bch_fs *c = container_of(j, struct bch_fs, journal);
451         struct bkey_cached *ck =
452                 container_of(pin, struct bkey_cached, journal);
453         struct bkey_cached_key key;
454         struct btree_trans trans;
455         int ret = 0;
456
457         int srcu_idx = srcu_read_lock(&c->btree_trans_barrier);
458
459         six_lock_read(&ck->c.lock, NULL, NULL);
460         key = ck->key;
461
462         if (ck->journal.seq != seq ||
463             !test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
464                 six_unlock_read(&ck->c.lock);
465                 goto unlock;
466         }
467         six_unlock_read(&ck->c.lock);
468
469         bch2_trans_init(&trans, c, 0, 0);
470         ret = btree_key_cache_flush_pos(&trans, key, seq,
471                                   BTREE_INSERT_JOURNAL_RECLAIM, false);
472         bch2_trans_exit(&trans);
473 unlock:
474         srcu_read_unlock(&c->btree_trans_barrier, srcu_idx);
475
476         return ret;
477 }
478
479 /*
480  * Flush and evict a key from the key cache:
481  */
482 int bch2_btree_key_cache_flush(struct btree_trans *trans,
483                                enum btree_id id, struct bpos pos)
484 {
485         struct bch_fs *c = trans->c;
486         struct bkey_cached_key key = { id, pos };
487
488         /* Fastpath - assume it won't be found: */
489         if (!bch2_btree_key_cache_find(c, id, pos))
490                 return 0;
491
492         return btree_key_cache_flush_pos(trans, key, 0, 0, true);
493 }
494
495 bool bch2_btree_insert_key_cached(struct btree_trans *trans,
496                                   struct btree_iter *iter,
497                                   struct bkey_i *insert)
498 {
499         struct bch_fs *c = trans->c;
500         struct bkey_cached *ck = (void *) iter->l[0].b;
501         bool kick_reclaim = false;
502
503         BUG_ON(insert->u64s > ck->u64s);
504
505         if (likely(!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY))) {
506                 int difference;
507
508                 BUG_ON(jset_u64s(insert->u64s) > trans->journal_preres.u64s);
509
510                 difference = jset_u64s(insert->u64s) - ck->res.u64s;
511                 if (difference > 0) {
512                         trans->journal_preres.u64s      -= difference;
513                         ck->res.u64s                    += difference;
514                 }
515         }
516
517         bkey_copy(ck->k, insert);
518         ck->valid = true;
519
520         if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
521                 set_bit(BKEY_CACHED_DIRTY, &ck->flags);
522                 atomic_long_inc(&c->btree_key_cache.nr_dirty);
523
524                 if (bch2_nr_btree_keys_need_flush(c))
525                         kick_reclaim = true;
526         }
527
528         bch2_journal_pin_update(&c->journal, trans->journal_res.seq,
529                                 &ck->journal, bch2_btree_key_cache_journal_flush);
530
531         if (kick_reclaim)
532                 journal_reclaim_kick(&c->journal);
533         return true;
534 }
535
536 #ifdef CONFIG_BCACHEFS_DEBUG
537 void bch2_btree_key_cache_verify_clean(struct btree_trans *trans,
538                                enum btree_id id, struct bpos pos)
539 {
540         BUG_ON(bch2_btree_key_cache_find(trans->c, id, pos));
541 }
542 #endif
543
544 static unsigned long bch2_btree_key_cache_scan(struct shrinker *shrink,
545                                            struct shrink_control *sc)
546 {
547         struct bch_fs *c = container_of(shrink, struct bch_fs,
548                                         btree_key_cache.shrink);
549         struct btree_key_cache *bc = &c->btree_key_cache;
550         struct bucket_table *tbl;
551         struct bkey_cached *ck, *t;
552         size_t scanned = 0, freed = 0, nr = sc->nr_to_scan;
553         unsigned start, flags;
554         int srcu_idx;
555
556         /* Return -1 if we can't do anything right now */
557         if (sc->gfp_mask & __GFP_FS)
558                 mutex_lock(&bc->lock);
559         else if (!mutex_trylock(&bc->lock))
560                 return -1;
561
562         srcu_idx = srcu_read_lock(&c->btree_trans_barrier);
563         flags = memalloc_nofs_save();
564
565         /*
566          * Newest freed entries are at the end of the list - once we hit one
567          * that's too new to be freed, we can bail out:
568          */
569         list_for_each_entry_safe(ck, t, &bc->freed, list) {
570                 if (!poll_state_synchronize_srcu(&c->btree_trans_barrier,
571                                                  ck->btree_trans_barrier_seq))
572                         break;
573
574                 list_del(&ck->list);
575                 kmem_cache_free(bch2_key_cache, ck);
576                 bc->nr_freed--;
577                 scanned++;
578                 freed++;
579         }
580
581         if (scanned >= nr)
582                 goto out;
583
584         rcu_read_lock();
585         tbl = rht_dereference_rcu(bc->table.tbl, &bc->table);
586         if (bc->shrink_iter >= tbl->size)
587                 bc->shrink_iter = 0;
588         start = bc->shrink_iter;
589
590         do {
591                 struct rhash_head *pos, *next;
592
593                 pos = rht_ptr_rcu(rht_bucket(tbl, bc->shrink_iter));
594
595                 while (!rht_is_a_nulls(pos)) {
596                         next = rht_dereference_bucket_rcu(pos->next, tbl, bc->shrink_iter);
597                         ck = container_of(pos, struct bkey_cached, hash);
598
599                         if (test_bit(BKEY_CACHED_DIRTY, &ck->flags))
600                                 goto next;
601
602                         if (test_bit(BKEY_CACHED_ACCESSED, &ck->flags))
603                                 clear_bit(BKEY_CACHED_ACCESSED, &ck->flags);
604                         else if (bkey_cached_lock_for_evict(ck)) {
605                                 bkey_cached_evict(bc, ck);
606                                 bkey_cached_free(bc, ck);
607                         }
608
609                         scanned++;
610                         if (scanned >= nr)
611                                 break;
612 next:
613                         pos = next;
614                 }
615
616                 bc->shrink_iter++;
617                 if (bc->shrink_iter >= tbl->size)
618                         bc->shrink_iter = 0;
619         } while (scanned < nr && bc->shrink_iter != start);
620
621         rcu_read_unlock();
622 out:
623         memalloc_nofs_restore(flags);
624         srcu_read_unlock(&c->btree_trans_barrier, srcu_idx);
625         mutex_unlock(&bc->lock);
626
627         return freed;
628 }
629
630 static unsigned long bch2_btree_key_cache_count(struct shrinker *shrink,
631                                             struct shrink_control *sc)
632 {
633         struct bch_fs *c = container_of(shrink, struct bch_fs,
634                                         btree_key_cache.shrink);
635         struct btree_key_cache *bc = &c->btree_key_cache;
636
637         return atomic_long_read(&bc->nr_keys);
638 }
639
640 void bch2_fs_btree_key_cache_exit(struct btree_key_cache *bc)
641 {
642         struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
643         struct bucket_table *tbl;
644         struct bkey_cached *ck, *n;
645         struct rhash_head *pos;
646         unsigned i;
647
648         if (bc->shrink.list.next)
649                 unregister_shrinker(&bc->shrink);
650
651         mutex_lock(&bc->lock);
652
653         rcu_read_lock();
654         tbl = rht_dereference_rcu(bc->table.tbl, &bc->table);
655         for (i = 0; i < tbl->size; i++)
656                 rht_for_each_entry_rcu(ck, pos, tbl, i, hash) {
657                         bkey_cached_evict(bc, ck);
658                         list_add(&ck->list, &bc->freed);
659                 }
660         rcu_read_unlock();
661
662         list_for_each_entry_safe(ck, n, &bc->freed, list) {
663                 cond_resched();
664
665                 bch2_journal_pin_drop(&c->journal, &ck->journal);
666                 bch2_journal_preres_put(&c->journal, &ck->res);
667
668                 list_del(&ck->list);
669                 kfree(ck->k);
670                 kmem_cache_free(bch2_key_cache, ck);
671         }
672
673         BUG_ON(atomic_long_read(&bc->nr_dirty) && !bch2_journal_error(&c->journal));
674         BUG_ON(atomic_long_read(&bc->nr_keys));
675
676         mutex_unlock(&bc->lock);
677
678         if (bc->table_init_done)
679                 rhashtable_destroy(&bc->table);
680 }
681
682 void bch2_fs_btree_key_cache_init_early(struct btree_key_cache *c)
683 {
684         mutex_init(&c->lock);
685         INIT_LIST_HEAD(&c->freed);
686 }
687
688 int bch2_fs_btree_key_cache_init(struct btree_key_cache *c)
689 {
690         int ret;
691
692         ret = rhashtable_init(&c->table, &bch2_btree_key_cache_params);
693         if (ret)
694                 return ret;
695
696         c->table_init_done = true;
697
698         c->shrink.seeks                 = 1;
699         c->shrink.count_objects         = bch2_btree_key_cache_count;
700         c->shrink.scan_objects          = bch2_btree_key_cache_scan;
701         return register_shrinker(&c->shrink);
702 }
703
704 void bch2_btree_key_cache_to_text(struct printbuf *out, struct btree_key_cache *c)
705 {
706         pr_buf(out, "nr_freed:\t%zu\n", c->nr_freed);
707         pr_buf(out, "nr_keys:\t%zu\n",  atomic_long_read(&c->nr_keys));
708         pr_buf(out, "nr_dirty:\t%zu\n", atomic_long_read(&c->nr_dirty));
709 }
710
711 void bch2_btree_key_cache_exit(void)
712 {
713         if (bch2_key_cache)
714                 kmem_cache_destroy(bch2_key_cache);
715 }
716
717 int __init bch2_btree_key_cache_init(void)
718 {
719         bch2_key_cache = KMEM_CACHE(bkey_cached, 0);
720         if (!bch2_key_cache)
721                 return -ENOMEM;
722
723         return 0;
724 }