[PATCH V4 nvme-cli 3/3] add "Path Access" field in command "nvme list"

chengjike chengjike.cheng at huawei.com
Fri Oct 8 05:25:53 PDT 2021


When users run commands such as nvme list, nvme list -o json, nvme list -v -o json,
"Path Access" entry of each disk is printed if NVMe MultiPath is
configured(Non-multipath disk does not display "Path Access").
The value of /sys/module/nvme_core/parameters/multipath is used to determine
whether the disk is nvme native multipath device.

Signed-off-by: chengjike <chengjike.cheng at huawei.com>
---
 nvme-print.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 58 insertions(+), 5 deletions(-)

diff --git a/nvme-print.c b/nvme-print.c
index 6c330d8..faf8e7c 100644
--- a/nvme-print.c
+++ b/nvme-print.c
@@ -5734,22 +5734,71 @@ void nvme_show_list_item(nvme_ns_t n)
 		nvme_ns_get_firmware(n));
 }
 
+void nvme_show_mpath_list_item(nvme_ns_t n)
+{
+	char usage[128] = { 0 }, format[128] = { 0 };
+
+	long long lba = nvme_ns_get_lba_size(n);
+	double nsze = nvme_ns_get_lba_count(n) * lba;
+	double nuse = nvme_ns_get_lba_util(n) * lba;
+
+	const char *s_suffix = suffix_si_get(&nsze);
+	const char *u_suffix = suffix_si_get(&nuse);
+	const char *l_suffix = suffix_binary_get(&lba);
+
+	snprintf(usage, sizeof(usage), "%6.2f %2sB / %6.2f %2sB", nuse,
+		u_suffix, nsze, s_suffix);
+	snprintf(format, sizeof(format), "%3.0f %2sB + %2d B", (double)lba,
+		l_suffix, nvme_ns_get_meta_size(n));
+
+	printf("%-21s %-20s %-40s %-9d %-26s %-16s %-8s %-12s\n",
+		nvme_ns_get_name(n), nvme_ns_get_serial(n),
+		nvme_ns_get_model(n), nvme_ns_get_nsid(n), usage, format,
+		nvme_ns_get_firmware(n), nvme_ns_get_access(n));
+}
+
+
+static bool nvme_check_mpath()
+{
+	char *mpath = NULL;
+
+	mpath = nvme_get_attr("/sys/module/nvme_core/parameters", "multipath");  
+	if (!mpath)
+		return false;
+
+	if (!strcmp(mpath, "Y")) {
+		free(mpath);
+		return true;
+	}
+
+	free(mpath);
+	return false;
+}
+
 static void nvme_show_simple_list(nvme_root_t r)
 {
 	nvme_host_t h;
 	nvme_subsystem_t s;
 	nvme_ctrl_t c;
 	nvme_ns_t n;
+	bool flag = nvme_check_mpath();
 
-	printf("%-21s %-20s %-40s %-9s %-26s %-16s %-8s\n",
-	    "Node", "SN", "Model", "Namespace", "Usage", "Format", "FW Rev");
-	printf("%-.21s %-.20s %-.40s %-.9s %-.26s %-.16s %-.8s\n", dash, dash,
-		dash, dash, dash, dash, dash);
+	if (flag) {
+		printf("%-21s %-20s %-40s %-9s %-26s %-16s %-8s %-12s\n",
+			"Node", "SN", "Model", "Namespace", "Usage", "Format", "FW Rev", "Path Access");
+		printf("%-.21s %-.20s %-.40s %-.9s %-.26s %-.16s %-.8s %-.12s\n", dash, dash,
+			dash, dash, dash, dash, dash, dash);
+	} else {
+		printf("%-21s %-20s %-40s %-9s %-26s %-16s %-8s\n",
+			"Node", "SN", "Model", "Namespace", "Usage", "Format", "FW Rev");
+		printf("%-.21s %-.20s %-.40s %-.9s %-.26s %-.16s %-.8s\n", dash, dash,
+			dash, dash, dash, dash, dash);
+	}
 
 	nvme_for_each_host(r, h) {
 		nvme_for_each_subsystem(h, s) {
 			nvme_subsystem_for_each_ns(s, n)
-				nvme_show_list_item(n);
+				nvme_show_mpath_list_item(n);
 
 			nvme_subsystem_for_each_ctrl(s, c)
 				nvme_ctrl_for_each_ns(c, n)
@@ -5952,6 +6001,7 @@ static void json_detail_list(nvme_root_t r)
 				json_object_add_value_int(jns, "maxlba", nvme_ns_get_lba_count(n));
 				json_object_add_value_int(jns, "capacity", nsze);
 				json_object_add_value_int(jns, "sector", lba);
+				json_object_add_value_string(jns, "path access", nvme_ns_get_access(n));
 
 				json_array_add_value_object(jnss, jns);
 			}
@@ -5971,6 +6021,7 @@ static void json_detail_list(nvme_root_t r)
 
 static struct json_object *json_list_item(nvme_ns_t n)
 {
+	bool flag = nvme_check_mpath();
 	struct json_object *jdevice = json_create_object();
 
 	long long lba = nvme_ns_get_lba_size(n);
@@ -5986,6 +6037,8 @@ static struct json_object *json_list_item(nvme_ns_t n)
 	json_object_add_value_int(jdevice, "maxlba", nvme_ns_get_lba_count(n));
 	json_object_add_value_int(jdevice, "capacity", nsze);
 	json_object_add_value_int(jdevice, "sector", lba);
+	if (flag)
+		json_object_add_value_string(jdevice, "path access", nvme_ns_get_access(n));
 
 	return jdevice;
 }
-- 
2.21.0.windows.1




More information about the Linux-nvme mailing list