1 ext4: convert DAX reads to iomap infrastructure
3 From: Jan Kara <jack@suse.cz>
5 Implement basic iomap_begin function that handles reading and use it for
8 Reviewed-by: Ross Zwisler <ross.zwisler@linux.intel.com>
9 Signed-off-by: Jan Kara <jack@suse.cz>
10 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
13 fs/ext4/file.c | 38 +++++++++++++++++++++++++++++++++++++-
14 fs/ext4/inode.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
15 3 files changed, 93 insertions(+), 1 deletion(-)
17 diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
18 index 282a51b07c57..098b39910001 100644
21 @@ -3271,6 +3271,8 @@ static inline bool ext4_aligned_io(struct inode *inode, loff_t off, loff_t len)
22 return IS_ALIGNED(off, blksize) && IS_ALIGNED(len, blksize);
25 +extern struct iomap_ops ext4_iomap_ops;
27 #endif /* __KERNEL__ */
29 #define EFSBADCRC EBADMSG /* Bad CRC detected */
30 diff --git a/fs/ext4/file.c b/fs/ext4/file.c
31 index 9facb4dc5c70..1f25c644cb12 100644
39 +static ssize_t ext4_dax_read_iter(struct kiocb *iocb, struct iov_iter *to)
41 + struct inode *inode = file_inode(iocb->ki_filp);
44 + inode_lock_shared(inode);
46 + * Recheck under inode lock - at this point we are sure it cannot
49 + if (!IS_DAX(inode)) {
50 + inode_unlock_shared(inode);
51 + /* Fallback to buffered IO in case we cannot support DAX */
52 + return generic_file_read_iter(iocb, to);
54 + ret = dax_iomap_rw(iocb, to, &ext4_iomap_ops);
55 + inode_unlock_shared(inode);
57 + file_accessed(iocb->ki_filp);
62 +static ssize_t ext4_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
64 + if (!iov_iter_count(to))
65 + return 0; /* skip atime */
68 + if (IS_DAX(file_inode(iocb->ki_filp)))
69 + return ext4_dax_read_iter(iocb, to);
71 + return generic_file_read_iter(iocb, to);
75 * Called when an inode is released. Note that this is different
76 * from ext4_file_open: open gets called at every open, but release
77 @@ -690,7 +726,7 @@ loff_t ext4_llseek(struct file *file, loff_t offset, int whence)
79 const struct file_operations ext4_file_operations = {
80 .llseek = ext4_llseek,
81 - .read_iter = generic_file_read_iter,
82 + .read_iter = ext4_file_read_iter,
83 .write_iter = ext4_file_write_iter,
84 .unlocked_ioctl = ext4_ioctl,
86 diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
87 index 5337828c68a7..83e8411370d3 100644
91 #include <linux/printk.h>
92 #include <linux/slab.h>
93 #include <linux/bitops.h>
94 +#include <linux/iomap.h>
96 #include "ext4_jbd2.h"
98 @@ -3310,6 +3311,59 @@ int ext4_dax_get_block(struct inode *inode, sector_t iblock,
99 clear_buffer_new(bh_result);
103 +static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
104 + unsigned flags, struct iomap *iomap)
106 + unsigned int blkbits = inode->i_blkbits;
107 + unsigned long first_block = offset >> blkbits;
108 + unsigned long last_block = (offset + length - 1) >> blkbits;
109 + struct ext4_map_blocks map;
112 + if (flags & IOMAP_WRITE)
115 + if (WARN_ON_ONCE(ext4_has_inline_data(inode)))
118 + map.m_lblk = first_block;
119 + map.m_len = last_block - first_block + 1;
121 + ret = ext4_map_blocks(NULL, inode, &map, 0);
126 + iomap->bdev = inode->i_sb->s_bdev;
127 + iomap->offset = first_block << blkbits;
130 + iomap->type = IOMAP_HOLE;
131 + iomap->blkno = IOMAP_NULL_BLOCK;
132 + iomap->length = (u64)map.m_len << blkbits;
134 + if (map.m_flags & EXT4_MAP_MAPPED) {
135 + iomap->type = IOMAP_MAPPED;
136 + } else if (map.m_flags & EXT4_MAP_UNWRITTEN) {
137 + iomap->type = IOMAP_UNWRITTEN;
142 + iomap->blkno = (sector_t)map.m_pblk << (blkbits - 9);
143 + iomap->length = (u64)map.m_len << blkbits;
146 + if (map.m_flags & EXT4_MAP_NEW)
147 + iomap->flags |= IOMAP_F_NEW;
151 +struct iomap_ops ext4_iomap_ops = {
152 + .iomap_begin = ext4_iomap_begin,
156 /* Just define empty function, it will never get called. */
157 int ext4_dax_get_block(struct inode *inode, sector_t iblock,