@@ -1014,20 +1014,88 @@ void coroutine_fn qcow2_co_load_dedup_hashes(void *opaque)
}
}
+static gint qcow2_dedup_compare_by_hash(gconstpointer a,
+ gconstpointer b,
+ gpointer data)
+{
+ QCowHash *hash_a = (QCowHash *) a;
+ QCowHash *hash_b = (QCowHash *) b;
+ return memcmp(hash_a->data, hash_b->data, HASH_LENGTH);
+}
+
+static void qcow2_dedup_destroy_qcow_hash_node(gpointer p)
+{
+ QCowHashNode *hash_node = (QCowHashNode *) p;
+ g_free(hash_node);
+}
+
+static gint qcow2_dedup_compare_by_offset(gconstpointer a,
+ gconstpointer b,
+ gpointer data)
+{
+ uint64_t offset_a = *((uint64_t *) a);
+ uint64_t offset_b = *((uint64_t *) b);
+
+ if (offset_a > offset_b) {
+ return 1;
+ }
+ if (offset_a < offset_b) {
+ return -1;
+ }
+ return 0;
+}
+
int qcow2_dedup_init(BlockDriverState *bs)
{
BDRVQcowState *s = bs->opaque;
- return qcow2_do_table_init(bs,
- &s->dedup_table,
- s->dedup_table_offset,
- s->dedup_table_size,
- false);
+ Coroutine *co;
+ int ret;
+
+ s->has_dedup = true;
+
+ ret = qcow2_do_table_init(bs,
+ &s->dedup_table,
+ s->dedup_table_offset,
+ s->dedup_table_size,
+ false);
+
+ if (ret < 0) {
+ return ret;
+ }
+
+ /* if we are read-only we don't deduplicate anything */
+ if (bs->read_only) {
+ return 0;
+ }
+
+ s->dedup_tree_by_hash = g_tree_new_full(qcow2_dedup_compare_by_hash, NULL,
+ NULL,
+ qcow2_dedup_destroy_qcow_hash_node);
+ s->dedup_tree_by_sect = g_tree_new_full(qcow2_dedup_compare_by_offset,
+ NULL, NULL, NULL);
+
+ s->dedup_cluster_cache = qcow2_cache_create(bs, DEDUP_CACHE_SIZE,
+ s->hash_block_size);
+
+ /* load asynchronously the hashes */
+ co = qemu_coroutine_create(qcow2_co_load_dedup_hashes);
+ qemu_coroutine_enter(co, bs);
+ return 0;
}
void qcow2_dedup_close(BlockDriverState *bs)
{
BDRVQcowState *s = bs->opaque;
g_free(s->dedup_table);
+
+ if (bs->read_only) {
+ return;
+ }
+
+ qcow2_cache_flush(bs, s->dedup_cluster_cache);
+ qcow2_cache_destroy(bs, s->dedup_cluster_cache);
+ g_tree_destroy(s->dedup_tree_by_sect);
+ g_tree_destroy(s->dedup_tree_by_hash);
}
/* Clean the last reference to a given cluster when it's refcount is zero
@@ -539,6 +539,13 @@ static int qcow2_open(BlockDriverState *bs, int flags)
}
}
+ if (s->incompatible_features & QCOW2_INCOMPAT_DEDUP) {
+ ret = qcow2_dedup_init(bs);
+ if (ret < 0) {
+ goto fail;
+ }
+ }
+
#ifdef DEBUG_ALLOC
{
BdrvCheckResult result = {0};
@@ -1003,11 +1010,11 @@ fail:
static void qcow2_close(BlockDriverState *bs)
{
BDRVQcowState *s = bs->opaque;
+
g_free(s->l1_table);
if (s->has_dedup) {
- qcow2_cache_flush(bs, s->dedup_cluster_cache);
- qcow2_cache_destroy(bs, s->dedup_cluster_cache);
+ qcow2_dedup_close(bs);
}
qcow2_cache_flush(bs, s->l2_table_cache);
@@ -1498,8 +1505,10 @@ static int qcow2_create2(const char *filename, int64_t total_size,
}
/* minimal init */
- s->dedup_cluster_cache = qcow2_cache_create(bs, DEDUP_CACHE_SIZE,
- s->hash_block_size);
+ ret = qcow2_dedup_init(bs);
+ if (ret < 0) {
+ goto out;
+ }
}
/* Want a backing file? There you go.*/
Signed-off-by: Benoit Canet <benoit@irqsave.net> --- block/qcow2-dedup.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++---- block/qcow2.c | 17 ++++++++--- 2 files changed, 86 insertions(+), 9 deletions(-)