qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Laurent Vivier <Laurent.Vivier@bull.net>
To: qemu-devel@nongnu.org
Cc: Laurent Vivier <Laurent.Vivier@bull.net>
Subject: [Qemu-devel] [PATCH 1/4] qcow2: Clean-up update_cluster_refcount().
Date: Thu, 06 Nov 2008 17:55:57 +0100	[thread overview]
Message-ID: <1225990557.6576.11.camel@frecb07144> (raw)
In-Reply-To: 20081106165212.380421945@bull.net

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 <Laurent.Vivier@bull.net>
---
 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 &

-- 

       reply	other threads:[~2008-11-06 16:56 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20081106165212.380421945@bull.net>
2008-11-06 16:55 ` Laurent Vivier [this message]
2008-11-06 17:32   ` [Qemu-devel] [PATCH 1/4] qcow2: Clean-up update_cluster_refcount() Kevin Wolf
2008-11-07  8:48     ` Laurent Vivier
2008-11-07  8:54       ` Kevin Wolf
2008-11-07  9:22         ` Laurent Vivier
2008-11-06 16:55 ` [Qemu-devel] [PATCH 2/4] qcow2: Allow update_cluster_refcount() to update several clusters refcount Laurent Vivier
2008-11-06 18:11   ` Kevin Wolf
2008-11-07 10:03     ` Laurent Vivier
2008-11-07 10:21       ` Kevin Wolf
2008-11-07 11:39         ` Laurent Vivier
2008-11-06 16:55 ` [Qemu-devel] [PATCH 3/4] qcow2: Align I/O access to l2 table and refcount block Laurent Vivier
2008-11-06 16:56 ` [Qemu-devel] [PATCH 4/4] qcow2: detect if no disk cache Laurent Vivier

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=1225990557.6576.11.camel@frecb07144 \
    --to=laurent.vivier@bull.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).