[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [RFC V3 23/24] qcow2: init and cleanup deduplication.
From: |
Benoît Canet |
Subject: |
[Qemu-devel] [RFC V3 23/24] qcow2: init and cleanup deduplication. |
Date: |
Mon, 26 Nov 2012 14:05:22 +0100 |
Signed-off-by: Benoit Canet <address@hidden>
---
block/qcow2-dedup.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++----
block/qcow2.c | 16 +++++++++---
2 files changed, 79 insertions(+), 8 deletions(-)
diff --git a/block/qcow2-dedup.c b/block/qcow2-dedup.c
index 097d71b..cdd1b83 100644
--- a/block/qcow2-dedup.c
+++ b/block/qcow2-dedup.c
@@ -795,20 +795,81 @@ int qcow2_dedup_grow_table(BlockDriverState *bs,
"dedup");
}
+static gint qcow2_dedup_compare_by_hash(gconstpointer a,
+ gconstpointer b,
+ gpointer data)
+{
+ uint8_t *hash_a = (uint8_t *) a;
+ uint8_t *hash_b = (uint8_t *) b;
+ return memcmp(hash_a, hash_b, HASH_LENGTH);
+}
+
+static void qcow2_dedup_destroy_qcow_hash_node(gpointer p)
+{
+ QCowHashNode *hash_node = (QCowHashNode *) p;
+ g_free(hash_node->hash);
+ 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;
+ 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_offset = g_tree_new_full(qcow2_dedup_compare_by_offset,
+ NULL, NULL, NULL);
+
+ s->dedup_cluster_cache = qcow2_cache_create(bs, DEDUP_CACHE_SIZE);
+
+ ret = qcow2_do_table_init(bs,
+ &s->dedup_table,
+ s->dedup_table_offset,
+ s->dedup_table_size,
+ false);
+
+ if (ret < 0) {
+ goto fail;
+ }
+
+ /* load asynchronously the hashes */
+ co = qemu_coroutine_create(qcow2_co_load_dedup_hashes);
+ qemu_coroutine_enter(co, bs);
+ return 0;
+
+fail:
+ qcow2_cache_destroy(bs, s->dedup_cluster_cache);
+ return ret;
}
void qcow2_dedup_close(BlockDriverState *bs)
{
BDRVQcowState *s = bs->opaque;
+ qcow2_cache_flush(bs, s->dedup_cluster_cache);
+ qcow2_cache_destroy(bs, s->dedup_cluster_cache);
g_free(s->dedup_table);
+ g_tree_destroy(s->dedup_tree_by_offset);
+ g_tree_destroy(s->dedup_tree_by_hash);
}
static void qcow2_dedup_refcount_limit_reached(BlockDriverState *bs,
diff --git a/block/qcow2.c b/block/qcow2.c
index d5f28dd..566cf1f 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -534,6 +534,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};
@@ -1000,11 +1007,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);
@@ -1457,7 +1464,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);
+ ret = qcow2_dedup_init(bs);
+ if (ret < 0) {
+ goto out;
+ }
}
/* Want a backing file? There you go.*/
--
1.7.10.4
- [Qemu-devel] [RFC V3 16/24] qcow2: Allow creation of images using deduplication., (continued)
- [Qemu-devel] [RFC V3 16/24] qcow2: Allow creation of images using deduplication., Benoît Canet, 2012/11/26
- [Qemu-devel] [RFC V3 17/24] qcow2: Behave correctly when refcount reach 0 or 2^16., Benoît Canet, 2012/11/26
- [Qemu-devel] [RFC V3 19/24] qcow2: Add verification of dedup table., Benoît Canet, 2012/11/26
- [Qemu-devel] [RFC V3 24/24] qemu-iotests: Filter dedup=on/off so existing tests don't break., Benoît Canet, 2012/11/26
- [Qemu-devel] [RFC V3 20/24] qcow2: Adapt checking of QCOW_OFLAG_COPIED for dedup., Benoît Canet, 2012/11/26
- [Qemu-devel] [RFC V3 22/24] qcow2: Do not overwrite existing entries with QCOW_OFLAG_COPIED., Benoît Canet, 2012/11/26
- [Qemu-devel] [RFC V3 07/24] qcow2: Add qcow2_dedup_write_new_hashes., Benoît Canet, 2012/11/26
- [Qemu-devel] [RFC V3 18/24] qcow2: Integrate deduplication in qcow2_co_writev loop., Benoît Canet, 2012/11/26
- [Qemu-devel] [RFC V3 08/24] qcow2: Implement qcow2_compute_cluster_hash., Benoît Canet, 2012/11/26
- [Qemu-devel] [RFC V3 21/24] qcow2: Add check_dedup_l2 in order to check l2 of dedup table., Benoît Canet, 2012/11/26
- [Qemu-devel] [RFC V3 23/24] qcow2: init and cleanup deduplication.,
Benoît Canet <=
- [Qemu-devel] [RFC V3 10/24] qcow2: create function to load deduplication hashes at startup., Benoît Canet, 2012/11/26
- [Qemu-devel] [RFC V3 15/24] block: Add dedup image create option., Benoît Canet, 2012/11/26