]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/io.c
Update bcachefs sources to ff83eed5f5 bcachefs: Fix bch2_mark_stripe()
[bcachefs-tools-debian] / libbcachefs / io.c
index 039dd044153ecf36123ad571a0baafb36eb0e6c4..58cc90b198f593e947f02b025f6b7e105f4d27c2 100644 (file)
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * Some low level IO code, and hacks for various block layer limitations
  *
@@ -6,7 +7,9 @@
  */
 
 #include "bcachefs.h"
-#include "alloc.h"
+#include "alloc_background.h"
+#include "alloc_foreground.h"
+#include "bkey_on_stack.h"
 #include "bset.h"
 #include "btree_update.h"
 #include "buckets.h"
 #include "compress.h"
 #include "clock.h"
 #include "debug.h"
+#include "disk_groups.h"
+#include "ec.h"
 #include "error.h"
-#include "extents.h"
+#include "extent_update.h"
+#include "inode.h"
 #include "io.h"
 #include "journal.h"
 #include "keylist.h"
 #include "move.h"
+#include "rebalance.h"
+#include "super.h"
 #include "super-io.h"
 
 #include <linux/blkdev.h>
 #include <linux/random.h>
+#include <linux/sched/mm.h>
 
 #include <trace/events/bcachefs.h>
 
-static inline void __bio_inc_remaining(struct bio *bio)
+const char *bch2_blk_status_to_str(blk_status_t status)
 {
-       bio_set_flag(bio, BIO_CHAIN);
-       smp_mb__before_atomic();
-       atomic_inc(&bio->__bi_remaining);
+       if (status == BLK_STS_REMOVED)
+               return "device removed";
+       return blk_status_to_str(status);
+}
+
+static bool bch2_target_congested(struct bch_fs *c, u16 target)
+{
+       const struct bch_devs_mask *devs;
+       unsigned d, nr = 0, total = 0;
+       u64 now = local_clock(), last;
+       s64 congested;
+       struct bch_dev *ca;
+
+       if (!target)
+               return false;
+
+       rcu_read_lock();
+       devs = bch2_target_to_mask(c, target) ?:
+               &c->rw_devs[BCH_DATA_user];
+
+       for_each_set_bit(d, devs->d, BCH_SB_MEMBERS_MAX) {
+               ca = rcu_dereference(c->devs[d]);
+               if (!ca)
+                       continue;
+
+               congested = atomic_read(&ca->congested);
+               last = READ_ONCE(ca->congested_last);
+               if (time_after64(now, last))
+                       congested -= (now - last) >> 12;
+
+               total += max(congested, 0LL);
+               nr++;
+       }
+       rcu_read_unlock();
+
+       return bch2_rand_range(nr * CONGESTED_MAX) < total;
+}
+
+static inline void bch2_congested_acct(struct bch_dev *ca, u64 io_latency,
+                                      u64 now, int rw)
+{
+       u64 latency_capable =
+               ca->io_latency[rw].quantiles.entries[QUANTILE_IDX(1)].m;
+       /* ideally we'd be taking into account the device's variance here: */
+       u64 latency_threshold = latency_capable << (rw == READ ? 2 : 3);
+       s64 latency_over = io_latency - latency_threshold;
+
+       if (latency_threshold && latency_over > 0) {
+               /*
+                * bump up congested by approximately latency_over * 4 /
+                * latency_threshold - we don't need much accuracy here so don't
+                * bother with the divide:
+                */
+               if (atomic_read(&ca->congested) < CONGESTED_MAX)
+                       atomic_add(latency_over >>
+                                  max_t(int, ilog2(latency_threshold) - 2, 0),
+                                  &ca->congested);
+
+               ca->congested_last = now;
+       } else if (atomic_read(&ca->congested) > 0) {
+               atomic_dec(&ca->congested);
+       }
+}
+
+void bch2_latency_acct(struct bch_dev *ca, u64 submit_time, int rw)
+{
+       atomic64_t *latency = &ca->cur_latency[rw];
+       u64 now = local_clock();
+       u64 io_latency = time_after64(now, submit_time)
+               ? now - submit_time
+               : 0;
+       u64 old, new, v = atomic64_read(latency);
+
+       do {
+               old = v;
+
+               /*
+                * If the io latency was reasonably close to the current
+                * latency, skip doing the update and atomic operation - most of
+                * the time:
+                */
+               if (abs((int) (old - io_latency)) < (old >> 1) &&
+                   now & ~(~0 << 5))
+                       break;
+
+               new = ewma_add(old, io_latency, 5);
+       } while ((v = atomic64_cmpxchg(latency, old, new)) != old);
+
+       bch2_congested_acct(ca, io_latency, now, rw);
+
+       __bch2_time_stats_update(&ca->io_latency[rw], submit_time, now);
 }
 
 /* Allocate, free from mempool: */
 
 void bch2_bio_free_pages_pool(struct bch_fs *c, struct bio *bio)
 {
+       struct bvec_iter_all iter;
        struct bio_vec *bv;
-       unsigned i;
 
-       bio_for_each_segment_all(bv, bio, i)
+       bio_for_each_segment_all(bv, bio, iter)
                if (bv->bv_page != ZERO_PAGE(0))
                        mempool_free(bv->bv_page, &c->bio_bounce_pages);
        bio->bi_vcnt = 0;
 }
 
-static void bch2_bio_alloc_page_pool(struct bch_fs *c, struct bio *bio,
-                                   bool *using_mempool)
+static struct page *__bio_alloc_page_pool(struct bch_fs *c, bool *using_mempool)
 {
-       struct bio_vec *bv = &bio->bi_io_vec[bio->bi_vcnt++];
+       struct page *page;
 
        if (likely(!*using_mempool)) {
-               bv->bv_page = alloc_page(GFP_NOIO);
-               if (unlikely(!bv->bv_page)) {
+               page = alloc_page(GFP_NOIO);
+               if (unlikely(!page)) {
                        mutex_lock(&c->bio_bounce_pages_lock);
                        *using_mempool = true;
                        goto pool_alloc;
@@ -62,153 +158,421 @@ static void bch2_bio_alloc_page_pool(struct bch_fs *c, struct bio *bio,
                }
        } else {
 pool_alloc:
-               bv->bv_page = mempool_alloc(&c->bio_bounce_pages, GFP_NOIO);
+               page = mempool_alloc(&c->bio_bounce_pages, GFP_NOIO);
        }
 
-       bv->bv_len = PAGE_SIZE;
-       bv->bv_offset = 0;
+       return page;
 }
 
 void bch2_bio_alloc_pages_pool(struct bch_fs *c, struct bio *bio,
-                             size_t bytes)
+                              size_t size)
 {
        bool using_mempool = false;
 
-       bio->bi_iter.bi_size = bytes;
+       while (size) {
+               struct page *page = __bio_alloc_page_pool(c, &using_mempool);
+               unsigned len = min(PAGE_SIZE, size);
 
-       while (bio->bi_vcnt < DIV_ROUND_UP(bytes, PAGE_SIZE))
-               bch2_bio_alloc_page_pool(c, bio, &using_mempool);
+               BUG_ON(!bio_add_page(bio, page, len, 0));
+               size -= len;
+       }
 
        if (using_mempool)
                mutex_unlock(&c->bio_bounce_pages_lock);
 }
 
-/* Bios with headers */
+/* Extent update path: */
+
+static int sum_sector_overwrites(struct btree_trans *trans,
+                                struct btree_iter *extent_iter,
+                                struct bkey_i *new,
+                                bool may_allocate,
+                                bool *maybe_extending,
+                                s64 *delta)
+{
+       struct btree_iter *iter;
+       struct bkey_s_c old;
+       int ret = 0;
+
+       *maybe_extending = true;
+       *delta = 0;
+
+       iter = bch2_trans_copy_iter(trans, extent_iter);
+       if (IS_ERR(iter))
+               return PTR_ERR(iter);
+
+       for_each_btree_key_continue(iter, BTREE_ITER_SLOTS, old, ret) {
+               if (!may_allocate &&
+                   bch2_bkey_nr_ptrs_fully_allocated(old) <
+                   bch2_bkey_nr_ptrs_allocated(bkey_i_to_s_c(new))) {
+                       ret = -ENOSPC;
+                       break;
+               }
+
+               *delta += (min(new->k.p.offset,
+                             old.k->p.offset) -
+                         max(bkey_start_offset(&new->k),
+                             bkey_start_offset(old.k))) *
+                       (bkey_extent_is_allocation(&new->k) -
+                        bkey_extent_is_allocation(old.k));
+
+               if (bkey_cmp(old.k->p, new->k.p) >= 0) {
+                       /*
+                        * Check if there's already data above where we're
+                        * going to be writing to - this means we're definitely
+                        * not extending the file:
+                        *
+                        * Note that it's not sufficient to check if there's
+                        * data up to the sector offset we're going to be
+                        * writing to, because i_size could be up to one block
+                        * less:
+                        */
+                       if (!bkey_cmp(old.k->p, new->k.p))
+                               old = bch2_btree_iter_next(iter);
+
+                       if (old.k && !bkey_err(old) &&
+                           old.k->p.inode == extent_iter->pos.inode &&
+                           bkey_extent_is_data(old.k))
+                               *maybe_extending = false;
+
+                       break;
+               }
+       }
+
+       bch2_trans_iter_put(trans, iter);
+       return ret;
+}
+
+int bch2_extent_update(struct btree_trans *trans,
+                      struct btree_iter *iter,
+                      struct bkey_i *k,
+                      struct disk_reservation *disk_res,
+                      u64 *journal_seq,
+                      u64 new_i_size,
+                      s64 *i_sectors_delta)
+{
+       /* this must live until after bch2_trans_commit(): */
+       struct bkey_inode_buf inode_p;
+       bool extending = false;
+       s64 delta = 0;
+       int ret;
+
+       ret = bch2_extent_trim_atomic(k, iter);
+       if (ret)
+               return ret;
+
+       ret = sum_sector_overwrites(trans, iter, k,
+                       disk_res && disk_res->sectors != 0,
+                       &extending, &delta);
+       if (ret)
+               return ret;
+
+       new_i_size = extending
+               ? min(k->k.p.offset << 9, new_i_size)
+               : 0;
+
+       if (delta || new_i_size) {
+               struct btree_iter *inode_iter;
+               struct bch_inode_unpacked inode_u;
+
+               inode_iter = bch2_inode_peek(trans, &inode_u,
+                               k->k.p.inode, BTREE_ITER_INTENT);
+               if (IS_ERR(inode_iter))
+                       return PTR_ERR(inode_iter);
+
+               /*
+                * XXX:
+                * writeback can race a bit with truncate, because truncate
+                * first updates the inode then truncates the pagecache. This is
+                * ugly, but lets us preserve the invariant that the in memory
+                * i_size is always >= the on disk i_size.
+                *
+               BUG_ON(new_i_size > inode_u.bi_size &&
+                      (inode_u.bi_flags & BCH_INODE_I_SIZE_DIRTY));
+                */
+               BUG_ON(new_i_size > inode_u.bi_size && !extending);
+
+               if (!(inode_u.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
+                   new_i_size > inode_u.bi_size)
+                       inode_u.bi_size = new_i_size;
+               else
+                       new_i_size = 0;
+
+               inode_u.bi_sectors += delta;
+
+               if (delta || new_i_size) {
+                       bch2_inode_pack(&inode_p, &inode_u);
+                       bch2_trans_update(trans, inode_iter,
+                                         &inode_p.inode.k_i, 0);
+               }
+
+               bch2_trans_iter_put(trans, inode_iter);
+       }
+
+       bch2_trans_update(trans, iter, k, 0);
+
+       ret = bch2_trans_commit(trans, disk_res, journal_seq,
+                               BTREE_INSERT_NOCHECK_RW|
+                               BTREE_INSERT_NOFAIL|
+                               BTREE_INSERT_USE_RESERVE);
+       if (!ret && i_sectors_delta)
+               *i_sectors_delta += delta;
+
+       return ret;
+}
+
+int bch2_fpunch_at(struct btree_trans *trans, struct btree_iter *iter,
+                  struct bpos end, u64 *journal_seq,
+                  s64 *i_sectors_delta)
+{
+       struct bch_fs *c        = trans->c;
+       unsigned max_sectors    = KEY_SIZE_MAX & (~0 << c->block_bits);
+       struct bkey_s_c k;
+       int ret = 0, ret2 = 0;
+
+       while ((k = bch2_btree_iter_peek(iter)).k &&
+              bkey_cmp(iter->pos, end) < 0) {
+               struct disk_reservation disk_res =
+                       bch2_disk_reservation_init(c, 0);
+               struct bkey_i delete;
+
+               bch2_trans_begin(trans);
+
+               ret = bkey_err(k);
+               if (ret)
+                       goto btree_err;
+
+               bkey_init(&delete.k);
+               delete.k.p = iter->pos;
+
+               /* create the biggest key we can */
+               bch2_key_resize(&delete.k, max_sectors);
+               bch2_cut_back(end, &delete);
+
+               ret = bch2_extent_update(trans, iter, &delete,
+                               &disk_res, journal_seq,
+                               0, i_sectors_delta);
+               bch2_disk_reservation_put(c, &disk_res);
+btree_err:
+               if (ret == -EINTR) {
+                       ret2 = ret;
+                       ret = 0;
+               }
+               if (ret)
+                       break;
+       }
+
+       if (bkey_cmp(iter->pos, end) > 0) {
+               bch2_btree_iter_set_pos(iter, end);
+               ret = bch2_btree_iter_traverse(iter);
+       }
+
+       return ret ?: ret2;
+}
+
+int bch2_fpunch(struct bch_fs *c, u64 inum, u64 start, u64 end,
+               u64 *journal_seq, s64 *i_sectors_delta)
+{
+       struct btree_trans trans;
+       struct btree_iter *iter;
+       int ret = 0;
+
+       bch2_trans_init(&trans, c, BTREE_ITER_MAX, 1024);
+       iter = bch2_trans_get_iter(&trans, BTREE_ID_EXTENTS,
+                                  POS(inum, start),
+                                  BTREE_ITER_INTENT);
+
+       ret = bch2_fpunch_at(&trans, iter, POS(inum, end),
+                            journal_seq, i_sectors_delta);
+       bch2_trans_exit(&trans);
+
+       if (ret == -EINTR)
+               ret = 0;
+
+       return ret;
+}
+
+int bch2_write_index_default(struct bch_write_op *op)
+{
+       struct bch_fs *c = op->c;
+       struct bkey_on_stack sk;
+       struct keylist *keys = &op->insert_keys;
+       struct bkey_i *k = bch2_keylist_front(keys);
+       struct btree_trans trans;
+       struct btree_iter *iter;
+       int ret;
+
+       bkey_on_stack_init(&sk);
+       bch2_trans_init(&trans, c, BTREE_ITER_MAX, 1024);
+
+       iter = bch2_trans_get_iter(&trans, BTREE_ID_EXTENTS,
+                                  bkey_start_pos(&k->k),
+                                  BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
+
+       do {
+               bch2_trans_begin(&trans);
+
+               k = bch2_keylist_front(keys);
+
+               bkey_on_stack_realloc(&sk, c, k->k.u64s);
+               bkey_copy(sk.k, k);
+               bch2_cut_front(iter->pos, sk.k);
+
+               ret = bch2_extent_update(&trans, iter, sk.k,
+                                        &op->res, op_journal_seq(op),
+                                        op->new_i_size, &op->i_sectors_delta);
+               if (ret == -EINTR)
+                       continue;
+               if (ret)
+                       break;
+
+               if (bkey_cmp(iter->pos, k->k.p) >= 0)
+                       bch2_keylist_pop_front(keys);
+       } while (!bch2_keylist_empty(keys));
+
+       bch2_trans_exit(&trans);
+       bkey_on_stack_exit(&sk, c);
+
+       return ret;
+}
+
+/* Writes */
 
 void bch2_submit_wbio_replicas(struct bch_write_bio *wbio, struct bch_fs *c,
+                              enum bch_data_type type,
                               const struct bkey_i *k)
 {
-       struct bkey_s_c_extent e = bkey_i_to_s_c_extent(k);
+       struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(bkey_i_to_s_c(k));
        const struct bch_extent_ptr *ptr;
        struct bch_write_bio *n;
        struct bch_dev *ca;
 
        BUG_ON(c->opts.nochanges);
 
-       wbio->split = false;
-       wbio->c = c;
+       bkey_for_each_ptr(ptrs, ptr) {
+               BUG_ON(ptr->dev >= BCH_SB_MEMBERS_MAX ||
+                      !c->devs[ptr->dev]);
 
-       extent_for_each_ptr(e, ptr) {
-               ca = c->devs[ptr->dev];
+               ca = bch_dev_bkey_exists(c, ptr->dev);
 
-               if (ptr + 1 < &extent_entry_last(e)->ptr) {
+               if (to_entry(ptr + 1) < ptrs.end) {
                        n = to_wbio(bio_clone_fast(&wbio->bio, GFP_NOIO,
                                                   &ca->replica_set));
 
                        n->bio.bi_end_io        = wbio->bio.bi_end_io;
                        n->bio.bi_private       = wbio->bio.bi_private;
-                       n->c                    = c;
-                       n->orig                 = &wbio->bio;
-                       n->bounce               = false;
+                       n->parent               = wbio;
                        n->split                = true;
+                       n->bounce               = false;
                        n->put_bio              = true;
                        n->bio.bi_opf           = wbio->bio.bi_opf;
-                       __bio_inc_remaining(n->orig);
+                       bio_inc_remaining(&wbio->bio);
                } else {
                        n = wbio;
+                       n->split                = false;
                }
 
+               n->c                    = c;
+               n->dev                  = ptr->dev;
+               n->have_ioref           = bch2_dev_get_ioref(ca,
+                                       type == BCH_DATA_btree ? READ : WRITE);
+               n->submit_time          = local_clock();
+               n->bio.bi_iter.bi_sector = ptr->offset;
+
                if (!journal_flushes_device(ca))
                        n->bio.bi_opf |= REQ_FUA;
 
-               n->ca                   = ca;
-               n->submit_time_us       = local_clock_us();
-               n->bio.bi_iter.bi_sector = ptr->offset;
+               if (likely(n->have_ioref)) {
+                       this_cpu_add(ca->io_done->sectors[WRITE][type],
+                                    bio_sectors(&n->bio));
 
-               if (likely(percpu_ref_tryget(&ca->io_ref))) {
-                       n->have_io_ref          = true;
-                       n->bio.bi_bdev          = ca->disk_sb.bdev;
-                       generic_make_request(&n->bio);
+                       bio_set_dev(&n->bio, ca->disk_sb.bdev);
+                       submit_bio(&n->bio);
                } else {
-                       n->have_io_ref          = false;
-                       bcache_io_error(c, &n->bio, "device has been removed");
+                       n->bio.bi_status        = BLK_STS_REMOVED;
                        bio_endio(&n->bio);
                }
        }
 }
 
-/* IO errors */
-
-/* Writes */
-
-static struct workqueue_struct *index_update_wq(struct bch_write_op *op)
-{
-       return op->alloc_reserve == RESERVE_MOVINGGC
-               ? op->c->copygc_wq
-               : op->c->wq;
-}
-
 static void __bch2_write(struct closure *);
 
 static void bch2_write_done(struct closure *cl)
 {
        struct bch_write_op *op = container_of(cl, struct bch_write_op, cl);
-
-       BUG_ON(!(op->flags & BCH_WRITE_DONE));
+       struct bch_fs *c = op->c;
 
        if (!op->error && (op->flags & BCH_WRITE_FLUSH))
-               op->error = bch2_journal_error(&op->c->journal);
+               op->error = bch2_journal_error(&c->journal);
 
-       bch2_disk_reservation_put(op->c, &op->res);
-       percpu_ref_put(&op->c->writes);
+       bch2_disk_reservation_put(c, &op->res);
+       percpu_ref_put(&c->writes);
        bch2_keylist_free(&op->insert_keys, op->inline_keys);
-       closure_return(cl);
-}
 
-static u64 keylist_sectors(struct keylist *keys)
-{
-       struct bkey_i *k;
-       u64 ret = 0;
+       bch2_time_stats_update(&c->times[BCH_TIME_data_write], op->start_time);
 
-       for_each_keylist_key(keys, k)
-               ret += k->k.size;
+       if (!(op->flags & BCH_WRITE_FROM_INTERNAL))
+               up(&c->io_in_flight);
 
-       return ret;
+       if (op->end_io) {
+               EBUG_ON(cl->parent);
+               closure_debug_destroy(cl);
+               op->end_io(op);
+       } else {
+               closure_return(cl);
+       }
 }
 
-static int bch2_write_index_default(struct bch_write_op *op)
+/**
+ * bch_write_index - after a write, update index to point to new data
+ */
+static void __bch2_write_index(struct bch_write_op *op)
 {
+       struct bch_fs *c = op->c;
        struct keylist *keys = &op->insert_keys;
-       struct btree_iter iter;
+       struct bch_extent_ptr *ptr;
+       struct bkey_i *src, *dst = keys->keys, *n, *k;
+       unsigned dev;
        int ret;
 
-       bch2_btree_iter_init_intent(&iter, op->c, BTREE_ID_EXTENTS,
-               bkey_start_pos(&bch2_keylist_front(keys)->k));
+       for (src = keys->keys; src != keys->top; src = n) {
+               n = bkey_next(src);
 
-       ret = bch2_btree_insert_list_at(&iter, keys, &op->res,
-                                      NULL, op_journal_seq(op),
-                                      BTREE_INSERT_NOFAIL);
-       bch2_btree_iter_unlock(&iter);
+               if (bkey_extent_is_direct_data(&src->k)) {
+                       bch2_bkey_drop_ptrs(bkey_i_to_s(src), ptr,
+                                           test_bit(ptr->dev, op->failed.d));
 
-       return ret;
-}
+                       if (!bch2_bkey_nr_ptrs(bkey_i_to_s_c(src))) {
+                               ret = -EIO;
+                               goto err;
+                       }
+               }
 
-/**
- * bch_write_index - after a write, update index to point to new data
- */
-static void bch2_write_index(struct closure *cl)
-{
-       struct bch_write_op *op = container_of(cl, struct bch_write_op, cl);
-       struct bch_fs *c = op->c;
-       struct keylist *keys = &op->insert_keys;
-       unsigned i;
+               if (dst != src)
+                       memmove_u64s_down(dst, src, src->u64s);
+               dst = bkey_next(dst);
+       }
+
+       keys->top = dst;
+
+       /*
+        * probably not the ideal place to hook this in, but I don't
+        * particularly want to plumb io_opts all the way through the btree
+        * update stack right now
+        */
+       for_each_keylist_key(keys, k) {
+               bch2_rebalance_add_key(c, bkey_i_to_s_c(k), &op->opts);
+
+               if (bch2_bkey_is_incompressible(bkey_i_to_s_c(k)))
+                       bch2_check_set_feature(op->c, BCH_FEATURE_incompressible);
 
-       op->flags |= BCH_WRITE_LOOPED;
+       }
 
        if (!bch2_keylist_empty(keys)) {
                u64 sectors_start = keylist_sectors(keys);
                int ret = op->index_update_fn(op);
 
+               BUG_ON(ret == -EINTR);
                BUG_ON(keylist_sectors(keys) && !ret);
 
                op->written += sectors_start - keylist_sectors(keys);
@@ -218,19 +582,29 @@ static void bch2_write_index(struct closure *cl)
                        op->error = ret;
                }
        }
+out:
+       /* If some a bucket wasn't written, we can't erasure code it: */
+       for_each_set_bit(dev, op->failed.d, BCH_SB_MEMBERS_MAX)
+               bch2_open_bucket_write_error(c, &op->open_buckets, dev);
 
-       for (i = 0; i < ARRAY_SIZE(op->open_buckets); i++)
-               if (op->open_buckets[i]) {
-                       bch2_open_bucket_put(c,
-                                            c->open_buckets +
-                                            op->open_buckets[i]);
-                       op->open_buckets[i] = 0;
-               }
+       bch2_open_buckets_put(c, &op->open_buckets);
+       return;
+err:
+       keys->top = keys->keys;
+       op->error = ret;
+       goto out;
+}
+
+static void bch2_write_index(struct closure *cl)
+{
+       struct bch_write_op *op = container_of(cl, struct bch_write_op, cl);
+       struct bch_fs *c = op->c;
 
-       if (!(op->flags & BCH_WRITE_DONE))
-               continue_at(cl, __bch2_write, op->io_wq);
+       __bch2_write_index(op);
 
-       if (!op->error && (op->flags & BCH_WRITE_FLUSH)) {
+       if (!(op->flags & BCH_WRITE_DONE)) {
+               continue_at(cl, __bch2_write, index_update_wq(op));
+       } else if (!op->error && (op->flags & BCH_WRITE_FLUSH)) {
                bch2_journal_flush_seq_async(&c->journal,
                                             *op_journal_seq(op),
                                             cl);
@@ -240,299 +614,448 @@ static void bch2_write_index(struct closure *cl)
        }
 }
 
-/**
- * bch_write_discard - discard range of keys
- *
- * Used to implement discard, and to handle when writethrough write hits
- * a write error on the cache device.
- */
-static void bch2_write_discard(struct closure *cl)
+static void bch2_write_endio(struct bio *bio)
 {
-       struct bch_write_op *op = container_of(cl, struct bch_write_op, cl);
-       struct bio *bio = &op->bio->bio;
-       struct bpos end = op->pos;
+       struct closure *cl              = bio->bi_private;
+       struct bch_write_op *op         = container_of(cl, struct bch_write_op, cl);
+       struct bch_write_bio *wbio      = to_wbio(bio);
+       struct bch_write_bio *parent    = wbio->split ? wbio->parent : NULL;
+       struct bch_fs *c                = wbio->c;
+       struct bch_dev *ca              = bch_dev_bkey_exists(c, wbio->dev);
+
+       if (bch2_dev_io_err_on(bio->bi_status, ca, "data write: %s",
+                              bch2_blk_status_to_str(bio->bi_status)))
+               set_bit(wbio->dev, op->failed.d);
+
+       if (wbio->have_ioref) {
+               bch2_latency_acct(ca, wbio->submit_time, WRITE);
+               percpu_ref_put(&ca->io_ref);
+       }
+
+       if (wbio->bounce)
+               bch2_bio_free_pages_pool(c, bio);
 
-       end.offset += bio_sectors(bio);
+       if (wbio->put_bio)
+               bio_put(bio);
 
-       op->error = bch2_discard(op->c, op->pos, end, op->version,
-                               &op->res, NULL, NULL);
+       if (parent)
+               bio_endio(&parent->bio);
+       else if (!(op->flags & BCH_WRITE_SKIP_CLOSURE_PUT))
+               closure_put(cl);
+       else
+               continue_at_nobarrier(cl, bch2_write_index, index_update_wq(op));
 }
 
-/*
- * Convert extents to be inserted to discards after an error:
- */
-static void bch2_write_io_error(struct closure *cl)
+static void init_append_extent(struct bch_write_op *op,
+                              struct write_point *wp,
+                              struct bversion version,
+                              struct bch_extent_crc_unpacked crc)
 {
-       struct bch_write_op *op = container_of(cl, struct bch_write_op, cl);
+       struct bch_fs *c = op->c;
+       struct bkey_i_extent *e;
+       struct open_bucket *ob;
+       unsigned i;
 
-       if (op->flags & BCH_WRITE_DISCARD_ON_ERROR) {
-               struct bkey_i *src = bch2_keylist_front(&op->insert_keys);
-               struct bkey_i *dst = bch2_keylist_front(&op->insert_keys);
+       BUG_ON(crc.compressed_size > wp->sectors_free);
+       wp->sectors_free -= crc.compressed_size;
+       op->pos.offset += crc.uncompressed_size;
 
-               /*
-                * Our data write just errored, which means we've got a bunch
-                * of keys to insert that point to data that wasn't
-                * successfully written.
-                *
-                * We don't have to insert those keys but we still have to
-                * invalidate that region of the cache - so, if we just strip
-                * off all the pointers from the keys we'll accomplish just
-                * that.
-                */
+       e = bkey_extent_init(op->insert_keys.top);
+       e->k.p          = op->pos;
+       e->k.size       = crc.uncompressed_size;
+       e->k.version    = version;
 
-               while (src != op->insert_keys.top) {
-                       struct bkey_i *n = bkey_next(src);
+       if (crc.csum_type ||
+           crc.compression_type ||
+           crc.nonce)
+               bch2_extent_crc_append(&e->k_i, crc);
 
-                       set_bkey_val_u64s(&src->k, 0);
-                       src->k.type = KEY_TYPE_DISCARD;
-                       bkey_copy(dst, src);
+       open_bucket_for_each(c, &wp->ptrs, ob, i) {
+               struct bch_dev *ca = bch_dev_bkey_exists(c, ob->ptr.dev);
+               union bch_extent_entry *end =
+                       bkey_val_end(bkey_i_to_s(&e->k_i));
 
-                       dst = bkey_next(dst);
-                       src = n;
-               }
+               end->ptr = ob->ptr;
+               end->ptr.type = 1 << BCH_EXTENT_ENTRY_ptr;
+               end->ptr.cached = !ca->mi.durability ||
+                       (op->flags & BCH_WRITE_CACHED) != 0;
+               end->ptr.offset += ca->mi.bucket_size - ob->sectors_free;
 
-               op->insert_keys.top = dst;
-               op->flags |= BCH_WRITE_DISCARD;
-       } else {
-               /* TODO: We could try to recover from this. */
-               while (!bch2_keylist_empty(&op->insert_keys))
-                       bch2_keylist_pop_front(&op->insert_keys);
+               e->k.u64s++;
 
-               op->error = -EIO;
-               op->flags |= BCH_WRITE_DONE;
+               BUG_ON(crc.compressed_size > ob->sectors_free);
+               ob->sectors_free -= crc.compressed_size;
        }
 
-       bch2_write_index(cl);
+       bch2_keylist_push(&op->insert_keys);
 }
 
-static void bch2_write_endio(struct bio *bio)
+static struct bio *bch2_write_bio_alloc(struct bch_fs *c,
+                                       struct write_point *wp,
+                                       struct bio *src,
+                                       bool *page_alloc_failed,
+                                       void *buf)
 {
-       struct closure *cl = bio->bi_private;
-       struct bch_write_op *op = container_of(cl, struct bch_write_op, cl);
-       struct bch_write_bio *wbio = to_wbio(bio);
-       struct bch_fs *c = wbio->c;
-       struct bio *orig = wbio->orig;
-       struct bch_dev *ca = wbio->ca;
+       struct bch_write_bio *wbio;
+       struct bio *bio;
+       unsigned output_available =
+               min(wp->sectors_free << 9, src->bi_iter.bi_size);
+       unsigned pages = DIV_ROUND_UP(output_available +
+                                     (buf
+                                      ? ((unsigned long) buf & (PAGE_SIZE - 1))
+                                      : 0), PAGE_SIZE);
+
+       bio = bio_alloc_bioset(GFP_NOIO, pages, &c->bio_write);
+       wbio                    = wbio_init(bio);
+       wbio->put_bio           = true;
+       /* copy WRITE_SYNC flag */
+       wbio->bio.bi_opf        = src->bi_opf;
+
+       if (buf) {
+               bch2_bio_map(bio, buf, output_available);
+               return bio;
+       }
 
-       if (bch2_dev_nonfatal_io_err_on(bio->bi_error, ca,
-                                      "data write"))
-               set_closure_fn(cl, bch2_write_io_error, index_update_wq(op));
+       wbio->bounce            = true;
 
-       if (wbio->have_io_ref)
-               percpu_ref_put(&ca->io_ref);
+       /*
+        * We can't use mempool for more than c->sb.encoded_extent_max
+        * worth of pages, but we'd like to allocate more if we can:
+        */
+       bch2_bio_alloc_pages_pool(c, bio,
+                                 min_t(unsigned, output_available,
+                                       c->sb.encoded_extent_max << 9));
+
+       if (bio->bi_iter.bi_size < output_available)
+               *page_alloc_failed =
+                       bch2_bio_alloc_pages(bio,
+                                            output_available -
+                                            bio->bi_iter.bi_size,
+                                            GFP_NOFS) != 0;
+
+       return bio;
+}
 
-       if (bio->bi_error && orig)
-               orig->bi_error = bio->bi_error;
+static int bch2_write_rechecksum(struct bch_fs *c,
+                                struct bch_write_op *op,
+                                unsigned new_csum_type)
+{
+       struct bio *bio = &op->wbio.bio;
+       struct bch_extent_crc_unpacked new_crc;
+       int ret;
 
-       if (wbio->bounce)
-               bch2_bio_free_pages_pool(c, bio);
+       /* bch2_rechecksum_bio() can't encrypt or decrypt data: */
 
-       if (wbio->put_bio)
-               bio_put(bio);
+       if (bch2_csum_type_is_encryption(op->crc.csum_type) !=
+           bch2_csum_type_is_encryption(new_csum_type))
+               new_csum_type = op->crc.csum_type;
 
-       if (orig)
-               bio_endio(orig);
-       else
-               closure_put(cl);
-}
+       ret = bch2_rechecksum_bio(c, bio, op->version, op->crc,
+                                 NULL, &new_crc,
+                                 op->crc.offset, op->crc.live_size,
+                                 new_csum_type);
+       if (ret)
+               return ret;
 
-static struct nonce extent_nonce(struct bversion version,
-                                unsigned nonce,
-                                unsigned uncompressed_size,
-                                unsigned compression_type)
-{
-       return (struct nonce) {{
-               [0] = cpu_to_le32((nonce                << 12) |
-                                 (uncompressed_size    << 22)),
-               [1] = cpu_to_le32(version.lo),
-               [2] = cpu_to_le32(version.lo >> 32),
-               [3] = cpu_to_le32(version.hi|
-                                 (compression_type << 24))^BCH_NONCE_EXTENT,
-       }};
+       bio_advance(bio, op->crc.offset << 9);
+       bio->bi_iter.bi_size = op->crc.live_size << 9;
+       op->crc = new_crc;
+       return 0;
 }
 
-static void init_append_extent(struct bch_write_op *op,
-                              unsigned compressed_size,
-                              unsigned uncompressed_size,
-                              unsigned compression_type,
-                              unsigned nonce,
-                              struct bch_csum csum, unsigned csum_type,
-                              struct open_bucket *ob)
+static int bch2_write_decrypt(struct bch_write_op *op)
 {
-       struct bkey_i_extent *e = bkey_extent_init(op->insert_keys.top);
-
-       op->pos.offset += uncompressed_size;
-       e->k.p = op->pos;
-       e->k.size = uncompressed_size;
-       e->k.version = op->version;
-       bkey_extent_set_cached(&e->k, op->flags & BCH_WRITE_CACHED);
-
-       bch2_extent_crc_append(e, compressed_size,
-                             uncompressed_size,
-                             compression_type,
-                             nonce, csum, csum_type);
+       struct bch_fs *c = op->c;
+       struct nonce nonce = extent_nonce(op->version, op->crc);
+       struct bch_csum csum;
 
-       bch2_alloc_sectors_append_ptrs(op->c, e, op->nr_replicas,
-                                     ob, compressed_size);
+       if (!bch2_csum_type_is_encryption(op->crc.csum_type))
+               return 0;
 
-       bkey_extent_set_cached(&e->k, (op->flags & BCH_WRITE_CACHED));
-       bch2_keylist_push(&op->insert_keys);
+       /*
+        * If we need to decrypt data in the write path, we'll no longer be able
+        * to verify the existing checksum (poly1305 mac, in this case) after
+        * it's decrypted - this is the last point we'll be able to reverify the
+        * checksum:
+        */
+       csum = bch2_checksum_bio(c, op->crc.csum_type, nonce, &op->wbio.bio);
+       if (bch2_crc_cmp(op->crc.csum, csum))
+               return -EIO;
+
+       bch2_encrypt_bio(c, op->crc.csum_type, nonce, &op->wbio.bio);
+       op->crc.csum_type = 0;
+       op->crc.csum = (struct bch_csum) { 0, 0 };
+       return 0;
 }
 
-static int bch2_write_extent(struct bch_write_op *op,
-                           struct open_bucket *ob,
-                           struct bio *orig)
+static enum prep_encoded_ret {
+       PREP_ENCODED_OK,
+       PREP_ENCODED_ERR,
+       PREP_ENCODED_CHECKSUM_ERR,
+       PREP_ENCODED_DO_WRITE,
+} bch2_write_prep_encoded_data(struct bch_write_op *op, struct write_point *wp)
 {
        struct bch_fs *c = op->c;
-       struct bio *bio;
-       struct bch_write_bio *wbio;
-       unsigned key_to_write_offset = op->insert_keys.top_p -
-               op->insert_keys.keys_p;
-       struct bkey_i *key_to_write;
-       unsigned csum_type = op->csum_type;
-       unsigned compression_type = op->compression_type;
-       int ret;
+       struct bio *bio = &op->wbio.bio;
 
-       /* don't refetch csum type/compression type */
-       barrier();
+       if (!(op->flags & BCH_WRITE_DATA_ENCODED))
+               return PREP_ENCODED_OK;
 
-       /* Need to decompress data? */
-       if ((op->flags & BCH_WRITE_DATA_COMPRESSED) &&
-           (crc_uncompressed_size(NULL, &op->crc) != op->size ||
-            crc_compressed_size(NULL, &op->crc) > ob->sectors_free)) {
-               int ret;
+       BUG_ON(bio_sectors(bio) != op->crc.compressed_size);
 
-               ret = bch2_bio_uncompress_inplace(c, orig, op->size, op->crc);
-               if (ret)
-                       return ret;
+       /* Can we just write the entire extent as is? */
+       if (op->crc.uncompressed_size == op->crc.live_size &&
+           op->crc.compressed_size <= wp->sectors_free &&
+           (op->crc.compression_type == op->compression_type ||
+            op->incompressible)) {
+               if (!crc_is_compressed(op->crc) &&
+                   op->csum_type != op->crc.csum_type &&
+                   bch2_write_rechecksum(c, op, op->csum_type))
+                       return PREP_ENCODED_CHECKSUM_ERR;
 
-               op->flags &= ~BCH_WRITE_DATA_COMPRESSED;
+               return PREP_ENCODED_DO_WRITE;
        }
 
-       if (op->flags & BCH_WRITE_DATA_COMPRESSED) {
-               init_append_extent(op,
-                                  crc_compressed_size(NULL, &op->crc),
-                                  crc_uncompressed_size(NULL, &op->crc),
-                                  op->crc.compression_type,
-                                  op->crc.nonce,
-                                  op->crc.csum,
-                                  op->crc.csum_type,
-                                  ob);
-
-               bio                     = orig;
-               wbio                    = to_wbio(bio);
-               wbio->orig              = NULL;
-               wbio->bounce            = false;
-               wbio->put_bio           = false;
-               ret                     = 0;
-       } else if (csum_type != BCH_CSUM_NONE ||
-                  compression_type != BCH_COMPRESSION_NONE) {
-               /* all units here in bytes */
-               unsigned total_output = 0, output_available =
-                       min(ob->sectors_free << 9, orig->bi_iter.bi_size);
-               unsigned crc_nonce = bch2_csum_type_is_encryption(csum_type)
-                       ? op->nonce : 0;
+       /*
+        * If the data is compressed and we couldn't write the entire extent as
+        * is, we have to decompress it:
+        */
+       if (crc_is_compressed(op->crc)) {
                struct bch_csum csum;
-               struct nonce nonce;
 
-               bio = bio_alloc_bioset(GFP_NOIO,
-                                      DIV_ROUND_UP(output_available, PAGE_SIZE),
-                                      &c->bio_write);
-               /*
-                * XXX: can't use mempool for more than
-                * BCH_COMPRESSED_EXTENT_MAX worth of pages
-                */
-               bch2_bio_alloc_pages_pool(c, bio, output_available);
+               if (bch2_write_decrypt(op))
+                       return PREP_ENCODED_CHECKSUM_ERR;
 
-               /* copy WRITE_SYNC flag */
-               bio->bi_opf             = orig->bi_opf;
-               wbio                    = to_wbio(bio);
-               wbio->orig              = NULL;
-               wbio->bounce            = true;
-               wbio->put_bio           = true;
-
-               do {
-                       unsigned fragment_compression_type = compression_type;
-                       size_t dst_len, src_len;
-
-                       bch2_bio_compress(c, bio, &dst_len,
-                                        orig, &src_len,
-                                        &fragment_compression_type);
-
-                       BUG_ON(!dst_len || dst_len > bio->bi_iter.bi_size);
-                       BUG_ON(!src_len || src_len > orig->bi_iter.bi_size);
-                       BUG_ON(dst_len & (block_bytes(c) - 1));
-                       BUG_ON(src_len & (block_bytes(c) - 1));
-
-                       swap(bio->bi_iter.bi_size, dst_len);
-                       nonce = extent_nonce(op->version,
-                                            crc_nonce,
-                                            src_len >> 9,
-                                            fragment_compression_type),
-
-                       bch2_encrypt_bio(c, csum_type, nonce, bio);
-
-                       csum = bch2_checksum_bio(c, csum_type, nonce, bio);
-                       swap(bio->bi_iter.bi_size, dst_len);
-
-                       init_append_extent(op,
-                                          dst_len >> 9, src_len >> 9,
-                                          fragment_compression_type,
-                                          crc_nonce, csum, csum_type, ob);
-
-                       total_output += dst_len;
-                       bio_advance(bio, dst_len);
-                       bio_advance(orig, src_len);
-               } while (bio->bi_iter.bi_size &&
-                        orig->bi_iter.bi_size &&
-                        !bch2_keylist_realloc(&op->insert_keys,
-                                             op->inline_keys,
-                                             ARRAY_SIZE(op->inline_keys),
-                                             BKEY_EXTENT_U64s_MAX));
-
-               BUG_ON(total_output > output_available);
-
-               memset(&bio->bi_iter, 0, sizeof(bio->bi_iter));
-               bio->bi_iter.bi_size = total_output;
+               /* Last point we can still verify checksum: */
+               csum = bch2_checksum_bio(c, op->crc.csum_type,
+                                        extent_nonce(op->version, op->crc),
+                                        bio);
+               if (bch2_crc_cmp(op->crc.csum, csum))
+                       return PREP_ENCODED_CHECKSUM_ERR;
 
-               /*
-                * Free unneeded pages after compressing:
-                */
-               while (bio->bi_vcnt * PAGE_SIZE >
-                      round_up(bio->bi_iter.bi_size, PAGE_SIZE))
-                       mempool_free(bio->bi_io_vec[--bio->bi_vcnt].bv_page,
-                                    &c->bio_bounce_pages);
+               if (bch2_bio_uncompress_inplace(c, bio, &op->crc))
+                       return PREP_ENCODED_ERR;
+       }
 
-               ret = orig->bi_iter.bi_size != 0;
-       } else {
-               bio = bio_next_split(orig, ob->sectors_free, GFP_NOIO,
-                                    &c->bio_write);
+       /*
+        * No longer have compressed data after this point - data might be
+        * encrypted:
+        */
 
-               wbio                    = to_wbio(bio);
-               wbio->orig              = NULL;
-               wbio->bounce            = false;
-               wbio->put_bio           = bio != orig;
+       /*
+        * If the data is checksummed and we're only writing a subset,
+        * rechecksum and adjust bio to point to currently live data:
+        */
+       if ((op->crc.live_size != op->crc.uncompressed_size ||
+            op->crc.csum_type != op->csum_type) &&
+           bch2_write_rechecksum(c, op, op->csum_type))
+               return PREP_ENCODED_CHECKSUM_ERR;
 
-               init_append_extent(op, bio_sectors(bio), bio_sectors(bio),
-                                  compression_type, 0,
-                                  (struct bch_csum) { 0 }, csum_type, ob);
+       /*
+        * If we want to compress the data, it has to be decrypted:
+        */
+       if ((op->compression_type ||
+            bch2_csum_type_is_encryption(op->crc.csum_type) !=
+            bch2_csum_type_is_encryption(op->csum_type)) &&
+           bch2_write_decrypt(op))
+               return PREP_ENCODED_CHECKSUM_ERR;
+
+       return PREP_ENCODED_OK;
+}
 
-               ret = bio != orig;
+static int bch2_write_extent(struct bch_write_op *op, struct write_point *wp,
+                            struct bio **_dst)
+{
+       struct bch_fs *c = op->c;
+       struct bio *src = &op->wbio.bio, *dst = src;
+       struct bvec_iter saved_iter;
+       void *ec_buf;
+       struct bpos ec_pos = op->pos;
+       unsigned total_output = 0, total_input = 0;
+       bool bounce = false;
+       bool page_alloc_failed = false;
+       int ret, more = 0;
+
+       BUG_ON(!bio_sectors(src));
+
+       ec_buf = bch2_writepoint_ec_buf(c, wp);
+
+       switch (bch2_write_prep_encoded_data(op, wp)) {
+       case PREP_ENCODED_OK:
+               break;
+       case PREP_ENCODED_ERR:
+               ret = -EIO;
+               goto err;
+       case PREP_ENCODED_CHECKSUM_ERR:
+               BUG();
+               goto csum_err;
+       case PREP_ENCODED_DO_WRITE:
+               /* XXX look for bug here */
+               if (ec_buf) {
+                       dst = bch2_write_bio_alloc(c, wp, src,
+                                                  &page_alloc_failed,
+                                                  ec_buf);
+                       bio_copy_data(dst, src);
+                       bounce = true;
+               }
+               init_append_extent(op, wp, op->version, op->crc);
+               goto do_write;
        }
 
-       bio->bi_end_io  = bch2_write_endio;
-       bio->bi_private = &op->cl;
-       bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
+       if (ec_buf ||
+           op->compression_type ||
+           (op->csum_type &&
+            !(op->flags & BCH_WRITE_PAGES_STABLE)) ||
+           (bch2_csum_type_is_encryption(op->csum_type) &&
+            !(op->flags & BCH_WRITE_PAGES_OWNED))) {
+               dst = bch2_write_bio_alloc(c, wp, src,
+                                          &page_alloc_failed,
+                                          ec_buf);
+               bounce = true;
+       }
 
-       closure_get(bio->bi_private);
+       saved_iter = dst->bi_iter;
 
-       /* might have done a realloc... */
+       do {
+               struct bch_extent_crc_unpacked crc =
+                       (struct bch_extent_crc_unpacked) { 0 };
+               struct bversion version = op->version;
+               size_t dst_len, src_len;
+
+               if (page_alloc_failed &&
+                   bio_sectors(dst) < wp->sectors_free &&
+                   bio_sectors(dst) < c->sb.encoded_extent_max)
+                       break;
+
+               BUG_ON(op->compression_type &&
+                      (op->flags & BCH_WRITE_DATA_ENCODED) &&
+                      bch2_csum_type_is_encryption(op->crc.csum_type));
+               BUG_ON(op->compression_type && !bounce);
+
+               crc.compression_type = op->incompressible
+                       ? BCH_COMPRESSION_TYPE_incompressible
+                       : op->compression_type
+                       ? bch2_bio_compress(c, dst, &dst_len, src, &src_len,
+                                           op->compression_type)
+                       : 0;
+               if (!crc_is_compressed(crc)) {
+                       dst_len = min(dst->bi_iter.bi_size, src->bi_iter.bi_size);
+                       dst_len = min_t(unsigned, dst_len, wp->sectors_free << 9);
+
+                       if (op->csum_type)
+                               dst_len = min_t(unsigned, dst_len,
+                                               c->sb.encoded_extent_max << 9);
+
+                       if (bounce) {
+                               swap(dst->bi_iter.bi_size, dst_len);
+                               bio_copy_data(dst, src);
+                               swap(dst->bi_iter.bi_size, dst_len);
+                       }
+
+                       src_len = dst_len;
+               }
+
+               BUG_ON(!src_len || !dst_len);
+
+               if (bch2_csum_type_is_encryption(op->csum_type)) {
+                       if (bversion_zero(version)) {
+                               version.lo = atomic64_inc_return(&c->key_version);
+                       } else {
+                               crc.nonce = op->nonce;
+                               op->nonce += src_len >> 9;
+                       }
+               }
+
+               if ((op->flags & BCH_WRITE_DATA_ENCODED) &&
+                   !crc_is_compressed(crc) &&
+                   bch2_csum_type_is_encryption(op->crc.csum_type) ==
+                   bch2_csum_type_is_encryption(op->csum_type)) {
+                       /*
+                        * Note: when we're using rechecksum(), we need to be
+                        * checksumming @src because it has all the data our
+                        * existing checksum covers - if we bounced (because we
+                        * were trying to compress), @dst will only have the
+                        * part of the data the new checksum will cover.
+                        *
+                        * But normally we want to be checksumming post bounce,
+                        * because part of the reason for bouncing is so the
+                        * data can't be modified (by userspace) while it's in
+                        * flight.
+                        */
+                       if (bch2_rechecksum_bio(c, src, version, op->crc,
+                                       &crc, &op->crc,
+                                       src_len >> 9,
+                                       bio_sectors(src) - (src_len >> 9),
+                                       op->csum_type))
+                               goto csum_err;
+               } else {
+                       if ((op->flags & BCH_WRITE_DATA_ENCODED) &&
+                           bch2_rechecksum_bio(c, src, version, op->crc,
+                                       NULL, &op->crc,
+                                       src_len >> 9,
+                                       bio_sectors(src) - (src_len >> 9),
+                                       op->crc.csum_type))
+                               goto csum_err;
+
+                       crc.compressed_size     = dst_len >> 9;
+                       crc.uncompressed_size   = src_len >> 9;
+                       crc.live_size           = src_len >> 9;
+
+                       swap(dst->bi_iter.bi_size, dst_len);
+                       bch2_encrypt_bio(c, op->csum_type,
+                                        extent_nonce(version, crc), dst);
+                       crc.csum = bch2_checksum_bio(c, op->csum_type,
+                                        extent_nonce(version, crc), dst);
+                       crc.csum_type = op->csum_type;
+                       swap(dst->bi_iter.bi_size, dst_len);
+               }
 
-       key_to_write = (void *) (op->insert_keys.keys_p + key_to_write_offset);
+               init_append_extent(op, wp, version, crc);
 
-       bch2_check_mark_super(c, key_to_write, false);
+               if (dst != src)
+                       bio_advance(dst, dst_len);
+               bio_advance(src, src_len);
+               total_output    += dst_len;
+               total_input     += src_len;
+       } while (dst->bi_iter.bi_size &&
+                src->bi_iter.bi_size &&
+                wp->sectors_free &&
+                !bch2_keylist_realloc(&op->insert_keys,
+                                     op->inline_keys,
+                                     ARRAY_SIZE(op->inline_keys),
+                                     BKEY_EXTENT_U64s_MAX));
+
+       more = src->bi_iter.bi_size != 0;
+
+       dst->bi_iter = saved_iter;
+
+       if (dst == src && more) {
+               BUG_ON(total_output != total_input);
+
+               dst = bio_split(src, total_input >> 9,
+                               GFP_NOIO, &c->bio_write);
+               wbio_init(dst)->put_bio = true;
+               /* copy WRITE_SYNC flag */
+               dst->bi_opf             = src->bi_opf;
+       }
+
+       dst->bi_iter.bi_size = total_output;
+do_write:
+       /* might have done a realloc... */
+       bch2_ec_add_backpointer(c, wp, ec_pos, total_input >> 9);
+
+       *_dst = dst;
+       return more;
+csum_err:
+       bch_err(c, "error verifying existing checksum while "
+               "rewriting existing data (memory corruption?)");
+       ret = -EIO;
+err:
+       if (to_wbio(dst)->bounce)
+               bch2_bio_free_pages_pool(c, dst);
+       if (to_wbio(dst)->put_bio)
+               bio_put(dst);
 
-       bch2_submit_wbio_replicas(to_wbio(bio), c, key_to_write);
        return ret;
 }
 
@@ -540,152 +1063,190 @@ static void __bch2_write(struct closure *cl)
 {
        struct bch_write_op *op = container_of(cl, struct bch_write_op, cl);
        struct bch_fs *c = op->c;
-       struct bio *bio = &op->bio->bio;
-       unsigned open_bucket_nr = 0;
-       struct open_bucket *b;
+       struct write_point *wp;
+       struct bio *bio;
+       bool skip_put = true;
+       unsigned nofs_flags;
        int ret;
 
-       memset(op->open_buckets, 0, sizeof(op->open_buckets));
-
-       if (op->flags & BCH_WRITE_DISCARD) {
-               op->flags |= BCH_WRITE_DONE;
-               bch2_write_discard(cl);
-               bio_put(bio);
-               continue_at(cl, bch2_write_done, index_update_wq(op));
-       }
-
-       /*
-        * Journal writes are marked REQ_PREFLUSH; if the original write was a
-        * flush, it'll wait on the journal write.
-        */
-       bio->bi_opf &= ~(REQ_PREFLUSH|REQ_FUA);
+       nofs_flags = memalloc_nofs_save();
+again:
+       memset(&op->failed, 0, sizeof(op->failed));
 
        do {
-               EBUG_ON(bio->bi_iter.bi_sector != op->pos.offset);
-               EBUG_ON(!bio_sectors(bio));
+               struct bkey_i *key_to_write;
+               unsigned key_to_write_offset = op->insert_keys.top_p -
+                       op->insert_keys.keys_p;
 
-               if (open_bucket_nr == ARRAY_SIZE(op->open_buckets))
-                       continue_at(cl, bch2_write_index, index_update_wq(op));
+               /* +1 for possible cache device: */
+               if (op->open_buckets.nr + op->nr_replicas + 1 >
+                   ARRAY_SIZE(op->open_buckets.v))
+                       goto flush_io;
 
-               /* for the device pointers and 1 for the chksum */
                if (bch2_keylist_realloc(&op->insert_keys,
                                        op->inline_keys,
                                        ARRAY_SIZE(op->inline_keys),
                                        BKEY_EXTENT_U64s_MAX))
-                       continue_at(cl, bch2_write_index, index_update_wq(op));
+                       goto flush_io;
 
-               b = bch2_alloc_sectors_start(c, op->wp,
+               if ((op->flags & BCH_WRITE_FROM_INTERNAL) &&
+                   percpu_ref_is_dying(&c->writes)) {
+                       ret = -EROFS;
+                       goto err;
+               }
+
+               /*
+                * The copygc thread is now global, which means it's no longer
+                * freeing up space on specific disks, which means that
+                * allocations for specific disks may hang arbitrarily long:
+                */
+               wp = bch2_alloc_sectors_start(c,
+                       op->target,
+                       op->opts.erasure_code,
+                       op->write_point,
+                       &op->devs_have,
                        op->nr_replicas,
-                       c->opts.data_replicas_required,
+                       op->nr_replicas_required,
                        op->alloc_reserve,
-                       (op->flags & BCH_WRITE_ALLOC_NOWAIT) ? NULL : cl);
-               EBUG_ON(!b);
-
-               if (unlikely(IS_ERR(b))) {
-                       if (unlikely(PTR_ERR(b) != -EAGAIN)) {
-                               ret = PTR_ERR(b);
+                       op->flags,
+                       (op->flags & (BCH_WRITE_ALLOC_NOWAIT|
+                                     BCH_WRITE_ONLY_SPECIFIED_DEVS)) ? NULL : cl);
+               EBUG_ON(!wp);
+
+               if (unlikely(IS_ERR(wp))) {
+                       if (unlikely(PTR_ERR(wp) != -EAGAIN)) {
+                               ret = PTR_ERR(wp);
                                goto err;
                        }
 
-                       /*
-                        * If we already have some keys, must insert them first
-                        * before allocating another open bucket. We only hit
-                        * this case if open_bucket_nr > 1.
-                        */
-                       if (!bch2_keylist_empty(&op->insert_keys))
-                               continue_at(cl, bch2_write_index,
-                                           index_update_wq(op));
+                       goto flush_io;
+               }
 
-                       /*
-                        * If we've looped, we're running out of a workqueue -
-                        * not the bch2_write() caller's context - and we don't
-                        * want to block the workqueue:
-                        */
-                       if (op->flags & BCH_WRITE_LOOPED)
-                               continue_at(cl, __bch2_write, op->io_wq);
+               /*
+                * It's possible for the allocator to fail, put us on the
+                * freelist waitlist, and then succeed in one of various retry
+                * paths: if that happens, we need to disable the skip_put
+                * optimization because otherwise there won't necessarily be a
+                * barrier before we free the bch_write_op:
+                */
+               if (atomic_read(&cl->remaining) & CLOSURE_WAITING)
+                       skip_put = false;
 
+               bch2_open_bucket_get(c, wp, &op->open_buckets);
+               ret = bch2_write_extent(op, wp, &bio);
+               bch2_alloc_sectors_done(c, wp);
+
+               if (ret < 0)
+                       goto err;
+
+               if (ret) {
+                       skip_put = false;
+               } else {
                        /*
-                        * Otherwise, we do want to block the caller on alloc
-                        * failure instead of letting it queue up more and more
-                        * writes:
-                        * XXX: this technically needs a try_to_freeze() -
-                        * except that that's not safe because caller may have
-                        * issued other IO... hmm..
+                        * for the skip_put optimization this has to be set
+                        * before we submit the bio:
                         */
-                       closure_sync(cl);
-                       continue;
+                       op->flags |= BCH_WRITE_DONE;
                }
 
-               BUG_ON(b - c->open_buckets == 0 ||
-                      b - c->open_buckets > U8_MAX);
-               op->open_buckets[open_bucket_nr++] = b - c->open_buckets;
+               bio->bi_end_io  = bch2_write_endio;
+               bio->bi_private = &op->cl;
+               bio->bi_opf |= REQ_OP_WRITE;
 
-               ret = bch2_write_extent(op, b, bio);
+               if (!skip_put)
+                       closure_get(bio->bi_private);
+               else
+                       op->flags |= BCH_WRITE_SKIP_CLOSURE_PUT;
 
-               bch2_alloc_sectors_done(c, op->wp, b);
+               key_to_write = (void *) (op->insert_keys.keys_p +
+                                        key_to_write_offset);
 
-               if (ret < 0)
-                       goto err;
+               bch2_submit_wbio_replicas(to_wbio(bio), c, BCH_DATA_user,
+                                         key_to_write);
        } while (ret);
 
-       op->flags |= BCH_WRITE_DONE;
-       continue_at(cl, bch2_write_index, index_update_wq(op));
+       if (!skip_put)
+               continue_at(cl, bch2_write_index, index_update_wq(op));
+out:
+       memalloc_nofs_restore(nofs_flags);
+       return;
 err:
-       if (op->flags & BCH_WRITE_DISCARD_ON_ERROR) {
-               /*
-                * If we were writing cached data, not doing the write is fine
-                * so long as we discard whatever would have been overwritten -
-                * then it's equivalent to doing the write and immediately
-                * reclaiming it.
-                */
-
-               bch2_write_discard(cl);
-       } else {
-               /*
-                * Right now we can only error here if we went RO - the
-                * allocation failed, but we already checked for -ENOSPC when we
-                * got our reservation.
-                *
-                * XXX capacity might have changed, but we don't check for that
-                * yet:
-                */
-               op->error = ret;
-       }
-
+       op->error = ret;
        op->flags |= BCH_WRITE_DONE;
 
+       continue_at(cl, bch2_write_index, index_update_wq(op));
+       goto out;
+flush_io:
        /*
-        * No reason not to insert keys for whatever data was successfully
-        * written (especially for a cmpxchg operation that's moving data
-        * around)
+        * If the write can't all be submitted at once, we generally want to
+        * block synchronously as that signals backpressure to the caller.
+        *
+        * However, if we're running out of a workqueue, we can't block here
+        * because we'll be blocking other work items from completing:
         */
-       continue_at(cl, !bch2_keylist_empty(&op->insert_keys)
-                   ? bch2_write_index
-                   : bch2_write_done, index_update_wq(op));
-}
+       if (current->flags & PF_WQ_WORKER) {
+               continue_at(cl, bch2_write_index, index_update_wq(op));
+               goto out;
+       }
 
-void bch2_wake_delayed_writes(unsigned long data)
-{
-       struct bch_fs *c = (void *) data;
-       struct bch_write_op *op;
-       unsigned long flags;
+       closure_sync(cl);
 
-       spin_lock_irqsave(&c->foreground_write_pd_lock, flags);
+       if (!bch2_keylist_empty(&op->insert_keys)) {
+               __bch2_write_index(op);
 
-       while ((op = c->write_wait_head)) {
-               if (time_after(op->expires, jiffies)) {
-                       mod_timer(&c->foreground_write_wakeup, op->expires);
-                       break;
+               if (op->error) {
+                       op->flags |= BCH_WRITE_DONE;
+                       continue_at_nobarrier(cl, bch2_write_done, NULL);
+                       goto out;
                }
+       }
+
+       goto again;
+}
+
+static void bch2_write_data_inline(struct bch_write_op *op, unsigned data_len)
+{
+       struct closure *cl = &op->cl;
+       struct bio *bio = &op->wbio.bio;
+       struct bvec_iter iter;
+       struct bkey_i_inline_data *id;
+       unsigned sectors;
+       int ret;
 
-               c->write_wait_head = op->next;
-               if (!c->write_wait_head)
-                       c->write_wait_tail = NULL;
+       bch2_check_set_feature(op->c, BCH_FEATURE_inline_data);
 
-               closure_put(&op->cl);
+       ret = bch2_keylist_realloc(&op->insert_keys, op->inline_keys,
+                                  ARRAY_SIZE(op->inline_keys),
+                                  BKEY_U64s + DIV_ROUND_UP(data_len, 8));
+       if (ret) {
+               op->error = ret;
+               goto err;
        }
 
-       spin_unlock_irqrestore(&c->foreground_write_pd_lock, flags);
+       sectors = bio_sectors(bio);
+       op->pos.offset += sectors;
+
+       id = bkey_inline_data_init(op->insert_keys.top);
+       id->k.p         = op->pos;
+       id->k.version   = op->version;
+       id->k.size      = sectors;
+
+       iter = bio->bi_iter;
+       iter.bi_size = data_len;
+       memcpy_from_bio(id->v.data, bio, iter);
+
+       while (data_len & 7)
+               id->v.data[data_len++] = '\0';
+       set_bkey_val_bytes(&id->k, data_len);
+       bch2_keylist_push(&op->insert_keys);
+
+       op->flags |= BCH_WRITE_WROTE_DATA_INLINE;
+       op->flags |= BCH_WRITE_DONE;
+
+       continue_at_nobarrier(cl, bch2_write_index, NULL);
+       return;
+err:
+       bch2_write_done(&op->cl);
 }
 
 /**
@@ -701,288 +1262,582 @@ void bch2_wake_delayed_writes(unsigned long data)
  * after the data is written it calls bch_journal, and after the keys have been
  * added to the next journal write they're inserted into the btree.
  *
- * It inserts the data in op->bio; bi_sector is used for the key offset, and
- * op->inode is used for the key inode.
- *
  * If op->discard is true, instead of inserting the data it invalidates the
  * region of the cache represented by op->bio and op->inode.
  */
 void bch2_write(struct closure *cl)
 {
        struct bch_write_op *op = container_of(cl, struct bch_write_op, cl);
-       struct bio *bio = &op->bio->bio;
+       struct bio *bio = &op->wbio.bio;
        struct bch_fs *c = op->c;
-       u64 inode = op->pos.inode;
+       unsigned data_len;
+
+       BUG_ON(!op->nr_replicas);
+       BUG_ON(!op->write_point.v);
+       BUG_ON(!bkey_cmp(op->pos, POS_MAX));
+
+       op->start_time = local_clock();
+       bch2_keylist_init(&op->insert_keys, op->inline_keys);
+       wbio_init(bio)->put_bio = false;
+
+       if (bio_sectors(bio) & (c->opts.block_size - 1)) {
+               __bcache_io_error(c, "misaligned write");
+               op->error = -EIO;
+               goto err;
+       }
 
        if (c->opts.nochanges ||
            !percpu_ref_tryget(&c->writes)) {
-               __bcache_io_error(c, "read only");
+               if (!(op->flags & BCH_WRITE_FROM_INTERNAL))
+                       __bcache_io_error(c, "read only");
                op->error = -EROFS;
-               bch2_disk_reservation_put(c, &op->res);
+               goto err;
+       }
+
+       /*
+        * Can't ratelimit copygc - we'd deadlock:
+        */
+       if (!(op->flags & BCH_WRITE_FROM_INTERNAL))
+               down(&c->io_in_flight);
+
+       bch2_increment_clock(c, bio_sectors(bio), WRITE);
+
+       data_len = min_t(u64, bio->bi_iter.bi_size,
+                        op->new_i_size - (op->pos.offset << 9));
+
+       if (c->opts.inline_data &&
+           data_len <= min(block_bytes(c) / 2, 1024U)) {
+               bch2_write_data_inline(op, data_len);
+               return;
+       }
+
+       continue_at_nobarrier(cl, __bch2_write, NULL);
+       return;
+err:
+       bch2_disk_reservation_put(c, &op->res);
+
+       if (op->end_io) {
+               EBUG_ON(cl->parent);
+               closure_debug_destroy(cl);
+               op->end_io(op);
+       } else {
                closure_return(cl);
        }
+}
+
+/* Cache promotion on read */
+
+struct promote_op {
+       struct closure          cl;
+       struct rcu_head         rcu;
+       u64                     start_time;
+
+       struct rhash_head       hash;
+       struct bpos             pos;
+
+       struct migrate_write    write;
+       struct bio_vec          bi_inline_vecs[0]; /* must be last */
+};
+
+static const struct rhashtable_params bch_promote_params = {
+       .head_offset    = offsetof(struct promote_op, hash),
+       .key_offset     = offsetof(struct promote_op, pos),
+       .key_len        = sizeof(struct bpos),
+};
+
+static inline bool should_promote(struct bch_fs *c, struct bkey_s_c k,
+                                 struct bpos pos,
+                                 struct bch_io_opts opts,
+                                 unsigned flags)
+{
+       if (!(flags & BCH_READ_MAY_PROMOTE))
+               return false;
+
+       if (!opts.promote_target)
+               return false;
+
+       if (bch2_bkey_has_target(c, k, opts.promote_target))
+               return false;
+
+       if (bch2_target_congested(c, opts.promote_target)) {
+               /* XXX trace this */
+               return false;
+       }
+
+       if (rhashtable_lookup_fast(&c->promote_table, &pos,
+                                  bch_promote_params))
+               return false;
+
+       return true;
+}
+
+static void promote_free(struct bch_fs *c, struct promote_op *op)
+{
+       int ret;
+
+       ret = rhashtable_remove_fast(&c->promote_table, &op->hash,
+                                    bch_promote_params);
+       BUG_ON(ret);
+       percpu_ref_put(&c->writes);
+       kfree_rcu(op, rcu);
+}
+
+static void promote_done(struct closure *cl)
+{
+       struct promote_op *op =
+               container_of(cl, struct promote_op, cl);
+       struct bch_fs *c = op->write.op.c;
+
+       bch2_time_stats_update(&c->times[BCH_TIME_data_promote],
+                              op->start_time);
+
+       bch2_bio_free_pages_pool(c, &op->write.op.wbio.bio);
+       promote_free(c, op);
+}
+
+static void promote_start(struct promote_op *op, struct bch_read_bio *rbio)
+{
+       struct bch_fs *c = rbio->c;
+       struct closure *cl = &op->cl;
+       struct bio *bio = &op->write.op.wbio.bio;
+
+       trace_promote(&rbio->bio);
+
+       /* we now own pages: */
+       BUG_ON(!rbio->bounce);
+       BUG_ON(rbio->bio.bi_vcnt > bio->bi_max_vecs);
+
+       memcpy(bio->bi_io_vec, rbio->bio.bi_io_vec,
+              sizeof(struct bio_vec) * rbio->bio.bi_vcnt);
+       swap(bio->bi_vcnt, rbio->bio.bi_vcnt);
+
+       bch2_migrate_read_done(&op->write, rbio);
+
+       closure_init(cl, NULL);
+       closure_call(&op->write.op.cl, bch2_write, c->wq, cl);
+       closure_return_with_destructor(cl, promote_done);
+}
+
+static struct promote_op *__promote_alloc(struct bch_fs *c,
+                                         enum btree_id btree_id,
+                                         struct bkey_s_c k,
+                                         struct bpos pos,
+                                         struct extent_ptr_decoded *pick,
+                                         struct bch_io_opts opts,
+                                         unsigned sectors,
+                                         struct bch_read_bio **rbio)
+{
+       struct promote_op *op = NULL;
+       struct bio *bio;
+       unsigned pages = DIV_ROUND_UP(sectors, PAGE_SECTORS);
+       int ret;
+
+       if (!percpu_ref_tryget(&c->writes))
+               return NULL;
+
+       op = kzalloc(sizeof(*op) + sizeof(struct bio_vec) * pages, GFP_NOIO);
+       if (!op)
+               goto err;
+
+       op->start_time = local_clock();
+       op->pos = pos;
+
+       /*
+        * We don't use the mempool here because extents that aren't
+        * checksummed or compressed can be too big for the mempool:
+        */
+       *rbio = kzalloc(sizeof(struct bch_read_bio) +
+                       sizeof(struct bio_vec) * pages,
+                       GFP_NOIO);
+       if (!*rbio)
+               goto err;
+
+       rbio_init(&(*rbio)->bio, opts);
+       bio_init(&(*rbio)->bio, (*rbio)->bio.bi_inline_vecs, pages);
+
+       if (bch2_bio_alloc_pages(&(*rbio)->bio, sectors << 9,
+                                GFP_NOIO))
+               goto err;
+
+       (*rbio)->bounce         = true;
+       (*rbio)->split          = true;
+       (*rbio)->kmalloc        = true;
+
+       if (rhashtable_lookup_insert_fast(&c->promote_table, &op->hash,
+                                         bch_promote_params))
+               goto err;
+
+       bio = &op->write.op.wbio.bio;
+       bio_init(bio, bio->bi_inline_vecs, pages);
+
+       ret = bch2_migrate_write_init(c, &op->write,
+                       writepoint_hashed((unsigned long) current),
+                       opts,
+                       DATA_PROMOTE,
+                       (struct data_opts) {
+                               .target         = opts.promote_target,
+                               .nr_replicas    = 1,
+                       },
+                       btree_id, k);
+       BUG_ON(ret);
+
+       return op;
+err:
+       if (*rbio)
+               bio_free_pages(&(*rbio)->bio);
+       kfree(*rbio);
+       *rbio = NULL;
+       kfree(op);
+       percpu_ref_put(&c->writes);
+       return NULL;
+}
+
+noinline
+static struct promote_op *promote_alloc(struct bch_fs *c,
+                                              struct bvec_iter iter,
+                                              struct bkey_s_c k,
+                                              struct extent_ptr_decoded *pick,
+                                              struct bch_io_opts opts,
+                                              unsigned flags,
+                                              struct bch_read_bio **rbio,
+                                              bool *bounce,
+                                              bool *read_full)
+{
+       bool promote_full = *read_full || READ_ONCE(c->promote_whole_extents);
+       /* data might have to be decompressed in the write path: */
+       unsigned sectors = promote_full
+               ? max(pick->crc.compressed_size, pick->crc.live_size)
+               : bvec_iter_sectors(iter);
+       struct bpos pos = promote_full
+               ? bkey_start_pos(k.k)
+               : POS(k.k->p.inode, iter.bi_sector);
+       struct promote_op *promote;
+
+       if (!should_promote(c, k, pos, opts, flags))
+               return NULL;
+
+       promote = __promote_alloc(c,
+                                 k.k->type == KEY_TYPE_reflink_v
+                                 ? BTREE_ID_REFLINK
+                                 : BTREE_ID_EXTENTS,
+                                 k, pos, pick, opts, sectors, rbio);
+       if (!promote)
+               return NULL;
+
+       *bounce         = true;
+       *read_full      = promote_full;
+       return promote;
+}
+
+/* Read */
+
+#define READ_RETRY_AVOID       1
+#define READ_RETRY             2
+#define READ_ERR               3
+
+enum rbio_context {
+       RBIO_CONTEXT_NULL,
+       RBIO_CONTEXT_HIGHPRI,
+       RBIO_CONTEXT_UNBOUND,
+};
+
+static inline struct bch_read_bio *
+bch2_rbio_parent(struct bch_read_bio *rbio)
+{
+       return rbio->split ? rbio->parent : rbio;
+}
+
+__always_inline
+static void bch2_rbio_punt(struct bch_read_bio *rbio, work_func_t fn,
+                          enum rbio_context context,
+                          struct workqueue_struct *wq)
+{
+       if (context <= rbio->context) {
+               fn(&rbio->work);
+       } else {
+               rbio->work.func         = fn;
+               rbio->context           = context;
+               queue_work(wq, &rbio->work);
+       }
+}
 
-       if (bversion_zero(op->version) &&
-           bch2_csum_type_is_encryption(op->csum_type))
-               op->version.lo =
-                       atomic64_inc_return(&c->key_version) + 1;
+static inline struct bch_read_bio *bch2_rbio_free(struct bch_read_bio *rbio)
+{
+       BUG_ON(rbio->bounce && !rbio->split);
 
-       if (!(op->flags & BCH_WRITE_DISCARD))
-               bch2_increment_clock(c, bio_sectors(bio), WRITE);
+       if (rbio->promote)
+               promote_free(rbio->c, rbio->promote);
+       rbio->promote = NULL;
 
-       /* Don't call bch2_next_delay() if rate is >= 1 GB/sec */
+       if (rbio->bounce)
+               bch2_bio_free_pages_pool(rbio->c, &rbio->bio);
 
-       if (c->foreground_write_ratelimit_enabled &&
-           c->foreground_write_pd.rate.rate < (1 << 30) &&
-           !(op->flags & BCH_WRITE_DISCARD) && op->wp->throttle) {
-               unsigned long flags;
-               u64 delay;
+       if (rbio->split) {
+               struct bch_read_bio *parent = rbio->parent;
 
-               spin_lock_irqsave(&c->foreground_write_pd_lock, flags);
-               bch2_ratelimit_increment(&c->foreground_write_pd.rate,
-                                       bio->bi_iter.bi_size);
+               if (rbio->kmalloc)
+                       kfree(rbio);
+               else
+                       bio_put(&rbio->bio);
 
-               delay = bch2_ratelimit_delay(&c->foreground_write_pd.rate);
+               rbio = parent;
+       }
 
-               if (delay >= HZ / 100) {
-                       trace_write_throttle(c, inode, bio, delay);
+       return rbio;
+}
 
-                       closure_get(&op->cl); /* list takes a ref */
+/*
+ * Only called on a top level bch_read_bio to complete an entire read request,
+ * not a split:
+ */
+static void bch2_rbio_done(struct bch_read_bio *rbio)
+{
+       if (rbio->start_time)
+               bch2_time_stats_update(&rbio->c->times[BCH_TIME_data_read],
+                                      rbio->start_time);
+       bio_endio(&rbio->bio);
+}
 
-                       op->expires = jiffies + delay;
-                       op->next = NULL;
+static void bch2_read_retry_nodecode(struct bch_fs *c, struct bch_read_bio *rbio,
+                                    struct bvec_iter bvec_iter, u64 inode,
+                                    struct bch_io_failures *failed,
+                                    unsigned flags)
+{
+       struct btree_trans trans;
+       struct btree_iter *iter;
+       struct bkey_on_stack sk;
+       struct bkey_s_c k;
+       int ret;
 
-                       if (c->write_wait_tail)
-                               c->write_wait_tail->next = op;
-                       else
-                               c->write_wait_head = op;
-                       c->write_wait_tail = op;
+       flags &= ~BCH_READ_LAST_FRAGMENT;
+       flags |= BCH_READ_MUST_CLONE;
 
-                       if (!timer_pending(&c->foreground_write_wakeup))
-                               mod_timer(&c->foreground_write_wakeup,
-                                         op->expires);
+       bkey_on_stack_init(&sk);
+       bch2_trans_init(&trans, c, 0, 0);
 
-                       spin_unlock_irqrestore(&c->foreground_write_pd_lock,
-                                              flags);
-                       continue_at(cl, __bch2_write, index_update_wq(op));
-               }
+       iter = bch2_trans_get_iter(&trans, BTREE_ID_EXTENTS,
+                                  rbio->pos, BTREE_ITER_SLOTS);
+retry:
+       rbio->bio.bi_status = 0;
 
-               spin_unlock_irqrestore(&c->foreground_write_pd_lock, flags);
-       }
+       k = bch2_btree_iter_peek_slot(iter);
+       if (bkey_err(k))
+               goto err;
 
-       continue_at_nobarrier(cl, __bch2_write, NULL);
-}
+       bkey_on_stack_reassemble(&sk, c, k);
+       k = bkey_i_to_s_c(sk.k);
+       bch2_trans_unlock(&trans);
 
-void bch2_write_op_init(struct bch_write_op *op, struct bch_fs *c,
-                      struct bch_write_bio *bio, struct disk_reservation res,
-                      struct write_point *wp, struct bpos pos,
-                      u64 *journal_seq, unsigned flags)
-{
-       EBUG_ON(res.sectors && !res.nr_replicas);
-
-       op->c           = c;
-       op->io_wq       = index_update_wq(op);
-       op->bio         = bio;
-       op->written     = 0;
-       op->error       = 0;
-       op->flags       = flags;
-       op->csum_type   = bch2_data_checksum_type(c);
-       op->compression_type = c->opts.compression;
-       op->nr_replicas = res.nr_replicas;
-       op->alloc_reserve = RESERVE_NONE;
-       op->nonce       = 0;
-       op->pos         = pos;
-       op->version     = ZERO_VERSION;
-       op->res         = res;
-       op->wp          = wp;
-
-       if (journal_seq) {
-               op->journal_seq_p = journal_seq;
-               op->flags |= BCH_WRITE_JOURNAL_SEQ_PTR;
-       } else {
-               op->journal_seq = 0;
+       if (!bch2_bkey_matches_ptr(c, k,
+                                  rbio->pick.ptr,
+                                  rbio->pos.offset -
+                                  rbio->pick.crc.offset)) {
+               /* extent we wanted to read no longer exists: */
+               rbio->hole = true;
+               goto out;
        }
 
-       op->index_update_fn = bch2_write_index_default;
-
-       bch2_keylist_init(&op->insert_keys,
-                         op->inline_keys,
-                         ARRAY_SIZE(op->inline_keys));
-
-       if (version_stress_test(c))
-               get_random_bytes(&op->version, sizeof(op->version));
+       ret = __bch2_read_extent(&trans, rbio, bvec_iter, k, 0, failed, flags);
+       if (ret == READ_RETRY)
+               goto retry;
+       if (ret)
+               goto err;
+out:
+       bch2_rbio_done(rbio);
+       bch2_trans_exit(&trans);
+       bkey_on_stack_exit(&sk, c);
+       return;
+err:
+       rbio->bio.bi_status = BLK_STS_IOERR;
+       goto out;
 }
 
-/* Discard */
-
-/* bch_discard - discard a range of keys from start_key to end_key.
- * @c          filesystem
- * @start_key  pointer to start location
- *             NOTE: discard starts at bkey_start_offset(start_key)
- * @end_key    pointer to end location
- *             NOTE: discard ends at KEY_OFFSET(end_key)
- * @version    version of discard (0ULL if none)
- *
- * Returns:
- *      0 on success
- *     <0 on error
- *
- * XXX: this needs to be refactored with inode_truncate, or more
- *     appropriately inode_truncate should call this
- */
-int bch2_discard(struct bch_fs *c, struct bpos start,
-                struct bpos end, struct bversion version,
-                struct disk_reservation *disk_res,
-                struct extent_insert_hook *hook,
-                u64 *journal_seq)
+static void bch2_read_retry(struct bch_fs *c, struct bch_read_bio *rbio,
+                           struct bvec_iter bvec_iter, u64 inode,
+                           struct bch_io_failures *failed, unsigned flags)
 {
-       return bch2_btree_delete_range(c, BTREE_ID_EXTENTS, start, end, version,
-                                     disk_res, hook, journal_seq);
-}
+       struct btree_trans trans;
+       struct btree_iter *iter;
+       struct bkey_on_stack sk;
+       struct bkey_s_c k;
+       int ret;
 
-/* Cache promotion on read */
+       flags &= ~BCH_READ_LAST_FRAGMENT;
+       flags |= BCH_READ_MUST_CLONE;
 
-struct cache_promote_op {
-       struct closure          cl;
-       struct migrate_write    write;
-       struct bio_vec          bi_inline_vecs[0]; /* must be last */
-};
+       bkey_on_stack_init(&sk);
+       bch2_trans_init(&trans, c, 0, 0);
+retry:
+       bch2_trans_begin(&trans);
 
-/* Read */
+       for_each_btree_key(&trans, iter, BTREE_ID_EXTENTS,
+                          POS(inode, bvec_iter.bi_sector),
+                          BTREE_ITER_SLOTS, k, ret) {
+               unsigned bytes, sectors, offset_into_extent;
 
-static int bio_checksum_uncompress(struct bch_fs *c,
-                                  struct bch_read_bio *rbio)
-{
-       struct bio *src = &rbio->bio;
-       struct bio *dst = &bch2_rbio_parent(rbio)->bio;
-       struct bvec_iter dst_iter = rbio->parent_iter;
-       struct nonce nonce = extent_nonce(rbio->version,
-                               rbio->crc.nonce,
-                               crc_uncompressed_size(NULL, &rbio->crc),
-                               rbio->crc.compression_type);
-       struct bch_csum csum;
-       int ret = 0;
+               bkey_on_stack_reassemble(&sk, c, k);
+               k = bkey_i_to_s_c(sk.k);
 
-       /*
-        * reset iterator for checksumming and copying bounced data: here we've
-        * set rbio->compressed_size to the amount of data we actually read,
-        * which was not necessarily the full extent if we were only bouncing
-        * in order to promote
-        */
-       if (rbio->bounce) {
-               src->bi_iter.bi_size    = crc_compressed_size(NULL, &rbio->crc) << 9;
-               src->bi_iter.bi_idx     = 0;
-               src->bi_iter.bi_bvec_done = 0;
-       } else {
-               src->bi_iter = rbio->parent_iter;
-       }
+               offset_into_extent = iter->pos.offset -
+                       bkey_start_offset(k.k);
+               sectors = k.k->size - offset_into_extent;
 
-       csum = bch2_checksum_bio(c, rbio->crc.csum_type, nonce, src);
-       if (bch2_dev_nonfatal_io_err_on(bch2_crc_cmp(rbio->crc.csum, csum),
-                                       rbio->ca,
-                       "data checksum error, inode %llu offset %llu: expected %0llx%0llx got %0llx%0llx (type %u)",
-                       rbio->inode, (u64) rbio->parent_iter.bi_sector << 9,
-                       rbio->crc.csum.hi, rbio->crc.csum.lo, csum.hi, csum.lo,
-                       rbio->crc.csum_type))
-               ret = -EIO;
+               ret = bch2_read_indirect_extent(&trans,
+                                       &offset_into_extent, &sk);
+               if (ret)
+                       break;
 
-       /*
-        * If there was a checksum error, still copy the data back - unless it
-        * was compressed, we don't want to decompress bad data:
-        */
-       if (rbio->crc.compression_type != BCH_COMPRESSION_NONE) {
-               if (!ret) {
-                       bch2_encrypt_bio(c, rbio->crc.csum_type, nonce, src);
-                       ret = bch2_bio_uncompress(c, src, dst,
-                                                dst_iter, rbio->crc);
-                       if (ret)
-                               __bcache_io_error(c, "decompression error");
-               }
-       } else if (rbio->bounce) {
-               bio_advance(src, rbio->crc.offset << 9);
+               sectors = min(sectors, k.k->size - offset_into_extent);
 
-               /* don't need to decrypt the entire bio: */
-               BUG_ON(src->bi_iter.bi_size < dst_iter.bi_size);
-               src->bi_iter.bi_size = dst_iter.bi_size;
+               bch2_trans_unlock(&trans);
+
+               bytes = min(sectors, bvec_iter_sectors(bvec_iter)) << 9;
+               swap(bvec_iter.bi_size, bytes);
 
-               nonce = nonce_add(nonce, rbio->crc.offset << 9);
+               ret = __bch2_read_extent(&trans, rbio, bvec_iter, k,
+                               offset_into_extent, failed, flags);
+               switch (ret) {
+               case READ_RETRY:
+                       goto retry;
+               case READ_ERR:
+                       goto err;
+               };
 
-               bch2_encrypt_bio(c, rbio->crc.csum_type,
-                               nonce, src);
+               if (bytes == bvec_iter.bi_size)
+                       goto out;
 
-               bio_copy_data_iter(dst, dst_iter,
-                                  src, src->bi_iter);
-       } else {
-               bch2_encrypt_bio(c, rbio->crc.csum_type, nonce, src);
+               swap(bvec_iter.bi_size, bytes);
+               bio_advance_iter(&rbio->bio, &bvec_iter, bytes);
        }
 
-       return ret;
+       if (ret == -EINTR)
+               goto retry;
+       /*
+        * If we get here, it better have been because there was an error
+        * reading a btree node
+        */
+       BUG_ON(!ret);
+       __bcache_io_error(c, "btree IO error: %i", ret);
+err:
+       rbio->bio.bi_status = BLK_STS_IOERR;
+out:
+       bch2_trans_exit(&trans);
+       bkey_on_stack_exit(&sk, c);
+       bch2_rbio_done(rbio);
 }
 
-static void bch2_rbio_free(struct bch_read_bio *rbio)
+static void bch2_rbio_retry(struct work_struct *work)
 {
-       struct bch_fs *c = rbio->c;
-       struct bio *bio = &rbio->bio;
+       struct bch_read_bio *rbio =
+               container_of(work, struct bch_read_bio, work);
+       struct bch_fs *c        = rbio->c;
+       struct bvec_iter iter   = rbio->bvec_iter;
+       unsigned flags          = rbio->flags;
+       u64 inode               = rbio->pos.inode;
+       struct bch_io_failures failed = { .nr = 0 };
 
-       BUG_ON(rbio->ca);
-       BUG_ON(!rbio->split);
+       trace_read_retry(&rbio->bio);
 
-       if (rbio->promote)
-               kfree(rbio->promote);
-       if (rbio->bounce)
-               bch2_bio_free_pages_pool(c, bio);
+       if (rbio->retry == READ_RETRY_AVOID)
+               bch2_mark_io_failure(&failed, &rbio->pick);
+
+       rbio->bio.bi_status = 0;
+
+       rbio = bch2_rbio_free(rbio);
 
-       bio_put(bio);
+       flags |= BCH_READ_IN_RETRY;
+       flags &= ~BCH_READ_MAY_PROMOTE;
+
+       if (flags & BCH_READ_NODECODE)
+               bch2_read_retry_nodecode(c, rbio, iter, inode, &failed, flags);
+       else
+               bch2_read_retry(c, rbio, iter, inode, &failed, flags);
 }
 
-static void bch2_rbio_done(struct bch_read_bio *rbio)
+static void bch2_rbio_error(struct bch_read_bio *rbio, int retry,
+                           blk_status_t error)
 {
-       struct bio *orig = &bch2_rbio_parent(rbio)->bio;
+       rbio->retry = retry;
 
-       percpu_ref_put(&rbio->ca->io_ref);
-       rbio->ca = NULL;
+       if (rbio->flags & BCH_READ_IN_RETRY)
+               return;
 
-       if (rbio->split) {
-               if (rbio->bio.bi_error)
-                       orig->bi_error = rbio->bio.bi_error;
+       if (retry == READ_ERR) {
+               rbio = bch2_rbio_free(rbio);
 
-               bio_endio(orig);
-               bch2_rbio_free(rbio);
+               rbio->bio.bi_status = error;
+               bch2_rbio_done(rbio);
        } else {
-               if (rbio->promote)
-                       kfree(rbio->promote);
-
-               orig->bi_end_io = rbio->orig_bi_end_io;
-               bio_endio_nodec(orig);
+               bch2_rbio_punt(rbio, bch2_rbio_retry,
+                              RBIO_CONTEXT_UNBOUND, system_unbound_wq);
        }
 }
 
-static void bch2_rbio_error(struct bch_read_bio *rbio, int error)
+static int __bch2_rbio_narrow_crcs(struct btree_trans *trans,
+                                  struct bch_read_bio *rbio)
 {
-       bch2_rbio_parent(rbio)->bio.bi_error = error;
-       bch2_rbio_done(rbio);
-}
+       struct bch_fs *c = rbio->c;
+       u64 data_offset = rbio->pos.offset - rbio->pick.crc.offset;
+       struct bch_extent_crc_unpacked new_crc;
+       struct btree_iter *iter = NULL;
+       struct bkey_i *new;
+       struct bkey_s_c k;
+       int ret = 0;
 
-static void bch2_rbio_retry(struct bch_fs *c, struct bch_read_bio *rbio)
-{
-       unsigned long flags;
+       if (crc_is_compressed(rbio->pick.crc))
+               return 0;
+
+       iter = bch2_trans_get_iter(trans, BTREE_ID_EXTENTS, rbio->pos,
+                                  BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
+       if ((ret = PTR_ERR_OR_ZERO(iter)))
+               goto out;
+
+       k = bch2_btree_iter_peek_slot(iter);
+       if ((ret = bkey_err(k)))
+               goto out;
 
-       percpu_ref_put(&rbio->ca->io_ref);
-       rbio->ca = NULL;
+       /*
+        * going to be temporarily appending another checksum entry:
+        */
+       new = bch2_trans_kmalloc(trans, bkey_bytes(k.k) +
+                                BKEY_EXTENT_U64s_MAX * 8);
+       if ((ret = PTR_ERR_OR_ZERO(new)))
+               goto out;
+
+       bkey_reassemble(new, k);
+       k = bkey_i_to_s_c(new);
+
+       if (bversion_cmp(k.k->version, rbio->version) ||
+           !bch2_bkey_matches_ptr(c, k, rbio->pick.ptr, data_offset))
+               goto out;
+
+       /* Extent was merged? */
+       if (bkey_start_offset(k.k) < data_offset ||
+           k.k->p.offset > data_offset + rbio->pick.crc.uncompressed_size)
+               goto out;
+
+       if (bch2_rechecksum_bio(c, &rbio->bio, rbio->version,
+                       rbio->pick.crc, NULL, &new_crc,
+                       bkey_start_offset(k.k) - data_offset, k.k->size,
+                       rbio->pick.crc.csum_type)) {
+               bch_err(c, "error verifying existing checksum while narrowing checksum (memory corruption?)");
+               ret = 0;
+               goto out;
+       }
+
+       if (!bch2_bkey_narrow_crcs(new, new_crc))
+               goto out;
 
-       spin_lock_irqsave(&c->read_retry_lock, flags);
-       bio_list_add(&c->read_retry_list, &rbio->bio);
-       spin_unlock_irqrestore(&c->read_retry_lock, flags);
-       queue_work(c->wq, &c->read_retry_work);
+       bch2_trans_update(trans, iter, new, 0);
+out:
+       bch2_trans_iter_put(trans, iter);
+       return ret;
 }
 
-static void cache_promote_done(struct closure *cl)
+static noinline void bch2_rbio_narrow_crcs(struct bch_read_bio *rbio)
 {
-       struct cache_promote_op *op =
-               container_of(cl, struct cache_promote_op, cl);
-
-       bch2_bio_free_pages_pool(op->write.op.c, &op->write.wbio.bio);
-       kfree(op);
+       bch2_trans_do(rbio->c, NULL, NULL, BTREE_INSERT_NOFAIL,
+                     __bch2_rbio_narrow_crcs(&trans, rbio));
 }
 
 /* Inner part that may run in process context */
@@ -990,164 +1845,304 @@ static void __bch2_read_endio(struct work_struct *work)
 {
        struct bch_read_bio *rbio =
                container_of(work, struct bch_read_bio, work);
-       struct bch_fs *c = rbio->c;
-       int ret;
+       struct bch_fs *c        = rbio->c;
+       struct bch_dev *ca      = bch_dev_bkey_exists(c, rbio->pick.ptr.dev);
+       struct bio *src         = &rbio->bio;
+       struct bio *dst         = &bch2_rbio_parent(rbio)->bio;
+       struct bvec_iter dst_iter = rbio->bvec_iter;
+       struct bch_extent_crc_unpacked crc = rbio->pick.crc;
+       struct nonce nonce = extent_nonce(rbio->version, crc);
+       struct bch_csum csum;
 
-       ret = bio_checksum_uncompress(c, rbio);
-       if (ret) {
-               /*
-                * Checksum error: if the bio wasn't bounced, we may have been
-                * reading into buffers owned by userspace (that userspace can
-                * scribble over) - retry the read, bouncing it this time:
-                */
-               if (!rbio->bounce && (rbio->flags & BCH_READ_USER_MAPPED)) {
-                       rbio->flags |= BCH_READ_FORCE_BOUNCE;
-                       bch2_rbio_retry(c, rbio);
-               } else {
-                       bch2_rbio_error(rbio, -EIO);
-               }
-               return;
+       /* Reset iterator for checksumming and copying bounced data: */
+       if (rbio->bounce) {
+               src->bi_iter.bi_size            = crc.compressed_size << 9;
+               src->bi_iter.bi_idx             = 0;
+               src->bi_iter.bi_bvec_done       = 0;
+       } else {
+               src->bi_iter                    = rbio->bvec_iter;
        }
 
-       if (rbio->promote) {
-               struct cache_promote_op *promote = rbio->promote;
-               struct closure *cl = &promote->cl;
-
-               BUG_ON(!rbio->split || !rbio->bounce);
+       csum = bch2_checksum_bio(c, crc.csum_type, nonce, src);
+       if (bch2_crc_cmp(csum, rbio->pick.crc.csum))
+               goto csum_err;
 
-               trace_promote(&rbio->bio);
+       if (unlikely(rbio->narrow_crcs))
+               bch2_rbio_narrow_crcs(rbio);
 
-               /* we now own pages: */
-               swap(promote->write.wbio.bio.bi_vcnt, rbio->bio.bi_vcnt);
-               rbio->promote = NULL;
+       if (rbio->flags & BCH_READ_NODECODE)
+               goto nodecode;
 
-               bch2_rbio_done(rbio);
+       /* Adjust crc to point to subset of data we want: */
+       crc.offset     += rbio->offset_into_extent;
+       crc.live_size   = bvec_iter_sectors(rbio->bvec_iter);
 
-               closure_init(cl, &c->cl);
-               closure_call(&promote->write.op.cl, bch2_write, c->wq, cl);
-               closure_return_with_destructor(cl, cache_promote_done);
+       if (crc_is_compressed(crc)) {
+               bch2_encrypt_bio(c, crc.csum_type, nonce, src);
+               if (bch2_bio_uncompress(c, src, dst, dst_iter, crc))
+                       goto decompression_err;
        } else {
+               /* don't need to decrypt the entire bio: */
+               nonce = nonce_add(nonce, crc.offset << 9);
+               bio_advance(src, crc.offset << 9);
+
+               BUG_ON(src->bi_iter.bi_size < dst_iter.bi_size);
+               src->bi_iter.bi_size = dst_iter.bi_size;
+
+               bch2_encrypt_bio(c, crc.csum_type, nonce, src);
+
+               if (rbio->bounce) {
+                       struct bvec_iter src_iter = src->bi_iter;
+                       bio_copy_data_iter(dst, &dst_iter, src, &src_iter);
+               }
+       }
+
+       if (rbio->promote) {
+               /*
+                * Re encrypt data we decrypted, so it's consistent with
+                * rbio->crc:
+                */
+               bch2_encrypt_bio(c, crc.csum_type, nonce, src);
+               promote_start(rbio->promote, rbio);
+               rbio->promote = NULL;
+       }
+nodecode:
+       if (likely(!(rbio->flags & BCH_READ_IN_RETRY))) {
+               rbio = bch2_rbio_free(rbio);
                bch2_rbio_done(rbio);
        }
+       return;
+csum_err:
+       /*
+        * Checksum error: if the bio wasn't bounced, we may have been
+        * reading into buffers owned by userspace (that userspace can
+        * scribble over) - retry the read, bouncing it this time:
+        */
+       if (!rbio->bounce && (rbio->flags & BCH_READ_USER_MAPPED)) {
+               rbio->flags |= BCH_READ_MUST_BOUNCE;
+               bch2_rbio_error(rbio, READ_RETRY, BLK_STS_IOERR);
+               return;
+       }
+
+       bch2_dev_io_error(ca,
+               "data checksum error, inode %llu offset %llu: expected %0llx:%0llx got %0llx:%0llx (type %u)",
+               rbio->pos.inode, (u64) rbio->bvec_iter.bi_sector,
+               rbio->pick.crc.csum.hi, rbio->pick.crc.csum.lo,
+               csum.hi, csum.lo, crc.csum_type);
+       bch2_rbio_error(rbio, READ_RETRY_AVOID, BLK_STS_IOERR);
+       return;
+decompression_err:
+       __bcache_io_error(c, "decompression error, inode %llu offset %llu",
+                         rbio->pos.inode,
+                         (u64) rbio->bvec_iter.bi_sector);
+       bch2_rbio_error(rbio, READ_ERR, BLK_STS_IOERR);
+       return;
 }
 
 static void bch2_read_endio(struct bio *bio)
 {
        struct bch_read_bio *rbio =
                container_of(bio, struct bch_read_bio, bio);
-       struct bch_fs *c = rbio->c;
+       struct bch_fs *c        = rbio->c;
+       struct bch_dev *ca      = bch_dev_bkey_exists(c, rbio->pick.ptr.dev);
+       struct workqueue_struct *wq = NULL;
+       enum rbio_context context = RBIO_CONTEXT_NULL;
+
+       if (rbio->have_ioref) {
+               bch2_latency_acct(ca, rbio->submit_time, READ);
+               percpu_ref_put(&ca->io_ref);
+       }
+
+       if (!rbio->split)
+               rbio->bio.bi_end_io = rbio->end_io;
 
-       if (bch2_dev_nonfatal_io_err_on(bio->bi_error, rbio->ca, "data read")) {
-               /* XXX: retry IO errors when we have another replica */
-               bch2_rbio_error(rbio, bio->bi_error);
+       if (bch2_dev_io_err_on(bio->bi_status, ca, "data read; %s",
+                              bch2_blk_status_to_str(bio->bi_status))) {
+               bch2_rbio_error(rbio, READ_RETRY_AVOID, bio->bi_status);
                return;
        }
 
-       if (rbio->ptr.cached &&
+       if (rbio->pick.ptr.cached &&
            (((rbio->flags & BCH_READ_RETRY_IF_STALE) && race_fault()) ||
-            ptr_stale(rbio->ca, &rbio->ptr))) {
+            ptr_stale(ca, &rbio->pick.ptr))) {
                atomic_long_inc(&c->read_realloc_races);
 
                if (rbio->flags & BCH_READ_RETRY_IF_STALE)
-                       bch2_rbio_retry(c, rbio);
+                       bch2_rbio_error(rbio, READ_RETRY, BLK_STS_AGAIN);
                else
-                       bch2_rbio_error(rbio, -EINTR);
+                       bch2_rbio_error(rbio, READ_ERR, BLK_STS_AGAIN);
                return;
        }
 
-       if (rbio->crc.compression_type ||
-           bch2_csum_type_is_encryption(rbio->crc.csum_type))
-               queue_work(system_unbound_wq, &rbio->work);
-       else if (rbio->crc.csum_type)
-               queue_work(system_highpri_wq, &rbio->work);
-       else
-               __bch2_read_endio(&rbio->work);
+       if (rbio->narrow_crcs ||
+           crc_is_compressed(rbio->pick.crc) ||
+           bch2_csum_type_is_encryption(rbio->pick.crc.csum_type))
+               context = RBIO_CONTEXT_UNBOUND, wq = system_unbound_wq;
+       else if (rbio->pick.crc.csum_type)
+               context = RBIO_CONTEXT_HIGHPRI, wq = system_highpri_wq;
+
+       bch2_rbio_punt(rbio, __bch2_read_endio, context, wq);
 }
 
-static bool should_promote(struct bch_fs *c,
-                          struct extent_pick_ptr *pick, unsigned flags)
+int __bch2_read_indirect_extent(struct btree_trans *trans,
+                               unsigned *offset_into_extent,
+                               struct bkey_on_stack *orig_k)
 {
-       if (!(flags & BCH_READ_PROMOTE))
-               return false;
+       struct btree_iter *iter;
+       struct bkey_s_c k;
+       u64 reflink_offset;
+       int ret;
 
-       if (percpu_ref_is_dying(&c->writes))
-               return false;
+       reflink_offset = le64_to_cpu(bkey_i_to_reflink_p(orig_k->k)->v.idx) +
+               *offset_into_extent;
+
+       iter = bch2_trans_get_iter(trans, BTREE_ID_REFLINK,
+                                  POS(0, reflink_offset),
+                                  BTREE_ITER_SLOTS);
+       ret = PTR_ERR_OR_ZERO(iter);
+       if (ret)
+               return ret;
+
+       k = bch2_btree_iter_peek_slot(iter);
+       ret = bkey_err(k);
+       if (ret)
+               goto err;
+
+       if (k.k->type != KEY_TYPE_reflink_v) {
+               __bcache_io_error(trans->c,
+                               "pointer to nonexistent indirect extent");
+               ret = -EIO;
+               goto err;
+       }
 
-       return c->fastest_tier &&
-               c->fastest_tier < c->tiers + pick->ca->mi.tier;
+       *offset_into_extent = iter->pos.offset - bkey_start_offset(k.k);
+       bkey_on_stack_reassemble(orig_k, trans->c, k);
+err:
+       bch2_trans_iter_put(trans, iter);
+       return ret;
 }
 
-void bch2_read_extent_iter(struct bch_fs *c, struct bch_read_bio *orig,
-                         struct bvec_iter iter, struct bkey_s_c k,
-                         struct extent_pick_ptr *pick, unsigned flags)
+int __bch2_read_extent(struct btree_trans *trans, struct bch_read_bio *orig,
+                      struct bvec_iter iter, struct bkey_s_c k,
+                      unsigned offset_into_extent,
+                      struct bch_io_failures *failed, unsigned flags)
 {
-       struct bch_read_bio *rbio;
-       struct cache_promote_op *promote_op = NULL;
-       unsigned skip = iter.bi_sector - bkey_start_offset(k.k);
-       bool bounce = false, split, read_full = false;
+       struct bch_fs *c = trans->c;
+       struct extent_ptr_decoded pick;
+       struct bch_read_bio *rbio = NULL;
+       struct bch_dev *ca;
+       struct promote_op *promote = NULL;
+       bool bounce = false, read_full = false, narrow_crcs = false;
+       struct bpos pos = bkey_start_pos(k.k);
+       int pick_ret;
+
+       if (k.k->type == KEY_TYPE_inline_data) {
+               struct bkey_s_c_inline_data d = bkey_s_c_to_inline_data(k);
+               unsigned bytes = min_t(unsigned, iter.bi_size,
+                                      bkey_val_bytes(d.k));
+
+               swap(iter.bi_size, bytes);
+               memcpy_to_bio(&orig->bio, iter, d.v->data);
+               swap(iter.bi_size, bytes);
+               bio_advance_iter(&orig->bio, &iter, bytes);
+               zero_fill_bio_iter(&orig->bio, iter);
+               goto out_read_done;
+       }
 
-       bch2_increment_clock(c, bio_sectors(&orig->bio), READ);
+       pick_ret = bch2_bkey_pick_read_device(c, k, failed, &pick);
 
-       EBUG_ON(bkey_start_offset(k.k) > iter.bi_sector ||
-               k.k->p.offset < bvec_iter_end_sector(iter));
+       /* hole or reservation - just zero fill: */
+       if (!pick_ret)
+               goto hole;
 
-       /* only promote if we're not reading from the fastest tier: */
+       if (pick_ret < 0) {
+               __bcache_io_error(c, "no device to read from");
+               goto err;
+       }
 
-       /*
-        * XXX: multiple promotes can race with each other, wastefully. Keep a
-        * list of outstanding promotes?
-        */
-       if (should_promote(c, pick, flags)) {
+       if (pick_ret > 0)
+               ca = bch_dev_bkey_exists(c, pick.ptr.dev);
+
+       if (flags & BCH_READ_NODECODE) {
                /*
-                * biovec needs to be big enough to hold decompressed data, if
-                * the bch2_write_extent() has to decompress/recompress it:
+                * can happen if we retry, and the extent we were going to read
+                * has been merged in the meantime:
                 */
-               unsigned sectors =
-                       max_t(unsigned, k.k->size,
-                             crc_uncompressed_size(NULL, &pick->crc));
-               unsigned pages = DIV_ROUND_UP(sectors, PAGE_SECTORS);
-
-               promote_op = kmalloc(sizeof(*promote_op) +
-                               sizeof(struct bio_vec) * pages, GFP_NOIO);
-               if (promote_op) {
-                       struct bio *promote_bio = &promote_op->write.wbio.bio;
-
-                       bio_init(promote_bio);
-                       promote_bio->bi_max_vecs = pages;
-                       promote_bio->bi_io_vec  = promote_bio->bi_inline_vecs;
-                       bounce = true;
-                       /* could also set read_full */
-               }
+               if (pick.crc.compressed_size > orig->bio.bi_vcnt * PAGE_SECTORS)
+                       goto hole;
+
+               iter.bi_size    = pick.crc.compressed_size << 9;
+               goto get_bio;
        }
 
-       /*
-        * note: if compression_type and crc_type both == none, then
-        * compressed/uncompressed size is zero
-        */
-       if (pick->crc.compression_type != BCH_COMPRESSION_NONE ||
-           (pick->crc.csum_type != BCH_CSUM_NONE &&
-            (bvec_iter_sectors(iter) != crc_uncompressed_size(NULL, &pick->crc) ||
-             (bch2_csum_type_is_encryption(pick->crc.csum_type) &&
+       if (!(flags & BCH_READ_LAST_FRAGMENT) ||
+           bio_flagged(&orig->bio, BIO_CHAIN))
+               flags |= BCH_READ_MUST_CLONE;
+
+       narrow_crcs = !(flags & BCH_READ_IN_RETRY) &&
+               bch2_can_narrow_extent_crcs(k, pick.crc);
+
+       if (narrow_crcs && (flags & BCH_READ_USER_MAPPED))
+               flags |= BCH_READ_MUST_BOUNCE;
+
+       EBUG_ON(offset_into_extent + bvec_iter_sectors(iter) > k.k->size);
+
+       if (crc_is_compressed(pick.crc) ||
+           (pick.crc.csum_type != BCH_CSUM_NONE &&
+            (bvec_iter_sectors(iter) != pick.crc.uncompressed_size ||
+             (bch2_csum_type_is_encryption(pick.crc.csum_type) &&
               (flags & BCH_READ_USER_MAPPED)) ||
-             (flags & BCH_READ_FORCE_BOUNCE)))) {
+             (flags & BCH_READ_MUST_BOUNCE)))) {
                read_full = true;
                bounce = true;
        }
 
-       if (bounce) {
-               unsigned sectors = read_full
-                       ? (crc_compressed_size(NULL, &pick->crc) ?: k.k->size)
-                       : bvec_iter_sectors(iter);
-
-               rbio = container_of(bio_alloc_bioset(GFP_NOIO,
-                                       DIV_ROUND_UP(sectors, PAGE_SECTORS),
-                                       &c->bio_read_split),
-                                   struct bch_read_bio, bio);
+       if (orig->opts.promote_target)
+               promote = promote_alloc(c, iter, k, &pick, orig->opts, flags,
+                                       &rbio, &bounce, &read_full);
+
+       if (!read_full) {
+               EBUG_ON(crc_is_compressed(pick.crc));
+               EBUG_ON(pick.crc.csum_type &&
+                       (bvec_iter_sectors(iter) != pick.crc.uncompressed_size ||
+                        bvec_iter_sectors(iter) != pick.crc.live_size ||
+                        pick.crc.offset ||
+                        offset_into_extent));
+
+               pos.offset += offset_into_extent;
+               pick.ptr.offset += pick.crc.offset +
+                       offset_into_extent;
+               offset_into_extent              = 0;
+               pick.crc.compressed_size        = bvec_iter_sectors(iter);
+               pick.crc.uncompressed_size      = bvec_iter_sectors(iter);
+               pick.crc.offset                 = 0;
+               pick.crc.live_size              = bvec_iter_sectors(iter);
+               offset_into_extent              = 0;
+       }
+get_bio:
+       if (rbio) {
+               /*
+                * promote already allocated bounce rbio:
+                * promote needs to allocate a bio big enough for uncompressing
+                * data in the write path, but we're not going to use it all
+                * here:
+                */
+               EBUG_ON(rbio->bio.bi_iter.bi_size <
+                      pick.crc.compressed_size << 9);
+               rbio->bio.bi_iter.bi_size =
+                       pick.crc.compressed_size << 9;
+       } else if (bounce) {
+               unsigned sectors = pick.crc.compressed_size;
+
+               rbio = rbio_init(bio_alloc_bioset(GFP_NOIO,
+                                                 DIV_ROUND_UP(sectors, PAGE_SECTORS),
+                                                 &c->bio_read_split),
+                                orig->opts);
 
                bch2_bio_alloc_pages_pool(c, &rbio->bio, sectors << 9);
-               split = true;
-       } else if (!(flags & BCH_READ_MAY_REUSE_BIO) ||
-                  !(flags & BCH_READ_IS_LAST)) {
+               rbio->bounce    = true;
+               rbio->split     = true;
+       } else if (flags & BCH_READ_MUST_CLONE) {
                /*
                 * Have to clone if there were any splits, due to error
                 * reporting issues (if a split errored, and retrying didn't
@@ -1156,225 +2151,240 @@ void bch2_read_extent_iter(struct bch_fs *c, struct bch_read_bio *orig,
                 * from the whole bio, in which case we don't want to retry and
                 * lose the error)
                 */
-               rbio = container_of(bio_clone_fast(&orig->bio,
-                                       GFP_NOIO, &c->bio_read_split),
-                                   struct bch_read_bio, bio);
+               rbio = rbio_init(bio_clone_fast(&orig->bio, GFP_NOIO,
+                                               &c->bio_read_split),
+                                orig->opts);
                rbio->bio.bi_iter = iter;
-               split = true;
+               rbio->split     = true;
        } else {
                rbio = orig;
                rbio->bio.bi_iter = iter;
-               split = false;
-               BUG_ON(bio_flagged(&rbio->bio, BIO_CHAIN));
+               EBUG_ON(bio_flagged(&rbio->bio, BIO_CHAIN));
        }
 
-       if (!(flags & BCH_READ_IS_LAST))
-               __bio_inc_remaining(&orig->bio);
+       EBUG_ON(bio_sectors(&rbio->bio) != pick.crc.compressed_size);
 
-       if (split)
+       rbio->c                 = c;
+       rbio->submit_time       = local_clock();
+       if (rbio->split)
                rbio->parent    = orig;
        else
-               rbio->orig_bi_end_io = orig->bio.bi_end_io;
-       rbio->parent_iter       = iter;
-
+               rbio->end_io    = orig->bio.bi_end_io;
+       rbio->bvec_iter         = iter;
+       rbio->offset_into_extent= offset_into_extent;
        rbio->flags             = flags;
-       rbio->bounce            = bounce;
-       rbio->split             = split;
-       rbio->c                 = c;
-       rbio->ca                = pick->ca;
-       rbio->ptr               = pick->ptr;
-       rbio->crc               = pick->crc;
-       /*
-        * crc.compressed_size will be 0 if there wasn't any checksum
-        * information, also we need to stash the original size of the bio if we
-        * bounced (which isn't necessarily the original key size, if we bounced
-        * only for promoting)
-        */
-       rbio->crc._compressed_size = bio_sectors(&rbio->bio) - 1;
+       rbio->have_ioref        = pick_ret > 0 && bch2_dev_get_ioref(ca, READ);
+       rbio->narrow_crcs       = narrow_crcs;
+       rbio->hole              = 0;
+       rbio->retry             = 0;
+       rbio->context           = 0;
+       /* XXX: only initialize this if needed */
+       rbio->devs_have         = bch2_bkey_devs(k);
+       rbio->pick              = pick;
+       rbio->pos               = pos;
        rbio->version           = k.k->version;
-       rbio->promote           = promote_op;
-       rbio->inode             = k.k->p.inode;
-       INIT_WORK(&rbio->work, __bch2_read_endio);
+       rbio->promote           = promote;
+       INIT_WORK(&rbio->work, NULL);
 
-       rbio->bio.bi_bdev       = pick->ca->disk_sb.bdev;
        rbio->bio.bi_opf        = orig->bio.bi_opf;
-       rbio->bio.bi_iter.bi_sector = pick->ptr.offset;
+       rbio->bio.bi_iter.bi_sector = pick.ptr.offset;
        rbio->bio.bi_end_io     = bch2_read_endio;
 
-       if (promote_op) {
-               struct bio *promote_bio = &promote_op->write.wbio.bio;
-
-               promote_bio->bi_iter = rbio->bio.bi_iter;
-               memcpy(promote_bio->bi_io_vec, rbio->bio.bi_io_vec,
-                      sizeof(struct bio_vec) * rbio->bio.bi_vcnt);
-
-               bch2_migrate_write_init(c, &promote_op->write,
-                                      &c->promote_write_point,
-                                      k, NULL,
-                                      BCH_WRITE_ALLOC_NOWAIT|
-                                      BCH_WRITE_CACHED);
-               promote_op->write.promote = true;
-
-               if (rbio->crc.compression_type) {
-                       promote_op->write.op.flags |= BCH_WRITE_DATA_COMPRESSED;
-                       promote_op->write.op.crc = rbio->crc;
-                       promote_op->write.op.size = k.k->size;
-               } else if (read_full) {
-                       /*
-                        * Adjust bio to correspond to _live_ portion of @k -
-                        * which might be less than what we're actually reading:
-                        */
-                       bio_advance(promote_bio, rbio->crc.offset << 9);
-                       BUG_ON(bio_sectors(promote_bio) < k.k->size);
-                       promote_bio->bi_iter.bi_size = k.k->size << 9;
-               } else {
-                       /*
-                        * Set insert pos to correspond to what we're actually
-                        * reading:
-                        */
-                       promote_op->write.op.pos.offset = iter.bi_sector;
+       if (rbio->bounce)
+               trace_read_bounce(&rbio->bio);
+
+       bch2_increment_clock(c, bio_sectors(&rbio->bio), READ);
+
+       if (pick.ptr.cached)
+               bch2_bucket_io_time_reset(trans, pick.ptr.dev,
+                       PTR_BUCKET_NR(ca, &pick.ptr), READ);
+
+       if (!(flags & (BCH_READ_IN_RETRY|BCH_READ_LAST_FRAGMENT))) {
+               bio_inc_remaining(&orig->bio);
+               trace_read_split(&orig->bio);
+       }
+
+       if (!rbio->pick.idx) {
+               if (!rbio->have_ioref) {
+                       __bcache_io_error(c, "no device to read from");
+                       bch2_rbio_error(rbio, READ_RETRY_AVOID, BLK_STS_IOERR);
+                       goto out;
                }
 
-               promote_bio->bi_iter.bi_sector =
-                       promote_op->write.op.pos.offset;
+               this_cpu_add(ca->io_done->sectors[READ][BCH_DATA_user],
+                            bio_sectors(&rbio->bio));
+               bio_set_dev(&rbio->bio, ca->disk_sb.bdev);
+
+               if (likely(!(flags & BCH_READ_IN_RETRY)))
+                       submit_bio(&rbio->bio);
+               else
+                       submit_bio_wait(&rbio->bio);
+       } else {
+               /* Attempting reconstruct read: */
+               if (bch2_ec_read_extent(c, rbio)) {
+                       bch2_rbio_error(rbio, READ_RETRY_AVOID, BLK_STS_IOERR);
+                       goto out;
+               }
+
+               if (likely(!(flags & BCH_READ_IN_RETRY)))
+                       bio_endio(&rbio->bio);
        }
+out:
+       if (likely(!(flags & BCH_READ_IN_RETRY))) {
+               return 0;
+       } else {
+               int ret;
 
-       /* _after_ promete stuff has looked at rbio->crc.offset */
-       if (read_full)
-               rbio->crc.offset += skip;
-       else
-               rbio->bio.bi_iter.bi_sector += skip;
+               rbio->context = RBIO_CONTEXT_UNBOUND;
+               bch2_read_endio(&rbio->bio);
 
-       rbio->submit_time_us = local_clock_us();
+               ret = rbio->retry;
+               rbio = bch2_rbio_free(rbio);
 
-       if (bounce)
-               trace_read_bounce(&rbio->bio);
+               if (ret == READ_RETRY_AVOID) {
+                       bch2_mark_io_failure(failed, &pick);
+                       ret = READ_RETRY;
+               }
+
+               return ret;
+       }
 
-       if (!(flags & BCH_READ_IS_LAST))
-               trace_read_split(&rbio->bio);
+err:
+       if (flags & BCH_READ_IN_RETRY)
+               return READ_ERR;
+
+       orig->bio.bi_status = BLK_STS_IOERR;
+       goto out_read_done;
 
-       generic_make_request(&rbio->bio);
+hole:
+       /*
+        * won't normally happen in the BCH_READ_NODECODE
+        * (bch2_move_extent()) path, but if we retry and the extent we wanted
+        * to read no longer exists we have to signal that:
+        */
+       if (flags & BCH_READ_NODECODE)
+               orig->hole = true;
+
+       zero_fill_bio_iter(&orig->bio, iter);
+out_read_done:
+       if (flags & BCH_READ_LAST_FRAGMENT)
+               bch2_rbio_done(orig);
+       return 0;
 }
 
-static void bch2_read_iter(struct bch_fs *c, struct bch_read_bio *rbio,
-                         struct bvec_iter bvec_iter, u64 inode,
-                         unsigned flags)
+void bch2_read(struct bch_fs *c, struct bch_read_bio *rbio, u64 inode)
 {
-       struct bio *bio = &rbio->bio;
-       struct btree_iter iter;
+       struct btree_trans trans;
+       struct btree_iter *iter;
+       struct bkey_on_stack sk;
        struct bkey_s_c k;
+       unsigned flags = BCH_READ_RETRY_IF_STALE|
+               BCH_READ_MAY_PROMOTE|
+               BCH_READ_USER_MAPPED;
        int ret;
 
-       for_each_btree_key_with_holes(&iter, c, BTREE_ID_EXTENTS,
-                                     POS(inode, bvec_iter.bi_sector), k) {
-               BKEY_PADDED(k) tmp;
-               struct extent_pick_ptr pick;
-               unsigned bytes, sectors;
-               bool is_last;
+       BUG_ON(rbio->_state);
+       BUG_ON(flags & BCH_READ_NODECODE);
+       BUG_ON(flags & BCH_READ_IN_RETRY);
 
-               /*
-                * Unlock the iterator while the btree node's lock is still in
-                * cache, before doing the IO:
-                */
-               bkey_reassemble(&tmp.k, k);
-               k = bkey_i_to_s_c(&tmp.k);
-               bch2_btree_iter_unlock(&iter);
-
-               bch2_extent_pick_ptr(c, k, &pick);
-               if (IS_ERR(pick.ca)) {
-                       bcache_io_error(c, bio, "no device to read from");
-                       bio_endio(bio);
-                       return;
-               }
+       rbio->c = c;
+       rbio->start_time = local_clock();
 
-               sectors = min_t(u64, k.k->p.offset,
-                               bvec_iter_end_sector(bvec_iter)) -
-                       bvec_iter.bi_sector;
-               bytes = sectors << 9;
-               is_last = bytes == bvec_iter.bi_size;
-               swap(bvec_iter.bi_size, bytes);
+       bkey_on_stack_init(&sk);
+       bch2_trans_init(&trans, c, 0, 0);
+retry:
+       bch2_trans_begin(&trans);
 
-               if (is_last)
-                       flags |= BCH_READ_IS_LAST;
+       iter = bch2_trans_get_iter(&trans, BTREE_ID_EXTENTS,
+                                  POS(inode, rbio->bio.bi_iter.bi_sector),
+                                  BTREE_ITER_SLOTS);
+       while (1) {
+               unsigned bytes, sectors, offset_into_extent;
 
-               if (pick.ca) {
-                       PTR_BUCKET(pick.ca, &pick.ptr)->read_prio =
-                               c->prio_clock[READ].hand;
+               bch2_btree_iter_set_pos(iter,
+                               POS(inode, rbio->bio.bi_iter.bi_sector));
 
-                       bch2_read_extent_iter(c, rbio, bvec_iter,
-                                            k, &pick, flags);
+               k = bch2_btree_iter_peek_slot(iter);
+               ret = bkey_err(k);
+               if (ret)
+                       goto err;
 
-                       flags &= ~BCH_READ_MAY_REUSE_BIO;
-               } else {
-                       zero_fill_bio_iter(bio, bvec_iter);
+               offset_into_extent = iter->pos.offset -
+                       bkey_start_offset(k.k);
+               sectors = k.k->size - offset_into_extent;
 
-                       if (is_last)
-                               bio_endio(bio);
-               }
+               bkey_on_stack_reassemble(&sk, c, k);
+               k = bkey_i_to_s_c(sk.k);
 
-               if (is_last)
-                       return;
+               ret = bch2_read_indirect_extent(&trans,
+                                       &offset_into_extent, &sk);
+               if (ret)
+                       goto err;
 
-               swap(bvec_iter.bi_size, bytes);
-               bio_advance_iter(bio, &bvec_iter, bytes);
-       }
+               /*
+                * With indirect extents, the amount of data to read is the min
+                * of the original extent and the indirect extent:
+                */
+               sectors = min(sectors, k.k->size - offset_into_extent);
 
-       /*
-        * If we get here, it better have been because there was an error
-        * reading a btree node
-        */
-       ret = bch2_btree_iter_unlock(&iter);
-       BUG_ON(!ret);
-       bcache_io_error(c, bio, "btree IO error %i", ret);
-       bio_endio(bio);
-}
+               /*
+                * Unlock the iterator while the btree node's lock is still in
+                * cache, before doing the IO:
+                */
+               bch2_trans_unlock(&trans);
 
-void bch2_read(struct bch_fs *c, struct bch_read_bio *bio, u64 inode)
-{
-       bch2_read_iter(c, bio, bio->bio.bi_iter, inode,
-                     BCH_READ_RETRY_IF_STALE|
-                     BCH_READ_PROMOTE|
-                     BCH_READ_MAY_REUSE_BIO|
-                     BCH_READ_USER_MAPPED);
-}
+               bytes = min(sectors, bio_sectors(&rbio->bio)) << 9;
+               swap(rbio->bio.bi_iter.bi_size, bytes);
 
-/**
- * bch_read_retry - re-submit a bio originally from bch2_read()
- */
-static void bch2_read_retry(struct bch_fs *c, struct bch_read_bio *rbio)
-{
-       struct bch_read_bio *parent = bch2_rbio_parent(rbio);
-       struct bvec_iter iter = rbio->parent_iter;
-       unsigned flags = rbio->flags;
-       u64 inode = rbio->inode;
+               if (rbio->bio.bi_iter.bi_size == bytes)
+                       flags |= BCH_READ_LAST_FRAGMENT;
 
-       trace_read_retry(&rbio->bio);
+               bch2_read_extent(&trans, rbio, k, offset_into_extent, flags);
 
-       if (rbio->split)
-               bch2_rbio_free(rbio);
-       else
-               rbio->bio.bi_end_io = rbio->orig_bi_end_io;
+               if (flags & BCH_READ_LAST_FRAGMENT)
+                       break;
 
-       bch2_read_iter(c, parent, iter, inode, flags);
+               swap(rbio->bio.bi_iter.bi_size, bytes);
+               bio_advance(&rbio->bio, bytes);
+       }
+out:
+       bch2_trans_exit(&trans);
+       bkey_on_stack_exit(&sk, c);
+       return;
+err:
+       if (ret == -EINTR)
+               goto retry;
+
+       bcache_io_error(c, &rbio->bio, "btree IO error: %i", ret);
+       bch2_rbio_done(rbio);
+       goto out;
 }
 
-void bch2_read_retry_work(struct work_struct *work)
+void bch2_fs_io_exit(struct bch_fs *c)
 {
-       struct bch_fs *c = container_of(work, struct bch_fs,
-                                          read_retry_work);
-       struct bch_read_bio *rbio;
-       struct bio *bio;
-       unsigned long flags;
-
-       while (1) {
-               spin_lock_irqsave(&c->read_retry_lock, flags);
-               bio = bio_list_pop(&c->read_retry_list);
-               spin_unlock_irqrestore(&c->read_retry_lock, flags);
-
-               if (!bio)
-                       break;
+       if (c->promote_table.tbl)
+               rhashtable_destroy(&c->promote_table);
+       mempool_exit(&c->bio_bounce_pages);
+       bioset_exit(&c->bio_write);
+       bioset_exit(&c->bio_read_split);
+       bioset_exit(&c->bio_read);
+}
 
-               rbio = container_of(bio, struct bch_read_bio, bio);
-               bch2_read_retry(c, rbio);
-       }
+int bch2_fs_io_init(struct bch_fs *c)
+{
+       if (bioset_init(&c->bio_read, 1, offsetof(struct bch_read_bio, bio),
+                       BIOSET_NEED_BVECS) ||
+           bioset_init(&c->bio_read_split, 1, offsetof(struct bch_read_bio, bio),
+                       BIOSET_NEED_BVECS) ||
+           bioset_init(&c->bio_write, 1, offsetof(struct bch_write_bio, bio),
+                       BIOSET_NEED_BVECS) ||
+           mempool_init_page_pool(&c->bio_bounce_pages,
+                                  max_t(unsigned,
+                                        c->opts.btree_node_size,
+                                        c->sb.encoded_extent_max) /
+                                  PAGE_SECTORS, 0) ||
+           rhashtable_init(&c->promote_table, &bch_promote_params))
+               return -ENOMEM;
+
+       return 0;
 }