]> xenbits.xensource.com Git - libvirt.git/commitdiff
qemu: hotplug: Adjust error path for attach virtio disk
authorJohn Ferlan <jferlan@redhat.com>
Mon, 11 Apr 2016 13:16:54 +0000 (09:16 -0400)
committerJohn Ferlan <jferlan@redhat.com>
Mon, 2 May 2016 10:29:21 +0000 (06:29 -0400)
Adjust error path logic to make it clearer how to undo the failed add.

Signed-off-by: John Ferlan <jferlan@redhat.com>
src/qemu/qemu_hotplug.c

index 396acd876e3176b43cc2ff6ad9ccbaca3981a264..53d664522dc85f45bcf6ae0f6fb26af55978f21d 100644 (file)
@@ -334,6 +334,7 @@ qemuDomainAttachVirtioDiskDevice(virConnectPtr conn,
 {
     int ret = -1;
     qemuDomainObjPrivatePtr priv = vm->privateData;
+    virErrorPtr orig_err;
     char *devstr = NULL;
     char *drivestr = NULL;
     char *drivealias = NULL;
@@ -384,34 +385,24 @@ 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);
-    ret = qemuMonitorAddDrive(priv->mon, drivestr);
-    if (ret == 0) {
-        ret = qemuMonitorAddDevice(priv->mon, devstr);
-        if (ret < 0) {
-            virErrorPtr orig_err = virSaveLastError();
-            if (qemuMonitorDriveDel(priv->mon, drivealias) < 0) {
-                VIR_WARN("Unable to remove drive %s (%s) after failed "
-                         "qemuMonitorAddDevice", drivealias, drivestr);
-            }
-            if (orig_err) {
-                virSetError(orig_err);
-                virFreeError(orig_err);
-            }
-        }
-    }
+
+    if (qemuMonitorAddDrive(priv->mon, drivestr) < 0)
+        goto failadddrive;
+
+    if (qemuMonitorAddDevice(priv->mon, devstr) < 0)
+        goto failadddevice;
+
     if (qemuDomainObjExitMonitor(driver, vm) < 0) {
         releaseaddr = false;
-        ret = -1;
-        goto error;
+        goto failexitmonitor;
     }
 
-    virDomainAuditDisk(vm, NULL, disk->src, "attach", ret >= 0);
-
-    if (ret < 0)
-        goto error;
+    virDomainAuditDisk(vm, NULL, disk->src, "attach", true);
 
     virDomainDiskInsertPreAlloced(vm->def, disk);
+    ret = 0;
 
  cleanup:
     qemuDomainSecretDiskDestroy(disk);
@@ -421,6 +412,24 @@ qemuDomainAttachVirtioDiskDevice(virConnectPtr conn,
     virObjectUnref(cfg);
     return ret;
 
+ failadddevice:
+    orig_err = virSaveLastError();
+    if (qemuMonitorDriveDel(priv->mon, drivealias) < 0) {
+        VIR_WARN("Unable to remove drive %s (%s) after failed "
+                 "qemuMonitorAddDevice", drivealias, drivestr);
+    }
+    if (orig_err) {
+        virSetError(orig_err);
+        virFreeError(orig_err);
+    }
+
+ failadddrive:
+    if (qemuDomainObjExitMonitor(driver, vm) < 0)
+        releaseaddr = false;
+
+ failexitmonitor:
+    virDomainAuditDisk(vm, NULL, disk->src, "attach", false);
+
  error:
     if (releaseaddr)
         qemuDomainReleaseDeviceAddress(vm, &disk->info, src);