9ae3a8
From 47886bf3b19f06d0a5255d9656d1c02800baddd0 Mon Sep 17 00:00:00 2001
9ae3a8
From: Fam Zheng <famz@redhat.com>
9ae3a8
Date: Mon, 15 Feb 2016 09:28:25 +0100
9ae3a8
Subject: [PATCH 12/18] vmdk: Fix next_cluster_sector for compressed write
9ae3a8
9ae3a8
RH-Author: Fam Zheng <famz@redhat.com>
9ae3a8
Message-id: <1455528511-9357-13-git-send-email-famz@redhat.com>
9ae3a8
Patchwork-id: 69178
9ae3a8
O-Subject: [RHEL-7.3 qemu-kvm PATCH 12/18] vmdk: Fix next_cluster_sector for compressed write
9ae3a8
Bugzilla: 1299250
9ae3a8
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
RH-Acked-by: Max Reitz <mreitz@redhat.com>
9ae3a8
RH-Acked-by: Markus Armbruster <armbru@redhat.com>
9ae3a8
9ae3a8
BZ: https://bugzilla.redhat.com/show_bug.cgi?id=1299250
9ae3a8
9ae3a8
This fixes the bug introduced by commit c6ac36e (vmdk: Optimize cluster
9ae3a8
allocation).
9ae3a8
9ae3a8
Sometimes, write_len could be larger than cluster size, because it
9ae3a8
contains both data and marker.  We must advance next_cluster_sector in
9ae3a8
this case, otherwise the image gets corrupted.
9ae3a8
9ae3a8
Cc: qemu-stable@nongnu.org
9ae3a8
Reported-by: Antoni Villalonga <qemu-list@friki.cat>
9ae3a8
Signed-off-by: Fam Zheng <famz@redhat.com>
9ae3a8
Reviewed-by: Max Reitz <mreitz@redhat.com>
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
(cherry picked from commit 5e82a31eb967db135fc4e688b134fb0972d62de3)
9ae3a8
Signed-off-by: Fam Zheng <famz@redhat.com>
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 block/vmdk.c | 14 ++++++++++----
9ae3a8
 1 file changed, 10 insertions(+), 4 deletions(-)
9ae3a8
9ae3a8
diff --git a/block/vmdk.c b/block/vmdk.c
9ae3a8
index 3810d75..dd8b638 100644
9ae3a8
--- a/block/vmdk.c
9ae3a8
+++ b/block/vmdk.c
9ae3a8
@@ -1297,6 +1297,8 @@ static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
9ae3a8
     uLongf buf_len;
9ae3a8
     const uint8_t *write_buf = buf;
9ae3a8
     int write_len = nb_sectors * 512;
9ae3a8
+    int64_t write_offset;
9ae3a8
+    int64_t write_end_sector;
9ae3a8
 
9ae3a8
     if (extent->compressed) {
9ae3a8
         if (!extent->has_marker) {
9ae3a8
@@ -1315,10 +1317,14 @@ static int vmdk_write_extent(VmdkExtent *extent, int64_t cluster_offset,
9ae3a8
         write_buf = (uint8_t *)data;
9ae3a8
         write_len = buf_len + sizeof(VmdkGrainMarker);
9ae3a8
     }
9ae3a8
-    ret = bdrv_pwrite(extent->file,
9ae3a8
-                        cluster_offset + offset_in_cluster,
9ae3a8
-                        write_buf,
9ae3a8
-                        write_len);
9ae3a8
+    write_offset = cluster_offset + offset_in_cluster,
9ae3a8
+    ret = bdrv_pwrite(extent->file, write_offset, write_buf, write_len);
9ae3a8
+
9ae3a8
+    write_end_sector = DIV_ROUND_UP(write_offset + write_len, BDRV_SECTOR_SIZE);
9ae3a8
+
9ae3a8
+    extent->next_cluster_sector = MAX(extent->next_cluster_sector,
9ae3a8
+                                      write_end_sector);
9ae3a8
+
9ae3a8
     if (ret != write_len) {
9ae3a8
         ret = ret < 0 ? ret : -EIO;
9ae3a8
         goto out;
9ae3a8
-- 
9ae3a8
1.8.3.1
9ae3a8