OSDN Git Service

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