Retrieve data for individual block nodes in a hash table. Currently only
capacity and allocation data is extracted but this will be extended in
the future.

Signed-off-by: Peter Krempa <pkre...@redhat.com>
---
 src/qemu/qemu_monitor.c      | 18 ++++++++++
 src/qemu/qemu_monitor.h      | 11 ++++++
 src/qemu/qemu_monitor_json.c | 69 ++++++++++++++++++++++++++++++++++++
 src/qemu/qemu_monitor_json.h |  3 ++
 4 files changed, 101 insertions(+)

diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
index 86d3800108..b6c890f9ce 100644
--- a/src/qemu/qemu_monitor.c
+++ b/src/qemu/qemu_monitor.c
@@ -2220,6 +2220,24 @@ 
qemuMonitorBlockStatsUpdateCapacityBlockdev(qemuMonitorPtr mon,
     return qemuMonitorJSONBlockStatsUpdateCapacityBlockdev(mon, stats);
 }

+
+/**
+ * qemuMonitorBlockGetNamedNodeData:
+ * @mon: monitor object
+ *
+ * Uses 'query-named-block-nodes' to retrieve information about individual
+ * storage nodes and returns them in a hash table of qemuBlockNamedNodeDataPtrs
+ * filled with the data. The hash table keys are node names.
+ */
+virHashTablePtr
+qemuMonitorBlockGetNamedNodeData(qemuMonitorPtr mon)
+{
+    QEMU_CHECK_MONITOR_NULL(mon);
+
+    return qemuMonitorJSONBlockGetNamedNodeData(mon);
+}
+
+
 int
 qemuMonitorBlockResize(qemuMonitorPtr mon,
                        const char *device,
diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h
index 11048dc76a..36eb5f342d 100644
--- a/src/qemu/qemu_monitor.h
+++ b/src/qemu/qemu_monitor.h
@@ -676,6 +676,17 @@ int 
qemuMonitorBlockStatsUpdateCapacityBlockdev(qemuMonitorPtr mon,
                                                 virHashTablePtr stats)
     ATTRIBUTE_NONNULL(2);

+
+typedef struct _qemuBlockNamedNodeData qemuBlockNamedNodeData;
+typedef qemuBlockNamedNodeData *qemuBlockNamedNodeDataPtr;
+struct _qemuBlockNamedNodeData {
+    unsigned long long capacity;
+    unsigned long long physical;
+};
+
+virHashTablePtr
+qemuMonitorBlockGetNamedNodeData(qemuMonitorPtr mon);
+
 int qemuMonitorBlockResize(qemuMonitorPtr mon,
                            const char *device,
                            const char *nodename,
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index 02308dffe6..c474ac0203 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -2879,6 +2879,75 @@ 
qemuMonitorJSONBlockStatsUpdateCapacityBlockdev(qemuMonitorPtr mon,
 }


+static void
+qemuMonitorJSONBlockNamedNodeDataFree(qemuBlockNamedNodeDataPtr data)
+{
+    if (!data)
+        return;
+
+    g_free(data);
+}
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(qemuBlockNamedNodeData, 
qemuMonitorJSONBlockNamedNodeDataFree);
+
+
+static int
+qemuMonitorJSONBlockGetNamedNodeDataWorker(size_t pos G_GNUC_UNUSED,
+                                           virJSONValuePtr val,
+                                           void *opaque)
+{
+    virHashTablePtr nodes = opaque;
+    virJSONValuePtr img;
+    const char *nodename;
+    g_autoptr(qemuBlockNamedNodeData) ent = NULL;
+
+    ent = g_new0(qemuBlockNamedNodeData, 1);
+
+    if (!(nodename = virJSONValueObjectGetString(val, "node-name")) ||
+        !(img = virJSONValueObjectGetObject(val, "image")))
+        goto broken;
+
+    if (virJSONValueObjectGetNumberUlong(img, "virtual-size", &ent->capacity) 
< 0)
+        goto broken;
+
+    /* if actual-size is missing, image is not thin provisioned */
+    if (virJSONValueObjectGetNumberUlong(img, "actual-size", &ent->physical) < 
0)
+        ent->physical = ent->capacity;
+
+    if (virHashAddEntry(nodes, nodename, ent) < 0)
+        return -1;
+
+    ent = NULL;
+
+    return 1; /* we don't want to steal the value from the JSON array */
+
+ broken:
+    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                   _("query-named-block-nodes entry was not in expected 
format"));
+    return -1;
+}
+
+
+virHashTablePtr
+qemuMonitorJSONBlockGetNamedNodeData(qemuMonitorPtr mon)
+{
+    g_autoptr(virJSONValue) nodes = NULL;
+    g_autoptr(virHashTable) ret = NULL;
+
+    if (!(nodes = qemuMonitorJSONQueryNamedBlockNodes(mon)))
+        return NULL;
+
+    if (!(ret = virHashNew((virHashDataFreeSimple) 
qemuMonitorJSONBlockNamedNodeDataFree)))
+        return NULL;
+
+    if (virJSONValueArrayForeachSteal(nodes,
+                                      
qemuMonitorJSONBlockGetNamedNodeDataWorker,
+                                      ret) < 0)
+        return NULL;
+
+    return g_steal_pointer(&ret);
+}
+
+
 int qemuMonitorJSONBlockResize(qemuMonitorPtr mon,
                                const char *device,
                                const char *nodename,
diff --git a/src/qemu/qemu_monitor_json.h b/src/qemu/qemu_monitor_json.h
index 975de3759a..6ee3e912f9 100644
--- a/src/qemu/qemu_monitor_json.h
+++ b/src/qemu/qemu_monitor_json.h
@@ -87,6 +87,9 @@ int qemuMonitorJSONBlockStatsUpdateCapacity(qemuMonitorPtr 
mon,
 int qemuMonitorJSONBlockStatsUpdateCapacityBlockdev(qemuMonitorPtr mon,
                                                     virHashTablePtr stats);

+virHashTablePtr
+qemuMonitorJSONBlockGetNamedNodeData(qemuMonitorPtr mon);
+
 int qemuMonitorJSONBlockResize(qemuMonitorPtr mon,
                                const char *device,
                                const char *nodename,
-- 
2.21.0

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

Reply via email to