From: Eric Biggers <[email protected]>

Convert f2fs_file_read_iter() to use iomap_dio_rw() for direct I/O
rather than using f2fs_direct_IO() via generic_file_read_iter().

Besides the new direct I/O implementation being more efficient
(especially with regards to the block mapping), this change retains the
existing f2fs behavior such as the conditions for falling back to
buffered I/O, the locking of i_gc_rwsem[READ], the iostat gathering, and
the f2fs_direct_IO_{enter,exit} tracepoints.  An exception is that we no
longer fall back to a buffered I/O read if a direct I/O read returns a
short read (previously this was done by generic_file_read_iter()), as
this doesn't appear to be a useful thing to do on f2fs.

Signed-off-by: Eric Biggers <[email protected]>
---
 fs/f2fs/f2fs.h |  7 ++---
 fs/f2fs/file.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 82 insertions(+), 9 deletions(-)

diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index d2b1ef6976c4..f869c4a2f79f 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -3243,10 +3243,9 @@ static inline void f2fs_update_iostat(struct 
f2fs_sb_info *sbi,
                        sbi->rw_iostat[APP_WRITE_IO] -
                        sbi->rw_iostat[APP_DIRECT_IO];
 
-       if (type == APP_READ_IO || type == APP_DIRECT_READ_IO)
-               sbi->rw_iostat[APP_BUFFERED_READ_IO] =
-                       sbi->rw_iostat[APP_READ_IO] -
-                       sbi->rw_iostat[APP_DIRECT_READ_IO];
+       if (type == APP_BUFFERED_READ_IO || type == APP_DIRECT_READ_IO)
+               sbi->rw_iostat[APP_READ_IO] += io_bytes;
+
        spin_unlock(&sbi->iostat_lock);
 
        f2fs_record_iostat(sbi);
diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
index 279252c7f7bc..52de655ef833 100644
--- a/fs/f2fs/file.c
+++ b/fs/f2fs/file.c
@@ -23,6 +23,7 @@
 #include <linux/nls.h>
 #include <linux/sched/signal.h>
 #include <linux/fileattr.h>
+#include <linux/iomap.h>
 
 #include "f2fs.h"
 #include "node.h"
@@ -4201,20 +4202,93 @@ long f2fs_ioctl(struct file *filp, unsigned int cmd, 
unsigned long arg)
        return __f2fs_ioctl(filp, cmd, arg);
 }
 
-static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
+/*
+ * Return %true if the given read or write request should use direct I/O, or
+ * %false if it should use buffered I/O.
+ */
+static bool f2fs_should_use_dio(struct inode *inode, struct kiocb *iocb,
+                               struct iov_iter *iter)
+{
+       unsigned int align;
+
+       if (!(iocb->ki_flags & IOCB_DIRECT))
+               return false;
+
+       if (f2fs_force_buffered_io(inode, iocb, iter))
+               return false;
+
+       /*
+        * Direct I/O not aligned to the disk's logical_block_size will be
+        * attempted, but will fail with -EINVAL.
+        *
+        * f2fs additionally requires that direct I/O be aligned to the
+        * filesystem block size, which is often a stricter requirement.
+        * However, f2fs traditionally falls back to buffered I/O on requests
+        * that are logical_block_size-aligned but not fs-block aligned.
+        *
+        * The below logic implements this behavior.
+        */
+       align = iocb->ki_pos | iov_iter_alignment(iter);
+       if (!IS_ALIGNED(align, i_blocksize(inode)) &&
+           IS_ALIGNED(align, bdev_logical_block_size(inode->i_sb->s_bdev)))
+               return false;
+
+       return true;
+}
+
+static ssize_t f2fs_dio_read_iter(struct kiocb *iocb, struct iov_iter *to)
 {
        struct file *file = iocb->ki_filp;
        struct inode *inode = file_inode(file);
-       int ret;
+       struct f2fs_inode_info *fi = F2FS_I(inode);
+       const loff_t pos = iocb->ki_pos;
+       const size_t count = iov_iter_count(to);
+       ssize_t ret;
+
+       if (count == 0)
+               return 0; /* skip atime update */
+
+       trace_f2fs_direct_IO_enter(inode, pos, count, READ);
+
+       if (iocb->ki_flags & IOCB_NOWAIT) {
+               if (!down_read_trylock(&fi->i_gc_rwsem[READ])) {
+                       ret = -EAGAIN;
+                       goto out;
+               }
+       } else {
+               down_read(&fi->i_gc_rwsem[READ]);
+       }
+
+       ret = iomap_dio_rw(iocb, to, &f2fs_iomap_ops, &f2fs_iomap_dio_ops, 0);
+
+       up_read(&fi->i_gc_rwsem[READ]);
+
+       file_accessed(file);
+
+       if (ret > 0)
+               f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_READ_IO, ret);
+       else if (ret == -EIOCBQUEUED)
+               f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_READ_IO,
+                                  count - iov_iter_count(to));
+out:
+       trace_f2fs_direct_IO_exit(inode, pos, count, READ, ret);
+       return ret;
+}
+
+static ssize_t f2fs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
+{
+       struct inode *inode = file_inode(iocb->ki_filp);
+       ssize_t ret;
 
        if (!f2fs_is_compress_backend_ready(inode))
                return -EOPNOTSUPP;
 
-       ret = generic_file_read_iter(iocb, iter);
+       if (f2fs_should_use_dio(inode, iocb, to))
+               return f2fs_dio_read_iter(iocb, to);
 
+       ret = filemap_read(iocb, to, 0);
        if (ret > 0)
-               f2fs_update_iostat(F2FS_I_SB(inode), APP_READ_IO, ret);
-
+               f2fs_update_iostat(F2FS_I_SB(inode), APP_BUFFERED_READ_IO, ret);
        return ret;
 }
 
-- 
2.32.0



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

Reply via email to