Move the block device setup into bdev_init_dma_buf_io_ctx(), so file
systems can use it for the device backing a file.

Signed-off-by: Anuj Gupta <[email protected]>
Signed-off-by: Nitesh Shetty <[email protected]>
---
 block/bdev.c           | 24 ++++++++++++++++++++++++
 block/fops.c           | 10 +---------
 include/linux/blkdev.h |  3 +++
 3 files changed, 28 insertions(+), 9 deletions(-)

diff --git a/block/bdev.c b/block/bdev.c
index f20f91b3176e..a949b3d0d97e 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,29 @@ struct block_device *file_bdev(struct file *bdev_file)
 }
 EXPORT_SYMBOL(file_bdev);
 
+/**
+ * bdev_init_dma_buf_io_ctx - set up a dma-buf I/O context for a block device
+ * @bdev:      block device the context is set up for
+ * @ctx:       context to initialise
+ *
+ * Used by block device files, and by file systems for the device holding
+ * a file's data.
+ *
+ * Return: On success zero is returned, negative error code on failure.
+ */
+int bdev_init_dma_buf_io_ctx(struct block_device *bdev,
+                            struct dma_buf_io_ctx *ctx)
+{
+       struct gendisk *disk = bdev->bd_disk;
+
+       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);
+}
+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 084379778413..f017bd1e01b2 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)
@@ -1005,16 +1004,9 @@ 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_bdev(file), ctx);
 }
 
 const struct file_operations def_blk_fops = {
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index 63c6e43ecdda..8b69a66b67df 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -1674,6 +1674,9 @@ struct block_device_operations {
        int (*alternative_gpt_sector)(struct gendisk *disk, sector_t *sector);
 };
 
+int bdev_init_dma_buf_io_ctx(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.47.3

Reply via email to