Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 2 | /* |
| 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. Shutemov | ea1754a | 2016-04-01 15:29:48 +0300 | [diff] [blame] | 27 | * the end-of-file on blocksize < PAGE_SIZE setups. |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 28 | * |
| 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 Srinivasan | e303055 | 2016-12-14 16:39:51 -0800 | [diff] [blame] | 49 | #include <trace/events/android_fs.h> |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 50 | |
Michael Halcrow | c9c7429 | 2015-04-12 00:56:10 -0400 | [diff] [blame] | 51 | static 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 Srinivasan | e303055 | 2016-12-14 16:39:51 -0800 | [diff] [blame] | 60 | static void |
| 61 | ext4_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 Halcrow | c9c7429 | 2015-04-12 00:56:10 -0400 | [diff] [blame] | 71 | /* |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 72 | * 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 Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 83 | static void mpage_end_io(struct bio *bio) |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 84 | { |
| 85 | struct bio_vec *bv; |
| 86 | int i; |
| 87 | |
Mohan Srinivasan | e303055 | 2016-12-14 16:39:51 -0800 | [diff] [blame] | 88 | if (trace_android_fs_dataread_start_enabled()) |
| 89 | ext4_trace_read_completion(bio); |
| 90 | |
Michael Halcrow | c9c7429 | 2015-04-12 00:56:10 -0400 | [diff] [blame] | 91 | if (ext4_bio_encrypted(bio)) { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 92 | if (bio->bi_status) { |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 93 | fscrypt_release_ctx(bio->bi_private); |
Michael Halcrow | c9c7429 | 2015-04-12 00:56:10 -0400 | [diff] [blame] | 94 | } else { |
Eric Biggers | 0cb8dae | 2018-04-18 11:09:47 -0700 | [diff] [blame] | 95 | fscrypt_enqueue_decrypt_bio(bio->bi_private, bio); |
Michael Halcrow | c9c7429 | 2015-04-12 00:56:10 -0400 | [diff] [blame] | 96 | return; |
| 97 | } |
| 98 | } |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 99 | bio_for_each_segment_all(bv, bio, i) { |
| 100 | struct page *page = bv->bv_page; |
| 101 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 102 | if (!bio->bi_status) { |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 103 | SetPageUptodate(page); |
| 104 | } else { |
| 105 | ClearPageUptodate(page); |
| 106 | SetPageError(page); |
| 107 | } |
| 108 | unlock_page(page); |
| 109 | } |
| 110 | |
| 111 | bio_put(bio); |
| 112 | } |
| 113 | |
Mohan Srinivasan | e303055 | 2016-12-14 16:39:51 -0800 | [diff] [blame] | 114 | static void |
| 115 | ext4_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 Srinivasan | 004c6cf | 2017-02-10 14:26:23 -0800 | [diff] [blame] | 121 | 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 Srinivasan | e303055 | 2016-12-14 16:39:51 -0800 | [diff] [blame] | 126 | 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 Srinivasan | 004c6cf | 2017-02-10 14:26:23 -0800 | [diff] [blame] | 131 | path, |
Mohan Srinivasan | e303055 | 2016-12-14 16:39:51 -0800 | [diff] [blame] | 132 | current->comm); |
| 133 | } |
| 134 | } |
| 135 | submit_bio(bio); |
| 136 | } |
| 137 | |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 138 | int ext4_mpage_readpages(struct address_space *mapping, |
| 139 | struct list_head *pages, struct page *page, |
Jens Axboe | ac22b46 | 2018-08-17 15:45:42 -0700 | [diff] [blame] | 140 | unsigned nr_pages, bool is_readahead) |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 141 | { |
| 142 | struct bio *bio = NULL; |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 143 | sector_t last_block_in_bio = 0; |
| 144 | |
| 145 | struct inode *inode = mapping->host; |
| 146 | const unsigned blkbits = inode->i_blkbits; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 147 | const unsigned blocks_per_page = PAGE_SIZE >> blkbits; |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 148 | 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 wang | de9e918 | 2016-07-05 16:32:32 -0400 | [diff] [blame] | 164 | for (; nr_pages; nr_pages--) { |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 165 | 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 Hocko | 063d99b | 2015-10-15 15:28:24 -0700 | [diff] [blame] | 172 | if (add_to_page_cache_lru(page, mapping, page->index, |
Michal Hocko | 8a5c743 | 2016-07-26 15:24:53 -0700 | [diff] [blame] | 173 | readahead_gfp_mask(mapping))) |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 174 | goto next_page; |
| 175 | } |
| 176 | |
| 177 | if (page_has_buffers(page)) |
| 178 | goto confused; |
| 179 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 180 | block_in_file = (sector_t)page->index << (PAGE_SHIFT - blkbits); |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 181 | 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. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 224 | PAGE_SIZE); |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 225 | 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. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 257 | PAGE_SIZE); |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 258 | 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 Srinivasan | e303055 | 2016-12-14 16:39:51 -0800 | [diff] [blame] | 278 | ext4_submit_bio_read(bio); |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 279 | bio = NULL; |
| 280 | } |
| 281 | if (bio == NULL) { |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 282 | struct fscrypt_ctx *ctx = NULL; |
Zhen Kong | ee7bdc6 | 2019-03-14 10:55:19 -0700 | [diff] [blame^] | 283 | unsigned int flags = 0; |
Michael Halcrow | c9c7429 | 2015-04-12 00:56:10 -0400 | [diff] [blame] | 284 | |
| 285 | if (ext4_encrypted_inode(inode) && |
| 286 | S_ISREG(inode->i_mode)) { |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 287 | ctx = fscrypt_get_ctx(inode, GFP_NOFS); |
Michael Halcrow | c9c7429 | 2015-04-12 00:56:10 -0400 | [diff] [blame] | 288 | if (IS_ERR(ctx)) |
| 289 | goto set_error_page; |
| 290 | } |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 291 | bio = bio_alloc(GFP_KERNEL, |
Kent Overstreet | b54ffb7 | 2015-05-19 14:31:01 +0200 | [diff] [blame] | 292 | min_t(int, nr_pages, BIO_MAX_PAGES)); |
Michael Halcrow | c9c7429 | 2015-04-12 00:56:10 -0400 | [diff] [blame] | 293 | if (!bio) { |
| 294 | if (ctx) |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 295 | fscrypt_release_ctx(ctx); |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 296 | goto set_error_page; |
Michael Halcrow | c9c7429 | 2015-04-12 00:56:10 -0400 | [diff] [blame] | 297 | } |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 298 | bio_set_dev(bio, bdev); |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 299 | bio->bi_iter.bi_sector = blocks[0] << (blkbits - 9); |
| 300 | bio->bi_end_io = mpage_end_io; |
Michael Halcrow | c9c7429 | 2015-04-12 00:56:10 -0400 | [diff] [blame] | 301 | bio->bi_private = ctx; |
Zhen Kong | ee7bdc6 | 2019-03-14 10:55:19 -0700 | [diff] [blame^] | 302 | if (is_readahead) |
| 303 | flags = flags | REQ_RAHEAD; |
| 304 | bio_set_op_attrs(bio, REQ_OP_READ, flags); |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 305 | } |
| 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 Srinivasan | e303055 | 2016-12-14 16:39:51 -0800 | [diff] [blame] | 314 | ext4_submit_bio_read(bio); |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 315 | bio = NULL; |
| 316 | } else |
| 317 | last_block_in_bio = blocks[blocks_per_page - 1]; |
| 318 | goto next_page; |
| 319 | confused: |
| 320 | if (bio) { |
Mohan Srinivasan | e303055 | 2016-12-14 16:39:51 -0800 | [diff] [blame] | 321 | ext4_submit_bio_read(bio); |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 322 | 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. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 330 | put_page(page); |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 331 | } |
| 332 | BUG_ON(pages && !list_empty(pages)); |
| 333 | if (bio) |
Mohan Srinivasan | e303055 | 2016-12-14 16:39:51 -0800 | [diff] [blame] | 334 | ext4_submit_bio_read(bio); |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 335 | return 0; |
| 336 | } |