On 9/9/19 8:33 AM, Nikolay Shirokovskiy wrote:
We are going to use qemuDomainAttachHostUSBDevice when
host usb device is plugged back to node.

Signed-off-by: Nikolay Shirokovskiy <nshirokovs...@virtuozzo.com>
---
  src/qemu/qemu_hotplug.c | 29 ++++++++++++++++++++++++-----
  1 file changed, 24 insertions(+), 5 deletions(-)

diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c
index b045735022..ea82cb54ef 100644
--- a/src/qemu/qemu_hotplug.c
+++ b/src/qemu/qemu_hotplug.c
@@ -2437,8 +2437,18 @@ qemuDomainAttachHostUSBDevice(virQEMUDriverPtr driver,
      bool teardownlabel = false;
      bool teardowndevice = false;
      int ret = -1;
+    bool replug;
+    size_t i;
+
+    for (i = 0; i < vm->def->nhostdevs; i++) {
+        if (vm->def->hostdevs[i] == hostdev) {
+            replug = true;
+            break;
+        }
+    }
- if (virDomainUSBAddressEnsure(priv->usbaddrs, hostdev->info) < 0)
+    if (!replug &&
+        virDomainUSBAddressEnsure(priv->usbaddrs, hostdev->info) < 0)

Compilation didn't complain about it, but for clarity, you can initialize
'replug' up there to 'false' to avoid using an uninitialized value here
(since replug will not necessarily be set to 'true' in the for loop).


Looks good otherwise.

Reviewed-by: Daniel Henrique Barboza <danielhb...@gmail.com>


          return -1;
if (qemuHostdevPrepareUSBDevices(driver, vm->def->name, &hostdev, 1, 0) < 0)
@@ -2463,7 +2473,7 @@ qemuDomainAttachHostUSBDevice(virQEMUDriverPtr driver,
      if (!(devstr = qemuBuildUSBHostdevDevStr(vm->def, hostdev, 
priv->qemuCaps)))
          goto cleanup;
- if (VIR_REALLOC_N(vm->def->hostdevs, vm->def->nhostdevs+1) < 0)
+    if (!replug && VIR_REALLOC_N(vm->def->hostdevs, vm->def->nhostdevs+1) < 0)
          goto cleanup;
qemuDomainObjEnterMonitor(driver, vm);
@@ -2476,7 +2486,8 @@ qemuDomainAttachHostUSBDevice(virQEMUDriverPtr driver,
      if (ret < 0)
          goto cleanup;
- vm->def->hostdevs[vm->def->nhostdevs++] = hostdev;
+    if (!replug)
+        vm->def->hostdevs[vm->def->nhostdevs++] = hostdev;
ret = 0;
   cleanup:
@@ -2489,9 +2500,17 @@ qemuDomainAttachHostUSBDevice(virQEMUDriverPtr driver,
          if (teardowndevice &&
              qemuDomainNamespaceTeardownHostdev(vm, hostdev) < 0)
              VIR_WARN("Unable to remove host device from /dev");
-        if (added)
+        if (added) {
              qemuHostdevReAttachUSBDevices(driver, vm->def->name, &hostdev, 1);
-        virDomainUSBAddressRelease(priv->usbaddrs, hostdev->info);
+
+            if (replug) {
+                virDomainHostdevSubsysUSBPtr usbsrc = 
&hostdev->source.subsys.u.usb;
+                usbsrc->bus = 0;
+                usbsrc->device = 0;
+            }
+        }
+        if (!replug)
+            virDomainUSBAddressRelease(priv->usbaddrs, hostdev->info);
      }
      VIR_FREE(devstr);
      return ret;

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

Reply via email to