Received: from eggs.gnu.org ([2001:4830:134:3::10]:52553) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VgTyN-0007Mh-VV for qemu-devel@nongnu.org; Wed, 13 Nov 2013 01:30:50 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VgTyD-0007nB-4h for qemu-devel@nongnu.org; Wed, 13 Nov 2013 01:30:43 -0500 Received: from ssl.dlhnet.de ([91.198.192.8]:39380 helo=ssl.dlh.net) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VgTyC-0007n4-My for qemu-devel@nongnu.org; Wed, 13 Nov 2013 01:30:33 -0500 Content-Type: multipart/alternative; boundary="Apple-Mail=_D6A714C2-461D-464C-8530-997A2BE6AA24" Mime-Version: 1.0 (Mac OS X Mail 6.5 \(1508\)) From: Peter Lieven In-Reply-To: Date: Wed, 13 Nov 2013 07:30:45 +0100 Message-Id: <0256171F-2F02-486E-AC91-9724968919A7@kamp.de> References: <1384271389-20716-1-git-send-email-pbonzini@redhat.com> <1384271389-20716-9-git-send-email-pbonzini@redhat.com> Subject: Re: [Qemu-devel] [PATCH 08/11] raw-posix: implement write_zeroes with MAY_UNMAP for files List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Paolo Bonzini Cc: kwolf@redhat.com, qemu-devel@nongnu.org, stefanha@redhat.com --Apple-Mail=_D6A714C2-461D-464C-8530-997A2BE6AA24 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Am 13.11.2013 um 07:27 schrieb Peter Lieven : >=20 > Am 12.11.2013 um 16:49 schrieb Paolo Bonzini : >=20 >> Writing zeroes to a file can be done by punching a hole if MAY_UNMAP >> is set. >>=20 >> Note that in this case handle_aiocb_discard's ENOTSUP return code >> is not ignored, but makes the block layer fall back to the generic >> implementation. >>=20 >> Signed-off-by: Paolo Bonzini >> --- >> block/raw-posix.c | 64 = ++++++++++++++++++++++++++++++++++++++++++++++++++++++- >> trace-events | 1 + >> 2 files changed, 64 insertions(+), 1 deletion(-) >>=20 >> diff --git a/block/raw-posix.c b/block/raw-posix.c >> index 27fe47d..830e109 100644 >> --- a/block/raw-posix.c >> +++ b/block/raw-posix.c >> @@ -142,6 +142,7 @@ typedef struct BDRVRawState { >> bool is_xfs : 1; >> #endif >> bool has_discard : 1; >> + bool discard_zeroes : 1; >> } BDRVRawState; >>=20 >> typedef struct BDRVRawReopenState { >> @@ -283,6 +284,7 @@ static int raw_open_common(BlockDriverState *bs, = QDict *options, >> Error *local_err =3D NULL; >> const char *filename; >> int fd, ret; >> + struct stat st; >>=20 >> opts =3D qemu_opts_create_nofail(&raw_runtime_opts); >> qemu_opts_absorb_qdict(opts, options, &local_err); >> @@ -325,6 +327,15 @@ static int raw_open_common(BlockDriverState *bs, = QDict *options, >> #endif >>=20 >> s->has_discard =3D true; >> + >> + if (fstat(s->fd, &st) < 0) { >> + error_setg_errno(errp, errno, "Could not stat file"); >> + goto fail; >> + } >> + if (S_ISREG(st.st_mode)) { >> + s->discard_zeroes =3D true; >> + } >> + >> #ifdef CONFIG_XFS >> if (platform_test_xfs_fd(s->fd)) { >> s->is_xfs =3D true; >> @@ -788,6 +799,29 @@ static int aio_worker(void *arg) >> return ret; >> } >>=20 >> +static int paio_submit_co(BlockDriverState *bs, int fd, >> + int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, >> + int type) >> +{ >> + RawPosixAIOData *acb =3D g_slice_new(RawPosixAIOData); >> + ThreadPool *pool; >> + >> + acb->bs =3D bs; >> + acb->aio_type =3D type; >> + acb->aio_fildes =3D fd; >> + >> + if (qiov) { >> + acb->aio_iov =3D qiov->iov; >> + acb->aio_niov =3D qiov->niov; >> + } >> + acb->aio_nbytes =3D nb_sectors * 512; >> + acb->aio_offset =3D sector_num * 512; >> + >> + trace_paio_submit_co(sector_num, nb_sectors, type); >> + pool =3D aio_get_thread_pool(bdrv_get_aio_context(bs)); >> + return thread_pool_submit_co(pool, aio_worker, acb); >> +} >> + >> static BlockDriverAIOCB *paio_submit(BlockDriverState *bs, int fd, >> int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, >> BlockDriverCompletionFunc *cb, void *opaque, int type) >> @@ -1200,6 +1234,31 @@ static coroutine_fn BlockDriverAIOCB = *raw_aio_discard(BlockDriverState *bs, >> cb, opaque, QEMU_AIO_DISCARD); >> } >>=20 >> +static int coroutine_fn raw_co_write_zeroes( >> + BlockDriverState *bs, int64_t sector_num, >> + int nb_sectors, BdrvRequestFlags flags) >> +{ >> + BDRVRawState *s =3D bs->opaque; >> + >> + if (!(flags & BDRV_REQ_MAY_UNMAP)) { >> + return -ENOTSUP; >> + } >> + if (!s->discard_zeroes) { >> + return -ENOTSUP; >> + } >> + return paio_submit_co(bs, s->fd, sector_num, NULL, nb_sectors, >> + QEMU_AIO_DISCARD); >> +} >> + >> +static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) >> +{ >> + BDRVRawState *s =3D bs->opaque; >> + >> + bdi->unallocated_blocks_are_zero =3D s->discard_zeroes; >> + bdi->can_write_zeroes_with_unmap =3D s->discard_zeroes; >=20 > does BLKDISCARDZEROES ioctl guarantee that a device is > zero initialized or does it just guarantee that a discard may not > fail and that it reads as zeroes afterwards? Please ignore this. We are talking about a file here. Peter >=20 > Peter >=20 >> + return 0; >> +} >> + >> static QEMUOptionParameter raw_create_options[] =3D { >> { >> .name =3D BLOCK_OPT_SIZE, >> @@ -1223,6 +1282,7 @@ static BlockDriver bdrv_file =3D { >> .bdrv_create =3D raw_create, >> .bdrv_has_zero_init =3D bdrv_has_zero_init_1, >> .bdrv_co_get_block_status =3D raw_co_get_block_status, >> + .bdrv_co_write_zeroes =3D raw_co_write_zeroes, >>=20 >> .bdrv_aio_readv =3D raw_aio_readv, >> .bdrv_aio_writev =3D raw_aio_writev, >> @@ -1231,6 +1291,7 @@ static BlockDriver bdrv_file =3D { >>=20 >> .bdrv_truncate =3D raw_truncate, >> .bdrv_getlength =3D raw_getlength, >> + .bdrv_get_info =3D raw_get_info, >> .bdrv_get_allocated_file_size >> =3D raw_get_allocated_file_size, >>=20 >> @@ -1586,6 +1647,7 @@ static BlockDriver bdrv_host_device =3D { >>=20 >> .bdrv_truncate =3D raw_truncate, >> .bdrv_getlength =3D raw_getlength, >> + .bdrv_get_info =3D raw_get_info, >> .bdrv_get_allocated_file_size >> =3D raw_get_allocated_file_size, >>=20 >> @@ -1715,7 +1777,7 @@ static BlockDriver bdrv_host_floppy =3D { >> .bdrv_aio_flush =3D raw_aio_flush, >>=20 >> .bdrv_truncate =3D raw_truncate, >> - .bdrv_getlength =3D raw_getlength, >> + .bdrv_getlength =3D raw_getlength, >> .has_variable_length =3D true, >> .bdrv_get_allocated_file_size >> =3D raw_get_allocated_file_size, >> diff --git a/trace-events b/trace-events >> index 96b3974..995c84a 100644 >> --- a/trace-events >> +++ b/trace-events >> @@ -128,6 +128,7 @@ thread_pool_cancel(void *req, void *opaque) "req = %p opaque %p" >>=20 >> # block/raw-win32.c >> # block/raw-posix.c >> +paio_submit_co(int64_t sector_num, int nb_sectors, int type) = "sector_num %"PRId64" nb_sectors %d type %d" >> paio_submit(void *acb, void *opaque, int64_t sector_num, int = nb_sectors, int type) "acb %p opaque %p sector_num %"PRId64" nb_sectors = %d type %d" >>=20 >> # ioport.c >> --=20 >> 1.8.4.2 >>=20 >>=20 >=20 --Apple-Mail=_D6A714C2-461D-464C-8530-997A2BE6AA24 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=us-ascii pl@kamp.de>:

