]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
qemu: conf: Split up qemuRemoveSharedDevice into per-device-type functions
authorPeter Krempa <pkrempa@redhat.com>
Thu, 7 Aug 2014 13:20:40 +0000 (15:20 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Wed, 20 Aug 2014 07:28:04 +0000 (09:28 +0200)
Removing a shared device needs special steps for disks and hostdevs.
Instead of having one function dealing this split the code into two
separate functions that can be used with better granularity.

src/qemu/qemu_conf.c

index 96626bfc563daa45cd237d33bfe2fb499c174ce4..679652963c97ebead8fb311cf40791e097ffdad6 100644 (file)
@@ -1088,64 +1088,65 @@ qemuAddSharedDevice(virQEMUDriverPtr driver,
         return 0;
 }
 
-/* qemuRemoveSharedDevice:
- * @driver: Pointer to qemu driver struct
- * @device: The device def
- * @name: The domain name
- *
- * Decrease ref count and remove the domain name from the list which
- * records all the domains that use the shared device if ref is not
- * 1, otherwise remove the entry.
- */
-int
-qemuRemoveSharedDevice(virQEMUDriverPtr driver,
-                       virDomainDeviceDefPtr dev,
-                       const char *name)
+
+static int
+qemuRemoveSharedDisk(virQEMUDriverPtr driver,
+                     virDomainDiskDefPtr disk,
+                     const char *name)
 {
-    virDomainDiskDefPtr disk = NULL;
-    virDomainHostdevDefPtr hostdev = NULL;
     char *key = NULL;
-    char *dev_name = NULL;
-    char *dev_path = NULL;
     int ret = -1;
 
-    if (dev->type == VIR_DOMAIN_DEVICE_DISK) {
-        disk = dev->data.disk;
+    if (!disk->src->shared || !virDomainDiskSourceIsBlockType(disk->src))
+        return 0;
 
-        if (!disk->src->shared || !virDomainDiskSourceIsBlockType(disk->src))
-            return 0;
-    } else if (dev->type == VIR_DOMAIN_DEVICE_HOSTDEV) {
-        hostdev = dev->data.hostdev;
+    qemuDriverLock(driver);
 
-        if (!hostdev->shareable ||
-            !(hostdev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS &&
-              hostdev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI))
-            return 0;
-    } else {
+    if (!(key = qemuGetSharedDeviceKey(virDomainDiskGetSource(disk))))
+        goto cleanup;
+
+    if (qemuSharedDeviceEntryRemove(driver, key, name) < 0)
+        goto cleanup;
+
+    ret = 0;
+ cleanup:
+    qemuDriverUnlock(driver);
+    VIR_FREE(key);
+    return ret;
+}
+
+
+static int
+qemuRemoveSharedHostdev(virQEMUDriverPtr driver,
+                        virDomainHostdevDefPtr hostdev,
+                        const char *name)
+{
+    virDomainHostdevSubsysSCSIPtr scsisrc = &hostdev->source.subsys.u.scsi;
+    virDomainHostdevSubsysSCSIHostPtr scsihostsrc = &scsisrc->u.host;
+    char *key = NULL;
+    char *dev_name = NULL;
+    char *dev_path = NULL;
+    int ret = -1;
+
+    if (!hostdev->shareable ||
+        !(hostdev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS &&
+          hostdev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI))
         return 0;
-    }
 
     qemuDriverLock(driver);
 
-    if (dev->type == VIR_DOMAIN_DEVICE_DISK) {
-        if (!(key = qemuGetSharedDeviceKey(virDomainDiskGetSource(disk))))
-            goto cleanup;
-    } else {
-        virDomainHostdevSubsysSCSIPtr scsisrc = &hostdev->source.subsys.u.scsi;
-        virDomainHostdevSubsysSCSIHostPtr scsihostsrc = &scsisrc->u.host;
-        if (!(dev_name = virSCSIDeviceGetDevName(NULL,
-                                                 scsihostsrc->adapter,
-                                                 scsihostsrc->bus,
-                                                 scsihostsrc->target,
-                                                 scsihostsrc->unit)))
-            goto cleanup;
+    if (!(dev_name = virSCSIDeviceGetDevName(NULL,
+                                             scsihostsrc->adapter,
+                                             scsihostsrc->bus,
+                                             scsihostsrc->target,
+                                             scsihostsrc->unit)))
+        goto cleanup;
 
-        if (virAsprintf(&dev_path, "/dev/%s", dev_name) < 0)
-            goto cleanup;
+    if (virAsprintf(&dev_path, "/dev/%s", dev_name) < 0)
+        goto cleanup;
 
-        if (!(key = qemuGetSharedDeviceKey(dev_path)))
-            goto cleanup;
-    }
+    if (!(key = qemuGetSharedDeviceKey(dev_path)))
+        goto cleanup;
 
     if (qemuSharedDeviceEntryRemove(driver, key, name) < 0)
         goto cleanup;
@@ -1159,6 +1160,30 @@ qemuRemoveSharedDevice(virQEMUDriverPtr driver,
     return ret;
 }
 
+
+/* qemuRemoveSharedDevice:
+ * @driver: Pointer to qemu driver struct
+ * @device: The device def
+ * @name: The domain name
+ *
+ * Decrease ref count and remove the domain name from the list which
+ * records all the domains that use the shared device if ref is not
+ * 1, otherwise remove the entry.
+ */
+int
+qemuRemoveSharedDevice(virQEMUDriverPtr driver,
+                       virDomainDeviceDefPtr dev,
+                       const char *name)
+{
+    if (dev->type == VIR_DOMAIN_DEVICE_DISK)
+        return qemuRemoveSharedDisk(driver, dev->data.disk, name);
+    else if (dev->type == VIR_DOMAIN_DEVICE_HOSTDEV)
+        return qemuRemoveSharedHostdev(driver, dev->data.hostdev, name);
+    else
+        return 0;
+}
+
+
 int
 qemuSetUnprivSGIO(virDomainDeviceDefPtr dev)
 {