return map_length;
}
+/*
+ * Look up the data checksums of a read without holding the bio counter, then
+ * map the bio again.
+ *
+ * The lookup can read csum tree blocks, and those reads go through
+ * btrfs_submit_chunk() and btrfs_bio_counter_inc_blocked() again. If device
+ * replace starts finishing in between, it waits for our counter reference to
+ * be dropped while we wait for it to finish, and every new bio blocks behind
+ * it. The counter only protects the mapping, so drop both for the lookup.
+ *
+ * Called and returns with the bio counter held.
+ */
+static int btrfs_lookup_sums_and_remap(struct btrfs_bio *bbio,
+ struct btrfs_io_context **bioc,
+ struct btrfs_io_stripe *smap, int *mirror_num)
+{
+ struct btrfs_fs_info *fs_info = bbio->inode->root->fs_info;
+ struct bio *bio = &bbio->bio;
+ const u64 logical = bio->bi_iter.bi_sector << SECTOR_SHIFT;
+ const u64 length = bio->bi_iter.bi_size;
+ u64 map_length = length;
+ int ret;
+
+ btrfs_put_bioc(*bioc);
+ *bioc = NULL;
+ btrfs_bio_counter_dec(fs_info);
+
+ ret = btrfs_lookup_bio_sums(bbio);
+
+ btrfs_bio_counter_inc_blocked(fs_info);
+ if (ret)
+ return ret;
+
+ /* @mirror_num is the mirror the first mapping picked, so this maps the same copy. */
+ ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length, bioc,
+ smap, mirror_num);
+ if (ret)
+ return ret;
+ /* The chunk can not change under a read of it. */
+ if (WARN_ON_ONCE(map_length < length)) {
+ btrfs_put_bioc(*bioc);
+ *bioc = NULL;
+ return -EUCLEAN;
+ }
+ return 0;
+}
+
static bool btrfs_submit_chunk(struct btrfs_bio *bbio, int mirror_num)
{
struct btrfs_inode *inode = bbio->inode;
*/
if (bio_op(bio) == REQ_OP_READ && is_data_inode(bbio->inode)) {
bbio->saved_iter = bio->bi_iter;
- ret = btrfs_lookup_bio_sums(bbio);
+ ret = btrfs_lookup_sums_and_remap(bbio, &bioc, &smap, &mirror_num);
status = errno_to_blk_status(ret);
if (status)
goto fail;