[PATCH 11/13] libmultipath: Add support for block device IOCTL

John Garry posted 13 patches 1 month, 1 week ago
[PATCH 11/13] libmultipath: Add support for block device IOCTL
Posted by John Garry 1 month, 1 week ago
Add mpath_bdev_ioctl() as a multipath block device IOCTL handler. This
handler calls into driver mpath_head_template.ioctl handler.

It is expected that the .ioctl handler will unlock the SRCU read lock,
as this is what NVMe requires - see nvme_ns_head_ctrl_ioctl(). As such,
export a handler to unlock, mpath_head_read_unlock().

The .compat_ioctl handler is given the standard handler.

Signed-off-by: John Garry <john.g.garry@oracle.com>
---
 include/linux/multipath.h |  4 ++++
 lib/multipath.c           | 42 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+)

diff --git a/include/linux/multipath.h b/include/linux/multipath.h
index 3846ea8cfd319..40dda6a914c5f 100644
--- a/include/linux/multipath.h
+++ b/include/linux/multipath.h
@@ -72,6 +72,9 @@ struct mpath_head_template {
 	bool (*is_disabled)(struct mpath_device *);
 	bool (*is_optimized)(struct mpath_device *);
 	enum mpath_access_state (*get_access_state)(struct mpath_device *);
+	int (*bdev_ioctl)(struct block_device *bdev, struct mpath_device *,
+			blk_mode_t mode, unsigned int cmd, unsigned long arg,
+			int srcu_idx);
 	int (*cdev_ioctl)(struct mpath_head *, struct mpath_device *,
 			blk_mode_t mode, unsigned int cmd, unsigned long arg, int srcu_idx);
 	int (*report_zones)(struct mpath_device *, sector_t sector,
@@ -154,6 +157,7 @@ void mpath_revalidate_paths(struct mpath_disk *mpath_disk,
 void mpath_add_sysfs_link(struct mpath_disk *mpath_disk);
 void mpath_remove_sysfs_link(struct mpath_disk *mpath_disk,
 				struct mpath_device *mpath_device);
+void mpath_head_read_unlock(struct mpath_head *mpath_head, int srcu_idx);
 int mpath_get_head(struct mpath_head *mpath_head);
 void mpath_put_head(struct mpath_head *mpath_head);
 void mpath_requeue_work(struct work_struct *work);
diff --git a/lib/multipath.c b/lib/multipath.c
index 4c57feefff480..537579ad5989e 100644
--- a/lib/multipath.c
+++ b/lib/multipath.c
@@ -496,6 +496,46 @@ static void mpath_bdev_release(struct gendisk *disk)
 	mpath_put_disk(mpath_disk);
 }
 
+static int mpath_bdev_ioctl(struct block_device *bdev, blk_mode_t mode,
+		    unsigned int cmd, unsigned long arg)
+{
+	struct gendisk *disk = bdev->bd_disk;
+	struct mpath_disk *mpath_disk = mpath_gendisk_to_disk(disk);
+	struct mpath_head *mpath_head = mpath_disk->mpath_head;
+	struct mpath_device *mpath_device;
+	int srcu_idx, err;
+
+	srcu_idx = srcu_read_lock(&mpath_head->srcu);
+	mpath_device = mpath_find_path(mpath_head);
+
+	if (!mpath_device) {
+		err = -EWOULDBLOCK;
+		goto out_unlock;
+	}
+
+	if (bdev_is_partition(bdev) && !capable(CAP_SYS_RAWIO)) {
+		err = -ENOIOCTLCMD;
+		goto out_unlock;
+	}
+
+	/* ->ioctl must always unlock */
+	err = mpath_head->mpdt->bdev_ioctl(bdev, mpath_device, mode, cmd,
+				arg, srcu_idx);
+	lockdep_assert_not_held(&mpath_head->srcu);
+	return err;
+
+out_unlock:
+	srcu_read_unlock(&mpath_head->srcu, srcu_idx);
+	return err;
+}
+
+void mpath_head_read_unlock(struct mpath_head *mpath_head, int srcu_idx)
+__releases(&mpath_head->srcu)
+{
+	srcu_read_unlock(&mpath_head->srcu, srcu_idx);
+}
+EXPORT_SYMBOL_GPL(mpath_head_read_unlock);
+
 static int mpath_pr_register(struct block_device *bdev, u64 old_key,
 			u64 new_key, unsigned int flags)
 {
@@ -646,6 +686,8 @@ const struct block_device_operations mpath_ops = {
 	.open		= mpath_bdev_open,
 	.release	= mpath_bdev_release,
 	.submit_bio	= mpath_bdev_submit_bio,
+	.ioctl		= mpath_bdev_ioctl,
+	.compat_ioctl	= blkdev_compat_ptr_ioctl,
 	.report_zones	= mpath_bdev_report_zones,
 	.pr_ops		= &mpath_pr_ops,
 };
-- 
2.43.5
Re: [PATCH 11/13] libmultipath: Add support for block device IOCTL
Posted by Benjamin Marzinski 1 month, 1 week ago
On Wed, Feb 25, 2026 at 03:32:23PM +0000, John Garry wrote:
> Add mpath_bdev_ioctl() as a multipath block device IOCTL handler. This
> handler calls into driver mpath_head_template.ioctl handler.
> 
> It is expected that the .ioctl handler will unlock the SRCU read lock,
> as this is what NVMe requires - see nvme_ns_head_ctrl_ioctl(). As such,
> export a handler to unlock, mpath_head_read_unlock().
> 
> The .compat_ioctl handler is given the standard handler.
> 
> Signed-off-by: John Garry <john.g.garry@oracle.com>
> ---
>  include/linux/multipath.h |  4 ++++
>  lib/multipath.c           | 42 +++++++++++++++++++++++++++++++++++++++
>  2 files changed, 46 insertions(+)
> 
> diff --git a/include/linux/multipath.h b/include/linux/multipath.h
> index 3846ea8cfd319..40dda6a914c5f 100644
> --- a/include/linux/multipath.h
> +++ b/include/linux/multipath.h
> @@ -72,6 +72,9 @@ struct mpath_head_template {
>  	bool (*is_disabled)(struct mpath_device *);
>  	bool (*is_optimized)(struct mpath_device *);
>  	enum mpath_access_state (*get_access_state)(struct mpath_device *);
> +	int (*bdev_ioctl)(struct block_device *bdev, struct mpath_device *,
> +			blk_mode_t mode, unsigned int cmd, unsigned long arg,
> +			int srcu_idx);

I don't know that this API is going to work out. SCSI persistent
reservations need access to all the mpath_devices, not just one, and
they are commonly handled via SG_IO ioctls. Unless you want to disallow
SCSI persistent reservations via SG_IO, you need to be able to detect
them, and handle them using the persistent reservation code with the
mpath_head.

-Ben
Re: [PATCH 11/13] libmultipath: Add support for block device IOCTL
Posted by John Garry 1 month ago
On 27/02/2026 19:52, Benjamin Marzinski wrote:
>> diff --git a/include/linux/multipath.h b/include/linux/multipath.h
>> index 3846ea8cfd319..40dda6a914c5f 100644
>> --- a/include/linux/multipath.h
>> +++ b/include/linux/multipath.h
>> @@ -72,6 +72,9 @@ struct mpath_head_template {
>>   	bool (*is_disabled)(struct mpath_device *);
>>   	bool (*is_optimized)(struct mpath_device *);
>>   	enum mpath_access_state (*get_access_state)(struct mpath_device *);
>> +	int (*bdev_ioctl)(struct block_device *bdev, struct mpath_device *,
>> +			blk_mode_t mode, unsigned int cmd, unsigned long arg,
>> +			int srcu_idx);
> I don't know that this API is going to work out. SCSI persistent
> reservations need access to all the mpath_devices, not just one, and
> they are commonly handled via SG_IO ioctls. Unless you want to disallow
> SCSI persistent reservations via SG_IO,

I want to make the multipathed /dev/sdX behave same as non-multipathed, 
so should then support it.

> you need to be able to detect
> them, and handle them using the persistent reservation code with the
> mpath_head.

Understood, I am going to check this PR handling further.

thanks!