blob: 5d31f80c05622a6a5d98ce4b15f6b9358694b15d [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 Mason6af118c2008-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
Josef Bacik7ee9e442013-06-21 16:37:03 -0400546 if (bits & EXTENT_DELALLOC)
547 bits |= EXTENT_NORESERVE;
548
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400549 if (delete)
550 bits |= ~EXTENT_CTLBITS;
551 bits |= EXTENT_FIRST_DELALLOC;
552
Josef Bacik2ac55d42010-02-03 19:33:23 +0000553 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
554 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500555again:
556 if (!prealloc && (mask & __GFP_WAIT)) {
557 prealloc = alloc_extent_state(mask);
558 if (!prealloc)
559 return -ENOMEM;
560 }
561
Chris Masoncad321a2008-12-17 14:51:42 -0500562 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400563 if (cached_state) {
564 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000565
566 if (clear) {
567 *cached_state = NULL;
568 cached_state = NULL;
569 }
570
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400571 if (cached && cached->tree && cached->start <= start &&
572 cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000573 if (clear)
574 atomic_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400575 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400576 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400577 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000578 if (clear)
579 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400580 }
Chris Masond1310b22008-01-24 16:13:08 -0500581 /*
582 * this search will find the extents that end after
583 * our range starts
584 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500585 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500586 if (!node)
587 goto out;
588 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400589hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500590 if (state->start > end)
591 goto out;
592 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400593 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500594
Liu Bo04493142012-02-16 18:34:37 +0800595 /* the state doesn't have the wanted bits, go ahead */
Li Zefancdc6a392012-03-12 16:39:48 +0800596 if (!(state->state & bits)) {
597 state = next_state(state);
Liu Bo04493142012-02-16 18:34:37 +0800598 goto next;
Li Zefancdc6a392012-03-12 16:39:48 +0800599 }
Liu Bo04493142012-02-16 18:34:37 +0800600
Chris Masond1310b22008-01-24 16:13:08 -0500601 /*
602 * | ---- desired range ---- |
603 * | state | or
604 * | ------------- state -------------- |
605 *
606 * We need to split the extent we found, and may flip
607 * bits on second half.
608 *
609 * If the extent we found extends past our range, we
610 * just split and search again. It'll get split again
611 * the next time though.
612 *
613 * If the extent we found is inside our range, we clear
614 * the desired bit on it.
615 */
616
617 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000618 prealloc = alloc_extent_state_atomic(prealloc);
619 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500620 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400621 if (err)
622 extent_io_tree_panic(tree, err);
623
Chris Masond1310b22008-01-24 16:13:08 -0500624 prealloc = NULL;
625 if (err)
626 goto out;
627 if (state->end <= end) {
Liu Bod1ac6e42012-05-10 18:10:39 +0800628 state = clear_state_bit(tree, state, &bits, wake);
629 goto next;
Chris Masond1310b22008-01-24 16:13:08 -0500630 }
631 goto search_again;
632 }
633 /*
634 * | ---- desired range ---- |
635 * | state |
636 * We need to split the extent, and clear the bit
637 * on the first half
638 */
639 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000640 prealloc = alloc_extent_state_atomic(prealloc);
641 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500642 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400643 if (err)
644 extent_io_tree_panic(tree, err);
645
Chris Masond1310b22008-01-24 16:13:08 -0500646 if (wake)
647 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400648
Jeff Mahoney6763af82012-03-01 14:56:29 +0100649 clear_state_bit(tree, prealloc, &bits, wake);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400650
Chris Masond1310b22008-01-24 16:13:08 -0500651 prealloc = NULL;
652 goto out;
653 }
Chris Mason42daec22009-09-23 19:51:09 -0400654
Li Zefancdc6a392012-03-12 16:39:48 +0800655 state = clear_state_bit(tree, state, &bits, wake);
Liu Bo04493142012-02-16 18:34:37 +0800656next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400657 if (last_end == (u64)-1)
658 goto out;
659 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800660 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800661 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500662 goto search_again;
663
664out:
Chris Masoncad321a2008-12-17 14:51:42 -0500665 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500666 if (prealloc)
667 free_extent_state(prealloc);
668
Jeff Mahoney6763af82012-03-01 14:56:29 +0100669 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500670
671search_again:
672 if (start > end)
673 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500674 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500675 if (mask & __GFP_WAIT)
676 cond_resched();
677 goto again;
678}
Chris Masond1310b22008-01-24 16:13:08 -0500679
Jeff Mahoney143bede2012-03-01 14:56:26 +0100680static void wait_on_state(struct extent_io_tree *tree,
681 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500682 __releases(tree->lock)
683 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500684{
685 DEFINE_WAIT(wait);
686 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500687 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500688 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500689 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500690 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500691}
692
693/*
694 * waits for one or more bits to clear on a range in the state tree.
695 * The range [start, end] is inclusive.
696 * The tree lock is taken by this function
697 */
David Sterba41074882013-04-29 13:38:46 +0000698static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
699 unsigned long bits)
Chris Masond1310b22008-01-24 16:13:08 -0500700{
701 struct extent_state *state;
702 struct rb_node *node;
703
David Sterba8d599ae2013-04-30 15:22:23 +0000704 btrfs_debug_check_extent_io_range(tree->mapping->host, start, end);
705
Chris Masoncad321a2008-12-17 14:51:42 -0500706 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500707again:
708 while (1) {
709 /*
710 * this search will find all the extents that end after
711 * our range starts
712 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500713 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500714 if (!node)
715 break;
716
717 state = rb_entry(node, struct extent_state, rb_node);
718
719 if (state->start > end)
720 goto out;
721
722 if (state->state & bits) {
723 start = state->start;
724 atomic_inc(&state->refs);
725 wait_on_state(tree, state);
726 free_extent_state(state);
727 goto again;
728 }
729 start = state->end + 1;
730
731 if (start > end)
732 break;
733
Xiao Guangrongded91f02011-07-14 03:19:27 +0000734 cond_resched_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500735 }
736out:
Chris Masoncad321a2008-12-17 14:51:42 -0500737 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500738}
Chris Masond1310b22008-01-24 16:13:08 -0500739
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000740static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500741 struct extent_state *state,
David Sterba41074882013-04-29 13:38:46 +0000742 unsigned long *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500743{
David Sterba41074882013-04-29 13:38:46 +0000744 unsigned long bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400745
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000746 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400747 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500748 u64 range = state->end - state->start + 1;
749 tree->dirty_bytes += range;
750 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400751 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500752}
753
Chris Mason2c64c532009-09-02 15:04:12 -0400754static void cache_state(struct extent_state *state,
755 struct extent_state **cached_ptr)
756{
757 if (cached_ptr && !(*cached_ptr)) {
758 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
759 *cached_ptr = state;
760 atomic_inc(&state->refs);
761 }
762 }
763}
764
Arne Jansen507903b2011-04-06 10:02:20 +0000765static void uncache_state(struct extent_state **cached_ptr)
766{
767 if (cached_ptr && (*cached_ptr)) {
768 struct extent_state *state = *cached_ptr;
Chris Mason109b36a2011-04-12 13:57:39 -0400769 *cached_ptr = NULL;
770 free_extent_state(state);
Arne Jansen507903b2011-04-06 10:02:20 +0000771 }
772}
773
Chris Masond1310b22008-01-24 16:13:08 -0500774/*
Chris Mason1edbb732009-09-02 13:24:36 -0400775 * set some bits on a range in the tree. This may require allocations or
776 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500777 *
Chris Mason1edbb732009-09-02 13:24:36 -0400778 * If any of the exclusive bits are set, this will fail with -EEXIST if some
779 * part of the range already has the desired bits set. The start of the
780 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500781 *
Chris Mason1edbb732009-09-02 13:24:36 -0400782 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500783 */
Chris Mason1edbb732009-09-02 13:24:36 -0400784
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100785static int __must_check
786__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +0000787 unsigned long bits, unsigned long exclusive_bits,
788 u64 *failed_start, struct extent_state **cached_state,
789 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500790{
791 struct extent_state *state;
792 struct extent_state *prealloc = NULL;
793 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500794 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500795 u64 last_start;
796 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400797
David Sterba8d599ae2013-04-30 15:22:23 +0000798 btrfs_debug_check_extent_io_range(tree->mapping->host, start, end);
799
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400800 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500801again:
802 if (!prealloc && (mask & __GFP_WAIT)) {
803 prealloc = alloc_extent_state(mask);
Xiao Guangrong82337672011-04-20 06:44:57 +0000804 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500805 }
806
Chris Masoncad321a2008-12-17 14:51:42 -0500807 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400808 if (cached_state && *cached_state) {
809 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400810 if (state->start <= start && state->end > start &&
811 state->tree) {
Chris Mason9655d292009-09-02 15:22:30 -0400812 node = &state->rb_node;
813 goto hit_next;
814 }
815 }
Chris Masond1310b22008-01-24 16:13:08 -0500816 /*
817 * this search will find all the extents that end after
818 * our range starts.
819 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500820 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500821 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000822 prealloc = alloc_extent_state_atomic(prealloc);
823 BUG_ON(!prealloc);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400824 err = insert_state(tree, prealloc, start, end, &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400825 if (err)
826 extent_io_tree_panic(tree, err);
827
Chris Masond1310b22008-01-24 16:13:08 -0500828 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500829 goto out;
830 }
Chris Masond1310b22008-01-24 16:13:08 -0500831 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400832hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500833 last_start = state->start;
834 last_end = state->end;
835
836 /*
837 * | ---- desired range ---- |
838 * | state |
839 *
840 * Just lock what we found and keep going
841 */
842 if (state->start == start && state->end <= end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400843 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500844 *failed_start = state->start;
845 err = -EEXIST;
846 goto out;
847 }
Chris Mason42daec22009-09-23 19:51:09 -0400848
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000849 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400850 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500851 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400852 if (last_end == (u64)-1)
853 goto out;
854 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800855 state = next_state(state);
856 if (start < end && state && state->start == start &&
857 !need_resched())
858 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500859 goto search_again;
860 }
861
862 /*
863 * | ---- desired range ---- |
864 * | state |
865 * or
866 * | ------------- state -------------- |
867 *
868 * We need to split the extent we found, and may flip bits on
869 * second half.
870 *
871 * If the extent we found extends past our
872 * range, we just split and search again. It'll get split
873 * again the next time though.
874 *
875 * If the extent we found is inside our range, we set the
876 * desired bit on it.
877 */
878 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400879 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500880 *failed_start = start;
881 err = -EEXIST;
882 goto out;
883 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000884
885 prealloc = alloc_extent_state_atomic(prealloc);
886 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500887 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400888 if (err)
889 extent_io_tree_panic(tree, err);
890
Chris Masond1310b22008-01-24 16:13:08 -0500891 prealloc = NULL;
892 if (err)
893 goto out;
894 if (state->end <= end) {
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000895 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400896 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500897 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400898 if (last_end == (u64)-1)
899 goto out;
900 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800901 state = next_state(state);
902 if (start < end && state && state->start == start &&
903 !need_resched())
904 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500905 }
906 goto search_again;
907 }
908 /*
909 * | ---- desired range ---- |
910 * | state | or | state |
911 *
912 * There's a hole, we need to insert something in it and
913 * ignore the extent we found.
914 */
915 if (state->start > start) {
916 u64 this_end;
917 if (end < last_start)
918 this_end = end;
919 else
Chris Masond3977122009-01-05 21:25:51 -0500920 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000921
922 prealloc = alloc_extent_state_atomic(prealloc);
923 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000924
925 /*
926 * Avoid to free 'prealloc' if it can be merged with
927 * the later extent.
928 */
Chris Masond1310b22008-01-24 16:13:08 -0500929 err = insert_state(tree, prealloc, start, this_end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400930 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400931 if (err)
932 extent_io_tree_panic(tree, err);
933
Chris Mason2c64c532009-09-02 15:04:12 -0400934 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500935 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500936 start = this_end + 1;
937 goto search_again;
938 }
939 /*
940 * | ---- desired range ---- |
941 * | state |
942 * We need to split the extent, and set the bit
943 * on the first half
944 */
945 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400946 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500947 *failed_start = start;
948 err = -EEXIST;
949 goto out;
950 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000951
952 prealloc = alloc_extent_state_atomic(prealloc);
953 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500954 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400955 if (err)
956 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -0500957
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000958 set_state_bits(tree, prealloc, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400959 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500960 merge_state(tree, prealloc);
961 prealloc = NULL;
962 goto out;
963 }
964
965 goto search_again;
966
967out:
Chris Masoncad321a2008-12-17 14:51:42 -0500968 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500969 if (prealloc)
970 free_extent_state(prealloc);
971
972 return err;
973
974search_again:
975 if (start > end)
976 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500977 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500978 if (mask & __GFP_WAIT)
979 cond_resched();
980 goto again;
981}
Chris Masond1310b22008-01-24 16:13:08 -0500982
David Sterba41074882013-04-29 13:38:46 +0000983int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
984 unsigned long bits, u64 * failed_start,
985 struct extent_state **cached_state, gfp_t mask)
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100986{
987 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
988 cached_state, mask);
989}
990
991
Josef Bacik462d6fa2011-09-26 13:56:12 -0400992/**
Liu Bo10983f22012-07-11 15:26:19 +0800993 * convert_extent_bit - convert all bits in a given range from one bit to
994 * another
Josef Bacik462d6fa2011-09-26 13:56:12 -0400995 * @tree: the io tree to search
996 * @start: the start offset in bytes
997 * @end: the end offset in bytes (inclusive)
998 * @bits: the bits to set in this range
999 * @clear_bits: the bits to clear in this range
Josef Bacike6138872012-09-27 17:07:30 -04001000 * @cached_state: state that we're going to cache
Josef Bacik462d6fa2011-09-26 13:56:12 -04001001 * @mask: the allocation mask
1002 *
1003 * This will go through and set bits for the given range. If any states exist
1004 * already in this range they are set with the given bit and cleared of the
1005 * clear_bits. This is only meant to be used by things that are mergeable, ie
1006 * converting from say DELALLOC to DIRTY. This is not meant to be used with
1007 * boundary bits like LOCK.
1008 */
1009int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +00001010 unsigned long bits, unsigned long clear_bits,
Josef Bacike6138872012-09-27 17:07:30 -04001011 struct extent_state **cached_state, gfp_t mask)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001012{
1013 struct extent_state *state;
1014 struct extent_state *prealloc = NULL;
1015 struct rb_node *node;
1016 int err = 0;
1017 u64 last_start;
1018 u64 last_end;
1019
David Sterba8d599ae2013-04-30 15:22:23 +00001020 btrfs_debug_check_extent_io_range(tree->mapping->host, start, end);
1021
Josef Bacik462d6fa2011-09-26 13:56:12 -04001022again:
1023 if (!prealloc && (mask & __GFP_WAIT)) {
1024 prealloc = alloc_extent_state(mask);
1025 if (!prealloc)
1026 return -ENOMEM;
1027 }
1028
1029 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001030 if (cached_state && *cached_state) {
1031 state = *cached_state;
1032 if (state->start <= start && state->end > start &&
1033 state->tree) {
1034 node = &state->rb_node;
1035 goto hit_next;
1036 }
1037 }
1038
Josef Bacik462d6fa2011-09-26 13:56:12 -04001039 /*
1040 * this search will find all the extents that end after
1041 * our range starts.
1042 */
1043 node = tree_search(tree, start);
1044 if (!node) {
1045 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001046 if (!prealloc) {
1047 err = -ENOMEM;
1048 goto out;
1049 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001050 err = insert_state(tree, prealloc, start, end, &bits);
1051 prealloc = NULL;
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001052 if (err)
1053 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001054 goto out;
1055 }
1056 state = rb_entry(node, struct extent_state, rb_node);
1057hit_next:
1058 last_start = state->start;
1059 last_end = state->end;
1060
1061 /*
1062 * | ---- desired range ---- |
1063 * | state |
1064 *
1065 * Just lock what we found and keep going
1066 */
1067 if (state->start == start && state->end <= end) {
Josef Bacik462d6fa2011-09-26 13:56:12 -04001068 set_state_bits(tree, state, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001069 cache_state(state, cached_state);
Liu Bod1ac6e42012-05-10 18:10:39 +08001070 state = clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001071 if (last_end == (u64)-1)
1072 goto out;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001073 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001074 if (start < end && state && state->start == start &&
1075 !need_resched())
1076 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001077 goto search_again;
1078 }
1079
1080 /*
1081 * | ---- desired range ---- |
1082 * | state |
1083 * or
1084 * | ------------- state -------------- |
1085 *
1086 * We need to split the extent we found, and may flip bits on
1087 * second half.
1088 *
1089 * If the extent we found extends past our
1090 * range, we just split and search again. It'll get split
1091 * again the next time though.
1092 *
1093 * If the extent we found is inside our range, we set the
1094 * desired bit on it.
1095 */
1096 if (state->start < start) {
1097 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001098 if (!prealloc) {
1099 err = -ENOMEM;
1100 goto out;
1101 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001102 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001103 if (err)
1104 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001105 prealloc = NULL;
1106 if (err)
1107 goto out;
1108 if (state->end <= end) {
1109 set_state_bits(tree, state, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001110 cache_state(state, cached_state);
Liu Bod1ac6e42012-05-10 18:10:39 +08001111 state = clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001112 if (last_end == (u64)-1)
1113 goto out;
1114 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001115 if (start < end && state && state->start == start &&
1116 !need_resched())
1117 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001118 }
1119 goto search_again;
1120 }
1121 /*
1122 * | ---- desired range ---- |
1123 * | state | or | state |
1124 *
1125 * There's a hole, we need to insert something in it and
1126 * ignore the extent we found.
1127 */
1128 if (state->start > start) {
1129 u64 this_end;
1130 if (end < last_start)
1131 this_end = end;
1132 else
1133 this_end = last_start - 1;
1134
1135 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001136 if (!prealloc) {
1137 err = -ENOMEM;
1138 goto out;
1139 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001140
1141 /*
1142 * Avoid to free 'prealloc' if it can be merged with
1143 * the later extent.
1144 */
1145 err = insert_state(tree, prealloc, start, this_end,
1146 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001147 if (err)
1148 extent_io_tree_panic(tree, err);
Josef Bacike6138872012-09-27 17:07:30 -04001149 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001150 prealloc = NULL;
1151 start = this_end + 1;
1152 goto search_again;
1153 }
1154 /*
1155 * | ---- desired range ---- |
1156 * | state |
1157 * We need to split the extent, and set the bit
1158 * on the first half
1159 */
1160 if (state->start <= end && state->end > end) {
1161 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001162 if (!prealloc) {
1163 err = -ENOMEM;
1164 goto out;
1165 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001166
1167 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001168 if (err)
1169 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001170
1171 set_state_bits(tree, prealloc, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001172 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001173 clear_state_bit(tree, prealloc, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001174 prealloc = NULL;
1175 goto out;
1176 }
1177
1178 goto search_again;
1179
1180out:
1181 spin_unlock(&tree->lock);
1182 if (prealloc)
1183 free_extent_state(prealloc);
1184
1185 return err;
1186
1187search_again:
1188 if (start > end)
1189 goto out;
1190 spin_unlock(&tree->lock);
1191 if (mask & __GFP_WAIT)
1192 cond_resched();
1193 goto again;
1194}
1195
Chris Masond1310b22008-01-24 16:13:08 -05001196/* wrappers around set/clear extent bit */
1197int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1198 gfp_t mask)
1199{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001200 return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001201 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001202}
Chris Masond1310b22008-01-24 16:13:08 -05001203
1204int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +00001205 unsigned long bits, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001206{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001207 return set_extent_bit(tree, start, end, bits, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001208 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001209}
Chris Masond1310b22008-01-24 16:13:08 -05001210
1211int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +00001212 unsigned long bits, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001213{
Chris Mason2c64c532009-09-02 15:04:12 -04001214 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001215}
Chris Masond1310b22008-01-24 16:13:08 -05001216
1217int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001218 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001219{
1220 return set_extent_bit(tree, start, end,
Liu Bofee187d2011-09-29 15:55:28 +08001221 EXTENT_DELALLOC | EXTENT_UPTODATE,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001222 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001223}
Chris Masond1310b22008-01-24 16:13:08 -05001224
Liu Bo9e8a4a82012-09-05 19:10:51 -06001225int set_extent_defrag(struct extent_io_tree *tree, u64 start, u64 end,
1226 struct extent_state **cached_state, gfp_t mask)
1227{
1228 return set_extent_bit(tree, start, end,
1229 EXTENT_DELALLOC | EXTENT_UPTODATE | EXTENT_DEFRAG,
1230 NULL, cached_state, mask);
1231}
1232
Chris Masond1310b22008-01-24 16:13:08 -05001233int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1234 gfp_t mask)
1235{
1236 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04001237 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001238 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001239}
Chris Masond1310b22008-01-24 16:13:08 -05001240
1241int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1242 gfp_t mask)
1243{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001244 return set_extent_bit(tree, start, end, EXTENT_NEW, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001245 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001246}
Chris Masond1310b22008-01-24 16:13:08 -05001247
Chris Masond1310b22008-01-24 16:13:08 -05001248int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +00001249 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001250{
Liu Bo6b67a322013-03-28 08:30:28 +00001251 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, NULL,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001252 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001253}
Chris Masond1310b22008-01-24 16:13:08 -05001254
Josef Bacik5fd02042012-05-02 14:00:54 -04001255int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1256 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001257{
Chris Mason2c64c532009-09-02 15:04:12 -04001258 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001259 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001260}
Chris Masond1310b22008-01-24 16:13:08 -05001261
Chris Masond352ac62008-09-29 15:18:18 -04001262/*
1263 * either insert or lock state struct between start and end use mask to tell
1264 * us if waiting is desired.
1265 */
Chris Mason1edbb732009-09-02 13:24:36 -04001266int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +00001267 unsigned long bits, struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001268{
1269 int err;
1270 u64 failed_start;
1271 while (1) {
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001272 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
1273 EXTENT_LOCKED, &failed_start,
1274 cached_state, GFP_NOFS);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001275 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001276 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1277 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001278 } else
Chris Masond1310b22008-01-24 16:13:08 -05001279 break;
Chris Masond1310b22008-01-24 16:13:08 -05001280 WARN_ON(start > end);
1281 }
1282 return err;
1283}
Chris Masond1310b22008-01-24 16:13:08 -05001284
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001285int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Mason1edbb732009-09-02 13:24:36 -04001286{
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001287 return lock_extent_bits(tree, start, end, 0, NULL);
Chris Mason1edbb732009-09-02 13:24:36 -04001288}
1289
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001290int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001291{
1292 int err;
1293 u64 failed_start;
1294
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001295 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1296 &failed_start, NULL, GFP_NOFS);
Yan Zheng66435582008-10-30 14:19:50 -04001297 if (err == -EEXIST) {
1298 if (failed_start > start)
1299 clear_extent_bit(tree, start, failed_start - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001300 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
Josef Bacik25179202008-10-29 14:49:05 -04001301 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001302 }
Josef Bacik25179202008-10-29 14:49:05 -04001303 return 1;
1304}
Josef Bacik25179202008-10-29 14:49:05 -04001305
Chris Mason2c64c532009-09-02 15:04:12 -04001306int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1307 struct extent_state **cached, gfp_t mask)
1308{
1309 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1310 mask);
1311}
1312
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001313int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001314{
Chris Mason2c64c532009-09-02 15:04:12 -04001315 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001316 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001317}
Chris Masond1310b22008-01-24 16:13:08 -05001318
Chris Mason4adaa612013-03-26 13:07:00 -04001319int extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
1320{
1321 unsigned long index = start >> PAGE_CACHE_SHIFT;
1322 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1323 struct page *page;
1324
1325 while (index <= end_index) {
1326 page = find_get_page(inode->i_mapping, index);
1327 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1328 clear_page_dirty_for_io(page);
1329 page_cache_release(page);
1330 index++;
1331 }
1332 return 0;
1333}
1334
1335int extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
1336{
1337 unsigned long index = start >> PAGE_CACHE_SHIFT;
1338 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1339 struct page *page;
1340
1341 while (index <= end_index) {
1342 page = find_get_page(inode->i_mapping, index);
1343 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1344 account_page_redirty(page);
1345 __set_page_dirty_nobuffers(page);
1346 page_cache_release(page);
1347 index++;
1348 }
1349 return 0;
1350}
1351
Chris Masond1310b22008-01-24 16:13:08 -05001352/*
Chris Masond1310b22008-01-24 16:13:08 -05001353 * helper function to set both pages and extents in the tree writeback
1354 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001355static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001356{
1357 unsigned long index = start >> PAGE_CACHE_SHIFT;
1358 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1359 struct page *page;
1360
1361 while (index <= end_index) {
1362 page = find_get_page(tree->mapping, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001363 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Masond1310b22008-01-24 16:13:08 -05001364 set_page_writeback(page);
1365 page_cache_release(page);
1366 index++;
1367 }
Chris Masond1310b22008-01-24 16:13:08 -05001368 return 0;
1369}
Chris Masond1310b22008-01-24 16:13:08 -05001370
Chris Masond352ac62008-09-29 15:18:18 -04001371/* find the first state struct with 'bits' set after 'start', and
1372 * return it. tree->lock must be held. NULL will returned if
1373 * nothing was found after 'start'
1374 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001375static struct extent_state *
1376find_first_extent_bit_state(struct extent_io_tree *tree,
David Sterba41074882013-04-29 13:38:46 +00001377 u64 start, unsigned long bits)
Chris Masond7fc6402008-02-18 12:12:38 -05001378{
1379 struct rb_node *node;
1380 struct extent_state *state;
1381
1382 /*
1383 * this search will find all the extents that end after
1384 * our range starts.
1385 */
1386 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001387 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001388 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001389
Chris Masond3977122009-01-05 21:25:51 -05001390 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001391 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001392 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001393 return state;
Chris Masond3977122009-01-05 21:25:51 -05001394
Chris Masond7fc6402008-02-18 12:12:38 -05001395 node = rb_next(node);
1396 if (!node)
1397 break;
1398 }
1399out:
1400 return NULL;
1401}
Chris Masond7fc6402008-02-18 12:12:38 -05001402
Chris Masond352ac62008-09-29 15:18:18 -04001403/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001404 * find the first offset in the io tree with 'bits' set. zero is
1405 * returned if we find something, and *start_ret and *end_ret are
1406 * set to reflect the state struct that was found.
1407 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001408 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001409 */
1410int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
David Sterba41074882013-04-29 13:38:46 +00001411 u64 *start_ret, u64 *end_ret, unsigned long bits,
Josef Bacike6138872012-09-27 17:07:30 -04001412 struct extent_state **cached_state)
Xiao Guangrong69261c42011-07-14 03:19:45 +00001413{
1414 struct extent_state *state;
Josef Bacike6138872012-09-27 17:07:30 -04001415 struct rb_node *n;
Xiao Guangrong69261c42011-07-14 03:19:45 +00001416 int ret = 1;
1417
1418 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001419 if (cached_state && *cached_state) {
1420 state = *cached_state;
1421 if (state->end == start - 1 && state->tree) {
1422 n = rb_next(&state->rb_node);
1423 while (n) {
1424 state = rb_entry(n, struct extent_state,
1425 rb_node);
1426 if (state->state & bits)
1427 goto got_it;
1428 n = rb_next(n);
1429 }
1430 free_extent_state(*cached_state);
1431 *cached_state = NULL;
1432 goto out;
1433 }
1434 free_extent_state(*cached_state);
1435 *cached_state = NULL;
1436 }
1437
Xiao Guangrong69261c42011-07-14 03:19:45 +00001438 state = find_first_extent_bit_state(tree, start, bits);
Josef Bacike6138872012-09-27 17:07:30 -04001439got_it:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001440 if (state) {
Josef Bacike6138872012-09-27 17:07:30 -04001441 cache_state(state, cached_state);
Xiao Guangrong69261c42011-07-14 03:19:45 +00001442 *start_ret = state->start;
1443 *end_ret = state->end;
1444 ret = 0;
1445 }
Josef Bacike6138872012-09-27 17:07:30 -04001446out:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001447 spin_unlock(&tree->lock);
1448 return ret;
1449}
1450
1451/*
Chris Masond352ac62008-09-29 15:18:18 -04001452 * find a contiguous range of bytes in the file marked as delalloc, not
1453 * more than 'max_bytes'. start and end are used to return the range,
1454 *
1455 * 1 is returned if we find something, 0 if nothing was in the tree
1456 */
Chris Masonc8b97812008-10-29 14:49:59 -04001457static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001458 u64 *start, u64 *end, u64 max_bytes,
1459 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001460{
1461 struct rb_node *node;
1462 struct extent_state *state;
1463 u64 cur_start = *start;
1464 u64 found = 0;
1465 u64 total_bytes = 0;
1466
Chris Masoncad321a2008-12-17 14:51:42 -05001467 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001468
Chris Masond1310b22008-01-24 16:13:08 -05001469 /*
1470 * this search will find all the extents that end after
1471 * our range starts.
1472 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001473 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001474 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001475 if (!found)
1476 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001477 goto out;
1478 }
1479
Chris Masond3977122009-01-05 21:25:51 -05001480 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001481 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001482 if (found && (state->start != cur_start ||
1483 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001484 goto out;
1485 }
1486 if (!(state->state & EXTENT_DELALLOC)) {
1487 if (!found)
1488 *end = state->end;
1489 goto out;
1490 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001491 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001492 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001493 *cached_state = state;
1494 atomic_inc(&state->refs);
1495 }
Chris Masond1310b22008-01-24 16:13:08 -05001496 found++;
1497 *end = state->end;
1498 cur_start = state->end + 1;
1499 node = rb_next(node);
1500 if (!node)
1501 break;
1502 total_bytes += state->end - state->start + 1;
1503 if (total_bytes >= max_bytes)
1504 break;
1505 }
1506out:
Chris Masoncad321a2008-12-17 14:51:42 -05001507 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001508 return found;
1509}
1510
Jeff Mahoney143bede2012-03-01 14:56:26 +01001511static noinline void __unlock_for_delalloc(struct inode *inode,
1512 struct page *locked_page,
1513 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001514{
1515 int ret;
1516 struct page *pages[16];
1517 unsigned long index = start >> PAGE_CACHE_SHIFT;
1518 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1519 unsigned long nr_pages = end_index - index + 1;
1520 int i;
1521
1522 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001523 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001524
Chris Masond3977122009-01-05 21:25:51 -05001525 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001526 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001527 min_t(unsigned long, nr_pages,
1528 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001529 for (i = 0; i < ret; i++) {
1530 if (pages[i] != locked_page)
1531 unlock_page(pages[i]);
1532 page_cache_release(pages[i]);
1533 }
1534 nr_pages -= ret;
1535 index += ret;
1536 cond_resched();
1537 }
Chris Masonc8b97812008-10-29 14:49:59 -04001538}
1539
1540static noinline int lock_delalloc_pages(struct inode *inode,
1541 struct page *locked_page,
1542 u64 delalloc_start,
1543 u64 delalloc_end)
1544{
1545 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1546 unsigned long start_index = index;
1547 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1548 unsigned long pages_locked = 0;
1549 struct page *pages[16];
1550 unsigned long nrpages;
1551 int ret;
1552 int i;
1553
1554 /* the caller is responsible for locking the start index */
1555 if (index == locked_page->index && index == end_index)
1556 return 0;
1557
1558 /* skip the page at the start index */
1559 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001560 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001561 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001562 min_t(unsigned long,
1563 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001564 if (ret == 0) {
1565 ret = -EAGAIN;
1566 goto done;
1567 }
1568 /* now we have an array of pages, lock them all */
1569 for (i = 0; i < ret; i++) {
1570 /*
1571 * the caller is taking responsibility for
1572 * locked_page
1573 */
Chris Mason771ed682008-11-06 22:02:51 -05001574 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001575 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001576 if (!PageDirty(pages[i]) ||
1577 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001578 ret = -EAGAIN;
1579 unlock_page(pages[i]);
1580 page_cache_release(pages[i]);
1581 goto done;
1582 }
1583 }
Chris Masonc8b97812008-10-29 14:49:59 -04001584 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001585 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001586 }
Chris Masonc8b97812008-10-29 14:49:59 -04001587 nrpages -= ret;
1588 index += ret;
1589 cond_resched();
1590 }
1591 ret = 0;
1592done:
1593 if (ret && pages_locked) {
1594 __unlock_for_delalloc(inode, locked_page,
1595 delalloc_start,
1596 ((u64)(start_index + pages_locked - 1)) <<
1597 PAGE_CACHE_SHIFT);
1598 }
1599 return ret;
1600}
1601
1602/*
1603 * find a contiguous range of bytes in the file marked as delalloc, not
1604 * more than 'max_bytes'. start and end are used to return the range,
1605 *
1606 * 1 is returned if we find something, 0 if nothing was in the tree
1607 */
1608static noinline u64 find_lock_delalloc_range(struct inode *inode,
1609 struct extent_io_tree *tree,
1610 struct page *locked_page,
1611 u64 *start, u64 *end,
1612 u64 max_bytes)
1613{
1614 u64 delalloc_start;
1615 u64 delalloc_end;
1616 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001617 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001618 int ret;
1619 int loops = 0;
1620
1621again:
1622 /* step one, find a bunch of delalloc bytes starting at start */
1623 delalloc_start = *start;
1624 delalloc_end = 0;
1625 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001626 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001627 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001628 *start = delalloc_start;
1629 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001630 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001631 return found;
1632 }
1633
1634 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001635 * start comes from the offset of locked_page. We have to lock
1636 * pages in order, so we can't process delalloc bytes before
1637 * locked_page
1638 */
Chris Masond3977122009-01-05 21:25:51 -05001639 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001640 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001641
1642 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001643 * make sure to limit the number of pages we try to lock down
1644 * if we're looping.
1645 */
Chris Masond3977122009-01-05 21:25:51 -05001646 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001647 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001648
Chris Masonc8b97812008-10-29 14:49:59 -04001649 /* step two, lock all the pages after the page that has start */
1650 ret = lock_delalloc_pages(inode, locked_page,
1651 delalloc_start, delalloc_end);
1652 if (ret == -EAGAIN) {
1653 /* some of the pages are gone, lets avoid looping by
1654 * shortening the size of the delalloc range we're searching
1655 */
Chris Mason9655d292009-09-02 15:22:30 -04001656 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001657 if (!loops) {
1658 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1659 max_bytes = PAGE_CACHE_SIZE - offset;
1660 loops = 1;
1661 goto again;
1662 } else {
1663 found = 0;
1664 goto out_failed;
1665 }
1666 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001667 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
Chris Masonc8b97812008-10-29 14:49:59 -04001668
1669 /* step three, lock the state bits for the whole range */
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001670 lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001671
1672 /* then test to make sure it is all still delalloc */
1673 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001674 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001675 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001676 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1677 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001678 __unlock_for_delalloc(inode, locked_page,
1679 delalloc_start, delalloc_end);
1680 cond_resched();
1681 goto again;
1682 }
Chris Mason9655d292009-09-02 15:22:30 -04001683 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001684 *start = delalloc_start;
1685 *end = delalloc_end;
1686out_failed:
1687 return found;
1688}
1689
1690int extent_clear_unlock_delalloc(struct inode *inode,
1691 struct extent_io_tree *tree,
1692 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001693 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001694{
1695 int ret;
1696 struct page *pages[16];
1697 unsigned long index = start >> PAGE_CACHE_SHIFT;
1698 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1699 unsigned long nr_pages = end_index - index + 1;
1700 int i;
David Sterba41074882013-04-29 13:38:46 +00001701 unsigned long clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001702
Chris Masona791e352009-10-08 11:27:10 -04001703 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001704 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001705 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001706 clear_bits |= EXTENT_DIRTY;
1707
Chris Masona791e352009-10-08 11:27:10 -04001708 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001709 clear_bits |= EXTENT_DELALLOC;
1710
Chris Mason2c64c532009-09-02 15:04:12 -04001711 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001712 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1713 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1714 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001715 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001716
Chris Masond3977122009-01-05 21:25:51 -05001717 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001718 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001719 min_t(unsigned long,
1720 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001721 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001722
Chris Masona791e352009-10-08 11:27:10 -04001723 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001724 SetPagePrivate2(pages[i]);
1725
Chris Masonc8b97812008-10-29 14:49:59 -04001726 if (pages[i] == locked_page) {
1727 page_cache_release(pages[i]);
1728 continue;
1729 }
Chris Masona791e352009-10-08 11:27:10 -04001730 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001731 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001732 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001733 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001734 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001735 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001736 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001737 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001738 page_cache_release(pages[i]);
1739 }
1740 nr_pages -= ret;
1741 index += ret;
1742 cond_resched();
1743 }
1744 return 0;
1745}
Chris Masonc8b97812008-10-29 14:49:59 -04001746
Chris Masond352ac62008-09-29 15:18:18 -04001747/*
1748 * count the number of bytes in the tree that have a given bit(s)
1749 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1750 * cached. The total number found is returned.
1751 */
Chris Masond1310b22008-01-24 16:13:08 -05001752u64 count_range_bits(struct extent_io_tree *tree,
1753 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001754 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001755{
1756 struct rb_node *node;
1757 struct extent_state *state;
1758 u64 cur_start = *start;
1759 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001760 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001761 int found = 0;
1762
1763 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001764 WARN_ON(1);
1765 return 0;
1766 }
1767
Chris Masoncad321a2008-12-17 14:51:42 -05001768 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001769 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1770 total_bytes = tree->dirty_bytes;
1771 goto out;
1772 }
1773 /*
1774 * this search will find all the extents that end after
1775 * our range starts.
1776 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001777 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001778 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001779 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001780
Chris Masond3977122009-01-05 21:25:51 -05001781 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001782 state = rb_entry(node, struct extent_state, rb_node);
1783 if (state->start > search_end)
1784 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001785 if (contig && found && state->start > last + 1)
1786 break;
1787 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001788 total_bytes += min(search_end, state->end) + 1 -
1789 max(cur_start, state->start);
1790 if (total_bytes >= max_bytes)
1791 break;
1792 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001793 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001794 found = 1;
1795 }
Chris Masonec29ed52011-02-23 16:23:20 -05001796 last = state->end;
1797 } else if (contig && found) {
1798 break;
Chris Masond1310b22008-01-24 16:13:08 -05001799 }
1800 node = rb_next(node);
1801 if (!node)
1802 break;
1803 }
1804out:
Chris Masoncad321a2008-12-17 14:51:42 -05001805 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001806 return total_bytes;
1807}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001808
Chris Masond352ac62008-09-29 15:18:18 -04001809/*
1810 * set the private field for a given byte offset in the tree. If there isn't
1811 * an extent_state there already, this does nothing.
1812 */
Chris Masond1310b22008-01-24 16:13:08 -05001813int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1814{
1815 struct rb_node *node;
1816 struct extent_state *state;
1817 int ret = 0;
1818
Chris Masoncad321a2008-12-17 14:51:42 -05001819 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001820 /*
1821 * this search will find all the extents that end after
1822 * our range starts.
1823 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001824 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001825 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001826 ret = -ENOENT;
1827 goto out;
1828 }
1829 state = rb_entry(node, struct extent_state, rb_node);
1830 if (state->start != start) {
1831 ret = -ENOENT;
1832 goto out;
1833 }
1834 state->private = private;
1835out:
Chris Masoncad321a2008-12-17 14:51:42 -05001836 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001837 return ret;
1838}
1839
Miao Xiee4100d92013-04-05 07:20:56 +00001840void extent_cache_csums_dio(struct extent_io_tree *tree, u64 start, u32 csums[],
1841 int count)
1842{
1843 struct rb_node *node;
1844 struct extent_state *state;
1845
1846 spin_lock(&tree->lock);
1847 /*
1848 * this search will find all the extents that end after
1849 * our range starts.
1850 */
1851 node = tree_search(tree, start);
1852 BUG_ON(!node);
1853
1854 state = rb_entry(node, struct extent_state, rb_node);
1855 BUG_ON(state->start != start);
1856
1857 while (count) {
1858 state->private = *csums++;
1859 count--;
1860 state = next_state(state);
1861 }
1862 spin_unlock(&tree->lock);
1863}
1864
1865static inline u64 __btrfs_get_bio_offset(struct bio *bio, int bio_index)
1866{
1867 struct bio_vec *bvec = bio->bi_io_vec + bio_index;
1868
1869 return page_offset(bvec->bv_page) + bvec->bv_offset;
1870}
1871
1872void extent_cache_csums(struct extent_io_tree *tree, struct bio *bio, int bio_index,
1873 u32 csums[], int count)
1874{
1875 struct rb_node *node;
1876 struct extent_state *state = NULL;
1877 u64 start;
1878
1879 spin_lock(&tree->lock);
1880 do {
1881 start = __btrfs_get_bio_offset(bio, bio_index);
1882 if (state == NULL || state->start != start) {
1883 node = tree_search(tree, start);
1884 BUG_ON(!node);
1885
1886 state = rb_entry(node, struct extent_state, rb_node);
1887 BUG_ON(state->start != start);
1888 }
1889 state->private = *csums++;
1890 count--;
1891 bio_index++;
1892
1893 state = next_state(state);
1894 } while (count);
1895 spin_unlock(&tree->lock);
1896}
1897
Chris Masond1310b22008-01-24 16:13:08 -05001898int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1899{
1900 struct rb_node *node;
1901 struct extent_state *state;
1902 int ret = 0;
1903
Chris Masoncad321a2008-12-17 14:51:42 -05001904 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001905 /*
1906 * this search will find all the extents that end after
1907 * our range starts.
1908 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001909 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001910 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001911 ret = -ENOENT;
1912 goto out;
1913 }
1914 state = rb_entry(node, struct extent_state, rb_node);
1915 if (state->start != start) {
1916 ret = -ENOENT;
1917 goto out;
1918 }
1919 *private = state->private;
1920out:
Chris Masoncad321a2008-12-17 14:51:42 -05001921 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001922 return ret;
1923}
1924
1925/*
1926 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001927 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001928 * has the bits set. Otherwise, 1 is returned if any bit in the
1929 * range is found set.
1930 */
1931int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +00001932 unsigned long bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001933{
1934 struct extent_state *state = NULL;
1935 struct rb_node *node;
1936 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001937
Chris Masoncad321a2008-12-17 14:51:42 -05001938 spin_lock(&tree->lock);
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001939 if (cached && cached->tree && cached->start <= start &&
1940 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001941 node = &cached->rb_node;
1942 else
1943 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001944 while (node && start <= end) {
1945 state = rb_entry(node, struct extent_state, rb_node);
1946
1947 if (filled && state->start > start) {
1948 bitset = 0;
1949 break;
1950 }
1951
1952 if (state->start > end)
1953 break;
1954
1955 if (state->state & bits) {
1956 bitset = 1;
1957 if (!filled)
1958 break;
1959 } else if (filled) {
1960 bitset = 0;
1961 break;
1962 }
Chris Mason46562ce2009-09-23 20:23:16 -04001963
1964 if (state->end == (u64)-1)
1965 break;
1966
Chris Masond1310b22008-01-24 16:13:08 -05001967 start = state->end + 1;
1968 if (start > end)
1969 break;
1970 node = rb_next(node);
1971 if (!node) {
1972 if (filled)
1973 bitset = 0;
1974 break;
1975 }
1976 }
Chris Masoncad321a2008-12-17 14:51:42 -05001977 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001978 return bitset;
1979}
Chris Masond1310b22008-01-24 16:13:08 -05001980
1981/*
1982 * helper function to set a given page up to date if all the
1983 * extents in the tree for that page are up to date
1984 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001985static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001986{
Miao Xie4eee4fa2012-12-21 09:17:45 +00001987 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05001988 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001989 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001990 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001991}
1992
1993/*
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001994 * When IO fails, either with EIO or csum verification fails, we
1995 * try other mirrors that might have a good copy of the data. This
1996 * io_failure_record is used to record state as we go through all the
1997 * mirrors. If another mirror has good data, the page is set up to date
1998 * and things continue. If a good mirror can't be found, the original
1999 * bio end_io callback is called to indicate things have failed.
2000 */
2001struct io_failure_record {
2002 struct page *page;
2003 u64 start;
2004 u64 len;
2005 u64 logical;
2006 unsigned long bio_flags;
2007 int this_mirror;
2008 int failed_mirror;
2009 int in_validation;
2010};
2011
2012static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
2013 int did_repair)
2014{
2015 int ret;
2016 int err = 0;
2017 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2018
2019 set_state_private(failure_tree, rec->start, 0);
2020 ret = clear_extent_bits(failure_tree, rec->start,
2021 rec->start + rec->len - 1,
2022 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2023 if (ret)
2024 err = ret;
2025
David Woodhouse53b381b2013-01-29 18:40:14 -05002026 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
2027 rec->start + rec->len - 1,
2028 EXTENT_DAMAGED, GFP_NOFS);
2029 if (ret && !err)
2030 err = ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002031
2032 kfree(rec);
2033 return err;
2034}
2035
2036static void repair_io_failure_callback(struct bio *bio, int err)
2037{
2038 complete(bio->bi_private);
2039}
2040
2041/*
2042 * this bypasses the standard btrfs submit functions deliberately, as
2043 * the standard behavior is to write all copies in a raid setup. here we only
2044 * want to write the one bad copy. so we do the mapping for ourselves and issue
2045 * submit_bio directly.
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002046 * to avoid any synchronization issues, wait for the data after writing, which
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002047 * actually prevents the read that triggered the error from finishing.
2048 * currently, there can be no more than two copies of every data bit. thus,
2049 * exactly one rewrite is required.
2050 */
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002051int repair_io_failure(struct btrfs_fs_info *fs_info, u64 start,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002052 u64 length, u64 logical, struct page *page,
2053 int mirror_num)
2054{
2055 struct bio *bio;
2056 struct btrfs_device *dev;
2057 DECLARE_COMPLETION_ONSTACK(compl);
2058 u64 map_length = 0;
2059 u64 sector;
2060 struct btrfs_bio *bbio = NULL;
David Woodhouse53b381b2013-01-29 18:40:14 -05002061 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002062 int ret;
2063
2064 BUG_ON(!mirror_num);
2065
David Woodhouse53b381b2013-01-29 18:40:14 -05002066 /* we can't repair anything in raid56 yet */
2067 if (btrfs_is_parity_mirror(map_tree, logical, length, mirror_num))
2068 return 0;
2069
Chris Mason9be33952013-05-17 18:30:14 -04002070 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002071 if (!bio)
2072 return -EIO;
2073 bio->bi_private = &compl;
2074 bio->bi_end_io = repair_io_failure_callback;
2075 bio->bi_size = 0;
2076 map_length = length;
2077
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002078 ret = btrfs_map_block(fs_info, WRITE, logical,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002079 &map_length, &bbio, mirror_num);
2080 if (ret) {
2081 bio_put(bio);
2082 return -EIO;
2083 }
2084 BUG_ON(mirror_num != bbio->mirror_num);
2085 sector = bbio->stripes[mirror_num-1].physical >> 9;
2086 bio->bi_sector = sector;
2087 dev = bbio->stripes[mirror_num-1].dev;
2088 kfree(bbio);
2089 if (!dev || !dev->bdev || !dev->writeable) {
2090 bio_put(bio);
2091 return -EIO;
2092 }
2093 bio->bi_bdev = dev->bdev;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002094 bio_add_page(bio, page, length, start - page_offset(page));
Stefan Behrens21adbd52011-11-09 13:44:05 +01002095 btrfsic_submit_bio(WRITE_SYNC, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002096 wait_for_completion(&compl);
2097
2098 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2099 /* try to remap that extent elsewhere? */
2100 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002101 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002102 return -EIO;
2103 }
2104
Anand Jaind5b025d2012-07-02 22:05:21 -06002105 printk_ratelimited_in_rcu(KERN_INFO "btrfs read error corrected: ino %lu off %llu "
Josef Bacik606686e2012-06-04 14:03:51 -04002106 "(dev %s sector %llu)\n", page->mapping->host->i_ino,
2107 start, rcu_str_deref(dev->name), sector);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002108
2109 bio_put(bio);
2110 return 0;
2111}
2112
Josef Bacikea466792012-03-26 21:57:36 -04002113int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
2114 int mirror_num)
2115{
Josef Bacikea466792012-03-26 21:57:36 -04002116 u64 start = eb->start;
2117 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond95603b2012-04-12 15:55:15 -04002118 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04002119
2120 for (i = 0; i < num_pages; i++) {
2121 struct page *p = extent_buffer_page(eb, i);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002122 ret = repair_io_failure(root->fs_info, start, PAGE_CACHE_SIZE,
Josef Bacikea466792012-03-26 21:57:36 -04002123 start, p, mirror_num);
2124 if (ret)
2125 break;
2126 start += PAGE_CACHE_SIZE;
2127 }
2128
2129 return ret;
2130}
2131
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002132/*
2133 * each time an IO finishes, we do a fast check in the IO failure tree
2134 * to see if we need to process or clean up an io_failure_record
2135 */
2136static int clean_io_failure(u64 start, struct page *page)
2137{
2138 u64 private;
2139 u64 private_failure;
2140 struct io_failure_record *failrec;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002141 struct btrfs_fs_info *fs_info;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002142 struct extent_state *state;
2143 int num_copies;
2144 int did_repair = 0;
2145 int ret;
2146 struct inode *inode = page->mapping->host;
2147
2148 private = 0;
2149 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
2150 (u64)-1, 1, EXTENT_DIRTY, 0);
2151 if (!ret)
2152 return 0;
2153
2154 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
2155 &private_failure);
2156 if (ret)
2157 return 0;
2158
2159 failrec = (struct io_failure_record *)(unsigned long) private_failure;
2160 BUG_ON(!failrec->this_mirror);
2161
2162 if (failrec->in_validation) {
2163 /* there was no real error, just free the record */
2164 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
2165 failrec->start);
2166 did_repair = 1;
2167 goto out;
2168 }
2169
2170 spin_lock(&BTRFS_I(inode)->io_tree.lock);
2171 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
2172 failrec->start,
2173 EXTENT_LOCKED);
2174 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
2175
2176 if (state && state->start == failrec->start) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002177 fs_info = BTRFS_I(inode)->root->fs_info;
2178 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2179 failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002180 if (num_copies > 1) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002181 ret = repair_io_failure(fs_info, start, failrec->len,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002182 failrec->logical, page,
2183 failrec->failed_mirror);
2184 did_repair = !ret;
2185 }
David Woodhouse53b381b2013-01-29 18:40:14 -05002186 ret = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002187 }
2188
2189out:
2190 if (!ret)
2191 ret = free_io_failure(inode, failrec, did_repair);
2192
2193 return ret;
2194}
2195
2196/*
2197 * this is a generic handler for readpage errors (default
2198 * readpage_io_failed_hook). if other copies exist, read those and write back
2199 * good data to the failed position. does not investigate in remapping the
2200 * failed extent elsewhere, hoping the device will be smart enough to do this as
2201 * needed
2202 */
2203
2204static int bio_readpage_error(struct bio *failed_bio, struct page *page,
Miao Xie09a7f7a2013-07-25 19:22:32 +08002205 u64 start, u64 end, int failed_mirror)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002206{
2207 struct io_failure_record *failrec = NULL;
2208 u64 private;
2209 struct extent_map *em;
2210 struct inode *inode = page->mapping->host;
2211 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2212 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2213 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Miao Xie09a7f7a2013-07-25 19:22:32 +08002214 struct extent_state *state;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002215 struct bio *bio;
2216 int num_copies;
2217 int ret;
2218 int read_mode;
2219 u64 logical;
2220
2221 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2222
2223 ret = get_state_private(failure_tree, start, &private);
2224 if (ret) {
2225 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2226 if (!failrec)
2227 return -ENOMEM;
2228 failrec->start = start;
2229 failrec->len = end - start + 1;
2230 failrec->this_mirror = 0;
2231 failrec->bio_flags = 0;
2232 failrec->in_validation = 0;
2233
2234 read_lock(&em_tree->lock);
2235 em = lookup_extent_mapping(em_tree, start, failrec->len);
2236 if (!em) {
2237 read_unlock(&em_tree->lock);
2238 kfree(failrec);
2239 return -EIO;
2240 }
2241
2242 if (em->start > start || em->start + em->len < start) {
2243 free_extent_map(em);
2244 em = NULL;
2245 }
2246 read_unlock(&em_tree->lock);
2247
Tsutomu Itoh7a2d6a62012-10-01 03:07:15 -06002248 if (!em) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002249 kfree(failrec);
2250 return -EIO;
2251 }
2252 logical = start - em->start;
2253 logical = em->block_start + logical;
2254 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2255 logical = em->block_start;
2256 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2257 extent_set_compress_type(&failrec->bio_flags,
2258 em->compress_type);
2259 }
2260 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2261 "len=%llu\n", logical, start, failrec->len);
2262 failrec->logical = logical;
2263 free_extent_map(em);
2264
2265 /* set the bits in the private failure tree */
2266 ret = set_extent_bits(failure_tree, start, end,
2267 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2268 if (ret >= 0)
2269 ret = set_state_private(failure_tree, start,
2270 (u64)(unsigned long)failrec);
2271 /* set the bits in the inode's tree */
2272 if (ret >= 0)
2273 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2274 GFP_NOFS);
2275 if (ret < 0) {
2276 kfree(failrec);
2277 return ret;
2278 }
2279 } else {
2280 failrec = (struct io_failure_record *)(unsigned long)private;
2281 pr_debug("bio_readpage_error: (found) logical=%llu, "
2282 "start=%llu, len=%llu, validation=%d\n",
2283 failrec->logical, failrec->start, failrec->len,
2284 failrec->in_validation);
2285 /*
2286 * when data can be on disk more than twice, add to failrec here
2287 * (e.g. with a list for failed_mirror) to make
2288 * clean_io_failure() clean all those errors at once.
2289 */
2290 }
Stefan Behrens5d964052012-11-05 14:59:07 +01002291 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
2292 failrec->logical, failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002293 if (num_copies == 1) {
2294 /*
2295 * we only have a single copy of the data, so don't bother with
2296 * all the retry and error correction code that follows. no
2297 * matter what the error is, it is very likely to persist.
2298 */
Miao Xie09a7f7a2013-07-25 19:22:32 +08002299 pr_debug("bio_readpage_error: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
2300 num_copies, failrec->this_mirror, failed_mirror);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002301 free_io_failure(inode, failrec, 0);
2302 return -EIO;
2303 }
2304
Miao Xie09a7f7a2013-07-25 19:22:32 +08002305 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);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002311
2312 /*
2313 * there are two premises:
2314 * a) deliver good data to the caller
2315 * b) correct the bad sectors on disk
2316 */
2317 if (failed_bio->bi_vcnt > 1) {
2318 /*
2319 * to fulfill b), we need to know the exact failing sectors, as
2320 * we don't want to rewrite any more than the failed ones. thus,
2321 * we need separate read requests for the failed bio
2322 *
2323 * if the following BUG_ON triggers, our validation request got
2324 * merged. we need separate requests for our algorithm to work.
2325 */
2326 BUG_ON(failrec->in_validation);
2327 failrec->in_validation = 1;
2328 failrec->this_mirror = failed_mirror;
2329 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2330 } else {
2331 /*
2332 * we're ready to fulfill a) and b) alongside. get a good copy
2333 * of the failed sector and if we succeed, we have setup
2334 * everything for repair_io_failure to do the rest for us.
2335 */
2336 if (failrec->in_validation) {
2337 BUG_ON(failrec->this_mirror != failed_mirror);
2338 failrec->in_validation = 0;
2339 failrec->this_mirror = 0;
2340 }
2341 failrec->failed_mirror = failed_mirror;
2342 failrec->this_mirror++;
2343 if (failrec->this_mirror == failed_mirror)
2344 failrec->this_mirror++;
2345 read_mode = READ_SYNC;
2346 }
2347
2348 if (!state || failrec->this_mirror > num_copies) {
2349 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2350 "next_mirror %d, failed_mirror %d\n", state,
2351 num_copies, failrec->this_mirror, failed_mirror);
2352 free_io_failure(inode, failrec, 0);
2353 return -EIO;
2354 }
2355
Chris Mason9be33952013-05-17 18:30:14 -04002356 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04002357 if (!bio) {
2358 free_io_failure(inode, failrec, 0);
2359 return -EIO;
2360 }
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002361 bio->bi_private = state;
2362 bio->bi_end_io = failed_bio->bi_end_io;
2363 bio->bi_sector = failrec->logical >> 9;
2364 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2365 bio->bi_size = 0;
2366
2367 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2368
2369 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2370 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2371 failrec->this_mirror, num_copies, failrec->in_validation);
2372
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002373 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2374 failrec->this_mirror,
2375 failrec->bio_flags, 0);
2376 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002377}
2378
Chris Masond1310b22008-01-24 16:13:08 -05002379/* lots and lots of room for performance fixes in the end_bio funcs */
2380
Jeff Mahoney87826df2012-02-15 16:23:57 +01002381int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2382{
2383 int uptodate = (err == 0);
2384 struct extent_io_tree *tree;
2385 int ret;
2386
2387 tree = &BTRFS_I(page->mapping->host)->io_tree;
2388
2389 if (tree->ops && tree->ops->writepage_end_io_hook) {
2390 ret = tree->ops->writepage_end_io_hook(page, start,
2391 end, NULL, uptodate);
2392 if (ret)
2393 uptodate = 0;
2394 }
2395
Jeff Mahoney87826df2012-02-15 16:23:57 +01002396 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002397 ClearPageUptodate(page);
2398 SetPageError(page);
2399 }
2400 return 0;
2401}
2402
Chris Masond1310b22008-01-24 16:13:08 -05002403/*
2404 * after a writepage IO is done, we need to:
2405 * clear the uptodate bits on error
2406 * clear the writeback bits in the extent tree for this IO
2407 * end_page_writeback if the page has no more pending IO
2408 *
2409 * Scheduling is not allowed, so the extent state tree is expected
2410 * to have one and only one object corresponding to this IO.
2411 */
Chris Masond1310b22008-01-24 16:13:08 -05002412static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002413{
Chris Masond1310b22008-01-24 16:13:08 -05002414 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04002415 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002416 u64 start;
2417 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05002418
Chris Masond1310b22008-01-24 16:13:08 -05002419 do {
2420 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04002421 tree = &BTRFS_I(page->mapping->host)->io_tree;
2422
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002423 /* We always issue full-page reads, but if some block
2424 * in a page fails to read, blk_update_request() will
2425 * advance bv_offset and adjust bv_len to compensate.
2426 * Print a warning for nonzero offsets, and an error
2427 * if they don't add up to a full page. */
2428 if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE)
2429 printk("%s page write in btrfs with offset %u and length %u\n",
2430 bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE
2431 ? KERN_ERR "partial" : KERN_INFO "incomplete",
2432 bvec->bv_offset, bvec->bv_len);
Chris Masond1310b22008-01-24 16:13:08 -05002433
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002434 start = page_offset(page);
2435 end = start + bvec->bv_offset + bvec->bv_len - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002436
2437 if (--bvec >= bio->bi_io_vec)
2438 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04002439
Jeff Mahoney87826df2012-02-15 16:23:57 +01002440 if (end_extent_writepage(page, err, start, end))
2441 continue;
Chris Mason70dec802008-01-29 09:59:12 -05002442
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002443 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002444 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04002445
Chris Masond1310b22008-01-24 16:13:08 -05002446 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002447}
2448
2449/*
2450 * after a readpage IO is done, we need to:
2451 * clear the uptodate bits on error
2452 * set the uptodate bits if things worked
2453 * set the page up to date if all extents in the tree are uptodate
2454 * clear the lock bit in the extent tree
2455 * unlock the page if there are no other extents locked for it
2456 *
2457 * Scheduling is not allowed, so the extent state tree is expected
2458 * to have one and only one object corresponding to this IO.
2459 */
Chris Masond1310b22008-01-24 16:13:08 -05002460static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002461{
2462 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00002463 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2464 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04002465 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002466 u64 start;
2467 u64 end;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002468 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002469 int ret;
2470
Chris Masond20f7042008-12-08 16:58:54 -05002471 if (err)
2472 uptodate = 0;
2473
Chris Masond1310b22008-01-24 16:13:08 -05002474 do {
2475 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00002476 struct extent_state *cached = NULL;
2477 struct extent_state *state;
Chris Mason9be33952013-05-17 18:30:14 -04002478 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacika71754f2013-06-17 17:14:39 -04002479 struct inode *inode = page->mapping->host;
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);
Josef Bacika71754f2013-06-17 17:14:39 -04002484 tree = &BTRFS_I(inode)->io_tree;
David Woodhouse902b22f2008-08-20 08:51:49 -04002485
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 */
Miao Xie09a7f7a2013-07-25 19:22:32 +08002540 ret = bio_readpage_error(bio, page, start, end, mirror);
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) {
Josef Bacika71754f2013-06-17 17:14:39 -04002558 loff_t i_size = i_size_read(inode);
2559 pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
2560 unsigned offset;
2561
2562 /* Zero out the end if this page straddles i_size */
2563 offset = i_size & (PAGE_CACHE_SIZE-1);
2564 if (page->index == end_index && offset)
2565 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002566 SetPageUptodate(page);
Chris Mason70dec802008-01-29 09:59:12 -05002567 } else {
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002568 ClearPageUptodate(page);
2569 SetPageError(page);
Chris Mason70dec802008-01-29 09:59:12 -05002570 }
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002571 unlock_page(page);
Chris Mason4125bf72010-02-03 18:18:45 +00002572 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05002573
2574 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002575}
2576
Chris Mason9be33952013-05-17 18:30:14 -04002577/*
2578 * this allocates from the btrfs_bioset. We're returning a bio right now
2579 * but you can call btrfs_io_bio for the appropriate container_of magic
2580 */
Miao Xie88f794e2010-11-22 03:02:55 +00002581struct bio *
2582btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2583 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002584{
2585 struct bio *bio;
2586
Chris Mason9be33952013-05-17 18:30:14 -04002587 bio = bio_alloc_bioset(gfp_flags, nr_vecs, btrfs_bioset);
Chris Masond1310b22008-01-24 16:13:08 -05002588
2589 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
Chris Mason9be33952013-05-17 18:30:14 -04002590 while (!bio && (nr_vecs /= 2)) {
2591 bio = bio_alloc_bioset(gfp_flags,
2592 nr_vecs, btrfs_bioset);
2593 }
Chris Masond1310b22008-01-24 16:13:08 -05002594 }
2595
2596 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04002597 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002598 bio->bi_bdev = bdev;
2599 bio->bi_sector = first_sector;
2600 }
2601 return bio;
2602}
2603
Chris Mason9be33952013-05-17 18:30:14 -04002604struct bio *btrfs_bio_clone(struct bio *bio, gfp_t gfp_mask)
2605{
2606 return bio_clone_bioset(bio, gfp_mask, btrfs_bioset);
2607}
2608
2609
2610/* this also allocates from the btrfs_bioset */
2611struct bio *btrfs_io_bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs)
2612{
2613 return bio_alloc_bioset(gfp_mask, nr_iovecs, btrfs_bioset);
2614}
2615
2616
Jeff Mahoney355808c2011-10-03 23:23:14 -04002617static int __must_check submit_one_bio(int rw, struct bio *bio,
2618 int mirror_num, unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002619{
Chris Masond1310b22008-01-24 16:13:08 -05002620 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002621 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2622 struct page *page = bvec->bv_page;
2623 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002624 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002625
Miao Xie4eee4fa2012-12-21 09:17:45 +00002626 start = page_offset(page) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002627
David Woodhouse902b22f2008-08-20 08:51:49 -04002628 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002629
2630 bio_get(bio);
2631
Chris Mason065631f2008-02-20 12:07:25 -05002632 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00002633 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002634 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002635 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01002636 btrfsic_submit_bio(rw, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002637
Chris Masond1310b22008-01-24 16:13:08 -05002638 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2639 ret = -EOPNOTSUPP;
2640 bio_put(bio);
2641 return ret;
2642}
2643
David Woodhouse64a16702009-07-15 23:29:37 +01002644static int merge_bio(int rw, struct extent_io_tree *tree, struct page *page,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002645 unsigned long offset, size_t size, struct bio *bio,
2646 unsigned long bio_flags)
2647{
2648 int ret = 0;
2649 if (tree->ops && tree->ops->merge_bio_hook)
David Woodhouse64a16702009-07-15 23:29:37 +01002650 ret = tree->ops->merge_bio_hook(rw, page, offset, size, bio,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002651 bio_flags);
2652 BUG_ON(ret < 0);
2653 return ret;
2654
2655}
2656
Chris Masond1310b22008-01-24 16:13:08 -05002657static int submit_extent_page(int rw, struct extent_io_tree *tree,
2658 struct page *page, sector_t sector,
2659 size_t size, unsigned long offset,
2660 struct block_device *bdev,
2661 struct bio **bio_ret,
2662 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04002663 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002664 int mirror_num,
2665 unsigned long prev_bio_flags,
2666 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002667{
2668 int ret = 0;
2669 struct bio *bio;
2670 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04002671 int contig = 0;
2672 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2673 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05002674 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002675
2676 if (bio_ret && *bio_ret) {
2677 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002678 if (old_compressed)
2679 contig = bio->bi_sector == sector;
2680 else
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002681 contig = bio_end_sector(bio) == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002682
2683 if (prev_bio_flags != bio_flags || !contig ||
David Woodhouse64a16702009-07-15 23:29:37 +01002684 merge_bio(rw, tree, page, offset, page_size, bio, bio_flags) ||
Chris Masonc8b97812008-10-29 14:49:59 -04002685 bio_add_page(bio, page, page_size, offset) < page_size) {
2686 ret = submit_one_bio(rw, bio, mirror_num,
2687 prev_bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002688 if (ret < 0)
2689 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05002690 bio = NULL;
2691 } else {
2692 return 0;
2693 }
2694 }
Chris Masonc8b97812008-10-29 14:49:59 -04002695 if (this_compressed)
2696 nr = BIO_MAX_PAGES;
2697 else
2698 nr = bio_get_nr_vecs(bdev);
2699
Miao Xie88f794e2010-11-22 03:02:55 +00002700 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002701 if (!bio)
2702 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002703
Chris Masonc8b97812008-10-29 14:49:59 -04002704 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002705 bio->bi_end_io = end_io_func;
2706 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05002707
Chris Masond3977122009-01-05 21:25:51 -05002708 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002709 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002710 else
Chris Masonc8b97812008-10-29 14:49:59 -04002711 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002712
2713 return ret;
2714}
2715
Eric Sandeen48a3b632013-04-25 20:41:01 +00002716static void attach_extent_buffer_page(struct extent_buffer *eb,
2717 struct page *page)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002718{
2719 if (!PagePrivate(page)) {
2720 SetPagePrivate(page);
2721 page_cache_get(page);
2722 set_page_private(page, (unsigned long)eb);
2723 } else {
2724 WARN_ON(page->private != (unsigned long)eb);
2725 }
2726}
2727
Chris Masond1310b22008-01-24 16:13:08 -05002728void set_page_extent_mapped(struct page *page)
2729{
2730 if (!PagePrivate(page)) {
2731 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002732 page_cache_get(page);
Chris Mason6af118c2008-07-22 11:18:07 -04002733 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002734 }
2735}
2736
Chris Masond1310b22008-01-24 16:13:08 -05002737/*
2738 * basic readpage implementation. Locked extent state structs are inserted
2739 * into the tree that are removed when the IO is done (by the end_io
2740 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002741 * XXX JDM: This needs looking at to ensure proper page locking
Chris Masond1310b22008-01-24 16:13:08 -05002742 */
2743static int __extent_read_full_page(struct extent_io_tree *tree,
2744 struct page *page,
2745 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002746 struct bio **bio, int mirror_num,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04002747 unsigned long *bio_flags, int rw)
Chris Masond1310b22008-01-24 16:13:08 -05002748{
2749 struct inode *inode = page->mapping->host;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002750 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05002751 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2752 u64 end;
2753 u64 cur = start;
2754 u64 extent_offset;
2755 u64 last_byte = i_size_read(inode);
2756 u64 block_start;
2757 u64 cur_end;
2758 sector_t sector;
2759 struct extent_map *em;
2760 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002761 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05002762 int ret;
2763 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002764 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002765 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002766 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002767 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002768 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002769
2770 set_page_extent_mapped(page);
2771
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002772 if (!PageUptodate(page)) {
2773 if (cleancache_get_page(page) == 0) {
2774 BUG_ON(blocksize != PAGE_SIZE);
2775 goto out;
2776 }
2777 }
2778
Chris Masond1310b22008-01-24 16:13:08 -05002779 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002780 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002781 lock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002782 ordered = btrfs_lookup_ordered_extent(inode, start);
2783 if (!ordered)
2784 break;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002785 unlock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002786 btrfs_start_ordered_extent(inode, ordered, 1);
2787 btrfs_put_ordered_extent(ordered);
2788 }
Chris Masond1310b22008-01-24 16:13:08 -05002789
Chris Masonc8b97812008-10-29 14:49:59 -04002790 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2791 char *userpage;
2792 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2793
2794 if (zero_offset) {
2795 iosize = PAGE_CACHE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002796 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002797 memset(userpage + zero_offset, 0, iosize);
2798 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002799 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002800 }
2801 }
Chris Masond1310b22008-01-24 16:13:08 -05002802 while (cur <= end) {
Josef Bacikc8f2f242013-02-11 11:33:00 -05002803 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2804
Chris Masond1310b22008-01-24 16:13:08 -05002805 if (cur >= last_byte) {
2806 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002807 struct extent_state *cached = NULL;
2808
David Sterba306e16c2011-04-19 14:29:38 +02002809 iosize = PAGE_CACHE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002810 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002811 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002812 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002813 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002814 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002815 &cached, GFP_NOFS);
2816 unlock_extent_cached(tree, cur, cur + iosize - 1,
2817 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002818 break;
2819 }
David Sterba306e16c2011-04-19 14:29:38 +02002820 em = get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002821 end - cur + 1, 0);
David Sterbac7040052011-04-19 18:00:01 +02002822 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002823 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002824 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002825 break;
2826 }
Chris Masond1310b22008-01-24 16:13:08 -05002827 extent_offset = cur - em->start;
2828 BUG_ON(extent_map_end(em) <= cur);
2829 BUG_ON(end < cur);
2830
Li Zefan261507a02010-12-17 14:21:50 +08002831 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002832 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002833 extent_set_compress_type(&this_bio_flag,
2834 em->compress_type);
2835 }
Chris Masonc8b97812008-10-29 14:49:59 -04002836
Chris Masond1310b22008-01-24 16:13:08 -05002837 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2838 cur_end = min(extent_map_end(em) - 1, end);
Qu Wenruofda28322013-02-26 08:10:22 +00002839 iosize = ALIGN(iosize, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -04002840 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2841 disk_io_size = em->block_len;
2842 sector = em->block_start >> 9;
2843 } else {
2844 sector = (em->block_start + extent_offset) >> 9;
2845 disk_io_size = iosize;
2846 }
Chris Masond1310b22008-01-24 16:13:08 -05002847 bdev = em->bdev;
2848 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002849 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2850 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002851 free_extent_map(em);
2852 em = NULL;
2853
2854 /* we've found a hole, just zero and go on */
2855 if (block_start == EXTENT_MAP_HOLE) {
2856 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002857 struct extent_state *cached = NULL;
2858
Cong Wang7ac687d2011-11-25 23:14:28 +08002859 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002860 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002861 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002862 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002863
2864 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002865 &cached, GFP_NOFS);
2866 unlock_extent_cached(tree, cur, cur + iosize - 1,
2867 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002868 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002869 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002870 continue;
2871 }
2872 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002873 if (test_range_bit(tree, cur, cur_end,
2874 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002875 check_page_uptodate(tree, page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002876 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002877 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002878 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002879 continue;
2880 }
Chris Mason70dec802008-01-29 09:59:12 -05002881 /* we have an inline extent but it didn't get marked up
2882 * to date. Error out
2883 */
2884 if (block_start == EXTENT_MAP_INLINE) {
2885 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002886 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05002887 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002888 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05002889 continue;
2890 }
Chris Masond1310b22008-01-24 16:13:08 -05002891
Josef Bacikc8f2f242013-02-11 11:33:00 -05002892 pnr -= page->index;
Josef Bacikd4c7ca82013-04-19 19:49:09 -04002893 ret = submit_extent_page(rw, tree, page,
David Sterba306e16c2011-04-19 14:29:38 +02002894 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04002895 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002896 end_bio_extent_readpage, mirror_num,
2897 *bio_flags,
2898 this_bio_flag);
Josef Bacikc8f2f242013-02-11 11:33:00 -05002899 if (!ret) {
2900 nr++;
2901 *bio_flags = this_bio_flag;
2902 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002903 SetPageError(page);
Josef Bacikedd33c92012-10-05 16:40:32 -04002904 unlock_extent(tree, cur, cur + iosize - 1);
2905 }
Chris Masond1310b22008-01-24 16:13:08 -05002906 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002907 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002908 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002909out:
Chris Masond1310b22008-01-24 16:13:08 -05002910 if (!nr) {
2911 if (!PageError(page))
2912 SetPageUptodate(page);
2913 unlock_page(page);
2914 }
2915 return 0;
2916}
2917
2918int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002919 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05002920{
2921 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002922 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002923 int ret;
2924
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002925 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04002926 &bio_flags, READ);
Chris Masond1310b22008-01-24 16:13:08 -05002927 if (bio)
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002928 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002929 return ret;
2930}
Chris Masond1310b22008-01-24 16:13:08 -05002931
Chris Mason11c83492009-04-20 15:50:09 -04002932static noinline void update_nr_written(struct page *page,
2933 struct writeback_control *wbc,
2934 unsigned long nr_written)
2935{
2936 wbc->nr_to_write -= nr_written;
2937 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2938 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2939 page->mapping->writeback_index = page->index + nr_written;
2940}
2941
Chris Masond1310b22008-01-24 16:13:08 -05002942/*
2943 * the writepage semantics are similar to regular writepage. extent
2944 * records are inserted to lock ranges in the tree, and as dirty areas
2945 * are found, they are marked writeback. Then the lock bits are removed
2946 * and the end_io handler clears the writeback ranges
2947 */
2948static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2949 void *data)
2950{
2951 struct inode *inode = page->mapping->host;
2952 struct extent_page_data *epd = data;
2953 struct extent_io_tree *tree = epd->tree;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002954 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05002955 u64 delalloc_start;
2956 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2957 u64 end;
2958 u64 cur = start;
2959 u64 extent_offset;
2960 u64 last_byte = i_size_read(inode);
2961 u64 block_start;
2962 u64 iosize;
2963 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002964 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002965 struct extent_map *em;
2966 struct block_device *bdev;
2967 int ret;
2968 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002969 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002970 size_t blocksize;
2971 loff_t i_size = i_size_read(inode);
2972 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2973 u64 nr_delalloc;
2974 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002975 int page_started;
2976 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002977 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002978 unsigned long nr_written = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002979 bool fill_delalloc = true;
Chris Masond1310b22008-01-24 16:13:08 -05002980
Chris Masonffbd5172009-04-20 15:50:09 -04002981 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002982 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002983 else
2984 write_flags = WRITE;
2985
liubo1abe9b82011-03-24 11:18:59 +00002986 trace___extent_writepage(page, inode, wbc);
2987
Chris Masond1310b22008-01-24 16:13:08 -05002988 WARN_ON(!PageLocked(page));
Chris Masonbf0da8c2011-11-04 12:29:37 -04002989
2990 ClearPageError(page);
2991
Chris Mason7f3c74f2008-07-18 12:01:11 -04002992 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002993 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002994 (page->index == end_index && !pg_offset)) {
Lukas Czernerd47992f2013-05-21 23:17:23 -04002995 page->mapping->a_ops->invalidatepage(page, 0, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002996 unlock_page(page);
2997 return 0;
2998 }
2999
3000 if (page->index == end_index) {
3001 char *userpage;
3002
Cong Wang7ac687d2011-11-25 23:14:28 +08003003 userpage = kmap_atomic(page);
Chris Mason7f3c74f2008-07-18 12:01:11 -04003004 memset(userpage + pg_offset, 0,
3005 PAGE_CACHE_SIZE - pg_offset);
Cong Wang7ac687d2011-11-25 23:14:28 +08003006 kunmap_atomic(userpage);
Chris Mason211c17f2008-05-15 09:13:45 -04003007 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003008 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003009 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003010
3011 set_page_extent_mapped(page);
3012
Josef Bacik9e487102011-08-01 12:08:18 -04003013 if (!tree->ops || !tree->ops->fill_delalloc)
3014 fill_delalloc = false;
3015
Chris Masond1310b22008-01-24 16:13:08 -05003016 delalloc_start = start;
3017 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003018 page_started = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04003019 if (!epd->extent_locked && fill_delalloc) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003020 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04003021 /*
3022 * make sure the wbc mapping index is at least updated
3023 * to this page.
3024 */
3025 update_nr_written(page, wbc, 0);
3026
Chris Masond3977122009-01-05 21:25:51 -05003027 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05003028 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04003029 page,
3030 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05003031 &delalloc_end,
3032 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05003033 if (nr_delalloc == 0) {
3034 delalloc_start = delalloc_end + 1;
3035 continue;
3036 }
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09003037 ret = tree->ops->fill_delalloc(inode, page,
3038 delalloc_start,
3039 delalloc_end,
3040 &page_started,
3041 &nr_written);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003042 /* File system has been set read-only */
3043 if (ret) {
3044 SetPageError(page);
3045 goto done;
3046 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003047 /*
3048 * delalloc_end is already one less than the total
3049 * length, so we don't subtract one from
3050 * PAGE_CACHE_SIZE
3051 */
3052 delalloc_to_write += (delalloc_end - delalloc_start +
3053 PAGE_CACHE_SIZE) >>
3054 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003055 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05003056 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003057 if (wbc->nr_to_write < delalloc_to_write) {
3058 int thresh = 8192;
3059
3060 if (delalloc_to_write < thresh * 2)
3061 thresh = delalloc_to_write;
3062 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3063 thresh);
3064 }
Chris Masonc8b97812008-10-29 14:49:59 -04003065
Chris Mason771ed682008-11-06 22:02:51 -05003066 /* did the fill delalloc function already unlock and start
3067 * the IO?
3068 */
3069 if (page_started) {
3070 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04003071 /*
3072 * we've unlocked the page, so we can't update
3073 * the mapping's writeback index, just update
3074 * nr_to_write.
3075 */
3076 wbc->nr_to_write -= nr_written;
3077 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05003078 }
Chris Masonc8b97812008-10-29 14:49:59 -04003079 }
Chris Mason247e7432008-07-17 12:53:51 -04003080 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04003081 ret = tree->ops->writepage_start_hook(page, start,
3082 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01003083 if (ret) {
3084 /* Fixup worker will requeue */
3085 if (ret == -EBUSY)
3086 wbc->pages_skipped++;
3087 else
3088 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04003089 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04003090 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05003091 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04003092 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04003093 }
3094 }
3095
Chris Mason11c83492009-04-20 15:50:09 -04003096 /*
3097 * we don't want to touch the inode after unlocking the page,
3098 * so we update the mapping writeback index now
3099 */
3100 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05003101
Chris Masond1310b22008-01-24 16:13:08 -05003102 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05003103 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003104 if (tree->ops && tree->ops->writepage_end_io_hook)
3105 tree->ops->writepage_end_io_hook(page, start,
3106 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003107 goto done;
3108 }
3109
Chris Masond1310b22008-01-24 16:13:08 -05003110 blocksize = inode->i_sb->s_blocksize;
3111
3112 while (cur <= end) {
3113 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003114 if (tree->ops && tree->ops->writepage_end_io_hook)
3115 tree->ops->writepage_end_io_hook(page, cur,
3116 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003117 break;
3118 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003119 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05003120 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02003121 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003122 SetPageError(page);
3123 break;
3124 }
3125
3126 extent_offset = cur - em->start;
3127 BUG_ON(extent_map_end(em) <= cur);
3128 BUG_ON(end < cur);
3129 iosize = min(extent_map_end(em) - cur, end - cur + 1);
Qu Wenruofda28322013-02-26 08:10:22 +00003130 iosize = ALIGN(iosize, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05003131 sector = (em->block_start + extent_offset) >> 9;
3132 bdev = em->bdev;
3133 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003134 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05003135 free_extent_map(em);
3136 em = NULL;
3137
Chris Masonc8b97812008-10-29 14:49:59 -04003138 /*
3139 * compressed and inline extents are written through other
3140 * paths in the FS
3141 */
3142 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003143 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003144 /*
3145 * end_io notification does not happen here for
3146 * compressed extents
3147 */
3148 if (!compressed && tree->ops &&
3149 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003150 tree->ops->writepage_end_io_hook(page, cur,
3151 cur + iosize - 1,
3152 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003153 else if (compressed) {
3154 /* we don't want to end_page_writeback on
3155 * a compressed extent. this happens
3156 * elsewhere
3157 */
3158 nr++;
3159 }
3160
3161 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003162 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003163 continue;
3164 }
Chris Masond1310b22008-01-24 16:13:08 -05003165 /* leave this out until we have a page_mkwrite call */
3166 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04003167 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05003168 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003169 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003170 continue;
3171 }
Chris Masonc8b97812008-10-29 14:49:59 -04003172
Chris Masond1310b22008-01-24 16:13:08 -05003173 if (tree->ops && tree->ops->writepage_io_hook) {
3174 ret = tree->ops->writepage_io_hook(page, cur,
3175 cur + iosize - 1);
3176 } else {
3177 ret = 0;
3178 }
Chris Mason1259ab72008-05-12 13:39:03 -04003179 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003180 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003181 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003182 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003183
Chris Masond1310b22008-01-24 16:13:08 -05003184 set_range_writeback(tree, cur, cur + iosize - 1);
3185 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05003186 printk(KERN_ERR "btrfs warning page %lu not "
3187 "writeback, cur %llu end %llu\n",
3188 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05003189 (unsigned long long)end);
3190 }
3191
Chris Masonffbd5172009-04-20 15:50:09 -04003192 ret = submit_extent_page(write_flags, tree, page,
3193 sector, iosize, pg_offset,
3194 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04003195 end_bio_extent_writepage,
3196 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003197 if (ret)
3198 SetPageError(page);
3199 }
3200 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003201 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003202 nr++;
3203 }
3204done:
3205 if (nr == 0) {
3206 /* make sure the mapping tag for page dirty gets cleared */
3207 set_page_writeback(page);
3208 end_page_writeback(page);
3209 }
Chris Masond1310b22008-01-24 16:13:08 -05003210 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05003211
Chris Mason11c83492009-04-20 15:50:09 -04003212done_unlocked:
3213
Chris Mason2c64c532009-09-02 15:04:12 -04003214 /* drop our reference on any cached states */
3215 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05003216 return 0;
3217}
3218
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003219static int eb_wait(void *word)
3220{
3221 io_schedule();
3222 return 0;
3223}
3224
Josef Bacikfd8b2b62013-04-24 16:41:19 -04003225void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003226{
3227 wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
3228 TASK_UNINTERRUPTIBLE);
3229}
3230
3231static int lock_extent_buffer_for_io(struct extent_buffer *eb,
3232 struct btrfs_fs_info *fs_info,
3233 struct extent_page_data *epd)
3234{
3235 unsigned long i, num_pages;
3236 int flush = 0;
3237 int ret = 0;
3238
3239 if (!btrfs_try_tree_write_lock(eb)) {
3240 flush = 1;
3241 flush_write_bio(epd);
3242 btrfs_tree_lock(eb);
3243 }
3244
3245 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3246 btrfs_tree_unlock(eb);
3247 if (!epd->sync_io)
3248 return 0;
3249 if (!flush) {
3250 flush_write_bio(epd);
3251 flush = 1;
3252 }
Chris Masona098d8e2012-03-21 12:09:56 -04003253 while (1) {
3254 wait_on_extent_buffer_writeback(eb);
3255 btrfs_tree_lock(eb);
3256 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3257 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003258 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003259 }
3260 }
3261
Josef Bacik51561ff2012-07-20 16:25:24 -04003262 /*
3263 * We need to do this to prevent races in people who check if the eb is
3264 * under IO since we can end up having no IO bits set for a short period
3265 * of time.
3266 */
3267 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003268 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3269 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003270 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003271 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Miao Xiee2d84522013-01-29 10:09:20 +00003272 __percpu_counter_add(&fs_info->dirty_metadata_bytes,
3273 -eb->len,
3274 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003275 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003276 } else {
3277 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003278 }
3279
3280 btrfs_tree_unlock(eb);
3281
3282 if (!ret)
3283 return ret;
3284
3285 num_pages = num_extent_pages(eb->start, eb->len);
3286 for (i = 0; i < num_pages; i++) {
3287 struct page *p = extent_buffer_page(eb, i);
3288
3289 if (!trylock_page(p)) {
3290 if (!flush) {
3291 flush_write_bio(epd);
3292 flush = 1;
3293 }
3294 lock_page(p);
3295 }
3296 }
3297
3298 return ret;
3299}
3300
3301static void end_extent_buffer_writeback(struct extent_buffer *eb)
3302{
3303 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3304 smp_mb__after_clear_bit();
3305 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3306}
3307
3308static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
3309{
3310 int uptodate = err == 0;
3311 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
3312 struct extent_buffer *eb;
3313 int done;
3314
3315 do {
3316 struct page *page = bvec->bv_page;
3317
3318 bvec--;
3319 eb = (struct extent_buffer *)page->private;
3320 BUG_ON(!eb);
3321 done = atomic_dec_and_test(&eb->io_pages);
3322
3323 if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
3324 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3325 ClearPageUptodate(page);
3326 SetPageError(page);
3327 }
3328
3329 end_page_writeback(page);
3330
3331 if (!done)
3332 continue;
3333
3334 end_extent_buffer_writeback(eb);
3335 } while (bvec >= bio->bi_io_vec);
3336
3337 bio_put(bio);
3338
3339}
3340
3341static int write_one_eb(struct extent_buffer *eb,
3342 struct btrfs_fs_info *fs_info,
3343 struct writeback_control *wbc,
3344 struct extent_page_data *epd)
3345{
3346 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
3347 u64 offset = eb->start;
3348 unsigned long i, num_pages;
Josef Bacikde0022b2012-09-25 14:25:58 -04003349 unsigned long bio_flags = 0;
Josef Bacikd4c7ca82013-04-19 19:49:09 -04003350 int rw = (epd->sync_io ? WRITE_SYNC : WRITE) | REQ_META;
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003351 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003352
3353 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3354 num_pages = num_extent_pages(eb->start, eb->len);
3355 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003356 if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
3357 bio_flags = EXTENT_BIO_TREE_LOG;
3358
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003359 for (i = 0; i < num_pages; i++) {
3360 struct page *p = extent_buffer_page(eb, i);
3361
3362 clear_page_dirty_for_io(p);
3363 set_page_writeback(p);
3364 ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
3365 PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
3366 -1, end_bio_extent_buffer_writepage,
Josef Bacikde0022b2012-09-25 14:25:58 -04003367 0, epd->bio_flags, bio_flags);
3368 epd->bio_flags = bio_flags;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003369 if (ret) {
3370 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3371 SetPageError(p);
3372 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3373 end_extent_buffer_writeback(eb);
3374 ret = -EIO;
3375 break;
3376 }
3377 offset += PAGE_CACHE_SIZE;
3378 update_nr_written(p, wbc, 1);
3379 unlock_page(p);
3380 }
3381
3382 if (unlikely(ret)) {
3383 for (; i < num_pages; i++) {
3384 struct page *p = extent_buffer_page(eb, i);
3385 unlock_page(p);
3386 }
3387 }
3388
3389 return ret;
3390}
3391
3392int btree_write_cache_pages(struct address_space *mapping,
3393 struct writeback_control *wbc)
3394{
3395 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3396 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3397 struct extent_buffer *eb, *prev_eb = NULL;
3398 struct extent_page_data epd = {
3399 .bio = NULL,
3400 .tree = tree,
3401 .extent_locked = 0,
3402 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003403 .bio_flags = 0,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003404 };
3405 int ret = 0;
3406 int done = 0;
3407 int nr_to_write_done = 0;
3408 struct pagevec pvec;
3409 int nr_pages;
3410 pgoff_t index;
3411 pgoff_t end; /* Inclusive */
3412 int scanned = 0;
3413 int tag;
3414
3415 pagevec_init(&pvec, 0);
3416 if (wbc->range_cyclic) {
3417 index = mapping->writeback_index; /* Start from prev offset */
3418 end = -1;
3419 } else {
3420 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3421 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3422 scanned = 1;
3423 }
3424 if (wbc->sync_mode == WB_SYNC_ALL)
3425 tag = PAGECACHE_TAG_TOWRITE;
3426 else
3427 tag = PAGECACHE_TAG_DIRTY;
3428retry:
3429 if (wbc->sync_mode == WB_SYNC_ALL)
3430 tag_pages_for_writeback(mapping, index, end);
3431 while (!done && !nr_to_write_done && (index <= end) &&
3432 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3433 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3434 unsigned i;
3435
3436 scanned = 1;
3437 for (i = 0; i < nr_pages; i++) {
3438 struct page *page = pvec.pages[i];
3439
3440 if (!PagePrivate(page))
3441 continue;
3442
3443 if (!wbc->range_cyclic && page->index > end) {
3444 done = 1;
3445 break;
3446 }
3447
Josef Bacikb5bae262012-09-14 13:43:01 -04003448 spin_lock(&mapping->private_lock);
3449 if (!PagePrivate(page)) {
3450 spin_unlock(&mapping->private_lock);
3451 continue;
3452 }
3453
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003454 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003455
3456 /*
3457 * Shouldn't happen and normally this would be a BUG_ON
3458 * but no sense in crashing the users box for something
3459 * we can survive anyway.
3460 */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003461 if (!eb) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003462 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003463 WARN_ON(1);
3464 continue;
3465 }
3466
Josef Bacikb5bae262012-09-14 13:43:01 -04003467 if (eb == prev_eb) {
3468 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003469 continue;
3470 }
3471
Josef Bacikb5bae262012-09-14 13:43:01 -04003472 ret = atomic_inc_not_zero(&eb->refs);
3473 spin_unlock(&mapping->private_lock);
3474 if (!ret)
3475 continue;
3476
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003477 prev_eb = eb;
3478 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3479 if (!ret) {
3480 free_extent_buffer(eb);
3481 continue;
3482 }
3483
3484 ret = write_one_eb(eb, fs_info, wbc, &epd);
3485 if (ret) {
3486 done = 1;
3487 free_extent_buffer(eb);
3488 break;
3489 }
3490 free_extent_buffer(eb);
3491
3492 /*
3493 * the filesystem may choose to bump up nr_to_write.
3494 * We have to make sure to honor the new nr_to_write
3495 * at any time
3496 */
3497 nr_to_write_done = wbc->nr_to_write <= 0;
3498 }
3499 pagevec_release(&pvec);
3500 cond_resched();
3501 }
3502 if (!scanned && !done) {
3503 /*
3504 * We hit the last page and there is more work to be done: wrap
3505 * back to the start of the file
3506 */
3507 scanned = 1;
3508 index = 0;
3509 goto retry;
3510 }
3511 flush_write_bio(&epd);
3512 return ret;
3513}
3514
Chris Masond1310b22008-01-24 16:13:08 -05003515/**
Chris Mason4bef0842008-09-08 11:18:08 -04003516 * 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 -05003517 * @mapping: address space structure to write
3518 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3519 * @writepage: function called for each page
3520 * @data: data passed to writepage function
3521 *
3522 * If a page is already under I/O, write_cache_pages() skips it, even
3523 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3524 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3525 * and msync() need to guarantee that all the data which was dirty at the time
3526 * the call was made get new I/O started against them. If wbc->sync_mode is
3527 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3528 * existing IO to complete.
3529 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003530static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04003531 struct address_space *mapping,
3532 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003533 writepage_t writepage, void *data,
3534 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05003535{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003536 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003537 int ret = 0;
3538 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003539 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003540 struct pagevec pvec;
3541 int nr_pages;
3542 pgoff_t index;
3543 pgoff_t end; /* Inclusive */
3544 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003545 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003546
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003547 /*
3548 * We have to hold onto the inode so that ordered extents can do their
3549 * work when the IO finishes. The alternative to this is failing to add
3550 * an ordered extent if the igrab() fails there and that is a huge pain
3551 * to deal with, so instead just hold onto the inode throughout the
3552 * writepages operation. If it fails here we are freeing up the inode
3553 * anyway and we'd rather not waste our time writing out stuff that is
3554 * going to be truncated anyway.
3555 */
3556 if (!igrab(inode))
3557 return 0;
3558
Chris Masond1310b22008-01-24 16:13:08 -05003559 pagevec_init(&pvec, 0);
3560 if (wbc->range_cyclic) {
3561 index = mapping->writeback_index; /* Start from prev offset */
3562 end = -1;
3563 } else {
3564 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3565 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003566 scanned = 1;
3567 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003568 if (wbc->sync_mode == WB_SYNC_ALL)
3569 tag = PAGECACHE_TAG_TOWRITE;
3570 else
3571 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003572retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003573 if (wbc->sync_mode == WB_SYNC_ALL)
3574 tag_pages_for_writeback(mapping, index, end);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003575 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003576 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3577 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003578 unsigned i;
3579
3580 scanned = 1;
3581 for (i = 0; i < nr_pages; i++) {
3582 struct page *page = pvec.pages[i];
3583
3584 /*
3585 * At this point we hold neither mapping->tree_lock nor
3586 * lock on the page itself: the page may be truncated or
3587 * invalidated (changing page->mapping to NULL), or even
3588 * swizzled back from swapper_space to tmpfs file
3589 * mapping
3590 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05003591 if (!trylock_page(page)) {
3592 flush_fn(data);
3593 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04003594 }
Chris Masond1310b22008-01-24 16:13:08 -05003595
3596 if (unlikely(page->mapping != mapping)) {
3597 unlock_page(page);
3598 continue;
3599 }
3600
3601 if (!wbc->range_cyclic && page->index > end) {
3602 done = 1;
3603 unlock_page(page);
3604 continue;
3605 }
3606
Chris Masond2c3f4f2008-11-19 12:44:22 -05003607 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003608 if (PageWriteback(page))
3609 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05003610 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003611 }
Chris Masond1310b22008-01-24 16:13:08 -05003612
3613 if (PageWriteback(page) ||
3614 !clear_page_dirty_for_io(page)) {
3615 unlock_page(page);
3616 continue;
3617 }
3618
3619 ret = (*writepage)(page, wbc, data);
3620
3621 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3622 unlock_page(page);
3623 ret = 0;
3624 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003625 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05003626 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003627
3628 /*
3629 * the filesystem may choose to bump up nr_to_write.
3630 * We have to make sure to honor the new nr_to_write
3631 * at any time
3632 */
3633 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05003634 }
3635 pagevec_release(&pvec);
3636 cond_resched();
3637 }
3638 if (!scanned && !done) {
3639 /*
3640 * We hit the last page and there is more work to be done: wrap
3641 * back to the start of the file
3642 */
3643 scanned = 1;
3644 index = 0;
3645 goto retry;
3646 }
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003647 btrfs_add_delayed_iput(inode);
Chris Masond1310b22008-01-24 16:13:08 -05003648 return ret;
3649}
Chris Masond1310b22008-01-24 16:13:08 -05003650
Chris Masonffbd5172009-04-20 15:50:09 -04003651static void flush_epd_write_bio(struct extent_page_data *epd)
3652{
3653 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04003654 int rw = WRITE;
3655 int ret;
3656
Chris Masonffbd5172009-04-20 15:50:09 -04003657 if (epd->sync_io)
Jeff Mahoney355808c2011-10-03 23:23:14 -04003658 rw = WRITE_SYNC;
3659
Josef Bacikde0022b2012-09-25 14:25:58 -04003660 ret = submit_one_bio(rw, epd->bio, 0, epd->bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003661 BUG_ON(ret < 0); /* -ENOMEM */
Chris Masonffbd5172009-04-20 15:50:09 -04003662 epd->bio = NULL;
3663 }
3664}
3665
Chris Masond2c3f4f2008-11-19 12:44:22 -05003666static noinline void flush_write_bio(void *data)
3667{
3668 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04003669 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003670}
3671
Chris Masond1310b22008-01-24 16:13:08 -05003672int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3673 get_extent_t *get_extent,
3674 struct writeback_control *wbc)
3675{
3676 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003677 struct extent_page_data epd = {
3678 .bio = NULL,
3679 .tree = tree,
3680 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003681 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003682 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003683 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05003684 };
Chris Masond1310b22008-01-24 16:13:08 -05003685
Chris Masond1310b22008-01-24 16:13:08 -05003686 ret = __extent_writepage(page, wbc, &epd);
3687
Chris Masonffbd5172009-04-20 15:50:09 -04003688 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003689 return ret;
3690}
Chris Masond1310b22008-01-24 16:13:08 -05003691
Chris Mason771ed682008-11-06 22:02:51 -05003692int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3693 u64 start, u64 end, get_extent_t *get_extent,
3694 int mode)
3695{
3696 int ret = 0;
3697 struct address_space *mapping = inode->i_mapping;
3698 struct page *page;
3699 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3700 PAGE_CACHE_SHIFT;
3701
3702 struct extent_page_data epd = {
3703 .bio = NULL,
3704 .tree = tree,
3705 .get_extent = get_extent,
3706 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04003707 .sync_io = mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003708 .bio_flags = 0,
Chris Mason771ed682008-11-06 22:02:51 -05003709 };
3710 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05003711 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05003712 .nr_to_write = nr_pages * 2,
3713 .range_start = start,
3714 .range_end = end + 1,
3715 };
3716
Chris Masond3977122009-01-05 21:25:51 -05003717 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05003718 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3719 if (clear_page_dirty_for_io(page))
3720 ret = __extent_writepage(page, &wbc_writepages, &epd);
3721 else {
3722 if (tree->ops && tree->ops->writepage_end_io_hook)
3723 tree->ops->writepage_end_io_hook(page, start,
3724 start + PAGE_CACHE_SIZE - 1,
3725 NULL, 1);
3726 unlock_page(page);
3727 }
3728 page_cache_release(page);
3729 start += PAGE_CACHE_SIZE;
3730 }
3731
Chris Masonffbd5172009-04-20 15:50:09 -04003732 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05003733 return ret;
3734}
Chris Masond1310b22008-01-24 16:13:08 -05003735
3736int extent_writepages(struct extent_io_tree *tree,
3737 struct address_space *mapping,
3738 get_extent_t *get_extent,
3739 struct writeback_control *wbc)
3740{
3741 int ret = 0;
3742 struct extent_page_data epd = {
3743 .bio = NULL,
3744 .tree = tree,
3745 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003746 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003747 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003748 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05003749 };
3750
Chris Mason4bef0842008-09-08 11:18:08 -04003751 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003752 __extent_writepage, &epd,
3753 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04003754 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003755 return ret;
3756}
Chris Masond1310b22008-01-24 16:13:08 -05003757
3758int extent_readpages(struct extent_io_tree *tree,
3759 struct address_space *mapping,
3760 struct list_head *pages, unsigned nr_pages,
3761 get_extent_t get_extent)
3762{
3763 struct bio *bio = NULL;
3764 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04003765 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06003766 struct page *pagepool[16];
3767 struct page *page;
3768 int i = 0;
3769 int nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003770
Chris Masond1310b22008-01-24 16:13:08 -05003771 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
Liu Bo67c96842012-07-20 21:43:09 -06003772 page = list_entry(pages->prev, struct page, lru);
Chris Masond1310b22008-01-24 16:13:08 -05003773
3774 prefetchw(&page->flags);
3775 list_del(&page->lru);
Liu Bo67c96842012-07-20 21:43:09 -06003776 if (add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04003777 page->index, GFP_NOFS)) {
Liu Bo67c96842012-07-20 21:43:09 -06003778 page_cache_release(page);
3779 continue;
Chris Masond1310b22008-01-24 16:13:08 -05003780 }
Liu Bo67c96842012-07-20 21:43:09 -06003781
3782 pagepool[nr++] = page;
3783 if (nr < ARRAY_SIZE(pagepool))
3784 continue;
3785 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 nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003791 }
Liu Bo67c96842012-07-20 21:43:09 -06003792 for (i = 0; i < nr; i++) {
3793 __extent_read_full_page(tree, pagepool[i], get_extent,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04003794 &bio, 0, &bio_flags, READ);
Liu Bo67c96842012-07-20 21:43:09 -06003795 page_cache_release(pagepool[i]);
3796 }
3797
Chris Masond1310b22008-01-24 16:13:08 -05003798 BUG_ON(!list_empty(pages));
3799 if (bio)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003800 return submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003801 return 0;
3802}
Chris Masond1310b22008-01-24 16:13:08 -05003803
3804/*
3805 * basic invalidatepage code, this waits on any locked or writeback
3806 * ranges corresponding to the page, and then deletes any extent state
3807 * records from the tree
3808 */
3809int extent_invalidatepage(struct extent_io_tree *tree,
3810 struct page *page, unsigned long offset)
3811{
Josef Bacik2ac55d42010-02-03 19:33:23 +00003812 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003813 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05003814 u64 end = start + PAGE_CACHE_SIZE - 1;
3815 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3816
Qu Wenruofda28322013-02-26 08:10:22 +00003817 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05003818 if (start > end)
3819 return 0;
3820
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003821 lock_extent_bits(tree, start, end, 0, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04003822 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003823 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04003824 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3825 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003826 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003827 return 0;
3828}
Chris Masond1310b22008-01-24 16:13:08 -05003829
3830/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04003831 * a helper for releasepage, this tests for areas of the page that
3832 * are locked or under IO and drops the related state bits if it is safe
3833 * to drop the page.
3834 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003835static int try_release_extent_state(struct extent_map_tree *map,
3836 struct extent_io_tree *tree,
3837 struct page *page, gfp_t mask)
Chris Mason7b13b7b2008-04-18 10:29:50 -04003838{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003839 u64 start = page_offset(page);
Chris Mason7b13b7b2008-04-18 10:29:50 -04003840 u64 end = start + PAGE_CACHE_SIZE - 1;
3841 int ret = 1;
3842
Chris Mason211f90e2008-07-18 11:56:15 -04003843 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04003844 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04003845 ret = 0;
3846 else {
3847 if ((mask & GFP_NOFS) == GFP_NOFS)
3848 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04003849 /*
3850 * at this point we can safely clear everything except the
3851 * locked bit and the nodatasum bit
3852 */
Chris Masone3f24cc2011-02-14 12:52:08 -05003853 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04003854 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3855 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05003856
3857 /* if clear_extent_bit failed for enomem reasons,
3858 * we can't allow the release to continue.
3859 */
3860 if (ret < 0)
3861 ret = 0;
3862 else
3863 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003864 }
3865 return ret;
3866}
Chris Mason7b13b7b2008-04-18 10:29:50 -04003867
3868/*
Chris Masond1310b22008-01-24 16:13:08 -05003869 * a helper for releasepage. As long as there are no locked extents
3870 * in the range corresponding to the page, both state records and extent
3871 * map records are removed
3872 */
3873int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05003874 struct extent_io_tree *tree, struct page *page,
3875 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05003876{
3877 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003878 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05003879 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003880
Chris Mason70dec802008-01-29 09:59:12 -05003881 if ((mask & __GFP_WAIT) &&
3882 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05003883 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05003884 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05003885 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04003886 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05003887 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09003888 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04003889 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003890 break;
3891 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003892 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3893 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04003894 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003895 free_extent_map(em);
3896 break;
3897 }
3898 if (!test_range_bit(tree, em->start,
3899 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04003900 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04003901 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05003902 remove_extent_mapping(map, em);
3903 /* once for the rb tree */
3904 free_extent_map(em);
3905 }
3906 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04003907 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003908
3909 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05003910 free_extent_map(em);
3911 }
Chris Masond1310b22008-01-24 16:13:08 -05003912 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04003913 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003914}
Chris Masond1310b22008-01-24 16:13:08 -05003915
Chris Masonec29ed52011-02-23 16:23:20 -05003916/*
3917 * helper function for fiemap, which doesn't want to see any holes.
3918 * This maps until we find something past 'last'
3919 */
3920static struct extent_map *get_extent_skip_holes(struct inode *inode,
3921 u64 offset,
3922 u64 last,
3923 get_extent_t *get_extent)
3924{
3925 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3926 struct extent_map *em;
3927 u64 len;
3928
3929 if (offset >= last)
3930 return NULL;
3931
3932 while(1) {
3933 len = last - offset;
3934 if (len == 0)
3935 break;
Qu Wenruofda28322013-02-26 08:10:22 +00003936 len = ALIGN(len, sectorsize);
Chris Masonec29ed52011-02-23 16:23:20 -05003937 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02003938 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05003939 return em;
3940
3941 /* if this isn't a hole return it */
3942 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3943 em->block_start != EXTENT_MAP_HOLE) {
3944 return em;
3945 }
3946
3947 /* this is a hole, advance to the next extent */
3948 offset = extent_map_end(em);
3949 free_extent_map(em);
3950 if (offset >= last)
3951 break;
3952 }
3953 return NULL;
3954}
3955
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003956int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3957 __u64 start, __u64 len, get_extent_t *get_extent)
3958{
Josef Bacik975f84f2010-11-23 19:36:57 +00003959 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003960 u64 off = start;
3961 u64 max = start + len;
3962 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00003963 u32 found_type;
3964 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05003965 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003966 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003967 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00003968 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003969 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003970 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00003971 struct btrfs_path *path;
3972 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003973 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003974 u64 em_start = 0;
3975 u64 em_len = 0;
3976 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003977 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003978
3979 if (len == 0)
3980 return -EINVAL;
3981
Josef Bacik975f84f2010-11-23 19:36:57 +00003982 path = btrfs_alloc_path();
3983 if (!path)
3984 return -ENOMEM;
3985 path->leave_spinning = 1;
3986
Josef Bacik4d479cf2011-11-17 11:34:31 -05003987 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3988 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3989
Chris Masonec29ed52011-02-23 16:23:20 -05003990 /*
3991 * lookup the last file extent. We're not using i_size here
3992 * because there might be preallocation past i_size
3993 */
Josef Bacik975f84f2010-11-23 19:36:57 +00003994 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
Li Zefan33345d012011-04-20 10:31:50 +08003995 path, btrfs_ino(inode), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00003996 if (ret < 0) {
3997 btrfs_free_path(path);
3998 return ret;
3999 }
4000 WARN_ON(!ret);
4001 path->slots[0]--;
4002 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4003 struct btrfs_file_extent_item);
4004 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
4005 found_type = btrfs_key_type(&found_key);
4006
Chris Masonec29ed52011-02-23 16:23:20 -05004007 /* No extents, but there might be delalloc bits */
Li Zefan33345d012011-04-20 10:31:50 +08004008 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00004009 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05004010 /* have to trust i_size as the end */
4011 last = (u64)-1;
4012 last_for_get_extent = isize;
4013 } else {
4014 /*
4015 * remember the start of the last extent. There are a
4016 * bunch of different factors that go into the length of the
4017 * extent, so its much less complex to remember where it started
4018 */
4019 last = found_key.offset;
4020 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00004021 }
Josef Bacik975f84f2010-11-23 19:36:57 +00004022 btrfs_free_path(path);
4023
Chris Masonec29ed52011-02-23 16:23:20 -05004024 /*
4025 * we might have some extents allocated but more delalloc past those
4026 * extents. so, we trust isize unless the start of the last extent is
4027 * beyond isize
4028 */
4029 if (last < isize) {
4030 last = (u64)-1;
4031 last_for_get_extent = isize;
4032 }
4033
Liu Boa52f4cd2013-05-01 16:23:41 +00004034 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004035 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05004036
Josef Bacik4d479cf2011-11-17 11:34:31 -05004037 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05004038 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004039 if (!em)
4040 goto out;
4041 if (IS_ERR(em)) {
4042 ret = PTR_ERR(em);
4043 goto out;
4044 }
Josef Bacik975f84f2010-11-23 19:36:57 +00004045
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004046 while (!end) {
Josef Bacikb76bb702013-07-05 13:52:51 -04004047 u64 offset_in_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004048
Chris Masonea8efc72011-03-08 11:54:40 -05004049 /* break if the extent we found is outside the range */
4050 if (em->start >= max || extent_map_end(em) < off)
4051 break;
4052
4053 /*
4054 * get_extent may return an extent that starts before our
4055 * requested range. We have to make sure the ranges
4056 * we return to fiemap always move forward and don't
4057 * overlap, so adjust the offsets here
4058 */
4059 em_start = max(em->start, off);
4060
4061 /*
4062 * record the offset from the start of the extent
Josef Bacikb76bb702013-07-05 13:52:51 -04004063 * for adjusting the disk offset below. Only do this if the
4064 * extent isn't compressed since our in ram offset may be past
4065 * what we have actually allocated on disk.
Chris Masonea8efc72011-03-08 11:54:40 -05004066 */
Josef Bacikb76bb702013-07-05 13:52:51 -04004067 if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4068 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05004069 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05004070 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05004071 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004072 disko = 0;
4073 flags = 0;
4074
Chris Masonea8efc72011-03-08 11:54:40 -05004075 /*
4076 * bump off for our next call to get_extent
4077 */
4078 off = extent_map_end(em);
4079 if (off >= max)
4080 end = 1;
4081
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004082 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004083 end = 1;
4084 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004085 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004086 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4087 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004088 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004089 flags |= (FIEMAP_EXTENT_DELALLOC |
4090 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004091 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05004092 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004093 }
4094 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4095 flags |= FIEMAP_EXTENT_ENCODED;
4096
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004097 free_extent_map(em);
4098 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05004099 if ((em_start >= last) || em_len == (u64)-1 ||
4100 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004101 flags |= FIEMAP_EXTENT_LAST;
4102 end = 1;
4103 }
4104
Chris Masonec29ed52011-02-23 16:23:20 -05004105 /* now scan forward to see if this is really the last extent. */
4106 em = get_extent_skip_holes(inode, off, last_for_get_extent,
4107 get_extent);
4108 if (IS_ERR(em)) {
4109 ret = PTR_ERR(em);
4110 goto out;
4111 }
4112 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00004113 flags |= FIEMAP_EXTENT_LAST;
4114 end = 1;
4115 }
Chris Masonec29ed52011-02-23 16:23:20 -05004116 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
4117 em_len, flags);
4118 if (ret)
4119 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004120 }
4121out_free:
4122 free_extent_map(em);
4123out:
Liu Boa52f4cd2013-05-01 16:23:41 +00004124 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004125 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004126 return ret;
4127}
4128
Chris Mason727011e2010-08-06 13:21:20 -04004129static void __free_extent_buffer(struct extent_buffer *eb)
4130{
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004131 btrfs_leak_debug_del(&eb->leak_list);
Chris Mason727011e2010-08-06 13:21:20 -04004132 kmem_cache_free(extent_buffer_cache, eb);
4133}
4134
Chris Masond1310b22008-01-24 16:13:08 -05004135static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
4136 u64 start,
4137 unsigned long len,
4138 gfp_t mask)
4139{
4140 struct extent_buffer *eb = NULL;
4141
Chris Masond1310b22008-01-24 16:13:08 -05004142 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00004143 if (eb == NULL)
4144 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004145 eb->start = start;
4146 eb->len = len;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004147 eb->tree = tree;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004148 eb->bflags = 0;
Chris Masonbd681512011-07-16 15:23:14 -04004149 rwlock_init(&eb->lock);
4150 atomic_set(&eb->write_locks, 0);
4151 atomic_set(&eb->read_locks, 0);
4152 atomic_set(&eb->blocking_readers, 0);
4153 atomic_set(&eb->blocking_writers, 0);
4154 atomic_set(&eb->spinning_readers, 0);
4155 atomic_set(&eb->spinning_writers, 0);
Arne Jansen5b25f702011-09-13 10:55:48 +02004156 eb->lock_nested = 0;
Chris Masonbd681512011-07-16 15:23:14 -04004157 init_waitqueue_head(&eb->write_lock_wq);
4158 init_waitqueue_head(&eb->read_lock_wq);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004159
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004160 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4161
Josef Bacik3083ee22012-03-09 16:01:49 -05004162 spin_lock_init(&eb->refs_lock);
Chris Masond1310b22008-01-24 16:13:08 -05004163 atomic_set(&eb->refs, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004164 atomic_set(&eb->io_pages, 0);
Chris Mason727011e2010-08-06 13:21:20 -04004165
David Sterbab8dae312013-02-28 14:54:18 +00004166 /*
4167 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4168 */
4169 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4170 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4171 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05004172
4173 return eb;
4174}
4175
Jan Schmidt815a51c2012-05-16 17:00:02 +02004176struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4177{
4178 unsigned long i;
4179 struct page *p;
4180 struct extent_buffer *new;
4181 unsigned long num_pages = num_extent_pages(src->start, src->len);
4182
4183 new = __alloc_extent_buffer(NULL, src->start, src->len, GFP_ATOMIC);
4184 if (new == NULL)
4185 return NULL;
4186
4187 for (i = 0; i < num_pages; i++) {
4188 p = alloc_page(GFP_ATOMIC);
4189 BUG_ON(!p);
4190 attach_extent_buffer_page(new, p);
4191 WARN_ON(PageDirty(p));
4192 SetPageUptodate(p);
4193 new->pages[i] = p;
4194 }
4195
4196 copy_extent_buffer(new, src, 0, 0, src->len);
4197 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
4198 set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
4199
4200 return new;
4201}
4202
4203struct extent_buffer *alloc_dummy_extent_buffer(u64 start, unsigned long len)
4204{
4205 struct extent_buffer *eb;
4206 unsigned long num_pages = num_extent_pages(0, len);
4207 unsigned long i;
4208
4209 eb = __alloc_extent_buffer(NULL, start, len, GFP_ATOMIC);
4210 if (!eb)
4211 return NULL;
4212
4213 for (i = 0; i < num_pages; i++) {
4214 eb->pages[i] = alloc_page(GFP_ATOMIC);
4215 if (!eb->pages[i])
4216 goto err;
4217 }
4218 set_extent_buffer_uptodate(eb);
4219 btrfs_set_header_nritems(eb, 0);
4220 set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4221
4222 return eb;
4223err:
Stefan Behrens84167d12012-10-11 07:25:16 -06004224 for (; i > 0; i--)
4225 __free_page(eb->pages[i - 1]);
Jan Schmidt815a51c2012-05-16 17:00:02 +02004226 __free_extent_buffer(eb);
4227 return NULL;
4228}
4229
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004230static int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004231{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004232 return (atomic_read(&eb->io_pages) ||
4233 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4234 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004235}
4236
Miao Xie897ca6e2010-10-26 20:57:29 -04004237/*
4238 * Helper for releasing extent buffer page.
4239 */
4240static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
4241 unsigned long start_idx)
4242{
4243 unsigned long index;
Wang Sheng-Hui39bab872012-04-06 14:35:31 +08004244 unsigned long num_pages;
Miao Xie897ca6e2010-10-26 20:57:29 -04004245 struct page *page;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004246 int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
Miao Xie897ca6e2010-10-26 20:57:29 -04004247
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004248 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e2010-10-26 20:57:29 -04004249
Wang Sheng-Hui39bab872012-04-06 14:35:31 +08004250 num_pages = num_extent_pages(eb->start, eb->len);
4251 index = start_idx + num_pages;
Miao Xie897ca6e2010-10-26 20:57:29 -04004252 if (start_idx >= index)
4253 return;
4254
4255 do {
4256 index--;
4257 page = extent_buffer_page(eb, index);
Jan Schmidt815a51c2012-05-16 17:00:02 +02004258 if (page && mapped) {
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004259 spin_lock(&page->mapping->private_lock);
4260 /*
4261 * We do this since we'll remove the pages after we've
4262 * removed the eb from the radix tree, so we could race
4263 * and have this page now attached to the new eb. So
4264 * only clear page_private if it's still connected to
4265 * this eb.
4266 */
4267 if (PagePrivate(page) &&
4268 page->private == (unsigned long)eb) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004269 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Josef Bacik3083ee22012-03-09 16:01:49 -05004270 BUG_ON(PageDirty(page));
4271 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004272 /*
4273 * We need to make sure we haven't be attached
4274 * to a new eb.
4275 */
4276 ClearPagePrivate(page);
4277 set_page_private(page, 0);
4278 /* One for the page private */
4279 page_cache_release(page);
4280 }
4281 spin_unlock(&page->mapping->private_lock);
4282
Jan Schmidt815a51c2012-05-16 17:00:02 +02004283 }
4284 if (page) {
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004285 /* One for when we alloced the page */
Miao Xie897ca6e2010-10-26 20:57:29 -04004286 page_cache_release(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004287 }
Miao Xie897ca6e2010-10-26 20:57:29 -04004288 } while (index != start_idx);
4289}
4290
4291/*
4292 * Helper for releasing the extent buffer.
4293 */
4294static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4295{
4296 btrfs_release_extent_buffer_page(eb, 0);
4297 __free_extent_buffer(eb);
4298}
4299
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004300static void check_buffer_tree_ref(struct extent_buffer *eb)
4301{
Chris Mason242e18c2013-01-29 17:49:37 -05004302 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004303 /* the ref bit is tricky. We have to make sure it is set
4304 * if we have the buffer dirty. Otherwise the
4305 * code to free a buffer can end up dropping a dirty
4306 * page
4307 *
4308 * Once the ref bit is set, it won't go away while the
4309 * buffer is dirty or in writeback, and it also won't
4310 * go away while we have the reference count on the
4311 * eb bumped.
4312 *
4313 * We can't just set the ref bit without bumping the
4314 * ref on the eb because free_extent_buffer might
4315 * see the ref bit and try to clear it. If this happens
4316 * free_extent_buffer might end up dropping our original
4317 * ref by mistake and freeing the page before we are able
4318 * to add one more ref.
4319 *
4320 * So bump the ref count first, then set the bit. If someone
4321 * beat us to it, drop the ref we added.
4322 */
Chris Mason242e18c2013-01-29 17:49:37 -05004323 refs = atomic_read(&eb->refs);
4324 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4325 return;
4326
Josef Bacik594831c2012-07-20 16:11:08 -04004327 spin_lock(&eb->refs_lock);
4328 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004329 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004330 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004331}
4332
Josef Bacik5df42352012-03-15 18:24:42 -04004333static void mark_extent_buffer_accessed(struct extent_buffer *eb)
4334{
4335 unsigned long num_pages, i;
4336
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004337 check_buffer_tree_ref(eb);
4338
Josef Bacik5df42352012-03-15 18:24:42 -04004339 num_pages = num_extent_pages(eb->start, eb->len);
4340 for (i = 0; i < num_pages; i++) {
4341 struct page *p = extent_buffer_page(eb, i);
4342 mark_page_accessed(p);
4343 }
4344}
4345
Chris Masond1310b22008-01-24 16:13:08 -05004346struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
Chris Mason727011e2010-08-06 13:21:20 -04004347 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004348{
4349 unsigned long num_pages = num_extent_pages(start, len);
4350 unsigned long i;
4351 unsigned long index = start >> PAGE_CACHE_SHIFT;
4352 struct extent_buffer *eb;
Chris Mason6af118c2008-07-22 11:18:07 -04004353 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004354 struct page *p;
4355 struct address_space *mapping = tree->mapping;
4356 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004357 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004358
Miao Xie19fe0a82010-10-26 20:57:29 -04004359 rcu_read_lock();
4360 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4361 if (eb && atomic_inc_not_zero(&eb->refs)) {
4362 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004363 mark_extent_buffer_accessed(eb);
Chris Mason6af118c2008-07-22 11:18:07 -04004364 return eb;
4365 }
Miao Xie19fe0a82010-10-26 20:57:29 -04004366 rcu_read_unlock();
Chris Mason6af118c2008-07-22 11:18:07 -04004367
David Sterbaba144192011-04-21 01:12:06 +02004368 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
Peter2b114d12008-04-01 11:21:40 -04004369 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004370 return NULL;
4371
Chris Mason727011e2010-08-06 13:21:20 -04004372 for (i = 0; i < num_pages; i++, index++) {
Chris Masona6591712011-07-19 12:04:14 -04004373 p = find_or_create_page(mapping, index, GFP_NOFS);
Josef Bacik4804b382012-10-05 16:43:45 -04004374 if (!p)
Chris Mason6af118c2008-07-22 11:18:07 -04004375 goto free_eb;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004376
4377 spin_lock(&mapping->private_lock);
4378 if (PagePrivate(p)) {
4379 /*
4380 * We could have already allocated an eb for this page
4381 * and attached one so lets see if we can get a ref on
4382 * the existing eb, and if we can we know it's good and
4383 * we can just return that one, else we know we can just
4384 * overwrite page->private.
4385 */
4386 exists = (struct extent_buffer *)p->private;
4387 if (atomic_inc_not_zero(&exists->refs)) {
4388 spin_unlock(&mapping->private_lock);
4389 unlock_page(p);
Josef Bacik17de39a2012-05-04 15:16:06 -04004390 page_cache_release(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004391 mark_extent_buffer_accessed(exists);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004392 goto free_eb;
4393 }
4394
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004395 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004396 * Do this so attach doesn't complain and we need to
4397 * drop the ref the old guy had.
4398 */
4399 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004400 WARN_ON(PageDirty(p));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004401 page_cache_release(p);
Chris Masond1310b22008-01-24 16:13:08 -05004402 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004403 attach_extent_buffer_page(eb, p);
4404 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004405 WARN_ON(PageDirty(p));
Chris Masond1310b22008-01-24 16:13:08 -05004406 mark_page_accessed(p);
Chris Mason727011e2010-08-06 13:21:20 -04004407 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05004408 if (!PageUptodate(p))
4409 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05004410
4411 /*
4412 * see below about how we avoid a nasty race with release page
4413 * and why we unlock later
4414 */
Chris Masond1310b22008-01-24 16:13:08 -05004415 }
4416 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004417 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05004418again:
Miao Xie19fe0a82010-10-26 20:57:29 -04004419 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4420 if (ret)
4421 goto free_eb;
4422
Chris Mason6af118c2008-07-22 11:18:07 -04004423 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004424 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
4425 if (ret == -EEXIST) {
4426 exists = radix_tree_lookup(&tree->buffer,
4427 start >> PAGE_CACHE_SHIFT);
Josef Bacik115391d2012-03-09 09:51:43 -05004428 if (!atomic_inc_not_zero(&exists->refs)) {
4429 spin_unlock(&tree->buffer_lock);
4430 radix_tree_preload_end();
Josef Bacik115391d2012-03-09 09:51:43 -05004431 exists = NULL;
4432 goto again;
4433 }
Chris Mason6af118c2008-07-22 11:18:07 -04004434 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004435 radix_tree_preload_end();
Josef Bacik5df42352012-03-15 18:24:42 -04004436 mark_extent_buffer_accessed(exists);
Chris Mason6af118c2008-07-22 11:18:07 -04004437 goto free_eb;
4438 }
Chris Mason6af118c2008-07-22 11:18:07 -04004439 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004440 check_buffer_tree_ref(eb);
Yan, Zhengf044ba72010-02-04 08:46:56 +00004441 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004442 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05004443
4444 /*
4445 * there is a race where release page may have
4446 * tried to find this extent buffer in the radix
4447 * but failed. It will tell the VM it is safe to
4448 * reclaim the, and it will clear the page private bit.
4449 * We must make sure to set the page private bit properly
4450 * after the extent buffer is in the radix tree so
4451 * it doesn't get lost
4452 */
Chris Mason727011e2010-08-06 13:21:20 -04004453 SetPageChecked(eb->pages[0]);
4454 for (i = 1; i < num_pages; i++) {
4455 p = extent_buffer_page(eb, i);
Chris Mason727011e2010-08-06 13:21:20 -04004456 ClearPageChecked(p);
4457 unlock_page(p);
4458 }
4459 unlock_page(eb->pages[0]);
Chris Masond1310b22008-01-24 16:13:08 -05004460 return eb;
4461
Chris Mason6af118c2008-07-22 11:18:07 -04004462free_eb:
Chris Mason727011e2010-08-06 13:21:20 -04004463 for (i = 0; i < num_pages; i++) {
4464 if (eb->pages[i])
4465 unlock_page(eb->pages[i]);
4466 }
Chris Masoneb14ab82011-02-10 12:35:00 -05004467
Josef Bacik17de39a2012-05-04 15:16:06 -04004468 WARN_ON(!atomic_dec_and_test(&eb->refs));
Miao Xie897ca6e2010-10-26 20:57:29 -04004469 btrfs_release_extent_buffer(eb);
Chris Mason6af118c2008-07-22 11:18:07 -04004470 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05004471}
Chris Masond1310b22008-01-24 16:13:08 -05004472
4473struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
David Sterbaf09d1f62011-04-21 01:08:01 +02004474 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004475{
Chris Masond1310b22008-01-24 16:13:08 -05004476 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05004477
Miao Xie19fe0a82010-10-26 20:57:29 -04004478 rcu_read_lock();
4479 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4480 if (eb && atomic_inc_not_zero(&eb->refs)) {
4481 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004482 mark_extent_buffer_accessed(eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04004483 return eb;
4484 }
4485 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04004486
Miao Xie19fe0a82010-10-26 20:57:29 -04004487 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004488}
Chris Masond1310b22008-01-24 16:13:08 -05004489
Josef Bacik3083ee22012-03-09 16:01:49 -05004490static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4491{
4492 struct extent_buffer *eb =
4493 container_of(head, struct extent_buffer, rcu_head);
4494
4495 __free_extent_buffer(eb);
4496}
4497
Josef Bacik3083ee22012-03-09 16:01:49 -05004498/* Expects to have eb->eb_lock already held */
David Sterbaf7a52a42013-04-26 14:56:29 +00004499static int release_extent_buffer(struct extent_buffer *eb)
Josef Bacik3083ee22012-03-09 16:01:49 -05004500{
4501 WARN_ON(atomic_read(&eb->refs) == 0);
4502 if (atomic_dec_and_test(&eb->refs)) {
Jan Schmidt815a51c2012-05-16 17:00:02 +02004503 if (test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) {
4504 spin_unlock(&eb->refs_lock);
4505 } else {
4506 struct extent_io_tree *tree = eb->tree;
Josef Bacik3083ee22012-03-09 16:01:49 -05004507
Jan Schmidt815a51c2012-05-16 17:00:02 +02004508 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05004509
Jan Schmidt815a51c2012-05-16 17:00:02 +02004510 spin_lock(&tree->buffer_lock);
4511 radix_tree_delete(&tree->buffer,
4512 eb->start >> PAGE_CACHE_SHIFT);
4513 spin_unlock(&tree->buffer_lock);
4514 }
Josef Bacik3083ee22012-03-09 16:01:49 -05004515
4516 /* Should be safe to release our pages at this point */
4517 btrfs_release_extent_buffer_page(eb, 0);
Josef Bacik3083ee22012-03-09 16:01:49 -05004518 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04004519 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05004520 }
4521 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04004522
4523 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05004524}
4525
Chris Masond1310b22008-01-24 16:13:08 -05004526void free_extent_buffer(struct extent_buffer *eb)
4527{
Chris Mason242e18c2013-01-29 17:49:37 -05004528 int refs;
4529 int old;
Chris Masond1310b22008-01-24 16:13:08 -05004530 if (!eb)
4531 return;
4532
Chris Mason242e18c2013-01-29 17:49:37 -05004533 while (1) {
4534 refs = atomic_read(&eb->refs);
4535 if (refs <= 3)
4536 break;
4537 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
4538 if (old == refs)
4539 return;
4540 }
4541
Josef Bacik3083ee22012-03-09 16:01:49 -05004542 spin_lock(&eb->refs_lock);
4543 if (atomic_read(&eb->refs) == 2 &&
Jan Schmidt815a51c2012-05-16 17:00:02 +02004544 test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
4545 atomic_dec(&eb->refs);
4546
4547 if (atomic_read(&eb->refs) == 2 &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004548 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004549 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004550 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4551 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05004552
Josef Bacik3083ee22012-03-09 16:01:49 -05004553 /*
4554 * I know this is terrible, but it's temporary until we stop tracking
4555 * the uptodate bits and such for the extent buffers.
4556 */
David Sterbaf7a52a42013-04-26 14:56:29 +00004557 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05004558}
Chris Masond1310b22008-01-24 16:13:08 -05004559
Josef Bacik3083ee22012-03-09 16:01:49 -05004560void free_extent_buffer_stale(struct extent_buffer *eb)
4561{
4562 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004563 return;
4564
Josef Bacik3083ee22012-03-09 16:01:49 -05004565 spin_lock(&eb->refs_lock);
4566 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
4567
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004568 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004569 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4570 atomic_dec(&eb->refs);
David Sterbaf7a52a42013-04-26 14:56:29 +00004571 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05004572}
4573
Chris Mason1d4284b2012-03-28 20:31:37 -04004574void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004575{
Chris Masond1310b22008-01-24 16:13:08 -05004576 unsigned long i;
4577 unsigned long num_pages;
4578 struct page *page;
4579
Chris Masond1310b22008-01-24 16:13:08 -05004580 num_pages = num_extent_pages(eb->start, eb->len);
4581
4582 for (i = 0; i < num_pages; i++) {
4583 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04004584 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05004585 continue;
4586
Chris Masona61e6f22008-07-22 11:18:08 -04004587 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05004588 WARN_ON(!PagePrivate(page));
4589
Chris Masond1310b22008-01-24 16:13:08 -05004590 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004591 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05004592 if (!PageDirty(page)) {
4593 radix_tree_tag_clear(&page->mapping->page_tree,
4594 page_index(page),
4595 PAGECACHE_TAG_DIRTY);
4596 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004597 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04004598 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04004599 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05004600 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004601 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05004602}
Chris Masond1310b22008-01-24 16:13:08 -05004603
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004604int set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004605{
4606 unsigned long i;
4607 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04004608 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004609
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004610 check_buffer_tree_ref(eb);
4611
Chris Masonb9473432009-03-13 11:00:37 -04004612 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004613
Chris Masond1310b22008-01-24 16:13:08 -05004614 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik3083ee22012-03-09 16:01:49 -05004615 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004616 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
4617
Chris Masonb9473432009-03-13 11:00:37 -04004618 for (i = 0; i < num_pages; i++)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004619 set_page_dirty(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04004620 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05004621}
Chris Masond1310b22008-01-24 16:13:08 -05004622
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004623int clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04004624{
4625 unsigned long i;
4626 struct page *page;
4627 unsigned long num_pages;
4628
Chris Masonb4ce94d2009-02-04 09:25:08 -05004629 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004630 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason1259ab72008-05-12 13:39:03 -04004631 for (i = 0; i < num_pages; i++) {
4632 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04004633 if (page)
4634 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04004635 }
4636 return 0;
4637}
4638
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004639int set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004640{
4641 unsigned long i;
4642 struct page *page;
4643 unsigned long num_pages;
4644
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004645 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004646 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05004647 for (i = 0; i < num_pages; i++) {
4648 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004649 SetPageUptodate(page);
4650 }
4651 return 0;
4652}
Chris Masond1310b22008-01-24 16:13:08 -05004653
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004654int extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004655{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004656 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004657}
Chris Masond1310b22008-01-24 16:13:08 -05004658
4659int read_extent_buffer_pages(struct extent_io_tree *tree,
Arne Jansenbb82ab82011-06-10 14:06:53 +02004660 struct extent_buffer *eb, u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04004661 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05004662{
4663 unsigned long i;
4664 unsigned long start_i;
4665 struct page *page;
4666 int err;
4667 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04004668 int locked_pages = 0;
4669 int all_uptodate = 1;
Chris Masond1310b22008-01-24 16:13:08 -05004670 unsigned long num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04004671 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004672 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004673 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004674
Chris Masonb4ce94d2009-02-04 09:25:08 -05004675 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05004676 return 0;
4677
Chris Masond1310b22008-01-24 16:13:08 -05004678 if (start) {
4679 WARN_ON(start < eb->start);
4680 start_i = (start >> PAGE_CACHE_SHIFT) -
4681 (eb->start >> PAGE_CACHE_SHIFT);
4682 } else {
4683 start_i = 0;
4684 }
4685
4686 num_pages = num_extent_pages(eb->start, eb->len);
4687 for (i = start_i; i < num_pages; i++) {
4688 page = extent_buffer_page(eb, i);
Arne Jansenbb82ab82011-06-10 14:06:53 +02004689 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04004690 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04004691 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05004692 } else {
4693 lock_page(page);
4694 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004695 locked_pages++;
Chris Mason727011e2010-08-06 13:21:20 -04004696 if (!PageUptodate(page)) {
4697 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04004698 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04004699 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004700 }
4701 if (all_uptodate) {
4702 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004703 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04004704 goto unlock_exit;
4705 }
4706
Josef Bacikea466792012-03-26 21:57:36 -04004707 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04004708 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004709 atomic_set(&eb->io_pages, num_reads);
Chris Masonce9adaa2008-04-09 16:28:12 -04004710 for (i = start_i; i < num_pages; i++) {
4711 page = extent_buffer_page(eb, i);
Chris Masonce9adaa2008-04-09 16:28:12 -04004712 if (!PageUptodate(page)) {
Chris Masonf1885912008-04-09 16:28:12 -04004713 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05004714 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04004715 get_extent, &bio,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04004716 mirror_num, &bio_flags,
4717 READ | REQ_META);
Chris Masond3977122009-01-05 21:25:51 -05004718 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05004719 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05004720 } else {
4721 unlock_page(page);
4722 }
4723 }
4724
Jeff Mahoney355808c2011-10-03 23:23:14 -04004725 if (bio) {
Josef Bacikd4c7ca82013-04-19 19:49:09 -04004726 err = submit_one_bio(READ | REQ_META, bio, mirror_num,
4727 bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004728 if (err)
4729 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04004730 }
Chris Masona86c12c2008-02-07 10:50:54 -05004731
Arne Jansenbb82ab82011-06-10 14:06:53 +02004732 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05004733 return ret;
Chris Masond3977122009-01-05 21:25:51 -05004734
Chris Masond1310b22008-01-24 16:13:08 -05004735 for (i = start_i; i < num_pages; i++) {
4736 page = extent_buffer_page(eb, i);
4737 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05004738 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05004739 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05004740 }
Chris Masond3977122009-01-05 21:25:51 -05004741
Chris Masond1310b22008-01-24 16:13:08 -05004742 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04004743
4744unlock_exit:
4745 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05004746 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004747 page = extent_buffer_page(eb, i);
4748 i++;
4749 unlock_page(page);
4750 locked_pages--;
4751 }
4752 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004753}
Chris Masond1310b22008-01-24 16:13:08 -05004754
4755void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4756 unsigned long start,
4757 unsigned long len)
4758{
4759 size_t cur;
4760 size_t offset;
4761 struct page *page;
4762 char *kaddr;
4763 char *dst = (char *)dstv;
4764 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4765 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004766
4767 WARN_ON(start > eb->len);
4768 WARN_ON(start + len > eb->start + eb->len);
4769
4770 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4771
Chris Masond3977122009-01-05 21:25:51 -05004772 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004773 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004774
4775 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04004776 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004777 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004778
4779 dst += cur;
4780 len -= cur;
4781 offset = 0;
4782 i++;
4783 }
4784}
Chris Masond1310b22008-01-24 16:13:08 -05004785
4786int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04004787 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05004788 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04004789 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05004790{
4791 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4792 char *kaddr;
4793 struct page *p;
4794 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4795 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4796 unsigned long end_i = (start_offset + start + min_len - 1) >>
4797 PAGE_CACHE_SHIFT;
4798
4799 if (i != end_i)
4800 return -EINVAL;
4801
4802 if (i == 0) {
4803 offset = start_offset;
4804 *map_start = 0;
4805 } else {
4806 offset = 0;
4807 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4808 }
Chris Masond3977122009-01-05 21:25:51 -05004809
Chris Masond1310b22008-01-24 16:13:08 -05004810 if (start + min_len > eb->len) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004811 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
Chris Masond3977122009-01-05 21:25:51 -05004812 "wanted %lu %lu\n", (unsigned long long)eb->start,
4813 eb->len, start, min_len);
Josef Bacik850265332011-03-15 14:52:12 -04004814 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05004815 }
4816
4817 p = extent_buffer_page(eb, i);
Chris Masona6591712011-07-19 12:04:14 -04004818 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05004819 *map = kaddr + offset;
4820 *map_len = PAGE_CACHE_SIZE - offset;
4821 return 0;
4822}
Chris Masond1310b22008-01-24 16:13:08 -05004823
Chris Masond1310b22008-01-24 16:13:08 -05004824int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4825 unsigned long start,
4826 unsigned long len)
4827{
4828 size_t cur;
4829 size_t offset;
4830 struct page *page;
4831 char *kaddr;
4832 char *ptr = (char *)ptrv;
4833 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4834 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4835 int ret = 0;
4836
4837 WARN_ON(start > eb->len);
4838 WARN_ON(start + len > eb->start + eb->len);
4839
4840 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4841
Chris Masond3977122009-01-05 21:25:51 -05004842 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004843 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004844
4845 cur = min(len, (PAGE_CACHE_SIZE - offset));
4846
Chris Masona6591712011-07-19 12:04:14 -04004847 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004848 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004849 if (ret)
4850 break;
4851
4852 ptr += cur;
4853 len -= cur;
4854 offset = 0;
4855 i++;
4856 }
4857 return ret;
4858}
Chris Masond1310b22008-01-24 16:13:08 -05004859
4860void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4861 unsigned long start, unsigned long len)
4862{
4863 size_t cur;
4864 size_t offset;
4865 struct page *page;
4866 char *kaddr;
4867 char *src = (char *)srcv;
4868 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4869 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4870
4871 WARN_ON(start > eb->len);
4872 WARN_ON(start + len > eb->start + eb->len);
4873
4874 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4875
Chris Masond3977122009-01-05 21:25:51 -05004876 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004877 page = extent_buffer_page(eb, i);
4878 WARN_ON(!PageUptodate(page));
4879
4880 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004881 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004882 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004883
4884 src += cur;
4885 len -= cur;
4886 offset = 0;
4887 i++;
4888 }
4889}
Chris Masond1310b22008-01-24 16:13:08 -05004890
4891void memset_extent_buffer(struct extent_buffer *eb, char c,
4892 unsigned long start, unsigned long len)
4893{
4894 size_t cur;
4895 size_t offset;
4896 struct page *page;
4897 char *kaddr;
4898 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4899 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4900
4901 WARN_ON(start > eb->len);
4902 WARN_ON(start + len > eb->start + eb->len);
4903
4904 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4905
Chris Masond3977122009-01-05 21:25:51 -05004906 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004907 page = extent_buffer_page(eb, i);
4908 WARN_ON(!PageUptodate(page));
4909
4910 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004911 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004912 memset(kaddr + offset, c, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004913
4914 len -= cur;
4915 offset = 0;
4916 i++;
4917 }
4918}
Chris Masond1310b22008-01-24 16:13:08 -05004919
4920void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4921 unsigned long dst_offset, unsigned long src_offset,
4922 unsigned long len)
4923{
4924 u64 dst_len = dst->len;
4925 size_t cur;
4926 size_t offset;
4927 struct page *page;
4928 char *kaddr;
4929 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4930 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4931
4932 WARN_ON(src->len != dst_len);
4933
4934 offset = (start_offset + dst_offset) &
4935 ((unsigned long)PAGE_CACHE_SIZE - 1);
4936
Chris Masond3977122009-01-05 21:25:51 -05004937 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004938 page = extent_buffer_page(dst, i);
4939 WARN_ON(!PageUptodate(page));
4940
4941 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4942
Chris Masona6591712011-07-19 12:04:14 -04004943 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004944 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004945
4946 src_offset += cur;
4947 len -= cur;
4948 offset = 0;
4949 i++;
4950 }
4951}
Chris Masond1310b22008-01-24 16:13:08 -05004952
4953static void move_pages(struct page *dst_page, struct page *src_page,
4954 unsigned long dst_off, unsigned long src_off,
4955 unsigned long len)
4956{
Chris Masona6591712011-07-19 12:04:14 -04004957 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004958 if (dst_page == src_page) {
4959 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4960 } else {
Chris Masona6591712011-07-19 12:04:14 -04004961 char *src_kaddr = page_address(src_page);
Chris Masond1310b22008-01-24 16:13:08 -05004962 char *p = dst_kaddr + dst_off + len;
4963 char *s = src_kaddr + src_off + len;
4964
4965 while (len--)
4966 *--p = *--s;
Chris Masond1310b22008-01-24 16:13:08 -05004967 }
Chris Masond1310b22008-01-24 16:13:08 -05004968}
4969
Sergei Trofimovich33872062011-04-11 21:52:52 +00004970static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4971{
4972 unsigned long distance = (src > dst) ? src - dst : dst - src;
4973 return distance < len;
4974}
4975
Chris Masond1310b22008-01-24 16:13:08 -05004976static void copy_pages(struct page *dst_page, struct page *src_page,
4977 unsigned long dst_off, unsigned long src_off,
4978 unsigned long len)
4979{
Chris Masona6591712011-07-19 12:04:14 -04004980 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004981 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004982 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004983
Sergei Trofimovich33872062011-04-11 21:52:52 +00004984 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04004985 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00004986 } else {
Chris Masond1310b22008-01-24 16:13:08 -05004987 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004988 if (areas_overlap(src_off, dst_off, len))
4989 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00004990 }
Chris Masond1310b22008-01-24 16:13:08 -05004991
Chris Mason727011e2010-08-06 13:21:20 -04004992 if (must_memmove)
4993 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
4994 else
4995 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05004996}
4997
4998void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4999 unsigned long src_offset, unsigned long len)
5000{
5001 size_t cur;
5002 size_t dst_off_in_page;
5003 size_t src_off_in_page;
5004 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
5005 unsigned long dst_i;
5006 unsigned long src_i;
5007
5008 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05005009 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
5010 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005011 BUG_ON(1);
5012 }
5013 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05005014 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
5015 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005016 BUG_ON(1);
5017 }
5018
Chris Masond3977122009-01-05 21:25:51 -05005019 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05005020 dst_off_in_page = (start_offset + dst_offset) &
5021 ((unsigned long)PAGE_CACHE_SIZE - 1);
5022 src_off_in_page = (start_offset + src_offset) &
5023 ((unsigned long)PAGE_CACHE_SIZE - 1);
5024
5025 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
5026 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
5027
5028 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
5029 src_off_in_page));
5030 cur = min_t(unsigned long, cur,
5031 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
5032
5033 copy_pages(extent_buffer_page(dst, dst_i),
5034 extent_buffer_page(dst, src_i),
5035 dst_off_in_page, src_off_in_page, cur);
5036
5037 src_offset += cur;
5038 dst_offset += cur;
5039 len -= cur;
5040 }
5041}
Chris Masond1310b22008-01-24 16:13:08 -05005042
5043void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5044 unsigned long src_offset, unsigned long len)
5045{
5046 size_t cur;
5047 size_t dst_off_in_page;
5048 size_t src_off_in_page;
5049 unsigned long dst_end = dst_offset + len - 1;
5050 unsigned long src_end = src_offset + len - 1;
5051 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
5052 unsigned long dst_i;
5053 unsigned long src_i;
5054
5055 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05005056 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
5057 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005058 BUG_ON(1);
5059 }
5060 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05005061 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
5062 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005063 BUG_ON(1);
5064 }
Chris Mason727011e2010-08-06 13:21:20 -04005065 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05005066 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
5067 return;
5068 }
Chris Masond3977122009-01-05 21:25:51 -05005069 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05005070 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
5071 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
5072
5073 dst_off_in_page = (start_offset + dst_end) &
5074 ((unsigned long)PAGE_CACHE_SIZE - 1);
5075 src_off_in_page = (start_offset + src_end) &
5076 ((unsigned long)PAGE_CACHE_SIZE - 1);
5077
5078 cur = min_t(unsigned long, len, src_off_in_page + 1);
5079 cur = min(cur, dst_off_in_page + 1);
5080 move_pages(extent_buffer_page(dst, dst_i),
5081 extent_buffer_page(dst, src_i),
5082 dst_off_in_page - cur + 1,
5083 src_off_in_page - cur + 1, cur);
5084
5085 dst_end -= cur;
5086 src_end -= cur;
5087 len -= cur;
5088 }
5089}
Chris Mason6af118c2008-07-22 11:18:07 -04005090
David Sterbaf7a52a42013-04-26 14:56:29 +00005091int try_release_extent_buffer(struct page *page)
Miao Xie19fe0a82010-10-26 20:57:29 -04005092{
Chris Mason6af118c2008-07-22 11:18:07 -04005093 struct extent_buffer *eb;
Miao Xie897ca6e2010-10-26 20:57:29 -04005094
Miao Xie19fe0a82010-10-26 20:57:29 -04005095 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005096 * We need to make sure noboody is attaching this page to an eb right
5097 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04005098 */
Josef Bacik3083ee22012-03-09 16:01:49 -05005099 spin_lock(&page->mapping->private_lock);
5100 if (!PagePrivate(page)) {
5101 spin_unlock(&page->mapping->private_lock);
5102 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005103 }
5104
Josef Bacik3083ee22012-03-09 16:01:49 -05005105 eb = (struct extent_buffer *)page->private;
5106 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04005107
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005108 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005109 * This is a little awful but should be ok, we need to make sure that
5110 * the eb doesn't disappear out from under us while we're looking at
5111 * this page.
5112 */
5113 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005114 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05005115 spin_unlock(&eb->refs_lock);
5116 spin_unlock(&page->mapping->private_lock);
5117 return 0;
5118 }
5119 spin_unlock(&page->mapping->private_lock);
5120
Josef Bacik3083ee22012-03-09 16:01:49 -05005121 /*
5122 * If tree ref isn't set then we know the ref on this eb is a real ref,
5123 * so just return, this page will likely be freed soon anyway.
5124 */
5125 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5126 spin_unlock(&eb->refs_lock);
5127 return 0;
5128 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005129
David Sterbaf7a52a42013-04-26 14:56:29 +00005130 return release_extent_buffer(eb);
Chris Mason6af118c2008-07-22 11:18:07 -04005131}