]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
fix list command bugs
authorchengjike <chengjike.cheng@huawei.com>
Mon, 23 Aug 2021 12:40:47 +0000 (20:40 +0800)
committerKeith Busch <kbusch@kernel.org>
Mon, 23 Aug 2021 16:18:50 +0000 (09:18 -0700)
When execute the "nvme list" command in a multipath environment, it generate a core dump. Such as:
[root@localhost ~]# nvme list
Node                  SN                   Model                                    Namespace Usage                      Format           FW Rev
--------------------- -------------------- ---------------------------------------- --------- -------------------------- ---------------- --------
Segmentation fault (core dumped)
[root@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@huawei.com>
Signed-off-by: Keith Busch <kbusch@kernel.org>
nvme-print.c

index 9ef3ab2904785b92da261bb6b237b9fd52623963..d321ec0c96849896ee6b23516ec9527973bba7c8 100755 (executable)
@@ -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);