Skip to content

Commit d8ef906

Browse files
committed
nvme: output NS management command result by json
This is to use the command result NSID created by TP. Signed-off-by: Tokunori Ikegami <[email protected]>
1 parent 6ae8d7d commit d8ef906

1 file changed

Lines changed: 27 additions & 23 deletions

File tree

nvme.c

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2825,26 +2825,39 @@ static int id_endurance_grp_list(int argc, char **argv, struct command *cmd,
28252825
return err;
28262826
}
28272827

2828-
static void ns_mgmt_show_error(struct nvme_dev *dev, int err, const char *cmd)
2828+
static bool is_ns_mgmt_support(struct nvme_dev *dev)
28292829
{
2830-
_cleanup_free_ struct nvme_id_ctrl *ctrl = NULL;
2830+
int err;
28312831

2832+
_cleanup_free_ struct nvme_id_ctrl *ctrl = nvme_alloc(sizeof(*ctrl));
2833+
2834+
if (ctrl) {
2835+
err = nvme_cli_identify_ctrl(dev, ctrl);
2836+
if (err)
2837+
goto out;
2838+
if (le16_to_cpu(ctrl->oacs) & NVME_CTRL_OACS_NS_MGMT)
2839+
return true;
2840+
return false;
2841+
}
2842+
out:
2843+
return true;
2844+
}
2845+
2846+
static void ns_mgmt_show_status(struct nvme_dev *dev, int err, char *cmd, __u32 nsid)
2847+
{
28322848
if (err < 0) {
2833-
nvme_show_error("%s namespace: %s", cmd, nvme_strerror(errno));
2849+
nvme_show_error("%s: %s", cmd, nvme_strerror(errno));
28342850
return;
28352851
}
28362852

28372853
nvme_show_init();
28382854

2839-
nvme_show_status(err);
2840-
2841-
ctrl = nvme_alloc(sizeof(*ctrl));
2842-
if (!ctrl)
2843-
return;
2844-
2845-
err = nvme_cli_identify_ctrl(dev, ctrl);
28462855
if (!err) {
2847-
if (!(le16_to_cpu(ctrl->oacs) & NVME_CTRL_OACS_NS_MGMT))
2856+
nvme_show_key_value(cmd, "success");
2857+
nvme_show_key_value("nsid", "%d", nsid);
2858+
} else {
2859+
nvme_show_status(err);
2860+
if (!is_ns_mgmt_support(dev))
28482861
nvme_show_result("NS management and attachment not supported");
28492862
}
28502863

@@ -2897,10 +2910,7 @@ static int delete_ns(int argc, char **argv, struct command *cmd, struct plugin *
28972910
}
28982911

28992912
err = nvme_cli_ns_mgmt_delete(dev, cfg.namespace_id, nvme_cfg.timeout);
2900-
if (!err)
2901-
printf("%s: Success, deleted nsid:%d\n", cmd->name, cfg.namespace_id);
2902-
else
2903-
ns_mgmt_show_error(dev, err, "delete");
2913+
ns_mgmt_show_status(dev, err, cmd->name, cfg.namespace_id);
29042914

29052915
return err;
29062916
}
@@ -2974,10 +2984,7 @@ static int nvme_attach_ns(int argc, char **argv, int attach, const char *desc, s
29742984
err = nvme_cli_ns_detach_ctrls(dev, cfg.namespace_id,
29752985
cntlist);
29762986

2977-
if (!err)
2978-
printf("%s: Success, nsid:%d\n", cmd->name, cfg.namespace_id);
2979-
else
2980-
ns_mgmt_show_error(dev, err, attach ? "attach" : "detach");
2987+
ns_mgmt_show_status(dev, err, cmd->name, cfg.namespace_id);
29812988

29822989
return err;
29832990
}
@@ -3357,10 +3364,7 @@ static int create_ns(int argc, char **argv, struct command *cmd, struct plugin *
33573364
data->phndl[i] = cpu_to_le16(phndl[i]);
33583365

33593366
err = nvme_cli_ns_mgmt_create(dev, data, &nsid, nvme_cfg.timeout, cfg.csi);
3360-
if (!err)
3361-
printf("%s: Success, created nsid:%d\n", cmd->name, nsid);
3362-
else
3363-
ns_mgmt_show_error(dev, err, "create");
3367+
ns_mgmt_show_status(dev, err, cmd->name, nsid);
33643368

33653369
return err;
33663370
}

0 commit comments

Comments
 (0)