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