[libvirt] [PATCH 1/2] virpci: simplify virPCIDeviceBindToStub

Jim Fehlig jfehlig at suse.com
Mon Jul 11 18:23:19 UTC 2016


Early in virPCIDeviceBindToStub, there is a check to see if the
stub is already bound to the device, returning success with no
further actions if that is the case.

The same condition is unnecessarily checked later in the function.
Remove the unneeded checks to simplify the logic a bit.

Signed-off-by: Jim Fehlig <jfehlig at suse.com>
---
 src/util/virpci.c | 68 +++++++++++++++++++++++--------------------------------
 1 file changed, 28 insertions(+), 40 deletions(-)

diff --git a/src/util/virpci.c b/src/util/virpci.c
index 132948d..127b3b6 100644
--- a/src/util/virpci.c
+++ b/src/util/virpci.c
@@ -1196,7 +1196,6 @@ static int
 virPCIDeviceBindToStub(virPCIDevicePtr dev)
 {
     int result = -1;
-    bool reprobe = false;
     char *stubDriverPath = NULL;
     char *driverLink = NULL;
     char *path = NULL; /* reused for different purposes */
@@ -1225,10 +1224,16 @@ virPCIDeviceBindToStub(virPCIDevicePtr dev)
             /* The device is already bound to the correct driver */
             VIR_DEBUG("Device %s is already bound to %s",
                       dev->name, stubDriverName);
+            dev->unbind_from_stub = true;
+            dev->remove_slot = true;
             result = 0;
             goto cleanup;
         }
-        reprobe = true;
+        /*
+         * If the device is bound to a driver that is not the stub,  we'll
+         * need to reprobe later
+         */
+        dev->reprobe = true;
     }
 
     /* Add the PCI device ID to the stub's dynamic ID table;
@@ -1249,51 +1254,34 @@ virPCIDeviceBindToStub(virPCIDevicePtr dev)
         goto cleanup;
     }
 
-    /* check whether the device is bound to pci-stub when we write dev->id to
-     * ${stubDriver}/new_id.
-     */
-    if (virFileLinkPointsTo(driverLink, stubDriverPath)) {
-        dev->unbind_from_stub = true;
-        dev->remove_slot = true;
-        result = 0;
-        goto remove_id;
-    }
-
     if (virPCIDeviceUnbind(dev) < 0)
         goto remove_id;
 
-    /* If the device was bound to a driver we'll need to reprobe later */
-    dev->reprobe = reprobe;
+    /* Xen's pciback.ko wants you to use new_slot first */
+    VIR_FREE(path);
+    if (!(path = virPCIDriverFile(stubDriverName, "new_slot")))
+        goto remove_id;
 
-    /* If the device isn't already bound to pci-stub, try binding it now.
-     */
-    if (!virFileLinkPointsTo(driverLink, stubDriverPath)) {
-        /* Xen's pciback.ko wants you to use new_slot first */
-        VIR_FREE(path);
-        if (!(path = virPCIDriverFile(stubDriverName, "new_slot")))
-            goto remove_id;
-
-        if (virFileExists(path) && virFileWriteStr(path, dev->name, 0) < 0) {
-            virReportSystemError(errno,
-                                 _("Failed to add slot for "
-                                   "PCI device '%s' to %s"),
-                                 dev->name, stubDriverName);
-            goto remove_id;
-        }
-        dev->remove_slot = true;
+    if (virFileExists(path) && virFileWriteStr(path, dev->name, 0) < 0) {
+        virReportSystemError(errno,
+                             _("Failed to add slot for "
+                               "PCI device '%s' to %s"),
+                             dev->name, stubDriverName);
+        goto remove_id;
+    }
+    dev->remove_slot = true;
 
-        VIR_FREE(path);
-        if (!(path = virPCIDriverFile(stubDriverName, "bind")))
-            goto remove_id;
+    VIR_FREE(path);
+    if (!(path = virPCIDriverFile(stubDriverName, "bind")))
+        goto remove_id;
 
-        if (virFileWriteStr(path, dev->name, 0) < 0) {
-            virReportSystemError(errno,
-                                 _("Failed to bind PCI device '%s' to %s"),
-                                 dev->name, stubDriverName);
-            goto remove_id;
-        }
-        dev->unbind_from_stub = true;
+    if (virFileWriteStr(path, dev->name, 0) < 0) {
+        virReportSystemError(errno,
+                             _("Failed to bind PCI device '%s' to %s"),
+                             dev->name, stubDriverName);
+        goto remove_id;
     }
+    dev->unbind_from_stub = true;
 
     result = 0;
 
-- 
2.1.4




More information about the libvir-list mailing list