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