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