Blame SOURCES/kvm-qcow2-Support-BDRV_REQ_ZERO_WRITE-for-truncate.patch

77c23f
From 3e603e344b81b3ecfea6fb9589ba91f70a22139d Mon Sep 17 00:00:00 2001
77c23f
From: Kevin Wolf <kwolf@redhat.com>
77c23f
Date: Mon, 8 Jun 2020 15:01:33 +0100
77c23f
Subject: [PATCH 05/17] qcow2: Support BDRV_REQ_ZERO_WRITE for truncate
77c23f
77c23f
RH-Author: Kevin Wolf <kwolf@redhat.com>
77c23f
Message-id: <20200608150140.38218-5-kwolf@redhat.com>
77c23f
Patchwork-id: 97449
77c23f
O-Subject: [RHEL-AV-8.2.1 qemu-kvm PATCH 04/11] qcow2: Support BDRV_REQ_ZERO_WRITE for truncate
77c23f
Bugzilla: 1780574
77c23f
RH-Acked-by: Sergio Lopez Pascual <slp@redhat.com>
77c23f
RH-Acked-by: Eric Blake <eblake@redhat.com>
77c23f
RH-Acked-by: Max Reitz <mreitz@redhat.com>
77c23f
77c23f
If BDRV_REQ_ZERO_WRITE is set and we're extending the image, calling
77c23f
qcow2_cluster_zeroize() with flags=0 does the right thing: It doesn't
77c23f
undo any previous preallocation, but just adds the zero flag to all
77c23f
relevant L2 entries. If an external data file is in use, a write_zeroes
77c23f
request to the data file is made instead.
77c23f
77c23f
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
77c23f
Message-Id: <20200424125448.63318-5-kwolf@redhat.com>
77c23f
Reviewed-by: Eric Blake <eblake@redhat.com>
77c23f
Reviewed-by: Max Reitz <mreitz@redhat.com>
77c23f
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
77c23f
(cherry picked from commit f01643fb8b47e8a70c04bbf45e0f12a9e5bc54de)
77c23f
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
77c23f
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
77c23f
---
77c23f
 block/qcow2-cluster.c |  2 +-
77c23f
 block/qcow2.c         | 34 ++++++++++++++++++++++++++++++++++
77c23f
 2 files changed, 35 insertions(+), 1 deletion(-)
77c23f
77c23f
diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c
77c23f
index dc3c270..9d04f8d 100644
77c23f
--- a/block/qcow2-cluster.c
77c23f
+++ b/block/qcow2-cluster.c
77c23f
@@ -1784,7 +1784,7 @@ int qcow2_cluster_zeroize(BlockDriverState *bs, uint64_t offset,
77c23f
     /* Caller must pass aligned values, except at image end */
77c23f
     assert(QEMU_IS_ALIGNED(offset, s->cluster_size));
77c23f
     assert(QEMU_IS_ALIGNED(end_offset, s->cluster_size) ||
77c23f
-           end_offset == bs->total_sectors << BDRV_SECTOR_BITS);
77c23f
+           end_offset >= bs->total_sectors << BDRV_SECTOR_BITS);
77c23f
 
77c23f
     /* The zero flag is only supported by version 3 and newer */
77c23f
     if (s->qcow_version < 3) {
77c23f
diff --git a/block/qcow2.c b/block/qcow2.c
77c23f
index 86aa74a..f3d6cb0 100644
77c23f
--- a/block/qcow2.c
77c23f
+++ b/block/qcow2.c
77c23f
@@ -1726,6 +1726,7 @@ static int coroutine_fn qcow2_do_open(BlockDriverState *bs, QDict *options,
77c23f
     }
77c23f
 
77c23f
     bs->supported_zero_flags = header.version >= 3 ? BDRV_REQ_MAY_UNMAP : 0;
77c23f
+    bs->supported_truncate_flags = BDRV_REQ_ZERO_WRITE;
77c23f
 
77c23f
     /* Repair image if dirty */
77c23f
     if (!(flags & (BDRV_O_CHECK | BDRV_O_INACTIVE)) && !bs->read_only &&
77c23f
@@ -4197,6 +4198,39 @@ static int coroutine_fn qcow2_co_truncate(BlockDriverState *bs, int64_t offset,
77c23f
         g_assert_not_reached();
77c23f
     }
77c23f
 
77c23f
+    if ((flags & BDRV_REQ_ZERO_WRITE) && offset > old_length) {
77c23f
+        uint64_t zero_start = QEMU_ALIGN_UP(old_length, s->cluster_size);
77c23f
+
77c23f
+        /*
77c23f
+         * Use zero clusters as much as we can. qcow2_cluster_zeroize()
77c23f
+         * requires a cluster-aligned start. The end may be unaligned if it is
77c23f
+         * at the end of the image (which it is here).
77c23f
+         */
77c23f
+        ret = qcow2_cluster_zeroize(bs, zero_start, offset - zero_start, 0);
77c23f
+        if (ret < 0) {
77c23f
+            error_setg_errno(errp, -ret, "Failed to zero out new clusters");
77c23f
+            goto fail;
77c23f
+        }
77c23f
+
77c23f
+        /* Write explicit zeros for the unaligned head */
77c23f
+        if (zero_start > old_length) {
77c23f
+            uint64_t len = zero_start - old_length;
77c23f
+            uint8_t *buf = qemu_blockalign0(bs, len);
77c23f
+            QEMUIOVector qiov;
77c23f
+            qemu_iovec_init_buf(&qiov, buf, len);
77c23f
+
77c23f
+            qemu_co_mutex_unlock(&s->lock);
77c23f
+            ret = qcow2_co_pwritev_part(bs, old_length, len, &qiov, 0, 0);
77c23f
+            qemu_co_mutex_lock(&s->lock);
77c23f
+
77c23f
+            qemu_vfree(buf);
77c23f
+            if (ret < 0) {
77c23f
+                error_setg_errno(errp, -ret, "Failed to zero out the new area");
77c23f
+                goto fail;
77c23f
+            }
77c23f
+        }
77c23f
+    }
77c23f
+
77c23f
     if (prealloc != PREALLOC_MODE_OFF) {
77c23f
         /* Flush metadata before actually changing the image size */
77c23f
         ret = qcow2_write_caches(bs);
77c23f
-- 
77c23f
1.8.3.1
77c23f