Add support to allocate and free a multipath gendisk.

NVMe has almost like-for-like equivalents here:
- mpath_alloc_head_disk() -> nvme_mpath_alloc_disk()
- multipath_partition_scan_work() -> nvme_partition_scan_work()
- mpath_remove_disk() -> nvme_remove_head()
- mpath_device_set_live() -> nvme_mpath_set_live()

struct mpath_head_template is introduced as a method for drivers to
provide custom multipath functionality.

Signed-off-by: John Garry <[email protected]>
---
 include/linux/multipath.h | 38 ++++++++++++++++
 lib/multipath.c           | 96 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 134 insertions(+)

diff --git a/include/linux/multipath.h b/include/linux/multipath.h
index 102dfcf21ffc9..3e2a513059cde 100644
--- a/include/linux/multipath.h
+++ b/include/linux/multipath.h
@@ -5,11 +5,19 @@
 #include <linux/blkdev.h>
 #include <linux/srcu.h>
 
+extern const struct block_device_operations mpath_ops;
+
 struct mpath_device {
+       struct mpath_head       *mpath_head;
        struct list_head        siblings;
        struct gendisk          *disk;
 };
 
+struct mpath_head_template {
+};
+
+#define MPATH_HEAD_DISK_LIVE                   0
+
 struct mpath_head {
        struct srcu_struct      srcu;
        struct list_head        dev_list;       /* list of all mpath_devs */
@@ -18,11 +26,41 @@ struct mpath_head {
        struct kref             ref;
 
        void                    *drvdata;
+       unsigned long           flags;
+       struct gendisk          *disk;
+       struct work_struct      partition_scan_work;
+       struct device           *parent;
+       const struct attribute_group            **disk_groups;
+       const struct mpath_head_template        *mpdt;
        struct mpath_device __rcu               *current_path[];
 };
 
+static inline struct mpath_head *mpath_bd_device_to_head(struct device *dev)
+{
+       return dev_get_drvdata(dev);
+}
+
+static inline struct mpath_head *mpath_gendisk_to_head(struct gendisk *disk)
+{
+       return mpath_bd_device_to_head(disk_to_dev(disk));
+}
+
 int mpath_get_head(struct mpath_head *mpath_head);
 void mpath_put_head(struct mpath_head *mpath_head);
 struct mpath_head *mpath_alloc_head(void);
 
+void mpath_put_disk(struct mpath_head *mpath_head);
+void mpath_remove_disk(struct mpath_head *mpath_head);
+int mpath_alloc_head_disk(struct mpath_head *mpath_head,
+                       struct queue_limits *lim, int numa_node);
+void mpath_device_set_live(struct mpath_device *mpath_device);
+
+static inline bool is_mpath_disk(struct gendisk *disk)
+{
+       #if IS_ENABLED(CONFIG_LIBMULTIPATH)
+       return disk->fops == &mpath_ops;
+       #else
+       return false;
+       #endif
+}
 #endif // _LIBMULTIPATH_H
diff --git a/lib/multipath.c b/lib/multipath.c
index 0d5690b5ba4ca..726d9bec13553 100644
--- a/lib/multipath.c
+++ b/lib/multipath.c
@@ -31,6 +31,99 @@ void mpath_put_head(struct mpath_head *mpath_head)
 }
 EXPORT_SYMBOL_GPL(mpath_put_head);
 
+static int mpath_bdev_open(struct gendisk *disk, blk_mode_t mode)
+{
+       struct mpath_head *mpath_head = disk->private_data;
+
+       return mpath_get_head(mpath_head);
+}
+
+static void mpath_bdev_release(struct gendisk *disk)
+{
+       struct mpath_head *mpath_head = disk->private_data;
+
+       mpath_put_head(mpath_head);
+}
+
+const struct block_device_operations mpath_ops = {
+       .owner          = THIS_MODULE,
+       .open           = mpath_bdev_open,
+       .release        = mpath_bdev_release,
+};
+EXPORT_SYMBOL_GPL(mpath_ops);
+
+static void multipath_partition_scan_work(struct work_struct *work)
+{
+       struct mpath_head *mpath_head =
+               container_of(work, struct mpath_head, partition_scan_work);
+
+       if (WARN_ON_ONCE(!test_and_clear_bit(GD_SUPPRESS_PART_SCAN,
+                                            &mpath_head->disk->state)))
+               return;
+
+       mutex_lock(&mpath_head->disk->open_mutex);
+       bdev_disk_changed(mpath_head->disk, false);
+       mutex_unlock(&mpath_head->disk->open_mutex);
+}
+
+void mpath_remove_disk(struct mpath_head *mpath_head)
+{
+       if (test_and_clear_bit(MPATH_HEAD_DISK_LIVE, &mpath_head->flags)) {
+               struct gendisk *disk = mpath_head->disk;
+
+               del_gendisk(disk);
+       }
+}
+EXPORT_SYMBOL_GPL(mpath_remove_disk);
+
+void mpath_put_disk(struct mpath_head *mpath_head)
+{
+       if (!mpath_head->disk)
+               return;
+
+       /* make sure all pending bios are cleaned up */
+       flush_work(&mpath_head->partition_scan_work);
+       put_disk(mpath_head->disk);
+}
+EXPORT_SYMBOL_GPL(mpath_put_disk);
+
+int mpath_alloc_head_disk(struct mpath_head *mpath_head,
+                       struct queue_limits *lim, int numa_node)
+{
+       mpath_head->disk = blk_alloc_disk(lim, numa_node);
+       if (IS_ERR(mpath_head->disk))
+               return PTR_ERR(mpath_head->disk);
+
+       mpath_head->disk->private_data = mpath_head;
+       mpath_head->disk->fops = &mpath_ops;
+
+       set_bit(GD_SUPPRESS_PART_SCAN, &mpath_head->disk->state);
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(mpath_alloc_head_disk);
+
+void mpath_device_set_live(struct mpath_device *mpath_device)
+{
+       struct mpath_head *mpath_head = mpath_device->mpath_head;
+       int ret;
+
+       if (!mpath_head->disk)
+               return;
+
+       if (!test_and_set_bit(MPATH_HEAD_DISK_LIVE, &mpath_head->flags)) {
+               dev_set_drvdata(disk_to_dev(mpath_head->disk), mpath_head);
+               ret = device_add_disk(mpath_head->parent, mpath_head->disk,
+                               mpath_head->disk_groups);
+               if (ret) {
+                       clear_bit(MPATH_HEAD_DISK_LIVE, &mpath_head->flags);
+                       return;
+               }
+               queue_work(mpath_wq, &mpath_head->partition_scan_work);
+       }
+}
+EXPORT_SYMBOL_GPL(mpath_device_set_live);
+
 struct mpath_head *mpath_alloc_head(void)
 {
        struct mpath_head *mpath_head;
@@ -44,6 +137,9 @@ struct mpath_head *mpath_alloc_head(void)
        mutex_init(&mpath_head->lock);
        kref_init(&mpath_head->ref);
 
+       INIT_WORK(&mpath_head->partition_scan_work,
+               multipath_partition_scan_work);
+
        ret = init_srcu_struct(&mpath_head->srcu);
        if (ret) {
                kfree(mpath_head);
-- 
2.43.5


Reply via email to