blob: 8036d3a848530daed167453ebfb68eb8a83950f3 [file] [log] [blame]
Chris Masond1310b22008-01-24 16:13:08 -05001#include <linux/bitops.h>
2#include <linux/slab.h>
3#include <linux/bio.h>
4#include <linux/mm.h>
Chris Masond1310b22008-01-24 16:13:08 -05005#include <linux/pagemap.h>
6#include <linux/page-flags.h>
7#include <linux/module.h>
8#include <linux/spinlock.h>
9#include <linux/blkdev.h>
10#include <linux/swap.h>
Chris Masond1310b22008-01-24 16:13:08 -050011#include <linux/writeback.h>
12#include <linux/pagevec.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070013#include <linux/prefetch.h>
Dan Magenheimer90a887c2011-05-26 10:01:56 -060014#include <linux/cleancache.h>
Chris Masond1310b22008-01-24 16:13:08 -050015#include "extent_io.h"
16#include "extent_map.h"
David Woodhouse2db04962008-08-07 11:19:43 -040017#include "compat.h"
David Woodhouse902b22f2008-08-20 08:51:49 -040018#include "ctree.h"
19#include "btrfs_inode.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020020#include "volumes.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010021#include "check-integrity.h"
Josef Bacik0b32f4b2012-03-13 09:38:00 -040022#include "locking.h"
Josef Bacik606686e2012-06-04 14:03:51 -040023#include "rcu-string.h"
Chris Masond1310b22008-01-24 16:13:08 -050024
Chris Masond1310b22008-01-24 16:13:08 -050025static struct kmem_cache *extent_state_cache;
26static struct kmem_cache *extent_buffer_cache;
27
28static LIST_HEAD(buffers);
29static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040030
Chris Masonb47eda82008-11-10 12:34:40 -050031#define LEAK_DEBUG 0
Chris Mason39351272009-02-04 09:24:05 -050032#if LEAK_DEBUG
Chris Masond3977122009-01-05 21:25:51 -050033static DEFINE_SPINLOCK(leak_lock);
Chris Mason4bef0842008-09-08 11:18:08 -040034#endif
Chris Masond1310b22008-01-24 16:13:08 -050035
Chris Masond1310b22008-01-24 16:13:08 -050036#define BUFFER_LRU_MAX 64
37
38struct tree_entry {
39 u64 start;
40 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -050041 struct rb_node rb_node;
42};
43
44struct extent_page_data {
45 struct bio *bio;
46 struct extent_io_tree *tree;
47 get_extent_t *get_extent;
Josef Bacikde0022b2012-09-25 14:25:58 -040048 unsigned long bio_flags;
Chris Mason771ed682008-11-06 22:02:51 -050049
50 /* tells writepage not to lock the state bits for this range
51 * it still does the unlocking
52 */
Chris Masonffbd5172009-04-20 15:50:09 -040053 unsigned int extent_locked:1;
54
55 /* tells the submit_bio code to use a WRITE_SYNC */
56 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -050057};
58
Josef Bacik0b32f4b2012-03-13 09:38:00 -040059static noinline void flush_write_bio(void *data);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -040060static inline struct btrfs_fs_info *
61tree_fs_info(struct extent_io_tree *tree)
62{
63 return btrfs_sb(tree->mapping->host->i_sb);
64}
Josef Bacik0b32f4b2012-03-13 09:38:00 -040065
Chris Masond1310b22008-01-24 16:13:08 -050066int __init extent_io_init(void)
67{
David Sterba837e1972012-09-07 03:00:48 -060068 extent_state_cache = kmem_cache_create("btrfs_extent_state",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020069 sizeof(struct extent_state), 0,
70 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050071 if (!extent_state_cache)
72 return -ENOMEM;
73
David Sterba837e1972012-09-07 03:00:48 -060074 extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020075 sizeof(struct extent_buffer), 0,
76 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050077 if (!extent_buffer_cache)
78 goto free_state_cache;
79 return 0;
80
81free_state_cache:
82 kmem_cache_destroy(extent_state_cache);
83 return -ENOMEM;
84}
85
86void extent_io_exit(void)
87{
88 struct extent_state *state;
Chris Mason2d2ae542008-03-26 16:24:23 -040089 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -050090
91 while (!list_empty(&states)) {
Chris Mason2d2ae542008-03-26 16:24:23 -040092 state = list_entry(states.next, struct extent_state, leak_list);
Chris Masond3977122009-01-05 21:25:51 -050093 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
94 "state %lu in tree %p refs %d\n",
95 (unsigned long long)state->start,
96 (unsigned long long)state->end,
97 state->state, state->tree, atomic_read(&state->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040098 list_del(&state->leak_list);
Chris Masond1310b22008-01-24 16:13:08 -050099 kmem_cache_free(extent_state_cache, state);
100
101 }
102
Chris Mason2d2ae542008-03-26 16:24:23 -0400103 while (!list_empty(&buffers)) {
104 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
Chris Masond3977122009-01-05 21:25:51 -0500105 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
106 "refs %d\n", (unsigned long long)eb->start,
107 eb->len, atomic_read(&eb->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -0400108 list_del(&eb->leak_list);
109 kmem_cache_free(extent_buffer_cache, eb);
110 }
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000111
112 /*
113 * Make sure all delayed rcu free are flushed before we
114 * destroy caches.
115 */
116 rcu_barrier();
Chris Masond1310b22008-01-24 16:13:08 -0500117 if (extent_state_cache)
118 kmem_cache_destroy(extent_state_cache);
119 if (extent_buffer_cache)
120 kmem_cache_destroy(extent_buffer_cache);
121}
122
123void extent_io_tree_init(struct extent_io_tree *tree,
David Sterbaf993c882011-04-20 23:35:57 +0200124 struct address_space *mapping)
Chris Masond1310b22008-01-24 16:13:08 -0500125{
Eric Paris6bef4d32010-02-23 19:43:04 +0000126 tree->state = RB_ROOT;
Miao Xie19fe0a82010-10-26 20:57:29 -0400127 INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500128 tree->ops = NULL;
129 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500130 spin_lock_init(&tree->lock);
Chris Mason6af118ce2008-07-22 11:18:07 -0400131 spin_lock_init(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -0500132 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500133}
Chris Masond1310b22008-01-24 16:13:08 -0500134
Christoph Hellwigb2950862008-12-02 09:54:17 -0500135static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500136{
137 struct extent_state *state;
Chris Mason39351272009-02-04 09:24:05 -0500138#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400139 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400140#endif
Chris Masond1310b22008-01-24 16:13:08 -0500141
142 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400143 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500144 return state;
145 state->state = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500146 state->private = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500147 state->tree = NULL;
Chris Mason39351272009-02-04 09:24:05 -0500148#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400149 spin_lock_irqsave(&leak_lock, flags);
150 list_add(&state->leak_list, &states);
151 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400152#endif
Chris Masond1310b22008-01-24 16:13:08 -0500153 atomic_set(&state->refs, 1);
154 init_waitqueue_head(&state->wq);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100155 trace_alloc_extent_state(state, mask, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500156 return state;
157}
Chris Masond1310b22008-01-24 16:13:08 -0500158
Chris Mason4845e442010-05-25 20:56:50 -0400159void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500160{
Chris Masond1310b22008-01-24 16:13:08 -0500161 if (!state)
162 return;
163 if (atomic_dec_and_test(&state->refs)) {
Chris Mason39351272009-02-04 09:24:05 -0500164#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400165 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400166#endif
Chris Mason70dec802008-01-29 09:59:12 -0500167 WARN_ON(state->tree);
Chris Mason39351272009-02-04 09:24:05 -0500168#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400169 spin_lock_irqsave(&leak_lock, flags);
170 list_del(&state->leak_list);
171 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400172#endif
Jeff Mahoney143bede2012-03-01 14:56:26 +0100173 trace_free_extent_state(state, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500174 kmem_cache_free(extent_state_cache, state);
175 }
176}
Chris Masond1310b22008-01-24 16:13:08 -0500177
178static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
179 struct rb_node *node)
180{
Chris Masond3977122009-01-05 21:25:51 -0500181 struct rb_node **p = &root->rb_node;
182 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500183 struct tree_entry *entry;
184
Chris Masond3977122009-01-05 21:25:51 -0500185 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500186 parent = *p;
187 entry = rb_entry(parent, struct tree_entry, rb_node);
188
189 if (offset < entry->start)
190 p = &(*p)->rb_left;
191 else if (offset > entry->end)
192 p = &(*p)->rb_right;
193 else
194 return parent;
195 }
196
Chris Masond1310b22008-01-24 16:13:08 -0500197 rb_link_node(node, parent, p);
198 rb_insert_color(node, root);
199 return NULL;
200}
201
Chris Mason80ea96b2008-02-01 14:51:59 -0500202static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Chris Masond1310b22008-01-24 16:13:08 -0500203 struct rb_node **prev_ret,
204 struct rb_node **next_ret)
205{
Chris Mason80ea96b2008-02-01 14:51:59 -0500206 struct rb_root *root = &tree->state;
Chris Masond3977122009-01-05 21:25:51 -0500207 struct rb_node *n = root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500208 struct rb_node *prev = NULL;
209 struct rb_node *orig_prev = NULL;
210 struct tree_entry *entry;
211 struct tree_entry *prev_entry = NULL;
212
Chris Masond3977122009-01-05 21:25:51 -0500213 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500214 entry = rb_entry(n, struct tree_entry, rb_node);
215 prev = n;
216 prev_entry = entry;
217
218 if (offset < entry->start)
219 n = n->rb_left;
220 else if (offset > entry->end)
221 n = n->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500222 else
Chris Masond1310b22008-01-24 16:13:08 -0500223 return n;
224 }
225
226 if (prev_ret) {
227 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500228 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500229 prev = rb_next(prev);
230 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
231 }
232 *prev_ret = prev;
233 prev = orig_prev;
234 }
235
236 if (next_ret) {
237 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500238 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500239 prev = rb_prev(prev);
240 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
241 }
242 *next_ret = prev;
243 }
244 return NULL;
245}
246
Chris Mason80ea96b2008-02-01 14:51:59 -0500247static inline struct rb_node *tree_search(struct extent_io_tree *tree,
248 u64 offset)
Chris Masond1310b22008-01-24 16:13:08 -0500249{
Chris Mason70dec802008-01-29 09:59:12 -0500250 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500251 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500252
Chris Mason80ea96b2008-02-01 14:51:59 -0500253 ret = __etree_search(tree, offset, &prev, NULL);
Chris Masond3977122009-01-05 21:25:51 -0500254 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500255 return prev;
256 return ret;
257}
258
Josef Bacik9ed74f22009-09-11 16:12:44 -0400259static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
260 struct extent_state *other)
261{
262 if (tree->ops && tree->ops->merge_extent_hook)
263 tree->ops->merge_extent_hook(tree->mapping->host, new,
264 other);
265}
266
Chris Masond1310b22008-01-24 16:13:08 -0500267/*
268 * utility function to look for merge candidates inside a given range.
269 * Any extents with matching state are merged together into a single
270 * extent in the tree. Extents with EXTENT_IO in their state field
271 * are not merged because the end_io handlers need to be able to do
272 * operations on them without sleeping (or doing allocations/splits).
273 *
274 * This should be called with the tree lock held.
275 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000276static void merge_state(struct extent_io_tree *tree,
277 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500278{
279 struct extent_state *other;
280 struct rb_node *other_node;
281
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400282 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000283 return;
Chris Masond1310b22008-01-24 16:13:08 -0500284
285 other_node = rb_prev(&state->rb_node);
286 if (other_node) {
287 other = rb_entry(other_node, struct extent_state, rb_node);
288 if (other->end == state->start - 1 &&
289 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400290 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500291 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500292 other->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500293 rb_erase(&other->rb_node, &tree->state);
294 free_extent_state(other);
295 }
296 }
297 other_node = rb_next(&state->rb_node);
298 if (other_node) {
299 other = rb_entry(other_node, struct extent_state, rb_node);
300 if (other->start == state->end + 1 &&
301 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400302 merge_cb(tree, state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400303 state->end = other->end;
304 other->tree = NULL;
305 rb_erase(&other->rb_node, &tree->state);
306 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500307 }
308 }
Chris Masond1310b22008-01-24 16:13:08 -0500309}
310
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000311static void set_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400312 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500313{
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000314 if (tree->ops && tree->ops->set_bit_hook)
315 tree->ops->set_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500316}
317
318static void clear_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400319 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500320{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400321 if (tree->ops && tree->ops->clear_bit_hook)
322 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500323}
324
Xiao Guangrong3150b692011-07-14 03:19:08 +0000325static void set_state_bits(struct extent_io_tree *tree,
326 struct extent_state *state, int *bits);
327
Chris Masond1310b22008-01-24 16:13:08 -0500328/*
329 * insert an extent_state struct into the tree. 'bits' are set on the
330 * struct before it is inserted.
331 *
332 * This may return -EEXIST if the extent is already there, in which case the
333 * state struct is freed.
334 *
335 * The tree lock is not taken internally. This is a utility function and
336 * probably isn't what you want to call (see set/clear_extent_bit).
337 */
338static int insert_state(struct extent_io_tree *tree,
339 struct extent_state *state, u64 start, u64 end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400340 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500341{
342 struct rb_node *node;
343
344 if (end < start) {
Chris Masond3977122009-01-05 21:25:51 -0500345 printk(KERN_ERR "btrfs end < start %llu %llu\n",
346 (unsigned long long)end,
347 (unsigned long long)start);
Chris Masond1310b22008-01-24 16:13:08 -0500348 WARN_ON(1);
349 }
Chris Masond1310b22008-01-24 16:13:08 -0500350 state->start = start;
351 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400352
Xiao Guangrong3150b692011-07-14 03:19:08 +0000353 set_state_bits(tree, state, bits);
354
Chris Masond1310b22008-01-24 16:13:08 -0500355 node = tree_insert(&tree->state, end, &state->rb_node);
356 if (node) {
357 struct extent_state *found;
358 found = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500359 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
360 "%llu %llu\n", (unsigned long long)found->start,
361 (unsigned long long)found->end,
362 (unsigned long long)start, (unsigned long long)end);
Chris Masond1310b22008-01-24 16:13:08 -0500363 return -EEXIST;
364 }
Chris Mason70dec802008-01-29 09:59:12 -0500365 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500366 merge_state(tree, state);
367 return 0;
368}
369
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000370static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
Josef Bacik9ed74f22009-09-11 16:12:44 -0400371 u64 split)
372{
373 if (tree->ops && tree->ops->split_extent_hook)
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000374 tree->ops->split_extent_hook(tree->mapping->host, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400375}
376
Chris Masond1310b22008-01-24 16:13:08 -0500377/*
378 * split a given extent state struct in two, inserting the preallocated
379 * struct 'prealloc' as the newly created second half. 'split' indicates an
380 * offset inside 'orig' where it should be split.
381 *
382 * Before calling,
383 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
384 * are two extent state structs in the tree:
385 * prealloc: [orig->start, split - 1]
386 * orig: [ split, orig->end ]
387 *
388 * The tree locks are not taken by this function. They need to be held
389 * by the caller.
390 */
391static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
392 struct extent_state *prealloc, u64 split)
393{
394 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400395
396 split_cb(tree, orig, split);
397
Chris Masond1310b22008-01-24 16:13:08 -0500398 prealloc->start = orig->start;
399 prealloc->end = split - 1;
400 prealloc->state = orig->state;
401 orig->start = split;
402
403 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
404 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500405 free_extent_state(prealloc);
406 return -EEXIST;
407 }
Chris Mason70dec802008-01-29 09:59:12 -0500408 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500409 return 0;
410}
411
Li Zefancdc6a392012-03-12 16:39:48 +0800412static struct extent_state *next_state(struct extent_state *state)
413{
414 struct rb_node *next = rb_next(&state->rb_node);
415 if (next)
416 return rb_entry(next, struct extent_state, rb_node);
417 else
418 return NULL;
419}
420
Chris Masond1310b22008-01-24 16:13:08 -0500421/*
422 * utility function to clear some bits in an extent state struct.
Wang Sheng-Hui1b303fc2012-04-06 14:35:18 +0800423 * it will optionally wake up any one waiting on this state (wake == 1).
Chris Masond1310b22008-01-24 16:13:08 -0500424 *
425 * If no bits are set on the state struct after clearing things, the
426 * struct is freed and removed from the tree
427 */
Li Zefancdc6a392012-03-12 16:39:48 +0800428static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
429 struct extent_state *state,
430 int *bits, int wake)
Chris Masond1310b22008-01-24 16:13:08 -0500431{
Li Zefancdc6a392012-03-12 16:39:48 +0800432 struct extent_state *next;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400433 int bits_to_clear = *bits & ~EXTENT_CTLBITS;
Chris Masond1310b22008-01-24 16:13:08 -0500434
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400435 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500436 u64 range = state->end - state->start + 1;
437 WARN_ON(range > tree->dirty_bytes);
438 tree->dirty_bytes -= range;
439 }
Chris Mason291d6732008-01-29 15:55:23 -0500440 clear_state_cb(tree, state, bits);
Josef Bacik32c00af2009-10-08 13:34:05 -0400441 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500442 if (wake)
443 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400444 if (state->state == 0) {
Li Zefancdc6a392012-03-12 16:39:48 +0800445 next = next_state(state);
Chris Mason70dec802008-01-29 09:59:12 -0500446 if (state->tree) {
Chris Masond1310b22008-01-24 16:13:08 -0500447 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500448 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500449 free_extent_state(state);
450 } else {
451 WARN_ON(1);
452 }
453 } else {
454 merge_state(tree, state);
Li Zefancdc6a392012-03-12 16:39:48 +0800455 next = next_state(state);
Chris Masond1310b22008-01-24 16:13:08 -0500456 }
Li Zefancdc6a392012-03-12 16:39:48 +0800457 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500458}
459
Xiao Guangrong82337672011-04-20 06:44:57 +0000460static struct extent_state *
461alloc_extent_state_atomic(struct extent_state *prealloc)
462{
463 if (!prealloc)
464 prealloc = alloc_extent_state(GFP_ATOMIC);
465
466 return prealloc;
467}
468
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400469void extent_io_tree_panic(struct extent_io_tree *tree, int err)
470{
471 btrfs_panic(tree_fs_info(tree), err, "Locking error: "
472 "Extent tree was modified by another "
473 "thread while locked.");
474}
475
Chris Masond1310b22008-01-24 16:13:08 -0500476/*
477 * clear some bits on a range in the tree. This may require splitting
478 * or inserting elements in the tree, so the gfp mask is used to
479 * indicate which allocations or sleeping are allowed.
480 *
481 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
482 * the given range from the tree regardless of state (ie for truncate).
483 *
484 * the range [start, end] is inclusive.
485 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100486 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500487 */
488int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400489 int bits, int wake, int delete,
490 struct extent_state **cached_state,
491 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500492{
493 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400494 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500495 struct extent_state *prealloc = NULL;
496 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400497 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500498 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000499 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500500
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400501 if (delete)
502 bits |= ~EXTENT_CTLBITS;
503 bits |= EXTENT_FIRST_DELALLOC;
504
Josef Bacik2ac55d42010-02-03 19:33:23 +0000505 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
506 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500507again:
508 if (!prealloc && (mask & __GFP_WAIT)) {
509 prealloc = alloc_extent_state(mask);
510 if (!prealloc)
511 return -ENOMEM;
512 }
513
Chris Masoncad321a2008-12-17 14:51:42 -0500514 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400515 if (cached_state) {
516 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000517
518 if (clear) {
519 *cached_state = NULL;
520 cached_state = NULL;
521 }
522
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400523 if (cached && cached->tree && cached->start <= start &&
524 cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000525 if (clear)
526 atomic_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400527 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400528 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400529 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000530 if (clear)
531 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400532 }
Chris Masond1310b22008-01-24 16:13:08 -0500533 /*
534 * this search will find the extents that end after
535 * our range starts
536 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500537 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500538 if (!node)
539 goto out;
540 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400541hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500542 if (state->start > end)
543 goto out;
544 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400545 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500546
Liu Bo04493142012-02-16 18:34:37 +0800547 /* the state doesn't have the wanted bits, go ahead */
Li Zefancdc6a392012-03-12 16:39:48 +0800548 if (!(state->state & bits)) {
549 state = next_state(state);
Liu Bo04493142012-02-16 18:34:37 +0800550 goto next;
Li Zefancdc6a392012-03-12 16:39:48 +0800551 }
Liu Bo04493142012-02-16 18:34:37 +0800552
Chris Masond1310b22008-01-24 16:13:08 -0500553 /*
554 * | ---- desired range ---- |
555 * | state | or
556 * | ------------- state -------------- |
557 *
558 * We need to split the extent we found, and may flip
559 * bits on second half.
560 *
561 * If the extent we found extends past our range, we
562 * just split and search again. It'll get split again
563 * the next time though.
564 *
565 * If the extent we found is inside our range, we clear
566 * the desired bit on it.
567 */
568
569 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000570 prealloc = alloc_extent_state_atomic(prealloc);
571 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500572 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400573 if (err)
574 extent_io_tree_panic(tree, err);
575
Chris Masond1310b22008-01-24 16:13:08 -0500576 prealloc = NULL;
577 if (err)
578 goto out;
579 if (state->end <= end) {
Liu Bod1ac6e42012-05-10 18:10:39 +0800580 state = clear_state_bit(tree, state, &bits, wake);
581 goto next;
Chris Masond1310b22008-01-24 16:13:08 -0500582 }
583 goto search_again;
584 }
585 /*
586 * | ---- desired range ---- |
587 * | state |
588 * We need to split the extent, and clear the bit
589 * on the first half
590 */
591 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000592 prealloc = alloc_extent_state_atomic(prealloc);
593 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500594 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400595 if (err)
596 extent_io_tree_panic(tree, err);
597
Chris Masond1310b22008-01-24 16:13:08 -0500598 if (wake)
599 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400600
Jeff Mahoney6763af82012-03-01 14:56:29 +0100601 clear_state_bit(tree, prealloc, &bits, wake);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400602
Chris Masond1310b22008-01-24 16:13:08 -0500603 prealloc = NULL;
604 goto out;
605 }
Chris Mason42daec22009-09-23 19:51:09 -0400606
Li Zefancdc6a392012-03-12 16:39:48 +0800607 state = clear_state_bit(tree, state, &bits, wake);
Liu Bo04493142012-02-16 18:34:37 +0800608next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400609 if (last_end == (u64)-1)
610 goto out;
611 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800612 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800613 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500614 goto search_again;
615
616out:
Chris Masoncad321a2008-12-17 14:51:42 -0500617 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500618 if (prealloc)
619 free_extent_state(prealloc);
620
Jeff Mahoney6763af82012-03-01 14:56:29 +0100621 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500622
623search_again:
624 if (start > end)
625 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500626 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500627 if (mask & __GFP_WAIT)
628 cond_resched();
629 goto again;
630}
Chris Masond1310b22008-01-24 16:13:08 -0500631
Jeff Mahoney143bede2012-03-01 14:56:26 +0100632static void wait_on_state(struct extent_io_tree *tree,
633 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500634 __releases(tree->lock)
635 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500636{
637 DEFINE_WAIT(wait);
638 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500639 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500640 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500641 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500642 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500643}
644
645/*
646 * waits for one or more bits to clear on a range in the state tree.
647 * The range [start, end] is inclusive.
648 * The tree lock is taken by this function
649 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100650void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
Chris Masond1310b22008-01-24 16:13:08 -0500651{
652 struct extent_state *state;
653 struct rb_node *node;
654
Chris Masoncad321a2008-12-17 14:51:42 -0500655 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500656again:
657 while (1) {
658 /*
659 * this search will find all the extents that end after
660 * our range starts
661 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500662 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500663 if (!node)
664 break;
665
666 state = rb_entry(node, struct extent_state, rb_node);
667
668 if (state->start > end)
669 goto out;
670
671 if (state->state & bits) {
672 start = state->start;
673 atomic_inc(&state->refs);
674 wait_on_state(tree, state);
675 free_extent_state(state);
676 goto again;
677 }
678 start = state->end + 1;
679
680 if (start > end)
681 break;
682
Xiao Guangrongded91f02011-07-14 03:19:27 +0000683 cond_resched_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500684 }
685out:
Chris Masoncad321a2008-12-17 14:51:42 -0500686 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500687}
Chris Masond1310b22008-01-24 16:13:08 -0500688
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000689static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500690 struct extent_state *state,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400691 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500692{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400693 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400694
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000695 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400696 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500697 u64 range = state->end - state->start + 1;
698 tree->dirty_bytes += range;
699 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400700 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500701}
702
Chris Mason2c64c532009-09-02 15:04:12 -0400703static void cache_state(struct extent_state *state,
704 struct extent_state **cached_ptr)
705{
706 if (cached_ptr && !(*cached_ptr)) {
707 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
708 *cached_ptr = state;
709 atomic_inc(&state->refs);
710 }
711 }
712}
713
Arne Jansen507903b2011-04-06 10:02:20 +0000714static void uncache_state(struct extent_state **cached_ptr)
715{
716 if (cached_ptr && (*cached_ptr)) {
717 struct extent_state *state = *cached_ptr;
Chris Mason109b36a2011-04-12 13:57:39 -0400718 *cached_ptr = NULL;
719 free_extent_state(state);
Arne Jansen507903b2011-04-06 10:02:20 +0000720 }
721}
722
Chris Masond1310b22008-01-24 16:13:08 -0500723/*
Chris Mason1edbb732009-09-02 13:24:36 -0400724 * set some bits on a range in the tree. This may require allocations or
725 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500726 *
Chris Mason1edbb732009-09-02 13:24:36 -0400727 * If any of the exclusive bits are set, this will fail with -EEXIST if some
728 * part of the range already has the desired bits set. The start of the
729 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500730 *
Chris Mason1edbb732009-09-02 13:24:36 -0400731 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500732 */
Chris Mason1edbb732009-09-02 13:24:36 -0400733
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100734static int __must_check
735__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
736 int bits, int exclusive_bits, u64 *failed_start,
737 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500738{
739 struct extent_state *state;
740 struct extent_state *prealloc = NULL;
741 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500742 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500743 u64 last_start;
744 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400745
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400746 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500747again:
748 if (!prealloc && (mask & __GFP_WAIT)) {
749 prealloc = alloc_extent_state(mask);
Xiao Guangrong82337672011-04-20 06:44:57 +0000750 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500751 }
752
Chris Masoncad321a2008-12-17 14:51:42 -0500753 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400754 if (cached_state && *cached_state) {
755 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400756 if (state->start <= start && state->end > start &&
757 state->tree) {
Chris Mason9655d292009-09-02 15:22:30 -0400758 node = &state->rb_node;
759 goto hit_next;
760 }
761 }
Chris Masond1310b22008-01-24 16:13:08 -0500762 /*
763 * this search will find all the extents that end after
764 * our range starts.
765 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500766 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500767 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000768 prealloc = alloc_extent_state_atomic(prealloc);
769 BUG_ON(!prealloc);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400770 err = insert_state(tree, prealloc, start, end, &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400771 if (err)
772 extent_io_tree_panic(tree, err);
773
Chris Masond1310b22008-01-24 16:13:08 -0500774 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500775 goto out;
776 }
Chris Masond1310b22008-01-24 16:13:08 -0500777 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400778hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500779 last_start = state->start;
780 last_end = state->end;
781
782 /*
783 * | ---- desired range ---- |
784 * | state |
785 *
786 * Just lock what we found and keep going
787 */
788 if (state->start == start && state->end <= end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400789 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500790 *failed_start = state->start;
791 err = -EEXIST;
792 goto out;
793 }
Chris Mason42daec22009-09-23 19:51:09 -0400794
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000795 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400796 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500797 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400798 if (last_end == (u64)-1)
799 goto out;
800 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800801 state = next_state(state);
802 if (start < end && state && state->start == start &&
803 !need_resched())
804 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500805 goto search_again;
806 }
807
808 /*
809 * | ---- desired range ---- |
810 * | state |
811 * or
812 * | ------------- state -------------- |
813 *
814 * We need to split the extent we found, and may flip bits on
815 * second half.
816 *
817 * If the extent we found extends past our
818 * range, we just split and search again. It'll get split
819 * again the next time though.
820 *
821 * If the extent we found is inside our range, we set the
822 * desired bit on it.
823 */
824 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400825 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500826 *failed_start = start;
827 err = -EEXIST;
828 goto out;
829 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000830
831 prealloc = alloc_extent_state_atomic(prealloc);
832 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500833 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400834 if (err)
835 extent_io_tree_panic(tree, err);
836
Chris Masond1310b22008-01-24 16:13:08 -0500837 prealloc = NULL;
838 if (err)
839 goto out;
840 if (state->end <= end) {
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000841 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400842 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500843 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400844 if (last_end == (u64)-1)
845 goto out;
846 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800847 state = next_state(state);
848 if (start < end && state && state->start == start &&
849 !need_resched())
850 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500851 }
852 goto search_again;
853 }
854 /*
855 * | ---- desired range ---- |
856 * | state | or | state |
857 *
858 * There's a hole, we need to insert something in it and
859 * ignore the extent we found.
860 */
861 if (state->start > start) {
862 u64 this_end;
863 if (end < last_start)
864 this_end = end;
865 else
Chris Masond3977122009-01-05 21:25:51 -0500866 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000867
868 prealloc = alloc_extent_state_atomic(prealloc);
869 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000870
871 /*
872 * Avoid to free 'prealloc' if it can be merged with
873 * the later extent.
874 */
Chris Masond1310b22008-01-24 16:13:08 -0500875 err = insert_state(tree, prealloc, start, this_end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400876 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400877 if (err)
878 extent_io_tree_panic(tree, err);
879
Chris Mason2c64c532009-09-02 15:04:12 -0400880 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500881 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500882 start = this_end + 1;
883 goto search_again;
884 }
885 /*
886 * | ---- desired range ---- |
887 * | state |
888 * We need to split the extent, and set the bit
889 * on the first half
890 */
891 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400892 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500893 *failed_start = start;
894 err = -EEXIST;
895 goto out;
896 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000897
898 prealloc = alloc_extent_state_atomic(prealloc);
899 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500900 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400901 if (err)
902 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -0500903
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000904 set_state_bits(tree, prealloc, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400905 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500906 merge_state(tree, prealloc);
907 prealloc = NULL;
908 goto out;
909 }
910
911 goto search_again;
912
913out:
Chris Masoncad321a2008-12-17 14:51:42 -0500914 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500915 if (prealloc)
916 free_extent_state(prealloc);
917
918 return err;
919
920search_again:
921 if (start > end)
922 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500923 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500924 if (mask & __GFP_WAIT)
925 cond_resched();
926 goto again;
927}
Chris Masond1310b22008-01-24 16:13:08 -0500928
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100929int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits,
930 u64 *failed_start, struct extent_state **cached_state,
931 gfp_t mask)
932{
933 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
934 cached_state, mask);
935}
936
937
Josef Bacik462d6fa2011-09-26 13:56:12 -0400938/**
Liu Bo10983f22012-07-11 15:26:19 +0800939 * convert_extent_bit - convert all bits in a given range from one bit to
940 * another
Josef Bacik462d6fa2011-09-26 13:56:12 -0400941 * @tree: the io tree to search
942 * @start: the start offset in bytes
943 * @end: the end offset in bytes (inclusive)
944 * @bits: the bits to set in this range
945 * @clear_bits: the bits to clear in this range
Josef Bacike6138872012-09-27 17:07:30 -0400946 * @cached_state: state that we're going to cache
Josef Bacik462d6fa2011-09-26 13:56:12 -0400947 * @mask: the allocation mask
948 *
949 * This will go through and set bits for the given range. If any states exist
950 * already in this range they are set with the given bit and cleared of the
951 * clear_bits. This is only meant to be used by things that are mergeable, ie
952 * converting from say DELALLOC to DIRTY. This is not meant to be used with
953 * boundary bits like LOCK.
954 */
955int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacike6138872012-09-27 17:07:30 -0400956 int bits, int clear_bits,
957 struct extent_state **cached_state, gfp_t mask)
Josef Bacik462d6fa2011-09-26 13:56:12 -0400958{
959 struct extent_state *state;
960 struct extent_state *prealloc = NULL;
961 struct rb_node *node;
962 int err = 0;
963 u64 last_start;
964 u64 last_end;
965
966again:
967 if (!prealloc && (mask & __GFP_WAIT)) {
968 prealloc = alloc_extent_state(mask);
969 if (!prealloc)
970 return -ENOMEM;
971 }
972
973 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -0400974 if (cached_state && *cached_state) {
975 state = *cached_state;
976 if (state->start <= start && state->end > start &&
977 state->tree) {
978 node = &state->rb_node;
979 goto hit_next;
980 }
981 }
982
Josef Bacik462d6fa2011-09-26 13:56:12 -0400983 /*
984 * this search will find all the extents that end after
985 * our range starts.
986 */
987 node = tree_search(tree, start);
988 if (!node) {
989 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -0500990 if (!prealloc) {
991 err = -ENOMEM;
992 goto out;
993 }
Josef Bacik462d6fa2011-09-26 13:56:12 -0400994 err = insert_state(tree, prealloc, start, end, &bits);
995 prealloc = NULL;
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400996 if (err)
997 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -0400998 goto out;
999 }
1000 state = rb_entry(node, struct extent_state, rb_node);
1001hit_next:
1002 last_start = state->start;
1003 last_end = state->end;
1004
1005 /*
1006 * | ---- desired range ---- |
1007 * | state |
1008 *
1009 * Just lock what we found and keep going
1010 */
1011 if (state->start == start && state->end <= end) {
Josef Bacik462d6fa2011-09-26 13:56:12 -04001012 set_state_bits(tree, state, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001013 cache_state(state, cached_state);
Liu Bod1ac6e42012-05-10 18:10:39 +08001014 state = clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001015 if (last_end == (u64)-1)
1016 goto out;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001017 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001018 if (start < end && state && state->start == start &&
1019 !need_resched())
1020 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001021 goto search_again;
1022 }
1023
1024 /*
1025 * | ---- desired range ---- |
1026 * | state |
1027 * or
1028 * | ------------- state -------------- |
1029 *
1030 * We need to split the extent we found, and may flip bits on
1031 * second half.
1032 *
1033 * If the extent we found extends past our
1034 * range, we just split and search again. It'll get split
1035 * again the next time though.
1036 *
1037 * If the extent we found is inside our range, we set the
1038 * desired bit on it.
1039 */
1040 if (state->start < start) {
1041 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001042 if (!prealloc) {
1043 err = -ENOMEM;
1044 goto out;
1045 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001046 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001047 if (err)
1048 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001049 prealloc = NULL;
1050 if (err)
1051 goto out;
1052 if (state->end <= end) {
1053 set_state_bits(tree, state, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001054 cache_state(state, cached_state);
Liu Bod1ac6e42012-05-10 18:10:39 +08001055 state = clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001056 if (last_end == (u64)-1)
1057 goto out;
1058 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001059 if (start < end && state && state->start == start &&
1060 !need_resched())
1061 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001062 }
1063 goto search_again;
1064 }
1065 /*
1066 * | ---- desired range ---- |
1067 * | state | or | state |
1068 *
1069 * There's a hole, we need to insert something in it and
1070 * ignore the extent we found.
1071 */
1072 if (state->start > start) {
1073 u64 this_end;
1074 if (end < last_start)
1075 this_end = end;
1076 else
1077 this_end = last_start - 1;
1078
1079 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001080 if (!prealloc) {
1081 err = -ENOMEM;
1082 goto out;
1083 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001084
1085 /*
1086 * Avoid to free 'prealloc' if it can be merged with
1087 * the later extent.
1088 */
1089 err = insert_state(tree, prealloc, start, this_end,
1090 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001091 if (err)
1092 extent_io_tree_panic(tree, err);
Josef Bacike6138872012-09-27 17:07:30 -04001093 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001094 prealloc = NULL;
1095 start = this_end + 1;
1096 goto search_again;
1097 }
1098 /*
1099 * | ---- desired range ---- |
1100 * | state |
1101 * We need to split the extent, and set the bit
1102 * on the first half
1103 */
1104 if (state->start <= end && state->end > end) {
1105 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001106 if (!prealloc) {
1107 err = -ENOMEM;
1108 goto out;
1109 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001110
1111 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001112 if (err)
1113 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001114
1115 set_state_bits(tree, prealloc, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001116 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001117 clear_state_bit(tree, prealloc, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001118 prealloc = NULL;
1119 goto out;
1120 }
1121
1122 goto search_again;
1123
1124out:
1125 spin_unlock(&tree->lock);
1126 if (prealloc)
1127 free_extent_state(prealloc);
1128
1129 return err;
1130
1131search_again:
1132 if (start > end)
1133 goto out;
1134 spin_unlock(&tree->lock);
1135 if (mask & __GFP_WAIT)
1136 cond_resched();
1137 goto again;
1138}
1139
Chris Masond1310b22008-01-24 16:13:08 -05001140/* wrappers around set/clear extent bit */
1141int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1142 gfp_t mask)
1143{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001144 return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001145 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001146}
Chris Masond1310b22008-01-24 16:13:08 -05001147
1148int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1149 int bits, gfp_t mask)
1150{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001151 return set_extent_bit(tree, start, end, bits, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001152 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001153}
Chris Masond1310b22008-01-24 16:13:08 -05001154
1155int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1156 int bits, gfp_t mask)
1157{
Chris Mason2c64c532009-09-02 15:04:12 -04001158 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001159}
Chris Masond1310b22008-01-24 16:13:08 -05001160
1161int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001162 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001163{
1164 return set_extent_bit(tree, start, end,
Liu Bofee187d2011-09-29 15:55:28 +08001165 EXTENT_DELALLOC | EXTENT_UPTODATE,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001166 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001167}
Chris Masond1310b22008-01-24 16:13:08 -05001168
Liu Bo9e8a4a82012-09-05 19:10:51 -06001169int set_extent_defrag(struct extent_io_tree *tree, u64 start, u64 end,
1170 struct extent_state **cached_state, gfp_t mask)
1171{
1172 return set_extent_bit(tree, start, end,
1173 EXTENT_DELALLOC | EXTENT_UPTODATE | EXTENT_DEFRAG,
1174 NULL, cached_state, mask);
1175}
1176
Chris Masond1310b22008-01-24 16:13:08 -05001177int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1178 gfp_t mask)
1179{
1180 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04001181 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001182 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001183}
Chris Masond1310b22008-01-24 16:13:08 -05001184
1185int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1186 gfp_t mask)
1187{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001188 return set_extent_bit(tree, start, end, EXTENT_NEW, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001189 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001190}
Chris Masond1310b22008-01-24 16:13:08 -05001191
Chris Masond1310b22008-01-24 16:13:08 -05001192int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +00001193 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001194{
Arne Jansen507903b2011-04-06 10:02:20 +00001195 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001196 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001197}
Chris Masond1310b22008-01-24 16:13:08 -05001198
Josef Bacik5fd02042012-05-02 14:00:54 -04001199int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1200 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001201{
Chris Mason2c64c532009-09-02 15:04:12 -04001202 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001203 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001204}
Chris Masond1310b22008-01-24 16:13:08 -05001205
Chris Masond352ac62008-09-29 15:18:18 -04001206/*
1207 * either insert or lock state struct between start and end use mask to tell
1208 * us if waiting is desired.
1209 */
Chris Mason1edbb732009-09-02 13:24:36 -04001210int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001211 int bits, struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001212{
1213 int err;
1214 u64 failed_start;
1215 while (1) {
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001216 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
1217 EXTENT_LOCKED, &failed_start,
1218 cached_state, GFP_NOFS);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001219 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001220 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1221 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001222 } else
Chris Masond1310b22008-01-24 16:13:08 -05001223 break;
Chris Masond1310b22008-01-24 16:13:08 -05001224 WARN_ON(start > end);
1225 }
1226 return err;
1227}
Chris Masond1310b22008-01-24 16:13:08 -05001228
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001229int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Mason1edbb732009-09-02 13:24:36 -04001230{
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001231 return lock_extent_bits(tree, start, end, 0, NULL);
Chris Mason1edbb732009-09-02 13:24:36 -04001232}
1233
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001234int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001235{
1236 int err;
1237 u64 failed_start;
1238
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001239 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1240 &failed_start, NULL, GFP_NOFS);
Yan Zheng66435582008-10-30 14:19:50 -04001241 if (err == -EEXIST) {
1242 if (failed_start > start)
1243 clear_extent_bit(tree, start, failed_start - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001244 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
Josef Bacik25179202008-10-29 14:49:05 -04001245 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001246 }
Josef Bacik25179202008-10-29 14:49:05 -04001247 return 1;
1248}
Josef Bacik25179202008-10-29 14:49:05 -04001249
Chris Mason2c64c532009-09-02 15:04:12 -04001250int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1251 struct extent_state **cached, gfp_t mask)
1252{
1253 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1254 mask);
1255}
1256
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001257int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001258{
Chris Mason2c64c532009-09-02 15:04:12 -04001259 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001260 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001261}
Chris Masond1310b22008-01-24 16:13:08 -05001262
1263/*
Chris Masond1310b22008-01-24 16:13:08 -05001264 * helper function to set both pages and extents in the tree writeback
1265 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001266static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001267{
1268 unsigned long index = start >> PAGE_CACHE_SHIFT;
1269 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1270 struct page *page;
1271
1272 while (index <= end_index) {
1273 page = find_get_page(tree->mapping, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001274 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Masond1310b22008-01-24 16:13:08 -05001275 set_page_writeback(page);
1276 page_cache_release(page);
1277 index++;
1278 }
Chris Masond1310b22008-01-24 16:13:08 -05001279 return 0;
1280}
Chris Masond1310b22008-01-24 16:13:08 -05001281
Chris Masond352ac62008-09-29 15:18:18 -04001282/* find the first state struct with 'bits' set after 'start', and
1283 * return it. tree->lock must be held. NULL will returned if
1284 * nothing was found after 'start'
1285 */
Chris Masond7fc6402008-02-18 12:12:38 -05001286struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1287 u64 start, int bits)
1288{
1289 struct rb_node *node;
1290 struct extent_state *state;
1291
1292 /*
1293 * this search will find all the extents that end after
1294 * our range starts.
1295 */
1296 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001297 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001298 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001299
Chris Masond3977122009-01-05 21:25:51 -05001300 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001301 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001302 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001303 return state;
Chris Masond3977122009-01-05 21:25:51 -05001304
Chris Masond7fc6402008-02-18 12:12:38 -05001305 node = rb_next(node);
1306 if (!node)
1307 break;
1308 }
1309out:
1310 return NULL;
1311}
Chris Masond7fc6402008-02-18 12:12:38 -05001312
Chris Masond352ac62008-09-29 15:18:18 -04001313/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001314 * find the first offset in the io tree with 'bits' set. zero is
1315 * returned if we find something, and *start_ret and *end_ret are
1316 * set to reflect the state struct that was found.
1317 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001318 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001319 */
1320int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
Josef Bacike6138872012-09-27 17:07:30 -04001321 u64 *start_ret, u64 *end_ret, int bits,
1322 struct extent_state **cached_state)
Xiao Guangrong69261c42011-07-14 03:19:45 +00001323{
1324 struct extent_state *state;
Josef Bacike6138872012-09-27 17:07:30 -04001325 struct rb_node *n;
Xiao Guangrong69261c42011-07-14 03:19:45 +00001326 int ret = 1;
1327
1328 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001329 if (cached_state && *cached_state) {
1330 state = *cached_state;
1331 if (state->end == start - 1 && state->tree) {
1332 n = rb_next(&state->rb_node);
1333 while (n) {
1334 state = rb_entry(n, struct extent_state,
1335 rb_node);
1336 if (state->state & bits)
1337 goto got_it;
1338 n = rb_next(n);
1339 }
1340 free_extent_state(*cached_state);
1341 *cached_state = NULL;
1342 goto out;
1343 }
1344 free_extent_state(*cached_state);
1345 *cached_state = NULL;
1346 }
1347
Xiao Guangrong69261c42011-07-14 03:19:45 +00001348 state = find_first_extent_bit_state(tree, start, bits);
Josef Bacike6138872012-09-27 17:07:30 -04001349got_it:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001350 if (state) {
Josef Bacike6138872012-09-27 17:07:30 -04001351 cache_state(state, cached_state);
Xiao Guangrong69261c42011-07-14 03:19:45 +00001352 *start_ret = state->start;
1353 *end_ret = state->end;
1354 ret = 0;
1355 }
Josef Bacike6138872012-09-27 17:07:30 -04001356out:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001357 spin_unlock(&tree->lock);
1358 return ret;
1359}
1360
1361/*
Chris Masond352ac62008-09-29 15:18:18 -04001362 * find a contiguous range of bytes in the file marked as delalloc, not
1363 * more than 'max_bytes'. start and end are used to return the range,
1364 *
1365 * 1 is returned if we find something, 0 if nothing was in the tree
1366 */
Chris Masonc8b97812008-10-29 14:49:59 -04001367static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001368 u64 *start, u64 *end, u64 max_bytes,
1369 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001370{
1371 struct rb_node *node;
1372 struct extent_state *state;
1373 u64 cur_start = *start;
1374 u64 found = 0;
1375 u64 total_bytes = 0;
1376
Chris Masoncad321a2008-12-17 14:51:42 -05001377 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001378
Chris Masond1310b22008-01-24 16:13:08 -05001379 /*
1380 * this search will find all the extents that end after
1381 * our range starts.
1382 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001383 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001384 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001385 if (!found)
1386 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001387 goto out;
1388 }
1389
Chris Masond3977122009-01-05 21:25:51 -05001390 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001391 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001392 if (found && (state->start != cur_start ||
1393 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001394 goto out;
1395 }
1396 if (!(state->state & EXTENT_DELALLOC)) {
1397 if (!found)
1398 *end = state->end;
1399 goto out;
1400 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001401 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001402 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001403 *cached_state = state;
1404 atomic_inc(&state->refs);
1405 }
Chris Masond1310b22008-01-24 16:13:08 -05001406 found++;
1407 *end = state->end;
1408 cur_start = state->end + 1;
1409 node = rb_next(node);
1410 if (!node)
1411 break;
1412 total_bytes += state->end - state->start + 1;
1413 if (total_bytes >= max_bytes)
1414 break;
1415 }
1416out:
Chris Masoncad321a2008-12-17 14:51:42 -05001417 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001418 return found;
1419}
1420
Jeff Mahoney143bede2012-03-01 14:56:26 +01001421static noinline void __unlock_for_delalloc(struct inode *inode,
1422 struct page *locked_page,
1423 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001424{
1425 int ret;
1426 struct page *pages[16];
1427 unsigned long index = start >> PAGE_CACHE_SHIFT;
1428 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1429 unsigned long nr_pages = end_index - index + 1;
1430 int i;
1431
1432 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001433 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001434
Chris Masond3977122009-01-05 21:25:51 -05001435 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001436 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001437 min_t(unsigned long, nr_pages,
1438 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001439 for (i = 0; i < ret; i++) {
1440 if (pages[i] != locked_page)
1441 unlock_page(pages[i]);
1442 page_cache_release(pages[i]);
1443 }
1444 nr_pages -= ret;
1445 index += ret;
1446 cond_resched();
1447 }
Chris Masonc8b97812008-10-29 14:49:59 -04001448}
1449
1450static noinline int lock_delalloc_pages(struct inode *inode,
1451 struct page *locked_page,
1452 u64 delalloc_start,
1453 u64 delalloc_end)
1454{
1455 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1456 unsigned long start_index = index;
1457 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1458 unsigned long pages_locked = 0;
1459 struct page *pages[16];
1460 unsigned long nrpages;
1461 int ret;
1462 int i;
1463
1464 /* the caller is responsible for locking the start index */
1465 if (index == locked_page->index && index == end_index)
1466 return 0;
1467
1468 /* skip the page at the start index */
1469 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001470 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001471 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001472 min_t(unsigned long,
1473 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001474 if (ret == 0) {
1475 ret = -EAGAIN;
1476 goto done;
1477 }
1478 /* now we have an array of pages, lock them all */
1479 for (i = 0; i < ret; i++) {
1480 /*
1481 * the caller is taking responsibility for
1482 * locked_page
1483 */
Chris Mason771ed682008-11-06 22:02:51 -05001484 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001485 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001486 if (!PageDirty(pages[i]) ||
1487 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001488 ret = -EAGAIN;
1489 unlock_page(pages[i]);
1490 page_cache_release(pages[i]);
1491 goto done;
1492 }
1493 }
Chris Masonc8b97812008-10-29 14:49:59 -04001494 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001495 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001496 }
Chris Masonc8b97812008-10-29 14:49:59 -04001497 nrpages -= ret;
1498 index += ret;
1499 cond_resched();
1500 }
1501 ret = 0;
1502done:
1503 if (ret && pages_locked) {
1504 __unlock_for_delalloc(inode, locked_page,
1505 delalloc_start,
1506 ((u64)(start_index + pages_locked - 1)) <<
1507 PAGE_CACHE_SHIFT);
1508 }
1509 return ret;
1510}
1511
1512/*
1513 * find a contiguous range of bytes in the file marked as delalloc, not
1514 * more than 'max_bytes'. start and end are used to return the range,
1515 *
1516 * 1 is returned if we find something, 0 if nothing was in the tree
1517 */
1518static noinline u64 find_lock_delalloc_range(struct inode *inode,
1519 struct extent_io_tree *tree,
1520 struct page *locked_page,
1521 u64 *start, u64 *end,
1522 u64 max_bytes)
1523{
1524 u64 delalloc_start;
1525 u64 delalloc_end;
1526 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001527 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001528 int ret;
1529 int loops = 0;
1530
1531again:
1532 /* step one, find a bunch of delalloc bytes starting at start */
1533 delalloc_start = *start;
1534 delalloc_end = 0;
1535 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001536 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001537 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001538 *start = delalloc_start;
1539 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001540 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001541 return found;
1542 }
1543
1544 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001545 * start comes from the offset of locked_page. We have to lock
1546 * pages in order, so we can't process delalloc bytes before
1547 * locked_page
1548 */
Chris Masond3977122009-01-05 21:25:51 -05001549 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001550 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001551
1552 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001553 * make sure to limit the number of pages we try to lock down
1554 * if we're looping.
1555 */
Chris Masond3977122009-01-05 21:25:51 -05001556 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001557 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001558
Chris Masonc8b97812008-10-29 14:49:59 -04001559 /* step two, lock all the pages after the page that has start */
1560 ret = lock_delalloc_pages(inode, locked_page,
1561 delalloc_start, delalloc_end);
1562 if (ret == -EAGAIN) {
1563 /* some of the pages are gone, lets avoid looping by
1564 * shortening the size of the delalloc range we're searching
1565 */
Chris Mason9655d292009-09-02 15:22:30 -04001566 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001567 if (!loops) {
1568 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1569 max_bytes = PAGE_CACHE_SIZE - offset;
1570 loops = 1;
1571 goto again;
1572 } else {
1573 found = 0;
1574 goto out_failed;
1575 }
1576 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001577 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
Chris Masonc8b97812008-10-29 14:49:59 -04001578
1579 /* step three, lock the state bits for the whole range */
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001580 lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001581
1582 /* then test to make sure it is all still delalloc */
1583 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001584 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001585 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001586 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1587 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001588 __unlock_for_delalloc(inode, locked_page,
1589 delalloc_start, delalloc_end);
1590 cond_resched();
1591 goto again;
1592 }
Chris Mason9655d292009-09-02 15:22:30 -04001593 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001594 *start = delalloc_start;
1595 *end = delalloc_end;
1596out_failed:
1597 return found;
1598}
1599
1600int extent_clear_unlock_delalloc(struct inode *inode,
1601 struct extent_io_tree *tree,
1602 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001603 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001604{
1605 int ret;
1606 struct page *pages[16];
1607 unsigned long index = start >> PAGE_CACHE_SHIFT;
1608 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1609 unsigned long nr_pages = end_index - index + 1;
1610 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001611 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001612
Chris Masona791e352009-10-08 11:27:10 -04001613 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001614 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001615 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001616 clear_bits |= EXTENT_DIRTY;
1617
Chris Masona791e352009-10-08 11:27:10 -04001618 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001619 clear_bits |= EXTENT_DELALLOC;
1620
Chris Mason2c64c532009-09-02 15:04:12 -04001621 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001622 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1623 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1624 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001625 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001626
Chris Masond3977122009-01-05 21:25:51 -05001627 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001628 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001629 min_t(unsigned long,
1630 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001631 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001632
Chris Masona791e352009-10-08 11:27:10 -04001633 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001634 SetPagePrivate2(pages[i]);
1635
Chris Masonc8b97812008-10-29 14:49:59 -04001636 if (pages[i] == locked_page) {
1637 page_cache_release(pages[i]);
1638 continue;
1639 }
Chris Masona791e352009-10-08 11:27:10 -04001640 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001641 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001642 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001643 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001644 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001645 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001646 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001647 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001648 page_cache_release(pages[i]);
1649 }
1650 nr_pages -= ret;
1651 index += ret;
1652 cond_resched();
1653 }
1654 return 0;
1655}
Chris Masonc8b97812008-10-29 14:49:59 -04001656
Chris Masond352ac62008-09-29 15:18:18 -04001657/*
1658 * count the number of bytes in the tree that have a given bit(s)
1659 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1660 * cached. The total number found is returned.
1661 */
Chris Masond1310b22008-01-24 16:13:08 -05001662u64 count_range_bits(struct extent_io_tree *tree,
1663 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001664 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001665{
1666 struct rb_node *node;
1667 struct extent_state *state;
1668 u64 cur_start = *start;
1669 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001670 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001671 int found = 0;
1672
1673 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001674 WARN_ON(1);
1675 return 0;
1676 }
1677
Chris Masoncad321a2008-12-17 14:51:42 -05001678 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001679 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1680 total_bytes = tree->dirty_bytes;
1681 goto out;
1682 }
1683 /*
1684 * this search will find all the extents that end after
1685 * our range starts.
1686 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001687 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001688 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001689 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001690
Chris Masond3977122009-01-05 21:25:51 -05001691 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001692 state = rb_entry(node, struct extent_state, rb_node);
1693 if (state->start > search_end)
1694 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001695 if (contig && found && state->start > last + 1)
1696 break;
1697 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001698 total_bytes += min(search_end, state->end) + 1 -
1699 max(cur_start, state->start);
1700 if (total_bytes >= max_bytes)
1701 break;
1702 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001703 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001704 found = 1;
1705 }
Chris Masonec29ed52011-02-23 16:23:20 -05001706 last = state->end;
1707 } else if (contig && found) {
1708 break;
Chris Masond1310b22008-01-24 16:13:08 -05001709 }
1710 node = rb_next(node);
1711 if (!node)
1712 break;
1713 }
1714out:
Chris Masoncad321a2008-12-17 14:51:42 -05001715 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001716 return total_bytes;
1717}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001718
Chris Masond352ac62008-09-29 15:18:18 -04001719/*
1720 * set the private field for a given byte offset in the tree. If there isn't
1721 * an extent_state there already, this does nothing.
1722 */
Chris Masond1310b22008-01-24 16:13:08 -05001723int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1724{
1725 struct rb_node *node;
1726 struct extent_state *state;
1727 int ret = 0;
1728
Chris Masoncad321a2008-12-17 14:51:42 -05001729 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001730 /*
1731 * this search will find all the extents that end after
1732 * our range starts.
1733 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001734 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001735 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001736 ret = -ENOENT;
1737 goto out;
1738 }
1739 state = rb_entry(node, struct extent_state, rb_node);
1740 if (state->start != start) {
1741 ret = -ENOENT;
1742 goto out;
1743 }
1744 state->private = private;
1745out:
Chris Masoncad321a2008-12-17 14:51:42 -05001746 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001747 return ret;
1748}
1749
1750int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1751{
1752 struct rb_node *node;
1753 struct extent_state *state;
1754 int ret = 0;
1755
Chris Masoncad321a2008-12-17 14:51:42 -05001756 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001757 /*
1758 * this search will find all the extents that end after
1759 * our range starts.
1760 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001761 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001762 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001763 ret = -ENOENT;
1764 goto out;
1765 }
1766 state = rb_entry(node, struct extent_state, rb_node);
1767 if (state->start != start) {
1768 ret = -ENOENT;
1769 goto out;
1770 }
1771 *private = state->private;
1772out:
Chris Masoncad321a2008-12-17 14:51:42 -05001773 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001774 return ret;
1775}
1776
1777/*
1778 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001779 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001780 * has the bits set. Otherwise, 1 is returned if any bit in the
1781 * range is found set.
1782 */
1783int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001784 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001785{
1786 struct extent_state *state = NULL;
1787 struct rb_node *node;
1788 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001789
Chris Masoncad321a2008-12-17 14:51:42 -05001790 spin_lock(&tree->lock);
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001791 if (cached && cached->tree && cached->start <= start &&
1792 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001793 node = &cached->rb_node;
1794 else
1795 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001796 while (node && start <= end) {
1797 state = rb_entry(node, struct extent_state, rb_node);
1798
1799 if (filled && state->start > start) {
1800 bitset = 0;
1801 break;
1802 }
1803
1804 if (state->start > end)
1805 break;
1806
1807 if (state->state & bits) {
1808 bitset = 1;
1809 if (!filled)
1810 break;
1811 } else if (filled) {
1812 bitset = 0;
1813 break;
1814 }
Chris Mason46562cec2009-09-23 20:23:16 -04001815
1816 if (state->end == (u64)-1)
1817 break;
1818
Chris Masond1310b22008-01-24 16:13:08 -05001819 start = state->end + 1;
1820 if (start > end)
1821 break;
1822 node = rb_next(node);
1823 if (!node) {
1824 if (filled)
1825 bitset = 0;
1826 break;
1827 }
1828 }
Chris Masoncad321a2008-12-17 14:51:42 -05001829 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001830 return bitset;
1831}
Chris Masond1310b22008-01-24 16:13:08 -05001832
1833/*
1834 * helper function to set a given page up to date if all the
1835 * extents in the tree for that page are up to date
1836 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001837static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001838{
1839 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1840 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001841 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001842 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001843}
1844
1845/*
1846 * helper function to unlock a page if all the extents in the tree
1847 * for that page are unlocked
1848 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001849static void check_page_locked(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001850{
1851 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1852 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001853 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001854 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05001855}
1856
1857/*
1858 * helper function to end page writeback if all the extents
1859 * in the tree for that page are done with writeback
1860 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001861static void check_page_writeback(struct extent_io_tree *tree,
1862 struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001863{
Chris Mason1edbb732009-09-02 13:24:36 -04001864 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001865}
1866
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001867/*
1868 * When IO fails, either with EIO or csum verification fails, we
1869 * try other mirrors that might have a good copy of the data. This
1870 * io_failure_record is used to record state as we go through all the
1871 * mirrors. If another mirror has good data, the page is set up to date
1872 * and things continue. If a good mirror can't be found, the original
1873 * bio end_io callback is called to indicate things have failed.
1874 */
1875struct io_failure_record {
1876 struct page *page;
1877 u64 start;
1878 u64 len;
1879 u64 logical;
1880 unsigned long bio_flags;
1881 int this_mirror;
1882 int failed_mirror;
1883 int in_validation;
1884};
1885
1886static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
1887 int did_repair)
1888{
1889 int ret;
1890 int err = 0;
1891 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1892
1893 set_state_private(failure_tree, rec->start, 0);
1894 ret = clear_extent_bits(failure_tree, rec->start,
1895 rec->start + rec->len - 1,
1896 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1897 if (ret)
1898 err = ret;
1899
1900 if (did_repair) {
1901 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1902 rec->start + rec->len - 1,
1903 EXTENT_DAMAGED, GFP_NOFS);
1904 if (ret && !err)
1905 err = ret;
1906 }
1907
1908 kfree(rec);
1909 return err;
1910}
1911
1912static void repair_io_failure_callback(struct bio *bio, int err)
1913{
1914 complete(bio->bi_private);
1915}
1916
1917/*
1918 * this bypasses the standard btrfs submit functions deliberately, as
1919 * the standard behavior is to write all copies in a raid setup. here we only
1920 * want to write the one bad copy. so we do the mapping for ourselves and issue
1921 * submit_bio directly.
1922 * to avoid any synchonization issues, wait for the data after writing, which
1923 * actually prevents the read that triggered the error from finishing.
1924 * currently, there can be no more than two copies of every data bit. thus,
1925 * exactly one rewrite is required.
1926 */
1927int repair_io_failure(struct btrfs_mapping_tree *map_tree, u64 start,
1928 u64 length, u64 logical, struct page *page,
1929 int mirror_num)
1930{
1931 struct bio *bio;
1932 struct btrfs_device *dev;
1933 DECLARE_COMPLETION_ONSTACK(compl);
1934 u64 map_length = 0;
1935 u64 sector;
1936 struct btrfs_bio *bbio = NULL;
1937 int ret;
1938
1939 BUG_ON(!mirror_num);
1940
1941 bio = bio_alloc(GFP_NOFS, 1);
1942 if (!bio)
1943 return -EIO;
1944 bio->bi_private = &compl;
1945 bio->bi_end_io = repair_io_failure_callback;
1946 bio->bi_size = 0;
1947 map_length = length;
1948
1949 ret = btrfs_map_block(map_tree, WRITE, logical,
1950 &map_length, &bbio, mirror_num);
1951 if (ret) {
1952 bio_put(bio);
1953 return -EIO;
1954 }
1955 BUG_ON(mirror_num != bbio->mirror_num);
1956 sector = bbio->stripes[mirror_num-1].physical >> 9;
1957 bio->bi_sector = sector;
1958 dev = bbio->stripes[mirror_num-1].dev;
1959 kfree(bbio);
1960 if (!dev || !dev->bdev || !dev->writeable) {
1961 bio_put(bio);
1962 return -EIO;
1963 }
1964 bio->bi_bdev = dev->bdev;
1965 bio_add_page(bio, page, length, start-page_offset(page));
Stefan Behrens21adbd52011-11-09 13:44:05 +01001966 btrfsic_submit_bio(WRITE_SYNC, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001967 wait_for_completion(&compl);
1968
1969 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
1970 /* try to remap that extent elsewhere? */
1971 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001972 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001973 return -EIO;
1974 }
1975
Anand Jaind5b025d2012-07-02 22:05:21 -06001976 printk_ratelimited_in_rcu(KERN_INFO "btrfs read error corrected: ino %lu off %llu "
Josef Bacik606686e2012-06-04 14:03:51 -04001977 "(dev %s sector %llu)\n", page->mapping->host->i_ino,
1978 start, rcu_str_deref(dev->name), sector);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001979
1980 bio_put(bio);
1981 return 0;
1982}
1983
Josef Bacikea466792012-03-26 21:57:36 -04001984int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
1985 int mirror_num)
1986{
1987 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1988 u64 start = eb->start;
1989 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond95603b2012-04-12 15:55:15 -04001990 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04001991
1992 for (i = 0; i < num_pages; i++) {
1993 struct page *p = extent_buffer_page(eb, i);
1994 ret = repair_io_failure(map_tree, start, PAGE_CACHE_SIZE,
1995 start, p, mirror_num);
1996 if (ret)
1997 break;
1998 start += PAGE_CACHE_SIZE;
1999 }
2000
2001 return ret;
2002}
2003
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002004/*
2005 * each time an IO finishes, we do a fast check in the IO failure tree
2006 * to see if we need to process or clean up an io_failure_record
2007 */
2008static int clean_io_failure(u64 start, struct page *page)
2009{
2010 u64 private;
2011 u64 private_failure;
2012 struct io_failure_record *failrec;
2013 struct btrfs_mapping_tree *map_tree;
2014 struct extent_state *state;
2015 int num_copies;
2016 int did_repair = 0;
2017 int ret;
2018 struct inode *inode = page->mapping->host;
2019
2020 private = 0;
2021 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
2022 (u64)-1, 1, EXTENT_DIRTY, 0);
2023 if (!ret)
2024 return 0;
2025
2026 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
2027 &private_failure);
2028 if (ret)
2029 return 0;
2030
2031 failrec = (struct io_failure_record *)(unsigned long) private_failure;
2032 BUG_ON(!failrec->this_mirror);
2033
2034 if (failrec->in_validation) {
2035 /* there was no real error, just free the record */
2036 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
2037 failrec->start);
2038 did_repair = 1;
2039 goto out;
2040 }
2041
2042 spin_lock(&BTRFS_I(inode)->io_tree.lock);
2043 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
2044 failrec->start,
2045 EXTENT_LOCKED);
2046 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
2047
2048 if (state && state->start == failrec->start) {
2049 map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree;
2050 num_copies = btrfs_num_copies(map_tree, failrec->logical,
2051 failrec->len);
2052 if (num_copies > 1) {
2053 ret = repair_io_failure(map_tree, start, failrec->len,
2054 failrec->logical, page,
2055 failrec->failed_mirror);
2056 did_repair = !ret;
2057 }
2058 }
2059
2060out:
2061 if (!ret)
2062 ret = free_io_failure(inode, failrec, did_repair);
2063
2064 return ret;
2065}
2066
2067/*
2068 * this is a generic handler for readpage errors (default
2069 * readpage_io_failed_hook). if other copies exist, read those and write back
2070 * good data to the failed position. does not investigate in remapping the
2071 * failed extent elsewhere, hoping the device will be smart enough to do this as
2072 * needed
2073 */
2074
2075static int bio_readpage_error(struct bio *failed_bio, struct page *page,
2076 u64 start, u64 end, int failed_mirror,
2077 struct extent_state *state)
2078{
2079 struct io_failure_record *failrec = NULL;
2080 u64 private;
2081 struct extent_map *em;
2082 struct inode *inode = page->mapping->host;
2083 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2084 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2085 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2086 struct bio *bio;
2087 int num_copies;
2088 int ret;
2089 int read_mode;
2090 u64 logical;
2091
2092 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2093
2094 ret = get_state_private(failure_tree, start, &private);
2095 if (ret) {
2096 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2097 if (!failrec)
2098 return -ENOMEM;
2099 failrec->start = start;
2100 failrec->len = end - start + 1;
2101 failrec->this_mirror = 0;
2102 failrec->bio_flags = 0;
2103 failrec->in_validation = 0;
2104
2105 read_lock(&em_tree->lock);
2106 em = lookup_extent_mapping(em_tree, start, failrec->len);
2107 if (!em) {
2108 read_unlock(&em_tree->lock);
2109 kfree(failrec);
2110 return -EIO;
2111 }
2112
2113 if (em->start > start || em->start + em->len < start) {
2114 free_extent_map(em);
2115 em = NULL;
2116 }
2117 read_unlock(&em_tree->lock);
2118
Tsutomu Itoh7a2d6a62012-10-01 03:07:15 -06002119 if (!em) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002120 kfree(failrec);
2121 return -EIO;
2122 }
2123 logical = start - em->start;
2124 logical = em->block_start + logical;
2125 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2126 logical = em->block_start;
2127 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2128 extent_set_compress_type(&failrec->bio_flags,
2129 em->compress_type);
2130 }
2131 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2132 "len=%llu\n", logical, start, failrec->len);
2133 failrec->logical = logical;
2134 free_extent_map(em);
2135
2136 /* set the bits in the private failure tree */
2137 ret = set_extent_bits(failure_tree, start, end,
2138 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2139 if (ret >= 0)
2140 ret = set_state_private(failure_tree, start,
2141 (u64)(unsigned long)failrec);
2142 /* set the bits in the inode's tree */
2143 if (ret >= 0)
2144 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2145 GFP_NOFS);
2146 if (ret < 0) {
2147 kfree(failrec);
2148 return ret;
2149 }
2150 } else {
2151 failrec = (struct io_failure_record *)(unsigned long)private;
2152 pr_debug("bio_readpage_error: (found) logical=%llu, "
2153 "start=%llu, len=%llu, validation=%d\n",
2154 failrec->logical, failrec->start, failrec->len,
2155 failrec->in_validation);
2156 /*
2157 * when data can be on disk more than twice, add to failrec here
2158 * (e.g. with a list for failed_mirror) to make
2159 * clean_io_failure() clean all those errors at once.
2160 */
2161 }
2162 num_copies = btrfs_num_copies(
2163 &BTRFS_I(inode)->root->fs_info->mapping_tree,
2164 failrec->logical, failrec->len);
2165 if (num_copies == 1) {
2166 /*
2167 * we only have a single copy of the data, so don't bother with
2168 * all the retry and error correction code that follows. no
2169 * matter what the error is, it is very likely to persist.
2170 */
2171 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2172 "state=%p, num_copies=%d, next_mirror %d, "
2173 "failed_mirror %d\n", state, num_copies,
2174 failrec->this_mirror, failed_mirror);
2175 free_io_failure(inode, failrec, 0);
2176 return -EIO;
2177 }
2178
2179 if (!state) {
2180 spin_lock(&tree->lock);
2181 state = find_first_extent_bit_state(tree, failrec->start,
2182 EXTENT_LOCKED);
2183 if (state && state->start != failrec->start)
2184 state = NULL;
2185 spin_unlock(&tree->lock);
2186 }
2187
2188 /*
2189 * there are two premises:
2190 * a) deliver good data to the caller
2191 * b) correct the bad sectors on disk
2192 */
2193 if (failed_bio->bi_vcnt > 1) {
2194 /*
2195 * to fulfill b), we need to know the exact failing sectors, as
2196 * we don't want to rewrite any more than the failed ones. thus,
2197 * we need separate read requests for the failed bio
2198 *
2199 * if the following BUG_ON triggers, our validation request got
2200 * merged. we need separate requests for our algorithm to work.
2201 */
2202 BUG_ON(failrec->in_validation);
2203 failrec->in_validation = 1;
2204 failrec->this_mirror = failed_mirror;
2205 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2206 } else {
2207 /*
2208 * we're ready to fulfill a) and b) alongside. get a good copy
2209 * of the failed sector and if we succeed, we have setup
2210 * everything for repair_io_failure to do the rest for us.
2211 */
2212 if (failrec->in_validation) {
2213 BUG_ON(failrec->this_mirror != failed_mirror);
2214 failrec->in_validation = 0;
2215 failrec->this_mirror = 0;
2216 }
2217 failrec->failed_mirror = failed_mirror;
2218 failrec->this_mirror++;
2219 if (failrec->this_mirror == failed_mirror)
2220 failrec->this_mirror++;
2221 read_mode = READ_SYNC;
2222 }
2223
2224 if (!state || failrec->this_mirror > num_copies) {
2225 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2226 "next_mirror %d, failed_mirror %d\n", state,
2227 num_copies, failrec->this_mirror, failed_mirror);
2228 free_io_failure(inode, failrec, 0);
2229 return -EIO;
2230 }
2231
2232 bio = bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04002233 if (!bio) {
2234 free_io_failure(inode, failrec, 0);
2235 return -EIO;
2236 }
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002237 bio->bi_private = state;
2238 bio->bi_end_io = failed_bio->bi_end_io;
2239 bio->bi_sector = failrec->logical >> 9;
2240 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2241 bio->bi_size = 0;
2242
2243 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2244
2245 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2246 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2247 failrec->this_mirror, num_copies, failrec->in_validation);
2248
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002249 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2250 failrec->this_mirror,
2251 failrec->bio_flags, 0);
2252 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002253}
2254
Chris Masond1310b22008-01-24 16:13:08 -05002255/* lots and lots of room for performance fixes in the end_bio funcs */
2256
Jeff Mahoney87826df2012-02-15 16:23:57 +01002257int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2258{
2259 int uptodate = (err == 0);
2260 struct extent_io_tree *tree;
2261 int ret;
2262
2263 tree = &BTRFS_I(page->mapping->host)->io_tree;
2264
2265 if (tree->ops && tree->ops->writepage_end_io_hook) {
2266 ret = tree->ops->writepage_end_io_hook(page, start,
2267 end, NULL, uptodate);
2268 if (ret)
2269 uptodate = 0;
2270 }
2271
Jeff Mahoney87826df2012-02-15 16:23:57 +01002272 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002273 ClearPageUptodate(page);
2274 SetPageError(page);
2275 }
2276 return 0;
2277}
2278
Chris Masond1310b22008-01-24 16:13:08 -05002279/*
2280 * after a writepage IO is done, we need to:
2281 * clear the uptodate bits on error
2282 * clear the writeback bits in the extent tree for this IO
2283 * end_page_writeback if the page has no more pending IO
2284 *
2285 * Scheduling is not allowed, so the extent state tree is expected
2286 * to have one and only one object corresponding to this IO.
2287 */
Chris Masond1310b22008-01-24 16:13:08 -05002288static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002289{
Chris Masond1310b22008-01-24 16:13:08 -05002290 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04002291 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002292 u64 start;
2293 u64 end;
2294 int whole_page;
2295
Chris Masond1310b22008-01-24 16:13:08 -05002296 do {
2297 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04002298 tree = &BTRFS_I(page->mapping->host)->io_tree;
2299
Chris Masond1310b22008-01-24 16:13:08 -05002300 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2301 bvec->bv_offset;
2302 end = start + bvec->bv_len - 1;
2303
2304 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2305 whole_page = 1;
2306 else
2307 whole_page = 0;
2308
2309 if (--bvec >= bio->bi_io_vec)
2310 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04002311
Jeff Mahoney87826df2012-02-15 16:23:57 +01002312 if (end_extent_writepage(page, err, start, end))
2313 continue;
Chris Mason70dec802008-01-29 09:59:12 -05002314
Chris Masond1310b22008-01-24 16:13:08 -05002315 if (whole_page)
2316 end_page_writeback(page);
2317 else
2318 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002319 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04002320
Chris Masond1310b22008-01-24 16:13:08 -05002321 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002322}
2323
2324/*
2325 * after a readpage IO is done, we need to:
2326 * clear the uptodate bits on error
2327 * set the uptodate bits if things worked
2328 * set the page up to date if all extents in the tree are uptodate
2329 * clear the lock bit in the extent tree
2330 * unlock the page if there are no other extents locked for it
2331 *
2332 * Scheduling is not allowed, so the extent state tree is expected
2333 * to have one and only one object corresponding to this IO.
2334 */
Chris Masond1310b22008-01-24 16:13:08 -05002335static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002336{
2337 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00002338 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2339 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04002340 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002341 u64 start;
2342 u64 end;
2343 int whole_page;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002344 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002345 int ret;
2346
Chris Masond20f7042008-12-08 16:58:54 -05002347 if (err)
2348 uptodate = 0;
2349
Chris Masond1310b22008-01-24 16:13:08 -05002350 do {
2351 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00002352 struct extent_state *cached = NULL;
2353 struct extent_state *state;
2354
Kent Overstreetbe3940c2012-09-11 14:23:05 -06002355 pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, "
2356 "mirror=%ld\n", (u64)bio->bi_sector, err,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002357 (long int)bio->bi_bdev);
David Woodhouse902b22f2008-08-20 08:51:49 -04002358 tree = &BTRFS_I(page->mapping->host)->io_tree;
2359
Chris Masond1310b22008-01-24 16:13:08 -05002360 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2361 bvec->bv_offset;
2362 end = start + bvec->bv_len - 1;
2363
2364 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2365 whole_page = 1;
2366 else
2367 whole_page = 0;
2368
Chris Mason4125bf72010-02-03 18:18:45 +00002369 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05002370 prefetchw(&bvec->bv_page->flags);
2371
Arne Jansen507903b2011-04-06 10:02:20 +00002372 spin_lock(&tree->lock);
Chris Mason0d399202011-04-16 06:55:39 -04002373 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
Chris Mason109b36a2011-04-12 13:57:39 -04002374 if (state && state->start == start) {
Arne Jansen507903b2011-04-06 10:02:20 +00002375 /*
2376 * take a reference on the state, unlock will drop
2377 * the ref
2378 */
2379 cache_state(state, &cached);
2380 }
2381 spin_unlock(&tree->lock);
2382
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002383 mirror = (int)(unsigned long)bio->bi_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002384 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05002385 ret = tree->ops->readpage_end_io_hook(page, start, end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002386 state, mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002387 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002388 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002389 else
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002390 clean_io_failure(start, page);
Chris Masond1310b22008-01-24 16:13:08 -05002391 }
Josef Bacikea466792012-03-26 21:57:36 -04002392
Josef Bacikea466792012-03-26 21:57:36 -04002393 if (!uptodate && tree->ops && tree->ops->readpage_io_failed_hook) {
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002394 ret = tree->ops->readpage_io_failed_hook(page, mirror);
Josef Bacikea466792012-03-26 21:57:36 -04002395 if (!ret && !err &&
2396 test_bit(BIO_UPTODATE, &bio->bi_flags))
2397 uptodate = 1;
2398 } else if (!uptodate) {
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002399 /*
2400 * The generic bio_readpage_error handles errors the
2401 * following way: If possible, new read requests are
2402 * created and submitted and will end up in
2403 * end_bio_extent_readpage as well (if we're lucky, not
2404 * in the !uptodate case). In that case it returns 0 and
2405 * we just go on with the next page in our bio. If it
2406 * can't handle the error it will return -EIO and we
2407 * remain responsible for that page.
2408 */
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002409 ret = bio_readpage_error(bio, page, start, end, mirror, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04002410 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04002411 uptodate =
2412 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05002413 if (err)
2414 uptodate = 0;
Arne Jansen507903b2011-04-06 10:02:20 +00002415 uncache_state(&cached);
Chris Mason7e383262008-04-09 16:28:12 -04002416 continue;
2417 }
2418 }
Chris Mason70dec802008-01-29 09:59:12 -05002419
Josef Bacik0b32f4b2012-03-13 09:38:00 -04002420 if (uptodate && tree->track_uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00002421 set_extent_uptodate(tree, start, end, &cached,
David Woodhouse902b22f2008-08-20 08:51:49 -04002422 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05002423 }
Arne Jansen507903b2011-04-06 10:02:20 +00002424 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05002425
Chris Mason70dec802008-01-29 09:59:12 -05002426 if (whole_page) {
2427 if (uptodate) {
2428 SetPageUptodate(page);
2429 } else {
2430 ClearPageUptodate(page);
2431 SetPageError(page);
2432 }
Chris Masond1310b22008-01-24 16:13:08 -05002433 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05002434 } else {
2435 if (uptodate) {
2436 check_page_uptodate(tree, page);
2437 } else {
2438 ClearPageUptodate(page);
2439 SetPageError(page);
2440 }
Chris Masond1310b22008-01-24 16:13:08 -05002441 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05002442 }
Chris Mason4125bf72010-02-03 18:18:45 +00002443 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05002444
2445 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002446}
2447
Miao Xie88f794e2010-11-22 03:02:55 +00002448struct bio *
2449btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2450 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002451{
2452 struct bio *bio;
2453
2454 bio = bio_alloc(gfp_flags, nr_vecs);
2455
2456 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2457 while (!bio && (nr_vecs /= 2))
2458 bio = bio_alloc(gfp_flags, nr_vecs);
2459 }
2460
2461 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04002462 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002463 bio->bi_bdev = bdev;
2464 bio->bi_sector = first_sector;
2465 }
2466 return bio;
2467}
2468
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002469/*
2470 * Since writes are async, they will only return -ENOMEM.
2471 * Reads can return the full range of I/O error conditions.
2472 */
Jeff Mahoney355808c2011-10-03 23:23:14 -04002473static int __must_check submit_one_bio(int rw, struct bio *bio,
2474 int mirror_num, unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002475{
Chris Masond1310b22008-01-24 16:13:08 -05002476 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002477 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2478 struct page *page = bvec->bv_page;
2479 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002480 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002481
2482 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002483
David Woodhouse902b22f2008-08-20 08:51:49 -04002484 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002485
2486 bio_get(bio);
2487
Chris Mason065631f2008-02-20 12:07:25 -05002488 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00002489 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002490 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002491 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01002492 btrfsic_submit_bio(rw, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002493
Chris Masond1310b22008-01-24 16:13:08 -05002494 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2495 ret = -EOPNOTSUPP;
2496 bio_put(bio);
2497 return ret;
2498}
2499
Jeff Mahoney3444a972011-10-03 23:23:13 -04002500static int merge_bio(struct extent_io_tree *tree, struct page *page,
2501 unsigned long offset, size_t size, struct bio *bio,
2502 unsigned long bio_flags)
2503{
2504 int ret = 0;
2505 if (tree->ops && tree->ops->merge_bio_hook)
2506 ret = tree->ops->merge_bio_hook(page, offset, size, bio,
2507 bio_flags);
2508 BUG_ON(ret < 0);
2509 return ret;
2510
2511}
2512
Chris Masond1310b22008-01-24 16:13:08 -05002513static int submit_extent_page(int rw, struct extent_io_tree *tree,
2514 struct page *page, sector_t sector,
2515 size_t size, unsigned long offset,
2516 struct block_device *bdev,
2517 struct bio **bio_ret,
2518 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04002519 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002520 int mirror_num,
2521 unsigned long prev_bio_flags,
2522 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002523{
2524 int ret = 0;
2525 struct bio *bio;
2526 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04002527 int contig = 0;
2528 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2529 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05002530 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002531
2532 if (bio_ret && *bio_ret) {
2533 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002534 if (old_compressed)
2535 contig = bio->bi_sector == sector;
2536 else
2537 contig = bio->bi_sector + (bio->bi_size >> 9) ==
2538 sector;
2539
2540 if (prev_bio_flags != bio_flags || !contig ||
Jeff Mahoney3444a972011-10-03 23:23:13 -04002541 merge_bio(tree, page, offset, page_size, bio, bio_flags) ||
Chris Masonc8b97812008-10-29 14:49:59 -04002542 bio_add_page(bio, page, page_size, offset) < page_size) {
2543 ret = submit_one_bio(rw, bio, mirror_num,
2544 prev_bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002545 if (ret < 0)
2546 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05002547 bio = NULL;
2548 } else {
2549 return 0;
2550 }
2551 }
Chris Masonc8b97812008-10-29 14:49:59 -04002552 if (this_compressed)
2553 nr = BIO_MAX_PAGES;
2554 else
2555 nr = bio_get_nr_vecs(bdev);
2556
Miao Xie88f794e2010-11-22 03:02:55 +00002557 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002558 if (!bio)
2559 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002560
Chris Masonc8b97812008-10-29 14:49:59 -04002561 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002562 bio->bi_end_io = end_io_func;
2563 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05002564
Chris Masond3977122009-01-05 21:25:51 -05002565 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002566 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002567 else
Chris Masonc8b97812008-10-29 14:49:59 -04002568 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002569
2570 return ret;
2571}
2572
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002573void attach_extent_buffer_page(struct extent_buffer *eb, struct page *page)
2574{
2575 if (!PagePrivate(page)) {
2576 SetPagePrivate(page);
2577 page_cache_get(page);
2578 set_page_private(page, (unsigned long)eb);
2579 } else {
2580 WARN_ON(page->private != (unsigned long)eb);
2581 }
2582}
2583
Chris Masond1310b22008-01-24 16:13:08 -05002584void set_page_extent_mapped(struct page *page)
2585{
2586 if (!PagePrivate(page)) {
2587 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002588 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002589 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002590 }
2591}
2592
Chris Masond1310b22008-01-24 16:13:08 -05002593/*
2594 * basic readpage implementation. Locked extent state structs are inserted
2595 * into the tree that are removed when the IO is done (by the end_io
2596 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002597 * XXX JDM: This needs looking at to ensure proper page locking
Chris Masond1310b22008-01-24 16:13:08 -05002598 */
2599static int __extent_read_full_page(struct extent_io_tree *tree,
2600 struct page *page,
2601 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002602 struct bio **bio, int mirror_num,
2603 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002604{
2605 struct inode *inode = page->mapping->host;
2606 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2607 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2608 u64 end;
2609 u64 cur = start;
2610 u64 extent_offset;
2611 u64 last_byte = i_size_read(inode);
2612 u64 block_start;
2613 u64 cur_end;
2614 sector_t sector;
2615 struct extent_map *em;
2616 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002617 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05002618 int ret;
2619 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002620 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002621 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002622 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002623 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002624 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002625
2626 set_page_extent_mapped(page);
2627
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002628 if (!PageUptodate(page)) {
2629 if (cleancache_get_page(page) == 0) {
2630 BUG_ON(blocksize != PAGE_SIZE);
2631 goto out;
2632 }
2633 }
2634
Chris Masond1310b22008-01-24 16:13:08 -05002635 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002636 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002637 lock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002638 ordered = btrfs_lookup_ordered_extent(inode, start);
2639 if (!ordered)
2640 break;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002641 unlock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002642 btrfs_start_ordered_extent(inode, ordered, 1);
2643 btrfs_put_ordered_extent(ordered);
2644 }
Chris Masond1310b22008-01-24 16:13:08 -05002645
Chris Masonc8b97812008-10-29 14:49:59 -04002646 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2647 char *userpage;
2648 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2649
2650 if (zero_offset) {
2651 iosize = PAGE_CACHE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002652 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002653 memset(userpage + zero_offset, 0, iosize);
2654 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002655 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002656 }
2657 }
Chris Masond1310b22008-01-24 16:13:08 -05002658 while (cur <= end) {
2659 if (cur >= last_byte) {
2660 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002661 struct extent_state *cached = NULL;
2662
David Sterba306e16c2011-04-19 14:29:38 +02002663 iosize = PAGE_CACHE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002664 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002665 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002666 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002667 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002668 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002669 &cached, GFP_NOFS);
2670 unlock_extent_cached(tree, cur, cur + iosize - 1,
2671 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002672 break;
2673 }
David Sterba306e16c2011-04-19 14:29:38 +02002674 em = get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002675 end - cur + 1, 0);
David Sterbac7040052011-04-19 18:00:01 +02002676 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002677 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002678 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002679 break;
2680 }
Chris Masond1310b22008-01-24 16:13:08 -05002681 extent_offset = cur - em->start;
2682 BUG_ON(extent_map_end(em) <= cur);
2683 BUG_ON(end < cur);
2684
Li Zefan261507a02010-12-17 14:21:50 +08002685 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002686 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002687 extent_set_compress_type(&this_bio_flag,
2688 em->compress_type);
2689 }
Chris Masonc8b97812008-10-29 14:49:59 -04002690
Chris Masond1310b22008-01-24 16:13:08 -05002691 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2692 cur_end = min(extent_map_end(em) - 1, end);
2693 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002694 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2695 disk_io_size = em->block_len;
2696 sector = em->block_start >> 9;
2697 } else {
2698 sector = (em->block_start + extent_offset) >> 9;
2699 disk_io_size = iosize;
2700 }
Chris Masond1310b22008-01-24 16:13:08 -05002701 bdev = em->bdev;
2702 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002703 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2704 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002705 free_extent_map(em);
2706 em = NULL;
2707
2708 /* we've found a hole, just zero and go on */
2709 if (block_start == EXTENT_MAP_HOLE) {
2710 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002711 struct extent_state *cached = NULL;
2712
Cong Wang7ac687d2011-11-25 23:14:28 +08002713 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002714 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002715 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002716 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002717
2718 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002719 &cached, GFP_NOFS);
2720 unlock_extent_cached(tree, cur, cur + iosize - 1,
2721 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002722 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002723 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002724 continue;
2725 }
2726 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002727 if (test_range_bit(tree, cur, cur_end,
2728 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002729 check_page_uptodate(tree, page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002730 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002731 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002732 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002733 continue;
2734 }
Chris Mason70dec802008-01-29 09:59:12 -05002735 /* we have an inline extent but it didn't get marked up
2736 * to date. Error out
2737 */
2738 if (block_start == EXTENT_MAP_INLINE) {
2739 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002740 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05002741 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002742 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05002743 continue;
2744 }
Chris Masond1310b22008-01-24 16:13:08 -05002745
2746 ret = 0;
2747 if (tree->ops && tree->ops->readpage_io_hook) {
2748 ret = tree->ops->readpage_io_hook(page, cur,
2749 cur + iosize - 1);
2750 }
2751 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002752 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2753 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002754 ret = submit_extent_page(READ, tree, page,
David Sterba306e16c2011-04-19 14:29:38 +02002755 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04002756 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002757 end_bio_extent_readpage, mirror_num,
2758 *bio_flags,
2759 this_bio_flag);
Josef Bacikedd33c92012-10-05 16:40:32 -04002760 if (!ret) {
2761 nr++;
2762 *bio_flags = this_bio_flag;
2763 }
Chris Masond1310b22008-01-24 16:13:08 -05002764 }
Josef Bacikedd33c92012-10-05 16:40:32 -04002765 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002766 SetPageError(page);
Josef Bacikedd33c92012-10-05 16:40:32 -04002767 unlock_extent(tree, cur, cur + iosize - 1);
2768 }
Chris Masond1310b22008-01-24 16:13:08 -05002769 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002770 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002771 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002772out:
Chris Masond1310b22008-01-24 16:13:08 -05002773 if (!nr) {
2774 if (!PageError(page))
2775 SetPageUptodate(page);
2776 unlock_page(page);
2777 }
2778 return 0;
2779}
2780
2781int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002782 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05002783{
2784 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002785 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002786 int ret;
2787
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002788 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Chris Masonc8b97812008-10-29 14:49:59 -04002789 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002790 if (bio)
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002791 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002792 return ret;
2793}
Chris Masond1310b22008-01-24 16:13:08 -05002794
Chris Mason11c83492009-04-20 15:50:09 -04002795static noinline void update_nr_written(struct page *page,
2796 struct writeback_control *wbc,
2797 unsigned long nr_written)
2798{
2799 wbc->nr_to_write -= nr_written;
2800 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2801 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2802 page->mapping->writeback_index = page->index + nr_written;
2803}
2804
Chris Masond1310b22008-01-24 16:13:08 -05002805/*
2806 * the writepage semantics are similar to regular writepage. extent
2807 * records are inserted to lock ranges in the tree, and as dirty areas
2808 * are found, they are marked writeback. Then the lock bits are removed
2809 * and the end_io handler clears the writeback ranges
2810 */
2811static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2812 void *data)
2813{
2814 struct inode *inode = page->mapping->host;
2815 struct extent_page_data *epd = data;
2816 struct extent_io_tree *tree = epd->tree;
2817 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2818 u64 delalloc_start;
2819 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2820 u64 end;
2821 u64 cur = start;
2822 u64 extent_offset;
2823 u64 last_byte = i_size_read(inode);
2824 u64 block_start;
2825 u64 iosize;
2826 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002827 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002828 struct extent_map *em;
2829 struct block_device *bdev;
2830 int ret;
2831 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002832 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002833 size_t blocksize;
2834 loff_t i_size = i_size_read(inode);
2835 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2836 u64 nr_delalloc;
2837 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002838 int page_started;
2839 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002840 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002841 unsigned long nr_written = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002842 bool fill_delalloc = true;
Chris Masond1310b22008-01-24 16:13:08 -05002843
Chris Masonffbd5172009-04-20 15:50:09 -04002844 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002845 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002846 else
2847 write_flags = WRITE;
2848
liubo1abe9b82011-03-24 11:18:59 +00002849 trace___extent_writepage(page, inode, wbc);
2850
Chris Masond1310b22008-01-24 16:13:08 -05002851 WARN_ON(!PageLocked(page));
Chris Masonbf0da8c2011-11-04 12:29:37 -04002852
2853 ClearPageError(page);
2854
Chris Mason7f3c74f2008-07-18 12:01:11 -04002855 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002856 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002857 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002858 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002859 unlock_page(page);
2860 return 0;
2861 }
2862
2863 if (page->index == end_index) {
2864 char *userpage;
2865
Cong Wang7ac687d2011-11-25 23:14:28 +08002866 userpage = kmap_atomic(page);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002867 memset(userpage + pg_offset, 0,
2868 PAGE_CACHE_SIZE - pg_offset);
Cong Wang7ac687d2011-11-25 23:14:28 +08002869 kunmap_atomic(userpage);
Chris Mason211c17f2008-05-15 09:13:45 -04002870 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002871 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002872 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002873
2874 set_page_extent_mapped(page);
2875
Josef Bacik9e487102011-08-01 12:08:18 -04002876 if (!tree->ops || !tree->ops->fill_delalloc)
2877 fill_delalloc = false;
2878
Chris Masond1310b22008-01-24 16:13:08 -05002879 delalloc_start = start;
2880 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002881 page_started = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002882 if (!epd->extent_locked && fill_delalloc) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002883 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002884 /*
2885 * make sure the wbc mapping index is at least updated
2886 * to this page.
2887 */
2888 update_nr_written(page, wbc, 0);
2889
Chris Masond3977122009-01-05 21:25:51 -05002890 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002891 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002892 page,
2893 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002894 &delalloc_end,
2895 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002896 if (nr_delalloc == 0) {
2897 delalloc_start = delalloc_end + 1;
2898 continue;
2899 }
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002900 ret = tree->ops->fill_delalloc(inode, page,
2901 delalloc_start,
2902 delalloc_end,
2903 &page_started,
2904 &nr_written);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002905 /* File system has been set read-only */
2906 if (ret) {
2907 SetPageError(page);
2908 goto done;
2909 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002910 /*
2911 * delalloc_end is already one less than the total
2912 * length, so we don't subtract one from
2913 * PAGE_CACHE_SIZE
2914 */
2915 delalloc_to_write += (delalloc_end - delalloc_start +
2916 PAGE_CACHE_SIZE) >>
2917 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002918 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002919 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002920 if (wbc->nr_to_write < delalloc_to_write) {
2921 int thresh = 8192;
2922
2923 if (delalloc_to_write < thresh * 2)
2924 thresh = delalloc_to_write;
2925 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2926 thresh);
2927 }
Chris Masonc8b97812008-10-29 14:49:59 -04002928
Chris Mason771ed682008-11-06 22:02:51 -05002929 /* did the fill delalloc function already unlock and start
2930 * the IO?
2931 */
2932 if (page_started) {
2933 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002934 /*
2935 * we've unlocked the page, so we can't update
2936 * the mapping's writeback index, just update
2937 * nr_to_write.
2938 */
2939 wbc->nr_to_write -= nr_written;
2940 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002941 }
Chris Masonc8b97812008-10-29 14:49:59 -04002942 }
Chris Mason247e7432008-07-17 12:53:51 -04002943 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002944 ret = tree->ops->writepage_start_hook(page, start,
2945 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002946 if (ret) {
2947 /* Fixup worker will requeue */
2948 if (ret == -EBUSY)
2949 wbc->pages_skipped++;
2950 else
2951 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002952 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002953 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002954 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002955 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002956 }
2957 }
2958
Chris Mason11c83492009-04-20 15:50:09 -04002959 /*
2960 * we don't want to touch the inode after unlocking the page,
2961 * so we update the mapping writeback index now
2962 */
2963 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002964
Chris Masond1310b22008-01-24 16:13:08 -05002965 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002966 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002967 if (tree->ops && tree->ops->writepage_end_io_hook)
2968 tree->ops->writepage_end_io_hook(page, start,
2969 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002970 goto done;
2971 }
2972
Chris Masond1310b22008-01-24 16:13:08 -05002973 blocksize = inode->i_sb->s_blocksize;
2974
2975 while (cur <= end) {
2976 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002977 if (tree->ops && tree->ops->writepage_end_io_hook)
2978 tree->ops->writepage_end_io_hook(page, cur,
2979 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002980 break;
2981 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002982 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002983 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02002984 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002985 SetPageError(page);
2986 break;
2987 }
2988
2989 extent_offset = cur - em->start;
2990 BUG_ON(extent_map_end(em) <= cur);
2991 BUG_ON(end < cur);
2992 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2993 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2994 sector = (em->block_start + extent_offset) >> 9;
2995 bdev = em->bdev;
2996 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002997 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002998 free_extent_map(em);
2999 em = NULL;
3000
Chris Masonc8b97812008-10-29 14:49:59 -04003001 /*
3002 * compressed and inline extents are written through other
3003 * paths in the FS
3004 */
3005 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003006 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003007 /*
3008 * end_io notification does not happen here for
3009 * compressed extents
3010 */
3011 if (!compressed && tree->ops &&
3012 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003013 tree->ops->writepage_end_io_hook(page, cur,
3014 cur + iosize - 1,
3015 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003016 else if (compressed) {
3017 /* we don't want to end_page_writeback on
3018 * a compressed extent. this happens
3019 * elsewhere
3020 */
3021 nr++;
3022 }
3023
3024 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003025 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003026 continue;
3027 }
Chris Masond1310b22008-01-24 16:13:08 -05003028 /* leave this out until we have a page_mkwrite call */
3029 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04003030 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05003031 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003032 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003033 continue;
3034 }
Chris Masonc8b97812008-10-29 14:49:59 -04003035
Chris Masond1310b22008-01-24 16:13:08 -05003036 if (tree->ops && tree->ops->writepage_io_hook) {
3037 ret = tree->ops->writepage_io_hook(page, cur,
3038 cur + iosize - 1);
3039 } else {
3040 ret = 0;
3041 }
Chris Mason1259ab72008-05-12 13:39:03 -04003042 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003043 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003044 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003045 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003046
Chris Masond1310b22008-01-24 16:13:08 -05003047 set_range_writeback(tree, cur, cur + iosize - 1);
3048 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05003049 printk(KERN_ERR "btrfs warning page %lu not "
3050 "writeback, cur %llu end %llu\n",
3051 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05003052 (unsigned long long)end);
3053 }
3054
Chris Masonffbd5172009-04-20 15:50:09 -04003055 ret = submit_extent_page(write_flags, tree, page,
3056 sector, iosize, pg_offset,
3057 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04003058 end_bio_extent_writepage,
3059 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003060 if (ret)
3061 SetPageError(page);
3062 }
3063 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003064 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003065 nr++;
3066 }
3067done:
3068 if (nr == 0) {
3069 /* make sure the mapping tag for page dirty gets cleared */
3070 set_page_writeback(page);
3071 end_page_writeback(page);
3072 }
Chris Masond1310b22008-01-24 16:13:08 -05003073 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05003074
Chris Mason11c83492009-04-20 15:50:09 -04003075done_unlocked:
3076
Chris Mason2c64c532009-09-02 15:04:12 -04003077 /* drop our reference on any cached states */
3078 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05003079 return 0;
3080}
3081
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003082static int eb_wait(void *word)
3083{
3084 io_schedule();
3085 return 0;
3086}
3087
3088static void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
3089{
3090 wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
3091 TASK_UNINTERRUPTIBLE);
3092}
3093
3094static int lock_extent_buffer_for_io(struct extent_buffer *eb,
3095 struct btrfs_fs_info *fs_info,
3096 struct extent_page_data *epd)
3097{
3098 unsigned long i, num_pages;
3099 int flush = 0;
3100 int ret = 0;
3101
3102 if (!btrfs_try_tree_write_lock(eb)) {
3103 flush = 1;
3104 flush_write_bio(epd);
3105 btrfs_tree_lock(eb);
3106 }
3107
3108 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3109 btrfs_tree_unlock(eb);
3110 if (!epd->sync_io)
3111 return 0;
3112 if (!flush) {
3113 flush_write_bio(epd);
3114 flush = 1;
3115 }
Chris Masona098d8e2012-03-21 12:09:56 -04003116 while (1) {
3117 wait_on_extent_buffer_writeback(eb);
3118 btrfs_tree_lock(eb);
3119 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3120 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003121 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003122 }
3123 }
3124
Josef Bacik51561ff2012-07-20 16:25:24 -04003125 /*
3126 * We need to do this to prevent races in people who check if the eb is
3127 * under IO since we can end up having no IO bits set for a short period
3128 * of time.
3129 */
3130 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003131 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3132 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003133 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003134 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
3135 spin_lock(&fs_info->delalloc_lock);
3136 if (fs_info->dirty_metadata_bytes >= eb->len)
3137 fs_info->dirty_metadata_bytes -= eb->len;
3138 else
3139 WARN_ON(1);
3140 spin_unlock(&fs_info->delalloc_lock);
3141 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003142 } else {
3143 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003144 }
3145
3146 btrfs_tree_unlock(eb);
3147
3148 if (!ret)
3149 return ret;
3150
3151 num_pages = num_extent_pages(eb->start, eb->len);
3152 for (i = 0; i < num_pages; i++) {
3153 struct page *p = extent_buffer_page(eb, i);
3154
3155 if (!trylock_page(p)) {
3156 if (!flush) {
3157 flush_write_bio(epd);
3158 flush = 1;
3159 }
3160 lock_page(p);
3161 }
3162 }
3163
3164 return ret;
3165}
3166
3167static void end_extent_buffer_writeback(struct extent_buffer *eb)
3168{
3169 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3170 smp_mb__after_clear_bit();
3171 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3172}
3173
3174static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
3175{
3176 int uptodate = err == 0;
3177 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
3178 struct extent_buffer *eb;
3179 int done;
3180
3181 do {
3182 struct page *page = bvec->bv_page;
3183
3184 bvec--;
3185 eb = (struct extent_buffer *)page->private;
3186 BUG_ON(!eb);
3187 done = atomic_dec_and_test(&eb->io_pages);
3188
3189 if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
3190 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3191 ClearPageUptodate(page);
3192 SetPageError(page);
3193 }
3194
3195 end_page_writeback(page);
3196
3197 if (!done)
3198 continue;
3199
3200 end_extent_buffer_writeback(eb);
3201 } while (bvec >= bio->bi_io_vec);
3202
3203 bio_put(bio);
3204
3205}
3206
3207static int write_one_eb(struct extent_buffer *eb,
3208 struct btrfs_fs_info *fs_info,
3209 struct writeback_control *wbc,
3210 struct extent_page_data *epd)
3211{
3212 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
3213 u64 offset = eb->start;
3214 unsigned long i, num_pages;
Josef Bacikde0022b2012-09-25 14:25:58 -04003215 unsigned long bio_flags = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003216 int rw = (epd->sync_io ? WRITE_SYNC : WRITE);
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003217 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003218
3219 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3220 num_pages = num_extent_pages(eb->start, eb->len);
3221 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003222 if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
3223 bio_flags = EXTENT_BIO_TREE_LOG;
3224
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003225 for (i = 0; i < num_pages; i++) {
3226 struct page *p = extent_buffer_page(eb, i);
3227
3228 clear_page_dirty_for_io(p);
3229 set_page_writeback(p);
3230 ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
3231 PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
3232 -1, end_bio_extent_buffer_writepage,
Josef Bacikde0022b2012-09-25 14:25:58 -04003233 0, epd->bio_flags, bio_flags);
3234 epd->bio_flags = bio_flags;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003235 if (ret) {
3236 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3237 SetPageError(p);
3238 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3239 end_extent_buffer_writeback(eb);
3240 ret = -EIO;
3241 break;
3242 }
3243 offset += PAGE_CACHE_SIZE;
3244 update_nr_written(p, wbc, 1);
3245 unlock_page(p);
3246 }
3247
3248 if (unlikely(ret)) {
3249 for (; i < num_pages; i++) {
3250 struct page *p = extent_buffer_page(eb, i);
3251 unlock_page(p);
3252 }
3253 }
3254
3255 return ret;
3256}
3257
3258int btree_write_cache_pages(struct address_space *mapping,
3259 struct writeback_control *wbc)
3260{
3261 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3262 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3263 struct extent_buffer *eb, *prev_eb = NULL;
3264 struct extent_page_data epd = {
3265 .bio = NULL,
3266 .tree = tree,
3267 .extent_locked = 0,
3268 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003269 .bio_flags = 0,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003270 };
3271 int ret = 0;
3272 int done = 0;
3273 int nr_to_write_done = 0;
3274 struct pagevec pvec;
3275 int nr_pages;
3276 pgoff_t index;
3277 pgoff_t end; /* Inclusive */
3278 int scanned = 0;
3279 int tag;
3280
3281 pagevec_init(&pvec, 0);
3282 if (wbc->range_cyclic) {
3283 index = mapping->writeback_index; /* Start from prev offset */
3284 end = -1;
3285 } else {
3286 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3287 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3288 scanned = 1;
3289 }
3290 if (wbc->sync_mode == WB_SYNC_ALL)
3291 tag = PAGECACHE_TAG_TOWRITE;
3292 else
3293 tag = PAGECACHE_TAG_DIRTY;
3294retry:
3295 if (wbc->sync_mode == WB_SYNC_ALL)
3296 tag_pages_for_writeback(mapping, index, end);
3297 while (!done && !nr_to_write_done && (index <= end) &&
3298 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3299 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3300 unsigned i;
3301
3302 scanned = 1;
3303 for (i = 0; i < nr_pages; i++) {
3304 struct page *page = pvec.pages[i];
3305
3306 if (!PagePrivate(page))
3307 continue;
3308
3309 if (!wbc->range_cyclic && page->index > end) {
3310 done = 1;
3311 break;
3312 }
3313
Josef Bacikb5bae262012-09-14 13:43:01 -04003314 spin_lock(&mapping->private_lock);
3315 if (!PagePrivate(page)) {
3316 spin_unlock(&mapping->private_lock);
3317 continue;
3318 }
3319
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003320 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003321
3322 /*
3323 * Shouldn't happen and normally this would be a BUG_ON
3324 * but no sense in crashing the users box for something
3325 * we can survive anyway.
3326 */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003327 if (!eb) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003328 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003329 WARN_ON(1);
3330 continue;
3331 }
3332
Josef Bacikb5bae262012-09-14 13:43:01 -04003333 if (eb == prev_eb) {
3334 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003335 continue;
3336 }
3337
Josef Bacikb5bae262012-09-14 13:43:01 -04003338 ret = atomic_inc_not_zero(&eb->refs);
3339 spin_unlock(&mapping->private_lock);
3340 if (!ret)
3341 continue;
3342
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003343 prev_eb = eb;
3344 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3345 if (!ret) {
3346 free_extent_buffer(eb);
3347 continue;
3348 }
3349
3350 ret = write_one_eb(eb, fs_info, wbc, &epd);
3351 if (ret) {
3352 done = 1;
3353 free_extent_buffer(eb);
3354 break;
3355 }
3356 free_extent_buffer(eb);
3357
3358 /*
3359 * the filesystem may choose to bump up nr_to_write.
3360 * We have to make sure to honor the new nr_to_write
3361 * at any time
3362 */
3363 nr_to_write_done = wbc->nr_to_write <= 0;
3364 }
3365 pagevec_release(&pvec);
3366 cond_resched();
3367 }
3368 if (!scanned && !done) {
3369 /*
3370 * We hit the last page and there is more work to be done: wrap
3371 * back to the start of the file
3372 */
3373 scanned = 1;
3374 index = 0;
3375 goto retry;
3376 }
3377 flush_write_bio(&epd);
3378 return ret;
3379}
3380
Chris Masond1310b22008-01-24 16:13:08 -05003381/**
Chris Mason4bef0842008-09-08 11:18:08 -04003382 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
Chris Masond1310b22008-01-24 16:13:08 -05003383 * @mapping: address space structure to write
3384 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3385 * @writepage: function called for each page
3386 * @data: data passed to writepage function
3387 *
3388 * If a page is already under I/O, write_cache_pages() skips it, even
3389 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3390 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3391 * and msync() need to guarantee that all the data which was dirty at the time
3392 * the call was made get new I/O started against them. If wbc->sync_mode is
3393 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3394 * existing IO to complete.
3395 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003396static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04003397 struct address_space *mapping,
3398 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003399 writepage_t writepage, void *data,
3400 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05003401{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003402 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003403 int ret = 0;
3404 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003405 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003406 struct pagevec pvec;
3407 int nr_pages;
3408 pgoff_t index;
3409 pgoff_t end; /* Inclusive */
3410 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003411 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003412
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003413 /*
3414 * We have to hold onto the inode so that ordered extents can do their
3415 * work when the IO finishes. The alternative to this is failing to add
3416 * an ordered extent if the igrab() fails there and that is a huge pain
3417 * to deal with, so instead just hold onto the inode throughout the
3418 * writepages operation. If it fails here we are freeing up the inode
3419 * anyway and we'd rather not waste our time writing out stuff that is
3420 * going to be truncated anyway.
3421 */
3422 if (!igrab(inode))
3423 return 0;
3424
Chris Masond1310b22008-01-24 16:13:08 -05003425 pagevec_init(&pvec, 0);
3426 if (wbc->range_cyclic) {
3427 index = mapping->writeback_index; /* Start from prev offset */
3428 end = -1;
3429 } else {
3430 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3431 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003432 scanned = 1;
3433 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003434 if (wbc->sync_mode == WB_SYNC_ALL)
3435 tag = PAGECACHE_TAG_TOWRITE;
3436 else
3437 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003438retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003439 if (wbc->sync_mode == WB_SYNC_ALL)
3440 tag_pages_for_writeback(mapping, index, end);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003441 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003442 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3443 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003444 unsigned i;
3445
3446 scanned = 1;
3447 for (i = 0; i < nr_pages; i++) {
3448 struct page *page = pvec.pages[i];
3449
3450 /*
3451 * At this point we hold neither mapping->tree_lock nor
3452 * lock on the page itself: the page may be truncated or
3453 * invalidated (changing page->mapping to NULL), or even
3454 * swizzled back from swapper_space to tmpfs file
3455 * mapping
3456 */
Chris Mason01d658f2011-11-01 10:08:06 -04003457 if (tree->ops &&
3458 tree->ops->write_cache_pages_lock_hook) {
3459 tree->ops->write_cache_pages_lock_hook(page,
3460 data, flush_fn);
3461 } else {
3462 if (!trylock_page(page)) {
3463 flush_fn(data);
3464 lock_page(page);
3465 }
3466 }
Chris Masond1310b22008-01-24 16:13:08 -05003467
3468 if (unlikely(page->mapping != mapping)) {
3469 unlock_page(page);
3470 continue;
3471 }
3472
3473 if (!wbc->range_cyclic && page->index > end) {
3474 done = 1;
3475 unlock_page(page);
3476 continue;
3477 }
3478
Chris Masond2c3f4f2008-11-19 12:44:22 -05003479 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003480 if (PageWriteback(page))
3481 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05003482 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003483 }
Chris Masond1310b22008-01-24 16:13:08 -05003484
3485 if (PageWriteback(page) ||
3486 !clear_page_dirty_for_io(page)) {
3487 unlock_page(page);
3488 continue;
3489 }
3490
3491 ret = (*writepage)(page, wbc, data);
3492
3493 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3494 unlock_page(page);
3495 ret = 0;
3496 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003497 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05003498 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003499
3500 /*
3501 * the filesystem may choose to bump up nr_to_write.
3502 * We have to make sure to honor the new nr_to_write
3503 * at any time
3504 */
3505 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05003506 }
3507 pagevec_release(&pvec);
3508 cond_resched();
3509 }
3510 if (!scanned && !done) {
3511 /*
3512 * We hit the last page and there is more work to be done: wrap
3513 * back to the start of the file
3514 */
3515 scanned = 1;
3516 index = 0;
3517 goto retry;
3518 }
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003519 btrfs_add_delayed_iput(inode);
Chris Masond1310b22008-01-24 16:13:08 -05003520 return ret;
3521}
Chris Masond1310b22008-01-24 16:13:08 -05003522
Chris Masonffbd5172009-04-20 15:50:09 -04003523static void flush_epd_write_bio(struct extent_page_data *epd)
3524{
3525 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04003526 int rw = WRITE;
3527 int ret;
3528
Chris Masonffbd5172009-04-20 15:50:09 -04003529 if (epd->sync_io)
Jeff Mahoney355808c2011-10-03 23:23:14 -04003530 rw = WRITE_SYNC;
3531
Josef Bacikde0022b2012-09-25 14:25:58 -04003532 ret = submit_one_bio(rw, epd->bio, 0, epd->bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003533 BUG_ON(ret < 0); /* -ENOMEM */
Chris Masonffbd5172009-04-20 15:50:09 -04003534 epd->bio = NULL;
3535 }
3536}
3537
Chris Masond2c3f4f2008-11-19 12:44:22 -05003538static noinline void flush_write_bio(void *data)
3539{
3540 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04003541 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003542}
3543
Chris Masond1310b22008-01-24 16:13:08 -05003544int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3545 get_extent_t *get_extent,
3546 struct writeback_control *wbc)
3547{
3548 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003549 struct extent_page_data epd = {
3550 .bio = NULL,
3551 .tree = tree,
3552 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003553 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003554 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003555 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05003556 };
Chris Masond1310b22008-01-24 16:13:08 -05003557
Chris Masond1310b22008-01-24 16:13:08 -05003558 ret = __extent_writepage(page, wbc, &epd);
3559
Chris Masonffbd5172009-04-20 15:50:09 -04003560 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003561 return ret;
3562}
Chris Masond1310b22008-01-24 16:13:08 -05003563
Chris Mason771ed682008-11-06 22:02:51 -05003564int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3565 u64 start, u64 end, get_extent_t *get_extent,
3566 int mode)
3567{
3568 int ret = 0;
3569 struct address_space *mapping = inode->i_mapping;
3570 struct page *page;
3571 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3572 PAGE_CACHE_SHIFT;
3573
3574 struct extent_page_data epd = {
3575 .bio = NULL,
3576 .tree = tree,
3577 .get_extent = get_extent,
3578 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04003579 .sync_io = mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003580 .bio_flags = 0,
Chris Mason771ed682008-11-06 22:02:51 -05003581 };
3582 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05003583 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05003584 .nr_to_write = nr_pages * 2,
3585 .range_start = start,
3586 .range_end = end + 1,
3587 };
3588
Chris Masond3977122009-01-05 21:25:51 -05003589 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05003590 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3591 if (clear_page_dirty_for_io(page))
3592 ret = __extent_writepage(page, &wbc_writepages, &epd);
3593 else {
3594 if (tree->ops && tree->ops->writepage_end_io_hook)
3595 tree->ops->writepage_end_io_hook(page, start,
3596 start + PAGE_CACHE_SIZE - 1,
3597 NULL, 1);
3598 unlock_page(page);
3599 }
3600 page_cache_release(page);
3601 start += PAGE_CACHE_SIZE;
3602 }
3603
Chris Masonffbd5172009-04-20 15:50:09 -04003604 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05003605 return ret;
3606}
Chris Masond1310b22008-01-24 16:13:08 -05003607
3608int extent_writepages(struct extent_io_tree *tree,
3609 struct address_space *mapping,
3610 get_extent_t *get_extent,
3611 struct writeback_control *wbc)
3612{
3613 int ret = 0;
3614 struct extent_page_data epd = {
3615 .bio = NULL,
3616 .tree = tree,
3617 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003618 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003619 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003620 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05003621 };
3622
Chris Mason4bef0842008-09-08 11:18:08 -04003623 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003624 __extent_writepage, &epd,
3625 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04003626 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003627 return ret;
3628}
Chris Masond1310b22008-01-24 16:13:08 -05003629
3630int extent_readpages(struct extent_io_tree *tree,
3631 struct address_space *mapping,
3632 struct list_head *pages, unsigned nr_pages,
3633 get_extent_t get_extent)
3634{
3635 struct bio *bio = NULL;
3636 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04003637 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06003638 struct page *pagepool[16];
3639 struct page *page;
3640 int i = 0;
3641 int nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003642
Chris Masond1310b22008-01-24 16:13:08 -05003643 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
Liu Bo67c96842012-07-20 21:43:09 -06003644 page = list_entry(pages->prev, struct page, lru);
Chris Masond1310b22008-01-24 16:13:08 -05003645
3646 prefetchw(&page->flags);
3647 list_del(&page->lru);
Liu Bo67c96842012-07-20 21:43:09 -06003648 if (add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04003649 page->index, GFP_NOFS)) {
Liu Bo67c96842012-07-20 21:43:09 -06003650 page_cache_release(page);
3651 continue;
Chris Masond1310b22008-01-24 16:13:08 -05003652 }
Liu Bo67c96842012-07-20 21:43:09 -06003653
3654 pagepool[nr++] = page;
3655 if (nr < ARRAY_SIZE(pagepool))
3656 continue;
3657 for (i = 0; i < nr; i++) {
3658 __extent_read_full_page(tree, pagepool[i], get_extent,
3659 &bio, 0, &bio_flags);
3660 page_cache_release(pagepool[i]);
3661 }
3662 nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003663 }
Liu Bo67c96842012-07-20 21:43:09 -06003664 for (i = 0; i < nr; i++) {
3665 __extent_read_full_page(tree, pagepool[i], get_extent,
3666 &bio, 0, &bio_flags);
3667 page_cache_release(pagepool[i]);
3668 }
3669
Chris Masond1310b22008-01-24 16:13:08 -05003670 BUG_ON(!list_empty(pages));
3671 if (bio)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003672 return submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003673 return 0;
3674}
Chris Masond1310b22008-01-24 16:13:08 -05003675
3676/*
3677 * basic invalidatepage code, this waits on any locked or writeback
3678 * ranges corresponding to the page, and then deletes any extent state
3679 * records from the tree
3680 */
3681int extent_invalidatepage(struct extent_io_tree *tree,
3682 struct page *page, unsigned long offset)
3683{
Josef Bacik2ac55d42010-02-03 19:33:23 +00003684 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003685 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
3686 u64 end = start + PAGE_CACHE_SIZE - 1;
3687 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3688
Chris Masond3977122009-01-05 21:25:51 -05003689 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003690 if (start > end)
3691 return 0;
3692
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003693 lock_extent_bits(tree, start, end, 0, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04003694 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003695 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04003696 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3697 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003698 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003699 return 0;
3700}
Chris Masond1310b22008-01-24 16:13:08 -05003701
3702/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04003703 * a helper for releasepage, this tests for areas of the page that
3704 * are locked or under IO and drops the related state bits if it is safe
3705 * to drop the page.
3706 */
3707int try_release_extent_state(struct extent_map_tree *map,
3708 struct extent_io_tree *tree, struct page *page,
3709 gfp_t mask)
3710{
3711 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3712 u64 end = start + PAGE_CACHE_SIZE - 1;
3713 int ret = 1;
3714
Chris Mason211f90e2008-07-18 11:56:15 -04003715 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04003716 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04003717 ret = 0;
3718 else {
3719 if ((mask & GFP_NOFS) == GFP_NOFS)
3720 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04003721 /*
3722 * at this point we can safely clear everything except the
3723 * locked bit and the nodatasum bit
3724 */
Chris Masone3f24cc2011-02-14 12:52:08 -05003725 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04003726 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3727 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05003728
3729 /* if clear_extent_bit failed for enomem reasons,
3730 * we can't allow the release to continue.
3731 */
3732 if (ret < 0)
3733 ret = 0;
3734 else
3735 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003736 }
3737 return ret;
3738}
Chris Mason7b13b7b2008-04-18 10:29:50 -04003739
3740/*
Chris Masond1310b22008-01-24 16:13:08 -05003741 * a helper for releasepage. As long as there are no locked extents
3742 * in the range corresponding to the page, both state records and extent
3743 * map records are removed
3744 */
3745int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05003746 struct extent_io_tree *tree, struct page *page,
3747 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05003748{
3749 struct extent_map *em;
3750 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3751 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003752
Chris Mason70dec802008-01-29 09:59:12 -05003753 if ((mask & __GFP_WAIT) &&
3754 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05003755 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05003756 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05003757 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04003758 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05003759 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09003760 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04003761 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003762 break;
3763 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003764 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3765 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04003766 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003767 free_extent_map(em);
3768 break;
3769 }
3770 if (!test_range_bit(tree, em->start,
3771 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04003772 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04003773 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05003774 remove_extent_mapping(map, em);
3775 /* once for the rb tree */
3776 free_extent_map(em);
3777 }
3778 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04003779 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003780
3781 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05003782 free_extent_map(em);
3783 }
Chris Masond1310b22008-01-24 16:13:08 -05003784 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04003785 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003786}
Chris Masond1310b22008-01-24 16:13:08 -05003787
Chris Masonec29ed52011-02-23 16:23:20 -05003788/*
3789 * helper function for fiemap, which doesn't want to see any holes.
3790 * This maps until we find something past 'last'
3791 */
3792static struct extent_map *get_extent_skip_holes(struct inode *inode,
3793 u64 offset,
3794 u64 last,
3795 get_extent_t *get_extent)
3796{
3797 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3798 struct extent_map *em;
3799 u64 len;
3800
3801 if (offset >= last)
3802 return NULL;
3803
3804 while(1) {
3805 len = last - offset;
3806 if (len == 0)
3807 break;
3808 len = (len + sectorsize - 1) & ~(sectorsize - 1);
3809 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02003810 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05003811 return em;
3812
3813 /* if this isn't a hole return it */
3814 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3815 em->block_start != EXTENT_MAP_HOLE) {
3816 return em;
3817 }
3818
3819 /* this is a hole, advance to the next extent */
3820 offset = extent_map_end(em);
3821 free_extent_map(em);
3822 if (offset >= last)
3823 break;
3824 }
3825 return NULL;
3826}
3827
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003828int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3829 __u64 start, __u64 len, get_extent_t *get_extent)
3830{
Josef Bacik975f84f2010-11-23 19:36:57 +00003831 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003832 u64 off = start;
3833 u64 max = start + len;
3834 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00003835 u32 found_type;
3836 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05003837 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003838 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003839 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00003840 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003841 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003842 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00003843 struct btrfs_path *path;
3844 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003845 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003846 u64 em_start = 0;
3847 u64 em_len = 0;
3848 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003849 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003850
3851 if (len == 0)
3852 return -EINVAL;
3853
Josef Bacik975f84f2010-11-23 19:36:57 +00003854 path = btrfs_alloc_path();
3855 if (!path)
3856 return -ENOMEM;
3857 path->leave_spinning = 1;
3858
Josef Bacik4d479cf2011-11-17 11:34:31 -05003859 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3860 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3861
Chris Masonec29ed52011-02-23 16:23:20 -05003862 /*
3863 * lookup the last file extent. We're not using i_size here
3864 * because there might be preallocation past i_size
3865 */
Josef Bacik975f84f2010-11-23 19:36:57 +00003866 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
Li Zefan33345d012011-04-20 10:31:50 +08003867 path, btrfs_ino(inode), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00003868 if (ret < 0) {
3869 btrfs_free_path(path);
3870 return ret;
3871 }
3872 WARN_ON(!ret);
3873 path->slots[0]--;
3874 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3875 struct btrfs_file_extent_item);
3876 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3877 found_type = btrfs_key_type(&found_key);
3878
Chris Masonec29ed52011-02-23 16:23:20 -05003879 /* No extents, but there might be delalloc bits */
Li Zefan33345d012011-04-20 10:31:50 +08003880 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00003881 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05003882 /* have to trust i_size as the end */
3883 last = (u64)-1;
3884 last_for_get_extent = isize;
3885 } else {
3886 /*
3887 * remember the start of the last extent. There are a
3888 * bunch of different factors that go into the length of the
3889 * extent, so its much less complex to remember where it started
3890 */
3891 last = found_key.offset;
3892 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00003893 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003894 btrfs_free_path(path);
3895
Chris Masonec29ed52011-02-23 16:23:20 -05003896 /*
3897 * we might have some extents allocated but more delalloc past those
3898 * extents. so, we trust isize unless the start of the last extent is
3899 * beyond isize
3900 */
3901 if (last < isize) {
3902 last = (u64)-1;
3903 last_for_get_extent = isize;
3904 }
3905
Josef Bacik2ac55d42010-02-03 19:33:23 +00003906 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003907 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05003908
Josef Bacik4d479cf2011-11-17 11:34:31 -05003909 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05003910 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003911 if (!em)
3912 goto out;
3913 if (IS_ERR(em)) {
3914 ret = PTR_ERR(em);
3915 goto out;
3916 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003917
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003918 while (!end) {
Chris Masonea8efc72011-03-08 11:54:40 -05003919 u64 offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003920
Chris Masonea8efc72011-03-08 11:54:40 -05003921 /* break if the extent we found is outside the range */
3922 if (em->start >= max || extent_map_end(em) < off)
3923 break;
3924
3925 /*
3926 * get_extent may return an extent that starts before our
3927 * requested range. We have to make sure the ranges
3928 * we return to fiemap always move forward and don't
3929 * overlap, so adjust the offsets here
3930 */
3931 em_start = max(em->start, off);
3932
3933 /*
3934 * record the offset from the start of the extent
3935 * for adjusting the disk offset below
3936 */
3937 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05003938 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05003939 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05003940 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003941 disko = 0;
3942 flags = 0;
3943
Chris Masonea8efc72011-03-08 11:54:40 -05003944 /*
3945 * bump off for our next call to get_extent
3946 */
3947 off = extent_map_end(em);
3948 if (off >= max)
3949 end = 1;
3950
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003951 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003952 end = 1;
3953 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003954 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003955 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3956 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003957 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003958 flags |= (FIEMAP_EXTENT_DELALLOC |
3959 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003960 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05003961 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003962 }
3963 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3964 flags |= FIEMAP_EXTENT_ENCODED;
3965
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003966 free_extent_map(em);
3967 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05003968 if ((em_start >= last) || em_len == (u64)-1 ||
3969 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003970 flags |= FIEMAP_EXTENT_LAST;
3971 end = 1;
3972 }
3973
Chris Masonec29ed52011-02-23 16:23:20 -05003974 /* now scan forward to see if this is really the last extent. */
3975 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3976 get_extent);
3977 if (IS_ERR(em)) {
3978 ret = PTR_ERR(em);
3979 goto out;
3980 }
3981 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00003982 flags |= FIEMAP_EXTENT_LAST;
3983 end = 1;
3984 }
Chris Masonec29ed52011-02-23 16:23:20 -05003985 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3986 em_len, flags);
3987 if (ret)
3988 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003989 }
3990out_free:
3991 free_extent_map(em);
3992out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00003993 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3994 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003995 return ret;
3996}
3997
Chris Mason727011e2010-08-06 13:21:20 -04003998static void __free_extent_buffer(struct extent_buffer *eb)
3999{
4000#if LEAK_DEBUG
4001 unsigned long flags;
4002 spin_lock_irqsave(&leak_lock, flags);
4003 list_del(&eb->leak_list);
4004 spin_unlock_irqrestore(&leak_lock, flags);
4005#endif
4006 if (eb->pages && eb->pages != eb->inline_pages)
4007 kfree(eb->pages);
4008 kmem_cache_free(extent_buffer_cache, eb);
4009}
4010
Chris Masond1310b22008-01-24 16:13:08 -05004011static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
4012 u64 start,
4013 unsigned long len,
4014 gfp_t mask)
4015{
4016 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05004017#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04004018 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04004019#endif
Chris Masond1310b22008-01-24 16:13:08 -05004020
Chris Masond1310b22008-01-24 16:13:08 -05004021 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00004022 if (eb == NULL)
4023 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004024 eb->start = start;
4025 eb->len = len;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004026 eb->tree = tree;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004027 eb->bflags = 0;
Chris Masonbd681512011-07-16 15:23:14 -04004028 rwlock_init(&eb->lock);
4029 atomic_set(&eb->write_locks, 0);
4030 atomic_set(&eb->read_locks, 0);
4031 atomic_set(&eb->blocking_readers, 0);
4032 atomic_set(&eb->blocking_writers, 0);
4033 atomic_set(&eb->spinning_readers, 0);
4034 atomic_set(&eb->spinning_writers, 0);
Arne Jansen5b25f702011-09-13 10:55:48 +02004035 eb->lock_nested = 0;
Chris Masonbd681512011-07-16 15:23:14 -04004036 init_waitqueue_head(&eb->write_lock_wq);
4037 init_waitqueue_head(&eb->read_lock_wq);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004038
Chris Mason39351272009-02-04 09:24:05 -05004039#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04004040 spin_lock_irqsave(&leak_lock, flags);
4041 list_add(&eb->leak_list, &buffers);
4042 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04004043#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05004044 spin_lock_init(&eb->refs_lock);
Chris Masond1310b22008-01-24 16:13:08 -05004045 atomic_set(&eb->refs, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004046 atomic_set(&eb->io_pages, 0);
Chris Mason727011e2010-08-06 13:21:20 -04004047
4048 if (len > MAX_INLINE_EXTENT_BUFFER_SIZE) {
4049 struct page **pages;
4050 int num_pages = (len + PAGE_CACHE_SIZE - 1) >>
4051 PAGE_CACHE_SHIFT;
4052 pages = kzalloc(num_pages, mask);
4053 if (!pages) {
4054 __free_extent_buffer(eb);
4055 return NULL;
4056 }
4057 eb->pages = pages;
4058 } else {
4059 eb->pages = eb->inline_pages;
4060 }
Chris Masond1310b22008-01-24 16:13:08 -05004061
4062 return eb;
4063}
4064
Jan Schmidt815a51c2012-05-16 17:00:02 +02004065struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4066{
4067 unsigned long i;
4068 struct page *p;
4069 struct extent_buffer *new;
4070 unsigned long num_pages = num_extent_pages(src->start, src->len);
4071
4072 new = __alloc_extent_buffer(NULL, src->start, src->len, GFP_ATOMIC);
4073 if (new == NULL)
4074 return NULL;
4075
4076 for (i = 0; i < num_pages; i++) {
4077 p = alloc_page(GFP_ATOMIC);
4078 BUG_ON(!p);
4079 attach_extent_buffer_page(new, p);
4080 WARN_ON(PageDirty(p));
4081 SetPageUptodate(p);
4082 new->pages[i] = p;
4083 }
4084
4085 copy_extent_buffer(new, src, 0, 0, src->len);
4086 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
4087 set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
4088
4089 return new;
4090}
4091
4092struct extent_buffer *alloc_dummy_extent_buffer(u64 start, unsigned long len)
4093{
4094 struct extent_buffer *eb;
4095 unsigned long num_pages = num_extent_pages(0, len);
4096 unsigned long i;
4097
4098 eb = __alloc_extent_buffer(NULL, start, len, GFP_ATOMIC);
4099 if (!eb)
4100 return NULL;
4101
4102 for (i = 0; i < num_pages; i++) {
4103 eb->pages[i] = alloc_page(GFP_ATOMIC);
4104 if (!eb->pages[i])
4105 goto err;
4106 }
4107 set_extent_buffer_uptodate(eb);
4108 btrfs_set_header_nritems(eb, 0);
4109 set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4110
4111 return eb;
4112err:
Josef Bacikf60b1b42012-10-05 16:53:34 -04004113 for (i--; i >= 0; i--)
Jan Schmidt815a51c2012-05-16 17:00:02 +02004114 __free_page(eb->pages[i]);
4115 __free_extent_buffer(eb);
4116 return NULL;
4117}
4118
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004119static int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004120{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004121 return (atomic_read(&eb->io_pages) ||
4122 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4123 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004124}
4125
Miao Xie897ca6e2010-10-26 20:57:29 -04004126/*
4127 * Helper for releasing extent buffer page.
4128 */
4129static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
4130 unsigned long start_idx)
4131{
4132 unsigned long index;
Wang Sheng-Hui39bab872012-04-06 14:35:31 +08004133 unsigned long num_pages;
Miao Xie897ca6e2010-10-26 20:57:29 -04004134 struct page *page;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004135 int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
Miao Xie897ca6e2010-10-26 20:57:29 -04004136
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004137 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e2010-10-26 20:57:29 -04004138
Wang Sheng-Hui39bab872012-04-06 14:35:31 +08004139 num_pages = num_extent_pages(eb->start, eb->len);
4140 index = start_idx + num_pages;
Miao Xie897ca6e2010-10-26 20:57:29 -04004141 if (start_idx >= index)
4142 return;
4143
4144 do {
4145 index--;
4146 page = extent_buffer_page(eb, index);
Jan Schmidt815a51c2012-05-16 17:00:02 +02004147 if (page && mapped) {
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004148 spin_lock(&page->mapping->private_lock);
4149 /*
4150 * We do this since we'll remove the pages after we've
4151 * removed the eb from the radix tree, so we could race
4152 * and have this page now attached to the new eb. So
4153 * only clear page_private if it's still connected to
4154 * this eb.
4155 */
4156 if (PagePrivate(page) &&
4157 page->private == (unsigned long)eb) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004158 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Josef Bacik3083ee22012-03-09 16:01:49 -05004159 BUG_ON(PageDirty(page));
4160 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004161 /*
4162 * We need to make sure we haven't be attached
4163 * to a new eb.
4164 */
4165 ClearPagePrivate(page);
4166 set_page_private(page, 0);
4167 /* One for the page private */
4168 page_cache_release(page);
4169 }
4170 spin_unlock(&page->mapping->private_lock);
4171
Jan Schmidt815a51c2012-05-16 17:00:02 +02004172 }
4173 if (page) {
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004174 /* One for when we alloced the page */
Miao Xie897ca6e2010-10-26 20:57:29 -04004175 page_cache_release(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004176 }
Miao Xie897ca6e2010-10-26 20:57:29 -04004177 } while (index != start_idx);
4178}
4179
4180/*
4181 * Helper for releasing the extent buffer.
4182 */
4183static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4184{
4185 btrfs_release_extent_buffer_page(eb, 0);
4186 __free_extent_buffer(eb);
4187}
4188
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004189static void check_buffer_tree_ref(struct extent_buffer *eb)
4190{
4191 /* the ref bit is tricky. We have to make sure it is set
4192 * if we have the buffer dirty. Otherwise the
4193 * code to free a buffer can end up dropping a dirty
4194 * page
4195 *
4196 * Once the ref bit is set, it won't go away while the
4197 * buffer is dirty or in writeback, and it also won't
4198 * go away while we have the reference count on the
4199 * eb bumped.
4200 *
4201 * We can't just set the ref bit without bumping the
4202 * ref on the eb because free_extent_buffer might
4203 * see the ref bit and try to clear it. If this happens
4204 * free_extent_buffer might end up dropping our original
4205 * ref by mistake and freeing the page before we are able
4206 * to add one more ref.
4207 *
4208 * So bump the ref count first, then set the bit. If someone
4209 * beat us to it, drop the ref we added.
4210 */
Josef Bacik594831c2012-07-20 16:11:08 -04004211 spin_lock(&eb->refs_lock);
4212 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004213 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004214 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004215}
4216
Josef Bacik5df42352012-03-15 18:24:42 -04004217static void mark_extent_buffer_accessed(struct extent_buffer *eb)
4218{
4219 unsigned long num_pages, i;
4220
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004221 check_buffer_tree_ref(eb);
4222
Josef Bacik5df42352012-03-15 18:24:42 -04004223 num_pages = num_extent_pages(eb->start, eb->len);
4224 for (i = 0; i < num_pages; i++) {
4225 struct page *p = extent_buffer_page(eb, i);
4226 mark_page_accessed(p);
4227 }
4228}
4229
Chris Masond1310b22008-01-24 16:13:08 -05004230struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
Chris Mason727011e2010-08-06 13:21:20 -04004231 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004232{
4233 unsigned long num_pages = num_extent_pages(start, len);
4234 unsigned long i;
4235 unsigned long index = start >> PAGE_CACHE_SHIFT;
4236 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004237 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004238 struct page *p;
4239 struct address_space *mapping = tree->mapping;
4240 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004241 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004242
Miao Xie19fe0a82010-10-26 20:57:29 -04004243 rcu_read_lock();
4244 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4245 if (eb && atomic_inc_not_zero(&eb->refs)) {
4246 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004247 mark_extent_buffer_accessed(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04004248 return eb;
4249 }
Miao Xie19fe0a82010-10-26 20:57:29 -04004250 rcu_read_unlock();
Chris Mason6af118ce2008-07-22 11:18:07 -04004251
David Sterbaba144192011-04-21 01:12:06 +02004252 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
Peter2b114d12008-04-01 11:21:40 -04004253 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004254 return NULL;
4255
Chris Mason727011e2010-08-06 13:21:20 -04004256 for (i = 0; i < num_pages; i++, index++) {
Chris Masona6591712011-07-19 12:04:14 -04004257 p = find_or_create_page(mapping, index, GFP_NOFS);
Josef Bacik4804b382012-10-05 16:43:45 -04004258 if (!p)
Chris Mason6af118ce2008-07-22 11:18:07 -04004259 goto free_eb;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004260
4261 spin_lock(&mapping->private_lock);
4262 if (PagePrivate(p)) {
4263 /*
4264 * We could have already allocated an eb for this page
4265 * and attached one so lets see if we can get a ref on
4266 * the existing eb, and if we can we know it's good and
4267 * we can just return that one, else we know we can just
4268 * overwrite page->private.
4269 */
4270 exists = (struct extent_buffer *)p->private;
4271 if (atomic_inc_not_zero(&exists->refs)) {
4272 spin_unlock(&mapping->private_lock);
4273 unlock_page(p);
Josef Bacik17de39a2012-05-04 15:16:06 -04004274 page_cache_release(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004275 mark_extent_buffer_accessed(exists);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004276 goto free_eb;
4277 }
4278
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004279 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004280 * Do this so attach doesn't complain and we need to
4281 * drop the ref the old guy had.
4282 */
4283 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004284 WARN_ON(PageDirty(p));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004285 page_cache_release(p);
Chris Masond1310b22008-01-24 16:13:08 -05004286 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004287 attach_extent_buffer_page(eb, p);
4288 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004289 WARN_ON(PageDirty(p));
Chris Masond1310b22008-01-24 16:13:08 -05004290 mark_page_accessed(p);
Chris Mason727011e2010-08-06 13:21:20 -04004291 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05004292 if (!PageUptodate(p))
4293 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05004294
4295 /*
4296 * see below about how we avoid a nasty race with release page
4297 * and why we unlock later
4298 */
Chris Masond1310b22008-01-24 16:13:08 -05004299 }
4300 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004301 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05004302again:
Miao Xie19fe0a82010-10-26 20:57:29 -04004303 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4304 if (ret)
4305 goto free_eb;
4306
Chris Mason6af118ce2008-07-22 11:18:07 -04004307 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004308 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
4309 if (ret == -EEXIST) {
4310 exists = radix_tree_lookup(&tree->buffer,
4311 start >> PAGE_CACHE_SHIFT);
Josef Bacik115391d2012-03-09 09:51:43 -05004312 if (!atomic_inc_not_zero(&exists->refs)) {
4313 spin_unlock(&tree->buffer_lock);
4314 radix_tree_preload_end();
Josef Bacik115391d2012-03-09 09:51:43 -05004315 exists = NULL;
4316 goto again;
4317 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004318 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004319 radix_tree_preload_end();
Josef Bacik5df42352012-03-15 18:24:42 -04004320 mark_extent_buffer_accessed(exists);
Chris Mason6af118ce2008-07-22 11:18:07 -04004321 goto free_eb;
4322 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004323 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004324 check_buffer_tree_ref(eb);
Yan, Zhengf044ba72010-02-04 08:46:56 +00004325 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004326 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05004327
4328 /*
4329 * there is a race where release page may have
4330 * tried to find this extent buffer in the radix
4331 * but failed. It will tell the VM it is safe to
4332 * reclaim the, and it will clear the page private bit.
4333 * We must make sure to set the page private bit properly
4334 * after the extent buffer is in the radix tree so
4335 * it doesn't get lost
4336 */
Chris Mason727011e2010-08-06 13:21:20 -04004337 SetPageChecked(eb->pages[0]);
4338 for (i = 1; i < num_pages; i++) {
4339 p = extent_buffer_page(eb, i);
Chris Mason727011e2010-08-06 13:21:20 -04004340 ClearPageChecked(p);
4341 unlock_page(p);
4342 }
4343 unlock_page(eb->pages[0]);
Chris Masond1310b22008-01-24 16:13:08 -05004344 return eb;
4345
Chris Mason6af118ce2008-07-22 11:18:07 -04004346free_eb:
Chris Mason727011e2010-08-06 13:21:20 -04004347 for (i = 0; i < num_pages; i++) {
4348 if (eb->pages[i])
4349 unlock_page(eb->pages[i]);
4350 }
Chris Masoneb14ab82011-02-10 12:35:00 -05004351
Josef Bacik17de39a2012-05-04 15:16:06 -04004352 WARN_ON(!atomic_dec_and_test(&eb->refs));
Miao Xie897ca6e2010-10-26 20:57:29 -04004353 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04004354 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05004355}
Chris Masond1310b22008-01-24 16:13:08 -05004356
4357struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
David Sterbaf09d1f62011-04-21 01:08:01 +02004358 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004359{
Chris Masond1310b22008-01-24 16:13:08 -05004360 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05004361
Miao Xie19fe0a82010-10-26 20:57:29 -04004362 rcu_read_lock();
4363 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4364 if (eb && atomic_inc_not_zero(&eb->refs)) {
4365 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004366 mark_extent_buffer_accessed(eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04004367 return eb;
4368 }
4369 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04004370
Miao Xie19fe0a82010-10-26 20:57:29 -04004371 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004372}
Chris Masond1310b22008-01-24 16:13:08 -05004373
Josef Bacik3083ee22012-03-09 16:01:49 -05004374static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4375{
4376 struct extent_buffer *eb =
4377 container_of(head, struct extent_buffer, rcu_head);
4378
4379 __free_extent_buffer(eb);
4380}
4381
Josef Bacik3083ee22012-03-09 16:01:49 -05004382/* Expects to have eb->eb_lock already held */
Josef Bacike64860a2012-07-20 16:05:36 -04004383static int release_extent_buffer(struct extent_buffer *eb, gfp_t mask)
Josef Bacik3083ee22012-03-09 16:01:49 -05004384{
4385 WARN_ON(atomic_read(&eb->refs) == 0);
4386 if (atomic_dec_and_test(&eb->refs)) {
Jan Schmidt815a51c2012-05-16 17:00:02 +02004387 if (test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) {
4388 spin_unlock(&eb->refs_lock);
4389 } else {
4390 struct extent_io_tree *tree = eb->tree;
Josef Bacik3083ee22012-03-09 16:01:49 -05004391
Jan Schmidt815a51c2012-05-16 17:00:02 +02004392 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05004393
Jan Schmidt815a51c2012-05-16 17:00:02 +02004394 spin_lock(&tree->buffer_lock);
4395 radix_tree_delete(&tree->buffer,
4396 eb->start >> PAGE_CACHE_SHIFT);
4397 spin_unlock(&tree->buffer_lock);
4398 }
Josef Bacik3083ee22012-03-09 16:01:49 -05004399
4400 /* Should be safe to release our pages at this point */
4401 btrfs_release_extent_buffer_page(eb, 0);
Josef Bacik3083ee22012-03-09 16:01:49 -05004402 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04004403 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05004404 }
4405 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04004406
4407 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05004408}
4409
Chris Masond1310b22008-01-24 16:13:08 -05004410void free_extent_buffer(struct extent_buffer *eb)
4411{
Chris Masond1310b22008-01-24 16:13:08 -05004412 if (!eb)
4413 return;
4414
Josef Bacik3083ee22012-03-09 16:01:49 -05004415 spin_lock(&eb->refs_lock);
4416 if (atomic_read(&eb->refs) == 2 &&
Jan Schmidt815a51c2012-05-16 17:00:02 +02004417 test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
4418 atomic_dec(&eb->refs);
4419
4420 if (atomic_read(&eb->refs) == 2 &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004421 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004422 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004423 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4424 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05004425
Josef Bacik3083ee22012-03-09 16:01:49 -05004426 /*
4427 * I know this is terrible, but it's temporary until we stop tracking
4428 * the uptodate bits and such for the extent buffers.
4429 */
4430 release_extent_buffer(eb, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05004431}
Chris Masond1310b22008-01-24 16:13:08 -05004432
Josef Bacik3083ee22012-03-09 16:01:49 -05004433void free_extent_buffer_stale(struct extent_buffer *eb)
4434{
4435 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004436 return;
4437
Josef Bacik3083ee22012-03-09 16:01:49 -05004438 spin_lock(&eb->refs_lock);
4439 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
4440
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004441 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004442 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4443 atomic_dec(&eb->refs);
4444 release_extent_buffer(eb, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05004445}
4446
Chris Mason1d4284b2012-03-28 20:31:37 -04004447void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004448{
Chris Masond1310b22008-01-24 16:13:08 -05004449 unsigned long i;
4450 unsigned long num_pages;
4451 struct page *page;
4452
Chris Masond1310b22008-01-24 16:13:08 -05004453 num_pages = num_extent_pages(eb->start, eb->len);
4454
4455 for (i = 0; i < num_pages; i++) {
4456 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04004457 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05004458 continue;
4459
Chris Masona61e6f22008-07-22 11:18:08 -04004460 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05004461 WARN_ON(!PagePrivate(page));
4462
Chris Masond1310b22008-01-24 16:13:08 -05004463 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004464 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05004465 if (!PageDirty(page)) {
4466 radix_tree_tag_clear(&page->mapping->page_tree,
4467 page_index(page),
4468 PAGECACHE_TAG_DIRTY);
4469 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004470 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04004471 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04004472 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05004473 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004474 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05004475}
Chris Masond1310b22008-01-24 16:13:08 -05004476
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004477int set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004478{
4479 unsigned long i;
4480 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04004481 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004482
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004483 check_buffer_tree_ref(eb);
4484
Chris Masonb9473432009-03-13 11:00:37 -04004485 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004486
Chris Masond1310b22008-01-24 16:13:08 -05004487 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik3083ee22012-03-09 16:01:49 -05004488 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004489 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
4490
Chris Masonb9473432009-03-13 11:00:37 -04004491 for (i = 0; i < num_pages; i++)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004492 set_page_dirty(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04004493 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05004494}
Chris Masond1310b22008-01-24 16:13:08 -05004495
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004496static int range_straddles_pages(u64 start, u64 len)
Chris Mason19b6caf2011-07-25 06:50:50 -04004497{
4498 if (len < PAGE_CACHE_SIZE)
4499 return 1;
4500 if (start & (PAGE_CACHE_SIZE - 1))
4501 return 1;
4502 if ((start + len) & (PAGE_CACHE_SIZE - 1))
4503 return 1;
4504 return 0;
4505}
4506
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004507int clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04004508{
4509 unsigned long i;
4510 struct page *page;
4511 unsigned long num_pages;
4512
Chris Masonb4ce94d2009-02-04 09:25:08 -05004513 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004514 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason1259ab72008-05-12 13:39:03 -04004515 for (i = 0; i < num_pages; i++) {
4516 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04004517 if (page)
4518 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04004519 }
4520 return 0;
4521}
4522
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004523int set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004524{
4525 unsigned long i;
4526 struct page *page;
4527 unsigned long num_pages;
4528
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004529 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004530 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05004531 for (i = 0; i < num_pages; i++) {
4532 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004533 SetPageUptodate(page);
4534 }
4535 return 0;
4536}
Chris Masond1310b22008-01-24 16:13:08 -05004537
Chris Masonce9adaa2008-04-09 16:28:12 -04004538int extent_range_uptodate(struct extent_io_tree *tree,
4539 u64 start, u64 end)
4540{
4541 struct page *page;
4542 int ret;
4543 int pg_uptodate = 1;
4544 int uptodate;
4545 unsigned long index;
4546
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004547 if (range_straddles_pages(start, end - start + 1)) {
Chris Mason19b6caf2011-07-25 06:50:50 -04004548 ret = test_range_bit(tree, start, end,
4549 EXTENT_UPTODATE, 1, NULL);
4550 if (ret)
4551 return 1;
4552 }
Chris Masond3977122009-01-05 21:25:51 -05004553 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004554 index = start >> PAGE_CACHE_SHIFT;
4555 page = find_get_page(tree->mapping, index);
Mitch Harder8bedd512012-01-26 15:01:11 -05004556 if (!page)
4557 return 1;
Chris Masonce9adaa2008-04-09 16:28:12 -04004558 uptodate = PageUptodate(page);
4559 page_cache_release(page);
4560 if (!uptodate) {
4561 pg_uptodate = 0;
4562 break;
4563 }
4564 start += PAGE_CACHE_SIZE;
4565 }
4566 return pg_uptodate;
4567}
4568
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004569int extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004570{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004571 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004572}
Chris Masond1310b22008-01-24 16:13:08 -05004573
4574int read_extent_buffer_pages(struct extent_io_tree *tree,
Arne Jansenbb82ab82011-06-10 14:06:53 +02004575 struct extent_buffer *eb, u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04004576 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05004577{
4578 unsigned long i;
4579 unsigned long start_i;
4580 struct page *page;
4581 int err;
4582 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04004583 int locked_pages = 0;
4584 int all_uptodate = 1;
Chris Masond1310b22008-01-24 16:13:08 -05004585 unsigned long num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04004586 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004587 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004588 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004589
Chris Masonb4ce94d2009-02-04 09:25:08 -05004590 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05004591 return 0;
4592
Chris Masond1310b22008-01-24 16:13:08 -05004593 if (start) {
4594 WARN_ON(start < eb->start);
4595 start_i = (start >> PAGE_CACHE_SHIFT) -
4596 (eb->start >> PAGE_CACHE_SHIFT);
4597 } else {
4598 start_i = 0;
4599 }
4600
4601 num_pages = num_extent_pages(eb->start, eb->len);
4602 for (i = start_i; i < num_pages; i++) {
4603 page = extent_buffer_page(eb, i);
Arne Jansenbb82ab82011-06-10 14:06:53 +02004604 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04004605 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04004606 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05004607 } else {
4608 lock_page(page);
4609 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004610 locked_pages++;
Chris Mason727011e2010-08-06 13:21:20 -04004611 if (!PageUptodate(page)) {
4612 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04004613 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04004614 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004615 }
4616 if (all_uptodate) {
4617 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004618 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04004619 goto unlock_exit;
4620 }
4621
Josef Bacikea466792012-03-26 21:57:36 -04004622 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04004623 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004624 atomic_set(&eb->io_pages, num_reads);
Chris Masonce9adaa2008-04-09 16:28:12 -04004625 for (i = start_i; i < num_pages; i++) {
4626 page = extent_buffer_page(eb, i);
Chris Masonce9adaa2008-04-09 16:28:12 -04004627 if (!PageUptodate(page)) {
Chris Masonf1885912008-04-09 16:28:12 -04004628 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05004629 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04004630 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04004631 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05004632 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05004633 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05004634 } else {
4635 unlock_page(page);
4636 }
4637 }
4638
Jeff Mahoney355808c2011-10-03 23:23:14 -04004639 if (bio) {
4640 err = submit_one_bio(READ, bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004641 if (err)
4642 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04004643 }
Chris Masona86c12c2008-02-07 10:50:54 -05004644
Arne Jansenbb82ab82011-06-10 14:06:53 +02004645 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05004646 return ret;
Chris Masond3977122009-01-05 21:25:51 -05004647
Chris Masond1310b22008-01-24 16:13:08 -05004648 for (i = start_i; i < num_pages; i++) {
4649 page = extent_buffer_page(eb, i);
4650 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05004651 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05004652 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05004653 }
Chris Masond3977122009-01-05 21:25:51 -05004654
Chris Masond1310b22008-01-24 16:13:08 -05004655 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04004656
4657unlock_exit:
4658 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05004659 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004660 page = extent_buffer_page(eb, i);
4661 i++;
4662 unlock_page(page);
4663 locked_pages--;
4664 }
4665 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004666}
Chris Masond1310b22008-01-24 16:13:08 -05004667
4668void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4669 unsigned long start,
4670 unsigned long len)
4671{
4672 size_t cur;
4673 size_t offset;
4674 struct page *page;
4675 char *kaddr;
4676 char *dst = (char *)dstv;
4677 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4678 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004679
4680 WARN_ON(start > eb->len);
4681 WARN_ON(start + len > eb->start + eb->len);
4682
4683 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4684
Chris Masond3977122009-01-05 21:25:51 -05004685 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004686 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004687
4688 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04004689 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004690 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004691
4692 dst += cur;
4693 len -= cur;
4694 offset = 0;
4695 i++;
4696 }
4697}
Chris Masond1310b22008-01-24 16:13:08 -05004698
4699int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04004700 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05004701 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04004702 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05004703{
4704 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4705 char *kaddr;
4706 struct page *p;
4707 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4708 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4709 unsigned long end_i = (start_offset + start + min_len - 1) >>
4710 PAGE_CACHE_SHIFT;
4711
4712 if (i != end_i)
4713 return -EINVAL;
4714
4715 if (i == 0) {
4716 offset = start_offset;
4717 *map_start = 0;
4718 } else {
4719 offset = 0;
4720 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4721 }
Chris Masond3977122009-01-05 21:25:51 -05004722
Chris Masond1310b22008-01-24 16:13:08 -05004723 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05004724 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
4725 "wanted %lu %lu\n", (unsigned long long)eb->start,
4726 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05004727 WARN_ON(1);
Josef Bacik850265332011-03-15 14:52:12 -04004728 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05004729 }
4730
4731 p = extent_buffer_page(eb, i);
Chris Masona6591712011-07-19 12:04:14 -04004732 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05004733 *map = kaddr + offset;
4734 *map_len = PAGE_CACHE_SIZE - offset;
4735 return 0;
4736}
Chris Masond1310b22008-01-24 16:13:08 -05004737
Chris Masond1310b22008-01-24 16:13:08 -05004738int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4739 unsigned long start,
4740 unsigned long len)
4741{
4742 size_t cur;
4743 size_t offset;
4744 struct page *page;
4745 char *kaddr;
4746 char *ptr = (char *)ptrv;
4747 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4748 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4749 int ret = 0;
4750
4751 WARN_ON(start > eb->len);
4752 WARN_ON(start + len > eb->start + eb->len);
4753
4754 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4755
Chris Masond3977122009-01-05 21:25:51 -05004756 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004757 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004758
4759 cur = min(len, (PAGE_CACHE_SIZE - offset));
4760
Chris Masona6591712011-07-19 12:04:14 -04004761 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004762 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004763 if (ret)
4764 break;
4765
4766 ptr += cur;
4767 len -= cur;
4768 offset = 0;
4769 i++;
4770 }
4771 return ret;
4772}
Chris Masond1310b22008-01-24 16:13:08 -05004773
4774void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4775 unsigned long start, unsigned long len)
4776{
4777 size_t cur;
4778 size_t offset;
4779 struct page *page;
4780 char *kaddr;
4781 char *src = (char *)srcv;
4782 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4783 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4784
4785 WARN_ON(start > eb->len);
4786 WARN_ON(start + len > eb->start + eb->len);
4787
4788 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4789
Chris Masond3977122009-01-05 21:25:51 -05004790 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004791 page = extent_buffer_page(eb, i);
4792 WARN_ON(!PageUptodate(page));
4793
4794 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004795 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004796 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004797
4798 src += cur;
4799 len -= cur;
4800 offset = 0;
4801 i++;
4802 }
4803}
Chris Masond1310b22008-01-24 16:13:08 -05004804
4805void memset_extent_buffer(struct extent_buffer *eb, char c,
4806 unsigned long start, unsigned long len)
4807{
4808 size_t cur;
4809 size_t offset;
4810 struct page *page;
4811 char *kaddr;
4812 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4813 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4814
4815 WARN_ON(start > eb->len);
4816 WARN_ON(start + len > eb->start + eb->len);
4817
4818 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4819
Chris Masond3977122009-01-05 21:25:51 -05004820 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004821 page = extent_buffer_page(eb, i);
4822 WARN_ON(!PageUptodate(page));
4823
4824 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004825 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004826 memset(kaddr + offset, c, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004827
4828 len -= cur;
4829 offset = 0;
4830 i++;
4831 }
4832}
Chris Masond1310b22008-01-24 16:13:08 -05004833
4834void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4835 unsigned long dst_offset, unsigned long src_offset,
4836 unsigned long len)
4837{
4838 u64 dst_len = dst->len;
4839 size_t cur;
4840 size_t offset;
4841 struct page *page;
4842 char *kaddr;
4843 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4844 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4845
4846 WARN_ON(src->len != dst_len);
4847
4848 offset = (start_offset + dst_offset) &
4849 ((unsigned long)PAGE_CACHE_SIZE - 1);
4850
Chris Masond3977122009-01-05 21:25:51 -05004851 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004852 page = extent_buffer_page(dst, i);
4853 WARN_ON(!PageUptodate(page));
4854
4855 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4856
Chris Masona6591712011-07-19 12:04:14 -04004857 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004858 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004859
4860 src_offset += cur;
4861 len -= cur;
4862 offset = 0;
4863 i++;
4864 }
4865}
Chris Masond1310b22008-01-24 16:13:08 -05004866
4867static void move_pages(struct page *dst_page, struct page *src_page,
4868 unsigned long dst_off, unsigned long src_off,
4869 unsigned long len)
4870{
Chris Masona6591712011-07-19 12:04:14 -04004871 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004872 if (dst_page == src_page) {
4873 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4874 } else {
Chris Masona6591712011-07-19 12:04:14 -04004875 char *src_kaddr = page_address(src_page);
Chris Masond1310b22008-01-24 16:13:08 -05004876 char *p = dst_kaddr + dst_off + len;
4877 char *s = src_kaddr + src_off + len;
4878
4879 while (len--)
4880 *--p = *--s;
Chris Masond1310b22008-01-24 16:13:08 -05004881 }
Chris Masond1310b22008-01-24 16:13:08 -05004882}
4883
Sergei Trofimovich33872062011-04-11 21:52:52 +00004884static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4885{
4886 unsigned long distance = (src > dst) ? src - dst : dst - src;
4887 return distance < len;
4888}
4889
Chris Masond1310b22008-01-24 16:13:08 -05004890static void copy_pages(struct page *dst_page, struct page *src_page,
4891 unsigned long dst_off, unsigned long src_off,
4892 unsigned long len)
4893{
Chris Masona6591712011-07-19 12:04:14 -04004894 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004895 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004896 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004897
Sergei Trofimovich33872062011-04-11 21:52:52 +00004898 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04004899 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00004900 } else {
Chris Masond1310b22008-01-24 16:13:08 -05004901 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004902 if (areas_overlap(src_off, dst_off, len))
4903 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00004904 }
Chris Masond1310b22008-01-24 16:13:08 -05004905
Chris Mason727011e2010-08-06 13:21:20 -04004906 if (must_memmove)
4907 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
4908 else
4909 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05004910}
4911
4912void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4913 unsigned long src_offset, unsigned long len)
4914{
4915 size_t cur;
4916 size_t dst_off_in_page;
4917 size_t src_off_in_page;
4918 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4919 unsigned long dst_i;
4920 unsigned long src_i;
4921
4922 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004923 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4924 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004925 BUG_ON(1);
4926 }
4927 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004928 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4929 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004930 BUG_ON(1);
4931 }
4932
Chris Masond3977122009-01-05 21:25:51 -05004933 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004934 dst_off_in_page = (start_offset + dst_offset) &
4935 ((unsigned long)PAGE_CACHE_SIZE - 1);
4936 src_off_in_page = (start_offset + src_offset) &
4937 ((unsigned long)PAGE_CACHE_SIZE - 1);
4938
4939 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4940 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4941
4942 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4943 src_off_in_page));
4944 cur = min_t(unsigned long, cur,
4945 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4946
4947 copy_pages(extent_buffer_page(dst, dst_i),
4948 extent_buffer_page(dst, src_i),
4949 dst_off_in_page, src_off_in_page, cur);
4950
4951 src_offset += cur;
4952 dst_offset += cur;
4953 len -= cur;
4954 }
4955}
Chris Masond1310b22008-01-24 16:13:08 -05004956
4957void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4958 unsigned long src_offset, unsigned long len)
4959{
4960 size_t cur;
4961 size_t dst_off_in_page;
4962 size_t src_off_in_page;
4963 unsigned long dst_end = dst_offset + len - 1;
4964 unsigned long src_end = src_offset + len - 1;
4965 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4966 unsigned long dst_i;
4967 unsigned long src_i;
4968
4969 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004970 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4971 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004972 BUG_ON(1);
4973 }
4974 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004975 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4976 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004977 BUG_ON(1);
4978 }
Chris Mason727011e2010-08-06 13:21:20 -04004979 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05004980 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4981 return;
4982 }
Chris Masond3977122009-01-05 21:25:51 -05004983 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004984 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
4985 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
4986
4987 dst_off_in_page = (start_offset + dst_end) &
4988 ((unsigned long)PAGE_CACHE_SIZE - 1);
4989 src_off_in_page = (start_offset + src_end) &
4990 ((unsigned long)PAGE_CACHE_SIZE - 1);
4991
4992 cur = min_t(unsigned long, len, src_off_in_page + 1);
4993 cur = min(cur, dst_off_in_page + 1);
4994 move_pages(extent_buffer_page(dst, dst_i),
4995 extent_buffer_page(dst, src_i),
4996 dst_off_in_page - cur + 1,
4997 src_off_in_page - cur + 1, cur);
4998
4999 dst_end -= cur;
5000 src_end -= cur;
5001 len -= cur;
5002 }
5003}
Chris Mason6af118ce2008-07-22 11:18:07 -04005004
Josef Bacik3083ee22012-03-09 16:01:49 -05005005int try_release_extent_buffer(struct page *page, gfp_t mask)
Miao Xie19fe0a82010-10-26 20:57:29 -04005006{
Chris Mason6af118ce2008-07-22 11:18:07 -04005007 struct extent_buffer *eb;
Miao Xie897ca6e2010-10-26 20:57:29 -04005008
Miao Xie19fe0a82010-10-26 20:57:29 -04005009 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005010 * We need to make sure noboody is attaching this page to an eb right
5011 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04005012 */
Josef Bacik3083ee22012-03-09 16:01:49 -05005013 spin_lock(&page->mapping->private_lock);
5014 if (!PagePrivate(page)) {
5015 spin_unlock(&page->mapping->private_lock);
5016 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005017 }
5018
Josef Bacik3083ee22012-03-09 16:01:49 -05005019 eb = (struct extent_buffer *)page->private;
5020 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04005021
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005022 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005023 * This is a little awful but should be ok, we need to make sure that
5024 * the eb doesn't disappear out from under us while we're looking at
5025 * this page.
5026 */
5027 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005028 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05005029 spin_unlock(&eb->refs_lock);
5030 spin_unlock(&page->mapping->private_lock);
5031 return 0;
5032 }
5033 spin_unlock(&page->mapping->private_lock);
5034
5035 if ((mask & GFP_NOFS) == GFP_NOFS)
5036 mask = GFP_NOFS;
5037
5038 /*
5039 * If tree ref isn't set then we know the ref on this eb is a real ref,
5040 * so just return, this page will likely be freed soon anyway.
5041 */
5042 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5043 spin_unlock(&eb->refs_lock);
5044 return 0;
5045 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005046
Josef Bacike64860a2012-07-20 16:05:36 -04005047 return release_extent_buffer(eb, mask);
Chris Mason6af118ce2008-07-22 11:18:07 -04005048}