blob: 7903b740cc111f6250bd75ebc83b4652118a0474 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static struct bio *
167do_mpage_readpage(struct bio *bio, struct page *page, unsigned nr_pages,
168 sector_t *last_block_in_bio, get_block_t get_block)
169{
170 struct inode *inode = page->mapping->host;
171 const unsigned blkbits = inode->i_blkbits;
172 const unsigned blocks_per_page = PAGE_CACHE_SIZE >> blkbits;
173 const unsigned blocksize = 1 << blkbits;
174 sector_t block_in_file;
175 sector_t last_block;
176 sector_t blocks[MAX_BUF_PER_PAGE];
177 unsigned page_block;
178 unsigned first_hole = blocks_per_page;
179 struct block_device *bdev = NULL;
180 struct buffer_head bh;
181 int length;
182 int fully_mapped = 1;
183
184 if (page_has_buffers(page))
185 goto confused;
186
Andrew Morton54b21a72006-01-08 01:03:05 -0800187 block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 last_block = (i_size_read(inode) + blocksize - 1) >> blkbits;
189
190 bh.b_page = page;
191 for (page_block = 0; page_block < blocks_per_page;
192 page_block++, block_in_file++) {
193 bh.b_state = 0;
194 if (block_in_file < last_block) {
Badari Pulavartyb0cf2322006-03-26 01:38:00 -0800195 bh.b_size = blocksize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (get_block(inode, block_in_file, &bh, 0))
197 goto confused;
198 }
199
200 if (!buffer_mapped(&bh)) {
201 fully_mapped = 0;
202 if (first_hole == blocks_per_page)
203 first_hole = page_block;
204 continue;
205 }
206
207 /* some filesystems will copy data into the page during
208 * the get_block call, in which case we don't want to
209 * read it again. map_buffer_to_page copies the data
210 * we just collected from get_block into the page's buffers
211 * so readpage doesn't have to repeat the get_block call
212 */
213 if (buffer_uptodate(&bh)) {
214 map_buffer_to_page(page, &bh, page_block);
215 goto confused;
216 }
217
218 if (first_hole != blocks_per_page)
219 goto confused; /* hole -> non-hole */
220
221 /* Contiguous blocks? */
222 if (page_block && blocks[page_block-1] != bh.b_blocknr-1)
223 goto confused;
224 blocks[page_block] = bh.b_blocknr;
225 bdev = bh.b_bdev;
226 }
227
228 if (first_hole != blocks_per_page) {
229 char *kaddr = kmap_atomic(page, KM_USER0);
230 memset(kaddr + (first_hole << blkbits), 0,
231 PAGE_CACHE_SIZE - (first_hole << blkbits));
232 flush_dcache_page(page);
233 kunmap_atomic(kaddr, KM_USER0);
234 if (first_hole == 0) {
235 SetPageUptodate(page);
236 unlock_page(page);
237 goto out;
238 }
239 } else if (fully_mapped) {
240 SetPageMappedToDisk(page);
241 }
242
243 /*
244 * This page will go to BIO. Do we need to send this BIO off first?
245 */
246 if (bio && (*last_block_in_bio != blocks[0] - 1))
247 bio = mpage_bio_submit(READ, bio);
248
249alloc_new:
250 if (bio == NULL) {
251 bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
252 min_t(int, nr_pages, bio_get_nr_vecs(bdev)),
253 GFP_KERNEL);
254 if (bio == NULL)
255 goto confused;
256 }
257
258 length = first_hole << blkbits;
259 if (bio_add_page(bio, page, length, 0) < length) {
260 bio = mpage_bio_submit(READ, bio);
261 goto alloc_new;
262 }
263
264 if (buffer_boundary(&bh) || (first_hole != blocks_per_page))
265 bio = mpage_bio_submit(READ, bio);
266 else
267 *last_block_in_bio = blocks[blocks_per_page - 1];
268out:
269 return bio;
270
271confused:
272 if (bio)
273 bio = mpage_bio_submit(READ, bio);
274 if (!PageUptodate(page))
275 block_read_full_page(page, get_block);
276 else
277 unlock_page(page);
278 goto out;
279}
280
Martin Waitz67be2dd2005-05-01 08:59:26 -0700281/**
282 * mpage_readpages - populate an address space with some pages, and
283 * start reads against them.
284 *
285 * @mapping: the address_space
286 * @pages: The address of a list_head which contains the target pages. These
287 * pages have their ->index populated and are otherwise uninitialised.
288 *
289 * The page at @pages->prev has the lowest file offset, and reads should be
290 * issued in @pages->prev to @pages->next order.
291 *
292 * @nr_pages: The number of pages at *@pages
293 * @get_block: The filesystem's block mapper function.
294 *
295 * This function walks the pages and the blocks within each page, building and
296 * emitting large BIOs.
297 *
298 * If anything unusual happens, such as:
299 *
300 * - encountering a page which has buffers
301 * - encountering a page which has a non-hole after a hole
302 * - encountering a page with non-contiguous blocks
303 *
304 * then this code just gives up and calls the buffer_head-based read function.
305 * It does handle a page which has holes at the end - that is a common case:
306 * the end-of-file on blocksize < PAGE_CACHE_SIZE setups.
307 *
308 * BH_Boundary explanation:
309 *
310 * There is a problem. The mpage read code assembles several pages, gets all
311 * their disk mappings, and then submits them all. That's fine, but obtaining
312 * the disk mappings may require I/O. Reads of indirect blocks, for example.
313 *
314 * So an mpage read of the first 16 blocks of an ext2 file will cause I/O to be
315 * submitted in the following order:
316 * 12 0 1 2 3 4 5 6 7 8 9 10 11 13 14 15 16
317 * because the indirect block has to be read to get the mappings of blocks
318 * 13,14,15,16. Obviously, this impacts performance.
319 *
320 * So what we do it to allow the filesystem's get_block() function to set
321 * BH_Boundary when it maps block 11. BH_Boundary says: mapping of the block
322 * after this one will require I/O against a block which is probably close to
323 * this one. So you should push what I/O you have currently accumulated.
324 *
325 * This all causes the disk requests to be issued in the correct order.
326 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327int
328mpage_readpages(struct address_space *mapping, struct list_head *pages,
329 unsigned nr_pages, get_block_t get_block)
330{
331 struct bio *bio = NULL;
332 unsigned page_idx;
333 sector_t last_block_in_bio = 0;
334 struct pagevec lru_pvec;
335
336 pagevec_init(&lru_pvec, 0);
337 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
338 struct page *page = list_entry(pages->prev, struct page, lru);
339
340 prefetchw(&page->flags);
341 list_del(&page->lru);
342 if (!add_to_page_cache(page, mapping,
343 page->index, GFP_KERNEL)) {
344 bio = do_mpage_readpage(bio, page,
345 nr_pages - page_idx,
346 &last_block_in_bio, get_block);
347 if (!pagevec_add(&lru_pvec, page))
348 __pagevec_lru_add(&lru_pvec);
349 } else {
350 page_cache_release(page);
351 }
352 }
353 pagevec_lru_add(&lru_pvec);
354 BUG_ON(!list_empty(pages));
355 if (bio)
356 mpage_bio_submit(READ, bio);
357 return 0;
358}
359EXPORT_SYMBOL(mpage_readpages);
360
361/*
362 * This isn't called much at all
363 */
364int mpage_readpage(struct page *page, get_block_t get_block)
365{
366 struct bio *bio = NULL;
367 sector_t last_block_in_bio = 0;
368
369 bio = do_mpage_readpage(bio, page, 1,
370 &last_block_in_bio, get_block);
371 if (bio)
372 mpage_bio_submit(READ, bio);
373 return 0;
374}
375EXPORT_SYMBOL(mpage_readpage);
376
377/*
378 * Writing is not so simple.
379 *
380 * If the page has buffers then they will be used for obtaining the disk
381 * mapping. We only support pages which are fully mapped-and-dirty, with a
382 * special case for pages which are unmapped at the end: end-of-file.
383 *
384 * If the page has no buffers (preferred) then the page is mapped here.
385 *
386 * If all blocks are found to be contiguous then the page can go into the
387 * BIO. Otherwise fall back to the mapping's writepage().
388 *
389 * FIXME: This code wants an estimate of how many pages are still to be
390 * written, so it can intelligently allocate a suitably-sized BIO. For now,
391 * just allocate full-size (16-page) BIOs.
392 */
393static struct bio *
394__mpage_writepage(struct bio *bio, struct page *page, get_block_t get_block,
395 sector_t *last_block_in_bio, int *ret, struct writeback_control *wbc,
396 writepage_t writepage_fn)
397{
398 struct address_space *mapping = page->mapping;
399 struct inode *inode = page->mapping->host;
400 const unsigned blkbits = inode->i_blkbits;
401 unsigned long end_index;
402 const unsigned blocks_per_page = PAGE_CACHE_SIZE >> blkbits;
403 sector_t last_block;
404 sector_t block_in_file;
405 sector_t blocks[MAX_BUF_PER_PAGE];
406 unsigned page_block;
407 unsigned first_unmapped = blocks_per_page;
408 struct block_device *bdev = NULL;
409 int boundary = 0;
410 sector_t boundary_block = 0;
411 struct block_device *boundary_bdev = NULL;
412 int length;
413 struct buffer_head map_bh;
414 loff_t i_size = i_size_read(inode);
415
416 if (page_has_buffers(page)) {
417 struct buffer_head *head = page_buffers(page);
418 struct buffer_head *bh = head;
419
420 /* If they're all mapped and dirty, do it */
421 page_block = 0;
422 do {
423 BUG_ON(buffer_locked(bh));
424 if (!buffer_mapped(bh)) {
425 /*
426 * unmapped dirty buffers are created by
427 * __set_page_dirty_buffers -> mmapped data
428 */
429 if (buffer_dirty(bh))
430 goto confused;
431 if (first_unmapped == blocks_per_page)
432 first_unmapped = page_block;
433 continue;
434 }
435
436 if (first_unmapped != blocks_per_page)
437 goto confused; /* hole -> non-hole */
438
439 if (!buffer_dirty(bh) || !buffer_uptodate(bh))
440 goto confused;
441 if (page_block) {
442 if (bh->b_blocknr != blocks[page_block-1] + 1)
443 goto confused;
444 }
445 blocks[page_block++] = bh->b_blocknr;
446 boundary = buffer_boundary(bh);
447 if (boundary) {
448 boundary_block = bh->b_blocknr;
449 boundary_bdev = bh->b_bdev;
450 }
451 bdev = bh->b_bdev;
452 } while ((bh = bh->b_this_page) != head);
453
454 if (first_unmapped)
455 goto page_is_mapped;
456
457 /*
458 * Page has buffers, but they are all unmapped. The page was
459 * created by pagein or read over a hole which was handled by
460 * block_read_full_page(). If this address_space is also
461 * using mpage_readpages then this can rarely happen.
462 */
463 goto confused;
464 }
465
466 /*
467 * The page has no buffers: map it to disk
468 */
469 BUG_ON(!PageUptodate(page));
Andrew Morton54b21a72006-01-08 01:03:05 -0800470 block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 last_block = (i_size - 1) >> blkbits;
472 map_bh.b_page = page;
473 for (page_block = 0; page_block < blocks_per_page; ) {
474
475 map_bh.b_state = 0;
Badari Pulavartyb0cf2322006-03-26 01:38:00 -0800476 map_bh.b_size = 1 << blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (get_block(inode, block_in_file, &map_bh, 1))
478 goto confused;
479 if (buffer_new(&map_bh))
480 unmap_underlying_metadata(map_bh.b_bdev,
481 map_bh.b_blocknr);
482 if (buffer_boundary(&map_bh)) {
483 boundary_block = map_bh.b_blocknr;
484 boundary_bdev = map_bh.b_bdev;
485 }
486 if (page_block) {
487 if (map_bh.b_blocknr != blocks[page_block-1] + 1)
488 goto confused;
489 }
490 blocks[page_block++] = map_bh.b_blocknr;
491 boundary = buffer_boundary(&map_bh);
492 bdev = map_bh.b_bdev;
493 if (block_in_file == last_block)
494 break;
495 block_in_file++;
496 }
497 BUG_ON(page_block == 0);
498
499 first_unmapped = page_block;
500
501page_is_mapped:
502 end_index = i_size >> PAGE_CACHE_SHIFT;
503 if (page->index >= end_index) {
504 /*
505 * The page straddles i_size. It must be zeroed out on each
506 * and every writepage invokation because it may be mmapped.
507 * "A file is mapped in multiples of the page size. For a file
508 * that is not a multiple of the page size, the remaining memory
509 * is zeroed when mapped, and writes to that region are not
510 * written out to the file."
511 */
512 unsigned offset = i_size & (PAGE_CACHE_SIZE - 1);
513 char *kaddr;
514
515 if (page->index > end_index || !offset)
516 goto confused;
517 kaddr = kmap_atomic(page, KM_USER0);
518 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
519 flush_dcache_page(page);
520 kunmap_atomic(kaddr, KM_USER0);
521 }
522
523 /*
524 * This page will go to BIO. Do we need to send this BIO off first?
525 */
526 if (bio && *last_block_in_bio != blocks[0] - 1)
527 bio = mpage_bio_submit(WRITE, bio);
528
529alloc_new:
530 if (bio == NULL) {
531 bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
532 bio_get_nr_vecs(bdev), GFP_NOFS|__GFP_HIGH);
533 if (bio == NULL)
534 goto confused;
535 }
536
537 /*
538 * Must try to add the page before marking the buffer clean or
539 * the confused fail path above (OOM) will be very confused when
540 * it finds all bh marked clean (i.e. it will not write anything)
541 */
542 length = first_unmapped << blkbits;
543 if (bio_add_page(bio, page, length, 0) < length) {
544 bio = mpage_bio_submit(WRITE, bio);
545 goto alloc_new;
546 }
547
548 /*
549 * OK, we have our BIO, so we can now mark the buffers clean. Make
550 * sure to only clean buffers which we know we'll be writing.
551 */
552 if (page_has_buffers(page)) {
553 struct buffer_head *head = page_buffers(page);
554 struct buffer_head *bh = head;
555 unsigned buffer_counter = 0;
556
557 do {
558 if (buffer_counter++ == first_unmapped)
559 break;
560 clear_buffer_dirty(bh);
561 bh = bh->b_this_page;
562 } while (bh != head);
563
564 /*
565 * we cannot drop the bh if the page is not uptodate
566 * or a concurrent readpage would fail to serialize with the bh
567 * and it would read from disk before we reach the platter.
568 */
569 if (buffer_heads_over_limit && PageUptodate(page))
570 try_to_free_buffers(page);
571 }
572
573 BUG_ON(PageWriteback(page));
574 set_page_writeback(page);
575 unlock_page(page);
576 if (boundary || (first_unmapped != blocks_per_page)) {
577 bio = mpage_bio_submit(WRITE, bio);
578 if (boundary_block) {
579 write_boundary_block(boundary_bdev,
580 boundary_block, 1 << blkbits);
581 }
582 } else {
583 *last_block_in_bio = blocks[blocks_per_page - 1];
584 }
585 goto out;
586
587confused:
588 if (bio)
589 bio = mpage_bio_submit(WRITE, bio);
590
591 if (writepage_fn) {
592 *ret = (*writepage_fn)(page, wbc);
593 } else {
594 *ret = -EAGAIN;
595 goto out;
596 }
597 /*
598 * The caller has a ref on the inode, so *mapping is stable
599 */
600 if (*ret) {
601 if (*ret == -ENOSPC)
602 set_bit(AS_ENOSPC, &mapping->flags);
603 else
604 set_bit(AS_EIO, &mapping->flags);
605 }
606out:
607 return bio;
608}
609
610/**
611 * mpage_writepages - walk the list of dirty pages of the given
612 * address space and writepage() all of them.
613 *
614 * @mapping: address space structure to write
615 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
616 * @get_block: the filesystem's block mapper function.
617 * If this is NULL then use a_ops->writepage. Otherwise, go
618 * direct-to-BIO.
619 *
620 * This is a library function, which implements the writepages()
621 * address_space_operation.
622 *
623 * If a page is already under I/O, generic_writepages() skips it, even
624 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
625 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
626 * and msync() need to guarantee that all the data which was dirty at the time
627 * the call was made get new I/O started against them. If wbc->sync_mode is
628 * WB_SYNC_ALL then we were called for data integrity and we must wait for
629 * existing IO to complete.
630 */
631int
632mpage_writepages(struct address_space *mapping,
633 struct writeback_control *wbc, get_block_t get_block)
634{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 struct backing_dev_info *bdi = mapping->backing_dev_info;
636 struct bio *bio = NULL;
637 sector_t last_block_in_bio = 0;
638 int ret = 0;
639 int done = 0;
640 int (*writepage)(struct page *page, struct writeback_control *wbc);
641 struct pagevec pvec;
642 int nr_pages;
643 pgoff_t index;
644 pgoff_t end = -1; /* Inclusive */
645 int scanned = 0;
646 int is_range = 0;
647
648 if (wbc->nonblocking && bdi_write_congested(bdi)) {
649 wbc->encountered_congestion = 1;
650 return 0;
651 }
652
653 writepage = NULL;
654 if (get_block == NULL)
655 writepage = mapping->a_ops->writepage;
656
657 pagevec_init(&pvec, 0);
658 if (wbc->sync_mode == WB_SYNC_NONE) {
659 index = mapping->writeback_index; /* Start from prev offset */
660 } else {
661 index = 0; /* whole-file sweep */
662 scanned = 1;
663 }
664 if (wbc->start || wbc->end) {
665 index = wbc->start >> PAGE_CACHE_SHIFT;
666 end = wbc->end >> PAGE_CACHE_SHIFT;
667 is_range = 1;
668 scanned = 1;
669 }
670retry:
671 while (!done && (index <= end) &&
672 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
673 PAGECACHE_TAG_DIRTY,
674 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
675 unsigned i;
676
677 scanned = 1;
678 for (i = 0; i < nr_pages; i++) {
679 struct page *page = pvec.pages[i];
680
681 /*
682 * At this point we hold neither mapping->tree_lock nor
683 * lock on the page itself: the page may be truncated or
684 * invalidated (changing page->mapping to NULL), or even
685 * swizzled back from swapper_space to tmpfs file
686 * mapping
687 */
688
689 lock_page(page);
690
691 if (unlikely(page->mapping != mapping)) {
692 unlock_page(page);
693 continue;
694 }
695
696 if (unlikely(is_range) && page->index > end) {
697 done = 1;
698 unlock_page(page);
699 continue;
700 }
701
702 if (wbc->sync_mode != WB_SYNC_NONE)
703 wait_on_page_writeback(page);
704
705 if (PageWriteback(page) ||
706 !clear_page_dirty_for_io(page)) {
707 unlock_page(page);
708 continue;
709 }
710
711 if (writepage) {
712 ret = (*writepage)(page, wbc);
713 if (ret) {
714 if (ret == -ENOSPC)
715 set_bit(AS_ENOSPC,
716 &mapping->flags);
717 else
718 set_bit(AS_EIO,
719 &mapping->flags);
720 }
721 } else {
722 bio = __mpage_writepage(bio, page, get_block,
723 &last_block_in_bio, &ret, wbc,
Andrew Mortond17d7fa2005-05-05 16:16:02 -0700724 page->mapping->a_ops->writepage);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
Zach Brown994fc28c2005-12-15 14:28:17 -0800726 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE))
Nikita Danilov552fca42005-05-01 08:58:39 -0700727 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (ret || (--(wbc->nr_to_write) <= 0))
729 done = 1;
730 if (wbc->nonblocking && bdi_write_congested(bdi)) {
731 wbc->encountered_congestion = 1;
732 done = 1;
733 }
734 }
735 pagevec_release(&pvec);
736 cond_resched();
737 }
738 if (!scanned && !done) {
739 /*
740 * We hit the last page and there is more work to be done: wrap
741 * back to the start of the file
742 */
743 scanned = 1;
744 index = 0;
745 goto retry;
746 }
747 if (!is_range)
748 mapping->writeback_index = index;
749 if (bio)
750 mpage_bio_submit(WRITE, bio);
751 return ret;
752}
753EXPORT_SYMBOL(mpage_writepages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755int mpage_writepage(struct page *page, get_block_t get_block,
756 struct writeback_control *wbc)
757{
758 int ret = 0;
759 struct bio *bio;
760 sector_t last_block_in_bio = 0;
761
762 bio = __mpage_writepage(NULL, page, get_block,
763 &last_block_in_bio, &ret, wbc, NULL);
764 if (bio)
765 mpage_bio_submit(WRITE, bio);
766
767 return ret;
768}
769EXPORT_SYMBOL(mpage_writepage);