Skip to contentSkip to conversation

nvmet: change sn size and check validity

PATCH Applied Max Gurtovoy 3 participants 8 messages Jun 7, 2021 – Jun 8, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 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.

---

changes from v1: minor nits from Chaitanya

---
 drivers/nvme/target/admin-cmd.c |  4 +---
 drivers/nvme/target/configfs.c  | 33 +++++++++++++++++++++++----------
 drivers/nvme/target/core.c      |  4 +++-
 drivers/nvme/target/discovery.c |  4 +---
 drivers/nvme/target/nvmet.h     |  3 ++-
 5 files changed, 30 insertions(+), 18 deletions(-)
drivers/nvme/target/admin-cmd.c +1 -3
index dcd49a72f2f3..9c73dbfb8228 100644
@@ -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 +23 -10
index 65a0cf99f557..027b28aaf7cd 100644
@@ -1030,24 +1030,43 @@ 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 = to_subsys(item);
+	int pos, len = strcspn(page, "\n");
 
-	if (sscanf(page, "%llx\n", &serial) != 1)
+	if (!len || 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 +1147,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
index 4ae4bea6625d..213a0c2af4f7 100644
@@ -1493,6 +1493,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)
@@ -1500,7 +1501,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
index fc3645fc2c24..b7fdad13094a 100644
@@ -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
index d69a409515d6..0ae809ca428c 100644
@@ -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.1


_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
#1 lore raw

PATCH v2 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  | 27 ++++++++++++++++++++++-----
 drivers/nvme/target/nvmet.h     |  1 +
 3 files changed, 29 insertions(+), 5 deletions(-)
drivers/nvme/target/admin-cmd.c +6 -0
index 9c73dbfb8228..5f15c3285a5b 100644
@@ -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 -5
index 027b28aaf7cd..a13da86fb374 100644
@@ -1044,12 +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 = to_subsys(item);
 	int pos, len = strcspn(page, "\n");
 
