add patch fix-checks-for-data-ordered-and-journal_async_commit-options
[ext4-patch-queue.git] / convert-dax-reads-to-iomap-infrastructure
blob2ae825a6ca6aec3665447489e323cc05f5edae9d
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
6 DAX reads.
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>
11 ---
12  fs/ext4/ext4.h  |  2 ++
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
19 --- a/fs/ext4/ext4.h
20 +++ b/fs/ext4/ext4.h
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);
23  }
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
32 --- a/fs/ext4/file.c
33 +++ b/fs/ext4/file.c
34 @@ -31,6 +31,42 @@
35  #include "xattr.h"
36  #include "acl.h"
38 +#ifdef CONFIG_FS_DAX
39 +static ssize_t ext4_dax_read_iter(struct kiocb *iocb, struct iov_iter *to)
41 +       struct inode *inode = file_inode(iocb->ki_filp);
42 +       ssize_t ret;
44 +       inode_lock_shared(inode);
45 +       /*
46 +        * Recheck under inode lock - at this point we are sure it cannot
47 +        * change anymore
48 +        */
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);
53 +       }
54 +       ret = dax_iomap_rw(iocb, to, &ext4_iomap_ops);
55 +       inode_unlock_shared(inode);
57 +       file_accessed(iocb->ki_filp);
58 +       return ret;
60 +#endif
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 */
67 +#ifdef CONFIG_FS_DAX
68 +       if (IS_DAX(file_inode(iocb->ki_filp)))
69 +               return ext4_dax_read_iter(iocb, to);
70 +#endif
71 +       return generic_file_read_iter(iocb, to);
74  /*
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,
85  #ifdef CONFIG_COMPAT
86 diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
87 index 5337828c68a7..83e8411370d3 100644
88 --- a/fs/ext4/inode.c
89 +++ b/fs/ext4/inode.c
90 @@ -37,6 +37,7 @@
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"
97  #include "xattr.h"
98 @@ -3310,6 +3311,59 @@ int ext4_dax_get_block(struct inode *inode, sector_t iblock,
99         clear_buffer_new(bh_result);
100         return 0;
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;
110 +       int ret;
112 +       if (flags & IOMAP_WRITE)
113 +               return -EIO;
115 +       if (WARN_ON_ONCE(ext4_has_inline_data(inode)))
116 +               return -ERANGE;
118 +       map.m_lblk = first_block;
119 +       map.m_len = last_block - first_block + 1;
121 +       ret = ext4_map_blocks(NULL, inode, &map, 0);
122 +       if (ret < 0)
123 +               return ret;
125 +       iomap->flags = 0;
126 +       iomap->bdev = inode->i_sb->s_bdev;
127 +       iomap->offset = first_block << blkbits;
129 +       if (ret == 0) {
130 +               iomap->type = IOMAP_HOLE;
131 +               iomap->blkno = IOMAP_NULL_BLOCK;
132 +               iomap->length = (u64)map.m_len << blkbits;
133 +       } else {
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;
138 +               } else {
139 +                       WARN_ON_ONCE(1);
140 +                       return -EIO;
141 +               }
142 +               iomap->blkno = (sector_t)map.m_pblk << (blkbits - 9);
143 +               iomap->length = (u64)map.m_len << blkbits;
144 +       }
146 +       if (map.m_flags & EXT4_MAP_NEW)
147 +               iomap->flags |= IOMAP_F_NEW;
148 +       return 0;
151 +struct iomap_ops ext4_iomap_ops = {
152 +       .iomap_begin            = ext4_iomap_begin,
155  #else
156  /* Just define empty function, it will never get called. */
157  int ext4_dax_get_block(struct inode *inode, sector_t iblock,
158 -- 
159 2.6.6