From 2d961cf3bab0a552e2beeb83bf8a491d172aa312 Mon Sep 17 00:00:00 2001 From: Chunyan Liu Date: Thu, 6 Mar 2014 14:37:30 +0800 Subject: [PATCH] move virHostdevReAttachUsbHostdevs to virhostdev.c --- src/libvirt_private.syms | 1 + src/qemu/qemu_hostdev.c | 66 ---------------------------------------- src/util/virhostdev.c | 66 ++++++++++++++++++++++++++++++++++++++++ src/util/virhostdev.h | 6 ++++ 4 files changed, 73 insertions(+), 66 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 23168e5dda..f31a58f0c0 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1303,6 +1303,7 @@ virHostdevPreparePCIDevices; virHostdevPrepareSCSIDevices; virHostdevPrepareUSBDevices; virHostdevReAttachPCIDevices; +virHostdevReAttachUsbHostdevs; virHostdevUpdateActivePciHostdevs; virHostdevUpdateActiveScsiHostdevs; virHostdevUpdateActiveUsbHostdevs; diff --git a/src/qemu/qemu_hostdev.c b/src/qemu/qemu_hostdev.c index af1fe876f3..41cda4838a 100644 --- a/src/qemu/qemu_hostdev.c +++ b/src/qemu/qemu_hostdev.c @@ -316,72 +316,6 @@ qemuDomainReAttachHostdevDevices(virQEMUDriverPtr driver, virObjectUnref(cfg); } -static void -virHostdevReAttachUsbHostdevs(virHostdevManagerPtr hostdev_mgr, - const char *drv_name, - const char *name, - virDomainHostdevDefPtr *hostdevs, - int nhostdevs) -{ - size_t i; - - virObjectLock(hostdev_mgr->activeUsbHostdevs); - for (i = 0; i < nhostdevs; i++) { - virDomainHostdevDefPtr hostdev = hostdevs[i]; - virUSBDevicePtr usb, tmp; - const char *usedby_drvname; - const char *usedby_domname; - - if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS) - continue; - if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB) - continue; - if (hostdev->missing) - continue; - - usb = virUSBDeviceNew(hostdev->source.subsys.u.usb.bus, - hostdev->source.subsys.u.usb.device, - NULL); - - if (!usb) { - VIR_WARN("Unable to reattach USB device %03d.%03d on domain %s", - hostdev->source.subsys.u.usb.bus, - hostdev->source.subsys.u.usb.device, - name); - continue; - } - - /* Delete only those USB devices which belongs - * to domain @name because qemuProcessStart() might - * have failed because USB device is already taken. - * Therefore we want to steal only those devices from - * the list which were taken by @name */ - - tmp = virUSBDeviceListFind(hostdev_mgr->activeUsbHostdevs, usb); - virUSBDeviceFree(usb); - - if (!tmp) { - VIR_WARN("Unable to find device %03d.%03d " - "in list of active USB devices", - hostdev->source.subsys.u.usb.bus, - hostdev->source.subsys.u.usb.device); - continue; - } - - virUSBDeviceGetUsedBy(tmp, &usedby_drvname, &usedby_domname); - if (STREQ_NULLABLE(drv_name, usedby_drvname) && - STREQ_NULLABLE(name, usedby_domname)) { - VIR_DEBUG("Removing %03d.%03d dom=%s from activeUsbHostdevs", - hostdev->source.subsys.u.usb.bus, - hostdev->source.subsys.u.usb.device, - name); - - virUSBDeviceListDel(hostdev_mgr->activeUsbHostdevs, tmp); - } - } - virObjectUnlock(hostdev_mgr->activeUsbHostdevs); -} - void qemuDomainReAttachHostUsbDevices(virQEMUDriverPtr driver, const char *name, diff --git a/src/util/virhostdev.c b/src/util/virhostdev.c index 8bebb66a4a..d8d6c903ae 100644 --- a/src/util/virhostdev.c +++ b/src/util/virhostdev.c @@ -1251,3 +1251,69 @@ cleanup: virObjectUnref(list); return -1; } + +void +virHostdevReAttachUsbHostdevs(virHostdevManagerPtr hostdev_mgr, + const char *drv_name, + const char *name, + virDomainHostdevDefPtr *hostdevs, + int nhostdevs) +{ + size_t i; + + virObjectLock(hostdev_mgr->activeUsbHostdevs); + for (i = 0; i < nhostdevs; i++) { + virDomainHostdevDefPtr hostdev = hostdevs[i]; + virUSBDevicePtr usb, tmp; + const char *usedby_drvname; + const char *usedby_domname; + + if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS) + continue; + if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB) + continue; + if (hostdev->missing) + continue; + + usb = virUSBDeviceNew(hostdev->source.subsys.u.usb.bus, + hostdev->source.subsys.u.usb.device, + NULL); + + if (!usb) { + VIR_WARN("Unable to reattach USB device %03d.%03d on domain %s", + hostdev->source.subsys.u.usb.bus, + hostdev->source.subsys.u.usb.device, + name); + continue; + } + + /* Delete only those USB devices which belongs + * to domain @name because qemuProcessStart() might + * have failed because USB device is already taken. + * Therefore we want to steal only those devices from + * the list which were taken by @name */ + + tmp = virUSBDeviceListFind(hostdev_mgr->activeUsbHostdevs, usb); + virUSBDeviceFree(usb); + + if (!tmp) { + VIR_WARN("Unable to find device %03d.%03d " + "in list of active USB devices", + hostdev->source.subsys.u.usb.bus, + hostdev->source.subsys.u.usb.device); + continue; + } + + virUSBDeviceGetUsedBy(tmp, &usedby_drvname, &usedby_domname); + if (STREQ_NULLABLE(drv_name, usedby_drvname) && + STREQ_NULLABLE(name, usedby_domname)) { + VIR_DEBUG("Removing %03d.%03d dom=%s from activeUsbHostdevs", + hostdev->source.subsys.u.usb.bus, + hostdev->source.subsys.u.usb.device, + name); + + virUSBDeviceListDel(hostdev_mgr->activeUsbHostdevs, tmp); + } + } + virObjectUnlock(hostdev_mgr->activeUsbHostdevs); +} diff --git a/src/util/virhostdev.h b/src/util/virhostdev.h index c14960384b..7bd5da1478 100644 --- a/src/util/virhostdev.h +++ b/src/util/virhostdev.h @@ -78,6 +78,12 @@ virHostdevReAttachPCIDevices(virHostdevManagerPtr hostdev_mgr, virDomainHostdevDefPtr *hostdevs, int nhostdevs, char *oldStateDir); +void +virHostdevReAttachUsbHostdevs(virHostdevManagerPtr hostdev_mgr, + const char *drv_name, + const char *name, + virDomainHostdevDefPtr *hostdevs, + int nhostdevs); int virHostdevUpdateActivePciHostdevs(virHostdevManagerPtr mgr, const char *drv_name,