x86: provide a bad_dma_address symbol for i386
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / mpage.c
blob235e4d3873a88d17837f1a9551f09664c8ffcb54
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 void mpage_end_io_read(struct bio *bio, 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 do {
48 struct page *page = bvec->bv_page;
50 if (--bvec >= bio->bi_io_vec)
51 prefetchw(&bvec->bv_page->flags);
53 if (uptodate) {
54 SetPageUptodate(page);
55 } else {
56 ClearPageUptodate(page);
57 SetPageError(page);
59 unlock_page(page);
60 } while (bvec >= bio->bi_io_vec);
61 bio_put(bio);
64 static void mpage_end_io_write(struct bio *bio, int err)
66 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
67 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
69 do {
70 struct page *page = bvec->bv_page;
72 if (--bvec >= bio->bi_io_vec)
73 prefetchw(&bvec->bv_page->flags);
75 if (!uptodate){
76 SetPageError(page);
77 if (page->mapping)
78 set_bit(AS_EIO, &page->mapping->flags);
80 end_page_writeback(page);
81 } while (bvec >= bio->bi_io_vec);
82 bio_put(bio);
85 static struct bio *mpage_bio_submit(int rw, struct bio *bio)
87 bio->bi_end_io = mpage_end_io_read;
88 if (rw == WRITE)
89 bio->bi_end_io = mpage_end_io_write;
90 submit_bio(rw, bio);
91 return NULL;
94 static struct bio *
95 mpage_alloc(struct block_device *bdev,
96 sector_t first_sector, int nr_vecs,
97 gfp_t gfp_flags)
99 struct bio *bio;
101 bio = bio_alloc(gfp_flags, nr_vecs);
103 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
104 while (!bio && (nr_vecs /= 2))
105 bio = bio_alloc(gfp_flags, nr_vecs);
108 if (bio) {
109 bio->bi_bdev = bdev;
110 bio->bi_sector = first_sector;
112 return bio;
116 * support function for mpage_readpages. The fs supplied get_block might
117 * return an up to date buffer. This is used to map that buffer into
118 * the page, which allows readpage to avoid triggering a duplicate call
119 * to get_block.
121 * The idea is to avoid adding buffers to pages that don't already have
122 * them. So when the buffer is up to date and the page size == block size,
123 * this marks the page up to date instead of adding new buffers.
125 static void
126 map_buffer_to_page(struct page *page, struct buffer_head *bh, int page_block)
128 struct inode *inode = page->mapping->host;
129 struct buffer_head *page_bh, *head;
130 int block = 0;
132 if (!page_has_buffers(page)) {
134 * don't make any buffers if there is only one buffer on
135 * the page and the page just needs to be set up to date
137 if (inode->i_blkbits == PAGE_CACHE_SHIFT &&
138 buffer_uptodate(bh)) {
139 SetPageUptodate(page);
140 return;
142 create_empty_buffers(page, 1 << inode->i_blkbits, 0);
144 head = page_buffers(page);
145 page_bh = head;
146 do {
147 if (block == page_block) {
148 page_bh->b_state = bh->b_state;
149 page_bh->b_bdev = bh->b_bdev;
150 page_bh->b_blocknr = bh->b_blocknr;
151 break;
153 page_bh = page_bh->b_this_page;
154 block++;
155 } while (page_bh != head);
159 * This is the worker routine which does all the work of mapping the disk
160 * blocks and constructs largest possible bios, submits them for IO if the
161 * blocks are not contiguous on the disk.
163 * We pass a buffer_head back and forth and use its buffer_mapped() flag to
164 * represent the validity of its disk mapping and to decide when to do the next
165 * get_block() call.
167 static struct bio *
168 do_mpage_readpage(struct bio *bio, struct page *page, unsigned nr_pages,
169 sector_t *last_block_in_bio, struct buffer_head *map_bh,
170 unsigned long *first_logical_block, get_block_t get_block)
172 struct inode *inode = page->mapping->host;
173 const unsigned blkbits = inode->i_blkbits;
174 const unsigned blocks_per_page = PAGE_CACHE_SIZE >> blkbits;
175 const unsigned blocksize = 1 << blkbits;
176 sector_t block_in_file;
177 sector_t last_block;
178 sector_t last_block_in_file;
179 sector_t blocks[MAX_BUF_PER_PAGE];
180 unsigned page_block;
181 unsigned first_hole = blocks_per_page;
182 struct block_device *bdev = NULL;
183 int length;
184 int fully_mapped = 1;
185 unsigned nblocks;
186 unsigned relative_block;
188 if (page_has_buffers(page))
189 goto confused;
191 block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits);
192 last_block = block_in_file + nr_pages * blocks_per_page;
193 last_block_in_file = (i_size_read(inode) + blocksize - 1) >> blkbits;
194 if (last_block > last_block_in_file)
195 last_block = last_block_in_file;
196 page_block = 0;
199 * Map blocks using the result from the previous get_blocks call first.
201 nblocks = map_bh->b_size >> blkbits;
202 if (buffer_mapped(map_bh) && block_in_file > *first_logical_block &&
203 block_in_file < (*first_logical_block + nblocks)) {
204 unsigned map_offset = block_in_file - *first_logical_block;
205 unsigned last = nblocks - map_offset;
207 for (relative_block = 0; ; relative_block++) {
208 if (relative_block == last) {
209 clear_buffer_mapped(map_bh);
210 break;
212 if (page_block == blocks_per_page)
213 break;
214 blocks[page_block] = map_bh->b_blocknr + map_offset +
215 relative_block;
216 page_block++;
217 block_in_file++;
219 bdev = map_bh->b_bdev;
223 * Then do more get_blocks calls until we are done with this page.
225 map_bh->b_page = page;
226 while (page_block < blocks_per_page) {
227 map_bh->b_state = 0;
228 map_bh->b_size = 0;
230 if (block_in_file < last_block) {
231 map_bh->b_size = (last_block-block_in_file) << blkbits;
232 if (get_block(inode, block_in_file, map_bh, 0))
233 goto confused;
234 *first_logical_block = block_in_file;
237 if (!buffer_mapped(map_bh)) {
238 fully_mapped = 0;
239 if (first_hole == blocks_per_page)
240 first_hole = page_block;
241 page_block++;
242 block_in_file++;
243 clear_buffer_mapped(map_bh);
244 continue;
247 /* some filesystems will copy data into the page during
248 * the get_block call, in which case we don't want to
249 * read it again. map_buffer_to_page copies the data
250 * we just collected from get_block into the page's buffers
251 * so readpage doesn't have to repeat the get_block call
253 if (buffer_uptodate(map_bh)) {
254 map_buffer_to_page(page, map_bh, page_block);
255 goto confused;
258 if (first_hole != blocks_per_page)
259 goto confused; /* hole -> non-hole */
261 /* Contiguous blocks? */
262 if (page_block && blocks[page_block-1] != map_bh->b_blocknr-1)
263 goto confused;
264 nblocks = map_bh->b_size >> blkbits;
265 for (relative_block = 0; ; relative_block++) {
266 if (relative_block == nblocks) {
267 clear_buffer_mapped(map_bh);
268 break;
269 } else if (page_block == blocks_per_page)
270 break;
271 blocks[page_block] = map_bh->b_blocknr+relative_block;
272 page_block++;
273 block_in_file++;
275 bdev = map_bh->b_bdev;
278 if (first_hole != blocks_per_page) {
279 zero_user_segment(page, first_hole << blkbits, PAGE_CACHE_SIZE);
280 if (first_hole == 0) {
281 SetPageUptodate(page);
282 unlock_page(page);
283 goto out;
285 } else if (fully_mapped) {
286 SetPageMappedToDisk(page);
290 * This page will go to BIO. Do we need to send this BIO off first?
292 if (bio && (*last_block_in_bio != blocks[0] - 1))
293 bio = mpage_bio_submit(READ, bio);
295 alloc_new:
296 if (bio == NULL) {
297 bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
298 min_t(int, nr_pages, bio_get_nr_vecs(bdev)),
299 GFP_KERNEL);
300 if (bio == NULL)
301 goto confused;
304 length = first_hole << blkbits;
305 if (bio_add_page(bio, page, length, 0) < length) {
306 bio = mpage_bio_submit(READ, bio);
307 goto alloc_new;
310 if (buffer_boundary(map_bh) || (first_hole != blocks_per_page))
311 bio = mpage_bio_submit(READ, bio);
312 else
313 *last_block_in_bio = blocks[blocks_per_page - 1];
314 out:
315 return bio;
317 confused:
318 if (bio)
319 bio = mpage_bio_submit(READ, bio);
320 if (!PageUptodate(page))
321 block_read_full_page(page, get_block);
322 else
323 unlock_page(page);
324 goto out;
328 * mpage_readpages - populate an address space with some pages & start reads against them
329 * @mapping: the address_space
330 * @pages: The address of a list_head which contains the target pages. These
331 * pages have their ->index populated and are otherwise uninitialised.
332 * The page at @pages->prev has the lowest file offset, and reads should be
333 * issued in @pages->prev to @pages->next order.
334 * @nr_pages: The number of pages at *@pages
335 * @get_block: The filesystem's block mapper function.
337 * This function walks the pages and the blocks within each page, building and
338 * emitting large BIOs.
340 * If anything unusual happens, such as:
342 * - encountering a page which has buffers
343 * - encountering a page which has a non-hole after a hole
344 * - encountering a page with non-contiguous blocks
346 * then this code just gives up and calls the buffer_head-based read function.
347 * It does handle a page which has holes at the end - that is a common case:
348 * the end-of-file on blocksize < PAGE_CACHE_SIZE setups.
350 * BH_Boundary explanation:
352 * There is a problem. The mpage read code assembles several pages, gets all
353 * their disk mappings, and then submits them all. That's fine, but obtaining
354 * the disk mappings may require I/O. Reads of indirect blocks, for example.
356 * So an mpage read of the first 16 blocks of an ext2 file will cause I/O to be
357 * submitted in the following order:
358 * 12 0 1 2 3 4 5 6 7 8 9 10 11 13 14 15 16
360 * because the indirect block has to be read to get the mappings of blocks
361 * 13,14,15,16. Obviously, this impacts performance.
363 * So what we do it to allow the filesystem's get_block() function to set
364 * BH_Boundary when it maps block 11. BH_Boundary says: mapping of the block
365 * after this one will require I/O against a block which is probably close to
366 * this one. So you should push what I/O you have currently accumulated.
368 * This all causes the disk requests to be issued in the correct order.
371 mpage_readpages(struct address_space *mapping, struct list_head *pages,
372 unsigned nr_pages, get_block_t get_block)
374 struct bio *bio = NULL;
375 unsigned page_idx;
376 sector_t last_block_in_bio = 0;
377 struct buffer_head map_bh;
378 unsigned long first_logical_block = 0;
380 clear_buffer_mapped(&map_bh);
381 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
382 struct page *page = list_entry(pages->prev, struct page, lru);
384 prefetchw(&page->flags);
385 list_del(&page->lru);
386 if (!add_to_page_cache_lru(page, mapping,
387 page->index, GFP_KERNEL)) {
388 bio = do_mpage_readpage(bio, page,
389 nr_pages - page_idx,
390 &last_block_in_bio, &map_bh,
391 &first_logical_block,
392 get_block);
394 page_cache_release(page);
396 BUG_ON(!list_empty(pages));
397 if (bio)
398 mpage_bio_submit(READ, bio);
399 return 0;
401 EXPORT_SYMBOL(mpage_readpages);
404 * This isn't called much at all
406 int mpage_readpage(struct page *page, get_block_t get_block)
408 struct bio *bio = NULL;
409 sector_t last_block_in_bio = 0;
410 struct buffer_head map_bh;
411 unsigned long first_logical_block = 0;
413 clear_buffer_mapped(&map_bh);
414 bio = do_mpage_readpage(bio, page, 1, &last_block_in_bio,
415 &map_bh, &first_logical_block, get_block);
416 if (bio)
417 mpage_bio_submit(READ, bio);
418 return 0;
420 EXPORT_SYMBOL(mpage_readpage);
423 * Writing is not so simple.
425 * If the page has buffers then they will be used for obtaining the disk
426 * mapping. We only support pages which are fully mapped-and-dirty, with a
427 * special case for pages which are unmapped at the end: end-of-file.
429 * If the page has no buffers (preferred) then the page is mapped here.
431 * If all blocks are found to be contiguous then the page can go into the
432 * BIO. Otherwise fall back to the mapping's writepage().
434 * FIXME: This code wants an estimate of how many pages are still to be
435 * written, so it can intelligently allocate a suitably-sized BIO. For now,
436 * just allocate full-size (16-page) BIOs.
438 struct mpage_data {
439 struct bio *bio;
440 sector_t last_block_in_bio;
441 get_block_t *get_block;
442 unsigned use_writepage;
445 static int __mpage_writepage(struct page *page, struct writeback_control *wbc,
446 void *data)
448 struct mpage_data *mpd = data;
449 struct bio *bio = mpd->bio;
450 struct address_space *mapping = page->mapping;
451 struct inode *inode = page->mapping->host;
452 const unsigned blkbits = inode->i_blkbits;
453 unsigned long end_index;
454 const unsigned blocks_per_page = PAGE_CACHE_SIZE >> blkbits;
455 sector_t last_block;
456 sector_t block_in_file;
457 sector_t blocks[MAX_BUF_PER_PAGE];
458 unsigned page_block;
459 unsigned first_unmapped = blocks_per_page;
460 struct block_device *bdev = NULL;
461 int boundary = 0;
462 sector_t boundary_block = 0;
463 struct block_device *boundary_bdev = NULL;
464 int length;
465 struct buffer_head map_bh;
466 loff_t i_size = i_size_read(inode);
467 int ret = 0;
469 if (page_has_buffers(page)) {
470 struct buffer_head *head = page_buffers(page);
471 struct buffer_head *bh = head;
473 /* If they're all mapped and dirty, do it */
474 page_block = 0;
475 do {
476 BUG_ON(buffer_locked(bh));
477 if (!buffer_mapped(bh)) {
479 * unmapped dirty buffers are created by
480 * __set_page_dirty_buffers -> mmapped data
482 if (buffer_dirty(bh))
483 goto confused;
484 if (first_unmapped == blocks_per_page)
485 first_unmapped = page_block;
486 continue;
489 if (first_unmapped != blocks_per_page)
490 goto confused; /* hole -> non-hole */
492 if (!buffer_dirty(bh) || !buffer_uptodate(bh))
493 goto confused;
494 if (page_block) {
495 if (bh->b_blocknr != blocks[page_block-1] + 1)
496 goto confused;
498 blocks[page_block++] = bh->b_blocknr;
499 boundary = buffer_boundary(bh);
500 if (boundary) {
501 boundary_block = bh->b_blocknr;
502 boundary_bdev = bh->b_bdev;
504 bdev = bh->b_bdev;
505 } while ((bh = bh->b_this_page) != head);
507 if (first_unmapped)
508 goto page_is_mapped;
511 * Page has buffers, but they are all unmapped. The page was
512 * created by pagein or read over a hole which was handled by
513 * block_read_full_page(). If this address_space is also
514 * using mpage_readpages then this can rarely happen.
516 goto confused;
520 * The page has no buffers: map it to disk
522 BUG_ON(!PageUptodate(page));
523 block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits);
524 last_block = (i_size - 1) >> blkbits;
525 map_bh.b_page = page;
526 for (page_block = 0; page_block < blocks_per_page; ) {
528 map_bh.b_state = 0;
529 map_bh.b_size = 1 << blkbits;
530 if (mpd->get_block(inode, block_in_file, &map_bh, 1))
531 goto confused;
532 if (buffer_new(&map_bh))
533 unmap_underlying_metadata(map_bh.b_bdev,
534 map_bh.b_blocknr);
535 if (buffer_boundary(&map_bh)) {
536 boundary_block = map_bh.b_blocknr;
537 boundary_bdev = map_bh.b_bdev;
539 if (page_block) {
540 if (map_bh.b_blocknr != blocks[page_block-1] + 1)
541 goto confused;
543 blocks[page_block++] = map_bh.b_blocknr;
544 boundary = buffer_boundary(&map_bh);
545 bdev = map_bh.b_bdev;
546 if (block_in_file == last_block)
547 break;
548 block_in_file++;
550 BUG_ON(page_block == 0);
552 first_unmapped = page_block;
554 page_is_mapped:
555 end_index = i_size >> PAGE_CACHE_SHIFT;
556 if (page->index >= end_index) {
558 * The page straddles i_size. It must be zeroed out on each
559 * and every writepage invokation because it may be mmapped.
560 * "A file is mapped in multiples of the page size. For a file
561 * that is not a multiple of the page size, the remaining memory
562 * is zeroed when mapped, and writes to that region are not
563 * written out to the file."
565 unsigned offset = i_size & (PAGE_CACHE_SIZE - 1);
567 if (page->index > end_index || !offset)
568 goto confused;
569 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
573 * This page will go to BIO. Do we need to send this BIO off first?
575 if (bio && mpd->last_block_in_bio != blocks[0] - 1)
576 bio = mpage_bio_submit(WRITE, bio);
578 alloc_new:
579 if (bio == NULL) {
580 bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
581 bio_get_nr_vecs(bdev), GFP_NOFS|__GFP_HIGH);
582 if (bio == NULL)
583 goto confused;
587 * Must try to add the page before marking the buffer clean or
588 * the confused fail path above (OOM) will be very confused when
589 * it finds all bh marked clean (i.e. it will not write anything)
591 length = first_unmapped << blkbits;
592 if (bio_add_page(bio, page, length, 0) < length) {
593 bio = mpage_bio_submit(WRITE, bio);
594 goto alloc_new;
598 * OK, we have our BIO, so we can now mark the buffers clean. Make
599 * sure to only clean buffers which we know we'll be writing.
601 if (page_has_buffers(page)) {
602 struct buffer_head *head = page_buffers(page);
603 struct buffer_head *bh = head;
604 unsigned buffer_counter = 0;
606 do {
607 if (buffer_counter++ == first_unmapped)
608 break;
609 clear_buffer_dirty(bh);
610 bh = bh->b_this_page;
611 } while (bh != head);
614 * we cannot drop the bh if the page is not uptodate
615 * or a concurrent readpage would fail to serialize with the bh
616 * and it would read from disk before we reach the platter.
618 if (buffer_heads_over_limit && PageUptodate(page))
619 try_to_free_buffers(page);
622 BUG_ON(PageWriteback(page));
623 set_page_writeback(page);
624 unlock_page(page);
625 if (boundary || (first_unmapped != blocks_per_page)) {
626 bio = mpage_bio_submit(WRITE, bio);
627 if (boundary_block) {
628 write_boundary_block(boundary_bdev,
629 boundary_block, 1 << blkbits);
631 } else {
632 mpd->last_block_in_bio = blocks[blocks_per_page - 1];
634 goto out;
636 confused:
637 if (bio)
638 bio = mpage_bio_submit(WRITE, bio);
640 if (mpd->use_writepage) {
641 ret = mapping->a_ops->writepage(page, wbc);
642 } else {
643 ret = -EAGAIN;
644 goto out;
647 * The caller has a ref on the inode, so *mapping is stable
649 mapping_set_error(mapping, ret);
650 out:
651 mpd->bio = bio;
652 return ret;
656 * mpage_writepages - walk the list of dirty pages of the given address space & writepage() all of them
657 * @mapping: address space structure to write
658 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
659 * @get_block: the filesystem's block mapper function.
660 * If this is NULL then use a_ops->writepage. Otherwise, go
661 * direct-to-BIO.
663 * This is a library function, which implements the writepages()
664 * address_space_operation.
666 * If a page is already under I/O, generic_writepages() skips it, even
667 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
668 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
669 * and msync() need to guarantee that all the data which was dirty at the time
670 * the call was made get new I/O started against them. If wbc->sync_mode is
671 * WB_SYNC_ALL then we were called for data integrity and we must wait for
672 * existing IO to complete.
675 mpage_writepages(struct address_space *mapping,
676 struct writeback_control *wbc, get_block_t get_block)
678 int ret;
680 if (!get_block)
681 ret = generic_writepages(mapping, wbc);
682 else {
683 struct mpage_data mpd = {
684 .bio = NULL,
685 .last_block_in_bio = 0,
686 .get_block = get_block,
687 .use_writepage = 1,
690 ret = write_cache_pages(mapping, wbc, __mpage_writepage, &mpd);
691 if (mpd.bio)
692 mpage_bio_submit(WRITE, mpd.bio);
694 return ret;
696 EXPORT_SYMBOL(mpage_writepages);
698 int mpage_writepage(struct page *page, get_block_t get_block,
699 struct writeback_control *wbc)
701 struct mpage_data mpd = {
702 .bio = NULL,
703 .last_block_in_bio = 0,
704 .get_block = get_block,
705 .use_writepage = 0,
707 int ret = __mpage_writepage(page, wbc, &mpd);
708 if (mpd.bio)
709 mpage_bio_submit(WRITE, mpd.bio);
710 return ret;
712 EXPORT_SYMBOL(mpage_writepage);