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

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