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