blob: ca4355ddea06e67ab2faad16e54e0080038c1c59 [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>
Chris Masond1310b22008-01-24 16:13:08 -05007#include <linux/spinlock.h>
8#include <linux/blkdev.h>
9#include <linux/swap.h>
Chris Masond1310b22008-01-24 16:13:08 -050010#include <linux/writeback.h>
11#include <linux/pagevec.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070012#include <linux/prefetch.h>
Dan Magenheimer90a887c2011-05-26 10:01:56 -060013#include <linux/cleancache.h>
Chris Masond1310b22008-01-24 16:13:08 -050014#include "extent_io.h"
15#include "extent_map.h"
David Woodhouse2db04962008-08-07 11:19:43 -040016#include "compat.h"
David Woodhouse902b22f2008-08-20 08:51:49 -040017#include "ctree.h"
18#include "btrfs_inode.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020019#include "volumes.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010020#include "check-integrity.h"
Josef Bacik0b32f4b2012-03-13 09:38:00 -040021#include "locking.h"
Josef Bacik606686e2012-06-04 14:03:51 -040022#include "rcu-string.h"
Chris Masond1310b22008-01-24 16:13:08 -050023
Chris Masond1310b22008-01-24 16:13:08 -050024static struct kmem_cache *extent_state_cache;
25static struct kmem_cache *extent_buffer_cache;
26
Eric Sandeen6d49ba12013-04-22 16:12:31 +000027#ifdef CONFIG_BTRFS_DEBUG
Chris Masond1310b22008-01-24 16:13:08 -050028static LIST_HEAD(buffers);
29static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040030
Chris Masond3977122009-01-05 21:25:51 -050031static DEFINE_SPINLOCK(leak_lock);
Eric Sandeen6d49ba12013-04-22 16:12:31 +000032
33static inline
34void btrfs_leak_debug_add(struct list_head *new, struct list_head *head)
35{
36 unsigned long flags;
37
38 spin_lock_irqsave(&leak_lock, flags);
39 list_add(new, head);
40 spin_unlock_irqrestore(&leak_lock, flags);
41}
42
43static inline
44void btrfs_leak_debug_del(struct list_head *entry)
45{
46 unsigned long flags;
47
48 spin_lock_irqsave(&leak_lock, flags);
49 list_del(entry);
50 spin_unlock_irqrestore(&leak_lock, flags);
51}
52
53static inline
54void btrfs_leak_debug_check(void)
55{
56 struct extent_state *state;
57 struct extent_buffer *eb;
58
59 while (!list_empty(&states)) {
60 state = list_entry(states.next, struct extent_state, leak_list);
61 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
62 "state %lu in tree %p refs %d\n",
63 (unsigned long long)state->start,
64 (unsigned long long)state->end,
65 state->state, state->tree, atomic_read(&state->refs));
66 list_del(&state->leak_list);
67 kmem_cache_free(extent_state_cache, state);
68 }
69
70 while (!list_empty(&buffers)) {
71 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
72 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
73 "refs %d\n", (unsigned long long)eb->start,
74 eb->len, atomic_read(&eb->refs));
75 list_del(&eb->leak_list);
76 kmem_cache_free(extent_buffer_cache, eb);
77 }
78}
79#else
80#define btrfs_leak_debug_add(new, head) do {} while (0)
81#define btrfs_leak_debug_del(entry) do {} while (0)
82#define btrfs_leak_debug_check() do {} while (0)
Chris Mason4bef0842008-09-08 11:18:08 -040083#endif
Chris Masond1310b22008-01-24 16:13:08 -050084
Chris Masond1310b22008-01-24 16:13:08 -050085#define BUFFER_LRU_MAX 64
86
87struct tree_entry {
88 u64 start;
89 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -050090 struct rb_node rb_node;
91};
92
93struct extent_page_data {
94 struct bio *bio;
95 struct extent_io_tree *tree;
96 get_extent_t *get_extent;
Josef Bacikde0022b2012-09-25 14:25:58 -040097 unsigned long bio_flags;
Chris Mason771ed682008-11-06 22:02:51 -050098
99 /* tells writepage not to lock the state bits for this range
100 * it still does the unlocking
101 */
Chris Masonffbd5172009-04-20 15:50:09 -0400102 unsigned int extent_locked:1;
103
104 /* tells the submit_bio code to use a WRITE_SYNC */
105 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -0500106};
107
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400108static noinline void flush_write_bio(void *data);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400109static inline struct btrfs_fs_info *
110tree_fs_info(struct extent_io_tree *tree)
111{
112 return btrfs_sb(tree->mapping->host->i_sb);
113}
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400114
Chris Masond1310b22008-01-24 16:13:08 -0500115int __init extent_io_init(void)
116{
David Sterba837e1972012-09-07 03:00:48 -0600117 extent_state_cache = kmem_cache_create("btrfs_extent_state",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200118 sizeof(struct extent_state), 0,
119 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500120 if (!extent_state_cache)
121 return -ENOMEM;
122
David Sterba837e1972012-09-07 03:00:48 -0600123 extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200124 sizeof(struct extent_buffer), 0,
125 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500126 if (!extent_buffer_cache)
127 goto free_state_cache;
128 return 0;
129
130free_state_cache:
131 kmem_cache_destroy(extent_state_cache);
132 return -ENOMEM;
133}
134
135void extent_io_exit(void)
136{
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000137 btrfs_leak_debug_check();
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000138
139 /*
140 * Make sure all delayed rcu free are flushed before we
141 * destroy caches.
142 */
143 rcu_barrier();
Chris Masond1310b22008-01-24 16:13:08 -0500144 if (extent_state_cache)
145 kmem_cache_destroy(extent_state_cache);
146 if (extent_buffer_cache)
147 kmem_cache_destroy(extent_buffer_cache);
148}
149
150void extent_io_tree_init(struct extent_io_tree *tree,
David Sterbaf993c882011-04-20 23:35:57 +0200151 struct address_space *mapping)
Chris Masond1310b22008-01-24 16:13:08 -0500152{
Eric Paris6bef4d32010-02-23 19:43:04 +0000153 tree->state = RB_ROOT;
Miao Xie19fe0a82010-10-26 20:57:29 -0400154 INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500155 tree->ops = NULL;
156 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500157 spin_lock_init(&tree->lock);
Chris Mason6af118ce2008-07-22 11:18:07 -0400158 spin_lock_init(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -0500159 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500160}
Chris Masond1310b22008-01-24 16:13:08 -0500161
Christoph Hellwigb2950862008-12-02 09:54:17 -0500162static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500163{
164 struct extent_state *state;
Chris Masond1310b22008-01-24 16:13:08 -0500165
166 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400167 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500168 return state;
169 state->state = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500170 state->private = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500171 state->tree = NULL;
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000172 btrfs_leak_debug_add(&state->leak_list, &states);
Chris Masond1310b22008-01-24 16:13:08 -0500173 atomic_set(&state->refs, 1);
174 init_waitqueue_head(&state->wq);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100175 trace_alloc_extent_state(state, mask, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500176 return state;
177}
Chris Masond1310b22008-01-24 16:13:08 -0500178
Chris Mason4845e442010-05-25 20:56:50 -0400179void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500180{
Chris Masond1310b22008-01-24 16:13:08 -0500181 if (!state)
182 return;
183 if (atomic_dec_and_test(&state->refs)) {
Chris Mason70dec802008-01-29 09:59:12 -0500184 WARN_ON(state->tree);
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000185 btrfs_leak_debug_del(&state->leak_list);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100186 trace_free_extent_state(state, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500187 kmem_cache_free(extent_state_cache, state);
188 }
189}
Chris Masond1310b22008-01-24 16:13:08 -0500190
191static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
192 struct rb_node *node)
193{
Chris Masond3977122009-01-05 21:25:51 -0500194 struct rb_node **p = &root->rb_node;
195 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500196 struct tree_entry *entry;
197
Chris Masond3977122009-01-05 21:25:51 -0500198 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500199 parent = *p;
200 entry = rb_entry(parent, struct tree_entry, rb_node);
201
202 if (offset < entry->start)
203 p = &(*p)->rb_left;
204 else if (offset > entry->end)
205 p = &(*p)->rb_right;
206 else
207 return parent;
208 }
209
Chris Masond1310b22008-01-24 16:13:08 -0500210 rb_link_node(node, parent, p);
211 rb_insert_color(node, root);
212 return NULL;
213}
214
Chris Mason80ea96b2008-02-01 14:51:59 -0500215static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Chris Masond1310b22008-01-24 16:13:08 -0500216 struct rb_node **prev_ret,
217 struct rb_node **next_ret)
218{
Chris Mason80ea96b2008-02-01 14:51:59 -0500219 struct rb_root *root = &tree->state;
Chris Masond3977122009-01-05 21:25:51 -0500220 struct rb_node *n = root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500221 struct rb_node *prev = NULL;
222 struct rb_node *orig_prev = NULL;
223 struct tree_entry *entry;
224 struct tree_entry *prev_entry = NULL;
225
Chris Masond3977122009-01-05 21:25:51 -0500226 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500227 entry = rb_entry(n, struct tree_entry, rb_node);
228 prev = n;
229 prev_entry = entry;
230
231 if (offset < entry->start)
232 n = n->rb_left;
233 else if (offset > entry->end)
234 n = n->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500235 else
Chris Masond1310b22008-01-24 16:13:08 -0500236 return n;
237 }
238
239 if (prev_ret) {
240 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500241 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500242 prev = rb_next(prev);
243 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
244 }
245 *prev_ret = prev;
246 prev = orig_prev;
247 }
248
249 if (next_ret) {
250 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500251 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500252 prev = rb_prev(prev);
253 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
254 }
255 *next_ret = prev;
256 }
257 return NULL;
258}
259
Chris Mason80ea96b2008-02-01 14:51:59 -0500260static inline struct rb_node *tree_search(struct extent_io_tree *tree,
261 u64 offset)
Chris Masond1310b22008-01-24 16:13:08 -0500262{
Chris Mason70dec802008-01-29 09:59:12 -0500263 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500264 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500265
Chris Mason80ea96b2008-02-01 14:51:59 -0500266 ret = __etree_search(tree, offset, &prev, NULL);
Chris Masond3977122009-01-05 21:25:51 -0500267 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500268 return prev;
269 return ret;
270}
271
Josef Bacik9ed74f22009-09-11 16:12:44 -0400272static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
273 struct extent_state *other)
274{
275 if (tree->ops && tree->ops->merge_extent_hook)
276 tree->ops->merge_extent_hook(tree->mapping->host, new,
277 other);
278}
279
Chris Masond1310b22008-01-24 16:13:08 -0500280/*
281 * utility function to look for merge candidates inside a given range.
282 * Any extents with matching state are merged together into a single
283 * extent in the tree. Extents with EXTENT_IO in their state field
284 * are not merged because the end_io handlers need to be able to do
285 * operations on them without sleeping (or doing allocations/splits).
286 *
287 * This should be called with the tree lock held.
288 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000289static void merge_state(struct extent_io_tree *tree,
290 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500291{
292 struct extent_state *other;
293 struct rb_node *other_node;
294
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400295 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000296 return;
Chris Masond1310b22008-01-24 16:13:08 -0500297
298 other_node = rb_prev(&state->rb_node);
299 if (other_node) {
300 other = rb_entry(other_node, struct extent_state, rb_node);
301 if (other->end == state->start - 1 &&
302 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400303 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500304 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500305 other->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500306 rb_erase(&other->rb_node, &tree->state);
307 free_extent_state(other);
308 }
309 }
310 other_node = rb_next(&state->rb_node);
311 if (other_node) {
312 other = rb_entry(other_node, struct extent_state, rb_node);
313 if (other->start == state->end + 1 &&
314 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400315 merge_cb(tree, state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400316 state->end = other->end;
317 other->tree = NULL;
318 rb_erase(&other->rb_node, &tree->state);
319 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500320 }
321 }
Chris Masond1310b22008-01-24 16:13:08 -0500322}
323
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000324static void set_state_cb(struct extent_io_tree *tree,
David Sterba41074882013-04-29 13:38:46 +0000325 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500326{
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000327 if (tree->ops && tree->ops->set_bit_hook)
328 tree->ops->set_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500329}
330
331static void clear_state_cb(struct extent_io_tree *tree,
David Sterba41074882013-04-29 13:38:46 +0000332 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500333{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400334 if (tree->ops && tree->ops->clear_bit_hook)
335 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500336}
337
Xiao Guangrong3150b692011-07-14 03:19:08 +0000338static void set_state_bits(struct extent_io_tree *tree,
David Sterba41074882013-04-29 13:38:46 +0000339 struct extent_state *state, unsigned long *bits);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000340
Chris Masond1310b22008-01-24 16:13:08 -0500341/*
342 * insert an extent_state struct into the tree. 'bits' are set on the
343 * struct before it is inserted.
344 *
345 * This may return -EEXIST if the extent is already there, in which case the
346 * state struct is freed.
347 *
348 * The tree lock is not taken internally. This is a utility function and
349 * probably isn't what you want to call (see set/clear_extent_bit).
350 */
351static int insert_state(struct extent_io_tree *tree,
352 struct extent_state *state, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +0000353 unsigned long *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500354{
355 struct rb_node *node;
356
Julia Lawall31b1a2b2012-11-03 10:58:34 +0000357 if (end < start)
358 WARN(1, KERN_ERR "btrfs end < start %llu %llu\n",
Chris Masond3977122009-01-05 21:25:51 -0500359 (unsigned long long)end,
360 (unsigned long long)start);
Chris Masond1310b22008-01-24 16:13:08 -0500361 state->start = start;
362 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400363
Xiao Guangrong3150b692011-07-14 03:19:08 +0000364 set_state_bits(tree, state, bits);
365
Chris Masond1310b22008-01-24 16:13:08 -0500366 node = tree_insert(&tree->state, end, &state->rb_node);
367 if (node) {
368 struct extent_state *found;
369 found = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500370 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
371 "%llu %llu\n", (unsigned long long)found->start,
372 (unsigned long long)found->end,
373 (unsigned long long)start, (unsigned long long)end);
Chris Masond1310b22008-01-24 16:13:08 -0500374 return -EEXIST;
375 }
Chris Mason70dec802008-01-29 09:59:12 -0500376 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500377 merge_state(tree, state);
378 return 0;
379}
380
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000381static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
Josef Bacik9ed74f22009-09-11 16:12:44 -0400382 u64 split)
383{
384 if (tree->ops && tree->ops->split_extent_hook)
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000385 tree->ops->split_extent_hook(tree->mapping->host, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400386}
387
Chris Masond1310b22008-01-24 16:13:08 -0500388/*
389 * split a given extent state struct in two, inserting the preallocated
390 * struct 'prealloc' as the newly created second half. 'split' indicates an
391 * offset inside 'orig' where it should be split.
392 *
393 * Before calling,
394 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
395 * are two extent state structs in the tree:
396 * prealloc: [orig->start, split - 1]
397 * orig: [ split, orig->end ]
398 *
399 * The tree locks are not taken by this function. They need to be held
400 * by the caller.
401 */
402static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
403 struct extent_state *prealloc, u64 split)
404{
405 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400406
407 split_cb(tree, orig, split);
408
Chris Masond1310b22008-01-24 16:13:08 -0500409 prealloc->start = orig->start;
410 prealloc->end = split - 1;
411 prealloc->state = orig->state;
412 orig->start = split;
413
414 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
415 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500416 free_extent_state(prealloc);
417 return -EEXIST;
418 }
Chris Mason70dec802008-01-29 09:59:12 -0500419 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500420 return 0;
421}
422
Li Zefancdc6a392012-03-12 16:39:48 +0800423static struct extent_state *next_state(struct extent_state *state)
424{
425 struct rb_node *next = rb_next(&state->rb_node);
426 if (next)
427 return rb_entry(next, struct extent_state, rb_node);
428 else
429 return NULL;
430}
431
Chris Masond1310b22008-01-24 16:13:08 -0500432/*
433 * utility function to clear some bits in an extent state struct.
Wang Sheng-Hui1b303fc2012-04-06 14:35:18 +0800434 * it will optionally wake up any one waiting on this state (wake == 1).
Chris Masond1310b22008-01-24 16:13:08 -0500435 *
436 * If no bits are set on the state struct after clearing things, the
437 * struct is freed and removed from the tree
438 */
Li Zefancdc6a392012-03-12 16:39:48 +0800439static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
440 struct extent_state *state,
David Sterba41074882013-04-29 13:38:46 +0000441 unsigned long *bits, int wake)
Chris Masond1310b22008-01-24 16:13:08 -0500442{
Li Zefancdc6a392012-03-12 16:39:48 +0800443 struct extent_state *next;
David Sterba41074882013-04-29 13:38:46 +0000444 unsigned long bits_to_clear = *bits & ~EXTENT_CTLBITS;
Chris Masond1310b22008-01-24 16:13:08 -0500445
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400446 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500447 u64 range = state->end - state->start + 1;
448 WARN_ON(range > tree->dirty_bytes);
449 tree->dirty_bytes -= range;
450 }
Chris Mason291d6732008-01-29 15:55:23 -0500451 clear_state_cb(tree, state, bits);
Josef Bacik32c00af2009-10-08 13:34:05 -0400452 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500453 if (wake)
454 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400455 if (state->state == 0) {
Li Zefancdc6a392012-03-12 16:39:48 +0800456 next = next_state(state);
Chris Mason70dec802008-01-29 09:59:12 -0500457 if (state->tree) {
Chris Masond1310b22008-01-24 16:13:08 -0500458 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500459 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500460 free_extent_state(state);
461 } else {
462 WARN_ON(1);
463 }
464 } else {
465 merge_state(tree, state);
Li Zefancdc6a392012-03-12 16:39:48 +0800466 next = next_state(state);
Chris Masond1310b22008-01-24 16:13:08 -0500467 }
Li Zefancdc6a392012-03-12 16:39:48 +0800468 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500469}
470
Xiao Guangrong82337672011-04-20 06:44:57 +0000471static struct extent_state *
472alloc_extent_state_atomic(struct extent_state *prealloc)
473{
474 if (!prealloc)
475 prealloc = alloc_extent_state(GFP_ATOMIC);
476
477 return prealloc;
478}
479
Eric Sandeen48a3b632013-04-25 20:41:01 +0000480static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400481{
482 btrfs_panic(tree_fs_info(tree), err, "Locking error: "
483 "Extent tree was modified by another "
484 "thread while locked.");
485}
486
Chris Masond1310b22008-01-24 16:13:08 -0500487/*
488 * clear some bits on a range in the tree. This may require splitting
489 * or inserting elements in the tree, so the gfp mask is used to
490 * indicate which allocations or sleeping are allowed.
491 *
492 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
493 * the given range from the tree regardless of state (ie for truncate).
494 *
495 * the range [start, end] is inclusive.
496 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100497 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500498 */
499int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +0000500 unsigned long bits, int wake, int delete,
Chris Mason2c64c532009-09-02 15:04:12 -0400501 struct extent_state **cached_state,
502 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500503{
504 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400505 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500506 struct extent_state *prealloc = NULL;
507 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400508 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500509 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000510 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500511
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400512 if (delete)
513 bits |= ~EXTENT_CTLBITS;
514 bits |= EXTENT_FIRST_DELALLOC;
515
Josef Bacik2ac55d42010-02-03 19:33:23 +0000516 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
517 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500518again:
519 if (!prealloc && (mask & __GFP_WAIT)) {
520 prealloc = alloc_extent_state(mask);
521 if (!prealloc)
522 return -ENOMEM;
523 }
524
Chris Masoncad321a2008-12-17 14:51:42 -0500525 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400526 if (cached_state) {
527 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000528
529 if (clear) {
530 *cached_state = NULL;
531 cached_state = NULL;
532 }
533
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400534 if (cached && cached->tree && cached->start <= start &&
535 cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000536 if (clear)
537 atomic_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400538 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400539 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400540 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000541 if (clear)
542 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400543 }
Chris Masond1310b22008-01-24 16:13:08 -0500544 /*
545 * this search will find the extents that end after
546 * our range starts
547 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500548 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500549 if (!node)
550 goto out;
551 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400552hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500553 if (state->start > end)
554 goto out;
555 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400556 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500557
Liu Bo04493142012-02-16 18:34:37 +0800558 /* the state doesn't have the wanted bits, go ahead */
Li Zefancdc6a392012-03-12 16:39:48 +0800559 if (!(state->state & bits)) {
560 state = next_state(state);
Liu Bo04493142012-02-16 18:34:37 +0800561 goto next;
Li Zefancdc6a392012-03-12 16:39:48 +0800562 }
Liu Bo04493142012-02-16 18:34:37 +0800563
Chris Masond1310b22008-01-24 16:13:08 -0500564 /*
565 * | ---- desired range ---- |
566 * | state | or
567 * | ------------- state -------------- |
568 *
569 * We need to split the extent we found, and may flip
570 * bits on second half.
571 *
572 * If the extent we found extends past our range, we
573 * just split and search again. It'll get split again
574 * the next time though.
575 *
576 * If the extent we found is inside our range, we clear
577 * the desired bit on it.
578 */
579
580 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000581 prealloc = alloc_extent_state_atomic(prealloc);
582 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500583 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400584 if (err)
585 extent_io_tree_panic(tree, err);
586
Chris Masond1310b22008-01-24 16:13:08 -0500587 prealloc = NULL;
588 if (err)
589 goto out;
590 if (state->end <= end) {
Liu Bod1ac6e42012-05-10 18:10:39 +0800591 state = clear_state_bit(tree, state, &bits, wake);
592 goto next;
Chris Masond1310b22008-01-24 16:13:08 -0500593 }
594 goto search_again;
595 }
596 /*
597 * | ---- desired range ---- |
598 * | state |
599 * We need to split the extent, and clear the bit
600 * on the first half
601 */
602 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000603 prealloc = alloc_extent_state_atomic(prealloc);
604 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500605 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400606 if (err)
607 extent_io_tree_panic(tree, err);
608
Chris Masond1310b22008-01-24 16:13:08 -0500609 if (wake)
610 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400611
Jeff Mahoney6763af82012-03-01 14:56:29 +0100612 clear_state_bit(tree, prealloc, &bits, wake);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400613
Chris Masond1310b22008-01-24 16:13:08 -0500614 prealloc = NULL;
615 goto out;
616 }
Chris Mason42daec22009-09-23 19:51:09 -0400617
Li Zefancdc6a392012-03-12 16:39:48 +0800618 state = clear_state_bit(tree, state, &bits, wake);
Liu Bo04493142012-02-16 18:34:37 +0800619next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400620 if (last_end == (u64)-1)
621 goto out;
622 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800623 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800624 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500625 goto search_again;
626
627out:
Chris Masoncad321a2008-12-17 14:51:42 -0500628 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500629 if (prealloc)
630 free_extent_state(prealloc);
631
Jeff Mahoney6763af82012-03-01 14:56:29 +0100632 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500633
634search_again:
635 if (start > end)
636 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500637 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500638 if (mask & __GFP_WAIT)
639 cond_resched();
640 goto again;
641}
Chris Masond1310b22008-01-24 16:13:08 -0500642
Jeff Mahoney143bede2012-03-01 14:56:26 +0100643static void wait_on_state(struct extent_io_tree *tree,
644 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500645 __releases(tree->lock)
646 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500647{
648 DEFINE_WAIT(wait);
649 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500650 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500651 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500652 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500653 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500654}
655
656/*
657 * waits for one or more bits to clear on a range in the state tree.
658 * The range [start, end] is inclusive.
659 * The tree lock is taken by this function
660 */
David Sterba41074882013-04-29 13:38:46 +0000661static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
662 unsigned long bits)
Chris Masond1310b22008-01-24 16:13:08 -0500663{
664 struct extent_state *state;
665 struct rb_node *node;
666
Chris Masoncad321a2008-12-17 14:51:42 -0500667 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500668again:
669 while (1) {
670 /*
671 * this search will find all the extents that end after
672 * our range starts
673 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500674 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500675 if (!node)
676 break;
677
678 state = rb_entry(node, struct extent_state, rb_node);
679
680 if (state->start > end)
681 goto out;
682
683 if (state->state & bits) {
684 start = state->start;
685 atomic_inc(&state->refs);
686 wait_on_state(tree, state);
687 free_extent_state(state);
688 goto again;
689 }
690 start = state->end + 1;
691
692 if (start > end)
693 break;
694
Xiao Guangrongded91f02011-07-14 03:19:27 +0000695 cond_resched_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500696 }
697out:
Chris Masoncad321a2008-12-17 14:51:42 -0500698 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500699}
Chris Masond1310b22008-01-24 16:13:08 -0500700
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000701static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500702 struct extent_state *state,
David Sterba41074882013-04-29 13:38:46 +0000703 unsigned long *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500704{
David Sterba41074882013-04-29 13:38:46 +0000705 unsigned long bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400706
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000707 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400708 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500709 u64 range = state->end - state->start + 1;
710 tree->dirty_bytes += range;
711 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400712 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500713}
714
Chris Mason2c64c532009-09-02 15:04:12 -0400715static void cache_state(struct extent_state *state,
716 struct extent_state **cached_ptr)
717{
718 if (cached_ptr && !(*cached_ptr)) {
719 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
720 *cached_ptr = state;
721 atomic_inc(&state->refs);
722 }
723 }
724}
725
Arne Jansen507903b2011-04-06 10:02:20 +0000726static void uncache_state(struct extent_state **cached_ptr)
727{
728 if (cached_ptr && (*cached_ptr)) {
729 struct extent_state *state = *cached_ptr;
Chris Mason109b36a2011-04-12 13:57:39 -0400730 *cached_ptr = NULL;
731 free_extent_state(state);
Arne Jansen507903b2011-04-06 10:02:20 +0000732 }
733}
734
Chris Masond1310b22008-01-24 16:13:08 -0500735/*
Chris Mason1edbb732009-09-02 13:24:36 -0400736 * set some bits on a range in the tree. This may require allocations or
737 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500738 *
Chris Mason1edbb732009-09-02 13:24:36 -0400739 * If any of the exclusive bits are set, this will fail with -EEXIST if some
740 * part of the range already has the desired bits set. The start of the
741 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500742 *
Chris Mason1edbb732009-09-02 13:24:36 -0400743 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500744 */
Chris Mason1edbb732009-09-02 13:24:36 -0400745
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100746static int __must_check
747__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +0000748 unsigned long bits, unsigned long exclusive_bits,
749 u64 *failed_start, struct extent_state **cached_state,
750 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500751{
752 struct extent_state *state;
753 struct extent_state *prealloc = NULL;
754 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500755 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500756 u64 last_start;
757 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400758
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400759 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500760again:
761 if (!prealloc && (mask & __GFP_WAIT)) {
762 prealloc = alloc_extent_state(mask);
Xiao Guangrong82337672011-04-20 06:44:57 +0000763 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500764 }
765
Chris Masoncad321a2008-12-17 14:51:42 -0500766 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400767 if (cached_state && *cached_state) {
768 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400769 if (state->start <= start && state->end > start &&
770 state->tree) {
Chris Mason9655d292009-09-02 15:22:30 -0400771 node = &state->rb_node;
772 goto hit_next;
773 }
774 }
Chris Masond1310b22008-01-24 16:13:08 -0500775 /*
776 * this search will find all the extents that end after
777 * our range starts.
778 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500779 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500780 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000781 prealloc = alloc_extent_state_atomic(prealloc);
782 BUG_ON(!prealloc);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400783 err = insert_state(tree, prealloc, start, end, &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400784 if (err)
785 extent_io_tree_panic(tree, err);
786
Chris Masond1310b22008-01-24 16:13:08 -0500787 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500788 goto out;
789 }
Chris Masond1310b22008-01-24 16:13:08 -0500790 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400791hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500792 last_start = state->start;
793 last_end = state->end;
794
795 /*
796 * | ---- desired range ---- |
797 * | state |
798 *
799 * Just lock what we found and keep going
800 */
801 if (state->start == start && state->end <= end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400802 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500803 *failed_start = state->start;
804 err = -EEXIST;
805 goto out;
806 }
Chris Mason42daec22009-09-23 19:51:09 -0400807
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000808 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400809 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500810 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400811 if (last_end == (u64)-1)
812 goto out;
813 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800814 state = next_state(state);
815 if (start < end && state && state->start == start &&
816 !need_resched())
817 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500818 goto search_again;
819 }
820
821 /*
822 * | ---- desired range ---- |
823 * | state |
824 * or
825 * | ------------- state -------------- |
826 *
827 * We need to split the extent we found, and may flip bits on
828 * second half.
829 *
830 * If the extent we found extends past our
831 * range, we just split and search again. It'll get split
832 * again the next time though.
833 *
834 * If the extent we found is inside our range, we set the
835 * desired bit on it.
836 */
837 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400838 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500839 *failed_start = start;
840 err = -EEXIST;
841 goto out;
842 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000843
844 prealloc = alloc_extent_state_atomic(prealloc);
845 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500846 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400847 if (err)
848 extent_io_tree_panic(tree, err);
849
Chris Masond1310b22008-01-24 16:13:08 -0500850 prealloc = NULL;
851 if (err)
852 goto out;
853 if (state->end <= end) {
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000854 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400855 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500856 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400857 if (last_end == (u64)-1)
858 goto out;
859 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800860 state = next_state(state);
861 if (start < end && state && state->start == start &&
862 !need_resched())
863 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500864 }
865 goto search_again;
866 }
867 /*
868 * | ---- desired range ---- |
869 * | state | or | state |
870 *
871 * There's a hole, we need to insert something in it and
872 * ignore the extent we found.
873 */
874 if (state->start > start) {
875 u64 this_end;
876 if (end < last_start)
877 this_end = end;
878 else
Chris Masond3977122009-01-05 21:25:51 -0500879 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000880
881 prealloc = alloc_extent_state_atomic(prealloc);
882 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000883
884 /*
885 * Avoid to free 'prealloc' if it can be merged with
886 * the later extent.
887 */
Chris Masond1310b22008-01-24 16:13:08 -0500888 err = insert_state(tree, prealloc, start, this_end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400889 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400890 if (err)
891 extent_io_tree_panic(tree, err);
892
Chris Mason2c64c532009-09-02 15:04:12 -0400893 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500894 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500895 start = this_end + 1;
896 goto search_again;
897 }
898 /*
899 * | ---- desired range ---- |
900 * | state |
901 * We need to split the extent, and set the bit
902 * on the first half
903 */
904 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400905 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500906 *failed_start = start;
907 err = -EEXIST;
908 goto out;
909 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000910
911 prealloc = alloc_extent_state_atomic(prealloc);
912 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500913 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400914 if (err)
915 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -0500916
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000917 set_state_bits(tree, prealloc, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400918 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500919 merge_state(tree, prealloc);
920 prealloc = NULL;
921 goto out;
922 }
923
924 goto search_again;
925
926out:
Chris Masoncad321a2008-12-17 14:51:42 -0500927 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500928 if (prealloc)
929 free_extent_state(prealloc);
930
931 return err;
932
933search_again:
934 if (start > end)
935 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500936 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500937 if (mask & __GFP_WAIT)
938 cond_resched();
939 goto again;
940}
Chris Masond1310b22008-01-24 16:13:08 -0500941
David Sterba41074882013-04-29 13:38:46 +0000942int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
943 unsigned long bits, u64 * failed_start,
944 struct extent_state **cached_state, gfp_t mask)
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100945{
946 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
947 cached_state, mask);
948}
949
950
Josef Bacik462d6fa2011-09-26 13:56:12 -0400951/**
Liu Bo10983f22012-07-11 15:26:19 +0800952 * convert_extent_bit - convert all bits in a given range from one bit to
953 * another
Josef Bacik462d6fa2011-09-26 13:56:12 -0400954 * @tree: the io tree to search
955 * @start: the start offset in bytes
956 * @end: the end offset in bytes (inclusive)
957 * @bits: the bits to set in this range
958 * @clear_bits: the bits to clear in this range
Josef Bacike6138872012-09-27 17:07:30 -0400959 * @cached_state: state that we're going to cache
Josef Bacik462d6fa2011-09-26 13:56:12 -0400960 * @mask: the allocation mask
961 *
962 * This will go through and set bits for the given range. If any states exist
963 * already in this range they are set with the given bit and cleared of the
964 * clear_bits. This is only meant to be used by things that are mergeable, ie
965 * converting from say DELALLOC to DIRTY. This is not meant to be used with
966 * boundary bits like LOCK.
967 */
968int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +0000969 unsigned long bits, unsigned long clear_bits,
Josef Bacike6138872012-09-27 17:07:30 -0400970 struct extent_state **cached_state, gfp_t mask)
Josef Bacik462d6fa2011-09-26 13:56:12 -0400971{
972 struct extent_state *state;
973 struct extent_state *prealloc = NULL;
974 struct rb_node *node;
975 int err = 0;
976 u64 last_start;
977 u64 last_end;
978
979again:
980 if (!prealloc && (mask & __GFP_WAIT)) {
981 prealloc = alloc_extent_state(mask);
982 if (!prealloc)
983 return -ENOMEM;
984 }
985
986 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -0400987 if (cached_state && *cached_state) {
988 state = *cached_state;
989 if (state->start <= start && state->end > start &&
990 state->tree) {
991 node = &state->rb_node;
992 goto hit_next;
993 }
994 }
995
Josef Bacik462d6fa2011-09-26 13:56:12 -0400996 /*
997 * this search will find all the extents that end after
998 * our range starts.
999 */
1000 node = tree_search(tree, start);
1001 if (!node) {
1002 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001003 if (!prealloc) {
1004 err = -ENOMEM;
1005 goto out;
1006 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001007 err = insert_state(tree, prealloc, start, end, &bits);
1008 prealloc = NULL;
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001009 if (err)
1010 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001011 goto out;
1012 }
1013 state = rb_entry(node, struct extent_state, rb_node);
1014hit_next:
1015 last_start = state->start;
1016 last_end = state->end;
1017
1018 /*
1019 * | ---- desired range ---- |
1020 * | state |
1021 *
1022 * Just lock what we found and keep going
1023 */
1024 if (state->start == start && state->end <= end) {
Josef Bacik462d6fa2011-09-26 13:56:12 -04001025 set_state_bits(tree, state, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001026 cache_state(state, cached_state);
Liu Bod1ac6e42012-05-10 18:10:39 +08001027 state = clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001028 if (last_end == (u64)-1)
1029 goto out;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001030 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001031 if (start < end && state && state->start == start &&
1032 !need_resched())
1033 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001034 goto search_again;
1035 }
1036
1037 /*
1038 * | ---- desired range ---- |
1039 * | state |
1040 * or
1041 * | ------------- state -------------- |
1042 *
1043 * We need to split the extent we found, and may flip bits on
1044 * second half.
1045 *
1046 * If the extent we found extends past our
1047 * range, we just split and search again. It'll get split
1048 * again the next time though.
1049 *
1050 * If the extent we found is inside our range, we set the
1051 * desired bit on it.
1052 */
1053 if (state->start < start) {
1054 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001055 if (!prealloc) {
1056 err = -ENOMEM;
1057 goto out;
1058 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001059 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001060 if (err)
1061 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001062 prealloc = NULL;
1063 if (err)
1064 goto out;
1065 if (state->end <= end) {
1066 set_state_bits(tree, state, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001067 cache_state(state, cached_state);
Liu Bod1ac6e42012-05-10 18:10:39 +08001068 state = clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001069 if (last_end == (u64)-1)
1070 goto out;
1071 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001072 if (start < end && state && state->start == start &&
1073 !need_resched())
1074 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001075 }
1076 goto search_again;
1077 }
1078 /*
1079 * | ---- desired range ---- |
1080 * | state | or | state |
1081 *
1082 * There's a hole, we need to insert something in it and
1083 * ignore the extent we found.
1084 */
1085 if (state->start > start) {
1086 u64 this_end;
1087 if (end < last_start)
1088 this_end = end;
1089 else
1090 this_end = last_start - 1;
1091
1092 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001093 if (!prealloc) {
1094 err = -ENOMEM;
1095 goto out;
1096 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001097
1098 /*
1099 * Avoid to free 'prealloc' if it can be merged with
1100 * the later extent.
1101 */
1102 err = insert_state(tree, prealloc, start, this_end,
1103 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001104 if (err)
1105 extent_io_tree_panic(tree, err);
Josef Bacike6138872012-09-27 17:07:30 -04001106 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001107 prealloc = NULL;
1108 start = this_end + 1;
1109 goto search_again;
1110 }
1111 /*
1112 * | ---- desired range ---- |
1113 * | state |
1114 * We need to split the extent, and set the bit
1115 * on the first half
1116 */
1117 if (state->start <= end && state->end > end) {
1118 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001119 if (!prealloc) {
1120 err = -ENOMEM;
1121 goto out;
1122 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001123
1124 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001125 if (err)
1126 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001127
1128 set_state_bits(tree, prealloc, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001129 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001130 clear_state_bit(tree, prealloc, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001131 prealloc = NULL;
1132 goto out;
1133 }
1134
1135 goto search_again;
1136
1137out:
1138 spin_unlock(&tree->lock);
1139 if (prealloc)
1140 free_extent_state(prealloc);
1141
1142 return err;
1143
1144search_again:
1145 if (start > end)
1146 goto out;
1147 spin_unlock(&tree->lock);
1148 if (mask & __GFP_WAIT)
1149 cond_resched();
1150 goto again;
1151}
1152
Chris Masond1310b22008-01-24 16:13:08 -05001153/* wrappers around set/clear extent bit */
1154int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1155 gfp_t mask)
1156{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001157 return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001158 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001159}
Chris Masond1310b22008-01-24 16:13:08 -05001160
1161int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +00001162 unsigned long bits, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001163{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001164 return set_extent_bit(tree, start, end, bits, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001165 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001166}
Chris Masond1310b22008-01-24 16:13:08 -05001167
1168int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +00001169 unsigned long bits, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001170{
Chris Mason2c64c532009-09-02 15:04:12 -04001171 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001172}
Chris Masond1310b22008-01-24 16:13:08 -05001173
1174int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001175 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001176{
1177 return set_extent_bit(tree, start, end,
Liu Bofee187d2011-09-29 15:55:28 +08001178 EXTENT_DELALLOC | EXTENT_UPTODATE,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001179 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001180}
Chris Masond1310b22008-01-24 16:13:08 -05001181
Liu Bo9e8a4a82012-09-05 19:10:51 -06001182int set_extent_defrag(struct extent_io_tree *tree, u64 start, u64 end,
1183 struct extent_state **cached_state, gfp_t mask)
1184{
1185 return set_extent_bit(tree, start, end,
1186 EXTENT_DELALLOC | EXTENT_UPTODATE | EXTENT_DEFRAG,
1187 NULL, cached_state, mask);
1188}
1189
Chris Masond1310b22008-01-24 16:13:08 -05001190int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1191 gfp_t mask)
1192{
1193 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04001194 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001195 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001196}
Chris Masond1310b22008-01-24 16:13:08 -05001197
1198int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1199 gfp_t mask)
1200{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001201 return set_extent_bit(tree, start, end, EXTENT_NEW, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001202 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001203}
Chris Masond1310b22008-01-24 16:13:08 -05001204
Chris Masond1310b22008-01-24 16:13:08 -05001205int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +00001206 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001207{
Liu Bo6b67a322013-03-28 08:30:28 +00001208 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, NULL,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001209 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001210}
Chris Masond1310b22008-01-24 16:13:08 -05001211
Josef Bacik5fd02042012-05-02 14:00:54 -04001212int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1213 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001214{
Chris Mason2c64c532009-09-02 15:04:12 -04001215 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001216 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001217}
Chris Masond1310b22008-01-24 16:13:08 -05001218
Chris Masond352ac62008-09-29 15:18:18 -04001219/*
1220 * either insert or lock state struct between start and end use mask to tell
1221 * us if waiting is desired.
1222 */
Chris Mason1edbb732009-09-02 13:24:36 -04001223int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +00001224 unsigned long bits, struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001225{
1226 int err;
1227 u64 failed_start;
1228 while (1) {
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001229 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
1230 EXTENT_LOCKED, &failed_start,
1231 cached_state, GFP_NOFS);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001232 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001233 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1234 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001235 } else
Chris Masond1310b22008-01-24 16:13:08 -05001236 break;
Chris Masond1310b22008-01-24 16:13:08 -05001237 WARN_ON(start > end);
1238 }
1239 return err;
1240}
Chris Masond1310b22008-01-24 16:13:08 -05001241
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001242int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Mason1edbb732009-09-02 13:24:36 -04001243{
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001244 return lock_extent_bits(tree, start, end, 0, NULL);
Chris Mason1edbb732009-09-02 13:24:36 -04001245}
1246
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001247int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001248{
1249 int err;
1250 u64 failed_start;
1251
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001252 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1253 &failed_start, NULL, GFP_NOFS);
Yan Zheng66435582008-10-30 14:19:50 -04001254 if (err == -EEXIST) {
1255 if (failed_start > start)
1256 clear_extent_bit(tree, start, failed_start - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001257 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
Josef Bacik25179202008-10-29 14:49:05 -04001258 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001259 }
Josef Bacik25179202008-10-29 14:49:05 -04001260 return 1;
1261}
Josef Bacik25179202008-10-29 14:49:05 -04001262
Chris Mason2c64c532009-09-02 15:04:12 -04001263int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1264 struct extent_state **cached, gfp_t mask)
1265{
1266 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1267 mask);
1268}
1269
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001270int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001271{
Chris Mason2c64c532009-09-02 15:04:12 -04001272 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001273 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001274}
Chris Masond1310b22008-01-24 16:13:08 -05001275
Chris Mason4adaa612013-03-26 13:07:00 -04001276int extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
1277{
1278 unsigned long index = start >> PAGE_CACHE_SHIFT;
1279 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1280 struct page *page;
1281
1282 while (index <= end_index) {
1283 page = find_get_page(inode->i_mapping, index);
1284 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1285 clear_page_dirty_for_io(page);
1286 page_cache_release(page);
1287 index++;
1288 }
1289 return 0;
1290}
1291
1292int extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
1293{
1294 unsigned long index = start >> PAGE_CACHE_SHIFT;
1295 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1296 struct page *page;
1297
1298 while (index <= end_index) {
1299 page = find_get_page(inode->i_mapping, index);
1300 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1301 account_page_redirty(page);
1302 __set_page_dirty_nobuffers(page);
1303 page_cache_release(page);
1304 index++;
1305 }
1306 return 0;
1307}
1308
Chris Masond1310b22008-01-24 16:13:08 -05001309/*
Chris Masond1310b22008-01-24 16:13:08 -05001310 * helper function to set both pages and extents in the tree writeback
1311 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001312static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001313{
1314 unsigned long index = start >> PAGE_CACHE_SHIFT;
1315 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1316 struct page *page;
1317
1318 while (index <= end_index) {
1319 page = find_get_page(tree->mapping, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001320 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Masond1310b22008-01-24 16:13:08 -05001321 set_page_writeback(page);
1322 page_cache_release(page);
1323 index++;
1324 }
Chris Masond1310b22008-01-24 16:13:08 -05001325 return 0;
1326}
Chris Masond1310b22008-01-24 16:13:08 -05001327
Chris Masond352ac62008-09-29 15:18:18 -04001328/* find the first state struct with 'bits' set after 'start', and
1329 * return it. tree->lock must be held. NULL will returned if
1330 * nothing was found after 'start'
1331 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001332static struct extent_state *
1333find_first_extent_bit_state(struct extent_io_tree *tree,
David Sterba41074882013-04-29 13:38:46 +00001334 u64 start, unsigned long bits)
Chris Masond7fc6402008-02-18 12:12:38 -05001335{
1336 struct rb_node *node;
1337 struct extent_state *state;
1338
1339 /*
1340 * this search will find all the extents that end after
1341 * our range starts.
1342 */
1343 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001344 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001345 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001346
Chris Masond3977122009-01-05 21:25:51 -05001347 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001348 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001349 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001350 return state;
Chris Masond3977122009-01-05 21:25:51 -05001351
Chris Masond7fc6402008-02-18 12:12:38 -05001352 node = rb_next(node);
1353 if (!node)
1354 break;
1355 }
1356out:
1357 return NULL;
1358}
Chris Masond7fc6402008-02-18 12:12:38 -05001359
Chris Masond352ac62008-09-29 15:18:18 -04001360/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001361 * find the first offset in the io tree with 'bits' set. zero is
1362 * returned if we find something, and *start_ret and *end_ret are
1363 * set to reflect the state struct that was found.
1364 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001365 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001366 */
1367int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
David Sterba41074882013-04-29 13:38:46 +00001368 u64 *start_ret, u64 *end_ret, unsigned long bits,
Josef Bacike6138872012-09-27 17:07:30 -04001369 struct extent_state **cached_state)
Xiao Guangrong69261c42011-07-14 03:19:45 +00001370{
1371 struct extent_state *state;
Josef Bacike6138872012-09-27 17:07:30 -04001372 struct rb_node *n;
Xiao Guangrong69261c42011-07-14 03:19:45 +00001373 int ret = 1;
1374
1375 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001376 if (cached_state && *cached_state) {
1377 state = *cached_state;
1378 if (state->end == start - 1 && state->tree) {
1379 n = rb_next(&state->rb_node);
1380 while (n) {
1381 state = rb_entry(n, struct extent_state,
1382 rb_node);
1383 if (state->state & bits)
1384 goto got_it;
1385 n = rb_next(n);
1386 }
1387 free_extent_state(*cached_state);
1388 *cached_state = NULL;
1389 goto out;
1390 }
1391 free_extent_state(*cached_state);
1392 *cached_state = NULL;
1393 }
1394
Xiao Guangrong69261c42011-07-14 03:19:45 +00001395 state = find_first_extent_bit_state(tree, start, bits);
Josef Bacike6138872012-09-27 17:07:30 -04001396got_it:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001397 if (state) {
Josef Bacike6138872012-09-27 17:07:30 -04001398 cache_state(state, cached_state);
Xiao Guangrong69261c42011-07-14 03:19:45 +00001399 *start_ret = state->start;
1400 *end_ret = state->end;
1401 ret = 0;
1402 }
Josef Bacike6138872012-09-27 17:07:30 -04001403out:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001404 spin_unlock(&tree->lock);
1405 return ret;
1406}
1407
1408/*
Chris Masond352ac62008-09-29 15:18:18 -04001409 * find a contiguous range of bytes in the file marked as delalloc, not
1410 * more than 'max_bytes'. start and end are used to return the range,
1411 *
1412 * 1 is returned if we find something, 0 if nothing was in the tree
1413 */
Chris Masonc8b97812008-10-29 14:49:59 -04001414static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001415 u64 *start, u64 *end, u64 max_bytes,
1416 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001417{
1418 struct rb_node *node;
1419 struct extent_state *state;
1420 u64 cur_start = *start;
1421 u64 found = 0;
1422 u64 total_bytes = 0;
1423
Chris Masoncad321a2008-12-17 14:51:42 -05001424 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001425
Chris Masond1310b22008-01-24 16:13:08 -05001426 /*
1427 * this search will find all the extents that end after
1428 * our range starts.
1429 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001430 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001431 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001432 if (!found)
1433 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001434 goto out;
1435 }
1436
Chris Masond3977122009-01-05 21:25:51 -05001437 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001438 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001439 if (found && (state->start != cur_start ||
1440 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001441 goto out;
1442 }
1443 if (!(state->state & EXTENT_DELALLOC)) {
1444 if (!found)
1445 *end = state->end;
1446 goto out;
1447 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001448 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001449 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001450 *cached_state = state;
1451 atomic_inc(&state->refs);
1452 }
Chris Masond1310b22008-01-24 16:13:08 -05001453 found++;
1454 *end = state->end;
1455 cur_start = state->end + 1;
1456 node = rb_next(node);
1457 if (!node)
1458 break;
1459 total_bytes += state->end - state->start + 1;
1460 if (total_bytes >= max_bytes)
1461 break;
1462 }
1463out:
Chris Masoncad321a2008-12-17 14:51:42 -05001464 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001465 return found;
1466}
1467
Jeff Mahoney143bede2012-03-01 14:56:26 +01001468static noinline void __unlock_for_delalloc(struct inode *inode,
1469 struct page *locked_page,
1470 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001471{
1472 int ret;
1473 struct page *pages[16];
1474 unsigned long index = start >> PAGE_CACHE_SHIFT;
1475 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1476 unsigned long nr_pages = end_index - index + 1;
1477 int i;
1478
1479 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001480 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001481
Chris Masond3977122009-01-05 21:25:51 -05001482 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001483 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001484 min_t(unsigned long, nr_pages,
1485 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001486 for (i = 0; i < ret; i++) {
1487 if (pages[i] != locked_page)
1488 unlock_page(pages[i]);
1489 page_cache_release(pages[i]);
1490 }
1491 nr_pages -= ret;
1492 index += ret;
1493 cond_resched();
1494 }
Chris Masonc8b97812008-10-29 14:49:59 -04001495}
1496
1497static noinline int lock_delalloc_pages(struct inode *inode,
1498 struct page *locked_page,
1499 u64 delalloc_start,
1500 u64 delalloc_end)
1501{
1502 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1503 unsigned long start_index = index;
1504 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1505 unsigned long pages_locked = 0;
1506 struct page *pages[16];
1507 unsigned long nrpages;
1508 int ret;
1509 int i;
1510
1511 /* the caller is responsible for locking the start index */
1512 if (index == locked_page->index && index == end_index)
1513 return 0;
1514
1515 /* skip the page at the start index */
1516 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001517 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001518 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001519 min_t(unsigned long,
1520 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001521 if (ret == 0) {
1522 ret = -EAGAIN;
1523 goto done;
1524 }
1525 /* now we have an array of pages, lock them all */
1526 for (i = 0; i < ret; i++) {
1527 /*
1528 * the caller is taking responsibility for
1529 * locked_page
1530 */
Chris Mason771ed682008-11-06 22:02:51 -05001531 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001532 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001533 if (!PageDirty(pages[i]) ||
1534 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001535 ret = -EAGAIN;
1536 unlock_page(pages[i]);
1537 page_cache_release(pages[i]);
1538 goto done;
1539 }
1540 }
Chris Masonc8b97812008-10-29 14:49:59 -04001541 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001542 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001543 }
Chris Masonc8b97812008-10-29 14:49:59 -04001544 nrpages -= ret;
1545 index += ret;
1546 cond_resched();
1547 }
1548 ret = 0;
1549done:
1550 if (ret && pages_locked) {
1551 __unlock_for_delalloc(inode, locked_page,
1552 delalloc_start,
1553 ((u64)(start_index + pages_locked - 1)) <<
1554 PAGE_CACHE_SHIFT);
1555 }
1556 return ret;
1557}
1558
1559/*
1560 * find a contiguous range of bytes in the file marked as delalloc, not
1561 * more than 'max_bytes'. start and end are used to return the range,
1562 *
1563 * 1 is returned if we find something, 0 if nothing was in the tree
1564 */
1565static noinline u64 find_lock_delalloc_range(struct inode *inode,
1566 struct extent_io_tree *tree,
1567 struct page *locked_page,
1568 u64 *start, u64 *end,
1569 u64 max_bytes)
1570{
1571 u64 delalloc_start;
1572 u64 delalloc_end;
1573 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001574 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001575 int ret;
1576 int loops = 0;
1577
1578again:
1579 /* step one, find a bunch of delalloc bytes starting at start */
1580 delalloc_start = *start;
1581 delalloc_end = 0;
1582 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001583 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001584 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001585 *start = delalloc_start;
1586 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001587 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001588 return found;
1589 }
1590
1591 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001592 * start comes from the offset of locked_page. We have to lock
1593 * pages in order, so we can't process delalloc bytes before
1594 * locked_page
1595 */
Chris Masond3977122009-01-05 21:25:51 -05001596 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001597 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001598
1599 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001600 * make sure to limit the number of pages we try to lock down
1601 * if we're looping.
1602 */
Chris Masond3977122009-01-05 21:25:51 -05001603 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001604 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001605
Chris Masonc8b97812008-10-29 14:49:59 -04001606 /* step two, lock all the pages after the page that has start */
1607 ret = lock_delalloc_pages(inode, locked_page,
1608 delalloc_start, delalloc_end);
1609 if (ret == -EAGAIN) {
1610 /* some of the pages are gone, lets avoid looping by
1611 * shortening the size of the delalloc range we're searching
1612 */
Chris Mason9655d292009-09-02 15:22:30 -04001613 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001614 if (!loops) {
1615 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1616 max_bytes = PAGE_CACHE_SIZE - offset;
1617 loops = 1;
1618 goto again;
1619 } else {
1620 found = 0;
1621 goto out_failed;
1622 }
1623 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001624 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
Chris Masonc8b97812008-10-29 14:49:59 -04001625
1626 /* step three, lock the state bits for the whole range */
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001627 lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001628
1629 /* then test to make sure it is all still delalloc */
1630 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001631 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001632 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001633 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1634 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001635 __unlock_for_delalloc(inode, locked_page,
1636 delalloc_start, delalloc_end);
1637 cond_resched();
1638 goto again;
1639 }
Chris Mason9655d292009-09-02 15:22:30 -04001640 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001641 *start = delalloc_start;
1642 *end = delalloc_end;
1643out_failed:
1644 return found;
1645}
1646
1647int extent_clear_unlock_delalloc(struct inode *inode,
1648 struct extent_io_tree *tree,
1649 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001650 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001651{
1652 int ret;
1653 struct page *pages[16];
1654 unsigned long index = start >> PAGE_CACHE_SHIFT;
1655 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1656 unsigned long nr_pages = end_index - index + 1;
1657 int i;
David Sterba41074882013-04-29 13:38:46 +00001658 unsigned long clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001659
Chris Masona791e352009-10-08 11:27:10 -04001660 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001661 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001662 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001663 clear_bits |= EXTENT_DIRTY;
1664
Chris Masona791e352009-10-08 11:27:10 -04001665 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001666 clear_bits |= EXTENT_DELALLOC;
1667
Chris Mason2c64c532009-09-02 15:04:12 -04001668 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001669 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1670 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1671 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001672 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001673
Chris Masond3977122009-01-05 21:25:51 -05001674 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001675 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001676 min_t(unsigned long,
1677 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001678 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001679
Chris Masona791e352009-10-08 11:27:10 -04001680 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001681 SetPagePrivate2(pages[i]);
1682
Chris Masonc8b97812008-10-29 14:49:59 -04001683 if (pages[i] == locked_page) {
1684 page_cache_release(pages[i]);
1685 continue;
1686 }
Chris Masona791e352009-10-08 11:27:10 -04001687 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001688 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001689 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001690 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001691 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001692 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001693 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001694 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001695 page_cache_release(pages[i]);
1696 }
1697 nr_pages -= ret;
1698 index += ret;
1699 cond_resched();
1700 }
1701 return 0;
1702}
Chris Masonc8b97812008-10-29 14:49:59 -04001703
Chris Masond352ac62008-09-29 15:18:18 -04001704/*
1705 * count the number of bytes in the tree that have a given bit(s)
1706 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1707 * cached. The total number found is returned.
1708 */
Chris Masond1310b22008-01-24 16:13:08 -05001709u64 count_range_bits(struct extent_io_tree *tree,
1710 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001711 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001712{
1713 struct rb_node *node;
1714 struct extent_state *state;
1715 u64 cur_start = *start;
1716 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001717 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001718 int found = 0;
1719
1720 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001721 WARN_ON(1);
1722 return 0;
1723 }
1724
Chris Masoncad321a2008-12-17 14:51:42 -05001725 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001726 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1727 total_bytes = tree->dirty_bytes;
1728 goto out;
1729 }
1730 /*
1731 * this search will find all the extents that end after
1732 * our range starts.
1733 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001734 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001735 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001736 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001737
Chris Masond3977122009-01-05 21:25:51 -05001738 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001739 state = rb_entry(node, struct extent_state, rb_node);
1740 if (state->start > search_end)
1741 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001742 if (contig && found && state->start > last + 1)
1743 break;
1744 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001745 total_bytes += min(search_end, state->end) + 1 -
1746 max(cur_start, state->start);
1747 if (total_bytes >= max_bytes)
1748 break;
1749 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001750 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001751 found = 1;
1752 }
Chris Masonec29ed52011-02-23 16:23:20 -05001753 last = state->end;
1754 } else if (contig && found) {
1755 break;
Chris Masond1310b22008-01-24 16:13:08 -05001756 }
1757 node = rb_next(node);
1758 if (!node)
1759 break;
1760 }
1761out:
Chris Masoncad321a2008-12-17 14:51:42 -05001762 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001763 return total_bytes;
1764}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001765
Chris Masond352ac62008-09-29 15:18:18 -04001766/*
1767 * set the private field for a given byte offset in the tree. If there isn't
1768 * an extent_state there already, this does nothing.
1769 */
Chris Masond1310b22008-01-24 16:13:08 -05001770int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1771{
1772 struct rb_node *node;
1773 struct extent_state *state;
1774 int ret = 0;
1775
Chris Masoncad321a2008-12-17 14:51:42 -05001776 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001777 /*
1778 * this search will find all the extents that end after
1779 * our range starts.
1780 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001781 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001782 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001783 ret = -ENOENT;
1784 goto out;
1785 }
1786 state = rb_entry(node, struct extent_state, rb_node);
1787 if (state->start != start) {
1788 ret = -ENOENT;
1789 goto out;
1790 }
1791 state->private = private;
1792out:
Chris Masoncad321a2008-12-17 14:51:42 -05001793 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001794 return ret;
1795}
1796
Miao Xiee4100d92013-04-05 07:20:56 +00001797void extent_cache_csums_dio(struct extent_io_tree *tree, u64 start, u32 csums[],
1798 int count)
1799{
1800 struct rb_node *node;
1801 struct extent_state *state;
1802
1803 spin_lock(&tree->lock);
1804 /*
1805 * this search will find all the extents that end after
1806 * our range starts.
1807 */
1808 node = tree_search(tree, start);
1809 BUG_ON(!node);
1810
1811 state = rb_entry(node, struct extent_state, rb_node);
1812 BUG_ON(state->start != start);
1813
1814 while (count) {
1815 state->private = *csums++;
1816 count--;
1817 state = next_state(state);
1818 }
1819 spin_unlock(&tree->lock);
1820}
1821
1822static inline u64 __btrfs_get_bio_offset(struct bio *bio, int bio_index)
1823{
1824 struct bio_vec *bvec = bio->bi_io_vec + bio_index;
1825
1826 return page_offset(bvec->bv_page) + bvec->bv_offset;
1827}
1828
1829void extent_cache_csums(struct extent_io_tree *tree, struct bio *bio, int bio_index,
1830 u32 csums[], int count)
1831{
1832 struct rb_node *node;
1833 struct extent_state *state = NULL;
1834 u64 start;
1835
1836 spin_lock(&tree->lock);
1837 do {
1838 start = __btrfs_get_bio_offset(bio, bio_index);
1839 if (state == NULL || state->start != start) {
1840 node = tree_search(tree, start);
1841 BUG_ON(!node);
1842
1843 state = rb_entry(node, struct extent_state, rb_node);
1844 BUG_ON(state->start != start);
1845 }
1846 state->private = *csums++;
1847 count--;
1848 bio_index++;
1849
1850 state = next_state(state);
1851 } while (count);
1852 spin_unlock(&tree->lock);
1853}
1854
Chris Masond1310b22008-01-24 16:13:08 -05001855int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1856{
1857 struct rb_node *node;
1858 struct extent_state *state;
1859 int ret = 0;
1860
Chris Masoncad321a2008-12-17 14:51:42 -05001861 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001862 /*
1863 * this search will find all the extents that end after
1864 * our range starts.
1865 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001866 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001867 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001868 ret = -ENOENT;
1869 goto out;
1870 }
1871 state = rb_entry(node, struct extent_state, rb_node);
1872 if (state->start != start) {
1873 ret = -ENOENT;
1874 goto out;
1875 }
1876 *private = state->private;
1877out:
Chris Masoncad321a2008-12-17 14:51:42 -05001878 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001879 return ret;
1880}
1881
1882/*
1883 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001884 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001885 * has the bits set. Otherwise, 1 is returned if any bit in the
1886 * range is found set.
1887 */
1888int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +00001889 unsigned long bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001890{
1891 struct extent_state *state = NULL;
1892 struct rb_node *node;
1893 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001894
Chris Masoncad321a2008-12-17 14:51:42 -05001895 spin_lock(&tree->lock);
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001896 if (cached && cached->tree && cached->start <= start &&
1897 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001898 node = &cached->rb_node;
1899 else
1900 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001901 while (node && start <= end) {
1902 state = rb_entry(node, struct extent_state, rb_node);
1903
1904 if (filled && state->start > start) {
1905 bitset = 0;
1906 break;
1907 }
1908
1909 if (state->start > end)
1910 break;
1911
1912 if (state->state & bits) {
1913 bitset = 1;
1914 if (!filled)
1915 break;
1916 } else if (filled) {
1917 bitset = 0;
1918 break;
1919 }
Chris Mason46562cec2009-09-23 20:23:16 -04001920
1921 if (state->end == (u64)-1)
1922 break;
1923
Chris Masond1310b22008-01-24 16:13:08 -05001924 start = state->end + 1;
1925 if (start > end)
1926 break;
1927 node = rb_next(node);
1928 if (!node) {
1929 if (filled)
1930 bitset = 0;
1931 break;
1932 }
1933 }
Chris Masoncad321a2008-12-17 14:51:42 -05001934 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001935 return bitset;
1936}
Chris Masond1310b22008-01-24 16:13:08 -05001937
1938/*
1939 * helper function to set a given page up to date if all the
1940 * extents in the tree for that page are up to date
1941 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001942static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001943{
Miao Xie4eee4fa2012-12-21 09:17:45 +00001944 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05001945 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001946 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001947 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001948}
1949
1950/*
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001951 * When IO fails, either with EIO or csum verification fails, we
1952 * try other mirrors that might have a good copy of the data. This
1953 * io_failure_record is used to record state as we go through all the
1954 * mirrors. If another mirror has good data, the page is set up to date
1955 * and things continue. If a good mirror can't be found, the original
1956 * bio end_io callback is called to indicate things have failed.
1957 */
1958struct io_failure_record {
1959 struct page *page;
1960 u64 start;
1961 u64 len;
1962 u64 logical;
1963 unsigned long bio_flags;
1964 int this_mirror;
1965 int failed_mirror;
1966 int in_validation;
1967};
1968
1969static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
1970 int did_repair)
1971{
1972 int ret;
1973 int err = 0;
1974 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1975
1976 set_state_private(failure_tree, rec->start, 0);
1977 ret = clear_extent_bits(failure_tree, rec->start,
1978 rec->start + rec->len - 1,
1979 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1980 if (ret)
1981 err = ret;
1982
David Woodhouse53b381b2013-01-29 18:40:14 -05001983 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1984 rec->start + rec->len - 1,
1985 EXTENT_DAMAGED, GFP_NOFS);
1986 if (ret && !err)
1987 err = ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001988
1989 kfree(rec);
1990 return err;
1991}
1992
1993static void repair_io_failure_callback(struct bio *bio, int err)
1994{
1995 complete(bio->bi_private);
1996}
1997
1998/*
1999 * this bypasses the standard btrfs submit functions deliberately, as
2000 * the standard behavior is to write all copies in a raid setup. here we only
2001 * want to write the one bad copy. so we do the mapping for ourselves and issue
2002 * submit_bio directly.
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002003 * to avoid any synchronization issues, wait for the data after writing, which
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002004 * actually prevents the read that triggered the error from finishing.
2005 * currently, there can be no more than two copies of every data bit. thus,
2006 * exactly one rewrite is required.
2007 */
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002008int repair_io_failure(struct btrfs_fs_info *fs_info, u64 start,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002009 u64 length, u64 logical, struct page *page,
2010 int mirror_num)
2011{
2012 struct bio *bio;
2013 struct btrfs_device *dev;
2014 DECLARE_COMPLETION_ONSTACK(compl);
2015 u64 map_length = 0;
2016 u64 sector;
2017 struct btrfs_bio *bbio = NULL;
David Woodhouse53b381b2013-01-29 18:40:14 -05002018 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002019 int ret;
2020
2021 BUG_ON(!mirror_num);
2022
David Woodhouse53b381b2013-01-29 18:40:14 -05002023 /* we can't repair anything in raid56 yet */
2024 if (btrfs_is_parity_mirror(map_tree, logical, length, mirror_num))
2025 return 0;
2026
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002027 bio = bio_alloc(GFP_NOFS, 1);
2028 if (!bio)
2029 return -EIO;
2030 bio->bi_private = &compl;
2031 bio->bi_end_io = repair_io_failure_callback;
2032 bio->bi_size = 0;
2033 map_length = length;
2034
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002035 ret = btrfs_map_block(fs_info, WRITE, logical,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002036 &map_length, &bbio, mirror_num);
2037 if (ret) {
2038 bio_put(bio);
2039 return -EIO;
2040 }
2041 BUG_ON(mirror_num != bbio->mirror_num);
2042 sector = bbio->stripes[mirror_num-1].physical >> 9;
2043 bio->bi_sector = sector;
2044 dev = bbio->stripes[mirror_num-1].dev;
2045 kfree(bbio);
2046 if (!dev || !dev->bdev || !dev->writeable) {
2047 bio_put(bio);
2048 return -EIO;
2049 }
2050 bio->bi_bdev = dev->bdev;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002051 bio_add_page(bio, page, length, start - page_offset(page));
Stefan Behrens21adbd52011-11-09 13:44:05 +01002052 btrfsic_submit_bio(WRITE_SYNC, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002053 wait_for_completion(&compl);
2054
2055 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2056 /* try to remap that extent elsewhere? */
2057 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002058 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002059 return -EIO;
2060 }
2061
Anand Jaind5b025d2012-07-02 22:05:21 -06002062 printk_ratelimited_in_rcu(KERN_INFO "btrfs read error corrected: ino %lu off %llu "
Josef Bacik606686e2012-06-04 14:03:51 -04002063 "(dev %s sector %llu)\n", page->mapping->host->i_ino,
2064 start, rcu_str_deref(dev->name), sector);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002065
2066 bio_put(bio);
2067 return 0;
2068}
2069
Josef Bacikea466792012-03-26 21:57:36 -04002070int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
2071 int mirror_num)
2072{
Josef Bacikea466792012-03-26 21:57:36 -04002073 u64 start = eb->start;
2074 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond95603b2012-04-12 15:55:15 -04002075 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04002076
2077 for (i = 0; i < num_pages; i++) {
2078 struct page *p = extent_buffer_page(eb, i);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002079 ret = repair_io_failure(root->fs_info, start, PAGE_CACHE_SIZE,
Josef Bacikea466792012-03-26 21:57:36 -04002080 start, p, mirror_num);
2081 if (ret)
2082 break;
2083 start += PAGE_CACHE_SIZE;
2084 }
2085
2086 return ret;
2087}
2088
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002089/*
2090 * each time an IO finishes, we do a fast check in the IO failure tree
2091 * to see if we need to process or clean up an io_failure_record
2092 */
2093static int clean_io_failure(u64 start, struct page *page)
2094{
2095 u64 private;
2096 u64 private_failure;
2097 struct io_failure_record *failrec;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002098 struct btrfs_fs_info *fs_info;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002099 struct extent_state *state;
2100 int num_copies;
2101 int did_repair = 0;
2102 int ret;
2103 struct inode *inode = page->mapping->host;
2104
2105 private = 0;
2106 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
2107 (u64)-1, 1, EXTENT_DIRTY, 0);
2108 if (!ret)
2109 return 0;
2110
2111 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
2112 &private_failure);
2113 if (ret)
2114 return 0;
2115
2116 failrec = (struct io_failure_record *)(unsigned long) private_failure;
2117 BUG_ON(!failrec->this_mirror);
2118
2119 if (failrec->in_validation) {
2120 /* there was no real error, just free the record */
2121 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
2122 failrec->start);
2123 did_repair = 1;
2124 goto out;
2125 }
2126
2127 spin_lock(&BTRFS_I(inode)->io_tree.lock);
2128 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
2129 failrec->start,
2130 EXTENT_LOCKED);
2131 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
2132
2133 if (state && state->start == failrec->start) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002134 fs_info = BTRFS_I(inode)->root->fs_info;
2135 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2136 failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002137 if (num_copies > 1) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002138 ret = repair_io_failure(fs_info, start, failrec->len,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002139 failrec->logical, page,
2140 failrec->failed_mirror);
2141 did_repair = !ret;
2142 }
David Woodhouse53b381b2013-01-29 18:40:14 -05002143 ret = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002144 }
2145
2146out:
2147 if (!ret)
2148 ret = free_io_failure(inode, failrec, did_repair);
2149
2150 return ret;
2151}
2152
2153/*
2154 * this is a generic handler for readpage errors (default
2155 * readpage_io_failed_hook). if other copies exist, read those and write back
2156 * good data to the failed position. does not investigate in remapping the
2157 * failed extent elsewhere, hoping the device will be smart enough to do this as
2158 * needed
2159 */
2160
2161static int bio_readpage_error(struct bio *failed_bio, struct page *page,
2162 u64 start, u64 end, int failed_mirror,
2163 struct extent_state *state)
2164{
2165 struct io_failure_record *failrec = NULL;
2166 u64 private;
2167 struct extent_map *em;
2168 struct inode *inode = page->mapping->host;
2169 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2170 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2171 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2172 struct bio *bio;
2173 int num_copies;
2174 int ret;
2175 int read_mode;
2176 u64 logical;
2177
2178 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2179
2180 ret = get_state_private(failure_tree, start, &private);
2181 if (ret) {
2182 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2183 if (!failrec)
2184 return -ENOMEM;
2185 failrec->start = start;
2186 failrec->len = end - start + 1;
2187 failrec->this_mirror = 0;
2188 failrec->bio_flags = 0;
2189 failrec->in_validation = 0;
2190
2191 read_lock(&em_tree->lock);
2192 em = lookup_extent_mapping(em_tree, start, failrec->len);
2193 if (!em) {
2194 read_unlock(&em_tree->lock);
2195 kfree(failrec);
2196 return -EIO;
2197 }
2198
2199 if (em->start > start || em->start + em->len < start) {
2200 free_extent_map(em);
2201 em = NULL;
2202 }
2203 read_unlock(&em_tree->lock);
2204
Tsutomu Itoh7a2d6a62012-10-01 03:07:15 -06002205 if (!em) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002206 kfree(failrec);
2207 return -EIO;
2208 }
2209 logical = start - em->start;
2210 logical = em->block_start + logical;
2211 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2212 logical = em->block_start;
2213 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2214 extent_set_compress_type(&failrec->bio_flags,
2215 em->compress_type);
2216 }
2217 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2218 "len=%llu\n", logical, start, failrec->len);
2219 failrec->logical = logical;
2220 free_extent_map(em);
2221
2222 /* set the bits in the private failure tree */
2223 ret = set_extent_bits(failure_tree, start, end,
2224 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2225 if (ret >= 0)
2226 ret = set_state_private(failure_tree, start,
2227 (u64)(unsigned long)failrec);
2228 /* set the bits in the inode's tree */
2229 if (ret >= 0)
2230 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2231 GFP_NOFS);
2232 if (ret < 0) {
2233 kfree(failrec);
2234 return ret;
2235 }
2236 } else {
2237 failrec = (struct io_failure_record *)(unsigned long)private;
2238 pr_debug("bio_readpage_error: (found) logical=%llu, "
2239 "start=%llu, len=%llu, validation=%d\n",
2240 failrec->logical, failrec->start, failrec->len,
2241 failrec->in_validation);
2242 /*
2243 * when data can be on disk more than twice, add to failrec here
2244 * (e.g. with a list for failed_mirror) to make
2245 * clean_io_failure() clean all those errors at once.
2246 */
2247 }
Stefan Behrens5d964052012-11-05 14:59:07 +01002248 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
2249 failrec->logical, failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002250 if (num_copies == 1) {
2251 /*
2252 * we only have a single copy of the data, so don't bother with
2253 * all the retry and error correction code that follows. no
2254 * matter what the error is, it is very likely to persist.
2255 */
2256 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2257 "state=%p, num_copies=%d, next_mirror %d, "
2258 "failed_mirror %d\n", state, num_copies,
2259 failrec->this_mirror, failed_mirror);
2260 free_io_failure(inode, failrec, 0);
2261 return -EIO;
2262 }
2263
2264 if (!state) {
2265 spin_lock(&tree->lock);
2266 state = find_first_extent_bit_state(tree, failrec->start,
2267 EXTENT_LOCKED);
2268 if (state && state->start != failrec->start)
2269 state = NULL;
2270 spin_unlock(&tree->lock);
2271 }
2272
2273 /*
2274 * there are two premises:
2275 * a) deliver good data to the caller
2276 * b) correct the bad sectors on disk
2277 */
2278 if (failed_bio->bi_vcnt > 1) {
2279 /*
2280 * to fulfill b), we need to know the exact failing sectors, as
2281 * we don't want to rewrite any more than the failed ones. thus,
2282 * we need separate read requests for the failed bio
2283 *
2284 * if the following BUG_ON triggers, our validation request got
2285 * merged. we need separate requests for our algorithm to work.
2286 */
2287 BUG_ON(failrec->in_validation);
2288 failrec->in_validation = 1;
2289 failrec->this_mirror = failed_mirror;
2290 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2291 } else {
2292 /*
2293 * we're ready to fulfill a) and b) alongside. get a good copy
2294 * of the failed sector and if we succeed, we have setup
2295 * everything for repair_io_failure to do the rest for us.
2296 */
2297 if (failrec->in_validation) {
2298 BUG_ON(failrec->this_mirror != failed_mirror);
2299 failrec->in_validation = 0;
2300 failrec->this_mirror = 0;
2301 }
2302 failrec->failed_mirror = failed_mirror;
2303 failrec->this_mirror++;
2304 if (failrec->this_mirror == failed_mirror)
2305 failrec->this_mirror++;
2306 read_mode = READ_SYNC;
2307 }
2308
2309 if (!state || failrec->this_mirror > num_copies) {
2310 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2311 "next_mirror %d, failed_mirror %d\n", state,
2312 num_copies, failrec->this_mirror, failed_mirror);
2313 free_io_failure(inode, failrec, 0);
2314 return -EIO;
2315 }
2316
2317 bio = bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04002318 if (!bio) {
2319 free_io_failure(inode, failrec, 0);
2320 return -EIO;
2321 }
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002322 bio->bi_private = state;
2323 bio->bi_end_io = failed_bio->bi_end_io;
2324 bio->bi_sector = failrec->logical >> 9;
2325 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2326 bio->bi_size = 0;
2327
2328 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2329
2330 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2331 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2332 failrec->this_mirror, num_copies, failrec->in_validation);
2333
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002334 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2335 failrec->this_mirror,
2336 failrec->bio_flags, 0);
2337 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002338}
2339
Chris Masond1310b22008-01-24 16:13:08 -05002340/* lots and lots of room for performance fixes in the end_bio funcs */
2341
Jeff Mahoney87826df2012-02-15 16:23:57 +01002342int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2343{
2344 int uptodate = (err == 0);
2345 struct extent_io_tree *tree;
2346 int ret;
2347
2348 tree = &BTRFS_I(page->mapping->host)->io_tree;
2349
2350 if (tree->ops && tree->ops->writepage_end_io_hook) {
2351 ret = tree->ops->writepage_end_io_hook(page, start,
2352 end, NULL, uptodate);
2353 if (ret)
2354 uptodate = 0;
2355 }
2356
Jeff Mahoney87826df2012-02-15 16:23:57 +01002357 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002358 ClearPageUptodate(page);
2359 SetPageError(page);
2360 }
2361 return 0;
2362}
2363
Chris Masond1310b22008-01-24 16:13:08 -05002364/*
2365 * after a writepage IO is done, we need to:
2366 * clear the uptodate bits on error
2367 * clear the writeback bits in the extent tree for this IO
2368 * end_page_writeback if the page has no more pending IO
2369 *
2370 * Scheduling is not allowed, so the extent state tree is expected
2371 * to have one and only one object corresponding to this IO.
2372 */
Chris Masond1310b22008-01-24 16:13:08 -05002373static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002374{
Chris Masond1310b22008-01-24 16:13:08 -05002375 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04002376 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002377 u64 start;
2378 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05002379
Chris Masond1310b22008-01-24 16:13:08 -05002380 do {
2381 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04002382 tree = &BTRFS_I(page->mapping->host)->io_tree;
2383
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002384 /* We always issue full-page reads, but if some block
2385 * in a page fails to read, blk_update_request() will
2386 * advance bv_offset and adjust bv_len to compensate.
2387 * Print a warning for nonzero offsets, and an error
2388 * if they don't add up to a full page. */
2389 if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE)
2390 printk("%s page write in btrfs with offset %u and length %u\n",
2391 bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE
2392 ? KERN_ERR "partial" : KERN_INFO "incomplete",
2393 bvec->bv_offset, bvec->bv_len);
Chris Masond1310b22008-01-24 16:13:08 -05002394
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002395 start = page_offset(page);
2396 end = start + bvec->bv_offset + bvec->bv_len - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002397
2398 if (--bvec >= bio->bi_io_vec)
2399 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04002400
Jeff Mahoney87826df2012-02-15 16:23:57 +01002401 if (end_extent_writepage(page, err, start, end))
2402 continue;
Chris Mason70dec802008-01-29 09:59:12 -05002403
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002404 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002405 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04002406
Chris Masond1310b22008-01-24 16:13:08 -05002407 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002408}
2409
2410/*
2411 * after a readpage IO is done, we need to:
2412 * clear the uptodate bits on error
2413 * set the uptodate bits if things worked
2414 * set the page up to date if all extents in the tree are uptodate
2415 * clear the lock bit in the extent tree
2416 * unlock the page if there are no other extents locked for it
2417 *
2418 * Scheduling is not allowed, so the extent state tree is expected
2419 * to have one and only one object corresponding to this IO.
2420 */
Chris Masond1310b22008-01-24 16:13:08 -05002421static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002422{
2423 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00002424 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2425 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04002426 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002427 u64 start;
2428 u64 end;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002429 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002430 int ret;
2431
Chris Masond20f7042008-12-08 16:58:54 -05002432 if (err)
2433 uptodate = 0;
2434
Chris Masond1310b22008-01-24 16:13:08 -05002435 do {
2436 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00002437 struct extent_state *cached = NULL;
2438 struct extent_state *state;
2439
Kent Overstreetbe3940c2012-09-11 14:23:05 -06002440 pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, "
2441 "mirror=%ld\n", (u64)bio->bi_sector, err,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002442 (long int)bio->bi_bdev);
David Woodhouse902b22f2008-08-20 08:51:49 -04002443 tree = &BTRFS_I(page->mapping->host)->io_tree;
2444
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002445 /* We always issue full-page reads, but if some block
2446 * in a page fails to read, blk_update_request() will
2447 * advance bv_offset and adjust bv_len to compensate.
2448 * Print a warning for nonzero offsets, and an error
2449 * if they don't add up to a full page. */
2450 if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE)
2451 printk("%s page read in btrfs with offset %u and length %u\n",
2452 bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE
2453 ? KERN_ERR "partial" : KERN_INFO "incomplete",
2454 bvec->bv_offset, bvec->bv_len);
Chris Masond1310b22008-01-24 16:13:08 -05002455
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002456 start = page_offset(page);
2457 end = start + bvec->bv_offset + bvec->bv_len - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002458
Chris Mason4125bf72010-02-03 18:18:45 +00002459 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05002460 prefetchw(&bvec->bv_page->flags);
2461
Arne Jansen507903b2011-04-06 10:02:20 +00002462 spin_lock(&tree->lock);
Chris Mason0d399202011-04-16 06:55:39 -04002463 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
Chris Mason109b36a2011-04-12 13:57:39 -04002464 if (state && state->start == start) {
Arne Jansen507903b2011-04-06 10:02:20 +00002465 /*
2466 * take a reference on the state, unlock will drop
2467 * the ref
2468 */
2469 cache_state(state, &cached);
2470 }
2471 spin_unlock(&tree->lock);
2472
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002473 mirror = (int)(unsigned long)bio->bi_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002474 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05002475 ret = tree->ops->readpage_end_io_hook(page, start, end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002476 state, mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002477 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002478 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002479 else
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002480 clean_io_failure(start, page);
Chris Masond1310b22008-01-24 16:13:08 -05002481 }
Josef Bacikea466792012-03-26 21:57:36 -04002482
Josef Bacikea466792012-03-26 21:57:36 -04002483 if (!uptodate && tree->ops && tree->ops->readpage_io_failed_hook) {
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002484 ret = tree->ops->readpage_io_failed_hook(page, mirror);
Josef Bacikea466792012-03-26 21:57:36 -04002485 if (!ret && !err &&
2486 test_bit(BIO_UPTODATE, &bio->bi_flags))
2487 uptodate = 1;
2488 } else if (!uptodate) {
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002489 /*
2490 * The generic bio_readpage_error handles errors the
2491 * following way: If possible, new read requests are
2492 * created and submitted and will end up in
2493 * end_bio_extent_readpage as well (if we're lucky, not
2494 * in the !uptodate case). In that case it returns 0 and
2495 * we just go on with the next page in our bio. If it
2496 * can't handle the error it will return -EIO and we
2497 * remain responsible for that page.
2498 */
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002499 ret = bio_readpage_error(bio, page, start, end, mirror, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04002500 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04002501 uptodate =
2502 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05002503 if (err)
2504 uptodate = 0;
Arne Jansen507903b2011-04-06 10:02:20 +00002505 uncache_state(&cached);
Chris Mason7e383262008-04-09 16:28:12 -04002506 continue;
2507 }
2508 }
Chris Mason70dec802008-01-29 09:59:12 -05002509
Josef Bacik0b32f4b2012-03-13 09:38:00 -04002510 if (uptodate && tree->track_uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00002511 set_extent_uptodate(tree, start, end, &cached,
David Woodhouse902b22f2008-08-20 08:51:49 -04002512 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05002513 }
Arne Jansen507903b2011-04-06 10:02:20 +00002514 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05002515
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002516 if (uptodate) {
2517 SetPageUptodate(page);
Chris Mason70dec802008-01-29 09:59:12 -05002518 } else {
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002519 ClearPageUptodate(page);
2520 SetPageError(page);
Chris Mason70dec802008-01-29 09:59:12 -05002521 }
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002522 unlock_page(page);
Chris Mason4125bf72010-02-03 18:18:45 +00002523 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05002524
2525 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002526}
2527
Miao Xie88f794e2010-11-22 03:02:55 +00002528struct bio *
2529btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2530 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002531{
2532 struct bio *bio;
2533
2534 bio = bio_alloc(gfp_flags, nr_vecs);
2535
2536 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2537 while (!bio && (nr_vecs /= 2))
2538 bio = bio_alloc(gfp_flags, nr_vecs);
2539 }
2540
2541 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04002542 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002543 bio->bi_bdev = bdev;
2544 bio->bi_sector = first_sector;
2545 }
2546 return bio;
2547}
2548
Jeff Mahoney355808c2011-10-03 23:23:14 -04002549static int __must_check submit_one_bio(int rw, struct bio *bio,
2550 int mirror_num, unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002551{
Chris Masond1310b22008-01-24 16:13:08 -05002552 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002553 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2554 struct page *page = bvec->bv_page;
2555 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002556 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002557
Miao Xie4eee4fa2012-12-21 09:17:45 +00002558 start = page_offset(page) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002559
David Woodhouse902b22f2008-08-20 08:51:49 -04002560 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002561
2562 bio_get(bio);
2563
Chris Mason065631f2008-02-20 12:07:25 -05002564 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00002565 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002566 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002567 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01002568 btrfsic_submit_bio(rw, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002569
Chris Masond1310b22008-01-24 16:13:08 -05002570 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2571 ret = -EOPNOTSUPP;
2572 bio_put(bio);
2573 return ret;
2574}
2575
David Woodhouse64a16702009-07-15 23:29:37 +01002576static int merge_bio(int rw, struct extent_io_tree *tree, struct page *page,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002577 unsigned long offset, size_t size, struct bio *bio,
2578 unsigned long bio_flags)
2579{
2580 int ret = 0;
2581 if (tree->ops && tree->ops->merge_bio_hook)
David Woodhouse64a16702009-07-15 23:29:37 +01002582 ret = tree->ops->merge_bio_hook(rw, page, offset, size, bio,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002583 bio_flags);
2584 BUG_ON(ret < 0);
2585 return ret;
2586
2587}
2588
Chris Masond1310b22008-01-24 16:13:08 -05002589static int submit_extent_page(int rw, struct extent_io_tree *tree,
2590 struct page *page, sector_t sector,
2591 size_t size, unsigned long offset,
2592 struct block_device *bdev,
2593 struct bio **bio_ret,
2594 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04002595 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002596 int mirror_num,
2597 unsigned long prev_bio_flags,
2598 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002599{
2600 int ret = 0;
2601 struct bio *bio;
2602 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04002603 int contig = 0;
2604 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2605 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05002606 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002607
2608 if (bio_ret && *bio_ret) {
2609 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002610 if (old_compressed)
2611 contig = bio->bi_sector == sector;
2612 else
2613 contig = bio->bi_sector + (bio->bi_size >> 9) ==
2614 sector;
2615
2616 if (prev_bio_flags != bio_flags || !contig ||
David Woodhouse64a16702009-07-15 23:29:37 +01002617 merge_bio(rw, tree, page, offset, page_size, bio, bio_flags) ||
Chris Masonc8b97812008-10-29 14:49:59 -04002618 bio_add_page(bio, page, page_size, offset) < page_size) {
2619 ret = submit_one_bio(rw, bio, mirror_num,
2620 prev_bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002621 if (ret < 0)
2622 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05002623 bio = NULL;
2624 } else {
2625 return 0;
2626 }
2627 }
Chris Masonc8b97812008-10-29 14:49:59 -04002628 if (this_compressed)
2629 nr = BIO_MAX_PAGES;
2630 else
2631 nr = bio_get_nr_vecs(bdev);
2632
Miao Xie88f794e2010-11-22 03:02:55 +00002633 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002634 if (!bio)
2635 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002636
Chris Masonc8b97812008-10-29 14:49:59 -04002637 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002638 bio->bi_end_io = end_io_func;
2639 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05002640
Chris Masond3977122009-01-05 21:25:51 -05002641 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002642 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002643 else
Chris Masonc8b97812008-10-29 14:49:59 -04002644 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002645
2646 return ret;
2647}
2648
Eric Sandeen48a3b632013-04-25 20:41:01 +00002649static void attach_extent_buffer_page(struct extent_buffer *eb,
2650 struct page *page)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002651{
2652 if (!PagePrivate(page)) {
2653 SetPagePrivate(page);
2654 page_cache_get(page);
2655 set_page_private(page, (unsigned long)eb);
2656 } else {
2657 WARN_ON(page->private != (unsigned long)eb);
2658 }
2659}
2660
Chris Masond1310b22008-01-24 16:13:08 -05002661void set_page_extent_mapped(struct page *page)
2662{
2663 if (!PagePrivate(page)) {
2664 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002665 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002666 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002667 }
2668}
2669
Chris Masond1310b22008-01-24 16:13:08 -05002670/*
2671 * basic readpage implementation. Locked extent state structs are inserted
2672 * into the tree that are removed when the IO is done (by the end_io
2673 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002674 * XXX JDM: This needs looking at to ensure proper page locking
Chris Masond1310b22008-01-24 16:13:08 -05002675 */
2676static int __extent_read_full_page(struct extent_io_tree *tree,
2677 struct page *page,
2678 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002679 struct bio **bio, int mirror_num,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04002680 unsigned long *bio_flags, int rw)
Chris Masond1310b22008-01-24 16:13:08 -05002681{
2682 struct inode *inode = page->mapping->host;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002683 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05002684 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2685 u64 end;
2686 u64 cur = start;
2687 u64 extent_offset;
2688 u64 last_byte = i_size_read(inode);
2689 u64 block_start;
2690 u64 cur_end;
2691 sector_t sector;
2692 struct extent_map *em;
2693 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002694 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05002695 int ret;
2696 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002697 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002698 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002699 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002700 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002701 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002702
2703 set_page_extent_mapped(page);
2704
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002705 if (!PageUptodate(page)) {
2706 if (cleancache_get_page(page) == 0) {
2707 BUG_ON(blocksize != PAGE_SIZE);
2708 goto out;
2709 }
2710 }
2711
Chris Masond1310b22008-01-24 16:13:08 -05002712 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002713 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002714 lock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002715 ordered = btrfs_lookup_ordered_extent(inode, start);
2716 if (!ordered)
2717 break;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002718 unlock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002719 btrfs_start_ordered_extent(inode, ordered, 1);
2720 btrfs_put_ordered_extent(ordered);
2721 }
Chris Masond1310b22008-01-24 16:13:08 -05002722
Chris Masonc8b97812008-10-29 14:49:59 -04002723 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2724 char *userpage;
2725 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2726
2727 if (zero_offset) {
2728 iosize = PAGE_CACHE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002729 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002730 memset(userpage + zero_offset, 0, iosize);
2731 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002732 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002733 }
2734 }
Chris Masond1310b22008-01-24 16:13:08 -05002735 while (cur <= end) {
Josef Bacikc8f2f242013-02-11 11:33:00 -05002736 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2737
Chris Masond1310b22008-01-24 16:13:08 -05002738 if (cur >= last_byte) {
2739 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002740 struct extent_state *cached = NULL;
2741
David Sterba306e16c2011-04-19 14:29:38 +02002742 iosize = PAGE_CACHE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002743 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002744 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002745 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002746 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002747 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002748 &cached, GFP_NOFS);
2749 unlock_extent_cached(tree, cur, cur + iosize - 1,
2750 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002751 break;
2752 }
David Sterba306e16c2011-04-19 14:29:38 +02002753 em = get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002754 end - cur + 1, 0);
David Sterbac7040052011-04-19 18:00:01 +02002755 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002756 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002757 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002758 break;
2759 }
Chris Masond1310b22008-01-24 16:13:08 -05002760 extent_offset = cur - em->start;
2761 BUG_ON(extent_map_end(em) <= cur);
2762 BUG_ON(end < cur);
2763
Li Zefan261507a02010-12-17 14:21:50 +08002764 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002765 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002766 extent_set_compress_type(&this_bio_flag,
2767 em->compress_type);
2768 }
Chris Masonc8b97812008-10-29 14:49:59 -04002769
Chris Masond1310b22008-01-24 16:13:08 -05002770 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2771 cur_end = min(extent_map_end(em) - 1, end);
Qu Wenruofda28322013-02-26 08:10:22 +00002772 iosize = ALIGN(iosize, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -04002773 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2774 disk_io_size = em->block_len;
2775 sector = em->block_start >> 9;
2776 } else {
2777 sector = (em->block_start + extent_offset) >> 9;
2778 disk_io_size = iosize;
2779 }
Chris Masond1310b22008-01-24 16:13:08 -05002780 bdev = em->bdev;
2781 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002782 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2783 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002784 free_extent_map(em);
2785 em = NULL;
2786
2787 /* we've found a hole, just zero and go on */
2788 if (block_start == EXTENT_MAP_HOLE) {
2789 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002790 struct extent_state *cached = NULL;
2791
Cong Wang7ac687d2011-11-25 23:14:28 +08002792 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002793 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002794 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002795 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002796
2797 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002798 &cached, GFP_NOFS);
2799 unlock_extent_cached(tree, cur, cur + iosize - 1,
2800 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002801 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002802 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002803 continue;
2804 }
2805 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002806 if (test_range_bit(tree, cur, cur_end,
2807 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002808 check_page_uptodate(tree, page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002809 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002810 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002811 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002812 continue;
2813 }
Chris Mason70dec802008-01-29 09:59:12 -05002814 /* we have an inline extent but it didn't get marked up
2815 * to date. Error out
2816 */
2817 if (block_start == EXTENT_MAP_INLINE) {
2818 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002819 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05002820 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002821 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05002822 continue;
2823 }
Chris Masond1310b22008-01-24 16:13:08 -05002824
Josef Bacikc8f2f242013-02-11 11:33:00 -05002825 pnr -= page->index;
Josef Bacikd4c7ca82013-04-19 19:49:09 -04002826 ret = submit_extent_page(rw, tree, page,
David Sterba306e16c2011-04-19 14:29:38 +02002827 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04002828 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002829 end_bio_extent_readpage, mirror_num,
2830 *bio_flags,
2831 this_bio_flag);
Josef Bacikc8f2f242013-02-11 11:33:00 -05002832 if (!ret) {
2833 nr++;
2834 *bio_flags = this_bio_flag;
2835 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002836 SetPageError(page);
Josef Bacikedd33c92012-10-05 16:40:32 -04002837 unlock_extent(tree, cur, cur + iosize - 1);
2838 }
Chris Masond1310b22008-01-24 16:13:08 -05002839 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002840 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002841 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002842out:
Chris Masond1310b22008-01-24 16:13:08 -05002843 if (!nr) {
2844 if (!PageError(page))
2845 SetPageUptodate(page);
2846 unlock_page(page);
2847 }
2848 return 0;
2849}
2850
2851int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002852 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05002853{
2854 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002855 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002856 int ret;
2857
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002858 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04002859 &bio_flags, READ);
Chris Masond1310b22008-01-24 16:13:08 -05002860 if (bio)
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002861 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002862 return ret;
2863}
Chris Masond1310b22008-01-24 16:13:08 -05002864
Chris Mason11c83492009-04-20 15:50:09 -04002865static noinline void update_nr_written(struct page *page,
2866 struct writeback_control *wbc,
2867 unsigned long nr_written)
2868{
2869 wbc->nr_to_write -= nr_written;
2870 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2871 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2872 page->mapping->writeback_index = page->index + nr_written;
2873}
2874
Chris Masond1310b22008-01-24 16:13:08 -05002875/*
2876 * the writepage semantics are similar to regular writepage. extent
2877 * records are inserted to lock ranges in the tree, and as dirty areas
2878 * are found, they are marked writeback. Then the lock bits are removed
2879 * and the end_io handler clears the writeback ranges
2880 */
2881static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2882 void *data)
2883{
2884 struct inode *inode = page->mapping->host;
2885 struct extent_page_data *epd = data;
2886 struct extent_io_tree *tree = epd->tree;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002887 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05002888 u64 delalloc_start;
2889 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2890 u64 end;
2891 u64 cur = start;
2892 u64 extent_offset;
2893 u64 last_byte = i_size_read(inode);
2894 u64 block_start;
2895 u64 iosize;
2896 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002897 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002898 struct extent_map *em;
2899 struct block_device *bdev;
2900 int ret;
2901 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002902 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002903 size_t blocksize;
2904 loff_t i_size = i_size_read(inode);
2905 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2906 u64 nr_delalloc;
2907 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002908 int page_started;
2909 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002910 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002911 unsigned long nr_written = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002912 bool fill_delalloc = true;
Chris Masond1310b22008-01-24 16:13:08 -05002913
Chris Masonffbd5172009-04-20 15:50:09 -04002914 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002915 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002916 else
2917 write_flags = WRITE;
2918
liubo1abe9b82011-03-24 11:18:59 +00002919 trace___extent_writepage(page, inode, wbc);
2920
Chris Masond1310b22008-01-24 16:13:08 -05002921 WARN_ON(!PageLocked(page));
Chris Masonbf0da8c2011-11-04 12:29:37 -04002922
2923 ClearPageError(page);
2924
Chris Mason7f3c74f2008-07-18 12:01:11 -04002925 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002926 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002927 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002928 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002929 unlock_page(page);
2930 return 0;
2931 }
2932
2933 if (page->index == end_index) {
2934 char *userpage;
2935
Cong Wang7ac687d2011-11-25 23:14:28 +08002936 userpage = kmap_atomic(page);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002937 memset(userpage + pg_offset, 0,
2938 PAGE_CACHE_SIZE - pg_offset);
Cong Wang7ac687d2011-11-25 23:14:28 +08002939 kunmap_atomic(userpage);
Chris Mason211c17f2008-05-15 09:13:45 -04002940 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002941 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002942 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002943
2944 set_page_extent_mapped(page);
2945
Josef Bacik9e487102011-08-01 12:08:18 -04002946 if (!tree->ops || !tree->ops->fill_delalloc)
2947 fill_delalloc = false;
2948
Chris Masond1310b22008-01-24 16:13:08 -05002949 delalloc_start = start;
2950 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002951 page_started = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002952 if (!epd->extent_locked && fill_delalloc) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002953 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002954 /*
2955 * make sure the wbc mapping index is at least updated
2956 * to this page.
2957 */
2958 update_nr_written(page, wbc, 0);
2959
Chris Masond3977122009-01-05 21:25:51 -05002960 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002961 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002962 page,
2963 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002964 &delalloc_end,
2965 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002966 if (nr_delalloc == 0) {
2967 delalloc_start = delalloc_end + 1;
2968 continue;
2969 }
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002970 ret = tree->ops->fill_delalloc(inode, page,
2971 delalloc_start,
2972 delalloc_end,
2973 &page_started,
2974 &nr_written);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002975 /* File system has been set read-only */
2976 if (ret) {
2977 SetPageError(page);
2978 goto done;
2979 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002980 /*
2981 * delalloc_end is already one less than the total
2982 * length, so we don't subtract one from
2983 * PAGE_CACHE_SIZE
2984 */
2985 delalloc_to_write += (delalloc_end - delalloc_start +
2986 PAGE_CACHE_SIZE) >>
2987 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002988 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002989 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002990 if (wbc->nr_to_write < delalloc_to_write) {
2991 int thresh = 8192;
2992
2993 if (delalloc_to_write < thresh * 2)
2994 thresh = delalloc_to_write;
2995 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2996 thresh);
2997 }
Chris Masonc8b97812008-10-29 14:49:59 -04002998
Chris Mason771ed682008-11-06 22:02:51 -05002999 /* did the fill delalloc function already unlock and start
3000 * the IO?
3001 */
3002 if (page_started) {
3003 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04003004 /*
3005 * we've unlocked the page, so we can't update
3006 * the mapping's writeback index, just update
3007 * nr_to_write.
3008 */
3009 wbc->nr_to_write -= nr_written;
3010 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05003011 }
Chris Masonc8b97812008-10-29 14:49:59 -04003012 }
Chris Mason247e7432008-07-17 12:53:51 -04003013 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04003014 ret = tree->ops->writepage_start_hook(page, start,
3015 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01003016 if (ret) {
3017 /* Fixup worker will requeue */
3018 if (ret == -EBUSY)
3019 wbc->pages_skipped++;
3020 else
3021 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04003022 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04003023 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05003024 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04003025 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04003026 }
3027 }
3028
Chris Mason11c83492009-04-20 15:50:09 -04003029 /*
3030 * we don't want to touch the inode after unlocking the page,
3031 * so we update the mapping writeback index now
3032 */
3033 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05003034
Chris Masond1310b22008-01-24 16:13:08 -05003035 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05003036 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003037 if (tree->ops && tree->ops->writepage_end_io_hook)
3038 tree->ops->writepage_end_io_hook(page, start,
3039 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003040 goto done;
3041 }
3042
Chris Masond1310b22008-01-24 16:13:08 -05003043 blocksize = inode->i_sb->s_blocksize;
3044
3045 while (cur <= end) {
3046 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003047 if (tree->ops && tree->ops->writepage_end_io_hook)
3048 tree->ops->writepage_end_io_hook(page, cur,
3049 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003050 break;
3051 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003052 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05003053 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02003054 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003055 SetPageError(page);
3056 break;
3057 }
3058
3059 extent_offset = cur - em->start;
3060 BUG_ON(extent_map_end(em) <= cur);
3061 BUG_ON(end < cur);
3062 iosize = min(extent_map_end(em) - cur, end - cur + 1);
Qu Wenruofda28322013-02-26 08:10:22 +00003063 iosize = ALIGN(iosize, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05003064 sector = (em->block_start + extent_offset) >> 9;
3065 bdev = em->bdev;
3066 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003067 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05003068 free_extent_map(em);
3069 em = NULL;
3070
Chris Masonc8b97812008-10-29 14:49:59 -04003071 /*
3072 * compressed and inline extents are written through other
3073 * paths in the FS
3074 */
3075 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003076 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003077 /*
3078 * end_io notification does not happen here for
3079 * compressed extents
3080 */
3081 if (!compressed && tree->ops &&
3082 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003083 tree->ops->writepage_end_io_hook(page, cur,
3084 cur + iosize - 1,
3085 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003086 else if (compressed) {
3087 /* we don't want to end_page_writeback on
3088 * a compressed extent. this happens
3089 * elsewhere
3090 */
3091 nr++;
3092 }
3093
3094 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003095 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003096 continue;
3097 }
Chris Masond1310b22008-01-24 16:13:08 -05003098 /* leave this out until we have a page_mkwrite call */
3099 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04003100 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05003101 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003102 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003103 continue;
3104 }
Chris Masonc8b97812008-10-29 14:49:59 -04003105
Chris Masond1310b22008-01-24 16:13:08 -05003106 if (tree->ops && tree->ops->writepage_io_hook) {
3107 ret = tree->ops->writepage_io_hook(page, cur,
3108 cur + iosize - 1);
3109 } else {
3110 ret = 0;
3111 }
Chris Mason1259ab72008-05-12 13:39:03 -04003112 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003113 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003114 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003115 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003116
Chris Masond1310b22008-01-24 16:13:08 -05003117 set_range_writeback(tree, cur, cur + iosize - 1);
3118 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05003119 printk(KERN_ERR "btrfs warning page %lu not "
3120 "writeback, cur %llu end %llu\n",
3121 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05003122 (unsigned long long)end);
3123 }
3124
Chris Masonffbd5172009-04-20 15:50:09 -04003125 ret = submit_extent_page(write_flags, tree, page,
3126 sector, iosize, pg_offset,
3127 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04003128 end_bio_extent_writepage,
3129 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003130 if (ret)
3131 SetPageError(page);
3132 }
3133 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003134 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003135 nr++;
3136 }
3137done:
3138 if (nr == 0) {
3139 /* make sure the mapping tag for page dirty gets cleared */
3140 set_page_writeback(page);
3141 end_page_writeback(page);
3142 }
Chris Masond1310b22008-01-24 16:13:08 -05003143 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05003144
Chris Mason11c83492009-04-20 15:50:09 -04003145done_unlocked:
3146
Chris Mason2c64c532009-09-02 15:04:12 -04003147 /* drop our reference on any cached states */
3148 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05003149 return 0;
3150}
3151
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003152static int eb_wait(void *word)
3153{
3154 io_schedule();
3155 return 0;
3156}
3157
Josef Bacikfd8b2b62013-04-24 16:41:19 -04003158void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003159{
3160 wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
3161 TASK_UNINTERRUPTIBLE);
3162}
3163
3164static int lock_extent_buffer_for_io(struct extent_buffer *eb,
3165 struct btrfs_fs_info *fs_info,
3166 struct extent_page_data *epd)
3167{
3168 unsigned long i, num_pages;
3169 int flush = 0;
3170 int ret = 0;
3171
3172 if (!btrfs_try_tree_write_lock(eb)) {
3173 flush = 1;
3174 flush_write_bio(epd);
3175 btrfs_tree_lock(eb);
3176 }
3177
3178 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3179 btrfs_tree_unlock(eb);
3180 if (!epd->sync_io)
3181 return 0;
3182 if (!flush) {
3183 flush_write_bio(epd);
3184 flush = 1;
3185 }
Chris Masona098d8e2012-03-21 12:09:56 -04003186 while (1) {
3187 wait_on_extent_buffer_writeback(eb);
3188 btrfs_tree_lock(eb);
3189 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3190 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003191 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003192 }
3193 }
3194
Josef Bacik51561ff2012-07-20 16:25:24 -04003195 /*
3196 * We need to do this to prevent races in people who check if the eb is
3197 * under IO since we can end up having no IO bits set for a short period
3198 * of time.
3199 */
3200 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003201 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3202 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003203 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003204 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Miao Xiee2d84522013-01-29 10:09:20 +00003205 __percpu_counter_add(&fs_info->dirty_metadata_bytes,
3206 -eb->len,
3207 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003208 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003209 } else {
3210 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003211 }
3212
3213 btrfs_tree_unlock(eb);
3214
3215 if (!ret)
3216 return ret;
3217
3218 num_pages = num_extent_pages(eb->start, eb->len);
3219 for (i = 0; i < num_pages; i++) {
3220 struct page *p = extent_buffer_page(eb, i);
3221
3222 if (!trylock_page(p)) {
3223 if (!flush) {
3224 flush_write_bio(epd);
3225 flush = 1;
3226 }
3227 lock_page(p);
3228 }
3229 }
3230
3231 return ret;
3232}
3233
3234static void end_extent_buffer_writeback(struct extent_buffer *eb)
3235{
3236 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3237 smp_mb__after_clear_bit();
3238 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3239}
3240
3241static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
3242{
3243 int uptodate = err == 0;
3244 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
3245 struct extent_buffer *eb;
3246 int done;
3247
3248 do {
3249 struct page *page = bvec->bv_page;
3250
3251 bvec--;
3252 eb = (struct extent_buffer *)page->private;
3253 BUG_ON(!eb);
3254 done = atomic_dec_and_test(&eb->io_pages);
3255
3256 if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
3257 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3258 ClearPageUptodate(page);
3259 SetPageError(page);
3260 }
3261
3262 end_page_writeback(page);
3263
3264 if (!done)
3265 continue;
3266
3267 end_extent_buffer_writeback(eb);
3268 } while (bvec >= bio->bi_io_vec);
3269
3270 bio_put(bio);
3271
3272}
3273
3274static int write_one_eb(struct extent_buffer *eb,
3275 struct btrfs_fs_info *fs_info,
3276 struct writeback_control *wbc,
3277 struct extent_page_data *epd)
3278{
3279 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
3280 u64 offset = eb->start;
3281 unsigned long i, num_pages;
Josef Bacikde0022b2012-09-25 14:25:58 -04003282 unsigned long bio_flags = 0;
Josef Bacikd4c7ca82013-04-19 19:49:09 -04003283 int rw = (epd->sync_io ? WRITE_SYNC : WRITE) | REQ_META;
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003284 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003285
3286 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3287 num_pages = num_extent_pages(eb->start, eb->len);
3288 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003289 if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
3290 bio_flags = EXTENT_BIO_TREE_LOG;
3291
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003292 for (i = 0; i < num_pages; i++) {
3293 struct page *p = extent_buffer_page(eb, i);
3294
3295 clear_page_dirty_for_io(p);
3296 set_page_writeback(p);
3297 ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
3298 PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
3299 -1, end_bio_extent_buffer_writepage,
Josef Bacikde0022b2012-09-25 14:25:58 -04003300 0, epd->bio_flags, bio_flags);
3301 epd->bio_flags = bio_flags;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003302 if (ret) {
3303 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3304 SetPageError(p);
3305 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3306 end_extent_buffer_writeback(eb);
3307 ret = -EIO;
3308 break;
3309 }
3310 offset += PAGE_CACHE_SIZE;
3311 update_nr_written(p, wbc, 1);
3312 unlock_page(p);
3313 }
3314
3315 if (unlikely(ret)) {
3316 for (; i < num_pages; i++) {
3317 struct page *p = extent_buffer_page(eb, i);
3318 unlock_page(p);
3319 }
3320 }
3321
3322 return ret;
3323}
3324
3325int btree_write_cache_pages(struct address_space *mapping,
3326 struct writeback_control *wbc)
3327{
3328 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3329 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3330 struct extent_buffer *eb, *prev_eb = NULL;
3331 struct extent_page_data epd = {
3332 .bio = NULL,
3333 .tree = tree,
3334 .extent_locked = 0,
3335 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003336 .bio_flags = 0,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003337 };
3338 int ret = 0;
3339 int done = 0;
3340 int nr_to_write_done = 0;
3341 struct pagevec pvec;
3342 int nr_pages;
3343 pgoff_t index;
3344 pgoff_t end; /* Inclusive */
3345 int scanned = 0;
3346 int tag;
3347
3348 pagevec_init(&pvec, 0);
3349 if (wbc->range_cyclic) {
3350 index = mapping->writeback_index; /* Start from prev offset */
3351 end = -1;
3352 } else {
3353 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3354 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3355 scanned = 1;
3356 }
3357 if (wbc->sync_mode == WB_SYNC_ALL)
3358 tag = PAGECACHE_TAG_TOWRITE;
3359 else
3360 tag = PAGECACHE_TAG_DIRTY;
3361retry:
3362 if (wbc->sync_mode == WB_SYNC_ALL)
3363 tag_pages_for_writeback(mapping, index, end);
3364 while (!done && !nr_to_write_done && (index <= end) &&
3365 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3366 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3367 unsigned i;
3368
3369 scanned = 1;
3370 for (i = 0; i < nr_pages; i++) {
3371 struct page *page = pvec.pages[i];
3372
3373 if (!PagePrivate(page))
3374 continue;
3375
3376 if (!wbc->range_cyclic && page->index > end) {
3377 done = 1;
3378 break;
3379 }
3380
Josef Bacikb5bae262012-09-14 13:43:01 -04003381 spin_lock(&mapping->private_lock);
3382 if (!PagePrivate(page)) {
3383 spin_unlock(&mapping->private_lock);
3384 continue;
3385 }
3386
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003387 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003388
3389 /*
3390 * Shouldn't happen and normally this would be a BUG_ON
3391 * but no sense in crashing the users box for something
3392 * we can survive anyway.
3393 */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003394 if (!eb) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003395 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003396 WARN_ON(1);
3397 continue;
3398 }
3399
Josef Bacikb5bae262012-09-14 13:43:01 -04003400 if (eb == prev_eb) {
3401 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003402 continue;
3403 }
3404
Josef Bacikb5bae262012-09-14 13:43:01 -04003405 ret = atomic_inc_not_zero(&eb->refs);
3406 spin_unlock(&mapping->private_lock);
3407 if (!ret)
3408 continue;
3409
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003410 prev_eb = eb;
3411 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3412 if (!ret) {
3413 free_extent_buffer(eb);
3414 continue;
3415 }
3416
3417 ret = write_one_eb(eb, fs_info, wbc, &epd);
3418 if (ret) {
3419 done = 1;
3420 free_extent_buffer(eb);
3421 break;
3422 }
3423 free_extent_buffer(eb);
3424
3425 /*
3426 * the filesystem may choose to bump up nr_to_write.
3427 * We have to make sure to honor the new nr_to_write
3428 * at any time
3429 */
3430 nr_to_write_done = wbc->nr_to_write <= 0;
3431 }
3432 pagevec_release(&pvec);
3433 cond_resched();
3434 }
3435 if (!scanned && !done) {
3436 /*
3437 * We hit the last page and there is more work to be done: wrap
3438 * back to the start of the file
3439 */
3440 scanned = 1;
3441 index = 0;
3442 goto retry;
3443 }
3444 flush_write_bio(&epd);
3445 return ret;
3446}
3447
Chris Masond1310b22008-01-24 16:13:08 -05003448/**
Chris Mason4bef0842008-09-08 11:18:08 -04003449 * 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 -05003450 * @mapping: address space structure to write
3451 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3452 * @writepage: function called for each page
3453 * @data: data passed to writepage function
3454 *
3455 * If a page is already under I/O, write_cache_pages() skips it, even
3456 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3457 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3458 * and msync() need to guarantee that all the data which was dirty at the time
3459 * the call was made get new I/O started against them. If wbc->sync_mode is
3460 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3461 * existing IO to complete.
3462 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003463static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04003464 struct address_space *mapping,
3465 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003466 writepage_t writepage, void *data,
3467 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05003468{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003469 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003470 int ret = 0;
3471 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003472 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003473 struct pagevec pvec;
3474 int nr_pages;
3475 pgoff_t index;
3476 pgoff_t end; /* Inclusive */
3477 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003478 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003479
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003480 /*
3481 * We have to hold onto the inode so that ordered extents can do their
3482 * work when the IO finishes. The alternative to this is failing to add
3483 * an ordered extent if the igrab() fails there and that is a huge pain
3484 * to deal with, so instead just hold onto the inode throughout the
3485 * writepages operation. If it fails here we are freeing up the inode
3486 * anyway and we'd rather not waste our time writing out stuff that is
3487 * going to be truncated anyway.
3488 */
3489 if (!igrab(inode))
3490 return 0;
3491
Chris Masond1310b22008-01-24 16:13:08 -05003492 pagevec_init(&pvec, 0);
3493 if (wbc->range_cyclic) {
3494 index = mapping->writeback_index; /* Start from prev offset */
3495 end = -1;
3496 } else {
3497 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3498 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003499 scanned = 1;
3500 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003501 if (wbc->sync_mode == WB_SYNC_ALL)
3502 tag = PAGECACHE_TAG_TOWRITE;
3503 else
3504 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003505retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003506 if (wbc->sync_mode == WB_SYNC_ALL)
3507 tag_pages_for_writeback(mapping, index, end);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003508 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003509 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3510 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003511 unsigned i;
3512
3513 scanned = 1;
3514 for (i = 0; i < nr_pages; i++) {
3515 struct page *page = pvec.pages[i];
3516
3517 /*
3518 * At this point we hold neither mapping->tree_lock nor
3519 * lock on the page itself: the page may be truncated or
3520 * invalidated (changing page->mapping to NULL), or even
3521 * swizzled back from swapper_space to tmpfs file
3522 * mapping
3523 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05003524 if (!trylock_page(page)) {
3525 flush_fn(data);
3526 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04003527 }
Chris Masond1310b22008-01-24 16:13:08 -05003528
3529 if (unlikely(page->mapping != mapping)) {
3530 unlock_page(page);
3531 continue;
3532 }
3533
3534 if (!wbc->range_cyclic && page->index > end) {
3535 done = 1;
3536 unlock_page(page);
3537 continue;
3538 }
3539
Chris Masond2c3f4f2008-11-19 12:44:22 -05003540 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003541 if (PageWriteback(page))
3542 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05003543 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003544 }
Chris Masond1310b22008-01-24 16:13:08 -05003545
3546 if (PageWriteback(page) ||
3547 !clear_page_dirty_for_io(page)) {
3548 unlock_page(page);
3549 continue;
3550 }
3551
3552 ret = (*writepage)(page, wbc, data);
3553
3554 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3555 unlock_page(page);
3556 ret = 0;
3557 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003558 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05003559 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003560
3561 /*
3562 * the filesystem may choose to bump up nr_to_write.
3563 * We have to make sure to honor the new nr_to_write
3564 * at any time
3565 */
3566 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05003567 }
3568 pagevec_release(&pvec);
3569 cond_resched();
3570 }
3571 if (!scanned && !done) {
3572 /*
3573 * We hit the last page and there is more work to be done: wrap
3574 * back to the start of the file
3575 */
3576 scanned = 1;
3577 index = 0;
3578 goto retry;
3579 }
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003580 btrfs_add_delayed_iput(inode);
Chris Masond1310b22008-01-24 16:13:08 -05003581 return ret;
3582}
Chris Masond1310b22008-01-24 16:13:08 -05003583
Chris Masonffbd5172009-04-20 15:50:09 -04003584static void flush_epd_write_bio(struct extent_page_data *epd)
3585{
3586 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04003587 int rw = WRITE;
3588 int ret;
3589
Chris Masonffbd5172009-04-20 15:50:09 -04003590 if (epd->sync_io)
Jeff Mahoney355808c2011-10-03 23:23:14 -04003591 rw = WRITE_SYNC;
3592
Josef Bacikde0022b2012-09-25 14:25:58 -04003593 ret = submit_one_bio(rw, epd->bio, 0, epd->bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003594 BUG_ON(ret < 0); /* -ENOMEM */
Chris Masonffbd5172009-04-20 15:50:09 -04003595 epd->bio = NULL;
3596 }
3597}
3598
Chris Masond2c3f4f2008-11-19 12:44:22 -05003599static noinline void flush_write_bio(void *data)
3600{
3601 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04003602 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003603}
3604
Chris Masond1310b22008-01-24 16:13:08 -05003605int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3606 get_extent_t *get_extent,
3607 struct writeback_control *wbc)
3608{
3609 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003610 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 };
Chris Masond1310b22008-01-24 16:13:08 -05003618
Chris Masond1310b22008-01-24 16:13:08 -05003619 ret = __extent_writepage(page, wbc, &epd);
3620
Chris Masonffbd5172009-04-20 15:50:09 -04003621 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003622 return ret;
3623}
Chris Masond1310b22008-01-24 16:13:08 -05003624
Chris Mason771ed682008-11-06 22:02:51 -05003625int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3626 u64 start, u64 end, get_extent_t *get_extent,
3627 int mode)
3628{
3629 int ret = 0;
3630 struct address_space *mapping = inode->i_mapping;
3631 struct page *page;
3632 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3633 PAGE_CACHE_SHIFT;
3634
3635 struct extent_page_data epd = {
3636 .bio = NULL,
3637 .tree = tree,
3638 .get_extent = get_extent,
3639 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04003640 .sync_io = mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003641 .bio_flags = 0,
Chris Mason771ed682008-11-06 22:02:51 -05003642 };
3643 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05003644 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05003645 .nr_to_write = nr_pages * 2,
3646 .range_start = start,
3647 .range_end = end + 1,
3648 };
3649
Chris Masond3977122009-01-05 21:25:51 -05003650 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05003651 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3652 if (clear_page_dirty_for_io(page))
3653 ret = __extent_writepage(page, &wbc_writepages, &epd);
3654 else {
3655 if (tree->ops && tree->ops->writepage_end_io_hook)
3656 tree->ops->writepage_end_io_hook(page, start,
3657 start + PAGE_CACHE_SIZE - 1,
3658 NULL, 1);
3659 unlock_page(page);
3660 }
3661 page_cache_release(page);
3662 start += PAGE_CACHE_SIZE;
3663 }
3664
Chris Masonffbd5172009-04-20 15:50:09 -04003665 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05003666 return ret;
3667}
Chris Masond1310b22008-01-24 16:13:08 -05003668
3669int extent_writepages(struct extent_io_tree *tree,
3670 struct address_space *mapping,
3671 get_extent_t *get_extent,
3672 struct writeback_control *wbc)
3673{
3674 int ret = 0;
3675 struct extent_page_data epd = {
3676 .bio = NULL,
3677 .tree = tree,
3678 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003679 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003680 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003681 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05003682 };
3683
Chris Mason4bef0842008-09-08 11:18:08 -04003684 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003685 __extent_writepage, &epd,
3686 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04003687 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003688 return ret;
3689}
Chris Masond1310b22008-01-24 16:13:08 -05003690
3691int extent_readpages(struct extent_io_tree *tree,
3692 struct address_space *mapping,
3693 struct list_head *pages, unsigned nr_pages,
3694 get_extent_t get_extent)
3695{
3696 struct bio *bio = NULL;
3697 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04003698 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06003699 struct page *pagepool[16];
3700 struct page *page;
3701 int i = 0;
3702 int nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003703
Chris Masond1310b22008-01-24 16:13:08 -05003704 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
Liu Bo67c96842012-07-20 21:43:09 -06003705 page = list_entry(pages->prev, struct page, lru);
Chris Masond1310b22008-01-24 16:13:08 -05003706
3707 prefetchw(&page->flags);
3708 list_del(&page->lru);
Liu Bo67c96842012-07-20 21:43:09 -06003709 if (add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04003710 page->index, GFP_NOFS)) {
Liu Bo67c96842012-07-20 21:43:09 -06003711 page_cache_release(page);
3712 continue;
Chris Masond1310b22008-01-24 16:13:08 -05003713 }
Liu Bo67c96842012-07-20 21:43:09 -06003714
3715 pagepool[nr++] = page;
3716 if (nr < ARRAY_SIZE(pagepool))
3717 continue;
3718 for (i = 0; i < nr; i++) {
3719 __extent_read_full_page(tree, pagepool[i], get_extent,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04003720 &bio, 0, &bio_flags, READ);
Liu Bo67c96842012-07-20 21:43:09 -06003721 page_cache_release(pagepool[i]);
3722 }
3723 nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003724 }
Liu Bo67c96842012-07-20 21:43:09 -06003725 for (i = 0; i < nr; i++) {
3726 __extent_read_full_page(tree, pagepool[i], get_extent,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04003727 &bio, 0, &bio_flags, READ);
Liu Bo67c96842012-07-20 21:43:09 -06003728 page_cache_release(pagepool[i]);
3729 }
3730
Chris Masond1310b22008-01-24 16:13:08 -05003731 BUG_ON(!list_empty(pages));
3732 if (bio)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003733 return submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003734 return 0;
3735}
Chris Masond1310b22008-01-24 16:13:08 -05003736
3737/*
3738 * basic invalidatepage code, this waits on any locked or writeback
3739 * ranges corresponding to the page, and then deletes any extent state
3740 * records from the tree
3741 */
3742int extent_invalidatepage(struct extent_io_tree *tree,
3743 struct page *page, unsigned long offset)
3744{
Josef Bacik2ac55d42010-02-03 19:33:23 +00003745 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003746 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05003747 u64 end = start + PAGE_CACHE_SIZE - 1;
3748 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3749
Qu Wenruofda28322013-02-26 08:10:22 +00003750 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05003751 if (start > end)
3752 return 0;
3753
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003754 lock_extent_bits(tree, start, end, 0, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04003755 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003756 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04003757 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3758 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003759 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003760 return 0;
3761}
Chris Masond1310b22008-01-24 16:13:08 -05003762
3763/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04003764 * a helper for releasepage, this tests for areas of the page that
3765 * are locked or under IO and drops the related state bits if it is safe
3766 * to drop the page.
3767 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003768static int try_release_extent_state(struct extent_map_tree *map,
3769 struct extent_io_tree *tree,
3770 struct page *page, gfp_t mask)
Chris Mason7b13b7b2008-04-18 10:29:50 -04003771{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003772 u64 start = page_offset(page);
Chris Mason7b13b7b2008-04-18 10:29:50 -04003773 u64 end = start + PAGE_CACHE_SIZE - 1;
3774 int ret = 1;
3775
Chris Mason211f90e2008-07-18 11:56:15 -04003776 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04003777 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04003778 ret = 0;
3779 else {
3780 if ((mask & GFP_NOFS) == GFP_NOFS)
3781 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04003782 /*
3783 * at this point we can safely clear everything except the
3784 * locked bit and the nodatasum bit
3785 */
Chris Masone3f24cc2011-02-14 12:52:08 -05003786 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04003787 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3788 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05003789
3790 /* if clear_extent_bit failed for enomem reasons,
3791 * we can't allow the release to continue.
3792 */
3793 if (ret < 0)
3794 ret = 0;
3795 else
3796 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003797 }
3798 return ret;
3799}
Chris Mason7b13b7b2008-04-18 10:29:50 -04003800
3801/*
Chris Masond1310b22008-01-24 16:13:08 -05003802 * a helper for releasepage. As long as there are no locked extents
3803 * in the range corresponding to the page, both state records and extent
3804 * map records are removed
3805 */
3806int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05003807 struct extent_io_tree *tree, struct page *page,
3808 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05003809{
3810 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003811 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05003812 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003813
Chris Mason70dec802008-01-29 09:59:12 -05003814 if ((mask & __GFP_WAIT) &&
3815 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05003816 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05003817 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05003818 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04003819 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05003820 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09003821 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04003822 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003823 break;
3824 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003825 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3826 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04003827 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003828 free_extent_map(em);
3829 break;
3830 }
3831 if (!test_range_bit(tree, em->start,
3832 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04003833 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04003834 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05003835 remove_extent_mapping(map, em);
3836 /* once for the rb tree */
3837 free_extent_map(em);
3838 }
3839 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04003840 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003841
3842 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05003843 free_extent_map(em);
3844 }
Chris Masond1310b22008-01-24 16:13:08 -05003845 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04003846 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003847}
Chris Masond1310b22008-01-24 16:13:08 -05003848
Chris Masonec29ed52011-02-23 16:23:20 -05003849/*
3850 * helper function for fiemap, which doesn't want to see any holes.
3851 * This maps until we find something past 'last'
3852 */
3853static struct extent_map *get_extent_skip_holes(struct inode *inode,
3854 u64 offset,
3855 u64 last,
3856 get_extent_t *get_extent)
3857{
3858 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3859 struct extent_map *em;
3860 u64 len;
3861
3862 if (offset >= last)
3863 return NULL;
3864
3865 while(1) {
3866 len = last - offset;
3867 if (len == 0)
3868 break;
Qu Wenruofda28322013-02-26 08:10:22 +00003869 len = ALIGN(len, sectorsize);
Chris Masonec29ed52011-02-23 16:23:20 -05003870 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02003871 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05003872 return em;
3873
3874 /* if this isn't a hole return it */
3875 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3876 em->block_start != EXTENT_MAP_HOLE) {
3877 return em;
3878 }
3879
3880 /* this is a hole, advance to the next extent */
3881 offset = extent_map_end(em);
3882 free_extent_map(em);
3883 if (offset >= last)
3884 break;
3885 }
3886 return NULL;
3887}
3888
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003889int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3890 __u64 start, __u64 len, get_extent_t *get_extent)
3891{
Josef Bacik975f84f2010-11-23 19:36:57 +00003892 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003893 u64 off = start;
3894 u64 max = start + len;
3895 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00003896 u32 found_type;
3897 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05003898 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003899 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003900 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00003901 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003902 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003903 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00003904 struct btrfs_path *path;
3905 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003906 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003907 u64 em_start = 0;
3908 u64 em_len = 0;
3909 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003910 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003911
3912 if (len == 0)
3913 return -EINVAL;
3914
Josef Bacik975f84f2010-11-23 19:36:57 +00003915 path = btrfs_alloc_path();
3916 if (!path)
3917 return -ENOMEM;
3918 path->leave_spinning = 1;
3919
Josef Bacik4d479cf2011-11-17 11:34:31 -05003920 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3921 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3922
Chris Masonec29ed52011-02-23 16:23:20 -05003923 /*
3924 * lookup the last file extent. We're not using i_size here
3925 * because there might be preallocation past i_size
3926 */
Josef Bacik975f84f2010-11-23 19:36:57 +00003927 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
Li Zefan33345d012011-04-20 10:31:50 +08003928 path, btrfs_ino(inode), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00003929 if (ret < 0) {
3930 btrfs_free_path(path);
3931 return ret;
3932 }
3933 WARN_ON(!ret);
3934 path->slots[0]--;
3935 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3936 struct btrfs_file_extent_item);
3937 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3938 found_type = btrfs_key_type(&found_key);
3939
Chris Masonec29ed52011-02-23 16:23:20 -05003940 /* No extents, but there might be delalloc bits */
Li Zefan33345d012011-04-20 10:31:50 +08003941 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00003942 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05003943 /* have to trust i_size as the end */
3944 last = (u64)-1;
3945 last_for_get_extent = isize;
3946 } else {
3947 /*
3948 * remember the start of the last extent. There are a
3949 * bunch of different factors that go into the length of the
3950 * extent, so its much less complex to remember where it started
3951 */
3952 last = found_key.offset;
3953 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00003954 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003955 btrfs_free_path(path);
3956
Chris Masonec29ed52011-02-23 16:23:20 -05003957 /*
3958 * we might have some extents allocated but more delalloc past those
3959 * extents. so, we trust isize unless the start of the last extent is
3960 * beyond isize
3961 */
3962 if (last < isize) {
3963 last = (u64)-1;
3964 last_for_get_extent = isize;
3965 }
3966
Liu Boa52f4cd2013-05-01 16:23:41 +00003967 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003968 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05003969
Josef Bacik4d479cf2011-11-17 11:34:31 -05003970 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05003971 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003972 if (!em)
3973 goto out;
3974 if (IS_ERR(em)) {
3975 ret = PTR_ERR(em);
3976 goto out;
3977 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003978
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003979 while (!end) {
Chris Masonea8efc72011-03-08 11:54:40 -05003980 u64 offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003981
Chris Masonea8efc72011-03-08 11:54:40 -05003982 /* break if the extent we found is outside the range */
3983 if (em->start >= max || extent_map_end(em) < off)
3984 break;
3985
3986 /*
3987 * get_extent may return an extent that starts before our
3988 * requested range. We have to make sure the ranges
3989 * we return to fiemap always move forward and don't
3990 * overlap, so adjust the offsets here
3991 */
3992 em_start = max(em->start, off);
3993
3994 /*
3995 * record the offset from the start of the extent
3996 * for adjusting the disk offset below
3997 */
3998 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05003999 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05004000 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05004001 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004002 disko = 0;
4003 flags = 0;
4004
Chris Masonea8efc72011-03-08 11:54:40 -05004005 /*
4006 * bump off for our next call to get_extent
4007 */
4008 off = extent_map_end(em);
4009 if (off >= max)
4010 end = 1;
4011
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004012 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004013 end = 1;
4014 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004015 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004016 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4017 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004018 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004019 flags |= (FIEMAP_EXTENT_DELALLOC |
4020 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004021 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05004022 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004023 }
4024 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4025 flags |= FIEMAP_EXTENT_ENCODED;
4026
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004027 free_extent_map(em);
4028 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05004029 if ((em_start >= last) || em_len == (u64)-1 ||
4030 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004031 flags |= FIEMAP_EXTENT_LAST;
4032 end = 1;
4033 }
4034
Chris Masonec29ed52011-02-23 16:23:20 -05004035 /* now scan forward to see if this is really the last extent. */
4036 em = get_extent_skip_holes(inode, off, last_for_get_extent,
4037 get_extent);
4038 if (IS_ERR(em)) {
4039 ret = PTR_ERR(em);
4040 goto out;
4041 }
4042 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00004043 flags |= FIEMAP_EXTENT_LAST;
4044 end = 1;
4045 }
Chris Masonec29ed52011-02-23 16:23:20 -05004046 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
4047 em_len, flags);
4048 if (ret)
4049 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004050 }
4051out_free:
4052 free_extent_map(em);
4053out:
Liu Boa52f4cd2013-05-01 16:23:41 +00004054 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004055 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004056 return ret;
4057}
4058
Chris Mason727011e2010-08-06 13:21:20 -04004059static void __free_extent_buffer(struct extent_buffer *eb)
4060{
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004061 btrfs_leak_debug_del(&eb->leak_list);
Chris Mason727011e2010-08-06 13:21:20 -04004062 kmem_cache_free(extent_buffer_cache, eb);
4063}
4064
Chris Masond1310b22008-01-24 16:13:08 -05004065static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
4066 u64 start,
4067 unsigned long len,
4068 gfp_t mask)
4069{
4070 struct extent_buffer *eb = NULL;
4071
Chris Masond1310b22008-01-24 16:13:08 -05004072 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00004073 if (eb == NULL)
4074 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004075 eb->start = start;
4076 eb->len = len;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004077 eb->tree = tree;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004078 eb->bflags = 0;
Chris Masonbd681512011-07-16 15:23:14 -04004079 rwlock_init(&eb->lock);
4080 atomic_set(&eb->write_locks, 0);
4081 atomic_set(&eb->read_locks, 0);
4082 atomic_set(&eb->blocking_readers, 0);
4083 atomic_set(&eb->blocking_writers, 0);
4084 atomic_set(&eb->spinning_readers, 0);
4085 atomic_set(&eb->spinning_writers, 0);
Arne Jansen5b25f702011-09-13 10:55:48 +02004086 eb->lock_nested = 0;
Chris Masonbd681512011-07-16 15:23:14 -04004087 init_waitqueue_head(&eb->write_lock_wq);
4088 init_waitqueue_head(&eb->read_lock_wq);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004089
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004090 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4091
Josef Bacik3083ee22012-03-09 16:01:49 -05004092 spin_lock_init(&eb->refs_lock);
Chris Masond1310b22008-01-24 16:13:08 -05004093 atomic_set(&eb->refs, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004094 atomic_set(&eb->io_pages, 0);
Chris Mason727011e2010-08-06 13:21:20 -04004095
David Sterbab8dae312013-02-28 14:54:18 +00004096 /*
4097 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4098 */
4099 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4100 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4101 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05004102
4103 return eb;
4104}
4105
Jan Schmidt815a51c2012-05-16 17:00:02 +02004106struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4107{
4108 unsigned long i;
4109 struct page *p;
4110 struct extent_buffer *new;
4111 unsigned long num_pages = num_extent_pages(src->start, src->len);
4112
4113 new = __alloc_extent_buffer(NULL, src->start, src->len, GFP_ATOMIC);
4114 if (new == NULL)
4115 return NULL;
4116
4117 for (i = 0; i < num_pages; i++) {
4118 p = alloc_page(GFP_ATOMIC);
4119 BUG_ON(!p);
4120 attach_extent_buffer_page(new, p);
4121 WARN_ON(PageDirty(p));
4122 SetPageUptodate(p);
4123 new->pages[i] = p;
4124 }
4125
4126 copy_extent_buffer(new, src, 0, 0, src->len);
4127 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
4128 set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
4129
4130 return new;
4131}
4132
4133struct extent_buffer *alloc_dummy_extent_buffer(u64 start, unsigned long len)
4134{
4135 struct extent_buffer *eb;
4136 unsigned long num_pages = num_extent_pages(0, len);
4137 unsigned long i;
4138
4139 eb = __alloc_extent_buffer(NULL, start, len, GFP_ATOMIC);
4140 if (!eb)
4141 return NULL;
4142
4143 for (i = 0; i < num_pages; i++) {
4144 eb->pages[i] = alloc_page(GFP_ATOMIC);
4145 if (!eb->pages[i])
4146 goto err;
4147 }
4148 set_extent_buffer_uptodate(eb);
4149 btrfs_set_header_nritems(eb, 0);
4150 set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4151
4152 return eb;
4153err:
Stefan Behrens84167d12012-10-11 07:25:16 -06004154 for (; i > 0; i--)
4155 __free_page(eb->pages[i - 1]);
Jan Schmidt815a51c2012-05-16 17:00:02 +02004156 __free_extent_buffer(eb);
4157 return NULL;
4158}
4159
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004160static int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004161{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004162 return (atomic_read(&eb->io_pages) ||
4163 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4164 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004165}
4166
Miao Xie897ca6e2010-10-26 20:57:29 -04004167/*
4168 * Helper for releasing extent buffer page.
4169 */
4170static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
4171 unsigned long start_idx)
4172{
4173 unsigned long index;
Wang Sheng-Hui39bab872012-04-06 14:35:31 +08004174 unsigned long num_pages;
Miao Xie897ca6e2010-10-26 20:57:29 -04004175 struct page *page;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004176 int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
Miao Xie897ca6e2010-10-26 20:57:29 -04004177
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004178 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e2010-10-26 20:57:29 -04004179
Wang Sheng-Hui39bab872012-04-06 14:35:31 +08004180 num_pages = num_extent_pages(eb->start, eb->len);
4181 index = start_idx + num_pages;
Miao Xie897ca6e2010-10-26 20:57:29 -04004182 if (start_idx >= index)
4183 return;
4184
4185 do {
4186 index--;
4187 page = extent_buffer_page(eb, index);
Jan Schmidt815a51c2012-05-16 17:00:02 +02004188 if (page && mapped) {
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004189 spin_lock(&page->mapping->private_lock);
4190 /*
4191 * We do this since we'll remove the pages after we've
4192 * removed the eb from the radix tree, so we could race
4193 * and have this page now attached to the new eb. So
4194 * only clear page_private if it's still connected to
4195 * this eb.
4196 */
4197 if (PagePrivate(page) &&
4198 page->private == (unsigned long)eb) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004199 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Josef Bacik3083ee22012-03-09 16:01:49 -05004200 BUG_ON(PageDirty(page));
4201 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004202 /*
4203 * We need to make sure we haven't be attached
4204 * to a new eb.
4205 */
4206 ClearPagePrivate(page);
4207 set_page_private(page, 0);
4208 /* One for the page private */
4209 page_cache_release(page);
4210 }
4211 spin_unlock(&page->mapping->private_lock);
4212
Jan Schmidt815a51c2012-05-16 17:00:02 +02004213 }
4214 if (page) {
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004215 /* One for when we alloced the page */
Miao Xie897ca6e2010-10-26 20:57:29 -04004216 page_cache_release(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004217 }
Miao Xie897ca6e2010-10-26 20:57:29 -04004218 } while (index != start_idx);
4219}
4220
4221/*
4222 * Helper for releasing the extent buffer.
4223 */
4224static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4225{
4226 btrfs_release_extent_buffer_page(eb, 0);
4227 __free_extent_buffer(eb);
4228}
4229
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004230static void check_buffer_tree_ref(struct extent_buffer *eb)
4231{
Chris Mason242e18c2013-01-29 17:49:37 -05004232 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004233 /* the ref bit is tricky. We have to make sure it is set
4234 * if we have the buffer dirty. Otherwise the
4235 * code to free a buffer can end up dropping a dirty
4236 * page
4237 *
4238 * Once the ref bit is set, it won't go away while the
4239 * buffer is dirty or in writeback, and it also won't
4240 * go away while we have the reference count on the
4241 * eb bumped.
4242 *
4243 * We can't just set the ref bit without bumping the
4244 * ref on the eb because free_extent_buffer might
4245 * see the ref bit and try to clear it. If this happens
4246 * free_extent_buffer might end up dropping our original
4247 * ref by mistake and freeing the page before we are able
4248 * to add one more ref.
4249 *
4250 * So bump the ref count first, then set the bit. If someone
4251 * beat us to it, drop the ref we added.
4252 */
Chris Mason242e18c2013-01-29 17:49:37 -05004253 refs = atomic_read(&eb->refs);
4254 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4255 return;
4256
Josef Bacik594831c2012-07-20 16:11:08 -04004257 spin_lock(&eb->refs_lock);
4258 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004259 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004260 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004261}
4262
Josef Bacik5df42352012-03-15 18:24:42 -04004263static void mark_extent_buffer_accessed(struct extent_buffer *eb)
4264{
4265 unsigned long num_pages, i;
4266
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004267 check_buffer_tree_ref(eb);
4268
Josef Bacik5df42352012-03-15 18:24:42 -04004269 num_pages = num_extent_pages(eb->start, eb->len);
4270 for (i = 0; i < num_pages; i++) {
4271 struct page *p = extent_buffer_page(eb, i);
4272 mark_page_accessed(p);
4273 }
4274}
4275
Chris Masond1310b22008-01-24 16:13:08 -05004276struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
Chris Mason727011e2010-08-06 13:21:20 -04004277 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004278{
4279 unsigned long num_pages = num_extent_pages(start, len);
4280 unsigned long i;
4281 unsigned long index = start >> PAGE_CACHE_SHIFT;
4282 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004283 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004284 struct page *p;
4285 struct address_space *mapping = tree->mapping;
4286 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004287 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004288
Miao Xie19fe0a82010-10-26 20:57:29 -04004289 rcu_read_lock();
4290 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4291 if (eb && atomic_inc_not_zero(&eb->refs)) {
4292 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004293 mark_extent_buffer_accessed(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04004294 return eb;
4295 }
Miao Xie19fe0a82010-10-26 20:57:29 -04004296 rcu_read_unlock();
Chris Mason6af118ce2008-07-22 11:18:07 -04004297
David Sterbaba144192011-04-21 01:12:06 +02004298 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
Peter2b114d12008-04-01 11:21:40 -04004299 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004300 return NULL;
4301
Chris Mason727011e2010-08-06 13:21:20 -04004302 for (i = 0; i < num_pages; i++, index++) {
Chris Masona6591712011-07-19 12:04:14 -04004303 p = find_or_create_page(mapping, index, GFP_NOFS);
Josef Bacik4804b382012-10-05 16:43:45 -04004304 if (!p)
Chris Mason6af118ce2008-07-22 11:18:07 -04004305 goto free_eb;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004306
4307 spin_lock(&mapping->private_lock);
4308 if (PagePrivate(p)) {
4309 /*
4310 * We could have already allocated an eb for this page
4311 * and attached one so lets see if we can get a ref on
4312 * the existing eb, and if we can we know it's good and
4313 * we can just return that one, else we know we can just
4314 * overwrite page->private.
4315 */
4316 exists = (struct extent_buffer *)p->private;
4317 if (atomic_inc_not_zero(&exists->refs)) {
4318 spin_unlock(&mapping->private_lock);
4319 unlock_page(p);
Josef Bacik17de39a2012-05-04 15:16:06 -04004320 page_cache_release(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004321 mark_extent_buffer_accessed(exists);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004322 goto free_eb;
4323 }
4324
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004325 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004326 * Do this so attach doesn't complain and we need to
4327 * drop the ref the old guy had.
4328 */
4329 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004330 WARN_ON(PageDirty(p));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004331 page_cache_release(p);
Chris Masond1310b22008-01-24 16:13:08 -05004332 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004333 attach_extent_buffer_page(eb, p);
4334 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004335 WARN_ON(PageDirty(p));
Chris Masond1310b22008-01-24 16:13:08 -05004336 mark_page_accessed(p);
Chris Mason727011e2010-08-06 13:21:20 -04004337 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05004338 if (!PageUptodate(p))
4339 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05004340
4341 /*
4342 * see below about how we avoid a nasty race with release page
4343 * and why we unlock later
4344 */
Chris Masond1310b22008-01-24 16:13:08 -05004345 }
4346 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004347 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05004348again:
Miao Xie19fe0a82010-10-26 20:57:29 -04004349 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4350 if (ret)
4351 goto free_eb;
4352
Chris Mason6af118ce2008-07-22 11:18:07 -04004353 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004354 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
4355 if (ret == -EEXIST) {
4356 exists = radix_tree_lookup(&tree->buffer,
4357 start >> PAGE_CACHE_SHIFT);
Josef Bacik115391d2012-03-09 09:51:43 -05004358 if (!atomic_inc_not_zero(&exists->refs)) {
4359 spin_unlock(&tree->buffer_lock);
4360 radix_tree_preload_end();
Josef Bacik115391d2012-03-09 09:51:43 -05004361 exists = NULL;
4362 goto again;
4363 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004364 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004365 radix_tree_preload_end();
Josef Bacik5df42352012-03-15 18:24:42 -04004366 mark_extent_buffer_accessed(exists);
Chris Mason6af118ce2008-07-22 11:18:07 -04004367 goto free_eb;
4368 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004369 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004370 check_buffer_tree_ref(eb);
Yan, Zhengf044ba72010-02-04 08:46:56 +00004371 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004372 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05004373
4374 /*
4375 * there is a race where release page may have
4376 * tried to find this extent buffer in the radix
4377 * but failed. It will tell the VM it is safe to
4378 * reclaim the, and it will clear the page private bit.
4379 * We must make sure to set the page private bit properly
4380 * after the extent buffer is in the radix tree so
4381 * it doesn't get lost
4382 */
Chris Mason727011e2010-08-06 13:21:20 -04004383 SetPageChecked(eb->pages[0]);
4384 for (i = 1; i < num_pages; i++) {
4385 p = extent_buffer_page(eb, i);
Chris Mason727011e2010-08-06 13:21:20 -04004386 ClearPageChecked(p);
4387 unlock_page(p);
4388 }
4389 unlock_page(eb->pages[0]);
Chris Masond1310b22008-01-24 16:13:08 -05004390 return eb;
4391
Chris Mason6af118ce2008-07-22 11:18:07 -04004392free_eb:
Chris Mason727011e2010-08-06 13:21:20 -04004393 for (i = 0; i < num_pages; i++) {
4394 if (eb->pages[i])
4395 unlock_page(eb->pages[i]);
4396 }
Chris Masoneb14ab82011-02-10 12:35:00 -05004397
Josef Bacik17de39a2012-05-04 15:16:06 -04004398 WARN_ON(!atomic_dec_and_test(&eb->refs));
Miao Xie897ca6e2010-10-26 20:57:29 -04004399 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04004400 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05004401}
Chris Masond1310b22008-01-24 16:13:08 -05004402
4403struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
David Sterbaf09d1f62011-04-21 01:08:01 +02004404 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004405{
Chris Masond1310b22008-01-24 16:13:08 -05004406 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05004407
Miao Xie19fe0a82010-10-26 20:57:29 -04004408 rcu_read_lock();
4409 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4410 if (eb && atomic_inc_not_zero(&eb->refs)) {
4411 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004412 mark_extent_buffer_accessed(eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04004413 return eb;
4414 }
4415 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04004416
Miao Xie19fe0a82010-10-26 20:57:29 -04004417 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004418}
Chris Masond1310b22008-01-24 16:13:08 -05004419
Josef Bacik3083ee22012-03-09 16:01:49 -05004420static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4421{
4422 struct extent_buffer *eb =
4423 container_of(head, struct extent_buffer, rcu_head);
4424
4425 __free_extent_buffer(eb);
4426}
4427
Josef Bacik3083ee22012-03-09 16:01:49 -05004428/* Expects to have eb->eb_lock already held */
David Sterbaf7a52a42013-04-26 14:56:29 +00004429static int release_extent_buffer(struct extent_buffer *eb)
Josef Bacik3083ee22012-03-09 16:01:49 -05004430{
4431 WARN_ON(atomic_read(&eb->refs) == 0);
4432 if (atomic_dec_and_test(&eb->refs)) {
Jan Schmidt815a51c2012-05-16 17:00:02 +02004433 if (test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) {
4434 spin_unlock(&eb->refs_lock);
4435 } else {
4436 struct extent_io_tree *tree = eb->tree;
Josef Bacik3083ee22012-03-09 16:01:49 -05004437
Jan Schmidt815a51c2012-05-16 17:00:02 +02004438 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05004439
Jan Schmidt815a51c2012-05-16 17:00:02 +02004440 spin_lock(&tree->buffer_lock);
4441 radix_tree_delete(&tree->buffer,
4442 eb->start >> PAGE_CACHE_SHIFT);
4443 spin_unlock(&tree->buffer_lock);
4444 }
Josef Bacik3083ee22012-03-09 16:01:49 -05004445
4446 /* Should be safe to release our pages at this point */
4447 btrfs_release_extent_buffer_page(eb, 0);
Josef Bacik3083ee22012-03-09 16:01:49 -05004448 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04004449 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05004450 }
4451 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04004452
4453 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05004454}
4455
Chris Masond1310b22008-01-24 16:13:08 -05004456void free_extent_buffer(struct extent_buffer *eb)
4457{
Chris Mason242e18c2013-01-29 17:49:37 -05004458 int refs;
4459 int old;
Chris Masond1310b22008-01-24 16:13:08 -05004460 if (!eb)
4461 return;
4462
Chris Mason242e18c2013-01-29 17:49:37 -05004463 while (1) {
4464 refs = atomic_read(&eb->refs);
4465 if (refs <= 3)
4466 break;
4467 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
4468 if (old == refs)
4469 return;
4470 }
4471
Josef Bacik3083ee22012-03-09 16:01:49 -05004472 spin_lock(&eb->refs_lock);
4473 if (atomic_read(&eb->refs) == 2 &&
Jan Schmidt815a51c2012-05-16 17:00:02 +02004474 test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
4475 atomic_dec(&eb->refs);
4476
4477 if (atomic_read(&eb->refs) == 2 &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004478 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004479 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004480 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4481 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05004482
Josef Bacik3083ee22012-03-09 16:01:49 -05004483 /*
4484 * I know this is terrible, but it's temporary until we stop tracking
4485 * the uptodate bits and such for the extent buffers.
4486 */
David Sterbaf7a52a42013-04-26 14:56:29 +00004487 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05004488}
Chris Masond1310b22008-01-24 16:13:08 -05004489
Josef Bacik3083ee22012-03-09 16:01:49 -05004490void free_extent_buffer_stale(struct extent_buffer *eb)
4491{
4492 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004493 return;
4494
Josef Bacik3083ee22012-03-09 16:01:49 -05004495 spin_lock(&eb->refs_lock);
4496 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
4497
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004498 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004499 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4500 atomic_dec(&eb->refs);
David Sterbaf7a52a42013-04-26 14:56:29 +00004501 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05004502}
4503
Chris Mason1d4284b2012-03-28 20:31:37 -04004504void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004505{
Chris Masond1310b22008-01-24 16:13:08 -05004506 unsigned long i;
4507 unsigned long num_pages;
4508 struct page *page;
4509
Chris Masond1310b22008-01-24 16:13:08 -05004510 num_pages = num_extent_pages(eb->start, eb->len);
4511
4512 for (i = 0; i < num_pages; i++) {
4513 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04004514 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05004515 continue;
4516
Chris Masona61e6f22008-07-22 11:18:08 -04004517 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05004518 WARN_ON(!PagePrivate(page));
4519
Chris Masond1310b22008-01-24 16:13:08 -05004520 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004521 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05004522 if (!PageDirty(page)) {
4523 radix_tree_tag_clear(&page->mapping->page_tree,
4524 page_index(page),
4525 PAGECACHE_TAG_DIRTY);
4526 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004527 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04004528 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04004529 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05004530 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004531 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05004532}
Chris Masond1310b22008-01-24 16:13:08 -05004533
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004534int set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004535{
4536 unsigned long i;
4537 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04004538 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004539
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004540 check_buffer_tree_ref(eb);
4541
Chris Masonb9473432009-03-13 11:00:37 -04004542 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004543
Chris Masond1310b22008-01-24 16:13:08 -05004544 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik3083ee22012-03-09 16:01:49 -05004545 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004546 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
4547
Chris Masonb9473432009-03-13 11:00:37 -04004548 for (i = 0; i < num_pages; i++)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004549 set_page_dirty(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04004550 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05004551}
Chris Masond1310b22008-01-24 16:13:08 -05004552
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004553int clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04004554{
4555 unsigned long i;
4556 struct page *page;
4557 unsigned long num_pages;
4558
Chris Masonb4ce94d2009-02-04 09:25:08 -05004559 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004560 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason1259ab72008-05-12 13:39:03 -04004561 for (i = 0; i < num_pages; i++) {
4562 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04004563 if (page)
4564 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04004565 }
4566 return 0;
4567}
4568
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004569int set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004570{
4571 unsigned long i;
4572 struct page *page;
4573 unsigned long num_pages;
4574
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004575 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004576 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05004577 for (i = 0; i < num_pages; i++) {
4578 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004579 SetPageUptodate(page);
4580 }
4581 return 0;
4582}
Chris Masond1310b22008-01-24 16:13:08 -05004583
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004584int extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004585{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004586 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004587}
Chris Masond1310b22008-01-24 16:13:08 -05004588
4589int read_extent_buffer_pages(struct extent_io_tree *tree,
Arne Jansenbb82ab82011-06-10 14:06:53 +02004590 struct extent_buffer *eb, u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04004591 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05004592{
4593 unsigned long i;
4594 unsigned long start_i;
4595 struct page *page;
4596 int err;
4597 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04004598 int locked_pages = 0;
4599 int all_uptodate = 1;
Chris Masond1310b22008-01-24 16:13:08 -05004600 unsigned long num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04004601 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004602 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004603 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004604
Chris Masonb4ce94d2009-02-04 09:25:08 -05004605 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05004606 return 0;
4607
Chris Masond1310b22008-01-24 16:13:08 -05004608 if (start) {
4609 WARN_ON(start < eb->start);
4610 start_i = (start >> PAGE_CACHE_SHIFT) -
4611 (eb->start >> PAGE_CACHE_SHIFT);
4612 } else {
4613 start_i = 0;
4614 }
4615
4616 num_pages = num_extent_pages(eb->start, eb->len);
4617 for (i = start_i; i < num_pages; i++) {
4618 page = extent_buffer_page(eb, i);
Arne Jansenbb82ab82011-06-10 14:06:53 +02004619 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04004620 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04004621 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05004622 } else {
4623 lock_page(page);
4624 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004625 locked_pages++;
Chris Mason727011e2010-08-06 13:21:20 -04004626 if (!PageUptodate(page)) {
4627 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04004628 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04004629 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004630 }
4631 if (all_uptodate) {
4632 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004633 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04004634 goto unlock_exit;
4635 }
4636
Josef Bacikea466792012-03-26 21:57:36 -04004637 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04004638 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004639 atomic_set(&eb->io_pages, num_reads);
Chris Masonce9adaa2008-04-09 16:28:12 -04004640 for (i = start_i; i < num_pages; i++) {
4641 page = extent_buffer_page(eb, i);
Chris Masonce9adaa2008-04-09 16:28:12 -04004642 if (!PageUptodate(page)) {
Chris Masonf1885912008-04-09 16:28:12 -04004643 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05004644 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04004645 get_extent, &bio,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04004646 mirror_num, &bio_flags,
4647 READ | REQ_META);
Chris Masond3977122009-01-05 21:25:51 -05004648 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05004649 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05004650 } else {
4651 unlock_page(page);
4652 }
4653 }
4654
Jeff Mahoney355808c2011-10-03 23:23:14 -04004655 if (bio) {
Josef Bacikd4c7ca82013-04-19 19:49:09 -04004656 err = submit_one_bio(READ | REQ_META, bio, mirror_num,
4657 bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004658 if (err)
4659 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04004660 }
Chris Masona86c12c2008-02-07 10:50:54 -05004661
Arne Jansenbb82ab82011-06-10 14:06:53 +02004662 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05004663 return ret;
Chris Masond3977122009-01-05 21:25:51 -05004664
Chris Masond1310b22008-01-24 16:13:08 -05004665 for (i = start_i; i < num_pages; i++) {
4666 page = extent_buffer_page(eb, i);
4667 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05004668 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05004669 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05004670 }
Chris Masond3977122009-01-05 21:25:51 -05004671
Chris Masond1310b22008-01-24 16:13:08 -05004672 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04004673
4674unlock_exit:
4675 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05004676 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004677 page = extent_buffer_page(eb, i);
4678 i++;
4679 unlock_page(page);
4680 locked_pages--;
4681 }
4682 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004683}
Chris Masond1310b22008-01-24 16:13:08 -05004684
4685void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4686 unsigned long start,
4687 unsigned long len)
4688{
4689 size_t cur;
4690 size_t offset;
4691 struct page *page;
4692 char *kaddr;
4693 char *dst = (char *)dstv;
4694 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4695 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004696
4697 WARN_ON(start > eb->len);
4698 WARN_ON(start + len > eb->start + eb->len);
4699
4700 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4701
Chris Masond3977122009-01-05 21:25:51 -05004702 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004703 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004704
4705 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04004706 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004707 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004708
4709 dst += cur;
4710 len -= cur;
4711 offset = 0;
4712 i++;
4713 }
4714}
Chris Masond1310b22008-01-24 16:13:08 -05004715
4716int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04004717 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05004718 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04004719 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05004720{
4721 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4722 char *kaddr;
4723 struct page *p;
4724 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4725 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4726 unsigned long end_i = (start_offset + start + min_len - 1) >>
4727 PAGE_CACHE_SHIFT;
4728
4729 if (i != end_i)
4730 return -EINVAL;
4731
4732 if (i == 0) {
4733 offset = start_offset;
4734 *map_start = 0;
4735 } else {
4736 offset = 0;
4737 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4738 }
Chris Masond3977122009-01-05 21:25:51 -05004739
Chris Masond1310b22008-01-24 16:13:08 -05004740 if (start + min_len > eb->len) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004741 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
Chris Masond3977122009-01-05 21:25:51 -05004742 "wanted %lu %lu\n", (unsigned long long)eb->start,
4743 eb->len, start, min_len);
Josef Bacik850265332011-03-15 14:52:12 -04004744 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05004745 }
4746
4747 p = extent_buffer_page(eb, i);
Chris Masona6591712011-07-19 12:04:14 -04004748 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05004749 *map = kaddr + offset;
4750 *map_len = PAGE_CACHE_SIZE - offset;
4751 return 0;
4752}
Chris Masond1310b22008-01-24 16:13:08 -05004753
Chris Masond1310b22008-01-24 16:13:08 -05004754int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4755 unsigned long start,
4756 unsigned long len)
4757{
4758 size_t cur;
4759 size_t offset;
4760 struct page *page;
4761 char *kaddr;
4762 char *ptr = (char *)ptrv;
4763 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4764 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4765 int ret = 0;
4766
4767 WARN_ON(start > eb->len);
4768 WARN_ON(start + len > eb->start + eb->len);
4769
4770 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4771
Chris Masond3977122009-01-05 21:25:51 -05004772 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004773 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004774
4775 cur = min(len, (PAGE_CACHE_SIZE - offset));
4776
Chris Masona6591712011-07-19 12:04:14 -04004777 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004778 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004779 if (ret)
4780 break;
4781
4782 ptr += cur;
4783 len -= cur;
4784 offset = 0;
4785 i++;
4786 }
4787 return ret;
4788}
Chris Masond1310b22008-01-24 16:13:08 -05004789
4790void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4791 unsigned long start, unsigned long len)
4792{
4793 size_t cur;
4794 size_t offset;
4795 struct page *page;
4796 char *kaddr;
4797 char *src = (char *)srcv;
4798 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4799 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4800
4801 WARN_ON(start > eb->len);
4802 WARN_ON(start + len > eb->start + eb->len);
4803
4804 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4805
Chris Masond3977122009-01-05 21:25:51 -05004806 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004807 page = extent_buffer_page(eb, i);
4808 WARN_ON(!PageUptodate(page));
4809
4810 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004811 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004812 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004813
4814 src += cur;
4815 len -= cur;
4816 offset = 0;
4817 i++;
4818 }
4819}
Chris Masond1310b22008-01-24 16:13:08 -05004820
4821void memset_extent_buffer(struct extent_buffer *eb, char c,
4822 unsigned long start, unsigned long len)
4823{
4824 size_t cur;
4825 size_t offset;
4826 struct page *page;
4827 char *kaddr;
4828 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4829 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4830
4831 WARN_ON(start > eb->len);
4832 WARN_ON(start + len > eb->start + eb->len);
4833
4834 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4835
Chris Masond3977122009-01-05 21:25:51 -05004836 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004837 page = extent_buffer_page(eb, i);
4838 WARN_ON(!PageUptodate(page));
4839
4840 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004841 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004842 memset(kaddr + offset, c, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004843
4844 len -= cur;
4845 offset = 0;
4846 i++;
4847 }
4848}
Chris Masond1310b22008-01-24 16:13:08 -05004849
4850void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4851 unsigned long dst_offset, unsigned long src_offset,
4852 unsigned long len)
4853{
4854 u64 dst_len = dst->len;
4855 size_t cur;
4856 size_t offset;
4857 struct page *page;
4858 char *kaddr;
4859 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4860 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4861
4862 WARN_ON(src->len != dst_len);
4863
4864 offset = (start_offset + dst_offset) &
4865 ((unsigned long)PAGE_CACHE_SIZE - 1);
4866
Chris Masond3977122009-01-05 21:25:51 -05004867 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004868 page = extent_buffer_page(dst, i);
4869 WARN_ON(!PageUptodate(page));
4870
4871 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4872
Chris Masona6591712011-07-19 12:04:14 -04004873 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004874 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004875
4876 src_offset += cur;
4877 len -= cur;
4878 offset = 0;
4879 i++;
4880 }
4881}
Chris Masond1310b22008-01-24 16:13:08 -05004882
4883static void move_pages(struct page *dst_page, struct page *src_page,
4884 unsigned long dst_off, unsigned long src_off,
4885 unsigned long len)
4886{
Chris Masona6591712011-07-19 12:04:14 -04004887 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004888 if (dst_page == src_page) {
4889 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4890 } else {
Chris Masona6591712011-07-19 12:04:14 -04004891 char *src_kaddr = page_address(src_page);
Chris Masond1310b22008-01-24 16:13:08 -05004892 char *p = dst_kaddr + dst_off + len;
4893 char *s = src_kaddr + src_off + len;
4894
4895 while (len--)
4896 *--p = *--s;
Chris Masond1310b22008-01-24 16:13:08 -05004897 }
Chris Masond1310b22008-01-24 16:13:08 -05004898}
4899
Sergei Trofimovich33872062011-04-11 21:52:52 +00004900static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4901{
4902 unsigned long distance = (src > dst) ? src - dst : dst - src;
4903 return distance < len;
4904}
4905
Chris Masond1310b22008-01-24 16:13:08 -05004906static void copy_pages(struct page *dst_page, struct page *src_page,
4907 unsigned long dst_off, unsigned long src_off,
4908 unsigned long len)
4909{
Chris Masona6591712011-07-19 12:04:14 -04004910 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004911 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004912 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004913
Sergei Trofimovich33872062011-04-11 21:52:52 +00004914 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04004915 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00004916 } else {
Chris Masond1310b22008-01-24 16:13:08 -05004917 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004918 if (areas_overlap(src_off, dst_off, len))
4919 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00004920 }
Chris Masond1310b22008-01-24 16:13:08 -05004921
Chris Mason727011e2010-08-06 13:21:20 -04004922 if (must_memmove)
4923 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
4924 else
4925 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05004926}
4927
4928void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4929 unsigned long src_offset, unsigned long len)
4930{
4931 size_t cur;
4932 size_t dst_off_in_page;
4933 size_t src_off_in_page;
4934 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4935 unsigned long dst_i;
4936 unsigned long src_i;
4937
4938 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004939 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4940 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004941 BUG_ON(1);
4942 }
4943 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004944 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4945 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004946 BUG_ON(1);
4947 }
4948
Chris Masond3977122009-01-05 21:25:51 -05004949 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004950 dst_off_in_page = (start_offset + dst_offset) &
4951 ((unsigned long)PAGE_CACHE_SIZE - 1);
4952 src_off_in_page = (start_offset + src_offset) &
4953 ((unsigned long)PAGE_CACHE_SIZE - 1);
4954
4955 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4956 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4957
4958 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4959 src_off_in_page));
4960 cur = min_t(unsigned long, cur,
4961 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4962
4963 copy_pages(extent_buffer_page(dst, dst_i),
4964 extent_buffer_page(dst, src_i),
4965 dst_off_in_page, src_off_in_page, cur);
4966
4967 src_offset += cur;
4968 dst_offset += cur;
4969 len -= cur;
4970 }
4971}
Chris Masond1310b22008-01-24 16:13:08 -05004972
4973void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4974 unsigned long src_offset, unsigned long len)
4975{
4976 size_t cur;
4977 size_t dst_off_in_page;
4978 size_t src_off_in_page;
4979 unsigned long dst_end = dst_offset + len - 1;
4980 unsigned long src_end = src_offset + len - 1;
4981 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4982 unsigned long dst_i;
4983 unsigned long src_i;
4984
4985 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004986 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4987 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004988 BUG_ON(1);
4989 }
4990 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004991 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4992 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004993 BUG_ON(1);
4994 }
Chris Mason727011e2010-08-06 13:21:20 -04004995 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05004996 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4997 return;
4998 }
Chris Masond3977122009-01-05 21:25:51 -05004999 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05005000 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
5001 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
5002
5003 dst_off_in_page = (start_offset + dst_end) &
5004 ((unsigned long)PAGE_CACHE_SIZE - 1);
5005 src_off_in_page = (start_offset + src_end) &
5006 ((unsigned long)PAGE_CACHE_SIZE - 1);
5007
5008 cur = min_t(unsigned long, len, src_off_in_page + 1);
5009 cur = min(cur, dst_off_in_page + 1);
5010 move_pages(extent_buffer_page(dst, dst_i),
5011 extent_buffer_page(dst, src_i),
5012 dst_off_in_page - cur + 1,
5013 src_off_in_page - cur + 1, cur);
5014
5015 dst_end -= cur;
5016 src_end -= cur;
5017 len -= cur;
5018 }
5019}
Chris Mason6af118ce2008-07-22 11:18:07 -04005020
David Sterbaf7a52a42013-04-26 14:56:29 +00005021int try_release_extent_buffer(struct page *page)
Miao Xie19fe0a82010-10-26 20:57:29 -04005022{
Chris Mason6af118ce2008-07-22 11:18:07 -04005023 struct extent_buffer *eb;
Miao Xie897ca6e2010-10-26 20:57:29 -04005024
Miao Xie19fe0a82010-10-26 20:57:29 -04005025 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005026 * We need to make sure noboody is attaching this page to an eb right
5027 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04005028 */
Josef Bacik3083ee22012-03-09 16:01:49 -05005029 spin_lock(&page->mapping->private_lock);
5030 if (!PagePrivate(page)) {
5031 spin_unlock(&page->mapping->private_lock);
5032 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005033 }
5034
Josef Bacik3083ee22012-03-09 16:01:49 -05005035 eb = (struct extent_buffer *)page->private;
5036 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04005037
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005038 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005039 * This is a little awful but should be ok, we need to make sure that
5040 * the eb doesn't disappear out from under us while we're looking at
5041 * this page.
5042 */
5043 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005044 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05005045 spin_unlock(&eb->refs_lock);
5046 spin_unlock(&page->mapping->private_lock);
5047 return 0;
5048 }
5049 spin_unlock(&page->mapping->private_lock);
5050
Josef Bacik3083ee22012-03-09 16:01:49 -05005051 /*
5052 * If tree ref isn't set then we know the ref on this eb is a real ref,
5053 * so just return, this page will likely be freed soon anyway.
5054 */
5055 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5056 spin_unlock(&eb->refs_lock);
5057 return 0;
5058 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005059
David Sterbaf7a52a42013-04-26 14:56:29 +00005060 return release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005061}