~ubuntu-branches/ubuntu/precise/linux-ti-omap4/precise

« back to all changes in this revision

Viewing changes to fs/btrfs/extent_io.c

  • Committer: Bazaar Package Importer
  • Author(s): Paolo Pisati
  • Date: 2011-06-29 15:23:51 UTC
  • mfrom: (26.1.1 natty-proposed)
  • Revision ID: james.westby@ubuntu.com-20110629152351-xs96tm303d95rpbk
Tags: 3.0.0-1200.2
* Rebased against 3.0.0-6.7
* BSP from TI based on 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
#include <linux/swap.h>
11
11
#include <linux/writeback.h>
12
12
#include <linux/pagevec.h>
 
13
#include <linux/prefetch.h>
 
14
#include <linux/cleancache.h>
13
15
#include "extent_io.h"
14
16
#include "extent_map.h"
15
17
#include "compat.h"
101
103
}
102
104
 
103
105
void extent_io_tree_init(struct extent_io_tree *tree,
104
 
                          struct address_space *mapping, gfp_t mask)
 
106
                         struct address_space *mapping)
105
107
{
106
108
        tree->state = RB_ROOT;
107
109
        INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
439
441
        return ret;
440
442
}
441
443
 
 
444
static struct extent_state *
 
445
alloc_extent_state_atomic(struct extent_state *prealloc)
 
446
{
 
447
        if (!prealloc)
 
448
                prealloc = alloc_extent_state(GFP_ATOMIC);
 
449
 
 
450
        return prealloc;
 
451
}
 
452
 
442
453
/*
443
454
 * clear some bits on a range in the tree.  This may require splitting
444
455
 * or inserting elements in the tree, so the gfp mask is used to
529
540
         */
530
541
 
531
542
        if (state->start < start) {
532
 
                if (!prealloc)
533
 
                        prealloc = alloc_extent_state(GFP_ATOMIC);
 
543
                prealloc = alloc_extent_state_atomic(prealloc);
 
544
                BUG_ON(!prealloc);
534
545
                err = split_state(tree, state, prealloc, start);
535
546
                BUG_ON(err == -EEXIST);
536
547
                prealloc = NULL;
551
562
         * on the first half
552
563
         */
553
564
        if (state->start <= end && state->end > end) {
554
 
                if (!prealloc)
555
 
                        prealloc = alloc_extent_state(GFP_ATOMIC);
 
565
                prealloc = alloc_extent_state_atomic(prealloc);
 
566
                BUG_ON(!prealloc);
556
567
                err = split_state(tree, state, prealloc, end + 1);
557
568
                BUG_ON(err == -EEXIST);
558
569
                if (wake)
690
701
        }
691
702
}
692
703
 
 
704
static void uncache_state(struct extent_state **cached_ptr)
 
705
{
 
706
        if (cached_ptr && (*cached_ptr)) {
 
707
                struct extent_state *state = *cached_ptr;
 
708
                *cached_ptr = NULL;
 
709
                free_extent_state(state);
 
710
        }
 
711
}
 
712
 
693
713
/*
694
714
 * set some bits on a range in the tree.  This may require allocations or
695
715
 * sleeping, so the gfp mask is used to indicate what is allowed.
716
736
again:
717
737
        if (!prealloc && (mask & __GFP_WAIT)) {
718
738
                prealloc = alloc_extent_state(mask);
719
 
                if (!prealloc)
720
 
                        return -ENOMEM;
 
739
                BUG_ON(!prealloc);
721
740
        }
722
741
 
723
742
        spin_lock(&tree->lock);
734
753
         */
735
754
        node = tree_search(tree, start);
