nodedev: Introduce virNodeDeviceObjGetDef

In preparation for privatizing the virNodeDeviceObj - create an accessor
for the @def field and then use it for various callers.

Signed-off-by: John Ferlan <jferlan@redhat.com>
This commit is contained in:
John Ferlan 2017-06-03 07:36:01 -04:00
parent 95ea171b39
commit 922af89e44
7 changed files with 89 additions and 46 deletions

View File

@ -33,6 +33,13 @@
VIR_LOG_INIT("conf.virnodedeviceobj");
virNodeDeviceDefPtr
virNodeDeviceObjGetDef(virNodeDeviceObjPtr obj)
{
return obj->def;
}
static int
virNodeDeviceObjHasCap(const virNodeDeviceObj *dev,
const char *cap)

View File

@ -40,6 +40,9 @@ struct _virNodeDeviceDriverState {
};
virNodeDeviceDefPtr
virNodeDeviceObjGetDef(virNodeDeviceObjPtr obj);
virNodeDeviceObjPtr
virNodeDeviceObjFindByName(virNodeDeviceObjListPtr devs,
const char *name);

View File

@ -959,6 +959,7 @@ virNetworkObjUpdateAssignDef;
virNodeDeviceObjAssignDef;
virNodeDeviceObjFindByName;
virNodeDeviceObjFindBySysfsPath;
virNodeDeviceObjGetDef;
virNodeDeviceObjGetNames;
virNodeDeviceObjGetParentHost;
virNodeDeviceObjListExport;

View File

