From patchwork Fri Mar 15 14:49:27 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: 228024 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 DE6172C00B2 for ; Sat, 16 Mar 2013 01:55:38 +1100 (EST) Received: from localhost ([::1]:56125 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UGW2j-0007Kx-3m for incoming@patchwork.ozlabs.org; Fri, 15 Mar 2013 10:55:37 -0400 Received: from eggs.gnu.org ([208.118.235.92]:36797) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UGVy8-0000No-ID for qemu-devel@nongnu.org; Fri, 15 Mar 2013 10:50:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UGVy4-0004ve-9l for qemu-devel@nongnu.org; Fri, 15 Mar 2013 10:50:52 -0400 Received: from nodalink.pck.nerim.net ([62.212.105.220]:59607 helo=paradis.irqsave.net) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UGVy3-0004tj-Mf for qemu-devel@nongnu.org; Fri, 15 Mar 2013 10:50:48 -0400 Received: by paradis.irqsave.net (Postfix, from userid 1002) id BF808874353; Fri, 15 Mar 2013 15:50:46 +0100 (CET) Received: from localhost.localdomain (unknown [192.168.77.1]) by paradis.irqsave.net (Postfix) with ESMTP id A1974874359; Fri, 15 Mar 2013 15:48:47 +0100 (CET) From: =?UTF-8?q?Beno=C3=AEt=20Canet?= To: qemu-devel@nongnu.org Date: Fri, 15 Mar 2013 15:49:27 +0100 Message-Id: <1363358986-8360-14-git-send-email-benoit@irqsave.net> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1363358986-8360-1-git-send-email-benoit@irqsave.net> References: <1363358986-8360-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, =?UTF-8?q?Beno=C3=AEt=20Canet?= , stefanha@redhat.com Subject: [Qemu-devel] [RFC V7 13/32] qcow2: make the deduplication forget a cluster hash when a cluster is to dedupe 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-cluster.c | 11 +++++++++-- block/qcow2-dedup.c | 40 ++++++++++++++++++++++++++++++++++++++++ block/qcow2.h | 2 ++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index 0d11ef0..3cbb64f 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -683,6 +683,7 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m) qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table); for (i = 0; i < m->nb_clusters; i++) { + uint64_t offset = cluster_offset + (i << s->cluster_bits); /* if two concurrent writes happen to the same unallocated cluster * each write allocates separate cluster and writes data concurrently. * The first one to complete updates l2 table with pointer to its @@ -692,8 +693,14 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m) if(l2_table[l2_index + i] != 0) old_cluster[j++] = l2_table[l2_index + i]; - l2_table[l2_index + i] = cpu_to_be64((cluster_offset + - (i << s->cluster_bits)) | m->l2_entry_flags); + l2_table[l2_index + i] = cpu_to_be64(offset | m->l2_entry_flags); + + /* make the deduplication forget the cluster to avoid making + * the dedup pointing to a cluster that has changed on it's back. + */ + if (m->to_deduplicate) { + qcow2_dedup_forget_cluster_by_sector(bs, offset >> 9); + } } diff --git a/block/qcow2-dedup.c b/block/qcow2-dedup.c index c106bd5..2aca01a 100644 --- a/block/qcow2-dedup.c +++ b/block/qcow2-dedup.c @@ -869,6 +869,46 @@ static inline bool is_hash_node_empty(QCowHashNode *hash_node) return hash_node->physical_sect & QCOW_DEDUP_FLAG_EMPTY; } +/* This function removes a hash_node from the trees given a physical sector + * + * @physical_sect: The physical sector of the cluster corresponding to the hash + */ +static void qcow2_remove_hash_node_by_sector(BlockDriverState *bs, + uint64_t physical_sect) +{ + BDRVQcowState *s = bs->opaque; + QCowHash hash; + int ret = 0; + uint8_t *data = qemu_blockalign(bs, s->cluster_sectors * BDRV_SECTOR_SIZE); + + + /* read the cluster data */ + ret = bdrv_pread(bs->file, physical_sect << 9, data, s->cluster_size); + + if (ret < 0) { + goto free_exit; + } + + ret = qcow2_compute_cluster_hash(bs, + &hash, + data); + + if (ret < 0) { + goto free_exit; + } + + g_tree_remove(s->dedup_tree_by_hash, &hash); + +free_exit: + qemu_vfree(data); +} + +void qcow2_dedup_forget_cluster_by_sector(BlockDriverState *bs, + uint64_t physical_sect) +{ + qcow2_remove_hash_node_by_sector(bs, physical_sect); +} + /* This function store a hash information to disk and RAM * * @hash: the QCowHash to process diff --git a/block/qcow2.h b/block/qcow2.h index 9f24b4c..2eab1a4 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -467,6 +467,8 @@ int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table); /* qcow2-dedup.c functions */ bool qcow2_must_deduplicate(BlockDriverState *bs); +void qcow2_dedup_forget_cluster_by_sector(BlockDriverState *bs, + uint64_t physical_sect); int qcow2_dedup_read_missing_and_concatenate(BlockDriverState *bs, QEMUIOVector *qiov, uint64_t sector,