From: Mengmeng Zhao <[email protected]>

Advertise FMODE_CAN_DMABUF_IO for inodes that can support dma-buf I/O
and implement ->init_dma_buf_io_ctx() by registering the buffer against
the filesystem's block device.  The capability follows
ext4_dio_alignment(), so an inode that can never do direct I/O -
fs-verity, data=journal or inline data - is refused at open, and
encrypted inodes are refused on the inode flag, which is set even
without CONFIG_FS_ENCRYPTION.  The inode is rechecked at registration
because DAX can be enabled after the file was opened.

A dma-buf iterator has no pages to copy from, so it must never reach the
page cache.  ext4_buffered_read_iter() and ext4_buffered_write_iter() are
the single sinks of the buffered fallbacks and refuse it there; a short
direct I/O write still reports its progress, and the DAX write path
refuses at its entry instead of after starting a journal handle.

Signed-off-by: Mengmeng Zhao <[email protected]>
Signed-off-by: Li Wang <[email protected]>
---
 fs/ext4/ext4.h | 11 +++++++++++
 fs/ext4/file.c | 44 +++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 52 insertions(+), 3 deletions(-)

diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index f38fec3167fb..028b2e5ddc8b 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -4135,6 +4135,17 @@ static inline bool ext4_inode_can_atomic_write(struct 
inode *inode)
                EXT4_SB(inode->i_sb)->s_awu_min > 0;
 }
 
+/*
+ * dma-buf I/O needs an inode that supports direct I/O without the page cache.
+ * IS_ENCRYPTED() tests the inode flag, which is set even without
+ * CONFIG_FS_ENCRYPTION.
+ */
+static inline bool ext4_inode_can_dmabuf_io(struct inode *inode)
+{
+       return !IS_DAX(inode) && !IS_ENCRYPTED(inode) &&
+              ext4_dio_alignment(inode) != 0;
+}
+
 extern int ext4_block_write_begin(handle_t *handle, struct folio *folio,
                                  loff_t pos, unsigned len,
                                  get_block_t *get_block);
diff --git a/fs/ext4/file.c b/fs/ext4/file.c
index 50d3c92709c8..5a8481637da5 100644
--- a/fs/ext4/file.c
+++ b/fs/ext4/file.c
@@ -30,6 +30,7 @@
 #include <linux/uio.h>
 #include <linux/mman.h>
 #include <linux/backing-dev.h>
+#include <linux/blkdev.h>
 #include "ext4.h"
 #include "ext4_jbd2.h"
 #include "xattr.h"
@@ -66,6 +67,15 @@ static bool ext4_should_use_dio(struct kiocb *iocb, struct 
iov_iter *iter)
        return IS_ALIGNED(iocb->ki_pos | iov_iter_alignment(iter), dio_align);
 }
 
+static ssize_t ext4_buffered_read_iter(struct kiocb *iocb, struct iov_iter *to)
+{
+       /* a dma-buf iterator has no pages for the page cache to copy into */
+       if (iov_iter_is_dmabuf_map(to))
+               return -EOPNOTSUPP;
+
+       return generic_file_read_iter(iocb, to);
+}
+
 static ssize_t ext4_dio_read_iter(struct kiocb *iocb, struct iov_iter *to)
 {
        ssize_t ret;
@@ -88,7 +98,7 @@ static ssize_t ext4_dio_read_iter(struct kiocb *iocb, struct 
iov_iter *to)
                 * taken.
                 */
                iocb->ki_flags &= ~IOCB_DIRECT;
-               return generic_file_read_iter(iocb, to);
+               return ext4_buffered_read_iter(iocb, to);
        }
 
        ret = iomap_dio_read_simple(iocb, to, ext4_iomap_begin);
