]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
virHostdevPrepareSCSIVHostDevices: Simplify logic
authorMichal Privoznik <mprivozn@redhat.com>
Sat, 15 Jun 2019 07:37:18 +0000 (09:37 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Thu, 20 Jun 2019 08:29:54 +0000 (10:29 +0200)
Couple of things happening in this patch:

1) We can mark the device we're adding onto active list as used
   way before - when adding it onto temporary list.

2) When actually moving device from a temporary helper list onto
   the list of active devices we check if the device isn't
   already there. The same check is performed by
   virSCSIVHostDeviceListAdd() later. Drop this duplicity.

3) The 'error' label is renamed to 'rollback' to reflect what it
   is actually doing. While in the rest of the code we don't
   allow random label names, this source file is different.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/util/virhostdev.c

index 29e37ee4dbab26d8b9d31015548d84648a0fa741..e32c550c2ff0b5c5ce732bd4aef84e8a2eeeeabd 100644 (file)
@@ -1645,10 +1645,9 @@ virHostdevPrepareSCSIVHostDevices(virHostdevManagerPtr mgr,
                                   virDomainHostdevDefPtr *hostdevs,
                                   int nhostdevs)
 {
-    size_t i, j;
-    int count;
     VIR_AUTOUNREF(virSCSIVHostDeviceListPtr) list = NULL;
-    virSCSIVHostDevicePtr host, tmp;
+    virSCSIVHostDevicePtr tmp;
+    size_t i, j;
 
     if (!nhostdevs)
         return 0;
@@ -1665,6 +1664,7 @@ virHostdevPrepareSCSIVHostDevices(virHostdevManagerPtr mgr,
     for (i = 0; i < nhostdevs; i++) {
         virDomainHostdevDefPtr hostdev = hostdevs[i];
         virDomainHostdevSubsysSCSIVHostPtr hostsrc = &hostdev->source.subsys.u.scsi_host;
+        VIR_AUTOPTR(virSCSIVHostDevice) host = NULL;
 
         if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS ||
             hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI_HOST)
@@ -1676,10 +1676,12 @@ virHostdevPrepareSCSIVHostDevices(virHostdevManagerPtr mgr,
         if (!(host = virSCSIVHostDeviceNew(hostsrc->wwpn)))
             return -1;
 
-        if (virSCSIVHostDeviceListAdd(list, host) < 0) {
-            virSCSIVHostDeviceFree(host);
+        if (virSCSIVHostDeviceSetUsedBy(host, drv_name, dom_name) < 0)
             return -1;
-        }
+
+        if (virSCSIVHostDeviceListAdd(list, host) < 0)
+            return -1;
+        host = NULL;
     }
 
     /* Loop 2: Mark devices in temporary list as used by @name
@@ -1687,27 +1689,15 @@ virHostdevPrepareSCSIVHostDevices(virHostdevManagerPtr mgr,
      * wrong, perform rollback.
      */
     virObjectLock(mgr->activeSCSIVHostHostdevs);
-    count = virSCSIVHostDeviceListCount(list);
 
-    for (i = 0; i < count; i++) {
-        host = virSCSIVHostDeviceListGet(list, i);
-        if ((tmp = virSCSIVHostDeviceListFind(mgr->activeSCSIVHostHostdevs,
-                                              host))) {
-            virReportError(VIR_ERR_OPERATION_INVALID,
-                           _("SCSI_host device %s is already in use by "
-                             "another domain"),
-                           virSCSIVHostDeviceGetName(tmp));
-            goto error;
-        } else {
-            if (virSCSIVHostDeviceSetUsedBy(host, drv_name, dom_name) < 0)
-                goto error;
+    for (i = 0; i < virSCSIVHostDeviceListCount(list); i++) {
+        tmp = virSCSIVHostDeviceListGet(list, i);
 
-            VIR_DEBUG("Adding %s to activeSCSIVHostHostdevs",
-                      virSCSIVHostDeviceGetName(host));
+        VIR_DEBUG("Adding %s to activeSCSIVHostHostdevs",
+                  virSCSIVHostDeviceGetName(tmp));
 
-            if (virSCSIVHostDeviceListAdd(mgr->activeSCSIVHostHostdevs, host) < 0)
-                goto error;
-        }
+        if (virSCSIVHostDeviceListAdd(mgr->activeSCSIVHostHostdevs, tmp) < 0)
+            goto rollback;
     }
 
     virObjectUnlock(mgr->activeSCSIVHostHostdevs);
@@ -1722,7 +1712,8 @@ virHostdevPrepareSCSIVHostDevices(virHostdevManagerPtr mgr,
     }
 
     return 0;
- error:
+
+ rollback:
     for (j = 0; j < i; j++) {
         tmp = virSCSIVHostDeviceListGet(list, i);
         virSCSIVHostDeviceListSteal(mgr->activeSCSIVHostHostdevs, tmp);