736
755
        if (!node) {
 
756
                prealloc = alloc_extent_state_atomic(prealloc);
 
757
                BUG_ON(!prealloc);
737
758
                err = insert_state(tree, prealloc, start, end, &bits);
738
759
                prealloc = NULL;
739
760
                BUG_ON(err == -EEXIST);
762
783
                if (err)
763
784
                        goto out;
764
785
 
 
786
                next_node = rb_next(node);
765
787
                cache_state(state, cached_state);
766
788
                merge_state(tree, state);
767
789
                if (last_end == (u64)-1)
768
790
                        goto out;
769
791
 
770
792
                start = last_end + 1;
771
 
                if (start < end && prealloc && !need_resched()) {
772
 
                        next_node = rb_next(node);
773
 
                        if (next_node) {
774
 
                                state = rb_entry(next_node, struct extent_state,
775
 
                                                 rb_node);
776
 
                                if (state->start == start)
777
 
                                        goto hit_next;
778
 
                        }
 
793
                if (next_node && start < end && prealloc && !need_resched()) {
 
794
                        state = rb_entry(next_node, struct extent_state,
 
795
                                         rb_node);
 
796
                        if (state->start == start)
 
797
                                goto hit_next;
779
798
                }
780
799
                goto search_again;
781
800
        }
802
821
                        err = -EEXIST;
803
822
                        goto out;
804
823
                }
 
824
 
 
825
                prealloc = alloc_extent_state_atomic(prealloc);
 
826
                BUG_ON(!prealloc);
805
827
                err = split_state(tree, state, prealloc, start);
806
828
                BUG_ON(err == -EEXIST);
807
829
                prealloc = NULL;
832
854
                        this_end = end;
833
855
                else
834
856
                        this_end = last_start - 1;
 
857
 
 
858
                prealloc = alloc_extent_state_atomic(prealloc);
 
859
                BUG_ON(!prealloc);
 
860
 
 
861
                /*
 
862
                 * Avoid to free 'prealloc' if it can be merged with
 
863
                 * the later extent.
 
864
                 */
 
865
                atomic_inc(&prealloc->refs);
835
866
                err = insert_state(tree, prealloc, start, this_end,
836
867
                                   &bits);
837
868
                BUG_ON(err == -EEXIST);
838
869
                if (err) {
 
870
                        free_extent_state(prealloc);
839
871
                        prealloc = NULL;
840
872
                        goto out;
841
873
                }
842
874
                cache_state(prealloc, cached_state);
 
875
                free_extent_state(prealloc);
843
876
                prealloc = NULL;
844
877
                start = this_end + 1;
845
878
                goto search_again;
856
889
                        err = -EEXIST;
857
890
                        goto out;
858
891
                }
 
892
 
 
893
                prealloc = alloc_extent_state_atomic(prealloc);
 
894
                BUG_ON(!prealloc);
859
895
                err = split_state(tree, state, prealloc, end + 1);
860
896
                BUG_ON(err == -EEXIST);
861
897
 
932
968
                              NULL, mask);
933
969
}
934
970
 
935
 
static int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
936
 
                       gfp_t mask)
937
 
{
938
 
        return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0,
939
 
                                NULL, mask);
940
 
}
941
 
 
942
971
int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
943
 
                        gfp_t mask)
 
972
                        struct extent_state **cached_state, gfp_t mask)
944
973
{
945
 
        return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL,
946
 
                              NULL, mask);
 
974
        return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
 
975
                              NULL, cached_state, mask);
947
976
}
948
977
 
949
978
static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
954
983
                                cached_state, mask);
955
984
}
956
985
 
957
 
int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end)
958
 
{
959
 
        return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK);
960
 
}
961
 
 
962
986
/*
963
987
 * either insert or lock state struct between start and end use mask to tell
964
988
 * us if waiting is desired.
1012
1036
                                mask);
1013
1037
}
1014
1038
 
1015
 
int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1016
 
                  gfp_t mask)
 
1039
int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
1017
1040
{
1018
1041
        return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1019
1042
                                mask);
1020
1043
}
1021
1044
 
1022
1045
/*
1023
 
 * helper function to set pages and extents in the tree dirty
1024
 
 */
1025
 
int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end)
1026
 
