0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031 #include <linux/kernel.h>
0032 #include <linux/export.h>
0033 #include <linux/mm.h>
0034 #include <linux/kdev_t.h>
0035 #include <linux/gfp.h>
0036 #include <linux/bio.h>
0037 #include <linux/fs.h>
0038 #include <linux/buffer_head.h>
0039 #include <linux/blkdev.h>
0040 #include <linux/highmem.h>
0041 #include <linux/prefetch.h>
0042 #include <linux/mpage.h>
0043 #include <linux/writeback.h>
0044 #include <linux/backing-dev.h>
0045 #include <linux/pagevec.h>
0046
0047 #include "ext4.h"
0048
0049 #define NUM_PREALLOC_POST_READ_CTXS 128
0050
0051 static struct kmem_cache *bio_post_read_ctx_cache;
0052 static mempool_t *bio_post_read_ctx_pool;
0053
0054
0055 enum bio_post_read_step {
0056 STEP_INITIAL = 0,
0057 STEP_DECRYPT,
0058 STEP_VERITY,
0059 STEP_MAX,
0060 };
0061
0062 struct bio_post_read_ctx {
0063 struct bio *bio;
0064 struct work_struct work;
0065 unsigned int cur_step;
0066 unsigned int enabled_steps;
0067 };
0068
0069 static void __read_end_io(struct bio *bio)
0070 {
0071 struct page *page;
0072 struct bio_vec *bv;
0073 struct bvec_iter_all iter_all;
0074
0075 bio_for_each_segment_all(bv, bio, iter_all) {
0076 page = bv->bv_page;
0077
0078
0079 if (bio->bi_status || PageError(page)) {
0080 ClearPageUptodate(page);
0081
0082 ClearPageError(page);
0083 } else {
0084 SetPageUptodate(page);
0085 }
0086 unlock_page(page);
0087 }
0088 if (bio->bi_private)
0089 mempool_free(bio->bi_private, bio_post_read_ctx_pool);
0090 bio_put(bio);
0091 }
0092
0093 static void bio_post_read_processing(struct bio_post_read_ctx *ctx);
0094
0095 static void decrypt_work(struct work_struct *work)
0096 {
0097 struct bio_post_read_ctx *ctx =
0098 container_of(work, struct bio_post_read_ctx, work);
0099
0100 fscrypt_decrypt_bio(ctx->bio);
0101
0102 bio_post_read_processing(ctx);
0103 }
0104
0105 static void verity_work(struct work_struct *work)
0106 {
0107 struct bio_post_read_ctx *ctx =
0108 container_of(work, struct bio_post_read_ctx, work);
0109 struct bio *bio = ctx->bio;
0110
0111
0112
0113
0114
0115
0116
0117
0118 BUILD_BUG_ON(STEP_VERITY + 1 != STEP_MAX);
0119 mempool_free(ctx, bio_post_read_ctx_pool);
0120 bio->bi_private = NULL;
0121
0122 fsverity_verify_bio(bio);
0123
0124 __read_end_io(bio);
0125 }
0126
0127 static void bio_post_read_processing(struct bio_post_read_ctx *ctx)
0128 {
0129
0130
0131
0132
0133
0134 switch (++ctx->cur_step) {
0135 case STEP_DECRYPT:
0136 if (ctx->enabled_steps & (1 << STEP_DECRYPT)) {
0137 INIT_WORK(&ctx->work, decrypt_work);
0138 fscrypt_enqueue_decrypt_work(&ctx->work);
0139 return;
0140 }
0141 ctx->cur_step++;
0142 fallthrough;
0143 case STEP_VERITY:
0144 if (ctx->enabled_steps & (1 << STEP_VERITY)) {
0145 INIT_WORK(&ctx->work, verity_work);
0146 fsverity_enqueue_verify_work(&ctx->work);
0147 return;
0148 }
0149 ctx->cur_step++;
0150 fallthrough;
0151 default:
0152 __read_end_io(ctx->bio);
0153 }
0154 }
0155
0156 static bool bio_post_read_required(struct bio *bio)
0157 {
0158 return bio->bi_private && !bio->bi_status;
0159 }
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173 static void mpage_end_io(struct bio *bio)
0174 {
0175 if (bio_post_read_required(bio)) {
0176 struct bio_post_read_ctx *ctx = bio->bi_private;
0177
0178 ctx->cur_step = STEP_INITIAL;
0179 bio_post_read_processing(ctx);
0180 return;
0181 }
0182 __read_end_io(bio);
0183 }
0184
0185 static inline bool ext4_need_verity(const struct inode *inode, pgoff_t idx)
0186 {
0187 return fsverity_active(inode) &&
0188 idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
0189 }
0190
0191 static void ext4_set_bio_post_read_ctx(struct bio *bio,
0192 const struct inode *inode,
0193 pgoff_t first_idx)
0194 {
0195 unsigned int post_read_steps = 0;
0196
0197 if (fscrypt_inode_uses_fs_layer_crypto(inode))
0198 post_read_steps |= 1 << STEP_DECRYPT;
0199
0200 if (ext4_need_verity(inode, first_idx))
0201 post_read_steps |= 1 << STEP_VERITY;
0202
0203 if (post_read_steps) {
0204
0205 struct bio_post_read_ctx *ctx =
0206 mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
0207
0208 ctx->bio = bio;
0209 ctx->enabled_steps = post_read_steps;
0210 bio->bi_private = ctx;
0211 }
0212 }
0213
0214 static inline loff_t ext4_readpage_limit(struct inode *inode)
0215 {
0216 if (IS_ENABLED(CONFIG_FS_VERITY) &&
0217 (IS_VERITY(inode) || ext4_verity_in_progress(inode)))
0218 return inode->i_sb->s_maxbytes;
0219
0220 return i_size_read(inode);
0221 }
0222
0223 int ext4_mpage_readpages(struct inode *inode,
0224 struct readahead_control *rac, struct page *page)
0225 {
0226 struct bio *bio = NULL;
0227 sector_t last_block_in_bio = 0;
0228
0229 const unsigned blkbits = inode->i_blkbits;
0230 const unsigned blocks_per_page = PAGE_SIZE >> blkbits;
0231 const unsigned blocksize = 1 << blkbits;
0232 sector_t next_block;
0233 sector_t block_in_file;
0234 sector_t last_block;
0235 sector_t last_block_in_file;
0236 sector_t blocks[MAX_BUF_PER_PAGE];
0237 unsigned page_block;
0238 struct block_device *bdev = inode->i_sb->s_bdev;
0239 int length;
0240 unsigned relative_block = 0;
0241 struct ext4_map_blocks map;
0242 unsigned int nr_pages = rac ? readahead_count(rac) : 1;
0243
0244 map.m_pblk = 0;
0245 map.m_lblk = 0;
0246 map.m_len = 0;
0247 map.m_flags = 0;
0248
0249 for (; nr_pages; nr_pages--) {
0250 int fully_mapped = 1;
0251 unsigned first_hole = blocks_per_page;
0252
0253 if (rac) {
0254 page = readahead_page(rac);
0255 prefetchw(&page->flags);
0256 }
0257
0258 if (page_has_buffers(page))
0259 goto confused;
0260
0261 block_in_file = next_block =
0262 (sector_t)page->index << (PAGE_SHIFT - blkbits);
0263 last_block = block_in_file + nr_pages * blocks_per_page;
0264 last_block_in_file = (ext4_readpage_limit(inode) +
0265 blocksize - 1) >> blkbits;
0266 if (last_block > last_block_in_file)
0267 last_block = last_block_in_file;
0268 page_block = 0;
0269
0270
0271
0272
0273 if ((map.m_flags & EXT4_MAP_MAPPED) &&
0274 block_in_file > map.m_lblk &&
0275 block_in_file < (map.m_lblk + map.m_len)) {
0276 unsigned map_offset = block_in_file - map.m_lblk;
0277 unsigned last = map.m_len - map_offset;
0278
0279 for (relative_block = 0; ; relative_block++) {
0280 if (relative_block == last) {
0281
0282 map.m_flags &= ~EXT4_MAP_MAPPED;
0283 break;
0284 }
0285 if (page_block == blocks_per_page)
0286 break;
0287 blocks[page_block] = map.m_pblk + map_offset +
0288 relative_block;
0289 page_block++;
0290 block_in_file++;
0291 }
0292 }
0293
0294
0295
0296
0297
0298 while (page_block < blocks_per_page) {
0299 if (block_in_file < last_block) {
0300 map.m_lblk = block_in_file;
0301 map.m_len = last_block - block_in_file;
0302
0303 if (ext4_map_blocks(NULL, inode, &map, 0) < 0) {
0304 set_error_page:
0305 SetPageError(page);
0306 zero_user_segment(page, 0,
0307 PAGE_SIZE);
0308 unlock_page(page);
0309 goto next_page;
0310 }
0311 }
0312 if ((map.m_flags & EXT4_MAP_MAPPED) == 0) {
0313 fully_mapped = 0;
0314 if (first_hole == blocks_per_page)
0315 first_hole = page_block;
0316 page_block++;
0317 block_in_file++;
0318 continue;
0319 }
0320 if (first_hole != blocks_per_page)
0321 goto confused;
0322
0323
0324 if (page_block && blocks[page_block-1] != map.m_pblk-1)
0325 goto confused;
0326 for (relative_block = 0; ; relative_block++) {
0327 if (relative_block == map.m_len) {
0328
0329 map.m_flags &= ~EXT4_MAP_MAPPED;
0330 break;
0331 } else if (page_block == blocks_per_page)
0332 break;
0333 blocks[page_block] = map.m_pblk+relative_block;
0334 page_block++;
0335 block_in_file++;
0336 }
0337 }
0338 if (first_hole != blocks_per_page) {
0339 zero_user_segment(page, first_hole << blkbits,
0340 PAGE_SIZE);
0341 if (first_hole == 0) {
0342 if (ext4_need_verity(inode, page->index) &&
0343 !fsverity_verify_page(page))
0344 goto set_error_page;
0345 SetPageUptodate(page);
0346 unlock_page(page);
0347 goto next_page;
0348 }
0349 } else if (fully_mapped) {
0350 SetPageMappedToDisk(page);
0351 }
0352
0353
0354
0355
0356
0357 if (bio && (last_block_in_bio != blocks[0] - 1 ||
0358 !fscrypt_mergeable_bio(bio, inode, next_block))) {
0359 submit_and_realloc:
0360 submit_bio(bio);
0361 bio = NULL;
0362 }
0363 if (bio == NULL) {
0364
0365
0366
0367
0368 bio = bio_alloc(bdev, bio_max_segs(nr_pages),
0369 REQ_OP_READ, GFP_KERNEL);
0370 fscrypt_set_bio_crypt_ctx(bio, inode, next_block,
0371 GFP_KERNEL);
0372 ext4_set_bio_post_read_ctx(bio, inode, page->index);
0373 bio->bi_iter.bi_sector = blocks[0] << (blkbits - 9);
0374 bio->bi_end_io = mpage_end_io;
0375 if (rac)
0376 bio->bi_opf |= REQ_RAHEAD;
0377 }
0378
0379 length = first_hole << blkbits;
0380 if (bio_add_page(bio, page, length, 0) < length)
0381 goto submit_and_realloc;
0382
0383 if (((map.m_flags & EXT4_MAP_BOUNDARY) &&
0384 (relative_block == map.m_len)) ||
0385 (first_hole != blocks_per_page)) {
0386 submit_bio(bio);
0387 bio = NULL;
0388 } else
0389 last_block_in_bio = blocks[blocks_per_page - 1];
0390 goto next_page;
0391 confused:
0392 if (bio) {
0393 submit_bio(bio);
0394 bio = NULL;
0395 }
0396 if (!PageUptodate(page))
0397 block_read_full_folio(page_folio(page), ext4_get_block);
0398 else
0399 unlock_page(page);
0400 next_page:
0401 if (rac)
0402 put_page(page);
0403 }
0404 if (bio)
0405 submit_bio(bio);
0406 return 0;
0407 }
0408
0409 int __init ext4_init_post_read_processing(void)
0410 {
0411 bio_post_read_ctx_cache =
0412 kmem_cache_create("ext4_bio_post_read_ctx",
0413 sizeof(struct bio_post_read_ctx), 0, 0, NULL);
0414 if (!bio_post_read_ctx_cache)
0415 goto fail;
0416 bio_post_read_ctx_pool =
0417 mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
0418 bio_post_read_ctx_cache);
0419 if (!bio_post_read_ctx_pool)
0420 goto fail_free_cache;
0421 return 0;
0422
0423 fail_free_cache:
0424 kmem_cache_destroy(bio_post_read_ctx_cache);
0425 fail:
0426 return -ENOMEM;
0427 }
0428
0429 void ext4_exit_post_read_processing(void)
0430 {
0431 mempool_destroy(bio_post_read_ctx_pool);
0432 kmem_cache_destroy(bio_post_read_ctx_cache);
0433 }