From: Max Reitz <mreitz@redhat.com>
To: qemu-devel@nongnu.org
Cc: Kevin Wolf <kwolf@redhat.com>, Peter Lieven <pl@kamp.de>,
Stefan Hajnoczi <stefanha@redhat.com>,
Max Reitz <mreitz@redhat.com>
Subject: [Qemu-devel] [PATCH 07/21] qcow2: Helper for refcount array size calculation
Date: Mon, 10 Nov 2014 14:45:45 +0100 [thread overview]
Message-ID: <1415627159-15941-8-git-send-email-mreitz@redhat.com> (raw)
In-Reply-To: <1415627159-15941-1-git-send-email-mreitz@redhat.com>
Add a helper function which correctly calculates the byte size of a
refcount array for any refcount order, and use that function.
Signed-off-by: Max Reitz <mreitz@redhat.com>
---
block/qcow2-refcount.c | 39 ++++++++++++++++++++++++++++-----------
1 file changed, 28 insertions(+), 11 deletions(-)
diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c
index 16652da..cfb4807 100644
--- a/block/qcow2-refcount.c
+++ b/block/qcow2-refcount.c
@@ -1132,6 +1132,20 @@ fail:
/* refcount checking functions */
+static size_t refcount_array_byte_size(BDRVQcowState *s, uint64_t entries)
+{
+ if (s->refcount_order < 3) {
+ /* sub-byte width */
+ int shift = 3 - s->refcount_order;
+ return (entries + (1 << shift) - 1) >> shift;
+ } else if (s->refcount_order == 3) {
+ /* byte width */
+ return entries;
+ } else {
+ /* multiple bytes wide */
+ return entries << (s->refcount_order - 3);
+ }
+}
/*
* Increases the refcount for a range of clusters in a given refcount table.
@@ -1161,12 +1175,13 @@ static int inc_refcounts(BlockDriverState *bs,
k = cluster_offset >> s->cluster_bits;
if (k >= *refcount_table_size) {
int64_t old_refcount_table_size = *refcount_table_size;
+ size_t new_byte_size;
void *new_refcount_table;
*refcount_table_size = k + 1;
- new_refcount_table = g_try_realloc(*refcount_table,
- *refcount_table_size *
- s->refcount_bits / 8);
+ new_byte_size = refcount_array_byte_size(s, *refcount_table_size);
+
+ new_refcount_table = g_try_realloc(*refcount_table, new_byte_size);
if (!new_refcount_table) {
*refcount_table_size = old_refcount_table_size;
res->check_errors++;
@@ -1547,6 +1562,7 @@ static int check_refblocks(BlockDriverState *bs, BdrvCheckResult *res,
if (fix & BDRV_FIX_ERRORS) {
int64_t old_nb_clusters = *nb_clusters;
+ size_t new_byte_size;
void *new_refcount_table;
if (offset > INT64_MAX - s->cluster_size) {
@@ -1567,9 +1583,9 @@ static int check_refblocks(BlockDriverState *bs, BdrvCheckResult *res,
*nb_clusters = size_to_clusters(s, size);
assert(*nb_clusters >= old_nb_clusters);
+ new_byte_size = refcount_array_byte_size(s, *nb_clusters);
new_refcount_table = g_try_realloc(*refcount_table,
- *nb_clusters *
- s->refcount_bits / 8);
+ new_byte_size);
if (!new_refcount_table) {
*nb_clusters = old_nb_clusters;
res->check_errors++;
@@ -1640,7 +1656,8 @@ static int calculate_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
int ret;
if (!*refcount_table) {
- *refcount_table = g_try_malloc0(*nb_clusters * s->refcount_bits / 8);
+ size_t byte_size = refcount_array_byte_size(s, *nb_clusters);
+ *refcount_table = g_try_malloc0(byte_size);
if (*nb_clusters && *refcount_table == NULL) {
res->check_errors++;
return -ENOMEM;
@@ -1805,6 +1822,7 @@ static int64_t alloc_clusters_imrt(BlockDriverState *bs,
* accordingly to append free clusters at the end of the image */
if (contiguous_free_clusters < cluster_count) {
int64_t old_imrt_nb_clusters = *imrt_nb_clusters;
+ size_t new_byte_size;
void *new_refcount_table;
/* contiguous_free_clusters clusters are already empty at the image end;
@@ -1814,9 +1832,8 @@ static int64_t alloc_clusters_imrt(BlockDriverState *bs,
* may exceed old_imrt_nb_clusters if *first_free_cluster pointed beyond
* the image end) */
*imrt_nb_clusters = cluster + cluster_count - contiguous_free_clusters;
- new_refcount_table = g_try_realloc(*refcount_table,
- *imrt_nb_clusters *
- s->refcount_bits / 8);
+ new_byte_size = refcount_array_byte_size(s, *imrt_nb_clusters);
+ new_refcount_table = g_try_realloc(*refcount_table, new_byte_size);
if (!new_refcount_table) {
*imrt_nb_clusters = old_imrt_nb_clusters;
return -ENOMEM;
@@ -1943,8 +1960,8 @@ write_refblocks:
memcpy(on_disk_refblock, (void *)((uintptr_t)*refcount_table +
(refblock_index << s->refcount_block_bits)),
- MIN(s->refcount_block_size, *nb_clusters - refblock_start)
- * s->refcount_bits / 8);
+ refcount_array_byte_size(s, MIN(s->refcount_block_size,
+ *nb_clusters - refblock_start)));
ret = bdrv_write(bs->file, refblock_offset / BDRV_SECTOR_SIZE,
(void *)on_disk_refblock, s->cluster_sectors);
--
1.9.3
next prev parent reply other threads:[~2014-11-10 13:46 UTC|newest]
Thread overview: 75+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-11-10 13:45 [Qemu-devel] [PATCH 00/21] qcow2: Support refcount orders != 4 Max Reitz
2014-11-10 13:45 ` [Qemu-devel] [PATCH 01/21] qcow2: Add two new fields to BDRVQcowState Max Reitz
2014-11-10 19:00 ` Eric Blake
2014-11-10 13:45 ` [Qemu-devel] [PATCH 02/21] qcow2: Add refcount_width to format-specific info Max Reitz
2014-11-10 19:06 ` Eric Blake
2014-11-11 8:11 ` Max Reitz
2014-11-11 15:49 ` Eric Blake
2014-11-10 13:45 ` [Qemu-devel] [PATCH 03/21] qcow2: Use 64 bits for refcount values Max Reitz
2014-11-10 20:59 ` Eric Blake
2014-11-11 8:12 ` Max Reitz
2014-11-11 9:22 ` Kevin Wolf
2014-11-11 9:25 ` Max Reitz
2014-11-11 9:26 ` Max Reitz
2014-11-11 9:36 ` Kevin Wolf
2014-11-10 13:45 ` [Qemu-devel] [PATCH 04/21] qcow2: Respect error in qcow2_alloc_bytes() Max Reitz
2014-11-10 21:05 ` Eric Blake
2014-11-10 13:45 ` [Qemu-devel] [PATCH 05/21] qcow2: Refcount overflow and qcow2_alloc_bytes() Max Reitz
2014-11-10 21:12 ` Eric Blake
2014-11-11 8:22 ` Max Reitz
2014-11-11 16:13 ` Eric Blake
2014-11-11 16:18 ` Max Reitz
2014-11-11 19:49 ` Eric Blake
2014-11-12 8:52 ` Max Reitz
2014-11-10 13:45 ` [Qemu-devel] [PATCH 06/21] qcow2: Helper function for refcount modification Max Reitz
2014-11-10 22:30 ` Eric Blake
2014-11-11 8:35 ` Max Reitz
2014-11-11 9:43 ` Max Reitz
2014-11-11 10:56 ` Max Reitz
2014-11-10 13:45 ` Max Reitz [this message]
2014-11-10 22:49 ` [Qemu-devel] [PATCH 07/21] qcow2: Helper for refcount array size calculation Eric Blake
2014-11-11 8:37 ` Max Reitz
2014-11-11 10:08 ` Max Reitz
2014-11-10 13:45 ` [Qemu-devel] [PATCH 08/21] qcow2: More helpers for refcount modification Max Reitz
2014-11-11 0:29 ` Eric Blake
2014-11-11 8:42 ` Max Reitz
2014-11-10 13:45 ` [Qemu-devel] [PATCH 09/21] qcow2: Open images with refcount order != 4 Max Reitz
2014-11-10 17:03 ` Eric Blake
2014-11-10 17:06 ` Max Reitz
2014-11-10 13:45 ` [Qemu-devel] [PATCH 10/21] qcow2: refcount_order parameter for qcow2_create2 Max Reitz
2014-11-11 5:40 ` Eric Blake
2014-11-11 8:48 ` Max Reitz
2014-11-10 13:45 ` [Qemu-devel] [PATCH 11/21] iotests: Prepare for refcount_width option Max Reitz
2014-11-11 17:57 ` Eric Blake
2014-11-12 8:41 ` Max Reitz
2014-11-10 13:45 ` [Qemu-devel] [PATCH 12/21] qcow2: Allow creation with refcount order != 4 Max Reitz
2014-11-11 18:05 ` Eric Blake
2014-11-12 8:47 ` Max Reitz
2014-11-10 13:45 ` [Qemu-devel] [PATCH 13/21] block: Add opaque value to the amend CB Max Reitz
2014-11-11 18:08 ` Eric Blake
2014-11-10 13:45 ` [Qemu-devel] [PATCH 14/21] qcow2: Use error_report() in qcow2_amend_options() Max Reitz
2014-11-11 18:11 ` Eric Blake
2014-11-12 8:47 ` Max Reitz
2014-11-10 13:45 ` [Qemu-devel] [PATCH 15/21] qcow2: Use abort() instead of assert(false) Max Reitz
2014-11-11 18:12 ` Eric Blake
2014-11-12 8:48 ` Max Reitz
2014-11-10 13:45 ` [Qemu-devel] [PATCH 16/21] qcow2: Split upgrade/downgrade paths for amend Max Reitz
2014-11-11 18:14 ` Eric Blake
2014-11-10 13:45 ` [Qemu-devel] [PATCH 17/21] qcow2: Use intermediate helper CB " Max Reitz
2014-11-11 21:05 ` Eric Blake
2014-11-12 9:10 ` Max Reitz
2014-11-10 13:45 ` [Qemu-devel] [PATCH 18/21] qcow2: Add function for refcount order amendment Max Reitz
2014-11-12 4:15 ` Eric Blake
2014-11-12 9:55 ` Max Reitz
2014-11-12 13:50 ` Eric Blake
2014-11-12 14:19 ` Eric Blake
2014-11-12 14:21 ` Max Reitz
2014-11-12 17:45 ` Max Reitz
2014-11-12 20:21 ` Eric Blake
2014-11-10 13:45 ` [Qemu-devel] [PATCH 19/21] qcow2: Invoke refcount order amendment function Max Reitz
2014-11-12 4:36 ` Eric Blake
2014-11-10 13:45 ` [Qemu-devel] [PATCH 20/21] qcow2: Point to amend function in check Max Reitz
2014-11-12 4:38 ` Eric Blake
2014-11-10 13:45 ` [Qemu-devel] [PATCH 21/21] iotests: Add test for different refcount widths Max Reitz
2014-11-11 19:53 ` Eric Blake
2014-11-12 8:58 ` 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=1415627159-15941-8-git-send-email-mreitz@redhat.com \
--to=mreitz@redhat.com \
--cc=kwolf@redhat.com \
--cc=pl@kamp.de \
--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).