All of lore.kernel.org
 help / color / mirror / Atom feed
From: mwilck@suse.com (Martin Wilck)
Subject: [PATCH 3/3] nvme: wwid_show: copy hex string verbatim
Date: Fri, 14 Jul 2017 00:25:33 +0200	[thread overview]
Message-ID: <20170713222533.30794-4-mwilck@suse.com> (raw)
In-Reply-To: <20170713222533.30794-1-mwilck@suse.com>

If the serial number is a series of hex digits (as usual for the
Linux target), there's no need to encode it in hex once more.
This will improve the readability of NVME wwids for users.

Signed-off-by: Martin Wilck <mwilck at suse.com>
---
 drivers/nvme/host/core.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
index deced6b91442d..c80e90603c9c4 100644
--- a/drivers/nvme/host/core.c
+++ b/drivers/nvme/host/core.c
@@ -27,6 +27,7 @@
 #include <linux/nvme_ioctl.h>
 #include <linux/t10-pi.h>
 #include <linux/pm_qos.h>
+#include <linux/ctype.h>
 #include <asm/unaligned.h>
 
 #include "nvme.h"
@@ -1987,6 +1988,14 @@ static ssize_t nvme_sysfs_rescan(struct device *dev,
 }
 static DEVICE_ATTR(rescan_controller, S_IWUSR, NULL, nvme_sysfs_rescan);
 
+static bool _is_hex_string(const char *buf, int len)
+{
+	const char *p;
+
+	for (p = buf; p < buf + len && isxdigit(*p); p++);
+	return p == buf + len;
+}
+
 static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
 								char *buf)
 {
@@ -1994,6 +2003,7 @@ static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
 	struct nvme_ctrl *ctrl = ns->ctrl;
 	int serial_len = sizeof(ctrl->serial);
 	int model_len = sizeof(ctrl->model);
+	char *p;
 
 	if (memchr_inv(ns->nguid, 0, sizeof(ns->nguid)))
 		return sprintf(buf, "eui.%16phN\n", ns->nguid);
@@ -2008,8 +2018,18 @@ static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
 	       ctrl->model[model_len - 1] == '\0')
 		model_len--;
 
-	return sprintf(buf, "nvme.%04x-%*phN-%*phN-%08x\n", ctrl->vid,
-		serial_len, ctrl->serial, model_len, ctrl->model, ns->ns_id);
+	p = buf + sprintf(buf, "nvme.%04x-", ctrl->vid);
+
+	if (_is_hex_string(ctrl->serial, serial_len)) {
+		memcpy(p, ctrl->serial, serial_len);
+		p += serial_len;
+		*p++ = '-';
+	} else
+		p += sprintf(p, "%*phN-", serial_len, ctrl->serial);
+
+	p += sprintf(p, "%*phN-%08x\n", model_len, ctrl->model, ns->ns_id);
+
+	return p - buf;
 }
 static DEVICE_ATTR(wwid, S_IRUGO, wwid_show, NULL);
 
-- 
2.13.2

  parent reply	other threads:[~2017-07-13 22:25 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-13 22:25 [PATCH 0/3] Improve readbility of NVME "wwid" attribute Martin Wilck
2017-07-13 22:25 ` [PATCH 1/3] nvmet: identify controller: improve standard compliance Martin Wilck
2017-07-14  7:54   ` Christoph Hellwig
2017-07-13 22:25 ` [PATCH 2/3] nvme: wwid_show: strip trailing 0-bytes Martin Wilck
2017-07-14  7:54   ` Christoph Hellwig
2017-07-13 22:25 ` Martin Wilck [this message]
2017-07-14  7:54   ` [PATCH 3/3] nvme: wwid_show: copy hex string verbatim Christoph Hellwig
2017-07-14  9:58     ` Martin Wilck
2017-07-14 12:30       ` Christoph Hellwig
2017-07-14 19:40         ` Martin Wilck
2017-07-15  8:46           ` Christoph Hellwig
2017-07-17  6:12             ` Hannes Reinecke
2017-07-13 22:57 ` [PATCH 0/3] Improve readbility of NVME "wwid" attribute Keith Busch
2017-07-14  7:54   ` Martin Wilck
2017-07-14  7:57     ` Christoph Hellwig
2017-07-14  7:56   ` Christoph Hellwig
2017-07-14  9:41     ` Martin Wilck
2017-07-14 12:28       ` Christoph Hellwig
2017-07-14  7:53 ` Christoph Hellwig
2017-07-14  8:54   ` Martin Wilck

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=20170713222533.30794-4-mwilck@suse.com \
    --to=mwilck@suse.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.