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