blob: 8e5947fda01abaf3dfb9e25ed0db18205203c981 [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"
Mohan Srinivasane3030552016-12-14 16:39:51 -080049#include <trace/events/android_fs.h>
Theodore Ts'of64e02f2015-04-08 00:00:32 -040050
Michael Halcrowc9c74292015-04-12 00:56:10 -040051static inline bool ext4_bio_encrypted(struct bio *bio)
52{
53#ifdef CONFIG_EXT4_FS_ENCRYPTION
54 return unlikely(bio->bi_private != NULL);
55#else
56 return false;
57#endif
58}
59
Mohan Srinivasane3030552016-12-14 16:39:51 -080060static void
61ext4_trace_read_completion(struct bio *bio)
62{
63 struct page *first_page = bio->bi_io_vec[0].bv_page;
64
65 if (first_page != NULL)
66 trace_android_fs_dataread_end(first_page->mapping->host,
67 page_offset(first_page),
68 bio->bi_iter.bi_size);
69}
70
Michael Halcrowc9c74292015-04-12 00:56:10 -040071/*
Theodore Ts'of64e02f2015-04-08 00:00:32 -040072 * I/O completion handler for multipage BIOs.
73 *
74 * The mpage code never puts partial pages into a BIO (except for end-of-file).
75 * If a page does not map to a contiguous run of blocks then it simply falls
76 * back to block_read_full_page().
77 *
78 * Why is this? If a page's completion depends on a number of different BIOs
79 * which can complete in any order (or at the same time) then determining the
80 * status of that page is hard. See end_buffer_async_read() for the details.
81 * There is no point in duplicating all that complexity.
82 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020083static void mpage_end_io(struct bio *bio)
Theodore Ts'of64e02f2015-04-08 00:00:32 -040084{
85 struct bio_vec *bv;
86 int i;
87
Mohan Srinivasane3030552016-12-14 16:39:51 -080088 if (trace_android_fs_dataread_start_enabled())
89 ext4_trace_read_completion(bio);
90
Michael Halcrowc9c74292015-04-12 00:56:10 -040091 if (ext4_bio_encrypted(bio)) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +020092 if (bio->bi_status) {
Jaegeuk Kima7550b32016-07-10 14:01:03 -040093 fscrypt_release_ctx(bio->bi_private);
Michael Halcrowc9c74292015-04-12 00:56:10 -040094 } else {
Eric Biggers0cb8dae2018-04-18 11:09:47 -070095 fscrypt_enqueue_decrypt_bio(bio->bi_private, bio);
Michael Halcrowc9c74292015-04-12 00:56:10 -040096 return;
97 }
98 }
Theodore Ts'of64e02f2015-04-08 00:00:32 -040099 bio_for_each_segment_all(bv, bio, i) {
100 struct page *page = bv->bv_page;
101
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200102 if (!bio->bi_status) {
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400103 SetPageUptodate(page);
104 } else {
105 ClearPageUptodate(page);
106 SetPageError(page);
107 }
108 unlock_page(page);
109 }
110
111 bio_put(bio);
112}
113
Mohan Srinivasane3030552016-12-14 16:39:51 -0800114static void
115ext4_submit_bio_read(struct bio *bio)
116{
117 if (trace_android_fs_dataread_start_enabled()) {
118 struct page *first_page = bio->bi_io_vec[0].bv_page;
119
120 if (first_page != NULL) {
Mohan Srinivasan004c6cf2017-02-10 14:26:23 -0800121 char *path, pathbuf[MAX_TRACE_PATHBUF_LEN];
122
123 path = android_fstrace_get_pathname(pathbuf,
124 MAX_TRACE_PATHBUF_LEN,
125 first_page->mapping->host);
Mohan Srinivasane3030552016-12-14 16:39:51 -0800126 trace_android_fs_dataread_start(
127 first_page->mapping->host,
128 page_offset(first_page),
129 bio->bi_iter.bi_size,
130 current->pid,
Mohan Srinivasan004c6cf2017-02-10 14:26:23 -0800131 path,
Mohan Srinivasane3030552016-12-14 16:39:51 -0800132 current->comm);
133 }
134 }
135 submit_bio(bio);
136}
137
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400138int ext4_mpage_readpages(struct address_space *mapping,
139 struct list_head *pages, struct page *page,
Jens Axboeac22b462018-08-17 15:45:42 -0700140 unsigned nr_pages, bool is_readahead)
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400141{
142 struct bio *bio = NULL;
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400143 sector_t last_block_in_bio = 0;
144
145 struct inode *inode = mapping->host;
146 const unsigned blkbits = inode->i_blkbits;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300147 const unsigned blocks_per_page = PAGE_SIZE >> blkbits;
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400148 const unsigned blocksize = 1 << blkbits;
149 sector_t block_in_file;
150 sector_t last_block;
151 sector_t last_block_in_file;
152 sector_t blocks[MAX_BUF_PER_PAGE];
153 unsigned page_block;
154 struct block_device *bdev = inode->i_sb->s_bdev;
155 int length;
156 unsigned relative_block = 0;
157 struct ext4_map_blocks map;
158
159 map.m_pblk = 0;
160 map.m_lblk = 0;
161 map.m_len = 0;
162 map.m_flags = 0;
163
yalin wangde9e9182016-07-05 16:32:32 -0400164 for (; nr_pages; nr_pages--) {
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400165 int fully_mapped = 1;
166 unsigned first_hole = blocks_per_page;
167
168 prefetchw(&page->flags);
169 if (pages) {
170 page = list_entry(pages->prev, struct page, lru);
171 list_del(&page->lru);
Michal Hocko063d99b2015-10-15 15:28:24 -0700172 if (add_to_page_cache_lru(page, mapping, page->index,
Michal Hocko8a5c7432016-07-26 15:24:53 -0700173 readahead_gfp_mask(mapping)))
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400174 goto next_page;
175 }
176
177 if (page_has_buffers(page))
178 goto confused;
179
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300180 block_in_file = (sector_t)page->index << (PAGE_SHIFT - blkbits);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400181 last_block = block_in_file + nr_pages * blocks_per_page;
182 last_block_in_file = (i_size_read(inode) + blocksize - 1) >> blkbits;
183 if (last_block > last_block_in_file)
184 last_block = last_block_in_file;
185 page_block = 0;
186
187 /*
188 * Map blocks using the previous result first.
189 */
190 if ((map.m_flags & EXT4_MAP_MAPPED) &&
191 block_in_file > map.m_lblk &&
192 block_in_file < (map.m_lblk + map.m_len)) {
193 unsigned map_offset = block_in_file - map.m_lblk;
194 unsigned last = map.m_len - map_offset;
195
196 for (relative_block = 0; ; relative_block++) {
197 if (relative_block == last) {
198 /* needed? */
199 map.m_flags &= ~EXT4_MAP_MAPPED;
200 break;
201 }
202 if (page_block == blocks_per_page)
203 break;
204 blocks[page_block] = map.m_pblk + map_offset +
205 relative_block;
206 page_block++;
207 block_in_file++;
208 }
209 }
210
211 /*
212 * Then do more ext4_map_blocks() calls until we are
213 * done with this page.
214 */
215 while (page_block < blocks_per_page) {
216 if (block_in_file < last_block) {
217 map.m_lblk = block_in_file;
218 map.m_len = last_block - block_in_file;
219
220 if (ext4_map_blocks(NULL, inode, &map, 0) < 0) {
221 set_error_page:
222 SetPageError(page);
223 zero_user_segment(page, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300224 PAGE_SIZE);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400225 unlock_page(page);
226 goto next_page;
227 }
228 }
229 if ((map.m_flags & EXT4_MAP_MAPPED) == 0) {
230 fully_mapped = 0;
231 if (first_hole == blocks_per_page)
232 first_hole = page_block;
233 page_block++;
234 block_in_file++;
235 continue;
236 }
237 if (first_hole != blocks_per_page)
238 goto confused; /* hole -> non-hole */
239
240 /* Contiguous blocks? */
241 if (page_block && blocks[page_block-1] != map.m_pblk-1)
242 goto confused;
243 for (relative_block = 0; ; relative_block++) {
244 if (relative_block == map.m_len) {
245 /* needed? */
246 map.m_flags &= ~EXT4_MAP_MAPPED;
247 break;
248 } else if (page_block == blocks_per_page)
249 break;
250 blocks[page_block] = map.m_pblk+relative_block;
251 page_block++;
252 block_in_file++;
253 }
254 }
255 if (first_hole != blocks_per_page) {
256 zero_user_segment(page, first_hole << blkbits,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300257 PAGE_SIZE);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400258 if (first_hole == 0) {
259 SetPageUptodate(page);
260 unlock_page(page);
261 goto next_page;
262 }
263 } else if (fully_mapped) {
264 SetPageMappedToDisk(page);
265 }
266 if (fully_mapped && blocks_per_page == 1 &&
267 !PageUptodate(page) && cleancache_get_page(page) == 0) {
268 SetPageUptodate(page);
269 goto confused;
270 }
271
272 /*
273 * This page will go to BIO. Do we need to send this
274 * BIO off first?
275 */
276 if (bio && (last_block_in_bio != blocks[0] - 1)) {
277 submit_and_realloc:
Mohan Srinivasane3030552016-12-14 16:39:51 -0800278 ext4_submit_bio_read(bio);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400279 bio = NULL;
280 }
281 if (bio == NULL) {
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400282 struct fscrypt_ctx *ctx = NULL;
Zhen Kongee7bdc62019-03-14 10:55:19 -0700283 unsigned int flags = 0;
Michael Halcrowc9c74292015-04-12 00:56:10 -0400284
285 if (ext4_encrypted_inode(inode) &&
286 S_ISREG(inode->i_mode)) {
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400287 ctx = fscrypt_get_ctx(inode, GFP_NOFS);
Michael Halcrowc9c74292015-04-12 00:56:10 -0400288 if (IS_ERR(ctx))
289 goto set_error_page;
290 }
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400291 bio = bio_alloc(GFP_KERNEL,
Kent Overstreetb54ffb72015-05-19 14:31:01 +0200292 min_t(int, nr_pages, BIO_MAX_PAGES));
Michael Halcrowc9c74292015-04-12 00:56:10 -0400293 if (!bio) {
294 if (ctx)
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400295 fscrypt_release_ctx(ctx);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400296 goto set_error_page;
Michael Halcrowc9c74292015-04-12 00:56:10 -0400297 }
Christoph Hellwig74d46992017-08-23 19:10:32 +0200298 bio_set_dev(bio, bdev);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400299 bio->bi_iter.bi_sector = blocks[0] << (blkbits - 9);
300 bio->bi_end_io = mpage_end_io;
Michael Halcrowc9c74292015-04-12 00:56:10 -0400301 bio->bi_private = ctx;
Zhen Kongee7bdc62019-03-14 10:55:19 -0700302 if (is_readahead)
303 flags = flags | REQ_RAHEAD;
304 bio_set_op_attrs(bio, REQ_OP_READ, flags);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400305 }
306
307 length = first_hole << blkbits;
308 if (bio_add_page(bio, page, length, 0) < length)
309 goto submit_and_realloc;
310
311 if (((map.m_flags & EXT4_MAP_BOUNDARY) &&
312 (relative_block == map.m_len)) ||
313 (first_hole != blocks_per_page)) {
Mohan Srinivasane3030552016-12-14 16:39:51 -0800314 ext4_submit_bio_read(bio);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400315 bio = NULL;
316 } else
317 last_block_in_bio = blocks[blocks_per_page - 1];
318 goto next_page;
319 confused:
320 if (bio) {
Mohan Srinivasane3030552016-12-14 16:39:51 -0800321 ext4_submit_bio_read(bio);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400322 bio = NULL;
323 }
324 if (!PageUptodate(page))
325 block_read_full_page(page, ext4_get_block);
326 else
327 unlock_page(page);
328 next_page:
329 if (pages)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300330 put_page(page);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400331 }
332 BUG_ON(pages && !list_empty(pages));
333 if (bio)
Mohan Srinivasane3030552016-12-14 16:39:51 -0800334 ext4_submit_bio_read(bio);
Theodore Ts'of64e02f2015-04-08 00:00:32 -0400335 return 0;
336}