From patchwork Wed Jan 16 16:25:08 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: 212804 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 A8B3E2C007E for ; Thu, 17 Jan 2013 03:30:13 +1100 (EST) Received: from localhost ([::1]:44489 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TvVsQ-000831-9h for incoming@patchwork.ozlabs.org; Wed, 16 Jan 2013 11:30:10 -0500 Received: from eggs.gnu.org ([208.118.235.92]:42119) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TvVrC-00066s-W8 for qemu-devel@nongnu.org; Wed, 16 Jan 2013 11:28:59 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TvVr9-0007cl-42 for qemu-devel@nongnu.org; Wed, 16 Jan 2013 11:28:54 -0500 Received: from nodalink.pck.nerim.net ([62.212.105.220]:43402 helo=paradis.irqsave.net) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TvVr8-0007cV-OW for qemu-devel@nongnu.org; Wed, 16 Jan 2013 11:28:51 -0500 Received: by paradis.irqsave.net (Postfix, from userid 1002) id 5372A87432A; Wed, 16 Jan 2013 18:00:27 +0100 (CET) Received: from localhost.localdomain (unknown [192.168.77.1]) by paradis.irqsave.net (Postfix) with ESMTP id E5932874351; Wed, 16 Jan 2013 17:56:23 +0100 (CET) From: =?UTF-8?q?Beno=C3=AEt=20Canet?= To: qemu-devel@nongnu.org Date: Wed, 16 Jan 2013 17:25:08 +0100 Message-Id: <1358353518-5421-3-git-send-email-benoit@irqsave.net> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1358353518-5421-1-git-send-email-benoit@irqsave.net> References: <1358353518-5421-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 V1 02/12] qcow2: Add code to deduplicate cluster flagged with QCOW_OFLAG_TO_DEDUP. 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 | 126 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) diff --git a/block/qcow2-dedup.c b/block/qcow2-dedup.c index 4305746..dd320bf 100644 --- a/block/qcow2-dedup.c +++ b/block/qcow2-dedup.c @@ -1166,6 +1166,130 @@ static int qcow2_drop_to_dedup_hashes(BlockDriverState *bs) return 0; } +static bool qcow2_try_dedup_on_disk_cluster(BlockDriverState *bs, + QcowPersistantHash *phash, + uint64_t index, + uint64_t physical_sect) +{ + BDRVQcowState *s = bs->opaque; + uint8_t *data; + int ret = 0; + bool result = false; + + data = qemu_blockalign(bs, s->cluster_size); + + /* read the cluster data from disk */ + ret = bdrv_pread(bs->file, physical_sect << 9, data, s->cluster_size); + + if (ret < 0) { + goto exit; + } + + /* force computation of the hash */ + phash->reuse = false; + + ret = qcow2_try_dedup_cluster(bs, + phash, + index * s->cluster_sectors, + data, + 0); + + if (ret < 0) { + goto exit; + } + + /* cluster was deduplicated -> result is true */ + if (ret) { + result = true; + } + +exit: + qemu_vfree(data); + return result; +} + +static bool qcow2_process_undeduplicated_cluster(BlockDriverState *bs, + QcowPersistantHash *phash, + uint64_t index, + uint64_t physical_sect) +{ + BDRVQcowState *s = bs->opaque; + int ret = 0; + + /* not duplicated */ + ret = qcow2_store_hash(bs, &phash->hash, + index * s->cluster_sectors, + physical_sect); + + if (ret < 0) { + error_printf("Error while storing hash"); + return false; + } + + /* remove the QCOW_OFLAG_TO_DEDUP flag from l2 entry + * note: we should take care of setting QCOW_OFLAG_COPIED if needed + */ + ret = qcow2_dedup_link_l2(bs, index * s->cluster_sectors, + physical_sect, true); + + return ret == 0 ? true : false; +} + +static bool qcow2_process_to_dedup_cluster(BlockDriverState *bs, + uint64_t index) +{ + QcowPersistantHash phash; + bool to_dedup, deduplicated; + uint64_t physical_sect; + int ret = 0; + + to_dedup = qcow2_is_cluster_to_dedup(bs, index, &physical_sect, &ret); + + if (ret < 0) { + error_printf("Error checking if cluster must be deduplicated"); + return false; + } + + if (!to_dedup) { + return false; + } + + deduplicated = qcow2_try_dedup_on_disk_cluster(bs, + &phash, + index, + physical_sect); + + if (deduplicated) { + return true; + } + + qcow2_process_undeduplicated_cluster(bs, + &phash, + index, + physical_sect); + + return true; +} + +/* This function try to deduplicate clusters written when dedup was not running. + */ +static void qcow2_deduplicate_after_resuming(BlockDriverState *bs) +{ + BDRVQcowState *s = bs->opaque; + uint64_t i; + bool processed; + + /* for each l2 entry */ + for (i = 0; i < s->l2_size * s->l1_size; i++) { + qemu_co_mutex_lock(&s->lock); + processed = qcow2_process_to_dedup_cluster(bs, i); + qemu_co_mutex_unlock(&s->lock); + if (processed || !(i % s->l2_size)) { + co_sleep_ns(rt_clock, s->dedup_co_delay); + } + } +} + /* * This coroutine resume deduplication * @@ -1194,6 +1318,8 @@ static void coroutine_fn qcow2_co_dedup_resume(void *opaque) s->dedup_status = QCOW_DEDUP_STARTED; qemu_co_mutex_unlock(&s->lock); + qcow2_deduplicate_after_resuming(bs); + return; fail: