From patchwork Fri Sep 27 10:14:15 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Max Reitz X-Patchwork-Id: 278532 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 652832C03BA for ; Fri, 27 Sep 2013 20:15:10 +1000 (EST) Received: from localhost ([::1]:35215 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VPV4m-0002Vo-2T for incoming@patchwork.ozlabs.org; Fri, 27 Sep 2013 06:15:08 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:33478) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VPV48-0002HR-7U for qemu-devel@nongnu.org; Fri, 27 Sep 2013 06:14:34 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VPV41-0005p3-KH for qemu-devel@nongnu.org; Fri, 27 Sep 2013 06:14:28 -0400 Received: from mx1.redhat.com ([209.132.183.28]:42832) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VPV41-0005oy-Dl for qemu-devel@nongnu.org; Fri, 27 Sep 2013 06:14:21 -0400 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r8RAEKmC012123 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Fri, 27 Sep 2013 06:14:20 -0400 Received: from localhost (dhcp-200-247.str.redhat.com [10.33.200.247]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id r8RAEJZI004497 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NO); Fri, 27 Sep 2013 06:14:20 -0400 From: Max Reitz To: qemu-devel@nongnu.org Date: Fri, 27 Sep 2013 12:14:15 +0200 Message-Id: <1380276856-29777-2-git-send-email-mreitz@redhat.com> In-Reply-To: <1380276856-29777-1-git-send-email-mreitz@redhat.com> References: <1380276856-29777-1-git-send-email-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Kevin Wolf , Stefan Hajnoczi , Max Reitz Subject: [Qemu-devel] [PATCH v2 1/2] qcow2: count_contiguous_clusters and compression 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 The function is not intended to be used on compressed clusters and will not work correctly, if used anyway, since L2E_OFFSET_MASK is not the right mask for determining the offset of compressed clusters. Therefore, assert that the first cluster is not compressed and always include the compression flag in the mask of significant flags, i.e., stop the search as soon as a compressed cluster occurs. Signed-off-by: Max Reitz --- block/qcow2-cluster.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index cab5f2e..a62ef42 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -281,12 +281,15 @@ static int count_contiguous_clusters(uint64_t nb_clusters, int cluster_size, uint64_t *l2_table, uint64_t start, uint64_t stop_flags) { int i; - uint64_t mask = stop_flags | L2E_OFFSET_MASK; - uint64_t offset = be64_to_cpu(l2_table[0]) & mask; + uint64_t mask = stop_flags | L2E_OFFSET_MASK | QCOW2_CLUSTER_COMPRESSED; + uint64_t first_entry = be64_to_cpu(l2_table[0]); + uint64_t offset = first_entry & mask; if (!offset) return 0; + assert(qcow2_get_cluster_type(first_entry) != QCOW2_CLUSTER_COMPRESSED); + for (i = start; i < start + nb_clusters; i++) { uint64_t l2_entry = be64_to_cpu(l2_table[i]) & mask; if (offset + (uint64_t) i * cluster_size != l2_entry) {