From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56715) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yq3dt-0005dQ-A6 for qemu-devel@nongnu.org; Wed, 06 May 2015 14:02:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Yq3ds-0006Ak-CR for qemu-devel@nongnu.org; Wed, 06 May 2015 14:01:57 -0400 Message-ID: <554A56FD.2080702@redhat.com> Date: Wed, 06 May 2015 20:01:33 +0200 From: Max Reitz MIME-Version: 1.0 References: <1430915026-19156-1-git-send-email-famz@redhat.com> In-Reply-To: <1430915026-19156-1-git-send-email-famz@redhat.com> Content-Type: text/plain; charset=iso-8859-15; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [Qemu-block] [PATCH] vmdk: Fix next_cluster_sector for compressed write List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Fam Zheng , qemu-devel@nongnu.org Cc: qemu-stable@nongnu.org, qemu-block@nongnu.org On 06.05.2015 14:23, Fam Zheng wrote: > This fixes the bug introduced by commit c6ac36e (vmdk: Optimize cluster > allocation). > > Sometimes, write_len could be larger than cluster size, because it > contains both data and marker. We must advance next_cluster_sector in > this case, otherwise the image gets corrupted. > > Reported-by: Antoni Villalonga > Signed-off-by: Fam Zheng > --- > block/vmdk.c | 14 ++++++++++---- > 1 file changed, 10 insertions(+), 4 deletions(-) > > diff --git a/block/vmdk.c b/block/vmdk.c > index 1c5e2ef..4b4a862 100644 > --- a/block/vmdk.c > +++ b/block/vmdk.c > @@ -1302,6 +1302,8 @@ static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset, > uLongf buf_len; > const uint8_t *write_buf = buf; > int write_len = nb_sectors * 512; > + int64_t write_offset; > + int64_t write_end_sector; > > if (extent->compressed) { > if (!extent->has_marker) { > @@ -1320,10 +1322,14 @@ static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset, > write_buf = (uint8_t *)data; > write_len = buf_len + sizeof(VmdkGrainMarker); > } > - ret = bdrv_pwrite(extent->file, > - cluster_offset + offset_in_cluster, > - write_buf, > - write_len); > + write_offset = cluster_offset + offset_in_cluster, > + ret = bdrv_pwrite(extent->file, write_offset, write_buf, write_len); > + > + write_end_sector = DIV_ROUND_UP(write_offset + write_len, BDRV_SECTOR_SIZE); > + > + extent->next_cluster_sector = MAX(extent->next_cluster_sector, > + write_end_sector); > + > if (ret != write_len) { > ret = ret < 0 ? ret : -EIO; > goto out; Reviewed-by: Max Reitz