From nobody Mon Apr 14 21:14:31 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1528111745724628.8917985234451; Mon, 4 Jun 2018 04:29:05 -0700 (PDT) Received: from localhost ([::1]:38963 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fPnfR-0005bM-7E for importer@patchew.org; Mon, 04 Jun 2018 07:28:53 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36654) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fPnXo-0007ue-89 for qemu-devel@nongnu.org; Mon, 04 Jun 2018 07:21:01 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fPnXm-0001SY-N6 for qemu-devel@nongnu.org; Mon, 04 Jun 2018 07:21:00 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:50964 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fPnXg-0001Qt-HQ; Mon, 04 Jun 2018 07:20:52 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 276654022414; Mon, 4 Jun 2018 11:20:52 +0000 (UTC) Received: from localhost (ovpn-116-202.ams2.redhat.com [10.36.116.202]) by smtp.corp.redhat.com (Postfix) with ESMTP id C172A2026DEF; Mon, 4 Jun 2018 11:20:51 +0000 (UTC) From: Stefan Hajnoczi To: Date: Mon, 4 Jun 2018 12:20:30 +0100 Message-Id: <20180604112036.2715-6-stefanha@redhat.com> In-Reply-To: <20180604112036.2715-1-stefanha@redhat.com> References: <20180604112036.2715-1-stefanha@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Mon, 04 Jun 2018 11:20:52 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.6]); Mon, 04 Jun 2018 11:20:52 +0000 (UTC) for IP:'10.11.54.4' DOMAIN:'int-mx04.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'stefanha@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 Subject: [Qemu-devel] [PULL 05/11] file-posix: Implement bdrv_co_copy_range X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Peter Maydell , Fam Zheng , Stefan Hajnoczi , qemu-block@nongnu.org, Peter Lieven , Max Reitz , Ronnie Sahlberg , Paolo Bonzini Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Fam Zheng With copy_file_range(2), we can implement the bdrv_co_copy_range semantics. Signed-off-by: Fam Zheng Message-id: 20180601092648.24614-6-famz@redhat.com Signed-off-by: Stefan Hajnoczi --- configure | 17 +++++++ include/block/raw-aio.h | 10 ++++- block/file-posix.c | 98 +++++++++++++++++++++++++++++++++++++++-- 3 files changed, 120 insertions(+), 5 deletions(-) diff --git a/configure b/configure index be6edc7b81..8c6ea9ba7f 100755 --- a/configure +++ b/configure @@ -5183,6 +5183,20 @@ if test "$fortify_source" !=3D "no"; then fi fi =20 +############################################### +# Check if copy_file_range is provided by glibc +have_copy_file_range=3Dno +cat > $TMPC << EOF +#include +int main(void) { + copy_file_range(0, NULL, 0, NULL, 0, 0); + return 0; +} +EOF +if compile_prog "" "" ; then + have_copy_file_range=3Dyes +fi + ########################################## # check if struct fsxattr is available via linux/fs.h =20 @@ -6286,6 +6300,9 @@ fi if test "$have_fsxattr" =3D "yes" ; then echo "HAVE_FSXATTR=3Dy" >> $config_host_mak fi +if test "$have_copy_file_range" =3D "yes" ; then + echo "HAVE_COPY_FILE_RANGE=3Dy" >> $config_host_mak +fi if test "$vte" =3D "yes" ; then echo "CONFIG_VTE=3Dy" >> $config_host_mak echo "VTE_CFLAGS=3D$vte_cflags" >> $config_host_mak diff --git a/include/block/raw-aio.h b/include/block/raw-aio.h index 9e47b8a629..0e717fd475 100644 --- a/include/block/raw-aio.h +++ b/include/block/raw-aio.h @@ -25,9 +25,15 @@ #define QEMU_AIO_FLUSH 0x0008 #define QEMU_AIO_DISCARD 0x0010 #define QEMU_AIO_WRITE_ZEROES 0x0020 +#define QEMU_AIO_COPY_RANGE 0x0040 #define QEMU_AIO_TYPE_MASK \ - (QEMU_AIO_READ|QEMU_AIO_WRITE|QEMU_AIO_IOCTL|QEMU_AIO_FLUSH| \ - QEMU_AIO_DISCARD|QEMU_AIO_WRITE_ZEROES) + (QEMU_AIO_READ | \ + QEMU_AIO_WRITE | \ + QEMU_AIO_IOCTL | \ + QEMU_AIO_FLUSH | \ + QEMU_AIO_DISCARD | \ + QEMU_AIO_WRITE_ZEROES | \ + QEMU_AIO_COPY_RANGE) =20 /* AIO flags */ #define QEMU_AIO_MISALIGNED 0x1000 diff --git a/block/file-posix.c b/block/file-posix.c index 5a602cfe37..513d371bb1 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -59,6 +59,7 @@ #ifdef __linux__ #include #include +#include #include #include #include @@ -187,6 +188,8 @@ typedef struct RawPosixAIOData { #define aio_ioctl_cmd aio_nbytes /* for QEMU_AIO_IOCTL */ off_t aio_offset; int aio_type; + int aio_fd2; + off_t aio_offset2; } RawPosixAIOData; =20 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) @@ -1446,6 +1449,49 @@ static ssize_t handle_aiocb_write_zeroes(RawPosixAIO= Data *aiocb) return -ENOTSUP; } =20 +#ifndef HAVE_COPY_FILE_RANGE +static off_t copy_file_range(int in_fd, off_t *in_off, int out_fd, + off_t *out_off, size_t len, unsigned int flag= s) +{ +#ifdef __NR_copy_file_range + return syscall(__NR_copy_file_range, in_fd, in_off, out_fd, + out_off, len, flags); +#else + errno =3D ENOSYS; + return -1; +#endif +} +#endif + +static ssize_t handle_aiocb_copy_range(RawPosixAIOData *aiocb) +{ + uint64_t bytes =3D aiocb->aio_nbytes; + off_t in_off =3D aiocb->aio_offset; + off_t out_off =3D aiocb->aio_offset2; + + while (bytes) { + ssize_t ret =3D copy_file_range(aiocb->aio_fildes, &in_off, + aiocb->aio_fd2, &out_off, + bytes, 0); + if (ret =3D=3D -EINTR) { + continue; + } + if (ret < 0) { + if (errno =3D=3D ENOSYS) { + return -ENOTSUP; + } else { + return -errno; + } + } + if (!ret) { + /* No progress (e.g. when beyond EOF), fall back to buffer I/O= . */ + return -ENOTSUP; + } + bytes -=3D ret; + } + return 0; +} + static ssize_t handle_aiocb_discard(RawPosixAIOData *aiocb) { int ret =3D -EOPNOTSUPP; @@ -1526,6 +1572,9 @@ static int aio_worker(void *arg) case QEMU_AIO_WRITE_ZEROES: ret =3D handle_aiocb_write_zeroes(aiocb); break; + case QEMU_AIO_COPY_RANGE: + ret =3D handle_aiocb_copy_range(aiocb); + break; default: fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type); ret =3D -EINVAL; @@ -1536,9 +1585,10 @@ static int aio_worker(void *arg) return ret; } =20 -static int paio_submit_co(BlockDriverState *bs, int fd, - int64_t offset, QEMUIOVector *qiov, - int bytes, int type) +static int paio_submit_co_full(BlockDriverState *bs, int fd, + int64_t offset, int fd2, int64_t offset2, + QEMUIOVector *qiov, + int bytes, int type) { RawPosixAIOData *acb =3D g_new(RawPosixAIOData, 1); ThreadPool *pool; @@ -1546,6 +1596,8 @@ static int paio_submit_co(BlockDriverState *bs, int f= d, acb->bs =3D bs; acb->aio_type =3D type; acb->aio_fildes =3D fd; + acb->aio_fd2 =3D fd2; + acb->aio_offset2 =3D offset2; =20 acb->aio_nbytes =3D bytes; acb->aio_offset =3D offset; @@ -1561,6 +1613,13 @@ static int paio_submit_co(BlockDriverState *bs, int = fd, return thread_pool_submit_co(pool, aio_worker, acb); } =20 +static inline int paio_submit_co(BlockDriverState *bs, int fd, + int64_t offset, QEMUIOVector *qiov, + int bytes, int type) +{ + return paio_submit_co_full(bs, fd, offset, -1, 0, qiov, bytes, type); +} + static BlockAIOCB *paio_submit(BlockDriverState *bs, int fd, int64_t offset, QEMUIOVector *qiov, int bytes, BlockCompletionFunc *cb, void *opaque, int type) @@ -2451,6 +2510,35 @@ static void raw_abort_perm_update(BlockDriverState *= bs) raw_handle_perm_lock(bs, RAW_PL_ABORT, 0, 0, NULL); } =20 +static int coroutine_fn raw_co_copy_range_from(BlockDriverState *bs, + BdrvChild *src, uint64_t sr= c_offset, + BdrvChild *dst, uint64_t ds= t_offset, + uint64_t bytes, BdrvRequest= Flags flags) +{ + return bdrv_co_copy_range_to(src, src_offset, dst, dst_offset, bytes, = flags); +} + +static int coroutine_fn raw_co_copy_range_to(BlockDriverState *bs, + BdrvChild *src, uint64_t src_= offset, + BdrvChild *dst, uint64_t dst_= offset, + uint64_t bytes, BdrvRequestFl= ags flags) +{ + BDRVRawState *s =3D bs->opaque; + BDRVRawState *src_s; + + assert(dst->bs =3D=3D bs); + if (src->bs->drv->bdrv_co_copy_range_to !=3D raw_co_copy_range_to) { + return -ENOTSUP; + } + + src_s =3D src->bs->opaque; + if (fd_open(bs) < 0 || fd_open(bs) < 0) { + return -EIO; + } + return paio_submit_co_full(bs, src_s->fd, src_offset, s->fd, dst_offse= t, + NULL, bytes, QEMU_AIO_COPY_RANGE); +} + BlockDriver bdrv_file =3D { .format_name =3D "file", .protocol_name =3D "file", @@ -2474,6 +2562,8 @@ BlockDriver bdrv_file =3D { .bdrv_co_pwritev =3D raw_co_pwritev, .bdrv_aio_flush =3D raw_aio_flush, .bdrv_aio_pdiscard =3D raw_aio_pdiscard, + .bdrv_co_copy_range_from =3D raw_co_copy_range_from, + .bdrv_co_copy_range_to =3D raw_co_copy_range_to, .bdrv_refresh_limits =3D raw_refresh_limits, .bdrv_io_plug =3D raw_aio_plug, .bdrv_io_unplug =3D raw_aio_unplug, @@ -2952,6 +3042,8 @@ static BlockDriver bdrv_host_device =3D { .bdrv_co_pwritev =3D raw_co_pwritev, .bdrv_aio_flush =3D raw_aio_flush, .bdrv_aio_pdiscard =3D hdev_aio_pdiscard, + .bdrv_co_copy_range_from =3D raw_co_copy_range_from, + .bdrv_co_copy_range_to =3D raw_co_copy_range_to, .bdrv_refresh_limits =3D raw_refresh_limits, .bdrv_io_plug =3D raw_aio_plug, .bdrv_io_unplug =3D raw_aio_unplug, --=20 2.17.1