qcow2: Drop REFCOUNT_SHIFT
With BDRVQcowState.refcount_block_bits, we don't need REFCOUNT_SHIFT anymore. Signed-off-by: Max Reitz <mreitz@redhat.com> Reviewed-by: Eric Blake <eblake@redhat.com> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
This commit is contained in:
parent
234764eed1
commit
17bd5f4727
@ -100,7 +100,7 @@ static int get_refcount(BlockDriverState *bs, int64_t cluster_index)
|
|||||||
uint16_t *refcount_block;
|
uint16_t *refcount_block;
|
||||||
uint16_t refcount;
|
uint16_t refcount;
|
||||||
|
|
||||||
refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
|
refcount_table_index = cluster_index >> s->refcount_block_bits;
|
||||||
if (refcount_table_index >= s->refcount_table_size)
|
if (refcount_table_index >= s->refcount_table_size)
|
||||||
return 0;
|
return 0;
|
||||||
refcount_block_offset =
|
refcount_block_offset =
|
||||||
@ -121,8 +121,7 @@ static int get_refcount(BlockDriverState *bs, int64_t cluster_index)
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
block_index = cluster_index &
|
block_index = cluster_index & (s->refcount_block_size - 1);
|
||||||
((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
|
|
||||||
refcount = be16_to_cpu(refcount_block[block_index]);
|
refcount = be16_to_cpu(refcount_block[block_index]);
|
||||||
|
|
||||||
ret = qcow2_cache_put(bs, s->refcount_block_cache,
|
ret = qcow2_cache_put(bs, s->refcount_block_cache,
|
||||||
@ -157,8 +156,8 @@ static unsigned int next_refcount_table_size(BDRVQcowState *s,
|
|||||||
static int in_same_refcount_block(BDRVQcowState *s, uint64_t offset_a,
|
static int in_same_refcount_block(BDRVQcowState *s, uint64_t offset_a,
|
||||||
uint64_t offset_b)
|
uint64_t offset_b)
|
||||||
{
|
{
|
||||||
uint64_t block_a = offset_a >> (2 * s->cluster_bits - REFCOUNT_SHIFT);
|
uint64_t block_a = offset_a >> (s->cluster_bits + s->refcount_block_bits);
|
||||||
uint64_t block_b = offset_b >> (2 * s->cluster_bits - REFCOUNT_SHIFT);
|
uint64_t block_b = offset_b >> (s->cluster_bits + s->refcount_block_bits);
|
||||||
|
|
||||||
return (block_a == block_b);
|
return (block_a == block_b);
|
||||||
}
|
}
|
||||||
@ -179,7 +178,7 @@ static int alloc_refcount_block(BlockDriverState *bs,
|
|||||||
BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC);
|
BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_ALLOC);
|
||||||
|
|
||||||
/* Find the refcount block for the given cluster */
|
/* Find the refcount block for the given cluster */
|
||||||
refcount_table_index = cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
|
refcount_table_index = cluster_index >> s->refcount_block_bits;
|
||||||
|
|
||||||
if (refcount_table_index < s->refcount_table_size) {
|
if (refcount_table_index < s->refcount_table_size) {
|
||||||
|
|
||||||
@ -256,7 +255,7 @@ static int alloc_refcount_block(BlockDriverState *bs,
|
|||||||
|
|
||||||
/* The block describes itself, need to update the cache */
|
/* The block describes itself, need to update the cache */
|
||||||
int block_index = (new_block >> s->cluster_bits) &
|
int block_index = (new_block >> s->cluster_bits) &
|
||||||
((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
|
(s->refcount_block_size - 1);
|
||||||
(*refcount_block)[block_index] = cpu_to_be16(1);
|
(*refcount_block)[block_index] = cpu_to_be16(1);
|
||||||
} else {
|
} else {
|
||||||
/* Described somewhere else. This can recurse at most twice before we
|
/* Described somewhere else. This can recurse at most twice before we
|
||||||
@ -328,8 +327,7 @@ static int alloc_refcount_block(BlockDriverState *bs,
|
|||||||
BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_GROW);
|
BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_GROW);
|
||||||
|
|
||||||
/* Calculate the number of refcount blocks needed so far */
|
/* Calculate the number of refcount blocks needed so far */
|
||||||
uint64_t refcount_block_clusters = 1 << (s->cluster_bits - REFCOUNT_SHIFT);
|
uint64_t blocks_used = DIV_ROUND_UP(cluster_index, s->refcount_block_size);
|
||||||
uint64_t blocks_used = DIV_ROUND_UP(cluster_index, refcount_block_clusters);
|
|
||||||
|
|
||||||
if (blocks_used > QCOW_MAX_REFTABLE_SIZE / sizeof(uint64_t)) {
|
if (blocks_used > QCOW_MAX_REFTABLE_SIZE / sizeof(uint64_t)) {
|
||||||
return -EFBIG;
|
return -EFBIG;
|
||||||
@ -343,14 +341,14 @@ static int alloc_refcount_block(BlockDriverState *bs,
|
|||||||
uint64_t table_clusters =
|
uint64_t table_clusters =
|
||||||
size_to_clusters(s, table_size * sizeof(uint64_t));
|
size_to_clusters(s, table_size * sizeof(uint64_t));
|
||||||
blocks_clusters = 1 +
|
blocks_clusters = 1 +
|
||||||
((table_clusters + refcount_block_clusters - 1)
|
((table_clusters + s->refcount_block_size - 1)
|
||||||
/ refcount_block_clusters);
|
/ s->refcount_block_size);
|
||||||
uint64_t meta_clusters = table_clusters + blocks_clusters;
|
uint64_t meta_clusters = table_clusters + blocks_clusters;
|
||||||
|
|
||||||
last_table_size = table_size;
|
last_table_size = table_size;
|
||||||
table_size = next_refcount_table_size(s, blocks_used +
|
table_size = next_refcount_table_size(s, blocks_used +
|
||||||
((meta_clusters + refcount_block_clusters - 1)
|
((meta_clusters + s->refcount_block_size - 1)
|
||||||
/ refcount_block_clusters));
|
/ s->refcount_block_size));
|
||||||
|
|
||||||
} while (last_table_size != table_size);
|
} while (last_table_size != table_size);
|
||||||
|
|
||||||
@ -360,7 +358,7 @@ static int alloc_refcount_block(BlockDriverState *bs,
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Create the new refcount table and blocks */
|
/* Create the new refcount table and blocks */
|
||||||
uint64_t meta_offset = (blocks_used * refcount_block_clusters) *
|
uint64_t meta_offset = (blocks_used * s->refcount_block_size) *
|
||||||
s->cluster_size;
|
s->cluster_size;
|
||||||
uint64_t table_offset = meta_offset + blocks_clusters * s->cluster_size;
|
uint64_t table_offset = meta_offset + blocks_clusters * s->cluster_size;
|
||||||
uint64_t *new_table = g_try_new0(uint64_t, table_size);
|
uint64_t *new_table = g_try_new0(uint64_t, table_size);
|
||||||
@ -560,8 +558,7 @@ static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs,
|
|||||||
{
|
{
|
||||||
int block_index, refcount;
|
int block_index, refcount;
|
||||||
int64_t cluster_index = cluster_offset >> s->cluster_bits;
|
int64_t cluster_index = cluster_offset >> s->cluster_bits;
|
||||||
int64_t table_index =
|
int64_t table_index = cluster_index >> s->refcount_block_bits;
|
||||||
cluster_index >> (s->cluster_bits - REFCOUNT_SHIFT);
|
|
||||||
|
|
||||||
/* Load the refcount block and allocate it if needed */
|
/* Load the refcount block and allocate it if needed */
|
||||||
if (table_index != old_table_index) {
|
if (table_index != old_table_index) {
|
||||||
@ -583,8 +580,7 @@ static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs,
|
|||||||
qcow2_cache_entry_mark_dirty(s->refcount_block_cache, refcount_block);
|
qcow2_cache_entry_mark_dirty(s->refcount_block_cache, refcount_block);
|
||||||
|
|
||||||
/* we can update the count and save it */
|
/* we can update the count and save it */
|
||||||
block_index = cluster_index &
|
block_index = cluster_index & (s->refcount_block_size - 1);
|
||||||
((1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1);
|
|
||||||
|
|
||||||
refcount = be16_to_cpu(refcount_block[block_index]);
|
refcount = be16_to_cpu(refcount_block[block_index]);
|
||||||
refcount += addend;
|
refcount += addend;
|
||||||
|
@ -1874,7 +1874,7 @@ static int qcow2_create2(const char *filename, int64_t total_size,
|
|||||||
.l1_size = cpu_to_be32(0),
|
.l1_size = cpu_to_be32(0),
|
||||||
.refcount_table_offset = cpu_to_be64(cluster_size),
|
.refcount_table_offset = cpu_to_be64(cluster_size),
|
||||||
.refcount_table_clusters = cpu_to_be32(1),
|
.refcount_table_clusters = cpu_to_be32(1),
|
||||||
.refcount_order = cpu_to_be32(3 + REFCOUNT_SHIFT),
|
.refcount_order = cpu_to_be32(4),
|
||||||
.header_length = cpu_to_be32(sizeof(*header)),
|
.header_length = cpu_to_be32(sizeof(*header)),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -59,8 +59,6 @@
|
|||||||
/* The cluster reads as all zeros */
|
/* The cluster reads as all zeros */
|
||||||
#define QCOW_OFLAG_ZERO (1ULL << 0)
|
#define QCOW_OFLAG_ZERO (1ULL << 0)
|
||||||
|
|
||||||
#define REFCOUNT_SHIFT 1 /* refcount size is 2 bytes */
|
|
||||||
|
|
||||||
#define MIN_CLUSTER_BITS 9
|
#define MIN_CLUSTER_BITS 9
|
||||||
#define MAX_CLUSTER_BITS 21
|
#define MAX_CLUSTER_BITS 21
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user