aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--fs/btrfs/disk-io.c76
-rw-r--r--fs/btrfs/extent_io.c497
-rw-r--r--fs/btrfs/extent_io.h24
-rw-r--r--fs/btrfs/inode.c2
4 files changed, 390 insertions, 209 deletions
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index 0ba055e03eb8..c54aec87e89d 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -333,7 +333,7 @@ static int verify_parent_transid(struct extent_io_tree *io_tree,
333 333
334 lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1, 334 lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1,
335 0, &cached_state, GFP_NOFS); 335 0, &cached_state, GFP_NOFS);
336 if (extent_buffer_uptodate(io_tree, eb, cached_state) && 336 if (extent_buffer_uptodate(eb) &&
337 btrfs_header_generation(eb) == parent_transid) { 337 btrfs_header_generation(eb) == parent_transid) {
338 ret = 0; 338 ret = 0;
339 goto out; 339 goto out;
@@ -344,7 +344,7 @@ static int verify_parent_transid(struct extent_io_tree *io_tree,
344 (unsigned long long)parent_transid, 344 (unsigned long long)parent_transid,
345 (unsigned long long)btrfs_header_generation(eb)); 345 (unsigned long long)btrfs_header_generation(eb));
346 ret = 1; 346 ret = 1;
347 clear_extent_buffer_uptodate(io_tree, eb, &cached_state); 347 clear_extent_buffer_uptodate(eb);
348out: 348out:
349 unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1, 349 unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1,
350 &cached_state, GFP_NOFS); 350 &cached_state, GFP_NOFS);
@@ -566,7 +566,12 @@ static int btree_readpage_end_io_hook(struct page *page, u64 start, u64 end,
566 tree = &BTRFS_I(page->mapping->host)->io_tree; 566 tree = &BTRFS_I(page->mapping->host)->io_tree;
567 eb = (struct extent_buffer *)page->private; 567 eb = (struct extent_buffer *)page->private;
568 568
569 reads_done = atomic_dec_and_test(&eb->pages_reading); 569 /* the pending IO might have been the only thing that kept this buffer
570 * in memory. Make sure we have a ref for all this other checks
571 */
572 extent_buffer_get(eb);
573
574 reads_done = atomic_dec_and_test(&eb->io_pages);
570 if (!reads_done) 575 if (!reads_done)
571 goto err; 576 goto err;
572 577
@@ -606,14 +611,17 @@ static int btree_readpage_end_io_hook(struct page *page, u64 start, u64 end,
606 ret = -EIO; 611 ret = -EIO;
607 } 612 }
608 613
614 if (!ret)
615 set_extent_buffer_uptodate(eb);
609err: 616err:
610 if (test_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) { 617 if (test_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags)) {
611 clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags); 618 clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags);
612 btree_readahead_hook(root, eb, eb->start, ret); 619 btree_readahead_hook(root, eb, eb->start, ret);
613 } 620 }
614 621
615 if (ret && eb) 622 if (ret)
616 clear_extent_buffer_uptodate(tree, eb, NULL); 623 clear_extent_buffer_uptodate(eb);
624 free_extent_buffer(eb);
617out: 625out:
618 return ret; 626 return ret;
619} 627}
@@ -878,20 +886,6 @@ static int btree_migratepage(struct address_space *mapping,
878} 886}
879#endif 887#endif
880 888
881static int btree_writepage(struct page *page, struct writeback_control *wbc)
882{
883 struct extent_io_tree *tree;
884 tree = &BTRFS_I(page->mapping->host)->io_tree;
885
886 if (!(current->flags & PF_MEMALLOC)) {
887 return extent_write_full_page(tree, page,
888 btree_get_extent, wbc);
889 }
890
891 redirty_page_for_writepage(wbc, page);
892 unlock_page(page);
893 return 0;
894}
895 889
896static int btree_writepages(struct address_space *mapping, 890static int btree_writepages(struct address_space *mapping,
897 struct writeback_control *wbc) 891 struct writeback_control *wbc)
@@ -911,7 +905,7 @@ static int btree_writepages(struct address_space *mapping,
911 if (num_dirty < thresh) 905 if (num_dirty < thresh)
912 return 0; 906 return 0;
913 } 907 }
914 return extent_writepages(tree, mapping, btree_get_extent, wbc); 908 return btree_write_cache_pages(mapping, wbc);
915} 909}
916 910
917static int btree_readpage(struct file *file, struct page *page) 911static int btree_readpage(struct file *file, struct page *page)
@@ -950,15 +944,28 @@ static void btree_invalidatepage(struct page *page, unsigned long offset)
950 } 944 }
951} 945}
952 946
947static int btree_set_page_dirty(struct page *page)
948{
949 struct extent_buffer *eb;
950
951 BUG_ON(!PagePrivate(page));
952 eb = (struct extent_buffer *)page->private;
953 BUG_ON(!eb);
954 BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
955 BUG_ON(!atomic_read(&eb->refs));
956 btrfs_assert_tree_locked(eb);
957 return __set_page_dirty_nobuffers(page);
958}
959
953static const struct address_space_operations btree_aops = { 960static const struct address_space_operations btree_aops = {
954 .readpage = btree_readpage, 961 .readpage = btree_readpage,
955 .writepage = btree_writepage,
956 .writepages = btree_writepages, 962 .writepages = btree_writepages,
957 .releasepage = btree_releasepage, 963 .releasepage = btree_releasepage,
958 .invalidatepage = btree_invalidatepage, 964 .invalidatepage = btree_invalidatepage,
959#ifdef CONFIG_MIGRATION 965#ifdef CONFIG_MIGRATION
960 .migratepage = btree_migratepage, 966 .migratepage = btree_migratepage,
961#endif 967#endif
968 .set_page_dirty = btree_set_page_dirty,
962}; 969};
963 970
964int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize, 971int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize,
@@ -1001,7 +1008,7 @@ int reada_tree_block_flagged(struct btrfs_root *root, u64 bytenr, u32 blocksize,
1001 if (test_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags)) { 1008 if (test_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags)) {
1002 free_extent_buffer(buf); 1009 free_extent_buffer(buf);
1003 return -EIO; 1010 return -EIO;
1004 } else if (extent_buffer_uptodate(io_tree, buf, NULL)) { 1011 } else if (extent_buffer_uptodate(buf)) {
1005 *eb = buf; 1012 *eb = buf;
1006 } else { 1013 } else {
1007 free_extent_buffer(buf); 1014 free_extent_buffer(buf);
@@ -1054,9 +1061,6 @@ struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
1054 return NULL; 1061 return NULL;
1055 1062
1056 ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid); 1063 ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
1057
1058 if (ret == 0)
1059 set_bit(EXTENT_BUFFER_UPTODATE, &buf->bflags);
1060 return buf; 1064 return buf;
1061 1065
1062} 1066}
@@ -1064,7 +1068,6 @@ struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
1064int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root, 1068int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1065 struct extent_buffer *buf) 1069 struct extent_buffer *buf)
1066{ 1070{
1067 struct inode *btree_inode = root->fs_info->btree_inode;
1068 if (btrfs_header_generation(buf) == 1071 if (btrfs_header_generation(buf) ==
1069 root->fs_info->running_transaction->transid) { 1072 root->fs_info->running_transaction->transid) {
1070 btrfs_assert_tree_locked(buf); 1073 btrfs_assert_tree_locked(buf);
@@ -1080,8 +1083,7 @@ int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1080 1083
1081 /* ugh, clear_extent_buffer_dirty needs to lock the page */ 1084 /* ugh, clear_extent_buffer_dirty needs to lock the page */
1082 btrfs_set_lock_blocking(buf); 1085 btrfs_set_lock_blocking(buf);
1083 clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, 1086 clear_extent_buffer_dirty(buf);
1084 buf);
1085 } 1087 }
1086 return 0; 1088 return 0;
1087} 1089}
@@ -1948,6 +1950,7 @@ int open_ctree(struct super_block *sb,
1948 RB_CLEAR_NODE(&BTRFS_I(fs_info->btree_inode)->rb_node); 1950 RB_CLEAR_NODE(&BTRFS_I(fs_info->btree_inode)->rb_node);
1949 extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree, 1951 extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
1950 fs_info->btree_inode->i_mapping); 1952 fs_info->btree_inode->i_mapping);
1953 BTRFS_I(fs_info->btree_inode)->io_tree.track_uptodate = 0;
1951 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree); 1954 extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree);
1952 1955
1953 BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops; 1956 BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
@@ -3058,8 +3061,7 @@ int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
3058 int ret; 3061 int ret;
3059 struct inode *btree_inode = buf->pages[0]->mapping->host; 3062 struct inode *btree_inode = buf->pages[0]->mapping->host;
3060 3063
3061 ret = extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf, 3064 ret = extent_buffer_uptodate(buf);
3062 NULL);
3063 if (!ret) 3065 if (!ret)
3064 return ret; 3066 return ret;
3065 3067
@@ -3070,16 +3072,13 @@ int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid)
3070 3072
3071int btrfs_set_buffer_uptodate(struct extent_buffer *buf) 3073int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
3072{ 3074{
3073 struct inode *btree_inode = buf->pages[0]->mapping->host; 3075 return set_extent_buffer_uptodate(buf);
3074 return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
3075 buf);
3076} 3076}
3077 3077
3078void btrfs_mark_buffer_dirty(struct extent_buffer *buf) 3078void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
3079{ 3079{
3080 struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root; 3080 struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root;
3081 u64 transid = btrfs_header_generation(buf); 3081 u64 transid = btrfs_header_generation(buf);
3082 struct inode *btree_inode = root->fs_info->btree_inode;
3083 int was_dirty; 3082 int was_dirty;
3084 3083
3085 btrfs_assert_tree_locked(buf); 3084 btrfs_assert_tree_locked(buf);
@@ -3091,8 +3090,7 @@ void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
3091 (unsigned long long)root->fs_info->generation); 3090 (unsigned long long)root->fs_info->generation);
3092 WARN_ON(1); 3091 WARN_ON(1);
3093 } 3092 }
3094 was_dirty = set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, 3093 was_dirty = set_extent_buffer_dirty(buf);
3095 buf);
3096 if (!was_dirty) { 3094 if (!was_dirty) {
3097 spin_lock(&root->fs_info->delalloc_lock); 3095 spin_lock(&root->fs_info->delalloc_lock);
3098 root->fs_info->dirty_metadata_bytes += buf->len; 3096 root->fs_info->dirty_metadata_bytes += buf->len;
@@ -3147,11 +3145,7 @@ void __btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
3147int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid) 3145int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid)
3148{ 3146{
3149 struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root; 3147 struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root;
3150 int ret; 3148 return btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
3151 ret = btree_read_extent_buffer_pages(root, buf, 0, parent_transid);
3152 if (ret == 0)
3153 set_bit(EXTENT_BUFFER_UPTODATE, &buf->bflags);
3154 return ret;
3155} 3149}
3156 3150
3157static int btree_lock_page_hook(struct page *page, void *data, 3151static int btree_lock_page_hook(struct page *page, void *data,
diff --git a/fs/btrfs/extent_io.c b/fs/btrfs/extent_io.c
index 4a97d8fd958d..c1b898d590d7 100644
--- a/fs/btrfs/extent_io.c
+++ b/fs/btrfs/extent_io.c
@@ -19,6 +19,7 @@
19#include "btrfs_inode.h" 19#include "btrfs_inode.h"
20#include "volumes.h" 20#include "volumes.h"
21#include "check-integrity.h" 21#include "check-integrity.h"
22#include "locking.h"
22 23
23static struct kmem_cache *extent_state_cache; 24static struct kmem_cache *extent_state_cache;
24static struct kmem_cache *extent_buffer_cache; 25static struct kmem_cache *extent_buffer_cache;
@@ -53,6 +54,8 @@ struct extent_page_data {
53 unsigned int sync_io:1; 54 unsigned int sync_io:1;
54}; 55};
55 56
57static noinline void flush_write_bio(void *data);
58
56int __init extent_io_init(void) 59int __init extent_io_init(void)
57{ 60{
58 extent_state_cache = kmem_cache_create("extent_state", 61 extent_state_cache = kmem_cache_create("extent_state",
@@ -2337,7 +2340,7 @@ error_handled:
2337 } 2340 }
2338 } 2341 }
2339 2342
2340 if (uptodate) { 2343 if (uptodate && tree->track_uptodate) {
2341 set_extent_uptodate(tree, start, end, &cached, 2344 set_extent_uptodate(tree, start, end, &cached,
2342 GFP_ATOMIC); 2345 GFP_ATOMIC);
2343 } 2346 }
@@ -2973,6 +2976,275 @@ done_unlocked:
2973 return 0; 2976 return 0;
2974} 2977}
2975 2978
2979static int eb_wait(void *word)
2980{
2981 io_schedule();
2982 return 0;
2983}
2984
2985static void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
2986{
2987 wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
2988 TASK_UNINTERRUPTIBLE);
2989}
2990
2991static int lock_extent_buffer_for_io(struct extent_buffer *eb,
2992 struct btrfs_fs_info *fs_info,
2993 struct extent_page_data *epd)
2994{
2995 unsigned long i, num_pages;
2996 int flush = 0;
2997 int ret = 0;
2998
2999 if (!btrfs_try_tree_write_lock(eb)) {
3000 flush = 1;
3001 flush_write_bio(epd);
3002 btrfs_tree_lock(eb);
3003 }
3004
3005 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3006 btrfs_tree_unlock(eb);
3007 if (!epd->sync_io)
3008 return 0;
3009 if (!flush) {
3010 flush_write_bio(epd);
3011 flush = 1;
3012 }
3013 wait_on_extent_buffer_writeback(eb);
3014 btrfs_tree_lock(eb);
3015 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3016 printk(KERN_ERR "Um, ok?\n");
3017 btrfs_tree_unlock(eb);
3018 return 0;
3019 }
3020 }
3021
3022 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3023 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3024 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
3025 spin_lock(&fs_info->delalloc_lock);
3026 if (fs_info->dirty_metadata_bytes >= eb->len)
3027 fs_info->dirty_metadata_bytes -= eb->len;
3028 else
3029 WARN_ON(1);
3030 spin_unlock(&fs_info->delalloc_lock);
3031 ret = 1;
3032 }
3033
3034 btrfs_tree_unlock(eb);
3035
3036 if (!ret)
3037 return ret;
3038
3039 num_pages = num_extent_pages(eb->start, eb->len);
3040 for (i = 0; i < num_pages; i++) {
3041 struct page *p = extent_buffer_page(eb, i);
3042
3043 if (!trylock_page(p)) {
3044 if (!flush) {
3045 flush_write_bio(epd);
3046 flush = 1;
3047 }
3048 lock_page(p);
3049 }
3050 }
3051
3052 return ret;
3053}
3054
3055static void end_extent_buffer_writeback(struct extent_buffer *eb)
3056{
3057 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3058 smp_mb__after_clear_bit();
3059 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3060}
3061
3062static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
3063{
3064 int uptodate = err == 0;
3065 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
3066 struct extent_buffer *eb;
3067 int done;
3068
3069 do {
3070 struct page *page = bvec->bv_page;
3071
3072 bvec--;
3073 eb = (struct extent_buffer *)page->private;
3074 BUG_ON(!eb);
3075 done = atomic_dec_and_test(&eb->io_pages);
3076
3077 if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
3078 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3079 ClearPageUptodate(page);
3080 SetPageError(page);
3081 }
3082
3083 end_page_writeback(page);
3084
3085 if (!done)
3086 continue;
3087
3088 end_extent_buffer_writeback(eb);
3089 } while (bvec >= bio->bi_io_vec);
3090
3091 bio_put(bio);
3092
3093}
3094
3095static int write_one_eb(struct extent_buffer *eb,
3096 struct btrfs_fs_info *fs_info,
3097 struct writeback_control *wbc,
3098 struct extent_page_data *epd)
3099{
3100 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
3101 u64 offset = eb->start;
3102 unsigned long i, num_pages;
3103 int rw = (epd->sync_io ? WRITE_SYNC : WRITE);
3104 int ret;
3105
3106 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3107 num_pages = num_extent_pages(eb->start, eb->len);
3108 atomic_set(&eb->io_pages, num_pages);
3109 for (i = 0; i < num_pages; i++) {
3110 struct page *p = extent_buffer_page(eb, i);
3111
3112 clear_page_dirty_for_io(p);
3113 set_page_writeback(p);
3114 ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
3115 PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
3116 -1, end_bio_extent_buffer_writepage,
3117 0, 0, 0);
3118 if (ret) {
3119 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3120 SetPageError(p);
3121 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3122 end_extent_buffer_writeback(eb);
3123 ret = -EIO;
3124 break;
3125 }
3126 offset += PAGE_CACHE_SIZE;
3127 update_nr_written(p, wbc, 1);
3128 unlock_page(p);
3129 }
3130
3131 if (unlikely(ret)) {
3132 for (; i < num_pages; i++) {
3133 struct page *p = extent_buffer_page(eb, i);
3134 unlock_page(p);
3135 }
3136 }
3137
3138 return ret;
3139}
3140
3141int btree_write_cache_pages(struct address_space *mapping,
3142 struct writeback_control *wbc)
3143{
3144 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3145 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3146 struct extent_buffer *eb, *prev_eb = NULL;
3147 struct extent_page_data epd = {
3148 .bio = NULL,
3149 .tree = tree,
3150 .extent_locked = 0,
3151 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3152 };
3153 int ret = 0;
3154 int done = 0;
3155 int nr_to_write_done = 0;
3156 struct pagevec pvec;
3157 int nr_pages;
3158 pgoff_t index;
3159 pgoff_t end; /* Inclusive */
3160 int scanned = 0;
3161 int tag;
3162
3163 pagevec_init(&pvec, 0);
3164 if (wbc->range_cyclic) {
3165 index = mapping->writeback_index; /* Start from prev offset */
3166 end = -1;
3167 } else {
3168 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3169 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3170 scanned = 1;
3171 }
3172 if (wbc->sync_mode == WB_SYNC_ALL)
3173 tag = PAGECACHE_TAG_TOWRITE;
3174 else
3175 tag = PAGECACHE_TAG_DIRTY;
3176retry:
3177 if (wbc->sync_mode == WB_SYNC_ALL)
3178 tag_pages_for_writeback(mapping, index, end);
3179 while (!done && !nr_to_write_done && (index <= end) &&
3180 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3181 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3182 unsigned i;
3183
3184 scanned = 1;
3185 for (i = 0; i < nr_pages; i++) {
3186 struct page *page = pvec.pages[i];
3187
3188 if (!PagePrivate(page))
3189 continue;
3190
3191 if (!wbc->range_cyclic && page->index > end) {
3192 done = 1;
3193 break;
3194 }
3195
3196 eb = (struct extent_buffer *)page->private;
3197 if (!eb) {
3198 WARN_ON(1);
3199 continue;
3200 }
3201
3202 if (eb == prev_eb)
3203 continue;
3204
3205 if (!atomic_inc_not_zero(&eb->refs)) {
3206 WARN_ON(1);
3207 continue;
3208 }
3209
3210 prev_eb = eb;
3211 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3212 if (!ret) {
3213 free_extent_buffer(eb);
3214 continue;
3215 }
3216
3217 ret = write_one_eb(eb, fs_info, wbc, &epd);
3218 if (ret) {
3219 done = 1;
3220 free_extent_buffer(eb);
3221 break;
3222 }
3223 free_extent_buffer(eb);
3224
3225 /*
3226 * the filesystem may choose to bump up nr_to_write.
3227 * We have to make sure to honor the new nr_to_write
3228 * at any time
3229 */
3230 nr_to_write_done = wbc->nr_to_write <= 0;
3231 }
3232 pagevec_release(&pvec);
3233 cond_resched();
3234 }
3235 if (!scanned && !done) {
3236 /*
3237 * We hit the last page and there is more work to be done: wrap
3238 * back to the start of the file
3239 */
3240 scanned = 1;
3241 index = 0;
3242 goto retry;
3243 }
3244 flush_write_bio(&epd);
3245 return ret;
3246}
3247
2976/** 3248/**
2977 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them. 3249 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
2978 * @mapping: address space structure to write 3250 * @mapping: address space structure to write
@@ -3609,7 +3881,7 @@ static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3609#endif 3881#endif
3610 spin_lock_init(&eb->refs_lock); 3882 spin_lock_init(&eb->refs_lock);
3611 atomic_set(&eb->refs, 1); 3883 atomic_set(&eb->refs, 1);
3612 atomic_set(&eb->pages_reading, 0); 3884 atomic_set(&eb->io_pages, 0);
3613 3885
3614 if (len > MAX_INLINE_EXTENT_BUFFER_SIZE) { 3886 if (len > MAX_INLINE_EXTENT_BUFFER_SIZE) {
3615 struct page **pages; 3887 struct page **pages;
@@ -3628,6 +3900,13 @@ static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3628 return eb; 3900 return eb;
3629} 3901}
3630 3902
3903static int extent_buffer_under_io(struct extent_buffer *eb)
3904{
3905 return (atomic_read(&eb->io_pages) ||
3906 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
3907 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
3908}
3909
3631/* 3910/*
3632 * Helper for releasing extent buffer page. 3911 * Helper for releasing extent buffer page.
3633 */ 3912 */
@@ -3637,6 +3916,8 @@ static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
3637 unsigned long index; 3916 unsigned long index;
3638 struct page *page; 3917 struct page *page;
3639 3918
3919 BUG_ON(extent_buffer_under_io(eb));
3920
3640 index = num_extent_pages(eb->start, eb->len); 3921 index = num_extent_pages(eb->start, eb->len);
3641 if (start_idx >= index) 3922 if (start_idx >= index)
3642 return; 3923 return;
@@ -3655,6 +3936,7 @@ static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
3655 */ 3936 */
3656 if (PagePrivate(page) && 3937 if (PagePrivate(page) &&
3657 page->private == (unsigned long)eb) { 3938 page->private == (unsigned long)eb) {
3939 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
3658 BUG_ON(PageDirty(page)); 3940 BUG_ON(PageDirty(page));
3659 BUG_ON(PageWriteback(page)); 3941 BUG_ON(PageWriteback(page));
3660 /* 3942 /*
@@ -3683,10 +3965,41 @@ static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
3683 __free_extent_buffer(eb); 3965 __free_extent_buffer(eb);
3684} 3966}
3685 3967
3968static void check_buffer_tree_ref(struct extent_buffer *eb)
3969{
3970 /* the ref bit is tricky. We have to make sure it is set
3971 * if we have the buffer dirty. Otherwise the
3972 * code to free a buffer can end up dropping a dirty
3973 * page
3974 *
3975 * Once the ref bit is set, it won't go away while the
3976 * buffer is dirty or in writeback, and it also won't
3977 * go away while we have the reference count on the
3978 * eb bumped.
3979 *
3980 * We can't just set the ref bit without bumping the
3981 * ref on the eb because free_extent_buffer might
3982 * see the ref bit and try to clear it. If this happens
3983 * free_extent_buffer might end up dropping our original
3984 * ref by mistake and freeing the page before we are able
3985 * to add one more ref.
3986 *
3987 * So bump the ref count first, then set the bit. If someone
3988 * beat us to it, drop the ref we added.
3989 */
3990 if (!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
3991 atomic_inc(&eb->refs);
3992 if (test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
3993 atomic_dec(&eb->refs);
3994 }
3995}
3996
3686static void mark_extent_buffer_accessed(struct extent_buffer *eb) 3997static void mark_extent_buffer_accessed(struct extent_buffer *eb)
3687{ 3998{
3688 unsigned long num_pages, i; 3999 unsigned long num_pages, i;
3689 4000
4001 check_buffer_tree_ref(eb);
4002
3690 num_pages = num_extent_pages(eb->start, eb->len); 4003 num_pages = num_extent_pages(eb->start, eb->len);
3691 for (i = 0; i < num_pages; i++) { 4004 for (i = 0; i < num_pages; i++) {
3692 struct page *p = extent_buffer_page(eb, i); 4005 struct page *p = extent_buffer_page(eb, i);
@@ -3744,15 +4057,17 @@ struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3744 goto free_eb; 4057 goto free_eb;
3745 } 4058 }
3746 4059
3747 /* 4060 /*
3748 * Do this so attach doesn't complain and we need to 4061 * Do this so attach doesn't complain and we need to
3749 * drop the ref the old guy had. 4062 * drop the ref the old guy had.
3750 */ 4063 */
3751 ClearPagePrivate(p); 4064 ClearPagePrivate(p);
4065 WARN_ON(PageDirty(p));
3752 page_cache_release(p); 4066 page_cache_release(p);
3753 } 4067 }
3754 attach_extent_buffer_page(eb, p); 4068 attach_extent_buffer_page(eb, p);
3755 spin_unlock(&mapping->private_lock); 4069 spin_unlock(&mapping->private_lock);
4070 WARN_ON(PageDirty(p));
3756 mark_page_accessed(p); 4071 mark_page_accessed(p);
3757 eb->pages[i] = p; 4072 eb->pages[i] = p;
3758 if (!PageUptodate(p)) 4073 if (!PageUptodate(p))
@@ -3788,8 +4103,7 @@ again:
3788 } 4103 }
3789 /* add one reference for the tree */ 4104 /* add one reference for the tree */
3790 spin_lock(&eb->refs_lock); 4105 spin_lock(&eb->refs_lock);
3791 atomic_inc(&eb->refs); 4106 check_buffer_tree_ref(eb);
3792 set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags);
3793 spin_unlock(&eb->refs_lock); 4107 spin_unlock(&eb->refs_lock);
3794 spin_unlock(&tree->buffer_lock); 4108 spin_unlock(&tree->buffer_lock);
3795 radix_tree_preload_end(); 4109 radix_tree_preload_end();
@@ -3849,90 +4163,15 @@ static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
3849 __free_extent_buffer(eb); 4163 __free_extent_buffer(eb);
3850} 4164}
3851 4165
3852static int extent_buffer_under_io(struct extent_buffer *eb,
3853 struct page *locked_page)
3854{
3855 unsigned long num_pages, i;
3856
3857 num_pages = num_extent_pages(eb->start, eb->len);
3858 for (i = 0; i < num_pages; i++) {
3859 struct page *page = eb->pages[i];
3860 int need_unlock = 0;
3861
3862 if (!page)
3863 continue;
3864
3865 if (page != locked_page) {
3866 if (!trylock_page(page))
3867 return 1;
3868 need_unlock = 1;
3869 }
3870
3871 if (PageDirty(page) || PageWriteback(page)) {
3872 if (need_unlock)
3873 unlock_page(page);
3874 return 1;
3875 }
3876 if (need_unlock)
3877 unlock_page(page);
3878 }
3879
3880 return 0;
3881}
3882
3883/* Expects to have eb->eb_lock already held */ 4166/* Expects to have eb->eb_lock already held */
3884static void release_extent_buffer(struct extent_buffer *eb, gfp_t mask) 4167static void release_extent_buffer(struct extent_buffer *eb, gfp_t mask)
3885{ 4168{
3886 WARN_ON(atomic_read(&eb->refs) == 0); 4169 WARN_ON(atomic_read(&eb->refs) == 0);
3887 if (atomic_dec_and_test(&eb->refs)) { 4170 if (atomic_dec_and_test(&eb->refs)) {
3888 struct extent_io_tree *tree = eb->tree; 4171 struct extent_io_tree *tree = eb->tree;
3889 int ret;
3890 4172
3891 spin_unlock(&eb->refs_lock); 4173 spin_unlock(&eb->refs_lock);
3892 4174
3893 might_sleep_if(mask & __GFP_WAIT);
3894 ret = clear_extent_bit(tree, eb->start,
3895 eb->start + eb->len - 1, -1, 0, 0,
3896 NULL, mask);
3897 if (ret < 0) {
3898 unsigned long num_pages, i;
3899
3900 num_pages = num_extent_pages(eb->start, eb->len);
3901 /*
3902 * We failed to clear the state bits which likely means
3903 * ENOMEM, so just re-up the eb ref and continue, we
3904 * will get freed later on via releasepage or something
3905 * else and will be ok.
3906 */
3907 spin_lock(&eb->tree->mapping->private_lock);
3908 spin_lock(&eb->refs_lock);
3909 set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags);
3910 atomic_inc(&eb->refs);
3911
3912 /*
3913 * We may have started to reclaim the pages for a newly
3914 * allocated eb, make sure we own all of them again.
3915 */
3916 for (i = 0; i < num_pages; i++) {
3917 struct page *page = eb->pages[i];
3918
3919 if (!page) {
3920 WARN_ON(1);
3921 continue;
3922 }
3923
3924 BUG_ON(!PagePrivate(page));
3925 if (page->private != (unsigned long)eb) {
3926 ClearPagePrivate(page);
3927 page_cache_release(page);
3928 attach_extent_buffer_page(eb, page);
3929 }
3930 }
3931 spin_unlock(&eb->refs_lock);
3932 spin_unlock(&eb->tree->mapping->private_lock);
3933 return;
3934 }
3935
3936 spin_lock(&tree->buffer_lock); 4175 spin_lock(&tree->buffer_lock);
3937 radix_tree_delete(&tree->buffer, 4176 radix_tree_delete(&tree->buffer,
3938 eb->start >> PAGE_CACHE_SHIFT); 4177 eb->start >> PAGE_CACHE_SHIFT);
@@ -3955,7 +4194,7 @@ void free_extent_buffer(struct extent_buffer *eb)
3955 spin_lock(&eb->refs_lock); 4194 spin_lock(&eb->refs_lock);
3956 if (atomic_read(&eb->refs) == 2 && 4195 if (atomic_read(&eb->refs) == 2 &&
3957 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) && 4196 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
3958 !extent_buffer_under_io(eb, NULL) && 4197 !extent_buffer_under_io(eb) &&
3959 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) 4198 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
3960 atomic_dec(&eb->refs); 4199 atomic_dec(&eb->refs);
3961 4200
@@ -3974,20 +4213,20 @@ void free_extent_buffer_stale(struct extent_buffer *eb)
3974 spin_lock(&eb->refs_lock); 4213 spin_lock(&eb->refs_lock);
3975 set_bit(EXTENT_BUFFER_STALE, &eb->bflags); 4214 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
3976 4215
3977 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb, NULL) && 4216 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
3978 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) 4217 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
3979 atomic_dec(&eb->refs); 4218 atomic_dec(&eb->refs);
3980 release_extent_buffer(eb, GFP_NOFS); 4219 release_extent_buffer(eb, GFP_NOFS);
3981} 4220}
3982 4221
3983int clear_extent_buffer_dirty(struct extent_io_tree *tree, 4222int clear_extent_buffer_dirty(struct extent_buffer *eb)
3984 struct extent_buffer *eb)
3985{ 4223{
3986 unsigned long i; 4224 unsigned long i;
3987 unsigned long num_pages; 4225 unsigned long num_pages;
3988 struct page *page; 4226 struct page *page;
3989 4227
3990 num_pages = num_extent_pages(eb->start, eb->len); 4228 num_pages = num_extent_pages(eb->start, eb->len);
4229 WARN_ON(atomic_read(&eb->refs) == 0);
3991 4230
3992 for (i = 0; i < num_pages; i++) { 4231 for (i = 0; i < num_pages; i++) {
3993 page = extent_buffer_page(eb, i); 4232 page = extent_buffer_page(eb, i);
@@ -4008,25 +4247,30 @@ int clear_extent_buffer_dirty(struct extent_io_tree *tree,
4008 ClearPageError(page); 4247 ClearPageError(page);
4009 unlock_page(page); 4248 unlock_page(page);
4010 } 4249 }
4250 WARN_ON(atomic_read(&eb->refs) == 0);
4011 return 0; 4251 return 0;
4012} 4252}
4013 4253
4014int set_extent_buffer_dirty(struct extent_io_tree *tree, 4254int set_extent_buffer_dirty(struct extent_buffer *eb)
4015 struct extent_buffer *eb)
4016{ 4255{
4017 unsigned long i; 4256 unsigned long i;
4018 unsigned long num_pages; 4257 unsigned long num_pages;
4019 int was_dirty = 0; 4258 int was_dirty = 0;
4020 4259
4260 check_buffer_tree_ref(eb);
4261
4021 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags); 4262 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
4263
4022 num_pages = num_extent_pages(eb->start, eb->len); 4264 num_pages = num_extent_pages(eb->start, eb->len);
4023 WARN_ON(atomic_read(&eb->refs) == 0); 4265 WARN_ON(atomic_read(&eb->refs) == 0);
4266 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
4267
4024 for (i = 0; i < num_pages; i++) 4268 for (i = 0; i < num_pages; i++)
4025 __set_page_dirty_nobuffers(extent_buffer_page(eb, i)); 4269 set_page_dirty(extent_buffer_page(eb, i));
4026 return was_dirty; 4270 return was_dirty;
4027} 4271}
4028 4272
4029static int __eb_straddles_pages(u64 start, u64 len) 4273static int range_straddles_pages(u64 start, u64 len)
4030{ 4274{
4031 if (len < PAGE_CACHE_SIZE) 4275 if (len < PAGE_CACHE_SIZE)
4032 return 1; 4276 return 1;
@@ -4037,25 +4281,14 @@ static int __eb_straddles_pages(u64 start, u64 len)
4037 return 0; 4281 return 0;
4038} 4282}
4039 4283
4040static int eb_straddles_pages(struct extent_buffer *eb) 4284int clear_extent_buffer_uptodate(struct extent_buffer *eb)
4041{
4042 return __eb_straddles_pages(eb->start, eb->len);
4043}
4044
4045int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
4046 struct extent_buffer *eb,
4047 struct extent_state **cached_state)
4048{ 4285{
4049 unsigned long i; 4286 unsigned long i;
4050 struct page *page; 4287 struct page *page;
4051 unsigned long num_pages; 4288 unsigned long num_pages;
4052 4289
4053 num_pages = num_extent_pages(eb->start, eb->len);
4054 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags); 4290 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4055 4291 num_pages = num_extent_pages(eb->start, eb->len);
4056 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
4057 cached_state, GFP_NOFS);
4058
4059 for (i = 0; i < num_pages; i++) { 4292 for (i = 0; i < num_pages; i++) {
4060 page = extent_buffer_page(eb, i); 4293 page = extent_buffer_page(eb, i);
4061 if (page) 4294 if (page)
@@ -4064,27 +4297,16 @@ int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
4064 return 0; 4297 return 0;
4065} 4298}
4066 4299
4067int set_extent_buffer_uptodate(struct extent_io_tree *tree, 4300int set_extent_buffer_uptodate(struct extent_buffer *eb)
4068 struct extent_buffer *eb)
4069{ 4301{
4070 unsigned long i; 4302 unsigned long i;
4071 struct page *page; 4303 struct page *page;
4072 unsigned long num_pages; 4304 unsigned long num_pages;
4073 4305
4306 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4074 num_pages = num_extent_pages(eb->start, eb->len); 4307 num_pages = num_extent_pages(eb->start, eb->len);
4075
4076 if (eb_straddles_pages(eb)) {
4077 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
4078 NULL, GFP_NOFS);
4079 }
4080 for (i = 0; i < num_pages; i++) { 4308 for (i = 0; i < num_pages; i++) {
4081 page = extent_buffer_page(eb, i); 4309 page = extent_buffer_page(eb, i);
4082 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
4083 ((i == num_pages - 1) &&
4084 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
4085 check_page_uptodate(tree, page);
4086 continue;
4087 }
4088 SetPageUptodate(page); 4310 SetPageUptodate(page);
4089 } 4311 }
4090 return 0; 4312 return 0;
@@ -4099,7 +4321,7 @@ int extent_range_uptodate(struct extent_io_tree *tree,
4099 int uptodate; 4321 int uptodate;
4100 unsigned long index; 4322 unsigned long index;
4101 4323
4102 if (__eb_straddles_pages(start, end - start + 1)) { 4324 if (range_straddles_pages(start, end - start + 1)) {
4103 ret = test_range_bit(tree, start, end, 4325 ret = test_range_bit(tree, start, end,
4104 EXTENT_UPTODATE, 1, NULL); 4326 EXTENT_UPTODATE, 1, NULL);
4105 if (ret) 4327 if (ret)
@@ -4121,35 +4343,9 @@ int extent_range_uptodate(struct extent_io_tree *tree,
4121 return pg_uptodate; 4343 return pg_uptodate;
4122} 4344}
4123 4345
4124int extent_buffer_uptodate(struct extent_io_tree *tree, 4346int extent_buffer_uptodate(struct extent_buffer *eb)
4125 struct extent_buffer *eb,
4126 struct extent_state *cached_state)
4127{ 4347{
4128 int ret = 0; 4348 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4129 unsigned long num_pages;
4130 unsigned long i;
4131 struct page *page;
4132 int pg_uptodate = 1;
4133
4134 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
4135 return 1;
4136
4137 if (eb_straddles_pages(eb)) {
4138 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
4139 EXTENT_UPTODATE, 1, cached_state);
4140 if (ret)
4141 return ret;
4142 }
4143
4144 num_pages = num_extent_pages(eb->start, eb->len);
4145 for (i = 0; i < num_pages; i++) {
4146 page = extent_buffer_page(eb, i);
4147 if (!PageUptodate(page)) {
4148 pg_uptodate = 0;
4149 break;
4150 }
4151 }
4152 return pg_uptodate;
4153} 4349}
4154 4350
4155int read_extent_buffer_pages(struct extent_io_tree *tree, 4351int read_extent_buffer_pages(struct extent_io_tree *tree,
@@ -4171,13 +4367,6 @@ int read_extent_buffer_pages(struct extent_io_tree *tree,
4171 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags)) 4367 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
4172 return 0; 4368 return 0;
4173 4369
4174 if (eb_straddles_pages(eb)) {
4175 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
4176 EXTENT_UPTODATE, 1, NULL)) {
4177 return 0;
4178 }
4179 }
4180
4181 if (start) { 4370 if (start) {
4182 WARN_ON(start < eb->start); 4371 WARN_ON(start < eb->start);
4183 start_i = (start >> PAGE_CACHE_SHIFT) - 4372 start_i = (start >> PAGE_CACHE_SHIFT) -
@@ -4207,7 +4396,7 @@ int read_extent_buffer_pages(struct extent_io_tree *tree,
4207 goto unlock_exit; 4396 goto unlock_exit;
4208 } 4397 }
4209 4398
4210 atomic_set(&eb->pages_reading, num_reads); 4399 atomic_set(&eb->io_pages, num_reads);
4211 for (i = start_i; i < num_pages; i++) { 4400 for (i = start_i; i < num_pages; i++) {
4212 page = extent_buffer_page(eb, i); 4401 page = extent_buffer_page(eb, i);
4213 if (!PageUptodate(page)) { 4402 if (!PageUptodate(page)) {
@@ -4235,8 +4424,6 @@ int read_extent_buffer_pages(struct extent_io_tree *tree,
4235 ret = -EIO; 4424 ret = -EIO;
4236 } 4425 }
4237 4426
4238 if (!ret)
4239 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
4240 return ret; 4427 return ret;
4241 4428
4242unlock_exit: 4429unlock_exit:
@@ -4604,13 +4791,13 @@ int try_release_extent_buffer(struct page *page, gfp_t mask)
4604 eb = (struct extent_buffer *)page->private; 4791 eb = (struct extent_buffer *)page->private;
4605 BUG_ON(!eb); 4792 BUG_ON(!eb);
4606 4793
4607 /* 4794 /*
4608 * This is a little awful but should be ok, we need to make sure that 4795 * This is a little awful but should be ok, we need to make sure that
4609 * the eb doesn't disappear out from under us while we're looking at 4796 * the eb doesn't disappear out from under us while we're looking at
4610 * this page. 4797 * this page.
4611 */ 4798 */
4612 spin_lock(&eb->refs_lock); 4799 spin_lock(&eb->refs_lock);
4613 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb, page)) { 4800 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
4614 spin_unlock(&eb->refs_lock); 4801 spin_unlock(&eb->refs_lock);
4615 spin_unlock(&page->mapping->private_lock); 4802 spin_unlock(&page->mapping->private_lock);
4616 return 0; 4803 return 0;
diff --git a/fs/btrfs/extent_io.h b/fs/btrfs/extent_io.h
index 60628341f156..489d7945154f 100644
--- a/fs/btrfs/extent_io.h
+++ b/fs/btrfs/extent_io.h
@@ -37,6 +37,8 @@
37#define EXTENT_BUFFER_READAHEAD 4 /* this got triggered by readahead */ 37#define EXTENT_BUFFER_READAHEAD 4 /* this got triggered by readahead */
38#define EXTENT_BUFFER_TREE_REF 5 38#define EXTENT_BUFFER_TREE_REF 5
39#define EXTENT_BUFFER_STALE 6 39#define EXTENT_BUFFER_STALE 6
40#define EXTENT_BUFFER_WRITEBACK 7
41#define EXTENT_BUFFER_IOERR 8
40 42
41/* these are flags for extent_clear_unlock_delalloc */ 43/* these are flags for extent_clear_unlock_delalloc */
42#define EXTENT_CLEAR_UNLOCK_PAGE 0x1 44#define EXTENT_CLEAR_UNLOCK_PAGE 0x1
@@ -99,6 +101,7 @@ struct extent_io_tree {
99 struct radix_tree_root buffer; 101 struct radix_tree_root buffer;
100 struct address_space *mapping; 102 struct address_space *mapping;
101 u64 dirty_bytes; 103 u64 dirty_bytes;
104 int track_uptodate;
102 spinlock_t lock; 105 spinlock_t lock;
103 spinlock_t buffer_lock; 106 spinlock_t buffer_lock;
104 struct extent_io_ops *ops; 107 struct extent_io_ops *ops;
@@ -132,7 +135,7 @@ struct extent_buffer {
132 struct extent_io_tree *tree; 135 struct extent_io_tree *tree;
133 spinlock_t refs_lock; 136 spinlock_t refs_lock;
134 atomic_t refs; 137 atomic_t refs;
135 atomic_t pages_reading; 138 atomic_t io_pages;
136 struct list_head leak_list; 139 struct list_head leak_list;
137 struct rcu_head rcu_head; 140 struct rcu_head rcu_head;
138 pid_t lock_owner; 141 pid_t lock_owner;
@@ -249,6 +252,8 @@ int extent_writepages(struct extent_io_tree *tree,
249 struct address_space *mapping, 252 struct address_space *mapping,
250 get_extent_t *get_extent, 253 get_extent_t *get_extent,
251 struct writeback_control *wbc); 254 struct writeback_control *wbc);
255int btree_write_cache_pages(struct address_space *mapping,
256 struct writeback_control *wbc);
252int extent_readpages(struct extent_io_tree *tree, 257int extent_readpages(struct extent_io_tree *tree,
253 struct address_space *mapping, 258 struct address_space *mapping,
254 struct list_head *pages, unsigned nr_pages, 259 struct list_head *pages, unsigned nr_pages,
@@ -297,18 +302,11 @@ void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
297void memset_extent_buffer(struct extent_buffer *eb, char c, 302void memset_extent_buffer(struct extent_buffer *eb, char c,
298 unsigned long start, unsigned long len); 303 unsigned long start, unsigned long len);
299int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits); 304int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits);
300int clear_extent_buffer_dirty(struct extent_io_tree *tree, 305int clear_extent_buffer_dirty(struct extent_buffer *eb);
301 struct extent_buffer *eb); 306int set_extent_buffer_dirty(struct extent_buffer *eb);
302int set_extent_buffer_dirty(struct extent_io_tree *tree, 307int set_extent_buffer_uptodate(struct extent_buffer *eb);
303 struct extent_buffer *eb); 308int clear_extent_buffer_uptodate(struct extent_buffer *eb);
304int set_extent_buffer_uptodate(struct extent_io_tree *tree, 309int extent_buffer_uptodate(struct extent_buffer *eb);
305 struct extent_buffer *eb);
306int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
307 struct extent_buffer *eb,
308 struct extent_state **cached_state);
309int extent_buffer_uptodate(struct extent_io_tree *tree,
310 struct extent_buffer *eb,
311 struct extent_state *cached_state);
312int map_private_extent_buffer(struct extent_buffer *eb, unsigned long offset, 310int map_private_extent_buffer(struct extent_buffer *eb, unsigned long offset,
313 unsigned long min_len, char **map, 311 unsigned long min_len, char **map,
314 unsigned long *map_start, 312 unsigned long *map_start,
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index bb268193d85d..341a8670165f 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -6782,6 +6782,8 @@ struct inode *btrfs_alloc_inode(struct super_block *sb)
6782 extent_map_tree_init(&ei->extent_tree); 6782 extent_map_tree_init(&ei->extent_tree);
6783 extent_io_tree_init(&ei->io_tree, &inode->i_data); 6783 extent_io_tree_init(&ei->io_tree, &inode->i_data);
6784 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data); 6784 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
6785 ei->io_tree.track_uptodate = 1;
6786 ei->io_failure_tree.track_uptodate = 1;
6785 mutex_init(&ei->log_mutex); 6787 mutex_init(&ei->log_mutex);
6786 mutex_init(&ei->delalloc_mutex); 6788 mutex_init(&ei->delalloc_mutex);
6787 btrfs_ordered_inode_tree_init(&ei->ordered_tree); 6789 btrfs_ordered_inode_tree_init(&ei->ordered_tree);