0a122b
From 4a5025f224c53d2194e2f62cb730a1fef961ff45 Mon Sep 17 00:00:00 2001
0a122b
Message-Id: <4a5025f224c53d2194e2f62cb730a1fef961ff45.1389014116.git.minovotn@redhat.com>
0a122b
In-Reply-To: <c8cc35838d42aa286242772d97e3a9be7bb786ba.1389014116.git.minovotn@redhat.com>
0a122b
References: <c8cc35838d42aa286242772d97e3a9be7bb786ba.1389014116.git.minovotn@redhat.com>
0a122b
From: Paolo Bonzini <pbonzini@redhat.com>
0a122b
Date: Mon, 9 Dec 2013 14:09:27 +0100
0a122b
Subject: [PATCH 39/50] raw-posix: implement write_zeroes with MAY_UNMAP for
0a122b
 files
0a122b
0a122b
RH-Author: Paolo Bonzini <pbonzini@redhat.com>
0a122b
Message-id: <1386598178-11845-42-git-send-email-pbonzini@redhat.com>
0a122b
Patchwork-id: 56078
0a122b
O-Subject: [RHEL 7.0 qemu-kvm PATCH 41/52] raw-posix: implement write_zeroes with MAY_UNMAP for files
0a122b
Bugzilla: 1007815
0a122b
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
0a122b
RH-Acked-by: Fam Zheng <famz@redhat.com>
0a122b
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
0a122b
0a122b
Writing zeroes to a file can be done by punching a hole if
0a122b
MAY_UNMAP is set.
0a122b
0a122b
Note that in this case ENOTSUP is not ignored, but makes
0a122b
the block layer fall back to the generic implementation.
0a122b
0a122b
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
0a122b
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
0a122b
(cherry picked from commit 260a82e524b7f86c12b8e39d4c3f208af95645f7)
0a122b
---
0a122b
 block/raw-posix.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++--
0a122b
 trace-events      |  1 +
0a122b
 2 files changed, 65 insertions(+), 2 deletions(-)
0a122b
0a122b
Signed-off-by: Michal Novotny <minovotn@redhat.com>
0a122b
---
0a122b
 block/raw-posix.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++--
0a122b
 trace-events      |  1 +
0a122b
 2 files changed, 65 insertions(+), 2 deletions(-)
0a122b
0a122b
diff --git a/block/raw-posix.c b/block/raw-posix.c
0a122b
index 7a140b0..ca5bcb3 100644
0a122b
--- a/block/raw-posix.c
0a122b
+++ b/block/raw-posix.c
0a122b
@@ -139,9 +139,10 @@ typedef struct BDRVRawState {
0a122b
     void *aio_ctx;
0a122b
 #endif
0a122b
 #ifdef CONFIG_XFS
0a122b
-    bool is_xfs : 1;
0a122b
+    bool is_xfs:1;
0a122b
 #endif
0a122b
-    bool has_discard : 1;
0a122b
+    bool has_discard:1;
0a122b
+    bool discard_zeroes:1;
0a122b
 } BDRVRawState;
0a122b
 
0a122b
 typedef struct BDRVRawReopenState {
0a122b
@@ -283,6 +284,7 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
0a122b
     Error *local_err = NULL;
0a122b
     const char *filename;
0a122b
     int fd, ret;
0a122b
+    struct stat st;
0a122b
 
0a122b
     opts = qemu_opts_create_nofail(&raw_runtime_opts);
0a122b
     qemu_opts_absorb_qdict(opts, options, &local_err);
0a122b
@@ -325,6 +327,15 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
0a122b
 #endif
0a122b
 
0a122b
     s->has_discard = true;
0a122b
+
0a122b
+    if (fstat(s->fd, &st) < 0) {
0a122b
+        error_setg_errno(errp, errno, "Could not stat file");
0a122b
+        goto fail;
0a122b
+    }
0a122b
+    if (S_ISREG(st.st_mode)) {
0a122b
+        s->discard_zeroes = true;
0a122b
+    }
0a122b
+
0a122b
 #ifdef CONFIG_XFS
0a122b
     if (platform_test_xfs_fd(s->fd)) {
0a122b
         s->is_xfs = true;
0a122b
@@ -788,6 +799,29 @@ static int aio_worker(void *arg)
0a122b
     return ret;
0a122b
 }
0a122b
 
0a122b
+static int paio_submit_co(BlockDriverState *bs, int fd,
0a122b
+        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
0a122b
+        int type)
0a122b
+{
0a122b
+    RawPosixAIOData *acb = g_slice_new(RawPosixAIOData);
0a122b
+    ThreadPool *pool;
0a122b
+
0a122b
+    acb->bs = bs;
0a122b
+    acb->aio_type = type;
0a122b
+    acb->aio_fildes = fd;
0a122b
+
0a122b
+    if (qiov) {
0a122b
+        acb->aio_iov = qiov->iov;
0a122b
+        acb->aio_niov = qiov->niov;
0a122b
+    }
0a122b
+    acb->aio_nbytes = nb_sectors * 512;
0a122b
+    acb->aio_offset = sector_num * 512;
0a122b
+
0a122b
+    trace_paio_submit_co(sector_num, nb_sectors, type);
0a122b
+    pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
0a122b
+    return thread_pool_submit_co(pool, aio_worker, acb);
0a122b
+}
0a122b
+
0a122b
 static BlockDriverAIOCB *paio_submit(BlockDriverState *bs, int fd,
0a122b
         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
0a122b
         BlockDriverCompletionFunc *cb, void *opaque, int type)
0a122b
@@ -1200,6 +1234,31 @@ static coroutine_fn BlockDriverAIOCB *raw_aio_discard(BlockDriverState *bs,
0a122b
                        cb, opaque, QEMU_AIO_DISCARD);
0a122b
 }
