Blame SOURCES/kvm-block-Introduce-API-for-copy-offloading.patch

357786
From 62cb216e19f41b1d1c4c5d4523df56dce865ab30 Mon Sep 17 00:00:00 2001
357786
From: Fam Zheng <famz@redhat.com>
357786
Date: Fri, 29 Jun 2018 06:11:41 +0200
357786
Subject: [PATCH 37/57] block: Introduce API for copy offloading
357786
357786
RH-Author: Fam Zheng <famz@redhat.com>
357786
Message-id: <20180629061153.12687-2-famz@redhat.com>
357786
Patchwork-id: 81153
357786
O-Subject: [RHEL-7.6 qemu-kvm-rhev PATCH v2 01/13] block: Introduce API for copy offloading
357786
Bugzilla: 1482537
357786
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
357786
RH-Acked-by: Max Reitz <mreitz@redhat.com>
357786
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
357786
357786
Introduce the bdrv_co_copy_range() API for copy offloading.  Block
357786
drivers implementing this API support efficient copy operations that
357786
avoid reading each block from the source device and writing it to the
357786
destination devices.  Examples of copy offload primitives are SCSI
357786
EXTENDED COPY and Linux copy_file_range(2).
357786
357786
Signed-off-by: Fam Zheng <famz@redhat.com>
357786
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
357786
Message-id: 20180601092648.24614-2-famz@redhat.com
357786
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
357786
(cherry picked from commit fcc6767836efe1b160289905dce7228d594c123c)
357786
Signed-off-by: Fam Zheng <famz@redhat.com>
357786
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
357786
---
357786
 block/io.c                | 97 +++++++++++++++++++++++++++++++++++++++++++++++
357786
 include/block/block.h     | 32 ++++++++++++++++
357786
 include/block/block_int.h | 38 +++++++++++++++++++
357786
 3 files changed, 167 insertions(+)
357786
357786
diff --git a/block/io.c b/block/io.c
357786
index fada4ef..5c043a4 100644
357786
--- a/block/io.c
357786
+++ b/block/io.c
357786
@@ -2832,3 +2832,100 @@ void bdrv_unregister_buf(BlockDriverState *bs, void *host)
357786
         bdrv_unregister_buf(child->bs, host);
357786
     }
357786
 }
