]> www.infradead.org Git - users/sagi/nvme-cli.git/commitdiff
nvme-cli: attach ns to local controller if none specified
authorKeith Busch <kbusch@kernel.org>
Thu, 3 Apr 2025 14:29:26 +0000 (07:29 -0700)
committerDaniel Wagner <wagi@kernel.org>
Thu, 3 Apr 2025 17:22:22 +0000 (19:22 +0200)
Assume the user meant to attach the namespace to the controller the
command was sent to if the user didn't provide a controller id list.

Suggested-by: Nilay Shroff <nilay@linux.ibm.com>
Signed-off-by: Keith Busch <kbusch@kernel.org>
Signed-off-by: Daniel Wagner <wagi@kernel.org>
Documentation/nvme-attach-ns.txt
nvme.c

index 1d507593129efc6344132db0d214304f7468cf23..7b544a463654365a9ff0d0dbc983fc4ef375a1e6 100644 (file)
@@ -29,8 +29,9 @@ OPTIONS
 
 -c <ctrl-list,>::
 -controllers=<ctrl-list,>::
-       The comma separated list of controller identifiers to attach
-       the namespace too.
+       The comma separated list of controller identifiers to attach the
+       namespace too. If no list is provided, the namespace will be attached
+       to the <device> controller used for this command.
 
 -o <fmt>::
 --output-format=<fmt>::
@@ -43,9 +44,13 @@ OPTIONS
 
 EXAMPLES
 --------
-       Attach namespace to the controller:
+       Attach namespace 2 to the controllers with id 0x21 and 0x22:
 
-               # nvme attach-ns /dev/nvme1 -n 0x2 -c 0x21
+               # nvme attach-ns /dev/nvme1 -n 0x2 -c 0x21,0x22
+
+       Attach namespace 3 to nvme0:
+
+               # nvme attach-ns /dev/nvme0 -n 3
 
 NVME
 ----
diff --git a/nvme.c b/nvme.c
index e78a2e6fffd2b094af808c0ea467d21f277b209b..0d07e2a52720300394505683017f8408817a338a 100644 (file)
--- a/nvme.c
+++ b/nvme.c
@@ -2953,9 +2953,6 @@ static int nvme_attach_ns(int argc, char **argv, int attach, const char *desc, s
 
        num = argconfig_parse_comma_sep_array_u16(cfg.cntlist,
                                                  list, ARRAY_SIZE(list));
-       if (!num)
-               fprintf(stderr, "warning: empty controller-id list will result in no actual change in namespace attachment\n");
-
        if (num == -1) {
                nvme_show_error("%s: controller id list is malformed", cmd->name);
                return -EINVAL;
@@ -2965,7 +2962,18 @@ static int nvme_attach_ns(int argc, char **argv, int attach, const char *desc, s
        if (!cntlist)
                return -ENOMEM;
 
-       nvme_init_ctrl_list(cntlist, num, list);
+       if (argconfig_parse_seen(opts, "controllers")) {
+               nvme_init_ctrl_list(cntlist, num, list);
+       } else {
+               struct nvme_id_ctrl ctrl = { 0 };
+
+               if (nvme_cli_identify_ctrl(dev, &ctrl)) {
+                       perror("identify-ctrl");
+                       return -errno;
+               }
+               cntlist->num = cpu_to_le16(1);
+               cntlist->identifier[0] = ctrl.cntlid;
+       }
 
        if (attach)
                err = nvme_cli_ns_attach_ctrls(dev, cfg.namespace_id,