{
1027
 
        unsigned long index = start >> PAGE_CACHE_SHIFT;
1028
 
        unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1029
 
        struct page *page;
1030
 
 
1031
 
        while (index <= end_index) {
1032
 
                page = find_get_page(tree->mapping, index);
1033
 
                BUG_ON(!page);
1034
 
                __set_page_dirty_nobuffers(page);
1035
 
                page_cache_release(page);
1036
 
                index++;
1037
 
        }
1038
 
        return 0;
1039
 
}
1040
 
 
1041
 
/*
1042
1046
 * helper function to set both pages and extents in the tree writeback
1043
1047
 */
1044
1048
static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
1472
1476
                        if (total_bytes >= max_bytes)
1473
1477
                                break;
1474
1478
                        if (!found) {
1475
 
                                *start = state->start;
 
1479
                                *start = max(cur_start, state->start);
1476
1480
                                found = 1;
1477
1481
                        }
1478
1482
                        last = state->end;
1735
1739
 
1736
1740
        do {
1737
1741
                struct page *page = bvec->bv_page;
 
1742
                struct extent_state *cached = NULL;
 
1743
                struct extent_state *state;
 
1744
 
1738
1745
                tree = &BTRFS_I(page->mapping->host)->io_tree;
1739
1746
 
1740
1747
                start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1749
1756
                if (++bvec <= bvec_end)
1750
1757
                        prefetchw(&bvec->bv_page->flags);
1751
1758
 
 
1759
                spin_lock(&tree->lock);
 
1760
                state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
 
1761
                if (state && state->start == start) {
 
1762
                        /*
 
1763
                         * take a reference on the state, unlock will drop
 
1764
                         * the ref
 
1765
                         */
 
1766
                        cache_state(state, &cached);
 
1767
                }
 
1768
                spin_unlock(&tree->lock);
 
1769
 
1752
1770
                if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
1753
1771
                        ret = tree->ops->readpage_end_io_hook(page, start, end,
1754
 
                                                              NULL);
 
1772
                                                              state);
1755
1773
                        if (ret)
1756
1774
                                uptodate = 0;
1757
1775
                }
1764
1782
                                        test_bit(BIO_UPTODATE, &bio->bi_flags);
1765
1783
                                if (err)
1766
1784
                                        uptodate = 0;
 
1785
                                uncache_state(&cached);
1767
1786
                                continue;
1768
1787
                        }
1769
1788
                }
1770
1789
 
