[PATCH nvme-cli] fix list command bugs

chengjike chengjike.cheng at huawei.com
Mon Aug 23 05:40:47 PDT 2021


When execute the "nvme list" command in a multipath environment, it generate a core dump. Such as:
[root at localhost ~]# nvme list
Node                  SN                   Model                                    Namespace Usage                      Format           FW Rev
--------------------- -------------------- ---------------------------------------- --------- -------------------------- ---------------- --------
Segmentation fault (core dumped)
[root at localhost ~]#

The reason is that when a device is a multipath disk, the value of "nvme_ns_t->c" is NULL.
This also happens when you execute "nvme list -o json" and "nvme list -v -o json" commands.

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

diff --git a/nvme-print.c b/nvme-print.c
index 9ef3ab2..d321ec0 100644
--- a/nvme-print.c
+++ b/nvme-print.c
@@ -5642,17 +5642,15 @@ static void nvme_show_list_item(nvme_ns_t n)
 	const char *u_suffix = suffix_si_get(&nuse);
 	const char *l_suffix = suffix_binary_get(&lba);
 
-	nvme_ctrl_t c = nvme_ns_get_ctrl(n);
-
 	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\n",
-		nvme_ns_get_name(n), nvme_ctrl_get_serial(c),
-		nvme_ctrl_get_model(c), nvme_ns_get_nsid(n), usage, format,
-		nvme_ctrl_get_firmware(c));
+		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));
 }
 
 static void nvme_show_simple_list(nvme_root_t r)
@@ -5794,6 +5792,7 @@ static void nvme_show_detailed_list(nvme_root_t r)
 static void json_detail_list(nvme_root_t r)
 {
 	struct json_object *jroot = json_create_object();
+	struct json_object *jdev = json_create_array();
 	struct json_object *jsslist = json_create_array();
 
 	nvme_host_t h;
@@ -5881,8 +5880,9 @@ static void json_detail_list(nvme_root_t r)
 		}
 
 		json_object_add_value_object(hss, "subsystems", jsslist);
-		json_array_add_value_object(jroot, hss);
+		json_array_add_value_object(jdev, hss);
 	}
+	json_object_add_value_array(jroot, "Devices", jdev);
 	json_print_object(jroot, NULL);
 	printf("\n");
 	json_free_object(jroot);
@@ -5891,7 +5891,6 @@ static void json_detail_list(nvme_root_t r)
 static struct json_object *json_list_item(nvme_ns_t n)
 {
 	struct json_object *jdevice = json_create_object();
-	nvme_ctrl_t c = nvme_ns_get_ctrl(n);
 
 	long long lba = nvme_ns_get_lba_size(n);
 	double nsze = nvme_ns_get_lba_count(n) * lba;
@@ -5899,9 +5898,9 @@ static struct json_object *json_list_item(nvme_ns_t n)
 
 	json_object_add_value_int(jdevice, "namespace", nvme_ns_get_nsid(n));
 	json_object_add_value_string(jdevice, "device", nvme_ns_get_name(n));
-	json_object_add_value_string(jdevice, "firmware", nvme_ctrl_get_firmware(c));
-	json_object_add_value_string(jdevice, "model", nvme_ctrl_get_model(c));
-	json_object_add_value_string(jdevice, "serial", nvme_ctrl_get_serial(c));
+	json_object_add_value_string(jdevice, "firmware", nvme_ns_get_firmware(n));
+	json_object_add_value_string(jdevice, "model", nvme_ns_get_model(n));
+	json_object_add_value_string(jdevice, "serial", nvme_ns_get_serial(n));
 	json_object_add_value_int(jdevice, "util", nuse);
 	json_object_add_value_int(jdevice, "maxlba", nvme_ns_get_lba_count(n));
 	json_object_add_value_int(jdevice, "capacity", nsze);
-- 
2.24.0.windows.2




More information about the Linux-nvme mailing list