[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 1/4] qcow2: Clean-up update_cluster_refcount().
From: |
Laurent Vivier |
Subject: |
[Qemu-devel] [PATCH 1/4] qcow2: Clean-up update_cluster_refcount(). |
Date: |
Thu, 06 Nov 2008 17:55:57 +0100 |
pièce jointe document texte brut
(0001-Clean-up-update_cluster_refcount.patch)
Move some parts to alloc_refcount_block() and block cache
checking to load_refcount_block().
Signed-off-by: Laurent Vivier <address@hidden>
---
block-qcow2.c | 71 +++++++++++++++++++++++++++++++++++-----------------------
1 file changed, 43 insertions(+), 28 deletions(-)
Index: qemu/block-qcow2.c
===================================================================
--- qemu.orig/block-qcow2.c 2008-11-06 16:34:46.000000000 +0100
+++ qemu/block-qcow2.c 2008-11-06 16:40:27.000000000 +0100
@@ -2169,6 +2169,10 @@ static int load_refcount_block(BlockDriv
{
BDRVQcowState *s = bs->opaque;
int ret;
+
+ if (refcount_block_offset == s->refcount_block_cache_offset)
+ return 0;
+
ret = bdrv_pread(s->hd, refcount_block_offset, s->refcount_block_cache,
s->cluster_size);
if (ret != s->cluster_size)
@@ -2189,11 +2193,8 @@ static int get_refcount(BlockDriverState
refcount_block_offset = s->refcount_table[refcount_table_index];
if (!refcount_block_offset)
return 0;
- if (refcount_block_offset != s->refcount_block_cache_offset) {
- /* better than nothing: return allocated if read error */
- if (load_refcount_block(bs, refcount_block_offset) < 0)
- return 1;
- }
+ if (load_refcount_block(bs, refcount_block_offset) < 0)
+ return 1;
block_index = cluster_index &
((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
return be16_to_cpu(s->refcount_block_cache[block_index]);
@@ -2227,6 +2228,38 @@ static int64_t alloc_clusters_noref(Bloc
}
}
+/* create a new refcount block */
+
+static int64_t alloc_refcount_block(BlockDriverState *bs, int
refcount_table_index)
+{
+ BDRVQcowState *s = bs->opaque;
+ int64_t offset;
+ int ret;
+ uint64_t data64;
+
+ /* Note: we cannot update the refcount now to avoid recursion */
+
+ offset = alloc_clusters_noref(bs, s->cluster_size);
+ memset(s->refcount_block_cache, 0, s->cluster_size);
+
+ ret = bdrv_pwrite(s->hd, offset, s->refcount_block_cache, s->cluster_size);
+ if (ret != s->cluster_size)
+ return -1;
+
+ s->refcount_table[refcount_table_index] = offset;
+ data64 = cpu_to_be64(offset);
+ ret = bdrv_pwrite(s->hd, s->refcount_table_offset +
+ refcount_table_index * sizeof(uint64_t),
+ &data64, sizeof(data64));
+ if (ret != sizeof(data64))
+ return -1;
+
+ s->refcount_block_cache_offset = offset;
+ update_refcount(bs, offset, s->cluster_size, 1);
+
+ return offset;
+}
+
static int64_t alloc_clusters(BlockDriverState *bs, int64_t size)
{
int64_t offset;
@@ -2359,9 +2392,8 @@ static int update_cluster_refcount(Block
int addend)
{
BDRVQcowState *s = bs->opaque;
- int64_t offset, refcount_block_offset;
+ int64_t refcount_block_offset;
int ret, refcount_table_index, block_index, refcount;
- uint64_t data64;
refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
if (refcount_table_index >= s->refcount_table_size) {
@@ -2375,29 +2407,12 @@ static int update_cluster_refcount(Block
if (!refcount_block_offset) {
if (addend < 0)
return -EINVAL;
- /* create a new refcount block */
- /* Note: we cannot update the refcount now to avoid recursion */
- offset = alloc_clusters_noref(bs, s->cluster_size);
- memset(s->refcount_block_cache, 0, s->cluster_size);
- ret = bdrv_pwrite(s->hd, offset, s->refcount_block_cache,
s->cluster_size);
- if (ret != s->cluster_size)
- return -EINVAL;
- s->refcount_table[refcount_table_index] = offset;
- data64 = cpu_to_be64(offset);
- ret = bdrv_pwrite(s->hd, s->refcount_table_offset +
- refcount_table_index * sizeof(uint64_t),
- &data64, sizeof(data64));
- if (ret != sizeof(data64))
+ refcount_block_offset = alloc_refcount_block(bs, refcount_table_index);
+ if (refcount_block_offset < 0)
return -EINVAL;
-
- refcount_block_offset = offset;
- s->refcount_block_cache_offset = offset;
- update_refcount(bs, offset, s->cluster_size, 1);
} else {
- if (refcount_block_offset != s->refcount_block_cache_offset) {
- if (load_refcount_block(bs, refcount_block_offset) < 0)
- return -EIO;
- }
+ if (load_refcount_block(bs, refcount_block_offset) < 0)
+ return -EIO;
}
/* we can update the count and save it */
block_index = cluster_index &
--
- [Qemu-devel] [PATCH 1/4] qcow2: Clean-up update_cluster_refcount().,
Laurent Vivier <=