+	if (subsys->subsys_discovered) {
+		pr_err("Can't set serial number. %s is already assigned\n",
+		       subsys->serial);
+		return -EINVAL;
+	}
+
 	if (!len || len > NVMET_SN_MAX_SIZE) {
 		pr_err("Serial Number can not be empty or exceed %d Bytes\n",
 		       NVMET_SN_MAX_SIZE);
@@ -1063,13 +1069,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
index 0ae809ca428c..bd0a0b91d843 100644
@@ -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.1


_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
#2 lore raw

Re: [PATCH v2 2/4] nvmet: make sn stable once connection was established

Max,

drivers/nvme/target/nvmet.h struct nvmet_subsys { · line 234

Quoted 9 lines · Max Gurtovoy
drivers/nvme/target/nvmet.h +1 -0
index 0ae809ca428c..bd0a0b91d843 100644
@@ -231,6 +231,7 @@ struct nvmet_subsys {
 
 	u64			ver;
 	char			serial[NVMET_SN_MAX_SIZE];
+	bool			subsys_discovered;

When subsystem is discovered (connected to the host) we will have non empty controller list, instead of adding a new bool variable why not use something like following tested patch on the top of this one ? (that also fixes the indentation of nvmet_subsys_attr_serial_store_locked()).

(quoted diff) +0 -0

b/drivers/nvme/target/admin-cmd.c
index bec84c231d37..897f419be618 100644

drivers/nvme/target/admin-cmd.c +0 -0
@@ -366,12 +366,6 @@ 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 +0 -0
index a13da86fb374..94e23dca763f 100644
@@ -1046,11 +1046,11 @@ static ssize_t

nvmet_subsys_attr_serial_show(struct config_item *item,

static ssize_t nvmet_subsys_attr_serial_store_locked(struct nvmet_subsys *subsys,
- const char *page, size_t count)
+ const char *page, size_t count)

 {
        int pos, len = strcspn(page, "\n");

- if (subsys->subsys_discovered) {

+       if (!list_empty(&subsys->ctrls)) {
                pr_err("Can't set serial number. %s is already assigned\n",
                       subsys->serial);
                return -EINVAL;
drivers/nvme/target/nvmet.h +0 -1
index f7fbf9690ce8..73c6c4dc8997 100644
@@ -232,7 +232,6 @@ struct nvmet_subsys {
 
        u64                     ver;
        char                    serial[NVMET_SN_MAX_SIZE];
-       bool                    subsys_discovered;
        char                    *subsysnqn;
        bool                    pi_support;
 
# nvme list
Node                  SN                  
Model                                    Namespace
Usage                      Format           FW Rev 
--------------------- --------------------
---------------------------------------- ---------
-------------------------- ---------------- --------
/dev/nvme0n1          foo                  QEMU NVMe
Ctrl                           1           1.07  GB /   1.07  GB   
512   B +  0 B   1.0    
/dev/nvme1n1          bf854da611a264d448c2
Linux                                    1           2.15  GB /   2.15 
GB      4 KiB +  0 B   5.13.0-r
# echo 1 > /sys/kernel/config/nvmet/subsystems/nqn/
allowed_hosts/       attr_cntlid_min      attr_serial          passthru/
attr_allow_any_host  attr_model           attr_version        
attr_cntlid_max      attr_pi_enable       namespaces/         
 # echo "1234567890" > /sys/kernel/config/nvmet/subsystems/nqn/attr_serial
bash: echo: write error: Invalid argument
# dmesg  -c
[311081.155148] nvmet: Can't set serial number. bf854da611a264d448c2 is
already assigned

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#3 lore raw

Re: [PATCH v2 2/4] nvmet: make sn stable once connection was established

Quoted 9 lines · Chaitanya Kulkarni

Max,

Quoted 6 lines · Max Gurtovoy
drivers/nvme/target/nvmet.h +0 -0
index 0ae809ca428c..bd0a0b91d843 100644
@@ -231,6 +231,7 @@ struct nvmet_subsys {
 
Quoted 6 lines

drivers/nvme/target/nvmet.h struct nvmet_subsys {

Quoted 3 lines
drivers/nvme/target/nvmet.h +1 -0
  	u64			ver;
  	char			serial[NVMET_SN_MAX_SIZE];
+	bool			subsys_discovered;

When subsystem is discovered (connected to the host) we will have non empty controller list, instead of adding a new bool variable why not use something like following tested patch on the top of this one ?

No it's not the same. The controller list can be empty if initiator_A performed connect + disconnect.

In your solution, it is allowed to change the sn after the disconnection but in the original solution we don't allow this.

Once *any* initiator connected to this subsystem, the serial nmber will not be changed anymore.

Quoted 68 lines

(that also fixes the indentation of
nvmet_subsys_attr_serial_store_locked()).

drivers/nvme/target/admin-cmd.c +0 -6
index bec84c231d37..897f419be618 100644
@@ -366,12 +366,6 @@ 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 +2 -2
index a13da86fb374..94e23dca763f 100644
@@ -1046,11 +1046,11 @@ static ssize_t
 nvmet_subsys_attr_serial_show(struct config_item *item,
 
  static ssize_t
  nvmet_subsys_attr_serial_store_locked(struct nvmet_subsys *subsys,
-               const char *page, size_t count)
+                                     const char *page, size_t count)
  {
         int pos, len = strcspn(page, "\n");
 
-       if (subsys->subsys_discovered) {
+       if (!list_empty(&subsys->ctrls)) {
                 pr_err("Can't set serial number. %s is already assigned\n",
                        subsys->serial);
                 return -EINVAL;
drivers/nvme/target/nvmet.h +0 -1
index f7fbf9690ce8..73c6c4dc8997 100644
@@ -232,7 +232,6 @@ struct nvmet_subsys {
 
         u64                     ver;
         char                    serial[NVMET_SN_MAX_SIZE];
-       bool                    subsys_discovered;
         char                    *subsysnqn;
         bool                    pi_support;
 
# nvme list
Node                  SN
Model                                    Namespace
Usage                      Format           FW Rev
--------------------- --------------------
---------------------------------------- ---------
-------------------------- ---------------- --------
/dev/nvme0n1          foo                  QEMU NVMe
Ctrl                           1           1.07  GB /   1.07  GB
512   B +  0 B   1.0
/dev/nvme1n1          bf854da611a264d448c2
Linux                                    1           2.15  GB /   2.15
GB      4 KiB +  0 B   5.13.0-r
# echo 1 > /sys/kernel/config/nvmet/subsystems/nqn/
Quoted 10 lines
allowed_hosts/       attr_cntlid_min      attr_serial          passthru/
attr_allow_any_host  attr_model           attr_version
attr_cntlid_max      attr_pi_enable       namespaces/
  # echo "1234567890" > /sys/kernel/config/nvmet/subsystems/nqn/attr_serial
bash: echo: write error: Invalid argument
# dmesg  -c
[311081.155148] nvmet: Can't set serial number. bf854da611a264d448c2 is
already assigned

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#4 lore raw

PATCH v2 3/4 nvmet: allow mn change if subsys not discovered

In reply to Max Gurtovoy

From: 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
index 5f15c3285a5b..cd60a8184d04 100644
@@ -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
index a13da86fb374..9ef8708b92c6 100644
@@ -1154,14 +1154,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,
@@ -1169,7 +1163,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
index 213a0c2af4f7..146909486b8f 100644
@@ -1494,6 +1494,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)
@@ -1504,6 +1505,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;
@@ -1513,15 +1520,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;
@@ -1533,6 +1540,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
index b7fdad13094a..7aa62bc6ae84 100644
@@ -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.1


_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
#5 lore raw

PATCH 4/4 nvmet: make ver stable once connection established

In reply to 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.

---

changes from v1: changing "ret" to ssize_t (from Chaitanya)

---
 drivers/nvme/target/configfs.c | 36 +++++++++++++++++++++++++++++-----
 1 file changed, 31 insertions(+), 5 deletions(-)
drivers/nvme/target/configfs.c +31 -5
index 9ef8708b92c6..273555127188 100644
@@ -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);
+	ssize_t 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.1


_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
Reviewed-by on v2 1/4
#6 lore raw
In reply to Max Gurtovoy
Quoted 6 lines · Max Gurtovoy

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.

Quoted 8 lines

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.

---

Looks good.

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme