blob: d64fda54148372938b9f77c46f6b6cce4c5d7b40 [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
Li Zefancdc6a392012-03-12 16:39:48 +0800405static struct extent_state *next_state(struct extent_state *state)
406{
407 struct rb_node *next = rb_next(&state->rb_node);
408 if (next)
409 return rb_entry(next, struct extent_state, rb_node);
410 else
411 return NULL;
412}
413
Chris Masond1310b22008-01-24 16:13:08 -0500414/*
415 * utility function to clear some bits in an extent state struct.
Li Zefan8e52acf2012-03-12 16:39:28 +0800416 * it will optionally wake up any one waiting on this state (wake == 1)
Chris Masond1310b22008-01-24 16:13:08 -0500417 *
418 * If no bits are set on the state struct after clearing things, the
419 * struct is freed and removed from the tree
420 */
Li Zefancdc6a392012-03-12 16:39:48 +0800421static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
422 struct extent_state *state,
423 int *bits, int wake)
Chris Masond1310b22008-01-24 16:13:08 -0500424{
Li Zefancdc6a392012-03-12 16:39:48 +0800425 struct extent_state *next;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400426 int bits_to_clear = *bits & ~EXTENT_CTLBITS;
Chris Masond1310b22008-01-24 16:13:08 -0500427
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400428 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500429 u64 range = state->end - state->start + 1;
430 WARN_ON(range > tree->dirty_bytes);
431 tree->dirty_bytes -= range;
432 }
Chris Mason291d6732008-01-29 15:55:23 -0500433 clear_state_cb(tree, state, bits);
Josef Bacik32c00af2009-10-08 13:34:05 -0400434 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500435 if (wake)
436 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400437 if (state->state == 0) {
Li Zefancdc6a392012-03-12 16:39:48 +0800438 next = next_state(state);
Chris Mason70dec802008-01-29 09:59:12 -0500439 if (state->tree) {
Chris Masond1310b22008-01-24 16:13:08 -0500440 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500441 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500442 free_extent_state(state);
443 } else {
444 WARN_ON(1);
445 }
446 } else {
447 merge_state(tree, state);
Li Zefancdc6a392012-03-12 16:39:48 +0800448 next = next_state(state);
Chris Masond1310b22008-01-24 16:13:08 -0500449 }
Li Zefancdc6a392012-03-12 16:39:48 +0800450 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500451}
452
Xiao Guangrong82337672011-04-20 06:44:57 +0000453static struct extent_state *
454alloc_extent_state_atomic(struct extent_state *prealloc)
455{
456 if (!prealloc)
457 prealloc = alloc_extent_state(GFP_ATOMIC);
458
459 return prealloc;
460}
461
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400462void extent_io_tree_panic(struct extent_io_tree *tree, int err)
463{
464 btrfs_panic(tree_fs_info(tree), err, "Locking error: "
465 "Extent tree was modified by another "
466 "thread while locked.");
467}
468
Chris Masond1310b22008-01-24 16:13:08 -0500469/*
470 * clear some bits on a range in the tree. This may require splitting
471 * or inserting elements in the tree, so the gfp mask is used to
472 * indicate which allocations or sleeping are allowed.
473 *
474 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
475 * the given range from the tree regardless of state (ie for truncate).
476 *
477 * the range [start, end] is inclusive.
478 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100479 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500480 */
481int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400482 int bits, int wake, int delete,
483 struct extent_state **cached_state,
484 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500485{
486 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400487 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500488 struct extent_state *prealloc = NULL;
489 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400490 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500491 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000492 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500493
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400494 if (delete)
495 bits |= ~EXTENT_CTLBITS;
496 bits |= EXTENT_FIRST_DELALLOC;
497
Josef Bacik2ac55d42010-02-03 19:33:23 +0000498 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
499 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500500again:
501 if (!prealloc && (mask & __GFP_WAIT)) {
502 prealloc = alloc_extent_state(mask);
503 if (!prealloc)
504 return -ENOMEM;
505 }
506
Chris Masoncad321a2008-12-17 14:51:42 -0500507 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400508 if (cached_state) {
509 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000510
511 if (clear) {
512 *cached_state = NULL;
513 cached_state = NULL;
514 }
515
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400516 if (cached && cached->tree && cached->start <= start &&
517 cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000518 if (clear)
519 atomic_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400520 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400521 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400522 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000523 if (clear)
524 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400525 }
Chris Masond1310b22008-01-24 16:13:08 -0500526 /*
527 * this search will find the extents that end after
528 * our range starts
529 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500530 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500531 if (!node)
532 goto out;
533 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400534hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500535 if (state->start > end)
536 goto out;
537 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400538 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500539
Liu Bo04493142012-02-16 18:34:37 +0800540 /* the state doesn't have the wanted bits, go ahead */
Li Zefancdc6a392012-03-12 16:39:48 +0800541 if (!(state->state & bits)) {
542 state = next_state(state);
Liu Bo04493142012-02-16 18:34:37 +0800543 goto next;
Li Zefancdc6a392012-03-12 16:39:48 +0800544 }
Liu Bo04493142012-02-16 18:34:37 +0800545
Chris Masond1310b22008-01-24 16:13:08 -0500546 /*
547 * | ---- desired range ---- |
548 * | state | or
549 * | ------------- state -------------- |
550 *
551 * We need to split the extent we found, and may flip
552 * bits on second half.
553 *
554 * If the extent we found extends past our range, we
555 * just split and search again. It'll get split again
556 * the next time though.
557 *
558 * If the extent we found is inside our range, we clear
559 * the desired bit on it.
560 */
561
562 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000563 prealloc = alloc_extent_state_atomic(prealloc);
564 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500565 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400566 if (err)
567 extent_io_tree_panic(tree, err);
568
Chris Masond1310b22008-01-24 16:13:08 -0500569 prealloc = NULL;
570 if (err)
571 goto out;
572 if (state->end <= end) {
Jeff Mahoney6763af82012-03-01 14:56:29 +0100573 clear_state_bit(tree, state, &bits, wake);
Yan Zheng5c939df2009-05-27 09:16:03 -0400574 if (last_end == (u64)-1)
575 goto out;
576 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500577 }
578 goto search_again;
579 }
580 /*
581 * | ---- desired range ---- |
582 * | state |
583 * We need to split the extent, and clear the bit
584 * on the first half
585 */
586 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000587 prealloc = alloc_extent_state_atomic(prealloc);
588 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500589 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400590 if (err)
591 extent_io_tree_panic(tree, err);
592
Chris Masond1310b22008-01-24 16:13:08 -0500593 if (wake)
594 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400595
Jeff Mahoney6763af82012-03-01 14:56:29 +0100596 clear_state_bit(tree, prealloc, &bits, wake);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400597
Chris Masond1310b22008-01-24 16:13:08 -0500598 prealloc = NULL;
599 goto out;
600 }
Chris Mason42daec22009-09-23 19:51:09 -0400601
Li Zefancdc6a392012-03-12 16:39:48 +0800602 state = clear_state_bit(tree, state, &bits, wake);
Liu Bo04493142012-02-16 18:34:37 +0800603next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400604 if (last_end == (u64)-1)
605 goto out;
606 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800607 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800608 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500609 goto search_again;
610
611out:
Chris Masoncad321a2008-12-17 14:51:42 -0500612 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500613 if (prealloc)
614 free_extent_state(prealloc);
615
Jeff Mahoney6763af82012-03-01 14:56:29 +0100616 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500617
618search_again:
619 if (start > end)
620 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500621 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500622 if (mask & __GFP_WAIT)
623 cond_resched();
624 goto again;
625}
Chris Masond1310b22008-01-24 16:13:08 -0500626
Jeff Mahoney143bede2012-03-01 14:56:26 +0100627static void wait_on_state(struct extent_io_tree *tree,
628 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500629 __releases(tree->lock)
630 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500631{
632 DEFINE_WAIT(wait);
633 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500634 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500635 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500636 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500637 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500638}
639
640/*
641 * waits for one or more bits to clear on a range in the state tree.
642 * The range [start, end] is inclusive.
643 * The tree lock is taken by this function
644 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100645void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
Chris Masond1310b22008-01-24 16:13:08 -0500646{
647 struct extent_state *state;
648 struct rb_node *node;
649
Chris Masoncad321a2008-12-17 14:51:42 -0500650 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500651again:
652 while (1) {
653 /*
654 * this search will find all the extents that end after
655 * our range starts
656 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500657 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500658 if (!node)
659 break;
660
661 state = rb_entry(node, struct extent_state, rb_node);
662
663 if (state->start > end)
664 goto out;
665
666 if (state->state & bits) {
667 start = state->start;
668 atomic_inc(&state->refs);
669 wait_on_state(tree, state);
670 free_extent_state(state);
671 goto again;
672 }
673 start = state->end + 1;
674
675 if (start > end)
676 break;
677
Xiao Guangrongded91f02011-07-14 03:19:27 +0000678 cond_resched_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500679 }
680out:
Chris Masoncad321a2008-12-17 14:51:42 -0500681 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500682}
Chris Masond1310b22008-01-24 16:13:08 -0500683
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000684static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500685 struct extent_state *state,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400686 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500687{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400688 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400689
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000690 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400691 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500692 u64 range = state->end - state->start + 1;
693 tree->dirty_bytes += range;
694 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400695 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500696}
697
Chris Mason2c64c532009-09-02 15:04:12 -0400698static void cache_state(struct extent_state *state,
699 struct extent_state **cached_ptr)
700{
701 if (cached_ptr && !(*cached_ptr)) {
702 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
703 *cached_ptr = state;
704 atomic_inc(&state->refs);
705 }
706 }
707}
708
Arne Jansen507903b2011-04-06 10:02:20 +0000709static void uncache_state(struct extent_state **cached_ptr)
710{
711 if (cached_ptr && (*cached_ptr)) {
712 struct extent_state *state = *cached_ptr;
Chris Mason109b36a2011-04-12 13:57:39 -0400713 *cached_ptr = NULL;
714 free_extent_state(state);
Arne Jansen507903b2011-04-06 10:02:20 +0000715 }
716}
717
Chris Masond1310b22008-01-24 16:13:08 -0500718/*
Chris Mason1edbb732009-09-02 13:24:36 -0400719 * set some bits on a range in the tree. This may require allocations or
720 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500721 *
Chris Mason1edbb732009-09-02 13:24:36 -0400722 * If any of the exclusive bits are set, this will fail with -EEXIST if some
723 * part of the range already has the desired bits set. The start of the
724 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500725 *
Chris Mason1edbb732009-09-02 13:24:36 -0400726 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500727 */
Chris Mason1edbb732009-09-02 13:24:36 -0400728
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100729static int __must_check
730__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
731 int bits, int exclusive_bits, u64 *failed_start,
732 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500733{
734 struct extent_state *state;
735 struct extent_state *prealloc = NULL;
736 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500737 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500738 u64 last_start;
739 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400740
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400741 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500742again:
743 if (!prealloc && (mask & __GFP_WAIT)) {
744 prealloc = alloc_extent_state(mask);
Xiao Guangrong82337672011-04-20 06:44:57 +0000745 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500746 }
747
Chris Masoncad321a2008-12-17 14:51:42 -0500748 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400749 if (cached_state && *cached_state) {
750 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400751 if (state->start <= start && state->end > start &&
752 state->tree) {
Chris Mason9655d292009-09-02 15:22:30 -0400753 node = &state->rb_node;
754 goto hit_next;
755 }
756 }
Chris Masond1310b22008-01-24 16:13:08 -0500757 /*
758 * this search will find all the extents that end after
759 * our range starts.
760 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500761 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500762 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000763 prealloc = alloc_extent_state_atomic(prealloc);
764 BUG_ON(!prealloc);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400765 err = insert_state(tree, prealloc, start, end, &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400766 if (err)
767 extent_io_tree_panic(tree, err);
768
Chris Masond1310b22008-01-24 16:13:08 -0500769 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500770 goto out;
771 }
Chris Masond1310b22008-01-24 16:13:08 -0500772 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400773hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500774 last_start = state->start;
775 last_end = state->end;
776
777 /*
778 * | ---- desired range ---- |
779 * | state |
780 *
781 * Just lock what we found and keep going
782 */
783 if (state->start == start && state->end <= end) {
Chris Mason40431d62009-08-05 12:57:59 -0400784 struct rb_node *next_node;
Chris Mason1edbb732009-09-02 13:24:36 -0400785 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500786 *failed_start = state->start;
787 err = -EEXIST;
788 goto out;
789 }
Chris Mason42daec22009-09-23 19:51:09 -0400790
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000791 set_state_bits(tree, state, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400792
Chris Mason2c64c532009-09-02 15:04:12 -0400793 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500794 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400795 if (last_end == (u64)-1)
796 goto out;
Chris Mason40431d62009-08-05 12:57:59 -0400797
Yan Zheng5c939df2009-05-27 09:16:03 -0400798 start = last_end + 1;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400799 next_node = rb_next(&state->rb_node);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000800 if (next_node && start < end && prealloc && !need_resched()) {
801 state = rb_entry(next_node, struct extent_state,
802 rb_node);
803 if (state->start == start)
804 goto hit_next;
Chris Mason40431d62009-08-05 12:57:59 -0400805 }
Chris Masond1310b22008-01-24 16:13:08 -0500806 goto search_again;
807 }
808
809 /*
810 * | ---- desired range ---- |
811 * | state |
812 * or
813 * | ------------- state -------------- |
814 *
815 * We need to split the extent we found, and may flip bits on
816 * second half.
817 *
818 * If the extent we found extends past our
819 * range, we just split and search again. It'll get split
820 * again the next time though.
821 *
822 * If the extent we found is inside our range, we set the
823 * desired bit on it.
824 */
825 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400826 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500827 *failed_start = start;
828 err = -EEXIST;
829 goto out;
830 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000831
832 prealloc = alloc_extent_state_atomic(prealloc);
833 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500834 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400835 if (err)
836 extent_io_tree_panic(tree, err);
837
Chris Masond1310b22008-01-24 16:13:08 -0500838 prealloc = NULL;
839 if (err)
840 goto out;
841 if (state->end <= end) {
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000842 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400843 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500844 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400845 if (last_end == (u64)-1)
846 goto out;
847 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500848 }
849 goto search_again;
850 }
851 /*
852 * | ---- desired range ---- |
853 * | state | or | state |
854 *
855 * There's a hole, we need to insert something in it and
856 * ignore the extent we found.
857 */
858 if (state->start > start) {
859 u64 this_end;
860 if (end < last_start)
861 this_end = end;
862 else
Chris Masond3977122009-01-05 21:25:51 -0500863 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000864
865 prealloc = alloc_extent_state_atomic(prealloc);
866 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000867
868 /*
869 * Avoid to free 'prealloc' if it can be merged with
870 * the later extent.
871 */
Chris Masond1310b22008-01-24 16:13:08 -0500872 err = insert_state(tree, prealloc, start, this_end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400873 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400874 if (err)
875 extent_io_tree_panic(tree, err);
876
Chris Mason2c64c532009-09-02 15:04:12 -0400877 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500878 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500879 start = this_end + 1;
880 goto search_again;
881 }
882 /*
883 * | ---- desired range ---- |
884 * | state |
885 * We need to split the extent, and set the bit
886 * on the first half
887 */
888 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400889 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500890 *failed_start = start;
891 err = -EEXIST;
892 goto out;
893 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000894
895 prealloc = alloc_extent_state_atomic(prealloc);
896 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500897 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400898 if (err)
899 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -0500900
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000901 set_state_bits(tree, prealloc, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400902 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500903 merge_state(tree, prealloc);
904 prealloc = NULL;
905 goto out;
906 }
907
908 goto search_again;
909
910out:
Chris Masoncad321a2008-12-17 14:51:42 -0500911 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500912 if (prealloc)
913 free_extent_state(prealloc);
914
915 return err;
916
917search_again:
918 if (start > end)
919 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500920 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500921 if (mask & __GFP_WAIT)
922 cond_resched();
923 goto again;
924}
Chris Masond1310b22008-01-24 16:13:08 -0500925
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100926int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits,
927 u64 *failed_start, struct extent_state **cached_state,
928 gfp_t mask)
929{
930 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
931 cached_state, mask);
932}
933
934
Josef Bacik462d6fa2011-09-26 13:56:12 -0400935/**
936 * convert_extent - convert all bits in a given range from one bit to another
937 * @tree: the io tree to search
938 * @start: the start offset in bytes
939 * @end: the end offset in bytes (inclusive)
940 * @bits: the bits to set in this range
941 * @clear_bits: the bits to clear in this range
942 * @mask: the allocation mask
943 *
944 * This will go through and set bits for the given range. If any states exist
945 * already in this range they are set with the given bit and cleared of the
946 * clear_bits. This is only meant to be used by things that are mergeable, ie
947 * converting from say DELALLOC to DIRTY. This is not meant to be used with
948 * boundary bits like LOCK.
949 */
950int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
951 int bits, int clear_bits, gfp_t mask)
952{
953 struct extent_state *state;
954 struct extent_state *prealloc = NULL;
955 struct rb_node *node;
956 int err = 0;
957 u64 last_start;
958 u64 last_end;
959
960again:
961 if (!prealloc && (mask & __GFP_WAIT)) {
962 prealloc = alloc_extent_state(mask);
963 if (!prealloc)
964 return -ENOMEM;
965 }
966
967 spin_lock(&tree->lock);
968 /*
969 * this search will find all the extents that end after
970 * our range starts.
971 */
972 node = tree_search(tree, start);
973 if (!node) {
974 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -0500975 if (!prealloc) {
976 err = -ENOMEM;
977 goto out;
978 }
Josef Bacik462d6fa2011-09-26 13:56:12 -0400979 err = insert_state(tree, prealloc, start, end, &bits);
980 prealloc = NULL;
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400981 if (err)
982 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -0400983 goto out;
984 }
985 state = rb_entry(node, struct extent_state, rb_node);
986hit_next:
987 last_start = state->start;
988 last_end = state->end;
989
990 /*
991 * | ---- desired range ---- |
992 * | state |
993 *
994 * Just lock what we found and keep going
995 */
996 if (state->start == start && state->end <= end) {
997 struct rb_node *next_node;
998
999 set_state_bits(tree, state, &bits);
1000 clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001001 if (last_end == (u64)-1)
1002 goto out;
1003
1004 start = last_end + 1;
1005 next_node = rb_next(&state->rb_node);
1006 if (next_node && start < end && prealloc && !need_resched()) {
1007 state = rb_entry(next_node, struct extent_state,
1008 rb_node);
1009 if (state->start == start)
1010 goto hit_next;
1011 }
1012 goto search_again;
1013 }
1014
1015 /*
1016 * | ---- desired range ---- |
1017 * | state |
1018 * or
1019 * | ------------- state -------------- |
1020 *
1021 * We need to split the extent we found, and may flip bits on
1022 * second half.
1023 *
1024 * If the extent we found extends past our
1025 * range, we just split and search again. It'll get split
1026 * again the next time though.
1027 *
1028 * If the extent we found is inside our range, we set the
1029 * desired bit on it.
1030 */
1031 if (state->start < start) {
1032 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001033 if (!prealloc) {
1034 err = -ENOMEM;
1035 goto out;
1036 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001037 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001038 if (err)
1039 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001040 prealloc = NULL;
1041 if (err)
1042 goto out;
1043 if (state->end <= end) {
1044 set_state_bits(tree, state, &bits);
1045 clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001046 if (last_end == (u64)-1)
1047 goto out;
1048 start = last_end + 1;
1049 }
1050 goto search_again;
1051 }
1052 /*
1053 * | ---- desired range ---- |
1054 * | state | or | state |
1055 *
1056 * There's a hole, we need to insert something in it and
1057 * ignore the extent we found.
1058 */
1059 if (state->start > start) {
1060 u64 this_end;
1061 if (end < last_start)
1062 this_end = end;
1063 else
1064 this_end = last_start - 1;
1065
1066 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001067 if (!prealloc) {
1068 err = -ENOMEM;
1069 goto out;
1070 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001071
1072 /*
1073 * Avoid to free 'prealloc' if it can be merged with
1074 * the later extent.
1075 */
1076 err = insert_state(tree, prealloc, start, this_end,
1077 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001078 if (err)
1079 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001080 prealloc = NULL;
1081 start = this_end + 1;
1082 goto search_again;
1083 }
1084 /*
1085 * | ---- desired range ---- |
1086 * | state |
1087 * We need to split the extent, and set the bit
1088 * on the first half
1089 */
1090 if (state->start <= end && state->end > end) {
1091 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001092 if (!prealloc) {
1093 err = -ENOMEM;
1094 goto out;
1095 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001096
1097 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001098 if (err)
1099 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001100
1101 set_state_bits(tree, prealloc, &bits);
1102 clear_state_bit(tree, prealloc, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001103 prealloc = NULL;
1104 goto out;
1105 }
1106
1107 goto search_again;
1108
1109out:
1110 spin_unlock(&tree->lock);
1111 if (prealloc)
1112 free_extent_state(prealloc);
1113
1114 return err;
1115
1116search_again:
1117 if (start > end)
1118 goto out;
1119 spin_unlock(&tree->lock);
1120 if (mask & __GFP_WAIT)
1121 cond_resched();
1122 goto again;
1123}
1124
Chris Masond1310b22008-01-24 16:13:08 -05001125/* wrappers around set/clear extent bit */
1126int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1127 gfp_t mask)
1128{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001129 return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001130 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001131}
Chris Masond1310b22008-01-24 16:13:08 -05001132
1133int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1134 int bits, gfp_t mask)
1135{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001136 return set_extent_bit(tree, start, end, bits, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001137 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001138}
Chris Masond1310b22008-01-24 16:13:08 -05001139
1140int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1141 int bits, gfp_t mask)
1142{
Chris Mason2c64c532009-09-02 15:04:12 -04001143 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001144}
Chris Masond1310b22008-01-24 16:13:08 -05001145
1146int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001147 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001148{
1149 return set_extent_bit(tree, start, end,
Liu Bofee187d2011-09-29 15:55:28 +08001150 EXTENT_DELALLOC | EXTENT_UPTODATE,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001151 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001152}
Chris Masond1310b22008-01-24 16:13:08 -05001153
1154int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1155 gfp_t mask)
1156{
1157 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04001158 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001159 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001160}
Chris Masond1310b22008-01-24 16:13:08 -05001161
1162int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1163 gfp_t mask)
1164{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001165 return set_extent_bit(tree, start, end, EXTENT_NEW, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001166 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001167}
Chris Masond1310b22008-01-24 16:13:08 -05001168
Chris Masond1310b22008-01-24 16:13:08 -05001169int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +00001170 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001171{
Arne Jansen507903b2011-04-06 10:02:20 +00001172 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001173 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001174}
Chris Masond1310b22008-01-24 16:13:08 -05001175
Chris Masond3977122009-01-05 21:25:51 -05001176static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001177 u64 end, struct extent_state **cached_state,
1178 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001179{
Chris Mason2c64c532009-09-02 15:04:12 -04001180 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001181 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001182}
Chris Masond1310b22008-01-24 16:13:08 -05001183
Chris Masond352ac62008-09-29 15:18:18 -04001184/*
1185 * either insert or lock state struct between start and end use mask to tell
1186 * us if waiting is desired.
1187 */
Chris Mason1edbb732009-09-02 13:24:36 -04001188int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001189 int bits, struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001190{
1191 int err;
1192 u64 failed_start;
1193 while (1) {
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001194 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
1195 EXTENT_LOCKED, &failed_start,
1196 cached_state, GFP_NOFS);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001197 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001198 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1199 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001200 } else
Chris Masond1310b22008-01-24 16:13:08 -05001201 break;
Chris Masond1310b22008-01-24 16:13:08 -05001202 WARN_ON(start > end);
1203 }
1204 return err;
1205}
Chris Masond1310b22008-01-24 16:13:08 -05001206
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001207int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Mason1edbb732009-09-02 13:24:36 -04001208{
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001209 return lock_extent_bits(tree, start, end, 0, NULL);
Chris Mason1edbb732009-09-02 13:24:36 -04001210}
1211
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001212int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001213{
1214 int err;
1215 u64 failed_start;
1216
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001217 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1218 &failed_start, NULL, GFP_NOFS);
Yan Zheng66435582008-10-30 14:19:50 -04001219 if (err == -EEXIST) {
1220 if (failed_start > start)
1221 clear_extent_bit(tree, start, failed_start - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001222 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
Josef Bacik25179202008-10-29 14:49:05 -04001223 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001224 }
Josef Bacik25179202008-10-29 14:49:05 -04001225 return 1;
1226}
Josef Bacik25179202008-10-29 14:49:05 -04001227
Chris Mason2c64c532009-09-02 15:04:12 -04001228int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1229 struct extent_state **cached, gfp_t mask)
1230{
1231 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1232 mask);
1233}
1234
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001235int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001236{
Chris Mason2c64c532009-09-02 15:04:12 -04001237 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001238 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001239}
Chris Masond1310b22008-01-24 16:13:08 -05001240
Chris Mason84519c02013-03-26 13:07:00 -04001241int extent_range_clear_dirty_for_io(struct inode *inode, u64 start, u64 end)
1242{
1243 unsigned long index = start >> PAGE_CACHE_SHIFT;
1244 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1245 struct page *page;
1246
1247 while (index <= end_index) {
1248 page = find_get_page(inode->i_mapping, index);
1249 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1250 clear_page_dirty_for_io(page);
1251 page_cache_release(page);
1252 index++;
1253 }
1254 return 0;
1255}
1256
1257int extent_range_redirty_for_io(struct inode *inode, u64 start, u64 end)
1258{
1259 unsigned long index = start >> PAGE_CACHE_SHIFT;
1260 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1261 struct page *page;
1262
1263 while (index <= end_index) {
1264 page = find_get_page(inode->i_mapping, index);
1265 BUG_ON(!page); /* Pages should be in the extent_io_tree */
1266 account_page_redirty(page);
1267 __set_page_dirty_nobuffers(page);
1268 page_cache_release(page);
1269 index++;
1270 }
1271 return 0;
1272}
1273
Chris Masond1310b22008-01-24 16:13:08 -05001274/*
Chris Masond1310b22008-01-24 16:13:08 -05001275 * helper function to set both pages and extents in the tree writeback
1276 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001277static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001278{
1279 unsigned long index = start >> PAGE_CACHE_SHIFT;
1280 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1281 struct page *page;
1282
1283 while (index <= end_index) {
1284 page = find_get_page(tree->mapping, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001285 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Masond1310b22008-01-24 16:13:08 -05001286 set_page_writeback(page);
1287 page_cache_release(page);
1288 index++;
1289 }
Chris Masond1310b22008-01-24 16:13:08 -05001290 return 0;
1291}
Chris Masond1310b22008-01-24 16:13:08 -05001292
Chris Masond352ac62008-09-29 15:18:18 -04001293/* find the first state struct with 'bits' set after 'start', and
1294 * return it. tree->lock must be held. NULL will returned if
1295 * nothing was found after 'start'
1296 */
Chris Masond7fc6402008-02-18 12:12:38 -05001297struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1298 u64 start, int bits)
1299{
1300 struct rb_node *node;
1301 struct extent_state *state;
1302
1303 /*
1304 * this search will find all the extents that end after
1305 * our range starts.
1306 */
1307 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001308 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001309 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001310
Chris Masond3977122009-01-05 21:25:51 -05001311 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001312 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001313 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001314 return state;
Chris Masond3977122009-01-05 21:25:51 -05001315
Chris Masond7fc6402008-02-18 12:12:38 -05001316 node = rb_next(node);
1317 if (!node)
1318 break;
1319 }
1320out:
1321 return NULL;
1322}
Chris Masond7fc6402008-02-18 12:12:38 -05001323
Chris Masond352ac62008-09-29 15:18:18 -04001324/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001325 * find the first offset in the io tree with 'bits' set. zero is
1326 * returned if we find something, and *start_ret and *end_ret are
1327 * set to reflect the state struct that was found.
1328 *
1329 * If nothing was found, 1 is returned, < 0 on error
1330 */
1331int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1332 u64 *start_ret, u64 *end_ret, int bits)
1333{
1334 struct extent_state *state;
1335 int ret = 1;
1336
1337 spin_lock(&tree->lock);
1338 state = find_first_extent_bit_state(tree, start, bits);
1339 if (state) {
1340 *start_ret = state->start;
1341 *end_ret = state->end;
1342 ret = 0;
1343 }
1344 spin_unlock(&tree->lock);
1345 return ret;
1346}
1347
1348/*
Chris Masond352ac62008-09-29 15:18:18 -04001349 * find a contiguous range of bytes in the file marked as delalloc, not
1350 * more than 'max_bytes'. start and end are used to return the range,
1351 *
1352 * 1 is returned if we find something, 0 if nothing was in the tree
1353 */
Chris Masonc8b97812008-10-29 14:49:59 -04001354static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001355 u64 *start, u64 *end, u64 max_bytes,
1356 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001357{
1358 struct rb_node *node;
1359 struct extent_state *state;
1360 u64 cur_start = *start;
1361 u64 found = 0;
1362 u64 total_bytes = 0;
1363
Chris Masoncad321a2008-12-17 14:51:42 -05001364 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001365
Chris Masond1310b22008-01-24 16:13:08 -05001366 /*
1367 * this search will find all the extents that end after
1368 * our range starts.
1369 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001370 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001371 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001372 if (!found)
1373 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001374 goto out;
1375 }
1376
Chris Masond3977122009-01-05 21:25:51 -05001377 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001378 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001379 if (found && (state->start != cur_start ||
1380 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001381 goto out;
1382 }
1383 if (!(state->state & EXTENT_DELALLOC)) {
1384 if (!found)
1385 *end = state->end;
1386 goto out;
1387 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001388 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001389 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001390 *cached_state = state;
1391 atomic_inc(&state->refs);
1392 }
Chris Masond1310b22008-01-24 16:13:08 -05001393 found++;
1394 *end = state->end;
1395 cur_start = state->end + 1;
1396 node = rb_next(node);
1397 if (!node)
1398 break;
1399 total_bytes += state->end - state->start + 1;
1400 if (total_bytes >= max_bytes)
1401 break;
1402 }
1403out:
Chris Masoncad321a2008-12-17 14:51:42 -05001404 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001405 return found;
1406}
1407
Jeff Mahoney143bede2012-03-01 14:56:26 +01001408static noinline void __unlock_for_delalloc(struct inode *inode,
1409 struct page *locked_page,
1410 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001411{
1412 int ret;
1413 struct page *pages[16];
1414 unsigned long index = start >> PAGE_CACHE_SHIFT;
1415 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1416 unsigned long nr_pages = end_index - index + 1;
1417 int i;
1418
1419 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001420 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001421
Chris Masond3977122009-01-05 21:25:51 -05001422 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001423 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001424 min_t(unsigned long, nr_pages,
1425 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001426 for (i = 0; i < ret; i++) {
1427 if (pages[i] != locked_page)
1428 unlock_page(pages[i]);
1429 page_cache_release(pages[i]);
1430 }
1431 nr_pages -= ret;
1432 index += ret;
1433 cond_resched();
1434 }
Chris Masonc8b97812008-10-29 14:49:59 -04001435}
1436
1437static noinline int lock_delalloc_pages(struct inode *inode,
1438 struct page *locked_page,
1439 u64 delalloc_start,
1440 u64 delalloc_end)
1441{
1442 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1443 unsigned long start_index = index;
1444 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1445 unsigned long pages_locked = 0;
1446 struct page *pages[16];
1447 unsigned long nrpages;
1448 int ret;
1449 int i;
1450
1451 /* the caller is responsible for locking the start index */
1452 if (index == locked_page->index && index == end_index)
1453 return 0;
1454
1455 /* skip the page at the start index */
1456 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001457 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001458 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001459 min_t(unsigned long,
1460 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001461 if (ret == 0) {
1462 ret = -EAGAIN;
1463 goto done;
1464 }
1465 /* now we have an array of pages, lock them all */
1466 for (i = 0; i < ret; i++) {
1467 /*
1468 * the caller is taking responsibility for
1469 * locked_page
1470 */
Chris Mason771ed682008-11-06 22:02:51 -05001471 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001472 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001473 if (!PageDirty(pages[i]) ||
1474 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001475 ret = -EAGAIN;
1476 unlock_page(pages[i]);
1477 page_cache_release(pages[i]);
1478 goto done;
1479 }
1480 }
Chris Masonc8b97812008-10-29 14:49:59 -04001481 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001482 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001483 }
Chris Masonc8b97812008-10-29 14:49:59 -04001484 nrpages -= ret;
1485 index += ret;
1486 cond_resched();
1487 }
1488 ret = 0;
1489done:
1490 if (ret && pages_locked) {
1491 __unlock_for_delalloc(inode, locked_page,
1492 delalloc_start,
1493 ((u64)(start_index + pages_locked - 1)) <<
1494 PAGE_CACHE_SHIFT);
1495 }
1496 return ret;
1497}
1498
1499/*
1500 * find a contiguous range of bytes in the file marked as delalloc, not
1501 * more than 'max_bytes'. start and end are used to return the range,
1502 *
1503 * 1 is returned if we find something, 0 if nothing was in the tree
1504 */
1505static noinline u64 find_lock_delalloc_range(struct inode *inode,
1506 struct extent_io_tree *tree,
1507 struct page *locked_page,
1508 u64 *start, u64 *end,
1509 u64 max_bytes)
1510{
1511 u64 delalloc_start;
1512 u64 delalloc_end;
1513 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001514 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001515 int ret;
1516 int loops = 0;
1517
1518again:
1519 /* step one, find a bunch of delalloc bytes starting at start */
1520 delalloc_start = *start;
1521 delalloc_end = 0;
1522 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001523 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001524 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001525 *start = delalloc_start;
1526 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001527 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001528 return found;
1529 }
1530
1531 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001532 * start comes from the offset of locked_page. We have to lock
1533 * pages in order, so we can't process delalloc bytes before
1534 * locked_page
1535 */
Chris Masond3977122009-01-05 21:25:51 -05001536 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001537 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001538
1539 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001540 * make sure to limit the number of pages we try to lock down
1541 * if we're looping.
1542 */
Chris Masond3977122009-01-05 21:25:51 -05001543 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001544 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001545
Chris Masonc8b97812008-10-29 14:49:59 -04001546 /* step two, lock all the pages after the page that has start */
1547 ret = lock_delalloc_pages(inode, locked_page,
1548 delalloc_start, delalloc_end);
1549 if (ret == -EAGAIN) {
1550 /* some of the pages are gone, lets avoid looping by
1551 * shortening the size of the delalloc range we're searching
1552 */
Chris Mason9655d292009-09-02 15:22:30 -04001553 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001554 if (!loops) {
1555 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1556 max_bytes = PAGE_CACHE_SIZE - offset;
1557 loops = 1;
1558 goto again;
1559 } else {
1560 found = 0;
1561 goto out_failed;
1562 }
1563 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001564 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
Chris Masonc8b97812008-10-29 14:49:59 -04001565
1566 /* step three, lock the state bits for the whole range */
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001567 lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001568
1569 /* then test to make sure it is all still delalloc */
1570 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001571 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001572 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001573 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1574 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001575 __unlock_for_delalloc(inode, locked_page,
1576 delalloc_start, delalloc_end);
1577 cond_resched();
1578 goto again;
1579 }
Chris Mason9655d292009-09-02 15:22:30 -04001580 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001581 *start = delalloc_start;
1582 *end = delalloc_end;
1583out_failed:
1584 return found;
1585}
1586
1587int extent_clear_unlock_delalloc(struct inode *inode,
1588 struct extent_io_tree *tree,
1589 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001590 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001591{
1592 int ret;
1593 struct page *pages[16];
1594 unsigned long index = start >> PAGE_CACHE_SHIFT;
1595 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1596 unsigned long nr_pages = end_index - index + 1;
1597 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001598 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001599
Chris Masona791e352009-10-08 11:27:10 -04001600 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001601 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001602 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001603 clear_bits |= EXTENT_DIRTY;
1604
Chris Masona791e352009-10-08 11:27:10 -04001605 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001606 clear_bits |= EXTENT_DELALLOC;
1607
Chris Mason2c64c532009-09-02 15:04:12 -04001608 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001609 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1610 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1611 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001612 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001613
Chris Masond3977122009-01-05 21:25:51 -05001614 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001615 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001616 min_t(unsigned long,
1617 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001618 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001619
Chris Masona791e352009-10-08 11:27:10 -04001620 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001621 SetPagePrivate2(pages[i]);
1622
Chris Masonc8b97812008-10-29 14:49:59 -04001623 if (pages[i] == locked_page) {
1624 page_cache_release(pages[i]);
1625 continue;
1626 }
Chris Masona791e352009-10-08 11:27:10 -04001627 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001628 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001629 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001630 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001631 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001632 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001633 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001634 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001635 page_cache_release(pages[i]);
1636 }
1637 nr_pages -= ret;
1638 index += ret;
1639 cond_resched();
1640 }
1641 return 0;
1642}
Chris Masonc8b97812008-10-29 14:49:59 -04001643
Chris Masond352ac62008-09-29 15:18:18 -04001644/*
1645 * count the number of bytes in the tree that have a given bit(s)
1646 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1647 * cached. The total number found is returned.
1648 */
Chris Masond1310b22008-01-24 16:13:08 -05001649u64 count_range_bits(struct extent_io_tree *tree,
1650 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001651 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001652{
1653 struct rb_node *node;
1654 struct extent_state *state;
1655 u64 cur_start = *start;
1656 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001657 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001658 int found = 0;
1659
1660 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001661 WARN_ON(1);
1662 return 0;
1663 }
1664
Chris Masoncad321a2008-12-17 14:51:42 -05001665 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001666 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1667 total_bytes = tree->dirty_bytes;
1668 goto out;
1669 }
1670 /*
1671 * this search will find all the extents that end after
1672 * our range starts.
1673 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001674 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001675 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001676 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001677
Chris Masond3977122009-01-05 21:25:51 -05001678 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001679 state = rb_entry(node, struct extent_state, rb_node);
1680 if (state->start > search_end)
1681 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001682 if (contig && found && state->start > last + 1)
1683 break;
1684 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001685 total_bytes += min(search_end, state->end) + 1 -
1686 max(cur_start, state->start);
1687 if (total_bytes >= max_bytes)
1688 break;
1689 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001690 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001691 found = 1;
1692 }
Chris Masonec29ed52011-02-23 16:23:20 -05001693 last = state->end;
1694 } else if (contig && found) {
1695 break;
Chris Masond1310b22008-01-24 16:13:08 -05001696 }
1697 node = rb_next(node);
1698 if (!node)
1699 break;
1700 }
1701out:
Chris Masoncad321a2008-12-17 14:51:42 -05001702 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001703 return total_bytes;
1704}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001705
Chris Masond352ac62008-09-29 15:18:18 -04001706/*
1707 * set the private field for a given byte offset in the tree. If there isn't
1708 * an extent_state there already, this does nothing.
1709 */
Chris Masond1310b22008-01-24 16:13:08 -05001710int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1711{
1712 struct rb_node *node;
1713 struct extent_state *state;
1714 int ret = 0;
1715
Chris Masoncad321a2008-12-17 14:51:42 -05001716 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001717 /*
1718 * this search will find all the extents that end after
1719 * our range starts.
1720 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001721 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001722 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001723 ret = -ENOENT;
1724 goto out;
1725 }
1726 state = rb_entry(node, struct extent_state, rb_node);
1727 if (state->start != start) {
1728 ret = -ENOENT;
1729 goto out;
1730 }
1731 state->private = private;
1732out:
Chris Masoncad321a2008-12-17 14:51:42 -05001733 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001734 return ret;
1735}
1736
1737int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1738{
1739 struct rb_node *node;
1740 struct extent_state *state;
1741 int ret = 0;
1742
Chris Masoncad321a2008-12-17 14:51:42 -05001743 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001744 /*
1745 * this search will find all the extents that end after
1746 * our range starts.
1747 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001748 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001749 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001750 ret = -ENOENT;
1751 goto out;
1752 }
1753 state = rb_entry(node, struct extent_state, rb_node);
1754 if (state->start != start) {
1755 ret = -ENOENT;
1756 goto out;
1757 }
1758 *private = state->private;
1759out:
Chris Masoncad321a2008-12-17 14:51:42 -05001760 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001761 return ret;
1762}
1763
1764/*
1765 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001766 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001767 * has the bits set. Otherwise, 1 is returned if any bit in the
1768 * range is found set.
1769 */
1770int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001771 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001772{
1773 struct extent_state *state = NULL;
1774 struct rb_node *node;
1775 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001776
Chris Masoncad321a2008-12-17 14:51:42 -05001777 spin_lock(&tree->lock);
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001778 if (cached && cached->tree && cached->start <= start &&
1779 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001780 node = &cached->rb_node;
1781 else
1782 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001783 while (node && start <= end) {
1784 state = rb_entry(node, struct extent_state, rb_node);
1785
1786 if (filled && state->start > start) {
1787 bitset = 0;
1788 break;
1789 }
1790
1791 if (state->start > end)
1792 break;
1793
1794 if (state->state & bits) {
1795 bitset = 1;
1796 if (!filled)
1797 break;
1798 } else if (filled) {
1799 bitset = 0;
1800 break;
1801 }
Chris Mason46562ce2009-09-23 20:23:16 -04001802
1803 if (state->end == (u64)-1)
1804 break;
1805
Chris Masond1310b22008-01-24 16:13:08 -05001806 start = state->end + 1;
1807 if (start > end)
1808 break;
1809 node = rb_next(node);
1810 if (!node) {
1811 if (filled)
1812 bitset = 0;
1813 break;
1814 }
1815 }
Chris Masoncad321a2008-12-17 14:51:42 -05001816 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001817 return bitset;
1818}
Chris Masond1310b22008-01-24 16:13:08 -05001819
1820/*
1821 * helper function to set a given page up to date if all the
1822 * extents in the tree for that page are up to date
1823 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001824static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001825{
1826 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1827 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001828 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001829 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001830}
1831
1832/*
1833 * helper function to unlock a page if all the extents in the tree
1834 * for that page are unlocked
1835 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001836static void check_page_locked(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001837{
1838 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1839 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001840 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001841 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05001842}
1843
1844/*
1845 * helper function to end page writeback if all the extents
1846 * in the tree for that page are done with writeback
1847 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001848static void check_page_writeback(struct extent_io_tree *tree,
1849 struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001850{
Chris Mason1edbb732009-09-02 13:24:36 -04001851 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001852}
1853
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001854/*
1855 * When IO fails, either with EIO or csum verification fails, we
1856 * try other mirrors that might have a good copy of the data. This
1857 * io_failure_record is used to record state as we go through all the
1858 * mirrors. If another mirror has good data, the page is set up to date
1859 * and things continue. If a good mirror can't be found, the original
1860 * bio end_io callback is called to indicate things have failed.
1861 */
1862struct io_failure_record {
1863 struct page *page;
1864 u64 start;
1865 u64 len;
1866 u64 logical;
1867 unsigned long bio_flags;
1868 int this_mirror;
1869 int failed_mirror;
1870 int in_validation;
1871};
1872
1873static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
1874 int did_repair)
1875{
1876 int ret;
1877 int err = 0;
1878 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1879
1880 set_state_private(failure_tree, rec->start, 0);
1881 ret = clear_extent_bits(failure_tree, rec->start,
1882 rec->start + rec->len - 1,
1883 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1884 if (ret)
1885 err = ret;
1886
1887 if (did_repair) {
1888 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1889 rec->start + rec->len - 1,
1890 EXTENT_DAMAGED, GFP_NOFS);
1891 if (ret && !err)
1892 err = ret;
1893 }
1894
1895 kfree(rec);
1896 return err;
1897}
1898
1899static void repair_io_failure_callback(struct bio *bio, int err)
1900{
1901 complete(bio->bi_private);
1902}
1903
1904/*
1905 * this bypasses the standard btrfs submit functions deliberately, as
1906 * the standard behavior is to write all copies in a raid setup. here we only
1907 * want to write the one bad copy. so we do the mapping for ourselves and issue
1908 * submit_bio directly.
1909 * to avoid any synchonization issues, wait for the data after writing, which
1910 * actually prevents the read that triggered the error from finishing.
1911 * currently, there can be no more than two copies of every data bit. thus,
1912 * exactly one rewrite is required.
1913 */
1914int repair_io_failure(struct btrfs_mapping_tree *map_tree, u64 start,
1915 u64 length, u64 logical, struct page *page,
1916 int mirror_num)
1917{
1918 struct bio *bio;
1919 struct btrfs_device *dev;
1920 DECLARE_COMPLETION_ONSTACK(compl);
1921 u64 map_length = 0;
1922 u64 sector;
1923 struct btrfs_bio *bbio = NULL;
1924 int ret;
1925
1926 BUG_ON(!mirror_num);
1927
1928 bio = bio_alloc(GFP_NOFS, 1);
1929 if (!bio)
1930 return -EIO;
1931 bio->bi_private = &compl;
1932 bio->bi_end_io = repair_io_failure_callback;
1933 bio->bi_size = 0;
1934 map_length = length;
1935
1936 ret = btrfs_map_block(map_tree, WRITE, logical,
1937 &map_length, &bbio, mirror_num);
1938 if (ret) {
1939 bio_put(bio);
1940 return -EIO;
1941 }
1942 BUG_ON(mirror_num != bbio->mirror_num);
1943 sector = bbio->stripes[mirror_num-1].physical >> 9;
1944 bio->bi_sector = sector;
1945 dev = bbio->stripes[mirror_num-1].dev;
1946 kfree(bbio);
1947 if (!dev || !dev->bdev || !dev->writeable) {
1948 bio_put(bio);
1949 return -EIO;
1950 }
1951 bio->bi_bdev = dev->bdev;
1952 bio_add_page(bio, page, length, start-page_offset(page));
Stefan Behrens21adbd52011-11-09 13:44:05 +01001953 btrfsic_submit_bio(WRITE_SYNC, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001954 wait_for_completion(&compl);
1955
1956 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
1957 /* try to remap that extent elsewhere? */
1958 bio_put(bio);
1959 return -EIO;
1960 }
1961
1962 printk(KERN_INFO "btrfs read error corrected: ino %lu off %llu (dev %s "
1963 "sector %llu)\n", page->mapping->host->i_ino, start,
1964 dev->name, sector);
1965
1966 bio_put(bio);
1967 return 0;
1968}
1969
Josef Bacikea466792012-03-26 21:57:36 -04001970int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
1971 int mirror_num)
1972{
1973 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1974 u64 start = eb->start;
1975 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond95603b2012-04-12 15:55:15 -04001976 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04001977
1978 for (i = 0; i < num_pages; i++) {
1979 struct page *p = extent_buffer_page(eb, i);
1980 ret = repair_io_failure(map_tree, start, PAGE_CACHE_SIZE,
1981 start, p, mirror_num);
1982 if (ret)
1983 break;
1984 start += PAGE_CACHE_SIZE;
1985 }
1986
1987 return ret;
1988}
1989
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001990/*
1991 * each time an IO finishes, we do a fast check in the IO failure tree
1992 * to see if we need to process or clean up an io_failure_record
1993 */
1994static int clean_io_failure(u64 start, struct page *page)
1995{
1996 u64 private;
1997 u64 private_failure;
1998 struct io_failure_record *failrec;
1999 struct btrfs_mapping_tree *map_tree;
2000 struct extent_state *state;
2001 int num_copies;
2002 int did_repair = 0;
2003 int ret;
2004 struct inode *inode = page->mapping->host;
2005
2006 private = 0;
2007 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
2008 (u64)-1, 1, EXTENT_DIRTY, 0);
2009 if (!ret)
2010 return 0;
2011
2012 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
2013 &private_failure);
2014 if (ret)
2015 return 0;
2016
2017 failrec = (struct io_failure_record *)(unsigned long) private_failure;
2018 BUG_ON(!failrec->this_mirror);
2019
2020 if (failrec->in_validation) {
2021 /* there was no real error, just free the record */
2022 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
2023 failrec->start);
2024 did_repair = 1;
2025 goto out;
2026 }
2027
2028 spin_lock(&BTRFS_I(inode)->io_tree.lock);
2029 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
2030 failrec->start,
2031 EXTENT_LOCKED);
2032 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
2033
2034 if (state && state->start == failrec->start) {
2035 map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree;
2036 num_copies = btrfs_num_copies(map_tree, failrec->logical,
2037 failrec->len);
2038 if (num_copies > 1) {
2039 ret = repair_io_failure(map_tree, start, failrec->len,
2040 failrec->logical, page,
2041 failrec->failed_mirror);
2042 did_repair = !ret;
2043 }
2044 }
2045
2046out:
2047 if (!ret)
2048 ret = free_io_failure(inode, failrec, did_repair);
2049
2050 return ret;
2051}
2052
2053/*
2054 * this is a generic handler for readpage errors (default
2055 * readpage_io_failed_hook). if other copies exist, read those and write back
2056 * good data to the failed position. does not investigate in remapping the
2057 * failed extent elsewhere, hoping the device will be smart enough to do this as
2058 * needed
2059 */
2060
2061static int bio_readpage_error(struct bio *failed_bio, struct page *page,
2062 u64 start, u64 end, int failed_mirror,
2063 struct extent_state *state)
2064{
2065 struct io_failure_record *failrec = NULL;
2066 u64 private;
2067 struct extent_map *em;
2068 struct inode *inode = page->mapping->host;
2069 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2070 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2071 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2072 struct bio *bio;
2073 int num_copies;
2074 int ret;
2075 int read_mode;
2076 u64 logical;
2077
2078 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2079
2080 ret = get_state_private(failure_tree, start, &private);
2081 if (ret) {
2082 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2083 if (!failrec)
2084 return -ENOMEM;
2085 failrec->start = start;
2086 failrec->len = end - start + 1;
2087 failrec->this_mirror = 0;
2088 failrec->bio_flags = 0;
2089 failrec->in_validation = 0;
2090
2091 read_lock(&em_tree->lock);
2092 em = lookup_extent_mapping(em_tree, start, failrec->len);
2093 if (!em) {
2094 read_unlock(&em_tree->lock);
2095 kfree(failrec);
2096 return -EIO;
2097 }
2098
2099 if (em->start > start || em->start + em->len < start) {
2100 free_extent_map(em);
2101 em = NULL;
2102 }
2103 read_unlock(&em_tree->lock);
2104
2105 if (!em || IS_ERR(em)) {
2106 kfree(failrec);
2107 return -EIO;
2108 }
2109 logical = start - em->start;
2110 logical = em->block_start + logical;
2111 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2112 logical = em->block_start;
2113 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2114 extent_set_compress_type(&failrec->bio_flags,
2115 em->compress_type);
2116 }
2117 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2118 "len=%llu\n", logical, start, failrec->len);
2119 failrec->logical = logical;
2120 free_extent_map(em);
2121
2122 /* set the bits in the private failure tree */
2123 ret = set_extent_bits(failure_tree, start, end,
2124 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2125 if (ret >= 0)
2126 ret = set_state_private(failure_tree, start,
2127 (u64)(unsigned long)failrec);
2128 /* set the bits in the inode's tree */
2129 if (ret >= 0)
2130 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2131 GFP_NOFS);
2132 if (ret < 0) {
2133 kfree(failrec);
2134 return ret;
2135 }
2136 } else {
2137 failrec = (struct io_failure_record *)(unsigned long)private;
2138 pr_debug("bio_readpage_error: (found) logical=%llu, "
2139 "start=%llu, len=%llu, validation=%d\n",
2140 failrec->logical, failrec->start, failrec->len,
2141 failrec->in_validation);
2142 /*
2143 * when data can be on disk more than twice, add to failrec here
2144 * (e.g. with a list for failed_mirror) to make
2145 * clean_io_failure() clean all those errors at once.
2146 */
2147 }
2148 num_copies = btrfs_num_copies(
2149 &BTRFS_I(inode)->root->fs_info->mapping_tree,
2150 failrec->logical, failrec->len);
2151 if (num_copies == 1) {
2152 /*
2153 * we only have a single copy of the data, so don't bother with
2154 * all the retry and error correction code that follows. no
2155 * matter what the error is, it is very likely to persist.
2156 */
2157 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2158 "state=%p, num_copies=%d, next_mirror %d, "
2159 "failed_mirror %d\n", state, num_copies,
2160 failrec->this_mirror, failed_mirror);
2161 free_io_failure(inode, failrec, 0);
2162 return -EIO;
2163 }
2164
2165 if (!state) {
2166 spin_lock(&tree->lock);
2167 state = find_first_extent_bit_state(tree, failrec->start,
2168 EXTENT_LOCKED);
2169 if (state && state->start != failrec->start)
2170 state = NULL;
2171 spin_unlock(&tree->lock);
2172 }
2173
2174 /*
2175 * there are two premises:
2176 * a) deliver good data to the caller
2177 * b) correct the bad sectors on disk
2178 */
2179 if (failed_bio->bi_vcnt > 1) {
2180 /*
2181 * to fulfill b), we need to know the exact failing sectors, as
2182 * we don't want to rewrite any more than the failed ones. thus,
2183 * we need separate read requests for the failed bio
2184 *
2185 * if the following BUG_ON triggers, our validation request got
2186 * merged. we need separate requests for our algorithm to work.
2187 */
2188 BUG_ON(failrec->in_validation);
2189 failrec->in_validation = 1;
2190 failrec->this_mirror = failed_mirror;
2191 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2192 } else {
2193 /*
2194 * we're ready to fulfill a) and b) alongside. get a good copy
2195 * of the failed sector and if we succeed, we have setup
2196 * everything for repair_io_failure to do the rest for us.
2197 */
2198 if (failrec->in_validation) {
2199 BUG_ON(failrec->this_mirror != failed_mirror);
2200 failrec->in_validation = 0;
2201 failrec->this_mirror = 0;
2202 }
2203 failrec->failed_mirror = failed_mirror;
2204 failrec->this_mirror++;
2205 if (failrec->this_mirror == failed_mirror)
2206 failrec->this_mirror++;
2207 read_mode = READ_SYNC;
2208 }
2209
2210 if (!state || failrec->this_mirror > num_copies) {
2211 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2212 "next_mirror %d, failed_mirror %d\n", state,
2213 num_copies, failrec->this_mirror, failed_mirror);
2214 free_io_failure(inode, failrec, 0);
2215 return -EIO;
2216 }
2217
2218 bio = bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04002219 if (!bio) {
2220 free_io_failure(inode, failrec, 0);
2221 return -EIO;
2222 }
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002223 bio->bi_private = state;
2224 bio->bi_end_io = failed_bio->bi_end_io;
2225 bio->bi_sector = failrec->logical >> 9;
2226 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2227 bio->bi_size = 0;
2228
2229 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2230
2231 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2232 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2233 failrec->this_mirror, num_copies, failrec->in_validation);
2234
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002235 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2236 failrec->this_mirror,
2237 failrec->bio_flags, 0);
2238 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002239}
2240
Chris Masond1310b22008-01-24 16:13:08 -05002241/* lots and lots of room for performance fixes in the end_bio funcs */
2242
Jeff Mahoney87826df2012-02-15 16:23:57 +01002243int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2244{
2245 int uptodate = (err == 0);
2246 struct extent_io_tree *tree;
2247 int ret;
2248
2249 tree = &BTRFS_I(page->mapping->host)->io_tree;
2250
2251 if (tree->ops && tree->ops->writepage_end_io_hook) {
2252 ret = tree->ops->writepage_end_io_hook(page, start,
2253 end, NULL, uptodate);
2254 if (ret)
2255 uptodate = 0;
2256 }
2257
2258 if (!uptodate && tree->ops &&
2259 tree->ops->writepage_io_failed_hook) {
2260 ret = tree->ops->writepage_io_failed_hook(NULL, page,
2261 start, end, NULL);
2262 /* Writeback already completed */
2263 if (ret == 0)
2264 return 1;
2265 }
2266
2267 if (!uptodate) {
2268 clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
2269 ClearPageUptodate(page);
2270 SetPageError(page);
2271 }
2272 return 0;
2273}
2274
Chris Masond1310b22008-01-24 16:13:08 -05002275/*
2276 * after a writepage IO is done, we need to:
2277 * clear the uptodate bits on error
2278 * clear the writeback bits in the extent tree for this IO
2279 * end_page_writeback if the page has no more pending IO
2280 *
2281 * Scheduling is not allowed, so the extent state tree is expected
2282 * to have one and only one object corresponding to this IO.
2283 */
Chris Masond1310b22008-01-24 16:13:08 -05002284static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002285{
Chris Masond1310b22008-01-24 16:13:08 -05002286 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04002287 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002288 u64 start;
2289 u64 end;
2290 int whole_page;
2291
Chris Masond1310b22008-01-24 16:13:08 -05002292 do {
2293 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04002294 tree = &BTRFS_I(page->mapping->host)->io_tree;
2295
Chris Masond1310b22008-01-24 16:13:08 -05002296 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2297 bvec->bv_offset;
2298 end = start + bvec->bv_len - 1;
2299
2300 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2301 whole_page = 1;
2302 else
2303 whole_page = 0;
2304
2305 if (--bvec >= bio->bi_io_vec)
2306 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04002307
Jeff Mahoney87826df2012-02-15 16:23:57 +01002308 if (end_extent_writepage(page, err, start, end))
2309 continue;
Chris Mason70dec802008-01-29 09:59:12 -05002310
Chris Masond1310b22008-01-24 16:13:08 -05002311 if (whole_page)
2312 end_page_writeback(page);
2313 else
2314 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002315 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04002316
Chris Masond1310b22008-01-24 16:13:08 -05002317 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002318}
2319
2320/*
2321 * after a readpage IO is done, we need to:
2322 * clear the uptodate bits on error
2323 * set the uptodate bits if things worked
2324 * set the page up to date if all extents in the tree are uptodate
2325 * clear the lock bit in the extent tree
2326 * unlock the page if there are no other extents locked for it
2327 *
2328 * Scheduling is not allowed, so the extent state tree is expected
2329 * to have one and only one object corresponding to this IO.
2330 */
Chris Masond1310b22008-01-24 16:13:08 -05002331static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002332{
2333 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00002334 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2335 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04002336 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002337 u64 start;
2338 u64 end;
2339 int whole_page;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002340 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002341 int ret;
2342
Chris Masond20f7042008-12-08 16:58:54 -05002343 if (err)
2344 uptodate = 0;
2345
Chris Masond1310b22008-01-24 16:13:08 -05002346 do {
2347 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00002348 struct extent_state *cached = NULL;
2349 struct extent_state *state;
2350
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002351 pr_debug("end_bio_extent_readpage: bi_vcnt=%d, idx=%d, err=%d, "
2352 "mirror=%ld\n", bio->bi_vcnt, bio->bi_idx, err,
2353 (long int)bio->bi_bdev);
David Woodhouse902b22f2008-08-20 08:51:49 -04002354 tree = &BTRFS_I(page->mapping->host)->io_tree;
2355
Chris Masond1310b22008-01-24 16:13:08 -05002356 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2357 bvec->bv_offset;
2358 end = start + bvec->bv_len - 1;
2359
2360 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2361 whole_page = 1;
2362 else
2363 whole_page = 0;
2364
Chris Mason4125bf72010-02-03 18:18:45 +00002365 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05002366 prefetchw(&bvec->bv_page->flags);
2367
Arne Jansen507903b2011-04-06 10:02:20 +00002368 spin_lock(&tree->lock);
Chris Mason0d399202011-04-16 06:55:39 -04002369 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
Chris Mason109b36a2011-04-12 13:57:39 -04002370 if (state && state->start == start) {
Arne Jansen507903b2011-04-06 10:02:20 +00002371 /*
2372 * take a reference on the state, unlock will drop
2373 * the ref
2374 */
2375 cache_state(state, &cached);
2376 }
2377 spin_unlock(&tree->lock);
2378
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002379 mirror = (int)(unsigned long)bio->bi_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002380 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05002381 ret = tree->ops->readpage_end_io_hook(page, start, end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002382 state, mirror);
Chris Masond1310b22008-01-24 16:13:08 -05002383 if (ret)
2384 uptodate = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002385 else
2386 clean_io_failure(start, page);
Chris Masond1310b22008-01-24 16:13:08 -05002387 }
Josef Bacikea466792012-03-26 21:57:36 -04002388
Josef Bacikea466792012-03-26 21:57:36 -04002389 if (!uptodate && tree->ops && tree->ops->readpage_io_failed_hook) {
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002390 ret = tree->ops->readpage_io_failed_hook(page, mirror);
Josef Bacikea466792012-03-26 21:57:36 -04002391 if (!ret && !err &&
2392 test_bit(BIO_UPTODATE, &bio->bi_flags))
2393 uptodate = 1;
2394 } else if (!uptodate) {
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002395 /*
2396 * The generic bio_readpage_error handles errors the
2397 * following way: If possible, new read requests are
2398 * created and submitted and will end up in
2399 * end_bio_extent_readpage as well (if we're lucky, not
2400 * in the !uptodate case). In that case it returns 0 and
2401 * we just go on with the next page in our bio. If it
2402 * can't handle the error it will return -EIO and we
2403 * remain responsible for that page.
2404 */
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002405 ret = bio_readpage_error(bio, page, start, end, mirror, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04002406 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04002407 uptodate =
2408 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05002409 if (err)
2410 uptodate = 0;
Arne Jansen507903b2011-04-06 10:02:20 +00002411 uncache_state(&cached);
Chris Mason7e383262008-04-09 16:28:12 -04002412 continue;
2413 }
2414 }
Chris Mason70dec802008-01-29 09:59:12 -05002415
Josef Bacik0b32f4b2012-03-13 09:38:00 -04002416 if (uptodate && tree->track_uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00002417 set_extent_uptodate(tree, start, end, &cached,
David Woodhouse902b22f2008-08-20 08:51:49 -04002418 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05002419 }
Arne Jansen507903b2011-04-06 10:02:20 +00002420 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05002421
Chris Mason70dec802008-01-29 09:59:12 -05002422 if (whole_page) {
2423 if (uptodate) {
2424 SetPageUptodate(page);
2425 } else {
2426 ClearPageUptodate(page);
2427 SetPageError(page);
2428 }
Chris Masond1310b22008-01-24 16:13:08 -05002429 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05002430 } else {
2431 if (uptodate) {
2432 check_page_uptodate(tree, page);
2433 } else {
2434 ClearPageUptodate(page);
2435 SetPageError(page);
2436 }
Chris Masond1310b22008-01-24 16:13:08 -05002437 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05002438 }
Chris Mason4125bf72010-02-03 18:18:45 +00002439 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05002440
2441 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002442}
2443
Miao Xie88f794e2010-11-22 03:02:55 +00002444struct bio *
2445btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2446 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002447{
2448 struct bio *bio;
2449
2450 bio = bio_alloc(gfp_flags, nr_vecs);
2451
2452 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2453 while (!bio && (nr_vecs /= 2))
2454 bio = bio_alloc(gfp_flags, nr_vecs);
2455 }
2456
2457 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04002458 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002459 bio->bi_bdev = bdev;
2460 bio->bi_sector = first_sector;
2461 }
2462 return bio;
2463}
2464
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002465/*
2466 * Since writes are async, they will only return -ENOMEM.
2467 * Reads can return the full range of I/O error conditions.
2468 */
Jeff Mahoney355808c2011-10-03 23:23:14 -04002469static int __must_check submit_one_bio(int rw, struct bio *bio,
2470 int mirror_num, unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002471{
Chris Masond1310b22008-01-24 16:13:08 -05002472 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002473 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2474 struct page *page = bvec->bv_page;
2475 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002476 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002477
2478 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002479
David Woodhouse902b22f2008-08-20 08:51:49 -04002480 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002481
2482 bio_get(bio);
2483
Chris Mason065631f2008-02-20 12:07:25 -05002484 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00002485 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002486 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002487 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01002488 btrfsic_submit_bio(rw, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002489
Chris Masond1310b22008-01-24 16:13:08 -05002490 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2491 ret = -EOPNOTSUPP;
2492 bio_put(bio);
2493 return ret;
2494}
2495
Jeff Mahoney3444a972011-10-03 23:23:13 -04002496static int merge_bio(struct extent_io_tree *tree, struct page *page,
2497 unsigned long offset, size_t size, struct bio *bio,
2498 unsigned long bio_flags)
2499{
2500 int ret = 0;
2501 if (tree->ops && tree->ops->merge_bio_hook)
2502 ret = tree->ops->merge_bio_hook(page, offset, size, bio,
2503 bio_flags);
2504 BUG_ON(ret < 0);
2505 return ret;
2506
2507}
2508
Chris Masond1310b22008-01-24 16:13:08 -05002509static int submit_extent_page(int rw, struct extent_io_tree *tree,
2510 struct page *page, sector_t sector,
2511 size_t size, unsigned long offset,
2512 struct block_device *bdev,
2513 struct bio **bio_ret,
2514 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04002515 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002516 int mirror_num,
2517 unsigned long prev_bio_flags,
2518 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002519{
2520 int ret = 0;
2521 struct bio *bio;
2522 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04002523 int contig = 0;
2524 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2525 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05002526 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002527
2528 if (bio_ret && *bio_ret) {
2529 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002530 if (old_compressed)
2531 contig = bio->bi_sector == sector;
2532 else
2533 contig = bio->bi_sector + (bio->bi_size >> 9) ==
2534 sector;
2535
2536 if (prev_bio_flags != bio_flags || !contig ||
Jeff Mahoney3444a972011-10-03 23:23:13 -04002537 merge_bio(tree, page, offset, page_size, bio, bio_flags) ||
Chris Masonc8b97812008-10-29 14:49:59 -04002538 bio_add_page(bio, page, page_size, offset) < page_size) {
2539 ret = submit_one_bio(rw, bio, mirror_num,
2540 prev_bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002541 if (ret < 0)
2542 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05002543 bio = NULL;
2544 } else {
2545 return 0;
2546 }
2547 }
Chris Masonc8b97812008-10-29 14:49:59 -04002548 if (this_compressed)
2549 nr = BIO_MAX_PAGES;
2550 else
2551 nr = bio_get_nr_vecs(bdev);
2552
Miao Xie88f794e2010-11-22 03:02:55 +00002553 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002554 if (!bio)
2555 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002556
Chris Masonc8b97812008-10-29 14:49:59 -04002557 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002558 bio->bi_end_io = end_io_func;
2559 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05002560
Chris Masond3977122009-01-05 21:25:51 -05002561 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002562 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002563 else
Chris Masonc8b97812008-10-29 14:49:59 -04002564 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002565
2566 return ret;
2567}
2568
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002569void attach_extent_buffer_page(struct extent_buffer *eb, struct page *page)
2570{
2571 if (!PagePrivate(page)) {
2572 SetPagePrivate(page);
2573 page_cache_get(page);
2574 set_page_private(page, (unsigned long)eb);
2575 } else {
2576 WARN_ON(page->private != (unsigned long)eb);
2577 }
2578}
2579
Chris Masond1310b22008-01-24 16:13:08 -05002580void set_page_extent_mapped(struct page *page)
2581{
2582 if (!PagePrivate(page)) {
2583 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002584 page_cache_get(page);
Chris Mason6af118c2008-07-22 11:18:07 -04002585 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002586 }
2587}
2588
Chris Masond1310b22008-01-24 16:13:08 -05002589/*
2590 * basic readpage implementation. Locked extent state structs are inserted
2591 * into the tree that are removed when the IO is done (by the end_io
2592 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002593 * XXX JDM: This needs looking at to ensure proper page locking
Chris Masond1310b22008-01-24 16:13:08 -05002594 */
2595static int __extent_read_full_page(struct extent_io_tree *tree,
2596 struct page *page,
2597 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002598 struct bio **bio, int mirror_num,
2599 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002600{
2601 struct inode *inode = page->mapping->host;
2602 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2603 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2604 u64 end;
2605 u64 cur = start;
2606 u64 extent_offset;
2607 u64 last_byte = i_size_read(inode);
2608 u64 block_start;
2609 u64 cur_end;
2610 sector_t sector;
2611 struct extent_map *em;
2612 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002613 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05002614 int ret;
2615 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002616 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002617 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002618 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002619 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002620 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002621
2622 set_page_extent_mapped(page);
2623
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002624 if (!PageUptodate(page)) {
2625 if (cleancache_get_page(page) == 0) {
2626 BUG_ON(blocksize != PAGE_SIZE);
2627 goto out;
2628 }
2629 }
2630
Chris Masond1310b22008-01-24 16:13:08 -05002631 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002632 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002633 lock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002634 ordered = btrfs_lookup_ordered_extent(inode, start);
2635 if (!ordered)
2636 break;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002637 unlock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002638 btrfs_start_ordered_extent(inode, ordered, 1);
2639 btrfs_put_ordered_extent(ordered);
2640 }
Chris Masond1310b22008-01-24 16:13:08 -05002641
Chris Masonc8b97812008-10-29 14:49:59 -04002642 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2643 char *userpage;
2644 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2645
2646 if (zero_offset) {
2647 iosize = PAGE_CACHE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002648 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002649 memset(userpage + zero_offset, 0, iosize);
2650 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002651 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002652 }
2653 }
Chris Masond1310b22008-01-24 16:13:08 -05002654 while (cur <= end) {
2655 if (cur >= last_byte) {
2656 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002657 struct extent_state *cached = NULL;
2658
David Sterba306e16c2011-04-19 14:29:38 +02002659 iosize = PAGE_CACHE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002660 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002661 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002662 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002663 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002664 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002665 &cached, GFP_NOFS);
2666 unlock_extent_cached(tree, cur, cur + iosize - 1,
2667 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002668 break;
2669 }
David Sterba306e16c2011-04-19 14:29:38 +02002670 em = get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002671 end - cur + 1, 0);
David Sterbac7040052011-04-19 18:00:01 +02002672 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002673 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002674 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002675 break;
2676 }
Chris Masond1310b22008-01-24 16:13:08 -05002677 extent_offset = cur - em->start;
2678 BUG_ON(extent_map_end(em) <= cur);
2679 BUG_ON(end < cur);
2680
Li Zefan261507a02010-12-17 14:21:50 +08002681 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002682 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002683 extent_set_compress_type(&this_bio_flag,
2684 em->compress_type);
2685 }
Chris Masonc8b97812008-10-29 14:49:59 -04002686
Chris Masond1310b22008-01-24 16:13:08 -05002687 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2688 cur_end = min(extent_map_end(em) - 1, end);
2689 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002690 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2691 disk_io_size = em->block_len;
2692 sector = em->block_start >> 9;
2693 } else {
2694 sector = (em->block_start + extent_offset) >> 9;
2695 disk_io_size = iosize;
2696 }
Chris Masond1310b22008-01-24 16:13:08 -05002697 bdev = em->bdev;
2698 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002699 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2700 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002701 free_extent_map(em);
2702 em = NULL;
2703
2704 /* we've found a hole, just zero and go on */
2705 if (block_start == EXTENT_MAP_HOLE) {
2706 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002707 struct extent_state *cached = NULL;
2708
Cong Wang7ac687d2011-11-25 23:14:28 +08002709 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002710 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002711 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002712 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002713
2714 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002715 &cached, GFP_NOFS);
2716 unlock_extent_cached(tree, cur, cur + iosize - 1,
2717 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002718 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002719 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002720 continue;
2721 }
2722 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002723 if (test_range_bit(tree, cur, cur_end,
2724 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002725 check_page_uptodate(tree, page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002726 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002727 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002728 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002729 continue;
2730 }
Chris Mason70dec802008-01-29 09:59:12 -05002731 /* we have an inline extent but it didn't get marked up
2732 * to date. Error out
2733 */
2734 if (block_start == EXTENT_MAP_INLINE) {
2735 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002736 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05002737 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002738 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05002739 continue;
2740 }
Chris Masond1310b22008-01-24 16:13:08 -05002741
2742 ret = 0;
2743 if (tree->ops && tree->ops->readpage_io_hook) {
2744 ret = tree->ops->readpage_io_hook(page, cur,
2745 cur + iosize - 1);
2746 }
2747 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002748 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2749 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002750 ret = submit_extent_page(READ, tree, page,
David Sterba306e16c2011-04-19 14:29:38 +02002751 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04002752 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002753 end_bio_extent_readpage, mirror_num,
2754 *bio_flags,
2755 this_bio_flag);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002756 BUG_ON(ret == -ENOMEM);
Chris Mason89642222008-07-24 09:41:53 -04002757 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002758 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002759 }
2760 if (ret)
2761 SetPageError(page);
2762 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002763 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002764 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002765out:
Chris Masond1310b22008-01-24 16:13:08 -05002766 if (!nr) {
2767 if (!PageError(page))
2768 SetPageUptodate(page);
2769 unlock_page(page);
2770 }
2771 return 0;
2772}
2773
2774int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002775 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05002776{
2777 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002778 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002779 int ret;
2780
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002781 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Chris Masonc8b97812008-10-29 14:49:59 -04002782 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002783 if (bio)
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002784 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002785 return ret;
2786}
Chris Masond1310b22008-01-24 16:13:08 -05002787
Chris Mason11c83492009-04-20 15:50:09 -04002788static noinline void update_nr_written(struct page *page,
2789 struct writeback_control *wbc,
2790 unsigned long nr_written)
2791{
2792 wbc->nr_to_write -= nr_written;
2793 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2794 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2795 page->mapping->writeback_index = page->index + nr_written;
2796}
2797
Chris Masond1310b22008-01-24 16:13:08 -05002798/*
2799 * the writepage semantics are similar to regular writepage. extent
2800 * records are inserted to lock ranges in the tree, and as dirty areas
2801 * are found, they are marked writeback. Then the lock bits are removed
2802 * and the end_io handler clears the writeback ranges
2803 */
2804static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2805 void *data)
2806{
2807 struct inode *inode = page->mapping->host;
2808 struct extent_page_data *epd = data;
2809 struct extent_io_tree *tree = epd->tree;
2810 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2811 u64 delalloc_start;
2812 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2813 u64 end;
2814 u64 cur = start;
2815 u64 extent_offset;
2816 u64 last_byte = i_size_read(inode);
2817 u64 block_start;
2818 u64 iosize;
2819 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002820 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002821 struct extent_map *em;
2822 struct block_device *bdev;
2823 int ret;
2824 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002825 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002826 size_t blocksize;
2827 loff_t i_size = i_size_read(inode);
2828 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2829 u64 nr_delalloc;
2830 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002831 int page_started;
2832 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002833 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002834 unsigned long nr_written = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002835 bool fill_delalloc = true;
Chris Masond1310b22008-01-24 16:13:08 -05002836
Chris Masonffbd5172009-04-20 15:50:09 -04002837 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002838 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002839 else
2840 write_flags = WRITE;
2841
liubo1abe9b82011-03-24 11:18:59 +00002842 trace___extent_writepage(page, inode, wbc);
2843
Chris Masond1310b22008-01-24 16:13:08 -05002844 WARN_ON(!PageLocked(page));
Chris Masonbf0da8c2011-11-04 12:29:37 -04002845
2846 ClearPageError(page);
2847
Chris Mason7f3c74f2008-07-18 12:01:11 -04002848 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002849 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002850 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002851 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002852 unlock_page(page);
2853 return 0;
2854 }
2855
2856 if (page->index == end_index) {
2857 char *userpage;
2858
Cong Wang7ac687d2011-11-25 23:14:28 +08002859 userpage = kmap_atomic(page);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002860 memset(userpage + pg_offset, 0,
2861 PAGE_CACHE_SIZE - pg_offset);
Cong Wang7ac687d2011-11-25 23:14:28 +08002862 kunmap_atomic(userpage);
Chris Mason211c17f2008-05-15 09:13:45 -04002863 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002864 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002865 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002866
2867 set_page_extent_mapped(page);
2868
Josef Bacik9e487102011-08-01 12:08:18 -04002869 if (!tree->ops || !tree->ops->fill_delalloc)
2870 fill_delalloc = false;
2871
Chris Masond1310b22008-01-24 16:13:08 -05002872 delalloc_start = start;
2873 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002874 page_started = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002875 if (!epd->extent_locked && fill_delalloc) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002876 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002877 /*
2878 * make sure the wbc mapping index is at least updated
2879 * to this page.
2880 */
2881 update_nr_written(page, wbc, 0);
2882
Chris Masond3977122009-01-05 21:25:51 -05002883 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002884 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002885 page,
2886 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002887 &delalloc_end,
2888 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002889 if (nr_delalloc == 0) {
2890 delalloc_start = delalloc_end + 1;
2891 continue;
2892 }
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002893 ret = tree->ops->fill_delalloc(inode, page,
2894 delalloc_start,
2895 delalloc_end,
2896 &page_started,
2897 &nr_written);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002898 /* File system has been set read-only */
2899 if (ret) {
2900 SetPageError(page);
2901 goto done;
2902 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002903 /*
2904 * delalloc_end is already one less than the total
2905 * length, so we don't subtract one from
2906 * PAGE_CACHE_SIZE
2907 */
2908 delalloc_to_write += (delalloc_end - delalloc_start +
2909 PAGE_CACHE_SIZE) >>
2910 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002911 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002912 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002913 if (wbc->nr_to_write < delalloc_to_write) {
2914 int thresh = 8192;
2915
2916 if (delalloc_to_write < thresh * 2)
2917 thresh = delalloc_to_write;
2918 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2919 thresh);
2920 }
Chris Masonc8b97812008-10-29 14:49:59 -04002921
Chris Mason771ed682008-11-06 22:02:51 -05002922 /* did the fill delalloc function already unlock and start
2923 * the IO?
2924 */
2925 if (page_started) {
2926 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002927 /*
2928 * we've unlocked the page, so we can't update
2929 * the mapping's writeback index, just update
2930 * nr_to_write.
2931 */
2932 wbc->nr_to_write -= nr_written;
2933 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002934 }
Chris Masonc8b97812008-10-29 14:49:59 -04002935 }
Chris Mason247e7432008-07-17 12:53:51 -04002936 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002937 ret = tree->ops->writepage_start_hook(page, start,
2938 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002939 if (ret) {
2940 /* Fixup worker will requeue */
2941 if (ret == -EBUSY)
2942 wbc->pages_skipped++;
2943 else
2944 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002945 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002946 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002947 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002948 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002949 }
2950 }
2951
Chris Mason11c83492009-04-20 15:50:09 -04002952 /*
2953 * we don't want to touch the inode after unlocking the page,
2954 * so we update the mapping writeback index now
2955 */
2956 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002957
Chris Masond1310b22008-01-24 16:13:08 -05002958 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002959 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002960 if (tree->ops && tree->ops->writepage_end_io_hook)
2961 tree->ops->writepage_end_io_hook(page, start,
2962 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002963 goto done;
2964 }
2965
Chris Masond1310b22008-01-24 16:13:08 -05002966 blocksize = inode->i_sb->s_blocksize;
2967
2968 while (cur <= end) {
2969 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002970 if (tree->ops && tree->ops->writepage_end_io_hook)
2971 tree->ops->writepage_end_io_hook(page, cur,
2972 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002973 break;
2974 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002975 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002976 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02002977 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002978 SetPageError(page);
2979 break;
2980 }
2981
2982 extent_offset = cur - em->start;
2983 BUG_ON(extent_map_end(em) <= cur);
2984 BUG_ON(end < cur);
2985 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2986 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2987 sector = (em->block_start + extent_offset) >> 9;
2988 bdev = em->bdev;
2989 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002990 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002991 free_extent_map(em);
2992 em = NULL;
2993
Chris Masonc8b97812008-10-29 14:49:59 -04002994 /*
2995 * compressed and inline extents are written through other
2996 * paths in the FS
2997 */
2998 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002999 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003000 /*
3001 * end_io notification does not happen here for
3002 * compressed extents
3003 */
3004 if (!compressed && tree->ops &&
3005 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003006 tree->ops->writepage_end_io_hook(page, cur,
3007 cur + iosize - 1,
3008 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003009 else if (compressed) {
3010 /* we don't want to end_page_writeback on
3011 * a compressed extent. this happens
3012 * elsewhere
3013 */
3014 nr++;
3015 }
3016
3017 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003018 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003019 continue;
3020 }
Chris Masond1310b22008-01-24 16:13:08 -05003021 /* leave this out until we have a page_mkwrite call */
3022 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04003023 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05003024 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003025 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003026 continue;
3027 }
Chris Masonc8b97812008-10-29 14:49:59 -04003028
Chris Masond1310b22008-01-24 16:13:08 -05003029 if (tree->ops && tree->ops->writepage_io_hook) {
3030 ret = tree->ops->writepage_io_hook(page, cur,
3031 cur + iosize - 1);
3032 } else {
3033 ret = 0;
3034 }
Chris Mason1259ab72008-05-12 13:39:03 -04003035 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003036 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003037 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003038 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003039
Chris Masond1310b22008-01-24 16:13:08 -05003040 set_range_writeback(tree, cur, cur + iosize - 1);
3041 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05003042 printk(KERN_ERR "btrfs warning page %lu not "
3043 "writeback, cur %llu end %llu\n",
3044 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05003045 (unsigned long long)end);
3046 }
3047
Chris Masonffbd5172009-04-20 15:50:09 -04003048 ret = submit_extent_page(write_flags, tree, page,
3049 sector, iosize, pg_offset,
3050 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04003051 end_bio_extent_writepage,
3052 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003053 if (ret)
3054 SetPageError(page);
3055 }
3056 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003057 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003058 nr++;
3059 }
3060done:
3061 if (nr == 0) {
3062 /* make sure the mapping tag for page dirty gets cleared */
3063 set_page_writeback(page);
3064 end_page_writeback(page);
3065 }
Chris Masond1310b22008-01-24 16:13:08 -05003066 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05003067
Chris Mason11c83492009-04-20 15:50:09 -04003068done_unlocked:
3069
Chris Mason2c64c532009-09-02 15:04:12 -04003070 /* drop our reference on any cached states */
3071 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05003072 return 0;
3073}
3074
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003075static int eb_wait(void *word)
3076{
3077 io_schedule();
3078 return 0;
3079}
3080
3081static void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
3082{
3083 wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
3084 TASK_UNINTERRUPTIBLE);
3085}
3086
3087static int lock_extent_buffer_for_io(struct extent_buffer *eb,
3088 struct btrfs_fs_info *fs_info,
3089 struct extent_page_data *epd)
3090{
3091 unsigned long i, num_pages;
3092 int flush = 0;
3093 int ret = 0;
3094
3095 if (!btrfs_try_tree_write_lock(eb)) {
3096 flush = 1;
3097 flush_write_bio(epd);
3098 btrfs_tree_lock(eb);
3099 }
3100
3101 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3102 btrfs_tree_unlock(eb);
3103 if (!epd->sync_io)
3104 return 0;
3105 if (!flush) {
3106 flush_write_bio(epd);
3107 flush = 1;
3108 }
Chris Masona098d8e2012-03-21 12:09:56 -04003109 while (1) {
3110 wait_on_extent_buffer_writeback(eb);
3111 btrfs_tree_lock(eb);
3112 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3113 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003114 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003115 }
3116 }
3117
3118 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3119 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3120 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
3121 spin_lock(&fs_info->delalloc_lock);
3122 if (fs_info->dirty_metadata_bytes >= eb->len)
3123 fs_info->dirty_metadata_bytes -= eb->len;
3124 else
3125 WARN_ON(1);
3126 spin_unlock(&fs_info->delalloc_lock);
3127 ret = 1;
3128 }
3129
3130 btrfs_tree_unlock(eb);
3131
3132 if (!ret)
3133 return ret;
3134
3135 num_pages = num_extent_pages(eb->start, eb->len);
3136 for (i = 0; i < num_pages; i++) {
3137 struct page *p = extent_buffer_page(eb, i);
3138
3139 if (!trylock_page(p)) {
3140 if (!flush) {
3141 flush_write_bio(epd);
3142 flush = 1;
3143 }
3144 lock_page(p);
3145 }
3146 }
3147
3148 return ret;
3149}
3150
3151static void end_extent_buffer_writeback(struct extent_buffer *eb)
3152{
3153 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3154 smp_mb__after_clear_bit();
3155 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3156}
3157
3158static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
3159{
3160 int uptodate = err == 0;
3161 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
3162 struct extent_buffer *eb;
3163 int done;
3164
3165 do {
3166 struct page *page = bvec->bv_page;
3167
3168 bvec--;
3169 eb = (struct extent_buffer *)page->private;
3170 BUG_ON(!eb);
3171 done = atomic_dec_and_test(&eb->io_pages);
3172
3173 if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
3174 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3175 ClearPageUptodate(page);
3176 SetPageError(page);
3177 }
3178
3179 end_page_writeback(page);
3180
3181 if (!done)
3182 continue;
3183
3184 end_extent_buffer_writeback(eb);
3185 } while (bvec >= bio->bi_io_vec);
3186
3187 bio_put(bio);
3188
3189}
3190
3191static int write_one_eb(struct extent_buffer *eb,
3192 struct btrfs_fs_info *fs_info,
3193 struct writeback_control *wbc,
3194 struct extent_page_data *epd)
3195{
3196 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
3197 u64 offset = eb->start;
3198 unsigned long i, num_pages;
3199 int rw = (epd->sync_io ? WRITE_SYNC : WRITE);
3200 int ret;
3201
3202 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3203 num_pages = num_extent_pages(eb->start, eb->len);
3204 atomic_set(&eb->io_pages, num_pages);
3205 for (i = 0; i < num_pages; i++) {
3206 struct page *p = extent_buffer_page(eb, i);
3207
3208 clear_page_dirty_for_io(p);
3209 set_page_writeback(p);
3210 ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
3211 PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
3212 -1, end_bio_extent_buffer_writepage,
3213 0, 0, 0);
3214 if (ret) {
3215 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3216 SetPageError(p);
3217 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3218 end_extent_buffer_writeback(eb);
3219 ret = -EIO;
3220 break;
3221 }
3222 offset += PAGE_CACHE_SIZE;
3223 update_nr_written(p, wbc, 1);
3224 unlock_page(p);
3225 }
3226
3227 if (unlikely(ret)) {
3228 for (; i < num_pages; i++) {
3229 struct page *p = extent_buffer_page(eb, i);
3230 unlock_page(p);
3231 }
3232 }
3233
3234 return ret;
3235}
3236
3237int btree_write_cache_pages(struct address_space *mapping,
3238 struct writeback_control *wbc)
3239{
3240 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3241 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3242 struct extent_buffer *eb, *prev_eb = NULL;
3243 struct extent_page_data epd = {
3244 .bio = NULL,
3245 .tree = tree,
3246 .extent_locked = 0,
3247 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3248 };
3249 int ret = 0;
3250 int done = 0;
3251 int nr_to_write_done = 0;
3252 struct pagevec pvec;
3253 int nr_pages;
3254 pgoff_t index;
3255 pgoff_t end; /* Inclusive */
3256 int scanned = 0;
3257 int tag;
3258
3259 pagevec_init(&pvec, 0);
3260 if (wbc->range_cyclic) {
3261 index = mapping->writeback_index; /* Start from prev offset */
3262 end = -1;
3263 } else {
3264 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3265 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3266 scanned = 1;
3267 }
3268 if (wbc->sync_mode == WB_SYNC_ALL)
3269 tag = PAGECACHE_TAG_TOWRITE;
3270 else
3271 tag = PAGECACHE_TAG_DIRTY;
3272retry:
3273 if (wbc->sync_mode == WB_SYNC_ALL)
3274 tag_pages_for_writeback(mapping, index, end);
3275 while (!done && !nr_to_write_done && (index <= end) &&
3276 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3277 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3278 unsigned i;
3279
3280 scanned = 1;
3281 for (i = 0; i < nr_pages; i++) {
3282 struct page *page = pvec.pages[i];
3283
3284 if (!PagePrivate(page))
3285 continue;
3286
3287 if (!wbc->range_cyclic && page->index > end) {
3288 done = 1;
3289 break;
3290 }
3291
3292 eb = (struct extent_buffer *)page->private;
3293 if (!eb) {
3294 WARN_ON(1);
3295 continue;
3296 }
3297
3298 if (eb == prev_eb)
3299 continue;
3300
3301 if (!atomic_inc_not_zero(&eb->refs)) {
3302 WARN_ON(1);
3303 continue;
3304 }
3305
3306 prev_eb = eb;
3307 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3308 if (!ret) {
3309 free_extent_buffer(eb);
3310 continue;
3311 }
3312
3313 ret = write_one_eb(eb, fs_info, wbc, &epd);
3314 if (ret) {
3315 done = 1;
3316 free_extent_buffer(eb);
3317 break;
3318 }
3319 free_extent_buffer(eb);
3320
3321 /*
3322 * the filesystem may choose to bump up nr_to_write.
3323 * We have to make sure to honor the new nr_to_write
3324 * at any time
3325 */
3326 nr_to_write_done = wbc->nr_to_write <= 0;
3327 }
3328 pagevec_release(&pvec);
3329 cond_resched();
3330 }
3331 if (!scanned && !done) {
3332 /*
3333 * We hit the last page and there is more work to be done: wrap
3334 * back to the start of the file
3335 */
3336 scanned = 1;
3337 index = 0;
3338 goto retry;
3339 }
3340 flush_write_bio(&epd);
3341 return ret;
3342}
3343
Chris Masond1310b22008-01-24 16:13:08 -05003344/**
Chris Mason4bef0842008-09-08 11:18:08 -04003345 * 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 -05003346 * @mapping: address space structure to write
3347 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3348 * @writepage: function called for each page
3349 * @data: data passed to writepage function
3350 *
3351 * If a page is already under I/O, write_cache_pages() skips it, even
3352 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3353 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3354 * and msync() need to guarantee that all the data which was dirty at the time
3355 * the call was made get new I/O started against them. If wbc->sync_mode is
3356 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3357 * existing IO to complete.
3358 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003359static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04003360 struct address_space *mapping,
3361 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003362 writepage_t writepage, void *data,
3363 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05003364{
Chris Masond1310b22008-01-24 16:13:08 -05003365 int ret = 0;
3366 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003367 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003368 struct pagevec pvec;
3369 int nr_pages;
3370 pgoff_t index;
3371 pgoff_t end; /* Inclusive */
3372 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003373 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003374
Chris Masond1310b22008-01-24 16:13:08 -05003375 pagevec_init(&pvec, 0);
3376 if (wbc->range_cyclic) {
3377 index = mapping->writeback_index; /* Start from prev offset */
3378 end = -1;
3379 } else {
3380 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3381 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003382 scanned = 1;
3383 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003384 if (wbc->sync_mode == WB_SYNC_ALL)
3385 tag = PAGECACHE_TAG_TOWRITE;
3386 else
3387 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003388retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003389 if (wbc->sync_mode == WB_SYNC_ALL)
3390 tag_pages_for_writeback(mapping, index, end);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003391 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003392 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3393 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003394 unsigned i;
3395
3396 scanned = 1;
3397 for (i = 0; i < nr_pages; i++) {
3398 struct page *page = pvec.pages[i];
3399
3400 /*
3401 * At this point we hold neither mapping->tree_lock nor
3402 * lock on the page itself: the page may be truncated or
3403 * invalidated (changing page->mapping to NULL), or even
3404 * swizzled back from swapper_space to tmpfs file
3405 * mapping
3406 */
Chris Mason01d658f2011-11-01 10:08:06 -04003407 if (tree->ops &&
3408 tree->ops->write_cache_pages_lock_hook) {
3409 tree->ops->write_cache_pages_lock_hook(page,
3410 data, flush_fn);
3411 } else {
3412 if (!trylock_page(page)) {
3413 flush_fn(data);
3414 lock_page(page);
3415 }
3416 }
Chris Masond1310b22008-01-24 16:13:08 -05003417
3418 if (unlikely(page->mapping != mapping)) {
3419 unlock_page(page);
3420 continue;
3421 }
3422
3423 if (!wbc->range_cyclic && page->index > end) {
3424 done = 1;
3425 unlock_page(page);
3426 continue;
3427 }
3428
Chris Masond2c3f4f2008-11-19 12:44:22 -05003429 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003430 if (PageWriteback(page))
3431 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05003432 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003433 }
Chris Masond1310b22008-01-24 16:13:08 -05003434
3435 if (PageWriteback(page) ||
3436 !clear_page_dirty_for_io(page)) {
3437 unlock_page(page);
3438 continue;
3439 }
3440
3441 ret = (*writepage)(page, wbc, data);
3442
3443 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3444 unlock_page(page);
3445 ret = 0;
3446 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003447 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05003448 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003449
3450 /*
3451 * the filesystem may choose to bump up nr_to_write.
3452 * We have to make sure to honor the new nr_to_write
3453 * at any time
3454 */
3455 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05003456 }
3457 pagevec_release(&pvec);
3458 cond_resched();
3459 }
3460 if (!scanned && !done) {
3461 /*
3462 * We hit the last page and there is more work to be done: wrap
3463 * back to the start of the file
3464 */
3465 scanned = 1;
3466 index = 0;
3467 goto retry;
3468 }
Chris Masond1310b22008-01-24 16:13:08 -05003469 return ret;
3470}
Chris Masond1310b22008-01-24 16:13:08 -05003471
Chris Masonffbd5172009-04-20 15:50:09 -04003472static void flush_epd_write_bio(struct extent_page_data *epd)
3473{
3474 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04003475 int rw = WRITE;
3476 int ret;
3477
Chris Masonffbd5172009-04-20 15:50:09 -04003478 if (epd->sync_io)
Jeff Mahoney355808c2011-10-03 23:23:14 -04003479 rw = WRITE_SYNC;
3480
3481 ret = submit_one_bio(rw, epd->bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003482 BUG_ON(ret < 0); /* -ENOMEM */
Chris Masonffbd5172009-04-20 15:50:09 -04003483 epd->bio = NULL;
3484 }
3485}
3486
Chris Masond2c3f4f2008-11-19 12:44:22 -05003487static noinline void flush_write_bio(void *data)
3488{
3489 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04003490 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003491}
3492
Chris Masond1310b22008-01-24 16:13:08 -05003493int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3494 get_extent_t *get_extent,
3495 struct writeback_control *wbc)
3496{
3497 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003498 struct extent_page_data epd = {
3499 .bio = NULL,
3500 .tree = tree,
3501 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003502 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003503 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05003504 };
Chris Masond1310b22008-01-24 16:13:08 -05003505
Chris Masond1310b22008-01-24 16:13:08 -05003506 ret = __extent_writepage(page, wbc, &epd);
3507
Chris Masonffbd5172009-04-20 15:50:09 -04003508 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003509 return ret;
3510}
Chris Masond1310b22008-01-24 16:13:08 -05003511
Chris Mason771ed682008-11-06 22:02:51 -05003512int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3513 u64 start, u64 end, get_extent_t *get_extent,
3514 int mode)
3515{
3516 int ret = 0;
3517 struct address_space *mapping = inode->i_mapping;
3518 struct page *page;
3519 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3520 PAGE_CACHE_SHIFT;
3521
3522 struct extent_page_data epd = {
3523 .bio = NULL,
3524 .tree = tree,
3525 .get_extent = get_extent,
3526 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04003527 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05003528 };
3529 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05003530 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05003531 .nr_to_write = nr_pages * 2,
3532 .range_start = start,
3533 .range_end = end + 1,
3534 };
3535
Chris Masond3977122009-01-05 21:25:51 -05003536 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05003537 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3538 if (clear_page_dirty_for_io(page))
3539 ret = __extent_writepage(page, &wbc_writepages, &epd);
3540 else {
3541 if (tree->ops && tree->ops->writepage_end_io_hook)
3542 tree->ops->writepage_end_io_hook(page, start,
3543 start + PAGE_CACHE_SIZE - 1,
3544 NULL, 1);
3545 unlock_page(page);
3546 }
3547 page_cache_release(page);
3548 start += PAGE_CACHE_SIZE;
3549 }
3550
Chris Masonffbd5172009-04-20 15:50:09 -04003551 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05003552 return ret;
3553}
Chris Masond1310b22008-01-24 16:13:08 -05003554
3555int extent_writepages(struct extent_io_tree *tree,
3556 struct address_space *mapping,
3557 get_extent_t *get_extent,
3558 struct writeback_control *wbc)
3559{
3560 int ret = 0;
3561 struct extent_page_data epd = {
3562 .bio = NULL,
3563 .tree = tree,
3564 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003565 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003566 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05003567 };
3568
Chris Mason4bef0842008-09-08 11:18:08 -04003569 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003570 __extent_writepage, &epd,
3571 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04003572 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003573 return ret;
3574}
Chris Masond1310b22008-01-24 16:13:08 -05003575
3576int extent_readpages(struct extent_io_tree *tree,
3577 struct address_space *mapping,
3578 struct list_head *pages, unsigned nr_pages,
3579 get_extent_t get_extent)
3580{
3581 struct bio *bio = NULL;
3582 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04003583 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003584
Chris Masond1310b22008-01-24 16:13:08 -05003585 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
3586 struct page *page = list_entry(pages->prev, struct page, lru);
3587
3588 prefetchw(&page->flags);
3589 list_del(&page->lru);
Nick Piggin28ecb602010-03-17 13:31:04 +00003590 if (!add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04003591 page->index, GFP_NOFS)) {
Chris Masonf1885912008-04-09 16:28:12 -04003592 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04003593 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003594 }
3595 page_cache_release(page);
3596 }
Chris Masond1310b22008-01-24 16:13:08 -05003597 BUG_ON(!list_empty(pages));
3598 if (bio)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003599 return submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003600 return 0;
3601}
Chris Masond1310b22008-01-24 16:13:08 -05003602
3603/*
3604 * basic invalidatepage code, this waits on any locked or writeback
3605 * ranges corresponding to the page, and then deletes any extent state
3606 * records from the tree
3607 */
3608int extent_invalidatepage(struct extent_io_tree *tree,
3609 struct page *page, unsigned long offset)
3610{
Josef Bacik2ac55d42010-02-03 19:33:23 +00003611 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003612 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
3613 u64 end = start + PAGE_CACHE_SIZE - 1;
3614 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3615
Chris Masond3977122009-01-05 21:25:51 -05003616 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003617 if (start > end)
3618 return 0;
3619
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003620 lock_extent_bits(tree, start, end, 0, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04003621 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003622 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04003623 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3624 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003625 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003626 return 0;
3627}
Chris Masond1310b22008-01-24 16:13:08 -05003628
3629/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04003630 * a helper for releasepage, this tests for areas of the page that
3631 * are locked or under IO and drops the related state bits if it is safe
3632 * to drop the page.
3633 */
3634int try_release_extent_state(struct extent_map_tree *map,
3635 struct extent_io_tree *tree, struct page *page,
3636 gfp_t mask)
3637{
3638 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3639 u64 end = start + PAGE_CACHE_SIZE - 1;
3640 int ret = 1;
3641
Chris Mason211f90e2008-07-18 11:56:15 -04003642 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04003643 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04003644 ret = 0;
3645 else {
3646 if ((mask & GFP_NOFS) == GFP_NOFS)
3647 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04003648 /*
3649 * at this point we can safely clear everything except the
3650 * locked bit and the nodatasum bit
3651 */
Chris Masone3f24cc2011-02-14 12:52:08 -05003652 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04003653 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3654 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05003655
3656 /* if clear_extent_bit failed for enomem reasons,
3657 * we can't allow the release to continue.
3658 */
3659 if (ret < 0)
3660 ret = 0;
3661 else
3662 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003663 }
3664 return ret;
3665}
Chris Mason7b13b7b2008-04-18 10:29:50 -04003666
3667/*
Chris Masond1310b22008-01-24 16:13:08 -05003668 * a helper for releasepage. As long as there are no locked extents
3669 * in the range corresponding to the page, both state records and extent
3670 * map records are removed
3671 */
3672int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05003673 struct extent_io_tree *tree, struct page *page,
3674 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05003675{
3676 struct extent_map *em;
3677 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3678 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003679
Chris Mason70dec802008-01-29 09:59:12 -05003680 if ((mask & __GFP_WAIT) &&
3681 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05003682 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05003683 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05003684 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04003685 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05003686 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09003687 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04003688 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003689 break;
3690 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003691 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3692 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04003693 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003694 free_extent_map(em);
3695 break;
3696 }
3697 if (!test_range_bit(tree, em->start,
3698 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04003699 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04003700 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05003701 remove_extent_mapping(map, em);
3702 /* once for the rb tree */
3703 free_extent_map(em);
3704 }
3705 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04003706 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003707
3708 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05003709 free_extent_map(em);
3710 }
Chris Masond1310b22008-01-24 16:13:08 -05003711 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04003712 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003713}
Chris Masond1310b22008-01-24 16:13:08 -05003714
Chris Masonec29ed52011-02-23 16:23:20 -05003715/*
3716 * helper function for fiemap, which doesn't want to see any holes.
3717 * This maps until we find something past 'last'
3718 */
3719static struct extent_map *get_extent_skip_holes(struct inode *inode,
3720 u64 offset,
3721 u64 last,
3722 get_extent_t *get_extent)
3723{
3724 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3725 struct extent_map *em;
3726 u64 len;
3727
3728 if (offset >= last)
3729 return NULL;
3730
3731 while(1) {
3732 len = last - offset;
3733 if (len == 0)
3734 break;
3735 len = (len + sectorsize - 1) & ~(sectorsize - 1);
3736 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02003737 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05003738 return em;
3739
3740 /* if this isn't a hole return it */
3741 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3742 em->block_start != EXTENT_MAP_HOLE) {
3743 return em;
3744 }
3745
3746 /* this is a hole, advance to the next extent */
3747 offset = extent_map_end(em);
3748 free_extent_map(em);
3749 if (offset >= last)
3750 break;
3751 }
3752 return NULL;
3753}
3754
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003755int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3756 __u64 start, __u64 len, get_extent_t *get_extent)
3757{
Josef Bacik975f84f2010-11-23 19:36:57 +00003758 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003759 u64 off = start;
3760 u64 max = start + len;
3761 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00003762 u32 found_type;
3763 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05003764 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003765 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003766 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00003767 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003768 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003769 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00003770 struct btrfs_path *path;
3771 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003772 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003773 u64 em_start = 0;
3774 u64 em_len = 0;
3775 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003776 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003777
3778 if (len == 0)
3779 return -EINVAL;
3780
Josef Bacik975f84f2010-11-23 19:36:57 +00003781 path = btrfs_alloc_path();
3782 if (!path)
3783 return -ENOMEM;
3784 path->leave_spinning = 1;
3785
Josef Bacik4d479cf2011-11-17 11:34:31 -05003786 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3787 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3788
Chris Masonec29ed52011-02-23 16:23:20 -05003789 /*
3790 * lookup the last file extent. We're not using i_size here
3791 * because there might be preallocation past i_size
3792 */
Josef Bacik975f84f2010-11-23 19:36:57 +00003793 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
Li Zefan33345d012011-04-20 10:31:50 +08003794 path, btrfs_ino(inode), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00003795 if (ret < 0) {
3796 btrfs_free_path(path);
3797 return ret;
3798 }
3799 WARN_ON(!ret);
3800 path->slots[0]--;
3801 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3802 struct btrfs_file_extent_item);
3803 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3804 found_type = btrfs_key_type(&found_key);
3805
Chris Masonec29ed52011-02-23 16:23:20 -05003806 /* No extents, but there might be delalloc bits */
Li Zefan33345d012011-04-20 10:31:50 +08003807 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00003808 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05003809 /* have to trust i_size as the end */
3810 last = (u64)-1;
3811 last_for_get_extent = isize;
3812 } else {
3813 /*
3814 * remember the start of the last extent. There are a
3815 * bunch of different factors that go into the length of the
3816 * extent, so its much less complex to remember where it started
3817 */
3818 last = found_key.offset;
3819 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00003820 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003821 btrfs_free_path(path);
3822
Chris Masonec29ed52011-02-23 16:23:20 -05003823 /*
3824 * we might have some extents allocated but more delalloc past those
3825 * extents. so, we trust isize unless the start of the last extent is
3826 * beyond isize
3827 */
3828 if (last < isize) {
3829 last = (u64)-1;
3830 last_for_get_extent = isize;
3831 }
3832
Josef Bacik2ac55d42010-02-03 19:33:23 +00003833 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003834 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05003835
Josef Bacik4d479cf2011-11-17 11:34:31 -05003836 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05003837 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003838 if (!em)
3839 goto out;
3840 if (IS_ERR(em)) {
3841 ret = PTR_ERR(em);
3842 goto out;
3843 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003844
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003845 while (!end) {
Chris Masonea8efc72011-03-08 11:54:40 -05003846 u64 offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003847
Chris Masonea8efc72011-03-08 11:54:40 -05003848 /* break if the extent we found is outside the range */
3849 if (em->start >= max || extent_map_end(em) < off)
3850 break;
3851
3852 /*
3853 * get_extent may return an extent that starts before our
3854 * requested range. We have to make sure the ranges
3855 * we return to fiemap always move forward and don't
3856 * overlap, so adjust the offsets here
3857 */
3858 em_start = max(em->start, off);
3859
3860 /*
3861 * record the offset from the start of the extent
3862 * for adjusting the disk offset below
3863 */
3864 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05003865 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05003866 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05003867 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003868 disko = 0;
3869 flags = 0;
3870
Chris Masonea8efc72011-03-08 11:54:40 -05003871 /*
3872 * bump off for our next call to get_extent
3873 */
3874 off = extent_map_end(em);
3875 if (off >= max)
3876 end = 1;
3877
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003878 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003879 end = 1;
3880 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003881 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003882 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3883 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003884 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003885 flags |= (FIEMAP_EXTENT_DELALLOC |
3886 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003887 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05003888 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003889 }
3890 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3891 flags |= FIEMAP_EXTENT_ENCODED;
3892
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003893 free_extent_map(em);
3894 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05003895 if ((em_start >= last) || em_len == (u64)-1 ||
3896 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003897 flags |= FIEMAP_EXTENT_LAST;
3898 end = 1;
3899 }
3900
Chris Masonec29ed52011-02-23 16:23:20 -05003901 /* now scan forward to see if this is really the last extent. */
3902 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3903 get_extent);
3904 if (IS_ERR(em)) {
3905 ret = PTR_ERR(em);
3906 goto out;
3907 }
3908 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00003909 flags |= FIEMAP_EXTENT_LAST;
3910 end = 1;
3911 }
Chris Masonec29ed52011-02-23 16:23:20 -05003912 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3913 em_len, flags);
3914 if (ret)
3915 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003916 }
3917out_free:
3918 free_extent_map(em);
3919out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00003920 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3921 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003922 return ret;
3923}
3924
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003925inline struct page *extent_buffer_page(struct extent_buffer *eb,
Chris Masond1310b22008-01-24 16:13:08 -05003926 unsigned long i)
3927{
Chris Mason727011e2010-08-06 13:21:20 -04003928 return eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05003929}
3930
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003931inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04003932{
Chris Mason6af118c2008-07-22 11:18:07 -04003933 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3934 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04003935}
3936
Chris Mason727011e2010-08-06 13:21:20 -04003937static void __free_extent_buffer(struct extent_buffer *eb)
3938{
3939#if LEAK_DEBUG
3940 unsigned long flags;
3941 spin_lock_irqsave(&leak_lock, flags);
3942 list_del(&eb->leak_list);
3943 spin_unlock_irqrestore(&leak_lock, flags);
3944#endif
3945 if (eb->pages && eb->pages != eb->inline_pages)
3946 kfree(eb->pages);
3947 kmem_cache_free(extent_buffer_cache, eb);
3948}
3949
Chris Masond1310b22008-01-24 16:13:08 -05003950static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3951 u64 start,
3952 unsigned long len,
3953 gfp_t mask)
3954{
3955 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003956#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003957 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003958#endif
Chris Masond1310b22008-01-24 16:13:08 -05003959
Chris Masond1310b22008-01-24 16:13:08 -05003960 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003961 if (eb == NULL)
3962 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003963 eb->start = start;
3964 eb->len = len;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05003965 eb->tree = tree;
Chris Masonbd681512011-07-16 15:23:14 -04003966 rwlock_init(&eb->lock);
3967 atomic_set(&eb->write_locks, 0);
3968 atomic_set(&eb->read_locks, 0);
3969 atomic_set(&eb->blocking_readers, 0);
3970 atomic_set(&eb->blocking_writers, 0);
3971 atomic_set(&eb->spinning_readers, 0);
3972 atomic_set(&eb->spinning_writers, 0);
Arne Jansen5b25f702011-09-13 10:55:48 +02003973 eb->lock_nested = 0;
Chris Masonbd681512011-07-16 15:23:14 -04003974 init_waitqueue_head(&eb->write_lock_wq);
3975 init_waitqueue_head(&eb->read_lock_wq);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003976
Chris Mason39351272009-02-04 09:24:05 -05003977#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003978 spin_lock_irqsave(&leak_lock, flags);
3979 list_add(&eb->leak_list, &buffers);
3980 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003981#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05003982 spin_lock_init(&eb->refs_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003983 atomic_set(&eb->refs, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003984 atomic_set(&eb->io_pages, 0);
Chris Mason727011e2010-08-06 13:21:20 -04003985
3986 if (len > MAX_INLINE_EXTENT_BUFFER_SIZE) {
3987 struct page **pages;
3988 int num_pages = (len + PAGE_CACHE_SIZE - 1) >>
3989 PAGE_CACHE_SHIFT;
3990 pages = kzalloc(num_pages, mask);
3991 if (!pages) {
3992 __free_extent_buffer(eb);
3993 return NULL;
3994 }
3995 eb->pages = pages;
3996 } else {
3997 eb->pages = eb->inline_pages;
3998 }
Chris Masond1310b22008-01-24 16:13:08 -05003999
4000 return eb;
4001}
4002
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004003static int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004004{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004005 return (atomic_read(&eb->io_pages) ||
4006 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4007 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004008}
4009
Miao Xie897ca6e2010-10-26 20:57:29 -04004010/*
4011 * Helper for releasing extent buffer page.
4012 */
4013static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
4014 unsigned long start_idx)
4015{
4016 unsigned long index;
4017 struct page *page;
4018
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004019 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e2010-10-26 20:57:29 -04004020
4021 index = num_extent_pages(eb->start, eb->len);
4022 if (start_idx >= index)
4023 return;
4024
4025 do {
4026 index--;
4027 page = extent_buffer_page(eb, index);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004028 if (page) {
4029 spin_lock(&page->mapping->private_lock);
4030 /*
4031 * We do this since we'll remove the pages after we've
4032 * removed the eb from the radix tree, so we could race
4033 * and have this page now attached to the new eb. So
4034 * only clear page_private if it's still connected to
4035 * this eb.
4036 */
4037 if (PagePrivate(page) &&
4038 page->private == (unsigned long)eb) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004039 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Josef Bacik3083ee22012-03-09 16:01:49 -05004040 BUG_ON(PageDirty(page));
4041 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004042 /*
4043 * We need to make sure we haven't be attached
4044 * to a new eb.
4045 */
4046 ClearPagePrivate(page);
4047 set_page_private(page, 0);
4048 /* One for the page private */
4049 page_cache_release(page);
4050 }
4051 spin_unlock(&page->mapping->private_lock);
4052
4053 /* One for when we alloced the page */
Miao Xie897ca6e2010-10-26 20:57:29 -04004054 page_cache_release(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004055 }
Miao Xie897ca6e2010-10-26 20:57:29 -04004056 } while (index != start_idx);
4057}
4058
4059/*
4060 * Helper for releasing the extent buffer.
4061 */
4062static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4063{
4064 btrfs_release_extent_buffer_page(eb, 0);
4065 __free_extent_buffer(eb);
4066}
4067
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004068static void check_buffer_tree_ref(struct extent_buffer *eb)
4069{
4070 /* the ref bit is tricky. We have to make sure it is set
4071 * if we have the buffer dirty. Otherwise the
4072 * code to free a buffer can end up dropping a dirty
4073 * page
4074 *
4075 * Once the ref bit is set, it won't go away while the
4076 * buffer is dirty or in writeback, and it also won't
4077 * go away while we have the reference count on the
4078 * eb bumped.
4079 *
4080 * We can't just set the ref bit without bumping the
4081 * ref on the eb because free_extent_buffer might
4082 * see the ref bit and try to clear it. If this happens
4083 * free_extent_buffer might end up dropping our original
4084 * ref by mistake and freeing the page before we are able
4085 * to add one more ref.
4086 *
4087 * So bump the ref count first, then set the bit. If someone
4088 * beat us to it, drop the ref we added.
4089 */
4090 if (!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
4091 atomic_inc(&eb->refs);
4092 if (test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4093 atomic_dec(&eb->refs);
4094 }
4095}
4096
Josef Bacik5df42352012-03-15 18:24:42 -04004097static void mark_extent_buffer_accessed(struct extent_buffer *eb)
4098{
4099 unsigned long num_pages, i;
4100
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004101 check_buffer_tree_ref(eb);
4102
Josef Bacik5df42352012-03-15 18:24:42 -04004103 num_pages = num_extent_pages(eb->start, eb->len);
4104 for (i = 0; i < num_pages; i++) {
4105 struct page *p = extent_buffer_page(eb, i);
4106 mark_page_accessed(p);
4107 }
4108}
4109
Chris Masond1310b22008-01-24 16:13:08 -05004110struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
Chris Mason727011e2010-08-06 13:21:20 -04004111 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004112{
4113 unsigned long num_pages = num_extent_pages(start, len);
4114 unsigned long i;
4115 unsigned long index = start >> PAGE_CACHE_SHIFT;
4116 struct extent_buffer *eb;
Chris Mason6af118c2008-07-22 11:18:07 -04004117 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004118 struct page *p;
4119 struct address_space *mapping = tree->mapping;
4120 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004121 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004122
Miao Xie19fe0a82010-10-26 20:57:29 -04004123 rcu_read_lock();
4124 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4125 if (eb && atomic_inc_not_zero(&eb->refs)) {
4126 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004127 mark_extent_buffer_accessed(eb);
Chris Mason6af118c2008-07-22 11:18:07 -04004128 return eb;
4129 }
Miao Xie19fe0a82010-10-26 20:57:29 -04004130 rcu_read_unlock();
Chris Mason6af118c2008-07-22 11:18:07 -04004131
David Sterbaba144192011-04-21 01:12:06 +02004132 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
Peter2b114d12008-04-01 11:21:40 -04004133 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004134 return NULL;
4135
Chris Mason727011e2010-08-06 13:21:20 -04004136 for (i = 0; i < num_pages; i++, index++) {
Chris Masona6591712011-07-19 12:04:14 -04004137 p = find_or_create_page(mapping, index, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05004138 if (!p) {
4139 WARN_ON(1);
Chris Mason6af118c2008-07-22 11:18:07 -04004140 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05004141 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004142
4143 spin_lock(&mapping->private_lock);
4144 if (PagePrivate(p)) {
4145 /*
4146 * We could have already allocated an eb for this page
4147 * and attached one so lets see if we can get a ref on
4148 * the existing eb, and if we can we know it's good and
4149 * we can just return that one, else we know we can just
4150 * overwrite page->private.
4151 */
4152 exists = (struct extent_buffer *)p->private;
4153 if (atomic_inc_not_zero(&exists->refs)) {
4154 spin_unlock(&mapping->private_lock);
4155 unlock_page(p);
Josef Bacik17de39a2012-05-04 15:16:06 -04004156 page_cache_release(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004157 mark_extent_buffer_accessed(exists);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004158 goto free_eb;
4159 }
4160
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004161 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004162 * Do this so attach doesn't complain and we need to
4163 * drop the ref the old guy had.
4164 */
4165 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004166 WARN_ON(PageDirty(p));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004167 page_cache_release(p);
Chris Masond1310b22008-01-24 16:13:08 -05004168 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004169 attach_extent_buffer_page(eb, p);
4170 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004171 WARN_ON(PageDirty(p));
Chris Masond1310b22008-01-24 16:13:08 -05004172 mark_page_accessed(p);
Chris Mason727011e2010-08-06 13:21:20 -04004173 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05004174 if (!PageUptodate(p))
4175 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05004176
4177 /*
4178 * see below about how we avoid a nasty race with release page
4179 * and why we unlock later
4180 */
Chris Masond1310b22008-01-24 16:13:08 -05004181 }
4182 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004183 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05004184again:
Miao Xie19fe0a82010-10-26 20:57:29 -04004185 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4186 if (ret)
4187 goto free_eb;
4188
Chris Mason6af118c2008-07-22 11:18:07 -04004189 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004190 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
4191 if (ret == -EEXIST) {
4192 exists = radix_tree_lookup(&tree->buffer,
4193 start >> PAGE_CACHE_SHIFT);
Josef Bacik115391d2012-03-09 09:51:43 -05004194 if (!atomic_inc_not_zero(&exists->refs)) {
4195 spin_unlock(&tree->buffer_lock);
4196 radix_tree_preload_end();
Josef Bacik115391d2012-03-09 09:51:43 -05004197 exists = NULL;
4198 goto again;
4199 }
Chris Mason6af118c2008-07-22 11:18:07 -04004200 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004201 radix_tree_preload_end();
Josef Bacik5df42352012-03-15 18:24:42 -04004202 mark_extent_buffer_accessed(exists);
Chris Mason6af118c2008-07-22 11:18:07 -04004203 goto free_eb;
4204 }
Chris Mason6af118c2008-07-22 11:18:07 -04004205 /* add one reference for the tree */
Josef Bacik3083ee22012-03-09 16:01:49 -05004206 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004207 check_buffer_tree_ref(eb);
Josef Bacik3083ee22012-03-09 16:01:49 -05004208 spin_unlock(&eb->refs_lock);
Yan, Zhengf044ba72010-02-04 08:46:56 +00004209 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004210 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05004211
4212 /*
4213 * there is a race where release page may have
4214 * tried to find this extent buffer in the radix
4215 * but failed. It will tell the VM it is safe to
4216 * reclaim the, and it will clear the page private bit.
4217 * We must make sure to set the page private bit properly
4218 * after the extent buffer is in the radix tree so
4219 * it doesn't get lost
4220 */
Chris Mason727011e2010-08-06 13:21:20 -04004221 SetPageChecked(eb->pages[0]);
4222 for (i = 1; i < num_pages; i++) {
4223 p = extent_buffer_page(eb, i);
Chris Mason727011e2010-08-06 13:21:20 -04004224 ClearPageChecked(p);
4225 unlock_page(p);
4226 }
4227 unlock_page(eb->pages[0]);
Chris Masond1310b22008-01-24 16:13:08 -05004228 return eb;
4229
Chris Mason6af118c2008-07-22 11:18:07 -04004230free_eb:
Chris Mason727011e2010-08-06 13:21:20 -04004231 for (i = 0; i < num_pages; i++) {
4232 if (eb->pages[i])
4233 unlock_page(eb->pages[i]);
4234 }
Chris Masoneb14ab82011-02-10 12:35:00 -05004235
Josef Bacik17de39a2012-05-04 15:16:06 -04004236 WARN_ON(!atomic_dec_and_test(&eb->refs));
Miao Xie897ca6e2010-10-26 20:57:29 -04004237 btrfs_release_extent_buffer(eb);
Chris Mason6af118c2008-07-22 11:18:07 -04004238 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05004239}
Chris Masond1310b22008-01-24 16:13:08 -05004240
4241struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
David Sterbaf09d1f62011-04-21 01:08:01 +02004242 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004243{
Chris Masond1310b22008-01-24 16:13:08 -05004244 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05004245
Miao Xie19fe0a82010-10-26 20:57:29 -04004246 rcu_read_lock();
4247 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4248 if (eb && atomic_inc_not_zero(&eb->refs)) {
4249 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004250 mark_extent_buffer_accessed(eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04004251 return eb;
4252 }
4253 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04004254
Miao Xie19fe0a82010-10-26 20:57:29 -04004255 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004256}
Chris Masond1310b22008-01-24 16:13:08 -05004257
Josef Bacik3083ee22012-03-09 16:01:49 -05004258static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4259{
4260 struct extent_buffer *eb =
4261 container_of(head, struct extent_buffer, rcu_head);
4262
4263 __free_extent_buffer(eb);
4264}
4265
Josef Bacik3083ee22012-03-09 16:01:49 -05004266/* Expects to have eb->eb_lock already held */
4267static void release_extent_buffer(struct extent_buffer *eb, gfp_t mask)
4268{
4269 WARN_ON(atomic_read(&eb->refs) == 0);
4270 if (atomic_dec_and_test(&eb->refs)) {
4271 struct extent_io_tree *tree = eb->tree;
Josef Bacik3083ee22012-03-09 16:01:49 -05004272
4273 spin_unlock(&eb->refs_lock);
4274
Josef Bacik3083ee22012-03-09 16:01:49 -05004275 spin_lock(&tree->buffer_lock);
4276 radix_tree_delete(&tree->buffer,
4277 eb->start >> PAGE_CACHE_SHIFT);
4278 spin_unlock(&tree->buffer_lock);
4279
4280 /* Should be safe to release our pages at this point */
4281 btrfs_release_extent_buffer_page(eb, 0);
4282
4283 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
4284 return;
4285 }
4286 spin_unlock(&eb->refs_lock);
4287}
4288
Chris Masond1310b22008-01-24 16:13:08 -05004289void free_extent_buffer(struct extent_buffer *eb)
4290{
Chris Masond1310b22008-01-24 16:13:08 -05004291 if (!eb)
4292 return;
4293
Josef Bacik3083ee22012-03-09 16:01:49 -05004294 spin_lock(&eb->refs_lock);
4295 if (atomic_read(&eb->refs) == 2 &&
4296 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004297 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004298 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4299 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05004300
Josef Bacik3083ee22012-03-09 16:01:49 -05004301 /*
4302 * I know this is terrible, but it's temporary until we stop tracking
4303 * the uptodate bits and such for the extent buffers.
4304 */
4305 release_extent_buffer(eb, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05004306}
Chris Masond1310b22008-01-24 16:13:08 -05004307
Josef Bacik3083ee22012-03-09 16:01:49 -05004308void free_extent_buffer_stale(struct extent_buffer *eb)
4309{
4310 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004311 return;
4312
Josef Bacik3083ee22012-03-09 16:01:49 -05004313 spin_lock(&eb->refs_lock);
4314 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
4315
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004316 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004317 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4318 atomic_dec(&eb->refs);
4319 release_extent_buffer(eb, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05004320}
4321
Chris Mason1d4284b2012-03-28 20:31:37 -04004322void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004323{
Chris Masond1310b22008-01-24 16:13:08 -05004324 unsigned long i;
4325 unsigned long num_pages;
4326 struct page *page;
4327
Chris Masond1310b22008-01-24 16:13:08 -05004328 num_pages = num_extent_pages(eb->start, eb->len);
4329
4330 for (i = 0; i < num_pages; i++) {
4331 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04004332 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05004333 continue;
4334
Chris Masona61e6f22008-07-22 11:18:08 -04004335 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05004336 WARN_ON(!PagePrivate(page));
4337
Chris Masond1310b22008-01-24 16:13:08 -05004338 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004339 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05004340 if (!PageDirty(page)) {
4341 radix_tree_tag_clear(&page->mapping->page_tree,
4342 page_index(page),
4343 PAGECACHE_TAG_DIRTY);
4344 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004345 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04004346 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04004347 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05004348 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004349 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05004350}
Chris Masond1310b22008-01-24 16:13:08 -05004351
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004352int set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004353{
4354 unsigned long i;
4355 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04004356 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004357
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004358 check_buffer_tree_ref(eb);
4359
Chris Masonb9473432009-03-13 11:00:37 -04004360 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004361
Chris Masond1310b22008-01-24 16:13:08 -05004362 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik3083ee22012-03-09 16:01:49 -05004363 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004364 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
4365
Chris Masonb9473432009-03-13 11:00:37 -04004366 for (i = 0; i < num_pages; i++)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004367 set_page_dirty(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04004368 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05004369}
Chris Masond1310b22008-01-24 16:13:08 -05004370
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004371static int range_straddles_pages(u64 start, u64 len)
Chris Mason19b6caf2011-07-25 06:50:50 -04004372{
4373 if (len < PAGE_CACHE_SIZE)
4374 return 1;
4375 if (start & (PAGE_CACHE_SIZE - 1))
4376 return 1;
4377 if ((start + len) & (PAGE_CACHE_SIZE - 1))
4378 return 1;
4379 return 0;
4380}
4381
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004382int clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04004383{
4384 unsigned long i;
4385 struct page *page;
4386 unsigned long num_pages;
4387
Chris Masonb4ce94d2009-02-04 09:25:08 -05004388 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004389 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason1259ab72008-05-12 13:39:03 -04004390 for (i = 0; i < num_pages; i++) {
4391 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04004392 if (page)
4393 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04004394 }
4395 return 0;
4396}
4397
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004398int set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004399{
4400 unsigned long i;
4401 struct page *page;
4402 unsigned long num_pages;
4403
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004404 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004405 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05004406 for (i = 0; i < num_pages; i++) {
4407 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004408 SetPageUptodate(page);
4409 }
4410 return 0;
4411}
Chris Masond1310b22008-01-24 16:13:08 -05004412
Chris Masonce9adaa2008-04-09 16:28:12 -04004413int extent_range_uptodate(struct extent_io_tree *tree,
4414 u64 start, u64 end)
4415{
4416 struct page *page;
4417 int ret;
4418 int pg_uptodate = 1;
4419 int uptodate;
4420 unsigned long index;
4421
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004422 if (range_straddles_pages(start, end - start + 1)) {
Chris Mason19b6caf2011-07-25 06:50:50 -04004423 ret = test_range_bit(tree, start, end,
4424 EXTENT_UPTODATE, 1, NULL);
4425 if (ret)
4426 return 1;
4427 }
Chris Masond3977122009-01-05 21:25:51 -05004428 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004429 index = start >> PAGE_CACHE_SHIFT;
4430 page = find_get_page(tree->mapping, index);
Mitch Harder8bedd512012-01-26 15:01:11 -05004431 if (!page)
4432 return 1;
Chris Masonce9adaa2008-04-09 16:28:12 -04004433 uptodate = PageUptodate(page);
4434 page_cache_release(page);
4435 if (!uptodate) {
4436 pg_uptodate = 0;
4437 break;
4438 }
4439 start += PAGE_CACHE_SIZE;
4440 }
4441 return pg_uptodate;
4442}
4443
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004444int extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004445{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004446 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004447}
Chris Masond1310b22008-01-24 16:13:08 -05004448
4449int read_extent_buffer_pages(struct extent_io_tree *tree,
Arne Jansenbb82ab82011-06-10 14:06:53 +02004450 struct extent_buffer *eb, u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04004451 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05004452{
4453 unsigned long i;
4454 unsigned long start_i;
4455 struct page *page;
4456 int err;
4457 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04004458 int locked_pages = 0;
4459 int all_uptodate = 1;
Chris Masond1310b22008-01-24 16:13:08 -05004460 unsigned long num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04004461 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004462 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004463 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004464
Chris Masonb4ce94d2009-02-04 09:25:08 -05004465 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05004466 return 0;
4467
Chris Masond1310b22008-01-24 16:13:08 -05004468 if (start) {
4469 WARN_ON(start < eb->start);
4470 start_i = (start >> PAGE_CACHE_SHIFT) -
4471 (eb->start >> PAGE_CACHE_SHIFT);
4472 } else {
4473 start_i = 0;
4474 }
4475
4476 num_pages = num_extent_pages(eb->start, eb->len);
4477 for (i = start_i; i < num_pages; i++) {
4478 page = extent_buffer_page(eb, i);
Arne Jansenbb82ab82011-06-10 14:06:53 +02004479 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04004480 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04004481 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05004482 } else {
4483 lock_page(page);
4484 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004485 locked_pages++;
Chris Mason727011e2010-08-06 13:21:20 -04004486 if (!PageUptodate(page)) {
4487 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04004488 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04004489 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004490 }
4491 if (all_uptodate) {
4492 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004493 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04004494 goto unlock_exit;
4495 }
4496
Josef Bacikea466792012-03-26 21:57:36 -04004497 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04004498 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004499 atomic_set(&eb->io_pages, num_reads);
Chris Masonce9adaa2008-04-09 16:28:12 -04004500 for (i = start_i; i < num_pages; i++) {
4501 page = extent_buffer_page(eb, i);
Chris Masonce9adaa2008-04-09 16:28:12 -04004502 if (!PageUptodate(page)) {
Chris Masonf1885912008-04-09 16:28:12 -04004503 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05004504 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04004505 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04004506 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05004507 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05004508 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05004509 } else {
4510 unlock_page(page);
4511 }
4512 }
4513
Jeff Mahoney355808c2011-10-03 23:23:14 -04004514 if (bio) {
4515 err = submit_one_bio(READ, bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004516 if (err)
4517 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04004518 }
Chris Masona86c12c2008-02-07 10:50:54 -05004519
Arne Jansenbb82ab82011-06-10 14:06:53 +02004520 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05004521 return ret;
Chris Masond3977122009-01-05 21:25:51 -05004522
Chris Masond1310b22008-01-24 16:13:08 -05004523 for (i = start_i; i < num_pages; i++) {
4524 page = extent_buffer_page(eb, i);
4525 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05004526 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05004527 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05004528 }
Chris Masond3977122009-01-05 21:25:51 -05004529
Chris Masond1310b22008-01-24 16:13:08 -05004530 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04004531
4532unlock_exit:
4533 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05004534 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004535 page = extent_buffer_page(eb, i);
4536 i++;
4537 unlock_page(page);
4538 locked_pages--;
4539 }
4540 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004541}
Chris Masond1310b22008-01-24 16:13:08 -05004542
4543void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4544 unsigned long start,
4545 unsigned long len)
4546{
4547 size_t cur;
4548 size_t offset;
4549 struct page *page;
4550 char *kaddr;
4551 char *dst = (char *)dstv;
4552 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4553 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004554
4555 WARN_ON(start > eb->len);
4556 WARN_ON(start + len > eb->start + eb->len);
4557
4558 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4559
Chris Masond3977122009-01-05 21:25:51 -05004560 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004561 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004562
4563 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04004564 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004565 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004566
4567 dst += cur;
4568 len -= cur;
4569 offset = 0;
4570 i++;
4571 }
4572}
Chris Masond1310b22008-01-24 16:13:08 -05004573
4574int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04004575 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05004576 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04004577 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05004578{
4579 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4580 char *kaddr;
4581 struct page *p;
4582 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4583 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4584 unsigned long end_i = (start_offset + start + min_len - 1) >>
4585 PAGE_CACHE_SHIFT;
4586
4587 if (i != end_i)
4588 return -EINVAL;
4589
4590 if (i == 0) {
4591 offset = start_offset;
4592 *map_start = 0;
4593 } else {
4594 offset = 0;
4595 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4596 }
Chris Masond3977122009-01-05 21:25:51 -05004597
Chris Masond1310b22008-01-24 16:13:08 -05004598 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05004599 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
4600 "wanted %lu %lu\n", (unsigned long long)eb->start,
4601 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05004602 WARN_ON(1);
Josef Bacik850265332011-03-15 14:52:12 -04004603 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05004604 }
4605
4606 p = extent_buffer_page(eb, i);
Chris Masona6591712011-07-19 12:04:14 -04004607 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05004608 *map = kaddr + offset;
4609 *map_len = PAGE_CACHE_SIZE - offset;
4610 return 0;
4611}
Chris Masond1310b22008-01-24 16:13:08 -05004612
Chris Masond1310b22008-01-24 16:13:08 -05004613int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4614 unsigned long start,
4615 unsigned long len)
4616{
4617 size_t cur;
4618 size_t offset;
4619 struct page *page;
4620 char *kaddr;
4621 char *ptr = (char *)ptrv;
4622 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4623 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4624 int ret = 0;
4625
4626 WARN_ON(start > eb->len);
4627 WARN_ON(start + len > eb->start + eb->len);
4628
4629 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4630
Chris Masond3977122009-01-05 21:25:51 -05004631 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004632 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004633
4634 cur = min(len, (PAGE_CACHE_SIZE - offset));
4635
Chris Masona6591712011-07-19 12:04:14 -04004636 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004637 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004638 if (ret)
4639 break;
4640
4641 ptr += cur;
4642 len -= cur;
4643 offset = 0;
4644 i++;
4645 }
4646 return ret;
4647}
Chris Masond1310b22008-01-24 16:13:08 -05004648
4649void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4650 unsigned long start, unsigned long len)
4651{
4652 size_t cur;
4653 size_t offset;
4654 struct page *page;
4655 char *kaddr;
4656 char *src = (char *)srcv;
4657 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4658 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4659
4660 WARN_ON(start > eb->len);
4661 WARN_ON(start + len > eb->start + eb->len);
4662
4663 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4664
Chris Masond3977122009-01-05 21:25:51 -05004665 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004666 page = extent_buffer_page(eb, i);
4667 WARN_ON(!PageUptodate(page));
4668
4669 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004670 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004671 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004672
4673 src += cur;
4674 len -= cur;
4675 offset = 0;
4676 i++;
4677 }
4678}
Chris Masond1310b22008-01-24 16:13:08 -05004679
4680void memset_extent_buffer(struct extent_buffer *eb, char c,
4681 unsigned long start, unsigned long len)
4682{
4683 size_t cur;
4684 size_t offset;
4685 struct page *page;
4686 char *kaddr;
4687 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4688 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4689
4690 WARN_ON(start > eb->len);
4691 WARN_ON(start + len > eb->start + eb->len);
4692
4693 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4694
Chris Masond3977122009-01-05 21:25:51 -05004695 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004696 page = extent_buffer_page(eb, i);
4697 WARN_ON(!PageUptodate(page));
4698
4699 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004700 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004701 memset(kaddr + offset, c, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004702
4703 len -= cur;
4704 offset = 0;
4705 i++;
4706 }
4707}
Chris Masond1310b22008-01-24 16:13:08 -05004708
4709void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4710 unsigned long dst_offset, unsigned long src_offset,
4711 unsigned long len)
4712{
4713 u64 dst_len = dst->len;
4714 size_t cur;
4715 size_t offset;
4716 struct page *page;
4717 char *kaddr;
4718 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4719 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4720
4721 WARN_ON(src->len != dst_len);
4722
4723 offset = (start_offset + dst_offset) &
4724 ((unsigned long)PAGE_CACHE_SIZE - 1);
4725
Chris Masond3977122009-01-05 21:25:51 -05004726 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004727 page = extent_buffer_page(dst, i);
4728 WARN_ON(!PageUptodate(page));
4729
4730 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4731
Chris Masona6591712011-07-19 12:04:14 -04004732 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004733 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004734
4735 src_offset += cur;
4736 len -= cur;
4737 offset = 0;
4738 i++;
4739 }
4740}
Chris Masond1310b22008-01-24 16:13:08 -05004741
4742static void move_pages(struct page *dst_page, struct page *src_page,
4743 unsigned long dst_off, unsigned long src_off,
4744 unsigned long len)
4745{
Chris Masona6591712011-07-19 12:04:14 -04004746 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004747 if (dst_page == src_page) {
4748 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4749 } else {
Chris Masona6591712011-07-19 12:04:14 -04004750 char *src_kaddr = page_address(src_page);
Chris Masond1310b22008-01-24 16:13:08 -05004751 char *p = dst_kaddr + dst_off + len;
4752 char *s = src_kaddr + src_off + len;
4753
4754 while (len--)
4755 *--p = *--s;
Chris Masond1310b22008-01-24 16:13:08 -05004756 }
Chris Masond1310b22008-01-24 16:13:08 -05004757}
4758
Sergei Trofimovich33872062011-04-11 21:52:52 +00004759static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4760{
4761 unsigned long distance = (src > dst) ? src - dst : dst - src;
4762 return distance < len;
4763}
4764
Chris Masond1310b22008-01-24 16:13:08 -05004765static void copy_pages(struct page *dst_page, struct page *src_page,
4766 unsigned long dst_off, unsigned long src_off,
4767 unsigned long len)
4768{
Chris Masona6591712011-07-19 12:04:14 -04004769 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004770 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004771 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004772
Sergei Trofimovich33872062011-04-11 21:52:52 +00004773 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04004774 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00004775 } else {
Chris Masond1310b22008-01-24 16:13:08 -05004776 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004777 if (areas_overlap(src_off, dst_off, len))
4778 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00004779 }
Chris Masond1310b22008-01-24 16:13:08 -05004780
Chris Mason727011e2010-08-06 13:21:20 -04004781 if (must_memmove)
4782 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
4783 else
4784 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05004785}
4786
4787void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4788 unsigned long src_offset, unsigned long len)
4789{
4790 size_t cur;
4791 size_t dst_off_in_page;
4792 size_t src_off_in_page;
4793 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4794 unsigned long dst_i;
4795 unsigned long src_i;
4796
4797 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004798 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4799 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004800 BUG_ON(1);
4801 }
4802 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004803 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4804 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004805 BUG_ON(1);
4806 }
4807
Chris Masond3977122009-01-05 21:25:51 -05004808 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004809 dst_off_in_page = (start_offset + dst_offset) &
4810 ((unsigned long)PAGE_CACHE_SIZE - 1);
4811 src_off_in_page = (start_offset + src_offset) &
4812 ((unsigned long)PAGE_CACHE_SIZE - 1);
4813
4814 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4815 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4816
4817 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4818 src_off_in_page));
4819 cur = min_t(unsigned long, cur,
4820 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4821
4822 copy_pages(extent_buffer_page(dst, dst_i),
4823 extent_buffer_page(dst, src_i),
4824 dst_off_in_page, src_off_in_page, cur);
4825
4826 src_offset += cur;
4827 dst_offset += cur;
4828 len -= cur;
4829 }
4830}
Chris Masond1310b22008-01-24 16:13:08 -05004831
4832void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4833 unsigned long src_offset, unsigned long len)
4834{
4835 size_t cur;
4836 size_t dst_off_in_page;
4837 size_t src_off_in_page;
4838 unsigned long dst_end = dst_offset + len - 1;
4839 unsigned long src_end = src_offset + len - 1;
4840 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4841 unsigned long dst_i;
4842 unsigned long src_i;
4843
4844 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004845 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4846 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004847 BUG_ON(1);
4848 }
4849 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004850 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4851 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004852 BUG_ON(1);
4853 }
Chris Mason727011e2010-08-06 13:21:20 -04004854 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05004855 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4856 return;
4857 }
Chris Masond3977122009-01-05 21:25:51 -05004858 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004859 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
4860 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
4861
4862 dst_off_in_page = (start_offset + dst_end) &
4863 ((unsigned long)PAGE_CACHE_SIZE - 1);
4864 src_off_in_page = (start_offset + src_end) &
4865 ((unsigned long)PAGE_CACHE_SIZE - 1);
4866
4867 cur = min_t(unsigned long, len, src_off_in_page + 1);
4868 cur = min(cur, dst_off_in_page + 1);
4869 move_pages(extent_buffer_page(dst, dst_i),
4870 extent_buffer_page(dst, src_i),
4871 dst_off_in_page - cur + 1,
4872 src_off_in_page - cur + 1, cur);
4873
4874 dst_end -= cur;
4875 src_end -= cur;
4876 len -= cur;
4877 }
4878}
Chris Mason6af118c2008-07-22 11:18:07 -04004879
Josef Bacik3083ee22012-03-09 16:01:49 -05004880int try_release_extent_buffer(struct page *page, gfp_t mask)
Miao Xie19fe0a82010-10-26 20:57:29 -04004881{
Chris Mason6af118c2008-07-22 11:18:07 -04004882 struct extent_buffer *eb;
Miao Xie897ca6e2010-10-26 20:57:29 -04004883
Miao Xie19fe0a82010-10-26 20:57:29 -04004884 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05004885 * We need to make sure noboody is attaching this page to an eb right
4886 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04004887 */
Josef Bacik3083ee22012-03-09 16:01:49 -05004888 spin_lock(&page->mapping->private_lock);
4889 if (!PagePrivate(page)) {
4890 spin_unlock(&page->mapping->private_lock);
4891 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004892 }
4893
Josef Bacik3083ee22012-03-09 16:01:49 -05004894 eb = (struct extent_buffer *)page->private;
4895 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04004896
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004897 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05004898 * This is a little awful but should be ok, we need to make sure that
4899 * the eb doesn't disappear out from under us while we're looking at
4900 * this page.
4901 */
4902 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004903 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05004904 spin_unlock(&eb->refs_lock);
4905 spin_unlock(&page->mapping->private_lock);
4906 return 0;
4907 }
4908 spin_unlock(&page->mapping->private_lock);
4909
4910 if ((mask & GFP_NOFS) == GFP_NOFS)
4911 mask = GFP_NOFS;
4912
4913 /*
4914 * If tree ref isn't set then we know the ref on this eb is a real ref,
4915 * so just return, this page will likely be freed soon anyway.
4916 */
4917 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
4918 spin_unlock(&eb->refs_lock);
4919 return 0;
4920 }
4921 release_extent_buffer(eb, mask);
4922
4923 return 1;
Chris Mason6af118c2008-07-22 11:18:07 -04004924}