From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:37410) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UGVzl-000317-Sx for qemu-devel@nongnu.org; Fri, 15 Mar 2013 10:52:37 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UGVzh-0005Kq-Nk for qemu-devel@nongnu.org; Fri, 15 Mar 2013 10:52:33 -0400 Received: from nodalink.pck.nerim.net ([62.212.105.220]:59651 helo=paradis.irqsave.net) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UGVzh-0005KK-6V for qemu-devel@nongnu.org; Fri, 15 Mar 2013 10:52:29 -0400 From: =?UTF-8?q?Beno=C3=AEt=20Canet?= Date: Fri, 15 Mar 2013 15:49:44 +0100 Message-Id: <1363358986-8360-31-git-send-email-benoit@irqsave.net> In-Reply-To: <1363358986-8360-1-git-send-email-benoit@irqsave.net> References: <1363358986-8360-1-git-send-email-benoit@irqsave.net> Subject: [Qemu-devel] [RFC V7 30/32] qcow2: Add qcow2_dedup_init and qcow2_dedup_close. List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: kwolf@redhat.com, =?UTF-8?q?Beno=C3=AEt=20Canet?= , stefanha@redhat.com Signed-off-by: Benoit Canet --- block/qcow2-dedup.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++ block/qcow2.h | 2 ++ 2 files changed, 80 insertions(+) diff --git a/block/qcow2-dedup.c b/block/qcow2-dedup.c index 6ad9d0c..c2dd145 100644 --- a/block/qcow2-dedup.c +++ b/block/qcow2-dedup.c @@ -1091,3 +1091,81 @@ bool qcow2_dedup_is_running(BlockDriverState *bs) BDRVQcowState *s = bs->opaque; return s->has_dedup && s->dedup_status == DEDUP_STATUS_STARTED; } + +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 int qcow2_dedup_alloc(BlockDriverState *bs) +{ + BDRVQcowState *s = bs->opaque; + int ret; + + ret = qcow2_do_table_init(bs, + &s->dedup_table, + s->dedup_table_offset, + s->dedup_table_size, + false); + + if (ret < 0) { + return ret; + } + + s->dedup_tree_by_hash = g_tree_new_full(qcow2_dedup_compare_by_hash, NULL, + NULL, + qcow2_dedup_destroy_qcow_hash_node); + + s->dedup_cluster_cache = qcow2_cache_create(bs, DEDUP_CACHE_SIZE, + s->hash_block_size); + + return 0; +} + +static void qcow2_dedup_free(BlockDriverState *bs) +{ + BDRVQcowState *s = bs->opaque; + g_free(s->dedup_table); + + qcow2_cache_flush(bs, s->dedup_cluster_cache); + qcow2_cache_destroy(bs, s->dedup_cluster_cache); + g_tree_destroy(s->dedup_tree_by_hash); +} + +int qcow2_dedup_init(BlockDriverState *bs) +{ + BDRVQcowState *s = bs->opaque; + int ret = 0; + + s->has_dedup = true; + + ret = qcow2_dedup_alloc(bs); + + if (ret < 0) { + return ret; + } + + /* if we are read-only we don't load the deduplication table */ + if (bs->read_only) { + return 0; + } + + s->dedup_status = DEDUP_STATUS_STARTING; + + return 0; +} + +void qcow2_dedup_close(BlockDriverState *bs) +{ + qcow2_dedup_free(bs); +} diff --git a/block/qcow2.h b/block/qcow2.h index a430fe1..9275be1 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -500,5 +500,7 @@ int qcow2_dedup_store_new_hashes(BlockDriverState *bs, void qcow2_dedup_destroy_hash(BlockDriverState *bs, uint64_t cluster_index); bool qcow2_dedup_is_running(BlockDriverState *bs); +int qcow2_dedup_init(BlockDriverState *bs); +void qcow2_dedup_close(BlockDriverState *bs); #endif -- 1.7.10.4