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>
10 fs/ext4/Makefile | 2 +-
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
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];
38 +extern int ext4_mpage_readpages(struct address_space *mapping,
39 + struct list_head *pages, struct page *page,
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
48 @@ -2798,7 +2798,7 @@ static int ext4_readpage(struct file *file, struct page *page)
49 ret = ext4_readpage_inline(inode, page);
52 - return mpage_readpage(page, ext4_get_block);
53 + return ext4_mpage_readpages(page->mapping, NULL, page, 1);
57 @@ -2813,7 +2813,7 @@ ext4_readpages(struct file *file, struct address_space *mapping,
58 if (ext4_has_inline_data(inode))
61 - return mpage_readpages(mapping, pages, nr_pages, ext4_get_block);
62 + return ext4_mpage_readpages(mapping, pages, NULL, nr_pages);
65 static void ext4_invalidatepage(struct page *page, unsigned int offset,
66 diff --git a/fs/ext4/readpage.c b/fs/ext4/readpage.c
68 index 0000000..fff9fe6
70 +++ b/fs/ext4/readpage.c
73 + * linux/fs/ext4/readpage.c
75 + * Copyright (C) 2002, Linus Torvalds.
76 + * Copyright (C) 2015, Google, Inc.
78 + * This was originally taken from fs/mpage.c
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.
86 + * This will allow us to attach a callback function to support ext4
89 + * If anything unusual happens, such as:
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
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.
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>
121 + * I/O completion handler for multipage BIOs.
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().
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.
132 +static void mpage_end_io(struct bio *bio, int err)
134 + struct bio_vec *bv;
137 + bio_for_each_segment_all(bv, bio, i) {
138 + struct page *page = bv->bv_page;
141 + SetPageUptodate(page);
143 + ClearPageUptodate(page);
144 + SetPageError(page);
152 +int ext4_mpage_readpages(struct address_space *mapping,
153 + struct list_head *pages, struct page *page,
156 + struct bio *bio = NULL;
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;
171 + unsigned relative_block = 0;
172 + struct ext4_map_blocks map;
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);
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))
192 + if (page_has_buffers(page))
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;
203 + * Map blocks using the previous result first.
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) {
214 + map.m_flags &= ~EXT4_MAP_MAPPED;
217 + if (page_block == blocks_per_page)
219 + blocks[page_block] = map.m_pblk + map_offset +
227 + * Then do more ext4_map_blocks() calls until we are
228 + * done with this page.
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) {
237 + SetPageError(page);
238 + zero_user_segment(page, 0,
244 + if ((map.m_flags & EXT4_MAP_MAPPED) == 0) {
246 + if (first_hole == blocks_per_page)
247 + first_hole = page_block;
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)
258 + for (relative_block = 0; ; relative_block++) {
259 + if (relative_block == map.m_len) {
261 + map.m_flags &= ~EXT4_MAP_MAPPED;
263 + } else if (page_block == blocks_per_page)
265 + blocks[page_block] = map.m_pblk+relative_block;
270 + if (first_hole != blocks_per_page) {
271 + zero_user_segment(page, first_hole << blkbits,
273 + if (first_hole == 0) {
274 + SetPageUptodate(page);
278 + } else if (fully_mapped) {
279 + SetPageMappedToDisk(page);
281 + if (fully_mapped && blocks_per_page == 1 &&
282 + !PageUptodate(page) && cleancache_get_page(page) == 0) {
283 + SetPageUptodate(page);
288 + * This page will go to BIO. Do we need to send this
291 + if (bio && (last_block_in_bio != blocks[0] - 1)) {
292 + submit_and_realloc:
293 + submit_bio(READ, bio);
297 + bio = bio_alloc(GFP_KERNEL,
298 + min_t(int, nr_pages, bio_get_nr_vecs(bdev)));
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;
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);
316 + last_block_in_bio = blocks[blocks_per_page - 1];
320 + submit_bio(READ, bio);
323 + if (!PageUptodate(page))
324 + block_read_full_page(page, ext4_get_block);
329 + page_cache_release(page);
331 + BUG_ON(pages && !list_empty(pages));
333 + submit_bio(READ, bio);