nodedev: Introduce virNodeDeviceObjListNew

In preparation to make things private, make the ->devs be pointers to a
virNodeDeviceObjList and then manage everything inside virnodedeviceobj

Signed-off-by: John Ferlan <jferlan@redhat.com>
This commit is contained in:
John Ferlan 2017-05-12 13:51:25 -04:00
parent 65981c8596
commit 9c5d98fd83
7 changed files with 61 additions and 38 deletions

View File

@ -273,6 +273,17 @@ virNodeDeviceObjFree(virNodeDeviceObjPtr obj)
} }
virNodeDeviceObjListPtr
virNodeDeviceObjListNew(void)
{
virNodeDeviceObjListPtr devs;
if (VIR_ALLOC(devs) < 0)
return NULL;
return devs;
}
void void
virNodeDeviceObjListFree(virNodeDeviceObjListPtr devs) virNodeDeviceObjListFree(virNodeDeviceObjListPtr devs)
{ {
@ -280,7 +291,7 @@ virNodeDeviceObjListFree(virNodeDeviceObjListPtr devs)
for (i = 0; i < devs->count; i++) for (i = 0; i < devs->count; i++)
virNodeDeviceObjFree(devs->objs[i]); virNodeDeviceObjFree(devs->objs[i]);
VIR_FREE(devs->objs); VIR_FREE(devs->objs);
devs->count = 0; VIR_FREE(devs);
} }

View File

