]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
{get,set}_feature: get nsid from device node
authorKlaus Jensen <k.jensen@samsung.com>
Tue, 1 Sep 2020 08:36:41 +0000 (10:36 +0200)
committerKeith Busch <kbusch@kernel.org>
Tue, 1 Sep 2020 15:38:07 +0000 (09:38 -0600)
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 <k.jensen@samsung.com>
nvme.c

diff --git a/nvme.c b/nvme.c
index f1ea6b7d0ba3ad4e47f3ddd1205b1ab21ab22fde..ac63f036a95a91fe066034c33a5ec21ceea54c87 100644 (file)
--- 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;