From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1LKc6o-0004xu-C9 for qemu-devel@nongnu.org; Wed, 07 Jan 2009 12:22:22 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1LKc6n-0004xi-Pe for qemu-devel@nongnu.org; Wed, 07 Jan 2009 12:22:21 -0500 Received: from [199.232.76.173] (port=53865 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LKc6n-0004xf-MS for qemu-devel@nongnu.org; Wed, 07 Jan 2009 12:22:21 -0500 Received: from savannah.gnu.org ([199.232.41.3]:35650 helo=sv.gnu.org) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1LKc6n-0002PF-EQ for qemu-devel@nongnu.org; Wed, 07 Jan 2009 12:22:21 -0500 Received: from cvs.savannah.gnu.org ([199.232.41.69]) by sv.gnu.org with esmtp (Exim 4.63) (envelope-from ) id 1LKc6m-0006X1-K0 for qemu-devel@nongnu.org; Wed, 07 Jan 2009 17:22:20 +0000 Received: from aliguori by cvs.savannah.gnu.org with local (Exim 4.63) (envelope-from ) id 1LKc6m-0006Wl-5c for qemu-devel@nongnu.org; Wed, 07 Jan 2009 17:22:20 +0000 MIME-Version: 1.0 Errors-To: aliguori Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Anthony Liguori Message-Id: Date: Wed, 07 Jan 2009 17:22:20 +0000 Subject: [Qemu-devel] [6213] qcow2: Fix cluster allocation (Kevin Wolf) 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 Revision: 6213 http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=6213 Author: aliguori Date: 2009-01-07 17:22:19 +0000 (Wed, 07 Jan 2009) Log Message: ----------- qcow2: Fix cluster allocation (Kevin Wolf) When allocating multiple clusters at once, the qcow2 implementation tries to find as many physically contiguous clusters as possible to allow larger writes. This search includes allocated clusters which are in the right place and still free clusters. If the range to allocate spans clusters in patterns like "10 allocated, then 10 free, then again 10 allocated" it is only checked that the chunks of allocated clusters are contiguous for themselves. However, what is actually needed is to have _all_ allocated clusters contiguous, starting at the first cluster of the allocation and spanning multiple such chunks. This patch changes the check so that each offset is not compared to the offset of the first cluster in its own chunk but to the first cluster in the whole allocation. I haven't seen it happen, but without this fix data corruption on qcow2 images is possible. Signed-off-by: Kevin Wolf Acked-by: Gleb Natapov Modified Paths: -------------- trunk/block-qcow2.c Modified: trunk/block-qcow2.c =================================================================== --- trunk/block-qcow2.c 2009-01-07 16:43:13 UTC (rev 6212) +++ trunk/block-qcow2.c 2009-01-07 17:22:19 UTC (rev 6213) @@ -615,7 +615,7 @@ } static int count_contiguous_clusters(uint64_t nb_clusters, int cluster_size, - uint64_t *l2_table, uint64_t mask) + uint64_t *l2_table, uint64_t start, uint64_t mask) { int i; uint64_t offset = be64_to_cpu(l2_table[0]) & ~mask; @@ -623,11 +623,11 @@ if (!offset) return 0; - for (i = 0; i < nb_clusters; i++) + for (i = start; i < start + nb_clusters; i++) if (offset + i * cluster_size != (be64_to_cpu(l2_table[i]) & ~mask)) break; - return i; + return (i - start); } static int count_contiguous_free_clusters(uint64_t nb_clusters, uint64_t *l2_table) @@ -714,7 +714,7 @@ } else { /* how many allocated clusters ? */ c = count_contiguous_clusters(nb_clusters, s->cluster_size, - &l2_table[l2_index], QCOW_OFLAG_COPIED); + &l2_table[l2_index], 0, QCOW_OFLAG_COPIED); } nb_available = (c * s->cluster_sectors); @@ -968,7 +968,7 @@ if (cluster_offset & QCOW_OFLAG_COPIED) { nb_clusters = count_contiguous_clusters(nb_clusters, s->cluster_size, - &l2_table[l2_index], 0); + &l2_table[l2_index], 0, 0); cluster_offset &= ~QCOW_OFLAG_COPIED; m->nb_clusters = 0; @@ -985,7 +985,7 @@ while (i < nb_clusters) { i += count_contiguous_clusters(nb_clusters - i, s->cluster_size, - &l2_table[l2_index + i], 0); + &l2_table[l2_index], i, 0); if(be64_to_cpu(l2_table[l2_index + i])) break;