Blame SOURCES/kvm-raw-posix-add-support-for-write_zeroes-on-XFS-and-bl.patch.patch

0a122b
From adf32529b8b95ed360356f13bd9a7f2a4b707412 Mon Sep 17 00:00:00 2001
0a122b
Message-Id: <adf32529b8b95ed360356f13bd9a7f2a4b707412.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:29 +0100
0a122b
Subject: [PATCH 41/50] raw-posix: add support for write_zeroes on XFS and
0a122b
 block devices
0a122b
0a122b
RH-Author: Paolo Bonzini <pbonzini@redhat.com>
0a122b
Message-id: <1386598178-11845-44-git-send-email-pbonzini@redhat.com>
0a122b
Patchwork-id: 56080
0a122b
O-Subject: [RHEL 7.0 qemu-kvm PATCH 43/52] raw-posix: add support for write_zeroes on XFS and block devices
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
The code is similar to the implementation of discard and write_zeroes
0a122b
with UNMAP.  However, failure must be propagated up to block.c.
0a122b
0a122b
The stale page cache problem can be reproduced as follows:
0a122b
0a122b
    # modprobe scsi-debug lbpws=1 lbprz=1
0a122b
    # ./qemu-io /dev/sdXX
0a122b
    qemu-io> write -P 0xcc 0 2M
0a122b
    qemu-io> write -z 0 1M
0a122b
    qemu-io> read -P 0x00 0 512
0a122b
    Pattern verification failed at offset 0, 512 bytes
0a122b
    qemu-io> read -v 0 512
0a122b
    00000000:  cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc cc  ................
0a122b
    ...
0a122b
0a122b
    # ./qemu-io --cache=none /dev/sdXX
0a122b
    qemu-io> write -P 0xcc 0 2M
0a122b
    qemu-io> write -z 0 1M
0a122b
    qemu-io> read -P 0x00 0 512
0a122b
    qemu-io> read -v 0 512
0a122b
    00000000:  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0a122b
    ...
0a122b
0a122b
And similarly with discard instead of "write -z".
0a122b
0a122b
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
0a122b
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
0a122b
(cherry picked from commit 97a2ae34537882df34810d538ab1f51085499d2c)
0a122b
---
0a122b
 block/raw-aio.h   |  3 +-
0a122b
 block/raw-posix.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++--------
0a122b
 2 files changed, 74 insertions(+), 13 deletions(-)
0a122b
0a122b
Signed-off-by: Michal Novotny <minovotn@redhat.com>
0a122b
---
0a122b
 block/raw-aio.h   |  3 +-
0a122b
 block/raw-posix.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++--------
0a122b
 2 files changed, 74 insertions(+), 13 deletions(-)
0a122b
0a122b
diff --git a/block/raw-aio.h b/block/raw-aio.h
0a122b
index c61f159..7ad0a8a 100644
0a122b
--- a/block/raw-aio.h
0a122b
+++ b/block/raw-aio.h
0a122b
@@ -21,9 +21,10 @@
0a122b
 #define QEMU_AIO_IOCTL        0x0004
0a122b
 #define QEMU_AIO_FLUSH        0x0008
0a122b
 #define QEMU_AIO_DISCARD      0x0010
0a122b
+#define QEMU_AIO_WRITE_ZEROES 0x0020
0a122b
 #define QEMU_AIO_TYPE_MASK \
0a122b
         (QEMU_AIO_READ|QEMU_AIO_WRITE|QEMU_AIO_IOCTL|QEMU_AIO_FLUSH| \
0a122b
-         QEMU_AIO_DISCARD)
0a122b
+         QEMU_AIO_DISCARD|QEMU_AIO_WRITE_ZEROES)
0a122b
 
0a122b
 /* AIO flags */
0a122b
 #define QEMU_AIO_MISALIGNED   0x1000
0a122b
diff --git a/block/raw-posix.c b/block/raw-posix.c
0a122b
index 815a80b..f410668 100644
0a122b
--- a/block/raw-posix.c
0a122b
+++ b/block/raw-posix.c
0a122b
@@ -142,6 +142,7 @@ typedef struct BDRVRawState {
0a122b
     bool is_xfs:1;
0a122b
 #endif
0a122b
     bool has_discard:1;
0a122b
+    bool has_write_zeroes:1;
0a122b
     bool discard_zeroes:1;
0a122b
 } BDRVRawState;
0a122b
 
0a122b
@@ -327,6 +328,7 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
0a122b
 #endif
0a122b
 
0a122b
     s->has_discard = true;
0a122b
+    s->has_write_zeroes = true;
0a122b
 
0a122b
     if (fstat(s->fd, &st) < 0) {
0a122b
         error_setg_errno(errp, errno, "Could not stat file");
0a122b
@@ -345,9 +347,11 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
0a122b
 #ifdef __linux__
0a122b
         /* On Linux 3.10, BLKDISCARD leaves stale data in the page cache.  Do
0a122b
          * not rely on the contents of discarded blocks unless using O_DIRECT.
0a122b
+         * Same for BLKZEROOUT.
0a122b
          */
0a122b
         if (!(bs->open_flags & BDRV_O_NOCACHE)) {
0a122b
             s->discard_zeroes = false;
0a122b
+            s->has_write_zeroes = false;
0a122b
         }
0a122b
 #endif
0a122b
     }
0a122b
@@ -703,6 +707,23 @@ static ssize_t handle_aiocb_rw(RawPosixAIOData *aiocb)
0a122b
 }
0a122b
 
0a122b
 #ifdef CONFIG_XFS
