blob: 3e7d160f543f0cc6b5daddaec3f4785086c52c7a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Theodore Ts'of64e02f2015-04-08 00:00:32 -04002/*
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 *
Matthew Wilcox (Oracle)6311f91f2020-06-01 21:47:16 -070010 * The ext4_mpage_readpages() function here is intended to
11 * replace mpage_readahead() in the general case, not just for
Theodore Ts'of64e02f2015-04-08 00:00:32 -040012 * 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:
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +030027 * the end-of-file on blocksize < PAGE_SIZE setups.
Theodore Ts'of64e02f2015-04-08 00:00:32 -040028 *
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>
Theodore Ts'of64e02f2015-04-08 00:00:32 -040046
47#include "ext4.h"
48
Eric Biggers22cfe4b2019-07-22 09:26:24 -070049#define NUM_PREALLOC_POST_READ_CTXS 128
50
51static struct kmem_cache *bio_post_read_ctx_cache;
52static mempool_t *bio_post_read_ctx_pool;
53
54/* postprocessing steps for read bios */
55enum bio_post_read_step {
56 STEP_INITIAL = 0,
57 STEP_DECRYPT,
58 STEP_VERITY,
Eric Biggers68e45332019-12-31 12:12:22 -060059 STEP_MAX,
Eric Biggers22cfe4b2019-07-22 09:26:24 -070060};
61
62struct bio_post_read_ctx {
63 struct bio *bio;
64 struct work_struct work;
65 unsigned int cur_step;
66 unsigned int enabled_steps;
67};
68
69static void __read_end_io(struct bio *bio)
Michael Halcrowc9c74292015-04-12 00:56:10 -040070{
Matthew Wilcoxf2b229a2023-03-24 18:01:26 +000071 struct folio_iter fi;
Eric Biggers22cfe4b2019-07-22 09:26:24 -070072
Matthew Wilcoxf2b229a2023-03-24 18:01:26 +000073 bio_for_each_folio_all(fi, bio) {
74 struct folio *folio = fi.folio;
Eric Biggers22cfe4b2019-07-22 09:26:24 -070075
Eric Biggers98dc08b2022-11-28 23:04:01 -080076 if (bio->bi_status)
Matthew Wilcoxf2b229a2023-03-24 18:01:26 +000077 folio_clear_uptodate(folio);
Eric Biggers98dc08b2022-11-28 23:04:01 -080078 else
Matthew Wilcoxf2b229a2023-03-24 18:01:26 +000079 folio_mark_uptodate(folio);
80 folio_unlock(folio);
Eric Biggers22cfe4b2019-07-22 09:26:24 -070081 }
82 if (bio->bi_private)
83 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
84 bio_put(bio);
85}
86
87static void bio_post_read_processing(struct bio_post_read_ctx *ctx);
88
89static void decrypt_work(struct work_struct *work)
90{
91 struct bio_post_read_ctx *ctx =
92 container_of(work, struct bio_post_read_ctx, work);
Eric Biggers14db0b32022-08-15 16:50:51 -070093 struct bio *bio = ctx->bio;
Eric Biggers22cfe4b2019-07-22 09:26:24 -070094
Eric Biggers14db0b32022-08-15 16:50:51 -070095 if (fscrypt_decrypt_bio(bio))
96 bio_post_read_processing(ctx);
97 else
98 __read_end_io(bio);
Eric Biggers22cfe4b2019-07-22 09:26:24 -070099}
100
101static void verity_work(struct work_struct *work)
102{
103 struct bio_post_read_ctx *ctx =
104 container_of(work, struct bio_post_read_ctx, work);
Eric Biggers68e45332019-12-31 12:12:22 -0600105 struct bio *bio = ctx->bio;
Eric Biggers22cfe4b2019-07-22 09:26:24 -0700106
Eric Biggers68e45332019-12-31 12:12:22 -0600107 /*
Matthew Wilcox (Oracle)704528d2022-03-23 21:29:04 -0400108 * fsverity_verify_bio() may call readahead() again, and although verity
Eric Biggers68e45332019-12-31 12:12:22 -0600109 * will be disabled for that, decryption may still be needed, causing
110 * another bio_post_read_ctx to be allocated. So to guarantee that
111 * mempool_alloc() never deadlocks we must free the current ctx first.
112 * This is safe because verity is the last post-read step.
113 */
114 BUILD_BUG_ON(STEP_VERITY + 1 != STEP_MAX);
115 mempool_free(ctx, bio_post_read_ctx_pool);
116 bio->bi_private = NULL;
Eric Biggers22cfe4b2019-07-22 09:26:24 -0700117
Eric Biggers68e45332019-12-31 12:12:22 -0600118 fsverity_verify_bio(bio);
119
120 __read_end_io(bio);
Eric Biggers22cfe4b2019-07-22 09:26:24 -0700121}
122
123static void bio_post_read_processing(struct bio_post_read_ctx *ctx)
124{
125 /*
126 * We use different work queues for decryption and for verity because
127 * verity may require reading metadata pages that need decryption, and
128 * we shouldn't recurse to the same workqueue.
129 */
130 switch (++ctx->cur_step) {
131 case STEP_DECRYPT:
132 if (ctx->enabled_steps & (1 << STEP_DECRYPT)) {
133 INIT_WORK(&ctx->work, decrypt_work);
134 fscrypt_enqueue_decrypt_work(&ctx->work);
135 return;
136 }
137 ctx->cur_step++;
Shijie Luo70d7ced2020-08-10 07:44:35 -0400138 fallthrough;
Eric Biggers22cfe4b2019-07-22 09:26:24 -0700139 case STEP_VERITY:
140 if (ctx->enabled_steps & (1 << STEP_VERITY)) {
141 INIT_WORK(&ctx->work, verity_work);
142 fsverity_enqueue_verify_work(&ctx->work);
143 return;
144 }
145 ctx->cur_step++;
Shijie Luo70d7ced2020-08-10 07:44:35 -0400146 fallthrough;
Eric Biggers22cfe4b2019-07-22 09:26:24 -0700147 default:
148 __read_end_io(ctx->bio);
149 }
150}
151
152static bool bio_post_read_required(struct bio *bio)
153{
154 return bio->bi_private && !bio->bi_status;
Michael Halcrowc9c74292015-04-12 00:56:10 -0400155}
156
157/*
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400158 * I/O completion handler for multipage BIOs.
159 *
160 * The mpage code never puts partial pages into a BIO (except for end-of-file).
161 * If a page does not map to a contiguous run of blocks then it simply falls
Matthew Wilcox (Oracle)2c69e202022-04-29 10:40:40 -0400162 * back to block_read_full_folio().
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400163 *
164 * Why is this? If a page's completion depends on a number of different BIOs
165 * which can complete in any order (or at the same time) then determining the
166 * status of that page is hard. See end_buffer_async_read() for the details.
167 * There is no point in duplicating all that complexity.
168 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200169static void mpage_end_io(struct bio *bio)
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400170{
Eric Biggers22cfe4b2019-07-22 09:26:24 -0700171 if (bio_post_read_required(bio)) {
172 struct bio_post_read_ctx *ctx = bio->bi_private;
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400173
Eric Biggers22cfe4b2019-07-22 09:26:24 -0700174 ctx->cur_step = STEP_INITIAL;
175 bio_post_read_processing(ctx);
176 return;
Michael Halcrowc9c74292015-04-12 00:56:10 -0400177 }
Eric Biggers22cfe4b2019-07-22 09:26:24 -0700178 __read_end_io(bio);
179}
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400180
Eric Biggers22cfe4b2019-07-22 09:26:24 -0700181static inline bool ext4_need_verity(const struct inode *inode, pgoff_t idx)
182{
183 return fsverity_active(inode) &&
184 idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
185}
186
Eric Biggersfd5fe252019-12-31 12:12:56 -0600187static void ext4_set_bio_post_read_ctx(struct bio *bio,
188 const struct inode *inode,
189 pgoff_t first_idx)
Eric Biggers22cfe4b2019-07-22 09:26:24 -0700190{
191 unsigned int post_read_steps = 0;
Eric Biggers22cfe4b2019-07-22 09:26:24 -0700192
Eric Biggers4f74d152020-07-02 01:56:07 +0000193 if (fscrypt_inode_uses_fs_layer_crypto(inode))
Eric Biggers22cfe4b2019-07-22 09:26:24 -0700194 post_read_steps |= 1 << STEP_DECRYPT;
195
196 if (ext4_need_verity(inode, first_idx))
197 post_read_steps |= 1 << STEP_VERITY;
198
199 if (post_read_steps) {
Eric Biggersfd5fe252019-12-31 12:12:56 -0600200 /* Due to the mempool, this never fails. */
201 struct bio_post_read_ctx *ctx =
202 mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
203
Eric Biggers22cfe4b2019-07-22 09:26:24 -0700204 ctx->bio = bio;
205 ctx->enabled_steps = post_read_steps;
206 bio->bi_private = ctx;
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400207 }
Eric Biggers22cfe4b2019-07-22 09:26:24 -0700208}
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400209
Eric Biggers22cfe4b2019-07-22 09:26:24 -0700210static inline loff_t ext4_readpage_limit(struct inode *inode)
211{
Eric Biggers5e122142022-12-23 12:36:35 -0800212 if (IS_ENABLED(CONFIG_FS_VERITY) && IS_VERITY(inode))
Eric Biggers22cfe4b2019-07-22 09:26:24 -0700213 return inode->i_sb->s_maxbytes;
214
215 return i_size_read(inode);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400216}
217
Matthew Wilcox (Oracle)a07f6242020-06-01 21:47:20 -0700218int ext4_mpage_readpages(struct inode *inode,
Matthew Wilcoxc0be8e62023-03-24 18:01:23 +0000219 struct readahead_control *rac, struct folio *folio)
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400220{
221 struct bio *bio = NULL;
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400222 sector_t last_block_in_bio = 0;
223
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400224 const unsigned blkbits = inode->i_blkbits;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300225 const unsigned blocks_per_page = PAGE_SIZE >> blkbits;
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400226 const unsigned blocksize = 1 << blkbits;
Eric Biggers4f74d152020-07-02 01:56:07 +0000227 sector_t next_block;
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400228 sector_t block_in_file;
229 sector_t last_block;
230 sector_t last_block_in_file;
231 sector_t blocks[MAX_BUF_PER_PAGE];
232 unsigned page_block;
233 struct block_device *bdev = inode->i_sb->s_bdev;
234 int length;
235 unsigned relative_block = 0;
236 struct ext4_map_blocks map;
Matthew Wilcox (Oracle)6311f91f2020-06-01 21:47:16 -0700237 unsigned int nr_pages = rac ? readahead_count(rac) : 1;
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400238
239 map.m_pblk = 0;
240 map.m_lblk = 0;
241 map.m_len = 0;
242 map.m_flags = 0;
243
yalin wangde9e9182016-07-05 16:32:32 -0400244 for (; nr_pages; nr_pages--) {
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400245 int fully_mapped = 1;
246 unsigned first_hole = blocks_per_page;
247
Matthew Wilcoxc0be8e62023-03-24 18:01:23 +0000248 if (rac)
249 folio = readahead_folio(rac);
250 prefetchw(&folio->flags);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400251
Matthew Wilcoxc0be8e62023-03-24 18:01:23 +0000252 if (folio_buffers(folio))
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400253 goto confused;
254
Eric Biggers4f74d152020-07-02 01:56:07 +0000255 block_in_file = next_block =
Matthew Wilcoxc0be8e62023-03-24 18:01:23 +0000256 (sector_t)folio->index << (PAGE_SHIFT - blkbits);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400257 last_block = block_in_file + nr_pages * blocks_per_page;
Eric Biggers22cfe4b2019-07-22 09:26:24 -0700258 last_block_in_file = (ext4_readpage_limit(inode) +
259 blocksize - 1) >> blkbits;
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400260 if (last_block > last_block_in_file)
261 last_block = last_block_in_file;
262 page_block = 0;
263
264 /*
265 * Map blocks using the previous result first.
266 */
267 if ((map.m_flags & EXT4_MAP_MAPPED) &&
268 block_in_file > map.m_lblk &&
269 block_in_file < (map.m_lblk + map.m_len)) {
270 unsigned map_offset = block_in_file - map.m_lblk;
271 unsigned last = map.m_len - map_offset;
272
273 for (relative_block = 0; ; relative_block++) {
274 if (relative_block == last) {
275 /* needed? */
276 map.m_flags &= ~EXT4_MAP_MAPPED;
277 break;
278 }
279 if (page_block == blocks_per_page)
280 break;
281 blocks[page_block] = map.m_pblk + map_offset +
282 relative_block;
283 page_block++;
284 block_in_file++;
285 }
286 }
287
288 /*
289 * Then do more ext4_map_blocks() calls until we are
Matthew Wilcoxc0be8e62023-03-24 18:01:23 +0000290 * done with this folio.
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400291 */
292 while (page_block < blocks_per_page) {
293 if (block_in_file < last_block) {
294 map.m_lblk = block_in_file;
295 map.m_len = last_block - block_in_file;
296
297 if (ext4_map_blocks(NULL, inode, &map, 0) < 0) {
298 set_error_page:
Matthew Wilcoxc0be8e62023-03-24 18:01:23 +0000299 folio_set_error(folio);
300 folio_zero_segment(folio, 0,
301 folio_size(folio));
302 folio_unlock(folio);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400303 goto next_page;
304 }
305 }
306 if ((map.m_flags & EXT4_MAP_MAPPED) == 0) {
307 fully_mapped = 0;
308 if (first_hole == blocks_per_page)
309 first_hole = page_block;
310 page_block++;
311 block_in_file++;
312 continue;
313 }
314 if (first_hole != blocks_per_page)
315 goto confused; /* hole -> non-hole */
316
317 /* Contiguous blocks? */
318 if (page_block && blocks[page_block-1] != map.m_pblk-1)
319 goto confused;
320 for (relative_block = 0; ; relative_block++) {
321 if (relative_block == map.m_len) {
322 /* needed? */
323 map.m_flags &= ~EXT4_MAP_MAPPED;
324 break;
325 } else if (page_block == blocks_per_page)
326 break;
327 blocks[page_block] = map.m_pblk+relative_block;
328 page_block++;
329 block_in_file++;
330 }
331 }
332 if (first_hole != blocks_per_page) {
Matthew Wilcoxc0be8e62023-03-24 18:01:23 +0000333 folio_zero_segment(folio, first_hole << blkbits,
334 folio_size(folio));
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400335 if (first_hole == 0) {
Matthew Wilcoxc0be8e62023-03-24 18:01:23 +0000336 if (ext4_need_verity(inode, folio->index) &&
Matthew Wilcox0dea40a2023-05-16 20:27:13 +0100337 !fsverity_verify_folio(folio))
Eric Biggers22cfe4b2019-07-22 09:26:24 -0700338 goto set_error_page;
Matthew Wilcoxc0be8e62023-03-24 18:01:23 +0000339 folio_mark_uptodate(folio);
340 folio_unlock(folio);
341 continue;
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400342 }
343 } else if (fully_mapped) {
Matthew Wilcoxc0be8e62023-03-24 18:01:23 +0000344 folio_set_mappedtodisk(folio);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400345 }
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400346
347 /*
Matthew Wilcoxc0be8e62023-03-24 18:01:23 +0000348 * This folio will go to BIO. Do we need to send this
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400349 * BIO off first?
350 */
Eric Biggers4f74d152020-07-02 01:56:07 +0000351 if (bio && (last_block_in_bio != blocks[0] - 1 ||
352 !fscrypt_mergeable_bio(bio, inode, next_block))) {
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400353 submit_and_realloc:
Mike Christie4e49ea42016-06-05 14:31:41 -0500354 submit_bio(bio);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400355 bio = NULL;
356 }
357 if (bio == NULL) {
Gao Xiang55002212019-10-31 17:23:15 +0800358 /*
359 * bio_alloc will _always_ be able to allocate a bio if
360 * __GFP_DIRECT_RECLAIM is set, see bio_alloc_bioset().
361 */
Christoph Hellwig07888c662022-01-24 10:11:05 +0100362 bio = bio_alloc(bdev, bio_max_segs(nr_pages),
363 REQ_OP_READ, GFP_KERNEL);
Eric Biggers4f74d152020-07-02 01:56:07 +0000364 fscrypt_set_bio_crypt_ctx(bio, inode, next_block,
365 GFP_KERNEL);
Matthew Wilcoxc0be8e62023-03-24 18:01:23 +0000366 ext4_set_bio_post_read_ctx(bio, inode, folio->index);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400367 bio->bi_iter.bi_sector = blocks[0] << (blkbits - 9);
368 bio->bi_end_io = mpage_end_io;
Christoph Hellwig07888c662022-01-24 10:11:05 +0100369 if (rac)
370 bio->bi_opf |= REQ_RAHEAD;
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400371 }
372
373 length = first_hole << blkbits;
Matthew Wilcoxc0be8e62023-03-24 18:01:23 +0000374 if (!bio_add_folio(bio, folio, length, 0))
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400375 goto submit_and_realloc;
376
377 if (((map.m_flags & EXT4_MAP_BOUNDARY) &&
378 (relative_block == map.m_len)) ||
379 (first_hole != blocks_per_page)) {
Mike Christie4e49ea42016-06-05 14:31:41 -0500380 submit_bio(bio);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400381 bio = NULL;
382 } else
383 last_block_in_bio = blocks[blocks_per_page - 1];
Matthew Wilcoxc0be8e62023-03-24 18:01:23 +0000384 continue;
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400385 confused:
386 if (bio) {
Mike Christie4e49ea42016-06-05 14:31:41 -0500387 submit_bio(bio);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400388 bio = NULL;
389 }
Matthew Wilcoxc0be8e62023-03-24 18:01:23 +0000390 if (!folio_test_uptodate(folio))
391 block_read_full_folio(folio, ext4_get_block);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400392 else
Matthew Wilcoxc0be8e62023-03-24 18:01:23 +0000393 folio_unlock(folio);
394next_page:
395 ; /* A label shall be followed by a statement until C23 */
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400396 }
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400397 if (bio)
Mike Christie4e49ea42016-06-05 14:31:41 -0500398 submit_bio(bio);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400399 return 0;
400}
Eric Biggers22cfe4b2019-07-22 09:26:24 -0700401
402int __init ext4_init_post_read_processing(void)
403{
JunChao Sun060f7732022-11-09 07:38:22 -0800404 bio_post_read_ctx_cache = KMEM_CACHE(bio_post_read_ctx, SLAB_RECLAIM_ACCOUNT);
405
Eric Biggers22cfe4b2019-07-22 09:26:24 -0700406 if (!bio_post_read_ctx_cache)
407 goto fail;
408 bio_post_read_ctx_pool =
409 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
410 bio_post_read_ctx_cache);
411 if (!bio_post_read_ctx_pool)
412 goto fail_free_cache;
413 return 0;
414
415fail_free_cache:
416 kmem_cache_destroy(bio_post_read_ctx_cache);
417fail:
418 return -ENOMEM;
419}
420
421void ext4_exit_post_read_processing(void)
422{
423 mempool_destroy(bio_post_read_ctx_pool);
424 kmem_cache_destroy(bio_post_read_ctx_cache);
425}