1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * linux/fs/ext4/readpage.c
4 *
5 * Copyright (C) 2002, Linus Torvalds.
6 * Copyright (C) 2015, Google, Inc.
7 *
8 * This was originally taken from fs/mpage.c
9 *
10 * The ext4_mpage_readpages() function here is intended to
11 * replace mpage_readahead() in the general case, not just for
12 * encrypted files. It has some limitations (see below), where it
13 * will fall back to read_block_full_page(), but these limitations
14 * should only be hit when page_size != block_size.
15 *
16 * This will allow us to attach a callback function to support ext4
17 * encryption.
18 *
19 * If anything unusual happens, such as:
20 *
21 * - encountering a page which has buffers
22 * - encountering a page which has a non-hole after a hole
23 * - encountering a page with non-contiguous blocks
24 *
25 * then this code just gives up and calls the buffer_head-based read function.
26 * It does handle a page which has holes at the end - that is a common case:
27 * the end-of-file on blocksize < PAGE_SIZE setups.
28 *
29 */
30
31 #include <linux/kernel.h>
32 #include <linux/export.h>
33 #include <linux/mm.h>
34 #include <linux/kdev_t.h>
35 #include <linux/gfp.h>
36 #include <linux/bio.h>
37 #include <linux/fs.h>
38 #include <linux/buffer_head.h>
39 #include <linux/blkdev.h>
40 #include <linux/highmem.h>
41 #include <linux/prefetch.h>
42 #include <linux/mpage.h>
43 #include <linux/writeback.h>
44 #include <linux/backing-dev.h>
45 #include <linux/pagevec.h>
46 #include <linux/cleancache.h>
47
48 #include "ext4.h"
49 #include <trace/events/android_fs.h>
50
51 #define NUM_PREALLOC_POST_READ_CTXS 128
52
53 static struct kmem_cache *bio_post_read_ctx_cache;
54 static mempool_t *bio_post_read_ctx_pool;
55
56 /* postprocessing steps for read bios */
57 enum bio_post_read_step {
58 STEP_INITIAL = 0,
59 STEP_DECRYPT,
60 STEP_VERITY,
61 STEP_MAX,
62 };
63
64 struct bio_post_read_ctx {
65 struct bio *bio;
66 struct work_struct work;
67 unsigned int cur_step;
68 unsigned int enabled_steps;
69 };
70
__read_end_io(struct bio * bio)71 static void __read_end_io(struct bio *bio)
72 {
73 struct page *page;
74 struct bio_vec *bv;
75 struct bvec_iter_all iter_all;
76
77 bio_for_each_segment_all(bv, bio, iter_all) {
78 page = bv->bv_page;
79
80 /* PG_error was set if any post_read step failed */
81 if (bio->bi_status || PageError(page)) {
82 ClearPageUptodate(page);
83 /* will re-read again later */
84 ClearPageError(page);
85 } else {
86 SetPageUptodate(page);
87 }
88 unlock_page(page);
89 }
90 if (bio->bi_private)
91 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
92 bio_put(bio);
93 }
94
95 static void bio_post_read_processing(struct bio_post_read_ctx *ctx);
96
decrypt_work(struct work_struct * work)97 static void decrypt_work(struct work_struct *work)
98 {
99 struct bio_post_read_ctx *ctx =
100 container_of(work, struct bio_post_read_ctx, work);
101
102 fscrypt_decrypt_bio(ctx->bio);
103
104 bio_post_read_processing(ctx);
105 }
106
verity_work(struct work_struct * work)107 static void verity_work(struct work_struct *work)
108 {
109 struct bio_post_read_ctx *ctx =
110 container_of(work, struct bio_post_read_ctx, work);
111 struct bio *bio = ctx->bio;
112
113 /*
114 * fsverity_verify_bio() may call readpages() again, and although verity
115 * will be disabled for that, decryption may still be needed, causing
116 * another bio_post_read_ctx to be allocated. So to guarantee that
117 * mempool_alloc() never deadlocks we must free the current ctx first.
118 * This is safe because verity is the last post-read step.
119 */
120 BUILD_BUG_ON(STEP_VERITY + 1 != STEP_MAX);
121 mempool_free(ctx, bio_post_read_ctx_pool);
122 bio->bi_private = NULL;
123
124 fsverity_verify_bio(bio);
125
126 __read_end_io(bio);
127 }
128
bio_post_read_processing(struct bio_post_read_ctx * ctx)129 static void bio_post_read_processing(struct bio_post_read_ctx *ctx)
130 {
131 /*
132 * We use different work queues for decryption and for verity because
133 * verity may require reading metadata pages that need decryption, and
134 * we shouldn't recurse to the same workqueue.
135 */
136 switch (++ctx->cur_step) {
137 case STEP_DECRYPT:
138 if (ctx->enabled_steps & (1 << STEP_DECRYPT)) {
139 INIT_WORK(&ctx->work, decrypt_work);
140 fscrypt_enqueue_decrypt_work(&ctx->work);
141 return;
142 }
143 ctx->cur_step++;
144 fallthrough;
145 case STEP_VERITY:
146 if (ctx->enabled_steps & (1 << STEP_VERITY)) {
147 INIT_WORK(&ctx->work, verity_work);
148 fsverity_enqueue_verify_work(&ctx->work);
149 return;
150 }
151 ctx->cur_step++;
152 fallthrough;
153 default:
154 __read_end_io(ctx->bio);
155 }
156 }
157
bio_post_read_required(struct bio * bio)158 static bool bio_post_read_required(struct bio *bio)
159 {
160 return bio->bi_private && !bio->bi_status;
161 }
162
163 static void
ext4_trace_read_completion(struct bio * bio)164 ext4_trace_read_completion(struct bio *bio)
165 {
166 struct page *first_page = bio->bi_io_vec[0].bv_page;
167
168 if (first_page != NULL)
169 trace_android_fs_dataread_end(first_page->mapping->host,
170 page_offset(first_page),
171 bio->bi_iter.bi_size);
172 }
173
174 /*
175 * I/O completion handler for multipage BIOs.
176 *
177 * The mpage code never puts partial pages into a BIO (except for end-of-file).
178 * If a page does not map to a contiguous run of blocks then it simply falls
179 * back to block_read_full_page().
180 *
181 * Why is this? If a page's completion depends on a number of different BIOs
182 * which can complete in any order (or at the same time) then determining the
183 * status of that page is hard. See end_buffer_async_read() for the details.
184 * There is no point in duplicating all that complexity.
185 */
mpage_end_io(struct bio * bio)186 static void mpage_end_io(struct bio *bio)
187 {
188 if (trace_android_fs_dataread_start_enabled())
189 ext4_trace_read_completion(bio);
190
191 if (bio_post_read_required(bio)) {
192 struct bio_post_read_ctx *ctx = bio->bi_private;
193
194 ctx->cur_step = STEP_INITIAL;
195 bio_post_read_processing(ctx);
196 return;
197 }
198 __read_end_io(bio);
199 }
200
ext4_need_verity(const struct inode * inode,pgoff_t idx)201 static inline bool ext4_need_verity(const struct inode *inode, pgoff_t idx)
202 {
203 return fsverity_active(inode) &&
204 idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
205 }
206
ext4_set_bio_post_read_ctx(struct bio * bio,const struct inode * inode,pgoff_t first_idx)207 static void ext4_set_bio_post_read_ctx(struct bio *bio,
208 const struct inode *inode,
209 pgoff_t first_idx)
210 {
211 unsigned int post_read_steps = 0;
212
213 if (fscrypt_inode_uses_fs_layer_crypto(inode))
214 post_read_steps |= 1 << STEP_DECRYPT;
215
216 if (ext4_need_verity(inode, first_idx))
217 post_read_steps |= 1 << STEP_VERITY;
218
219 if (post_read_steps) {
220 /* Due to the mempool, this never fails. */
221 struct bio_post_read_ctx *ctx =
222 mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
223
224 ctx->bio = bio;
225 ctx->enabled_steps = post_read_steps;
226 bio->bi_private = ctx;
227 }
228 }
229
ext4_readpage_limit(struct inode * inode)230 static inline loff_t ext4_readpage_limit(struct inode *inode)
231 {
232 if (IS_ENABLED(CONFIG_FS_VERITY) &&
233 (IS_VERITY(inode) || ext4_verity_in_progress(inode)))
234 return inode->i_sb->s_maxbytes;
235
236 return i_size_read(inode);
237 }
238
239 static void
ext4_submit_bio_read(struct bio * bio)240 ext4_submit_bio_read(struct bio *bio)
241 {
242 if (trace_android_fs_dataread_start_enabled()) {
243 struct page *first_page = bio->bi_io_vec[0].bv_page;
244
245 if (first_page != NULL) {
246 char *path, pathbuf[MAX_TRACE_PATHBUF_LEN];
247
248 path = android_fstrace_get_pathname(pathbuf,
249 MAX_TRACE_PATHBUF_LEN,
250 first_page->mapping->host);
251 trace_android_fs_dataread_start(
252 first_page->mapping->host,
253 page_offset(first_page),
254 bio->bi_iter.bi_size,
255 current->pid,
256 path,
257 current->comm);
258 }
259 }
260 submit_bio(bio);
261 }
262
ext4_mpage_readpages(struct inode * inode,struct readahead_control * rac,struct page * page)263 int ext4_mpage_readpages(struct inode *inode,
264 struct readahead_control *rac, struct page *page)
265 {
266 struct bio *bio = NULL;
267 sector_t last_block_in_bio = 0;
268
269 const unsigned blkbits = inode->i_blkbits;
270 const unsigned blocks_per_page = PAGE_SIZE >> blkbits;
271 const unsigned blocksize = 1 << blkbits;
272 sector_t next_block;
273 sector_t block_in_file;
274 sector_t last_block;
275 sector_t last_block_in_file;
276 sector_t blocks[MAX_BUF_PER_PAGE];
277 unsigned page_block;
278 struct block_device *bdev = inode->i_sb->s_bdev;
279 int length;
280 unsigned relative_block = 0;
281 struct ext4_map_blocks map;
282 unsigned int nr_pages = rac ? readahead_count(rac) : 1;
283
284 map.m_pblk = 0;
285 map.m_lblk = 0;
286 map.m_len = 0;
287 map.m_flags = 0;
288
289 for (; nr_pages; nr_pages--) {
290 int fully_mapped = 1;
291 unsigned first_hole = blocks_per_page;
292
293 if (rac) {
294 page = readahead_page(rac);
295 prefetchw(&page->flags);
296 }
297
298 if (page_has_buffers(page))
299 goto confused;
300
301 block_in_file = next_block =
302 (sector_t)page->index << (PAGE_SHIFT - blkbits);
303 last_block = block_in_file + nr_pages * blocks_per_page;
304 last_block_in_file = (ext4_readpage_limit(inode) +
305 blocksize - 1) >> blkbits;
306 if (last_block > last_block_in_file)
307 last_block = last_block_in_file;
308 page_block = 0;
309
310 /*
311 * Map blocks using the previous result first.
312 */
313 if ((map.m_flags & EXT4_MAP_MAPPED) &&
314 block_in_file > map.m_lblk &&
315 block_in_file < (map.m_lblk + map.m_len)) {
316 unsigned map_offset = block_in_file - map.m_lblk;
317 unsigned last = map.m_len - map_offset;
318
319 for (relative_block = 0; ; relative_block++) {
320 if (relative_block == last) {
321 /* needed? */
322 map.m_flags &= ~EXT4_MAP_MAPPED;
323 break;
324 }
325 if (page_block == blocks_per_page)
326 break;
327 blocks[page_block] = map.m_pblk + map_offset +
328 relative_block;
329 page_block++;
330 block_in_file++;
331 }
332 }
333
334 /*
335 * Then do more ext4_map_blocks() calls until we are
336 * done with this page.
337 */
338 while (page_block < blocks_per_page) {
339 if (block_in_file < last_block) {
340 map.m_lblk = block_in_file;
341 map.m_len = last_block - block_in_file;
342
343 if (ext4_map_blocks(NULL, inode, &map, 0) < 0) {
344 set_error_page:
345 SetPageError(page);
346 zero_user_segment(page, 0,
347 PAGE_SIZE);
348 unlock_page(page);
349 goto next_page;
350 }
351 }
352 if ((map.m_flags & EXT4_MAP_MAPPED) == 0) {
353 fully_mapped = 0;
354 if (first_hole == blocks_per_page)
355 first_hole = page_block;
356 page_block++;
357 block_in_file++;
358 continue;
359 }
360 if (first_hole != blocks_per_page)
361 goto confused; /* hole -> non-hole */
362
363 /* Contiguous blocks? */
364 if (page_block && blocks[page_block-1] != map.m_pblk-1)
365 goto confused;
366 for (relative_block = 0; ; relative_block++) {
367 if (relative_block == map.m_len) {
368 /* needed? */
369 map.m_flags &= ~EXT4_MAP_MAPPED;
370 break;
371 } else if (page_block == blocks_per_page)
372 break;
373 blocks[page_block] = map.m_pblk+relative_block;
374 page_block++;
375 block_in_file++;
376 }
377 }
378 if (first_hole != blocks_per_page) {
379 zero_user_segment(page, first_hole << blkbits,
380 PAGE_SIZE);
381 if (first_hole == 0) {
382 if (ext4_need_verity(inode, page->index) &&
383 !fsverity_verify_page(page))
384 goto set_error_page;
385 SetPageUptodate(page);
386 unlock_page(page);
387 goto next_page;
388 }
389 } else if (fully_mapped) {
390 SetPageMappedToDisk(page);
391 }
392 if (fully_mapped && blocks_per_page == 1 &&
393 !PageUptodate(page) && cleancache_get_page(page) == 0) {
394 SetPageUptodate(page);
395 goto confused;
396 }
397
398 /*
399 * This page will go to BIO. Do we need to send this
400 * BIO off first?
401 */
402 if (bio && (last_block_in_bio != blocks[0] - 1 ||
403 !fscrypt_mergeable_bio(bio, inode, next_block))) {
404 submit_and_realloc:
405 ext4_submit_bio_read(bio);
406 bio = NULL;
407 }
408 if (bio == NULL) {
409 /*
410 * bio_alloc will _always_ be able to allocate a bio if
411 * __GFP_DIRECT_RECLAIM is set, see bio_alloc_bioset().
412 */
413 bio = bio_alloc(GFP_KERNEL,
414 min_t(int, nr_pages, BIO_MAX_PAGES));
415 fscrypt_set_bio_crypt_ctx(bio, inode, next_block,
416 GFP_KERNEL);
417 ext4_set_bio_post_read_ctx(bio, inode, page->index);
418 bio_set_dev(bio, bdev);
419 bio->bi_iter.bi_sector = blocks[0] << (blkbits - 9);
420 bio->bi_end_io = mpage_end_io;
421 bio_set_op_attrs(bio, REQ_OP_READ,
422 rac ? REQ_RAHEAD : 0);
423 }
424
425 length = first_hole << blkbits;
426 if (bio_add_page(bio, page, length, 0) < length)
427 goto submit_and_realloc;
428
429 if (((map.m_flags & EXT4_MAP_BOUNDARY) &&
430 (relative_block == map.m_len)) ||
431 (first_hole != blocks_per_page)) {
432 ext4_submit_bio_read(bio);
433 bio = NULL;
434 } else
435 last_block_in_bio = blocks[blocks_per_page - 1];
436 goto next_page;
437 confused:
438 if (bio) {
439 ext4_submit_bio_read(bio);
440 bio = NULL;
441 }
442 if (!PageUptodate(page))
443 block_read_full_page(page, ext4_get_block);
444 else
445 unlock_page(page);
446 next_page:
447 if (rac)
448 put_page(page);
449 }
450 if (bio)
451 ext4_submit_bio_read(bio);
452 return 0;
453 }
454
ext4_init_post_read_processing(void)455 int __init ext4_init_post_read_processing(void)
456 {
457 bio_post_read_ctx_cache =
458 kmem_cache_create("ext4_bio_post_read_ctx",
459 sizeof(struct bio_post_read_ctx), 0, 0, NULL);
460 if (!bio_post_read_ctx_cache)
461 goto fail;
462 bio_post_read_ctx_pool =
463 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
464 bio_post_read_ctx_cache);
465 if (!bio_post_read_ctx_pool)
466 goto fail_free_cache;
467 return 0;
468
469 fail_free_cache:
470 kmem_cache_destroy(bio_post_read_ctx_cache);
471 fail:
472 return -ENOMEM;
473 }
474
ext4_exit_post_read_processing(void)475 void ext4_exit_post_read_processing(void)
476 {
477 mempool_destroy(bio_post_read_ctx_pool);
478 kmem_cache_destroy(bio_post_read_ctx_cache);
479 }
480