1771
1790
                if (uptodate) {
1772
 
                        set_extent_uptodate(tree, start, end,
 
1791
                        set_extent_uptodate(tree, start, end, &cached,
1773
1792
                                            GFP_ATOMIC);
1774
1793
                }
1775
 
                unlock_extent(tree, start, end, GFP_ATOMIC);
 
1794
                unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
1776
1795
 
1777
1796
                if (whole_page) {
1778
1797
                        if (uptodate) {
1796
1815
        bio_put(bio);
1797
1816
}
1798
1817
 
1799
 
/*
1800
 
 * IO done from prepare_write is pretty simple, we just unlock
1801
 
 * the structs in the extent tree when done, and set the uptodate bits
1802
 
 * as appropriate.
1803
 
 */
1804
 
static void end_bio_extent_preparewrite(struct bio *bio, int err)
1805
 
{
1806
 
        const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1807
 
        struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1808
 
        struct extent_io_tree *tree;
1809
 
        u64 start;
1810
 
        u64 end;
1811
 
 
1812
 
        do {
1813
 
                struct page *page = bvec->bv_page;
1814
 
                tree = &BTRFS_I(page->mapping->host)->io_tree;
1815
 
 
1816
 
                start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1817
 
                        bvec->bv_offset;
1818
 
                end = start + bvec->bv_len - 1;
1819
 
 
1820
 
                if (--bvec >= bio->bi_io_vec)
1821
 
                        prefetchw(&bvec->bv_page->flags);
1822
 
 
1823
 
                if (uptodate) {
1824
 
                        set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1825
 
                } else {
1826
 
                        ClearPageUptodate(page);
1827
 
                        SetPageError(page);
1828
 
                }
1829
 
 
1830
 
                unlock_extent(tree, start, end, GFP_ATOMIC);
1831
 
 
1832
 
        } while (bvec >= bio->bi_io_vec);
1833
 
 
1834
 
        bio_put(bio);
1835
 
}
1836
 
 
1837
1818
struct bio *
1838
1819
btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1839
1820
                gfp_t gfp_flags)
1982
1963
        struct btrfs_ordered_extent *ordered;
1983
1964
        int ret;
1984
1965
        int nr = 0;
1985
 
        size_t page_offset = 0;
 
1966
        size_t pg_offset = 0;
1986
1967
        size_t iosize;
1987
1968
        size_t disk_io_size;
1988
1969
        size_t blocksize = inode->i_sb->s_blocksize;
1990
1971
 
1991
1972
        set_page_extent_mapped(page);
1992
1973
 
 
1974
        if (!PageUptodate(page)) {
 
1975
                if (cleancache_get_page(page) == 0) {
 
1976
                        BUG_ON(blocksize != PAGE_SIZE);
 
1977
                        goto out;
 
1978
                }
 
1979
        }
 
1980
 
1993
1981
        end = page_end;
1994
1982
        while (1) {
1995
1983
                lock_extent(tree, start, end, GFP_NOFS);
2016
2004
        while (cur <= end) {
2017
2005
                if (cur >= last_byte) {
2018
2006
                        char *userpage;
2019
 
                        iosize = PAGE_CACHE_SIZE - page_offset;
 
2007
                        struct extent_state *cached = NULL;
 
2008
 
 
2009
                        iosize = PAGE_CACHE_SIZE - pg_offset;
2020
2010
                        userpage = kmap_atomic(page, KM_USER0);
2021
 
                        memset(userpage + page_offset, 0, iosize);
 
2011
                        memset(userpage + pg_offset, 0, iosize);
2022
2012
                        flush_dcache_page(page);
2023
2013
                        kunmap_atomic(userpage, KM_USER0);
2024
2014
                        set_extent_uptodate(tree, cur, cur + iosize - 1,
2025
 
                                            GFP_NOFS);
2026
 
                        unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
 
2015
                                            &cached, GFP_NOFS);
 
2016
                        unlock_extent_cached(tree, cur, cur + iosize - 1,
 
2017
                                             &cached, GFP_NOFS);
2027
2018
                        break;
2028
2019
                }
2029
 
                em = get_extent(inode, page, page_offset, cur,
 
2020
                em = get_extent(inode, page, pg_offset, cur,
2030
2021
                                end - cur + 1, 0);
2031
 
                if (IS_ERR(em) || !em) {
 
2022
                if (IS_ERR_OR_NULL(em)) {
2032
2023
                        SetPageError(page);
2033
2024
                        unlock_extent(tree, cur, end, GFP_NOFS);
2034
2025
                        break;
2063
2054
                /* we've found a hole, just zero and go on */
2064
2055
                if (block_start == EXTENT_MAP_HOLE) {
2065
2056
                        char *userpage;
 
2057
                        struct extent_state *cached = NULL;
 
2058
 
2066
2059
                        userpage = kmap_atomic(page, KM_USER0);
2067
 
                        memset(userpage + page_offset, 0, iosize);
 
2060
                        memset(userpage + pg_offset, 0, iosize);
2068
2061
                        flush_dcache_page(page);
2069
2062
                        kunmap_atomic(userpage, KM_USER0);
2070
2063
 
2071
2064
                        set_extent_uptodate(tree, cur, cur + iosize - 1,
2072
 
                                            GFP_NOFS);
2073
 
                        unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
 
2065
                                            &cached, GFP_NOFS);
 
2066
                        unlock_extent_cached(tree, cur, cur + iosize - 1,
 
2067
                                             &cached, GFP_NOFS);
2074
2068
                        cur = cur + iosize;
2075
 
                        page_offset += iosize;
 
2069
                        pg_offset += iosize;
2076
2070
                        continue;
2077
2071
                }
2078
2072
                /* the get_extent function already copied into the page */
2081
2075
                        check_page_uptodate(tree, page);
2082
2076
                        unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2083
2077
                        cur = cur + iosize;
2084
 
                        page_offset += iosize;
 
2078
                        pg_offset += iosize;
2085
2079
                        continue;
2086
2080
                }
2087
2081
                /* we have an inline extent but it didn't get marked up
2091
2085
                        SetPageError(page);
2092
2086
                        unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2093
2087
                        cur = cur + iosize;
2094
 
                        page_offset += iosize;
 
2088
                        pg_offset += iosize;
2095
2089
                        continue;
2096
2090
                }
2097
2091
 
2104
2098
                        unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2105
2099
                        pnr -= page->index;
2106
2100
                        ret = submit_extent_page(READ, tree, page,
2107
 
                                         sector, disk_io_size, page_offset,
 
2101
                                         sector, disk_io_size, pg_offset,
2108
2102
                                         bdev, bio, pnr,
2109
2103
                                         end_bio_extent_readpage, mirror_num,
2110
2104
                                         *bio_flags,
2115
2109
                if (ret)
2116
2110
                        SetPageError(page);
2117
2111
                cur = cur + iosize;
2118
 
                page_offset += iosize;
 
2112
                pg_offset += iosize;
2119
2113
        }
 
2114
out:
2120
2115
        if (!nr) {
2121
2116
                if (!PageError(page))
2122
2117
                        SetPageUptodate(page);
2188
2183
        unsigned long nr_written = 0;
2189
2184
 
2190
2185
        if (wbc->sync_mode == WB_SYNC_ALL)
2191
 
                write_flags = WRITE_SYNC_PLUG;
 
2186
                write_flags = WRITE_SYNC;
2192
2187
        else
2193
2188
                write_flags = WRITE;
2194
2189
 
 
2190
        trace___extent_writepage(page, inode, wbc);
 
2191
 
2195
2192
        WARN_ON(!PageLocked(page));
2196
2193
        pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
2197
2194
        if (page->index > end_index ||
2308
2305
                }
2309
2306
                em = epd->get_extent(inode, page, pg_offset, cur,
2310
2307
                                     end - cur + 1, 1);
2311
 
                if (IS_ERR(em) || !em) {
 
2308
                if (IS_ERR_OR_NULL(em)) {
2312
2309
                        SetPageError(page);
2313
2310
                        break;
2314
2311
                }
2648
2645
                prefetchw(&page->flags);
2649
2646
                list_del(&page->lru);
2650
2647
                if (!add_to_page_cache_lru(page, mapping,
2651
 
                                        page->index, GFP_KERNEL)) {
 
2648
                                        page->index, GFP_NOFS)) {
2652
2649
                        __extent_read_full_page(tree, page, get_extent,
2653
2650
                                                &bio, 0, &bio_flags);
2654
2651
                }
2687
2684
}
2688
2685
 
2689
2686
/*
2690
 
 * simple commit_write call, set_range_dirty is used to mark both
2691
 
 * the pages and the extent records as dirty
2692
 
 */
2693
 
int extent_commit_write(struct extent_io_tree *tree,
2694
 
                        struct inode *inode, struct page *page,
2695
 
                        unsigned from, unsigned to)
2696
 
{
2697
 
        loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2698
 
 
2699
 
        set_page_extent_mapped(page);
2700
 
        set_page_dirty(page);
2701
 
 
2702
 
        if (pos > inode->i_size) {
2703
 
                i_size_write(inode, pos);
2704
 
                mark_inode_dirty(inode);
2705
 
        }
2706
 
        return 0;
2707
 
}
2708
 
 
2709
 
int extent_prepare_write(struct extent_io_tree *tree,
2710
 
                         struct inode *inode, struct page *page,
2711
 
                         unsigned from, unsigned to, get_extent_t *get_extent)
2712
 
{
2713
 
        u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2714
 
        u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2715
 
        u64 block_start;
2716
 
        u64 orig_block_start;
2717
 
        u64 block_end;
2718
 
        u64 cur_end;
2719
 
        struct extent_map *em;
2720
 
        unsigned blocksize = 1 << inode->i_blkbits;
2721
 
        size_t page_offset = 0;
2722
 
        size_t block_off_start;
2723
 
        size_t block_off_end;
2724
 
        int err = 0;
2725
 
        int iocount = 0;
2726
 
        int ret = 0;
2727
 
        int isnew;
2728
 
 
2729
 
        set_page_extent_mapped(page);
2730
 
 
2731
 
        block_start = (page_start + from) & ~((u64)blocksize - 1);
2732
 
        block_end = (page_start + to - 1) | (blocksize - 1);
2733
 
        orig_block_start = block_start;
2734
 
 
2735
 
        lock_extent(tree, page_start, page_end, GFP_NOFS);
2736
 
        while (block_start <= block_end) {
2737
 
                em = get_extent(inode, page, page_offset, block_start,
2738
 
                                block_end - block_start + 1, 1);
2739
 
                if (IS_ERR(em) || !em)
2740
 
                        goto err;
2741
 
 
2742
 
                cur_end = min(block_end, extent_map_end(em) - 1);
2743
 
                block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2744
 
                block_off_end = block_off_start + blocksize;
2745
 
                isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2746
 
 
2747
 
                if (!PageUptodate(page) && isnew &&
2748
 
                    (block_off_end > to || block_off_start < from)) {
2749
 
                        void *kaddr;
2750
 
 
2751
 
                        kaddr = kmap_atomic(page, KM_USER0);
2752
 
                        if (block_off_end > to)
2753
 
                                memset(kaddr + to, 0, block_off_end - to);
2754
 
                        if (block_off_start < from)
2755
 
                                memset(kaddr + block_off_start, 0,
2756
 
                                       from - block_off_start);
2757
 
                        flush_dcache_page(page);
2758
 
                        kunmap_atomic(kaddr, KM_USER0);
2759
 
                }
2760
 
                if ((em->block_start != EXTENT_MAP_HOLE &&
2761
 
                     em->block_start != EXTENT_MAP_INLINE) &&
2762
 
                    !isnew && !PageUptodate(page) &&
2763
 
                    (block_off_end > to || block_off_start < from) &&
2764
 
                    !test_range_bit(tree, block_start, cur_end,
2765
 
                                    EXTENT_UPTODATE, 1, NULL)) {
2766
 
                        u64 sector;
2767
 
                        u64 extent_offset = block_start - em->start;
2768
 
                        size_t iosize;
2769
 
                        sector = (em->block_start + extent_offset) >> 9;
2770
 
                        iosize = (cur_end - block_start + blocksize) &
2771
 
                                ~((u64)blocksize - 1);
2772
 
                        /*
2773
 
                         * we've already got the extent locked, but we
2774
 
                         * need to split the state such that our end_bio
2775
 
                         * handler can clear the lock.
2776
 
                         */
2777
 
                        set_extent_bit(tree, block_start,
2778
 
                                       block_start + iosize - 1,
2779
 
                                       EXTENT_LOCKED, 0, NULL, NULL, GFP_NOFS);
2780
 
                        ret = submit_extent_page(READ, tree, page,
2781
 
                                         sector, iosize, page_offset, em->bdev,
2782
 
                                         NULL, 1,
2783
 
                                         end_bio_extent_preparewrite, 0,
2784
 
                                         0, 0);
2785
 
                        if (ret && !err)
2786
 
                                err = ret;
2787
 
                        iocount++;
2788
 
                        block_start = block_start + iosize;
2789
 
                } else {
2790
 
                        set_extent_uptodate(tree, block_start, cur_end,
2791
 
                                            GFP_NOFS);
2792
 
                        unlock_extent(tree, block_start, cur_end, GFP_NOFS);
2793
 
                        block_start = cur_end + 1;
2794
 
                }
2795
 
                page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2796
 
                free_extent_map(em);
2797
 
        }
2798
 
        if (iocount) {
2799
 
                wait_extent_bit(tree, orig_block_start,
2800
 
                                block_end, EXTENT_LOCKED);
2801
 
        }
2802
 
        check_page_uptodate(tree, page);
2803
 
err:
2804
 
        /* FIXME, zero out newly allocated blocks on error */
2805
 
        return err;
2806
 
}
2807
 
 
2808
 
/*
2809
2687
 * a helper for releasepage, this tests for areas of the page that
2810
2688
 * are locked or under IO and drops the related state bits if it is safe
2811
2689
 * to drop the page.
2863
2741
                        len = end - start + 1;
2864
2742
                        write_lock(&map->lock);
2865
2743
                        em = lookup_extent_mapping(map, start, len);
2866
 
                        if (!em || IS_ERR(em)) {
 
2744
                        if (IS_ERR_OR_NULL(em)) {
2867
2745
                                write_unlock(&map->lock);
2868
2746
                                break;
2869
2747
                        }
2891
2769
        return try_release_extent_state(map, tree, page, mask);
2892
2770
}
2893
2771
 
2894
 
sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2895
 
                get_extent_t *get_extent)
2896
 
{
2897
 
        struct inode *inode = mapping->host;
2898
 
        struct extent_state *cached_state = NULL;
2899
 
        u64 start = iblock << inode->i_blkbits;
2900
 
        sector_t sector = 0;
2901
 
        size_t blksize = (1 << inode->i_blkbits);
2902
 
        struct extent_map *em;
2903
 
 
2904
 
        lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2905
 
                         0, &cached_state, GFP_NOFS);
2906
 
        em = get_extent(inode, NULL, 0, start, blksize, 0);
2907
 
        unlock_extent_cached(&BTRFS_I(inode)->io_tree, start,
2908
 
                             start + blksize - 1, &cached_state, GFP_NOFS);
2909
 
        if (!em || IS_ERR(em))
2910
 
                return 0;
2911
 
 
2912
 
        if (em->block_start > EXTENT_MAP_LAST_BYTE)
2913
 
                goto out;
2914
 
 
2915
 
        sector = (em->block_start + start - em->start) >> inode->i_blkbits;
2916
 
out:
2917
 
        free_extent_map(em);
2918
 
        return sector;
2919
 
}
2920
 
 
2921
2772
/*
2922
2773
 * helper function for fiemap, which doesn't want to see any holes.
2923
2774
 * This maps until we find something past 'last'
2940
2791
                        break;
2941
2792
                len = (len + sectorsize - 1) & ~(sectorsize - 1);
2942
2793
                em = get_extent(inode, NULL, 0, offset, len, 0);
2943
 
                if (!em || IS_ERR(em))
 
2794
                if (IS_ERR_OR_NULL(em))
2944
2795
                        return em;
2945
2796
 
2946
2797
                /* if this isn't a hole return it */
2994
2845
         * because there might be preallocation past i_size
2995
2846
         */
2996
2847
        ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
2997
 
                                       path, inode->i_ino, -1, 0);
 
2848
                                       path, btrfs_ino(inode), -1, 0);
2998
2849
        if (ret < 0) {
2999
2850
                btrfs_free_path(path);
3000
2851
                return ret;
3007
2858
        found_type = btrfs_key_type(&found_key);
3008
2859
 
3009
2860
        /* No extents, but there might be delalloc bits */
3010
 
        if (found_key.objectid != inode->i_ino ||
 
2861
        if (found_key.objectid != btrfs_ino(inode) ||
3011
2862
            found_type != BTRFS_EXTENT_DATA_KEY) {
3012
2863
                /* have to trust i_size as the end */
3013
2864
                last = (u64)-1;
3230
3081
 
3231
3082
struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3232
3083
                                          u64 start, unsigned long len,
3233
 
                                          struct page *page0,
3234
 
                                          gfp_t mask)
 
3084
                                          struct page *page0)
3235
3085
{
3236
3086
        unsigned long num_pages = num_extent_pages(start, len);
3237
3087
        unsigned long i;
3252
3102
        }
3253
3103
        rcu_read_unlock();
3254
3104
 
3255
 
        eb = __alloc_extent_buffer(tree, start, len, mask);
 
3105
        eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
3256
3106
        if (!eb)
3257
3107
                return NULL;
3258
3108
 
3269
3119
                i = 0;
3270
3120
        }
3271
3121
        for (; i < num_pages; i++, index++) {
3272
 
                p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
 
3122
                p = find_or_create_page(mapping, index, GFP_NOFS | __GFP_HIGHMEM);
3273
3123
                if (!p) {
3274
3124
                        WARN_ON(1);
3275
3125
                        goto free_eb;
3341
3191
}
3342
3192
 
3343
3193
struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
3344
 
                                         u64 start, unsigned long len,
3345
 
                                          gfp_t mask)
 
3194
                                         u64 start, unsigned long len)
