MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / fs / mpage.c
blob097d3629a017cba7751117b8153f22162a503dac
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 #if 0 // mask by Victor Yu. 02-12-2007
85 if (page->mapping)
86 set_bit(AS_EIO, &page->mapping->flags);
87 #else
88 if (page->u.xx.mapping)
89 set_bit(AS_EIO, &page->u.xx.mapping->flags);
90 #endif
92 end_page_writeback(page);
93 } while (bvec >= bio->bi_io_vec);
94 bio_put(bio);
95 return 0;
98 static struct bio *mpage_bio_submit(int rw, struct bio *bio)
100 bio->bi_end_io = mpage_end_io_read;
101 if (rw == WRITE)
102 bio->bi_end_io = mpage_end_io_write;
103 submit_bio(rw, bio);
104 return NULL;
107 static struct bio *
108 mpage_alloc(struct block_device *bdev,
109 sector_t first_sector, int nr_vecs,
110 gfp_t gfp_flags)
112 struct bio *bio;
114 bio = bio_alloc(gfp_flags, nr_vecs);
116 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
117 while (!bio && (nr_vecs /= 2))
118 bio = bio_alloc(gfp_flags, nr_vecs);
121 if (bio) {
122 bio->bi_bdev = bdev;
123 bio->bi_sector = first_sector;
125 return bio;
129 * support function for mpage_readpages. The fs supplied get_block might
130 * return an up to date buffer. This is used to map that buffer into
131 * the page, which allows readpage to avoid triggering a duplicate call
132 * to get_block.
134 * The idea is to avoid adding buffers to pages that don't already have
135 * them. So when the buffer is up to date and the page size == block size,
136 * this marks the page up to date instead of adding new buffers.
138 static void
139 map_buffer_to_page(struct page *page, struct buffer_head *bh, int page_block)
141 #if 0 // mask by Victor Yu. 02-12-2007
142 struct inode *inode = page->mapping->host;
143 #else
144 struct inode *inode = page->u.xx.mapping->host;
145 #endif
146 struct buffer_head *page_bh, *head;
147 int block = 0;
149 if (!page_has_buffers(page)) {
151 * don't make any buffers if there is only one buffer on
152 * the page and the page just needs to be set up to date
154 if (inode->i_blkbits == PAGE_CACHE_SHIFT &&
155 buffer_uptodate(bh)) {
156 SetPageUptodate(page);
157 return;
159 create_empty_buffers(page, 1 << inode->i_blkbits, 0);
161 head = page_buffers(page);
162 page_bh = head;
163 do {
164 if (block == page_block) {
165 page_bh->b_state = bh->b_state;
166 page_bh->b_bdev = bh->b_bdev;
167 page_bh->b_blocknr = bh->b_blocknr;
168 break;
170 page_bh = page_bh->b_this_page;
171 block++;
172 } while (page_bh != head);
176 * This is the worker routine which does all the work of mapping the disk
177 * blocks and constructs largest possible bios, submits them for IO if the
178 * blocks are not contiguous on the disk.
180 * We pass a buffer_head back and forth and use its buffer_mapped() flag to
181 * represent the validity of its disk mapping and to decide when to do the next
182 * get_block() call.
184 static struct bio *
185 do_mpage_readpage(struct bio *bio, struct page *page, unsigned nr_pages,
186 sector_t *last_block_in_bio, struct buffer_head *map_bh,
187 unsigned long *first_logical_block, get_block_t get_block)
189 #if 0 // mask by Victor Yu. 02-12-2007
190 struct inode *inode = page->mapping->host;
191 #else
192 struct inode *inode = page->u.xx.mapping->host;
193 #endif
194 const unsigned blkbits = inode->i_blkbits;
195 const unsigned blocks_per_page = PAGE_CACHE_SIZE >> blkbits;
196 const unsigned blocksize = 1 << blkbits;
197 sector_t block_in_file;
198 sector_t last_block;
199 sector_t last_block_in_file;
200 sector_t blocks[MAX_BUF_PER_PAGE];
201 unsigned page_block;
202 unsigned first_hole = blocks_per_page;
203 struct block_device *bdev = NULL;
204 int length;
205 int fully_mapped = 1;
206 unsigned nblocks;
207 unsigned relative_block;
209 if (page_has_buffers(page))
210 goto confused;
212 block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits);
213 last_block = block_in_file + nr_pages * blocks_per_page;
214 last_block_in_file = (i_size_read(inode) + blocksize - 1) >> blkbits;
215 if (last_block > last_block_in_file)
216 last_block = last_block_in_file;
217 page_block = 0;
220 * Map blocks using the result from the previous get_blocks call first.
222 nblocks = map_bh->b_size >> blkbits;
223 if (buffer_mapped(map_bh) && block_in_file > *first_logical_block &&
224 block_in_file < (*first_logical_block + nblocks)) {
225 unsigned map_offset = block_in_file - *first_logical_block;
226 unsigned last = nblocks - map_offset;
228 for (relative_block = 0; ; relative_block++) {
229 if (relative_block == last) {
230 clear_buffer_mapped(map_bh);
231 break;
233 if (page_block == blocks_per_page)
234 break;
235 blocks[page_block] = map_bh->b_blocknr + map_offset +
236 relative_block;
237 page_block++;
238 block_in_file++;
240 bdev = map_bh->b_bdev;
244 * Then do more get_blocks calls until we are done with this page.
246 map_bh->b_page = page;
247 while (page_block < blocks_per_page) {
248 map_bh->b_state = 0;
249 map_bh->b_size = 0;
251 if (block_in_file < last_block) {
252 map_bh->b_size = (last_block-block_in_file) << blkbits;
253 if (get_block(inode, block_in_file, map_bh, 0))
254 goto confused;
255 *first_logical_block = block_in_file;
258 if (!buffer_mapped(map_bh)) {
259 fully_mapped = 0;
260 if (first_hole == blocks_per_page)
261 first_hole = page_block;
262 page_block++;
263 block_in_file++;
264 clear_buffer_mapped(map_bh);
265 continue;
268 /* some filesystems will copy data into the page during
269 * the get_block call, in which case we don't want to
270 * read it again. map_buffer_to_page copies the data
271 * we just collected from get_block into the page's buffers
272 * so readpage doesn't have to repeat the get_block call
274 if (buffer_uptodate(map_bh)) {
275 map_buffer_to_page(page, map_bh, page_block);
276 goto confused;
279 if (first_hole != blocks_per_page)
280 goto confused; /* hole -> non-hole */
282 /* Contiguous blocks? */
283 if (page_block && blocks[page_block-1] != map_bh->b_blocknr-1)
284 goto confused;
285 nblocks = map_bh->b_size >> blkbits;
286 for (relative_block = 0; ; relative_block++) {
287 if (relative_block == nblocks) {
288 clear_buffer_mapped(map_bh);
289 break;
290 } else if (page_block == blocks_per_page)
291 break;
292 blocks[page_block] = map_bh->b_blocknr+relative_block;
293 page_block++;
294 block_in_file++;
296 bdev = map_bh->b_bdev;
299 if (first_hole != blocks_per_page) {
300 char *kaddr = kmap_atomic(page, KM_USER0);
301 memset(kaddr + (first_hole << blkbits), 0,
302 PAGE_CACHE_SIZE - (first_hole << blkbits));
303 flush_dcache_page(page);
304 kunmap_atomic(kaddr, KM_USER0);
305 if (first_hole == 0) {
306 SetPageUptodate(page);
307 unlock_page(page);
308 goto out;
310 } else if (fully_mapped) {
311 SetPageMappedToDisk(page);
315 * This page will go to BIO. Do we need to send this BIO off first?
317 if (bio && (*last_block_in_bio != blocks[0] - 1))
318 bio = mpage_bio_submit(READ, bio);
320 alloc_new:
321 if (bio == NULL) {
322 bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
323 min_t(int, nr_pages, bio_get_nr_vecs(bdev)),
324 GFP_KERNEL);
325 if (bio == NULL)
326 goto confused;
329 length = first_hole << blkbits;
330 if (bio_add_page(bio, page, length, 0) < length) {
331 bio = mpage_bio_submit(READ, bio);
332 goto alloc_new;
335 if (buffer_boundary(map_bh) || (first_hole != blocks_per_page))
336 bio = mpage_bio_submit(READ, bio);
337 else
338 *last_block_in_bio = blocks[blocks_per_page - 1];
339 out:
340 return bio;
342 confused:
343 if (bio)
344 bio = mpage_bio_submit(READ, bio);
345 if (!PageUptodate(page))
346 block_read_full_page(page, get_block);
347 else
348 unlock_page(page);
349 goto out;
353 * mpage_readpages - populate an address space with some pages, and
354 * start reads against them.
356 * @mapping: the address_space
357 * @pages: The address of a list_head which contains the target pages. These
358 * pages have their ->index populated and are otherwise uninitialised.
360 * The page at @pages->prev has the lowest file offset, and reads should be
361 * issued in @pages->prev to @pages->next order.
363 * @nr_pages: The number of pages at *@pages
364 * @get_block: The filesystem's block mapper function.
366 * This function walks the pages and the blocks within each page, building and
367 * emitting large BIOs.
369 * If anything unusual happens, such as:
371 * - encountering a page which has buffers
372 * - encountering a page which has a non-hole after a hole
373 * - encountering a page with non-contiguous blocks
375 * then this code just gives up and calls the buffer_head-based read function.
376 * It does handle a page which has holes at the end - that is a common case:
377 * the end-of-file on blocksize < PAGE_CACHE_SIZE setups.
379 * BH_Boundary explanation:
381 * There is a problem. The mpage read code assembles several pages, gets all
382 * their disk mappings, and then submits them all. That's fine, but obtaining
383 * the disk mappings may require I/O. Reads of indirect blocks, for example.
385 * So an mpage read of the first 16 blocks of an ext2 file will cause I/O to be
386 * submitted in the following order:
387 * 12 0 1 2 3 4 5 6 7 8 9 10 11 13 14 15 16
388 * because the indirect block has to be read to get the mappings of blocks
389 * 13,14,15,16. Obviously, this impacts performance.
391 * So what we do it to allow the filesystem's get_block() function to set
392 * BH_Boundary when it maps block 11. BH_Boundary says: mapping of the block
393 * after this one will require I/O against a block which is probably close to
394 * this one. So you should push what I/O you have currently accumulated.
396 * This all causes the disk requests to be issued in the correct order.
399 mpage_readpages(struct address_space *mapping, struct list_head *pages,
400 unsigned nr_pages, get_block_t get_block)
402 struct bio *bio = NULL;
403 unsigned page_idx;
404 sector_t last_block_in_bio = 0;
405 struct pagevec lru_pvec;
406 struct buffer_head map_bh;
407 unsigned long first_logical_block = 0;
409 clear_buffer_mapped(&map_bh);
410 pagevec_init(&lru_pvec, 0);
411 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
412 struct page *page = list_entry(pages->prev, struct page, lru);
414 prefetchw(&page->flags);
415 list_del(&page->lru);
416 if (!add_to_page_cache(page, mapping,
417 page->index, GFP_KERNEL)) {
418 bio = do_mpage_readpage(bio, page,
419 nr_pages - page_idx,
420 &last_block_in_bio, &map_bh,
421 &first_logical_block,
422 get_block);
423 if (!pagevec_add(&lru_pvec, page))
424 __pagevec_lru_add(&lru_pvec);
425 } else {
426 page_cache_release(page);
429 pagevec_lru_add(&lru_pvec);
430 BUG_ON(!list_empty(pages));
431 if (bio)
432 mpage_bio_submit(READ, bio);
433 return 0;
435 EXPORT_SYMBOL(mpage_readpages);
438 * This isn't called much at all
440 int mpage_readpage(struct page *page, get_block_t get_block)
442 struct bio *bio = NULL;
443 sector_t last_block_in_bio = 0;
444 struct buffer_head map_bh;
445 unsigned long first_logical_block = 0;
447 clear_buffer_mapped(&map_bh);
448 bio = do_mpage_readpage(bio, page, 1, &last_block_in_bio,
449 &map_bh, &first_logical_block, get_block);
450 if (bio)
451 mpage_bio_submit(READ, bio);
452 return 0;
454 EXPORT_SYMBOL(mpage_readpage);
457 * Writing is not so simple.
459 * If the page has buffers then they will be used for obtaining the disk
460 * mapping. We only support pages which are fully mapped-and-dirty, with a
461 * special case for pages which are unmapped at the end: end-of-file.
463 * If the page has no buffers (preferred) then the page is mapped here.
465 * If all blocks are found to be contiguous then the page can go into the
466 * BIO. Otherwise fall back to the mapping's writepage().
468 * FIXME: This code wants an estimate of how many pages are still to be
469 * written, so it can intelligently allocate a suitably-sized BIO. For now,
470 * just allocate full-size (16-page) BIOs.
472 static struct bio *
473 __mpage_writepage(struct bio *bio, struct page *page, get_block_t get_block,
474 sector_t *last_block_in_bio, int *ret, struct writeback_control *wbc,
475 writepage_t writepage_fn)
477 #if 0 // mask by Victor Yu. 02-12-2007
478 struct address_space *mapping = page->mapping;
479 struct inode *inode = page->mapping->host;
480 #else
481 struct address_space *mapping = page->u.xx.mapping;
482 struct inode *inode = page->u.xx.mapping->host;
483 #endif
484 const unsigned blkbits = inode->i_blkbits;
485 unsigned long end_index;
486 const unsigned blocks_per_page = PAGE_CACHE_SIZE >> blkbits;
487 sector_t last_block;
488 sector_t block_in_file;
489 sector_t blocks[MAX_BUF_PER_PAGE];
490 unsigned page_block;
491 unsigned first_unmapped = blocks_per_page;
492 struct block_device *bdev = NULL;
493 int boundary = 0;
494 sector_t boundary_block = 0;
495 struct block_device *boundary_bdev = NULL;
496 int length;
497 struct buffer_head map_bh;
498 loff_t i_size = i_size_read(inode);
500 if (page_has_buffers(page)) {
501 struct buffer_head *head = page_buffers(page);
502 struct buffer_head *bh = head;
504 /* If they're all mapped and dirty, do it */
505 page_block = 0;
506 do {
507 BUG_ON(buffer_locked(bh));
508 if (!buffer_mapped(bh)) {
510 * unmapped dirty buffers are created by
511 * __set_page_dirty_buffers -> mmapped data
513 if (buffer_dirty(bh))
514 goto confused;
515 if (first_unmapped == blocks_per_page)
516 first_unmapped = page_block;
517 continue;
520 if (first_unmapped != blocks_per_page)
521 goto confused; /* hole -> non-hole */
523 if (!buffer_dirty(bh) || !buffer_uptodate(bh))
524 goto confused;
525 if (page_block) {
526 if (bh->b_blocknr != blocks[page_block-1] + 1)
527 goto confused;
529 blocks[page_block++] = bh->b_blocknr;
530 boundary = buffer_boundary(bh);
531 if (boundary) {
532 boundary_block = bh->b_blocknr;
533 boundary_bdev = bh->b_bdev;
535 bdev = bh->b_bdev;
536 } while ((bh = bh->b_this_page) != head);
538 if (first_unmapped)
539 goto page_is_mapped;
542 * Page has buffers, but they are all unmapped. The page was
543 * created by pagein or read over a hole which was handled by
544 * block_read_full_page(). If this address_space is also
545 * using mpage_readpages then this can rarely happen.
547 goto confused;
551 * The page has no buffers: map it to disk
553 BUG_ON(!PageUptodate(page));
554 block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits);
555 last_block = (i_size - 1) >> blkbits;
556 map_bh.b_page = page;
557 for (page_block = 0; page_block < blocks_per_page; ) {
559 map_bh.b_state = 0;
560 map_bh.b_size = 1 << blkbits;
561 if (get_block(inode, block_in_file, &map_bh, 1))
562 goto confused;
563 if (buffer_new(&map_bh))
564 unmap_underlying_metadata(map_bh.b_bdev,
565 map_bh.b_blocknr);
566 if (buffer_boundary(&map_bh)) {
567 boundary_block = map_bh.b_blocknr;
568 boundary_bdev = map_bh.b_bdev;
570 if (page_block) {
571 if (map_bh.b_blocknr != blocks[page_block-1] + 1)
572 goto confused;
574 blocks[page_block++] = map_bh.b_blocknr;
575 boundary = buffer_boundary(&map_bh);
576 bdev = map_bh.b_bdev;
577 if (block_in_file == last_block)
578 break;
579 block_in_file++;
581 BUG_ON(page_block == 0);
583 first_unmapped = page_block;
585 page_is_mapped:
586 end_index = i_size >> PAGE_CACHE_SHIFT;
587 if (page->index >= end_index) {
589 * The page straddles i_size. It must be zeroed out on each
590 * and every writepage invokation because it may be mmapped.
591 * "A file is mapped in multiples of the page size. For a file
592 * that is not a multiple of the page size, the remaining memory
593 * is zeroed when mapped, and writes to that region are not
594 * written out to the file."
596 unsigned offset = i_size & (PAGE_CACHE_SIZE - 1);
597 char *kaddr;
599 if (page->index > end_index || !offset)
600 goto confused;
601 kaddr = kmap_atomic(page, KM_USER0);
602 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
603 flush_dcache_page(page);
604 kunmap_atomic(kaddr, KM_USER0);
608 * This page will go to BIO. Do we need to send this BIO off first?
610 if (bio && *last_block_in_bio != blocks[0] - 1)
611 bio = mpage_bio_submit(WRITE, bio);
613 alloc_new:
614 if (bio == NULL) {
615 bio = mpage_alloc(bdev, blocks[0] << (blkbits - 9),
616 bio_get_nr_vecs(bdev), GFP_NOFS|__GFP_HIGH);
617 if (bio == NULL)
618 goto confused;
622 * Must try to add the page before marking the buffer clean or
623 * the confused fail path above (OOM) will be very confused when
624 * it finds all bh marked clean (i.e. it will not write anything)
626 length = first_unmapped << blkbits;
627 if (bio_add_page(bio, page, length, 0) < length) {
628 bio = mpage_bio_submit(WRITE, bio);
629 goto alloc_new;
633 * OK, we have our BIO, so we can now mark the buffers clean. Make
634 * sure to only clean buffers which we know we'll be writing.
636 if (page_has_buffers(page)) {
637 struct buffer_head *head = page_buffers(page);
638 struct buffer_head *bh = head;
639 unsigned buffer_counter = 0;
641 do {
642 if (buffer_counter++ == first_unmapped)
643 break;
644 clear_buffer_dirty(bh);
645 bh = bh->b_this_page;
646 } while (bh != head);
649 * we cannot drop the bh if the page is not uptodate
650 * or a concurrent readpage would fail to serialize with the bh
651 * and it would read from disk before we reach the platter.
653 if (buffer_heads_over_limit && PageUptodate(page))
654 try_to_free_buffers(page);
657 BUG_ON(PageWriteback(page));
658 set_page_writeback(page);
659 unlock_page(page);
660 if (boundary || (first_unmapped != blocks_per_page)) {
661 bio = mpage_bio_submit(WRITE, bio);
662 if (boundary_block) {
663 write_boundary_block(boundary_bdev,
664 boundary_block, 1 << blkbits);
666 } else {
667 *last_block_in_bio = blocks[blocks_per_page - 1];
669 goto out;
671 confused:
672 if (bio)
673 bio = mpage_bio_submit(WRITE, bio);
675 if (writepage_fn) {
676 *ret = (*writepage_fn)(page, wbc);
677 } else {
678 *ret = -EAGAIN;
679 goto out;
682 * The caller has a ref on the inode, so *mapping is stable
684 if (*ret) {
685 if (*ret == -ENOSPC)
686 set_bit(AS_ENOSPC, &mapping->flags);
687 else
688 set_bit(AS_EIO, &mapping->flags);
690 out:
691 return bio;
695 * mpage_writepages - walk the list of dirty pages of the given
696 * address space and writepage() all of them.
698 * @mapping: address space structure to write
699 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
700 * @get_block: the filesystem's block mapper function.
701 * If this is NULL then use a_ops->writepage. Otherwise, go
702 * direct-to-BIO.
704 * This is a library function, which implements the writepages()
705 * address_space_operation.
707 * If a page is already under I/O, generic_writepages() skips it, even
708 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
709 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
710 * and msync() need to guarantee that all the data which was dirty at the time
711 * the call was made get new I/O started against them. If wbc->sync_mode is
712 * WB_SYNC_ALL then we were called for data integrity and we must wait for
713 * existing IO to complete.
715 * If you fix this you should check generic_writepages() also!
718 mpage_writepages(struct address_space *mapping,
719 struct writeback_control *wbc, get_block_t get_block)
721 struct backing_dev_info *bdi = mapping->backing_dev_info;
722 struct bio *bio = NULL;
723 sector_t last_block_in_bio = 0;
724 int ret = 0;
725 int done = 0;
726 int (*writepage)(struct page *page, struct writeback_control *wbc);
727 struct pagevec pvec;
728 int nr_pages;
729 pgoff_t index;
730 pgoff_t end; /* Inclusive */
731 int scanned = 0;
732 int range_whole = 0;
734 if (wbc->nonblocking && bdi_write_congested(bdi)) {
735 wbc->encountered_congestion = 1;
736 return 0;
739 writepage = NULL;
740 if (get_block == NULL)
741 writepage = mapping->a_ops->writepage;
743 pagevec_init(&pvec, 0);
744 if (wbc->range_cyclic) {
745 index = mapping->writeback_index; /* Start from prev offset */
746 end = -1;
747 } else {
748 index = wbc->range_start >> PAGE_CACHE_SHIFT;
749 end = wbc->range_end >> PAGE_CACHE_SHIFT;
750 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
751 range_whole = 1;
752 scanned = 1;
754 retry:
755 while (!done && (index <= end) &&
756 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
757 PAGECACHE_TAG_DIRTY,
758 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
759 unsigned i;
761 scanned = 1;
762 for (i = 0; i < nr_pages; i++) {
763 struct page *page = pvec.pages[i];
766 * At this point we hold neither mapping->tree_lock nor
767 * lock on the page itself: the page may be truncated or
768 * invalidated (changing page->mapping to NULL), or even
769 * swizzled back from swapper_space to tmpfs file
770 * mapping
773 lock_page(page);
775 #if 0 // mask by Victor Yu. 02-12-2007
776 if (unlikely(page->mapping != mapping)) {
777 #else
778 if (unlikely(page->u.xx.mapping != mapping)) {
779 #endif
780 unlock_page(page);
781 continue;
784 if (!wbc->range_cyclic && page->index > end) {
785 done = 1;
786 unlock_page(page);
787 continue;
790 if (wbc->sync_mode != WB_SYNC_NONE)
791 wait_on_page_writeback(page);
793 if (PageWriteback(page) ||
794 !clear_page_dirty_for_io(page)) {
795 unlock_page(page);
796 continue;
799 if (writepage) {
800 ret = (*writepage)(page, wbc);
801 if (ret) {
802 if (ret == -ENOSPC)
803 set_bit(AS_ENOSPC,
804 &mapping->flags);
805 else
806 set_bit(AS_EIO,
807 &mapping->flags);
809 } else {
810 #if 0 // mask by Victor Yu. 02-12-2007
811 bio = __mpage_writepage(bio, page, get_block,
812 &last_block_in_bio, &ret, wbc,
813 page->mapping->a_ops->writepage);
814 #else
815 bio = __mpage_writepage(bio, page, get_block,
816 &last_block_in_bio, &ret, wbc,
817 page->u.xx.mapping->a_ops->writepage);
818 #endif
820 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE))
821 unlock_page(page);
822 if (ret || (--(wbc->nr_to_write) <= 0))
823 done = 1;
824 if (wbc->nonblocking && bdi_write_congested(bdi)) {
825 wbc->encountered_congestion = 1;
826 done = 1;
829 pagevec_release(&pvec);
830 cond_resched();
832 if (!scanned && !done) {
834 * We hit the last page and there is more work to be done: wrap
835 * back to the start of the file
837 scanned = 1;
838 index = 0;
839 goto retry;
841 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
842 mapping->writeback_index = index;
843 if (bio)
844 mpage_bio_submit(WRITE, bio);
845 return ret;
847 EXPORT_SYMBOL(mpage_writepages);
849 int mpage_writepage(struct page *page, get_block_t get_block,
850 struct writeback_control *wbc)
852 int ret = 0;
853 struct bio *bio;
854 sector_t last_block_in_bio = 0;
856 bio = __mpage_writepage(NULL, page, get_block,
857 &last_block_in_bio, &ret, wbc, NULL);
858 if (bio)
859 mpage_bio_submit(WRITE, bio);
861 return ret;
863 EXPORT_SYMBOL(mpage_writepage);