357786
+
357786
+static int coroutine_fn bdrv_co_copy_range_internal(BdrvChild *src,
357786
+                                                    uint64_t src_offset,
357786
+                                                    BdrvChild *dst,
357786
+                                                    uint64_t dst_offset,
357786
+                                                    uint64_t bytes,
357786
+                                                    BdrvRequestFlags flags,
357786
+                                                    bool recurse_src)
357786
+{
357786
+    int ret;
357786
+
357786
+    if (!src || !dst || !src->bs || !dst->bs) {
357786
+        return -ENOMEDIUM;
357786
+    }
357786
+    ret = bdrv_check_byte_request(src->bs, src_offset, bytes);
357786
+    if (ret) {
357786
+        return ret;
357786
+    }
357786
+
357786
+    ret = bdrv_check_byte_request(dst->bs, dst_offset, bytes);
357786
+    if (ret) {
357786
+        return ret;
357786
+    }
357786
+    if (flags & BDRV_REQ_ZERO_WRITE) {
357786
+        return bdrv_co_pwrite_zeroes(dst, dst_offset, bytes, flags);
357786
+    }
357786
+
357786
+    if (!src->bs->drv->bdrv_co_copy_range_from
357786
+        || !dst->bs->drv->bdrv_co_copy_range_to
357786
+        || src->bs->encrypted || dst->bs->encrypted) {
357786
+        return -ENOTSUP;
357786
+    }
357786
+    if (recurse_src) {
357786
+        return src->bs->drv->bdrv_co_copy_range_from(src->bs,
357786
+                                                     src, src_offset,
357786
+                                                     dst, dst_offset,
357786
+                                                     bytes, flags);
357786
+    } else {
357786
+        return dst->bs->drv->bdrv_co_copy_range_to(dst->bs,
357786
+                                                   src, src_offset,
357786
+                                                   dst, dst_offset,
357786
+                                                   bytes, flags);
357786
+    }
357786
+}
357786
+
357786
+/* Copy range from @src to @dst.
357786
+ *
357786
+ * See the comment of bdrv_co_copy_range for the parameter and return value
357786
+ * semantics. */
357786
+int coroutine_fn bdrv_co_copy_range_from(BdrvChild *src, uint64_t src_offset,
357786
+                                         BdrvChild *dst, uint64_t dst_offset,
357786
+                                         uint64_t bytes, BdrvRequestFlags flags)
357786
+{
357786
+    return bdrv_co_copy_range_internal(src, src_offset, dst, dst_offset,
357786
+                                       bytes, flags, true);
357786
+}
357786
+
357786
+/* Copy range from @src to @dst.
357786
+ *
357786
+ * See the comment of bdrv_co_copy_range for the parameter and return value
357786
+ * semantics. */
357786
+int coroutine_fn bdrv_co_copy_range_to(BdrvChild *src, uint64_t src_offset,
357786
+                                       BdrvChild *dst, uint64_t dst_offset,
357786
+                                       uint64_t bytes, BdrvRequestFlags flags)
357786
+{
357786
+    return bdrv_co_copy_range_internal(src, src_offset, dst, dst_offset,
357786
+                                       bytes, flags, false);
357786
+}
357786
+
357786
+int coroutine_fn bdrv_co_copy_range(BdrvChild *src, uint64_t src_offset,
357786
+                                    BdrvChild *dst, uint64_t dst_offset,
357786
+                                    uint64_t bytes, BdrvRequestFlags flags)
357786
+{
357786
+    BdrvTrackedRequest src_req, dst_req;
357786
+    BlockDriverState *src_bs = src->bs;
357786
+    BlockDriverState *dst_bs = dst->bs;
357786
+    int ret;
357786
+
357786
+    bdrv_inc_in_flight(src_bs);
357786
+    bdrv_inc_in_flight(dst_bs);
357786
+    tracked_request_begin(&src_req, src_bs, src_offset,
357786
+                          bytes, BDRV_TRACKED_READ);
357786
+    tracked_request_begin(&dst_req, dst_bs, dst_offset,
357786
+                          bytes, BDRV_TRACKED_WRITE);
357786
+
357786
+    wait_serialising_requests(&src_req);
357786
+    wait_serialising_requests(&dst_req);
357786
+    ret = bdrv_co_copy_range_from(src, src_offset,
357786
+                                  dst, dst_offset,
357786
+                                  bytes, flags);
357786
+
357786
+    tracked_request_end(&src_req);
357786
+    tracked_request_end(&dst_req);
357786
+    bdrv_dec_in_flight(src_bs);
357786
+    bdrv_dec_in_flight(dst_bs);
357786
+    return ret;
357786
+}
357786
diff --git a/include/block/block.h b/include/block/block.h
357786
index 2d17b09..e677080 100644
357786
--- a/include/block/block.h
357786
+++ b/include/block/block.h
357786
@@ -613,4 +613,36 @@ bool bdrv_can_store_new_dirty_bitmap(BlockDriverState *bs, const char *name,
357786
  */
357786
 void bdrv_register_buf(BlockDriverState *bs, void *host, size_t size);
357786
 void bdrv_unregister_buf(BlockDriverState *bs, void *host);
357786
+
357786
+/**
357786
+ *
357786
+ * bdrv_co_copy_range:
357786
+ *
357786
+ * Do offloaded copy between two children. If the operation is not implemented
357786
+ * by the driver, or if the backend storage doesn't support it, a negative
357786
+ * error code will be returned.
357786
+ *
357786
+ * Note: block layer doesn't emulate or fallback to a bounce buffer approach
357786
+ * because usually the caller shouldn't attempt offloaded copy any more (e.g.
357786
+ * calling copy_file_range(2)) after the first error, thus it should fall back
357786
+ * to a read+write path in the caller level.
357786
+ *
357786
+ * @src: Source child to copy data from
357786
+ * @src_offset: offset in @src image to read data
357786
+ * @dst: Destination child to copy data to
357786
+ * @dst_offset: offset in @dst image to write data
357786
+ * @bytes: number of bytes to copy
357786
+ * @flags: request flags. Must be one of:
357786
+ *         0 - actually read data from src;
357786
+ *         BDRV_REQ_ZERO_WRITE - treat the @src range as zero data and do zero
357786
+ *                               write on @dst as if bdrv_co_pwrite_zeroes is
357786
+ *                               called. Used to simplify caller code, or
357786
+ *                               during BlockDriver.bdrv_co_copy_range_from()
357786
+ *                               recursion.
357786
+ *
357786
+ * Returns: 0 if succeeded; negative error code if failed.
357786
+ **/
357786
+int coroutine_fn bdrv_co_copy_range(BdrvChild *src, uint64_t src_offset,
357786
+                                    BdrvChild *dst, uint64_t dst_offset,
357786
+                                    uint64_t bytes, BdrvRequestFlags flags);
357786
 #endif
357786
diff --git a/include/block/block_int.h b/include/block/block_int.h
357786
index ad2b852..3da86a7 100644
357786
--- a/include/block/block_int.h
357786
+++ b/include/block/block_int.h
357786
@@ -206,6 +206,37 @@ struct BlockDriver {
357786
     int coroutine_fn (*bdrv_co_pdiscard)(BlockDriverState *bs,
357786
         int64_t offset, int bytes);
357786
 
357786
+    /* Map [offset, offset + nbytes) range onto a child of @bs to copy from,
357786
+     * and invoke bdrv_co_copy_range_from(child, ...), or invoke
357786
+     * bdrv_co_copy_range_to() if @bs is the leaf child to copy data from.
357786
+     *
357786
+     * See the comment of bdrv_co_copy_range for the parameter and return value
357786
+     * semantics.
357786
+     */
357786
+    int coroutine_fn (*bdrv_co_copy_range_from)(BlockDriverState *bs,
357786
+                                                BdrvChild *src,
357786
+                                                uint64_t offset,
357786
+                                                BdrvChild *dst,
357786
+                                                uint64_t dst_offset,
357786
+                                                uint64_t bytes,
357786
+                                                BdrvRequestFlags flags);
357786
+
357786
+    /* Map [offset, offset + nbytes) range onto a child of bs to copy data to,
357786
+     * and invoke bdrv_co_copy_range_to(child, src, ...), or perform the copy
357786
+     * operation if @bs is the leaf and @src has the same BlockDriver.  Return
357786
+     * -ENOTSUP if @bs is the leaf but @src has a different BlockDriver.
357786
+     *
357786
+     * See the comment of bdrv_co_copy_range for the parameter and return value
357786
+     * semantics.
357786
+     */
357786
+    int coroutine_fn (*bdrv_co_copy_range_to)(BlockDriverState *bs,
357786
+                                              BdrvChild *src,
357786
+                                              uint64_t src_offset,
357786
+                                              BdrvChild *dst,
357786
+                                              uint64_t dst_offset,
357786
+                                              uint64_t bytes,
357786
+                                              BdrvRequestFlags flags);
357786
+
357786
     /*
357786
      * Building block for bdrv_block_status[_above] and
357786
      * bdrv_is_allocated[_above].  The driver should answer only
357786
@@ -1091,4 +1122,11 @@ void bdrv_dec_in_flight(BlockDriverState *bs);
357786
 
357786
 void blockdev_close_all_bdrv_states(void);
357786
 
357786
+int coroutine_fn bdrv_co_copy_range_from(BdrvChild *src, uint64_t src_offset,
357786
+                                         BdrvChild *dst, uint64_t dst_offset,
357786
+                                         uint64_t bytes, BdrvRequestFlags flags);
357786
+int coroutine_fn bdrv_co_copy_range_to(BdrvChild *src, uint64_t src_offset,
357786
+                                       BdrvChild *dst, uint64_t dst_offset,
357786
+                                       uint64_t bytes, BdrvRequestFlags flags);
357786
+
357786
 #endif /* BLOCK_INT_H */
357786
-- 
357786
1.8.3.1
357786