blob: 03ca3ab958981475909780a46537c9f6d2b1c532 [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;
Chris Mason9be33952013-05-17 18:30:14 -040026static struct bio_set *btrfs_bioset;
Chris Masond1310b22008-01-24 16:13:08 -050027
Eric Sandeen6d49ba12013-04-22 16:12:31 +000028#ifdef CONFIG_BTRFS_DEBUG
Chris Masond1310b22008-01-24 16:13:08 -050029static LIST_HEAD(buffers);
30static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040031
Chris Masond3977122009-01-05 21:25:51 -050032static DEFINE_SPINLOCK(leak_lock);
Eric Sandeen6d49ba12013-04-22 16:12:31 +000033
34static inline
35void btrfs_leak_debug_add(struct list_head *new, struct list_head *head)
36{
37 unsigned long flags;
38
39 spin_lock_irqsave(&leak_lock, flags);
40 list_add(new, head);
41 spin_unlock_irqrestore(&leak_lock, flags);
42}
43
44static inline
45void btrfs_leak_debug_del(struct list_head *entry)
46{
47 unsigned long flags;
48
49 spin_lock_irqsave(&leak_lock, flags);
50 list_del(entry);
51 spin_unlock_irqrestore(&leak_lock, flags);
52}
53
54static inline
55void btrfs_leak_debug_check(void)
56{
57 struct extent_state *state;
58 struct extent_buffer *eb;
59
60 while (!list_empty(&states)) {
61 state = list_entry(states.next, struct extent_state, leak_list);
62 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
63 "state %lu in tree %p refs %d\n",
64 (unsigned long long)state->start,
65 (unsigned long long)state->end,
66 state->state, state->tree, atomic_read(&state->refs));
67 list_del(&state->leak_list);
68 kmem_cache_free(extent_state_cache, state);
69 }
70
71 while (!list_empty(&buffers)) {
72 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
73 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
74 "refs %d\n", (unsigned long long)eb->start,
75 eb->len, atomic_read(&eb->refs));
76 list_del(&eb->leak_list);
77 kmem_cache_free(extent_buffer_cache, eb);
78 }
79}
David Sterba8d599ae2013-04-30 15:22:23 +000080
81#define btrfs_debug_check_extent_io_range(inode, start, end) \
82 __btrfs_debug_check_extent_io_range(__func__, (inode), (start), (end))
83static inline void __btrfs_debug_check_extent_io_range(const char *caller,
84 struct inode *inode, u64 start, u64 end)
85{
86 u64 isize = i_size_read(inode);
87
88 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
89 printk_ratelimited(KERN_DEBUG
90 "btrfs: %s: ino %llu isize %llu odd range [%llu,%llu]\n",
91 caller,
92 (unsigned long long)btrfs_ino(inode),
93 (unsigned long long)isize,
94 (unsigned long long)start,
95 (unsigned long long)end);
96 }
97}
Eric Sandeen6d49ba12013-04-22 16:12:31 +000098#else
99#define btrfs_leak_debug_add(new, head) do {} while (0)
100#define btrfs_leak_debug_del(entry) do {} while (0)
101#define btrfs_leak_debug_check() do {} while (0)
David Sterba8d599ae2013-04-30 15:22:23 +0000102#define btrfs_debug_check_extent_io_range(c, s, e) do {} while (0)
Chris Mason4bef0842008-09-08 11:18:08 -0400103#endif
Chris Masond1310b22008-01-24 16:13:08 -0500104
Chris Masond1310b22008-01-24 16:13:08 -0500105#define BUFFER_LRU_MAX 64
106
107struct tree_entry {
108 u64 start;
109 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -0500110 struct rb_node rb_node;
111};
112
113struct extent_page_data {
114 struct bio *bio;
115 struct extent_io_tree *tree;
116 get_extent_t *get_extent;
Josef Bacikde0022b2012-09-25 14:25:58 -0400117 unsigned long bio_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500118
119 /* tells writepage not to lock the state bits for this range
120 * it still does the unlocking
121 */
Chris Masonffbd5172009-04-20 15:50:09 -0400122 unsigned int extent_locked:1;
123
124 /* tells the submit_bio code to use a WRITE_SYNC */
125 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -0500126};
127
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400128static noinline void flush_write_bio(void *data);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400129static inline struct btrfs_fs_info *
130tree_fs_info(struct extent_io_tree *tree)
131{
132 return btrfs_sb(tree->mapping->host->i_sb);
133}
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400134
Chris Masond1310b22008-01-24 16:13:08 -0500135int __init extent_io_init(void)
136{
David Sterba837e1972012-09-07 03:00:48 -0600137 extent_state_cache = kmem_cache_create("btrfs_extent_state",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200138 sizeof(struct extent_state), 0,
139 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500140 if (!extent_state_cache)
141 return -ENOMEM;
142
David Sterba837e1972012-09-07 03:00:48 -0600143 extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200144 sizeof(struct extent_buffer), 0,
145 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500146 if (!extent_buffer_cache)
147 goto free_state_cache;
Chris Mason9be33952013-05-17 18:30:14 -0400148
149 btrfs_bioset = bioset_create(BIO_POOL_SIZE,
150 offsetof(struct btrfs_io_bio, bio));
151 if (!btrfs_bioset)
152 goto free_buffer_cache;
Chris Masond1310b22008-01-24 16:13:08 -0500153 return 0;
154
Chris Mason9be33952013-05-17 18:30:14 -0400155free_buffer_cache:
156 kmem_cache_destroy(extent_buffer_cache);
157 extent_buffer_cache = NULL;
158
Chris Masond1310b22008-01-24 16:13:08 -0500159free_state_cache:
160 kmem_cache_destroy(extent_state_cache);
Chris Mason9be33952013-05-17 18:30:14 -0400161 extent_state_cache = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500162 return -ENOMEM;
163}
164
165void extent_io_exit(void)
166{
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000167 btrfs_leak_debug_check();
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000168
169 /*
170 * Make sure all delayed rcu free are flushed before we
171 * destroy caches.
172 */
173 rcu_barrier();
Chris Masond1310b22008-01-24 16:13:08 -0500174 if (extent_state_cache)
175 kmem_cache_destroy(extent_state_cache);
176 if (extent_buffer_cache)
177 kmem_cache_destroy(extent_buffer_cache);
Chris Mason9be33952013-05-17 18:30:14 -0400178 if (btrfs_bioset)
179 bioset_free(btrfs_bioset);
Chris Masond1310b22008-01-24 16:13:08 -0500180}
181
182void extent_io_tree_init(struct extent_io_tree *tree,
David Sterbaf993c882011-04-20 23:35:57 +0200183 struct address_space *mapping)
Chris Masond1310b22008-01-24 16:13:08 -0500184{
Eric Paris6bef4d32010-02-23 19:43:04 +0000185 tree->state = RB_ROOT;
Miao Xie19fe0a82010-10-26 20:57:29 -0400186 INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500187 tree->ops = NULL;
188 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500189 spin_lock_init(&tree->lock);
Chris Mason6af118ce2008-07-22 11:18:07 -0400190 spin_lock_init(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -0500191 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500192}
Chris Masond1310b22008-01-24 16:13:08 -0500193
Christoph Hellwigb2950862008-12-02 09:54:17 -0500194static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500195{
196 struct extent_state *state;
Chris Masond1310b22008-01-24 16:13:08 -0500197
198 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400199 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500200 return state;
201 state->state = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500202 state->private = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500203 state->tree = NULL;
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000204 btrfs_leak_debug_add(&state->leak_list, &states);
Chris Masond1310b22008-01-24 16:13:08 -0500205 atomic_set(&state->refs, 1);
206 init_waitqueue_head(&state->wq);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100207 trace_alloc_extent_state(state, mask, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500208 return state;
209}
Chris Masond1310b22008-01-24 16:13:08 -0500210
Chris Mason4845e442010-05-25 20:56:50 -0400211void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500212{
Chris Masond1310b22008-01-24 16:13:08 -0500213 if (!state)
214 return;
215 if (atomic_dec_and_test(&state->refs)) {
Chris Mason70dec802008-01-29 09:59:12 -0500216 WARN_ON(state->tree);
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000217 btrfs_leak_debug_del(&state->leak_list);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100218 trace_free_extent_state(state, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500219 kmem_cache_free(extent_state_cache, state);
220 }
221}
Chris Masond1310b22008-01-24 16:13:08 -0500222
223static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
224 struct rb_node *node)
225{
Chris Masond3977122009-01-05 21:25:51 -0500226 struct rb_node **p = &root->rb_node;
227 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500228 struct tree_entry *entry;
229
Chris Masond3977122009-01-05 21:25:51 -0500230 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500231 parent = *p;
232 entry = rb_entry(parent, struct tree_entry, rb_node);
233
234 if (offset < entry->start)
235 p = &(*p)->rb_left;
236 else if (offset > entry->end)
237 p = &(*p)->rb_right;
238 else
239 return parent;
240 }
241
Chris Masond1310b22008-01-24 16:13:08 -0500242 rb_link_node(node, parent, p);
243 rb_insert_color(node, root);
244 return NULL;
245}
246
Chris Mason80ea96b2008-02-01 14:51:59 -0500247static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Chris Masond1310b22008-01-24 16:13:08 -0500248 struct rb_node **prev_ret,
249 struct rb_node **next_ret)
250{
Chris Mason80ea96b2008-02-01 14:51:59 -0500251 struct rb_root *root = &tree->state;
Chris Masond3977122009-01-05 21:25:51 -0500252 struct rb_node *n = root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500253 struct rb_node *prev = NULL;
254 struct rb_node *orig_prev = NULL;
255 struct tree_entry *entry;
256 struct tree_entry *prev_entry = NULL;
257
Chris Masond3977122009-01-05 21:25:51 -0500258 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500259 entry = rb_entry(n, struct tree_entry, rb_node);
260 prev = n;
261 prev_entry = entry;
262
263 if (offset < entry->start)
264 n = n->rb_left;
265 else if (offset > entry->end)
266 n = n->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500267 else
Chris Masond1310b22008-01-24 16:13:08 -0500268 return n;
269 }
270
271 if (prev_ret) {
272 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500273 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500274 prev = rb_next(prev);
275 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
276 }
277 *prev_ret = prev;
278 prev = orig_prev;
279 }
280
281 if (next_ret) {
282 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500283 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500284 prev = rb_prev(prev);
285 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
286 }
287 *next_ret = prev;
288 }
289 return NULL;
290}
291
Chris Mason80ea96b2008-02-01 14:51:59 -0500292static inline struct rb_node *tree_search(struct extent_io_tree *tree,
293 u64 offset)
Chris Masond1310b22008-01-24 16:13:08 -0500294{
Chris Mason70dec802008-01-29 09:59:12 -0500295 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500296 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500297
Chris Mason80ea96b2008-02-01 14:51:59 -0500298 ret = __etree_search(tree, offset, &prev, NULL);
Chris Masond3977122009-01-05 21:25:51 -0500299 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500300 return prev;
301 return ret;
302}
303
Josef Bacik9ed74f22009-09-11 16:12:44 -0400304static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
305 struct extent_state *other)
306{
307 if (tree->ops && tree->ops->merge_extent_hook)
308 tree->ops->merge_extent_hook(tree->mapping->host, new,
309 other);
310}
311
Chris Masond1310b22008-01-24 16:13:08 -0500312/*
313 * utility function to look for merge candidates inside a given range.
314 * Any extents with matching state are merged together into a single
315 * extent in the tree. Extents with EXTENT_IO in their state field
316 * are not merged because the end_io handlers need to be able to do
317 * operations on them without sleeping (or doing allocations/splits).
318 *
319 * This should be called with the tree lock held.
320 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000321static void merge_state(struct extent_io_tree *tree,
322 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500323{
324 struct extent_state *other;
325 struct rb_node *other_node;
326
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400327 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000328 return;
Chris Masond1310b22008-01-24 16:13:08 -0500329
330 other_node = rb_prev(&state->rb_node);
331 if (other_node) {
332 other = rb_entry(other_node, struct extent_state, rb_node);
333 if (other->end == state->start - 1 &&
334 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400335 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500336 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500337 other->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500338 rb_erase(&other->rb_node, &tree->state);
339 free_extent_state(other);
340 }
341 }
342 other_node = rb_next(&state->rb_node);
343 if (other_node) {
344 other = rb_entry(other_node, struct extent_state, rb_node);
345 if (other->start == state->end + 1 &&
346 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400347 merge_cb(tree, state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400348 state->end = other->end;
349 other->tree = NULL;
350 rb_erase(&other->rb_node, &tree->state);
351 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500352 }
353 }
Chris Masond1310b22008-01-24 16:13:08 -0500354}
355
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000356static void set_state_cb(struct extent_io_tree *tree,
David Sterba41074882013-04-29 13:38:46 +0000357 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500358{
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000359 if (tree->ops && tree->ops->set_bit_hook)
360 tree->ops->set_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500361}
362
363static void clear_state_cb(struct extent_io_tree *tree,
David Sterba41074882013-04-29 13:38:46 +0000364 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500365{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400366 if (tree->ops && tree->ops->clear_bit_hook)
367 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500368}
369
Xiao Guangrong3150b692011-07-14 03:19:08 +0000370static void set_state_bits(struct extent_io_tree *tree,
David Sterba41074882013-04-29 13:38:46 +0000371 struct extent_state *state, unsigned long *bits);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000372
Chris Masond1310b22008-01-24 16:13:08 -0500373/*
374 * insert an extent_state struct into the tree. 'bits' are set on the
375 * struct before it is inserted.
376 *
377 * This may return -EEXIST if the extent is already there, in which case the
378 * state struct is freed.
379 *
380 * The tree lock is not taken internally. This is a utility function and
381 * probably isn't what you want to call (see set/clear_extent_bit).
382 */
383static int insert_state(struct extent_io_tree *tree,
384 struct extent_state *state, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +0000385 unsigned long *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500386{
387 struct rb_node *node;
388
Julia Lawall31b1a2b2012-11-03 10:58:34 +0000389 if (end < start)
390 WARN(1, KERN_ERR "btrfs end < start %llu %llu\n",
Chris Masond3977122009-01-05 21:25:51 -0500391 (unsigned long long)end,
392 (unsigned long long)start);
Chris Masond1310b22008-01-24 16:13:08 -0500393 state->start = start;
394 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400395
Xiao Guangrong3150b692011-07-14 03:19:08 +0000396 set_state_bits(tree, state, bits);
397
Chris Masond1310b22008-01-24 16:13:08 -0500398 node = tree_insert(&tree->state, end, &state->rb_node);
399 if (node) {
400 struct extent_state *found;
401 found = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500402 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
403 "%llu %llu\n", (unsigned long long)found->start,
404 (unsigned long long)found->end,
405 (unsigned long long)start, (unsigned long long)end);
Chris Masond1310b22008-01-24 16:13:08 -0500406 return -EEXIST;
407 }
Chris Mason70dec802008-01-29 09:59:12 -0500408 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500409 merge_state(tree, state);
410 return 0;
411}
412
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000413static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
Josef Bacik9ed74f22009-09-11 16:12:44 -0400414 u64 split)
415{
416 if (tree->ops && tree->ops->split_extent_hook)
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000417 tree->ops->split_extent_hook(tree->mapping->host, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400418}
419
Chris Masond1310b22008-01-24 16:13:08 -0500420/*
421 * split a given extent state struct in two, inserting the preallocated
422 * struct 'prealloc' as the newly created second half. 'split' indicates an
423 * offset inside 'orig' where it should be split.
424 *
425 * Before calling,
426 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
427 * are two extent state structs in the tree:
428 * prealloc: [orig->start, split - 1]
429 * orig: [ split, orig->end ]
430 *
431 * The tree locks are not taken by this function. They need to be held
432 * by the caller.
433 */
434static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
435 struct extent_state *prealloc, u64 split)
436{
437 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400438
439 split_cb(tree, orig, split);
440
Chris Masond1310b22008-01-24 16:13:08 -0500441 prealloc->start = orig->start;
442 prealloc->end = split - 1;
443 prealloc->state = orig->state;
444 orig->start = split;
445
446 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
447 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500448 free_extent_state(prealloc);
449 return -EEXIST;
450 }
Chris Mason70dec802008-01-29 09:59:12 -0500451 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500452 return 0;
453}
454
Li Zefancdc6a392012-03-12 16:39:48 +0800455static struct extent_state *next_state(struct extent_state *state)
456{
457 struct rb_node *next = rb_next(&state->rb_node);
458 if (next)
459 return rb_entry(next, struct extent_state, rb_node);
460 else
461 return NULL;
462}
463
Chris Masond1310b22008-01-24 16:13:08 -0500464/*
465 * utility function to clear some bits in an extent state struct.
Wang Sheng-Hui1b303fc2012-04-06 14:35:18 +0800466 * it will optionally wake up any one waiting on this state (wake == 1).
Chris Masond1310b22008-01-24 16:13:08 -0500467 *
468 * If no bits are set on the state struct after clearing things, the
469 * struct is freed and removed from the tree
470 */
Li Zefancdc6a392012-03-12 16:39:48 +0800471static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
472 struct extent_state *state,
David Sterba41074882013-04-29 13:38:46 +0000473 unsigned long *bits, int wake)
Chris Masond1310b22008-01-24 16:13:08 -0500474{
Li Zefancdc6a392012-03-12 16:39:48 +0800475 struct extent_state *next;
David Sterba41074882013-04-29 13:38:46 +0000476 unsigned long bits_to_clear = *bits & ~EXTENT_CTLBITS;
Chris Masond1310b22008-01-24 16:13:08 -0500477
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400478 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500479 u64 range = state->end - state->start + 1;
480 WARN_ON(range > tree->dirty_bytes);
481 tree->dirty_bytes -= range;
482 }
Chris Mason291d6732008-01-29 15:55:23 -0500483 clear_state_cb(tree, state, bits);
Josef Bacik32c00af2009-10-08 13:34:05 -0400484 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500485 if (wake)
486 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400487 if (state->state == 0) {
Li Zefancdc6a392012-03-12 16:39:48 +0800488 next = next_state(state);
Chris Mason70dec802008-01-29 09:59:12 -0500489 if (state->tree) {
Chris Masond1310b22008-01-24 16:13:08 -0500490 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500491 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500492 free_extent_state(state);
493 } else {
494 WARN_ON(1);
495 }
496 } else {
497 merge_state(tree, state);
Li Zefancdc6a392012-03-12 16:39:48 +0800498 next = next_state(state);
Chris Masond1310b22008-01-24 16:13:08 -0500499 }
Li Zefancdc6a392012-03-12 16:39:48 +0800500 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500501}
502
Xiao Guangrong82337672011-04-20 06:44:57 +0000503static struct extent_state *
504alloc_extent_state_atomic(struct extent_state *prealloc)
505{
506 if (!prealloc)
507 prealloc = alloc_extent_state(GFP_ATOMIC);
508
509 return prealloc;
510}
511
Eric Sandeen48a3b632013-04-25 20:41:01 +0000512static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400513{
514 btrfs_panic(tree_fs_info(tree), err, "Locking error: "
515 "Extent tree was modified by another "
516 "thread while locked.");
517}
518
Chris Masond1310b22008-01-24 16:13:08 -0500519/*
520 * clear some bits on a range in the tree. This may require splitting
521 * or inserting elements in the tree, so the gfp mask is used to
522 * indicate which allocations or sleeping are allowed.
523 *
524 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
525 * the given range from the tree regardless of state (ie for truncate).
526 *
527 * the range [start, end] is inclusive.
528 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100529 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500530 */
531int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +0000532 unsigned long bits, int wake, int delete,
Chris Mason2c64c532009-09-02 15:04:12 -0400533 struct extent_state **cached_state,
534 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500535{
536 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400537 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500538 struct extent_state *prealloc = NULL;
539 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400540 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500541 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000542 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500543
David Sterba8d599ae2013-04-30 15:22:23 +0000544 btrfs_debug_check_extent_io_range(tree->mapping->host, start, end);
545
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400546 if (delete)
547 bits |= ~EXTENT_CTLBITS;
548 bits |= EXTENT_FIRST_DELALLOC;
549
Josef Bacik2ac55d42010-02-03 19:33:23 +0000550 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
551 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500552again:
553 if (!prealloc && (mask & __GFP_WAIT)) {
554 prealloc = alloc_extent_state(mask);
555 if (!prealloc)
556 return -ENOMEM;
557 }
558
Chris Masoncad321a2008-12-17 14:51:42 -0500559 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400560 if (cached_state) {
561 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000562
563 if (clear) {
564 *cached_state = NULL;
565 cached_state = NULL;
566 }
567
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400568 if (cached && cached->tree && cached->start <= start &&
569 cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000570 if (clear)
571 atomic_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400572 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400573 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400574 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000575 if (clear)
576 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400577 }
Chris Masond1310b22008-01-24 16:13:08 -0500578 /*
579 * this search will find the extents that end after
580 * our range starts
581 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500582 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500583 if (!node)
584 goto out;
585 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400586hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500587 if (state->start > end)
588 goto out;
589 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400590 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500591
Liu Bo04493142012-02-16 18:34:37 +0800592 /* the state doesn't have the wanted bits, go ahead */
Li Zefancdc6a392012-03-12 16:39:48 +0800593 if (!(state->state & bits)) {
594 state = next_state(state);
Liu Bo04493142012-02-16 18:34:37 +0800595 goto next;
Li Zefancdc6a392012-03-12 16:39:48 +0800596 }
Liu Bo04493142012-02-16 18:34:37 +0800597
Chris Masond1310b22008-01-24 16:13:08 -0500598 /*
599 * | ---- desired range ---- |
600 * | state | or
601 * | ------------- state -------------- |
602 *
603 * We need to split the extent we found, and may flip
604 * bits on second half.
605 *
606 * If the extent we found extends past our range, we
607 * just split and search again. It'll get split again
608 * the next time though.
609 *
610 * If the extent we found is inside our range, we clear
611 * the desired bit on it.
612 */
613
614 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000615 prealloc = alloc_extent_state_atomic(prealloc);
616 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500617 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400618 if (err)
619 extent_io_tree_panic(tree, err);
620
Chris Masond1310b22008-01-24 16:13:08 -0500621 prealloc = NULL;
622 if (err)
623 goto out;
624 if (state->end <= end) {
Liu Bod1ac6e42012-05-10 18:10:39 +0800625 state = clear_state_bit(tree, state, &bits, wake);
626 goto next;
Chris Masond1310b22008-01-24 16:13:08 -0500627 }
628 goto search_again;
629 }
630 /*
631 * | ---- desired range ---- |
632 * | state |
633 * We need to split the extent, and clear the bit
634 * on the first half
635 */
636 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000637 prealloc = alloc_extent_state_atomic(prealloc);
638 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500639 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400640 if (err)
641 extent_io_tree_panic(tree, err);
642
Chris Masond1310b22008-01-24 16:13:08 -0500643 if (wake)
644 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400645
Jeff Mahoney6763af82012-03-01 14:56:29 +0100646 clear_state_bit(tree, prealloc, &bits, wake);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400647
Chris Masond1310b22008-01-24 16:13:08 -0500648 prealloc = NULL;
649 goto out;
650 }
Chris Mason42daec22009-09-23 19:51:09 -0400651
Li Zefancdc6a392012-03-12 16:39:48 +0800652 state = clear_state_bit(tree, state, &bits, wake);
Liu Bo04493142012-02-16 18:34:37 +0800653next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400654 if (last_end == (u64)-1)
655 goto out;
656 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800657 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800658 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500659 goto search_again;
660
661out:
Chris Masoncad321a2008-12-17 14:51:42 -0500662 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500663 if (prealloc)
664 free_extent_state(prealloc);
665
Jeff Mahoney6763af82012-03-01 14:56:29 +0100666 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500667
668search_again:
669 if (start > end)
670 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500671 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500672 if (mask & __GFP_WAIT)
673 cond_resched();
674 goto again;
675}
Chris Masond1310b22008-01-24 16:13:08 -0500676
Jeff Mahoney143bede2012-03-01 14:56:26 +0100677static void wait_on_state(struct extent_io_tree *tree,
678 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500679 __releases(tree->lock)
680 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500681{
682 DEFINE_WAIT(wait);
683 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500684 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500685 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500686 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500687 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500688}
689
690/*
691 * waits for one or more bits to clear on a range in the state tree.
692 * The range [start, end] is inclusive.
693 * The tree lock is taken by this function
694 */
David Sterba41074882013-04-29 13:38:46 +0000695static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
696 unsigned long bits)
Chris Masond1310b22008-01-24 16:13:08 -0500697{
698 struct extent_state *state;
699 struct rb_node *node;
700
David Sterba8d599ae2013-04-30 15:22:23 +0000701 btrfs_debug_check_extent_io_range(tree->mapping->host, start, end);
702
Chris Masoncad321a2008-12-17 14:51:42 -0500703 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500704again:
705 while (1) {
706 /*
707 * this search will find all the extents that end after
708 * our range starts
709 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500710 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500711 if (!node)
712 break;
713
714 state = rb_entry(node, struct extent_state, rb_node);
715
716 if (state->start > end)
717 goto out;
718
719 if (state->state & bits) {
720 start = state->start;
721 atomic_inc(&state->refs);
722 wait_on_state(tree, state);
723 free_extent_state(state);
724 goto again;
725 }
726 start = state->end + 1;
727
728 if (start > end)
729 break;
730
Xiao Guangrongded91f02011-07-14 03:19:27 +0000731 cond_resched_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500732 }
733out:
Chris Masoncad321a2008-12-17 14:51:42 -0500734 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500735}
Chris Masond1310b22008-01-24 16:13:08 -0500736
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000737static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500738 struct extent_state *state,
David Sterba41074882013-04-29 13:38:46 +0000739 unsigned long *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500740{
David Sterba41074882013-04-29 13:38:46 +0000741 unsigned long bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400742
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000743 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400744 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500745 u64 range = state->end - state->start + 1;
746 tree->dirty_bytes += range;
747 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400748 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500749}
750
Chris Mason2c64c532009-09-02 15:04:12 -0400751static void cache_state(struct extent_state *state,
752 struct extent_state **cached_ptr)
753{
754 if (cached_ptr && !(*cached_ptr)) {
755 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
756 *cached_ptr = state;
757 atomic_inc(&state->refs);
758 }
759 }
760}
761
Arne Jansen507903b2011-04-06 10:02:20 +0000762static void uncache_state(struct extent_state **cached_ptr)
763{
764 if (cached_ptr && (*cached_ptr)) {
765 struct extent_state *state = *cached_ptr;
Chris Mason109b36a2011-04-12 13:57:39 -0400766 *cached_ptr = NULL;
767 free_extent_state(state);
Arne Jansen507903b2011-04-06 10:02:20 +0000768 }
769}
770
Chris Masond1310b22008-01-24 16:13:08 -0500771/*
Chris Mason1edbb732009-09-02 13:24:36 -0400772 * set some bits on a range in the tree. This may require allocations or
773 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500774 *
Chris Mason1edbb732009-09-02 13:24:36 -0400775 * If any of the exclusive bits are set, this will fail with -EEXIST if some
776 * part of the range already has the desired bits set. The start of the
777 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500778 *
Chris Mason1edbb732009-09-02 13:24:36 -0400779 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500780 */
Chris Mason1edbb732009-09-02 13:24:36 -0400781
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100782static int __must_check
783__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +0000784 unsigned long bits, unsigned long exclusive_bits,
785 u64 *failed_start, struct extent_state **cached_state,
786 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500787{
788 struct extent_state *state;
789 struct extent_state *prealloc = NULL;
790 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500791 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500792 u64 last_start;
793 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400794
David Sterba8d599ae2013-04-30 15:22:23 +0000795 btrfs_debug_check_extent_io_range(tree->mapping->host, start, end);
796
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400797 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500798again:
799 if (!prealloc && (mask & __GFP_WAIT)) {
800 prealloc = alloc_extent_state(mask);
Xiao Guangrong82337672011-04-20 06:44:57 +0000801 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500802 }
803
Chris Masoncad321a2008-12-17 14:51:42 -0500804 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400805 if (cached_state && *cached_state) {
806 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400807 if (state->start <= start && state->end > start &&
808 state->tree) {
Chris Mason9655d292009-09-02 15:22:30 -0400809 node = &state->rb_node;
810 goto hit_next;
811 }
812 }
Chris Masond1310b22008-01-24 16:13:08 -0500813 /*
814 * this search will find all the extents that end after
815 * our range starts.
816 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500817 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500818 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000819 prealloc = alloc_extent_state_atomic(prealloc);
820 BUG_ON(!prealloc);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400821 err = insert_state(tree, prealloc, start, end, &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400822 if (err)
823 extent_io_tree_panic(tree, err);
824
Chris Masond1310b22008-01-24 16:13:08 -0500825 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500826 goto out;
827 }
Chris Masond1310b22008-01-24 16:13:08 -0500828 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400829hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500830 last_start = state->start;
831 last_end = state->end;
832
833 /*
834 * | ---- desired range ---- |
835 * | state |
836 *
837 * Just lock what we found and keep going
838 */
839 if (state->start == start && state->end <= end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400840 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500841 *failed_start = state->start;
842 err = -EEXIST;
843 goto out;
844 }
Chris Mason42daec22009-09-23 19:51:09 -0400845
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000846 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400847 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500848 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400849 if (last_end == (u64)-1)
850 goto out;
851 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800852 state = next_state(state);
853 if (start < end && state && state->start == start &&
854 !need_resched())
855 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500856 goto search_again;
857 }
858
859 /*
860 * | ---- desired range ---- |
861 * | state |
862 * or
863 * | ------------- state -------------- |
864 *
865 * We need to split the extent we found, and may flip bits on
866 * second half.
867 *
868 * If the extent we found extends past our
869 * range, we just split and search again. It'll get split
870 * again the next time though.
871 *
872 * If the extent we found is inside our range, we set the
873 * desired bit on it.
874 */
875 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400876 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500877 *failed_start = start;
878 err = -EEXIST;
879 goto out;
880 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000881
882 prealloc = alloc_extent_state_atomic(prealloc);
883 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500884 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400885 if (err)
886 extent_io_tree_panic(tree, err);
887
Chris Masond1310b22008-01-24 16:13:08 -0500888 prealloc = NULL;
889 if (err)
890 goto out;
891 if (state->end <= end) {
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000892 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400893 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500894 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400895 if (last_end == (u64)-1)
896 goto out;
897 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800898 state = next_state(state);
899 if (start < end && state && state->start == start &&
900 !need_resched())
901 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500902 }
903 goto search_again;
904 }
905 /*
906 * | ---- desired range ---- |
907 * | state | or | state |
908 *
909 * There's a hole, we need to insert something in it and
910 * ignore the extent we found.
911 */
912 if (state->start > start) {
913 u64 this_end;
914 if (end < last_start)
915 this_end = end;
916 else
Chris Masond3977122009-01-05 21:25:51 -0500917 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000918
919 prealloc = alloc_extent_state_atomic(prealloc);
920 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000921
922 /*
923 * Avoid to free 'prealloc' if it can be merged with
924 * the later extent.
925 */
Chris Masond1310b22008-01-24 16:13:08 -0500926 err = insert_state(tree, prealloc, start, this_end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400927 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400928 if (err)
929 extent_io_tree_panic(tree, err);
930
Chris Mason2c64c532009-09-02 15:04:12 -0400931 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500932 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500933 start = this_end + 1;
934 goto search_again;
935 }
936 /*
937 * | ---- desired range ---- |
938 * | state |
939 * We need to split the extent, and set the bit
940 * on the first half
941 */
942 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400943 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500944 *failed_start = start;
945 err = -EEXIST;
946 goto out;
947 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000948
949 prealloc = alloc_extent_state_atomic(prealloc);
950 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500951 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400952 if (err)
953 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -0500954
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000955 set_state_bits(tree, prealloc, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400956 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500957 merge_state(tree, prealloc);
958 prealloc = NULL;
959 goto out;
960 }
961
962 goto search_again;
963
964out:
Chris Masoncad321a2008-12-17 14:51:42 -0500965 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500966 if (prealloc)
967 free_extent_state(prealloc);
968
969 return err;
970
971search_again:
972 if (start > end)
973 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500974 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500975 if (mask & __GFP_WAIT)
976 cond_resched();
977 goto again;
978}
Chris Masond1310b22008-01-24 16:13:08 -0500979
David Sterba41074882013-04-29 13:38:46 +0000980int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
981 unsigned long bits, u64 * failed_start,
982 struct extent_state **cached_state, gfp_t mask)
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100983{
984 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
985 cached_state, mask);
986}
987
988
Josef Bacik462d6fa2011-09-26 13:56:12 -0400989/**
Liu Bo10983f22012-07-11 15:26:19 +0800990 * convert_extent_bit - convert all bits in a given range from one bit to
991 * another
Josef Bacik462d6fa2011-09-26 13:56:12 -0400992 * @tree: the io tree to search
993 * @start: the start offset in bytes
994 * @end: the end offset in bytes (inclusive)
995 * @bits: the bits to set in this range
996 * @clear_bits: the bits to clear in this range
Josef Bacike6138872012-09-27 17:07:30 -0400997 * @cached_state: state that we're going to cache
Josef Bacik462d6fa2011-09-26 13:56:12 -0400998 * @mask: the allocation mask
999 *
1000 * This will go through and set bits for the given range. If any states exist
1001 * already in this range they are set with the given bit and cleared of the
1002 * clear_bits. This is only meant to be used by things that are mergeable, ie
1003 * converting from say DELALLOC to DIRTY. This is not meant to be used with
1004 * boundary bits like LOCK.
1005 */
1006int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +00001007 unsigned long bits, unsigned long clear_bits,
Josef Bacike6138872012-09-27 17:07:30 -04001008 struct extent_state **cached_state, gfp_t mask)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001009{
1010 struct extent_state *state;
1011 struct extent_state *prealloc = NULL;
1012 struct rb_node *node;
1013 int err = 0;
1014 u64 last_start;
1015 u64 last_end;
1016
David Sterba8d599ae2013-04-30 15:22:23 +00001017 btrfs_debug_check_extent_io_range(tree->mapping->host, start, end);
1018
Josef Bacik462d6fa2011-09-26 13:56:12 -04001019again:
1020 if (!prealloc && (mask & __GFP_WAIT)) {
1021 prealloc = alloc_extent_state(mask);
1022 if (!prealloc)
1023 return -ENOMEM;
1024 }
1025
1026 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001027 if (cached_state && *cached_state) {
1028 state = *cached_state;
1029 if (state->start <= start && state->end > start &&
1030 state->tree) {
1031 node = &state->rb_node;
1032 goto hit_next;
1033 }
1034 }
1035
Josef Bacik462d6fa2011-09-26 13:56:12 -04001036 /*
1037 * this search will find all the extents that end after
1038 * our range starts.
1039 */
1040 node = tree_search(tree, start);
1041 if (!node) {
1042 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001043 if (!prealloc) {
1044 err = -ENOMEM;
1045 goto out;
1046 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001047 err = insert_state(tree, prealloc, start, end, &bits);
1048 prealloc = NULL;
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001049 if (err)
1050 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001051 goto out;
1052 }
1053 state = rb_entry(node, struct extent_state, rb_node);
1054hit_next:
1055 last_start = state->start;
1056 last_end = state->end;
1057
1058 /*
1059 * | ---- desired range ---- |
1060 * | state |
1061 *
1062 * Just lock what we found and keep going
1063 */
1064 if (state->start == start && state->end <= end) {
Josef Bacik462d6fa2011-09-26 13:56:12 -04001065 set_state_bits(tree, state, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001066 cache_state(state, cached_state);
Liu Bod1ac6e42012-05-10 18:10:39 +08001067 state = clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001068 if (last_end == (u64)-1)
1069 goto out;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001070 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001071 if (start < end && state && state->start == start &&
1072 !need_resched())
1073 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001074 goto search_again;
1075 }
1076
1077 /*
1078 * | ---- desired range ---- |
1079 * | state |
1080 * or
1081 * | ------------- state -------------- |
1082 *
1083 * We need to split the extent we found, and may flip bits on
1084 * second half.
1085 *
1086 * If the extent we found extends past our
1087 * range, we just split and search again. It'll get split
1088 * again the next time though.
1089 *
1090 * If the extent we found is inside our range, we set the
1091 * desired bit on it.
1092 */
1093 if (state->start < start) {
1094 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001095 if (!prealloc) {
1096 err = -ENOMEM;
1097 goto out;
1098 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001099 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001100 if (err)
1101 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001102 prealloc = NULL;
1103 if (err)
1104 goto out;
1105 if (state->end <= end) {
1106 set_state_bits(tree, state, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001107 cache_state(state, cached_state);
Liu Bod1ac6e42012-05-10 18:10:39 +08001108 state = clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001109 if (last_end == (u64)-1)
1110 goto out;
1111 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001112 if (start < end && state && state->start == start &&
1113 !need_resched())
1114 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001115 }
1116 goto search_again;
1117 }
1118 /*
1119 * | ---- desired range ---- |
1120 * | state | or | state |
1121 *
1122 * There's a hole, we need to insert something in it and
1123 * ignore the extent we found.
1124 */
1125 if (state->start > start) {
1126 u64 this_end;
1127 if (end < last_start)
1128 this_end = end;
1129 else
1130 this_end = last_start - 1;
1131
1132 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001133 if (!prealloc) {
1134 err = -ENOMEM;
1135 goto out;
1136 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001137
1138 /*
1139 * Avoid to free 'prealloc' if it can be merged with
1140 * the later extent.
1141 */
1142 err = insert_state(tree, prealloc, start, this_end,
1143 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001144 if (err)
1145 extent_io_tree_panic(tree, err);
Josef Bacike6138872012-09-27 17:07:30 -04001146 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001147 prealloc = NULL;
1148 start = this_end + 1;
1149 goto search_again;
1150 }
1151 /*
1152 * | ---- desired range ---- |
1153 * | state |
1154 * We need to split the extent, and set the bit
1155 * on the first half
1156 */
1157 if (state->start <= end && state->end > end) {
1158 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001159 if (!prealloc) {
1160 err = -ENOMEM;
1161 goto out;
1162 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001163
1164 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001165 if (err)
1166 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001167
1168 set_state_bits(tree, prealloc, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001169 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001170 clear_state_bit(tree, prealloc, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001171 prealloc = NULL;
1172 goto out;
1173 }
1174
1175 goto search_again;
1176
1177out:
1178 spin_unlock(&tree->lock);
1179 if (prealloc)
1180 free_extent_state(prealloc);
1181
1182 return err;
1183
1184search_again:
1185 if (start > end)
1186 goto out;
1187 spin_unlock(&tree->lock);
1188 if (mask & __GFP_WAIT)
1189 cond_resched();
1190 goto again;
1191}
1192
Chris Masond1310b22008-01-24 16:13:08 -05001193/* wrappers around set/clear extent bit */
1194int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1195 gfp_t mask)
1196{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001197 return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001198 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001199}
Chris Masond1310b22008-01-24 16:13:08 -05001200
1201int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +00001202 unsigned long bits, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001203{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001204 return set_extent_bit(tree, start, end, bits, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001205 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001206}
Chris Masond1310b22008-01-24 16:13:08 -05001207
1208int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +00001209 unsigned long bits, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001210{
Chris Mason2c64c532009-09-02 15:04:12 -04001211 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001212}
Chris Masond1310b22008-01-24 16:13:08 -05001213
1214int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001215 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001216{
1217 return set_extent_bit(tree, start, end,
Liu Bofee187d2011-09-29 15:55:28 +08001218 EXTENT_DELALLOC | EXTENT_UPTODATE,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001219 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001220}
Chris Masond1310b22008-01-24 16:13:08 -05001221
Liu Bo9e8a4a82012-09-05 19:10:51 -06001222int set_extent_defrag(struct extent_io_tree *tree, u64 start, u64 end,
1223 struct extent_state **cached_state, gfp_t mask)
1224{
1225 return set_extent_bit(tree, start, end,
1226 EXTENT_DELALLOC | EXTENT_UPTODATE | EXTENT_DEFRAG,
1227 NULL, cached_state, mask);
1228}
1229
Chris Masond1310b22008-01-24 16:13:08 -05001230int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1231 gfp_t mask)
1232{
1233 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04001234 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001235 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001236}
Chris Masond1310b22008-01-24 16:13:08 -05001237
1238int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1239 gfp_t mask)
1240{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001241 return set_extent_bit(tree, start, end, EXTENT_NEW, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001242 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001243}
Chris Masond1310b22008-01-24 16:13:08 -05001244
Chris Masond1310b22008-01-24 16:13:08 -05001245int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +00001246 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001247{
Liu Bo6b67a322013-03-28 08:30:28 +00001248 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, NULL,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001249 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001250}
Chris Masond1310b22008-01-24 16:13:08 -05001251
Josef Bacik5fd02042012-05-02 14:00:54 -04001252int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1253 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001254{
Chris Mason2c64c532009-09-02 15:04:12 -04001255 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001256 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001257}
Chris Masond1310b22008-01-24 16:13:08 -05001258
Chris Masond352ac62008-09-29 15:18:18 -04001259/*
1260 * either insert or lock state struct between start and end use mask to tell
1261 * us if waiting is desired.
1262 */
Chris Mason1edbb732009-09-02 13:24:36 -04001263int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +00001264 unsigned long bits, struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001265{
1266 int err;
1267 u64 failed_start;
1268 while (1) {
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001269 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
1270 EXTENT_LOCKED, &failed_start,
1271 cached_state, GFP_NOFS);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001272 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001273 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1274 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001275 } else
Chris Masond1310b22008-01-24 16:13:08 -05001276 break;
Chris Masond1310b22008-01-24 16:13:08 -05001277 WARN_ON(start > end);
1278 }
1279 return err;
1280}
Chris Masond1310b22008-01-24 16:13:08 -05001281
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001282int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Mason1edbb732009-09-02 13:24:36 -04001283{
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001284 return lock_extent_bits(tree, start, end, 0, NULL);
Chris Mason1edbb732009-09-02 13:24:36 -04001285}
1286
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001287int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001288{
1289 int err;
1290 u64 failed_start;
1291
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001292 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1293 &failed_start, NULL, GFP_NOFS);
Yan Zheng66435582008-10-30 14:19:50 -04001294 if (err == -EEXIST) {
1295 if (failed_start > start)
1296 clear_extent_bit(tree, start, failed_start - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001297 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
Josef Bacik25179202008-10-29 14:49:05 -04001298 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001299 }
Josef Bacik25179202008-10-29 14:49:05 -04001300 return 1;
1301}
Josef Bacik25179202008-10-29 14:49:05 -04001302
Chris Mason2c64c532009-09-02 15:04:12 -04001303int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1304 struct extent_state **cached, gfp_t mask)
1305{
1306 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1307 mask);
1308}
1309
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001310int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001311{
Chris Mason2c64c532009-09-02 15:04:12 -04001312 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001313 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001314}
Chris Masond1310b22008-01-24 16:13:08 -05001315
Chris Mason4adaa612013-03-26 13:07:00 -04001316int extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
1317{
1318 unsigned long index = start >> PAGE_CACHE_SHIFT;
1319 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1320 struct page *page;
1321
1322 while (index <= end_index) {
1323 page = find_get_page(inode->i_mapping, index);
1324 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1325 clear_page_dirty_for_io(page);
1326 page_cache_release(page);
1327 index++;
1328 }
1329 return 0;
1330}
1331
1332int extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
1333{
1334 unsigned long index = start >> PAGE_CACHE_SHIFT;
1335 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1336 struct page *page;
1337
1338 while (index <= end_index) {
1339 page = find_get_page(inode->i_mapping, index);
1340 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1341 account_page_redirty(page);
1342 __set_page_dirty_nobuffers(page);
1343 page_cache_release(page);
1344 index++;
1345 }
1346 return 0;
1347}
1348
Chris Masond1310b22008-01-24 16:13:08 -05001349/*
Chris Masond1310b22008-01-24 16:13:08 -05001350 * helper function to set both pages and extents in the tree writeback
1351 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001352static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001353{
1354 unsigned long index = start >> PAGE_CACHE_SHIFT;
1355 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1356 struct page *page;
1357
1358 while (index <= end_index) {
1359 page = find_get_page(tree->mapping, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001360 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Masond1310b22008-01-24 16:13:08 -05001361 set_page_writeback(page);
1362 page_cache_release(page);
1363 index++;
1364 }
Chris Masond1310b22008-01-24 16:13:08 -05001365 return 0;
1366}
Chris Masond1310b22008-01-24 16:13:08 -05001367
Chris Masond352ac62008-09-29 15:18:18 -04001368/* find the first state struct with 'bits' set after 'start', and
1369 * return it. tree->lock must be held. NULL will returned if
1370 * nothing was found after 'start'
1371 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001372static struct extent_state *
1373find_first_extent_bit_state(struct extent_io_tree *tree,
David Sterba41074882013-04-29 13:38:46 +00001374 u64 start, unsigned long bits)
Chris Masond7fc6402008-02-18 12:12:38 -05001375{
1376 struct rb_node *node;
1377 struct extent_state *state;
1378
1379 /*
1380 * this search will find all the extents that end after
1381 * our range starts.
1382 */
1383 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001384 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001385 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001386
Chris Masond3977122009-01-05 21:25:51 -05001387 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001388 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001389 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001390 return state;
Chris Masond3977122009-01-05 21:25:51 -05001391
Chris Masond7fc6402008-02-18 12:12:38 -05001392 node = rb_next(node);
1393 if (!node)
1394 break;
1395 }
1396out:
1397 return NULL;
1398}
Chris Masond7fc6402008-02-18 12:12:38 -05001399
Chris Masond352ac62008-09-29 15:18:18 -04001400/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001401 * find the first offset in the io tree with 'bits' set. zero is
1402 * returned if we find something, and *start_ret and *end_ret are
1403 * set to reflect the state struct that was found.
1404 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001405 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001406 */
1407int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
David Sterba41074882013-04-29 13:38:46 +00001408 u64 *start_ret, u64 *end_ret, unsigned long bits,
Josef Bacike6138872012-09-27 17:07:30 -04001409 struct extent_state **cached_state)
Xiao Guangrong69261c42011-07-14 03:19:45 +00001410{
1411 struct extent_state *state;
Josef Bacike6138872012-09-27 17:07:30 -04001412 struct rb_node *n;
Xiao Guangrong69261c42011-07-14 03:19:45 +00001413 int ret = 1;
1414
1415 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001416 if (cached_state && *cached_state) {
1417 state = *cached_state;
1418 if (state->end == start - 1 && state->tree) {
1419 n = rb_next(&state->rb_node);
1420 while (n) {
1421 state = rb_entry(n, struct extent_state,
1422 rb_node);
1423 if (state->state & bits)
1424 goto got_it;
1425 n = rb_next(n);
1426 }
1427 free_extent_state(*cached_state);
1428 *cached_state = NULL;
1429 goto out;
1430 }
1431 free_extent_state(*cached_state);
1432 *cached_state = NULL;
1433 }
1434
Xiao Guangrong69261c42011-07-14 03:19:45 +00001435 state = find_first_extent_bit_state(tree, start, bits);
Josef Bacike6138872012-09-27 17:07:30 -04001436got_it:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001437 if (state) {
Josef Bacike6138872012-09-27 17:07:30 -04001438 cache_state(state, cached_state);
Xiao Guangrong69261c42011-07-14 03:19:45 +00001439 *start_ret = state->start;
1440 *end_ret = state->end;
1441 ret = 0;
1442 }
Josef Bacike6138872012-09-27 17:07:30 -04001443out:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001444 spin_unlock(&tree->lock);
1445 return ret;
1446}
1447
1448/*
Chris Masond352ac62008-09-29 15:18:18 -04001449 * find a contiguous range of bytes in the file marked as delalloc, not
1450 * more than 'max_bytes'. start and end are used to return the range,
1451 *
1452 * 1 is returned if we find something, 0 if nothing was in the tree
1453 */
Chris Masonc8b97812008-10-29 14:49:59 -04001454static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001455 u64 *start, u64 *end, u64 max_bytes,
1456 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001457{
1458 struct rb_node *node;
1459 struct extent_state *state;
1460 u64 cur_start = *start;
1461 u64 found = 0;
1462 u64 total_bytes = 0;
1463
Chris Masoncad321a2008-12-17 14:51:42 -05001464 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001465
Chris Masond1310b22008-01-24 16:13:08 -05001466 /*
1467 * this search will find all the extents that end after
1468 * our range starts.
1469 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001470 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001471 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001472 if (!found)
1473 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001474 goto out;
1475 }
1476
Chris Masond3977122009-01-05 21:25:51 -05001477 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001478 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001479 if (found && (state->start != cur_start ||
1480 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001481 goto out;
1482 }
1483 if (!(state->state & EXTENT_DELALLOC)) {
1484 if (!found)
1485 *end = state->end;
1486 goto out;
1487 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001488 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001489 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001490 *cached_state = state;
1491 atomic_inc(&state->refs);
1492 }
Chris Masond1310b22008-01-24 16:13:08 -05001493 found++;
1494 *end = state->end;
1495 cur_start = state->end + 1;
1496 node = rb_next(node);
1497 if (!node)
1498 break;
1499 total_bytes += state->end - state->start + 1;
1500 if (total_bytes >= max_bytes)
1501 break;
1502 }
1503out:
Chris Masoncad321a2008-12-17 14:51:42 -05001504 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001505 return found;
1506}
1507
Jeff Mahoney143bede2012-03-01 14:56:26 +01001508static noinline void __unlock_for_delalloc(struct inode *inode,
1509 struct page *locked_page,
1510 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001511{
1512 int ret;
1513 struct page *pages[16];
1514 unsigned long index = start >> PAGE_CACHE_SHIFT;
1515 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1516 unsigned long nr_pages = end_index - index + 1;
1517 int i;
1518
1519 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001520 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001521
Chris Masond3977122009-01-05 21:25:51 -05001522 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001523 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001524 min_t(unsigned long, nr_pages,
1525 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001526 for (i = 0; i < ret; i++) {
1527 if (pages[i] != locked_page)
1528 unlock_page(pages[i]);
1529 page_cache_release(pages[i]);
1530 }
1531 nr_pages -= ret;
1532 index += ret;
1533 cond_resched();
1534 }
Chris Masonc8b97812008-10-29 14:49:59 -04001535}
1536
1537static noinline int lock_delalloc_pages(struct inode *inode,
1538 struct page *locked_page,
1539 u64 delalloc_start,
1540 u64 delalloc_end)
1541{
1542 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1543 unsigned long start_index = index;
1544 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1545 unsigned long pages_locked = 0;
1546 struct page *pages[16];
1547 unsigned long nrpages;
1548 int ret;
1549 int i;
1550
1551 /* the caller is responsible for locking the start index */
1552 if (index == locked_page->index && index == end_index)
1553 return 0;
1554
1555 /* skip the page at the start index */
1556 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001557 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001558 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001559 min_t(unsigned long,
1560 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001561 if (ret == 0) {
1562 ret = -EAGAIN;
1563 goto done;
1564 }
1565 /* now we have an array of pages, lock them all */
1566 for (i = 0; i < ret; i++) {
1567 /*
1568 * the caller is taking responsibility for
1569 * locked_page
1570 */
Chris Mason771ed682008-11-06 22:02:51 -05001571 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001572 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001573 if (!PageDirty(pages[i]) ||
1574 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001575 ret = -EAGAIN;
1576 unlock_page(pages[i]);
1577 page_cache_release(pages[i]);
1578 goto done;
1579 }
1580 }
Chris Masonc8b97812008-10-29 14:49:59 -04001581 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001582 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001583 }
Chris Masonc8b97812008-10-29 14:49:59 -04001584 nrpages -= ret;
1585 index += ret;
1586 cond_resched();
1587 }
1588 ret = 0;
1589done:
1590 if (ret && pages_locked) {
1591 __unlock_for_delalloc(inode, locked_page,
1592 delalloc_start,
1593 ((u64)(start_index + pages_locked - 1)) <<
1594 PAGE_CACHE_SHIFT);
1595 }
1596 return ret;
1597}
1598
1599/*
1600 * find a contiguous range of bytes in the file marked as delalloc, not
1601 * more than 'max_bytes'. start and end are used to return the range,
1602 *
1603 * 1 is returned if we find something, 0 if nothing was in the tree
1604 */
1605static noinline u64 find_lock_delalloc_range(struct inode *inode,
1606 struct extent_io_tree *tree,
1607 struct page *locked_page,
1608 u64 *start, u64 *end,
1609 u64 max_bytes)
1610{
1611 u64 delalloc_start;
1612 u64 delalloc_end;
1613 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001614 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001615 int ret;
1616 int loops = 0;
1617
1618again:
1619 /* step one, find a bunch of delalloc bytes starting at start */
1620 delalloc_start = *start;
1621 delalloc_end = 0;
1622 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001623 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001624 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001625 *start = delalloc_start;
1626 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001627 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001628 return found;
1629 }
1630
1631 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001632 * start comes from the offset of locked_page. We have to lock
1633 * pages in order, so we can't process delalloc bytes before
1634 * locked_page
1635 */
Chris Masond3977122009-01-05 21:25:51 -05001636 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001637 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001638
1639 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001640 * make sure to limit the number of pages we try to lock down
1641 * if we're looping.
1642 */
Chris Masond3977122009-01-05 21:25:51 -05001643 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001644 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001645
Chris Masonc8b97812008-10-29 14:49:59 -04001646 /* step two, lock all the pages after the page that has start */
1647 ret = lock_delalloc_pages(inode, locked_page,
1648 delalloc_start, delalloc_end);
1649 if (ret == -EAGAIN) {
1650 /* some of the pages are gone, lets avoid looping by
1651 * shortening the size of the delalloc range we're searching
1652 */
Chris Mason9655d292009-09-02 15:22:30 -04001653 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001654 if (!loops) {
1655 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1656 max_bytes = PAGE_CACHE_SIZE - offset;
1657 loops = 1;
1658 goto again;
1659 } else {
1660 found = 0;
1661 goto out_failed;
1662 }
1663 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001664 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
Chris Masonc8b97812008-10-29 14:49:59 -04001665
1666 /* step three, lock the state bits for the whole range */
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001667 lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001668
1669 /* then test to make sure it is all still delalloc */
1670 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001671 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001672 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001673 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1674 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001675 __unlock_for_delalloc(inode, locked_page,
1676 delalloc_start, delalloc_end);
1677 cond_resched();
1678 goto again;
1679 }
Chris Mason9655d292009-09-02 15:22:30 -04001680 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001681 *start = delalloc_start;
1682 *end = delalloc_end;
1683out_failed:
1684 return found;
1685}
1686
1687int extent_clear_unlock_delalloc(struct inode *inode,
1688 struct extent_io_tree *tree,
1689 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001690 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001691{
1692 int ret;
1693 struct page *pages[16];
1694 unsigned long index = start >> PAGE_CACHE_SHIFT;
1695 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1696 unsigned long nr_pages = end_index - index + 1;
1697 int i;
David Sterba41074882013-04-29 13:38:46 +00001698 unsigned long clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001699
Chris Masona791e352009-10-08 11:27:10 -04001700 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001701 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001702 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001703 clear_bits |= EXTENT_DIRTY;
1704
Chris Masona791e352009-10-08 11:27:10 -04001705 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001706 clear_bits |= EXTENT_DELALLOC;
1707
Chris Mason2c64c532009-09-02 15:04:12 -04001708 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001709 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1710 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1711 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001712 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001713
Chris Masond3977122009-01-05 21:25:51 -05001714 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001715 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001716 min_t(unsigned long,
1717 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001718 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001719
Chris Masona791e352009-10-08 11:27:10 -04001720 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001721 SetPagePrivate2(pages[i]);
1722
Chris Masonc8b97812008-10-29 14:49:59 -04001723 if (pages[i] == locked_page) {
1724 page_cache_release(pages[i]);
1725 continue;
1726 }
Chris Masona791e352009-10-08 11:27:10 -04001727 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001728 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001729 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001730 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001731 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001732 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001733 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001734 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001735 page_cache_release(pages[i]);
1736 }
1737 nr_pages -= ret;
1738 index += ret;
1739 cond_resched();
1740 }
1741 return 0;
1742}
Chris Masonc8b97812008-10-29 14:49:59 -04001743
Chris Masond352ac62008-09-29 15:18:18 -04001744/*
1745 * count the number of bytes in the tree that have a given bit(s)
1746 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1747 * cached. The total number found is returned.
1748 */
Chris Masond1310b22008-01-24 16:13:08 -05001749u64 count_range_bits(struct extent_io_tree *tree,
1750 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001751 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001752{
1753 struct rb_node *node;
1754 struct extent_state *state;
1755 u64 cur_start = *start;
1756 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001757 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001758 int found = 0;
1759
1760 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001761 WARN_ON(1);
1762 return 0;
1763 }
1764
Chris Masoncad321a2008-12-17 14:51:42 -05001765 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001766 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1767 total_bytes = tree->dirty_bytes;
1768 goto out;
1769 }
1770 /*
1771 * this search will find all the extents that end after
1772 * our range starts.
1773 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001774 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001775 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001776 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001777
Chris Masond3977122009-01-05 21:25:51 -05001778 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001779 state = rb_entry(node, struct extent_state, rb_node);
1780 if (state->start > search_end)
1781 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001782 if (contig && found && state->start > last + 1)
1783 break;
1784 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001785 total_bytes += min(search_end, state->end) + 1 -
1786 max(cur_start, state->start);
1787 if (total_bytes >= max_bytes)
1788 break;
1789 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001790 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001791 found = 1;
1792 }
Chris Masonec29ed52011-02-23 16:23:20 -05001793 last = state->end;
1794 } else if (contig && found) {
1795 break;
Chris Masond1310b22008-01-24 16:13:08 -05001796 }
1797 node = rb_next(node);
1798 if (!node)
1799 break;
1800 }
1801out:
Chris Masoncad321a2008-12-17 14:51:42 -05001802 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001803 return total_bytes;
1804}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001805
Chris Masond352ac62008-09-29 15:18:18 -04001806/*
1807 * set the private field for a given byte offset in the tree. If there isn't
1808 * an extent_state there already, this does nothing.
1809 */
Chris Masond1310b22008-01-24 16:13:08 -05001810int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1811{
1812 struct rb_node *node;
1813 struct extent_state *state;
1814 int ret = 0;
1815
Chris Masoncad321a2008-12-17 14:51:42 -05001816 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001817 /*
1818 * this search will find all the extents that end after
1819 * our range starts.
1820 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001821 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001822 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001823 ret = -ENOENT;
1824 goto out;
1825 }
1826 state = rb_entry(node, struct extent_state, rb_node);
1827 if (state->start != start) {
1828 ret = -ENOENT;
1829 goto out;
1830 }
1831 state->private = private;
1832out:
Chris Masoncad321a2008-12-17 14:51:42 -05001833 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001834 return ret;
1835}
1836
Miao Xiee4100d92013-04-05 07:20:56 +00001837void extent_cache_csums_dio(struct extent_io_tree *tree, u64 start, u32 csums[],
1838 int count)
1839{
1840 struct rb_node *node;
1841 struct extent_state *state;
1842
1843 spin_lock(&tree->lock);
1844 /*
1845 * this search will find all the extents that end after
1846 * our range starts.
1847 */
1848 node = tree_search(tree, start);
1849 BUG_ON(!node);
1850
1851 state = rb_entry(node, struct extent_state, rb_node);
1852 BUG_ON(state->start != start);
1853
1854 while (count) {
1855 state->private = *csums++;
1856 count--;
1857 state = next_state(state);
1858 }
1859 spin_unlock(&tree->lock);
1860}
1861
1862static inline u64 __btrfs_get_bio_offset(struct bio *bio, int bio_index)
1863{
1864 struct bio_vec *bvec = bio->bi_io_vec + bio_index;
1865
1866 return page_offset(bvec->bv_page) + bvec->bv_offset;
1867}
1868
1869void extent_cache_csums(struct extent_io_tree *tree, struct bio *bio, int bio_index,
1870 u32 csums[], int count)
1871{
1872 struct rb_node *node;
1873 struct extent_state *state = NULL;
1874 u64 start;
1875
1876 spin_lock(&tree->lock);
1877 do {
1878 start = __btrfs_get_bio_offset(bio, bio_index);
1879 if (state == NULL || state->start != start) {
1880 node = tree_search(tree, start);
1881 BUG_ON(!node);
1882
1883 state = rb_entry(node, struct extent_state, rb_node);
1884 BUG_ON(state->start != start);
1885 }
1886 state->private = *csums++;
1887 count--;
1888 bio_index++;
1889
1890 state = next_state(state);
1891 } while (count);
1892 spin_unlock(&tree->lock);
1893}
1894
Chris Masond1310b22008-01-24 16:13:08 -05001895int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1896{
1897 struct rb_node *node;
1898 struct extent_state *state;
1899 int ret = 0;
1900
Chris Masoncad321a2008-12-17 14:51:42 -05001901 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001902 /*
1903 * this search will find all the extents that end after
1904 * our range starts.
1905 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001906 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001907 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001908 ret = -ENOENT;
1909 goto out;
1910 }
1911 state = rb_entry(node, struct extent_state, rb_node);
1912 if (state->start != start) {
1913 ret = -ENOENT;
1914 goto out;
1915 }
1916 *private = state->private;
1917out:
Chris Masoncad321a2008-12-17 14:51:42 -05001918 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001919 return ret;
1920}
1921
1922/*
1923 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001924 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001925 * has the bits set. Otherwise, 1 is returned if any bit in the
1926 * range is found set.
1927 */
1928int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +00001929 unsigned long bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001930{
1931 struct extent_state *state = NULL;
1932 struct rb_node *node;
1933 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001934
Chris Masoncad321a2008-12-17 14:51:42 -05001935 spin_lock(&tree->lock);
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001936 if (cached && cached->tree && cached->start <= start &&
1937 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001938 node = &cached->rb_node;
1939 else
1940 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001941 while (node && start <= end) {
1942 state = rb_entry(node, struct extent_state, rb_node);
1943
1944 if (filled && state->start > start) {
1945 bitset = 0;
1946 break;
1947 }
1948
1949 if (state->start > end)
1950 break;
1951
1952 if (state->state & bits) {
1953 bitset = 1;
1954 if (!filled)
1955 break;
1956 } else if (filled) {
1957 bitset = 0;
1958 break;
1959 }
Chris Mason46562cec2009-09-23 20:23:16 -04001960
1961 if (state->end == (u64)-1)
1962 break;
1963
Chris Masond1310b22008-01-24 16:13:08 -05001964 start = state->end + 1;
1965 if (start > end)
1966 break;
1967 node = rb_next(node);
1968 if (!node) {
1969 if (filled)
1970 bitset = 0;
1971 break;
1972 }
1973 }
Chris Masoncad321a2008-12-17 14:51:42 -05001974 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001975 return bitset;
1976}
Chris Masond1310b22008-01-24 16:13:08 -05001977
1978/*
1979 * helper function to set a given page up to date if all the
1980 * extents in the tree for that page are up to date
1981 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001982static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001983{
Miao Xie4eee4fa2012-12-21 09:17:45 +00001984 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05001985 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001986 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001987 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001988}
1989
1990/*
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001991 * When IO fails, either with EIO or csum verification fails, we
1992 * try other mirrors that might have a good copy of the data. This
1993 * io_failure_record is used to record state as we go through all the
1994 * mirrors. If another mirror has good data, the page is set up to date
1995 * and things continue. If a good mirror can't be found, the original
1996 * bio end_io callback is called to indicate things have failed.
1997 */
1998struct io_failure_record {
1999 struct page *page;
2000 u64 start;
2001 u64 len;
2002 u64 logical;
2003 unsigned long bio_flags;
2004 int this_mirror;
2005 int failed_mirror;
2006 int in_validation;
2007};
2008
2009static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
2010 int did_repair)
2011{
2012 int ret;
2013 int err = 0;
2014 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2015
2016 set_state_private(failure_tree, rec->start, 0);
2017 ret = clear_extent_bits(failure_tree, rec->start,
2018 rec->start + rec->len - 1,
2019 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2020 if (ret)
2021 err = ret;
2022
David Woodhouse53b381b2013-01-29 18:40:14 -05002023 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
2024 rec->start + rec->len - 1,
2025 EXTENT_DAMAGED, GFP_NOFS);
2026 if (ret && !err)
2027 err = ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002028
2029 kfree(rec);
2030 return err;
2031}
2032
2033static void repair_io_failure_callback(struct bio *bio, int err)
2034{
2035 complete(bio->bi_private);
2036}
2037
2038/*
2039 * this bypasses the standard btrfs submit functions deliberately, as
2040 * the standard behavior is to write all copies in a raid setup. here we only
2041 * want to write the one bad copy. so we do the mapping for ourselves and issue
2042 * submit_bio directly.
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002043 * to avoid any synchronization issues, wait for the data after writing, which
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002044 * actually prevents the read that triggered the error from finishing.
2045 * currently, there can be no more than two copies of every data bit. thus,
2046 * exactly one rewrite is required.
2047 */
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002048int repair_io_failure(struct btrfs_fs_info *fs_info, u64 start,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002049 u64 length, u64 logical, struct page *page,
2050 int mirror_num)
2051{
2052 struct bio *bio;
2053 struct btrfs_device *dev;
2054 DECLARE_COMPLETION_ONSTACK(compl);
2055 u64 map_length = 0;
2056 u64 sector;
2057 struct btrfs_bio *bbio = NULL;
David Woodhouse53b381b2013-01-29 18:40:14 -05002058 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002059 int ret;
2060
2061 BUG_ON(!mirror_num);
2062
David Woodhouse53b381b2013-01-29 18:40:14 -05002063 /* we can't repair anything in raid56 yet */
2064 if (btrfs_is_parity_mirror(map_tree, logical, length, mirror_num))
2065 return 0;
2066
Chris Mason9be33952013-05-17 18:30:14 -04002067 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002068 if (!bio)
2069 return -EIO;
2070 bio->bi_private = &compl;
2071 bio->bi_end_io = repair_io_failure_callback;
2072 bio->bi_size = 0;
2073 map_length = length;
2074
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002075 ret = btrfs_map_block(fs_info, WRITE, logical,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002076 &map_length, &bbio, mirror_num);
2077 if (ret) {
2078 bio_put(bio);
2079 return -EIO;
2080 }
2081 BUG_ON(mirror_num != bbio->mirror_num);
2082 sector = bbio->stripes[mirror_num-1].physical >> 9;
2083 bio->bi_sector = sector;
2084 dev = bbio->stripes[mirror_num-1].dev;
2085 kfree(bbio);
2086 if (!dev || !dev->bdev || !dev->writeable) {
2087 bio_put(bio);
2088 return -EIO;
2089 }
2090 bio->bi_bdev = dev->bdev;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002091 bio_add_page(bio, page, length, start - page_offset(page));
Stefan Behrens21adbd52011-11-09 13:44:05 +01002092 btrfsic_submit_bio(WRITE_SYNC, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002093 wait_for_completion(&compl);
2094
2095 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2096 /* try to remap that extent elsewhere? */
2097 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002098 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002099 return -EIO;
2100 }
2101
Anand Jaind5b025d2012-07-02 22:05:21 -06002102 printk_ratelimited_in_rcu(KERN_INFO "btrfs read error corrected: ino %lu off %llu "
Josef Bacik606686e2012-06-04 14:03:51 -04002103 "(dev %s sector %llu)\n", page->mapping->host->i_ino,
2104 start, rcu_str_deref(dev->name), sector);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002105
2106 bio_put(bio);
2107 return 0;
2108}
2109
Josef Bacikea466792012-03-26 21:57:36 -04002110int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
2111 int mirror_num)
2112{
Josef Bacikea466792012-03-26 21:57:36 -04002113 u64 start = eb->start;
2114 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond95603b2012-04-12 15:55:15 -04002115 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04002116
2117 for (i = 0; i < num_pages; i++) {
2118 struct page *p = extent_buffer_page(eb, i);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002119 ret = repair_io_failure(root->fs_info, start, PAGE_CACHE_SIZE,
Josef Bacikea466792012-03-26 21:57:36 -04002120 start, p, mirror_num);
2121 if (ret)
2122 break;
2123 start += PAGE_CACHE_SIZE;
2124 }
2125
2126 return ret;
2127}
2128
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002129/*
2130 * each time an IO finishes, we do a fast check in the IO failure tree
2131 * to see if we need to process or clean up an io_failure_record
2132 */
2133static int clean_io_failure(u64 start, struct page *page)
2134{
2135 u64 private;
2136 u64 private_failure;
2137 struct io_failure_record *failrec;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002138 struct btrfs_fs_info *fs_info;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002139 struct extent_state *state;
2140 int num_copies;
2141 int did_repair = 0;
2142 int ret;
2143 struct inode *inode = page->mapping->host;
2144
2145 private = 0;
2146 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
2147 (u64)-1, 1, EXTENT_DIRTY, 0);
2148 if (!ret)
2149 return 0;
2150
2151 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
2152 &private_failure);
2153 if (ret)
2154 return 0;
2155
2156 failrec = (struct io_failure_record *)(unsigned long) private_failure;
2157 BUG_ON(!failrec->this_mirror);
2158
2159 if (failrec->in_validation) {
2160 /* there was no real error, just free the record */
2161 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
2162 failrec->start);
2163 did_repair = 1;
2164 goto out;
2165 }
2166
2167 spin_lock(&BTRFS_I(inode)->io_tree.lock);
2168 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
2169 failrec->start,
2170 EXTENT_LOCKED);
2171 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
2172
2173 if (state && state->start == failrec->start) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002174 fs_info = BTRFS_I(inode)->root->fs_info;
2175 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2176 failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002177 if (num_copies > 1) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002178 ret = repair_io_failure(fs_info, start, failrec->len,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002179 failrec->logical, page,
2180 failrec->failed_mirror);
2181 did_repair = !ret;
2182 }
David Woodhouse53b381b2013-01-29 18:40:14 -05002183 ret = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002184 }
2185
2186out:
2187 if (!ret)
2188 ret = free_io_failure(inode, failrec, did_repair);
2189
2190 return ret;
2191}
2192
2193/*
2194 * this is a generic handler for readpage errors (default
2195 * readpage_io_failed_hook). if other copies exist, read those and write back
2196 * good data to the failed position. does not investigate in remapping the
2197 * failed extent elsewhere, hoping the device will be smart enough to do this as
2198 * needed
2199 */
2200
2201static int bio_readpage_error(struct bio *failed_bio, struct page *page,
2202 u64 start, u64 end, int failed_mirror,
2203 struct extent_state *state)
2204{
2205 struct io_failure_record *failrec = NULL;
2206 u64 private;
2207 struct extent_map *em;
2208 struct inode *inode = page->mapping->host;
2209 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2210 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2211 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2212 struct bio *bio;
2213 int num_copies;
2214 int ret;
2215 int read_mode;
2216 u64 logical;
2217
2218 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2219
2220 ret = get_state_private(failure_tree, start, &private);
2221 if (ret) {
2222 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2223 if (!failrec)
2224 return -ENOMEM;
2225 failrec->start = start;
2226 failrec->len = end - start + 1;
2227 failrec->this_mirror = 0;
2228 failrec->bio_flags = 0;
2229 failrec->in_validation = 0;
2230
2231 read_lock(&em_tree->lock);
2232 em = lookup_extent_mapping(em_tree, start, failrec->len);
2233 if (!em) {
2234 read_unlock(&em_tree->lock);
2235 kfree(failrec);
2236 return -EIO;
2237 }
2238
2239 if (em->start > start || em->start + em->len < start) {
2240 free_extent_map(em);
2241 em = NULL;
2242 }
2243 read_unlock(&em_tree->lock);
2244
Tsutomu Itoh7a2d6a62012-10-01 03:07:15 -06002245 if (!em) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002246 kfree(failrec);
2247 return -EIO;
2248 }
2249 logical = start - em->start;
2250 logical = em->block_start + logical;
2251 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2252 logical = em->block_start;
2253 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2254 extent_set_compress_type(&failrec->bio_flags,
2255 em->compress_type);
2256 }
2257 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2258 "len=%llu\n", logical, start, failrec->len);
2259 failrec->logical = logical;
2260 free_extent_map(em);
2261
2262 /* set the bits in the private failure tree */
2263 ret = set_extent_bits(failure_tree, start, end,
2264 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2265 if (ret >= 0)
2266 ret = set_state_private(failure_tree, start,
2267 (u64)(unsigned long)failrec);
2268 /* set the bits in the inode's tree */
2269 if (ret >= 0)
2270 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2271 GFP_NOFS);
2272 if (ret < 0) {
2273 kfree(failrec);
2274 return ret;
2275 }
2276 } else {
2277 failrec = (struct io_failure_record *)(unsigned long)private;
2278 pr_debug("bio_readpage_error: (found) logical=%llu, "
2279 "start=%llu, len=%llu, validation=%d\n",
2280 failrec->logical, failrec->start, failrec->len,
2281 failrec->in_validation);
2282 /*
2283 * when data can be on disk more than twice, add to failrec here
2284 * (e.g. with a list for failed_mirror) to make
2285 * clean_io_failure() clean all those errors at once.
2286 */
2287 }
Stefan Behrens5d964052012-11-05 14:59:07 +01002288 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
2289 failrec->logical, failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002290 if (num_copies == 1) {
2291 /*
2292 * we only have a single copy of the data, so don't bother with
2293 * all the retry and error correction code that follows. no
2294 * matter what the error is, it is very likely to persist.
2295 */
2296 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2297 "state=%p, num_copies=%d, next_mirror %d, "
2298 "failed_mirror %d\n", state, num_copies,
2299 failrec->this_mirror, failed_mirror);
2300 free_io_failure(inode, failrec, 0);
2301 return -EIO;
2302 }
2303
2304 if (!state) {
2305 spin_lock(&tree->lock);
2306 state = find_first_extent_bit_state(tree, failrec->start,
2307 EXTENT_LOCKED);
2308 if (state && state->start != failrec->start)
2309 state = NULL;
2310 spin_unlock(&tree->lock);
2311 }
2312
2313 /*
2314 * there are two premises:
2315 * a) deliver good data to the caller
2316 * b) correct the bad sectors on disk
2317 */
2318 if (failed_bio->bi_vcnt > 1) {
2319 /*
2320 * to fulfill b), we need to know the exact failing sectors, as
2321 * we don't want to rewrite any more than the failed ones. thus,
2322 * we need separate read requests for the failed bio
2323 *
2324 * if the following BUG_ON triggers, our validation request got
2325 * merged. we need separate requests for our algorithm to work.
2326 */
2327 BUG_ON(failrec->in_validation);
2328 failrec->in_validation = 1;
2329 failrec->this_mirror = failed_mirror;
2330 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2331 } else {
2332 /*
2333 * we're ready to fulfill a) and b) alongside. get a good copy
2334 * of the failed sector and if we succeed, we have setup
2335 * everything for repair_io_failure to do the rest for us.
2336 */
2337 if (failrec->in_validation) {
2338 BUG_ON(failrec->this_mirror != failed_mirror);
2339 failrec->in_validation = 0;
2340 failrec->this_mirror = 0;
2341 }
2342 failrec->failed_mirror = failed_mirror;
2343 failrec->this_mirror++;
2344 if (failrec->this_mirror == failed_mirror)
2345 failrec->this_mirror++;
2346 read_mode = READ_SYNC;
2347 }
2348
2349 if (!state || failrec->this_mirror > num_copies) {
2350 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2351 "next_mirror %d, failed_mirror %d\n", state,
2352 num_copies, failrec->this_mirror, failed_mirror);
2353 free_io_failure(inode, failrec, 0);
2354 return -EIO;
2355 }
2356
Chris Mason9be33952013-05-17 18:30:14 -04002357 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04002358 if (!bio) {
2359 free_io_failure(inode, failrec, 0);
2360 return -EIO;
2361 }
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002362 bio->bi_private = state;
2363 bio->bi_end_io = failed_bio->bi_end_io;
2364 bio->bi_sector = failrec->logical >> 9;
2365 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2366 bio->bi_size = 0;
2367
2368 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2369
2370 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2371 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2372 failrec->this_mirror, num_copies, failrec->in_validation);
2373
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002374 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2375 failrec->this_mirror,
2376 failrec->bio_flags, 0);
2377 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002378}
2379
Chris Masond1310b22008-01-24 16:13:08 -05002380/* lots and lots of room for performance fixes in the end_bio funcs */
2381
Jeff Mahoney87826df2012-02-15 16:23:57 +01002382int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2383{
2384 int uptodate = (err == 0);
2385 struct extent_io_tree *tree;
2386 int ret;
2387
2388 tree = &BTRFS_I(page->mapping->host)->io_tree;
2389
2390 if (tree->ops && tree->ops->writepage_end_io_hook) {
2391 ret = tree->ops->writepage_end_io_hook(page, start,
2392 end, NULL, uptodate);
2393 if (ret)
2394 uptodate = 0;
2395 }
2396
Jeff Mahoney87826df2012-02-15 16:23:57 +01002397 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002398 ClearPageUptodate(page);
2399 SetPageError(page);
2400 }
2401 return 0;
2402}
2403
Chris Masond1310b22008-01-24 16:13:08 -05002404/*
2405 * after a writepage IO is done, we need to:
2406 * clear the uptodate bits on error
2407 * clear the writeback bits in the extent tree for this IO
2408 * end_page_writeback if the page has no more pending IO
2409 *
2410 * Scheduling is not allowed, so the extent state tree is expected
2411 * to have one and only one object corresponding to this IO.
2412 */
Chris Masond1310b22008-01-24 16:13:08 -05002413static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002414{
Chris Masond1310b22008-01-24 16:13:08 -05002415 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04002416 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002417 u64 start;
2418 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05002419
Chris Masond1310b22008-01-24 16:13:08 -05002420 do {
2421 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04002422 tree = &BTRFS_I(page->mapping->host)->io_tree;
2423
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002424 /* We always issue full-page reads, but if some block
2425 * in a page fails to read, blk_update_request() will
2426 * advance bv_offset and adjust bv_len to compensate.
2427 * Print a warning for nonzero offsets, and an error
2428 * if they don't add up to a full page. */
2429 if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE)
2430 printk("%s page write in btrfs with offset %u and length %u\n",
2431 bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE
2432 ? KERN_ERR "partial" : KERN_INFO "incomplete",
2433 bvec->bv_offset, bvec->bv_len);
Chris Masond1310b22008-01-24 16:13:08 -05002434
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002435 start = page_offset(page);
2436 end = start + bvec->bv_offset + bvec->bv_len - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002437
2438 if (--bvec >= bio->bi_io_vec)
2439 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04002440
Jeff Mahoney87826df2012-02-15 16:23:57 +01002441 if (end_extent_writepage(page, err, start, end))
2442 continue;
Chris Mason70dec802008-01-29 09:59:12 -05002443
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002444 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002445 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04002446
Chris Masond1310b22008-01-24 16:13:08 -05002447 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002448}
2449
2450/*
2451 * after a readpage IO is done, we need to:
2452 * clear the uptodate bits on error
2453 * set the uptodate bits if things worked
2454 * set the page up to date if all extents in the tree are uptodate
2455 * clear the lock bit in the extent tree
2456 * unlock the page if there are no other extents locked for it
2457 *
2458 * Scheduling is not allowed, so the extent state tree is expected
2459 * to have one and only one object corresponding to this IO.
2460 */
Chris Masond1310b22008-01-24 16:13:08 -05002461static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002462{
2463 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00002464 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2465 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04002466 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002467 u64 start;
2468 u64 end;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002469 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002470 int ret;
2471
Chris Masond20f7042008-12-08 16:58:54 -05002472 if (err)
2473 uptodate = 0;
2474
Chris Masond1310b22008-01-24 16:13:08 -05002475 do {
2476 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00002477 struct extent_state *cached = NULL;
2478 struct extent_state *state;
Chris Mason9be33952013-05-17 18:30:14 -04002479 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Arne Jansen507903b2011-04-06 10:02:20 +00002480
Kent Overstreetbe3940c2012-09-11 14:23:05 -06002481 pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, "
Chris Mason9be33952013-05-17 18:30:14 -04002482 "mirror=%lu\n", (u64)bio->bi_sector, err,
2483 io_bio->mirror_num);
David Woodhouse902b22f2008-08-20 08:51:49 -04002484 tree = &BTRFS_I(page->mapping->host)->io_tree;
2485
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002486 /* We always issue full-page reads, but if some block
2487 * in a page fails to read, blk_update_request() will
2488 * advance bv_offset and adjust bv_len to compensate.
2489 * Print a warning for nonzero offsets, and an error
2490 * if they don't add up to a full page. */
2491 if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE)
2492 printk("%s page read in btrfs with offset %u and length %u\n",
2493 bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE
2494 ? KERN_ERR "partial" : KERN_INFO "incomplete",
2495 bvec->bv_offset, bvec->bv_len);
Chris Masond1310b22008-01-24 16:13:08 -05002496
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002497 start = page_offset(page);
2498 end = start + bvec->bv_offset + bvec->bv_len - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002499
Chris Mason4125bf72010-02-03 18:18:45 +00002500 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05002501 prefetchw(&bvec->bv_page->flags);
2502
Arne Jansen507903b2011-04-06 10:02:20 +00002503 spin_lock(&tree->lock);
Chris Mason0d399202011-04-16 06:55:39 -04002504 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
Chris Mason109b36a2011-04-12 13:57:39 -04002505 if (state && state->start == start) {
Arne Jansen507903b2011-04-06 10:02:20 +00002506 /*
2507 * take a reference on the state, unlock will drop
2508 * the ref
2509 */
2510 cache_state(state, &cached);
2511 }
2512 spin_unlock(&tree->lock);
2513
Chris Mason9be33952013-05-17 18:30:14 -04002514 mirror = io_bio->mirror_num;
Chris Masond1310b22008-01-24 16:13:08 -05002515 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05002516 ret = tree->ops->readpage_end_io_hook(page, start, end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002517 state, mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002518 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002519 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002520 else
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002521 clean_io_failure(start, page);
Chris Masond1310b22008-01-24 16:13:08 -05002522 }
Josef Bacikea466792012-03-26 21:57:36 -04002523
Josef Bacikea466792012-03-26 21:57:36 -04002524 if (!uptodate && tree->ops && tree->ops->readpage_io_failed_hook) {
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002525 ret = tree->ops->readpage_io_failed_hook(page, mirror);
Josef Bacikea466792012-03-26 21:57:36 -04002526 if (!ret && !err &&
2527 test_bit(BIO_UPTODATE, &bio->bi_flags))
2528 uptodate = 1;
2529 } else if (!uptodate) {
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002530 /*
2531 * The generic bio_readpage_error handles errors the
2532 * following way: If possible, new read requests are
2533 * created and submitted and will end up in
2534 * end_bio_extent_readpage as well (if we're lucky, not
2535 * in the !uptodate case). In that case it returns 0 and
2536 * we just go on with the next page in our bio. If it
2537 * can't handle the error it will return -EIO and we
2538 * remain responsible for that page.
2539 */
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002540 ret = bio_readpage_error(bio, page, start, end, mirror, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04002541 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04002542 uptodate =
2543 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05002544 if (err)
2545 uptodate = 0;
Arne Jansen507903b2011-04-06 10:02:20 +00002546 uncache_state(&cached);
Chris Mason7e383262008-04-09 16:28:12 -04002547 continue;
2548 }
2549 }
Chris Mason70dec802008-01-29 09:59:12 -05002550
Josef Bacik0b32f4b2012-03-13 09:38:00 -04002551 if (uptodate && tree->track_uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00002552 set_extent_uptodate(tree, start, end, &cached,
David Woodhouse902b22f2008-08-20 08:51:49 -04002553 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05002554 }
Arne Jansen507903b2011-04-06 10:02:20 +00002555 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05002556
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002557 if (uptodate) {
2558 SetPageUptodate(page);
Chris Mason70dec802008-01-29 09:59:12 -05002559 } else {
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002560 ClearPageUptodate(page);
2561 SetPageError(page);
Chris Mason70dec802008-01-29 09:59:12 -05002562 }
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002563 unlock_page(page);
Chris Mason4125bf72010-02-03 18:18:45 +00002564 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05002565
2566 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002567}
2568
Chris Mason9be33952013-05-17 18:30:14 -04002569/*
2570 * this allocates from the btrfs_bioset. We're returning a bio right now
2571 * but you can call btrfs_io_bio for the appropriate container_of magic
2572 */
Miao Xie88f794e2010-11-22 03:02:55 +00002573struct bio *
2574btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2575 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002576{
2577 struct bio *bio;
2578
Chris Mason9be33952013-05-17 18:30:14 -04002579 bio = bio_alloc_bioset(gfp_flags, nr_vecs, btrfs_bioset);
Chris Masond1310b22008-01-24 16:13:08 -05002580
2581 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
Chris Mason9be33952013-05-17 18:30:14 -04002582 while (!bio && (nr_vecs /= 2)) {
2583 bio = bio_alloc_bioset(gfp_flags,
2584 nr_vecs, btrfs_bioset);
2585 }
Chris Masond1310b22008-01-24 16:13:08 -05002586 }
2587
2588 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04002589 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002590 bio->bi_bdev = bdev;
2591 bio->bi_sector = first_sector;
2592 }
2593 return bio;
2594}
2595
Chris Mason9be33952013-05-17 18:30:14 -04002596struct bio *btrfs_bio_clone(struct bio *bio, gfp_t gfp_mask)
2597{
2598 return bio_clone_bioset(bio, gfp_mask, btrfs_bioset);
2599}
2600
2601
2602/* this also allocates from the btrfs_bioset */
2603struct bio *btrfs_io_bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs)
2604{
2605 return bio_alloc_bioset(gfp_mask, nr_iovecs, btrfs_bioset);
2606}
2607
2608
Jeff Mahoney355808c2011-10-03 23:23:14 -04002609static int __must_check submit_one_bio(int rw, struct bio *bio,
2610 int mirror_num, unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002611{
Chris Masond1310b22008-01-24 16:13:08 -05002612 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002613 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2614 struct page *page = bvec->bv_page;
2615 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002616 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002617
Miao Xie4eee4fa2012-12-21 09:17:45 +00002618 start = page_offset(page) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002619
David Woodhouse902b22f2008-08-20 08:51:49 -04002620 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002621
2622 bio_get(bio);
2623
Chris Mason065631f2008-02-20 12:07:25 -05002624 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00002625 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002626 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002627 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01002628 btrfsic_submit_bio(rw, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002629
Chris Masond1310b22008-01-24 16:13:08 -05002630 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2631 ret = -EOPNOTSUPP;
2632 bio_put(bio);
2633 return ret;
2634}
2635
David Woodhouse64a16702009-07-15 23:29:37 +01002636static int merge_bio(int rw, struct extent_io_tree *tree, struct page *page,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002637 unsigned long offset, size_t size, struct bio *bio,
2638 unsigned long bio_flags)
2639{
2640 int ret = 0;
2641 if (tree->ops && tree->ops->merge_bio_hook)
David Woodhouse64a16702009-07-15 23:29:37 +01002642 ret = tree->ops->merge_bio_hook(rw, page, offset, size, bio,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002643 bio_flags);
2644 BUG_ON(ret < 0);
2645 return ret;
2646
2647}
2648
Chris Masond1310b22008-01-24 16:13:08 -05002649static int submit_extent_page(int rw, struct extent_io_tree *tree,
2650 struct page *page, sector_t sector,
2651 size_t size, unsigned long offset,
2652 struct block_device *bdev,
2653 struct bio **bio_ret,
2654 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04002655 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002656 int mirror_num,
2657 unsigned long prev_bio_flags,
2658 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002659{
2660 int ret = 0;
2661 struct bio *bio;
2662 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04002663 int contig = 0;
2664 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2665 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05002666 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002667
2668 if (bio_ret && *bio_ret) {
2669 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002670 if (old_compressed)
2671 contig = bio->bi_sector == sector;
2672 else
2673 contig = bio->bi_sector + (bio->bi_size >> 9) ==
2674 sector;
2675
2676 if (prev_bio_flags != bio_flags || !contig ||
David Woodhouse64a16702009-07-15 23:29:37 +01002677 merge_bio(rw, tree, page, offset, page_size, bio, bio_flags) ||
Chris Masonc8b97812008-10-29 14:49:59 -04002678 bio_add_page(bio, page, page_size, offset) < page_size) {
2679 ret = submit_one_bio(rw, bio, mirror_num,
2680 prev_bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002681 if (ret < 0)
2682 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05002683 bio = NULL;
2684 } else {
2685 return 0;
2686 }
2687 }
Chris Masonc8b97812008-10-29 14:49:59 -04002688 if (this_compressed)
2689 nr = BIO_MAX_PAGES;
2690 else
2691 nr = bio_get_nr_vecs(bdev);
2692
Miao Xie88f794e2010-11-22 03:02:55 +00002693 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002694 if (!bio)
2695 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002696
Chris Masonc8b97812008-10-29 14:49:59 -04002697 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002698 bio->bi_end_io = end_io_func;
2699 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05002700
Chris Masond3977122009-01-05 21:25:51 -05002701 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002702 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002703 else
Chris Masonc8b97812008-10-29 14:49:59 -04002704 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002705
2706 return ret;
2707}
2708
Eric Sandeen48a3b632013-04-25 20:41:01 +00002709static void attach_extent_buffer_page(struct extent_buffer *eb,
2710 struct page *page)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002711{
2712 if (!PagePrivate(page)) {
2713 SetPagePrivate(page);
2714 page_cache_get(page);
2715 set_page_private(page, (unsigned long)eb);
2716 } else {
2717 WARN_ON(page->private != (unsigned long)eb);
2718 }
2719}
2720
Chris Masond1310b22008-01-24 16:13:08 -05002721void set_page_extent_mapped(struct page *page)
2722{
2723 if (!PagePrivate(page)) {
2724 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002725 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002726 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002727 }
2728}
2729
Chris Masond1310b22008-01-24 16:13:08 -05002730/*
2731 * basic readpage implementation. Locked extent state structs are inserted
2732 * into the tree that are removed when the IO is done (by the end_io
2733 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002734 * XXX JDM: This needs looking at to ensure proper page locking
Chris Masond1310b22008-01-24 16:13:08 -05002735 */
2736static int __extent_read_full_page(struct extent_io_tree *tree,
2737 struct page *page,
2738 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002739 struct bio **bio, int mirror_num,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04002740 unsigned long *bio_flags, int rw)
Chris Masond1310b22008-01-24 16:13:08 -05002741{
2742 struct inode *inode = page->mapping->host;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002743 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05002744 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2745 u64 end;
2746 u64 cur = start;
2747 u64 extent_offset;
2748 u64 last_byte = i_size_read(inode);
2749 u64 block_start;
2750 u64 cur_end;
2751 sector_t sector;
2752 struct extent_map *em;
2753 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002754 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05002755 int ret;
2756 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002757 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002758 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002759 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002760 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002761 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002762
2763 set_page_extent_mapped(page);
2764
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002765 if (!PageUptodate(page)) {
2766 if (cleancache_get_page(page) == 0) {
2767 BUG_ON(blocksize != PAGE_SIZE);
2768 goto out;
2769 }
2770 }
2771
Chris Masond1310b22008-01-24 16:13:08 -05002772 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002773 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002774 lock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002775 ordered = btrfs_lookup_ordered_extent(inode, start);
2776 if (!ordered)
2777 break;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002778 unlock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002779 btrfs_start_ordered_extent(inode, ordered, 1);
2780 btrfs_put_ordered_extent(ordered);
2781 }
Chris Masond1310b22008-01-24 16:13:08 -05002782
Chris Masonc8b97812008-10-29 14:49:59 -04002783 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2784 char *userpage;
2785 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2786
2787 if (zero_offset) {
2788 iosize = PAGE_CACHE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002789 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002790 memset(userpage + zero_offset, 0, iosize);
2791 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002792 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002793 }
2794 }
Chris Masond1310b22008-01-24 16:13:08 -05002795 while (cur <= end) {
Josef Bacikc8f2f242013-02-11 11:33:00 -05002796 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2797
Chris Masond1310b22008-01-24 16:13:08 -05002798 if (cur >= last_byte) {
2799 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002800 struct extent_state *cached = NULL;
2801
David Sterba306e16c2011-04-19 14:29:38 +02002802 iosize = PAGE_CACHE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002803 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002804 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002805 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002806 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002807 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002808 &cached, GFP_NOFS);
2809 unlock_extent_cached(tree, cur, cur + iosize - 1,
2810 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002811 break;
2812 }
David Sterba306e16c2011-04-19 14:29:38 +02002813 em = get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002814 end - cur + 1, 0);
David Sterbac7040052011-04-19 18:00:01 +02002815 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002816 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002817 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002818 break;
2819 }
Chris Masond1310b22008-01-24 16:13:08 -05002820 extent_offset = cur - em->start;
2821 BUG_ON(extent_map_end(em) <= cur);
2822 BUG_ON(end < cur);
2823
Li Zefan261507a02010-12-17 14:21:50 +08002824 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002825 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002826 extent_set_compress_type(&this_bio_flag,
2827 em->compress_type);
2828 }
Chris Masonc8b97812008-10-29 14:49:59 -04002829
Chris Masond1310b22008-01-24 16:13:08 -05002830 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2831 cur_end = min(extent_map_end(em) - 1, end);
Qu Wenruofda28322013-02-26 08:10:22 +00002832 iosize = ALIGN(iosize, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -04002833 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2834 disk_io_size = em->block_len;
2835 sector = em->block_start >> 9;
2836 } else {
2837 sector = (em->block_start + extent_offset) >> 9;
2838 disk_io_size = iosize;
2839 }
Chris Masond1310b22008-01-24 16:13:08 -05002840 bdev = em->bdev;
2841 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002842 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2843 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002844 free_extent_map(em);
2845 em = NULL;
2846
2847 /* we've found a hole, just zero and go on */
2848 if (block_start == EXTENT_MAP_HOLE) {
2849 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002850 struct extent_state *cached = NULL;
2851
Cong Wang7ac687d2011-11-25 23:14:28 +08002852 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002853 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002854 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002855 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002856
2857 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002858 &cached, GFP_NOFS);
2859 unlock_extent_cached(tree, cur, cur + iosize - 1,
2860 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002861 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002862 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002863 continue;
2864 }
2865 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002866 if (test_range_bit(tree, cur, cur_end,
2867 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002868 check_page_uptodate(tree, page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002869 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002870 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002871 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002872 continue;
2873 }
Chris Mason70dec802008-01-29 09:59:12 -05002874 /* we have an inline extent but it didn't get marked up
2875 * to date. Error out
2876 */
2877 if (block_start == EXTENT_MAP_INLINE) {
2878 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002879 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05002880 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002881 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05002882 continue;
2883 }
Chris Masond1310b22008-01-24 16:13:08 -05002884
Josef Bacikc8f2f242013-02-11 11:33:00 -05002885 pnr -= page->index;
Josef Bacikd4c7ca82013-04-19 19:49:09 -04002886 ret = submit_extent_page(rw, tree, page,
David Sterba306e16c2011-04-19 14:29:38 +02002887 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04002888 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002889 end_bio_extent_readpage, mirror_num,
2890 *bio_flags,
2891 this_bio_flag);
Josef Bacikc8f2f242013-02-11 11:33:00 -05002892 if (!ret) {
2893 nr++;
2894 *bio_flags = this_bio_flag;
2895 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002896 SetPageError(page);
Josef Bacikedd33c92012-10-05 16:40:32 -04002897 unlock_extent(tree, cur, cur + iosize - 1);
2898 }
Chris Masond1310b22008-01-24 16:13:08 -05002899 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002900 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002901 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002902out:
Chris Masond1310b22008-01-24 16:13:08 -05002903 if (!nr) {
2904 if (!PageError(page))
2905 SetPageUptodate(page);
2906 unlock_page(page);
2907 }
2908 return 0;
2909}
2910
2911int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002912 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05002913{
2914 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002915 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002916 int ret;
2917
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002918 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04002919 &bio_flags, READ);
Chris Masond1310b22008-01-24 16:13:08 -05002920 if (bio)
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002921 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002922 return ret;
2923}
Chris Masond1310b22008-01-24 16:13:08 -05002924
Chris Mason11c83492009-04-20 15:50:09 -04002925static noinline void update_nr_written(struct page *page,
2926 struct writeback_control *wbc,
2927 unsigned long nr_written)
2928{
2929 wbc->nr_to_write -= nr_written;
2930 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2931 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2932 page->mapping->writeback_index = page->index + nr_written;
2933}
2934
Chris Masond1310b22008-01-24 16:13:08 -05002935/*
2936 * the writepage semantics are similar to regular writepage. extent
2937 * records are inserted to lock ranges in the tree, and as dirty areas
2938 * are found, they are marked writeback. Then the lock bits are removed
2939 * and the end_io handler clears the writeback ranges
2940 */
2941static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2942 void *data)
2943{
2944 struct inode *inode = page->mapping->host;
2945 struct extent_page_data *epd = data;
2946 struct extent_io_tree *tree = epd->tree;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002947 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05002948 u64 delalloc_start;
2949 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2950 u64 end;
2951 u64 cur = start;
2952 u64 extent_offset;
2953 u64 last_byte = i_size_read(inode);
2954 u64 block_start;
2955 u64 iosize;
2956 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002957 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002958 struct extent_map *em;
2959 struct block_device *bdev;
2960 int ret;
2961 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002962 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002963 size_t blocksize;
2964 loff_t i_size = i_size_read(inode);
2965 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2966 u64 nr_delalloc;
2967 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002968 int page_started;
2969 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002970 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002971 unsigned long nr_written = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002972 bool fill_delalloc = true;
Chris Masond1310b22008-01-24 16:13:08 -05002973
Chris Masonffbd5172009-04-20 15:50:09 -04002974 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002975 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002976 else
2977 write_flags = WRITE;
2978
liubo1abe9b82011-03-24 11:18:59 +00002979 trace___extent_writepage(page, inode, wbc);
2980
Chris Masond1310b22008-01-24 16:13:08 -05002981 WARN_ON(!PageLocked(page));
Chris Masonbf0da8c2011-11-04 12:29:37 -04002982
2983 ClearPageError(page);
2984
Chris Mason7f3c74f2008-07-18 12:01:11 -04002985 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002986 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002987 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002988 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002989 unlock_page(page);
2990 return 0;
2991 }
2992
2993 if (page->index == end_index) {
2994 char *userpage;
2995
Cong Wang7ac687d2011-11-25 23:14:28 +08002996 userpage = kmap_atomic(page);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002997 memset(userpage + pg_offset, 0,
2998 PAGE_CACHE_SIZE - pg_offset);
Cong Wang7ac687d2011-11-25 23:14:28 +08002999 kunmap_atomic(userpage);
Chris Mason211c17f2008-05-15 09:13:45 -04003000 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003001 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003002 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003003
3004 set_page_extent_mapped(page);
3005
Josef Bacik9e487102011-08-01 12:08:18 -04003006 if (!tree->ops || !tree->ops->fill_delalloc)
3007 fill_delalloc = false;
3008
Chris Masond1310b22008-01-24 16:13:08 -05003009 delalloc_start = start;
3010 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003011 page_started = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04003012 if (!epd->extent_locked && fill_delalloc) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003013 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04003014 /*
3015 * make sure the wbc mapping index is at least updated
3016 * to this page.
3017 */
3018 update_nr_written(page, wbc, 0);
3019
Chris Masond3977122009-01-05 21:25:51 -05003020 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05003021 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04003022 page,
3023 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05003024 &delalloc_end,
3025 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05003026 if (nr_delalloc == 0) {
3027 delalloc_start = delalloc_end + 1;
3028 continue;
3029 }
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09003030 ret = tree->ops->fill_delalloc(inode, page,
3031 delalloc_start,
3032 delalloc_end,
3033 &page_started,
3034 &nr_written);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003035 /* File system has been set read-only */
3036 if (ret) {
3037 SetPageError(page);
3038 goto done;
3039 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003040 /*
3041 * delalloc_end is already one less than the total
3042 * length, so we don't subtract one from
3043 * PAGE_CACHE_SIZE
3044 */
3045 delalloc_to_write += (delalloc_end - delalloc_start +
3046 PAGE_CACHE_SIZE) >>
3047 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003048 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05003049 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003050 if (wbc->nr_to_write < delalloc_to_write) {
3051 int thresh = 8192;
3052
3053 if (delalloc_to_write < thresh * 2)
3054 thresh = delalloc_to_write;
3055 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3056 thresh);
3057 }
Chris Masonc8b97812008-10-29 14:49:59 -04003058
Chris Mason771ed682008-11-06 22:02:51 -05003059 /* did the fill delalloc function already unlock and start
3060 * the IO?
3061 */
3062 if (page_started) {
3063 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04003064 /*
3065 * we've unlocked the page, so we can't update
3066 * the mapping's writeback index, just update
3067 * nr_to_write.
3068 */
3069 wbc->nr_to_write -= nr_written;
3070 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05003071 }
Chris Masonc8b97812008-10-29 14:49:59 -04003072 }
Chris Mason247e7432008-07-17 12:53:51 -04003073 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04003074 ret = tree->ops->writepage_start_hook(page, start,
3075 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01003076 if (ret) {
3077 /* Fixup worker will requeue */
3078 if (ret == -EBUSY)
3079 wbc->pages_skipped++;
3080 else
3081 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04003082 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04003083 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05003084 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04003085 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04003086 }
3087 }
3088
Chris Mason11c83492009-04-20 15:50:09 -04003089 /*
3090 * we don't want to touch the inode after unlocking the page,
3091 * so we update the mapping writeback index now
3092 */
3093 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05003094
Chris Masond1310b22008-01-24 16:13:08 -05003095 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05003096 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003097 if (tree->ops && tree->ops->writepage_end_io_hook)
3098 tree->ops->writepage_end_io_hook(page, start,
3099 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003100 goto done;
3101 }
3102
Chris Masond1310b22008-01-24 16:13:08 -05003103 blocksize = inode->i_sb->s_blocksize;
3104
3105 while (cur <= end) {
3106 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003107 if (tree->ops && tree->ops->writepage_end_io_hook)
3108 tree->ops->writepage_end_io_hook(page, cur,
3109 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003110 break;
3111 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003112 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05003113 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02003114 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003115 SetPageError(page);
3116 break;
3117 }
3118
3119 extent_offset = cur - em->start;
3120 BUG_ON(extent_map_end(em) <= cur);
3121 BUG_ON(end < cur);
3122 iosize = min(extent_map_end(em) - cur, end - cur + 1);
Qu Wenruofda28322013-02-26 08:10:22 +00003123 iosize = ALIGN(iosize, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05003124 sector = (em->block_start + extent_offset) >> 9;
3125 bdev = em->bdev;
3126 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003127 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05003128 free_extent_map(em);
3129 em = NULL;
3130
Chris Masonc8b97812008-10-29 14:49:59 -04003131 /*
3132 * compressed and inline extents are written through other
3133 * paths in the FS
3134 */
3135 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003136 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003137 /*
3138 * end_io notification does not happen here for
3139 * compressed extents
3140 */
3141 if (!compressed && tree->ops &&
3142 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003143 tree->ops->writepage_end_io_hook(page, cur,
3144 cur + iosize - 1,
3145 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003146 else if (compressed) {
3147 /* we don't want to end_page_writeback on
3148 * a compressed extent. this happens
3149 * elsewhere
3150 */
3151 nr++;
3152 }
3153
3154 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003155 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003156 continue;
3157 }
Chris Masond1310b22008-01-24 16:13:08 -05003158 /* leave this out until we have a page_mkwrite call */
3159 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04003160 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05003161 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003162 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003163 continue;
3164 }
Chris Masonc8b97812008-10-29 14:49:59 -04003165
Chris Masond1310b22008-01-24 16:13:08 -05003166 if (tree->ops && tree->ops->writepage_io_hook) {
3167 ret = tree->ops->writepage_io_hook(page, cur,
3168 cur + iosize - 1);
3169 } else {
3170 ret = 0;
3171 }
Chris Mason1259ab72008-05-12 13:39:03 -04003172 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003173 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003174 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003175 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003176
Chris Masond1310b22008-01-24 16:13:08 -05003177 set_range_writeback(tree, cur, cur + iosize - 1);
3178 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05003179 printk(KERN_ERR "btrfs warning page %lu not "
3180 "writeback, cur %llu end %llu\n",
3181 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05003182 (unsigned long long)end);
3183 }
3184
Chris Masonffbd5172009-04-20 15:50:09 -04003185 ret = submit_extent_page(write_flags, tree, page,
3186 sector, iosize, pg_offset,
3187 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04003188 end_bio_extent_writepage,
3189 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003190 if (ret)
3191 SetPageError(page);
3192 }
3193 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003194 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003195 nr++;
3196 }
3197done:
3198 if (nr == 0) {
3199 /* make sure the mapping tag for page dirty gets cleared */
3200 set_page_writeback(page);
3201 end_page_writeback(page);
3202 }
Chris Masond1310b22008-01-24 16:13:08 -05003203 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05003204
Chris Mason11c83492009-04-20 15:50:09 -04003205done_unlocked:
3206
Chris Mason2c64c532009-09-02 15:04:12 -04003207 /* drop our reference on any cached states */
3208 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05003209 return 0;
3210}
3211
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003212static int eb_wait(void *word)
3213{
3214 io_schedule();
3215 return 0;
3216}
3217
Josef Bacikfd8b2b62013-04-24 16:41:19 -04003218void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003219{
3220 wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
3221 TASK_UNINTERRUPTIBLE);
3222}
3223
3224static int lock_extent_buffer_for_io(struct extent_buffer *eb,
3225 struct btrfs_fs_info *fs_info,
3226 struct extent_page_data *epd)
3227{
3228 unsigned long i, num_pages;
3229 int flush = 0;
3230 int ret = 0;
3231
3232 if (!btrfs_try_tree_write_lock(eb)) {
3233 flush = 1;
3234 flush_write_bio(epd);
3235 btrfs_tree_lock(eb);
3236 }
3237
3238 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3239 btrfs_tree_unlock(eb);
3240 if (!epd->sync_io)
3241 return 0;
3242 if (!flush) {
3243 flush_write_bio(epd);
3244 flush = 1;
3245 }
Chris Masona098d8e2012-03-21 12:09:56 -04003246 while (1) {
3247 wait_on_extent_buffer_writeback(eb);
3248 btrfs_tree_lock(eb);
3249 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3250 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003251 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003252 }
3253 }
3254
Josef Bacik51561ff2012-07-20 16:25:24 -04003255 /*
3256 * We need to do this to prevent races in people who check if the eb is
3257 * under IO since we can end up having no IO bits set for a short period
3258 * of time.
3259 */
3260 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003261 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3262 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003263 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003264 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Miao Xiee2d84522013-01-29 10:09:20 +00003265 __percpu_counter_add(&fs_info->dirty_metadata_bytes,
3266 -eb->len,
3267 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003268 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003269 } else {
3270 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003271 }
3272
3273 btrfs_tree_unlock(eb);
3274
3275 if (!ret)
3276 return ret;
3277
3278 num_pages = num_extent_pages(eb->start, eb->len);
3279 for (i = 0; i < num_pages; i++) {
3280 struct page *p = extent_buffer_page(eb, i);
3281
3282 if (!trylock_page(p)) {
3283 if (!flush) {
3284 flush_write_bio(epd);
3285 flush = 1;
3286 }
3287 lock_page(p);
3288 }
3289 }
3290
3291 return ret;
3292}
3293
3294static void end_extent_buffer_writeback(struct extent_buffer *eb)
3295{
3296 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3297 smp_mb__after_clear_bit();
3298 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3299}
3300
3301static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
3302{
3303 int uptodate = err == 0;
3304 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
3305 struct extent_buffer *eb;
3306 int done;
3307
3308 do {
3309 struct page *page = bvec->bv_page;
3310
3311 bvec--;
3312 eb = (struct extent_buffer *)page->private;
3313 BUG_ON(!eb);
3314 done = atomic_dec_and_test(&eb->io_pages);
3315
3316 if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
3317 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3318 ClearPageUptodate(page);
3319 SetPageError(page);
3320 }
3321
3322 end_page_writeback(page);
3323
3324 if (!done)
3325 continue;
3326
3327 end_extent_buffer_writeback(eb);
3328 } while (bvec >= bio->bi_io_vec);
3329
3330 bio_put(bio);
3331
3332}
3333
3334static int write_one_eb(struct extent_buffer *eb,
3335 struct btrfs_fs_info *fs_info,
3336 struct writeback_control *wbc,
3337 struct extent_page_data *epd)
3338{
3339 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
3340 u64 offset = eb->start;
3341 unsigned long i, num_pages;
Josef Bacikde0022b2012-09-25 14:25:58 -04003342 unsigned long bio_flags = 0;
Josef Bacikd4c7ca82013-04-19 19:49:09 -04003343 int rw = (epd->sync_io ? WRITE_SYNC : WRITE) | REQ_META;
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003344 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003345
3346 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3347 num_pages = num_extent_pages(eb->start, eb->len);
3348 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003349 if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
3350 bio_flags = EXTENT_BIO_TREE_LOG;
3351
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003352 for (i = 0; i < num_pages; i++) {
3353 struct page *p = extent_buffer_page(eb, i);
3354
3355 clear_page_dirty_for_io(p);
3356 set_page_writeback(p);
3357 ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
3358 PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
3359 -1, end_bio_extent_buffer_writepage,
Josef Bacikde0022b2012-09-25 14:25:58 -04003360 0, epd->bio_flags, bio_flags);
3361 epd->bio_flags = bio_flags;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003362 if (ret) {
3363 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3364 SetPageError(p);
3365 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3366 end_extent_buffer_writeback(eb);
3367 ret = -EIO;
3368 break;
3369 }
3370 offset += PAGE_CACHE_SIZE;
3371 update_nr_written(p, wbc, 1);
3372 unlock_page(p);
3373 }
3374
3375 if (unlikely(ret)) {
3376 for (; i < num_pages; i++) {
3377 struct page *p = extent_buffer_page(eb, i);
3378 unlock_page(p);
3379 }
3380 }
3381
3382 return ret;
3383}
3384
3385int btree_write_cache_pages(struct address_space *mapping,
3386 struct writeback_control *wbc)
3387{
3388 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3389 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3390 struct extent_buffer *eb, *prev_eb = NULL;
3391 struct extent_page_data epd = {
3392 .bio = NULL,
3393 .tree = tree,
3394 .extent_locked = 0,
3395 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003396 .bio_flags = 0,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003397 };
3398 int ret = 0;
3399 int done = 0;
3400 int nr_to_write_done = 0;
3401 struct pagevec pvec;
3402 int nr_pages;
3403 pgoff_t index;
3404 pgoff_t end; /* Inclusive */
3405 int scanned = 0;
3406 int tag;
3407
3408 pagevec_init(&pvec, 0);
3409 if (wbc->range_cyclic) {
3410 index = mapping->writeback_index; /* Start from prev offset */
3411 end = -1;
3412 } else {
3413 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3414 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3415 scanned = 1;
3416 }
3417 if (wbc->sync_mode == WB_SYNC_ALL)
3418 tag = PAGECACHE_TAG_TOWRITE;
3419 else
3420 tag = PAGECACHE_TAG_DIRTY;
3421retry:
3422 if (wbc->sync_mode == WB_SYNC_ALL)
3423 tag_pages_for_writeback(mapping, index, end);
3424 while (!done && !nr_to_write_done && (index <= end) &&
3425 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3426 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3427 unsigned i;
3428
3429 scanned = 1;
3430 for (i = 0; i < nr_pages; i++) {
3431 struct page *page = pvec.pages[i];
3432
3433 if (!PagePrivate(page))
3434 continue;
3435
3436 if (!wbc->range_cyclic && page->index > end) {
3437 done = 1;
3438 break;
3439 }
3440
Josef Bacikb5bae262012-09-14 13:43:01 -04003441 spin_lock(&mapping->private_lock);
3442 if (!PagePrivate(page)) {
3443 spin_unlock(&mapping->private_lock);
3444 continue;
3445 }
3446
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003447 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003448
3449 /*
3450 * Shouldn't happen and normally this would be a BUG_ON
3451 * but no sense in crashing the users box for something
3452 * we can survive anyway.
3453 */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003454 if (!eb) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003455 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003456 WARN_ON(1);
3457 continue;
3458 }
3459
Josef Bacikb5bae262012-09-14 13:43:01 -04003460 if (eb == prev_eb) {
3461 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003462 continue;
3463 }
3464
Josef Bacikb5bae262012-09-14 13:43:01 -04003465 ret = atomic_inc_not_zero(&eb->refs);
3466 spin_unlock(&mapping->private_lock);
3467 if (!ret)
3468 continue;
3469
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003470 prev_eb = eb;
3471 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3472 if (!ret) {
3473 free_extent_buffer(eb);
3474 continue;
3475 }
3476
3477 ret = write_one_eb(eb, fs_info, wbc, &epd);
3478 if (ret) {
3479 done = 1;
3480 free_extent_buffer(eb);
3481 break;
3482 }
3483 free_extent_buffer(eb);
3484
3485 /*
3486 * the filesystem may choose to bump up nr_to_write.
3487 * We have to make sure to honor the new nr_to_write
3488 * at any time
3489 */
3490 nr_to_write_done = wbc->nr_to_write <= 0;
3491 }
3492 pagevec_release(&pvec);
3493 cond_resched();
3494 }
3495 if (!scanned && !done) {
3496 /*
3497 * We hit the last page and there is more work to be done: wrap
3498 * back to the start of the file
3499 */
3500 scanned = 1;
3501 index = 0;
3502 goto retry;
3503 }
3504 flush_write_bio(&epd);
3505 return ret;
3506}
3507
Chris Masond1310b22008-01-24 16:13:08 -05003508/**
Chris Mason4bef0842008-09-08 11:18:08 -04003509 * 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 -05003510 * @mapping: address space structure to write
3511 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3512 * @writepage: function called for each page
3513 * @data: data passed to writepage function
3514 *
3515 * If a page is already under I/O, write_cache_pages() skips it, even
3516 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3517 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3518 * and msync() need to guarantee that all the data which was dirty at the time
3519 * the call was made get new I/O started against them. If wbc->sync_mode is
3520 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3521 * existing IO to complete.
3522 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003523static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04003524 struct address_space *mapping,
3525 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003526 writepage_t writepage, void *data,
3527 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05003528{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003529 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003530 int ret = 0;
3531 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003532 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003533 struct pagevec pvec;
3534 int nr_pages;
3535 pgoff_t index;
3536 pgoff_t end; /* Inclusive */
3537 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003538 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003539
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003540 /*
3541 * We have to hold onto the inode so that ordered extents can do their
3542 * work when the IO finishes. The alternative to this is failing to add
3543 * an ordered extent if the igrab() fails there and that is a huge pain
3544 * to deal with, so instead just hold onto the inode throughout the
3545 * writepages operation. If it fails here we are freeing up the inode
3546 * anyway and we'd rather not waste our time writing out stuff that is
3547 * going to be truncated anyway.
3548 */
3549 if (!igrab(inode))
3550 return 0;
3551
Chris Masond1310b22008-01-24 16:13:08 -05003552 pagevec_init(&pvec, 0);
3553 if (wbc->range_cyclic) {
3554 index = mapping->writeback_index; /* Start from prev offset */
3555 end = -1;
3556 } else {
3557 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3558 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003559 scanned = 1;
3560 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003561 if (wbc->sync_mode == WB_SYNC_ALL)
3562 tag = PAGECACHE_TAG_TOWRITE;
3563 else
3564 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003565retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003566 if (wbc->sync_mode == WB_SYNC_ALL)
3567 tag_pages_for_writeback(mapping, index, end);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003568 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003569 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3570 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003571 unsigned i;
3572
3573 scanned = 1;
3574 for (i = 0; i < nr_pages; i++) {
3575 struct page *page = pvec.pages[i];
3576
3577 /*
3578 * At this point we hold neither mapping->tree_lock nor
3579 * lock on the page itself: the page may be truncated or
3580 * invalidated (changing page->mapping to NULL), or even
3581 * swizzled back from swapper_space to tmpfs file
3582 * mapping
3583 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05003584 if (!trylock_page(page)) {
3585 flush_fn(data);
3586 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04003587 }
Chris Masond1310b22008-01-24 16:13:08 -05003588
3589 if (unlikely(page->mapping != mapping)) {
3590 unlock_page(page);
3591 continue;
3592 }
3593
3594 if (!wbc->range_cyclic && page->index > end) {
3595 done = 1;
3596 unlock_page(page);
3597 continue;
3598 }
3599
Chris Masond2c3f4f2008-11-19 12:44:22 -05003600 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003601 if (PageWriteback(page))
3602 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05003603 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003604 }
Chris Masond1310b22008-01-24 16:13:08 -05003605
3606 if (PageWriteback(page) ||
3607 !clear_page_dirty_for_io(page)) {
3608 unlock_page(page);
3609 continue;
3610 }
3611
3612 ret = (*writepage)(page, wbc, data);
3613
3614 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3615 unlock_page(page);
3616 ret = 0;
3617 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003618 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05003619 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003620
3621 /*
3622 * the filesystem may choose to bump up nr_to_write.
3623 * We have to make sure to honor the new nr_to_write
3624 * at any time
3625 */
3626 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05003627 }
3628 pagevec_release(&pvec);
3629 cond_resched();
3630 }
3631 if (!scanned && !done) {
3632 /*
3633 * We hit the last page and there is more work to be done: wrap
3634 * back to the start of the file
3635 */
3636 scanned = 1;
3637 index = 0;
3638 goto retry;
3639 }
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003640 btrfs_add_delayed_iput(inode);
Chris Masond1310b22008-01-24 16:13:08 -05003641 return ret;
3642}
Chris Masond1310b22008-01-24 16:13:08 -05003643
Chris Masonffbd5172009-04-20 15:50:09 -04003644static void flush_epd_write_bio(struct extent_page_data *epd)
3645{
3646 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04003647 int rw = WRITE;
3648 int ret;
3649
Chris Masonffbd5172009-04-20 15:50:09 -04003650 if (epd->sync_io)
Jeff Mahoney355808c2011-10-03 23:23:14 -04003651 rw = WRITE_SYNC;
3652
Josef Bacikde0022b2012-09-25 14:25:58 -04003653 ret = submit_one_bio(rw, epd->bio, 0, epd->bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003654 BUG_ON(ret < 0); /* -ENOMEM */
Chris Masonffbd5172009-04-20 15:50:09 -04003655 epd->bio = NULL;
3656 }
3657}
3658
Chris Masond2c3f4f2008-11-19 12:44:22 -05003659static noinline void flush_write_bio(void *data)
3660{
3661 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04003662 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003663}
3664
Chris Masond1310b22008-01-24 16:13:08 -05003665int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3666 get_extent_t *get_extent,
3667 struct writeback_control *wbc)
3668{
3669 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003670 struct extent_page_data epd = {
3671 .bio = NULL,
3672 .tree = tree,
3673 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003674 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003675 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003676 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05003677 };
Chris Masond1310b22008-01-24 16:13:08 -05003678
Chris Masond1310b22008-01-24 16:13:08 -05003679 ret = __extent_writepage(page, wbc, &epd);
3680
Chris Masonffbd5172009-04-20 15:50:09 -04003681 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003682 return ret;
3683}
Chris Masond1310b22008-01-24 16:13:08 -05003684
Chris Mason771ed682008-11-06 22:02:51 -05003685int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3686 u64 start, u64 end, get_extent_t *get_extent,
3687 int mode)
3688{
3689 int ret = 0;
3690 struct address_space *mapping = inode->i_mapping;
3691 struct page *page;
3692 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3693 PAGE_CACHE_SHIFT;
3694
3695 struct extent_page_data epd = {
3696 .bio = NULL,
3697 .tree = tree,
3698 .get_extent = get_extent,
3699 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04003700 .sync_io = mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003701 .bio_flags = 0,
Chris Mason771ed682008-11-06 22:02:51 -05003702 };
3703 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05003704 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05003705 .nr_to_write = nr_pages * 2,
3706 .range_start = start,
3707 .range_end = end + 1,
3708 };
3709
Chris Masond3977122009-01-05 21:25:51 -05003710 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05003711 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3712 if (clear_page_dirty_for_io(page))
3713 ret = __extent_writepage(page, &wbc_writepages, &epd);
3714 else {
3715 if (tree->ops && tree->ops->writepage_end_io_hook)
3716 tree->ops->writepage_end_io_hook(page, start,
3717 start + PAGE_CACHE_SIZE - 1,
3718 NULL, 1);
3719 unlock_page(page);
3720 }
3721 page_cache_release(page);
3722 start += PAGE_CACHE_SIZE;
3723 }
3724
Chris Masonffbd5172009-04-20 15:50:09 -04003725 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05003726 return ret;
3727}
Chris Masond1310b22008-01-24 16:13:08 -05003728
3729int extent_writepages(struct extent_io_tree *tree,
3730 struct address_space *mapping,
3731 get_extent_t *get_extent,
3732 struct writeback_control *wbc)
3733{
3734 int ret = 0;
3735 struct extent_page_data epd = {
3736 .bio = NULL,
3737 .tree = tree,
3738 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003739 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003740 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003741 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05003742 };
3743
Chris Mason4bef0842008-09-08 11:18:08 -04003744 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003745 __extent_writepage, &epd,
3746 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04003747 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003748 return ret;
3749}
Chris Masond1310b22008-01-24 16:13:08 -05003750
3751int extent_readpages(struct extent_io_tree *tree,
3752 struct address_space *mapping,
3753 struct list_head *pages, unsigned nr_pages,
3754 get_extent_t get_extent)
3755{
3756 struct bio *bio = NULL;
3757 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04003758 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06003759 struct page *pagepool[16];
3760 struct page *page;
3761 int i = 0;
3762 int nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003763
Chris Masond1310b22008-01-24 16:13:08 -05003764 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
Liu Bo67c96842012-07-20 21:43:09 -06003765 page = list_entry(pages->prev, struct page, lru);
Chris Masond1310b22008-01-24 16:13:08 -05003766
3767 prefetchw(&page->flags);
3768 list_del(&page->lru);
Liu Bo67c96842012-07-20 21:43:09 -06003769 if (add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04003770 page->index, GFP_NOFS)) {
Liu Bo67c96842012-07-20 21:43:09 -06003771 page_cache_release(page);
3772 continue;
Chris Masond1310b22008-01-24 16:13:08 -05003773 }
Liu Bo67c96842012-07-20 21:43:09 -06003774
3775 pagepool[nr++] = page;
3776 if (nr < ARRAY_SIZE(pagepool))
3777 continue;
3778 for (i = 0; i < nr; i++) {
3779 __extent_read_full_page(tree, pagepool[i], get_extent,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04003780 &bio, 0, &bio_flags, READ);
Liu Bo67c96842012-07-20 21:43:09 -06003781 page_cache_release(pagepool[i]);
3782 }
3783 nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003784 }
Liu Bo67c96842012-07-20 21:43:09 -06003785 for (i = 0; i < nr; i++) {
3786 __extent_read_full_page(tree, pagepool[i], get_extent,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04003787 &bio, 0, &bio_flags, READ);
Liu Bo67c96842012-07-20 21:43:09 -06003788 page_cache_release(pagepool[i]);
3789 }
3790
Chris Masond1310b22008-01-24 16:13:08 -05003791 BUG_ON(!list_empty(pages));
3792 if (bio)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003793 return submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003794 return 0;
3795}
Chris Masond1310b22008-01-24 16:13:08 -05003796
3797/*
3798 * basic invalidatepage code, this waits on any locked or writeback
3799 * ranges corresponding to the page, and then deletes any extent state
3800 * records from the tree
3801 */
3802int extent_invalidatepage(struct extent_io_tree *tree,
3803 struct page *page, unsigned long offset)
3804{
Josef Bacik2ac55d42010-02-03 19:33:23 +00003805 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003806 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05003807 u64 end = start + PAGE_CACHE_SIZE - 1;
3808 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3809
Qu Wenruofda28322013-02-26 08:10:22 +00003810 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05003811 if (start > end)
3812 return 0;
3813
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003814 lock_extent_bits(tree, start, end, 0, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04003815 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003816 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04003817 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3818 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003819 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003820 return 0;
3821}
Chris Masond1310b22008-01-24 16:13:08 -05003822
3823/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04003824 * a helper for releasepage, this tests for areas of the page that
3825 * are locked or under IO and drops the related state bits if it is safe
3826 * to drop the page.
3827 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003828static int try_release_extent_state(struct extent_map_tree *map,
3829 struct extent_io_tree *tree,
3830 struct page *page, gfp_t mask)
Chris Mason7b13b7b2008-04-18 10:29:50 -04003831{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003832 u64 start = page_offset(page);
Chris Mason7b13b7b2008-04-18 10:29:50 -04003833 u64 end = start + PAGE_CACHE_SIZE - 1;
3834 int ret = 1;
3835
Chris Mason211f90e2008-07-18 11:56:15 -04003836 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04003837 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04003838 ret = 0;
3839 else {
3840 if ((mask & GFP_NOFS) == GFP_NOFS)
3841 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04003842 /*
3843 * at this point we can safely clear everything except the
3844 * locked bit and the nodatasum bit
3845 */
Chris Masone3f24cc2011-02-14 12:52:08 -05003846 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04003847 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3848 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05003849
3850 /* if clear_extent_bit failed for enomem reasons,
3851 * we can't allow the release to continue.
3852 */
3853 if (ret < 0)
3854 ret = 0;
3855 else
3856 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003857 }
3858 return ret;
3859}
Chris Mason7b13b7b2008-04-18 10:29:50 -04003860
3861/*
Chris Masond1310b22008-01-24 16:13:08 -05003862 * a helper for releasepage. As long as there are no locked extents
3863 * in the range corresponding to the page, both state records and extent
3864 * map records are removed
3865 */
3866int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05003867 struct extent_io_tree *tree, struct page *page,
3868 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05003869{
3870 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003871 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05003872 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003873
Chris Mason70dec802008-01-29 09:59:12 -05003874 if ((mask & __GFP_WAIT) &&
3875 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05003876 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05003877 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05003878 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04003879 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05003880 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09003881 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04003882 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003883 break;
3884 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003885 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3886 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04003887 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003888 free_extent_map(em);
3889 break;
3890 }
3891 if (!test_range_bit(tree, em->start,
3892 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04003893 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04003894 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05003895 remove_extent_mapping(map, em);
3896 /* once for the rb tree */
3897 free_extent_map(em);
3898 }
3899 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04003900 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003901
3902 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05003903 free_extent_map(em);
3904 }
Chris Masond1310b22008-01-24 16:13:08 -05003905 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04003906 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003907}
Chris Masond1310b22008-01-24 16:13:08 -05003908
Chris Masonec29ed52011-02-23 16:23:20 -05003909/*
3910 * helper function for fiemap, which doesn't want to see any holes.
3911 * This maps until we find something past 'last'
3912 */
3913static struct extent_map *get_extent_skip_holes(struct inode *inode,
3914 u64 offset,
3915 u64 last,
3916 get_extent_t *get_extent)
3917{
3918 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3919 struct extent_map *em;
3920 u64 len;
3921
3922 if (offset >= last)
3923 return NULL;
3924
3925 while(1) {
3926 len = last - offset;
3927 if (len == 0)
3928 break;
Qu Wenruofda28322013-02-26 08:10:22 +00003929 len = ALIGN(len, sectorsize);
Chris Masonec29ed52011-02-23 16:23:20 -05003930 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02003931 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05003932 return em;
3933
3934 /* if this isn't a hole return it */
3935 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3936 em->block_start != EXTENT_MAP_HOLE) {
3937 return em;
3938 }
3939
3940 /* this is a hole, advance to the next extent */
3941 offset = extent_map_end(em);
3942 free_extent_map(em);
3943 if (offset >= last)
3944 break;
3945 }
3946 return NULL;
3947}
3948
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003949int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3950 __u64 start, __u64 len, get_extent_t *get_extent)
3951{
Josef Bacik975f84f2010-11-23 19:36:57 +00003952 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003953 u64 off = start;
3954 u64 max = start + len;
3955 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00003956 u32 found_type;
3957 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05003958 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003959 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003960 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00003961 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003962 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003963 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00003964 struct btrfs_path *path;
3965 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003966 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003967 u64 em_start = 0;
3968 u64 em_len = 0;
3969 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003970 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003971
3972 if (len == 0)
3973 return -EINVAL;
3974
Josef Bacik975f84f2010-11-23 19:36:57 +00003975 path = btrfs_alloc_path();
3976 if (!path)
3977 return -ENOMEM;
3978 path->leave_spinning = 1;
3979
Josef Bacik4d479cf2011-11-17 11:34:31 -05003980 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3981 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3982
Chris Masonec29ed52011-02-23 16:23:20 -05003983 /*
3984 * lookup the last file extent. We're not using i_size here
3985 * because there might be preallocation past i_size
3986 */
Josef Bacik975f84f2010-11-23 19:36:57 +00003987 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
Li Zefan33345d012011-04-20 10:31:50 +08003988 path, btrfs_ino(inode), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00003989 if (ret < 0) {
3990 btrfs_free_path(path);
3991 return ret;
3992 }
3993 WARN_ON(!ret);
3994 path->slots[0]--;
3995 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3996 struct btrfs_file_extent_item);
3997 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3998 found_type = btrfs_key_type(&found_key);
3999
Chris Masonec29ed52011-02-23 16:23:20 -05004000 /* No extents, but there might be delalloc bits */
Li Zefan33345d012011-04-20 10:31:50 +08004001 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00004002 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05004003 /* have to trust i_size as the end */
4004 last = (u64)-1;
4005 last_for_get_extent = isize;
4006 } else {
4007 /*
4008 * remember the start of the last extent. There are a
4009 * bunch of different factors that go into the length of the
4010 * extent, so its much less complex to remember where it started
4011 */
4012 last = found_key.offset;
4013 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00004014 }
Josef Bacik975f84f2010-11-23 19:36:57 +00004015 btrfs_free_path(path);
4016
Chris Masonec29ed52011-02-23 16:23:20 -05004017 /*
4018 * we might have some extents allocated but more delalloc past those
4019 * extents. so, we trust isize unless the start of the last extent is
4020 * beyond isize
4021 */
4022 if (last < isize) {
4023 last = (u64)-1;
4024 last_for_get_extent = isize;
4025 }
4026
Liu Boa52f4cd2013-05-01 16:23:41 +00004027 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004028 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05004029
Josef Bacik4d479cf2011-11-17 11:34:31 -05004030 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05004031 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004032 if (!em)
4033 goto out;
4034 if (IS_ERR(em)) {
4035 ret = PTR_ERR(em);
4036 goto out;
4037 }
Josef Bacik975f84f2010-11-23 19:36:57 +00004038
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004039 while (!end) {
Chris Masonea8efc72011-03-08 11:54:40 -05004040 u64 offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004041
Chris Masonea8efc72011-03-08 11:54:40 -05004042 /* break if the extent we found is outside the range */
4043 if (em->start >= max || extent_map_end(em) < off)
4044 break;
4045
4046 /*
4047 * get_extent may return an extent that starts before our
4048 * requested range. We have to make sure the ranges
4049 * we return to fiemap always move forward and don't
4050 * overlap, so adjust the offsets here
4051 */
4052 em_start = max(em->start, off);
4053
4054 /*
4055 * record the offset from the start of the extent
4056 * for adjusting the disk offset below
4057 */
4058 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05004059 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05004060 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05004061 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004062 disko = 0;
4063 flags = 0;
4064
Chris Masonea8efc72011-03-08 11:54:40 -05004065 /*
4066 * bump off for our next call to get_extent
4067 */
4068 off = extent_map_end(em);
4069 if (off >= max)
4070 end = 1;
4071
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004072 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004073 end = 1;
4074 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004075 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004076 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4077 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004078 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004079 flags |= (FIEMAP_EXTENT_DELALLOC |
4080 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004081 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05004082 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004083 }
4084 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4085 flags |= FIEMAP_EXTENT_ENCODED;
4086
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004087 free_extent_map(em);
4088 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05004089 if ((em_start >= last) || em_len == (u64)-1 ||
4090 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004091 flags |= FIEMAP_EXTENT_LAST;
4092 end = 1;
4093 }
4094
Chris Masonec29ed52011-02-23 16:23:20 -05004095 /* now scan forward to see if this is really the last extent. */
4096 em = get_extent_skip_holes(inode, off, last_for_get_extent,
4097 get_extent);
4098 if (IS_ERR(em)) {
4099 ret = PTR_ERR(em);
4100 goto out;
4101 }
4102 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00004103 flags |= FIEMAP_EXTENT_LAST;
4104 end = 1;
4105 }
Chris Masonec29ed52011-02-23 16:23:20 -05004106 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
4107 em_len, flags);
4108 if (ret)
4109 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004110 }
4111out_free:
4112 free_extent_map(em);
4113out:
Liu Boa52f4cd2013-05-01 16:23:41 +00004114 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004115 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004116 return ret;
4117}
4118
Chris Mason727011e2010-08-06 13:21:20 -04004119static void __free_extent_buffer(struct extent_buffer *eb)
4120{
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004121 btrfs_leak_debug_del(&eb->leak_list);
Chris Mason727011e2010-08-06 13:21:20 -04004122 kmem_cache_free(extent_buffer_cache, eb);
4123}
4124
Chris Masond1310b22008-01-24 16:13:08 -05004125static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
4126 u64 start,
4127 unsigned long len,
4128 gfp_t mask)
4129{
4130 struct extent_buffer *eb = NULL;
4131
Chris Masond1310b22008-01-24 16:13:08 -05004132 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00004133 if (eb == NULL)
4134 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004135 eb->start = start;
4136 eb->len = len;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004137 eb->tree = tree;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004138 eb->bflags = 0;
Chris Masonbd681512011-07-16 15:23:14 -04004139 rwlock_init(&eb->lock);
4140 atomic_set(&eb->write_locks, 0);
4141 atomic_set(&eb->read_locks, 0);
4142 atomic_set(&eb->blocking_readers, 0);
4143 atomic_set(&eb->blocking_writers, 0);
4144 atomic_set(&eb->spinning_readers, 0);
4145 atomic_set(&eb->spinning_writers, 0);
Arne Jansen5b25f702011-09-13 10:55:48 +02004146 eb->lock_nested = 0;
Chris Masonbd681512011-07-16 15:23:14 -04004147 init_waitqueue_head(&eb->write_lock_wq);
4148 init_waitqueue_head(&eb->read_lock_wq);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004149
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004150 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4151
Josef Bacik3083ee22012-03-09 16:01:49 -05004152 spin_lock_init(&eb->refs_lock);
Chris Masond1310b22008-01-24 16:13:08 -05004153 atomic_set(&eb->refs, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004154 atomic_set(&eb->io_pages, 0);
Chris Mason727011e2010-08-06 13:21:20 -04004155
David Sterbab8dae312013-02-28 14:54:18 +00004156 /*
4157 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4158 */
4159 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4160 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4161 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05004162
4163 return eb;
4164}
4165
Jan Schmidt815a51c2012-05-16 17:00:02 +02004166struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4167{
4168 unsigned long i;
4169 struct page *p;
4170 struct extent_buffer *new;
4171 unsigned long num_pages = num_extent_pages(src->start, src->len);
4172
4173 new = __alloc_extent_buffer(NULL, src->start, src->len, GFP_ATOMIC);
4174 if (new == NULL)
4175 return NULL;
4176
4177 for (i = 0; i < num_pages; i++) {
4178 p = alloc_page(GFP_ATOMIC);
4179 BUG_ON(!p);
4180 attach_extent_buffer_page(new, p);
4181 WARN_ON(PageDirty(p));
4182 SetPageUptodate(p);
4183 new->pages[i] = p;
4184 }
4185
4186 copy_extent_buffer(new, src, 0, 0, src->len);
4187 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
4188 set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
4189
4190 return new;
4191}
4192
4193struct extent_buffer *alloc_dummy_extent_buffer(u64 start, unsigned long len)
4194{
4195 struct extent_buffer *eb;
4196 unsigned long num_pages = num_extent_pages(0, len);
4197 unsigned long i;
4198
4199 eb = __alloc_extent_buffer(NULL, start, len, GFP_ATOMIC);
4200 if (!eb)
4201 return NULL;
4202
4203 for (i = 0; i < num_pages; i++) {
4204 eb->pages[i] = alloc_page(GFP_ATOMIC);
4205 if (!eb->pages[i])
4206 goto err;
4207 }
4208 set_extent_buffer_uptodate(eb);
4209 btrfs_set_header_nritems(eb, 0);
4210 set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4211
4212 return eb;
4213err:
Stefan Behrens84167d12012-10-11 07:25:16 -06004214 for (; i > 0; i--)
4215 __free_page(eb->pages[i - 1]);
Jan Schmidt815a51c2012-05-16 17:00:02 +02004216 __free_extent_buffer(eb);
4217 return NULL;
4218}
4219
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004220static int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004221{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004222 return (atomic_read(&eb->io_pages) ||
4223 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4224 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004225}
4226
Miao Xie897ca6e2010-10-26 20:57:29 -04004227/*
4228 * Helper for releasing extent buffer page.
4229 */
4230static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
4231 unsigned long start_idx)
4232{
4233 unsigned long index;
Wang Sheng-Hui39bab872012-04-06 14:35:31 +08004234 unsigned long num_pages;
Miao Xie897ca6e2010-10-26 20:57:29 -04004235 struct page *page;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004236 int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
Miao Xie897ca6e2010-10-26 20:57:29 -04004237
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004238 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e2010-10-26 20:57:29 -04004239
Wang Sheng-Hui39bab872012-04-06 14:35:31 +08004240 num_pages = num_extent_pages(eb->start, eb->len);
4241 index = start_idx + num_pages;
Miao Xie897ca6e2010-10-26 20:57:29 -04004242 if (start_idx >= index)
4243 return;
4244
4245 do {
4246 index--;
4247 page = extent_buffer_page(eb, index);
Jan Schmidt815a51c2012-05-16 17:00:02 +02004248 if (page && mapped) {
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004249 spin_lock(&page->mapping->private_lock);
4250 /*
4251 * We do this since we'll remove the pages after we've
4252 * removed the eb from the radix tree, so we could race
4253 * and have this page now attached to the new eb. So
4254 * only clear page_private if it's still connected to
4255 * this eb.
4256 */
4257 if (PagePrivate(page) &&
4258 page->private == (unsigned long)eb) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004259 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Josef Bacik3083ee22012-03-09 16:01:49 -05004260 BUG_ON(PageDirty(page));
4261 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004262 /*
4263 * We need to make sure we haven't be attached
4264 * to a new eb.
4265 */
4266 ClearPagePrivate(page);
4267 set_page_private(page, 0);
4268 /* One for the page private */
4269 page_cache_release(page);
4270 }
4271 spin_unlock(&page->mapping->private_lock);
4272
Jan Schmidt815a51c2012-05-16 17:00:02 +02004273 }
4274 if (page) {
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004275 /* One for when we alloced the page */
Miao Xie897ca6e2010-10-26 20:57:29 -04004276 page_cache_release(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004277 }
Miao Xie897ca6e2010-10-26 20:57:29 -04004278 } while (index != start_idx);
4279}
4280
4281/*
4282 * Helper for releasing the extent buffer.
4283 */
4284static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4285{
4286 btrfs_release_extent_buffer_page(eb, 0);
4287 __free_extent_buffer(eb);
4288}
4289
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004290static void check_buffer_tree_ref(struct extent_buffer *eb)
4291{
Chris Mason242e18c2013-01-29 17:49:37 -05004292 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004293 /* the ref bit is tricky. We have to make sure it is set
4294 * if we have the buffer dirty. Otherwise the
4295 * code to free a buffer can end up dropping a dirty
4296 * page
4297 *
4298 * Once the ref bit is set, it won't go away while the
4299 * buffer is dirty or in writeback, and it also won't
4300 * go away while we have the reference count on the
4301 * eb bumped.
4302 *
4303 * We can't just set the ref bit without bumping the
4304 * ref on the eb because free_extent_buffer might
4305 * see the ref bit and try to clear it. If this happens
4306 * free_extent_buffer might end up dropping our original
4307 * ref by mistake and freeing the page before we are able
4308 * to add one more ref.
4309 *
4310 * So bump the ref count first, then set the bit. If someone
4311 * beat us to it, drop the ref we added.
4312 */
Chris Mason242e18c2013-01-29 17:49:37 -05004313 refs = atomic_read(&eb->refs);
4314 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4315 return;
4316
Josef Bacik594831c2012-07-20 16:11:08 -04004317 spin_lock(&eb->refs_lock);
4318 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004319 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004320 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004321}
4322
Josef Bacik5df42352012-03-15 18:24:42 -04004323static void mark_extent_buffer_accessed(struct extent_buffer *eb)
4324{
4325 unsigned long num_pages, i;
4326
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004327 check_buffer_tree_ref(eb);
4328
Josef Bacik5df42352012-03-15 18:24:42 -04004329 num_pages = num_extent_pages(eb->start, eb->len);
4330 for (i = 0; i < num_pages; i++) {
4331 struct page *p = extent_buffer_page(eb, i);
4332 mark_page_accessed(p);
4333 }
4334}
4335
Chris Masond1310b22008-01-24 16:13:08 -05004336struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
Chris Mason727011e2010-08-06 13:21:20 -04004337 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004338{
4339 unsigned long num_pages = num_extent_pages(start, len);
4340 unsigned long i;
4341 unsigned long index = start >> PAGE_CACHE_SHIFT;
4342 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004343 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004344 struct page *p;
4345 struct address_space *mapping = tree->mapping;
4346 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004347 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004348
Miao Xie19fe0a82010-10-26 20:57:29 -04004349 rcu_read_lock();
4350 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4351 if (eb && atomic_inc_not_zero(&eb->refs)) {
4352 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004353 mark_extent_buffer_accessed(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04004354 return eb;
4355 }
Miao Xie19fe0a82010-10-26 20:57:29 -04004356 rcu_read_unlock();
Chris Mason6af118ce2008-07-22 11:18:07 -04004357
David Sterbaba144192011-04-21 01:12:06 +02004358 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
Peter2b114d12008-04-01 11:21:40 -04004359 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004360 return NULL;
4361
Chris Mason727011e2010-08-06 13:21:20 -04004362 for (i = 0; i < num_pages; i++, index++) {
Chris Masona6591712011-07-19 12:04:14 -04004363 p = find_or_create_page(mapping, index, GFP_NOFS);
Josef Bacik4804b382012-10-05 16:43:45 -04004364 if (!p)
Chris Mason6af118ce2008-07-22 11:18:07 -04004365 goto free_eb;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004366
4367 spin_lock(&mapping->private_lock);
4368 if (PagePrivate(p)) {
4369 /*
4370 * We could have already allocated an eb for this page
4371 * and attached one so lets see if we can get a ref on
4372 * the existing eb, and if we can we know it's good and
4373 * we can just return that one, else we know we can just
4374 * overwrite page->private.
4375 */
4376 exists = (struct extent_buffer *)p->private;
4377 if (atomic_inc_not_zero(&exists->refs)) {
4378 spin_unlock(&mapping->private_lock);
4379 unlock_page(p);
Josef Bacik17de39a2012-05-04 15:16:06 -04004380 page_cache_release(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004381 mark_extent_buffer_accessed(exists);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004382 goto free_eb;
4383 }
4384
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004385 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004386 * Do this so attach doesn't complain and we need to
4387 * drop the ref the old guy had.
4388 */
4389 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004390 WARN_ON(PageDirty(p));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004391 page_cache_release(p);
Chris Masond1310b22008-01-24 16:13:08 -05004392 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004393 attach_extent_buffer_page(eb, p);
4394 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004395 WARN_ON(PageDirty(p));
Chris Masond1310b22008-01-24 16:13:08 -05004396 mark_page_accessed(p);
Chris Mason727011e2010-08-06 13:21:20 -04004397 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05004398 if (!PageUptodate(p))
4399 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05004400
4401 /*
4402 * see below about how we avoid a nasty race with release page
4403 * and why we unlock later
4404 */
Chris Masond1310b22008-01-24 16:13:08 -05004405 }
4406 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004407 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05004408again:
Miao Xie19fe0a82010-10-26 20:57:29 -04004409 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4410 if (ret)
4411 goto free_eb;
4412
Chris Mason6af118ce2008-07-22 11:18:07 -04004413 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004414 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
4415 if (ret == -EEXIST) {
4416 exists = radix_tree_lookup(&tree->buffer,
4417 start >> PAGE_CACHE_SHIFT);
Josef Bacik115391d2012-03-09 09:51:43 -05004418 if (!atomic_inc_not_zero(&exists->refs)) {
4419 spin_unlock(&tree->buffer_lock);
4420 radix_tree_preload_end();
Josef Bacik115391d2012-03-09 09:51:43 -05004421 exists = NULL;
4422 goto again;
4423 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004424 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004425 radix_tree_preload_end();
Josef Bacik5df42352012-03-15 18:24:42 -04004426 mark_extent_buffer_accessed(exists);
Chris Mason6af118ce2008-07-22 11:18:07 -04004427 goto free_eb;
4428 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004429 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004430 check_buffer_tree_ref(eb);
Yan, Zhengf044ba72010-02-04 08:46:56 +00004431 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004432 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05004433
4434 /*
4435 * there is a race where release page may have
4436 * tried to find this extent buffer in the radix
4437 * but failed. It will tell the VM it is safe to
4438 * reclaim the, and it will clear the page private bit.
4439 * We must make sure to set the page private bit properly
4440 * after the extent buffer is in the radix tree so
4441 * it doesn't get lost
4442 */
Chris Mason727011e2010-08-06 13:21:20 -04004443 SetPageChecked(eb->pages[0]);
4444 for (i = 1; i < num_pages; i++) {
4445 p = extent_buffer_page(eb, i);
Chris Mason727011e2010-08-06 13:21:20 -04004446 ClearPageChecked(p);
4447 unlock_page(p);
4448 }
4449 unlock_page(eb->pages[0]);
Chris Masond1310b22008-01-24 16:13:08 -05004450 return eb;
4451
Chris Mason6af118ce2008-07-22 11:18:07 -04004452free_eb:
Chris Mason727011e2010-08-06 13:21:20 -04004453 for (i = 0; i < num_pages; i++) {
4454 if (eb->pages[i])
4455 unlock_page(eb->pages[i]);
4456 }
Chris Masoneb14ab82011-02-10 12:35:00 -05004457
Josef Bacik17de39a2012-05-04 15:16:06 -04004458 WARN_ON(!atomic_dec_and_test(&eb->refs));
Miao Xie897ca6e2010-10-26 20:57:29 -04004459 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04004460 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05004461}
Chris Masond1310b22008-01-24 16:13:08 -05004462
4463struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
David Sterbaf09d1f62011-04-21 01:08:01 +02004464 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004465{
Chris Masond1310b22008-01-24 16:13:08 -05004466 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05004467
Miao Xie19fe0a82010-10-26 20:57:29 -04004468 rcu_read_lock();
4469 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4470 if (eb && atomic_inc_not_zero(&eb->refs)) {
4471 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004472 mark_extent_buffer_accessed(eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04004473 return eb;
4474 }
4475 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04004476
Miao Xie19fe0a82010-10-26 20:57:29 -04004477 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004478}
Chris Masond1310b22008-01-24 16:13:08 -05004479
Josef Bacik3083ee22012-03-09 16:01:49 -05004480static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4481{
4482 struct extent_buffer *eb =
4483 container_of(head, struct extent_buffer, rcu_head);
4484
4485 __free_extent_buffer(eb);
4486}
4487
Josef Bacik3083ee22012-03-09 16:01:49 -05004488/* Expects to have eb->eb_lock already held */
David Sterbaf7a52a42013-04-26 14:56:29 +00004489static int release_extent_buffer(struct extent_buffer *eb)
Josef Bacik3083ee22012-03-09 16:01:49 -05004490{
4491 WARN_ON(atomic_read(&eb->refs) == 0);
4492 if (atomic_dec_and_test(&eb->refs)) {
Jan Schmidt815a51c2012-05-16 17:00:02 +02004493 if (test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) {
4494 spin_unlock(&eb->refs_lock);
4495 } else {
4496 struct extent_io_tree *tree = eb->tree;
Josef Bacik3083ee22012-03-09 16:01:49 -05004497
Jan Schmidt815a51c2012-05-16 17:00:02 +02004498 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05004499
Jan Schmidt815a51c2012-05-16 17:00:02 +02004500 spin_lock(&tree->buffer_lock);
4501 radix_tree_delete(&tree->buffer,
4502 eb->start >> PAGE_CACHE_SHIFT);
4503 spin_unlock(&tree->buffer_lock);
4504 }
Josef Bacik3083ee22012-03-09 16:01:49 -05004505
4506 /* Should be safe to release our pages at this point */
4507 btrfs_release_extent_buffer_page(eb, 0);
Josef Bacik3083ee22012-03-09 16:01:49 -05004508 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04004509 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05004510 }
4511 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04004512
4513 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05004514}
4515
Chris Masond1310b22008-01-24 16:13:08 -05004516void free_extent_buffer(struct extent_buffer *eb)
4517{
Chris Mason242e18c2013-01-29 17:49:37 -05004518 int refs;
4519 int old;
Chris Masond1310b22008-01-24 16:13:08 -05004520 if (!eb)
4521 return;
4522
Chris Mason242e18c2013-01-29 17:49:37 -05004523 while (1) {
4524 refs = atomic_read(&eb->refs);
4525 if (refs <= 3)
4526 break;
4527 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
4528 if (old == refs)
4529 return;
4530 }
4531
Josef Bacik3083ee22012-03-09 16:01:49 -05004532 spin_lock(&eb->refs_lock);
4533 if (atomic_read(&eb->refs) == 2 &&
Jan Schmidt815a51c2012-05-16 17:00:02 +02004534 test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
4535 atomic_dec(&eb->refs);
4536
4537 if (atomic_read(&eb->refs) == 2 &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004538 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004539 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004540 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4541 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05004542
Josef Bacik3083ee22012-03-09 16:01:49 -05004543 /*
4544 * I know this is terrible, but it's temporary until we stop tracking
4545 * the uptodate bits and such for the extent buffers.
4546 */
David Sterbaf7a52a42013-04-26 14:56:29 +00004547 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05004548}
Chris Masond1310b22008-01-24 16:13:08 -05004549
Josef Bacik3083ee22012-03-09 16:01:49 -05004550void free_extent_buffer_stale(struct extent_buffer *eb)
4551{
4552 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004553 return;
4554
Josef Bacik3083ee22012-03-09 16:01:49 -05004555 spin_lock(&eb->refs_lock);
4556 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
4557
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004558 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004559 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4560 atomic_dec(&eb->refs);
David Sterbaf7a52a42013-04-26 14:56:29 +00004561 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05004562}
4563
Chris Mason1d4284b2012-03-28 20:31:37 -04004564void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004565{
Chris Masond1310b22008-01-24 16:13:08 -05004566 unsigned long i;
4567 unsigned long num_pages;
4568 struct page *page;
4569
Chris Masond1310b22008-01-24 16:13:08 -05004570 num_pages = num_extent_pages(eb->start, eb->len);
4571
4572 for (i = 0; i < num_pages; i++) {
4573 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04004574 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05004575 continue;
4576
Chris Masona61e6f22008-07-22 11:18:08 -04004577 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05004578 WARN_ON(!PagePrivate(page));
4579
Chris Masond1310b22008-01-24 16:13:08 -05004580 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004581 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05004582 if (!PageDirty(page)) {
4583 radix_tree_tag_clear(&page->mapping->page_tree,
4584 page_index(page),
4585 PAGECACHE_TAG_DIRTY);
4586 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004587 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04004588 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04004589 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05004590 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004591 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05004592}
Chris Masond1310b22008-01-24 16:13:08 -05004593
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004594int set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004595{
4596 unsigned long i;
4597 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04004598 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004599
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004600 check_buffer_tree_ref(eb);
4601
Chris Masonb9473432009-03-13 11:00:37 -04004602 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004603
Chris Masond1310b22008-01-24 16:13:08 -05004604 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik3083ee22012-03-09 16:01:49 -05004605 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004606 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
4607
Chris Masonb9473432009-03-13 11:00:37 -04004608 for (i = 0; i < num_pages; i++)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004609 set_page_dirty(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04004610 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05004611}
Chris Masond1310b22008-01-24 16:13:08 -05004612
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004613int clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04004614{
4615 unsigned long i;
4616 struct page *page;
4617 unsigned long num_pages;
4618
Chris Masonb4ce94d2009-02-04 09:25:08 -05004619 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004620 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason1259ab72008-05-12 13:39:03 -04004621 for (i = 0; i < num_pages; i++) {
4622 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04004623 if (page)
4624 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04004625 }
4626 return 0;
4627}
4628
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004629int set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004630{
4631 unsigned long i;
4632 struct page *page;
4633 unsigned long num_pages;
4634
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004635 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004636 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05004637 for (i = 0; i < num_pages; i++) {
4638 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004639 SetPageUptodate(page);
4640 }
4641 return 0;
4642}
Chris Masond1310b22008-01-24 16:13:08 -05004643
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004644int extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004645{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004646 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004647}
Chris Masond1310b22008-01-24 16:13:08 -05004648
4649int read_extent_buffer_pages(struct extent_io_tree *tree,
Arne Jansenbb82ab82011-06-10 14:06:53 +02004650 struct extent_buffer *eb, u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04004651 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05004652{
4653 unsigned long i;
4654 unsigned long start_i;
4655 struct page *page;
4656 int err;
4657 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04004658 int locked_pages = 0;
4659 int all_uptodate = 1;
Chris Masond1310b22008-01-24 16:13:08 -05004660 unsigned long num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04004661 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004662 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004663 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004664
Chris Masonb4ce94d2009-02-04 09:25:08 -05004665 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05004666 return 0;
4667
Chris Masond1310b22008-01-24 16:13:08 -05004668 if (start) {
4669 WARN_ON(start < eb->start);
4670 start_i = (start >> PAGE_CACHE_SHIFT) -
4671 (eb->start >> PAGE_CACHE_SHIFT);
4672 } else {
4673 start_i = 0;
4674 }
4675
4676 num_pages = num_extent_pages(eb->start, eb->len);
4677 for (i = start_i; i < num_pages; i++) {
4678 page = extent_buffer_page(eb, i);
Arne Jansenbb82ab82011-06-10 14:06:53 +02004679 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04004680 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04004681 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05004682 } else {
4683 lock_page(page);
4684 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004685 locked_pages++;
Chris Mason727011e2010-08-06 13:21:20 -04004686 if (!PageUptodate(page)) {
4687 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04004688 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04004689 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004690 }
4691 if (all_uptodate) {
4692 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004693 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04004694 goto unlock_exit;
4695 }
4696
Josef Bacikea466792012-03-26 21:57:36 -04004697 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04004698 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004699 atomic_set(&eb->io_pages, num_reads);
Chris Masonce9adaa2008-04-09 16:28:12 -04004700 for (i = start_i; i < num_pages; i++) {
4701 page = extent_buffer_page(eb, i);
Chris Masonce9adaa2008-04-09 16:28:12 -04004702 if (!PageUptodate(page)) {
Chris Masonf1885912008-04-09 16:28:12 -04004703 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05004704 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04004705 get_extent, &bio,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04004706 mirror_num, &bio_flags,
4707 READ | REQ_META);
Chris Masond3977122009-01-05 21:25:51 -05004708 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05004709 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05004710 } else {
4711 unlock_page(page);
4712 }
4713 }
4714
Jeff Mahoney355808c2011-10-03 23:23:14 -04004715 if (bio) {
Josef Bacikd4c7ca82013-04-19 19:49:09 -04004716 err = submit_one_bio(READ | REQ_META, bio, mirror_num,
4717 bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004718 if (err)
4719 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04004720 }
Chris Masona86c12c2008-02-07 10:50:54 -05004721
Arne Jansenbb82ab82011-06-10 14:06:53 +02004722 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05004723 return ret;
Chris Masond3977122009-01-05 21:25:51 -05004724
Chris Masond1310b22008-01-24 16:13:08 -05004725 for (i = start_i; i < num_pages; i++) {
4726 page = extent_buffer_page(eb, i);
4727 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05004728 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05004729 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05004730 }
Chris Masond3977122009-01-05 21:25:51 -05004731
Chris Masond1310b22008-01-24 16:13:08 -05004732 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04004733
4734unlock_exit:
4735 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05004736 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004737 page = extent_buffer_page(eb, i);
4738 i++;
4739 unlock_page(page);
4740 locked_pages--;
4741 }
4742 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004743}
Chris Masond1310b22008-01-24 16:13:08 -05004744
4745void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4746 unsigned long start,
4747 unsigned long len)
4748{
4749 size_t cur;
4750 size_t offset;
4751 struct page *page;
4752 char *kaddr;
4753 char *dst = (char *)dstv;
4754 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4755 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004756
4757 WARN_ON(start > eb->len);
4758 WARN_ON(start + len > eb->start + eb->len);
4759
4760 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4761
Chris Masond3977122009-01-05 21:25:51 -05004762 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004763 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004764
4765 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04004766 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004767 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004768
4769 dst += cur;
4770 len -= cur;
4771 offset = 0;
4772 i++;
4773 }
4774}
Chris Masond1310b22008-01-24 16:13:08 -05004775
4776int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04004777 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05004778 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04004779 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05004780{
4781 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4782 char *kaddr;
4783 struct page *p;
4784 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4785 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4786 unsigned long end_i = (start_offset + start + min_len - 1) >>
4787 PAGE_CACHE_SHIFT;
4788
4789 if (i != end_i)
4790 return -EINVAL;
4791
4792 if (i == 0) {
4793 offset = start_offset;
4794 *map_start = 0;
4795 } else {
4796 offset = 0;
4797 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4798 }
Chris Masond3977122009-01-05 21:25:51 -05004799
Chris Masond1310b22008-01-24 16:13:08 -05004800 if (start + min_len > eb->len) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004801 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
Chris Masond3977122009-01-05 21:25:51 -05004802 "wanted %lu %lu\n", (unsigned long long)eb->start,
4803 eb->len, start, min_len);
Josef Bacik850265332011-03-15 14:52:12 -04004804 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05004805 }
4806
4807 p = extent_buffer_page(eb, i);
Chris Masona6591712011-07-19 12:04:14 -04004808 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05004809 *map = kaddr + offset;
4810 *map_len = PAGE_CACHE_SIZE - offset;
4811 return 0;
4812}
Chris Masond1310b22008-01-24 16:13:08 -05004813
Chris Masond1310b22008-01-24 16:13:08 -05004814int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4815 unsigned long start,
4816 unsigned long len)
4817{
4818 size_t cur;
4819 size_t offset;
4820 struct page *page;
4821 char *kaddr;
4822 char *ptr = (char *)ptrv;
4823 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4824 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4825 int ret = 0;
4826
4827 WARN_ON(start > eb->len);
4828 WARN_ON(start + len > eb->start + eb->len);
4829
4830 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4831
Chris Masond3977122009-01-05 21:25:51 -05004832 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004833 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004834
4835 cur = min(len, (PAGE_CACHE_SIZE - offset));
4836
Chris Masona6591712011-07-19 12:04:14 -04004837 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004838 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004839 if (ret)
4840 break;
4841
4842 ptr += cur;
4843 len -= cur;
4844 offset = 0;
4845 i++;
4846 }
4847 return ret;
4848}
Chris Masond1310b22008-01-24 16:13:08 -05004849
4850void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4851 unsigned long start, unsigned long len)
4852{
4853 size_t cur;
4854 size_t offset;
4855 struct page *page;
4856 char *kaddr;
4857 char *src = (char *)srcv;
4858 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4859 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4860
4861 WARN_ON(start > eb->len);
4862 WARN_ON(start + len > eb->start + eb->len);
4863
4864 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4865
Chris Masond3977122009-01-05 21:25:51 -05004866 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004867 page = extent_buffer_page(eb, i);
4868 WARN_ON(!PageUptodate(page));
4869
4870 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004871 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004872 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004873
4874 src += cur;
4875 len -= cur;
4876 offset = 0;
4877 i++;
4878 }
4879}
Chris Masond1310b22008-01-24 16:13:08 -05004880
4881void memset_extent_buffer(struct extent_buffer *eb, char c,
4882 unsigned long start, unsigned long len)
4883{
4884 size_t cur;
4885 size_t offset;
4886 struct page *page;
4887 char *kaddr;
4888 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4889 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4890
4891 WARN_ON(start > eb->len);
4892 WARN_ON(start + len > eb->start + eb->len);
4893
4894 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4895
Chris Masond3977122009-01-05 21:25:51 -05004896 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004897 page = extent_buffer_page(eb, i);
4898 WARN_ON(!PageUptodate(page));
4899
4900 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004901 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004902 memset(kaddr + offset, c, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004903
4904 len -= cur;
4905 offset = 0;
4906 i++;
4907 }
4908}
Chris Masond1310b22008-01-24 16:13:08 -05004909
4910void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4911 unsigned long dst_offset, unsigned long src_offset,
4912 unsigned long len)
4913{
4914 u64 dst_len = dst->len;
4915 size_t cur;
4916 size_t offset;
4917 struct page *page;
4918 char *kaddr;
4919 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4920 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4921
4922 WARN_ON(src->len != dst_len);
4923
4924 offset = (start_offset + dst_offset) &
4925 ((unsigned long)PAGE_CACHE_SIZE - 1);
4926
Chris Masond3977122009-01-05 21:25:51 -05004927 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004928 page = extent_buffer_page(dst, i);
4929 WARN_ON(!PageUptodate(page));
4930
4931 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4932
Chris Masona6591712011-07-19 12:04:14 -04004933 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004934 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004935
4936 src_offset += cur;
4937 len -= cur;
4938 offset = 0;
4939 i++;
4940 }
4941}
Chris Masond1310b22008-01-24 16:13:08 -05004942
4943static void move_pages(struct page *dst_page, struct page *src_page,
4944 unsigned long dst_off, unsigned long src_off,
4945 unsigned long len)
4946{
Chris Masona6591712011-07-19 12:04:14 -04004947 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004948 if (dst_page == src_page) {
4949 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4950 } else {
Chris Masona6591712011-07-19 12:04:14 -04004951 char *src_kaddr = page_address(src_page);
Chris Masond1310b22008-01-24 16:13:08 -05004952 char *p = dst_kaddr + dst_off + len;
4953 char *s = src_kaddr + src_off + len;
4954
4955 while (len--)
4956 *--p = *--s;
Chris Masond1310b22008-01-24 16:13:08 -05004957 }
Chris Masond1310b22008-01-24 16:13:08 -05004958}
4959
Sergei Trofimovich33872062011-04-11 21:52:52 +00004960static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4961{
4962 unsigned long distance = (src > dst) ? src - dst : dst - src;
4963 return distance < len;
4964}
4965
Chris Masond1310b22008-01-24 16:13:08 -05004966static void copy_pages(struct page *dst_page, struct page *src_page,
4967 unsigned long dst_off, unsigned long src_off,
4968 unsigned long len)
4969{
Chris Masona6591712011-07-19 12:04:14 -04004970 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004971 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004972 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004973
Sergei Trofimovich33872062011-04-11 21:52:52 +00004974 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04004975 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00004976 } else {
Chris Masond1310b22008-01-24 16:13:08 -05004977 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004978 if (areas_overlap(src_off, dst_off, len))
4979 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00004980 }
Chris Masond1310b22008-01-24 16:13:08 -05004981
Chris Mason727011e2010-08-06 13:21:20 -04004982 if (must_memmove)
4983 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
4984 else
4985 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05004986}
4987
4988void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4989 unsigned long src_offset, unsigned long len)
4990{
4991 size_t cur;
4992 size_t dst_off_in_page;
4993 size_t src_off_in_page;
4994 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4995 unsigned long dst_i;
4996 unsigned long src_i;
4997
4998 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004999 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
5000 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005001 BUG_ON(1);
5002 }
5003 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05005004 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
5005 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005006 BUG_ON(1);
5007 }
5008
Chris Masond3977122009-01-05 21:25:51 -05005009 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05005010 dst_off_in_page = (start_offset + dst_offset) &
5011 ((unsigned long)PAGE_CACHE_SIZE - 1);
5012 src_off_in_page = (start_offset + src_offset) &
5013 ((unsigned long)PAGE_CACHE_SIZE - 1);
5014
5015 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
5016 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
5017
5018 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
5019 src_off_in_page));
5020 cur = min_t(unsigned long, cur,
5021 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
5022
5023 copy_pages(extent_buffer_page(dst, dst_i),
5024 extent_buffer_page(dst, src_i),
5025 dst_off_in_page, src_off_in_page, cur);
5026
5027 src_offset += cur;
5028 dst_offset += cur;
5029 len -= cur;
5030 }
5031}
Chris Masond1310b22008-01-24 16:13:08 -05005032
5033void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5034 unsigned long src_offset, unsigned long len)
5035{
5036 size_t cur;
5037 size_t dst_off_in_page;
5038 size_t src_off_in_page;
5039 unsigned long dst_end = dst_offset + len - 1;
5040 unsigned long src_end = src_offset + len - 1;
5041 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
5042 unsigned long dst_i;
5043 unsigned long src_i;
5044
5045 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05005046 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
5047 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005048 BUG_ON(1);
5049 }
5050 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05005051 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
5052 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005053 BUG_ON(1);
5054 }
Chris Mason727011e2010-08-06 13:21:20 -04005055 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05005056 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
5057 return;
5058 }
Chris Masond3977122009-01-05 21:25:51 -05005059 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05005060 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
5061 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
5062
5063 dst_off_in_page = (start_offset + dst_end) &
5064 ((unsigned long)PAGE_CACHE_SIZE - 1);
5065 src_off_in_page = (start_offset + src_end) &
5066 ((unsigned long)PAGE_CACHE_SIZE - 1);
5067
5068 cur = min_t(unsigned long, len, src_off_in_page + 1);
5069 cur = min(cur, dst_off_in_page + 1);
5070 move_pages(extent_buffer_page(dst, dst_i),
5071 extent_buffer_page(dst, src_i),
5072 dst_off_in_page - cur + 1,
5073 src_off_in_page - cur + 1, cur);
5074
5075 dst_end -= cur;
5076 src_end -= cur;
5077 len -= cur;
5078 }
5079}
Chris Mason6af118ce2008-07-22 11:18:07 -04005080
David Sterbaf7a52a42013-04-26 14:56:29 +00005081int try_release_extent_buffer(struct page *page)
Miao Xie19fe0a82010-10-26 20:57:29 -04005082{
Chris Mason6af118ce2008-07-22 11:18:07 -04005083 struct extent_buffer *eb;
Miao Xie897ca6e2010-10-26 20:57:29 -04005084
Miao Xie19fe0a82010-10-26 20:57:29 -04005085 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005086 * We need to make sure noboody is attaching this page to an eb right
5087 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04005088 */
Josef Bacik3083ee22012-03-09 16:01:49 -05005089 spin_lock(&page->mapping->private_lock);
5090 if (!PagePrivate(page)) {
5091 spin_unlock(&page->mapping->private_lock);
5092 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005093 }
5094
Josef Bacik3083ee22012-03-09 16:01:49 -05005095 eb = (struct extent_buffer *)page->private;
5096 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04005097
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005098 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005099 * This is a little awful but should be ok, we need to make sure that
5100 * the eb doesn't disappear out from under us while we're looking at
5101 * this page.
5102 */
5103 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005104 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05005105 spin_unlock(&eb->refs_lock);
5106 spin_unlock(&page->mapping->private_lock);
5107 return 0;
5108 }
5109 spin_unlock(&page->mapping->private_lock);
5110
Josef Bacik3083ee22012-03-09 16:01:49 -05005111 /*
5112 * If tree ref isn't set then we know the ref on this eb is a real ref,
5113 * so just return, this page will likely be freed soon anyway.
5114 */
5115 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5116 spin_unlock(&eb->refs_lock);
5117 return 0;
5118 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005119
David Sterbaf7a52a42013-04-26 14:56:29 +00005120 return release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005121}