aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/extent_io.c
diff options
context:
space:
mode:
authorJosef Bacik <josef@redhat.com>2012-03-13 09:38:00 -0400
committerChris Mason <chris.mason@oracle.com>2012-03-26 17:04:23 -0400
commit0b32f4bbb423f02acee6d43cd442f5f0775db7e0 (patch)
tree1963e5420071e70274bf77c05373dabd43bac675 /fs/btrfs/extent_io.c
parent5df4235ea15bd39f441ef334d8329b3d46b2cc57 (diff)
Btrfs: ensure an entire eb is written at once
This patch simplifies how we track our extent buffers. Previously we could exit writepages with only having written half of an extent buffer, which meant we had to track the state of the pages and the state of the extent buffers differently. Now we only read in entire extent buffers and write out entire extent buffers, this allows us to simply set bits in our bflags to indicate the state of the eb and we no longer have to do things like track uptodate with our iotree. Thanks, Signed-off-by: Josef Bacik <josef@redhat.com> Signed-off-by: Chris Mason <chris.mason@oracle.com>
Diffstat (limited to 'fs/btrfs/extent_io.c')
-rw-r--r--fs/btrfs/extent_io.c497
1 files changed, 342 insertions, 155 deletions
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;