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