3346
3195
{
3347
3196
        struct extent_buffer *eb;
3348
3197
 
3403
3252
        return 0;
3404
3253
}
3405
3254
 
3406
 
int wait_on_extent_buffer_writeback(struct extent_io_tree *tree,
3407
 
                                    struct extent_buffer *eb)
3408
 
{
3409
 
        return wait_on_extent_writeback(tree, eb->start,
3410
 
                                        eb->start + eb->len - 1);
3411
 
}
3412
 
 
3413
3255
int set_extent_buffer_dirty(struct extent_io_tree *tree,
3414
3256
                             struct extent_buffer *eb)
3415
3257
{
3455
3297
        num_pages = num_extent_pages(eb->start, eb->len);
3456
3298
 
3457
3299
        set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3458
 
                            GFP_NOFS);
 
3300
                            NULL, GFP_NOFS);
3459
3301
        for (i = 0; i < num_pages; i++) {
3460
3302
                page = extent_buffer_page(eb, i);
3461
3303
                if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3690
3532
                       "wanted %lu %lu\n", (unsigned long long)eb->start,
3691
3533
                       eb->len, start, min_len);
3692
3534
                WARN_ON(1);
 
3535
                return -EINVAL;
3693
3536
        }
3694
3537
 
3695
3538
        p = extent_buffer_page(eb, i);
