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