From c478c1e9291722ed22aa90249043d626759c60bd Mon Sep 17 00:00:00 2001 From: Gollu Appalanaidu Date: Sun, 16 May 2021 09:49:02 +0530 Subject: [PATCH] nvme: add json output format for list_ns Signed-off-by: Gollu Appalanaidu --- Documentation/nvme-list-ns.1 | 59 +++++++++++++++++++++++++++++---- Documentation/nvme-list-ns.html | 37 +++++++++++++++++++-- Documentation/nvme-list-ns.txt | 19 +++++++++-- nvme-print.c | 36 ++++++++++++++++++++ nvme-print.h | 1 + nvme.c | 28 +++++++++++----- 6 files changed, 160 insertions(+), 20 deletions(-) diff --git a/Documentation/nvme-list-ns.1 b/Documentation/nvme-list-ns.1 index 084eb3e8..6abe99fa 100644 --- a/Documentation/nvme-list-ns.1 +++ b/Documentation/nvme-list-ns.1 @@ -1,13 +1,13 @@ '\" t .\" Title: nvme-id-ns -.\" Author: [FIXME: author] [see http://www.docbook.org/tdg5/en/html/author] -.\" Generator: DocBook XSL Stylesheets vsnapshot -.\" Date: 10/20/2020 +.\" Author: [FIXME: author] [see http://docbook.sf.net/el/author] +.\" Generator: DocBook XSL Stylesheets v1.79.1 +.\" Date: 05/16/2021 .\" Manual: NVMe Manual .\" Source: NVMe .\" Language: English .\" -.TH "NVME\-ID\-NS" "1" "10/20/2020" "NVMe" "NVMe Manual" +.TH "NVME\-ID\-NS" "1" "05/16/2021" "NVMe" "NVMe Manual" .\" ----------------------------------------------------------------- .\" * Define some portability stuff .\" ----------------------------------------------------------------- @@ -34,7 +34,7 @@ nvme-list-ns \- Send NVMe Identify List Namespaces, return result and structure .nf \fInvme list\-ns\fR [\-\-namespace\-id= | \-n ] [\-\-csi= | \-y ] - [\-\-all | \-a] + [\-\-all | \-a] [\-\-output\-format= | \-o ] .fi .SH "DESCRIPTION" .sp @@ -59,9 +59,56 @@ If this value is given, retrieve the identify list structure associated with the .RS 4 Retrieve the identify list structure for all namespaces in the subsystem, whether attached or inactive\&. .RE +.PP +\-o , \-\-output\-format= +.RS 4 +Set the reporting format to +\fInormal\fR, or +\fIjson\fR\&. Only one output format can be used at a time\&. +.RE .SH "EXAMPLES" .sp -No examples yet\&. +.RS 4 +.ie n \{\ +\h'-04'\(bu\h'+03'\c +.\} +.el \{\ +.sp -1 +.IP \(bu 2.3 +.\} +Print the namespaces present for zoned command set in JSON format +.sp +.if n \{\ +.RS 4 +.\} +.nf +# nvme list\-ns /dev/nvme0 \-y 2 \-a \-o json +.fi +.if n \{\ +.RE +.\} +.RE +.sp +.RS 4 +.ie n \{\ +\h'-04'\(bu\h'+03'\c +.\} +.el \{\ +.sp -1 +.IP \(bu 2.3 +.\} +Print the namespaces present for NVM Command Set in normal format +.sp +.if n \{\ +.RS 4 +.\} +.nf +# nvme list\-ns /dev/nvme0 +.fi +.if n \{\ +.RE +.\} +.RE .SH "NVME" .sp Part of the nvme\-user suite diff --git a/Documentation/nvme-list-ns.html b/Documentation/nvme-list-ns.html index f88f3e85..c79b81bb 100644 --- a/Documentation/nvme-list-ns.html +++ b/Documentation/nvme-list-ns.html @@ -751,7 +751,7 @@ nvme-id-ns(1) Manual Page
nvme list-ns <device> [--namespace-id=<nsid> | -n <nsid>]
                         [--csi=<command_set_identifier> | -y <command_set_identifier>]
-                        [--all | -a]
+ [--all | -a] [--output-format=<fmt> | -o <fmt>]
@@ -808,13 +808,44 @@ a valid nsid.

subsystem, whether attached or inactive.

+
+-o <format> +
+
+--output-format=<format> +
+
+

+ Set the reporting format to normal, or json. + Only one output format can be used at a time. +

+

EXAMPLES

-

No examples yet.

+
    +
  • +

    +Print the namespaces present for zoned command set in JSON format +

    +
    +
    +
    # nvme list-ns /dev/nvme0 -y 2 -a -o json
    +
    +
  • +
  • +

    +Print the namespaces present for NVM Command Set in normal format +

    +
    +
    +
    # nvme list-ns /dev/nvme0
    +
    +
  • +
@@ -828,7 +859,7 @@ a valid nsid.

