blob: 680ba60863ffb2dee0f81ffbda6974c419c79668 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Camie1f8e872008-10-15 22:01:59 -07009 * 15May2002 Andrew Morton
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * 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>
19#include <linux/bio.h>
20#include <linux/fs.h>
21#include <linux/buffer_head.h>
22#include <linux/blkdev.h>
23#include <linux/highmem.h>
24#include <linux/prefetch.h>
25#include <linux/mpage.h>
26#include <linux/writeback.h>
27#include <linux/backing-dev.h>
28#include <linux/pagevec.h>
29
30/*
31 * I/O completion handler for multipage BIOs.
32 *
33 * The mpage code never puts partial pages into a BIO (except for end-of-file).
34 * If a page does not map to a contiguous run of blocks then it simply falls
35 * back to block_read_full_page().
36 *
37 * Why is this? If a page's completion depends on a number of different BIOs
38 * which can complete in any order (or at the same time) then determining the
39 * status of that page is hard. See end_buffer_async_read() for the details.
40 * There is no point in duplicating all that complexity.
41 */
NeilBrown6712ecf2007-09-27 12:47:43 +020042static void mpage_end_io_read(struct bio *bio, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
44 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
45 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 do {
48 struct page *page = bvec->bv_page;
49
50 if (--bvec >= bio->bi_io_vec)
51 prefetchw(&bvec->bv_page->flags);
52
53 if (uptodate) {
54 SetPageUptodate(page);
55 } else {
56 ClearPageUptodate(page);
57 SetPageError(page);
58 }
59 unlock_page(page);
60 } while (bvec >= bio->bi_io_vec);
61 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
63
NeilBrown6712ecf2007-09-27 12:47:43 +020064static void mpage_end_io_write(struct bio *bio, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
66 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
67 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 do {
70 struct page *page = bvec->bv_page;
71
72 if (--bvec >= bio->bi_io_vec)
73 prefetchw(&bvec->bv_page->flags);
74
Qu Fuping854715b2005-06-04 15:43:29 -070075 if (!uptodate){
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 SetPageError(page);
Qu Fuping854715b2005-06-04 15:43:29 -070077 if (page->mapping)
78 set_bit(AS_EIO, &page->mapping->flags);
79 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 end_page_writeback(page);
81 } while (bvec >= bio->bi_io_vec);
82 bio_put(bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
Dmitri Vorobievced117c2009-03-31 00:41:20 +030085static struct bio *mpage_bio_submit(int rw, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 bio->bi_end_io = mpage_end_io_read;
88 if (rw == WRITE)
89 bio->bi_end_io = mpage_end_io_write;
90 submit_bio(rw, bio);
91 return NULL;
92}
93
94static struct bio *
95mpage_alloc(struct block_device *bdev,
96 sector_t first_sector, int nr_vecs,
Al Virodd0fc662005-10-07 07:46:04 +010097 gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 struct bio *bio;
100
101 bio = bio_alloc(gfp_flags, nr_vecs);
102
103 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
104 while (!bio && (nr_vecs /= 2))
105 bio = bio_alloc(gfp_flags, nr_vecs);
106 }
107
108 if (bio) {
109 bio->bi_bdev = bdev;
110 bio->bi_sector = first_sector;
111 }
112 return bio;
113}
114
115/*
116 * support function for mpage_readpages. The fs supplied get_block might
117 * return an up to date buffer. This is used to map that buffer into
118 * the page, which allows readpage to avoid triggering a duplicate call
119 * to get_block.
120 *
121 * The idea is to avoid adding buffers to pages that don't already have
122 * them. So when the buffer is up to date and the page size == block size,
123 * this marks the page up to date instead of adding new buffers.
124 */
125static void
126map_buffer_to_page(struct page *page, struct buffer_head *bh, int page_block)
127{
128 struct inode *inode = page->mapping->host;
129 struct buffer_head *page_bh, *head;
130 int block = 0;
131
132 if (!page_has_buffers(page)) {
133 /*
134 * don't make any buffers if there is only one buffer on
135 * the page and the page just needs to be set up to date
136 */
137 if (inode->i_blkbits == PAGE_CACHE_SHIFT &&
138 buffer_uptodate(bh)) {
139 SetPageUptodate(page);
140 return;
141 }
142 create_empty_buffers(page, 1 << inode->i_blkbits, 0);
143 }
144 head = page_buffers(page);
145 page_bh = head;
146 do {
147 if (block == page_block) {
148 page_bh->b_state = bh->b_state;
149 page_bh->b_bdev = bh->b_bdev;
150 page_bh->b_blocknr = bh->b_blocknr;
151 break;
152 }
153 page_bh = page_bh->b_this_page;
154 block++;
155 } while (page_bh != head);
156}
157
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800158/*
159 * This is the worker routine which does all the work of mapping the disk
160 * blocks and constructs largest possible bios, submits them for IO if the
161 * blocks are not contiguous on the disk.
162 *
163 * We pass a buffer_head back and forth and use its buffer_mapped() flag to
164 * represent the validity of its disk mapping and to decide when to do the next
165 * get_block() call.
166 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static struct bio *
168do_mpage_readpage(struct bio *bio, struct page *page, unsigned nr_pages,
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800169 sector_t *last_block_in_bio, struct buffer_head *map_bh,
170 unsigned long *first_logical_block, get_block_t get_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
172 struct inode *inode = page->mapping->host;
173 const unsigned blkbits = inode->i_blkbits;
174 const unsigned blocks_per_page = PAGE_CACHE_SIZE >> blkbits;
175 const unsigned blocksize = 1 << blkbits;
176 sector_t block_in_file;
177 sector_t last_block;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800178 sector_t last_block_in_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 sector_t blocks[MAX_BUF_PER_PAGE];
180 unsigned page_block;
181 unsigned first_hole = blocks_per_page;
182 struct block_device *bdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 int length;
184 int fully_mapped = 1;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800185 unsigned nblocks;
186 unsigned relative_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 if (page_has_buffers(page))
189 goto confused;
190
Andrew Morton54b21a72006-01-08 01:03:05 -0800191 block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits);
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800192 last_block = block_in_file + nr_pages * blocks_per_page;
193 last_block_in_file = (i_size_read(inode) + blocksize - 1) >> blkbits;
194 if (last_block > last_block_in_file)
195 last_block = last_block_in_file;
196 page_block = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800198 /*
199 * Map blocks using the result from the previous get_blocks call first.
200 */
201 nblocks = map_bh->b_size >> blkbits;
202 if (buffer_mapped(map_bh) && block_in_file > *first_logical_block &&
203 block_in_file < (*first_logical_block + nblocks)) {
204 unsigned map_offset = block_in_file - *first_logical_block;
205 unsigned last = nblocks - map_offset;
206
207 for (relative_block = 0; ; relative_block++) {
208 if (relative_block == last) {
209 clear_buffer_mapped(map_bh);
210 break;
211 }
212 if (page_block == blocks_per_page)
213 break;
214 blocks[page_block] = map_bh->b_blocknr + map_offset +
215 relative_block;
216 page_block++;
217 block_in_file++;
218 }
219 bdev = map_bh->b_bdev;
220 }
221
222 /*
223 * Then do more get_blocks calls until we are done with this page.
224 */
225 map_bh->b_page = page;
226 while (page_block < blocks_per_page) {
227 map_bh->b_state = 0;
228 map_bh->b_size = 0;
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 if (block_in_file < last_block) {
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800231 map_bh->b_size = (last_block-block_in_file) << blkbits;
232 if (get_block(inode, block_in_file, map_bh, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 goto confused;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800234 *first_logical_block = block_in_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800237 if (!buffer_mapped(map_bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 fully_mapped = 0;
239 if (first_hole == blocks_per_page)
240 first_hole = page_block;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800241 page_block++;
242 block_in_file++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 continue;
244 }
245
246 /* some filesystems will copy data into the page during
247 * the get_block call, in which case we don't want to
248 * read it again. map_buffer_to_page copies the data
249 * we just collected from get_block into the page's buffers
250 * so readpage doesn't have to repeat the get_block call
251 */
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800252 if (buffer_uptodate(map_bh)) {
253 map_buffer_to_page(page, map_bh, page_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 goto confused;
255 }
256
257 if (first_hole != blocks_per_page)
258 goto confused; /* hole -> non-hole */
259
260 /* Contiguous blocks? */
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800261 if (page_block && blocks[page_block-1] != map_bh->b_blocknr-1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 goto confused;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800263 nblocks = map_bh->b_size >> blkbits;
264 for (relative_block = 0; ; relative_block++) {
265 if (relative_block == nblocks) {
266 clear_buffer_mapped(map_bh);
267 break;
268 } else if (page_block == blocks_per_page)
269 break;
270 blocks[page_block] = map_bh->b_blocknr+relative_block;
271 page_block++;
272 block_in_file++;
273 }
274 bdev = map_bh->b_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
276
277 if (first_hole != blocks_per_page) {
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800278 zero_user_segment(page, first_hole << blkbits, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (first_hole == 0) {
280 SetPageUptodate(page);
281 unlock_page(page);
282 goto out;
283 }
284 } else if (fully_mapped) {
285 SetPageMappedToDisk(page);
286 }
287
288 /*
289 * This page will go to BIO. Do we need to send this BIO off first?
290 */
291 if (bio && (*last_block_in_bio != blocks[0] - 1))
292 bio = mpage_bio_submit(READ, bio);
293
294alloc_new:
295 if (bio == NULL) {
296 bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
297 min_t(int, nr_pages, bio_get_nr_vecs(bdev)),
298 GFP_KERNEL);
299 if (bio == NULL)
300 goto confused;
301 }
302
303 length = first_hole << blkbits;
304 if (bio_add_page(bio, page, length, 0) < length) {
305 bio = mpage_bio_submit(READ, bio);
306 goto alloc_new;
307 }
308
Miquel van Smoorenburg38c8e612009-01-06 14:39:02 -0800309 relative_block = block_in_file - *first_logical_block;
310 nblocks = map_bh->b_size >> blkbits;
311 if ((buffer_boundary(map_bh) && relative_block == nblocks) ||
312 (first_hole != blocks_per_page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 bio = mpage_bio_submit(READ, bio);
314 else
315 *last_block_in_bio = blocks[blocks_per_page - 1];
316out:
317 return bio;
318
319confused:
320 if (bio)
321 bio = mpage_bio_submit(READ, bio);
322 if (!PageUptodate(page))
323 block_read_full_page(page, get_block);
324 else
325 unlock_page(page);
326 goto out;
327}
328
Martin Waitz67be2dd2005-05-01 08:59:26 -0700329/**
Randy Dunlap78a4a502008-02-29 22:02:31 -0800330 * mpage_readpages - populate an address space with some pages & start reads against them
Martin Waitz67be2dd2005-05-01 08:59:26 -0700331 * @mapping: the address_space
332 * @pages: The address of a list_head which contains the target pages. These
333 * pages have their ->index populated and are otherwise uninitialised.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700334 * The page at @pages->prev has the lowest file offset, and reads should be
335 * issued in @pages->prev to @pages->next order.
Martin Waitz67be2dd2005-05-01 08:59:26 -0700336 * @nr_pages: The number of pages at *@pages
337 * @get_block: The filesystem's block mapper function.
338 *
339 * This function walks the pages and the blocks within each page, building and
340 * emitting large BIOs.
341 *
342 * If anything unusual happens, such as:
343 *
344 * - encountering a page which has buffers
345 * - encountering a page which has a non-hole after a hole
346 * - encountering a page with non-contiguous blocks
347 *
348 * then this code just gives up and calls the buffer_head-based read function.
349 * It does handle a page which has holes at the end - that is a common case:
350 * the end-of-file on blocksize < PAGE_CACHE_SIZE setups.
351 *
352 * BH_Boundary explanation:
353 *
354 * There is a problem. The mpage read code assembles several pages, gets all
355 * their disk mappings, and then submits them all. That's fine, but obtaining
356 * the disk mappings may require I/O. Reads of indirect blocks, for example.
357 *
358 * So an mpage read of the first 16 blocks of an ext2 file will cause I/O to be
359 * submitted in the following order:
360 * 12 0 1 2 3 4 5 6 7 8 9 10 11 13 14 15 16
Randy Dunlap78a4a502008-02-29 22:02:31 -0800361 *
Martin Waitz67be2dd2005-05-01 08:59:26 -0700362 * because the indirect block has to be read to get the mappings of blocks
363 * 13,14,15,16. Obviously, this impacts performance.
364 *
365 * So what we do it to allow the filesystem's get_block() function to set
366 * BH_Boundary when it maps block 11. BH_Boundary says: mapping of the block
367 * after this one will require I/O against a block which is probably close to
368 * this one. So you should push what I/O you have currently accumulated.
369 *
370 * This all causes the disk requests to be issued in the correct order.
371 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372int
373mpage_readpages(struct address_space *mapping, struct list_head *pages,
374 unsigned nr_pages, get_block_t get_block)
375{
376 struct bio *bio = NULL;
377 unsigned page_idx;
378 sector_t last_block_in_bio = 0;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800379 struct buffer_head map_bh;
380 unsigned long first_logical_block = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800382 clear_buffer_mapped(&map_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
384 struct page *page = list_entry(pages->prev, struct page, lru);
385
386 prefetchw(&page->flags);
387 list_del(&page->lru);
Nick Piggineb2be182007-10-16 01:24:57 -0700388 if (!add_to_page_cache_lru(page, mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 page->index, GFP_KERNEL)) {
390 bio = do_mpage_readpage(bio, page,
391 nr_pages - page_idx,
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800392 &last_block_in_bio, &map_bh,
393 &first_logical_block,
394 get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
Nick Piggineb2be182007-10-16 01:24:57 -0700396 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 BUG_ON(!list_empty(pages));
399 if (bio)
400 mpage_bio_submit(READ, bio);
401 return 0;
402}
403EXPORT_SYMBOL(mpage_readpages);
404
405/*
406 * This isn't called much at all
407 */
408int mpage_readpage(struct page *page, get_block_t get_block)
409{
410 struct bio *bio = NULL;
411 sector_t last_block_in_bio = 0;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800412 struct buffer_head map_bh;
413 unsigned long first_logical_block = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800415 clear_buffer_mapped(&map_bh);
416 bio = do_mpage_readpage(bio, page, 1, &last_block_in_bio,
417 &map_bh, &first_logical_block, get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (bio)
419 mpage_bio_submit(READ, bio);
420 return 0;
421}
422EXPORT_SYMBOL(mpage_readpage);
423
424/*
425 * Writing is not so simple.
426 *
427 * If the page has buffers then they will be used for obtaining the disk
428 * mapping. We only support pages which are fully mapped-and-dirty, with a
429 * special case for pages which are unmapped at the end: end-of-file.
430 *
431 * If the page has no buffers (preferred) then the page is mapped here.
432 *
433 * If all blocks are found to be contiguous then the page can go into the
434 * BIO. Otherwise fall back to the mapping's writepage().
435 *
436 * FIXME: This code wants an estimate of how many pages are still to be
437 * written, so it can intelligently allocate a suitably-sized BIO. For now,
438 * just allocate full-size (16-page) BIOs.
439 */
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700440
Dmitri Vorobievced117c2009-03-31 00:41:20 +0300441struct mpage_data {
442 struct bio *bio;
443 sector_t last_block_in_bio;
444 get_block_t *get_block;
445 unsigned use_writepage;
446};
447
448static int __mpage_writepage(struct page *page, struct writeback_control *wbc,
Alex Tomas29a814d2008-07-11 19:27:31 -0400449 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700451 struct mpage_data *mpd = data;
452 struct bio *bio = mpd->bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 struct address_space *mapping = page->mapping;
454 struct inode *inode = page->mapping->host;
455 const unsigned blkbits = inode->i_blkbits;
456 unsigned long end_index;
457 const unsigned blocks_per_page = PAGE_CACHE_SIZE >> blkbits;
458 sector_t last_block;
459 sector_t block_in_file;
460 sector_t blocks[MAX_BUF_PER_PAGE];
461 unsigned page_block;
462 unsigned first_unmapped = blocks_per_page;
463 struct block_device *bdev = NULL;
464 int boundary = 0;
465 sector_t boundary_block = 0;
466 struct block_device *boundary_bdev = NULL;
467 int length;
468 struct buffer_head map_bh;
469 loff_t i_size = i_size_read(inode);
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700470 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 if (page_has_buffers(page)) {
473 struct buffer_head *head = page_buffers(page);
474 struct buffer_head *bh = head;
475
476 /* If they're all mapped and dirty, do it */
477 page_block = 0;
478 do {
479 BUG_ON(buffer_locked(bh));
480 if (!buffer_mapped(bh)) {
481 /*
482 * unmapped dirty buffers are created by
483 * __set_page_dirty_buffers -> mmapped data
484 */
485 if (buffer_dirty(bh))
486 goto confused;
487 if (first_unmapped == blocks_per_page)
488 first_unmapped = page_block;
489 continue;
490 }
491
492 if (first_unmapped != blocks_per_page)
493 goto confused; /* hole -> non-hole */
494
495 if (!buffer_dirty(bh) || !buffer_uptodate(bh))
496 goto confused;
497 if (page_block) {
498 if (bh->b_blocknr != blocks[page_block-1] + 1)
499 goto confused;
500 }
501 blocks[page_block++] = bh->b_blocknr;
502 boundary = buffer_boundary(bh);
503 if (boundary) {
504 boundary_block = bh->b_blocknr;
505 boundary_bdev = bh->b_bdev;
506 }
507 bdev = bh->b_bdev;
508 } while ((bh = bh->b_this_page) != head);
509
510 if (first_unmapped)
511 goto page_is_mapped;
512
513 /*
514 * Page has buffers, but they are all unmapped. The page was
515 * created by pagein or read over a hole which was handled by
516 * block_read_full_page(). If this address_space is also
517 * using mpage_readpages then this can rarely happen.
518 */
519 goto confused;
520 }
521
522 /*
523 * The page has no buffers: map it to disk
524 */
525 BUG_ON(!PageUptodate(page));
Andrew Morton54b21a72006-01-08 01:03:05 -0800526 block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 last_block = (i_size - 1) >> blkbits;
528 map_bh.b_page = page;
529 for (page_block = 0; page_block < blocks_per_page; ) {
530
531 map_bh.b_state = 0;
Badari Pulavartyb0cf2322006-03-26 01:38:00 -0800532 map_bh.b_size = 1 << blkbits;
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700533 if (mpd->get_block(inode, block_in_file, &map_bh, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 goto confused;
535 if (buffer_new(&map_bh))
536 unmap_underlying_metadata(map_bh.b_bdev,
537 map_bh.b_blocknr);
538 if (buffer_boundary(&map_bh)) {
539 boundary_block = map_bh.b_blocknr;
540 boundary_bdev = map_bh.b_bdev;
541 }
542 if (page_block) {
543 if (map_bh.b_blocknr != blocks[page_block-1] + 1)
544 goto confused;
545 }
546 blocks[page_block++] = map_bh.b_blocknr;
547 boundary = buffer_boundary(&map_bh);
548 bdev = map_bh.b_bdev;
549 if (block_in_file == last_block)
550 break;
551 block_in_file++;
552 }
553 BUG_ON(page_block == 0);
554
555 first_unmapped = page_block;
556
557page_is_mapped:
558 end_index = i_size >> PAGE_CACHE_SHIFT;
559 if (page->index >= end_index) {
560 /*
561 * The page straddles i_size. It must be zeroed out on each
562 * and every writepage invokation because it may be mmapped.
563 * "A file is mapped in multiples of the page size. For a file
564 * that is not a multiple of the page size, the remaining memory
565 * is zeroed when mapped, and writes to that region are not
566 * written out to the file."
567 */
568 unsigned offset = i_size & (PAGE_CACHE_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 if (page->index > end_index || !offset)
571 goto confused;
Christoph Lametereebd2aa2008-02-04 22:28:29 -0800572 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
574
575 /*
576 * This page will go to BIO. Do we need to send this BIO off first?
577 */
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700578 if (bio && mpd->last_block_in_bio != blocks[0] - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 bio = mpage_bio_submit(WRITE, bio);
580
581alloc_new:
582 if (bio == NULL) {
583 bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
584 bio_get_nr_vecs(bdev), GFP_NOFS|__GFP_HIGH);
585 if (bio == NULL)
586 goto confused;
587 }
588
589 /*
590 * Must try to add the page before marking the buffer clean or
591 * the confused fail path above (OOM) will be very confused when
592 * it finds all bh marked clean (i.e. it will not write anything)
593 */
594 length = first_unmapped << blkbits;
595 if (bio_add_page(bio, page, length, 0) < length) {
596 bio = mpage_bio_submit(WRITE, bio);
597 goto alloc_new;
598 }
599
600 /*
601 * OK, we have our BIO, so we can now mark the buffers clean. Make
602 * sure to only clean buffers which we know we'll be writing.
603 */
604 if (page_has_buffers(page)) {
605 struct buffer_head *head = page_buffers(page);
606 struct buffer_head *bh = head;
607 unsigned buffer_counter = 0;
608
609 do {
610 if (buffer_counter++ == first_unmapped)
611 break;
612 clear_buffer_dirty(bh);
613 bh = bh->b_this_page;
614 } while (bh != head);
615
616 /*
617 * we cannot drop the bh if the page is not uptodate
618 * or a concurrent readpage would fail to serialize with the bh
619 * and it would read from disk before we reach the platter.
620 */
621 if (buffer_heads_over_limit && PageUptodate(page))
622 try_to_free_buffers(page);
623 }
624
625 BUG_ON(PageWriteback(page));
626 set_page_writeback(page);
627 unlock_page(page);
628 if (boundary || (first_unmapped != blocks_per_page)) {
629 bio = mpage_bio_submit(WRITE, bio);
630 if (boundary_block) {
631 write_boundary_block(boundary_bdev,
632 boundary_block, 1 << blkbits);
633 }
634 } else {
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700635 mpd->last_block_in_bio = blocks[blocks_per_page - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
637 goto out;
638
639confused:
640 if (bio)
641 bio = mpage_bio_submit(WRITE, bio);
642
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700643 if (mpd->use_writepage) {
644 ret = mapping->a_ops->writepage(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 } else {
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700646 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 goto out;
648 }
649 /*
650 * The caller has a ref on the inode, so *mapping is stable
651 */
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700652 mapping_set_error(mapping, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653out:
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700654 mpd->bio = bio;
655 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
658/**
Randy Dunlap78a4a502008-02-29 22:02:31 -0800659 * mpage_writepages - walk the list of dirty pages of the given address space & writepage() all of them
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 * @mapping: address space structure to write
661 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
662 * @get_block: the filesystem's block mapper function.
663 * If this is NULL then use a_ops->writepage. Otherwise, go
664 * direct-to-BIO.
665 *
666 * This is a library function, which implements the writepages()
667 * address_space_operation.
668 *
669 * If a page is already under I/O, generic_writepages() skips it, even
670 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
671 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
672 * and msync() need to guarantee that all the data which was dirty at the time
673 * the call was made get new I/O started against them. If wbc->sync_mode is
674 * WB_SYNC_ALL then we were called for data integrity and we must wait for
675 * existing IO to complete.
676 */
677int
678mpage_writepages(struct address_space *mapping,
679 struct writeback_control *wbc, get_block_t get_block)
680{
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700681 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700683 if (!get_block)
684 ret = generic_writepages(mapping, wbc);
685 else {
686 struct mpage_data mpd = {
687 .bio = NULL,
688 .last_block_in_bio = 0,
689 .get_block = get_block,
690 .use_writepage = 1,
691 };
692
693 ret = write_cache_pages(mapping, wbc, __mpage_writepage, &mpd);
694 if (mpd.bio)
695 mpage_bio_submit(WRITE, mpd.bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return ret;
698}
699EXPORT_SYMBOL(mpage_writepages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701int mpage_writepage(struct page *page, get_block_t get_block,
702 struct writeback_control *wbc)
703{
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700704 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 Torvalds1da177e2005-04-16 15:20:36 -0700713 return ret;
714}
715EXPORT_SYMBOL(mpage_writepage);