1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/ext4/readpage.c
5 * Copyright (C) 2002, Linus Torvalds.
6 * Copyright (C) 2015, Google, Inc.
8 * This was originally taken from fs/mpage.c
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.
16 * This will allow us to attach a callback function to support ext4
19 * If anything unusual happens, such as:
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
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.
31 #include <linux/kernel.h>
32 #include <linux/export.h>
34 #include <linux/kdev_t.h>
35 #include <linux/gfp.h>
36 #include <linux/bio.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>
50 static inline bool ext4_bio_encrypted(struct bio *bio)
52 #ifdef CONFIG_EXT4_FS_ENCRYPTION
53 return unlikely(bio->bi_private != NULL);
60 * I/O completion handler for multipage BIOs.
62 * The mpage code never puts partial pages into a BIO (except for end-of-file).
63 * If a page does not map to a contiguous run of blocks then it simply falls
64 * back to block_read_full_page().
66 * Why is this? If a page's completion depends on a number of different BIOs
67 * which can complete in any order (or at the same time) then determining the
68 * status of that page is hard. See end_buffer_async_read() for the details.
69 * There is no point in duplicating all that complexity.
71 static void mpage_end_io(struct bio *bio)
76 if (ext4_bio_encrypted(bio)) {
78 fscrypt_release_ctx(bio->bi_private);
80 fscrypt_enqueue_decrypt_bio(bio->bi_private, bio);
84 bio_for_each_segment_all(bv, bio, i) {
85 struct page *page = bv->bv_page;
87 if (!bio->bi_status) {
88 SetPageUptodate(page);
90 ClearPageUptodate(page);
99 int ext4_mpage_readpages(struct address_space *mapping,
100 struct list_head *pages, struct page *page,
101 unsigned nr_pages, bool is_readahead)
103 struct bio *bio = NULL;
104 sector_t last_block_in_bio = 0;
106 struct inode *inode = mapping->host;
107 const unsigned blkbits = inode->i_blkbits;
108 const unsigned blocks_per_page = PAGE_SIZE >> blkbits;
109 const unsigned blocksize = 1 << blkbits;
110 sector_t block_in_file;
112 sector_t last_block_in_file;
113 sector_t blocks[MAX_BUF_PER_PAGE];
115 struct block_device *bdev = inode->i_sb->s_bdev;
117 unsigned relative_block = 0;
118 struct ext4_map_blocks map;
125 for (; nr_pages; nr_pages--) {
126 int fully_mapped = 1;
127 unsigned first_hole = blocks_per_page;
129 prefetchw(&page->flags);
131 page = lru_to_page(pages);
132 list_del(&page->lru);
133 if (add_to_page_cache_lru(page, mapping, page->index,
134 readahead_gfp_mask(mapping)))
138 if (page_has_buffers(page))
141 block_in_file = (sector_t)page->index << (PAGE_SHIFT - blkbits);
142 last_block = block_in_file + nr_pages * blocks_per_page;
143 last_block_in_file = (i_size_read(inode) + blocksize - 1) >> blkbits;
144 if (last_block > last_block_in_file)
145 last_block = last_block_in_file;
149 * Map blocks using the previous result first.
151 if ((map.m_flags & EXT4_MAP_MAPPED) &&
152 block_in_file > map.m_lblk &&
153 block_in_file < (map.m_lblk + map.m_len)) {
154 unsigned map_offset = block_in_file - map.m_lblk;
155 unsigned last = map.m_len - map_offset;
157 for (relative_block = 0; ; relative_block++) {
158 if (relative_block == last) {
160 map.m_flags &= ~EXT4_MAP_MAPPED;
163 if (page_block == blocks_per_page)
165 blocks[page_block] = map.m_pblk + map_offset +
173 * Then do more ext4_map_blocks() calls until we are
174 * done with this page.
176 while (page_block < blocks_per_page) {
177 if (block_in_file < last_block) {
178 map.m_lblk = block_in_file;
179 map.m_len = last_block - block_in_file;
181 if (ext4_map_blocks(NULL, inode, &map, 0) < 0) {
184 zero_user_segment(page, 0,
190 if ((map.m_flags & EXT4_MAP_MAPPED) == 0) {
192 if (first_hole == blocks_per_page)
193 first_hole = page_block;
198 if (first_hole != blocks_per_page)
199 goto confused; /* hole -> non-hole */
201 /* Contiguous blocks? */
202 if (page_block && blocks[page_block-1] != map.m_pblk-1)
204 for (relative_block = 0; ; relative_block++) {
205 if (relative_block == map.m_len) {
207 map.m_flags &= ~EXT4_MAP_MAPPED;
209 } else if (page_block == blocks_per_page)
211 blocks[page_block] = map.m_pblk+relative_block;
216 if (first_hole != blocks_per_page) {
217 zero_user_segment(page, first_hole << blkbits,
219 if (first_hole == 0) {
220 SetPageUptodate(page);
224 } else if (fully_mapped) {
225 SetPageMappedToDisk(page);
227 if (fully_mapped && blocks_per_page == 1 &&
228 !PageUptodate(page) && cleancache_get_page(page) == 0) {
229 SetPageUptodate(page);
234 * This page will go to BIO. Do we need to send this
237 if (bio && (last_block_in_bio != blocks[0] - 1)) {
243 struct fscrypt_ctx *ctx = NULL;
245 if (ext4_encrypted_inode(inode) &&
246 S_ISREG(inode->i_mode)) {
247 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
251 bio = bio_alloc(GFP_KERNEL,
252 min_t(int, nr_pages, BIO_MAX_PAGES));
255 fscrypt_release_ctx(ctx);
258 bio_set_dev(bio, bdev);
259 bio->bi_iter.bi_sector = blocks[0] << (blkbits - 9);
260 bio->bi_end_io = mpage_end_io;
261 bio->bi_private = ctx;
262 bio_set_op_attrs(bio, REQ_OP_READ,
263 is_readahead ? REQ_RAHEAD : 0);
266 length = first_hole << blkbits;
267 if (bio_add_page(bio, page, length, 0) < length)
268 goto submit_and_realloc;
270 if (((map.m_flags & EXT4_MAP_BOUNDARY) &&
271 (relative_block == map.m_len)) ||
272 (first_hole != blocks_per_page)) {
276 last_block_in_bio = blocks[blocks_per_page - 1];
283 if (!PageUptodate(page))
284 block_read_full_page(page, ext4_get_block);
291 BUG_ON(pages && !list_empty(pages));