]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
nvme: add json output format for list_ns
authorGollu Appalanaidu <anaidu.gollu@samsung.com>
Sun, 16 May 2021 04:19:02 +0000 (09:49 +0530)
committerKeith Busch <kbusch@kernel.org>
Tue, 25 May 2021 14:36:28 +0000 (08:36 -0600)
Signed-off-by: Gollu Appalanaidu <anaidu.gollu@samsung.com>
Documentation/nvme-list-ns.1
Documentation/nvme-list-ns.html
Documentation/nvme-list-ns.txt
nvme-print.c
nvme-print.h
nvme.c

index 084eb3e8e4b97558b8120bb0836c8ec3dfdfe935..6abe99fa2111f033a81be1f17ea566f6f3340d09 100644 (file)
@@ -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 <http://docbook.sf.net/>
-.\"      Date: 10/20/2020
+.\"    Author: [FIXME: author] [see http://docbook.sf.net/el/author]
+.\" Generator: DocBook XSL Stylesheets v1.79.1 <http://docbook.sf.net/>
+.\"      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 <device> [\-\-namespace\-id=<nsid> | \-n <nsid>]
                         [\-\-csi=<command_set_identifier> | \-y <command_set_identifier>]
-                        [\-\-all | \-a]
+                        [\-\-all | \-a] [\-\-output\-format=<fmt> | \-o <fmt>]
 .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 <format>, \-\-output\-format=<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
index f88f3e85aff5c6b8c852496899dcc405c6505d08..c79b81bb8a2e402d079bffeb8b389f13b771abbd 100644 (file)
@@ -751,7 +751,7 @@ nvme-id-ns(1) Manual Page
 <div class="verseblock">\r
 <pre class="content"><em>nvme list-ns</em> &lt;device&gt; [--namespace-id=&lt;nsid&gt; | -n &lt;nsid&gt;]\r
                         [--csi=&lt;command_set_identifier&gt; | -y &lt;command_set_identifier&gt;]\r
-                        [--all | -a]</pre>\r
+                        [--all | -a] [--output-format=&lt;fmt&gt; | -o &lt;fmt&gt;]</pre>\r
 <div class="attribution">\r
 </div></div>\r
 </div>\r
@@ -808,13 +808,44 @@ a valid nsid.</p></div>
         subsystem, whether attached or inactive.\r
 </p>\r
 </dd>\r
+<dt class="hdlist1">\r
+-o &lt;format&gt;\r
+</dt>\r
+<dt class="hdlist1">\r
+--output-format=&lt;format&gt;\r
+</dt>\r
+<dd>\r
+<p>\r
+    Set the reporting format to <em>normal</em>, or <em>json</em>.\r
+    Only one output format can be used at a time.\r
+</p>\r
+</dd>\r
 </dl></div>\r
 </div>\r
 </div>\r
 <div class="sect1">\r
 <h2 id="_examples">EXAMPLES</h2>\r
 <div class="sectionbody">\r
-<div class="paragraph"><p>No examples yet.</p></div>\r
+<div class="ulist"><ul>\r
+<li>\r
+<p>\r
+Print the namespaces present for zoned command set in JSON format\r
+</p>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><code># nvme list-ns /dev/nvme0 -y 2 -a -o json</code></pre>\r
+</div></div>\r
+</li>\r
+<li>\r
+<p>\r
+Print the namespaces present for NVM Command Set in normal format\r
+</p>\r
+<div class="listingblock">\r
+<div class="content">\r
+<pre><code># nvme list-ns /dev/nvme0</code></pre>\r
+</div></div>\r
+</li>\r
+</ul></div>\r
 </div>\r
 </div>\r
 <div class="sect1">\r
@@ -828,7 +859,7 @@ a valid nsid.</p></div>
 <div id="footer">\r
 <div id="footer-text">\r
 Last updated\r
- 2020-05-06 10:35:44 CEST\r
+ 2021-05-16 10:13:29 IST\r
 </div>\r
 </div>\r
 </body>\r
index 6d7c1f495ae9ed3bac49ae92b37faa200ce10adb..0ccd1adaffdeb70524ecb495ff888166d7c7f0eb 100644 (file)
@@ -10,7 +10,7 @@ SYNOPSIS
 [verse]
 '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>]
 
 DESCRIPTION
 -----------
@@ -41,9 +41,24 @@ OPTIONS
        Retrieve the identify list structure for all namespaces in the
        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
+------------
 
 NVME
 ----
index 09a4dcbc1a85203855d95c9de7ab369a1dae36ee..2ede782346b2e257e39f33e65034d7e645456c9c 100755 (executable)
@@ -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)
 {
index 3eb9c519a3d86948d73e624a396cccfc767158e4..a988dd0fc2e988b3e23b50e51da627ffe9a24ed1 100644 (file)
@@ -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 3a672e869c30f0842a997941d29ec6097579ff86..6b89797c4220bd2cd4a83b1ae93db22f3cbfd632 100644 (file)
--- 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");