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