9ae3a8
From 0b47923f65c2e457bb642b949f0e3dda722eba35 Mon Sep 17 00:00:00 2001
9ae3a8
From: Max Reitz <mreitz@redhat.com>
9ae3a8
Date: Sat, 13 Jun 2015 16:22:26 +0200
9ae3a8
Subject: [PATCH 32/42] qcow2: Drop REFCOUNT_SHIFT
9ae3a8
9ae3a8
Message-id: <1434212556-3927-33-git-send-email-mreitz@redhat.com>
9ae3a8
Patchwork-id: 66051
9ae3a8
O-Subject: [RHEL-7.2 qemu-kvm PATCH 32/42] qcow2: Drop REFCOUNT_SHIFT
9ae3a8
Bugzilla: 1129893
9ae3a8
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
9ae3a8
RH-Acked-by: Fam Zheng <famz@redhat.com>
9ae3a8
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
9ae3a8
BZ: 1129893
9ae3a8
9ae3a8
With BDRVQcowState.refcount_block_bits, we don't need REFCOUNT_SHIFT
9ae3a8
anymore.
9ae3a8
9ae3a8
Signed-off-by: Max Reitz <mreitz@redhat.com>
9ae3a8
Reviewed-by: Eric Blake <eblake@redhat.com>
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
(cherry picked from commit 17bd5f472754acd2458b53dc02a30d5651e6dd79)
9ae3a8
9ae3a8
Signed-off-by: Max Reitz <mreitz@redhat.com>
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 block/qcow2-refcount.c | 32 ++++++++++++++------------------
9ae3a8
 block/qcow2.c          |  2 +-
9ae3a8
 block/qcow2.h          |  2 --
9ae3a8
 3 files changed, 15 insertions(+), 21 deletions(-)
9ae3a8
9ae3a8
diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c
9ae3a8
index fe36bbd..87b932c 100644
9ae3a8
--- a/block/qcow2-refcount.c
9ae3a8
+++ b/block/qcow2-refcount.c
9ae3a8
@@ -94,7 +94,7 @@ static int get_refcount(BlockDriverState *bs, int64_t cluster_index)
9ae3a8
     uint16_t *refcount_block;
9ae3a8
     uint16_t refcount;
9ae3a8
 
9ae3a8
-    refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
9ae3a8
+    refcount_table_index = cluster_index >> s->refcount_block_bits;
9ae3a8
     if (refcount_table_index >= s->refcount_table_size)
9ae3a8
         return 0;
9ae3a8
     refcount_block_offset = s->refcount_table[refcount_table_index];
9ae3a8
@@ -114,8 +114,7 @@ static int get_refcount(BlockDriverState *bs, int64_t cluster_index)
9ae3a8
         return ret;
9ae3a8
     }
9ae3a8
 
9ae3a8
-    block_index = cluster_index &
9ae3a8
-        ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
9ae3a8
+    block_index = cluster_index & (s->refcount_block_size - 1);
9ae3a8
     refcount = be16_to_cpu(refcount_block[block_index]);
9ae3a8
 
