blob: eb9c630cdbb7d03c282a7e5190c923f66a878183 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001// 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 intent is the ext4_mpage_readpages() function here is intended
11 * to replace mpage_readpages() 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
53static struct kmem_cache *bio_post_read_ctx_cache;
54static mempool_t *bio_post_read_ctx_pool;
55
56/* postprocessing steps for read bios */
57enum bio_post_read_step {
58 STEP_INITIAL = 0,
59 STEP_DECRYPT,
60 STEP_VERITY,
61};
62
63struct bio_post_read_ctx {
64 struct bio *bio;
65 struct work_struct work;
66 unsigned int cur_step;
67 unsigned int enabled_steps;
68};
69
70static void __read_end_io(struct bio *bio)
71{
72 struct page *page;
73 struct bio_vec *bv;
74 int i;
75
76 bio_for_each_segment_all(bv, bio, i) {
77 page = bv->bv_page;
78
79 /* PG_error was set if any post_read step failed */
80 if (bio->bi_status || PageError(page)) {
81 ClearPageUptodate(page);
82 /* will re-read again later */
83 ClearPageError(page);
84 } else {
85 SetPageUptodate(page);
86 }
87 unlock_page(page);
88 }
89 if (bio->bi_private)
90 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
91 bio_put(bio);
92}
93
94static void bio_post_read_processing(struct bio_post_read_ctx *ctx);
95
96static void decrypt_work(struct work_struct *work)
97{
98 struct bio_post_read_ctx *ctx =
99 container_of(work, struct bio_post_read_ctx, work);
100
101 fscrypt_decrypt_bio(ctx->bio);
102
103 bio_post_read_processing(ctx);
104}
105
106static void verity_work(struct work_struct *work)
107{
108 struct bio_post_read_ctx *ctx =
109 container_of(work, struct bio_post_read_ctx, work);
110
111 fsverity_verify_bio(ctx->bio);
112
113 bio_post_read_processing(ctx);
114}
115
116static void bio_post_read_processing(struct bio_post_read_ctx *ctx)
117{
118 /*
119 * We use different work queues for decryption and for verity because
120 * verity may require reading metadata pages that need decryption, and
121 * we shouldn't recurse to the same workqueue.
122 */
123 switch (++ctx->cur_step) {
124 case STEP_DECRYPT:
125 if (ctx->enabled_steps & (1 << STEP_DECRYPT)) {
126 INIT_WORK(&ctx->work, decrypt_work);
127 fscrypt_enqueue_decrypt_work(&ctx->work);
128 return;
129 }
130 ctx->cur_step++;
131 /* fall-through */
132 case STEP_VERITY:
133 if (ctx->enabled_steps & (1 << STEP_VERITY)) {
134 INIT_WORK(&ctx->work, verity_work);
135 fsverity_enqueue_verify_work(&ctx->work);
136 return;
137 }
138 ctx->cur_step++;
139 /* fall-through */
140 default:
141 __read_end_io(ctx->bio);
142 }
143}
144
145static bool bio_post_read_required(struct bio *bio)
146{
147 return bio->bi_private && !bio->bi_status;
148}
149
150static void
151ext4_trace_read_completion(struct bio *bio)
152{
153 struct page *first_page = bio->bi_io_vec[0].bv_page;
154
155 if (first_page != NULL)
156 trace_android_fs_dataread_end(first_page->mapping->host,
157 page_offset(first_page),
158 bio->bi_iter.bi_size);
159}
160
161/*
162 * I/O completion handler for multipage BIOs.
163 *
164 * The mpage code never puts partial pages into a BIO (except for end-of-file).
165 * If a page does not map to a contiguous run of blocks then it simply falls
166 * back to block_read_full_page().
167 *
168 * Why is this? If a page's completion depends on a number of different BIOs
169 * which can complete in any order (or at the same time) then determining the
170 * status of that page is hard. See end_buffer_async_read() for the details.
171 * There is no point in duplicating all that complexity.
172 */
173static void mpage_end_io(struct bio *bio)
174{
175 if (trace_android_fs_dataread_start_enabled())
176 ext4_trace_read_completion(bio);
177
178 if (bio_post_read_required(bio)) {
179 struct bio_post_read_ctx *ctx = bio->bi_private;
180
181 ctx->cur_step = STEP_INITIAL;
182 bio_post_read_processing(ctx);
183 return;
184 }
185 __read_end_io(bio);
186}
187
188static inline bool ext4_need_verity(const struct inode *inode, pgoff_t idx)
189{
190 return fsverity_active(inode) &&
191 idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
192}
193
194static struct bio_post_read_ctx *get_bio_post_read_ctx(struct inode *inode,
195 struct bio *bio,
196 pgoff_t first_idx)
197{
198 unsigned int post_read_steps = 0;
199 struct bio_post_read_ctx *ctx = NULL;
200
201 if (fscrypt_inode_uses_fs_layer_crypto(inode))
202 post_read_steps |= 1 << STEP_DECRYPT;
203
204 if (ext4_need_verity(inode, first_idx))
205 post_read_steps |= 1 << STEP_VERITY;
206
207 if (post_read_steps) {
208 ctx = mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
209 if (!ctx)
210 return ERR_PTR(-ENOMEM);
211 ctx->bio = bio;
212 ctx->enabled_steps = post_read_steps;
213 bio->bi_private = ctx;
214 }
215 return ctx;
216}
217
218static inline loff_t ext4_readpage_limit(struct inode *inode)
219{
220 if (IS_ENABLED(CONFIG_FS_VERITY) &&
221 (IS_VERITY(inode) || ext4_verity_in_progress(inode)))
222 return inode->i_sb->s_maxbytes;
223
224 return i_size_read(inode);
225}
226
227static void
228ext4_submit_bio_read(struct bio *bio)
229{
230 if (trace_android_fs_dataread_start_enabled()) {
231 struct page *first_page = bio->bi_io_vec[0].bv_page;
232
233 if (first_page != NULL) {
234 char *path, pathbuf[MAX_TRACE_PATHBUF_LEN];
235
236 path = android_fstrace_get_pathname(pathbuf,
237 MAX_TRACE_PATHBUF_LEN,
238 first_page->mapping->host);
239 trace_android_fs_dataread_start(
240 first_page->mapping->host,
241 page_offset(first_page),
242 bio->bi_iter.bi_size,
243 current->pid,
244 path,
245 current->comm);
246 }
247 }
248 submit_bio(bio);
249}
250
251int ext4_mpage_readpages(struct address_space *mapping,
252 struct list_head *pages, struct page *page,
253 unsigned nr_pages, bool is_readahead)
254{
255 struct bio *bio = NULL;
256 sector_t last_block_in_bio = 0;
257
258 struct inode *inode = mapping->host;
259 const unsigned blkbits = inode->i_blkbits;
260 const unsigned blocks_per_page = PAGE_SIZE >> blkbits;
261 const unsigned blocksize = 1 << blkbits;
262 sector_t next_block;
263 sector_t block_in_file;
264 sector_t last_block;
265 sector_t last_block_in_file;
266 sector_t blocks[MAX_BUF_PER_PAGE];
267 unsigned page_block;
268 struct block_device *bdev = inode->i_sb->s_bdev;
269 int length;
270 unsigned relative_block = 0;
271 struct ext4_map_blocks map;
272
273 map.m_pblk = 0;
274 map.m_lblk = 0;
275 map.m_len = 0;
276 map.m_flags = 0;
277
278 for (; nr_pages; nr_pages--) {
279 int fully_mapped = 1;
280 unsigned first_hole = blocks_per_page;
281
282 prefetchw(&page->flags);
283 if (pages) {
284 page = list_entry(pages->prev, struct page, lru);
285 list_del(&page->lru);
286 if (add_to_page_cache_lru(page, mapping, page->index,
287 readahead_gfp_mask(mapping)))
288 goto next_page;
289 }
290
291 if (page_has_buffers(page))
292 goto confused;
293
294 block_in_file = next_block =
295 (sector_t)page->index << (PAGE_SHIFT - blkbits);
296 last_block = block_in_file + nr_pages * blocks_per_page;
297 last_block_in_file = (ext4_readpage_limit(inode) +
298 blocksize - 1) >> blkbits;
299 if (last_block > last_block_in_file)
300 last_block = last_block_in_file;
301 page_block = 0;
302
303 /*
304 * Map blocks using the previous result first.
305 */
306 if ((map.m_flags & EXT4_MAP_MAPPED) &&
307 block_in_file > map.m_lblk &&
308 block_in_file < (map.m_lblk + map.m_len)) {
309 unsigned map_offset = block_in_file - map.m_lblk;
310 unsigned last = map.m_len - map_offset;
311
312 for (relative_block = 0; ; relative_block++) {
313 if (relative_block == last) {
314 /* needed? */
315 map.m_flags &= ~EXT4_MAP_MAPPED;
316 break;
317 }
318 if (page_block == blocks_per_page)
319 break;
320 blocks[page_block] = map.m_pblk + map_offset +
321 relative_block;
322 page_block++;
323 block_in_file++;
324 }
325 }
326
327 /*
328 * Then do more ext4_map_blocks() calls until we are
329 * done with this page.
330 */
331 while (page_block < blocks_per_page) {
332 if (block_in_file < last_block) {
333 map.m_lblk = block_in_file;
334 map.m_len = last_block - block_in_file;
335
336 if (ext4_map_blocks(NULL, inode, &map, 0) < 0) {
337 set_error_page:
338 SetPageError(page);
339 zero_user_segment(page, 0,
340 PAGE_SIZE);
341 unlock_page(page);
342 goto next_page;
343 }
344 }
345 if ((map.m_flags & EXT4_MAP_MAPPED) == 0) {
346 fully_mapped = 0;
347 if (first_hole == blocks_per_page)
348 first_hole = page_block;
349 page_block++;
350 block_in_file++;
351 continue;
352 }
353 if (first_hole != blocks_per_page)
354 goto confused; /* hole -> non-hole */
355
356 /* Contiguous blocks? */
357 if (page_block && blocks[page_block-1] != map.m_pblk-1)
358 goto confused;
359 for (relative_block = 0; ; relative_block++) {
360 if (relative_block == map.m_len) {
361 /* needed? */
362 map.m_flags &= ~EXT4_MAP_MAPPED;
363 break;
364 } else if (page_block == blocks_per_page)
365 break;
366 blocks[page_block] = map.m_pblk+relative_block;
367 page_block++;
368 block_in_file++;
369 }
370 }
371 if (first_hole != blocks_per_page) {
372 zero_user_segment(page, first_hole << blkbits,
373 PAGE_SIZE);
374 if (first_hole == 0) {
375 if (ext4_need_verity(inode, page->index) &&
376 !fsverity_verify_page(page))
377 goto set_error_page;
378 SetPageUptodate(page);
379 unlock_page(page);
380 goto next_page;
381 }
382 } else if (fully_mapped) {
383 SetPageMappedToDisk(page);
384 }
385 if (fully_mapped && blocks_per_page == 1 &&
386 !PageUptodate(page) && cleancache_get_page(page) == 0) {
387 SetPageUptodate(page);
388 goto confused;
389 }
390
391 /*
392 * This page will go to BIO. Do we need to send this
393 * BIO off first?
394 */
395 if (bio && (last_block_in_bio != blocks[0] - 1 ||
396 !fscrypt_mergeable_bio(bio, inode, next_block))) {
397 submit_and_realloc:
398 ext4_submit_bio_read(bio);
399 bio = NULL;
400 }
401 if (bio == NULL) {
402 struct bio_post_read_ctx *ctx;
403
404 bio = bio_alloc(GFP_KERNEL,
405 min_t(int, nr_pages, BIO_MAX_PAGES));
406 if (!bio)
407 goto set_error_page;
408 fscrypt_set_bio_crypt_ctx(bio, inode, next_block,
409 GFP_KERNEL);
410 ctx = get_bio_post_read_ctx(inode, bio, page->index);
411 if (IS_ERR(ctx)) {
412 bio_put(bio);
413 bio = NULL;
414 goto set_error_page;
415 }
416 bio_set_dev(bio, bdev);
417 bio->bi_iter.bi_sector = blocks[0] << (blkbits - 9);
418 bio->bi_end_io = mpage_end_io;
419 bio->bi_private = ctx;
420 bio_set_op_attrs(bio, REQ_OP_READ,
421 is_readahead ? REQ_RAHEAD : 0);
422 }
423
424 length = first_hole << blkbits;
425 if (bio_add_page(bio, page, length, 0) < length)
426 goto submit_and_realloc;
427
428 if (((map.m_flags & EXT4_MAP_BOUNDARY) &&
429 (relative_block == map.m_len)) ||
430 (first_hole != blocks_per_page)) {
431 ext4_submit_bio_read(bio);
432 bio = NULL;
433 } else
434 last_block_in_bio = blocks[blocks_per_page - 1];
435 goto next_page;
436 confused:
437 if (bio) {
438 ext4_submit_bio_read(bio);
439 bio = NULL;
440 }
441 if (!PageUptodate(page))
442 block_read_full_page(page, ext4_get_block);
443 else
444 unlock_page(page);
445 next_page:
446 if (pages)
447 put_page(page);
448 }
449 BUG_ON(pages && !list_empty(pages));
450 if (bio)
451 ext4_submit_bio_read(bio);
452 return 0;
453}
454
455int __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
469fail_free_cache:
470 kmem_cache_destroy(bio_post_read_ctx_cache);
471fail:
472 return -ENOMEM;
473}
474
475void 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}