Am 12.11.2013 um = 16:49 schrieb Paolo Bonzini <pbonzini@redhat.com>:

Writing = zeroes to a file can be done by punching a hole if MAY_UNMAP
is = set.

Note that in this case handle_aiocb_discard's ENOTSUP return = code
is not ignored, but makes the block layer fall back to the = generic
implementation.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
= block/raw-posix.c | 64 = ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
trace-events =      |  1 +
2 files changed, 64 = insertions(+), 1 deletion(-)

diff --git a/block/raw-posix.c = b/block/raw-posix.c
index 27fe47d..830e109 100644
--- = a/block/raw-posix.c
+++ b/block/raw-posix.c
@@ -142,6 +142,7 @@ = typedef struct BDRVRawState {
    bool is_xfs : = 1;
#endif
    bool has_discard : 1;
+ =    bool discard_zeroes : 1;
} BDRVRawState;

= typedef struct BDRVRawReopenState {
@@ -283,6 +284,7 @@ static int = raw_open_common(BlockDriverState *bs, QDict *options,
=     Error *local_err =3D NULL;
=     const char *filename;
=     int fd, ret;
+    struct stat = st;

    opts =3D = qemu_opts_create_nofail(&raw_runtime_opts);
=     qemu_opts_absorb_qdict(opts, options, = &local_err);
@@ -325,6 +327,15 @@ static int = raw_open_common(BlockDriverState *bs, QDict *options,
#endif

=     s->has_discard =3D true;
+
+ =    if (fstat(s->fd, &st) < 0) {
+ =        error_setg_errno(errp, errno, = "Could not stat file");
+ =        goto fail;
+ =    }
+    if (S_ISREG(st.st_mode)) = {
+        s->discard_zeroes =3D = true;
+    }
+
#ifdef CONFIG_XFS
=     if (platform_test_xfs_fd(s->fd)) {
=         s->is_xfs =3D = true;
@@ -788,6 +799,29 @@ static int aio_worker(void *arg)
=     return ret;
}

+static int = paio_submit_co(BlockDriverState *bs, int fd,
+ =        int64_t sector_num, = QEMUIOVector *qiov, int nb_sectors,
+ =        int type)
+{
+ =    RawPosixAIOData *acb =3D = g_slice_new(RawPosixAIOData);
+    ThreadPool = *pool;
+
+    acb->bs =3D bs;
+ =    acb->aio_type =3D type;
+ =    acb->aio_fildes =3D fd;
+
+ =    if (qiov) {
+ =        acb->aio_iov =3D = qiov->iov;
+ =        acb->aio_niov =3D = qiov->niov;
+    }
+ =    acb->aio_nbytes =3D nb_sectors * 512;
+ =    acb->aio_offset =3D sector_num * 512;
+
+ =    trace_paio_submit_co(sector_num, nb_sectors, = type);
+    pool =3D = aio_get_thread_pool(bdrv_get_aio_context(bs));
+ =    return thread_pool_submit_co(pool, aio_worker, = acb);
+}
+
static BlockDriverAIOCB = *paio_submit(BlockDriverState *bs, int fd,
=         int64_t sector_num, = QEMUIOVector *qiov, int nb_sectors,
=         BlockDriverCompletionFunc = *cb, void *opaque, int type)
@@ -1200,6 +1234,31 @@ static = coroutine_fn BlockDriverAIOCB *raw_aio_discard(BlockDriverState *bs,
=             &n= bsp;          cb, = opaque, QEMU_AIO_DISCARD);
}

+static int coroutine_fn = raw_co_write_zeroes(
+    BlockDriverState *bs, = int64_t sector_num,
+    int nb_sectors, = BdrvRequestFlags flags)
+{
+    BDRVRawState *s =3D = bs->opaque;
+
+    if (!(flags & = BDRV_REQ_MAY_UNMAP)) {
+ =        return -ENOTSUP;
+ =    }
+    if (!s->discard_zeroes) = {
+        return -ENOTSUP;
+ =    }
+    return paio_submit_co(bs, = s->fd, sector_num, NULL, nb_sectors,
+ =             &n= bsp;           &nbs= p;QEMU_AIO_DISCARD);
+}
+
+static int = raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
+{
+ =    BDRVRawState *s =3D bs->opaque;
+
+ =    bdi->unallocated_blocks_are_zero =3D = s->discard_zeroes;
+ =    bdi->can_write_zeroes_with_unmap =3D = s->discard_zeroes;

does zero initialized or does it just guarantee = that a discard may not
fail and that it = reads as zeroes = afterwards?

Pleas= e ignore this. We are talking about a file = here.

Peter

+ =    return 0;
+}
+
static QEMUOptionParameter = raw_create_options[] =3D {
    {
=         .name =3D = BLOCK_OPT_SIZE,
@@ -1223,6 +1282,7 @@ static BlockDriver bdrv_file =3D = {
    .bdrv_create =3D raw_create,
=     .bdrv_has_zero_init =3D = bdrv_has_zero_init_1,
=     .bdrv_co_get_block_status =3D = raw_co_get_block_status,
+    .bdrv_co_write_zeroes =3D = raw_co_write_zeroes,

    .bdrv_aio_readv =3D = raw_aio_readv,
    .bdrv_aio_writev =3D = raw_aio_writev,
@@ -1231,6 +1291,7 @@ static BlockDriver bdrv_file =3D = {

    .bdrv_truncate =3D raw_truncate,
=     .bdrv_getlength =3D raw_getlength,
+ =    .bdrv_get_info =3D raw_get_info,
=     .bdrv_get_allocated_file_size
=             &n= bsp;           =3D = raw_get_allocated_file_size,

@@ -1586,6 +1647,7 @@ static = BlockDriver bdrv_host_device =3D {

=     .bdrv_truncate      =3D = raw_truncate,
    .bdrv_getlength =3D = raw_getlength,
+    .bdrv_get_info =3D = raw_get_info,
=     .bdrv_get_allocated_file_size
=             &n= bsp;           =3D = raw_get_allocated_file_size,

@@ -1715,7 +1777,7 @@ static = BlockDriver bdrv_host_floppy =3D {
=     .bdrv_aio_flush =3D raw_aio_flush,

=     .bdrv_truncate      =3D = raw_truncate,
-    .bdrv_getlength =      =3D raw_getlength,
+ =    .bdrv_getlength     =3D = raw_getlength,
    .has_variable_length =3D = true,
    .bdrv_get_allocated_file_size
=             &n= bsp;           =3D = raw_get_allocated_file_size,
diff --git a/trace-events = b/trace-events
index 96b3974..995c84a 100644
--- = a/trace-events
+++ b/trace-events
@@ -128,6 +128,7 @@ = thread_pool_cancel(void *req, void *opaque) "req %p opaque %p"

# = block/raw-win32.c
# block/raw-posix.c
+paio_submit_co(int64_t = sector_num, int nb_sectors, int type) "sector_num %"PRId64" nb_sectors = %d type %d"
paio_submit(void *acb, void *opaque, int64_t sector_num, = int nb_sectors, int type) "acb %p opaque %p sector_num %"PRId64" = nb_sectors %d type %d"

# ioport.c
-- =
1.8.4.2



= --Apple-Mail=_D6A714C2-461D-464C-8530-997A2BE6AA24--