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