Blame SOURCES/kvm-qemu-img-Check-post-truncation-size.patch

357786
From 28b3e17abefac49221fe9b8826ed10f4e0f3f585 Mon Sep 17 00:00:00 2001
357786
From: Max Reitz <mreitz@redhat.com>
357786
Date: Wed, 16 May 2018 12:00:18 +0200
357786
Subject: [PATCH 1/8] qemu-img: Check post-truncation size
357786
357786
RH-Author: Max Reitz <mreitz@redhat.com>
357786
Message-id: <20180516120018.27565-2-mreitz@redhat.com>
357786
Patchwork-id: 80281
357786
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH 1/1] qemu-img: Check post-truncation size
357786
Bugzilla: 1523065
357786
RH-Acked-by: John Snow <jsnow@redhat.com>
357786
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
357786
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
357786
357786
Some block drivers (iscsi and file-posix when dealing with device files)
357786
do not actually support truncation, even though they provide a
357786
.bdrv_truncate() method and will happily return success when providing a
357786
new size that does not exceed the current size.  This is because these
357786
drivers expect the user to resize the image outside of qemu and then
357786
provide qemu with that information through the block_resize command
357786
(compare cb1b83e740384b4e0d950f3d7c81c02b8ce86c2e).
357786
357786
Of course, anyone using qemu-img resize will find that behavior useless.
357786
So we should check the actual size of the image after the supposedly
357786
successful truncation took place, emit an error if nothing changed and
357786
emit a warning if the target size was not met.
357786
357786
Signed-off-by: Max Reitz <mreitz@redhat.com>
357786
Message-id: 20180421163957.29872-1-mreitz@redhat.com
357786
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
357786
Signed-off-by: Max Reitz <mreitz@redhat.com>
357786
(cherry picked from commit 5279b30392da7a3248b320c75f20c61e3a95863c)
357786
Signed-off-by: Max Reitz <mreitz@redhat.com>
357786
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
357786
---
357786
 qemu-img.c | 39 +++++++++++++++++++++++++++++++++++----
357786
 1 file changed, 35 insertions(+), 4 deletions(-)
357786
357786
diff --git a/qemu-img.c b/qemu-img.c
357786
index 855fa52..8320887 100644
357786
--- a/qemu-img.c
357786
+++ b/qemu-img.c
357786
@@ -3381,7 +3381,7 @@ static int img_resize(int argc, char **argv)
357786
     Error *err = NULL;
357786
     int c, ret, relative;
357786
     const char *filename, *fmt, *size;
357786
-    int64_t n, total_size, current_size;
357786
+    int64_t n, total_size, current_size, new_size;
357786
     bool quiet = false;
357786
     BlockBackend *blk = NULL;
357786
     PreallocMode prealloc = PREALLOC_MODE_OFF;
357786
@@ -3557,11 +3557,42 @@ static int img_resize(int argc, char **argv)
357786
     }
357786
 
357786
     ret = blk_truncate(blk, total_size, prealloc, &err;;
357786
-    if (!ret) {
357786
-        qprintf(quiet, "Image resized.\n");
357786
-    } else {
357786
+    if (ret < 0) {
357786
         error_report_err(err);
357786
+        goto out;
357786
+    }
357786
+
357786
+    new_size = blk_getlength(blk);
357786
+    if (new_size < 0) {
357786
+        error_report("Failed to verify truncated image length: %s",
357786
+                     strerror(-new_size));
357786
+        ret = -1;
357786
+        goto out;
357786
     }
357786
+
357786
+    /* Some block drivers implement a truncation method, but only so
357786
+     * the user can cause qemu to refresh the image's size from disk.
357786
+     * The idea is that the user resizes the image outside of qemu and
357786
+     * then invokes block_resize to inform qemu about it.
357786
+     * (This includes iscsi and file-posix for device files.)
357786
+     * Of course, that is not the behavior someone invoking
357786
+     * qemu-img resize would find useful, so we catch that behavior
357786
+     * here and tell the user. */
357786
+    if (new_size != total_size && new_size == current_size) {
357786
+        error_report("Image was not resized; resizing may not be supported "
357786
+                     "for this image");
357786
+        ret = -1;
357786
+        goto out;
357786
+    }
357786
+
357786
+    if (new_size != total_size) {
357786
+        warn_report("Image should have been resized to %" PRIi64
357786
+                    " bytes, but was resized to %" PRIi64 " bytes",
357786
+                    total_size, new_size);
357786
+    }
357786
+
357786
+    qprintf(quiet, "Image resized.\n");
357786
+
357786
 out:
357786
     blk_unref(blk);
357786
     if (ret) {
357786
-- 
357786
1.8.3.1
357786