@@ -119,7 +129,7 @@ static ssize_t ext4_dax_read_iter(struct kiocb *iocb, 
struct iov_iter *to)
        if (!IS_DAX(inode)) {
                inode_unlock_shared(inode);
                /* Fallback to buffered IO in case we cannot support DAX */
-               return generic_file_read_iter(iocb, to);
+               return ext4_buffered_read_iter(iocb, to);
        }
        ret = dax_iomap_rw(iocb, to, &ext4_iomap_ops);
        inode_unlock_shared(inode);
@@ -146,7 +156,7 @@ static ssize_t ext4_file_read_iter(struct kiocb *iocb, 
struct iov_iter *to)
        if (iocb->ki_flags & IOCB_DIRECT)
                return ext4_dio_read_iter(iocb, to);
 
-       return generic_file_read_iter(iocb, to);
+       return ext4_buffered_read_iter(iocb, to);
 }
 
 static ssize_t ext4_file_splice_read(struct file *in, loff_t *ppos,
@@ -354,6 +364,10 @@ static ssize_t ext4_buffered_write_iter(struct kiocb *iocb,
        if (iocb->ki_flags & IOCB_NOWAIT)
                return -EOPNOTSUPP;
 
+       /* a dma-buf iterator cannot be handled by buffered I/O */
+       if (iov_iter_is_dmabuf_map(from))
+               return -EOPNOTSUPP;
+
        inode_lock(inode);
 
        /*
@@ -688,6 +702,10 @@ static ssize_t ext4_dio_write_iter(struct kiocb *iocb, 
struct iov_iter *from)
                ssize_t err;
                loff_t endbyte;
 
+               /* a dma-buf iterator cannot fall back to buffered I/O */
+               if (iov_iter_is_dmabuf_map(from))
+                       return ret;
+
                /*
                 * There is no support for atomic writes on buffered-io yet,
                 * we should never fallback to buffered-io for DIO atomic
@@ -731,6 +749,10 @@ ext4_dax_write_iter(struct kiocb *iocb, struct iov_iter 
*from)
        bool extend = false;
        struct inode *inode = file_inode(iocb->ki_filp);
 
+       /* a dma-buf iterator cannot be handled by DAX either */
+       if (iov_iter_is_dmabuf_map(from))
+               return -EOPNOTSUPP;
+
        if (iocb->ki_flags & IOCB_NOWAIT) {
                if (!inode_trylock(inode))
                        return -EAGAIN;
@@ -1014,6 +1036,8 @@ static int ext4_file_open(struct inode *inode, struct 
file *filp)
                filp->f_mode |= FMODE_CAN_ATOMIC_WRITE;
 
        filp->f_mode |= FMODE_NOWAIT | FMODE_CAN_ODIRECT;
+       if (ext4_inode_can_dmabuf_io(inode))
+               filp->f_mode |= FMODE_CAN_DMABUF_IO;
        return dquot_file_open(inode, filp);
 }
 
@@ -1050,10 +1074,24 @@ loff_t ext4_llseek(struct file *file, loff_t offset, 
int whence)
        return vfs_setpos(file, offset, maxbytes);
 }
 
+static int ext4_file_init_dma_buf_io_ctx(struct file *file,
+                                        struct dma_buf_io_ctx *ctx)
+{
+       struct inode *inode = file_inode(file);
+
+       /* DAX can be enabled after the file was opened */
+       if (!ext4_inode_can_dmabuf_io(inode))
+               return -EOPNOTSUPP;
+
+       /* all file data lives on s_bdev, which never changes for ext4 */
+       return bdev_init_dma_buf_io_ctx(inode->i_sb->s_bdev, ctx);
+}
+
 const struct file_operations ext4_file_operations = {
        .llseek         = ext4_llseek,
        .read_iter      = ext4_file_read_iter,
        .write_iter     = ext4_file_write_iter,
+       .init_dma_buf_io_ctx = ext4_file_init_dma_buf_io_ctx,
        .iopoll         = iocb_bio_iopoll,
        .unlocked_ioctl = ext4_ioctl,
 #ifdef CONFIG_COMPAT
-- 
2.43.0


Reply via email to