blob: 24b58c7f01ef2635c388575b2480504f46f83d9b [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 Masona25ebe22014-05-21 05:49:54 -07001554 cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001555 if (!loops) {
1556 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1557 max_bytes = PAGE_CACHE_SIZE - offset;
1558 loops = 1;
1559 goto again;
1560 } else {
1561 found = 0;
1562 goto out_failed;
1563 }
1564 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001565 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
Chris Masonc8b97812008-10-29 14:49:59 -04001566
1567 /* step three, lock the state bits for the whole range */
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001568 lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001569
1570 /* then test to make sure it is all still delalloc */
1571 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001572 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001573 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001574 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1575 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001576 __unlock_for_delalloc(inode, locked_page,
1577 delalloc_start, delalloc_end);
1578 cond_resched();
1579 goto again;
1580 }
Chris Mason9655d292009-09-02 15:22:30 -04001581 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001582 *start = delalloc_start;
1583 *end = delalloc_end;
1584out_failed:
1585 return found;
1586}
1587
1588int extent_clear_unlock_delalloc(struct inode *inode,
1589 struct extent_io_tree *tree,
1590 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001591 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001592{
1593 int ret;
1594 struct page *pages[16];
1595 unsigned long index = start >> PAGE_CACHE_SHIFT;
1596 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1597 unsigned long nr_pages = end_index - index + 1;
1598 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001599 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001600
Chris Masona791e352009-10-08 11:27:10 -04001601 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001602 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001603 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001604 clear_bits |= EXTENT_DIRTY;
1605
Chris Masona791e352009-10-08 11:27:10 -04001606 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001607 clear_bits |= EXTENT_DELALLOC;
1608
Chris Mason2c64c532009-09-02 15:04:12 -04001609 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001610 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1611 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1612 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001613 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001614
Chris Masond3977122009-01-05 21:25:51 -05001615 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001616 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001617 min_t(unsigned long,
1618 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001619 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001620
Chris Masona791e352009-10-08 11:27:10 -04001621 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001622 SetPagePrivate2(pages[i]);
1623
Chris Masonc8b97812008-10-29 14:49:59 -04001624 if (pages[i] == locked_page) {
1625 page_cache_release(pages[i]);
1626 continue;
1627 }
Chris Masona791e352009-10-08 11:27:10 -04001628 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001629 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001630 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001631 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001632 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001633 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001634 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001635 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001636 page_cache_release(pages[i]);
1637 }
1638 nr_pages -= ret;
1639 index += ret;
1640 cond_resched();
1641 }
1642 return 0;
1643}
Chris Masonc8b97812008-10-29 14:49:59 -04001644
Chris Masond352ac62008-09-29 15:18:18 -04001645/*
1646 * count the number of bytes in the tree that have a given bit(s)
1647 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1648 * cached. The total number found is returned.
1649 */
Chris Masond1310b22008-01-24 16:13:08 -05001650u64 count_range_bits(struct extent_io_tree *tree,
1651 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001652 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001653{
1654 struct rb_node *node;
1655 struct extent_state *state;
1656 u64 cur_start = *start;
1657 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001658 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001659 int found = 0;
1660
1661 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001662 WARN_ON(1);
1663 return 0;
1664 }
1665
Chris Masoncad321a2008-12-17 14:51:42 -05001666 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001667 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1668 total_bytes = tree->dirty_bytes;
1669 goto out;
1670 }
1671 /*
1672 * this search will find all the extents that end after
1673 * our range starts.
1674 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001675 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001676 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001677 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001678
Chris Masond3977122009-01-05 21:25:51 -05001679 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001680 state = rb_entry(node, struct extent_state, rb_node);
1681 if (state->start > search_end)
1682 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001683 if (contig && found && state->start > last + 1)
1684 break;
1685 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001686 total_bytes += min(search_end, state->end) + 1 -
1687 max(cur_start, state->start);
1688 if (total_bytes >= max_bytes)
1689 break;
1690 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001691 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001692 found = 1;
1693 }
Chris Masonec29ed52011-02-23 16:23:20 -05001694 last = state->end;
1695 } else if (contig && found) {
1696 break;
Chris Masond1310b22008-01-24 16:13:08 -05001697 }
1698 node = rb_next(node);
1699 if (!node)
1700 break;
1701 }
1702out:
Chris Masoncad321a2008-12-17 14:51:42 -05001703 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001704 return total_bytes;
1705}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001706
Chris Masond352ac62008-09-29 15:18:18 -04001707/*
1708 * set the private field for a given byte offset in the tree. If there isn't
1709 * an extent_state there already, this does nothing.
1710 */
Chris Masond1310b22008-01-24 16:13:08 -05001711int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1712{
1713 struct rb_node *node;
1714 struct extent_state *state;
1715 int ret = 0;
1716
Chris Masoncad321a2008-12-17 14:51:42 -05001717 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001718 /*
1719 * this search will find all the extents that end after
1720 * our range starts.
1721 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001722 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001723 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001724 ret = -ENOENT;
1725 goto out;
1726 }
1727 state = rb_entry(node, struct extent_state, rb_node);
1728 if (state->start != start) {
1729 ret = -ENOENT;
1730 goto out;
1731 }
1732 state->private = private;
1733out:
Chris Masoncad321a2008-12-17 14:51:42 -05001734 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001735 return ret;
1736}
1737
1738int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1739{
1740 struct rb_node *node;
1741 struct extent_state *state;
1742 int ret = 0;
1743
Chris Masoncad321a2008-12-17 14:51:42 -05001744 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001745 /*
1746 * this search will find all the extents that end after
1747 * our range starts.
1748 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001749 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001750 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001751 ret = -ENOENT;
1752 goto out;
1753 }
1754 state = rb_entry(node, struct extent_state, rb_node);
1755 if (state->start != start) {
1756 ret = -ENOENT;
1757 goto out;
1758 }
1759 *private = state->private;
1760out:
Chris Masoncad321a2008-12-17 14:51:42 -05001761 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001762 return ret;
1763}
1764
1765/*
1766 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001767 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001768 * has the bits set. Otherwise, 1 is returned if any bit in the
1769 * range is found set.
1770 */
1771int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001772 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001773{
1774 struct extent_state *state = NULL;
1775 struct rb_node *node;
1776 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001777
Chris Masoncad321a2008-12-17 14:51:42 -05001778 spin_lock(&tree->lock);
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001779 if (cached && cached->tree && cached->start <= start &&
1780 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001781 node = &cached->rb_node;
1782 else
1783 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001784 while (node && start <= end) {
1785 state = rb_entry(node, struct extent_state, rb_node);
1786
1787 if (filled && state->start > start) {
1788 bitset = 0;
1789 break;
1790 }
1791
1792 if (state->start > end)
1793 break;
1794
1795 if (state->state & bits) {
1796 bitset = 1;
1797 if (!filled)
1798 break;
1799 } else if (filled) {
1800 bitset = 0;
1801 break;
1802 }
Chris Mason46562ce2009-09-23 20:23:16 -04001803
1804 if (state->end == (u64)-1)
1805 break;
1806
Chris Masond1310b22008-01-24 16:13:08 -05001807 start = state->end + 1;
1808 if (start > end)
1809 break;
1810 node = rb_next(node);
1811 if (!node) {
1812 if (filled)
1813 bitset = 0;
1814 break;
1815 }
1816 }
Chris Masoncad321a2008-12-17 14:51:42 -05001817 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001818 return bitset;
1819}
Chris Masond1310b22008-01-24 16:13:08 -05001820
1821/*
1822 * helper function to set a given page up to date if all the
1823 * extents in the tree for that page are up to date
1824 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001825static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001826{
1827 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1828 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001829 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001830 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001831}
1832
1833/*
1834 * helper function to unlock a page if all the extents in the tree
1835 * for that page are unlocked
1836 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001837static void check_page_locked(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001838{
1839 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1840 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001841 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001842 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05001843}
1844
1845/*
1846 * helper function to end page writeback if all the extents
1847 * in the tree for that page are done with writeback
1848 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001849static void check_page_writeback(struct extent_io_tree *tree,
1850 struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001851{
Chris Mason1edbb732009-09-02 13:24:36 -04001852 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001853}
1854
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001855/*
1856 * When IO fails, either with EIO or csum verification fails, we
1857 * try other mirrors that might have a good copy of the data. This
1858 * io_failure_record is used to record state as we go through all the
1859 * mirrors. If another mirror has good data, the page is set up to date
1860 * and things continue. If a good mirror can't be found, the original
1861 * bio end_io callback is called to indicate things have failed.
1862 */
1863struct io_failure_record {
1864 struct page *page;
1865 u64 start;
1866 u64 len;
1867 u64 logical;
1868 unsigned long bio_flags;
1869 int this_mirror;
1870 int failed_mirror;
1871 int in_validation;
1872};
1873
1874static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
1875 int did_repair)
1876{
1877 int ret;
1878 int err = 0;
1879 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1880
1881 set_state_private(failure_tree, rec->start, 0);
1882 ret = clear_extent_bits(failure_tree, rec->start,
1883 rec->start + rec->len - 1,
1884 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1885 if (ret)
1886 err = ret;
1887
1888 if (did_repair) {
1889 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1890 rec->start + rec->len - 1,
1891 EXTENT_DAMAGED, GFP_NOFS);
1892 if (ret && !err)
1893 err = ret;
1894 }
1895
1896 kfree(rec);
1897 return err;
1898}
1899
1900static void repair_io_failure_callback(struct bio *bio, int err)
1901{
1902 complete(bio->bi_private);
1903}
1904
1905/*
1906 * this bypasses the standard btrfs submit functions deliberately, as
1907 * the standard behavior is to write all copies in a raid setup. here we only
1908 * want to write the one bad copy. so we do the mapping for ourselves and issue
1909 * submit_bio directly.
1910 * to avoid any synchonization issues, wait for the data after writing, which
1911 * actually prevents the read that triggered the error from finishing.
1912 * currently, there can be no more than two copies of every data bit. thus,
1913 * exactly one rewrite is required.
1914 */
1915int repair_io_failure(struct btrfs_mapping_tree *map_tree, u64 start,
1916 u64 length, u64 logical, struct page *page,
1917 int mirror_num)
1918{
1919 struct bio *bio;
1920 struct btrfs_device *dev;
1921 DECLARE_COMPLETION_ONSTACK(compl);
1922 u64 map_length = 0;
1923 u64 sector;
1924 struct btrfs_bio *bbio = NULL;
1925 int ret;
1926
1927 BUG_ON(!mirror_num);
1928
1929 bio = bio_alloc(GFP_NOFS, 1);
1930 if (!bio)
1931 return -EIO;
1932 bio->bi_private = &compl;
1933 bio->bi_end_io = repair_io_failure_callback;
1934 bio->bi_size = 0;
1935 map_length = length;
1936
1937 ret = btrfs_map_block(map_tree, WRITE, logical,
1938 &map_length, &bbio, mirror_num);
1939 if (ret) {
1940 bio_put(bio);
1941 return -EIO;
1942 }
1943 BUG_ON(mirror_num != bbio->mirror_num);
1944 sector = bbio->stripes[mirror_num-1].physical >> 9;
1945 bio->bi_sector = sector;
1946 dev = bbio->stripes[mirror_num-1].dev;
1947 kfree(bbio);
1948 if (!dev || !dev->bdev || !dev->writeable) {
1949 bio_put(bio);
1950 return -EIO;
1951 }
1952 bio->bi_bdev = dev->bdev;
1953 bio_add_page(bio, page, length, start-page_offset(page));
Stefan Behrens21adbd52011-11-09 13:44:05 +01001954 btrfsic_submit_bio(WRITE_SYNC, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001955 wait_for_completion(&compl);
1956
1957 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
1958 /* try to remap that extent elsewhere? */
1959 bio_put(bio);
1960 return -EIO;
1961 }
1962
1963 printk(KERN_INFO "btrfs read error corrected: ino %lu off %llu (dev %s "
1964 "sector %llu)\n", page->mapping->host->i_ino, start,
1965 dev->name, sector);
1966
1967 bio_put(bio);
1968 return 0;
1969}
1970
Josef Bacikea466792012-03-26 21:57:36 -04001971int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
1972 int mirror_num)
1973{
1974 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1975 u64 start = eb->start;
1976 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond95603b2012-04-12 15:55:15 -04001977 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04001978
1979 for (i = 0; i < num_pages; i++) {
1980 struct page *p = extent_buffer_page(eb, i);
1981 ret = repair_io_failure(map_tree, start, PAGE_CACHE_SIZE,
1982 start, p, mirror_num);
1983 if (ret)
1984 break;
1985 start += PAGE_CACHE_SIZE;
1986 }
1987
1988 return ret;
1989}
1990
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001991/*
1992 * each time an IO finishes, we do a fast check in the IO failure tree
1993 * to see if we need to process or clean up an io_failure_record
1994 */
1995static int clean_io_failure(u64 start, struct page *page)
1996{
1997 u64 private;
1998 u64 private_failure;
1999 struct io_failure_record *failrec;
2000 struct btrfs_mapping_tree *map_tree;
2001 struct extent_state *state;
2002 int num_copies;
2003 int did_repair = 0;
2004 int ret;
2005 struct inode *inode = page->mapping->host;
2006
2007 private = 0;
2008 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
2009 (u64)-1, 1, EXTENT_DIRTY, 0);
2010 if (!ret)
2011 return 0;
2012
2013 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
2014 &private_failure);
2015 if (ret)
2016 return 0;
2017
2018 failrec = (struct io_failure_record *)(unsigned long) private_failure;
2019 BUG_ON(!failrec->this_mirror);
2020
2021 if (failrec->in_validation) {
2022 /* there was no real error, just free the record */
2023 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
2024 failrec->start);
2025 did_repair = 1;
2026 goto out;
2027 }
2028
2029 spin_lock(&BTRFS_I(inode)->io_tree.lock);
2030 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
2031 failrec->start,
2032 EXTENT_LOCKED);
2033 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
2034
2035 if (state && state->start == failrec->start) {
2036 map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree;
2037 num_copies = btrfs_num_copies(map_tree, failrec->logical,
2038 failrec->len);
2039 if (num_copies > 1) {
2040 ret = repair_io_failure(map_tree, start, failrec->len,
2041 failrec->logical, page,
2042 failrec->failed_mirror);
2043 did_repair = !ret;
2044 }
2045 }
2046
2047out:
2048 if (!ret)
2049 ret = free_io_failure(inode, failrec, did_repair);
2050
2051 return ret;
2052}
2053
2054/*
2055 * this is a generic handler for readpage errors (default
2056 * readpage_io_failed_hook). if other copies exist, read those and write back
2057 * good data to the failed position. does not investigate in remapping the
2058 * failed extent elsewhere, hoping the device will be smart enough to do this as
2059 * needed
2060 */
2061
2062static int bio_readpage_error(struct bio *failed_bio, struct page *page,
2063 u64 start, u64 end, int failed_mirror,
2064 struct extent_state *state)
2065{
2066 struct io_failure_record *failrec = NULL;
2067 u64 private;
2068 struct extent_map *em;
2069 struct inode *inode = page->mapping->host;
2070 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2071 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2072 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2073 struct bio *bio;
2074 int num_copies;
2075 int ret;
2076 int read_mode;
2077 u64 logical;
2078
2079 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2080
2081 ret = get_state_private(failure_tree, start, &private);
2082 if (ret) {
2083 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2084 if (!failrec)
2085 return -ENOMEM;
2086 failrec->start = start;
2087 failrec->len = end - start + 1;
2088 failrec->this_mirror = 0;
2089 failrec->bio_flags = 0;
2090 failrec->in_validation = 0;
2091
2092 read_lock(&em_tree->lock);
2093 em = lookup_extent_mapping(em_tree, start, failrec->len);
2094 if (!em) {
2095 read_unlock(&em_tree->lock);
2096 kfree(failrec);
2097 return -EIO;
2098 }
2099
2100 if (em->start > start || em->start + em->len < start) {
2101 free_extent_map(em);
2102 em = NULL;
2103 }
2104 read_unlock(&em_tree->lock);
2105
2106 if (!em || IS_ERR(em)) {
2107 kfree(failrec);
2108 return -EIO;
2109 }
2110 logical = start - em->start;
2111 logical = em->block_start + logical;
2112 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2113 logical = em->block_start;
2114 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2115 extent_set_compress_type(&failrec->bio_flags,
2116 em->compress_type);
2117 }
2118 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2119 "len=%llu\n", logical, start, failrec->len);
2120 failrec->logical = logical;
2121 free_extent_map(em);
2122
2123 /* set the bits in the private failure tree */
2124 ret = set_extent_bits(failure_tree, start, end,
2125 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2126 if (ret >= 0)
2127 ret = set_state_private(failure_tree, start,
2128 (u64)(unsigned long)failrec);
2129 /* set the bits in the inode's tree */
2130 if (ret >= 0)
2131 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2132 GFP_NOFS);
2133 if (ret < 0) {
2134 kfree(failrec);
2135 return ret;
2136 }
2137 } else {
2138 failrec = (struct io_failure_record *)(unsigned long)private;
2139 pr_debug("bio_readpage_error: (found) logical=%llu, "
2140 "start=%llu, len=%llu, validation=%d\n",
2141 failrec->logical, failrec->start, failrec->len,
2142 failrec->in_validation);
2143 /*
2144 * when data can be on disk more than twice, add to failrec here
2145 * (e.g. with a list for failed_mirror) to make
2146 * clean_io_failure() clean all those errors at once.
2147 */
2148 }
2149 num_copies = btrfs_num_copies(
2150 &BTRFS_I(inode)->root->fs_info->mapping_tree,
2151 failrec->logical, failrec->len);
2152 if (num_copies == 1) {
2153 /*
2154 * we only have a single copy of the data, so don't bother with
2155 * all the retry and error correction code that follows. no
2156 * matter what the error is, it is very likely to persist.
2157 */
2158 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2159 "state=%p, num_copies=%d, next_mirror %d, "
2160 "failed_mirror %d\n", state, num_copies,
2161 failrec->this_mirror, failed_mirror);
2162 free_io_failure(inode, failrec, 0);
2163 return -EIO;
2164 }
2165
2166 if (!state) {
2167 spin_lock(&tree->lock);
2168 state = find_first_extent_bit_state(tree, failrec->start,
2169 EXTENT_LOCKED);
2170 if (state && state->start != failrec->start)
2171 state = NULL;
2172 spin_unlock(&tree->lock);
2173 }
2174
2175 /*
2176 * there are two premises:
2177 * a) deliver good data to the caller
2178 * b) correct the bad sectors on disk
2179 */
2180 if (failed_bio->bi_vcnt > 1) {
2181 /*
2182 * to fulfill b), we need to know the exact failing sectors, as
2183 * we don't want to rewrite any more than the failed ones. thus,
2184 * we need separate read requests for the failed bio
2185 *
2186 * if the following BUG_ON triggers, our validation request got
2187 * merged. we need separate requests for our algorithm to work.
2188 */
2189 BUG_ON(failrec->in_validation);
2190 failrec->in_validation = 1;
2191 failrec->this_mirror = failed_mirror;
2192 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2193 } else {
2194 /*
2195 * we're ready to fulfill a) and b) alongside. get a good copy
2196 * of the failed sector and if we succeed, we have setup
2197 * everything for repair_io_failure to do the rest for us.
2198 */
2199 if (failrec->in_validation) {
2200 BUG_ON(failrec->this_mirror != failed_mirror);
2201 failrec->in_validation = 0;
2202 failrec->this_mirror = 0;
2203 }
2204 failrec->failed_mirror = failed_mirror;
2205 failrec->this_mirror++;
2206 if (failrec->this_mirror == failed_mirror)
2207 failrec->this_mirror++;
2208 read_mode = READ_SYNC;
2209 }
2210
2211 if (!state || failrec->this_mirror > num_copies) {
2212 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2213 "next_mirror %d, failed_mirror %d\n", state,
2214 num_copies, failrec->this_mirror, failed_mirror);
2215 free_io_failure(inode, failrec, 0);
2216 return -EIO;
2217 }
2218
2219 bio = bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04002220 if (!bio) {
2221 free_io_failure(inode, failrec, 0);
2222 return -EIO;
2223 }
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002224 bio->bi_private = state;
2225 bio->bi_end_io = failed_bio->bi_end_io;
2226 bio->bi_sector = failrec->logical >> 9;
2227 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2228 bio->bi_size = 0;
2229
2230 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2231
2232 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2233 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2234 failrec->this_mirror, num_copies, failrec->in_validation);
2235
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002236 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2237 failrec->this_mirror,
2238 failrec->bio_flags, 0);
2239 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002240}
2241
Chris Masond1310b22008-01-24 16:13:08 -05002242/* lots and lots of room for performance fixes in the end_bio funcs */
2243
Jeff Mahoney87826df2012-02-15 16:23:57 +01002244int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2245{
2246 int uptodate = (err == 0);
2247 struct extent_io_tree *tree;
Eric Sandeene18bac22014-06-12 00:39:58 -05002248 int ret = 0;
Jeff Mahoney87826df2012-02-15 16:23:57 +01002249
2250 tree = &BTRFS_I(page->mapping->host)->io_tree;
2251
2252 if (tree->ops && tree->ops->writepage_end_io_hook) {
2253 ret = tree->ops->writepage_end_io_hook(page, start,
2254 end, NULL, uptodate);
2255 if (ret)
2256 uptodate = 0;
2257 }
2258
2259 if (!uptodate && tree->ops &&
2260 tree->ops->writepage_io_failed_hook) {
2261 ret = tree->ops->writepage_io_failed_hook(NULL, page,
2262 start, end, NULL);
2263 /* Writeback already completed */
2264 if (ret == 0)
2265 return 1;
2266 }
2267
2268 if (!uptodate) {
2269 clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
2270 ClearPageUptodate(page);
2271 SetPageError(page);
2272 }
2273 return 0;
2274}
2275
Chris Masond1310b22008-01-24 16:13:08 -05002276/*
2277 * after a writepage IO is done, we need to:
2278 * clear the uptodate bits on error
2279 * clear the writeback bits in the extent tree for this IO
2280 * end_page_writeback if the page has no more pending IO
2281 *
2282 * Scheduling is not allowed, so the extent state tree is expected
2283 * to have one and only one object corresponding to this IO.
2284 */
Chris Masond1310b22008-01-24 16:13:08 -05002285static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002286{
Chris Masond1310b22008-01-24 16:13:08 -05002287 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04002288 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002289 u64 start;
2290 u64 end;
2291 int whole_page;
2292
Chris Masond1310b22008-01-24 16:13:08 -05002293 do {
2294 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04002295 tree = &BTRFS_I(page->mapping->host)->io_tree;
2296
Chris Masond1310b22008-01-24 16:13:08 -05002297 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2298 bvec->bv_offset;
2299 end = start + bvec->bv_len - 1;
2300
2301 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2302 whole_page = 1;
2303 else
2304 whole_page = 0;
2305
2306 if (--bvec >= bio->bi_io_vec)
2307 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04002308
Jeff Mahoney87826df2012-02-15 16:23:57 +01002309 if (end_extent_writepage(page, err, start, end))
2310 continue;
Chris Mason70dec802008-01-29 09:59:12 -05002311
Chris Masond1310b22008-01-24 16:13:08 -05002312 if (whole_page)
2313 end_page_writeback(page);
2314 else
2315 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002316 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04002317
Chris Masond1310b22008-01-24 16:13:08 -05002318 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002319}
2320
2321/*
2322 * after a readpage IO is done, we need to:
2323 * clear the uptodate bits on error
2324 * set the uptodate bits if things worked
2325 * set the page up to date if all extents in the tree are uptodate
2326 * clear the lock bit in the extent tree
2327 * unlock the page if there are no other extents locked for it
2328 *
2329 * Scheduling is not allowed, so the extent state tree is expected
2330 * to have one and only one object corresponding to this IO.
2331 */
Chris Masond1310b22008-01-24 16:13:08 -05002332static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002333{
2334 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00002335 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2336 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04002337 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002338 u64 start;
2339 u64 end;
2340 int whole_page;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002341 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002342 int ret;
2343
Chris Masond20f7042008-12-08 16:58:54 -05002344 if (err)
2345 uptodate = 0;
2346
Chris Masond1310b22008-01-24 16:13:08 -05002347 do {
2348 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00002349 struct extent_state *cached = NULL;
2350 struct extent_state *state;
2351
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002352 pr_debug("end_bio_extent_readpage: bi_vcnt=%d, idx=%d, err=%d, "
2353 "mirror=%ld\n", bio->bi_vcnt, bio->bi_idx, err,
2354 (long int)bio->bi_bdev);
David Woodhouse902b22f2008-08-20 08:51:49 -04002355 tree = &BTRFS_I(page->mapping->host)->io_tree;
2356
Chris Masond1310b22008-01-24 16:13:08 -05002357 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2358 bvec->bv_offset;
2359 end = start + bvec->bv_len - 1;
2360
2361 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2362 whole_page = 1;
2363 else
2364 whole_page = 0;
2365
Chris Mason4125bf72010-02-03 18:18:45 +00002366 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05002367 prefetchw(&bvec->bv_page->flags);
2368
Arne Jansen507903b2011-04-06 10:02:20 +00002369 spin_lock(&tree->lock);
Chris Mason0d399202011-04-16 06:55:39 -04002370 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
Chris Mason109b36a2011-04-12 13:57:39 -04002371 if (state && state->start == start) {
Arne Jansen507903b2011-04-06 10:02:20 +00002372 /*
2373 * take a reference on the state, unlock will drop
2374 * the ref
2375 */
2376 cache_state(state, &cached);
2377 }
2378 spin_unlock(&tree->lock);
2379
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002380 mirror = (int)(unsigned long)bio->bi_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002381 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05002382 ret = tree->ops->readpage_end_io_hook(page, start, end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002383 state, mirror);
Chris Masond1310b22008-01-24 16:13:08 -05002384 if (ret)
2385 uptodate = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002386 else
2387 clean_io_failure(start, page);
Chris Masond1310b22008-01-24 16:13:08 -05002388 }
Josef Bacikea466792012-03-26 21:57:36 -04002389
Josef Bacikea466792012-03-26 21:57:36 -04002390 if (!uptodate && tree->ops && tree->ops->readpage_io_failed_hook) {
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002391 ret = tree->ops->readpage_io_failed_hook(page, mirror);
Josef Bacikea466792012-03-26 21:57:36 -04002392 if (!ret && !err &&
2393 test_bit(BIO_UPTODATE, &bio->bi_flags))
2394 uptodate = 1;
2395 } else if (!uptodate) {
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002396 /*
2397 * The generic bio_readpage_error handles errors the
2398 * following way: If possible, new read requests are
2399 * created and submitted and will end up in
2400 * end_bio_extent_readpage as well (if we're lucky, not
2401 * in the !uptodate case). In that case it returns 0 and
2402 * we just go on with the next page in our bio. If it
2403 * can't handle the error it will return -EIO and we
2404 * remain responsible for that page.
2405 */
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002406 ret = bio_readpage_error(bio, page, start, end, mirror, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04002407 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04002408 uptodate =
2409 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05002410 if (err)
2411 uptodate = 0;
Arne Jansen507903b2011-04-06 10:02:20 +00002412 uncache_state(&cached);
Chris Mason7e383262008-04-09 16:28:12 -04002413 continue;
2414 }
2415 }
Chris Mason70dec802008-01-29 09:59:12 -05002416
Josef Bacik0b32f4b2012-03-13 09:38:00 -04002417 if (uptodate && tree->track_uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00002418 set_extent_uptodate(tree, start, end, &cached,
David Woodhouse902b22f2008-08-20 08:51:49 -04002419 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05002420 }
Arne Jansen507903b2011-04-06 10:02:20 +00002421 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05002422
Chris Mason70dec802008-01-29 09:59:12 -05002423 if (whole_page) {
2424 if (uptodate) {
2425 SetPageUptodate(page);
2426 } else {
2427 ClearPageUptodate(page);
2428 SetPageError(page);
2429 }
Chris Masond1310b22008-01-24 16:13:08 -05002430 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05002431 } else {
2432 if (uptodate) {
2433 check_page_uptodate(tree, page);
2434 } else {
2435 ClearPageUptodate(page);
2436 SetPageError(page);
2437 }
Chris Masond1310b22008-01-24 16:13:08 -05002438 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05002439 }
Chris Mason4125bf72010-02-03 18:18:45 +00002440 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05002441
2442 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002443}
2444
Miao Xie88f794e2010-11-22 03:02:55 +00002445struct bio *
2446btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2447 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002448{
2449 struct bio *bio;
2450
2451 bio = bio_alloc(gfp_flags, nr_vecs);
2452
2453 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2454 while (!bio && (nr_vecs /= 2))
2455 bio = bio_alloc(gfp_flags, nr_vecs);
2456 }
2457
2458 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04002459 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002460 bio->bi_bdev = bdev;
2461 bio->bi_sector = first_sector;
2462 }
2463 return bio;
2464}
2465
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002466/*
2467 * Since writes are async, they will only return -ENOMEM.
2468 * Reads can return the full range of I/O error conditions.
2469 */
Jeff Mahoney355808c2011-10-03 23:23:14 -04002470static int __must_check submit_one_bio(int rw, struct bio *bio,
2471 int mirror_num, unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002472{
Chris Masond1310b22008-01-24 16:13:08 -05002473 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002474 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2475 struct page *page = bvec->bv_page;
2476 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002477 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002478
2479 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002480
David Woodhouse902b22f2008-08-20 08:51:49 -04002481 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002482
2483 bio_get(bio);
2484
Chris Mason065631f2008-02-20 12:07:25 -05002485 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00002486 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002487 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002488 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01002489 btrfsic_submit_bio(rw, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002490
Chris Masond1310b22008-01-24 16:13:08 -05002491 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2492 ret = -EOPNOTSUPP;
2493 bio_put(bio);
2494 return ret;
2495}
2496
Jeff Mahoney3444a972011-10-03 23:23:13 -04002497static int merge_bio(struct extent_io_tree *tree, struct page *page,
2498 unsigned long offset, size_t size, struct bio *bio,
2499 unsigned long bio_flags)
2500{
2501 int ret = 0;
2502 if (tree->ops && tree->ops->merge_bio_hook)
2503 ret = tree->ops->merge_bio_hook(page, offset, size, bio,
2504 bio_flags);
2505 BUG_ON(ret < 0);
2506 return ret;
2507
2508}
2509
Chris Masond1310b22008-01-24 16:13:08 -05002510static int submit_extent_page(int rw, struct extent_io_tree *tree,
2511 struct page *page, sector_t sector,
2512 size_t size, unsigned long offset,
2513 struct block_device *bdev,
2514 struct bio **bio_ret,
2515 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04002516 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002517 int mirror_num,
2518 unsigned long prev_bio_flags,
2519 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002520{
2521 int ret = 0;
2522 struct bio *bio;
2523 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04002524 int contig = 0;
2525 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2526 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05002527 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002528
2529 if (bio_ret && *bio_ret) {
2530 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002531 if (old_compressed)
2532 contig = bio->bi_sector == sector;
2533 else
2534 contig = bio->bi_sector + (bio->bi_size >> 9) ==
2535 sector;
2536
2537 if (prev_bio_flags != bio_flags || !contig ||
Jeff Mahoney3444a972011-10-03 23:23:13 -04002538 merge_bio(tree, page, offset, page_size, bio, bio_flags) ||
Chris Masonc8b97812008-10-29 14:49:59 -04002539 bio_add_page(bio, page, page_size, offset) < page_size) {
2540 ret = submit_one_bio(rw, bio, mirror_num,
2541 prev_bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002542 if (ret < 0)
2543 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05002544 bio = NULL;
2545 } else {
2546 return 0;
2547 }
2548 }
Chris Masonc8b97812008-10-29 14:49:59 -04002549 if (this_compressed)
2550 nr = BIO_MAX_PAGES;
2551 else
2552 nr = bio_get_nr_vecs(bdev);
2553
Miao Xie88f794e2010-11-22 03:02:55 +00002554 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002555 if (!bio)
2556 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002557
Chris Masonc8b97812008-10-29 14:49:59 -04002558 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002559 bio->bi_end_io = end_io_func;
2560 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05002561
Chris Masond3977122009-01-05 21:25:51 -05002562 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002563 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002564 else
Chris Masonc8b97812008-10-29 14:49:59 -04002565 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002566
2567 return ret;
2568}
2569
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002570void attach_extent_buffer_page(struct extent_buffer *eb, struct page *page)
2571{
2572 if (!PagePrivate(page)) {
2573 SetPagePrivate(page);
2574 page_cache_get(page);
2575 set_page_private(page, (unsigned long)eb);
2576 } else {
2577 WARN_ON(page->private != (unsigned long)eb);
2578 }
2579}
2580
Chris Masond1310b22008-01-24 16:13:08 -05002581void set_page_extent_mapped(struct page *page)
2582{
2583 if (!PagePrivate(page)) {
2584 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002585 page_cache_get(page);
Chris Mason6af118c2008-07-22 11:18:07 -04002586 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002587 }
2588}
2589
Chris Masond1310b22008-01-24 16:13:08 -05002590/*
2591 * basic readpage implementation. Locked extent state structs are inserted
2592 * into the tree that are removed when the IO is done (by the end_io
2593 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002594 * XXX JDM: This needs looking at to ensure proper page locking
Chris Masond1310b22008-01-24 16:13:08 -05002595 */
2596static int __extent_read_full_page(struct extent_io_tree *tree,
2597 struct page *page,
2598 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002599 struct bio **bio, int mirror_num,
2600 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002601{
2602 struct inode *inode = page->mapping->host;
2603 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2604 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2605 u64 end;
2606 u64 cur = start;
2607 u64 extent_offset;
2608 u64 last_byte = i_size_read(inode);
2609 u64 block_start;
2610 u64 cur_end;
2611 sector_t sector;
2612 struct extent_map *em;
2613 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002614 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05002615 int ret;
2616 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002617 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002618 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002619 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002620 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002621 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002622
2623 set_page_extent_mapped(page);
2624
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002625 if (!PageUptodate(page)) {
2626 if (cleancache_get_page(page) == 0) {
2627 BUG_ON(blocksize != PAGE_SIZE);
2628 goto out;
2629 }
2630 }
2631
Chris Masond1310b22008-01-24 16:13:08 -05002632 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002633 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002634 lock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002635 ordered = btrfs_lookup_ordered_extent(inode, start);
2636 if (!ordered)
2637 break;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002638 unlock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002639 btrfs_start_ordered_extent(inode, ordered, 1);
2640 btrfs_put_ordered_extent(ordered);
2641 }
Chris Masond1310b22008-01-24 16:13:08 -05002642
Chris Masonc8b97812008-10-29 14:49:59 -04002643 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2644 char *userpage;
2645 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2646
2647 if (zero_offset) {
2648 iosize = PAGE_CACHE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002649 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002650 memset(userpage + zero_offset, 0, iosize);
2651 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002652 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002653 }
2654 }
Chris Masond1310b22008-01-24 16:13:08 -05002655 while (cur <= end) {
2656 if (cur >= last_byte) {
2657 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002658 struct extent_state *cached = NULL;
2659
David Sterba306e16c2011-04-19 14:29:38 +02002660 iosize = PAGE_CACHE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002661 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002662 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002663 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002664 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002665 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002666 &cached, GFP_NOFS);
2667 unlock_extent_cached(tree, cur, cur + iosize - 1,
2668 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002669 break;
2670 }
David Sterba306e16c2011-04-19 14:29:38 +02002671 em = get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002672 end - cur + 1, 0);
David Sterbac7040052011-04-19 18:00:01 +02002673 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002674 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002675 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002676 break;
2677 }
Chris Masond1310b22008-01-24 16:13:08 -05002678 extent_offset = cur - em->start;
2679 BUG_ON(extent_map_end(em) <= cur);
2680 BUG_ON(end < cur);
2681
Li Zefan261507a02010-12-17 14:21:50 +08002682 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002683 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002684 extent_set_compress_type(&this_bio_flag,
2685 em->compress_type);
2686 }
Chris Masonc8b97812008-10-29 14:49:59 -04002687
Chris Masond1310b22008-01-24 16:13:08 -05002688 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2689 cur_end = min(extent_map_end(em) - 1, end);
2690 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002691 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2692 disk_io_size = em->block_len;
2693 sector = em->block_start >> 9;
2694 } else {
2695 sector = (em->block_start + extent_offset) >> 9;
2696 disk_io_size = iosize;
2697 }
Chris Masond1310b22008-01-24 16:13:08 -05002698 bdev = em->bdev;
2699 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002700 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2701 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002702 free_extent_map(em);
2703 em = NULL;
2704
2705 /* we've found a hole, just zero and go on */
2706 if (block_start == EXTENT_MAP_HOLE) {
2707 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002708 struct extent_state *cached = NULL;
2709
Cong Wang7ac687d2011-11-25 23:14:28 +08002710 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002711 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002712 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002713 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002714
2715 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002716 &cached, GFP_NOFS);
2717 unlock_extent_cached(tree, cur, cur + iosize - 1,
2718 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002719 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002720 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002721 continue;
2722 }
2723 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002724 if (test_range_bit(tree, cur, cur_end,
2725 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002726 check_page_uptodate(tree, page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002727 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002728 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002729 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002730 continue;
2731 }
Chris Mason70dec802008-01-29 09:59:12 -05002732 /* we have an inline extent but it didn't get marked up
2733 * to date. Error out
2734 */
2735 if (block_start == EXTENT_MAP_INLINE) {
2736 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002737 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05002738 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002739 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05002740 continue;
2741 }
Chris Masond1310b22008-01-24 16:13:08 -05002742
2743 ret = 0;
2744 if (tree->ops && tree->ops->readpage_io_hook) {
2745 ret = tree->ops->readpage_io_hook(page, cur,
2746 cur + iosize - 1);
2747 }
2748 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002749 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2750 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002751 ret = submit_extent_page(READ, tree, page,
David Sterba306e16c2011-04-19 14:29:38 +02002752 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04002753 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002754 end_bio_extent_readpage, mirror_num,
2755 *bio_flags,
2756 this_bio_flag);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002757 BUG_ON(ret == -ENOMEM);
Chris Mason89642222008-07-24 09:41:53 -04002758 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002759 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002760 }
2761 if (ret)
2762 SetPageError(page);
2763 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002764 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002765 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002766out:
Chris Masond1310b22008-01-24 16:13:08 -05002767 if (!nr) {
2768 if (!PageError(page))
2769 SetPageUptodate(page);
2770 unlock_page(page);
2771 }
2772 return 0;
2773}
2774
2775int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002776 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05002777{
2778 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002779 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002780 int ret;
2781
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002782 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Chris Masonc8b97812008-10-29 14:49:59 -04002783 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002784 if (bio)
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002785 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002786 return ret;
2787}
Chris Masond1310b22008-01-24 16:13:08 -05002788
Chris Mason11c83492009-04-20 15:50:09 -04002789static noinline void update_nr_written(struct page *page,
2790 struct writeback_control *wbc,
2791 unsigned long nr_written)
2792{
2793 wbc->nr_to_write -= nr_written;
2794 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2795 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2796 page->mapping->writeback_index = page->index + nr_written;
2797}
2798
Chris Masond1310b22008-01-24 16:13:08 -05002799/*
2800 * the writepage semantics are similar to regular writepage. extent
2801 * records are inserted to lock ranges in the tree, and as dirty areas
2802 * are found, they are marked writeback. Then the lock bits are removed
2803 * and the end_io handler clears the writeback ranges
2804 */
2805static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2806 void *data)
2807{
2808 struct inode *inode = page->mapping->host;
2809 struct extent_page_data *epd = data;
2810 struct extent_io_tree *tree = epd->tree;
2811 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2812 u64 delalloc_start;
2813 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2814 u64 end;
2815 u64 cur = start;
2816 u64 extent_offset;
2817 u64 last_byte = i_size_read(inode);
2818 u64 block_start;
2819 u64 iosize;
2820 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002821 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002822 struct extent_map *em;
2823 struct block_device *bdev;
2824 int ret;
2825 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002826 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002827 size_t blocksize;
2828 loff_t i_size = i_size_read(inode);
2829 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2830 u64 nr_delalloc;
2831 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002832 int page_started;
2833 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002834 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002835 unsigned long nr_written = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002836 bool fill_delalloc = true;
Chris Masond1310b22008-01-24 16:13:08 -05002837
Chris Masonffbd5172009-04-20 15:50:09 -04002838 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002839 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002840 else
2841 write_flags = WRITE;
2842
liubo1abe9b82011-03-24 11:18:59 +00002843 trace___extent_writepage(page, inode, wbc);
2844
Chris Masond1310b22008-01-24 16:13:08 -05002845 WARN_ON(!PageLocked(page));
Chris Masonbf0da8c2011-11-04 12:29:37 -04002846
2847 ClearPageError(page);
2848
Chris Mason7f3c74f2008-07-18 12:01:11 -04002849 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002850 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002851 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002852 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002853 unlock_page(page);
2854 return 0;
2855 }
2856
2857 if (page->index == end_index) {
2858 char *userpage;
2859
Cong Wang7ac687d2011-11-25 23:14:28 +08002860 userpage = kmap_atomic(page);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002861 memset(userpage + pg_offset, 0,
2862 PAGE_CACHE_SIZE - pg_offset);
Cong Wang7ac687d2011-11-25 23:14:28 +08002863 kunmap_atomic(userpage);
Chris Mason211c17f2008-05-15 09:13:45 -04002864 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002865 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002866 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002867
2868 set_page_extent_mapped(page);
2869
Josef Bacik9e487102011-08-01 12:08:18 -04002870 if (!tree->ops || !tree->ops->fill_delalloc)
2871 fill_delalloc = false;
2872
Chris Masond1310b22008-01-24 16:13:08 -05002873 delalloc_start = start;
2874 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002875 page_started = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002876 if (!epd->extent_locked && fill_delalloc) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002877 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002878 /*
2879 * make sure the wbc mapping index is at least updated
2880 * to this page.
2881 */
2882 update_nr_written(page, wbc, 0);
2883
Chris Masond3977122009-01-05 21:25:51 -05002884 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002885 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002886 page,
2887 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002888 &delalloc_end,
2889 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002890 if (nr_delalloc == 0) {
2891 delalloc_start = delalloc_end + 1;
2892 continue;
2893 }
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002894 ret = tree->ops->fill_delalloc(inode, page,
2895 delalloc_start,
2896 delalloc_end,
2897 &page_started,
2898 &nr_written);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002899 /* File system has been set read-only */
2900 if (ret) {
2901 SetPageError(page);
2902 goto done;
2903 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002904 /*
2905 * delalloc_end is already one less than the total
2906 * length, so we don't subtract one from
2907 * PAGE_CACHE_SIZE
2908 */
2909 delalloc_to_write += (delalloc_end - delalloc_start +
2910 PAGE_CACHE_SIZE) >>
2911 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002912 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002913 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002914 if (wbc->nr_to_write < delalloc_to_write) {
2915 int thresh = 8192;
2916
2917 if (delalloc_to_write < thresh * 2)
2918 thresh = delalloc_to_write;
2919 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2920 thresh);
2921 }
Chris Masonc8b97812008-10-29 14:49:59 -04002922
Chris Mason771ed682008-11-06 22:02:51 -05002923 /* did the fill delalloc function already unlock and start
2924 * the IO?
2925 */
2926 if (page_started) {
2927 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002928 /*
2929 * we've unlocked the page, so we can't update
2930 * the mapping's writeback index, just update
2931 * nr_to_write.
2932 */
2933 wbc->nr_to_write -= nr_written;
2934 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002935 }
Chris Masonc8b97812008-10-29 14:49:59 -04002936 }
Chris Mason247e7432008-07-17 12:53:51 -04002937 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002938 ret = tree->ops->writepage_start_hook(page, start,
2939 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002940 if (ret) {
2941 /* Fixup worker will requeue */
2942 if (ret == -EBUSY)
2943 wbc->pages_skipped++;
2944 else
2945 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002946 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002947 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002948 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002949 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002950 }
2951 }
2952
Chris Mason11c83492009-04-20 15:50:09 -04002953 /*
2954 * we don't want to touch the inode after unlocking the page,
2955 * so we update the mapping writeback index now
2956 */
2957 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002958
Chris Masond1310b22008-01-24 16:13:08 -05002959 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002960 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002961 if (tree->ops && tree->ops->writepage_end_io_hook)
2962 tree->ops->writepage_end_io_hook(page, start,
2963 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002964 goto done;
2965 }
2966
Chris Masond1310b22008-01-24 16:13:08 -05002967 blocksize = inode->i_sb->s_blocksize;
2968
2969 while (cur <= end) {
2970 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002971 if (tree->ops && tree->ops->writepage_end_io_hook)
2972 tree->ops->writepage_end_io_hook(page, cur,
2973 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002974 break;
2975 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002976 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002977 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02002978 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002979 SetPageError(page);
2980 break;
2981 }
2982
2983 extent_offset = cur - em->start;
2984 BUG_ON(extent_map_end(em) <= cur);
2985 BUG_ON(end < cur);
2986 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2987 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2988 sector = (em->block_start + extent_offset) >> 9;
2989 bdev = em->bdev;
2990 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002991 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002992 free_extent_map(em);
2993 em = NULL;
2994
Chris Masonc8b97812008-10-29 14:49:59 -04002995 /*
2996 * compressed and inline extents are written through other
2997 * paths in the FS
2998 */
2999 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05003000 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04003001 /*
3002 * end_io notification does not happen here for
3003 * compressed extents
3004 */
3005 if (!compressed && tree->ops &&
3006 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003007 tree->ops->writepage_end_io_hook(page, cur,
3008 cur + iosize - 1,
3009 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04003010 else if (compressed) {
3011 /* we don't want to end_page_writeback on
3012 * a compressed extent. this happens
3013 * elsewhere
3014 */
3015 nr++;
3016 }
3017
3018 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003019 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003020 continue;
3021 }
Chris Masond1310b22008-01-24 16:13:08 -05003022 /* leave this out until we have a page_mkwrite call */
3023 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04003024 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05003025 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003026 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003027 continue;
3028 }
Chris Masonc8b97812008-10-29 14:49:59 -04003029
Chris Masond1310b22008-01-24 16:13:08 -05003030 if (tree->ops && tree->ops->writepage_io_hook) {
3031 ret = tree->ops->writepage_io_hook(page, cur,
3032 cur + iosize - 1);
3033 } else {
3034 ret = 0;
3035 }
Chris Mason1259ab72008-05-12 13:39:03 -04003036 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003037 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003038 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003039 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003040
Chris Masond1310b22008-01-24 16:13:08 -05003041 set_range_writeback(tree, cur, cur + iosize - 1);
3042 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05003043 printk(KERN_ERR "btrfs warning page %lu not "
3044 "writeback, cur %llu end %llu\n",
3045 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05003046 (unsigned long long)end);
3047 }
3048
Chris Masonffbd5172009-04-20 15:50:09 -04003049 ret = submit_extent_page(write_flags, tree, page,
3050 sector, iosize, pg_offset,
3051 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04003052 end_bio_extent_writepage,
3053 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003054 if (ret)
3055 SetPageError(page);
3056 }
3057 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003058 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003059 nr++;
3060 }
3061done:
3062 if (nr == 0) {
3063 /* make sure the mapping tag for page dirty gets cleared */
3064 set_page_writeback(page);
3065 end_page_writeback(page);
3066 }
Chris Masond1310b22008-01-24 16:13:08 -05003067 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05003068
Chris Mason11c83492009-04-20 15:50:09 -04003069done_unlocked:
3070
Chris Mason2c64c532009-09-02 15:04:12 -04003071 /* drop our reference on any cached states */
3072 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05003073 return 0;
3074}
3075
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003076static int eb_wait(void *word)
3077{
3078 io_schedule();
3079 return 0;
3080}
3081
3082static void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
3083{
3084 wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
3085 TASK_UNINTERRUPTIBLE);
3086}
3087
3088static int lock_extent_buffer_for_io(struct extent_buffer *eb,
3089 struct btrfs_fs_info *fs_info,
3090 struct extent_page_data *epd)
3091{
3092 unsigned long i, num_pages;
3093 int flush = 0;
3094 int ret = 0;
3095
3096 if (!btrfs_try_tree_write_lock(eb)) {
3097 flush = 1;
3098 flush_write_bio(epd);
3099 btrfs_tree_lock(eb);
3100 }
3101
3102 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3103 btrfs_tree_unlock(eb);
3104 if (!epd->sync_io)
3105 return 0;
3106 if (!flush) {
3107 flush_write_bio(epd);
3108 flush = 1;
3109 }
Chris Masona098d8e2012-03-21 12:09:56 -04003110 while (1) {
3111 wait_on_extent_buffer_writeback(eb);
3112 btrfs_tree_lock(eb);
3113 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3114 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003115 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003116 }
3117 }
3118
3119 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3120 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3121 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
3122 spin_lock(&fs_info->delalloc_lock);
3123 if (fs_info->dirty_metadata_bytes >= eb->len)
3124 fs_info->dirty_metadata_bytes -= eb->len;
3125 else
3126 WARN_ON(1);
3127 spin_unlock(&fs_info->delalloc_lock);
3128 ret = 1;
3129 }
3130
3131 btrfs_tree_unlock(eb);
3132
3133 if (!ret)
3134 return ret;
3135
3136 num_pages = num_extent_pages(eb->start, eb->len);
3137 for (i = 0; i < num_pages; i++) {
3138 struct page *p = extent_buffer_page(eb, i);
3139
3140 if (!trylock_page(p)) {
3141 if (!flush) {
3142 flush_write_bio(epd);
3143 flush = 1;
3144 }
3145 lock_page(p);
3146 }
3147 }
3148
3149 return ret;
3150}
3151
3152static void end_extent_buffer_writeback(struct extent_buffer *eb)
3153{
3154 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3155 smp_mb__after_clear_bit();
3156 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3157}
3158
3159static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
3160{
3161 int uptodate = err == 0;
3162 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
3163 struct extent_buffer *eb;
3164 int done;
3165
3166 do {
3167 struct page *page = bvec->bv_page;
3168
3169 bvec--;
3170 eb = (struct extent_buffer *)page->private;
3171 BUG_ON(!eb);
3172 done = atomic_dec_and_test(&eb->io_pages);
3173
3174 if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
3175 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3176 ClearPageUptodate(page);
3177 SetPageError(page);
3178 }
3179
3180 end_page_writeback(page);
3181
3182 if (!done)
3183 continue;
3184
3185 end_extent_buffer_writeback(eb);
3186 } while (bvec >= bio->bi_io_vec);
3187
3188 bio_put(bio);
3189
3190}
3191
3192static int write_one_eb(struct extent_buffer *eb,
3193 struct btrfs_fs_info *fs_info,
3194 struct writeback_control *wbc,
3195 struct extent_page_data *epd)
3196{
3197 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
3198 u64 offset = eb->start;
3199 unsigned long i, num_pages;
3200 int rw = (epd->sync_io ? WRITE_SYNC : WRITE);
3201 int ret;
3202
3203 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3204 num_pages = num_extent_pages(eb->start, eb->len);
3205 atomic_set(&eb->io_pages, num_pages);
3206 for (i = 0; i < num_pages; i++) {
3207 struct page *p = extent_buffer_page(eb, i);
3208
3209 clear_page_dirty_for_io(p);
3210 set_page_writeback(p);
3211 ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
3212 PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
3213 -1, end_bio_extent_buffer_writepage,
3214 0, 0, 0);
3215 if (ret) {
3216 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3217 SetPageError(p);
3218 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3219 end_extent_buffer_writeback(eb);
3220 ret = -EIO;
3221 break;
3222 }
3223 offset += PAGE_CACHE_SIZE;
3224 update_nr_written(p, wbc, 1);
3225 unlock_page(p);
3226 }
3227
3228 if (unlikely(ret)) {
3229 for (; i < num_pages; i++) {
3230 struct page *p = extent_buffer_page(eb, i);
3231 unlock_page(p);
3232 }
3233 }
3234
3235 return ret;
3236}
3237
3238int btree_write_cache_pages(struct address_space *mapping,
3239 struct writeback_control *wbc)
3240{
3241 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3242 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3243 struct extent_buffer *eb, *prev_eb = NULL;
3244 struct extent_page_data epd = {
3245 .bio = NULL,
3246 .tree = tree,
3247 .extent_locked = 0,
3248 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3249 };
3250 int ret = 0;
3251 int done = 0;
3252 int nr_to_write_done = 0;
3253 struct pagevec pvec;
3254 int nr_pages;
3255 pgoff_t index;
3256 pgoff_t end; /* Inclusive */
3257 int scanned = 0;
3258 int tag;
3259
3260 pagevec_init(&pvec, 0);
3261 if (wbc->range_cyclic) {
3262 index = mapping->writeback_index; /* Start from prev offset */
3263 end = -1;
3264 } else {
3265 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3266 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3267 scanned = 1;
3268 }
3269 if (wbc->sync_mode == WB_SYNC_ALL)
3270 tag = PAGECACHE_TAG_TOWRITE;
3271 else
3272 tag = PAGECACHE_TAG_DIRTY;
3273retry:
3274 if (wbc->sync_mode == WB_SYNC_ALL)
3275 tag_pages_for_writeback(mapping, index, end);
3276 while (!done && !nr_to_write_done && (index <= end) &&
3277 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3278 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3279 unsigned i;
3280
3281 scanned = 1;
3282 for (i = 0; i < nr_pages; i++) {
3283 struct page *page = pvec.pages[i];
3284
3285 if (!PagePrivate(page))
3286 continue;
3287
3288 if (!wbc->range_cyclic && page->index > end) {
3289 done = 1;
3290 break;
3291 }
3292
3293 eb = (struct extent_buffer *)page->private;
3294 if (!eb) {
3295 WARN_ON(1);
3296 continue;
3297 }
3298
3299 if (eb == prev_eb)
3300 continue;
3301
3302 if (!atomic_inc_not_zero(&eb->refs)) {
3303 WARN_ON(1);
3304 continue;
3305 }
3306
3307 prev_eb = eb;
3308 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3309 if (!ret) {
3310 free_extent_buffer(eb);
3311 continue;
3312 }
3313
3314 ret = write_one_eb(eb, fs_info, wbc, &epd);
3315 if (ret) {
3316 done = 1;
3317 free_extent_buffer(eb);
3318 break;
3319 }
3320 free_extent_buffer(eb);
3321
3322 /*
3323 * the filesystem may choose to bump up nr_to_write.
3324 * We have to make sure to honor the new nr_to_write
3325 * at any time
3326 */
3327 nr_to_write_done = wbc->nr_to_write <= 0;
3328 }
3329 pagevec_release(&pvec);
3330 cond_resched();
3331 }
3332 if (!scanned && !done) {
3333 /*
3334 * We hit the last page and there is more work to be done: wrap
3335 * back to the start of the file
3336 */
3337 scanned = 1;
3338 index = 0;
3339 goto retry;
3340 }
3341 flush_write_bio(&epd);
3342 return ret;
3343}
3344
Chris Masond1310b22008-01-24 16:13:08 -05003345/**
Chris Mason4bef0842008-09-08 11:18:08 -04003346 * 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 -05003347 * @mapping: address space structure to write
3348 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3349 * @writepage: function called for each page
3350 * @data: data passed to writepage function
3351 *
3352 * If a page is already under I/O, write_cache_pages() skips it, even
3353 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3354 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3355 * and msync() need to guarantee that all the data which was dirty at the time
3356 * the call was made get new I/O started against them. If wbc->sync_mode is
3357 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3358 * existing IO to complete.
3359 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003360static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04003361 struct address_space *mapping,
3362 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003363 writepage_t writepage, void *data,
3364 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05003365{
Chris Masond1310b22008-01-24 16:13:08 -05003366 int ret = 0;
3367 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003368 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003369 struct pagevec pvec;
3370 int nr_pages;
3371 pgoff_t index;
3372 pgoff_t end; /* Inclusive */
3373 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003374 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003375
Chris Masond1310b22008-01-24 16:13:08 -05003376 pagevec_init(&pvec, 0);
3377 if (wbc->range_cyclic) {
3378 index = mapping->writeback_index; /* Start from prev offset */
3379 end = -1;
3380 } else {
3381 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3382 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003383 scanned = 1;
3384 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003385 if (wbc->sync_mode == WB_SYNC_ALL)
3386 tag = PAGECACHE_TAG_TOWRITE;
3387 else
3388 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003389retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003390 if (wbc->sync_mode == WB_SYNC_ALL)
3391 tag_pages_for_writeback(mapping, index, end);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003392 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003393 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3394 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003395 unsigned i;
3396
3397 scanned = 1;
3398 for (i = 0; i < nr_pages; i++) {
3399 struct page *page = pvec.pages[i];
3400
3401 /*
3402 * At this point we hold neither mapping->tree_lock nor
3403 * lock on the page itself: the page may be truncated or
3404 * invalidated (changing page->mapping to NULL), or even
3405 * swizzled back from swapper_space to tmpfs file
3406 * mapping
3407 */
Chris Mason01d658f2011-11-01 10:08:06 -04003408 if (tree->ops &&
3409 tree->ops->write_cache_pages_lock_hook) {
3410 tree->ops->write_cache_pages_lock_hook(page,
3411 data, flush_fn);
3412 } else {
3413 if (!trylock_page(page)) {
3414 flush_fn(data);
3415 lock_page(page);
3416 }
3417 }
Chris Masond1310b22008-01-24 16:13:08 -05003418
3419 if (unlikely(page->mapping != mapping)) {
3420 unlock_page(page);
3421 continue;
3422 }
3423
3424 if (!wbc->range_cyclic && page->index > end) {
3425 done = 1;
3426 unlock_page(page);
3427 continue;
3428 }
3429
Chris Masond2c3f4f2008-11-19 12:44:22 -05003430 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003431 if (PageWriteback(page))
3432 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05003433 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003434 }
Chris Masond1310b22008-01-24 16:13:08 -05003435
3436 if (PageWriteback(page) ||
3437 !clear_page_dirty_for_io(page)) {
3438 unlock_page(page);
3439 continue;
3440 }
3441
3442 ret = (*writepage)(page, wbc, data);
3443
3444 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3445 unlock_page(page);
3446 ret = 0;
3447 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003448 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05003449 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003450
3451 /*
3452 * the filesystem may choose to bump up nr_to_write.
3453 * We have to make sure to honor the new nr_to_write
3454 * at any time
3455 */
3456 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05003457 }
3458 pagevec_release(&pvec);
3459 cond_resched();
3460 }
3461 if (!scanned && !done) {
3462 /*
3463 * We hit the last page and there is more work to be done: wrap
3464 * back to the start of the file
3465 */
3466 scanned = 1;
3467 index = 0;
3468 goto retry;
3469 }
Chris Masond1310b22008-01-24 16:13:08 -05003470 return ret;
3471}
Chris Masond1310b22008-01-24 16:13:08 -05003472
Chris Masonffbd5172009-04-20 15:50:09 -04003473static void flush_epd_write_bio(struct extent_page_data *epd)
3474{
3475 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04003476 int rw = WRITE;
3477 int ret;
3478
Chris Masonffbd5172009-04-20 15:50:09 -04003479 if (epd->sync_io)
Jeff Mahoney355808c2011-10-03 23:23:14 -04003480 rw = WRITE_SYNC;
3481
3482 ret = submit_one_bio(rw, epd->bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003483 BUG_ON(ret < 0); /* -ENOMEM */
Chris Masonffbd5172009-04-20 15:50:09 -04003484 epd->bio = NULL;
3485 }
3486}
3487
Chris Masond2c3f4f2008-11-19 12:44:22 -05003488static noinline void flush_write_bio(void *data)
3489{
3490 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04003491 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003492}
3493
Chris Masond1310b22008-01-24 16:13:08 -05003494int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3495 get_extent_t *get_extent,
3496 struct writeback_control *wbc)
3497{
3498 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003499 struct extent_page_data epd = {
3500 .bio = NULL,
3501 .tree = tree,
3502 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003503 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003504 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05003505 };
Chris Masond1310b22008-01-24 16:13:08 -05003506
Chris Masond1310b22008-01-24 16:13:08 -05003507 ret = __extent_writepage(page, wbc, &epd);
3508
Chris Masonffbd5172009-04-20 15:50:09 -04003509 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003510 return ret;
3511}
Chris Masond1310b22008-01-24 16:13:08 -05003512
Chris Mason771ed682008-11-06 22:02:51 -05003513int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3514 u64 start, u64 end, get_extent_t *get_extent,
3515 int mode)
3516{
3517 int ret = 0;
3518 struct address_space *mapping = inode->i_mapping;
3519 struct page *page;
3520 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3521 PAGE_CACHE_SHIFT;
3522
3523 struct extent_page_data epd = {
3524 .bio = NULL,
3525 .tree = tree,
3526 .get_extent = get_extent,
3527 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04003528 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05003529 };
3530 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05003531 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05003532 .nr_to_write = nr_pages * 2,
3533 .range_start = start,
3534 .range_end = end + 1,
3535 };
3536
Chris Masond3977122009-01-05 21:25:51 -05003537 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05003538 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3539 if (clear_page_dirty_for_io(page))
3540 ret = __extent_writepage(page, &wbc_writepages, &epd);
3541 else {
3542 if (tree->ops && tree->ops->writepage_end_io_hook)
3543 tree->ops->writepage_end_io_hook(page, start,
3544 start + PAGE_CACHE_SIZE - 1,
3545 NULL, 1);
3546 unlock_page(page);
3547 }
3548 page_cache_release(page);
3549 start += PAGE_CACHE_SIZE;
3550 }
3551
Chris Masonffbd5172009-04-20 15:50:09 -04003552 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05003553 return ret;
3554}
Chris Masond1310b22008-01-24 16:13:08 -05003555
3556int extent_writepages(struct extent_io_tree *tree,
3557 struct address_space *mapping,
3558 get_extent_t *get_extent,
3559 struct writeback_control *wbc)
3560{
3561 int ret = 0;
3562 struct extent_page_data epd = {
3563 .bio = NULL,
3564 .tree = tree,
3565 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003566 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003567 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05003568 };
3569
Chris Mason4bef0842008-09-08 11:18:08 -04003570 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003571 __extent_writepage, &epd,
3572 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04003573 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003574 return ret;
3575}
Chris Masond1310b22008-01-24 16:13:08 -05003576
3577int extent_readpages(struct extent_io_tree *tree,
3578 struct address_space *mapping,
3579 struct list_head *pages, unsigned nr_pages,
3580 get_extent_t get_extent)
3581{
3582 struct bio *bio = NULL;
3583 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04003584 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003585
Chris Masond1310b22008-01-24 16:13:08 -05003586 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
3587 struct page *page = list_entry(pages->prev, struct page, lru);
3588
3589 prefetchw(&page->flags);
3590 list_del(&page->lru);
Nick Piggin28ecb602010-03-17 13:31:04 +00003591 if (!add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04003592 page->index, GFP_NOFS)) {
Chris Masonf1885912008-04-09 16:28:12 -04003593 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04003594 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003595 }
3596 page_cache_release(page);
3597 }
Chris Masond1310b22008-01-24 16:13:08 -05003598 BUG_ON(!list_empty(pages));
3599 if (bio)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003600 return submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003601 return 0;
3602}
Chris Masond1310b22008-01-24 16:13:08 -05003603
3604/*
3605 * basic invalidatepage code, this waits on any locked or writeback
3606 * ranges corresponding to the page, and then deletes any extent state
3607 * records from the tree
3608 */
3609int extent_invalidatepage(struct extent_io_tree *tree,
3610 struct page *page, unsigned long offset)
3611{
Josef Bacik2ac55d42010-02-03 19:33:23 +00003612 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003613 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
3614 u64 end = start + PAGE_CACHE_SIZE - 1;
3615 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3616
Chris Masond3977122009-01-05 21:25:51 -05003617 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003618 if (start > end)
3619 return 0;
3620
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003621 lock_extent_bits(tree, start, end, 0, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04003622 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003623 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04003624 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3625 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003626 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003627 return 0;
3628}
Chris Masond1310b22008-01-24 16:13:08 -05003629
3630/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04003631 * a helper for releasepage, this tests for areas of the page that
3632 * are locked or under IO and drops the related state bits if it is safe
3633 * to drop the page.
3634 */
3635int try_release_extent_state(struct extent_map_tree *map,
3636 struct extent_io_tree *tree, struct page *page,
3637 gfp_t mask)
3638{
3639 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3640 u64 end = start + PAGE_CACHE_SIZE - 1;
3641 int ret = 1;
3642
Chris Mason211f90e2008-07-18 11:56:15 -04003643 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04003644 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04003645 ret = 0;
3646 else {
3647 if ((mask & GFP_NOFS) == GFP_NOFS)
3648 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04003649 /*
3650 * at this point we can safely clear everything except the
3651 * locked bit and the nodatasum bit
3652 */
Chris Masone3f24cc2011-02-14 12:52:08 -05003653 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04003654 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3655 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05003656
3657 /* if clear_extent_bit failed for enomem reasons,
3658 * we can't allow the release to continue.
3659 */
3660 if (ret < 0)
3661 ret = 0;
3662 else
3663 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003664 }
3665 return ret;
3666}
Chris Mason7b13b7b2008-04-18 10:29:50 -04003667
3668/*
Chris Masond1310b22008-01-24 16:13:08 -05003669 * a helper for releasepage. As long as there are no locked extents
3670 * in the range corresponding to the page, both state records and extent
3671 * map records are removed
3672 */
3673int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05003674 struct extent_io_tree *tree, struct page *page,
3675 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05003676{
3677 struct extent_map *em;
3678 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3679 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003680
Chris Mason70dec802008-01-29 09:59:12 -05003681 if ((mask & __GFP_WAIT) &&
3682 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05003683 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05003684 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05003685 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04003686 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05003687 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09003688 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04003689 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003690 break;
3691 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003692 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3693 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04003694 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003695 free_extent_map(em);
3696 break;
3697 }
3698 if (!test_range_bit(tree, em->start,
3699 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04003700 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04003701 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05003702 remove_extent_mapping(map, em);
3703 /* once for the rb tree */
3704 free_extent_map(em);
3705 }
3706 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04003707 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003708
3709 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05003710 free_extent_map(em);
3711 }
Chris Masond1310b22008-01-24 16:13:08 -05003712 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04003713 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003714}
Chris Masond1310b22008-01-24 16:13:08 -05003715
Chris Masonec29ed52011-02-23 16:23:20 -05003716/*
3717 * helper function for fiemap, which doesn't want to see any holes.
3718 * This maps until we find something past 'last'
3719 */
3720static struct extent_map *get_extent_skip_holes(struct inode *inode,
3721 u64 offset,
3722 u64 last,
3723 get_extent_t *get_extent)
3724{
3725 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3726 struct extent_map *em;
3727 u64 len;
3728
3729 if (offset >= last)
3730 return NULL;
3731
3732 while(1) {
3733 len = last - offset;
3734 if (len == 0)
3735 break;
3736 len = (len + sectorsize - 1) & ~(sectorsize - 1);
3737 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02003738 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05003739 return em;
3740
3741 /* if this isn't a hole return it */
3742 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3743 em->block_start != EXTENT_MAP_HOLE) {
3744 return em;
3745 }
3746
3747 /* this is a hole, advance to the next extent */
3748 offset = extent_map_end(em);
3749 free_extent_map(em);
3750 if (offset >= last)
3751 break;
3752 }
3753 return NULL;
3754}
3755
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003756int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3757 __u64 start, __u64 len, get_extent_t *get_extent)
3758{
Josef Bacik975f84f2010-11-23 19:36:57 +00003759 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003760 u64 off = start;
3761 u64 max = start + len;
3762 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00003763 u32 found_type;
3764 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05003765 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003766 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003767 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00003768 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003769 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003770 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00003771 struct btrfs_path *path;
3772 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003773 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003774 u64 em_start = 0;
3775 u64 em_len = 0;
3776 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003777 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003778
3779 if (len == 0)
3780 return -EINVAL;
3781
Josef Bacik975f84f2010-11-23 19:36:57 +00003782 path = btrfs_alloc_path();
3783 if (!path)
3784 return -ENOMEM;
3785 path->leave_spinning = 1;
3786
Josef Bacik4d479cf2011-11-17 11:34:31 -05003787 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3788 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3789
Chris Masonec29ed52011-02-23 16:23:20 -05003790 /*
3791 * lookup the last file extent. We're not using i_size here
3792 * because there might be preallocation past i_size
3793 */
Josef Bacik975f84f2010-11-23 19:36:57 +00003794 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
Li Zefan33345d012011-04-20 10:31:50 +08003795 path, btrfs_ino(inode), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00003796 if (ret < 0) {
3797 btrfs_free_path(path);
3798 return ret;
3799 }
3800 WARN_ON(!ret);
3801 path->slots[0]--;
3802 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3803 struct btrfs_file_extent_item);
3804 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3805 found_type = btrfs_key_type(&found_key);
3806
Chris Masonec29ed52011-02-23 16:23:20 -05003807 /* No extents, but there might be delalloc bits */
Li Zefan33345d012011-04-20 10:31:50 +08003808 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00003809 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05003810 /* have to trust i_size as the end */
3811 last = (u64)-1;
3812 last_for_get_extent = isize;
3813 } else {
3814 /*
3815 * remember the start of the last extent. There are a
3816 * bunch of different factors that go into the length of the
3817 * extent, so its much less complex to remember where it started
3818 */
3819 last = found_key.offset;
3820 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00003821 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003822 btrfs_free_path(path);
3823
Chris Masonec29ed52011-02-23 16:23:20 -05003824 /*
3825 * we might have some extents allocated but more delalloc past those
3826 * extents. so, we trust isize unless the start of the last extent is
3827 * beyond isize
3828 */
3829 if (last < isize) {
3830 last = (u64)-1;
3831 last_for_get_extent = isize;
3832 }
3833
Josef Bacik2ac55d42010-02-03 19:33:23 +00003834 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003835 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05003836
Josef Bacik4d479cf2011-11-17 11:34:31 -05003837 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05003838 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003839 if (!em)
3840 goto out;
3841 if (IS_ERR(em)) {
3842 ret = PTR_ERR(em);
3843 goto out;
3844 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003845
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003846 while (!end) {
Chris Masonea8efc72011-03-08 11:54:40 -05003847 u64 offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003848
Chris Masonea8efc72011-03-08 11:54:40 -05003849 /* break if the extent we found is outside the range */
3850 if (em->start >= max || extent_map_end(em) < off)
3851 break;
3852
3853 /*
3854 * get_extent may return an extent that starts before our
3855 * requested range. We have to make sure the ranges
3856 * we return to fiemap always move forward and don't
3857 * overlap, so adjust the offsets here
3858 */
3859 em_start = max(em->start, off);
3860
3861 /*
3862 * record the offset from the start of the extent
3863 * for adjusting the disk offset below
3864 */
3865 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05003866 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05003867 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05003868 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003869 disko = 0;
3870 flags = 0;
3871
Chris Masonea8efc72011-03-08 11:54:40 -05003872 /*
3873 * bump off for our next call to get_extent
3874 */
3875 off = extent_map_end(em);
3876 if (off >= max)
3877 end = 1;
3878
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003879 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003880 end = 1;
3881 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003882 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003883 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3884 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003885 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003886 flags |= (FIEMAP_EXTENT_DELALLOC |
3887 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003888 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05003889 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003890 }
3891 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3892 flags |= FIEMAP_EXTENT_ENCODED;
3893
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003894 free_extent_map(em);
3895 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05003896 if ((em_start >= last) || em_len == (u64)-1 ||
3897 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003898 flags |= FIEMAP_EXTENT_LAST;
3899 end = 1;
3900 }
3901
Chris Masonec29ed52011-02-23 16:23:20 -05003902 /* now scan forward to see if this is really the last extent. */
3903 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3904 get_extent);
3905 if (IS_ERR(em)) {
3906 ret = PTR_ERR(em);
3907 goto out;
3908 }
3909 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00003910 flags |= FIEMAP_EXTENT_LAST;
3911 end = 1;
3912 }
Chris Masonec29ed52011-02-23 16:23:20 -05003913 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3914 em_len, flags);
3915 if (ret)
3916 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003917 }
3918out_free:
3919 free_extent_map(em);
3920out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00003921 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3922 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003923 return ret;
3924}
3925
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003926inline struct page *extent_buffer_page(struct extent_buffer *eb,
Chris Masond1310b22008-01-24 16:13:08 -05003927 unsigned long i)
3928{
Chris Mason727011e2010-08-06 13:21:20 -04003929 return eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05003930}
3931
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003932inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04003933{
Chris Mason6af118c2008-07-22 11:18:07 -04003934 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3935 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04003936}
3937
Chris Mason727011e2010-08-06 13:21:20 -04003938static void __free_extent_buffer(struct extent_buffer *eb)
3939{
3940#if LEAK_DEBUG
3941 unsigned long flags;
3942 spin_lock_irqsave(&leak_lock, flags);
3943 list_del(&eb->leak_list);
3944 spin_unlock_irqrestore(&leak_lock, flags);
3945#endif
3946 if (eb->pages && eb->pages != eb->inline_pages)
3947 kfree(eb->pages);
3948 kmem_cache_free(extent_buffer_cache, eb);
3949}
3950
Chris Masond1310b22008-01-24 16:13:08 -05003951static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3952 u64 start,
3953 unsigned long len,
3954 gfp_t mask)
3955{
3956 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003957#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003958 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003959#endif
Chris Masond1310b22008-01-24 16:13:08 -05003960
Chris Masond1310b22008-01-24 16:13:08 -05003961 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003962 if (eb == NULL)
3963 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003964 eb->start = start;
3965 eb->len = len;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05003966 eb->tree = tree;
Chris Masonbd681512011-07-16 15:23:14 -04003967 rwlock_init(&eb->lock);
3968 atomic_set(&eb->write_locks, 0);
3969 atomic_set(&eb->read_locks, 0);
3970 atomic_set(&eb->blocking_readers, 0);
3971 atomic_set(&eb->blocking_writers, 0);
3972 atomic_set(&eb->spinning_readers, 0);
3973 atomic_set(&eb->spinning_writers, 0);
Arne Jansen5b25f702011-09-13 10:55:48 +02003974 eb->lock_nested = 0;
Chris Masonbd681512011-07-16 15:23:14 -04003975 init_waitqueue_head(&eb->write_lock_wq);
3976 init_waitqueue_head(&eb->read_lock_wq);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003977
Chris Mason39351272009-02-04 09:24:05 -05003978#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003979 spin_lock_irqsave(&leak_lock, flags);
3980 list_add(&eb->leak_list, &buffers);
3981 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003982#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05003983 spin_lock_init(&eb->refs_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003984 atomic_set(&eb->refs, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003985 atomic_set(&eb->io_pages, 0);
Chris Mason727011e2010-08-06 13:21:20 -04003986
3987 if (len > MAX_INLINE_EXTENT_BUFFER_SIZE) {
3988 struct page **pages;
3989 int num_pages = (len + PAGE_CACHE_SIZE - 1) >>
3990 PAGE_CACHE_SHIFT;
3991 pages = kzalloc(num_pages, mask);
3992 if (!pages) {
3993 __free_extent_buffer(eb);
3994 return NULL;
3995 }
3996 eb->pages = pages;
3997 } else {
3998 eb->pages = eb->inline_pages;
3999 }
Chris Masond1310b22008-01-24 16:13:08 -05004000
4001 return eb;
4002}
4003
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004004static int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004005{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004006 return (atomic_read(&eb->io_pages) ||
4007 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4008 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004009}
4010
Miao Xie897ca6e2010-10-26 20:57:29 -04004011/*
4012 * Helper for releasing extent buffer page.
4013 */
4014static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
4015 unsigned long start_idx)
4016{
4017 unsigned long index;
4018 struct page *page;
4019
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004020 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e2010-10-26 20:57:29 -04004021
4022 index = num_extent_pages(eb->start, eb->len);
4023 if (start_idx >= index)
4024 return;
4025
4026 do {
4027 index--;
4028 page = extent_buffer_page(eb, index);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004029 if (page) {
4030 spin_lock(&page->mapping->private_lock);
4031 /*
4032 * We do this since we'll remove the pages after we've
4033 * removed the eb from the radix tree, so we could race
4034 * and have this page now attached to the new eb. So
4035 * only clear page_private if it's still connected to
4036 * this eb.
4037 */
4038 if (PagePrivate(page) &&
4039 page->private == (unsigned long)eb) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004040 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Josef Bacik3083ee22012-03-09 16:01:49 -05004041 BUG_ON(PageDirty(page));
4042 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004043 /*
4044 * We need to make sure we haven't be attached
4045 * to a new eb.
4046 */
4047 ClearPagePrivate(page);
4048 set_page_private(page, 0);
4049 /* One for the page private */
4050 page_cache_release(page);
4051 }
4052 spin_unlock(&page->mapping->private_lock);
4053
4054 /* One for when we alloced the page */
Miao Xie897ca6e2010-10-26 20:57:29 -04004055 page_cache_release(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004056 }
Miao Xie897ca6e2010-10-26 20:57:29 -04004057 } while (index != start_idx);
4058}
4059
4060/*
4061 * Helper for releasing the extent buffer.
4062 */
4063static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4064{
4065 btrfs_release_extent_buffer_page(eb, 0);
4066 __free_extent_buffer(eb);
4067}
4068
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004069static void check_buffer_tree_ref(struct extent_buffer *eb)
4070{
4071 /* the ref bit is tricky. We have to make sure it is set
4072 * if we have the buffer dirty. Otherwise the
4073 * code to free a buffer can end up dropping a dirty
4074 * page
4075 *
4076 * Once the ref bit is set, it won't go away while the
4077 * buffer is dirty or in writeback, and it also won't
4078 * go away while we have the reference count on the
4079 * eb bumped.
4080 *
4081 * We can't just set the ref bit without bumping the
4082 * ref on the eb because free_extent_buffer might
4083 * see the ref bit and try to clear it. If this happens
4084 * free_extent_buffer might end up dropping our original
4085 * ref by mistake and freeing the page before we are able
4086 * to add one more ref.
4087 *
4088 * So bump the ref count first, then set the bit. If someone
4089 * beat us to it, drop the ref we added.
4090 */
4091 if (!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
4092 atomic_inc(&eb->refs);
4093 if (test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4094 atomic_dec(&eb->refs);
4095 }
4096}
4097
Josef Bacik5df42352012-03-15 18:24:42 -04004098static void mark_extent_buffer_accessed(struct extent_buffer *eb)
4099{
4100 unsigned long num_pages, i;
4101
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004102 check_buffer_tree_ref(eb);
4103
Josef Bacik5df42352012-03-15 18:24:42 -04004104 num_pages = num_extent_pages(eb->start, eb->len);
4105 for (i = 0; i < num_pages; i++) {
4106 struct page *p = extent_buffer_page(eb, i);
4107 mark_page_accessed(p);
4108 }
4109}
4110
Chris Masond1310b22008-01-24 16:13:08 -05004111struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
Chris Mason727011e2010-08-06 13:21:20 -04004112 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004113{
4114 unsigned long num_pages = num_extent_pages(start, len);
4115 unsigned long i;
4116 unsigned long index = start >> PAGE_CACHE_SHIFT;
4117 struct extent_buffer *eb;
Chris Mason6af118c2008-07-22 11:18:07 -04004118 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004119 struct page *p;
4120 struct address_space *mapping = tree->mapping;
4121 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004122 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004123
Miao Xie19fe0a82010-10-26 20:57:29 -04004124 rcu_read_lock();
4125 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4126 if (eb && atomic_inc_not_zero(&eb->refs)) {
4127 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004128 mark_extent_buffer_accessed(eb);
Chris Mason6af118c2008-07-22 11:18:07 -04004129 return eb;
4130 }
Miao Xie19fe0a82010-10-26 20:57:29 -04004131 rcu_read_unlock();
Chris Mason6af118c2008-07-22 11:18:07 -04004132
David Sterbaba144192011-04-21 01:12:06 +02004133 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
Peter2b114d12008-04-01 11:21:40 -04004134 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004135 return NULL;
4136
Chris Mason727011e2010-08-06 13:21:20 -04004137 for (i = 0; i < num_pages; i++, index++) {
Chris Masona6591712011-07-19 12:04:14 -04004138 p = find_or_create_page(mapping, index, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05004139 if (!p) {
4140 WARN_ON(1);
Chris Mason6af118c2008-07-22 11:18:07 -04004141 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05004142 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004143
4144 spin_lock(&mapping->private_lock);
4145 if (PagePrivate(p)) {
4146 /*
4147 * We could have already allocated an eb for this page
4148 * and attached one so lets see if we can get a ref on
4149 * the existing eb, and if we can we know it's good and
4150 * we can just return that one, else we know we can just
4151 * overwrite page->private.
4152 */
4153 exists = (struct extent_buffer *)p->private;
4154 if (atomic_inc_not_zero(&exists->refs)) {
4155 spin_unlock(&mapping->private_lock);
4156 unlock_page(p);
Josef Bacik17de39a2012-05-04 15:16:06 -04004157 page_cache_release(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004158 mark_extent_buffer_accessed(exists);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004159 goto free_eb;
4160 }
4161
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004162 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004163 * Do this so attach doesn't complain and we need to
4164 * drop the ref the old guy had.
4165 */
4166 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004167 WARN_ON(PageDirty(p));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004168 page_cache_release(p);
Chris Masond1310b22008-01-24 16:13:08 -05004169 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004170 attach_extent_buffer_page(eb, p);
4171 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004172 WARN_ON(PageDirty(p));
Chris Masond1310b22008-01-24 16:13:08 -05004173 mark_page_accessed(p);
Chris Mason727011e2010-08-06 13:21:20 -04004174 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05004175 if (!PageUptodate(p))
4176 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05004177
4178 /*
4179 * see below about how we avoid a nasty race with release page
4180 * and why we unlock later
4181 */
Chris Masond1310b22008-01-24 16:13:08 -05004182 }
4183 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004184 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05004185again:
Miao Xie19fe0a82010-10-26 20:57:29 -04004186 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4187 if (ret)
4188 goto free_eb;
4189
Chris Mason6af118c2008-07-22 11:18:07 -04004190 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004191 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
4192 if (ret == -EEXIST) {
4193 exists = radix_tree_lookup(&tree->buffer,
4194 start >> PAGE_CACHE_SHIFT);
Josef Bacik115391d2012-03-09 09:51:43 -05004195 if (!atomic_inc_not_zero(&exists->refs)) {
4196 spin_unlock(&tree->buffer_lock);
4197 radix_tree_preload_end();
Josef Bacik115391d2012-03-09 09:51:43 -05004198 exists = NULL;
4199 goto again;
4200 }
Chris Mason6af118c2008-07-22 11:18:07 -04004201 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004202 radix_tree_preload_end();
Josef Bacik5df42352012-03-15 18:24:42 -04004203 mark_extent_buffer_accessed(exists);
Chris Mason6af118c2008-07-22 11:18:07 -04004204 goto free_eb;
4205 }
Chris Mason6af118c2008-07-22 11:18:07 -04004206 /* add one reference for the tree */
Josef Bacik3083ee22012-03-09 16:01:49 -05004207 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004208 check_buffer_tree_ref(eb);
Josef Bacik3083ee22012-03-09 16:01:49 -05004209 spin_unlock(&eb->refs_lock);
Yan, Zhengf044ba72010-02-04 08:46:56 +00004210 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004211 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05004212
4213 /*
4214 * there is a race where release page may have
4215 * tried to find this extent buffer in the radix
4216 * but failed. It will tell the VM it is safe to
4217 * reclaim the, and it will clear the page private bit.
4218 * We must make sure to set the page private bit properly
4219 * after the extent buffer is in the radix tree so
4220 * it doesn't get lost
4221 */
Chris Mason727011e2010-08-06 13:21:20 -04004222 SetPageChecked(eb->pages[0]);
4223 for (i = 1; i < num_pages; i++) {
4224 p = extent_buffer_page(eb, i);
Chris Mason727011e2010-08-06 13:21:20 -04004225 ClearPageChecked(p);
4226 unlock_page(p);
4227 }
4228 unlock_page(eb->pages[0]);
Chris Masond1310b22008-01-24 16:13:08 -05004229 return eb;
4230
Chris Mason6af118c2008-07-22 11:18:07 -04004231free_eb:
Chris Mason727011e2010-08-06 13:21:20 -04004232 for (i = 0; i < num_pages; i++) {
4233 if (eb->pages[i])
4234 unlock_page(eb->pages[i]);
4235 }
Chris Masoneb14ab82011-02-10 12:35:00 -05004236
Josef Bacik17de39a2012-05-04 15:16:06 -04004237 WARN_ON(!atomic_dec_and_test(&eb->refs));
Miao Xie897ca6e2010-10-26 20:57:29 -04004238 btrfs_release_extent_buffer(eb);
Chris Mason6af118c2008-07-22 11:18:07 -04004239 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05004240}
Chris Masond1310b22008-01-24 16:13:08 -05004241
4242struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
David Sterbaf09d1f62011-04-21 01:08:01 +02004243 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004244{
Chris Masond1310b22008-01-24 16:13:08 -05004245 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05004246
Miao Xie19fe0a82010-10-26 20:57:29 -04004247 rcu_read_lock();
4248 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4249 if (eb && atomic_inc_not_zero(&eb->refs)) {
4250 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004251 mark_extent_buffer_accessed(eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04004252 return eb;
4253 }
4254 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04004255
Miao Xie19fe0a82010-10-26 20:57:29 -04004256 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004257}
Chris Masond1310b22008-01-24 16:13:08 -05004258
Josef Bacik3083ee22012-03-09 16:01:49 -05004259static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4260{
4261 struct extent_buffer *eb =
4262 container_of(head, struct extent_buffer, rcu_head);
4263
4264 __free_extent_buffer(eb);
4265}
4266
Josef Bacik3083ee22012-03-09 16:01:49 -05004267/* Expects to have eb->eb_lock already held */
4268static void release_extent_buffer(struct extent_buffer *eb, gfp_t mask)
4269{
4270 WARN_ON(atomic_read(&eb->refs) == 0);
4271 if (atomic_dec_and_test(&eb->refs)) {
4272 struct extent_io_tree *tree = eb->tree;
Josef Bacik3083ee22012-03-09 16:01:49 -05004273
4274 spin_unlock(&eb->refs_lock);
4275
Josef Bacik3083ee22012-03-09 16:01:49 -05004276 spin_lock(&tree->buffer_lock);
4277 radix_tree_delete(&tree->buffer,
4278 eb->start >> PAGE_CACHE_SHIFT);
4279 spin_unlock(&tree->buffer_lock);
4280
4281 /* Should be safe to release our pages at this point */
4282 btrfs_release_extent_buffer_page(eb, 0);
4283
4284 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
4285 return;
4286 }
4287 spin_unlock(&eb->refs_lock);
4288}
4289
Chris Masond1310b22008-01-24 16:13:08 -05004290void free_extent_buffer(struct extent_buffer *eb)
4291{
Chris Masond1310b22008-01-24 16:13:08 -05004292 if (!eb)
4293 return;
4294
Josef Bacik3083ee22012-03-09 16:01:49 -05004295 spin_lock(&eb->refs_lock);
4296 if (atomic_read(&eb->refs) == 2 &&
4297 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004298 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004299 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4300 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05004301
Josef Bacik3083ee22012-03-09 16:01:49 -05004302 /*
4303 * I know this is terrible, but it's temporary until we stop tracking
4304 * the uptodate bits and such for the extent buffers.
4305 */
4306 release_extent_buffer(eb, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05004307}
Chris Masond1310b22008-01-24 16:13:08 -05004308
Josef Bacik3083ee22012-03-09 16:01:49 -05004309void free_extent_buffer_stale(struct extent_buffer *eb)
4310{
4311 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004312 return;
4313
Josef Bacik3083ee22012-03-09 16:01:49 -05004314 spin_lock(&eb->refs_lock);
4315 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
4316
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004317 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004318 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4319 atomic_dec(&eb->refs);
4320 release_extent_buffer(eb, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05004321}
4322
Chris Mason1d4284b2012-03-28 20:31:37 -04004323void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004324{
Chris Masond1310b22008-01-24 16:13:08 -05004325 unsigned long i;
4326 unsigned long num_pages;
4327 struct page *page;
4328
Chris Masond1310b22008-01-24 16:13:08 -05004329 num_pages = num_extent_pages(eb->start, eb->len);
4330
4331 for (i = 0; i < num_pages; i++) {
4332 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04004333 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05004334 continue;
4335
Chris Masona61e6f22008-07-22 11:18:08 -04004336 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05004337 WARN_ON(!PagePrivate(page));
4338
Chris Masond1310b22008-01-24 16:13:08 -05004339 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004340 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05004341 if (!PageDirty(page)) {
4342 radix_tree_tag_clear(&page->mapping->page_tree,
4343 page_index(page),
4344 PAGECACHE_TAG_DIRTY);
4345 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004346 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04004347 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04004348 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05004349 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004350 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05004351}
Chris Masond1310b22008-01-24 16:13:08 -05004352
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004353int set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004354{
4355 unsigned long i;
4356 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04004357 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004358
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004359 check_buffer_tree_ref(eb);
4360
Chris Masonb9473432009-03-13 11:00:37 -04004361 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004362
Chris Masond1310b22008-01-24 16:13:08 -05004363 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik3083ee22012-03-09 16:01:49 -05004364 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004365 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
4366
Chris Masonb9473432009-03-13 11:00:37 -04004367 for (i = 0; i < num_pages; i++)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004368 set_page_dirty(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04004369 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05004370}
Chris Masond1310b22008-01-24 16:13:08 -05004371
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004372static int range_straddles_pages(u64 start, u64 len)
Chris Mason19b6caf2011-07-25 06:50:50 -04004373{
4374 if (len < PAGE_CACHE_SIZE)
4375 return 1;
4376 if (start & (PAGE_CACHE_SIZE - 1))
4377 return 1;
4378 if ((start + len) & (PAGE_CACHE_SIZE - 1))
4379 return 1;
4380 return 0;
4381}
4382
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004383int clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04004384{
4385 unsigned long i;
4386 struct page *page;
4387 unsigned long num_pages;
4388
Chris Masonb4ce94d2009-02-04 09:25:08 -05004389 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004390 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason1259ab72008-05-12 13:39:03 -04004391 for (i = 0; i < num_pages; i++) {
4392 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04004393 if (page)
4394 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04004395 }
4396 return 0;
4397}
4398
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004399int set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004400{
4401 unsigned long i;
4402 struct page *page;
4403 unsigned long num_pages;
4404
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004405 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004406 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05004407 for (i = 0; i < num_pages; i++) {
4408 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004409 SetPageUptodate(page);
4410 }
4411 return 0;
4412}
Chris Masond1310b22008-01-24 16:13:08 -05004413
Chris Masonce9adaa2008-04-09 16:28:12 -04004414int extent_range_uptodate(struct extent_io_tree *tree,
4415 u64 start, u64 end)
4416{
4417 struct page *page;
4418 int ret;
4419 int pg_uptodate = 1;
4420 int uptodate;
4421 unsigned long index;
4422
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004423 if (range_straddles_pages(start, end - start + 1)) {
Chris Mason19b6caf2011-07-25 06:50:50 -04004424 ret = test_range_bit(tree, start, end,
4425 EXTENT_UPTODATE, 1, NULL);
4426 if (ret)
4427 return 1;
4428 }
Chris Masond3977122009-01-05 21:25:51 -05004429 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004430 index = start >> PAGE_CACHE_SHIFT;
4431 page = find_get_page(tree->mapping, index);
Mitch Harder8bedd512012-01-26 15:01:11 -05004432 if (!page)
4433 return 1;
Chris Masonce9adaa2008-04-09 16:28:12 -04004434 uptodate = PageUptodate(page);
4435 page_cache_release(page);
4436 if (!uptodate) {
4437 pg_uptodate = 0;
4438 break;
4439 }
4440 start += PAGE_CACHE_SIZE;
4441 }
4442 return pg_uptodate;
4443}
4444
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004445int extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004446{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004447 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004448}
Chris Masond1310b22008-01-24 16:13:08 -05004449
4450int read_extent_buffer_pages(struct extent_io_tree *tree,
Arne Jansenbb82ab82011-06-10 14:06:53 +02004451 struct extent_buffer *eb, u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04004452 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05004453{
4454 unsigned long i;
4455 unsigned long start_i;
4456 struct page *page;
4457 int err;
4458 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04004459 int locked_pages = 0;
4460 int all_uptodate = 1;
Chris Masond1310b22008-01-24 16:13:08 -05004461 unsigned long num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04004462 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004463 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004464 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004465
Chris Masonb4ce94d2009-02-04 09:25:08 -05004466 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05004467 return 0;
4468
Chris Masond1310b22008-01-24 16:13:08 -05004469 if (start) {
4470 WARN_ON(start < eb->start);
4471 start_i = (start >> PAGE_CACHE_SHIFT) -
4472 (eb->start >> PAGE_CACHE_SHIFT);
4473 } else {
4474 start_i = 0;
4475 }
4476
4477 num_pages = num_extent_pages(eb->start, eb->len);
4478 for (i = start_i; i < num_pages; i++) {
4479 page = extent_buffer_page(eb, i);
Arne Jansenbb82ab82011-06-10 14:06:53 +02004480 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04004481 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04004482 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05004483 } else {
4484 lock_page(page);
4485 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004486 locked_pages++;
Chris Mason727011e2010-08-06 13:21:20 -04004487 if (!PageUptodate(page)) {
4488 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04004489 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04004490 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004491 }
4492 if (all_uptodate) {
4493 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004494 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04004495 goto unlock_exit;
4496 }
4497
Josef Bacikea466792012-03-26 21:57:36 -04004498 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04004499 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004500 atomic_set(&eb->io_pages, num_reads);
Chris Masonce9adaa2008-04-09 16:28:12 -04004501 for (i = start_i; i < num_pages; i++) {
4502 page = extent_buffer_page(eb, i);
Chris Masonce9adaa2008-04-09 16:28:12 -04004503 if (!PageUptodate(page)) {
Chris Masonf1885912008-04-09 16:28:12 -04004504 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05004505 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04004506 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04004507 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05004508 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05004509 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05004510 } else {
4511 unlock_page(page);
4512 }
4513 }
4514
Jeff Mahoney355808c2011-10-03 23:23:14 -04004515 if (bio) {
4516 err = submit_one_bio(READ, bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004517 if (err)
4518 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04004519 }
Chris Masona86c12c2008-02-07 10:50:54 -05004520
Arne Jansenbb82ab82011-06-10 14:06:53 +02004521 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05004522 return ret;
Chris Masond3977122009-01-05 21:25:51 -05004523
Chris Masond1310b22008-01-24 16:13:08 -05004524 for (i = start_i; i < num_pages; i++) {
4525 page = extent_buffer_page(eb, i);
4526 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05004527 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05004528 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05004529 }
Chris Masond3977122009-01-05 21:25:51 -05004530
Chris Masond1310b22008-01-24 16:13:08 -05004531 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04004532
4533unlock_exit:
4534 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05004535 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004536 page = extent_buffer_page(eb, i);
4537 i++;
4538 unlock_page(page);
4539 locked_pages--;
4540 }
4541 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004542}
Chris Masond1310b22008-01-24 16:13:08 -05004543
4544void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4545 unsigned long start,
4546 unsigned long len)
4547{
4548 size_t cur;
4549 size_t offset;
4550 struct page *page;
4551 char *kaddr;
4552 char *dst = (char *)dstv;
4553 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4554 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004555
4556 WARN_ON(start > eb->len);
4557 WARN_ON(start + len > eb->start + eb->len);
4558
4559 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4560
Chris Masond3977122009-01-05 21:25:51 -05004561 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004562 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004563
4564 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04004565 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004566 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004567
4568 dst += cur;
4569 len -= cur;
4570 offset = 0;
4571 i++;
4572 }
4573}
Chris Masond1310b22008-01-24 16:13:08 -05004574
4575int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04004576 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05004577 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04004578 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05004579{
4580 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4581 char *kaddr;
4582 struct page *p;
4583 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4584 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4585 unsigned long end_i = (start_offset + start + min_len - 1) >>
4586 PAGE_CACHE_SHIFT;
4587
4588 if (i != end_i)
4589 return -EINVAL;
4590
4591 if (i == 0) {
4592 offset = start_offset;
4593 *map_start = 0;
4594 } else {
4595 offset = 0;
4596 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4597 }
Chris Masond3977122009-01-05 21:25:51 -05004598
Chris Masond1310b22008-01-24 16:13:08 -05004599 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05004600 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
4601 "wanted %lu %lu\n", (unsigned long long)eb->start,
4602 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05004603 WARN_ON(1);
Josef Bacik850265332011-03-15 14:52:12 -04004604 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05004605 }
4606
4607 p = extent_buffer_page(eb, i);
Chris Masona6591712011-07-19 12:04:14 -04004608 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05004609 *map = kaddr + offset;
4610 *map_len = PAGE_CACHE_SIZE - offset;
4611 return 0;
4612}
Chris Masond1310b22008-01-24 16:13:08 -05004613
Chris Masond1310b22008-01-24 16:13:08 -05004614int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4615 unsigned long start,
4616 unsigned long len)
4617{
4618 size_t cur;
4619 size_t offset;
4620 struct page *page;
4621 char *kaddr;
4622 char *ptr = (char *)ptrv;
4623 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4624 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4625 int ret = 0;
4626
4627 WARN_ON(start > eb->len);
4628 WARN_ON(start + len > eb->start + eb->len);
4629
4630 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4631
Chris Masond3977122009-01-05 21:25:51 -05004632 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004633 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004634
4635 cur = min(len, (PAGE_CACHE_SIZE - offset));
4636
Chris Masona6591712011-07-19 12:04:14 -04004637 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004638 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004639 if (ret)
4640 break;
4641
4642 ptr += cur;
4643 len -= cur;
4644 offset = 0;
4645 i++;
4646 }
4647 return ret;
4648}
Chris Masond1310b22008-01-24 16:13:08 -05004649
4650void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4651 unsigned long start, unsigned long len)
4652{
4653 size_t cur;
4654 size_t offset;
4655 struct page *page;
4656 char *kaddr;
4657 char *src = (char *)srcv;
4658 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4659 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4660
4661 WARN_ON(start > eb->len);
4662 WARN_ON(start + len > eb->start + eb->len);
4663
4664 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4665
Chris Masond3977122009-01-05 21:25:51 -05004666 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004667 page = extent_buffer_page(eb, i);
4668 WARN_ON(!PageUptodate(page));
4669
4670 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004671 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004672 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004673
4674 src += cur;
4675 len -= cur;
4676 offset = 0;
4677 i++;
4678 }
4679}
Chris Masond1310b22008-01-24 16:13:08 -05004680
4681void memset_extent_buffer(struct extent_buffer *eb, char c,
4682 unsigned long start, unsigned long len)
4683{
4684 size_t cur;
4685 size_t offset;
4686 struct page *page;
4687 char *kaddr;
4688 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4689 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4690
4691 WARN_ON(start > eb->len);
4692 WARN_ON(start + len > eb->start + eb->len);
4693
4694 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4695
Chris Masond3977122009-01-05 21:25:51 -05004696 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004697 page = extent_buffer_page(eb, i);
4698 WARN_ON(!PageUptodate(page));
4699
4700 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004701 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004702 memset(kaddr + offset, c, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004703
4704 len -= cur;
4705 offset = 0;
4706 i++;
4707 }
4708}
Chris Masond1310b22008-01-24 16:13:08 -05004709
4710void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4711 unsigned long dst_offset, unsigned long src_offset,
4712 unsigned long len)
4713{
4714 u64 dst_len = dst->len;
4715 size_t cur;
4716 size_t offset;
4717 struct page *page;
4718 char *kaddr;
4719 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4720 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4721
4722 WARN_ON(src->len != dst_len);
4723
4724 offset = (start_offset + dst_offset) &
4725 ((unsigned long)PAGE_CACHE_SIZE - 1);
4726
Chris Masond3977122009-01-05 21:25:51 -05004727 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004728 page = extent_buffer_page(dst, i);
4729 WARN_ON(!PageUptodate(page));
4730
4731 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4732
Chris Masona6591712011-07-19 12:04:14 -04004733 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004734 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004735
4736 src_offset += cur;
4737 len -= cur;
4738 offset = 0;
4739 i++;
4740 }
4741}
Chris Masond1310b22008-01-24 16:13:08 -05004742
4743static void move_pages(struct page *dst_page, struct page *src_page,
4744 unsigned long dst_off, unsigned long src_off,
4745 unsigned long len)
4746{
Chris Masona6591712011-07-19 12:04:14 -04004747 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004748 if (dst_page == src_page) {
4749 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4750 } else {
Chris Masona6591712011-07-19 12:04:14 -04004751 char *src_kaddr = page_address(src_page);
Chris Masond1310b22008-01-24 16:13:08 -05004752 char *p = dst_kaddr + dst_off + len;
4753 char *s = src_kaddr + src_off + len;
4754
4755 while (len--)
4756 *--p = *--s;
Chris Masond1310b22008-01-24 16:13:08 -05004757 }
Chris Masond1310b22008-01-24 16:13:08 -05004758}
4759
Sergei Trofimovich33872062011-04-11 21:52:52 +00004760static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4761{
4762 unsigned long distance = (src > dst) ? src - dst : dst - src;
4763 return distance < len;
4764}
4765
Chris Masond1310b22008-01-24 16:13:08 -05004766static void copy_pages(struct page *dst_page, struct page *src_page,
4767 unsigned long dst_off, unsigned long src_off,
4768 unsigned long len)
4769{
Chris Masona6591712011-07-19 12:04:14 -04004770 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004771 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004772 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004773
Sergei Trofimovich33872062011-04-11 21:52:52 +00004774 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04004775 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00004776 } else {
Chris Masond1310b22008-01-24 16:13:08 -05004777 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004778 if (areas_overlap(src_off, dst_off, len))
4779 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00004780 }
Chris Masond1310b22008-01-24 16:13:08 -05004781
Chris Mason727011e2010-08-06 13:21:20 -04004782 if (must_memmove)
4783 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
4784 else
4785 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05004786}
4787
4788void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4789 unsigned long src_offset, unsigned long len)
4790{
4791 size_t cur;
4792 size_t dst_off_in_page;
4793 size_t src_off_in_page;
4794 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4795 unsigned long dst_i;
4796 unsigned long src_i;
4797
4798 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004799 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4800 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004801 BUG_ON(1);
4802 }
4803 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004804 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4805 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004806 BUG_ON(1);
4807 }
4808
Chris Masond3977122009-01-05 21:25:51 -05004809 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004810 dst_off_in_page = (start_offset + dst_offset) &
4811 ((unsigned long)PAGE_CACHE_SIZE - 1);
4812 src_off_in_page = (start_offset + src_offset) &
4813 ((unsigned long)PAGE_CACHE_SIZE - 1);
4814
4815 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4816 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4817
4818 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4819 src_off_in_page));
4820 cur = min_t(unsigned long, cur,
4821 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4822
4823 copy_pages(extent_buffer_page(dst, dst_i),
4824 extent_buffer_page(dst, src_i),
4825 dst_off_in_page, src_off_in_page, cur);
4826
4827 src_offset += cur;
4828 dst_offset += cur;
4829 len -= cur;
4830 }
4831}
Chris Masond1310b22008-01-24 16:13:08 -05004832
4833void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4834 unsigned long src_offset, unsigned long len)
4835{
4836 size_t cur;
4837 size_t dst_off_in_page;
4838 size_t src_off_in_page;
4839 unsigned long dst_end = dst_offset + len - 1;
4840 unsigned long src_end = src_offset + len - 1;
4841 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4842 unsigned long dst_i;
4843 unsigned long src_i;
4844
4845 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004846 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4847 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004848 BUG_ON(1);
4849 }
4850 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004851 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4852 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004853 BUG_ON(1);
4854 }
Chris Mason727011e2010-08-06 13:21:20 -04004855 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05004856 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4857 return;
4858 }
Chris Masond3977122009-01-05 21:25:51 -05004859 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004860 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
4861 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
4862
4863 dst_off_in_page = (start_offset + dst_end) &
4864 ((unsigned long)PAGE_CACHE_SIZE - 1);
4865 src_off_in_page = (start_offset + src_end) &
4866 ((unsigned long)PAGE_CACHE_SIZE - 1);
4867
4868 cur = min_t(unsigned long, len, src_off_in_page + 1);
4869 cur = min(cur, dst_off_in_page + 1);
4870 move_pages(extent_buffer_page(dst, dst_i),
4871 extent_buffer_page(dst, src_i),
4872 dst_off_in_page - cur + 1,
4873 src_off_in_page - cur + 1, cur);
4874
4875 dst_end -= cur;
4876 src_end -= cur;
4877 len -= cur;
4878 }
4879}
Chris Mason6af118c2008-07-22 11:18:07 -04004880
Josef Bacik3083ee22012-03-09 16:01:49 -05004881int try_release_extent_buffer(struct page *page, gfp_t mask)
Miao Xie19fe0a82010-10-26 20:57:29 -04004882{
Chris Mason6af118c2008-07-22 11:18:07 -04004883 struct extent_buffer *eb;
Miao Xie897ca6e2010-10-26 20:57:29 -04004884
Miao Xie19fe0a82010-10-26 20:57:29 -04004885 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05004886 * We need to make sure noboody is attaching this page to an eb right
4887 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04004888 */
Josef Bacik3083ee22012-03-09 16:01:49 -05004889 spin_lock(&page->mapping->private_lock);
4890 if (!PagePrivate(page)) {
4891 spin_unlock(&page->mapping->private_lock);
4892 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004893 }
4894
Josef Bacik3083ee22012-03-09 16:01:49 -05004895 eb = (struct extent_buffer *)page->private;
4896 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04004897
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004898 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05004899 * This is a little awful but should be ok, we need to make sure that
4900 * the eb doesn't disappear out from under us while we're looking at
4901 * this page.
4902 */
4903 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004904 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05004905 spin_unlock(&eb->refs_lock);
4906 spin_unlock(&page->mapping->private_lock);
4907 return 0;
4908 }
4909 spin_unlock(&page->mapping->private_lock);
4910
4911 if ((mask & GFP_NOFS) == GFP_NOFS)
4912 mask = GFP_NOFS;
4913
4914 /*
4915 * If tree ref isn't set then we know the ref on this eb is a real ref,
4916 * so just return, this page will likely be freed soon anyway.
4917 */
4918 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
4919 spin_unlock(&eb->refs_lock);
4920 return 0;
4921 }
4922 release_extent_buffer(eb, mask);
4923
4924 return 1;
Chris Mason6af118c2008-07-22 11:18:07 -04004925}