[RFC PATCH v2 1/8] block: factor out bdev_init_dma_buf_io_ctx()

From: Mengmeng Zhao

Date: Fri Nov 13 2026 - 06:04:37 EST


From: Nitesh Shetty <nj.shetty@xxxxxxxxxxx>

Factor the checks and the driver setup out of
blkdev_init_dma_buf_io_ctx() into an exported helper that takes the
block device as an argument, so callers can set up a dma-buf I/O
context for any block device. The block device file operation becomes
a thin wrapper using file_bdev(), so its behavior is unchanged.

Signed-off-by: Nitesh Shetty <nj.shetty@xxxxxxxxxxx>
Signed-off-by: Mengmeng Zhao <zhaomengmeng@xxxxxxxxxx>
Signed-off-by: Li Wang <liwang@xxxxxxxxxx>
---
block/bdev.c | 14 ++++++++++++++
block/fops.c | 12 +-----------
include/linux/blkdev.h | 3 +++
3 files changed, 18 insertions(+), 11 deletions(-)

diff --git a/block/bdev.c b/block/bdev.c
index f20f91b3176e..88e3f552f5d4 100644
--- a/block/bdev.c
+++ b/block/bdev.c
@@ -17,6 +17,7 @@
#include <linux/blkpg.h>
#include <linux/magic.h>
#include <linux/buffer_head.h>
+#include <linux/dma-buf-io.h>
#include <linux/swap.h>
#include <linux/writeback.h>
#include <linux/mount.h>
@@ -60,6 +61,19 @@ struct block_device *file_bdev(struct file *bdev_file)
}
EXPORT_SYMBOL(file_bdev);

+int bdev_init_dma_buf_io_ctx(struct file *file, struct block_device *bdev,
+ struct dma_buf_io_ctx *ctx)
+{
+ if (!(file->f_flags & O_DIRECT))
+ return -EINVAL;
+ if (ctx->dmabuf->size > UINT_MAX)
+ return -EINVAL;
+ if (!bdev->bd_disk->fops->init_dma_buf_io_ctx)
+ return -EOPNOTSUPP;
+ return bdev->bd_disk->fops->init_dma_buf_io_ctx(bdev, ctx);
+}
+EXPORT_SYMBOL_GPL(bdev_init_dma_buf_io_ctx);
+
static void bdev_write_inode(struct block_device *bdev)
{
struct inode *inode = BD_INODE(bdev);
diff --git a/block/fops.c b/block/fops.c
index a0ccadaa9c79..86bf1f298b79 100644
--- a/block/fops.c
+++ b/block/fops.c
@@ -19,7 +19,6 @@
#include <linux/iomap.h>
#include <linux/module.h>
#include <linux/io_uring/cmd.h>
-#include <linux/dma-buf-io.h>
#include "blk.h"

static inline struct inode *bdev_file_inode(struct file *file)
@@ -1009,16 +1008,7 @@ static int blkdev_mmap_prepare(struct vm_area_desc *desc)
static int blkdev_init_dma_buf_io_ctx(struct file *file,
struct dma_buf_io_ctx *ctx)
{
- struct block_device *bdev = file_bdev(file);
- struct gendisk *disk = bdev->bd_disk;
-
- if (!(file->f_flags & O_DIRECT))
- return -EINVAL;
- if (ctx->dmabuf->size > UINT_MAX)
- return -EINVAL;
- if (!disk->fops->init_dma_buf_io_ctx)
- return -EOPNOTSUPP;
- return disk->fops->init_dma_buf_io_ctx(bdev, ctx);
+ return bdev_init_dma_buf_io_ctx(file, file_bdev(file), ctx);
}

const struct file_operations def_blk_fops = {
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index 215753171cef..b98f43478cc0 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -1610,6 +1610,9 @@ struct block_device_operations {
int (*alternative_gpt_sector)(struct gendisk *disk, sector_t *sector);
};

+int bdev_init_dma_buf_io_ctx(struct file *file, struct block_device *bdev,
+ struct dma_buf_io_ctx *ctx);
+
#ifdef CONFIG_COMPAT
extern int blkdev_compat_ptr_ioctl(struct block_device *, blk_mode_t,
unsigned int, unsigned long);
--
2.43.0