9ae3a8
From 5a762ccf05fea97bc3e98b8dd317374c51c14c26 Mon Sep 17 00:00:00 2001
9ae3a8
From: Jeffrey Cody <jcody@redhat.com>
9ae3a8
Date: Tue, 16 Sep 2014 20:11:47 +0200
9ae3a8
Subject: [PATCH 09/20] block/vdi: Error out immediately in vdi_create()
9ae3a8
9ae3a8
Message-id: <2947dd91a4c9967cbd5a241145c355263e04207a.1410897407.git.jcody@redhat.com>
9ae3a8
Patchwork-id: 61213
9ae3a8
O-Subject: [PATCH qemu-kvm-rhel RHEL7.1 08/15] block/vdi: Error out immediately in vdi_create()
9ae3a8
Bugzilla: 1098086
9ae3a8
RH-Acked-by: Fam Zheng <famz@redhat.com>
9ae3a8
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
RH-Acked-by: Max Reitz <mreitz@redhat.com>
9ae3a8
9ae3a8
From: Max Reitz <mreitz@redhat.com>
9ae3a8
9ae3a8
Currently, if an error occurs during the part of vdi_create() which
9ae3a8
actually writes the image, the function stores -errno, but continues
9ae3a8
anyway.
9ae3a8
9ae3a8
Instead of trying to write data which (if it can be written at all) does
9ae3a8
not make any sense without the operations before succeeding (e.g.,
9ae3a8
writing the image header), just error out immediately.
9ae3a8
9ae3a8
Signed-off-by: Max Reitz <mreitz@redhat.com>
9ae3a8
Reviewed-by: Stefan Weil <sw@weilnetz.de>
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
(cherry picked from commit 0549ea8b6d3ed4eba9a3bd0abfaed3af5de69873)
9ae3a8
Signed-off-by: Jeff Cody <jcody@redhat.com>
9ae3a8
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 block/vdi.c |    7 ++++++-
9ae3a8
 1 files changed, 6 insertions(+), 1 deletions(-)
9ae3a8
9ae3a8
diff --git a/block/vdi.c b/block/vdi.c
9ae3a8
index 0211023..fb25424 100644
9ae3a8
--- a/block/vdi.c
9ae3a8
+++ b/block/vdi.c
9ae3a8
@@ -753,6 +753,7 @@ static int vdi_create(const char *filename, QEMUOptionParameter *options,
9ae3a8
     vdi_header_to_le(&header);
9ae3a8
     if (write(fd, &header, sizeof(header)) < 0) {
9ae3a8
         result = -errno;
9ae3a8
+        goto close_and_exit;
9ae3a8
     }
9ae3a8
 
9ae3a8
     if (bmap_size > 0) {
9ae3a8
@@ -766,6 +767,8 @@ static int vdi_create(const char *filename, QEMUOptionParameter *options,
9ae3a8
         }
9ae3a8
         if (write(fd, bmap, bmap_size) < 0) {
9ae3a8
             result = -errno;
9ae3a8
+            g_free(bmap);
9ae3a8
+            goto close_and_exit;
9ae3a8
         }
9ae3a8
         g_free(bmap);
9ae3a8
     }
9ae3a8
@@ -773,10 +776,12 @@ static int vdi_create(const char *filename, QEMUOptionParameter *options,
9ae3a8
     if (image_type == VDI_TYPE_STATIC) {
9ae3a8
         if (ftruncate(fd, sizeof(header) + bmap_size + blocks * block_size)) {
9ae3a8
             result = -errno;
9ae3a8
+            goto close_and_exit;
9ae3a8
         }
9ae3a8
     }
9ae3a8
 
9ae3a8
-    if (close(fd) < 0) {
9ae3a8
+close_and_exit:
9ae3a8
+    if ((close(fd) < 0) && !result) {
9ae3a8
         result = -errno;
9ae3a8
     }
9ae3a8
 
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8