Blame SOURCES/kvm-file-posix-Implement-bdrv_co_copy_range.patch

357786
From 29d5339959b539767f6482adc00e02f0bb6083f9 Mon Sep 17 00:00:00 2001
357786
From: Fam Zheng <famz@redhat.com>
357786
Date: Fri, 29 Jun 2018 06:11:45 +0200
357786
Subject: [PATCH 41/57] file-posix: Implement bdrv_co_copy_range
357786
357786
RH-Author: Fam Zheng <famz@redhat.com>
357786
Message-id: <20180629061153.12687-6-famz@redhat.com>
357786
Patchwork-id: 81156
357786
O-Subject: [RHEL-7.6 qemu-kvm-rhev PATCH v2 05/13] file-posix: Implement bdrv_co_copy_range
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
With copy_file_range(2), we can implement the bdrv_co_copy_range
357786
semantics.
357786
357786
Signed-off-by: Fam Zheng <famz@redhat.com>
357786
Message-id: 20180601092648.24614-6-famz@redhat.com
357786
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
357786
(cherry picked from commit 1efad060d7e131dd52ecd1e038a6ddd37a3940c8)
357786
Signed-off-by: Fam Zheng <famz@redhat.com>
357786
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
357786
---
357786
 block/file-posix.c      | 98 +++++++++++++++++++++++++++++++++++++++++++++++--
357786
 configure               | 17 +++++++++
357786
 include/block/raw-aio.h | 10 ++++-
357786
 3 files changed, 120 insertions(+), 5 deletions(-)
357786
357786
diff --git a/block/file-posix.c b/block/file-posix.c
357786
index 370a483..29ff699 100644
357786
--- a/block/file-posix.c
357786
+++ b/block/file-posix.c
357786
@@ -59,6 +59,7 @@
357786
 #ifdef __linux__
357786
 #include <sys/ioctl.h>
357786
 #include <sys/param.h>
357786
+#include <sys/syscall.h>
357786
 #include <linux/cdrom.h>
357786
 #include <linux/fd.h>
357786
 #include <linux/fs.h>
357786
@@ -185,6 +186,8 @@ typedef struct RawPosixAIOData {
357786
 #define aio_ioctl_cmd   aio_nbytes /* for QEMU_AIO_IOCTL */
357786
     off_t aio_offset;
357786
     int aio_type;
357786
+    int aio_fd2;
357786
+    off_t aio_offset2;
357786
 } RawPosixAIOData;
357786
 
357786
 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
357786
@@ -1422,6 +1425,49 @@ static ssize_t handle_aiocb_write_zeroes(RawPosixAIOData *aiocb)
357786
     return -ENOTSUP;
357786
 }
357786
 
357786
+#ifndef HAVE_COPY_FILE_RANGE
357786
+static off_t copy_file_range(int in_fd, off_t *in_off, int out_fd,
357786
+                             off_t *out_off, size_t len, unsigned int flags)
357786
+{
357786
+#ifdef __NR_copy_file_range
357786
+    return syscall(__NR_copy_file_range, in_fd, in_off, out_fd,
357786
+                   out_off, len, flags);
357786
+#else
357786
+    errno = ENOSYS;
357786
+    return -1;
357786
+#endif
357786
+}
357786
+#endif
357786
+
357786
+static ssize_t handle_aiocb_copy_range(RawPosixAIOData *aiocb)
357786
+{
357786
+    uint64_t bytes = aiocb->aio_nbytes;
357786
+    off_t in_off = aiocb->aio_offset;
357786
+    off_t out_off = aiocb->aio_offset2;
357786
+
357786
+    while (bytes) {
357786
+        ssize_t ret = copy_file_range(aiocb->aio_fildes, &in_off,
357786
+                                      aiocb->aio_fd2, &out_off,
357786
+                                      bytes, 0);
357786
+        if (ret == -EINTR) {
357786
+            continue;
357786
+        }
357786
+        if (ret < 0) {
357786
+            if (errno == ENOSYS) {
357786
+                return -ENOTSUP;
357786
+            } else {
357786
+                return -errno;
357786
+            }
357786
+        }
357786
+        if (!ret) {
357786
+            /* No progress (e.g. when beyond EOF), fall back to buffer I/O. */
357786
+            return -ENOTSUP;
357786
+        }
357786
+        bytes -= ret;
357786
+    }
357786
+    return 0;
357786
+}
357786
+
357786
 static ssize_t handle_aiocb_discard(RawPosixAIOData *aiocb)
