qemu: block: Add code to fetch block node data by node name

To allow updating stats based on the node name, add a helper function
that will fetch the required data from 'query-named-block-nodes' and
return it in hash table for easy lookup.
This commit is contained in:
Peter Krempa 2017-03-16 12:30:16 +01:00
parent 86e51d68f9
commit 51c4b744d8
2 changed files with 49 additions and 0 deletions

View File

@ -380,3 +380,49 @@ qemuBlockNodeNamesDetect(virQEMUDriverPtr driver,
return ret;
}
static int
qemuBlockFillNodeData(size_t pos ATTRIBUTE_UNUSED,
virJSONValuePtr item,
void *opaque)
{
virHashTablePtr table = opaque;
const char *name;
if (!(name = virJSONValueObjectGetString(item, "node-name")))
return 1;
if (virHashAddEntry(table, name, item) < 0)
return -1;
return 0;
}
/**
* qemuBlockGetNodeData:
* @data: JSON object returned from query-named-block-nodes
*
* Returns a hash table organized by the node name of the JSON value objects of
* data for given qemu block nodes.
*
* Returns a filled virHashTablePtr on success NULL on error.
*/
virHashTablePtr
qemuBlockGetNodeData(virJSONValuePtr data)
{
virHashTablePtr ret = NULL;
if (!(ret = virHashCreate(50, virJSONValueHashFree)))
return NULL;
if (virJSONValueArrayForeachSteal(data, qemuBlockFillNodeData, ret) < 0)
goto error;
return ret;
error:
virHashFree(ret);
return NULL;
}

View File

@ -48,4 +48,7 @@ int
qemuBlockNodeNamesDetect(virQEMUDriverPtr driver,
virDomainObjPtr vm);
virHashTablePtr
qemuBlockGetNodeData(virJSONValuePtr data);
#endif /* __QEMU_BLOCK_H__ */