Sync ext4 encryption as of commit dffd334e4d7134
[ext4-patch-queue.git] / add-readpage-file
blobee4fdcd73cb9b0988ed6ee6894003f60b611acea
1 ext4 crypto: add ext4_mpage_readpages()
3 This takes code from fs/mpage.c and optimizes it for ext4.  Its
4 primary reason is to allow us to more easily add encryption to ext4's
5 read path in an efficient manner.
7 Change-Id: I0a115e90ab13861ab3e96641a0eb82b951198ecc
8 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
9 ---
10  fs/ext4/Makefile   |   2 +-
11  fs/ext4/ext4.h     |   4 ++
12  fs/ext4/inode.c    |   4 +-
13  fs/ext4/readpage.c | 264 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
14  4 files changed, 271 insertions(+), 3 deletions(-)
16 diff --git a/fs/ext4/Makefile b/fs/ext4/Makefile
17 index 0310fec..cd6f50f 100644
18 --- a/fs/ext4/Makefile
19 +++ b/fs/ext4/Makefile
20 @@ -8,7 +8,7 @@ ext4-y  := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o page-io.o \
21                 ioctl.o namei.o super.o symlink.o hash.o resize.o extents.o \
22                 ext4_jbd2.o migrate.o mballoc.o block_validity.o move_extent.o \
23                 mmp.o indirect.o extents_status.o xattr.o xattr_user.o \
24 -               xattr_trusted.o inline.o
25 +               xattr_trusted.o inline.o readpage.o
27  ext4-$(CONFIG_EXT4_FS_POSIX_ACL)       += acl.o
28  ext4-$(CONFIG_EXT4_FS_SECURITY)                += xattr_security.o
29 diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
30 index a75fba6..06e8add 100644
31 --- a/fs/ext4/ext4.h
32 +++ b/fs/ext4/ext4.h
33 @@ -2683,6 +2683,10 @@ static inline void ext4_set_de_type(struct super_block *sb,
34                 de->file_type = ext4_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
35  }
37 +/* readpages.c */
38 +extern int ext4_mpage_readpages(struct address_space *mapping,
39 +                               struct list_head *pages, struct page *page,
40 +                               unsigned nr_pages);
42  /* symlink.c */
43  extern const struct inode_operations ext4_symlink_inode_operations;
44 diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
45 index 5653fa4..a68cacc 100644
46 --- a/fs/ext4/inode.c
47 +++ b/fs/ext4/inode.c
48 @@ -2798,7 +2798,7 @@ static int ext4_readpage(struct file *file, struct page *page)
49                 ret = ext4_readpage_inline(inode, page);
51         if (ret == -EAGAIN)
52 -               return mpage_readpage(page, ext4_get_block);
53 +               return ext4_mpage_readpages(page->mapping, NULL, page, 1);
55         return ret;
56  }
57 @@ -2813,7 +2813,7 @@ ext4_readpages(struct file *file, struct address_space *mapping,
58         if (ext4_has_inline_data(inode))
59                 return 0;
61 -       return mpage_readpages(mapping, pages, nr_pages, ext4_get_block);
62 +       return ext4_mpage_readpages(mapping, pages, NULL, nr_pages);
63  }
65  static void ext4_invalidatepage(struct page *page, unsigned int offset,
66 diff --git a/fs/ext4/readpage.c b/fs/ext4/readpage.c
67 new file mode 100644
68 index 0000000..fff9fe6
69 --- /dev/null
70 +++ b/fs/ext4/readpage.c
71 @@ -0,0 +1,264 @@
72 +/*
73 + * linux/fs/ext4/readpage.c
74 + *
75 + * Copyright (C) 2002, Linus Torvalds.
76 + * Copyright (C) 2015, Google, Inc.
77 + *
78 + * This was originally taken from fs/mpage.c
79 + *
80 + * The intent is the ext4_mpage_readpages() function here is intended
81 + * to replace mpage_readpages() in the general case, not just for
82 + * encrypted files.  It has some limitations (see below), where it
83 + * will fall back to read_block_full_page(), but these limitations
84 + * should only be hit when page_size != block_size.
85 + *
86 + * This will allow us to attach a callback function to support ext4
87 + * encryption.
88 + *
89 + * If anything unusual happens, such as:
90 + *
91 + * - encountering a page which has buffers
92 + * - encountering a page which has a non-hole after a hole
93 + * - encountering a page with non-contiguous blocks
94 + *
95 + * then this code just gives up and calls the buffer_head-based read function.
96 + * It does handle a page which has holes at the end - that is a common case:
97 + * the end-of-file on blocksize < PAGE_CACHE_SIZE setups.
98 + *
99 + */
101 +#include <linux/kernel.h>
102 +#include <linux/export.h>
103 +#include <linux/mm.h>
104 +#include <linux/kdev_t.h>
105 +#include <linux/gfp.h>
106 +#include <linux/bio.h>
107 +#include <linux/fs.h>
108 +#include <linux/buffer_head.h>
109 +#include <linux/blkdev.h>
110 +#include <linux/highmem.h>
111 +#include <linux/prefetch.h>
112 +#include <linux/mpage.h>
113 +#include <linux/writeback.h>
114 +#include <linux/backing-dev.h>
115 +#include <linux/pagevec.h>
116 +#include <linux/cleancache.h>
118 +#include "ext4.h"
121 + * I/O completion handler for multipage BIOs.
122 + *
123 + * The mpage code never puts partial pages into a BIO (except for end-of-file).
124 + * If a page does not map to a contiguous run of blocks then it simply falls
125 + * back to block_read_full_page().
126 + *
127 + * Why is this?  If a page's completion depends on a number of different BIOs
128 + * which can complete in any order (or at the same time) then determining the
129 + * status of that page is hard.  See end_buffer_async_read() for the details.
130 + * There is no point in duplicating all that complexity.
131 + */
132 +static void mpage_end_io(struct bio *bio, int err)
134 +       struct bio_vec *bv;
135 +       int i;
137 +       bio_for_each_segment_all(bv, bio, i) {
138 +               struct page *page = bv->bv_page;
140 +               if (!err) {
141 +                       SetPageUptodate(page);
142 +               } else {
143 +                       ClearPageUptodate(page);
144 +                       SetPageError(page);
145 +               }
146 +               unlock_page(page);
147 +       }
149 +       bio_put(bio);
152 +int ext4_mpage_readpages(struct address_space *mapping,
153 +                        struct list_head *pages, struct page *page,
154 +                        unsigned nr_pages)
156 +       struct bio *bio = NULL;
157 +       unsigned page_idx;
158 +       sector_t last_block_in_bio = 0;
160 +       struct inode *inode = mapping->host;
161 +       const unsigned blkbits = inode->i_blkbits;
162 +       const unsigned blocks_per_page = PAGE_CACHE_SIZE >> blkbits;
163 +       const unsigned blocksize = 1 << blkbits;
164 +       sector_t block_in_file;
165 +       sector_t last_block;
166 +       sector_t last_block_in_file;
167 +       sector_t blocks[MAX_BUF_PER_PAGE];
168 +       unsigned page_block;
169 +       struct block_device *bdev = inode->i_sb->s_bdev;
170 +       int length;
171 +       unsigned relative_block = 0;
172 +       struct ext4_map_blocks map;
174 +       map.m_pblk = 0;
175 +       map.m_lblk = 0;
176 +       map.m_len = 0;
177 +       map.m_flags = 0;
179 +       for (page_idx = 0; nr_pages; page_idx++, nr_pages--) {
180 +               int fully_mapped = 1;
181 +               unsigned first_hole = blocks_per_page;
183 +               prefetchw(&page->flags);
184 +               if (pages) {
185 +                       page = list_entry(pages->prev, struct page, lru);
186 +                       list_del(&page->lru);
187 +                       if (add_to_page_cache_lru(page, mapping,
188 +                                                 page->index, GFP_KERNEL))
189 +                               goto next_page;
190 +               }
192 +               if (page_has_buffers(page))
193 +                       goto confused;
195 +               block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits);
196 +               last_block = block_in_file + nr_pages * blocks_per_page;
197 +               last_block_in_file = (i_size_read(inode) + blocksize - 1) >> blkbits;
198 +               if (last_block > last_block_in_file)
199 +                       last_block = last_block_in_file;
200 +               page_block = 0;
202 +               /*
203 +                * Map blocks using the previous result first.
204 +                */
205 +               if ((map.m_flags & EXT4_MAP_MAPPED) &&
206 +                   block_in_file > map.m_lblk &&
207 +                   block_in_file < (map.m_lblk + map.m_len)) {
208 +                       unsigned map_offset = block_in_file - map.m_lblk;
209 +                       unsigned last = map.m_len - map_offset;
211 +                       for (relative_block = 0; ; relative_block++) {
212 +                               if (relative_block == last) {
213 +                                       /* needed? */
214 +                                       map.m_flags &= ~EXT4_MAP_MAPPED;
215 +                                       break;
216 +                               }
217 +                               if (page_block == blocks_per_page)
218 +                                       break;
219 +                               blocks[page_block] = map.m_pblk + map_offset +
220 +                                       relative_block;
221 +                               page_block++;
222 +                               block_in_file++;
223 +                       }
224 +               }
226 +               /*
227 +                * Then do more ext4_map_blocks() calls until we are
228 +                * done with this page.
229 +                */
230 +               while (page_block < blocks_per_page) {
231 +                       if (block_in_file < last_block) {
232 +                               map.m_lblk = block_in_file;
233 +                               map.m_len = last_block - block_in_file;
235 +                               if (ext4_map_blocks(NULL, inode, &map, 0) < 0) {
236 +                               set_error_page:
237 +                                       SetPageError(page);
238 +                                       zero_user_segment(page, 0,
239 +                                                         PAGE_CACHE_SIZE);
240 +                                       unlock_page(page);
241 +                                       goto next_page;
242 +                               }
243 +                       }
244 +                       if ((map.m_flags & EXT4_MAP_MAPPED) == 0) {
245 +                               fully_mapped = 0;
246 +                               if (first_hole == blocks_per_page)
247 +                                       first_hole = page_block;
248 +                               page_block++;
249 +                               block_in_file++;
250 +                               continue;
251 +                       }
252 +                       if (first_hole != blocks_per_page)
253 +                               goto confused;          /* hole -> non-hole */
255 +                       /* Contiguous blocks? */
256 +                       if (page_block && blocks[page_block-1] != map.m_pblk-1)
257 +                               goto confused;
258 +                       for (relative_block = 0; ; relative_block++) {
259 +                               if (relative_block == map.m_len) {
260 +                                       /* needed? */
261 +                                       map.m_flags &= ~EXT4_MAP_MAPPED;
262 +                                       break;
263 +                               } else if (page_block == blocks_per_page)
264 +                                       break;
265 +                               blocks[page_block] = map.m_pblk+relative_block;
266 +                               page_block++;
267 +                               block_in_file++;
268 +                       }
269 +               }
270 +               if (first_hole != blocks_per_page) {
271 +                       zero_user_segment(page, first_hole << blkbits,
272 +                                         PAGE_CACHE_SIZE);
273 +                       if (first_hole == 0) {
274 +                               SetPageUptodate(page);
275 +                               unlock_page(page);
276 +                               goto next_page;
277 +                       }
278 +               } else if (fully_mapped) {
279 +                       SetPageMappedToDisk(page);
280 +               }
281 +               if (fully_mapped && blocks_per_page == 1 &&
282 +                   !PageUptodate(page) && cleancache_get_page(page) == 0) {
283 +                       SetPageUptodate(page);
284 +                       goto confused;
285 +               }
287 +               /*
288 +                * This page will go to BIO.  Do we need to send this
289 +                * BIO off first?
290 +                */
291 +               if (bio && (last_block_in_bio != blocks[0] - 1)) {
292 +               submit_and_realloc:
293 +                       submit_bio(READ, bio);
294 +                       bio = NULL;
295 +               }
296 +               if (bio == NULL) {
297 +                       bio = bio_alloc(GFP_KERNEL,
298 +                               min_t(int, nr_pages, bio_get_nr_vecs(bdev)));
299 +                       if (!bio)
300 +                               goto set_error_page;
301 +                       bio->bi_bdev = bdev;
302 +                       bio->bi_iter.bi_sector = blocks[0] << (blkbits - 9);
303 +                       bio->bi_end_io = mpage_end_io;
304 +               }
306 +               length = first_hole << blkbits;
307 +               if (bio_add_page(bio, page, length, 0) < length)
308 +                       goto submit_and_realloc;
310 +               if (((map.m_flags & EXT4_MAP_BOUNDARY) &&
311 +                    (relative_block == map.m_len)) ||
312 +                   (first_hole != blocks_per_page)) {
313 +                       submit_bio(READ, bio);
314 +                       bio = NULL;
315 +               } else
316 +                       last_block_in_bio = blocks[blocks_per_page - 1];
317 +               goto next_page;
318 +       confused:
319 +               if (bio) {
320 +                       submit_bio(READ, bio);
321 +                       bio = NULL;
322 +               }
323 +               if (!PageUptodate(page))
324 +                       block_read_full_page(page, ext4_get_block);
325 +               else
326 +                       unlock_page(page);
327 +       next_page:
328 +               if (pages)
329 +                       page_cache_release(page);
330 +       }
331 +       BUG_ON(pages && !list_empty(pages));
332 +       if (bio)
333 +               submit_bio(READ, bio);
334 +       return 0;