]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
qemu: conf: Split up qemuAddSharedDevice 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)
Adding 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 c367a200bfdb7ca1a79d9d0fbb137ade607c4861..96626bfc563daa45cd237d33bfe2fb499c174ce4 100644 (file)
@@ -951,71 +951,78 @@ qemuSharedDeviceEntryInsert(virQEMUDriverPtr driver,
     return ret;
 }
 
-/* qemuAddSharedDevice:
+
+/* qemuAddSharedDisk:
  * @driver: Pointer to qemu driver struct
- * @dev: The device def
+ * @src: disk source
  * @name: The domain name
  *
  * Increase ref count and add the domain name into the list which
  * records all the domains that use the shared device if the entry
  * already exists, otherwise add a new entry.
  */
-int
-qemuAddSharedDevice(virQEMUDriverPtr driver,
-                    virDomainDeviceDefPtr dev,
-                    const char *name)
+static int
+qemuAddSharedDisk(virQEMUDriverPtr driver,
+                  virDomainDiskDefPtr disk,
+                  const char *name)
 {
-    virDomainDiskDefPtr disk = NULL;
-    virDomainHostdevDefPtr hostdev = NULL;
-    char *dev_name = NULL;
-    char *dev_path = NULL;
     char *key = NULL;
     int ret = -1;
 
-    /* Currently the only conflicts we have to care about for
-     * the shared disk and shared host device is "sgio" setting,
-     * which is only valid for block disk and scsi host device.
-     */
-    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 (qemuCheckSharedDisk(driver->sharedDevices, disk) < 0)
+        goto cleanup;
+
+    if (!(key = qemuGetSharedDeviceKey(virDomainDiskGetSource(disk))))
+        goto cleanup;
+
+    if (qemuSharedDeviceEntryInsert(driver, key, name) < 0)
+        goto cleanup;
+
+    ret = 0;
+
+ cleanup:
+    qemuDriverUnlock(driver);
+    VIR_FREE(key);
+    return ret;
+}
+
+
+static int
+qemuAddSharedHostdev(virQEMUDriverPtr driver,
+                     virDomainHostdevDefPtr hostdev,
+                     const char *name)
+{
+    virDomainHostdevSubsysSCSIPtr scsisrc = &hostdev->source.subsys.u.scsi;
+    virDomainHostdevSubsysSCSIHostPtr scsihostsrc = &scsisrc->u.host;
+    char *dev_name = NULL;
+    char *dev_path = NULL;
+    char *key = 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 (qemuCheckSharedDisk(driver->sharedDevices, disk) < 0)
-            goto cleanup;
-
-        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 (qemuSharedDeviceEntryInsert(driver, key, name) < 0)
         goto cleanup;
@@ -1055,6 +1062,32 @@ qemuSharedDeviceEntryRemove(virQEMUDriverPtr driver,
 }
 
 
+/* qemuAddSharedDevice:
+ * @driver: Pointer to qemu driver struct
+ * @dev: The device def
+ * @name: The domain name
+ *
+ * Increase ref count and add the domain name into the list which
+ * records all the domains that use the shared device if the entry
+ * already exists, otherwise add a new entry.
+ */
+int
+qemuAddSharedDevice(virQEMUDriverPtr driver,
+                    virDomainDeviceDefPtr dev,
+                    const char *name)
+{
+    /* Currently the only conflicts we have to care about for
+     * the shared disk and shared host device is "sgio" setting,
+     * which is only valid for block disk and scsi host device.
+     */
+    if (dev->type == VIR_DOMAIN_DEVICE_DISK)
+        return qemuAddSharedDisk(driver, dev->data.disk, name);
+    else if (dev->type == VIR_DOMAIN_DEVICE_HOSTDEV)
+        return qemuAddSharedHostdev(driver, dev->data.hostdev, name);
+    else
+        return 0;
+}
+
 /* qemuRemoveSharedDevice:
  * @driver: Pointer to qemu driver struct
  * @device: The device def