[RFC 5/9] block: Add support functions for persistent durable name

Tony Asleson
2019-12-23T22:56:15Z

Add functions to retrieve and build durable name string into supplied
buffer for functions that log using struct device and struct scsi_device.

Signed-off-by: Tony Asleson <tasleson@redhat.com>
---
drivers/base/core.c | 24 ++++++++++++++++++++++++
drivers/scsi/scsi_lib.c | 20 ++++++++++++++++++++
drivers/scsi/scsi_sysfs.c | 1 +
include/linux/device.h | 2 ++
include/scsi/scsi_device.h | 3 +++
5 files changed, 50 insertions(+)

diff --git a/drivers/base/core.c b/drivers/base/core.c
index 7bd9cd366d41..93cc1c45e9d3 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -2009,6 +2009,30 @@ int dev_set_name(struct device *dev, const char *fmt, ...)
}
EXPORT_SYMBOL_GPL(dev_set_name);

+/**
+ * dev_durable_name - Write "DURABLE_NAME"=<durable name> in buffer
+ * @dev: device
+ * @buffer: character buffer to write results
+ * @len: length of buffer
+ * @return Number of bytes written to buffer
+ */
+int dev_durable_name(const struct device *dev, char *buffer, size_t len)
+{
+ int tmp, dlen;
+
+ if (dev->type && dev->type->durable_name) {
+ tmp = snprintf(buffer, len, "DURABLE_NAME=");
+ if (tmp < len) {
+ dlen = dev->type->durable_name(dev, buffer + tmp,
+ len - tmp);
+ if (dlen > 0 && ((dlen + tmp) < len))
+ return dlen + tmp;
+ }
+ }
+ return 0;
+}
+EXPORT_SYMBOL_GPL(dev_durable_name);
+
/**
* device_to_dev_kobj - select a /sys/dev/ directory for the device
* @dev: device
diff --git a/drivers/scsi/scsi_lib.c b/drivers/scsi/scsi_lib.c
index 91c007d26c1e..f22e59253d9d 100644
--- a/drivers/scsi/scsi_lib.c
+++ b/drivers/scsi/scsi_lib.c
@@ -3120,3 +3120,23 @@ int scsi_vpd_tpg_id(struct scsi_device *sdev, int *rel_id)
return group_id;
}
EXPORT_SYMBOL(scsi_vpd_tpg_id);
+
+int dev_to_scsi_durable_name(const struct device *dev, char *buf, size_t len)
+{
+ return scsi_durable_name(to_scsi_device(dev), buf, len);
+}
+EXPORT_SYMBOL(dev_to_scsi_durable_name);
+
+int scsi_durable_name(struct scsi_device *sdev, char *buf, size_t len)
+{
+ int vpd_len = 0;
+
+ vpd_len = scsi_vpd_lun_id(sdev, buf, len);
+ if (vpd_len > 0 && vpd_len < len)
+ vpd_len = strim_dupe(buf) + 1;
+ else
+ vpd_len = 0;
+
+ return vpd_len;
+}
+EXPORT_SYMBOL(scsi_durable_name);
diff --git a/drivers/scsi/scsi_sysfs.c b/drivers/scsi/scsi_sysfs.c
index 6d7362e7367e..ee5b8197916f 100644
--- a/drivers/scsi/scsi_sysfs.c
+++ b/drivers/scsi/scsi_sysfs.c
@@ -1560,6 +1560,7 @@ static struct device_type scsi_dev_type = {
.name = "scsi_device",
.release = scsi_device_dev_release,
.groups = scsi_sdev_attr_groups,
+ .durable_name = dev_to_scsi_durable_name,
};

void scsi_sysfs_device_initialize(struct scsi_device *sdev)
diff --git a/include/linux/device.h b/include/linux/device.h
index dd4ac8db5f57..566f6be6ee0d 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -1350,6 +1350,8 @@ static inline const char *dev_name(const struct device *dev)
extern __printf(2, 3)
int dev_set_name(struct device *dev, const char *name, ...);

+int dev_durable_name(const struct device *d, char *buffer, size_t len);
+
#ifdef CONFIG_NUMA
static inline int dev_to_node(struct device *dev)
{
diff --git a/include/scsi/scsi_device.h b/include/scsi/scsi_device.h
index 202f4d6a4342..1cb35e10f54a 100644
--- a/include/scsi/scsi_device.h
+++ b/include/scsi/scsi_device.h
@@ -455,6 +455,9 @@ extern void sdev_disable_disk_events(struct scsi_device *sdev);
extern void sdev_enable_disk_events(struct scsi_device *sdev);
extern int scsi_vpd_lun_id(struct scsi_device *, char *, size_t);
extern int scsi_vpd_tpg_id(struct scsi_device *, int *);
+extern int dev_to_scsi_durable_name(const struct device *dev, char *buf,
+ size_t len);
+extern int scsi_durable_name(struct scsi_device *sdev, char *buf, size_t len);

#ifdef CONFIG_PM
extern int scsi_autopm_get_device(struct scsi_device *);
--
2.21.0