diff --git a/Documentation/nvme-list-ns.txt b/Documentation/nvme-list-ns.txt index 6d7c1f49..0ccd1ada 100644 --- a/Documentation/nvme-list-ns.txt +++ b/Documentation/nvme-list-ns.txt @@ -10,7 +10,7 @@ SYNOPSIS [verse] 'nvme list-ns' [--namespace-id= | -n ] [--csi= | -y ] - [--all | -a] + [--all | -a] [--output-format= | -o ] DESCRIPTION ----------- @@ -41,9 +41,24 @@ OPTIONS Retrieve the identify list structure for all namespaces in the subsystem, whether attached or inactive. +-o :: +--output-format=:: + Set the reporting format to 'normal', or 'json'. + Only one output format can be used at a time. + EXAMPLES -------- -No examples yet. +* Print the namespaces present for zoned command set in JSON format ++ +------------ +# nvme list-ns /dev/nvme0 -y 2 -a -o json +------------ + +* Print the namespaces present for NVM Command Set in normal format ++ +------------ +# nvme list-ns /dev/nvme0 +------------ NVME ---- diff --git a/nvme-print.c b/nvme-print.c index 09a4dcbc..2ede7823 100755 --- a/nvme-print.c +++ b/nvme-print.c @@ -4075,6 +4075,42 @@ void nvme_show_zns_id_ns(struct nvme_zns_id_ns *ns, } } +static void json_nvme_list_ns(__u32 *ns_list) +{ + struct json_object *root; + struct json_object *valid_attrs; + struct json_object *valid; + int i; + + root = json_create_object(); + valid = json_create_array(); + + for (i = 0; i < 1024; i++) { + if (ns_list[i]) { + valid_attrs = json_create_object(); + json_object_add_value_uint(valid_attrs, "nsid", + le32_to_cpu(ns_list[i])); + json_array_add_value_object(valid, valid_attrs); + } + } + json_object_add_value_array(root, "nsid_list", valid); + json_print_object(root, NULL); + printf("\n"); + json_free_object(root); +} + +void nvme_show_list_ns(__u32 *ns_list, enum nvme_print_flags flags) +{ + int i; + if (flags & JSON) + return json_nvme_list_ns(ns_list); + + for (i = 0; i < 1024; i++) { + if (ns_list[i]) + printf("[%4u]:%#x\n", i, le32_to_cpu(ns_list[i])); + } +} + void nvme_show_zns_changed(struct nvme_zns_changed_zone_log *log, unsigned long flags) { diff --git a/nvme-print.h b/nvme-print.h index 3eb9c519..a988dd0f 100644 --- a/nvme-print.h +++ b/nvme-print.h @@ -86,6 +86,7 @@ void nvme_show_id_uuid_list(const struct nvme_id_uuid_list *uuid_list, void nvme_show_id_iocs(struct nvme_id_iocs *iocs); void nvme_show_list_ctrl(struct nvme_controller_list *ctrl_list, enum nvme_print_flags flags); +void nvme_show_list_ns(__u32 *ns_list, enum nvme_print_flags flags); void nvme_feature_show_fields(enum nvme_feat fid, unsigned int result, unsigned char *buf); void nvme_directive_show(__u8 type, __u8 oper, __u16 spec, __u32 nsid, __u32 result, diff --git a/nvme.c b/nvme.c index 3a672e86..6b89797c 100644 --- a/nvme.c +++ b/nvme.c @@ -1443,23 +1443,27 @@ static int list_ns(int argc, char **argv, struct command *cmd, struct plugin *pl const char *namespace_id = "first nsid returned list should start from"; const char *csi = "I/O command set identifier"; const char *all = "show all namespaces in the subsystem, whether attached or inactive"; - int err, i, fd; + int err, fd; __le32 ns_list[1024]; + enum nvme_print_flags flags; struct config { __u32 namespace_id; int all; __u8 csi; + char *output_format; }; struct config cfg = { .namespace_id = 1, + .output_format = "normal", }; OPT_ARGS(opts) = { - OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id), - OPT_BYTE("csi", 'y', &cfg.csi, csi), - OPT_FLAG("all", 'a', &cfg.all, all), + OPT_UINT("namespace-id", 'n', &cfg.namespace_id, namespace_id), + OPT_BYTE("csi", 'y', &cfg.csi, csi), + OPT_FLAG("all", 'a', &cfg.all, all), + OPT_FMT("output-format", 'o', &cfg.output_format, output_format_no_binary), OPT_END() }; @@ -1467,6 +1471,14 @@ static int list_ns(int argc, char **argv, struct command *cmd, struct plugin *pl if (fd < 0) goto ret; + err = flags = validate_output_format(cfg.output_format); + if (flags < 0) + goto close_fd; + if (flags != JSON && flags != NORMAL) { + err = -EINVAL; + goto close_fd; + } + if (!cfg.namespace_id) { err = -EINVAL; fprintf(stderr, "invalid nsid parameter\n"); @@ -1475,11 +1487,9 @@ static int list_ns(int argc, char **argv, struct command *cmd, struct plugin *pl err = nvme_identify_ns_list_csi(fd, cfg.namespace_id - 1, cfg.csi, !!cfg.all, ns_list); - if (!err) { - for (i = 0; i < 1024; i++) - if (ns_list[i]) - printf("[%4u]:%#x\n", i, le32_to_cpu(ns_list[i])); - } else if (err > 0) + if (!err) + nvme_show_list_ns(ns_list, flags); + else if (err > 0) nvme_show_status(err); else perror("id namespace list"); -- 2.50.1