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