From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:49328) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S3XUC-00086B-BX for qemu-devel@nongnu.org; Fri, 02 Mar 2012 13:45:49 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1S3XTq-000213-Bi for qemu-devel@nongnu.org; Fri, 02 Mar 2012 13:45:47 -0500 Received: from mx1.redhat.com ([209.132.183.28]:64726) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S3XTq-00020i-3X for qemu-devel@nongnu.org; Fri, 02 Mar 2012 13:45:26 -0500 From: Kevin Wolf Date: Fri, 2 Mar 2012 19:48:49 +0100 Message-Id: <1330714131-16908-2-git-send-email-kwolf@redhat.com> In-Reply-To: <1330714131-16908-1-git-send-email-kwolf@redhat.com> References: <1330714131-16908-1-git-send-email-kwolf@redhat.com> Subject: [Qemu-devel] [PATCH 1/3] qcow2: Factor out 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 Signed-off-by: Kevin Wolf --- block/qcow2-cluster.c | 55 ++++++++++++++++++++++++++++++++----------------- 1 files changed, 36 insertions(+), 19 deletions(-) diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index a791bbe..903454d 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -677,6 +677,41 @@ err: } /* + * Returns the number of contiguous clusters that can be used for an allocating + * write, but require COW to be performed (this includes yet unallocated space, + * which must copy from the backing file) + */ +static int count_cow_clusters(BDRVQcowState *s, int nb_clusters, + uint64_t *l2_table, int l2_index) +{ + int i = 0; + uint64_t cluster_offset; + + while (i < nb_clusters) { + i += count_contiguous_clusters(nb_clusters - i, s->cluster_size, + &l2_table[l2_index], i, 0); + if ((i >= nb_clusters) || be64_to_cpu(l2_table[l2_index + i])) { + break; + } + + i += count_contiguous_free_clusters(nb_clusters - i, + &l2_table[l2_index + i]); + if (i >= nb_clusters) { + break; + } + + cluster_offset = be64_to_cpu(l2_table[l2_index + i]); + + if ((cluster_offset & QCOW_OFLAG_COPIED) || + (cluster_offset & QCOW_OFLAG_COMPRESSED)) + break; + } + + assert(i <= nb_clusters); + return i; +} + +/* * alloc_cluster_offset * * For a given offset of the disk image, return cluster offset in qcow2 file. @@ -739,25 +774,7 @@ again: /* how many available clusters ? */ - while (i < nb_clusters) { - i += count_contiguous_clusters(nb_clusters - i, s->cluster_size, - &l2_table[l2_index], i, 0); - if ((i >= nb_clusters) || be64_to_cpu(l2_table[l2_index + i])) { - break; - } - - i += count_contiguous_free_clusters(nb_clusters - i, - &l2_table[l2_index + i]); - if (i >= nb_clusters) { - break; - } - - cluster_offset = be64_to_cpu(l2_table[l2_index + i]); - - if ((cluster_offset & QCOW_OFLAG_COPIED) || - (cluster_offset & QCOW_OFLAG_COMPRESSED)) - break; - } + i = count_cow_clusters(s, nb_clusters, l2_table, l2_index); assert(i <= nb_clusters); nb_clusters = i; -- 1.7.6.5