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

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