]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
nvme: Fix get-feature command mixed stdout and json outputs
authorTokunori Ikegami <ikegami.t@gmail.com>
Thu, 12 Oct 2023 13:52:37 +0000 (22:52 +0900)
committerDaniel Wagner <wagi@monom.org>
Thu, 16 Nov 2023 10:05:54 +0000 (11:05 +0100)
Signed-off-by: Tokunori Ikegami <ikegami.t@gmail.com>
nvme-print-binary.c
nvme-print-json.c
nvme-print-stdout.c
nvme-print.c
nvme-print.h
nvme.c

index 23d176d1538947147c8446fdb2af15bdc46000ca..bbca97375c10993bc188ccd1f8b32f4eb80f2f72 100644 (file)
@@ -355,6 +355,7 @@ static struct print_ops binary_print_ops = {
        .zns_id_ctrl                    = binary_zns_id_ctrl,
        .zns_id_ns                      = binary_zns_id_ns,
        .zns_report_zones               = binary_zns_report_zones,
+       .show_feature                   = NULL,
        .show_feature_fields            = NULL,
        .id_ctrl_rpmbs                  = NULL,
        .lba_range                      = NULL,
index 6462e08f3217aa0d9f3e26aa7a45cb5dbe7399c8..dbe06f9ce39b6f6b36b67f5aa878069ae5fb627e 100644 (file)
@@ -2826,6 +2826,23 @@ static void json_feature_show_fields_fdp_events(unsigned int result, unsigned ch
        json_print(root);
 }
 
+static void json_feature_show(enum nvme_features_id fid, int sel, unsigned int result)
+{
+       struct json_object *root = json_create_object();
+       char json_str[STR_LEN];
+
+       sprintf(json_str, "%#0*x", fid ? 4 : 2, fid);
+       json_object_add_value_string(root, "feature", json_str);
+
+       json_object_add_value_string(root, "name", nvme_feature_to_string(fid));
+
+       sprintf(json_str, "%#0*x", result ? 10 : 8, result);
+       json_object_add_value_string(root, nvme_select_to_string(sel), json_str);
+
+       json_print(root);
+}
+
+
 static void json_feature_show_fields(enum nvme_features_id fid, unsigned int result,
                                     unsigned char *buf)
 {
@@ -3803,6 +3820,7 @@ static struct print_ops json_print_ops = {
        .zns_id_ctrl                    = json_nvme_zns_id_ctrl,
        .zns_id_ns                      = json_nvme_zns_id_ns,
        .zns_report_zones               = json_nvme_zns_report_zones,
+       .show_feature                   = json_feature_show,
        .show_feature_fields            = json_feature_show_fields,
        .id_ctrl_rpmbs                  = NULL,
        .lba_range                      = NULL,
index ada33a17233707125f2e52db0089954085263988..a0f5dd1c8a47188b88146463100904aadccc6ea9 100644 (file)
@@ -4355,6 +4355,12 @@ static void stdout_host_metadata(enum nvme_features_id fid,
        }
 }
 
+static void stdout_feature_show(enum nvme_features_id fid, int sel, unsigned int result)
+{
+       printf("get-feature:%#0*x (%s), %s value:%#0*x\n", fid ? 4 : 2, fid,
+              nvme_feature_to_string(fid), nvme_select_to_string(sel), result ? 10 : 8, result);
+}
+
 static void stdout_feature_show_fields(enum nvme_features_id fid,
                                       unsigned int result,
                                       unsigned char *buf)
@@ -5128,6 +5134,7 @@ static struct print_ops stdout_print_ops = {
        .zns_id_ctrl                    = stdout_zns_id_ctrl,
        .zns_id_ns                      = stdout_zns_id_ns,
        .zns_report_zones               = stdout_zns_report_zones,
+       .show_feature                   = stdout_feature_show,
        .show_feature_fields            = stdout_feature_show_fields,
        .id_ctrl_rpmbs                  = stdout_id_ctrl_rpmbs,
        .lba_range                      = stdout_lba_range,
index 72a9f6aa5b88ffe247644d36a3a16b78eac23f30..f4c3cc81dd6e621b2380afd1e0e2e3a3aff6ac64 100644 (file)
@@ -970,6 +970,11 @@ const char *nvme_host_metadata_type_to_string(enum nvme_features_id fid,
        }
 }
 
+void nvme_feature_show(enum nvme_features_id fid, int sel, unsigned int result)
+{
+       nvme_print(show_feature, 0, fid, sel, result);
+}
+
 void nvme_feature_show_fields(enum nvme_features_id fid, unsigned int result, unsigned char *buf)
 {
        nvme_print(show_feature_fields, 0, fid, result, buf);
index bc1d857677397147db62422a81267b49bc85ca62..88ff9ed08c388be4e90ff1d48f87f402dd386588 100644 (file)
@@ -77,6 +77,7 @@ struct print_ops {
        void (*zns_id_ctrl)(struct nvme_zns_id_ctrl *ctrl);
        void (*zns_id_ns)(struct nvme_zns_id_ns *ns, struct nvme_id_ns *id_ns);
        void (*zns_report_zones)(void *report, __u32 descs, __u8 ext_size, __u32 report_size, struct json_object *zone_list);
+       void (*show_feature)(enum nvme_features_id id, int sel, unsigned int result);
        void (*show_feature_fields)(enum nvme_features_id id, unsigned int result, unsigned char *buf);
        void (*id_ctrl_rpmbs)(__le32 ctrl_rpmbs);
        void (*lba_range)(struct nvme_lba_range_type *lbrt, int nr_ranges);
@@ -208,6 +209,7 @@ void nvme_show_topology(nvme_root_t t,
                        enum nvme_cli_topo_ranking ranking,
                        enum nvme_print_flags flags);
 
+void nvme_feature_show(enum nvme_features_id fid, int sel, unsigned int result);
 void nvme_feature_show_fields(enum nvme_features_id fid, unsigned int result, unsigned char *buf);
 void nvme_directive_show(__u8 type, __u8 oper, __u16 spec, __u32 nsid, __u32 result,
        void *buf, __u32 len, enum nvme_print_flags flags);
diff --git a/nvme.c b/nvme.c
index 24f5329041cedefe4bb780be67eda6499cc05ffb..f4b79efed98bd7f9feb50b8bb36c97e18a4b9f11 100644 (file)
--- a/nvme.c
+++ b/nvme.c
@@ -4570,11 +4570,7 @@ static void get_feature_id_print(struct feat_cfg cfg, int err, __u32 result,
 
        if (!err) {
                if (!cfg.raw_binary || !buf) {
-                       printf("get-feature:%#0*x (%s), %s value:%#0*x\n",
-                              cfg.feature_id ? 4 : 2, cfg.feature_id,
-                              nvme_feature_to_string(cfg.feature_id),
-                              nvme_select_to_string(cfg.sel), result ? 10 : 8,
-                              result);
+                       nvme_feature_show(cfg.feature_id, cfg.sel, result);
                        if (cfg.sel == 3)
                                nvme_show_select_result(result);
                        else if (cfg.human_readable)