blob: 5fc9481515f3f836ac1e30446bfd7941c9ca5f53 [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 Woodhouse902b22f2008-08-20 08:51:49 -040016#include "ctree.h"
17#include "btrfs_inode.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020018#include "volumes.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010019#include "check-integrity.h"
Josef Bacik0b32f4b2012-03-13 09:38:00 -040020#include "locking.h"
Josef Bacik606686e2012-06-04 14:03:51 -040021#include "rcu-string.h"
Liu Bofe09e162013-09-22 12:54:23 +080022#include "backref.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",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +020064 state->start, state->end, state->state, state->tree,
65 atomic_read(&state->refs));
Eric Sandeen6d49ba12013-04-22 16:12:31 +000066 list_del(&state->leak_list);
67 kmem_cache_free(extent_state_cache, state);
68 }
69
70 while (!list_empty(&buffers)) {
71 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
72 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +020073 "refs %d\n",
74 eb->start, eb->len, atomic_read(&eb->refs));
Eric Sandeen6d49ba12013-04-22 16:12:31 +000075 list_del(&eb->leak_list);
76 kmem_cache_free(extent_buffer_cache, eb);
77 }
78}
David Sterba8d599ae2013-04-30 15:22:23 +000079
80#define btrfs_debug_check_extent_io_range(inode, start, end) \
81 __btrfs_debug_check_extent_io_range(__func__, (inode), (start), (end))
82static inline void __btrfs_debug_check_extent_io_range(const char *caller,
83 struct inode *inode, u64 start, u64 end)
84{
85 u64 isize = i_size_read(inode);
86
87 if (end >= PAGE_SIZE && (end % 2) == 0 && end != isize - 1) {
88 printk_ratelimited(KERN_DEBUG
89 "btrfs: %s: ino %llu isize %llu odd range [%llu,%llu]\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +020090 caller, btrfs_ino(inode), isize, start, end);
David Sterba8d599ae2013-04-30 15:22:23 +000091 }
92}
Eric Sandeen6d49ba12013-04-22 16:12:31 +000093#else
94#define btrfs_leak_debug_add(new, head) do {} while (0)
95#define btrfs_leak_debug_del(entry) do {} while (0)
96#define btrfs_leak_debug_check() do {} while (0)
David Sterba8d599ae2013-04-30 15:22:23 +000097#define btrfs_debug_check_extent_io_range(c, s, e) do {} while (0)
Chris Mason4bef0842008-09-08 11:18:08 -040098#endif
Chris Masond1310b22008-01-24 16:13:08 -050099
Chris Masond1310b22008-01-24 16:13:08 -0500100#define BUFFER_LRU_MAX 64
101
102struct tree_entry {
103 u64 start;
104 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -0500105 struct rb_node rb_node;
106};
107
108struct extent_page_data {
109 struct bio *bio;
110 struct extent_io_tree *tree;
111 get_extent_t *get_extent;
Josef Bacikde0022b2012-09-25 14:25:58 -0400112 unsigned long bio_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500113
114 /* tells writepage not to lock the state bits for this range
115 * it still does the unlocking
116 */
Chris Masonffbd5172009-04-20 15:50:09 -0400117 unsigned int extent_locked:1;
118
119 /* tells the submit_bio code to use a WRITE_SYNC */
120 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -0500121};
122
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400123static noinline void flush_write_bio(void *data);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400124static inline struct btrfs_fs_info *
125tree_fs_info(struct extent_io_tree *tree)
126{
127 return btrfs_sb(tree->mapping->host->i_sb);
128}
Josef Bacik0b32f4b2012-03-13 09:38:00 -0400129
Chris Masond1310b22008-01-24 16:13:08 -0500130int __init extent_io_init(void)
131{
David Sterba837e1972012-09-07 03:00:48 -0600132 extent_state_cache = kmem_cache_create("btrfs_extent_state",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200133 sizeof(struct extent_state), 0,
134 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500135 if (!extent_state_cache)
136 return -ENOMEM;
137
David Sterba837e1972012-09-07 03:00:48 -0600138 extent_buffer_cache = kmem_cache_create("btrfs_extent_buffer",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +0200139 sizeof(struct extent_buffer), 0,
140 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500141 if (!extent_buffer_cache)
142 goto free_state_cache;
Chris Mason9be33952013-05-17 18:30:14 -0400143
144 btrfs_bioset = bioset_create(BIO_POOL_SIZE,
145 offsetof(struct btrfs_io_bio, bio));
146 if (!btrfs_bioset)
147 goto free_buffer_cache;
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700148
149 if (bioset_integrity_create(btrfs_bioset, BIO_POOL_SIZE))
150 goto free_bioset;
151
Chris Masond1310b22008-01-24 16:13:08 -0500152 return 0;
153
Darrick J. Wongb208c2f2013-09-19 20:37:07 -0700154free_bioset:
155 bioset_free(btrfs_bioset);
156 btrfs_bioset = NULL;
157
Chris Mason9be33952013-05-17 18:30:14 -0400158free_buffer_cache:
159 kmem_cache_destroy(extent_buffer_cache);
160 extent_buffer_cache = NULL;
161
Chris Masond1310b22008-01-24 16:13:08 -0500162free_state_cache:
163 kmem_cache_destroy(extent_state_cache);
Chris Mason9be33952013-05-17 18:30:14 -0400164 extent_state_cache = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500165 return -ENOMEM;
166}
167
168void extent_io_exit(void)
169{
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000170 btrfs_leak_debug_check();
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000171
172 /*
173 * Make sure all delayed rcu free are flushed before we
174 * destroy caches.
175 */
176 rcu_barrier();
Chris Masond1310b22008-01-24 16:13:08 -0500177 if (extent_state_cache)
178 kmem_cache_destroy(extent_state_cache);
179 if (extent_buffer_cache)
180 kmem_cache_destroy(extent_buffer_cache);
Chris Mason9be33952013-05-17 18:30:14 -0400181 if (btrfs_bioset)
182 bioset_free(btrfs_bioset);
Chris Masond1310b22008-01-24 16:13:08 -0500183}
184
185void extent_io_tree_init(struct extent_io_tree *tree,
David Sterbaf993c882011-04-20 23:35:57 +0200186 struct address_space *mapping)
Chris Masond1310b22008-01-24 16:13:08 -0500187{
Eric Paris6bef4d32010-02-23 19:43:04 +0000188 tree->state = RB_ROOT;
Miao Xie19fe0a82010-10-26 20:57:29 -0400189 INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500190 tree->ops = NULL;
191 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500192 spin_lock_init(&tree->lock);
Chris Mason6af118ce2008-07-22 11:18:07 -0400193 spin_lock_init(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -0500194 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500195}
Chris Masond1310b22008-01-24 16:13:08 -0500196
Christoph Hellwigb2950862008-12-02 09:54:17 -0500197static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500198{
199 struct extent_state *state;
Chris Masond1310b22008-01-24 16:13:08 -0500200
201 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400202 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500203 return state;
204 state->state = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500205 state->private = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500206 state->tree = NULL;
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000207 btrfs_leak_debug_add(&state->leak_list, &states);
Chris Masond1310b22008-01-24 16:13:08 -0500208 atomic_set(&state->refs, 1);
209 init_waitqueue_head(&state->wq);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100210 trace_alloc_extent_state(state, mask, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500211 return state;
212}
Chris Masond1310b22008-01-24 16:13:08 -0500213
Chris Mason4845e442010-05-25 20:56:50 -0400214void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500215{
Chris Masond1310b22008-01-24 16:13:08 -0500216 if (!state)
217 return;
218 if (atomic_dec_and_test(&state->refs)) {
Chris Mason70dec802008-01-29 09:59:12 -0500219 WARN_ON(state->tree);
Eric Sandeen6d49ba12013-04-22 16:12:31 +0000220 btrfs_leak_debug_del(&state->leak_list);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100221 trace_free_extent_state(state, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500222 kmem_cache_free(extent_state_cache, state);
223 }
224}
Chris Masond1310b22008-01-24 16:13:08 -0500225
226static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000227 struct rb_node *node,
228 struct rb_node ***p_in,
229 struct rb_node **parent_in)
Chris Masond1310b22008-01-24 16:13:08 -0500230{
Chris Masond3977122009-01-05 21:25:51 -0500231 struct rb_node **p = &root->rb_node;
232 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500233 struct tree_entry *entry;
234
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000235 if (p_in && parent_in) {
236 p = *p_in;
237 parent = *parent_in;
238 goto do_insert;
239 }
240
Chris Masond3977122009-01-05 21:25:51 -0500241 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500242 parent = *p;
243 entry = rb_entry(parent, struct tree_entry, rb_node);
244
245 if (offset < entry->start)
246 p = &(*p)->rb_left;
247 else if (offset > entry->end)
248 p = &(*p)->rb_right;
249 else
250 return parent;
251 }
252
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000253do_insert:
Chris Masond1310b22008-01-24 16:13:08 -0500254 rb_link_node(node, parent, p);
255 rb_insert_color(node, root);
256 return NULL;
257}
258
Chris Mason80ea96b2008-02-01 14:51:59 -0500259static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000260 struct rb_node **prev_ret,
261 struct rb_node **next_ret,
262 struct rb_node ***p_ret,
263 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500264{
Chris Mason80ea96b2008-02-01 14:51:59 -0500265 struct rb_root *root = &tree->state;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000266 struct rb_node **n = &root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500267 struct rb_node *prev = NULL;
268 struct rb_node *orig_prev = NULL;
269 struct tree_entry *entry;
270 struct tree_entry *prev_entry = NULL;
271
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000272 while (*n) {
273 prev = *n;
274 entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500275 prev_entry = entry;
276
277 if (offset < entry->start)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000278 n = &(*n)->rb_left;
Chris Masond1310b22008-01-24 16:13:08 -0500279 else if (offset > entry->end)
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000280 n = &(*n)->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500281 else
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000282 return *n;
Chris Masond1310b22008-01-24 16:13:08 -0500283 }
284
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000285 if (p_ret)
286 *p_ret = n;
287 if (parent_ret)
288 *parent_ret = prev;
289
Chris Masond1310b22008-01-24 16:13:08 -0500290 if (prev_ret) {
291 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500292 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500293 prev = rb_next(prev);
294 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
295 }
296 *prev_ret = prev;
297 prev = orig_prev;
298 }
299
300 if (next_ret) {
301 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500302 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500303 prev = rb_prev(prev);
304 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
305 }
306 *next_ret = prev;
307 }
308 return NULL;
309}
310
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000311static inline struct rb_node *
312tree_search_for_insert(struct extent_io_tree *tree,
313 u64 offset,
314 struct rb_node ***p_ret,
315 struct rb_node **parent_ret)
Chris Masond1310b22008-01-24 16:13:08 -0500316{
Chris Mason70dec802008-01-29 09:59:12 -0500317 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500318 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500319
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000320 ret = __etree_search(tree, offset, &prev, NULL, p_ret, parent_ret);
Chris Masond3977122009-01-05 21:25:51 -0500321 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500322 return prev;
323 return ret;
324}
325
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000326static inline struct rb_node *tree_search(struct extent_io_tree *tree,
327 u64 offset)
328{
329 return tree_search_for_insert(tree, offset, NULL, NULL);
330}
331
Josef Bacik9ed74f22009-09-11 16:12:44 -0400332static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
333 struct extent_state *other)
334{
335 if (tree->ops && tree->ops->merge_extent_hook)
336 tree->ops->merge_extent_hook(tree->mapping->host, new,
337 other);
338}
339
Chris Masond1310b22008-01-24 16:13:08 -0500340/*
341 * utility function to look for merge candidates inside a given range.
342 * Any extents with matching state are merged together into a single
343 * extent in the tree. Extents with EXTENT_IO in their state field
344 * are not merged because the end_io handlers need to be able to do
345 * operations on them without sleeping (or doing allocations/splits).
346 *
347 * This should be called with the tree lock held.
348 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000349static void merge_state(struct extent_io_tree *tree,
350 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500351{
352 struct extent_state *other;
353 struct rb_node *other_node;
354
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400355 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000356 return;
Chris Masond1310b22008-01-24 16:13:08 -0500357
358 other_node = rb_prev(&state->rb_node);
359 if (other_node) {
360 other = rb_entry(other_node, struct extent_state, rb_node);
361 if (other->end == state->start - 1 &&
362 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400363 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500364 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500365 other->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500366 rb_erase(&other->rb_node, &tree->state);
367 free_extent_state(other);
368 }
369 }
370 other_node = rb_next(&state->rb_node);
371 if (other_node) {
372 other = rb_entry(other_node, struct extent_state, rb_node);
373 if (other->start == state->end + 1 &&
374 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400375 merge_cb(tree, state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400376 state->end = other->end;
377 other->tree = NULL;
378 rb_erase(&other->rb_node, &tree->state);
379 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500380 }
381 }
Chris Masond1310b22008-01-24 16:13:08 -0500382}
383
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000384static void set_state_cb(struct extent_io_tree *tree,
David Sterba41074882013-04-29 13:38:46 +0000385 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500386{
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000387 if (tree->ops && tree->ops->set_bit_hook)
388 tree->ops->set_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500389}
390
391static void clear_state_cb(struct extent_io_tree *tree,
David Sterba41074882013-04-29 13:38:46 +0000392 struct extent_state *state, unsigned long *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500393{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400394 if (tree->ops && tree->ops->clear_bit_hook)
395 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500396}
397
Xiao Guangrong3150b692011-07-14 03:19:08 +0000398static void set_state_bits(struct extent_io_tree *tree,
David Sterba41074882013-04-29 13:38:46 +0000399 struct extent_state *state, unsigned long *bits);
Xiao Guangrong3150b692011-07-14 03:19:08 +0000400
Chris Masond1310b22008-01-24 16:13:08 -0500401/*
402 * insert an extent_state struct into the tree. 'bits' are set on the
403 * struct before it is inserted.
404 *
405 * This may return -EEXIST if the extent is already there, in which case the
406 * state struct is freed.
407 *
408 * The tree lock is not taken internally. This is a utility function and
409 * probably isn't what you want to call (see set/clear_extent_bit).
410 */
411static int insert_state(struct extent_io_tree *tree,
412 struct extent_state *state, u64 start, u64 end,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000413 struct rb_node ***p,
414 struct rb_node **parent,
David Sterba41074882013-04-29 13:38:46 +0000415 unsigned long *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500416{
417 struct rb_node *node;
418
Julia Lawall31b1a2b2012-11-03 10:58:34 +0000419 if (end < start)
420 WARN(1, KERN_ERR "btrfs end < start %llu %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200421 end, start);
Chris Masond1310b22008-01-24 16:13:08 -0500422 state->start = start;
423 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400424
Xiao Guangrong3150b692011-07-14 03:19:08 +0000425 set_state_bits(tree, state, bits);
426
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000427 node = tree_insert(&tree->state, end, &state->rb_node, p, parent);
Chris Masond1310b22008-01-24 16:13:08 -0500428 if (node) {
429 struct extent_state *found;
430 found = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500431 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +0200432 "%llu %llu\n",
433 found->start, found->end, start, end);
Chris Masond1310b22008-01-24 16:13:08 -0500434 return -EEXIST;
435 }
Chris Mason70dec802008-01-29 09:59:12 -0500436 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500437 merge_state(tree, state);
438 return 0;
439}
440
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000441static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
Josef Bacik9ed74f22009-09-11 16:12:44 -0400442 u64 split)
443{
444 if (tree->ops && tree->ops->split_extent_hook)
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000445 tree->ops->split_extent_hook(tree->mapping->host, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400446}
447
Chris Masond1310b22008-01-24 16:13:08 -0500448/*
449 * split a given extent state struct in two, inserting the preallocated
450 * struct 'prealloc' as the newly created second half. 'split' indicates an
451 * offset inside 'orig' where it should be split.
452 *
453 * Before calling,
454 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
455 * are two extent state structs in the tree:
456 * prealloc: [orig->start, split - 1]
457 * orig: [ split, orig->end ]
458 *
459 * The tree locks are not taken by this function. They need to be held
460 * by the caller.
461 */
462static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
463 struct extent_state *prealloc, u64 split)
464{
465 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400466
467 split_cb(tree, orig, split);
468
Chris Masond1310b22008-01-24 16:13:08 -0500469 prealloc->start = orig->start;
470 prealloc->end = split - 1;
471 prealloc->state = orig->state;
472 orig->start = split;
473
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000474 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node,
475 NULL, NULL);
Chris Masond1310b22008-01-24 16:13:08 -0500476 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500477 free_extent_state(prealloc);
478 return -EEXIST;
479 }
Chris Mason70dec802008-01-29 09:59:12 -0500480 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500481 return 0;
482}
483
Li Zefancdc6a392012-03-12 16:39:48 +0800484static struct extent_state *next_state(struct extent_state *state)
485{
486 struct rb_node *next = rb_next(&state->rb_node);
487 if (next)
488 return rb_entry(next, struct extent_state, rb_node);
489 else
490 return NULL;
491}
492
Chris Masond1310b22008-01-24 16:13:08 -0500493/*
494 * utility function to clear some bits in an extent state struct.
Wang Sheng-Hui1b303fc2012-04-06 14:35:18 +0800495 * it will optionally wake up any one waiting on this state (wake == 1).
Chris Masond1310b22008-01-24 16:13:08 -0500496 *
497 * If no bits are set on the state struct after clearing things, the
498 * struct is freed and removed from the tree
499 */
Li Zefancdc6a392012-03-12 16:39:48 +0800500static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
501 struct extent_state *state,
David Sterba41074882013-04-29 13:38:46 +0000502 unsigned long *bits, int wake)
Chris Masond1310b22008-01-24 16:13:08 -0500503{
Li Zefancdc6a392012-03-12 16:39:48 +0800504 struct extent_state *next;
David Sterba41074882013-04-29 13:38:46 +0000505 unsigned long bits_to_clear = *bits & ~EXTENT_CTLBITS;
Chris Masond1310b22008-01-24 16:13:08 -0500506
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400507 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500508 u64 range = state->end - state->start + 1;
509 WARN_ON(range > tree->dirty_bytes);
510 tree->dirty_bytes -= range;
511 }
Chris Mason291d6732008-01-29 15:55:23 -0500512 clear_state_cb(tree, state, bits);
Josef Bacik32c00af2009-10-08 13:34:05 -0400513 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500514 if (wake)
515 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400516 if (state->state == 0) {
Li Zefancdc6a392012-03-12 16:39:48 +0800517 next = next_state(state);
Chris Mason70dec802008-01-29 09:59:12 -0500518 if (state->tree) {
Chris Masond1310b22008-01-24 16:13:08 -0500519 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500520 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500521 free_extent_state(state);
522 } else {
523 WARN_ON(1);
524 }
525 } else {
526 merge_state(tree, state);
Li Zefancdc6a392012-03-12 16:39:48 +0800527 next = next_state(state);
Chris Masond1310b22008-01-24 16:13:08 -0500528 }
Li Zefancdc6a392012-03-12 16:39:48 +0800529 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500530}
531
Xiao Guangrong82337672011-04-20 06:44:57 +0000532static struct extent_state *
533alloc_extent_state_atomic(struct extent_state *prealloc)
534{
535 if (!prealloc)
536 prealloc = alloc_extent_state(GFP_ATOMIC);
537
538 return prealloc;
539}
540
Eric Sandeen48a3b632013-04-25 20:41:01 +0000541static void extent_io_tree_panic(struct extent_io_tree *tree, int err)
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400542{
543 btrfs_panic(tree_fs_info(tree), err, "Locking error: "
544 "Extent tree was modified by another "
545 "thread while locked.");
546}
547
Chris Masond1310b22008-01-24 16:13:08 -0500548/*
549 * clear some bits on a range in the tree. This may require splitting
550 * or inserting elements in the tree, so the gfp mask is used to
551 * indicate which allocations or sleeping are allowed.
552 *
553 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
554 * the given range from the tree regardless of state (ie for truncate).
555 *
556 * the range [start, end] is inclusive.
557 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100558 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500559 */
560int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +0000561 unsigned long bits, int wake, int delete,
Chris Mason2c64c532009-09-02 15:04:12 -0400562 struct extent_state **cached_state,
563 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500564{
565 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400566 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500567 struct extent_state *prealloc = NULL;
568 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400569 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500570 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000571 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500572
David Sterba8d599ae2013-04-30 15:22:23 +0000573 btrfs_debug_check_extent_io_range(tree->mapping->host, start, end);
574
Josef Bacik7ee9e442013-06-21 16:37:03 -0400575 if (bits & EXTENT_DELALLOC)
576 bits |= EXTENT_NORESERVE;
577
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400578 if (delete)
579 bits |= ~EXTENT_CTLBITS;
580 bits |= EXTENT_FIRST_DELALLOC;
581
Josef Bacik2ac55d42010-02-03 19:33:23 +0000582 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
583 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500584again:
585 if (!prealloc && (mask & __GFP_WAIT)) {
586 prealloc = alloc_extent_state(mask);
587 if (!prealloc)
588 return -ENOMEM;
589 }
590
Chris Masoncad321a2008-12-17 14:51:42 -0500591 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400592 if (cached_state) {
593 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000594
595 if (clear) {
596 *cached_state = NULL;
597 cached_state = NULL;
598 }
599
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400600 if (cached && cached->tree && cached->start <= start &&
601 cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000602 if (clear)
603 atomic_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400604 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400605 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400606 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000607 if (clear)
608 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400609 }
Chris Masond1310b22008-01-24 16:13:08 -0500610 /*
611 * this search will find the extents that end after
612 * our range starts
613 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500614 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500615 if (!node)
616 goto out;
617 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400618hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500619 if (state->start > end)
620 goto out;
621 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400622 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500623
Liu Bo04493142012-02-16 18:34:37 +0800624 /* the state doesn't have the wanted bits, go ahead */
Li Zefancdc6a392012-03-12 16:39:48 +0800625 if (!(state->state & bits)) {
626 state = next_state(state);
Liu Bo04493142012-02-16 18:34:37 +0800627 goto next;
Li Zefancdc6a392012-03-12 16:39:48 +0800628 }
Liu Bo04493142012-02-16 18:34:37 +0800629
Chris Masond1310b22008-01-24 16:13:08 -0500630 /*
631 * | ---- desired range ---- |
632 * | state | or
633 * | ------------- state -------------- |
634 *
635 * We need to split the extent we found, and may flip
636 * bits on second half.
637 *
638 * If the extent we found extends past our range, we
639 * just split and search again. It'll get split again
640 * the next time though.
641 *
642 * If the extent we found is inside our range, we clear
643 * the desired bit on it.
644 */
645
646 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000647 prealloc = alloc_extent_state_atomic(prealloc);
648 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500649 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400650 if (err)
651 extent_io_tree_panic(tree, err);
652
Chris Masond1310b22008-01-24 16:13:08 -0500653 prealloc = NULL;
654 if (err)
655 goto out;
656 if (state->end <= end) {
Liu Bod1ac6e42012-05-10 18:10:39 +0800657 state = clear_state_bit(tree, state, &bits, wake);
658 goto next;
Chris Masond1310b22008-01-24 16:13:08 -0500659 }
660 goto search_again;
661 }
662 /*
663 * | ---- desired range ---- |
664 * | state |
665 * We need to split the extent, and clear the bit
666 * on the first half
667 */
668 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000669 prealloc = alloc_extent_state_atomic(prealloc);
670 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500671 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400672 if (err)
673 extent_io_tree_panic(tree, err);
674
Chris Masond1310b22008-01-24 16:13:08 -0500675 if (wake)
676 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400677
Jeff Mahoney6763af82012-03-01 14:56:29 +0100678 clear_state_bit(tree, prealloc, &bits, wake);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400679
Chris Masond1310b22008-01-24 16:13:08 -0500680 prealloc = NULL;
681 goto out;
682 }
Chris Mason42daec22009-09-23 19:51:09 -0400683
Li Zefancdc6a392012-03-12 16:39:48 +0800684 state = clear_state_bit(tree, state, &bits, wake);
Liu Bo04493142012-02-16 18:34:37 +0800685next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400686 if (last_end == (u64)-1)
687 goto out;
688 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800689 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800690 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500691 goto search_again;
692
693out:
Chris Masoncad321a2008-12-17 14:51:42 -0500694 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500695 if (prealloc)
696 free_extent_state(prealloc);
697
Jeff Mahoney6763af82012-03-01 14:56:29 +0100698 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500699
700search_again:
701 if (start > end)
702 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500703 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500704 if (mask & __GFP_WAIT)
705 cond_resched();
706 goto again;
707}
Chris Masond1310b22008-01-24 16:13:08 -0500708
Jeff Mahoney143bede2012-03-01 14:56:26 +0100709static void wait_on_state(struct extent_io_tree *tree,
710 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500711 __releases(tree->lock)
712 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500713{
714 DEFINE_WAIT(wait);
715 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500716 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500717 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500718 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500719 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500720}
721
722/*
723 * waits for one or more bits to clear on a range in the state tree.
724 * The range [start, end] is inclusive.
725 * The tree lock is taken by this function
726 */
David Sterba41074882013-04-29 13:38:46 +0000727static void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
728 unsigned long bits)
Chris Masond1310b22008-01-24 16:13:08 -0500729{
730 struct extent_state *state;
731 struct rb_node *node;
732
David Sterba8d599ae2013-04-30 15:22:23 +0000733 btrfs_debug_check_extent_io_range(tree->mapping->host, start, end);
734
Chris Masoncad321a2008-12-17 14:51:42 -0500735 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500736again:
737 while (1) {
738 /*
739 * this search will find all the extents that end after
740 * our range starts
741 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500742 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500743 if (!node)
744 break;
745
746 state = rb_entry(node, struct extent_state, rb_node);
747
748 if (state->start > end)
749 goto out;
750
751 if (state->state & bits) {
752 start = state->start;
753 atomic_inc(&state->refs);
754 wait_on_state(tree, state);
755 free_extent_state(state);
756 goto again;
757 }
758 start = state->end + 1;
759
760 if (start > end)
761 break;
762
Xiao Guangrongded91f02011-07-14 03:19:27 +0000763 cond_resched_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500764 }
765out:
Chris Masoncad321a2008-12-17 14:51:42 -0500766 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500767}
Chris Masond1310b22008-01-24 16:13:08 -0500768
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000769static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500770 struct extent_state *state,
David Sterba41074882013-04-29 13:38:46 +0000771 unsigned long *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500772{
David Sterba41074882013-04-29 13:38:46 +0000773 unsigned long bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400774
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000775 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400776 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500777 u64 range = state->end - state->start + 1;
778 tree->dirty_bytes += range;
779 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400780 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500781}
782
Chris Mason2c64c532009-09-02 15:04:12 -0400783static void cache_state(struct extent_state *state,
784 struct extent_state **cached_ptr)
785{
786 if (cached_ptr && !(*cached_ptr)) {
787 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
788 *cached_ptr = state;
789 atomic_inc(&state->refs);
790 }
791 }
792}
793
Chris Masond1310b22008-01-24 16:13:08 -0500794/*
Chris Mason1edbb732009-09-02 13:24:36 -0400795 * set some bits on a range in the tree. This may require allocations or
796 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500797 *
Chris Mason1edbb732009-09-02 13:24:36 -0400798 * If any of the exclusive bits are set, this will fail with -EEXIST if some
799 * part of the range already has the desired bits set. The start of the
800 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500801 *
Chris Mason1edbb732009-09-02 13:24:36 -0400802 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500803 */
Chris Mason1edbb732009-09-02 13:24:36 -0400804
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100805static int __must_check
806__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +0000807 unsigned long bits, unsigned long exclusive_bits,
808 u64 *failed_start, struct extent_state **cached_state,
809 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500810{
811 struct extent_state *state;
812 struct extent_state *prealloc = NULL;
813 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000814 struct rb_node **p;
815 struct rb_node *parent;
Chris Masond1310b22008-01-24 16:13:08 -0500816 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500817 u64 last_start;
818 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400819
David Sterba8d599ae2013-04-30 15:22:23 +0000820 btrfs_debug_check_extent_io_range(tree->mapping->host, start, end);
821
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400822 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500823again:
824 if (!prealloc && (mask & __GFP_WAIT)) {
825 prealloc = alloc_extent_state(mask);
Xiao Guangrong82337672011-04-20 06:44:57 +0000826 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500827 }
828
Chris Masoncad321a2008-12-17 14:51:42 -0500829 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400830 if (cached_state && *cached_state) {
831 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400832 if (state->start <= start && state->end > start &&
833 state->tree) {
Chris Mason9655d292009-09-02 15:22:30 -0400834 node = &state->rb_node;
835 goto hit_next;
836 }
837 }
Chris Masond1310b22008-01-24 16:13:08 -0500838 /*
839 * this search will find all the extents that end after
840 * our range starts.
841 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000842 node = tree_search_for_insert(tree, start, &p, &parent);
Chris Masond1310b22008-01-24 16:13:08 -0500843 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000844 prealloc = alloc_extent_state_atomic(prealloc);
845 BUG_ON(!prealloc);
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000846 err = insert_state(tree, prealloc, start, end,
847 &p, &parent, &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400848 if (err)
849 extent_io_tree_panic(tree, err);
850
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +0000851 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500852 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500853 goto out;
854 }
Chris Masond1310b22008-01-24 16:13:08 -0500855 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400856hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500857 last_start = state->start;
858 last_end = state->end;
859
860 /*
861 * | ---- desired range ---- |
862 * | state |
863 *
864 * Just lock what we found and keep going
865 */
866 if (state->start == start && state->end <= end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400867 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500868 *failed_start = state->start;
869 err = -EEXIST;
870 goto out;
871 }
Chris Mason42daec22009-09-23 19:51:09 -0400872
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000873 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400874 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500875 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400876 if (last_end == (u64)-1)
877 goto out;
878 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800879 state = next_state(state);
880 if (start < end && state && state->start == start &&
881 !need_resched())
882 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500883 goto search_again;
884 }
885
886 /*
887 * | ---- desired range ---- |
888 * | state |
889 * or
890 * | ------------- state -------------- |
891 *
892 * We need to split the extent we found, and may flip bits on
893 * second half.
894 *
895 * If the extent we found extends past our
896 * range, we just split and search again. It'll get split
897 * again the next time though.
898 *
899 * If the extent we found is inside our range, we set the
900 * desired bit on it.
901 */
902 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400903 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500904 *failed_start = start;
905 err = -EEXIST;
906 goto out;
907 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000908
909 prealloc = alloc_extent_state_atomic(prealloc);
910 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500911 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400912 if (err)
913 extent_io_tree_panic(tree, err);
914
Chris Masond1310b22008-01-24 16:13:08 -0500915 prealloc = NULL;
916 if (err)
917 goto out;
918 if (state->end <= end) {
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000919 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400920 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500921 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400922 if (last_end == (u64)-1)
923 goto out;
924 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800925 state = next_state(state);
926 if (start < end && state && state->start == start &&
927 !need_resched())
928 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500929 }
930 goto search_again;
931 }
932 /*
933 * | ---- desired range ---- |
934 * | state | or | state |
935 *
936 * There's a hole, we need to insert something in it and
937 * ignore the extent we found.
938 */
939 if (state->start > start) {
940 u64 this_end;
941 if (end < last_start)
942 this_end = end;
943 else
Chris Masond3977122009-01-05 21:25:51 -0500944 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000945
946 prealloc = alloc_extent_state_atomic(prealloc);
947 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000948
949 /*
950 * Avoid to free 'prealloc' if it can be merged with
951 * the later extent.
952 */
Chris Masond1310b22008-01-24 16:13:08 -0500953 err = insert_state(tree, prealloc, start, this_end,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +0000954 NULL, NULL, &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400955 if (err)
956 extent_io_tree_panic(tree, err);
957
Chris Mason2c64c532009-09-02 15:04:12 -0400958 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500959 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500960 start = this_end + 1;
961 goto search_again;
962 }
963 /*
964 * | ---- desired range ---- |
965 * | state |
966 * We need to split the extent, and set the bit
967 * on the first half
968 */
969 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400970 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500971 *failed_start = start;
972 err = -EEXIST;
973 goto out;
974 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000975
976 prealloc = alloc_extent_state_atomic(prealloc);
977 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500978 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400979 if (err)
980 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -0500981
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000982 set_state_bits(tree, prealloc, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400983 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500984 merge_state(tree, prealloc);
985 prealloc = NULL;
986 goto out;
987 }
988
989 goto search_again;
990
991out:
Chris Masoncad321a2008-12-17 14:51:42 -0500992 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500993 if (prealloc)
994 free_extent_state(prealloc);
995
996 return err;
997
998search_again:
999 if (start > end)
1000 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -05001001 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001002 if (mask & __GFP_WAIT)
1003 cond_resched();
1004 goto again;
1005}
Chris Masond1310b22008-01-24 16:13:08 -05001006
David Sterba41074882013-04-29 13:38:46 +00001007int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
1008 unsigned long bits, u64 * failed_start,
1009 struct extent_state **cached_state, gfp_t mask)
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001010{
1011 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
1012 cached_state, mask);
1013}
1014
1015
Josef Bacik462d6fa2011-09-26 13:56:12 -04001016/**
Liu Bo10983f22012-07-11 15:26:19 +08001017 * convert_extent_bit - convert all bits in a given range from one bit to
1018 * another
Josef Bacik462d6fa2011-09-26 13:56:12 -04001019 * @tree: the io tree to search
1020 * @start: the start offset in bytes
1021 * @end: the end offset in bytes (inclusive)
1022 * @bits: the bits to set in this range
1023 * @clear_bits: the bits to clear in this range
Josef Bacike6138872012-09-27 17:07:30 -04001024 * @cached_state: state that we're going to cache
Josef Bacik462d6fa2011-09-26 13:56:12 -04001025 * @mask: the allocation mask
1026 *
1027 * This will go through and set bits for the given range. If any states exist
1028 * already in this range they are set with the given bit and cleared of the
1029 * clear_bits. This is only meant to be used by things that are mergeable, ie
1030 * converting from say DELALLOC to DIRTY. This is not meant to be used with
1031 * boundary bits like LOCK.
1032 */
1033int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +00001034 unsigned long bits, unsigned long clear_bits,
Josef Bacike6138872012-09-27 17:07:30 -04001035 struct extent_state **cached_state, gfp_t mask)
Josef Bacik462d6fa2011-09-26 13:56:12 -04001036{
1037 struct extent_state *state;
1038 struct extent_state *prealloc = NULL;
1039 struct rb_node *node;
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001040 struct rb_node **p;
1041 struct rb_node *parent;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001042 int err = 0;
1043 u64 last_start;
1044 u64 last_end;
1045
David Sterba8d599ae2013-04-30 15:22:23 +00001046 btrfs_debug_check_extent_io_range(tree->mapping->host, start, end);
1047
Josef Bacik462d6fa2011-09-26 13:56:12 -04001048again:
1049 if (!prealloc && (mask & __GFP_WAIT)) {
1050 prealloc = alloc_extent_state(mask);
1051 if (!prealloc)
1052 return -ENOMEM;
1053 }
1054
1055 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001056 if (cached_state && *cached_state) {
1057 state = *cached_state;
1058 if (state->start <= start && state->end > start &&
1059 state->tree) {
1060 node = &state->rb_node;
1061 goto hit_next;
1062 }
1063 }
1064
Josef Bacik462d6fa2011-09-26 13:56:12 -04001065 /*
1066 * this search will find all the extents that end after
1067 * our range starts.
1068 */
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001069 node = tree_search_for_insert(tree, start, &p, &parent);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001070 if (!node) {
1071 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001072 if (!prealloc) {
1073 err = -ENOMEM;
1074 goto out;
1075 }
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001076 err = insert_state(tree, prealloc, start, end,
1077 &p, &parent, &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001078 if (err)
1079 extent_io_tree_panic(tree, err);
Filipe David Borba Mananac42ac0b2013-11-26 15:01:34 +00001080 cache_state(prealloc, cached_state);
1081 prealloc = NULL;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001082 goto out;
1083 }
1084 state = rb_entry(node, struct extent_state, rb_node);
1085hit_next:
1086 last_start = state->start;
1087 last_end = state->end;
1088
1089 /*
1090 * | ---- desired range ---- |
1091 * | state |
1092 *
1093 * Just lock what we found and keep going
1094 */
1095 if (state->start == start && state->end <= end) {
Josef Bacik462d6fa2011-09-26 13:56:12 -04001096 set_state_bits(tree, state, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001097 cache_state(state, cached_state);
Liu Bod1ac6e42012-05-10 18:10:39 +08001098 state = clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001099 if (last_end == (u64)-1)
1100 goto out;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001101 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001102 if (start < end && state && state->start == start &&
1103 !need_resched())
1104 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001105 goto search_again;
1106 }
1107
1108 /*
1109 * | ---- desired range ---- |
1110 * | state |
1111 * or
1112 * | ------------- state -------------- |
1113 *
1114 * We need to split the extent we found, and may flip bits on
1115 * second half.
1116 *
1117 * If the extent we found extends past our
1118 * range, we just split and search again. It'll get split
1119 * again the next time though.
1120 *
1121 * If the extent we found is inside our range, we set the
1122 * desired bit on it.
1123 */
1124 if (state->start < start) {
1125 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001126 if (!prealloc) {
1127 err = -ENOMEM;
1128 goto out;
1129 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001130 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001131 if (err)
1132 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001133 prealloc = NULL;
1134 if (err)
1135 goto out;
1136 if (state->end <= end) {
1137 set_state_bits(tree, state, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001138 cache_state(state, cached_state);
Liu Bod1ac6e42012-05-10 18:10:39 +08001139 state = clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001140 if (last_end == (u64)-1)
1141 goto out;
1142 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001143 if (start < end && state && state->start == start &&
1144 !need_resched())
1145 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001146 }
1147 goto search_again;
1148 }
1149 /*
1150 * | ---- desired range ---- |
1151 * | state | or | state |
1152 *
1153 * There's a hole, we need to insert something in it and
1154 * ignore the extent we found.
1155 */
1156 if (state->start > start) {
1157 u64 this_end;
1158 if (end < last_start)
1159 this_end = end;
1160 else
1161 this_end = last_start - 1;
1162
1163 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001164 if (!prealloc) {
1165 err = -ENOMEM;
1166 goto out;
1167 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001168
1169 /*
1170 * Avoid to free 'prealloc' if it can be merged with
1171 * the later extent.
1172 */
1173 err = insert_state(tree, prealloc, start, this_end,
Filipe David Borba Manana12cfbad2013-11-26 15:41:47 +00001174 NULL, NULL, &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001175 if (err)
1176 extent_io_tree_panic(tree, err);
Josef Bacike6138872012-09-27 17:07:30 -04001177 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001178 prealloc = NULL;
1179 start = this_end + 1;
1180 goto search_again;
1181 }
1182 /*
1183 * | ---- desired range ---- |
1184 * | state |
1185 * We need to split the extent, and set the bit
1186 * on the first half
1187 */
1188 if (state->start <= end && state->end > end) {
1189 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001190 if (!prealloc) {
1191 err = -ENOMEM;
1192 goto out;
1193 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001194
1195 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001196 if (err)
1197 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001198
1199 set_state_bits(tree, prealloc, &bits);
Josef Bacike6138872012-09-27 17:07:30 -04001200 cache_state(prealloc, cached_state);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001201 clear_state_bit(tree, prealloc, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001202 prealloc = NULL;
1203 goto out;
1204 }
1205
1206 goto search_again;
1207
1208out:
1209 spin_unlock(&tree->lock);
1210 if (prealloc)
1211 free_extent_state(prealloc);
1212
1213 return err;
1214
1215search_again:
1216 if (start > end)
1217 goto out;
1218 spin_unlock(&tree->lock);
1219 if (mask & __GFP_WAIT)
1220 cond_resched();
1221 goto again;
1222}
1223
Chris Masond1310b22008-01-24 16:13:08 -05001224/* wrappers around set/clear extent bit */
1225int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1226 gfp_t mask)
1227{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001228 return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001229 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001230}
Chris Masond1310b22008-01-24 16:13:08 -05001231
1232int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +00001233 unsigned long bits, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001234{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001235 return set_extent_bit(tree, start, end, bits, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001236 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001237}
Chris Masond1310b22008-01-24 16:13:08 -05001238
1239int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +00001240 unsigned long bits, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001241{
Chris Mason2c64c532009-09-02 15:04:12 -04001242 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001243}
Chris Masond1310b22008-01-24 16:13:08 -05001244
1245int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001246 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001247{
1248 return set_extent_bit(tree, start, end,
Liu Bofee187d2011-09-29 15:55:28 +08001249 EXTENT_DELALLOC | EXTENT_UPTODATE,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001250 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001251}
Chris Masond1310b22008-01-24 16:13:08 -05001252
Liu Bo9e8a4a82012-09-05 19:10:51 -06001253int set_extent_defrag(struct extent_io_tree *tree, u64 start, u64 end,
1254 struct extent_state **cached_state, gfp_t mask)
1255{
1256 return set_extent_bit(tree, start, end,
1257 EXTENT_DELALLOC | EXTENT_UPTODATE | EXTENT_DEFRAG,
1258 NULL, cached_state, mask);
1259}
1260
Chris Masond1310b22008-01-24 16:13:08 -05001261int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1262 gfp_t mask)
1263{
1264 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04001265 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001266 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001267}
Chris Masond1310b22008-01-24 16:13:08 -05001268
1269int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1270 gfp_t mask)
1271{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001272 return set_extent_bit(tree, start, end, EXTENT_NEW, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001273 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001274}
Chris Masond1310b22008-01-24 16:13:08 -05001275
Chris Masond1310b22008-01-24 16:13:08 -05001276int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +00001277 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001278{
Liu Bo6b67a322013-03-28 08:30:28 +00001279 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, NULL,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001280 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001281}
Chris Masond1310b22008-01-24 16:13:08 -05001282
Josef Bacik5fd02042012-05-02 14:00:54 -04001283int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1284 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001285{
Chris Mason2c64c532009-09-02 15:04:12 -04001286 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001287 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001288}
Chris Masond1310b22008-01-24 16:13:08 -05001289
Chris Masond352ac62008-09-29 15:18:18 -04001290/*
1291 * either insert or lock state struct between start and end use mask to tell
1292 * us if waiting is desired.
1293 */
Chris Mason1edbb732009-09-02 13:24:36 -04001294int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +00001295 unsigned long bits, struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001296{
1297 int err;
1298 u64 failed_start;
1299 while (1) {
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001300 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
1301 EXTENT_LOCKED, &failed_start,
1302 cached_state, GFP_NOFS);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001303 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001304 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1305 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001306 } else
Chris Masond1310b22008-01-24 16:13:08 -05001307 break;
Chris Masond1310b22008-01-24 16:13:08 -05001308 WARN_ON(start > end);
1309 }
1310 return err;
1311}
Chris Masond1310b22008-01-24 16:13:08 -05001312
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001313int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Mason1edbb732009-09-02 13:24:36 -04001314{
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001315 return lock_extent_bits(tree, start, end, 0, NULL);
Chris Mason1edbb732009-09-02 13:24:36 -04001316}
1317
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001318int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001319{
1320 int err;
1321 u64 failed_start;
1322
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001323 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1324 &failed_start, NULL, GFP_NOFS);
Yan Zheng66435582008-10-30 14:19:50 -04001325 if (err == -EEXIST) {
1326 if (failed_start > start)
1327 clear_extent_bit(tree, start, failed_start - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001328 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
Josef Bacik25179202008-10-29 14:49:05 -04001329 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001330 }
Josef Bacik25179202008-10-29 14:49:05 -04001331 return 1;
1332}
Josef Bacik25179202008-10-29 14:49:05 -04001333
Chris Mason2c64c532009-09-02 15:04:12 -04001334int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1335 struct extent_state **cached, gfp_t mask)
1336{
1337 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1338 mask);
1339}
1340
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001341int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001342{
Chris Mason2c64c532009-09-02 15:04:12 -04001343 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001344 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001345}
Chris Masond1310b22008-01-24 16:13:08 -05001346
Chris Mason4adaa612013-03-26 13:07:00 -04001347int extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
1348{
1349 unsigned long index = start >> PAGE_CACHE_SHIFT;
1350 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1351 struct page *page;
1352
1353 while (index <= end_index) {
1354 page = find_get_page(inode->i_mapping, index);
1355 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1356 clear_page_dirty_for_io(page);
1357 page_cache_release(page);
1358 index++;
1359 }
1360 return 0;
1361}
1362
1363int extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
1364{
1365 unsigned long index = start >> PAGE_CACHE_SHIFT;
1366 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1367 struct page *page;
1368
1369 while (index <= end_index) {
1370 page = find_get_page(inode->i_mapping, index);
1371 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1372 account_page_redirty(page);
1373 __set_page_dirty_nobuffers(page);
1374 page_cache_release(page);
1375 index++;
1376 }
1377 return 0;
1378}
1379
Chris Masond1310b22008-01-24 16:13:08 -05001380/*
Chris Masond1310b22008-01-24 16:13:08 -05001381 * helper function to set both pages and extents in the tree writeback
1382 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001383static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001384{
1385 unsigned long index = start >> PAGE_CACHE_SHIFT;
1386 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1387 struct page *page;
1388
1389 while (index <= end_index) {
1390 page = find_get_page(tree->mapping, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001391 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Masond1310b22008-01-24 16:13:08 -05001392 set_page_writeback(page);
1393 page_cache_release(page);
1394 index++;
1395 }
Chris Masond1310b22008-01-24 16:13:08 -05001396 return 0;
1397}
Chris Masond1310b22008-01-24 16:13:08 -05001398
Chris Masond352ac62008-09-29 15:18:18 -04001399/* find the first state struct with 'bits' set after 'start', and
1400 * return it. tree->lock must be held. NULL will returned if
1401 * nothing was found after 'start'
1402 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00001403static struct extent_state *
1404find_first_extent_bit_state(struct extent_io_tree *tree,
David Sterba41074882013-04-29 13:38:46 +00001405 u64 start, unsigned long bits)
Chris Masond7fc6402008-02-18 12:12:38 -05001406{
1407 struct rb_node *node;
1408 struct extent_state *state;
1409
1410 /*
1411 * this search will find all the extents that end after
1412 * our range starts.
1413 */
1414 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001415 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001416 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001417
Chris Masond3977122009-01-05 21:25:51 -05001418 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001419 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001420 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001421 return state;
Chris Masond3977122009-01-05 21:25:51 -05001422
Chris Masond7fc6402008-02-18 12:12:38 -05001423 node = rb_next(node);
1424 if (!node)
1425 break;
1426 }
1427out:
1428 return NULL;
1429}
Chris Masond7fc6402008-02-18 12:12:38 -05001430
Chris Masond352ac62008-09-29 15:18:18 -04001431/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001432 * find the first offset in the io tree with 'bits' set. zero is
1433 * returned if we find something, and *start_ret and *end_ret are
1434 * set to reflect the state struct that was found.
1435 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001436 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001437 */
1438int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
David Sterba41074882013-04-29 13:38:46 +00001439 u64 *start_ret, u64 *end_ret, unsigned long bits,
Josef Bacike6138872012-09-27 17:07:30 -04001440 struct extent_state **cached_state)
Xiao Guangrong69261c42011-07-14 03:19:45 +00001441{
1442 struct extent_state *state;
Josef Bacike6138872012-09-27 17:07:30 -04001443 struct rb_node *n;
Xiao Guangrong69261c42011-07-14 03:19:45 +00001444 int ret = 1;
1445
1446 spin_lock(&tree->lock);
Josef Bacike6138872012-09-27 17:07:30 -04001447 if (cached_state && *cached_state) {
1448 state = *cached_state;
1449 if (state->end == start - 1 && state->tree) {
1450 n = rb_next(&state->rb_node);
1451 while (n) {
1452 state = rb_entry(n, struct extent_state,
1453 rb_node);
1454 if (state->state & bits)
1455 goto got_it;
1456 n = rb_next(n);
1457 }
1458 free_extent_state(*cached_state);
1459 *cached_state = NULL;
1460 goto out;
1461 }
1462 free_extent_state(*cached_state);
1463 *cached_state = NULL;
1464 }
1465
Xiao Guangrong69261c42011-07-14 03:19:45 +00001466 state = find_first_extent_bit_state(tree, start, bits);
Josef Bacike6138872012-09-27 17:07:30 -04001467got_it:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001468 if (state) {
Josef Bacike6138872012-09-27 17:07:30 -04001469 cache_state(state, cached_state);
Xiao Guangrong69261c42011-07-14 03:19:45 +00001470 *start_ret = state->start;
1471 *end_ret = state->end;
1472 ret = 0;
1473 }
Josef Bacike6138872012-09-27 17:07:30 -04001474out:
Xiao Guangrong69261c42011-07-14 03:19:45 +00001475 spin_unlock(&tree->lock);
1476 return ret;
1477}
1478
1479/*
Chris Masond352ac62008-09-29 15:18:18 -04001480 * find a contiguous range of bytes in the file marked as delalloc, not
1481 * more than 'max_bytes'. start and end are used to return the range,
1482 *
1483 * 1 is returned if we find something, 0 if nothing was in the tree
1484 */
Chris Masonc8b97812008-10-29 14:49:59 -04001485static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001486 u64 *start, u64 *end, u64 max_bytes,
1487 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001488{
1489 struct rb_node *node;
1490 struct extent_state *state;
1491 u64 cur_start = *start;
1492 u64 found = 0;
1493 u64 total_bytes = 0;
1494
Chris Masoncad321a2008-12-17 14:51:42 -05001495 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001496
Chris Masond1310b22008-01-24 16:13:08 -05001497 /*
1498 * this search will find all the extents that end after
1499 * our range starts.
1500 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001501 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001502 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001503 if (!found)
1504 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001505 goto out;
1506 }
1507
Chris Masond3977122009-01-05 21:25:51 -05001508 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001509 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001510 if (found && (state->start != cur_start ||
1511 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001512 goto out;
1513 }
1514 if (!(state->state & EXTENT_DELALLOC)) {
1515 if (!found)
1516 *end = state->end;
1517 goto out;
1518 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001519 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001520 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001521 *cached_state = state;
1522 atomic_inc(&state->refs);
1523 }
Chris Masond1310b22008-01-24 16:13:08 -05001524 found++;
1525 *end = state->end;
1526 cur_start = state->end + 1;
1527 node = rb_next(node);
Chris Masond1310b22008-01-24 16:13:08 -05001528 total_bytes += state->end - state->start + 1;
Josef Bacik7bf811a52013-10-07 22:11:09 -04001529 if (total_bytes >= max_bytes)
Josef Bacik573aeca2013-08-30 14:38:49 -04001530 break;
Josef Bacik573aeca2013-08-30 14:38:49 -04001531 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001532 break;
1533 }
1534out:
Chris Masoncad321a2008-12-17 14:51:42 -05001535 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001536 return found;
1537}
1538
Jeff Mahoney143bede2012-03-01 14:56:26 +01001539static noinline void __unlock_for_delalloc(struct inode *inode,
1540 struct page *locked_page,
1541 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001542{
1543 int ret;
1544 struct page *pages[16];
1545 unsigned long index = start >> PAGE_CACHE_SHIFT;
1546 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1547 unsigned long nr_pages = end_index - index + 1;
1548 int i;
1549
1550 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001551 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001552
Chris Masond3977122009-01-05 21:25:51 -05001553 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001554 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001555 min_t(unsigned long, nr_pages,
1556 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001557 for (i = 0; i < ret; i++) {
1558 if (pages[i] != locked_page)
1559 unlock_page(pages[i]);
1560 page_cache_release(pages[i]);
1561 }
1562 nr_pages -= ret;
1563 index += ret;
1564 cond_resched();
1565 }
Chris Masonc8b97812008-10-29 14:49:59 -04001566}
1567
1568static noinline int lock_delalloc_pages(struct inode *inode,
1569 struct page *locked_page,
1570 u64 delalloc_start,
1571 u64 delalloc_end)
1572{
1573 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1574 unsigned long start_index = index;
1575 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1576 unsigned long pages_locked = 0;
1577 struct page *pages[16];
1578 unsigned long nrpages;
1579 int ret;
1580 int i;
1581
1582 /* the caller is responsible for locking the start index */
1583 if (index == locked_page->index && index == end_index)
1584 return 0;
1585
1586 /* skip the page at the start index */
1587 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001588 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001589 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001590 min_t(unsigned long,
1591 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001592 if (ret == 0) {
1593 ret = -EAGAIN;
1594 goto done;
1595 }
1596 /* now we have an array of pages, lock them all */
1597 for (i = 0; i < ret; i++) {
1598 /*
1599 * the caller is taking responsibility for
1600 * locked_page
1601 */
Chris Mason771ed682008-11-06 22:02:51 -05001602 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001603 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001604 if (!PageDirty(pages[i]) ||
1605 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001606 ret = -EAGAIN;
1607 unlock_page(pages[i]);
1608 page_cache_release(pages[i]);
1609 goto done;
1610 }
1611 }
Chris Masonc8b97812008-10-29 14:49:59 -04001612 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001613 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001614 }
Chris Masonc8b97812008-10-29 14:49:59 -04001615 nrpages -= ret;
1616 index += ret;
1617 cond_resched();
1618 }
1619 ret = 0;
1620done:
1621 if (ret && pages_locked) {
1622 __unlock_for_delalloc(inode, locked_page,
1623 delalloc_start,
1624 ((u64)(start_index + pages_locked - 1)) <<
1625 PAGE_CACHE_SHIFT);
1626 }
1627 return ret;
1628}
1629
1630/*
1631 * find a contiguous range of bytes in the file marked as delalloc, not
1632 * more than 'max_bytes'. start and end are used to return the range,
1633 *
1634 * 1 is returned if we find something, 0 if nothing was in the tree
1635 */
Josef Bacik294e30f2013-10-09 12:00:56 -04001636STATIC u64 find_lock_delalloc_range(struct inode *inode,
1637 struct extent_io_tree *tree,
1638 struct page *locked_page, u64 *start,
1639 u64 *end, u64 max_bytes)
Chris Masonc8b97812008-10-29 14:49:59 -04001640{
1641 u64 delalloc_start;
1642 u64 delalloc_end;
1643 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001644 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001645 int ret;
1646 int loops = 0;
1647
1648again:
1649 /* step one, find a bunch of delalloc bytes starting at start */
1650 delalloc_start = *start;
1651 delalloc_end = 0;
1652 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001653 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001654 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001655 *start = delalloc_start;
1656 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001657 free_extent_state(cached_state);
Liu Bo385fe0b2013-10-01 23:49:49 +08001658 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001659 }
1660
1661 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001662 * start comes from the offset of locked_page. We have to lock
1663 * pages in order, so we can't process delalloc bytes before
1664 * locked_page
1665 */
Chris Masond3977122009-01-05 21:25:51 -05001666 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001667 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001668
1669 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001670 * make sure to limit the number of pages we try to lock down
Chris Masonc8b97812008-10-29 14:49:59 -04001671 */
Josef Bacik7bf811a52013-10-07 22:11:09 -04001672 if (delalloc_end + 1 - delalloc_start > max_bytes)
1673 delalloc_end = delalloc_start + max_bytes - 1;
Chris Masond3977122009-01-05 21:25:51 -05001674
Chris Masonc8b97812008-10-29 14:49:59 -04001675 /* step two, lock all the pages after the page that has start */
1676 ret = lock_delalloc_pages(inode, locked_page,
1677 delalloc_start, delalloc_end);
1678 if (ret == -EAGAIN) {
1679 /* some of the pages are gone, lets avoid looping by
1680 * shortening the size of the delalloc range we're searching
1681 */
Chris Mason9655d292009-09-02 15:22:30 -04001682 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001683 if (!loops) {
Josef Bacik7bf811a52013-10-07 22:11:09 -04001684 max_bytes = PAGE_CACHE_SIZE;
Chris Masonc8b97812008-10-29 14:49:59 -04001685 loops = 1;
1686 goto again;
1687 } else {
1688 found = 0;
1689 goto out_failed;
1690 }
1691 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001692 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
Chris Masonc8b97812008-10-29 14:49:59 -04001693
1694 /* step three, lock the state bits for the whole range */
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001695 lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001696
1697 /* then test to make sure it is all still delalloc */
1698 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001699 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001700 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001701 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1702 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001703 __unlock_for_delalloc(inode, locked_page,
1704 delalloc_start, delalloc_end);
1705 cond_resched();
1706 goto again;
1707 }
Chris Mason9655d292009-09-02 15:22:30 -04001708 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001709 *start = delalloc_start;
1710 *end = delalloc_end;
1711out_failed:
1712 return found;
1713}
1714
Josef Bacikc2790a22013-07-29 11:20:47 -04001715int extent_clear_unlock_delalloc(struct inode *inode, u64 start, u64 end,
1716 struct page *locked_page,
1717 unsigned long clear_bits,
1718 unsigned long page_ops)
Chris Masonc8b97812008-10-29 14:49:59 -04001719{
Josef Bacikc2790a22013-07-29 11:20:47 -04001720 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
Chris Masonc8b97812008-10-29 14:49:59 -04001721 int ret;
1722 struct page *pages[16];
1723 unsigned long index = start >> PAGE_CACHE_SHIFT;
1724 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1725 unsigned long nr_pages = end_index - index + 1;
1726 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001727
Chris Mason2c64c532009-09-02 15:04:12 -04001728 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacikc2790a22013-07-29 11:20:47 -04001729 if (page_ops == 0)
Chris Mason771ed682008-11-06 22:02:51 -05001730 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001731
Chris Masond3977122009-01-05 21:25:51 -05001732 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001733 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001734 min_t(unsigned long,
1735 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001736 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001737
Josef Bacikc2790a22013-07-29 11:20:47 -04001738 if (page_ops & PAGE_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001739 SetPagePrivate2(pages[i]);
1740
Chris Masonc8b97812008-10-29 14:49:59 -04001741 if (pages[i] == locked_page) {
1742 page_cache_release(pages[i]);
1743 continue;
1744 }
Josef Bacikc2790a22013-07-29 11:20:47 -04001745 if (page_ops & PAGE_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001746 clear_page_dirty_for_io(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001747 if (page_ops & PAGE_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001748 set_page_writeback(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001749 if (page_ops & PAGE_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001750 end_page_writeback(pages[i]);
Josef Bacikc2790a22013-07-29 11:20:47 -04001751 if (page_ops & PAGE_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001752 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001753 page_cache_release(pages[i]);
1754 }
1755 nr_pages -= ret;
1756 index += ret;
1757 cond_resched();
1758 }
1759 return 0;
1760}
Chris Masonc8b97812008-10-29 14:49:59 -04001761
Chris Masond352ac62008-09-29 15:18:18 -04001762/*
1763 * count the number of bytes in the tree that have a given bit(s)
1764 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1765 * cached. The total number found is returned.
1766 */
Chris Masond1310b22008-01-24 16:13:08 -05001767u64 count_range_bits(struct extent_io_tree *tree,
1768 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001769 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001770{
1771 struct rb_node *node;
1772 struct extent_state *state;
1773 u64 cur_start = *start;
1774 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001775 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001776 int found = 0;
1777
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05301778 if (WARN_ON(search_end <= cur_start))
Chris Masond1310b22008-01-24 16:13:08 -05001779 return 0;
Chris Masond1310b22008-01-24 16:13:08 -05001780
Chris Masoncad321a2008-12-17 14:51:42 -05001781 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001782 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1783 total_bytes = tree->dirty_bytes;
1784 goto out;
1785 }
1786 /*
1787 * this search will find all the extents that end after
1788 * our range starts.
1789 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001790 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001791 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001792 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001793
Chris Masond3977122009-01-05 21:25:51 -05001794 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001795 state = rb_entry(node, struct extent_state, rb_node);
1796 if (state->start > search_end)
1797 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001798 if (contig && found && state->start > last + 1)
1799 break;
1800 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001801 total_bytes += min(search_end, state->end) + 1 -
1802 max(cur_start, state->start);
1803 if (total_bytes >= max_bytes)
1804 break;
1805 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001806 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001807 found = 1;
1808 }
Chris Masonec29ed52011-02-23 16:23:20 -05001809 last = state->end;
1810 } else if (contig && found) {
1811 break;
Chris Masond1310b22008-01-24 16:13:08 -05001812 }
1813 node = rb_next(node);
1814 if (!node)
1815 break;
1816 }
1817out:
Chris Masoncad321a2008-12-17 14:51:42 -05001818 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001819 return total_bytes;
1820}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001821
Chris Masond352ac62008-09-29 15:18:18 -04001822/*
1823 * set the private field for a given byte offset in the tree. If there isn't
1824 * an extent_state there already, this does nothing.
1825 */
Sergei Trofimovich171170c2013-08-14 23:27:46 +03001826static int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
Chris Masond1310b22008-01-24 16:13:08 -05001827{
1828 struct rb_node *node;
1829 struct extent_state *state;
1830 int ret = 0;
1831
Chris Masoncad321a2008-12-17 14:51:42 -05001832 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001833 /*
1834 * this search will find all the extents that end after
1835 * our range starts.
1836 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001837 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001838 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001839 ret = -ENOENT;
1840 goto out;
1841 }
1842 state = rb_entry(node, struct extent_state, rb_node);
1843 if (state->start != start) {
1844 ret = -ENOENT;
1845 goto out;
1846 }
1847 state->private = private;
1848out:
Chris Masoncad321a2008-12-17 14:51:42 -05001849 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001850 return ret;
1851}
1852
1853int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1854{
1855 struct rb_node *node;
1856 struct extent_state *state;
1857 int ret = 0;
1858
Chris Masoncad321a2008-12-17 14:51:42 -05001859 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001860 /*
1861 * this search will find all the extents that end after
1862 * our range starts.
1863 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001864 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001865 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001866 ret = -ENOENT;
1867 goto out;
1868 }
1869 state = rb_entry(node, struct extent_state, rb_node);
1870 if (state->start != start) {
1871 ret = -ENOENT;
1872 goto out;
1873 }
1874 *private = state->private;
1875out:
Chris Masoncad321a2008-12-17 14:51:42 -05001876 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001877 return ret;
1878}
1879
1880/*
1881 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001882 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001883 * has the bits set. Otherwise, 1 is returned if any bit in the
1884 * range is found set.
1885 */
1886int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
David Sterba41074882013-04-29 13:38:46 +00001887 unsigned long bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001888{
1889 struct extent_state *state = NULL;
1890 struct rb_node *node;
1891 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001892
Chris Masoncad321a2008-12-17 14:51:42 -05001893 spin_lock(&tree->lock);
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001894 if (cached && cached->tree && cached->start <= start &&
1895 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001896 node = &cached->rb_node;
1897 else
1898 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001899 while (node && start <= end) {
1900 state = rb_entry(node, struct extent_state, rb_node);
1901
1902 if (filled && state->start > start) {
1903 bitset = 0;
1904 break;
1905 }
1906
1907 if (state->start > end)
1908 break;
1909
1910 if (state->state & bits) {
1911 bitset = 1;
1912 if (!filled)
1913 break;
1914 } else if (filled) {
1915 bitset = 0;
1916 break;
1917 }
Chris Mason46562ce2009-09-23 20:23:16 -04001918
1919 if (state->end == (u64)-1)
1920 break;
1921
Chris Masond1310b22008-01-24 16:13:08 -05001922 start = state->end + 1;
1923 if (start > end)
1924 break;
1925 node = rb_next(node);
1926 if (!node) {
1927 if (filled)
1928 bitset = 0;
1929 break;
1930 }
1931 }
Chris Masoncad321a2008-12-17 14:51:42 -05001932 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001933 return bitset;
1934}
Chris Masond1310b22008-01-24 16:13:08 -05001935
1936/*
1937 * helper function to set a given page up to date if all the
1938 * extents in the tree for that page are up to date
1939 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001940static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001941{
Miao Xie4eee4fa2012-12-21 09:17:45 +00001942 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05001943 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001944 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001945 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001946}
1947
1948/*
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001949 * When IO fails, either with EIO or csum verification fails, we
1950 * try other mirrors that might have a good copy of the data. This
1951 * io_failure_record is used to record state as we go through all the
1952 * mirrors. If another mirror has good data, the page is set up to date
1953 * and things continue. If a good mirror can't be found, the original
1954 * bio end_io callback is called to indicate things have failed.
1955 */
1956struct io_failure_record {
1957 struct page *page;
1958 u64 start;
1959 u64 len;
1960 u64 logical;
1961 unsigned long bio_flags;
1962 int this_mirror;
1963 int failed_mirror;
1964 int in_validation;
1965};
1966
1967static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
1968 int did_repair)
1969{
1970 int ret;
1971 int err = 0;
1972 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1973
1974 set_state_private(failure_tree, rec->start, 0);
1975 ret = clear_extent_bits(failure_tree, rec->start,
1976 rec->start + rec->len - 1,
1977 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1978 if (ret)
1979 err = ret;
1980
David Woodhouse53b381b2013-01-29 18:40:14 -05001981 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1982 rec->start + rec->len - 1,
1983 EXTENT_DAMAGED, GFP_NOFS);
1984 if (ret && !err)
1985 err = ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001986
1987 kfree(rec);
1988 return err;
1989}
1990
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001991/*
1992 * this bypasses the standard btrfs submit functions deliberately, as
1993 * the standard behavior is to write all copies in a raid setup. here we only
1994 * want to write the one bad copy. so we do the mapping for ourselves and issue
1995 * submit_bio directly.
Stefan Behrens3ec706c2012-11-05 15:46:42 +01001996 * to avoid any synchronization issues, wait for the data after writing, which
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001997 * actually prevents the read that triggered the error from finishing.
1998 * currently, there can be no more than two copies of every data bit. thus,
1999 * exactly one rewrite is required.
2000 */
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002001int repair_io_failure(struct btrfs_fs_info *fs_info, u64 start,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002002 u64 length, u64 logical, struct page *page,
2003 int mirror_num)
2004{
2005 struct bio *bio;
2006 struct btrfs_device *dev;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002007 u64 map_length = 0;
2008 u64 sector;
2009 struct btrfs_bio *bbio = NULL;
David Woodhouse53b381b2013-01-29 18:40:14 -05002010 struct btrfs_mapping_tree *map_tree = &fs_info->mapping_tree;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002011 int ret;
2012
Ilya Dryomov908960c2013-11-03 19:06:39 +02002013 ASSERT(!(fs_info->sb->s_flags & MS_RDONLY));
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002014 BUG_ON(!mirror_num);
2015
David Woodhouse53b381b2013-01-29 18:40:14 -05002016 /* we can't repair anything in raid56 yet */
2017 if (btrfs_is_parity_mirror(map_tree, logical, length, mirror_num))
2018 return 0;
2019
Chris Mason9be33952013-05-17 18:30:14 -04002020 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002021 if (!bio)
2022 return -EIO;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002023 bio->bi_size = 0;
2024 map_length = length;
2025
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002026 ret = btrfs_map_block(fs_info, WRITE, logical,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002027 &map_length, &bbio, mirror_num);
2028 if (ret) {
2029 bio_put(bio);
2030 return -EIO;
2031 }
2032 BUG_ON(mirror_num != bbio->mirror_num);
2033 sector = bbio->stripes[mirror_num-1].physical >> 9;
2034 bio->bi_sector = sector;
2035 dev = bbio->stripes[mirror_num-1].dev;
2036 kfree(bbio);
2037 if (!dev || !dev->bdev || !dev->writeable) {
2038 bio_put(bio);
2039 return -EIO;
2040 }
2041 bio->bi_bdev = dev->bdev;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002042 bio_add_page(bio, page, length, start - page_offset(page));
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002043
Kent Overstreetc170bbb2013-11-24 16:32:22 -07002044 if (btrfsic_submit_bio_wait(WRITE_SYNC, bio)) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002045 /* try to remap that extent elsewhere? */
2046 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002047 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002048 return -EIO;
2049 }
2050
Anand Jaind5b025d2012-07-02 22:05:21 -06002051 printk_ratelimited_in_rcu(KERN_INFO "btrfs read error corrected: ino %lu off %llu "
Josef Bacik606686e2012-06-04 14:03:51 -04002052 "(dev %s sector %llu)\n", page->mapping->host->i_ino,
2053 start, rcu_str_deref(dev->name), sector);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002054
2055 bio_put(bio);
2056 return 0;
2057}
2058
Josef Bacikea466792012-03-26 21:57:36 -04002059int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
2060 int mirror_num)
2061{
Josef Bacikea466792012-03-26 21:57:36 -04002062 u64 start = eb->start;
2063 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond95603b2012-04-12 15:55:15 -04002064 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04002065
Ilya Dryomov908960c2013-11-03 19:06:39 +02002066 if (root->fs_info->sb->s_flags & MS_RDONLY)
2067 return -EROFS;
2068
Josef Bacikea466792012-03-26 21:57:36 -04002069 for (i = 0; i < num_pages; i++) {
2070 struct page *p = extent_buffer_page(eb, i);
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002071 ret = repair_io_failure(root->fs_info, start, PAGE_CACHE_SIZE,
Josef Bacikea466792012-03-26 21:57:36 -04002072 start, p, mirror_num);
2073 if (ret)
2074 break;
2075 start += PAGE_CACHE_SIZE;
2076 }
2077
2078 return ret;
2079}
2080
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002081/*
2082 * each time an IO finishes, we do a fast check in the IO failure tree
2083 * to see if we need to process or clean up an io_failure_record
2084 */
2085static int clean_io_failure(u64 start, struct page *page)
2086{
2087 u64 private;
2088 u64 private_failure;
2089 struct io_failure_record *failrec;
Ilya Dryomov908960c2013-11-03 19:06:39 +02002090 struct inode *inode = page->mapping->host;
2091 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002092 struct extent_state *state;
2093 int num_copies;
2094 int did_repair = 0;
2095 int ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002096
2097 private = 0;
2098 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
2099 (u64)-1, 1, EXTENT_DIRTY, 0);
2100 if (!ret)
2101 return 0;
2102
2103 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
2104 &private_failure);
2105 if (ret)
2106 return 0;
2107
2108 failrec = (struct io_failure_record *)(unsigned long) private_failure;
2109 BUG_ON(!failrec->this_mirror);
2110
2111 if (failrec->in_validation) {
2112 /* there was no real error, just free the record */
2113 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
2114 failrec->start);
2115 did_repair = 1;
2116 goto out;
2117 }
Ilya Dryomov908960c2013-11-03 19:06:39 +02002118 if (fs_info->sb->s_flags & MS_RDONLY)
2119 goto out;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002120
2121 spin_lock(&BTRFS_I(inode)->io_tree.lock);
2122 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
2123 failrec->start,
2124 EXTENT_LOCKED);
2125 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
2126
Miao Xie883d0de2013-07-25 19:22:35 +08002127 if (state && state->start <= failrec->start &&
2128 state->end >= failrec->start + failrec->len - 1) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002129 num_copies = btrfs_num_copies(fs_info, failrec->logical,
2130 failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002131 if (num_copies > 1) {
Stefan Behrens3ec706c2012-11-05 15:46:42 +01002132 ret = repair_io_failure(fs_info, start, failrec->len,
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002133 failrec->logical, page,
2134 failrec->failed_mirror);
2135 did_repair = !ret;
2136 }
David Woodhouse53b381b2013-01-29 18:40:14 -05002137 ret = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002138 }
2139
2140out:
2141 if (!ret)
2142 ret = free_io_failure(inode, failrec, did_repair);
2143
2144 return ret;
2145}
2146
2147/*
2148 * this is a generic handler for readpage errors (default
2149 * readpage_io_failed_hook). if other copies exist, read those and write back
2150 * good data to the failed position. does not investigate in remapping the
2151 * failed extent elsewhere, hoping the device will be smart enough to do this as
2152 * needed
2153 */
2154
Miao Xiefacc8a222013-07-25 19:22:34 +08002155static int bio_readpage_error(struct bio *failed_bio, u64 phy_offset,
2156 struct page *page, u64 start, u64 end,
2157 int failed_mirror)
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002158{
2159 struct io_failure_record *failrec = NULL;
2160 u64 private;
2161 struct extent_map *em;
2162 struct inode *inode = page->mapping->host;
2163 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2164 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2165 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2166 struct bio *bio;
Miao Xiefacc8a222013-07-25 19:22:34 +08002167 struct btrfs_io_bio *btrfs_failed_bio;
2168 struct btrfs_io_bio *btrfs_bio;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002169 int num_copies;
2170 int ret;
2171 int read_mode;
2172 u64 logical;
2173
2174 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2175
2176 ret = get_state_private(failure_tree, start, &private);
2177 if (ret) {
2178 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2179 if (!failrec)
2180 return -ENOMEM;
2181 failrec->start = start;
2182 failrec->len = end - start + 1;
2183 failrec->this_mirror = 0;
2184 failrec->bio_flags = 0;
2185 failrec->in_validation = 0;
2186
2187 read_lock(&em_tree->lock);
2188 em = lookup_extent_mapping(em_tree, start, failrec->len);
2189 if (!em) {
2190 read_unlock(&em_tree->lock);
2191 kfree(failrec);
2192 return -EIO;
2193 }
2194
Filipe David Borba Manana68ba9902013-11-25 03:22:07 +00002195 if (em->start > start || em->start + em->len <= start) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002196 free_extent_map(em);
2197 em = NULL;
2198 }
2199 read_unlock(&em_tree->lock);
2200
Tsutomu Itoh7a2d6a62012-10-01 03:07:15 -06002201 if (!em) {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002202 kfree(failrec);
2203 return -EIO;
2204 }
2205 logical = start - em->start;
2206 logical = em->block_start + logical;
2207 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2208 logical = em->block_start;
2209 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2210 extent_set_compress_type(&failrec->bio_flags,
2211 em->compress_type);
2212 }
2213 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2214 "len=%llu\n", logical, start, failrec->len);
2215 failrec->logical = logical;
2216 free_extent_map(em);
2217
2218 /* set the bits in the private failure tree */
2219 ret = set_extent_bits(failure_tree, start, end,
2220 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2221 if (ret >= 0)
2222 ret = set_state_private(failure_tree, start,
2223 (u64)(unsigned long)failrec);
2224 /* set the bits in the inode's tree */
2225 if (ret >= 0)
2226 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2227 GFP_NOFS);
2228 if (ret < 0) {
2229 kfree(failrec);
2230 return ret;
2231 }
2232 } else {
2233 failrec = (struct io_failure_record *)(unsigned long)private;
2234 pr_debug("bio_readpage_error: (found) logical=%llu, "
2235 "start=%llu, len=%llu, validation=%d\n",
2236 failrec->logical, failrec->start, failrec->len,
2237 failrec->in_validation);
2238 /*
2239 * when data can be on disk more than twice, add to failrec here
2240 * (e.g. with a list for failed_mirror) to make
2241 * clean_io_failure() clean all those errors at once.
2242 */
2243 }
Stefan Behrens5d964052012-11-05 14:59:07 +01002244 num_copies = btrfs_num_copies(BTRFS_I(inode)->root->fs_info,
2245 failrec->logical, failrec->len);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002246 if (num_copies == 1) {
2247 /*
2248 * we only have a single copy of the data, so don't bother with
2249 * all the retry and error correction code that follows. no
2250 * matter what the error is, it is very likely to persist.
2251 */
Miao Xie09a7f7a2013-07-25 19:22:32 +08002252 pr_debug("bio_readpage_error: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d\n",
2253 num_copies, failrec->this_mirror, failed_mirror);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002254 free_io_failure(inode, failrec, 0);
2255 return -EIO;
2256 }
2257
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002258 /*
2259 * there are two premises:
2260 * a) deliver good data to the caller
2261 * b) correct the bad sectors on disk
2262 */
2263 if (failed_bio->bi_vcnt > 1) {
2264 /*
2265 * to fulfill b), we need to know the exact failing sectors, as
2266 * we don't want to rewrite any more than the failed ones. thus,
2267 * we need separate read requests for the failed bio
2268 *
2269 * if the following BUG_ON triggers, our validation request got
2270 * merged. we need separate requests for our algorithm to work.
2271 */
2272 BUG_ON(failrec->in_validation);
2273 failrec->in_validation = 1;
2274 failrec->this_mirror = failed_mirror;
2275 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2276 } else {
2277 /*
2278 * we're ready to fulfill a) and b) alongside. get a good copy
2279 * of the failed sector and if we succeed, we have setup
2280 * everything for repair_io_failure to do the rest for us.
2281 */
2282 if (failrec->in_validation) {
2283 BUG_ON(failrec->this_mirror != failed_mirror);
2284 failrec->in_validation = 0;
2285 failrec->this_mirror = 0;
2286 }
2287 failrec->failed_mirror = failed_mirror;
2288 failrec->this_mirror++;
2289 if (failrec->this_mirror == failed_mirror)
2290 failrec->this_mirror++;
2291 read_mode = READ_SYNC;
2292 }
2293
Miao Xiefacc8a222013-07-25 19:22:34 +08002294 if (failrec->this_mirror > num_copies) {
2295 pr_debug("bio_readpage_error: (fail) num_copies=%d, next_mirror %d, failed_mirror %d\n",
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002296 num_copies, failrec->this_mirror, failed_mirror);
2297 free_io_failure(inode, failrec, 0);
2298 return -EIO;
2299 }
2300
Chris Mason9be33952013-05-17 18:30:14 -04002301 bio = btrfs_io_bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04002302 if (!bio) {
2303 free_io_failure(inode, failrec, 0);
2304 return -EIO;
2305 }
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002306 bio->bi_end_io = failed_bio->bi_end_io;
2307 bio->bi_sector = failrec->logical >> 9;
2308 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2309 bio->bi_size = 0;
2310
Miao Xiefacc8a222013-07-25 19:22:34 +08002311 btrfs_failed_bio = btrfs_io_bio(failed_bio);
2312 if (btrfs_failed_bio->csum) {
2313 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2314 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2315
2316 btrfs_bio = btrfs_io_bio(bio);
2317 btrfs_bio->csum = btrfs_bio->csum_inline;
2318 phy_offset >>= inode->i_sb->s_blocksize_bits;
2319 phy_offset *= csum_size;
2320 memcpy(btrfs_bio->csum, btrfs_failed_bio->csum + phy_offset,
2321 csum_size);
2322 }
2323
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002324 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2325
2326 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2327 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2328 failrec->this_mirror, num_copies, failrec->in_validation);
2329
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002330 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2331 failrec->this_mirror,
2332 failrec->bio_flags, 0);
2333 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002334}
2335
Chris Masond1310b22008-01-24 16:13:08 -05002336/* lots and lots of room for performance fixes in the end_bio funcs */
2337
Jeff Mahoney87826df2012-02-15 16:23:57 +01002338int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2339{
2340 int uptodate = (err == 0);
2341 struct extent_io_tree *tree;
2342 int ret;
2343
2344 tree = &BTRFS_I(page->mapping->host)->io_tree;
2345
2346 if (tree->ops && tree->ops->writepage_end_io_hook) {
2347 ret = tree->ops->writepage_end_io_hook(page, start,
2348 end, NULL, uptodate);
2349 if (ret)
2350 uptodate = 0;
2351 }
2352
Jeff Mahoney87826df2012-02-15 16:23:57 +01002353 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002354 ClearPageUptodate(page);
2355 SetPageError(page);
2356 }
2357 return 0;
2358}
2359
Chris Masond1310b22008-01-24 16:13:08 -05002360/*
2361 * after a writepage IO is done, we need to:
2362 * clear the uptodate bits on error
2363 * clear the writeback bits in the extent tree for this IO
2364 * end_page_writeback if the page has no more pending IO
2365 *
2366 * Scheduling is not allowed, so the extent state tree is expected
2367 * to have one and only one object corresponding to this IO.
2368 */
Chris Masond1310b22008-01-24 16:13:08 -05002369static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002370{
Chris Masond1310b22008-01-24 16:13:08 -05002371 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002372 u64 start;
2373 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05002374
Chris Masond1310b22008-01-24 16:13:08 -05002375 do {
2376 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04002377
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002378 /* We always issue full-page reads, but if some block
2379 * in a page fails to read, blk_update_request() will
2380 * advance bv_offset and adjust bv_len to compensate.
2381 * Print a warning for nonzero offsets, and an error
2382 * if they don't add up to a full page. */
2383 if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE)
2384 printk("%s page write in btrfs with offset %u and length %u\n",
2385 bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE
2386 ? KERN_ERR "partial" : KERN_INFO "incomplete",
2387 bvec->bv_offset, bvec->bv_len);
Chris Masond1310b22008-01-24 16:13:08 -05002388
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002389 start = page_offset(page);
2390 end = start + bvec->bv_offset + bvec->bv_len - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002391
2392 if (--bvec >= bio->bi_io_vec)
2393 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04002394
Jeff Mahoney87826df2012-02-15 16:23:57 +01002395 if (end_extent_writepage(page, err, start, end))
2396 continue;
Chris Mason70dec802008-01-29 09:59:12 -05002397
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002398 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002399 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04002400
Chris Masond1310b22008-01-24 16:13:08 -05002401 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002402}
2403
Miao Xie883d0de2013-07-25 19:22:35 +08002404static void
2405endio_readpage_release_extent(struct extent_io_tree *tree, u64 start, u64 len,
2406 int uptodate)
2407{
2408 struct extent_state *cached = NULL;
2409 u64 end = start + len - 1;
2410
2411 if (uptodate && tree->track_uptodate)
2412 set_extent_uptodate(tree, start, end, &cached, GFP_ATOMIC);
2413 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
2414}
2415
Chris Masond1310b22008-01-24 16:13:08 -05002416/*
2417 * after a readpage IO is done, we need to:
2418 * clear the uptodate bits on error
2419 * set the uptodate bits if things worked
2420 * set the page up to date if all extents in the tree are uptodate
2421 * clear the lock bit in the extent tree
2422 * unlock the page if there are no other extents locked for it
2423 *
2424 * Scheduling is not allowed, so the extent state tree is expected
2425 * to have one and only one object corresponding to this IO.
2426 */
Chris Masond1310b22008-01-24 16:13:08 -05002427static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002428{
2429 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00002430 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2431 struct bio_vec *bvec = bio->bi_io_vec;
Miao Xiefacc8a222013-07-25 19:22:34 +08002432 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
David Woodhouse902b22f2008-08-20 08:51:49 -04002433 struct extent_io_tree *tree;
Miao Xiefacc8a222013-07-25 19:22:34 +08002434 u64 offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002435 u64 start;
2436 u64 end;
Miao Xiefacc8a222013-07-25 19:22:34 +08002437 u64 len;
Miao Xie883d0de2013-07-25 19:22:35 +08002438 u64 extent_start = 0;
2439 u64 extent_len = 0;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002440 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002441 int ret;
2442
Chris Masond20f7042008-12-08 16:58:54 -05002443 if (err)
2444 uptodate = 0;
2445
Chris Masond1310b22008-01-24 16:13:08 -05002446 do {
2447 struct page *page = bvec->bv_page;
Josef Bacika71754f2013-06-17 17:14:39 -04002448 struct inode *inode = page->mapping->host;
Arne Jansen507903b2011-04-06 10:02:20 +00002449
Kent Overstreetbe3940c2012-09-11 14:23:05 -06002450 pr_debug("end_bio_extent_readpage: bi_sector=%llu, err=%d, "
Chris Mason9be33952013-05-17 18:30:14 -04002451 "mirror=%lu\n", (u64)bio->bi_sector, err,
2452 io_bio->mirror_num);
Josef Bacika71754f2013-06-17 17:14:39 -04002453 tree = &BTRFS_I(inode)->io_tree;
David Woodhouse902b22f2008-08-20 08:51:49 -04002454
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002455 /* We always issue full-page reads, but if some block
2456 * in a page fails to read, blk_update_request() will
2457 * advance bv_offset and adjust bv_len to compensate.
2458 * Print a warning for nonzero offsets, and an error
2459 * if they don't add up to a full page. */
2460 if (bvec->bv_offset || bvec->bv_len != PAGE_CACHE_SIZE)
2461 printk("%s page read in btrfs with offset %u and length %u\n",
2462 bvec->bv_offset + bvec->bv_len != PAGE_CACHE_SIZE
2463 ? KERN_ERR "partial" : KERN_INFO "incomplete",
2464 bvec->bv_offset, bvec->bv_len);
Chris Masond1310b22008-01-24 16:13:08 -05002465
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002466 start = page_offset(page);
2467 end = start + bvec->bv_offset + bvec->bv_len - 1;
Miao Xiefacc8a222013-07-25 19:22:34 +08002468 len = bvec->bv_len;
Chris Masond1310b22008-01-24 16:13:08 -05002469
Chris Mason4125bf72010-02-03 18:18:45 +00002470 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05002471 prefetchw(&bvec->bv_page->flags);
2472
Chris Mason9be33952013-05-17 18:30:14 -04002473 mirror = io_bio->mirror_num;
Miao Xief2a09da2013-07-25 19:22:33 +08002474 if (likely(uptodate && tree->ops &&
2475 tree->ops->readpage_end_io_hook)) {
Miao Xiefacc8a222013-07-25 19:22:34 +08002476 ret = tree->ops->readpage_end_io_hook(io_bio, offset,
2477 page, start, end,
2478 mirror);
Stefan Behrens5ee08442012-08-27 08:30:03 -06002479 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002480 uptodate = 0;
Stefan Behrens5ee08442012-08-27 08:30:03 -06002481 else
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002482 clean_io_failure(start, page);
Chris Masond1310b22008-01-24 16:13:08 -05002483 }
Josef Bacikea466792012-03-26 21:57:36 -04002484
Miao Xief2a09da2013-07-25 19:22:33 +08002485 if (likely(uptodate))
2486 goto readpage_ok;
2487
2488 if (tree->ops && tree->ops->readpage_io_failed_hook) {
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002489 ret = tree->ops->readpage_io_failed_hook(page, mirror);
Josef Bacikea466792012-03-26 21:57:36 -04002490 if (!ret && !err &&
2491 test_bit(BIO_UPTODATE, &bio->bi_flags))
2492 uptodate = 1;
Miao Xief2a09da2013-07-25 19:22:33 +08002493 } else {
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002494 /*
2495 * The generic bio_readpage_error handles errors the
2496 * following way: If possible, new read requests are
2497 * created and submitted and will end up in
2498 * end_bio_extent_readpage as well (if we're lucky, not
2499 * in the !uptodate case). In that case it returns 0 and
2500 * we just go on with the next page in our bio. If it
2501 * can't handle the error it will return -EIO and we
2502 * remain responsible for that page.
2503 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002504 ret = bio_readpage_error(bio, offset, page, start, end,
2505 mirror);
Chris Mason7e383262008-04-09 16:28:12 -04002506 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04002507 uptodate =
2508 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05002509 if (err)
2510 uptodate = 0;
Chris Mason7e383262008-04-09 16:28:12 -04002511 continue;
2512 }
2513 }
Miao Xief2a09da2013-07-25 19:22:33 +08002514readpage_ok:
Miao Xie883d0de2013-07-25 19:22:35 +08002515 if (likely(uptodate)) {
Josef Bacika71754f2013-06-17 17:14:39 -04002516 loff_t i_size = i_size_read(inode);
2517 pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
2518 unsigned offset;
2519
2520 /* Zero out the end if this page straddles i_size */
2521 offset = i_size & (PAGE_CACHE_SIZE-1);
2522 if (page->index == end_index && offset)
2523 zero_user_segment(page, offset, PAGE_CACHE_SIZE);
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002524 SetPageUptodate(page);
Chris Mason70dec802008-01-29 09:59:12 -05002525 } else {
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002526 ClearPageUptodate(page);
2527 SetPageError(page);
Chris Mason70dec802008-01-29 09:59:12 -05002528 }
Alexandre Oliva17a5adc2013-05-15 11:38:55 -04002529 unlock_page(page);
Miao Xiefacc8a222013-07-25 19:22:34 +08002530 offset += len;
Miao Xie883d0de2013-07-25 19:22:35 +08002531
2532 if (unlikely(!uptodate)) {
2533 if (extent_len) {
2534 endio_readpage_release_extent(tree,
2535 extent_start,
2536 extent_len, 1);
2537 extent_start = 0;
2538 extent_len = 0;
2539 }
2540 endio_readpage_release_extent(tree, start,
2541 end - start + 1, 0);
2542 } else if (!extent_len) {
2543 extent_start = start;
2544 extent_len = end + 1 - start;
2545 } else if (extent_start + extent_len == start) {
2546 extent_len += end + 1 - start;
2547 } else {
2548 endio_readpage_release_extent(tree, extent_start,
2549 extent_len, uptodate);
2550 extent_start = start;
2551 extent_len = end + 1 - start;
2552 }
Chris Mason4125bf72010-02-03 18:18:45 +00002553 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05002554
Miao Xie883d0de2013-07-25 19:22:35 +08002555 if (extent_len)
2556 endio_readpage_release_extent(tree, extent_start, extent_len,
2557 uptodate);
Miao Xiefacc8a222013-07-25 19:22:34 +08002558 if (io_bio->end_io)
2559 io_bio->end_io(io_bio, err);
Chris Masond1310b22008-01-24 16:13:08 -05002560 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002561}
2562
Chris Mason9be33952013-05-17 18:30:14 -04002563/*
2564 * this allocates from the btrfs_bioset. We're returning a bio right now
2565 * but you can call btrfs_io_bio for the appropriate container_of magic
2566 */
Miao Xie88f794e2010-11-22 03:02:55 +00002567struct bio *
2568btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2569 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002570{
Miao Xiefacc8a222013-07-25 19:22:34 +08002571 struct btrfs_io_bio *btrfs_bio;
Chris Masond1310b22008-01-24 16:13:08 -05002572 struct bio *bio;
2573
Chris Mason9be33952013-05-17 18:30:14 -04002574 bio = bio_alloc_bioset(gfp_flags, nr_vecs, btrfs_bioset);
Chris Masond1310b22008-01-24 16:13:08 -05002575
2576 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
Chris Mason9be33952013-05-17 18:30:14 -04002577 while (!bio && (nr_vecs /= 2)) {
2578 bio = bio_alloc_bioset(gfp_flags,
2579 nr_vecs, btrfs_bioset);
2580 }
Chris Masond1310b22008-01-24 16:13:08 -05002581 }
2582
2583 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04002584 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002585 bio->bi_bdev = bdev;
2586 bio->bi_sector = first_sector;
Miao Xiefacc8a222013-07-25 19:22:34 +08002587 btrfs_bio = btrfs_io_bio(bio);
2588 btrfs_bio->csum = NULL;
2589 btrfs_bio->csum_allocated = NULL;
2590 btrfs_bio->end_io = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002591 }
2592 return bio;
2593}
2594
Chris Mason9be33952013-05-17 18:30:14 -04002595struct bio *btrfs_bio_clone(struct bio *bio, gfp_t gfp_mask)
2596{
2597 return bio_clone_bioset(bio, gfp_mask, btrfs_bioset);
2598}
2599
2600
2601/* this also allocates from the btrfs_bioset */
2602struct bio *btrfs_io_bio_alloc(gfp_t gfp_mask, unsigned int nr_iovecs)
2603{
Miao Xiefacc8a222013-07-25 19:22:34 +08002604 struct btrfs_io_bio *btrfs_bio;
2605 struct bio *bio;
2606
2607 bio = bio_alloc_bioset(gfp_mask, nr_iovecs, btrfs_bioset);
2608 if (bio) {
2609 btrfs_bio = btrfs_io_bio(bio);
2610 btrfs_bio->csum = NULL;
2611 btrfs_bio->csum_allocated = NULL;
2612 btrfs_bio->end_io = NULL;
2613 }
2614 return bio;
Chris Mason9be33952013-05-17 18:30:14 -04002615}
2616
2617
Jeff Mahoney355808c2011-10-03 23:23:14 -04002618static int __must_check submit_one_bio(int rw, struct bio *bio,
2619 int mirror_num, unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002620{
Chris Masond1310b22008-01-24 16:13:08 -05002621 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002622 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2623 struct page *page = bvec->bv_page;
2624 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002625 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002626
Miao Xie4eee4fa2012-12-21 09:17:45 +00002627 start = page_offset(page) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002628
David Woodhouse902b22f2008-08-20 08:51:49 -04002629 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002630
2631 bio_get(bio);
2632
Chris Mason065631f2008-02-20 12:07:25 -05002633 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00002634 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002635 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002636 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01002637 btrfsic_submit_bio(rw, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002638
Chris Masond1310b22008-01-24 16:13:08 -05002639 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2640 ret = -EOPNOTSUPP;
2641 bio_put(bio);
2642 return ret;
2643}
2644
David Woodhouse64a16702009-07-15 23:29:37 +01002645static int merge_bio(int rw, struct extent_io_tree *tree, struct page *page,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002646 unsigned long offset, size_t size, struct bio *bio,
2647 unsigned long bio_flags)
2648{
2649 int ret = 0;
2650 if (tree->ops && tree->ops->merge_bio_hook)
David Woodhouse64a16702009-07-15 23:29:37 +01002651 ret = tree->ops->merge_bio_hook(rw, page, offset, size, bio,
Jeff Mahoney3444a972011-10-03 23:23:13 -04002652 bio_flags);
2653 BUG_ON(ret < 0);
2654 return ret;
2655
2656}
2657
Chris Masond1310b22008-01-24 16:13:08 -05002658static int submit_extent_page(int rw, struct extent_io_tree *tree,
2659 struct page *page, sector_t sector,
2660 size_t size, unsigned long offset,
2661 struct block_device *bdev,
2662 struct bio **bio_ret,
2663 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04002664 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002665 int mirror_num,
2666 unsigned long prev_bio_flags,
2667 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002668{
2669 int ret = 0;
2670 struct bio *bio;
2671 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04002672 int contig = 0;
2673 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2674 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05002675 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002676
2677 if (bio_ret && *bio_ret) {
2678 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002679 if (old_compressed)
2680 contig = bio->bi_sector == sector;
2681 else
Kent Overstreetf73a1c72012-09-25 15:05:12 -07002682 contig = bio_end_sector(bio) == sector;
Chris Masonc8b97812008-10-29 14:49:59 -04002683
2684 if (prev_bio_flags != bio_flags || !contig ||
David Woodhouse64a16702009-07-15 23:29:37 +01002685 merge_bio(rw, tree, page, offset, page_size, bio, bio_flags) ||
Chris Masonc8b97812008-10-29 14:49:59 -04002686 bio_add_page(bio, page, page_size, offset) < page_size) {
2687 ret = submit_one_bio(rw, bio, mirror_num,
2688 prev_bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002689 if (ret < 0)
2690 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05002691 bio = NULL;
2692 } else {
2693 return 0;
2694 }
2695 }
Chris Masonc8b97812008-10-29 14:49:59 -04002696 if (this_compressed)
2697 nr = BIO_MAX_PAGES;
2698 else
2699 nr = bio_get_nr_vecs(bdev);
2700
Miao Xie88f794e2010-11-22 03:02:55 +00002701 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002702 if (!bio)
2703 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002704
Chris Masonc8b97812008-10-29 14:49:59 -04002705 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002706 bio->bi_end_io = end_io_func;
2707 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05002708
Chris Masond3977122009-01-05 21:25:51 -05002709 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002710 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002711 else
Chris Masonc8b97812008-10-29 14:49:59 -04002712 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002713
2714 return ret;
2715}
2716
Eric Sandeen48a3b632013-04-25 20:41:01 +00002717static void attach_extent_buffer_page(struct extent_buffer *eb,
2718 struct page *page)
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002719{
2720 if (!PagePrivate(page)) {
2721 SetPagePrivate(page);
2722 page_cache_get(page);
2723 set_page_private(page, (unsigned long)eb);
2724 } else {
2725 WARN_ON(page->private != (unsigned long)eb);
2726 }
2727}
2728
Chris Masond1310b22008-01-24 16:13:08 -05002729void set_page_extent_mapped(struct page *page)
2730{
2731 if (!PagePrivate(page)) {
2732 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002733 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002734 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002735 }
2736}
2737
Miao Xie125bac012013-07-25 19:22:37 +08002738static struct extent_map *
2739__get_extent_map(struct inode *inode, struct page *page, size_t pg_offset,
2740 u64 start, u64 len, get_extent_t *get_extent,
2741 struct extent_map **em_cached)
2742{
2743 struct extent_map *em;
2744
2745 if (em_cached && *em_cached) {
2746 em = *em_cached;
2747 if (em->in_tree && start >= em->start &&
2748 start < extent_map_end(em)) {
2749 atomic_inc(&em->refs);
2750 return em;
2751 }
2752
2753 free_extent_map(em);
2754 *em_cached = NULL;
2755 }
2756
2757 em = get_extent(inode, page, pg_offset, start, len, 0);
2758 if (em_cached && !IS_ERR_OR_NULL(em)) {
2759 BUG_ON(*em_cached);
2760 atomic_inc(&em->refs);
2761 *em_cached = em;
2762 }
2763 return em;
2764}
Chris Masond1310b22008-01-24 16:13:08 -05002765/*
2766 * basic readpage implementation. Locked extent state structs are inserted
2767 * into the tree that are removed when the IO is done (by the end_io
2768 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002769 * XXX JDM: This needs looking at to ensure proper page locking
Chris Masond1310b22008-01-24 16:13:08 -05002770 */
Miao Xie99740902013-07-25 19:22:36 +08002771static int __do_readpage(struct extent_io_tree *tree,
2772 struct page *page,
2773 get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08002774 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08002775 struct bio **bio, int mirror_num,
2776 unsigned long *bio_flags, int rw)
Chris Masond1310b22008-01-24 16:13:08 -05002777{
2778 struct inode *inode = page->mapping->host;
Miao Xie4eee4fa2012-12-21 09:17:45 +00002779 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05002780 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2781 u64 end;
2782 u64 cur = start;
2783 u64 extent_offset;
2784 u64 last_byte = i_size_read(inode);
2785 u64 block_start;
2786 u64 cur_end;
2787 sector_t sector;
2788 struct extent_map *em;
2789 struct block_device *bdev;
2790 int ret;
2791 int nr = 0;
Mark Fasheh4b384312013-08-06 11:42:50 -07002792 int parent_locked = *bio_flags & EXTENT_BIO_PARENT_LOCKED;
David Sterba306e16c2011-04-19 14:29:38 +02002793 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002794 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002795 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002796 size_t blocksize = inode->i_sb->s_blocksize;
Mark Fasheh4b384312013-08-06 11:42:50 -07002797 unsigned long this_bio_flag = *bio_flags & EXTENT_BIO_PARENT_LOCKED;
Chris Masond1310b22008-01-24 16:13:08 -05002798
2799 set_page_extent_mapped(page);
2800
Miao Xie99740902013-07-25 19:22:36 +08002801 end = page_end;
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002802 if (!PageUptodate(page)) {
2803 if (cleancache_get_page(page) == 0) {
2804 BUG_ON(blocksize != PAGE_SIZE);
Miao Xie99740902013-07-25 19:22:36 +08002805 unlock_extent(tree, start, end);
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002806 goto out;
2807 }
2808 }
2809
Chris Masonc8b97812008-10-29 14:49:59 -04002810 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2811 char *userpage;
2812 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2813
2814 if (zero_offset) {
2815 iosize = PAGE_CACHE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002816 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002817 memset(userpage + zero_offset, 0, iosize);
2818 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002819 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002820 }
2821 }
Chris Masond1310b22008-01-24 16:13:08 -05002822 while (cur <= end) {
Josef Bacikc8f2f242013-02-11 11:33:00 -05002823 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2824
Chris Masond1310b22008-01-24 16:13:08 -05002825 if (cur >= last_byte) {
2826 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002827 struct extent_state *cached = NULL;
2828
David Sterba306e16c2011-04-19 14:29:38 +02002829 iosize = PAGE_CACHE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002830 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002831 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002832 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002833 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002834 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002835 &cached, GFP_NOFS);
Mark Fasheh4b384312013-08-06 11:42:50 -07002836 if (!parent_locked)
2837 unlock_extent_cached(tree, cur,
2838 cur + iosize - 1,
2839 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002840 break;
2841 }
Miao Xie125bac012013-07-25 19:22:37 +08002842 em = __get_extent_map(inode, page, pg_offset, cur,
2843 end - cur + 1, get_extent, em_cached);
David Sterbac7040052011-04-19 18:00:01 +02002844 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002845 SetPageError(page);
Mark Fasheh4b384312013-08-06 11:42:50 -07002846 if (!parent_locked)
2847 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002848 break;
2849 }
Chris Masond1310b22008-01-24 16:13:08 -05002850 extent_offset = cur - em->start;
2851 BUG_ON(extent_map_end(em) <= cur);
2852 BUG_ON(end < cur);
2853
Li Zefan261507a02010-12-17 14:21:50 +08002854 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Mark Fasheh4b384312013-08-06 11:42:50 -07002855 this_bio_flag |= EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002856 extent_set_compress_type(&this_bio_flag,
2857 em->compress_type);
2858 }
Chris Masonc8b97812008-10-29 14:49:59 -04002859
Chris Masond1310b22008-01-24 16:13:08 -05002860 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2861 cur_end = min(extent_map_end(em) - 1, end);
Qu Wenruofda28322013-02-26 08:10:22 +00002862 iosize = ALIGN(iosize, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -04002863 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2864 disk_io_size = em->block_len;
2865 sector = em->block_start >> 9;
2866 } else {
2867 sector = (em->block_start + extent_offset) >> 9;
2868 disk_io_size = iosize;
2869 }
Chris Masond1310b22008-01-24 16:13:08 -05002870 bdev = em->bdev;
2871 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002872 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2873 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002874 free_extent_map(em);
2875 em = NULL;
2876
2877 /* we've found a hole, just zero and go on */
2878 if (block_start == EXTENT_MAP_HOLE) {
2879 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002880 struct extent_state *cached = NULL;
2881
Cong Wang7ac687d2011-11-25 23:14:28 +08002882 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002883 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002884 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002885 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002886
2887 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002888 &cached, GFP_NOFS);
2889 unlock_extent_cached(tree, cur, cur + iosize - 1,
2890 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002891 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002892 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002893 continue;
2894 }
2895 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002896 if (test_range_bit(tree, cur, cur_end,
2897 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002898 check_page_uptodate(tree, page);
Mark Fasheh4b384312013-08-06 11:42:50 -07002899 if (!parent_locked)
2900 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002901 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002902 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002903 continue;
2904 }
Chris Mason70dec802008-01-29 09:59:12 -05002905 /* we have an inline extent but it didn't get marked up
2906 * to date. Error out
2907 */
2908 if (block_start == EXTENT_MAP_INLINE) {
2909 SetPageError(page);
Mark Fasheh4b384312013-08-06 11:42:50 -07002910 if (!parent_locked)
2911 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05002912 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002913 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05002914 continue;
2915 }
Chris Masond1310b22008-01-24 16:13:08 -05002916
Josef Bacikc8f2f242013-02-11 11:33:00 -05002917 pnr -= page->index;
Josef Bacikd4c7ca82013-04-19 19:49:09 -04002918 ret = submit_extent_page(rw, tree, page,
David Sterba306e16c2011-04-19 14:29:38 +02002919 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04002920 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002921 end_bio_extent_readpage, mirror_num,
2922 *bio_flags,
2923 this_bio_flag);
Josef Bacikc8f2f242013-02-11 11:33:00 -05002924 if (!ret) {
2925 nr++;
2926 *bio_flags = this_bio_flag;
2927 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002928 SetPageError(page);
Mark Fasheh4b384312013-08-06 11:42:50 -07002929 if (!parent_locked)
2930 unlock_extent(tree, cur, cur + iosize - 1);
Josef Bacikedd33c92012-10-05 16:40:32 -04002931 }
Chris Masond1310b22008-01-24 16:13:08 -05002932 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002933 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002934 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002935out:
Chris Masond1310b22008-01-24 16:13:08 -05002936 if (!nr) {
2937 if (!PageError(page))
2938 SetPageUptodate(page);
2939 unlock_page(page);
2940 }
2941 return 0;
2942}
2943
Miao Xie99740902013-07-25 19:22:36 +08002944static inline void __do_contiguous_readpages(struct extent_io_tree *tree,
2945 struct page *pages[], int nr_pages,
2946 u64 start, u64 end,
2947 get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08002948 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08002949 struct bio **bio, int mirror_num,
2950 unsigned long *bio_flags, int rw)
2951{
2952 struct inode *inode;
2953 struct btrfs_ordered_extent *ordered;
2954 int index;
2955
2956 inode = pages[0]->mapping->host;
2957 while (1) {
2958 lock_extent(tree, start, end);
2959 ordered = btrfs_lookup_ordered_range(inode, start,
2960 end - start + 1);
2961 if (!ordered)
2962 break;
2963 unlock_extent(tree, start, end);
2964 btrfs_start_ordered_extent(inode, ordered, 1);
2965 btrfs_put_ordered_extent(ordered);
2966 }
2967
2968 for (index = 0; index < nr_pages; index++) {
Miao Xie125bac012013-07-25 19:22:37 +08002969 __do_readpage(tree, pages[index], get_extent, em_cached, bio,
2970 mirror_num, bio_flags, rw);
Miao Xie99740902013-07-25 19:22:36 +08002971 page_cache_release(pages[index]);
2972 }
2973}
2974
2975static void __extent_readpages(struct extent_io_tree *tree,
2976 struct page *pages[],
2977 int nr_pages, get_extent_t *get_extent,
Miao Xie125bac012013-07-25 19:22:37 +08002978 struct extent_map **em_cached,
Miao Xie99740902013-07-25 19:22:36 +08002979 struct bio **bio, int mirror_num,
2980 unsigned long *bio_flags, int rw)
2981{
Stefan Behrens35a36212013-08-14 18:12:25 +02002982 u64 start = 0;
Miao Xie99740902013-07-25 19:22:36 +08002983 u64 end = 0;
2984 u64 page_start;
2985 int index;
Stefan Behrens35a36212013-08-14 18:12:25 +02002986 int first_index = 0;
Miao Xie99740902013-07-25 19:22:36 +08002987
2988 for (index = 0; index < nr_pages; index++) {
2989 page_start = page_offset(pages[index]);
2990 if (!end) {
2991 start = page_start;
2992 end = start + PAGE_CACHE_SIZE - 1;
2993 first_index = index;
2994 } else if (end + 1 == page_start) {
2995 end += PAGE_CACHE_SIZE;
2996 } else {
2997 __do_contiguous_readpages(tree, &pages[first_index],
2998 index - first_index, start,
Miao Xie125bac012013-07-25 19:22:37 +08002999 end, get_extent, em_cached,
3000 bio, mirror_num, bio_flags,
3001 rw);
Miao Xie99740902013-07-25 19:22:36 +08003002 start = page_start;
3003 end = start + PAGE_CACHE_SIZE - 1;
3004 first_index = index;
3005 }
3006 }
3007
3008 if (end)
3009 __do_contiguous_readpages(tree, &pages[first_index],
3010 index - first_index, start,
Miao Xie125bac012013-07-25 19:22:37 +08003011 end, get_extent, em_cached, bio,
Miao Xie99740902013-07-25 19:22:36 +08003012 mirror_num, bio_flags, rw);
3013}
3014
3015static int __extent_read_full_page(struct extent_io_tree *tree,
3016 struct page *page,
3017 get_extent_t *get_extent,
3018 struct bio **bio, int mirror_num,
3019 unsigned long *bio_flags, int rw)
3020{
3021 struct inode *inode = page->mapping->host;
3022 struct btrfs_ordered_extent *ordered;
3023 u64 start = page_offset(page);
3024 u64 end = start + PAGE_CACHE_SIZE - 1;
3025 int ret;
3026
3027 while (1) {
3028 lock_extent(tree, start, end);
3029 ordered = btrfs_lookup_ordered_extent(inode, start);
3030 if (!ordered)
3031 break;
3032 unlock_extent(tree, start, end);
3033 btrfs_start_ordered_extent(inode, ordered, 1);
3034 btrfs_put_ordered_extent(ordered);
3035 }
3036
Miao Xie125bac012013-07-25 19:22:37 +08003037 ret = __do_readpage(tree, page, get_extent, NULL, bio, mirror_num,
3038 bio_flags, rw);
Miao Xie99740902013-07-25 19:22:36 +08003039 return ret;
3040}
3041
Chris Masond1310b22008-01-24 16:13:08 -05003042int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003043 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003044{
3045 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003046 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003047 int ret;
3048
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003049 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04003050 &bio_flags, READ);
Chris Masond1310b22008-01-24 16:13:08 -05003051 if (bio)
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02003052 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003053 return ret;
3054}
Chris Masond1310b22008-01-24 16:13:08 -05003055
Mark Fasheh4b384312013-08-06 11:42:50 -07003056int extent_read_full_page_nolock(struct extent_io_tree *tree, struct page *page,
3057 get_extent_t *get_extent, int mirror_num)
3058{
3059 struct bio *bio = NULL;
3060 unsigned long bio_flags = EXTENT_BIO_PARENT_LOCKED;
3061 int ret;
3062
3063 ret = __do_readpage(tree, page, get_extent, NULL, &bio, mirror_num,
3064 &bio_flags, READ);
3065 if (bio)
3066 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
3067 return ret;
3068}
3069
Chris Mason11c83492009-04-20 15:50:09 -04003070static noinline void update_nr_written(struct page *page,
3071 struct writeback_control *wbc,
3072 unsigned long nr_written)
3073{
3074 wbc->nr_to_write -= nr_written;
3075 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
3076 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
3077 page->mapping->writeback_index = page->index + nr_written;
3078}
3079
Chris Masond1310b22008-01-24 16:13:08 -05003080/*
3081 * the writepage semantics are similar to regular writepage. extent
3082 * records are inserted to lock ranges in the tree, and as dirty areas
3083 * are found, they are marked writeback. Then the lock bits are removed
3084 * and the end_io handler clears the writeback ranges
3085 */
3086static int __extent_writepage(struct page *page, struct writeback_control *wbc,
3087 void *data)
3088{
3089 struct inode *inode = page->mapping->host;
3090 struct extent_page_data *epd = data;
3091 struct extent_io_tree *tree = epd->tree;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003092 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05003093 u64 delalloc_start;
3094 u64 page_end = start + PAGE_CACHE_SIZE - 1;
3095 u64 end;
3096 u64 cur = start;
3097 u64 extent_offset;
3098 u64 last_byte = i_size_read(inode);
3099 u64 block_start;
3100 u64 iosize;
3101 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04003102 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003103 struct extent_map *em;
3104 struct block_device *bdev;
3105 int ret;
3106 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003107 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003108 size_t blocksize;
3109 loff_t i_size = i_size_read(inode);
3110 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
3111 u64 nr_delalloc;
3112 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04003113 int page_started;
3114 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04003115 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05003116 unsigned long nr_written = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04003117 bool fill_delalloc = true;
Chris Masond1310b22008-01-24 16:13:08 -05003118
Chris Masonffbd5172009-04-20 15:50:09 -04003119 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01003120 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04003121 else
3122 write_flags = WRITE;
3123
liubo1abe9b82011-03-24 11:18:59 +00003124 trace___extent_writepage(page, inode, wbc);
3125
Chris Masond1310b22008-01-24 16:13:08 -05003126 WARN_ON(!PageLocked(page));
Chris Masonbf0da8c2011-11-04 12:29:37 -04003127
3128 ClearPageError(page);
3129
Chris Mason7f3c74f2008-07-18 12:01:11 -04003130 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04003131 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04003132 (page->index == end_index && !pg_offset)) {
Lukas Czernerd47992f2013-05-21 23:17:23 -04003133 page->mapping->a_ops->invalidatepage(page, 0, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05003134 unlock_page(page);
3135 return 0;
3136 }
3137
3138 if (page->index == end_index) {
3139 char *userpage;
3140
Cong Wang7ac687d2011-11-25 23:14:28 +08003141 userpage = kmap_atomic(page);
Chris Mason7f3c74f2008-07-18 12:01:11 -04003142 memset(userpage + pg_offset, 0,
3143 PAGE_CACHE_SIZE - pg_offset);
Cong Wang7ac687d2011-11-25 23:14:28 +08003144 kunmap_atomic(userpage);
Chris Mason211c17f2008-05-15 09:13:45 -04003145 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003146 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003147 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003148
3149 set_page_extent_mapped(page);
3150
Josef Bacik9e487102011-08-01 12:08:18 -04003151 if (!tree->ops || !tree->ops->fill_delalloc)
3152 fill_delalloc = false;
3153
Chris Masond1310b22008-01-24 16:13:08 -05003154 delalloc_start = start;
3155 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04003156 page_started = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04003157 if (!epd->extent_locked && fill_delalloc) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003158 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04003159 /*
3160 * make sure the wbc mapping index is at least updated
3161 * to this page.
3162 */
3163 update_nr_written(page, wbc, 0);
3164
Chris Masond3977122009-01-05 21:25:51 -05003165 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05003166 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04003167 page,
3168 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05003169 &delalloc_end,
3170 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05003171 if (nr_delalloc == 0) {
3172 delalloc_start = delalloc_end + 1;
3173 continue;
3174 }
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09003175 ret = tree->ops->fill_delalloc(inode, page,
3176 delalloc_start,
3177 delalloc_end,
3178 &page_started,
3179 &nr_written);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003180 /* File system has been set read-only */
3181 if (ret) {
3182 SetPageError(page);
3183 goto done;
3184 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003185 /*
3186 * delalloc_end is already one less than the total
3187 * length, so we don't subtract one from
3188 * PAGE_CACHE_SIZE
3189 */
3190 delalloc_to_write += (delalloc_end - delalloc_start +
3191 PAGE_CACHE_SIZE) >>
3192 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003193 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05003194 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003195 if (wbc->nr_to_write < delalloc_to_write) {
3196 int thresh = 8192;
3197
3198 if (delalloc_to_write < thresh * 2)
3199 thresh = delalloc_to_write;
3200 wbc->nr_to_write = min_t(u64, delalloc_to_write,
3201 thresh);
3202 }
Chris Masonc8b97812008-10-29 14:49:59 -04003203
Chris Mason771ed682008-11-06 22:02:51 -05003204 /* did the fill delalloc function already unlock and start
3205 * the IO?
3206 */
3207 if (page_started) {
3208 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04003209 /*
3210 * we've unlocked the page, so we can't update
3211 * the mapping's writeback index, just update
3212 * nr_to_write.
3213 */
3214 wbc->nr_to_write -= nr_written;
3215 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05003216 }
Chris Masonc8b97812008-10-29 14:49:59 -04003217 }
Chris Mason247e7432008-07-17 12:53:51 -04003218 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04003219 ret = tree->ops->writepage_start_hook(page, start,
3220 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01003221 if (ret) {
3222 /* Fixup worker will requeue */
3223 if (ret == -EBUSY)
3224 wbc->pages_skipped++;
3225 else
3226 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04003227 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04003228 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05003229 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04003230 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04003231 }
3232 }
3233
Chris Mason11c83492009-04-20 15:50:09 -04003234 /*
3235 * we don't want to touch the inode after unlocking the page,
3236 * so we update the mapping writeback index now
3237 */
3238 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05003239
Chris Masond1310b22008-01-24 16:13:08 -05003240 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05003241 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003242 if (tree->ops && tree->ops->writepage_end_io_hook)
3243 tree->ops->writepage_end_io_hook(page, start,
3244 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003245 goto done;
3246 }
3247
Chris Masond1310b22008-01-24 16:13:08 -05003248 blocksize = inode->i_sb->s_blocksize;
3249
3250 while (cur <= end) {
3251 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04003252 if (tree->ops && tree->ops->writepage_end_io_hook)
3253 tree->ops->writepage_end_io_hook(page, cur,
3254 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05003255 break;
3256 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003257 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05003258 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02003259 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05003260 SetPageError(page);
3261 break;
3262 }
3263
3264 extent_offset = cur - em->start;
3265 BUG_ON(extent_map_end(em) <= cur);
3266 BUG_ON(end < cur);
3267 iosize = min(extent_map_end(em) - cur, end - cur + 1);
Qu Wenruofda28322013-02-26 08:10:22 +00003268 iosize = ALIGN(iosize, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05003269 sector = (em->block_start + extent_offset) >> 9;
3270 bdev = em->bdev;
3271 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04003272 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05003273 free_extent_map(em);
3274 em = NULL;
3275
Chris Masonc8b97812008-10-29 14:49:59 -04003276 /*
3277 * compressed and inline extents are written through other
3278 * paths in the FS
3279 */
3280 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003281 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003282 /*
3283 * end_io notification does not happen here for
3284 * compressed extents
3285 */
3286 if (!compressed && tree->ops &&
3287 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003288 tree->ops->writepage_end_io_hook(page, cur,
3289 cur + iosize - 1,
3290 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003291 else if (compressed) {
3292 /* we don't want to end_page_writeback on
3293 * a compressed extent. this happens
3294 * elsewhere
3295 */
3296 nr++;
3297 }
3298
3299 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003300 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003301 continue;
3302 }
Chris Masond1310b22008-01-24 16:13:08 -05003303 /* leave this out until we have a page_mkwrite call */
3304 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04003305 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05003306 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003307 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003308 continue;
3309 }
Chris Masonc8b97812008-10-29 14:49:59 -04003310
Chris Masond1310b22008-01-24 16:13:08 -05003311 if (tree->ops && tree->ops->writepage_io_hook) {
3312 ret = tree->ops->writepage_io_hook(page, cur,
3313 cur + iosize - 1);
3314 } else {
3315 ret = 0;
3316 }
Chris Mason1259ab72008-05-12 13:39:03 -04003317 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003318 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003319 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003320 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003321
Chris Masond1310b22008-01-24 16:13:08 -05003322 set_range_writeback(tree, cur, cur + iosize - 1);
3323 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05003324 printk(KERN_ERR "btrfs warning page %lu not "
3325 "writeback, cur %llu end %llu\n",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003326 page->index, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05003327 }
3328
Chris Masonffbd5172009-04-20 15:50:09 -04003329 ret = submit_extent_page(write_flags, tree, page,
3330 sector, iosize, pg_offset,
3331 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04003332 end_bio_extent_writepage,
3333 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003334 if (ret)
3335 SetPageError(page);
3336 }
3337 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003338 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003339 nr++;
3340 }
3341done:
3342 if (nr == 0) {
3343 /* make sure the mapping tag for page dirty gets cleared */
3344 set_page_writeback(page);
3345 end_page_writeback(page);
3346 }
Chris Masond1310b22008-01-24 16:13:08 -05003347 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05003348
Chris Mason11c83492009-04-20 15:50:09 -04003349done_unlocked:
3350
Chris Mason2c64c532009-09-02 15:04:12 -04003351 /* drop our reference on any cached states */
3352 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05003353 return 0;
3354}
3355
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003356static int eb_wait(void *word)
3357{
3358 io_schedule();
3359 return 0;
3360}
3361
Josef Bacikfd8b2b62013-04-24 16:41:19 -04003362void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003363{
3364 wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
3365 TASK_UNINTERRUPTIBLE);
3366}
3367
3368static int lock_extent_buffer_for_io(struct extent_buffer *eb,
3369 struct btrfs_fs_info *fs_info,
3370 struct extent_page_data *epd)
3371{
3372 unsigned long i, num_pages;
3373 int flush = 0;
3374 int ret = 0;
3375
3376 if (!btrfs_try_tree_write_lock(eb)) {
3377 flush = 1;
3378 flush_write_bio(epd);
3379 btrfs_tree_lock(eb);
3380 }
3381
3382 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3383 btrfs_tree_unlock(eb);
3384 if (!epd->sync_io)
3385 return 0;
3386 if (!flush) {
3387 flush_write_bio(epd);
3388 flush = 1;
3389 }
Chris Masona098d8e2012-03-21 12:09:56 -04003390 while (1) {
3391 wait_on_extent_buffer_writeback(eb);
3392 btrfs_tree_lock(eb);
3393 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3394 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003395 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003396 }
3397 }
3398
Josef Bacik51561ff2012-07-20 16:25:24 -04003399 /*
3400 * We need to do this to prevent races in people who check if the eb is
3401 * under IO since we can end up having no IO bits set for a short period
3402 * of time.
3403 */
3404 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003405 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3406 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
Josef Bacik51561ff2012-07-20 16:25:24 -04003407 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003408 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
Miao Xiee2d84522013-01-29 10:09:20 +00003409 __percpu_counter_add(&fs_info->dirty_metadata_bytes,
3410 -eb->len,
3411 fs_info->dirty_metadata_batch);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003412 ret = 1;
Josef Bacik51561ff2012-07-20 16:25:24 -04003413 } else {
3414 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003415 }
3416
3417 btrfs_tree_unlock(eb);
3418
3419 if (!ret)
3420 return ret;
3421
3422 num_pages = num_extent_pages(eb->start, eb->len);
3423 for (i = 0; i < num_pages; i++) {
3424 struct page *p = extent_buffer_page(eb, i);
3425
3426 if (!trylock_page(p)) {
3427 if (!flush) {
3428 flush_write_bio(epd);
3429 flush = 1;
3430 }
3431 lock_page(p);
3432 }
3433 }
3434
3435 return ret;
3436}
3437
3438static void end_extent_buffer_writeback(struct extent_buffer *eb)
3439{
3440 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3441 smp_mb__after_clear_bit();
3442 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3443}
3444
3445static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
3446{
3447 int uptodate = err == 0;
3448 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
3449 struct extent_buffer *eb;
3450 int done;
3451
3452 do {
3453 struct page *page = bvec->bv_page;
3454
3455 bvec--;
3456 eb = (struct extent_buffer *)page->private;
3457 BUG_ON(!eb);
3458 done = atomic_dec_and_test(&eb->io_pages);
3459
3460 if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
3461 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3462 ClearPageUptodate(page);
3463 SetPageError(page);
3464 }
3465
3466 end_page_writeback(page);
3467
3468 if (!done)
3469 continue;
3470
3471 end_extent_buffer_writeback(eb);
3472 } while (bvec >= bio->bi_io_vec);
3473
3474 bio_put(bio);
3475
3476}
3477
3478static int write_one_eb(struct extent_buffer *eb,
3479 struct btrfs_fs_info *fs_info,
3480 struct writeback_control *wbc,
3481 struct extent_page_data *epd)
3482{
3483 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
3484 u64 offset = eb->start;
3485 unsigned long i, num_pages;
Josef Bacikde0022b2012-09-25 14:25:58 -04003486 unsigned long bio_flags = 0;
Josef Bacikd4c7ca82013-04-19 19:49:09 -04003487 int rw = (epd->sync_io ? WRITE_SYNC : WRITE) | REQ_META;
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003488 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003489
3490 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3491 num_pages = num_extent_pages(eb->start, eb->len);
3492 atomic_set(&eb->io_pages, num_pages);
Josef Bacikde0022b2012-09-25 14:25:58 -04003493 if (btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID)
3494 bio_flags = EXTENT_BIO_TREE_LOG;
3495
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003496 for (i = 0; i < num_pages; i++) {
3497 struct page *p = extent_buffer_page(eb, i);
3498
3499 clear_page_dirty_for_io(p);
3500 set_page_writeback(p);
3501 ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
3502 PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
3503 -1, end_bio_extent_buffer_writepage,
Josef Bacikde0022b2012-09-25 14:25:58 -04003504 0, epd->bio_flags, bio_flags);
3505 epd->bio_flags = bio_flags;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003506 if (ret) {
3507 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3508 SetPageError(p);
3509 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3510 end_extent_buffer_writeback(eb);
3511 ret = -EIO;
3512 break;
3513 }
3514 offset += PAGE_CACHE_SIZE;
3515 update_nr_written(p, wbc, 1);
3516 unlock_page(p);
3517 }
3518
3519 if (unlikely(ret)) {
3520 for (; i < num_pages; i++) {
3521 struct page *p = extent_buffer_page(eb, i);
3522 unlock_page(p);
3523 }
3524 }
3525
3526 return ret;
3527}
3528
3529int btree_write_cache_pages(struct address_space *mapping,
3530 struct writeback_control *wbc)
3531{
3532 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3533 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3534 struct extent_buffer *eb, *prev_eb = NULL;
3535 struct extent_page_data epd = {
3536 .bio = NULL,
3537 .tree = tree,
3538 .extent_locked = 0,
3539 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003540 .bio_flags = 0,
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003541 };
3542 int ret = 0;
3543 int done = 0;
3544 int nr_to_write_done = 0;
3545 struct pagevec pvec;
3546 int nr_pages;
3547 pgoff_t index;
3548 pgoff_t end; /* Inclusive */
3549 int scanned = 0;
3550 int tag;
3551
3552 pagevec_init(&pvec, 0);
3553 if (wbc->range_cyclic) {
3554 index = mapping->writeback_index; /* Start from prev offset */
3555 end = -1;
3556 } else {
3557 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3558 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3559 scanned = 1;
3560 }
3561 if (wbc->sync_mode == WB_SYNC_ALL)
3562 tag = PAGECACHE_TAG_TOWRITE;
3563 else
3564 tag = PAGECACHE_TAG_DIRTY;
3565retry:
3566 if (wbc->sync_mode == WB_SYNC_ALL)
3567 tag_pages_for_writeback(mapping, index, end);
3568 while (!done && !nr_to_write_done && (index <= end) &&
3569 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3570 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3571 unsigned i;
3572
3573 scanned = 1;
3574 for (i = 0; i < nr_pages; i++) {
3575 struct page *page = pvec.pages[i];
3576
3577 if (!PagePrivate(page))
3578 continue;
3579
3580 if (!wbc->range_cyclic && page->index > end) {
3581 done = 1;
3582 break;
3583 }
3584
Josef Bacikb5bae262012-09-14 13:43:01 -04003585 spin_lock(&mapping->private_lock);
3586 if (!PagePrivate(page)) {
3587 spin_unlock(&mapping->private_lock);
3588 continue;
3589 }
3590
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003591 eb = (struct extent_buffer *)page->private;
Josef Bacikb5bae262012-09-14 13:43:01 -04003592
3593 /*
3594 * Shouldn't happen and normally this would be a BUG_ON
3595 * but no sense in crashing the users box for something
3596 * we can survive anyway.
3597 */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05303598 if (WARN_ON(!eb)) {
Josef Bacikb5bae262012-09-14 13:43:01 -04003599 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003600 continue;
3601 }
3602
Josef Bacikb5bae262012-09-14 13:43:01 -04003603 if (eb == prev_eb) {
3604 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003605 continue;
3606 }
3607
Josef Bacikb5bae262012-09-14 13:43:01 -04003608 ret = atomic_inc_not_zero(&eb->refs);
3609 spin_unlock(&mapping->private_lock);
3610 if (!ret)
3611 continue;
3612
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003613 prev_eb = eb;
3614 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3615 if (!ret) {
3616 free_extent_buffer(eb);
3617 continue;
3618 }
3619
3620 ret = write_one_eb(eb, fs_info, wbc, &epd);
3621 if (ret) {
3622 done = 1;
3623 free_extent_buffer(eb);
3624 break;
3625 }
3626 free_extent_buffer(eb);
3627
3628 /*
3629 * the filesystem may choose to bump up nr_to_write.
3630 * We have to make sure to honor the new nr_to_write
3631 * at any time
3632 */
3633 nr_to_write_done = wbc->nr_to_write <= 0;
3634 }
3635 pagevec_release(&pvec);
3636 cond_resched();
3637 }
3638 if (!scanned && !done) {
3639 /*
3640 * We hit the last page and there is more work to be done: wrap
3641 * back to the start of the file
3642 */
3643 scanned = 1;
3644 index = 0;
3645 goto retry;
3646 }
3647 flush_write_bio(&epd);
3648 return ret;
3649}
3650
Chris Masond1310b22008-01-24 16:13:08 -05003651/**
Chris Mason4bef0842008-09-08 11:18:08 -04003652 * 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 -05003653 * @mapping: address space structure to write
3654 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3655 * @writepage: function called for each page
3656 * @data: data passed to writepage function
3657 *
3658 * If a page is already under I/O, write_cache_pages() skips it, even
3659 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3660 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3661 * and msync() need to guarantee that all the data which was dirty at the time
3662 * the call was made get new I/O started against them. If wbc->sync_mode is
3663 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3664 * existing IO to complete.
3665 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003666static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04003667 struct address_space *mapping,
3668 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003669 writepage_t writepage, void *data,
3670 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05003671{
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003672 struct inode *inode = mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003673 int ret = 0;
3674 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003675 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003676 struct pagevec pvec;
3677 int nr_pages;
3678 pgoff_t index;
3679 pgoff_t end; /* Inclusive */
3680 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003681 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003682
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003683 /*
3684 * We have to hold onto the inode so that ordered extents can do their
3685 * work when the IO finishes. The alternative to this is failing to add
3686 * an ordered extent if the igrab() fails there and that is a huge pain
3687 * to deal with, so instead just hold onto the inode throughout the
3688 * writepages operation. If it fails here we are freeing up the inode
3689 * anyway and we'd rather not waste our time writing out stuff that is
3690 * going to be truncated anyway.
3691 */
3692 if (!igrab(inode))
3693 return 0;
3694
Chris Masond1310b22008-01-24 16:13:08 -05003695 pagevec_init(&pvec, 0);
3696 if (wbc->range_cyclic) {
3697 index = mapping->writeback_index; /* Start from prev offset */
3698 end = -1;
3699 } else {
3700 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3701 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003702 scanned = 1;
3703 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003704 if (wbc->sync_mode == WB_SYNC_ALL)
3705 tag = PAGECACHE_TAG_TOWRITE;
3706 else
3707 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003708retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003709 if (wbc->sync_mode == WB_SYNC_ALL)
3710 tag_pages_for_writeback(mapping, index, end);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003711 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003712 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3713 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003714 unsigned i;
3715
3716 scanned = 1;
3717 for (i = 0; i < nr_pages; i++) {
3718 struct page *page = pvec.pages[i];
3719
3720 /*
3721 * At this point we hold neither mapping->tree_lock nor
3722 * lock on the page itself: the page may be truncated or
3723 * invalidated (changing page->mapping to NULL), or even
3724 * swizzled back from swapper_space to tmpfs file
3725 * mapping
3726 */
Josef Bacikc8f2f242013-02-11 11:33:00 -05003727 if (!trylock_page(page)) {
3728 flush_fn(data);
3729 lock_page(page);
Chris Mason01d658f2011-11-01 10:08:06 -04003730 }
Chris Masond1310b22008-01-24 16:13:08 -05003731
3732 if (unlikely(page->mapping != mapping)) {
3733 unlock_page(page);
3734 continue;
3735 }
3736
3737 if (!wbc->range_cyclic && page->index > end) {
3738 done = 1;
3739 unlock_page(page);
3740 continue;
3741 }
3742
Chris Masond2c3f4f2008-11-19 12:44:22 -05003743 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003744 if (PageWriteback(page))
3745 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05003746 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003747 }
Chris Masond1310b22008-01-24 16:13:08 -05003748
3749 if (PageWriteback(page) ||
3750 !clear_page_dirty_for_io(page)) {
3751 unlock_page(page);
3752 continue;
3753 }
3754
3755 ret = (*writepage)(page, wbc, data);
3756
3757 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3758 unlock_page(page);
3759 ret = 0;
3760 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003761 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05003762 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003763
3764 /*
3765 * the filesystem may choose to bump up nr_to_write.
3766 * We have to make sure to honor the new nr_to_write
3767 * at any time
3768 */
3769 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05003770 }
3771 pagevec_release(&pvec);
3772 cond_resched();
3773 }
3774 if (!scanned && !done) {
3775 /*
3776 * We hit the last page and there is more work to be done: wrap
3777 * back to the start of the file
3778 */
3779 scanned = 1;
3780 index = 0;
3781 goto retry;
3782 }
Josef Bacik7fd1a3f2012-06-27 17:18:41 -04003783 btrfs_add_delayed_iput(inode);
Chris Masond1310b22008-01-24 16:13:08 -05003784 return ret;
3785}
Chris Masond1310b22008-01-24 16:13:08 -05003786
Chris Masonffbd5172009-04-20 15:50:09 -04003787static void flush_epd_write_bio(struct extent_page_data *epd)
3788{
3789 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04003790 int rw = WRITE;
3791 int ret;
3792
Chris Masonffbd5172009-04-20 15:50:09 -04003793 if (epd->sync_io)
Jeff Mahoney355808c2011-10-03 23:23:14 -04003794 rw = WRITE_SYNC;
3795
Josef Bacikde0022b2012-09-25 14:25:58 -04003796 ret = submit_one_bio(rw, epd->bio, 0, epd->bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003797 BUG_ON(ret < 0); /* -ENOMEM */
Chris Masonffbd5172009-04-20 15:50:09 -04003798 epd->bio = NULL;
3799 }
3800}
3801
Chris Masond2c3f4f2008-11-19 12:44:22 -05003802static noinline void flush_write_bio(void *data)
3803{
3804 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04003805 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003806}
3807
Chris Masond1310b22008-01-24 16:13:08 -05003808int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3809 get_extent_t *get_extent,
3810 struct writeback_control *wbc)
3811{
3812 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003813 struct extent_page_data epd = {
3814 .bio = NULL,
3815 .tree = tree,
3816 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003817 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003818 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003819 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05003820 };
Chris Masond1310b22008-01-24 16:13:08 -05003821
Chris Masond1310b22008-01-24 16:13:08 -05003822 ret = __extent_writepage(page, wbc, &epd);
3823
Chris Masonffbd5172009-04-20 15:50:09 -04003824 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003825 return ret;
3826}
Chris Masond1310b22008-01-24 16:13:08 -05003827
Chris Mason771ed682008-11-06 22:02:51 -05003828int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3829 u64 start, u64 end, get_extent_t *get_extent,
3830 int mode)
3831{
3832 int ret = 0;
3833 struct address_space *mapping = inode->i_mapping;
3834 struct page *page;
3835 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3836 PAGE_CACHE_SHIFT;
3837
3838 struct extent_page_data epd = {
3839 .bio = NULL,
3840 .tree = tree,
3841 .get_extent = get_extent,
3842 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04003843 .sync_io = mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003844 .bio_flags = 0,
Chris Mason771ed682008-11-06 22:02:51 -05003845 };
3846 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05003847 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05003848 .nr_to_write = nr_pages * 2,
3849 .range_start = start,
3850 .range_end = end + 1,
3851 };
3852
Chris Masond3977122009-01-05 21:25:51 -05003853 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05003854 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3855 if (clear_page_dirty_for_io(page))
3856 ret = __extent_writepage(page, &wbc_writepages, &epd);
3857 else {
3858 if (tree->ops && tree->ops->writepage_end_io_hook)
3859 tree->ops->writepage_end_io_hook(page, start,
3860 start + PAGE_CACHE_SIZE - 1,
3861 NULL, 1);
3862 unlock_page(page);
3863 }
3864 page_cache_release(page);
3865 start += PAGE_CACHE_SIZE;
3866 }
3867
Chris Masonffbd5172009-04-20 15:50:09 -04003868 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05003869 return ret;
3870}
Chris Masond1310b22008-01-24 16:13:08 -05003871
3872int extent_writepages(struct extent_io_tree *tree,
3873 struct address_space *mapping,
3874 get_extent_t *get_extent,
3875 struct writeback_control *wbc)
3876{
3877 int ret = 0;
3878 struct extent_page_data epd = {
3879 .bio = NULL,
3880 .tree = tree,
3881 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003882 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003883 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Josef Bacikde0022b2012-09-25 14:25:58 -04003884 .bio_flags = 0,
Chris Masond1310b22008-01-24 16:13:08 -05003885 };
3886
Chris Mason4bef0842008-09-08 11:18:08 -04003887 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003888 __extent_writepage, &epd,
3889 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04003890 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003891 return ret;
3892}
Chris Masond1310b22008-01-24 16:13:08 -05003893
3894int extent_readpages(struct extent_io_tree *tree,
3895 struct address_space *mapping,
3896 struct list_head *pages, unsigned nr_pages,
3897 get_extent_t get_extent)
3898{
3899 struct bio *bio = NULL;
3900 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04003901 unsigned long bio_flags = 0;
Liu Bo67c96842012-07-20 21:43:09 -06003902 struct page *pagepool[16];
3903 struct page *page;
Miao Xie125bac012013-07-25 19:22:37 +08003904 struct extent_map *em_cached = NULL;
Liu Bo67c96842012-07-20 21:43:09 -06003905 int nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003906
Chris Masond1310b22008-01-24 16:13:08 -05003907 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
Liu Bo67c96842012-07-20 21:43:09 -06003908 page = list_entry(pages->prev, struct page, lru);
Chris Masond1310b22008-01-24 16:13:08 -05003909
3910 prefetchw(&page->flags);
3911 list_del(&page->lru);
Liu Bo67c96842012-07-20 21:43:09 -06003912 if (add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04003913 page->index, GFP_NOFS)) {
Liu Bo67c96842012-07-20 21:43:09 -06003914 page_cache_release(page);
3915 continue;
Chris Masond1310b22008-01-24 16:13:08 -05003916 }
Liu Bo67c96842012-07-20 21:43:09 -06003917
3918 pagepool[nr++] = page;
3919 if (nr < ARRAY_SIZE(pagepool))
3920 continue;
Miao Xie125bac012013-07-25 19:22:37 +08003921 __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
Miao Xie99740902013-07-25 19:22:36 +08003922 &bio, 0, &bio_flags, READ);
Liu Bo67c96842012-07-20 21:43:09 -06003923 nr = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003924 }
Miao Xie99740902013-07-25 19:22:36 +08003925 if (nr)
Miao Xie125bac012013-07-25 19:22:37 +08003926 __extent_readpages(tree, pagepool, nr, get_extent, &em_cached,
Miao Xie99740902013-07-25 19:22:36 +08003927 &bio, 0, &bio_flags, READ);
Liu Bo67c96842012-07-20 21:43:09 -06003928
Miao Xie125bac012013-07-25 19:22:37 +08003929 if (em_cached)
3930 free_extent_map(em_cached);
3931
Chris Masond1310b22008-01-24 16:13:08 -05003932 BUG_ON(!list_empty(pages));
3933 if (bio)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003934 return submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003935 return 0;
3936}
Chris Masond1310b22008-01-24 16:13:08 -05003937
3938/*
3939 * basic invalidatepage code, this waits on any locked or writeback
3940 * ranges corresponding to the page, and then deletes any extent state
3941 * records from the tree
3942 */
3943int extent_invalidatepage(struct extent_io_tree *tree,
3944 struct page *page, unsigned long offset)
3945{
Josef Bacik2ac55d42010-02-03 19:33:23 +00003946 struct extent_state *cached_state = NULL;
Miao Xie4eee4fa2012-12-21 09:17:45 +00003947 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05003948 u64 end = start + PAGE_CACHE_SIZE - 1;
3949 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3950
Qu Wenruofda28322013-02-26 08:10:22 +00003951 start += ALIGN(offset, blocksize);
Chris Masond1310b22008-01-24 16:13:08 -05003952 if (start > end)
3953 return 0;
3954
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003955 lock_extent_bits(tree, start, end, 0, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04003956 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003957 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04003958 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3959 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003960 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003961 return 0;
3962}
Chris Masond1310b22008-01-24 16:13:08 -05003963
3964/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04003965 * a helper for releasepage, this tests for areas of the page that
3966 * are locked or under IO and drops the related state bits if it is safe
3967 * to drop the page.
3968 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003969static int try_release_extent_state(struct extent_map_tree *map,
3970 struct extent_io_tree *tree,
3971 struct page *page, gfp_t mask)
Chris Mason7b13b7b2008-04-18 10:29:50 -04003972{
Miao Xie4eee4fa2012-12-21 09:17:45 +00003973 u64 start = page_offset(page);
Chris Mason7b13b7b2008-04-18 10:29:50 -04003974 u64 end = start + PAGE_CACHE_SIZE - 1;
3975 int ret = 1;
3976
Chris Mason211f90e2008-07-18 11:56:15 -04003977 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04003978 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04003979 ret = 0;
3980 else {
3981 if ((mask & GFP_NOFS) == GFP_NOFS)
3982 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04003983 /*
3984 * at this point we can safely clear everything except the
3985 * locked bit and the nodatasum bit
3986 */
Chris Masone3f24cc2011-02-14 12:52:08 -05003987 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04003988 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3989 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05003990
3991 /* if clear_extent_bit failed for enomem reasons,
3992 * we can't allow the release to continue.
3993 */
3994 if (ret < 0)
3995 ret = 0;
3996 else
3997 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003998 }
3999 return ret;
4000}
Chris Mason7b13b7b2008-04-18 10:29:50 -04004001
4002/*
Chris Masond1310b22008-01-24 16:13:08 -05004003 * a helper for releasepage. As long as there are no locked extents
4004 * in the range corresponding to the page, both state records and extent
4005 * map records are removed
4006 */
4007int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05004008 struct extent_io_tree *tree, struct page *page,
4009 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05004010{
4011 struct extent_map *em;
Miao Xie4eee4fa2012-12-21 09:17:45 +00004012 u64 start = page_offset(page);
Chris Masond1310b22008-01-24 16:13:08 -05004013 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04004014
Chris Mason70dec802008-01-29 09:59:12 -05004015 if ((mask & __GFP_WAIT) &&
4016 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05004017 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05004018 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05004019 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04004020 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05004021 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09004022 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04004023 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004024 break;
4025 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04004026 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
4027 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04004028 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004029 free_extent_map(em);
4030 break;
4031 }
4032 if (!test_range_bit(tree, em->start,
4033 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04004034 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04004035 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05004036 remove_extent_mapping(map, em);
4037 /* once for the rb tree */
4038 free_extent_map(em);
4039 }
4040 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04004041 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05004042
4043 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05004044 free_extent_map(em);
4045 }
Chris Masond1310b22008-01-24 16:13:08 -05004046 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04004047 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05004048}
Chris Masond1310b22008-01-24 16:13:08 -05004049
Chris Masonec29ed52011-02-23 16:23:20 -05004050/*
4051 * helper function for fiemap, which doesn't want to see any holes.
4052 * This maps until we find something past 'last'
4053 */
4054static struct extent_map *get_extent_skip_holes(struct inode *inode,
4055 u64 offset,
4056 u64 last,
4057 get_extent_t *get_extent)
4058{
4059 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
4060 struct extent_map *em;
4061 u64 len;
4062
4063 if (offset >= last)
4064 return NULL;
4065
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05304066 while (1) {
Chris Masonec29ed52011-02-23 16:23:20 -05004067 len = last - offset;
4068 if (len == 0)
4069 break;
Qu Wenruofda28322013-02-26 08:10:22 +00004070 len = ALIGN(len, sectorsize);
Chris Masonec29ed52011-02-23 16:23:20 -05004071 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02004072 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05004073 return em;
4074
4075 /* if this isn't a hole return it */
4076 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
4077 em->block_start != EXTENT_MAP_HOLE) {
4078 return em;
4079 }
4080
4081 /* this is a hole, advance to the next extent */
4082 offset = extent_map_end(em);
4083 free_extent_map(em);
4084 if (offset >= last)
4085 break;
4086 }
4087 return NULL;
4088}
4089
Liu Bofe09e162013-09-22 12:54:23 +08004090static noinline int count_ext_ref(u64 inum, u64 offset, u64 root_id, void *ctx)
4091{
4092 unsigned long cnt = *((unsigned long *)ctx);
4093
4094 cnt++;
4095 *((unsigned long *)ctx) = cnt;
4096
4097 /* Now we're sure that the extent is shared. */
4098 if (cnt > 1)
4099 return 1;
4100 return 0;
4101}
4102
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004103int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4104 __u64 start, __u64 len, get_extent_t *get_extent)
4105{
Josef Bacik975f84f2010-11-23 19:36:57 +00004106 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004107 u64 off = start;
4108 u64 max = start + len;
4109 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00004110 u32 found_type;
4111 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05004112 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004113 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004114 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00004115 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004116 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004117 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00004118 struct btrfs_path *path;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004119 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05004120 u64 em_start = 0;
4121 u64 em_len = 0;
4122 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004123
4124 if (len == 0)
4125 return -EINVAL;
4126
Josef Bacik975f84f2010-11-23 19:36:57 +00004127 path = btrfs_alloc_path();
4128 if (!path)
4129 return -ENOMEM;
4130 path->leave_spinning = 1;
4131
Josef Bacik4d479cf2011-11-17 11:34:31 -05004132 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
4133 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
4134
Chris Masonec29ed52011-02-23 16:23:20 -05004135 /*
4136 * lookup the last file extent. We're not using i_size here
4137 * because there might be preallocation past i_size
4138 */
Josef Bacik975f84f2010-11-23 19:36:57 +00004139 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
Li Zefan33345d012011-04-20 10:31:50 +08004140 path, btrfs_ino(inode), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00004141 if (ret < 0) {
4142 btrfs_free_path(path);
4143 return ret;
4144 }
4145 WARN_ON(!ret);
4146 path->slots[0]--;
Josef Bacik975f84f2010-11-23 19:36:57 +00004147 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
4148 found_type = btrfs_key_type(&found_key);
4149
Chris Masonec29ed52011-02-23 16:23:20 -05004150 /* No extents, but there might be delalloc bits */
Li Zefan33345d012011-04-20 10:31:50 +08004151 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00004152 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05004153 /* have to trust i_size as the end */
4154 last = (u64)-1;
4155 last_for_get_extent = isize;
4156 } else {
4157 /*
4158 * remember the start of the last extent. There are a
4159 * bunch of different factors that go into the length of the
4160 * extent, so its much less complex to remember where it started
4161 */
4162 last = found_key.offset;
4163 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00004164 }
Liu Bofe09e162013-09-22 12:54:23 +08004165 btrfs_release_path(path);
Josef Bacik975f84f2010-11-23 19:36:57 +00004166
Chris Masonec29ed52011-02-23 16:23:20 -05004167 /*
4168 * we might have some extents allocated but more delalloc past those
4169 * extents. so, we trust isize unless the start of the last extent is
4170 * beyond isize
4171 */
4172 if (last < isize) {
4173 last = (u64)-1;
4174 last_for_get_extent = isize;
4175 }
4176
Liu Boa52f4cd2013-05-01 16:23:41 +00004177 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len - 1, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01004178 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05004179
Josef Bacik4d479cf2011-11-17 11:34:31 -05004180 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05004181 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004182 if (!em)
4183 goto out;
4184 if (IS_ERR(em)) {
4185 ret = PTR_ERR(em);
4186 goto out;
4187 }
Josef Bacik975f84f2010-11-23 19:36:57 +00004188
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004189 while (!end) {
Josef Bacikb76bb702013-07-05 13:52:51 -04004190 u64 offset_in_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004191
Chris Masonea8efc72011-03-08 11:54:40 -05004192 /* break if the extent we found is outside the range */
4193 if (em->start >= max || extent_map_end(em) < off)
4194 break;
4195
4196 /*
4197 * get_extent may return an extent that starts before our
4198 * requested range. We have to make sure the ranges
4199 * we return to fiemap always move forward and don't
4200 * overlap, so adjust the offsets here
4201 */
4202 em_start = max(em->start, off);
4203
4204 /*
4205 * record the offset from the start of the extent
Josef Bacikb76bb702013-07-05 13:52:51 -04004206 * for adjusting the disk offset below. Only do this if the
4207 * extent isn't compressed since our in ram offset may be past
4208 * what we have actually allocated on disk.
Chris Masonea8efc72011-03-08 11:54:40 -05004209 */
Josef Bacikb76bb702013-07-05 13:52:51 -04004210 if (!test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4211 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05004212 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05004213 em_len = em_end - em_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004214 disko = 0;
4215 flags = 0;
4216
Chris Masonea8efc72011-03-08 11:54:40 -05004217 /*
4218 * bump off for our next call to get_extent
4219 */
4220 off = extent_map_end(em);
4221 if (off >= max)
4222 end = 1;
4223
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004224 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004225 end = 1;
4226 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004227 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004228 flags |= (FIEMAP_EXTENT_DATA_INLINE |
4229 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004230 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004231 flags |= (FIEMAP_EXTENT_DELALLOC |
4232 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04004233 } else {
Liu Bofe09e162013-09-22 12:54:23 +08004234 unsigned long ref_cnt = 0;
4235
Chris Masonea8efc72011-03-08 11:54:40 -05004236 disko = em->block_start + offset_in_extent;
Liu Bofe09e162013-09-22 12:54:23 +08004237
4238 /*
4239 * As btrfs supports shared space, this information
4240 * can be exported to userspace tools via
4241 * flag FIEMAP_EXTENT_SHARED.
4242 */
4243 ret = iterate_inodes_from_logical(
4244 em->block_start,
4245 BTRFS_I(inode)->root->fs_info,
4246 path, count_ext_ref, &ref_cnt);
4247 if (ret < 0 && ret != -ENOENT)
4248 goto out_free;
4249
4250 if (ref_cnt > 1)
4251 flags |= FIEMAP_EXTENT_SHARED;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004252 }
4253 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
4254 flags |= FIEMAP_EXTENT_ENCODED;
4255
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004256 free_extent_map(em);
4257 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05004258 if ((em_start >= last) || em_len == (u64)-1 ||
4259 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004260 flags |= FIEMAP_EXTENT_LAST;
4261 end = 1;
4262 }
4263
Chris Masonec29ed52011-02-23 16:23:20 -05004264 /* now scan forward to see if this is really the last extent. */
4265 em = get_extent_skip_holes(inode, off, last_for_get_extent,
4266 get_extent);
4267 if (IS_ERR(em)) {
4268 ret = PTR_ERR(em);
4269 goto out;
4270 }
4271 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00004272 flags |= FIEMAP_EXTENT_LAST;
4273 end = 1;
4274 }
Chris Masonec29ed52011-02-23 16:23:20 -05004275 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
4276 em_len, flags);
4277 if (ret)
4278 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004279 }
4280out_free:
4281 free_extent_map(em);
4282out:
Liu Bofe09e162013-09-22 12:54:23 +08004283 btrfs_free_path(path);
Liu Boa52f4cd2013-05-01 16:23:41 +00004284 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00004285 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05004286 return ret;
4287}
4288
Chris Mason727011e2010-08-06 13:21:20 -04004289static void __free_extent_buffer(struct extent_buffer *eb)
4290{
Eric Sandeen6d49ba12013-04-22 16:12:31 +00004291 btrfs_leak_debug_del(&eb->leak_list);
Chris Mason727011e2010-08-06 13:21:20 -04004292 kmem_cache_free(extent_buffer_cache, eb);
4293}
4294
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004295static int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004296{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004297 return (atomic_read(&eb->io_pages) ||
4298 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4299 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004300}
4301
Miao Xie897ca6e92010-10-26 20:57:29 -04004302/*
4303 * Helper for releasing extent buffer page.
4304 */
4305static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
4306 unsigned long start_idx)
4307{
4308 unsigned long index;
Wang Sheng-Hui39bab872012-04-06 14:35:31 +08004309 unsigned long num_pages;
Miao Xie897ca6e92010-10-26 20:57:29 -04004310 struct page *page;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004311 int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
Miao Xie897ca6e92010-10-26 20:57:29 -04004312
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004313 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e92010-10-26 20:57:29 -04004314
Wang Sheng-Hui39bab872012-04-06 14:35:31 +08004315 num_pages = num_extent_pages(eb->start, eb->len);
4316 index = start_idx + num_pages;
Miao Xie897ca6e92010-10-26 20:57:29 -04004317 if (start_idx >= index)
4318 return;
4319
4320 do {
4321 index--;
4322 page = extent_buffer_page(eb, index);
Jan Schmidt815a51c2012-05-16 17:00:02 +02004323 if (page && mapped) {
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004324 spin_lock(&page->mapping->private_lock);
4325 /*
4326 * We do this since we'll remove the pages after we've
4327 * removed the eb from the radix tree, so we could race
4328 * and have this page now attached to the new eb. So
4329 * only clear page_private if it's still connected to
4330 * this eb.
4331 */
4332 if (PagePrivate(page) &&
4333 page->private == (unsigned long)eb) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004334 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Josef Bacik3083ee22012-03-09 16:01:49 -05004335 BUG_ON(PageDirty(page));
4336 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004337 /*
4338 * We need to make sure we haven't be attached
4339 * to a new eb.
4340 */
4341 ClearPagePrivate(page);
4342 set_page_private(page, 0);
4343 /* One for the page private */
4344 page_cache_release(page);
4345 }
4346 spin_unlock(&page->mapping->private_lock);
4347
Jan Schmidt815a51c2012-05-16 17:00:02 +02004348 }
4349 if (page) {
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004350 /* One for when we alloced the page */
Miao Xie897ca6e92010-10-26 20:57:29 -04004351 page_cache_release(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004352 }
Miao Xie897ca6e92010-10-26 20:57:29 -04004353 } while (index != start_idx);
4354}
4355
4356/*
4357 * Helper for releasing the extent buffer.
4358 */
4359static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4360{
4361 btrfs_release_extent_buffer_page(eb, 0);
4362 __free_extent_buffer(eb);
4363}
4364
Josef Bacikdb7f3432013-08-07 14:54:37 -04004365static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
4366 u64 start,
4367 unsigned long len,
4368 gfp_t mask)
4369{
4370 struct extent_buffer *eb = NULL;
4371
4372 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
4373 if (eb == NULL)
4374 return NULL;
4375 eb->start = start;
4376 eb->len = len;
4377 eb->tree = tree;
4378 eb->bflags = 0;
4379 rwlock_init(&eb->lock);
4380 atomic_set(&eb->write_locks, 0);
4381 atomic_set(&eb->read_locks, 0);
4382 atomic_set(&eb->blocking_readers, 0);
4383 atomic_set(&eb->blocking_writers, 0);
4384 atomic_set(&eb->spinning_readers, 0);
4385 atomic_set(&eb->spinning_writers, 0);
4386 eb->lock_nested = 0;
4387 init_waitqueue_head(&eb->write_lock_wq);
4388 init_waitqueue_head(&eb->read_lock_wq);
4389
4390 btrfs_leak_debug_add(&eb->leak_list, &buffers);
4391
4392 spin_lock_init(&eb->refs_lock);
4393 atomic_set(&eb->refs, 1);
4394 atomic_set(&eb->io_pages, 0);
4395
4396 /*
4397 * Sanity checks, currently the maximum is 64k covered by 16x 4k pages
4398 */
4399 BUILD_BUG_ON(BTRFS_MAX_METADATA_BLOCKSIZE
4400 > MAX_INLINE_EXTENT_BUFFER_SIZE);
4401 BUG_ON(len > MAX_INLINE_EXTENT_BUFFER_SIZE);
4402
4403 return eb;
4404}
4405
4406struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
4407{
4408 unsigned long i;
4409 struct page *p;
4410 struct extent_buffer *new;
4411 unsigned long num_pages = num_extent_pages(src->start, src->len);
4412
Josef Bacik9ec72672013-08-07 16:57:23 -04004413 new = __alloc_extent_buffer(NULL, src->start, src->len, GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004414 if (new == NULL)
4415 return NULL;
4416
4417 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004418 p = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004419 if (!p) {
4420 btrfs_release_extent_buffer(new);
4421 return NULL;
4422 }
4423 attach_extent_buffer_page(new, p);
4424 WARN_ON(PageDirty(p));
4425 SetPageUptodate(p);
4426 new->pages[i] = p;
4427 }
4428
4429 copy_extent_buffer(new, src, 0, 0, src->len);
4430 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
4431 set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
4432
4433 return new;
4434}
4435
4436struct extent_buffer *alloc_dummy_extent_buffer(u64 start, unsigned long len)
4437{
4438 struct extent_buffer *eb;
4439 unsigned long num_pages = num_extent_pages(0, len);
4440 unsigned long i;
4441
Josef Bacik9ec72672013-08-07 16:57:23 -04004442 eb = __alloc_extent_buffer(NULL, start, len, GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004443 if (!eb)
4444 return NULL;
4445
4446 for (i = 0; i < num_pages; i++) {
Josef Bacik9ec72672013-08-07 16:57:23 -04004447 eb->pages[i] = alloc_page(GFP_NOFS);
Josef Bacikdb7f3432013-08-07 14:54:37 -04004448 if (!eb->pages[i])
4449 goto err;
4450 }
4451 set_extent_buffer_uptodate(eb);
4452 btrfs_set_header_nritems(eb, 0);
4453 set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4454
4455 return eb;
4456err:
4457 for (; i > 0; i--)
4458 __free_page(eb->pages[i - 1]);
4459 __free_extent_buffer(eb);
4460 return NULL;
4461}
4462
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004463static void check_buffer_tree_ref(struct extent_buffer *eb)
4464{
Chris Mason242e18c2013-01-29 17:49:37 -05004465 int refs;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004466 /* the ref bit is tricky. We have to make sure it is set
4467 * if we have the buffer dirty. Otherwise the
4468 * code to free a buffer can end up dropping a dirty
4469 * page
4470 *
4471 * Once the ref bit is set, it won't go away while the
4472 * buffer is dirty or in writeback, and it also won't
4473 * go away while we have the reference count on the
4474 * eb bumped.
4475 *
4476 * We can't just set the ref bit without bumping the
4477 * ref on the eb because free_extent_buffer might
4478 * see the ref bit and try to clear it. If this happens
4479 * free_extent_buffer might end up dropping our original
4480 * ref by mistake and freeing the page before we are able
4481 * to add one more ref.
4482 *
4483 * So bump the ref count first, then set the bit. If someone
4484 * beat us to it, drop the ref we added.
4485 */
Chris Mason242e18c2013-01-29 17:49:37 -05004486 refs = atomic_read(&eb->refs);
4487 if (refs >= 2 && test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4488 return;
4489
Josef Bacik594831c2012-07-20 16:11:08 -04004490 spin_lock(&eb->refs_lock);
4491 if (!test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004492 atomic_inc(&eb->refs);
Josef Bacik594831c2012-07-20 16:11:08 -04004493 spin_unlock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004494}
4495
Josef Bacik5df42352012-03-15 18:24:42 -04004496static void mark_extent_buffer_accessed(struct extent_buffer *eb)
4497{
4498 unsigned long num_pages, i;
4499
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004500 check_buffer_tree_ref(eb);
4501
Josef Bacik5df42352012-03-15 18:24:42 -04004502 num_pages = num_extent_pages(eb->start, eb->len);
4503 for (i = 0; i < num_pages; i++) {
4504 struct page *p = extent_buffer_page(eb, i);
4505 mark_page_accessed(p);
4506 }
4507}
4508
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004509struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
4510 u64 start)
4511{
4512 struct extent_buffer *eb;
4513
4514 rcu_read_lock();
4515 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4516 if (eb && atomic_inc_not_zero(&eb->refs)) {
4517 rcu_read_unlock();
4518 mark_extent_buffer_accessed(eb);
4519 return eb;
4520 }
4521 rcu_read_unlock();
4522
4523 return NULL;
4524}
4525
Chris Masond1310b22008-01-24 16:13:08 -05004526struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
Chris Mason727011e2010-08-06 13:21:20 -04004527 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004528{
4529 unsigned long num_pages = num_extent_pages(start, len);
4530 unsigned long i;
4531 unsigned long index = start >> PAGE_CACHE_SHIFT;
4532 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004533 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004534 struct page *p;
4535 struct address_space *mapping = tree->mapping;
4536 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004537 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004538
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004539
4540 eb = find_extent_buffer(tree, start);
4541 if (eb)
Chris Mason6af118ce2008-07-22 11:18:07 -04004542 return eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004543
David Sterbaba144192011-04-21 01:12:06 +02004544 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
Peter2b114d12008-04-01 11:21:40 -04004545 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004546 return NULL;
4547
Chris Mason727011e2010-08-06 13:21:20 -04004548 for (i = 0; i < num_pages; i++, index++) {
Chris Masona6591712011-07-19 12:04:14 -04004549 p = find_or_create_page(mapping, index, GFP_NOFS);
Josef Bacik4804b382012-10-05 16:43:45 -04004550 if (!p)
Chris Mason6af118ce2008-07-22 11:18:07 -04004551 goto free_eb;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004552
4553 spin_lock(&mapping->private_lock);
4554 if (PagePrivate(p)) {
4555 /*
4556 * We could have already allocated an eb for this page
4557 * and attached one so lets see if we can get a ref on
4558 * the existing eb, and if we can we know it's good and
4559 * we can just return that one, else we know we can just
4560 * overwrite page->private.
4561 */
4562 exists = (struct extent_buffer *)p->private;
4563 if (atomic_inc_not_zero(&exists->refs)) {
4564 spin_unlock(&mapping->private_lock);
4565 unlock_page(p);
Josef Bacik17de39a2012-05-04 15:16:06 -04004566 page_cache_release(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004567 mark_extent_buffer_accessed(exists);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004568 goto free_eb;
4569 }
4570
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004571 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004572 * Do this so attach doesn't complain and we need to
4573 * drop the ref the old guy had.
4574 */
4575 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004576 WARN_ON(PageDirty(p));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004577 page_cache_release(p);
Chris Masond1310b22008-01-24 16:13:08 -05004578 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004579 attach_extent_buffer_page(eb, p);
4580 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004581 WARN_ON(PageDirty(p));
Chris Masond1310b22008-01-24 16:13:08 -05004582 mark_page_accessed(p);
Chris Mason727011e2010-08-06 13:21:20 -04004583 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05004584 if (!PageUptodate(p))
4585 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05004586
4587 /*
4588 * see below about how we avoid a nasty race with release page
4589 * and why we unlock later
4590 */
Chris Masond1310b22008-01-24 16:13:08 -05004591 }
4592 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004593 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05004594again:
Miao Xie19fe0a82010-10-26 20:57:29 -04004595 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4596 if (ret)
4597 goto free_eb;
4598
Chris Mason6af118ce2008-07-22 11:18:07 -04004599 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004600 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004601 spin_unlock(&tree->buffer_lock);
4602 radix_tree_preload_end();
Miao Xie19fe0a82010-10-26 20:57:29 -04004603 if (ret == -EEXIST) {
Chandra Seetharaman452c75c2013-10-07 10:45:25 -05004604 exists = find_extent_buffer(tree, start);
4605 if (exists)
4606 goto free_eb;
4607 else
Josef Bacik115391d2012-03-09 09:51:43 -05004608 goto again;
Chris Mason6af118ce2008-07-22 11:18:07 -04004609 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004610 /* add one reference for the tree */
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004611 check_buffer_tree_ref(eb);
Chris Masoneb14ab82011-02-10 12:35:00 -05004612
4613 /*
4614 * there is a race where release page may have
4615 * tried to find this extent buffer in the radix
4616 * but failed. It will tell the VM it is safe to
4617 * reclaim the, and it will clear the page private bit.
4618 * We must make sure to set the page private bit properly
4619 * after the extent buffer is in the radix tree so
4620 * it doesn't get lost
4621 */
Chris Mason727011e2010-08-06 13:21:20 -04004622 SetPageChecked(eb->pages[0]);
4623 for (i = 1; i < num_pages; i++) {
4624 p = extent_buffer_page(eb, i);
Chris Mason727011e2010-08-06 13:21:20 -04004625 ClearPageChecked(p);
4626 unlock_page(p);
4627 }
4628 unlock_page(eb->pages[0]);
Chris Masond1310b22008-01-24 16:13:08 -05004629 return eb;
4630
Chris Mason6af118ce2008-07-22 11:18:07 -04004631free_eb:
Chris Mason727011e2010-08-06 13:21:20 -04004632 for (i = 0; i < num_pages; i++) {
4633 if (eb->pages[i])
4634 unlock_page(eb->pages[i]);
4635 }
Chris Masoneb14ab82011-02-10 12:35:00 -05004636
Josef Bacik17de39a2012-05-04 15:16:06 -04004637 WARN_ON(!atomic_dec_and_test(&eb->refs));
Miao Xie897ca6e92010-10-26 20:57:29 -04004638 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04004639 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05004640}
Chris Masond1310b22008-01-24 16:13:08 -05004641
Josef Bacik3083ee22012-03-09 16:01:49 -05004642static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4643{
4644 struct extent_buffer *eb =
4645 container_of(head, struct extent_buffer, rcu_head);
4646
4647 __free_extent_buffer(eb);
4648}
4649
Josef Bacik3083ee22012-03-09 16:01:49 -05004650/* Expects to have eb->eb_lock already held */
David Sterbaf7a52a42013-04-26 14:56:29 +00004651static int release_extent_buffer(struct extent_buffer *eb)
Josef Bacik3083ee22012-03-09 16:01:49 -05004652{
4653 WARN_ON(atomic_read(&eb->refs) == 0);
4654 if (atomic_dec_and_test(&eb->refs)) {
Jan Schmidt815a51c2012-05-16 17:00:02 +02004655 if (test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) {
4656 spin_unlock(&eb->refs_lock);
4657 } else {
4658 struct extent_io_tree *tree = eb->tree;
Josef Bacik3083ee22012-03-09 16:01:49 -05004659
Jan Schmidt815a51c2012-05-16 17:00:02 +02004660 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05004661
Jan Schmidt815a51c2012-05-16 17:00:02 +02004662 spin_lock(&tree->buffer_lock);
4663 radix_tree_delete(&tree->buffer,
4664 eb->start >> PAGE_CACHE_SHIFT);
4665 spin_unlock(&tree->buffer_lock);
4666 }
Josef Bacik3083ee22012-03-09 16:01:49 -05004667
4668 /* Should be safe to release our pages at this point */
4669 btrfs_release_extent_buffer_page(eb, 0);
Josef Bacik3083ee22012-03-09 16:01:49 -05004670 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
Josef Bacike64860a2012-07-20 16:05:36 -04004671 return 1;
Josef Bacik3083ee22012-03-09 16:01:49 -05004672 }
4673 spin_unlock(&eb->refs_lock);
Josef Bacike64860a2012-07-20 16:05:36 -04004674
4675 return 0;
Josef Bacik3083ee22012-03-09 16:01:49 -05004676}
4677
Chris Masond1310b22008-01-24 16:13:08 -05004678void free_extent_buffer(struct extent_buffer *eb)
4679{
Chris Mason242e18c2013-01-29 17:49:37 -05004680 int refs;
4681 int old;
Chris Masond1310b22008-01-24 16:13:08 -05004682 if (!eb)
4683 return;
4684
Chris Mason242e18c2013-01-29 17:49:37 -05004685 while (1) {
4686 refs = atomic_read(&eb->refs);
4687 if (refs <= 3)
4688 break;
4689 old = atomic_cmpxchg(&eb->refs, refs, refs - 1);
4690 if (old == refs)
4691 return;
4692 }
4693
Josef Bacik3083ee22012-03-09 16:01:49 -05004694 spin_lock(&eb->refs_lock);
4695 if (atomic_read(&eb->refs) == 2 &&
Jan Schmidt815a51c2012-05-16 17:00:02 +02004696 test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
4697 atomic_dec(&eb->refs);
4698
4699 if (atomic_read(&eb->refs) == 2 &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004700 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004701 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004702 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4703 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05004704
Josef Bacik3083ee22012-03-09 16:01:49 -05004705 /*
4706 * I know this is terrible, but it's temporary until we stop tracking
4707 * the uptodate bits and such for the extent buffers.
4708 */
David Sterbaf7a52a42013-04-26 14:56:29 +00004709 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05004710}
Chris Masond1310b22008-01-24 16:13:08 -05004711
Josef Bacik3083ee22012-03-09 16:01:49 -05004712void free_extent_buffer_stale(struct extent_buffer *eb)
4713{
4714 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004715 return;
4716
Josef Bacik3083ee22012-03-09 16:01:49 -05004717 spin_lock(&eb->refs_lock);
4718 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
4719
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004720 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004721 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4722 atomic_dec(&eb->refs);
David Sterbaf7a52a42013-04-26 14:56:29 +00004723 release_extent_buffer(eb);
Chris Masond1310b22008-01-24 16:13:08 -05004724}
4725
Chris Mason1d4284b2012-03-28 20:31:37 -04004726void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004727{
Chris Masond1310b22008-01-24 16:13:08 -05004728 unsigned long i;
4729 unsigned long num_pages;
4730 struct page *page;
4731
Chris Masond1310b22008-01-24 16:13:08 -05004732 num_pages = num_extent_pages(eb->start, eb->len);
4733
4734 for (i = 0; i < num_pages; i++) {
4735 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04004736 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05004737 continue;
4738
Chris Masona61e6f22008-07-22 11:18:08 -04004739 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05004740 WARN_ON(!PagePrivate(page));
4741
Chris Masond1310b22008-01-24 16:13:08 -05004742 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004743 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05004744 if (!PageDirty(page)) {
4745 radix_tree_tag_clear(&page->mapping->page_tree,
4746 page_index(page),
4747 PAGECACHE_TAG_DIRTY);
4748 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004749 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04004750 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04004751 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05004752 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004753 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05004754}
Chris Masond1310b22008-01-24 16:13:08 -05004755
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004756int set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004757{
4758 unsigned long i;
4759 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04004760 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004761
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004762 check_buffer_tree_ref(eb);
4763
Chris Masonb9473432009-03-13 11:00:37 -04004764 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004765
Chris Masond1310b22008-01-24 16:13:08 -05004766 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik3083ee22012-03-09 16:01:49 -05004767 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004768 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
4769
Chris Masonb9473432009-03-13 11:00:37 -04004770 for (i = 0; i < num_pages; i++)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004771 set_page_dirty(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04004772 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05004773}
Chris Masond1310b22008-01-24 16:13:08 -05004774
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004775int clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04004776{
4777 unsigned long i;
4778 struct page *page;
4779 unsigned long num_pages;
4780
Chris Masonb4ce94d2009-02-04 09:25:08 -05004781 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004782 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason1259ab72008-05-12 13:39:03 -04004783 for (i = 0; i < num_pages; i++) {
4784 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04004785 if (page)
4786 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04004787 }
4788 return 0;
4789}
4790
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004791int set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004792{
4793 unsigned long i;
4794 struct page *page;
4795 unsigned long num_pages;
4796
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004797 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004798 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05004799 for (i = 0; i < num_pages; i++) {
4800 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004801 SetPageUptodate(page);
4802 }
4803 return 0;
4804}
Chris Masond1310b22008-01-24 16:13:08 -05004805
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004806int extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004807{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004808 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004809}
Chris Masond1310b22008-01-24 16:13:08 -05004810
4811int read_extent_buffer_pages(struct extent_io_tree *tree,
Arne Jansenbb82ab82011-06-10 14:06:53 +02004812 struct extent_buffer *eb, u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04004813 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05004814{
4815 unsigned long i;
4816 unsigned long start_i;
4817 struct page *page;
4818 int err;
4819 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04004820 int locked_pages = 0;
4821 int all_uptodate = 1;
Chris Masond1310b22008-01-24 16:13:08 -05004822 unsigned long num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04004823 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004824 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004825 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004826
Chris Masonb4ce94d2009-02-04 09:25:08 -05004827 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05004828 return 0;
4829
Chris Masond1310b22008-01-24 16:13:08 -05004830 if (start) {
4831 WARN_ON(start < eb->start);
4832 start_i = (start >> PAGE_CACHE_SHIFT) -
4833 (eb->start >> PAGE_CACHE_SHIFT);
4834 } else {
4835 start_i = 0;
4836 }
4837
4838 num_pages = num_extent_pages(eb->start, eb->len);
4839 for (i = start_i; i < num_pages; i++) {
4840 page = extent_buffer_page(eb, i);
Arne Jansenbb82ab82011-06-10 14:06:53 +02004841 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04004842 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04004843 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05004844 } else {
4845 lock_page(page);
4846 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004847 locked_pages++;
Chris Mason727011e2010-08-06 13:21:20 -04004848 if (!PageUptodate(page)) {
4849 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04004850 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04004851 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004852 }
4853 if (all_uptodate) {
4854 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004855 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04004856 goto unlock_exit;
4857 }
4858
Josef Bacikea466792012-03-26 21:57:36 -04004859 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04004860 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004861 atomic_set(&eb->io_pages, num_reads);
Chris Masonce9adaa2008-04-09 16:28:12 -04004862 for (i = start_i; i < num_pages; i++) {
4863 page = extent_buffer_page(eb, i);
Chris Masonce9adaa2008-04-09 16:28:12 -04004864 if (!PageUptodate(page)) {
Chris Masonf1885912008-04-09 16:28:12 -04004865 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05004866 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04004867 get_extent, &bio,
Josef Bacikd4c7ca82013-04-19 19:49:09 -04004868 mirror_num, &bio_flags,
4869 READ | REQ_META);
Chris Masond3977122009-01-05 21:25:51 -05004870 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05004871 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05004872 } else {
4873 unlock_page(page);
4874 }
4875 }
4876
Jeff Mahoney355808c2011-10-03 23:23:14 -04004877 if (bio) {
Josef Bacikd4c7ca82013-04-19 19:49:09 -04004878 err = submit_one_bio(READ | REQ_META, bio, mirror_num,
4879 bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004880 if (err)
4881 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04004882 }
Chris Masona86c12c2008-02-07 10:50:54 -05004883
Arne Jansenbb82ab82011-06-10 14:06:53 +02004884 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05004885 return ret;
Chris Masond3977122009-01-05 21:25:51 -05004886
Chris Masond1310b22008-01-24 16:13:08 -05004887 for (i = start_i; i < num_pages; i++) {
4888 page = extent_buffer_page(eb, i);
4889 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05004890 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05004891 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05004892 }
Chris Masond3977122009-01-05 21:25:51 -05004893
Chris Masond1310b22008-01-24 16:13:08 -05004894 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04004895
4896unlock_exit:
4897 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05004898 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004899 page = extent_buffer_page(eb, i);
4900 i++;
4901 unlock_page(page);
4902 locked_pages--;
4903 }
4904 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004905}
Chris Masond1310b22008-01-24 16:13:08 -05004906
4907void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4908 unsigned long start,
4909 unsigned long len)
4910{
4911 size_t cur;
4912 size_t offset;
4913 struct page *page;
4914 char *kaddr;
4915 char *dst = (char *)dstv;
4916 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4917 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004918
4919 WARN_ON(start > eb->len);
4920 WARN_ON(start + len > eb->start + eb->len);
4921
Geert Uytterhoeven778746b2013-08-20 13:20:16 +02004922 offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05004923
Chris Masond3977122009-01-05 21:25:51 -05004924 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004925 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004926
4927 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04004928 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004929 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004930
4931 dst += cur;
4932 len -= cur;
4933 offset = 0;
4934 i++;
4935 }
4936}
Chris Masond1310b22008-01-24 16:13:08 -05004937
4938int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04004939 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05004940 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04004941 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05004942{
4943 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4944 char *kaddr;
4945 struct page *p;
4946 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4947 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4948 unsigned long end_i = (start_offset + start + min_len - 1) >>
4949 PAGE_CACHE_SHIFT;
4950
4951 if (i != end_i)
4952 return -EINVAL;
4953
4954 if (i == 0) {
4955 offset = start_offset;
4956 *map_start = 0;
4957 } else {
4958 offset = 0;
4959 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4960 }
Chris Masond3977122009-01-05 21:25:51 -05004961
Chris Masond1310b22008-01-24 16:13:08 -05004962 if (start + min_len > eb->len) {
Julia Lawall31b1a2b2012-11-03 10:58:34 +00004963 WARN(1, KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02004964 "wanted %lu %lu\n",
4965 eb->start, eb->len, start, min_len);
Josef Bacik850265332011-03-15 14:52:12 -04004966 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05004967 }
4968
4969 p = extent_buffer_page(eb, i);
Chris Masona6591712011-07-19 12:04:14 -04004970 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05004971 *map = kaddr + offset;
4972 *map_len = PAGE_CACHE_SIZE - offset;
4973 return 0;
4974}
Chris Masond1310b22008-01-24 16:13:08 -05004975
Chris Masond1310b22008-01-24 16:13:08 -05004976int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4977 unsigned long start,
4978 unsigned long len)
4979{
4980 size_t cur;
4981 size_t offset;
4982 struct page *page;
4983 char *kaddr;
4984 char *ptr = (char *)ptrv;
4985 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4986 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4987 int ret = 0;
4988
4989 WARN_ON(start > eb->len);
4990 WARN_ON(start + len > eb->start + eb->len);
4991
Geert Uytterhoeven778746b2013-08-20 13:20:16 +02004992 offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05004993
Chris Masond3977122009-01-05 21:25:51 -05004994 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004995 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004996
4997 cur = min(len, (PAGE_CACHE_SIZE - offset));
4998
Chris Masona6591712011-07-19 12:04:14 -04004999 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005000 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005001 if (ret)
5002 break;
5003
5004 ptr += cur;
5005 len -= cur;
5006 offset = 0;
5007 i++;
5008 }
5009 return ret;
5010}
Chris Masond1310b22008-01-24 16:13:08 -05005011
5012void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
5013 unsigned long start, unsigned long len)
5014{
5015 size_t cur;
5016 size_t offset;
5017 struct page *page;
5018 char *kaddr;
5019 char *src = (char *)srcv;
5020 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
5021 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
5022
5023 WARN_ON(start > eb->len);
5024 WARN_ON(start + len > eb->start + eb->len);
5025
Geert Uytterhoeven778746b2013-08-20 13:20:16 +02005026 offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005027
Chris Masond3977122009-01-05 21:25:51 -05005028 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05005029 page = extent_buffer_page(eb, i);
5030 WARN_ON(!PageUptodate(page));
5031
5032 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005033 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005034 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005035
5036 src += cur;
5037 len -= cur;
5038 offset = 0;
5039 i++;
5040 }
5041}
Chris Masond1310b22008-01-24 16:13:08 -05005042
5043void memset_extent_buffer(struct extent_buffer *eb, char c,
5044 unsigned long start, unsigned long len)
5045{
5046 size_t cur;
5047 size_t offset;
5048 struct page *page;
5049 char *kaddr;
5050 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
5051 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
5052
5053 WARN_ON(start > eb->len);
5054 WARN_ON(start + len > eb->start + eb->len);
5055
Geert Uytterhoeven778746b2013-08-20 13:20:16 +02005056 offset = (start_offset + start) & (PAGE_CACHE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005057
Chris Masond3977122009-01-05 21:25:51 -05005058 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05005059 page = extent_buffer_page(eb, i);
5060 WARN_ON(!PageUptodate(page));
5061
5062 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04005063 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005064 memset(kaddr + offset, c, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005065
5066 len -= cur;
5067 offset = 0;
5068 i++;
5069 }
5070}
Chris Masond1310b22008-01-24 16:13:08 -05005071
5072void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
5073 unsigned long dst_offset, unsigned long src_offset,
5074 unsigned long len)
5075{
5076 u64 dst_len = dst->len;
5077 size_t cur;
5078 size_t offset;
5079 struct page *page;
5080 char *kaddr;
5081 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
5082 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
5083
5084 WARN_ON(src->len != dst_len);
5085
5086 offset = (start_offset + dst_offset) &
Geert Uytterhoeven778746b2013-08-20 13:20:16 +02005087 (PAGE_CACHE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005088
Chris Masond3977122009-01-05 21:25:51 -05005089 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05005090 page = extent_buffer_page(dst, i);
5091 WARN_ON(!PageUptodate(page));
5092
5093 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
5094
Chris Masona6591712011-07-19 12:04:14 -04005095 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05005096 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05005097
5098 src_offset += cur;
5099 len -= cur;
5100 offset = 0;
5101 i++;
5102 }
5103}
Chris Masond1310b22008-01-24 16:13:08 -05005104
Sergei Trofimovich33872062011-04-11 21:52:52 +00005105static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
5106{
5107 unsigned long distance = (src > dst) ? src - dst : dst - src;
5108 return distance < len;
5109}
5110
Chris Masond1310b22008-01-24 16:13:08 -05005111static void copy_pages(struct page *dst_page, struct page *src_page,
5112 unsigned long dst_off, unsigned long src_off,
5113 unsigned long len)
5114{
Chris Masona6591712011-07-19 12:04:14 -04005115 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05005116 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005117 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05005118
Sergei Trofimovich33872062011-04-11 21:52:52 +00005119 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04005120 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00005121 } else {
Chris Masond1310b22008-01-24 16:13:08 -05005122 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04005123 if (areas_overlap(src_off, dst_off, len))
5124 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00005125 }
Chris Masond1310b22008-01-24 16:13:08 -05005126
Chris Mason727011e2010-08-06 13:21:20 -04005127 if (must_memmove)
5128 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
5129 else
5130 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05005131}
5132
5133void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5134 unsigned long src_offset, unsigned long len)
5135{
5136 size_t cur;
5137 size_t dst_off_in_page;
5138 size_t src_off_in_page;
5139 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
5140 unsigned long dst_i;
5141 unsigned long src_i;
5142
5143 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05005144 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
5145 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005146 BUG_ON(1);
5147 }
5148 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05005149 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
5150 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005151 BUG_ON(1);
5152 }
5153
Chris Masond3977122009-01-05 21:25:51 -05005154 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05005155 dst_off_in_page = (start_offset + dst_offset) &
Geert Uytterhoeven778746b2013-08-20 13:20:16 +02005156 (PAGE_CACHE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005157 src_off_in_page = (start_offset + src_offset) &
Geert Uytterhoeven778746b2013-08-20 13:20:16 +02005158 (PAGE_CACHE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005159
5160 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
5161 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
5162
5163 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
5164 src_off_in_page));
5165 cur = min_t(unsigned long, cur,
5166 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
5167
5168 copy_pages(extent_buffer_page(dst, dst_i),
5169 extent_buffer_page(dst, src_i),
5170 dst_off_in_page, src_off_in_page, cur);
5171
5172 src_offset += cur;
5173 dst_offset += cur;
5174 len -= cur;
5175 }
5176}
Chris Masond1310b22008-01-24 16:13:08 -05005177
5178void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
5179 unsigned long src_offset, unsigned long len)
5180{
5181 size_t cur;
5182 size_t dst_off_in_page;
5183 size_t src_off_in_page;
5184 unsigned long dst_end = dst_offset + len - 1;
5185 unsigned long src_end = src_offset + len - 1;
5186 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
5187 unsigned long dst_i;
5188 unsigned long src_i;
5189
5190 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05005191 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
5192 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005193 BUG_ON(1);
5194 }
5195 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05005196 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
5197 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05005198 BUG_ON(1);
5199 }
Chris Mason727011e2010-08-06 13:21:20 -04005200 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05005201 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
5202 return;
5203 }
Chris Masond3977122009-01-05 21:25:51 -05005204 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05005205 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
5206 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
5207
5208 dst_off_in_page = (start_offset + dst_end) &
Geert Uytterhoeven778746b2013-08-20 13:20:16 +02005209 (PAGE_CACHE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005210 src_off_in_page = (start_offset + src_end) &
Geert Uytterhoeven778746b2013-08-20 13:20:16 +02005211 (PAGE_CACHE_SIZE - 1);
Chris Masond1310b22008-01-24 16:13:08 -05005212
5213 cur = min_t(unsigned long, len, src_off_in_page + 1);
5214 cur = min(cur, dst_off_in_page + 1);
Zach Brown1877e1a2013-10-16 12:10:33 -07005215 copy_pages(extent_buffer_page(dst, dst_i),
Chris Masond1310b22008-01-24 16:13:08 -05005216 extent_buffer_page(dst, src_i),
5217 dst_off_in_page - cur + 1,
5218 src_off_in_page - cur + 1, cur);
5219
5220 dst_end -= cur;
5221 src_end -= cur;
5222 len -= cur;
5223 }
5224}
Chris Mason6af118ce2008-07-22 11:18:07 -04005225
David Sterbaf7a52a42013-04-26 14:56:29 +00005226int try_release_extent_buffer(struct page *page)
Miao Xie19fe0a82010-10-26 20:57:29 -04005227{
Chris Mason6af118ce2008-07-22 11:18:07 -04005228 struct extent_buffer *eb;
Miao Xie897ca6e92010-10-26 20:57:29 -04005229
Miao Xie19fe0a82010-10-26 20:57:29 -04005230 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005231 * We need to make sure noboody is attaching this page to an eb right
5232 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04005233 */
Josef Bacik3083ee22012-03-09 16:01:49 -05005234 spin_lock(&page->mapping->private_lock);
5235 if (!PagePrivate(page)) {
5236 spin_unlock(&page->mapping->private_lock);
5237 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04005238 }
5239
Josef Bacik3083ee22012-03-09 16:01:49 -05005240 eb = (struct extent_buffer *)page->private;
5241 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04005242
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005243 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05005244 * This is a little awful but should be ok, we need to make sure that
5245 * the eb doesn't disappear out from under us while we're looking at
5246 * this page.
5247 */
5248 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04005249 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05005250 spin_unlock(&eb->refs_lock);
5251 spin_unlock(&page->mapping->private_lock);
5252 return 0;
5253 }
5254 spin_unlock(&page->mapping->private_lock);
5255
Josef Bacik3083ee22012-03-09 16:01:49 -05005256 /*
5257 * If tree ref isn't set then we know the ref on this eb is a real ref,
5258 * so just return, this page will likely be freed soon anyway.
5259 */
5260 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
5261 spin_unlock(&eb->refs_lock);
5262 return 0;
5263 }
Josef Bacik3083ee22012-03-09 16:01:49 -05005264
David Sterbaf7a52a42013-04-26 14:56:29 +00005265 return release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04005266}