blob: f110d12de2d57226594d20e900267588ffdc4fb9 [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 Mason6af118c2008-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,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400325 struct extent_state *state, int *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,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400332 struct extent_state *state, int *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,
339 struct extent_state *state, int *bits);
340
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,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400353 int *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,
441 int *bits, int wake)
Chris Masond1310b22008-01-24 16:13:08 -0500442{
Li Zefancdc6a392012-03-12 16:39:48 +0800443 struct extent_state *next;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400444 int 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
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400480void extent_io_tree_panic(struct extent_io_tree *tree, int err)
481{
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,
Chris Mason2c64c532009-09-02 15:04:12 -0400500 int bits, int wake, int delete,
501 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 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100661void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
Chris Masond1310b22008-01-24 16:13:08 -0500662{
663 struct extent_state *state;
664 struct rb_node *node;
665
Chris Masoncad321a2008-12-17 14:51:42 -0500666 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500667again:
668 while (1) {
669 /*
670 * this search will find all the extents that end after
671 * our range starts
672 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500673 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500674 if (!node)
675 break;
676
677 state = rb_entry(node, struct extent_state, rb_node);
678
679 if (state->start > end)
680 goto out;
681
682 if (state->state & bits) {
683 start = state->start;
684 atomic_inc(&state->refs);
685 wait_on_state(tree, state);
686 free_extent_state(state);
687 goto again;
688 }
689 start = state->end + 1;
690
691 if (start > end)
692 break;
693
Xiao Guangrongded91f02011-07-14 03:19:27 +0000694 cond_resched_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500695 }
696out:
Chris Masoncad321a2008-12-17 14:51:42 -0500697 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500698}
Chris Masond1310b22008-01-24 16:13:08 -0500699
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000700static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500701 struct extent_state *state,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400702 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500703{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400704 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400705
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000706 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400707 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500708 u64 range = state->end - state->start + 1;
709 tree->dirty_bytes += range;
710 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400711 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500712}
713
Chris Mason2c64c532009-09-02 15:04:12 -0400714static void cache_state(struct extent_state *state,
715 struct extent_state **cached_ptr)
716{
717 if (cached_ptr && !(*cached_ptr)) {
718 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
719 *cached_ptr = state;
720 atomic_inc(&state->refs);
721 }
722 }
723}
724
Arne Jansen507903b2011-04-06 10:02:20 +0000725static void uncache_state(struct extent_state **cached_ptr)
726{
727 if (cached_ptr && (*cached_ptr)) {
728 struct extent_state *state = *cached_ptr;
Chris Mason109b36a2011-04-12 13:57:39 -0400729 *cached_ptr = NULL;
730 free_extent_state(state);
Arne Jansen507903b2011-04-06 10:02:20 +0000731 }
732}
733
Chris Masond1310b22008-01-24 16:13:08 -0500734/*
Chris Mason1edbb732009-09-02 13:24:36 -0400735 * set some bits on a range in the tree. This may require allocations or
736 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500737 *
Chris Mason1edbb732009-09-02 13:24:36 -0400738 * If any of the exclusive bits are set, this will fail with -EEXIST if some
739 * part of the range already has the desired bits set. The start of the
740 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500741 *
Chris Mason1edbb732009-09-02 13:24:36 -0400742 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500743 */
Chris Mason1edbb732009-09-02 13:24:36 -0400744
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100745static int __must_check
746__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
747 int bits, int exclusive_bits, u64 *failed_start,
748 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500749{
750 struct extent_state *state;
751 struct extent_state *prealloc = NULL;
752 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500753 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500754 u64 last_start;
755 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400756
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400757 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500758again:
759 if (!prealloc && (mask & __GFP_WAIT)) {
760 prealloc = alloc_extent_state(mask);
Xiao Guangrong82337672011-04-20 06:44:57 +0000761 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500762 }
763
Chris Masoncad321a2008-12-17 14:51:42 -0500764 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400765 if (cached_state && *cached_state) {
766 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400767 if (state->start <= start && state->end > start &&
768 state->tree) {
Chris Mason9655d292009-09-02 15:22:30 -0400769 node = &state->rb_node;
770 goto hit_next;
771 }
772 }
Chris Masond1310b22008-01-24 16:13:08 -0500773 /*
774 * this search will find all the extents that end after
775 * our range starts.
776 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500777 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500778 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000779 prealloc = alloc_extent_state_atomic(prealloc);
780 BUG_ON(!prealloc);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400781 err = insert_state(tree, prealloc, start, end, &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400782 if (err)
783 extent_io_tree_panic(tree, err);
784
Chris Masond1310b22008-01-24 16:13:08 -0500785 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500786 goto out;
787 }
Chris Masond1310b22008-01-24 16:13:08 -0500788 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400789hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500790 last_start = state->start;
791 last_end = state->end;
792
793 /*
794 * | ---- desired range ---- |
795 * | state |
796 *
797 * Just lock what we found and keep going
798 */
799 if (state->start == start && state->end <= end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400800 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500801 *failed_start = state->start;
802 err = -EEXIST;
803 goto out;
804 }
Chris Mason42daec22009-09-23 19:51:09 -0400805
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000806 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400807 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500808 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400809 if (last_end == (u64)-1)
810 goto out;
811 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800812 state = next_state(state);
813 if (start < end && state && state->start == start &&
814 !need_resched())
815 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500816 goto search_again;
817 }
818
819 /*
820 * | ---- desired range ---- |
821 * | state |
822 * or
823 * | ------------- state -------------- |
824 *
825 * We need to split the extent we found, and may flip bits on
826 * second half.
827 *
828 * If the extent we found extends past our
829 * range, we just split and search again. It'll get split
830 * again the next time though.
831 *
832 * If the extent we found is inside our range, we set the
833 * desired bit on it.
834 */
835 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400836 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500837 *failed_start = start;
838 err = -EEXIST;
839 goto out;
840 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000841
842 prealloc = alloc_extent_state_atomic(prealloc);
843 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500844 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400845 if (err)
846 extent_io_tree_panic(tree, err);
847
Chris Masond1310b22008-01-24 16:13:08 -0500848 prealloc = NULL;
849 if (err)
850 goto out;
851 if (state->end <= end) {
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000852 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400853 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500854 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400855 if (last_end == (u64)-1)
856 goto out;
857 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800858 state = next_state(state);
859 if (start < end && state && state->start == start &&
860 !need_resched())
861 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500862 }
863 goto search_again;
864 }
865 /*
866 * | ---- desired range ---- |
867 * | state | or | state |
868 *
869 * There's a hole, we need to insert something in it and
870 * ignore the extent we found.
871 */
872 if (state->start > start) {
873 u64 this_end;
874 if (end < last_start)
875 this_end = end;
876 else
Chris Masond3977122009-01-05 21:25:51 -0500877 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000878
879 prealloc = alloc_extent_state_atomic(prealloc);
880 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000881
882 /*
883 * Avoid to free 'prealloc' if it can be merged with
884 * the later extent.
885 */
Chris Masond1310b22008-01-24 16:13:08 -0500886 err = insert_state(tree, prealloc, start, this_end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400887 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400888 if (err)
889 extent_io_tree_panic(tree, err);
890
Chris Mason2c64c532009-09-02 15:04:12 -0400891 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500892 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500893 start = this_end + 1;
894 goto search_again;
895 }
896 /*
897 * | ---- desired range ---- |
898 * | state |
899 * We need to split the extent, and set the bit
900 * on the first half
901 */
902 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400903 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500904 *failed_start = start;
905 err = -EEXIST;
906 goto out;
907 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000908
909 prealloc = alloc_extent_state_atomic(prealloc);
910 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500911 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400912 if (err)
913 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -0500914
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000915 set_state_bits(tree, prealloc, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400916 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500917 merge_state(tree, prealloc);
918 prealloc = NULL;
919 goto out;
920 }
921
922 goto search_again;
923
924out:
Chris Masoncad321a2008-12-17 14:51:42 -0500925 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500926 if (prealloc)
927 free_extent_state(prealloc);
928
929 return err;
930
931search_again:
932 if (start > end)
933 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500934 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500935 if (mask & __GFP_WAIT)
936 cond_resched();
937 goto again;
938}
Chris Masond1310b22008-01-24 16:13:08 -0500939
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100940int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits,
941 u64 *failed_start, struct extent_state **cached_state,
942 gfp_t mask)
943{
944 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
945 cached_state, mask);
946}
947
948
Josef Bacik462d6fa2011-09-26 13:56:12 -0400949/**
Liu Bo10983f22012-07-11 15:26:19 +0800950 * convert_extent_bit - convert all bits in a given range from one bit to
951 * another
Josef Bacik462d6fa2011-09-26 13:56:12 -0400952 * @tree: the io tree to search
953 * @start: the start offset in bytes
954 * @end: the end offset in bytes (inclusive)
955 * @bits: the bits to set in this range
956 * @clear_bits: the bits to clear in this range
Josef Bacike6138872012-09-27 17:07:30 -0400957 * @cached_state: state that we're going to cache
Josef Bacik462d6fa2011-09-26 13:56:12 -0400958 * @mask: the allocation mask
959 *
960 * This will go through and set bits for the given range. If any states exist
961 * already in this range they are set with the given bit and cleared of the
962 * clear_bits. This is only meant to be used by things that are mergeable, ie
963 * converting from say DELALLOC to DIRTY. This is not meant to be used with
964 * boundary bits like LOCK.
965 */
966int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacike6138872012-09-27 17:07:30 -0400967 int bits, int clear_bits,
968 struct extent_state **cached_state, gfp_t mask)
Josef Bacik462d6fa2011-09-26 13:56:12 -0400969{
970 struct extent_state *state;
971 struct extent_state *prealloc = NULL;
972 struct rb_node *node;
973 int err = 0;
974 u64 last_start;
975 u64 last_end;
976
977again:
978 if (!prealloc && (mask & __GFP_WAIT)) {
979 prealloc = alloc_extent_state(mask);
980 if (!prealloc)
981 return -ENOMEM;
982 }
983
984 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -0400985 if (cached_state && *cached_state) {
986 state = *cached_state;
987 if (state->start <= start && state->end > start &&
988 state->tree) {
989 node = &state->rb_node;
990 goto hit_next;
991 }
992 }
993
Josef Bacik462d6fa2011-09-26 13:56:12 -0400994 /*
995 * this search will find all the extents that end after
996 * our range starts.
997 */
998 node = tree_search(tree, start);
999 if (!node) {
1000 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001001 if (!prealloc) {
1002 err = -ENOMEM;
1003 goto out;
1004 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001005 err = insert_state(tree, prealloc, start, end, &bits);
1006 prealloc = NULL;
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001007 if (err)
1008 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001009 goto out;
1010 }
1011 state = rb_entry(node, struct extent_state, rb_node);
1012hit_next:
1013 last_start = state->start;
1014 last_end = state->end;
1015
1016 /*
1017 * | ---- desired range ---- |
1018 * | state |
1019 *
1020 * Just lock what we found and keep going
1021 */
1022 if (state->start == start && state->end <= end) {
Josef Bacik462d6fa2011-09-26 13:56:12 -04001023 set_state_bits(tree, state, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001024 cache_state(state, cached_state);
Liu Bod1ac6e42012-05-10 18:10:39 +08001025 state = clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001026 if (last_end == (u64)-1)
1027 goto out;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001028 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001029 if (start < end && state && state->start == start &&
1030 !need_resched())
1031 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001032 goto search_again;
1033 }
1034
1035 /*
1036 * | ---- desired range ---- |
1037 * | state |
1038 * or
1039 * | ------------- state -------------- |
1040 *
1041 * We need to split the extent we found, and may flip bits on
1042 * second half.
1043 *
1044 * If the extent we found extends past our
1045 * range, we just split and search again. It'll get split
1046 * again the next time though.
1047 *
1048 * If the extent we found is inside our range, we set the
1049 * desired bit on it.
1050 */
1051 if (state->start < start) {
1052 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001053 if (!prealloc) {
1054 err = -ENOMEM;
1055 goto out;
1056 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001057 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001058 if (err)
1059 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001060 prealloc = NULL;
1061 if (err)
1062 goto out;
1063 if (state->end <= end) {
1064 set_state_bits(tree, state, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001065 cache_state(state, cached_state);
Liu Bod1ac6e42012-05-10 18:10:39 +08001066 state = clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001067 if (last_end == (u64)-1)
1068 goto out;
1069 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001070 if (start < end && state && state->start == start &&
1071 !need_resched())
1072 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001073 }
1074 goto search_again;
1075 }
1076 /*
1077 * | ---- desired range ---- |
1078 * | state | or | state |
1079 *
1080 * There's a hole, we need to insert something in it and
1081 * ignore the extent we found.
1082 */
1083 if (state->start > start) {
1084 u64 this_end;
1085 if (end < last_start)
1086 this_end = end;
1087 else
1088 this_end = last_start - 1;
1089
1090 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001091 if (!prealloc) {
1092 err = -ENOMEM;
1093 goto out;
1094 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001095
1096 /*
1097 * Avoid to free 'prealloc' if it can be merged with
1098 * the later extent.
1099 */
1100 err = insert_state(tree, prealloc, start, this_end,
1101 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001102 if (err)
1103 extent_io_tree_panic(tree, err);
Josef Bacike6138872012-09-27 17:07:30 -04001104 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001105 prealloc = NULL;
1106 start = this_end + 1;
1107 goto search_again;
1108 }
1109 /*
1110 * | ---- desired range ---- |
1111 * | state |
1112 * We need to split the extent, and set the bit
1113 * on the first half
1114 */
1115 if (state->start <= end && state->end > end) {
1116 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001117 if (!prealloc) {
1118 err = -ENOMEM;
1119 goto out;
1120 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001121
1122 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001123 if (err)
1124 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001125
1126 set_state_bits(tree, prealloc, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001127 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001128 clear_state_bit(tree, prealloc, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001129 prealloc = NULL;
1130 goto out;
1131 }
1132
1133 goto search_again;
1134
1135out:
1136 spin_unlock(&tree->lock);
1137 if (prealloc)
1138 free_extent_state(prealloc);
1139
1140 return err;
1141
1142search_again:
1143 if (start > end)
1144 goto out;
1145 spin_unlock(&tree->lock);
1146 if (mask & __GFP_WAIT)
1147 cond_resched();
1148 goto again;
1149}
1150
Chris Masond1310b22008-01-24 16:13:08 -05001151/* wrappers around set/clear extent bit */
1152int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1153 gfp_t mask)
1154{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001155 return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001156 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001157}
Chris Masond1310b22008-01-24 16:13:08 -05001158
1159int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1160 int bits, gfp_t mask)
1161{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001162 return set_extent_bit(tree, start, end, bits, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001163 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001164}
Chris Masond1310b22008-01-24 16:13:08 -05001165
1166int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1167 int bits, gfp_t mask)
1168{
Chris Mason2c64c532009-09-02 15:04:12 -04001169 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001170}
Chris Masond1310b22008-01-24 16:13:08 -05001171
1172int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001173 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001174{
1175 return set_extent_bit(tree, start, end,
Liu Bofee187d2011-09-29 15:55:28 +08001176 EXTENT_DELALLOC | EXTENT_UPTODATE,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001177 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001178}
Chris Masond1310b22008-01-24 16:13:08 -05001179
Liu Bo9e8a4a82012-09-05 19:10:51 -06001180int set_extent_defrag(struct extent_io_tree *tree, u64 start, u64 end,
1181 struct extent_state **cached_state, gfp_t mask)
1182{
1183 return set_extent_bit(tree, start, end,
1184 EXTENT_DELALLOC | EXTENT_UPTODATE | EXTENT_DEFRAG,
1185 NULL, cached_state, mask);
1186}
1187
Chris Masond1310b22008-01-24 16:13:08 -05001188int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1189 gfp_t mask)
1190{
1191 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04001192 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001193 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001194}
Chris Masond1310b22008-01-24 16:13:08 -05001195
1196int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1197 gfp_t mask)
1198{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001199 return set_extent_bit(tree, start, end, EXTENT_NEW, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001200 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001201}
Chris Masond1310b22008-01-24 16:13:08 -05001202
Chris Masond1310b22008-01-24 16:13:08 -05001203int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +00001204 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001205{
Liu Bo6b67a322013-03-28 08:30:28 +00001206 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, NULL,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001207 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001208}
Chris Masond1310b22008-01-24 16:13:08 -05001209
Josef Bacik5fd02042012-05-02 14:00:54 -04001210int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1211 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001212{
Chris Mason2c64c532009-09-02 15:04:12 -04001213 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001214 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001215}
Chris Masond1310b22008-01-24 16:13:08 -05001216
Chris Masond352ac62008-09-29 15:18:18 -04001217/*
1218 * either insert or lock state struct between start and end use mask to tell
1219 * us if waiting is desired.
1220 */
Chris Mason1edbb732009-09-02 13:24:36 -04001221int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001222 int bits, struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001223{
1224 int err;
1225 u64 failed_start;
1226 while (1) {
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001227 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
1228 EXTENT_LOCKED, &failed_start,
1229 cached_state, GFP_NOFS);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001230 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001231 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1232 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001233 } else
Chris Masond1310b22008-01-24 16:13:08 -05001234 break;
Chris Masond1310b22008-01-24 16:13:08 -05001235 WARN_ON(start > end);
1236 }
1237 return err;
1238}
Chris Masond1310b22008-01-24 16:13:08 -05001239
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001240int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Mason1edbb732009-09-02 13:24:36 -04001241{
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001242 return lock_extent_bits(tree, start, end, 0, NULL);
Chris Mason1edbb732009-09-02 13:24:36 -04001243}
1244
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001245int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001246{
1247 int err;
1248 u64 failed_start;
1249
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001250 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1251 &failed_start, NULL, GFP_NOFS);
Yan Zheng66435582008-10-30 14:19:50 -04001252 if (err == -EEXIST) {
1253 if (failed_start > start)
1254 clear_extent_bit(tree, start, failed_start - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001255 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
Josef Bacik25179202008-10-29 14:49:05 -04001256 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001257 }
Josef Bacik25179202008-10-29 14:49:05 -04001258 return 1;
1259}
Josef Bacik25179202008-10-29 14:49:05 -04001260
Chris Mason2c64c532009-09-02 15:04:12 -04001261int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1262 struct extent_state **cached, gfp_t mask)
1263{
1264 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1265 mask);
1266}
1267
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001268int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001269{
Chris Mason2c64c532009-09-02 15:04:12 -04001270 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001271 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001272}
Chris Masond1310b22008-01-24 16:13:08 -05001273
Chris Mason4adaa612013-03-26 13:07:00 -04001274int extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
1275{
1276 unsigned long index = start >> PAGE_CACHE_SHIFT;
1277 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1278 struct page *page;
1279
1280 while (index <= end_index) {
1281 page = find_get_page(inode->i_mapping, index);
1282 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1283 clear_page_dirty_for_io(page);
1284 page_cache_release(page);
1285 index++;
1286 }
1287 return 0;
1288}
1289
1290int extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
1291{
1292 unsigned long index = start >> PAGE_CACHE_SHIFT;
1293 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1294 struct page *page;
1295
1296 while (index <= end_index) {
1297 page = find_get_page(inode->i_mapping, index);
1298 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1299 account_page_redirty(page);
1300 __set_page_dirty_nobuffers(page);
1301 page_cache_release(page);
1302 index++;
1303 }
1304 return 0;
1305}
1306
Chris Masond1310b22008-01-24 16:13:08 -05001307/*
Chris Masond1310b22008-01-24 16:13:08 -05001308 * helper function to set both pages and extents in the tree writeback
1309 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001310static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001311{
1312 unsigned long index = start >> PAGE_CACHE_SHIFT;
1313 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1314 struct page *page;
1315
1316 while (index <= end_index) {
1317 page = find_get_page(tree->mapping, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001318 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Masond1310b22008-01-24 16:13:08 -05001319 set_page_writeback(page);
1320 page_cache_release(page);
1321 index++;
1322 }
Chris Masond1310b22008-01-24 16:13:08 -05001323 return 0;
1324}
Chris Masond1310b22008-01-24 16:13:08 -05001325
Chris Masond352ac62008-09-29 15:18:18 -04001326/* find the first state struct with 'bits' set after 'start', and
1327 * return it. tree->lock must be held. NULL will returned if
1328 * nothing was found after 'start'
1329 */
Chris Masond7fc6402008-02-18 12:12:38 -05001330struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1331 u64 start, int bits)
1332{
1333 struct rb_node *node;
1334 struct extent_state *state;
1335
1336 /*
1337 * this search will find all the extents that end after
1338 * our range starts.
1339 */
1340 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001341 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001342 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001343
Chris Masond3977122009-01-05 21:25:51 -05001344 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001345 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001346 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001347 return state;
Chris Masond3977122009-01-05 21:25:51 -05001348
Chris Masond7fc6402008-02-18 12:12:38 -05001349 node = rb_next(node);
1350 if (!node)
1351 break;
1352 }
1353out:
1354 return NULL;
1355}
Chris Masond7fc6402008-02-18 12:12:38 -05001356
Chris Masond352ac62008-09-29 15:18:18 -04001357/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001358 * find the first offset in the io tree with 'bits' set. zero is
1359 * returned if we find something, and *start_ret and *end_ret are
1360 * set to reflect the state struct that was found.
1361 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001362 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001363 */
1364int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
Josef Bacike6138872012-09-27 17:07:30 -04001365 u64 *start_ret, u64 *end_ret, int bits,
1366 struct extent_state **cached_state)
Xiao Guangrong69261c42011-07-14 03:19:45 +00001367{
1368 struct extent_state *state;
Josef Bacike6138872012-09-27 17:07:30 -04001369 struct rb_node *n;
Xiao Guangrong69261c42011-07-14 03:19:45 +00001370 int ret = 1;
1371
1372 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001373 if (cached_state && *cached_state) {
1374 state = *cached_state;
1375 if (state->end == start - 1 && state->tree) {
1376 n = rb_next(&state->rb_node);
1377 while (n) {
1378 state = rb_entry(n, struct extent_state,
1379 rb_node);
1380 if (state->state & bits)
1381 goto got_it;
1382 n = rb_next(n);
1383 }
1384 free_extent_state(*cached_state);
1385 *cached_state = NULL;
1386 goto out;
1387 }
1388 free_extent_state(*cached_state);
1389 *cached_state = NULL;
1390 }
1391
Xiao Guangrong69261c42011-07-14 03:19:45 +00001392 state = find_first_extent_bit_state(tree, start, bits);
Josef Bacike6138872012-09-27 17:07:30 -04001393got_it:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001394 if (state) {
Josef Bacike6138872012-09-27 17:07:30 -04001395 cache_state(state, cached_state);
Xiao Guangrong69261c42011-07-14 03:19:45 +00001396 *start_ret = state->start;
1397 *end_ret = state->end;
1398 ret = 0;
1399 }
Josef Bacike6138872012-09-27 17:07:30 -04001400out:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001401 spin_unlock(&tree->lock);
1402 return ret;
1403}
1404
1405/*
Chris Masond352ac62008-09-29 15:18:18 -04001406 * find a contiguous range of bytes in the file marked as delalloc, not
1407 * more than 'max_bytes'. start and end are used to return the range,
1408 *
1409 * 1 is returned if we find something, 0 if nothing was in the tree
1410 */
Chris Masonc8b97812008-10-29 14:49:59 -04001411static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001412 u64 *start, u64 *end, u64 max_bytes,
1413 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001414{
1415 struct rb_node *node;
1416 struct extent_state *state;
1417 u64 cur_start = *start;
1418 u64 found = 0;
1419 u64 total_bytes = 0;
1420
Chris Masoncad321a2008-12-17 14:51:42 -05001421 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001422
Chris Masond1310b22008-01-24 16:13:08 -05001423 /*
1424 * this search will find all the extents that end after
1425 * our range starts.
1426 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001427 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001428 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001429 if (!found)
1430 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001431 goto out;
1432 }
1433
Chris Masond3977122009-01-05 21:25:51 -05001434 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001435 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001436 if (found && (state->start != cur_start ||
1437 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001438 goto out;
1439 }
1440 if (!(state->state & EXTENT_DELALLOC)) {
1441 if (!found)
1442 *end = state->end;
1443 goto out;
1444 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001445 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001446 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001447 *cached_state = state;
1448 atomic_inc(&state->refs);
1449 }
Chris Masond1310b22008-01-24 16:13:08 -05001450 found++;
1451 *end = state->end;
1452 cur_start = state->end + 1;
1453 node = rb_next(node);
1454 if (!node)
1455 break;
1456 total_bytes += state->end - state->start + 1;
1457 if (total_bytes >= max_bytes)
1458 break;
1459 }
1460out:
Chris Masoncad321a2008-12-17 14:51:42 -05001461 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001462 return found;
1463}
1464
Jeff Mahoney143bede2012-03-01 14:56:26 +01001465static noinline void __unlock_for_delalloc(struct inode *inode,
1466 struct page *locked_page,
1467 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001468{
1469 int ret;
1470 struct page *pages[16];
1471 unsigned long index = start >> PAGE_CACHE_SHIFT;
1472 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1473 unsigned long nr_pages = end_index - index + 1;
1474 int i;
1475
1476 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001477 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001478
Chris Masond3977122009-01-05 21:25:51 -05001479 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001480 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001481 min_t(unsigned long, nr_pages,
1482 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001483 for (i = 0; i < ret; i++) {
1484 if (pages[i] != locked_page)
1485 unlock_page(pages[i]);
1486 page_cache_release(pages[i]);
1487 }
1488 nr_pages -= ret;
1489 index += ret;
1490 cond_resched();
1491 }
Chris Masonc8b97812008-10-29 14:49:59 -04001492}
1493
1494static noinline int lock_delalloc_pages(struct inode *inode,
1495 struct page *locked_page,
1496 u64 delalloc_start,
1497 u64 delalloc_end)
1498{
1499 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1500 unsigned long start_index = index;
1501 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1502 unsigned long pages_locked = 0;
1503 struct page *pages[16];
1504 unsigned long nrpages;
1505 int ret;
1506 int i;
1507
1508 /* the caller is responsible for locking the start index */
1509 if (index == locked_page->index && index == end_index)
1510 return 0;
1511
1512 /* skip the page at the start index */
1513 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001514 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001515 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001516 min_t(unsigned long,
1517 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001518 if (ret == 0) {
1519 ret = -EAGAIN;
1520 goto done;
1521 }
1522 /* now we have an array of pages, lock them all */
1523 for (i = 0; i < ret; i++) {
1524 /*
1525 * the caller is taking responsibility for
1526 * locked_page
1527 */
Chris Mason771ed682008-11-06 22:02:51 -05001528 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001529 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001530 if (!PageDirty(pages[i]) ||
1531 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001532 ret = -EAGAIN;
1533 unlock_page(pages[i]);
1534 page_cache_release(pages[i]);
1535 goto done;
1536 }
1537 }
Chris Masonc8b97812008-10-29 14:49:59 -04001538 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001539 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001540 }
Chris Masonc8b97812008-10-29 14:49:59 -04001541 nrpages -= ret;
1542 index += ret;
1543 cond_resched();
1544 }
1545 ret = 0;
1546done:
1547 if (ret && pages_locked) {
1548 __unlock_for_delalloc(inode, locked_page,
1549 delalloc_start,
1550 ((u64)(start_index + pages_locked - 1)) <<
1551 PAGE_CACHE_SHIFT);
1552 }
1553 return ret;
1554}
1555
1556/*
1557 * find a contiguous range of bytes in the file marked as delalloc, not
1558 * more than 'max_bytes'. start and end are used to return the range,
1559 *
1560 * 1 is returned if we find something, 0 if nothing was in the tree
1561 */
1562static noinline u64 find_lock_delalloc_range(struct inode *inode,
1563 struct extent_io_tree *tree,
1564 struct page *locked_page,
1565 u64 *start, u64 *end,
1566 u64 max_bytes)
1567{
1568 u64 delalloc_start;
1569 u64 delalloc_end;
1570 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001571 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001572 int ret;
1573 int loops = 0;
1574
1575again:
1576 /* step one, find a bunch of delalloc bytes starting at start */
1577 delalloc_start = *start;
1578 delalloc_end = 0;
1579 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001580 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001581 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001582 *start = delalloc_start;
1583 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001584 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001585 return found;
1586 }
1587
1588 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001589 * start comes from the offset of locked_page. We have to lock
1590 * pages in order, so we can't process delalloc bytes before
1591 * locked_page
1592 */
Chris Masond3977122009-01-05 21:25:51 -05001593 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001594 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001595
1596 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001597 * make sure to limit the number of pages we try to lock down
1598 * if we're looping.
1599 */
Chris Masond3977122009-01-05 21:25:51 -05001600 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001601 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001602
Chris Masonc8b97812008-10-29 14:49:59 -04001603 /* step two, lock all the pages after the page that has start */
1604 ret = lock_delalloc_pages(inode, locked_page,
1605 delalloc_start, delalloc_end);
1606 if (ret == -EAGAIN) {
1607 /* some of the pages are gone, lets avoid looping by
1608 * shortening the size of the delalloc range we're searching
1609 */
Chris Mason9655d292009-09-02 15:22:30 -04001610 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001611 if (!loops) {
1612 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1613 max_bytes = PAGE_CACHE_SIZE - offset;
1614 loops = 1;
1615 goto again;
1616 } else {
1617 found = 0;
1618 goto out_failed;
1619 }
1620 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001621 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
Chris Masonc8b97812008-10-29 14:49:59 -04001622
1623 /* step three, lock the state bits for the whole range */
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001624 lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001625
1626 /* then test to make sure it is all still delalloc */
1627 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001628 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001629 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001630 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1631 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001632 __unlock_for_delalloc(inode, locked_page,
1633 delalloc_start, delalloc_end);
1634 cond_resched();
1635 goto again;
1636 }
Chris Mason9655d292009-09-02 15:22:30 -04001637 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001638 *start = delalloc_start;
1639 *end = delalloc_end;
1640out_failed:
1641 return found;
1642}
1643
1644int extent_clear_unlock_delalloc(struct inode *inode,
1645 struct extent_io_tree *tree,
1646 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001647 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001648{
1649 int ret;
1650 struct page *pages[16];
1651 unsigned long index = start >> PAGE_CACHE_SHIFT;
1652 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1653 unsigned long nr_pages = end_index - index + 1;
1654 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001655 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001656
Chris Masona791e352009-10-08 11:27:10 -04001657 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001658 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001659 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001660 clear_bits |= EXTENT_DIRTY;
1661
Chris Masona791e352009-10-08 11:27:10 -04001662 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001663 clear_bits |= EXTENT_DELALLOC;
1664
Chris Mason2c64c532009-09-02 15:04:12 -04001665 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001666 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1667 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1668 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001669 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001670
Chris Masond3977122009-01-05 21:25:51 -05001671 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001672 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001673 min_t(unsigned long,
1674 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001675 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001676
Chris Masona791e352009-10-08 11:27:10 -04001677 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001678 SetPagePrivate2(pages[i]);
1679
Chris Masonc8b97812008-10-29 14:49:59 -04001680 if (pages[i] == locked_page) {
1681 page_cache_release(pages[i]);
1682 continue;
1683 }
Chris Masona791e352009-10-08 11:27:10 -04001684 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001685 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001686 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001687 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001688 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001689 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001690 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001691 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001692 page_cache_release(pages[i]);
1693 }
1694 nr_pages -= ret;
1695 index += ret;
1696 cond_resched();
1697 }
1698 return 0;
1699}
Chris Masonc8b97812008-10-29 14:49:59 -04001700
Chris Masond352ac62008-09-29 15:18:18 -04001701/*
1702 * count the number of bytes in the tree that have a given bit(s)
1703 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1704 * cached. The total number found is returned.
1705 */
Chris Masond1310b22008-01-24 16:13:08 -05001706u64 count_range_bits(struct extent_io_tree *tree,
1707 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001708 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001709{
1710 struct rb_node *node;
1711 struct extent_state *state;
1712 u64 cur_start = *start;
1713 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001714 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001715 int found = 0;
1716
1717 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001718 WARN_ON(1);
1719 return 0;
1720 }
1721
Chris Masoncad321a2008-12-17 14:51:42 -05001722 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001723 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1724 total_bytes = tree->dirty_bytes;
1725 goto out;
1726 }
1727 /*
1728 * this search will find all the extents that end after
1729 * our range starts.
1730 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001731 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001732 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001733 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001734
Chris Masond3977122009-01-05 21:25:51 -05001735 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001736 state = rb_entry(node, struct extent_state, rb_node);
1737 if (state->start > search_end)
1738 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001739 if (contig && found && state->start > last + 1)
1740 break;
1741 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001742 total_bytes += min(search_end, state->end) + 1 -
1743 max(cur_start, state->start);
1744 if (total_bytes >= max_bytes)
1745 break;
1746 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001747 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001748 found = 1;
1749 }
Chris Masonec29ed52011-02-23 16:23:20 -05001750 last = state->end;
1751 } else if (contig && found) {
1752 break;
Chris Masond1310b22008-01-24 16:13:08 -05001753 }
1754 node = rb_next(node);
1755 if (!node)
1756 break;
1757 }
1758out:
Chris Masoncad321a2008-12-17 14:51:42 -05001759 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001760 return total_bytes;
1761}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001762
Chris Masond352ac62008-09-29 15:18:18 -04001763/*
1764 * set the private field for a given byte offset in the tree. If there isn't
1765 * an extent_state there already, this does nothing.
1766 */
Chris Masond1310b22008-01-24 16:13:08 -05001767int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1768{
1769 struct rb_node *node;
1770 struct extent_state *state;
1771 int ret = 0;
1772
Chris Masoncad321a2008-12-17 14:51:42 -05001773 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001774 /*
1775 * this search will find all the extents that end after
1776 * our range starts.
1777 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001778 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001779 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001780 ret = -ENOENT;
1781 goto out;
1782 }
1783 state = rb_entry(node, struct extent_state, rb_node);
1784 if (state->start != start) {
1785 ret = -ENOENT;
1786 goto out;
1787 }
1788 state->private = private;
1789out:
Chris Masoncad321a2008-12-17 14:51:42 -05001790 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001791 return ret;
1792}
1793
Miao Xiee4100d92013-04-05 07:20:56 +00001794void extent_cache_csums_dio(struct extent_io_tree *tree, u64 start, u32 csums[],
1795 int count)
1796{
1797 struct rb_node *node;
1798 struct extent_state *state;
1799
1800 spin_lock(&tree->lock);
1801 /*
1802 * this search will find all the extents that end after
1803 * our range starts.
1804 */
1805 node = tree_search(tree, start);
1806 BUG_ON(!node);
1807
1808 state = rb_entry(node, struct extent_state, rb_node);
1809 BUG_ON(state->start != start);
1810
1811 while (count) {
1812 state->private = *csums++;
1813 count--;
1814 state = next_state(state);
1815 }
1816 spin_unlock(&tree->lock);
1817}
1818
1819static inline u64 __btrfs_get_bio_offset(struct bio *bio, int bio_index)
1820{
1821 struct bio_vec *bvec = bio->bi_io_vec + bio_index;
1822
1823 return page_offset(bvec->bv_page) + bvec->bv_offset;
1824}
1825
1826void extent_cache_csums(struct extent_io_tree *tree, struct bio *bio, int bio_index,
1827 u32 csums[], int count)
1828{
1829 struct rb_node *node;
1830 struct extent_state *state = NULL;
1831 u64 start;
1832
1833 spin_lock(&tree->lock);
1834 do {
1835 start = __btrfs_get_bio_offset(bio, bio_index);
1836 if (state == NULL || state->start != start) {
1837 node = tree_search(tree, start);
1838 BUG_ON(!node);
1839
1840 state = rb_entry(node, struct extent_state, rb_node);
1841 BUG_ON(state->start != start);
1842 }
1843 state->private = *csums++;
1844 count--;
1845 bio_index++;
1846
1847 state = next_state(state);
1848 } while (count);
1849 spin_unlock(&tree->lock);
1850}
1851
Chris Masond1310b22008-01-24 16:13:08 -05001852int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1853{
1854 struct rb_node *node;
1855 struct extent_state *state;
1856 int ret = 0;
1857
Chris Masoncad321a2008-12-17 14:51:42 -05001858 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001859 /*
1860 * this search will find all the extents that end after
1861 * our range starts.
1862 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001863 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001864 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001865 ret = -ENOENT;
1866 goto out;
1867 }
1868 state = rb_entry(node, struct extent_state, rb_node);
1869 if (state->start != start) {
1870 ret = -ENOENT;
1871 goto out;
1872 }
1873 *private = state->private;
1874out:
Chris Masoncad321a2008-12-17 14:51:42 -05001875 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001876 return ret;
1877}
1878
1879/*
1880 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001881 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001882 * has the bits set. Otherwise, 1 is returned if any bit in the
1883 * range is found set.
1884 */
1885int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001886 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001887{
1888 struct extent_state *state = NULL;
1889 struct rb_node *node;
1890 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001891
Chris Masoncad321a2008-12-17 14:51:42 -05001892 spin_lock(&tree->lock);
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001893 if (cached && cached->tree && cached->start <= start &&
1894 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001895 node = &cached->rb_node;
1896 else
1897 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001898 while (node && start <= end) {
1899 state = rb_entry(node, struct extent_state, rb_node);
1900
1901 if (filled && state->start > start) {
1902 bitset = 0;
1903 break;
1904 }
1905
1906 if (state->start > end)
1907 break;
1908
1909 if (state->state & bits) {
1910 bitset = 1;
1911 if (!filled)
1912 break;
1913 } else if (filled) {
1914 bitset = 0;
1915 break;
1916 }
Chris Mason46562ce2009-09-23 20:23:16 -04001917
1918 if (state->end == (u64)-1)
1919 break;
1920
Chris Masond1310b22008-01-24 16:13:08 -05001921 start = state->end + 1;
1922 if (start > end)
1923 break;
1924 node = rb_next(node);
1925 if (!node) {
1926 if (filled)
1927 bitset = 0;
1928 break;
1929 }
1930 }
Chris Masoncad321a2008-12-17 14:51:42 -05001931 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001932 return bitset;
1933}
Chris Masond1310b22008-01-24 16:13:08 -05001934
1935/*
1936 * helper function to set a given page up to date if all the
1937 * extents in the tree for that page are up to date
1938 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001939static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001940{
Miao Xie4eee4fa2012-12-21 09:17:45 +00001941 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05001942 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001943 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001944 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001945}
1946
1947/*
1948 * helper function to unlock a page if all the extents in the tree
1949 * for that page are unlocked
1950 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001951static void check_page_locked(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001952{
Miao Xie4eee4fa2012-12-21 09:17:45 +00001953 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05001954 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001955 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001956 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05001957}
1958
1959/*
1960 * helper function to end page writeback if all the extents
1961 * in the tree for that page are done with writeback
1962 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001963static void check_page_writeback(struct extent_io_tree *tree,
1964 struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001965{
Chris Mason1edbb732009-09-02 13:24:36 -04001966 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001967}
1968
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001969/*
1970 * When IO fails, either with EIO or csum verification fails, we
1971 * try other mirrors that might have a good copy of the data. This
1972 * io_failure_record is used to record state as we go through all the
1973 * mirrors. If another mirror has good data, the page is set up to date
1974 * and things continue. If a good mirror can't be found, the original
1975 * bio end_io callback is called to indicate things have failed.
1976 */
1977struct io_failure_record {
1978 struct page *page;
1979 u64 start;
1980 u64 len;
1981 u64 logical;
1982 unsigned long bio_flags;
1983 int this_mirror;
1984 int failed_mirror;
1985 int in_validation;
1986};
1987
1988static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
1989 int did_repair)
1990{
1991 int ret;
1992 int err = 0;
1993 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1994
1995 set_state_private(failure_tree, rec->start, 0);
1996 ret = clear_extent_bits(failure_tree, rec->start,
1997 rec->start + rec->len - 1,
1998 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1999 if (ret)
2000 err = ret;
2001
David Woodhouse53b381b2013-01-29 18:40:14 -05002002 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
2003 rec->start + rec->len - 1,
2004 EXTENT_DAMAGED, GFP_NOFS);
2005 if (ret && !err)
2006 err = ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002007
2008 kfree(rec);
2009 return err;
2010}
2011
2012static void repair_io_failure_callback(struct bio *bio, int err)
2013{
2014 complete(bio->bi_private);
2015}
2016
2017/*
2018 * this bypasses the standard btrfs submit functions deliberately, as
2019 * the standard behavior is to write all copies in a raid setup. here we only
2020 * want to write the one bad copy. so we do the mapping for ourselves and issue
2021 * submit_bio directly.
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002022 * to avoid any synchronization issues, wait for the data after writing, which
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002023 * actually prevents the read that triggered the error from finishing.
2024 * currently, there can be no more than two copies of every data bit. thus,
2025 * exactly one rewrite is required.
2026 */
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002027int repair_io_failure(struct btrfs_fs_info *fs_info, u64 start,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002028 u64 length, u64 logical, struct page *page,
2029 int mirror_num)
2030{
2031 struct bio *bio;
2032 struct btrfs_device *dev;
2033 DECLARE_COMPLETION_ONSTACK(compl);
2034 u64 map_length = 0;
2035 u64 sector;
2036 struct btrfs_bio *bbio = NULL;
David Woodhouse53b381b2013-01-29 18:40:14 -05002037 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002038 int ret;
2039
2040 BUG_ON(!mirror_num);
2041
David Woodhouse53b381b2013-01-29 18:40:14 -05002042 /* we can't repair anything in raid56 yet */
2043 if (btrfs_is_parity_mirror(map_tree, logical, length, mirror_num))
2044 return 0;
2045
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002046 bio = bio_alloc(GFP_NOFS, 1);
2047 if (!bio)
2048 return -EIO;
2049 bio->bi_private = &compl;
2050 bio->bi_end_io = repair_io_failure_callback;
2051 bio->bi_size = 0;
2052 map_length = length;
2053
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002054 ret = btrfs_map_block(fs_info, WRITE, logical,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002055 &map_length, &bbio, mirror_num);
2056 if (ret) {
2057 bio_put(bio);
2058 return -EIO;
2059 }
2060 BUG_ON(mirror_num != bbio->mirror_num);
2061 sector = bbio->stripes[mirror_num-1].physical >> 9;
2062 bio->bi_sector = sector;
2063 dev = bbio->stripes[mirror_num-1].dev;
2064 kfree(bbio);
2065 if (!dev || !dev->bdev || !dev->writeable) {
2066 bio_put(bio);
2067 return -EIO;
2068 }
2069 bio->bi_bdev = dev->bdev;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002070 bio_add_page(bio, page, length, start - page_offset(page));
Stefan Behrens21adbd52011-11-09 13:44:05 +01002071 btrfsic_submit_bio(WRITE_SYNC, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002072 wait_for_completion(&compl);
2073
2074 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2075 /* try to remap that extent elsewhere? */
2076 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002077 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002078 return -EIO;
2079 }
2080
Anand Jaind5b025d2012-07-02 22:05:21 -06002081 printk_ratelimited_in_rcu(KERN_INFO "btrfs read error corrected: ino %lu off %llu "
Josef Bacik606686e2012-06-04 14:03:51 -04002082 "(dev %s sector %llu)\n", page->mapping->host->i_ino,
2083 start, rcu_str_deref(dev->name), sector);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002084
2085 bio_put(bio);
2086 return 0;
2087}
2088
Josef Bacikea466792012-03-26 21:57:36 -04002089int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
2090 int mirror_num)
2091{
Josef Bacikea466792012-03-26 21:57:36 -04002092 u64 start = eb->start;
2093 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond95603b2012-04-12 15:55:15 -04002094 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04002095
2096 for (i = 0; i < num_pages; i++) {
2097 struct page *p = extent_buffer_page(eb, i);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002098 ret = repair_io_failure(root->fs_info, start, PAGE_CACHE_SIZE,
Josef Bacikea466792012-03-26 21:57:36 -04002099 start, p, mirror_num);
2100 if (ret)
2101 break;
2102 start += PAGE_CACHE_SIZE;
2103 }
2104
2105 return ret;
2106}
2107
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002108/*
2109 * each time an IO finishes, we do a fast check in the IO failure tree
2110 * to see if we need to process or clean up an io_failure_record
2111 */
2112static int clean_io_failure(u64 start, struct page *page)
2113{
2114 u64 private;
2115 u64 private_failure;
2116 struct io_failure_record *failrec;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002117 struct btrfs_fs_info *fs_info;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002118 struct extent_state *state;
2119 int num_copies;
2120 int did_repair = 0;
2121 int ret;
2122 struct inode *inode = page->mapping->host;
2123
2124 private = 0;
2125 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
2126 (u64)-1, 1, EXTENT_DIRTY, 0);
2127 if (!ret)
2128 return 0;
2129
2130 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
2131 &private_failure);
2132 if (ret)
2133 return 0;
2134
2135 failrec = (struct io_failure_record *)(unsigned long) private_failure;
2136 BUG_ON(!failrec->this_mirror);
2137
2138 if (failrec->in_validation) {
2139 /* there was no real error, just free the record */
2140 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
2141 failrec->start);
2142 did_repair = 1;
2143 goto out;
2144 }
2145
2146 spin_lock(&BTRFS_I(inode)->io_tree.lock);
2147 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
2148 failrec->start,
2149 EXTENT_LOCKED);
2150 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
2151
2152 if (state && state->start == failrec->start) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002153 fs_info = BTRFS_I(inode)->root->fs_info;
2154 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2155 failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002156 if (num_copies > 1) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002157 ret = repair_io_failure(fs_info, start, failrec->len,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002158 failrec->logical, page,
2159 failrec->failed_mirror);
2160 did_repair = !ret;
2161 }
David Woodhouse53b381b2013-01-29 18:40:14 -05002162 ret = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002163 }
2164
2165out:
2166 if (!ret)
2167 ret = free_io_failure(inode, failrec, did_repair);
2168
2169 return ret;
2170}
2171
2172/*
2173 * this is a generic handler for readpage errors (default
2174 * readpage_io_failed_hook). if other copies exist, read those and write back
2175 * good data to the failed position. does not investigate in remapping the
2176 * failed extent elsewhere, hoping the device will be smart enough to do this as
2177 * needed
2178 */
2179
2180static int bio_readpage_error(struct bio *failed_bio, struct page *page,
2181 u64 start, u64 end, int failed_mirror,
2182 struct extent_state *state)
2183{
2184 struct io_failure_record *failrec = NULL;
2185 u64 private;
2186 struct extent_map *em;
2187 struct inode *inode = page->mapping->host;
2188 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2189 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2190 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2191 struct bio *bio;
2192 int num_copies;
2193 int ret;
2194 int read_mode;
2195 u64 logical;
2196
2197 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2198
2199 ret = get_state_private(failure_tree, start, &private);
2200 if (ret) {
2201 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2202 if (!failrec)
2203 return -ENOMEM;
2204 failrec->start = start;
2205 failrec->len = end - start + 1;
2206 failrec->this_mirror = 0;
2207 failrec->bio_flags = 0;
2208 failrec->in_validation = 0;
2209
2210 read_lock(&em_tree->lock);
2211 em = lookup_extent_mapping(em_tree, start, failrec->len);
2212 if (!em) {
2213 read_unlock(&em_tree->lock);
2214 kfree(failrec);
2215 return -EIO;
2216 }
2217
2218 if (em->start > start || em->start + em->len < start) {
2219 free_extent_map(em);
2220 em = NULL;
2221 }
2222 read_unlock(&em_tree->lock);
2223
Tsutomu Itoh7a2d6a62012-10-01 03:07:15 -06002224 if (!em) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002225 kfree(failrec);
2226 return -EIO;
2227 }
2228 logical = start - em->start;
2229 logical = em->block_start + logical;
2230 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2231 logical = em->block_start;
2232 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2233 extent_set_compress_type(&failrec->bio_flags,
2234 em->compress_type);
2235 }
2236 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2237 "len=%llu\n", logical, start, failrec->len);
2238 failrec->logical = logical;
2239 free_extent_map(em);
2240
2241 /* set the bits in the private failure tree */
2242 ret = set_extent_bits(failure_tree, start, end,
2243 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2244 if (ret >= 0)
2245 ret = set_state_private(failure_tree, start,
2246 (u64)(unsigned long)failrec);
2247 /* set the bits in the inode's tree */
2248 if (ret >= 0)
2249 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2250 GFP_NOFS);
2251 if (ret < 0) {
2252 kfree(failrec);
2253 return ret;
2254 }
2255 } else {
2256 failrec = (struct io_failure_record *)(unsigned long)private;
2257 pr_debug("bio_readpage_error: (found) logical=%llu, "
2258 "start=%llu, len=%llu, validation=%d\n",
2259 failrec->logical, failrec->start, failrec->len,
2260 failrec->in_validation);
2261 /*
2262 * when data can be on disk more than twice, add to failrec here
2263 * (e.g. with a list for failed_mirror) to make
2264 * clean_io_failure() clean all those errors at once.
2265 */
2266 }
Stefan Behrens5d964052012-11-05 14:59:07 +01002267 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
2268 failrec->logical, failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002269 if (num_copies == 1) {
2270 /*
2271 * we only have a single copy of the data, so don't bother with
2272 * all the retry and error correction code that follows. no
2273 * matter what the error is, it is very likely to persist.
2274 */
2275 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2276 "state=%p, num_copies=%d, next_mirror %d, "
2277 "failed_mirror %d\n", state, num_copies,
2278 failrec->this_mirror, failed_mirror);
2279 free_io_failure(inode, failrec, 0);
2280 return -EIO;
2281 }
2282
2283 if (!state) {
2284 spin_lock(&tree->lock);
2285 state = find_first_extent_bit_state(tree, failrec->start,
2286 EXTENT_LOCKED);
2287 if (state && state->start != failrec->start)
2288 state = NULL;
2289 spin_unlock(&tree->lock);
2290 }
2291
2292 /*
2293 * there are two premises:
2294 * a) deliver good data to the caller
2295 * b) correct the bad sectors on disk
2296 */
2297 if (failed_bio->bi_vcnt > 1) {
2298 /*
2299 * to fulfill b), we need to know the exact failing sectors, as
2300 * we don't want to rewrite any more than the failed ones. thus,
2301 * we need separate read requests for the failed bio
2302 *
2303 * if the following BUG_ON triggers, our validation request got
2304 * merged. we need separate requests for our algorithm to work.
2305 */
2306 BUG_ON(failrec->in_validation);
2307 failrec->in_validation = 1;
2308 failrec->this_mirror = failed_mirror;
2309 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2310 } else {
2311 /*
2312 * we're ready to fulfill a) and b) alongside. get a good copy
2313 * of the failed sector and if we succeed, we have setup
2314 * everything for repair_io_failure to do the rest for us.
2315 */
2316 if (failrec->in_validation) {
2317 BUG_ON(failrec->this_mirror != failed_mirror);
2318 failrec->in_validation = 0;
2319 failrec->this_mirror = 0;
2320 }
2321 failrec->failed_mirror = failed_mirror;
2322 failrec->this_mirror++;
2323 if (failrec->this_mirror == failed_mirror)
2324 failrec->this_mirror++;
2325 read_mode = READ_SYNC;
2326 }
2327
2328 if (!state || failrec->this_mirror > num_copies) {
2329 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2330 "next_mirror %d, failed_mirror %d\n", state,
2331 num_copies, failrec->this_mirror, failed_mirror);
2332 free_io_failure(inode, failrec, 0);
2333 return -EIO;
2334 }
2335
2336 bio = bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04002337 if (!bio) {
2338 free_io_failure(inode, failrec, 0);
2339 return -EIO;
2340 }
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002341 bio->bi_private = state;
2342 bio->bi_end_io = failed_bio->bi_end_io;
2343 bio->bi_sector = failrec->logical >> 9;
2344 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2345 bio->bi_size = 0;
2346
2347 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2348
2349 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2350 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2351 failrec->this_mirror, num_copies, failrec->in_validation);
2352
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002353 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2354 failrec->this_mirror,
2355 failrec->bio_flags, 0);
2356 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002357}
2358
Chris Masond1310b22008-01-24 16:13:08 -05002359/* lots and lots of room for performance fixes in the end_bio funcs */
2360
Jeff Mahoney87826df2012-02-15 16:23:57 +01002361int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2362{
2363 int uptodate = (err == 0);
2364 struct extent_io_tree *tree;
2365 int ret;
2366
2367 tree = &BTRFS_I(page->mapping->host)->io_tree;
2368
2369 if (tree->ops && tree->ops->writepage_end_io_hook) {
2370 ret = tree->ops->writepage_end_io_hook(page, start,
2371 end, NULL, uptodate);
2372 if (ret)
2373 uptodate = 0;
2374 }
2375
Jeff Mahoney87826df2012-02-15 16:23:57 +01002376 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002377 ClearPageUptodate(page);
2378 SetPageError(page);
2379 }
2380 return 0;
2381}
2382
Chris Masond1310b22008-01-24 16:13:08 -05002383/*
2384 * after a writepage IO is done, we need to:
2385 * clear the uptodate bits on error
2386 * clear the writeback bits in the extent tree for this IO
2387 * end_page_writeback if the page has no more pending IO
2388 *
2389 * Scheduling is not allowed, so the extent state tree is expected
2390 * to have one and only one object corresponding to this IO.
2391 */
Chris Masond1310b22008-01-24 16:13:08 -05002392static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002393{
Chris Masond1310b22008-01-24 16:13:08 -05002394 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04002395 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002396 u64 start;
2397 u64 end;
2398 int whole_page;
2399
Chris Masond1310b22008-01-24 16:13:08 -05002400 do {
2401 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04002402 tree = &BTRFS_I(page->mapping->host)->io_tree;
2403
Miao Xie4eee4fa2012-12-21 09:17:45 +00002404 start = page_offset(page) + bvec->bv_offset;
Chris Masond1310b22008-01-24 16:13:08 -05002405 end = start + bvec->bv_len - 1;
2406
2407 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2408 whole_page = 1;
2409 else
2410 whole_page = 0;
2411
2412 if (--bvec >= bio->bi_io_vec)
2413 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04002414
Jeff Mahoney87826df2012-02-15 16:23:57 +01002415 if (end_extent_writepage(page, err, start, end))
2416 continue;
Chris Mason70dec802008-01-29 09:59:12 -05002417
Chris Masond1310b22008-01-24 16:13:08 -05002418 if (whole_page)
2419 end_page_writeback(page);
2420 else
2421 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002422 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04002423
Chris Masond1310b22008-01-24 16:13:08 -05002424 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002425}
2426
2427/*
2428 * after a readpage IO is done, we need to:
2429 * clear the uptodate bits on error
2430 * set the uptodate bits if things worked
2431 * set the page up to date if all extents in the tree are uptodate
2432 * clear the lock bit in the extent tree
2433 * unlock the page if there are no other extents locked for it
2434 *
2435 * Scheduling is not allowed, so the extent state tree is expected
2436 * to have one and only one object corresponding to this IO.
2437 */
Chris Masond1310b22008-01-24 16:13:08 -05002438static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002439{
2440 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00002441 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2442 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04002443 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002444 u64 start;
2445 u64 end;
2446 int whole_page;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002447 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002448 int ret;
2449
Chris Masond20f7042008-12-08 16:58:54 -05002450 if (err)
2451 uptodate = 0;
2452
Chris Masond1310b22008-01-24 16:13:08 -05002453 do {
2454 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00002455 struct extent_state *cached = NULL;
2456 struct extent_state *state;
2457
Kent Overstreetbe3940c2012-09-11 14:23:05 -06002458 pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, "
2459 "mirror=%ld\n", (u64)bio->bi_sector, err,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002460 (long int)bio->bi_bdev);
David Woodhouse902b22f2008-08-20 08:51:49 -04002461 tree = &BTRFS_I(page->mapping->host)->io_tree;
2462
Miao Xie4eee4fa2012-12-21 09:17:45 +00002463 start = page_offset(page) + bvec->bv_offset;
Chris Masond1310b22008-01-24 16:13:08 -05002464 end = start + bvec->bv_len - 1;
2465
2466 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2467 whole_page = 1;
2468 else
2469 whole_page = 0;
2470
Chris Mason4125bf72010-02-03 18:18:45 +00002471 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05002472 prefetchw(&bvec->bv_page->flags);
2473
Arne Jansen507903b2011-04-06 10:02:20 +00002474 spin_lock(&tree->lock);
Chris Mason0d399202011-04-16 06:55:39 -04002475 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
Chris Mason109b36a2011-04-12 13:57:39 -04002476 if (state && state->start == start) {
Arne Jansen507903b2011-04-06 10:02:20 +00002477 /*
2478 * take a reference on the state, unlock will drop
2479 * the ref
2480 */
2481 cache_state(state, &cached);
2482 }
2483 spin_unlock(&tree->lock);
2484
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002485 mirror = (int)(unsigned long)bio->bi_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002486 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05002487 ret = tree->ops->readpage_end_io_hook(page, start, end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002488 state, mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002489 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002490 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002491 else
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002492 clean_io_failure(start, page);
Chris Masond1310b22008-01-24 16:13:08 -05002493 }
Josef Bacikea466792012-03-26 21:57:36 -04002494
Josef Bacikea466792012-03-26 21:57:36 -04002495 if (!uptodate && tree->ops && tree->ops->readpage_io_failed_hook) {
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002496 ret = tree->ops->readpage_io_failed_hook(page, mirror);
Josef Bacikea466792012-03-26 21:57:36 -04002497 if (!ret && !err &&
2498 test_bit(BIO_UPTODATE, &bio->bi_flags))
2499 uptodate = 1;
2500 } else if (!uptodate) {
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002501 /*
2502 * The generic bio_readpage_error handles errors the
2503 * following way: If possible, new read requests are
2504 * created and submitted and will end up in
2505 * end_bio_extent_readpage as well (if we're lucky, not
2506 * in the !uptodate case). In that case it returns 0 and
2507 * we just go on with the next page in our bio. If it
2508 * can't handle the error it will return -EIO and we
2509 * remain responsible for that page.
2510 */
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002511 ret = bio_readpage_error(bio, page, start, end, mirror, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04002512 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04002513 uptodate =
2514 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05002515 if (err)
2516 uptodate = 0;
Arne Jansen507903b2011-04-06 10:02:20 +00002517 uncache_state(&cached);
Chris Mason7e383262008-04-09 16:28:12 -04002518 continue;
2519 }
2520 }
Chris Mason70dec802008-01-29 09:59:12 -05002521
Josef Bacik0b32f4b2012-03-13 09:38:00 -04002522 if (uptodate && tree->track_uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00002523 set_extent_uptodate(tree, start, end, &cached,
David Woodhouse902b22f2008-08-20 08:51:49 -04002524 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05002525 }
Arne Jansen507903b2011-04-06 10:02:20 +00002526 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05002527
Chris Mason70dec802008-01-29 09:59:12 -05002528 if (whole_page) {
2529 if (uptodate) {
2530 SetPageUptodate(page);
2531 } else {
2532 ClearPageUptodate(page);
2533 SetPageError(page);
2534 }
Chris Masond1310b22008-01-24 16:13:08 -05002535 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05002536 } else {
2537 if (uptodate) {
2538 check_page_uptodate(tree, page);
2539 } else {
2540 ClearPageUptodate(page);
2541 SetPageError(page);
2542 }
Chris Masond1310b22008-01-24 16:13:08 -05002543 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05002544 }
Chris Mason4125bf72010-02-03 18:18:45 +00002545 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05002546
2547 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002548}
2549
Miao Xie88f794e2010-11-22 03:02:55 +00002550struct bio *
2551btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2552 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002553{
2554 struct bio *bio;
2555
2556 bio = bio_alloc(gfp_flags, nr_vecs);
2557
2558 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2559 while (!bio && (nr_vecs /= 2))
2560 bio = bio_alloc(gfp_flags, nr_vecs);
2561 }
2562
2563 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04002564 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002565 bio->bi_bdev = bdev;
2566 bio->bi_sector = first_sector;
2567 }
2568 return bio;
2569}
2570
Jeff Mahoney355808c2011-10-03 23:23:14 -04002571static int __must_check submit_one_bio(int rw, struct bio *bio,
2572 int mirror_num, unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002573{
Chris Masond1310b22008-01-24 16:13:08 -05002574 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002575 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2576 struct page *page = bvec->bv_page;
2577 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002578 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002579
Miao Xie4eee4fa2012-12-21 09:17:45 +00002580 start = page_offset(page) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002581
David Woodhouse902b22f2008-08-20 08:51:49 -04002582 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002583
2584 bio_get(bio);
2585
Chris Mason065631f2008-02-20 12:07:25 -05002586 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00002587 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002588 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002589 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01002590 btrfsic_submit_bio(rw, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002591
Chris Masond1310b22008-01-24 16:13:08 -05002592 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2593 ret = -EOPNOTSUPP;
2594 bio_put(bio);
2595 return ret;
2596}
2597
David Woodhouse64a16702009-07-15 23:29:37 +01002598static int merge_bio(int rw, struct extent_io_tree *tree, struct page *page,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002599 unsigned long offset, size_t size, struct bio *bio,
2600 unsigned long bio_flags)
2601{
2602 int ret = 0;
2603 if (tree->ops && tree->ops->merge_bio_hook)
David Woodhouse64a16702009-07-15 23:29:37 +01002604 ret = tree->ops->merge_bio_hook(rw, page, offset, size, bio,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002605 bio_flags);
2606 BUG_ON(ret < 0);
2607 return ret;
2608
2609}
2610
Chris Masond1310b22008-01-24 16:13:08 -05002611static int submit_extent_page(int rw, struct extent_io_tree *tree,
2612 struct page *page, sector_t sector,
2613 size_t size, unsigned long offset,
2614 struct block_device *bdev,
2615 struct bio **bio_ret,
2616 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04002617 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002618 int mirror_num,
2619 unsigned long prev_bio_flags,
2620 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002621{
2622 int ret = 0;
2623 struct bio *bio;
2624 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04002625 int contig = 0;
2626 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2627 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05002628 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002629
2630 if (bio_ret && *bio_ret) {
2631 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002632 if (old_compressed)
2633 contig = bio->bi_sector == sector;
2634 else
2635 contig = bio->bi_sector + (bio->bi_size >> 9) ==
2636 sector;
2637
2638 if (prev_bio_flags != bio_flags || !contig ||
David Woodhouse64a16702009-07-15 23:29:37 +01002639 merge_bio(rw, tree, page, offset, page_size, bio, bio_flags) ||
Chris Masonc8b97812008-10-29 14:49:59 -04002640 bio_add_page(bio, page, page_size, offset) < page_size) {
2641 ret = submit_one_bio(rw, bio, mirror_num,
2642 prev_bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002643 if (ret < 0)
2644 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05002645 bio = NULL;
2646 } else {
2647 return 0;
2648 }
2649 }
Chris Masonc8b97812008-10-29 14:49:59 -04002650 if (this_compressed)
2651 nr = BIO_MAX_PAGES;
2652 else
2653 nr = bio_get_nr_vecs(bdev);
2654
Miao Xie88f794e2010-11-22 03:02:55 +00002655 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002656 if (!bio)
2657 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002658
Chris Masonc8b97812008-10-29 14:49:59 -04002659 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002660 bio->bi_end_io = end_io_func;
2661 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05002662
Chris Masond3977122009-01-05 21:25:51 -05002663 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002664 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002665 else
Chris Masonc8b97812008-10-29 14:49:59 -04002666 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002667
2668 return ret;
2669}
2670
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002671void attach_extent_buffer_page(struct extent_buffer *eb, struct page *page)
2672{
2673 if (!PagePrivate(page)) {
2674 SetPagePrivate(page);
2675 page_cache_get(page);
2676 set_page_private(page, (unsigned long)eb);
2677 } else {
2678 WARN_ON(page->private != (unsigned long)eb);
2679 }
2680}
2681
Chris Masond1310b22008-01-24 16:13:08 -05002682void set_page_extent_mapped(struct page *page)
2683{
2684 if (!PagePrivate(page)) {
2685 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002686 page_cache_get(page);
Chris Mason6af118c2008-07-22 11:18:07 -04002687 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002688 }
2689}
2690
Chris Masond1310b22008-01-24 16:13:08 -05002691/*
2692 * basic readpage implementation. Locked extent state structs are inserted
2693 * into the tree that are removed when the IO is done (by the end_io
2694 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002695 * XXX JDM: This needs looking at to ensure proper page locking
Chris Masond1310b22008-01-24 16:13:08 -05002696 */
2697static int __extent_read_full_page(struct extent_io_tree *tree,
2698 struct page *page,
2699 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002700 struct bio **bio, int mirror_num,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04002701 unsigned long *bio_flags, int rw)
Chris Masond1310b22008-01-24 16:13:08 -05002702{
2703 struct inode *inode = page->mapping->host;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002704 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05002705 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2706 u64 end;
2707 u64 cur = start;
2708 u64 extent_offset;
2709 u64 last_byte = i_size_read(inode);
2710 u64 block_start;
2711 u64 cur_end;
2712 sector_t sector;
2713 struct extent_map *em;
2714 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002715 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05002716 int ret;
2717 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002718 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002719 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002720 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002721 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002722 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002723
2724 set_page_extent_mapped(page);
2725
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002726 if (!PageUptodate(page)) {
2727 if (cleancache_get_page(page) == 0) {
2728 BUG_ON(blocksize != PAGE_SIZE);
2729 goto out;
2730 }
2731 }
2732
Chris Masond1310b22008-01-24 16:13:08 -05002733 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002734 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002735 lock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002736 ordered = btrfs_lookup_ordered_extent(inode, start);
2737 if (!ordered)
2738 break;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002739 unlock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002740 btrfs_start_ordered_extent(inode, ordered, 1);
2741 btrfs_put_ordered_extent(ordered);
2742 }
Chris Masond1310b22008-01-24 16:13:08 -05002743
Chris Masonc8b97812008-10-29 14:49:59 -04002744 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2745 char *userpage;
2746 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2747
2748 if (zero_offset) {
2749 iosize = PAGE_CACHE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002750 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002751 memset(userpage + zero_offset, 0, iosize);
2752 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002753 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002754 }
2755 }
Chris Masond1310b22008-01-24 16:13:08 -05002756 while (cur <= end) {
Josef Bacikc8f2f242013-02-11 11:33:00 -05002757 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2758
Chris Masond1310b22008-01-24 16:13:08 -05002759 if (cur >= last_byte) {
2760 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002761 struct extent_state *cached = NULL;
2762
David Sterba306e16c2011-04-19 14:29:38 +02002763 iosize = PAGE_CACHE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002764 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002765 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002766 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002767 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002768 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002769 &cached, GFP_NOFS);
2770 unlock_extent_cached(tree, cur, cur + iosize - 1,
2771 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002772 break;
2773 }
David Sterba306e16c2011-04-19 14:29:38 +02002774 em = get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002775 end - cur + 1, 0);
David Sterbac7040052011-04-19 18:00:01 +02002776 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002777 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002778 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002779 break;
2780 }
Chris Masond1310b22008-01-24 16:13:08 -05002781 extent_offset = cur - em->start;
2782 BUG_ON(extent_map_end(em) <= cur);
2783 BUG_ON(end < cur);
2784
Li Zefan261507a02010-12-17 14:21:50 +08002785 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002786 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002787 extent_set_compress_type(&this_bio_flag,
2788 em->compress_type);
2789 }
Chris Masonc8b97812008-10-29 14:49:59 -04002790
Chris Masond1310b22008-01-24 16:13:08 -05002791 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2792 cur_end = min(extent_map_end(em) - 1, end);
Qu Wenruofda28322013-02-26 08:10:22 +00002793 iosize = ALIGN(iosize, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -04002794 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2795 disk_io_size = em->block_len;
2796 sector = em->block_start >> 9;
2797 } else {
2798 sector = (em->block_start + extent_offset) >> 9;
2799 disk_io_size = iosize;
2800 }
Chris Masond1310b22008-01-24 16:13:08 -05002801 bdev = em->bdev;
2802 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002803 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2804 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002805 free_extent_map(em);
2806 em = NULL;
2807
2808 /* we've found a hole, just zero and go on */
2809 if (block_start == EXTENT_MAP_HOLE) {
2810 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002811 struct extent_state *cached = NULL;
2812
Cong Wang7ac687d2011-11-25 23:14:28 +08002813 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002814 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002815 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002816 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002817
2818 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002819 &cached, GFP_NOFS);
2820 unlock_extent_cached(tree, cur, cur + iosize - 1,
2821 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002822 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002823 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002824 continue;
2825 }
2826 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002827 if (test_range_bit(tree, cur, cur_end,
2828 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002829 check_page_uptodate(tree, page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002830 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002831 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002832 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002833 continue;
2834 }
Chris Mason70dec802008-01-29 09:59:12 -05002835 /* we have an inline extent but it didn't get marked up
2836 * to date. Error out
2837 */
2838 if (block_start == EXTENT_MAP_INLINE) {
2839 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002840 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05002841 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002842 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05002843 continue;
2844 }
Chris Masond1310b22008-01-24 16:13:08 -05002845
Josef Bacikc8f2f242013-02-11 11:33:00 -05002846 pnr -= page->index;
Josef Bacikd4c7ca82013-04-19 19:49:09 -04002847 ret = submit_extent_page(rw, tree, page,
David Sterba306e16c2011-04-19 14:29:38 +02002848 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04002849 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002850 end_bio_extent_readpage, mirror_num,
2851 *bio_flags,
2852 this_bio_flag);
Josef Bacikc8f2f242013-02-11 11:33:00 -05002853 if (!ret) {
2854 nr++;
2855 *bio_flags = this_bio_flag;
2856 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002857 SetPageError(page);
Josef Bacikedd33c92012-10-05 16:40:32 -04002858 unlock_extent(tree, cur, cur + iosize - 1);
2859 }
Chris Masond1310b22008-01-24 16:13:08 -05002860 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002861 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002862 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002863out:
Chris Masond1310b22008-01-24 16:13:08 -05002864 if (!nr) {
2865 if (!PageError(page))
2866 SetPageUptodate(page);
2867 unlock_page(page);
2868 }
2869 return 0;
2870}
2871
2872int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002873 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05002874{
2875 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002876 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002877 int ret;
2878
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002879 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04002880 &bio_flags, READ);
Chris Masond1310b22008-01-24 16:13:08 -05002881 if (bio)
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002882 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002883 return ret;
2884}
Chris Masond1310b22008-01-24 16:13:08 -05002885
Chris Mason11c83492009-04-20 15:50:09 -04002886static noinline void update_nr_written(struct page *page,
2887 struct writeback_control *wbc,
2888 unsigned long nr_written)
2889{
2890 wbc->nr_to_write -= nr_written;
2891 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2892 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2893 page->mapping->writeback_index = page->index + nr_written;
2894}
2895
Chris Masond1310b22008-01-24 16:13:08 -05002896/*
2897 * the writepage semantics are similar to regular writepage. extent
2898 * records are inserted to lock ranges in the tree, and as dirty areas
2899 * are found, they are marked writeback. Then the lock bits are removed
2900 * and the end_io handler clears the writeback ranges
2901 */
2902static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2903 void *data)
2904{
2905 struct inode *inode = page->mapping->host;
2906 struct extent_page_data *epd = data;
2907 struct extent_io_tree *tree = epd->tree;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002908 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05002909 u64 delalloc_start;
2910 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2911 u64 end;
2912 u64 cur = start;
2913 u64 extent_offset;
2914 u64 last_byte = i_size_read(inode);
2915 u64 block_start;
2916 u64 iosize;
2917 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002918 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002919 struct extent_map *em;
2920 struct block_device *bdev;
2921 int ret;
2922 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002923 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002924 size_t blocksize;
2925 loff_t i_size = i_size_read(inode);
2926 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2927 u64 nr_delalloc;
2928 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002929 int page_started;
2930 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002931 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002932 unsigned long nr_written = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002933 bool fill_delalloc = true;
Chris Masond1310b22008-01-24 16:13:08 -05002934
Chris Masonffbd5172009-04-20 15:50:09 -04002935 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002936 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002937 else
2938 write_flags = WRITE;
2939
liubo1abe9b82011-03-24 11:18:59 +00002940 trace___extent_writepage(page, inode, wbc);
2941
Chris Masond1310b22008-01-24 16:13:08 -05002942 WARN_ON(!PageLocked(page));
Chris Masonbf0da8c2011-11-04 12:29:37 -04002943
2944 ClearPageError(page);
2945
Chris Mason7f3c74f2008-07-18 12:01:11 -04002946 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002947 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002948 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002949 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002950 unlock_page(page);
2951 return 0;
2952 }
2953
2954 if (page->index == end_index) {
2955 char *userpage;
2956
Cong Wang7ac687d2011-11-25 23:14:28 +08002957 userpage = kmap_atomic(page);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002958 memset(userpage + pg_offset, 0,
2959 PAGE_CACHE_SIZE - pg_offset);
Cong Wang7ac687d2011-11-25 23:14:28 +08002960 kunmap_atomic(userpage);
Chris Mason211c17f2008-05-15 09:13:45 -04002961 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002962 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002963 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002964
2965 set_page_extent_mapped(page);
2966
Josef Bacik9e487102011-08-01 12:08:18 -04002967 if (!tree->ops || !tree->ops->fill_delalloc)
2968 fill_delalloc = false;
2969
Chris Masond1310b22008-01-24 16:13:08 -05002970 delalloc_start = start;
2971 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002972 page_started = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002973 if (!epd->extent_locked && fill_delalloc) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002974 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002975 /*
2976 * make sure the wbc mapping index is at least updated
2977 * to this page.
2978 */
2979 update_nr_written(page, wbc, 0);
2980
Chris Masond3977122009-01-05 21:25:51 -05002981 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002982 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002983 page,
2984 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002985 &delalloc_end,
2986 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002987 if (nr_delalloc == 0) {
2988 delalloc_start = delalloc_end + 1;
2989 continue;
2990 }
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002991 ret = tree->ops->fill_delalloc(inode, page,
2992 delalloc_start,
2993 delalloc_end,
2994 &page_started,
2995 &nr_written);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002996 /* File system has been set read-only */
2997 if (ret) {
2998 SetPageError(page);
2999 goto done;
3000 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003001 /*
3002 * delalloc_end is already one less than the total
3003 * length, so we don't subtract one from
3004 * PAGE_CACHE_SIZE
3005 */
3006 delalloc_to_write += (delalloc_end - delalloc_start +
3007 PAGE_CACHE_SIZE) >>
3008 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003009 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05003010 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003011 if (wbc->nr_to_write < delalloc_to_write) {
3012 int thresh = 8192;
3013
3014 if (delalloc_to_write < thresh * 2)
3015 thresh = delalloc_to_write;
3016 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3017 thresh);
3018 }
Chris Masonc8b97812008-10-29 14:49:59 -04003019
Chris Mason771ed682008-11-06 22:02:51 -05003020 /* did the fill delalloc function already unlock and start
3021 * the IO?
3022 */
3023 if (page_started) {
3024 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04003025 /*
3026 * we've unlocked the page, so we can't update
3027 * the mapping's writeback index, just update
3028 * nr_to_write.
3029 */
3030 wbc->nr_to_write -= nr_written;
3031 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05003032 }
Chris Masonc8b97812008-10-29 14:49:59 -04003033 }
Chris Mason247e7432008-07-17 12:53:51 -04003034 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04003035 ret = tree->ops->writepage_start_hook(page, start,
3036 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01003037 if (ret) {
3038 /* Fixup worker will requeue */
3039 if (ret == -EBUSY)
3040 wbc->pages_skipped++;
3041 else
3042 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04003043 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04003044 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05003045 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04003046 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04003047 }
3048 }
3049
Chris Mason11c83492009-04-20 15:50:09 -04003050 /*
3051 * we don't want to touch the inode after unlocking the page,
3052 * so we update the mapping writeback index now
3053 */
3054 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05003055
Chris Masond1310b22008-01-24 16:13:08 -05003056 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05003057 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003058 if (tree->ops && tree->ops->writepage_end_io_hook)
3059 tree->ops->writepage_end_io_hook(page, start,
3060 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003061 goto done;
3062 }
3063
Chris Masond1310b22008-01-24 16:13:08 -05003064 blocksize = inode->i_sb->s_blocksize;
3065
3066 while (cur <= end) {
3067 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003068 if (tree->ops && tree->ops->writepage_end_io_hook)
3069 tree->ops->writepage_end_io_hook(page, cur,
3070 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003071 break;
3072 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003073 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05003074 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02003075 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003076 SetPageError(page);
3077 break;
3078 }
3079
3080 extent_offset = cur - em->start;
3081 BUG_ON(extent_map_end(em) <= cur);
3082 BUG_ON(end < cur);
3083 iosize = min(extent_map_end(em) - cur, end - cur + 1);
Qu Wenruofda28322013-02-26 08:10:22 +00003084 iosize = ALIGN(iosize, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05003085 sector = (em->block_start + extent_offset) >> 9;
3086 bdev = em->bdev;
3087 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003088 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05003089 free_extent_map(em);
3090 em = NULL;
3091
Chris Masonc8b97812008-10-29 14:49:59 -04003092 /*
3093 * compressed and inline extents are written through other
3094 * paths in the FS
3095 */
3096 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003097 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003098 /*
3099 * end_io notification does not happen here for
3100 * compressed extents
3101 */
3102 if (!compressed && tree->ops &&
3103 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003104 tree->ops->writepage_end_io_hook(page, cur,
3105 cur + iosize - 1,
3106 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003107 else if (compressed) {
3108 /* we don't want to end_page_writeback on
3109 * a compressed extent. this happens
3110 * elsewhere
3111 */
3112 nr++;
3113 }
3114
3115 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003116 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003117 continue;
3118 }
Chris Masond1310b22008-01-24 16:13:08 -05003119 /* leave this out until we have a page_mkwrite call */
3120 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04003121 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05003122 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003123 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003124 continue;
3125 }
Chris Masonc8b97812008-10-29 14:49:59 -04003126
Chris Masond1310b22008-01-24 16:13:08 -05003127 if (tree->ops && tree->ops->writepage_io_hook) {
3128 ret = tree->ops->writepage_io_hook(page, cur,
3129 cur + iosize - 1);
3130 } else {
3131 ret = 0;
3132 }
Chris Mason1259ab72008-05-12 13:39:03 -04003133 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003134 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003135 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003136 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003137
Chris Masond1310b22008-01-24 16:13:08 -05003138 set_range_writeback(tree, cur, cur + iosize - 1);
3139 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05003140 printk(KERN_ERR "btrfs warning page %lu not "
3141 "writeback, cur %llu end %llu\n",
3142 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05003143 (unsigned long long)end);
3144 }
3145
Chris Masonffbd5172009-04-20 15:50:09 -04003146 ret = submit_extent_page(write_flags, tree, page,
3147 sector, iosize, pg_offset,
3148 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04003149 end_bio_extent_writepage,
3150 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003151 if (ret)
3152 SetPageError(page);
3153 }
3154 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003155 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003156 nr++;
3157 }
3158done:
3159 if (nr == 0) {
3160 /* make sure the mapping tag for page dirty gets cleared */
3161 set_page_writeback(page);
3162 end_page_writeback(page);
3163 }
Chris Masond1310b22008-01-24 16:13:08 -05003164 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05003165
Chris Mason11c83492009-04-20 15:50:09 -04003166done_unlocked:
3167
Chris Mason2c64c532009-09-02 15:04:12 -04003168 /* drop our reference on any cached states */
3169 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05003170 return 0;
3171}
3172
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003173static int eb_wait(void *word)
3174{
3175 io_schedule();
3176 return 0;
3177}
3178
Josef Bacikfd8b2b62013-04-24 16:41:19 -04003179void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003180{
3181 wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
3182 TASK_UNINTERRUPTIBLE);
3183}
3184
3185static int lock_extent_buffer_for_io(struct extent_buffer *eb,
3186 struct btrfs_fs_info *fs_info,
3187 struct extent_page_data *epd)
3188{
3189 unsigned long i, num_pages;
3190 int flush = 0;
3191 int ret = 0;
3192
3193 if (!btrfs_try_tree_write_lock(eb)) {
3194 flush = 1;
3195 flush_write_bio(epd);
3196 btrfs_tree_lock(eb);
3197 }
3198
3199 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3200 btrfs_tree_unlock(eb);
3201 if (!epd->sync_io)
3202 return 0;
3203 if (!flush) {
3204 flush_write_bio(epd);
3205 flush = 1;
3206 }
Chris Masona098d8e2012-03-21 12:09:56 -04003207 while (1) {
3208 wait_on_extent_buffer_writeback(eb);
3209 btrfs_tree_lock(eb);
3210 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3211 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003212 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003213 }
3214 }
3215
Josef Bacik51561ff2012-07-20 16:25:24 -04003216 /*
3217 * We need to do this to prevent races in people who check if the eb is
3218 * under IO since we can end up having no IO bits set for a short period
3219 * of time.
3220 */
3221 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003222 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3223 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003224 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003225 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Miao Xiee2d84522013-01-29 10:09:20 +00003226 __percpu_counter_add(&fs_info->dirty_metadata_bytes,
3227 -eb->len,
3228 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003229 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003230 } else {
3231 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003232 }
3233
3234 btrfs_tree_unlock(eb);
3235
3236 if (!ret)
3237 return ret;
3238
3239 num_pages = num_extent_pages(eb->start, eb->len);
3240 for (i = 0; i < num_pages; i++) {
3241 struct page *p = extent_buffer_page(eb, i);
3242
3243 if (!trylock_page(p)) {
3244 if (!flush) {
3245 flush_write_bio(epd);
3246 flush = 1;
3247 }
3248 lock_page(p);
3249 }
3250 }
3251
3252 return ret;
3253}
3254
3255static void end_extent_buffer_writeback(struct extent_buffer *eb)
3256{
3257 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3258 smp_mb__after_clear_bit();
3259 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3260}
3261
3262static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
3263{
3264 int uptodate = err == 0;
3265 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
3266 struct extent_buffer *eb;
3267 int done;
3268
3269 do {
3270 struct page *page = bvec->bv_page;
3271
3272 bvec--;
3273 eb = (struct extent_buffer *)page->private;
3274 BUG_ON(!eb);
3275 done = atomic_dec_and_test(&eb->io_pages);
3276
3277 if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
3278 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3279 ClearPageUptodate(page);
3280 SetPageError(page);
3281 }
3282
3283 end_page_writeback(page);
3284
3285 if (!done)
3286 continue;
3287
3288 end_extent_buffer_writeback(eb);
3289 } while (bvec >= bio->bi_io_vec);
3290
3291 bio_put(bio);
3292
3293}
3294
3295static int write_one_eb(struct extent_buffer *eb,
3296 struct btrfs_fs_info *fs_info,
3297 struct writeback_control *wbc,
3298 struct extent_page_data *epd)
3299{
3300 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
3301 u64 offset = eb->start;
3302 unsigned long i, num_pages;
Josef Bacikde0022b2012-09-25 14:25:58 -04003303 unsigned long bio_flags = 0;
Josef Bacikd4c7ca82013-04-19 19:49:09 -04003304 int rw = (epd->sync_io ? WRITE_SYNC : WRITE) | REQ_META;
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003305 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003306
3307 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3308 num_pages = num_extent_pages(eb->start, eb->len);
3309 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003310 if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
3311 bio_flags = EXTENT_BIO_TREE_LOG;
3312
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003313 for (i = 0; i < num_pages; i++) {
3314 struct page *p = extent_buffer_page(eb, i);
3315
3316 clear_page_dirty_for_io(p);
3317 set_page_writeback(p);
3318 ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
3319 PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
3320 -1, end_bio_extent_buffer_writepage,
Josef Bacikde0022b2012-09-25 14:25:58 -04003321 0, epd->bio_flags, bio_flags);
3322 epd->bio_flags = bio_flags;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003323 if (ret) {
3324 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3325 SetPageError(p);
3326 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3327 end_extent_buffer_writeback(eb);
3328 ret = -EIO;
3329 break;
3330 }
3331 offset += PAGE_CACHE_SIZE;
3332 update_nr_written(p, wbc, 1);
3333 unlock_page(p);
3334 }
3335
3336 if (unlikely(ret)) {
3337 for (; i < num_pages; i++) {
3338 struct page *p = extent_buffer_page(eb, i);
3339 unlock_page(p);
3340 }
3341 }
3342
3343 return ret;
3344}
3345
3346int btree_write_cache_pages(struct address_space *mapping,
3347 struct writeback_control *wbc)
3348{
3349 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3350 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3351 struct extent_buffer *eb, *prev_eb = NULL;
3352 struct extent_page_data epd = {
3353 .bio = NULL,
3354 .tree = tree,
3355 .extent_locked = 0,
3356 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003357 .bio_flags = 0,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003358 };
3359 int ret = 0;
3360 int done = 0;
3361 int nr_to_write_done = 0;
3362 struct pagevec pvec;
3363 int nr_pages;
3364 pgoff_t index;
3365 pgoff_t end; /* Inclusive */
3366 int scanned = 0;
3367 int tag;
3368
3369 pagevec_init(&pvec, 0);
3370 if (wbc->range_cyclic) {
3371 index = mapping->writeback_index; /* Start from prev offset */
3372 end = -1;
3373 } else {
3374 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3375 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3376 scanned = 1;
3377 }
3378 if (wbc->sync_mode == WB_SYNC_ALL)
3379 tag = PAGECACHE_TAG_TOWRITE;
3380 else
3381 tag = PAGECACHE_TAG_DIRTY;
3382retry:
3383 if (wbc->sync_mode == WB_SYNC_ALL)
3384 tag_pages_for_writeback(mapping, index, end);
3385 while (!done && !nr_to_write_done && (index <= end) &&
3386 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3387 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3388 unsigned i;
3389
3390 scanned = 1;
3391 for (i = 0; i < nr_pages; i++) {
3392 struct page *page = pvec.pages[i];
3393
3394 if (!PagePrivate(page))
3395 continue;
3396
3397 if (!wbc->range_cyclic && page->index > end) {
3398 done = 1;
3399 break;
3400 }
3401
Josef Bacikb5bae262012-09-14 13:43:01 -04003402 spin_lock(&mapping->private_lock);
3403 if (!PagePrivate(page)) {
3404 spin_unlock(&mapping->private_lock);
3405 continue;
3406 }
3407
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003408 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003409
3410 /*
3411 * Shouldn't happen and normally this would be a BUG_ON
3412 * but no sense in crashing the users box for something
3413 * we can survive anyway.
3414 */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003415 if (!eb) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003416 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003417 WARN_ON(1);
3418 continue;
3419 }
3420
Josef Bacikb5bae262012-09-14 13:43:01 -04003421 if (eb == prev_eb) {
3422 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003423 continue;
3424 }
3425
Josef Bacikb5bae262012-09-14 13:43:01 -04003426 ret = atomic_inc_not_zero(&eb->refs);
3427 spin_unlock(&mapping->private_lock);
3428 if (!ret)
3429 continue;
3430
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003431 prev_eb = eb;
3432 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3433 if (!ret) {
3434 free_extent_buffer(eb);
3435 continue;
3436 }
3437
3438 ret = write_one_eb(eb, fs_info, wbc, &epd);
3439 if (ret) {
3440 done = 1;
3441 free_extent_buffer(eb);
3442 break;
3443 }
3444 free_extent_buffer(eb);
3445
3446 /*
3447 * the filesystem may choose to bump up nr_to_write.
3448 * We have to make sure to honor the new nr_to_write
3449 * at any time
3450 */
3451 nr_to_write_done = wbc->nr_to_write <= 0;
3452 }
3453 pagevec_release(&pvec);
3454 cond_resched();
3455 }
3456 if (!scanned && !done) {
3457 /*
3458 * We hit the last page and there is more work to be done: wrap
3459 * back to the start of the file
3460 */
3461 scanned = 1;
3462 index = 0;
3463 goto retry;
3464 }
3465 flush_write_bio(&epd);
3466 return ret;
3467}
3468
Chris Masond1310b22008-01-24 16:13:08 -05003469/**
Chris Mason4bef0842008-09-08 11:18:08 -04003470 * 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 -05003471 * @mapping: address space structure to write
3472 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3473 * @writepage: function called for each page
3474 * @data: data passed to writepage function
3475 *
3476 * If a page is already under I/O, write_cache_pages() skips it, even
3477 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3478 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3479 * and msync() need to guarantee that all the data which was dirty at the time
3480 * the call was made get new I/O started against them. If wbc->sync_mode is
3481 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3482 * existing IO to complete.
3483 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003484static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04003485 struct address_space *mapping,
3486 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003487 writepage_t writepage, void *data,
3488 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05003489{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003490 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003491 int ret = 0;
3492 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003493 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003494 struct pagevec pvec;
3495 int nr_pages;
3496 pgoff_t index;
3497 pgoff_t end; /* Inclusive */
3498 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003499 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003500
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003501 /*
3502 * We have to hold onto the inode so that ordered extents can do their
3503 * work when the IO finishes. The alternative to this is failing to add
3504 * an ordered extent if the igrab() fails there and that is a huge pain
3505 * to deal with, so instead just hold onto the inode throughout the
3506 * writepages operation. If it fails here we are freeing up the inode
3507 * anyway and we'd rather not waste our time writing out stuff that is
3508 * going to be truncated anyway.
3509 */
3510 if (!igrab(inode))
3511 return 0;
3512
Chris Masond1310b22008-01-24 16:13:08 -05003513 pagevec_init(&pvec, 0);
3514 if (wbc->range_cyclic) {
3515 index = mapping->writeback_index; /* Start from prev offset */
3516 end = -1;
3517 } else {
3518 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3519 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003520 scanned = 1;
3521 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003522 if (wbc->sync_mode == WB_SYNC_ALL)
3523 tag = PAGECACHE_TAG_TOWRITE;
3524 else
3525 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003526retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003527 if (wbc->sync_mode == WB_SYNC_ALL)
3528 tag_pages_for_writeback(mapping, index, end);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003529 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003530 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3531 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003532 unsigned i;
3533
3534 scanned = 1;
3535 for (i = 0; i < nr_pages; i++) {
3536 struct page *page = pvec.pages[i];
3537
3538 /*
3539 * At this point we hold neither mapping->tree_lock nor
3540 * lock on the page itself: the page may be truncated or
3541 * invalidated (changing page->mapping to NULL), or even
3542 * swizzled back from swapper_space to tmpfs file
3543 * mapping
3544 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05003545 if (!trylock_page(page)) {
3546 flush_fn(data);
3547 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04003548 }
Chris Masond1310b22008-01-24 16:13:08 -05003549
3550 if (unlikely(page->mapping != mapping)) {
3551 unlock_page(page);
3552 continue;
3553 }
3554
3555 if (!wbc->range_cyclic && page->index > end) {
3556 done = 1;
3557 unlock_page(page);
3558 continue;
3559 }
3560
Chris Masond2c3f4f2008-11-19 12:44:22 -05003561 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003562 if (PageWriteback(page))
3563 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05003564 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003565 }
Chris Masond1310b22008-01-24 16:13:08 -05003566
3567 if (PageWriteback(page) ||
3568 !clear_page_dirty_for_io(page)) {
3569 unlock_page(page);
3570 continue;
3571 }
3572
3573 ret = (*writepage)(page, wbc, data);
3574
3575 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3576 unlock_page(page);
3577 ret = 0;
3578 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003579 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05003580 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003581
3582 /*
3583 * the filesystem may choose to bump up nr_to_write.
3584 * We have to make sure to honor the new nr_to_write
3585 * at any time
3586 */
3587 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05003588 }
3589 pagevec_release(&pvec);
3590 cond_resched();
3591 }
3592 if (!scanned && !done) {
3593 /*
3594 * We hit the last page and there is more work to be done: wrap
3595 * back to the start of the file
3596 */
3597 scanned = 1;
3598 index = 0;
3599 goto retry;
3600 }
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003601 btrfs_add_delayed_iput(inode);
Chris Masond1310b22008-01-24 16:13:08 -05003602 return ret;
3603}
Chris Masond1310b22008-01-24 16:13:08 -05003604
Chris Masonffbd5172009-04-20 15:50:09 -04003605static void flush_epd_write_bio(struct extent_page_data *epd)
3606{
3607 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04003608 int rw = WRITE;
3609 int ret;
3610
Chris Masonffbd5172009-04-20 15:50:09 -04003611 if (epd->sync_io)
Jeff Mahoney355808c2011-10-03 23:23:14 -04003612 rw = WRITE_SYNC;
3613
Josef Bacikde0022b2012-09-25 14:25:58 -04003614 ret = submit_one_bio(rw, epd->bio, 0, epd->bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003615 BUG_ON(ret < 0); /* -ENOMEM */
Chris Masonffbd5172009-04-20 15:50:09 -04003616 epd->bio = NULL;
3617 }
3618}
3619
Chris Masond2c3f4f2008-11-19 12:44:22 -05003620static noinline void flush_write_bio(void *data)
3621{
3622 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04003623 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003624}
3625
Chris Masond1310b22008-01-24 16:13:08 -05003626int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3627 get_extent_t *get_extent,
3628 struct writeback_control *wbc)
3629{
3630 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003631 struct extent_page_data epd = {
3632 .bio = NULL,
3633 .tree = tree,
3634 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003635 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003636 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003637 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05003638 };
Chris Masond1310b22008-01-24 16:13:08 -05003639
Chris Masond1310b22008-01-24 16:13:08 -05003640 ret = __extent_writepage(page, wbc, &epd);
3641
Chris Masonffbd5172009-04-20 15:50:09 -04003642 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003643 return ret;
3644}
Chris Masond1310b22008-01-24 16:13:08 -05003645
Chris Mason771ed682008-11-06 22:02:51 -05003646int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3647 u64 start, u64 end, get_extent_t *get_extent,
3648 int mode)
3649{
3650 int ret = 0;
3651 struct address_space *mapping = inode->i_mapping;
3652 struct page *page;
3653 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3654 PAGE_CACHE_SHIFT;
3655
3656 struct extent_page_data epd = {
3657 .bio = NULL,
3658 .tree = tree,
3659 .get_extent = get_extent,
3660 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04003661 .sync_io = mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003662 .bio_flags = 0,
Chris Mason771ed682008-11-06 22:02:51 -05003663 };
3664 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05003665 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05003666 .nr_to_write = nr_pages * 2,
3667 .range_start = start,
3668 .range_end = end + 1,
3669 };
3670
Chris Masond3977122009-01-05 21:25:51 -05003671 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05003672 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3673 if (clear_page_dirty_for_io(page))
3674 ret = __extent_writepage(page, &wbc_writepages, &epd);
3675 else {
3676 if (tree->ops && tree->ops->writepage_end_io_hook)
3677 tree->ops->writepage_end_io_hook(page, start,
3678 start + PAGE_CACHE_SIZE - 1,
3679 NULL, 1);
3680 unlock_page(page);
3681 }
3682 page_cache_release(page);
3683 start += PAGE_CACHE_SIZE;
3684 }
3685
Chris Masonffbd5172009-04-20 15:50:09 -04003686 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05003687 return ret;
3688}
Chris Masond1310b22008-01-24 16:13:08 -05003689
3690int extent_writepages(struct extent_io_tree *tree,
3691 struct address_space *mapping,
3692 get_extent_t *get_extent,
3693 struct writeback_control *wbc)
3694{
3695 int ret = 0;
3696 struct extent_page_data epd = {
3697 .bio = NULL,
3698 .tree = tree,
3699 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003700 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003701 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003702 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05003703 };
3704
Chris Mason4bef0842008-09-08 11:18:08 -04003705 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003706 __extent_writepage, &epd,
3707 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04003708 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003709 return ret;
3710}
Chris Masond1310b22008-01-24 16:13:08 -05003711
3712int extent_readpages(struct extent_io_tree *tree,
3713 struct address_space *mapping,
3714 struct list_head *pages, unsigned nr_pages,
3715 get_extent_t get_extent)
3716{
3717 struct bio *bio = NULL;
3718 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04003719 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06003720 struct page *pagepool[16];
3721 struct page *page;
3722 int i = 0;
3723 int nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003724
Chris Masond1310b22008-01-24 16:13:08 -05003725 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
Liu Bo67c96842012-07-20 21:43:09 -06003726 page = list_entry(pages->prev, struct page, lru);
Chris Masond1310b22008-01-24 16:13:08 -05003727
3728 prefetchw(&page->flags);
3729 list_del(&page->lru);
Liu Bo67c96842012-07-20 21:43:09 -06003730 if (add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04003731 page->index, GFP_NOFS)) {
Liu Bo67c96842012-07-20 21:43:09 -06003732 page_cache_release(page);
3733 continue;
Chris Masond1310b22008-01-24 16:13:08 -05003734 }
Liu Bo67c96842012-07-20 21:43:09 -06003735
3736 pagepool[nr++] = page;
3737 if (nr < ARRAY_SIZE(pagepool))
3738 continue;
3739 for (i = 0; i < nr; i++) {
3740 __extent_read_full_page(tree, pagepool[i], get_extent,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04003741 &bio, 0, &bio_flags, READ);
Liu Bo67c96842012-07-20 21:43:09 -06003742 page_cache_release(pagepool[i]);
3743 }
3744 nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003745 }
Liu Bo67c96842012-07-20 21:43:09 -06003746 for (i = 0; i < nr; i++) {
3747 __extent_read_full_page(tree, pagepool[i], get_extent,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04003748 &bio, 0, &bio_flags, READ);
Liu Bo67c96842012-07-20 21:43:09 -06003749 page_cache_release(pagepool[i]);
3750 }
3751
Chris Masond1310b22008-01-24 16:13:08 -05003752 BUG_ON(!list_empty(pages));
3753 if (bio)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003754 return submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003755 return 0;
3756}
Chris Masond1310b22008-01-24 16:13:08 -05003757
3758/*
3759 * basic invalidatepage code, this waits on any locked or writeback
3760 * ranges corresponding to the page, and then deletes any extent state
3761 * records from the tree
3762 */
3763int extent_invalidatepage(struct extent_io_tree *tree,
3764 struct page *page, unsigned long offset)
3765{
Josef Bacik2ac55d42010-02-03 19:33:23 +00003766 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003767 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05003768 u64 end = start + PAGE_CACHE_SIZE - 1;
3769 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3770
Qu Wenruofda28322013-02-26 08:10:22 +00003771 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05003772 if (start > end)
3773 return 0;
3774
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003775 lock_extent_bits(tree, start, end, 0, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04003776 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003777 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04003778 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3779 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003780 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003781 return 0;
3782}
Chris Masond1310b22008-01-24 16:13:08 -05003783
3784/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04003785 * a helper for releasepage, this tests for areas of the page that
3786 * are locked or under IO and drops the related state bits if it is safe
3787 * to drop the page.
3788 */
3789int try_release_extent_state(struct extent_map_tree *map,
3790 struct extent_io_tree *tree, struct page *page,
3791 gfp_t mask)
3792{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003793 u64 start = page_offset(page);
Chris Mason7b13b7b2008-04-18 10:29:50 -04003794 u64 end = start + PAGE_CACHE_SIZE - 1;
3795 int ret = 1;
3796
Chris Mason211f90e2008-07-18 11:56:15 -04003797 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04003798 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04003799 ret = 0;
3800 else {
3801 if ((mask & GFP_NOFS) == GFP_NOFS)
3802 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04003803 /*
3804 * at this point we can safely clear everything except the
3805 * locked bit and the nodatasum bit
3806 */
Chris Masone3f24cc2011-02-14 12:52:08 -05003807 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04003808 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3809 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05003810
3811 /* if clear_extent_bit failed for enomem reasons,
3812 * we can't allow the release to continue.
3813 */
3814 if (ret < 0)
3815 ret = 0;
3816 else
3817 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003818 }
3819 return ret;
3820}
Chris Mason7b13b7b2008-04-18 10:29:50 -04003821
3822/*
Chris Masond1310b22008-01-24 16:13:08 -05003823 * a helper for releasepage. As long as there are no locked extents
3824 * in the range corresponding to the page, both state records and extent
3825 * map records are removed
3826 */
3827int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05003828 struct extent_io_tree *tree, struct page *page,
3829 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05003830{
3831 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003832 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05003833 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003834
Chris Mason70dec802008-01-29 09:59:12 -05003835 if ((mask & __GFP_WAIT) &&
3836 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05003837 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05003838 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05003839 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04003840 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05003841 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09003842 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04003843 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003844 break;
3845 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003846 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3847 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04003848 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003849 free_extent_map(em);
3850 break;
3851 }
3852 if (!test_range_bit(tree, em->start,
3853 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04003854 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04003855 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05003856 remove_extent_mapping(map, em);
3857 /* once for the rb tree */
3858 free_extent_map(em);
3859 }
3860 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04003861 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003862
3863 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05003864 free_extent_map(em);
3865 }
Chris Masond1310b22008-01-24 16:13:08 -05003866 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04003867 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003868}
Chris Masond1310b22008-01-24 16:13:08 -05003869
Chris Masonec29ed52011-02-23 16:23:20 -05003870/*
3871 * helper function for fiemap, which doesn't want to see any holes.
3872 * This maps until we find something past 'last'
3873 */
3874static struct extent_map *get_extent_skip_holes(struct inode *inode,
3875 u64 offset,
3876 u64 last,
3877 get_extent_t *get_extent)
3878{
3879 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3880 struct extent_map *em;
3881 u64 len;
3882
3883 if (offset >= last)
3884 return NULL;
3885
3886 while(1) {
3887 len = last - offset;
3888 if (len == 0)
3889 break;
Qu Wenruofda28322013-02-26 08:10:22 +00003890 len = ALIGN(len, sectorsize);
Chris Masonec29ed52011-02-23 16:23:20 -05003891 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02003892 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05003893 return em;
3894
3895 /* if this isn't a hole return it */
3896 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3897 em->block_start != EXTENT_MAP_HOLE) {
3898 return em;
3899 }
3900
3901 /* this is a hole, advance to the next extent */
3902 offset = extent_map_end(em);
3903 free_extent_map(em);
3904 if (offset >= last)
3905 break;
3906 }
3907 return NULL;
3908}
3909
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003910int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3911 __u64 start, __u64 len, get_extent_t *get_extent)
3912{
Josef Bacik975f84f2010-11-23 19:36:57 +00003913 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003914 u64 off = start;
3915 u64 max = start + len;
3916 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00003917 u32 found_type;
3918 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05003919 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003920 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003921 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00003922 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003923 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003924 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00003925 struct btrfs_path *path;
3926 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003927 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003928 u64 em_start = 0;
3929 u64 em_len = 0;
3930 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003931 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003932
3933 if (len == 0)
3934 return -EINVAL;
3935
Josef Bacik975f84f2010-11-23 19:36:57 +00003936 path = btrfs_alloc_path();
3937 if (!path)
3938 return -ENOMEM;
3939 path->leave_spinning = 1;
3940
Josef Bacik4d479cf2011-11-17 11:34:31 -05003941 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3942 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3943
Chris Masonec29ed52011-02-23 16:23:20 -05003944 /*
3945 * lookup the last file extent. We're not using i_size here
3946 * because there might be preallocation past i_size
3947 */
Josef Bacik975f84f2010-11-23 19:36:57 +00003948 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
Li Zefan33345d012011-04-20 10:31:50 +08003949 path, btrfs_ino(inode), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00003950 if (ret < 0) {
3951 btrfs_free_path(path);
3952 return ret;
3953 }
3954 WARN_ON(!ret);
3955 path->slots[0]--;
3956 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3957 struct btrfs_file_extent_item);
3958 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3959 found_type = btrfs_key_type(&found_key);
3960
Chris Masonec29ed52011-02-23 16:23:20 -05003961 /* No extents, but there might be delalloc bits */
Li Zefan33345d012011-04-20 10:31:50 +08003962 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00003963 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05003964 /* have to trust i_size as the end */
3965 last = (u64)-1;
3966 last_for_get_extent = isize;
3967 } else {
3968 /*
3969 * remember the start of the last extent. There are a
3970 * bunch of different factors that go into the length of the
3971 * extent, so its much less complex to remember where it started
3972 */
3973 last = found_key.offset;
3974 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00003975 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003976 btrfs_free_path(path);
3977
Chris Masonec29ed52011-02-23 16:23:20 -05003978 /*
3979 * we might have some extents allocated but more delalloc past those
3980 * extents. so, we trust isize unless the start of the last extent is
3981 * beyond isize
3982 */
3983 if (last < isize) {
3984 last = (u64)-1;
3985 last_for_get_extent = isize;
3986 }
3987
Josef Bacik2ac55d42010-02-03 19:33:23 +00003988 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003989 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05003990
Josef Bacik4d479cf2011-11-17 11:34:31 -05003991 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05003992 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003993 if (!em)
3994 goto out;
3995 if (IS_ERR(em)) {
3996 ret = PTR_ERR(em);
3997 goto out;
3998 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003999
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004000 while (!end) {
Chris Masonea8efc72011-03-08 11:54:40 -05004001 u64 offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004002
Chris Masonea8efc72011-03-08 11:54:40 -05004003 /* break if the extent we found is outside the range */
4004 if (em->start >= max || extent_map_end(em) < off)
4005 break;
4006
4007 /*
4008 * get_extent may return an extent that starts before our
4009 * requested range. We have to make sure the ranges
4010 * we return to fiemap always move forward and don't
4011 * overlap, so adjust the offsets here
4012 */
4013 em_start = max(em->start, off);
4014
4015 /*
4016 * record the offset from the start of the extent
4017 * for adjusting the disk offset below
4018 */
4019 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05004020 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05004021 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05004022 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004023 disko = 0;
4024 flags = 0;
4025
Chris Masonea8efc72011-03-08 11:54:40 -05004026 /*
4027 * bump off for our next call to get_extent
4028 */
4029 off = extent_map_end(em);
4030 if (off >= max)
4031 end = 1;
4032
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004033 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004034 end = 1;
4035 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004036 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004037 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4038 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004039 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004040 flags |= (FIEMAP_EXTENT_DELALLOC |
4041 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004042 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05004043 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004044 }
4045 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4046 flags |= FIEMAP_EXTENT_ENCODED;
4047
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004048 free_extent_map(em);
4049 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05004050 if ((em_start >= last) || em_len == (u64)-1 ||
4051 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004052 flags |= FIEMAP_EXTENT_LAST;
4053 end = 1;
4054 }
4055
Chris Masonec29ed52011-02-23 16:23:20 -05004056 /* now scan forward to see if this is really the last extent. */
4057 em = get_extent_skip_holes(inode, off, last_for_get_extent,
4058 get_extent);
4059 if (IS_ERR(em)) {
4060 ret = PTR_ERR(em);
4061 goto out;
4062 }
4063 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00004064 flags |= FIEMAP_EXTENT_LAST;
4065 end = 1;
4066 }
Chris Masonec29ed52011-02-23 16:23:20 -05004067 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
4068 em_len, flags);
4069 if (ret)
4070 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004071 }
4072out_free:
4073 free_extent_map(em);
4074out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00004075 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
4076 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004077 return ret;
4078}
4079
Chris Mason727011e2010-08-06 13:21:20 -04004080static void __free_extent_buffer(struct extent_buffer *eb)
4081{
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004082 btrfs_leak_debug_del(&eb->leak_list);
Chris Mason727011e2010-08-06 13:21:20 -04004083 kmem_cache_free(extent_buffer_cache, eb);
4084}
4085
Chris Masond1310b22008-01-24 16:13:08 -05004086static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
4087 u64 start,
4088 unsigned long len,
4089 gfp_t mask)
4090{
4091 struct extent_buffer *eb = NULL;
4092
Chris Masond1310b22008-01-24 16:13:08 -05004093 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00004094 if (eb == NULL)
4095 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004096 eb->start = start;
4097 eb->len = len;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004098 eb->tree = tree;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004099 eb->bflags = 0;
Chris Masonbd681512011-07-16 15:23:14 -04004100 rwlock_init(&eb->lock);
4101 atomic_set(&eb->write_locks, 0);
4102 atomic_set(&eb->read_locks, 0);
4103 atomic_set(&eb->blocking_readers, 0);
4104 atomic_set(&eb->blocking_writers, 0);
4105 atomic_set(&eb->spinning_readers, 0);
4106 atomic_set(&eb->spinning_writers, 0);
Arne Jansen5b25f702011-09-13 10:55:48 +02004107 eb->lock_nested = 0;
Chris Masonbd681512011-07-16 15:23:14 -04004108 init_waitqueue_head(&eb->write_lock_wq);
4109 init_waitqueue_head(&eb->read_lock_wq);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004110
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004111 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4112
Josef Bacik3083ee22012-03-09 16:01:49 -05004113 spin_lock_init(&eb->refs_lock);
Chris Masond1310b22008-01-24 16:13:08 -05004114 atomic_set(&eb->refs, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004115 atomic_set(&eb->io_pages, 0);
Chris Mason727011e2010-08-06 13:21:20 -04004116
David Sterbab8dae312013-02-28 14:54:18 +00004117 /*
4118 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4119 */
4120 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4121 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4122 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05004123
4124 return eb;
4125}
4126
Jan Schmidt815a51c2012-05-16 17:00:02 +02004127struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4128{
4129 unsigned long i;
4130 struct page *p;
4131 struct extent_buffer *new;
4132 unsigned long num_pages = num_extent_pages(src->start, src->len);
4133
4134 new = __alloc_extent_buffer(NULL, src->start, src->len, GFP_ATOMIC);
4135 if (new == NULL)
4136 return NULL;
4137
4138 for (i = 0; i < num_pages; i++) {
4139 p = alloc_page(GFP_ATOMIC);
4140 BUG_ON(!p);
4141 attach_extent_buffer_page(new, p);
4142 WARN_ON(PageDirty(p));
4143 SetPageUptodate(p);
4144 new->pages[i] = p;
4145 }
4146
4147 copy_extent_buffer(new, src, 0, 0, src->len);
4148 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
4149 set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
4150
4151 return new;
4152}
4153
4154struct extent_buffer *alloc_dummy_extent_buffer(u64 start, unsigned long len)
4155{
4156 struct extent_buffer *eb;
4157 unsigned long num_pages = num_extent_pages(0, len);
4158 unsigned long i;
4159
4160 eb = __alloc_extent_buffer(NULL, start, len, GFP_ATOMIC);
4161 if (!eb)
4162 return NULL;
4163
4164 for (i = 0; i < num_pages; i++) {
4165 eb->pages[i] = alloc_page(GFP_ATOMIC);
4166 if (!eb->pages[i])
4167 goto err;
4168 }
4169 set_extent_buffer_uptodate(eb);
4170 btrfs_set_header_nritems(eb, 0);
4171 set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4172
4173 return eb;
4174err:
Stefan Behrens84167d12012-10-11 07:25:16 -06004175 for (; i > 0; i--)
4176 __free_page(eb->pages[i - 1]);
Jan Schmidt815a51c2012-05-16 17:00:02 +02004177 __free_extent_buffer(eb);
4178 return NULL;
4179}
4180
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004181static int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004182{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004183 return (atomic_read(&eb->io_pages) ||
4184 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4185 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004186}
4187
Miao Xie897ca6e2010-10-26 20:57:29 -04004188/*
4189 * Helper for releasing extent buffer page.
4190 */
4191static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
4192 unsigned long start_idx)
4193{
4194 unsigned long index;
Wang Sheng-Hui39bab872012-04-06 14:35:31 +08004195 unsigned long num_pages;
Miao Xie897ca6e2010-10-26 20:57:29 -04004196 struct page *page;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004197 int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
Miao Xie897ca6e2010-10-26 20:57:29 -04004198
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004199 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e2010-10-26 20:57:29 -04004200
Wang Sheng-Hui39bab872012-04-06 14:35:31 +08004201 num_pages = num_extent_pages(eb->start, eb->len);
4202 index = start_idx + num_pages;
Miao Xie897ca6e2010-10-26 20:57:29 -04004203 if (start_idx >= index)
4204 return;
4205
4206 do {
4207 index--;
4208 page = extent_buffer_page(eb, index);
Jan Schmidt815a51c2012-05-16 17:00:02 +02004209 if (page && mapped) {
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004210 spin_lock(&page->mapping->private_lock);
4211 /*
4212 * We do this since we'll remove the pages after we've
4213 * removed the eb from the radix tree, so we could race
4214 * and have this page now attached to the new eb. So
4215 * only clear page_private if it's still connected to
4216 * this eb.
4217 */
4218 if (PagePrivate(page) &&
4219 page->private == (unsigned long)eb) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004220 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Josef Bacik3083ee22012-03-09 16:01:49 -05004221 BUG_ON(PageDirty(page));
4222 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004223 /*
4224 * We need to make sure we haven't be attached
4225 * to a new eb.
4226 */
4227 ClearPagePrivate(page);
4228 set_page_private(page, 0);
4229 /* One for the page private */
4230 page_cache_release(page);
4231 }
4232 spin_unlock(&page->mapping->private_lock);
4233
Jan Schmidt815a51c2012-05-16 17:00:02 +02004234 }
4235 if (page) {
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004236 /* One for when we alloced the page */
Miao Xie897ca6e2010-10-26 20:57:29 -04004237 page_cache_release(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004238 }
Miao Xie897ca6e2010-10-26 20:57:29 -04004239 } while (index != start_idx);
4240}
4241
4242/*
4243 * Helper for releasing the extent buffer.
4244 */
4245static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4246{
4247 btrfs_release_extent_buffer_page(eb, 0);
4248 __free_extent_buffer(eb);
4249}
4250
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004251static void check_buffer_tree_ref(struct extent_buffer *eb)
4252{
Chris Mason242e18c2013-01-29 17:49:37 -05004253 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004254 /* the ref bit is tricky. We have to make sure it is set
4255 * if we have the buffer dirty. Otherwise the
4256 * code to free a buffer can end up dropping a dirty
4257 * page
4258 *
4259 * Once the ref bit is set, it won't go away while the
4260 * buffer is dirty or in writeback, and it also won't
4261 * go away while we have the reference count on the
4262 * eb bumped.
4263 *
4264 * We can't just set the ref bit without bumping the
4265 * ref on the eb because free_extent_buffer might
4266 * see the ref bit and try to clear it. If this happens
4267 * free_extent_buffer might end up dropping our original
4268 * ref by mistake and freeing the page before we are able
4269 * to add one more ref.
4270 *
4271 * So bump the ref count first, then set the bit. If someone
4272 * beat us to it, drop the ref we added.
4273 */
Chris Mason242e18c2013-01-29 17:49:37 -05004274 refs = atomic_read(&eb->refs);
4275 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4276 return;
4277
Josef Bacik594831c2012-07-20 16:11:08 -04004278 spin_lock(&eb->refs_lock);
4279 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004280 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004281 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004282}
4283
Josef Bacik5df42352012-03-15 18:24:42 -04004284static void mark_extent_buffer_accessed(struct extent_buffer *eb)
4285{
4286 unsigned long num_pages, i;
4287
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004288 check_buffer_tree_ref(eb);
4289
Josef Bacik5df42352012-03-15 18:24:42 -04004290 num_pages = num_extent_pages(eb->start, eb->len);
4291 for (i = 0; i < num_pages; i++) {
4292 struct page *p = extent_buffer_page(eb, i);
4293 mark_page_accessed(p);
4294 }
4295}
4296
Chris Masond1310b22008-01-24 16:13:08 -05004297struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
Chris Mason727011e2010-08-06 13:21:20 -04004298 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004299{
4300 unsigned long num_pages = num_extent_pages(start, len);
4301 unsigned long i;
4302 unsigned long index = start >> PAGE_CACHE_SHIFT;
4303 struct extent_buffer *eb;
Chris Mason6af118c2008-07-22 11:18:07 -04004304 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004305 struct page *p;
4306 struct address_space *mapping = tree->mapping;
4307 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004308 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004309
Miao Xie19fe0a82010-10-26 20:57:29 -04004310 rcu_read_lock();
4311 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4312 if (eb && atomic_inc_not_zero(&eb->refs)) {
4313 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004314 mark_extent_buffer_accessed(eb);
Chris Mason6af118c2008-07-22 11:18:07 -04004315 return eb;
4316 }
Miao Xie19fe0a82010-10-26 20:57:29 -04004317 rcu_read_unlock();
Chris Mason6af118c2008-07-22 11:18:07 -04004318
David Sterbaba144192011-04-21 01:12:06 +02004319 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
Peter2b114d12008-04-01 11:21:40 -04004320 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004321 return NULL;
4322
Chris Mason727011e2010-08-06 13:21:20 -04004323 for (i = 0; i < num_pages; i++, index++) {
Chris Masona6591712011-07-19 12:04:14 -04004324 p = find_or_create_page(mapping, index, GFP_NOFS);
Josef Bacik4804b382012-10-05 16:43:45 -04004325 if (!p)
Chris Mason6af118c2008-07-22 11:18:07 -04004326 goto free_eb;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004327
4328 spin_lock(&mapping->private_lock);
4329 if (PagePrivate(p)) {
4330 /*
4331 * We could have already allocated an eb for this page
4332 * and attached one so lets see if we can get a ref on
4333 * the existing eb, and if we can we know it's good and
4334 * we can just return that one, else we know we can just
4335 * overwrite page->private.
4336 */
4337 exists = (struct extent_buffer *)p->private;
4338 if (atomic_inc_not_zero(&exists->refs)) {
4339 spin_unlock(&mapping->private_lock);
4340 unlock_page(p);
Josef Bacik17de39a2012-05-04 15:16:06 -04004341 page_cache_release(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004342 mark_extent_buffer_accessed(exists);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004343 goto free_eb;
4344 }
4345
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004346 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004347 * Do this so attach doesn't complain and we need to
4348 * drop the ref the old guy had.
4349 */
4350 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004351 WARN_ON(PageDirty(p));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004352 page_cache_release(p);
Chris Masond1310b22008-01-24 16:13:08 -05004353 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004354 attach_extent_buffer_page(eb, p);
4355 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004356 WARN_ON(PageDirty(p));
Chris Masond1310b22008-01-24 16:13:08 -05004357 mark_page_accessed(p);
Chris Mason727011e2010-08-06 13:21:20 -04004358 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05004359 if (!PageUptodate(p))
4360 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05004361
4362 /*
4363 * see below about how we avoid a nasty race with release page
4364 * and why we unlock later
4365 */
Chris Masond1310b22008-01-24 16:13:08 -05004366 }
4367 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004368 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05004369again:
Miao Xie19fe0a82010-10-26 20:57:29 -04004370 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4371 if (ret)
4372 goto free_eb;
4373
Chris Mason6af118c2008-07-22 11:18:07 -04004374 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004375 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
4376 if (ret == -EEXIST) {
4377 exists = radix_tree_lookup(&tree->buffer,
4378 start >> PAGE_CACHE_SHIFT);
Josef Bacik115391d2012-03-09 09:51:43 -05004379 if (!atomic_inc_not_zero(&exists->refs)) {
4380 spin_unlock(&tree->buffer_lock);
4381 radix_tree_preload_end();
Josef Bacik115391d2012-03-09 09:51:43 -05004382 exists = NULL;
4383 goto again;
4384 }
Chris Mason6af118c2008-07-22 11:18:07 -04004385 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004386 radix_tree_preload_end();
Josef Bacik5df42352012-03-15 18:24:42 -04004387 mark_extent_buffer_accessed(exists);
Chris Mason6af118c2008-07-22 11:18:07 -04004388 goto free_eb;
4389 }
Chris Mason6af118c2008-07-22 11:18:07 -04004390 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004391 check_buffer_tree_ref(eb);
Yan, Zhengf044ba72010-02-04 08:46:56 +00004392 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004393 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05004394
4395 /*
4396 * there is a race where release page may have
4397 * tried to find this extent buffer in the radix
4398 * but failed. It will tell the VM it is safe to
4399 * reclaim the, and it will clear the page private bit.
4400 * We must make sure to set the page private bit properly
4401 * after the extent buffer is in the radix tree so
4402 * it doesn't get lost
4403 */
Chris Mason727011e2010-08-06 13:21:20 -04004404 SetPageChecked(eb->pages[0]);
4405 for (i = 1; i < num_pages; i++) {
4406 p = extent_buffer_page(eb, i);
Chris Mason727011e2010-08-06 13:21:20 -04004407 ClearPageChecked(p);
4408 unlock_page(p);
4409 }
4410 unlock_page(eb->pages[0]);
Chris Masond1310b22008-01-24 16:13:08 -05004411 return eb;
4412
Chris Mason6af118c2008-07-22 11:18:07 -04004413free_eb:
Chris Mason727011e2010-08-06 13:21:20 -04004414 for (i = 0; i < num_pages; i++) {
4415 if (eb->pages[i])
4416 unlock_page(eb->pages[i]);
4417 }
Chris Masoneb14ab82011-02-10 12:35:00 -05004418
Josef Bacik17de39a2012-05-04 15:16:06 -04004419 WARN_ON(!atomic_dec_and_test(&eb->refs));
Miao Xie897ca6e2010-10-26 20:57:29 -04004420 btrfs_release_extent_buffer(eb);
Chris Mason6af118c2008-07-22 11:18:07 -04004421 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05004422}
Chris Masond1310b22008-01-24 16:13:08 -05004423
4424struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
David Sterbaf09d1f62011-04-21 01:08:01 +02004425 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004426{
Chris Masond1310b22008-01-24 16:13:08 -05004427 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05004428
Miao Xie19fe0a82010-10-26 20:57:29 -04004429 rcu_read_lock();
4430 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4431 if (eb && atomic_inc_not_zero(&eb->refs)) {
4432 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004433 mark_extent_buffer_accessed(eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04004434 return eb;
4435 }
4436 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04004437
Miao Xie19fe0a82010-10-26 20:57:29 -04004438 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004439}
Chris Masond1310b22008-01-24 16:13:08 -05004440
Josef Bacik3083ee22012-03-09 16:01:49 -05004441static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4442{
4443 struct extent_buffer *eb =
4444 container_of(head, struct extent_buffer, rcu_head);
4445
4446 __free_extent_buffer(eb);
4447}
4448
Josef Bacik3083ee22012-03-09 16:01:49 -05004449/* Expects to have eb->eb_lock already held */
Josef Bacike64860a2012-07-20 16:05:36 -04004450static int release_extent_buffer(struct extent_buffer *eb, gfp_t mask)
Josef Bacik3083ee22012-03-09 16:01:49 -05004451{
4452 WARN_ON(atomic_read(&eb->refs) == 0);
4453 if (atomic_dec_and_test(&eb->refs)) {
Jan Schmidt815a51c2012-05-16 17:00:02 +02004454 if (test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) {
4455 spin_unlock(&eb->refs_lock);
4456 } else {
4457 struct extent_io_tree *tree = eb->tree;
Josef Bacik3083ee22012-03-09 16:01:49 -05004458
Jan Schmidt815a51c2012-05-16 17:00:02 +02004459 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05004460
Jan Schmidt815a51c2012-05-16 17:00:02 +02004461 spin_lock(&tree->buffer_lock);
4462 radix_tree_delete(&tree->buffer,
4463 eb->start >> PAGE_CACHE_SHIFT);
4464 spin_unlock(&tree->buffer_lock);
4465 }
Josef Bacik3083ee22012-03-09 16:01:49 -05004466
4467 /* Should be safe to release our pages at this point */
4468 btrfs_release_extent_buffer_page(eb, 0);
Josef Bacik3083ee22012-03-09 16:01:49 -05004469 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04004470 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05004471 }
4472 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04004473
4474 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05004475}
4476
Chris Masond1310b22008-01-24 16:13:08 -05004477void free_extent_buffer(struct extent_buffer *eb)
4478{
Chris Mason242e18c2013-01-29 17:49:37 -05004479 int refs;
4480 int old;
Chris Masond1310b22008-01-24 16:13:08 -05004481 if (!eb)
4482 return;
4483
Chris Mason242e18c2013-01-29 17:49:37 -05004484 while (1) {
4485 refs = atomic_read(&eb->refs);
4486 if (refs <= 3)
4487 break;
4488 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
4489 if (old == refs)
4490 return;
4491 }
4492
Josef Bacik3083ee22012-03-09 16:01:49 -05004493 spin_lock(&eb->refs_lock);
4494 if (atomic_read(&eb->refs) == 2 &&
Jan Schmidt815a51c2012-05-16 17:00:02 +02004495 test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
4496 atomic_dec(&eb->refs);
4497
4498 if (atomic_read(&eb->refs) == 2 &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004499 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004500 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004501 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4502 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05004503
Josef Bacik3083ee22012-03-09 16:01:49 -05004504 /*
4505 * I know this is terrible, but it's temporary until we stop tracking
4506 * the uptodate bits and such for the extent buffers.
4507 */
4508 release_extent_buffer(eb, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05004509}
Chris Masond1310b22008-01-24 16:13:08 -05004510
Josef Bacik3083ee22012-03-09 16:01:49 -05004511void free_extent_buffer_stale(struct extent_buffer *eb)
4512{
4513 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004514 return;
4515
Josef Bacik3083ee22012-03-09 16:01:49 -05004516 spin_lock(&eb->refs_lock);
4517 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
4518
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004519 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004520 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4521 atomic_dec(&eb->refs);
4522 release_extent_buffer(eb, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05004523}
4524
Chris Mason1d4284b2012-03-28 20:31:37 -04004525void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004526{
Chris Masond1310b22008-01-24 16:13:08 -05004527 unsigned long i;
4528 unsigned long num_pages;
4529 struct page *page;
4530
Chris Masond1310b22008-01-24 16:13:08 -05004531 num_pages = num_extent_pages(eb->start, eb->len);
4532
4533 for (i = 0; i < num_pages; i++) {
4534 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04004535 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05004536 continue;
4537
Chris Masona61e6f22008-07-22 11:18:08 -04004538 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05004539 WARN_ON(!PagePrivate(page));
4540
Chris Masond1310b22008-01-24 16:13:08 -05004541 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004542 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05004543 if (!PageDirty(page)) {
4544 radix_tree_tag_clear(&page->mapping->page_tree,
4545 page_index(page),
4546 PAGECACHE_TAG_DIRTY);
4547 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004548 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04004549 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04004550 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05004551 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004552 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05004553}
Chris Masond1310b22008-01-24 16:13:08 -05004554
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004555int set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004556{
4557 unsigned long i;
4558 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04004559 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004560
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004561 check_buffer_tree_ref(eb);
4562
Chris Masonb9473432009-03-13 11:00:37 -04004563 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004564
Chris Masond1310b22008-01-24 16:13:08 -05004565 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik3083ee22012-03-09 16:01:49 -05004566 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004567 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
4568
Chris Masonb9473432009-03-13 11:00:37 -04004569 for (i = 0; i < num_pages; i++)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004570 set_page_dirty(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04004571 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05004572}
Chris Masond1310b22008-01-24 16:13:08 -05004573
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004574static int range_straddles_pages(u64 start, u64 len)
Chris Mason19b6caf2011-07-25 06:50:50 -04004575{
4576 if (len < PAGE_CACHE_SIZE)
4577 return 1;
4578 if (start & (PAGE_CACHE_SIZE - 1))
4579 return 1;
4580 if ((start + len) & (PAGE_CACHE_SIZE - 1))
4581 return 1;
4582 return 0;
4583}
4584
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004585int clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04004586{
4587 unsigned long i;
4588 struct page *page;
4589 unsigned long num_pages;
4590
Chris Masonb4ce94d2009-02-04 09:25:08 -05004591 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004592 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason1259ab72008-05-12 13:39:03 -04004593 for (i = 0; i < num_pages; i++) {
4594 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04004595 if (page)
4596 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04004597 }
4598 return 0;
4599}
4600
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004601int set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004602{
4603 unsigned long i;
4604 struct page *page;
4605 unsigned long num_pages;
4606
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004607 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004608 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05004609 for (i = 0; i < num_pages; i++) {
4610 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004611 SetPageUptodate(page);
4612 }
4613 return 0;
4614}
Chris Masond1310b22008-01-24 16:13:08 -05004615
Chris Masonce9adaa2008-04-09 16:28:12 -04004616int extent_range_uptodate(struct extent_io_tree *tree,
4617 u64 start, u64 end)
4618{
4619 struct page *page;
4620 int ret;
4621 int pg_uptodate = 1;
4622 int uptodate;
4623 unsigned long index;
4624
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004625 if (range_straddles_pages(start, end - start + 1)) {
Chris Mason19b6caf2011-07-25 06:50:50 -04004626 ret = test_range_bit(tree, start, end,
4627 EXTENT_UPTODATE, 1, NULL);
4628 if (ret)
4629 return 1;
4630 }
Chris Masond3977122009-01-05 21:25:51 -05004631 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004632 index = start >> PAGE_CACHE_SHIFT;
4633 page = find_get_page(tree->mapping, index);
Mitch Harder8bedd512012-01-26 15:01:11 -05004634 if (!page)
4635 return 1;
Chris Masonce9adaa2008-04-09 16:28:12 -04004636 uptodate = PageUptodate(page);
4637 page_cache_release(page);
4638 if (!uptodate) {
4639 pg_uptodate = 0;
4640 break;
4641 }
4642 start += PAGE_CACHE_SIZE;
4643 }
4644 return pg_uptodate;
4645}
4646
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004647int extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004648{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004649 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004650}
Chris Masond1310b22008-01-24 16:13:08 -05004651
4652int read_extent_buffer_pages(struct extent_io_tree *tree,
Arne Jansenbb82ab82011-06-10 14:06:53 +02004653 struct extent_buffer *eb, u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04004654 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05004655{
4656 unsigned long i;
4657 unsigned long start_i;
4658 struct page *page;
4659 int err;
4660 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04004661 int locked_pages = 0;
4662 int all_uptodate = 1;
Chris Masond1310b22008-01-24 16:13:08 -05004663 unsigned long num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04004664 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004665 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004666 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004667
Chris Masonb4ce94d2009-02-04 09:25:08 -05004668 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05004669 return 0;
4670
Chris Masond1310b22008-01-24 16:13:08 -05004671 if (start) {
4672 WARN_ON(start < eb->start);
4673 start_i = (start >> PAGE_CACHE_SHIFT) -
4674 (eb->start >> PAGE_CACHE_SHIFT);
4675 } else {
4676 start_i = 0;
4677 }
4678
4679 num_pages = num_extent_pages(eb->start, eb->len);
4680 for (i = start_i; i < num_pages; i++) {
4681 page = extent_buffer_page(eb, i);
Arne Jansenbb82ab82011-06-10 14:06:53 +02004682 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04004683 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04004684 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05004685 } else {
4686 lock_page(page);
4687 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004688 locked_pages++;
Chris Mason727011e2010-08-06 13:21:20 -04004689 if (!PageUptodate(page)) {
4690 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04004691 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04004692 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004693 }
4694 if (all_uptodate) {
4695 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004696 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04004697 goto unlock_exit;
4698 }
4699
Josef Bacikea466792012-03-26 21:57:36 -04004700 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04004701 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004702 atomic_set(&eb->io_pages, num_reads);
Chris Masonce9adaa2008-04-09 16:28:12 -04004703 for (i = start_i; i < num_pages; i++) {
4704 page = extent_buffer_page(eb, i);
Chris Masonce9adaa2008-04-09 16:28:12 -04004705 if (!PageUptodate(page)) {
Chris Masonf1885912008-04-09 16:28:12 -04004706 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05004707 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04004708 get_extent, &bio,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04004709 mirror_num, &bio_flags,
4710 READ | REQ_META);
Chris Masond3977122009-01-05 21:25:51 -05004711 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05004712 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05004713 } else {
4714 unlock_page(page);
4715 }
4716 }
4717
Jeff Mahoney355808c2011-10-03 23:23:14 -04004718 if (bio) {
Josef Bacikd4c7ca82013-04-19 19:49:09 -04004719 err = submit_one_bio(READ | REQ_META, bio, mirror_num,
4720 bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004721 if (err)
4722 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04004723 }
Chris Masona86c12c2008-02-07 10:50:54 -05004724
Arne Jansenbb82ab82011-06-10 14:06:53 +02004725 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05004726 return ret;
Chris Masond3977122009-01-05 21:25:51 -05004727
Chris Masond1310b22008-01-24 16:13:08 -05004728 for (i = start_i; i < num_pages; i++) {
4729 page = extent_buffer_page(eb, i);
4730 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05004731 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05004732 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05004733 }
Chris Masond3977122009-01-05 21:25:51 -05004734
Chris Masond1310b22008-01-24 16:13:08 -05004735 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04004736
4737unlock_exit:
4738 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05004739 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004740 page = extent_buffer_page(eb, i);
4741 i++;
4742 unlock_page(page);
4743 locked_pages--;
4744 }
4745 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004746}
Chris Masond1310b22008-01-24 16:13:08 -05004747
4748void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4749 unsigned long start,
4750 unsigned long len)
4751{
4752 size_t cur;
4753 size_t offset;
4754 struct page *page;
4755 char *kaddr;
4756 char *dst = (char *)dstv;
4757 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4758 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004759
4760 WARN_ON(start > eb->len);
4761 WARN_ON(start + len > eb->start + eb->len);
4762
4763 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4764
Chris Masond3977122009-01-05 21:25:51 -05004765 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004766 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004767
4768 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04004769 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004770 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004771
4772 dst += cur;
4773 len -= cur;
4774 offset = 0;
4775 i++;
4776 }
4777}
Chris Masond1310b22008-01-24 16:13:08 -05004778
4779int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04004780 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05004781 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04004782 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05004783{
4784 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4785 char *kaddr;
4786 struct page *p;
4787 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4788 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4789 unsigned long end_i = (start_offset + start + min_len - 1) >>
4790 PAGE_CACHE_SHIFT;
4791
4792 if (i != end_i)
4793 return -EINVAL;
4794
4795 if (i == 0) {
4796 offset = start_offset;
4797 *map_start = 0;
4798 } else {
4799 offset = 0;
4800 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4801 }
Chris Masond3977122009-01-05 21:25:51 -05004802
Chris Masond1310b22008-01-24 16:13:08 -05004803 if (start + min_len > eb->len) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004804 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
Chris Masond3977122009-01-05 21:25:51 -05004805 "wanted %lu %lu\n", (unsigned long long)eb->start,
4806 eb->len, start, min_len);
Josef Bacik850265332011-03-15 14:52:12 -04004807 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05004808 }
4809
4810 p = extent_buffer_page(eb, i);
Chris Masona6591712011-07-19 12:04:14 -04004811 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05004812 *map = kaddr + offset;
4813 *map_len = PAGE_CACHE_SIZE - offset;
4814 return 0;
4815}
Chris Masond1310b22008-01-24 16:13:08 -05004816
Chris Masond1310b22008-01-24 16:13:08 -05004817int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4818 unsigned long start,
4819 unsigned long len)
4820{
4821 size_t cur;
4822 size_t offset;
4823 struct page *page;
4824 char *kaddr;
4825 char *ptr = (char *)ptrv;
4826 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4827 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4828 int ret = 0;
4829
4830 WARN_ON(start > eb->len);
4831 WARN_ON(start + len > eb->start + eb->len);
4832
4833 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4834
Chris Masond3977122009-01-05 21:25:51 -05004835 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004836 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004837
4838 cur = min(len, (PAGE_CACHE_SIZE - offset));
4839
Chris Masona6591712011-07-19 12:04:14 -04004840 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004841 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004842 if (ret)
4843 break;
4844
4845 ptr += cur;
4846 len -= cur;
4847 offset = 0;
4848 i++;
4849 }
4850 return ret;
4851}
Chris Masond1310b22008-01-24 16:13:08 -05004852
4853void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4854 unsigned long start, unsigned long len)
4855{
4856 size_t cur;
4857 size_t offset;
4858 struct page *page;
4859 char *kaddr;
4860 char *src = (char *)srcv;
4861 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4862 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4863
4864 WARN_ON(start > eb->len);
4865 WARN_ON(start + len > eb->start + eb->len);
4866
4867 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4868
Chris Masond3977122009-01-05 21:25:51 -05004869 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004870 page = extent_buffer_page(eb, i);
4871 WARN_ON(!PageUptodate(page));
4872
4873 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004874 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004875 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004876
4877 src += cur;
4878 len -= cur;
4879 offset = 0;
4880 i++;
4881 }
4882}
Chris Masond1310b22008-01-24 16:13:08 -05004883
4884void memset_extent_buffer(struct extent_buffer *eb, char c,
4885 unsigned long start, unsigned long len)
4886{
4887 size_t cur;
4888 size_t offset;
4889 struct page *page;
4890 char *kaddr;
4891 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4892 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4893
4894 WARN_ON(start > eb->len);
4895 WARN_ON(start + len > eb->start + eb->len);
4896
4897 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4898
Chris Masond3977122009-01-05 21:25:51 -05004899 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004900 page = extent_buffer_page(eb, i);
4901 WARN_ON(!PageUptodate(page));
4902
4903 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004904 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004905 memset(kaddr + offset, c, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004906
4907 len -= cur;
4908 offset = 0;
4909 i++;
4910 }
4911}
Chris Masond1310b22008-01-24 16:13:08 -05004912
4913void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4914 unsigned long dst_offset, unsigned long src_offset,
4915 unsigned long len)
4916{
4917 u64 dst_len = dst->len;
4918 size_t cur;
4919 size_t offset;
4920 struct page *page;
4921 char *kaddr;
4922 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4923 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4924
4925 WARN_ON(src->len != dst_len);
4926
4927 offset = (start_offset + dst_offset) &
4928 ((unsigned long)PAGE_CACHE_SIZE - 1);
4929
Chris Masond3977122009-01-05 21:25:51 -05004930 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004931 page = extent_buffer_page(dst, i);
4932 WARN_ON(!PageUptodate(page));
4933
4934 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4935
Chris Masona6591712011-07-19 12:04:14 -04004936 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004937 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004938
4939 src_offset += cur;
4940 len -= cur;
4941 offset = 0;
4942 i++;
4943 }
4944}
Chris Masond1310b22008-01-24 16:13:08 -05004945
4946static void move_pages(struct page *dst_page, struct page *src_page,
4947 unsigned long dst_off, unsigned long src_off,
4948 unsigned long len)
4949{
Chris Masona6591712011-07-19 12:04:14 -04004950 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004951 if (dst_page == src_page) {
4952 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4953 } else {
Chris Masona6591712011-07-19 12:04:14 -04004954 char *src_kaddr = page_address(src_page);
Chris Masond1310b22008-01-24 16:13:08 -05004955 char *p = dst_kaddr + dst_off + len;
4956 char *s = src_kaddr + src_off + len;
4957
4958 while (len--)
4959 *--p = *--s;
Chris Masond1310b22008-01-24 16:13:08 -05004960 }
Chris Masond1310b22008-01-24 16:13:08 -05004961}
4962
Sergei Trofimovich33872062011-04-11 21:52:52 +00004963static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4964{
4965 unsigned long distance = (src > dst) ? src - dst : dst - src;
4966 return distance < len;
4967}
4968
Chris Masond1310b22008-01-24 16:13:08 -05004969static void copy_pages(struct page *dst_page, struct page *src_page,
4970 unsigned long dst_off, unsigned long src_off,
4971 unsigned long len)
4972{
Chris Masona6591712011-07-19 12:04:14 -04004973 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004974 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004975 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004976
Sergei Trofimovich33872062011-04-11 21:52:52 +00004977 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04004978 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00004979 } else {
Chris Masond1310b22008-01-24 16:13:08 -05004980 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004981 if (areas_overlap(src_off, dst_off, len))
4982 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00004983 }
Chris Masond1310b22008-01-24 16:13:08 -05004984
Chris Mason727011e2010-08-06 13:21:20 -04004985 if (must_memmove)
4986 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
4987 else
4988 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05004989}
4990
4991void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4992 unsigned long src_offset, unsigned long len)
4993{
4994 size_t cur;
4995 size_t dst_off_in_page;
4996 size_t src_off_in_page;
4997 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4998 unsigned long dst_i;
4999 unsigned long src_i;
5000
5001 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05005002 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
5003 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005004 BUG_ON(1);
5005 }
5006 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05005007 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
5008 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005009 BUG_ON(1);
5010 }
5011
Chris Masond3977122009-01-05 21:25:51 -05005012 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05005013 dst_off_in_page = (start_offset + dst_offset) &
5014 ((unsigned long)PAGE_CACHE_SIZE - 1);
5015 src_off_in_page = (start_offset + src_offset) &
5016 ((unsigned long)PAGE_CACHE_SIZE - 1);
5017
5018 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
5019 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
5020
5021 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
5022 src_off_in_page));
5023 cur = min_t(unsigned long, cur,
5024 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
5025
5026 copy_pages(extent_buffer_page(dst, dst_i),
5027 extent_buffer_page(dst, src_i),
5028 dst_off_in_page, src_off_in_page, cur);
5029
5030 src_offset += cur;
5031 dst_offset += cur;
5032 len -= cur;
5033 }
5034}
Chris Masond1310b22008-01-24 16:13:08 -05005035
5036void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5037 unsigned long src_offset, unsigned long len)
5038{
5039 size_t cur;
5040 size_t dst_off_in_page;
5041 size_t src_off_in_page;
5042 unsigned long dst_end = dst_offset + len - 1;
5043 unsigned long src_end = src_offset + len - 1;
5044 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
5045 unsigned long dst_i;
5046 unsigned long src_i;
5047
5048 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05005049 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
5050 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005051 BUG_ON(1);
5052 }
5053 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05005054 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
5055 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005056 BUG_ON(1);
5057 }
Chris Mason727011e2010-08-06 13:21:20 -04005058 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05005059 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
5060 return;
5061 }
Chris Masond3977122009-01-05 21:25:51 -05005062 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05005063 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
5064 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
5065
5066 dst_off_in_page = (start_offset + dst_end) &
5067 ((unsigned long)PAGE_CACHE_SIZE - 1);
5068 src_off_in_page = (start_offset + src_end) &
5069 ((unsigned long)PAGE_CACHE_SIZE - 1);
5070
5071 cur = min_t(unsigned long, len, src_off_in_page + 1);
5072 cur = min(cur, dst_off_in_page + 1);
5073 move_pages(extent_buffer_page(dst, dst_i),
5074 extent_buffer_page(dst, src_i),
5075 dst_off_in_page - cur + 1,
5076 src_off_in_page - cur + 1, cur);
5077
5078 dst_end -= cur;
5079 src_end -= cur;
5080 len -= cur;
5081 }
5082}
Chris Mason6af118c2008-07-22 11:18:07 -04005083
Josef Bacik3083ee22012-03-09 16:01:49 -05005084int try_release_extent_buffer(struct page *page, gfp_t mask)
Miao Xie19fe0a82010-10-26 20:57:29 -04005085{
Chris Mason6af118c2008-07-22 11:18:07 -04005086 struct extent_buffer *eb;
Miao Xie897ca6e2010-10-26 20:57:29 -04005087
Miao Xie19fe0a82010-10-26 20:57:29 -04005088 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005089 * We need to make sure noboody is attaching this page to an eb right
5090 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04005091 */
Josef Bacik3083ee22012-03-09 16:01:49 -05005092 spin_lock(&page->mapping->private_lock);
5093 if (!PagePrivate(page)) {
5094 spin_unlock(&page->mapping->private_lock);
5095 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005096 }
5097
Josef Bacik3083ee22012-03-09 16:01:49 -05005098 eb = (struct extent_buffer *)page->private;
5099 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04005100
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005101 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005102 * This is a little awful but should be ok, we need to make sure that
5103 * the eb doesn't disappear out from under us while we're looking at
5104 * this page.
5105 */
5106 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005107 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05005108 spin_unlock(&eb->refs_lock);
5109 spin_unlock(&page->mapping->private_lock);
5110 return 0;
5111 }
5112 spin_unlock(&page->mapping->private_lock);
5113
5114 if ((mask & GFP_NOFS) == GFP_NOFS)
5115 mask = GFP_NOFS;
5116
5117 /*
5118 * If tree ref isn't set then we know the ref on this eb is a real ref,
5119 * so just return, this page will likely be freed soon anyway.
5120 */
5121 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5122 spin_unlock(&eb->refs_lock);
5123 return 0;
5124 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005125
Josef Bacike64860a2012-07-20 16:05:36 -04005126 return release_extent_buffer(eb, mask);
Chris Mason6af118c2008-07-22 11:18:07 -04005127}