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

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