From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:37488) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UGW04-0003ab-0N for qemu-devel@nongnu.org; Fri, 15 Mar 2013 10:52:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UGW00-0005Ni-Oo for qemu-devel@nongnu.org; Fri, 15 Mar 2013 10:52:51 -0400 Received: from nodalink.pck.nerim.net ([62.212.105.220]:59653 helo=paradis.irqsave.net) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UGW00-0005Na-8W for qemu-devel@nongnu.org; Fri, 15 Mar 2013 10:52:48 -0400 From: =?UTF-8?q?Beno=C3=AEt=20Canet?= Date: Fri, 15 Mar 2013 15:49:45 +0100 Message-Id: <1363358986-8360-32-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 31/32] qcow2: Add qcow2_co_dedup_resume to restart deduplication. 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 --- block/qcow2-dedup.c | 179 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 179 insertions(+) diff --git a/block/qcow2-dedup.c b/block/qcow2-dedup.c index c2dd145..93917d5 100644 --- a/block/qcow2-dedup.c +++ b/block/qcow2-dedup.c @@ -37,6 +37,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, @@ -1092,6 +1093,174 @@ bool qcow2_dedup_is_running(BlockDriverState *bs) return s->has_dedup && s->dedup_status == DEDUP_STATUS_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); +} + +/* 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_hash_node_new(&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_PENDING_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 = DEDUP_STATUS_STARTED; + qemu_co_mutex_unlock(&s->lock); + + return; + +fail: + qemu_co_mutex_lock(&s->lock); + s->dedup_status = DEDUP_STATUS_STOPPED; + qcow2_dedup_reset(bs); + qemu_co_mutex_unlock(&s->lock); +} + static gint qcow2_dedup_compare_by_hash(gconstpointer a, gconstpointer b, gpointer data) @@ -1142,6 +1311,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; @@ -1162,6 +1337,10 @@ int qcow2_dedup_init(BlockDriverState *bs) s->dedup_status = DEDUP_STATUS_STARTING; + /* resume deduplication */ + s->dedup_resume_co = qemu_coroutine_create(qcow2_co_dedup_resume); + qemu_coroutine_enter(s->dedup_resume_co, bs); + return 0; } -- 1.7.10.4