blob: 270c4f495db30f831d0d6126e770c04bfcf3efec [file] [log] [blame]
Theodore Ts'of64e02f2015-04-08 00:00:32 -04001/*
2 * linux/fs/ext4/readpage.c
3 *
4 * Copyright (C) 2002, Linus Torvalds.
5 * Copyright (C) 2015, Google, Inc.
6 *
7 * This was originally taken from fs/mpage.c
8 *
9 * The intent is the ext4_mpage_readpages() function here is intended
10 * to replace mpage_readpages() in the general case, not just for
11 * encrypted files. It has some limitations (see below), where it
12 * will fall back to read_block_full_page(), but these limitations
13 * should only be hit when page_size != block_size.
14 *
15 * This will allow us to attach a callback function to support ext4
16 * encryption.
17 *
18 * If anything unusual happens, such as:
19 *
20 * - encountering a page which has buffers
21 * - encountering a page which has a non-hole after a hole
22 * - encountering a page with non-contiguous blocks
23 *
24 * then this code just gives up and calls the buffer_head-based read function.
25 * It does handle a page which has holes at the end - that is a common case:
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +030026 * the end-of-file on blocksize < PAGE_SIZE setups.
Theodore Ts'of64e02f2015-04-08 00:00:32 -040027 *
28 */
29
30#include <linux/kernel.h>
31#include <linux/export.h>
32#include <linux/mm.h>
33#include <linux/kdev_t.h>
34#include <linux/gfp.h>
35#include <linux/bio.h>
36#include <linux/fs.h>
37#include <linux/buffer_head.h>
38#include <linux/blkdev.h>
39#include <linux/highmem.h>
40#include <linux/prefetch.h>
41#include <linux/mpage.h>
42#include <linux/writeback.h>
43#include <linux/backing-dev.h>
44#include <linux/pagevec.h>
45#include <linux/cleancache.h>
46
47#include "ext4.h"
Mohan Srinivasan25cc70f2016-12-14 16:39:51 -080048#include <trace/events/android_fs.h>
Theodore Ts'of64e02f2015-04-08 00:00:32 -040049
Michael Halcrowc9c74292015-04-12 00:56:10 -040050static inline bool ext4_bio_encrypted(struct bio *bio)
51{
52#ifdef CONFIG_EXT4_FS_ENCRYPTION
53 return unlikely(bio->bi_private != NULL);
54#else
55 return false;
56#endif
57}
58
Mohan Srinivasan25cc70f2016-12-14 16:39:51 -080059static void
60ext4_trace_read_completion(struct bio *bio)
61{
62 struct page *first_page = bio->bi_io_vec[0].bv_page;
63
64 if (first_page != NULL)
65 trace_android_fs_dataread_end(first_page->mapping->host,
66 page_offset(first_page),
67 bio->bi_iter.bi_size);
68}
69
Michael Halcrowc9c74292015-04-12 00:56:10 -040070/*
Theodore Ts'of64e02f2015-04-08 00:00:32 -040071 * I/O completion handler for multipage BIOs.
72 *
73 * The mpage code never puts partial pages into a BIO (except for end-of-file).
74 * If a page does not map to a contiguous run of blocks then it simply falls
75 * back to block_read_full_page().
76 *
77 * Why is this? If a page's completion depends on a number of different BIOs
78 * which can complete in any order (or at the same time) then determining the
79 * status of that page is hard. See end_buffer_async_read() for the details.
80 * There is no point in duplicating all that complexity.
81 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020082static void mpage_end_io(struct bio *bio)
Theodore Ts'of64e02f2015-04-08 00:00:32 -040083{
84 struct bio_vec *bv;
85 int i;
86
Mohan Srinivasan25cc70f2016-12-14 16:39:51 -080087 if (trace_android_fs_dataread_start_enabled())
88 ext4_trace_read_completion(bio);
89
Michael Halcrowc9c74292015-04-12 00:56:10 -040090 if (ext4_bio_encrypted(bio)) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020091 if (bio->bi_error) {
Jaegeuk Kima7550b32016-07-10 14:01:03 -040092 fscrypt_release_ctx(bio->bi_private);
Michael Halcrowc9c74292015-04-12 00:56:10 -040093 } else {
Eric Biggers823fd562018-04-18 11:09:47 -070094 fscrypt_enqueue_decrypt_bio(bio->bi_private, bio);
Michael Halcrowc9c74292015-04-12 00:56:10 -040095 return;
96 }
97 }
Theodore Ts'of64e02f2015-04-08 00:00:32 -040098 bio_for_each_segment_all(bv, bio, i) {
99 struct page *page = bv->bv_page;
100
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200101 if (!bio->bi_error) {
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400102 SetPageUptodate(page);
103 } else {
104 ClearPageUptodate(page);
105 SetPageError(page);
106 }
107 unlock_page(page);
108 }
109
110 bio_put(bio);
111}
112
Mohan Srinivasan25cc70f2016-12-14 16:39:51 -0800113static void
114ext4_submit_bio_read(struct bio *bio)
115{
116 if (trace_android_fs_dataread_start_enabled()) {
117 struct page *first_page = bio->bi_io_vec[0].bv_page;
118
119 if (first_page != NULL) {
Mohan Srinivasan009e6082017-02-10 14:26:23 -0800120 char *path, pathbuf[MAX_TRACE_PATHBUF_LEN];
121
122 path = android_fstrace_get_pathname(pathbuf,
123 MAX_TRACE_PATHBUF_LEN,
124 first_page->mapping->host);
Mohan Srinivasan25cc70f2016-12-14 16:39:51 -0800125 trace_android_fs_dataread_start(
126 first_page->mapping->host,
127 page_offset(first_page),
128 bio->bi_iter.bi_size,
129 current->pid,
Mohan Srinivasan009e6082017-02-10 14:26:23 -0800130 path,
Mohan Srinivasan25cc70f2016-12-14 16:39:51 -0800131 current->comm);
132 }
133 }
134 submit_bio(bio);
135}
136
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400137int ext4_mpage_readpages(struct address_space *mapping,
138 struct list_head *pages, struct page *page,
139 unsigned nr_pages)
140{
141 struct bio *bio = NULL;
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400142 sector_t last_block_in_bio = 0;
143
144 struct inode *inode = mapping->host;
145 const unsigned blkbits = inode->i_blkbits;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300146 const unsigned blocks_per_page = PAGE_SIZE >> blkbits;
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400147 const unsigned blocksize = 1 << blkbits;
148 sector_t block_in_file;
149 sector_t last_block;
150 sector_t last_block_in_file;
151 sector_t blocks[MAX_BUF_PER_PAGE];
152 unsigned page_block;
153 struct block_device *bdev = inode->i_sb->s_bdev;
154 int length;
155 unsigned relative_block = 0;
156 struct ext4_map_blocks map;
157
158 map.m_pblk = 0;
159 map.m_lblk = 0;
160 map.m_len = 0;
161 map.m_flags = 0;
162
yalin wangde9e9182016-07-05 16:32:32 -0400163 for (; nr_pages; nr_pages--) {
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400164 int fully_mapped = 1;
165 unsigned first_hole = blocks_per_page;
166
167 prefetchw(&page->flags);
168 if (pages) {
169 page = list_entry(pages->prev, struct page, lru);
170 list_del(&page->lru);
Michal Hocko063d99b2015-10-15 15:28:24 -0700171 if (add_to_page_cache_lru(page, mapping, page->index,
Michal Hocko8a5c7432016-07-26 15:24:53 -0700172 readahead_gfp_mask(mapping)))
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400173 goto next_page;
174 }
175
176 if (page_has_buffers(page))
177 goto confused;
178
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300179 block_in_file = (sector_t)page->index << (PAGE_SHIFT - blkbits);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400180 last_block = block_in_file + nr_pages * blocks_per_page;
181 last_block_in_file = (i_size_read(inode) + blocksize - 1) >> blkbits;
182 if (last_block > last_block_in_file)
183 last_block = last_block_in_file;
184 page_block = 0;
185
186 /*
187 * Map blocks using the previous result first.
188 */
189 if ((map.m_flags & EXT4_MAP_MAPPED) &&
190 block_in_file > map.m_lblk &&
191 block_in_file < (map.m_lblk + map.m_len)) {
192 unsigned map_offset = block_in_file - map.m_lblk;
193 unsigned last = map.m_len - map_offset;
194
195 for (relative_block = 0; ; relative_block++) {
196 if (relative_block == last) {
197 /* needed? */
198 map.m_flags &= ~EXT4_MAP_MAPPED;
199 break;
200 }
201 if (page_block == blocks_per_page)
202 break;
203 blocks[page_block] = map.m_pblk + map_offset +
204 relative_block;
205 page_block++;
206 block_in_file++;
207 }
208 }
209
210 /*
211 * Then do more ext4_map_blocks() calls until we are
212 * done with this page.
213 */
214 while (page_block < blocks_per_page) {
215 if (block_in_file < last_block) {
216 map.m_lblk = block_in_file;
217 map.m_len = last_block - block_in_file;
218
219 if (ext4_map_blocks(NULL, inode, &map, 0) < 0) {
220 set_error_page:
221 SetPageError(page);
222 zero_user_segment(page, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300223 PAGE_SIZE);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400224 unlock_page(page);
225 goto next_page;
226 }
227 }
228 if ((map.m_flags & EXT4_MAP_MAPPED) == 0) {
229 fully_mapped = 0;
230 if (first_hole == blocks_per_page)
231 first_hole = page_block;
232 page_block++;
233 block_in_file++;
234 continue;
235 }
236 if (first_hole != blocks_per_page)
237 goto confused; /* hole -> non-hole */
238
239 /* Contiguous blocks? */
240 if (page_block && blocks[page_block-1] != map.m_pblk-1)
241 goto confused;
242 for (relative_block = 0; ; relative_block++) {
243 if (relative_block == map.m_len) {
244 /* needed? */
245 map.m_flags &= ~EXT4_MAP_MAPPED;
246 break;
247 } else if (page_block == blocks_per_page)
248 break;
249 blocks[page_block] = map.m_pblk+relative_block;
250 page_block++;
251 block_in_file++;
252 }
253 }
254 if (first_hole != blocks_per_page) {
255 zero_user_segment(page, first_hole << blkbits,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300256 PAGE_SIZE);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400257 if (first_hole == 0) {
258 SetPageUptodate(page);
259 unlock_page(page);
260 goto next_page;
261 }
262 } else if (fully_mapped) {
263 SetPageMappedToDisk(page);
264 }
265 if (fully_mapped && blocks_per_page == 1 &&
266 !PageUptodate(page) && cleancache_get_page(page) == 0) {
267 SetPageUptodate(page);
268 goto confused;
269 }
270
271 /*
272 * This page will go to BIO. Do we need to send this
273 * BIO off first?
274 */
275 if (bio && (last_block_in_bio != blocks[0] - 1)) {
276 submit_and_realloc:
Mohan Srinivasan25cc70f2016-12-14 16:39:51 -0800277 ext4_submit_bio_read(bio);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400278 bio = NULL;
279 }
280 if (bio == NULL) {
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400281 struct fscrypt_ctx *ctx = NULL;
Michael Halcrowc9c74292015-04-12 00:56:10 -0400282
283 if (ext4_encrypted_inode(inode) &&
284 S_ISREG(inode->i_mode)) {
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400285 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
Michael Halcrowc9c74292015-04-12 00:56:10 -0400286 if (IS_ERR(ctx))
287 goto set_error_page;
288 }
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400289 bio = bio_alloc(GFP_KERNEL,
Kent Overstreetb54ffb72015-05-19 14:31:01 +0200290 min_t(int, nr_pages, BIO_MAX_PAGES));
Michael Halcrowc9c74292015-04-12 00:56:10 -0400291 if (!bio) {
292 if (ctx)
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400293 fscrypt_release_ctx(ctx);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400294 goto set_error_page;
Michael Halcrowc9c74292015-04-12 00:56:10 -0400295 }
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400296 bio->bi_bdev = bdev;
297 bio->bi_iter.bi_sector = blocks[0] << (blkbits - 9);
298 bio->bi_end_io = mpage_end_io;
Michael Halcrowc9c74292015-04-12 00:56:10 -0400299 bio->bi_private = ctx;
Michael Halcrowe93c1c12017-10-11 16:36:05 -0700300 bio_set_op_attrs(bio, REQ_OP_READ,
301 ctx ? REQ_NOENCRYPT : 0);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400302 }
303
304 length = first_hole << blkbits;
305 if (bio_add_page(bio, page, length, 0) < length)
306 goto submit_and_realloc;
307
308 if (((map.m_flags & EXT4_MAP_BOUNDARY) &&
309 (relative_block == map.m_len)) ||
310 (first_hole != blocks_per_page)) {
Mohan Srinivasan25cc70f2016-12-14 16:39:51 -0800311 ext4_submit_bio_read(bio);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400312 bio = NULL;
313 } else
314 last_block_in_bio = blocks[blocks_per_page - 1];
315 goto next_page;
316 confused:
317 if (bio) {
Mohan Srinivasan25cc70f2016-12-14 16:39:51 -0800318 ext4_submit_bio_read(bio);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400319 bio = NULL;
320 }
321 if (!PageUptodate(page))
322 block_read_full_page(page, ext4_get_block);
323 else
324 unlock_page(page);
325 next_page:
326 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300327 put_page(page);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400328 }
329 BUG_ON(pages && !list_empty(pages));
330 if (bio)
Mohan Srinivasan25cc70f2016-12-14 16:39:51 -0800331 ext4_submit_bio_read(bio);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400332 return 0;
333}