blob: 9ffa6fad18dbef1528f3ddbacc67fafff370cf03 [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 *
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.
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>
46#include <linux/cleancache.h>
47
48#include "ext4.h"
49
Michael Halcrowc9c74292015-04-12 00:56:10 -040050static inline bool ext4_bio_encrypted(struct bio *bio)
51{
52#ifdef CONFIG_EXT4_FS_ENCRYPTION
53 return unlikely(bio->bi_private != NULL);
54#else
55 return false;
56#endif
57}
58
59/*
Theodore Ts'of64e02f2015-04-08 00:00:32 -040060 * I/O completion handler for multipage BIOs.
61 *
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().
65 *
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.
70 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020071static void mpage_end_io(struct bio *bio)
Theodore Ts'of64e02f2015-04-08 00:00:32 -040072{
73 struct bio_vec *bv;
74 int i;
75
Michael Halcrowc9c74292015-04-12 00:56:10 -040076 if (ext4_bio_encrypted(bio)) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020077 if (bio->bi_status) {
Jaegeuk Kima7550b32016-07-10 14:01:03 -040078 fscrypt_release_ctx(bio->bi_private);
Michael Halcrowc9c74292015-04-12 00:56:10 -040079 } else {
Jaegeuk Kima7550b32016-07-10 14:01:03 -040080 fscrypt_decrypt_bio_pages(bio->bi_private, bio);
Michael Halcrowc9c74292015-04-12 00:56:10 -040081 return;
82 }
83 }
Theodore Ts'of64e02f2015-04-08 00:00:32 -040084 bio_for_each_segment_all(bv, bio, i) {
85 struct page *page = bv->bv_page;
86
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020087 if (!bio->bi_status) {
Theodore Ts'of64e02f2015-04-08 00:00:32 -040088 SetPageUptodate(page);
89 } else {
90 ClearPageUptodate(page);
91 SetPageError(page);
92 }
93 unlock_page(page);
94 }
95
96 bio_put(bio);
97}
98
99int ext4_mpage_readpages(struct address_space *mapping,
100 struct list_head *pages, struct page *page,
101 unsigned nr_pages)
102{
103 struct bio *bio = NULL;
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400104 sector_t last_block_in_bio = 0;
105
106 struct inode *inode = mapping->host;
107 const unsigned blkbits = inode->i_blkbits;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300108 const unsigned blocks_per_page = PAGE_SIZE >> blkbits;
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400109 const unsigned blocksize = 1 << blkbits;
110 sector_t block_in_file;
111 sector_t last_block;
112 sector_t last_block_in_file;
113 sector_t blocks[MAX_BUF_PER_PAGE];
114 unsigned page_block;
115 struct block_device *bdev = inode->i_sb->s_bdev;
116 int length;
117 unsigned relative_block = 0;
118 struct ext4_map_blocks map;
119
120 map.m_pblk = 0;
121 map.m_lblk = 0;
122 map.m_len = 0;
123 map.m_flags = 0;
124
yalin wangde9e9182016-07-05 16:32:32 -0400125 for (; nr_pages; nr_pages--) {
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400126 int fully_mapped = 1;
127 unsigned first_hole = blocks_per_page;
128
129 prefetchw(&page->flags);
130 if (pages) {
131 page = list_entry(pages->prev, struct page, lru);
132 list_del(&page->lru);
Michal Hocko063d99b2015-10-15 15:28:24 -0700133 if (add_to_page_cache_lru(page, mapping, page->index,
Michal Hocko8a5c7432016-07-26 15:24:53 -0700134 readahead_gfp_mask(mapping)))
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400135 goto next_page;
136 }
137
138 if (page_has_buffers(page))
139 goto confused;
140
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300141 block_in_file = (sector_t)page->index << (PAGE_SHIFT - blkbits);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400142 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;
146 page_block = 0;
147
148 /*
149 * Map blocks using the previous result first.
150 */
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;
156
157 for (relative_block = 0; ; relative_block++) {
158 if (relative_block == last) {
159 /* needed? */
160 map.m_flags &= ~EXT4_MAP_MAPPED;
161 break;
162 }
163 if (page_block == blocks_per_page)
164 break;
165 blocks[page_block] = map.m_pblk + map_offset +
166 relative_block;
167 page_block++;
168 block_in_file++;
169 }
170 }
171
172 /*
173 * Then do more ext4_map_blocks() calls until we are
174 * done with this page.
175 */
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;
180
181 if (ext4_map_blocks(NULL, inode, &map, 0) < 0) {
182 set_error_page:
183 SetPageError(page);
184 zero_user_segment(page, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300185 PAGE_SIZE);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400186 unlock_page(page);
187 goto next_page;
188 }
189 }
190 if ((map.m_flags & EXT4_MAP_MAPPED) == 0) {
191 fully_mapped = 0;
192 if (first_hole == blocks_per_page)
193 first_hole = page_block;
194 page_block++;
195 block_in_file++;
196 continue;
197 }
198 if (first_hole != blocks_per_page)
199 goto confused; /* hole -> non-hole */
200
201 /* Contiguous blocks? */
202 if (page_block && blocks[page_block-1] != map.m_pblk-1)
203 goto confused;
204 for (relative_block = 0; ; relative_block++) {
205 if (relative_block == map.m_len) {
206 /* needed? */
207 map.m_flags &= ~EXT4_MAP_MAPPED;
208 break;
209 } else if (page_block == blocks_per_page)
210 break;
211 blocks[page_block] = map.m_pblk+relative_block;
212 page_block++;
213 block_in_file++;
214 }
215 }
216 if (first_hole != blocks_per_page) {
217 zero_user_segment(page, first_hole << blkbits,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300218 PAGE_SIZE);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400219 if (first_hole == 0) {
220 SetPageUptodate(page);
221 unlock_page(page);
222 goto next_page;
223 }
224 } else if (fully_mapped) {
225 SetPageMappedToDisk(page);
226 }
227 if (fully_mapped && blocks_per_page == 1 &&
228 !PageUptodate(page) && cleancache_get_page(page) == 0) {
229 SetPageUptodate(page);
230 goto confused;
231 }
232
233 /*
234 * This page will go to BIO. Do we need to send this
235 * BIO off first?
236 */
237 if (bio && (last_block_in_bio != blocks[0] - 1)) {
238 submit_and_realloc:
Mike Christie4e49ea42016-06-05 14:31:41 -0500239 submit_bio(bio);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400240 bio = NULL;
241 }
242 if (bio == NULL) {
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400243 struct fscrypt_ctx *ctx = NULL;
Michael Halcrowc9c74292015-04-12 00:56:10 -0400244
245 if (ext4_encrypted_inode(inode) &&
246 S_ISREG(inode->i_mode)) {
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400247 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
Michael Halcrowc9c74292015-04-12 00:56:10 -0400248 if (IS_ERR(ctx))
249 goto set_error_page;
250 }
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400251 bio = bio_alloc(GFP_KERNEL,
Kent Overstreetb54ffb72015-05-19 14:31:01 +0200252 min_t(int, nr_pages, BIO_MAX_PAGES));
Michael Halcrowc9c74292015-04-12 00:56:10 -0400253 if (!bio) {
254 if (ctx)
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400255 fscrypt_release_ctx(ctx);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400256 goto set_error_page;
Michael Halcrowc9c74292015-04-12 00:56:10 -0400257 }
Christoph Hellwig74d46992017-08-23 19:10:32 +0200258 bio_set_dev(bio, bdev);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400259 bio->bi_iter.bi_sector = blocks[0] << (blkbits - 9);
260 bio->bi_end_io = mpage_end_io;
Michael Halcrowc9c74292015-04-12 00:56:10 -0400261 bio->bi_private = ctx;
Mike Christie95fe6c12016-06-05 14:31:48 -0500262 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400263 }
264
265 length = first_hole << blkbits;
266 if (bio_add_page(bio, page, length, 0) < length)
267 goto submit_and_realloc;
268
269 if (((map.m_flags & EXT4_MAP_BOUNDARY) &&
270 (relative_block == map.m_len)) ||
271 (first_hole != blocks_per_page)) {
Mike Christie4e49ea42016-06-05 14:31:41 -0500272 submit_bio(bio);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400273 bio = NULL;
274 } else
275 last_block_in_bio = blocks[blocks_per_page - 1];
276 goto next_page;
277 confused:
278 if (bio) {
Mike Christie4e49ea42016-06-05 14:31:41 -0500279 submit_bio(bio);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400280 bio = NULL;
281 }
282 if (!PageUptodate(page))
283 block_read_full_page(page, ext4_get_block);
284 else
285 unlock_page(page);
286 next_page:
287 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300288 put_page(page);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400289 }
290 BUG_ON(pages && !list_empty(pages));
291 if (bio)
Mike Christie4e49ea42016-06-05 14:31:41 -0500292 submit_bio(bio);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400293 return 0;
294}