[PATCH] nvme-cli: attach ns to local controller if none specified

Nilay Shroff nilay at linux.ibm.com
Tue Apr 1 00:20:38 PDT 2025



On 4/1/25 12:38 AM, Keith Busch wrote:
> From: Keith Busch <kbusch at kernel.org>
> 
> 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 at linux.ibm.com>
> Signed-off-by: Keith Busch <kbusch at kernel.org>
> ---
>  nvme.c | 16 ++++++++++++----
>  1 file changed, 12 insertions(+), 4 deletions(-)
> 
> diff --git a/nvme.c b/nvme.c
> index bb412843..7a2c2e9f 100644
> --- 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->identifier[0] = ctrl.cntlid;

Thanks Keith for working on this!

I think we also need to set the num of cntrls in the list here. In this particular case, 
we need to add the following line after we initialize the cntrl identifier:

cntlist->num = cpu_to_le16(1);

Otherwise changes looks good to me. 

Thanks,
--Nilay




More information about the Linux-nvme mailing list