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 do_mpage_readpage(struct bio *bio, struct page *page, unsigned nr_pages,
168 sector_t *last_block_in_bio, get_block_t get_block)
170 struct inode *inode = page->mapping->host;
171 const unsigned blkbits = inode->i_blkbits;
172 const unsigned blocks_per_page = PAGE_CACHE_SIZE >> blkbits;
173 const unsigned blocksize = 1 << blkbits;
174 sector_t block_in_file;
176 sector_t blocks[MAX_BUF_PER_PAGE];
178 unsigned first_hole = blocks_per_page;
179 struct block_device *bdev = NULL;
180 struct buffer_head bh;
182 int fully_mapped = 1;
184 if (page_has_buffers(page))
187 block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits);
188 last_block = (i_size_read(inode) + blocksize - 1) >> blkbits;
191 for (page_block = 0; page_block < blocks_per_page;
192 page_block++, block_in_file++) {
194 if (block_in_file < last_block) {
195 bh.b_size = blocksize;
196 if (get_block(inode, block_in_file, &bh, 0))
200 if (!buffer_mapped(&bh)) {
202 if (first_hole == blocks_per_page)
203 first_hole = page_block;
207 /* some filesystems will copy data into the page during
208 * the get_block call, in which case we don't want to
209 * read it again. map_buffer_to_page copies the data
210 * we just collected from get_block into the page's buffers
211 * so readpage doesn't have to repeat the get_block call
213 if (buffer_uptodate(&bh)) {
214 map_buffer_to_page(page, &bh, page_block);
218 if (first_hole != blocks_per_page)
219 goto confused; /* hole -> non-hole */
221 /* Contiguous blocks? */
222 if (page_block && blocks[page_block-1] != bh.b_blocknr-1)
224 blocks[page_block] = bh.b_blocknr;
228 if (first_hole != blocks_per_page) {
229 char *kaddr = kmap_atomic(page, KM_USER0);
230 memset(kaddr + (first_hole << blkbits), 0,
231 PAGE_CACHE_SIZE - (first_hole << blkbits));
232 flush_dcache_page(page);
233 kunmap_atomic(kaddr, KM_USER0);
234 if (first_hole == 0) {
235 SetPageUptodate(page);
239 } else if (fully_mapped) {
240 SetPageMappedToDisk(page);
244 * This page will go to BIO. Do we need to send this BIO off first?
246 if (bio && (*last_block_in_bio != blocks[0] - 1))
247 bio = mpage_bio_submit(READ, bio);
251 bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
252 min_t(int, nr_pages, bio_get_nr_vecs(bdev)),
258 length = first_hole << blkbits;
259 if (bio_add_page(bio, page, length, 0) < length) {
260 bio = mpage_bio_submit(READ, bio);
264 if (buffer_boundary(&bh) || (first_hole != blocks_per_page))
265 bio = mpage_bio_submit(READ, bio);
267 *last_block_in_bio = blocks[blocks_per_page - 1];
273 bio = mpage_bio_submit(READ, bio);
274 if (!PageUptodate(page))
275 block_read_full_page(page, get_block);
282 * mpage_readpages - populate an address space with some pages, and
283 * start reads against them.
285 * @mapping: the address_space
286 * @pages: The address of a list_head which contains the target pages. These
287 * pages have their ->index populated and are otherwise uninitialised.
289 * The page at @pages->prev has the lowest file offset, and reads should be
290 * issued in @pages->prev to @pages->next order.
292 * @nr_pages: The number of pages at *@pages
293 * @get_block: The filesystem's block mapper function.
295 * This function walks the pages and the blocks within each page, building and
296 * emitting large BIOs.
298 * If anything unusual happens, such as:
300 * - encountering a page which has buffers
301 * - encountering a page which has a non-hole after a hole
302 * - encountering a page with non-contiguous blocks
304 * then this code just gives up and calls the buffer_head-based read function.
305 * It does handle a page which has holes at the end - that is a common case:
306 * the end-of-file on blocksize < PAGE_CACHE_SIZE setups.
308 * BH_Boundary explanation:
310 * There is a problem. The mpage read code assembles several pages, gets all
311 * their disk mappings, and then submits them all. That's fine, but obtaining
312 * the disk mappings may require I/O. Reads of indirect blocks, for example.
314 * So an mpage read of the first 16 blocks of an ext2 file will cause I/O to be
315 * submitted in the following order:
316 * 12 0 1 2 3 4 5 6 7 8 9 10 11 13 14 15 16
317 * because the indirect block has to be read to get the mappings of blocks
318 * 13,14,15,16. Obviously, this impacts performance.
320 * So what we do it to allow the filesystem's get_block() function to set
321 * BH_Boundary when it maps block 11. BH_Boundary says: mapping of the block
322 * after this one will require I/O against a block which is probably close to
323 * this one. So you should push what I/O you have currently accumulated.
325 * This all causes the disk requests to be issued in the correct order.
328 mpage_readpages(struct address_space *mapping, struct list_head *pages,
329 unsigned nr_pages, get_block_t get_block)
331 struct bio *bio = NULL;
333 sector_t last_block_in_bio = 0;
334 struct pagevec lru_pvec;
336 pagevec_init(&lru_pvec, 0);
337 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
338 struct page *page = list_entry(pages->prev, struct page, lru);
340 prefetchw(&page->flags);
341 list_del(&page->lru);
342 if (!add_to_page_cache(page, mapping,
343 page->index, GFP_KERNEL)) {
344 bio = do_mpage_readpage(bio, page,
346 &last_block_in_bio, get_block);
347 if (!pagevec_add(&lru_pvec, page))
348 __pagevec_lru_add(&lru_pvec);
350 page_cache_release(page);
353 pagevec_lru_add(&lru_pvec);
354 BUG_ON(!list_empty(pages));
356 mpage_bio_submit(READ, bio);
359 EXPORT_SYMBOL(mpage_readpages);
362 * This isn't called much at all
364 int mpage_readpage(struct page *page, get_block_t get_block)
366 struct bio *bio = NULL;
367 sector_t last_block_in_bio = 0;
369 bio = do_mpage_readpage(bio, page, 1,
370 &last_block_in_bio, get_block);
372 mpage_bio_submit(READ, bio);
375 EXPORT_SYMBOL(mpage_readpage);
378 * Writing is not so simple.
380 * If the page has buffers then they will be used for obtaining the disk
381 * mapping. We only support pages which are fully mapped-and-dirty, with a
382 * special case for pages which are unmapped at the end: end-of-file.
384 * If the page has no buffers (preferred) then the page is mapped here.
386 * If all blocks are found to be contiguous then the page can go into the
387 * BIO. Otherwise fall back to the mapping's writepage().
389 * FIXME: This code wants an estimate of how many pages are still to be
390 * written, so it can intelligently allocate a suitably-sized BIO. For now,
391 * just allocate full-size (16-page) BIOs.
394 __mpage_writepage(struct bio *bio, struct page *page, get_block_t get_block,
395 sector_t *last_block_in_bio, int *ret, struct writeback_control *wbc,
396 writepage_t writepage_fn)
398 struct address_space *mapping = page->mapping;
399 struct inode *inode = page->mapping->host;
400 const unsigned blkbits = inode->i_blkbits;
401 unsigned long end_index;
402 const unsigned blocks_per_page = PAGE_CACHE_SIZE >> blkbits;
404 sector_t block_in_file;
405 sector_t blocks[MAX_BUF_PER_PAGE];
407 unsigned first_unmapped = blocks_per_page;
408 struct block_device *bdev = NULL;
410 sector_t boundary_block = 0;
411 struct block_device *boundary_bdev = NULL;
413 struct buffer_head map_bh;
414 loff_t i_size = i_size_read(inode);
416 if (page_has_buffers(page)) {
417 struct buffer_head *head = page_buffers(page);
418 struct buffer_head *bh = head;
420 /* If they're all mapped and dirty, do it */
423 BUG_ON(buffer_locked(bh));
424 if (!buffer_mapped(bh)) {
426 * unmapped dirty buffers are created by
427 * __set_page_dirty_buffers -> mmapped data
429 if (buffer_dirty(bh))
431 if (first_unmapped == blocks_per_page)
432 first_unmapped = page_block;
436 if (first_unmapped != blocks_per_page)
437 goto confused; /* hole -> non-hole */
439 if (!buffer_dirty(bh) || !buffer_uptodate(bh))
442 if (bh->b_blocknr != blocks[page_block-1] + 1)
445 blocks[page_block++] = bh->b_blocknr;
446 boundary = buffer_boundary(bh);
448 boundary_block = bh->b_blocknr;
449 boundary_bdev = bh->b_bdev;
452 } while ((bh = bh->b_this_page) != head);
458 * Page has buffers, but they are all unmapped. The page was
459 * created by pagein or read over a hole which was handled by
460 * block_read_full_page(). If this address_space is also
461 * using mpage_readpages then this can rarely happen.
467 * The page has no buffers: map it to disk
469 BUG_ON(!PageUptodate(page));
470 block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits);
471 last_block = (i_size - 1) >> blkbits;
472 map_bh.b_page = page;
473 for (page_block = 0; page_block < blocks_per_page; ) {
476 map_bh.b_size = 1 << blkbits;
477 if (get_block(inode, block_in_file, &map_bh, 1))
479 if (buffer_new(&map_bh))
480 unmap_underlying_metadata(map_bh.b_bdev,
482 if (buffer_boundary(&map_bh)) {
483 boundary_block = map_bh.b_blocknr;
484 boundary_bdev = map_bh.b_bdev;
487 if (map_bh.b_blocknr != blocks[page_block-1] + 1)
490 blocks[page_block++] = map_bh.b_blocknr;
491 boundary = buffer_boundary(&map_bh);
492 bdev = map_bh.b_bdev;
493 if (block_in_file == last_block)
497 BUG_ON(page_block == 0);
499 first_unmapped = page_block;
502 end_index = i_size >> PAGE_CACHE_SHIFT;
503 if (page->index >= end_index) {
505 * The page straddles i_size. It must be zeroed out on each
506 * and every writepage invokation because it may be mmapped.
507 * "A file is mapped in multiples of the page size. For a file
508 * that is not a multiple of the page size, the remaining memory
509 * is zeroed when mapped, and writes to that region are not
510 * written out to the file."
512 unsigned offset = i_size & (PAGE_CACHE_SIZE - 1);
515 if (page->index > end_index || !offset)
517 kaddr = kmap_atomic(page, KM_USER0);
518 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
519 flush_dcache_page(page);
520 kunmap_atomic(kaddr, KM_USER0);
524 * This page will go to BIO. Do we need to send this BIO off first?
526 if (bio && *last_block_in_bio != blocks[0] - 1)
527 bio = mpage_bio_submit(WRITE, bio);
531 bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
532 bio_get_nr_vecs(bdev), GFP_NOFS|__GFP_HIGH);
538 * Must try to add the page before marking the buffer clean or
539 * the confused fail path above (OOM) will be very confused when
540 * it finds all bh marked clean (i.e. it will not write anything)
542 length = first_unmapped << blkbits;
543 if (bio_add_page(bio, page, length, 0) < length) {
544 bio = mpage_bio_submit(WRITE, bio);
549 * OK, we have our BIO, so we can now mark the buffers clean. Make
550 * sure to only clean buffers which we know we'll be writing.
552 if (page_has_buffers(page)) {
553 struct buffer_head *head = page_buffers(page);
554 struct buffer_head *bh = head;
555 unsigned buffer_counter = 0;
558 if (buffer_counter++ == first_unmapped)
560 clear_buffer_dirty(bh);
561 bh = bh->b_this_page;
562 } while (bh != head);
565 * we cannot drop the bh if the page is not uptodate
566 * or a concurrent readpage would fail to serialize with the bh
567 * and it would read from disk before we reach the platter.
569 if (buffer_heads_over_limit && PageUptodate(page))
570 try_to_free_buffers(page);
573 BUG_ON(PageWriteback(page));
574 set_page_writeback(page);
576 if (boundary || (first_unmapped != blocks_per_page)) {
577 bio = mpage_bio_submit(WRITE, bio);
578 if (boundary_block) {
579 write_boundary_block(boundary_bdev,
580 boundary_block, 1 << blkbits);
583 *last_block_in_bio = blocks[blocks_per_page - 1];
589 bio = mpage_bio_submit(WRITE, bio);
592 *ret = (*writepage_fn)(page, wbc);
598 * The caller has a ref on the inode, so *mapping is stable
602 set_bit(AS_ENOSPC, &mapping->flags);
604 set_bit(AS_EIO, &mapping->flags);
611 * mpage_writepages - walk the list of dirty pages of the given
612 * address space and writepage() all of them.
614 * @mapping: address space structure to write
615 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
616 * @get_block: the filesystem's block mapper function.
617 * If this is NULL then use a_ops->writepage. Otherwise, go
620 * This is a library function, which implements the writepages()
621 * address_space_operation.
623 * If a page is already under I/O, generic_writepages() skips it, even
624 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
625 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
626 * and msync() need to guarantee that all the data which was dirty at the time
627 * the call was made get new I/O started against them. If wbc->sync_mode is
628 * WB_SYNC_ALL then we were called for data integrity and we must wait for
629 * existing IO to complete.
632 mpage_writepages(struct address_space *mapping,
633 struct writeback_control *wbc, get_block_t get_block)
635 struct backing_dev_info *bdi = mapping->backing_dev_info;
636 struct bio *bio = NULL;
637 sector_t last_block_in_bio = 0;
640 int (*writepage)(struct page *page, struct writeback_control *wbc);
644 pgoff_t end = -1; /* Inclusive */
648 if (wbc->nonblocking && bdi_write_congested(bdi)) {
649 wbc->encountered_congestion = 1;
654 if (get_block == NULL)
655 writepage = mapping->a_ops->writepage;
657 pagevec_init(&pvec, 0);
658 if (wbc->sync_mode == WB_SYNC_NONE) {
659 index = mapping->writeback_index; /* Start from prev offset */
661 index = 0; /* whole-file sweep */
664 if (wbc->start || wbc->end) {
665 index = wbc->start >> PAGE_CACHE_SHIFT;
666 end = wbc->end >> PAGE_CACHE_SHIFT;
671 while (!done && (index <= end) &&
672 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
674 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
678 for (i = 0; i < nr_pages; i++) {
679 struct page *page = pvec.pages[i];
682 * At this point we hold neither mapping->tree_lock nor
683 * lock on the page itself: the page may be truncated or
684 * invalidated (changing page->mapping to NULL), or even
685 * swizzled back from swapper_space to tmpfs file
691 if (unlikely(page->mapping != mapping)) {
696 if (unlikely(is_range) && page->index > end) {
702 if (wbc->sync_mode != WB_SYNC_NONE)
703 wait_on_page_writeback(page);
705 if (PageWriteback(page) ||
706 !clear_page_dirty_for_io(page)) {
712 ret = (*writepage)(page, wbc);
722 bio = __mpage_writepage(bio, page, get_block,
723 &last_block_in_bio, &ret, wbc,
724 page->mapping->a_ops->writepage);
726 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE))
728 if (ret || (--(wbc->nr_to_write) <= 0))
730 if (wbc->nonblocking && bdi_write_congested(bdi)) {
731 wbc->encountered_congestion = 1;
735 pagevec_release(&pvec);
738 if (!scanned && !done) {
740 * We hit the last page and there is more work to be done: wrap
741 * back to the start of the file
748 mapping->writeback_index = index;
750 mpage_bio_submit(WRITE, bio);
753 EXPORT_SYMBOL(mpage_writepages);
755 int mpage_writepage(struct page *page, get_block_t get_block,
756 struct writeback_control *wbc)
760 sector_t last_block_in_bio = 0;
762 bio = __mpage_writepage(NULL, page, get_block,
763 &last_block_in_bio, &ret, wbc, NULL);
765 mpage_bio_submit(WRITE, bio);
769 EXPORT_SYMBOL(mpage_writepage);