Skip to content

Commit b1b4705

Browse files
matthewbobrowskitytso
authored andcommitted
ext4: introduce direct I/O read using iomap infrastructure
This patch introduces a new direct I/O read path which makes use of the iomap infrastructure. The new function ext4_do_read_iter() is responsible for calling into the iomap infrastructure via iomap_dio_rw(). If the read operation performed on the inode is not supported, which is checked via ext4_dio_supported(), then we simply fallback and complete the I/O using buffered I/O. Existing direct I/O read code path has been removed, as it is now redundant. Signed-off-by: Matthew Bobrowski <[email protected]> Reviewed-by: Jan Kara <[email protected]> Reviewed-by: Ritesh Harjani <[email protected]> Link: https://lore.kernel.org/r/f98a6f73fadddbfbad0fc5ed04f712ca0b799f37.1572949325.git.mbobrowski@mbobrowski.org Signed-off-by: Theodore Ts'o <[email protected]>
1 parent 09edf4d commit b1b4705

File tree

2 files changed

+54
-39
lines changed

2 files changed

+54
-39
lines changed

fs/ext4/file.c

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,52 @@
3434
#include "xattr.h"
3535
#include "acl.h"
3636

37+
static bool ext4_dio_supported(struct inode *inode)
38+
{
39+
if (IS_ENABLED(CONFIG_FS_ENCRYPTION) && IS_ENCRYPTED(inode))
40+
return false;
41+
if (fsverity_active(inode))
42+
return false;
43+
if (ext4_should_journal_data(inode))
44+
return false;
45+
if (ext4_has_inline_data(inode))
46+
return false;
47+
return true;
48+
}
49+
50+
static ssize_t ext4_dio_read_iter(struct kiocb *iocb, struct iov_iter *to)
51+
{
52+
ssize_t ret;
53+
struct inode *inode = file_inode(iocb->ki_filp);
54+
55+
if (iocb->ki_flags & IOCB_NOWAIT) {
56+
if (!inode_trylock_shared(inode))
57+
return -EAGAIN;
58+
} else {
59+
inode_lock_shared(inode);
60+
}
61+
62+
if (!ext4_dio_supported(inode)) {
63+
inode_unlock_shared(inode);
64+
/*
65+
* Fallback to buffered I/O if the operation being performed on
66+
* the inode is not supported by direct I/O. The IOCB_DIRECT
67+
* flag needs to be cleared here in order to ensure that the
68+
* direct I/O path within generic_file_read_iter() is not
69+
* taken.
70+
*/
71+
iocb->ki_flags &= ~IOCB_DIRECT;
72+
return generic_file_read_iter(iocb, to);
73+
}
74+
75+
ret = iomap_dio_rw(iocb, to, &ext4_iomap_ops, NULL,
76+
is_sync_kiocb(iocb));
77+
inode_unlock_shared(inode);
78+
79+
file_accessed(iocb->ki_filp);
80+
return ret;
81+
}
82+
3783
#ifdef CONFIG_FS_DAX
3884
static ssize_t ext4_dax_read_iter(struct kiocb *iocb, struct iov_iter *to)
3985
{
@@ -64,16 +110,21 @@ static ssize_t ext4_dax_read_iter(struct kiocb *iocb, struct iov_iter *to)
64110

65111
static ssize_t ext4_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
66112
{
67-
if (unlikely(ext4_forced_shutdown(EXT4_SB(file_inode(iocb->ki_filp)->i_sb))))
113+
struct inode *inode = file_inode(iocb->ki_filp);
114+
115+
if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
68116
return -EIO;
69117

70118
if (!iov_iter_count(to))
71119
return 0; /* skip atime */
72120

73121
#ifdef CONFIG_FS_DAX
74-
if (IS_DAX(file_inode(iocb->ki_filp)))
122+
if (IS_DAX(inode))
75123
return ext4_dax_read_iter(iocb, to);
76124
#endif
125+
if (iocb->ki_flags & IOCB_DIRECT)
126+
return ext4_dio_read_iter(iocb, to);
127+
77128
return generic_file_read_iter(iocb, to);
78129
}
79130

fs/ext4/inode.c

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -863,9 +863,6 @@ int ext4_dio_get_block(struct inode *inode, sector_t iblock,
863863
{
864864
/* We don't expect handle for direct IO */
865865
WARN_ON_ONCE(ext4_journal_current_handle());
866-
867-
if (!create)
868-
return _ext4_get_block(inode, iblock, bh, 0);
869866
return ext4_get_block_trans(inode, iblock, bh, EXT4_GET_BLOCKS_CREATE);
870867
}
871868

@@ -3916,36 +3913,6 @@ static ssize_t ext4_direct_IO_write(struct kiocb *iocb, struct iov_iter *iter)
39163913
return ret;
39173914
}
39183915

3919-
static ssize_t ext4_direct_IO_read(struct kiocb *iocb, struct iov_iter *iter)
3920-
{
3921-
struct address_space *mapping = iocb->ki_filp->f_mapping;
3922-
struct inode *inode = mapping->host;
3923-
size_t count = iov_iter_count(iter);
3924-
ssize_t ret;
3925-
3926-
/*
3927-
* Shared inode_lock is enough for us - it protects against concurrent
3928-
* writes & truncates and since we take care of writing back page cache,
3929-
* we are protected against page writeback as well.
3930-
*/
3931-
if (iocb->ki_flags & IOCB_NOWAIT) {
3932-
if (!inode_trylock_shared(inode))
3933-
return -EAGAIN;
3934-
} else {
3935-
inode_lock_shared(inode);
3936-
}
3937-
3938-
ret = filemap_write_and_wait_range(mapping, iocb->ki_pos,
3939-
iocb->ki_pos + count - 1);
3940-
if (ret)
3941-
goto out_unlock;
3942-
ret = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev,
3943-
iter, ext4_dio_get_block, NULL, NULL, 0);
3944-
out_unlock:
3945-
inode_unlock_shared(inode);
3946-
return ret;
3947-
}
3948-
39493916
static ssize_t ext4_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
39503917
{
39513918
struct file *file = iocb->ki_filp;
@@ -3972,10 +3939,7 @@ static ssize_t ext4_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
39723939
return 0;
39733940

39743941
trace_ext4_direct_IO_enter(inode, offset, count, iov_iter_rw(iter));
3975-
if (iov_iter_rw(iter) == READ)
3976-
ret = ext4_direct_IO_read(iocb, iter);
3977-
else
3978-
ret = ext4_direct_IO_write(iocb, iter);
3942+
ret = ext4_direct_IO_write(iocb, iter);
39793943
trace_ext4_direct_IO_exit(inode, offset, count, iov_iter_rw(iter), ret);
39803944
return ret;
39813945
}

0 commit comments

Comments
 (0)