Add a mpath_dev file so that the multipath disk can be looked up from
per-path gendisk 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/0/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_device is missing.

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

diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index 409c0937764d9..f5922a9fe6c1b 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -4198,6 +4198,51 @@ static int sd_mpath_revalidate_head(struct scsi_disk 
*sdkp)
 
        return ret;
 }
+
+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 sd_mpath_disk *sd_mpath_disk = sdkp->sd_mpath_disk;
+       struct mpath_disk *mpath_disk = sd_mpath_disk->mpath_disk;
+       struct gendisk *disk = mpath_disk->disk;
+       struct device *disk_dev = disk_to_dev(disk);
+
+       return print_dev_t(page, disk_dev->devt);
+}
+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 int sd_mpath_get_disk(struct sd_mpath_disk *sd_mpath_disk)
 {
        if (!get_device(&sd_mpath_disk->dev))
@@ -4461,6 +4506,8 @@ static int sd_mpath_revalidate_head(struct scsi_disk 
*sdkp)
 static void sd_mpath_add_disk(struct scsi_disk *sdkp)
 {
 }
+
+#define sd_mpath_dev_groups NULL
 #endif
 /**
  *     sd_probe - called during driver initialization and whenever a
@@ -4602,7 +4649,7 @@ static int sd_probe(struct device *dev)
                        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.5


Reply via email to