Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * fs/mpage.c |
| 4 | * |
| 5 | * Copyright (C) 2002, Linus Torvalds. |
| 6 | * |
| 7 | * Contains functions related to preparing and submitting BIOs which contain |
| 8 | * multiple pagecache pages. |
| 9 | * |
Francois Cami | e1f8e87 | 2008-10-15 22:01:59 -0700 | [diff] [blame] | 10 | * 15May2002 Andrew Morton |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * Initial version |
| 12 | * 27Jun2002 axboe@suse.de |
| 13 | * use bio_add_page() to build bio's just the right size |
| 14 | */ |
| 15 | |
| 16 | #include <linux/kernel.h> |
Paul Gortmaker | 630d9c4 | 2011-11-16 23:57:37 -0500 | [diff] [blame] | 17 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/mm.h> |
| 19 | #include <linux/kdev_t.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/bio.h> |
| 22 | #include <linux/fs.h> |
| 23 | #include <linux/buffer_head.h> |
| 24 | #include <linux/blkdev.h> |
| 25 | #include <linux/highmem.h> |
| 26 | #include <linux/prefetch.h> |
| 27 | #include <linux/mpage.h> |
Andrew Morton | 02c4363 | 2016-03-15 14:55:15 -0700 | [diff] [blame] | 28 | #include <linux/mm_inline.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/writeback.h> |
| 30 | #include <linux/backing-dev.h> |
| 31 | #include <linux/pagevec.h> |
Dan Magenheimer | c515e1f | 2011-05-26 10:01:43 -0600 | [diff] [blame] | 32 | #include <linux/cleancache.h> |
Akinobu Mita | 4db96b7 | 2014-10-09 15:26:55 -0700 | [diff] [blame] | 33 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Mohan Srinivasan | d859afe | 2016-12-14 16:39:51 -0800 | [diff] [blame] | 35 | #define CREATE_TRACE_POINTS |
| 36 | #include <trace/events/android_fs.h> |
| 37 | |
| 38 | EXPORT_TRACEPOINT_SYMBOL(android_fs_datawrite_start); |
| 39 | EXPORT_TRACEPOINT_SYMBOL(android_fs_datawrite_end); |
| 40 | EXPORT_TRACEPOINT_SYMBOL(android_fs_dataread_start); |
| 41 | EXPORT_TRACEPOINT_SYMBOL(android_fs_dataread_end); |
Jaegeuk Kim | 281eb60 | 2019-08-15 19:29:22 -0700 | [diff] [blame] | 42 | EXPORT_TRACEPOINT_SYMBOL(android_fs_fsync_start); |
| 43 | EXPORT_TRACEPOINT_SYMBOL(android_fs_fsync_end); |
Mohan Srinivasan | d859afe | 2016-12-14 16:39:51 -0800 | [diff] [blame] | 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | /* |
| 46 | * I/O completion handler for multipage BIOs. |
| 47 | * |
| 48 | * The mpage code never puts partial pages into a BIO (except for end-of-file). |
| 49 | * If a page does not map to a contiguous run of blocks then it simply falls |
| 50 | * back to block_read_full_page(). |
| 51 | * |
| 52 | * Why is this? If a page's completion depends on a number of different BIOs |
| 53 | * which can complete in any order (or at the same time) then determining the |
| 54 | * status of that page is hard. See end_buffer_async_read() for the details. |
| 55 | * There is no point in duplicating all that complexity. |
| 56 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 57 | static void mpage_end_io(struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | { |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 59 | struct bio_vec *bv; |
| 60 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
Mohan Srinivasan | d859afe | 2016-12-14 16:39:51 -0800 | [diff] [blame] | 62 | if (trace_android_fs_dataread_end_enabled() && |
| 63 | (bio_data_dir(bio) == READ)) { |
| 64 | struct page *first_page = bio->bi_io_vec[0].bv_page; |
| 65 | |
| 66 | if (first_page != NULL) |
| 67 | trace_android_fs_dataread_end(first_page->mapping->host, |
| 68 | page_offset(first_page), |
| 69 | bio->bi_iter.bi_size); |
| 70 | } |
| 71 | |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 72 | bio_for_each_segment_all(bv, bio, i) { |
| 73 | struct page *page = bv->bv_page; |
Tejun Heo | 3f289dc | 2018-07-18 04:47:36 -0700 | [diff] [blame] | 74 | page_endio(page, bio_op(bio), |
| 75 | blk_status_to_errno(bio->bi_status)); |
Kent Overstreet | 2c30c71 | 2013-11-07 12:20:26 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | bio_put(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Mike Christie | eed25cd | 2016-06-05 14:31:59 -0500 | [diff] [blame] | 81 | static struct bio *mpage_bio_submit(int op, int op_flags, struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | { |
Mohan Srinivasan | d859afe | 2016-12-14 16:39:51 -0800 | [diff] [blame] | 83 | if (trace_android_fs_dataread_start_enabled() && (op == REQ_OP_READ)) { |
| 84 | struct page *first_page = bio->bi_io_vec[0].bv_page; |
| 85 | |
| 86 | if (first_page != NULL) { |
| 87 | char *path, pathbuf[MAX_TRACE_PATHBUF_LEN]; |
| 88 | |
| 89 | path = android_fstrace_get_pathname(pathbuf, |
| 90 | MAX_TRACE_PATHBUF_LEN, |
| 91 | first_page->mapping->host); |
| 92 | trace_android_fs_dataread_start( |
| 93 | first_page->mapping->host, |
| 94 | page_offset(first_page), |
| 95 | bio->bi_iter.bi_size, |
| 96 | current->pid, |
| 97 | path, |
| 98 | current->comm); |
| 99 | } |
| 100 | } |
Hai Shan | c32b0d4 | 2011-01-13 15:45:51 -0800 | [diff] [blame] | 101 | bio->bi_end_io = mpage_end_io; |
Mike Christie | eed25cd | 2016-06-05 14:31:59 -0500 | [diff] [blame] | 102 | bio_set_op_attrs(bio, op, op_flags); |
| 103 | guard_bio_eod(op, bio); |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 104 | submit_bio(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | return NULL; |
| 106 | } |
| 107 | |
| 108 | static struct bio * |
| 109 | mpage_alloc(struct block_device *bdev, |
| 110 | sector_t first_sector, int nr_vecs, |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 111 | gfp_t gfp_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | { |
| 113 | struct bio *bio; |
| 114 | |
Michal Hocko | 8a5c743 | 2016-07-26 15:24:53 -0700 | [diff] [blame] | 115 | /* Restrict the given (page cache) mask for slab allocations */ |
| 116 | gfp_flags &= GFP_KERNEL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | bio = bio_alloc(gfp_flags, nr_vecs); |
| 118 | |
| 119 | if (bio == NULL && (current->flags & PF_MEMALLOC)) { |
| 120 | while (!bio && (nr_vecs /= 2)) |
| 121 | bio = bio_alloc(gfp_flags, nr_vecs); |
| 122 | } |
| 123 | |
| 124 | if (bio) { |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 125 | bio_set_dev(bio, bdev); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 126 | bio->bi_iter.bi_sector = first_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | } |
| 128 | return bio; |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * support function for mpage_readpages. The fs supplied get_block might |
| 133 | * return an up to date buffer. This is used to map that buffer into |
| 134 | * the page, which allows readpage to avoid triggering a duplicate call |
| 135 | * to get_block. |
| 136 | * |
| 137 | * The idea is to avoid adding buffers to pages that don't already have |
| 138 | * them. So when the buffer is up to date and the page size == block size, |
| 139 | * this marks the page up to date instead of adding new buffers. |
| 140 | */ |
| 141 | static void |
| 142 | map_buffer_to_page(struct page *page, struct buffer_head *bh, int page_block) |
| 143 | { |
| 144 | struct inode *inode = page->mapping->host; |
| 145 | struct buffer_head *page_bh, *head; |
| 146 | int block = 0; |
| 147 | |
| 148 | if (!page_has_buffers(page)) { |
| 149 | /* |
| 150 | * don't make any buffers if there is only one buffer on |
| 151 | * the page and the page just needs to be set up to date |
| 152 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 153 | if (inode->i_blkbits == PAGE_SHIFT && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | buffer_uptodate(bh)) { |
| 155 | SetPageUptodate(page); |
| 156 | return; |
| 157 | } |
Fabian Frederick | 9340747 | 2017-02-27 14:28:32 -0800 | [diff] [blame] | 158 | create_empty_buffers(page, i_blocksize(inode), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } |
| 160 | head = page_buffers(page); |
| 161 | page_bh = head; |
| 162 | do { |
| 163 | if (block == page_block) { |
| 164 | page_bh->b_state = bh->b_state; |
| 165 | page_bh->b_bdev = bh->b_bdev; |
| 166 | page_bh->b_blocknr = bh->b_blocknr; |
| 167 | break; |
| 168 | } |
| 169 | page_bh = page_bh->b_this_page; |
| 170 | block++; |
| 171 | } while (page_bh != head); |
| 172 | } |
| 173 | |
Jens Axboe | 357c120 | 2018-08-17 15:45:32 -0700 | [diff] [blame] | 174 | struct mpage_readpage_args { |
| 175 | struct bio *bio; |
| 176 | struct page *page; |
| 177 | unsigned int nr_pages; |
Jens Axboe | 74c8164 | 2018-08-17 15:45:36 -0700 | [diff] [blame] | 178 | bool is_readahead; |
Jens Axboe | 357c120 | 2018-08-17 15:45:32 -0700 | [diff] [blame] | 179 | sector_t last_block_in_bio; |
| 180 | struct buffer_head map_bh; |
| 181 | unsigned long first_logical_block; |
| 182 | get_block_t *get_block; |
Jens Axboe | 357c120 | 2018-08-17 15:45:32 -0700 | [diff] [blame] | 183 | }; |
| 184 | |
Badari Pulavarty | fa30bd0 | 2006-03-26 01:38:01 -0800 | [diff] [blame] | 185 | /* |
| 186 | * This is the worker routine which does all the work of mapping the disk |
| 187 | * blocks and constructs largest possible bios, submits them for IO if the |
| 188 | * blocks are not contiguous on the disk. |
| 189 | * |
| 190 | * We pass a buffer_head back and forth and use its buffer_mapped() flag to |
| 191 | * represent the validity of its disk mapping and to decide when to do the next |
| 192 | * get_block() call. |
| 193 | */ |
Jens Axboe | 357c120 | 2018-08-17 15:45:32 -0700 | [diff] [blame] | 194 | static struct bio *do_mpage_readpage(struct mpage_readpage_args *args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | { |
Jens Axboe | 357c120 | 2018-08-17 15:45:32 -0700 | [diff] [blame] | 196 | struct page *page = args->page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | struct inode *inode = page->mapping->host; |
| 198 | const unsigned blkbits = inode->i_blkbits; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 199 | const unsigned blocks_per_page = PAGE_SIZE >> blkbits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | const unsigned blocksize = 1 << blkbits; |
Jens Axboe | 357c120 | 2018-08-17 15:45:32 -0700 | [diff] [blame] | 201 | struct buffer_head *map_bh = &args->map_bh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | sector_t block_in_file; |
| 203 | sector_t last_block; |
Badari Pulavarty | fa30bd0 | 2006-03-26 01:38:01 -0800 | [diff] [blame] | 204 | sector_t last_block_in_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | sector_t blocks[MAX_BUF_PER_PAGE]; |
| 206 | unsigned page_block; |
| 207 | unsigned first_hole = blocks_per_page; |
| 208 | struct block_device *bdev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | int length; |
| 210 | int fully_mapped = 1; |
Jens Axboe | 74c8164 | 2018-08-17 15:45:36 -0700 | [diff] [blame] | 211 | int op_flags; |
Badari Pulavarty | fa30bd0 | 2006-03-26 01:38:01 -0800 | [diff] [blame] | 212 | unsigned nblocks; |
| 213 | unsigned relative_block; |
Jens Axboe | 74c8164 | 2018-08-17 15:45:36 -0700 | [diff] [blame] | 214 | gfp_t gfp; |
| 215 | |
| 216 | if (args->is_readahead) { |
| 217 | op_flags = REQ_RAHEAD; |
| 218 | gfp = readahead_gfp_mask(page->mapping); |
| 219 | } else { |
| 220 | op_flags = 0; |
| 221 | gfp = mapping_gfp_constraint(page->mapping, GFP_KERNEL); |
| 222 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
| 224 | if (page_has_buffers(page)) |
| 225 | goto confused; |
| 226 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 227 | block_in_file = (sector_t)page->index << (PAGE_SHIFT - blkbits); |
Jens Axboe | 357c120 | 2018-08-17 15:45:32 -0700 | [diff] [blame] | 228 | last_block = block_in_file + args->nr_pages * blocks_per_page; |
Badari Pulavarty | fa30bd0 | 2006-03-26 01:38:01 -0800 | [diff] [blame] | 229 | last_block_in_file = (i_size_read(inode) + blocksize - 1) >> blkbits; |
| 230 | if (last_block > last_block_in_file) |
| 231 | last_block = last_block_in_file; |
| 232 | page_block = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
Badari Pulavarty | fa30bd0 | 2006-03-26 01:38:01 -0800 | [diff] [blame] | 234 | /* |
| 235 | * Map blocks using the result from the previous get_blocks call first. |
| 236 | */ |
| 237 | nblocks = map_bh->b_size >> blkbits; |
Jens Axboe | 357c120 | 2018-08-17 15:45:32 -0700 | [diff] [blame] | 238 | if (buffer_mapped(map_bh) && |
| 239 | block_in_file > args->first_logical_block && |
| 240 | block_in_file < (args->first_logical_block + nblocks)) { |
| 241 | unsigned map_offset = block_in_file - args->first_logical_block; |
Badari Pulavarty | fa30bd0 | 2006-03-26 01:38:01 -0800 | [diff] [blame] | 242 | unsigned last = nblocks - map_offset; |
| 243 | |
| 244 | for (relative_block = 0; ; relative_block++) { |
| 245 | if (relative_block == last) { |
| 246 | clear_buffer_mapped(map_bh); |
| 247 | break; |
| 248 | } |
| 249 | if (page_block == blocks_per_page) |
| 250 | break; |
| 251 | blocks[page_block] = map_bh->b_blocknr + map_offset + |
| 252 | relative_block; |
| 253 | page_block++; |
| 254 | block_in_file++; |
| 255 | } |
| 256 | bdev = map_bh->b_bdev; |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * Then do more get_blocks calls until we are done with this page. |
| 261 | */ |
| 262 | map_bh->b_page = page; |
| 263 | while (page_block < blocks_per_page) { |
| 264 | map_bh->b_state = 0; |
| 265 | map_bh->b_size = 0; |
| 266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | if (block_in_file < last_block) { |
Badari Pulavarty | fa30bd0 | 2006-03-26 01:38:01 -0800 | [diff] [blame] | 268 | map_bh->b_size = (last_block-block_in_file) << blkbits; |
Jens Axboe | 357c120 | 2018-08-17 15:45:32 -0700 | [diff] [blame] | 269 | if (args->get_block(inode, block_in_file, map_bh, 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | goto confused; |
Jens Axboe | 357c120 | 2018-08-17 15:45:32 -0700 | [diff] [blame] | 271 | args->first_logical_block = block_in_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Badari Pulavarty | fa30bd0 | 2006-03-26 01:38:01 -0800 | [diff] [blame] | 274 | if (!buffer_mapped(map_bh)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | fully_mapped = 0; |
| 276 | if (first_hole == blocks_per_page) |
| 277 | first_hole = page_block; |
Badari Pulavarty | fa30bd0 | 2006-03-26 01:38:01 -0800 | [diff] [blame] | 278 | page_block++; |
| 279 | block_in_file++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | continue; |
| 281 | } |
| 282 | |
| 283 | /* some filesystems will copy data into the page during |
| 284 | * the get_block call, in which case we don't want to |
| 285 | * read it again. map_buffer_to_page copies the data |
| 286 | * we just collected from get_block into the page's buffers |
| 287 | * so readpage doesn't have to repeat the get_block call |
| 288 | */ |
Badari Pulavarty | fa30bd0 | 2006-03-26 01:38:01 -0800 | [diff] [blame] | 289 | if (buffer_uptodate(map_bh)) { |
| 290 | map_buffer_to_page(page, map_bh, page_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | goto confused; |
| 292 | } |
| 293 | |
| 294 | if (first_hole != blocks_per_page) |
| 295 | goto confused; /* hole -> non-hole */ |
| 296 | |
| 297 | /* Contiguous blocks? */ |
Badari Pulavarty | fa30bd0 | 2006-03-26 01:38:01 -0800 | [diff] [blame] | 298 | if (page_block && blocks[page_block-1] != map_bh->b_blocknr-1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | goto confused; |
Badari Pulavarty | fa30bd0 | 2006-03-26 01:38:01 -0800 | [diff] [blame] | 300 | nblocks = map_bh->b_size >> blkbits; |
| 301 | for (relative_block = 0; ; relative_block++) { |
| 302 | if (relative_block == nblocks) { |
| 303 | clear_buffer_mapped(map_bh); |
| 304 | break; |
| 305 | } else if (page_block == blocks_per_page) |
| 306 | break; |
| 307 | blocks[page_block] = map_bh->b_blocknr+relative_block; |
| 308 | page_block++; |
| 309 | block_in_file++; |
| 310 | } |
| 311 | bdev = map_bh->b_bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | if (first_hole != blocks_per_page) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 315 | zero_user_segment(page, first_hole << blkbits, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | if (first_hole == 0) { |
| 317 | SetPageUptodate(page); |
| 318 | unlock_page(page); |
| 319 | goto out; |
| 320 | } |
| 321 | } else if (fully_mapped) { |
| 322 | SetPageMappedToDisk(page); |
| 323 | } |
| 324 | |
Dan Magenheimer | c515e1f | 2011-05-26 10:01:43 -0600 | [diff] [blame] | 325 | if (fully_mapped && blocks_per_page == 1 && !PageUptodate(page) && |
| 326 | cleancache_get_page(page) == 0) { |
| 327 | SetPageUptodate(page); |
| 328 | goto confused; |
| 329 | } |
| 330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | /* |
| 332 | * This page will go to BIO. Do we need to send this BIO off first? |
| 333 | */ |
Jens Axboe | 357c120 | 2018-08-17 15:45:32 -0700 | [diff] [blame] | 334 | if (args->bio && (args->last_block_in_bio != blocks[0] - 1)) |
Jens Axboe | 74c8164 | 2018-08-17 15:45:36 -0700 | [diff] [blame] | 335 | args->bio = mpage_bio_submit(REQ_OP_READ, op_flags, args->bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
| 337 | alloc_new: |
Jens Axboe | 357c120 | 2018-08-17 15:45:32 -0700 | [diff] [blame] | 338 | if (args->bio == NULL) { |
Matthew Wilcox | 47a191f | 2014-06-04 16:07:46 -0700 | [diff] [blame] | 339 | if (first_hole == blocks_per_page) { |
| 340 | if (!bdev_read_page(bdev, blocks[0] << (blkbits - 9), |
| 341 | page)) |
| 342 | goto out; |
| 343 | } |
Jens Axboe | 357c120 | 2018-08-17 15:45:32 -0700 | [diff] [blame] | 344 | args->bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9), |
| 345 | min_t(int, args->nr_pages, |
| 346 | BIO_MAX_PAGES), |
Jens Axboe | 74c8164 | 2018-08-17 15:45:36 -0700 | [diff] [blame] | 347 | gfp); |
Jens Axboe | 357c120 | 2018-08-17 15:45:32 -0700 | [diff] [blame] | 348 | if (args->bio == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | goto confused; |
| 350 | } |
| 351 | |
| 352 | length = first_hole << blkbits; |
Jens Axboe | 357c120 | 2018-08-17 15:45:32 -0700 | [diff] [blame] | 353 | if (bio_add_page(args->bio, page, length, 0) < length) { |
Jens Axboe | 74c8164 | 2018-08-17 15:45:36 -0700 | [diff] [blame] | 354 | args->bio = mpage_bio_submit(REQ_OP_READ, op_flags, args->bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | goto alloc_new; |
| 356 | } |
| 357 | |
Jens Axboe | 357c120 | 2018-08-17 15:45:32 -0700 | [diff] [blame] | 358 | relative_block = block_in_file - args->first_logical_block; |
Miquel van Smoorenburg | 38c8e61 | 2009-01-06 14:39:02 -0800 | [diff] [blame] | 359 | nblocks = map_bh->b_size >> blkbits; |
| 360 | if ((buffer_boundary(map_bh) && relative_block == nblocks) || |
| 361 | (first_hole != blocks_per_page)) |
Jens Axboe | 74c8164 | 2018-08-17 15:45:36 -0700 | [diff] [blame] | 362 | args->bio = mpage_bio_submit(REQ_OP_READ, op_flags, args->bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | else |
Jens Axboe | 357c120 | 2018-08-17 15:45:32 -0700 | [diff] [blame] | 364 | args->last_block_in_bio = blocks[blocks_per_page - 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | out: |
Jens Axboe | 357c120 | 2018-08-17 15:45:32 -0700 | [diff] [blame] | 366 | return args->bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
| 368 | confused: |
Jens Axboe | 357c120 | 2018-08-17 15:45:32 -0700 | [diff] [blame] | 369 | if (args->bio) |
Jens Axboe | 74c8164 | 2018-08-17 15:45:36 -0700 | [diff] [blame] | 370 | args->bio = mpage_bio_submit(REQ_OP_READ, op_flags, args->bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | if (!PageUptodate(page)) |
Jens Axboe | 357c120 | 2018-08-17 15:45:32 -0700 | [diff] [blame] | 372 | block_read_full_page(page, args->get_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | else |
| 374 | unlock_page(page); |
| 375 | goto out; |
| 376 | } |
| 377 | |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 378 | /** |
Randy Dunlap | 78a4a50 | 2008-02-29 22:02:31 -0800 | [diff] [blame] | 379 | * mpage_readpages - populate an address space with some pages & start reads against them |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 380 | * @mapping: the address_space |
| 381 | * @pages: The address of a list_head which contains the target pages. These |
| 382 | * pages have their ->index populated and are otherwise uninitialised. |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 383 | * The page at @pages->prev has the lowest file offset, and reads should be |
| 384 | * issued in @pages->prev to @pages->next order. |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 385 | * @nr_pages: The number of pages at *@pages |
| 386 | * @get_block: The filesystem's block mapper function. |
| 387 | * |
| 388 | * This function walks the pages and the blocks within each page, building and |
| 389 | * emitting large BIOs. |
| 390 | * |
| 391 | * If anything unusual happens, such as: |
| 392 | * |
| 393 | * - encountering a page which has buffers |
| 394 | * - encountering a page which has a non-hole after a hole |
| 395 | * - encountering a page with non-contiguous blocks |
| 396 | * |
| 397 | * then this code just gives up and calls the buffer_head-based read function. |
| 398 | * 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] | 399 | * the end-of-file on blocksize < PAGE_SIZE setups. |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 400 | * |
| 401 | * BH_Boundary explanation: |
| 402 | * |
| 403 | * There is a problem. The mpage read code assembles several pages, gets all |
| 404 | * their disk mappings, and then submits them all. That's fine, but obtaining |
| 405 | * the disk mappings may require I/O. Reads of indirect blocks, for example. |
| 406 | * |
| 407 | * So an mpage read of the first 16 blocks of an ext2 file will cause I/O to be |
| 408 | * submitted in the following order: |
Mauro Carvalho Chehab | 0117d42 | 2017-05-12 07:45:42 -0300 | [diff] [blame] | 409 | * |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 410 | * 12 0 1 2 3 4 5 6 7 8 9 10 11 13 14 15 16 |
Randy Dunlap | 78a4a50 | 2008-02-29 22:02:31 -0800 | [diff] [blame] | 411 | * |
Martin Waitz | 67be2dd | 2005-05-01 08:59:26 -0700 | [diff] [blame] | 412 | * because the indirect block has to be read to get the mappings of blocks |
| 413 | * 13,14,15,16. Obviously, this impacts performance. |
| 414 | * |
| 415 | * So what we do it to allow the filesystem's get_block() function to set |
| 416 | * BH_Boundary when it maps block 11. BH_Boundary says: mapping of the block |
| 417 | * after this one will require I/O against a block which is probably close to |
| 418 | * this one. So you should push what I/O you have currently accumulated. |
| 419 | * |
| 420 | * This all causes the disk requests to be issued in the correct order. |
| 421 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | int |
| 423 | mpage_readpages(struct address_space *mapping, struct list_head *pages, |
| 424 | unsigned nr_pages, get_block_t get_block) |
| 425 | { |
Jens Axboe | 357c120 | 2018-08-17 15:45:32 -0700 | [diff] [blame] | 426 | struct mpage_readpage_args args = { |
| 427 | .get_block = get_block, |
Jens Axboe | 74c8164 | 2018-08-17 15:45:36 -0700 | [diff] [blame] | 428 | .is_readahead = true, |
Jens Axboe | 357c120 | 2018-08-17 15:45:32 -0700 | [diff] [blame] | 429 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | unsigned page_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | for (page_idx = 0; page_idx < nr_pages; page_idx++) { |
Andrew Morton | 02c4363 | 2016-03-15 14:55:15 -0700 | [diff] [blame] | 433 | struct page *page = lru_to_page(pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
| 435 | prefetchw(&page->flags); |
| 436 | list_del(&page->lru); |
Nick Piggin | eb2be18 | 2007-10-16 01:24:57 -0700 | [diff] [blame] | 437 | if (!add_to_page_cache_lru(page, mapping, |
Michal Hocko | 063d99b | 2015-10-15 15:28:24 -0700 | [diff] [blame] | 438 | page->index, |
Jens Axboe | 74c8164 | 2018-08-17 15:45:36 -0700 | [diff] [blame] | 439 | readahead_gfp_mask(mapping))) { |
Jens Axboe | 357c120 | 2018-08-17 15:45:32 -0700 | [diff] [blame] | 440 | args.page = page; |
| 441 | args.nr_pages = nr_pages - page_idx; |
| 442 | args.bio = do_mpage_readpage(&args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 444 | put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | BUG_ON(!list_empty(pages)); |
Jens Axboe | 357c120 | 2018-08-17 15:45:32 -0700 | [diff] [blame] | 447 | if (args.bio) |
Jens Axboe | 74c8164 | 2018-08-17 15:45:36 -0700 | [diff] [blame] | 448 | mpage_bio_submit(REQ_OP_READ, REQ_RAHEAD, args.bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | return 0; |
| 450 | } |
| 451 | EXPORT_SYMBOL(mpage_readpages); |
| 452 | |
| 453 | /* |
| 454 | * This isn't called much at all |
| 455 | */ |
| 456 | int mpage_readpage(struct page *page, get_block_t get_block) |
| 457 | { |
Jens Axboe | 357c120 | 2018-08-17 15:45:32 -0700 | [diff] [blame] | 458 | struct mpage_readpage_args args = { |
| 459 | .page = page, |
| 460 | .nr_pages = 1, |
| 461 | .get_block = get_block, |
Jens Axboe | 357c120 | 2018-08-17 15:45:32 -0700 | [diff] [blame] | 462 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | |
Jens Axboe | 357c120 | 2018-08-17 15:45:32 -0700 | [diff] [blame] | 464 | args.bio = do_mpage_readpage(&args); |
| 465 | if (args.bio) |
| 466 | mpage_bio_submit(REQ_OP_READ, 0, args.bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | return 0; |
| 468 | } |
| 469 | EXPORT_SYMBOL(mpage_readpage); |
| 470 | |
| 471 | /* |
| 472 | * Writing is not so simple. |
| 473 | * |
| 474 | * If the page has buffers then they will be used for obtaining the disk |
| 475 | * mapping. We only support pages which are fully mapped-and-dirty, with a |
| 476 | * special case for pages which are unmapped at the end: end-of-file. |
| 477 | * |
| 478 | * If the page has no buffers (preferred) then the page is mapped here. |
| 479 | * |
| 480 | * If all blocks are found to be contiguous then the page can go into the |
| 481 | * BIO. Otherwise fall back to the mapping's writepage(). |
| 482 | * |
| 483 | * FIXME: This code wants an estimate of how many pages are still to be |
| 484 | * written, so it can intelligently allocate a suitably-sized BIO. For now, |
| 485 | * just allocate full-size (16-page) BIOs. |
| 486 | */ |
Miklos Szeredi | 0ea9718 | 2007-05-10 22:22:51 -0700 | [diff] [blame] | 487 | |
Dmitri Vorobiev | ced117c | 2009-03-31 00:41:20 +0300 | [diff] [blame] | 488 | struct mpage_data { |
| 489 | struct bio *bio; |
| 490 | sector_t last_block_in_bio; |
| 491 | get_block_t *get_block; |
| 492 | unsigned use_writepage; |
| 493 | }; |
| 494 | |
Matthew Wilcox | 90768ee | 2014-06-04 16:07:44 -0700 | [diff] [blame] | 495 | /* |
| 496 | * We have our BIO, so we can now mark the buffers clean. Make |
| 497 | * sure to only clean buffers which we know we'll be writing. |
| 498 | */ |
| 499 | static void clean_buffers(struct page *page, unsigned first_unmapped) |
| 500 | { |
| 501 | unsigned buffer_counter = 0; |
| 502 | struct buffer_head *bh, *head; |
| 503 | if (!page_has_buffers(page)) |
| 504 | return; |
| 505 | head = page_buffers(page); |
| 506 | bh = head; |
| 507 | |
| 508 | do { |
| 509 | if (buffer_counter++ == first_unmapped) |
| 510 | break; |
| 511 | clear_buffer_dirty(bh); |
| 512 | bh = bh->b_this_page; |
| 513 | } while (bh != head); |
| 514 | |
| 515 | /* |
| 516 | * we cannot drop the bh if the page is not uptodate or a concurrent |
| 517 | * readpage would fail to serialize with the bh and it would read from |
| 518 | * disk before we reach the platter. |
| 519 | */ |
| 520 | if (buffer_heads_over_limit && PageUptodate(page)) |
| 521 | try_to_free_buffers(page); |
| 522 | } |
| 523 | |
Matthew Wilcox | f892760 | 2017-10-13 15:58:15 -0700 | [diff] [blame] | 524 | /* |
| 525 | * For situations where we want to clean all buffers attached to a page. |
| 526 | * We don't need to calculate how many buffers are attached to the page, |
| 527 | * we just need to specify a number larger than the maximum number of buffers. |
| 528 | */ |
| 529 | void clean_page_buffers(struct page *page) |
| 530 | { |
| 531 | clean_buffers(page, ~0U); |
| 532 | } |
| 533 | |
Dmitri Vorobiev | ced117c | 2009-03-31 00:41:20 +0300 | [diff] [blame] | 534 | static int __mpage_writepage(struct page *page, struct writeback_control *wbc, |
Alex Tomas | 29a814d | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 535 | void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | { |
Miklos Szeredi | 0ea9718 | 2007-05-10 22:22:51 -0700 | [diff] [blame] | 537 | struct mpage_data *mpd = data; |
| 538 | struct bio *bio = mpd->bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | struct address_space *mapping = page->mapping; |
| 540 | struct inode *inode = page->mapping->host; |
| 541 | const unsigned blkbits = inode->i_blkbits; |
| 542 | unsigned long end_index; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 543 | const unsigned blocks_per_page = PAGE_SIZE >> blkbits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | sector_t last_block; |
| 545 | sector_t block_in_file; |
| 546 | sector_t blocks[MAX_BUF_PER_PAGE]; |
| 547 | unsigned page_block; |
| 548 | unsigned first_unmapped = blocks_per_page; |
| 549 | struct block_device *bdev = NULL; |
| 550 | int boundary = 0; |
| 551 | sector_t boundary_block = 0; |
| 552 | struct block_device *boundary_bdev = NULL; |
| 553 | int length; |
| 554 | struct buffer_head map_bh; |
| 555 | loff_t i_size = i_size_read(inode); |
Miklos Szeredi | 0ea9718 | 2007-05-10 22:22:51 -0700 | [diff] [blame] | 556 | int ret = 0; |
Jens Axboe | 7637241 | 2016-11-01 10:00:38 -0600 | [diff] [blame] | 557 | int op_flags = wbc_to_write_flags(wbc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
| 559 | if (page_has_buffers(page)) { |
| 560 | struct buffer_head *head = page_buffers(page); |
| 561 | struct buffer_head *bh = head; |
| 562 | |
| 563 | /* If they're all mapped and dirty, do it */ |
| 564 | page_block = 0; |
| 565 | do { |
| 566 | BUG_ON(buffer_locked(bh)); |
| 567 | if (!buffer_mapped(bh)) { |
| 568 | /* |
| 569 | * unmapped dirty buffers are created by |
| 570 | * __set_page_dirty_buffers -> mmapped data |
| 571 | */ |
| 572 | if (buffer_dirty(bh)) |
| 573 | goto confused; |
| 574 | if (first_unmapped == blocks_per_page) |
| 575 | first_unmapped = page_block; |
| 576 | continue; |
| 577 | } |
| 578 | |
| 579 | if (first_unmapped != blocks_per_page) |
| 580 | goto confused; /* hole -> non-hole */ |
| 581 | |
| 582 | if (!buffer_dirty(bh) || !buffer_uptodate(bh)) |
| 583 | goto confused; |
| 584 | if (page_block) { |
| 585 | if (bh->b_blocknr != blocks[page_block-1] + 1) |
| 586 | goto confused; |
| 587 | } |
| 588 | blocks[page_block++] = bh->b_blocknr; |
| 589 | boundary = buffer_boundary(bh); |
| 590 | if (boundary) { |
| 591 | boundary_block = bh->b_blocknr; |
| 592 | boundary_bdev = bh->b_bdev; |
| 593 | } |
| 594 | bdev = bh->b_bdev; |
| 595 | } while ((bh = bh->b_this_page) != head); |
| 596 | |
| 597 | if (first_unmapped) |
| 598 | goto page_is_mapped; |
| 599 | |
| 600 | /* |
| 601 | * Page has buffers, but they are all unmapped. The page was |
| 602 | * created by pagein or read over a hole which was handled by |
| 603 | * block_read_full_page(). If this address_space is also |
| 604 | * using mpage_readpages then this can rarely happen. |
| 605 | */ |
| 606 | goto confused; |
| 607 | } |
| 608 | |
| 609 | /* |
| 610 | * The page has no buffers: map it to disk |
| 611 | */ |
| 612 | BUG_ON(!PageUptodate(page)); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 613 | block_in_file = (sector_t)page->index << (PAGE_SHIFT - blkbits); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | last_block = (i_size - 1) >> blkbits; |
| 615 | map_bh.b_page = page; |
| 616 | for (page_block = 0; page_block < blocks_per_page; ) { |
| 617 | |
| 618 | map_bh.b_state = 0; |
Badari Pulavarty | b0cf232 | 2006-03-26 01:38:00 -0800 | [diff] [blame] | 619 | map_bh.b_size = 1 << blkbits; |
Miklos Szeredi | 0ea9718 | 2007-05-10 22:22:51 -0700 | [diff] [blame] | 620 | if (mpd->get_block(inode, block_in_file, &map_bh, 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | goto confused; |
| 622 | if (buffer_new(&map_bh)) |
Jan Kara | e64855c | 2016-11-04 18:08:15 +0100 | [diff] [blame] | 623 | clean_bdev_bh_alias(&map_bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | if (buffer_boundary(&map_bh)) { |
| 625 | boundary_block = map_bh.b_blocknr; |
| 626 | boundary_bdev = map_bh.b_bdev; |
| 627 | } |
| 628 | if (page_block) { |
| 629 | if (map_bh.b_blocknr != blocks[page_block-1] + 1) |
| 630 | goto confused; |
| 631 | } |
| 632 | blocks[page_block++] = map_bh.b_blocknr; |
| 633 | boundary = buffer_boundary(&map_bh); |
| 634 | bdev = map_bh.b_bdev; |
| 635 | if (block_in_file == last_block) |
| 636 | break; |
| 637 | block_in_file++; |
| 638 | } |
| 639 | BUG_ON(page_block == 0); |
| 640 | |
| 641 | first_unmapped = page_block; |
| 642 | |
| 643 | page_is_mapped: |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 644 | end_index = i_size >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | if (page->index >= end_index) { |
| 646 | /* |
| 647 | * The page straddles i_size. It must be zeroed out on each |
Adam Buchbinder | 2a61aa4 | 2009-12-11 16:35:40 -0500 | [diff] [blame] | 648 | * and every writepage invocation because it may be mmapped. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | * "A file is mapped in multiples of the page size. For a file |
| 650 | * that is not a multiple of the page size, the remaining memory |
| 651 | * is zeroed when mapped, and writes to that region are not |
| 652 | * written out to the file." |
| 653 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 654 | unsigned offset = i_size & (PAGE_SIZE - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | |
| 656 | if (page->index > end_index || !offset) |
| 657 | goto confused; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 658 | zero_user_segment(page, offset, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | /* |
| 662 | * This page will go to BIO. Do we need to send this BIO off first? |
| 663 | */ |
Miklos Szeredi | 0ea9718 | 2007-05-10 22:22:51 -0700 | [diff] [blame] | 664 | if (bio && mpd->last_block_in_bio != blocks[0] - 1) |
Mike Christie | eed25cd | 2016-06-05 14:31:59 -0500 | [diff] [blame] | 665 | bio = mpage_bio_submit(REQ_OP_WRITE, op_flags, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | |
| 667 | alloc_new: |
| 668 | if (bio == NULL) { |
Matthew Wilcox | 47a191f | 2014-06-04 16:07:46 -0700 | [diff] [blame] | 669 | if (first_unmapped == blocks_per_page) { |
| 670 | if (!bdev_write_page(bdev, blocks[0] << (blkbits - 9), |
Matthew Wilcox | f892760 | 2017-10-13 15:58:15 -0700 | [diff] [blame] | 671 | page, wbc)) |
Matthew Wilcox | 47a191f | 2014-06-04 16:07:46 -0700 | [diff] [blame] | 672 | goto out; |
Matthew Wilcox | 47a191f | 2014-06-04 16:07:46 -0700 | [diff] [blame] | 673 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9), |
Kent Overstreet | b54ffb7 | 2015-05-19 14:31:01 +0200 | [diff] [blame] | 675 | BIO_MAX_PAGES, GFP_NOFS|__GFP_HIGH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | if (bio == NULL) |
| 677 | goto confused; |
Tejun Heo | 429b3fb | 2015-05-22 17:14:04 -0400 | [diff] [blame] | 678 | |
Tejun Heo | b16b1de | 2015-06-02 08:39:48 -0600 | [diff] [blame] | 679 | wbc_init_bio(wbc, bio); |
Jens Axboe | 8e8f929 | 2017-06-27 09:30:05 -0600 | [diff] [blame] | 680 | bio->bi_write_hint = inode->i_write_hint; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | /* |
| 684 | * Must try to add the page before marking the buffer clean or |
| 685 | * the confused fail path above (OOM) will be very confused when |
| 686 | * it finds all bh marked clean (i.e. it will not write anything) |
| 687 | */ |
Tejun Heo | 2a81490 | 2015-05-28 14:50:51 -0400 | [diff] [blame] | 688 | wbc_account_io(wbc, page, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | length = first_unmapped << blkbits; |
| 690 | if (bio_add_page(bio, page, length, 0) < length) { |
Mike Christie | eed25cd | 2016-06-05 14:31:59 -0500 | [diff] [blame] | 691 | bio = mpage_bio_submit(REQ_OP_WRITE, op_flags, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | goto alloc_new; |
| 693 | } |
| 694 | |
Matthew Wilcox | 90768ee | 2014-06-04 16:07:44 -0700 | [diff] [blame] | 695 | clean_buffers(page, first_unmapped); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | |
| 697 | BUG_ON(PageWriteback(page)); |
| 698 | set_page_writeback(page); |
| 699 | unlock_page(page); |
| 700 | if (boundary || (first_unmapped != blocks_per_page)) { |
Mike Christie | eed25cd | 2016-06-05 14:31:59 -0500 | [diff] [blame] | 701 | bio = mpage_bio_submit(REQ_OP_WRITE, op_flags, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | if (boundary_block) { |
| 703 | write_boundary_block(boundary_bdev, |
| 704 | boundary_block, 1 << blkbits); |
| 705 | } |
| 706 | } else { |
Miklos Szeredi | 0ea9718 | 2007-05-10 22:22:51 -0700 | [diff] [blame] | 707 | mpd->last_block_in_bio = blocks[blocks_per_page - 1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | } |
| 709 | goto out; |
| 710 | |
| 711 | confused: |
| 712 | if (bio) |
Mike Christie | eed25cd | 2016-06-05 14:31:59 -0500 | [diff] [blame] | 713 | bio = mpage_bio_submit(REQ_OP_WRITE, op_flags, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | |
Miklos Szeredi | 0ea9718 | 2007-05-10 22:22:51 -0700 | [diff] [blame] | 715 | if (mpd->use_writepage) { |
| 716 | ret = mapping->a_ops->writepage(page, wbc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | } else { |
Miklos Szeredi | 0ea9718 | 2007-05-10 22:22:51 -0700 | [diff] [blame] | 718 | ret = -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | goto out; |
| 720 | } |
| 721 | /* |
| 722 | * The caller has a ref on the inode, so *mapping is stable |
| 723 | */ |
Miklos Szeredi | 0ea9718 | 2007-05-10 22:22:51 -0700 | [diff] [blame] | 724 | mapping_set_error(mapping, ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | out: |
Miklos Szeredi | 0ea9718 | 2007-05-10 22:22:51 -0700 | [diff] [blame] | 726 | mpd->bio = bio; |
| 727 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | /** |
Randy Dunlap | 78a4a50 | 2008-02-29 22:02:31 -0800 | [diff] [blame] | 731 | * mpage_writepages - walk the list of dirty pages of the given address space & writepage() all of them |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | * @mapping: address space structure to write |
| 733 | * @wbc: subtract the number of written pages from *@wbc->nr_to_write |
| 734 | * @get_block: the filesystem's block mapper function. |
| 735 | * If this is NULL then use a_ops->writepage. Otherwise, go |
| 736 | * direct-to-BIO. |
| 737 | * |
| 738 | * This is a library function, which implements the writepages() |
| 739 | * address_space_operation. |
| 740 | * |
| 741 | * If a page is already under I/O, generic_writepages() skips it, even |
| 742 | * if it's dirty. This is desirable behaviour for memory-cleaning writeback, |
| 743 | * but it is INCORRECT for data-integrity system calls such as fsync(). fsync() |
| 744 | * and msync() need to guarantee that all the data which was dirty at the time |
| 745 | * the call was made get new I/O started against them. If wbc->sync_mode is |
| 746 | * WB_SYNC_ALL then we were called for data integrity and we must wait for |
| 747 | * existing IO to complete. |
| 748 | */ |
| 749 | int |
| 750 | mpage_writepages(struct address_space *mapping, |
| 751 | struct writeback_control *wbc, get_block_t get_block) |
| 752 | { |
Jens Axboe | 2ed1a6b | 2010-06-22 12:52:14 +0200 | [diff] [blame] | 753 | struct blk_plug plug; |
Miklos Szeredi | 0ea9718 | 2007-05-10 22:22:51 -0700 | [diff] [blame] | 754 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | |
Jens Axboe | 2ed1a6b | 2010-06-22 12:52:14 +0200 | [diff] [blame] | 756 | blk_start_plug(&plug); |
| 757 | |
Miklos Szeredi | 0ea9718 | 2007-05-10 22:22:51 -0700 | [diff] [blame] | 758 | if (!get_block) |
| 759 | ret = generic_writepages(mapping, wbc); |
| 760 | else { |
| 761 | struct mpage_data mpd = { |
| 762 | .bio = NULL, |
| 763 | .last_block_in_bio = 0, |
| 764 | .get_block = get_block, |
| 765 | .use_writepage = 1, |
| 766 | }; |
| 767 | |
| 768 | ret = write_cache_pages(mapping, wbc, __mpage_writepage, &mpd); |
Roman Pen | 5948edb | 2015-09-15 08:27:25 -0600 | [diff] [blame] | 769 | if (mpd.bio) { |
Mike Christie | eed25cd | 2016-06-05 14:31:59 -0500 | [diff] [blame] | 770 | int op_flags = (wbc->sync_mode == WB_SYNC_ALL ? |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 771 | REQ_SYNC : 0); |
Mike Christie | eed25cd | 2016-06-05 14:31:59 -0500 | [diff] [blame] | 772 | mpage_bio_submit(REQ_OP_WRITE, op_flags, mpd.bio); |
Roman Pen | 5948edb | 2015-09-15 08:27:25 -0600 | [diff] [blame] | 773 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | } |
Jens Axboe | 2ed1a6b | 2010-06-22 12:52:14 +0200 | [diff] [blame] | 775 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | return ret; |
| 777 | } |
| 778 | EXPORT_SYMBOL(mpage_writepages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | |
| 780 | int mpage_writepage(struct page *page, get_block_t get_block, |
| 781 | struct writeback_control *wbc) |
| 782 | { |
Miklos Szeredi | 0ea9718 | 2007-05-10 22:22:51 -0700 | [diff] [blame] | 783 | struct mpage_data mpd = { |
| 784 | .bio = NULL, |
| 785 | .last_block_in_bio = 0, |
| 786 | .get_block = get_block, |
| 787 | .use_writepage = 0, |
| 788 | }; |
| 789 | int ret = __mpage_writepage(page, wbc, &mpd); |
Roman Pen | 5948edb | 2015-09-15 08:27:25 -0600 | [diff] [blame] | 790 | if (mpd.bio) { |
Mike Christie | eed25cd | 2016-06-05 14:31:59 -0500 | [diff] [blame] | 791 | int op_flags = (wbc->sync_mode == WB_SYNC_ALL ? |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 792 | REQ_SYNC : 0); |
Mike Christie | eed25cd | 2016-06-05 14:31:59 -0500 | [diff] [blame] | 793 | mpage_bio_submit(REQ_OP_WRITE, op_flags, mpd.bio); |
Roman Pen | 5948edb | 2015-09-15 08:27:25 -0600 | [diff] [blame] | 794 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | return ret; |
| 796 | } |
| 797 | EXPORT_SYMBOL(mpage_writepage); |