Skip to content

Commit c634947

Browse files
committed
nvme: add fallback support for block vs char device to flush command
Also the flush commands depends operating on a block device. The kernel lacks the fallback support in the NVME_IOCTL_IO64_CMD to lookup the namespace. Refactor the fallback from the dsm command and reuse it for flush as well. Signed-off-by: Daniel Wagner <[email protected]>
1 parent 9c218af commit c634947

File tree

1 file changed

+43
-21
lines changed

1 file changed

+43
-21
lines changed

nvme.c

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,42 @@ int open_exclusive(struct nvme_global_ctx **ctx,
445445
return 0;
446446
}
447447

448+
static int open_fallback_chardev(struct nvme_global_ctx *ctx,
449+
__u32 nsid,
450+
struct nvme_transport_handle **phdl)
451+
{
452+
struct nvme_transport_handle *hdl = *phdl;
453+
int err;
454+
455+
if (nvme_transport_handle_is_chardev(hdl)) {
456+
_cleanup_free_ char *cdev = NULL;
457+
458+
if (!nsid) {
459+
nvme_show_error("char device not supported without --namespace-id");
460+
return -EINVAL;
461+
}
462+
463+
if (asprintf(&cdev, "/dev/%sn%d",
464+
nvme_transport_handle_get_name(hdl), nsid) < 0)
465+
return -ENOMEM;
466+
467+
nvme_close(hdl);
468+
469+
err = nvme_open(ctx, cdev, &hdl);
470+
if (err) {
471+
*phdl = NULL;
472+
473+
nvme_show_error("could not open %s", cdev);
474+
return err;
475+
}
476+
477+
*phdl = hdl;
478+
}
479+
480+
return 0;
481+
}
482+
483+
448484
int validate_output_format(const char *format, nvme_print_flags_t *flags)
449485
{
450486
nvme_print_flags_t f;
@@ -7364,27 +7400,9 @@ static int dsm(int argc, char **argv, struct command *acmd, struct plugin *plugi
73647400
if (err)
73657401
return err;
73667402

7367-
if (nvme_transport_handle_is_chardev(hdl)) {
7368-
_cleanup_free_ char *cdev = NULL;
7369-
7370-
if (!cfg.namespace_id) {
7371-
nvme_show_error("char device not supported without --namespace-id");
7372-
return -EINVAL;
7373-
}
7374-
7375-
if (asprintf(&cdev, "/dev/%sn%d",
7376-
nvme_transport_handle_get_name(hdl),
7377-
cfg.namespace_id) < 0)
7378-
return -ENOMEM;
7379-
7380-
nvme_close(hdl);
7381-
7382-
err = nvme_open(ctx, cdev, &hdl);
7383-
if (err) {
7384-
nvme_show_error("could not open %s", cdev);
7385-
return err;
7386-
}
7387-
}
7403+
err = open_fallback_chardev(ctx, cfg.namespace_id, &hdl);
7404+
if (err)
7405+
return err;
73887406

73897407
err = validate_output_format(nvme_cfg.output_format, &flags);
73907408
if (err < 0) {
@@ -7681,6 +7699,10 @@ static int flush_cmd(int argc, char **argv, struct command *acmd, struct plugin
76817699
if (err)
76827700
return err;
76837701

7702+
err = open_fallback_chardev(ctx, cfg.namespace_id, &hdl);
7703+
if (err)
7704+
return err;
7705+
76847706
if (!cfg.namespace_id) {
76857707
err = nvme_get_nsid(hdl, &cfg.namespace_id);
76867708
if (err < 0) {

0 commit comments

Comments
 (0)