From: Max Reitz <mreitz@redhat.com>
To: qemu-block@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>, Jeff Cody <codyprime@gmail.com>,
Stefan Weil <sw@weilnetz.de>,
qemu-devel@nongnu.org, Max Reitz <mreitz@redhat.com>,
Stefano Garzarella <sgarzare@redhat.com>
Subject: [Qemu-devel] [PATCH for-4.1? 1/4] qcow2: Fix .bdrv_has_zero_init()
Date: Mon, 15 Jul 2019 12:45:05 +0200 [thread overview]
Message-ID: <20190715104508.7568-2-mreitz@redhat.com> (raw)
In-Reply-To: <20190715104508.7568-1-mreitz@redhat.com>
If a qcow2 file is preallocated, it can no longer guarantee that it
initially appears as filled with zeroes.
So implement .bdrv_has_zero_init() by checking whether the file is
preallocated; if so, forward the call to the underlying storage node,
except for when it is encrypted: Encrypted preallocated images always
return effectively random data, so .bdrv_has_zero_init() must always
return 0 for them.
Reported-by: Stefano Garzarella <sgarzare@redhat.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
---
block/qcow2.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 89 insertions(+), 1 deletion(-)
diff --git a/block/qcow2.c b/block/qcow2.c
index 039bdc2f7e..730fd53890 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -4631,6 +4631,94 @@ static ImageInfoSpecific *qcow2_get_specific_info(BlockDriverState *bs,
return spec_info;
}
+/*
+ * Return 1 if the file only contains zero and unallocated clusters.
+ * Return 0 if it contains compressed or normal clusters.
+ * Return -errno on error.
+ */
+static int qcow2_is_zero(BlockDriverState *bs)
+{
+ BDRVQcow2State *s = bs->opaque;
+ int l1_i;
+ int ret;
+
+ if (bs->backing) {
+ return 0;
+ }
+
+ for (l1_i = 0; l1_i < s->l1_size; l1_i++) {
+ uint64_t l2_offset = s->l1_table[l1_i] & L1E_OFFSET_MASK;
+ int slice_start_i;
+
+ if (!l2_offset) {
+ continue;
+ }
+
+ for (slice_start_i = 0; slice_start_i < s->l2_size;
+ slice_start_i += s->l2_slice_size)
+ {
+ uint64_t *l2_slice;
+ int l2_slice_i;
+
+ ret = qcow2_cache_get(bs, s->l2_table_cache,
+ l2_offset + slice_start_i * sizeof(uint64_t),
+ (void **)&l2_slice);
+ if (ret < 0) {
+ return ret;
+ }
+
+ for (l2_slice_i = 0; l2_slice_i < s->l2_slice_size; l2_slice_i++) {
+ uint64_t l2_entry = be64_to_cpu(l2_slice[l2_slice_i]);
+
+ switch (qcow2_get_cluster_type(bs, l2_entry)) {
+ case QCOW2_CLUSTER_UNALLOCATED:
+ case QCOW2_CLUSTER_ZERO_PLAIN:
+ case QCOW2_CLUSTER_ZERO_ALLOC:
+ break;
+
+ case QCOW2_CLUSTER_NORMAL:
+ case QCOW2_CLUSTER_COMPRESSED:
+ qcow2_cache_put(s->l2_table_cache, (void **)&l2_slice);
+ return 0;
+
+ default:
+ abort();
+ }
+ }
+
+ qcow2_cache_put(s->l2_table_cache, (void **)&l2_slice);
+ }
+ }
+
+ return 1;
+}
+
+static int qcow2_has_zero_init(BlockDriverState *bs)
+{
+ BDRVQcow2State *s = bs->opaque;
+ int ret;
+
+ if (qemu_in_coroutine()) {
+ qemu_co_mutex_lock(&s->lock);
+ }
+ /* Check preallocation status */
+ ret = qcow2_is_zero(bs);
+ if (qemu_in_coroutine()) {
+ qemu_co_mutex_unlock(&s->lock);
+ }
+ if (ret < 0) {
+ return 0;
+ }
+
+ if (ret == 1) {
+ return 1;
+ } else if (bs->encrypted) {
+ return 0;
+ } else {
+ return bdrv_has_zero_init(s->data_file->bs);
+ }
+}
+
static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
int64_t pos)
{
@@ -5186,7 +5274,7 @@ BlockDriver bdrv_qcow2 = {
.bdrv_child_perm = bdrv_format_default_perms,
.bdrv_co_create_opts = qcow2_co_create_opts,
.bdrv_co_create = qcow2_co_create,
- .bdrv_has_zero_init = bdrv_has_zero_init_1,
+ .bdrv_has_zero_init = qcow2_has_zero_init,
.bdrv_co_block_status = qcow2_co_block_status,
.bdrv_co_preadv = qcow2_co_preadv,
--
2.21.0
next prev parent reply other threads:[~2019-07-15 10:46 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-07-15 10:45 [Qemu-devel] [PATCH for-4.1? 0/4] block: Fix three .bdrv_has_zero_init()s Max Reitz
2019-07-15 10:45 ` Max Reitz [this message]
2019-07-16 16:54 ` [Qemu-devel] [PATCH for-4.1? 1/4] qcow2: Fix .bdrv_has_zero_init() Kevin Wolf
2019-07-17 8:37 ` Max Reitz
2019-07-15 10:45 ` [Qemu-devel] [PATCH for-4.1? 2/4] vdi: " Max Reitz
2019-07-15 10:45 ` [Qemu-devel] [PATCH for-4.1? 3/4] vhdx: " Max Reitz
2019-07-15 10:45 ` [Qemu-devel] [PATCH for-4.1? 4/4] iotests: Convert to preallocated encrypted qcow2 Max Reitz
2019-07-15 14:49 ` [Qemu-devel] [PATCH for-4.1? 0/4] block: Fix three .bdrv_has_zero_init()s Stefano Garzarella
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=20190715104508.7568-2-mreitz@redhat.com \
--to=mreitz@redhat.com \
--cc=codyprime@gmail.com \
--cc=kwolf@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=sgarzare@redhat.com \
--cc=sw@weilnetz.de \
/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).