Signed-off-by: Chunyan Liu <cyliu@xxxxxxxx> --- src/qemu/qemu_conf.h | 8 -- src/qemu/qemu_driver.c | 74 +++++++++---------- src/qemu/qemu_hostdev.c | 192 ++++++++++++++++++++++++++++------------------- src/qemu/qemu_hotplug.c | 1 + 4 files changed, 151 insertions(+), 124 deletions(-) diff --git a/src/qemu/qemu_conf.h b/src/qemu/qemu_conf.h index ece185b..2ac88fb 100644 --- a/src/qemu/qemu_conf.h +++ b/src/qemu/qemu_conf.h @@ -215,14 +215,6 @@ struct _virQEMUDriver { /* Immutable pointer. self-locking APIs */ virSecurityManagerPtr securityManager; - /* Immutable pointers. Requires locks to be held before - * calling APIs. activePciHostdevs must be locked before - * inactivePciHostdevs */ - virPCIDeviceListPtr activePciHostdevs; - virPCIDeviceListPtr inactivePciHostdevs; - virUSBDeviceListPtr activeUsbHostdevs; - virSCSIDeviceListPtr activeScsiHostdevs; - /* Immutable pointer. Unsafe APIs. XXX */ virHashTablePtr sharedDevices; diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index f36a8b4..7d924b2 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -95,6 +95,7 @@ #include "viraccessapicheck.h" #include "viraccessapicheckqemu.h" #include "storage/storage_driver.h" +#include "virhostdev.h" #define VIR_FROM_THIS VIR_FROM_QEMU @@ -695,18 +696,6 @@ qemuStateInitialize(bool privileged, if (qemuSecurityInit(qemu_driver) < 0) goto error; - if ((qemu_driver->activePciHostdevs = virPCIDeviceListNew()) == NULL) - goto error; - - if ((qemu_driver->activeUsbHostdevs = virUSBDeviceListNew()) == NULL) - goto error; - - if ((qemu_driver->inactivePciHostdevs = virPCIDeviceListNew()) == NULL) - goto error; - - if ((qemu_driver->activeScsiHostdevs = virSCSIDeviceListNew()) == NULL) - goto error; - if (!(qemu_driver->sharedDevices = virHashCreate(30, qemuSharedDeviceEntryFree))) goto error; @@ -983,10 +972,6 @@ qemuStateCleanup(void) { virNWFilterUnRegisterCallbackDriver(&qemuCallbackDriver); virObjectUnref(qemu_driver->config); - virObjectUnref(qemu_driver->activePciHostdevs); - virObjectUnref(qemu_driver->inactivePciHostdevs); - virObjectUnref(qemu_driver->activeUsbHostdevs); - virObjectUnref(qemu_driver->activeScsiHostdevs); virHashFree(qemu_driver->sharedDevices); virObjectUnref(qemu_driver->caps); virQEMUCapsCacheFree(qemu_driver->qemuCapsCache); @@ -11226,7 +11211,6 @@ qemuNodeDeviceDetachFlags(virNodeDevicePtr dev, const char *driverName, unsigned int flags) { - virQEMUDriverPtr driver = dev->conn->privateData; virPCIDevicePtr pci = NULL; unsigned domain = 0, bus = 0, slot = 0, function = 0; int ret = -1; @@ -11234,6 +11218,7 @@ qemuNodeDeviceDetachFlags(virNodeDevicePtr dev, char *xml = NULL; bool legacy = qemuHostdevHostSupportsPassthroughLegacy(); bool vfio = qemuHostdevHostSupportsPassthroughVFIO(); + virHostdevManagerPtr hostdev_mgr; virCheckFlags(0, -1); @@ -11292,18 +11277,21 @@ qemuNodeDeviceDetachFlags(virNodeDevicePtr dev, goto cleanup; } - virObjectLock(driver->activePciHostdevs); - virObjectLock(driver->inactivePciHostdevs); + hostdev_mgr = virHostdevManagerGetDefault(); + if (hostdev_mgr == NULL) + goto cleanup; + virObjectLock(hostdev_mgr->activePciHostdevs); + virObjectLock(hostdev_mgr->inactivePciHostdevs); - if (virPCIDeviceDetach(pci, driver->activePciHostdevs, - driver->inactivePciHostdevs) < 0) { + if (virPCIDeviceDetach(pci, hostdev_mgr->activePciHostdevs, + hostdev_mgr->inactivePciHostdevs) < 0) { goto out; } ret = 0; out: - virObjectUnlock(driver->inactivePciHostdevs); - virObjectUnlock(driver->activePciHostdevs); + virObjectUnlock(hostdev_mgr->inactivePciHostdevs); + virObjectUnlock(hostdev_mgr->activePciHostdevs); cleanup: virPCIDeviceFree(pci); virNodeDeviceDefFree(def); @@ -11320,13 +11308,13 @@ qemuNodeDeviceDettach(virNodeDevicePtr dev) static int qemuNodeDeviceReAttach(virNodeDevicePtr dev) { - virQEMUDriverPtr driver = dev->conn->privateData; virPCIDevicePtr pci = NULL; virPCIDevicePtr other; unsigned domain = 0, bus = 0, slot = 0, function = 0; int ret = -1; virNodeDeviceDefPtr def = NULL; char *xml = NULL; + virHostdevManagerPtr hostdev_mgr; xml = virNodeDeviceGetXMLDesc(dev, 0); if (!xml) @@ -11346,9 +11334,13 @@ qemuNodeDeviceReAttach(virNodeDevicePtr dev) if (!pci) goto cleanup; - virObjectLock(driver->activePciHostdevs); - virObjectLock(driver->inactivePciHostdevs); - other = virPCIDeviceListFind(driver->activePciHostdevs, pci); + hostdev_mgr = virHostdevManagerGetDefault(); + if (hostdev_mgr == NULL) + goto cleanup_pci; + + virObjectLock(hostdev_mgr->activePciHostdevs); + virObjectLock(hostdev_mgr->inactivePciHostdevs); + other = virPCIDeviceListFind(hostdev_mgr->activePciHostdevs, pci); if (other) { const char *other_drvname = NULL; const char *other_domname = NULL; @@ -11369,14 +11361,15 @@ qemuNodeDeviceReAttach(virNodeDevicePtr dev) virPCIDeviceReattachInit(pci); - if (virPCIDeviceReattach(pci, driver->activePciHostdevs, - driver->inactivePciHostdevs) < 0) + if (virPCIDeviceReattach(pci, hostdev_mgr->activePciHostdevs, + hostdev_mgr->inactivePciHostdevs) < 0) goto out; ret = 0; out: - virObjectUnlock(driver->inactivePciHostdevs); - virObjectUnlock(driver->activePciHostdevs); + virObjectUnlock(hostdev_mgr->inactivePciHostdevs); + virObjectUnlock(hostdev_mgr->activePciHostdevs); +cleanup_pci: virPCIDeviceFree(pci); cleanup: virNodeDeviceDefFree(def); @@ -11387,12 +11380,12 @@ cleanup: static int qemuNodeDeviceReset(virNodeDevicePtr dev) { - virQEMUDriverPtr driver = dev->conn->privateData; virPCIDevicePtr pci; unsigned domain = 0, bus = 0, slot = 0, function = 0; int ret = -1; virNodeDeviceDefPtr def = NULL; char *xml = NULL; + virHostdevManagerPtr hostdev_mgr; xml = virNodeDeviceGetXMLDesc(dev, 0); if (!xml) @@ -11412,17 +11405,20 @@ qemuNodeDeviceReset(virNodeDevicePtr dev) if (!pci) goto cleanup; - virObjectLock(driver->activePciHostdevs); - virObjectLock(driver->inactivePciHostdevs); - - if (virPCIDeviceReset(pci, driver->activePciHostdevs, - driver->inactivePciHostdevs) < 0) + hostdev_mgr = virHostdevManagerGetDefault(); + if (hostdev_mgr == NULL) + goto cleanup_pci; + virObjectLock(hostdev_mgr->activePciHostdevs); + virObjectLock(hostdev_mgr->inactivePciHostdevs); + if (virPCIDeviceReset(pci, hostdev_mgr->activePciHostdevs, + hostdev_mgr->inactivePciHostdevs) < 0) goto out; ret = 0; out: - virObjectUnlock(driver->inactivePciHostdevs); - virObjectUnlock(driver->activePciHostdevs); + virObjectUnlock(hostdev_mgr->inactivePciHostdevs); + virObjectUnlock(hostdev_mgr->activePciHostdevs); +cleanup_pci: virPCIDeviceFree(pci); cleanup: virNodeDeviceDefFree(def); diff --git a/src/qemu/qemu_hostdev.c b/src/qemu/qemu_hostdev.c index d37d3fe..54ac973 100644 --- a/src/qemu/qemu_hostdev.c +++ b/src/qemu/qemu_hostdev.c @@ -37,6 +37,7 @@ #include "virscsi.h" #include "virnetdev.h" #include "virfile.h" +#include "virhostdev.h" #define VIR_FROM_THIS VIR_FROM_QEMU @@ -99,10 +100,10 @@ qemuGetPciHostDeviceList(virDomainHostdevDefPtr *hostdevs, int nhostdevs) * * Return the new list, or NULL if there was a failure. * - * Pre-condition: driver->activePciHostdevs is locked + * Pre-condition: activePciHostdevs is locked */ static virPCIDeviceListPtr -qemuGetActivePciHostDeviceList(virQEMUDriverPtr driver, +qemuGetActivePciHostDeviceList(virHostdevManagerPtr mgr, virDomainHostdevDefPtr *hostdevs, int nhostdevs) { @@ -123,7 +124,7 @@ qemuGetActivePciHostDeviceList(virQEMUDriverPtr driver, continue; addr = &hostdev->source.subsys.u.pci.addr; - activeDev = virPCIDeviceListFindByIDs(driver->activePciHostdevs, + activeDev = virPCIDeviceListFindByIDs(mgr->activePciHostdevs, addr->domain, addr->bus, addr->slot, addr->function); if (activeDev && virPCIDeviceListAddCopy(list, activeDev) < 0) { @@ -137,19 +138,22 @@ qemuGetActivePciHostDeviceList(virQEMUDriverPtr driver, int -qemuUpdateActivePciHostdevs(virQEMUDriverPtr driver, +qemuUpdateActivePciHostdevs(virQEMUDriverPtr driver ATTRIBUTE_UNUSED, virDomainDefPtr def) { virDomainHostdevDefPtr hostdev = NULL; virPCIDevicePtr dev = NULL; size_t i; int ret = -1; + virHostdevManagerPtr mgr; if (!def->nhostdevs) return 0; - - virObjectLock(driver->activePciHostdevs); - virObjectLock(driver->inactivePciHostdevs); + mgr = virHostdevManagerGetDefault(); + if (mgr == NULL) + return -1; + virObjectLock(mgr->activePciHostdevs); + virObjectLock(mgr->inactivePciHostdevs); for (i = 0; i < def->nhostdevs; i++) { hostdev = def->hostdevs[i]; @@ -184,7 +188,7 @@ qemuUpdateActivePciHostdevs(virQEMUDriverPtr driver, virPCIDeviceSetRemoveSlot(dev, hostdev->origstates.states.pci.remove_slot); virPCIDeviceSetReprobe(dev, hostdev->origstates.states.pci.reprobe); - if (virPCIDeviceListAdd(driver->activePciHostdevs, dev) < 0) + if (virPCIDeviceListAdd(mgr->activePciHostdevs, dev) < 0) goto cleanup; dev = NULL; } @@ -192,24 +196,26 @@ qemuUpdateActivePciHostdevs(virQEMUDriverPtr driver, ret = 0; cleanup: virPCIDeviceFree(dev); - virObjectUnlock(driver->activePciHostdevs); - virObjectUnlock(driver->inactivePciHostdevs); + virObjectUnlock(mgr->activePciHostdevs); + virObjectUnlock(mgr->inactivePciHostdevs); return ret; } - int -qemuUpdateActiveUsbHostdevs(virQEMUDriverPtr driver, +qemuUpdateActiveUsbHostdevs(virQEMUDriverPtr driver ATTRIBUTE_UNUSED, virDomainDefPtr def) { virDomainHostdevDefPtr hostdev = NULL; size_t i; int ret = -1; + virHostdevManagerPtr mgr; if (!def->nhostdevs) return 0; - - virObjectLock(driver->activeUsbHostdevs); + mgr = virHostdevManagerGetDefault(); + if (mgr == NULL) + return -1; + virObjectLock(mgr->activeUsbHostdevs); for (i = 0; i < def->nhostdevs; i++) { virUSBDevicePtr usb = NULL; hostdev = def->hostdevs[i]; @@ -232,19 +238,19 @@ qemuUpdateActiveUsbHostdevs(virQEMUDriverPtr driver, virUSBDeviceSetUsedBy(usb, QEMU_DRIVER_NAME, def->name); - if (virUSBDeviceListAdd(driver->activeUsbHostdevs, usb) < 0) { + if (virUSBDeviceListAdd(mgr->activeUsbHostdevs, usb) < 0) { virUSBDeviceFree(usb); goto cleanup; } } ret = 0; cleanup: - virObjectUnlock(driver->activeUsbHostdevs); + virObjectUnlock(mgr->activeUsbHostdevs); return ret; } int -qemuUpdateActiveScsiHostdevs(virQEMUDriverPtr driver, +qemuUpdateActiveScsiHostdevs(virQEMUDriverPtr driver ATTRIBUTE_UNUSED, virDomainDefPtr def) { virDomainHostdevDefPtr hostdev = NULL; @@ -252,11 +258,14 @@ qemuUpdateActiveScsiHostdevs(virQEMUDriverPtr driver, int ret = -1; virSCSIDevicePtr scsi = NULL; virSCSIDevicePtr tmp = NULL; + virHostdevManagerPtr mgr; if (!def->nhostdevs) return 0; - - virObjectLock(driver->activeScsiHostdevs); + mgr = virHostdevManagerGetDefault(); + if (mgr == NULL) + return -1; + virObjectLock(mgr->activeScsiHostdevs); for (i = 0; i < def->nhostdevs; i++) { hostdev = def->hostdevs[i]; @@ -273,7 +282,7 @@ qemuUpdateActiveScsiHostdevs(virQEMUDriverPtr driver, hostdev->shareable))) goto cleanup; - if ((tmp = virSCSIDeviceListFind(driver->activeScsiHostdevs, scsi))) { + if ((tmp = virSCSIDeviceListFind(mgr->activeScsiHostdevs, scsi))) { if (virSCSIDeviceSetUsedBy(tmp, QEMU_DRIVER_NAME, def->name) < 0) { virSCSIDeviceFree(scsi); goto cleanup; @@ -281,7 +290,7 @@ qemuUpdateActiveScsiHostdevs(virQEMUDriverPtr driver, virSCSIDeviceFree(scsi); } else { if (virSCSIDeviceSetUsedBy(scsi, QEMU_DRIVER_NAME, def->name) < 0 || - virSCSIDeviceListAdd(driver->activeScsiHostdevs, scsi) < 0) { + virSCSIDeviceListAdd(mgr->activeScsiHostdevs, scsi) < 0) { virSCSIDeviceFree(scsi); goto cleanup; } @@ -290,7 +299,7 @@ qemuUpdateActiveScsiHostdevs(virQEMUDriverPtr driver, ret = 0; cleanup: - virObjectUnlock(driver->activeScsiHostdevs); + virObjectUnlock(mgr->activeScsiHostdevs); return ret; } @@ -657,12 +666,16 @@ qemuPrepareHostdevPCIDevices(virQEMUDriverPtr driver, size_t i; int ret = -1; virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver); + virHostdevManagerPtr hostdev_mgr; if (!qemuPrepareHostdevPCICheckSupport(hostdevs, nhostdevs, qemuCaps)) - goto cleanup; + goto out; - virObjectLock(driver->activePciHostdevs); - virObjectLock(driver->inactivePciHostdevs); + hostdev_mgr = virHostdevManagerGetDefault(); + if (hostdev_mgr == NULL) + goto out; + virObjectLock(hostdev_mgr->activePciHostdevs); + virObjectLock(hostdev_mgr->inactivePciHostdevs); if (!(pcidevs = qemuGetPciHostDeviceList(hostdevs, nhostdevs))) goto cleanup; @@ -690,9 +703,9 @@ qemuPrepareHostdevPCIDevices(virQEMUDriverPtr driver, goto cleanup; } /* The device is in use by other active domain if - * the dev is in list driver->activePciHostdevs. + * the dev is in list activePciHostdevs. */ - if ((other = virPCIDeviceListFind(driver->activePciHostdevs, dev))) { + if ((other = virPCIDeviceListFind(hostdev_mgr->activePciHostdevs, dev))) { const char *other_drvname; const char *other_domname; @@ -715,7 +728,7 @@ qemuPrepareHostdevPCIDevices(virQEMUDriverPtr driver, for (i = 0; i < virPCIDeviceListCount(pcidevs); i++) { virPCIDevicePtr dev = virPCIDeviceListGet(pcidevs, i); if (virPCIDeviceGetManaged(dev) && - virPCIDeviceDetach(dev, driver->activePciHostdevs, NULL) < 0) + virPCIDeviceDetach(dev, hostdev_mgr->activePciHostdevs, NULL) < 0) goto reattachdevs; } @@ -724,8 +737,8 @@ qemuPrepareHostdevPCIDevices(virQEMUDriverPtr driver, for (i = 0; i < virPCIDeviceListCount(pcidevs); i++) { virPCIDevicePtr dev = virPCIDeviceListGet(pcidevs, i); - if (virPCIDeviceReset(dev, driver->activePciHostdevs, - driver->inactivePciHostdevs) < 0) + if (virPCIDeviceReset(dev, hostdev_mgr->activePciHostdevs, + hostdev_mgr->inactivePciHostdevs) < 0) goto reattachdevs; } @@ -750,24 +763,24 @@ qemuPrepareHostdevPCIDevices(virQEMUDriverPtr driver, /* Loop 5: Now mark all the devices as active */ for (i = 0; i < virPCIDeviceListCount(pcidevs); i++) { virPCIDevicePtr dev = virPCIDeviceListGet(pcidevs, i); - if (virPCIDeviceListAdd(driver->activePciHostdevs, dev) < 0) + if (virPCIDeviceListAdd(hostdev_mgr->activePciHostdevs, dev) < 0) goto inactivedevs; } /* Loop 6: Now remove the devices from inactive list. */ for (i = 0; i < virPCIDeviceListCount(pcidevs); i++) { virPCIDevicePtr dev = virPCIDeviceListGet(pcidevs, i); - virPCIDeviceListDel(driver->inactivePciHostdevs, dev); + virPCIDeviceListDel(hostdev_mgr->inactivePciHostdevs, dev); } /* Loop 7: Now set the used_by_domain of the device in - * driver->activePciHostdevs as domain name. + * activePciHostdevs as domain name. */ for (i = 0; i < virPCIDeviceListCount(pcidevs); i++) { virPCIDevicePtr dev, activeDev; dev = virPCIDeviceListGet(pcidevs, i); - activeDev = virPCIDeviceListFind(driver->activePciHostdevs, dev); + activeDev = virPCIDeviceListFind(hostdev_mgr->activePciHostdevs, dev); if (activeDev) virPCIDeviceSetUsedBy(activeDev, QEMU_DRIVER_NAME, name); @@ -813,12 +826,12 @@ qemuPrepareHostdevPCIDevices(virQEMUDriverPtr driver, goto cleanup; inactivedevs: - /* Only steal all the devices from driver->activePciHostdevs. We will + /* Only steal all the devices from activePciHostdevs. We will * free them in virObjectUnref(). */ for (i = 0; i < virPCIDeviceListCount(pcidevs); i++) { virPCIDevicePtr dev = virPCIDeviceListGet(pcidevs, i); - virPCIDeviceListSteal(driver->activePciHostdevs, dev); + virPCIDeviceListSteal(hostdev_mgr->activePciHostdevs, dev); } resetvfnetconfig: @@ -833,14 +846,15 @@ reattachdevs: /* NB: This doesn't actually re-bind to original driver, just * unbinds from the stub driver */ - ignore_value(virPCIDeviceReattach(dev, driver->activePciHostdevs, + ignore_value(virPCIDeviceReattach(dev, hostdev_mgr->activePciHostdevs, NULL)); } cleanup: - virObjectUnlock(driver->activePciHostdevs); - virObjectUnlock(driver->inactivePciHostdevs); + virObjectUnlock(hostdev_mgr->activePciHostdevs); + virObjectUnlock(hostdev_mgr->inactivePciHostdevs); virObjectUnref(pcidevs); +out: virObjectUnref(cfg); return ret; } @@ -855,12 +869,12 @@ qemuPrepareHostdevUSBDevices(virHostdevManagerPtr mgr, unsigned int count; virUSBDevicePtr tmp; - virObjectLock(driver->activeUsbHostdevs); + virObjectLock(mgr->activeUsbHostdevs); count = virUSBDeviceListCount(list); for (i = 0; i < count; i++) { virUSBDevicePtr usb = virUSBDeviceListGet(list, i); - if ((tmp = virUSBDeviceListFind(driver->activeUsbHostdevs, usb))) { + if ((tmp = virUSBDeviceListFind(mgr->activeUsbHostdevs, usb))) { const char *other_drvname; const char *other_domname; @@ -886,19 +900,19 @@ qemuPrepareHostdevUSBDevices(virHostdevManagerPtr mgr, * from the virUSBDeviceList that passed in on success, * perform rollback on failure. */ - if (virUSBDeviceListAdd(driver->activeUsbHostdevs, usb) < 0) + if (virUSBDeviceListAdd(mgr->activeUsbHostdevs, usb) < 0) goto error; } - virObjectUnlock(driver->activeUsbHostdevs); + virObjectUnlock(mgr->activeUsbHostdevs); return 0; error: for (j = 0; j < i; j++) { tmp = virUSBDeviceListGet(list, i); - virUSBDeviceListSteal(driver->activeUsbHostdevs, tmp); + virUSBDeviceListSteal(mgr->activeUsbHostdevs, tmp); } - virObjectUnlock(driver->activeUsbHostdevs); + virObjectUnlock(mgr->activeUsbHostdevs); return -1; } @@ -992,7 +1006,7 @@ out: int -qemuPrepareHostUSBDevices(virQEMUDriverPtr driver, +qemuPrepareHostUSBDevices(virQEMUDriverPtr driver ATTRIBUTE_UNUSED, virDomainDefPtr def, bool coldBoot) { @@ -1002,6 +1016,7 @@ qemuPrepareHostUSBDevices(virQEMUDriverPtr driver, virUSBDevicePtr tmp; virDomainHostdevDefPtr *hostdevs = def->hostdevs; int nhostdevs = def->nhostdevs; + virHostdevManagerPtr hostdev_mgr; /* To prevent situation where USB device is assigned to two domains * we need to keep a list of currently assigned USB devices. @@ -1041,7 +1056,10 @@ qemuPrepareHostUSBDevices(virQEMUDriverPtr driver, * and add them do driver list. However, if something goes * wrong, perform rollback. */ - if (qemuPrepareHostdevUSBDevices(driver, def->name, list) < 0) + hostdev_mgr = virHostdevManagerGetDefault(); + if (hostdev_mgr == NULL) + goto cleanup; + if (qemuPrepareHostdevUSBDevices(hostdev_mgr, def->name, list) < 0) goto cleanup; /* Loop 2: Temporary list was successfully merged with @@ -1071,6 +1089,7 @@ qemuPrepareHostdevSCSIDevices(virQEMUDriverPtr driver, int count; virSCSIDeviceListPtr list; virSCSIDevicePtr tmp; + virHostdevManagerPtr hostdev_mgr; /* Loop 1: Add the shared scsi host device to shared device * table. @@ -1130,12 +1149,16 @@ qemuPrepareHostdevSCSIDevices(virQEMUDriverPtr driver, * and add them to driver list. However, if something goes * wrong, perform rollback. */ - virObjectLock(driver->activeScsiHostdevs); + hostdev_mgr = virHostdevManagerGetDefault(); + if (hostdev_mgr == NULL) + goto cleanup; + virObjectLock(hostdev_mgr->activeScsiHostdevs); count = virSCSIDeviceListCount(list); for (i = 0; i < count; i++) { virSCSIDevicePtr scsi = virSCSIDeviceListGet(list, i); - if ((tmp = virSCSIDeviceListFind(driver->activeScsiHostdevs, scsi))) { + if ((tmp = virSCSIDeviceListFind(hostdev_mgr->activeScsiHostdevs, + scsi))) { bool scsi_shareable = virSCSIDeviceGetShareable(scsi); bool tmp_shareable = virSCSIDeviceGetShareable(tmp); @@ -1156,12 +1179,12 @@ qemuPrepareHostdevSCSIDevices(virQEMUDriverPtr driver, VIR_DEBUG("Adding %s to activeScsiHostdevs", virSCSIDeviceGetName(scsi)); - if (virSCSIDeviceListAdd(driver->activeScsiHostdevs, scsi) < 0) + if (virSCSIDeviceListAdd(hostdev_mgr->activeScsiHostdevs, scsi) < 0) goto error; } } - virObjectUnlock(driver->activeScsiHostdevs); + virObjectUnlock(hostdev_mgr->activeScsiHostdevs); /* Loop 4: Temporary list was successfully merged with * driver list, so steal all items to avoid freeing them @@ -1178,9 +1201,9 @@ qemuPrepareHostdevSCSIDevices(virQEMUDriverPtr driver, error: for (j = 0; j < i; j++) { tmp = virSCSIDeviceListGet(list, i); - virSCSIDeviceListSteal(driver->activeScsiHostdevs, tmp); + virSCSIDeviceListSteal(hostdev_mgr->activeScsiHostdevs, tmp); } - virObjectUnlock(driver->activeScsiHostdevs); + virObjectUnlock(hostdev_mgr->activeScsiHostdevs); cleanup: virObjectUnref(list); return -1; @@ -1213,11 +1236,11 @@ qemuPrepareHostDevices(virQEMUDriverPtr driver, /* - * Pre-condition: driver->inactivePciHostdevs & driver->activePciHostdevs + * Pre-condition: inactivePciHostdevs & activePciHostdevs * are locked */ static void -qemuReattachPciDevice(virPCIDevicePtr dev, QEMUDriverPtr driver) +qemuReattachPciDevice(virPCIDevicePtr dev, virHostdevManagerPtr mgr) { int retries = 100; @@ -1225,7 +1248,7 @@ qemuReattachPciDevice(virPCIDevicePtr dev, QEMUDriverPtr driver) * successfully, it must have been inactive. */ if (!virPCIDeviceGetManaged(dev)) { - if (virPCIDeviceListAdd(driver->inactivePciHostdevs, dev) < 0) + if (virPCIDeviceListAdd(mgr->inactivePciHostdevs, dev) < 0) virPCIDeviceFree(dev); return; } @@ -1236,8 +1259,8 @@ qemuReattachPciDevice(virPCIDevicePtr dev, QEMUDriverPtr driver) retries--; } - if (virPCIDeviceReattach(dev, driver->activePciHostdevs, - driver->inactivePciHostdevs) < 0) { + if (virPCIDeviceReattach(dev, mgr->activePciHostdevs, + mgr->inactivePciHostdevs) < 0) { virErrorPtr err = virGetLastError(); VIR_ERROR(_("Failed to re-attach PCI device: %s"), err ? err->message : _("unknown error")); @@ -1256,11 +1279,15 @@ qemuDomainReAttachHostdevDevices(virQEMUDriverPtr driver, virPCIDeviceListPtr pcidevs; size_t i; virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver); + virHostdevManagerPtr hostdev_mgr; - virObjectLock(driver->activePciHostdevs); - virObjectLock(driver->inactivePciHostdevs); + hostdev_mgr = virHostdevManagerGetDefault(); + if (hostdev_mgr == NULL) + goto out; + virObjectLock(hostdev_mgr->activePciHostdevs); + virObjectLock(hostdev_mgr->inactivePciHostdevs); - if (!(pcidevs = qemuGetActivePciHostDeviceList(driver, + if (!(pcidevs = qemuGetActivePciHostDeviceList(hostdev_mgr, hostdevs, nhostdevs))) { virErrorPtr err = virGetLastError(); @@ -1282,7 +1309,7 @@ qemuDomainReAttachHostdevDevices(virQEMUDriverPtr driver, * other domain. Or delete it from activePciHostDevs if it had * been used by this domain. */ - activeDev = virPCIDeviceListFind(driver->activePciHostdevs, dev); + activeDev = virPCIDeviceListFind(hostdev_mgr->activePciHostdevs, dev); if (activeDev) { const char *usedby_drvname; const char *usedby_domname; @@ -1294,7 +1321,7 @@ qemuDomainReAttachHostdevDevices(virQEMUDriverPtr driver, } } - virPCIDeviceListDel(driver->activePciHostdevs, dev); + virPCIDeviceListDel(hostdev_mgr->activePciHostdevs, dev); } /* At this point, any device that had been used by the guest is in @@ -1311,8 +1338,8 @@ qemuDomainReAttachHostdevDevices(virQEMUDriverPtr driver, for (i = 0; i < virPCIDeviceListCount(pcidevs); i++) { virPCIDevicePtr dev = virPCIDeviceListGet(pcidevs, i); - if (virPCIDeviceReset(dev, driver->activePciHostdevs, - driver->inactivePciHostdevs) < 0) { + if (virPCIDeviceReset(dev, hostdev_mgr->activePciHostdevs, + hostdev_mgr->inactivePciHostdevs) < 0) { virErrorPtr err = virGetLastError(); VIR_ERROR(_("Failed to reset PCI device: %s"), err ? err->message : _("unknown error")); @@ -1322,26 +1349,32 @@ qemuDomainReAttachHostdevDevices(virQEMUDriverPtr driver, while (virPCIDeviceListCount(pcidevs) > 0) { virPCIDevicePtr dev = virPCIDeviceListStealIndex(pcidevs, 0); - qemuReattachPciDevice(dev, driver); + qemuReattachPciDevice(dev, hostdev_mgr); } virObjectUnref(pcidevs); cleanup: - virObjectUnlock(driver->activePciHostdevs); - virObjectUnlock(driver->inactivePciHostdevs); + virObjectUnlock(hostdev_mgr->activePciHostdevs); + virObjectUnlock(hostdev_mgr->inactivePciHostdevs); +out: virObjectUnref(cfg); } void -qemuDomainReAttachHostUsbDevices(virQEMUDriverPtr driver, +qemuDomainReAttachHostUsbDevices(virQEMUDriverPtr driver ATTRIBUTE_UNUSED, const char *name, virDomainHostdevDefPtr *hostdevs, int nhostdevs) { size_t i; + virHostdevManagerPtr hostdev_mgr; - virObjectLock(driver->activeUsbHostdevs); + hostdev_mgr = virHostdevManagerGetDefault(); + if (hostdev_mgr == NULL) + return; + + virObjectLock(hostdev_mgr->activeUsbHostdevs); for (i = 0; i < nhostdevs; i++) { virDomainHostdevDefPtr hostdev = hostdevs[i]; virUSBDevicePtr usb, tmp; @@ -1373,7 +1406,7 @@ qemuDomainReAttachHostUsbDevices(virQEMUDriverPtr driver, * Therefore we want to steal only those devices from * the list which were taken by @name */ - tmp = virUSBDeviceListFind(driver->activeUsbHostdevs, usb); + tmp = virUSBDeviceListFind(hostdev_mgr->activeUsbHostdevs, usb); virUSBDeviceFree(usb); if (!tmp) { @@ -1392,10 +1425,10 @@ qemuDomainReAttachHostUsbDevices(virQEMUDriverPtr driver, hostdev->source.subsys.u.usb.device, name); - virUSBDeviceListDel(driver->activeUsbHostdevs, tmp); + virUSBDeviceListDel(hostdev_mgr->activeUsbHostdevs, tmp); } } - virObjectUnlock(driver->activeUsbHostdevs); + virObjectUnlock(hostdev_mgr->activeUsbHostdevs); } @@ -1406,8 +1439,13 @@ qemuDomainReAttachHostScsiDevices(virQEMUDriverPtr driver, int nhostdevs) { size_t i; + virHostdevManagerPtr hostdev_mgr; + + hostdev_mgr = virHostdevManagerGetDefault(); + if (hostdev_mgr == NULL) + return; - virObjectLock(driver->activeScsiHostdevs); + virObjectLock(hostdev_mgr->activeScsiHostdevs); for (i = 0; i < nhostdevs; i++) { virDomainHostdevDefPtr hostdev = hostdevs[i]; virSCSIDevicePtr scsi; @@ -1442,7 +1480,7 @@ qemuDomainReAttachHostScsiDevices(virQEMUDriverPtr driver, /* Only delete the devices which are marked as being used by @name, * because qemuProcessStart could fail on the half way. */ - if (!(tmp = virSCSIDeviceListFind(driver->activeScsiHostdevs, scsi))) { + if (!(tmp = virSCSIDeviceListFind(hostdev_mgr->activeScsiHostdevs, scsi))) { VIR_WARN("Unable to find device %s:%d:%d:%d " "in list of active SCSI devices", hostdev->source.subsys.u.scsi.adapter, @@ -1460,10 +1498,10 @@ qemuDomainReAttachHostScsiDevices(virQEMUDriverPtr driver, hostdev->source.subsys.u.scsi.unit, name); - virSCSIDeviceListDel(driver->activeScsiHostdevs, tmp, QEMU_DRIVER_NAME, name); + virSCSIDeviceListDel(hostdev_mgr->activeScsiHostdevs, tmp, QEMU_DRIVER_NAME, name); virSCSIDeviceFree(scsi); } - virObjectUnlock(driver->activeScsiHostdevs); + virObjectUnlock(hostdev_mgr->activeScsiHostdevs); } void diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c index 5546693..eeb1f24 100644 --- a/src/qemu/qemu_hotplug.c +++ b/src/qemu/qemu_hotplug.c @@ -50,6 +50,7 @@ #include "virstoragefile.h" #include "virstring.h" #include "virtime.h" +#include "virhostdev.h" #define VIR_FROM_THIS VIR_FROM_QEMU #define CHANGE_MEDIA_RETRIES 10 -- 1.6.0.2 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list