linux-nvme.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Max Gurtovoy <mgurtovoy@nvidia.com>
To: Chaitanya Kulkarni <Chaitanya.Kulkarni@wdc.com>,
	"linux-nvme@lists.infradead.org" <linux-nvme@lists.infradead.org>,
	"sagi@grimberg.me" <sagi@grimberg.me>,
	"kbusch@kernel.org" <kbusch@kernel.org>,
	"hch@lst.de" <hch@lst.de>
Cc: "oren@nvidia.com" <oren@nvidia.com>,
	"ngottlieb@nvidia.com" <ngottlieb@nvidia.com>
Subject: Re: [PATCH 1/4] nvmet: change sn size and check validity
Date: Thu, 22 Apr 2021 11:50:42 +0300	[thread overview]
Message-ID: <bd1e5f26-8eda-d784-ff00-686190ed68b4@nvidia.com> (raw)
In-Reply-To: <BYAPR04MB4965297F005A2E8EC059606D86489@BYAPR04MB4965.namprd04.prod.outlook.com>


On 4/20/2021 8:35 PM, Chaitanya Kulkarni wrote:
> On 4/20/21 02:09, Max Gurtovoy wrote:
>> From: Noam Gottlieb <ngottlieb@nvidia.com>
>>
>> According to the NVM specification, the serial_number should be 20 bytes
>> (bytes 23:04 of the Identify Controller data structure), and should
>> contain only ASCII characters.
>>
>> In accordance, the serial_number size is changed to 20 bytes and before
>> any attempt to store a new value in serial_number we check that the
>> input is valid - i.e. contains only ASCII characters, is not empty and
>> does not exceed 20 bytes.
>>
>> Reviewed-by: Max Gurtovoy <mgurtovoy@nvidia.com>
>> Signed-off-by: Noam Gottlieb <ngottlieb@nvidia.com>
>> ---
>>   drivers/nvme/target/admin-cmd.c |  4 +---
>>   drivers/nvme/target/configfs.c  | 36 ++++++++++++++++++++++++---------
>>   drivers/nvme/target/core.c      |  4 +++-
>>   drivers/nvme/target/discovery.c |  4 +---
>>   drivers/nvme/target/nvmet.h     |  3 ++-
>>   5 files changed, 33 insertions(+), 18 deletions(-)
>>
>> diff --git a/drivers/nvme/target/admin-cmd.c b/drivers/nvme/target/admin-cmd.c
>> index d2a26ff3f7b3..91eb7562a88a 100644
>> --- a/drivers/nvme/target/admin-cmd.c
>> +++ b/drivers/nvme/target/admin-cmd.c
>> @@ -357,9 +357,7 @@ static void nvmet_execute_identify_ctrl(struct nvmet_req *req)
>>   	id->vid = 0;
>>   	id->ssvid = 0;
>>   
>> -	memset(id->sn, ' ', sizeof(id->sn));
>> -	bin2hex(id->sn, &ctrl->subsys->serial,
>> -		min(sizeof(ctrl->subsys->serial), sizeof(id->sn) / 2));
>> +	memcpy(id->sn, ctrl->subsys->serial, NVMET_SN_MAX_SIZE);
>>   	memcpy_and_pad(id->mn, sizeof(id->mn), subsys->model_number,
>>   		       strlen(subsys->model_number), ' ');
>>   	memcpy_and_pad(id->fr, sizeof(id->fr),
>> diff --git a/drivers/nvme/target/configfs.c b/drivers/nvme/target/configfs.c
>> index 65a0cf99f557..576540fdba73 100644
>> --- a/drivers/nvme/target/configfs.c
>> +++ b/drivers/nvme/target/configfs.c
>> @@ -1030,24 +1030,46 @@ static ssize_t nvmet_subsys_attr_version_store(struct config_item *item,
>>   }
>>   CONFIGFS_ATTR(nvmet_subsys_, attr_version);
>>   
>> +/* See Section 1.5 of NVMe 1.4 */
>> +static bool nvmet_is_ascii(const char c)
>> +{
>> +	return c >= 0x20 && c <= 0x7e;
>> +}
>> +
>>   static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item,
>>   					     char *page)
>>   {
>>   	struct nvmet_subsys *subsys = to_subsys(item);
>>   
>> -	return snprintf(page, PAGE_SIZE, "%llx\n", subsys->serial);
>> +	return snprintf(page, PAGE_SIZE, "%s\n", subsys->serial);
>>   }
>>   
>>   static ssize_t nvmet_subsys_attr_serial_store(struct config_item *item,
>>   					      const char *page, size_t count)
>>   {
>> -	u64 serial;
>> +	struct nvmet_subsys *subsys;
>> +	int pos, len;
>> +
>> +	subsys = to_subsys(item);
>> +	len = strcspn(page, "\n");
>>   
> nit:- we can avoid two extra initialization lines with :-
>   
>          struct nvmet_subsys *subsys = to_subsys(item);
>          size_t pos, len = strcspn(page, "\n");
>
>> -	if (sscanf(page, "%llx\n", &serial) != 1)
>> +	if (len == 0 || len > NVMET_SN_MAX_SIZE) {
> nit:- 's/len == 0/!len/' is pretty common in the code.

Ok.


>> +		pr_err("Serial Number can not be empty or exceed %d Bytes\n",
>> +		       NVMET_SN_MAX_SIZE);
>>   		return -EINVAL;
>> +	}
>> +
>> +	for (pos = 0; pos < len; pos++) {
>> +		if (!nvmet_is_ascii(page[pos])) {
>> +			pr_err("Serial Number must contain only ASCII strings\n");
>> +			return -EINVAL;
>> +		}
>> +	}
>>   
>>   	down_write(&nvmet_config_sem);
>> -	to_subsys(item)->serial = serial;
>> +	mutex_lock(&subsys->lock);
>> +	memcpy_and_pad(subsys->serial, NVMET_SN_MAX_SIZE, page, len, ' ');
>> +	mutex_unlock(&subsys->lock);
>>   	up_write(&nvmet_config_sem);
>>   
>>   	return count;
>> @@ -1128,12 +1150,6 @@ static ssize_t nvmet_subsys_attr_model_show(struct config_item *item,
>>   	return ret;
>>   }
>>   
>> -/* See Section 1.5 of NVMe 1.4 */
>> -static bool nvmet_is_ascii(const char c)
>> -{
>> -	return c >= 0x20 && c <= 0x7e;
>> -}
>> -
>>   static ssize_t nvmet_subsys_attr_model_store_locked(struct nvmet_subsys *subsys,
>>   		const char *page, size_t count)
>>   {
>> diff --git a/drivers/nvme/target/core.c b/drivers/nvme/target/core.c
>> index adbede9ab7f3..3efd48b0a34e 100644
>> --- a/drivers/nvme/target/core.c
>> +++ b/drivers/nvme/target/core.c
>> @@ -1482,6 +1482,7 @@ struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
>>   		enum nvme_subsys_type type)
>>   {
>>   	struct nvmet_subsys *subsys;
>> +	char serial[NVMET_SN_MAX_SIZE / 2];
> This needs a comment why NVMET_SN_MAX_SIZE / 2.

explain bin2hex ?


>>   
>>   	subsys = kzalloc(sizeof(*subsys), GFP_KERNEL);
>>   	if (!subsys)
>> @@ -1489,7 +1490,8 @@ struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,
>>   
>>   	subsys->ver = NVMET_DEFAULT_VS;
>>   	/* generate a random serial number as our controllers are ephemeral: */
>> -	get_random_bytes(&subsys->serial, sizeof(subsys->serial));
>> +	get_random_bytes(&serial, sizeof(serial));
>> +	bin2hex(subsys->serial, &serial, sizeof(serial));
>>   
>>   	switch (type) {
>>   	case NVME_NQN_NVME:
>> diff --git a/drivers/nvme/target/discovery.c b/drivers/nvme/target/discovery.c
>> index 4845d12e374a..f39946615fd6 100644
>> --- a/drivers/nvme/target/discovery.c
>> +++ b/drivers/nvme/target/discovery.c
>> @@ -262,9 +262,7 @@ static void nvmet_execute_disc_identify(struct nvmet_req *req)
>>   		goto out;
>>   	}
>>   
>> -	memset(id->sn, ' ', sizeof(id->sn));
>> -	bin2hex(id->sn, &ctrl->subsys->serial,
>> -		min(sizeof(ctrl->subsys->serial), sizeof(id->sn) / 2));
>> +	memcpy(id->sn, ctrl->subsys->serial, NVMET_SN_MAX_SIZE);
>>   	memset(id->fr, ' ', sizeof(id->fr));
>>   	memcpy_and_pad(id->mn, sizeof(id->mn), model, sizeof(model) - 1, ' ');
>>   	memcpy_and_pad(id->fr, sizeof(id->fr),
>> diff --git a/drivers/nvme/target/nvmet.h b/drivers/nvme/target/nvmet.h
>> index 5566ed403576..53999bd259ed 100644
>> --- a/drivers/nvme/target/nvmet.h
>> +++ b/drivers/nvme/target/nvmet.h
>> @@ -28,6 +28,7 @@
>>   #define NVMET_NO_ERROR_LOC		((u16)-1)
>>   #define NVMET_DEFAULT_CTRL_MODEL	"Linux"
>>   #define NVMET_MN_MAX_SIZE		40
>> +#define NVMET_SN_MAX_SIZE		20
>>   
>>   /*
>>    * Supported optional AENs:
>> @@ -229,7 +230,7 @@ struct nvmet_subsys {
>>   	u16			max_qid;
>>   
>>   	u64			ver;
>> -	u64			serial;
>> +	char			serial[NVMET_SN_MAX_SIZE];
>>   	char			*subsysnqn;
>>   	bool			pi_support;
>>   

_______________________________________________
Linux-nvme mailing list
Linux-nvme@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-nvme

  reply	other threads:[~2021-04-22  8:51 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-20  9:09 [PATCH 1/4] nvmet: change sn size and check validity Max Gurtovoy
2021-04-20  9:09 ` [PATCH 2/4] nvmet: make sn stable once connection was established Max Gurtovoy
2021-04-20  9:09 ` [PATCH 3/4] nvmet: allow mn change if subsys not discovered Max Gurtovoy
2021-04-20  9:09 ` [PATCH 4/4] nvmet: make ver stable once connection established Max Gurtovoy
2021-04-20 17:47   ` Chaitanya Kulkarni
2021-04-22  8:43     ` Max Gurtovoy
2021-04-22 19:01       ` Chaitanya Kulkarni
2021-04-20 17:35 ` [PATCH 1/4] nvmet: change sn size and check validity Chaitanya Kulkarni
2021-04-22  8:50   ` Max Gurtovoy [this message]
2021-04-22 19:11     ` Chaitanya Kulkarni
2021-04-23 15:51 ` Keith Busch
2021-04-26 13:15   ` Max Gurtovoy
2021-04-29 10:47     ` Max Gurtovoy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bd1e5f26-8eda-d784-ff00-686190ed68b4@nvidia.com \
    --to=mgurtovoy@nvidia.com \
    --cc=Chaitanya.Kulkarni@wdc.com \
    --cc=hch@lst.de \
    --cc=kbusch@kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    --cc=ngottlieb@nvidia.com \
    --cc=oren@nvidia.com \
    --cc=sagi@grimberg.me \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).