Skip to content

Commit 0264cec

Browse files
ikegami-tigaw
authored andcommitted
nvme-print-stdout: add format command relatives prints
The output changed as TBD by the commit 18de3a6 before. Signed-off-by: Tokunori Ikegami <ikegami.t@gmail.com>
1 parent ebe366f commit 0264cec

File tree

4 files changed

+93
-4
lines changed

4 files changed

+93
-4
lines changed

nvme-print-stdout.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5973,6 +5973,65 @@ static void stdout_pull_model_ddc_req_log(struct nvme_pull_model_ddc_req_log *lo
59735973
d((unsigned char *)log->osp, osp_len, 16, 1);
59745974
}
59755975

5976+
static void stdout_relatives(nvme_root_t r, const char *name)
5977+
{
5978+
struct nvme_resources res;
5979+
struct htable_ns_iter it;
5980+
bool block = true;
5981+
bool first = true;
5982+
nvme_ctrl_t c;
5983+
nvme_path_t p;
5984+
nvme_ns_t n;
5985+
int nsid;
5986+
int ret;
5987+
int id;
5988+
5989+
ret = sscanf(name, "nvme%dn%d", &id, &nsid);
5990+
5991+
switch (ret) {
5992+
case 1:
5993+
block = false;
5994+
break;
5995+
case 2:
5996+
break;
5997+
default:
5998+
return;
5999+
}
6000+
6001+
nvme_resources_init(r, &res);
6002+
6003+
if (block) {
6004+
fprintf(stderr, "Namespace %s has parent controller(s):", name);
6005+
for (n = htable_ns_getfirst(&res.ht_n, name, &it); n;
6006+
n = htable_ns_getnext(&res.ht_n, name, &it)) {
6007+
if (nvme_ns_get_ctrl(n)) {
6008+
fprintf(stderr, "%s", nvme_ctrl_get_name(nvme_ns_get_ctrl(n)));
6009+
break;
6010+
}
6011+
nvme_namespace_for_each_path(n, p) {
6012+
c = nvme_path_get_ctrl(p);
6013+
fprintf(stderr, "%s%s", first ? "" : ", ", nvme_ctrl_get_name(c));
6014+
if (first)
6015+
first = false;
6016+
}
6017+
}
6018+
fprintf(stderr, "\n\n");
6019+
} else {
6020+
c = htable_ctrl_get(&res.ht_c, name);
6021+
if (c) {
6022+
fprintf(stderr, "Controller %s has child namespace(s):", name);
6023+
nvme_ctrl_for_each_ns(c, n) {
6024+
fprintf(stderr, "%s%s", first ? "" : ", ", nvme_ns_get_name(n));
6025+
if (first)
6026+
first = false;
6027+
}
6028+
fprintf(stderr, "\n\n");
6029+
}
6030+
}
6031+
6032+
nvme_resources_free(&res);
6033+
}
6034+
59766035
static struct print_ops stdout_print_ops = {
59776036
/* libnvme types.h print functions */
59786037
.ana_log = stdout_ana_log,
@@ -6016,6 +6075,7 @@ static struct print_ops stdout_print_ops = {
60166075
.predictable_latency_event_agg_log = stdout_predictable_latency_event_agg_log,
60176076
.predictable_latency_per_nvmset = stdout_predictable_latency_per_nvmset,
60186077
.primary_ctrl_cap = stdout_primary_ctrl_cap,
6078+
.relatives = stdout_relatives,
60196079
.resv_notification_log = stdout_resv_notif_log,
60206080
.resv_report = stdout_resv_report,
60216081
.sanitize_log_page = stdout_sanitize_log,

nvme-print.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "util/suffix.h"
1616
#include "util/types.h"
1717
#include "common.h"
18+
#include "logging.h"
1819

1920
#define nvme_print(name, flags, ...) \
2021
do { \
@@ -490,9 +491,9 @@ void nvme_show_single_property(int offset, uint64_t value64, nvme_print_flags_t
490491
nvme_print(single_property, flags, offset, value64);
491492
}
492493

493-
void nvme_show_relatives(const char *name)
494+
void nvme_show_relatives(nvme_root_t r, const char *name, nvme_print_flags_t flags)
494495
{
495-
/* XXX: TBD */
496+
nvme_print(relatives, flags, r, name);
496497
}
497498

498499
void d(unsigned char *buf, int len, int width, int group)

nvme-print.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ struct print_ops {
6464
void (*predictable_latency_event_agg_log)(struct nvme_aggregate_predictable_lat_event *pea_log, __u64 log_entries, __u32 size, const char *devname);
6565
void (*predictable_latency_per_nvmset)(struct nvme_nvmset_predictable_lat_log *plpns_log, __u16 nvmset_id, const char *devname);
6666
void (*primary_ctrl_cap)(const struct nvme_primary_ctrl_cap *caps);
67+
void (*relatives)(nvme_root_t r, const char *name);
6768
void (*resv_notification_log)(struct nvme_resv_notification_log *resv, const char *devname);
6869
void (*resv_report)(struct nvme_resv_status *status, int bytes, bool eds);
6970
void (*sanitize_log_page)(struct nvme_sanitize_log_page *sanitize_log, const char *devname);
@@ -153,7 +154,7 @@ struct print_ops *nvme_get_binary_print_ops(nvme_print_flags_t flags);
153154

154155
void nvme_show_status(int status);
155156
void nvme_show_lba_status_info(__u32 result);
156-
void nvme_show_relatives(const char *name);
157+
void nvme_show_relatives(nvme_root_t r, const char *name, nvme_print_flags_t flags);
157158

158159
void nvme_show_id_iocs(struct nvme_id_iocs *iocs, nvme_print_flags_t flags);
159160
void nvme_show_id_ctrl(struct nvme_id_ctrl *ctrl, nvme_print_flags_t flags,

nvme.c

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6335,6 +6335,26 @@ static int set_property(int argc, char **argv, struct command *cmd, struct plugi
63356335
return nvme_set_single_property(dev_fd(dev), cfg.offset, cfg.value);
63366336
}
63376337

6338+
static void show_relatives(const char *name, nvme_print_flags_t flags)
6339+
{
6340+
int err = 0;
6341+
6342+
_cleanup_nvme_root_ nvme_root_t r = nvme_create_root(stderr, log_level);
6343+
6344+
if (!r) {
6345+
nvme_show_error("Failed to create topology root: %s", nvme_strerror(errno));
6346+
return;
6347+
}
6348+
6349+
err = nvme_scan_topology(r, NULL, NULL);
6350+
if (err < 0) {
6351+
nvme_show_error("Failed to scan topology: %s", nvme_strerror(errno));
6352+
return;
6353+
}
6354+
6355+
nvme_show_relatives(r, name, flags);
6356+
}
6357+
63386358
static int format_cmd(int argc, char **argv, struct command *cmd, struct plugin *plugin)
63396359
{
63406360
const char *desc = "Re-format a specified namespace on the\n"
@@ -6356,6 +6376,7 @@ static int format_cmd(int argc, char **argv, struct command *cmd, struct plugin
63566376
__u8 prev_lbaf = 0;
63576377
int block_size;
63586378
int err, i;
6379+
nvme_print_flags_t flags = NORMAL;
63596380

63606381
struct config {
63616382
__u32 namespace_id;
@@ -6411,6 +6432,12 @@ static int format_cmd(int argc, char **argv, struct command *cmd, struct plugin
64116432
return err;
64126433
}
64136434

6435+
err = validate_output_format(nvme_cfg.output_format, &flags);
6436+
if (err < 0) {
6437+
nvme_show_error("Invalid output format");
6438+
return err;
6439+
}
6440+
64146441
if (cfg.lbaf != 0xff && cfg.bs != 0) {
64156442
nvme_show_error(
64166443
"Invalid specification of both LBAF and Block Size, please specify only one");
@@ -6523,7 +6550,7 @@ static int format_cmd(int argc, char **argv, struct command *cmd, struct plugin
65236550
fprintf(stderr, "You are about to format %s, namespace %#x%s.\n",
65246551
dev->name, cfg.namespace_id,
65256552
cfg.namespace_id == NVME_NSID_ALL ? "(ALL namespaces)" : "");
6526-
nvme_show_relatives(dev->name);
6553+
show_relatives(dev->name, flags);
65276554
fprintf(stderr,
65286555
"WARNING: Format may irrevocably delete this device's data.\n"
65296556
"You have 10 seconds to press Ctrl-C to cancel this operation.\n\n"

0 commit comments

Comments
 (0)