nvmet: change sn size and check validity
Subsystems (2)
PATCH 1/4 nvmet: change sn size and check validity
From: Noam Gottlieb <[email protected]>
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.
--- 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(-)
drivers/nvme/target/admin-cmd.c +1 -3
@@ -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),
drivers/nvme/target/configfs.c +26 -10
@@ -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" ); -if (sscanf (page,"%llx\n" , &serial) !=1 ) +if (len ==0 || len >NVMET_SN_MAX_SIZE ) { +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) {
drivers/nvme/target/core.c +3 -1
@@ -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 ]; 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 :
drivers/nvme/target/discovery.c +1 -3
@@ -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),
drivers/nvme/target/nvmet.h +2 -1
@@ -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;
Signature · 7 lines
2.25.4 _______________________________________________ Linux-nvme mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-nvme
PATCH 2/4 nvmet: make sn stable once connection was established
From: Noam Gottlieb <[email protected]>
Once some host has connected to the target, make sure that the serial number is stable and cannot be changed.
--- drivers/nvme/target/admin-cmd.c | 6 ++++++ drivers/nvme/target/configfs.c | 28 ++++++++++++++++++++++------ drivers/nvme/target/nvmet.h | 1 + 3 files changed, 29 insertions(+), 6 deletions(-)
drivers/nvme/target/admin-cmd.c +6 -0
@@ -337,6 +337,12 @@ static void nvmet_execute_identify_ctrl(struct nvmet_req *req)u32 cmd_capsule_size;u16 status =0 ; +if (!subsys->subsys_discovered) { +mutex_lock (&subsys->lock); + subsys->subsys_discovered = true; +mutex_unlock (&subsys->lock); + } +/* * If there is no model number yet, set it now. It will then remain * stable for the life time of the subsystem.
drivers/nvme/target/configfs.c +22 -6
@@ -1044,13 +1044,18 @@ static ssize_t nvmet_subsys_attr_serial_show(struct config_item *item,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) +static ssize_t +nvmet_subsys_attr_serial_store_locked (struct nvmet_subsys *subsys, +const char *page,size_t count) { -struct nvmet_subsys *subsys;int pos, len; - subsys =to_subsys (item); +if (subsys->subsys_discovered) { +pr_err ("Can't set serial number. %s is already assigned\n" , + subsys->serial); +return -EINVAL ; + } + len =strcspn (page,"\n" );if (len ==0 || len >NVMET_SN_MAX_SIZE ) {
@@ -1066,13 +1071,24 @@ static ssize_t nvmet_subsys_attr_serial_store(struct config_item *item, } } +memcpy_and_pad (subsys->serial,NVMET_SN_MAX_SIZE , page, len,' ' ); + +return count; +} + +static ssize_t nvmet_subsys_attr_serial_store (struct config_item *item, +const char *page,size_t count) +{ +struct nvmet_subsys *subsys =to_subsys (item); +ssize_t ret; +down_write (&nvmet_config_sem);mutex_lock (&subsys->lock); -memcpy_and_pad (subsys->serial,NVMET_SN_MAX_SIZE , page, len,' ' ); + ret =nvmet_subsys_attr_serial_store_locked (subsys, page, count);mutex_unlock (&subsys->lock);up_write (&nvmet_config_sem); -return count; +return ret; }CONFIGFS_ATTR (nvmet_subsys_, attr_serial);
drivers/nvme/target/nvmet.h +1 -0
@@ -231,6 +231,7 @@ struct nvmet_subsys {u64 ver;char serial[NVMET_SN_MAX_SIZE ]; +bool subsys_discovered;char *subsysnqn;bool pi_support;
Signature · 7 lines
2.25.4 _______________________________________________ Linux-nvme mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-nvme
PATCH 3/4 nvmet: allow mn change if subsys not discovered
In reply to Max GurtovoyFrom: Noam Gottlieb <[email protected]>
Currently, once the subsystem's model_number is set for the first time there is no way to change it. However, as long as no connection was established to nvmf target, there is no reason for such restriction and we should allow to change the subsystem's model_number as many times as needed.
In addition, in order to simplfy the changes and make the model number flow more similar to the rest of the attributes in the Identify Controller data structure, we set a default value for the model number at the initiation of the subsystem.
--- drivers/nvme/target/admin-cmd.c | 26 -------------------------- drivers/nvme/target/configfs.c | 10 ++-------- drivers/nvme/target/core.c | 21 +++++++++++++++++---- drivers/nvme/target/discovery.c | 4 ++-- 4 files changed, 21 insertions(+), 40 deletions(-)
drivers/nvme/target/admin-cmd.c +0 -26
@@ -313,22 +313,6 @@ static void nvmet_execute_get_log_page(struct nvmet_req *req)nvmet_req_complete (req,NVME_SC_INVALID_FIELD |NVME_SC_DNR ); } -static u16 nvmet_set_model_number (struct nvmet_subsys *subsys) -{ -u16 status =0 ; - -mutex_lock (&subsys->lock); -if (!subsys->model_number) { - subsys->model_number = -kstrdup (NVMET_DEFAULT_CTRL_MODEL ,GFP_KERNEL ); -if (!subsys->model_number) - status =NVME_SC_INTERNAL ; - } -mutex_unlock (&subsys->lock); - -return status; -} -static void nvmet_execute_identify_ctrl (struct nvmet_req *req) {struct nvmet_ctrl *ctrl = req->sq->ctrl;
@@ -343,16 +327,6 @@ static void nvmet_execute_identify_ctrl(struct nvmet_req *req)mutex_unlock (&subsys->lock); } -/* -* If there is no model number yet, set it now. It will then remain -* stable for the life time of the subsystem. -*/ -if (!subsys->model_number) { - status =nvmet_set_model_number (subsys); -if (status) - goto out; - } - id =kzalloc (sizeof (*id),GFP_KERNEL );if (!id) { status =NVME_SC_INTERNAL ;
drivers/nvme/target/configfs.c +2 -8
@@ -1156,14 +1156,8 @@ static ssize_t nvmet_subsys_attr_model_show(struct config_item *item, char *page) {struct nvmet_subsys *subsys =to_subsys (item); -int ret; - -mutex_lock (&subsys->lock); - ret =snprintf (page,PAGE_SIZE ,"%s\n" , subsys->model_number ? - subsys->model_number :NVMET_DEFAULT_CTRL_MODEL ); -mutex_unlock (&subsys->lock); -return ret; +return snprintf (page,PAGE_SIZE ,"%s\n" , subsys->model_number); } static ssize_t nvmet_subsys_attr_model_store_locked(struct nvmet_subsys *subsys,
@@ -1171,7 +1165,7 @@ static ssize_t nvmet_subsys_attr_model_store_locked(struct nvmet_subsys *subsys, {int pos =0 , len; -if (subsys->model_number) { +if (subsys->subsys_discovered) {pr_err ("Can't set model number. %s is already assigned\n" , subsys->model_number);return -EINVAL ;
drivers/nvme/target/core.c +17 -4
@@ -1483,6 +1483,7 @@ struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn, {struct nvmet_subsys *subsys;char serial[NVMET_SN_MAX_SIZE /2 ]; +int ret; subsys =kzalloc (sizeof (*subsys),GFP_KERNEL );if (!subsys)
@@ -1493,6 +1494,12 @@ struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,get_random_bytes (&serial,sizeof (serial));bin2hex (subsys->serial, &serial,sizeof (serial)); + subsys->model_number =kstrdup (NVMET_DEFAULT_CTRL_MODEL ,GFP_KERNEL ); +if (!subsys->model_number) { + ret = -ENOMEM ; + goto free_subsys; + } +switch (type) {case NVME_NQN_NVME : subsys->max_qid =NVMET_NR_QUEUES ;
@@ -1502,15 +1509,15 @@ struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,break ;default :pr_err ("%s: Unknown Subsystem type - %d\n" , __func__, type); -kfree (subsys); -return ERR_PTR (-EINVAL ); + ret = -EINVAL ; + goto free_mn; } subsys->type = type; subsys->subsysnqn =kstrndup (subsysnqn,NVMF_NQN_SIZE ,GFP_KERNEL );if (!subsys->subsysnqn) { -kfree (subsys); -return ERR_PTR (-ENOMEM ); + ret = -ENOMEM ; + goto free_mn; } subsys->cntlid_min =NVME_CNTLID_MIN ; subsys->cntlid_max =NVME_CNTLID_MAX ;
@@ -1522,6 +1529,12 @@ struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn,INIT_LIST_HEAD (&subsys->hosts);return subsys; + +free_mn: +kfree (subsys->model_number); +free_subsys: +kfree (subsys); +return ERR_PTR (ret); }static void nvmet_subsys_free (struct kref *ref)
drivers/nvme/target/discovery.c +2 -2
@@ -244,7 +244,6 @@ static void nvmet_execute_disc_identify(struct nvmet_req *req) {struct nvmet_ctrl *ctrl = req->sq->ctrl;struct nvme_id_ctrl *id; -const char model[] ="Linux" ;u16 status =0 ;if (!nvmet_check_transfer_len (req,NVME_IDENTIFY_DATA_SIZE ))
@@ -264,7 +263,8 @@ static void nvmet_execute_disc_identify(struct nvmet_req *req)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->mn,sizeof (id->mn), ctrl->subsys->model_number, +strlen (ctrl->subsys->model_number),' ' );memcpy_and_pad (id->fr,sizeof (id->fr),UTS_RELEASE ,strlen (UTS_RELEASE ),' ' );
Signature · 7 lines
2.25.4 _______________________________________________ Linux-nvme mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-nvme
PATCH 4/4 nvmet: make ver stable once connection established
In reply to Max GurtovoyFrom: Noam Gottlieb <[email protected]>
Once some host has connected to the nvmf target, make sure that the version number is stable and cannot be changed.
--- drivers/nvme/target/configfs.c | 36 +++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-)
drivers/nvme/target/configfs.c +31 -5
@@ -1007,13 +1007,26 @@ static ssize_t nvmet_subsys_attr_version_show(struct config_item *item, NVME_MINOR(subsys->ver)); } -static ssize_t nvmet_subsys_attr_version_store (struct config_item *item, -const char *page,size_t count) +static ssize_t +nvmet_subsys_attr_version_store_locked (struct nvmet_subsys *subsys, +const char *page,size_t count) { -struct nvmet_subsys *subsys =to_subsys (item);int major, minor, tertiary =0 ;int ret; +if (subsys->subsys_discovered) { +if (NVME_TERTIARY (subsys->ver)) +pr_err ("Can't set version number. %llu.%llu.%llu is already assigned\n" , +NVME_MAJOR (subsys->ver), +NVME_MINOR (subsys->ver), +NVME_TERTIARY (subsys->ver)); +else +pr_err ("Can't set version number. %llu.%llu is already assigned\n" , +NVME_MAJOR (subsys->ver), +NVME_MINOR (subsys->ver)); +return -EINVAL ; + } +/* passthru subsystems use the underlying controller's version */ if (nvmet_passthru_ctrl (subsys))return -EINVAL ;
@@ -1022,12 +1035,25 @@ static ssize_t nvmet_subsys_attr_version_store(struct config_item *item,if (ret !=2 && ret !=3 )return -EINVAL ; -down_write (&nvmet_config_sem); subsys->ver =NVME_VS (major, minor, tertiary); -up_write (&nvmet_config_sem);return count; } + +static ssize_t nvmet_subsys_attr_version_store (struct config_item *item, +const char *page,size_t count) +{ +struct nvmet_subsys *subsys =to_subsys (item); +int ret; + +down_write (&nvmet_config_sem); +mutex_lock (&subsys->lock); + ret =nvmet_subsys_attr_version_store_locked (subsys, page, count); +mutex_unlock (&subsys->lock); +up_write (&nvmet_config_sem); + +return ret; +}CONFIGFS_ATTR (nvmet_subsys_, attr_version);/* See Section 1.5 of NVMe 1.4 */
Signature · 7 lines
2.25.4 _______________________________________________ Linux-nvme mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-nvme
Re: [PATCH 4/4] nvmet: make ver stable once connection established
Quoted 16 lines · Max Gurtovoy
From: Noam Gottlieb <[email protected]>
Once some host has connected to the nvmf target, make sure that the version number is stable and cannot be changed.
Reviewed-by Max Gurtovoy <[email protected]>Signed-off-by Noam Gottlieb <[email protected]>--- drivers/nvme/target/configfs.c | 36 +++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-)drivers/nvme/target/configfs.c +0
-0
drivers/nvme/target/configfs.c static ssize_t nvmet_subsys_attr_version_show(struct config_item *item, · line 1013
Quoted 9 lines
drivers/nvme/target/configfs.c +3
-2@@ -1007,13 +1007,26 @@ static ssize_t nvmet_subsys_attr_version_show(struct config_item *item, NVME_MINOR(subsys->ver)); } -static ssize_t nvmet_subsys_attr_version_store (struct config_item *item, -const char *page,size_t count) +static ssize_t +nvmet_subsys_attr_version_store_locked (struct nvmet_subsys *subsys, +const char *page,size_t count) {
Seems like we lost the indentation in above line which was there previously.
Quoted 7 lines
drivers/nvme/target/configfs.c +3
-1-struct nvmet_subsys *subsys =to_subsys (item);int major, minor, tertiary =0 ;int ret; +if (subsys->subsys_discovered) { +if (NVME_TERTIARY (subsys->ver)) + pr_err("Can't set version number. %llu.%llu.%llu is already assigned\n",
drivers/nvme/target/configfs.c static ssize_t nvmet_subsys_attr_version_show(struct config_item *item, · line 1029
Quoted 10 lines
drivers/nvme/target/configfs.c +10
-0+NVME_MAJOR (subsys->ver), +NVME_MINOR (subsys->ver), + NVME_TERTIARY(subsys->ver)); + else + pr_err("Can't set version number. %llu.%llu is already assigned\n", +NVME_MAJOR (subsys->ver), + NVME_MINOR(subsys->ver)); +return -EINVAL ; + } +
Can you create a helper to for about error reporting so we can avoid extra long lines whenever itis possible something like :-
if (subsys->subsys_discovered)
return nvmet_subsys_discover_err(subsys);Quoted 10 lines
drivers/nvme/target/configfs.c +0
-2/* passthru subsystems use the underlying controller's version */ if (nvmet_passthru_ctrl (subsys))return -EINVAL ;@@ -1022,12 +1035,25 @@ static ssize_t nvmet_subsys_attr_version_store(struct config_item *item,if (ret !=2 && ret !=3 )return -EINVAL ; -down_write (&nvmet_config_sem); subsys->ver =NVME_VS (major, minor, tertiary); -up_write (&nvmet_config_sem);
drivers/nvme/target/configfs.c static ssize_t nvmet_subsys_attr_version_store(struct config_item *item, · line 1048
Quoted 10 lines
drivers/nvme/target/configfs.c +7
-0return count; } + +static ssize_t nvmet_subsys_attr_version_store (struct config_item *item, +const char *page,size_t count) +{ +struct nvmet_subsys *subsys =to_subsys (item); +int ret; +
nit:- ret variable type should be ssize_tnvmet_subsys_attr_version_store_locke().
drivers/nvme/target/configfs.c static ssize_t nvmet_subsys_attr_version_store(struct config_item *item, · line 1059
Quoted 11 lines
drivers/nvme/target/configfs.c +8
-0+down_write (&nvmet_config_sem); +mutex_lock (&subsys->lock); + ret =nvmet_subsys_attr_version_store_locked (subsys, page, count); +mutex_unlock (&subsys->lock); +up_write (&nvmet_config_sem); + +return ret; +}CONFIGFS_ATTR (nvmet_subsys_, attr_version);/* See Section 1.5 of NVMe 1.4 */
_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
Re: [PATCH 4/4] nvmet: make ver stable once connection established
Quoted 20 lines · Chaitanya Kulkarni
Quoted 19 lines · Max Gurtovoy
From: Noam Gottlieb <[email protected]>
Once some host has connected to the nvmf target, make sure that the version number is stable and cannot be changed.
Reviewed-by Max Gurtovoy <[email protected]>Signed-off-by Noam Gottlieb <[email protected]>--- drivers/nvme/target/configfs.c | 36 +++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-)drivers/nvme/target/configfs.c +0
-0@@ -1007,13 +1007,26 @@ static ssize_t nvmet_subsys_attr_version_show(struct config_item *item, NVME_MINOR(subsys->ver)); }
Quoted 7 lines
drivers/nvme/target/configfs.c static ssize_t nvmet_subsys_attr_version_show(struct config_item *item, · line 1013
Quoted 6 lines
drivers/nvme/target/configfs.c +3
-2-static ssize_t nvmet_subsys_attr_version_store (struct config_item *item, -const char *page,size_t count) +static ssize_t +nvmet_subsys_attr_version_store_locked (struct nvmet_subsys *subsys, +const char *page,size_t count) {Seems like we lost the indentation in above line which was there previously.
We added a new line since the line was > 80 chars.
And we follow the conventions.
Quoted 21 lines
Quoted 8 lines
drivers/nvme/target/configfs.c +3
-1-struct nvmet_subsys *subsys =to_subsys (item);int major, minor, tertiary =0 ;int ret; +if (subsys->subsys_discovered) { +if (NVME_TERTIARY (subsys->ver)) + pr_err("Can't set version number. %llu.%llu.%llu is already assigned\n",drivers/nvme/target/configfs.c static ssize_t nvmet_subsys_attr_version_show(struct config_item *item, · line 1029
Quoted 10 lines
drivers/nvme/target/configfs.c +10
-0+NVME_MAJOR (subsys->ver), +NVME_MINOR (subsys->ver), + NVME_TERTIARY(subsys->ver)); + else + pr_err("Can't set version number. %llu.%llu is already assigned\n", +NVME_MAJOR (subsys->ver), + NVME_MINOR(subsys->ver)); +return -EINVAL ; + } +Can you create a helper to for about error reporting so we can avoid extra long lines whenever itis possible something like :-
Quoted 2 lines
if (subsys->subsys_discovered)
return nvmet_subsys_discover_err(subsys);We will create helper in the future in case this code will be re-used.
There is no need to create it now.
Long lines avoided by new lines.
Quoted 12 lines
Quoted 12 lines
drivers/nvme/target/configfs.c +0
-2/* passthru subsystems use the underlying controller's version */ if (nvmet_passthru_ctrl (subsys))return -EINVAL ;@@ -1022,12 +1035,25 @@ static ssize_t nvmet_subsys_attr_version_store(struct config_item *item,if (ret !=2 && ret !=3 )return -EINVAL ; -down_write (&nvmet_config_sem); subsys->ver =NVME_VS (major, minor, tertiary); -up_write (&nvmet_config_sem);
Quoted 11 lines
drivers/nvme/target/configfs.c static ssize_t nvmet_subsys_attr_version_store(struct config_item *item, · line 1048
Quoted 9 lines
drivers/nvme/target/configfs.c +7
-0return count; } + +static ssize_t nvmet_subsys_attr_version_store (struct config_item *item, +const char *page,size_t count) +{ +struct nvmet_subsys *subsys =to_subsys (item); +int ret; +nit:- ret variable type should be
ssize_tnvmet_subsys_attr_version_store_locke().
Ok. good catch.
Thanks.
Quoted 11 lines
Quoted 11 lines
drivers/nvme/target/configfs.c +8
-0+down_write (&nvmet_config_sem); +mutex_lock (&subsys->lock); + ret =nvmet_subsys_attr_version_store_locked (subsys, page, count); +mutex_unlock (&subsys->lock); +up_write (&nvmet_config_sem); + +return ret; +}CONFIGFS_ATTR (nvmet_subsys_, attr_version);
Quoted 1 line
drivers/nvme/target/configfs.c static ssize_t nvmet_subsys_attr_version_store(struct config_item *item, · line 1059
Quoted 1 line
drivers/nvme/target/configfs.c +0
-0/* See Section 1.5 of NVMe 1.4 */
_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
Re: [PATCH 4/4] nvmet: make ver stable once connection established
Quoted 9 lines · Max Gurtovoy
Quoted 8 lines · Chaitanya Kulkarni
drivers/nvme/target/configfs.c static ssize_t nvmet_subsys_attr_version_show(struct config_item *item, · line 1013
Quoted 6 lines · Max Gurtovoy
drivers/nvme/target/configfs.c +3
-2-static ssize_t nvmet_subsys_attr_version_store (struct config_item *item, -const char *page,size_t count) +static ssize_t +nvmet_subsys_attr_version_store_locked (struct nvmet_subsys *subsys, +const char *page,size_t count) {Seems like we lost the indentation in above line which was there previously.
Quoted 3 lines
We added a new line since the line was > 80 chars.
And we follow the conventions.
Original line has 5 tabs + spaces :-
-static ssize_t nvmet_subsys_attr_version_store(struct config_item *item,$
-^I^I^I^I^I const char *page, size_t count)$
This change has 2 tabs no spaces, breaks the alignment of args:-
+nvmet_subsys_attr_version_store_locked(struct nvmet_subsys *subsys,$ +^I^Iconst char *page, size_t count)$
Following fits in 70 char(note the tabs before spaces):-
static ssize_t$ nvmet_subsys_attr_version_store_locked(struct nvmet_subsys *subsys,$
^I^I^I^I const char *page, size_t count)$
It doesn't break convention, also keeps the alignment of args.
Quoted 1 line
_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
Quoted 59 lines · Max Gurtovoy
From: Noam Gottlieb <[email protected]>
According to the NVM specification, the
serial_numbershould be 20 bytes (bytes 23:04 of the Identify Controller data structure), and should contain only ASCII characters.In accordance, the
serial_numbersize is changed to 20 bytes and before any attempt to store a new value inserial_numberwe 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 <[email protected]>Signed-off-by Noam Gottlieb <[email protected]>--- 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(-)drivers/nvme/target/admin-cmd.c +1
-3@@ -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),drivers/nvme/target/configfs.c +7
-1@@ -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); }
drivers/nvme/target/configfs.c static ssize_t nvmet_subsys_attr_version_store(struct config_item *item, · line 1055
Quoted 10 lines
drivers/nvme/target/configfs.c +5
-1static 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"); drivers/nvme/target/configfs.c static ssize_t nvmet_subsys_attr_version_store(struct config_item *item, · line 1056
Quoted 2 lines
drivers/nvme/target/configfs.c +1
-1-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.
Quoted 38 lines
drivers/nvme/target/configfs.c +13
-7+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) {drivers/nvme/target/core.c +0
-0
drivers/nvme/target/core.c struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn, · line 1485
Quoted 4 lines
drivers/nvme/target/core.c +1
-0@@ -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.
Quoted 41 lines
drivers/nvme/target/core.c +2
-1subsys =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 :drivers/nvme/target/discovery.c +1
-3@@ -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),drivers/nvme/target/nvmet.h +1
-0@@ -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:
drivers/nvme/target/nvmet.h struct nvmet_subsys { · line 236
Quoted 8 lines
drivers/nvme/target/nvmet.h +1
-1@@ -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
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
Quoted 72 lines · Chaitanya Kulkarni
Quoted 59 lines · Max Gurtovoy
From: Noam Gottlieb <[email protected]>
According to the NVM specification, the
serial_numbershould be 20 bytes (bytes 23:04 of the Identify Controller data structure), and should contain only ASCII characters.In accordance, the
serial_numbersize is changed to 20 bytes and before any attempt to store a new value inserial_numberwe 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 <[email protected]>Signed-off-by Noam Gottlieb <[email protected]>--- 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(-)drivers/nvme/target/admin-cmd.c +1
-3@@ -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),drivers/nvme/target/configfs.c +7
-1@@ -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); }drivers/nvme/target/configfs.c static ssize_t nvmet_subsys_attr_version_store(struct config_item *item, · line 1055
Quoted 10 lines
drivers/nvme/target/configfs.c +5
-1static 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 :-
Quoted 6 lines
struct nvmet_subsys *subsys = to_subsys(item); size_t pos, len = strcspn(page, "\n");drivers/nvme/target/configfs.c static ssize_t nvmet_subsys_attr_version_store(struct config_item *item, · line 1056
Quoted 2 lines
drivers/nvme/target/configfs.c +1
-1-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.
Quoted 35 lines
Quoted 35 lines
drivers/nvme/target/configfs.c +13
-7+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) {drivers/nvme/target/core.c +0
-0
Quoted 8 lines
drivers/nvme/target/core.c struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn, · line 1485
Quoted 7 lines
drivers/nvme/target/core.c +1
-0@@ -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 ?
Quoted 43 lines
Quoted 43 lines
drivers/nvme/target/core.c +2
-1subsys =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 :drivers/nvme/target/discovery.c +1
-3@@ -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),drivers/nvme/target/nvmet.h +1
-0@@ -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;
Quoted 6 lines
drivers/nvme/target/nvmet.h struct nvmet_subsys { · line 236
Quoted 6 lines
drivers/nvme/target/nvmet.h +1
-1u64 ver; -u64 serial; +char serial[NVMET_SN_MAX_SIZE ];char *subsysnqn;bool pi_support;
_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
Quoted 19 lines · Max Gurtovoy
Quoted 11 lines
Quoted 11 lines
drivers/nvme/target/configfs.c +0
-6-/* 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) {drivers/nvme/target/core.c +0
-0Quoted 8 lines
drivers/nvme/target/core.c struct nvmet_subsys *nvmet_subsys_alloc(const char *subsysnqn, · line 1485
Quoted 7 lines
drivers/nvme/target/core.c +1
-0@@ -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.
Quoted 3 lines
explain bin2hex ?
I'm not sure, I'll leave it to Christoph and Sagi.
_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
Keith Busch
maintainerdrivers/nvme/target/configfs.c static ssize_t nvmet_subsys_attr_version_store(struct config_item *item, · line 1037
Quoted 5 lines · Max Gurtovoy
drivers/nvme/target/configfs.c +5
-0+/* See Section 1.5 of NVMe 1.4 */ +static bool nvmet_is_ascii (const char c) +{ +return c >=0x20 && c <=0x7e ; +}
There's a library function, "isprint()", that can provide this check.
_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
Quoted 7 lines · Keith Busch
drivers/nvme/target/configfs.c static ssize_t nvmet_subsys_attr_version_store(struct config_item *item, · line 1037
Quoted 5 lines · Max Gurtovoy
drivers/nvme/target/configfs.c +5
-0+/* See Section 1.5 of NVMe 1.4 */ +static bool nvmet_is_ascii (const char c) +{ +return c >=0x20 && c <=0x7e ; +}There's a library function, "
isprint()", that can provide this check.
This is an existing function in the code.
We'll add a dedicated commit to replace it.
_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
Quoted 10 lines · Max Gurtovoy
Quoted 7 lines · Keith Busch
drivers/nvme/target/configfs.c static ssize_t nvmet_subsys_attr_version_store(struct config_item *item, · line 1037
Quoted 5 lines · Max Gurtovoy
drivers/nvme/target/configfs.c +5
-0+/* See Section 1.5 of NVMe 1.4 */ +static bool nvmet_is_ascii (const char c) +{ + return c >= 0x20 && c <= 0x7e; +}There's a library function, "
isprint()", that can provide this check.
Quoted 4 lines
This is an existing function in the code.
We'll add a dedicated commit to replace it.
Actually the isprint check is weaker than the nvmet_is_ascii so we'll leave it as-is.
Thanks.
_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme