diff mbox series

[v4,04/11] scsi: Add durable_name for dev_printk

Message ID 20200724171706.1550403-5-tasleson@redhat.com
State Not Applicable
Delegated to: David Miller
Headers show
Series Add persistent durable identifier to storage log messages | expand

Commit Message

Tony Asleson July 24, 2020, 5:16 p.m. UTC
Add the needed functions to fill out the durable_name function
call back for scsi based storage devices.  This allows calls
into dev_printk for scsi devices to have a persistent id
associated with them.

Signed-off-by: Tony Asleson <tasleson@redhat.com>
---
 drivers/scsi/scsi_lib.c    | 14 ++++++++++++++
 drivers/scsi/scsi_sysfs.c  | 23 +++++++++++++++++++++++
 drivers/scsi/sd.c          |  2 ++
 include/scsi/scsi_device.h |  3 +++
 4 files changed, 42 insertions(+)

Comments

Andy Shevchenko July 25, 2020, 10:20 a.m. UTC | #1
On Fri, Jul 24, 2020 at 8:19 PM Tony Asleson <tasleson@redhat.com> wrote:
>
> Add the needed functions to fill out the durable_name function
> call back for scsi based storage devices.  This allows calls
> into dev_printk for scsi devices to have a persistent id
> associated with them.

...

> +int scsi_durable_name(struct scsi_device *sdev, char *buf, size_t len)
> +{
> +       int vpd_len = 0;

What is the purpose of the assignment?

> +       vpd_len = scsi_vpd_lun_id(sdev, buf, len);
> +       if (vpd_len > 0 && vpd_len < len)
> +               vpd_len++;
> +       else
> +               vpd_len = 0;
> +
> +       return vpd_len;

int vpd_len;

vpd_len = ...(...);
if (vpd_len ...)
  return vpd_len + 1;
return 0;

> +}

...

>  };
>
> +

One blank line is enough.

> +int dev_to_scsi_durable_name(const struct device *dev, char *buf, size_t len)
> +{
> +       struct scsi_device *sd_dev = NULL;

Redundant assignment.

> +       // When we go through dev_printk in the scsi layer, dev is embedded
> +       // in a struct scsi_device.  When we go through the block layer,
> +       // dev is embedded in struct genhd, thus we need different paths to
> +       // retrieve the struct scsi_device to call scsi_durable_name.
> +       if (dev->type == &scsi_dev_type) {
> +               sd_dev = to_scsi_device(dev);

This...

> +       } else if (dev->parent && dev->parent->type == &scsi_dev_type) {
> +               sd_dev = to_scsi_device(dev->parent);

...and this along with pieces in scsi_bus_uevent(), scsi_bus_match()
are candidates for a good helper (perhaps with utilization of
scsi_is_sdev_device(), but maybe not necessary).

static struct scsi_device *dev_to_scsi_device(struct device *dev)
{
  return dev->type == &scsi_dev_type ? to_scsi_device(dev) : NULL;
}

sdev = dev_to_scsi_device(dev);
if (!sdev)
  sdev = dev_to_scsi_device(dev->parent);
if (!sdev)
  return 0;

And when introducing a helper change users at the same time.

> +       } else {
> +               // We have a pointer to something else, bail
> +               return 0;
> +       }
> +
> +       return scsi_durable_name(sd_dev, buf, len);
> +}
diff mbox series

Patch

diff --git a/drivers/scsi/scsi_lib.c b/drivers/scsi/scsi_lib.c
index 06c260f6cdae..9f6c41162c55 100644
--- a/drivers/scsi/scsi_lib.c
+++ b/drivers/scsi/scsi_lib.c
@@ -3142,3 +3142,17 @@  int scsi_vpd_tpg_id(struct scsi_device *sdev, int *rel_id)
 	return group_id;
 }
 EXPORT_SYMBOL(scsi_vpd_tpg_id);
+
+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++;
+	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 163dbcb741c1..f719b63f4b63 100644
--- a/drivers/scsi/scsi_sysfs.c
+++ b/drivers/scsi/scsi_sysfs.c
@@ -1582,6 +1582,28 @@  static struct device_type scsi_dev_type = {
 	.groups =	scsi_sdev_attr_groups,
 };
 
+
+int dev_to_scsi_durable_name(const struct device *dev, char *buf, size_t len)
+{
+	struct scsi_device *sd_dev = NULL;
+
+	// When we go through dev_printk in the scsi layer, dev is embedded
+	// in a struct scsi_device.  When we go through the block layer,
+	// dev is embedded in struct genhd, thus we need different paths to
+	// retrieve the struct scsi_device to call scsi_durable_name.
+	if (dev->type == &scsi_dev_type) {
+		sd_dev = to_scsi_device(dev);
+	} else if (dev->parent && dev->parent->type == &scsi_dev_type) {
+		sd_dev = to_scsi_device(dev->parent);
+	} else {
+		// We have a pointer to something else, bail
+		return 0;
+	}
+
+	return scsi_durable_name(sd_dev, buf, len);
+}
+EXPORT_SYMBOL(dev_to_scsi_durable_name);
+
 void scsi_sysfs_device_initialize(struct scsi_device *sdev)
 {
 	unsigned long flags;
@@ -1591,6 +1613,7 @@  void scsi_sysfs_device_initialize(struct scsi_device *sdev)
 	device_initialize(&sdev->sdev_gendev);
 	sdev->sdev_gendev.bus = &scsi_bus_type;
 	sdev->sdev_gendev.type = &scsi_dev_type;
+	sdev->sdev_gendev.durable_name = dev_to_scsi_durable_name;
 	dev_set_name(&sdev->sdev_gendev, "%d:%d:%d:%llu",
 		     sdev->host->host_no, sdev->channel, sdev->id, sdev->lun);
 
diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index a793cb08d025..f40e4cb4a5f6 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -3360,6 +3360,8 @@  static int sd_probe(struct device *dev)
 	gd->private_data = &sdkp->driver;
 	gd->queue = sdkp->device->request_queue;
 
+	disk_to_dev(gd)->durable_name = dev_to_scsi_durable_name;
+
 	/* defaults, until the device tells us otherwise */
 	sdp->sector_size = 512;
 	sdkp->capacity = 0;
diff --git a/include/scsi/scsi_device.h b/include/scsi/scsi_device.h
index c3cba2aaf934..7be5861565f7 100644
--- a/include/scsi/scsi_device.h
+++ b/include/scsi/scsi_device.h
@@ -461,6 +461,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 *);