[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [RFC V4 29/30] qcow2: init and cleanup deduplication.
From: |
Benoît Canet |
Subject: |
[Qemu-devel] [RFC V4 29/30] qcow2: init and cleanup deduplication. |
Date: |
Wed, 2 Jan 2013 17:16:32 +0100 |
Signed-off-by: Benoit Canet <address@hidden>
---
block/qcow2-dedup.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++----
block/qcow2.c | 17 ++++++++---
2 files changed, 86 insertions(+), 9 deletions(-)
diff --git a/block/qcow2-dedup.c b/block/qcow2-dedup.c
index bd8397e..da1a668 100644
--- a/block/qcow2-dedup.c
+++ b/block/qcow2-dedup.c
@@ -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
diff --git a/block/qcow2.c b/block/qcow2.c
index f1e0f5f..d534077 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -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.*/
--
1.7.10.4
- [Qemu-devel] [RFC V4 15/30] qcow2: Add qcow2_dedup_init and qcow2_dedup_close., (continued)
- [Qemu-devel] [RFC V4 15/30] qcow2: Add qcow2_dedup_init and qcow2_dedup_close., Benoît Canet, 2013/01/02
- [Qemu-devel] [RFC V4 17/30] block: Add qemu-img dedup create option., Benoît Canet, 2013/01/02
- [Qemu-devel] [RFC V4 21/30] qcow2: Add verification of dedup table., Benoît Canet, 2013/01/02
- [Qemu-devel] [RFC V4 23/30] qcow2: Add check_dedup_l2 in order to check l2 of dedup table., Benoît Canet, 2013/01/02
- [Qemu-devel] [RFC V4 19/30] qcow2: Integrate deduplication in qcow2_co_writev loop., Benoît Canet, 2013/01/02
- [Qemu-devel] [RFC V4 25/30] qcow2: Integrate SKEIN hash algorithm in deduplication., Benoît Canet, 2013/01/02
- [Qemu-devel] [RFC V4 24/30] qcow2: Do not overwrite existing entries with QCOW_OFLAG_COPIED., Benoît Canet, 2013/01/02
- [Qemu-devel] [RFC V4 30/30] qemu-iotests: Filter dedup=on/off so existing tests don't break., Benoît Canet, 2013/01/02
- [Qemu-devel] [RFC V4 29/30] qcow2: init and cleanup deduplication.,
Benoît Canet <=
- [Qemu-devel] [RFC V4 20/30] qcow2: Serialize write requests when deduplication is activated., Benoît Canet, 2013/01/02
- [Qemu-devel] [RFC V4 22/30] qcow2: Adapt checking of QCOW_OFLAG_COPIED for dedup., Benoît Canet, 2013/01/02
- [Qemu-devel] [RFC V4 28/30] qcow: Set dedup cluster block size to 64KB., Benoît Canet, 2013/01/02
- [Qemu-devel] [RFC V4 27/30] qcow2: Use large L2 table for deduplication., Benoît Canet, 2013/01/02
- [Qemu-devel] [RFC V4 12/30] qcow2: Load and save deduplication table header extension., Benoît Canet, 2013/01/02
- [Qemu-devel] [RFC V4 16/30] qcow2: Extract qcow2_add_feature and qcow2_remove_feature., Benoît Canet, 2013/01/02
- [Qemu-devel] [RFC V4 06/30] qcow2: Add qcow2_dedup and related functions, Benoît Canet, 2013/01/02
- [Qemu-devel] [RFC V4 26/30] qcow2: Add lazy refcounts to deduplication to prevent qcow2_cache_set_dependency loops, Benoît Canet, 2013/01/02
- [Qemu-devel] [RFC V4 18/30] qcow2: Behave correctly when refcount reach 0 or 2^16., Benoît Canet, 2013/01/02