diff mbox series

[RFC,v2,7/7] nvme: Add durable name for dev_printk

Message ID 20200513213621.470411-8-tasleson@redhat.com (mailing list archive)
State New, archived
Headers show
Series Add persistent durable identifier to storage log messages | expand

Commit Message

Tony Asleson May 13, 2020, 9:36 p.m. UTC
Signed-off-by: Tony Asleson <tasleson@redhat.com>
---
 drivers/nvme/host/core.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

Comments

Keith Busch May 13, 2020, 11:04 p.m. UTC | #1
On Wed, May 13, 2020 at 04:36:21PM -0500, Tony Asleson wrote:
> +static int dev_to_nvme_durable_name(const struct device *dev, char *buf, size_t len)
> +{
> +	char serial[128];
> +	ssize_t serial_len = wwid_show((struct device *)dev, NULL, serial);

wwid_show() can generate a serial larger than 128 bytes.

> +
> +	if (serial_len > 0 && serial_len < len) {
> +		serial_len -= 1;  // Remove the '\n' from the string

Comments in this driver should use the /* */ style.

> +		strncpy(buf, serial, serial_len);
> +		return serial_len;
> +	}
> +	return 0;
> +}
Tony Asleson May 14, 2020, 5:13 p.m. UTC | #2
On 5/13/20 6:04 PM, Keith Busch wrote:
> On Wed, May 13, 2020 at 04:36:21PM -0500, Tony Asleson wrote:
>> +static int dev_to_nvme_durable_name(const struct device *dev, char *buf, size_t len)
>> +{
>> +	char serial[128];
>> +	ssize_t serial_len = wwid_show((struct device *)dev, NULL, serial);
> 
> wwid_show() can generate a serial larger than 128 bytes.

Looking at this again

return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", 	
    subsys->vendor_id,
    serial_len, subsys->serial,
    model_len, subsys->model,
    subsys->ns_id);

'nvme.' = 5
vendor_id = 4
'-' = 1
serial (20 * 2) = 40
'-' = 1
model (40 * 2) = 80
'-' = 1
ns_id = 8
'\n' = 1

5 + 4 + 1 + 40 + 1 + 80 + 1 + 8 + 1 = 141

Does this match your understanding?

My mistake was thinking each byte of SN and Model = 1 character in
output, instead of 2.

This will also require the buffer used in dev_vprintk_emit to be quite a
bit bigger to accommodate max size.

>> +
>> +	if (serial_len > 0 && serial_len < len) {
>> +		serial_len -= 1;  // Remove the '\n' from the string
> 
> Comments in this driver should use the /* */ style.

OK, will revise.

> 
>> +		strncpy(buf, serial, serial_len);
>> +		return serial_len;
>> +	}
>> +	return 0;
>> +}
> 

Thanks
-Tony
Keith Busch May 14, 2020, 5:20 p.m. UTC | #3
On Thu, May 14, 2020 at 12:13:36PM -0500, Tony Asleson wrote:
> On 5/13/20 6:04 PM, Keith Busch wrote:
> > On Wed, May 13, 2020 at 04:36:21PM -0500, Tony Asleson wrote:
> >> +static int dev_to_nvme_durable_name(const struct device *dev, char *buf, size_t len)
> >> +{
> >> +	char serial[128];
> >> +	ssize_t serial_len = wwid_show((struct device *)dev, NULL, serial);
> > 
> > wwid_show() can generate a serial larger than 128 bytes.
> 
> Looking at this again
> 
> return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", 	
>     subsys->vendor_id,
>     serial_len, subsys->serial,
>     model_len, subsys->model,
>     subsys->ns_id);
> 
> 'nvme.' = 5
> vendor_id = 4
> '-' = 1
> serial (20 * 2) = 40
> '-' = 1
> model (40 * 2) = 80
> '-' = 1
> ns_id = 8
> '\n' = 1
> 
> 5 + 4 + 1 + 40 + 1 + 80 + 1 + 8 + 1 = 141
> 
> Does this match your understanding?

Yep, that looks like the correct max possible length. I didn't actually
count it out, but I just knew it could be larger than 128. :)
diff mbox series

Patch

diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
index a4d8c90ee7cc..7479c7e82200 100644
--- a/drivers/nvme/host/core.c
+++ b/drivers/nvme/host/core.c
@@ -2614,6 +2614,22 @@  static bool nvme_validate_cntlid(struct nvme_subsystem *subsys,
 	return true;
 }
 
+static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
+			char *buf);
+
+static int dev_to_nvme_durable_name(const struct device *dev, char *buf, size_t len)
+{
+	char serial[128];
+	ssize_t serial_len = wwid_show((struct device *)dev, NULL, serial);
+
+	if (serial_len > 0 && serial_len < len) {
+		serial_len -= 1;  // Remove the '\n' from the string
+		strncpy(buf, serial, serial_len);
+		return serial_len;
+	}
+	return 0;
+}
+
 static int nvme_init_subsystem(struct nvme_ctrl *ctrl, struct nvme_id_ctrl *id)
 {
 	struct nvme_subsystem *subsys, *found;
@@ -3541,6 +3557,8 @@  static int nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
 	disk->queue = ns->queue;
 	disk->flags = flags;
 	memcpy(disk->disk_name, disk_name, DISK_NAME_LEN);
+	disk_to_dev(disk)->durable_name = dev_to_nvme_durable_name;
+
 	ns->disk = disk;
 
 	__nvme_revalidate_disk(disk, id);