]> git.sesse.net Git - bcachefs-tools-debian/blobdiff - libbcachefs/io.c
Update bcachefs sources to 95ff72a6c1 fixup! mm: Centralize & improve oom reporting...
[bcachefs-tools-debian] / libbcachefs / io.c
index 092ece2c8bdee20594ad57fc34148acf4b7a87f7..50fa572344a97da07db4617f1238417901bd7ac8 100644 (file)
@@ -27,6 +27,7 @@
 #include "keylist.h"
 #include "move.h"
 #include "rebalance.h"
+#include "subvolume.h"
 #include "super.h"
 #include "super-io.h"
 
@@ -186,26 +187,24 @@ void bch2_bio_alloc_pages_pool(struct bch_fs *c, struct bio *bio,
 int bch2_sum_sector_overwrites(struct btree_trans *trans,
                               struct btree_iter *extent_iter,
                               struct bkey_i *new,
-                              bool *maybe_extending,
                               bool *usage_increasing,
                               s64 *i_sectors_delta,
                               s64 *disk_sectors_delta)
 {
        struct bch_fs *c = trans->c;
-       struct btree_iter *iter;
+       struct btree_iter iter;
        struct bkey_s_c old;
        unsigned new_replicas = bch2_bkey_replicas(c, bkey_i_to_s_c(new));
        bool new_compressed = bch2_bkey_sectors_compressed(bkey_i_to_s_c(new));
        int ret = 0;
 
-       *maybe_extending        = true;
        *usage_increasing       = false;
        *i_sectors_delta        = 0;
        *disk_sectors_delta     = 0;
 
-       iter = bch2_trans_copy_iter(trans, extent_iter);
+       bch2_trans_copy_iter(&iter, extent_iter);
 
-       for_each_btree_key_continue(iter, BTREE_ITER_SLOTS, old, ret) {
+       for_each_btree_key_continue_norestart(iter, BTREE_ITER_SLOTS, old, ret) {
                s64 sectors = min(new->k.p.offset, old.k->p.offset) -
                        max(bkey_start_offset(&new->k),
                            bkey_start_offset(old.k));
@@ -220,38 +219,21 @@ int bch2_sum_sector_overwrites(struct btree_trans *trans,
                        : 0;
 
                if (!*usage_increasing &&
-                   (new_replicas > bch2_bkey_replicas(c, old) ||
+                   (new->k.p.snapshot != old.k->p.snapshot ||
+                    new_replicas > bch2_bkey_replicas(c, old) ||
                     (!new_compressed && bch2_bkey_sectors_compressed(old))))
                        *usage_increasing = true;
 
-               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;
-
+               if (bkey_cmp(old.k->p, new->k.p) >= 0)
                        break;
-               }
        }
 
-       bch2_trans_iter_put(trans, iter);
+       bch2_trans_iter_exit(trans, &iter);
        return ret;
 }
 
 int bch2_extent_update(struct btree_trans *trans,
+                      subvol_inum inum,
                       struct btree_iter *iter,
                       struct bkey_i *k,
                       struct disk_reservation *disk_res,
@@ -260,214 +242,208 @@ int bch2_extent_update(struct btree_trans *trans,
                       s64 *i_sectors_delta_total,
                       bool check_enospc)
 {
-       /* this must live until after bch2_trans_commit(): */
-       struct bkey_inode_buf inode_p;
-       bool extending = false, usage_increasing;
+       struct btree_iter inode_iter;
+       struct bch_inode_unpacked inode_u;
+       struct bpos next_pos;
+       bool usage_increasing;
        s64 i_sectors_delta = 0, disk_sectors_delta = 0;
        int ret;
 
-       ret = bch2_extent_trim_atomic(k, iter);
+       /*
+        * This traverses us the iterator without changing iter->path->pos to
+        * search_key() (which is pos + 1 for extents): we want there to be a
+        * path already traversed at iter->pos because
+        * bch2_trans_extent_update() will use it to attempt extent merging
+        */
+       ret = __bch2_btree_iter_traverse(iter);
+       if (ret)
+               return ret;
+
+       ret = bch2_extent_trim_atomic(trans, iter, k);
        if (ret)
                return ret;
 
+       new_i_size = min(k->k.p.offset << 9, new_i_size);
+       next_pos = k->k.p;
+
        ret = bch2_sum_sector_overwrites(trans, iter, k,
-                       &extending,
                        &usage_increasing,
                        &i_sectors_delta,
                        &disk_sectors_delta);
        if (ret)
                return ret;
 
-       if (!usage_increasing)
-               check_enospc = false;
-
        if (disk_res &&
            disk_sectors_delta > (s64) disk_res->sectors) {
                ret = bch2_disk_reservation_add(trans->c, disk_res,
                                        disk_sectors_delta - disk_res->sectors,
-                                       !check_enospc
+                                       !check_enospc || !usage_increasing
                                        ? BCH_DISK_RESERVATION_NOFAIL : 0);
                if (ret)
                        return ret;
        }
 
-       new_i_size = extending
-               ? min(k->k.p.offset << 9, new_i_size)
-               : 0;
-
-       if (i_sectors_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);
-               ret = PTR_ERR_OR_ZERO(inode_iter);
-               if (ret)
-                       return ret;
-
-               /*
-                * 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 += i_sectors_delta;
-
-               if (i_sectors_delta || new_i_size) {
-                       bch2_inode_pack(trans->c, &inode_p, &inode_u);
-
-                       inode_p.inode.k.p.snapshot = iter->snapshot;
-
-                       ret = bch2_trans_update(trans, inode_iter,
-                                         &inode_p.inode.k_i, 0);
-               }
+       ret = bch2_inode_peek(trans, &inode_iter, &inode_u, inum,
+                             BTREE_ITER_INTENT);
+       if (ret)
+               return ret;
 
-               bch2_trans_iter_put(trans, inode_iter);
+       if (!(inode_u.bi_flags & BCH_INODE_I_SIZE_DIRTY) &&
+           new_i_size > inode_u.bi_size)
+               inode_u.bi_size = new_i_size;
 
-               if (ret)
-                       return ret;
-       }
+       inode_u.bi_sectors += i_sectors_delta;
 
        ret =   bch2_trans_update(trans, iter, k, 0) ?:
+               bch2_inode_write(trans, &inode_iter, &inode_u) ?:
                bch2_trans_commit(trans, disk_res, journal_seq,
                                BTREE_INSERT_NOCHECK_RW|
                                BTREE_INSERT_NOFAIL);
-       BUG_ON(ret == -ENOSPC);
+       bch2_trans_iter_exit(trans, &inode_iter);
+
        if (ret)
                return ret;
 
        if (i_sectors_delta_total)
                *i_sectors_delta_total += i_sectors_delta;
+       bch2_btree_iter_set_pos(iter, next_pos);
+
        return 0;
 }
 
+/*
+ * Returns -EINTR if we had to drop locks:
+ */
 int bch2_fpunch_at(struct btree_trans *trans, struct btree_iter *iter,
-                  struct bpos end, u64 *journal_seq,
+                  subvol_inum inum, u64 end,
                   s64 *i_sectors_delta)
 {
        struct bch_fs *c        = trans->c;
        unsigned max_sectors    = KEY_SIZE_MAX & (~0 << c->block_bits);
+       struct bpos end_pos = POS(inum.inum, end);
        struct bkey_s_c k;
        int ret = 0, ret2 = 0;
+       u32 snapshot;
 
-       while ((k = bch2_btree_iter_peek(iter)).k &&
-              bkey_cmp(iter->pos, end) < 0) {
+       while (!ret || ret == -EINTR) {
                struct disk_reservation disk_res =
                        bch2_disk_reservation_init(c, 0);
                struct bkey_i delete;
 
+               if (ret)
+                       ret2 = ret;
+
                bch2_trans_begin(trans);
 
+               ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
+               if (ret)
+                       continue;
+
+               bch2_btree_iter_set_snapshot(iter, snapshot);
+
+               k = bch2_btree_iter_peek(iter);
+               if (bkey_cmp(iter->pos, end_pos) >= 0) {
+                       bch2_btree_iter_set_pos(iter, end_pos);
+                       break;
+               }
+
                ret = bkey_err(k);
                if (ret)
-                       goto btree_err;
+                       continue;
 
                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);
+               bch2_cut_back(end_pos, &delete);
 
-               ret = bch2_extent_update(trans, iter, &delete,
-                               &disk_res, journal_seq,
+               ret = bch2_extent_update(trans, inum, iter, &delete,
+                               &disk_res, NULL,
                                0, i_sectors_delta, false);
                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)
+int bch2_fpunch(struct bch_fs *c, subvol_inum inum, u64 start, u64 end,
+               s64 *i_sectors_delta)
 {
        struct btree_trans trans;
-       struct btree_iter *iter;
-       int ret = 0;
+       struct btree_iter iter;
+       int ret;
 
        bch2_trans_init(&trans, c, BTREE_ITER_MAX, 1024);
-       iter = bch2_trans_get_iter(&trans, BTREE_ID_extents,
-                                  POS(inum, start),
-                                  BTREE_ITER_INTENT);
+       bch2_trans_iter_init(&trans, &iter, BTREE_ID_extents,
+                            POS(inum.inum, start),
+                            BTREE_ITER_INTENT);
 
-       ret = bch2_fpunch_at(&trans, iter, POS(inum, end),
-                            journal_seq, i_sectors_delta);
+       ret = bch2_fpunch_at(&trans, &iter, inum, end, i_sectors_delta);
 
-       bch2_trans_iter_put(&trans, iter);
+       bch2_trans_iter_exit(&trans, &iter);
        bch2_trans_exit(&trans);
 
-       if (ret == -EINTR)
-               ret = 0;
-
-       return ret;
+       return ret == -EINTR ? 0 : ret;
 }
 
 int bch2_write_index_default(struct bch_write_op *op)
 {
        struct bch_fs *c = op->c;
        struct bkey_buf sk;
+       struct open_bucket *ec_ob = ec_open_bucket(c, &op->open_buckets);
        struct keylist *keys = &op->insert_keys;
        struct bkey_i *k = bch2_keylist_front(keys);
        struct btree_trans trans;
-       struct btree_iter *iter;
+       struct btree_iter iter;
+       subvol_inum inum = {
+               .subvol = op->subvol,
+               .inum   = k->k.p.inode,
+       };
        int ret;
 
+       BUG_ON(!inum.subvol);
+
        bch2_bkey_buf_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);
+               bch2_bkey_buf_copy(&sk, c, k);
 
-               k->k.p.snapshot = iter->snapshot;
+               ret = bch2_subvolume_get_snapshot(&trans, inum.subvol,
+                                                 &sk.k->k.p.snapshot);
+               if (ret == -EINTR)
+                       continue;
+               if (ret)
+                       break;
 
-               bch2_bkey_buf_realloc(&sk, c, k->k.u64s);
-               bkey_copy(sk.k, k);
-               bch2_cut_front(iter->pos, sk.k);
+               bch2_trans_iter_init(&trans, &iter, BTREE_ID_extents,
+                                    bkey_start_pos(&sk.k->k),
+                                    BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
 
-               ret = bch2_extent_update(&trans, iter, sk.k,
+               ret = bch2_extent_update(&trans, inum, &iter, sk.k,
                                         &op->res, op_journal_seq(op),
                                         op->new_i_size, &op->i_sectors_delta,
                                         op->flags & BCH_WRITE_CHECK_ENOSPC);
+               bch2_trans_iter_exit(&trans, &iter);
+
                if (ret == -EINTR)
                        continue;
                if (ret)
                        break;
 
-               if (bkey_cmp(iter->pos, k->k.p) >= 0)
-                       bch2_keylist_pop_front(keys);
+               if (ec_ob)
+                       bch2_ob_add_backpointer(c, ec_ob, &sk.k->k);
+
+               if (bkey_cmp(iter.pos, k->k.p) >= 0)
+                       bch2_keylist_pop_front(&op->insert_keys);
+               else
+                       bch2_cut_front(iter.pos, k);
        } while (!bch2_keylist_empty(keys));
 
-       bch2_trans_iter_put(&trans, iter);
        bch2_trans_exit(&trans);
        bch2_bkey_buf_exit(&sk, c);
 
@@ -494,8 +470,8 @@ void bch2_submit_wbio_replicas(struct bch_write_bio *wbio, struct bch_fs *c,
                ca = bch_dev_bkey_exists(c, ptr->dev);
 
                if (to_entry(ptr + 1) < ptrs.end) {
-                       n = to_wbio(bio_clone_fast(&wbio->bio, GFP_NOIO,
-                                                  &ca->replica_set));
+                       n = to_wbio(bio_alloc_clone(NULL, &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;
@@ -689,11 +665,7 @@ static void init_append_extent(struct bch_write_op *op,
 {
        struct bch_fs *c = op->c;
        struct bkey_i_extent *e;
-       struct open_bucket *ob;
-       unsigned i;
 
-       BUG_ON(crc.compressed_size > wp->sectors_free);
-       wp->sectors_free -= crc.compressed_size;
        op->pos.offset += crc.uncompressed_size;
 
        e = bkey_extent_init(op->insert_keys.top);
@@ -706,22 +678,8 @@ static void init_append_extent(struct bch_write_op *op,
            crc.nonce)
                bch2_extent_crc_append(&e->k_i, crc);
 
-       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));
-
-               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;
-
-               e->k.u64s++;
-
-               BUG_ON(crc.compressed_size > ob->sectors_free);
-               ob->sectors_free -= crc.compressed_size;
-       }
+       bch2_alloc_sectors_append_ptrs(c, wp, &e->k_i, crc.compressed_size,
+                                      op->flags & BCH_WRITE_CACHED);
 
        bch2_keylist_push(&op->insert_keys);
 }
@@ -741,7 +699,10 @@ static struct bio *bch2_write_bio_alloc(struct bch_fs *c,
                                       ? ((unsigned long) buf & (PAGE_SIZE - 1))
                                       : 0), PAGE_SIZE);
 
-       bio = bio_alloc_bioset(GFP_NOIO, pages, &c->bio_write);
+       pages = min(pages, BIO_MAX_VECS);
+
+       bio = bio_alloc_bioset(NULL, pages, 0,
+                              GFP_NOIO, &c->bio_write);
        wbio                    = wbio_init(bio);
        wbio->put_bio           = true;
        /* copy WRITE_SYNC flag */
@@ -760,7 +721,7 @@ static struct bio *bch2_write_bio_alloc(struct bch_fs *c,
         */
        bch2_bio_alloc_pages_pool(c, bio,
                                  min_t(unsigned, output_available,
-                                       c->sb.encoded_extent_max << 9));
+                                       c->opts.encoded_extent_max));
 
        if (bio->bi_iter.bi_size < output_available)
                *page_alloc_failed =
@@ -804,6 +765,7 @@ static int bch2_write_decrypt(struct bch_write_op *op)
        struct bch_fs *c = op->c;
        struct nonce nonce = extent_nonce(op->version, op->crc);
        struct bch_csum csum;
+       int ret;
 
        if (!bch2_csum_type_is_encryption(op->crc.csum_type))
                return 0;
@@ -818,10 +780,10 @@ static int bch2_write_decrypt(struct bch_write_op *op)
        if (bch2_crc_cmp(op->crc.csum, csum))
                return -EIO;
 
-       bch2_encrypt_bio(c, op->crc.csum_type, nonce, &op->wbio.bio);
+       ret = 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;
+       return ret;
 }
 
 static enum prep_encoded_ret {
@@ -906,7 +868,6 @@ static int bch2_write_extent(struct bch_write_op *op, struct write_point *wp,
        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;
@@ -923,7 +884,6 @@ static int bch2_write_extent(struct bch_write_op *op, struct write_point *wp,
                ret = -EIO;
                goto err;
        case PREP_ENCODED_CHECKSUM_ERR:
-               BUG();
                goto csum_err;
        case PREP_ENCODED_DO_WRITE:
                /* XXX look for bug here */
@@ -959,8 +919,8 @@ static int bch2_write_extent(struct bch_write_op *op, struct write_point *wp,
                size_t dst_len, src_len;
 
                if (page_alloc_failed &&
-                   bio_sectors(dst) < wp->sectors_free &&
-                   bio_sectors(dst) < c->sb.encoded_extent_max)
+                   dst->bi_iter.bi_size  < (wp->sectors_free << 9) &&
+                   dst->bi_iter.bi_size < c->opts.encoded_extent_max)
                        break;
 
                BUG_ON(op->compression_type &&
@@ -980,7 +940,7 @@ static int bch2_write_extent(struct bch_write_op *op, struct write_point *wp,
 
                        if (op->csum_type)
                                dst_len = min_t(unsigned, dst_len,
-                                               c->sb.encoded_extent_max << 9);
+                                               c->opts.encoded_extent_max);
 
                        if (bounce) {
                                swap(dst->bi_iter.bi_size, dst_len);
@@ -1038,8 +998,11 @@ static int bch2_write_extent(struct bch_write_op *op, struct write_point *wp,
                        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);
+                       ret = bch2_encrypt_bio(c, op->csum_type,
+                                              extent_nonce(version, crc), dst);
+                       if (ret)
+                               goto err;
+
                        crc.csum = bch2_checksum_bio(c, op->csum_type,
                                         extent_nonce(version, crc), dst);
                        crc.csum_type = op->csum_type;
@@ -1077,14 +1040,10 @@ static int bch2_write_extent(struct bch_write_op *op, struct write_point *wp,
 
        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?)");
+       bch_err(c, "error verifying existing checksum while rewriting existing data (memory corruption?)");
        ret = -EIO;
 err:
        if (to_wbio(dst)->bounce)
@@ -1100,7 +1059,7 @@ 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 write_point *wp;
-       struct bio *bio;
+       struct bio *bio = NULL;
        bool skip_put = true;
        unsigned nofs_flags;
        int ret;
@@ -1125,12 +1084,6 @@ again:
                                        BKEY_EXTENT_U64s_MAX))
                        goto flush_io;
 
-               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
@@ -1138,7 +1091,7 @@ again:
                 */
                wp = bch2_alloc_sectors_start(c,
                        op->target,
-                       op->opts.erasure_code,
+                       op->opts.erasure_code && !(op->flags & BCH_WRITE_CACHED),
                        op->write_point,
                        &op->devs_have,
                        op->nr_replicas,
@@ -1316,7 +1269,7 @@ void bch2_write(struct closure *cl)
        bch2_keylist_init(&op->insert_keys, op->inline_keys);
        wbio_init(bio)->put_bio = false;
 
-       if (bio_sectors(bio) & (c->opts.block_size - 1)) {
+       if (bio->bi_iter.bi_size & (c->opts.block_size - 1)) {
                bch_err_inum_ratelimited(c, op->pos.inode,
                                         "misaligned write");
                op->error = -EIO;
@@ -1324,11 +1277,12 @@ void bch2_write(struct closure *cl)
        }
 
        if (c->opts.nochanges ||
-           !percpu_ref_tryget(&c->writes)) {
+           !percpu_ref_tryget_live(&c->writes)) {
                op->error = -EROFS;
                goto err;
        }
 
+       this_cpu_add(c->counters[BCH_COUNTER_io_write], bio_sectors(bio));
        bch2_increment_clock(c, bio_sectors(bio), WRITE);
 
        data_len = min_t(u64, bio->bi_iter.bi_size,
@@ -1364,7 +1318,7 @@ struct promote_op {
        struct rhash_head       hash;
        struct bpos             pos;
 
-       struct migrate_write    write;
+       struct data_update      write;
        struct bio_vec          bi_inline_vecs[0]; /* must be last */
 };
 
@@ -1420,13 +1374,12 @@ static void promote_done(struct closure *cl)
        bch2_time_stats_update(&c->times[BCH_TIME_data_promote],
                               op->start_time);
 
-       bch2_bio_free_pages_pool(c, &op->write.op.wbio.bio);
+       bch2_data_update_exit(&op->write);
        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;
 
@@ -1440,10 +1393,8 @@ static void promote_start(struct promote_op *op, struct bch_read_bio *rbio)
               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->btree_update_wq, cl);
+       bch2_data_update_read_done(&op->write, rbio->pick.crc, cl);
        closure_return_with_destructor(cl, promote_done);
 }
 
@@ -1461,7 +1412,7 @@ static struct promote_op *__promote_alloc(struct bch_fs *c,
        unsigned pages = DIV_ROUND_UP(sectors, PAGE_SECTORS);
        int ret;
 
-       if (!percpu_ref_tryget(&c->writes))
+       if (!percpu_ref_tryget_live(&c->writes))
                return NULL;
 
        op = kzalloc(sizeof(*op) + sizeof(struct bio_vec) * pages, GFP_NOIO);
@@ -1482,7 +1433,7 @@ static struct promote_op *__promote_alloc(struct bch_fs *c,
                goto err;
 
        rbio_init(&(*rbio)->bio, opts);
-       bio_init(&(*rbio)->bio, (*rbio)->bio.bi_inline_vecs, pages);
+       bio_init(&(*rbio)->bio, NULL, (*rbio)->bio.bi_inline_vecs, pages, 0);
 
        if (bch2_bio_alloc_pages(&(*rbio)->bio, sectors << 9,
                                 GFP_NOIO))
@@ -1497,15 +1448,15 @@ static struct promote_op *__promote_alloc(struct bch_fs *c,
                goto err;
 
        bio = &op->write.op.wbio.bio;
-       bio_init(bio, bio->bi_inline_vecs, pages);
+       bio_init(bio, NULL, bio->bi_inline_vecs, pages, 0);
 
-       ret = bch2_migrate_write_init(c, &op->write,
+       ret = bch2_data_update_init(c, &op->write,
                        writepoint_hashed((unsigned long) current),
                        opts,
-                       DATA_PROMOTE,
-                       (struct data_opts) {
+                       (struct data_update_opts) {
                                .target         = opts.promote_target,
-                               .nr_replicas    = 1,
+                               .extra_replicas = 1,
+                               .write_flags    = BCH_WRITE_ALLOC_NOWAIT|BCH_WRITE_CACHED,
                        },
                        btree_id, k);
        BUG_ON(ret);
@@ -1628,12 +1579,12 @@ static void bch2_rbio_done(struct bch_read_bio *rbio)
 }
 
 static void bch2_read_retry_nodecode(struct bch_fs *c, struct bch_read_bio *rbio,
-                                    struct bvec_iter bvec_iter, u64 inode,
+                                    struct bvec_iter bvec_iter,
                                     struct bch_io_failures *failed,
                                     unsigned flags)
 {
        struct btree_trans trans;
-       struct btree_iter *iter;
+       struct btree_iter iter;
        struct bkey_buf sk;
        struct bkey_s_c k;
        int ret;
@@ -1644,12 +1595,12 @@ static void bch2_read_retry_nodecode(struct bch_fs *c, struct bch_read_bio *rbio
        bch2_bkey_buf_init(&sk);
        bch2_trans_init(&trans, c, 0, 0);
 
-       iter = bch2_trans_get_iter(&trans, rbio->data_btree,
-                                  rbio->read_pos, BTREE_ITER_SLOTS);
+       bch2_trans_iter_init(&trans, &iter, rbio->data_btree,
+                            rbio->read_pos, BTREE_ITER_SLOTS);
 retry:
        rbio->bio.bi_status = 0;
 
-       k = bch2_btree_iter_peek_slot(iter);
+       k = bch2_btree_iter_peek_slot(&iter);
        if (bkey_err(k))
                goto err;
 
@@ -1676,7 +1627,7 @@ retry:
                goto err;
 out:
        bch2_rbio_done(rbio);
-       bch2_trans_iter_put(&trans, iter);
+       bch2_trans_iter_exit(&trans, &iter);
        bch2_trans_exit(&trans);
        bch2_bkey_buf_exit(&sk, c);
        return;
@@ -1692,7 +1643,10 @@ static void bch2_rbio_retry(struct work_struct *work)
        struct bch_fs *c        = rbio->c;
        struct bvec_iter iter   = rbio->bvec_iter;
        unsigned flags          = rbio->flags;
-       u64 inode               = rbio->read_pos.inode;
+       subvol_inum inum = {
+               .subvol = rbio->subvol,
+               .inum   = rbio->read_pos.inode,
+       };
        struct bch_io_failures failed = { .nr = 0 };
 
        trace_read_retry(&rbio->bio);
@@ -1708,12 +1662,12 @@ static void bch2_rbio_retry(struct work_struct *work)
        flags &= ~BCH_READ_MAY_PROMOTE;
 
        if (flags & BCH_READ_NODECODE) {
-               bch2_read_retry_nodecode(c, rbio, iter, inode, &failed, flags);
+               bch2_read_retry_nodecode(c, rbio, iter, &failed, flags);
        } else {
                flags &= ~BCH_READ_LAST_FRAGMENT;
                flags |= BCH_READ_MUST_CLONE;
 
-               __bch2_read(c, rbio, iter, inode, &failed, flags);
+               __bch2_read(c, rbio, iter, inum, &failed, flags);
        }
 }
 
@@ -1742,7 +1696,7 @@ static int __bch2_rbio_narrow_crcs(struct btree_trans *trans,
        struct bch_fs *c = rbio->c;
        u64 data_offset = rbio->data_pos.offset - rbio->pick.crc.offset;
        struct bch_extent_crc_unpacked new_crc;
-       struct btree_iter *iter = NULL;
+       struct btree_iter iter;
        struct bkey_i *new;
        struct bkey_s_c k;
        int ret = 0;
@@ -1750,9 +1704,9 @@ static int __bch2_rbio_narrow_crcs(struct btree_trans *trans,
        if (crc_is_compressed(rbio->pick.crc))
                return 0;
 
-       iter = bch2_trans_get_iter(trans, rbio->data_btree, rbio->data_pos,
-                                  BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
-       k = bch2_btree_iter_peek_slot(iter);
+       bch2_trans_iter_init(trans, &iter, rbio->data_btree, rbio->data_pos,
+                            BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
+       k = bch2_btree_iter_peek_slot(&iter);
        if ((ret = bkey_err(k)))
                goto out;
 
@@ -1787,9 +1741,10 @@ static int __bch2_rbio_narrow_crcs(struct btree_trans *trans,
        if (!bch2_bkey_narrow_crcs(new, new_crc))
                goto out;
 
-       ret = bch2_trans_update(trans, iter, new, 0);
+       ret = bch2_trans_update(trans, &iter, new,
+                               BTREE_UPDATE_INTERNAL_SNAPSHOT_NODE);
 out:
-       bch2_trans_iter_put(trans, iter);
+       bch2_trans_iter_exit(trans, &iter);
        return ret;
 }
 
@@ -1813,6 +1768,7 @@ static void __bch2_read_endio(struct work_struct *work)
        struct nonce nonce = extent_nonce(rbio->version, crc);
        unsigned nofs_flags;
        struct bch_csum csum;
+       int ret;
 
        nofs_flags = memalloc_nofs_save();
 
@@ -1847,7 +1803,10 @@ static void __bch2_read_endio(struct work_struct *work)
        crc.live_size   = bvec_iter_sectors(rbio->bvec_iter);
 
        if (crc_is_compressed(crc)) {
-               bch2_encrypt_bio(c, crc.csum_type, nonce, src);
+               ret = bch2_encrypt_bio(c, crc.csum_type, nonce, src);
+               if (ret)
+                       goto decrypt_err;
+
                if (bch2_bio_uncompress(c, src, dst, dst_iter, crc))
                        goto decompression_err;
        } else {
@@ -1858,7 +1817,9 @@ static void __bch2_read_endio(struct work_struct *work)
                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);
+               ret = bch2_encrypt_bio(c, crc.csum_type, nonce, src);
+               if (ret)
+                       goto decrypt_err;
 
                if (rbio->bounce) {
                        struct bvec_iter src_iter = src->bi_iter;
@@ -1871,7 +1832,10 @@ static void __bch2_read_endio(struct work_struct *work)
                 * Re encrypt data we decrypted, so it's consistent with
                 * rbio->crc:
                 */
-               bch2_encrypt_bio(c, crc.csum_type, nonce, src);
+               ret = bch2_encrypt_bio(c, crc.csum_type, nonce, src);
+               if (ret)
+                       goto decrypt_err;
+
                promote_start(rbio->promote, rbio);
                rbio->promote = NULL;
        }
@@ -1896,9 +1860,9 @@ csum_err:
        }
 
        bch2_dev_inum_io_error(ca, rbio->read_pos.inode, (u64) rbio->bvec_iter.bi_sector,
-               "data checksum error: expected %0llx:%0llx got %0llx:%0llx (type %u)",
+               "data checksum error: expected %0llx:%0llx got %0llx:%0llx (type %s)",
                rbio->pick.crc.csum.hi, rbio->pick.crc.csum.lo,
-               csum.hi, csum.lo, crc.csum_type);
+               csum.hi, csum.lo, bch2_csum_types[crc.csum_type]);
        bch2_rbio_error(rbio, READ_RETRY_AVOID, BLK_STS_IOERR);
        goto out;
 decompression_err:
@@ -1906,6 +1870,11 @@ decompression_err:
                                 "decompression error");
        bch2_rbio_error(rbio, READ_ERR, BLK_STS_IOERR);
        goto out;
+decrypt_err:
+       bch_err_inum_ratelimited(c, rbio->read_pos.inode,
+                                "decrypt error");
+       bch2_rbio_error(rbio, READ_ERR, BLK_STS_IOERR);
+       goto out;
 }
 
 static void bch2_read_endio(struct bio *bio)
@@ -1934,9 +1903,8 @@ static void bch2_read_endio(struct bio *bio)
                return;
        }
 
-       if (rbio->pick.ptr.cached &&
-           (((rbio->flags & BCH_READ_RETRY_IF_STALE) && race_fault()) ||
-            ptr_stale(ca, &rbio->pick.ptr))) {
+       if (((rbio->flags & BCH_READ_RETRY_IF_STALE) && race_fault()) ||
+           ptr_stale(ca, &rbio->pick.ptr)) {
                atomic_long_inc(&c->read_realloc_races);
 
                if (rbio->flags & BCH_READ_RETRY_IF_STALE)
@@ -1960,7 +1928,7 @@ int __bch2_read_indirect_extent(struct btree_trans *trans,
                                unsigned *offset_into_extent,
                                struct bkey_buf *orig_k)
 {
-       struct btree_iter *iter;
+       struct btree_iter iter;
        struct bkey_s_c k;
        u64 reflink_offset;
        int ret;
@@ -1968,10 +1936,10 @@ int __bch2_read_indirect_extent(struct btree_trans *trans,
        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);
-       k = bch2_btree_iter_peek_slot(iter);
+       bch2_trans_iter_init(trans, &iter, BTREE_ID_reflink,
+                            POS(0, reflink_offset),
+                            BTREE_ITER_SLOTS);
+       k = bch2_btree_iter_peek_slot(&iter);
        ret = bkey_err(k);
        if (ret)
                goto err;
@@ -1988,13 +1956,48 @@ int __bch2_read_indirect_extent(struct btree_trans *trans,
                goto err;
        }
 
-       *offset_into_extent = iter->pos.offset - bkey_start_offset(k.k);
+       *offset_into_extent = iter.pos.offset - bkey_start_offset(k.k);
        bch2_bkey_buf_reassemble(orig_k, trans->c, k);
 err:
-       bch2_trans_iter_put(trans, iter);
+       bch2_trans_iter_exit(trans, &iter);
        return ret;
 }
 
+static noinline void read_from_stale_dirty_pointer(struct btree_trans *trans,
+                                                  struct bkey_s_c k,
+                                                  struct bch_extent_ptr ptr)
+{
+       struct bch_fs *c = trans->c;
+       struct bch_dev *ca = bch_dev_bkey_exists(c, ptr.dev);
+       struct btree_iter iter;
+       struct printbuf buf = PRINTBUF;
+       int ret;
+
+       bch2_trans_iter_init(trans, &iter, BTREE_ID_alloc,
+                            PTR_BUCKET_POS(c, &ptr),
+                            BTREE_ITER_CACHED);
+
+       prt_printf(&buf, "Attempting to read from stale dirty pointer:");
+       printbuf_indent_add(&buf, 2);
+       prt_newline(&buf);
+
+       bch2_bkey_val_to_text(&buf, c, k);
+       prt_newline(&buf);
+
+       prt_printf(&buf, "memory gen: %u", *bucket_gen(ca, iter.pos.offset));
+
+       ret = lockrestart_do(trans, bkey_err(k = bch2_btree_iter_peek_slot(&iter)));
+       if (!ret) {
+               prt_newline(&buf);
+               bch2_bkey_val_to_text(&buf, c, k);
+       }
+
+       bch2_fs_inconsistent(c, "%s", buf.buf);
+
+       bch2_trans_iter_exit(trans, &iter);
+       printbuf_exit(&buf);
+}
+
 int __bch2_read_extent(struct btree_trans *trans, struct bch_read_bio *orig,
                       struct bvec_iter iter, struct bpos read_pos,
                       enum btree_id data_btree, struct bkey_s_c k,
@@ -2004,7 +2007,7 @@ int __bch2_read_extent(struct btree_trans *trans, struct bch_read_bio *orig,
        struct bch_fs *c = trans->c;
        struct extent_ptr_decoded pick;
        struct bch_read_bio *rbio = NULL;
-       struct bch_dev *ca;
+       struct bch_dev *ca = NULL;
        struct promote_op *promote = NULL;
        bool bounce = false, read_full = false, narrow_crcs = false;
        struct bpos data_pos = bkey_start_pos(k.k);
@@ -2021,7 +2024,7 @@ int __bch2_read_extent(struct btree_trans *trans, struct bch_read_bio *orig,
                zero_fill_bio_iter(&orig->bio, iter);
                goto out_read_done;
        }
-
+retry_pick:
        pick_ret = bch2_bkey_pick_read_device(c, k, failed, &pick);
 
        /* hole or reservation - just zero fill: */
@@ -2034,8 +2037,27 @@ int __bch2_read_extent(struct btree_trans *trans, struct bch_read_bio *orig,
                goto err;
        }
 
-       if (pick_ret > 0)
-               ca = bch_dev_bkey_exists(c, pick.ptr.dev);
+       ca = bch_dev_bkey_exists(c, pick.ptr.dev);
+
+       /*
+        * Stale dirty pointers are treated as IO errors, but @failed isn't
+        * allocated unless we're in the retry path - so if we're not in the
+        * retry path, don't check here, it'll be caught in bch2_read_endio()
+        * and we'll end up in the retry path:
+        */
+       if ((flags & BCH_READ_IN_RETRY) &&
+           !pick.ptr.cached &&
+           unlikely(ptr_stale(ca, &pick.ptr))) {
+               read_from_stale_dirty_pointer(trans, k, pick.ptr);
+               bch2_mark_io_failure(failed, &pick);
+               goto retry_pick;
+       }
+
+       /*
+        * Unlock the iterator while the btree node's lock is still in
+        * cache, before doing the IO:
+        */
+       bch2_trans_unlock(trans);
 
        if (flags & BCH_READ_NODECODE) {
                /*
@@ -2062,7 +2084,7 @@ int __bch2_read_extent(struct btree_trans *trans, struct bch_read_bio *orig,
        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 &&
+           (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)) ||
@@ -2108,8 +2130,10 @@ get_bio:
        } else if (bounce) {
                unsigned sectors = pick.crc.compressed_size;
 
-               rbio = rbio_init(bio_alloc_bioset(GFP_NOIO,
+               rbio = rbio_init(bio_alloc_bioset(NULL,
                                                  DIV_ROUND_UP(sectors, PAGE_SECTORS),
+                                                 0,
+                                                 GFP_NOIO,
                                                  &c->bio_read_split),
                                 orig->opts);
 
@@ -2125,8 +2149,8 @@ get_bio:
                 * from the whole bio, in which case we don't want to retry and
                 * lose the error)
                 */
-               rbio = rbio_init(bio_clone_fast(&orig->bio, GFP_NOIO,
-                                               &c->bio_read_split),
+               rbio = rbio_init(bio_alloc_clone(NULL, &orig->bio, GFP_NOIO,
+                                                &c->bio_read_split),
                                 orig->opts);
                rbio->bio.bi_iter = iter;
                rbio->split     = true;
@@ -2155,6 +2179,7 @@ get_bio:
        /* XXX: only initialize this if needed */
        rbio->devs_have         = bch2_bkey_devs(k);
        rbio->pick              = pick;
+       rbio->subvol            = orig->subvol;
        rbio->read_pos          = read_pos;
        rbio->data_btree        = data_btree;
        rbio->data_pos          = data_pos;
@@ -2169,6 +2194,7 @@ get_bio:
        if (rbio->bounce)
                trace_read_bounce(&rbio->bio);
 
+       this_cpu_add(c->counters[BCH_COUNTER_io_read], bio_sectors(&rbio->bio));
        bch2_increment_clock(c, bio_sectors(&rbio->bio), READ);
 
        /*
@@ -2257,13 +2283,14 @@ out_read_done:
 }
 
 void __bch2_read(struct bch_fs *c, struct bch_read_bio *rbio,
-                struct bvec_iter bvec_iter, u64 inode,
+                struct bvec_iter bvec_iter, subvol_inum inum,
                 struct bch_io_failures *failed, unsigned flags)
 {
        struct btree_trans trans;
-       struct btree_iter *iter;
+       struct btree_iter iter;
        struct bkey_buf sk;
        struct bkey_s_c k;
+       u32 snapshot;
        int ret;
 
        BUG_ON(flags & BCH_READ_NODECODE);
@@ -2272,23 +2299,37 @@ void __bch2_read(struct bch_fs *c, struct bch_read_bio *rbio,
        bch2_trans_init(&trans, c, 0, 0);
 retry:
        bch2_trans_begin(&trans);
+       iter = (struct btree_iter) { NULL };
+
+       ret = bch2_subvolume_get_snapshot(&trans, inum.subvol, &snapshot);
+       if (ret)
+               goto err;
 
-       iter = bch2_trans_get_iter(&trans, BTREE_ID_extents,
-                                  POS(inode, bvec_iter.bi_sector),
-                                  BTREE_ITER_SLOTS);
+       bch2_trans_iter_init(&trans, &iter, BTREE_ID_extents,
+                            SPOS(inum.inum, bvec_iter.bi_sector, snapshot),
+                            BTREE_ITER_SLOTS);
        while (1) {
                unsigned bytes, sectors, offset_into_extent;
                enum btree_id data_btree = BTREE_ID_extents;
 
-               bch2_btree_iter_set_pos(iter,
-                               POS(inode, bvec_iter.bi_sector));
+               /*
+                * read_extent -> io_time_reset may cause a transaction restart
+                * without returning an error, we need to check for that here:
+                */
+               if (!bch2_trans_relock(&trans)) {
+                       ret = -EINTR;
+                       break;
+               }
+
+               bch2_btree_iter_set_pos(&iter,
+                               POS(inum.inum, bvec_iter.bi_sector));
 
-               k = bch2_btree_iter_peek_slot(iter);
+               k = bch2_btree_iter_peek_slot(&iter);
                ret = bkey_err(k);
                if (ret)
                        break;
 
-               offset_into_extent = iter->pos.offset -
+               offset_into_extent = iter.pos.offset -
                        bkey_start_offset(k.k);
                sectors = k.k->size - offset_into_extent;
 
@@ -2307,19 +2348,13 @@ retry:
                 */
                sectors = min(sectors, k.k->size - offset_into_extent);
 
-               /*
-                * Unlock the iterator while the btree node's lock is still in
-                * cache, before doing the IO:
-                */
-               bch2_trans_unlock(&trans);
-
                bytes = min(sectors, bvec_iter_sectors(bvec_iter)) << 9;
                swap(bvec_iter.bi_size, bytes);
 
                if (bvec_iter.bi_size == bytes)
                        flags |= BCH_READ_LAST_FRAGMENT;
 
-               ret = __bch2_read_extent(&trans, rbio, bvec_iter, iter->pos,
+               ret = __bch2_read_extent(&trans, rbio, bvec_iter, iter.pos,
                                         data_btree, k,
                                         offset_into_extent, failed, flags);
                if (ret)
@@ -2330,20 +2365,26 @@ retry:
 
                swap(bvec_iter.bi_size, bytes);
                bio_advance_iter(&rbio->bio, &bvec_iter, bytes);
+
+               ret = btree_trans_too_many_iters(&trans);
+               if (ret)
+                       break;
        }
-       bch2_trans_iter_put(&trans, iter);
+err:
+       bch2_trans_iter_exit(&trans, &iter);
 
        if (ret == -EINTR || ret == READ_RETRY || ret == READ_RETRY_AVOID)
                goto retry;
 
+       bch2_trans_exit(&trans);
+       bch2_bkey_buf_exit(&sk, c);
+
        if (ret) {
-               bch_err_inum_ratelimited(c, inode,
+               bch_err_inum_ratelimited(c, inum.inum,
                                         "read error %i from btree lookup", ret);
                rbio->bio.bi_status = BLK_STS_IOERR;
                bch2_rbio_done(rbio);
        }
-       bch2_trans_exit(&trans);
-       bch2_bkey_buf_exit(&sk, c);
 }
 
 void bch2_fs_io_exit(struct bch_fs *c)
@@ -2367,8 +2408,8 @@ int bch2_fs_io_init(struct bch_fs *c)
            mempool_init_page_pool(&c->bio_bounce_pages,
                                   max_t(unsigned,
                                         c->opts.btree_node_size,
-                                        c->sb.encoded_extent_max) /
-                                  PAGE_SECTORS, 0) ||
+                                        c->opts.encoded_extent_max) /
+                                  PAGE_SIZE, 0) ||
            rhashtable_init(&c->promote_table, &bch_promote_params))
                return -ENOMEM;