]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/btree_key_cache.c
Update bcachefs sources to 90a9c61e2b bcachefs: Switch bch2_btree_delete_range()...
[bcachefs-tools-debian] / libbcachefs / btree_key_cache.c
index 2230da8b3acdefe87f9a22d42184df80bdaa9faa..fa90581f7ade7d6869e5bc8244e489f9d819bfaf 100644 (file)
@@ -5,6 +5,7 @@
 #include "btree_key_cache.h"
 #include "btree_locking.h"
 #include "btree_update.h"
+#include "errcode.h"
 #include "error.h"
 #include "journal.h"
 #include "journal_reclaim.h"
@@ -21,7 +22,7 @@ static int bch2_btree_key_cache_cmp_fn(struct rhashtable_compare_arg *arg,
        const struct bkey_cached_key *key = arg->key;
 
        return cmp_int(ck->key.btree_id, key->btree_id) ?:
-               bkey_cmp(ck->key.pos, key->pos);
+               bpos_cmp(ck->key.pos, key->pos);
 }
 
 static const struct rhashtable_params bch2_btree_key_cache_params = {
@@ -70,7 +71,7 @@ static void bkey_cached_evict(struct btree_key_cache *c,
                                      bch2_btree_key_cache_params));
        memset(&ck->key, ~0, sizeof(ck->key));
 
