Add a mpath_dev file so that the multipath disk can be looked up from
per-path scsi_disk directory.

The following is an example of this usage:

$ ls -l /dev/sdc
brw-rw----    1 root     disk        8,  32 Feb 24 16:08 /dev/sdc
$ cat /sys/class/scsi_mpath_disk/scsi_mpath_disk0/sdc/multipath/sdc:0/mpath_dev
8:32

This can be used by a util like lsscsi, which would find that the gendisk
for the per-path scsi_disk is missing.

Signed-off-by: John Garry <[email protected]>
---
 drivers/scsi/sd.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 70 insertions(+), 1 deletion(-)

diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index f044b2dfaa9fd..0d73b03a3daee 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -4109,6 +4109,73 @@ static int sd_mpath_add_disk(struct scsi_disk *sdkp)
        return 0;
 }
 
+static ssize_t sd_mpath_dev_show(struct device *dev,
+                       struct device_attribute *attr, char *page)
+{
+       struct gendisk *gd = dev_to_disk(dev);
+       struct scsi_disk *sdkp = gd->private_data;
+       struct scsi_device *sdev = sdkp->device;
+       struct scsi_mpath_device *scsi_mpath_dev = sdev->scsi_mpath_dev;
+       struct scsi_mpath_head *scsi_mpath_head =
+                               scsi_mpath_dev->scsi_mpath_head;
+       struct mpath_head *mpath_head = &scsi_mpath_head->mpath_head;
+       struct sd_mpath_disk *sd_mpath_disk;
+       struct device *disk_dev;
+       struct gendisk *disk;
+       char devt_str[13] = "";
+
+       /*
+        * This attr is for the per-path scsi_disk block device. We need to
+        * handle the scenario that the multipath disk has been deleted and
+        * the per-path scsi_disk (and its attr files) is still present.
+        */
+       mutex_lock(&sd_mpath_disks_lock);
+       sd_mpath_disk = sd_mpath_find_disk(scsi_mpath_head);
+       if (!sd_mpath_disk)
+               goto out;
+       if (!sd_mpath_disk->disk_count)
+               goto out_put_disk;
+       disk = mpath_head->disk;
+       disk_dev = disk_to_dev(disk);
+       format_dev_t(devt_str, disk_dev->devt);
+out_put_disk:
+       sd_mpath_put_disk(sd_mpath_disk);
+out:
+       mutex_unlock(&sd_mpath_disks_lock);
+       return sysfs_emit(page, "%s\n", devt_str);
+}
+static DEVICE_ATTR(mpath_dev, 0444, sd_mpath_dev_show, NULL);
+
+static struct attribute *sd_mpath_dev_attrs[] = {
+       &dev_attr_mpath_dev.attr,
+       NULL
+};
+
+static umode_t sd_mpath_dev_attr_is_visible(struct kobject *kobj,
+                               struct attribute *attr, int i)
+{
+       struct device *dev = kobj_to_dev(kobj);
+       struct gendisk *gd = dev_to_disk(dev);
+       struct scsi_disk *sdkp = gd->private_data;
+       struct scsi_device *sdev = sdkp->device;
+       struct scsi_mpath_device *scsi_mpath_device = sdev->scsi_mpath_dev;
+
+       if (!scsi_mpath_device)
+               return 0;
+
+       return attr->mode;
+}
+
+static const struct attribute_group sd_mpath_dev_attr_group = {
+       .is_visible = sd_mpath_dev_attr_is_visible,
+       .attrs = sd_mpath_dev_attrs,
+};
+
+static const struct attribute_group *sd_mpath_dev_groups[] = {
+       &sd_mpath_dev_attr_group,
+       NULL
+};
+
 static ssize_t sd_mpath_device_delayed_removal_store(struct device *dev,
                struct device_attribute *attr, const char *buf, size_t count)
 {
@@ -4389,6 +4456,8 @@ static int sd_mpath_add_disk(struct scsi_disk *sdkp)
 {
        return 0;
 }
+
+#define sd_mpath_dev_groups NULL
 #endif
 
 /**
@@ -4539,7 +4608,7 @@ static int sd_probe(struct scsi_device *sdp)
                        sdp->host->rpm_autosuspend_delay);
        }
 
-       error = device_add_disk(dev, gd, NULL);
+       error = device_add_disk(dev, gd, sd_mpath_dev_groups);
        if (error) {
                sd_mpath_fail_probe(sdkp);
                device_unregister(&sdkp->disk_dev);
-- 
2.43.7


Reply via email to