357786
 {
357786
     int ret = -EOPNOTSUPP;
357786
@@ -1502,6 +1548,9 @@ static int aio_worker(void *arg)
357786
     case QEMU_AIO_WRITE_ZEROES:
357786
         ret = handle_aiocb_write_zeroes(aiocb);
357786
         break;
357786
+    case QEMU_AIO_COPY_RANGE:
357786
+        ret = handle_aiocb_copy_range(aiocb);
357786
+        break;
357786
     default:
357786
         fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
357786
         ret = -EINVAL;
357786
@@ -1512,9 +1561,10 @@ static int aio_worker(void *arg)
357786
     return ret;
357786
 }
357786
 
357786
-static int paio_submit_co(BlockDriverState *bs, int fd,
357786
-                          int64_t offset, QEMUIOVector *qiov,
357786
-                          int bytes, int type)
357786
+static int paio_submit_co_full(BlockDriverState *bs, int fd,
357786
+                               int64_t offset, int fd2, int64_t offset2,
357786
+                               QEMUIOVector *qiov,
357786
+                               int bytes, int type)
357786
 {
357786
     RawPosixAIOData *acb = g_new(RawPosixAIOData, 1);
357786
     ThreadPool *pool;
357786
@@ -1522,6 +1572,8 @@ static int paio_submit_co(BlockDriverState *bs, int fd,
357786
     acb->bs = bs;
357786
     acb->aio_type = type;
357786
     acb->aio_fildes = fd;
357786
+    acb->aio_fd2 = fd2;
357786
+    acb->aio_offset2 = offset2;
357786
 
357786
     acb->aio_nbytes = bytes;
357786
     acb->aio_offset = offset;
357786
@@ -1537,6 +1589,13 @@ static int paio_submit_co(BlockDriverState *bs, int fd,
357786
     return thread_pool_submit_co(pool, aio_worker, acb);
357786
 }
357786
 
357786
+static inline int paio_submit_co(BlockDriverState *bs, int fd,
357786
+                                 int64_t offset, QEMUIOVector *qiov,
357786
+                                 int bytes, int type)
357786
+{
357786
+    return paio_submit_co_full(bs, fd, offset, -1, 0, qiov, bytes, type);
357786
+}
357786
+
357786
 static BlockAIOCB *paio_submit(BlockDriverState *bs, int fd,
357786
         int64_t offset, QEMUIOVector *qiov, int bytes,
357786
         BlockCompletionFunc *cb, void *opaque, int type)
357786
@@ -2346,6 +2405,35 @@ static void raw_abort_perm_update(BlockDriverState *bs)
357786
     raw_handle_perm_lock(bs, RAW_PL_ABORT, 0, 0, NULL);
357786
 }
357786
 
357786
+static int coroutine_fn raw_co_copy_range_from(BlockDriverState *bs,
357786
+                                               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_to(src, src_offset, dst, dst_offset, bytes, flags);
357786
+}
357786
+
357786
+static int coroutine_fn raw_co_copy_range_to(BlockDriverState *bs,
357786
+                                             BdrvChild *src, uint64_t src_offset,
357786
+                                             BdrvChild *dst, uint64_t dst_offset,
357786
+                                             uint64_t bytes, BdrvRequestFlags flags)
357786
+{
357786
+    BDRVRawState *s = bs->opaque;
357786
+    BDRVRawState *src_s;
357786
+
357786
+    assert(dst->bs == bs);
357786
+    if (src->bs->drv->bdrv_co_copy_range_to != raw_co_copy_range_to) {
357786
+        return -ENOTSUP;
357786
+    }
357786
+
357786
+    src_s = src->bs->opaque;
357786
+    if (fd_open(bs) < 0 || fd_open(bs) < 0) {
357786
+        return -EIO;
357786
+    }
357786
+    return paio_submit_co_full(bs, src_s->fd, src_offset, s->fd, dst_offset,
357786
+                               NULL, bytes, QEMU_AIO_COPY_RANGE);
357786
+}
357786
+
357786
 BlockDriver bdrv_file = {
357786
     .format_name = "file",
357786
     .protocol_name = "file",
357786
@@ -2368,6 +2456,8 @@ BlockDriver bdrv_file = {
357786
     .bdrv_co_pwritev        = raw_co_pwritev,
357786
     .bdrv_aio_flush = raw_aio_flush,
357786
     .bdrv_aio_pdiscard = raw_aio_pdiscard,
357786
+    .bdrv_co_copy_range_from = raw_co_copy_range_from,
357786
+    .bdrv_co_copy_range_to  = raw_co_copy_range_to,
357786
     .bdrv_refresh_limits = raw_refresh_limits,
357786
     .bdrv_io_plug = raw_aio_plug,
357786
     .bdrv_io_unplug = raw_aio_unplug,
357786
@@ -2845,6 +2935,8 @@ static BlockDriver bdrv_host_device = {
357786
     .bdrv_co_pwritev        = raw_co_pwritev,
357786
     .bdrv_aio_flush	= raw_aio_flush,
357786
     .bdrv_aio_pdiscard   = hdev_aio_pdiscard,
357786
+    .bdrv_co_copy_range_from = raw_co_copy_range_from,
357786
+    .bdrv_co_copy_range_to  = raw_co_copy_range_to,
357786
     .bdrv_refresh_limits = raw_refresh_limits,
357786
     .bdrv_io_plug = raw_aio_plug,
357786
     .bdrv_io_unplug = raw_aio_unplug,
357786
diff --git a/configure b/configure
357786
index d2367f1..a869f19 100755
357786
--- a/configure
357786
+++ b/configure
357786
@@ -5156,6 +5156,20 @@ if test "$fortify_source" != "no"; then
357786
   fi
357786
 fi
357786
 
357786
+###############################################
357786
+# Check if copy_file_range is provided by glibc
357786
+have_copy_file_range=no
357786
+cat > $TMPC << EOF
357786
+#include <unistd.h>
357786
+int main(void) {
357786
+  copy_file_range(0, NULL, 0, NULL, 0, 0);
357786
+  return 0;
357786
+}
357786
+EOF
357786
+if compile_prog "" "" ; then
357786
+    have_copy_file_range=yes
357786
+fi
357786
+
357786
 ##########################################
357786
 # check if struct fsxattr is available via linux/fs.h
357786
 
357786
@@ -6233,6 +6247,9 @@ fi
357786
 if test "$have_fsxattr" = "yes" ; then
357786
     echo "HAVE_FSXATTR=y" >> $config_host_mak
357786
 fi
357786
+if test "$have_copy_file_range" = "yes" ; then
357786
+    echo "HAVE_COPY_FILE_RANGE=y" >> $config_host_mak
357786
+fi
357786
 if test "$vte" = "yes" ; then
357786
   echo "CONFIG_VTE=y" >> $config_host_mak
357786
   echo "VTE_CFLAGS=$vte_cflags" >> $config_host_mak
357786
diff --git a/include/block/raw-aio.h b/include/block/raw-aio.h
357786
index a4cdbbf..3240530 100644
357786
--- a/include/block/raw-aio.h
357786
+++ b/include/block/raw-aio.h
357786
@@ -25,9 +25,15 @@
357786
 #define QEMU_AIO_FLUSH        0x0008
357786
 #define QEMU_AIO_DISCARD      0x0010
357786
 #define QEMU_AIO_WRITE_ZEROES 0x0020
357786
+#define QEMU_AIO_COPY_RANGE   0x0040
357786
 #define QEMU_AIO_TYPE_MASK \
357786
-        (QEMU_AIO_READ|QEMU_AIO_WRITE|QEMU_AIO_IOCTL|QEMU_AIO_FLUSH| \
357786
-         QEMU_AIO_DISCARD|QEMU_AIO_WRITE_ZEROES)
357786
+        (QEMU_AIO_READ | \
357786
+         QEMU_AIO_WRITE | \
357786
+         QEMU_AIO_IOCTL | \
357786
+         QEMU_AIO_FLUSH | \
357786
+         QEMU_AIO_DISCARD | \
357786
+         QEMU_AIO_WRITE_ZEROES | \
357786
+         QEMU_AIO_COPY_RANGE)
357786
 
357786
 /* AIO flags */
357786
 #define QEMU_AIO_MISALIGNED   0x1000
357786
-- 
357786
1.8.3.1
357786