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