From patchwork Wed Jan 16 15:48:14 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: 212970 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 02CF22C0082 for ; Thu, 17 Jan 2013 05:48:49 +1100 (EST) Received: from localhost ([::1]:59386 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TvVHs-0000ms-0N for incoming@patchwork.ozlabs.org; Wed, 16 Jan 2013 10:52:24 -0500 Received: from eggs.gnu.org ([208.118.235.92]:60187) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TvVH5-00083n-93 for qemu-devel@nongnu.org; Wed, 16 Jan 2013 10:51:42 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TvVH1-0005md-R9 for qemu-devel@nongnu.org; Wed, 16 Jan 2013 10:51:35 -0500 Received: from nodalink.pck.nerim.net ([62.212.105.220]:42976 helo=paradis.irqsave.net) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TvVH1-0005mH-FI for qemu-devel@nongnu.org; Wed, 16 Jan 2013 10:51:31 -0500 Received: by paradis.irqsave.net (Postfix, from userid 1002) id 544F887431F; Wed, 16 Jan 2013 17:23:07 +0100 (CET) Received: from localhost.localdomain (unknown [192.168.77.1]) by paradis.irqsave.net (Postfix) with ESMTP id B0FF987433C; Wed, 16 Jan 2013 17:19:48 +0100 (CET) From: =?UTF-8?q?Beno=C3=AEt=20Canet?= To: qemu-devel@nongnu.org Date: Wed, 16 Jan 2013 16:48:14 +0100 Message-Id: <1358351321-4891-36-git-send-email-benoit@irqsave.net> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1358351321-4891-1-git-send-email-benoit@irqsave.net> References: <1358351321-4891-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 V5 35/62] qcow2: Add qcow2_co_dedup_resume to restart 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 --- block/qcow2-dedup.c | 180 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 180 insertions(+) diff --git a/block/qcow2-dedup.c b/block/qcow2-dedup.c index 35fcc01..6cd1af4 100644 --- a/block/qcow2-dedup.c +++ b/block/qcow2-dedup.c @@ -34,6 +34,7 @@ #include #endif +static void qcow2_dedup_reset(BlockDriverState *bs); static int qcow2_dedup_read_write_hash(BlockDriverState *bs, QCowHash *hash, uint64_t *first_logical_sect, @@ -1020,6 +1021,175 @@ bool qcow2_dedup_is_running(BlockDriverState *bs) return s->has_dedup && s->dedup_status == QCOW_DEDUP_STARTED; } +static bool hash_is_null(QCowHash *hash) +{ + QCowHash null_hash; + memset(&null_hash.data, 0, HASH_LENGTH); + return !memcmp(hash->data, null_hash.data, HASH_LENGTH); +} + +static void qcow2_dedup_insert_hash_node(BlockDriverState *bs, + QCowHashNode *hash_node) +{ + BDRVQcowState *s = bs->opaque; + + g_tree_insert(s->dedup_tree_by_hash, &hash_node->hash, hash_node); + g_tree_insert(s->dedup_tree_by_sect, &hash_node->physical_sect, hash_node); +} + +/* This load the QCowHashNode corresponding to a given cluster index into ram + * + * @index: index of the given physical sector + * @ret: 0 on succes, negative on error + */ +static int qcow2_load_cluster_hash(BlockDriverState *bs, + uint64_t index) +{ + BDRVQcowState *s = bs->opaque; + int ret = 0; + QCowHash hash; + uint64_t first_logical_sect; + QCowHashNode *hash_node; + + /* get the hash */ + ret = qcow2_dedup_read_write_hash(bs, &hash, + &first_logical_sect, + index * s->cluster_sectors, + false); + + if (ret < 0) { + error_report("Failed to load deduplication hash."); + return ret; + } + + /* if the hash is null don't load it */ + if (hash_is_null(&hash)) { + return ret; + } + + hash_node = qcow2_dedup_build_qcow_hash_node(&hash, + index * s->cluster_sectors, + first_logical_sect); + qcow2_dedup_insert_hash_node(bs, hash_node); + + return 0; +} + +/* Load all the actives hashes into RAM + * + * @ret: 0 on success, negative on error + */ +static int qcow2_load_valid_hashes(BlockDriverState *bs) +{ + BDRVQcowState *s = bs->opaque; + uint64_t max_clusters, i; + int nb_hash_in_hash_block = s->hash_block_size / (HASH_LENGTH + 8); + int ret = 0; + + max_clusters = s->dedup_table_size * nb_hash_in_hash_block; + + /* load all the hash stored to disk in memory */ + for (i = 0; i < max_clusters; i++) { + if (!(i % nb_hash_in_hash_block)) { + co_sleep_ns(rt_clock, s->dedup_co_delay); + } + qemu_co_mutex_lock(&s->lock); + ret = qcow2_load_cluster_hash(bs, i); + qemu_co_mutex_unlock(&s->lock); + if (ret < 0) { + return ret; + } + } + + return 0; +} + +static int qcow2_drop_to_dedup_stale_hash(BlockDriverState *bs, + uint64_t index) +{ + int ret = 0; + bool to_dedup; + uint64_t physical_sect; + + to_dedup = qcow2_is_cluster_to_dedup(bs, index, &physical_sect, &ret); + + if (ret < 0) { + return ret; + } + + if (!to_dedup) { + return 0; + } + + qcow2_remove_hash_node_by_sector(bs, physical_sect); + return 0; +} + +/* For each l2 entry marked as QCOW_OFLAG_TO_DEDUP drop the obsolete hash + * from the trees + * + * @ret: 0 on success, negative on error + */ +static int qcow2_drop_to_dedup_hashes(BlockDriverState *bs) +{ + BDRVQcowState *s = bs->opaque; + uint64_t i; + int ret = 0; + + /* for each l2 entry */ + for (i = 0; i < s->l2_size * s->l1_size; i++) { + if (!(i % s->l2_size)) { + co_sleep_ns(rt_clock, s->dedup_co_delay); + } + qemu_co_mutex_lock(&s->lock); + ret = qcow2_drop_to_dedup_stale_hash(bs, i); + qemu_co_mutex_unlock(&s->lock); + + if (ret < 0) { + return ret; + } + } + + return 0; +} + +/* + * This coroutine resume deduplication + * + * @data: the given BlockDriverState + * @ret: NULL + */ +static void coroutine_fn qcow2_co_dedup_resume(void *opaque) +{ + BlockDriverState *bs = opaque; + BDRVQcowState *s = bs->opaque; + int ret = 0; + + ret = qcow2_load_valid_hashes(bs); + + if (ret < 0) { + goto fail; + } + + ret = qcow2_drop_to_dedup_hashes(bs); + + if (ret < 0) { + goto fail; + } + + qemu_co_mutex_lock(&s->lock); + s->dedup_status = QCOW_DEDUP_STARTED; + qemu_co_mutex_unlock(&s->lock); + + return; + +fail: + qemu_co_mutex_lock(&s->lock); + s->dedup_status = QCOW_DEDUP_STOPPED; + qcow2_dedup_reset(bs); + qemu_co_mutex_unlock(&s->lock); +} + static gint qcow2_dedup_compare_by_hash(gconstpointer a, gconstpointer b, gpointer data) @@ -1089,6 +1259,12 @@ static void qcow2_dedup_free(BlockDriverState *bs) g_tree_destroy(s->dedup_tree_by_hash); } +static void qcow2_dedup_reset(BlockDriverState *bs) +{ + qcow2_dedup_free(bs); + qcow2_dedup_alloc(bs); +} + int qcow2_dedup_init(BlockDriverState *bs) { BDRVQcowState *s = bs->opaque; @@ -1109,6 +1285,10 @@ int qcow2_dedup_init(BlockDriverState *bs) s->dedup_status = QCOW_DEDUP_STARTING; + /* resume deduplication */ + s->dedup_resume_co = qemu_coroutine_create(qcow2_co_dedup_resume); + qemu_coroutine_enter(s->dedup_resume_co, bs); + return 0; }