[PATCH] V4L: follow changes in saa7146
[linux-2.6/history.git] / fs / mpage.c
blob4bbf15ee91fe01bcaa529f7e74dbd0e1bb702a36
1 /*
2 * fs/mpage.c
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
10 * Initial version
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>
17 #include <linux/mm.h>
18 #include <linux/kdev_t.h>
19 #include <linux/bio.h>
20 #include <linux/fs.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;
47 if (bio->bi_size)
48 return 1;
50 do {
51 struct page *page = bvec->bv_page;
53 if (--bvec >= bio->bi_io_vec)
54 prefetchw(&bvec->bv_page->flags);
56 if (uptodate) {
57 SetPageUptodate(page);
58 } else {
59 ClearPageUptodate(page);
60 SetPageError(page);
62 unlock_page(page);
63 } while (bvec >= bio->bi_io_vec);
64 bio_put(bio);
65 return 0;
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;
73 if (bio->bi_size)
74 return 1;
76 do {
77 struct page *page = bvec->bv_page;
79 if (--bvec >= bio->bi_io_vec)
80 prefetchw(&bvec->bv_page->flags);
82 if (!uptodate)
83 SetPageError(page);
84 end_page_writeback(page);
85 } while (bvec >= bio->bi_io_vec);
86 bio_put(bio);
87 return 0;
90 struct bio *mpage_bio_submit(int rw, struct bio *bio)
92 bio->bi_end_io = mpage_end_io_read;
93 if (rw == WRITE)
94 bio->bi_end_io = mpage_end_io_write;
95 submit_bio(rw, bio);
96 return NULL;
99 static struct bio *
100 mpage_alloc(struct block_device *bdev,
101 sector_t first_sector, int nr_vecs, int gfp_flags)
103 struct bio *bio;
105 bio = bio_alloc(gfp_flags, nr_vecs);
107 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
108 while (!bio && (nr_vecs /= 2))
109 bio = bio_alloc(gfp_flags, nr_vecs);
112 if (bio) {
113 bio->bi_bdev = bdev;
114 bio->bi_sector = first_sector;
116 return bio;
120 * support function for mpage_readpages. The fs supplied get_block might
121 * return an up to date buffer. This is used to map that buffer into
122 * the page, which allows readpage to avoid triggering a duplicate call
123 * to get_block.
125 * The idea is to avoid adding buffers to pages that don't already have
126 * them. So when the buffer is up to date and the page size == block size,
127 * this marks the page up to date instead of adding new buffers.
129 static void
130 map_buffer_to_page(struct page *page, struct buffer_head *bh, int page_block)
132 struct inode *inode = page->mapping->host;
133 struct buffer_head *page_bh, *head;
134 int block = 0;
136 if (!page_has_buffers(page)) {
138 * don't make any buffers if there is only one buffer on
139 * the page and the page just needs to be set up to date
141 if (inode->i_blkbits == PAGE_CACHE_SHIFT &&
142 buffer_uptodate(bh)) {
143 SetPageUptodate(page);
144 return;
146 create_empty_buffers(page, 1 << inode->i_blkbits, 0);
148 head = page_buffers(page);
149 page_bh = head;
150 do {
151 if (block == page_block) {
152 page_bh->b_state = bh->b_state;
153 page_bh->b_bdev = bh->b_bdev;
154 page_bh->b_blocknr = bh->b_blocknr;
155 break;
157 page_bh = page_bh->b_this_page;
158 block++;
159 } while (page_bh != head);
163 * mpage_readpages - populate an address space with some pages, and
164 * start reads against them.
166 * @mapping: the address_space
167 * @pages: The address of a list_head which contains the target pages. These
168 * pages have their ->index populated and are otherwise uninitialised.
170 * The page at @pages->prev has the lowest file offset, and reads should be
171 * issued in @pages->prev to @pages->next order.
173 * @nr_pages: The number of pages at *@pages
174 * @get_block: The filesystem's block mapper function.
176 * This function walks the pages and the blocks within each page, building and
177 * emitting large BIOs.
179 * If anything unusual happens, such as:
181 * - encountering a page which has buffers
182 * - encountering a page which has a non-hole after a hole
183 * - encountering a page with non-contiguous blocks
185 * then this code just gives up and calls the buffer_head-based read function.
186 * It does handle a page which has holes at the end - that is a common case:
187 * the end-of-file on blocksize < PAGE_CACHE_SIZE setups.
189 * BH_Boundary explanation:
191 * There is a problem. The mpage read code assembles several pages, gets all
192 * their disk mappings, and then submits them all. That's fine, but obtaining
193 * the disk mappings may require I/O. Reads of indirect blocks, for example.
195 * So an mpage read of the first 16 blocks of an ext2 file will cause I/O to be
196 * submitted in the following order:
197 * 12 0 1 2 3 4 5 6 7 8 9 10 11 13 14 15 16
198 * because the indirect block has to be read to get the mappings of blocks
199 * 13,14,15,16. Obviously, this impacts performance.
201 * So what we do it to allow the filesystem's get_block() function to set
202 * BH_Boundary when it maps block 11. BH_Boundary says: mapping of the block
203 * after this one will require I/O against a block which is probably close to
204 * this one. So you should push what I/O you have currently accumulated.
206 * This all causes the disk requests to be issued in the correct order.
208 static struct bio *
209 do_mpage_readpage(struct bio *bio, struct page *page, unsigned nr_pages,
210 sector_t *last_block_in_bio, get_block_t get_block)
212 struct inode *inode = page->mapping->host;
213 const unsigned blkbits = inode->i_blkbits;
214 const unsigned blocks_per_page = PAGE_CACHE_SIZE >> blkbits;
215 const unsigned blocksize = 1 << blkbits;
216 sector_t block_in_file;
217 sector_t last_block;
218 sector_t blocks[MAX_BUF_PER_PAGE];
219 unsigned page_block;
220 unsigned first_hole = blocks_per_page;
221 struct block_device *bdev = NULL;
222 struct buffer_head bh;
223 int length;
224 int fully_mapped = 1;
226 if (page_has_buffers(page))
227 goto confused;
229 block_in_file = page->index << (PAGE_CACHE_SHIFT - blkbits);
230 last_block = (i_size_read(inode) + blocksize - 1) >> blkbits;
232 bh.b_page = page;
233 for (page_block = 0; page_block < blocks_per_page;
234 page_block++, block_in_file++) {
235 bh.b_state = 0;
236 if (block_in_file < last_block) {
237 if (get_block(inode, block_in_file, &bh, 0))
238 goto confused;
241 if (!buffer_mapped(&bh)) {
242 fully_mapped = 0;
243 if (first_hole == blocks_per_page)
244 first_hole = page_block;
245 continue;
248 /* some filesystems will copy data into the page during
249 * the get_block call, in which case we don't want to
250 * read it again. map_buffer_to_page copies the data
251 * we just collected from get_block into the page's buffers
252 * so readpage doesn't have to repeat the get_block call
254 if (buffer_uptodate(&bh)) {
255 map_buffer_to_page(page, &bh, page_block);
256 goto confused;
259 if (first_hole != blocks_per_page)
260 goto confused; /* hole -> non-hole */
262 /* Contiguous blocks? */
263 if (page_block && blocks[page_block-1] != bh.b_blocknr-1)
264 goto confused;
265 blocks[page_block] = bh.b_blocknr;
266 bdev = bh.b_bdev;
269 if (first_hole != blocks_per_page) {
270 char *kaddr = kmap_atomic(page, KM_USER0);
271 memset(kaddr + (first_hole << blkbits), 0,
272 PAGE_CACHE_SIZE - (first_hole << blkbits));
273 flush_dcache_page(page);
274 kunmap_atomic(kaddr, KM_USER0);
275 if (first_hole == 0) {
276 SetPageUptodate(page);
277 unlock_page(page);
278 goto out;
280 } else if (fully_mapped) {
281 SetPageMappedToDisk(page);
285 * This page will go to BIO. Do we need to send this BIO off first?
287 if (bio && (*last_block_in_bio != blocks[0] - 1))
288 bio = mpage_bio_submit(READ, bio);
290 alloc_new:
291 if (bio == NULL) {
292 bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
293 min_t(int, nr_pages, bio_get_nr_vecs(bdev)),
294 GFP_KERNEL);
295 if (bio == NULL)
296 goto confused;
299 length = first_hole << blkbits;
300 if (bio_add_page(bio, page, length, 0) < length) {
301 bio = mpage_bio_submit(READ, bio);
302 goto alloc_new;
305 if (buffer_boundary(&bh) || (first_hole != blocks_per_page))
306 bio = mpage_bio_submit(READ, bio);
307 else
308 *last_block_in_bio = blocks[blocks_per_page - 1];
309 out:
310 return bio;
312 confused:
313 if (bio)
314 bio = mpage_bio_submit(READ, bio);
315 if (!PageUptodate(page))
316 block_read_full_page(page, get_block);
317 else
318 unlock_page(page);
319 goto out;
323 mpage_readpages(struct address_space *mapping, struct list_head *pages,
324 unsigned nr_pages, get_block_t get_block)
326 struct bio *bio = NULL;
327 unsigned page_idx;
328 sector_t last_block_in_bio = 0;
329 struct pagevec lru_pvec;
331 pagevec_init(&lru_pvec, 0);
332 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
333 struct page *page = list_entry(pages->prev, struct page, lru);
335 prefetchw(&page->flags);
336 list_del(&page->lru);
337 if (!add_to_page_cache(page, mapping,
338 page->index, GFP_KERNEL)) {
339 bio = do_mpage_readpage(bio, page,
340 nr_pages - page_idx,
341 &last_block_in_bio, get_block);
342 if (!pagevec_add(&lru_pvec, page))
343 __pagevec_lru_add(&lru_pvec);
344 } else {
345 page_cache_release(page);
348 pagevec_lru_add(&lru_pvec);
349 BUG_ON(!list_empty(pages));
350 if (bio)
351 mpage_bio_submit(READ, bio);
352 return 0;
354 EXPORT_SYMBOL(mpage_readpages);
357 * This isn't called much at all
359 int mpage_readpage(struct page *page, get_block_t get_block)
361 struct bio *bio = NULL;
362 sector_t last_block_in_bio = 0;
364 bio = do_mpage_readpage(bio, page, 1,
365 &last_block_in_bio, get_block);
366 if (bio)
367 mpage_bio_submit(READ, bio);
368 return 0;
370 EXPORT_SYMBOL(mpage_readpage);
373 * Writing is not so simple.
375 * If the page has buffers then they will be used for obtaining the disk
376 * mapping. We only support pages which are fully mapped-and-dirty, with a
377 * special case for pages which are unmapped at the end: end-of-file.
379 * If the page has no buffers (preferred) then the page is mapped here.
381 * If all blocks are found to be contiguous then the page can go into the
382 * BIO. Otherwise fall back to the mapping's writepage().
384 * FIXME: This code wants an estimate of how many pages are still to be
385 * written, so it can intelligently allocate a suitably-sized BIO. For now,
386 * just allocate full-size (16-page) BIOs.
388 static struct bio *
389 mpage_writepage(struct bio *bio, struct page *page, get_block_t get_block,
390 sector_t *last_block_in_bio, int *ret, struct writeback_control *wbc)
392 struct address_space *mapping = page->mapping;
393 struct inode *inode = page->mapping->host;
394 const unsigned blkbits = inode->i_blkbits;
395 unsigned long end_index;
396 const unsigned blocks_per_page = PAGE_CACHE_SIZE >> blkbits;
397 sector_t last_block;
398 sector_t block_in_file;
399 sector_t blocks[MAX_BUF_PER_PAGE];
400 unsigned page_block;
401 unsigned first_unmapped = blocks_per_page;
402 struct block_device *bdev = NULL;
403 int boundary = 0;
404 sector_t boundary_block = 0;
405 struct block_device *boundary_bdev = NULL;
406 int length;
407 struct buffer_head map_bh;
408 loff_t i_size = i_size_read(inode);
410 if (page_has_buffers(page)) {
411 struct buffer_head *head = page_buffers(page);
412 struct buffer_head *bh = head;
414 /* If they're all mapped and dirty, do it */
415 page_block = 0;
416 do {
417 BUG_ON(buffer_locked(bh));
418 if (!buffer_mapped(bh)) {
420 * unmapped dirty buffers are created by
421 * __set_page_dirty_buffers -> mmapped data
423 if (buffer_dirty(bh))
424 goto confused;
425 if (first_unmapped == blocks_per_page)
426 first_unmapped = page_block;
427 continue;
430 if (first_unmapped != blocks_per_page)
431 goto confused; /* hole -> non-hole */
433 if (!buffer_dirty(bh) || !buffer_uptodate(bh))
434 goto confused;
435 if (page_block) {
436 if (bh->b_blocknr != blocks[page_block-1] + 1)
437 goto confused;
439 blocks[page_block++] = bh->b_blocknr;
440 boundary = buffer_boundary(bh);
441 if (boundary) {
442 boundary_block = bh->b_blocknr;
443 boundary_bdev = bh->b_bdev;
445 bdev = bh->b_bdev;
446 } while ((bh = bh->b_this_page) != head);
448 if (first_unmapped)
449 goto page_is_mapped;
452 * Page has buffers, but they are all unmapped. The page was
453 * created by pagein or read over a hole which was handled by
454 * block_read_full_page(). If this address_space is also
455 * using mpage_readpages then this can rarely happen.
457 goto confused;
461 * The page has no buffers: map it to disk
463 BUG_ON(!PageUptodate(page));
464 block_in_file = page->index << (PAGE_CACHE_SHIFT - blkbits);
465 last_block = (i_size - 1) >> blkbits;
466 map_bh.b_page = page;
467 for (page_block = 0; page_block < blocks_per_page; ) {
469 map_bh.b_state = 0;
470 if (get_block(inode, block_in_file, &map_bh, 1))
471 goto confused;
472 if (buffer_new(&map_bh))
473 unmap_underlying_metadata(map_bh.b_bdev,
474 map_bh.b_blocknr);
475 if (buffer_boundary(&map_bh)) {
476 boundary_block = map_bh.b_blocknr;
477 boundary_bdev = map_bh.b_bdev;
479 if (page_block) {
480 if (map_bh.b_blocknr != blocks[page_block-1] + 1)
481 goto confused;
483 blocks[page_block++] = map_bh.b_blocknr;
484 boundary = buffer_boundary(&map_bh);
485 bdev = map_bh.b_bdev;
486 if (block_in_file == last_block)
487 break;
488 block_in_file++;
490 BUG_ON(page_block == 0);
492 first_unmapped = page_block;
494 page_is_mapped:
495 end_index = i_size >> PAGE_CACHE_SHIFT;
496 if (page->index >= end_index) {
498 * The page straddles i_size. It must be zeroed out on each
499 * and every writepage invokation because it may be mmapped.
500 * "A file is mapped in multiples of the page size. For a file
501 * that is not a multiple of the page size, the remaining memory
502 * is zeroed when mapped, and writes to that region are not
503 * written out to the file."
505 unsigned offset = i_size & (PAGE_CACHE_SIZE - 1);
506 char *kaddr;
508 if (page->index > end_index || !offset)
509 goto confused;
510 kaddr = kmap_atomic(page, KM_USER0);
511 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
512 flush_dcache_page(page);
513 kunmap_atomic(kaddr, KM_USER0);
517 * This page will go to BIO. Do we need to send this BIO off first?
519 if (bio && *last_block_in_bio != blocks[0] - 1)
520 bio = mpage_bio_submit(WRITE, bio);
522 alloc_new:
523 if (bio == NULL) {
524 bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
525 bio_get_nr_vecs(bdev), GFP_NOFS|__GFP_HIGH);
526 if (bio == NULL)
527 goto confused;
531 * Must try to add the page before marking the buffer clean or
532 * the confused fail path above (OOM) will be very confused when
533 * it finds all bh marked clean (i.e. it will not write anything)
535 length = first_unmapped << blkbits;
536 if (bio_add_page(bio, page, length, 0) < length) {
537 bio = mpage_bio_submit(WRITE, bio);
538 goto alloc_new;
542 * OK, we have our BIO, so we can now mark the buffers clean. Make
543 * sure to only clean buffers which we know we'll be writing.
545 if (page_has_buffers(page)) {
546 struct buffer_head *head = page_buffers(page);
547 struct buffer_head *bh = head;
548 unsigned buffer_counter = 0;
550 do {
551 if (buffer_counter++ == first_unmapped)
552 break;
553 clear_buffer_dirty(bh);
554 bh = bh->b_this_page;
555 } while (bh != head);
558 * we cannot drop the bh if the page is not uptodate
559 * or a concurrent readpage would fail to serialize with the bh
560 * and it would read from disk before we reach the platter.
562 if (buffer_heads_over_limit && PageUptodate(page))
563 try_to_free_buffers(page);
566 BUG_ON(PageWriteback(page));
567 set_page_writeback(page);
568 unlock_page(page);
569 if (boundary || (first_unmapped != blocks_per_page)) {
570 bio = mpage_bio_submit(WRITE, bio);
571 if (boundary_block) {
572 write_boundary_block(boundary_bdev,
573 boundary_block, 1 << blkbits);
575 } else {
576 *last_block_in_bio = blocks[blocks_per_page - 1];
578 goto out;
580 confused:
581 if (bio)
582 bio = mpage_bio_submit(WRITE, bio);
583 *ret = page->mapping->a_ops->writepage(page, wbc);
585 * The caller has a ref on the inode, so *mapping is stable
587 if (*ret) {
588 if (*ret == -ENOSPC)
589 set_bit(AS_ENOSPC, &mapping->flags);
590 else
591 set_bit(AS_EIO, &mapping->flags);
593 out:
594 return bio;
598 * mpage_writepages - walk the list of dirty pages of the given
599 * address space and writepage() all of them.
601 * @mapping: address space structure to write
602 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
603 * @get_block: the filesystem's block mapper function.
604 * If this is NULL then use a_ops->writepage. Otherwise, go
605 * direct-to-BIO.
607 * This is a library function, which implements the writepages()
608 * address_space_operation.
610 * If a page is already under I/O, generic_writepages() skips it, even
611 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
612 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
613 * and msync() need to guarantee that all the data which was dirty at the time
614 * the call was made get new I/O started against them. If wbc->sync_mode is
615 * WB_SYNC_ALL then we were called for data integrity and we must wait for
616 * existing IO to complete.
619 mpage_writepages(struct address_space *mapping,
620 struct writeback_control *wbc, get_block_t get_block)
622 struct backing_dev_info *bdi = mapping->backing_dev_info;
623 struct bio *bio = NULL;
624 sector_t last_block_in_bio = 0;
625 int ret = 0;
626 int done = 0;
627 int (*writepage)(struct page *page, struct writeback_control *wbc);
628 struct pagevec pvec;
629 int nr_pages;
630 pgoff_t index;
631 pgoff_t end = -1; /* Inclusive */
632 int scanned = 0;
633 int is_range = 0;
635 if (wbc->nonblocking && bdi_write_congested(bdi)) {
636 wbc->encountered_congestion = 1;
637 return 0;
640 writepage = NULL;
641 if (get_block == NULL)
642 writepage = mapping->a_ops->writepage;
644 pagevec_init(&pvec, 0);
645 if (wbc->sync_mode == WB_SYNC_NONE) {
646 index = mapping->writeback_index; /* Start from prev offset */
647 } else {
648 index = 0; /* whole-file sweep */
649 scanned = 1;
651 if (wbc->start || wbc->end) {
652 index = wbc->start >> PAGE_CACHE_SHIFT;
653 end = wbc->end >> PAGE_CACHE_SHIFT;
654 is_range = 1;
655 scanned = 1;
657 retry:
658 while (!done && (index <= end) &&
659 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
660 PAGECACHE_TAG_DIRTY,
661 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
662 unsigned i;
664 scanned = 1;
665 for (i = 0; i < nr_pages; i++) {
666 struct page *page = pvec.pages[i];
669 * At this point we hold neither mapping->tree_lock nor
670 * lock on the page itself: the page may be truncated or
671 * invalidated (changing page->mapping to NULL), or even
672 * swizzled back from swapper_space to tmpfs file
673 * mapping
676 lock_page(page);
678 if (unlikely(page->mapping != mapping)) {
679 unlock_page(page);
680 continue;
683 if (unlikely(is_range) && page->index > end) {
684 done = 1;
685 unlock_page(page);
686 continue;
689 if (wbc->sync_mode != WB_SYNC_NONE)
690 wait_on_page_writeback(page);
692 if (PageWriteback(page) ||
693 !clear_page_dirty_for_io(page)) {
694 unlock_page(page);
695 continue;
698 if (writepage) {
699 ret = (*writepage)(page, wbc);
700 if (ret) {
701 if (ret == -ENOSPC)
702 set_bit(AS_ENOSPC,
703 &mapping->flags);
704 else
705 set_bit(AS_EIO,
706 &mapping->flags);
708 } else {
709 bio = mpage_writepage(bio, page, get_block,
710 &last_block_in_bio, &ret, wbc);
712 if (ret || (--(wbc->nr_to_write) <= 0))
713 done = 1;
714 if (wbc->nonblocking && bdi_write_congested(bdi)) {
715 wbc->encountered_congestion = 1;
716 done = 1;
719 pagevec_release(&pvec);
720 cond_resched();
722 if (!scanned && !done) {
724 * We hit the last page and there is more work to be done: wrap
725 * back to the start of the file
727 scanned = 1;
728 index = 0;
729 goto retry;
731 if (!is_range)
732 mapping->writeback_index = index;
733 if (bio)
734 mpage_bio_submit(WRITE, bio);
735 return ret;
737 EXPORT_SYMBOL(mpage_writepages);