blob: 05951bdf72cc822e90a202d840a439e515e781c1 [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>
7#include <linux/module.h>
8#include <linux/spinlock.h>
9#include <linux/blkdev.h>
10#include <linux/swap.h>
Chris Masond1310b22008-01-24 16:13:08 -050011#include <linux/writeback.h>
12#include <linux/pagevec.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -070013#include <linux/prefetch.h>
Dan Magenheimer90a887c2011-05-26 10:01:56 -060014#include <linux/cleancache.h>
Chris Masond1310b22008-01-24 16:13:08 -050015#include "extent_io.h"
16#include "extent_map.h"
David Woodhouse2db04962008-08-07 11:19:43 -040017#include "compat.h"
David Woodhouse902b22f2008-08-20 08:51:49 -040018#include "ctree.h"
19#include "btrfs_inode.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020020#include "volumes.h"
Stefan Behrens21adbd52011-11-09 13:44:05 +010021#include "check-integrity.h"
Josef Bacik0b32f4b2012-03-13 09:38:00 -040022#include "locking.h"
Chris Masond1310b22008-01-24 16:13:08 -050023
Chris Masond1310b22008-01-24 16:13:08 -050024static struct kmem_cache *extent_state_cache;
25static struct kmem_cache *extent_buffer_cache;
26
27static LIST_HEAD(buffers);
28static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040029
Chris Masonb47eda82008-11-10 12:34:40 -050030#define LEAK_DEBUG 0
Chris Mason39351272009-02-04 09:24:05 -050031#if LEAK_DEBUG
Chris Masond3977122009-01-05 21:25:51 -050032static DEFINE_SPINLOCK(leak_lock);
Chris Mason4bef0842008-09-08 11:18:08 -040033#endif
Chris Masond1310b22008-01-24 16:13:08 -050034
Chris Masond1310b22008-01-24 16:13:08 -050035#define BUFFER_LRU_MAX 64
36
37struct tree_entry {
38 u64 start;
39 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -050040 struct rb_node rb_node;
41};
42
43struct extent_page_data {
44 struct bio *bio;
45 struct extent_io_tree *tree;
46 get_extent_t *get_extent;
Chris Mason771ed682008-11-06 22:02:51 -050047
48 /* tells writepage not to lock the state bits for this range
49 * it still does the unlocking
50 */
Chris Masonffbd5172009-04-20 15:50:09 -040051 unsigned int extent_locked:1;
52
53 /* tells the submit_bio code to use a WRITE_SYNC */
54 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -050055};
56
Josef Bacik0b32f4b2012-03-13 09:38:00 -040057static noinline void flush_write_bio(void *data);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -040058static inline struct btrfs_fs_info *
59tree_fs_info(struct extent_io_tree *tree)
60{
61 return btrfs_sb(tree->mapping->host->i_sb);
62}
Josef Bacik0b32f4b2012-03-13 09:38:00 -040063
Chris Masond1310b22008-01-24 16:13:08 -050064int __init extent_io_init(void)
65{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020066 extent_state_cache = kmem_cache_create("extent_state",
67 sizeof(struct extent_state), 0,
68 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050069 if (!extent_state_cache)
70 return -ENOMEM;
71
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020072 extent_buffer_cache = kmem_cache_create("extent_buffers",
73 sizeof(struct extent_buffer), 0,
74 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050075 if (!extent_buffer_cache)
76 goto free_state_cache;
77 return 0;
78
79free_state_cache:
80 kmem_cache_destroy(extent_state_cache);
81 return -ENOMEM;
82}
83
84void extent_io_exit(void)
85{
86 struct extent_state *state;
Chris Mason2d2ae542008-03-26 16:24:23 -040087 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -050088
89 while (!list_empty(&states)) {
Chris Mason2d2ae542008-03-26 16:24:23 -040090 state = list_entry(states.next, struct extent_state, leak_list);
Chris Masond3977122009-01-05 21:25:51 -050091 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
92 "state %lu in tree %p refs %d\n",
93 (unsigned long long)state->start,
94 (unsigned long long)state->end,
95 state->state, state->tree, atomic_read(&state->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040096 list_del(&state->leak_list);
Chris Masond1310b22008-01-24 16:13:08 -050097 kmem_cache_free(extent_state_cache, state);
98
99 }
100
Chris Mason2d2ae542008-03-26 16:24:23 -0400101 while (!list_empty(&buffers)) {
102 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
Chris Masond3977122009-01-05 21:25:51 -0500103 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
104 "refs %d\n", (unsigned long long)eb->start,
105 eb->len, atomic_read(&eb->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -0400106 list_del(&eb->leak_list);
107 kmem_cache_free(extent_buffer_cache, eb);
108 }
Chris Masond1310b22008-01-24 16:13:08 -0500109 if (extent_state_cache)
110 kmem_cache_destroy(extent_state_cache);
111 if (extent_buffer_cache)
112 kmem_cache_destroy(extent_buffer_cache);
113}
114
115void extent_io_tree_init(struct extent_io_tree *tree,
David Sterbaf993c882011-04-20 23:35:57 +0200116 struct address_space *mapping)
Chris Masond1310b22008-01-24 16:13:08 -0500117{
Eric Paris6bef4d32010-02-23 19:43:04 +0000118 tree->state = RB_ROOT;
Miao Xie19fe0a82010-10-26 20:57:29 -0400119 INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500120 tree->ops = NULL;
121 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500122 spin_lock_init(&tree->lock);
Chris Mason6af118c2008-07-22 11:18:07 -0400123 spin_lock_init(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -0500124 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500125}
Chris Masond1310b22008-01-24 16:13:08 -0500126
Christoph Hellwigb2950862008-12-02 09:54:17 -0500127static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500128{
129 struct extent_state *state;
Chris Mason39351272009-02-04 09:24:05 -0500130#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400131 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400132#endif
Chris Masond1310b22008-01-24 16:13:08 -0500133
134 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400135 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500136 return state;
137 state->state = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500138 state->private = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500139 state->tree = NULL;
Chris Mason39351272009-02-04 09:24:05 -0500140#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400141 spin_lock_irqsave(&leak_lock, flags);
142 list_add(&state->leak_list, &states);
143 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400144#endif
Chris Masond1310b22008-01-24 16:13:08 -0500145 atomic_set(&state->refs, 1);
146 init_waitqueue_head(&state->wq);
Jeff Mahoney143bede2012-03-01 14:56:26 +0100147 trace_alloc_extent_state(state, mask, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500148 return state;
149}
Chris Masond1310b22008-01-24 16:13:08 -0500150
Chris Mason4845e442010-05-25 20:56:50 -0400151void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500152{
Chris Masond1310b22008-01-24 16:13:08 -0500153 if (!state)
154 return;
155 if (atomic_dec_and_test(&state->refs)) {
Chris Mason39351272009-02-04 09:24:05 -0500156#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400157 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400158#endif
Chris Mason70dec802008-01-29 09:59:12 -0500159 WARN_ON(state->tree);
Chris Mason39351272009-02-04 09:24:05 -0500160#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400161 spin_lock_irqsave(&leak_lock, flags);
162 list_del(&state->leak_list);
163 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400164#endif
Jeff Mahoney143bede2012-03-01 14:56:26 +0100165 trace_free_extent_state(state, _RET_IP_);
Chris Masond1310b22008-01-24 16:13:08 -0500166 kmem_cache_free(extent_state_cache, state);
167 }
168}
Chris Masond1310b22008-01-24 16:13:08 -0500169
170static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
171 struct rb_node *node)
172{
Chris Masond3977122009-01-05 21:25:51 -0500173 struct rb_node **p = &root->rb_node;
174 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500175 struct tree_entry *entry;
176
Chris Masond3977122009-01-05 21:25:51 -0500177 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500178 parent = *p;
179 entry = rb_entry(parent, struct tree_entry, rb_node);
180
181 if (offset < entry->start)
182 p = &(*p)->rb_left;
183 else if (offset > entry->end)
184 p = &(*p)->rb_right;
185 else
186 return parent;
187 }
188
189 entry = rb_entry(node, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500190 rb_link_node(node, parent, p);
191 rb_insert_color(node, root);
192 return NULL;
193}
194
Chris Mason80ea96b2008-02-01 14:51:59 -0500195static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Chris Masond1310b22008-01-24 16:13:08 -0500196 struct rb_node **prev_ret,
197 struct rb_node **next_ret)
198{
Chris Mason80ea96b2008-02-01 14:51:59 -0500199 struct rb_root *root = &tree->state;
Chris Masond3977122009-01-05 21:25:51 -0500200 struct rb_node *n = root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500201 struct rb_node *prev = NULL;
202 struct rb_node *orig_prev = NULL;
203 struct tree_entry *entry;
204 struct tree_entry *prev_entry = NULL;
205
Chris Masond3977122009-01-05 21:25:51 -0500206 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500207 entry = rb_entry(n, struct tree_entry, rb_node);
208 prev = n;
209 prev_entry = entry;
210
211 if (offset < entry->start)
212 n = n->rb_left;
213 else if (offset > entry->end)
214 n = n->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500215 else
Chris Masond1310b22008-01-24 16:13:08 -0500216 return n;
217 }
218
219 if (prev_ret) {
220 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500221 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500222 prev = rb_next(prev);
223 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
224 }
225 *prev_ret = prev;
226 prev = orig_prev;
227 }
228
229 if (next_ret) {
230 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500231 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500232 prev = rb_prev(prev);
233 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
234 }
235 *next_ret = prev;
236 }
237 return NULL;
238}
239
Chris Mason80ea96b2008-02-01 14:51:59 -0500240static inline struct rb_node *tree_search(struct extent_io_tree *tree,
241 u64 offset)
Chris Masond1310b22008-01-24 16:13:08 -0500242{
Chris Mason70dec802008-01-29 09:59:12 -0500243 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500244 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500245
Chris Mason80ea96b2008-02-01 14:51:59 -0500246 ret = __etree_search(tree, offset, &prev, NULL);
Chris Masond3977122009-01-05 21:25:51 -0500247 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500248 return prev;
249 return ret;
250}
251
Josef Bacik9ed74f22009-09-11 16:12:44 -0400252static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
253 struct extent_state *other)
254{
255 if (tree->ops && tree->ops->merge_extent_hook)
256 tree->ops->merge_extent_hook(tree->mapping->host, new,
257 other);
258}
259
Chris Masond1310b22008-01-24 16:13:08 -0500260/*
261 * utility function to look for merge candidates inside a given range.
262 * Any extents with matching state are merged together into a single
263 * extent in the tree. Extents with EXTENT_IO in their state field
264 * are not merged because the end_io handlers need to be able to do
265 * operations on them without sleeping (or doing allocations/splits).
266 *
267 * This should be called with the tree lock held.
268 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000269static void merge_state(struct extent_io_tree *tree,
270 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500271{
272 struct extent_state *other;
273 struct rb_node *other_node;
274
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400275 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000276 return;
Chris Masond1310b22008-01-24 16:13:08 -0500277
278 other_node = rb_prev(&state->rb_node);
279 if (other_node) {
280 other = rb_entry(other_node, struct extent_state, rb_node);
281 if (other->end == state->start - 1 &&
282 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400283 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500284 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500285 other->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500286 rb_erase(&other->rb_node, &tree->state);
287 free_extent_state(other);
288 }
289 }
290 other_node = rb_next(&state->rb_node);
291 if (other_node) {
292 other = rb_entry(other_node, struct extent_state, rb_node);
293 if (other->start == state->end + 1 &&
294 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400295 merge_cb(tree, state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400296 state->end = other->end;
297 other->tree = NULL;
298 rb_erase(&other->rb_node, &tree->state);
299 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500300 }
301 }
Chris Masond1310b22008-01-24 16:13:08 -0500302}
303
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000304static void set_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400305 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500306{
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000307 if (tree->ops && tree->ops->set_bit_hook)
308 tree->ops->set_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500309}
310
311static void clear_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400312 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500313{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400314 if (tree->ops && tree->ops->clear_bit_hook)
315 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500316}
317
Xiao Guangrong3150b692011-07-14 03:19:08 +0000318static void set_state_bits(struct extent_io_tree *tree,
319 struct extent_state *state, int *bits);
320
Chris Masond1310b22008-01-24 16:13:08 -0500321/*
322 * insert an extent_state struct into the tree. 'bits' are set on the
323 * struct before it is inserted.
324 *
325 * This may return -EEXIST if the extent is already there, in which case the
326 * state struct is freed.
327 *
328 * The tree lock is not taken internally. This is a utility function and
329 * probably isn't what you want to call (see set/clear_extent_bit).
330 */
331static int insert_state(struct extent_io_tree *tree,
332 struct extent_state *state, u64 start, u64 end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400333 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500334{
335 struct rb_node *node;
336
337 if (end < start) {
Chris Masond3977122009-01-05 21:25:51 -0500338 printk(KERN_ERR "btrfs end < start %llu %llu\n",
339 (unsigned long long)end,
340 (unsigned long long)start);
Chris Masond1310b22008-01-24 16:13:08 -0500341 WARN_ON(1);
342 }
Chris Masond1310b22008-01-24 16:13:08 -0500343 state->start = start;
344 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400345
Xiao Guangrong3150b692011-07-14 03:19:08 +0000346 set_state_bits(tree, state, bits);
347
Chris Masond1310b22008-01-24 16:13:08 -0500348 node = tree_insert(&tree->state, end, &state->rb_node);
349 if (node) {
350 struct extent_state *found;
351 found = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500352 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
353 "%llu %llu\n", (unsigned long long)found->start,
354 (unsigned long long)found->end,
355 (unsigned long long)start, (unsigned long long)end);
Chris Masond1310b22008-01-24 16:13:08 -0500356 return -EEXIST;
357 }
Chris Mason70dec802008-01-29 09:59:12 -0500358 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500359 merge_state(tree, state);
360 return 0;
361}
362
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000363static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
Josef Bacik9ed74f22009-09-11 16:12:44 -0400364 u64 split)
365{
366 if (tree->ops && tree->ops->split_extent_hook)
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000367 tree->ops->split_extent_hook(tree->mapping->host, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400368}
369
Chris Masond1310b22008-01-24 16:13:08 -0500370/*
371 * split a given extent state struct in two, inserting the preallocated
372 * struct 'prealloc' as the newly created second half. 'split' indicates an
373 * offset inside 'orig' where it should be split.
374 *
375 * Before calling,
376 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
377 * are two extent state structs in the tree:
378 * prealloc: [orig->start, split - 1]
379 * orig: [ split, orig->end ]
380 *
381 * The tree locks are not taken by this function. They need to be held
382 * by the caller.
383 */
384static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
385 struct extent_state *prealloc, u64 split)
386{
387 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400388
389 split_cb(tree, orig, split);
390
Chris Masond1310b22008-01-24 16:13:08 -0500391 prealloc->start = orig->start;
392 prealloc->end = split - 1;
393 prealloc->state = orig->state;
394 orig->start = split;
395
396 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
397 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500398 free_extent_state(prealloc);
399 return -EEXIST;
400 }
Chris Mason70dec802008-01-29 09:59:12 -0500401 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500402 return 0;
403}
404
405/*
406 * utility function to clear some bits in an extent state struct.
Li Zefan8e52acf2012-03-12 16:39:28 +0800407 * it will optionally wake up any one waiting on this state (wake == 1)
Chris Masond1310b22008-01-24 16:13:08 -0500408 *
409 * If no bits are set on the state struct after clearing things, the
410 * struct is freed and removed from the tree
411 */
Li Zefan8e52acf2012-03-12 16:39:28 +0800412static void clear_state_bit(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400413 struct extent_state *state,
414 int *bits, int wake)
Chris Masond1310b22008-01-24 16:13:08 -0500415{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400416 int bits_to_clear = *bits & ~EXTENT_CTLBITS;
Chris Masond1310b22008-01-24 16:13:08 -0500417
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400418 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500419 u64 range = state->end - state->start + 1;
420 WARN_ON(range > tree->dirty_bytes);
421 tree->dirty_bytes -= range;
422 }
Chris Mason291d6732008-01-29 15:55:23 -0500423 clear_state_cb(tree, state, bits);
Josef Bacik32c00af2009-10-08 13:34:05 -0400424 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500425 if (wake)
426 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400427 if (state->state == 0) {
Chris Mason70dec802008-01-29 09:59:12 -0500428 if (state->tree) {
Chris Masond1310b22008-01-24 16:13:08 -0500429 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500430 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500431 free_extent_state(state);
432 } else {
433 WARN_ON(1);
434 }
435 } else {
436 merge_state(tree, state);
437 }
Chris Masond1310b22008-01-24 16:13:08 -0500438}
439
Xiao Guangrong82337672011-04-20 06:44:57 +0000440static struct extent_state *
441alloc_extent_state_atomic(struct extent_state *prealloc)
442{
443 if (!prealloc)
444 prealloc = alloc_extent_state(GFP_ATOMIC);
445
446 return prealloc;
447}
448
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400449void extent_io_tree_panic(struct extent_io_tree *tree, int err)
450{
451 btrfs_panic(tree_fs_info(tree), err, "Locking error: "
452 "Extent tree was modified by another "
453 "thread while locked.");
454}
455
Chris Masond1310b22008-01-24 16:13:08 -0500456/*
457 * clear some bits on a range in the tree. This may require splitting
458 * or inserting elements in the tree, so the gfp mask is used to
459 * indicate which allocations or sleeping are allowed.
460 *
461 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
462 * the given range from the tree regardless of state (ie for truncate).
463 *
464 * the range [start, end] is inclusive.
465 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100466 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500467 */
468int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400469 int bits, int wake, int delete,
470 struct extent_state **cached_state,
471 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500472{
473 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400474 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500475 struct extent_state *prealloc = NULL;
Chris Mason2c64c532009-09-02 15:04:12 -0400476 struct rb_node *next_node;
Chris Masond1310b22008-01-24 16:13:08 -0500477 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400478 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500479 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000480 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500481
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400482 if (delete)
483 bits |= ~EXTENT_CTLBITS;
484 bits |= EXTENT_FIRST_DELALLOC;
485
Josef Bacik2ac55d42010-02-03 19:33:23 +0000486 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
487 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500488again:
489 if (!prealloc && (mask & __GFP_WAIT)) {
490 prealloc = alloc_extent_state(mask);
491 if (!prealloc)
492 return -ENOMEM;
493 }
494
Chris Masoncad321a2008-12-17 14:51:42 -0500495 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400496 if (cached_state) {
497 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000498
499 if (clear) {
500 *cached_state = NULL;
501 cached_state = NULL;
502 }
503
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400504 if (cached && cached->tree && cached->start <= start &&
505 cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000506 if (clear)
507 atomic_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400508 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400509 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400510 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000511 if (clear)
512 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400513 }
Chris Masond1310b22008-01-24 16:13:08 -0500514 /*
515 * this search will find the extents that end after
516 * our range starts
517 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500518 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500519 if (!node)
520 goto out;
521 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400522hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500523 if (state->start > end)
524 goto out;
525 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400526 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500527
Liu Bo04493142012-02-16 18:34:37 +0800528 if (state->end < end && !need_resched())
529 next_node = rb_next(&state->rb_node);
530 else
531 next_node = NULL;
532
533 /* the state doesn't have the wanted bits, go ahead */
534 if (!(state->state & bits))
535 goto next;
536
Chris Masond1310b22008-01-24 16:13:08 -0500537 /*
538 * | ---- desired range ---- |
539 * | state | or
540 * | ------------- state -------------- |
541 *
542 * We need to split the extent we found, and may flip
543 * bits on second half.
544 *
545 * If the extent we found extends past our range, we
546 * just split and search again. It'll get split again
547 * the next time though.
548 *
549 * If the extent we found is inside our range, we clear
550 * the desired bit on it.
551 */
552
553 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000554 prealloc = alloc_extent_state_atomic(prealloc);
555 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500556 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400557 if (err)
558 extent_io_tree_panic(tree, err);
559
Chris Masond1310b22008-01-24 16:13:08 -0500560 prealloc = NULL;
561 if (err)
562 goto out;
563 if (state->end <= end) {
Jeff Mahoney6763af82012-03-01 14:56:29 +0100564 clear_state_bit(tree, state, &bits, wake);
Yan Zheng5c939df2009-05-27 09:16:03 -0400565 if (last_end == (u64)-1)
566 goto out;
567 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500568 }
569 goto search_again;
570 }
571 /*
572 * | ---- desired range ---- |
573 * | state |
574 * We need to split the extent, and clear the bit
575 * on the first half
576 */
577 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000578 prealloc = alloc_extent_state_atomic(prealloc);
579 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500580 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400581 if (err)
582 extent_io_tree_panic(tree, err);
583
Chris Masond1310b22008-01-24 16:13:08 -0500584 if (wake)
585 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400586
Jeff Mahoney6763af82012-03-01 14:56:29 +0100587 clear_state_bit(tree, prealloc, &bits, wake);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400588
Chris Masond1310b22008-01-24 16:13:08 -0500589 prealloc = NULL;
590 goto out;
591 }
Chris Mason42daec22009-09-23 19:51:09 -0400592
Jeff Mahoney6763af82012-03-01 14:56:29 +0100593 clear_state_bit(tree, state, &bits, wake);
Liu Bo04493142012-02-16 18:34:37 +0800594next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400595 if (last_end == (u64)-1)
596 goto out;
597 start = last_end + 1;
Chris Mason2c64c532009-09-02 15:04:12 -0400598 if (start <= end && next_node) {
599 state = rb_entry(next_node, struct extent_state,
600 rb_node);
Liu Bo692e5752012-02-16 18:34:36 +0800601 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400602 }
Chris Masond1310b22008-01-24 16:13:08 -0500603 goto search_again;
604
605out:
Chris Masoncad321a2008-12-17 14:51:42 -0500606 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500607 if (prealloc)
608 free_extent_state(prealloc);
609
Jeff Mahoney6763af82012-03-01 14:56:29 +0100610 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500611
612search_again:
613 if (start > end)
614 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500615 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500616 if (mask & __GFP_WAIT)
617 cond_resched();
618 goto again;
619}
Chris Masond1310b22008-01-24 16:13:08 -0500620
Jeff Mahoney143bede2012-03-01 14:56:26 +0100621static void wait_on_state(struct extent_io_tree *tree,
622 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500623 __releases(tree->lock)
624 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500625{
626 DEFINE_WAIT(wait);
627 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500628 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500629 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500630 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500631 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500632}
633
634/*
635 * waits for one or more bits to clear on a range in the state tree.
636 * The range [start, end] is inclusive.
637 * The tree lock is taken by this function
638 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100639void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
Chris Masond1310b22008-01-24 16:13:08 -0500640{
641 struct extent_state *state;
642 struct rb_node *node;
643
Chris Masoncad321a2008-12-17 14:51:42 -0500644 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500645again:
646 while (1) {
647 /*
648 * this search will find all the extents that end after
649 * our range starts
650 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500651 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500652 if (!node)
653 break;
654
655 state = rb_entry(node, struct extent_state, rb_node);
656
657 if (state->start > end)
658 goto out;
659
660 if (state->state & bits) {
661 start = state->start;
662 atomic_inc(&state->refs);
663 wait_on_state(tree, state);
664 free_extent_state(state);
665 goto again;
666 }
667 start = state->end + 1;
668
669 if (start > end)
670 break;
671
Xiao Guangrongded91f02011-07-14 03:19:27 +0000672 cond_resched_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500673 }
674out:
Chris Masoncad321a2008-12-17 14:51:42 -0500675 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500676}
Chris Masond1310b22008-01-24 16:13:08 -0500677
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000678static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500679 struct extent_state *state,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400680 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500681{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400682 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400683
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000684 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400685 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500686 u64 range = state->end - state->start + 1;
687 tree->dirty_bytes += range;
688 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400689 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500690}
691
Chris Mason2c64c532009-09-02 15:04:12 -0400692static void cache_state(struct extent_state *state,
693 struct extent_state **cached_ptr)
694{
695 if (cached_ptr && !(*cached_ptr)) {
696 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
697 *cached_ptr = state;
698 atomic_inc(&state->refs);
699 }
700 }
701}
702
Arne Jansen507903b2011-04-06 10:02:20 +0000703static void uncache_state(struct extent_state **cached_ptr)
704{
705 if (cached_ptr && (*cached_ptr)) {
706 struct extent_state *state = *cached_ptr;
Chris Mason109b36a2011-04-12 13:57:39 -0400707 *cached_ptr = NULL;
708 free_extent_state(state);
Arne Jansen507903b2011-04-06 10:02:20 +0000709 }
710}
711
Chris Masond1310b22008-01-24 16:13:08 -0500712/*
Chris Mason1edbb732009-09-02 13:24:36 -0400713 * set some bits on a range in the tree. This may require allocations or
714 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500715 *
Chris Mason1edbb732009-09-02 13:24:36 -0400716 * If any of the exclusive bits are set, this will fail with -EEXIST if some
717 * part of the range already has the desired bits set. The start of the
718 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500719 *
Chris Mason1edbb732009-09-02 13:24:36 -0400720 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500721 */
Chris Mason1edbb732009-09-02 13:24:36 -0400722
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100723static int __must_check
724__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
725 int bits, int exclusive_bits, u64 *failed_start,
726 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500727{
728 struct extent_state *state;
729 struct extent_state *prealloc = NULL;
730 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500731 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500732 u64 last_start;
733 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400734
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400735 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500736again:
737 if (!prealloc && (mask & __GFP_WAIT)) {
738 prealloc = alloc_extent_state(mask);
Xiao Guangrong82337672011-04-20 06:44:57 +0000739 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500740 }
741
Chris Masoncad321a2008-12-17 14:51:42 -0500742 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400743 if (cached_state && *cached_state) {
744 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400745 if (state->start <= start && state->end > start &&
746 state->tree) {
Chris Mason9655d292009-09-02 15:22:30 -0400747 node = &state->rb_node;
748 goto hit_next;
749 }
750 }
Chris Masond1310b22008-01-24 16:13:08 -0500751 /*
752 * this search will find all the extents that end after
753 * our range starts.
754 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500755 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500756 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000757 prealloc = alloc_extent_state_atomic(prealloc);
758 BUG_ON(!prealloc);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400759 err = insert_state(tree, prealloc, start, end, &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400760 if (err)
761 extent_io_tree_panic(tree, err);
762
Chris Masond1310b22008-01-24 16:13:08 -0500763 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500764 goto out;
765 }
Chris Masond1310b22008-01-24 16:13:08 -0500766 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400767hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500768 last_start = state->start;
769 last_end = state->end;
770
771 /*
772 * | ---- desired range ---- |
773 * | state |
774 *
775 * Just lock what we found and keep going
776 */
777 if (state->start == start && state->end <= end) {
Chris Mason40431d62009-08-05 12:57:59 -0400778 struct rb_node *next_node;
Chris Mason1edbb732009-09-02 13:24:36 -0400779 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500780 *failed_start = state->start;
781 err = -EEXIST;
782 goto out;
783 }
Chris Mason42daec22009-09-23 19:51:09 -0400784
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000785 set_state_bits(tree, state, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400786
Chris Mason2c64c532009-09-02 15:04:12 -0400787 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500788 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400789 if (last_end == (u64)-1)
790 goto out;
Chris Mason40431d62009-08-05 12:57:59 -0400791
Yan Zheng5c939df2009-05-27 09:16:03 -0400792 start = last_end + 1;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400793 next_node = rb_next(&state->rb_node);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000794 if (next_node && start < end && prealloc && !need_resched()) {
795 state = rb_entry(next_node, struct extent_state,
796 rb_node);
797 if (state->start == start)
798 goto hit_next;
Chris Mason40431d62009-08-05 12:57:59 -0400799 }
Chris Masond1310b22008-01-24 16:13:08 -0500800 goto search_again;
801 }
802
803 /*
804 * | ---- desired range ---- |
805 * | state |
806 * or
807 * | ------------- state -------------- |
808 *
809 * We need to split the extent we found, and may flip bits on
810 * second half.
811 *
812 * If the extent we found extends past our
813 * range, we just split and search again. It'll get split
814 * again the next time though.
815 *
816 * If the extent we found is inside our range, we set the
817 * desired bit on it.
818 */
819 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400820 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500821 *failed_start = start;
822 err = -EEXIST;
823 goto out;
824 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000825
826 prealloc = alloc_extent_state_atomic(prealloc);
827 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500828 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400829 if (err)
830 extent_io_tree_panic(tree, err);
831
Chris Masond1310b22008-01-24 16:13:08 -0500832 prealloc = NULL;
833 if (err)
834 goto out;
835 if (state->end <= end) {
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000836 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400837 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500838 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400839 if (last_end == (u64)-1)
840 goto out;
841 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500842 }
843 goto search_again;
844 }
845 /*
846 * | ---- desired range ---- |
847 * | state | or | state |
848 *
849 * There's a hole, we need to insert something in it and
850 * ignore the extent we found.
851 */
852 if (state->start > start) {
853 u64 this_end;
854 if (end < last_start)
855 this_end = end;
856 else
Chris Masond3977122009-01-05 21:25:51 -0500857 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000858
859 prealloc = alloc_extent_state_atomic(prealloc);
860 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000861
862 /*
863 * Avoid to free 'prealloc' if it can be merged with
864 * the later extent.
865 */
Chris Masond1310b22008-01-24 16:13:08 -0500866 err = insert_state(tree, prealloc, start, this_end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400867 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400868 if (err)
869 extent_io_tree_panic(tree, err);
870
Chris Mason2c64c532009-09-02 15:04:12 -0400871 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500872 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500873 start = this_end + 1;
874 goto search_again;
875 }
876 /*
877 * | ---- desired range ---- |
878 * | state |
879 * We need to split the extent, and set the bit
880 * on the first half
881 */
882 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400883 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500884 *failed_start = start;
885 err = -EEXIST;
886 goto out;
887 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000888
889 prealloc = alloc_extent_state_atomic(prealloc);
890 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500891 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400892 if (err)
893 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -0500894
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000895 set_state_bits(tree, prealloc, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400896 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500897 merge_state(tree, prealloc);
898 prealloc = NULL;
899 goto out;
900 }
901
902 goto search_again;
903
904out:
Chris Masoncad321a2008-12-17 14:51:42 -0500905 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500906 if (prealloc)
907 free_extent_state(prealloc);
908
909 return err;
910
911search_again:
912 if (start > end)
913 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500914 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500915 if (mask & __GFP_WAIT)
916 cond_resched();
917 goto again;
918}
Chris Masond1310b22008-01-24 16:13:08 -0500919
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100920int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits,
921 u64 *failed_start, struct extent_state **cached_state,
922 gfp_t mask)
923{
924 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
925 cached_state, mask);
926}
927
928
Josef Bacik462d6fa2011-09-26 13:56:12 -0400929/**
930 * convert_extent - convert all bits in a given range from one bit to another
931 * @tree: the io tree to search
932 * @start: the start offset in bytes
933 * @end: the end offset in bytes (inclusive)
934 * @bits: the bits to set in this range
935 * @clear_bits: the bits to clear in this range
936 * @mask: the allocation mask
937 *
938 * This will go through and set bits for the given range. If any states exist
939 * already in this range they are set with the given bit and cleared of the
940 * clear_bits. This is only meant to be used by things that are mergeable, ie
941 * converting from say DELALLOC to DIRTY. This is not meant to be used with
942 * boundary bits like LOCK.
943 */
944int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
945 int bits, int clear_bits, gfp_t mask)
946{
947 struct extent_state *state;
948 struct extent_state *prealloc = NULL;
949 struct rb_node *node;
950 int err = 0;
951 u64 last_start;
952 u64 last_end;
953
954again:
955 if (!prealloc && (mask & __GFP_WAIT)) {
956 prealloc = alloc_extent_state(mask);
957 if (!prealloc)
958 return -ENOMEM;
959 }
960
961 spin_lock(&tree->lock);
962 /*
963 * this search will find all the extents that end after
964 * our range starts.
965 */
966 node = tree_search(tree, start);
967 if (!node) {
968 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -0500969 if (!prealloc) {
970 err = -ENOMEM;
971 goto out;
972 }
Josef Bacik462d6fa2011-09-26 13:56:12 -0400973 err = insert_state(tree, prealloc, start, end, &bits);
974 prealloc = NULL;
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400975 if (err)
976 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -0400977 goto out;
978 }
979 state = rb_entry(node, struct extent_state, rb_node);
980hit_next:
981 last_start = state->start;
982 last_end = state->end;
983
984 /*
985 * | ---- desired range ---- |
986 * | state |
987 *
988 * Just lock what we found and keep going
989 */
990 if (state->start == start && state->end <= end) {
991 struct rb_node *next_node;
992
993 set_state_bits(tree, state, &bits);
994 clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -0400995 if (last_end == (u64)-1)
996 goto out;
997
998 start = last_end + 1;
999 next_node = rb_next(&state->rb_node);
1000 if (next_node && start < end && prealloc && !need_resched()) {
1001 state = rb_entry(next_node, struct extent_state,
1002 rb_node);
1003 if (state->start == start)
1004 goto hit_next;
1005 }
1006 goto search_again;
1007 }
1008
1009 /*
1010 * | ---- desired range ---- |
1011 * | state |
1012 * or
1013 * | ------------- state -------------- |
1014 *
1015 * We need to split the extent we found, and may flip bits on
1016 * second half.
1017 *
1018 * If the extent we found extends past our
1019 * range, we just split and search again. It'll get split
1020 * again the next time though.
1021 *
1022 * If the extent we found is inside our range, we set the
1023 * desired bit on it.
1024 */
1025 if (state->start < start) {
1026 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001027 if (!prealloc) {
1028 err = -ENOMEM;
1029 goto out;
1030 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001031 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001032 if (err)
1033 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001034 prealloc = NULL;
1035 if (err)
1036 goto out;
1037 if (state->end <= end) {
1038 set_state_bits(tree, state, &bits);
1039 clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001040 if (last_end == (u64)-1)
1041 goto out;
1042 start = last_end + 1;
1043 }
1044 goto search_again;
1045 }
1046 /*
1047 * | ---- desired range ---- |
1048 * | state | or | state |
1049 *
1050 * There's a hole, we need to insert something in it and
1051 * ignore the extent we found.
1052 */
1053 if (state->start > start) {
1054 u64 this_end;
1055 if (end < last_start)
1056 this_end = end;
1057 else
1058 this_end = last_start - 1;
1059
1060 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001061 if (!prealloc) {
1062 err = -ENOMEM;
1063 goto out;
1064 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001065
1066 /*
1067 * Avoid to free 'prealloc' if it can be merged with
1068 * the later extent.
1069 */
1070 err = insert_state(tree, prealloc, start, this_end,
1071 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001072 if (err)
1073 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001074 prealloc = NULL;
1075 start = this_end + 1;
1076 goto search_again;
1077 }
1078 /*
1079 * | ---- desired range ---- |
1080 * | state |
1081 * We need to split the extent, and set the bit
1082 * on the first half
1083 */
1084 if (state->start <= end && state->end > end) {
1085 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001086 if (!prealloc) {
1087 err = -ENOMEM;
1088 goto out;
1089 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001090
1091 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001092 if (err)
1093 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001094
1095 set_state_bits(tree, prealloc, &bits);
1096 clear_state_bit(tree, prealloc, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001097 prealloc = NULL;
1098 goto out;
1099 }
1100
1101 goto search_again;
1102
1103out:
1104 spin_unlock(&tree->lock);
1105 if (prealloc)
1106 free_extent_state(prealloc);
1107
1108 return err;
1109
1110search_again:
1111 if (start > end)
1112 goto out;
1113 spin_unlock(&tree->lock);
1114 if (mask & __GFP_WAIT)
1115 cond_resched();
1116 goto again;
1117}
1118
Chris Masond1310b22008-01-24 16:13:08 -05001119/* wrappers around set/clear extent bit */
1120int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1121 gfp_t mask)
1122{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001123 return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001124 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001125}
Chris Masond1310b22008-01-24 16:13:08 -05001126
1127int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1128 int bits, gfp_t mask)
1129{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001130 return set_extent_bit(tree, start, end, bits, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001131 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001132}
Chris Masond1310b22008-01-24 16:13:08 -05001133
1134int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1135 int bits, gfp_t mask)
1136{
Chris Mason2c64c532009-09-02 15:04:12 -04001137 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001138}
Chris Masond1310b22008-01-24 16:13:08 -05001139
1140int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001141 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001142{
1143 return set_extent_bit(tree, start, end,
Liu Bofee187d2011-09-29 15:55:28 +08001144 EXTENT_DELALLOC | EXTENT_UPTODATE,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001145 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001146}
Chris Masond1310b22008-01-24 16:13:08 -05001147
1148int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1149 gfp_t mask)
1150{
1151 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04001152 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001153 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001154}
Chris Masond1310b22008-01-24 16:13:08 -05001155
1156int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1157 gfp_t mask)
1158{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001159 return set_extent_bit(tree, start, end, EXTENT_NEW, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001160 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001161}
Chris Masond1310b22008-01-24 16:13:08 -05001162
Chris Masond1310b22008-01-24 16:13:08 -05001163int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +00001164 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001165{
Arne Jansen507903b2011-04-06 10:02:20 +00001166 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001167 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001168}
Chris Masond1310b22008-01-24 16:13:08 -05001169
Chris Masond3977122009-01-05 21:25:51 -05001170static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001171 u64 end, struct extent_state **cached_state,
1172 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001173{
Chris Mason2c64c532009-09-02 15:04:12 -04001174 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001175 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001176}
Chris Masond1310b22008-01-24 16:13:08 -05001177
Chris Masond352ac62008-09-29 15:18:18 -04001178/*
1179 * either insert or lock state struct between start and end use mask to tell
1180 * us if waiting is desired.
1181 */
Chris Mason1edbb732009-09-02 13:24:36 -04001182int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001183 int bits, struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001184{
1185 int err;
1186 u64 failed_start;
1187 while (1) {
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001188 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
1189 EXTENT_LOCKED, &failed_start,
1190 cached_state, GFP_NOFS);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001191 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001192 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1193 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001194 } else
Chris Masond1310b22008-01-24 16:13:08 -05001195 break;
Chris Masond1310b22008-01-24 16:13:08 -05001196 WARN_ON(start > end);
1197 }
1198 return err;
1199}
Chris Masond1310b22008-01-24 16:13:08 -05001200
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001201int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Mason1edbb732009-09-02 13:24:36 -04001202{
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001203 return lock_extent_bits(tree, start, end, 0, NULL);
Chris Mason1edbb732009-09-02 13:24:36 -04001204}
1205
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001206int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001207{
1208 int err;
1209 u64 failed_start;
1210
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001211 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1212 &failed_start, NULL, GFP_NOFS);
Yan Zheng66435582008-10-30 14:19:50 -04001213 if (err == -EEXIST) {
1214 if (failed_start > start)
1215 clear_extent_bit(tree, start, failed_start - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001216 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
Josef Bacik25179202008-10-29 14:49:05 -04001217 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001218 }
Josef Bacik25179202008-10-29 14:49:05 -04001219 return 1;
1220}
Josef Bacik25179202008-10-29 14:49:05 -04001221
Chris Mason2c64c532009-09-02 15:04:12 -04001222int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1223 struct extent_state **cached, gfp_t mask)
1224{
1225 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1226 mask);
1227}
1228
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001229int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001230{
Chris Mason2c64c532009-09-02 15:04:12 -04001231 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001232 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001233}
Chris Masond1310b22008-01-24 16:13:08 -05001234
1235/*
Chris Masond1310b22008-01-24 16:13:08 -05001236 * helper function to set both pages and extents in the tree writeback
1237 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001238static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001239{
1240 unsigned long index = start >> PAGE_CACHE_SHIFT;
1241 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1242 struct page *page;
1243
1244 while (index <= end_index) {
1245 page = find_get_page(tree->mapping, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001246 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Masond1310b22008-01-24 16:13:08 -05001247 set_page_writeback(page);
1248 page_cache_release(page);
1249 index++;
1250 }
Chris Masond1310b22008-01-24 16:13:08 -05001251 return 0;
1252}
Chris Masond1310b22008-01-24 16:13:08 -05001253
Chris Masond352ac62008-09-29 15:18:18 -04001254/* find the first state struct with 'bits' set after 'start', and
1255 * return it. tree->lock must be held. NULL will returned if
1256 * nothing was found after 'start'
1257 */
Chris Masond7fc6402008-02-18 12:12:38 -05001258struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1259 u64 start, int bits)
1260{
1261 struct rb_node *node;
1262 struct extent_state *state;
1263
1264 /*
1265 * this search will find all the extents that end after
1266 * our range starts.
1267 */
1268 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001269 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001270 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001271
Chris Masond3977122009-01-05 21:25:51 -05001272 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001273 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001274 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001275 return state;
Chris Masond3977122009-01-05 21:25:51 -05001276
Chris Masond7fc6402008-02-18 12:12:38 -05001277 node = rb_next(node);
1278 if (!node)
1279 break;
1280 }
1281out:
1282 return NULL;
1283}
Chris Masond7fc6402008-02-18 12:12:38 -05001284
Chris Masond352ac62008-09-29 15:18:18 -04001285/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001286 * find the first offset in the io tree with 'bits' set. zero is
1287 * returned if we find something, and *start_ret and *end_ret are
1288 * set to reflect the state struct that was found.
1289 *
1290 * If nothing was found, 1 is returned, < 0 on error
1291 */
1292int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1293 u64 *start_ret, u64 *end_ret, int bits)
1294{
1295 struct extent_state *state;
1296 int ret = 1;
1297
1298 spin_lock(&tree->lock);
1299 state = find_first_extent_bit_state(tree, start, bits);
1300 if (state) {
1301 *start_ret = state->start;
1302 *end_ret = state->end;
1303 ret = 0;
1304 }
1305 spin_unlock(&tree->lock);
1306 return ret;
1307}
1308
1309/*
Chris Masond352ac62008-09-29 15:18:18 -04001310 * find a contiguous range of bytes in the file marked as delalloc, not
1311 * more than 'max_bytes'. start and end are used to return the range,
1312 *
1313 * 1 is returned if we find something, 0 if nothing was in the tree
1314 */
Chris Masonc8b97812008-10-29 14:49:59 -04001315static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001316 u64 *start, u64 *end, u64 max_bytes,
1317 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001318{
1319 struct rb_node *node;
1320 struct extent_state *state;
1321 u64 cur_start = *start;
1322 u64 found = 0;
1323 u64 total_bytes = 0;
1324
Chris Masoncad321a2008-12-17 14:51:42 -05001325 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001326
Chris Masond1310b22008-01-24 16:13:08 -05001327 /*
1328 * this search will find all the extents that end after
1329 * our range starts.
1330 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001331 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001332 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001333 if (!found)
1334 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001335 goto out;
1336 }
1337
Chris Masond3977122009-01-05 21:25:51 -05001338 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001339 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001340 if (found && (state->start != cur_start ||
1341 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001342 goto out;
1343 }
1344 if (!(state->state & EXTENT_DELALLOC)) {
1345 if (!found)
1346 *end = state->end;
1347 goto out;
1348 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001349 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001350 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001351 *cached_state = state;
1352 atomic_inc(&state->refs);
1353 }
Chris Masond1310b22008-01-24 16:13:08 -05001354 found++;
1355 *end = state->end;
1356 cur_start = state->end + 1;
1357 node = rb_next(node);
1358 if (!node)
1359 break;
1360 total_bytes += state->end - state->start + 1;
1361 if (total_bytes >= max_bytes)
1362 break;
1363 }
1364out:
Chris Masoncad321a2008-12-17 14:51:42 -05001365 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001366 return found;
1367}
1368
Jeff Mahoney143bede2012-03-01 14:56:26 +01001369static noinline void __unlock_for_delalloc(struct inode *inode,
1370 struct page *locked_page,
1371 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001372{
1373 int ret;
1374 struct page *pages[16];
1375 unsigned long index = start >> PAGE_CACHE_SHIFT;
1376 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1377 unsigned long nr_pages = end_index - index + 1;
1378 int i;
1379
1380 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001381 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001382
Chris Masond3977122009-01-05 21:25:51 -05001383 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001384 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001385 min_t(unsigned long, nr_pages,
1386 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001387 for (i = 0; i < ret; i++) {
1388 if (pages[i] != locked_page)
1389 unlock_page(pages[i]);
1390 page_cache_release(pages[i]);
1391 }
1392 nr_pages -= ret;
1393 index += ret;
1394 cond_resched();
1395 }
Chris Masonc8b97812008-10-29 14:49:59 -04001396}
1397
1398static noinline int lock_delalloc_pages(struct inode *inode,
1399 struct page *locked_page,
1400 u64 delalloc_start,
1401 u64 delalloc_end)
1402{
1403 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1404 unsigned long start_index = index;
1405 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1406 unsigned long pages_locked = 0;
1407 struct page *pages[16];
1408 unsigned long nrpages;
1409 int ret;
1410 int i;
1411
1412 /* the caller is responsible for locking the start index */
1413 if (index == locked_page->index && index == end_index)
1414 return 0;
1415
1416 /* skip the page at the start index */
1417 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001418 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001419 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001420 min_t(unsigned long,
1421 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001422 if (ret == 0) {
1423 ret = -EAGAIN;
1424 goto done;
1425 }
1426 /* now we have an array of pages, lock them all */
1427 for (i = 0; i < ret; i++) {
1428 /*
1429 * the caller is taking responsibility for
1430 * locked_page
1431 */
Chris Mason771ed682008-11-06 22:02:51 -05001432 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001433 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001434 if (!PageDirty(pages[i]) ||
1435 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001436 ret = -EAGAIN;
1437 unlock_page(pages[i]);
1438 page_cache_release(pages[i]);
1439 goto done;
1440 }
1441 }
Chris Masonc8b97812008-10-29 14:49:59 -04001442 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001443 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001444 }
Chris Masonc8b97812008-10-29 14:49:59 -04001445 nrpages -= ret;
1446 index += ret;
1447 cond_resched();
1448 }
1449 ret = 0;
1450done:
1451 if (ret && pages_locked) {
1452 __unlock_for_delalloc(inode, locked_page,
1453 delalloc_start,
1454 ((u64)(start_index + pages_locked - 1)) <<
1455 PAGE_CACHE_SHIFT);
1456 }
1457 return ret;
1458}
1459
1460/*
1461 * find a contiguous range of bytes in the file marked as delalloc, not
1462 * more than 'max_bytes'. start and end are used to return the range,
1463 *
1464 * 1 is returned if we find something, 0 if nothing was in the tree
1465 */
1466static noinline u64 find_lock_delalloc_range(struct inode *inode,
1467 struct extent_io_tree *tree,
1468 struct page *locked_page,
1469 u64 *start, u64 *end,
1470 u64 max_bytes)
1471{
1472 u64 delalloc_start;
1473 u64 delalloc_end;
1474 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001475 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001476 int ret;
1477 int loops = 0;
1478
1479again:
1480 /* step one, find a bunch of delalloc bytes starting at start */
1481 delalloc_start = *start;
1482 delalloc_end = 0;
1483 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001484 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001485 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001486 *start = delalloc_start;
1487 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001488 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001489 return found;
1490 }
1491
1492 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001493 * start comes from the offset of locked_page. We have to lock
1494 * pages in order, so we can't process delalloc bytes before
1495 * locked_page
1496 */
Chris Masond3977122009-01-05 21:25:51 -05001497 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001498 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001499
1500 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001501 * make sure to limit the number of pages we try to lock down
1502 * if we're looping.
1503 */
Chris Masond3977122009-01-05 21:25:51 -05001504 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001505 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001506
Chris Masonc8b97812008-10-29 14:49:59 -04001507 /* step two, lock all the pages after the page that has start */
1508 ret = lock_delalloc_pages(inode, locked_page,
1509 delalloc_start, delalloc_end);
1510 if (ret == -EAGAIN) {
1511 /* some of the pages are gone, lets avoid looping by
1512 * shortening the size of the delalloc range we're searching
1513 */
Chris Mason9655d292009-09-02 15:22:30 -04001514 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001515 if (!loops) {
1516 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1517 max_bytes = PAGE_CACHE_SIZE - offset;
1518 loops = 1;
1519 goto again;
1520 } else {
1521 found = 0;
1522 goto out_failed;
1523 }
1524 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001525 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
Chris Masonc8b97812008-10-29 14:49:59 -04001526
1527 /* step three, lock the state bits for the whole range */
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001528 lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001529
1530 /* then test to make sure it is all still delalloc */
1531 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001532 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001533 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001534 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1535 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001536 __unlock_for_delalloc(inode, locked_page,
1537 delalloc_start, delalloc_end);
1538 cond_resched();
1539 goto again;
1540 }
Chris Mason9655d292009-09-02 15:22:30 -04001541 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001542 *start = delalloc_start;
1543 *end = delalloc_end;
1544out_failed:
1545 return found;
1546}
1547
1548int extent_clear_unlock_delalloc(struct inode *inode,
1549 struct extent_io_tree *tree,
1550 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001551 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001552{
1553 int ret;
1554 struct page *pages[16];
1555 unsigned long index = start >> PAGE_CACHE_SHIFT;
1556 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1557 unsigned long nr_pages = end_index - index + 1;
1558 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001559 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001560
Chris Masona791e352009-10-08 11:27:10 -04001561 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001562 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001563 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001564 clear_bits |= EXTENT_DIRTY;
1565
Chris Masona791e352009-10-08 11:27:10 -04001566 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001567 clear_bits |= EXTENT_DELALLOC;
1568
Chris Mason2c64c532009-09-02 15:04:12 -04001569 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001570 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1571 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1572 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001573 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001574
Chris Masond3977122009-01-05 21:25:51 -05001575 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001576 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001577 min_t(unsigned long,
1578 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001579 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001580
Chris Masona791e352009-10-08 11:27:10 -04001581 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001582 SetPagePrivate2(pages[i]);
1583
Chris Masonc8b97812008-10-29 14:49:59 -04001584 if (pages[i] == locked_page) {
1585 page_cache_release(pages[i]);
1586 continue;
1587 }
Chris Masona791e352009-10-08 11:27:10 -04001588 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001589 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001590 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001591 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001592 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001593 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001594 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001595 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001596 page_cache_release(pages[i]);
1597 }
1598 nr_pages -= ret;
1599 index += ret;
1600 cond_resched();
1601 }
1602 return 0;
1603}
Chris Masonc8b97812008-10-29 14:49:59 -04001604
Chris Masond352ac62008-09-29 15:18:18 -04001605/*
1606 * count the number of bytes in the tree that have a given bit(s)
1607 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1608 * cached. The total number found is returned.
1609 */
Chris Masond1310b22008-01-24 16:13:08 -05001610u64 count_range_bits(struct extent_io_tree *tree,
1611 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001612 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001613{
1614 struct rb_node *node;
1615 struct extent_state *state;
1616 u64 cur_start = *start;
1617 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001618 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001619 int found = 0;
1620
1621 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001622 WARN_ON(1);
1623 return 0;
1624 }
1625
Chris Masoncad321a2008-12-17 14:51:42 -05001626 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001627 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1628 total_bytes = tree->dirty_bytes;
1629 goto out;
1630 }
1631 /*
1632 * this search will find all the extents that end after
1633 * our range starts.
1634 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001635 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001636 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001637 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001638
Chris Masond3977122009-01-05 21:25:51 -05001639 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001640 state = rb_entry(node, struct extent_state, rb_node);
1641 if (state->start > search_end)
1642 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001643 if (contig && found && state->start > last + 1)
1644 break;
1645 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001646 total_bytes += min(search_end, state->end) + 1 -
1647 max(cur_start, state->start);
1648 if (total_bytes >= max_bytes)
1649 break;
1650 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001651 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001652 found = 1;
1653 }
Chris Masonec29ed52011-02-23 16:23:20 -05001654 last = state->end;
1655 } else if (contig && found) {
1656 break;
Chris Masond1310b22008-01-24 16:13:08 -05001657 }
1658 node = rb_next(node);
1659 if (!node)
1660 break;
1661 }
1662out:
Chris Masoncad321a2008-12-17 14:51:42 -05001663 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001664 return total_bytes;
1665}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001666
Chris Masond352ac62008-09-29 15:18:18 -04001667/*
1668 * set the private field for a given byte offset in the tree. If there isn't
1669 * an extent_state there already, this does nothing.
1670 */
Chris Masond1310b22008-01-24 16:13:08 -05001671int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1672{
1673 struct rb_node *node;
1674 struct extent_state *state;
1675 int ret = 0;
1676
Chris Masoncad321a2008-12-17 14:51:42 -05001677 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001678 /*
1679 * this search will find all the extents that end after
1680 * our range starts.
1681 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001682 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001683 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001684 ret = -ENOENT;
1685 goto out;
1686 }
1687 state = rb_entry(node, struct extent_state, rb_node);
1688 if (state->start != start) {
1689 ret = -ENOENT;
1690 goto out;
1691 }
1692 state->private = private;
1693out:
Chris Masoncad321a2008-12-17 14:51:42 -05001694 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001695 return ret;
1696}
1697
1698int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1699{
1700 struct rb_node *node;
1701 struct extent_state *state;
1702 int ret = 0;
1703
Chris Masoncad321a2008-12-17 14:51:42 -05001704 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001705 /*
1706 * this search will find all the extents that end after
1707 * our range starts.
1708 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001709 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001710 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001711 ret = -ENOENT;
1712 goto out;
1713 }
1714 state = rb_entry(node, struct extent_state, rb_node);
1715 if (state->start != start) {
1716 ret = -ENOENT;
1717 goto out;
1718 }
1719 *private = state->private;
1720out:
Chris Masoncad321a2008-12-17 14:51:42 -05001721 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001722 return ret;
1723}
1724
1725/*
1726 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001727 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001728 * has the bits set. Otherwise, 1 is returned if any bit in the
1729 * range is found set.
1730 */
1731int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001732 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001733{
1734 struct extent_state *state = NULL;
1735 struct rb_node *node;
1736 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001737
Chris Masoncad321a2008-12-17 14:51:42 -05001738 spin_lock(&tree->lock);
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001739 if (cached && cached->tree && cached->start <= start &&
1740 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001741 node = &cached->rb_node;
1742 else
1743 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001744 while (node && start <= end) {
1745 state = rb_entry(node, struct extent_state, rb_node);
1746
1747 if (filled && state->start > start) {
1748 bitset = 0;
1749 break;
1750 }
1751
1752 if (state->start > end)
1753 break;
1754
1755 if (state->state & bits) {
1756 bitset = 1;
1757 if (!filled)
1758 break;
1759 } else if (filled) {
1760 bitset = 0;
1761 break;
1762 }
Chris Mason46562ce2009-09-23 20:23:16 -04001763
1764 if (state->end == (u64)-1)
1765 break;
1766
Chris Masond1310b22008-01-24 16:13:08 -05001767 start = state->end + 1;
1768 if (start > end)
1769 break;
1770 node = rb_next(node);
1771 if (!node) {
1772 if (filled)
1773 bitset = 0;
1774 break;
1775 }
1776 }
Chris Masoncad321a2008-12-17 14:51:42 -05001777 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001778 return bitset;
1779}
Chris Masond1310b22008-01-24 16:13:08 -05001780
1781/*
1782 * helper function to set a given page up to date if all the
1783 * extents in the tree for that page are up to date
1784 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001785static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001786{
1787 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1788 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001789 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001790 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001791}
1792
1793/*
1794 * helper function to unlock a page if all the extents in the tree
1795 * for that page are unlocked
1796 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001797static void check_page_locked(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001798{
1799 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1800 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001801 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001802 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05001803}
1804
1805/*
1806 * helper function to end page writeback if all the extents
1807 * in the tree for that page are done with writeback
1808 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001809static void check_page_writeback(struct extent_io_tree *tree,
1810 struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001811{
Chris Mason1edbb732009-09-02 13:24:36 -04001812 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001813}
1814
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001815/*
1816 * When IO fails, either with EIO or csum verification fails, we
1817 * try other mirrors that might have a good copy of the data. This
1818 * io_failure_record is used to record state as we go through all the
1819 * mirrors. If another mirror has good data, the page is set up to date
1820 * and things continue. If a good mirror can't be found, the original
1821 * bio end_io callback is called to indicate things have failed.
1822 */
1823struct io_failure_record {
1824 struct page *page;
1825 u64 start;
1826 u64 len;
1827 u64 logical;
1828 unsigned long bio_flags;
1829 int this_mirror;
1830 int failed_mirror;
1831 int in_validation;
1832};
1833
1834static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
1835 int did_repair)
1836{
1837 int ret;
1838 int err = 0;
1839 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1840
1841 set_state_private(failure_tree, rec->start, 0);
1842 ret = clear_extent_bits(failure_tree, rec->start,
1843 rec->start + rec->len - 1,
1844 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1845 if (ret)
1846 err = ret;
1847
1848 if (did_repair) {
1849 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1850 rec->start + rec->len - 1,
1851 EXTENT_DAMAGED, GFP_NOFS);
1852 if (ret && !err)
1853 err = ret;
1854 }
1855
1856 kfree(rec);
1857 return err;
1858}
1859
1860static void repair_io_failure_callback(struct bio *bio, int err)
1861{
1862 complete(bio->bi_private);
1863}
1864
1865/*
1866 * this bypasses the standard btrfs submit functions deliberately, as
1867 * the standard behavior is to write all copies in a raid setup. here we only
1868 * want to write the one bad copy. so we do the mapping for ourselves and issue
1869 * submit_bio directly.
1870 * to avoid any synchonization issues, wait for the data after writing, which
1871 * actually prevents the read that triggered the error from finishing.
1872 * currently, there can be no more than two copies of every data bit. thus,
1873 * exactly one rewrite is required.
1874 */
1875int repair_io_failure(struct btrfs_mapping_tree *map_tree, u64 start,
1876 u64 length, u64 logical, struct page *page,
1877 int mirror_num)
1878{
1879 struct bio *bio;
1880 struct btrfs_device *dev;
1881 DECLARE_COMPLETION_ONSTACK(compl);
1882 u64 map_length = 0;
1883 u64 sector;
1884 struct btrfs_bio *bbio = NULL;
1885 int ret;
1886
1887 BUG_ON(!mirror_num);
1888
1889 bio = bio_alloc(GFP_NOFS, 1);
1890 if (!bio)
1891 return -EIO;
1892 bio->bi_private = &compl;
1893 bio->bi_end_io = repair_io_failure_callback;
1894 bio->bi_size = 0;
1895 map_length = length;
1896
1897 ret = btrfs_map_block(map_tree, WRITE, logical,
1898 &map_length, &bbio, mirror_num);
1899 if (ret) {
1900 bio_put(bio);
1901 return -EIO;
1902 }
1903 BUG_ON(mirror_num != bbio->mirror_num);
1904 sector = bbio->stripes[mirror_num-1].physical >> 9;
1905 bio->bi_sector = sector;
1906 dev = bbio->stripes[mirror_num-1].dev;
1907 kfree(bbio);
1908 if (!dev || !dev->bdev || !dev->writeable) {
1909 bio_put(bio);
1910 return -EIO;
1911 }
1912 bio->bi_bdev = dev->bdev;
1913 bio_add_page(bio, page, length, start-page_offset(page));
Stefan Behrens21adbd52011-11-09 13:44:05 +01001914 btrfsic_submit_bio(WRITE_SYNC, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001915 wait_for_completion(&compl);
1916
1917 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
1918 /* try to remap that extent elsewhere? */
1919 bio_put(bio);
1920 return -EIO;
1921 }
1922
1923 printk(KERN_INFO "btrfs read error corrected: ino %lu off %llu (dev %s "
1924 "sector %llu)\n", page->mapping->host->i_ino, start,
1925 dev->name, sector);
1926
1927 bio_put(bio);
1928 return 0;
1929}
1930
Josef Bacikea466792012-03-26 21:57:36 -04001931int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
1932 int mirror_num)
1933{
1934 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1935 u64 start = eb->start;
1936 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond95603b2012-04-12 15:55:15 -04001937 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04001938
1939 for (i = 0; i < num_pages; i++) {
1940 struct page *p = extent_buffer_page(eb, i);
1941 ret = repair_io_failure(map_tree, start, PAGE_CACHE_SIZE,
1942 start, p, mirror_num);
1943 if (ret)
1944 break;
1945 start += PAGE_CACHE_SIZE;
1946 }
1947
1948 return ret;
1949}
1950
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001951/*
1952 * each time an IO finishes, we do a fast check in the IO failure tree
1953 * to see if we need to process or clean up an io_failure_record
1954 */
1955static int clean_io_failure(u64 start, struct page *page)
1956{
1957 u64 private;
1958 u64 private_failure;
1959 struct io_failure_record *failrec;
1960 struct btrfs_mapping_tree *map_tree;
1961 struct extent_state *state;
1962 int num_copies;
1963 int did_repair = 0;
1964 int ret;
1965 struct inode *inode = page->mapping->host;
1966
1967 private = 0;
1968 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1969 (u64)-1, 1, EXTENT_DIRTY, 0);
1970 if (!ret)
1971 return 0;
1972
1973 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
1974 &private_failure);
1975 if (ret)
1976 return 0;
1977
1978 failrec = (struct io_failure_record *)(unsigned long) private_failure;
1979 BUG_ON(!failrec->this_mirror);
1980
1981 if (failrec->in_validation) {
1982 /* there was no real error, just free the record */
1983 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
1984 failrec->start);
1985 did_repair = 1;
1986 goto out;
1987 }
1988
1989 spin_lock(&BTRFS_I(inode)->io_tree.lock);
1990 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1991 failrec->start,
1992 EXTENT_LOCKED);
1993 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
1994
1995 if (state && state->start == failrec->start) {
1996 map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree;
1997 num_copies = btrfs_num_copies(map_tree, failrec->logical,
1998 failrec->len);
1999 if (num_copies > 1) {
2000 ret = repair_io_failure(map_tree, start, failrec->len,
2001 failrec->logical, page,
2002 failrec->failed_mirror);
2003 did_repair = !ret;
2004 }
2005 }
2006
2007out:
2008 if (!ret)
2009 ret = free_io_failure(inode, failrec, did_repair);
2010
2011 return ret;
2012}
2013
2014/*
2015 * this is a generic handler for readpage errors (default
2016 * readpage_io_failed_hook). if other copies exist, read those and write back
2017 * good data to the failed position. does not investigate in remapping the
2018 * failed extent elsewhere, hoping the device will be smart enough to do this as
2019 * needed
2020 */
2021
2022static int bio_readpage_error(struct bio *failed_bio, struct page *page,
2023 u64 start, u64 end, int failed_mirror,
2024 struct extent_state *state)
2025{
2026 struct io_failure_record *failrec = NULL;
2027 u64 private;
2028 struct extent_map *em;
2029 struct inode *inode = page->mapping->host;
2030 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2031 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2032 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2033 struct bio *bio;
2034 int num_copies;
2035 int ret;
2036 int read_mode;
2037 u64 logical;
2038
2039 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2040
2041 ret = get_state_private(failure_tree, start, &private);
2042 if (ret) {
2043 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2044 if (!failrec)
2045 return -ENOMEM;
2046 failrec->start = start;
2047 failrec->len = end - start + 1;
2048 failrec->this_mirror = 0;
2049 failrec->bio_flags = 0;
2050 failrec->in_validation = 0;
2051
2052 read_lock(&em_tree->lock);
2053 em = lookup_extent_mapping(em_tree, start, failrec->len);
2054 if (!em) {
2055 read_unlock(&em_tree->lock);
2056 kfree(failrec);
2057 return -EIO;
2058 }
2059
2060 if (em->start > start || em->start + em->len < start) {
2061 free_extent_map(em);
2062 em = NULL;
2063 }
2064 read_unlock(&em_tree->lock);
2065
2066 if (!em || IS_ERR(em)) {
2067 kfree(failrec);
2068 return -EIO;
2069 }
2070 logical = start - em->start;
2071 logical = em->block_start + logical;
2072 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2073 logical = em->block_start;
2074 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2075 extent_set_compress_type(&failrec->bio_flags,
2076 em->compress_type);
2077 }
2078 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2079 "len=%llu\n", logical, start, failrec->len);
2080 failrec->logical = logical;
2081 free_extent_map(em);
2082
2083 /* set the bits in the private failure tree */
2084 ret = set_extent_bits(failure_tree, start, end,
2085 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2086 if (ret >= 0)
2087 ret = set_state_private(failure_tree, start,
2088 (u64)(unsigned long)failrec);
2089 /* set the bits in the inode's tree */
2090 if (ret >= 0)
2091 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2092 GFP_NOFS);
2093 if (ret < 0) {
2094 kfree(failrec);
2095 return ret;
2096 }
2097 } else {
2098 failrec = (struct io_failure_record *)(unsigned long)private;
2099 pr_debug("bio_readpage_error: (found) logical=%llu, "
2100 "start=%llu, len=%llu, validation=%d\n",
2101 failrec->logical, failrec->start, failrec->len,
2102 failrec->in_validation);
2103 /*
2104 * when data can be on disk more than twice, add to failrec here
2105 * (e.g. with a list for failed_mirror) to make
2106 * clean_io_failure() clean all those errors at once.
2107 */
2108 }
2109 num_copies = btrfs_num_copies(
2110 &BTRFS_I(inode)->root->fs_info->mapping_tree,
2111 failrec->logical, failrec->len);
2112 if (num_copies == 1) {
2113 /*
2114 * we only have a single copy of the data, so don't bother with
2115 * all the retry and error correction code that follows. no
2116 * matter what the error is, it is very likely to persist.
2117 */
2118 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2119 "state=%p, num_copies=%d, next_mirror %d, "
2120 "failed_mirror %d\n", state, num_copies,
2121 failrec->this_mirror, failed_mirror);
2122 free_io_failure(inode, failrec, 0);
2123 return -EIO;
2124 }
2125
2126 if (!state) {
2127 spin_lock(&tree->lock);
2128 state = find_first_extent_bit_state(tree, failrec->start,
2129 EXTENT_LOCKED);
2130 if (state && state->start != failrec->start)
2131 state = NULL;
2132 spin_unlock(&tree->lock);
2133 }
2134
2135 /*
2136 * there are two premises:
2137 * a) deliver good data to the caller
2138 * b) correct the bad sectors on disk
2139 */
2140 if (failed_bio->bi_vcnt > 1) {
2141 /*
2142 * to fulfill b), we need to know the exact failing sectors, as
2143 * we don't want to rewrite any more than the failed ones. thus,
2144 * we need separate read requests for the failed bio
2145 *
2146 * if the following BUG_ON triggers, our validation request got
2147 * merged. we need separate requests for our algorithm to work.
2148 */
2149 BUG_ON(failrec->in_validation);
2150 failrec->in_validation = 1;
2151 failrec->this_mirror = failed_mirror;
2152 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2153 } else {
2154 /*
2155 * we're ready to fulfill a) and b) alongside. get a good copy
2156 * of the failed sector and if we succeed, we have setup
2157 * everything for repair_io_failure to do the rest for us.
2158 */
2159 if (failrec->in_validation) {
2160 BUG_ON(failrec->this_mirror != failed_mirror);
2161 failrec->in_validation = 0;
2162 failrec->this_mirror = 0;
2163 }
2164 failrec->failed_mirror = failed_mirror;
2165 failrec->this_mirror++;
2166 if (failrec->this_mirror == failed_mirror)
2167 failrec->this_mirror++;
2168 read_mode = READ_SYNC;
2169 }
2170
2171 if (!state || failrec->this_mirror > num_copies) {
2172 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2173 "next_mirror %d, failed_mirror %d\n", state,
2174 num_copies, failrec->this_mirror, failed_mirror);
2175 free_io_failure(inode, failrec, 0);
2176 return -EIO;
2177 }
2178
2179 bio = bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04002180 if (!bio) {
2181 free_io_failure(inode, failrec, 0);
2182 return -EIO;
2183 }
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002184 bio->bi_private = state;
2185 bio->bi_end_io = failed_bio->bi_end_io;
2186 bio->bi_sector = failrec->logical >> 9;
2187 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2188 bio->bi_size = 0;
2189
2190 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2191
2192 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2193 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2194 failrec->this_mirror, num_copies, failrec->in_validation);
2195
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002196 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2197 failrec->this_mirror,
2198 failrec->bio_flags, 0);
2199 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002200}
2201
Chris Masond1310b22008-01-24 16:13:08 -05002202/* lots and lots of room for performance fixes in the end_bio funcs */
2203
Jeff Mahoney87826df2012-02-15 16:23:57 +01002204int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2205{
2206 int uptodate = (err == 0);
2207 struct extent_io_tree *tree;
2208 int ret;
2209
2210 tree = &BTRFS_I(page->mapping->host)->io_tree;
2211
2212 if (tree->ops && tree->ops->writepage_end_io_hook) {
2213 ret = tree->ops->writepage_end_io_hook(page, start,
2214 end, NULL, uptodate);
2215 if (ret)
2216 uptodate = 0;
2217 }
2218
2219 if (!uptodate && tree->ops &&
2220 tree->ops->writepage_io_failed_hook) {
2221 ret = tree->ops->writepage_io_failed_hook(NULL, page,
2222 start, end, NULL);
2223 /* Writeback already completed */
2224 if (ret == 0)
2225 return 1;
2226 }
2227
2228 if (!uptodate) {
2229 clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
2230 ClearPageUptodate(page);
2231 SetPageError(page);
2232 }
2233 return 0;
2234}
2235
Chris Masond1310b22008-01-24 16:13:08 -05002236/*
2237 * after a writepage IO is done, we need to:
2238 * clear the uptodate bits on error
2239 * clear the writeback bits in the extent tree for this IO
2240 * end_page_writeback if the page has no more pending IO
2241 *
2242 * Scheduling is not allowed, so the extent state tree is expected
2243 * to have one and only one object corresponding to this IO.
2244 */
Chris Masond1310b22008-01-24 16:13:08 -05002245static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002246{
Chris Masond1310b22008-01-24 16:13:08 -05002247 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04002248 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002249 u64 start;
2250 u64 end;
2251 int whole_page;
2252
Chris Masond1310b22008-01-24 16:13:08 -05002253 do {
2254 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04002255 tree = &BTRFS_I(page->mapping->host)->io_tree;
2256
Chris Masond1310b22008-01-24 16:13:08 -05002257 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2258 bvec->bv_offset;
2259 end = start + bvec->bv_len - 1;
2260
2261 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2262 whole_page = 1;
2263 else
2264 whole_page = 0;
2265
2266 if (--bvec >= bio->bi_io_vec)
2267 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04002268
Jeff Mahoney87826df2012-02-15 16:23:57 +01002269 if (end_extent_writepage(page, err, start, end))
2270 continue;
Chris Mason70dec802008-01-29 09:59:12 -05002271
Chris Masond1310b22008-01-24 16:13:08 -05002272 if (whole_page)
2273 end_page_writeback(page);
2274 else
2275 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002276 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04002277
Chris Masond1310b22008-01-24 16:13:08 -05002278 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002279}
2280
2281/*
2282 * after a readpage IO is done, we need to:
2283 * clear the uptodate bits on error
2284 * set the uptodate bits if things worked
2285 * set the page up to date if all extents in the tree are uptodate
2286 * clear the lock bit in the extent tree
2287 * unlock the page if there are no other extents locked for it
2288 *
2289 * Scheduling is not allowed, so the extent state tree is expected
2290 * to have one and only one object corresponding to this IO.
2291 */
Chris Masond1310b22008-01-24 16:13:08 -05002292static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002293{
2294 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00002295 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2296 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04002297 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002298 u64 start;
2299 u64 end;
2300 int whole_page;
Josef Bacikea466792012-03-26 21:57:36 -04002301 int failed_mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002302 int ret;
2303
Chris Masond20f7042008-12-08 16:58:54 -05002304 if (err)
2305 uptodate = 0;
2306
Chris Masond1310b22008-01-24 16:13:08 -05002307 do {
2308 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00002309 struct extent_state *cached = NULL;
2310 struct extent_state *state;
2311
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002312 pr_debug("end_bio_extent_readpage: bi_vcnt=%d, idx=%d, err=%d, "
2313 "mirror=%ld\n", bio->bi_vcnt, bio->bi_idx, err,
2314 (long int)bio->bi_bdev);
David Woodhouse902b22f2008-08-20 08:51:49 -04002315 tree = &BTRFS_I(page->mapping->host)->io_tree;
2316
Chris Masond1310b22008-01-24 16:13:08 -05002317 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2318 bvec->bv_offset;
2319 end = start + bvec->bv_len - 1;
2320
2321 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2322 whole_page = 1;
2323 else
2324 whole_page = 0;
2325
Chris Mason4125bf72010-02-03 18:18:45 +00002326 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05002327 prefetchw(&bvec->bv_page->flags);
2328
Arne Jansen507903b2011-04-06 10:02:20 +00002329 spin_lock(&tree->lock);
Chris Mason0d399202011-04-16 06:55:39 -04002330 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
Chris Mason109b36a2011-04-12 13:57:39 -04002331 if (state && state->start == start) {
Arne Jansen507903b2011-04-06 10:02:20 +00002332 /*
2333 * take a reference on the state, unlock will drop
2334 * the ref
2335 */
2336 cache_state(state, &cached);
2337 }
2338 spin_unlock(&tree->lock);
2339
Chris Masond1310b22008-01-24 16:13:08 -05002340 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05002341 ret = tree->ops->readpage_end_io_hook(page, start, end,
Arne Jansen507903b2011-04-06 10:02:20 +00002342 state);
Chris Masond1310b22008-01-24 16:13:08 -05002343 if (ret)
2344 uptodate = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002345 else
2346 clean_io_failure(start, page);
Chris Masond1310b22008-01-24 16:13:08 -05002347 }
Josef Bacikea466792012-03-26 21:57:36 -04002348
2349 if (!uptodate)
Jan Schmidt32240a92011-11-20 07:33:38 -05002350 failed_mirror = (int)(unsigned long)bio->bi_bdev;
Josef Bacikea466792012-03-26 21:57:36 -04002351
2352 if (!uptodate && tree->ops && tree->ops->readpage_io_failed_hook) {
2353 ret = tree->ops->readpage_io_failed_hook(page, failed_mirror);
2354 if (!ret && !err &&
2355 test_bit(BIO_UPTODATE, &bio->bi_flags))
2356 uptodate = 1;
2357 } else if (!uptodate) {
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002358 /*
2359 * The generic bio_readpage_error handles errors the
2360 * following way: If possible, new read requests are
2361 * created and submitted and will end up in
2362 * end_bio_extent_readpage as well (if we're lucky, not
2363 * in the !uptodate case). In that case it returns 0 and
2364 * we just go on with the next page in our bio. If it
2365 * can't handle the error it will return -EIO and we
2366 * remain responsible for that page.
2367 */
2368 ret = bio_readpage_error(bio, page, start, end,
2369 failed_mirror, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04002370 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04002371 uptodate =
2372 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05002373 if (err)
2374 uptodate = 0;
Arne Jansen507903b2011-04-06 10:02:20 +00002375 uncache_state(&cached);
Chris Mason7e383262008-04-09 16:28:12 -04002376 continue;
2377 }
2378 }
Chris Mason70dec802008-01-29 09:59:12 -05002379
Josef Bacik0b32f4b2012-03-13 09:38:00 -04002380 if (uptodate && tree->track_uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00002381 set_extent_uptodate(tree, start, end, &cached,
David Woodhouse902b22f2008-08-20 08:51:49 -04002382 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05002383 }
Arne Jansen507903b2011-04-06 10:02:20 +00002384 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05002385
Chris Mason70dec802008-01-29 09:59:12 -05002386 if (whole_page) {
2387 if (uptodate) {
2388 SetPageUptodate(page);
2389 } else {
2390 ClearPageUptodate(page);
2391 SetPageError(page);
2392 }
Chris Masond1310b22008-01-24 16:13:08 -05002393 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05002394 } else {
2395 if (uptodate) {
2396 check_page_uptodate(tree, page);
2397 } else {
2398 ClearPageUptodate(page);
2399 SetPageError(page);
2400 }
Chris Masond1310b22008-01-24 16:13:08 -05002401 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05002402 }
Chris Mason4125bf72010-02-03 18:18:45 +00002403 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05002404
2405 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002406}
2407
Miao Xie88f794e2010-11-22 03:02:55 +00002408struct bio *
2409btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2410 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002411{
2412 struct bio *bio;
2413
2414 bio = bio_alloc(gfp_flags, nr_vecs);
2415
2416 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2417 while (!bio && (nr_vecs /= 2))
2418 bio = bio_alloc(gfp_flags, nr_vecs);
2419 }
2420
2421 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04002422 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002423 bio->bi_bdev = bdev;
2424 bio->bi_sector = first_sector;
2425 }
2426 return bio;
2427}
2428
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002429/*
2430 * Since writes are async, they will only return -ENOMEM.
2431 * Reads can return the full range of I/O error conditions.
2432 */
Jeff Mahoney355808c2011-10-03 23:23:14 -04002433static int __must_check submit_one_bio(int rw, struct bio *bio,
2434 int mirror_num, unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002435{
Chris Masond1310b22008-01-24 16:13:08 -05002436 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002437 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2438 struct page *page = bvec->bv_page;
2439 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002440 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002441
2442 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002443
David Woodhouse902b22f2008-08-20 08:51:49 -04002444 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002445
2446 bio_get(bio);
2447
Chris Mason065631f2008-02-20 12:07:25 -05002448 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00002449 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002450 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002451 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01002452 btrfsic_submit_bio(rw, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002453
Chris Masond1310b22008-01-24 16:13:08 -05002454 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2455 ret = -EOPNOTSUPP;
2456 bio_put(bio);
2457 return ret;
2458}
2459
Jeff Mahoney3444a972011-10-03 23:23:13 -04002460static int merge_bio(struct extent_io_tree *tree, struct page *page,
2461 unsigned long offset, size_t size, struct bio *bio,
2462 unsigned long bio_flags)
2463{
2464 int ret = 0;
2465 if (tree->ops && tree->ops->merge_bio_hook)
2466 ret = tree->ops->merge_bio_hook(page, offset, size, bio,
2467 bio_flags);
2468 BUG_ON(ret < 0);
2469 return ret;
2470
2471}
2472
Chris Masond1310b22008-01-24 16:13:08 -05002473static int submit_extent_page(int rw, struct extent_io_tree *tree,
2474 struct page *page, sector_t sector,
2475 size_t size, unsigned long offset,
2476 struct block_device *bdev,
2477 struct bio **bio_ret,
2478 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04002479 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002480 int mirror_num,
2481 unsigned long prev_bio_flags,
2482 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002483{
2484 int ret = 0;
2485 struct bio *bio;
2486 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04002487 int contig = 0;
2488 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2489 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05002490 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002491
2492 if (bio_ret && *bio_ret) {
2493 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002494 if (old_compressed)
2495 contig = bio->bi_sector == sector;
2496 else
2497 contig = bio->bi_sector + (bio->bi_size >> 9) ==
2498 sector;
2499
2500 if (prev_bio_flags != bio_flags || !contig ||
Jeff Mahoney3444a972011-10-03 23:23:13 -04002501 merge_bio(tree, page, offset, page_size, bio, bio_flags) ||
Chris Masonc8b97812008-10-29 14:49:59 -04002502 bio_add_page(bio, page, page_size, offset) < page_size) {
2503 ret = submit_one_bio(rw, bio, mirror_num,
2504 prev_bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002505 if (ret < 0)
2506 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05002507 bio = NULL;
2508 } else {
2509 return 0;
2510 }
2511 }
Chris Masonc8b97812008-10-29 14:49:59 -04002512 if (this_compressed)
2513 nr = BIO_MAX_PAGES;
2514 else
2515 nr = bio_get_nr_vecs(bdev);
2516
Miao Xie88f794e2010-11-22 03:02:55 +00002517 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002518 if (!bio)
2519 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002520
Chris Masonc8b97812008-10-29 14:49:59 -04002521 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002522 bio->bi_end_io = end_io_func;
2523 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05002524
Chris Masond3977122009-01-05 21:25:51 -05002525 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002526 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002527 else
Chris Masonc8b97812008-10-29 14:49:59 -04002528 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002529
2530 return ret;
2531}
2532
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002533void attach_extent_buffer_page(struct extent_buffer *eb, struct page *page)
2534{
2535 if (!PagePrivate(page)) {
2536 SetPagePrivate(page);
2537 page_cache_get(page);
2538 set_page_private(page, (unsigned long)eb);
2539 } else {
2540 WARN_ON(page->private != (unsigned long)eb);
2541 }
2542}
2543
Chris Masond1310b22008-01-24 16:13:08 -05002544void set_page_extent_mapped(struct page *page)
2545{
2546 if (!PagePrivate(page)) {
2547 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002548 page_cache_get(page);
Chris Mason6af118c2008-07-22 11:18:07 -04002549 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002550 }
2551}
2552
Chris Masond1310b22008-01-24 16:13:08 -05002553/*
2554 * basic readpage implementation. Locked extent state structs are inserted
2555 * into the tree that are removed when the IO is done (by the end_io
2556 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002557 * XXX JDM: This needs looking at to ensure proper page locking
Chris Masond1310b22008-01-24 16:13:08 -05002558 */
2559static int __extent_read_full_page(struct extent_io_tree *tree,
2560 struct page *page,
2561 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002562 struct bio **bio, int mirror_num,
2563 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002564{
2565 struct inode *inode = page->mapping->host;
2566 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2567 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2568 u64 end;
2569 u64 cur = start;
2570 u64 extent_offset;
2571 u64 last_byte = i_size_read(inode);
2572 u64 block_start;
2573 u64 cur_end;
2574 sector_t sector;
2575 struct extent_map *em;
2576 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002577 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05002578 int ret;
2579 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002580 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002581 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002582 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002583 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002584 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002585
2586 set_page_extent_mapped(page);
2587
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002588 if (!PageUptodate(page)) {
2589 if (cleancache_get_page(page) == 0) {
2590 BUG_ON(blocksize != PAGE_SIZE);
2591 goto out;
2592 }
2593 }
2594
Chris Masond1310b22008-01-24 16:13:08 -05002595 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002596 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002597 lock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002598 ordered = btrfs_lookup_ordered_extent(inode, start);
2599 if (!ordered)
2600 break;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002601 unlock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002602 btrfs_start_ordered_extent(inode, ordered, 1);
2603 btrfs_put_ordered_extent(ordered);
2604 }
Chris Masond1310b22008-01-24 16:13:08 -05002605
Chris Masonc8b97812008-10-29 14:49:59 -04002606 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2607 char *userpage;
2608 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2609
2610 if (zero_offset) {
2611 iosize = PAGE_CACHE_SIZE - zero_offset;
2612 userpage = kmap_atomic(page, KM_USER0);
2613 memset(userpage + zero_offset, 0, iosize);
2614 flush_dcache_page(page);
2615 kunmap_atomic(userpage, KM_USER0);
2616 }
2617 }
Chris Masond1310b22008-01-24 16:13:08 -05002618 while (cur <= end) {
2619 if (cur >= last_byte) {
2620 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002621 struct extent_state *cached = NULL;
2622
David Sterba306e16c2011-04-19 14:29:38 +02002623 iosize = PAGE_CACHE_SIZE - pg_offset;
Chris Masond1310b22008-01-24 16:13:08 -05002624 userpage = kmap_atomic(page, KM_USER0);
David Sterba306e16c2011-04-19 14:29:38 +02002625 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002626 flush_dcache_page(page);
2627 kunmap_atomic(userpage, KM_USER0);
2628 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002629 &cached, GFP_NOFS);
2630 unlock_extent_cached(tree, cur, cur + iosize - 1,
2631 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002632 break;
2633 }
David Sterba306e16c2011-04-19 14:29:38 +02002634 em = get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002635 end - cur + 1, 0);
David Sterbac7040052011-04-19 18:00:01 +02002636 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002637 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002638 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002639 break;
2640 }
Chris Masond1310b22008-01-24 16:13:08 -05002641 extent_offset = cur - em->start;
2642 BUG_ON(extent_map_end(em) <= cur);
2643 BUG_ON(end < cur);
2644
Li Zefan261507a02010-12-17 14:21:50 +08002645 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002646 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002647 extent_set_compress_type(&this_bio_flag,
2648 em->compress_type);
2649 }
Chris Masonc8b97812008-10-29 14:49:59 -04002650
Chris Masond1310b22008-01-24 16:13:08 -05002651 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2652 cur_end = min(extent_map_end(em) - 1, end);
2653 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002654 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2655 disk_io_size = em->block_len;
2656 sector = em->block_start >> 9;
2657 } else {
2658 sector = (em->block_start + extent_offset) >> 9;
2659 disk_io_size = iosize;
2660 }
Chris Masond1310b22008-01-24 16:13:08 -05002661 bdev = em->bdev;
2662 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002663 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2664 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002665 free_extent_map(em);
2666 em = NULL;
2667
2668 /* we've found a hole, just zero and go on */
2669 if (block_start == EXTENT_MAP_HOLE) {
2670 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002671 struct extent_state *cached = NULL;
2672
Chris Masond1310b22008-01-24 16:13:08 -05002673 userpage = kmap_atomic(page, KM_USER0);
David Sterba306e16c2011-04-19 14:29:38 +02002674 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002675 flush_dcache_page(page);
2676 kunmap_atomic(userpage, KM_USER0);
2677
2678 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002679 &cached, GFP_NOFS);
2680 unlock_extent_cached(tree, cur, cur + iosize - 1,
2681 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002682 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002683 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002684 continue;
2685 }
2686 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002687 if (test_range_bit(tree, cur, cur_end,
2688 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002689 check_page_uptodate(tree, page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002690 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002691 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002692 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002693 continue;
2694 }
Chris Mason70dec802008-01-29 09:59:12 -05002695 /* we have an inline extent but it didn't get marked up
2696 * to date. Error out
2697 */
2698 if (block_start == EXTENT_MAP_INLINE) {
2699 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002700 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05002701 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002702 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05002703 continue;
2704 }
Chris Masond1310b22008-01-24 16:13:08 -05002705
2706 ret = 0;
2707 if (tree->ops && tree->ops->readpage_io_hook) {
2708 ret = tree->ops->readpage_io_hook(page, cur,
2709 cur + iosize - 1);
2710 }
2711 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002712 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2713 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002714 ret = submit_extent_page(READ, tree, page,
David Sterba306e16c2011-04-19 14:29:38 +02002715 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04002716 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002717 end_bio_extent_readpage, mirror_num,
2718 *bio_flags,
2719 this_bio_flag);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002720 BUG_ON(ret == -ENOMEM);
Chris Mason89642222008-07-24 09:41:53 -04002721 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002722 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002723 }
2724 if (ret)
2725 SetPageError(page);
2726 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002727 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002728 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002729out:
Chris Masond1310b22008-01-24 16:13:08 -05002730 if (!nr) {
2731 if (!PageError(page))
2732 SetPageUptodate(page);
2733 unlock_page(page);
2734 }
2735 return 0;
2736}
2737
2738int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002739 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05002740{
2741 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002742 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002743 int ret;
2744
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002745 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Chris Masonc8b97812008-10-29 14:49:59 -04002746 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002747 if (bio)
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002748 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002749 return ret;
2750}
Chris Masond1310b22008-01-24 16:13:08 -05002751
Chris Mason11c83492009-04-20 15:50:09 -04002752static noinline void update_nr_written(struct page *page,
2753 struct writeback_control *wbc,
2754 unsigned long nr_written)
2755{
2756 wbc->nr_to_write -= nr_written;
2757 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2758 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2759 page->mapping->writeback_index = page->index + nr_written;
2760}
2761
Chris Masond1310b22008-01-24 16:13:08 -05002762/*
2763 * the writepage semantics are similar to regular writepage. extent
2764 * records are inserted to lock ranges in the tree, and as dirty areas
2765 * are found, they are marked writeback. Then the lock bits are removed
2766 * and the end_io handler clears the writeback ranges
2767 */
2768static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2769 void *data)
2770{
2771 struct inode *inode = page->mapping->host;
2772 struct extent_page_data *epd = data;
2773 struct extent_io_tree *tree = epd->tree;
2774 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2775 u64 delalloc_start;
2776 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2777 u64 end;
2778 u64 cur = start;
2779 u64 extent_offset;
2780 u64 last_byte = i_size_read(inode);
2781 u64 block_start;
2782 u64 iosize;
2783 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002784 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002785 struct extent_map *em;
2786 struct block_device *bdev;
2787 int ret;
2788 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002789 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002790 size_t blocksize;
2791 loff_t i_size = i_size_read(inode);
2792 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2793 u64 nr_delalloc;
2794 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002795 int page_started;
2796 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002797 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002798 unsigned long nr_written = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002799 bool fill_delalloc = true;
Chris Masond1310b22008-01-24 16:13:08 -05002800
Chris Masonffbd5172009-04-20 15:50:09 -04002801 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002802 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002803 else
2804 write_flags = WRITE;
2805
liubo1abe9b82011-03-24 11:18:59 +00002806 trace___extent_writepage(page, inode, wbc);
2807
Chris Masond1310b22008-01-24 16:13:08 -05002808 WARN_ON(!PageLocked(page));
Chris Masonbf0da8c2011-11-04 12:29:37 -04002809
2810 ClearPageError(page);
2811
Chris Mason7f3c74f2008-07-18 12:01:11 -04002812 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002813 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002814 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002815 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002816 unlock_page(page);
2817 return 0;
2818 }
2819
2820 if (page->index == end_index) {
2821 char *userpage;
2822
Chris Masond1310b22008-01-24 16:13:08 -05002823 userpage = kmap_atomic(page, KM_USER0);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002824 memset(userpage + pg_offset, 0,
2825 PAGE_CACHE_SIZE - pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002826 kunmap_atomic(userpage, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002827 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002828 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002829 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002830
2831 set_page_extent_mapped(page);
2832
Josef Bacik9e487102011-08-01 12:08:18 -04002833 if (!tree->ops || !tree->ops->fill_delalloc)
2834 fill_delalloc = false;
2835
Chris Masond1310b22008-01-24 16:13:08 -05002836 delalloc_start = start;
2837 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002838 page_started = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002839 if (!epd->extent_locked && fill_delalloc) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002840 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002841 /*
2842 * make sure the wbc mapping index is at least updated
2843 * to this page.
2844 */
2845 update_nr_written(page, wbc, 0);
2846
Chris Masond3977122009-01-05 21:25:51 -05002847 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002848 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002849 page,
2850 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002851 &delalloc_end,
2852 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002853 if (nr_delalloc == 0) {
2854 delalloc_start = delalloc_end + 1;
2855 continue;
2856 }
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002857 ret = tree->ops->fill_delalloc(inode, page,
2858 delalloc_start,
2859 delalloc_end,
2860 &page_started,
2861 &nr_written);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002862 /* File system has been set read-only */
2863 if (ret) {
2864 SetPageError(page);
2865 goto done;
2866 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002867 /*
2868 * delalloc_end is already one less than the total
2869 * length, so we don't subtract one from
2870 * PAGE_CACHE_SIZE
2871 */
2872 delalloc_to_write += (delalloc_end - delalloc_start +
2873 PAGE_CACHE_SIZE) >>
2874 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002875 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002876 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002877 if (wbc->nr_to_write < delalloc_to_write) {
2878 int thresh = 8192;
2879
2880 if (delalloc_to_write < thresh * 2)
2881 thresh = delalloc_to_write;
2882 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2883 thresh);
2884 }
Chris Masonc8b97812008-10-29 14:49:59 -04002885
Chris Mason771ed682008-11-06 22:02:51 -05002886 /* did the fill delalloc function already unlock and start
2887 * the IO?
2888 */
2889 if (page_started) {
2890 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002891 /*
2892 * we've unlocked the page, so we can't update
2893 * the mapping's writeback index, just update
2894 * nr_to_write.
2895 */
2896 wbc->nr_to_write -= nr_written;
2897 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002898 }
Chris Masonc8b97812008-10-29 14:49:59 -04002899 }
Chris Mason247e7432008-07-17 12:53:51 -04002900 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002901 ret = tree->ops->writepage_start_hook(page, start,
2902 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002903 if (ret) {
2904 /* Fixup worker will requeue */
2905 if (ret == -EBUSY)
2906 wbc->pages_skipped++;
2907 else
2908 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002909 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002910 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002911 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002912 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002913 }
2914 }
2915
Chris Mason11c83492009-04-20 15:50:09 -04002916 /*
2917 * we don't want to touch the inode after unlocking the page,
2918 * so we update the mapping writeback index now
2919 */
2920 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002921
Chris Masond1310b22008-01-24 16:13:08 -05002922 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002923 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002924 if (tree->ops && tree->ops->writepage_end_io_hook)
2925 tree->ops->writepage_end_io_hook(page, start,
2926 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002927 goto done;
2928 }
2929
Chris Masond1310b22008-01-24 16:13:08 -05002930 blocksize = inode->i_sb->s_blocksize;
2931
2932 while (cur <= end) {
2933 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002934 if (tree->ops && tree->ops->writepage_end_io_hook)
2935 tree->ops->writepage_end_io_hook(page, cur,
2936 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002937 break;
2938 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002939 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002940 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02002941 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002942 SetPageError(page);
2943 break;
2944 }
2945
2946 extent_offset = cur - em->start;
2947 BUG_ON(extent_map_end(em) <= cur);
2948 BUG_ON(end < cur);
2949 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2950 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2951 sector = (em->block_start + extent_offset) >> 9;
2952 bdev = em->bdev;
2953 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002954 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002955 free_extent_map(em);
2956 em = NULL;
2957
Chris Masonc8b97812008-10-29 14:49:59 -04002958 /*
2959 * compressed and inline extents are written through other
2960 * paths in the FS
2961 */
2962 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002963 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002964 /*
2965 * end_io notification does not happen here for
2966 * compressed extents
2967 */
2968 if (!compressed && tree->ops &&
2969 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002970 tree->ops->writepage_end_io_hook(page, cur,
2971 cur + iosize - 1,
2972 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002973 else if (compressed) {
2974 /* we don't want to end_page_writeback on
2975 * a compressed extent. this happens
2976 * elsewhere
2977 */
2978 nr++;
2979 }
2980
2981 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002982 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002983 continue;
2984 }
Chris Masond1310b22008-01-24 16:13:08 -05002985 /* leave this out until we have a page_mkwrite call */
2986 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04002987 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002988 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002989 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002990 continue;
2991 }
Chris Masonc8b97812008-10-29 14:49:59 -04002992
Chris Masond1310b22008-01-24 16:13:08 -05002993 if (tree->ops && tree->ops->writepage_io_hook) {
2994 ret = tree->ops->writepage_io_hook(page, cur,
2995 cur + iosize - 1);
2996 } else {
2997 ret = 0;
2998 }
Chris Mason1259ab72008-05-12 13:39:03 -04002999 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003000 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003001 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003002 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003003
Chris Masond1310b22008-01-24 16:13:08 -05003004 set_range_writeback(tree, cur, cur + iosize - 1);
3005 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05003006 printk(KERN_ERR "btrfs warning page %lu not "
3007 "writeback, cur %llu end %llu\n",
3008 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05003009 (unsigned long long)end);
3010 }
3011
Chris Masonffbd5172009-04-20 15:50:09 -04003012 ret = submit_extent_page(write_flags, tree, page,
3013 sector, iosize, pg_offset,
3014 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04003015 end_bio_extent_writepage,
3016 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003017 if (ret)
3018 SetPageError(page);
3019 }
3020 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003021 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003022 nr++;
3023 }
3024done:
3025 if (nr == 0) {
3026 /* make sure the mapping tag for page dirty gets cleared */
3027 set_page_writeback(page);
3028 end_page_writeback(page);
3029 }
Chris Masond1310b22008-01-24 16:13:08 -05003030 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05003031
Chris Mason11c83492009-04-20 15:50:09 -04003032done_unlocked:
3033
Chris Mason2c64c532009-09-02 15:04:12 -04003034 /* drop our reference on any cached states */
3035 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05003036 return 0;
3037}
3038
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003039static int eb_wait(void *word)
3040{
3041 io_schedule();
3042 return 0;
3043}
3044
3045static void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
3046{
3047 wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
3048 TASK_UNINTERRUPTIBLE);
3049}
3050
3051static int lock_extent_buffer_for_io(struct extent_buffer *eb,
3052 struct btrfs_fs_info *fs_info,
3053 struct extent_page_data *epd)
3054{
3055 unsigned long i, num_pages;
3056 int flush = 0;
3057 int ret = 0;
3058
3059 if (!btrfs_try_tree_write_lock(eb)) {
3060 flush = 1;
3061 flush_write_bio(epd);
3062 btrfs_tree_lock(eb);
3063 }
3064
3065 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3066 btrfs_tree_unlock(eb);
3067 if (!epd->sync_io)
3068 return 0;
3069 if (!flush) {
3070 flush_write_bio(epd);
3071 flush = 1;
3072 }
Chris Masona098d8e2012-03-21 12:09:56 -04003073 while (1) {
3074 wait_on_extent_buffer_writeback(eb);
3075 btrfs_tree_lock(eb);
3076 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3077 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003078 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003079 }
3080 }
3081
3082 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3083 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3084 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
3085 spin_lock(&fs_info->delalloc_lock);
3086 if (fs_info->dirty_metadata_bytes >= eb->len)
3087 fs_info->dirty_metadata_bytes -= eb->len;
3088 else
3089 WARN_ON(1);
3090 spin_unlock(&fs_info->delalloc_lock);
3091 ret = 1;
3092 }
3093
3094 btrfs_tree_unlock(eb);
3095
3096 if (!ret)
3097 return ret;
3098
3099 num_pages = num_extent_pages(eb->start, eb->len);
3100 for (i = 0; i < num_pages; i++) {
3101 struct page *p = extent_buffer_page(eb, i);
3102
3103 if (!trylock_page(p)) {
3104 if (!flush) {
3105 flush_write_bio(epd);
3106 flush = 1;
3107 }
3108 lock_page(p);
3109 }
3110 }
3111
3112 return ret;
3113}
3114
3115static void end_extent_buffer_writeback(struct extent_buffer *eb)
3116{
3117 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3118 smp_mb__after_clear_bit();
3119 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3120}
3121
3122static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
3123{
3124 int uptodate = err == 0;
3125 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
3126 struct extent_buffer *eb;
3127 int done;
3128
3129 do {
3130 struct page *page = bvec->bv_page;
3131
3132 bvec--;
3133 eb = (struct extent_buffer *)page->private;
3134 BUG_ON(!eb);
3135 done = atomic_dec_and_test(&eb->io_pages);
3136
3137 if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
3138 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3139 ClearPageUptodate(page);
3140 SetPageError(page);
3141 }
3142
3143 end_page_writeback(page);
3144
3145 if (!done)
3146 continue;
3147
3148 end_extent_buffer_writeback(eb);
3149 } while (bvec >= bio->bi_io_vec);
3150
3151 bio_put(bio);
3152
3153}
3154
3155static int write_one_eb(struct extent_buffer *eb,
3156 struct btrfs_fs_info *fs_info,
3157 struct writeback_control *wbc,
3158 struct extent_page_data *epd)
3159{
3160 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
3161 u64 offset = eb->start;
3162 unsigned long i, num_pages;
3163 int rw = (epd->sync_io ? WRITE_SYNC : WRITE);
3164 int ret;
3165
3166 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3167 num_pages = num_extent_pages(eb->start, eb->len);
3168 atomic_set(&eb->io_pages, num_pages);
3169 for (i = 0; i < num_pages; i++) {
3170 struct page *p = extent_buffer_page(eb, i);
3171
3172 clear_page_dirty_for_io(p);
3173 set_page_writeback(p);
3174 ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
3175 PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
3176 -1, end_bio_extent_buffer_writepage,
3177 0, 0, 0);
3178 if (ret) {
3179 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3180 SetPageError(p);
3181 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3182 end_extent_buffer_writeback(eb);
3183 ret = -EIO;
3184 break;
3185 }
3186 offset += PAGE_CACHE_SIZE;
3187 update_nr_written(p, wbc, 1);
3188 unlock_page(p);
3189 }
3190
3191 if (unlikely(ret)) {
3192 for (; i < num_pages; i++) {
3193 struct page *p = extent_buffer_page(eb, i);
3194 unlock_page(p);
3195 }
3196 }
3197
3198 return ret;
3199}
3200
3201int btree_write_cache_pages(struct address_space *mapping,
3202 struct writeback_control *wbc)
3203{
3204 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3205 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3206 struct extent_buffer *eb, *prev_eb = NULL;
3207 struct extent_page_data epd = {
3208 .bio = NULL,
3209 .tree = tree,
3210 .extent_locked = 0,
3211 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3212 };
3213 int ret = 0;
3214 int done = 0;
3215 int nr_to_write_done = 0;
3216 struct pagevec pvec;
3217 int nr_pages;
3218 pgoff_t index;
3219 pgoff_t end; /* Inclusive */
3220 int scanned = 0;
3221 int tag;
3222
3223 pagevec_init(&pvec, 0);
3224 if (wbc->range_cyclic) {
3225 index = mapping->writeback_index; /* Start from prev offset */
3226 end = -1;
3227 } else {
3228 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3229 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3230 scanned = 1;
3231 }
3232 if (wbc->sync_mode == WB_SYNC_ALL)
3233 tag = PAGECACHE_TAG_TOWRITE;
3234 else
3235 tag = PAGECACHE_TAG_DIRTY;
3236retry:
3237 if (wbc->sync_mode == WB_SYNC_ALL)
3238 tag_pages_for_writeback(mapping, index, end);
3239 while (!done && !nr_to_write_done && (index <= end) &&
3240 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3241 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3242 unsigned i;
3243
3244 scanned = 1;
3245 for (i = 0; i < nr_pages; i++) {
3246 struct page *page = pvec.pages[i];
3247
3248 if (!PagePrivate(page))
3249 continue;
3250
3251 if (!wbc->range_cyclic && page->index > end) {
3252 done = 1;
3253 break;
3254 }
3255
3256 eb = (struct extent_buffer *)page->private;
3257 if (!eb) {
3258 WARN_ON(1);
3259 continue;
3260 }
3261
3262 if (eb == prev_eb)
3263 continue;
3264
3265 if (!atomic_inc_not_zero(&eb->refs)) {
3266 WARN_ON(1);
3267 continue;
3268 }
3269
3270 prev_eb = eb;
3271 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3272 if (!ret) {
3273 free_extent_buffer(eb);
3274 continue;
3275 }
3276
3277 ret = write_one_eb(eb, fs_info, wbc, &epd);
3278 if (ret) {
3279 done = 1;
3280 free_extent_buffer(eb);
3281 break;
3282 }
3283 free_extent_buffer(eb);
3284
3285 /*
3286 * the filesystem may choose to bump up nr_to_write.
3287 * We have to make sure to honor the new nr_to_write
3288 * at any time
3289 */
3290 nr_to_write_done = wbc->nr_to_write <= 0;
3291 }
3292 pagevec_release(&pvec);
3293 cond_resched();
3294 }
3295 if (!scanned && !done) {
3296 /*
3297 * We hit the last page and there is more work to be done: wrap
3298 * back to the start of the file
3299 */
3300 scanned = 1;
3301 index = 0;
3302 goto retry;
3303 }
3304 flush_write_bio(&epd);
3305 return ret;
3306}
3307
Chris Masond1310b22008-01-24 16:13:08 -05003308/**
Chris Mason4bef0842008-09-08 11:18:08 -04003309 * 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 -05003310 * @mapping: address space structure to write
3311 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3312 * @writepage: function called for each page
3313 * @data: data passed to writepage function
3314 *
3315 * If a page is already under I/O, write_cache_pages() skips it, even
3316 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3317 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3318 * and msync() need to guarantee that all the data which was dirty at the time
3319 * the call was made get new I/O started against them. If wbc->sync_mode is
3320 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3321 * existing IO to complete.
3322 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003323static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04003324 struct address_space *mapping,
3325 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003326 writepage_t writepage, void *data,
3327 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05003328{
Chris Masond1310b22008-01-24 16:13:08 -05003329 int ret = 0;
3330 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003331 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003332 struct pagevec pvec;
3333 int nr_pages;
3334 pgoff_t index;
3335 pgoff_t end; /* Inclusive */
3336 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003337 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003338
Chris Masond1310b22008-01-24 16:13:08 -05003339 pagevec_init(&pvec, 0);
3340 if (wbc->range_cyclic) {
3341 index = mapping->writeback_index; /* Start from prev offset */
3342 end = -1;
3343 } else {
3344 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3345 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003346 scanned = 1;
3347 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003348 if (wbc->sync_mode == WB_SYNC_ALL)
3349 tag = PAGECACHE_TAG_TOWRITE;
3350 else
3351 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003352retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003353 if (wbc->sync_mode == WB_SYNC_ALL)
3354 tag_pages_for_writeback(mapping, index, end);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003355 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003356 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3357 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003358 unsigned i;
3359
3360 scanned = 1;
3361 for (i = 0; i < nr_pages; i++) {
3362 struct page *page = pvec.pages[i];
3363
3364 /*
3365 * At this point we hold neither mapping->tree_lock nor
3366 * lock on the page itself: the page may be truncated or
3367 * invalidated (changing page->mapping to NULL), or even
3368 * swizzled back from swapper_space to tmpfs file
3369 * mapping
3370 */
Chris Mason01d658f2011-11-01 10:08:06 -04003371 if (tree->ops &&
3372 tree->ops->write_cache_pages_lock_hook) {
3373 tree->ops->write_cache_pages_lock_hook(page,
3374 data, flush_fn);
3375 } else {
3376 if (!trylock_page(page)) {
3377 flush_fn(data);
3378 lock_page(page);
3379 }
3380 }
Chris Masond1310b22008-01-24 16:13:08 -05003381
3382 if (unlikely(page->mapping != mapping)) {
3383 unlock_page(page);
3384 continue;
3385 }
3386
3387 if (!wbc->range_cyclic && page->index > end) {
3388 done = 1;
3389 unlock_page(page);
3390 continue;
3391 }
3392
Chris Masond2c3f4f2008-11-19 12:44:22 -05003393 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003394 if (PageWriteback(page))
3395 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05003396 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003397 }
Chris Masond1310b22008-01-24 16:13:08 -05003398
3399 if (PageWriteback(page) ||
3400 !clear_page_dirty_for_io(page)) {
3401 unlock_page(page);
3402 continue;
3403 }
3404
3405 ret = (*writepage)(page, wbc, data);
3406
3407 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3408 unlock_page(page);
3409 ret = 0;
3410 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003411 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05003412 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003413
3414 /*
3415 * the filesystem may choose to bump up nr_to_write.
3416 * We have to make sure to honor the new nr_to_write
3417 * at any time
3418 */
3419 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05003420 }
3421 pagevec_release(&pvec);
3422 cond_resched();
3423 }
3424 if (!scanned && !done) {
3425 /*
3426 * We hit the last page and there is more work to be done: wrap
3427 * back to the start of the file
3428 */
3429 scanned = 1;
3430 index = 0;
3431 goto retry;
3432 }
Chris Masond1310b22008-01-24 16:13:08 -05003433 return ret;
3434}
Chris Masond1310b22008-01-24 16:13:08 -05003435
Chris Masonffbd5172009-04-20 15:50:09 -04003436static void flush_epd_write_bio(struct extent_page_data *epd)
3437{
3438 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04003439 int rw = WRITE;
3440 int ret;
3441
Chris Masonffbd5172009-04-20 15:50:09 -04003442 if (epd->sync_io)
Jeff Mahoney355808c2011-10-03 23:23:14 -04003443 rw = WRITE_SYNC;
3444
3445 ret = submit_one_bio(rw, epd->bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003446 BUG_ON(ret < 0); /* -ENOMEM */
Chris Masonffbd5172009-04-20 15:50:09 -04003447 epd->bio = NULL;
3448 }
3449}
3450
Chris Masond2c3f4f2008-11-19 12:44:22 -05003451static noinline void flush_write_bio(void *data)
3452{
3453 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04003454 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003455}
3456
Chris Masond1310b22008-01-24 16:13:08 -05003457int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3458 get_extent_t *get_extent,
3459 struct writeback_control *wbc)
3460{
3461 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003462 struct extent_page_data epd = {
3463 .bio = NULL,
3464 .tree = tree,
3465 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003466 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003467 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05003468 };
Chris Masond1310b22008-01-24 16:13:08 -05003469
Chris Masond1310b22008-01-24 16:13:08 -05003470 ret = __extent_writepage(page, wbc, &epd);
3471
Chris Masonffbd5172009-04-20 15:50:09 -04003472 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003473 return ret;
3474}
Chris Masond1310b22008-01-24 16:13:08 -05003475
Chris Mason771ed682008-11-06 22:02:51 -05003476int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3477 u64 start, u64 end, get_extent_t *get_extent,
3478 int mode)
3479{
3480 int ret = 0;
3481 struct address_space *mapping = inode->i_mapping;
3482 struct page *page;
3483 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3484 PAGE_CACHE_SHIFT;
3485
3486 struct extent_page_data epd = {
3487 .bio = NULL,
3488 .tree = tree,
3489 .get_extent = get_extent,
3490 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04003491 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05003492 };
3493 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05003494 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05003495 .nr_to_write = nr_pages * 2,
3496 .range_start = start,
3497 .range_end = end + 1,
3498 };
3499
Chris Masond3977122009-01-05 21:25:51 -05003500 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05003501 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3502 if (clear_page_dirty_for_io(page))
3503 ret = __extent_writepage(page, &wbc_writepages, &epd);
3504 else {
3505 if (tree->ops && tree->ops->writepage_end_io_hook)
3506 tree->ops->writepage_end_io_hook(page, start,
3507 start + PAGE_CACHE_SIZE - 1,
3508 NULL, 1);
3509 unlock_page(page);
3510 }
3511 page_cache_release(page);
3512 start += PAGE_CACHE_SIZE;
3513 }
3514
Chris Masonffbd5172009-04-20 15:50:09 -04003515 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05003516 return ret;
3517}
Chris Masond1310b22008-01-24 16:13:08 -05003518
3519int extent_writepages(struct extent_io_tree *tree,
3520 struct address_space *mapping,
3521 get_extent_t *get_extent,
3522 struct writeback_control *wbc)
3523{
3524 int ret = 0;
3525 struct extent_page_data epd = {
3526 .bio = NULL,
3527 .tree = tree,
3528 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003529 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003530 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05003531 };
3532
Chris Mason4bef0842008-09-08 11:18:08 -04003533 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003534 __extent_writepage, &epd,
3535 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04003536 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003537 return ret;
3538}
Chris Masond1310b22008-01-24 16:13:08 -05003539
3540int extent_readpages(struct extent_io_tree *tree,
3541 struct address_space *mapping,
3542 struct list_head *pages, unsigned nr_pages,
3543 get_extent_t get_extent)
3544{
3545 struct bio *bio = NULL;
3546 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04003547 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003548
Chris Masond1310b22008-01-24 16:13:08 -05003549 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
3550 struct page *page = list_entry(pages->prev, struct page, lru);
3551
3552 prefetchw(&page->flags);
3553 list_del(&page->lru);
Nick Piggin28ecb6092010-03-17 13:31:04 +00003554 if (!add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04003555 page->index, GFP_NOFS)) {
Chris Masonf1885912008-04-09 16:28:12 -04003556 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04003557 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003558 }
3559 page_cache_release(page);
3560 }
Chris Masond1310b22008-01-24 16:13:08 -05003561 BUG_ON(!list_empty(pages));
3562 if (bio)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003563 return submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003564 return 0;
3565}
Chris Masond1310b22008-01-24 16:13:08 -05003566
3567/*
3568 * basic invalidatepage code, this waits on any locked or writeback
3569 * ranges corresponding to the page, and then deletes any extent state
3570 * records from the tree
3571 */
3572int extent_invalidatepage(struct extent_io_tree *tree,
3573 struct page *page, unsigned long offset)
3574{
Josef Bacik2ac55d42010-02-03 19:33:23 +00003575 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003576 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
3577 u64 end = start + PAGE_CACHE_SIZE - 1;
3578 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3579
Chris Masond3977122009-01-05 21:25:51 -05003580 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003581 if (start > end)
3582 return 0;
3583
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003584 lock_extent_bits(tree, start, end, 0, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04003585 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003586 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04003587 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3588 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003589 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003590 return 0;
3591}
Chris Masond1310b22008-01-24 16:13:08 -05003592
3593/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04003594 * a helper for releasepage, this tests for areas of the page that
3595 * are locked or under IO and drops the related state bits if it is safe
3596 * to drop the page.
3597 */
3598int try_release_extent_state(struct extent_map_tree *map,
3599 struct extent_io_tree *tree, struct page *page,
3600 gfp_t mask)
3601{
3602 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3603 u64 end = start + PAGE_CACHE_SIZE - 1;
3604 int ret = 1;
3605
Chris Mason211f90e2008-07-18 11:56:15 -04003606 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04003607 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04003608 ret = 0;
3609 else {
3610 if ((mask & GFP_NOFS) == GFP_NOFS)
3611 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04003612 /*
3613 * at this point we can safely clear everything except the
3614 * locked bit and the nodatasum bit
3615 */
Chris Masone3f24cc2011-02-14 12:52:08 -05003616 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04003617 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3618 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05003619
3620 /* if clear_extent_bit failed for enomem reasons,
3621 * we can't allow the release to continue.
3622 */
3623 if (ret < 0)
3624 ret = 0;
3625 else
3626 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003627 }
3628 return ret;
3629}
Chris Mason7b13b7b2008-04-18 10:29:50 -04003630
3631/*
Chris Masond1310b22008-01-24 16:13:08 -05003632 * a helper for releasepage. As long as there are no locked extents
3633 * in the range corresponding to the page, both state records and extent
3634 * map records are removed
3635 */
3636int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05003637 struct extent_io_tree *tree, struct page *page,
3638 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05003639{
3640 struct extent_map *em;
3641 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3642 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003643
Chris Mason70dec802008-01-29 09:59:12 -05003644 if ((mask & __GFP_WAIT) &&
3645 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05003646 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05003647 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05003648 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04003649 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05003650 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09003651 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04003652 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003653 break;
3654 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003655 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3656 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04003657 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003658 free_extent_map(em);
3659 break;
3660 }
3661 if (!test_range_bit(tree, em->start,
3662 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04003663 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04003664 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05003665 remove_extent_mapping(map, em);
3666 /* once for the rb tree */
3667 free_extent_map(em);
3668 }
3669 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04003670 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003671
3672 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05003673 free_extent_map(em);
3674 }
Chris Masond1310b22008-01-24 16:13:08 -05003675 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04003676 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003677}
Chris Masond1310b22008-01-24 16:13:08 -05003678
Chris Masonec29ed52011-02-23 16:23:20 -05003679/*
3680 * helper function for fiemap, which doesn't want to see any holes.
3681 * This maps until we find something past 'last'
3682 */
3683static struct extent_map *get_extent_skip_holes(struct inode *inode,
3684 u64 offset,
3685 u64 last,
3686 get_extent_t *get_extent)
3687{
3688 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3689 struct extent_map *em;
3690 u64 len;
3691
3692 if (offset >= last)
3693 return NULL;
3694
3695 while(1) {
3696 len = last - offset;
3697 if (len == 0)
3698 break;
3699 len = (len + sectorsize - 1) & ~(sectorsize - 1);
3700 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02003701 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05003702 return em;
3703
3704 /* if this isn't a hole return it */
3705 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3706 em->block_start != EXTENT_MAP_HOLE) {
3707 return em;
3708 }
3709
3710 /* this is a hole, advance to the next extent */
3711 offset = extent_map_end(em);
3712 free_extent_map(em);
3713 if (offset >= last)
3714 break;
3715 }
3716 return NULL;
3717}
3718
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003719int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3720 __u64 start, __u64 len, get_extent_t *get_extent)
3721{
Josef Bacik975f84f2010-11-23 19:36:57 +00003722 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003723 u64 off = start;
3724 u64 max = start + len;
3725 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00003726 u32 found_type;
3727 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05003728 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003729 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003730 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00003731 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003732 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003733 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00003734 struct btrfs_path *path;
3735 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003736 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003737 u64 em_start = 0;
3738 u64 em_len = 0;
3739 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003740 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003741
3742 if (len == 0)
3743 return -EINVAL;
3744
Josef Bacik975f84f2010-11-23 19:36:57 +00003745 path = btrfs_alloc_path();
3746 if (!path)
3747 return -ENOMEM;
3748 path->leave_spinning = 1;
3749
Josef Bacik4d479cf2011-11-17 11:34:31 -05003750 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3751 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3752
Chris Masonec29ed52011-02-23 16:23:20 -05003753 /*
3754 * lookup the last file extent. We're not using i_size here
3755 * because there might be preallocation past i_size
3756 */
Josef Bacik975f84f2010-11-23 19:36:57 +00003757 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
Li Zefan33345d012011-04-20 10:31:50 +08003758 path, btrfs_ino(inode), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00003759 if (ret < 0) {
3760 btrfs_free_path(path);
3761 return ret;
3762 }
3763 WARN_ON(!ret);
3764 path->slots[0]--;
3765 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3766 struct btrfs_file_extent_item);
3767 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3768 found_type = btrfs_key_type(&found_key);
3769
Chris Masonec29ed52011-02-23 16:23:20 -05003770 /* No extents, but there might be delalloc bits */
Li Zefan33345d012011-04-20 10:31:50 +08003771 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00003772 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05003773 /* have to trust i_size as the end */
3774 last = (u64)-1;
3775 last_for_get_extent = isize;
3776 } else {
3777 /*
3778 * remember the start of the last extent. There are a
3779 * bunch of different factors that go into the length of the
3780 * extent, so its much less complex to remember where it started
3781 */
3782 last = found_key.offset;
3783 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00003784 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003785 btrfs_free_path(path);
3786
Chris Masonec29ed52011-02-23 16:23:20 -05003787 /*
3788 * we might have some extents allocated but more delalloc past those
3789 * extents. so, we trust isize unless the start of the last extent is
3790 * beyond isize
3791 */
3792 if (last < isize) {
3793 last = (u64)-1;
3794 last_for_get_extent = isize;
3795 }
3796
Josef Bacik2ac55d42010-02-03 19:33:23 +00003797 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003798 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05003799
Josef Bacik4d479cf2011-11-17 11:34:31 -05003800 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05003801 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003802 if (!em)
3803 goto out;
3804 if (IS_ERR(em)) {
3805 ret = PTR_ERR(em);
3806 goto out;
3807 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003808
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003809 while (!end) {
Chris Masonea8efc72011-03-08 11:54:40 -05003810 u64 offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003811
Chris Masonea8efc72011-03-08 11:54:40 -05003812 /* break if the extent we found is outside the range */
3813 if (em->start >= max || extent_map_end(em) < off)
3814 break;
3815
3816 /*
3817 * get_extent may return an extent that starts before our
3818 * requested range. We have to make sure the ranges
3819 * we return to fiemap always move forward and don't
3820 * overlap, so adjust the offsets here
3821 */
3822 em_start = max(em->start, off);
3823
3824 /*
3825 * record the offset from the start of the extent
3826 * for adjusting the disk offset below
3827 */
3828 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05003829 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05003830 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05003831 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003832 disko = 0;
3833 flags = 0;
3834
Chris Masonea8efc72011-03-08 11:54:40 -05003835 /*
3836 * bump off for our next call to get_extent
3837 */
3838 off = extent_map_end(em);
3839 if (off >= max)
3840 end = 1;
3841
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003842 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003843 end = 1;
3844 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003845 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003846 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3847 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003848 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003849 flags |= (FIEMAP_EXTENT_DELALLOC |
3850 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003851 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05003852 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003853 }
3854 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3855 flags |= FIEMAP_EXTENT_ENCODED;
3856
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003857 free_extent_map(em);
3858 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05003859 if ((em_start >= last) || em_len == (u64)-1 ||
3860 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003861 flags |= FIEMAP_EXTENT_LAST;
3862 end = 1;
3863 }
3864
Chris Masonec29ed52011-02-23 16:23:20 -05003865 /* now scan forward to see if this is really the last extent. */
3866 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3867 get_extent);
3868 if (IS_ERR(em)) {
3869 ret = PTR_ERR(em);
3870 goto out;
3871 }
3872 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00003873 flags |= FIEMAP_EXTENT_LAST;
3874 end = 1;
3875 }
Chris Masonec29ed52011-02-23 16:23:20 -05003876 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3877 em_len, flags);
3878 if (ret)
3879 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003880 }
3881out_free:
3882 free_extent_map(em);
3883out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00003884 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3885 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003886 return ret;
3887}
3888
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003889inline struct page *extent_buffer_page(struct extent_buffer *eb,
Chris Masond1310b22008-01-24 16:13:08 -05003890 unsigned long i)
3891{
Chris Mason727011e2010-08-06 13:21:20 -04003892 return eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05003893}
3894
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003895inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04003896{
Chris Mason6af118c2008-07-22 11:18:07 -04003897 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3898 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04003899}
3900
Chris Mason727011e2010-08-06 13:21:20 -04003901static void __free_extent_buffer(struct extent_buffer *eb)
3902{
3903#if LEAK_DEBUG
3904 unsigned long flags;
3905 spin_lock_irqsave(&leak_lock, flags);
3906 list_del(&eb->leak_list);
3907 spin_unlock_irqrestore(&leak_lock, flags);
3908#endif
3909 if (eb->pages && eb->pages != eb->inline_pages)
3910 kfree(eb->pages);
3911 kmem_cache_free(extent_buffer_cache, eb);
3912}
3913
Chris Masond1310b22008-01-24 16:13:08 -05003914static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3915 u64 start,
3916 unsigned long len,
3917 gfp_t mask)
3918{
3919 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003920#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003921 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003922#endif
Chris Masond1310b22008-01-24 16:13:08 -05003923
Chris Masond1310b22008-01-24 16:13:08 -05003924 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003925 if (eb == NULL)
3926 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003927 eb->start = start;
3928 eb->len = len;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05003929 eb->tree = tree;
Chris Masonbd681512011-07-16 15:23:14 -04003930 rwlock_init(&eb->lock);
3931 atomic_set(&eb->write_locks, 0);
3932 atomic_set(&eb->read_locks, 0);
3933 atomic_set(&eb->blocking_readers, 0);
3934 atomic_set(&eb->blocking_writers, 0);
3935 atomic_set(&eb->spinning_readers, 0);
3936 atomic_set(&eb->spinning_writers, 0);
Arne Jansen5b25f702011-09-13 10:55:48 +02003937 eb->lock_nested = 0;
Chris Masonbd681512011-07-16 15:23:14 -04003938 init_waitqueue_head(&eb->write_lock_wq);
3939 init_waitqueue_head(&eb->read_lock_wq);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003940
Chris Mason39351272009-02-04 09:24:05 -05003941#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003942 spin_lock_irqsave(&leak_lock, flags);
3943 list_add(&eb->leak_list, &buffers);
3944 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003945#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05003946 spin_lock_init(&eb->refs_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003947 atomic_set(&eb->refs, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003948 atomic_set(&eb->io_pages, 0);
Chris Mason727011e2010-08-06 13:21:20 -04003949
3950 if (len > MAX_INLINE_EXTENT_BUFFER_SIZE) {
3951 struct page **pages;
3952 int num_pages = (len + PAGE_CACHE_SIZE - 1) >>
3953 PAGE_CACHE_SHIFT;
3954 pages = kzalloc(num_pages, mask);
3955 if (!pages) {
3956 __free_extent_buffer(eb);
3957 return NULL;
3958 }
3959 eb->pages = pages;
3960 } else {
3961 eb->pages = eb->inline_pages;
3962 }
Chris Masond1310b22008-01-24 16:13:08 -05003963
3964 return eb;
3965}
3966
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003967static int extent_buffer_under_io(struct extent_buffer *eb)
3968{
3969 return (atomic_read(&eb->io_pages) ||
3970 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
3971 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
3972}
3973
Miao Xie897ca6e2010-10-26 20:57:29 -04003974/*
3975 * Helper for releasing extent buffer page.
3976 */
3977static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
3978 unsigned long start_idx)
3979{
3980 unsigned long index;
3981 struct page *page;
3982
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003983 BUG_ON(extent_buffer_under_io(eb));
3984
Miao Xie897ca6e2010-10-26 20:57:29 -04003985 index = num_extent_pages(eb->start, eb->len);
3986 if (start_idx >= index)
3987 return;
3988
3989 do {
3990 index--;
3991 page = extent_buffer_page(eb, index);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05003992 if (page) {
3993 spin_lock(&page->mapping->private_lock);
3994 /*
3995 * We do this since we'll remove the pages after we've
3996 * removed the eb from the radix tree, so we could race
3997 * and have this page now attached to the new eb. So
3998 * only clear page_private if it's still connected to
3999 * this eb.
4000 */
4001 if (PagePrivate(page) &&
4002 page->private == (unsigned long)eb) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004003 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Josef Bacik3083ee22012-03-09 16:01:49 -05004004 BUG_ON(PageDirty(page));
4005 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004006 /*
4007 * We need to make sure we haven't be attached
4008 * to a new eb.
4009 */
4010 ClearPagePrivate(page);
4011 set_page_private(page, 0);
4012 /* One for the page private */
4013 page_cache_release(page);
4014 }
4015 spin_unlock(&page->mapping->private_lock);
4016
4017 /* One for when we alloced the page */
Miao Xie897ca6e2010-10-26 20:57:29 -04004018 page_cache_release(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004019 }
Miao Xie897ca6e2010-10-26 20:57:29 -04004020 } while (index != start_idx);
4021}
4022
4023/*
4024 * Helper for releasing the extent buffer.
4025 */
4026static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4027{
4028 btrfs_release_extent_buffer_page(eb, 0);
4029 __free_extent_buffer(eb);
4030}
4031
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004032static void check_buffer_tree_ref(struct extent_buffer *eb)
4033{
4034 /* the ref bit is tricky. We have to make sure it is set
4035 * if we have the buffer dirty. Otherwise the
4036 * code to free a buffer can end up dropping a dirty
4037 * page
4038 *
4039 * Once the ref bit is set, it won't go away while the
4040 * buffer is dirty or in writeback, and it also won't
4041 * go away while we have the reference count on the
4042 * eb bumped.
4043 *
4044 * We can't just set the ref bit without bumping the
4045 * ref on the eb because free_extent_buffer might
4046 * see the ref bit and try to clear it. If this happens
4047 * free_extent_buffer might end up dropping our original
4048 * ref by mistake and freeing the page before we are able
4049 * to add one more ref.
4050 *
4051 * So bump the ref count first, then set the bit. If someone
4052 * beat us to it, drop the ref we added.
4053 */
4054 if (!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
4055 atomic_inc(&eb->refs);
4056 if (test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4057 atomic_dec(&eb->refs);
4058 }
4059}
4060
Josef Bacik5df42352012-03-15 18:24:42 -04004061static void mark_extent_buffer_accessed(struct extent_buffer *eb)
4062{
4063 unsigned long num_pages, i;
4064
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004065 check_buffer_tree_ref(eb);
4066
Josef Bacik5df42352012-03-15 18:24:42 -04004067 num_pages = num_extent_pages(eb->start, eb->len);
4068 for (i = 0; i < num_pages; i++) {
4069 struct page *p = extent_buffer_page(eb, i);
4070 mark_page_accessed(p);
4071 }
4072}
4073
Chris Masond1310b22008-01-24 16:13:08 -05004074struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
Chris Mason727011e2010-08-06 13:21:20 -04004075 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004076{
4077 unsigned long num_pages = num_extent_pages(start, len);
4078 unsigned long i;
4079 unsigned long index = start >> PAGE_CACHE_SHIFT;
4080 struct extent_buffer *eb;
Chris Mason6af118c2008-07-22 11:18:07 -04004081 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004082 struct page *p;
4083 struct address_space *mapping = tree->mapping;
4084 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004085 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004086
Miao Xie19fe0a82010-10-26 20:57:29 -04004087 rcu_read_lock();
4088 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4089 if (eb && atomic_inc_not_zero(&eb->refs)) {
4090 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004091 mark_extent_buffer_accessed(eb);
Chris Mason6af118c2008-07-22 11:18:07 -04004092 return eb;
4093 }
Miao Xie19fe0a82010-10-26 20:57:29 -04004094 rcu_read_unlock();
Chris Mason6af118c2008-07-22 11:18:07 -04004095
David Sterbaba144192011-04-21 01:12:06 +02004096 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
Peter2b114d12008-04-01 11:21:40 -04004097 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004098 return NULL;
4099
Chris Mason727011e2010-08-06 13:21:20 -04004100 for (i = 0; i < num_pages; i++, index++) {
Chris Masona6591712011-07-19 12:04:14 -04004101 p = find_or_create_page(mapping, index, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05004102 if (!p) {
4103 WARN_ON(1);
Chris Mason6af118c2008-07-22 11:18:07 -04004104 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05004105 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004106
4107 spin_lock(&mapping->private_lock);
4108 if (PagePrivate(p)) {
4109 /*
4110 * We could have already allocated an eb for this page
4111 * and attached one so lets see if we can get a ref on
4112 * the existing eb, and if we can we know it's good and
4113 * we can just return that one, else we know we can just
4114 * overwrite page->private.
4115 */
4116 exists = (struct extent_buffer *)p->private;
4117 if (atomic_inc_not_zero(&exists->refs)) {
4118 spin_unlock(&mapping->private_lock);
4119 unlock_page(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004120 mark_extent_buffer_accessed(exists);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004121 goto free_eb;
4122 }
4123
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004124 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004125 * Do this so attach doesn't complain and we need to
4126 * drop the ref the old guy had.
4127 */
4128 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004129 WARN_ON(PageDirty(p));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004130 page_cache_release(p);
4131 }
4132 attach_extent_buffer_page(eb, p);
4133 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004134 WARN_ON(PageDirty(p));
Chris Masond1310b22008-01-24 16:13:08 -05004135 mark_page_accessed(p);
Chris Mason727011e2010-08-06 13:21:20 -04004136 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05004137 if (!PageUptodate(p))
4138 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05004139
4140 /*
4141 * see below about how we avoid a nasty race with release page
4142 * and why we unlock later
4143 */
Chris Masond1310b22008-01-24 16:13:08 -05004144 }
4145 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004146 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05004147again:
Miao Xie19fe0a82010-10-26 20:57:29 -04004148 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4149 if (ret)
4150 goto free_eb;
4151
Chris Mason6af118c2008-07-22 11:18:07 -04004152 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004153 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
4154 if (ret == -EEXIST) {
4155 exists = radix_tree_lookup(&tree->buffer,
4156 start >> PAGE_CACHE_SHIFT);
Josef Bacik115391d2012-03-09 09:51:43 -05004157 if (!atomic_inc_not_zero(&exists->refs)) {
4158 spin_unlock(&tree->buffer_lock);
4159 radix_tree_preload_end();
Josef Bacik115391d2012-03-09 09:51:43 -05004160 exists = NULL;
4161 goto again;
4162 }
Chris Mason6af118c2008-07-22 11:18:07 -04004163 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004164 radix_tree_preload_end();
Josef Bacik5df42352012-03-15 18:24:42 -04004165 mark_extent_buffer_accessed(exists);
Chris Mason6af118c2008-07-22 11:18:07 -04004166 goto free_eb;
4167 }
Chris Mason6af118c2008-07-22 11:18:07 -04004168 /* add one reference for the tree */
Josef Bacik3083ee22012-03-09 16:01:49 -05004169 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004170 check_buffer_tree_ref(eb);
Josef Bacik3083ee22012-03-09 16:01:49 -05004171 spin_unlock(&eb->refs_lock);
Yan, Zhengf044ba72010-02-04 08:46:56 +00004172 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004173 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05004174
4175 /*
4176 * there is a race where release page may have
4177 * tried to find this extent buffer in the radix
4178 * but failed. It will tell the VM it is safe to
4179 * reclaim the, and it will clear the page private bit.
4180 * We must make sure to set the page private bit properly
4181 * after the extent buffer is in the radix tree so
4182 * it doesn't get lost
4183 */
Chris Mason727011e2010-08-06 13:21:20 -04004184 SetPageChecked(eb->pages[0]);
4185 for (i = 1; i < num_pages; i++) {
4186 p = extent_buffer_page(eb, i);
Chris Mason727011e2010-08-06 13:21:20 -04004187 ClearPageChecked(p);
4188 unlock_page(p);
4189 }
4190 unlock_page(eb->pages[0]);
Chris Masond1310b22008-01-24 16:13:08 -05004191 return eb;
4192
Chris Mason6af118c2008-07-22 11:18:07 -04004193free_eb:
Chris Mason727011e2010-08-06 13:21:20 -04004194 for (i = 0; i < num_pages; i++) {
4195 if (eb->pages[i])
4196 unlock_page(eb->pages[i]);
4197 }
Chris Masoneb14ab82011-02-10 12:35:00 -05004198
Chris Masond1310b22008-01-24 16:13:08 -05004199 if (!atomic_dec_and_test(&eb->refs))
Chris Mason6af118c2008-07-22 11:18:07 -04004200 return exists;
Miao Xie897ca6e2010-10-26 20:57:29 -04004201 btrfs_release_extent_buffer(eb);
Chris Mason6af118c2008-07-22 11:18:07 -04004202 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05004203}
Chris Masond1310b22008-01-24 16:13:08 -05004204
4205struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
David Sterbaf09d1f62011-04-21 01:08:01 +02004206 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004207{
Chris Masond1310b22008-01-24 16:13:08 -05004208 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05004209
Miao Xie19fe0a82010-10-26 20:57:29 -04004210 rcu_read_lock();
4211 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4212 if (eb && atomic_inc_not_zero(&eb->refs)) {
4213 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004214 mark_extent_buffer_accessed(eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04004215 return eb;
4216 }
4217 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04004218
Miao Xie19fe0a82010-10-26 20:57:29 -04004219 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004220}
Chris Masond1310b22008-01-24 16:13:08 -05004221
Josef Bacik3083ee22012-03-09 16:01:49 -05004222static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4223{
4224 struct extent_buffer *eb =
4225 container_of(head, struct extent_buffer, rcu_head);
4226
4227 __free_extent_buffer(eb);
4228}
4229
Josef Bacik3083ee22012-03-09 16:01:49 -05004230/* Expects to have eb->eb_lock already held */
4231static void release_extent_buffer(struct extent_buffer *eb, gfp_t mask)
4232{
4233 WARN_ON(atomic_read(&eb->refs) == 0);
4234 if (atomic_dec_and_test(&eb->refs)) {
4235 struct extent_io_tree *tree = eb->tree;
Josef Bacik3083ee22012-03-09 16:01:49 -05004236
4237 spin_unlock(&eb->refs_lock);
4238
Josef Bacik3083ee22012-03-09 16:01:49 -05004239 spin_lock(&tree->buffer_lock);
4240 radix_tree_delete(&tree->buffer,
4241 eb->start >> PAGE_CACHE_SHIFT);
4242 spin_unlock(&tree->buffer_lock);
4243
4244 /* Should be safe to release our pages at this point */
4245 btrfs_release_extent_buffer_page(eb, 0);
4246
4247 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
4248 return;
4249 }
4250 spin_unlock(&eb->refs_lock);
4251}
4252
Chris Masond1310b22008-01-24 16:13:08 -05004253void free_extent_buffer(struct extent_buffer *eb)
4254{
Chris Masond1310b22008-01-24 16:13:08 -05004255 if (!eb)
4256 return;
4257
Josef Bacik3083ee22012-03-09 16:01:49 -05004258 spin_lock(&eb->refs_lock);
4259 if (atomic_read(&eb->refs) == 2 &&
4260 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004261 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004262 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4263 atomic_dec(&eb->refs);
4264
4265 /*
4266 * I know this is terrible, but it's temporary until we stop tracking
4267 * the uptodate bits and such for the extent buffers.
4268 */
4269 release_extent_buffer(eb, GFP_ATOMIC);
4270}
4271
4272void free_extent_buffer_stale(struct extent_buffer *eb)
4273{
4274 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004275 return;
4276
Josef Bacik3083ee22012-03-09 16:01:49 -05004277 spin_lock(&eb->refs_lock);
4278 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
4279
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004280 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004281 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4282 atomic_dec(&eb->refs);
4283 release_extent_buffer(eb, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05004284}
Chris Masond1310b22008-01-24 16:13:08 -05004285
Chris Mason1d4284b2012-03-28 20:31:37 -04004286void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004287{
Chris Masond1310b22008-01-24 16:13:08 -05004288 unsigned long i;
4289 unsigned long num_pages;
4290 struct page *page;
4291
Chris Masond1310b22008-01-24 16:13:08 -05004292 num_pages = num_extent_pages(eb->start, eb->len);
4293
4294 for (i = 0; i < num_pages; i++) {
4295 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04004296 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05004297 continue;
4298
Chris Masona61e6f22008-07-22 11:18:08 -04004299 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05004300 WARN_ON(!PagePrivate(page));
4301
Chris Masond1310b22008-01-24 16:13:08 -05004302 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004303 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05004304 if (!PageDirty(page)) {
4305 radix_tree_tag_clear(&page->mapping->page_tree,
4306 page_index(page),
4307 PAGECACHE_TAG_DIRTY);
4308 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004309 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04004310 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04004311 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05004312 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004313 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05004314}
Chris Masond1310b22008-01-24 16:13:08 -05004315
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004316int set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004317{
4318 unsigned long i;
4319 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04004320 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004321
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004322 check_buffer_tree_ref(eb);
4323
Chris Masonb9473432009-03-13 11:00:37 -04004324 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004325
Chris Masond1310b22008-01-24 16:13:08 -05004326 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik3083ee22012-03-09 16:01:49 -05004327 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004328 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
4329
Chris Masonb9473432009-03-13 11:00:37 -04004330 for (i = 0; i < num_pages; i++)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004331 set_page_dirty(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04004332 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05004333}
Chris Masond1310b22008-01-24 16:13:08 -05004334
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004335static int range_straddles_pages(u64 start, u64 len)
Chris Mason19b6caf2011-07-25 06:50:50 -04004336{
4337 if (len < PAGE_CACHE_SIZE)
4338 return 1;
4339 if (start & (PAGE_CACHE_SIZE - 1))
4340 return 1;
4341 if ((start + len) & (PAGE_CACHE_SIZE - 1))
4342 return 1;
4343 return 0;
4344}
4345
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004346int clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04004347{
4348 unsigned long i;
4349 struct page *page;
4350 unsigned long num_pages;
4351
Chris Masonb4ce94d2009-02-04 09:25:08 -05004352 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004353 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason1259ab72008-05-12 13:39:03 -04004354 for (i = 0; i < num_pages; i++) {
4355 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04004356 if (page)
4357 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04004358 }
4359 return 0;
4360}
4361
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004362int set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004363{
4364 unsigned long i;
4365 struct page *page;
4366 unsigned long num_pages;
4367
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004368 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004369 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05004370 for (i = 0; i < num_pages; i++) {
4371 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004372 SetPageUptodate(page);
4373 }
4374 return 0;
4375}
Chris Masond1310b22008-01-24 16:13:08 -05004376
Chris Masonce9adaa2008-04-09 16:28:12 -04004377int extent_range_uptodate(struct extent_io_tree *tree,
4378 u64 start, u64 end)
4379{
4380 struct page *page;
4381 int ret;
4382 int pg_uptodate = 1;
4383 int uptodate;
4384 unsigned long index;
4385
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004386 if (range_straddles_pages(start, end - start + 1)) {
Chris Mason19b6caf2011-07-25 06:50:50 -04004387 ret = test_range_bit(tree, start, end,
4388 EXTENT_UPTODATE, 1, NULL);
4389 if (ret)
4390 return 1;
4391 }
Chris Masond3977122009-01-05 21:25:51 -05004392 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004393 index = start >> PAGE_CACHE_SHIFT;
4394 page = find_get_page(tree->mapping, index);
Mitch Harder8bedd512012-01-26 15:01:11 -05004395 if (!page)
4396 return 1;
Chris Masonce9adaa2008-04-09 16:28:12 -04004397 uptodate = PageUptodate(page);
4398 page_cache_release(page);
4399 if (!uptodate) {
4400 pg_uptodate = 0;
4401 break;
4402 }
4403 start += PAGE_CACHE_SIZE;
4404 }
4405 return pg_uptodate;
4406}
4407
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004408int extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004409{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004410 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004411}
Chris Masond1310b22008-01-24 16:13:08 -05004412
4413int read_extent_buffer_pages(struct extent_io_tree *tree,
Arne Jansenbb82ab82011-06-10 14:06:53 +02004414 struct extent_buffer *eb, u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04004415 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05004416{
4417 unsigned long i;
4418 unsigned long start_i;
4419 struct page *page;
4420 int err;
4421 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04004422 int locked_pages = 0;
4423 int all_uptodate = 1;
Chris Masond1310b22008-01-24 16:13:08 -05004424 unsigned long num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04004425 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004426 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004427 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004428
Chris Masonb4ce94d2009-02-04 09:25:08 -05004429 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05004430 return 0;
4431
Chris Masond1310b22008-01-24 16:13:08 -05004432 if (start) {
4433 WARN_ON(start < eb->start);
4434 start_i = (start >> PAGE_CACHE_SHIFT) -
4435 (eb->start >> PAGE_CACHE_SHIFT);
4436 } else {
4437 start_i = 0;
4438 }
4439
4440 num_pages = num_extent_pages(eb->start, eb->len);
4441 for (i = start_i; i < num_pages; i++) {
4442 page = extent_buffer_page(eb, i);
Arne Jansenbb82ab82011-06-10 14:06:53 +02004443 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04004444 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04004445 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05004446 } else {
4447 lock_page(page);
4448 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004449 locked_pages++;
Chris Mason727011e2010-08-06 13:21:20 -04004450 if (!PageUptodate(page)) {
4451 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04004452 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04004453 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004454 }
4455 if (all_uptodate) {
4456 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004457 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04004458 goto unlock_exit;
4459 }
4460
Josef Bacikea466792012-03-26 21:57:36 -04004461 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
4462 eb->failed_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004463 atomic_set(&eb->io_pages, num_reads);
Chris Masonce9adaa2008-04-09 16:28:12 -04004464 for (i = start_i; i < num_pages; i++) {
4465 page = extent_buffer_page(eb, i);
Chris Masonce9adaa2008-04-09 16:28:12 -04004466 if (!PageUptodate(page)) {
Chris Masonf1885912008-04-09 16:28:12 -04004467 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05004468 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04004469 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04004470 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05004471 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05004472 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05004473 } else {
4474 unlock_page(page);
4475 }
4476 }
4477
Jeff Mahoney355808c2011-10-03 23:23:14 -04004478 if (bio) {
4479 err = submit_one_bio(READ, bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004480 if (err)
4481 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04004482 }
Chris Masona86c12c2008-02-07 10:50:54 -05004483
Arne Jansenbb82ab82011-06-10 14:06:53 +02004484 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05004485 return ret;
Chris Masond3977122009-01-05 21:25:51 -05004486
Chris Masond1310b22008-01-24 16:13:08 -05004487 for (i = start_i; i < num_pages; i++) {
4488 page = extent_buffer_page(eb, i);
4489 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05004490 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05004491 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05004492 }
Chris Masond3977122009-01-05 21:25:51 -05004493
Chris Masond1310b22008-01-24 16:13:08 -05004494 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04004495
4496unlock_exit:
4497 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05004498 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004499 page = extent_buffer_page(eb, i);
4500 i++;
4501 unlock_page(page);
4502 locked_pages--;
4503 }
4504 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004505}
Chris Masond1310b22008-01-24 16:13:08 -05004506
4507void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4508 unsigned long start,
4509 unsigned long len)
4510{
4511 size_t cur;
4512 size_t offset;
4513 struct page *page;
4514 char *kaddr;
4515 char *dst = (char *)dstv;
4516 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4517 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004518
4519 WARN_ON(start > eb->len);
4520 WARN_ON(start + len > eb->start + eb->len);
4521
4522 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4523
Chris Masond3977122009-01-05 21:25:51 -05004524 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004525 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004526
4527 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04004528 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004529 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004530
4531 dst += cur;
4532 len -= cur;
4533 offset = 0;
4534 i++;
4535 }
4536}
Chris Masond1310b22008-01-24 16:13:08 -05004537
4538int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04004539 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05004540 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04004541 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05004542{
4543 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4544 char *kaddr;
4545 struct page *p;
4546 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4547 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4548 unsigned long end_i = (start_offset + start + min_len - 1) >>
4549 PAGE_CACHE_SHIFT;
4550
4551 if (i != end_i)
4552 return -EINVAL;
4553
4554 if (i == 0) {
4555 offset = start_offset;
4556 *map_start = 0;
4557 } else {
4558 offset = 0;
4559 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4560 }
Chris Masond3977122009-01-05 21:25:51 -05004561
Chris Masond1310b22008-01-24 16:13:08 -05004562 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05004563 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
4564 "wanted %lu %lu\n", (unsigned long long)eb->start,
4565 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05004566 WARN_ON(1);
Josef Bacik850265332011-03-15 14:52:12 -04004567 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05004568 }
4569
4570 p = extent_buffer_page(eb, i);
Chris Masona6591712011-07-19 12:04:14 -04004571 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05004572 *map = kaddr + offset;
4573 *map_len = PAGE_CACHE_SIZE - offset;
4574 return 0;
4575}
Chris Masond1310b22008-01-24 16:13:08 -05004576
Chris Masond1310b22008-01-24 16:13:08 -05004577int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4578 unsigned long start,
4579 unsigned long len)
4580{
4581 size_t cur;
4582 size_t offset;
4583 struct page *page;
4584 char *kaddr;
4585 char *ptr = (char *)ptrv;
4586 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4587 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4588 int ret = 0;
4589
4590 WARN_ON(start > eb->len);
4591 WARN_ON(start + len > eb->start + eb->len);
4592
4593 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4594
Chris Masond3977122009-01-05 21:25:51 -05004595 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004596 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004597
4598 cur = min(len, (PAGE_CACHE_SIZE - offset));
4599
Chris Masona6591712011-07-19 12:04:14 -04004600 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004601 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004602 if (ret)
4603 break;
4604
4605 ptr += cur;
4606 len -= cur;
4607 offset = 0;
4608 i++;
4609 }
4610 return ret;
4611}
Chris Masond1310b22008-01-24 16:13:08 -05004612
4613void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4614 unsigned long start, unsigned long len)
4615{
4616 size_t cur;
4617 size_t offset;
4618 struct page *page;
4619 char *kaddr;
4620 char *src = (char *)srcv;
4621 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4622 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4623
4624 WARN_ON(start > eb->len);
4625 WARN_ON(start + len > eb->start + eb->len);
4626
4627 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4628
Chris Masond3977122009-01-05 21:25:51 -05004629 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004630 page = extent_buffer_page(eb, i);
4631 WARN_ON(!PageUptodate(page));
4632
4633 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004634 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004635 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004636
4637 src += cur;
4638 len -= cur;
4639 offset = 0;
4640 i++;
4641 }
4642}
Chris Masond1310b22008-01-24 16:13:08 -05004643
4644void memset_extent_buffer(struct extent_buffer *eb, char c,
4645 unsigned long start, unsigned long len)
4646{
4647 size_t cur;
4648 size_t offset;
4649 struct page *page;
4650 char *kaddr;
4651 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4652 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4653
4654 WARN_ON(start > eb->len);
4655 WARN_ON(start + len > eb->start + eb->len);
4656
4657 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4658
Chris Masond3977122009-01-05 21:25:51 -05004659 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004660 page = extent_buffer_page(eb, i);
4661 WARN_ON(!PageUptodate(page));
4662
4663 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004664 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004665 memset(kaddr + offset, c, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004666
4667 len -= cur;
4668 offset = 0;
4669 i++;
4670 }
4671}
Chris Masond1310b22008-01-24 16:13:08 -05004672
4673void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4674 unsigned long dst_offset, unsigned long src_offset,
4675 unsigned long len)
4676{
4677 u64 dst_len = dst->len;
4678 size_t cur;
4679 size_t offset;
4680 struct page *page;
4681 char *kaddr;
4682 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4683 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4684
4685 WARN_ON(src->len != dst_len);
4686
4687 offset = (start_offset + dst_offset) &
4688 ((unsigned long)PAGE_CACHE_SIZE - 1);
4689
Chris Masond3977122009-01-05 21:25:51 -05004690 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004691 page = extent_buffer_page(dst, i);
4692 WARN_ON(!PageUptodate(page));
4693
4694 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4695
Chris Masona6591712011-07-19 12:04:14 -04004696 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004697 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004698
4699 src_offset += cur;
4700 len -= cur;
4701 offset = 0;
4702 i++;
4703 }
4704}
Chris Masond1310b22008-01-24 16:13:08 -05004705
4706static void move_pages(struct page *dst_page, struct page *src_page,
4707 unsigned long dst_off, unsigned long src_off,
4708 unsigned long len)
4709{
Chris Masona6591712011-07-19 12:04:14 -04004710 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004711 if (dst_page == src_page) {
4712 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4713 } else {
Chris Masona6591712011-07-19 12:04:14 -04004714 char *src_kaddr = page_address(src_page);
Chris Masond1310b22008-01-24 16:13:08 -05004715 char *p = dst_kaddr + dst_off + len;
4716 char *s = src_kaddr + src_off + len;
4717
4718 while (len--)
4719 *--p = *--s;
Chris Masond1310b22008-01-24 16:13:08 -05004720 }
Chris Masond1310b22008-01-24 16:13:08 -05004721}
4722
Sergei Trofimovich33872062011-04-11 21:52:52 +00004723static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4724{
4725 unsigned long distance = (src > dst) ? src - dst : dst - src;
4726 return distance < len;
4727}
4728
Chris Masond1310b22008-01-24 16:13:08 -05004729static void copy_pages(struct page *dst_page, struct page *src_page,
4730 unsigned long dst_off, unsigned long src_off,
4731 unsigned long len)
4732{
Chris Masona6591712011-07-19 12:04:14 -04004733 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004734 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004735 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004736
Sergei Trofimovich33872062011-04-11 21:52:52 +00004737 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04004738 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00004739 } else {
Chris Masond1310b22008-01-24 16:13:08 -05004740 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004741 if (areas_overlap(src_off, dst_off, len))
4742 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00004743 }
Chris Masond1310b22008-01-24 16:13:08 -05004744
Chris Mason727011e2010-08-06 13:21:20 -04004745 if (must_memmove)
4746 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
4747 else
4748 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05004749}
4750
4751void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4752 unsigned long src_offset, unsigned long len)
4753{
4754 size_t cur;
4755 size_t dst_off_in_page;
4756 size_t src_off_in_page;
4757 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4758 unsigned long dst_i;
4759 unsigned long src_i;
4760
4761 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004762 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4763 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004764 BUG_ON(1);
4765 }
4766 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004767 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4768 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004769 BUG_ON(1);
4770 }
4771
Chris Masond3977122009-01-05 21:25:51 -05004772 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004773 dst_off_in_page = (start_offset + dst_offset) &
4774 ((unsigned long)PAGE_CACHE_SIZE - 1);
4775 src_off_in_page = (start_offset + src_offset) &
4776 ((unsigned long)PAGE_CACHE_SIZE - 1);
4777
4778 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4779 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4780
4781 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4782 src_off_in_page));
4783 cur = min_t(unsigned long, cur,
4784 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4785
4786 copy_pages(extent_buffer_page(dst, dst_i),
4787 extent_buffer_page(dst, src_i),
4788 dst_off_in_page, src_off_in_page, cur);
4789
4790 src_offset += cur;
4791 dst_offset += cur;
4792 len -= cur;
4793 }
4794}
Chris Masond1310b22008-01-24 16:13:08 -05004795
4796void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4797 unsigned long src_offset, unsigned long len)
4798{
4799 size_t cur;
4800 size_t dst_off_in_page;
4801 size_t src_off_in_page;
4802 unsigned long dst_end = dst_offset + len - 1;
4803 unsigned long src_end = src_offset + len - 1;
4804 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4805 unsigned long dst_i;
4806 unsigned long src_i;
4807
4808 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004809 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4810 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004811 BUG_ON(1);
4812 }
4813 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004814 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4815 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004816 BUG_ON(1);
4817 }
Chris Mason727011e2010-08-06 13:21:20 -04004818 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05004819 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4820 return;
4821 }
Chris Masond3977122009-01-05 21:25:51 -05004822 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004823 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
4824 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
4825
4826 dst_off_in_page = (start_offset + dst_end) &
4827 ((unsigned long)PAGE_CACHE_SIZE - 1);
4828 src_off_in_page = (start_offset + src_end) &
4829 ((unsigned long)PAGE_CACHE_SIZE - 1);
4830
4831 cur = min_t(unsigned long, len, src_off_in_page + 1);
4832 cur = min(cur, dst_off_in_page + 1);
4833 move_pages(extent_buffer_page(dst, dst_i),
4834 extent_buffer_page(dst, src_i),
4835 dst_off_in_page - cur + 1,
4836 src_off_in_page - cur + 1, cur);
4837
4838 dst_end -= cur;
4839 src_end -= cur;
4840 len -= cur;
4841 }
4842}
Chris Mason6af118c2008-07-22 11:18:07 -04004843
Josef Bacik3083ee22012-03-09 16:01:49 -05004844int try_release_extent_buffer(struct page *page, gfp_t mask)
Miao Xie19fe0a82010-10-26 20:57:29 -04004845{
Josef Bacik3083ee22012-03-09 16:01:49 -05004846 struct extent_buffer *eb;
Miao Xie897ca6e2010-10-26 20:57:29 -04004847
Miao Xie19fe0a82010-10-26 20:57:29 -04004848 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05004849 * We need to make sure noboody is attaching this page to an eb right
4850 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04004851 */
Josef Bacik3083ee22012-03-09 16:01:49 -05004852 spin_lock(&page->mapping->private_lock);
4853 if (!PagePrivate(page)) {
4854 spin_unlock(&page->mapping->private_lock);
4855 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004856 }
Miao Xie19fe0a82010-10-26 20:57:29 -04004857
Josef Bacik3083ee22012-03-09 16:01:49 -05004858 eb = (struct extent_buffer *)page->private;
4859 BUG_ON(!eb);
4860
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004861 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05004862 * This is a little awful but should be ok, we need to make sure that
4863 * the eb doesn't disappear out from under us while we're looking at
4864 * this page.
4865 */
4866 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004867 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05004868 spin_unlock(&eb->refs_lock);
4869 spin_unlock(&page->mapping->private_lock);
4870 return 0;
4871 }
4872 spin_unlock(&page->mapping->private_lock);
4873
4874 if ((mask & GFP_NOFS) == GFP_NOFS)
4875 mask = GFP_NOFS;
4876
4877 /*
4878 * If tree ref isn't set then we know the ref on this eb is a real ref,
4879 * so just return, this page will likely be freed soon anyway.
4880 */
4881 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
4882 spin_unlock(&eb->refs_lock);
4883 return 0;
4884 }
4885 release_extent_buffer(eb, mask);
4886
4887 return 1;
Chris Mason6af118c2008-07-22 11:18:07 -04004888}