1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2002, Linus Torvalds.
7 * Contains functions related to preparing and submitting BIOs which contain
8 * multiple pagecache pages.
10 * 15May2002 Andrew Morton
12 * 27Jun2002 axboe@suse.de
13 * use bio_add_page() to build bio's just the right size
16 #include <linux/kernel.h>
17 #include <linux/export.h>
19 #include <linux/kdev_t.h>
20 #include <linux/gfp.h>
21 #include <linux/bio.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>
28 #include <linux/mm_inline.h>
29 #include <linux/writeback.h>
30 #include <linux/backing-dev.h>
31 #include <linux/pagevec.h>
32 #include <linux/cleancache.h>
35 #define CREATE_TRACE_POINTS
36 #include <trace/events/android_fs.h>
38 EXPORT_TRACEPOINT_SYMBOL(android_fs_datawrite_start
);
39 EXPORT_TRACEPOINT_SYMBOL(android_fs_datawrite_end
);
40 EXPORT_TRACEPOINT_SYMBOL(android_fs_dataread_start
);
41 EXPORT_TRACEPOINT_SYMBOL(android_fs_dataread_end
);
44 * I/O completion handler for multipage BIOs.
46 * The mpage code never puts partial pages into a BIO (except for end-of-file).
47 * If a page does not map to a contiguous run of blocks then it simply falls
48 * back to block_read_full_page().
50 * Why is this? If a page's completion depends on a number of different BIOs
51 * which can complete in any order (or at the same time) then determining the
52 * status of that page is hard. See end_buffer_async_read() for the details.
53 * There is no point in duplicating all that complexity.
55 static void mpage_end_io(struct bio
*bio
)
60 if (trace_android_fs_dataread_end_enabled() &&
61 (bio_data_dir(bio
) == READ
)) {
62 struct page
*first_page
= bio
->bi_io_vec
[0].bv_page
;
64 if (first_page
!= NULL
)
65 trace_android_fs_dataread_end(first_page
->mapping
->host
,
66 page_offset(first_page
),
67 bio
->bi_iter
.bi_size
);
70 bio_for_each_segment_all(bv
, bio
, i
) {
71 struct page
*page
= bv
->bv_page
;
72 page_endio(page
, op_is_write(bio_op(bio
)),
73 blk_status_to_errno(bio
->bi_status
));
79 static struct bio
*mpage_bio_submit(int op
, int op_flags
, struct bio
*bio
)
81 if (trace_android_fs_dataread_start_enabled() && (op
== REQ_OP_READ
)) {
82 struct page
*first_page
= bio
->bi_io_vec
[0].bv_page
;
84 if (first_page
!= NULL
) {
85 char *path
, pathbuf
[MAX_TRACE_PATHBUF_LEN
];
87 path
= android_fstrace_get_pathname(pathbuf
,
88 MAX_TRACE_PATHBUF_LEN
,
89 first_page
->mapping
->host
);
90 trace_android_fs_dataread_start(
91 first_page
->mapping
->host
,
92 page_offset(first_page
),
99 bio
->bi_end_io
= mpage_end_io
;
100 bio_set_op_attrs(bio
, op
, op_flags
);
101 guard_bio_eod(op
, bio
);
107 mpage_alloc(struct block_device
*bdev
,
108 sector_t first_sector
, int nr_vecs
,
113 /* Restrict the given (page cache) mask for slab allocations */
114 gfp_flags
&= GFP_KERNEL
;
115 bio
= bio_alloc(gfp_flags
, nr_vecs
);
117 if (bio
== NULL
&& (current
->flags
& PF_MEMALLOC
)) {
118 while (!bio
&& (nr_vecs
/= 2))
119 bio
= bio_alloc(gfp_flags
, nr_vecs
);
123 bio_set_dev(bio
, bdev
);
124 bio
->bi_iter
.bi_sector
= first_sector
;
130 * support function for mpage_readpages. The fs supplied get_block might
131 * return an up to date buffer. This is used to map that buffer into
132 * the page, which allows readpage to avoid triggering a duplicate call
135 * The idea is to avoid adding buffers to pages that don't already have
136 * them. So when the buffer is up to date and the page size == block size,
137 * this marks the page up to date instead of adding new buffers.
140 map_buffer_to_page(struct page
*page
, struct buffer_head
*bh
, int page_block
)
142 struct inode
*inode
= page
->mapping
->host
;
143 struct buffer_head
*page_bh
, *head
;
146 if (!page_has_buffers(page
)) {
148 * don't make any buffers if there is only one buffer on
149 * the page and the page just needs to be set up to date
151 if (inode
->i_blkbits
== PAGE_SHIFT
&&
152 buffer_uptodate(bh
)) {
153 SetPageUptodate(page
);
156 create_empty_buffers(page
, i_blocksize(inode
), 0);
158 head
= page_buffers(page
);
161 if (block
== page_block
) {
162 page_bh
->b_state
= bh
->b_state
;
163 page_bh
->b_bdev
= bh
->b_bdev
;
164 page_bh
->b_blocknr
= bh
->b_blocknr
;
167 page_bh
= page_bh
->b_this_page
;
169 } while (page_bh
!= head
);
173 * This is the worker routine which does all the work of mapping the disk
174 * blocks and constructs largest possible bios, submits them for IO if the
175 * blocks are not contiguous on the disk.
177 * We pass a buffer_head back and forth and use its buffer_mapped() flag to
178 * represent the validity of its disk mapping and to decide when to do the next
182 do_mpage_readpage(struct bio
*bio
, struct page
*page
, unsigned nr_pages
,
183 sector_t
*last_block_in_bio
, struct buffer_head
*map_bh
,
184 unsigned long *first_logical_block
, get_block_t get_block
,
187 struct inode
*inode
= page
->mapping
->host
;
188 const unsigned blkbits
= inode
->i_blkbits
;
189 const unsigned blocks_per_page
= PAGE_SIZE
>> blkbits
;
190 const unsigned blocksize
= 1 << blkbits
;
191 sector_t block_in_file
;
193 sector_t last_block_in_file
;
194 sector_t blocks
[MAX_BUF_PER_PAGE
];
196 unsigned first_hole
= blocks_per_page
;
197 struct block_device
*bdev
= NULL
;
199 int fully_mapped
= 1;
201 unsigned relative_block
;
203 if (page_has_buffers(page
))
206 block_in_file
= (sector_t
)page
->index
<< (PAGE_SHIFT
- blkbits
);
207 last_block
= block_in_file
+ nr_pages
* blocks_per_page
;
208 last_block_in_file
= (i_size_read(inode
) + blocksize
- 1) >> blkbits
;
209 if (last_block
> last_block_in_file
)
210 last_block
= last_block_in_file
;
214 * Map blocks using the result from the previous get_blocks call first.
216 nblocks
= map_bh
->b_size
>> blkbits
;
217 if (buffer_mapped(map_bh
) && block_in_file
> *first_logical_block
&&
218 block_in_file
< (*first_logical_block
+ nblocks
)) {
219 unsigned map_offset
= block_in_file
- *first_logical_block
;
220 unsigned last
= nblocks
- map_offset
;
222 for (relative_block
= 0; ; relative_block
++) {
223 if (relative_block
== last
) {
224 clear_buffer_mapped(map_bh
);
227 if (page_block
== blocks_per_page
)
229 blocks
[page_block
] = map_bh
->b_blocknr
+ map_offset
+
234 bdev
= map_bh
->b_bdev
;
238 * Then do more get_blocks calls until we are done with this page.
240 map_bh
->b_page
= page
;
241 while (page_block
< blocks_per_page
) {
245 if (block_in_file
< last_block
) {
246 map_bh
->b_size
= (last_block
-block_in_file
) << blkbits
;
247 if (get_block(inode
, block_in_file
, map_bh
, 0))
249 *first_logical_block
= block_in_file
;
252 if (!buffer_mapped(map_bh
)) {
254 if (first_hole
== blocks_per_page
)
255 first_hole
= page_block
;
261 /* some filesystems will copy data into the page during
262 * the get_block call, in which case we don't want to
263 * read it again. map_buffer_to_page copies the data
264 * we just collected from get_block into the page's buffers
265 * so readpage doesn't have to repeat the get_block call
267 if (buffer_uptodate(map_bh
)) {
268 map_buffer_to_page(page
, map_bh
, page_block
);
272 if (first_hole
!= blocks_per_page
)
273 goto confused
; /* hole -> non-hole */
275 /* Contiguous blocks? */
276 if (page_block
&& blocks
[page_block
-1] != map_bh
->b_blocknr
-1)
278 nblocks
= map_bh
->b_size
>> blkbits
;
279 for (relative_block
= 0; ; relative_block
++) {
280 if (relative_block
== nblocks
) {
281 clear_buffer_mapped(map_bh
);
283 } else if (page_block
== blocks_per_page
)
285 blocks
[page_block
] = map_bh
->b_blocknr
+relative_block
;
289 bdev
= map_bh
->b_bdev
;
292 if (first_hole
!= blocks_per_page
) {
293 zero_user_segment(page
, first_hole
<< blkbits
, PAGE_SIZE
);
294 if (first_hole
== 0) {
295 SetPageUptodate(page
);
299 } else if (fully_mapped
) {
300 SetPageMappedToDisk(page
);
303 if (fully_mapped
&& blocks_per_page
== 1 && !PageUptodate(page
) &&
304 cleancache_get_page(page
) == 0) {
305 SetPageUptodate(page
);
310 * This page will go to BIO. Do we need to send this BIO off first?
312 if (bio
&& (*last_block_in_bio
!= blocks
[0] - 1))
313 bio
= mpage_bio_submit(REQ_OP_READ
, 0, bio
);
317 if (first_hole
== blocks_per_page
) {
318 if (!bdev_read_page(bdev
, blocks
[0] << (blkbits
- 9),
322 bio
= mpage_alloc(bdev
, blocks
[0] << (blkbits
- 9),
323 min_t(int, nr_pages
, BIO_MAX_PAGES
), gfp
);
328 length
= first_hole
<< blkbits
;
329 if (bio_add_page(bio
, page
, length
, 0) < length
) {
330 bio
= mpage_bio_submit(REQ_OP_READ
, 0, bio
);
334 relative_block
= block_in_file
- *first_logical_block
;
335 nblocks
= map_bh
->b_size
>> blkbits
;
336 if ((buffer_boundary(map_bh
) && relative_block
== nblocks
) ||
337 (first_hole
!= blocks_per_page
))
338 bio
= mpage_bio_submit(REQ_OP_READ
, 0, bio
);
340 *last_block_in_bio
= blocks
[blocks_per_page
- 1];
346 bio
= mpage_bio_submit(REQ_OP_READ
, 0, bio
);
347 if (!PageUptodate(page
))
348 block_read_full_page(page
, get_block
);
355 * mpage_readpages - populate an address space with some pages & start reads against them
356 * @mapping: the address_space
357 * @pages: The address of a list_head which contains the target pages. These
358 * pages have their ->index populated and are otherwise uninitialised.
359 * The page at @pages->prev has the lowest file offset, and reads should be
360 * issued in @pages->prev to @pages->next order.
361 * @nr_pages: The number of pages at *@pages
362 * @get_block: The filesystem's block mapper function.
364 * This function walks the pages and the blocks within each page, building and
365 * emitting large BIOs.
367 * If anything unusual happens, such as:
369 * - encountering a page which has buffers
370 * - encountering a page which has a non-hole after a hole
371 * - encountering a page with non-contiguous blocks
373 * then this code just gives up and calls the buffer_head-based read function.
374 * It does handle a page which has holes at the end - that is a common case:
375 * the end-of-file on blocksize < PAGE_SIZE setups.
377 * BH_Boundary explanation:
379 * There is a problem. The mpage read code assembles several pages, gets all
380 * their disk mappings, and then submits them all. That's fine, but obtaining
381 * the disk mappings may require I/O. Reads of indirect blocks, for example.
383 * So an mpage read of the first 16 blocks of an ext2 file will cause I/O to be
384 * submitted in the following order:
386 * 12 0 1 2 3 4 5 6 7 8 9 10 11 13 14 15 16
388 * because the indirect block has to be read to get the mappings of blocks
389 * 13,14,15,16. Obviously, this impacts performance.
391 * So what we do it to allow the filesystem's get_block() function to set
392 * BH_Boundary when it maps block 11. BH_Boundary says: mapping of the block
393 * after this one will require I/O against a block which is probably close to
394 * this one. So you should push what I/O you have currently accumulated.
396 * This all causes the disk requests to be issued in the correct order.
399 mpage_readpages(struct address_space
*mapping
, struct list_head
*pages
,
400 unsigned nr_pages
, get_block_t get_block
)
402 struct bio
*bio
= NULL
;
404 sector_t last_block_in_bio
= 0;
405 struct buffer_head map_bh
;
406 unsigned long first_logical_block
= 0;
407 gfp_t gfp
= readahead_gfp_mask(mapping
);
411 for (page_idx
= 0; page_idx
< nr_pages
; page_idx
++) {
412 struct page
*page
= lru_to_page(pages
);
414 prefetchw(&page
->flags
);
415 list_del(&page
->lru
);
416 if (!add_to_page_cache_lru(page
, mapping
,
419 bio
= do_mpage_readpage(bio
, page
,
421 &last_block_in_bio
, &map_bh
,
422 &first_logical_block
,
427 BUG_ON(!list_empty(pages
));
429 mpage_bio_submit(REQ_OP_READ
, 0, bio
);
432 EXPORT_SYMBOL(mpage_readpages
);
435 * This isn't called much at all
437 int mpage_readpage(struct page
*page
, get_block_t get_block
)
439 struct bio
*bio
= NULL
;
440 sector_t last_block_in_bio
= 0;
441 struct buffer_head map_bh
;
442 unsigned long first_logical_block
= 0;
443 gfp_t gfp
= mapping_gfp_constraint(page
->mapping
, GFP_KERNEL
);
447 bio
= do_mpage_readpage(bio
, page
, 1, &last_block_in_bio
,
448 &map_bh
, &first_logical_block
, get_block
, gfp
);
450 mpage_bio_submit(REQ_OP_READ
, 0, bio
);
453 EXPORT_SYMBOL(mpage_readpage
);
456 * Writing is not so simple.
458 * If the page has buffers then they will be used for obtaining the disk
459 * mapping. We only support pages which are fully mapped-and-dirty, with a
460 * special case for pages which are unmapped at the end: end-of-file.
462 * If the page has no buffers (preferred) then the page is mapped here.
464 * If all blocks are found to be contiguous then the page can go into the
465 * BIO. Otherwise fall back to the mapping's writepage().
467 * FIXME: This code wants an estimate of how many pages are still to be
468 * written, so it can intelligently allocate a suitably-sized BIO. For now,
469 * just allocate full-size (16-page) BIOs.
474 sector_t last_block_in_bio
;
475 get_block_t
*get_block
;
476 unsigned use_writepage
;
480 * We have our BIO, so we can now mark the buffers clean. Make
481 * sure to only clean buffers which we know we'll be writing.
483 static void clean_buffers(struct page
*page
, unsigned first_unmapped
)
485 unsigned buffer_counter
= 0;
486 struct buffer_head
*bh
, *head
;
487 if (!page_has_buffers(page
))
489 head
= page_buffers(page
);
493 if (buffer_counter
++ == first_unmapped
)
495 clear_buffer_dirty(bh
);
496 bh
= bh
->b_this_page
;
497 } while (bh
!= head
);
500 * we cannot drop the bh if the page is not uptodate or a concurrent
501 * readpage would fail to serialize with the bh and it would read from
502 * disk before we reach the platter.
504 if (buffer_heads_over_limit
&& PageUptodate(page
))
505 try_to_free_buffers(page
);
509 * For situations where we want to clean all buffers attached to a page.
510 * We don't need to calculate how many buffers are attached to the page,
511 * we just need to specify a number larger than the maximum number of buffers.
513 void clean_page_buffers(struct page
*page
)
515 clean_buffers(page
, ~0U);
518 static int __mpage_writepage(struct page
*page
, struct writeback_control
*wbc
,
521 struct mpage_data
*mpd
= data
;
522 struct bio
*bio
= mpd
->bio
;
523 struct address_space
*mapping
= page
->mapping
;
524 struct inode
*inode
= page
->mapping
->host
;
525 const unsigned blkbits
= inode
->i_blkbits
;
526 unsigned long end_index
;
527 const unsigned blocks_per_page
= PAGE_SIZE
>> blkbits
;
529 sector_t block_in_file
;
530 sector_t blocks
[MAX_BUF_PER_PAGE
];
532 unsigned first_unmapped
= blocks_per_page
;
533 struct block_device
*bdev
= NULL
;
535 sector_t boundary_block
= 0;
536 struct block_device
*boundary_bdev
= NULL
;
538 struct buffer_head map_bh
;
539 loff_t i_size
= i_size_read(inode
);
541 int op_flags
= wbc_to_write_flags(wbc
);
543 if (page_has_buffers(page
)) {
544 struct buffer_head
*head
= page_buffers(page
);
545 struct buffer_head
*bh
= head
;
547 /* If they're all mapped and dirty, do it */
550 BUG_ON(buffer_locked(bh
));
551 if (!buffer_mapped(bh
)) {
553 * unmapped dirty buffers are created by
554 * __set_page_dirty_buffers -> mmapped data
556 if (buffer_dirty(bh
))
558 if (first_unmapped
== blocks_per_page
)
559 first_unmapped
= page_block
;
563 if (first_unmapped
!= blocks_per_page
)
564 goto confused
; /* hole -> non-hole */
566 if (!buffer_dirty(bh
) || !buffer_uptodate(bh
))
569 if (bh
->b_blocknr
!= blocks
[page_block
-1] + 1)
572 blocks
[page_block
++] = bh
->b_blocknr
;
573 boundary
= buffer_boundary(bh
);
575 boundary_block
= bh
->b_blocknr
;
576 boundary_bdev
= bh
->b_bdev
;
579 } while ((bh
= bh
->b_this_page
) != head
);
585 * Page has buffers, but they are all unmapped. The page was
586 * created by pagein or read over a hole which was handled by
587 * block_read_full_page(). If this address_space is also
588 * using mpage_readpages then this can rarely happen.
594 * The page has no buffers: map it to disk
596 BUG_ON(!PageUptodate(page
));
597 block_in_file
= (sector_t
)page
->index
<< (PAGE_SHIFT
- blkbits
);
598 last_block
= (i_size
- 1) >> blkbits
;
599 map_bh
.b_page
= page
;
600 for (page_block
= 0; page_block
< blocks_per_page
; ) {
603 map_bh
.b_size
= 1 << blkbits
;
604 if (mpd
->get_block(inode
, block_in_file
, &map_bh
, 1))
606 if (buffer_new(&map_bh
))
607 clean_bdev_bh_alias(&map_bh
);
608 if (buffer_boundary(&map_bh
)) {
609 boundary_block
= map_bh
.b_blocknr
;
610 boundary_bdev
= map_bh
.b_bdev
;
613 if (map_bh
.b_blocknr
!= blocks
[page_block
-1] + 1)
616 blocks
[page_block
++] = map_bh
.b_blocknr
;
617 boundary
= buffer_boundary(&map_bh
);
618 bdev
= map_bh
.b_bdev
;
619 if (block_in_file
== last_block
)
623 BUG_ON(page_block
== 0);
625 first_unmapped
= page_block
;
628 end_index
= i_size
>> PAGE_SHIFT
;
629 if (page
->index
>= end_index
) {
631 * The page straddles i_size. It must be zeroed out on each
632 * and every writepage invocation because it may be mmapped.
633 * "A file is mapped in multiples of the page size. For a file
634 * that is not a multiple of the page size, the remaining memory
635 * is zeroed when mapped, and writes to that region are not
636 * written out to the file."
638 unsigned offset
= i_size
& (PAGE_SIZE
- 1);
640 if (page
->index
> end_index
|| !offset
)
642 zero_user_segment(page
, offset
, PAGE_SIZE
);
646 * This page will go to BIO. Do we need to send this BIO off first?
648 if (bio
&& mpd
->last_block_in_bio
!= blocks
[0] - 1)
649 bio
= mpage_bio_submit(REQ_OP_WRITE
, op_flags
, bio
);
653 if (first_unmapped
== blocks_per_page
) {
654 if (!bdev_write_page(bdev
, blocks
[0] << (blkbits
- 9),
658 bio
= mpage_alloc(bdev
, blocks
[0] << (blkbits
- 9),
659 BIO_MAX_PAGES
, GFP_NOFS
|__GFP_HIGH
);
663 wbc_init_bio(wbc
, bio
);
664 bio
->bi_write_hint
= inode
->i_write_hint
;
668 * Must try to add the page before marking the buffer clean or
669 * the confused fail path above (OOM) will be very confused when
670 * it finds all bh marked clean (i.e. it will not write anything)
672 wbc_account_io(wbc
, page
, PAGE_SIZE
);
673 length
= first_unmapped
<< blkbits
;
674 if (bio_add_page(bio
, page
, length
, 0) < length
) {
675 bio
= mpage_bio_submit(REQ_OP_WRITE
, op_flags
, bio
);
679 clean_buffers(page
, first_unmapped
);
681 BUG_ON(PageWriteback(page
));
682 set_page_writeback(page
);
684 if (boundary
|| (first_unmapped
!= blocks_per_page
)) {
685 bio
= mpage_bio_submit(REQ_OP_WRITE
, op_flags
, bio
);
686 if (boundary_block
) {
687 write_boundary_block(boundary_bdev
,
688 boundary_block
, 1 << blkbits
);
691 mpd
->last_block_in_bio
= blocks
[blocks_per_page
- 1];
697 bio
= mpage_bio_submit(REQ_OP_WRITE
, op_flags
, bio
);
699 if (mpd
->use_writepage
) {
700 ret
= mapping
->a_ops
->writepage(page
, wbc
);
706 * The caller has a ref on the inode, so *mapping is stable
708 mapping_set_error(mapping
, ret
);
715 * mpage_writepages - walk the list of dirty pages of the given address space & writepage() all of them
716 * @mapping: address space structure to write
717 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
718 * @get_block: the filesystem's block mapper function.
719 * If this is NULL then use a_ops->writepage. Otherwise, go
722 * This is a library function, which implements the writepages()
723 * address_space_operation.
725 * If a page is already under I/O, generic_writepages() skips it, even
726 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
727 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
728 * and msync() need to guarantee that all the data which was dirty at the time
729 * the call was made get new I/O started against them. If wbc->sync_mode is
730 * WB_SYNC_ALL then we were called for data integrity and we must wait for
731 * existing IO to complete.
734 mpage_writepages(struct address_space
*mapping
,
735 struct writeback_control
*wbc
, get_block_t get_block
)
737 struct blk_plug plug
;
740 blk_start_plug(&plug
);
743 ret
= generic_writepages(mapping
, wbc
);
745 struct mpage_data mpd
= {
747 .last_block_in_bio
= 0,
748 .get_block
= get_block
,
752 ret
= write_cache_pages(mapping
, wbc
, __mpage_writepage
, &mpd
);
754 int op_flags
= (wbc
->sync_mode
== WB_SYNC_ALL
?
756 mpage_bio_submit(REQ_OP_WRITE
, op_flags
, mpd
.bio
);
759 blk_finish_plug(&plug
);
762 EXPORT_SYMBOL(mpage_writepages
);
764 int mpage_writepage(struct page
*page
, get_block_t get_block
,
765 struct writeback_control
*wbc
)
767 struct mpage_data mpd
= {
769 .last_block_in_bio
= 0,
770 .get_block
= get_block
,
773 int ret
= __mpage_writepage(page
, wbc
, &mpd
);
775 int op_flags
= (wbc
->sync_mode
== WB_SYNC_ALL
?
777 mpage_bio_submit(REQ_OP_WRITE
, op_flags
, mpd
.bio
);
781 EXPORT_SYMBOL(mpage_writepage
);