Move the O_DIRECT check from blkdev_init_dma_buf_io_ctx() into
dma_buf_io_ctx_create(), so it covers every file type.
Also refuse files without a tag, as the context could not be used
with them.

Signed-off-by: Anuj Gupta <[email protected]>
Signed-off-by: Nitesh Shetty <[email protected]>
---
 block/fops.c                 |  2 --
 drivers/dma-buf/dma-buf-io.c | 16 +++++++++++++---
 2 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/block/fops.c b/block/fops.c
index f723220194a4..5c38db558f2d 100644
--- a/block/fops.c
+++ b/block/fops.c
@@ -1004,8 +1004,6 @@ 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)
 {
-       if (!(file->f_flags & O_DIRECT))
-               return -EINVAL;
        return bdev_init_dma_buf_io_ctx(file_bdev(file), ctx);
 }
 
diff --git a/drivers/dma-buf/dma-buf-io.c b/drivers/dma-buf/dma-buf-io.c
index 8440151de860..020a998e305f 100644
--- a/drivers/dma-buf/dma-buf-io.c
+++ b/drivers/dma-buf/dma-buf-io.c
@@ -152,9 +152,9 @@ static void dma_buf_io_ctx_shutdown(struct dma_buf_io_ctx 
*ctx)
  * @out_ctx:   [out]   resulting ctx
  *
  * Create a new I/O context from the given DMA buffer for the given file.
- * It only works with files implementing ->init_dma_buf_io_ctx and fails
- * with -EOPNOTSUPP otherwise. Once created, the user can create a map
- * and use it to issue I/O, such as read/write requests, to @file.
+ * The file must be opened with O_DIRECT and have a non-zero dma-buf I/O
+ * tag. Once created, the user can create a map and use it to issue I/O to
+ * any file with the same tag.
  */
 int dma_buf_io_ctx_create(struct file *file,
                           struct dma_buf *dmabuf,
@@ -166,6 +166,10 @@ int dma_buf_io_ctx_create(struct file *file,
 
        if (!file->f_op->init_dma_buf_io_ctx)
                return -EOPNOTSUPP;
+       if (!(file->f_flags & O_DIRECT))
+               return -EINVAL;
+       if (!dma_buf_io_file_tag(file))
+               return -EOPNOTSUPP;
 
        ctx = kzalloc_obj(*ctx);
        if (!ctx)
@@ -186,6 +190,12 @@ int dma_buf_io_ctx_create(struct file *file,
                         !ctx->dev_ops->unmap ||
                         !ctx->dev_ops->release))
                return -EINVAL;
+       if (WARN_ON_ONCE(!ctx->importer_tag)) {
+               ctx->dev_ops->release(ctx);
+               dma_buf_put(dmabuf);
+               kfree(ctx);
+               return -EINVAL;
+       }
 
        *out_ctx = ctx;
        return 0;
-- 
2.47.3

Reply via email to