[PATCH v2 5/9] hostdev: Use common detach code in virHostdevPCINodeDeviceDetach()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This ensures the behavior for detach is consistent, no matter how it
was triggered (eg. 'virsh nodedev-detach', 'virsh attach-device' or
startup of a domain that is configured to use hostdevs).
---
 src/util/virhostdev.c | 28 ++++++++++++++++++++++++++--
 1 file changed, 26 insertions(+), 2 deletions(-)

diff --git a/src/util/virhostdev.c b/src/util/virhostdev.c
index 6f14574..5ded1e9 100644
--- a/src/util/virhostdev.c
+++ b/src/util/virhostdev.c
@@ -1646,6 +1646,19 @@ virHostdevReAttachSCSIDevices(virHostdevManagerPtr hostdev_mgr,
     virObjectUnlock(hostdev_mgr->activeSCSIHostdevs);
 }
 
+/**
+ * virHostdevPCINodeDeviceDetach:
+ * @hostdev_mgr: hostdev manager
+ * @pci: PCI device
+ *
+ * Detach a specific PCI device from the host.
+ *
+ * This function makes sure the device is not in use before detaching it
+ * from the host; if the device has already been detached from the host,
+ * the operation is considered successful.
+ *
+ * Returns: 0 on success, <0 on failure
+ */
 int
 virHostdevPCINodeDeviceDetach(virHostdevManagerPtr hostdev_mgr,
                               virPCIDevicePtr pci)
@@ -1660,11 +1673,22 @@ virHostdevPCINodeDeviceDetach(virHostdevManagerPtr hostdev_mgr,
     if (virHostdevIsPCINodeDeviceUsed(virPCIDeviceGetAddress(pci), &data))
         goto out;
 
-    if (virPCIDeviceDetach(pci, hostdev_mgr->activePCIHostdevs,
-                           hostdev_mgr->inactivePCIHostdevs) < 0) {
+    /* We want this function to be idempotent, so if the device has already
+     * been added to the inactive list (and is not active, as per the check
+     * above) just return right away */
+    if (virPCIDeviceListFind(hostdev_mgr->inactivePCIHostdevs, pci)) {
+        VIR_DEBUG("PCI device %s is already detached from the host",
+                  virPCIDeviceGetName(pci));
+        ret = 0;
         goto out;
     }
 
+    /* Detach the device. We don't want to skip unmanaged devices in
+     * this case, because the user explicitly asked for the device to
+     * be detached from the host driver */
+    if (virHostdevOnlyDetachPCIDevice(hostdev_mgr, pci, false) < 0)
+        goto out;
+
     ret = 0;
  out:
     virObjectUnlock(hostdev_mgr->inactivePCIHostdevs);
-- 
2.5.0

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]