qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: qemu-block@nongnu.org
Cc: kwolf@redhat.com, vsementsov@virtuozzo.com, berto@igalia.com,
	qemu-devel@nongnu.org, mreitz@redhat.com
Subject: [PATCH v5 4/9] qcow2: Support BDRV_REQ_ZERO_WRITE for truncate
Date: Wed, 22 Apr 2020 17:21:24 +0200	[thread overview]
Message-ID: <20200422152129.167074-5-kwolf@redhat.com> (raw)
In-Reply-To: <20200422152129.167074-1-kwolf@redhat.com>

If BDRV_REQ_ZERO_WRITE is set and we're extending the image, calling
qcow2_cluster_zeroize() with flags=0 does the right thing: It doesn't
undo any previous preallocation, but just adds the zero flag to all
relevant L2 entries. If an external data file is in use, a write_zeroes
request to the data file is made instead.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 block/qcow2.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/block/qcow2.c b/block/qcow2.c
index 9cfbdfc939..bd632405d1 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -1726,6 +1726,7 @@ static int coroutine_fn qcow2_do_open(BlockDriverState *bs, QDict *options,
 
     bs->supported_zero_flags = header.version >= 3 ?
                                BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK : 0;
+    bs->supported_truncate_flags = BDRV_REQ_ZERO_WRITE;
 
     /* Repair image if dirty */
     if (!(flags & (BDRV_O_CHECK | BDRV_O_INACTIVE)) && !bs->read_only &&
@@ -4214,6 +4215,35 @@ static int coroutine_fn qcow2_co_truncate(BlockDriverState *bs, int64_t offset,
         g_assert_not_reached();
     }
 
+    if ((flags & BDRV_REQ_ZERO_WRITE) && offset > old_length) {
+        uint64_t zero_start = QEMU_ALIGN_UP(old_length, s->cluster_size);
+        uint64_t zero_end = QEMU_ALIGN_UP(offset, s->cluster_size);
+
+        /* Use zero clusters as much as we can */
+        ret = qcow2_cluster_zeroize(bs, zero_start, zero_end - zero_start, 0);
+        if (ret < 0) {
+            error_setg_errno(errp, -ret, "Failed to zero out the new area");
+            goto fail;
+        }
+
+        /* Write explicit zeros for the unaligned head */
+        if (zero_start > old_length) {
+            uint8_t *buf = qemu_blockalign0(bs, s->cluster_size);
+            QEMUIOVector qiov;
+            qemu_iovec_init_buf(&qiov, buf, zero_start - old_length);
+
+            qemu_co_mutex_unlock(&s->lock);
+            ret = qcow2_co_pwritev_part(bs, old_length, qiov.size, &qiov, 0, 0);
+            qemu_co_mutex_lock(&s->lock);
+
+            qemu_vfree(buf);
+            if (ret < 0) {
+                error_setg_errno(errp, -ret, "Failed to zero out the new area");
+                goto fail;
+            }
+        }
+    }
+
     if (prealloc != PREALLOC_MODE_OFF) {
         /* Flush metadata before actually changing the image size */
         ret = qcow2_write_caches(bs);
-- 
2.25.3



  parent reply	other threads:[~2020-04-22 15:27 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-22 15:21 [PATCH v5 0/9] block: Fix resize (extending) of short overlays Kevin Wolf
2020-04-22 15:21 ` [PATCH v5 1/9] block: Add flags to BlockDriver.bdrv_co_truncate() Kevin Wolf
2020-04-23  9:41   ` Max Reitz
2020-04-23 12:42     ` Kevin Wolf
2020-04-22 15:21 ` [PATCH v5 2/9] block: Add flags to bdrv(_co)_truncate() Kevin Wolf
2020-04-23 10:04   ` Max Reitz
2020-04-22 15:21 ` [PATCH v5 3/9] block-backend: Add flags to blk_truncate() Kevin Wolf
2020-04-23 10:34   ` Max Reitz
2020-04-22 15:21 ` Kevin Wolf [this message]
2020-04-22 15:33   ` [PATCH v5 4/9] qcow2: Support BDRV_REQ_ZERO_WRITE for truncate Eric Blake
2020-04-22 15:58     ` Kevin Wolf
2020-04-22 16:14       ` Eric Blake
2020-04-23 13:23         ` Kevin Wolf
2020-04-23 13:59           ` Eric Blake
2020-04-23 10:53   ` Max Reitz
2020-04-23 13:25     ` Kevin Wolf
2020-04-23 13:56       ` Max Reitz
2020-04-22 15:21 ` [PATCH v5 5/9] raw-format: " Kevin Wolf
2020-04-22 15:34   ` Eric Blake
2020-04-23 10:54   ` Max Reitz
2020-04-22 15:21 ` [PATCH v5 6/9] file-posix: " Kevin Wolf
2020-04-23 10:57   ` Max Reitz
2020-04-22 15:21 ` [PATCH v5 7/9] block: truncate: Don't make backing file data visible Kevin Wolf
2020-04-23 11:14   ` Max Reitz
2020-04-23 13:00     ` Kevin Wolf
2020-04-22 15:21 ` [PATCH v5 8/9] iotests: Filter testfiles out in filter_img_info() Kevin Wolf
2020-04-23 11:24   ` Max Reitz
2020-04-22 15:21 ` [PATCH v5 9/9] iotests: Test committing to short backing file Kevin Wolf
2020-04-23 11:53   ` Max Reitz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200422152129.167074-5-kwolf@redhat.com \
    --to=kwolf@redhat.com \
    --cc=berto@igalia.com \
    --cc=mreitz@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=vsementsov@virtuozzo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).