yeahuh / rpms / qemu-kvm

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