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

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