Blob Blame History Raw
From bf72946b78df7a69566eb9e25df8210f926aa513 Mon Sep 17 00:00:00 2001
Message-Id: <bf72946b78df7a69566eb9e25df8210f926aa513@dist-git>
From: John Ferlan <jferlan@redhat.com>
Date: Mon, 25 Jul 2016 12:42:54 -0400
Subject: [PATCH] qemu: Alter error path cleanup for
 qemuDomainAttachVirtioDiskDevice

https://bugzilla.redhat.com/show_bug.cgi?id=1301021

Based on recent review comment - rather than have a spate of goto failxxxx,
change to a boolean based model. Ensures that the original error can be
preserved and cleanup is a bit more orderly if more objects are added.

(cherry picked from commit a7f84cb1ee037c2ea6c803450eb692d8030b4227)
Signed-off-by: John Ferlan <jferlan@redhat.com>
---
 src/qemu/qemu_hotplug.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)

diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index 3f63b0e..e21720b 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -309,6 +309,7 @@ qemuDomainAttachVirtioDiskDevice(virConnectPtr conn,
     char *drivestr = NULL;
     char *drivealias = NULL;
     bool releaseaddr = false;
+    bool driveAdded = false;
     virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
     const char *src = virDomainDiskGetSource(disk);
 
@@ -355,18 +356,18 @@ qemuDomainAttachVirtioDiskDevice(virConnectPtr conn,
     if (VIR_REALLOC_N(vm->def->disks, vm->def->ndisks+1) < 0)
         goto error;
 
-    /* Attach the device - 2 step process */
     qemuDomainObjEnterMonitor(driver, vm);
 
     if (qemuMonitorAddDrive(priv->mon, drivestr) < 0)
-        goto failadddrive;
+        goto exit_monitor;
+    driveAdded = true;
 
     if (qemuMonitorAddDevice(priv->mon, devstr) < 0)
-        goto failadddevice;
+        goto exit_monitor;
 
     if (qemuDomainObjExitMonitor(driver, vm) < 0) {
         releaseaddr = false;
-        goto failexitmonitor;
+        goto error;
     }
 
     virDomainAuditDisk(vm, NULL, disk->src, "attach", true);
@@ -382,9 +383,9 @@ qemuDomainAttachVirtioDiskDevice(virConnectPtr conn,
     virObjectUnref(cfg);
     return ret;
 
- failadddevice:
+ exit_monitor:
     orig_err = virSaveLastError();
-    if (qemuMonitorDriveDel(priv->mon, drivealias) < 0) {
+    if (driveAdded && qemuMonitorDriveDel(priv->mon, drivealias) < 0) {
         VIR_WARN("Unable to remove drive %s (%s) after failed "
                  "qemuMonitorAddDevice", drivealias, drivestr);
     }
@@ -392,12 +393,9 @@ qemuDomainAttachVirtioDiskDevice(virConnectPtr conn,
         virSetError(orig_err);
         virFreeError(orig_err);
     }
-
- failadddrive:
     if (qemuDomainObjExitMonitor(driver, vm) < 0)
         releaseaddr = false;
 
- failexitmonitor:
     virDomainAuditDisk(vm, NULL, disk->src, "attach", false);
 
  error:
-- 
2.9.2