18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * linux/fs/ext4/readpage.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2002, Linus Torvalds.
68c2ecf20Sopenharmony_ci * Copyright (C) 2015, Google, Inc.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * This was originally taken from fs/mpage.c
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * The ext4_mpage_readpages() function here is intended to
118c2ecf20Sopenharmony_ci * replace mpage_readahead() in the general case, not just for
128c2ecf20Sopenharmony_ci * encrypted files.  It has some limitations (see below), where it
138c2ecf20Sopenharmony_ci * will fall back to read_block_full_page(), but these limitations
148c2ecf20Sopenharmony_ci * should only be hit when page_size != block_size.
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci * This will allow us to attach a callback function to support ext4
178c2ecf20Sopenharmony_ci * encryption.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * If anything unusual happens, such as:
208c2ecf20Sopenharmony_ci *
218c2ecf20Sopenharmony_ci * - encountering a page which has buffers
228c2ecf20Sopenharmony_ci * - encountering a page which has a non-hole after a hole
238c2ecf20Sopenharmony_ci * - encountering a page with non-contiguous blocks
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci * then this code just gives up and calls the buffer_head-based read function.
268c2ecf20Sopenharmony_ci * It does handle a page which has holes at the end - that is a common case:
278c2ecf20Sopenharmony_ci * the end-of-file on blocksize < PAGE_SIZE setups.
288c2ecf20Sopenharmony_ci *
298c2ecf20Sopenharmony_ci */
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#include <linux/kernel.h>
328c2ecf20Sopenharmony_ci#include <linux/export.h>
338c2ecf20Sopenharmony_ci#include <linux/mm.h>
348c2ecf20Sopenharmony_ci#include <linux/kdev_t.h>
358c2ecf20Sopenharmony_ci#include <linux/gfp.h>
368c2ecf20Sopenharmony_ci#include <linux/bio.h>
378c2ecf20Sopenharmony_ci#include <linux/fs.h>
388c2ecf20Sopenharmony_ci#include <linux/buffer_head.h>
398c2ecf20Sopenharmony_ci#include <linux/blkdev.h>
408c2ecf20Sopenharmony_ci#include <linux/highmem.h>
418c2ecf20Sopenharmony_ci#include <linux/prefetch.h>
428c2ecf20Sopenharmony_ci#include <linux/mpage.h>
438c2ecf20Sopenharmony_ci#include <linux/writeback.h>
448c2ecf20Sopenharmony_ci#include <linux/backing-dev.h>
458c2ecf20Sopenharmony_ci#include <linux/pagevec.h>
468c2ecf20Sopenharmony_ci#include <linux/cleancache.h>
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#include "ext4.h"
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#define NUM_PREALLOC_POST_READ_CTXS	128
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic struct kmem_cache *bio_post_read_ctx_cache;
538c2ecf20Sopenharmony_cistatic mempool_t *bio_post_read_ctx_pool;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci/* postprocessing steps for read bios */
568c2ecf20Sopenharmony_cienum bio_post_read_step {
578c2ecf20Sopenharmony_ci	STEP_INITIAL = 0,
588c2ecf20Sopenharmony_ci	STEP_DECRYPT,
598c2ecf20Sopenharmony_ci	STEP_VERITY,
608c2ecf20Sopenharmony_ci	STEP_MAX,
618c2ecf20Sopenharmony_ci};
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cistruct bio_post_read_ctx {
648c2ecf20Sopenharmony_ci	struct bio *bio;
658c2ecf20Sopenharmony_ci	struct work_struct work;
668c2ecf20Sopenharmony_ci	unsigned int cur_step;
678c2ecf20Sopenharmony_ci	unsigned int enabled_steps;
688c2ecf20Sopenharmony_ci};
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistatic void __read_end_io(struct bio *bio)
718c2ecf20Sopenharmony_ci{
728c2ecf20Sopenharmony_ci	struct page *page;
738c2ecf20Sopenharmony_ci	struct bio_vec *bv;
748c2ecf20Sopenharmony_ci	struct bvec_iter_all iter_all;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	bio_for_each_segment_all(bv, bio, iter_all) {
778c2ecf20Sopenharmony_ci		page = bv->bv_page;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci		/* PG_error was set if any post_read step failed */
808c2ecf20Sopenharmony_ci		if (bio->bi_status || PageError(page)) {
818c2ecf20Sopenharmony_ci			ClearPageUptodate(page);
828c2ecf20Sopenharmony_ci			/* will re-read again later */
838c2ecf20Sopenharmony_ci			ClearPageError(page);
848c2ecf20Sopenharmony_ci		} else {
858c2ecf20Sopenharmony_ci			SetPageUptodate(page);
868c2ecf20Sopenharmony_ci		}
878c2ecf20Sopenharmony_ci		unlock_page(page);
888c2ecf20Sopenharmony_ci	}
898c2ecf20Sopenharmony_ci	if (bio->bi_private)
908c2ecf20Sopenharmony_ci		mempool_free(bio->bi_private, bio_post_read_ctx_pool);
918c2ecf20Sopenharmony_ci	bio_put(bio);
928c2ecf20Sopenharmony_ci}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_cistatic void bio_post_read_processing(struct bio_post_read_ctx *ctx);
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistatic void decrypt_work(struct work_struct *work)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci	struct bio_post_read_ctx *ctx =
998c2ecf20Sopenharmony_ci		container_of(work, struct bio_post_read_ctx, work);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	fscrypt_decrypt_bio(ctx->bio);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	bio_post_read_processing(ctx);
1048c2ecf20Sopenharmony_ci}
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_cistatic void verity_work(struct work_struct *work)
1078c2ecf20Sopenharmony_ci{
1088c2ecf20Sopenharmony_ci	struct bio_post_read_ctx *ctx =
1098c2ecf20Sopenharmony_ci		container_of(work, struct bio_post_read_ctx, work);
1108c2ecf20Sopenharmony_ci	struct bio *bio = ctx->bio;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	/*
1138c2ecf20Sopenharmony_ci	 * fsverity_verify_bio() may call readpages() again, and although verity
1148c2ecf20Sopenharmony_ci	 * will be disabled for that, decryption may still be needed, causing
1158c2ecf20Sopenharmony_ci	 * another bio_post_read_ctx to be allocated.  So to guarantee that
1168c2ecf20Sopenharmony_ci	 * mempool_alloc() never deadlocks we must free the current ctx first.
1178c2ecf20Sopenharmony_ci	 * This is safe because verity is the last post-read step.
1188c2ecf20Sopenharmony_ci	 */
1198c2ecf20Sopenharmony_ci	BUILD_BUG_ON(STEP_VERITY + 1 != STEP_MAX);
1208c2ecf20Sopenharmony_ci	mempool_free(ctx, bio_post_read_ctx_pool);
1218c2ecf20Sopenharmony_ci	bio->bi_private = NULL;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	fsverity_verify_bio(bio);
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	__read_end_io(bio);
1268c2ecf20Sopenharmony_ci}
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_cistatic void bio_post_read_processing(struct bio_post_read_ctx *ctx)
1298c2ecf20Sopenharmony_ci{
1308c2ecf20Sopenharmony_ci	/*
1318c2ecf20Sopenharmony_ci	 * We use different work queues for decryption and for verity because
1328c2ecf20Sopenharmony_ci	 * verity may require reading metadata pages that need decryption, and
1338c2ecf20Sopenharmony_ci	 * we shouldn't recurse to the same workqueue.
1348c2ecf20Sopenharmony_ci	 */
1358c2ecf20Sopenharmony_ci	switch (++ctx->cur_step) {
1368c2ecf20Sopenharmony_ci	case STEP_DECRYPT:
1378c2ecf20Sopenharmony_ci		if (ctx->enabled_steps & (1 << STEP_DECRYPT)) {
1388c2ecf20Sopenharmony_ci			INIT_WORK(&ctx->work, decrypt_work);
1398c2ecf20Sopenharmony_ci			fscrypt_enqueue_decrypt_work(&ctx->work);
1408c2ecf20Sopenharmony_ci			return;
1418c2ecf20Sopenharmony_ci		}
1428c2ecf20Sopenharmony_ci		ctx->cur_step++;
1438c2ecf20Sopenharmony_ci		fallthrough;
1448c2ecf20Sopenharmony_ci	case STEP_VERITY:
1458c2ecf20Sopenharmony_ci		if (ctx->enabled_steps & (1 << STEP_VERITY)) {
1468c2ecf20Sopenharmony_ci			INIT_WORK(&ctx->work, verity_work);
1478c2ecf20Sopenharmony_ci			fsverity_enqueue_verify_work(&ctx->work);
1488c2ecf20Sopenharmony_ci			return;
1498c2ecf20Sopenharmony_ci		}
1508c2ecf20Sopenharmony_ci		ctx->cur_step++;
1518c2ecf20Sopenharmony_ci		fallthrough;
1528c2ecf20Sopenharmony_ci	default:
1538c2ecf20Sopenharmony_ci		__read_end_io(ctx->bio);
1548c2ecf20Sopenharmony_ci	}
1558c2ecf20Sopenharmony_ci}
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_cistatic bool bio_post_read_required(struct bio *bio)
1588c2ecf20Sopenharmony_ci{
1598c2ecf20Sopenharmony_ci	return bio->bi_private && !bio->bi_status;
1608c2ecf20Sopenharmony_ci}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci/*
1638c2ecf20Sopenharmony_ci * I/O completion handler for multipage BIOs.
1648c2ecf20Sopenharmony_ci *
1658c2ecf20Sopenharmony_ci * The mpage code never puts partial pages into a BIO (except for end-of-file).
1668c2ecf20Sopenharmony_ci * If a page does not map to a contiguous run of blocks then it simply falls
1678c2ecf20Sopenharmony_ci * back to block_read_full_page().
1688c2ecf20Sopenharmony_ci *
1698c2ecf20Sopenharmony_ci * Why is this?  If a page's completion depends on a number of different BIOs
1708c2ecf20Sopenharmony_ci * which can complete in any order (or at the same time) then determining the
1718c2ecf20Sopenharmony_ci * status of that page is hard.  See end_buffer_async_read() for the details.
1728c2ecf20Sopenharmony_ci * There is no point in duplicating all that complexity.
1738c2ecf20Sopenharmony_ci */
1748c2ecf20Sopenharmony_cistatic void mpage_end_io(struct bio *bio)
1758c2ecf20Sopenharmony_ci{
1768c2ecf20Sopenharmony_ci	if (bio_post_read_required(bio)) {
1778c2ecf20Sopenharmony_ci		struct bio_post_read_ctx *ctx = bio->bi_private;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci		ctx->cur_step = STEP_INITIAL;
1808c2ecf20Sopenharmony_ci		bio_post_read_processing(ctx);
1818c2ecf20Sopenharmony_ci		return;
1828c2ecf20Sopenharmony_ci	}
1838c2ecf20Sopenharmony_ci	__read_end_io(bio);
1848c2ecf20Sopenharmony_ci}
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_cistatic inline bool ext4_need_verity(const struct inode *inode, pgoff_t idx)
1878c2ecf20Sopenharmony_ci{
1888c2ecf20Sopenharmony_ci	return fsverity_active(inode) &&
1898c2ecf20Sopenharmony_ci	       idx < DIV_ROUND_UP(inode->i_size, PAGE_SIZE);
1908c2ecf20Sopenharmony_ci}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_cistatic void ext4_set_bio_post_read_ctx(struct bio *bio,
1938c2ecf20Sopenharmony_ci				       const struct inode *inode,
1948c2ecf20Sopenharmony_ci				       pgoff_t first_idx)
1958c2ecf20Sopenharmony_ci{
1968c2ecf20Sopenharmony_ci	unsigned int post_read_steps = 0;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	if (fscrypt_inode_uses_fs_layer_crypto(inode))
1998c2ecf20Sopenharmony_ci		post_read_steps |= 1 << STEP_DECRYPT;
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	if (ext4_need_verity(inode, first_idx))
2028c2ecf20Sopenharmony_ci		post_read_steps |= 1 << STEP_VERITY;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	if (post_read_steps) {
2058c2ecf20Sopenharmony_ci		/* Due to the mempool, this never fails. */
2068c2ecf20Sopenharmony_ci		struct bio_post_read_ctx *ctx =
2078c2ecf20Sopenharmony_ci			mempool_alloc(bio_post_read_ctx_pool, GFP_NOFS);
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci		ctx->bio = bio;
2108c2ecf20Sopenharmony_ci		ctx->enabled_steps = post_read_steps;
2118c2ecf20Sopenharmony_ci		bio->bi_private = ctx;
2128c2ecf20Sopenharmony_ci	}
2138c2ecf20Sopenharmony_ci}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_cistatic inline loff_t ext4_readpage_limit(struct inode *inode)
2168c2ecf20Sopenharmony_ci{
2178c2ecf20Sopenharmony_ci	if (IS_ENABLED(CONFIG_FS_VERITY) &&
2188c2ecf20Sopenharmony_ci	    (IS_VERITY(inode) || ext4_verity_in_progress(inode)))
2198c2ecf20Sopenharmony_ci		return inode->i_sb->s_maxbytes;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	return i_size_read(inode);
2228c2ecf20Sopenharmony_ci}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ciint ext4_mpage_readpages(struct inode *inode,
2258c2ecf20Sopenharmony_ci		struct readahead_control *rac, struct page *page)
2268c2ecf20Sopenharmony_ci{
2278c2ecf20Sopenharmony_ci	struct bio *bio = NULL;
2288c2ecf20Sopenharmony_ci	sector_t last_block_in_bio = 0;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci	const unsigned blkbits = inode->i_blkbits;
2318c2ecf20Sopenharmony_ci	const unsigned blocks_per_page = PAGE_SIZE >> blkbits;
2328c2ecf20Sopenharmony_ci	const unsigned blocksize = 1 << blkbits;
2338c2ecf20Sopenharmony_ci	sector_t next_block;
2348c2ecf20Sopenharmony_ci	sector_t block_in_file;
2358c2ecf20Sopenharmony_ci	sector_t last_block;
2368c2ecf20Sopenharmony_ci	sector_t last_block_in_file;
2378c2ecf20Sopenharmony_ci	sector_t blocks[MAX_BUF_PER_PAGE];
2388c2ecf20Sopenharmony_ci	unsigned page_block;
2398c2ecf20Sopenharmony_ci	struct block_device *bdev = inode->i_sb->s_bdev;
2408c2ecf20Sopenharmony_ci	int length;
2418c2ecf20Sopenharmony_ci	unsigned relative_block = 0;
2428c2ecf20Sopenharmony_ci	struct ext4_map_blocks map;
2438c2ecf20Sopenharmony_ci	unsigned int nr_pages = rac ? readahead_count(rac) : 1;
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	map.m_pblk = 0;
2468c2ecf20Sopenharmony_ci	map.m_lblk = 0;
2478c2ecf20Sopenharmony_ci	map.m_len = 0;
2488c2ecf20Sopenharmony_ci	map.m_flags = 0;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	for (; nr_pages; nr_pages--) {
2518c2ecf20Sopenharmony_ci		int fully_mapped = 1;
2528c2ecf20Sopenharmony_ci		unsigned first_hole = blocks_per_page;
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci		if (rac) {
2558c2ecf20Sopenharmony_ci			page = readahead_page(rac);
2568c2ecf20Sopenharmony_ci			prefetchw(&page->flags);
2578c2ecf20Sopenharmony_ci		}
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci		if (page_has_buffers(page))
2608c2ecf20Sopenharmony_ci			goto confused;
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci		block_in_file = next_block =
2638c2ecf20Sopenharmony_ci			(sector_t)page->index << (PAGE_SHIFT - blkbits);
2648c2ecf20Sopenharmony_ci		last_block = block_in_file + nr_pages * blocks_per_page;
2658c2ecf20Sopenharmony_ci		last_block_in_file = (ext4_readpage_limit(inode) +
2668c2ecf20Sopenharmony_ci				      blocksize - 1) >> blkbits;
2678c2ecf20Sopenharmony_ci		if (last_block > last_block_in_file)
2688c2ecf20Sopenharmony_ci			last_block = last_block_in_file;
2698c2ecf20Sopenharmony_ci		page_block = 0;
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci		/*
2728c2ecf20Sopenharmony_ci		 * Map blocks using the previous result first.
2738c2ecf20Sopenharmony_ci		 */
2748c2ecf20Sopenharmony_ci		if ((map.m_flags & EXT4_MAP_MAPPED) &&
2758c2ecf20Sopenharmony_ci		    block_in_file > map.m_lblk &&
2768c2ecf20Sopenharmony_ci		    block_in_file < (map.m_lblk + map.m_len)) {
2778c2ecf20Sopenharmony_ci			unsigned map_offset = block_in_file - map.m_lblk;
2788c2ecf20Sopenharmony_ci			unsigned last = map.m_len - map_offset;
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci			for (relative_block = 0; ; relative_block++) {
2818c2ecf20Sopenharmony_ci				if (relative_block == last) {
2828c2ecf20Sopenharmony_ci					/* needed? */
2838c2ecf20Sopenharmony_ci					map.m_flags &= ~EXT4_MAP_MAPPED;
2848c2ecf20Sopenharmony_ci					break;
2858c2ecf20Sopenharmony_ci				}
2868c2ecf20Sopenharmony_ci				if (page_block == blocks_per_page)
2878c2ecf20Sopenharmony_ci					break;
2888c2ecf20Sopenharmony_ci				blocks[page_block] = map.m_pblk + map_offset +
2898c2ecf20Sopenharmony_ci					relative_block;
2908c2ecf20Sopenharmony_ci				page_block++;
2918c2ecf20Sopenharmony_ci				block_in_file++;
2928c2ecf20Sopenharmony_ci			}
2938c2ecf20Sopenharmony_ci		}
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci		/*
2968c2ecf20Sopenharmony_ci		 * Then do more ext4_map_blocks() calls until we are
2978c2ecf20Sopenharmony_ci		 * done with this page.
2988c2ecf20Sopenharmony_ci		 */
2998c2ecf20Sopenharmony_ci		while (page_block < blocks_per_page) {
3008c2ecf20Sopenharmony_ci			if (block_in_file < last_block) {
3018c2ecf20Sopenharmony_ci				map.m_lblk = block_in_file;
3028c2ecf20Sopenharmony_ci				map.m_len = last_block - block_in_file;
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci				if (ext4_map_blocks(NULL, inode, &map, 0) < 0) {
3058c2ecf20Sopenharmony_ci				set_error_page:
3068c2ecf20Sopenharmony_ci					SetPageError(page);
3078c2ecf20Sopenharmony_ci					zero_user_segment(page, 0,
3088c2ecf20Sopenharmony_ci							  PAGE_SIZE);
3098c2ecf20Sopenharmony_ci					unlock_page(page);
3108c2ecf20Sopenharmony_ci					goto next_page;
3118c2ecf20Sopenharmony_ci				}
3128c2ecf20Sopenharmony_ci			}
3138c2ecf20Sopenharmony_ci			if ((map.m_flags & EXT4_MAP_MAPPED) == 0) {
3148c2ecf20Sopenharmony_ci				fully_mapped = 0;
3158c2ecf20Sopenharmony_ci				if (first_hole == blocks_per_page)
3168c2ecf20Sopenharmony_ci					first_hole = page_block;
3178c2ecf20Sopenharmony_ci				page_block++;
3188c2ecf20Sopenharmony_ci				block_in_file++;
3198c2ecf20Sopenharmony_ci				continue;
3208c2ecf20Sopenharmony_ci			}
3218c2ecf20Sopenharmony_ci			if (first_hole != blocks_per_page)
3228c2ecf20Sopenharmony_ci				goto confused;		/* hole -> non-hole */
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci			/* Contiguous blocks? */
3258c2ecf20Sopenharmony_ci			if (page_block && blocks[page_block-1] != map.m_pblk-1)
3268c2ecf20Sopenharmony_ci				goto confused;
3278c2ecf20Sopenharmony_ci			for (relative_block = 0; ; relative_block++) {
3288c2ecf20Sopenharmony_ci				if (relative_block == map.m_len) {
3298c2ecf20Sopenharmony_ci					/* needed? */
3308c2ecf20Sopenharmony_ci					map.m_flags &= ~EXT4_MAP_MAPPED;
3318c2ecf20Sopenharmony_ci					break;
3328c2ecf20Sopenharmony_ci				} else if (page_block == blocks_per_page)
3338c2ecf20Sopenharmony_ci					break;
3348c2ecf20Sopenharmony_ci				blocks[page_block] = map.m_pblk+relative_block;
3358c2ecf20Sopenharmony_ci				page_block++;
3368c2ecf20Sopenharmony_ci				block_in_file++;
3378c2ecf20Sopenharmony_ci			}
3388c2ecf20Sopenharmony_ci		}
3398c2ecf20Sopenharmony_ci		if (first_hole != blocks_per_page) {
3408c2ecf20Sopenharmony_ci			zero_user_segment(page, first_hole << blkbits,
3418c2ecf20Sopenharmony_ci					  PAGE_SIZE);
3428c2ecf20Sopenharmony_ci			if (first_hole == 0) {
3438c2ecf20Sopenharmony_ci				if (ext4_need_verity(inode, page->index) &&
3448c2ecf20Sopenharmony_ci				    !fsverity_verify_page(page))
3458c2ecf20Sopenharmony_ci					goto set_error_page;
3468c2ecf20Sopenharmony_ci				SetPageUptodate(page);
3478c2ecf20Sopenharmony_ci				unlock_page(page);
3488c2ecf20Sopenharmony_ci				goto next_page;
3498c2ecf20Sopenharmony_ci			}
3508c2ecf20Sopenharmony_ci		} else if (fully_mapped) {
3518c2ecf20Sopenharmony_ci			SetPageMappedToDisk(page);
3528c2ecf20Sopenharmony_ci		}
3538c2ecf20Sopenharmony_ci		if (fully_mapped && blocks_per_page == 1 &&
3548c2ecf20Sopenharmony_ci		    !PageUptodate(page) && cleancache_get_page(page) == 0) {
3558c2ecf20Sopenharmony_ci			SetPageUptodate(page);
3568c2ecf20Sopenharmony_ci			goto confused;
3578c2ecf20Sopenharmony_ci		}
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci		/*
3608c2ecf20Sopenharmony_ci		 * This page will go to BIO.  Do we need to send this
3618c2ecf20Sopenharmony_ci		 * BIO off first?
3628c2ecf20Sopenharmony_ci		 */
3638c2ecf20Sopenharmony_ci		if (bio && (last_block_in_bio != blocks[0] - 1 ||
3648c2ecf20Sopenharmony_ci			    !fscrypt_mergeable_bio(bio, inode, next_block))) {
3658c2ecf20Sopenharmony_ci		submit_and_realloc:
3668c2ecf20Sopenharmony_ci			submit_bio(bio);
3678c2ecf20Sopenharmony_ci			bio = NULL;
3688c2ecf20Sopenharmony_ci		}
3698c2ecf20Sopenharmony_ci		if (bio == NULL) {
3708c2ecf20Sopenharmony_ci			/*
3718c2ecf20Sopenharmony_ci			 * bio_alloc will _always_ be able to allocate a bio if
3728c2ecf20Sopenharmony_ci			 * __GFP_DIRECT_RECLAIM is set, see bio_alloc_bioset().
3738c2ecf20Sopenharmony_ci			 */
3748c2ecf20Sopenharmony_ci			bio = bio_alloc(GFP_KERNEL,
3758c2ecf20Sopenharmony_ci				min_t(int, nr_pages, BIO_MAX_PAGES));
3768c2ecf20Sopenharmony_ci			fscrypt_set_bio_crypt_ctx(bio, inode, next_block,
3778c2ecf20Sopenharmony_ci						  GFP_KERNEL);
3788c2ecf20Sopenharmony_ci			ext4_set_bio_post_read_ctx(bio, inode, page->index);
3798c2ecf20Sopenharmony_ci			bio_set_dev(bio, bdev);
3808c2ecf20Sopenharmony_ci			bio->bi_iter.bi_sector = blocks[0] << (blkbits - 9);
3818c2ecf20Sopenharmony_ci			bio->bi_end_io = mpage_end_io;
3828c2ecf20Sopenharmony_ci			bio_set_op_attrs(bio, REQ_OP_READ,
3838c2ecf20Sopenharmony_ci						rac ? REQ_RAHEAD : 0);
3848c2ecf20Sopenharmony_ci		}
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci		length = first_hole << blkbits;
3878c2ecf20Sopenharmony_ci		if (bio_add_page(bio, page, length, 0) < length)
3888c2ecf20Sopenharmony_ci			goto submit_and_realloc;
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci		if (((map.m_flags & EXT4_MAP_BOUNDARY) &&
3918c2ecf20Sopenharmony_ci		     (relative_block == map.m_len)) ||
3928c2ecf20Sopenharmony_ci		    (first_hole != blocks_per_page)) {
3938c2ecf20Sopenharmony_ci			submit_bio(bio);
3948c2ecf20Sopenharmony_ci			bio = NULL;
3958c2ecf20Sopenharmony_ci		} else
3968c2ecf20Sopenharmony_ci			last_block_in_bio = blocks[blocks_per_page - 1];
3978c2ecf20Sopenharmony_ci		goto next_page;
3988c2ecf20Sopenharmony_ci	confused:
3998c2ecf20Sopenharmony_ci		if (bio) {
4008c2ecf20Sopenharmony_ci			submit_bio(bio);
4018c2ecf20Sopenharmony_ci			bio = NULL;
4028c2ecf20Sopenharmony_ci		}
4038c2ecf20Sopenharmony_ci		if (!PageUptodate(page))
4048c2ecf20Sopenharmony_ci			block_read_full_page(page, ext4_get_block);
4058c2ecf20Sopenharmony_ci		else
4068c2ecf20Sopenharmony_ci			unlock_page(page);
4078c2ecf20Sopenharmony_ci	next_page:
4088c2ecf20Sopenharmony_ci		if (rac)
4098c2ecf20Sopenharmony_ci			put_page(page);
4108c2ecf20Sopenharmony_ci	}
4118c2ecf20Sopenharmony_ci	if (bio)
4128c2ecf20Sopenharmony_ci		submit_bio(bio);
4138c2ecf20Sopenharmony_ci	return 0;
4148c2ecf20Sopenharmony_ci}
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ciint __init ext4_init_post_read_processing(void)
4178c2ecf20Sopenharmony_ci{
4188c2ecf20Sopenharmony_ci	bio_post_read_ctx_cache =
4198c2ecf20Sopenharmony_ci		kmem_cache_create("ext4_bio_post_read_ctx",
4208c2ecf20Sopenharmony_ci				  sizeof(struct bio_post_read_ctx), 0, 0, NULL);
4218c2ecf20Sopenharmony_ci	if (!bio_post_read_ctx_cache)
4228c2ecf20Sopenharmony_ci		goto fail;
4238c2ecf20Sopenharmony_ci	bio_post_read_ctx_pool =
4248c2ecf20Sopenharmony_ci		mempool_create_slab_pool(NUM_PREALLOC_POST_READ_CTXS,
4258c2ecf20Sopenharmony_ci					 bio_post_read_ctx_cache);
4268c2ecf20Sopenharmony_ci	if (!bio_post_read_ctx_pool)
4278c2ecf20Sopenharmony_ci		goto fail_free_cache;
4288c2ecf20Sopenharmony_ci	return 0;
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_cifail_free_cache:
4318c2ecf20Sopenharmony_ci	kmem_cache_destroy(bio_post_read_ctx_cache);
4328c2ecf20Sopenharmony_cifail:
4338c2ecf20Sopenharmony_ci	return -ENOMEM;
4348c2ecf20Sopenharmony_ci}
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_civoid ext4_exit_post_read_processing(void)
4378c2ecf20Sopenharmony_ci{
4388c2ecf20Sopenharmony_ci	mempool_destroy(bio_post_read_ctx_pool);
4398c2ecf20Sopenharmony_ci	kmem_cache_destroy(bio_post_read_ctx_cache);
4408c2ecf20Sopenharmony_ci}
441