Blame SOURCES/0181-cxl-memdev-Fix-json-for-multi-device-partitioning.patch

2eb93d
From 8ed95d22504d7b2b258d1800878e32c162badf8c Mon Sep 17 00:00:00 2001
2eb93d
From: Dan Williams <dan.j.williams@intel.com>
2eb93d
Date: Thu, 14 Jul 2022 10:02:27 -0700
2eb93d
Subject: [PATCH 181/217] cxl/memdev: Fix json for multi-device partitioning
2eb93d
2eb93d
In the case when someone partitions several devices at once, collect all
2eb93d
the affected memdevs into a json array.
2eb93d
2eb93d
With the move to use util_display_json_array() that also requires a set of
2eb93d
flags to be specifiied. Apply the UTIL_JSON_HUMAN flag for all interactive
2eb93d
command result output to bring this command in line with other tools.
2eb93d
2eb93d
Link: https://lore.kernel.org/r/165781814737.1555691.889129128205037941.stgit@dwillia2-xfh.jf.intel.com
2eb93d
Cc: Alison Schofield <alison.schofield@intel.com>
2eb93d
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
2eb93d
Signed-off-by: Vishal Verma <vishal.l.verma@intel.com>
2eb93d
---
2eb93d
 cxl/memdev.c | 26 +++++++++++++++++++++-----
2eb93d
 1 file changed, 21 insertions(+), 5 deletions(-)
2eb93d
2eb93d
diff --git a/cxl/memdev.c b/cxl/memdev.c
2eb93d
index 91d914d..9fcd8ae 100644
2eb93d
--- a/cxl/memdev.c
2eb93d
+++ b/cxl/memdev.c
2eb93d
@@ -19,6 +19,7 @@
2eb93d
 struct action_context {
2eb93d
 	FILE *f_out;
2eb93d
 	FILE *f_in;
2eb93d
+	struct json_object *jdevs;
2eb93d
 };
2eb93d
 
2eb93d
 static struct parameters {
2eb93d
@@ -339,12 +340,13 @@ out:
2eb93d
 }
2eb93d
 
2eb93d
 static int action_setpartition(struct cxl_memdev *memdev,
2eb93d
-		struct action_context *actx)
2eb93d
+			       struct action_context *actx)
2eb93d
 {
2eb93d
 	const char *devname = cxl_memdev_get_devname(memdev);
2eb93d
 	enum cxl_setpart_type type = CXL_SETPART_PMEM;
2eb93d
 	unsigned long long size = ULLONG_MAX;
2eb93d
 	struct json_object *jmemdev;
2eb93d
+	unsigned long flags;
2eb93d
 	struct cxl_cmd *cmd;
2eb93d
 	int rc;
2eb93d
 
2eb93d
@@ -396,10 +398,12 @@ out_err:
2eb93d
 	if (rc)
2eb93d
 		log_err(&ml, "%s error: %s\n", devname, strerror(-rc));
2eb93d
 
2eb93d
-	jmemdev = util_cxl_memdev_to_json(memdev, UTIL_JSON_PARTITION);
2eb93d
-	if (jmemdev)
2eb93d
-		printf("%s\n", json_object_to_json_string_ext(jmemdev,
2eb93d
-		       JSON_C_TO_STRING_PRETTY));
2eb93d
+	flags = UTIL_JSON_PARTITION;
2eb93d
+	if (actx->f_out == stdout && isatty(1))
2eb93d
+		flags |= UTIL_JSON_HUMAN;
2eb93d
+	jmemdev = util_cxl_memdev_to_json(memdev, flags);
2eb93d
+	if (actx->jdevs && jmemdev)
2eb93d
+		json_object_array_add(actx->jdevs, jmemdev);
2eb93d
 
2eb93d
 	return rc;
2eb93d
 }
2eb93d
@@ -446,6 +450,9 @@ static int memdev_action(int argc, const char **argv, struct cxl_ctx *ctx,
2eb93d
 		err++;
2eb93d
 	}
2eb93d
 
2eb93d
+	if (action == action_setpartition)
2eb93d
+		actx.jdevs = json_object_new_array();
2eb93d
+
2eb93d
 	if (err == argc) {
2eb93d
 		usage_with_options(u, options);
2eb93d
 		return -EINVAL;
2eb93d
@@ -528,6 +535,15 @@ static int memdev_action(int argc, const char **argv, struct cxl_ctx *ctx,
2eb93d
 	if (actx.f_in != stdin)
2eb93d
 		fclose(actx.f_in);
2eb93d
 
2eb93d
+	if (actx.jdevs) {
2eb93d
+		unsigned long flags = 0;
2eb93d
+
2eb93d
+		if (actx.f_out == stdout && isatty(1))
2eb93d
+			flags |= UTIL_JSON_HUMAN;
2eb93d
+		util_display_json_array(actx.f_out, actx.jdevs, flags);
2eb93d
+	}
2eb93d
+
2eb93d
+
2eb93d
  out_close_fout:
2eb93d
 	if (actx.f_out != stdout)
2eb93d
 		fclose(actx.f_out);
2eb93d
-- 
2eb93d
2.27.0
2eb93d