9ae3a8
From f1b8961cbef828b62ba1ca7da79cd516f7dfc52b Mon Sep 17 00:00:00 2001
9ae3a8
Message-Id: <f1b8961cbef828b62ba1ca7da79cd516f7dfc52b.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:28 +0100
9ae3a8
Subject: [PATCH 40/50] raw-posix: implement write_zeroes with MAY_UNMAP for
9ae3a8
 block devices
9ae3a8
9ae3a8
RH-Author: Paolo Bonzini <pbonzini@redhat.com>
9ae3a8
Message-id: <1386598178-11845-43-git-send-email-pbonzini@redhat.com>
9ae3a8
Patchwork-id: 56079
9ae3a8
O-Subject: [RHEL 7.0 qemu-kvm PATCH 42/52] raw-posix: implement write_zeroes with MAY_UNMAP for block devices
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
See the next commit for the description of the Linux kernel problem
9ae3a8
that is worked around in raw_open_common.
9ae3a8
9ae3a8
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
9ae3a8
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
(cherry picked from commit d0b4503ed2d8713791c38839341b023f78d1a3d9)
9ae3a8
---
9ae3a8
 block/raw-posix.c | 37 +++++++++++++++++++++++++++++++++++++
9ae3a8
 1 file changed, 37 insertions(+)
9ae3a8
9ae3a8
Signed-off-by: Michal Novotny <minovotn@redhat.com>
9ae3a8
---
9ae3a8
 block/raw-posix.c | 37 +++++++++++++++++++++++++++++++++++++
9ae3a8
 1 file changed, 37 insertions(+)
9ae3a8
9ae3a8
diff --git a/block/raw-posix.c b/block/raw-posix.c
9ae3a8
index ca5bcb3..815a80b 100644
9ae3a8
--- a/block/raw-posix.c
9ae3a8
+++ b/block/raw-posix.c
9ae3a8
@@ -335,6 +335,22 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
9ae3a8
     if (S_ISREG(st.st_mode)) {
9ae3a8
         s->discard_zeroes = true;
9ae3a8
     }
9ae3a8
+    if (S_ISBLK(st.st_mode)) {
9ae3a8
+#ifdef BLKDISCARDZEROES
9ae3a8
+        unsigned int arg;
9ae3a8
+        if (ioctl(s->fd, BLKDISCARDZEROES, &arg) == 0 && arg) {
9ae3a8
+            s->discard_zeroes = true;
9ae3a8
+        }
9ae3a8
+#endif
9ae3a8
+#ifdef __linux__
9ae3a8
+        /* On Linux 3.10, BLKDISCARD leaves stale data in the page cache.  Do
9ae3a8
+         * not rely on the contents of discarded blocks unless using O_DIRECT.
9ae3a8
+         */
9ae3a8
+        if (!(bs->open_flags & BDRV_O_NOCACHE)) {
9ae3a8
+            s->discard_zeroes = false;
9ae3a8
+        }
9ae3a8
+#endif
9ae3a8
+    }
9ae3a8
 
9ae3a8
 #ifdef CONFIG_XFS
9ae3a8
     if (platform_test_xfs_fd(s->fd)) {
9ae3a8
@@ -1586,6 +1602,26 @@ static coroutine_fn BlockDriverAIOCB *hdev_aio_discard(BlockDriverState *bs,
9ae3a8
                        cb, opaque, QEMU_AIO_DISCARD|QEMU_AIO_BLKDEV);
9ae3a8
 }
9ae3a8
 
9ae3a8
+static coroutine_fn int hdev_co_write_zeroes(BlockDriverState *bs,
9ae3a8
+    int64_t sector_num, int nb_sectors, BdrvRequestFlags flags)
9ae3a8
+{
9ae3a8
+    BDRVRawState *s = bs->opaque;
9ae3a8
+    int rc;
9ae3a8
+
9ae3a8
+    rc = fd_open(bs);
9ae3a8
+    if (rc < 0) {
9ae3a8
+        return rc;
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|QEMU_AIO_BLKDEV);
9ae3a8
+}
9ae3a8
+
9ae3a8
 static int hdev_create(const char *filename, QEMUOptionParameter *options,
9ae3a8
                        Error **errp)
9ae3a8
 {
9ae3a8
@@ -1637,6 +1673,7 @@ static BlockDriver bdrv_host_device = {
9ae3a8
     .bdrv_reopen_abort   = raw_reopen_abort,
9ae3a8
     .bdrv_create        = hdev_create,
9ae3a8
     .create_options     = raw_create_options,
9ae3a8
+    .bdrv_co_write_zeroes = hdev_co_write_zeroes,
9ae3a8
 
9ae3a8
     .bdrv_aio_readv	= raw_aio_readv,
9ae3a8
     .bdrv_aio_writev	= raw_aio_writev,
9ae3a8
-- 
9ae3a8
1.7.11.7
9ae3a8