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