Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1KNpye-0008AU-NI for qemu-devel@nongnu.org; Tue, 29 Jul 2008 10:15:00 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1KNpye-00089z-07 for qemu-devel@nongnu.org; Tue, 29 Jul 2008 10:15:00 -0400 Received: from [199.232.76.173] (port=55898 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KNpyd-00089u-Qp for qemu-devel@nongnu.org; Tue, 29 Jul 2008 10:14:59 -0400 Received: from ecfrec.frec.bull.fr ([129.183.4.8]:51309) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1KNpyd-0006eu-78 for qemu-devel@nongnu.org; Tue, 29 Jul 2008 10:14:59 -0400 Message-Id: <20080729141447.763322831@bull.net> References: <20080729141352.573798859@bull.net> Date: Tue, 29 Jul 2008 16:13:57 +0200 From: Laurent Vivier Content-Disposition: inline; filename=qcow2-extent-more.patch Subject: [Qemu-devel] [patch 5/5][v2] Try to aggregate free clusters and freed clusters Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Laurent Vivier In free_used_clusters(), try to aggregate free clusters and freed clusters. Signed-off-by: Laurent Vivier --- block-qcow2.c | 61 ++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 25 deletions(-) Index: qemu/block-qcow2.c =================================================================== --- qemu.orig/block-qcow2.c 2008-07-29 15:22:28.000000000 +0200 +++ qemu/block-qcow2.c 2008-07-29 15:22:30.000000000 +0200 @@ -649,6 +649,8 @@ static uint64_t free_used_clusters(Block BDRVQcowState *s = bs->opaque; int l1_index, ret; uint64_t cluster_offset; + int i, j, do_loop; + uint64_t current; l1_index = offset >> (s->l2_bits + s->cluster_bits); if (l1_index >= s->l1_size) { @@ -676,17 +678,6 @@ static uint64_t free_used_clusters(Block if (nb_clusters && *nb_clusters > s->l2_size - (*l2_index)) *nb_clusters = s->l2_size - (*l2_index); - if (!cluster_offset) { - if (nb_clusters) { - int i = 1; - while (i < *nb_clusters && (*l2_table)[(*l2_index) + i] == 0) { - i++; - } - *nb_clusters = i; - } - return 0; - } - if (cluster_offset & QCOW_OFLAG_COPIED) { if (nb_clusters) { int i = 1; @@ -702,8 +693,6 @@ static uint64_t free_used_clusters(Block return cluster_offset; } - /* free the cluster */ - if (cluster_offset & QCOW_OFLAG_COMPRESSED) { int nb_csectors; nb_csectors = ((cluster_offset >> s->csize_shift) & s->csize_mask) + 1; @@ -714,22 +703,44 @@ static uint64_t free_used_clusters(Block return 0; } - if (nb_clusters) { - int i = 1; - uint64_t current; - while (i < *nb_clusters) { - current = be64_to_cpu((*l2_table)[(*l2_index) + i]); - if (cluster_offset + (i << s->cluster_bits) != current) - break; - i++; - } - *nb_clusters = i; - free_clusters(bs, cluster_offset, i << s->cluster_bits); + if (!nb_clusters) { + if (cluster_offset) + free_clusters(bs, cluster_offset, s->cluster_size); return 0; } - free_clusters(bs, cluster_offset, s->cluster_size); + i = 0; + do_loop = 1; + while (i < *nb_clusters && do_loop) { + i++; + if (!cluster_offset) { + while (i < *nb_clusters) { + cluster_offset = (*l2_table)[(*l2_index) + i]; + if (cluster_offset) + break; + i++; + } + if ((cluster_offset & QCOW_OFLAG_COPIED) || + (cluster_offset & QCOW_OFLAG_COMPRESSED)) + do_loop = 0; + } else { + j = 1; + current = 0; + while (i < *nb_clusters) { + current = be64_to_cpu((*l2_table)[(*l2_index) + i]); + if (cluster_offset + (j << s->cluster_bits) != current) + break; + i++; + j++; + } + free_clusters(bs, cluster_offset, j << s->cluster_bits); + cluster_offset = current; + if (current) + do_loop = 0; + } + } + *nb_clusters = i; return 0; } --