Patchwork [RFC,V3,23/24] qcow2: init and cleanup deduplication.

login
register
mail settings
Submitter Benoît Canet
Date Nov. 26, 2012, 1:05 p.m.
Message ID <1353935123-24199-24-git-send-email-benoit@irqsave.net>
Download mbox | patch
Permalink /patch/201690/
State New
Headers show

Comments

Benoît Canet - Nov. 26, 2012, 1:05 p.m.
Signed-off-by: Benoit Canet <benoit@irqsave.net>
---
 block/qcow2-dedup.c |   71 +++++++++++++++++++++++++++++++++++++++++++++++----
 block/qcow2.c       |   16 +++++++++---
 2 files changed, 79 insertions(+), 8 deletions(-)

Patch

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.*/