[PATCH] kconfig documentation update
[linux-2.6/history.git] / fs / mpage.c
blob7d4bc97259abaa864907162bab1a5cd483adc385
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/kdev_t.h>
18 #include <linux/bio.h>
19 #include <linux/fs.h>
20 #include <linux/buffer_head.h>
21 #include <linux/blkdev.h>
22 #include <linux/highmem.h>
23 #include <linux/prefetch.h>
24 #include <linux/mpage.h>
25 #include <linux/writeback.h>
26 #include <linux/backing-dev.h>
27 #include <linux/pagevec.h>
30 * I/O completion handler for multipage BIOs.
32 * The mpage code never puts partial pages into a BIO (except for end-of-file).
33 * If a page does not map to a contiguous run of blocks then it simply falls
34 * back to block_read_full_page().
36 * Why is this? If a page's completion depends on a number of different BIOs
37 * which can complete in any order (or at the same time) then determining the
38 * status of that page is hard. See end_buffer_async_read() for the details.
39 * There is no point in duplicating all that complexity.
41 static int mpage_end_io_read(struct bio *bio, unsigned int bytes_done, int err)
43 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
44 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
46 if (bio->bi_size)
47 return 1;
49 do {
50 struct page *page = bvec->bv_page;
52 if (--bvec >= bio->bi_io_vec)
53 prefetchw(&bvec->bv_page->flags);
55 if (uptodate) {
56 SetPageUptodate(page);
57 } else {
58 ClearPageUptodate(page);
59 SetPageError(page);
61 unlock_page(page);
62 } while (bvec >= bio->bi_io_vec);
63 bio_put(bio);
64 return 0;
67 static int mpage_end_io_write(struct bio *bio, unsigned int bytes_done, int err)
69 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
70 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
72 if (bio->bi_size)
73 return 1;
75 do {
76 struct page *page = bvec->bv_page;
78 if (--bvec >= bio->bi_io_vec)
79 prefetchw(&bvec->bv_page->flags);
81 if (!uptodate)
82 SetPageError(page);
83 end_page_writeback(page);
84 } while (bvec >= bio->bi_io_vec);
85 bio_put(bio);
86 return 0;
89 struct bio *mpage_bio_submit(int rw, struct bio *bio)
91 bio->bi_end_io = mpage_end_io_read;
92 if (rw == WRITE)
93 bio->bi_end_io = mpage_end_io_write;
94 submit_bio(rw, bio);
95 return NULL;
98 static struct bio *
99 mpage_alloc(struct block_device *bdev,
100 sector_t first_sector, int nr_vecs, int gfp_flags)
102 struct bio *bio;
104 bio = bio_alloc(gfp_flags, nr_vecs);
106 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
107 while (!bio && (nr_vecs /= 2))
108 bio = bio_alloc(gfp_flags, nr_vecs);
111 if (bio) {
112 bio->bi_bdev = bdev;
113 bio->bi_sector = first_sector;
115 return bio;
119 * mpage_readpages - populate an address space with some pages, and
120 * start reads against them.
122 * @mapping: the address_space
123 * @pages: The address of a list_head which contains the target pages. These
124 * pages have their ->index populated and are otherwise uninitialised.
126 * The page at @pages->prev has the lowest file offset, and reads should be
127 * issued in @pages->prev to @pages->next order.
129 * @nr_pages: The number of pages at *@pages
130 * @get_block: The filesystem's block mapper function.
132 * This function walks the pages and the blocks within each page, building and
133 * emitting large BIOs.
135 * If anything unusual happens, such as:
137 * - encountering a page which has buffers
138 * - encountering a page which has a non-hole after a hole
139 * - encountering a page with non-contiguous blocks
141 * then this code just gives up and calls the buffer_head-based read function.
142 * It does handle a page which has holes at the end - that is a common case:
143 * the end-of-file on blocksize < PAGE_CACHE_SIZE setups.
145 * BH_Boundary explanation:
147 * There is a problem. The mpage read code assembles several pages, gets all
148 * their disk mappings, and then submits them all. That's fine, but obtaining
149 * the disk mappings may require I/O. Reads of indirect blocks, for example.
151 * So an mpage read of the first 16 blocks of an ext2 file will cause I/O to be
152 * submitted in the following order:
153 * 12 0 1 2 3 4 5 6 7 8 9 10 11 13 14 15 16
154 * because the indirect block has to be read to get the mappings of blocks
155 * 13,14,15,16. Obviously, this impacts performance.
157 * So what we do it to allow the filesystem's get_block() function to set
158 * BH_Boundary when it maps block 11. BH_Boundary says: mapping of the block
159 * after this one will require I/O against a block which is probably close to
160 * this one. So you should push what I/O you have currently accumulated.
162 * This all causes the disk requests to be issued in the correct order.
164 static struct bio *
165 do_mpage_readpage(struct bio *bio, struct page *page, unsigned nr_pages,
166 sector_t *last_block_in_bio, get_block_t get_block)
168 struct inode *inode = page->mapping->host;
169 const unsigned blkbits = inode->i_blkbits;
170 const unsigned blocks_per_page = PAGE_CACHE_SIZE >> blkbits;
171 const unsigned blocksize = 1 << blkbits;
172 sector_t block_in_file;
173 sector_t last_block;
174 sector_t blocks[MAX_BUF_PER_PAGE];
175 unsigned page_block;
176 unsigned first_hole = blocks_per_page;
177 struct block_device *bdev = NULL;
178 struct buffer_head bh;
179 int length;
181 if (page_has_buffers(page))
182 goto confused;
184 block_in_file = page->index << (PAGE_CACHE_SHIFT - blkbits);
185 last_block = (inode->i_size + blocksize - 1) >> blkbits;
187 for (page_block = 0; page_block < blocks_per_page;
188 page_block++, block_in_file++) {
189 bh.b_state = 0;
190 if (block_in_file < last_block) {
191 if (get_block(inode, block_in_file, &bh, 0))
192 goto confused;
195 if (!buffer_mapped(&bh)) {
196 if (first_hole == blocks_per_page)
197 first_hole = page_block;
198 continue;
201 if (first_hole != blocks_per_page)
202 goto confused; /* hole -> non-hole */
204 /* Contiguous blocks? */
205 if (page_block && blocks[page_block-1] != bh.b_blocknr-1)
206 goto confused;
207 blocks[page_block] = bh.b_blocknr;
208 bdev = bh.b_bdev;
211 if (first_hole != blocks_per_page) {
212 memset(kmap(page) + (first_hole << blkbits), 0,
213 PAGE_CACHE_SIZE - (first_hole << blkbits));
214 flush_dcache_page(page);
215 kunmap(page);
216 if (first_hole == 0) {
217 SetPageUptodate(page);
218 unlock_page(page);
219 goto out;
224 * This page will go to BIO. Do we need to send this BIO off first?
226 if (bio && (*last_block_in_bio != blocks[0] - 1))
227 bio = mpage_bio_submit(READ, bio);
229 alloc_new:
230 if (bio == NULL) {
231 bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
232 nr_pages, GFP_KERNEL);
233 if (bio == NULL)
234 goto confused;
237 length = first_hole << blkbits;
238 if (bio_add_page(bio, page, length, 0) < length) {
239 bio = mpage_bio_submit(READ, bio);
240 goto alloc_new;
243 if (buffer_boundary(&bh) || (first_hole != blocks_per_page))
244 bio = mpage_bio_submit(READ, bio);
245 else
246 *last_block_in_bio = blocks[blocks_per_page - 1];
247 out:
248 return bio;
250 confused:
251 if (bio)
252 bio = mpage_bio_submit(READ, bio);
253 block_read_full_page(page, get_block);
254 goto out;
258 mpage_readpages(struct address_space *mapping, struct list_head *pages,
259 unsigned nr_pages, get_block_t get_block)
261 struct bio *bio = NULL;
262 unsigned page_idx;
263 sector_t last_block_in_bio = 0;
264 struct pagevec lru_pvec;
266 pagevec_init(&lru_pvec, 0);
267 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
268 struct page *page = list_entry(pages->prev, struct page, list);
270 prefetchw(&page->flags);
271 list_del(&page->list);
272 if (!add_to_page_cache(page, mapping, page->index)) {
273 bio = do_mpage_readpage(bio, page,
274 nr_pages - page_idx,
275 &last_block_in_bio, get_block);
276 if (!pagevec_add(&lru_pvec, page))
277 __pagevec_lru_add(&lru_pvec);
278 } else {
279 page_cache_release(page);
282 pagevec_lru_add(&lru_pvec);
283 BUG_ON(!list_empty(pages));
284 if (bio)
285 mpage_bio_submit(READ, bio);
286 return 0;
288 EXPORT_SYMBOL(mpage_readpages);
291 * This isn't called much at all
293 int mpage_readpage(struct page *page, get_block_t get_block)
295 struct bio *bio = NULL;
296 sector_t last_block_in_bio = 0;
298 bio = do_mpage_readpage(bio, page, 1,
299 &last_block_in_bio, get_block);
300 if (bio)
301 mpage_bio_submit(READ, bio);
302 return 0;
304 EXPORT_SYMBOL(mpage_readpage);
307 * Writing is not so simple.
309 * If the page has buffers then they will be used for obtaining the disk
310 * mapping. We only support pages which are fully mapped-and-dirty, with a
311 * special case for pages which are unmapped at the end: end-of-file.
313 * If the page has no buffers (preferred) then the page is mapped here.
315 * If all blocks are found to be contiguous then the page can go into the
316 * BIO. Otherwise fall back to the mapping's writepage().
318 * FIXME: This code wants an estimate of how many pages are still to be
319 * written, so it can intelligently allocate a suitably-sized BIO. For now,
320 * just allocate full-size (16-page) BIOs.
322 static struct bio *
323 mpage_writepage(struct bio *bio, struct page *page, get_block_t get_block,
324 sector_t *last_block_in_bio, int *ret)
326 struct inode *inode = page->mapping->host;
327 const unsigned blkbits = inode->i_blkbits;
328 unsigned long end_index;
329 const unsigned blocks_per_page = PAGE_CACHE_SIZE >> blkbits;
330 sector_t last_block;
331 sector_t block_in_file;
332 sector_t blocks[MAX_BUF_PER_PAGE];
333 unsigned page_block;
334 unsigned first_unmapped = blocks_per_page;
335 struct block_device *bdev = NULL;
336 int boundary = 0;
337 sector_t boundary_block = 0;
338 struct block_device *boundary_bdev = NULL;
339 int length;
341 if (page_has_buffers(page)) {
342 struct buffer_head *head = page_buffers(page);
343 struct buffer_head *bh = head;
345 /* If they're all mapped and dirty, do it */
346 page_block = 0;
347 do {
348 BUG_ON(buffer_locked(bh));
349 if (!buffer_mapped(bh)) {
351 * unmapped dirty buffers are created by
352 * __set_page_dirty_buffers -> mmapped data
354 if (buffer_dirty(bh))
355 goto confused;
356 if (first_unmapped == blocks_per_page)
357 first_unmapped = page_block;
358 continue;
361 if (first_unmapped != blocks_per_page)
362 goto confused; /* hole -> non-hole */
364 if (!buffer_dirty(bh) || !buffer_uptodate(bh))
365 goto confused;
366 if (page_block) {
367 if (bh->b_blocknr != blocks[page_block-1] + 1)
368 goto confused;
370 blocks[page_block++] = bh->b_blocknr;
371 boundary = buffer_boundary(bh);
372 if (boundary) {
373 boundary_block = bh->b_blocknr;
374 boundary_bdev = bh->b_bdev;
376 bdev = bh->b_bdev;
377 } while ((bh = bh->b_this_page) != head);
379 if (first_unmapped)
380 goto page_is_mapped;
383 * Page has buffers, but they are all unmapped. The page was
384 * created by pagein or read over a hole which was handled by
385 * block_read_full_page(). If this address_space is also
386 * using mpage_readpages then this can rarely happen.
388 goto confused;
392 * The page has no buffers: map it to disk
394 BUG_ON(!PageUptodate(page));
395 block_in_file = page->index << (PAGE_CACHE_SHIFT - blkbits);
396 last_block = (inode->i_size - 1) >> blkbits;
397 for (page_block = 0; page_block < blocks_per_page; ) {
398 struct buffer_head map_bh;
400 map_bh.b_state = 0;
401 if (get_block(inode, block_in_file, &map_bh, 1))
402 goto confused;
403 if (buffer_new(&map_bh))
404 unmap_underlying_metadata(map_bh.b_bdev,
405 map_bh.b_blocknr);
406 if (buffer_boundary(&map_bh)) {
407 boundary_block = map_bh.b_blocknr;
408 boundary_bdev = map_bh.b_bdev;
410 if (page_block) {
411 if (map_bh.b_blocknr != blocks[page_block-1] + 1)
412 goto confused;
414 blocks[page_block++] = map_bh.b_blocknr;
415 boundary = buffer_boundary(&map_bh);
416 bdev = map_bh.b_bdev;
417 if (block_in_file == last_block)
418 break;
419 block_in_file++;
421 if (page_block == 0)
422 buffer_error();
424 first_unmapped = page_block;
426 end_index = inode->i_size >> PAGE_CACHE_SHIFT;
427 if (page->index >= end_index) {
428 unsigned offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
430 if (page->index > end_index || !offset)
431 goto confused;
432 memset(kmap(page) + offset, 0, PAGE_CACHE_SIZE - offset);
433 flush_dcache_page(page);
434 kunmap(page);
437 page_is_mapped:
440 * This page will go to BIO. Do we need to send this BIO off first?
442 if (bio && *last_block_in_bio != blocks[0] - 1)
443 bio = mpage_bio_submit(WRITE, bio);
445 alloc_new:
446 if (bio == NULL) {
447 bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
448 bio_get_nr_vecs(bdev), GFP_NOFS|__GFP_HIGH);
449 if (bio == NULL)
450 goto confused;
454 * OK, we have our BIO, so we can now mark the buffers clean. Make
455 * sure to only clean buffers which we know we'll be writing.
457 if (page_has_buffers(page)) {
458 struct buffer_head *head = page_buffers(page);
459 struct buffer_head *bh = head;
460 unsigned buffer_counter = 0;
462 do {
463 if (buffer_counter++ == first_unmapped)
464 break;
465 clear_buffer_dirty(bh);
466 bh = bh->b_this_page;
467 } while (bh != head);
469 if (buffer_heads_over_limit)
470 try_to_free_buffers(page);
473 length = first_unmapped << blkbits;
474 if (bio_add_page(bio, page, length, 0) < length) {
475 bio = mpage_bio_submit(WRITE, bio);
476 goto alloc_new;
479 BUG_ON(PageWriteback(page));
480 SetPageWriteback(page);
481 unlock_page(page);
482 if (boundary || (first_unmapped != blocks_per_page)) {
483 bio = mpage_bio_submit(WRITE, bio);
484 if (boundary_block) {
485 write_boundary_block(boundary_bdev,
486 boundary_block, 1 << blkbits);
488 } else {
489 *last_block_in_bio = blocks[blocks_per_page - 1];
491 goto out;
493 confused:
494 if (bio)
495 bio = mpage_bio_submit(WRITE, bio);
496 *ret = page->mapping->a_ops->writepage(page);
497 out:
498 return bio;
502 * mpage_writepages - walk the list of dirty pages of the given
503 * address space and writepage() all of them.
505 * @mapping: address space structure to write
506 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
507 * @get_block: the filesystem's block mapper function.
508 * If this is NULL then use a_ops->writepage. Otherwise, go
509 * direct-to-BIO.
511 * This is a library function, which implements the writepages()
512 * address_space_operation.
514 * (The next two paragraphs refer to code which isn't here yet, but they
515 * explain the presence of address_space.io_pages)
517 * Pages can be moved from clean_pages or locked_pages onto dirty_pages
518 * at any time - it's not possible to lock against that. So pages which
519 * have already been added to a BIO may magically reappear on the dirty_pages
520 * list. And generic_writepages() will again try to lock those pages.
521 * But I/O has not yet been started against the page. Thus deadlock.
523 * To avoid this, the entire contents of the dirty_pages list are moved
524 * onto io_pages up-front. We then walk io_pages, locking the
525 * pages and submitting them for I/O, moving them to locked_pages.
527 * This has the added benefit of preventing a livelock which would otherwise
528 * occur if pages are being dirtied faster than we can write them out.
530 * If a page is already under I/O, generic_writepages() skips it, even
531 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
532 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
533 * and msync() need to guarentee that all the data which was dirty at the time
534 * the call was made get new I/O started against them. The way to do this is
535 * to run filemap_fdatawait() before calling filemap_fdatawrite().
537 * It's fairly rare for PageWriteback pages to be on ->dirty_pages. It
538 * means that someone redirtied the page while it was under I/O.
541 mpage_writepages(struct address_space *mapping,
542 struct writeback_control *wbc, get_block_t get_block)
544 struct backing_dev_info *bdi = mapping->backing_dev_info;
545 struct bio *bio = NULL;
546 sector_t last_block_in_bio = 0;
547 int ret = 0;
548 int done = 0;
549 int sync = called_for_sync();
550 struct pagevec pvec;
551 int (*writepage)(struct page *);
553 if (wbc->nonblocking && bdi_write_congested(bdi)) {
554 blk_run_queues();
555 wbc->encountered_congestion = 1;
556 return 0;
559 writepage = NULL;
560 if (get_block == NULL)
561 writepage = mapping->a_ops->writepage;
563 pagevec_init(&pvec, 0);
564 write_lock(&mapping->page_lock);
566 list_splice_init(&mapping->dirty_pages, &mapping->io_pages);
568 while (!list_empty(&mapping->io_pages) && !done) {
569 struct page *page = list_entry(mapping->io_pages.prev,
570 struct page, list);
571 list_del(&page->list);
572 if (PageWriteback(page) && !sync) {
573 if (PageDirty(page)) {
574 list_add(&page->list, &mapping->dirty_pages);
575 continue;
577 list_add(&page->list, &mapping->locked_pages);
578 continue;
580 if (!PageDirty(page)) {
581 list_add(&page->list, &mapping->clean_pages);
582 continue;
584 list_add(&page->list, &mapping->locked_pages);
586 page_cache_get(page);
587 write_unlock(&mapping->page_lock);
589 lock_page(page);
591 if (sync)
592 wait_on_page_writeback(page);
594 if (page->mapping && !PageWriteback(page) &&
595 test_clear_page_dirty(page)) {
596 if (writepage) {
597 ret = (*writepage)(page);
598 if (ret == -EAGAIN) {
599 __set_page_dirty_nobuffers(page);
600 ret = 0;
602 } else {
603 bio = mpage_writepage(bio, page, get_block,
604 &last_block_in_bio, &ret);
606 if ((current->flags & PF_MEMALLOC) &&
607 !PageActive(page) && PageLRU(page)) {
608 if (!pagevec_add(&pvec, page))
609 pagevec_deactivate_inactive(&pvec);
610 page = NULL;
612 if (ret || (--(wbc->nr_to_write) <= 0))
613 done = 1;
614 if (wbc->nonblocking && bdi_write_congested(bdi)) {
615 blk_run_queues();
616 wbc->encountered_congestion = 1;
617 done = 1;
619 } else {
620 unlock_page(page);
623 if (page)
624 page_cache_release(page);
625 write_lock(&mapping->page_lock);
628 * Leave any remaining dirty pages on ->io_pages
630 write_unlock(&mapping->page_lock);
631 pagevec_deactivate_inactive(&pvec);
632 if (bio)
633 mpage_bio_submit(WRITE, bio);
634 return ret;
636 EXPORT_SYMBOL(mpage_writepages);