blob: 84ceff6abbc11f66e8e3f96d24740d67cbacd1ed [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}
80#else
81#define btrfs_leak_debug_add(new, head) do {} while (0)
82#define btrfs_leak_debug_del(entry) do {} while (0)
83#define btrfs_leak_debug_check() do {} while (0)
Chris Mason4bef0842008-09-08 11:18:08 -040084#endif
Chris Masond1310b22008-01-24 16:13:08 -050085
Chris Masond1310b22008-01-24 16:13:08 -050086#define BUFFER_LRU_MAX 64
87
88struct tree_entry {
89 u64 start;
90 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -050091 struct rb_node rb_node;
92};
93
94struct extent_page_data {
95 struct bio *bio;
96 struct extent_io_tree *tree;
97 get_extent_t *get_extent;
Josef Bacikde0022b2012-09-25 14:25:58 -040098 unsigned long bio_flags;
Chris Mason771ed682008-11-06 22:02:51 -050099
100 /* tells writepage not to lock the state bits for this range
101 * it still does the unlocking
102 */
Chris Masonffbd5172009-04-20 15:50:09 -0400103 unsigned int extent_locked:1;
104
105 /* tells the submit_bio code to use a WRITE_SYNC */
106 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -0500107};
108
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400109static noinline void flush_write_bio(void *data);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400110static inline struct btrfs_fs_info *
111tree_fs_info(struct extent_io_tree *tree)
112{
113 return btrfs_sb(tree->mapping->host->i_sb);
114}
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400115
Chris Masond1310b22008-01-24 16:13:08 -0500116int __init extent_io_init(void)
117{
David Sterba837e1972012-09-07 03:00:48 -0600118 extent_state_cache = kmem_cache_create("btrfs_extent_state",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200119 sizeof(struct extent_state), 0,
120 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500121 if (!extent_state_cache)
122 return -ENOMEM;
123
David Sterba837e1972012-09-07 03:00:48 -0600124 extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200125 sizeof(struct extent_buffer), 0,
126 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500127 if (!extent_buffer_cache)
128 goto free_state_cache;
Chris Mason9be33952013-05-17 18:30:14 -0400129
130 btrfs_bioset = bioset_create(BIO_POOL_SIZE,
131 offsetof(struct btrfs_io_bio, bio));
132 if (!btrfs_bioset)
133 goto free_buffer_cache;
Chris Masond1310b22008-01-24 16:13:08 -0500134 return 0;
135
Chris Mason9be33952013-05-17 18:30:14 -0400136free_buffer_cache:
137 kmem_cache_destroy(extent_buffer_cache);
138 extent_buffer_cache = NULL;
139
Chris Masond1310b22008-01-24 16:13:08 -0500140free_state_cache:
141 kmem_cache_destroy(extent_state_cache);
Chris Mason9be33952013-05-17 18:30:14 -0400142 extent_state_cache = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500143 return -ENOMEM;
144}
145
146void extent_io_exit(void)
147{
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000148 btrfs_leak_debug_check();
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000149
150 /*
151 * Make sure all delayed rcu free are flushed before we
152 * destroy caches.
153 */
154 rcu_barrier();
Chris Masond1310b22008-01-24 16:13:08 -0500155 if (extent_state_cache)
156 kmem_cache_destroy(extent_state_cache);
157 if (extent_buffer_cache)
158 kmem_cache_destroy(extent_buffer_cache);
Chris Mason9be33952013-05-17 18:30:14 -0400159 if (btrfs_bioset)
160 bioset_free(btrfs_bioset);
Chris Masond1310b22008-01-24 16:13:08 -0500161}
162
163void extent_io_tree_init(struct extent_io_tree *tree,
David Sterbaf993c882011-04-20 23:35:57 +0200164 struct address_space *mapping)
Chris Masond1310b22008-01-24 16:13:08 -0500165{
Eric Paris6bef4d32010-02-23 19:43:04 +0000166 tree->state = RB_ROOT;
Miao Xie19fe0a82010-10-26 20:57:29 -0400167 INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500168 tree->ops = NULL;
169 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500170 spin_lock_init(&tree->lock);
Chris Mason6af118ce2008-07-22 11:18:07 -0400171 spin_lock_init(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -0500172 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500173}
Chris Masond1310b22008-01-24 16:13:08 -0500174
Christoph Hellwigb2950862008-12-02 09:54:17 -0500175static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500176{
177 struct extent_state *state;
Chris Masond1310b22008-01-24 16:13:08 -0500178
179 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400180 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500181 return state;
182 state->state = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500183 state->private = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500184 state->tree = NULL;
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000185 btrfs_leak_debug_add(&state->leak_list, &states);
Chris Masond1310b22008-01-24 16:13:08 -0500186 atomic_set(&state->refs, 1);
187 init_waitqueue_head(&state->wq);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100188 trace_alloc_extent_state(state, mask, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500189 return state;
190}
Chris Masond1310b22008-01-24 16:13:08 -0500191
Chris Mason4845e442010-05-25 20:56:50 -0400192void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500193{
Chris Masond1310b22008-01-24 16:13:08 -0500194 if (!state)
195 return;
196 if (atomic_dec_and_test(&state->refs)) {
Chris Mason70dec802008-01-29 09:59:12 -0500197 WARN_ON(state->tree);
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000198 btrfs_leak_debug_del(&state->leak_list);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100199 trace_free_extent_state(state, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500200 kmem_cache_free(extent_state_cache, state);
201 }
202}
Chris Masond1310b22008-01-24 16:13:08 -0500203
204static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
205 struct rb_node *node)
206{
Chris Masond3977122009-01-05 21:25:51 -0500207 struct rb_node **p = &root->rb_node;
208 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500209 struct tree_entry *entry;
210
Chris Masond3977122009-01-05 21:25:51 -0500211 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500212 parent = *p;
213 entry = rb_entry(parent, struct tree_entry, rb_node);
214
215 if (offset < entry->start)
216 p = &(*p)->rb_left;
217 else if (offset > entry->end)
218 p = &(*p)->rb_right;
219 else
220 return parent;
221 }
222
Chris Masond1310b22008-01-24 16:13:08 -0500223 rb_link_node(node, parent, p);
224 rb_insert_color(node, root);
225 return NULL;
226}
227
Chris Mason80ea96b2008-02-01 14:51:59 -0500228static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Chris Masond1310b22008-01-24 16:13:08 -0500229 struct rb_node **prev_ret,
230 struct rb_node **next_ret)
231{
Chris Mason80ea96b2008-02-01 14:51:59 -0500232 struct rb_root *root = &tree->state;
Chris Masond3977122009-01-05 21:25:51 -0500233 struct rb_node *n = root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500234 struct rb_node *prev = NULL;
235 struct rb_node *orig_prev = NULL;
236 struct tree_entry *entry;
237 struct tree_entry *prev_entry = NULL;
238
Chris Masond3977122009-01-05 21:25:51 -0500239 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500240 entry = rb_entry(n, struct tree_entry, rb_node);
241 prev = n;
242 prev_entry = entry;
243
244 if (offset < entry->start)
245 n = n->rb_left;
246 else if (offset > entry->end)
247 n = n->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500248 else
Chris Masond1310b22008-01-24 16:13:08 -0500249 return n;
250 }
251
252 if (prev_ret) {
253 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500254 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500255 prev = rb_next(prev);
256 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
257 }
258 *prev_ret = prev;
259 prev = orig_prev;
260 }
261
262 if (next_ret) {
263 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500264 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500265 prev = rb_prev(prev);
266 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
267 }
268 *next_ret = prev;
269 }
270 return NULL;
271}
272
Chris Mason80ea96b2008-02-01 14:51:59 -0500273static inline struct rb_node *tree_search(struct extent_io_tree *tree,
274 u64 offset)
Chris Masond1310b22008-01-24 16:13:08 -0500275{
Chris Mason70dec802008-01-29 09:59:12 -0500276 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500277 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500278
Chris Mason80ea96b2008-02-01 14:51:59 -0500279 ret = __etree_search(tree, offset, &prev, NULL);
Chris Masond3977122009-01-05 21:25:51 -0500280 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500281 return prev;
282 return ret;
283}
284
Josef Bacik9ed74f22009-09-11 16:12:44 -0400285static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
286 struct extent_state *other)
287{
288 if (tree->ops && tree->ops->merge_extent_hook)
289 tree->ops->merge_extent_hook(tree->mapping->host, new,
290 other);
291}
292
Chris Masond1310b22008-01-24 16:13:08 -0500293/*
294 * utility function to look for merge candidates inside a given range.
295 * Any extents with matching state are merged together into a single
296 * extent in the tree. Extents with EXTENT_IO in their state field
297 * are not merged because the end_io handlers need to be able to do
298 * operations on them without sleeping (or doing allocations/splits).
299 *
300 * This should be called with the tree lock held.
301 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000302static void merge_state(struct extent_io_tree *tree,
303 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500304{
305 struct extent_state *other;
306 struct rb_node *other_node;
307
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400308 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000309 return;
Chris Masond1310b22008-01-24 16:13:08 -0500310
311 other_node = rb_prev(&state->rb_node);
312 if (other_node) {
313 other = rb_entry(other_node, struct extent_state, rb_node);
314 if (other->end == state->start - 1 &&
315 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400316 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500317 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500318 other->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500319 rb_erase(&other->rb_node, &tree->state);
320 free_extent_state(other);
321 }
322 }
323 other_node = rb_next(&state->rb_node);
324 if (other_node) {
325 other = rb_entry(other_node, struct extent_state, rb_node);
326 if (other->start == state->end + 1 &&
327 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400328 merge_cb(tree, state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400329 state->end = other->end;
330 other->tree = NULL;
331 rb_erase(&other->rb_node, &tree->state);
332 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500333 }
334 }
Chris Masond1310b22008-01-24 16:13:08 -0500335}
336
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000337static void set_state_cb(struct extent_io_tree *tree,
David Sterba41074882013-04-29 13:38:46 +0000338 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500339{
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000340 if (tree->ops && tree->ops->set_bit_hook)
341 tree->ops->set_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500342}
343
344static void clear_state_cb(struct extent_io_tree *tree,
David Sterba41074882013-04-29 13:38:46 +0000345 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500346{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400347 if (tree->ops && tree->ops->clear_bit_hook)
348 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500349}
350
Xiao Guangrong3150b692011-07-14 03:19:08 +0000351static void set_state_bits(struct extent_io_tree *tree,
David Sterba41074882013-04-29 13:38:46 +0000352 struct extent_state *state, unsigned long *bits);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000353
Chris Masond1310b22008-01-24 16:13:08 -0500354/*
355 * insert an extent_state struct into the tree. 'bits' are set on the
356 * struct before it is inserted.
357 *
358 * This may return -EEXIST if the extent is already there, in which case the
359 * state struct is freed.
360 *
361 * The tree lock is not taken internally. This is a utility function and
362 * probably isn't what you want to call (see set/clear_extent_bit).
363 */
364static int insert_state(struct extent_io_tree *tree,
365 struct extent_state *state, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +0000366 unsigned long *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500367{
368 struct rb_node *node;
369
Julia Lawall31b1a2b2012-11-03 10:58:34 +0000370 if (end < start)
371 WARN(1, KERN_ERR "btrfs end < start %llu %llu\n",
Chris Masond3977122009-01-05 21:25:51 -0500372 (unsigned long long)end,
373 (unsigned long long)start);
Chris Masond1310b22008-01-24 16:13:08 -0500374 state->start = start;
375 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400376
Xiao Guangrong3150b692011-07-14 03:19:08 +0000377 set_state_bits(tree, state, bits);
378
Chris Masond1310b22008-01-24 16:13:08 -0500379 node = tree_insert(&tree->state, end, &state->rb_node);
380 if (node) {
381 struct extent_state *found;
382 found = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500383 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
384 "%llu %llu\n", (unsigned long long)found->start,
385 (unsigned long long)found->end,
386 (unsigned long long)start, (unsigned long long)end);
Chris Masond1310b22008-01-24 16:13:08 -0500387 return -EEXIST;
388 }
Chris Mason70dec802008-01-29 09:59:12 -0500389 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500390 merge_state(tree, state);
391 return 0;
392}
393
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000394static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
Josef Bacik9ed74f22009-09-11 16:12:44 -0400395 u64 split)
396{
397 if (tree->ops && tree->ops->split_extent_hook)
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000398 tree->ops->split_extent_hook(tree->mapping->host, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400399}
400
Chris Masond1310b22008-01-24 16:13:08 -0500401/*
402 * split a given extent state struct in two, inserting the preallocated
403 * struct 'prealloc' as the newly created second half. 'split' indicates an
404 * offset inside 'orig' where it should be split.
405 *
406 * Before calling,
407 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
408 * are two extent state structs in the tree:
409 * prealloc: [orig->start, split - 1]
410 * orig: [ split, orig->end ]
411 *
412 * The tree locks are not taken by this function. They need to be held
413 * by the caller.
414 */
415static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
416 struct extent_state *prealloc, u64 split)
417{
418 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400419
420 split_cb(tree, orig, split);
421
Chris Masond1310b22008-01-24 16:13:08 -0500422 prealloc->start = orig->start;
423 prealloc->end = split - 1;
424 prealloc->state = orig->state;
425 orig->start = split;
426
427 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
428 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500429 free_extent_state(prealloc);
430 return -EEXIST;
431 }
Chris Mason70dec802008-01-29 09:59:12 -0500432 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500433 return 0;
434}
435
Li Zefancdc6a392012-03-12 16:39:48 +0800436static struct extent_state *next_state(struct extent_state *state)
437{
438 struct rb_node *next = rb_next(&state->rb_node);
439 if (next)
440 return rb_entry(next, struct extent_state, rb_node);
441 else
442 return NULL;
443}
444
Chris Masond1310b22008-01-24 16:13:08 -0500445/*
446 * utility function to clear some bits in an extent state struct.
Wang Sheng-Hui1b303fc2012-04-06 14:35:18 +0800447 * it will optionally wake up any one waiting on this state (wake == 1).
Chris Masond1310b22008-01-24 16:13:08 -0500448 *
449 * If no bits are set on the state struct after clearing things, the
450 * struct is freed and removed from the tree
451 */
Li Zefancdc6a392012-03-12 16:39:48 +0800452static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
453 struct extent_state *state,
David Sterba41074882013-04-29 13:38:46 +0000454 unsigned long *bits, int wake)
Chris Masond1310b22008-01-24 16:13:08 -0500455{
Li Zefancdc6a392012-03-12 16:39:48 +0800456 struct extent_state *next;
David Sterba41074882013-04-29 13:38:46 +0000457 unsigned long bits_to_clear = *bits & ~EXTENT_CTLBITS;
Chris Masond1310b22008-01-24 16:13:08 -0500458
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400459 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500460 u64 range = state->end - state->start + 1;
461 WARN_ON(range > tree->dirty_bytes);
462 tree->dirty_bytes -= range;
463 }
Chris Mason291d6732008-01-29 15:55:23 -0500464 clear_state_cb(tree, state, bits);
Josef Bacik32c00af2009-10-08 13:34:05 -0400465 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500466 if (wake)
467 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400468 if (state->state == 0) {
Li Zefancdc6a392012-03-12 16:39:48 +0800469 next = next_state(state);
Chris Mason70dec802008-01-29 09:59:12 -0500470 if (state->tree) {
Chris Masond1310b22008-01-24 16:13:08 -0500471 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500472 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500473 free_extent_state(state);
474 } else {
475 WARN_ON(1);
476 }
477 } else {
478 merge_state(tree, state);
Li Zefancdc6a392012-03-12 16:39:48 +0800479 next = next_state(state);
Chris Masond1310b22008-01-24 16:13:08 -0500480 }
Li Zefancdc6a392012-03-12 16:39:48 +0800481 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500482}
483
Xiao Guangrong82337672011-04-20 06:44:57 +0000484static struct extent_state *
485alloc_extent_state_atomic(struct extent_state *prealloc)
486{
487 if (!prealloc)
488 prealloc = alloc_extent_state(GFP_ATOMIC);
489
490 return prealloc;
491}
492
Eric Sandeen48a3b632013-04-25 20:41:01 +0000493static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400494{
495 btrfs_panic(tree_fs_info(tree), err, "Locking error: "
496 "Extent tree was modified by another "
497 "thread while locked.");
498}
499
Chris Masond1310b22008-01-24 16:13:08 -0500500/*
501 * clear some bits on a range in the tree. This may require splitting
502 * or inserting elements in the tree, so the gfp mask is used to
503 * indicate which allocations or sleeping are allowed.
504 *
505 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
506 * the given range from the tree regardless of state (ie for truncate).
507 *
508 * the range [start, end] is inclusive.
509 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100510 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500511 */
512int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +0000513 unsigned long bits, int wake, int delete,
Chris Mason2c64c532009-09-02 15:04:12 -0400514 struct extent_state **cached_state,
515 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500516{
517 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400518 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500519 struct extent_state *prealloc = NULL;
520 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400521 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500522 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000523 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500524
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400525 if (delete)
526 bits |= ~EXTENT_CTLBITS;
527 bits |= EXTENT_FIRST_DELALLOC;
528
Josef Bacik2ac55d42010-02-03 19:33:23 +0000529 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
530 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500531again:
532 if (!prealloc && (mask & __GFP_WAIT)) {
533 prealloc = alloc_extent_state(mask);
534 if (!prealloc)
535 return -ENOMEM;
536 }
537
Chris Masoncad321a2008-12-17 14:51:42 -0500538 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400539 if (cached_state) {
540 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000541
542 if (clear) {
543 *cached_state = NULL;
544 cached_state = NULL;
545 }
546
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400547 if (cached && cached->tree && cached->start <= start &&
548 cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000549 if (clear)
550 atomic_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400551 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400552 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400553 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000554 if (clear)
555 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400556 }
Chris Masond1310b22008-01-24 16:13:08 -0500557 /*
558 * this search will find the extents that end after
559 * our range starts
560 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500561 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500562 if (!node)
563 goto out;
564 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400565hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500566 if (state->start > end)
567 goto out;
568 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400569 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500570
Liu Bo04493142012-02-16 18:34:37 +0800571 /* the state doesn't have the wanted bits, go ahead */
Li Zefancdc6a392012-03-12 16:39:48 +0800572 if (!(state->state & bits)) {
573 state = next_state(state);
Liu Bo04493142012-02-16 18:34:37 +0800574 goto next;
Li Zefancdc6a392012-03-12 16:39:48 +0800575 }
Liu Bo04493142012-02-16 18:34:37 +0800576
Chris Masond1310b22008-01-24 16:13:08 -0500577 /*
578 * | ---- desired range ---- |
579 * | state | or
580 * | ------------- state -------------- |
581 *
582 * We need to split the extent we found, and may flip
583 * bits on second half.
584 *
585 * If the extent we found extends past our range, we
586 * just split and search again. It'll get split again
587 * the next time though.
588 *
589 * If the extent we found is inside our range, we clear
590 * the desired bit on it.
591 */
592
593 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000594 prealloc = alloc_extent_state_atomic(prealloc);
595 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500596 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400597 if (err)
598 extent_io_tree_panic(tree, err);
599
Chris Masond1310b22008-01-24 16:13:08 -0500600 prealloc = NULL;
601 if (err)
602 goto out;
603 if (state->end <= end) {
Liu Bod1ac6e42012-05-10 18:10:39 +0800604 state = clear_state_bit(tree, state, &bits, wake);
605 goto next;
Chris Masond1310b22008-01-24 16:13:08 -0500606 }
607 goto search_again;
608 }
609 /*
610 * | ---- desired range ---- |
611 * | state |
612 * We need to split the extent, and clear the bit
613 * on the first half
614 */
615 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000616 prealloc = alloc_extent_state_atomic(prealloc);
617 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500618 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400619 if (err)
620 extent_io_tree_panic(tree, err);
621
Chris Masond1310b22008-01-24 16:13:08 -0500622 if (wake)
623 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400624
Jeff Mahoney6763af82012-03-01 14:56:29 +0100625 clear_state_bit(tree, prealloc, &bits, wake);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400626
Chris Masond1310b22008-01-24 16:13:08 -0500627 prealloc = NULL;
628 goto out;
629 }
Chris Mason42daec22009-09-23 19:51:09 -0400630
Li Zefancdc6a392012-03-12 16:39:48 +0800631 state = clear_state_bit(tree, state, &bits, wake);
Liu Bo04493142012-02-16 18:34:37 +0800632next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400633 if (last_end == (u64)-1)
634 goto out;
635 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800636 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800637 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500638 goto search_again;
639
640out:
Chris Masoncad321a2008-12-17 14:51:42 -0500641 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500642 if (prealloc)
643 free_extent_state(prealloc);
644
Jeff Mahoney6763af82012-03-01 14:56:29 +0100645 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500646
647search_again:
648 if (start > end)
649 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500650 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500651 if (mask & __GFP_WAIT)
652 cond_resched();
653 goto again;
654}
Chris Masond1310b22008-01-24 16:13:08 -0500655
Jeff Mahoney143bede2012-03-01 14:56:26 +0100656static void wait_on_state(struct extent_io_tree *tree,
657 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500658 __releases(tree->lock)
659 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500660{
661 DEFINE_WAIT(wait);
662 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500663 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500664 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500665 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500666 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500667}
668
669/*
670 * waits for one or more bits to clear on a range in the state tree.
671 * The range [start, end] is inclusive.
672 * The tree lock is taken by this function
673 */
David Sterba41074882013-04-29 13:38:46 +0000674static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
675 unsigned long bits)
Chris Masond1310b22008-01-24 16:13:08 -0500676{
677 struct extent_state *state;
678 struct rb_node *node;
679
Chris Masoncad321a2008-12-17 14:51:42 -0500680 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500681again:
682 while (1) {
683 /*
684 * this search will find all the extents that end after
685 * our range starts
686 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500687 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500688 if (!node)
689 break;
690
691 state = rb_entry(node, struct extent_state, rb_node);
692
693 if (state->start > end)
694 goto out;
695
696 if (state->state & bits) {
697 start = state->start;
698 atomic_inc(&state->refs);
699 wait_on_state(tree, state);
700 free_extent_state(state);
701 goto again;
702 }
703 start = state->end + 1;
704
705 if (start > end)
706 break;
707
Xiao Guangrongded91f02011-07-14 03:19:27 +0000708 cond_resched_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500709 }
710out:
Chris Masoncad321a2008-12-17 14:51:42 -0500711 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500712}
Chris Masond1310b22008-01-24 16:13:08 -0500713
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000714static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500715 struct extent_state *state,
David Sterba41074882013-04-29 13:38:46 +0000716 unsigned long *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500717{
David Sterba41074882013-04-29 13:38:46 +0000718 unsigned long bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400719
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000720 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400721 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500722 u64 range = state->end - state->start + 1;
723 tree->dirty_bytes += range;
724 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400725 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500726}
727
Chris Mason2c64c532009-09-02 15:04:12 -0400728static void cache_state(struct extent_state *state,
729 struct extent_state **cached_ptr)
730{
731 if (cached_ptr && !(*cached_ptr)) {
732 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
733 *cached_ptr = state;
734 atomic_inc(&state->refs);
735 }
736 }
737}
738
Arne Jansen507903b2011-04-06 10:02:20 +0000739static void uncache_state(struct extent_state **cached_ptr)
740{
741 if (cached_ptr && (*cached_ptr)) {
742 struct extent_state *state = *cached_ptr;
Chris Mason109b36a2011-04-12 13:57:39 -0400743 *cached_ptr = NULL;
744 free_extent_state(state);
Arne Jansen507903b2011-04-06 10:02:20 +0000745 }
746}
747
Chris Masond1310b22008-01-24 16:13:08 -0500748/*
Chris Mason1edbb732009-09-02 13:24:36 -0400749 * set some bits on a range in the tree. This may require allocations or
750 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500751 *
Chris Mason1edbb732009-09-02 13:24:36 -0400752 * If any of the exclusive bits are set, this will fail with -EEXIST if some
753 * part of the range already has the desired bits set. The start of the
754 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500755 *
Chris Mason1edbb732009-09-02 13:24:36 -0400756 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500757 */
Chris Mason1edbb732009-09-02 13:24:36 -0400758
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100759static int __must_check
760__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +0000761 unsigned long bits, unsigned long exclusive_bits,
762 u64 *failed_start, struct extent_state **cached_state,
763 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500764{
765 struct extent_state *state;
766 struct extent_state *prealloc = NULL;
767 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500768 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500769 u64 last_start;
770 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400771
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400772 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500773again:
774 if (!prealloc && (mask & __GFP_WAIT)) {
775 prealloc = alloc_extent_state(mask);
Xiao Guangrong82337672011-04-20 06:44:57 +0000776 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500777 }
778
Chris Masoncad321a2008-12-17 14:51:42 -0500779 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400780 if (cached_state && *cached_state) {
781 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400782 if (state->start <= start && state->end > start &&
783 state->tree) {
Chris Mason9655d292009-09-02 15:22:30 -0400784 node = &state->rb_node;
785 goto hit_next;
786 }
787 }
Chris Masond1310b22008-01-24 16:13:08 -0500788 /*
789 * this search will find all the extents that end after
790 * our range starts.
791 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500792 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500793 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000794 prealloc = alloc_extent_state_atomic(prealloc);
795 BUG_ON(!prealloc);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400796 err = insert_state(tree, prealloc, start, end, &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400797 if (err)
798 extent_io_tree_panic(tree, err);
799
Chris Masond1310b22008-01-24 16:13:08 -0500800 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500801 goto out;
802 }
Chris Masond1310b22008-01-24 16:13:08 -0500803 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400804hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500805 last_start = state->start;
806 last_end = state->end;
807
808 /*
809 * | ---- desired range ---- |
810 * | state |
811 *
812 * Just lock what we found and keep going
813 */
814 if (state->start == start && state->end <= end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400815 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500816 *failed_start = state->start;
817 err = -EEXIST;
818 goto out;
819 }
Chris Mason42daec22009-09-23 19:51:09 -0400820
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000821 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400822 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500823 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400824 if (last_end == (u64)-1)
825 goto out;
826 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800827 state = next_state(state);
828 if (start < end && state && state->start == start &&
829 !need_resched())
830 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500831 goto search_again;
832 }
833
834 /*
835 * | ---- desired range ---- |
836 * | state |
837 * or
838 * | ------------- state -------------- |
839 *
840 * We need to split the extent we found, and may flip bits on
841 * second half.
842 *
843 * If the extent we found extends past our
844 * range, we just split and search again. It'll get split
845 * again the next time though.
846 *
847 * If the extent we found is inside our range, we set the
848 * desired bit on it.
849 */
850 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400851 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500852 *failed_start = start;
853 err = -EEXIST;
854 goto out;
855 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000856
857 prealloc = alloc_extent_state_atomic(prealloc);
858 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500859 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400860 if (err)
861 extent_io_tree_panic(tree, err);
862
Chris Masond1310b22008-01-24 16:13:08 -0500863 prealloc = NULL;
864 if (err)
865 goto out;
866 if (state->end <= end) {
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000867 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400868 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500869 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400870 if (last_end == (u64)-1)
871 goto out;
872 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800873 state = next_state(state);
874 if (start < end && state && state->start == start &&
875 !need_resched())
876 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500877 }
878 goto search_again;
879 }
880 /*
881 * | ---- desired range ---- |
882 * | state | or | state |
883 *
884 * There's a hole, we need to insert something in it and
885 * ignore the extent we found.
886 */
887 if (state->start > start) {
888 u64 this_end;
889 if (end < last_start)
890 this_end = end;
891 else
Chris Masond3977122009-01-05 21:25:51 -0500892 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000893
894 prealloc = alloc_extent_state_atomic(prealloc);
895 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000896
897 /*
898 * Avoid to free 'prealloc' if it can be merged with
899 * the later extent.
900 */
Chris Masond1310b22008-01-24 16:13:08 -0500901 err = insert_state(tree, prealloc, start, this_end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400902 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400903 if (err)
904 extent_io_tree_panic(tree, err);
905
Chris Mason2c64c532009-09-02 15:04:12 -0400906 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500907 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500908 start = this_end + 1;
909 goto search_again;
910 }
911 /*
912 * | ---- desired range ---- |
913 * | state |
914 * We need to split the extent, and set the bit
915 * on the first half
916 */
917 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400918 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500919 *failed_start = start;
920 err = -EEXIST;
921 goto out;
922 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000923
924 prealloc = alloc_extent_state_atomic(prealloc);
925 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500926 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400927 if (err)
928 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -0500929
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000930 set_state_bits(tree, prealloc, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400931 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500932 merge_state(tree, prealloc);
933 prealloc = NULL;
934 goto out;
935 }
936
937 goto search_again;
938
939out:
Chris Masoncad321a2008-12-17 14:51:42 -0500940 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500941 if (prealloc)
942 free_extent_state(prealloc);
943
944 return err;
945
946search_again:
947 if (start > end)
948 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500949 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500950 if (mask & __GFP_WAIT)
951 cond_resched();
952 goto again;
953}
Chris Masond1310b22008-01-24 16:13:08 -0500954
David Sterba41074882013-04-29 13:38:46 +0000955int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
956 unsigned long bits, u64 * failed_start,
957 struct extent_state **cached_state, gfp_t mask)
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100958{
959 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
960 cached_state, mask);
961}
962
963
Josef Bacik462d6fa2011-09-26 13:56:12 -0400964/**
Liu Bo10983f22012-07-11 15:26:19 +0800965 * convert_extent_bit - convert all bits in a given range from one bit to
966 * another
Josef Bacik462d6fa2011-09-26 13:56:12 -0400967 * @tree: the io tree to search
968 * @start: the start offset in bytes
969 * @end: the end offset in bytes (inclusive)
970 * @bits: the bits to set in this range
971 * @clear_bits: the bits to clear in this range
Josef Bacike6138872012-09-27 17:07:30 -0400972 * @cached_state: state that we're going to cache
Josef Bacik462d6fa2011-09-26 13:56:12 -0400973 * @mask: the allocation mask
974 *
975 * This will go through and set bits for the given range. If any states exist
976 * already in this range they are set with the given bit and cleared of the
977 * clear_bits. This is only meant to be used by things that are mergeable, ie
978 * converting from say DELALLOC to DIRTY. This is not meant to be used with
979 * boundary bits like LOCK.
980 */
981int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +0000982 unsigned long bits, unsigned long clear_bits,
Josef Bacike6138872012-09-27 17:07:30 -0400983 struct extent_state **cached_state, gfp_t mask)
Josef Bacik462d6fa2011-09-26 13:56:12 -0400984{
985 struct extent_state *state;
986 struct extent_state *prealloc = NULL;
987 struct rb_node *node;
988 int err = 0;
989 u64 last_start;
990 u64 last_end;
991
992again:
993 if (!prealloc && (mask & __GFP_WAIT)) {
994 prealloc = alloc_extent_state(mask);
995 if (!prealloc)
996 return -ENOMEM;
997 }
998
999 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001000 if (cached_state && *cached_state) {
1001 state = *cached_state;
1002 if (state->start <= start && state->end > start &&
1003 state->tree) {
1004 node = &state->rb_node;
1005 goto hit_next;
1006 }
1007 }
1008
Josef Bacik462d6fa2011-09-26 13:56:12 -04001009 /*
1010 * this search will find all the extents that end after
1011 * our range starts.
1012 */
1013 node = tree_search(tree, start);
1014 if (!node) {
1015 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001016 if (!prealloc) {
1017 err = -ENOMEM;
1018 goto out;
1019 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001020 err = insert_state(tree, prealloc, start, end, &bits);
1021 prealloc = NULL;
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001022 if (err)
1023 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001024 goto out;
1025 }
1026 state = rb_entry(node, struct extent_state, rb_node);
1027hit_next:
1028 last_start = state->start;
1029 last_end = state->end;
1030
1031 /*
1032 * | ---- desired range ---- |
1033 * | state |
1034 *
1035 * Just lock what we found and keep going
1036 */
1037 if (state->start == start && state->end <= end) {
Josef Bacik462d6fa2011-09-26 13:56:12 -04001038 set_state_bits(tree, state, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001039 cache_state(state, cached_state);
Liu Bod1ac6e42012-05-10 18:10:39 +08001040 state = clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001041 if (last_end == (u64)-1)
1042 goto out;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001043 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001044 if (start < end && state && state->start == start &&
1045 !need_resched())
1046 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001047 goto search_again;
1048 }
1049
1050 /*
1051 * | ---- desired range ---- |
1052 * | state |
1053 * or
1054 * | ------------- state -------------- |
1055 *
1056 * We need to split the extent we found, and may flip bits on
1057 * second half.
1058 *
1059 * If the extent we found extends past our
1060 * range, we just split and search again. It'll get split
1061 * again the next time though.
1062 *
1063 * If the extent we found is inside our range, we set the
1064 * desired bit on it.
1065 */
1066 if (state->start < start) {
1067 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001068 if (!prealloc) {
1069 err = -ENOMEM;
1070 goto out;
1071 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001072 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001073 if (err)
1074 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001075 prealloc = NULL;
1076 if (err)
1077 goto out;
1078 if (state->end <= end) {
1079 set_state_bits(tree, state, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001080 cache_state(state, cached_state);
Liu Bod1ac6e42012-05-10 18:10:39 +08001081 state = clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001082 if (last_end == (u64)-1)
1083 goto out;
1084 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001085 if (start < end && state && state->start == start &&
1086 !need_resched())
1087 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001088 }
1089 goto search_again;
1090 }
1091 /*
1092 * | ---- desired range ---- |
1093 * | state | or | state |
1094 *
1095 * There's a hole, we need to insert something in it and
1096 * ignore the extent we found.
1097 */
1098 if (state->start > start) {
1099 u64 this_end;
1100 if (end < last_start)
1101 this_end = end;
1102 else
1103 this_end = last_start - 1;
1104
1105 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001106 if (!prealloc) {
1107 err = -ENOMEM;
1108 goto out;
1109 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001110
1111 /*
1112 * Avoid to free 'prealloc' if it can be merged with
1113 * the later extent.
1114 */
1115 err = insert_state(tree, prealloc, start, this_end,
1116 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001117 if (err)
1118 extent_io_tree_panic(tree, err);
Josef Bacike6138872012-09-27 17:07:30 -04001119 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001120 prealloc = NULL;
1121 start = this_end + 1;
1122 goto search_again;
1123 }
1124 /*
1125 * | ---- desired range ---- |
1126 * | state |
1127 * We need to split the extent, and set the bit
1128 * on the first half
1129 */
1130 if (state->start <= end && state->end > end) {
1131 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001132 if (!prealloc) {
1133 err = -ENOMEM;
1134 goto out;
1135 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001136
1137 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001138 if (err)
1139 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001140
1141 set_state_bits(tree, prealloc, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001142 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001143 clear_state_bit(tree, prealloc, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001144 prealloc = NULL;
1145 goto out;
1146 }
1147
1148 goto search_again;
1149
1150out:
1151 spin_unlock(&tree->lock);
1152 if (prealloc)
1153 free_extent_state(prealloc);
1154
1155 return err;
1156
1157search_again:
1158 if (start > end)
1159 goto out;
1160 spin_unlock(&tree->lock);
1161 if (mask & __GFP_WAIT)
1162 cond_resched();
1163 goto again;
1164}
1165
Chris Masond1310b22008-01-24 16:13:08 -05001166/* wrappers around set/clear extent bit */
1167int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1168 gfp_t mask)
1169{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001170 return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001171 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001172}
Chris Masond1310b22008-01-24 16:13:08 -05001173
1174int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +00001175 unsigned long bits, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001176{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001177 return set_extent_bit(tree, start, end, bits, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001178 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001179}
Chris Masond1310b22008-01-24 16:13:08 -05001180
1181int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +00001182 unsigned long bits, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001183{
Chris Mason2c64c532009-09-02 15:04:12 -04001184 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001185}
Chris Masond1310b22008-01-24 16:13:08 -05001186
1187int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001188 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001189{
1190 return set_extent_bit(tree, start, end,
Liu Bofee187d2011-09-29 15:55:28 +08001191 EXTENT_DELALLOC | EXTENT_UPTODATE,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001192 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001193}
Chris Masond1310b22008-01-24 16:13:08 -05001194
Liu Bo9e8a4a82012-09-05 19:10:51 -06001195int set_extent_defrag(struct extent_io_tree *tree, u64 start, u64 end,
1196 struct extent_state **cached_state, gfp_t mask)
1197{
1198 return set_extent_bit(tree, start, end,
1199 EXTENT_DELALLOC | EXTENT_UPTODATE | EXTENT_DEFRAG,
1200 NULL, cached_state, mask);
1201}
1202
Chris Masond1310b22008-01-24 16:13:08 -05001203int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1204 gfp_t mask)
1205{
1206 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04001207 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001208 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001209}
Chris Masond1310b22008-01-24 16:13:08 -05001210
1211int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1212 gfp_t mask)
1213{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001214 return set_extent_bit(tree, start, end, EXTENT_NEW, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001215 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001216}
Chris Masond1310b22008-01-24 16:13:08 -05001217
Chris Masond1310b22008-01-24 16:13:08 -05001218int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +00001219 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001220{
Liu Bo6b67a322013-03-28 08:30:28 +00001221 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, NULL,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001222 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001223}
Chris Masond1310b22008-01-24 16:13:08 -05001224
Josef Bacik5fd02042012-05-02 14:00:54 -04001225int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1226 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001227{
Chris Mason2c64c532009-09-02 15:04:12 -04001228 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001229 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001230}
Chris Masond1310b22008-01-24 16:13:08 -05001231
Chris Masond352ac62008-09-29 15:18:18 -04001232/*
1233 * either insert or lock state struct between start and end use mask to tell
1234 * us if waiting is desired.
1235 */
Chris Mason1edbb732009-09-02 13:24:36 -04001236int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +00001237 unsigned long bits, struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001238{
1239 int err;
1240 u64 failed_start;
1241 while (1) {
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001242 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
1243 EXTENT_LOCKED, &failed_start,
1244 cached_state, GFP_NOFS);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001245 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001246 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1247 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001248 } else
Chris Masond1310b22008-01-24 16:13:08 -05001249 break;
Chris Masond1310b22008-01-24 16:13:08 -05001250 WARN_ON(start > end);
1251 }
1252 return err;
1253}
Chris Masond1310b22008-01-24 16:13:08 -05001254
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001255int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Mason1edbb732009-09-02 13:24:36 -04001256{
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001257 return lock_extent_bits(tree, start, end, 0, NULL);
Chris Mason1edbb732009-09-02 13:24:36 -04001258}
1259
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001260int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001261{
1262 int err;
1263 u64 failed_start;
1264
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001265 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1266 &failed_start, NULL, GFP_NOFS);
Yan Zheng66435582008-10-30 14:19:50 -04001267 if (err == -EEXIST) {
1268 if (failed_start > start)
1269 clear_extent_bit(tree, start, failed_start - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001270 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
Josef Bacik25179202008-10-29 14:49:05 -04001271 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001272 }
Josef Bacik25179202008-10-29 14:49:05 -04001273 return 1;
1274}
Josef Bacik25179202008-10-29 14:49:05 -04001275
Chris Mason2c64c532009-09-02 15:04:12 -04001276int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1277 struct extent_state **cached, gfp_t mask)
1278{
1279 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1280 mask);
1281}
1282
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001283int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001284{
Chris Mason2c64c532009-09-02 15:04:12 -04001285 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001286 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001287}
Chris Masond1310b22008-01-24 16:13:08 -05001288
Chris Mason4adaa612013-03-26 13:07:00 -04001289int extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
1290{
1291 unsigned long index = start >> PAGE_CACHE_SHIFT;
1292 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1293 struct page *page;
1294
1295 while (index <= end_index) {
1296 page = find_get_page(inode->i_mapping, index);
1297 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1298 clear_page_dirty_for_io(page);
1299 page_cache_release(page);
1300 index++;
1301 }
1302 return 0;
1303}
1304
1305int extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
1306{
1307 unsigned long index = start >> PAGE_CACHE_SHIFT;
1308 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1309 struct page *page;
1310
1311 while (index <= end_index) {
1312 page = find_get_page(inode->i_mapping, index);
1313 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1314 account_page_redirty(page);
1315 __set_page_dirty_nobuffers(page);
1316 page_cache_release(page);
1317 index++;
1318 }
1319 return 0;
1320}
1321
Chris Masond1310b22008-01-24 16:13:08 -05001322/*
Chris Masond1310b22008-01-24 16:13:08 -05001323 * helper function to set both pages and extents in the tree writeback
1324 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001325static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001326{
1327 unsigned long index = start >> PAGE_CACHE_SHIFT;
1328 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1329 struct page *page;
1330
1331 while (index <= end_index) {
1332 page = find_get_page(tree->mapping, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001333 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Masond1310b22008-01-24 16:13:08 -05001334 set_page_writeback(page);
1335 page_cache_release(page);
1336 index++;
1337 }
Chris Masond1310b22008-01-24 16:13:08 -05001338 return 0;
1339}
Chris Masond1310b22008-01-24 16:13:08 -05001340
Chris Masond352ac62008-09-29 15:18:18 -04001341/* find the first state struct with 'bits' set after 'start', and
1342 * return it. tree->lock must be held. NULL will returned if
1343 * nothing was found after 'start'
1344 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001345static struct extent_state *
1346find_first_extent_bit_state(struct extent_io_tree *tree,
David Sterba41074882013-04-29 13:38:46 +00001347 u64 start, unsigned long bits)
Chris Masond7fc6402008-02-18 12:12:38 -05001348{
1349 struct rb_node *node;
1350 struct extent_state *state;
1351
1352 /*
1353 * this search will find all the extents that end after
1354 * our range starts.
1355 */
1356 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001357 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001358 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001359
Chris Masond3977122009-01-05 21:25:51 -05001360 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001361 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001362 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001363 return state;
Chris Masond3977122009-01-05 21:25:51 -05001364
Chris Masond7fc6402008-02-18 12:12:38 -05001365 node = rb_next(node);
1366 if (!node)
1367 break;
1368 }
1369out:
1370 return NULL;
1371}
Chris Masond7fc6402008-02-18 12:12:38 -05001372
Chris Masond352ac62008-09-29 15:18:18 -04001373/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001374 * find the first offset in the io tree with 'bits' set. zero is
1375 * returned if we find something, and *start_ret and *end_ret are
1376 * set to reflect the state struct that was found.
1377 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001378 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001379 */
1380int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
David Sterba41074882013-04-29 13:38:46 +00001381 u64 *start_ret, u64 *end_ret, unsigned long bits,
Josef Bacike6138872012-09-27 17:07:30 -04001382 struct extent_state **cached_state)
Xiao Guangrong69261c42011-07-14 03:19:45 +00001383{
1384 struct extent_state *state;
Josef Bacike6138872012-09-27 17:07:30 -04001385 struct rb_node *n;
Xiao Guangrong69261c42011-07-14 03:19:45 +00001386 int ret = 1;
1387
1388 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001389 if (cached_state && *cached_state) {
1390 state = *cached_state;
1391 if (state->end == start - 1 && state->tree) {
1392 n = rb_next(&state->rb_node);
1393 while (n) {
1394 state = rb_entry(n, struct extent_state,
1395 rb_node);
1396 if (state->state & bits)
1397 goto got_it;
1398 n = rb_next(n);
1399 }
1400 free_extent_state(*cached_state);
1401 *cached_state = NULL;
1402 goto out;
1403 }
1404 free_extent_state(*cached_state);
1405 *cached_state = NULL;
1406 }
1407
Xiao Guangrong69261c42011-07-14 03:19:45 +00001408 state = find_first_extent_bit_state(tree, start, bits);
Josef Bacike6138872012-09-27 17:07:30 -04001409got_it:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001410 if (state) {
Josef Bacike6138872012-09-27 17:07:30 -04001411 cache_state(state, cached_state);
Xiao Guangrong69261c42011-07-14 03:19:45 +00001412 *start_ret = state->start;
1413 *end_ret = state->end;
1414 ret = 0;
1415 }
Josef Bacike6138872012-09-27 17:07:30 -04001416out:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001417 spin_unlock(&tree->lock);
1418 return ret;
1419}
1420
1421/*
Chris Masond352ac62008-09-29 15:18:18 -04001422 * find a contiguous range of bytes in the file marked as delalloc, not
1423 * more than 'max_bytes'. start and end are used to return the range,
1424 *
1425 * 1 is returned if we find something, 0 if nothing was in the tree
1426 */
Chris Masonc8b97812008-10-29 14:49:59 -04001427static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001428 u64 *start, u64 *end, u64 max_bytes,
1429 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001430{
1431 struct rb_node *node;
1432 struct extent_state *state;
1433 u64 cur_start = *start;
1434 u64 found = 0;
1435 u64 total_bytes = 0;
1436
Chris Masoncad321a2008-12-17 14:51:42 -05001437 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001438
Chris Masond1310b22008-01-24 16:13:08 -05001439 /*
1440 * this search will find all the extents that end after
1441 * our range starts.
1442 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001443 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001444 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001445 if (!found)
1446 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001447 goto out;
1448 }
1449
Chris Masond3977122009-01-05 21:25:51 -05001450 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001451 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001452 if (found && (state->start != cur_start ||
1453 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001454 goto out;
1455 }
1456 if (!(state->state & EXTENT_DELALLOC)) {
1457 if (!found)
1458 *end = state->end;
1459 goto out;
1460 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001461 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001462 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001463 *cached_state = state;
1464 atomic_inc(&state->refs);
1465 }
Chris Masond1310b22008-01-24 16:13:08 -05001466 found++;
1467 *end = state->end;
1468 cur_start = state->end + 1;
1469 node = rb_next(node);
1470 if (!node)
1471 break;
1472 total_bytes += state->end - state->start + 1;
1473 if (total_bytes >= max_bytes)
1474 break;
1475 }
1476out:
Chris Masoncad321a2008-12-17 14:51:42 -05001477 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001478 return found;
1479}
1480
Jeff Mahoney143bede2012-03-01 14:56:26 +01001481static noinline void __unlock_for_delalloc(struct inode *inode,
1482 struct page *locked_page,
1483 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001484{
1485 int ret;
1486 struct page *pages[16];
1487 unsigned long index = start >> PAGE_CACHE_SHIFT;
1488 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1489 unsigned long nr_pages = end_index - index + 1;
1490 int i;
1491
1492 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001493 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001494
Chris Masond3977122009-01-05 21:25:51 -05001495 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001496 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001497 min_t(unsigned long, nr_pages,
1498 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001499 for (i = 0; i < ret; i++) {
1500 if (pages[i] != locked_page)
1501 unlock_page(pages[i]);
1502 page_cache_release(pages[i]);
1503 }
1504 nr_pages -= ret;
1505 index += ret;
1506 cond_resched();
1507 }
Chris Masonc8b97812008-10-29 14:49:59 -04001508}
1509
1510static noinline int lock_delalloc_pages(struct inode *inode,
1511 struct page *locked_page,
1512 u64 delalloc_start,
1513 u64 delalloc_end)
1514{
1515 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1516 unsigned long start_index = index;
1517 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1518 unsigned long pages_locked = 0;
1519 struct page *pages[16];
1520 unsigned long nrpages;
1521 int ret;
1522 int i;
1523
1524 /* the caller is responsible for locking the start index */
1525 if (index == locked_page->index && index == end_index)
1526 return 0;
1527
1528 /* skip the page at the start index */
1529 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001530 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001531 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001532 min_t(unsigned long,
1533 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001534 if (ret == 0) {
1535 ret = -EAGAIN;
1536 goto done;
1537 }
1538 /* now we have an array of pages, lock them all */
1539 for (i = 0; i < ret; i++) {
1540 /*
1541 * the caller is taking responsibility for
1542 * locked_page
1543 */
Chris Mason771ed682008-11-06 22:02:51 -05001544 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001545 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001546 if (!PageDirty(pages[i]) ||
1547 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001548 ret = -EAGAIN;
1549 unlock_page(pages[i]);
1550 page_cache_release(pages[i]);
1551 goto done;
1552 }
1553 }
Chris Masonc8b97812008-10-29 14:49:59 -04001554 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001555 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001556 }
Chris Masonc8b97812008-10-29 14:49:59 -04001557 nrpages -= ret;
1558 index += ret;
1559 cond_resched();
1560 }
1561 ret = 0;
1562done:
1563 if (ret && pages_locked) {
1564 __unlock_for_delalloc(inode, locked_page,
1565 delalloc_start,
1566 ((u64)(start_index + pages_locked - 1)) <<
1567 PAGE_CACHE_SHIFT);
1568 }
1569 return ret;
1570}
1571
1572/*
1573 * find a contiguous range of bytes in the file marked as delalloc, not
1574 * more than 'max_bytes'. start and end are used to return the range,
1575 *
1576 * 1 is returned if we find something, 0 if nothing was in the tree
1577 */
1578static noinline u64 find_lock_delalloc_range(struct inode *inode,
1579 struct extent_io_tree *tree,
1580 struct page *locked_page,
1581 u64 *start, u64 *end,
1582 u64 max_bytes)
1583{
1584 u64 delalloc_start;
1585 u64 delalloc_end;
1586 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001587 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001588 int ret;
1589 int loops = 0;
1590
1591again:
1592 /* step one, find a bunch of delalloc bytes starting at start */
1593 delalloc_start = *start;
1594 delalloc_end = 0;
1595 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001596 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001597 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001598 *start = delalloc_start;
1599 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001600 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001601 return found;
1602 }
1603
1604 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001605 * start comes from the offset of locked_page. We have to lock
1606 * pages in order, so we can't process delalloc bytes before
1607 * locked_page
1608 */
Chris Masond3977122009-01-05 21:25:51 -05001609 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001610 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001611
1612 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001613 * make sure to limit the number of pages we try to lock down
1614 * if we're looping.
1615 */
Chris Masond3977122009-01-05 21:25:51 -05001616 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001617 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001618
Chris Masonc8b97812008-10-29 14:49:59 -04001619 /* step two, lock all the pages after the page that has start */
1620 ret = lock_delalloc_pages(inode, locked_page,
1621 delalloc_start, delalloc_end);
1622 if (ret == -EAGAIN) {
1623 /* some of the pages are gone, lets avoid looping by
1624 * shortening the size of the delalloc range we're searching
1625 */
Chris Mason9655d292009-09-02 15:22:30 -04001626 free_extent_state(cached_state);
Chris Mason8e3ddf42014-05-21 05:49:54 -07001627 cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001628 if (!loops) {
1629 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1630 max_bytes = PAGE_CACHE_SIZE - offset;
1631 loops = 1;
1632 goto again;
1633 } else {
1634 found = 0;
1635 goto out_failed;
1636 }
1637 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001638 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
Chris Masonc8b97812008-10-29 14:49:59 -04001639
1640 /* step three, lock the state bits for the whole range */
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001641 lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001642
1643 /* then test to make sure it is all still delalloc */
1644 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001645 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001646 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001647 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1648 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001649 __unlock_for_delalloc(inode, locked_page,
1650 delalloc_start, delalloc_end);
1651 cond_resched();
1652 goto again;
1653 }
Chris Mason9655d292009-09-02 15:22:30 -04001654 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001655 *start = delalloc_start;
1656 *end = delalloc_end;
1657out_failed:
1658 return found;
1659}
1660
1661int extent_clear_unlock_delalloc(struct inode *inode,
1662 struct extent_io_tree *tree,
1663 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001664 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001665{
1666 int ret;
1667 struct page *pages[16];
1668 unsigned long index = start >> PAGE_CACHE_SHIFT;
1669 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1670 unsigned long nr_pages = end_index - index + 1;
1671 int i;
David Sterba41074882013-04-29 13:38:46 +00001672 unsigned long clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001673
Chris Masona791e352009-10-08 11:27:10 -04001674 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001675 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001676 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001677 clear_bits |= EXTENT_DIRTY;
1678
Chris Masona791e352009-10-08 11:27:10 -04001679 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001680 clear_bits |= EXTENT_DELALLOC;
1681
Chris Mason2c64c532009-09-02 15:04:12 -04001682 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001683 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1684 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1685 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001686 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001687
Chris Masond3977122009-01-05 21:25:51 -05001688 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001689 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001690 min_t(unsigned long,
1691 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001692 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001693
Chris Masona791e352009-10-08 11:27:10 -04001694 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001695 SetPagePrivate2(pages[i]);
1696
Chris Masonc8b97812008-10-29 14:49:59 -04001697 if (pages[i] == locked_page) {
1698 page_cache_release(pages[i]);
1699 continue;
1700 }
Chris Masona791e352009-10-08 11:27:10 -04001701 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001702 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001703 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001704 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001705 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001706 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001707 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001708 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001709 page_cache_release(pages[i]);
1710 }
1711 nr_pages -= ret;
1712 index += ret;
1713 cond_resched();
1714 }
1715 return 0;
1716}
Chris Masonc8b97812008-10-29 14:49:59 -04001717
Chris Masond352ac62008-09-29 15:18:18 -04001718/*
1719 * count the number of bytes in the tree that have a given bit(s)
1720 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1721 * cached. The total number found is returned.
1722 */
Chris Masond1310b22008-01-24 16:13:08 -05001723u64 count_range_bits(struct extent_io_tree *tree,
1724 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001725 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001726{
1727 struct rb_node *node;
1728 struct extent_state *state;
1729 u64 cur_start = *start;
1730 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001731 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001732 int found = 0;
1733
1734 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001735 WARN_ON(1);
1736 return 0;
1737 }
1738
Chris Masoncad321a2008-12-17 14:51:42 -05001739 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001740 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1741 total_bytes = tree->dirty_bytes;
1742 goto out;
1743 }
1744 /*
1745 * this search will find all the extents that end after
1746 * our range starts.
1747 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001748 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001749 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001750 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001751
Chris Masond3977122009-01-05 21:25:51 -05001752 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001753 state = rb_entry(node, struct extent_state, rb_node);
1754 if (state->start > search_end)
1755 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001756 if (contig && found && state->start > last + 1)
1757 break;
1758 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001759 total_bytes += min(search_end, state->end) + 1 -
1760 max(cur_start, state->start);
1761 if (total_bytes >= max_bytes)
1762 break;
1763 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001764 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001765 found = 1;
1766 }
Chris Masonec29ed52011-02-23 16:23:20 -05001767 last = state->end;
1768 } else if (contig && found) {
1769 break;
Chris Masond1310b22008-01-24 16:13:08 -05001770 }
1771 node = rb_next(node);
1772 if (!node)
1773 break;
1774 }
1775out:
Chris Masoncad321a2008-12-17 14:51:42 -05001776 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001777 return total_bytes;
1778}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001779
Chris Masond352ac62008-09-29 15:18:18 -04001780/*
1781 * set the private field for a given byte offset in the tree. If there isn't
1782 * an extent_state there already, this does nothing.
1783 */
Chris Masond1310b22008-01-24 16:13:08 -05001784int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1785{
1786 struct rb_node *node;
1787 struct extent_state *state;
1788 int ret = 0;
1789
Chris Masoncad321a2008-12-17 14:51:42 -05001790 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001791 /*
1792 * this search will find all the extents that end after
1793 * our range starts.
1794 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001795 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001796 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001797 ret = -ENOENT;
1798 goto out;
1799 }
1800 state = rb_entry(node, struct extent_state, rb_node);
1801 if (state->start != start) {
1802 ret = -ENOENT;
1803 goto out;
1804 }
1805 state->private = private;
1806out:
Chris Masoncad321a2008-12-17 14:51:42 -05001807 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001808 return ret;
1809}
1810
Miao Xiee4100d92013-04-05 07:20:56 +00001811void extent_cache_csums_dio(struct extent_io_tree *tree, u64 start, u32 csums[],
1812 int count)
1813{
1814 struct rb_node *node;
1815 struct extent_state *state;
1816
1817 spin_lock(&tree->lock);
1818 /*
1819 * this search will find all the extents that end after
1820 * our range starts.
1821 */
1822 node = tree_search(tree, start);
1823 BUG_ON(!node);
1824
1825 state = rb_entry(node, struct extent_state, rb_node);
1826 BUG_ON(state->start != start);
1827
1828 while (count) {
1829 state->private = *csums++;
1830 count--;
1831 state = next_state(state);
1832 }
1833 spin_unlock(&tree->lock);
1834}
1835
1836static inline u64 __btrfs_get_bio_offset(struct bio *bio, int bio_index)
1837{
1838 struct bio_vec *bvec = bio->bi_io_vec + bio_index;
1839
1840 return page_offset(bvec->bv_page) + bvec->bv_offset;
1841}
1842
1843void extent_cache_csums(struct extent_io_tree *tree, struct bio *bio, int bio_index,
1844 u32 csums[], int count)
1845{
1846 struct rb_node *node;
1847 struct extent_state *state = NULL;
1848 u64 start;
1849
1850 spin_lock(&tree->lock);
1851 do {
1852 start = __btrfs_get_bio_offset(bio, bio_index);
1853 if (state == NULL || state->start != start) {
1854 node = tree_search(tree, start);
1855 BUG_ON(!node);
1856
1857 state = rb_entry(node, struct extent_state, rb_node);
1858 BUG_ON(state->start != start);
1859 }
1860 state->private = *csums++;
1861 count--;
1862 bio_index++;
1863
1864 state = next_state(state);
1865 } while (count);
1866 spin_unlock(&tree->lock);
1867}
1868
Chris Masond1310b22008-01-24 16:13:08 -05001869int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1870{
1871 struct rb_node *node;
1872 struct extent_state *state;
1873 int ret = 0;
1874
Chris Masoncad321a2008-12-17 14:51:42 -05001875 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001876 /*
1877 * this search will find all the extents that end after
1878 * our range starts.
1879 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001880 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001881 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001882 ret = -ENOENT;
1883 goto out;
1884 }
1885 state = rb_entry(node, struct extent_state, rb_node);
1886 if (state->start != start) {
1887 ret = -ENOENT;
1888 goto out;
1889 }
1890 *private = state->private;
1891out:
Chris Masoncad321a2008-12-17 14:51:42 -05001892 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001893 return ret;
1894}
1895
1896/*
1897 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001898 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001899 * has the bits set. Otherwise, 1 is returned if any bit in the
1900 * range is found set.
1901 */
1902int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +00001903 unsigned long bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001904{
1905 struct extent_state *state = NULL;
1906 struct rb_node *node;
1907 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001908
Chris Masoncad321a2008-12-17 14:51:42 -05001909 spin_lock(&tree->lock);
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001910 if (cached && cached->tree && cached->start <= start &&
1911 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001912 node = &cached->rb_node;
1913 else
1914 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001915 while (node && start <= end) {
1916 state = rb_entry(node, struct extent_state, rb_node);
1917
1918 if (filled && state->start > start) {
1919 bitset = 0;
1920 break;
1921 }
1922
1923 if (state->start > end)
1924 break;
1925
1926 if (state->state & bits) {
1927 bitset = 1;
1928 if (!filled)
1929 break;
1930 } else if (filled) {
1931 bitset = 0;
1932 break;
1933 }
Chris Mason46562cec2009-09-23 20:23:16 -04001934
1935 if (state->end == (u64)-1)
1936 break;
1937
Chris Masond1310b22008-01-24 16:13:08 -05001938 start = state->end + 1;
1939 if (start > end)
1940 break;
1941 node = rb_next(node);
1942 if (!node) {
1943 if (filled)
1944 bitset = 0;
1945 break;
1946 }
1947 }
Chris Masoncad321a2008-12-17 14:51:42 -05001948 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001949 return bitset;
1950}
Chris Masond1310b22008-01-24 16:13:08 -05001951
1952/*
1953 * helper function to set a given page up to date if all the
1954 * extents in the tree for that page are up to date
1955 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001956static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001957{
Miao Xie4eee4fa2012-12-21 09:17:45 +00001958 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05001959 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001960 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001961 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001962}
1963
1964/*
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001965 * When IO fails, either with EIO or csum verification fails, we
1966 * try other mirrors that might have a good copy of the data. This
1967 * io_failure_record is used to record state as we go through all the
1968 * mirrors. If another mirror has good data, the page is set up to date
1969 * and things continue. If a good mirror can't be found, the original
1970 * bio end_io callback is called to indicate things have failed.
1971 */
1972struct io_failure_record {
1973 struct page *page;
1974 u64 start;
1975 u64 len;
1976 u64 logical;
1977 unsigned long bio_flags;
1978 int this_mirror;
1979 int failed_mirror;
1980 int in_validation;
1981};
1982
1983static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
1984 int did_repair)
1985{
1986 int ret;
1987 int err = 0;
1988 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1989
1990 set_state_private(failure_tree, rec->start, 0);
1991 ret = clear_extent_bits(failure_tree, rec->start,
1992 rec->start + rec->len - 1,
1993 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1994 if (ret)
1995 err = ret;
1996
David Woodhouse53b381b2013-01-29 18:40:14 -05001997 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1998 rec->start + rec->len - 1,
1999 EXTENT_DAMAGED, GFP_NOFS);
2000 if (ret && !err)
2001 err = ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002002
2003 kfree(rec);
2004 return err;
2005}
2006
2007static void repair_io_failure_callback(struct bio *bio, int err)
2008{
2009 complete(bio->bi_private);
2010}
2011
2012/*
2013 * this bypasses the standard btrfs submit functions deliberately, as
2014 * the standard behavior is to write all copies in a raid setup. here we only
2015 * want to write the one bad copy. so we do the mapping for ourselves and issue
2016 * submit_bio directly.
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002017 * to avoid any synchronization issues, wait for the data after writing, which
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002018 * actually prevents the read that triggered the error from finishing.
2019 * currently, there can be no more than two copies of every data bit. thus,
2020 * exactly one rewrite is required.
2021 */
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002022int repair_io_failure(struct btrfs_fs_info *fs_info, u64 start,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002023 u64 length, u64 logical, struct page *page,
2024 int mirror_num)
2025{
2026 struct bio *bio;
2027 struct btrfs_device *dev;
2028 DECLARE_COMPLETION_ONSTACK(compl);
2029 u64 map_length = 0;
2030 u64 sector;
2031 struct btrfs_bio *bbio = NULL;
David Woodhouse53b381b2013-01-29 18:40:14 -05002032 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002033 int ret;
2034
2035 BUG_ON(!mirror_num);
2036
David Woodhouse53b381b2013-01-29 18:40:14 -05002037 /* we can't repair anything in raid56 yet */
2038 if (btrfs_is_parity_mirror(map_tree, logical, length, mirror_num))
2039 return 0;
2040
Chris Mason9be33952013-05-17 18:30:14 -04002041 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002042 if (!bio)
2043 return -EIO;
2044 bio->bi_private = &compl;
2045 bio->bi_end_io = repair_io_failure_callback;
2046 bio->bi_size = 0;
2047 map_length = length;
2048
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002049 ret = btrfs_map_block(fs_info, WRITE, logical,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002050 &map_length, &bbio, mirror_num);
2051 if (ret) {
2052 bio_put(bio);
2053 return -EIO;
2054 }
2055 BUG_ON(mirror_num != bbio->mirror_num);
2056 sector = bbio->stripes[mirror_num-1].physical >> 9;
2057 bio->bi_sector = sector;
2058 dev = bbio->stripes[mirror_num-1].dev;
2059 kfree(bbio);
2060 if (!dev || !dev->bdev || !dev->writeable) {
2061 bio_put(bio);
2062 return -EIO;
2063 }
2064 bio->bi_bdev = dev->bdev;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002065 bio_add_page(bio, page, length, start - page_offset(page));
Stefan Behrens21adbd52011-11-09 13:44:05 +01002066 btrfsic_submit_bio(WRITE_SYNC, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002067 wait_for_completion(&compl);
2068
2069 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2070 /* try to remap that extent elsewhere? */
2071 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002072 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002073 return -EIO;
2074 }
2075
Anand Jaind5b025d2012-07-02 22:05:21 -06002076 printk_ratelimited_in_rcu(KERN_INFO "btrfs read error corrected: ino %lu off %llu "
Josef Bacik606686e2012-06-04 14:03:51 -04002077 "(dev %s sector %llu)\n", page->mapping->host->i_ino,
2078 start, rcu_str_deref(dev->name), sector);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002079
2080 bio_put(bio);
2081 return 0;
2082}
2083
Josef Bacikea466792012-03-26 21:57:36 -04002084int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
2085 int mirror_num)
2086{
Josef Bacikea466792012-03-26 21:57:36 -04002087 u64 start = eb->start;
2088 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond95603b2012-04-12 15:55:15 -04002089 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04002090
2091 for (i = 0; i < num_pages; i++) {
2092 struct page *p = extent_buffer_page(eb, i);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002093 ret = repair_io_failure(root->fs_info, start, PAGE_CACHE_SIZE,
Josef Bacikea466792012-03-26 21:57:36 -04002094 start, p, mirror_num);
2095 if (ret)
2096 break;
2097 start += PAGE_CACHE_SIZE;
2098 }
2099
2100 return ret;
2101}
2102
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002103/*
2104 * each time an IO finishes, we do a fast check in the IO failure tree
2105 * to see if we need to process or clean up an io_failure_record
2106 */
2107static int clean_io_failure(u64 start, struct page *page)
2108{
2109 u64 private;
2110 u64 private_failure;
2111 struct io_failure_record *failrec;
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002112 struct btrfs_fs_info *fs_info;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002113 struct extent_state *state;
2114 int num_copies;
2115 int did_repair = 0;
2116 int ret;
2117 struct inode *inode = page->mapping->host;
2118
2119 private = 0;
2120 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
2121 (u64)-1, 1, EXTENT_DIRTY, 0);
2122 if (!ret)
2123 return 0;
2124
2125 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
2126 &private_failure);
2127 if (ret)
2128 return 0;
2129
2130 failrec = (struct io_failure_record *)(unsigned long) private_failure;
2131 BUG_ON(!failrec->this_mirror);
2132
2133 if (failrec->in_validation) {
2134 /* there was no real error, just free the record */
2135 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
2136 failrec->start);
2137 did_repair = 1;
2138 goto out;
2139 }
2140
2141 spin_lock(&BTRFS_I(inode)->io_tree.lock);
2142 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
2143 failrec->start,
2144 EXTENT_LOCKED);
2145 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
2146
2147 if (state && state->start == failrec->start) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002148 fs_info = BTRFS_I(inode)->root->fs_info;
2149 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2150 failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002151 if (num_copies > 1) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002152 ret = repair_io_failure(fs_info, start, failrec->len,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002153 failrec->logical, page,
2154 failrec->failed_mirror);
2155 did_repair = !ret;
2156 }
David Woodhouse53b381b2013-01-29 18:40:14 -05002157 ret = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002158 }
2159
2160out:
2161 if (!ret)
2162 ret = free_io_failure(inode, failrec, did_repair);
2163
2164 return ret;
2165}
2166
2167/*
2168 * this is a generic handler for readpage errors (default
2169 * readpage_io_failed_hook). if other copies exist, read those and write back
2170 * good data to the failed position. does not investigate in remapping the
2171 * failed extent elsewhere, hoping the device will be smart enough to do this as
2172 * needed
2173 */
2174
2175static int bio_readpage_error(struct bio *failed_bio, struct page *page,
2176 u64 start, u64 end, int failed_mirror,
2177 struct extent_state *state)
2178{
2179 struct io_failure_record *failrec = NULL;
2180 u64 private;
2181 struct extent_map *em;
2182 struct inode *inode = page->mapping->host;
2183 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2184 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2185 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2186 struct bio *bio;
2187 int num_copies;
2188 int ret;
2189 int read_mode;
2190 u64 logical;
2191
2192 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2193
2194 ret = get_state_private(failure_tree, start, &private);
2195 if (ret) {
2196 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2197 if (!failrec)
2198 return -ENOMEM;
2199 failrec->start = start;
2200 failrec->len = end - start + 1;
2201 failrec->this_mirror = 0;
2202 failrec->bio_flags = 0;
2203 failrec->in_validation = 0;
2204
2205 read_lock(&em_tree->lock);
2206 em = lookup_extent_mapping(em_tree, start, failrec->len);
2207 if (!em) {
2208 read_unlock(&em_tree->lock);
2209 kfree(failrec);
2210 return -EIO;
2211 }
2212
2213 if (em->start > start || em->start + em->len < start) {
2214 free_extent_map(em);
2215 em = NULL;
2216 }
2217 read_unlock(&em_tree->lock);
2218
Tsutomu Itoh7a2d6a62012-10-01 03:07:15 -06002219 if (!em) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002220 kfree(failrec);
2221 return -EIO;
2222 }
2223 logical = start - em->start;
2224 logical = em->block_start + logical;
2225 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2226 logical = em->block_start;
2227 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2228 extent_set_compress_type(&failrec->bio_flags,
2229 em->compress_type);
2230 }
2231 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2232 "len=%llu\n", logical, start, failrec->len);
2233 failrec->logical = logical;
2234 free_extent_map(em);
2235
2236 /* set the bits in the private failure tree */
2237 ret = set_extent_bits(failure_tree, start, end,
2238 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2239 if (ret >= 0)
2240 ret = set_state_private(failure_tree, start,
2241 (u64)(unsigned long)failrec);
2242 /* set the bits in the inode's tree */
2243 if (ret >= 0)
2244 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2245 GFP_NOFS);
2246 if (ret < 0) {
2247 kfree(failrec);
2248 return ret;
2249 }
2250 } else {
2251 failrec = (struct io_failure_record *)(unsigned long)private;
2252 pr_debug("bio_readpage_error: (found) logical=%llu, "
2253 "start=%llu, len=%llu, validation=%d\n",
2254 failrec->logical, failrec->start, failrec->len,
2255 failrec->in_validation);
2256 /*
2257 * when data can be on disk more than twice, add to failrec here
2258 * (e.g. with a list for failed_mirror) to make
2259 * clean_io_failure() clean all those errors at once.
2260 */
2261 }
Stefan Behrens5d964052012-11-05 14:59:07 +01002262 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
2263 failrec->logical, failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002264 if (num_copies == 1) {
2265 /*
2266 * we only have a single copy of the data, so don't bother with
2267 * all the retry and error correction code that follows. no
2268 * matter what the error is, it is very likely to persist.
2269 */
2270 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2271 "state=%p, num_copies=%d, next_mirror %d, "
2272 "failed_mirror %d\n", state, num_copies,
2273 failrec->this_mirror, failed_mirror);
2274 free_io_failure(inode, failrec, 0);
2275 return -EIO;
2276 }
2277
2278 if (!state) {
2279 spin_lock(&tree->lock);
2280 state = find_first_extent_bit_state(tree, failrec->start,
2281 EXTENT_LOCKED);
2282 if (state && state->start != failrec->start)
2283 state = NULL;
2284 spin_unlock(&tree->lock);
2285 }
2286
2287 /*
2288 * there are two premises:
2289 * a) deliver good data to the caller
2290 * b) correct the bad sectors on disk
2291 */
2292 if (failed_bio->bi_vcnt > 1) {
2293 /*
2294 * to fulfill b), we need to know the exact failing sectors, as
2295 * we don't want to rewrite any more than the failed ones. thus,
2296 * we need separate read requests for the failed bio
2297 *
2298 * if the following BUG_ON triggers, our validation request got
2299 * merged. we need separate requests for our algorithm to work.
2300 */
2301 BUG_ON(failrec->in_validation);
2302 failrec->in_validation = 1;
2303 failrec->this_mirror = failed_mirror;
2304 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2305 } else {
2306 /*
2307 * we're ready to fulfill a) and b) alongside. get a good copy
2308 * of the failed sector and if we succeed, we have setup
2309 * everything for repair_io_failure to do the rest for us.
2310 */
2311 if (failrec->in_validation) {
2312 BUG_ON(failrec->this_mirror != failed_mirror);
2313 failrec->in_validation = 0;
2314 failrec->this_mirror = 0;
2315 }
2316 failrec->failed_mirror = failed_mirror;
2317 failrec->this_mirror++;
2318 if (failrec->this_mirror == failed_mirror)
2319 failrec->this_mirror++;
2320 read_mode = READ_SYNC;
2321 }
2322
2323 if (!state || failrec->this_mirror > num_copies) {
2324 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2325 "next_mirror %d, failed_mirror %d\n", state,
2326 num_copies, failrec->this_mirror, failed_mirror);
2327 free_io_failure(inode, failrec, 0);
2328 return -EIO;
2329 }
2330
Chris Mason9be33952013-05-17 18:30:14 -04002331 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04002332 if (!bio) {
2333 free_io_failure(inode, failrec, 0);
2334 return -EIO;
2335 }
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002336 bio->bi_private = state;
2337 bio->bi_end_io = failed_bio->bi_end_io;
2338 bio->bi_sector = failrec->logical >> 9;
2339 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2340 bio->bi_size = 0;
2341
2342 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2343
2344 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2345 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2346 failrec->this_mirror, num_copies, failrec->in_validation);
2347
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002348 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2349 failrec->this_mirror,
2350 failrec->bio_flags, 0);
2351 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002352}
2353
Chris Masond1310b22008-01-24 16:13:08 -05002354/* lots and lots of room for performance fixes in the end_bio funcs */
2355
Jeff Mahoney87826df2012-02-15 16:23:57 +01002356int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2357{
2358 int uptodate = (err == 0);
2359 struct extent_io_tree *tree;
Eric Sandeen26401cb2014-06-12 00:39:58 -05002360 int ret = 0;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002361
2362 tree = &BTRFS_I(page->mapping->host)->io_tree;
2363
2364 if (tree->ops && tree->ops->writepage_end_io_hook) {
2365 ret = tree->ops->writepage_end_io_hook(page, start,
2366 end, NULL, uptodate);
2367 if (ret)
2368 uptodate = 0;
2369 }
2370
Jeff Mahoney87826df2012-02-15 16:23:57 +01002371 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002372 ClearPageUptodate(page);
2373 SetPageError(page);
Liu Bo372fad02014-05-12 12:47:36 +08002374 ret = ret < 0 ? ret : -EIO;
2375 mapping_set_error(page->mapping, ret);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002376 }
2377 return 0;
2378}
2379
Chris Masond1310b22008-01-24 16:13:08 -05002380/*
2381 * after a writepage IO is done, we need to:
2382 * clear the uptodate bits on error
2383 * clear the writeback bits in the extent tree for this IO
2384 * end_page_writeback if the page has no more pending IO
2385 *
2386 * Scheduling is not allowed, so the extent state tree is expected
2387 * to have one and only one object corresponding to this IO.
2388 */
Chris Masond1310b22008-01-24 16:13:08 -05002389static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002390{
Chris Masond1310b22008-01-24 16:13:08 -05002391 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04002392 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002393 u64 start;
2394 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05002395
Chris Masond1310b22008-01-24 16:13:08 -05002396 do {
2397 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04002398 tree = &BTRFS_I(page->mapping->host)->io_tree;
2399
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002400 /* We always issue full-page reads, but if some block
2401 * in a page fails to read, blk_update_request() will
2402 * advance bv_offset and adjust bv_len to compensate.
2403 * Print a warning for nonzero offsets, and an error
2404 * if they don't add up to a full page. */
2405 if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE)
2406 printk("%s page write in btrfs with offset %u and length %u\n",
2407 bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE
2408 ? KERN_ERR "partial" : KERN_INFO "incomplete",
2409 bvec->bv_offset, bvec->bv_len);
Chris Masond1310b22008-01-24 16:13:08 -05002410
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002411 start = page_offset(page);
2412 end = start + bvec->bv_offset + bvec->bv_len - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002413
2414 if (--bvec >= bio->bi_io_vec)
2415 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04002416
Jeff Mahoney87826df2012-02-15 16:23:57 +01002417 if (end_extent_writepage(page, err, start, end))
2418 continue;
Chris Mason70dec802008-01-29 09:59:12 -05002419
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002420 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002421 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04002422
Chris Masond1310b22008-01-24 16:13:08 -05002423 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002424}
2425
2426/*
2427 * after a readpage IO is done, we need to:
2428 * clear the uptodate bits on error
2429 * set the uptodate bits if things worked
2430 * set the page up to date if all extents in the tree are uptodate
2431 * clear the lock bit in the extent tree
2432 * unlock the page if there are no other extents locked for it
2433 *
2434 * Scheduling is not allowed, so the extent state tree is expected
2435 * to have one and only one object corresponding to this IO.
2436 */
Chris Masond1310b22008-01-24 16:13:08 -05002437static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002438{
2439 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00002440 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2441 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04002442 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002443 u64 start;
2444 u64 end;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002445 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002446 int ret;
2447
Chris Masond20f7042008-12-08 16:58:54 -05002448 if (err)
2449 uptodate = 0;
2450
Chris Masond1310b22008-01-24 16:13:08 -05002451 do {
2452 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00002453 struct extent_state *cached = NULL;
2454 struct extent_state *state;
Chris Mason9be33952013-05-17 18:30:14 -04002455 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Arne Jansen507903b2011-04-06 10:02:20 +00002456
Kent Overstreetbe3940c2012-09-11 14:23:05 -06002457 pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, "
Chris Mason9be33952013-05-17 18:30:14 -04002458 "mirror=%lu\n", (u64)bio->bi_sector, err,
2459 io_bio->mirror_num);
David Woodhouse902b22f2008-08-20 08:51:49 -04002460 tree = &BTRFS_I(page->mapping->host)->io_tree;
2461
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002462 /* We always issue full-page reads, but if some block
2463 * in a page fails to read, blk_update_request() will
2464 * advance bv_offset and adjust bv_len to compensate.
2465 * Print a warning for nonzero offsets, and an error
2466 * if they don't add up to a full page. */
2467 if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE)
2468 printk("%s page read in btrfs with offset %u and length %u\n",
2469 bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE
2470 ? KERN_ERR "partial" : KERN_INFO "incomplete",
2471 bvec->bv_offset, bvec->bv_len);
Chris Masond1310b22008-01-24 16:13:08 -05002472
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002473 start = page_offset(page);
2474 end = start + bvec->bv_offset + bvec->bv_len - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002475
Chris Mason4125bf72010-02-03 18:18:45 +00002476 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05002477 prefetchw(&bvec->bv_page->flags);
2478
Arne Jansen507903b2011-04-06 10:02:20 +00002479 spin_lock(&tree->lock);
Chris Mason0d399202011-04-16 06:55:39 -04002480 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
Chris Mason109b36a2011-04-12 13:57:39 -04002481 if (state && state->start == start) {
Arne Jansen507903b2011-04-06 10:02:20 +00002482 /*
2483 * take a reference on the state, unlock will drop
2484 * the ref
2485 */
2486 cache_state(state, &cached);
2487 }
2488 spin_unlock(&tree->lock);
2489
Chris Mason9be33952013-05-17 18:30:14 -04002490 mirror = io_bio->mirror_num;
Chris Masond1310b22008-01-24 16:13:08 -05002491 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05002492 ret = tree->ops->readpage_end_io_hook(page, start, end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002493 state, mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002494 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002495 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002496 else
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002497 clean_io_failure(start, page);
Chris Masond1310b22008-01-24 16:13:08 -05002498 }
Josef Bacikea466792012-03-26 21:57:36 -04002499
Josef Bacikea466792012-03-26 21:57:36 -04002500 if (!uptodate && tree->ops && tree->ops->readpage_io_failed_hook) {
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002501 ret = tree->ops->readpage_io_failed_hook(page, mirror);
Josef Bacikea466792012-03-26 21:57:36 -04002502 if (!ret && !err &&
2503 test_bit(BIO_UPTODATE, &bio->bi_flags))
2504 uptodate = 1;
2505 } else if (!uptodate) {
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002506 /*
2507 * The generic bio_readpage_error handles errors the
2508 * following way: If possible, new read requests are
2509 * created and submitted and will end up in
2510 * end_bio_extent_readpage as well (if we're lucky, not
2511 * in the !uptodate case). In that case it returns 0 and
2512 * we just go on with the next page in our bio. If it
2513 * can't handle the error it will return -EIO and we
2514 * remain responsible for that page.
2515 */
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002516 ret = bio_readpage_error(bio, page, start, end, mirror, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04002517 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04002518 uptodate =
2519 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05002520 if (err)
2521 uptodate = 0;
Arne Jansen507903b2011-04-06 10:02:20 +00002522 uncache_state(&cached);
Chris Mason7e383262008-04-09 16:28:12 -04002523 continue;
2524 }
2525 }
Chris Mason70dec802008-01-29 09:59:12 -05002526
Josef Bacik0b32f4b2012-03-13 09:38:00 -04002527 if (uptodate && tree->track_uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00002528 set_extent_uptodate(tree, start, end, &cached,
David Woodhouse902b22f2008-08-20 08:51:49 -04002529 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05002530 }
Arne Jansen507903b2011-04-06 10:02:20 +00002531 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05002532
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002533 if (uptodate) {
2534 SetPageUptodate(page);
Chris Mason70dec802008-01-29 09:59:12 -05002535 } else {
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002536 ClearPageUptodate(page);
2537 SetPageError(page);
Chris Mason70dec802008-01-29 09:59:12 -05002538 }
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002539 unlock_page(page);
Chris Mason4125bf72010-02-03 18:18:45 +00002540 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05002541
2542 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002543}
2544
Chris Mason9be33952013-05-17 18:30:14 -04002545/*
2546 * this allocates from the btrfs_bioset. We're returning a bio right now
2547 * but you can call btrfs_io_bio for the appropriate container_of magic
2548 */
Miao Xie88f794e2010-11-22 03:02:55 +00002549struct bio *
2550btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2551 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002552{
2553 struct bio *bio;
2554
Chris Mason9be33952013-05-17 18:30:14 -04002555 bio = bio_alloc_bioset(gfp_flags, nr_vecs, btrfs_bioset);
Chris Masond1310b22008-01-24 16:13:08 -05002556
2557 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
Chris Mason9be33952013-05-17 18:30:14 -04002558 while (!bio && (nr_vecs /= 2)) {
2559 bio = bio_alloc_bioset(gfp_flags,
2560 nr_vecs, btrfs_bioset);
2561 }
Chris Masond1310b22008-01-24 16:13:08 -05002562 }
2563
2564 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04002565 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002566 bio->bi_bdev = bdev;
2567 bio->bi_sector = first_sector;
2568 }
2569 return bio;
2570}
2571
Chris Mason9be33952013-05-17 18:30:14 -04002572struct bio *btrfs_bio_clone(struct bio *bio, gfp_t gfp_mask)
2573{
2574 return bio_clone_bioset(bio, gfp_mask, btrfs_bioset);
2575}
2576
2577
2578/* this also allocates from the btrfs_bioset */
2579struct bio *btrfs_io_bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs)
2580{
2581 return bio_alloc_bioset(gfp_mask, nr_iovecs, btrfs_bioset);
2582}
2583
2584
Jeff Mahoney355808c2011-10-03 23:23:14 -04002585static int __must_check submit_one_bio(int rw, struct bio *bio,
2586 int mirror_num, unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002587{
Chris Masond1310b22008-01-24 16:13:08 -05002588 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002589 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2590 struct page *page = bvec->bv_page;
2591 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002592 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002593
Miao Xie4eee4fa2012-12-21 09:17:45 +00002594 start = page_offset(page) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002595
David Woodhouse902b22f2008-08-20 08:51:49 -04002596 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002597
2598 bio_get(bio);
2599
Chris Mason065631f2008-02-20 12:07:25 -05002600 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00002601 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002602 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002603 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01002604 btrfsic_submit_bio(rw, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002605
Chris Masond1310b22008-01-24 16:13:08 -05002606 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2607 ret = -EOPNOTSUPP;
2608 bio_put(bio);
2609 return ret;
2610}
2611
David Woodhouse64a16702009-07-15 23:29:37 +01002612static int merge_bio(int rw, struct extent_io_tree *tree, struct page *page,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002613 unsigned long offset, size_t size, struct bio *bio,
2614 unsigned long bio_flags)
2615{
2616 int ret = 0;
2617 if (tree->ops && tree->ops->merge_bio_hook)
David Woodhouse64a16702009-07-15 23:29:37 +01002618 ret = tree->ops->merge_bio_hook(rw, page, offset, size, bio,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002619 bio_flags);
2620 BUG_ON(ret < 0);
2621 return ret;
2622
2623}
2624
Chris Masond1310b22008-01-24 16:13:08 -05002625static int submit_extent_page(int rw, struct extent_io_tree *tree,
2626 struct page *page, sector_t sector,
2627 size_t size, unsigned long offset,
2628 struct block_device *bdev,
2629 struct bio **bio_ret,
2630 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04002631 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002632 int mirror_num,
2633 unsigned long prev_bio_flags,
2634 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002635{
2636 int ret = 0;
2637 struct bio *bio;
2638 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04002639 int contig = 0;
2640 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2641 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05002642 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002643
2644 if (bio_ret && *bio_ret) {
2645 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002646 if (old_compressed)
2647 contig = bio->bi_sector == sector;
2648 else
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002649 contig = bio_end_sector(bio) == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002650
2651 if (prev_bio_flags != bio_flags || !contig ||
David Woodhouse64a16702009-07-15 23:29:37 +01002652 merge_bio(rw, tree, page, offset, page_size, bio, bio_flags) ||
Chris Masonc8b97812008-10-29 14:49:59 -04002653 bio_add_page(bio, page, page_size, offset) < page_size) {
2654 ret = submit_one_bio(rw, bio, mirror_num,
2655 prev_bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002656 if (ret < 0)
2657 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05002658 bio = NULL;
2659 } else {
2660 return 0;
2661 }
2662 }
Chris Masonc8b97812008-10-29 14:49:59 -04002663 if (this_compressed)
2664 nr = BIO_MAX_PAGES;
2665 else
2666 nr = bio_get_nr_vecs(bdev);
2667
Miao Xie88f794e2010-11-22 03:02:55 +00002668 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002669 if (!bio)
2670 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002671
Chris Masonc8b97812008-10-29 14:49:59 -04002672 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002673 bio->bi_end_io = end_io_func;
2674 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05002675
Chris Masond3977122009-01-05 21:25:51 -05002676 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002677 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002678 else
Chris Masonc8b97812008-10-29 14:49:59 -04002679 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002680
2681 return ret;
2682}
2683
Eric Sandeen48a3b632013-04-25 20:41:01 +00002684static void attach_extent_buffer_page(struct extent_buffer *eb,
2685 struct page *page)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002686{
2687 if (!PagePrivate(page)) {
2688 SetPagePrivate(page);
2689 page_cache_get(page);
2690 set_page_private(page, (unsigned long)eb);
2691 } else {
2692 WARN_ON(page->private != (unsigned long)eb);
2693 }
2694}
2695
Chris Masond1310b22008-01-24 16:13:08 -05002696void set_page_extent_mapped(struct page *page)
2697{
2698 if (!PagePrivate(page)) {
2699 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002700 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002701 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002702 }
2703}
2704
Chris Masond1310b22008-01-24 16:13:08 -05002705/*
2706 * basic readpage implementation. Locked extent state structs are inserted
2707 * into the tree that are removed when the IO is done (by the end_io
2708 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002709 * XXX JDM: This needs looking at to ensure proper page locking
Chris Masond1310b22008-01-24 16:13:08 -05002710 */
2711static int __extent_read_full_page(struct extent_io_tree *tree,
2712 struct page *page,
2713 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002714 struct bio **bio, int mirror_num,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04002715 unsigned long *bio_flags, int rw)
Chris Masond1310b22008-01-24 16:13:08 -05002716{
2717 struct inode *inode = page->mapping->host;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002718 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05002719 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2720 u64 end;
2721 u64 cur = start;
2722 u64 extent_offset;
2723 u64 last_byte = i_size_read(inode);
2724 u64 block_start;
2725 u64 cur_end;
2726 sector_t sector;
2727 struct extent_map *em;
2728 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002729 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05002730 int ret;
2731 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002732 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002733 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002734 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002735 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002736 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002737
2738 set_page_extent_mapped(page);
2739
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002740 if (!PageUptodate(page)) {
2741 if (cleancache_get_page(page) == 0) {
2742 BUG_ON(blocksize != PAGE_SIZE);
2743 goto out;
2744 }
2745 }
2746
Chris Masond1310b22008-01-24 16:13:08 -05002747 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002748 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002749 lock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002750 ordered = btrfs_lookup_ordered_extent(inode, start);
2751 if (!ordered)
2752 break;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002753 unlock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002754 btrfs_start_ordered_extent(inode, ordered, 1);
2755 btrfs_put_ordered_extent(ordered);
2756 }
Chris Masond1310b22008-01-24 16:13:08 -05002757
Chris Masonc8b97812008-10-29 14:49:59 -04002758 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2759 char *userpage;
2760 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2761
2762 if (zero_offset) {
2763 iosize = PAGE_CACHE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002764 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002765 memset(userpage + zero_offset, 0, iosize);
2766 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002767 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002768 }
2769 }
Chris Masond1310b22008-01-24 16:13:08 -05002770 while (cur <= end) {
Josef Bacikc8f2f242013-02-11 11:33:00 -05002771 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2772
Chris Masond1310b22008-01-24 16:13:08 -05002773 if (cur >= last_byte) {
2774 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002775 struct extent_state *cached = NULL;
2776
David Sterba306e16c2011-04-19 14:29:38 +02002777 iosize = PAGE_CACHE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002778 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002779 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002780 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002781 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002782 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002783 &cached, GFP_NOFS);
2784 unlock_extent_cached(tree, cur, cur + iosize - 1,
2785 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002786 break;
2787 }
David Sterba306e16c2011-04-19 14:29:38 +02002788 em = get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002789 end - cur + 1, 0);
David Sterbac7040052011-04-19 18:00:01 +02002790 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002791 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002792 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002793 break;
2794 }
Chris Masond1310b22008-01-24 16:13:08 -05002795 extent_offset = cur - em->start;
2796 BUG_ON(extent_map_end(em) <= cur);
2797 BUG_ON(end < cur);
2798
Li Zefan261507a02010-12-17 14:21:50 +08002799 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002800 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002801 extent_set_compress_type(&this_bio_flag,
2802 em->compress_type);
2803 }
Chris Masonc8b97812008-10-29 14:49:59 -04002804
Chris Masond1310b22008-01-24 16:13:08 -05002805 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2806 cur_end = min(extent_map_end(em) - 1, end);
Qu Wenruofda28322013-02-26 08:10:22 +00002807 iosize = ALIGN(iosize, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -04002808 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2809 disk_io_size = em->block_len;
2810 sector = em->block_start >> 9;
2811 } else {
2812 sector = (em->block_start + extent_offset) >> 9;
2813 disk_io_size = iosize;
2814 }
Chris Masond1310b22008-01-24 16:13:08 -05002815 bdev = em->bdev;
2816 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002817 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2818 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002819 free_extent_map(em);
2820 em = NULL;
2821
2822 /* we've found a hole, just zero and go on */
2823 if (block_start == EXTENT_MAP_HOLE) {
2824 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002825 struct extent_state *cached = NULL;
2826
Cong Wang7ac687d2011-11-25 23:14:28 +08002827 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002828 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002829 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002830 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002831
2832 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002833 &cached, GFP_NOFS);
2834 unlock_extent_cached(tree, cur, cur + iosize - 1,
2835 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002836 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002837 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002838 continue;
2839 }
2840 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002841 if (test_range_bit(tree, cur, cur_end,
2842 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002843 check_page_uptodate(tree, page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002844 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002845 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002846 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002847 continue;
2848 }
Chris Mason70dec802008-01-29 09:59:12 -05002849 /* we have an inline extent but it didn't get marked up
2850 * to date. Error out
2851 */
2852 if (block_start == EXTENT_MAP_INLINE) {
2853 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002854 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05002855 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002856 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05002857 continue;
2858 }
Chris Masond1310b22008-01-24 16:13:08 -05002859
Josef Bacikc8f2f242013-02-11 11:33:00 -05002860 pnr -= page->index;
Josef Bacikd4c7ca82013-04-19 19:49:09 -04002861 ret = submit_extent_page(rw, tree, page,
David Sterba306e16c2011-04-19 14:29:38 +02002862 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04002863 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002864 end_bio_extent_readpage, mirror_num,
2865 *bio_flags,
2866 this_bio_flag);
Josef Bacikc8f2f242013-02-11 11:33:00 -05002867 if (!ret) {
2868 nr++;
2869 *bio_flags = this_bio_flag;
2870 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002871 SetPageError(page);
Josef Bacikedd33c92012-10-05 16:40:32 -04002872 unlock_extent(tree, cur, cur + iosize - 1);
2873 }
Chris Masond1310b22008-01-24 16:13:08 -05002874 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002875 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002876 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002877out:
Chris Masond1310b22008-01-24 16:13:08 -05002878 if (!nr) {
2879 if (!PageError(page))
2880 SetPageUptodate(page);
2881 unlock_page(page);
2882 }
2883 return 0;
2884}
2885
2886int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002887 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05002888{
2889 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002890 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002891 int ret;
2892
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002893 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04002894 &bio_flags, READ);
Chris Masond1310b22008-01-24 16:13:08 -05002895 if (bio)
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002896 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002897 return ret;
2898}
Chris Masond1310b22008-01-24 16:13:08 -05002899
Chris Mason11c83492009-04-20 15:50:09 -04002900static noinline void update_nr_written(struct page *page,
2901 struct writeback_control *wbc,
2902 unsigned long nr_written)
2903{
2904 wbc->nr_to_write -= nr_written;
2905 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2906 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2907 page->mapping->writeback_index = page->index + nr_written;
2908}
2909
Chris Masond1310b22008-01-24 16:13:08 -05002910/*
2911 * the writepage semantics are similar to regular writepage. extent
2912 * records are inserted to lock ranges in the tree, and as dirty areas
2913 * are found, they are marked writeback. Then the lock bits are removed
2914 * and the end_io handler clears the writeback ranges
2915 */
2916static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2917 void *data)
2918{
2919 struct inode *inode = page->mapping->host;
2920 struct extent_page_data *epd = data;
2921 struct extent_io_tree *tree = epd->tree;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002922 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05002923 u64 delalloc_start;
2924 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2925 u64 end;
2926 u64 cur = start;
2927 u64 extent_offset;
2928 u64 last_byte = i_size_read(inode);
2929 u64 block_start;
2930 u64 iosize;
2931 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002932 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002933 struct extent_map *em;
2934 struct block_device *bdev;
2935 int ret;
2936 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002937 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002938 size_t blocksize;
2939 loff_t i_size = i_size_read(inode);
2940 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2941 u64 nr_delalloc;
2942 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002943 int page_started;
2944 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002945 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002946 unsigned long nr_written = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002947 bool fill_delalloc = true;
Chris Masond1310b22008-01-24 16:13:08 -05002948
Chris Masonffbd5172009-04-20 15:50:09 -04002949 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002950 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002951 else
2952 write_flags = WRITE;
2953
liubo1abe9b82011-03-24 11:18:59 +00002954 trace___extent_writepage(page, inode, wbc);
2955
Chris Masond1310b22008-01-24 16:13:08 -05002956 WARN_ON(!PageLocked(page));
Chris Masonbf0da8c2011-11-04 12:29:37 -04002957
2958 ClearPageError(page);
2959
Chris Mason7f3c74f2008-07-18 12:01:11 -04002960 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002961 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002962 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002963 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002964 unlock_page(page);
2965 return 0;
2966 }
2967
2968 if (page->index == end_index) {
2969 char *userpage;
2970
Cong Wang7ac687d2011-11-25 23:14:28 +08002971 userpage = kmap_atomic(page);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002972 memset(userpage + pg_offset, 0,
2973 PAGE_CACHE_SIZE - pg_offset);
Cong Wang7ac687d2011-11-25 23:14:28 +08002974 kunmap_atomic(userpage);
Chris Mason211c17f2008-05-15 09:13:45 -04002975 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002976 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002977 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002978
2979 set_page_extent_mapped(page);
2980
Josef Bacik9e487102011-08-01 12:08:18 -04002981 if (!tree->ops || !tree->ops->fill_delalloc)
2982 fill_delalloc = false;
2983
Chris Masond1310b22008-01-24 16:13:08 -05002984 delalloc_start = start;
2985 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002986 page_started = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002987 if (!epd->extent_locked && fill_delalloc) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002988 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002989 /*
2990 * make sure the wbc mapping index is at least updated
2991 * to this page.
2992 */
2993 update_nr_written(page, wbc, 0);
2994
Chris Masond3977122009-01-05 21:25:51 -05002995 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002996 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002997 page,
2998 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002999 &delalloc_end,
3000 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05003001 if (nr_delalloc == 0) {
3002 delalloc_start = delalloc_end + 1;
3003 continue;
3004 }
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09003005 ret = tree->ops->fill_delalloc(inode, page,
3006 delalloc_start,
3007 delalloc_end,
3008 &page_started,
3009 &nr_written);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003010 /* File system has been set read-only */
3011 if (ret) {
3012 SetPageError(page);
3013 goto done;
3014 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003015 /*
3016 * delalloc_end is already one less than the total
3017 * length, so we don't subtract one from
3018 * PAGE_CACHE_SIZE
3019 */
3020 delalloc_to_write += (delalloc_end - delalloc_start +
3021 PAGE_CACHE_SIZE) >>
3022 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003023 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05003024 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003025 if (wbc->nr_to_write < delalloc_to_write) {
3026 int thresh = 8192;
3027
3028 if (delalloc_to_write < thresh * 2)
3029 thresh = delalloc_to_write;
3030 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3031 thresh);
3032 }
Chris Masonc8b97812008-10-29 14:49:59 -04003033
Chris Mason771ed682008-11-06 22:02:51 -05003034 /* did the fill delalloc function already unlock and start
3035 * the IO?
3036 */
3037 if (page_started) {
3038 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04003039 /*
3040 * we've unlocked the page, so we can't update
3041 * the mapping's writeback index, just update
3042 * nr_to_write.
3043 */
3044 wbc->nr_to_write -= nr_written;
3045 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05003046 }
Chris Masonc8b97812008-10-29 14:49:59 -04003047 }
Chris Mason247e7432008-07-17 12:53:51 -04003048 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04003049 ret = tree->ops->writepage_start_hook(page, start,
3050 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01003051 if (ret) {
3052 /* Fixup worker will requeue */
3053 if (ret == -EBUSY)
3054 wbc->pages_skipped++;
3055 else
3056 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04003057 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04003058 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05003059 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04003060 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04003061 }
3062 }
3063
Chris Mason11c83492009-04-20 15:50:09 -04003064 /*
3065 * we don't want to touch the inode after unlocking the page,
3066 * so we update the mapping writeback index now
3067 */
3068 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05003069
Chris Masond1310b22008-01-24 16:13:08 -05003070 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05003071 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003072 if (tree->ops && tree->ops->writepage_end_io_hook)
3073 tree->ops->writepage_end_io_hook(page, start,
3074 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003075 goto done;
3076 }
3077
Chris Masond1310b22008-01-24 16:13:08 -05003078 blocksize = inode->i_sb->s_blocksize;
3079
3080 while (cur <= end) {
3081 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003082 if (tree->ops && tree->ops->writepage_end_io_hook)
3083 tree->ops->writepage_end_io_hook(page, cur,
3084 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003085 break;
3086 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003087 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05003088 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02003089 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003090 SetPageError(page);
3091 break;
3092 }
3093
3094 extent_offset = cur - em->start;
3095 BUG_ON(extent_map_end(em) <= cur);
3096 BUG_ON(end < cur);
3097 iosize = min(extent_map_end(em) - cur, end - cur + 1);
Qu Wenruofda28322013-02-26 08:10:22 +00003098 iosize = ALIGN(iosize, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05003099 sector = (em->block_start + extent_offset) >> 9;
3100 bdev = em->bdev;
3101 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003102 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05003103 free_extent_map(em);
3104 em = NULL;
3105
Chris Masonc8b97812008-10-29 14:49:59 -04003106 /*
3107 * compressed and inline extents are written through other
3108 * paths in the FS
3109 */
3110 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003111 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003112 /*
3113 * end_io notification does not happen here for
3114 * compressed extents
3115 */
3116 if (!compressed && tree->ops &&
3117 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003118 tree->ops->writepage_end_io_hook(page, cur,
3119 cur + iosize - 1,
3120 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003121 else if (compressed) {
3122 /* we don't want to end_page_writeback on
3123 * a compressed extent. this happens
3124 * elsewhere
3125 */
3126 nr++;
3127 }
3128
3129 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003130 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003131 continue;
3132 }
Chris Masond1310b22008-01-24 16:13:08 -05003133 /* leave this out until we have a page_mkwrite call */
3134 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04003135 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05003136 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003137 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003138 continue;
3139 }
Chris Masonc8b97812008-10-29 14:49:59 -04003140
Chris Masond1310b22008-01-24 16:13:08 -05003141 if (tree->ops && tree->ops->writepage_io_hook) {
3142 ret = tree->ops->writepage_io_hook(page, cur,
3143 cur + iosize - 1);
3144 } else {
3145 ret = 0;
3146 }
Chris Mason1259ab72008-05-12 13:39:03 -04003147 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003148 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003149 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003150 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003151
Chris Masond1310b22008-01-24 16:13:08 -05003152 set_range_writeback(tree, cur, cur + iosize - 1);
3153 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05003154 printk(KERN_ERR "btrfs warning page %lu not "
3155 "writeback, cur %llu end %llu\n",
3156 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05003157 (unsigned long long)end);
3158 }
3159
Chris Masonffbd5172009-04-20 15:50:09 -04003160 ret = submit_extent_page(write_flags, tree, page,
3161 sector, iosize, pg_offset,
3162 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04003163 end_bio_extent_writepage,
3164 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003165 if (ret)
3166 SetPageError(page);
3167 }
3168 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003169 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003170 nr++;
3171 }
3172done:
3173 if (nr == 0) {
3174 /* make sure the mapping tag for page dirty gets cleared */
3175 set_page_writeback(page);
3176 end_page_writeback(page);
3177 }
Chris Masond1310b22008-01-24 16:13:08 -05003178 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05003179
Chris Mason11c83492009-04-20 15:50:09 -04003180done_unlocked:
3181
Chris Mason2c64c532009-09-02 15:04:12 -04003182 /* drop our reference on any cached states */
3183 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05003184 return 0;
3185}
3186
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003187static int eb_wait(void *word)
3188{
3189 io_schedule();
3190 return 0;
3191}
3192
Josef Bacikfd8b2b62013-04-24 16:41:19 -04003193void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003194{
3195 wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
3196 TASK_UNINTERRUPTIBLE);
3197}
3198
3199static int lock_extent_buffer_for_io(struct extent_buffer *eb,
3200 struct btrfs_fs_info *fs_info,
3201 struct extent_page_data *epd)
3202{
3203 unsigned long i, num_pages;
3204 int flush = 0;
3205 int ret = 0;
3206
3207 if (!btrfs_try_tree_write_lock(eb)) {
3208 flush = 1;
3209 flush_write_bio(epd);
3210 btrfs_tree_lock(eb);
3211 }
3212
3213 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3214 btrfs_tree_unlock(eb);
3215 if (!epd->sync_io)
3216 return 0;
3217 if (!flush) {
3218 flush_write_bio(epd);
3219 flush = 1;
3220 }
Chris Masona098d8e2012-03-21 12:09:56 -04003221 while (1) {
3222 wait_on_extent_buffer_writeback(eb);
3223 btrfs_tree_lock(eb);
3224 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3225 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003226 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003227 }
3228 }
3229
Josef Bacik51561ff2012-07-20 16:25:24 -04003230 /*
3231 * We need to do this to prevent races in people who check if the eb is
3232 * under IO since we can end up having no IO bits set for a short period
3233 * of time.
3234 */
3235 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003236 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3237 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003238 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003239 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Miao Xiee2d84522013-01-29 10:09:20 +00003240 __percpu_counter_add(&fs_info->dirty_metadata_bytes,
3241 -eb->len,
3242 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003243 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003244 } else {
3245 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003246 }
3247
3248 btrfs_tree_unlock(eb);
3249
3250 if (!ret)
3251 return ret;
3252
3253 num_pages = num_extent_pages(eb->start, eb->len);
3254 for (i = 0; i < num_pages; i++) {
3255 struct page *p = extent_buffer_page(eb, i);
3256
3257 if (!trylock_page(p)) {
3258 if (!flush) {
3259 flush_write_bio(epd);
3260 flush = 1;
3261 }
3262 lock_page(p);
3263 }
3264 }
3265
3266 return ret;
3267}
3268
3269static void end_extent_buffer_writeback(struct extent_buffer *eb)
3270{
3271 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3272 smp_mb__after_clear_bit();
3273 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3274}
3275
3276static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
3277{
3278 int uptodate = err == 0;
3279 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
3280 struct extent_buffer *eb;
3281 int done;
3282
3283 do {
3284 struct page *page = bvec->bv_page;
3285
3286 bvec--;
3287 eb = (struct extent_buffer *)page->private;
3288 BUG_ON(!eb);
3289 done = atomic_dec_and_test(&eb->io_pages);
3290
3291 if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
3292 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3293 ClearPageUptodate(page);
3294 SetPageError(page);
3295 }
3296
3297 end_page_writeback(page);
3298
3299 if (!done)
3300 continue;
3301
3302 end_extent_buffer_writeback(eb);
3303 } while (bvec >= bio->bi_io_vec);
3304
3305 bio_put(bio);
3306
3307}
3308
3309static int write_one_eb(struct extent_buffer *eb,
3310 struct btrfs_fs_info *fs_info,
3311 struct writeback_control *wbc,
3312 struct extent_page_data *epd)
3313{
3314 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
3315 u64 offset = eb->start;
3316 unsigned long i, num_pages;
Josef Bacikde0022b2012-09-25 14:25:58 -04003317 unsigned long bio_flags = 0;
Josef Bacikd4c7ca82013-04-19 19:49:09 -04003318 int rw = (epd->sync_io ? WRITE_SYNC : WRITE) | REQ_META;
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003319 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003320
3321 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3322 num_pages = num_extent_pages(eb->start, eb->len);
3323 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003324 if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
3325 bio_flags = EXTENT_BIO_TREE_LOG;
3326
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003327 for (i = 0; i < num_pages; i++) {
3328 struct page *p = extent_buffer_page(eb, i);
3329
3330 clear_page_dirty_for_io(p);
3331 set_page_writeback(p);
3332 ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
3333 PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
3334 -1, end_bio_extent_buffer_writepage,
Josef Bacikde0022b2012-09-25 14:25:58 -04003335 0, epd->bio_flags, bio_flags);
3336 epd->bio_flags = bio_flags;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003337 if (ret) {
3338 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3339 SetPageError(p);
3340 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3341 end_extent_buffer_writeback(eb);
3342 ret = -EIO;
3343 break;
3344 }
3345 offset += PAGE_CACHE_SIZE;
3346 update_nr_written(p, wbc, 1);
3347 unlock_page(p);
3348 }
3349
3350 if (unlikely(ret)) {
3351 for (; i < num_pages; i++) {
3352 struct page *p = extent_buffer_page(eb, i);
3353 unlock_page(p);
3354 }
3355 }
3356
3357 return ret;
3358}
3359
3360int btree_write_cache_pages(struct address_space *mapping,
3361 struct writeback_control *wbc)
3362{
3363 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3364 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3365 struct extent_buffer *eb, *prev_eb = NULL;
3366 struct extent_page_data epd = {
3367 .bio = NULL,
3368 .tree = tree,
3369 .extent_locked = 0,
3370 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003371 .bio_flags = 0,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003372 };
3373 int ret = 0;
3374 int done = 0;
3375 int nr_to_write_done = 0;
3376 struct pagevec pvec;
3377 int nr_pages;
3378 pgoff_t index;
3379 pgoff_t end; /* Inclusive */
3380 int scanned = 0;
3381 int tag;
3382
3383 pagevec_init(&pvec, 0);
3384 if (wbc->range_cyclic) {
3385 index = mapping->writeback_index; /* Start from prev offset */
3386 end = -1;
3387 } else {
3388 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3389 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3390 scanned = 1;
3391 }
3392 if (wbc->sync_mode == WB_SYNC_ALL)
3393 tag = PAGECACHE_TAG_TOWRITE;
3394 else
3395 tag = PAGECACHE_TAG_DIRTY;
3396retry:
3397 if (wbc->sync_mode == WB_SYNC_ALL)
3398 tag_pages_for_writeback(mapping, index, end);
3399 while (!done && !nr_to_write_done && (index <= end) &&
3400 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3401 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3402 unsigned i;
3403
3404 scanned = 1;
3405 for (i = 0; i < nr_pages; i++) {
3406 struct page *page = pvec.pages[i];
3407
3408 if (!PagePrivate(page))
3409 continue;
3410
3411 if (!wbc->range_cyclic && page->index > end) {
3412 done = 1;
3413 break;
3414 }
3415
Josef Bacikb5bae262012-09-14 13:43:01 -04003416 spin_lock(&mapping->private_lock);
3417 if (!PagePrivate(page)) {
3418 spin_unlock(&mapping->private_lock);
3419 continue;
3420 }
3421
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003422 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003423
3424 /*
3425 * Shouldn't happen and normally this would be a BUG_ON
3426 * but no sense in crashing the users box for something
3427 * we can survive anyway.
3428 */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003429 if (!eb) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003430 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003431 WARN_ON(1);
3432 continue;
3433 }
3434
Josef Bacikb5bae262012-09-14 13:43:01 -04003435 if (eb == prev_eb) {
3436 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003437 continue;
3438 }
3439
Josef Bacikb5bae262012-09-14 13:43:01 -04003440 ret = atomic_inc_not_zero(&eb->refs);
3441 spin_unlock(&mapping->private_lock);
3442 if (!ret)
3443 continue;
3444
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003445 prev_eb = eb;
3446 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3447 if (!ret) {
3448 free_extent_buffer(eb);
3449 continue;
3450 }
3451
3452 ret = write_one_eb(eb, fs_info, wbc, &epd);
3453 if (ret) {
3454 done = 1;
3455 free_extent_buffer(eb);
3456 break;
3457 }
3458 free_extent_buffer(eb);
3459
3460 /*
3461 * the filesystem may choose to bump up nr_to_write.
3462 * We have to make sure to honor the new nr_to_write
3463 * at any time
3464 */
3465 nr_to_write_done = wbc->nr_to_write <= 0;
3466 }
3467 pagevec_release(&pvec);
3468 cond_resched();
3469 }
3470 if (!scanned && !done) {
3471 /*
3472 * We hit the last page and there is more work to be done: wrap
3473 * back to the start of the file
3474 */
3475 scanned = 1;
3476 index = 0;
3477 goto retry;
3478 }
3479 flush_write_bio(&epd);
3480 return ret;
3481}
3482
Chris Masond1310b22008-01-24 16:13:08 -05003483/**
Chris Mason4bef0842008-09-08 11:18:08 -04003484 * 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 -05003485 * @mapping: address space structure to write
3486 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3487 * @writepage: function called for each page
3488 * @data: data passed to writepage function
3489 *
3490 * If a page is already under I/O, write_cache_pages() skips it, even
3491 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3492 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3493 * and msync() need to guarantee that all the data which was dirty at the time
3494 * the call was made get new I/O started against them. If wbc->sync_mode is
3495 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3496 * existing IO to complete.
3497 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003498static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04003499 struct address_space *mapping,
3500 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003501 writepage_t writepage, void *data,
3502 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05003503{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003504 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003505 int ret = 0;
3506 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003507 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003508 struct pagevec pvec;
3509 int nr_pages;
3510 pgoff_t index;
3511 pgoff_t end; /* Inclusive */
3512 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003513 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003514
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003515 /*
3516 * We have to hold onto the inode so that ordered extents can do their
3517 * work when the IO finishes. The alternative to this is failing to add
3518 * an ordered extent if the igrab() fails there and that is a huge pain
3519 * to deal with, so instead just hold onto the inode throughout the
3520 * writepages operation. If it fails here we are freeing up the inode
3521 * anyway and we'd rather not waste our time writing out stuff that is
3522 * going to be truncated anyway.
3523 */
3524 if (!igrab(inode))
3525 return 0;
3526
Chris Masond1310b22008-01-24 16:13:08 -05003527 pagevec_init(&pvec, 0);
3528 if (wbc->range_cyclic) {
3529 index = mapping->writeback_index; /* Start from prev offset */
3530 end = -1;
3531 } else {
3532 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3533 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003534 scanned = 1;
3535 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003536 if (wbc->sync_mode == WB_SYNC_ALL)
3537 tag = PAGECACHE_TAG_TOWRITE;
3538 else
3539 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003540retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003541 if (wbc->sync_mode == WB_SYNC_ALL)
3542 tag_pages_for_writeback(mapping, index, end);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003543 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003544 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3545 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003546 unsigned i;
3547
3548 scanned = 1;
3549 for (i = 0; i < nr_pages; i++) {
3550 struct page *page = pvec.pages[i];
3551
3552 /*
3553 * At this point we hold neither mapping->tree_lock nor
3554 * lock on the page itself: the page may be truncated or
3555 * invalidated (changing page->mapping to NULL), or even
3556 * swizzled back from swapper_space to tmpfs file
3557 * mapping
3558 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05003559 if (!trylock_page(page)) {
3560 flush_fn(data);
3561 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04003562 }
Chris Masond1310b22008-01-24 16:13:08 -05003563
3564 if (unlikely(page->mapping != mapping)) {
3565 unlock_page(page);
3566 continue;
3567 }
3568
3569 if (!wbc->range_cyclic && page->index > end) {
3570 done = 1;
3571 unlock_page(page);
3572 continue;
3573 }
3574
Chris Masond2c3f4f2008-11-19 12:44:22 -05003575 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003576 if (PageWriteback(page))
3577 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05003578 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003579 }
Chris Masond1310b22008-01-24 16:13:08 -05003580
3581 if (PageWriteback(page) ||
3582 !clear_page_dirty_for_io(page)) {
3583 unlock_page(page);
3584 continue;
3585 }
3586
3587 ret = (*writepage)(page, wbc, data);
3588
3589 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3590 unlock_page(page);
3591 ret = 0;
3592 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003593 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05003594 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003595
3596 /*
3597 * the filesystem may choose to bump up nr_to_write.
3598 * We have to make sure to honor the new nr_to_write
3599 * at any time
3600 */
3601 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05003602 }
3603 pagevec_release(&pvec);
3604 cond_resched();
3605 }
3606 if (!scanned && !done) {
3607 /*
3608 * We hit the last page and there is more work to be done: wrap
3609 * back to the start of the file
3610 */
3611 scanned = 1;
3612 index = 0;
3613 goto retry;
3614 }
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003615 btrfs_add_delayed_iput(inode);
Chris Masond1310b22008-01-24 16:13:08 -05003616 return ret;
3617}
Chris Masond1310b22008-01-24 16:13:08 -05003618
Chris Masonffbd5172009-04-20 15:50:09 -04003619static void flush_epd_write_bio(struct extent_page_data *epd)
3620{
3621 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04003622 int rw = WRITE;
3623 int ret;
3624
Chris Masonffbd5172009-04-20 15:50:09 -04003625 if (epd->sync_io)
Jeff Mahoney355808c2011-10-03 23:23:14 -04003626 rw = WRITE_SYNC;
3627
Josef Bacikde0022b2012-09-25 14:25:58 -04003628 ret = submit_one_bio(rw, epd->bio, 0, epd->bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003629 BUG_ON(ret < 0); /* -ENOMEM */
Chris Masonffbd5172009-04-20 15:50:09 -04003630 epd->bio = NULL;
3631 }
3632}
3633
Chris Masond2c3f4f2008-11-19 12:44:22 -05003634static noinline void flush_write_bio(void *data)
3635{
3636 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04003637 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003638}
3639
Chris Masond1310b22008-01-24 16:13:08 -05003640int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3641 get_extent_t *get_extent,
3642 struct writeback_control *wbc)
3643{
3644 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003645 struct extent_page_data epd = {
3646 .bio = NULL,
3647 .tree = tree,
3648 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003649 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003650 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003651 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05003652 };
Chris Masond1310b22008-01-24 16:13:08 -05003653
Chris Masond1310b22008-01-24 16:13:08 -05003654 ret = __extent_writepage(page, wbc, &epd);
3655
Chris Masonffbd5172009-04-20 15:50:09 -04003656 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003657 return ret;
3658}
Chris Masond1310b22008-01-24 16:13:08 -05003659
Chris Mason771ed682008-11-06 22:02:51 -05003660int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3661 u64 start, u64 end, get_extent_t *get_extent,
3662 int mode)
3663{
3664 int ret = 0;
3665 struct address_space *mapping = inode->i_mapping;
3666 struct page *page;
3667 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3668 PAGE_CACHE_SHIFT;
3669
3670 struct extent_page_data epd = {
3671 .bio = NULL,
3672 .tree = tree,
3673 .get_extent = get_extent,
3674 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04003675 .sync_io = mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003676 .bio_flags = 0,
Chris Mason771ed682008-11-06 22:02:51 -05003677 };
3678 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05003679 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05003680 .nr_to_write = nr_pages * 2,
3681 .range_start = start,
3682 .range_end = end + 1,
3683 };
3684
Chris Masond3977122009-01-05 21:25:51 -05003685 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05003686 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3687 if (clear_page_dirty_for_io(page))
3688 ret = __extent_writepage(page, &wbc_writepages, &epd);
3689 else {
3690 if (tree->ops && tree->ops->writepage_end_io_hook)
3691 tree->ops->writepage_end_io_hook(page, start,
3692 start + PAGE_CACHE_SIZE - 1,
3693 NULL, 1);
3694 unlock_page(page);
3695 }
3696 page_cache_release(page);
3697 start += PAGE_CACHE_SIZE;
3698 }
3699
Chris Masonffbd5172009-04-20 15:50:09 -04003700 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05003701 return ret;
3702}
Chris Masond1310b22008-01-24 16:13:08 -05003703
3704int extent_writepages(struct extent_io_tree *tree,
3705 struct address_space *mapping,
3706 get_extent_t *get_extent,
3707 struct writeback_control *wbc)
3708{
3709 int ret = 0;
3710 struct extent_page_data epd = {
3711 .bio = NULL,
3712 .tree = tree,
3713 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003714 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003715 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003716 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05003717 };
3718
Chris Mason4bef0842008-09-08 11:18:08 -04003719 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003720 __extent_writepage, &epd,
3721 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04003722 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003723 return ret;
3724}
Chris Masond1310b22008-01-24 16:13:08 -05003725
3726int extent_readpages(struct extent_io_tree *tree,
3727 struct address_space *mapping,
3728 struct list_head *pages, unsigned nr_pages,
3729 get_extent_t get_extent)
3730{
3731 struct bio *bio = NULL;
3732 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04003733 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06003734 struct page *pagepool[16];
3735 struct page *page;
3736 int i = 0;
3737 int nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003738
Chris Masond1310b22008-01-24 16:13:08 -05003739 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
Liu Bo67c96842012-07-20 21:43:09 -06003740 page = list_entry(pages->prev, struct page, lru);
Chris Masond1310b22008-01-24 16:13:08 -05003741
3742 prefetchw(&page->flags);
3743 list_del(&page->lru);
Liu Bo67c96842012-07-20 21:43:09 -06003744 if (add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04003745 page->index, GFP_NOFS)) {
Liu Bo67c96842012-07-20 21:43:09 -06003746 page_cache_release(page);
3747 continue;
Chris Masond1310b22008-01-24 16:13:08 -05003748 }
Liu Bo67c96842012-07-20 21:43:09 -06003749
3750 pagepool[nr++] = page;
3751 if (nr < ARRAY_SIZE(pagepool))
3752 continue;
3753 for (i = 0; i < nr; i++) {
3754 __extent_read_full_page(tree, pagepool[i], get_extent,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04003755 &bio, 0, &bio_flags, READ);
Liu Bo67c96842012-07-20 21:43:09 -06003756 page_cache_release(pagepool[i]);
3757 }
3758 nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003759 }
Liu Bo67c96842012-07-20 21:43:09 -06003760 for (i = 0; i < nr; i++) {
3761 __extent_read_full_page(tree, pagepool[i], get_extent,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04003762 &bio, 0, &bio_flags, READ);
Liu Bo67c96842012-07-20 21:43:09 -06003763 page_cache_release(pagepool[i]);
3764 }
3765
Chris Masond1310b22008-01-24 16:13:08 -05003766 BUG_ON(!list_empty(pages));
3767 if (bio)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003768 return submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003769 return 0;
3770}
Chris Masond1310b22008-01-24 16:13:08 -05003771
3772/*
3773 * basic invalidatepage code, this waits on any locked or writeback
3774 * ranges corresponding to the page, and then deletes any extent state
3775 * records from the tree
3776 */
3777int extent_invalidatepage(struct extent_io_tree *tree,
3778 struct page *page, unsigned long offset)
3779{
Josef Bacik2ac55d42010-02-03 19:33:23 +00003780 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003781 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05003782 u64 end = start + PAGE_CACHE_SIZE - 1;
3783 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3784
Qu Wenruofda28322013-02-26 08:10:22 +00003785 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05003786 if (start > end)
3787 return 0;
3788
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003789 lock_extent_bits(tree, start, end, 0, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04003790 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003791 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04003792 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3793 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003794 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003795 return 0;
3796}
Chris Masond1310b22008-01-24 16:13:08 -05003797
3798/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04003799 * a helper for releasepage, this tests for areas of the page that
3800 * are locked or under IO and drops the related state bits if it is safe
3801 * to drop the page.
3802 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003803static int try_release_extent_state(struct extent_map_tree *map,
3804 struct extent_io_tree *tree,
3805 struct page *page, gfp_t mask)
Chris Mason7b13b7b2008-04-18 10:29:50 -04003806{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003807 u64 start = page_offset(page);
Chris Mason7b13b7b2008-04-18 10:29:50 -04003808 u64 end = start + PAGE_CACHE_SIZE - 1;
3809 int ret = 1;
3810
Chris Mason211f90e2008-07-18 11:56:15 -04003811 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04003812 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04003813 ret = 0;
3814 else {
3815 if ((mask & GFP_NOFS) == GFP_NOFS)
3816 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04003817 /*
3818 * at this point we can safely clear everything except the
3819 * locked bit and the nodatasum bit
3820 */
Chris Masone3f24cc2011-02-14 12:52:08 -05003821 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04003822 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3823 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05003824
3825 /* if clear_extent_bit failed for enomem reasons,
3826 * we can't allow the release to continue.
3827 */
3828 if (ret < 0)
3829 ret = 0;
3830 else
3831 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003832 }
3833 return ret;
3834}
Chris Mason7b13b7b2008-04-18 10:29:50 -04003835
3836/*
Chris Masond1310b22008-01-24 16:13:08 -05003837 * a helper for releasepage. As long as there are no locked extents
3838 * in the range corresponding to the page, both state records and extent
3839 * map records are removed
3840 */
3841int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05003842 struct extent_io_tree *tree, struct page *page,
3843 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05003844{
3845 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003846 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05003847 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003848
Chris Mason70dec802008-01-29 09:59:12 -05003849 if ((mask & __GFP_WAIT) &&
3850 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05003851 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05003852 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05003853 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04003854 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05003855 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09003856 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04003857 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003858 break;
3859 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003860 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3861 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04003862 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003863 free_extent_map(em);
3864 break;
3865 }
3866 if (!test_range_bit(tree, em->start,
3867 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04003868 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04003869 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05003870 remove_extent_mapping(map, em);
3871 /* once for the rb tree */
3872 free_extent_map(em);
3873 }
3874 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04003875 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003876
3877 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05003878 free_extent_map(em);
3879 }
Chris Masond1310b22008-01-24 16:13:08 -05003880 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04003881 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003882}
Chris Masond1310b22008-01-24 16:13:08 -05003883
Chris Masonec29ed52011-02-23 16:23:20 -05003884/*
3885 * helper function for fiemap, which doesn't want to see any holes.
3886 * This maps until we find something past 'last'
3887 */
3888static struct extent_map *get_extent_skip_holes(struct inode *inode,
3889 u64 offset,
3890 u64 last,
3891 get_extent_t *get_extent)
3892{
3893 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3894 struct extent_map *em;
3895 u64 len;
3896
3897 if (offset >= last)
3898 return NULL;
3899
3900 while(1) {
3901 len = last - offset;
3902 if (len == 0)
3903 break;
Qu Wenruofda28322013-02-26 08:10:22 +00003904 len = ALIGN(len, sectorsize);
Chris Masonec29ed52011-02-23 16:23:20 -05003905 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02003906 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05003907 return em;
3908
3909 /* if this isn't a hole return it */
3910 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3911 em->block_start != EXTENT_MAP_HOLE) {
3912 return em;
3913 }
3914
3915 /* this is a hole, advance to the next extent */
3916 offset = extent_map_end(em);
3917 free_extent_map(em);
3918 if (offset >= last)
3919 break;
3920 }
3921 return NULL;
3922}
3923
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003924int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3925 __u64 start, __u64 len, get_extent_t *get_extent)
3926{
Josef Bacik975f84f2010-11-23 19:36:57 +00003927 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003928 u64 off = start;
3929 u64 max = start + len;
3930 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00003931 u32 found_type;
3932 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05003933 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003934 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003935 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00003936 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003937 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003938 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00003939 struct btrfs_path *path;
3940 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003941 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003942 u64 em_start = 0;
3943 u64 em_len = 0;
3944 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003945 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003946
3947 if (len == 0)
3948 return -EINVAL;
3949
Josef Bacik975f84f2010-11-23 19:36:57 +00003950 path = btrfs_alloc_path();
3951 if (!path)
3952 return -ENOMEM;
3953 path->leave_spinning = 1;
3954
Josef Bacik4d479cf2011-11-17 11:34:31 -05003955 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3956 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3957
Chris Masonec29ed52011-02-23 16:23:20 -05003958 /*
3959 * lookup the last file extent. We're not using i_size here
3960 * because there might be preallocation past i_size
3961 */
Josef Bacik975f84f2010-11-23 19:36:57 +00003962 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
Li Zefan33345d012011-04-20 10:31:50 +08003963 path, btrfs_ino(inode), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00003964 if (ret < 0) {
3965 btrfs_free_path(path);
3966 return ret;
3967 }
3968 WARN_ON(!ret);
3969 path->slots[0]--;
3970 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3971 struct btrfs_file_extent_item);
3972 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3973 found_type = btrfs_key_type(&found_key);
3974
Chris Masonec29ed52011-02-23 16:23:20 -05003975 /* No extents, but there might be delalloc bits */
Li Zefan33345d012011-04-20 10:31:50 +08003976 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00003977 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05003978 /* have to trust i_size as the end */
3979 last = (u64)-1;
3980 last_for_get_extent = isize;
3981 } else {
3982 /*
3983 * remember the start of the last extent. There are a
3984 * bunch of different factors that go into the length of the
3985 * extent, so its much less complex to remember where it started
3986 */
3987 last = found_key.offset;
3988 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00003989 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003990 btrfs_free_path(path);
3991
Chris Masonec29ed52011-02-23 16:23:20 -05003992 /*
3993 * we might have some extents allocated but more delalloc past those
3994 * extents. so, we trust isize unless the start of the last extent is
3995 * beyond isize
3996 */
3997 if (last < isize) {
3998 last = (u64)-1;
3999 last_for_get_extent = isize;
4000 }
4001
Liu Boa52f4cd2013-05-01 16:23:41 +00004002 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004003 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05004004
Josef Bacik4d479cf2011-11-17 11:34:31 -05004005 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05004006 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004007 if (!em)
4008 goto out;
4009 if (IS_ERR(em)) {
4010 ret = PTR_ERR(em);
4011 goto out;
4012 }
Josef Bacik975f84f2010-11-23 19:36:57 +00004013
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004014 while (!end) {
Chris Masonea8efc72011-03-08 11:54:40 -05004015 u64 offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004016
Chris Masonea8efc72011-03-08 11:54:40 -05004017 /* break if the extent we found is outside the range */
4018 if (em->start >= max || extent_map_end(em) < off)
4019 break;
4020
4021 /*
4022 * get_extent may return an extent that starts before our
4023 * requested range. We have to make sure the ranges
4024 * we return to fiemap always move forward and don't
4025 * overlap, so adjust the offsets here
4026 */
4027 em_start = max(em->start, off);
4028
4029 /*
4030 * record the offset from the start of the extent
4031 * for adjusting the disk offset below
4032 */
4033 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05004034 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05004035 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05004036 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004037 disko = 0;
4038 flags = 0;
4039
Chris Masonea8efc72011-03-08 11:54:40 -05004040 /*
4041 * bump off for our next call to get_extent
4042 */
4043 off = extent_map_end(em);
4044 if (off >= max)
4045 end = 1;
4046
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004047 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004048 end = 1;
4049 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004050 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004051 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4052 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004053 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004054 flags |= (FIEMAP_EXTENT_DELALLOC |
4055 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004056 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05004057 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004058 }
4059 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4060 flags |= FIEMAP_EXTENT_ENCODED;
4061
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004062 free_extent_map(em);
4063 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05004064 if ((em_start >= last) || em_len == (u64)-1 ||
4065 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004066 flags |= FIEMAP_EXTENT_LAST;
4067 end = 1;
4068 }
4069
Chris Masonec29ed52011-02-23 16:23:20 -05004070 /* now scan forward to see if this is really the last extent. */
4071 em = get_extent_skip_holes(inode, off, last_for_get_extent,
4072 get_extent);
4073 if (IS_ERR(em)) {
4074 ret = PTR_ERR(em);
4075 goto out;
4076 }
4077 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00004078 flags |= FIEMAP_EXTENT_LAST;
4079 end = 1;
4080 }
Chris Masonec29ed52011-02-23 16:23:20 -05004081 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
4082 em_len, flags);
4083 if (ret)
4084 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004085 }
4086out_free:
4087 free_extent_map(em);
4088out:
Liu Boa52f4cd2013-05-01 16:23:41 +00004089 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004090 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004091 return ret;
4092}
4093
Chris Mason727011e2010-08-06 13:21:20 -04004094static void __free_extent_buffer(struct extent_buffer *eb)
4095{
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004096 btrfs_leak_debug_del(&eb->leak_list);
Chris Mason727011e2010-08-06 13:21:20 -04004097 kmem_cache_free(extent_buffer_cache, eb);
4098}
4099
Chris Masond1310b22008-01-24 16:13:08 -05004100static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
4101 u64 start,
4102 unsigned long len,
4103 gfp_t mask)
4104{
4105 struct extent_buffer *eb = NULL;
4106
Chris Masond1310b22008-01-24 16:13:08 -05004107 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00004108 if (eb == NULL)
4109 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004110 eb->start = start;
4111 eb->len = len;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004112 eb->tree = tree;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004113 eb->bflags = 0;
Chris Masonbd681512011-07-16 15:23:14 -04004114 rwlock_init(&eb->lock);
4115 atomic_set(&eb->write_locks, 0);
4116 atomic_set(&eb->read_locks, 0);
4117 atomic_set(&eb->blocking_readers, 0);
4118 atomic_set(&eb->blocking_writers, 0);
4119 atomic_set(&eb->spinning_readers, 0);
4120 atomic_set(&eb->spinning_writers, 0);
Arne Jansen5b25f702011-09-13 10:55:48 +02004121 eb->lock_nested = 0;
Chris Masonbd681512011-07-16 15:23:14 -04004122 init_waitqueue_head(&eb->write_lock_wq);
4123 init_waitqueue_head(&eb->read_lock_wq);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004124
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004125 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4126
Josef Bacik3083ee22012-03-09 16:01:49 -05004127 spin_lock_init(&eb->refs_lock);
Chris Masond1310b22008-01-24 16:13:08 -05004128 atomic_set(&eb->refs, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004129 atomic_set(&eb->io_pages, 0);
Chris Mason727011e2010-08-06 13:21:20 -04004130
David Sterbab8dae312013-02-28 14:54:18 +00004131 /*
4132 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4133 */
4134 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4135 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4136 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05004137
4138 return eb;
4139}
4140
Jan Schmidt815a51c2012-05-16 17:00:02 +02004141struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4142{
4143 unsigned long i;
4144 struct page *p;
4145 struct extent_buffer *new;
4146 unsigned long num_pages = num_extent_pages(src->start, src->len);
4147
4148 new = __alloc_extent_buffer(NULL, src->start, src->len, GFP_ATOMIC);
4149 if (new == NULL)
4150 return NULL;
4151
4152 for (i = 0; i < num_pages; i++) {
4153 p = alloc_page(GFP_ATOMIC);
4154 BUG_ON(!p);
4155 attach_extent_buffer_page(new, p);
4156 WARN_ON(PageDirty(p));
4157 SetPageUptodate(p);
4158 new->pages[i] = p;
4159 }
4160
4161 copy_extent_buffer(new, src, 0, 0, src->len);
4162 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
4163 set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
4164
4165 return new;
4166}
4167
4168struct extent_buffer *alloc_dummy_extent_buffer(u64 start, unsigned long len)
4169{
4170 struct extent_buffer *eb;
4171 unsigned long num_pages = num_extent_pages(0, len);
4172 unsigned long i;
4173
4174 eb = __alloc_extent_buffer(NULL, start, len, GFP_ATOMIC);
4175 if (!eb)
4176 return NULL;
4177
4178 for (i = 0; i < num_pages; i++) {
4179 eb->pages[i] = alloc_page(GFP_ATOMIC);
4180 if (!eb->pages[i])
4181 goto err;
4182 }
4183 set_extent_buffer_uptodate(eb);
4184 btrfs_set_header_nritems(eb, 0);
4185 set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4186
4187 return eb;
4188err:
Stefan Behrens84167d12012-10-11 07:25:16 -06004189 for (; i > 0; i--)
4190 __free_page(eb->pages[i - 1]);
Jan Schmidt815a51c2012-05-16 17:00:02 +02004191 __free_extent_buffer(eb);
4192 return NULL;
4193}
4194
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004195static int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004196{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004197 return (atomic_read(&eb->io_pages) ||
4198 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4199 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004200}
4201
Miao Xie897ca6e92010-10-26 20:57:29 -04004202/*
4203 * Helper for releasing extent buffer page.
4204 */
4205static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
4206 unsigned long start_idx)
4207{
4208 unsigned long index;
Wang Sheng-Hui39bab872012-04-06 14:35:31 +08004209 unsigned long num_pages;
Miao Xie897ca6e92010-10-26 20:57:29 -04004210 struct page *page;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004211 int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
Miao Xie897ca6e92010-10-26 20:57:29 -04004212
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004213 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e92010-10-26 20:57:29 -04004214
Wang Sheng-Hui39bab872012-04-06 14:35:31 +08004215 num_pages = num_extent_pages(eb->start, eb->len);
4216 index = start_idx + num_pages;
Miao Xie897ca6e92010-10-26 20:57:29 -04004217 if (start_idx >= index)
4218 return;
4219
4220 do {
4221 index--;
4222 page = extent_buffer_page(eb, index);
Jan Schmidt815a51c2012-05-16 17:00:02 +02004223 if (page && mapped) {
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004224 spin_lock(&page->mapping->private_lock);
4225 /*
4226 * We do this since we'll remove the pages after we've
4227 * removed the eb from the radix tree, so we could race
4228 * and have this page now attached to the new eb. So
4229 * only clear page_private if it's still connected to
4230 * this eb.
4231 */
4232 if (PagePrivate(page) &&
4233 page->private == (unsigned long)eb) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004234 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Josef Bacik3083ee22012-03-09 16:01:49 -05004235 BUG_ON(PageDirty(page));
4236 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004237 /*
4238 * We need to make sure we haven't be attached
4239 * to a new eb.
4240 */
4241 ClearPagePrivate(page);
4242 set_page_private(page, 0);
4243 /* One for the page private */
4244 page_cache_release(page);
4245 }
4246 spin_unlock(&page->mapping->private_lock);
4247
Jan Schmidt815a51c2012-05-16 17:00:02 +02004248 }
4249 if (page) {
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004250 /* One for when we alloced the page */
Miao Xie897ca6e92010-10-26 20:57:29 -04004251 page_cache_release(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004252 }
Miao Xie897ca6e92010-10-26 20:57:29 -04004253 } while (index != start_idx);
4254}
4255
4256/*
4257 * Helper for releasing the extent buffer.
4258 */
4259static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4260{
4261 btrfs_release_extent_buffer_page(eb, 0);
4262 __free_extent_buffer(eb);
4263}
4264
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004265static void check_buffer_tree_ref(struct extent_buffer *eb)
4266{
Chris Mason242e18c2013-01-29 17:49:37 -05004267 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004268 /* the ref bit is tricky. We have to make sure it is set
4269 * if we have the buffer dirty. Otherwise the
4270 * code to free a buffer can end up dropping a dirty
4271 * page
4272 *
4273 * Once the ref bit is set, it won't go away while the
4274 * buffer is dirty or in writeback, and it also won't
4275 * go away while we have the reference count on the
4276 * eb bumped.
4277 *
4278 * We can't just set the ref bit without bumping the
4279 * ref on the eb because free_extent_buffer might
4280 * see the ref bit and try to clear it. If this happens
4281 * free_extent_buffer might end up dropping our original
4282 * ref by mistake and freeing the page before we are able
4283 * to add one more ref.
4284 *
4285 * So bump the ref count first, then set the bit. If someone
4286 * beat us to it, drop the ref we added.
4287 */
Chris Mason242e18c2013-01-29 17:49:37 -05004288 refs = atomic_read(&eb->refs);
4289 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4290 return;
4291
Josef Bacik594831c2012-07-20 16:11:08 -04004292 spin_lock(&eb->refs_lock);
4293 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004294 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004295 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004296}
4297
Josef Bacik5df42352012-03-15 18:24:42 -04004298static void mark_extent_buffer_accessed(struct extent_buffer *eb)
4299{
4300 unsigned long num_pages, i;
4301
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004302 check_buffer_tree_ref(eb);
4303
Josef Bacik5df42352012-03-15 18:24:42 -04004304 num_pages = num_extent_pages(eb->start, eb->len);
4305 for (i = 0; i < num_pages; i++) {
4306 struct page *p = extent_buffer_page(eb, i);
4307 mark_page_accessed(p);
4308 }
4309}
4310
Chris Masond1310b22008-01-24 16:13:08 -05004311struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
Chris Mason727011e2010-08-06 13:21:20 -04004312 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004313{
4314 unsigned long num_pages = num_extent_pages(start, len);
4315 unsigned long i;
4316 unsigned long index = start >> PAGE_CACHE_SHIFT;
4317 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004318 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004319 struct page *p;
4320 struct address_space *mapping = tree->mapping;
4321 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004322 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004323
Miao Xie19fe0a82010-10-26 20:57:29 -04004324 rcu_read_lock();
4325 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4326 if (eb && atomic_inc_not_zero(&eb->refs)) {
4327 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004328 mark_extent_buffer_accessed(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04004329 return eb;
4330 }
Miao Xie19fe0a82010-10-26 20:57:29 -04004331 rcu_read_unlock();
Chris Mason6af118ce2008-07-22 11:18:07 -04004332
David Sterbaba144192011-04-21 01:12:06 +02004333 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
Peter2b114d12008-04-01 11:21:40 -04004334 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004335 return NULL;
4336
Chris Mason727011e2010-08-06 13:21:20 -04004337 for (i = 0; i < num_pages; i++, index++) {
Chris Masona6591712011-07-19 12:04:14 -04004338 p = find_or_create_page(mapping, index, GFP_NOFS);
Josef Bacik4804b382012-10-05 16:43:45 -04004339 if (!p)
Chris Mason6af118ce2008-07-22 11:18:07 -04004340 goto free_eb;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004341
4342 spin_lock(&mapping->private_lock);
4343 if (PagePrivate(p)) {
4344 /*
4345 * We could have already allocated an eb for this page
4346 * and attached one so lets see if we can get a ref on
4347 * the existing eb, and if we can we know it's good and
4348 * we can just return that one, else we know we can just
4349 * overwrite page->private.
4350 */
4351 exists = (struct extent_buffer *)p->private;
4352 if (atomic_inc_not_zero(&exists->refs)) {
4353 spin_unlock(&mapping->private_lock);
4354 unlock_page(p);
Josef Bacik17de39a2012-05-04 15:16:06 -04004355 page_cache_release(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004356 mark_extent_buffer_accessed(exists);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004357 goto free_eb;
4358 }
4359
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004360 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004361 * Do this so attach doesn't complain and we need to
4362 * drop the ref the old guy had.
4363 */
4364 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004365 WARN_ON(PageDirty(p));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004366 page_cache_release(p);
Chris Masond1310b22008-01-24 16:13:08 -05004367 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004368 attach_extent_buffer_page(eb, p);
4369 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004370 WARN_ON(PageDirty(p));
Chris Masond1310b22008-01-24 16:13:08 -05004371 mark_page_accessed(p);
Chris Mason727011e2010-08-06 13:21:20 -04004372 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05004373 if (!PageUptodate(p))
4374 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05004375
4376 /*
4377 * see below about how we avoid a nasty race with release page
4378 * and why we unlock later
4379 */
Chris Masond1310b22008-01-24 16:13:08 -05004380 }
4381 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004382 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05004383again:
Miao Xie19fe0a82010-10-26 20:57:29 -04004384 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4385 if (ret)
4386 goto free_eb;
4387
Chris Mason6af118ce2008-07-22 11:18:07 -04004388 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004389 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
4390 if (ret == -EEXIST) {
4391 exists = radix_tree_lookup(&tree->buffer,
4392 start >> PAGE_CACHE_SHIFT);
Josef Bacik115391d2012-03-09 09:51:43 -05004393 if (!atomic_inc_not_zero(&exists->refs)) {
4394 spin_unlock(&tree->buffer_lock);
4395 radix_tree_preload_end();
Josef Bacik115391d2012-03-09 09:51:43 -05004396 exists = NULL;
4397 goto again;
4398 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004399 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004400 radix_tree_preload_end();
Josef Bacik5df42352012-03-15 18:24:42 -04004401 mark_extent_buffer_accessed(exists);
Chris Mason6af118ce2008-07-22 11:18:07 -04004402 goto free_eb;
4403 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004404 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004405 check_buffer_tree_ref(eb);
Yan, Zhengf044ba72010-02-04 08:46:56 +00004406 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004407 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05004408
4409 /*
4410 * there is a race where release page may have
4411 * tried to find this extent buffer in the radix
4412 * but failed. It will tell the VM it is safe to
4413 * reclaim the, and it will clear the page private bit.
4414 * We must make sure to set the page private bit properly
4415 * after the extent buffer is in the radix tree so
4416 * it doesn't get lost
4417 */
Chris Mason727011e2010-08-06 13:21:20 -04004418 SetPageChecked(eb->pages[0]);
4419 for (i = 1; i < num_pages; i++) {
4420 p = extent_buffer_page(eb, i);
Chris Mason727011e2010-08-06 13:21:20 -04004421 ClearPageChecked(p);
4422 unlock_page(p);
4423 }
4424 unlock_page(eb->pages[0]);
Chris Masond1310b22008-01-24 16:13:08 -05004425 return eb;
4426
Chris Mason6af118ce2008-07-22 11:18:07 -04004427free_eb:
Chris Mason727011e2010-08-06 13:21:20 -04004428 for (i = 0; i < num_pages; i++) {
4429 if (eb->pages[i])
4430 unlock_page(eb->pages[i]);
4431 }
Chris Masoneb14ab82011-02-10 12:35:00 -05004432
Josef Bacik17de39a2012-05-04 15:16:06 -04004433 WARN_ON(!atomic_dec_and_test(&eb->refs));
Miao Xie897ca6e92010-10-26 20:57:29 -04004434 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04004435 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05004436}
Chris Masond1310b22008-01-24 16:13:08 -05004437
4438struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
David Sterbaf09d1f62011-04-21 01:08:01 +02004439 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004440{
Chris Masond1310b22008-01-24 16:13:08 -05004441 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05004442
Miao Xie19fe0a82010-10-26 20:57:29 -04004443 rcu_read_lock();
4444 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4445 if (eb && atomic_inc_not_zero(&eb->refs)) {
4446 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004447 mark_extent_buffer_accessed(eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04004448 return eb;
4449 }
4450 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04004451
Miao Xie19fe0a82010-10-26 20:57:29 -04004452 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004453}
Chris Masond1310b22008-01-24 16:13:08 -05004454
Josef Bacik3083ee22012-03-09 16:01:49 -05004455static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4456{
4457 struct extent_buffer *eb =
4458 container_of(head, struct extent_buffer, rcu_head);
4459
4460 __free_extent_buffer(eb);
4461}
4462
Josef Bacik3083ee22012-03-09 16:01:49 -05004463/* Expects to have eb->eb_lock already held */
David Sterbaf7a52a42013-04-26 14:56:29 +00004464static int release_extent_buffer(struct extent_buffer *eb)
Josef Bacik3083ee22012-03-09 16:01:49 -05004465{
4466 WARN_ON(atomic_read(&eb->refs) == 0);
4467 if (atomic_dec_and_test(&eb->refs)) {
Jan Schmidt815a51c2012-05-16 17:00:02 +02004468 if (test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) {
4469 spin_unlock(&eb->refs_lock);
4470 } else {
4471 struct extent_io_tree *tree = eb->tree;
Josef Bacik3083ee22012-03-09 16:01:49 -05004472
Jan Schmidt815a51c2012-05-16 17:00:02 +02004473 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05004474
Jan Schmidt815a51c2012-05-16 17:00:02 +02004475 spin_lock(&tree->buffer_lock);
4476 radix_tree_delete(&tree->buffer,
4477 eb->start >> PAGE_CACHE_SHIFT);
4478 spin_unlock(&tree->buffer_lock);
4479 }
Josef Bacik3083ee22012-03-09 16:01:49 -05004480
4481 /* Should be safe to release our pages at this point */
4482 btrfs_release_extent_buffer_page(eb, 0);
Josef Bacik3083ee22012-03-09 16:01:49 -05004483 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04004484 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05004485 }
4486 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04004487
4488 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05004489}
4490
Chris Masond1310b22008-01-24 16:13:08 -05004491void free_extent_buffer(struct extent_buffer *eb)
4492{
Chris Mason242e18c2013-01-29 17:49:37 -05004493 int refs;
4494 int old;
Chris Masond1310b22008-01-24 16:13:08 -05004495 if (!eb)
4496 return;
4497
Chris Mason242e18c2013-01-29 17:49:37 -05004498 while (1) {
4499 refs = atomic_read(&eb->refs);
4500 if (refs <= 3)
4501 break;
4502 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
4503 if (old == refs)
4504 return;
4505 }
4506
Josef Bacik3083ee22012-03-09 16:01:49 -05004507 spin_lock(&eb->refs_lock);
4508 if (atomic_read(&eb->refs) == 2 &&
Jan Schmidt815a51c2012-05-16 17:00:02 +02004509 test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
4510 atomic_dec(&eb->refs);
4511
4512 if (atomic_read(&eb->refs) == 2 &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004513 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004514 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004515 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4516 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05004517
Josef Bacik3083ee22012-03-09 16:01:49 -05004518 /*
4519 * I know this is terrible, but it's temporary until we stop tracking
4520 * the uptodate bits and such for the extent buffers.
4521 */
David Sterbaf7a52a42013-04-26 14:56:29 +00004522 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05004523}
Chris Masond1310b22008-01-24 16:13:08 -05004524
Josef Bacik3083ee22012-03-09 16:01:49 -05004525void free_extent_buffer_stale(struct extent_buffer *eb)
4526{
4527 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004528 return;
4529
Josef Bacik3083ee22012-03-09 16:01:49 -05004530 spin_lock(&eb->refs_lock);
4531 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
4532
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004533 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004534 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4535 atomic_dec(&eb->refs);
David Sterbaf7a52a42013-04-26 14:56:29 +00004536 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05004537}
4538
Chris Mason1d4284b2012-03-28 20:31:37 -04004539void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004540{
Chris Masond1310b22008-01-24 16:13:08 -05004541 unsigned long i;
4542 unsigned long num_pages;
4543 struct page *page;
4544
Chris Masond1310b22008-01-24 16:13:08 -05004545 num_pages = num_extent_pages(eb->start, eb->len);
4546
4547 for (i = 0; i < num_pages; i++) {
4548 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04004549 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05004550 continue;
4551
Chris Masona61e6f22008-07-22 11:18:08 -04004552 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05004553 WARN_ON(!PagePrivate(page));
4554
Chris Masond1310b22008-01-24 16:13:08 -05004555 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004556 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05004557 if (!PageDirty(page)) {
4558 radix_tree_tag_clear(&page->mapping->page_tree,
4559 page_index(page),
4560 PAGECACHE_TAG_DIRTY);
4561 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004562 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04004563 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04004564 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05004565 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004566 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05004567}
Chris Masond1310b22008-01-24 16:13:08 -05004568
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004569int set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004570{
4571 unsigned long i;
4572 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04004573 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004574
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004575 check_buffer_tree_ref(eb);
4576
Chris Masonb9473432009-03-13 11:00:37 -04004577 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004578
Chris Masond1310b22008-01-24 16:13:08 -05004579 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik3083ee22012-03-09 16:01:49 -05004580 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004581 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
4582
Chris Masonb9473432009-03-13 11:00:37 -04004583 for (i = 0; i < num_pages; i++)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004584 set_page_dirty(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04004585 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05004586}
Chris Masond1310b22008-01-24 16:13:08 -05004587
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004588int clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04004589{
4590 unsigned long i;
4591 struct page *page;
4592 unsigned long num_pages;
4593
Chris Masonb4ce94d2009-02-04 09:25:08 -05004594 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004595 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason1259ab72008-05-12 13:39:03 -04004596 for (i = 0; i < num_pages; i++) {
4597 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04004598 if (page)
4599 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04004600 }
4601 return 0;
4602}
4603
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004604int set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004605{
4606 unsigned long i;
4607 struct page *page;
4608 unsigned long num_pages;
4609
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004610 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004611 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05004612 for (i = 0; i < num_pages; i++) {
4613 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004614 SetPageUptodate(page);
4615 }
4616 return 0;
4617}
Chris Masond1310b22008-01-24 16:13:08 -05004618
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004619int extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004620{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004621 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004622}
Chris Masond1310b22008-01-24 16:13:08 -05004623
4624int read_extent_buffer_pages(struct extent_io_tree *tree,
Arne Jansenbb82ab82011-06-10 14:06:53 +02004625 struct extent_buffer *eb, u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04004626 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05004627{
4628 unsigned long i;
4629 unsigned long start_i;
4630 struct page *page;
4631 int err;
4632 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04004633 int locked_pages = 0;
4634 int all_uptodate = 1;
Chris Masond1310b22008-01-24 16:13:08 -05004635 unsigned long num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04004636 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004637 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004638 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004639
Chris Masonb4ce94d2009-02-04 09:25:08 -05004640 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05004641 return 0;
4642
Chris Masond1310b22008-01-24 16:13:08 -05004643 if (start) {
4644 WARN_ON(start < eb->start);
4645 start_i = (start >> PAGE_CACHE_SHIFT) -
4646 (eb->start >> PAGE_CACHE_SHIFT);
4647 } else {
4648 start_i = 0;
4649 }
4650
4651 num_pages = num_extent_pages(eb->start, eb->len);
4652 for (i = start_i; i < num_pages; i++) {
4653 page = extent_buffer_page(eb, i);
Arne Jansenbb82ab82011-06-10 14:06:53 +02004654 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04004655 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04004656 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05004657 } else {
4658 lock_page(page);
4659 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004660 locked_pages++;
Chris Mason727011e2010-08-06 13:21:20 -04004661 if (!PageUptodate(page)) {
4662 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04004663 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04004664 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004665 }
4666 if (all_uptodate) {
4667 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004668 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04004669 goto unlock_exit;
4670 }
4671
Josef Bacikea466792012-03-26 21:57:36 -04004672 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04004673 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004674 atomic_set(&eb->io_pages, num_reads);
Chris Masonce9adaa2008-04-09 16:28:12 -04004675 for (i = start_i; i < num_pages; i++) {
4676 page = extent_buffer_page(eb, i);
Chris Masonce9adaa2008-04-09 16:28:12 -04004677 if (!PageUptodate(page)) {
Chris Masonf1885912008-04-09 16:28:12 -04004678 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05004679 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04004680 get_extent, &bio,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04004681 mirror_num, &bio_flags,
4682 READ | REQ_META);
Chris Masond3977122009-01-05 21:25:51 -05004683 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05004684 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05004685 } else {
4686 unlock_page(page);
4687 }
4688 }
4689
Jeff Mahoney355808c2011-10-03 23:23:14 -04004690 if (bio) {
Josef Bacikd4c7ca82013-04-19 19:49:09 -04004691 err = submit_one_bio(READ | REQ_META, bio, mirror_num,
4692 bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004693 if (err)
4694 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04004695 }
Chris Masona86c12c2008-02-07 10:50:54 -05004696
Arne Jansenbb82ab82011-06-10 14:06:53 +02004697 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05004698 return ret;
Chris Masond3977122009-01-05 21:25:51 -05004699
Chris Masond1310b22008-01-24 16:13:08 -05004700 for (i = start_i; i < num_pages; i++) {
4701 page = extent_buffer_page(eb, i);
4702 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05004703 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05004704 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05004705 }
Chris Masond3977122009-01-05 21:25:51 -05004706
Chris Masond1310b22008-01-24 16:13:08 -05004707 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04004708
4709unlock_exit:
4710 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05004711 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004712 page = extent_buffer_page(eb, i);
4713 i++;
4714 unlock_page(page);
4715 locked_pages--;
4716 }
4717 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004718}
Chris Masond1310b22008-01-24 16:13:08 -05004719
4720void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4721 unsigned long start,
4722 unsigned long len)
4723{
4724 size_t cur;
4725 size_t offset;
4726 struct page *page;
4727 char *kaddr;
4728 char *dst = (char *)dstv;
4729 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4730 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004731
4732 WARN_ON(start > eb->len);
4733 WARN_ON(start + len > eb->start + eb->len);
4734
4735 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4736
Chris Masond3977122009-01-05 21:25:51 -05004737 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004738 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004739
4740 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04004741 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004742 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004743
4744 dst += cur;
4745 len -= cur;
4746 offset = 0;
4747 i++;
4748 }
4749}
Chris Masond1310b22008-01-24 16:13:08 -05004750
4751int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04004752 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05004753 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04004754 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05004755{
4756 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4757 char *kaddr;
4758 struct page *p;
4759 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4760 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4761 unsigned long end_i = (start_offset + start + min_len - 1) >>
4762 PAGE_CACHE_SHIFT;
4763
4764 if (i != end_i)
4765 return -EINVAL;
4766
4767 if (i == 0) {
4768 offset = start_offset;
4769 *map_start = 0;
4770 } else {
4771 offset = 0;
4772 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4773 }
Chris Masond3977122009-01-05 21:25:51 -05004774
Chris Masond1310b22008-01-24 16:13:08 -05004775 if (start + min_len > eb->len) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004776 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
Chris Masond3977122009-01-05 21:25:51 -05004777 "wanted %lu %lu\n", (unsigned long long)eb->start,
4778 eb->len, start, min_len);
Josef Bacik850265332011-03-15 14:52:12 -04004779 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05004780 }
4781
4782 p = extent_buffer_page(eb, i);
Chris Masona6591712011-07-19 12:04:14 -04004783 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05004784 *map = kaddr + offset;
4785 *map_len = PAGE_CACHE_SIZE - offset;
4786 return 0;
4787}
Chris Masond1310b22008-01-24 16:13:08 -05004788
Chris Masond1310b22008-01-24 16:13:08 -05004789int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4790 unsigned long start,
4791 unsigned long len)
4792{
4793 size_t cur;
4794 size_t offset;
4795 struct page *page;
4796 char *kaddr;
4797 char *ptr = (char *)ptrv;
4798 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4799 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4800 int ret = 0;
4801
4802 WARN_ON(start > eb->len);
4803 WARN_ON(start + len > eb->start + eb->len);
4804
4805 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4806
Chris Masond3977122009-01-05 21:25:51 -05004807 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004808 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004809
4810 cur = min(len, (PAGE_CACHE_SIZE - offset));
4811
Chris Masona6591712011-07-19 12:04:14 -04004812 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004813 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004814 if (ret)
4815 break;
4816
4817 ptr += cur;
4818 len -= cur;
4819 offset = 0;
4820 i++;
4821 }
4822 return ret;
4823}
Chris Masond1310b22008-01-24 16:13:08 -05004824
4825void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4826 unsigned long start, unsigned long len)
4827{
4828 size_t cur;
4829 size_t offset;
4830 struct page *page;
4831 char *kaddr;
4832 char *src = (char *)srcv;
4833 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4834 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4835
4836 WARN_ON(start > eb->len);
4837 WARN_ON(start + len > eb->start + eb->len);
4838
4839 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4840
Chris Masond3977122009-01-05 21:25:51 -05004841 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004842 page = extent_buffer_page(eb, i);
4843 WARN_ON(!PageUptodate(page));
4844
4845 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004846 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004847 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004848
4849 src += cur;
4850 len -= cur;
4851 offset = 0;
4852 i++;
4853 }
4854}
Chris Masond1310b22008-01-24 16:13:08 -05004855
4856void memset_extent_buffer(struct extent_buffer *eb, char c,
4857 unsigned long start, unsigned long len)
4858{
4859 size_t cur;
4860 size_t offset;
4861 struct page *page;
4862 char *kaddr;
4863 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4864 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4865
4866 WARN_ON(start > eb->len);
4867 WARN_ON(start + len > eb->start + eb->len);
4868
4869 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4870
Chris Masond3977122009-01-05 21:25:51 -05004871 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004872 page = extent_buffer_page(eb, i);
4873 WARN_ON(!PageUptodate(page));
4874
4875 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004876 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004877 memset(kaddr + offset, c, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004878
4879 len -= cur;
4880 offset = 0;
4881 i++;
4882 }
4883}
Chris Masond1310b22008-01-24 16:13:08 -05004884
4885void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4886 unsigned long dst_offset, unsigned long src_offset,
4887 unsigned long len)
4888{
4889 u64 dst_len = dst->len;
4890 size_t cur;
4891 size_t offset;
4892 struct page *page;
4893 char *kaddr;
4894 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4895 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4896
4897 WARN_ON(src->len != dst_len);
4898
4899 offset = (start_offset + dst_offset) &
4900 ((unsigned long)PAGE_CACHE_SIZE - 1);
4901
Chris Masond3977122009-01-05 21:25:51 -05004902 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004903 page = extent_buffer_page(dst, i);
4904 WARN_ON(!PageUptodate(page));
4905
4906 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4907
Chris Masona6591712011-07-19 12:04:14 -04004908 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004909 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004910
4911 src_offset += cur;
4912 len -= cur;
4913 offset = 0;
4914 i++;
4915 }
4916}
Chris Masond1310b22008-01-24 16:13:08 -05004917
4918static void move_pages(struct page *dst_page, struct page *src_page,
4919 unsigned long dst_off, unsigned long src_off,
4920 unsigned long len)
4921{
Chris Masona6591712011-07-19 12:04:14 -04004922 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004923 if (dst_page == src_page) {
4924 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4925 } else {
Chris Masona6591712011-07-19 12:04:14 -04004926 char *src_kaddr = page_address(src_page);
Chris Masond1310b22008-01-24 16:13:08 -05004927 char *p = dst_kaddr + dst_off + len;
4928 char *s = src_kaddr + src_off + len;
4929
4930 while (len--)
4931 *--p = *--s;
Chris Masond1310b22008-01-24 16:13:08 -05004932 }
Chris Masond1310b22008-01-24 16:13:08 -05004933}
4934
Sergei Trofimovich33872062011-04-11 21:52:52 +00004935static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4936{
4937 unsigned long distance = (src > dst) ? src - dst : dst - src;
4938 return distance < len;
4939}
4940
Chris Masond1310b22008-01-24 16:13:08 -05004941static void copy_pages(struct page *dst_page, struct page *src_page,
4942 unsigned long dst_off, unsigned long src_off,
4943 unsigned long len)
4944{
Chris Masona6591712011-07-19 12:04:14 -04004945 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004946 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004947 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004948
Sergei Trofimovich33872062011-04-11 21:52:52 +00004949 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04004950 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00004951 } else {
Chris Masond1310b22008-01-24 16:13:08 -05004952 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004953 if (areas_overlap(src_off, dst_off, len))
4954 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00004955 }
Chris Masond1310b22008-01-24 16:13:08 -05004956
Chris Mason727011e2010-08-06 13:21:20 -04004957 if (must_memmove)
4958 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
4959 else
4960 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05004961}
4962
4963void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4964 unsigned long src_offset, unsigned long len)
4965{
4966 size_t cur;
4967 size_t dst_off_in_page;
4968 size_t src_off_in_page;
4969 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4970 unsigned long dst_i;
4971 unsigned long src_i;
4972
4973 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004974 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4975 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004976 BUG_ON(1);
4977 }
4978 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004979 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4980 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004981 BUG_ON(1);
4982 }
4983
Chris Masond3977122009-01-05 21:25:51 -05004984 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004985 dst_off_in_page = (start_offset + dst_offset) &
4986 ((unsigned long)PAGE_CACHE_SIZE - 1);
4987 src_off_in_page = (start_offset + src_offset) &
4988 ((unsigned long)PAGE_CACHE_SIZE - 1);
4989
4990 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4991 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4992
4993 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4994 src_off_in_page));
4995 cur = min_t(unsigned long, cur,
4996 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4997
4998 copy_pages(extent_buffer_page(dst, dst_i),
4999 extent_buffer_page(dst, src_i),
5000 dst_off_in_page, src_off_in_page, cur);
5001
5002 src_offset += cur;
5003 dst_offset += cur;
5004 len -= cur;
5005 }
5006}
Chris Masond1310b22008-01-24 16:13:08 -05005007
5008void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5009 unsigned long src_offset, unsigned long len)
5010{
5011 size_t cur;
5012 size_t dst_off_in_page;
5013 size_t src_off_in_page;
5014 unsigned long dst_end = dst_offset + len - 1;
5015 unsigned long src_end = src_offset + len - 1;
5016 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
5017 unsigned long dst_i;
5018 unsigned long src_i;
5019
5020 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05005021 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
5022 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005023 BUG_ON(1);
5024 }
5025 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05005026 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
5027 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005028 BUG_ON(1);
5029 }
Chris Mason727011e2010-08-06 13:21:20 -04005030 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05005031 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
5032 return;
5033 }
Chris Masond3977122009-01-05 21:25:51 -05005034 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05005035 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
5036 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
5037
5038 dst_off_in_page = (start_offset + dst_end) &
5039 ((unsigned long)PAGE_CACHE_SIZE - 1);
5040 src_off_in_page = (start_offset + src_end) &
5041 ((unsigned long)PAGE_CACHE_SIZE - 1);
5042
5043 cur = min_t(unsigned long, len, src_off_in_page + 1);
5044 cur = min(cur, dst_off_in_page + 1);
5045 move_pages(extent_buffer_page(dst, dst_i),
5046 extent_buffer_page(dst, src_i),
5047 dst_off_in_page - cur + 1,
5048 src_off_in_page - cur + 1, cur);
5049
5050 dst_end -= cur;
5051 src_end -= cur;
5052 len -= cur;
5053 }
5054}
Chris Mason6af118ce2008-07-22 11:18:07 -04005055
David Sterbaf7a52a42013-04-26 14:56:29 +00005056int try_release_extent_buffer(struct page *page)
Miao Xie19fe0a82010-10-26 20:57:29 -04005057{
Chris Mason6af118ce2008-07-22 11:18:07 -04005058 struct extent_buffer *eb;
Miao Xie897ca6e92010-10-26 20:57:29 -04005059
Miao Xie19fe0a82010-10-26 20:57:29 -04005060 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005061 * We need to make sure noboody is attaching this page to an eb right
5062 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04005063 */
Josef Bacik3083ee22012-03-09 16:01:49 -05005064 spin_lock(&page->mapping->private_lock);
5065 if (!PagePrivate(page)) {
5066 spin_unlock(&page->mapping->private_lock);
5067 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005068 }
5069
Josef Bacik3083ee22012-03-09 16:01:49 -05005070 eb = (struct extent_buffer *)page->private;
5071 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04005072
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005073 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005074 * This is a little awful but should be ok, we need to make sure that
5075 * the eb doesn't disappear out from under us while we're looking at
5076 * this page.
5077 */
5078 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005079 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05005080 spin_unlock(&eb->refs_lock);
5081 spin_unlock(&page->mapping->private_lock);
5082 return 0;
5083 }
5084 spin_unlock(&page->mapping->private_lock);
5085
Josef Bacik3083ee22012-03-09 16:01:49 -05005086 /*
5087 * If tree ref isn't set then we know the ref on this eb is a real ref,
5088 * so just return, this page will likely be freed soon anyway.
5089 */
5090 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5091 spin_unlock(&eb->refs_lock);
5092 return 0;
5093 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005094
David Sterbaf7a52a42013-04-26 14:56:29 +00005095 return release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005096}