-       c->nr_keys--;
+       atomic_long_dec(&c->nr_keys);
 }
 
 static void bkey_cached_free(struct btree_key_cache *bc,
@@ -84,7 +85,7 @@ static void bkey_cached_free(struct btree_key_cache *bc,
                start_poll_synchronize_srcu(&c->btree_trans_barrier);
 
        list_move_tail(&ck->list, &bc->freed);
-       bc->nr_freed++;
+       atomic_long_inc(&bc->nr_freed);
 
        kfree(ck->k);
        ck->k           = NULL;
@@ -94,17 +95,89 @@ static void bkey_cached_free(struct btree_key_cache *bc,
        six_unlock_intent(&ck->c.lock);
 }
 
+static void bkey_cached_free_fast(struct btree_key_cache *bc,
+                                 struct bkey_cached *ck)
+{
+       struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
+       struct btree_key_cache_freelist *f;
+       bool freed = false;
+
+       BUG_ON(test_bit(BKEY_CACHED_DIRTY, &ck->flags));
+
+       ck->btree_trans_barrier_seq =
+               start_poll_synchronize_srcu(&c->btree_trans_barrier);
+
+       list_del_init(&ck->list);
+       atomic_long_inc(&bc->nr_freed);
+
+       kfree(ck->k);
+       ck->k           = NULL;
+       ck->u64s        = 0;
+
+       preempt_disable();
+       f = this_cpu_ptr(bc->pcpu_freed);
+
+       if (f->nr < ARRAY_SIZE(f->objs)) {
+               f->objs[f->nr++] = ck;
+               freed = true;
+       }
+       preempt_enable();
+
+       if (!freed) {
+               mutex_lock(&bc->lock);
+               preempt_disable();
+               f = this_cpu_ptr(bc->pcpu_freed);
+
+               while (f->nr > ARRAY_SIZE(f->objs) / 2) {
+                       struct bkey_cached *ck2 = f->objs[--f->nr];
+
+                       list_move_tail(&ck2->list, &bc->freed);
+               }
+               preempt_enable();
+
+               list_move_tail(&ck->list, &bc->freed);
+               mutex_unlock(&bc->lock);
+       }
+
+       six_unlock_write(&ck->c.lock);
+       six_unlock_intent(&ck->c.lock);
+}
+
 static struct bkey_cached *
 bkey_cached_alloc(struct btree_key_cache *c)
 {
-       struct bkey_cached *ck;
+       struct bkey_cached *ck = NULL;
+       struct btree_key_cache_freelist *f;
+
+       preempt_disable();
+       f = this_cpu_ptr(c->pcpu_freed);
+       if (f->nr)
+               ck = f->objs[--f->nr];
+       preempt_enable();
 
-       list_for_each_entry_reverse(ck, &c->freed, list)
-               if (bkey_cached_lock_for_evict(ck)) {
-                       c->nr_freed--;
-                       return ck;
+       if (!ck) {
+               mutex_lock(&c->lock);
+               preempt_disable();
+               f = this_cpu_ptr(c->pcpu_freed);
+
+               while (!list_empty(&c->freed) &&
+                      f->nr < ARRAY_SIZE(f->objs) / 2) {
+                       ck = list_last_entry(&c->freed, struct bkey_cached, list);
+                       list_del_init(&ck->list);
+                       f->objs[f->nr++] = ck;
                }
 
+               ck = f->nr ? f->objs[--f->nr] : NULL;
+               preempt_enable();
+               mutex_unlock(&c->lock);
+       }
+
+       if (ck) {
+               six_lock_intent(&ck->c.lock, NULL, NULL);
+               six_lock_write(&ck->c.lock, NULL, NULL);
+               return ck;
+       }
+
        ck = kmem_cache_alloc(bch2_key_cache, GFP_NOFS|__GFP_ZERO);
        if (likely(ck)) {
                INIT_LIST_HEAD(&ck->list);
@@ -114,25 +187,59 @@ bkey_cached_alloc(struct btree_key_cache *c)
                return ck;
        }
 
-       list_for_each_entry(ck, &c->clean, list)
-               if (bkey_cached_lock_for_evict(ck)) {
-                       bkey_cached_evict(c, ck);
-                       return ck;
+       return NULL;
+}
+
+static struct bkey_cached *
+bkey_cached_reuse(struct btree_key_cache *c)
+{
+       struct bucket_table *tbl;
+       struct rhash_head *pos;
+       struct bkey_cached *ck;
+       unsigned i;
+
+       rcu_read_lock();
+       tbl = rht_dereference_rcu(c->table.tbl, &c->table);
+       for (i = 0; i < tbl->size; i++)
+               rht_for_each_entry_rcu(ck, pos, tbl, i, hash) {
+                       if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags) &&
+                           bkey_cached_lock_for_evict(ck)) {
+                               bkey_cached_evict(c, ck);
+                               rcu_read_unlock();
+                               return ck;
+                       }
                }
+       rcu_read_unlock();
 
        return NULL;
 }
 
 static struct bkey_cached *
-btree_key_cache_create(struct btree_key_cache *c,
+btree_key_cache_create(struct bch_fs *c,
                       enum btree_id btree_id,
                       struct bpos pos)
 {
+       struct btree_key_cache *bc = &c->btree_key_cache;
        struct bkey_cached *ck;
+       bool was_new = true;
 
-       ck = bkey_cached_alloc(c);
-       if (!ck)
-               return ERR_PTR(-ENOMEM);
+       ck = bkey_cached_alloc(bc);
+
+       if (unlikely(!ck)) {
+               ck = bkey_cached_reuse(bc);
+               if (unlikely(!ck)) {
+                       bch_err(c, "error allocating memory for key cache item, btree %s",
+                               bch2_btree_ids[btree_id]);
+                       return ERR_PTR(-ENOMEM);
+               }
+
+               was_new = false;
+       } else {
+               if (btree_id == BTREE_ID_subvolumes)
+                       six_lock_pcpu_alloc(&ck->c.lock);
+               else
+                       six_lock_pcpu_free(&ck->c.lock);
+       }
 
        ck->c.level             = 0;
        ck->c.btree_id          = btree_id;
@@ -141,57 +248,83 @@ btree_key_cache_create(struct btree_key_cache *c,
        ck->valid               = false;
        ck->flags               = 1U << BKEY_CACHED_ACCESSED;
 
-       if (rhashtable_lookup_insert_fast(&c->table,
+       if (unlikely(rhashtable_lookup_insert_fast(&bc->table,
                                          &ck->hash,
-                                         bch2_btree_key_cache_params)) {
+                                         bch2_btree_key_cache_params))) {
                /* We raced with another fill: */
-               bkey_cached_free(c, ck);
+
+               if (likely(was_new)) {
+                       six_unlock_write(&ck->c.lock);
+                       six_unlock_intent(&ck->c.lock);
+                       kfree(ck);
+               } else {
+                       bkey_cached_free_fast(bc, ck);
+               }
+
                return NULL;
        }
 
-       c->nr_keys++;
+       atomic_long_inc(&bc->nr_keys);
 
-       list_move(&ck->list, &c->clean);
        six_unlock_write(&ck->c.lock);
 
        return ck;
 }
 
 static int btree_key_cache_fill(struct btree_trans *trans,
-                               struct btree_iter *ck_iter,
+                               struct btree_path *ck_path,
                                struct bkey_cached *ck)
 {
-       struct btree_iter *iter;
+       struct btree_path *path;
        struct bkey_s_c k;
        unsigned new_u64s = 0;
        struct bkey_i *new_k = NULL;
+       struct bkey u;
        int ret;
 
-       iter = bch2_trans_get_iter(trans, ck->key.btree_id,
-                                  ck->key.pos, BTREE_ITER_SLOTS);
-       k = bch2_btree_iter_peek_slot(iter);
-       ret = bkey_err(k);
-       if (ret) {
-               bch2_trans_iter_put(trans, iter);
-               return ret;
-       }
+       path = bch2_path_get(trans, ck->key.btree_id,
+                            ck->key.pos, 0, 0, 0, _THIS_IP_);
+       ret = bch2_btree_path_traverse(trans, path, 0);
+       if (ret)
+               goto err;
 
-       if (!bch2_btree_node_relock(ck_iter, 0)) {
-               bch2_trans_iter_put(trans, iter);
-               trace_transaction_restart_ip(trans->ip, _THIS_IP_);
-               return -EINTR;
+       k = bch2_btree_path_peek_slot(path, &u);
+
+       if (!bch2_btree_node_relock(trans, ck_path, 0)) {
+               trace_trans_restart_relock_key_cache_fill(trans, _THIS_IP_, ck_path);
+               ret = btree_trans_restart(trans, BCH_ERR_transaction_restart_key_cache_raced);
+               goto err;
        }
 
-       if (k.k->u64s > ck->u64s) {
-               new_u64s = roundup_pow_of_two(k.k->u64s);
+       /*
+        * bch2_varint_decode can read past the end of the buffer by at
+        * most 7 bytes (it won't be used):
+        */
+       new_u64s = k.k->u64s + 1;
+
+       /*
+        * Allocate some extra space so that the transaction commit path is less
+        * likely to have to reallocate, since that requires a transaction
+        * restart:
+        */
+       new_u64s = min(256U, (new_u64s * 3) / 2);
+
+       if (new_u64s > ck->u64s) {
+               new_u64s = roundup_pow_of_two(new_u64s);
                new_k = kmalloc(new_u64s * sizeof(u64), GFP_NOFS);
                if (!new_k) {
-                       bch2_trans_iter_put(trans, iter);
-                       return -ENOMEM;
+                       bch_err(trans->c, "error allocating memory for key cache key, btree %s u64s %u",
+                               bch2_btree_ids[ck->key.btree_id], new_u64s);
+                       ret = -ENOMEM;
+                       goto err;
                }
        }
 
-       bch2_btree_node_lock_write(ck_iter->l[0].b, ck_iter);
+       /*
+        * XXX: not allowed to be holding read locks when we take a write lock,
+        * currently
+        */
+       bch2_btree_node_lock_write(trans, ck_path, ck_path->l[0].b);
        if (new_k) {
                kfree(ck->k);
                ck->u64s = new_u64s;
@@ -200,95 +333,93 @@ static int btree_key_cache_fill(struct btree_trans *trans,
 
        bkey_reassemble(ck->k, k);
        ck->valid = true;
-       bch2_btree_node_unlock_write(ck_iter->l[0].b, ck_iter);
+       bch2_btree_node_unlock_write(trans, ck_path, ck_path->l[0].b);
 
        /* We're not likely to need this iterator again: */
-       bch2_trans_iter_free(trans, iter);
-
-       return 0;
+       path->preserve = false;
+err:
+       bch2_path_put(trans, path, 0);
+       return ret;
 }
 
 static int bkey_cached_check_fn(struct six_lock *lock, void *p)
 {
        struct bkey_cached *ck = container_of(lock, struct bkey_cached, c.lock);
-       const struct btree_iter *iter = p;
+       const struct btree_path *path = p;
 
-       return ck->key.btree_id == iter->btree_id &&
-               !bkey_cmp(ck->key.pos, iter->pos) ? 0 : -1;
+       if (ck->key.btree_id != path->btree_id &&
+           bpos_cmp(ck->key.pos, path->pos))
+               return BCH_ERR_lock_fail_node_reused;
+       return 0;
 }
 
 __flatten
-int bch2_btree_iter_traverse_cached(struct btree_iter *iter)
+int bch2_btree_path_traverse_cached(struct btree_trans *trans, struct btree_path *path,
+                                   unsigned flags)
 {
-       struct btree_trans *trans = iter->trans;
        struct bch_fs *c = trans->c;
        struct bkey_cached *ck;
        int ret = 0;
 
-       BUG_ON(iter->level);
+       BUG_ON(path->level);
+
+       path->l[1].b = NULL;
 
-       if (btree_node_locked(iter, 0)) {
-               ck = (void *) iter->l[0].b;
+       if (bch2_btree_node_relock(trans, path, 0)) {
+               ck = (void *) path->l[0].b;
                goto fill;
        }
 retry:
-       ck = bch2_btree_key_cache_find(c, iter->btree_id, iter->pos);
+       ck = bch2_btree_key_cache_find(c, path->btree_id, path->pos);
        if (!ck) {
-               if (iter->flags & BTREE_ITER_CACHED_NOCREATE) {
-                       iter->l[0].b = NULL;
-                       return 0;
-               }
-
-               mutex_lock(&c->btree_key_cache.lock);
-               ck = btree_key_cache_create(&c->btree_key_cache,
-                                           iter->btree_id, iter->pos);
-               mutex_unlock(&c->btree_key_cache.lock);
-
+               ck = btree_key_cache_create(c, path->btree_id, path->pos);
                ret = PTR_ERR_OR_ZERO(ck);
                if (ret)
                        goto err;
                if (!ck)
                        goto retry;
 
-               mark_btree_node_locked(iter, 0, SIX_LOCK_intent);
-               iter->locks_want = 1;
+               mark_btree_node_locked(trans, path, 0, SIX_LOCK_intent);
+               path->locks_want = 1;
        } else {
-               enum six_lock_type lock_want = __btree_lock_want(iter, 0);
+               enum six_lock_type lock_want = __btree_lock_want(path, 0);
 
-               if (!btree_node_lock((void *) ck, iter->pos, 0, iter, lock_want,
-                                    bkey_cached_check_fn, iter, _THIS_IP_)) {
-                       if (ck->key.btree_id != iter->btree_id ||
-                           bkey_cmp(ck->key.pos, iter->pos)) {
+               ret = btree_node_lock(trans, path, (void *) ck, path->pos, 0,
+                                     lock_want,
+                                     bkey_cached_check_fn, path, _THIS_IP_);
+               if (ret) {
+                       if (bch2_err_matches(ret, BCH_ERR_lock_fail_node_reused))
                                goto retry;
-                       }
-
-                       trace_transaction_restart_ip(trans->ip, _THIS_IP_);
-                       ret = -EINTR;
-                       goto err;
+                       if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
+                               goto err;
+                       BUG();
                }
 
-               if (ck->key.btree_id != iter->btree_id ||
-                   bkey_cmp(ck->key.pos, iter->pos)) {
+               if (ck->key.btree_id != path->btree_id ||
+                   bpos_cmp(ck->key.pos, path->pos)) {
                        six_unlock_type(&ck->c.lock, lock_want);
                        goto retry;
                }
 
-               mark_btree_node_locked(iter, 0, lock_want);
+               mark_btree_node_locked(trans, path, 0, lock_want);
        }
 
-       iter->l[0].lock_seq     = ck->c.lock.state.seq;
-       iter->l[0].b            = (void *) ck;
+       path->l[0].lock_seq     = ck->c.lock.state.seq;
+       path->l[0].b            = (void *) ck;
 fill:
-       if (!ck->valid && !(iter->flags & BTREE_ITER_CACHED_NOFILL)) {
-               if (!btree_node_intent_locked(iter, 0))
-                       bch2_btree_iter_upgrade(iter, 1);
-               if (!btree_node_intent_locked(iter, 0)) {
-                       trace_transaction_restart_ip(trans->ip, _THIS_IP_);
-                       ret = -EINTR;
+       if (!ck->valid) {
+               /*
+                * Using the underscore version because we haven't set
+                * path->uptodate yet:
+                */
+               if (!path->locks_want &&
+                   !__bch2_btree_path_upgrade(trans, path, 1)) {
+                       trace_transaction_restart_key_cache_upgrade(trans, _THIS_IP_);
+                       ret = btree_trans_restart(trans, BCH_ERR_transaction_restart_key_cache_upgrade);
                        goto err;
                }
 
-               ret = btree_key_cache_fill(trans, iter, ck);
+               ret = btree_key_cache_fill(trans, path, ck);
                if (ret)
                        goto err;
        }
@@ -296,21 +427,15 @@ fill:
        if (!test_bit(BKEY_CACHED_ACCESSED, &ck->flags))
                set_bit(BKEY_CACHED_ACCESSED, &ck->flags);
 
-       iter->uptodate = BTREE_ITER_NEED_PEEK;
-
-       if (!(iter->flags & BTREE_ITER_INTENT))
-               bch2_btree_iter_downgrade(iter);
-       else if (!iter->locks_want) {
-               if (!__bch2_btree_iter_upgrade(iter, 1))
-                       ret = -EINTR;
-       }
+       path->uptodate = BTREE_ITER_UPTODATE;
+       BUG_ON(!ck->valid);
+       BUG_ON(btree_node_locked_type(path, 0) != btree_lock_want(path, 0));
 
        return ret;
 err:
-       if (ret != -EINTR) {
-               btree_node_unlock(iter, 0);
-               iter->flags |= BTREE_ITER_ERROR;
-               iter->l[0].b = BTREE_ITER_NO_NODE_ERROR;
+       if (!bch2_err_matches(ret, BCH_ERR_transaction_restart)) {
+               btree_node_unlock(trans, path, 0);
+               path->l[0].b = ERR_PTR(ret);
        }
        return ret;
 }
@@ -318,102 +443,112 @@ err:
 static int btree_key_cache_flush_pos(struct btree_trans *trans,
                                     struct bkey_cached_key key,
                                     u64 journal_seq,
+                                    unsigned commit_flags,
                                     bool evict)
 {
        struct bch_fs *c = trans->c;
        struct journal *j = &c->journal;
-       struct btree_iter *c_iter = NULL, *b_iter = NULL;
+       struct btree_iter c_iter, b_iter;
        struct bkey_cached *ck = NULL;
        int ret;
 
-       b_iter = bch2_trans_get_iter(trans, key.btree_id, key.pos,
-                                    BTREE_ITER_SLOTS|
-                                    BTREE_ITER_INTENT);
-       c_iter = bch2_trans_get_iter(trans, key.btree_id, key.pos,
-                                    BTREE_ITER_CACHED|
-                                    BTREE_ITER_CACHED_NOFILL|
-                                    BTREE_ITER_CACHED_NOCREATE|
-                                    BTREE_ITER_INTENT);
-retry:
-       ret = bch2_btree_iter_traverse(c_iter);
+       bch2_trans_iter_init(trans, &b_iter, key.btree_id, key.pos,
+                            BTREE_ITER_SLOTS|
+                            BTREE_ITER_INTENT|
+                            BTREE_ITER_ALL_SNAPSHOTS);
+       bch2_trans_iter_init(trans, &c_iter, key.btree_id, key.pos,
+                            BTREE_ITER_CACHED|
+                            BTREE_ITER_INTENT);
+       b_iter.flags &= ~BTREE_ITER_WITH_KEY_CACHE;
+
+       ret = bch2_btree_iter_traverse(&c_iter);
        if (ret)
-               goto err;
+               goto out;
 
-       ck = (void *) c_iter->l[0].b;
-       if (!ck ||
-           (journal_seq && ck->journal.seq != journal_seq))
+       ck = (void *) c_iter.path->l[0].b;
+       if (!ck)
                goto out;
 
        if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
-               if (!evict)
-                       goto out;
-               goto evict;
+               if (evict)
+                       goto evict;
+               goto out;
        }
 
-       ret   = bch2_btree_iter_traverse(b_iter) ?:
-               bch2_trans_update(trans, b_iter, ck->k, BTREE_TRIGGER_NORUN) ?:
+       BUG_ON(!ck->valid);
+
+       if (journal_seq && ck->journal.seq != journal_seq)
+               goto out;
+
+       /*
+        * Since journal reclaim depends on us making progress here, and the
+        * allocator/copygc depend on journal reclaim making progress, we need
+        * to be using alloc reserves:
+        * */
+       ret   = bch2_btree_iter_traverse(&b_iter) ?:
+               bch2_trans_update(trans, &b_iter, ck->k,
+                                 BTREE_UPDATE_KEY_CACHE_RECLAIM|
+                                 BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE|
+                                 BTREE_TRIGGER_NORUN) ?:
                bch2_trans_commit(trans, NULL, NULL,
-                                 BTREE_INSERT_NOUNLOCK|
                                  BTREE_INSERT_NOCHECK_RW|
                                  BTREE_INSERT_NOFAIL|
-                                 BTREE_INSERT_JOURNAL_RESERVED|
-                                 BTREE_INSERT_JOURNAL_RECLAIM);
-err:
-       if (ret == -EINTR)
-               goto retry;
-
-       if (ret) {
-               bch2_fs_fatal_err_on(!bch2_journal_error(j), c,
-                       "error flushing key cache: %i", ret);
+                                 BTREE_INSERT_USE_RESERVE|
+                                 (ck->journal.seq == journal_last_seq(j)
+                                  ? JOURNAL_WATERMARK_reserved
+                                  : 0)|
+                                 commit_flags);
+
+       bch2_fs_fatal_err_on(ret &&
+                            !bch2_err_matches(ret, BCH_ERR_transaction_restart) &&
+                            !bch2_err_matches(ret, BCH_ERR_journal_reclaim_would_deadlock) &&
+                            !bch2_journal_error(j), c,
+                            "error flushing key cache: %s", bch2_err_str(ret));
+       if (ret)
                goto out;
-       }
 
        bch2_journal_pin_drop(j, &ck->journal);
        bch2_journal_preres_put(j, &ck->res);
 
+       BUG_ON(!btree_node_locked(c_iter.path, 0));
+
        if (!evict) {
-               mutex_lock(&c->btree_key_cache.lock);
                if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
                        clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
-                       c->btree_key_cache.nr_dirty--;
+                       atomic_long_dec(&c->btree_key_cache.nr_dirty);
                }
-
-               list_move_tail(&ck->list, &c->btree_key_cache.clean);
-               mutex_unlock(&c->btree_key_cache.lock);
        } else {
 evict:
-               BUG_ON(!btree_node_intent_locked(c_iter, 0));
+               BUG_ON(!btree_node_intent_locked(c_iter.path, 0));
 
-               mark_btree_node_unlocked(c_iter, 0);
-               c_iter->l[0].b = NULL;
+               mark_btree_node_unlocked(c_iter.path, 0);
+               c_iter.path->l[0].b = NULL;
 
                six_lock_write(&ck->c.lock, NULL, NULL);
 
-               mutex_lock(&c->btree_key_cache.lock);
                if (test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
                        clear_bit(BKEY_CACHED_DIRTY, &ck->flags);
-                       c->btree_key_cache.nr_dirty--;
+                       atomic_long_dec(&c->btree_key_cache.nr_dirty);
                }
 
                bkey_cached_evict(&c->btree_key_cache, ck);
-               bkey_cached_free(&c->btree_key_cache, ck);
-               mutex_unlock(&c->btree_key_cache.lock);
+
+               bkey_cached_free_fast(&c->btree_key_cache, ck);
        }
 out:
-       bch2_trans_iter_put(trans, b_iter);
-       bch2_trans_iter_put(trans, c_iter);
+       bch2_trans_iter_exit(trans, &b_iter);
+       bch2_trans_iter_exit(trans, &c_iter);
        return ret;
 }
 
-static void btree_key_cache_journal_flush(struct journal *j,
-                                         struct journal_entry_pin *pin,
-                                         u64 seq)
+int bch2_btree_key_cache_journal_flush(struct journal *j,
+                               struct journal_entry_pin *pin, u64 seq)
 {
        struct bch_fs *c = container_of(j, struct bch_fs, journal);
        struct bkey_cached *ck =
                container_of(pin, struct bkey_cached, journal);
        struct bkey_cached_key key;
-       struct btree_trans trans;
+       int ret = 0;
 
        int srcu_idx = srcu_read_lock(&c->btree_trans_barrier);
 
@@ -427,11 +562,13 @@ static void btree_key_cache_journal_flush(struct journal *j,
        }
        six_unlock_read(&ck->c.lock);
 
-       bch2_trans_init(&trans, c, 0, 0);
-       btree_key_cache_flush_pos(&trans, key, seq, false);
-       bch2_trans_exit(&trans);
+       ret = bch2_trans_do(c, NULL, NULL, 0,
+               btree_key_cache_flush_pos(&trans, key, seq,
+                               BTREE_INSERT_JOURNAL_RECLAIM, false));
 unlock:
        srcu_read_unlock(&c->btree_trans_barrier, srcu_idx);
+
+       return ret;
 }
 
 /*
@@ -447,15 +584,15 @@ int bch2_btree_key_cache_flush(struct btree_trans *trans,
        if (!bch2_btree_key_cache_find(c, id, pos))
                return 0;
 
-       return btree_key_cache_flush_pos(trans, key, 0, true);
+       return btree_key_cache_flush_pos(trans, key, 0, 0, true);
 }
 
 bool bch2_btree_insert_key_cached(struct btree_trans *trans,
-                                 struct btree_iter *iter,
+                                 struct btree_path *path,
                                  struct bkey_i *insert)
 {
        struct bch_fs *c = trans->c;
-       struct bkey_cached *ck = (void *) iter->l[0].b;
+       struct bkey_cached *ck = (void *) path->l[0].b;
        bool kick_reclaim = false;
 
        BUG_ON(insert->u64s > ck->u64s);
@@ -476,33 +613,30 @@ bool bch2_btree_insert_key_cached(struct btree_trans *trans,
        ck->valid = true;
 
        if (!test_bit(BKEY_CACHED_DIRTY, &ck->flags)) {
-               mutex_lock(&c->btree_key_cache.lock);
-               list_move(&ck->list, &c->btree_key_cache.dirty);
-
                set_bit(BKEY_CACHED_DIRTY, &ck->flags);
-               c->btree_key_cache.nr_dirty++;
+               atomic_long_inc(&c->btree_key_cache.nr_dirty);
 
                if (bch2_nr_btree_keys_need_flush(c))
                        kick_reclaim = true;
-
-               mutex_unlock(&c->btree_key_cache.lock);
        }
 
        bch2_journal_pin_update(&c->journal, trans->journal_res.seq,
-                               &ck->journal, btree_key_cache_journal_flush);
+                               &ck->journal, bch2_btree_key_cache_journal_flush);
 
        if (kick_reclaim)
                journal_reclaim_kick(&c->journal);
        return true;
 }
 
-#ifdef CONFIG_BCACHEFS_DEBUG
-void bch2_btree_key_cache_verify_clean(struct btree_trans *trans,
-                              enum btree_id id, struct bpos pos)
+void bch2_btree_key_cache_drop(struct btree_trans *trans,
+                              struct btree_path *path)
 {
-       BUG_ON(bch2_btree_key_cache_find(trans->c, id, pos));
+       struct bkey_cached *ck = (void *) path->l[0].b;
+
+       ck->valid = false;
+
+       BUG_ON(test_bit(BKEY_CACHED_DIRTY, &ck->flags));
 }
-#endif
 
 static unsigned long bch2_btree_key_cache_scan(struct shrinker *shrink,
                                           struct shrink_control *sc)
@@ -510,9 +644,11 @@ static unsigned long bch2_btree_key_cache_scan(struct shrinker *shrink,
        struct bch_fs *c = container_of(shrink, struct bch_fs,
                                        btree_key_cache.shrink);
        struct btree_key_cache *bc = &c->btree_key_cache;
+       struct bucket_table *tbl;
        struct bkey_cached *ck, *t;
        size_t scanned = 0, freed = 0, nr = sc->nr_to_scan;
-       unsigned flags;
+       unsigned start, flags;
+       int srcu_idx;
 
        /* Return -1 if we can't do anything right now */
        if (sc->gfp_mask & __GFP_FS)
@@ -520,6 +656,7 @@ static unsigned long bch2_btree_key_cache_scan(struct shrinker *shrink,
        else if (!mutex_trylock(&bc->lock))
                return -1;
 
+       srcu_idx = srcu_read_lock(&c->btree_trans_barrier);
        flags = memalloc_nofs_save();
 
        /*
@@ -533,7 +670,7 @@ static unsigned long bch2_btree_key_cache_scan(struct shrinker *shrink,
 
                list_del(&ck->list);
                kmem_cache_free(bch2_key_cache, ck);
-               bc->nr_freed--;
+               atomic_long_dec(&bc->nr_freed);
                scanned++;
                freed++;
        }
@@ -541,23 +678,47 @@ static unsigned long bch2_btree_key_cache_scan(struct shrinker *shrink,
        if (scanned >= nr)
                goto out;
 
-       list_for_each_entry_safe(ck, t, &bc->clean, list) {
-               if (test_bit(BKEY_CACHED_ACCESSED, &ck->flags))
-                       clear_bit(BKEY_CACHED_ACCESSED, &ck->flags);
-               else if (bkey_cached_lock_for_evict(ck)) {
-                       bkey_cached_evict(bc, ck);
-                       bkey_cached_free(bc, ck);
-               }
+       rcu_read_lock();
+       tbl = rht_dereference_rcu(bc->table.tbl, &bc->table);
+       if (bc->shrink_iter >= tbl->size)
+               bc->shrink_iter = 0;
+       start = bc->shrink_iter;
 
-               scanned++;
-               if (scanned >= nr) {
-                       if (&t->list != &bc->clean)
-                               list_move_tail(&bc->clean, &t->list);
-                       goto out;
+       do {
+               struct rhash_head *pos, *next;
+
+               pos = rht_ptr_rcu(rht_bucket(tbl, bc->shrink_iter));
+
+               while (!rht_is_a_nulls(pos)) {
+                       next = rht_dereference_bucket_rcu(pos->next, tbl, bc->shrink_iter);
+                       ck = container_of(pos, struct bkey_cached, hash);
+
+                       if (test_bit(BKEY_CACHED_DIRTY, &ck->flags))
+                               goto next;
+
+                       if (test_bit(BKEY_CACHED_ACCESSED, &ck->flags))
+                               clear_bit(BKEY_CACHED_ACCESSED, &ck->flags);
+                       else if (bkey_cached_lock_for_evict(ck)) {
+                               bkey_cached_evict(bc, ck);
+                               bkey_cached_free(bc, ck);
+                       }
+
+                       scanned++;
+                       if (scanned >= nr)
+                               break;
+next:
+                       pos = next;
                }
-       }
+
+               bc->shrink_iter++;
+               if (bc->shrink_iter >= tbl->size)
+                       bc->shrink_iter = 0;
+       } while (scanned < nr && bc->shrink_iter != start);
+
+       rcu_read_unlock();
 out:
        memalloc_nofs_restore(flags);
+       srcu_read_unlock(&c->btree_trans_barrier, srcu_idx);
        mutex_unlock(&bc->lock);
 
        return freed;
@@ -569,81 +730,110 @@ static unsigned long bch2_btree_key_cache_count(struct shrinker *shrink,
        struct bch_fs *c = container_of(shrink, struct bch_fs,
                                        btree_key_cache.shrink);
        struct btree_key_cache *bc = &c->btree_key_cache;
+       long nr = atomic_long_read(&bc->nr_keys) -
+               atomic_long_read(&bc->nr_dirty);
 
-       return bc->nr_keys;
+       return max(0L, nr);
 }
 
 void bch2_fs_btree_key_cache_exit(struct btree_key_cache *bc)
 {
        struct bch_fs *c = container_of(bc, struct bch_fs, btree_key_cache);
+       struct bucket_table *tbl;
        struct bkey_cached *ck, *n;
+       struct rhash_head *pos;
+       unsigned i;
+       int cpu;
 
        if (bc->shrink.list.next)
                unregister_shrinker(&bc->shrink);
 
        mutex_lock(&bc->lock);
-       list_splice(&bc->dirty, &bc->clean);
 
-       list_for_each_entry_safe(ck, n, &bc->clean, list) {
-               cond_resched();
+       rcu_read_lock();
+       tbl = rht_dereference_rcu(bc->table.tbl, &bc->table);
+       if (tbl)
+               for (i = 0; i < tbl->size; i++)
+                       rht_for_each_entry_rcu(ck, pos, tbl, i, hash) {
+                               bkey_cached_evict(bc, ck);
+                               list_add(&ck->list, &bc->freed);
+                       }
+       rcu_read_unlock();
 
-               bch2_journal_pin_drop(&c->journal, &ck->journal);
-               bch2_journal_preres_put(&c->journal, &ck->res);
+       for_each_possible_cpu(cpu) {
+               struct btree_key_cache_freelist *f =
+                       per_cpu_ptr(bc->pcpu_freed, cpu);
 
-               kfree(ck->k);
-               list_del(&ck->list);
-               kmem_cache_free(bch2_key_cache, ck);
-               bc->nr_keys--;
+               for (i = 0; i < f->nr; i++) {
+                       ck = f->objs[i];
+                       list_add(&ck->list, &bc->freed);
+               }
        }
 
-       BUG_ON(bc->nr_dirty && !bch2_journal_error(&c->journal));
-       BUG_ON(bc->nr_keys);
-
        list_for_each_entry_safe(ck, n, &bc->freed, list) {
                cond_resched();
 
+               bch2_journal_pin_drop(&c->journal, &ck->journal);
+               bch2_journal_preres_put(&c->journal, &ck->res);
+
                list_del(&ck->list);
+               kfree(ck->k);
                kmem_cache_free(bch2_key_cache, ck);
        }
+
+       BUG_ON(atomic_long_read(&bc->nr_dirty) &&
+              !bch2_journal_error(&c->journal) &&
+              test_bit(BCH_FS_WAS_RW, &c->flags));
+       BUG_ON(atomic_long_read(&bc->nr_keys));
+
        mutex_unlock(&bc->lock);
 
        if (bc->table_init_done)
                rhashtable_destroy(&bc->table);
+
+       free_percpu(bc->pcpu_freed);
 }
 
 void bch2_fs_btree_key_cache_init_early(struct btree_key_cache *c)
 {
        mutex_init(&c->lock);
        INIT_LIST_HEAD(&c->freed);
-       INIT_LIST_HEAD(&c->clean);
-       INIT_LIST_HEAD(&c->dirty);
+}
+
+static void bch2_btree_key_cache_shrinker_to_text(struct printbuf *out, struct shrinker *shrink)
+{
+       struct btree_key_cache *bc =
+               container_of(shrink, struct btree_key_cache, shrink);
+
+       bch2_btree_key_cache_to_text(out, bc);
 }
 
 int bch2_fs_btree_key_cache_init(struct btree_key_cache *c)
 {
        int ret;
 
-       c->shrink.seeks                 = 1;
-       c->shrink.count_objects         = bch2_btree_key_cache_count;
-       c->shrink.scan_objects          = bch2_btree_key_cache_scan;
-
-       ret = register_shrinker(&c->shrink);
-       if (ret)
-               return ret;
+       c->pcpu_freed = alloc_percpu(struct btree_key_cache_freelist);
+       if (!c->pcpu_freed)
+               return -ENOMEM;
 
        ret = rhashtable_init(&c->table, &bch2_btree_key_cache_params);
        if (ret)
                return ret;
 
        c->table_init_done = true;
-       return 0;
+
+       c->shrink.seeks                 = 1;
+       c->shrink.count_objects         = bch2_btree_key_cache_count;
+       c->shrink.scan_objects          = bch2_btree_key_cache_scan;
+       c->shrink.to_text               = bch2_btree_key_cache_shrinker_to_text;
+       return register_shrinker(&c->shrink);
 }
 
 void bch2_btree_key_cache_to_text(struct printbuf *out, struct btree_key_cache *c)
 {
-       pr_buf(out, "nr_freed:\t%zu\n", c->nr_freed);
-       pr_buf(out, "nr_keys:\t%zu\n",  c->nr_keys);
-       pr_buf(out, "nr_dirty:\t%zu\n", c->nr_dirty);
+       prt_printf(out, "nr_freed:\t%zu\n",     atomic_long_read(&c->nr_freed));
+       prt_printf(out, "nr_keys:\t%lu\n",      atomic_long_read(&c->nr_keys));
+       prt_printf(out, "nr_dirty:\t%lu\n",     atomic_long_read(&c->nr_dirty));
 }
 
 void bch2_btree_key_cache_exit(void)