diff mbox series

[v5,7/7] ndctl: Add supported_alignments to the JSON output

Message ID 20190130105927.31901-7-oohall@gmail.com (mailing list archive)
State Accepted
Commit 31f2f8b26ae911d3e334f3afb0d5b1e5e47823fe
Headers show
Series [v5,1/7] libndctl: Use the supported_alignment attribute | expand

Commit Message

Oliver O'Halloran Jan. 30, 2019, 10:59 a.m. UTC
Add the list of supported alignemnts to PFN and DAX namespaces. Also add
the list of supported sector sizes to BTT namespaces.

Signed-off-by: Oliver O'Halloran <oohall@gmail.com>
---
v5: Added again. The supported_size array is now only included in the
    verbose JSON output. This means it won't be included in the JSON
    output from setup_namespace() so to see the list of supported
    alignments you need to use `ndctl list -v`.

v4: Dropped due to unit test breakage.

v3: Added

Not sure the namespace JSON blob are the best place to put these. The
region might be better, but slightly less accessable to users.
---
 util/json.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 47 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/util/json.c b/util/json.c
index eee459aa453b..dd77b41e114c 100644
--- a/util/json.c
+++ b/util/json.c
@@ -303,6 +303,38 @@  struct json_object *util_daxctl_devs_to_list(struct daxctl_region *region,
 	return jdevs;
 }
 
+#define _SZ(get_max, get_elem, type) \
+static struct json_object *type##_build_size_array(struct type *arg)	\
+{								\
+	struct json_object *arr = json_object_new_array();	\
+	int i;							\
+								\
+	if (!arr)						\
+		return NULL;					\
+								\
+	for (i = 0; i < get_max(arg); i++) {			\
+		struct json_object *jobj;			\
+		int64_t align;					\
+								\
+		align = get_elem(arg, i);			\
+		jobj = json_object_new_int64(align);		\
+		if (!jobj)					\
+			goto err;				\
+		json_object_array_add(arr, jobj);		\
+	}							\
+								\
+	return arr;						\
+err:								\
+	json_object_put(arr);					\
+	return NULL;						\
+}
+#define SZ(type, kind) _SZ(ndctl_##type##_get_num_##kind##s, \
+			   ndctl_##type##_get_supported_##kind, ndctl_##type)
+SZ(pfn, alignment)
+SZ(dax, alignment)
+SZ(btt, sector_size)
+//SZ(namespace, sector_size)
+
 struct json_object *util_daxctl_region_to_json(struct daxctl_region *region,
 		const char *ident, unsigned long flags)
 {
@@ -739,7 +771,7 @@  struct json_object *util_namespace_to_json(struct ndctl_namespace *ndns,
 {
 	struct json_object *jndns = json_object_new_object();
 	enum ndctl_pfn_loc loc = NDCTL_PFN_LOC_NONE;
-	struct json_object *jobj, *jbbs = NULL;
+	struct json_object *jobj, *jbbs = NULL, *size_array = NULL;
 	const char *locations[] = {
 		[NDCTL_PFN_LOC_NONE] = "none",
 		[NDCTL_PFN_LOC_RAM] = "mem",
@@ -749,6 +781,7 @@  struct json_object *util_namespace_to_json(struct ndctl_namespace *ndns,
 	unsigned int sector_size = UINT_MAX;
 	enum ndctl_namespace_mode mode;
 	const char *bdev = NULL, *name;
+	const char *size_array_name;
 	unsigned int bb_count = 0;
 	struct ndctl_btt *btt;
 	struct ndctl_pfn *pfn;
@@ -936,6 +969,19 @@  struct json_object *util_namespace_to_json(struct ndctl_namespace *ndns,
 			json_object_object_add(jndns, "numa_node", jobj);
 	}
 
+	if (pfn) {
+		size_array_name = "supported_alignments";
+		size_array = ndctl_pfn_build_size_array(pfn);
+	} else if (dax) {
+		size_array_name = "supported_alignments";
+		size_array = ndctl_dax_build_size_array(dax);
+	} else if (btt) {
+		size_array_name = "supported sector sizes";
+		size_array = ndctl_btt_build_size_array(btt);
+	}
+	if (size_array && flags & UTIL_JSON_VERBOSE)
+		json_object_object_add(jndns, size_array_name, size_array);
+
 	if (pfn)
 		jbbs = util_pfn_badblocks_to_json(pfn, &bb_count, flags);
 	else if (dax)