Skip to content

Commit eb06760

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 705f711 commit eb06760

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
@@ -2827,26 +2827,42 @@ static int id_endurance_grp_list(int argc, char **argv, struct command *cmd,
28272827
return err;
28282828
}
28292829

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

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

28392856
nvme_show_init();
28402857

2841-
nvme_show_status(err);
2842-
2843-
ctrl = nvme_alloc(sizeof(*ctrl));
2844-
if (!ctrl)
2845-
return;
2846-
2847-
err = nvme_cli_identify_ctrl(dev, ctrl);
28482858
if (!err) {
2849-
if (!(le16_to_cpu(ctrl->oacs) & NVME_CTRL_OACS_NS_MGMT))
2859+
nvme_show_key_value(true, false, name, "Success, %s",
2860+
!strcmp(cmd, "create") ? "created":
2861+
!strcmp(cmd, "delete") ? "deleted" : "");
2862+
nvme_show_key_value(false, true, "nsid", "%d", nsid);
2863+
} else {
2864+
nvme_show_status(err);
2865+
if (!is_ns_mgmt_support(dev))
28502866
nvme_show_result("NS management and attachment not supported");
28512867
}
28522868

@@ -2892,10 +2908,7 @@ static int delete_ns(int argc, char **argv, struct command *cmd, struct plugin *
28922908
}
28932909

28942910
err = nvme_cli_ns_mgmt_delete(dev, cfg.namespace_id, nvme_cfg.timeout);
2895-
if (!err)
2896-
printf("%s: Success, deleted nsid:%d\n", cmd->name, cfg.namespace_id);
2897-
else
2898-
ns_mgmt_show_error(dev, err, "delete");
2911+
ns_mgmt_show_status(dev, err, "delete", cmd->name, cfg.namespace_id);
28992912

29002913
return err;
29012914
}
@@ -2956,10 +2969,7 @@ static int nvme_attach_ns(int argc, char **argv, int attach, const char *desc, s
29562969
err = nvme_cli_ns_detach_ctrls(dev, cfg.namespace_id,
29572970
cntlist);
29582971

2959-
if (!err)
2960-
printf("%s: Success, nsid:%d\n", cmd->name, cfg.namespace_id);
2961-
else
2962-
ns_mgmt_show_error(dev, err, attach ? "attach" : "detach");
2972+
ns_mgmt_show_status(dev, err, attach ? "attach" : "detach", cmd->name, cfg.namespace_id);
29632973

29642974
return err;
29652975
}
@@ -3324,10 +3334,7 @@ static int create_ns(int argc, char **argv, struct command *cmd, struct plugin *
33243334
data->phndl[i] = cpu_to_le16(phndl[i]);
33253335

33263336
err = nvme_cli_ns_mgmt_create(dev, data, &nsid, nvme_cfg.timeout, cfg.csi);
3327-
if (!err)
3328-
printf("%s: Success, created nsid:%d\n", cmd->name, nsid);
3329-
else
3330-
ns_mgmt_show_error(dev, err, "create");
3337+
ns_mgmt_show_status(dev, err, "create", cmd->name, nsid);
33313338

33323339
return err;
33333340
}

0 commit comments

Comments
 (0)