3882
3725
        kunmap_atomic(dst_kaddr, KM_USER0);
3883
3726
}
3884
3727
 
 
3728
static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
 
3729
{
 
3730
        unsigned long distance = (src > dst) ? src - dst : dst - src;
 
3731
        return distance < len;
 
3732
}
 
3733
 
3885
3734
static void copy_pages(struct page *dst_page, struct page *src_page,
3886
3735
                       unsigned long dst_off, unsigned long src_off,
3887
3736
                       unsigned long len)
3889
3738
        char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3890
3739
        char *src_kaddr;
3891
3740
 
3892
 
        if (dst_page != src_page)
 
3741
        if (dst_page != src_page) {
3893
3742
                src_kaddr = kmap_atomic(src_page, KM_USER1);
3894
 
        else
 
3743
        } else {
3895
3744
                src_kaddr = dst_kaddr;
 
3745
                BUG_ON(areas_overlap(src_off, dst_off, len));
 
3746
        }
3896
3747
 
3897
3748
        memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3898
3749
        kunmap_atomic(dst_kaddr, KM_USER0);
3967
3818
                       "len %lu len %lu\n", dst_offset, len, dst->len);
3968
3819
                BUG_ON(1);
3969
3820
        }
3970
 
        if (dst_offset < src_offset) {
 
3821
        if (!areas_overlap(src_offset, dst_offset, len)) {
3971
3822
                memcpy_extent_buffer(dst, dst_offset, src_offset, len);
3972
3823
                return;
3973
3824
        }