From fa2b91da7439b776a7eb01af9e2b23ac1373c027 Mon Sep 17 00:00:00 2001 From: Klaus Jensen Date: Tue, 1 Sep 2020 10:36:41 +0200 Subject: [PATCH] {get,set}_feature: get nsid from device node If the given device is the controller device, set nsid to 0xFFFFFFFF. This allows something like nvme set-feature /dev/nvme0 -f 0x5 -v $((1 << 16)) to work as expected. Since we cannot use S_ISCHR/S_ISBLK to differentiate between controller and namespace devices since commit e77046661509 ("remove block device checking for namespace"), just try the NVME_IOCTL_ID ioctl and if it fails with ENOTTY, assume that it is the controller device. Signed-off-by: Klaus Jensen --- nvme.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/nvme.c b/nvme.c index f1ea6b7d..ac63f036 100644 --- a/nvme.c +++ b/nvme.c @@ -2097,7 +2097,7 @@ static int get_feature(int argc, char **argv, struct command *cmd, struct plugin }; struct config cfg = { - .namespace_id = 1, + .namespace_id = 0, .feature_id = 0, .sel = 0, .cdw11 = 0, @@ -2119,6 +2119,18 @@ static int get_feature(int argc, char **argv, struct command *cmd, struct plugin if (fd < 0) goto ret; + if (!cfg.namespace_id) { + err = cfg.namespace_id = nvme_get_nsid(fd); + if (err < 0) { + if (errno != ENOTTY) { + perror("get-namespace-id"); + goto close_fd; + } + + cfg.namespace_id = NVME_NSID_ALL; + } + } + if (cfg.sel > 7) { fprintf(stderr, "invalid 'select' param:%d\n", cfg.sel); err = -EINVAL; @@ -3010,6 +3022,18 @@ static int set_feature(int argc, char **argv, struct command *cmd, struct plugin if (fd < 0) goto ret; + if (!cfg.namespace_id) { + err = cfg.namespace_id = nvme_get_nsid(fd); + if (err < 0) { + if (errno != ENOTTY) { + perror("get-namespace-id"); + goto close_fd; + } + + cfg.namespace_id = NVME_NSID_ALL; + } + } + if (!cfg.feature_id) { fprintf(stderr, "feature-id required param\n"); err = -EINVAL; -- 2.50.1