Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/ext4/readpage.c |
| 3 | * |
| 4 | * Copyright (C) 2002, Linus Torvalds. |
| 5 | * Copyright (C) 2015, Google, Inc. |
| 6 | * |
| 7 | * This was originally taken from fs/mpage.c |
| 8 | * |
| 9 | * The intent is the ext4_mpage_readpages() function here is intended |
| 10 | * to replace mpage_readpages() in the general case, not just for |
| 11 | * encrypted files. It has some limitations (see below), where it |
| 12 | * will fall back to read_block_full_page(), but these limitations |
| 13 | * should only be hit when page_size != block_size. |
| 14 | * |
| 15 | * This will allow us to attach a callback function to support ext4 |
| 16 | * encryption. |
| 17 | * |
| 18 | * If anything unusual happens, such as: |
| 19 | * |
| 20 | * - encountering a page which has buffers |
| 21 | * - encountering a page which has a non-hole after a hole |
| 22 | * - encountering a page with non-contiguous blocks |
| 23 | * |
| 24 | * then this code just gives up and calls the buffer_head-based read function. |
| 25 | * 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] | 26 | * the end-of-file on blocksize < PAGE_SIZE setups. |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 27 | * |
| 28 | */ |
| 29 | |
| 30 | #include <linux/kernel.h> |
| 31 | #include <linux/export.h> |
| 32 | #include <linux/mm.h> |
| 33 | #include <linux/kdev_t.h> |
| 34 | #include <linux/gfp.h> |
| 35 | #include <linux/bio.h> |
| 36 | #include <linux/fs.h> |
| 37 | #include <linux/buffer_head.h> |
| 38 | #include <linux/blkdev.h> |
| 39 | #include <linux/highmem.h> |
| 40 | #include <linux/prefetch.h> |
| 41 | #include <linux/mpage.h> |
| 42 | #include <linux/writeback.h> |
| 43 | #include <linux/backing-dev.h> |
| 44 | #include <linux/pagevec.h> |
| 45 | #include <linux/cleancache.h> |
| 46 | |
| 47 | #include "ext4.h" |
Mohan Srinivasan | 25cc70f | 2016-12-14 16:39:51 -0800 | [diff] [blame] | 48 | #include <trace/events/android_fs.h> |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 49 | |
Michael Halcrow | c9c7429 | 2015-04-12 00:56:10 -0400 | [diff] [blame] | 50 | static 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 | |
Mohan Srinivasan | 25cc70f | 2016-12-14 16:39:51 -0800 | [diff] [blame] | 59 | static void |
| 60 | ext4_trace_read_completion(struct bio *bio) |
| 61 | { |
| 62 | struct page *first_page = bio->bi_io_vec[0].bv_page; |
| 63 | |
| 64 | if (first_page != NULL) |
| 65 | trace_android_fs_dataread_end(first_page->mapping->host, |
| 66 | page_offset(first_page), |
| 67 | bio->bi_iter.bi_size); |
| 68 | } |
| 69 | |
Michael Halcrow | c9c7429 | 2015-04-12 00:56:10 -0400 | [diff] [blame] | 70 | /* |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 71 | * I/O completion handler for multipage BIOs. |
| 72 | * |
| 73 | * The mpage code never puts partial pages into a BIO (except for end-of-file). |
| 74 | * If a page does not map to a contiguous run of blocks then it simply falls |
| 75 | * back to block_read_full_page(). |
| 76 | * |
| 77 | * Why is this? If a page's completion depends on a number of different BIOs |
| 78 | * which can complete in any order (or at the same time) then determining the |
| 79 | * status of that page is hard. See end_buffer_async_read() for the details. |
| 80 | * There is no point in duplicating all that complexity. |
| 81 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 82 | static void mpage_end_io(struct bio *bio) |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 83 | { |
| 84 | struct bio_vec *bv; |
| 85 | int i; |
| 86 | |
Mohan Srinivasan | 25cc70f | 2016-12-14 16:39:51 -0800 | [diff] [blame] | 87 | if (trace_android_fs_dataread_start_enabled()) |
| 88 | ext4_trace_read_completion(bio); |
| 89 | |
Michael Halcrow | c9c7429 | 2015-04-12 00:56:10 -0400 | [diff] [blame] | 90 | if (ext4_bio_encrypted(bio)) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 91 | if (bio->bi_error) { |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 92 | fscrypt_release_ctx(bio->bi_private); |
Michael Halcrow | c9c7429 | 2015-04-12 00:56:10 -0400 | [diff] [blame] | 93 | } else { |
Eric Biggers | 823fd56 | 2018-04-18 11:09:47 -0700 | [diff] [blame] | 94 | fscrypt_enqueue_decrypt_bio(bio->bi_private, bio); |
Michael Halcrow | c9c7429 | 2015-04-12 00:56:10 -0400 | [diff] [blame] | 95 | return; |
| 96 | } |
| 97 | } |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 98 | bio_for_each_segment_all(bv, bio, i) { |
| 99 | struct page *page = bv->bv_page; |
| 100 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 101 | if (!bio->bi_error) { |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 102 | SetPageUptodate(page); |
| 103 | } else { |
| 104 | ClearPageUptodate(page); |
| 105 | SetPageError(page); |
| 106 | } |
| 107 | unlock_page(page); |
| 108 | } |
| 109 | |
| 110 | bio_put(bio); |
| 111 | } |
| 112 | |
Mohan Srinivasan | 25cc70f | 2016-12-14 16:39:51 -0800 | [diff] [blame] | 113 | static void |
| 114 | ext4_submit_bio_read(struct bio *bio) |
| 115 | { |
| 116 | if (trace_android_fs_dataread_start_enabled()) { |
| 117 | struct page *first_page = bio->bi_io_vec[0].bv_page; |
| 118 | |
| 119 | if (first_page != NULL) { |
Mohan Srinivasan | 009e608 | 2017-02-10 14:26:23 -0800 | [diff] [blame] | 120 | char *path, pathbuf[MAX_TRACE_PATHBUF_LEN]; |
| 121 | |
| 122 | path = android_fstrace_get_pathname(pathbuf, |
| 123 | MAX_TRACE_PATHBUF_LEN, |
| 124 | first_page->mapping->host); |
Mohan Srinivasan | 25cc70f | 2016-12-14 16:39:51 -0800 | [diff] [blame] | 125 | trace_android_fs_dataread_start( |
| 126 | first_page->mapping->host, |
| 127 | page_offset(first_page), |
| 128 | bio->bi_iter.bi_size, |
| 129 | current->pid, |
Mohan Srinivasan | 009e608 | 2017-02-10 14:26:23 -0800 | [diff] [blame] | 130 | path, |
Mohan Srinivasan | 25cc70f | 2016-12-14 16:39:51 -0800 | [diff] [blame] | 131 | current->comm); |
| 132 | } |
| 133 | } |
| 134 | submit_bio(bio); |
| 135 | } |
| 136 | |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 137 | int ext4_mpage_readpages(struct address_space *mapping, |
| 138 | struct list_head *pages, struct page *page, |
| 139 | unsigned nr_pages) |
| 140 | { |
| 141 | struct bio *bio = NULL; |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 142 | sector_t last_block_in_bio = 0; |
| 143 | |
| 144 | struct inode *inode = mapping->host; |
| 145 | const unsigned blkbits = inode->i_blkbits; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 146 | const unsigned blocks_per_page = PAGE_SIZE >> blkbits; |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 147 | const unsigned blocksize = 1 << blkbits; |
| 148 | sector_t block_in_file; |
| 149 | sector_t last_block; |
| 150 | sector_t last_block_in_file; |
| 151 | sector_t blocks[MAX_BUF_PER_PAGE]; |
| 152 | unsigned page_block; |
| 153 | struct block_device *bdev = inode->i_sb->s_bdev; |
| 154 | int length; |
| 155 | unsigned relative_block = 0; |
| 156 | struct ext4_map_blocks map; |
| 157 | |
| 158 | map.m_pblk = 0; |
| 159 | map.m_lblk = 0; |
| 160 | map.m_len = 0; |
| 161 | map.m_flags = 0; |
| 162 | |
yalin wang | de9e918 | 2016-07-05 16:32:32 -0400 | [diff] [blame] | 163 | for (; nr_pages; nr_pages--) { |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 164 | int fully_mapped = 1; |
| 165 | unsigned first_hole = blocks_per_page; |
| 166 | |
| 167 | prefetchw(&page->flags); |
| 168 | if (pages) { |
| 169 | page = list_entry(pages->prev, struct page, lru); |
| 170 | list_del(&page->lru); |
Michal Hocko | 063d99b | 2015-10-15 15:28:24 -0700 | [diff] [blame] | 171 | if (add_to_page_cache_lru(page, mapping, page->index, |
Michal Hocko | 8a5c743 | 2016-07-26 15:24:53 -0700 | [diff] [blame] | 172 | readahead_gfp_mask(mapping))) |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 173 | goto next_page; |
| 174 | } |
| 175 | |
| 176 | if (page_has_buffers(page)) |
| 177 | goto confused; |
| 178 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 179 | block_in_file = (sector_t)page->index << (PAGE_SHIFT - blkbits); |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 180 | last_block = block_in_file + nr_pages * blocks_per_page; |
| 181 | last_block_in_file = (i_size_read(inode) + blocksize - 1) >> blkbits; |
| 182 | if (last_block > last_block_in_file) |
| 183 | last_block = last_block_in_file; |
| 184 | page_block = 0; |
| 185 | |
| 186 | /* |
| 187 | * Map blocks using the previous result first. |
| 188 | */ |
| 189 | if ((map.m_flags & EXT4_MAP_MAPPED) && |
| 190 | block_in_file > map.m_lblk && |
| 191 | block_in_file < (map.m_lblk + map.m_len)) { |
| 192 | unsigned map_offset = block_in_file - map.m_lblk; |
| 193 | unsigned last = map.m_len - map_offset; |
| 194 | |
| 195 | for (relative_block = 0; ; relative_block++) { |
| 196 | if (relative_block == last) { |
| 197 | /* needed? */ |
| 198 | map.m_flags &= ~EXT4_MAP_MAPPED; |
| 199 | break; |
| 200 | } |
| 201 | if (page_block == blocks_per_page) |
| 202 | break; |
| 203 | blocks[page_block] = map.m_pblk + map_offset + |
| 204 | relative_block; |
| 205 | page_block++; |
| 206 | block_in_file++; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /* |
| 211 | * Then do more ext4_map_blocks() calls until we are |
| 212 | * done with this page. |
| 213 | */ |
| 214 | while (page_block < blocks_per_page) { |
| 215 | if (block_in_file < last_block) { |
| 216 | map.m_lblk = block_in_file; |
| 217 | map.m_len = last_block - block_in_file; |
| 218 | |
| 219 | if (ext4_map_blocks(NULL, inode, &map, 0) < 0) { |
| 220 | set_error_page: |
| 221 | SetPageError(page); |
| 222 | zero_user_segment(page, 0, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 223 | PAGE_SIZE); |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 224 | unlock_page(page); |
| 225 | goto next_page; |
| 226 | } |
| 227 | } |
| 228 | if ((map.m_flags & EXT4_MAP_MAPPED) == 0) { |
| 229 | fully_mapped = 0; |
| 230 | if (first_hole == blocks_per_page) |
| 231 | first_hole = page_block; |
| 232 | page_block++; |
| 233 | block_in_file++; |
| 234 | continue; |
| 235 | } |
| 236 | if (first_hole != blocks_per_page) |
| 237 | goto confused; /* hole -> non-hole */ |
| 238 | |
| 239 | /* Contiguous blocks? */ |
| 240 | if (page_block && blocks[page_block-1] != map.m_pblk-1) |
| 241 | goto confused; |
| 242 | for (relative_block = 0; ; relative_block++) { |
| 243 | if (relative_block == map.m_len) { |
| 244 | /* needed? */ |
| 245 | map.m_flags &= ~EXT4_MAP_MAPPED; |
| 246 | break; |
| 247 | } else if (page_block == blocks_per_page) |
| 248 | break; |
| 249 | blocks[page_block] = map.m_pblk+relative_block; |
| 250 | page_block++; |
| 251 | block_in_file++; |
| 252 | } |
| 253 | } |
| 254 | if (first_hole != blocks_per_page) { |
| 255 | zero_user_segment(page, first_hole << blkbits, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 256 | PAGE_SIZE); |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 257 | if (first_hole == 0) { |
| 258 | SetPageUptodate(page); |
| 259 | unlock_page(page); |
| 260 | goto next_page; |
| 261 | } |
| 262 | } else if (fully_mapped) { |
| 263 | SetPageMappedToDisk(page); |
| 264 | } |
| 265 | if (fully_mapped && blocks_per_page == 1 && |
| 266 | !PageUptodate(page) && cleancache_get_page(page) == 0) { |
| 267 | SetPageUptodate(page); |
| 268 | goto confused; |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | * This page will go to BIO. Do we need to send this |
| 273 | * BIO off first? |
| 274 | */ |
| 275 | if (bio && (last_block_in_bio != blocks[0] - 1)) { |
| 276 | submit_and_realloc: |
Mohan Srinivasan | 25cc70f | 2016-12-14 16:39:51 -0800 | [diff] [blame] | 277 | ext4_submit_bio_read(bio); |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 278 | bio = NULL; |
| 279 | } |
| 280 | if (bio == NULL) { |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 281 | struct fscrypt_ctx *ctx = NULL; |
Michael Halcrow | c9c7429 | 2015-04-12 00:56:10 -0400 | [diff] [blame] | 282 | |
| 283 | if (ext4_encrypted_inode(inode) && |
| 284 | S_ISREG(inode->i_mode)) { |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 285 | ctx = fscrypt_get_ctx(inode, GFP_NOFS); |
Michael Halcrow | c9c7429 | 2015-04-12 00:56:10 -0400 | [diff] [blame] | 286 | if (IS_ERR(ctx)) |
| 287 | goto set_error_page; |
| 288 | } |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 289 | bio = bio_alloc(GFP_KERNEL, |
Kent Overstreet | b54ffb7 | 2015-05-19 14:31:01 +0200 | [diff] [blame] | 290 | min_t(int, nr_pages, BIO_MAX_PAGES)); |
Michael Halcrow | c9c7429 | 2015-04-12 00:56:10 -0400 | [diff] [blame] | 291 | if (!bio) { |
| 292 | if (ctx) |
Jaegeuk Kim | a7550b3 | 2016-07-10 14:01:03 -0400 | [diff] [blame] | 293 | fscrypt_release_ctx(ctx); |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 294 | goto set_error_page; |
Michael Halcrow | c9c7429 | 2015-04-12 00:56:10 -0400 | [diff] [blame] | 295 | } |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 296 | bio->bi_bdev = bdev; |
| 297 | bio->bi_iter.bi_sector = blocks[0] << (blkbits - 9); |
| 298 | bio->bi_end_io = mpage_end_io; |
Michael Halcrow | c9c7429 | 2015-04-12 00:56:10 -0400 | [diff] [blame] | 299 | bio->bi_private = ctx; |
Michael Halcrow | e93c1c1 | 2017-10-11 16:36:05 -0700 | [diff] [blame^] | 300 | bio_set_op_attrs(bio, REQ_OP_READ, |
| 301 | ctx ? REQ_NOENCRYPT : 0); |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | length = first_hole << blkbits; |
| 305 | if (bio_add_page(bio, page, length, 0) < length) |
| 306 | goto submit_and_realloc; |
| 307 | |
| 308 | if (((map.m_flags & EXT4_MAP_BOUNDARY) && |
| 309 | (relative_block == map.m_len)) || |
| 310 | (first_hole != blocks_per_page)) { |
Mohan Srinivasan | 25cc70f | 2016-12-14 16:39:51 -0800 | [diff] [blame] | 311 | ext4_submit_bio_read(bio); |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 312 | bio = NULL; |
| 313 | } else |
| 314 | last_block_in_bio = blocks[blocks_per_page - 1]; |
| 315 | goto next_page; |
| 316 | confused: |
| 317 | if (bio) { |
Mohan Srinivasan | 25cc70f | 2016-12-14 16:39:51 -0800 | [diff] [blame] | 318 | ext4_submit_bio_read(bio); |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 319 | bio = NULL; |
| 320 | } |
| 321 | if (!PageUptodate(page)) |
| 322 | block_read_full_page(page, ext4_get_block); |
| 323 | else |
| 324 | unlock_page(page); |
| 325 | next_page: |
| 326 | if (pages) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 327 | put_page(page); |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 328 | } |
| 329 | BUG_ON(pages && !list_empty(pages)); |
| 330 | if (bio) |
Mohan Srinivasan | 25cc70f | 2016-12-14 16:39:51 -0800 | [diff] [blame] | 331 | ext4_submit_bio_read(bio); |
Theodore Ts'o | f64e02f | 2015-04-08 00:00:32 -0400 | [diff] [blame] | 332 | return 0; |
| 333 | } |