anitazha / rpms / ndctl

Forked from rpms/ndctl 2 years ago
Clone

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

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