From patchwork Fri Apr 20 13:56:01 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Wolf X-Patchwork-Id: 154044 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id E31D6B7044 for ; Fri, 20 Apr 2012 23:52:39 +1000 (EST) Received: from localhost ([::1]:59080 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SLEGK-0002ns-Fe for incoming@patchwork.ozlabs.org; Fri, 20 Apr 2012 09:52:36 -0400 Received: from eggs.gnu.org ([208.118.235.92]:59106) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SLEGB-0002nj-6n for qemu-devel@nongnu.org; Fri, 20 Apr 2012 09:52:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SLEG5-0002Ur-0F for qemu-devel@nongnu.org; Fri, 20 Apr 2012 09:52:26 -0400 Received: from mx1.redhat.com ([209.132.183.28]:9505) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SLEG4-0002Ub-PB for qemu-devel@nongnu.org; Fri, 20 Apr 2012 09:52:20 -0400 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q3KDqIYM030149 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Fri, 20 Apr 2012 09:52:18 -0400 Received: from dhcp-5-188.str.redhat.com (vpn1-5-13.ams2.redhat.com [10.36.5.13]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id q3KDqGcT029877; Fri, 20 Apr 2012 09:52:17 -0400 From: Kevin Wolf To: qemu-devel@nongnu.org Date: Fri, 20 Apr 2012 15:56:01 +0200 Message-Id: <1334930161-21972-1-git-send-email-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.132.183.28 Cc: kwolf@redhat.com, mtosatti@redhat.com Subject: [Qemu-devel] [PATCH] qcow2: Fix refcount block allocation during qcow2_allocate_cluster_at() 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 Refcount block allocation and refcount table growth rely on s->free_cluster_index pointing to somewhere after the current allocation. Change qcow2_allocate_cluster_at() to fulfill this assumption. Without this change it could happen that a newly allocated refcount block and the allocated data block point to the same area in the image file, causing data corruption in the long run. This fixes a bug that became first visible after commit 250196f1. Signed-off-by: Kevin Wolf --- block/qcow2-refcount.c | 6 ++++++ 1 files changed, 6 insertions(+), 0 deletions(-) diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index 565bd54..6c38337 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -587,6 +587,7 @@ int qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset, { BDRVQcowState *s = bs->opaque; uint64_t cluster_index; + uint64_t old_free_cluster_index; int i, refcount, ret; /* Check how many clusters there are free */ @@ -602,11 +603,16 @@ int qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset, } /* And then allocate them */ + old_free_cluster_index = s->free_cluster_index; + s->free_cluster_index = cluster_index + i; + ret = update_refcount(bs, offset, i << s->cluster_bits, 1); if (ret < 0) { return ret; } + s->free_cluster_index = old_free_cluster_index; + return i; }