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/blk-crypto.h> 40 #include <linux/blkdev.h> 41 #include <linux/highmem.h> 42 #include <linux/prefetch.h> 43 #include <linux/mpage.h> 44 #include <linux/writeback.h> 45 #include <linux/backing-dev.h> 46 #include <linux/pagevec.h> 47 48 #include "ext4.h" 49 50 #define NUM_PREALLOC_POST_READ_CTXS 128 51 52 static struct kmem_cache *bio_post_read_ctx_cache; 53 static mempool_t *bio_post_read_ctx_pool; 54 55 /* postprocessing steps for read bios */ 56 enum bio_post_read_step { 57 STEP_INITIAL = 0, 58 STEP_DECRYPT, 59 STEP_VERITY, 60 STEP_MAX, 61 }; 62 63 struct 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 70 static void __read_end_io(struct bio *bio) 71 { 72 struct folio_iter fi; 73 74 bio_for_each_folio_all(fi, bio) 75 folio_end_read(fi.folio, bio->bi_status == 0); 76 if (bio->bi_private) 77 mempool_free(bio->bi_private, bio_post_read_ctx_pool); 78 bio_put(bio); 79 } 80 81 static void bio_post_read_processing(struct bio_post_read_ctx *ctx); 82 83 static void decrypt_work(struct work_struct *work) 84 { 85 struct bio_post_read_ctx *ctx = 86 container_of(work, struct bio_post_read_ctx, work); 87 struct bio *bio = ctx->bio; 88 89 if (fscrypt_decrypt_bio(bio)) 90 bio_post_read_processing(ctx); 91 else 92 __read_end_io(bio); 93 } 94 95 static void verity_work(struct work_struct *work) 96 { 97 struct bio_post_read_ctx *ctx = 98 container_of(work, struct bio_post_read_ctx, work); 99 struct bio *bio = ctx->bio; 100 101 /* 102 * fsverity_verify_bio() may call readahead() again, and although verity 103 * will be disabled for that, decryption may still be needed, causing 104 * another bio_post_read_ctx to be allocated. So to guarantee that 105 * mempool_alloc() never deadlocks we must free the current ctx first. 106 * This is safe because verity is the last post-read step. 107 */ 108 BUILD_BUG_ON(STEP_VERITY + 1 != STEP_MAX); 109 mempool_free(ctx, bio_post_read_ctx_pool); 110 bio->bi_private = NULL; 111 112 fsverity_verify_bio(bio); 113 114 __read_end_io(bio); 115 } 116 117 static void bio_post_read_processing(struct bio_post_read_ctx *ctx) 118 { 119 /* 120 * We use different work queues for decryption and for verity because 121 * verity may require reading metadata pages that need decryption, and 122 * we shouldn't recurse to the same workqueue. 123 */ 124 switch (++ctx->cur_step) { 125 case STEP_DECRYPT: 126 if (ctx->enabled_steps & (1 << STEP_DECRYPT)) { 127 INIT_WORK(&ctx->work, decrypt_work); 128 fscrypt_enqueue_decrypt_work(&ctx->work); 129 return; 130 } 131 ctx->cur_step++; 132 fallthrough; 133 case STEP_VERITY: 134 if (ctx->enabled_steps & (1 << STEP_VERITY)) { 135 INIT_WORK(&ctx->work, verity_work); 136 fsverity_enqueue_verify_work(&ctx->work); 137 return; 138 } 139 ctx->cur_step++; 140 fallthrough; 141 default: 142 __read_end_io(ctx->bio); 143 } 144 } 145 146 static bool bio_post_read_required(struct bio *bio) 147 { 148 return bio->bi_private && !bio->bi_status; 149 } 150 151 /* 152 * I/O completion handler for multipage BIOs. 153 * 154 * The mpage code never puts partial pages into a BIO (except for end-of-file). 155 * If a page does not map to a contiguous run of blocks then it simply falls 156 * back to block_read_full_folio(). 157 * 158 * Why is this? If a page's completion depends on a number of different BIOs 159 * which can complete in any order (or at the same time) then determining the 160 * status of that page is hard. See end_buffer_async_read() for the details. 161 * There is no point in duplicating all that complexity. 162 */ 163 static void mpage_end_io(struct bio *bio) 164 { 165 if (bio_post_read_required(bio)) { 166 struct bio_post_read_ctx *ctx = bio->bi_private; 167 168 ctx->cur_step = STEP_INITIAL; 169 bio_post_read_processing(ctx); 170 return; 171 } 172 __read_end_io(bio); 173 } 174 175 static inline bool ext4_need_verity(const struct inode *inode, pgoff_t idx) 176 { 177 return fsverity_active(inode) && 178 idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE); 179 } 180 181 static void ext4_set_bio_post_read_ctx(struct bio *bio, 182 const struct inode *inode, 183 pgoff_t first_idx) 184 { 185 unsigned int post_read_steps = 0; 186 187 if (fscrypt_inode_uses_fs_layer_crypto(inode)) 188 post_read_steps |= 1 << STEP_DECRYPT; 189 190 if (ext4_need_verity(inode, first_idx)) 191 post_read_steps |= 1 << STEP_VERITY; 192 193 if (post_read_steps) { 194 /* Due to the mempool, this never fails. */ 195 struct bio_post_read_ctx *ctx = 196 mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS); 197 198 ctx->bio = bio; 199 ctx->enabled_steps = post_read_steps; 200 bio->bi_private = ctx; 201 } 202 } 203 204 static inline loff_t ext4_readpage_limit(struct inode *inode) 205 { 206 if (IS_ENABLED(CONFIG_FS_VERITY) && IS_VERITY(inode)) 207 return inode->i_sb->s_maxbytes; 208 209 return i_size_read(inode); 210 } 211 212 int ext4_mpage_readpages(struct inode *inode, 213 struct readahead_control *rac, struct folio *folio) 214 { 215 struct bio *bio = NULL; 216 sector_t last_block_in_bio = 0; 217 const unsigned blkbits = inode->i_blkbits; 218 const unsigned blocksize = 1 << blkbits; 219 sector_t next_block; 220 sector_t block_in_file; 221 sector_t last_block; 222 sector_t last_block_in_file; 223 sector_t first_block; 224 unsigned page_block; 225 struct block_device *bdev = inode->i_sb->s_bdev; 226 int length; 227 unsigned relative_block = 0; 228 struct ext4_map_blocks map; 229 unsigned int nr_pages, folio_pages; 230 231 map.m_pblk = 0; 232 map.m_lblk = 0; 233 map.m_len = 0; 234 map.m_flags = 0; 235 236 nr_pages = rac ? readahead_count(rac) : folio_nr_pages(folio); 237 for (; nr_pages; nr_pages -= folio_pages) { 238 int fully_mapped = 1; 239 unsigned int first_hole; 240 unsigned int blocks_per_folio; 241 242 if (rac) 243 folio = readahead_folio(rac); 244 245 folio_pages = folio_nr_pages(folio); 246 prefetchw(&folio->flags); 247 248 if (folio_buffers(folio)) 249 goto confused; 250 251 blocks_per_folio = folio_size(folio) >> blkbits; 252 first_hole = blocks_per_folio; 253 block_in_file = next_block = EXT4_PG_TO_LBLK(inode, folio->index); 254 last_block = EXT4_PG_TO_LBLK(inode, folio->index + nr_pages); 255 last_block_in_file = (ext4_readpage_limit(inode) + 256 blocksize - 1) >> blkbits; 257 if (last_block > last_block_in_file) 258 last_block = last_block_in_file; 259 page_block = 0; 260 261 /* 262 * Map blocks using the previous result first. 263 */ 264 if ((map.m_flags & EXT4_MAP_MAPPED) && 265 block_in_file > map.m_lblk && 266 block_in_file < (map.m_lblk + map.m_len)) { 267 unsigned map_offset = block_in_file - map.m_lblk; 268 unsigned last = map.m_len - map_offset; 269 270 first_block = map.m_pblk + map_offset; 271 for (relative_block = 0; ; relative_block++) { 272 if (relative_block == last) { 273 /* needed? */ 274 map.m_flags &= ~EXT4_MAP_MAPPED; 275 break; 276 } 277 if (page_block == blocks_per_folio) 278 break; 279 page_block++; 280 block_in_file++; 281 } 282 } 283 284 /* 285 * Then do more ext4_map_blocks() calls until we are 286 * done with this folio. 287 */ 288 while (page_block < blocks_per_folio) { 289 if (block_in_file < last_block) { 290 map.m_lblk = block_in_file; 291 map.m_len = last_block - block_in_file; 292 293 if (ext4_map_blocks(NULL, inode, &map, 0) < 0) { 294 set_error_page: 295 folio_zero_segment(folio, 0, 296 folio_size(folio)); 297 folio_unlock(folio); 298 goto next_page; 299 } 300 } 301 if ((map.m_flags & EXT4_MAP_MAPPED) == 0) { 302 fully_mapped = 0; 303 if (first_hole == blocks_per_folio) 304 first_hole = page_block; 305 page_block++; 306 block_in_file++; 307 continue; 308 } 309 if (first_hole != blocks_per_folio) 310 goto confused; /* hole -> non-hole */ 311 312 /* Contiguous blocks? */ 313 if (!page_block) 314 first_block = map.m_pblk; 315 else if (first_block + page_block != map.m_pblk) 316 goto confused; 317 for (relative_block = 0; ; relative_block++) { 318 if (relative_block == map.m_len) { 319 /* needed? */ 320 map.m_flags &= ~EXT4_MAP_MAPPED; 321 break; 322 } else if (page_block == blocks_per_folio) 323 break; 324 page_block++; 325 block_in_file++; 326 } 327 } 328 if (first_hole != blocks_per_folio) { 329 folio_zero_segment(folio, first_hole << blkbits, 330 folio_size(folio)); 331 if (first_hole == 0) { 332 if (ext4_need_verity(inode, folio->index) && 333 !fsverity_verify_folio(folio)) 334 goto set_error_page; 335 folio_end_read(folio, true); 336 continue; 337 } 338 } else if (fully_mapped) { 339 folio_set_mappedtodisk(folio); 340 } 341 342 /* 343 * This folio will go to BIO. Do we need to send this 344 * BIO off first? 345 */ 346 if (bio && (last_block_in_bio != first_block - 1 || 347 !fscrypt_mergeable_bio(bio, inode, next_block))) { 348 submit_and_realloc: 349 blk_crypto_submit_bio(bio); 350 bio = NULL; 351 } 352 if (bio == NULL) { 353 /* 354 * bio_alloc will _always_ be able to allocate a bio if 355 * __GFP_DIRECT_RECLAIM is set, see bio_alloc_bioset(). 356 */ 357 bio = bio_alloc(bdev, bio_max_segs(nr_pages), 358 REQ_OP_READ, GFP_KERNEL); 359 fscrypt_set_bio_crypt_ctx(bio, inode, next_block, 360 GFP_KERNEL); 361 ext4_set_bio_post_read_ctx(bio, inode, folio->index); 362 bio->bi_iter.bi_sector = first_block << (blkbits - 9); 363 bio->bi_end_io = mpage_end_io; 364 if (rac) 365 bio->bi_opf |= REQ_RAHEAD; 366 } 367 368 length = first_hole << blkbits; 369 if (!bio_add_folio(bio, folio, length, 0)) 370 goto submit_and_realloc; 371 372 if (((map.m_flags & EXT4_MAP_BOUNDARY) && 373 (relative_block == map.m_len)) || 374 (first_hole != blocks_per_folio)) { 375 blk_crypto_submit_bio(bio); 376 bio = NULL; 377 } else 378 last_block_in_bio = first_block + blocks_per_folio - 1; 379 continue; 380 confused: 381 if (bio) { 382 blk_crypto_submit_bio(bio); 383 bio = NULL; 384 } 385 if (!folio_test_uptodate(folio)) 386 block_read_full_folio(folio, ext4_get_block); 387 else 388 folio_unlock(folio); 389 next_page: 390 ; /* A label shall be followed by a statement until C23 */ 391 } 392 if (bio) 393 blk_crypto_submit_bio(bio); 394 return 0; 395 } 396 397 int __init ext4_init_post_read_processing(void) 398 { 399 bio_post_read_ctx_cache = KMEM_CACHE(bio_post_read_ctx, SLAB_RECLAIM_ACCOUNT); 400 401 if (!bio_post_read_ctx_cache) 402 goto fail; 403 bio_post_read_ctx_pool = 404 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS, 405 bio_post_read_ctx_cache); 406 if (!bio_post_read_ctx_pool) 407 goto fail_free_cache; 408 return 0; 409 410 fail_free_cache: 411 kmem_cache_destroy(bio_post_read_ctx_cache); 412 fail: 413 return -ENOMEM; 414 } 415 416 void ext4_exit_post_read_processing(void) 417 { 418 mempool_destroy(bio_post_read_ctx_pool); 419 kmem_cache_destroy(bio_post_read_ctx_cache); 420 } 421