From: Kevin Wolf <kwolf@redhat.com>
To: aurelien@aurel32.net
Cc: kwolf@redhat.com, qemu-devel@nongnu.org
Subject: [Qemu-devel] [STABLE][PATCH 02/14] qcow2: Fix creation of large images
Date: Wed, 14 Jul 2010 13:24:01 +0200 [thread overview]
Message-ID: <1279106653-24351-3-git-send-email-kwolf@redhat.com> (raw)
In-Reply-To: <1279106653-24351-1-git-send-email-kwolf@redhat.com>
qcow_create2 assumes that the new image will only need one cluster for its
refcount table initially. Obviously that's not true any more when the image is
big enough (exact value depends on the cluster size).
This patch calculates the refcount table size dynamically.
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
(cherry picked from commit 4768fa902c3860f2fe34403e6e1c83bfca6da034)
Conflicts:
block/qcow2.c
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
block/qcow2.c | 41 ++++++++++++++++++++++++++++++++---------
1 files changed, 32 insertions(+), 9 deletions(-)
diff --git a/block/qcow2.c b/block/qcow2.c
index 5d33d6c..35c05e0 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -747,10 +747,11 @@ static int qcow_create2(const char *filename, int64_t total_size,
{
int fd, header_size, backing_filename_len, l1_size, i, shift, l2_bits;
- int ref_clusters, backing_format_len = 0;
+ int ref_clusters, reftable_clusters, backing_format_len = 0;
int rounded_ext_bf_len = 0;
QCowHeader header;
uint64_t tmp, offset;
+ uint64_t old_ref_clusters;
QCowCreateState s1, *s = &s1;
QCowExtension ext_bf = {0, 0};
@@ -809,17 +810,37 @@ static int qcow_create2(const char *filename, int64_t total_size,
header.l1_size = cpu_to_be32(l1_size);
offset += align_offset(l1_size * sizeof(uint64_t), s->cluster_size);
- s->refcount_table = qemu_mallocz(s->cluster_size);
+ /* count how many refcount blocks needed */
+
+#define NUM_CLUSTERS(bytes) \
+ (((bytes) + (s->cluster_size) - 1) / (s->cluster_size))
+
+ ref_clusters = NUM_CLUSTERS(NUM_CLUSTERS(offset) * sizeof(uint16_t));
+
+ do {
+ uint64_t image_clusters;
+ old_ref_clusters = ref_clusters;
+
+ /* Number of clusters used for the refcount table */
+ reftable_clusters = NUM_CLUSTERS(ref_clusters * sizeof(uint64_t));
+
+ /* Number of clusters that the whole image will have */
+ image_clusters = NUM_CLUSTERS(offset) + ref_clusters
+ + reftable_clusters;
+
+ /* Number of refcount blocks needed for the image */
+ ref_clusters = NUM_CLUSTERS(image_clusters * sizeof(uint16_t));
+
+ } while (ref_clusters != old_ref_clusters);
+
+ s->refcount_table = qemu_mallocz(reftable_clusters * s->cluster_size);
s->refcount_table_offset = offset;
header.refcount_table_offset = cpu_to_be64(offset);
- header.refcount_table_clusters = cpu_to_be32(1);
- offset += s->cluster_size;
+ header.refcount_table_clusters = cpu_to_be32(reftable_clusters);
+ offset += (reftable_clusters * s->cluster_size);
s->refcount_block_offset = offset;
- /* count how many refcount blocks needed */
- tmp = offset >> s->cluster_bits;
- ref_clusters = (tmp >> (s->cluster_bits - REFCOUNT_SHIFT)) + 1;
for (i=0; i < ref_clusters; i++) {
s->refcount_table[i] = cpu_to_be64(offset);
offset += s->cluster_size;
@@ -831,7 +852,8 @@ static int qcow_create2(const char *filename, int64_t total_size,
qcow2_create_refcount_update(s, 0, header_size);
qcow2_create_refcount_update(s, s->l1_table_offset,
l1_size * sizeof(uint64_t));
- qcow2_create_refcount_update(s, s->refcount_table_offset, s->cluster_size);
+ qcow2_create_refcount_update(s, s->refcount_table_offset,
+ reftable_clusters * s->cluster_size);
qcow2_create_refcount_update(s, s->refcount_block_offset,
ref_clusters * s->cluster_size);
@@ -859,7 +881,8 @@ static int qcow_create2(const char *filename, int64_t total_size,
write(fd, &tmp, sizeof(tmp));
}
lseek(fd, s->refcount_table_offset, SEEK_SET);
- write(fd, s->refcount_table, s->cluster_size);
+ write(fd, s->refcount_table,
+ reftable_clusters * s->cluster_size);
lseek(fd, s->refcount_block_offset, SEEK_SET);
write(fd, s->refcount_block, ref_clusters * s->cluster_size);
--
1.7.1.1
next prev parent reply other threads:[~2010-07-14 11:24 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-07-14 11:23 [Qemu-devel] [STABLE][PULL 00/14] Block patches for 0.12.5 Kevin Wolf
2010-07-14 11:24 ` [Qemu-devel] [STABLE][PATCH 01/14] vmdk: fix double free Kevin Wolf
2010-07-14 11:24 ` Kevin Wolf [this message]
2010-07-14 11:24 ` [Qemu-devel] [STABLE][PATCH 03/14] vmdk: Fix COW Kevin Wolf
2010-07-14 11:24 ` [Qemu-devel] [STABLE][PATCH 04/14] qcow2: Remove abort on free_clusters failure Kevin Wolf
2010-07-14 11:24 ` [Qemu-devel] [STABLE][PATCH 05/14] block/vpc: Fix conversion from size to disk geometry Kevin Wolf
2010-07-14 11:24 ` [Qemu-devel] [STABLE][PATCH 06/14] block/vdi: Fix image opening and creation for odd disk sizes Kevin Wolf
2010-07-14 11:24 ` [Qemu-devel] [STABLE][PATCH 07/14] qcow2: Restore L1 entry on l2_allocate failure Kevin Wolf
2010-07-14 11:24 ` [Qemu-devel] [STABLE][PATCH 08/14] block: Add bdrv_(p)write_sync Kevin Wolf
2010-07-14 11:24 ` [Qemu-devel] [STABLE][PATCH 09/14] qcow: Use bdrv_(p)write_sync for metadata writes Kevin Wolf
2010-07-14 11:24 ` [Qemu-devel] [STABLE][PATCH 10/14] qcow2: " Kevin Wolf
2010-07-14 11:24 ` [Qemu-devel] [STABLE][PATCH 11/14] vmdk: " Kevin Wolf
2010-07-14 11:24 ` [Qemu-devel] [STABLE][PATCH 12/14] vpc: " Kevin Wolf
2010-07-14 11:24 ` [Qemu-devel] [STABLE][PATCH 13/14] block: Fix early failure in multiwrite Kevin Wolf
2010-07-14 11:24 ` [Qemu-devel] [STABLE][PATCH 14/14] block: Handle multiwrite errors only when all requests have completed Kevin Wolf
2010-07-14 12:50 ` [Qemu-devel] [STABLE][PULL 00/14] Block patches for 0.12.5 Aurelien Jarno
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=1279106653-24351-3-git-send-email-kwolf@redhat.com \
--to=kwolf@redhat.com \
--cc=aurelien@aurel32.net \
--cc=qemu-devel@nongnu.org \
/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).