btrfs: handle recording of zoned writes in the storage layer
[linux-block.git] / fs / btrfs / bio.c
index 8affc88b0e0a4b68ed61e08fd1552331fd9c8517..c49a4d7d4a573cf8bc65ff1e1d2cc48899e2caed 100644 (file)
 #include "dev-replace.h"
 #include "rcu-string.h"
 #include "zoned.h"
+#include "file-item.h"
 
 static struct bio_set btrfs_bioset;
+static struct bio_set btrfs_repair_bioset;
+static mempool_t btrfs_failed_bio_pool;
+
+struct btrfs_failed_bio {
+       struct btrfs_bio *bbio;
+       int num_copies;
+       atomic_t repair_count;
+};
 
 /*
  * Initialize a btrfs_bio structure.  This skips the embedded bio itself as it
  * is already initialized by the block layer.
  */
 static inline void btrfs_bio_init(struct btrfs_bio *bbio,
+                                 struct btrfs_inode *inode,
                                  btrfs_bio_end_io_t end_io, void *private)
 {
        memset(bbio, 0, offsetof(struct btrfs_bio, bio));
+       bbio->inode = inode;
        bbio->end_io = end_io;
        bbio->private = private;
 }
@@ -37,16 +48,18 @@ static inline void btrfs_bio_init(struct btrfs_bio *bbio,
  * a mempool.
  */
 struct bio *btrfs_bio_alloc(unsigned int nr_vecs, blk_opf_t opf,
+                           struct btrfs_inode *inode,
                            btrfs_bio_end_io_t end_io, void *private)
 {
        struct bio *bio;
 
        bio = bio_alloc_bioset(NULL, nr_vecs, opf, GFP_NOFS, &btrfs_bioset);
-       btrfs_bio_init(btrfs_bio(bio), end_io, private);
+       btrfs_bio_init(btrfs_bio(bio), inode, end_io, private);
        return bio;
 }
 
 struct bio *btrfs_bio_clone_partial(struct bio *orig, u64 offset, u64 size,
+                                   struct btrfs_inode *inode,
                                    btrfs_bio_end_io_t end_io, void *private)
 {
        struct bio *bio;
@@ -56,13 +69,169 @@ struct bio *btrfs_bio_clone_partial(struct bio *orig, u64 offset, u64 size,
 
        bio = bio_alloc_clone(orig->bi_bdev, orig, GFP_NOFS, &btrfs_bioset);
        bbio = btrfs_bio(bio);
-       btrfs_bio_init(bbio, end_io, private);
+       btrfs_bio_init(bbio, inode, end_io, private);
 
        bio_trim(bio, offset >> 9, size >> 9);
-       bbio->iter = bio->bi_iter;
        return bio;
 }
 
+static int next_repair_mirror(struct btrfs_failed_bio *fbio, int cur_mirror)
+{
+       if (cur_mirror == fbio->num_copies)
+               return cur_mirror + 1 - fbio->num_copies;
+       return cur_mirror + 1;
+}
+
+static int prev_repair_mirror(struct btrfs_failed_bio *fbio, int cur_mirror)
+{
+       if (cur_mirror == 1)
+               return fbio->num_copies;
+       return cur_mirror - 1;
+}
+
+static void btrfs_repair_done(struct btrfs_failed_bio *fbio)
+{
+       if (atomic_dec_and_test(&fbio->repair_count)) {
+               fbio->bbio->end_io(fbio->bbio);
+               mempool_free(fbio, &btrfs_failed_bio_pool);
+       }
+}
+
+static void btrfs_end_repair_bio(struct btrfs_bio *repair_bbio,
+                                struct btrfs_device *dev)
+{
+       struct btrfs_failed_bio *fbio = repair_bbio->private;
+       struct btrfs_inode *inode = repair_bbio->inode;
+       struct btrfs_fs_info *fs_info = inode->root->fs_info;
+       struct bio_vec *bv = bio_first_bvec_all(&repair_bbio->bio);
+       int mirror = repair_bbio->mirror_num;
+
+       if (repair_bbio->bio.bi_status ||
+           !btrfs_data_csum_ok(repair_bbio, dev, 0, bv)) {
+               bio_reset(&repair_bbio->bio, NULL, REQ_OP_READ);
+               repair_bbio->bio.bi_iter = repair_bbio->saved_iter;
+
+               mirror = next_repair_mirror(fbio, mirror);
+               if (mirror == fbio->bbio->mirror_num) {
+                       btrfs_debug(fs_info, "no mirror left");
+                       fbio->bbio->bio.bi_status = BLK_STS_IOERR;
+                       goto done;
+               }
+
+               btrfs_submit_bio(fs_info, &repair_bbio->bio, mirror);
+               return;
+       }
+
+       do {
+               mirror = prev_repair_mirror(fbio, mirror);
+               btrfs_repair_io_failure(fs_info, btrfs_ino(inode),
+                                 repair_bbio->file_offset, fs_info->sectorsize,
+                                 repair_bbio->saved_iter.bi_sector << SECTOR_SHIFT,
+                                 bv->bv_page, bv->bv_offset, mirror);
+       } while (mirror != fbio->bbio->mirror_num);
+
+done:
+       btrfs_repair_done(fbio);
+       bio_put(&repair_bbio->bio);
+}
+
+/*
+ * Try to kick off a repair read to the next available mirror for a bad sector.
+ *
+ * This primarily tries to recover good data to serve the actual read request,
+ * but also tries to write the good data back to the bad mirror(s) when a
+ * read succeeded to restore the redundancy.
+ */
+static struct btrfs_failed_bio *repair_one_sector(struct btrfs_bio *failed_bbio,
+                                                 u32 bio_offset,
+                                                 struct bio_vec *bv,
+                                                 struct btrfs_failed_bio *fbio)
+{
+       struct btrfs_inode *inode = failed_bbio->inode;
+       struct btrfs_fs_info *fs_info = inode->root->fs_info;
+       const u32 sectorsize = fs_info->sectorsize;
+       const u64 logical = (failed_bbio->saved_iter.bi_sector << SECTOR_SHIFT);
+       struct btrfs_bio *repair_bbio;
+       struct bio *repair_bio;
+       int num_copies;
+       int mirror;
+
+       btrfs_debug(fs_info, "repair read error: read error at %llu",
+                   failed_bbio->file_offset + bio_offset);
+
+       num_copies = btrfs_num_copies(fs_info, logical, sectorsize);
+       if (num_copies == 1) {
+               btrfs_debug(fs_info, "no copy to repair from");
+               failed_bbio->bio.bi_status = BLK_STS_IOERR;
+               return fbio;
+       }
+
+       if (!fbio) {
+               fbio = mempool_alloc(&btrfs_failed_bio_pool, GFP_NOFS);
+               fbio->bbio = failed_bbio;
+               fbio->num_copies = num_copies;
+               atomic_set(&fbio->repair_count, 1);
+       }
+
+       atomic_inc(&fbio->repair_count);
+
+       repair_bio = bio_alloc_bioset(NULL, 1, REQ_OP_READ, GFP_NOFS,
+                                     &btrfs_repair_bioset);
+       repair_bio->bi_iter.bi_sector = failed_bbio->saved_iter.bi_sector;
+       bio_add_page(repair_bio, bv->bv_page, bv->bv_len, bv->bv_offset);
+
+       repair_bbio = btrfs_bio(repair_bio);
+       btrfs_bio_init(repair_bbio, failed_bbio->inode, NULL, fbio);
+       repair_bbio->file_offset = failed_bbio->file_offset + bio_offset;
+
+       mirror = next_repair_mirror(fbio, failed_bbio->mirror_num);
+       btrfs_debug(fs_info, "submitting repair read to mirror %d", mirror);
+       btrfs_submit_bio(fs_info, repair_bio, mirror);
+       return fbio;
+}
+
+static void btrfs_check_read_bio(struct btrfs_bio *bbio, struct btrfs_device *dev)
+{
+       struct btrfs_inode *inode = bbio->inode;
+       struct btrfs_fs_info *fs_info = inode->root->fs_info;
+       u32 sectorsize = fs_info->sectorsize;
+       struct bvec_iter *iter = &bbio->saved_iter;
+       blk_status_t status = bbio->bio.bi_status;
+       struct btrfs_failed_bio *fbio = NULL;
+       u32 offset = 0;
+
+       /*
+        * Hand off repair bios to the repair code as there is no upper level
+        * submitter for them.
+        */
+       if (bbio->bio.bi_pool == &btrfs_repair_bioset) {
+               btrfs_end_repair_bio(bbio, dev);
+               return;
+       }
+
+       /* Clear the I/O error. A failed repair will reset it. */
+       bbio->bio.bi_status = BLK_STS_OK;
+
+       while (iter->bi_size) {
+               struct bio_vec bv = bio_iter_iovec(&bbio->bio, *iter);
+
+               bv.bv_len = min(bv.bv_len, sectorsize);
+               if (status || !btrfs_data_csum_ok(bbio, dev, offset, &bv))
+                       fbio = repair_one_sector(bbio, offset, &bv, fbio);
+
+               bio_advance_iter_single(&bbio->bio, iter, sectorsize);
+               offset += sectorsize;
+       }
+
+       if (bbio->csum != bbio->csum_inline)
+               kfree(bbio->csum);
+
+       if (fbio)
+               btrfs_repair_done(fbio);
+       else
+               bbio->end_io(bbio);
+}
+
 static void btrfs_log_dev_io_error(struct bio *bio, struct btrfs_device *dev)
 {
        if (!dev || !dev->bdev)
@@ -90,23 +259,30 @@ static void btrfs_end_bio_work(struct work_struct *work)
 {
        struct btrfs_bio *bbio = container_of(work, struct btrfs_bio, end_io_work);
 
-       bbio->end_io(bbio);
+       /* Metadata reads are checked and repaired by the submitter. */
+       if (bbio->bio.bi_opf & REQ_META)
+               bbio->end_io(bbio);
+       else
+               btrfs_check_read_bio(bbio, bbio->bio.bi_private);
 }
 
 static void btrfs_simple_end_io(struct bio *bio)
 {
-       struct btrfs_fs_info *fs_info = bio->bi_private;
        struct btrfs_bio *bbio = btrfs_bio(bio);
+       struct btrfs_device *dev = bio->bi_private;
+       struct btrfs_fs_info *fs_info = bbio->inode->root->fs_info;
 
        btrfs_bio_counter_dec(fs_info);
 
        if (bio->bi_status)
-               btrfs_log_dev_io_error(bio, bbio->device);
+               btrfs_log_dev_io_error(bio, dev);
 
        if (bio_op(bio) == REQ_OP_READ) {
                INIT_WORK(&bbio->end_io_work, btrfs_end_bio_work);
                queue_work(btrfs_end_io_wq(fs_info, bio), &bbio->end_io_work);
        } else {
+               if (bio_op(bio) == REQ_OP_ZONE_APPEND)
+                       btrfs_record_physical_zoned(bbio);
                bbio->end_io(bbio);
        }
 }
@@ -118,7 +294,10 @@ static void btrfs_raid56_end_io(struct bio *bio)
 
        btrfs_bio_counter_dec(bioc->fs_info);
        bbio->mirror_num = bioc->mirror_num;
-       bbio->end_io(bbio);
+       if (bio_op(bio) == REQ_OP_READ && !(bbio->bio.bi_opf & REQ_META))
+               btrfs_check_read_bio(bbio, NULL);
+       else
+               bbio->end_io(bbio);
 
        btrfs_put_bioc(bioc);
 }
@@ -224,22 +403,183 @@ static void btrfs_submit_mirrored_bio(struct btrfs_io_context *bioc, int dev_nr)
        btrfs_submit_dev_bio(bioc->stripes[dev_nr].dev, bio);
 }
 
+static void __btrfs_submit_bio(struct bio *bio, struct btrfs_io_context *bioc,
+                              struct btrfs_io_stripe *smap, int mirror_num)
+{
+       /* Do not leak our private flag into the block layer. */
+       bio->bi_opf &= ~REQ_BTRFS_ONE_ORDERED;
+
+       if (!bioc) {
+               /* Single mirror read/write fast path. */
+               btrfs_bio(bio)->mirror_num = mirror_num;
+               bio->bi_iter.bi_sector = smap->physical >> SECTOR_SHIFT;
+               bio->bi_private = smap->dev;
+               bio->bi_end_io = btrfs_simple_end_io;
+               btrfs_submit_dev_bio(smap->dev, bio);
+       } else if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
+               /* Parity RAID write or read recovery. */
+               bio->bi_private = bioc;
+               bio->bi_end_io = btrfs_raid56_end_io;
+               if (bio_op(bio) == REQ_OP_READ)
+                       raid56_parity_recover(bio, bioc, mirror_num);
+               else
+                       raid56_parity_write(bio, bioc);
+       } else {
+               /* Write to multiple mirrors. */
+               int total_devs = bioc->num_stripes;
+
+               bioc->orig_bio = bio;
+               for (int dev_nr = 0; dev_nr < total_devs; dev_nr++)
+                       btrfs_submit_mirrored_bio(bioc, dev_nr);
+       }
+}
+
+static blk_status_t btrfs_bio_csum(struct btrfs_bio *bbio)
+{
+       if (bbio->bio.bi_opf & REQ_META)
+               return btree_csum_one_bio(&bbio->bio);
+       return btrfs_csum_one_bio(bbio);
+}
+
+/*
+ * Async submit bios are used to offload expensive checksumming onto the worker
+ * threads.
+ */
+struct async_submit_bio {
+       struct btrfs_bio *bbio;
+       struct btrfs_io_context *bioc;
+       struct btrfs_io_stripe smap;
+       int mirror_num;
+       struct btrfs_work work;
+};
+
+/*
+ * In order to insert checksums into the metadata in large chunks, we wait
+ * until bio submission time.   All the pages in the bio are checksummed and
+ * sums are attached onto the ordered extent record.
+ *
+ * At IO completion time the csums attached on the ordered extent record are
+ * inserted into the btree.
+ */
+static void run_one_async_start(struct btrfs_work *work)
+{
+       struct async_submit_bio *async =
+               container_of(work, struct async_submit_bio, work);
+       blk_status_t ret;
+
+       ret = btrfs_bio_csum(async->bbio);
+       if (ret)
+               async->bbio->bio.bi_status = ret;
+}
+
+/*
+ * In order to insert checksums into the metadata in large chunks, we wait
+ * until bio submission time.   All the pages in the bio are checksummed and
+ * sums are attached onto the ordered extent record.
+ *
+ * At IO completion time the csums attached on the ordered extent record are
+ * inserted into the tree.
+ */
+static void run_one_async_done(struct btrfs_work *work)
+{
+       struct async_submit_bio *async =
+               container_of(work, struct async_submit_bio, work);
+       struct bio *bio = &async->bbio->bio;
+
+       /* If an error occurred we just want to clean up the bio and move on. */
+       if (bio->bi_status) {
+               btrfs_bio_end_io(async->bbio, bio->bi_status);
+               return;
+       }
+
+       /*
+        * All of the bios that pass through here are from async helpers.
+        * Use REQ_CGROUP_PUNT to issue them from the owning cgroup's context.
+        * This changes nothing when cgroups aren't in use.
+        */
+       bio->bi_opf |= REQ_CGROUP_PUNT;
+       __btrfs_submit_bio(bio, async->bioc, &async->smap, async->mirror_num);
+}
+
+static void run_one_async_free(struct btrfs_work *work)
+{
+       kfree(container_of(work, struct async_submit_bio, work));
+}
+
+static bool should_async_write(struct btrfs_bio *bbio)
+{
+       /*
+        * If the I/O is not issued by fsync and friends, (->sync_writers != 0),
+        * then try to defer the submission to a workqueue to parallelize the
+        * checksum calculation.
+        */
+       if (atomic_read(&bbio->inode->sync_writers))
+               return false;
+
+       /*
+        * Submit metadata writes synchronously if the checksum implementation
+        * is fast, or we are on a zoned device that wants I/O to be submitted
+        * in order.
+        */
+       if (bbio->bio.bi_opf & REQ_META) {
+               struct btrfs_fs_info *fs_info = bbio->inode->root->fs_info;
+
+               if (btrfs_is_zoned(fs_info))
+                       return false;
+               if (test_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags))
+                       return false;
+       }
+
+       return true;
+}
+
+/*
+ * Submit bio to an async queue.
+ *
+ * Return true if the work has been succesfuly submitted, else false.
+ */
+static bool btrfs_wq_submit_bio(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 async_submit_bio *async;
+
+       async = kmalloc(sizeof(*async), GFP_NOFS);
+       if (!async)
+               return false;
+
+       async->bbio = bbio;
+       async->bioc = bioc;
+       async->smap = *smap;
+       async->mirror_num = mirror_num;
+
+       btrfs_init_work(&async->work, run_one_async_start, run_one_async_done,
+                       run_one_async_free);
+       if (op_is_sync(bbio->bio.bi_opf))
+               btrfs_queue_work(fs_info->hipri_workers, &async->work);
+       else
+               btrfs_queue_work(fs_info->workers, &async->work);
+       return true;
+}
+
 void btrfs_submit_bio(struct btrfs_fs_info *fs_info, struct bio *bio, int mirror_num)
 {
+       struct btrfs_bio *bbio = btrfs_bio(bio);
        u64 logical = bio->bi_iter.bi_sector << 9;
        u64 length = bio->bi_iter.bi_size;
        u64 map_length = length;
        struct btrfs_io_context *bioc = NULL;
        struct btrfs_io_stripe smap;
-       int ret;
+       blk_status_t ret;
+       int error;
 
        btrfs_bio_counter_inc_blocked(fs_info);
-       ret = __btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
-                               &bioc, &smap, &mirror_num, 1);
-       if (ret) {
-               btrfs_bio_counter_dec(fs_info);
-               btrfs_bio_end_io(btrfs_bio(bio), errno_to_blk_status(ret));
-               return;
+       error = __btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
+                                 &bioc, &smap, &mirror_num, 1);
+       if (error) {
+               ret = errno_to_blk_status(error);
+               goto fail;
        }
 
        if (map_length < length) {
@@ -249,31 +589,47 @@ void btrfs_submit_bio(struct btrfs_fs_info *fs_info, struct bio *bio, int mirror
                BUG();
        }
 
-       if (!bioc) {
-               /* Single mirror read/write fast path */
-               btrfs_bio(bio)->mirror_num = mirror_num;
-               btrfs_bio(bio)->device = smap.dev;
-               bio->bi_iter.bi_sector = smap.physical >> SECTOR_SHIFT;
-               bio->bi_private = fs_info;
-               bio->bi_end_io = btrfs_simple_end_io;
-               btrfs_submit_dev_bio(smap.dev, bio);
-       } else if (bioc->map_type & BTRFS_BLOCK_GROUP_RAID56_MASK) {
-               /* Parity RAID write or read recovery */
-               bio->bi_private = bioc;
-               bio->bi_end_io = btrfs_raid56_end_io;
-               if (bio_op(bio) == REQ_OP_READ)
-                       raid56_parity_recover(bio, bioc, mirror_num);
-               else
-                       raid56_parity_write(bio, bioc);
-       } else {
-               /* Write to multiple mirrors */
-               int total_devs = bioc->num_stripes;
-               int dev_nr;
+       /*
+        * Save the iter for the end_io handler and preload the checksums for
+        * data reads.
+        */
+       if (bio_op(bio) == REQ_OP_READ && !(bio->bi_opf & REQ_META)) {
+               bbio->saved_iter = bio->bi_iter;
+               ret = btrfs_lookup_bio_sums(bbio);
+               if (ret)
+                       goto fail;
+       }
 
-               bioc->orig_bio = bio;
-               for (dev_nr = 0; dev_nr < total_devs; dev_nr++)
-                       btrfs_submit_mirrored_bio(bioc, dev_nr);
+       if (btrfs_op(bio) == BTRFS_MAP_WRITE) {
+               if (bio_op(bio) == REQ_OP_ZONE_APPEND) {
+                       ret = btrfs_extract_ordered_extent(btrfs_bio(bio));
+                       if (ret)
+                               goto fail;
+               }
+
+               /*
+                * Csum items for reloc roots have already been cloned at this
+                * point, so they are handled as part of the no-checksum case.
+                */
+               if (!(bbio->inode->flags & BTRFS_INODE_NODATASUM) &&
+                   !test_bit(BTRFS_FS_STATE_NO_CSUMS, &fs_info->fs_state) &&
+                   !btrfs_is_data_reloc_root(bbio->inode->root)) {
+                       if (should_async_write(bbio) &&
+                           btrfs_wq_submit_bio(bbio, bioc, &smap, mirror_num))
+                               return;
+
+                       ret = btrfs_bio_csum(bbio);
+                       if (ret)
+                               goto fail;
+               }
        }
+
+       __btrfs_submit_bio(bio, bioc, &smap, mirror_num);
+       return;
+
+fail:
+       btrfs_bio_counter_dec(fs_info);
+       btrfs_bio_end_io(bbio, ret);
 }
 
 /*
@@ -283,7 +639,7 @@ void btrfs_submit_bio(struct btrfs_fs_info *fs_info, struct bio *bio, int mirror
  * RAID setup.  Here we only want to write the one bad copy, so we do the
  * mapping ourselves and submit the bio directly.
  *
- * The I/O is issued sychronously to block the repair read completion from
+ * The I/O is issued synchronously to block the repair read completion from
  * freeing the bio.
  */
 int btrfs_repair_io_failure(struct btrfs_fs_info *fs_info, u64 ino, u64 start,
@@ -381,10 +737,25 @@ int __init btrfs_bioset_init(void)
                        offsetof(struct btrfs_bio, bio),
                        BIOSET_NEED_BVECS))
                return -ENOMEM;
+       if (bioset_init(&btrfs_repair_bioset, BIO_POOL_SIZE,
+                       offsetof(struct btrfs_bio, bio),
+                       BIOSET_NEED_BVECS))
+               goto out_free_bioset;
+       if (mempool_init_kmalloc_pool(&btrfs_failed_bio_pool, BIO_POOL_SIZE,
+                                     sizeof(struct btrfs_failed_bio)))
+               goto out_free_repair_bioset;
        return 0;
+
+out_free_repair_bioset:
+       bioset_exit(&btrfs_repair_bioset);
+out_free_bioset:
+       bioset_exit(&btrfs_bioset);
+       return -ENOMEM;
 }
 
 void __cold btrfs_bioset_exit(void)
 {
+       mempool_exit(&btrfs_failed_bio_pool);
+       bioset_exit(&btrfs_repair_bioset);
        bioset_exit(&btrfs_bioset);
 }