4 * Copyright (C) 2002, Linus Torvalds.
6 * Contains functions related to preparing and submitting BIOs which contain
7 * multiple pagecache pages.
9 * 15May2002 akpm@zip.com.au
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/module.h>
18 #include <linux/kdev_t.h>
19 #include <linux/bio.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>
31 * I/O completion handler for multipage BIOs.
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().
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.
42 static int mpage_end_io_read(struct bio
*bio
, unsigned int bytes_done
, int err
)
44 const int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
45 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
51 struct page
*page
= bvec
->bv_page
;
53 if (--bvec
>= bio
->bi_io_vec
)
54 prefetchw(&bvec
->bv_page
->flags
);
57 SetPageUptodate(page
);
59 ClearPageUptodate(page
);
63 } while (bvec
>= bio
->bi_io_vec
);
68 static int mpage_end_io_write(struct bio
*bio
, unsigned int bytes_done
, int err
)
70 const int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
71 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
77 struct page
*page
= bvec
->bv_page
;
79 if (--bvec
>= bio
->bi_io_vec
)
80 prefetchw(&bvec
->bv_page
->flags
);
85 set_bit(AS_EIO
, &page
->mapping
->flags
);
87 end_page_writeback(page
);
88 } while (bvec
>= bio
->bi_io_vec
);
93 static struct bio
*mpage_bio_submit(int rw
, struct bio
*bio
)
95 bio
->bi_end_io
= mpage_end_io_read
;
97 bio
->bi_end_io
= mpage_end_io_write
;
103 mpage_alloc(struct block_device
*bdev
,
104 sector_t first_sector
, int nr_vecs
,
109 bio
= bio_alloc(gfp_flags
, nr_vecs
);
111 if (bio
== NULL
&& (current
->flags
& PF_MEMALLOC
)) {
112 while (!bio
&& (nr_vecs
/= 2))
113 bio
= bio_alloc(gfp_flags
, nr_vecs
);
118 bio
->bi_sector
= first_sector
;
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
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.
134 map_buffer_to_page(struct page
*page
, struct buffer_head
*bh
, int page_block
)
136 struct inode
*inode
= page
->mapping
->host
;
137 struct buffer_head
*page_bh
, *head
;
140 if (!page_has_buffers(page
)) {
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
145 if (inode
->i_blkbits
== PAGE_CACHE_SHIFT
&&
146 buffer_uptodate(bh
)) {
147 SetPageUptodate(page
);
150 create_empty_buffers(page
, 1 << inode
->i_blkbits
, 0);
152 head
= page_buffers(page
);
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
;
161 page_bh
= page_bh
->b_this_page
;
163 } while (page_bh
!= head
);
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.
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
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
)
180 struct inode
*inode
= page
->mapping
->host
;
181 const unsigned blkbits
= inode
->i_blkbits
;
182 const unsigned blocks_per_page
= PAGE_CACHE_SIZE
>> blkbits
;
183 const unsigned blocksize
= 1 << blkbits
;
184 sector_t block_in_file
;
186 sector_t last_block_in_file
;
187 sector_t blocks
[MAX_BUF_PER_PAGE
];
189 unsigned first_hole
= blocks_per_page
;
190 struct block_device
*bdev
= NULL
;
192 int fully_mapped
= 1;
194 unsigned relative_block
;
196 if (page_has_buffers(page
))
199 block_in_file
= (sector_t
)page
->index
<< (PAGE_CACHE_SHIFT
- blkbits
);
200 last_block
= block_in_file
+ nr_pages
* blocks_per_page
;
201 last_block_in_file
= (i_size_read(inode
) + blocksize
- 1) >> blkbits
;
202 if (last_block
> last_block_in_file
)
203 last_block
= last_block_in_file
;
207 * Map blocks using the result from the previous get_blocks call first.
209 nblocks
= map_bh
->b_size
>> blkbits
;
210 if (buffer_mapped(map_bh
) && block_in_file
> *first_logical_block
&&
211 block_in_file
< (*first_logical_block
+ nblocks
)) {
212 unsigned map_offset
= block_in_file
- *first_logical_block
;
213 unsigned last
= nblocks
- map_offset
;
215 for (relative_block
= 0; ; relative_block
++) {
216 if (relative_block
== last
) {
217 clear_buffer_mapped(map_bh
);
220 if (page_block
== blocks_per_page
)
222 blocks
[page_block
] = map_bh
->b_blocknr
+ map_offset
+
227 bdev
= map_bh
->b_bdev
;
231 * Then do more get_blocks calls until we are done with this page.
233 map_bh
->b_page
= page
;
234 while (page_block
< blocks_per_page
) {
238 if (block_in_file
< last_block
) {
239 map_bh
->b_size
= (last_block
-block_in_file
) << blkbits
;
240 if (get_block(inode
, block_in_file
, map_bh
, 0))
242 *first_logical_block
= block_in_file
;
245 if (!buffer_mapped(map_bh
)) {
247 if (first_hole
== blocks_per_page
)
248 first_hole
= page_block
;
251 clear_buffer_mapped(map_bh
);
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
261 if (buffer_uptodate(map_bh
)) {
262 map_buffer_to_page(page
, map_bh
, page_block
);
266 if (first_hole
!= blocks_per_page
)
267 goto confused
; /* hole -> non-hole */
269 /* Contiguous blocks? */
270 if (page_block
&& blocks
[page_block
-1] != map_bh
->b_blocknr
-1)
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
);
277 } else if (page_block
== blocks_per_page
)
279 blocks
[page_block
] = map_bh
->b_blocknr
+relative_block
;
283 bdev
= map_bh
->b_bdev
;
286 if (first_hole
!= blocks_per_page
) {
287 zero_user_page(page
, first_hole
<< blkbits
,
288 PAGE_CACHE_SIZE
- (first_hole
<< blkbits
),
290 if (first_hole
== 0) {
291 SetPageUptodate(page
);
295 } else if (fully_mapped
) {
296 SetPageMappedToDisk(page
);
300 * This page will go to BIO. Do we need to send this BIO off first?
302 if (bio
&& (*last_block_in_bio
!= blocks
[0] - 1))
303 bio
= mpage_bio_submit(READ
, bio
);
307 bio
= mpage_alloc(bdev
, blocks
[0] << (blkbits
- 9),
308 min_t(int, nr_pages
, bio_get_nr_vecs(bdev
)),
314 length
= first_hole
<< blkbits
;
315 if (bio_add_page(bio
, page
, length
, 0) < length
) {
316 bio
= mpage_bio_submit(READ
, bio
);
320 if (buffer_boundary(map_bh
) || (first_hole
!= blocks_per_page
))
321 bio
= mpage_bio_submit(READ
, bio
);
323 *last_block_in_bio
= blocks
[blocks_per_page
- 1];
329 bio
= mpage_bio_submit(READ
, bio
);
330 if (!PageUptodate(page
))
331 block_read_full_page(page
, get_block
);
338 * mpage_readpages - populate an address space with some pages, and
339 * start reads against them.
341 * @mapping: the address_space
342 * @pages: The address of a list_head which contains the target pages. These
343 * pages have their ->index populated and are otherwise uninitialised.
345 * The page at @pages->prev has the lowest file offset, and reads should be
346 * issued in @pages->prev to @pages->next order.
348 * @nr_pages: The number of pages at *@pages
349 * @get_block: The filesystem's block mapper function.
351 * This function walks the pages and the blocks within each page, building and
352 * emitting large BIOs.
354 * If anything unusual happens, such as:
356 * - encountering a page which has buffers
357 * - encountering a page which has a non-hole after a hole
358 * - encountering a page with non-contiguous blocks
360 * then this code just gives up and calls the buffer_head-based read function.
361 * It does handle a page which has holes at the end - that is a common case:
362 * the end-of-file on blocksize < PAGE_CACHE_SIZE setups.
364 * BH_Boundary explanation:
366 * There is a problem. The mpage read code assembles several pages, gets all
367 * their disk mappings, and then submits them all. That's fine, but obtaining
368 * the disk mappings may require I/O. Reads of indirect blocks, for example.
370 * So an mpage read of the first 16 blocks of an ext2 file will cause I/O to be
371 * submitted in the following order:
372 * 12 0 1 2 3 4 5 6 7 8 9 10 11 13 14 15 16
373 * because the indirect block has to be read to get the mappings of blocks
374 * 13,14,15,16. Obviously, this impacts performance.
376 * So what we do it to allow the filesystem's get_block() function to set
377 * BH_Boundary when it maps block 11. BH_Boundary says: mapping of the block
378 * after this one will require I/O against a block which is probably close to
379 * this one. So you should push what I/O you have currently accumulated.
381 * This all causes the disk requests to be issued in the correct order.
384 mpage_readpages(struct address_space
*mapping
, struct list_head
*pages
,
385 unsigned nr_pages
, get_block_t get_block
)
387 struct bio
*bio
= NULL
;
389 sector_t last_block_in_bio
= 0;
390 struct pagevec lru_pvec
;
391 struct buffer_head map_bh
;
392 unsigned long first_logical_block
= 0;
394 clear_buffer_mapped(&map_bh
);
395 pagevec_init(&lru_pvec
, 0);
396 for (page_idx
= 0; page_idx
< nr_pages
; page_idx
++) {
397 struct page
*page
= list_entry(pages
->prev
, struct page
, lru
);
399 prefetchw(&page
->flags
);
400 list_del(&page
->lru
);
401 if (!add_to_page_cache(page
, mapping
,
402 page
->index
, GFP_KERNEL
)) {
403 bio
= do_mpage_readpage(bio
, page
,
405 &last_block_in_bio
, &map_bh
,
406 &first_logical_block
,
408 if (!pagevec_add(&lru_pvec
, page
))
409 __pagevec_lru_add(&lru_pvec
);
411 page_cache_release(page
);
414 pagevec_lru_add(&lru_pvec
);
415 BUG_ON(!list_empty(pages
));
417 mpage_bio_submit(READ
, bio
);
420 EXPORT_SYMBOL(mpage_readpages
);
423 * This isn't called much at all
425 int mpage_readpage(struct page
*page
, get_block_t get_block
)
427 struct bio
*bio
= NULL
;
428 sector_t last_block_in_bio
= 0;
429 struct buffer_head map_bh
;
430 unsigned long first_logical_block
= 0;
432 clear_buffer_mapped(&map_bh
);
433 bio
= do_mpage_readpage(bio
, page
, 1, &last_block_in_bio
,
434 &map_bh
, &first_logical_block
, get_block
);
436 mpage_bio_submit(READ
, bio
);
439 EXPORT_SYMBOL(mpage_readpage
);
442 * Writing is not so simple.
444 * If the page has buffers then they will be used for obtaining the disk
445 * mapping. We only support pages which are fully mapped-and-dirty, with a
446 * special case for pages which are unmapped at the end: end-of-file.
448 * If the page has no buffers (preferred) then the page is mapped here.
450 * If all blocks are found to be contiguous then the page can go into the
451 * BIO. Otherwise fall back to the mapping's writepage().
453 * FIXME: This code wants an estimate of how many pages are still to be
454 * written, so it can intelligently allocate a suitably-sized BIO. For now,
455 * just allocate full-size (16-page) BIOs.
458 __mpage_writepage(struct bio
*bio
, struct page
*page
, get_block_t get_block
,
459 sector_t
*last_block_in_bio
, int *ret
, struct writeback_control
*wbc
,
460 writepage_t writepage_fn
)
462 struct address_space
*mapping
= page
->mapping
;
463 struct inode
*inode
= page
->mapping
->host
;
464 const unsigned blkbits
= inode
->i_blkbits
;
465 unsigned long end_index
;
466 const unsigned blocks_per_page
= PAGE_CACHE_SIZE
>> blkbits
;
468 sector_t block_in_file
;
469 sector_t blocks
[MAX_BUF_PER_PAGE
];
471 unsigned first_unmapped
= blocks_per_page
;
472 struct block_device
*bdev
= NULL
;
474 sector_t boundary_block
= 0;
475 struct block_device
*boundary_bdev
= NULL
;
477 struct buffer_head map_bh
;
478 loff_t i_size
= i_size_read(inode
);
480 if (page_has_buffers(page
)) {
481 struct buffer_head
*head
= page_buffers(page
);
482 struct buffer_head
*bh
= head
;
484 /* If they're all mapped and dirty, do it */
487 BUG_ON(buffer_locked(bh
));
488 if (!buffer_mapped(bh
)) {
490 * unmapped dirty buffers are created by
491 * __set_page_dirty_buffers -> mmapped data
493 if (buffer_dirty(bh
))
495 if (first_unmapped
== blocks_per_page
)
496 first_unmapped
= page_block
;
500 if (first_unmapped
!= blocks_per_page
)
501 goto confused
; /* hole -> non-hole */
503 if (!buffer_dirty(bh
) || !buffer_uptodate(bh
))
506 if (bh
->b_blocknr
!= blocks
[page_block
-1] + 1)
509 blocks
[page_block
++] = bh
->b_blocknr
;
510 boundary
= buffer_boundary(bh
);
512 boundary_block
= bh
->b_blocknr
;
513 boundary_bdev
= bh
->b_bdev
;
516 } while ((bh
= bh
->b_this_page
) != head
);
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.
531 * The page has no buffers: map it to disk
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
; ) {
540 map_bh
.b_size
= 1 << blkbits
;
541 if (get_block(inode
, block_in_file
, &map_bh
, 1))
543 if (buffer_new(&map_bh
))
544 unmap_underlying_metadata(map_bh
.b_bdev
,
546 if (buffer_boundary(&map_bh
)) {
547 boundary_block
= map_bh
.b_blocknr
;
548 boundary_bdev
= map_bh
.b_bdev
;
551 if (map_bh
.b_blocknr
!= blocks
[page_block
-1] + 1)
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
)
561 BUG_ON(page_block
== 0);
563 first_unmapped
= page_block
;
566 end_index
= i_size
>> PAGE_CACHE_SHIFT
;
567 if (page
->index
>= end_index
) {
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."
576 unsigned offset
= i_size
& (PAGE_CACHE_SIZE
- 1);
578 if (page
->index
> end_index
|| !offset
)
580 zero_user_page(page
, offset
, PAGE_CACHE_SIZE
- offset
,
585 * This page will go to BIO. Do we need to send this BIO off first?
587 if (bio
&& *last_block_in_bio
!= blocks
[0] - 1)
588 bio
= mpage_bio_submit(WRITE
, bio
);
592 bio
= mpage_alloc(bdev
, blocks
[0] << (blkbits
- 9),
593 bio_get_nr_vecs(bdev
), GFP_NOFS
|__GFP_HIGH
);
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)
603 length
= first_unmapped
<< blkbits
;
604 if (bio_add_page(bio
, page
, length
, 0) < length
) {
605 bio
= mpage_bio_submit(WRITE
, bio
);
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.
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;
619 if (buffer_counter
++ == first_unmapped
)
621 clear_buffer_dirty(bh
);
622 bh
= bh
->b_this_page
;
623 } while (bh
!= head
);
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.
630 if (buffer_heads_over_limit
&& PageUptodate(page
))
631 try_to_free_buffers(page
);
634 BUG_ON(PageWriteback(page
));
635 set_page_writeback(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
);
644 *last_block_in_bio
= blocks
[blocks_per_page
- 1];
650 bio
= mpage_bio_submit(WRITE
, bio
);
653 *ret
= (*writepage_fn
)(page
, wbc
);
659 * The caller has a ref on the inode, so *mapping is stable
661 mapping_set_error(mapping
, *ret
);
667 * mpage_writepages - walk the list of dirty pages of the given
668 * address space and writepage() all of them.
670 * @mapping: address space structure to write
671 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
672 * @get_block: the filesystem's block mapper function.
673 * If this is NULL then use a_ops->writepage. Otherwise, go
676 * This is a library function, which implements the writepages()
677 * address_space_operation.
679 * If a page is already under I/O, generic_writepages() skips it, even
680 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
681 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
682 * and msync() need to guarantee that all the data which was dirty at the time
683 * the call was made get new I/O started against them. If wbc->sync_mode is
684 * WB_SYNC_ALL then we were called for data integrity and we must wait for
685 * existing IO to complete.
687 * If you fix this you should check generic_writepages() also!
690 mpage_writepages(struct address_space
*mapping
,
691 struct writeback_control
*wbc
, get_block_t get_block
)
693 struct backing_dev_info
*bdi
= mapping
->backing_dev_info
;
694 struct bio
*bio
= NULL
;
695 sector_t last_block_in_bio
= 0;
698 int (*writepage
)(struct page
*page
, struct writeback_control
*wbc
);
702 pgoff_t end
; /* Inclusive */
706 if (wbc
->nonblocking
&& bdi_write_congested(bdi
)) {
707 wbc
->encountered_congestion
= 1;
712 if (get_block
== NULL
)
713 writepage
= mapping
->a_ops
->writepage
;
715 pagevec_init(&pvec
, 0);
716 if (wbc
->range_cyclic
) {
717 index
= mapping
->writeback_index
; /* Start from prev offset */
720 index
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
721 end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
722 if (wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
)
727 while (!done
&& (index
<= end
) &&
728 (nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
,
730 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
-1) + 1))) {
734 for (i
= 0; i
< nr_pages
; i
++) {
735 struct page
*page
= pvec
.pages
[i
];
738 * At this point we hold neither mapping->tree_lock nor
739 * lock on the page itself: the page may be truncated or
740 * invalidated (changing page->mapping to NULL), or even
741 * swizzled back from swapper_space to tmpfs file
747 if (unlikely(page
->mapping
!= mapping
)) {
752 if (!wbc
->range_cyclic
&& page
->index
> end
) {
758 if (wbc
->sync_mode
!= WB_SYNC_NONE
)
759 wait_on_page_writeback(page
);
761 if (PageWriteback(page
) ||
762 !clear_page_dirty_for_io(page
)) {
768 ret
= (*writepage
)(page
, wbc
);
769 mapping_set_error(mapping
, ret
);
771 bio
= __mpage_writepage(bio
, page
, get_block
,
772 &last_block_in_bio
, &ret
, wbc
,
773 page
->mapping
->a_ops
->writepage
);
775 if (unlikely(ret
== AOP_WRITEPAGE_ACTIVATE
))
777 if (ret
|| (--(wbc
->nr_to_write
) <= 0))
779 if (wbc
->nonblocking
&& bdi_write_congested(bdi
)) {
780 wbc
->encountered_congestion
= 1;
784 pagevec_release(&pvec
);
787 if (!scanned
&& !done
) {
789 * We hit the last page and there is more work to be done: wrap
790 * back to the start of the file
796 if (wbc
->range_cyclic
|| (range_whole
&& wbc
->nr_to_write
> 0))
797 mapping
->writeback_index
= index
;
799 mpage_bio_submit(WRITE
, bio
);
802 EXPORT_SYMBOL(mpage_writepages
);
804 int mpage_writepage(struct page
*page
, get_block_t get_block
,
805 struct writeback_control
*wbc
)
809 sector_t last_block_in_bio
= 0;
811 bio
= __mpage_writepage(NULL
, page
, get_block
,
812 &last_block_in_bio
, &ret
, wbc
, NULL
);
814 mpage_bio_submit(WRITE
, bio
);
818 EXPORT_SYMBOL(mpage_writepage
);