0a122b
+static int xfs_write_zeroes(BDRVRawState *s, int64_t offset, uint64_t bytes)
0a122b
+{
0a122b
+    struct xfs_flock64 fl;
0a122b
+
0a122b
+    memset(&fl, 0, sizeof(fl));
0a122b
+    fl.l_whence = SEEK_SET;
0a122b
+    fl.l_start = offset;
0a122b
+    fl.l_len = bytes;
0a122b
+
0a122b
+    if (xfsctl(NULL, s->fd, XFS_IOC_ZERO_RANGE, &fl) < 0) {
0a122b
+        DEBUG_BLOCK_PRINT("cannot write zero range (%s)\n", strerror(errno));
0a122b
+        return -errno;
0a122b
+    }
0a122b
+
0a122b
+    return 0;
0a122b
+}
0a122b
+
0a122b
 static int xfs_discard(BDRVRawState *s, int64_t offset, uint64_t bytes)
0a122b
 {
0a122b
     struct xfs_flock64 fl;
0a122b
@@ -721,6 +742,42 @@ static int xfs_discard(BDRVRawState *s, int64_t offset, uint64_t bytes)
0a122b
 }
0a122b
 #endif
0a122b
 
0a122b
+static ssize_t handle_aiocb_write_zeroes(RawPosixAIOData *aiocb)
0a122b
+{
0a122b
+    int ret = -EOPNOTSUPP;
0a122b
+    BDRVRawState *s = aiocb->bs->opaque;
0a122b
+
0a122b
+    if (s->has_write_zeroes == 0) {
0a122b
+        return -ENOTSUP;
0a122b
+    }
0a122b
+
0a122b
+    if (aiocb->aio_type & QEMU_AIO_BLKDEV) {
0a122b
+#ifdef BLKZEROOUT
0a122b
+        do {
0a122b
+            uint64_t range[2] = { aiocb->aio_offset, aiocb->aio_nbytes };
0a122b
+            if (ioctl(aiocb->aio_fildes, BLKZEROOUT, range) == 0) {
0a122b
+                return 0;
0a122b
+            }
0a122b
+        } while (errno == EINTR);
0a122b
+
0a122b
+        ret = -errno;
0a122b
+#endif
0a122b
+    } else {
0a122b
+#ifdef CONFIG_XFS
0a122b
+        if (s->is_xfs) {
0a122b
+            return xfs_write_zeroes(s, aiocb->aio_offset, aiocb->aio_nbytes);
0a122b
+        }
0a122b
+#endif
0a122b
+    }
0a122b
+
0a122b
+    if (ret == -ENODEV || ret == -ENOSYS || ret == -EOPNOTSUPP ||
0a122b
+        ret == -ENOTTY) {
0a122b
+        s->has_write_zeroes = false;
0a122b
+        ret = -ENOTSUP;
0a122b
+    }
0a122b
+    return ret;
0a122b
+}
0a122b
+
0a122b
 static ssize_t handle_aiocb_discard(RawPosixAIOData *aiocb)
0a122b
 {
0a122b
     int ret = -EOPNOTSUPP;
0a122b
@@ -805,6 +862,9 @@ static int aio_worker(void *arg)
0a122b
     case QEMU_AIO_DISCARD:
0a122b
         ret = handle_aiocb_discard(aiocb);
0a122b
         break;
0a122b
+    case QEMU_AIO_WRITE_ZEROES:
0a122b
+        ret = handle_aiocb_write_zeroes(aiocb);
0a122b
+        break;
0a122b
     default:
0a122b
         fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
0a122b
         ret = -EINVAL;
0a122b
@@ -1257,13 +1317,13 @@ static int coroutine_fn raw_co_write_zeroes(
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
+        return paio_submit_co(bs, s->fd, sector_num, NULL, nb_sectors,
0a122b
+                              QEMU_AIO_WRITE_ZEROES);
0a122b
+    } else if (s->discard_zeroes) {
0a122b
+        return paio_submit_co(bs, s->fd, sector_num, NULL, nb_sectors,
0a122b
+                              QEMU_AIO_DISCARD);
0a122b
     }
0a122b
-    return paio_submit_co(bs, s->fd, sector_num, NULL, nb_sectors,
0a122b
-                          QEMU_AIO_DISCARD);
0a122b
+    return -ENOTSUP;
0a122b
 }
0a122b
 
0a122b
 static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
0a122b
@@ -1613,13 +1673,13 @@ static coroutine_fn int hdev_co_write_zeroes(BlockDriverState *bs,
0a122b
         return rc;
0a122b
     }
0a122b
     if (!(flags & BDRV_REQ_MAY_UNMAP)) {
0a122b
-        return -ENOTSUP;
0a122b
-    }
0a122b
-    if (!s->discard_zeroes) {
0a122b
-        return -ENOTSUP;
0a122b
+        return paio_submit_co(bs, s->fd, sector_num, NULL, nb_sectors,
0a122b
+                              QEMU_AIO_WRITE_ZEROES|QEMU_AIO_BLKDEV);
0a122b
+    } else if (s->discard_zeroes) {
0a122b
+        return paio_submit_co(bs, s->fd, sector_num, NULL, nb_sectors,
0a122b
+                              QEMU_AIO_DISCARD|QEMU_AIO_BLKDEV);
0a122b
     }
0a122b
-    return paio_submit_co(bs, s->fd, sector_num, NULL, nb_sectors,
0a122b
-                          QEMU_AIO_DISCARD|QEMU_AIO_BLKDEV);
0a122b
+    return -ENOTSUP;
0a122b
 }
0a122b
 
0a122b
 static int hdev_create(const char *filename, QEMUOptionParameter *options,
0a122b
-- 
0a122b
1.7.11.7
0a122b