]> git.sesse.net Git - bcachefs-tools-debian/blob - libbcachefs/btree_key_cache.c
Update bcachefs sources to 3f3f969859 bcachefs: Fix some compiler warnings
[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_path *ck_path,
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         bch2_trans_iter_init(trans, &iter, 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(trans, ck_path, 0)) {
216                 trace_transaction_restart_ip(trans->ip, _THIS_IP_);
217                 ret = btree_trans_restart(trans);
218                 goto err;
219         }
220
221         /*
222          * bch2_varint_decode can read past the end of the buffer by at
223          * most 7 bytes (it won't be used):
224          */
225         new_u64s = k.k->u64s + 1;
226
227         if (new_u64s > ck->u64s) {
228                 new_u64s = roundup_pow_of_two(new_u64s);
229                 new_k = kmalloc(new_u64s * sizeof(u64), GFP_NOFS);
230                 if (!new_k) {
231                         ret = -ENOMEM;
232                         goto err;
233                 }
234         }
235
236         /*
237          * XXX: not allowed to be holding read locks when we take a write lock,
238          * currently
239          */
240         bch2_btree_node_lock_write(trans, ck_path, ck_path->l[0].b);
241         if (new_k) {
242                 kfree(ck->k);
243                 ck->u64s = new_u64s;
244                 ck->k = new_k;
245         }
246
247         bkey_reassemble(ck->k, k);
248         ck->valid = true;
249         bch2_btree_node_unlock_write(trans, ck_path, ck_path->l[0].b);
250
251         /* We're not likely to need this iterator again: */
252         set_btree_iter_dontneed(&iter);
253 err:
254         bch2_trans_iter_exit(trans, &iter);
255         return ret;
256 }
257
258 static int bkey_cached_check_fn(struct six_lock *lock, void *p)
259 {
260         struct bkey_cached *ck = container_of(lock, struct bkey_cached, c.lock);
261         const struct btree_path *path = p;
262
263         return ck->key.btree_id == path->btree_id &&
264                 !bpos_cmp(ck->key.pos, path->pos) ? 0 : -1;
265 }
266
267 __flatten
268 int bch2_btree_path_traverse_cached(struct btree_trans *trans, struct btree_path *path,
269                                     unsigned flags)
270 {
271         struct bch_fs *c = trans->c;
272         struct bkey_cached *ck;
273         int ret = 0;
274
275         BUG_ON(path->level);
276
277         path->l[1].b = NULL;
278
279         if (bch2_btree_node_relock(trans, path, 0)) {
280                 ck = (void *) path->l[0].b;
281                 goto fill;
282         }
283 retry:
284         ck = bch2_btree_key_cache_find(c, path->btree_id, path->pos);
285         if (!ck) {
286                 if (flags & BTREE_ITER_CACHED_NOCREATE) {
287                         path->l[0].b = NULL;
288                         return 0;
289                 }
290
291                 ck = btree_key_cache_create(&c->btree_key_cache,
292                                             path->btree_id, path->pos);
293                 ret = PTR_ERR_OR_ZERO(ck);
294                 if (ret)
295                         goto err;
296                 if (!ck)
297                         goto retry;
298
299                 mark_btree_node_locked(trans, path, 0, SIX_LOCK_intent);
300                 path->locks_want = 1;
301         } else {
302                 enum six_lock_type lock_want = __btree_lock_want(path, 0);
303
304                 if (!btree_node_lock(trans, path, (void *) ck, path->pos, 0,
305                                      lock_want,
306                                      bkey_cached_check_fn, path, _THIS_IP_)) {
307                         if (!trans->restarted)
308                                 goto retry;
309
310                         trace_transaction_restart_ip(trans->ip, _THIS_IP_);
311                         ret = -EINTR;
312                         goto err;
313                 }
314
315                 if (ck->key.btree_id != path->btree_id ||
316                     bpos_cmp(ck->key.pos, path->pos)) {
317                         six_unlock_type(&ck->c.lock, lock_want);
318                         goto retry;
319                 }
320
321                 mark_btree_node_locked(trans, path, 0, lock_want);
322         }
323
324         path->l[0].lock_seq     = ck->c.lock.state.seq;
325         path->l[0].b            = (void *) ck;
326 fill:
327         if (!ck->valid && !(flags & BTREE_ITER_CACHED_NOFILL)) {
328                 if (!path->locks_want &&
329                     !__bch2_btree_path_upgrade(trans, path, 1)) {
330                         trace_transaction_restart_ip(trans->ip, _THIS_IP_);
331                         ret = btree_trans_restart(trans);
332                         goto err;
333                 }
334
335                 ret = btree_key_cache_fill(trans, path, ck);
336                 if (ret)
337                         goto err;
338         }
339
340         if (!test_bit(BKEY_CACHED_ACCESSED, &ck->flags))
341                 set_bit(BKEY_CACHED_ACCESSED, &ck->flags);
342
343         path->uptodate = BTREE_ITER_UPTODATE;
344         BUG_ON(btree_node_locked_type(path, 0) != btree_lock_want(path, 0));
345
346         return ret;
347 err:
348         if (ret != -EINTR) {
349                 btree_node_unlock(path, 0);
350                 path->l[0].b = BTREE_ITER_NO_NODE_ERROR;
351         }
352         return ret;
353 }
354
355 static int btree_key_cache_flush_pos(struct btree_trans *trans,
356                                      struct bkey_cached_key key,
357                                      u64 journal_seq,
358                                      unsigned commit_flags,
359                                      bool evict)
360 {
361         struct bch_fs *c = trans->c;
362         struct journal *j = &c->journal;
363         struct btree_iter c_iter, b_iter;
364         struct bkey_cached *ck = NULL;
365         int ret;
366
367         bch2_trans_iter_init(trans, &b_iter, key.btree_id, key.pos,
368                              BTREE_ITER_SLOTS|
369                              BTREE_ITER_INTENT);
370         bch2_trans_iter_init(trans, &c_iter, key.btree_id, key.pos,
371                              BTREE_ITER_CACHED|
372                              BTREE_ITER_CACHED_NOFILL|
373                              BTREE_ITER_CACHED_NOCREATE|
374                              BTREE_ITER_INTENT);
375         ret = bch2_btree_iter_traverse(&c_iter);
376         if (ret)
377                 goto out;
378
379         ck = (void *) c_iter.path->l[0].b;
380         if (!ck ||
381             (journal_seq && ck->journal.seq != journal_seq))
382                 goto out;
383
384         if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
385                 if (!evict)
386                         goto out;
387                 goto evict;
388         }
389
390         /*
391          * Since journal reclaim depends on us making progress here, and the
392          * allocator/copygc depend on journal reclaim making progress, we need
393          * to be using alloc reserves:
394          * */
395         ret   = bch2_btree_iter_traverse(&b_iter) ?:
396                 bch2_trans_update(trans, &b_iter, ck->k,
397                                   BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE|
398                                   BTREE_TRIGGER_NORUN) ?:
399                 bch2_trans_commit(trans, NULL, NULL,
400                                   BTREE_INSERT_NOCHECK_RW|
401                                   BTREE_INSERT_NOFAIL|
402                                   BTREE_INSERT_USE_RESERVE|
403                                   (ck->journal.seq == journal_last_seq(j)
404                                    ? BTREE_INSERT_JOURNAL_RESERVED
405                                    : 0)|
406                                   commit_flags);
407         if (ret) {
408                 bch2_fs_fatal_err_on(ret != -EINTR &&
409                                      ret != -EAGAIN &&
410                                      !bch2_journal_error(j), c,
411                         "error flushing key cache: %i", ret);
412                 goto out;
413         }
414
415         bch2_journal_pin_drop(j, &ck->journal);
416         bch2_journal_preres_put(j, &ck->res);
417
418         BUG_ON(!btree_node_locked(c_iter.path, 0));
419
420         if (!evict) {
421                 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
422                         clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
423                         atomic_long_dec(&c->btree_key_cache.nr_dirty);
424                 }
425         } else {
426 evict:
427                 BUG_ON(!btree_node_intent_locked(c_iter.path, 0));
428
429                 mark_btree_node_unlocked(c_iter.path, 0);
430                 c_iter.path->l[0].b = NULL;
431
432                 six_lock_write(&ck->c.lock, NULL, NULL);
433
434                 if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
435                         clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
436                         atomic_long_dec(&c->btree_key_cache.nr_dirty);
437                 }
438
439                 bkey_cached_evict(&c->btree_key_cache, ck);
440
441                 mutex_lock(&c->btree_key_cache.lock);
442                 bkey_cached_free(&c->btree_key_cache, ck);
443                 mutex_unlock(&c->btree_key_cache.lock);
444         }
445 out:
446         bch2_trans_iter_exit(trans, &b_iter);
447         bch2_trans_iter_exit(trans, &c_iter);
448         return ret;
449 }
450
451 int bch2_btree_key_cache_journal_flush(struct journal *j,
452                                 struct journal_entry_pin *pin, u64 seq)
453 {
454         struct bch_fs *c = container_of(j, struct bch_fs, journal);
455         struct bkey_cached *ck =
456                 container_of(pin, struct bkey_cached, journal);
457         struct bkey_cached_key key;
458         int ret = 0;
459
460         int srcu_idx = srcu_read_lock(&c->btree_trans_barrier);
461
462         six_lock_read(&ck->c.lock, NULL, NULL);
463         key = ck->key;
464
465         if (ck->journal.seq != seq ||
466             !test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
467                 six_unlock_read(&ck->c.lock);
468                 goto unlock;
469         }
470         six_unlock_read(&ck->c.lock);
471
472         ret = bch2_trans_do(c, NULL, NULL, 0,
473                 btree_key_cache_flush_pos(&trans, key, seq,
474                                 BTREE_INSERT_JOURNAL_RECLAIM, false));
475 unlock:
476         srcu_read_unlock(&c->btree_trans_barrier, srcu_idx);
477
478         return ret;
479 }
480
481 /*
482  * Flush and evict a key from the key cache:
483  */
484 int bch2_btree_key_cache_flush(struct btree_trans *trans,
485                                enum btree_id id, struct bpos pos)
486 {
487         struct bch_fs *c = trans->c;
488         struct bkey_cached_key key = { id, pos };
489
490         /* Fastpath - assume it won't be found: */
491         if (!bch2_btree_key_cache_find(c, id, pos))
492                 return 0;
493
494         return btree_key_cache_flush_pos(trans, key, 0, 0, true);
495 }
496
497 bool bch2_btree_insert_key_cached(struct btree_trans *trans,
498                                   struct btree_path *path,
499                                   struct bkey_i *insert)
500 {
501         struct bch_fs *c = trans->c;
502         struct bkey_cached *ck = (void *) path->l[0].b;
503         bool kick_reclaim = false;
504
505         BUG_ON(insert->u64s > ck->u64s);
506
507         if (likely(!(trans->flags & BTREE_INSERT_JOURNAL_REPLAY))) {
508                 int difference;
509
510                 BUG_ON(jset_u64s(insert->u64s) > trans->journal_preres.u64s);
511
512                 difference = jset_u64s(insert->u64s) - ck->res.u64s;
513                 if (difference > 0) {
514                         trans->journal_preres.u64s      -= difference;
515                         ck->res.u64s                    += difference;
516                 }
517         }
518
519         bkey_copy(ck->k, insert);
520         ck->valid = true;
521
522         if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
523                 set_bit(BKEY_CACHED_DIRTY, &ck->flags);
524                 atomic_long_inc(&c->btree_key_cache.nr_dirty);
525
526                 if (bch2_nr_btree_keys_need_flush(c))
527                         kick_reclaim = true;
528         }
529
530         bch2_journal_pin_update(&c->journal, trans->journal_res.seq,
531                                 &ck->journal, bch2_btree_key_cache_journal_flush);
532
533         if (kick_reclaim)
534                 journal_reclaim_kick(&c->journal);
535         return true;
536 }
537
538 #ifdef CONFIG_BCACHEFS_DEBUG
539 void bch2_btree_key_cache_verify_clean(struct btree_trans *trans,
540                                enum btree_id id, struct bpos pos)
541 {
542         BUG_ON(bch2_btree_key_cache_find(trans->c, id, pos));
543 }
544 #endif
545
546 static unsigned long bch2_btree_key_cache_scan(struct shrinker *shrink,
547                                            struct shrink_control *sc)
548 {
549         struct bch_fs *c = container_of(shrink, struct bch_fs,
550                                         btree_key_cache.shrink);
551         struct btree_key_cache *bc = &c->btree_key_cache;
552         struct bucket_table *tbl;
553         struct bkey_cached *ck, *t;
554         size_t scanned = 0, freed = 0, nr = sc->nr_to_scan;
555         unsigned start, flags;
556         int srcu_idx;
557
558         /* Return -1 if we can't do anything right now */
559         if (sc->gfp_mask & __GFP_FS)
560                 mutex_lock(&bc->lock);
561         else if (!mutex_trylock(&bc->lock))
562                 return -1;
563
564         srcu_idx = srcu_read_lock(&c->btree_trans_barrier);
565         flags = memalloc_nofs_save();
566
567         /*
568          * Newest freed entries are at the end of the list - once we hit one
569          * that's too new to be freed, we can bail out:
570          */
571         list_for_each_entry_safe(ck, t, &bc->freed, list) {
572                 if (!poll_state_synchronize_srcu(&c->btree_trans_barrier,
573                                                  ck->btree_trans_barrier_seq))
574                         break;
575
576                 list_del(&ck->list);
577                 kmem_cache_free(bch2_key_cache, ck);
578                 bc->nr_freed--;
579                 scanned++;
580                 freed++;
581         }
582
583         if (scanned >= nr)
584                 goto out;
585
586         rcu_read_lock();
587         tbl = rht_dereference_rcu(bc->table.tbl, &bc->table);
588         if (bc->shrink_iter >= tbl->size)
589                 bc->shrink_iter = 0;
590         start = bc->shrink_iter;
591
592         do {
593                 struct rhash_head *pos, *next;
594
595                 pos = rht_ptr_rcu(rht_bucket(tbl, bc->shrink_iter));
596
597                 while (!rht_is_a_nulls(pos)) {
598                         next = rht_dereference_bucket_rcu(pos->next, tbl, bc->shrink_iter);
599                         ck = container_of(pos, struct bkey_cached, hash);
600
601                         if (test_bit(BKEY_CACHED_DIRTY, &ck->flags))
602                                 goto next;
603
604                         if (test_bit(BKEY_CACHED_ACCESSED, &ck->flags))
605                                 clear_bit(BKEY_CACHED_ACCESSED, &ck->flags);
606                         else if (bkey_cached_lock_for_evict(ck)) {
607                                 bkey_cached_evict(bc, ck);
608                                 bkey_cached_free(bc, ck);
609                         }
610
611                         scanned++;
612                         if (scanned >= nr)
613                                 break;
614 next:
615                         pos = next;
616                 }
617
618                 bc->shrink_iter++;
619                 if (bc->shrink_iter >= tbl->size)
620                         bc->shrink_iter = 0;
621         } while (scanned < nr && bc->shrink_iter != start);
622
623         rcu_read_unlock();
624 out:
625         memalloc_nofs_restore(flags);
626         srcu_read_unlock(&c->btree_trans_barrier, srcu_idx);
627         mutex_unlock(&bc->lock);
628
629         return freed;
630 }
631
632 static unsigned long bch2_btree_key_cache_count(struct shrinker *shrink,
633                                             struct shrink_control *sc)
634 {
635         struct bch_fs *c = container_of(shrink, struct bch_fs,
636                                         btree_key_cache.shrink);
637         struct btree_key_cache *bc = &c->btree_key_cache;
638         long nr = atomic_long_read(&bc->nr_keys) -
639                 atomic_long_read(&bc->nr_dirty);
640
641         return max(0L, nr);
642 }
643
644 void bch2_fs_btree_key_cache_exit(struct btree_key_cache *bc)
645 {
646         struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
647         struct bucket_table *tbl;
648         struct bkey_cached *ck, *n;
649         struct rhash_head *pos;
650         unsigned i;
651
652         if (bc->shrink.list.next)
653                 unregister_shrinker(&bc->shrink);
654
655         mutex_lock(&bc->lock);
656
657         rcu_read_lock();
658         tbl = rht_dereference_rcu(bc->table.tbl, &bc->table);
659         for (i = 0; i < tbl->size; i++)
660                 rht_for_each_entry_rcu(ck, pos, tbl, i, hash) {
661                         bkey_cached_evict(bc, ck);
662                         list_add(&ck->list, &bc->freed);
663                 }
664         rcu_read_unlock();
665
666         list_for_each_entry_safe(ck, n, &bc->freed, list) {
667                 cond_resched();
668
669                 bch2_journal_pin_drop(&c->journal, &ck->journal);
670                 bch2_journal_preres_put(&c->journal, &ck->res);
671
672                 list_del(&ck->list);
673                 kfree(ck->k);
674                 kmem_cache_free(bch2_key_cache, ck);
675         }
676
677         BUG_ON(atomic_long_read(&bc->nr_dirty) &&
678                !bch2_journal_error(&c->journal) &&
679                test_bit(BCH_FS_WAS_RW, &c->flags));
680         BUG_ON(atomic_long_read(&bc->nr_keys));
681
682         mutex_unlock(&bc->lock);
683
684         if (bc->table_init_done)
685                 rhashtable_destroy(&bc->table);
686 }
687
688 void bch2_fs_btree_key_cache_init_early(struct btree_key_cache *c)
689 {
690         mutex_init(&c->lock);
691         INIT_LIST_HEAD(&c->freed);
692 }
693
694 int bch2_fs_btree_key_cache_init(struct btree_key_cache *c)
695 {
696         int ret;
697
698         ret = rhashtable_init(&c->table, &bch2_btree_key_cache_params);
699         if (ret)
700                 return ret;
701
702         c->table_init_done = true;
703
704         c->shrink.seeks                 = 1;
705         c->shrink.count_objects         = bch2_btree_key_cache_count;
706         c->shrink.scan_objects          = bch2_btree_key_cache_scan;
707         return register_shrinker(&c->shrink);
708 }
709
710 void bch2_btree_key_cache_to_text(struct printbuf *out, struct btree_key_cache *c)
711 {
712         pr_buf(out, "nr_freed:\t%zu\n", c->nr_freed);
713         pr_buf(out, "nr_keys:\t%zu\n",  atomic_long_read(&c->nr_keys));
714         pr_buf(out, "nr_dirty:\t%zu\n", atomic_long_read(&c->nr_dirty));
715 }
716
717 void bch2_btree_key_cache_exit(void)
718 {
719         if (bch2_key_cache)
720                 kmem_cache_destroy(bch2_key_cache);
721 }
722
723 int __init bch2_btree_key_cache_init(void)
724 {
725         bch2_key_cache = KMEM_CACHE(bkey_cached, 0);
726         if (!bch2_key_cache)
727                 return -ENOMEM;
728
729         return 0;
730 }