blob: 907ed0025dd4076dcc0af0f6ca283967e44b85ce [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
Julia Lawall31b1a2b2012-11-03 10:58:34 +0000344 if (end < start)
345 WARN(1, KERN_ERR "btrfs end < start %llu %llu\n",
Chris Masond3977122009-01-05 21:25:51 -0500346 (unsigned long long)end,
347 (unsigned long long)start);
Chris Masond1310b22008-01-24 16:13:08 -0500348 state->start = start;
349 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400350
Xiao Guangrong3150b692011-07-14 03:19:08 +0000351 set_state_bits(tree, state, bits);
352
Chris Masond1310b22008-01-24 16:13:08 -0500353 node = tree_insert(&tree->state, end, &state->rb_node);
354 if (node) {
355 struct extent_state *found;
356 found = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500357 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
358 "%llu %llu\n", (unsigned long long)found->start,
359 (unsigned long long)found->end,
360 (unsigned long long)start, (unsigned long long)end);
Chris Masond1310b22008-01-24 16:13:08 -0500361 return -EEXIST;
362 }
Chris Mason70dec802008-01-29 09:59:12 -0500363 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500364 merge_state(tree, state);
365 return 0;
366}
367
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000368static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
Josef Bacik9ed74f22009-09-11 16:12:44 -0400369 u64 split)
370{
371 if (tree->ops && tree->ops->split_extent_hook)
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000372 tree->ops->split_extent_hook(tree->mapping->host, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400373}
374
Chris Masond1310b22008-01-24 16:13:08 -0500375/*
376 * split a given extent state struct in two, inserting the preallocated
377 * struct 'prealloc' as the newly created second half. 'split' indicates an
378 * offset inside 'orig' where it should be split.
379 *
380 * Before calling,
381 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
382 * are two extent state structs in the tree:
383 * prealloc: [orig->start, split - 1]
384 * orig: [ split, orig->end ]
385 *
386 * The tree locks are not taken by this function. They need to be held
387 * by the caller.
388 */
389static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
390 struct extent_state *prealloc, u64 split)
391{
392 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400393
394 split_cb(tree, orig, split);
395
Chris Masond1310b22008-01-24 16:13:08 -0500396 prealloc->start = orig->start;
397 prealloc->end = split - 1;
398 prealloc->state = orig->state;
399 orig->start = split;
400
401 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
402 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500403 free_extent_state(prealloc);
404 return -EEXIST;
405 }
Chris Mason70dec802008-01-29 09:59:12 -0500406 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500407 return 0;
408}
409
Li Zefancdc6a392012-03-12 16:39:48 +0800410static struct extent_state *next_state(struct extent_state *state)
411{
412 struct rb_node *next = rb_next(&state->rb_node);
413 if (next)
414 return rb_entry(next, struct extent_state, rb_node);
415 else
416 return NULL;
417}
418
Chris Masond1310b22008-01-24 16:13:08 -0500419/*
420 * utility function to clear some bits in an extent state struct.
Wang Sheng-Hui1b303fc2012-04-06 14:35:18 +0800421 * it will optionally wake up any one waiting on this state (wake == 1).
Chris Masond1310b22008-01-24 16:13:08 -0500422 *
423 * If no bits are set on the state struct after clearing things, the
424 * struct is freed and removed from the tree
425 */
Li Zefancdc6a392012-03-12 16:39:48 +0800426static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
427 struct extent_state *state,
428 int *bits, int wake)
Chris Masond1310b22008-01-24 16:13:08 -0500429{
Li Zefancdc6a392012-03-12 16:39:48 +0800430 struct extent_state *next;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400431 int bits_to_clear = *bits & ~EXTENT_CTLBITS;
Chris Masond1310b22008-01-24 16:13:08 -0500432
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400433 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500434 u64 range = state->end - state->start + 1;
435 WARN_ON(range > tree->dirty_bytes);
436 tree->dirty_bytes -= range;
437 }
Chris Mason291d6732008-01-29 15:55:23 -0500438 clear_state_cb(tree, state, bits);
Josef Bacik32c00af2009-10-08 13:34:05 -0400439 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500440 if (wake)
441 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400442 if (state->state == 0) {
Li Zefancdc6a392012-03-12 16:39:48 +0800443 next = next_state(state);
Chris Mason70dec802008-01-29 09:59:12 -0500444 if (state->tree) {
Chris Masond1310b22008-01-24 16:13:08 -0500445 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500446 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500447 free_extent_state(state);
448 } else {
449 WARN_ON(1);
450 }
451 } else {
452 merge_state(tree, state);
Li Zefancdc6a392012-03-12 16:39:48 +0800453 next = next_state(state);
Chris Masond1310b22008-01-24 16:13:08 -0500454 }
Li Zefancdc6a392012-03-12 16:39:48 +0800455 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500456}
457
Xiao Guangrong82337672011-04-20 06:44:57 +0000458static struct extent_state *
459alloc_extent_state_atomic(struct extent_state *prealloc)
460{
461 if (!prealloc)
462 prealloc = alloc_extent_state(GFP_ATOMIC);
463
464 return prealloc;
465}
466
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400467void extent_io_tree_panic(struct extent_io_tree *tree, int err)
468{
469 btrfs_panic(tree_fs_info(tree), err, "Locking error: "
470 "Extent tree was modified by another "
471 "thread while locked.");
472}
473
Chris Masond1310b22008-01-24 16:13:08 -0500474/*
475 * clear some bits on a range in the tree. This may require splitting
476 * or inserting elements in the tree, so the gfp mask is used to
477 * indicate which allocations or sleeping are allowed.
478 *
479 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
480 * the given range from the tree regardless of state (ie for truncate).
481 *
482 * the range [start, end] is inclusive.
483 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100484 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500485 */
486int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400487 int bits, int wake, int delete,
488 struct extent_state **cached_state,
489 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500490{
491 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400492 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500493 struct extent_state *prealloc = NULL;
494 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400495 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500496 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000497 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500498
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400499 if (delete)
500 bits |= ~EXTENT_CTLBITS;
501 bits |= EXTENT_FIRST_DELALLOC;
502
Josef Bacik2ac55d42010-02-03 19:33:23 +0000503 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
504 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500505again:
506 if (!prealloc && (mask & __GFP_WAIT)) {
507 prealloc = alloc_extent_state(mask);
508 if (!prealloc)
509 return -ENOMEM;
510 }
511
Chris Masoncad321a2008-12-17 14:51:42 -0500512 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400513 if (cached_state) {
514 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000515
516 if (clear) {
517 *cached_state = NULL;
518 cached_state = NULL;
519 }
520
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400521 if (cached && cached->tree && cached->start <= start &&
522 cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000523 if (clear)
524 atomic_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400525 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400526 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400527 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000528 if (clear)
529 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400530 }
Chris Masond1310b22008-01-24 16:13:08 -0500531 /*
532 * this search will find the extents that end after
533 * our range starts
534 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500535 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500536 if (!node)
537 goto out;
538 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400539hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500540 if (state->start > end)
541 goto out;
542 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400543 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500544
Liu Bo04493142012-02-16 18:34:37 +0800545 /* the state doesn't have the wanted bits, go ahead */
Li Zefancdc6a392012-03-12 16:39:48 +0800546 if (!(state->state & bits)) {
547 state = next_state(state);
Liu Bo04493142012-02-16 18:34:37 +0800548 goto next;
Li Zefancdc6a392012-03-12 16:39:48 +0800549 }
Liu Bo04493142012-02-16 18:34:37 +0800550
Chris Masond1310b22008-01-24 16:13:08 -0500551 /*
552 * | ---- desired range ---- |
553 * | state | or
554 * | ------------- state -------------- |
555 *
556 * We need to split the extent we found, and may flip
557 * bits on second half.
558 *
559 * If the extent we found extends past our range, we
560 * just split and search again. It'll get split again
561 * the next time though.
562 *
563 * If the extent we found is inside our range, we clear
564 * the desired bit on it.
565 */
566
567 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000568 prealloc = alloc_extent_state_atomic(prealloc);
569 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500570 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400571 if (err)
572 extent_io_tree_panic(tree, err);
573
Chris Masond1310b22008-01-24 16:13:08 -0500574 prealloc = NULL;
575 if (err)
576 goto out;
577 if (state->end <= end) {
Liu Bod1ac6e42012-05-10 18:10:39 +0800578 state = clear_state_bit(tree, state, &bits, wake);
579 goto next;
Chris Masond1310b22008-01-24 16:13:08 -0500580 }
581 goto search_again;
582 }
583 /*
584 * | ---- desired range ---- |
585 * | state |
586 * We need to split the extent, and clear the bit
587 * on the first half
588 */
589 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000590 prealloc = alloc_extent_state_atomic(prealloc);
591 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500592 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400593 if (err)
594 extent_io_tree_panic(tree, err);
595
Chris Masond1310b22008-01-24 16:13:08 -0500596 if (wake)
597 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400598
Jeff Mahoney6763af82012-03-01 14:56:29 +0100599 clear_state_bit(tree, prealloc, &bits, wake);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400600
Chris Masond1310b22008-01-24 16:13:08 -0500601 prealloc = NULL;
602 goto out;
603 }
Chris Mason42daec22009-09-23 19:51:09 -0400604
Li Zefancdc6a392012-03-12 16:39:48 +0800605 state = clear_state_bit(tree, state, &bits, wake);
Liu Bo04493142012-02-16 18:34:37 +0800606next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400607 if (last_end == (u64)-1)
608 goto out;
609 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800610 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800611 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500612 goto search_again;
613
614out:
Chris Masoncad321a2008-12-17 14:51:42 -0500615 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500616 if (prealloc)
617 free_extent_state(prealloc);
618
Jeff Mahoney6763af82012-03-01 14:56:29 +0100619 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500620
621search_again:
622 if (start > end)
623 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500624 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500625 if (mask & __GFP_WAIT)
626 cond_resched();
627 goto again;
628}
Chris Masond1310b22008-01-24 16:13:08 -0500629
Jeff Mahoney143bede2012-03-01 14:56:26 +0100630static void wait_on_state(struct extent_io_tree *tree,
631 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500632 __releases(tree->lock)
633 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500634{
635 DEFINE_WAIT(wait);
636 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500637 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500638 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500639 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500640 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500641}
642
643/*
644 * waits for one or more bits to clear on a range in the state tree.
645 * The range [start, end] is inclusive.
646 * The tree lock is taken by this function
647 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100648void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
Chris Masond1310b22008-01-24 16:13:08 -0500649{
650 struct extent_state *state;
651 struct rb_node *node;
652
Chris Masoncad321a2008-12-17 14:51:42 -0500653 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500654again:
655 while (1) {
656 /*
657 * this search will find all the extents that end after
658 * our range starts
659 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500660 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500661 if (!node)
662 break;
663
664 state = rb_entry(node, struct extent_state, rb_node);
665
666 if (state->start > end)
667 goto out;
668
669 if (state->state & bits) {
670 start = state->start;
671 atomic_inc(&state->refs);
672 wait_on_state(tree, state);
673 free_extent_state(state);
674 goto again;
675 }
676 start = state->end + 1;
677
678 if (start > end)
679 break;
680
Xiao Guangrongded91f02011-07-14 03:19:27 +0000681 cond_resched_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500682 }
683out:
Chris Masoncad321a2008-12-17 14:51:42 -0500684 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500685}
Chris Masond1310b22008-01-24 16:13:08 -0500686
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000687static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500688 struct extent_state *state,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400689 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500690{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400691 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400692
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000693 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400694 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500695 u64 range = state->end - state->start + 1;
696 tree->dirty_bytes += range;
697 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400698 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500699}
700
Chris Mason2c64c532009-09-02 15:04:12 -0400701static void cache_state(struct extent_state *state,
702 struct extent_state **cached_ptr)
703{
704 if (cached_ptr && !(*cached_ptr)) {
705 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
706 *cached_ptr = state;
707 atomic_inc(&state->refs);
708 }
709 }
710}
711
Arne Jansen507903b2011-04-06 10:02:20 +0000712static void uncache_state(struct extent_state **cached_ptr)
713{
714 if (cached_ptr && (*cached_ptr)) {
715 struct extent_state *state = *cached_ptr;
Chris Mason109b36a2011-04-12 13:57:39 -0400716 *cached_ptr = NULL;
717 free_extent_state(state);
Arne Jansen507903b2011-04-06 10:02:20 +0000718 }
719}
720
Chris Masond1310b22008-01-24 16:13:08 -0500721/*
Chris Mason1edbb732009-09-02 13:24:36 -0400722 * set some bits on a range in the tree. This may require allocations or
723 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500724 *
Chris Mason1edbb732009-09-02 13:24:36 -0400725 * If any of the exclusive bits are set, this will fail with -EEXIST if some
726 * part of the range already has the desired bits set. The start of the
727 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500728 *
Chris Mason1edbb732009-09-02 13:24:36 -0400729 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500730 */
Chris Mason1edbb732009-09-02 13:24:36 -0400731
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100732static int __must_check
733__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
734 int bits, int exclusive_bits, u64 *failed_start,
735 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500736{
737 struct extent_state *state;
738 struct extent_state *prealloc = NULL;
739 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500740 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500741 u64 last_start;
742 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400743
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400744 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500745again:
746 if (!prealloc && (mask & __GFP_WAIT)) {
747 prealloc = alloc_extent_state(mask);
Xiao Guangrong82337672011-04-20 06:44:57 +0000748 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500749 }
750
Chris Masoncad321a2008-12-17 14:51:42 -0500751 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400752 if (cached_state && *cached_state) {
753 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400754 if (state->start <= start && state->end > start &&
755 state->tree) {
Chris Mason9655d292009-09-02 15:22:30 -0400756 node = &state->rb_node;
757 goto hit_next;
758 }
759 }
Chris Masond1310b22008-01-24 16:13:08 -0500760 /*
761 * this search will find all the extents that end after
762 * our range starts.
763 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500764 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500765 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000766 prealloc = alloc_extent_state_atomic(prealloc);
767 BUG_ON(!prealloc);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400768 err = insert_state(tree, prealloc, start, end, &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400769 if (err)
770 extent_io_tree_panic(tree, err);
771
Chris Masond1310b22008-01-24 16:13:08 -0500772 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500773 goto out;
774 }
Chris Masond1310b22008-01-24 16:13:08 -0500775 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400776hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500777 last_start = state->start;
778 last_end = state->end;
779
780 /*
781 * | ---- desired range ---- |
782 * | state |
783 *
784 * Just lock what we found and keep going
785 */
786 if (state->start == start && state->end <= end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400787 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500788 *failed_start = state->start;
789 err = -EEXIST;
790 goto out;
791 }
Chris Mason42daec22009-09-23 19:51:09 -0400792
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000793 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400794 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500795 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400796 if (last_end == (u64)-1)
797 goto out;
798 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800799 state = next_state(state);
800 if (start < end && state && state->start == start &&
801 !need_resched())
802 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500803 goto search_again;
804 }
805
806 /*
807 * | ---- desired range ---- |
808 * | state |
809 * or
810 * | ------------- state -------------- |
811 *
812 * We need to split the extent we found, and may flip bits on
813 * second half.
814 *
815 * If the extent we found extends past our
816 * range, we just split and search again. It'll get split
817 * again the next time though.
818 *
819 * If the extent we found is inside our range, we set the
820 * desired bit on it.
821 */
822 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400823 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500824 *failed_start = start;
825 err = -EEXIST;
826 goto out;
827 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000828
829 prealloc = alloc_extent_state_atomic(prealloc);
830 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500831 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400832 if (err)
833 extent_io_tree_panic(tree, err);
834
Chris Masond1310b22008-01-24 16:13:08 -0500835 prealloc = NULL;
836 if (err)
837 goto out;
838 if (state->end <= end) {
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000839 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400840 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500841 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400842 if (last_end == (u64)-1)
843 goto out;
844 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800845 state = next_state(state);
846 if (start < end && state && state->start == start &&
847 !need_resched())
848 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500849 }
850 goto search_again;
851 }
852 /*
853 * | ---- desired range ---- |
854 * | state | or | state |
855 *
856 * There's a hole, we need to insert something in it and
857 * ignore the extent we found.
858 */
859 if (state->start > start) {
860 u64 this_end;
861 if (end < last_start)
862 this_end = end;
863 else
Chris Masond3977122009-01-05 21:25:51 -0500864 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000865
866 prealloc = alloc_extent_state_atomic(prealloc);
867 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000868
869 /*
870 * Avoid to free 'prealloc' if it can be merged with
871 * the later extent.
872 */
Chris Masond1310b22008-01-24 16:13:08 -0500873 err = insert_state(tree, prealloc, start, this_end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400874 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400875 if (err)
876 extent_io_tree_panic(tree, err);
877
Chris Mason2c64c532009-09-02 15:04:12 -0400878 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500879 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500880 start = this_end + 1;
881 goto search_again;
882 }
883 /*
884 * | ---- desired range ---- |
885 * | state |
886 * We need to split the extent, and set the bit
887 * on the first half
888 */
889 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400890 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500891 *failed_start = start;
892 err = -EEXIST;
893 goto out;
894 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000895
896 prealloc = alloc_extent_state_atomic(prealloc);
897 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500898 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400899 if (err)
900 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -0500901
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000902 set_state_bits(tree, prealloc, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400903 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500904 merge_state(tree, prealloc);
905 prealloc = NULL;
906 goto out;
907 }
908
909 goto search_again;
910
911out:
Chris Masoncad321a2008-12-17 14:51:42 -0500912 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500913 if (prealloc)
914 free_extent_state(prealloc);
915
916 return err;
917
918search_again:
919 if (start > end)
920 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500921 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500922 if (mask & __GFP_WAIT)
923 cond_resched();
924 goto again;
925}
Chris Masond1310b22008-01-24 16:13:08 -0500926
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100927int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits,
928 u64 *failed_start, struct extent_state **cached_state,
929 gfp_t mask)
930{
931 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
932 cached_state, mask);
933}
934
935
Josef Bacik462d6fa2011-09-26 13:56:12 -0400936/**
Liu Bo10983f22012-07-11 15:26:19 +0800937 * convert_extent_bit - convert all bits in a given range from one bit to
938 * another
Josef Bacik462d6fa2011-09-26 13:56:12 -0400939 * @tree: the io tree to search
940 * @start: the start offset in bytes
941 * @end: the end offset in bytes (inclusive)
942 * @bits: the bits to set in this range
943 * @clear_bits: the bits to clear in this range
Josef Bacike6138872012-09-27 17:07:30 -0400944 * @cached_state: state that we're going to cache
Josef Bacik462d6fa2011-09-26 13:56:12 -0400945 * @mask: the allocation mask
946 *
947 * This will go through and set bits for the given range. If any states exist
948 * already in this range they are set with the given bit and cleared of the
949 * clear_bits. This is only meant to be used by things that are mergeable, ie
950 * converting from say DELALLOC to DIRTY. This is not meant to be used with
951 * boundary bits like LOCK.
952 */
953int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacike6138872012-09-27 17:07:30 -0400954 int bits, int clear_bits,
955 struct extent_state **cached_state, gfp_t mask)
Josef Bacik462d6fa2011-09-26 13:56:12 -0400956{
957 struct extent_state *state;
958 struct extent_state *prealloc = NULL;
959 struct rb_node *node;
960 int err = 0;
961 u64 last_start;
962 u64 last_end;
963
964again:
965 if (!prealloc && (mask & __GFP_WAIT)) {
966 prealloc = alloc_extent_state(mask);
967 if (!prealloc)
968 return -ENOMEM;
969 }
970
971 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -0400972 if (cached_state && *cached_state) {
973 state = *cached_state;
974 if (state->start <= start && state->end > start &&
975 state->tree) {
976 node = &state->rb_node;
977 goto hit_next;
978 }
979 }
980
Josef Bacik462d6fa2011-09-26 13:56:12 -0400981 /*
982 * this search will find all the extents that end after
983 * our range starts.
984 */
985 node = tree_search(tree, start);
986 if (!node) {
987 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -0500988 if (!prealloc) {
989 err = -ENOMEM;
990 goto out;
991 }
Josef Bacik462d6fa2011-09-26 13:56:12 -0400992 err = insert_state(tree, prealloc, start, end, &bits);
993 prealloc = NULL;
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400994 if (err)
995 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -0400996 goto out;
997 }
998 state = rb_entry(node, struct extent_state, rb_node);
999hit_next:
1000 last_start = state->start;
1001 last_end = state->end;
1002
1003 /*
1004 * | ---- desired range ---- |
1005 * | state |
1006 *
1007 * Just lock what we found and keep going
1008 */
1009 if (state->start == start && state->end <= end) {
Josef Bacik462d6fa2011-09-26 13:56:12 -04001010 set_state_bits(tree, state, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001011 cache_state(state, cached_state);
Liu Bod1ac6e42012-05-10 18:10:39 +08001012 state = clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001013 if (last_end == (u64)-1)
1014 goto out;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001015 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001016 if (start < end && state && state->start == start &&
1017 !need_resched())
1018 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001019 goto search_again;
1020 }
1021
1022 /*
1023 * | ---- desired range ---- |
1024 * | state |
1025 * or
1026 * | ------------- state -------------- |
1027 *
1028 * We need to split the extent we found, and may flip bits on
1029 * second half.
1030 *
1031 * If the extent we found extends past our
1032 * range, we just split and search again. It'll get split
1033 * again the next time though.
1034 *
1035 * If the extent we found is inside our range, we set the
1036 * desired bit on it.
1037 */
1038 if (state->start < start) {
1039 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001040 if (!prealloc) {
1041 err = -ENOMEM;
1042 goto out;
1043 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001044 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001045 if (err)
1046 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001047 prealloc = NULL;
1048 if (err)
1049 goto out;
1050 if (state->end <= end) {
1051 set_state_bits(tree, state, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001052 cache_state(state, cached_state);
Liu Bod1ac6e42012-05-10 18:10:39 +08001053 state = clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001054 if (last_end == (u64)-1)
1055 goto out;
1056 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001057 if (start < end && state && state->start == start &&
1058 !need_resched())
1059 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001060 }
1061 goto search_again;
1062 }
1063 /*
1064 * | ---- desired range ---- |
1065 * | state | or | state |
1066 *
1067 * There's a hole, we need to insert something in it and
1068 * ignore the extent we found.
1069 */
1070 if (state->start > start) {
1071 u64 this_end;
1072 if (end < last_start)
1073 this_end = end;
1074 else
1075 this_end = last_start - 1;
1076
1077 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001078 if (!prealloc) {
1079 err = -ENOMEM;
1080 goto out;
1081 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001082
1083 /*
1084 * Avoid to free 'prealloc' if it can be merged with
1085 * the later extent.
1086 */
1087 err = insert_state(tree, prealloc, start, this_end,
1088 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001089 if (err)
1090 extent_io_tree_panic(tree, err);
Josef Bacike6138872012-09-27 17:07:30 -04001091 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001092 prealloc = NULL;
1093 start = this_end + 1;
1094 goto search_again;
1095 }
1096 /*
1097 * | ---- desired range ---- |
1098 * | state |
1099 * We need to split the extent, and set the bit
1100 * on the first half
1101 */
1102 if (state->start <= end && state->end > end) {
1103 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001104 if (!prealloc) {
1105 err = -ENOMEM;
1106 goto out;
1107 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001108
1109 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001110 if (err)
1111 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001112
1113 set_state_bits(tree, prealloc, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001114 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001115 clear_state_bit(tree, prealloc, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001116 prealloc = NULL;
1117 goto out;
1118 }
1119
1120 goto search_again;
1121
1122out:
1123 spin_unlock(&tree->lock);
1124 if (prealloc)
1125 free_extent_state(prealloc);
1126
1127 return err;
1128
1129search_again:
1130 if (start > end)
1131 goto out;
1132 spin_unlock(&tree->lock);
1133 if (mask & __GFP_WAIT)
1134 cond_resched();
1135 goto again;
1136}
1137
Chris Masond1310b22008-01-24 16:13:08 -05001138/* wrappers around set/clear extent bit */
1139int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1140 gfp_t mask)
1141{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001142 return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001143 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001144}
Chris Masond1310b22008-01-24 16:13:08 -05001145
1146int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1147 int bits, gfp_t mask)
1148{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001149 return set_extent_bit(tree, start, end, bits, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001150 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001151}
Chris Masond1310b22008-01-24 16:13:08 -05001152
1153int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1154 int bits, gfp_t mask)
1155{
Chris Mason2c64c532009-09-02 15:04:12 -04001156 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001157}
Chris Masond1310b22008-01-24 16:13:08 -05001158
1159int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001160 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001161{
1162 return set_extent_bit(tree, start, end,
Liu Bofee187d2011-09-29 15:55:28 +08001163 EXTENT_DELALLOC | EXTENT_UPTODATE,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001164 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001165}
Chris Masond1310b22008-01-24 16:13:08 -05001166
Liu Bo9e8a4a82012-09-05 19:10:51 -06001167int set_extent_defrag(struct extent_io_tree *tree, u64 start, u64 end,
1168 struct extent_state **cached_state, gfp_t mask)
1169{
1170 return set_extent_bit(tree, start, end,
1171 EXTENT_DELALLOC | EXTENT_UPTODATE | EXTENT_DEFRAG,
1172 NULL, cached_state, mask);
1173}
1174
Chris Masond1310b22008-01-24 16:13:08 -05001175int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1176 gfp_t mask)
1177{
1178 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04001179 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001180 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001181}
Chris Masond1310b22008-01-24 16:13:08 -05001182
1183int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1184 gfp_t mask)
1185{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001186 return set_extent_bit(tree, start, end, EXTENT_NEW, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001187 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001188}
Chris Masond1310b22008-01-24 16:13:08 -05001189
Chris Masond1310b22008-01-24 16:13:08 -05001190int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +00001191 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001192{
Arne Jansen507903b2011-04-06 10:02:20 +00001193 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001194 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001195}
Chris Masond1310b22008-01-24 16:13:08 -05001196
Josef Bacik5fd02042012-05-02 14:00:54 -04001197int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1198 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001199{
Chris Mason2c64c532009-09-02 15:04:12 -04001200 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001201 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001202}
Chris Masond1310b22008-01-24 16:13:08 -05001203
Chris Masond352ac62008-09-29 15:18:18 -04001204/*
1205 * either insert or lock state struct between start and end use mask to tell
1206 * us if waiting is desired.
1207 */
Chris Mason1edbb732009-09-02 13:24:36 -04001208int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001209 int bits, struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001210{
1211 int err;
1212 u64 failed_start;
1213 while (1) {
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001214 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
1215 EXTENT_LOCKED, &failed_start,
1216 cached_state, GFP_NOFS);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001217 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001218 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1219 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001220 } else
Chris Masond1310b22008-01-24 16:13:08 -05001221 break;
Chris Masond1310b22008-01-24 16:13:08 -05001222 WARN_ON(start > end);
1223 }
1224 return err;
1225}
Chris Masond1310b22008-01-24 16:13:08 -05001226
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001227int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Mason1edbb732009-09-02 13:24:36 -04001228{
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001229 return lock_extent_bits(tree, start, end, 0, NULL);
Chris Mason1edbb732009-09-02 13:24:36 -04001230}
1231
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001232int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001233{
1234 int err;
1235 u64 failed_start;
1236
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001237 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1238 &failed_start, NULL, GFP_NOFS);
Yan Zheng66435582008-10-30 14:19:50 -04001239 if (err == -EEXIST) {
1240 if (failed_start > start)
1241 clear_extent_bit(tree, start, failed_start - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001242 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
Josef Bacik25179202008-10-29 14:49:05 -04001243 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001244 }
Josef Bacik25179202008-10-29 14:49:05 -04001245 return 1;
1246}
Josef Bacik25179202008-10-29 14:49:05 -04001247
Chris Mason2c64c532009-09-02 15:04:12 -04001248int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1249 struct extent_state **cached, gfp_t mask)
1250{
1251 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1252 mask);
1253}
1254
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001255int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001256{
Chris Mason2c64c532009-09-02 15:04:12 -04001257 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001258 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001259}
Chris Masond1310b22008-01-24 16:13:08 -05001260
1261/*
Chris Masond1310b22008-01-24 16:13:08 -05001262 * helper function to set both pages and extents in the tree writeback
1263 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001264static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001265{
1266 unsigned long index = start >> PAGE_CACHE_SHIFT;
1267 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1268 struct page *page;
1269
1270 while (index <= end_index) {
1271 page = find_get_page(tree->mapping, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001272 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Masond1310b22008-01-24 16:13:08 -05001273 set_page_writeback(page);
1274 page_cache_release(page);
1275 index++;
1276 }
Chris Masond1310b22008-01-24 16:13:08 -05001277 return 0;
1278}
Chris Masond1310b22008-01-24 16:13:08 -05001279
Chris Masond352ac62008-09-29 15:18:18 -04001280/* find the first state struct with 'bits' set after 'start', and
1281 * return it. tree->lock must be held. NULL will returned if
1282 * nothing was found after 'start'
1283 */
Chris Masond7fc6402008-02-18 12:12:38 -05001284struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1285 u64 start, int bits)
1286{
1287 struct rb_node *node;
1288 struct extent_state *state;
1289
1290 /*
1291 * this search will find all the extents that end after
1292 * our range starts.
1293 */
1294 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001295 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001296 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001297
Chris Masond3977122009-01-05 21:25:51 -05001298 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001299 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001300 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001301 return state;
Chris Masond3977122009-01-05 21:25:51 -05001302
Chris Masond7fc6402008-02-18 12:12:38 -05001303 node = rb_next(node);
1304 if (!node)
1305 break;
1306 }
1307out:
1308 return NULL;
1309}
Chris Masond7fc6402008-02-18 12:12:38 -05001310
Chris Masond352ac62008-09-29 15:18:18 -04001311/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001312 * find the first offset in the io tree with 'bits' set. zero is
1313 * returned if we find something, and *start_ret and *end_ret are
1314 * set to reflect the state struct that was found.
1315 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001316 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001317 */
1318int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
Josef Bacike6138872012-09-27 17:07:30 -04001319 u64 *start_ret, u64 *end_ret, int bits,
1320 struct extent_state **cached_state)
Xiao Guangrong69261c42011-07-14 03:19:45 +00001321{
1322 struct extent_state *state;
Josef Bacike6138872012-09-27 17:07:30 -04001323 struct rb_node *n;
Xiao Guangrong69261c42011-07-14 03:19:45 +00001324 int ret = 1;
1325
1326 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001327 if (cached_state && *cached_state) {
1328 state = *cached_state;
1329 if (state->end == start - 1 && state->tree) {
1330 n = rb_next(&state->rb_node);
1331 while (n) {
1332 state = rb_entry(n, struct extent_state,
1333 rb_node);
1334 if (state->state & bits)
1335 goto got_it;
1336 n = rb_next(n);
1337 }
1338 free_extent_state(*cached_state);
1339 *cached_state = NULL;
1340 goto out;
1341 }
1342 free_extent_state(*cached_state);
1343 *cached_state = NULL;
1344 }
1345
Xiao Guangrong69261c42011-07-14 03:19:45 +00001346 state = find_first_extent_bit_state(tree, start, bits);
Josef Bacike6138872012-09-27 17:07:30 -04001347got_it:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001348 if (state) {
Josef Bacike6138872012-09-27 17:07:30 -04001349 cache_state(state, cached_state);
Xiao Guangrong69261c42011-07-14 03:19:45 +00001350 *start_ret = state->start;
1351 *end_ret = state->end;
1352 ret = 0;
1353 }
Josef Bacike6138872012-09-27 17:07:30 -04001354out:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001355 spin_unlock(&tree->lock);
1356 return ret;
1357}
1358
1359/*
Chris Masond352ac62008-09-29 15:18:18 -04001360 * find a contiguous range of bytes in the file marked as delalloc, not
1361 * more than 'max_bytes'. start and end are used to return the range,
1362 *
1363 * 1 is returned if we find something, 0 if nothing was in the tree
1364 */
Chris Masonc8b97812008-10-29 14:49:59 -04001365static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001366 u64 *start, u64 *end, u64 max_bytes,
1367 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001368{
1369 struct rb_node *node;
1370 struct extent_state *state;
1371 u64 cur_start = *start;
1372 u64 found = 0;
1373 u64 total_bytes = 0;
1374
Chris Masoncad321a2008-12-17 14:51:42 -05001375 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001376
Chris Masond1310b22008-01-24 16:13:08 -05001377 /*
1378 * this search will find all the extents that end after
1379 * our range starts.
1380 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001381 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001382 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001383 if (!found)
1384 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001385 goto out;
1386 }
1387
Chris Masond3977122009-01-05 21:25:51 -05001388 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001389 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001390 if (found && (state->start != cur_start ||
1391 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001392 goto out;
1393 }
1394 if (!(state->state & EXTENT_DELALLOC)) {
1395 if (!found)
1396 *end = state->end;
1397 goto out;
1398 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001399 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001400 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001401 *cached_state = state;
1402 atomic_inc(&state->refs);
1403 }
Chris Masond1310b22008-01-24 16:13:08 -05001404 found++;
1405 *end = state->end;
1406 cur_start = state->end + 1;
1407 node = rb_next(node);
1408 if (!node)
1409 break;
1410 total_bytes += state->end - state->start + 1;
1411 if (total_bytes >= max_bytes)
1412 break;
1413 }
1414out:
Chris Masoncad321a2008-12-17 14:51:42 -05001415 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001416 return found;
1417}
1418
Jeff Mahoney143bede2012-03-01 14:56:26 +01001419static noinline void __unlock_for_delalloc(struct inode *inode,
1420 struct page *locked_page,
1421 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001422{
1423 int ret;
1424 struct page *pages[16];
1425 unsigned long index = start >> PAGE_CACHE_SHIFT;
1426 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1427 unsigned long nr_pages = end_index - index + 1;
1428 int i;
1429
1430 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001431 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001432
Chris Masond3977122009-01-05 21:25:51 -05001433 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001434 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001435 min_t(unsigned long, nr_pages,
1436 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001437 for (i = 0; i < ret; i++) {
1438 if (pages[i] != locked_page)
1439 unlock_page(pages[i]);
1440 page_cache_release(pages[i]);
1441 }
1442 nr_pages -= ret;
1443 index += ret;
1444 cond_resched();
1445 }
Chris Masonc8b97812008-10-29 14:49:59 -04001446}
1447
1448static noinline int lock_delalloc_pages(struct inode *inode,
1449 struct page *locked_page,
1450 u64 delalloc_start,
1451 u64 delalloc_end)
1452{
1453 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1454 unsigned long start_index = index;
1455 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1456 unsigned long pages_locked = 0;
1457 struct page *pages[16];
1458 unsigned long nrpages;
1459 int ret;
1460 int i;
1461
1462 /* the caller is responsible for locking the start index */
1463 if (index == locked_page->index && index == end_index)
1464 return 0;
1465
1466 /* skip the page at the start index */
1467 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001468 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001469 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001470 min_t(unsigned long,
1471 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001472 if (ret == 0) {
1473 ret = -EAGAIN;
1474 goto done;
1475 }
1476 /* now we have an array of pages, lock them all */
1477 for (i = 0; i < ret; i++) {
1478 /*
1479 * the caller is taking responsibility for
1480 * locked_page
1481 */
Chris Mason771ed682008-11-06 22:02:51 -05001482 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001483 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001484 if (!PageDirty(pages[i]) ||
1485 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001486 ret = -EAGAIN;
1487 unlock_page(pages[i]);
1488 page_cache_release(pages[i]);
1489 goto done;
1490 }
1491 }
Chris Masonc8b97812008-10-29 14:49:59 -04001492 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001493 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001494 }
Chris Masonc8b97812008-10-29 14:49:59 -04001495 nrpages -= ret;
1496 index += ret;
1497 cond_resched();
1498 }
1499 ret = 0;
1500done:
1501 if (ret && pages_locked) {
1502 __unlock_for_delalloc(inode, locked_page,
1503 delalloc_start,
1504 ((u64)(start_index + pages_locked - 1)) <<
1505 PAGE_CACHE_SHIFT);
1506 }
1507 return ret;
1508}
1509
1510/*
1511 * find a contiguous range of bytes in the file marked as delalloc, not
1512 * more than 'max_bytes'. start and end are used to return the range,
1513 *
1514 * 1 is returned if we find something, 0 if nothing was in the tree
1515 */
1516static noinline u64 find_lock_delalloc_range(struct inode *inode,
1517 struct extent_io_tree *tree,
1518 struct page *locked_page,
1519 u64 *start, u64 *end,
1520 u64 max_bytes)
1521{
1522 u64 delalloc_start;
1523 u64 delalloc_end;
1524 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001525 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001526 int ret;
1527 int loops = 0;
1528
1529again:
1530 /* step one, find a bunch of delalloc bytes starting at start */
1531 delalloc_start = *start;
1532 delalloc_end = 0;
1533 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001534 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001535 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001536 *start = delalloc_start;
1537 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001538 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001539 return found;
1540 }
1541
1542 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001543 * start comes from the offset of locked_page. We have to lock
1544 * pages in order, so we can't process delalloc bytes before
1545 * locked_page
1546 */
Chris Masond3977122009-01-05 21:25:51 -05001547 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001548 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001549
1550 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001551 * make sure to limit the number of pages we try to lock down
1552 * if we're looping.
1553 */
Chris Masond3977122009-01-05 21:25:51 -05001554 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001555 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001556
Chris Masonc8b97812008-10-29 14:49:59 -04001557 /* step two, lock all the pages after the page that has start */
1558 ret = lock_delalloc_pages(inode, locked_page,
1559 delalloc_start, delalloc_end);
1560 if (ret == -EAGAIN) {
1561 /* some of the pages are gone, lets avoid looping by
1562 * shortening the size of the delalloc range we're searching
1563 */
Chris Mason9655d292009-09-02 15:22:30 -04001564 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001565 if (!loops) {
1566 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1567 max_bytes = PAGE_CACHE_SIZE - offset;
1568 loops = 1;
1569 goto again;
1570 } else {
1571 found = 0;
1572 goto out_failed;
1573 }
1574 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001575 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
Chris Masonc8b97812008-10-29 14:49:59 -04001576
1577 /* step three, lock the state bits for the whole range */
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001578 lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001579
1580 /* then test to make sure it is all still delalloc */
1581 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001582 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001583 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001584 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1585 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001586 __unlock_for_delalloc(inode, locked_page,
1587 delalloc_start, delalloc_end);
1588 cond_resched();
1589 goto again;
1590 }
Chris Mason9655d292009-09-02 15:22:30 -04001591 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001592 *start = delalloc_start;
1593 *end = delalloc_end;
1594out_failed:
1595 return found;
1596}
1597
1598int extent_clear_unlock_delalloc(struct inode *inode,
1599 struct extent_io_tree *tree,
1600 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001601 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001602{
1603 int ret;
1604 struct page *pages[16];
1605 unsigned long index = start >> PAGE_CACHE_SHIFT;
1606 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1607 unsigned long nr_pages = end_index - index + 1;
1608 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001609 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001610
Chris Masona791e352009-10-08 11:27:10 -04001611 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001612 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001613 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001614 clear_bits |= EXTENT_DIRTY;
1615
Chris Masona791e352009-10-08 11:27:10 -04001616 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001617 clear_bits |= EXTENT_DELALLOC;
1618
Chris Mason2c64c532009-09-02 15:04:12 -04001619 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001620 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1621 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1622 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001623 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001624
Chris Masond3977122009-01-05 21:25:51 -05001625 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001626 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001627 min_t(unsigned long,
1628 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001629 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001630
Chris Masona791e352009-10-08 11:27:10 -04001631 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001632 SetPagePrivate2(pages[i]);
1633
Chris Masonc8b97812008-10-29 14:49:59 -04001634 if (pages[i] == locked_page) {
1635 page_cache_release(pages[i]);
1636 continue;
1637 }
Chris Masona791e352009-10-08 11:27:10 -04001638 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001639 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001640 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001641 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001642 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001643 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001644 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001645 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001646 page_cache_release(pages[i]);
1647 }
1648 nr_pages -= ret;
1649 index += ret;
1650 cond_resched();
1651 }
1652 return 0;
1653}
Chris Masonc8b97812008-10-29 14:49:59 -04001654
Chris Masond352ac62008-09-29 15:18:18 -04001655/*
1656 * count the number of bytes in the tree that have a given bit(s)
1657 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1658 * cached. The total number found is returned.
1659 */
Chris Masond1310b22008-01-24 16:13:08 -05001660u64 count_range_bits(struct extent_io_tree *tree,
1661 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001662 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001663{
1664 struct rb_node *node;
1665 struct extent_state *state;
1666 u64 cur_start = *start;
1667 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001668 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001669 int found = 0;
1670
1671 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001672 WARN_ON(1);
1673 return 0;
1674 }
1675
Chris Masoncad321a2008-12-17 14:51:42 -05001676 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001677 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1678 total_bytes = tree->dirty_bytes;
1679 goto out;
1680 }
1681 /*
1682 * this search will find all the extents that end after
1683 * our range starts.
1684 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001685 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001686 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001687 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001688
Chris Masond3977122009-01-05 21:25:51 -05001689 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001690 state = rb_entry(node, struct extent_state, rb_node);
1691 if (state->start > search_end)
1692 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001693 if (contig && found && state->start > last + 1)
1694 break;
1695 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001696 total_bytes += min(search_end, state->end) + 1 -
1697 max(cur_start, state->start);
1698 if (total_bytes >= max_bytes)
1699 break;
1700 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001701 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001702 found = 1;
1703 }
Chris Masonec29ed52011-02-23 16:23:20 -05001704 last = state->end;
1705 } else if (contig && found) {
1706 break;
Chris Masond1310b22008-01-24 16:13:08 -05001707 }
1708 node = rb_next(node);
1709 if (!node)
1710 break;
1711 }
1712out:
Chris Masoncad321a2008-12-17 14:51:42 -05001713 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001714 return total_bytes;
1715}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001716
Chris Masond352ac62008-09-29 15:18:18 -04001717/*
1718 * set the private field for a given byte offset in the tree. If there isn't
1719 * an extent_state there already, this does nothing.
1720 */
Chris Masond1310b22008-01-24 16:13:08 -05001721int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1722{
1723 struct rb_node *node;
1724 struct extent_state *state;
1725 int ret = 0;
1726
Chris Masoncad321a2008-12-17 14:51:42 -05001727 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001728 /*
1729 * this search will find all the extents that end after
1730 * our range starts.
1731 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001732 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001733 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001734 ret = -ENOENT;
1735 goto out;
1736 }
1737 state = rb_entry(node, struct extent_state, rb_node);
1738 if (state->start != start) {
1739 ret = -ENOENT;
1740 goto out;
1741 }
1742 state->private = private;
1743out:
Chris Masoncad321a2008-12-17 14:51:42 -05001744 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001745 return ret;
1746}
1747
1748int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1749{
1750 struct rb_node *node;
1751 struct extent_state *state;
1752 int ret = 0;
1753
Chris Masoncad321a2008-12-17 14:51:42 -05001754 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001755 /*
1756 * this search will find all the extents that end after
1757 * our range starts.
1758 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001759 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001760 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001761 ret = -ENOENT;
1762 goto out;
1763 }
1764 state = rb_entry(node, struct extent_state, rb_node);
1765 if (state->start != start) {
1766 ret = -ENOENT;
1767 goto out;
1768 }
1769 *private = state->private;
1770out:
Chris Masoncad321a2008-12-17 14:51:42 -05001771 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001772 return ret;
1773}
1774
1775/*
1776 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001777 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001778 * has the bits set. Otherwise, 1 is returned if any bit in the
1779 * range is found set.
1780 */
1781int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001782 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001783{
1784 struct extent_state *state = NULL;
1785 struct rb_node *node;
1786 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001787
Chris Masoncad321a2008-12-17 14:51:42 -05001788 spin_lock(&tree->lock);
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001789 if (cached && cached->tree && cached->start <= start &&
1790 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001791 node = &cached->rb_node;
1792 else
1793 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001794 while (node && start <= end) {
1795 state = rb_entry(node, struct extent_state, rb_node);
1796
1797 if (filled && state->start > start) {
1798 bitset = 0;
1799 break;
1800 }
1801
1802 if (state->start > end)
1803 break;
1804
1805 if (state->state & bits) {
1806 bitset = 1;
1807 if (!filled)
1808 break;
1809 } else if (filled) {
1810 bitset = 0;
1811 break;
1812 }
Chris Mason46562cec2009-09-23 20:23:16 -04001813
1814 if (state->end == (u64)-1)
1815 break;
1816
Chris Masond1310b22008-01-24 16:13:08 -05001817 start = state->end + 1;
1818 if (start > end)
1819 break;
1820 node = rb_next(node);
1821 if (!node) {
1822 if (filled)
1823 bitset = 0;
1824 break;
1825 }
1826 }
Chris Masoncad321a2008-12-17 14:51:42 -05001827 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001828 return bitset;
1829}
Chris Masond1310b22008-01-24 16:13:08 -05001830
1831/*
1832 * helper function to set a given page up to date if all the
1833 * extents in the tree for that page are up to date
1834 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001835static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001836{
1837 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1838 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001839 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001840 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001841}
1842
1843/*
1844 * helper function to unlock a page if all the extents in the tree
1845 * for that page are unlocked
1846 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001847static void check_page_locked(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001848{
1849 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1850 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001851 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001852 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05001853}
1854
1855/*
1856 * helper function to end page writeback if all the extents
1857 * in the tree for that page are done with writeback
1858 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001859static void check_page_writeback(struct extent_io_tree *tree,
1860 struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001861{
Chris Mason1edbb732009-09-02 13:24:36 -04001862 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001863}
1864
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001865/*
1866 * When IO fails, either with EIO or csum verification fails, we
1867 * try other mirrors that might have a good copy of the data. This
1868 * io_failure_record is used to record state as we go through all the
1869 * mirrors. If another mirror has good data, the page is set up to date
1870 * and things continue. If a good mirror can't be found, the original
1871 * bio end_io callback is called to indicate things have failed.
1872 */
1873struct io_failure_record {
1874 struct page *page;
1875 u64 start;
1876 u64 len;
1877 u64 logical;
1878 unsigned long bio_flags;
1879 int this_mirror;
1880 int failed_mirror;
1881 int in_validation;
1882};
1883
1884static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
1885 int did_repair)
1886{
1887 int ret;
1888 int err = 0;
1889 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1890
1891 set_state_private(failure_tree, rec->start, 0);
1892 ret = clear_extent_bits(failure_tree, rec->start,
1893 rec->start + rec->len - 1,
1894 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1895 if (ret)
1896 err = ret;
1897
David Woodhouse53b381b2013-01-29 18:40:14 -05001898 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1899 rec->start + rec->len - 1,
1900 EXTENT_DAMAGED, GFP_NOFS);
1901 if (ret && !err)
1902 err = ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001903
1904 kfree(rec);
1905 return err;
1906}
1907
1908static void repair_io_failure_callback(struct bio *bio, int err)
1909{
1910 complete(bio->bi_private);
1911}
1912
1913/*
1914 * this bypasses the standard btrfs submit functions deliberately, as
1915 * the standard behavior is to write all copies in a raid setup. here we only
1916 * want to write the one bad copy. so we do the mapping for ourselves and issue
1917 * submit_bio directly.
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001918 * to avoid any synchronization issues, wait for the data after writing, which
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001919 * actually prevents the read that triggered the error from finishing.
1920 * currently, there can be no more than two copies of every data bit. thus,
1921 * exactly one rewrite is required.
1922 */
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001923int repair_io_failure(struct btrfs_fs_info *fs_info, u64 start,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001924 u64 length, u64 logical, struct page *page,
1925 int mirror_num)
1926{
1927 struct bio *bio;
1928 struct btrfs_device *dev;
1929 DECLARE_COMPLETION_ONSTACK(compl);
1930 u64 map_length = 0;
1931 u64 sector;
1932 struct btrfs_bio *bbio = NULL;
David Woodhouse53b381b2013-01-29 18:40:14 -05001933 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001934 int ret;
1935
1936 BUG_ON(!mirror_num);
1937
David Woodhouse53b381b2013-01-29 18:40:14 -05001938 /* we can't repair anything in raid56 yet */
1939 if (btrfs_is_parity_mirror(map_tree, logical, length, mirror_num))
1940 return 0;
1941
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001942 bio = bio_alloc(GFP_NOFS, 1);
1943 if (!bio)
1944 return -EIO;
1945 bio->bi_private = &compl;
1946 bio->bi_end_io = repair_io_failure_callback;
1947 bio->bi_size = 0;
1948 map_length = length;
1949
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001950 ret = btrfs_map_block(fs_info, WRITE, logical,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001951 &map_length, &bbio, mirror_num);
1952 if (ret) {
1953 bio_put(bio);
1954 return -EIO;
1955 }
1956 BUG_ON(mirror_num != bbio->mirror_num);
1957 sector = bbio->stripes[mirror_num-1].physical >> 9;
1958 bio->bi_sector = sector;
1959 dev = bbio->stripes[mirror_num-1].dev;
1960 kfree(bbio);
1961 if (!dev || !dev->bdev || !dev->writeable) {
1962 bio_put(bio);
1963 return -EIO;
1964 }
1965 bio->bi_bdev = dev->bdev;
1966 bio_add_page(bio, page, length, start-page_offset(page));
Stefan Behrens21adbd52011-11-09 13:44:05 +01001967 btrfsic_submit_bio(WRITE_SYNC, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001968 wait_for_completion(&compl);
1969
1970 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
1971 /* try to remap that extent elsewhere? */
1972 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001973 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001974 return -EIO;
1975 }
1976
Anand Jaind5b025d2012-07-02 22:05:21 -06001977 printk_ratelimited_in_rcu(KERN_INFO "btrfs read error corrected: ino %lu off %llu "
Josef Bacik606686e2012-06-04 14:03:51 -04001978 "(dev %s sector %llu)\n", page->mapping->host->i_ino,
1979 start, rcu_str_deref(dev->name), sector);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001980
1981 bio_put(bio);
1982 return 0;
1983}
1984
Josef Bacikea466792012-03-26 21:57:36 -04001985int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
1986 int mirror_num)
1987{
Josef Bacikea466792012-03-26 21:57:36 -04001988 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);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001994 ret = repair_io_failure(root->fs_info, start, PAGE_CACHE_SIZE,
Josef Bacikea466792012-03-26 21:57:36 -04001995 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;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002013 struct btrfs_fs_info *fs_info;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002014 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) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002049 fs_info = BTRFS_I(inode)->root->fs_info;
2050 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2051 failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002052 if (num_copies > 1) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002053 ret = repair_io_failure(fs_info, start, failrec->len,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002054 failrec->logical, page,
2055 failrec->failed_mirror);
2056 did_repair = !ret;
2057 }
David Woodhouse53b381b2013-01-29 18:40:14 -05002058 ret = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002059 }
2060
2061out:
2062 if (!ret)
2063 ret = free_io_failure(inode, failrec, did_repair);
2064
2065 return ret;
2066}
2067
2068/*
2069 * this is a generic handler for readpage errors (default
2070 * readpage_io_failed_hook). if other copies exist, read those and write back
2071 * good data to the failed position. does not investigate in remapping the
2072 * failed extent elsewhere, hoping the device will be smart enough to do this as
2073 * needed
2074 */
2075
2076static int bio_readpage_error(struct bio *failed_bio, struct page *page,
2077 u64 start, u64 end, int failed_mirror,
2078 struct extent_state *state)
2079{
2080 struct io_failure_record *failrec = NULL;
2081 u64 private;
2082 struct extent_map *em;
2083 struct inode *inode = page->mapping->host;
2084 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2085 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2086 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2087 struct bio *bio;
2088 int num_copies;
2089 int ret;
2090 int read_mode;
2091 u64 logical;
2092
2093 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2094
2095 ret = get_state_private(failure_tree, start, &private);
2096 if (ret) {
2097 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2098 if (!failrec)
2099 return -ENOMEM;
2100 failrec->start = start;
2101 failrec->len = end - start + 1;
2102 failrec->this_mirror = 0;
2103 failrec->bio_flags = 0;
2104 failrec->in_validation = 0;
2105
2106 read_lock(&em_tree->lock);
2107 em = lookup_extent_mapping(em_tree, start, failrec->len);
2108 if (!em) {
2109 read_unlock(&em_tree->lock);
2110 kfree(failrec);
2111 return -EIO;
2112 }
2113
2114 if (em->start > start || em->start + em->len < start) {
2115 free_extent_map(em);
2116 em = NULL;
2117 }
2118 read_unlock(&em_tree->lock);
2119
Tsutomu Itoh7a2d6a62012-10-01 03:07:15 -06002120 if (!em) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002121 kfree(failrec);
2122 return -EIO;
2123 }
2124 logical = start - em->start;
2125 logical = em->block_start + logical;
2126 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2127 logical = em->block_start;
2128 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2129 extent_set_compress_type(&failrec->bio_flags,
2130 em->compress_type);
2131 }
2132 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2133 "len=%llu\n", logical, start, failrec->len);
2134 failrec->logical = logical;
2135 free_extent_map(em);
2136
2137 /* set the bits in the private failure tree */
2138 ret = set_extent_bits(failure_tree, start, end,
2139 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2140 if (ret >= 0)
2141 ret = set_state_private(failure_tree, start,
2142 (u64)(unsigned long)failrec);
2143 /* set the bits in the inode's tree */
2144 if (ret >= 0)
2145 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2146 GFP_NOFS);
2147 if (ret < 0) {
2148 kfree(failrec);
2149 return ret;
2150 }
2151 } else {
2152 failrec = (struct io_failure_record *)(unsigned long)private;
2153 pr_debug("bio_readpage_error: (found) logical=%llu, "
2154 "start=%llu, len=%llu, validation=%d\n",
2155 failrec->logical, failrec->start, failrec->len,
2156 failrec->in_validation);
2157 /*
2158 * when data can be on disk more than twice, add to failrec here
2159 * (e.g. with a list for failed_mirror) to make
2160 * clean_io_failure() clean all those errors at once.
2161 */
2162 }
Stefan Behrens5d964052012-11-05 14:59:07 +01002163 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
2164 failrec->logical, failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002165 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 Mahoney355808c2011-10-03 23:23:14 -04002469static int __must_check submit_one_bio(int rw, struct bio *bio,
2470 int mirror_num, unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002471{
Chris Masond1310b22008-01-24 16:13:08 -05002472 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002473 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2474 struct page *page = bvec->bv_page;
2475 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002476 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002477
2478 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002479
David Woodhouse902b22f2008-08-20 08:51:49 -04002480 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002481
2482 bio_get(bio);
2483
Chris Mason065631f2008-02-20 12:07:25 -05002484 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00002485 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002486 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002487 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01002488 btrfsic_submit_bio(rw, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002489
Chris Masond1310b22008-01-24 16:13:08 -05002490 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2491 ret = -EOPNOTSUPP;
2492 bio_put(bio);
2493 return ret;
2494}
2495
David Woodhouse64a16702009-07-15 23:29:37 +01002496static int merge_bio(int rw, struct extent_io_tree *tree, struct page *page,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002497 unsigned long offset, size_t size, struct bio *bio,
2498 unsigned long bio_flags)
2499{
2500 int ret = 0;
2501 if (tree->ops && tree->ops->merge_bio_hook)
David Woodhouse64a16702009-07-15 23:29:37 +01002502 ret = tree->ops->merge_bio_hook(rw, page, offset, size, bio,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002503 bio_flags);
2504 BUG_ON(ret < 0);
2505 return ret;
2506
2507}
2508
Chris Masond1310b22008-01-24 16:13:08 -05002509static int submit_extent_page(int rw, struct extent_io_tree *tree,
2510 struct page *page, sector_t sector,
2511 size_t size, unsigned long offset,
2512 struct block_device *bdev,
2513 struct bio **bio_ret,
2514 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04002515 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002516 int mirror_num,
2517 unsigned long prev_bio_flags,
2518 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002519{
2520 int ret = 0;
2521 struct bio *bio;
2522 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04002523 int contig = 0;
2524 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2525 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05002526 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002527
2528 if (bio_ret && *bio_ret) {
2529 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002530 if (old_compressed)
2531 contig = bio->bi_sector == sector;
2532 else
2533 contig = bio->bi_sector + (bio->bi_size >> 9) ==
2534 sector;
2535
2536 if (prev_bio_flags != bio_flags || !contig ||
David Woodhouse64a16702009-07-15 23:29:37 +01002537 merge_bio(rw, tree, page, offset, page_size, bio, bio_flags) ||
Chris Masonc8b97812008-10-29 14:49:59 -04002538 bio_add_page(bio, page, page_size, offset) < page_size) {
2539 ret = submit_one_bio(rw, bio, mirror_num,
2540 prev_bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002541 if (ret < 0)
2542 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05002543 bio = NULL;
2544 } else {
2545 return 0;
2546 }
2547 }
Chris Masonc8b97812008-10-29 14:49:59 -04002548 if (this_compressed)
2549 nr = BIO_MAX_PAGES;
2550 else
2551 nr = bio_get_nr_vecs(bdev);
2552
Miao Xie88f794e2010-11-22 03:02:55 +00002553 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002554 if (!bio)
2555 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002556
Chris Masonc8b97812008-10-29 14:49:59 -04002557 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002558 bio->bi_end_io = end_io_func;
2559 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05002560
Chris Masond3977122009-01-05 21:25:51 -05002561 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002562 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002563 else
Chris Masonc8b97812008-10-29 14:49:59 -04002564 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002565
2566 return ret;
2567}
2568
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002569void attach_extent_buffer_page(struct extent_buffer *eb, struct page *page)
2570{
2571 if (!PagePrivate(page)) {
2572 SetPagePrivate(page);
2573 page_cache_get(page);
2574 set_page_private(page, (unsigned long)eb);
2575 } else {
2576 WARN_ON(page->private != (unsigned long)eb);
2577 }
2578}
2579
Chris Masond1310b22008-01-24 16:13:08 -05002580void set_page_extent_mapped(struct page *page)
2581{
2582 if (!PagePrivate(page)) {
2583 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002584 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002585 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002586 }
2587}
2588
Chris Masond1310b22008-01-24 16:13:08 -05002589/*
2590 * basic readpage implementation. Locked extent state structs are inserted
2591 * into the tree that are removed when the IO is done (by the end_io
2592 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002593 * XXX JDM: This needs looking at to ensure proper page locking
Chris Masond1310b22008-01-24 16:13:08 -05002594 */
2595static int __extent_read_full_page(struct extent_io_tree *tree,
2596 struct page *page,
2597 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002598 struct bio **bio, int mirror_num,
2599 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002600{
2601 struct inode *inode = page->mapping->host;
2602 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2603 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2604 u64 end;
2605 u64 cur = start;
2606 u64 extent_offset;
2607 u64 last_byte = i_size_read(inode);
2608 u64 block_start;
2609 u64 cur_end;
2610 sector_t sector;
2611 struct extent_map *em;
2612 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002613 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05002614 int ret;
2615 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002616 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002617 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002618 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002619 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002620 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002621
2622 set_page_extent_mapped(page);
2623
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002624 if (!PageUptodate(page)) {
2625 if (cleancache_get_page(page) == 0) {
2626 BUG_ON(blocksize != PAGE_SIZE);
2627 goto out;
2628 }
2629 }
2630
Chris Masond1310b22008-01-24 16:13:08 -05002631 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002632 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002633 lock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002634 ordered = btrfs_lookup_ordered_extent(inode, start);
2635 if (!ordered)
2636 break;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002637 unlock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002638 btrfs_start_ordered_extent(inode, ordered, 1);
2639 btrfs_put_ordered_extent(ordered);
2640 }
Chris Masond1310b22008-01-24 16:13:08 -05002641
Chris Masonc8b97812008-10-29 14:49:59 -04002642 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2643 char *userpage;
2644 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2645
2646 if (zero_offset) {
2647 iosize = PAGE_CACHE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002648 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002649 memset(userpage + zero_offset, 0, iosize);
2650 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002651 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002652 }
2653 }
Chris Masond1310b22008-01-24 16:13:08 -05002654 while (cur <= end) {
2655 if (cur >= last_byte) {
2656 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002657 struct extent_state *cached = NULL;
2658
David Sterba306e16c2011-04-19 14:29:38 +02002659 iosize = PAGE_CACHE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002660 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002661 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002662 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002663 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002664 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002665 &cached, GFP_NOFS);
2666 unlock_extent_cached(tree, cur, cur + iosize - 1,
2667 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002668 break;
2669 }
David Sterba306e16c2011-04-19 14:29:38 +02002670 em = get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002671 end - cur + 1, 0);
David Sterbac7040052011-04-19 18:00:01 +02002672 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002673 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002674 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002675 break;
2676 }
Chris Masond1310b22008-01-24 16:13:08 -05002677 extent_offset = cur - em->start;
2678 BUG_ON(extent_map_end(em) <= cur);
2679 BUG_ON(end < cur);
2680
Li Zefan261507a02010-12-17 14:21:50 +08002681 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002682 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002683 extent_set_compress_type(&this_bio_flag,
2684 em->compress_type);
2685 }
Chris Masonc8b97812008-10-29 14:49:59 -04002686
Chris Masond1310b22008-01-24 16:13:08 -05002687 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2688 cur_end = min(extent_map_end(em) - 1, end);
2689 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002690 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2691 disk_io_size = em->block_len;
2692 sector = em->block_start >> 9;
2693 } else {
2694 sector = (em->block_start + extent_offset) >> 9;
2695 disk_io_size = iosize;
2696 }
Chris Masond1310b22008-01-24 16:13:08 -05002697 bdev = em->bdev;
2698 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002699 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2700 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002701 free_extent_map(em);
2702 em = NULL;
2703
2704 /* we've found a hole, just zero and go on */
2705 if (block_start == EXTENT_MAP_HOLE) {
2706 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002707 struct extent_state *cached = NULL;
2708
Cong Wang7ac687d2011-11-25 23:14:28 +08002709 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002710 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002711 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002712 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002713
2714 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002715 &cached, GFP_NOFS);
2716 unlock_extent_cached(tree, cur, cur + iosize - 1,
2717 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002718 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002719 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002720 continue;
2721 }
2722 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002723 if (test_range_bit(tree, cur, cur_end,
2724 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002725 check_page_uptodate(tree, page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002726 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002727 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002728 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002729 continue;
2730 }
Chris Mason70dec802008-01-29 09:59:12 -05002731 /* we have an inline extent but it didn't get marked up
2732 * to date. Error out
2733 */
2734 if (block_start == EXTENT_MAP_INLINE) {
2735 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002736 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05002737 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002738 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05002739 continue;
2740 }
Chris Masond1310b22008-01-24 16:13:08 -05002741
2742 ret = 0;
2743 if (tree->ops && tree->ops->readpage_io_hook) {
2744 ret = tree->ops->readpage_io_hook(page, cur,
2745 cur + iosize - 1);
2746 }
2747 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002748 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2749 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002750 ret = submit_extent_page(READ, tree, page,
David Sterba306e16c2011-04-19 14:29:38 +02002751 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04002752 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002753 end_bio_extent_readpage, mirror_num,
2754 *bio_flags,
2755 this_bio_flag);
Josef Bacikedd33c92012-10-05 16:40:32 -04002756 if (!ret) {
2757 nr++;
2758 *bio_flags = this_bio_flag;
2759 }
Chris Masond1310b22008-01-24 16:13:08 -05002760 }
Josef Bacikedd33c92012-10-05 16:40:32 -04002761 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002762 SetPageError(page);
Josef Bacikedd33c92012-10-05 16:40:32 -04002763 unlock_extent(tree, cur, cur + iosize - 1);
2764 }
Chris Masond1310b22008-01-24 16:13:08 -05002765 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002766 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002767 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002768out:
Chris Masond1310b22008-01-24 16:13:08 -05002769 if (!nr) {
2770 if (!PageError(page))
2771 SetPageUptodate(page);
2772 unlock_page(page);
2773 }
2774 return 0;
2775}
2776
2777int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002778 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05002779{
2780 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002781 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002782 int ret;
2783
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002784 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Chris Masonc8b97812008-10-29 14:49:59 -04002785 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002786 if (bio)
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002787 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002788 return ret;
2789}
Chris Masond1310b22008-01-24 16:13:08 -05002790
Chris Mason11c83492009-04-20 15:50:09 -04002791static noinline void update_nr_written(struct page *page,
2792 struct writeback_control *wbc,
2793 unsigned long nr_written)
2794{
2795 wbc->nr_to_write -= nr_written;
2796 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2797 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2798 page->mapping->writeback_index = page->index + nr_written;
2799}
2800
Chris Masond1310b22008-01-24 16:13:08 -05002801/*
2802 * the writepage semantics are similar to regular writepage. extent
2803 * records are inserted to lock ranges in the tree, and as dirty areas
2804 * are found, they are marked writeback. Then the lock bits are removed
2805 * and the end_io handler clears the writeback ranges
2806 */
2807static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2808 void *data)
2809{
2810 struct inode *inode = page->mapping->host;
2811 struct extent_page_data *epd = data;
2812 struct extent_io_tree *tree = epd->tree;
2813 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2814 u64 delalloc_start;
2815 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2816 u64 end;
2817 u64 cur = start;
2818 u64 extent_offset;
2819 u64 last_byte = i_size_read(inode);
2820 u64 block_start;
2821 u64 iosize;
2822 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002823 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002824 struct extent_map *em;
2825 struct block_device *bdev;
2826 int ret;
2827 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002828 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002829 size_t blocksize;
2830 loff_t i_size = i_size_read(inode);
2831 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2832 u64 nr_delalloc;
2833 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002834 int page_started;
2835 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002836 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002837 unsigned long nr_written = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002838 bool fill_delalloc = true;
Chris Masond1310b22008-01-24 16:13:08 -05002839
Chris Masonffbd5172009-04-20 15:50:09 -04002840 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002841 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002842 else
2843 write_flags = WRITE;
2844
liubo1abe9b82011-03-24 11:18:59 +00002845 trace___extent_writepage(page, inode, wbc);
2846
Chris Masond1310b22008-01-24 16:13:08 -05002847 WARN_ON(!PageLocked(page));
Chris Masonbf0da8c2011-11-04 12:29:37 -04002848
2849 ClearPageError(page);
2850
Chris Mason7f3c74f2008-07-18 12:01:11 -04002851 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002852 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002853 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002854 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002855 unlock_page(page);
2856 return 0;
2857 }
2858
2859 if (page->index == end_index) {
2860 char *userpage;
2861
Cong Wang7ac687d2011-11-25 23:14:28 +08002862 userpage = kmap_atomic(page);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002863 memset(userpage + pg_offset, 0,
2864 PAGE_CACHE_SIZE - pg_offset);
Cong Wang7ac687d2011-11-25 23:14:28 +08002865 kunmap_atomic(userpage);
Chris Mason211c17f2008-05-15 09:13:45 -04002866 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002867 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002868 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002869
2870 set_page_extent_mapped(page);
2871
Josef Bacik9e487102011-08-01 12:08:18 -04002872 if (!tree->ops || !tree->ops->fill_delalloc)
2873 fill_delalloc = false;
2874
Chris Masond1310b22008-01-24 16:13:08 -05002875 delalloc_start = start;
2876 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002877 page_started = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002878 if (!epd->extent_locked && fill_delalloc) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002879 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002880 /*
2881 * make sure the wbc mapping index is at least updated
2882 * to this page.
2883 */
2884 update_nr_written(page, wbc, 0);
2885
Chris Masond3977122009-01-05 21:25:51 -05002886 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002887 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002888 page,
2889 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002890 &delalloc_end,
2891 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002892 if (nr_delalloc == 0) {
2893 delalloc_start = delalloc_end + 1;
2894 continue;
2895 }
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002896 ret = tree->ops->fill_delalloc(inode, page,
2897 delalloc_start,
2898 delalloc_end,
2899 &page_started,
2900 &nr_written);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002901 /* File system has been set read-only */
2902 if (ret) {
2903 SetPageError(page);
2904 goto done;
2905 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002906 /*
2907 * delalloc_end is already one less than the total
2908 * length, so we don't subtract one from
2909 * PAGE_CACHE_SIZE
2910 */
2911 delalloc_to_write += (delalloc_end - delalloc_start +
2912 PAGE_CACHE_SIZE) >>
2913 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002914 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002915 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002916 if (wbc->nr_to_write < delalloc_to_write) {
2917 int thresh = 8192;
2918
2919 if (delalloc_to_write < thresh * 2)
2920 thresh = delalloc_to_write;
2921 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2922 thresh);
2923 }
Chris Masonc8b97812008-10-29 14:49:59 -04002924
Chris Mason771ed682008-11-06 22:02:51 -05002925 /* did the fill delalloc function already unlock and start
2926 * the IO?
2927 */
2928 if (page_started) {
2929 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002930 /*
2931 * we've unlocked the page, so we can't update
2932 * the mapping's writeback index, just update
2933 * nr_to_write.
2934 */
2935 wbc->nr_to_write -= nr_written;
2936 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002937 }
Chris Masonc8b97812008-10-29 14:49:59 -04002938 }
Chris Mason247e7432008-07-17 12:53:51 -04002939 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002940 ret = tree->ops->writepage_start_hook(page, start,
2941 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002942 if (ret) {
2943 /* Fixup worker will requeue */
2944 if (ret == -EBUSY)
2945 wbc->pages_skipped++;
2946 else
2947 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002948 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002949 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002950 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002951 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002952 }
2953 }
2954
Chris Mason11c83492009-04-20 15:50:09 -04002955 /*
2956 * we don't want to touch the inode after unlocking the page,
2957 * so we update the mapping writeback index now
2958 */
2959 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002960
Chris Masond1310b22008-01-24 16:13:08 -05002961 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002962 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002963 if (tree->ops && tree->ops->writepage_end_io_hook)
2964 tree->ops->writepage_end_io_hook(page, start,
2965 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002966 goto done;
2967 }
2968
Chris Masond1310b22008-01-24 16:13:08 -05002969 blocksize = inode->i_sb->s_blocksize;
2970
2971 while (cur <= end) {
2972 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002973 if (tree->ops && tree->ops->writepage_end_io_hook)
2974 tree->ops->writepage_end_io_hook(page, cur,
2975 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002976 break;
2977 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002978 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002979 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02002980 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002981 SetPageError(page);
2982 break;
2983 }
2984
2985 extent_offset = cur - em->start;
2986 BUG_ON(extent_map_end(em) <= cur);
2987 BUG_ON(end < cur);
2988 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2989 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2990 sector = (em->block_start + extent_offset) >> 9;
2991 bdev = em->bdev;
2992 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002993 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002994 free_extent_map(em);
2995 em = NULL;
2996
Chris Masonc8b97812008-10-29 14:49:59 -04002997 /*
2998 * compressed and inline extents are written through other
2999 * paths in the FS
3000 */
3001 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003002 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003003 /*
3004 * end_io notification does not happen here for
3005 * compressed extents
3006 */
3007 if (!compressed && tree->ops &&
3008 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003009 tree->ops->writepage_end_io_hook(page, cur,
3010 cur + iosize - 1,
3011 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003012 else if (compressed) {
3013 /* we don't want to end_page_writeback on
3014 * a compressed extent. this happens
3015 * elsewhere
3016 */
3017 nr++;
3018 }
3019
3020 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003021 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003022 continue;
3023 }
Chris Masond1310b22008-01-24 16:13:08 -05003024 /* leave this out until we have a page_mkwrite call */
3025 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04003026 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05003027 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003028 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003029 continue;
3030 }
Chris Masonc8b97812008-10-29 14:49:59 -04003031
Chris Masond1310b22008-01-24 16:13:08 -05003032 if (tree->ops && tree->ops->writepage_io_hook) {
3033 ret = tree->ops->writepage_io_hook(page, cur,
3034 cur + iosize - 1);
3035 } else {
3036 ret = 0;
3037 }
Chris Mason1259ab72008-05-12 13:39:03 -04003038 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003039 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003040 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003041 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003042
Chris Masond1310b22008-01-24 16:13:08 -05003043 set_range_writeback(tree, cur, cur + iosize - 1);
3044 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05003045 printk(KERN_ERR "btrfs warning page %lu not "
3046 "writeback, cur %llu end %llu\n",
3047 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05003048 (unsigned long long)end);
3049 }
3050
Chris Masonffbd5172009-04-20 15:50:09 -04003051 ret = submit_extent_page(write_flags, tree, page,
3052 sector, iosize, pg_offset,
3053 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04003054 end_bio_extent_writepage,
3055 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003056 if (ret)
3057 SetPageError(page);
3058 }
3059 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003060 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003061 nr++;
3062 }
3063done:
3064 if (nr == 0) {
3065 /* make sure the mapping tag for page dirty gets cleared */
3066 set_page_writeback(page);
3067 end_page_writeback(page);
3068 }
Chris Masond1310b22008-01-24 16:13:08 -05003069 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05003070
Chris Mason11c83492009-04-20 15:50:09 -04003071done_unlocked:
3072
Chris Mason2c64c532009-09-02 15:04:12 -04003073 /* drop our reference on any cached states */
3074 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05003075 return 0;
3076}
3077
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003078static int eb_wait(void *word)
3079{
3080 io_schedule();
3081 return 0;
3082}
3083
3084static void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
3085{
3086 wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
3087 TASK_UNINTERRUPTIBLE);
3088}
3089
3090static int lock_extent_buffer_for_io(struct extent_buffer *eb,
3091 struct btrfs_fs_info *fs_info,
3092 struct extent_page_data *epd)
3093{
3094 unsigned long i, num_pages;
3095 int flush = 0;
3096 int ret = 0;
3097
3098 if (!btrfs_try_tree_write_lock(eb)) {
3099 flush = 1;
3100 flush_write_bio(epd);
3101 btrfs_tree_lock(eb);
3102 }
3103
3104 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3105 btrfs_tree_unlock(eb);
3106 if (!epd->sync_io)
3107 return 0;
3108 if (!flush) {
3109 flush_write_bio(epd);
3110 flush = 1;
3111 }
Chris Masona098d8e2012-03-21 12:09:56 -04003112 while (1) {
3113 wait_on_extent_buffer_writeback(eb);
3114 btrfs_tree_lock(eb);
3115 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3116 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003117 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003118 }
3119 }
3120
Josef Bacik51561ff2012-07-20 16:25:24 -04003121 /*
3122 * We need to do this to prevent races in people who check if the eb is
3123 * under IO since we can end up having no IO bits set for a short period
3124 * of time.
3125 */
3126 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003127 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3128 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003129 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003130 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
3131 spin_lock(&fs_info->delalloc_lock);
3132 if (fs_info->dirty_metadata_bytes >= eb->len)
3133 fs_info->dirty_metadata_bytes -= eb->len;
3134 else
3135 WARN_ON(1);
3136 spin_unlock(&fs_info->delalloc_lock);
3137 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003138 } else {
3139 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003140 }
3141
3142 btrfs_tree_unlock(eb);
3143
3144 if (!ret)
3145 return ret;
3146
3147 num_pages = num_extent_pages(eb->start, eb->len);
3148 for (i = 0; i < num_pages; i++) {
3149 struct page *p = extent_buffer_page(eb, i);
3150
3151 if (!trylock_page(p)) {
3152 if (!flush) {
3153 flush_write_bio(epd);
3154 flush = 1;
3155 }
3156 lock_page(p);
3157 }
3158 }
3159
3160 return ret;
3161}
3162
3163static void end_extent_buffer_writeback(struct extent_buffer *eb)
3164{
3165 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3166 smp_mb__after_clear_bit();
3167 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3168}
3169
3170static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
3171{
3172 int uptodate = err == 0;
3173 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
3174 struct extent_buffer *eb;
3175 int done;
3176
3177 do {
3178 struct page *page = bvec->bv_page;
3179
3180 bvec--;
3181 eb = (struct extent_buffer *)page->private;
3182 BUG_ON(!eb);
3183 done = atomic_dec_and_test(&eb->io_pages);
3184
3185 if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
3186 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3187 ClearPageUptodate(page);
3188 SetPageError(page);
3189 }
3190
3191 end_page_writeback(page);
3192
3193 if (!done)
3194 continue;
3195
3196 end_extent_buffer_writeback(eb);
3197 } while (bvec >= bio->bi_io_vec);
3198
3199 bio_put(bio);
3200
3201}
3202
3203static int write_one_eb(struct extent_buffer *eb,
3204 struct btrfs_fs_info *fs_info,
3205 struct writeback_control *wbc,
3206 struct extent_page_data *epd)
3207{
3208 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
3209 u64 offset = eb->start;
3210 unsigned long i, num_pages;
Josef Bacikde0022b2012-09-25 14:25:58 -04003211 unsigned long bio_flags = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003212 int rw = (epd->sync_io ? WRITE_SYNC : WRITE);
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003213 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003214
3215 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3216 num_pages = num_extent_pages(eb->start, eb->len);
3217 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003218 if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
3219 bio_flags = EXTENT_BIO_TREE_LOG;
3220
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003221 for (i = 0; i < num_pages; i++) {
3222 struct page *p = extent_buffer_page(eb, i);
3223
3224 clear_page_dirty_for_io(p);
3225 set_page_writeback(p);
3226 ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
3227 PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
3228 -1, end_bio_extent_buffer_writepage,
Josef Bacikde0022b2012-09-25 14:25:58 -04003229 0, epd->bio_flags, bio_flags);
3230 epd->bio_flags = bio_flags;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003231 if (ret) {
3232 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3233 SetPageError(p);
3234 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3235 end_extent_buffer_writeback(eb);
3236 ret = -EIO;
3237 break;
3238 }
3239 offset += PAGE_CACHE_SIZE;
3240 update_nr_written(p, wbc, 1);
3241 unlock_page(p);
3242 }
3243
3244 if (unlikely(ret)) {
3245 for (; i < num_pages; i++) {
3246 struct page *p = extent_buffer_page(eb, i);
3247 unlock_page(p);
3248 }
3249 }
3250
3251 return ret;
3252}
3253
3254int btree_write_cache_pages(struct address_space *mapping,
3255 struct writeback_control *wbc)
3256{
3257 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3258 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3259 struct extent_buffer *eb, *prev_eb = NULL;
3260 struct extent_page_data epd = {
3261 .bio = NULL,
3262 .tree = tree,
3263 .extent_locked = 0,
3264 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003265 .bio_flags = 0,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003266 };
3267 int ret = 0;
3268 int done = 0;
3269 int nr_to_write_done = 0;
3270 struct pagevec pvec;
3271 int nr_pages;
3272 pgoff_t index;
3273 pgoff_t end; /* Inclusive */
3274 int scanned = 0;
3275 int tag;
3276
3277 pagevec_init(&pvec, 0);
3278 if (wbc->range_cyclic) {
3279 index = mapping->writeback_index; /* Start from prev offset */
3280 end = -1;
3281 } else {
3282 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3283 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3284 scanned = 1;
3285 }
3286 if (wbc->sync_mode == WB_SYNC_ALL)
3287 tag = PAGECACHE_TAG_TOWRITE;
3288 else
3289 tag = PAGECACHE_TAG_DIRTY;
3290retry:
3291 if (wbc->sync_mode == WB_SYNC_ALL)
3292 tag_pages_for_writeback(mapping, index, end);
3293 while (!done && !nr_to_write_done && (index <= end) &&
3294 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3295 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3296 unsigned i;
3297
3298 scanned = 1;
3299 for (i = 0; i < nr_pages; i++) {
3300 struct page *page = pvec.pages[i];
3301
3302 if (!PagePrivate(page))
3303 continue;
3304
3305 if (!wbc->range_cyclic && page->index > end) {
3306 done = 1;
3307 break;
3308 }
3309
Josef Bacikb5bae262012-09-14 13:43:01 -04003310 spin_lock(&mapping->private_lock);
3311 if (!PagePrivate(page)) {
3312 spin_unlock(&mapping->private_lock);
3313 continue;
3314 }
3315
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003316 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003317
3318 /*
3319 * Shouldn't happen and normally this would be a BUG_ON
3320 * but no sense in crashing the users box for something
3321 * we can survive anyway.
3322 */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003323 if (!eb) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003324 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003325 WARN_ON(1);
3326 continue;
3327 }
3328
Josef Bacikb5bae262012-09-14 13:43:01 -04003329 if (eb == prev_eb) {
3330 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003331 continue;
3332 }
3333
Josef Bacikb5bae262012-09-14 13:43:01 -04003334 ret = atomic_inc_not_zero(&eb->refs);
3335 spin_unlock(&mapping->private_lock);
3336 if (!ret)
3337 continue;
3338
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003339 prev_eb = eb;
3340 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3341 if (!ret) {
3342 free_extent_buffer(eb);
3343 continue;
3344 }
3345
3346 ret = write_one_eb(eb, fs_info, wbc, &epd);
3347 if (ret) {
3348 done = 1;
3349 free_extent_buffer(eb);
3350 break;
3351 }
3352 free_extent_buffer(eb);
3353
3354 /*
3355 * the filesystem may choose to bump up nr_to_write.
3356 * We have to make sure to honor the new nr_to_write
3357 * at any time
3358 */
3359 nr_to_write_done = wbc->nr_to_write <= 0;
3360 }
3361 pagevec_release(&pvec);
3362 cond_resched();
3363 }
3364 if (!scanned && !done) {
3365 /*
3366 * We hit the last page and there is more work to be done: wrap
3367 * back to the start of the file
3368 */
3369 scanned = 1;
3370 index = 0;
3371 goto retry;
3372 }
3373 flush_write_bio(&epd);
3374 return ret;
3375}
3376
Chris Masond1310b22008-01-24 16:13:08 -05003377/**
Chris Mason4bef0842008-09-08 11:18:08 -04003378 * 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 -05003379 * @mapping: address space structure to write
3380 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3381 * @writepage: function called for each page
3382 * @data: data passed to writepage function
3383 *
3384 * If a page is already under I/O, write_cache_pages() skips it, even
3385 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3386 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3387 * and msync() need to guarantee that all the data which was dirty at the time
3388 * the call was made get new I/O started against them. If wbc->sync_mode is
3389 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3390 * existing IO to complete.
3391 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003392static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04003393 struct address_space *mapping,
3394 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003395 writepage_t writepage, void *data,
3396 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05003397{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003398 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003399 int ret = 0;
3400 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003401 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003402 struct pagevec pvec;
3403 int nr_pages;
3404 pgoff_t index;
3405 pgoff_t end; /* Inclusive */
3406 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003407 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003408
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003409 /*
3410 * We have to hold onto the inode so that ordered extents can do their
3411 * work when the IO finishes. The alternative to this is failing to add
3412 * an ordered extent if the igrab() fails there and that is a huge pain
3413 * to deal with, so instead just hold onto the inode throughout the
3414 * writepages operation. If it fails here we are freeing up the inode
3415 * anyway and we'd rather not waste our time writing out stuff that is
3416 * going to be truncated anyway.
3417 */
3418 if (!igrab(inode))
3419 return 0;
3420
Chris Masond1310b22008-01-24 16:13:08 -05003421 pagevec_init(&pvec, 0);
3422 if (wbc->range_cyclic) {
3423 index = mapping->writeback_index; /* Start from prev offset */
3424 end = -1;
3425 } else {
3426 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3427 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003428 scanned = 1;
3429 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003430 if (wbc->sync_mode == WB_SYNC_ALL)
3431 tag = PAGECACHE_TAG_TOWRITE;
3432 else
3433 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003434retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003435 if (wbc->sync_mode == WB_SYNC_ALL)
3436 tag_pages_for_writeback(mapping, index, end);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003437 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003438 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3439 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003440 unsigned i;
3441
3442 scanned = 1;
3443 for (i = 0; i < nr_pages; i++) {
3444 struct page *page = pvec.pages[i];
3445
3446 /*
3447 * At this point we hold neither mapping->tree_lock nor
3448 * lock on the page itself: the page may be truncated or
3449 * invalidated (changing page->mapping to NULL), or even
3450 * swizzled back from swapper_space to tmpfs file
3451 * mapping
3452 */
Chris Mason01d658f2011-11-01 10:08:06 -04003453 if (tree->ops &&
3454 tree->ops->write_cache_pages_lock_hook) {
3455 tree->ops->write_cache_pages_lock_hook(page,
3456 data, flush_fn);
3457 } else {
3458 if (!trylock_page(page)) {
3459 flush_fn(data);
3460 lock_page(page);
3461 }
3462 }
Chris Masond1310b22008-01-24 16:13:08 -05003463
3464 if (unlikely(page->mapping != mapping)) {
3465 unlock_page(page);
3466 continue;
3467 }
3468
3469 if (!wbc->range_cyclic && page->index > end) {
3470 done = 1;
3471 unlock_page(page);
3472 continue;
3473 }
3474
Chris Masond2c3f4f2008-11-19 12:44:22 -05003475 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003476 if (PageWriteback(page))
3477 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05003478 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003479 }
Chris Masond1310b22008-01-24 16:13:08 -05003480
3481 if (PageWriteback(page) ||
3482 !clear_page_dirty_for_io(page)) {
3483 unlock_page(page);
3484 continue;
3485 }
3486
3487 ret = (*writepage)(page, wbc, data);
3488
3489 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3490 unlock_page(page);
3491 ret = 0;
3492 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003493 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05003494 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003495
3496 /*
3497 * the filesystem may choose to bump up nr_to_write.
3498 * We have to make sure to honor the new nr_to_write
3499 * at any time
3500 */
3501 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05003502 }
3503 pagevec_release(&pvec);
3504 cond_resched();
3505 }
3506 if (!scanned && !done) {
3507 /*
3508 * We hit the last page and there is more work to be done: wrap
3509 * back to the start of the file
3510 */
3511 scanned = 1;
3512 index = 0;
3513 goto retry;
3514 }
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003515 btrfs_add_delayed_iput(inode);
Chris Masond1310b22008-01-24 16:13:08 -05003516 return ret;
3517}
Chris Masond1310b22008-01-24 16:13:08 -05003518
Chris Masonffbd5172009-04-20 15:50:09 -04003519static void flush_epd_write_bio(struct extent_page_data *epd)
3520{
3521 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04003522 int rw = WRITE;
3523 int ret;
3524
Chris Masonffbd5172009-04-20 15:50:09 -04003525 if (epd->sync_io)
Jeff Mahoney355808c2011-10-03 23:23:14 -04003526 rw = WRITE_SYNC;
3527
Josef Bacikde0022b2012-09-25 14:25:58 -04003528 ret = submit_one_bio(rw, epd->bio, 0, epd->bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003529 BUG_ON(ret < 0); /* -ENOMEM */
Chris Masonffbd5172009-04-20 15:50:09 -04003530 epd->bio = NULL;
3531 }
3532}
3533
Chris Masond2c3f4f2008-11-19 12:44:22 -05003534static noinline void flush_write_bio(void *data)
3535{
3536 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04003537 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003538}
3539
Chris Masond1310b22008-01-24 16:13:08 -05003540int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3541 get_extent_t *get_extent,
3542 struct writeback_control *wbc)
3543{
3544 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003545 struct extent_page_data epd = {
3546 .bio = NULL,
3547 .tree = tree,
3548 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003549 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003550 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003551 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05003552 };
Chris Masond1310b22008-01-24 16:13:08 -05003553
Chris Masond1310b22008-01-24 16:13:08 -05003554 ret = __extent_writepage(page, wbc, &epd);
3555
Chris Masonffbd5172009-04-20 15:50:09 -04003556 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003557 return ret;
3558}
Chris Masond1310b22008-01-24 16:13:08 -05003559
Chris Mason771ed682008-11-06 22:02:51 -05003560int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3561 u64 start, u64 end, get_extent_t *get_extent,
3562 int mode)
3563{
3564 int ret = 0;
3565 struct address_space *mapping = inode->i_mapping;
3566 struct page *page;
3567 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3568 PAGE_CACHE_SHIFT;
3569
3570 struct extent_page_data epd = {
3571 .bio = NULL,
3572 .tree = tree,
3573 .get_extent = get_extent,
3574 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04003575 .sync_io = mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003576 .bio_flags = 0,
Chris Mason771ed682008-11-06 22:02:51 -05003577 };
3578 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05003579 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05003580 .nr_to_write = nr_pages * 2,
3581 .range_start = start,
3582 .range_end = end + 1,
3583 };
3584
Chris Masond3977122009-01-05 21:25:51 -05003585 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05003586 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3587 if (clear_page_dirty_for_io(page))
3588 ret = __extent_writepage(page, &wbc_writepages, &epd);
3589 else {
3590 if (tree->ops && tree->ops->writepage_end_io_hook)
3591 tree->ops->writepage_end_io_hook(page, start,
3592 start + PAGE_CACHE_SIZE - 1,
3593 NULL, 1);
3594 unlock_page(page);
3595 }
3596 page_cache_release(page);
3597 start += PAGE_CACHE_SIZE;
3598 }
3599
Chris Masonffbd5172009-04-20 15:50:09 -04003600 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05003601 return ret;
3602}
Chris Masond1310b22008-01-24 16:13:08 -05003603
3604int extent_writepages(struct extent_io_tree *tree,
3605 struct address_space *mapping,
3606 get_extent_t *get_extent,
3607 struct writeback_control *wbc)
3608{
3609 int ret = 0;
3610 struct extent_page_data epd = {
3611 .bio = NULL,
3612 .tree = tree,
3613 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003614 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003615 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003616 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05003617 };
3618
Chris Mason4bef0842008-09-08 11:18:08 -04003619 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003620 __extent_writepage, &epd,
3621 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04003622 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003623 return ret;
3624}
Chris Masond1310b22008-01-24 16:13:08 -05003625
3626int extent_readpages(struct extent_io_tree *tree,
3627 struct address_space *mapping,
3628 struct list_head *pages, unsigned nr_pages,
3629 get_extent_t get_extent)
3630{
3631 struct bio *bio = NULL;
3632 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04003633 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06003634 struct page *pagepool[16];
3635 struct page *page;
3636 int i = 0;
3637 int nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003638
Chris Masond1310b22008-01-24 16:13:08 -05003639 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
Liu Bo67c96842012-07-20 21:43:09 -06003640 page = list_entry(pages->prev, struct page, lru);
Chris Masond1310b22008-01-24 16:13:08 -05003641
3642 prefetchw(&page->flags);
3643 list_del(&page->lru);
Liu Bo67c96842012-07-20 21:43:09 -06003644 if (add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04003645 page->index, GFP_NOFS)) {
Liu Bo67c96842012-07-20 21:43:09 -06003646 page_cache_release(page);
3647 continue;
Chris Masond1310b22008-01-24 16:13:08 -05003648 }
Liu Bo67c96842012-07-20 21:43:09 -06003649
3650 pagepool[nr++] = page;
3651 if (nr < ARRAY_SIZE(pagepool))
3652 continue;
3653 for (i = 0; i < nr; i++) {
3654 __extent_read_full_page(tree, pagepool[i], get_extent,
3655 &bio, 0, &bio_flags);
3656 page_cache_release(pagepool[i]);
3657 }
3658 nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003659 }
Liu Bo67c96842012-07-20 21:43:09 -06003660 for (i = 0; i < nr; i++) {
3661 __extent_read_full_page(tree, pagepool[i], get_extent,
3662 &bio, 0, &bio_flags);
3663 page_cache_release(pagepool[i]);
3664 }
3665
Chris Masond1310b22008-01-24 16:13:08 -05003666 BUG_ON(!list_empty(pages));
3667 if (bio)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003668 return submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003669 return 0;
3670}
Chris Masond1310b22008-01-24 16:13:08 -05003671
3672/*
3673 * basic invalidatepage code, this waits on any locked or writeback
3674 * ranges corresponding to the page, and then deletes any extent state
3675 * records from the tree
3676 */
3677int extent_invalidatepage(struct extent_io_tree *tree,
3678 struct page *page, unsigned long offset)
3679{
Josef Bacik2ac55d42010-02-03 19:33:23 +00003680 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003681 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
3682 u64 end = start + PAGE_CACHE_SIZE - 1;
3683 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3684
Chris Masond3977122009-01-05 21:25:51 -05003685 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003686 if (start > end)
3687 return 0;
3688
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003689 lock_extent_bits(tree, start, end, 0, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04003690 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003691 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04003692 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3693 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003694 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003695 return 0;
3696}
Chris Masond1310b22008-01-24 16:13:08 -05003697
3698/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04003699 * a helper for releasepage, this tests for areas of the page that
3700 * are locked or under IO and drops the related state bits if it is safe
3701 * to drop the page.
3702 */
3703int try_release_extent_state(struct extent_map_tree *map,
3704 struct extent_io_tree *tree, struct page *page,
3705 gfp_t mask)
3706{
3707 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3708 u64 end = start + PAGE_CACHE_SIZE - 1;
3709 int ret = 1;
3710
Chris Mason211f90e2008-07-18 11:56:15 -04003711 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04003712 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04003713 ret = 0;
3714 else {
3715 if ((mask & GFP_NOFS) == GFP_NOFS)
3716 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04003717 /*
3718 * at this point we can safely clear everything except the
3719 * locked bit and the nodatasum bit
3720 */
Chris Masone3f24cc2011-02-14 12:52:08 -05003721 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04003722 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3723 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05003724
3725 /* if clear_extent_bit failed for enomem reasons,
3726 * we can't allow the release to continue.
3727 */
3728 if (ret < 0)
3729 ret = 0;
3730 else
3731 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003732 }
3733 return ret;
3734}
Chris Mason7b13b7b2008-04-18 10:29:50 -04003735
3736/*
Chris Masond1310b22008-01-24 16:13:08 -05003737 * a helper for releasepage. As long as there are no locked extents
3738 * in the range corresponding to the page, both state records and extent
3739 * map records are removed
3740 */
3741int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05003742 struct extent_io_tree *tree, struct page *page,
3743 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05003744{
3745 struct extent_map *em;
3746 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3747 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003748
Chris Mason70dec802008-01-29 09:59:12 -05003749 if ((mask & __GFP_WAIT) &&
3750 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05003751 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05003752 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05003753 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04003754 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05003755 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09003756 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04003757 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003758 break;
3759 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003760 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3761 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04003762 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003763 free_extent_map(em);
3764 break;
3765 }
3766 if (!test_range_bit(tree, em->start,
3767 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04003768 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04003769 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05003770 remove_extent_mapping(map, em);
3771 /* once for the rb tree */
3772 free_extent_map(em);
3773 }
3774 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04003775 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003776
3777 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05003778 free_extent_map(em);
3779 }
Chris Masond1310b22008-01-24 16:13:08 -05003780 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04003781 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003782}
Chris Masond1310b22008-01-24 16:13:08 -05003783
Chris Masonec29ed52011-02-23 16:23:20 -05003784/*
3785 * helper function for fiemap, which doesn't want to see any holes.
3786 * This maps until we find something past 'last'
3787 */
3788static struct extent_map *get_extent_skip_holes(struct inode *inode,
3789 u64 offset,
3790 u64 last,
3791 get_extent_t *get_extent)
3792{
3793 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3794 struct extent_map *em;
3795 u64 len;
3796
3797 if (offset >= last)
3798 return NULL;
3799
3800 while(1) {
3801 len = last - offset;
3802 if (len == 0)
3803 break;
3804 len = (len + sectorsize - 1) & ~(sectorsize - 1);
3805 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02003806 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05003807 return em;
3808
3809 /* if this isn't a hole return it */
3810 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3811 em->block_start != EXTENT_MAP_HOLE) {
3812 return em;
3813 }
3814
3815 /* this is a hole, advance to the next extent */
3816 offset = extent_map_end(em);
3817 free_extent_map(em);
3818 if (offset >= last)
3819 break;
3820 }
3821 return NULL;
3822}
3823
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003824int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3825 __u64 start, __u64 len, get_extent_t *get_extent)
3826{
Josef Bacik975f84f2010-11-23 19:36:57 +00003827 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003828 u64 off = start;
3829 u64 max = start + len;
3830 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00003831 u32 found_type;
3832 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05003833 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003834 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003835 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00003836 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003837 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003838 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00003839 struct btrfs_path *path;
3840 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003841 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003842 u64 em_start = 0;
3843 u64 em_len = 0;
3844 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003845 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003846
3847 if (len == 0)
3848 return -EINVAL;
3849
Josef Bacik975f84f2010-11-23 19:36:57 +00003850 path = btrfs_alloc_path();
3851 if (!path)
3852 return -ENOMEM;
3853 path->leave_spinning = 1;
3854
Josef Bacik4d479cf2011-11-17 11:34:31 -05003855 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3856 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3857
Chris Masonec29ed52011-02-23 16:23:20 -05003858 /*
3859 * lookup the last file extent. We're not using i_size here
3860 * because there might be preallocation past i_size
3861 */
Josef Bacik975f84f2010-11-23 19:36:57 +00003862 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
Li Zefan33345d012011-04-20 10:31:50 +08003863 path, btrfs_ino(inode), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00003864 if (ret < 0) {
3865 btrfs_free_path(path);
3866 return ret;
3867 }
3868 WARN_ON(!ret);
3869 path->slots[0]--;
3870 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3871 struct btrfs_file_extent_item);
3872 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3873 found_type = btrfs_key_type(&found_key);
3874
Chris Masonec29ed52011-02-23 16:23:20 -05003875 /* No extents, but there might be delalloc bits */
Li Zefan33345d012011-04-20 10:31:50 +08003876 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00003877 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05003878 /* have to trust i_size as the end */
3879 last = (u64)-1;
3880 last_for_get_extent = isize;
3881 } else {
3882 /*
3883 * remember the start of the last extent. There are a
3884 * bunch of different factors that go into the length of the
3885 * extent, so its much less complex to remember where it started
3886 */
3887 last = found_key.offset;
3888 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00003889 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003890 btrfs_free_path(path);
3891
Chris Masonec29ed52011-02-23 16:23:20 -05003892 /*
3893 * we might have some extents allocated but more delalloc past those
3894 * extents. so, we trust isize unless the start of the last extent is
3895 * beyond isize
3896 */
3897 if (last < isize) {
3898 last = (u64)-1;
3899 last_for_get_extent = isize;
3900 }
3901
Josef Bacik2ac55d42010-02-03 19:33:23 +00003902 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003903 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05003904
Josef Bacik4d479cf2011-11-17 11:34:31 -05003905 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05003906 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003907 if (!em)
3908 goto out;
3909 if (IS_ERR(em)) {
3910 ret = PTR_ERR(em);
3911 goto out;
3912 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003913
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003914 while (!end) {
Chris Masonea8efc72011-03-08 11:54:40 -05003915 u64 offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003916
Chris Masonea8efc72011-03-08 11:54:40 -05003917 /* break if the extent we found is outside the range */
3918 if (em->start >= max || extent_map_end(em) < off)
3919 break;
3920
3921 /*
3922 * get_extent may return an extent that starts before our
3923 * requested range. We have to make sure the ranges
3924 * we return to fiemap always move forward and don't
3925 * overlap, so adjust the offsets here
3926 */
3927 em_start = max(em->start, off);
3928
3929 /*
3930 * record the offset from the start of the extent
3931 * for adjusting the disk offset below
3932 */
3933 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05003934 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05003935 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05003936 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003937 disko = 0;
3938 flags = 0;
3939
Chris Masonea8efc72011-03-08 11:54:40 -05003940 /*
3941 * bump off for our next call to get_extent
3942 */
3943 off = extent_map_end(em);
3944 if (off >= max)
3945 end = 1;
3946
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003947 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003948 end = 1;
3949 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003950 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003951 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3952 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003953 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003954 flags |= (FIEMAP_EXTENT_DELALLOC |
3955 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003956 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05003957 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003958 }
3959 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3960 flags |= FIEMAP_EXTENT_ENCODED;
3961
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003962 free_extent_map(em);
3963 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05003964 if ((em_start >= last) || em_len == (u64)-1 ||
3965 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003966 flags |= FIEMAP_EXTENT_LAST;
3967 end = 1;
3968 }
3969
Chris Masonec29ed52011-02-23 16:23:20 -05003970 /* now scan forward to see if this is really the last extent. */
3971 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3972 get_extent);
3973 if (IS_ERR(em)) {
3974 ret = PTR_ERR(em);
3975 goto out;
3976 }
3977 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00003978 flags |= FIEMAP_EXTENT_LAST;
3979 end = 1;
3980 }
Chris Masonec29ed52011-02-23 16:23:20 -05003981 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3982 em_len, flags);
3983 if (ret)
3984 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003985 }
3986out_free:
3987 free_extent_map(em);
3988out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00003989 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3990 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003991 return ret;
3992}
3993
Chris Mason727011e2010-08-06 13:21:20 -04003994static void __free_extent_buffer(struct extent_buffer *eb)
3995{
3996#if LEAK_DEBUG
3997 unsigned long flags;
3998 spin_lock_irqsave(&leak_lock, flags);
3999 list_del(&eb->leak_list);
4000 spin_unlock_irqrestore(&leak_lock, flags);
4001#endif
4002 if (eb->pages && eb->pages != eb->inline_pages)
4003 kfree(eb->pages);
4004 kmem_cache_free(extent_buffer_cache, eb);
4005}
4006
Chris Masond1310b22008-01-24 16:13:08 -05004007static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
4008 u64 start,
4009 unsigned long len,
4010 gfp_t mask)
4011{
4012 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05004013#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04004014 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04004015#endif
Chris Masond1310b22008-01-24 16:13:08 -05004016
Chris Masond1310b22008-01-24 16:13:08 -05004017 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00004018 if (eb == NULL)
4019 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004020 eb->start = start;
4021 eb->len = len;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004022 eb->tree = tree;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004023 eb->bflags = 0;
Chris Masonbd681512011-07-16 15:23:14 -04004024 rwlock_init(&eb->lock);
4025 atomic_set(&eb->write_locks, 0);
4026 atomic_set(&eb->read_locks, 0);
4027 atomic_set(&eb->blocking_readers, 0);
4028 atomic_set(&eb->blocking_writers, 0);
4029 atomic_set(&eb->spinning_readers, 0);
4030 atomic_set(&eb->spinning_writers, 0);
Arne Jansen5b25f702011-09-13 10:55:48 +02004031 eb->lock_nested = 0;
Chris Masonbd681512011-07-16 15:23:14 -04004032 init_waitqueue_head(&eb->write_lock_wq);
4033 init_waitqueue_head(&eb->read_lock_wq);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004034
Chris Mason39351272009-02-04 09:24:05 -05004035#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04004036 spin_lock_irqsave(&leak_lock, flags);
4037 list_add(&eb->leak_list, &buffers);
4038 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04004039#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05004040 spin_lock_init(&eb->refs_lock);
Chris Masond1310b22008-01-24 16:13:08 -05004041 atomic_set(&eb->refs, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004042 atomic_set(&eb->io_pages, 0);
Chris Mason727011e2010-08-06 13:21:20 -04004043
4044 if (len > MAX_INLINE_EXTENT_BUFFER_SIZE) {
4045 struct page **pages;
4046 int num_pages = (len + PAGE_CACHE_SIZE - 1) >>
4047 PAGE_CACHE_SHIFT;
4048 pages = kzalloc(num_pages, mask);
4049 if (!pages) {
4050 __free_extent_buffer(eb);
4051 return NULL;
4052 }
4053 eb->pages = pages;
4054 } else {
4055 eb->pages = eb->inline_pages;
4056 }
Chris Masond1310b22008-01-24 16:13:08 -05004057
4058 return eb;
4059}
4060
Jan Schmidt815a51c2012-05-16 17:00:02 +02004061struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4062{
4063 unsigned long i;
4064 struct page *p;
4065 struct extent_buffer *new;
4066 unsigned long num_pages = num_extent_pages(src->start, src->len);
4067
4068 new = __alloc_extent_buffer(NULL, src->start, src->len, GFP_ATOMIC);
4069 if (new == NULL)
4070 return NULL;
4071
4072 for (i = 0; i < num_pages; i++) {
4073 p = alloc_page(GFP_ATOMIC);
4074 BUG_ON(!p);
4075 attach_extent_buffer_page(new, p);
4076 WARN_ON(PageDirty(p));
4077 SetPageUptodate(p);
4078 new->pages[i] = p;
4079 }
4080
4081 copy_extent_buffer(new, src, 0, 0, src->len);
4082 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
4083 set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
4084
4085 return new;
4086}
4087
4088struct extent_buffer *alloc_dummy_extent_buffer(u64 start, unsigned long len)
4089{
4090 struct extent_buffer *eb;
4091 unsigned long num_pages = num_extent_pages(0, len);
4092 unsigned long i;
4093
4094 eb = __alloc_extent_buffer(NULL, start, len, GFP_ATOMIC);
4095 if (!eb)
4096 return NULL;
4097
4098 for (i = 0; i < num_pages; i++) {
4099 eb->pages[i] = alloc_page(GFP_ATOMIC);
4100 if (!eb->pages[i])
4101 goto err;
4102 }
4103 set_extent_buffer_uptodate(eb);
4104 btrfs_set_header_nritems(eb, 0);
4105 set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4106
4107 return eb;
4108err:
Stefan Behrens84167d12012-10-11 07:25:16 -06004109 for (; i > 0; i--)
4110 __free_page(eb->pages[i - 1]);
Jan Schmidt815a51c2012-05-16 17:00:02 +02004111 __free_extent_buffer(eb);
4112 return NULL;
4113}
4114
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004115static int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004116{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004117 return (atomic_read(&eb->io_pages) ||
4118 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4119 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004120}
4121
Miao Xie897ca6e2010-10-26 20:57:29 -04004122/*
4123 * Helper for releasing extent buffer page.
4124 */
4125static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
4126 unsigned long start_idx)
4127{
4128 unsigned long index;
Wang Sheng-Hui39bab872012-04-06 14:35:31 +08004129 unsigned long num_pages;
Miao Xie897ca6e2010-10-26 20:57:29 -04004130 struct page *page;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004131 int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
Miao Xie897ca6e2010-10-26 20:57:29 -04004132
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004133 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e2010-10-26 20:57:29 -04004134
Wang Sheng-Hui39bab872012-04-06 14:35:31 +08004135 num_pages = num_extent_pages(eb->start, eb->len);
4136 index = start_idx + num_pages;
Miao Xie897ca6e2010-10-26 20:57:29 -04004137 if (start_idx >= index)
4138 return;
4139
4140 do {
4141 index--;
4142 page = extent_buffer_page(eb, index);
Jan Schmidt815a51c2012-05-16 17:00:02 +02004143 if (page && mapped) {
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004144 spin_lock(&page->mapping->private_lock);
4145 /*
4146 * We do this since we'll remove the pages after we've
4147 * removed the eb from the radix tree, so we could race
4148 * and have this page now attached to the new eb. So
4149 * only clear page_private if it's still connected to
4150 * this eb.
4151 */
4152 if (PagePrivate(page) &&
4153 page->private == (unsigned long)eb) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004154 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Josef Bacik3083ee22012-03-09 16:01:49 -05004155 BUG_ON(PageDirty(page));
4156 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004157 /*
4158 * We need to make sure we haven't be attached
4159 * to a new eb.
4160 */
4161 ClearPagePrivate(page);
4162 set_page_private(page, 0);
4163 /* One for the page private */
4164 page_cache_release(page);
4165 }
4166 spin_unlock(&page->mapping->private_lock);
4167
Jan Schmidt815a51c2012-05-16 17:00:02 +02004168 }
4169 if (page) {
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004170 /* One for when we alloced the page */
Miao Xie897ca6e2010-10-26 20:57:29 -04004171 page_cache_release(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004172 }
Miao Xie897ca6e2010-10-26 20:57:29 -04004173 } while (index != start_idx);
4174}
4175
4176/*
4177 * Helper for releasing the extent buffer.
4178 */
4179static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4180{
4181 btrfs_release_extent_buffer_page(eb, 0);
4182 __free_extent_buffer(eb);
4183}
4184
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004185static void check_buffer_tree_ref(struct extent_buffer *eb)
4186{
Chris Mason242e18c2013-01-29 17:49:37 -05004187 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004188 /* the ref bit is tricky. We have to make sure it is set
4189 * if we have the buffer dirty. Otherwise the
4190 * code to free a buffer can end up dropping a dirty
4191 * page
4192 *
4193 * Once the ref bit is set, it won't go away while the
4194 * buffer is dirty or in writeback, and it also won't
4195 * go away while we have the reference count on the
4196 * eb bumped.
4197 *
4198 * We can't just set the ref bit without bumping the
4199 * ref on the eb because free_extent_buffer might
4200 * see the ref bit and try to clear it. If this happens
4201 * free_extent_buffer might end up dropping our original
4202 * ref by mistake and freeing the page before we are able
4203 * to add one more ref.
4204 *
4205 * So bump the ref count first, then set the bit. If someone
4206 * beat us to it, drop the ref we added.
4207 */
Chris Mason242e18c2013-01-29 17:49:37 -05004208 refs = atomic_read(&eb->refs);
4209 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4210 return;
4211
Josef Bacik594831c2012-07-20 16:11:08 -04004212 spin_lock(&eb->refs_lock);
4213 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004214 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004215 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004216}
4217
Josef Bacik5df42352012-03-15 18:24:42 -04004218static void mark_extent_buffer_accessed(struct extent_buffer *eb)
4219{
4220 unsigned long num_pages, i;
4221
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004222 check_buffer_tree_ref(eb);
4223
Josef Bacik5df42352012-03-15 18:24:42 -04004224 num_pages = num_extent_pages(eb->start, eb->len);
4225 for (i = 0; i < num_pages; i++) {
4226 struct page *p = extent_buffer_page(eb, i);
4227 mark_page_accessed(p);
4228 }
4229}
4230
Chris Masond1310b22008-01-24 16:13:08 -05004231struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
Chris Mason727011e2010-08-06 13:21:20 -04004232 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004233{
4234 unsigned long num_pages = num_extent_pages(start, len);
4235 unsigned long i;
4236 unsigned long index = start >> PAGE_CACHE_SHIFT;
4237 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004238 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004239 struct page *p;
4240 struct address_space *mapping = tree->mapping;
4241 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004242 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004243
Miao Xie19fe0a82010-10-26 20:57:29 -04004244 rcu_read_lock();
4245 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4246 if (eb && atomic_inc_not_zero(&eb->refs)) {
4247 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004248 mark_extent_buffer_accessed(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04004249 return eb;
4250 }
Miao Xie19fe0a82010-10-26 20:57:29 -04004251 rcu_read_unlock();
Chris Mason6af118ce2008-07-22 11:18:07 -04004252
David Sterbaba144192011-04-21 01:12:06 +02004253 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
Peter2b114d12008-04-01 11:21:40 -04004254 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004255 return NULL;
4256
Chris Mason727011e2010-08-06 13:21:20 -04004257 for (i = 0; i < num_pages; i++, index++) {
Chris Masona6591712011-07-19 12:04:14 -04004258 p = find_or_create_page(mapping, index, GFP_NOFS);
Josef Bacik4804b382012-10-05 16:43:45 -04004259 if (!p)
Chris Mason6af118ce2008-07-22 11:18:07 -04004260 goto free_eb;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004261
4262 spin_lock(&mapping->private_lock);
4263 if (PagePrivate(p)) {
4264 /*
4265 * We could have already allocated an eb for this page
4266 * and attached one so lets see if we can get a ref on
4267 * the existing eb, and if we can we know it's good and
4268 * we can just return that one, else we know we can just
4269 * overwrite page->private.
4270 */
4271 exists = (struct extent_buffer *)p->private;
4272 if (atomic_inc_not_zero(&exists->refs)) {
4273 spin_unlock(&mapping->private_lock);
4274 unlock_page(p);
Josef Bacik17de39a2012-05-04 15:16:06 -04004275 page_cache_release(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004276 mark_extent_buffer_accessed(exists);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004277 goto free_eb;
4278 }
4279
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004280 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004281 * Do this so attach doesn't complain and we need to
4282 * drop the ref the old guy had.
4283 */
4284 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004285 WARN_ON(PageDirty(p));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004286 page_cache_release(p);
Chris Masond1310b22008-01-24 16:13:08 -05004287 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004288 attach_extent_buffer_page(eb, p);
4289 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004290 WARN_ON(PageDirty(p));
Chris Masond1310b22008-01-24 16:13:08 -05004291 mark_page_accessed(p);
Chris Mason727011e2010-08-06 13:21:20 -04004292 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05004293 if (!PageUptodate(p))
4294 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05004295
4296 /*
4297 * see below about how we avoid a nasty race with release page
4298 * and why we unlock later
4299 */
Chris Masond1310b22008-01-24 16:13:08 -05004300 }
4301 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004302 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05004303again:
Miao Xie19fe0a82010-10-26 20:57:29 -04004304 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4305 if (ret)
4306 goto free_eb;
4307
Chris Mason6af118ce2008-07-22 11:18:07 -04004308 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004309 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
4310 if (ret == -EEXIST) {
4311 exists = radix_tree_lookup(&tree->buffer,
4312 start >> PAGE_CACHE_SHIFT);
Josef Bacik115391d2012-03-09 09:51:43 -05004313 if (!atomic_inc_not_zero(&exists->refs)) {
4314 spin_unlock(&tree->buffer_lock);
4315 radix_tree_preload_end();
Josef Bacik115391d2012-03-09 09:51:43 -05004316 exists = NULL;
4317 goto again;
4318 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004319 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004320 radix_tree_preload_end();
Josef Bacik5df42352012-03-15 18:24:42 -04004321 mark_extent_buffer_accessed(exists);
Chris Mason6af118ce2008-07-22 11:18:07 -04004322 goto free_eb;
4323 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004324 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004325 check_buffer_tree_ref(eb);
Yan, Zhengf044ba72010-02-04 08:46:56 +00004326 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004327 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05004328
4329 /*
4330 * there is a race where release page may have
4331 * tried to find this extent buffer in the radix
4332 * but failed. It will tell the VM it is safe to
4333 * reclaim the, and it will clear the page private bit.
4334 * We must make sure to set the page private bit properly
4335 * after the extent buffer is in the radix tree so
4336 * it doesn't get lost
4337 */
Chris Mason727011e2010-08-06 13:21:20 -04004338 SetPageChecked(eb->pages[0]);
4339 for (i = 1; i < num_pages; i++) {
4340 p = extent_buffer_page(eb, i);
Chris Mason727011e2010-08-06 13:21:20 -04004341 ClearPageChecked(p);
4342 unlock_page(p);
4343 }
4344 unlock_page(eb->pages[0]);
Chris Masond1310b22008-01-24 16:13:08 -05004345 return eb;
4346
Chris Mason6af118ce2008-07-22 11:18:07 -04004347free_eb:
Chris Mason727011e2010-08-06 13:21:20 -04004348 for (i = 0; i < num_pages; i++) {
4349 if (eb->pages[i])
4350 unlock_page(eb->pages[i]);
4351 }
Chris Masoneb14ab82011-02-10 12:35:00 -05004352
Josef Bacik17de39a2012-05-04 15:16:06 -04004353 WARN_ON(!atomic_dec_and_test(&eb->refs));
Miao Xie897ca6e2010-10-26 20:57:29 -04004354 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04004355 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05004356}
Chris Masond1310b22008-01-24 16:13:08 -05004357
4358struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
David Sterbaf09d1f62011-04-21 01:08:01 +02004359 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004360{
Chris Masond1310b22008-01-24 16:13:08 -05004361 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05004362
Miao Xie19fe0a82010-10-26 20:57:29 -04004363 rcu_read_lock();
4364 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4365 if (eb && atomic_inc_not_zero(&eb->refs)) {
4366 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004367 mark_extent_buffer_accessed(eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04004368 return eb;
4369 }
4370 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04004371
Miao Xie19fe0a82010-10-26 20:57:29 -04004372 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004373}
Chris Masond1310b22008-01-24 16:13:08 -05004374
Josef Bacik3083ee22012-03-09 16:01:49 -05004375static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4376{
4377 struct extent_buffer *eb =
4378 container_of(head, struct extent_buffer, rcu_head);
4379
4380 __free_extent_buffer(eb);
4381}
4382
Josef Bacik3083ee22012-03-09 16:01:49 -05004383/* Expects to have eb->eb_lock already held */
Josef Bacike64860a2012-07-20 16:05:36 -04004384static int release_extent_buffer(struct extent_buffer *eb, gfp_t mask)
Josef Bacik3083ee22012-03-09 16:01:49 -05004385{
4386 WARN_ON(atomic_read(&eb->refs) == 0);
4387 if (atomic_dec_and_test(&eb->refs)) {
Jan Schmidt815a51c2012-05-16 17:00:02 +02004388 if (test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) {
4389 spin_unlock(&eb->refs_lock);
4390 } else {
4391 struct extent_io_tree *tree = eb->tree;
Josef Bacik3083ee22012-03-09 16:01:49 -05004392
Jan Schmidt815a51c2012-05-16 17:00:02 +02004393 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05004394
Jan Schmidt815a51c2012-05-16 17:00:02 +02004395 spin_lock(&tree->buffer_lock);
4396 radix_tree_delete(&tree->buffer,
4397 eb->start >> PAGE_CACHE_SHIFT);
4398 spin_unlock(&tree->buffer_lock);
4399 }
Josef Bacik3083ee22012-03-09 16:01:49 -05004400
4401 /* Should be safe to release our pages at this point */
4402 btrfs_release_extent_buffer_page(eb, 0);
Josef Bacik3083ee22012-03-09 16:01:49 -05004403 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04004404 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05004405 }
4406 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04004407
4408 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05004409}
4410
Chris Masond1310b22008-01-24 16:13:08 -05004411void free_extent_buffer(struct extent_buffer *eb)
4412{
Chris Mason242e18c2013-01-29 17:49:37 -05004413 int refs;
4414 int old;
Chris Masond1310b22008-01-24 16:13:08 -05004415 if (!eb)
4416 return;
4417
Chris Mason242e18c2013-01-29 17:49:37 -05004418 while (1) {
4419 refs = atomic_read(&eb->refs);
4420 if (refs <= 3)
4421 break;
4422 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
4423 if (old == refs)
4424 return;
4425 }
4426
Josef Bacik3083ee22012-03-09 16:01:49 -05004427 spin_lock(&eb->refs_lock);
4428 if (atomic_read(&eb->refs) == 2 &&
Jan Schmidt815a51c2012-05-16 17:00:02 +02004429 test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
4430 atomic_dec(&eb->refs);
4431
4432 if (atomic_read(&eb->refs) == 2 &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004433 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004434 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004435 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4436 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05004437
Josef Bacik3083ee22012-03-09 16:01:49 -05004438 /*
4439 * I know this is terrible, but it's temporary until we stop tracking
4440 * the uptodate bits and such for the extent buffers.
4441 */
4442 release_extent_buffer(eb, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05004443}
Chris Masond1310b22008-01-24 16:13:08 -05004444
Josef Bacik3083ee22012-03-09 16:01:49 -05004445void free_extent_buffer_stale(struct extent_buffer *eb)
4446{
4447 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004448 return;
4449
Josef Bacik3083ee22012-03-09 16:01:49 -05004450 spin_lock(&eb->refs_lock);
4451 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
4452
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004453 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004454 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4455 atomic_dec(&eb->refs);
4456 release_extent_buffer(eb, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05004457}
4458
Chris Mason1d4284b2012-03-28 20:31:37 -04004459void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004460{
Chris Masond1310b22008-01-24 16:13:08 -05004461 unsigned long i;
4462 unsigned long num_pages;
4463 struct page *page;
4464
Chris Masond1310b22008-01-24 16:13:08 -05004465 num_pages = num_extent_pages(eb->start, eb->len);
4466
4467 for (i = 0; i < num_pages; i++) {
4468 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04004469 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05004470 continue;
4471
Chris Masona61e6f22008-07-22 11:18:08 -04004472 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05004473 WARN_ON(!PagePrivate(page));
4474
Chris Masond1310b22008-01-24 16:13:08 -05004475 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004476 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05004477 if (!PageDirty(page)) {
4478 radix_tree_tag_clear(&page->mapping->page_tree,
4479 page_index(page),
4480 PAGECACHE_TAG_DIRTY);
4481 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004482 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04004483 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04004484 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05004485 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004486 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05004487}
Chris Masond1310b22008-01-24 16:13:08 -05004488
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004489int set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004490{
4491 unsigned long i;
4492 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04004493 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004494
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004495 check_buffer_tree_ref(eb);
4496
Chris Masonb9473432009-03-13 11:00:37 -04004497 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004498
Chris Masond1310b22008-01-24 16:13:08 -05004499 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik3083ee22012-03-09 16:01:49 -05004500 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004501 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
4502
Chris Masonb9473432009-03-13 11:00:37 -04004503 for (i = 0; i < num_pages; i++)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004504 set_page_dirty(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04004505 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05004506}
Chris Masond1310b22008-01-24 16:13:08 -05004507
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004508static int range_straddles_pages(u64 start, u64 len)
Chris Mason19b6caf2011-07-25 06:50:50 -04004509{
4510 if (len < PAGE_CACHE_SIZE)
4511 return 1;
4512 if (start & (PAGE_CACHE_SIZE - 1))
4513 return 1;
4514 if ((start + len) & (PAGE_CACHE_SIZE - 1))
4515 return 1;
4516 return 0;
4517}
4518
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004519int clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04004520{
4521 unsigned long i;
4522 struct page *page;
4523 unsigned long num_pages;
4524
Chris Masonb4ce94d2009-02-04 09:25:08 -05004525 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004526 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason1259ab72008-05-12 13:39:03 -04004527 for (i = 0; i < num_pages; i++) {
4528 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04004529 if (page)
4530 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04004531 }
4532 return 0;
4533}
4534
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004535int set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004536{
4537 unsigned long i;
4538 struct page *page;
4539 unsigned long num_pages;
4540
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004541 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004542 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05004543 for (i = 0; i < num_pages; i++) {
4544 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004545 SetPageUptodate(page);
4546 }
4547 return 0;
4548}
Chris Masond1310b22008-01-24 16:13:08 -05004549
Chris Masonce9adaa2008-04-09 16:28:12 -04004550int extent_range_uptodate(struct extent_io_tree *tree,
4551 u64 start, u64 end)
4552{
4553 struct page *page;
4554 int ret;
4555 int pg_uptodate = 1;
4556 int uptodate;
4557 unsigned long index;
4558
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004559 if (range_straddles_pages(start, end - start + 1)) {
Chris Mason19b6caf2011-07-25 06:50:50 -04004560 ret = test_range_bit(tree, start, end,
4561 EXTENT_UPTODATE, 1, NULL);
4562 if (ret)
4563 return 1;
4564 }
Chris Masond3977122009-01-05 21:25:51 -05004565 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004566 index = start >> PAGE_CACHE_SHIFT;
4567 page = find_get_page(tree->mapping, index);
Mitch Harder8bedd512012-01-26 15:01:11 -05004568 if (!page)
4569 return 1;
Chris Masonce9adaa2008-04-09 16:28:12 -04004570 uptodate = PageUptodate(page);
4571 page_cache_release(page);
4572 if (!uptodate) {
4573 pg_uptodate = 0;
4574 break;
4575 }
4576 start += PAGE_CACHE_SIZE;
4577 }
4578 return pg_uptodate;
4579}
4580
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004581int extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004582{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004583 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004584}
Chris Masond1310b22008-01-24 16:13:08 -05004585
4586int read_extent_buffer_pages(struct extent_io_tree *tree,
Arne Jansenbb82ab82011-06-10 14:06:53 +02004587 struct extent_buffer *eb, u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04004588 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05004589{
4590 unsigned long i;
4591 unsigned long start_i;
4592 struct page *page;
4593 int err;
4594 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04004595 int locked_pages = 0;
4596 int all_uptodate = 1;
Chris Masond1310b22008-01-24 16:13:08 -05004597 unsigned long num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04004598 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004599 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004600 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004601
Chris Masonb4ce94d2009-02-04 09:25:08 -05004602 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05004603 return 0;
4604
Chris Masond1310b22008-01-24 16:13:08 -05004605 if (start) {
4606 WARN_ON(start < eb->start);
4607 start_i = (start >> PAGE_CACHE_SHIFT) -
4608 (eb->start >> PAGE_CACHE_SHIFT);
4609 } else {
4610 start_i = 0;
4611 }
4612
4613 num_pages = num_extent_pages(eb->start, eb->len);
4614 for (i = start_i; i < num_pages; i++) {
4615 page = extent_buffer_page(eb, i);
Arne Jansenbb82ab82011-06-10 14:06:53 +02004616 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04004617 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04004618 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05004619 } else {
4620 lock_page(page);
4621 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004622 locked_pages++;
Chris Mason727011e2010-08-06 13:21:20 -04004623 if (!PageUptodate(page)) {
4624 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04004625 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04004626 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004627 }
4628 if (all_uptodate) {
4629 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004630 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04004631 goto unlock_exit;
4632 }
4633
Josef Bacikea466792012-03-26 21:57:36 -04004634 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04004635 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004636 atomic_set(&eb->io_pages, num_reads);
Chris Masonce9adaa2008-04-09 16:28:12 -04004637 for (i = start_i; i < num_pages; i++) {
4638 page = extent_buffer_page(eb, i);
Chris Masonce9adaa2008-04-09 16:28:12 -04004639 if (!PageUptodate(page)) {
Chris Masonf1885912008-04-09 16:28:12 -04004640 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05004641 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04004642 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04004643 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05004644 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05004645 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05004646 } else {
4647 unlock_page(page);
4648 }
4649 }
4650
Jeff Mahoney355808c2011-10-03 23:23:14 -04004651 if (bio) {
4652 err = submit_one_bio(READ, bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004653 if (err)
4654 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04004655 }
Chris Masona86c12c2008-02-07 10:50:54 -05004656
Arne Jansenbb82ab82011-06-10 14:06:53 +02004657 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05004658 return ret;
Chris Masond3977122009-01-05 21:25:51 -05004659
Chris Masond1310b22008-01-24 16:13:08 -05004660 for (i = start_i; i < num_pages; i++) {
4661 page = extent_buffer_page(eb, i);
4662 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05004663 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05004664 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05004665 }
Chris Masond3977122009-01-05 21:25:51 -05004666
Chris Masond1310b22008-01-24 16:13:08 -05004667 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04004668
4669unlock_exit:
4670 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05004671 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004672 page = extent_buffer_page(eb, i);
4673 i++;
4674 unlock_page(page);
4675 locked_pages--;
4676 }
4677 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004678}
Chris Masond1310b22008-01-24 16:13:08 -05004679
4680void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4681 unsigned long start,
4682 unsigned long len)
4683{
4684 size_t cur;
4685 size_t offset;
4686 struct page *page;
4687 char *kaddr;
4688 char *dst = (char *)dstv;
4689 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4690 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004691
4692 WARN_ON(start > eb->len);
4693 WARN_ON(start + len > eb->start + eb->len);
4694
4695 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4696
Chris Masond3977122009-01-05 21:25:51 -05004697 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004698 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004699
4700 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04004701 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004702 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004703
4704 dst += cur;
4705 len -= cur;
4706 offset = 0;
4707 i++;
4708 }
4709}
Chris Masond1310b22008-01-24 16:13:08 -05004710
4711int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04004712 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05004713 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04004714 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05004715{
4716 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4717 char *kaddr;
4718 struct page *p;
4719 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4720 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4721 unsigned long end_i = (start_offset + start + min_len - 1) >>
4722 PAGE_CACHE_SHIFT;
4723
4724 if (i != end_i)
4725 return -EINVAL;
4726
4727 if (i == 0) {
4728 offset = start_offset;
4729 *map_start = 0;
4730 } else {
4731 offset = 0;
4732 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4733 }
Chris Masond3977122009-01-05 21:25:51 -05004734
Chris Masond1310b22008-01-24 16:13:08 -05004735 if (start + min_len > eb->len) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004736 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
Chris Masond3977122009-01-05 21:25:51 -05004737 "wanted %lu %lu\n", (unsigned long long)eb->start,
4738 eb->len, start, min_len);
Josef Bacik850265332011-03-15 14:52:12 -04004739 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05004740 }
4741
4742 p = extent_buffer_page(eb, i);
Chris Masona6591712011-07-19 12:04:14 -04004743 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05004744 *map = kaddr + offset;
4745 *map_len = PAGE_CACHE_SIZE - offset;
4746 return 0;
4747}
Chris Masond1310b22008-01-24 16:13:08 -05004748
Chris Masond1310b22008-01-24 16:13:08 -05004749int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4750 unsigned long start,
4751 unsigned long len)
4752{
4753 size_t cur;
4754 size_t offset;
4755 struct page *page;
4756 char *kaddr;
4757 char *ptr = (char *)ptrv;
4758 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4759 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4760 int ret = 0;
4761
4762 WARN_ON(start > eb->len);
4763 WARN_ON(start + len > eb->start + eb->len);
4764
4765 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4766
Chris Masond3977122009-01-05 21:25:51 -05004767 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004768 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004769
4770 cur = min(len, (PAGE_CACHE_SIZE - offset));
4771
Chris Masona6591712011-07-19 12:04:14 -04004772 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004773 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004774 if (ret)
4775 break;
4776
4777 ptr += cur;
4778 len -= cur;
4779 offset = 0;
4780 i++;
4781 }
4782 return ret;
4783}
Chris Masond1310b22008-01-24 16:13:08 -05004784
4785void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4786 unsigned long start, unsigned long len)
4787{
4788 size_t cur;
4789 size_t offset;
4790 struct page *page;
4791 char *kaddr;
4792 char *src = (char *)srcv;
4793 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4794 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4795
4796 WARN_ON(start > eb->len);
4797 WARN_ON(start + len > eb->start + eb->len);
4798
4799 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4800
Chris Masond3977122009-01-05 21:25:51 -05004801 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004802 page = extent_buffer_page(eb, i);
4803 WARN_ON(!PageUptodate(page));
4804
4805 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004806 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004807 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004808
4809 src += cur;
4810 len -= cur;
4811 offset = 0;
4812 i++;
4813 }
4814}
Chris Masond1310b22008-01-24 16:13:08 -05004815
4816void memset_extent_buffer(struct extent_buffer *eb, char c,
4817 unsigned long start, unsigned long len)
4818{
4819 size_t cur;
4820 size_t offset;
4821 struct page *page;
4822 char *kaddr;
4823 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4824 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4825
4826 WARN_ON(start > eb->len);
4827 WARN_ON(start + len > eb->start + eb->len);
4828
4829 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4830
Chris Masond3977122009-01-05 21:25:51 -05004831 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004832 page = extent_buffer_page(eb, i);
4833 WARN_ON(!PageUptodate(page));
4834
4835 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004836 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004837 memset(kaddr + offset, c, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004838
4839 len -= cur;
4840 offset = 0;
4841 i++;
4842 }
4843}
Chris Masond1310b22008-01-24 16:13:08 -05004844
4845void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4846 unsigned long dst_offset, unsigned long src_offset,
4847 unsigned long len)
4848{
4849 u64 dst_len = dst->len;
4850 size_t cur;
4851 size_t offset;
4852 struct page *page;
4853 char *kaddr;
4854 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4855 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4856
4857 WARN_ON(src->len != dst_len);
4858
4859 offset = (start_offset + dst_offset) &
4860 ((unsigned long)PAGE_CACHE_SIZE - 1);
4861
Chris Masond3977122009-01-05 21:25:51 -05004862 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004863 page = extent_buffer_page(dst, i);
4864 WARN_ON(!PageUptodate(page));
4865
4866 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4867
Chris Masona6591712011-07-19 12:04:14 -04004868 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004869 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004870
4871 src_offset += cur;
4872 len -= cur;
4873 offset = 0;
4874 i++;
4875 }
4876}
Chris Masond1310b22008-01-24 16:13:08 -05004877
4878static void move_pages(struct page *dst_page, struct page *src_page,
4879 unsigned long dst_off, unsigned long src_off,
4880 unsigned long len)
4881{
Chris Masona6591712011-07-19 12:04:14 -04004882 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004883 if (dst_page == src_page) {
4884 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4885 } else {
Chris Masona6591712011-07-19 12:04:14 -04004886 char *src_kaddr = page_address(src_page);
Chris Masond1310b22008-01-24 16:13:08 -05004887 char *p = dst_kaddr + dst_off + len;
4888 char *s = src_kaddr + src_off + len;
4889
4890 while (len--)
4891 *--p = *--s;
Chris Masond1310b22008-01-24 16:13:08 -05004892 }
Chris Masond1310b22008-01-24 16:13:08 -05004893}
4894
Sergei Trofimovich33872062011-04-11 21:52:52 +00004895static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4896{
4897 unsigned long distance = (src > dst) ? src - dst : dst - src;
4898 return distance < len;
4899}
4900
Chris Masond1310b22008-01-24 16:13:08 -05004901static void copy_pages(struct page *dst_page, struct page *src_page,
4902 unsigned long dst_off, unsigned long src_off,
4903 unsigned long len)
4904{
Chris Masona6591712011-07-19 12:04:14 -04004905 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004906 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004907 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004908
Sergei Trofimovich33872062011-04-11 21:52:52 +00004909 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04004910 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00004911 } else {
Chris Masond1310b22008-01-24 16:13:08 -05004912 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004913 if (areas_overlap(src_off, dst_off, len))
4914 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00004915 }
Chris Masond1310b22008-01-24 16:13:08 -05004916
Chris Mason727011e2010-08-06 13:21:20 -04004917 if (must_memmove)
4918 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
4919 else
4920 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05004921}
4922
4923void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4924 unsigned long src_offset, unsigned long len)
4925{
4926 size_t cur;
4927 size_t dst_off_in_page;
4928 size_t src_off_in_page;
4929 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4930 unsigned long dst_i;
4931 unsigned long src_i;
4932
4933 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004934 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4935 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004936 BUG_ON(1);
4937 }
4938 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004939 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4940 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004941 BUG_ON(1);
4942 }
4943
Chris Masond3977122009-01-05 21:25:51 -05004944 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004945 dst_off_in_page = (start_offset + dst_offset) &
4946 ((unsigned long)PAGE_CACHE_SIZE - 1);
4947 src_off_in_page = (start_offset + src_offset) &
4948 ((unsigned long)PAGE_CACHE_SIZE - 1);
4949
4950 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4951 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4952
4953 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4954 src_off_in_page));
4955 cur = min_t(unsigned long, cur,
4956 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4957
4958 copy_pages(extent_buffer_page(dst, dst_i),
4959 extent_buffer_page(dst, src_i),
4960 dst_off_in_page, src_off_in_page, cur);
4961
4962 src_offset += cur;
4963 dst_offset += cur;
4964 len -= cur;
4965 }
4966}
Chris Masond1310b22008-01-24 16:13:08 -05004967
4968void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4969 unsigned long src_offset, unsigned long len)
4970{
4971 size_t cur;
4972 size_t dst_off_in_page;
4973 size_t src_off_in_page;
4974 unsigned long dst_end = dst_offset + len - 1;
4975 unsigned long src_end = src_offset + len - 1;
4976 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4977 unsigned long dst_i;
4978 unsigned long src_i;
4979
4980 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004981 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4982 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004983 BUG_ON(1);
4984 }
4985 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004986 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4987 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004988 BUG_ON(1);
4989 }
Chris Mason727011e2010-08-06 13:21:20 -04004990 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05004991 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4992 return;
4993 }
Chris Masond3977122009-01-05 21:25:51 -05004994 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004995 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
4996 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
4997
4998 dst_off_in_page = (start_offset + dst_end) &
4999 ((unsigned long)PAGE_CACHE_SIZE - 1);
5000 src_off_in_page = (start_offset + src_end) &
5001 ((unsigned long)PAGE_CACHE_SIZE - 1);
5002
5003 cur = min_t(unsigned long, len, src_off_in_page + 1);
5004 cur = min(cur, dst_off_in_page + 1);
5005 move_pages(extent_buffer_page(dst, dst_i),
5006 extent_buffer_page(dst, src_i),
5007 dst_off_in_page - cur + 1,
5008 src_off_in_page - cur + 1, cur);
5009
5010 dst_end -= cur;
5011 src_end -= cur;
5012 len -= cur;
5013 }
5014}
Chris Mason6af118ce2008-07-22 11:18:07 -04005015
Josef Bacik3083ee22012-03-09 16:01:49 -05005016int try_release_extent_buffer(struct page *page, gfp_t mask)
Miao Xie19fe0a82010-10-26 20:57:29 -04005017{
Chris Mason6af118ce2008-07-22 11:18:07 -04005018 struct extent_buffer *eb;
Miao Xie897ca6e2010-10-26 20:57:29 -04005019
Miao Xie19fe0a82010-10-26 20:57:29 -04005020 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005021 * We need to make sure noboody is attaching this page to an eb right
5022 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04005023 */
Josef Bacik3083ee22012-03-09 16:01:49 -05005024 spin_lock(&page->mapping->private_lock);
5025 if (!PagePrivate(page)) {
5026 spin_unlock(&page->mapping->private_lock);
5027 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005028 }
5029
Josef Bacik3083ee22012-03-09 16:01:49 -05005030 eb = (struct extent_buffer *)page->private;
5031 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04005032
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005033 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005034 * This is a little awful but should be ok, we need to make sure that
5035 * the eb doesn't disappear out from under us while we're looking at
5036 * this page.
5037 */
5038 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005039 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05005040 spin_unlock(&eb->refs_lock);
5041 spin_unlock(&page->mapping->private_lock);
5042 return 0;
5043 }
5044 spin_unlock(&page->mapping->private_lock);
5045
5046 if ((mask & GFP_NOFS) == GFP_NOFS)
5047 mask = GFP_NOFS;
5048
5049 /*
5050 * If tree ref isn't set then we know the ref on this eb is a real ref,
5051 * so just return, this page will likely be freed soon anyway.
5052 */
5053 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5054 spin_unlock(&eb->refs_lock);
5055 return 0;
5056 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005057
Josef Bacike64860a2012-07-20 16:05:36 -04005058 return release_extent_buffer(eb, mask);
Chris Mason6af118ce2008-07-22 11:18:07 -04005059}