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