@ -32,7 +32,7 @@ typedef virNodeDeviceDriverState *virNodeDeviceDriverStatePtr;
struct _virNodeDeviceDriverState { struct _virNodeDeviceDriverState {
virMutex lock; virMutex lock;
virNodeDeviceObjList devs; /* currently-known devices */ virNodeDeviceObjListPtr devs; /* currently-known devices */
void *privateData; /* driver-specific private data */ void *privateData; /* driver-specific private data */
/* Immutable pointer, self-locking APIs */ /* Immutable pointer, self-locking APIs */
@ -68,6 +68,9 @@ virNodeDeviceObjGetParentHost(virNodeDeviceObjListPtr devs,
void void
virNodeDeviceObjFree(virNodeDeviceObjPtr dev); virNodeDeviceObjFree(virNodeDeviceObjPtr dev);
virNodeDeviceObjListPtr
virNodeDeviceObjListNew(void);
void void
virNodeDeviceObjListFree(virNodeDeviceObjListPtr devs); virNodeDeviceObjListFree(virNodeDeviceObjListPtr devs);

View File

@ -972,6 +972,7 @@ virNodeDeviceObjGetNames;
virNodeDeviceObjGetParentHost; virNodeDeviceObjGetParentHost;
virNodeDeviceObjListExport; virNodeDeviceObjListExport;
virNodeDeviceObjListFree; virNodeDeviceObjListFree;
virNodeDeviceObjListNew;
virNodeDeviceObjLock; virNodeDeviceObjLock;
virNodeDeviceObjNumOfDevices; virNodeDeviceObjNumOfDevices;
virNodeDeviceObjRemove; virNodeDeviceObjRemove;

View File

@ -182,7 +182,7 @@ nodeNumOfDevices(virConnectPtr conn,
virCheckFlags(0, -1); virCheckFlags(0, -1);
nodeDeviceLock(); nodeDeviceLock();
ndevs = virNodeDeviceObjNumOfDevices(&driver->devs, conn, cap, ndevs = virNodeDeviceObjNumOfDevices(driver->devs, conn, cap,
virNodeNumOfDevicesCheckACL); virNodeNumOfDevicesCheckACL);
nodeDeviceUnlock(); nodeDeviceUnlock();
@ -205,7 +205,7 @@ nodeListDevices(virConnectPtr conn,
virCheckFlags(0, -1); virCheckFlags(0, -1);
nodeDeviceLock(); nodeDeviceLock();
nnames = virNodeDeviceObjGetNames(&driver->devs, conn, nnames = virNodeDeviceObjGetNames(driver->devs, conn,
virNodeListDevicesCheckACL, virNodeListDevicesCheckACL,
cap, names, maxnames); cap, names, maxnames);
nodeDeviceUnlock(); nodeDeviceUnlock();
@ -227,7 +227,7 @@ nodeConnectListAllNodeDevices(virConnectPtr conn,
return -1; return -1;
nodeDeviceLock(); nodeDeviceLock();
ret = virNodeDeviceObjListExport(conn, &driver->devs, devices, ret = virNodeDeviceObjListExport(conn, driver->devs, devices,
virConnectListAllNodeDevicesCheckACL, virConnectListAllNodeDevicesCheckACL,
flags); flags);
nodeDeviceUnlock(); nodeDeviceUnlock();
@ -241,7 +241,7 @@ nodeDeviceObjFindByName(const char *name)
virNodeDeviceObjPtr obj; virNodeDeviceObjPtr obj;
nodeDeviceLock(); nodeDeviceLock();
obj = virNodeDeviceObjFindByName(&driver->devs, name); obj = virNodeDeviceObjFindByName(driver->devs, name);
nodeDeviceUnlock(); nodeDeviceUnlock();
if (!obj) { if (!obj) {
@ -289,7 +289,7 @@ nodeDeviceLookupSCSIHostByWWN(virConnectPtr conn,
unsigned int flags) unsigned int flags)
{ {
size_t i; size_t i;
virNodeDeviceObjListPtr devs = &driver->devs; virNodeDeviceObjListPtr devs = driver->devs;
virNodeDevCapsDefPtr cap = NULL; virNodeDevCapsDefPtr cap = NULL;
virNodeDeviceObjPtr obj = NULL; virNodeDeviceObjPtr obj = NULL;
virNodeDeviceDefPtr def; virNodeDeviceDefPtr def;
@ -587,7 +587,7 @@ nodeDeviceCreateXML(virConnectPtr conn,
if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) == -1) if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) == -1)
goto cleanup; goto cleanup;
if ((parent_host = virNodeDeviceObjGetParentHost(&driver->devs, def, if ((parent_host = virNodeDeviceObjGetParentHost(driver->devs, def,
CREATE_DEVICE)) < 0) CREATE_DEVICE)) < 0)
goto cleanup; goto cleanup;
@ -639,7 +639,7 @@ nodeDeviceDestroy(virNodeDevicePtr device)
* or parent_fabric_wwn) and drop the object lock. */ * or parent_fabric_wwn) and drop the object lock. */
virNodeDeviceObjUnlock(obj); virNodeDeviceObjUnlock(obj);
obj = NULL; obj = NULL;
if ((parent_host = virNodeDeviceObjGetParentHost(&driver->devs, def, if ((parent_host = virNodeDeviceObjGetParentHost(driver->devs, def,
EXISTING_DEVICE)) < 0) EXISTING_DEVICE)) < 0)
goto cleanup; goto cleanup;

View File

@ -482,7 +482,7 @@ dev_create(const char *udi)
/* Some devices don't have a path in sysfs, so ignore failure */ /* Some devices don't have a path in sysfs, so ignore failure */
(void)get_str_prop(ctx, udi, "linux.sysfs_path", &devicePath); (void)get_str_prop(ctx, udi, "linux.sysfs_path", &devicePath);
if (!(obj = virNodeDeviceObjAssignDef(&driver->devs, def))) { if (!(obj = virNodeDeviceObjAssignDef(driver->devs, def))) {
VIR_FREE(devicePath); VIR_FREE(devicePath);
goto failure; goto failure;
} }
@ -509,11 +509,11 @@ dev_refresh(const char *udi)
virNodeDeviceObjPtr obj; virNodeDeviceObjPtr obj;
nodeDeviceLock(); nodeDeviceLock();
if ((obj = virNodeDeviceObjFindByName(&driver->devs, name))) { if ((obj = virNodeDeviceObjFindByName(driver->devs, name))) {
/* Simply "rediscover" device -- incrementally handling changes /* Simply "rediscover" device -- incrementally handling changes
* to sub-capabilities (like net.80203) is nasty ... so avoid it. * to sub-capabilities (like net.80203) is nasty ... so avoid it.
*/ */
virNodeDeviceObjRemove(&driver->devs, obj); virNodeDeviceObjRemove(driver->devs, obj);
} else { } else {
VIR_DEBUG("no device named %s", name); VIR_DEBUG("no device named %s", name);
} }
@ -542,10 +542,10 @@ device_removed(LibHalContext *ctx ATTRIBUTE_UNUSED,
virNodeDeviceObjPtr obj; virNodeDeviceObjPtr obj;
nodeDeviceLock(); nodeDeviceLock();
obj = virNodeDeviceObjFindByName(&driver->devs, name); obj = virNodeDeviceObjFindByName(driver->devs, name);
VIR_DEBUG("%s", name); VIR_DEBUG("%s", name);
if (obj) if (obj)
virNodeDeviceObjRemove(&driver->devs, obj); virNodeDeviceObjRemove(driver->devs, obj);
else else
VIR_DEBUG("no device named %s", name); VIR_DEBUG("no device named %s", name);
nodeDeviceUnlock(); nodeDeviceUnlock();
@ -562,7 +562,7 @@ device_cap_added(LibHalContext *ctx,
virNodeDeviceDefPtr def; virNodeDeviceDefPtr def;
nodeDeviceLock(); nodeDeviceLock();
obj = virNodeDeviceObjFindByName(&driver->devs, name); obj = virNodeDeviceObjFindByName(driver->devs, name);
nodeDeviceUnlock(); nodeDeviceUnlock();
VIR_DEBUG("%s %s", cap, name); VIR_DEBUG("%s %s", cap, name);
if (obj) { if (obj) {
@ -627,6 +627,9 @@ nodeStateInitialize(bool privileged ATTRIBUTE_UNUSED,
} }
nodeDeviceLock(); nodeDeviceLock();
if (!(driver->devs = virNodeDeviceObjListNew()))
goto failure;
dbus_error_init(&err); dbus_error_init(&err);
if (!(sysbus = virDBusGetSystemBus())) { if (!(sysbus = virDBusGetSystemBus())) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
@ -701,7 +704,7 @@ nodeStateInitialize(bool privileged ATTRIBUTE_UNUSED,
_("%s: %s"), err.name, err.message); _("%s: %s"), err.name, err.message);
dbus_error_free(&err); dbus_error_free(&err);
} }
virNodeDeviceObjListFree(&driver->devs); virNodeDeviceObjListFree(driver->devs);
if (hal_ctx) if (hal_ctx)
(void)libhal_ctx_free(hal_ctx); (void)libhal_ctx_free(hal_ctx);
nodeDeviceUnlock(); nodeDeviceUnlock();
@ -717,7 +720,7 @@ nodeStateCleanup(void)
if (driver) { if (driver) {
nodeDeviceLock(); nodeDeviceLock();
LibHalContext *hal_ctx = DRV_STATE_HAL_CTX(driver); LibHalContext *hal_ctx = DRV_STATE_HAL_CTX(driver);
virNodeDeviceObjListFree(&driver->devs); virNodeDeviceObjListFree(driver->devs);
(void)libhal_ctx_shutdown(hal_ctx, NULL); (void)libhal_ctx_shutdown(hal_ctx, NULL);
(void)libhal_ctx_free(hal_ctx); (void)libhal_ctx_free(hal_ctx);
nodeDeviceUnlock(); nodeDeviceUnlock();

View File

@ -1318,7 +1318,7 @@ udevRemoveOneDevice(struct udev_device *device)
const char *name = NULL; const char *name = NULL;
name = udev_device_get_syspath(device); name = udev_device_get_syspath(device);
if (!(obj = virNodeDeviceObjFindBySysfsPath(&driver->devs, name))) { if (!(obj = virNodeDeviceObjFindBySysfsPath(driver->devs, name))) {
VIR_DEBUG("Failed to find device to remove that has udev name '%s'", VIR_DEBUG("Failed to find device to remove that has udev name '%s'",
name); name);
return -1; return -1;
@ -1331,7 +1331,7 @@ udevRemoveOneDevice(struct udev_device *device)
VIR_DEBUG("Removing device '%s' with sysfs path '%s'", VIR_DEBUG("Removing device '%s' with sysfs path '%s'",
def->name, name); def->name, name);
virNodeDeviceObjRemove(&driver->devs, obj); virNodeDeviceObjRemove(driver->devs, obj);
virNodeDeviceObjFree(obj); virNodeDeviceObjFree(obj);
if (event) if (event)
@ -1365,7 +1365,7 @@ udevSetParent(struct udev_device *device,
goto cleanup; goto cleanup;
} }
if ((obj = virNodeDeviceObjFindBySysfsPath(&driver->devs, if ((obj = virNodeDeviceObjFindBySysfsPath(driver->devs,
parent_sysfs_path))) { parent_sysfs_path))) {
objdef = virNodeDeviceObjGetDef(obj); objdef = virNodeDeviceObjGetDef(obj);
if (VIR_STRDUP(def->parent, objdef->name) < 0) { if (VIR_STRDUP(def->parent, objdef->name) < 0) {
@ -1424,14 +1424,14 @@ udevAddOneDevice(struct udev_device *device)
if (udevSetParent(device, def) != 0) if (udevSetParent(device, def) != 0)
goto cleanup; goto cleanup;
if ((obj = virNodeDeviceObjFindByName(&driver->devs, def->name))) { if ((obj = virNodeDeviceObjFindByName(driver->devs, def->name))) {
virNodeDeviceObjUnlock(obj); virNodeDeviceObjUnlock(obj);
new_device = false; new_device = false;
} }
/* If this is a device change, the old definition will be freed /* If this is a device change, the old definition will be freed
* and the current definition will take its place. */ * and the current definition will take its place. */
if (!(obj = virNodeDeviceObjAssignDef(&driver->devs, def))) if (!(obj = virNodeDeviceObjAssignDef(driver->devs, def)))
goto cleanup; goto cleanup;
objdef = virNodeDeviceObjGetDef(obj); objdef = virNodeDeviceObjGetDef(obj);
@ -1585,7 +1585,7 @@ nodeStateCleanup(void)
if (udev != NULL) if (udev != NULL)
udev_unref(udev); udev_unref(udev);
virNodeDeviceObjListFree(&driver->devs); virNodeDeviceObjListFree(driver->devs);
nodeDeviceUnlock(); nodeDeviceUnlock();
virMutexDestroy(&driver->lock); virMutexDestroy(&driver->lock);
VIR_FREE(driver); VIR_FREE(driver);
@ -1721,7 +1721,7 @@ udevSetupSystemDev(void)
udevGetDMIData(&def->caps->data.system); udevGetDMIData(&def->caps->data.system);
#endif #endif
if (!(obj = virNodeDeviceObjAssignDef(&driver->devs, def))) if (!(obj = virNodeDeviceObjAssignDef(driver->devs, def)))
goto cleanup; goto cleanup;
virNodeDeviceObjUnlock(obj); virNodeDeviceObjUnlock(obj);
@ -1790,6 +1790,10 @@ nodeStateInitialize(bool privileged,
driver->privateData = priv; driver->privateData = priv;
nodeDeviceLock(); nodeDeviceLock();
if (!(driver->devs = virNodeDeviceObjListNew()))
goto cleanup;
driver->nodeDeviceEventState = virObjectEventStateNew(); driver->nodeDeviceEventState = virObjectEventStateNew();
if (udevPCITranslateInit(privileged) < 0) if (udevPCITranslateInit(privileged) < 0)

View File

@ -101,7 +101,7 @@ struct _testDriver {
bool transaction_running; bool transaction_running;
virInterfaceObjListPtr backupIfaces; virInterfaceObjListPtr backupIfaces;
virStoragePoolObjList pools; virStoragePoolObjList pools;
virNodeDeviceObjList devs; virNodeDeviceObjListPtr devs;
int numCells; int numCells;
testCell cells[MAX_CELLS]; testCell cells[MAX_CELLS];
size_t numAuths; size_t numAuths;
@ -152,7 +152,7 @@ testDriverFree(testDriverPtr driver)
virObjectUnref(driver->caps); virObjectUnref(driver->caps);
virObjectUnref(driver->xmlopt); virObjectUnref(driver->xmlopt);
virObjectUnref(driver->domains); virObjectUnref(driver->domains);
virNodeDeviceObjListFree(&driver->devs); virNodeDeviceObjListFree(driver->devs);
virObjectUnref(driver->networks); virObjectUnref(driver->networks);
virInterfaceObjListFree(driver->ifaces); virInterfaceObjListFree(driver->ifaces);
virStoragePoolObjListFree(&driver->pools); virStoragePoolObjListFree(&driver->pools);
@ -418,7 +418,8 @@ testDriverNew(void)
!(ret->eventState = virObjectEventStateNew()) || !(ret->eventState = virObjectEventStateNew()) ||
!(ret->ifaces = virInterfaceObjListNew()) || !(ret->ifaces = virInterfaceObjListNew()) ||
!(ret->domains = virDomainObjListNew()) || !(ret->domains = virDomainObjListNew()) ||
!(ret->networks = virNetworkObjListNew())) !(ret->networks = virNetworkObjListNew()) ||
!(ret->devs = virNodeDeviceObjListNew()))
goto error; goto error;
virAtomicIntSet(&ret->nextDomID, 1); virAtomicIntSet(&ret->nextDomID, 1);
@ -1171,7 +1172,7 @@ testParseNodedevs(testDriverPtr privconn,
if (!def) if (!def)
goto error; goto error;
if (!(obj = virNodeDeviceObjAssignDef(&privconn->devs, def))) { if (!(obj = virNodeDeviceObjAssignDef(privconn->devs, def))) {
virNodeDeviceDefFree(def); virNodeDeviceDefFree(def);
goto error; goto error;
} }
@ -4560,7 +4561,7 @@ testDestroyVport(testDriverPtr privconn,
* *
* Reaching across the boundaries of space and time into the * Reaching across the boundaries of space and time into the
* Node Device in order to remove */ * Node Device in order to remove */
if (!(obj = virNodeDeviceObjFindByName(&privconn->devs, "scsi_host12"))) { if (!(obj = virNodeDeviceObjFindByName(privconn->devs, "scsi_host12"))) {
virReportError(VIR_ERR_NO_NODE_DEVICE, "%s", virReportError(VIR_ERR_NO_NODE_DEVICE, "%s",
_("no node device with matching name 'scsi_host12'")); _("no node device with matching name 'scsi_host12'"));
return -1; return -1;
@ -4570,7 +4571,7 @@ testDestroyVport(testDriverPtr privconn,
VIR_NODE_DEVICE_EVENT_DELETED, VIR_NODE_DEVICE_EVENT_DELETED,
0); 0);
virNodeDeviceObjRemove(&privconn->devs, obj); virNodeDeviceObjRemove(privconn->devs, obj);
virNodeDeviceObjFree(obj); virNodeDeviceObjFree(obj);
testObjectEventQueue(privconn, event); testObjectEventQueue(privconn, event);
@ -5302,7 +5303,7 @@ testNodeDeviceObjFindByName(testDriverPtr driver,
virNodeDeviceObjPtr obj; virNodeDeviceObjPtr obj;
testDriverLock(driver); testDriverLock(driver);
obj = virNodeDeviceObjFindByName(&driver->devs, name); obj = virNodeDeviceObjFindByName(driver->devs, name);
testDriverUnlock(driver); testDriverUnlock(driver);
if (!obj) if (!obj)
@ -5325,7 +5326,7 @@ testNodeNumOfDevices(virConnectPtr conn,
virCheckFlags(0, -1); virCheckFlags(0, -1);
testDriverLock(driver); testDriverLock(driver);
ndevs = virNodeDeviceObjNumOfDevices(&driver->devs, conn, cap, NULL); ndevs = virNodeDeviceObjNumOfDevices(driver->devs, conn, cap, NULL);
testDriverUnlock(driver); testDriverUnlock(driver);
return ndevs; return ndevs;
@ -5345,7 +5346,7 @@ testNodeListDevices(virConnectPtr conn,
virCheckFlags(0, -1); virCheckFlags(0, -1);
testDriverLock(driver); testDriverLock(driver);
nnames = virNodeDeviceObjGetNames(&driver->devs, conn, NULL, nnames = virNodeDeviceObjGetNames(driver->devs, conn, NULL,
cap, names, maxnames); cap, names, maxnames);
testDriverUnlock(driver); testDriverUnlock(driver);
@ -5492,7 +5493,7 @@ testNodeDeviceMockCreateVport(testDriverPtr driver,
* using the scsi_host11 definition, changing the name and the * using the scsi_host11 definition, changing the name and the
* scsi_host capability fields before calling virNodeDeviceAssignDef * scsi_host capability fields before calling virNodeDeviceAssignDef
* to add the def to the node device objects list. */ * to add the def to the node device objects list. */
if (!(objcopy = virNodeDeviceObjFindByName(&driver->devs, "scsi_host11"))) if (!(objcopy = virNodeDeviceObjFindByName(driver->devs, "scsi_host11")))
goto cleanup; goto cleanup;
xml = virNodeDeviceDefFormat(virNodeDeviceObjGetDef(objcopy)); xml = virNodeDeviceDefFormat(virNodeDeviceObjGetDef(objcopy));
@ -5532,7 +5533,7 @@ testNodeDeviceMockCreateVport(testDriverPtr driver,
caps = caps->next; caps = caps->next;
} }
if (!(obj = virNodeDeviceObjAssignDef(&driver->devs, def))) if (!(obj = virNodeDeviceObjAssignDef(driver->devs, def)))
goto cleanup; goto cleanup;
def = NULL; def = NULL;
objdef = virNodeDeviceObjGetDef(obj); objdef = virNodeDeviceObjGetDef(obj);
@ -5577,7 +5578,7 @@ testNodeDeviceCreateXML(virConnectPtr conn,
/* Unlike the "real" code we don't need the parent_host in order to /* Unlike the "real" code we don't need the parent_host in order to
* call virVHBAManageVport, but still let's make sure the code finds * call virVHBAManageVport, but still let's make sure the code finds
* something valid and no one messed up the mock environment. */ * something valid and no one messed up the mock environment. */
if (virNodeDeviceObjGetParentHost(&driver->devs, def, CREATE_DEVICE) < 0) if (virNodeDeviceObjGetParentHost(driver->devs, def, CREATE_DEVICE) < 0)
goto cleanup; goto cleanup;
/* In the real code, we'd call virVHBAManageVport followed by /* In the real code, we'd call virVHBAManageVport followed by
@ -5638,7 +5639,7 @@ testNodeDeviceDestroy(virNodeDevicePtr dev)
/* We do this just for basic validation, but also avoid finding a /* We do this just for basic validation, but also avoid finding a
* vport capable HBA if for some reason our vHBA doesn't exist */ * vport capable HBA if for some reason our vHBA doesn't exist */
if (virNodeDeviceObjGetParentHost(&driver->devs, def, if (virNodeDeviceObjGetParentHost(driver->devs, def,
EXISTING_DEVICE) < 0) { EXISTING_DEVICE) < 0) {
obj = NULL; obj = NULL;
goto cleanup; goto cleanup;
@ -5649,7 +5650,7 @@ testNodeDeviceDestroy(virNodeDevicePtr dev)
0); 0);
virNodeDeviceObjLock(obj); virNodeDeviceObjLock(obj);
virNodeDeviceObjRemove(&driver->devs, obj); virNodeDeviceObjRemove(driver->devs, obj);
virNodeDeviceObjFree(obj); virNodeDeviceObjFree(obj);
obj = NULL; obj = NULL;