blob: c1698f2291aa2ee953b54f71a092b4908b4fb343 [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 *
9 * 15May2002 akpm@zip.com.au
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>
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 */
42static int mpage_end_io_read(struct bio *bio, unsigned int bytes_done, int err)
43{
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
47 if (bio->bi_size)
48 return 1;
49
50 do {
51 struct page *page = bvec->bv_page;
52
53 if (--bvec >= bio->bi_io_vec)
54 prefetchw(&bvec->bv_page->flags);
55
56 if (uptodate) {
57 SetPageUptodate(page);
58 } else {
59 ClearPageUptodate(page);
60 SetPageError(page);
61 }
62 unlock_page(page);
63 } while (bvec >= bio->bi_io_vec);
64 bio_put(bio);
65 return 0;
66}
67
68static int mpage_end_io_write(struct bio *bio, unsigned int bytes_done, int err)
69{
70 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
71 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
72
73 if (bio->bi_size)
74 return 1;
75
76 do {
77 struct page *page = bvec->bv_page;
78
79 if (--bvec >= bio->bi_io_vec)
80 prefetchw(&bvec->bv_page->flags);
81
Qu Fuping854715b2005-06-04 15:43:29 -070082 if (!uptodate){
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 SetPageError(page);
Qu Fuping854715b2005-06-04 15:43:29 -070084 if (page->mapping)
85 set_bit(AS_EIO, &page->mapping->flags);
86 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 end_page_writeback(page);
88 } while (bvec >= bio->bi_io_vec);
89 bio_put(bio);
90 return 0;
91}
92
Adrian Bunk75c96f82005-05-05 16:16:09 -070093static struct bio *mpage_bio_submit(int rw, struct bio *bio)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 bio->bi_end_io = mpage_end_io_read;
96 if (rw == WRITE)
97 bio->bi_end_io = mpage_end_io_write;
98 submit_bio(rw, bio);
99 return NULL;
100}
101
102static struct bio *
103mpage_alloc(struct block_device *bdev,
104 sector_t first_sector, int nr_vecs,
Al Virodd0fc662005-10-07 07:46:04 +0100105 gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 struct bio *bio;
108
109 bio = bio_alloc(gfp_flags, nr_vecs);
110
111 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
112 while (!bio && (nr_vecs /= 2))
113 bio = bio_alloc(gfp_flags, nr_vecs);
114 }
115
116 if (bio) {
117 bio->bi_bdev = bdev;
118 bio->bi_sector = first_sector;
119 }
120 return bio;
121}
122
123/*
124 * support function for mpage_readpages. The fs supplied get_block might
125 * return an up to date buffer. This is used to map that buffer into
126 * the page, which allows readpage to avoid triggering a duplicate call
127 * to get_block.
128 *
129 * The idea is to avoid adding buffers to pages that don't already have
130 * them. So when the buffer is up to date and the page size == block size,
131 * this marks the page up to date instead of adding new buffers.
132 */
133static void
134map_buffer_to_page(struct page *page, struct buffer_head *bh, int page_block)
135{
136 struct inode *inode = page->mapping->host;
137 struct buffer_head *page_bh, *head;
138 int block = 0;
139
140 if (!page_has_buffers(page)) {
141 /*
142 * don't make any buffers if there is only one buffer on
143 * the page and the page just needs to be set up to date
144 */
145 if (inode->i_blkbits == PAGE_CACHE_SHIFT &&
146 buffer_uptodate(bh)) {
147 SetPageUptodate(page);
148 return;
149 }
150 create_empty_buffers(page, 1 << inode->i_blkbits, 0);
151 }
152 head = page_buffers(page);
153 page_bh = head;
154 do {
155 if (block == page_block) {
156 page_bh->b_state = bh->b_state;
157 page_bh->b_bdev = bh->b_bdev;
158 page_bh->b_blocknr = bh->b_blocknr;
159 break;
160 }
161 page_bh = page_bh->b_this_page;
162 block++;
163 } while (page_bh != head);
164}
165
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800166/*
167 * This is the worker routine which does all the work of mapping the disk
168 * blocks and constructs largest possible bios, submits them for IO if the
169 * blocks are not contiguous on the disk.
170 *
171 * We pass a buffer_head back and forth and use its buffer_mapped() flag to
172 * represent the validity of its disk mapping and to decide when to do the next
173 * get_block() call.
174 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175static struct bio *
176do_mpage_readpage(struct bio *bio, struct page *page, unsigned nr_pages,
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800177 sector_t *last_block_in_bio, struct buffer_head *map_bh,
178 unsigned long *first_logical_block, get_block_t get_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 struct inode *inode = page->mapping->host;
181 const unsigned blkbits = inode->i_blkbits;
182 const unsigned blocks_per_page = PAGE_CACHE_SIZE >> blkbits;
183 const unsigned blocksize = 1 << blkbits;
184 sector_t block_in_file;
185 sector_t last_block;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800186 sector_t last_block_in_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 sector_t blocks[MAX_BUF_PER_PAGE];
188 unsigned page_block;
189 unsigned first_hole = blocks_per_page;
190 struct block_device *bdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 int length;
192 int fully_mapped = 1;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800193 unsigned nblocks;
194 unsigned relative_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 if (page_has_buffers(page))
197 goto confused;
198
Andrew Morton54b21a72006-01-08 01:03:05 -0800199 block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits);
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800200 last_block = block_in_file + nr_pages * blocks_per_page;
201 last_block_in_file = (i_size_read(inode) + blocksize - 1) >> blkbits;
202 if (last_block > last_block_in_file)
203 last_block = last_block_in_file;
204 page_block = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800206 /*
207 * Map blocks using the result from the previous get_blocks call first.
208 */
209 nblocks = map_bh->b_size >> blkbits;
210 if (buffer_mapped(map_bh) && block_in_file > *first_logical_block &&
211 block_in_file < (*first_logical_block + nblocks)) {
212 unsigned map_offset = block_in_file - *first_logical_block;
213 unsigned last = nblocks - map_offset;
214
215 for (relative_block = 0; ; relative_block++) {
216 if (relative_block == last) {
217 clear_buffer_mapped(map_bh);
218 break;
219 }
220 if (page_block == blocks_per_page)
221 break;
222 blocks[page_block] = map_bh->b_blocknr + map_offset +
223 relative_block;
224 page_block++;
225 block_in_file++;
226 }
227 bdev = map_bh->b_bdev;
228 }
229
230 /*
231 * Then do more get_blocks calls until we are done with this page.
232 */
233 map_bh->b_page = page;
234 while (page_block < blocks_per_page) {
235 map_bh->b_state = 0;
236 map_bh->b_size = 0;
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 if (block_in_file < last_block) {
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800239 map_bh->b_size = (last_block-block_in_file) << blkbits;
240 if (get_block(inode, block_in_file, map_bh, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 goto confused;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800242 *first_logical_block = block_in_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800245 if (!buffer_mapped(map_bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 fully_mapped = 0;
247 if (first_hole == blocks_per_page)
248 first_hole = page_block;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800249 page_block++;
250 block_in_file++;
251 clear_buffer_mapped(map_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 continue;
253 }
254
255 /* some filesystems will copy data into the page during
256 * the get_block call, in which case we don't want to
257 * read it again. map_buffer_to_page copies the data
258 * we just collected from get_block into the page's buffers
259 * so readpage doesn't have to repeat the get_block call
260 */
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800261 if (buffer_uptodate(map_bh)) {
262 map_buffer_to_page(page, map_bh, page_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 goto confused;
264 }
265
266 if (first_hole != blocks_per_page)
267 goto confused; /* hole -> non-hole */
268
269 /* Contiguous blocks? */
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800270 if (page_block && blocks[page_block-1] != map_bh->b_blocknr-1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 goto confused;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800272 nblocks = map_bh->b_size >> blkbits;
273 for (relative_block = 0; ; relative_block++) {
274 if (relative_block == nblocks) {
275 clear_buffer_mapped(map_bh);
276 break;
277 } else if (page_block == blocks_per_page)
278 break;
279 blocks[page_block] = map_bh->b_blocknr+relative_block;
280 page_block++;
281 block_in_file++;
282 }
283 bdev = map_bh->b_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285
286 if (first_hole != blocks_per_page) {
Nate Diller01f27052007-05-09 02:35:07 -0700287 zero_user_page(page, first_hole << blkbits,
288 PAGE_CACHE_SIZE - (first_hole << blkbits),
289 KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (first_hole == 0) {
291 SetPageUptodate(page);
292 unlock_page(page);
293 goto out;
294 }
295 } else if (fully_mapped) {
296 SetPageMappedToDisk(page);
297 }
298
299 /*
300 * This page will go to BIO. Do we need to send this BIO off first?
301 */
302 if (bio && (*last_block_in_bio != blocks[0] - 1))
303 bio = mpage_bio_submit(READ, bio);
304
305alloc_new:
306 if (bio == NULL) {
307 bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
308 min_t(int, nr_pages, bio_get_nr_vecs(bdev)),
309 GFP_KERNEL);
310 if (bio == NULL)
311 goto confused;
312 }
313
314 length = first_hole << blkbits;
315 if (bio_add_page(bio, page, length, 0) < length) {
316 bio = mpage_bio_submit(READ, bio);
317 goto alloc_new;
318 }
319
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800320 if (buffer_boundary(map_bh) || (first_hole != blocks_per_page))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 bio = mpage_bio_submit(READ, bio);
322 else
323 *last_block_in_bio = blocks[blocks_per_page - 1];
324out:
325 return bio;
326
327confused:
328 if (bio)
329 bio = mpage_bio_submit(READ, bio);
330 if (!PageUptodate(page))
331 block_read_full_page(page, get_block);
332 else
333 unlock_page(page);
334 goto out;
335}
336
Martin Waitz67be2dd2005-05-01 08:59:26 -0700337/**
338 * mpage_readpages - populate an address space with some pages, and
339 * start reads against them.
340 *
341 * @mapping: the address_space
342 * @pages: The address of a list_head which contains the target pages. These
343 * pages have their ->index populated and are otherwise uninitialised.
344 *
345 * The page at @pages->prev has the lowest file offset, and reads should be
346 * issued in @pages->prev to @pages->next order.
347 *
348 * @nr_pages: The number of pages at *@pages
349 * @get_block: The filesystem's block mapper function.
350 *
351 * This function walks the pages and the blocks within each page, building and
352 * emitting large BIOs.
353 *
354 * If anything unusual happens, such as:
355 *
356 * - encountering a page which has buffers
357 * - encountering a page which has a non-hole after a hole
358 * - encountering a page with non-contiguous blocks
359 *
360 * then this code just gives up and calls the buffer_head-based read function.
361 * It does handle a page which has holes at the end - that is a common case:
362 * the end-of-file on blocksize < PAGE_CACHE_SIZE setups.
363 *
364 * BH_Boundary explanation:
365 *
366 * There is a problem. The mpage read code assembles several pages, gets all
367 * their disk mappings, and then submits them all. That's fine, but obtaining
368 * the disk mappings may require I/O. Reads of indirect blocks, for example.
369 *
370 * So an mpage read of the first 16 blocks of an ext2 file will cause I/O to be
371 * submitted in the following order:
372 * 12 0 1 2 3 4 5 6 7 8 9 10 11 13 14 15 16
373 * because the indirect block has to be read to get the mappings of blocks
374 * 13,14,15,16. Obviously, this impacts performance.
375 *
376 * So what we do it to allow the filesystem's get_block() function to set
377 * BH_Boundary when it maps block 11. BH_Boundary says: mapping of the block
378 * after this one will require I/O against a block which is probably close to
379 * this one. So you should push what I/O you have currently accumulated.
380 *
381 * This all causes the disk requests to be issued in the correct order.
382 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383int
384mpage_readpages(struct address_space *mapping, struct list_head *pages,
385 unsigned nr_pages, get_block_t get_block)
386{
387 struct bio *bio = NULL;
388 unsigned page_idx;
389 sector_t last_block_in_bio = 0;
390 struct pagevec lru_pvec;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800391 struct buffer_head map_bh;
392 unsigned long first_logical_block = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800394 clear_buffer_mapped(&map_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 pagevec_init(&lru_pvec, 0);
396 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
397 struct page *page = list_entry(pages->prev, struct page, lru);
398
399 prefetchw(&page->flags);
400 list_del(&page->lru);
401 if (!add_to_page_cache(page, mapping,
402 page->index, GFP_KERNEL)) {
403 bio = do_mpage_readpage(bio, page,
404 nr_pages - page_idx,
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800405 &last_block_in_bio, &map_bh,
406 &first_logical_block,
407 get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if (!pagevec_add(&lru_pvec, page))
409 __pagevec_lru_add(&lru_pvec);
410 } else {
411 page_cache_release(page);
412 }
413 }
414 pagevec_lru_add(&lru_pvec);
415 BUG_ON(!list_empty(pages));
416 if (bio)
417 mpage_bio_submit(READ, bio);
418 return 0;
419}
420EXPORT_SYMBOL(mpage_readpages);
421
422/*
423 * This isn't called much at all
424 */
425int mpage_readpage(struct page *page, get_block_t get_block)
426{
427 struct bio *bio = NULL;
428 sector_t last_block_in_bio = 0;
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800429 struct buffer_head map_bh;
430 unsigned long first_logical_block = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Badari Pulavartyfa30bd02006-03-26 01:38:01 -0800432 clear_buffer_mapped(&map_bh);
433 bio = do_mpage_readpage(bio, page, 1, &last_block_in_bio,
434 &map_bh, &first_logical_block, get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (bio)
436 mpage_bio_submit(READ, bio);
437 return 0;
438}
439EXPORT_SYMBOL(mpage_readpage);
440
441/*
442 * Writing is not so simple.
443 *
444 * If the page has buffers then they will be used for obtaining the disk
445 * mapping. We only support pages which are fully mapped-and-dirty, with a
446 * special case for pages which are unmapped at the end: end-of-file.
447 *
448 * If the page has no buffers (preferred) then the page is mapped here.
449 *
450 * If all blocks are found to be contiguous then the page can go into the
451 * BIO. Otherwise fall back to the mapping's writepage().
452 *
453 * FIXME: This code wants an estimate of how many pages are still to be
454 * written, so it can intelligently allocate a suitably-sized BIO. For now,
455 * just allocate full-size (16-page) BIOs.
456 */
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700457struct mpage_data {
458 struct bio *bio;
459 sector_t last_block_in_bio;
460 get_block_t *get_block;
461 unsigned use_writepage;
462};
463
464static int __mpage_writepage(struct page *page, struct writeback_control *wbc,
465 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700467 struct mpage_data *mpd = data;
468 struct bio *bio = mpd->bio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 struct address_space *mapping = page->mapping;
470 struct inode *inode = page->mapping->host;
471 const unsigned blkbits = inode->i_blkbits;
472 unsigned long end_index;
473 const unsigned blocks_per_page = PAGE_CACHE_SIZE >> blkbits;
474 sector_t last_block;
475 sector_t block_in_file;
476 sector_t blocks[MAX_BUF_PER_PAGE];
477 unsigned page_block;
478 unsigned first_unmapped = blocks_per_page;
479 struct block_device *bdev = NULL;
480 int boundary = 0;
481 sector_t boundary_block = 0;
482 struct block_device *boundary_bdev = NULL;
483 int length;
484 struct buffer_head map_bh;
485 loff_t i_size = i_size_read(inode);
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700486 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 if (page_has_buffers(page)) {
489 struct buffer_head *head = page_buffers(page);
490 struct buffer_head *bh = head;
491
492 /* If they're all mapped and dirty, do it */
493 page_block = 0;
494 do {
495 BUG_ON(buffer_locked(bh));
496 if (!buffer_mapped(bh)) {
497 /*
498 * unmapped dirty buffers are created by
499 * __set_page_dirty_buffers -> mmapped data
500 */
501 if (buffer_dirty(bh))
502 goto confused;
503 if (first_unmapped == blocks_per_page)
504 first_unmapped = page_block;
505 continue;
506 }
507
508 if (first_unmapped != blocks_per_page)
509 goto confused; /* hole -> non-hole */
510
511 if (!buffer_dirty(bh) || !buffer_uptodate(bh))
512 goto confused;
513 if (page_block) {
514 if (bh->b_blocknr != blocks[page_block-1] + 1)
515 goto confused;
516 }
517 blocks[page_block++] = bh->b_blocknr;
518 boundary = buffer_boundary(bh);
519 if (boundary) {
520 boundary_block = bh->b_blocknr;
521 boundary_bdev = bh->b_bdev;
522 }
523 bdev = bh->b_bdev;
524 } while ((bh = bh->b_this_page) != head);
525
526 if (first_unmapped)
527 goto page_is_mapped;
528
529 /*
530 * Page has buffers, but they are all unmapped. The page was
531 * created by pagein or read over a hole which was handled by
532 * block_read_full_page(). If this address_space is also
533 * using mpage_readpages then this can rarely happen.
534 */
535 goto confused;
536 }
537
538 /*
539 * The page has no buffers: map it to disk
540 */
541 BUG_ON(!PageUptodate(page));
Andrew Morton54b21a72006-01-08 01:03:05 -0800542 block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 last_block = (i_size - 1) >> blkbits;
544 map_bh.b_page = page;
545 for (page_block = 0; page_block < blocks_per_page; ) {
546
547 map_bh.b_state = 0;
Badari Pulavartyb0cf2322006-03-26 01:38:00 -0800548 map_bh.b_size = 1 << blkbits;
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700549 if (mpd->get_block(inode, block_in_file, &map_bh, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 goto confused;
551 if (buffer_new(&map_bh))
552 unmap_underlying_metadata(map_bh.b_bdev,
553 map_bh.b_blocknr);
554 if (buffer_boundary(&map_bh)) {
555 boundary_block = map_bh.b_blocknr;
556 boundary_bdev = map_bh.b_bdev;
557 }
558 if (page_block) {
559 if (map_bh.b_blocknr != blocks[page_block-1] + 1)
560 goto confused;
561 }
562 blocks[page_block++] = map_bh.b_blocknr;
563 boundary = buffer_boundary(&map_bh);
564 bdev = map_bh.b_bdev;
565 if (block_in_file == last_block)
566 break;
567 block_in_file++;
568 }
569 BUG_ON(page_block == 0);
570
571 first_unmapped = page_block;
572
573page_is_mapped:
574 end_index = i_size >> PAGE_CACHE_SHIFT;
575 if (page->index >= end_index) {
576 /*
577 * The page straddles i_size. It must be zeroed out on each
578 * and every writepage invokation because it may be mmapped.
579 * "A file is mapped in multiples of the page size. For a file
580 * that is not a multiple of the page size, the remaining memory
581 * is zeroed when mapped, and writes to that region are not
582 * written out to the file."
583 */
584 unsigned offset = i_size & (PAGE_CACHE_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 if (page->index > end_index || !offset)
587 goto confused;
Nate Diller01f27052007-05-09 02:35:07 -0700588 zero_user_page(page, offset, PAGE_CACHE_SIZE - offset,
589 KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
591
592 /*
593 * This page will go to BIO. Do we need to send this BIO off first?
594 */
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700595 if (bio && mpd->last_block_in_bio != blocks[0] - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 bio = mpage_bio_submit(WRITE, bio);
597
598alloc_new:
599 if (bio == NULL) {
600 bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
601 bio_get_nr_vecs(bdev), GFP_NOFS|__GFP_HIGH);
602 if (bio == NULL)
603 goto confused;
604 }
605
606 /*
607 * Must try to add the page before marking the buffer clean or
608 * the confused fail path above (OOM) will be very confused when
609 * it finds all bh marked clean (i.e. it will not write anything)
610 */
611 length = first_unmapped << blkbits;
612 if (bio_add_page(bio, page, length, 0) < length) {
613 bio = mpage_bio_submit(WRITE, bio);
614 goto alloc_new;
615 }
616
617 /*
618 * OK, we have our BIO, so we can now mark the buffers clean. Make
619 * sure to only clean buffers which we know we'll be writing.
620 */
621 if (page_has_buffers(page)) {
622 struct buffer_head *head = page_buffers(page);
623 struct buffer_head *bh = head;
624 unsigned buffer_counter = 0;
625
626 do {
627 if (buffer_counter++ == first_unmapped)
628 break;
629 clear_buffer_dirty(bh);
630 bh = bh->b_this_page;
631 } while (bh != head);
632
633 /*
634 * we cannot drop the bh if the page is not uptodate
635 * or a concurrent readpage would fail to serialize with the bh
636 * and it would read from disk before we reach the platter.
637 */
638 if (buffer_heads_over_limit && PageUptodate(page))
639 try_to_free_buffers(page);
640 }
641
642 BUG_ON(PageWriteback(page));
643 set_page_writeback(page);
644 unlock_page(page);
645 if (boundary || (first_unmapped != blocks_per_page)) {
646 bio = mpage_bio_submit(WRITE, bio);
647 if (boundary_block) {
648 write_boundary_block(boundary_bdev,
649 boundary_block, 1 << blkbits);
650 }
651 } else {
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700652 mpd->last_block_in_bio = blocks[blocks_per_page - 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
654 goto out;
655
656confused:
657 if (bio)
658 bio = mpage_bio_submit(WRITE, bio);
659
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700660 if (mpd->use_writepage) {
661 ret = mapping->a_ops->writepage(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 } else {
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700663 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 goto out;
665 }
666 /*
667 * The caller has a ref on the inode, so *mapping is stable
668 */
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700669 mapping_set_error(mapping, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670out:
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700671 mpd->bio = bio;
672 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
675/**
676 * mpage_writepages - walk the list of dirty pages of the given
677 * address space and writepage() all of them.
678 *
679 * @mapping: address space structure to write
680 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
681 * @get_block: the filesystem's block mapper function.
682 * If this is NULL then use a_ops->writepage. Otherwise, go
683 * direct-to-BIO.
684 *
685 * This is a library function, which implements the writepages()
686 * address_space_operation.
687 *
688 * If a page is already under I/O, generic_writepages() skips it, even
689 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
690 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
691 * and msync() need to guarantee that all the data which was dirty at the time
692 * the call was made get new I/O started against them. If wbc->sync_mode is
693 * WB_SYNC_ALL then we were called for data integrity and we must wait for
694 * existing IO to complete.
695 */
696int
697mpage_writepages(struct address_space *mapping,
698 struct writeback_control *wbc, get_block_t get_block)
699{
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700700 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700702 if (!get_block)
703 ret = generic_writepages(mapping, wbc);
704 else {
705 struct mpage_data mpd = {
706 .bio = NULL,
707 .last_block_in_bio = 0,
708 .get_block = get_block,
709 .use_writepage = 1,
710 };
711
712 ret = write_cache_pages(mapping, wbc, __mpage_writepage, &mpd);
713 if (mpd.bio)
714 mpage_bio_submit(WRITE, mpd.bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return ret;
717}
718EXPORT_SYMBOL(mpage_writepages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720int mpage_writepage(struct page *page, get_block_t get_block,
721 struct writeback_control *wbc)
722{
Miklos Szeredi0ea97182007-05-10 22:22:51 -0700723 struct mpage_data mpd = {
724 .bio = NULL,
725 .last_block_in_bio = 0,
726 .get_block = get_block,
727 .use_writepage = 0,
728 };
729 int ret = __mpage_writepage(page, wbc, &mpd);
730 if (mpd.bio)
731 mpage_bio_submit(WRITE, mpd.bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return ret;
733}
734EXPORT_SYMBOL(mpage_writepage);