4 * Copyright (C) 2002, Linus Torvalds.
6 * Contains functions related to preparing and submitting BIOs which contain
7 * multiple pagecache pages.
9 * 15May2002 Andrew Morton
11 * 27Jun2002 axboe@suse.de
12 * use bio_add_page() to build bio's just the right size
15 #include <linux/kernel.h>
16 #include <linux/export.h>
18 #include <linux/kdev_t.h>
19 #include <linux/gfp.h>
20 #include <linux/bio.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>
33 #define CREATE_TRACE_POINTS
34 #include <trace/events/android_fs.h>
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);
42 * I/O completion handler for multipage BIOs.
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().
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.
53 static void mpage_end_io(struct bio *bio)
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;
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);
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);
76 static struct bio *mpage_bio_submit(int rw, struct bio *bio)
78 if (trace_android_fs_dataread_start_enabled() && (rw == READ)) {
79 struct page *first_page = bio->bi_io_vec[0].bv_page;
81 if (first_page != NULL) {
82 trace_android_fs_dataread_start(
83 first_page->mapping->host,
84 page_offset(first_page),
90 bio->bi_end_io = mpage_end_io;
91 guard_bio_eod(rw, bio);
97 mpage_alloc(struct block_device *bdev,
98 sector_t first_sector, int nr_vecs,
103 bio = bio_alloc(gfp_flags, nr_vecs);
105 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
106 while (!bio && (nr_vecs /= 2))
107 bio = bio_alloc(gfp_flags, nr_vecs);
112 bio->bi_iter.bi_sector = first_sector;
118 * support function for mpage_readpages. The fs supplied get_block might
119 * return an up to date buffer. This is used to map that buffer into
120 * the page, which allows readpage to avoid triggering a duplicate call
123 * The idea is to avoid adding buffers to pages that don't already have
124 * them. So when the buffer is up to date and the page size == block size,
125 * this marks the page up to date instead of adding new buffers.
128 map_buffer_to_page(struct page *page, struct buffer_head *bh, int page_block)
130 struct inode *inode = page->mapping->host;
131 struct buffer_head *page_bh, *head;
134 if (!page_has_buffers(page)) {
136 * don't make any buffers if there is only one buffer on
137 * the page and the page just needs to be set up to date
139 if (inode->i_blkbits == PAGE_CACHE_SHIFT &&
140 buffer_uptodate(bh)) {
141 SetPageUptodate(page);
144 create_empty_buffers(page, 1 << inode->i_blkbits, 0);
146 head = page_buffers(page);
149 if (block == page_block) {
150 page_bh->b_state = bh->b_state;
151 page_bh->b_bdev = bh->b_bdev;
152 page_bh->b_blocknr = bh->b_blocknr;
155 page_bh = page_bh->b_this_page;
157 } while (page_bh != head);
161 * This is the worker routine which does all the work of mapping the disk
162 * blocks and constructs largest possible bios, submits them for IO if the
163 * blocks are not contiguous on the disk.
165 * We pass a buffer_head back and forth and use its buffer_mapped() flag to
166 * represent the validity of its disk mapping and to decide when to do the next
170 do_mpage_readpage(struct bio *bio, struct page *page, unsigned nr_pages,
171 sector_t *last_block_in_bio, struct buffer_head *map_bh,
172 unsigned long *first_logical_block, get_block_t get_block,
175 struct inode *inode = page->mapping->host;
176 const unsigned blkbits = inode->i_blkbits;
177 const unsigned blocks_per_page = PAGE_CACHE_SIZE >> blkbits;
178 const unsigned blocksize = 1 << blkbits;
179 sector_t block_in_file;
181 sector_t last_block_in_file;
182 sector_t blocks[MAX_BUF_PER_PAGE];
184 unsigned first_hole = blocks_per_page;
185 struct block_device *bdev = NULL;
187 int fully_mapped = 1;
189 unsigned relative_block;
191 if (page_has_buffers(page))
194 block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits);
195 last_block = block_in_file + nr_pages * blocks_per_page;
196 last_block_in_file = (i_size_read(inode) + blocksize - 1) >> blkbits;
197 if (last_block > last_block_in_file)
198 last_block = last_block_in_file;
202 * Map blocks using the result from the previous get_blocks call first.
204 nblocks = map_bh->b_size >> blkbits;
205 if (buffer_mapped(map_bh) && block_in_file > *first_logical_block &&
206 block_in_file < (*first_logical_block + nblocks)) {
207 unsigned map_offset = block_in_file - *first_logical_block;
208 unsigned last = nblocks - map_offset;
210 for (relative_block = 0; ; relative_block++) {
211 if (relative_block == last) {
212 clear_buffer_mapped(map_bh);
215 if (page_block == blocks_per_page)
217 blocks[page_block] = map_bh->b_blocknr + map_offset +
222 bdev = map_bh->b_bdev;
226 * Then do more get_blocks calls until we are done with this page.
228 map_bh->b_page = page;
229 while (page_block < blocks_per_page) {
233 if (block_in_file < last_block) {
234 map_bh->b_size = (last_block-block_in_file) << blkbits;
235 if (get_block(inode, block_in_file, map_bh, 0))
237 *first_logical_block = block_in_file;
240 if (!buffer_mapped(map_bh)) {
242 if (first_hole == blocks_per_page)
243 first_hole = page_block;
249 /* some filesystems will copy data into the page during
250 * the get_block call, in which case we don't want to
251 * read it again. map_buffer_to_page copies the data
252 * we just collected from get_block into the page's buffers
253 * so readpage doesn't have to repeat the get_block call
255 if (buffer_uptodate(map_bh)) {
256 map_buffer_to_page(page, map_bh, page_block);
260 if (first_hole != blocks_per_page)
261 goto confused; /* hole -> non-hole */
263 /* Contiguous blocks? */
264 if (page_block && blocks[page_block-1] != map_bh->b_blocknr-1)
266 nblocks = map_bh->b_size >> blkbits;
267 for (relative_block = 0; ; relative_block++) {
268 if (relative_block == nblocks) {
269 clear_buffer_mapped(map_bh);
271 } else if (page_block == blocks_per_page)
273 blocks[page_block] = map_bh->b_blocknr+relative_block;
277 bdev = map_bh->b_bdev;
280 if (first_hole != blocks_per_page) {
281 zero_user_segment(page, first_hole << blkbits, PAGE_CACHE_SIZE);
282 if (first_hole == 0) {
283 SetPageUptodate(page);
287 } else if (fully_mapped) {
288 SetPageMappedToDisk(page);
291 if (fully_mapped && blocks_per_page == 1 && !PageUptodate(page) &&
292 cleancache_get_page(page) == 0) {
293 SetPageUptodate(page);
298 * This page will go to BIO. Do we need to send this BIO off first?
300 if (bio && (*last_block_in_bio != blocks[0] - 1))
301 bio = mpage_bio_submit(READ, bio);
305 if (first_hole == blocks_per_page) {
306 if (!bdev_read_page(bdev, blocks[0] << (blkbits - 9),
310 bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
311 min_t(int, nr_pages, BIO_MAX_PAGES), gfp);
316 length = first_hole << blkbits;
317 if (bio_add_page(bio, page, length, 0) < length) {
318 bio = mpage_bio_submit(READ, bio);
322 relative_block = block_in_file - *first_logical_block;
323 nblocks = map_bh->b_size >> blkbits;
324 if ((buffer_boundary(map_bh) && relative_block == nblocks) ||
325 (first_hole != blocks_per_page))
326 bio = mpage_bio_submit(READ, bio);
328 *last_block_in_bio = blocks[blocks_per_page - 1];
334 bio = mpage_bio_submit(READ, bio);
335 if (!PageUptodate(page))
336 block_read_full_page(page, get_block);
343 * mpage_readpages - populate an address space with some pages & start reads against them
344 * @mapping: the address_space
345 * @pages: The address of a list_head which contains the target pages. These
346 * pages have their ->index populated and are otherwise uninitialised.
347 * The page at @pages->prev has the lowest file offset, and reads should be
348 * issued in @pages->prev to @pages->next order.
349 * @nr_pages: The number of pages at *@pages
350 * @get_block: The filesystem's block mapper function.
352 * This function walks the pages and the blocks within each page, building and
353 * emitting large BIOs.
355 * If anything unusual happens, such as:
357 * - encountering a page which has buffers
358 * - encountering a page which has a non-hole after a hole
359 * - encountering a page with non-contiguous blocks
361 * then this code just gives up and calls the buffer_head-based read function.
362 * It does handle a page which has holes at the end - that is a common case:
363 * the end-of-file on blocksize < PAGE_CACHE_SIZE setups.
365 * BH_Boundary explanation:
367 * There is a problem. The mpage read code assembles several pages, gets all
368 * their disk mappings, and then submits them all. That's fine, but obtaining
369 * the disk mappings may require I/O. Reads of indirect blocks, for example.
371 * So an mpage read of the first 16 blocks of an ext2 file will cause I/O to be
372 * submitted in the following order:
373 * 12 0 1 2 3 4 5 6 7 8 9 10 11 13 14 15 16
375 * because the indirect block has to be read to get the mappings of blocks
376 * 13,14,15,16. Obviously, this impacts performance.
378 * So what we do it to allow the filesystem's get_block() function to set
379 * BH_Boundary when it maps block 11. BH_Boundary says: mapping of the block
380 * after this one will require I/O against a block which is probably close to
381 * this one. So you should push what I/O you have currently accumulated.
383 * This all causes the disk requests to be issued in the correct order.
386 mpage_readpages(struct address_space *mapping, struct list_head *pages,
387 unsigned nr_pages, get_block_t get_block)
389 struct bio *bio = NULL;
391 sector_t last_block_in_bio = 0;
392 struct buffer_head map_bh;
393 unsigned long first_logical_block = 0;
394 gfp_t gfp = mapping_gfp_constraint(mapping, GFP_KERNEL);
398 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
399 struct page *page = list_entry(pages->prev, struct page, lru);
401 prefetchw(&page->flags);
402 list_del(&page->lru);
403 if (!add_to_page_cache_lru(page, mapping,
406 bio = do_mpage_readpage(bio, page,
408 &last_block_in_bio, &map_bh,
409 &first_logical_block,
412 page_cache_release(page);
414 BUG_ON(!list_empty(pages));
416 mpage_bio_submit(READ, bio);
419 EXPORT_SYMBOL(mpage_readpages);
422 * This isn't called much at all
424 int mpage_readpage(struct page *page, get_block_t get_block)
426 struct bio *bio = NULL;
427 sector_t last_block_in_bio = 0;
428 struct buffer_head map_bh;
429 unsigned long first_logical_block = 0;
430 gfp_t gfp = mapping_gfp_constraint(page->mapping, GFP_KERNEL);
434 bio = do_mpage_readpage(bio, page, 1, &last_block_in_bio,
435 &map_bh, &first_logical_block, get_block, gfp);
437 mpage_bio_submit(READ, bio);
440 EXPORT_SYMBOL(mpage_readpage);
443 * Writing is not so simple.
445 * If the page has buffers then they will be used for obtaining the disk
446 * mapping. We only support pages which are fully mapped-and-dirty, with a
447 * special case for pages which are unmapped at the end: end-of-file.
449 * If the page has no buffers (preferred) then the page is mapped here.
451 * If all blocks are found to be contiguous then the page can go into the
452 * BIO. Otherwise fall back to the mapping's writepage().
454 * FIXME: This code wants an estimate of how many pages are still to be
455 * written, so it can intelligently allocate a suitably-sized BIO. For now,
456 * just allocate full-size (16-page) BIOs.
461 sector_t last_block_in_bio;
462 get_block_t *get_block;
463 unsigned use_writepage;
467 * We have our BIO, so we can now mark the buffers clean. Make
468 * sure to only clean buffers which we know we'll be writing.
470 static void clean_buffers(struct page *page, unsigned first_unmapped)
472 unsigned buffer_counter = 0;
473 struct buffer_head *bh, *head;
474 if (!page_has_buffers(page))
476 head = page_buffers(page);
480 if (buffer_counter++ == first_unmapped)
482 clear_buffer_dirty(bh);
483 bh = bh->b_this_page;
484 } while (bh != head);
487 * we cannot drop the bh if the page is not uptodate or a concurrent
488 * readpage would fail to serialize with the bh and it would read from
489 * disk before we reach the platter.
491 if (buffer_heads_over_limit && PageUptodate(page))
492 try_to_free_buffers(page);
495 static int __mpage_writepage(struct page *page, struct writeback_control *wbc,
498 struct mpage_data *mpd = data;
499 struct bio *bio = mpd->bio;
500 struct address_space *mapping = page->mapping;
501 struct inode *inode = page->mapping->host;
502 const unsigned blkbits = inode->i_blkbits;
503 unsigned long end_index;
504 const unsigned blocks_per_page = PAGE_CACHE_SIZE >> blkbits;
506 sector_t block_in_file;
507 sector_t blocks[MAX_BUF_PER_PAGE];
509 unsigned first_unmapped = blocks_per_page;
510 struct block_device *bdev = NULL;
512 sector_t boundary_block = 0;
513 struct block_device *boundary_bdev = NULL;
515 struct buffer_head map_bh;
516 loff_t i_size = i_size_read(inode);
518 int wr = (wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE);
520 if (page_has_buffers(page)) {
521 struct buffer_head *head = page_buffers(page);
522 struct buffer_head *bh = head;
524 /* If they're all mapped and dirty, do it */
527 BUG_ON(buffer_locked(bh));
528 if (!buffer_mapped(bh)) {
530 * unmapped dirty buffers are created by
531 * __set_page_dirty_buffers -> mmapped data
533 if (buffer_dirty(bh))
535 if (first_unmapped == blocks_per_page)
536 first_unmapped = page_block;
540 if (first_unmapped != blocks_per_page)
541 goto confused; /* hole -> non-hole */
543 if (!buffer_dirty(bh) || !buffer_uptodate(bh))
546 if (bh->b_blocknr != blocks[page_block-1] + 1)
549 blocks[page_block++] = bh->b_blocknr;
550 boundary = buffer_boundary(bh);
552 boundary_block = bh->b_blocknr;
553 boundary_bdev = bh->b_bdev;
556 } while ((bh = bh->b_this_page) != head);
562 * Page has buffers, but they are all unmapped. The page was
563 * created by pagein or read over a hole which was handled by
564 * block_read_full_page(). If this address_space is also
565 * using mpage_readpages then this can rarely happen.
571 * The page has no buffers: map it to disk
573 BUG_ON(!PageUptodate(page));
574 block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits);
575 last_block = (i_size - 1) >> blkbits;
576 map_bh.b_page = page;
577 for (page_block = 0; page_block < blocks_per_page; ) {
580 map_bh.b_size = 1 << blkbits;
581 if (mpd->get_block(inode, block_in_file, &map_bh, 1))
583 if (buffer_new(&map_bh))
584 unmap_underlying_metadata(map_bh.b_bdev,
586 if (buffer_boundary(&map_bh)) {
587 boundary_block = map_bh.b_blocknr;
588 boundary_bdev = map_bh.b_bdev;
591 if (map_bh.b_blocknr != blocks[page_block-1] + 1)
594 blocks[page_block++] = map_bh.b_blocknr;
595 boundary = buffer_boundary(&map_bh);
596 bdev = map_bh.b_bdev;
597 if (block_in_file == last_block)
601 BUG_ON(page_block == 0);
603 first_unmapped = page_block;
606 end_index = i_size >> PAGE_CACHE_SHIFT;
607 if (page->index >= end_index) {
609 * The page straddles i_size. It must be zeroed out on each
610 * and every writepage invocation because it may be mmapped.
611 * "A file is mapped in multiples of the page size. For a file
612 * that is not a multiple of the page size, the remaining memory
613 * is zeroed when mapped, and writes to that region are not
614 * written out to the file."
616 unsigned offset = i_size & (PAGE_CACHE_SIZE - 1);
618 if (page->index > end_index || !offset)
620 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
624 * This page will go to BIO. Do we need to send this BIO off first?
626 if (bio && mpd->last_block_in_bio != blocks[0] - 1)
627 bio = mpage_bio_submit(wr, bio);
631 if (first_unmapped == blocks_per_page) {
632 if (!bdev_write_page(bdev, blocks[0] << (blkbits - 9),
634 clean_buffers(page, first_unmapped);
638 bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
639 BIO_MAX_PAGES, GFP_NOFS|__GFP_HIGH);
643 wbc_init_bio(wbc, bio);
647 * Must try to add the page before marking the buffer clean or
648 * the confused fail path above (OOM) will be very confused when
649 * it finds all bh marked clean (i.e. it will not write anything)
651 wbc_account_io(wbc, page, PAGE_SIZE);
652 length = first_unmapped << blkbits;
653 if (bio_add_page(bio, page, length, 0) < length) {
654 bio = mpage_bio_submit(wr, bio);
658 clean_buffers(page, first_unmapped);
660 BUG_ON(PageWriteback(page));
661 set_page_writeback(page);
663 if (boundary || (first_unmapped != blocks_per_page)) {
664 bio = mpage_bio_submit(wr, bio);
665 if (boundary_block) {
666 write_boundary_block(boundary_bdev,
667 boundary_block, 1 << blkbits);
670 mpd->last_block_in_bio = blocks[blocks_per_page - 1];
676 bio = mpage_bio_submit(wr, bio);
678 if (mpd->use_writepage) {
679 ret = mapping->a_ops->writepage(page, wbc);
685 * The caller has a ref on the inode, so *mapping is stable
687 mapping_set_error(mapping, ret);
694 * mpage_writepages - walk the list of dirty pages of the given address space & writepage() all of them
695 * @mapping: address space structure to write
696 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
697 * @get_block: the filesystem's block mapper function.
698 * If this is NULL then use a_ops->writepage. Otherwise, go
701 * This is a library function, which implements the writepages()
702 * address_space_operation.
704 * If a page is already under I/O, generic_writepages() skips it, even
705 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
706 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
707 * and msync() need to guarantee that all the data which was dirty at the time
708 * the call was made get new I/O started against them. If wbc->sync_mode is
709 * WB_SYNC_ALL then we were called for data integrity and we must wait for
710 * existing IO to complete.
713 mpage_writepages(struct address_space *mapping,
714 struct writeback_control *wbc, get_block_t get_block)
716 struct blk_plug plug;
719 blk_start_plug(&plug);
722 ret = generic_writepages(mapping, wbc);
724 struct mpage_data mpd = {
726 .last_block_in_bio = 0,
727 .get_block = get_block,
731 ret = write_cache_pages(mapping, wbc, __mpage_writepage, &mpd);
733 int wr = (wbc->sync_mode == WB_SYNC_ALL ?
735 mpage_bio_submit(wr, mpd.bio);
738 blk_finish_plug(&plug);
741 EXPORT_SYMBOL(mpage_writepages);
743 int mpage_writepage(struct page *page, get_block_t get_block,
744 struct writeback_control *wbc)
746 struct mpage_data mpd = {
748 .last_block_in_bio = 0,
749 .get_block = get_block,
752 int ret = __mpage_writepage(page, wbc, &mpd);
754 int wr = (wbc->sync_mode == WB_SYNC_ALL ?
756 mpage_bio_submit(wr, mpd.bio);
760 EXPORT_SYMBOL(mpage_writepage);