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