From patchwork Wed Jan 2 16:16:32 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Beno=C3=AEt_Canet?= X-Patchwork-Id: 209095 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id B6D2C2C007E for ; Thu, 3 Jan 2013 03:50:39 +1100 (EST) Received: from localhost ([::1]:33720 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TqR20-0004VV-RZ for incoming@patchwork.ozlabs.org; Wed, 02 Jan 2013 11:19:04 -0500 Received: from eggs.gnu.org ([208.118.235.92]:53790) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TqR1t-0004Kw-Jd for qemu-devel@nongnu.org; Wed, 02 Jan 2013 11:19:00 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TqR1p-0006Qi-Ps for qemu-devel@nongnu.org; Wed, 02 Jan 2013 11:18:57 -0500 Received: from nodalink.pck.nerim.net ([62.212.105.220]:56281 helo=paradis.irqsave.net) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TqR1p-0006QY-Cc for qemu-devel@nongnu.org; Wed, 02 Jan 2013 11:18:53 -0500 Received: by paradis.irqsave.net (Postfix, from userid 1002) id EC7E9874314; Wed, 2 Jan 2013 17:45:21 +0100 (CET) Received: from localhost.localdomain (unknown [192.168.77.1]) by paradis.irqsave.net (Postfix) with ESMTP id 8C88187432B; Wed, 2 Jan 2013 17:42:40 +0100 (CET) From: =?UTF-8?q?Beno=C3=AEt=20Canet?= To: qemu-devel@nongnu.org Date: Wed, 2 Jan 2013 17:16:32 +0100 Message-Id: <1357143393-29832-30-git-send-email-benoit@irqsave.net> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1357143393-29832-1-git-send-email-benoit@irqsave.net> References: <1357143393-29832-1-git-send-email-benoit@irqsave.net> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 62.212.105.220 Cc: kwolf@redhat.com, pbonzini@redhat.com, =?UTF-8?q?Beno=C3=AEt=20Canet?= , stefanha@redhat.com Subject: [Qemu-devel] [RFC V4 29/30] qcow2: init and cleanup deduplication. X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Signed-off-by: Benoit Canet --- 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.*/