Provide a link in sysfs from a scsi_mpath_device to member scsi_device's.

An example is as follows:
# ls -l /sys/class/scsi_mpath_device/0/multipath/
total 0
lrwxrwxrwx    1 root     root             0 Feb 24 12:01 8:0:0:0 -> 
../../../../platform/host8/session1/target8:0:0/8:0:0:0
lrwxrwxrwx    1 root     root             0 Feb 24 12:01 9:0:0:0 -> 
../../../../platform/host9/session2/target9:0:0/9:0:0:0

Signed-off-by: John Garry <[email protected]>
---
 drivers/scsi/scsi_multipath.c | 45 +++++++++++++++++++++++++++++++++++
 drivers/scsi/scsi_sysfs.c     |  5 ++++
 include/scsi/scsi_multipath.h |  9 +++++++
 3 files changed, 59 insertions(+)

diff --git a/drivers/scsi/scsi_multipath.c b/drivers/scsi/scsi_multipath.c
index 05af178921cb4..ac55f9f39a5b2 100644
--- a/drivers/scsi/scsi_multipath.c
+++ b/drivers/scsi/scsi_multipath.c
@@ -125,6 +125,15 @@ static const struct attribute_group 
scsi_mpath_device_attrs_group = {
        .attrs = scsi_mpath_device_attrs,
 };
 
+static struct attribute dummy_attr = {
+       .name = "dummy",
+};
+
+static struct attribute *scsi_mpath_attrs[] = {
+       &dummy_attr,
+       NULL
+};
+
 static bool scsi_multipath_sysfs_group_visible(struct kobject *kobj)
 {
        return true;
@@ -137,11 +146,47 @@ static bool scsi_multipath_sysfs_attr_visible(struct 
kobject *kobj,
 }
 DEFINE_SYSFS_GROUP_VISIBLE(scsi_multipath_sysfs)
 
+static const struct attribute_group scsi_mpath_attr_group = {
+       .name           = "multipath",
+       .attrs          = scsi_mpath_attrs,
+       .is_visible     = SYSFS_GROUP_VISIBLE(scsi_multipath_sysfs),
+};
+
 const struct attribute_group *scsi_mpath_device_groups[] = {
        &scsi_mpath_device_attrs_group,
+       &scsi_mpath_attr_group,
        NULL
 };
 
+void scsi_mpath_add_sysfs_link(struct scsi_device *sdev)
+{
+       struct device *target = &sdev->sdev_gendev;
+       struct scsi_mpath_head *scsi_mpath_head =
+               sdev->scsi_mpath_dev->scsi_mpath_head;
+       struct device *source = &scsi_mpath_head->dev;
+       int error;
+
+       error = sysfs_add_link_to_group(&source->kobj, "multipath",
+                       &target->kobj, dev_name(target));
+       if (error) {
+               sdev_printk(KERN_INFO, sdev, "Failed to create mpath sysfs 
link, errno=%d\n",
+                                   error);
+       }
+}
+EXPORT_SYMBOL_GPL(scsi_mpath_add_sysfs_link);
+
+void scsi_mpath_remove_sysfs_link(struct scsi_device *sdev)
+{
+       struct device *target = &sdev->sdev_gendev;
+       struct scsi_mpath_head *scsi_mpath_head =
+               sdev->scsi_mpath_dev->scsi_mpath_head;
+       struct device *source = &scsi_mpath_head->dev;
+
+       sysfs_remove_link_from_group(&source->kobj, "multipath",
+               dev_name(target));
+}
+EXPORT_SYMBOL_GPL(scsi_mpath_remove_sysfs_link);
+
 static const struct class scsi_mpath_device_class = {
        .name = "scsi_mpath_device",
        .dev_groups = scsi_mpath_device_groups,
diff --git a/drivers/scsi/scsi_sysfs.c b/drivers/scsi/scsi_sysfs.c
index 287a683e89ae5..3b03ee00c8df3 100644
--- a/drivers/scsi/scsi_sysfs.c
+++ b/drivers/scsi/scsi_sysfs.c
@@ -1389,6 +1389,9 @@ int scsi_sysfs_add_sdev(struct scsi_device *sdev)
        transport_add_device(&sdev->sdev_gendev);
        sdev->is_visible = 1;
 
+       if (sdev->scsi_mpath_dev)
+               scsi_mpath_add_sysfs_link(sdev);
+
        if (IS_ENABLED(CONFIG_BLK_DEV_BSG)) {
                sdev->bsg_dev = scsi_bsg_register_queue(sdev);
                if (IS_ERR(sdev->bsg_dev)) {
@@ -1441,6 +1444,8 @@ void __scsi_remove_device(struct scsi_device *sdev)
 
                if (IS_ENABLED(CONFIG_BLK_DEV_BSG) && sdev->bsg_dev)
                        bsg_unregister_queue(sdev->bsg_dev);
+               if (sdev->scsi_mpath_dev)
+                       scsi_mpath_remove_sysfs_link(sdev);
                device_unregister(&sdev->sdev_dev);
                transport_remove_device(dev);
                device_del(dev);
diff --git a/include/scsi/scsi_multipath.h b/include/scsi/scsi_multipath.h
index 38953b05a44dc..d8102df329d6b 100644
--- a/include/scsi/scsi_multipath.h
+++ b/include/scsi/scsi_multipath.h
@@ -46,6 +46,8 @@ void scsi_mpath_dev_release(struct scsi_device *sdev);
 int scsi_multipath_init(void);
 void scsi_multipath_exit(void);
 void scsi_mpath_remove_device(struct scsi_mpath_device *scsi_mpath_dev);
+void scsi_mpath_add_sysfs_link(struct scsi_device *sdev);
+void scsi_mpath_remove_sysfs_link(struct scsi_device *sdev);
 int scsi_mpath_get_head(struct scsi_mpath_head *);
 void scsi_mpath_put_head(struct scsi_mpath_head *);
 #else /* CONFIG_SCSI_MULTIPATH */
@@ -80,5 +82,12 @@ static inline int scsi_mpath_get_head(struct scsi_mpath_head 
*)
 static inline void scsi_mpath_put_head(struct scsi_mpath_head *)
 {
 }
+
+static inline void scsi_mpath_add_sysfs_link(struct scsi_device *sdev)
+{
+}
+static inline void scsi_mpath_remove_sysfs_link(struct scsi_device *sdev)
+{
+}
 #endif /* CONFIG_SCSI_MULTIPATH */
 #endif /* _SCSI_SCSI_MULTIPATH_H */
-- 
2.43.5


Reply via email to