0a122b
 
0a122b
+static int coroutine_fn raw_co_write_zeroes(
0a122b
+    BlockDriverState *bs, int64_t sector_num,
0a122b
+    int nb_sectors, BdrvRequestFlags flags)
0a122b
+{
0a122b
+    BDRVRawState *s = bs->opaque;
0a122b
+
0a122b
+    if (!(flags & BDRV_REQ_MAY_UNMAP)) {
0a122b
+        return -ENOTSUP;
0a122b
+    }
0a122b
+    if (!s->discard_zeroes) {
0a122b
+        return -ENOTSUP;
0a122b
+    }
0a122b
+    return paio_submit_co(bs, s->fd, sector_num, NULL, nb_sectors,
0a122b
+                          QEMU_AIO_DISCARD);
0a122b
+}
0a122b
+
0a122b
+static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
0a122b
+{
0a122b
+    BDRVRawState *s = bs->opaque;
0a122b
+
0a122b
+    bdi->unallocated_blocks_are_zero = s->discard_zeroes;
0a122b
+    bdi->can_write_zeroes_with_unmap = s->discard_zeroes;
0a122b
+    return 0;
0a122b
+}
0a122b
+
0a122b
 static QEMUOptionParameter raw_create_options[] = {
0a122b
     {
0a122b
         .name = BLOCK_OPT_SIZE,
0a122b
@@ -1222,6 +1281,7 @@ static BlockDriver bdrv_file = {
0a122b
     .bdrv_create = raw_create,
0a122b
     .bdrv_has_zero_init = bdrv_has_zero_init_1,
0a122b
     .bdrv_co_get_block_status = raw_co_get_block_status,
0a122b
+    .bdrv_co_write_zeroes = raw_co_write_zeroes,
0a122b
 
0a122b
     .bdrv_aio_readv = raw_aio_readv,
0a122b
     .bdrv_aio_writev = raw_aio_writev,
0a122b
@@ -1230,6 +1290,7 @@ static BlockDriver bdrv_file = {
0a122b
 
0a122b
     .bdrv_truncate = raw_truncate,
0a122b
     .bdrv_getlength = raw_getlength,
0a122b
+    .bdrv_get_info = raw_get_info,
0a122b
     .bdrv_get_allocated_file_size
0a122b
                         = raw_get_allocated_file_size,
0a122b
 
0a122b
@@ -1584,6 +1645,7 @@ static BlockDriver bdrv_host_device = {
0a122b
 
0a122b
     .bdrv_truncate      = raw_truncate,
0a122b
     .bdrv_getlength	= raw_getlength,
0a122b
+    .bdrv_get_info = raw_get_info,
0a122b
     .bdrv_get_allocated_file_size
0a122b
                         = raw_get_allocated_file_size,
0a122b
 
0a122b
diff --git a/trace-events b/trace-events
0a122b
index 40d4312..e9ee94f 100644
0a122b
--- a/trace-events
0a122b
+++ b/trace-events
0a122b
@@ -120,6 +120,7 @@ thread_pool_cancel(void *req, void *opaque) "req %p opaque %p"
0a122b
 
0a122b
 # block/raw-win32.c
0a122b
 # block/raw-posix.c
0a122b
+paio_submit_co(int64_t sector_num, int nb_sectors, int type) "sector_num %"PRId64" nb_sectors %d type %d"
0a122b
 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"
0a122b
 
0a122b
 # ioport.c
0a122b
-- 
0a122b
1.7.11.7
0a122b