@ -259,16 +259,18 @@ nodeDeviceLookupByName(virConnectPtr conn,
const char *name)
{
virNodeDeviceObjPtr obj;
virNodeDeviceDefPtr def;
virNodeDevicePtr ret = NULL;
if (!(obj = nodeDeviceObjFindByName(name)))
return NULL;
def = virNodeDeviceObjGetDef(obj);
if (virNodeDeviceLookupByNameEnsureACL(conn, obj->def) < 0)
if (virNodeDeviceLookupByNameEnsureACL(conn, def) < 0)
goto cleanup;
if ((ret = virGetNodeDevice(conn, name))) {
if (VIR_STRDUP(ret->parent, obj->def->parent) < 0) {
if (VIR_STRDUP(ret->parent, def->parent) < 0) {
virObjectUnref(ret);
ret = NULL;
}
@ -290,6 +292,7 @@ nodeDeviceLookupSCSIHostByWWN(virConnectPtr conn,
virNodeDeviceObjListPtr devs = &driver->devs;
virNodeDevCapsDefPtr cap = NULL;
virNodeDeviceObjPtr obj = NULL;
virNodeDeviceDefPtr def;
virNodeDevicePtr dev = NULL;
virCheckFlags(0, NULL);
@ -299,7 +302,8 @@ nodeDeviceLookupSCSIHostByWWN(virConnectPtr conn,
for (i = 0; i < devs->count; i++) {
obj = devs->objs[i];
virNodeDeviceObjLock(obj);
cap = obj->def->caps;
def = virNodeDeviceObjGetDef(obj);
cap = def->caps;
while (cap) {
if (cap->data.type == VIR_NODE_DEV_CAP_SCSI_HOST) {
@ -309,11 +313,11 @@ nodeDeviceLookupSCSIHostByWWN(virConnectPtr conn,
if (STREQ(cap->data.scsi_host.wwnn, wwnn) &&
STREQ(cap->data.scsi_host.wwpn, wwpn)) {
if (virNodeDeviceLookupSCSIHostByWWNEnsureACL(conn, obj->def) < 0)
if (virNodeDeviceLookupSCSIHostByWWNEnsureACL(conn, def) < 0)
goto error;
if ((dev = virGetNodeDevice(conn, obj->def->name))) {
if (VIR_STRDUP(dev->parent, obj->def->parent) < 0) {
if ((dev = virGetNodeDevice(conn, def->name))) {
if (VIR_STRDUP(dev->parent, def->parent) < 0) {
virObjectUnref(dev);
dev = NULL;
}
@ -344,23 +348,25 @@ nodeDeviceGetXMLDesc(virNodeDevicePtr dev,
unsigned int flags)
{
virNodeDeviceObjPtr obj;
virNodeDeviceDefPtr def;
char *ret = NULL;
virCheckFlags(0, NULL);
if (!(obj = nodeDeviceObjFindByName(dev->name)))
return NULL;
def = virNodeDeviceObjGetDef(obj);
if (virNodeDeviceGetXMLDescEnsureACL(dev->conn, obj->def) < 0)
if (virNodeDeviceGetXMLDescEnsureACL(dev->conn, def) < 0)
goto cleanup;
if (nodeDeviceUpdateDriverName(obj->def) < 0)
if (nodeDeviceUpdateDriverName(def) < 0)
goto cleanup;
if (nodeDeviceUpdateCaps(obj->def) < 0)
if (nodeDeviceUpdateCaps(def) < 0)
goto cleanup;
ret = virNodeDeviceDefFormat(obj->def);
ret = virNodeDeviceDefFormat(def);
cleanup:
virNodeDeviceObjUnlock(obj);
@ -372,16 +378,18 @@ char *
nodeDeviceGetParent(virNodeDevicePtr dev)
{
virNodeDeviceObjPtr obj;
virNodeDeviceDefPtr def;
char *ret = NULL;
if (!(obj = nodeDeviceObjFindByName(dev->name)))
return NULL;
def = virNodeDeviceObjGetDef(obj);
if (virNodeDeviceGetParentEnsureACL(dev->conn, obj->def) < 0)
if (virNodeDeviceGetParentEnsureACL(dev->conn, def) < 0)
goto cleanup;
if (obj->def->parent) {
if (VIR_STRDUP(ret, obj->def->parent) < 0)
if (def->parent) {
if (VIR_STRDUP(ret, def->parent) < 0)
goto cleanup;
} else {
virReportError(VIR_ERR_INTERNAL_ERROR,
@ -398,17 +406,19 @@ int
nodeDeviceNumOfCaps(virNodeDevicePtr dev)
{
virNodeDeviceObjPtr obj;
virNodeDeviceDefPtr def;
virNodeDevCapsDefPtr caps;
int ncaps = 0;
int ret = -1;
if (!(obj = nodeDeviceObjFindByName(dev->name)))
return -1;
def = virNodeDeviceObjGetDef(obj);
if (virNodeDeviceNumOfCapsEnsureACL(dev->conn, obj->def) < 0)
if (virNodeDeviceNumOfCapsEnsureACL(dev->conn, def) < 0)
goto cleanup;
for (caps = obj->def->caps; caps; caps = caps->next) {
for (caps = def->caps; caps; caps = caps->next) {
++ncaps;
if (caps->data.type == VIR_NODE_DEV_CAP_SCSI_HOST) {
@ -437,17 +447,19 @@ nodeDeviceListCaps(virNodeDevicePtr dev,
int maxnames)
{
virNodeDeviceObjPtr obj;
virNodeDeviceDefPtr def;
virNodeDevCapsDefPtr caps;
int ncaps = 0;
int ret = -1;
if (!(obj = nodeDeviceObjFindByName(dev->name)))
return -1;
def = virNodeDeviceObjGetDef(obj);
if (virNodeDeviceListCapsEnsureACL(dev->conn, obj->def) < 0)
if (virNodeDeviceListCapsEnsureACL(dev->conn, def) < 0)
goto cleanup;
for (caps = obj->def->caps; caps && ncaps < maxnames; caps = caps->next) {
for (caps = def->caps; caps && ncaps < maxnames; caps = caps->next) {
if (VIR_STRDUP(names[ncaps++], virNodeDevCapTypeToString(caps->data.type)) < 0)
goto cleanup;
@ -611,20 +623,20 @@ nodeDeviceDestroy(virNodeDevicePtr dev)
if (!(obj = nodeDeviceObjFindByName(dev->name)))
return -1;
def = virNodeDeviceObjGetDef(obj);
nodeDeviceLock();
if (virNodeDeviceDestroyEnsureACL(dev->conn, obj->def) < 0)
if (virNodeDeviceDestroyEnsureACL(dev->conn, def) < 0)
goto cleanup;
if (virNodeDeviceGetWWNs(obj->def, &wwnn, &wwpn) < 0)
if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) < 0)
goto cleanup;
/* virNodeDeviceGetParentHost will cause the device object's lock
* to be taken, so grab the object def which will have the various
* fields used to search (name, parent, parent_wwnn, parent_wwpn,
* or parent_fabric_wwn) and drop the object lock. */
def = obj->def;
virNodeDeviceObjUnlock(obj);
obj = NULL;
if ((parent_host = virNodeDeviceObjGetParentHost(&driver->devs, def,

View File

@ -459,6 +459,7 @@ dev_create(const char *udi)
char *parent_key = NULL;
virNodeDeviceObjPtr dev = NULL;
virNodeDeviceDefPtr def = NULL;
virNodeDeviceDefPtr objdef;
const char *name = hal_name(udi);
int rv;
char *privData;
@ -493,15 +494,15 @@ dev_create(const char *udi)
/* Some devices don't have a path in sysfs, so ignore failure */
(void)get_str_prop(ctx, udi, "linux.sysfs_path", &devicePath);
dev = virNodeDeviceObjAssignDef(&driver->devs, def);
if (!dev) {
if (!(dev = virNodeDeviceObjAssignDef(&driver->devs, def))) {
VIR_FREE(devicePath);
goto failure;
}
objdef = virNodeDeviceObjGetDef(dev);
dev->privateData = privData;
dev->privateFree = free_udi;
dev->def->sysfs_path = devicePath;
objdef->sysfs_path = devicePath;
virNodeDeviceObjUnlock(dev);
@ -571,13 +572,15 @@ device_cap_added(LibHalContext *ctx,
{
const char *name = hal_name(udi);
virNodeDeviceObjPtr dev;
virNodeDeviceDefPtr def;
nodeDeviceLock();
dev = virNodeDeviceObjFindByName(&driver->devs, name);
nodeDeviceUnlock();
VIR_DEBUG("%s %s", cap, name);
if (dev) {
(void)gather_capability(ctx, udi, cap, &dev->def->caps);
def = virNodeDeviceObjGetDef(dev);
(void)gather_capability(ctx, udi, cap, &def->caps);
virNodeDeviceObjUnlock(dev);
} else {
VIR_DEBUG("no device named %s", name);

View File

@ -1313,9 +1313,9 @@ static int
udevRemoveOneDevice(struct udev_device *device)
{
virNodeDeviceObjPtr dev = NULL;
virNodeDeviceDefPtr def;
virObjectEventPtr event = NULL;
const char *name = NULL;
int ret = -1;
name = udev_device_get_syspath(device);
dev = virNodeDeviceObjFindBySysfsPath(&driver->devs, name);
@ -1323,22 +1323,21 @@ udevRemoveOneDevice(struct udev_device *device)
if (!dev) {
VIR_DEBUG("Failed to find device to remove that has udev name '%s'",
name);
goto cleanup;
return -1;
}
def = virNodeDeviceObjGetDef(dev);
event = virNodeDeviceEventLifecycleNew(dev->def->name,
event = virNodeDeviceEventLifecycleNew(def->name,
VIR_NODE_DEVICE_EVENT_DELETED,
0);
VIR_DEBUG("Removing device '%s' with sysfs path '%s'",
dev->def->name, name);
def->name, name);
virNodeDeviceObjRemove(&driver->devs, &dev);
ret = 0;
cleanup:
if (event)
virObjectEventStateQueue(driver->nodeDeviceEventState, event);
return ret;
return 0;
}
@ -1349,6 +1348,7 @@ udevSetParent(struct udev_device *device,
struct udev_device *parent_device = NULL;
const char *parent_sysfs_path = NULL;
virNodeDeviceObjPtr dev = NULL;
virNodeDeviceDefPtr objdef;
int ret = -1;
parent_device = device;
@ -1369,7 +1369,8 @@ udevSetParent(struct udev_device *device,
dev = virNodeDeviceObjFindBySysfsPath(&driver->devs,
parent_sysfs_path);
if (dev != NULL) {
if (VIR_STRDUP(def->parent, dev->def->name) < 0) {
objdef = virNodeDeviceObjGetDef(dev);
if (VIR_STRDUP(def->parent, objdef->name) < 0) {
virNodeDeviceObjUnlock(dev);
goto cleanup;
}
@ -1396,6 +1397,7 @@ udevAddOneDevice(struct udev_device *device)
{
virNodeDeviceDefPtr def = NULL;
virNodeDeviceObjPtr dev = NULL;
virNodeDeviceDefPtr objdef;
virObjectEventPtr event = NULL;
bool new_device = true;
int ret = -1;
@ -1435,13 +1437,14 @@ udevAddOneDevice(struct udev_device *device)
dev = virNodeDeviceObjAssignDef(&driver->devs, def);
if (dev == NULL)
goto cleanup;
objdef = virNodeDeviceObjGetDef(dev);
if (new_device)
event = virNodeDeviceEventLifecycleNew(dev->def->name,
event = virNodeDeviceEventLifecycleNew(objdef->name,
VIR_NODE_DEVICE_EVENT_CREATED,
0);
else
event = virNodeDeviceEventUpdateNew(dev->def->name);
event = virNodeDeviceEventUpdateNew(objdef->name);
virNodeDeviceObjUnlock(dev);

View File

@ -5322,13 +5322,15 @@ testNodeDeviceLookupByName(virConnectPtr conn, const char *name)
{
testDriverPtr driver = conn->privateData;
virNodeDeviceObjPtr obj;
virNodeDeviceDefPtr def;
virNodeDevicePtr ret = NULL;
if (!(obj = testNodeDeviceObjFindByName(driver, name)))
goto cleanup;
def = virNodeDeviceObjGetDef(obj);
if ((ret = virGetNodeDevice(conn, name))) {
if (VIR_STRDUP(ret->parent, obj->def->parent) < 0) {
if (VIR_STRDUP(ret->parent, def->parent) < 0) {
virObjectUnref(ret);
ret = NULL;
}
@ -5353,7 +5355,7 @@ testNodeDeviceGetXMLDesc(virNodeDevicePtr dev,
if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
goto cleanup;
ret = virNodeDeviceDefFormat(obj->def);
ret = virNodeDeviceDefFormat(virNodeDeviceObjGetDef(obj));
cleanup:
if (obj)
@ -5366,13 +5368,15 @@ testNodeDeviceGetParent(virNodeDevicePtr dev)
{
testDriverPtr driver = dev->conn->privateData;
virNodeDeviceObjPtr obj;
virNodeDeviceDefPtr def;
char *ret = NULL;
if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
goto cleanup;
def = virNodeDeviceObjGetDef(obj);
if (obj->def->parent) {
ignore_value(VIR_STRDUP(ret, obj->def->parent));
if (def->parent) {
ignore_value(VIR_STRDUP(ret, def->parent));
} else {
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("no parent for this device"));
@ -5390,14 +5394,16 @@ testNodeDeviceNumOfCaps(virNodeDevicePtr dev)
{
testDriverPtr driver = dev->conn->privateData;
virNodeDeviceObjPtr obj;
virNodeDeviceDefPtr def;
virNodeDevCapsDefPtr caps;
int ncaps = 0;
int ret = -1;
if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
goto cleanup;
def = virNodeDeviceObjGetDef(obj);
for (caps = obj->def->caps; caps; caps = caps->next)
for (caps = def->caps; caps; caps = caps->next)
++ncaps;
ret = ncaps;
@ -5413,14 +5419,16 @@ testNodeDeviceListCaps(virNodeDevicePtr dev, char **const names, int maxnames)
{
testDriverPtr driver = dev->conn->privateData;
virNodeDeviceObjPtr obj;
virNodeDeviceDefPtr def;
virNodeDevCapsDefPtr caps;
int ncaps = 0;
int ret = -1;
if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
goto cleanup;
def = virNodeDeviceObjGetDef(obj);
for (caps = obj->def->caps; caps && ncaps < maxnames; caps = caps->next) {
for (caps = def->caps; caps && ncaps < maxnames; caps = caps->next) {
if (VIR_STRDUP(names[ncaps++], virNodeDevCapTypeToString(caps->data.type)) < 0)
goto cleanup;
}
@ -5447,6 +5455,7 @@ testNodeDeviceMockCreateVport(testDriverPtr driver,
virNodeDeviceDefPtr def = NULL;
virNodeDevCapsDefPtr caps;
virNodeDeviceObjPtr obj = NULL, objcopy = NULL;
virNodeDeviceDefPtr objdef;
virObjectEventPtr event = NULL;
/* In the real code, we'd call virVHBAManageVport which would take the
@ -5461,7 +5470,7 @@ testNodeDeviceMockCreateVport(testDriverPtr driver,
if (!(objcopy = virNodeDeviceObjFindByName(&driver->devs, "scsi_host11")))
goto cleanup;
xml = virNodeDeviceDefFormat(objcopy->def);
xml = virNodeDeviceDefFormat(virNodeDeviceObjGetDef(objcopy));
virNodeDeviceObjUnlock(objcopy);
if (!xml)
goto cleanup;
@ -5501,8 +5510,9 @@ testNodeDeviceMockCreateVport(testDriverPtr driver,
if (!(obj = virNodeDeviceObjAssignDef(&driver->devs, def)))
goto cleanup;
def = NULL;
objdef = virNodeDeviceObjGetDef(obj);
event = virNodeDeviceEventLifecycleNew(obj->def->name,
event = virNodeDeviceEventLifecycleNew(objdef->name,
VIR_NODE_DEVICE_EVENT_CREATED,
0);
testObjectEventQueue(driver, event);
@ -5524,6 +5534,7 @@ testNodeDeviceCreateXML(virConnectPtr conn,
char *wwnn = NULL, *wwpn = NULL;
virNodeDevicePtr dev = NULL, ret = NULL;
virNodeDeviceObjPtr obj = NULL;
virNodeDeviceDefPtr objdef;
virCheckFlags(0, NULL);
@ -5551,8 +5562,9 @@ testNodeDeviceCreateXML(virConnectPtr conn,
* work properly, we need to drop our lock */
if (!(obj = testNodeDeviceMockCreateVport(driver, wwnn, wwpn)))
goto cleanup;
objdef = virNodeDeviceObjGetDef(obj);
if (!(dev = virGetNodeDevice(conn, obj->def->name)))
if (!(dev = virGetNodeDevice(conn, objdef->name)))
goto cleanup;
VIR_FREE(dev->parent);
@ -5579,16 +5591,18 @@ testNodeDeviceDestroy(virNodeDevicePtr dev)
int ret = 0;
testDriverPtr driver = dev->conn->privateData;
virNodeDeviceObjPtr obj = NULL;
virNodeDeviceDefPtr def;
char *parent_name = NULL, *wwnn = NULL, *wwpn = NULL;
virObjectEventPtr event = NULL;
if (!(obj = testNodeDeviceObjFindByName(driver, dev->name)))
goto out;
def = virNodeDeviceObjGetDef(obj);
if (virNodeDeviceGetWWNs(obj->def, &wwnn, &wwpn) == -1)
if (virNodeDeviceGetWWNs(def, &wwnn, &wwpn) == -1)
goto out;
if (VIR_STRDUP(parent_name, obj->def->parent) < 0)
if (VIR_STRDUP(parent_name, def->parent) < 0)
goto out;
/* virNodeDeviceGetParentHost will cause the device object's lock to be
@ -5599,7 +5613,7 @@ testNodeDeviceDestroy(virNodeDevicePtr dev)
/* We do this just for basic validation, but also avoid finding a
* vport capable HBA if for some reason our vHBA doesn't exist */
if (virNodeDeviceObjGetParentHost(&driver->devs, obj->def,
if (virNodeDeviceObjGetParentHost(&driver->devs, def,
EXISTING_DEVICE) < 0) {
obj = NULL;
goto out;