[PATCH 11/13] libmultipath: Add support for block device IOCTL
From: John Garry
Date: Wed Feb 25 2026 - 10:40:37 EST
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@xxxxxxxxxx>
---
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