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);
84 end_page_writeback(page);
85 } while (bvec >= bio->bi_io_vec);
90 static struct bio *mpage_bio_submit(int rw, struct bio *bio)
92 bio->bi_end_io = mpage_end_io_read;
94 bio->bi_end_io = mpage_end_io_write;
100 mpage_alloc(struct block_device *bdev,
101 sector_t first_sector, int nr_vecs,
102 unsigned int __nocast gfp_flags)
106 bio = bio_alloc(gfp_flags, nr_vecs);
108 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
109 while (!bio && (nr_vecs /= 2))
110 bio = bio_alloc(gfp_flags, nr_vecs);
115 bio->bi_sector = first_sector;
121 * support function for mpage_readpages. The fs supplied get_block might
122 * return an up to date buffer. This is used to map that buffer into
123 * the page, which allows readpage to avoid triggering a duplicate call
126 * The idea is to avoid adding buffers to pages that don't already have
127 * them. So when the buffer is up to date and the page size == block size,
128 * this marks the page up to date instead of adding new buffers.
131 map_buffer_to_page(struct page *page, struct buffer_head *bh, int page_block)
133 struct inode *inode = page->mapping->host;
134 struct buffer_head *page_bh, *head;
137 if (!page_has_buffers(page)) {
139 * don't make any buffers if there is only one buffer on
140 * the page and the page just needs to be set up to date
142 if (inode->i_blkbits == PAGE_CACHE_SHIFT &&
143 buffer_uptodate(bh)) {
144 SetPageUptodate(page);
147 create_empty_buffers(page, 1 << inode->i_blkbits, 0);
149 head = page_buffers(page);
152 if (block == page_block) {
153 page_bh->b_state = bh->b_state;
154 page_bh->b_bdev = bh->b_bdev;
155 page_bh->b_blocknr = bh->b_blocknr;
158 page_bh = page_bh->b_this_page;
160 } while (page_bh != head);
164 do_mpage_readpage(struct bio *bio, struct page *page, unsigned nr_pages,
165 sector_t *last_block_in_bio, get_block_t get_block)
167 struct inode *inode = page->mapping->host;
168 const unsigned blkbits = inode->i_blkbits;
169 const unsigned blocks_per_page = PAGE_CACHE_SIZE >> blkbits;
170 const unsigned blocksize = 1 << blkbits;
171 sector_t block_in_file;
173 sector_t blocks[MAX_BUF_PER_PAGE];
175 unsigned first_hole = blocks_per_page;
176 struct block_device *bdev = NULL;
177 struct buffer_head bh;
179 int fully_mapped = 1;
181 if (page_has_buffers(page))
184 block_in_file = page->index << (PAGE_CACHE_SHIFT - blkbits);
185 last_block = (i_size_read(inode) + blocksize - 1) >> blkbits;
188 for (page_block = 0; page_block < blocks_per_page;
189 page_block++, block_in_file++) {
191 if (block_in_file < last_block) {
192 if (get_block(inode, block_in_file, &bh, 0))
196 if (!buffer_mapped(&bh)) {
198 if (first_hole == blocks_per_page)
199 first_hole = page_block;
203 /* some filesystems will copy data into the page during
204 * the get_block call, in which case we don't want to
205 * read it again. map_buffer_to_page copies the data
206 * we just collected from get_block into the page's buffers
207 * so readpage doesn't have to repeat the get_block call
209 if (buffer_uptodate(&bh)) {
210 map_buffer_to_page(page, &bh, page_block);
214 if (first_hole != blocks_per_page)
215 goto confused; /* hole -> non-hole */
217 /* Contiguous blocks? */
218 if (page_block && blocks[page_block-1] != bh.b_blocknr-1)
220 blocks[page_block] = bh.b_blocknr;
224 if (first_hole != blocks_per_page) {
225 char *kaddr = kmap_atomic(page, KM_USER0);
226 memset(kaddr + (first_hole << blkbits), 0,
227 PAGE_CACHE_SIZE - (first_hole << blkbits));
228 flush_dcache_page(page);
229 kunmap_atomic(kaddr, KM_USER0);
230 if (first_hole == 0) {
231 SetPageUptodate(page);
235 } else if (fully_mapped) {
236 SetPageMappedToDisk(page);
240 * This page will go to BIO. Do we need to send this BIO off first?
242 if (bio && (*last_block_in_bio != blocks[0] - 1))
243 bio = mpage_bio_submit(READ, bio);
247 bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
248 min_t(int, nr_pages, bio_get_nr_vecs(bdev)),
254 length = first_hole << blkbits;
255 if (bio_add_page(bio, page, length, 0) < length) {
256 bio = mpage_bio_submit(READ, bio);
260 if (buffer_boundary(&bh) || (first_hole != blocks_per_page))
261 bio = mpage_bio_submit(READ, bio);
263 *last_block_in_bio = blocks[blocks_per_page - 1];
269 bio = mpage_bio_submit(READ, bio);
270 if (!PageUptodate(page))
271 block_read_full_page(page, get_block);
278 * mpage_readpages - populate an address space with some pages, and
279 * start reads against them.
281 * @mapping: the address_space
282 * @pages: The address of a list_head which contains the target pages. These
283 * pages have their ->index populated and are otherwise uninitialised.
285 * The page at @pages->prev has the lowest file offset, and reads should be
286 * issued in @pages->prev to @pages->next order.
288 * @nr_pages: The number of pages at *@pages
289 * @get_block: The filesystem's block mapper function.
291 * This function walks the pages and the blocks within each page, building and
292 * emitting large BIOs.
294 * If anything unusual happens, such as:
296 * - encountering a page which has buffers
297 * - encountering a page which has a non-hole after a hole
298 * - encountering a page with non-contiguous blocks
300 * then this code just gives up and calls the buffer_head-based read function.
301 * It does handle a page which has holes at the end - that is a common case:
302 * the end-of-file on blocksize < PAGE_CACHE_SIZE setups.
304 * BH_Boundary explanation:
306 * There is a problem. The mpage read code assembles several pages, gets all
307 * their disk mappings, and then submits them all. That's fine, but obtaining
308 * the disk mappings may require I/O. Reads of indirect blocks, for example.
310 * So an mpage read of the first 16 blocks of an ext2 file will cause I/O to be
311 * submitted in the following order:
312 * 12 0 1 2 3 4 5 6 7 8 9 10 11 13 14 15 16
313 * because the indirect block has to be read to get the mappings of blocks
314 * 13,14,15,16. Obviously, this impacts performance.
316 * So what we do it to allow the filesystem's get_block() function to set
317 * BH_Boundary when it maps block 11. BH_Boundary says: mapping of the block
318 * after this one will require I/O against a block which is probably close to
319 * this one. So you should push what I/O you have currently accumulated.
321 * This all causes the disk requests to be issued in the correct order.
324 mpage_readpages(struct address_space *mapping, struct list_head *pages,
325 unsigned nr_pages, get_block_t get_block)
327 struct bio *bio = NULL;
329 sector_t last_block_in_bio = 0;
330 struct pagevec lru_pvec;
332 pagevec_init(&lru_pvec, 0);
333 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
334 struct page *page = list_entry(pages->prev, struct page, lru);
336 prefetchw(&page->flags);
337 list_del(&page->lru);
338 if (!add_to_page_cache(page, mapping,
339 page->index, GFP_KERNEL)) {
340 bio = do_mpage_readpage(bio, page,
342 &last_block_in_bio, get_block);
343 if (!pagevec_add(&lru_pvec, page))
344 __pagevec_lru_add(&lru_pvec);
346 page_cache_release(page);
349 pagevec_lru_add(&lru_pvec);
350 BUG_ON(!list_empty(pages));
352 mpage_bio_submit(READ, bio);
355 EXPORT_SYMBOL(mpage_readpages);
358 * This isn't called much at all
360 int mpage_readpage(struct page *page, get_block_t get_block)
362 struct bio *bio = NULL;
363 sector_t last_block_in_bio = 0;
365 bio = do_mpage_readpage(bio, page, 1,
366 &last_block_in_bio, get_block);
368 mpage_bio_submit(READ, bio);
371 EXPORT_SYMBOL(mpage_readpage);
374 * Writing is not so simple.
376 * If the page has buffers then they will be used for obtaining the disk
377 * mapping. We only support pages which are fully mapped-and-dirty, with a
378 * special case for pages which are unmapped at the end: end-of-file.
380 * If the page has no buffers (preferred) then the page is mapped here.
382 * If all blocks are found to be contiguous then the page can go into the
383 * BIO. Otherwise fall back to the mapping's writepage().
385 * FIXME: This code wants an estimate of how many pages are still to be
386 * written, so it can intelligently allocate a suitably-sized BIO. For now,
387 * just allocate full-size (16-page) BIOs.
390 __mpage_writepage(struct bio *bio, struct page *page, get_block_t get_block,
391 sector_t *last_block_in_bio, int *ret, struct writeback_control *wbc,
392 writepage_t writepage_fn)
394 struct address_space *mapping = page->mapping;
395 struct inode *inode = page->mapping->host;
396 const unsigned blkbits = inode->i_blkbits;
397 unsigned long end_index;
398 const unsigned blocks_per_page = PAGE_CACHE_SIZE >> blkbits;
400 sector_t block_in_file;
401 sector_t blocks[MAX_BUF_PER_PAGE];
403 unsigned first_unmapped = blocks_per_page;
404 struct block_device *bdev = NULL;
406 sector_t boundary_block = 0;
407 struct block_device *boundary_bdev = NULL;
409 struct buffer_head map_bh;
410 loff_t i_size = i_size_read(inode);
412 if (page_has_buffers(page)) {
413 struct buffer_head *head = page_buffers(page);
414 struct buffer_head *bh = head;
416 /* If they're all mapped and dirty, do it */
419 BUG_ON(buffer_locked(bh));
420 if (!buffer_mapped(bh)) {
422 * unmapped dirty buffers are created by
423 * __set_page_dirty_buffers -> mmapped data
425 if (buffer_dirty(bh))
427 if (first_unmapped == blocks_per_page)
428 first_unmapped = page_block;
432 if (first_unmapped != blocks_per_page)
433 goto confused; /* hole -> non-hole */
435 if (!buffer_dirty(bh) || !buffer_uptodate(bh))
438 if (bh->b_blocknr != blocks[page_block-1] + 1)
441 blocks[page_block++] = bh->b_blocknr;
442 boundary = buffer_boundary(bh);
444 boundary_block = bh->b_blocknr;
445 boundary_bdev = bh->b_bdev;
448 } while ((bh = bh->b_this_page) != head);
454 * Page has buffers, but they are all unmapped. The page was
455 * created by pagein or read over a hole which was handled by
456 * block_read_full_page(). If this address_space is also
457 * using mpage_readpages then this can rarely happen.
463 * The page has no buffers: map it to disk
465 BUG_ON(!PageUptodate(page));
466 block_in_file = page->index << (PAGE_CACHE_SHIFT - blkbits);
467 last_block = (i_size - 1) >> blkbits;
468 map_bh.b_page = page;
469 for (page_block = 0; page_block < blocks_per_page; ) {
472 if (get_block(inode, block_in_file, &map_bh, 1))
474 if (buffer_new(&map_bh))
475 unmap_underlying_metadata(map_bh.b_bdev,
477 if (buffer_boundary(&map_bh)) {
478 boundary_block = map_bh.b_blocknr;
479 boundary_bdev = map_bh.b_bdev;
482 if (map_bh.b_blocknr != blocks[page_block-1] + 1)
485 blocks[page_block++] = map_bh.b_blocknr;
486 boundary = buffer_boundary(&map_bh);
487 bdev = map_bh.b_bdev;
488 if (block_in_file == last_block)
492 BUG_ON(page_block == 0);
494 first_unmapped = page_block;
497 end_index = i_size >> PAGE_CACHE_SHIFT;
498 if (page->index >= end_index) {
500 * The page straddles i_size. It must be zeroed out on each
501 * and every writepage invokation because it may be mmapped.
502 * "A file is mapped in multiples of the page size. For a file
503 * that is not a multiple of the page size, the remaining memory
504 * is zeroed when mapped, and writes to that region are not
505 * written out to the file."
507 unsigned offset = i_size & (PAGE_CACHE_SIZE - 1);
510 if (page->index > end_index || !offset)
512 kaddr = kmap_atomic(page, KM_USER0);
513 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
514 flush_dcache_page(page);
515 kunmap_atomic(kaddr, KM_USER0);
519 * This page will go to BIO. Do we need to send this BIO off first?
521 if (bio && *last_block_in_bio != blocks[0] - 1)
522 bio = mpage_bio_submit(WRITE, bio);
526 bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
527 bio_get_nr_vecs(bdev), GFP_NOFS|__GFP_HIGH);
533 * Must try to add the page before marking the buffer clean or
534 * the confused fail path above (OOM) will be very confused when
535 * it finds all bh marked clean (i.e. it will not write anything)
537 length = first_unmapped << blkbits;
538 if (bio_add_page(bio, page, length, 0) < length) {
539 bio = mpage_bio_submit(WRITE, bio);
544 * OK, we have our BIO, so we can now mark the buffers clean. Make
545 * sure to only clean buffers which we know we'll be writing.
547 if (page_has_buffers(page)) {
548 struct buffer_head *head = page_buffers(page);
549 struct buffer_head *bh = head;
550 unsigned buffer_counter = 0;
553 if (buffer_counter++ == first_unmapped)
555 clear_buffer_dirty(bh);
556 bh = bh->b_this_page;
557 } while (bh != head);
560 * we cannot drop the bh if the page is not uptodate
561 * or a concurrent readpage would fail to serialize with the bh
562 * and it would read from disk before we reach the platter.
564 if (buffer_heads_over_limit && PageUptodate(page))
565 try_to_free_buffers(page);
568 BUG_ON(PageWriteback(page));
569 set_page_writeback(page);
571 if (boundary || (first_unmapped != blocks_per_page)) {
572 bio = mpage_bio_submit(WRITE, bio);
573 if (boundary_block) {
574 write_boundary_block(boundary_bdev,
575 boundary_block, 1 << blkbits);
578 *last_block_in_bio = blocks[blocks_per_page - 1];
584 bio = mpage_bio_submit(WRITE, bio);
587 *ret = (*writepage_fn)(page, wbc);
593 * The caller has a ref on the inode, so *mapping is stable
597 set_bit(AS_ENOSPC, &mapping->flags);
599 set_bit(AS_EIO, &mapping->flags);
606 * mpage_writepages - walk the list of dirty pages of the given
607 * address space and writepage() all of them.
609 * @mapping: address space structure to write
610 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
611 * @get_block: the filesystem's block mapper function.
612 * If this is NULL then use a_ops->writepage. Otherwise, go
615 * This is a library function, which implements the writepages()
616 * address_space_operation.
618 * If a page is already under I/O, generic_writepages() skips it, even
619 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
620 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
621 * and msync() need to guarantee that all the data which was dirty at the time
622 * the call was made get new I/O started against them. If wbc->sync_mode is
623 * WB_SYNC_ALL then we were called for data integrity and we must wait for
624 * existing IO to complete.
627 mpage_writepages(struct address_space *mapping,
628 struct writeback_control *wbc, get_block_t get_block)
630 struct backing_dev_info *bdi = mapping->backing_dev_info;
631 struct bio *bio = NULL;
632 sector_t last_block_in_bio = 0;
635 int (*writepage)(struct page *page, struct writeback_control *wbc);
639 pgoff_t end = -1; /* Inclusive */
643 if (wbc->nonblocking && bdi_write_congested(bdi)) {
644 wbc->encountered_congestion = 1;
649 if (get_block == NULL)
650 writepage = mapping->a_ops->writepage;
652 pagevec_init(&pvec, 0);
653 if (wbc->sync_mode == WB_SYNC_NONE) {
654 index = mapping->writeback_index; /* Start from prev offset */
656 index = 0; /* whole-file sweep */
659 if (wbc->start || wbc->end) {
660 index = wbc->start >> PAGE_CACHE_SHIFT;
661 end = wbc->end >> PAGE_CACHE_SHIFT;
666 while (!done && (index <= end) &&
667 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
669 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
673 for (i = 0; i < nr_pages; i++) {
674 struct page *page = pvec.pages[i];
677 * At this point we hold neither mapping->tree_lock nor
678 * lock on the page itself: the page may be truncated or
679 * invalidated (changing page->mapping to NULL), or even
680 * swizzled back from swapper_space to tmpfs file
686 if (unlikely(page->mapping != mapping)) {
691 if (unlikely(is_range) && page->index > end) {
697 if (wbc->sync_mode != WB_SYNC_NONE)
698 wait_on_page_writeback(page);
700 if (PageWriteback(page) ||
701 !clear_page_dirty_for_io(page)) {
707 ret = (*writepage)(page, wbc);
717 bio = __mpage_writepage(bio, page, get_block,
718 &last_block_in_bio, &ret, wbc,
719 page->mapping->a_ops->writepage);
721 if (unlikely(ret == WRITEPAGE_ACTIVATE))
723 if (ret || (--(wbc->nr_to_write) <= 0))
725 if (wbc->nonblocking && bdi_write_congested(bdi)) {
726 wbc->encountered_congestion = 1;
730 pagevec_release(&pvec);
733 if (!scanned && !done) {
735 * We hit the last page and there is more work to be done: wrap
736 * back to the start of the file
743 mapping->writeback_index = index;
745 mpage_bio_submit(WRITE, bio);
748 EXPORT_SYMBOL(mpage_writepages);
750 int mpage_writepage(struct page *page, get_block_t get_block,
751 struct writeback_control *wbc)
755 sector_t last_block_in_bio = 0;
757 bio = __mpage_writepage(NULL, page, get_block,
758 &last_block_in_bio, &ret, wbc, NULL);
760 mpage_bio_submit(WRITE, bio);
764 EXPORT_SYMBOL(mpage_writepage);