From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:57604) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SCXtE-000360-5C for qemu-devel@nongnu.org; Tue, 27 Mar 2012 11:00:57 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SCXt7-0007Rg-VM for qemu-devel@nongnu.org; Tue, 27 Mar 2012 11:00:51 -0400 Received: from mx1.redhat.com ([209.132.183.28]:43004) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SCXt7-0007RS-Ms for qemu-devel@nongnu.org; Tue, 27 Mar 2012 11:00:45 -0400 From: Kevin Wolf Date: Tue, 27 Mar 2012 17:03:26 +0200 Message-Id: <1332860615-3047-8-git-send-email-kwolf@redhat.com> In-Reply-To: <1332860615-3047-1-git-send-email-kwolf@redhat.com> References: <1332860615-3047-1-git-send-email-kwolf@redhat.com> Subject: [Qemu-devel] [RFC PATCH 07/16] qcow2: Simplify count_cow_clusters List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: kwolf@redhat.com, stefanha@gmail.com count_cow_clusters() tries to reuse existing functions, and all it achieves is to make things much more complicated than they really are: Everything needs COW, unless it's a normal cluster with refcount 1. This patch implements the obvious way of doing this, and by using qcow2_get_cluster_type() it gets rid of all flag magic. Signed-off-by: Kevin Wolf --- block/qcow2-cluster.c | 35 ++++++++++++++++------------------- 1 files changed, 16 insertions(+), 19 deletions(-) diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index 157a156..b120b92 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -706,30 +706,27 @@ err: static int count_cow_clusters(BDRVQcowState *s, int nb_clusters, uint64_t *l2_table, int l2_index) { - int i = 0; - uint64_t cluster_offset; + int i; - while (i < nb_clusters) { - i += count_contiguous_clusters(nb_clusters - i, s->cluster_size, - &l2_table[l2_index], i, - QCOW_OFLAG_COPIED | QCOW_OFLAG_COMPRESSED); - if ((i >= nb_clusters) || be64_to_cpu(l2_table[l2_index + i])) { - break; - } + for (i = 0; i < nb_clusters; i++) { + uint64_t l2_entry = be64_to_cpu(l2_table[l2_index + i]); + int cluster_type = qcow2_get_cluster_type(l2_entry); - i += count_contiguous_free_clusters(nb_clusters - i, - &l2_table[l2_index + i]); - if (i >= nb_clusters) { + switch(cluster_type) { + case QCOW2_CLUSTER_NORMAL: + if (l2_entry & QCOW_OFLAG_COPIED) { + goto out; + } break; - } - - cluster_offset = be64_to_cpu(l2_table[l2_index + i]); - - if ((cluster_offset & QCOW_OFLAG_COPIED) || - (cluster_offset & QCOW_OFLAG_COMPRESSED)) + case QCOW2_CLUSTER_UNALLOCATED: + case QCOW2_CLUSTER_COMPRESSED: break; + default: + abort(); + } } +out: assert(i <= nb_clusters); return i; } -- 1.7.6.5