Skip to content

Commit 34a40cf

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 9eb886c commit 34a40cf

1 file changed

Lines changed: 29 additions & 22 deletions

File tree

nvme.c

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2822,26 +2822,42 @@ static int id_endurance_grp_list(int argc, char **argv, struct command *cmd,
28222822
return err;
28232823
}
28242824

2825-
static void ns_mgmt_show_error(struct nvme_dev *dev, int err, const char *cmd)
2825+
static bool is_ns_mgmt_support(struct nvme_dev *dev)
28262826
{
2827-
_cleanup_free_ struct nvme_id_ctrl *ctrl = NULL;
2827+
int err;
28282828

2829+
_cleanup_free_ struct nvme_id_ctrl *ctrl = nvme_alloc(sizeof(*ctrl));
2830+
2831+
if (ctrl) {
2832+
err = nvme_cli_identify_ctrl(dev, ctrl);
2833+
if (err)
2834+
goto out;
2835+
if (le16_to_cpu(ctrl->oacs) & NVME_CTRL_OACS_NS_MGMT)
2836+
return true;
2837+
return false;
2838+
}
2839+
out:
2840+
return true;
2841+
}
2842+
2843+
static void ns_mgmt_show_status(struct nvme_dev *dev, int err, const char *cmd, char *name,
2844+
__u32 nsid)
2845+
{
28292846
if (err < 0) {
28302847
nvme_show_error("%s namespace: %s", cmd, nvme_strerror(errno));
28312848
return;
28322849
}
28332850

28342851
nvme_show_init();
28352852

2836-
nvme_show_status(err);
2837-
2838-
ctrl = nvme_alloc(sizeof(*ctrl));
2839-
if (!ctrl)
2840-
return;
2841-
2842-
err = nvme_cli_identify_ctrl(dev, ctrl);
28432853
if (!err) {
2844-
if (!(le16_to_cpu(ctrl->oacs) & NVME_CTRL_OACS_NS_MGMT))
2854+
nvme_show_key_value(true, false, name, "Success, %s",
2855+
!strcmp(cmd, "create") ? "created" :
2856+
!strcmp(cmd, "delete") ? "deleted" : "");
2857+
nvme_show_key_value(false, true, "nsid", "%d", nsid);
2858+
} else {
2859+
nvme_show_status(err);
2860+
if (!is_ns_mgmt_support(dev))
28452861
nvme_show_result("NS management and attachment not supported");
28462862
}
28472863

@@ -2894,10 +2910,7 @@ static int delete_ns(int argc, char **argv, struct command *cmd, struct plugin *
28942910
}
28952911

28962912
err = nvme_cli_ns_mgmt_delete(dev, cfg.namespace_id, nvme_cfg.timeout);
2897-
if (!err)
2898-
printf("%s: Success, deleted nsid:%d\n", cmd->name, cfg.namespace_id);
2899-
else
2900-
ns_mgmt_show_error(dev, err, "delete");
2913+
ns_mgmt_show_status(dev, err, "delete", cmd->name, cfg.namespace_id);
29012914

29022915
return err;
29032916
}
@@ -2971,10 +2984,7 @@ static int nvme_attach_ns(int argc, char **argv, int attach, const char *desc, s
29712984
err = nvme_cli_ns_detach_ctrls(dev, cfg.namespace_id,
29722985
cntlist);
29732986

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

29792989
return err;
29802990
}
@@ -3354,10 +3364,7 @@ static int create_ns(int argc, char **argv, struct command *cmd, struct plugin *
33543364
data->phndl[i] = cpu_to_le16(phndl[i]);
33553365

33563366
err = nvme_cli_ns_mgmt_create(dev, data, &nsid, nvme_cfg.timeout, cfg.csi);
3357-
if (!err)
3358-
printf("%s: Success, created nsid:%d\n", cmd->name, nsid);
3359-
else
3360-
ns_mgmt_show_error(dev, err, "create");
3367+
ns_mgmt_show_status(dev, err, "create", cmd->name, nsid);
33613368

33623369
return err;
33633370
}

0 commit comments

Comments
 (0)