9ae3a8
     ret = qcow2_cache_put(bs, s->refcount_block_cache,
9ae3a8
@@ -150,8 +149,8 @@ static unsigned int next_refcount_table_size(BDRVQcowState *s,
9ae3a8
 static int in_same_refcount_block(BDRVQcowState *s, uint64_t offset_a,
9ae3a8
     uint64_t offset_b)
9ae3a8
 {
9ae3a8
-    uint64_t block_a = offset_a >> (2 * s->cluster_bits - REFCOUNT_SHIFT);
9ae3a8
-    uint64_t block_b = offset_b >> (2 * s->cluster_bits - REFCOUNT_SHIFT);
9ae3a8
+    uint64_t block_a = offset_a >> (s->cluster_bits + s->refcount_block_bits);
9ae3a8
+    uint64_t block_b = offset_b >> (s->cluster_bits + s->refcount_block_bits);
9ae3a8
 
9ae3a8
     return (block_a == block_b);
9ae3a8
 }
9ae3a8
@@ -172,7 +171,7 @@ static int alloc_refcount_block(BlockDriverState *bs,
9ae3a8
     BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC);
9ae3a8
 
9ae3a8
     /* Find the refcount block for the given cluster */
9ae3a8
-    refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
9ae3a8
+    refcount_table_index = cluster_index >> s->refcount_block_bits;
9ae3a8
 
9ae3a8
     if (refcount_table_index < s->refcount_table_size) {
9ae3a8
 
9ae3a8
@@ -249,7 +248,7 @@ static int alloc_refcount_block(BlockDriverState *bs,
9ae3a8
 
9ae3a8
         /* The block describes itself, need to update the cache */
9ae3a8
         int block_index = (new_block >> s->cluster_bits) &
9ae3a8
-            ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
9ae3a8
+            (s->refcount_block_size - 1);
9ae3a8
         (*refcount_block)[block_index] = cpu_to_be16(1);
9ae3a8
     } else {
9ae3a8
         /* Described somewhere else. This can recurse at most twice before we
9ae3a8
@@ -321,8 +320,7 @@ static int alloc_refcount_block(BlockDriverState *bs,
9ae3a8
     BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_GROW);
9ae3a8
 
9ae3a8
     /* Calculate the number of refcount blocks needed so far */
9ae3a8
-    uint64_t refcount_block_clusters = 1 << (s->cluster_bits - REFCOUNT_SHIFT);
9ae3a8
-    uint64_t blocks_used = DIV_ROUND_UP(cluster_index, refcount_block_clusters);
9ae3a8
+    uint64_t blocks_used = DIV_ROUND_UP(cluster_index, s->refcount_block_size);
9ae3a8
 
9ae3a8
     if (blocks_used > QCOW_MAX_REFTABLE_SIZE / sizeof(uint64_t)) {
9ae3a8
         return -EFBIG;
9ae3a8
@@ -336,14 +334,14 @@ static int alloc_refcount_block(BlockDriverState *bs,
9ae3a8
         uint64_t table_clusters =
9ae3a8
             size_to_clusters(s, table_size * sizeof(uint64_t));
9ae3a8
         blocks_clusters = 1 +
9ae3a8
-            ((table_clusters + refcount_block_clusters - 1)
9ae3a8
-            / refcount_block_clusters);
9ae3a8
+            ((table_clusters + s->refcount_block_size - 1)
9ae3a8
+            / s->refcount_block_size);
9ae3a8
         uint64_t meta_clusters = table_clusters + blocks_clusters;
9ae3a8
 
9ae3a8
         last_table_size = table_size;
9ae3a8
         table_size = next_refcount_table_size(s, blocks_used +
9ae3a8
-            ((meta_clusters + refcount_block_clusters - 1)
9ae3a8
-            / refcount_block_clusters));
9ae3a8
+            ((meta_clusters + s->refcount_block_size - 1)
9ae3a8
+            / s->refcount_block_size));
9ae3a8
 
9ae3a8
     } while (last_table_size != table_size);
9ae3a8
 
9ae3a8
@@ -353,7 +351,7 @@ static int alloc_refcount_block(BlockDriverState *bs,
9ae3a8
 #endif
9ae3a8
 
9ae3a8
     /* Create the new refcount table and blocks */
9ae3a8
-    uint64_t meta_offset = (blocks_used * refcount_block_clusters) *
9ae3a8
+    uint64_t meta_offset = (blocks_used * s->refcount_block_size) *
9ae3a8
         s->cluster_size;
9ae3a8
     uint64_t table_offset = meta_offset + blocks_clusters * s->cluster_size;
9ae3a8
     uint16_t *new_blocks = g_malloc0(blocks_clusters * s->cluster_size);
9ae3a8
@@ -545,8 +543,7 @@ static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs,
9ae3a8
     {
9ae3a8
         int block_index, refcount;
9ae3a8
         int64_t cluster_index = cluster_offset >> s->cluster_bits;
9ae3a8
-        int64_t table_index =
9ae3a8
-            cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
9ae3a8
+        int64_t table_index = cluster_index >> s->refcount_block_bits;
9ae3a8
 
9ae3a8
         /* Load the refcount block and allocate it if needed */
9ae3a8
         if (table_index != old_table_index) {
9ae3a8
@@ -568,8 +565,7 @@ static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs,
9ae3a8
         qcow2_cache_entry_mark_dirty(s->refcount_block_cache, refcount_block);
9ae3a8
 
9ae3a8
         /* we can update the count and save it */
9ae3a8
-        block_index = cluster_index &
9ae3a8
-            ((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
9ae3a8
+        block_index = cluster_index & (s->refcount_block_size - 1);
9ae3a8
 
9ae3a8
         refcount = be16_to_cpu(refcount_block[block_index]);
9ae3a8
         refcount += addend;
9ae3a8
diff --git a/block/qcow2.c b/block/qcow2.c
9ae3a8
index 6c3c6bc..4e60077 100644
9ae3a8
--- a/block/qcow2.c
9ae3a8
+++ b/block/qcow2.c
9ae3a8
@@ -1678,7 +1678,7 @@ static int qcow2_create2(const char *filename, int64_t total_size,
9ae3a8
         .l1_size                    = cpu_to_be32(0),
9ae3a8
         .refcount_table_offset      = cpu_to_be64(cluster_size),
9ae3a8
         .refcount_table_clusters    = cpu_to_be32(1),
9ae3a8
-        .refcount_order             = cpu_to_be32(3 + REFCOUNT_SHIFT),
9ae3a8
+        .refcount_order             = cpu_to_be32(4),
9ae3a8
         .header_length              = cpu_to_be32(sizeof(*header)),
9ae3a8
     };
9ae3a8
 
9ae3a8
diff --git a/block/qcow2.h b/block/qcow2.h
9ae3a8
index b65f629..b210a7f 100644
9ae3a8
--- a/block/qcow2.h
9ae3a8
+++ b/block/qcow2.h
9ae3a8
@@ -59,8 +59,6 @@
9ae3a8
 /* The cluster reads as all zeros */
9ae3a8
 #define QCOW_OFLAG_ZERO (1LL << 0)
9ae3a8
 
9ae3a8
-#define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */
9ae3a8
-
9ae3a8
 #define MIN_CLUSTER_BITS 9
9ae3a8
 #define MAX_CLUSTER_BITS 21
9ae3a8
 
9ae3a8
-- 
9ae3a8
1.8.3.1
9ae3a8