From: Max Reitz <mreitz@redhat.com>
To: qemu-devel@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>,
Stefan Hajnoczi <stefanha@redhat.com>,
Max Reitz <mreitz@redhat.com>
Subject: [Qemu-devel] [PATCH 7/8] block/qcow2: Speed up zero cluster expansion
Date: Fri, 25 Jul 2014 20:07:44 +0200 [thread overview]
Message-ID: <1406311665-2814-8-git-send-email-mreitz@redhat.com> (raw)
In-Reply-To: <1406311665-2814-1-git-send-email-mreitz@redhat.com>
Actually, we do not need to allocate a new data cluster for every zero
cluster to be expanded: It is completely sufficient to rely on qcow2's
COW part and instead create a single zero cluster and reuse it as much
as possible.
Signed-off-by: Max Reitz <mreitz@redhat.com>
---
block/qcow2-cluster.c | 119 ++++++++++++++++++++++++++++++++++++++------------
1 file changed, 92 insertions(+), 27 deletions(-)
diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c
index 905beb6..867db03 100644
--- a/block/qcow2-cluster.c
+++ b/block/qcow2-cluster.c
@@ -1558,6 +1558,9 @@ static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table,
BDRVQcowState *s = bs->opaque;
bool is_active_l1 = (l1_table == s->l1_table);
uint64_t *l2_table = NULL;
+ int64_t zeroed_cluster_offset = 0;
+ int zeroed_cluster_refcount = 0;
+ int last_zeroed_cluster_l1i = 0, last_zeroed_cluster_l2i = 0;
int ret;
int i, j;
@@ -1617,47 +1620,79 @@ static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table,
continue;
}
- offset = qcow2_alloc_clusters(bs, s->cluster_size);
- if (offset < 0) {
- ret = offset;
- goto fail;
+ if (zeroed_cluster_offset) {
+ zeroed_cluster_refcount += l2_refcount;
+ if (zeroed_cluster_refcount > 0xffff) {
+ zeroed_cluster_refcount = 0;
+ zeroed_cluster_offset = 0;
+ }
}
+ if (!zeroed_cluster_offset) {
+ offset = qcow2_alloc_clusters(bs, s->cluster_size);
+ if (offset < 0) {
+ ret = offset;
+ goto fail;
+ }
- if (l2_refcount > 1) {
- /* For shared L2 tables, set the refcount accordingly (it is
- * already 1 and needs to be l2_refcount) */
- ret = qcow2_update_cluster_refcount(bs,
- offset >> s->cluster_bits, l2_refcount - 1,
- QCOW2_DISCARD_OTHER);
+ ret = qcow2_pre_write_overlap_check(bs, 0, offset,
+ s->cluster_size);
+ if (ret < 0) {
+ qcow2_free_clusters(bs, offset, s->cluster_size,
+ QCOW2_DISCARD_OTHER);
+ goto fail;
+ }
+
+ ret = bdrv_write_zeroes(bs->file, offset / BDRV_SECTOR_SIZE,
+ s->cluster_sectors, 0);
if (ret < 0) {
qcow2_free_clusters(bs, offset, s->cluster_size,
QCOW2_DISCARD_OTHER);
goto fail;
}
+
+ if (l2_refcount > 1) {
+ ret = qcow2_update_cluster_refcount(bs,
+ offset >> s->cluster_bits, l2_refcount - 1,
+ QCOW2_DISCARD_OTHER);
+ if (ret < 0) {
+ qcow2_free_clusters(bs, offset, s->cluster_size,
+ QCOW2_DISCARD_OTHER);
+ goto fail;
+ }
+ }
+
+ zeroed_cluster_offset = offset;
+ zeroed_cluster_refcount = l2_refcount;
+ } else {
+ ret = qcow2_update_cluster_refcount(bs,
+ zeroed_cluster_offset >> s->cluster_bits,
+ l2_refcount, QCOW2_DISCARD_OTHER);
+ if (ret < 0) {
+ goto fail;
+ }
}
+
+ offset = zeroed_cluster_offset;
+ last_zeroed_cluster_l1i = i;
+ last_zeroed_cluster_l2i = j;
}
- ret = qcow2_pre_write_overlap_check(bs, 0, offset, s->cluster_size);
- if (ret < 0) {
- if (!preallocated) {
- qcow2_free_clusters(bs, offset, s->cluster_size,
- QCOW2_DISCARD_ALWAYS);
+ if (preallocated) {
+ ret = qcow2_pre_write_overlap_check(bs, 0, offset,
+ s->cluster_size);
+ if (ret < 0) {
+ goto fail;
}
- goto fail;
- }
- ret = bdrv_write_zeroes(bs->file, offset / BDRV_SECTOR_SIZE,
- s->cluster_sectors, 0);
- if (ret < 0) {
- if (!preallocated) {
- qcow2_free_clusters(bs, offset, s->cluster_size,
- QCOW2_DISCARD_ALWAYS);
+ ret = bdrv_write_zeroes(bs->file, offset / BDRV_SECTOR_SIZE,
+ s->cluster_sectors, 0);
+ if (ret < 0) {
+ goto fail;
}
- goto fail;
}
- if (l2_refcount == 1) {
- l2_table[j] = cpu_to_be64(offset | QCOW_OFLAG_COPIED);
+ if (preallocated) {
+ l2_table[j] = cpu_to_be64(offset | (l2_entry & QCOW_OFLAG_COPIED));
} else {
l2_table[j] = cpu_to_be64(offset);
}
@@ -1670,8 +1705,8 @@ static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table,
qcow2_cache_depends_on_flush(s->l2_table_cache);
}
ret = qcow2_cache_put(bs, s->l2_table_cache, (void **)&l2_table);
+ l2_table = NULL;
if (ret < 0) {
- l2_table = NULL;
goto fail;
}
} else {
@@ -1697,6 +1732,36 @@ static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table,
}
}
+ /* Fix COPIED (only valid for active L2 tables) */
+ if (is_active_l1 && zeroed_cluster_refcount == 1) {
+ uint64_t l2_offset, l2_entry;
+
+ l2_offset = l1_table[last_zeroed_cluster_l1i] & L1E_OFFSET_MASK;
+ assert(l2_offset);
+
+ ret = qcow2_cache_get(bs, s->l2_table_cache, l2_offset,
+ (void **)&l2_table);
+ if (ret < 0) {
+ goto fail;
+ }
+
+ l2_entry = be64_to_cpu(l2_table[last_zeroed_cluster_l2i]);
+
+ assert(!(l2_entry & QCOW_OFLAG_COPIED));
+ l2_entry |= QCOW_OFLAG_COPIED;
+
+ l2_table[last_zeroed_cluster_l2i] = cpu_to_be64(l2_entry);
+
+ qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table);
+ qcow2_cache_depends_on_flush(s->l2_table_cache);
+
+ ret = qcow2_cache_put(bs, s->l2_table_cache, (void **)&l2_table);
+ l2_table = NULL;
+ if (ret < 0) {
+ goto fail;
+ }
+ }
+
ret = 0;
fail:
--
2.0.1
next prev parent reply other threads:[~2014-07-25 18:07 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-07-25 18:07 [Qemu-devel] [PATCH 0/8] block/qcow2: Improve (?) zero cluster expansion Max Reitz
2014-07-25 18:07 ` [Qemu-devel] [PATCH 1/8] block: Add status callback to bdrv_amend_options() Max Reitz
2014-07-30 14:50 ` Eric Blake
2014-07-25 18:07 ` [Qemu-devel] [PATCH 2/8] qemu-img: Add progress output for amend Max Reitz
2014-07-30 14:55 ` Eric Blake
2014-07-30 20:20 ` Max Reitz
2014-07-25 18:07 ` [Qemu-devel] [PATCH 3/8] qemu-img: Fix insignifcant memleak Max Reitz
2014-07-30 14:56 ` Eric Blake
2014-07-25 18:07 ` [Qemu-devel] [PATCH 4/8] block/qcow2: Make get_refcount() global Max Reitz
2014-07-30 15:04 ` Eric Blake
2014-07-25 18:07 ` [Qemu-devel] [PATCH 5/8] block/qcow2: Implement status CB for amend Max Reitz
2014-07-30 15:23 ` Eric Blake
2014-07-25 18:07 ` [Qemu-devel] [PATCH 6/8] block/qcow2: Simplify shared L2 handling in amend Max Reitz
2014-07-30 15:36 ` Eric Blake
2014-07-25 18:07 ` Max Reitz [this message]
2014-07-30 16:14 ` [Qemu-devel] [PATCH 7/8] block/qcow2: Speed up zero cluster expansion Eric Blake
2014-07-30 20:31 ` Max Reitz
2014-07-30 20:31 ` Eric Blake
2014-07-30 20:41 ` Max Reitz
2014-07-25 18:07 ` [Qemu-devel] [PATCH 8/8] iotests: Expand test 061 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=1406311665-2814-8-git-send-email-mreitz@redhat.com \
--to=mreitz@redhat.com \
--cc=kwolf@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@redhat.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).