fs: convert writepage_t callback to pass a folio
[linux-block.git] / fs / mpage.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
2/*
3 * fs/mpage.c
4 *
5 * Copyright (C) 2002, Linus Torvalds.
6 *
7 * Contains functions related to preparing and submitting BIOs which contain
8 * multiple pagecache pages.
9 *
e1f8e874 10 * 15May2002 Andrew Morton
1da177e4
LT
11 * Initial version
12 * 27Jun2002 axboe@suse.de
13 * use bio_add_page() to build bio's just the right size
14 */
15
16#include <linux/kernel.h>
630d9c47 17#include <linux/export.h>
1da177e4
LT
18#include <linux/mm.h>
19#include <linux/kdev_t.h>
5a0e3ad6 20#include <linux/gfp.h>
1da177e4
LT
21#include <linux/bio.h>
22#include <linux/fs.h>
23#include <linux/buffer_head.h>
24#include <linux/blkdev.h>
25#include <linux/highmem.h>
26#include <linux/prefetch.h>
27#include <linux/mpage.h>
02c43638 28#include <linux/mm_inline.h>
1da177e4
LT
29#include <linux/writeback.h>
30#include <linux/backing-dev.h>
31#include <linux/pagevec.h>
4db96b71 32#include "internal.h"
1da177e4
LT
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
2c69e205 39 * back to block_read_full_folio().
1da177e4
LT
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 */
4246a0b6 46static void mpage_end_io(struct bio *bio)
1da177e4 47{
2c30c71b 48 struct bio_vec *bv;
6dc4f100 49 struct bvec_iter_all iter_all;
1da177e4 50
2b070cfe 51 bio_for_each_segment_all(bv, bio, iter_all) {
2c30c71b 52 struct page *page = bv->bv_page;
3f289dcb
TH
53 page_endio(page, bio_op(bio),
54 blk_status_to_errno(bio->bi_status));
2c30c71b
KO
55 }
56
1da177e4 57 bio_put(bio);
1da177e4
LT
58}
59
77c436de 60static struct bio *mpage_bio_submit(struct bio *bio)
1da177e4 61{
c32b0d4b 62 bio->bi_end_io = mpage_end_io;
83c9c547 63 guard_bio_eod(bio);
4e49ea4a 64 submit_bio(bio);
1da177e4
LT
65 return NULL;
66}
67
1da177e4 68/*
d4388340 69 * support function for mpage_readahead. The fs supplied get_block might
1da177e4 70 * return an up to date buffer. This is used to map that buffer into
2c69e205 71 * the page, which allows read_folio to avoid triggering a duplicate call
1da177e4
LT
72 * to get_block.
73 *
74 * The idea is to avoid adding buffers to pages that don't already have
75 * them. So when the buffer is up to date and the page size == block size,
76 * this marks the page up to date instead of adding new buffers.
77 */
211d0444
MWO
78static void map_buffer_to_folio(struct folio *folio, struct buffer_head *bh,
79 int page_block)
1da177e4 80{
211d0444 81 struct inode *inode = folio->mapping->host;
1da177e4
LT
82 struct buffer_head *page_bh, *head;
83 int block = 0;
84
211d0444
MWO
85 head = folio_buffers(folio);
86 if (!head) {
1da177e4
LT
87 /*
88 * don't make any buffers if there is only one buffer on
211d0444 89 * the folio and the folio just needs to be set up to date
1da177e4 90 */
09cbfeaf 91 if (inode->i_blkbits == PAGE_SHIFT &&
1da177e4 92 buffer_uptodate(bh)) {
211d0444 93 folio_mark_uptodate(folio);
1da177e4
LT
94 return;
95 }
211d0444
MWO
96 create_empty_buffers(&folio->page, i_blocksize(inode), 0);
97 head = folio_buffers(folio);
1da177e4 98 }
211d0444 99
1da177e4
LT
100 page_bh = head;
101 do {
102 if (block == page_block) {
103 page_bh->b_state = bh->b_state;
104 page_bh->b_bdev = bh->b_bdev;
105 page_bh->b_blocknr = bh->b_blocknr;
106 break;
107 }
108 page_bh = page_bh->b_this_page;
109 block++;
110 } while (page_bh != head);
111}
112
357c1206
JA
113struct mpage_readpage_args {
114 struct bio *bio;
211d0444 115 struct folio *folio;
357c1206 116 unsigned int nr_pages;
74c8164e 117 bool is_readahead;
357c1206
JA
118 sector_t last_block_in_bio;
119 struct buffer_head map_bh;
120 unsigned long first_logical_block;
121 get_block_t *get_block;
357c1206
JA
122};
123
fa30bd05
BP
124/*
125 * This is the worker routine which does all the work of mapping the disk
126 * blocks and constructs largest possible bios, submits them for IO if the
127 * blocks are not contiguous on the disk.
128 *
129 * We pass a buffer_head back and forth and use its buffer_mapped() flag to
130 * represent the validity of its disk mapping and to decide when to do the next
131 * get_block() call.
132 */
357c1206 133static struct bio *do_mpage_readpage(struct mpage_readpage_args *args)
1da177e4 134{
211d0444
MWO
135 struct folio *folio = args->folio;
136 struct inode *inode = folio->mapping->host;
1da177e4 137 const unsigned blkbits = inode->i_blkbits;
09cbfeaf 138 const unsigned blocks_per_page = PAGE_SIZE >> blkbits;
1da177e4 139 const unsigned blocksize = 1 << blkbits;
357c1206 140 struct buffer_head *map_bh = &args->map_bh;
1da177e4
LT
141 sector_t block_in_file;
142 sector_t last_block;
fa30bd05 143 sector_t last_block_in_file;
1da177e4
LT
144 sector_t blocks[MAX_BUF_PER_PAGE];
145 unsigned page_block;
146 unsigned first_hole = blocks_per_page;
147 struct block_device *bdev = NULL;
1da177e4
LT
148 int length;
149 int fully_mapped = 1;
f84c94af 150 blk_opf_t opf = REQ_OP_READ;
fa30bd05
BP
151 unsigned nblocks;
152 unsigned relative_block;
211d0444
MWO
153 gfp_t gfp = mapping_gfp_constraint(folio->mapping, GFP_KERNEL);
154
155 /* MAX_BUF_PER_PAGE, for example */
156 VM_BUG_ON_FOLIO(folio_test_large(folio), folio);
74c8164e
JA
157
158 if (args->is_readahead) {
f84c94af 159 opf |= REQ_RAHEAD;
61285ff7 160 gfp |= __GFP_NORETRY | __GFP_NOWARN;
74c8164e 161 }
1da177e4 162
211d0444 163 if (folio_buffers(folio))
1da177e4
LT
164 goto confused;
165
211d0444 166 block_in_file = (sector_t)folio->index << (PAGE_SHIFT - blkbits);
357c1206 167 last_block = block_in_file + args->nr_pages * blocks_per_page;
fa30bd05
BP
168 last_block_in_file = (i_size_read(inode) + blocksize - 1) >> blkbits;
169 if (last_block > last_block_in_file)
170 last_block = last_block_in_file;
171 page_block = 0;
172
173 /*
174 * Map blocks using the result from the previous get_blocks call first.
175 */
176 nblocks = map_bh->b_size >> blkbits;
357c1206
JA
177 if (buffer_mapped(map_bh) &&
178 block_in_file > args->first_logical_block &&
179 block_in_file < (args->first_logical_block + nblocks)) {
180 unsigned map_offset = block_in_file - args->first_logical_block;
fa30bd05
BP
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 /*
211d0444 199 * Then do more get_blocks calls until we are done with this folio.
fa30bd05 200 */
a5fd8390 201 map_bh->b_folio = folio;
fa30bd05
BP
202 while (page_block < blocks_per_page) {
203 map_bh->b_state = 0;
204 map_bh->b_size = 0;
1da177e4 205
1da177e4 206 if (block_in_file < last_block) {
fa30bd05 207 map_bh->b_size = (last_block-block_in_file) << blkbits;
357c1206 208 if (args->get_block(inode, block_in_file, map_bh, 0))
1da177e4 209 goto confused;
357c1206 210 args->first_logical_block = block_in_file;
1da177e4
LT
211 }
212
fa30bd05 213 if (!buffer_mapped(map_bh)) {
1da177e4
LT
214 fully_mapped = 0;
215 if (first_hole == blocks_per_page)
216 first_hole = page_block;
fa30bd05
BP
217 page_block++;
218 block_in_file++;
1da177e4
LT
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
211d0444
MWO
224 * read it again. map_buffer_to_folio copies the data
225 * we just collected from get_block into the folio's buffers
226 * so read_folio doesn't have to repeat the get_block call
1da177e4 227 */
fa30bd05 228 if (buffer_uptodate(map_bh)) {
211d0444 229 map_buffer_to_folio(folio, map_bh, page_block);
1da177e4
LT
230 goto confused;
231 }
232
233 if (first_hole != blocks_per_page)
234 goto confused; /* hole -> non-hole */
235
236 /* Contiguous blocks? */
fa30bd05 237 if (page_block && blocks[page_block-1] != map_bh->b_blocknr-1)
1da177e4 238 goto confused;
fa30bd05
BP
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;
1da177e4
LT
251 }
252
253 if (first_hole != blocks_per_page) {
211d0444 254 folio_zero_segment(folio, first_hole << blkbits, PAGE_SIZE);
1da177e4 255 if (first_hole == 0) {
211d0444
MWO
256 folio_mark_uptodate(folio);
257 folio_unlock(folio);
1da177e4
LT
258 goto out;
259 }
260 } else if (fully_mapped) {
211d0444 261 folio_set_mappedtodisk(folio);
1da177e4
LT
262 }
263
264 /*
211d0444 265 * This folio will go to BIO. Do we need to send this BIO off first?
1da177e4 266 */
357c1206 267 if (args->bio && (args->last_block_in_bio != blocks[0] - 1))
77c436de 268 args->bio = mpage_bio_submit(args->bio);
1da177e4
LT
269
270alloc_new:
357c1206 271 if (args->bio == NULL) {
f84c94af 272 args->bio = bio_alloc(bdev, bio_max_segs(args->nr_pages), opf,
07888c66 273 gfp);
357c1206 274 if (args->bio == NULL)
1da177e4 275 goto confused;
d5f68a42 276 args->bio->bi_iter.bi_sector = blocks[0] << (blkbits - 9);
1da177e4
LT
277 }
278
279 length = first_hole << blkbits;
211d0444 280 if (!bio_add_folio(args->bio, folio, length, 0)) {
77c436de 281 args->bio = mpage_bio_submit(args->bio);
1da177e4
LT
282 goto alloc_new;
283 }
284
357c1206 285 relative_block = block_in_file - args->first_logical_block;
38c8e618
MS
286 nblocks = map_bh->b_size >> blkbits;
287 if ((buffer_boundary(map_bh) && relative_block == nblocks) ||
288 (first_hole != blocks_per_page))
77c436de 289 args->bio = mpage_bio_submit(args->bio);
1da177e4 290 else
357c1206 291 args->last_block_in_bio = blocks[blocks_per_page - 1];
1da177e4 292out:
357c1206 293 return args->bio;
1da177e4
LT
294
295confused:
357c1206 296 if (args->bio)
77c436de 297 args->bio = mpage_bio_submit(args->bio);
211d0444
MWO
298 if (!folio_test_uptodate(folio))
299 block_read_full_folio(folio, args->get_block);
1da177e4 300 else
211d0444 301 folio_unlock(folio);
1da177e4
LT
302 goto out;
303}
304
67be2dd1 305/**
d4388340
MWO
306 * mpage_readahead - start reads against pages
307 * @rac: Describes which pages to read.
67be2dd1
MW
308 * @get_block: The filesystem's block mapper function.
309 *
310 * This function walks the pages and the blocks within each page, building and
311 * emitting large BIOs.
312 *
313 * If anything unusual happens, such as:
314 *
315 * - encountering a page which has buffers
316 * - encountering a page which has a non-hole after a hole
317 * - encountering a page with non-contiguous blocks
318 *
319 * then this code just gives up and calls the buffer_head-based read function.
320 * It does handle a page which has holes at the end - that is a common case:
ea1754a0 321 * the end-of-file on blocksize < PAGE_SIZE setups.
67be2dd1
MW
322 *
323 * BH_Boundary explanation:
324 *
325 * There is a problem. The mpage read code assembles several pages, gets all
326 * their disk mappings, and then submits them all. That's fine, but obtaining
327 * the disk mappings may require I/O. Reads of indirect blocks, for example.
328 *
329 * So an mpage read of the first 16 blocks of an ext2 file will cause I/O to be
330 * submitted in the following order:
0117d427 331 *
67be2dd1 332 * 12 0 1 2 3 4 5 6 7 8 9 10 11 13 14 15 16
78a4a50a 333 *
67be2dd1
MW
334 * because the indirect block has to be read to get the mappings of blocks
335 * 13,14,15,16. Obviously, this impacts performance.
336 *
337 * So what we do it to allow the filesystem's get_block() function to set
338 * BH_Boundary when it maps block 11. BH_Boundary says: mapping of the block
339 * after this one will require I/O against a block which is probably close to
340 * this one. So you should push what I/O you have currently accumulated.
341 *
342 * This all causes the disk requests to be issued in the correct order.
343 */
d4388340 344void mpage_readahead(struct readahead_control *rac, get_block_t get_block)
1da177e4 345{
211d0444 346 struct folio *folio;
357c1206
JA
347 struct mpage_readpage_args args = {
348 .get_block = get_block,
74c8164e 349 .is_readahead = true,
357c1206 350 };
1da177e4 351
211d0444
MWO
352 while ((folio = readahead_folio(rac))) {
353 prefetchw(&folio->flags);
354 args.folio = folio;
d4388340
MWO
355 args.nr_pages = readahead_count(rac);
356 args.bio = do_mpage_readpage(&args);
1da177e4 357 }
357c1206 358 if (args.bio)
77c436de 359 mpage_bio_submit(args.bio);
1da177e4 360}
d4388340 361EXPORT_SYMBOL(mpage_readahead);
1da177e4
LT
362
363/*
364 * This isn't called much at all
365 */
f132ab7d 366int mpage_read_folio(struct folio *folio, get_block_t get_block)
1da177e4 367{
357c1206 368 struct mpage_readpage_args args = {
211d0444 369 .folio = folio,
357c1206
JA
370 .nr_pages = 1,
371 .get_block = get_block,
357c1206
JA
372 };
373
374 args.bio = do_mpage_readpage(&args);
375 if (args.bio)
77c436de 376 mpage_bio_submit(args.bio);
1da177e4
LT
377 return 0;
378}
f132ab7d 379EXPORT_SYMBOL(mpage_read_folio);
1da177e4
LT
380
381/*
382 * Writing is not so simple.
383 *
384 * If the page has buffers then they will be used for obtaining the disk
385 * mapping. We only support pages which are fully mapped-and-dirty, with a
386 * special case for pages which are unmapped at the end: end-of-file.
387 *
388 * If the page has no buffers (preferred) then the page is mapped here.
389 *
390 * If all blocks are found to be contiguous then the page can go into the
391 * BIO. Otherwise fall back to the mapping's writepage().
392 *
393 * FIXME: This code wants an estimate of how many pages are still to be
394 * written, so it can intelligently allocate a suitably-sized BIO. For now,
395 * just allocate full-size (16-page) BIOs.
396 */
0ea97180 397
ced117c7
DV
398struct mpage_data {
399 struct bio *bio;
400 sector_t last_block_in_bio;
401 get_block_t *get_block;
ced117c7
DV
402};
403
90768eee
MW
404/*
405 * We have our BIO, so we can now mark the buffers clean. Make
406 * sure to only clean buffers which we know we'll be writing.
407 */
408static void clean_buffers(struct page *page, unsigned first_unmapped)
409{
410 unsigned buffer_counter = 0;
411 struct buffer_head *bh, *head;
412 if (!page_has_buffers(page))
413 return;
414 head = page_buffers(page);
415 bh = head;
416
417 do {
418 if (buffer_counter++ == first_unmapped)
419 break;
420 clear_buffer_dirty(bh);
421 bh = bh->b_this_page;
422 } while (bh != head);
423
424 /*
425 * we cannot drop the bh if the page is not uptodate or a concurrent
2c69e205 426 * read_folio would fail to serialize with the bh and it would read from
90768eee
MW
427 * disk before we reach the platter.
428 */
429 if (buffer_heads_over_limit && PageUptodate(page))
68189fef 430 try_to_free_buffers(page_folio(page));
90768eee
MW
431}
432
f892760a
MW
433/*
434 * For situations where we want to clean all buffers attached to a page.
435 * We don't need to calculate how many buffers are attached to the page,
436 * we just need to specify a number larger than the maximum number of buffers.
437 */
438void clean_page_buffers(struct page *page)
439{
440 clean_buffers(page, ~0U);
441}
442
d585bdbe 443static int __mpage_writepage(struct folio *folio, struct writeback_control *wbc,
29a814d2 444 void *data)
1da177e4 445{
d585bdbe 446 struct page *page = &folio->page;
0ea97180
MS
447 struct mpage_data *mpd = data;
448 struct bio *bio = mpd->bio;
1da177e4
LT
449 struct address_space *mapping = page->mapping;
450 struct inode *inode = page->mapping->host;
451 const unsigned blkbits = inode->i_blkbits;
452 unsigned long end_index;
09cbfeaf 453 const unsigned blocks_per_page = PAGE_SIZE >> blkbits;
1da177e4
LT
454 sector_t last_block;
455 sector_t block_in_file;
456 sector_t blocks[MAX_BUF_PER_PAGE];
457 unsigned page_block;
458 unsigned first_unmapped = blocks_per_page;
459 struct block_device *bdev = NULL;
460 int boundary = 0;
461 sector_t boundary_block = 0;
462 struct block_device *boundary_bdev = NULL;
463 int length;
464 struct buffer_head map_bh;
465 loff_t i_size = i_size_read(inode);
0ea97180 466 int ret = 0;
1da177e4
LT
467
468 if (page_has_buffers(page)) {
469 struct buffer_head *head = page_buffers(page);
470 struct buffer_head *bh = head;
471
472 /* If they're all mapped and dirty, do it */
473 page_block = 0;
474 do {
475 BUG_ON(buffer_locked(bh));
476 if (!buffer_mapped(bh)) {
477 /*
478 * unmapped dirty buffers are created by
e621900a 479 * block_dirty_folio -> mmapped data
1da177e4
LT
480 */
481 if (buffer_dirty(bh))
482 goto confused;
483 if (first_unmapped == blocks_per_page)
484 first_unmapped = page_block;
485 continue;
486 }
487
488 if (first_unmapped != blocks_per_page)
489 goto confused; /* hole -> non-hole */
490
491 if (!buffer_dirty(bh) || !buffer_uptodate(bh))
492 goto confused;
493 if (page_block) {
494 if (bh->b_blocknr != blocks[page_block-1] + 1)
495 goto confused;
496 }
497 blocks[page_block++] = bh->b_blocknr;
498 boundary = buffer_boundary(bh);
499 if (boundary) {
500 boundary_block = bh->b_blocknr;
501 boundary_bdev = bh->b_bdev;
502 }
503 bdev = bh->b_bdev;
504 } while ((bh = bh->b_this_page) != head);
505
506 if (first_unmapped)
507 goto page_is_mapped;
508
509 /*
510 * Page has buffers, but they are all unmapped. The page was
511 * created by pagein or read over a hole which was handled by
2c69e205 512 * block_read_full_folio(). If this address_space is also
d4388340 513 * using mpage_readahead then this can rarely happen.
1da177e4
LT
514 */
515 goto confused;
516 }
517
518 /*
519 * The page has no buffers: map it to disk
520 */
521 BUG_ON(!PageUptodate(page));
09cbfeaf 522 block_in_file = (sector_t)page->index << (PAGE_SHIFT - blkbits);
4b89a37d
JK
523 /*
524 * Whole page beyond EOF? Skip allocating blocks to avoid leaking
525 * space.
526 */
527 if (block_in_file >= (i_size + (1 << blkbits) - 1) >> blkbits)
528 goto page_is_mapped;
1da177e4
LT
529 last_block = (i_size - 1) >> blkbits;
530 map_bh.b_page = page;
531 for (page_block = 0; page_block < blocks_per_page; ) {
532
533 map_bh.b_state = 0;
b0cf2321 534 map_bh.b_size = 1 << blkbits;
0ea97180 535 if (mpd->get_block(inode, block_in_file, &map_bh, 1))
1da177e4
LT
536 goto confused;
537 if (buffer_new(&map_bh))
e64855c6 538 clean_bdev_bh_alias(&map_bh);
1da177e4
LT
539 if (buffer_boundary(&map_bh)) {
540 boundary_block = map_bh.b_blocknr;
541 boundary_bdev = map_bh.b_bdev;
542 }
543 if (page_block) {
544 if (map_bh.b_blocknr != blocks[page_block-1] + 1)
545 goto confused;
546 }
547 blocks[page_block++] = map_bh.b_blocknr;
548 boundary = buffer_boundary(&map_bh);
549 bdev = map_bh.b_bdev;
550 if (block_in_file == last_block)
551 break;
552 block_in_file++;
553 }
554 BUG_ON(page_block == 0);
555
556 first_unmapped = page_block;
557
558page_is_mapped:
09cbfeaf 559 end_index = i_size >> PAGE_SHIFT;
1da177e4
LT
560 if (page->index >= end_index) {
561 /*
562 * The page straddles i_size. It must be zeroed out on each
2a61aa40 563 * and every writepage invocation because it may be mmapped.
1da177e4
LT
564 * "A file is mapped in multiples of the page size. For a file
565 * that is not a multiple of the page size, the remaining memory
566 * is zeroed when mapped, and writes to that region are not
567 * written out to the file."
568 */
09cbfeaf 569 unsigned offset = i_size & (PAGE_SIZE - 1);
1da177e4
LT
570
571 if (page->index > end_index || !offset)
572 goto confused;
09cbfeaf 573 zero_user_segment(page, offset, PAGE_SIZE);
1da177e4
LT
574 }
575
576 /*
577 * This page will go to BIO. Do we need to send this BIO off first?
578 */
0ea97180 579 if (bio && mpd->last_block_in_bio != blocks[0] - 1)
77c436de 580 bio = mpage_bio_submit(bio);
1da177e4
LT
581
582alloc_new:
583 if (bio == NULL) {
77c436de
CH
584 bio = bio_alloc(bdev, BIO_MAX_VECS,
585 REQ_OP_WRITE | wbc_to_write_flags(wbc),
586 GFP_NOFS);
d5f68a42 587 bio->bi_iter.bi_sector = blocks[0] << (blkbits - 9);
b16b1deb 588 wbc_init_bio(wbc, bio);
1da177e4
LT
589 }
590
591 /*
592 * Must try to add the page before marking the buffer clean or
593 * the confused fail path above (OOM) will be very confused when
594 * it finds all bh marked clean (i.e. it will not write anything)
595 */
34e51a5e 596 wbc_account_cgroup_owner(wbc, page, PAGE_SIZE);
1da177e4
LT
597 length = first_unmapped << blkbits;
598 if (bio_add_page(bio, page, length, 0) < length) {
77c436de 599 bio = mpage_bio_submit(bio);
1da177e4
LT
600 goto alloc_new;
601 }
602
90768eee 603 clean_buffers(page, first_unmapped);
1da177e4
LT
604
605 BUG_ON(PageWriteback(page));
606 set_page_writeback(page);
607 unlock_page(page);
608 if (boundary || (first_unmapped != blocks_per_page)) {
77c436de 609 bio = mpage_bio_submit(bio);
1da177e4
LT
610 if (boundary_block) {
611 write_boundary_block(boundary_bdev,
612 boundary_block, 1 << blkbits);
613 }
614 } else {
0ea97180 615 mpd->last_block_in_bio = blocks[blocks_per_page - 1];
1da177e4
LT
616 }
617 goto out;
618
619confused:
620 if (bio)
77c436de 621 bio = mpage_bio_submit(bio);
1da177e4 622
1da177e4
LT
623 /*
624 * The caller has a ref on the inode, so *mapping is stable
625 */
f2d3e573 626 ret = block_write_full_page(page, mpd->get_block, wbc);
0ea97180 627 mapping_set_error(mapping, ret);
1da177e4 628out:
0ea97180
MS
629 mpd->bio = bio;
630 return ret;
1da177e4
LT
631}
632
633/**
78a4a50a 634 * mpage_writepages - walk the list of dirty pages of the given address space & writepage() all of them
1da177e4
LT
635 * @mapping: address space structure to write
636 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
637 * @get_block: the filesystem's block mapper function.
1da177e4
LT
638 *
639 * This is a library function, which implements the writepages()
640 * address_space_operation.
1da177e4
LT
641 */
642int
643mpage_writepages(struct address_space *mapping,
644 struct writeback_control *wbc, get_block_t get_block)
1da177e4 645{
cf5e7a65
CH
646 struct mpage_data mpd = {
647 .get_block = get_block,
648 };
2ed1a6bc 649 struct blk_plug plug;
0ea97180
MS
650 int ret;
651
2ed1a6bc 652 blk_start_plug(&plug);
cf5e7a65 653 ret = write_cache_pages(mapping, wbc, __mpage_writepage, &mpd);
77c436de
CH
654 if (mpd.bio)
655 mpage_bio_submit(mpd.bio);
2ed1a6bc 656 blk_finish_plug(&plug);
1da177e4
LT
657 return ret;
658}
1da177e4 659EXPORT_SYMBOL(mpage_writepages);