blob: 2c8f7b2046173954f720125a6e53e96de3c7727e [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 Mason6af118ce2008-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
Chris Masond1310b22008-01-24 16:13:08 -0500189 rb_link_node(node, parent, p);
190 rb_insert_color(node, root);
191 return NULL;
192}
193
Chris Mason80ea96b2008-02-01 14:51:59 -0500194static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Chris Masond1310b22008-01-24 16:13:08 -0500195 struct rb_node **prev_ret,
196 struct rb_node **next_ret)
197{
Chris Mason80ea96b2008-02-01 14:51:59 -0500198 struct rb_root *root = &tree->state;
Chris Masond3977122009-01-05 21:25:51 -0500199 struct rb_node *n = root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500200 struct rb_node *prev = NULL;
201 struct rb_node *orig_prev = NULL;
202 struct tree_entry *entry;
203 struct tree_entry *prev_entry = NULL;
204
Chris Masond3977122009-01-05 21:25:51 -0500205 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500206 entry = rb_entry(n, struct tree_entry, rb_node);
207 prev = n;
208 prev_entry = entry;
209
210 if (offset < entry->start)
211 n = n->rb_left;
212 else if (offset > entry->end)
213 n = n->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500214 else
Chris Masond1310b22008-01-24 16:13:08 -0500215 return n;
216 }
217
218 if (prev_ret) {
219 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500220 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500221 prev = rb_next(prev);
222 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
223 }
224 *prev_ret = prev;
225 prev = orig_prev;
226 }
227
228 if (next_ret) {
229 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500230 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500231 prev = rb_prev(prev);
232 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
233 }
234 *next_ret = prev;
235 }
236 return NULL;
237}
238
Chris Mason80ea96b2008-02-01 14:51:59 -0500239static inline struct rb_node *tree_search(struct extent_io_tree *tree,
240 u64 offset)
Chris Masond1310b22008-01-24 16:13:08 -0500241{
Chris Mason70dec802008-01-29 09:59:12 -0500242 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500243 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500244
Chris Mason80ea96b2008-02-01 14:51:59 -0500245 ret = __etree_search(tree, offset, &prev, NULL);
Chris Masond3977122009-01-05 21:25:51 -0500246 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500247 return prev;
248 return ret;
249}
250
Josef Bacik9ed74f22009-09-11 16:12:44 -0400251static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
252 struct extent_state *other)
253{
254 if (tree->ops && tree->ops->merge_extent_hook)
255 tree->ops->merge_extent_hook(tree->mapping->host, new,
256 other);
257}
258
Chris Masond1310b22008-01-24 16:13:08 -0500259/*
260 * utility function to look for merge candidates inside a given range.
261 * Any extents with matching state are merged together into a single
262 * extent in the tree. Extents with EXTENT_IO in their state field
263 * are not merged because the end_io handlers need to be able to do
264 * operations on them without sleeping (or doing allocations/splits).
265 *
266 * This should be called with the tree lock held.
267 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000268static void merge_state(struct extent_io_tree *tree,
269 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500270{
271 struct extent_state *other;
272 struct rb_node *other_node;
273
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400274 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000275 return;
Chris Masond1310b22008-01-24 16:13:08 -0500276
277 other_node = rb_prev(&state->rb_node);
278 if (other_node) {
279 other = rb_entry(other_node, struct extent_state, rb_node);
280 if (other->end == state->start - 1 &&
281 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400282 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500283 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500284 other->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500285 rb_erase(&other->rb_node, &tree->state);
286 free_extent_state(other);
287 }
288 }
289 other_node = rb_next(&state->rb_node);
290 if (other_node) {
291 other = rb_entry(other_node, struct extent_state, rb_node);
292 if (other->start == state->end + 1 &&
293 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400294 merge_cb(tree, state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400295 state->end = other->end;
296 other->tree = NULL;
297 rb_erase(&other->rb_node, &tree->state);
298 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500299 }
300 }
Chris Masond1310b22008-01-24 16:13:08 -0500301}
302
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000303static void set_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400304 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500305{
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000306 if (tree->ops && tree->ops->set_bit_hook)
307 tree->ops->set_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500308}
309
310static void clear_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400311 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500312{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400313 if (tree->ops && tree->ops->clear_bit_hook)
314 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500315}
316
Xiao Guangrong3150b692011-07-14 03:19:08 +0000317static void set_state_bits(struct extent_io_tree *tree,
318 struct extent_state *state, int *bits);
319
Chris Masond1310b22008-01-24 16:13:08 -0500320/*
321 * insert an extent_state struct into the tree. 'bits' are set on the
322 * struct before it is inserted.
323 *
324 * This may return -EEXIST if the extent is already there, in which case the
325 * state struct is freed.
326 *
327 * The tree lock is not taken internally. This is a utility function and
328 * probably isn't what you want to call (see set/clear_extent_bit).
329 */
330static int insert_state(struct extent_io_tree *tree,
331 struct extent_state *state, u64 start, u64 end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400332 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500333{
334 struct rb_node *node;
335
336 if (end < start) {
Chris Masond3977122009-01-05 21:25:51 -0500337 printk(KERN_ERR "btrfs end < start %llu %llu\n",
338 (unsigned long long)end,
339 (unsigned long long)start);
Chris Masond1310b22008-01-24 16:13:08 -0500340 WARN_ON(1);
341 }
Chris Masond1310b22008-01-24 16:13:08 -0500342 state->start = start;
343 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400344
Xiao Guangrong3150b692011-07-14 03:19:08 +0000345 set_state_bits(tree, state, bits);
346
Chris Masond1310b22008-01-24 16:13:08 -0500347 node = tree_insert(&tree->state, end, &state->rb_node);
348 if (node) {
349 struct extent_state *found;
350 found = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500351 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
352 "%llu %llu\n", (unsigned long long)found->start,
353 (unsigned long long)found->end,
354 (unsigned long long)start, (unsigned long long)end);
Chris Masond1310b22008-01-24 16:13:08 -0500355 return -EEXIST;
356 }
Chris Mason70dec802008-01-29 09:59:12 -0500357 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500358 merge_state(tree, state);
359 return 0;
360}
361
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000362static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
Josef Bacik9ed74f22009-09-11 16:12:44 -0400363 u64 split)
364{
365 if (tree->ops && tree->ops->split_extent_hook)
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000366 tree->ops->split_extent_hook(tree->mapping->host, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400367}
368
Chris Masond1310b22008-01-24 16:13:08 -0500369/*
370 * split a given extent state struct in two, inserting the preallocated
371 * struct 'prealloc' as the newly created second half. 'split' indicates an
372 * offset inside 'orig' where it should be split.
373 *
374 * Before calling,
375 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
376 * are two extent state structs in the tree:
377 * prealloc: [orig->start, split - 1]
378 * orig: [ split, orig->end ]
379 *
380 * The tree locks are not taken by this function. They need to be held
381 * by the caller.
382 */
383static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
384 struct extent_state *prealloc, u64 split)
385{
386 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400387
388 split_cb(tree, orig, split);
389
Chris Masond1310b22008-01-24 16:13:08 -0500390 prealloc->start = orig->start;
391 prealloc->end = split - 1;
392 prealloc->state = orig->state;
393 orig->start = split;
394
395 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
396 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500397 free_extent_state(prealloc);
398 return -EEXIST;
399 }
Chris Mason70dec802008-01-29 09:59:12 -0500400 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500401 return 0;
402}
403
Li Zefancdc6a392012-03-12 16:39:48 +0800404static struct extent_state *next_state(struct extent_state *state)
405{
406 struct rb_node *next = rb_next(&state->rb_node);
407 if (next)
408 return rb_entry(next, struct extent_state, rb_node);
409 else
410 return NULL;
411}
412
Chris Masond1310b22008-01-24 16:13:08 -0500413/*
414 * utility function to clear some bits in an extent state struct.
Wang Sheng-Hui1b303fc2012-04-06 14:35:18 +0800415 * it will optionally wake up any one waiting on this state (wake == 1).
Chris Masond1310b22008-01-24 16:13:08 -0500416 *
417 * If no bits are set on the state struct after clearing things, the
418 * struct is freed and removed from the tree
419 */
Li Zefancdc6a392012-03-12 16:39:48 +0800420static struct extent_state *clear_state_bit(struct extent_io_tree *tree,
421 struct extent_state *state,
422 int *bits, int wake)
Chris Masond1310b22008-01-24 16:13:08 -0500423{
Li Zefancdc6a392012-03-12 16:39:48 +0800424 struct extent_state *next;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400425 int bits_to_clear = *bits & ~EXTENT_CTLBITS;
Chris Masond1310b22008-01-24 16:13:08 -0500426
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400427 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500428 u64 range = state->end - state->start + 1;
429 WARN_ON(range > tree->dirty_bytes);
430 tree->dirty_bytes -= range;
431 }
Chris Mason291d6732008-01-29 15:55:23 -0500432 clear_state_cb(tree, state, bits);
Josef Bacik32c00af2009-10-08 13:34:05 -0400433 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500434 if (wake)
435 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400436 if (state->state == 0) {
Li Zefancdc6a392012-03-12 16:39:48 +0800437 next = next_state(state);
Chris Mason70dec802008-01-29 09:59:12 -0500438 if (state->tree) {
Chris Masond1310b22008-01-24 16:13:08 -0500439 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500440 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500441 free_extent_state(state);
442 } else {
443 WARN_ON(1);
444 }
445 } else {
446 merge_state(tree, state);
Li Zefancdc6a392012-03-12 16:39:48 +0800447 next = next_state(state);
Chris Masond1310b22008-01-24 16:13:08 -0500448 }
Li Zefancdc6a392012-03-12 16:39:48 +0800449 return next;
Chris Masond1310b22008-01-24 16:13:08 -0500450}
451
Xiao Guangrong82337672011-04-20 06:44:57 +0000452static struct extent_state *
453alloc_extent_state_atomic(struct extent_state *prealloc)
454{
455 if (!prealloc)
456 prealloc = alloc_extent_state(GFP_ATOMIC);
457
458 return prealloc;
459}
460
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400461void extent_io_tree_panic(struct extent_io_tree *tree, int err)
462{
463 btrfs_panic(tree_fs_info(tree), err, "Locking error: "
464 "Extent tree was modified by another "
465 "thread while locked.");
466}
467
Chris Masond1310b22008-01-24 16:13:08 -0500468/*
469 * clear some bits on a range in the tree. This may require splitting
470 * or inserting elements in the tree, so the gfp mask is used to
471 * indicate which allocations or sleeping are allowed.
472 *
473 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
474 * the given range from the tree regardless of state (ie for truncate).
475 *
476 * the range [start, end] is inclusive.
477 *
Jeff Mahoney6763af82012-03-01 14:56:29 +0100478 * This takes the tree lock, and returns 0 on success and < 0 on error.
Chris Masond1310b22008-01-24 16:13:08 -0500479 */
480int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400481 int bits, int wake, int delete,
482 struct extent_state **cached_state,
483 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500484{
485 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400486 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500487 struct extent_state *prealloc = NULL;
488 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400489 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500490 int err;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000491 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500492
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400493 if (delete)
494 bits |= ~EXTENT_CTLBITS;
495 bits |= EXTENT_FIRST_DELALLOC;
496
Josef Bacik2ac55d42010-02-03 19:33:23 +0000497 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
498 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500499again:
500 if (!prealloc && (mask & __GFP_WAIT)) {
501 prealloc = alloc_extent_state(mask);
502 if (!prealloc)
503 return -ENOMEM;
504 }
505
Chris Masoncad321a2008-12-17 14:51:42 -0500506 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400507 if (cached_state) {
508 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000509
510 if (clear) {
511 *cached_state = NULL;
512 cached_state = NULL;
513 }
514
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400515 if (cached && cached->tree && cached->start <= start &&
516 cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000517 if (clear)
518 atomic_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400519 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400520 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400521 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000522 if (clear)
523 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400524 }
Chris Masond1310b22008-01-24 16:13:08 -0500525 /*
526 * this search will find the extents that end after
527 * our range starts
528 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500529 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500530 if (!node)
531 goto out;
532 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400533hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500534 if (state->start > end)
535 goto out;
536 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400537 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500538
Liu Bo04493142012-02-16 18:34:37 +0800539 /* the state doesn't have the wanted bits, go ahead */
Li Zefancdc6a392012-03-12 16:39:48 +0800540 if (!(state->state & bits)) {
541 state = next_state(state);
Liu Bo04493142012-02-16 18:34:37 +0800542 goto next;
Li Zefancdc6a392012-03-12 16:39:48 +0800543 }
Liu Bo04493142012-02-16 18:34:37 +0800544
Chris Masond1310b22008-01-24 16:13:08 -0500545 /*
546 * | ---- desired range ---- |
547 * | state | or
548 * | ------------- state -------------- |
549 *
550 * We need to split the extent we found, and may flip
551 * bits on second half.
552 *
553 * If the extent we found extends past our range, we
554 * just split and search again. It'll get split again
555 * the next time though.
556 *
557 * If the extent we found is inside our range, we clear
558 * the desired bit on it.
559 */
560
561 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000562 prealloc = alloc_extent_state_atomic(prealloc);
563 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500564 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400565 if (err)
566 extent_io_tree_panic(tree, err);
567
Chris Masond1310b22008-01-24 16:13:08 -0500568 prealloc = NULL;
569 if (err)
570 goto out;
571 if (state->end <= end) {
Liu Bod1ac6e42012-05-10 18:10:39 +0800572 state = clear_state_bit(tree, state, &bits, wake);
573 goto next;
Chris Masond1310b22008-01-24 16:13:08 -0500574 }
575 goto search_again;
576 }
577 /*
578 * | ---- desired range ---- |
579 * | state |
580 * We need to split the extent, and clear the bit
581 * on the first half
582 */
583 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000584 prealloc = alloc_extent_state_atomic(prealloc);
585 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500586 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400587 if (err)
588 extent_io_tree_panic(tree, err);
589
Chris Masond1310b22008-01-24 16:13:08 -0500590 if (wake)
591 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400592
Jeff Mahoney6763af82012-03-01 14:56:29 +0100593 clear_state_bit(tree, prealloc, &bits, wake);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400594
Chris Masond1310b22008-01-24 16:13:08 -0500595 prealloc = NULL;
596 goto out;
597 }
Chris Mason42daec22009-09-23 19:51:09 -0400598
Li Zefancdc6a392012-03-12 16:39:48 +0800599 state = clear_state_bit(tree, state, &bits, wake);
Liu Bo04493142012-02-16 18:34:37 +0800600next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400601 if (last_end == (u64)-1)
602 goto out;
603 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800604 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800605 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500606 goto search_again;
607
608out:
Chris Masoncad321a2008-12-17 14:51:42 -0500609 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500610 if (prealloc)
611 free_extent_state(prealloc);
612
Jeff Mahoney6763af82012-03-01 14:56:29 +0100613 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500614
615search_again:
616 if (start > end)
617 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500618 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500619 if (mask & __GFP_WAIT)
620 cond_resched();
621 goto again;
622}
Chris Masond1310b22008-01-24 16:13:08 -0500623
Jeff Mahoney143bede2012-03-01 14:56:26 +0100624static void wait_on_state(struct extent_io_tree *tree,
625 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500626 __releases(tree->lock)
627 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500628{
629 DEFINE_WAIT(wait);
630 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500631 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500632 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500633 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500634 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500635}
636
637/*
638 * waits for one or more bits to clear on a range in the state tree.
639 * The range [start, end] is inclusive.
640 * The tree lock is taken by this function
641 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100642void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
Chris Masond1310b22008-01-24 16:13:08 -0500643{
644 struct extent_state *state;
645 struct rb_node *node;
646
Chris Masoncad321a2008-12-17 14:51:42 -0500647 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500648again:
649 while (1) {
650 /*
651 * this search will find all the extents that end after
652 * our range starts
653 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500654 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500655 if (!node)
656 break;
657
658 state = rb_entry(node, struct extent_state, rb_node);
659
660 if (state->start > end)
661 goto out;
662
663 if (state->state & bits) {
664 start = state->start;
665 atomic_inc(&state->refs);
666 wait_on_state(tree, state);
667 free_extent_state(state);
668 goto again;
669 }
670 start = state->end + 1;
671
672 if (start > end)
673 break;
674
Xiao Guangrongded91f02011-07-14 03:19:27 +0000675 cond_resched_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500676 }
677out:
Chris Masoncad321a2008-12-17 14:51:42 -0500678 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500679}
Chris Masond1310b22008-01-24 16:13:08 -0500680
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000681static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500682 struct extent_state *state,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400683 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500684{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400685 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400686
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000687 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400688 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500689 u64 range = state->end - state->start + 1;
690 tree->dirty_bytes += range;
691 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400692 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500693}
694
Chris Mason2c64c532009-09-02 15:04:12 -0400695static void cache_state(struct extent_state *state,
696 struct extent_state **cached_ptr)
697{
698 if (cached_ptr && !(*cached_ptr)) {
699 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
700 *cached_ptr = state;
701 atomic_inc(&state->refs);
702 }
703 }
704}
705
Arne Jansen507903b2011-04-06 10:02:20 +0000706static void uncache_state(struct extent_state **cached_ptr)
707{
708 if (cached_ptr && (*cached_ptr)) {
709 struct extent_state *state = *cached_ptr;
Chris Mason109b36a2011-04-12 13:57:39 -0400710 *cached_ptr = NULL;
711 free_extent_state(state);
Arne Jansen507903b2011-04-06 10:02:20 +0000712 }
713}
714
Chris Masond1310b22008-01-24 16:13:08 -0500715/*
Chris Mason1edbb732009-09-02 13:24:36 -0400716 * set some bits on a range in the tree. This may require allocations or
717 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500718 *
Chris Mason1edbb732009-09-02 13:24:36 -0400719 * If any of the exclusive bits are set, this will fail with -EEXIST if some
720 * part of the range already has the desired bits set. The start of the
721 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500722 *
Chris Mason1edbb732009-09-02 13:24:36 -0400723 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500724 */
Chris Mason1edbb732009-09-02 13:24:36 -0400725
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100726static int __must_check
727__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
728 int bits, int exclusive_bits, u64 *failed_start,
729 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500730{
731 struct extent_state *state;
732 struct extent_state *prealloc = NULL;
733 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500734 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500735 u64 last_start;
736 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400737
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400738 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500739again:
740 if (!prealloc && (mask & __GFP_WAIT)) {
741 prealloc = alloc_extent_state(mask);
Xiao Guangrong82337672011-04-20 06:44:57 +0000742 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500743 }
744
Chris Masoncad321a2008-12-17 14:51:42 -0500745 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400746 if (cached_state && *cached_state) {
747 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400748 if (state->start <= start && state->end > start &&
749 state->tree) {
Chris Mason9655d292009-09-02 15:22:30 -0400750 node = &state->rb_node;
751 goto hit_next;
752 }
753 }
Chris Masond1310b22008-01-24 16:13:08 -0500754 /*
755 * this search will find all the extents that end after
756 * our range starts.
757 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500758 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500759 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000760 prealloc = alloc_extent_state_atomic(prealloc);
761 BUG_ON(!prealloc);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400762 err = insert_state(tree, prealloc, start, end, &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400763 if (err)
764 extent_io_tree_panic(tree, err);
765
Chris Masond1310b22008-01-24 16:13:08 -0500766 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500767 goto out;
768 }
Chris Masond1310b22008-01-24 16:13:08 -0500769 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400770hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500771 last_start = state->start;
772 last_end = state->end;
773
774 /*
775 * | ---- desired range ---- |
776 * | state |
777 *
778 * Just lock what we found and keep going
779 */
780 if (state->start == start && state->end <= end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400781 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500782 *failed_start = state->start;
783 err = -EEXIST;
784 goto out;
785 }
Chris Mason42daec22009-09-23 19:51:09 -0400786
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000787 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400788 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500789 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400790 if (last_end == (u64)-1)
791 goto out;
792 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800793 state = next_state(state);
794 if (start < end && state && state->start == start &&
795 !need_resched())
796 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500797 goto search_again;
798 }
799
800 /*
801 * | ---- desired range ---- |
802 * | state |
803 * or
804 * | ------------- state -------------- |
805 *
806 * We need to split the extent we found, and may flip bits on
807 * second half.
808 *
809 * If the extent we found extends past our
810 * range, we just split and search again. It'll get split
811 * again the next time though.
812 *
813 * If the extent we found is inside our range, we set the
814 * desired bit on it.
815 */
816 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400817 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500818 *failed_start = start;
819 err = -EEXIST;
820 goto out;
821 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000822
823 prealloc = alloc_extent_state_atomic(prealloc);
824 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500825 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400826 if (err)
827 extent_io_tree_panic(tree, err);
828
Chris Masond1310b22008-01-24 16:13:08 -0500829 prealloc = NULL;
830 if (err)
831 goto out;
832 if (state->end <= end) {
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000833 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400834 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500835 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400836 if (last_end == (u64)-1)
837 goto out;
838 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800839 state = next_state(state);
840 if (start < end && state && state->start == start &&
841 !need_resched())
842 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500843 }
844 goto search_again;
845 }
846 /*
847 * | ---- desired range ---- |
848 * | state | or | state |
849 *
850 * There's a hole, we need to insert something in it and
851 * ignore the extent we found.
852 */
853 if (state->start > start) {
854 u64 this_end;
855 if (end < last_start)
856 this_end = end;
857 else
Chris Masond3977122009-01-05 21:25:51 -0500858 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000859
860 prealloc = alloc_extent_state_atomic(prealloc);
861 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000862
863 /*
864 * Avoid to free 'prealloc' if it can be merged with
865 * the later extent.
866 */
Chris Masond1310b22008-01-24 16:13:08 -0500867 err = insert_state(tree, prealloc, start, this_end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400868 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400869 if (err)
870 extent_io_tree_panic(tree, err);
871
Chris Mason2c64c532009-09-02 15:04:12 -0400872 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500873 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500874 start = this_end + 1;
875 goto search_again;
876 }
877 /*
878 * | ---- desired range ---- |
879 * | state |
880 * We need to split the extent, and set the bit
881 * on the first half
882 */
883 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400884 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500885 *failed_start = start;
886 err = -EEXIST;
887 goto out;
888 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000889
890 prealloc = alloc_extent_state_atomic(prealloc);
891 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500892 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400893 if (err)
894 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -0500895
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000896 set_state_bits(tree, prealloc, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400897 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500898 merge_state(tree, prealloc);
899 prealloc = NULL;
900 goto out;
901 }
902
903 goto search_again;
904
905out:
Chris Masoncad321a2008-12-17 14:51:42 -0500906 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500907 if (prealloc)
908 free_extent_state(prealloc);
909
910 return err;
911
912search_again:
913 if (start > end)
914 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500915 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500916 if (mask & __GFP_WAIT)
917 cond_resched();
918 goto again;
919}
Chris Masond1310b22008-01-24 16:13:08 -0500920
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100921int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits,
922 u64 *failed_start, struct extent_state **cached_state,
923 gfp_t mask)
924{
925 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
926 cached_state, mask);
927}
928
929
Josef Bacik462d6fa2011-09-26 13:56:12 -0400930/**
931 * convert_extent - convert all bits in a given range from one bit to another
932 * @tree: the io tree to search
933 * @start: the start offset in bytes
934 * @end: the end offset in bytes (inclusive)
935 * @bits: the bits to set in this range
936 * @clear_bits: the bits to clear in this range
937 * @mask: the allocation mask
938 *
939 * This will go through and set bits for the given range. If any states exist
940 * already in this range they are set with the given bit and cleared of the
941 * clear_bits. This is only meant to be used by things that are mergeable, ie
942 * converting from say DELALLOC to DIRTY. This is not meant to be used with
943 * boundary bits like LOCK.
944 */
945int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
946 int bits, int clear_bits, gfp_t mask)
947{
948 struct extent_state *state;
949 struct extent_state *prealloc = NULL;
950 struct rb_node *node;
951 int err = 0;
952 u64 last_start;
953 u64 last_end;
954
955again:
956 if (!prealloc && (mask & __GFP_WAIT)) {
957 prealloc = alloc_extent_state(mask);
958 if (!prealloc)
959 return -ENOMEM;
960 }
961
962 spin_lock(&tree->lock);
963 /*
964 * this search will find all the extents that end after
965 * our range starts.
966 */
967 node = tree_search(tree, start);
968 if (!node) {
969 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -0500970 if (!prealloc) {
971 err = -ENOMEM;
972 goto out;
973 }
Josef Bacik462d6fa2011-09-26 13:56:12 -0400974 err = insert_state(tree, prealloc, start, end, &bits);
975 prealloc = NULL;
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400976 if (err)
977 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -0400978 goto out;
979 }
980 state = rb_entry(node, struct extent_state, rb_node);
981hit_next:
982 last_start = state->start;
983 last_end = state->end;
984
985 /*
986 * | ---- desired range ---- |
987 * | state |
988 *
989 * Just lock what we found and keep going
990 */
991 if (state->start == start && state->end <= end) {
Josef Bacik462d6fa2011-09-26 13:56:12 -0400992 set_state_bits(tree, state, &bits);
Liu Bod1ac6e42012-05-10 18:10:39 +0800993 state = clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -0400994 if (last_end == (u64)-1)
995 goto out;
Josef Bacik462d6fa2011-09-26 13:56:12 -0400996 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +0800997 if (start < end && state && state->start == start &&
998 !need_resched())
999 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001000 goto search_again;
1001 }
1002
1003 /*
1004 * | ---- desired range ---- |
1005 * | state |
1006 * or
1007 * | ------------- state -------------- |
1008 *
1009 * We need to split the extent we found, and may flip bits on
1010 * second half.
1011 *
1012 * If the extent we found extends past our
1013 * range, we just split and search again. It'll get split
1014 * again the next time though.
1015 *
1016 * If the extent we found is inside our range, we set the
1017 * desired bit on it.
1018 */
1019 if (state->start < start) {
1020 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001021 if (!prealloc) {
1022 err = -ENOMEM;
1023 goto out;
1024 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001025 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001026 if (err)
1027 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001028 prealloc = NULL;
1029 if (err)
1030 goto out;
1031 if (state->end <= end) {
1032 set_state_bits(tree, state, &bits);
Liu Bod1ac6e42012-05-10 18:10:39 +08001033 state = clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001034 if (last_end == (u64)-1)
1035 goto out;
1036 start = last_end + 1;
Liu Bod1ac6e42012-05-10 18:10:39 +08001037 if (start < end && state && state->start == start &&
1038 !need_resched())
1039 goto hit_next;
Josef Bacik462d6fa2011-09-26 13:56:12 -04001040 }
1041 goto search_again;
1042 }
1043 /*
1044 * | ---- desired range ---- |
1045 * | state | or | state |
1046 *
1047 * There's a hole, we need to insert something in it and
1048 * ignore the extent we found.
1049 */
1050 if (state->start > start) {
1051 u64 this_end;
1052 if (end < last_start)
1053 this_end = end;
1054 else
1055 this_end = last_start - 1;
1056
1057 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001058 if (!prealloc) {
1059 err = -ENOMEM;
1060 goto out;
1061 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001062
1063 /*
1064 * Avoid to free 'prealloc' if it can be merged with
1065 * the later extent.
1066 */
1067 err = insert_state(tree, prealloc, start, this_end,
1068 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001069 if (err)
1070 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001071 prealloc = NULL;
1072 start = this_end + 1;
1073 goto search_again;
1074 }
1075 /*
1076 * | ---- desired range ---- |
1077 * | state |
1078 * We need to split the extent, and set the bit
1079 * on the first half
1080 */
1081 if (state->start <= end && state->end > end) {
1082 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001083 if (!prealloc) {
1084 err = -ENOMEM;
1085 goto out;
1086 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001087
1088 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001089 if (err)
1090 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001091
1092 set_state_bits(tree, prealloc, &bits);
1093 clear_state_bit(tree, prealloc, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001094 prealloc = NULL;
1095 goto out;
1096 }
1097
1098 goto search_again;
1099
1100out:
1101 spin_unlock(&tree->lock);
1102 if (prealloc)
1103 free_extent_state(prealloc);
1104
1105 return err;
1106
1107search_again:
1108 if (start > end)
1109 goto out;
1110 spin_unlock(&tree->lock);
1111 if (mask & __GFP_WAIT)
1112 cond_resched();
1113 goto again;
1114}
1115
Chris Masond1310b22008-01-24 16:13:08 -05001116/* wrappers around set/clear extent bit */
1117int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1118 gfp_t mask)
1119{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001120 return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001121 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001122}
Chris Masond1310b22008-01-24 16:13:08 -05001123
1124int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1125 int bits, gfp_t mask)
1126{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001127 return set_extent_bit(tree, start, end, bits, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001128 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001129}
Chris Masond1310b22008-01-24 16:13:08 -05001130
1131int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1132 int bits, gfp_t mask)
1133{
Chris Mason2c64c532009-09-02 15:04:12 -04001134 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001135}
Chris Masond1310b22008-01-24 16:13:08 -05001136
1137int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001138 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001139{
1140 return set_extent_bit(tree, start, end,
Liu Bofee187d2011-09-29 15:55:28 +08001141 EXTENT_DELALLOC | EXTENT_UPTODATE,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001142 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001143}
Chris Masond1310b22008-01-24 16:13:08 -05001144
1145int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1146 gfp_t mask)
1147{
1148 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04001149 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001150 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001151}
Chris Masond1310b22008-01-24 16:13:08 -05001152
1153int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1154 gfp_t mask)
1155{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001156 return set_extent_bit(tree, start, end, EXTENT_NEW, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001157 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001158}
Chris Masond1310b22008-01-24 16:13:08 -05001159
Chris Masond1310b22008-01-24 16:13:08 -05001160int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +00001161 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001162{
Arne Jansen507903b2011-04-06 10:02:20 +00001163 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001164 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001165}
Chris Masond1310b22008-01-24 16:13:08 -05001166
Josef Bacik5fd02042012-05-02 14:00:54 -04001167int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
1168 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001169{
Chris Mason2c64c532009-09-02 15:04:12 -04001170 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001171 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001172}
Chris Masond1310b22008-01-24 16:13:08 -05001173
Chris Masond352ac62008-09-29 15:18:18 -04001174/*
1175 * either insert or lock state struct between start and end use mask to tell
1176 * us if waiting is desired.
1177 */
Chris Mason1edbb732009-09-02 13:24:36 -04001178int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001179 int bits, struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001180{
1181 int err;
1182 u64 failed_start;
1183 while (1) {
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001184 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
1185 EXTENT_LOCKED, &failed_start,
1186 cached_state, GFP_NOFS);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001187 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001188 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1189 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001190 } else
Chris Masond1310b22008-01-24 16:13:08 -05001191 break;
Chris Masond1310b22008-01-24 16:13:08 -05001192 WARN_ON(start > end);
1193 }
1194 return err;
1195}
Chris Masond1310b22008-01-24 16:13:08 -05001196
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001197int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Mason1edbb732009-09-02 13:24:36 -04001198{
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001199 return lock_extent_bits(tree, start, end, 0, NULL);
Chris Mason1edbb732009-09-02 13:24:36 -04001200}
1201
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001202int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001203{
1204 int err;
1205 u64 failed_start;
1206
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001207 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1208 &failed_start, NULL, GFP_NOFS);
Yan Zheng66435582008-10-30 14:19:50 -04001209 if (err == -EEXIST) {
1210 if (failed_start > start)
1211 clear_extent_bit(tree, start, failed_start - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001212 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
Josef Bacik25179202008-10-29 14:49:05 -04001213 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001214 }
Josef Bacik25179202008-10-29 14:49:05 -04001215 return 1;
1216}
Josef Bacik25179202008-10-29 14:49:05 -04001217
Chris Mason2c64c532009-09-02 15:04:12 -04001218int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1219 struct extent_state **cached, gfp_t mask)
1220{
1221 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1222 mask);
1223}
1224
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001225int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001226{
Chris Mason2c64c532009-09-02 15:04:12 -04001227 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001228 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001229}
Chris Masond1310b22008-01-24 16:13:08 -05001230
1231/*
Chris Masond1310b22008-01-24 16:13:08 -05001232 * helper function to set both pages and extents in the tree writeback
1233 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001234static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001235{
1236 unsigned long index = start >> PAGE_CACHE_SHIFT;
1237 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1238 struct page *page;
1239
1240 while (index <= end_index) {
1241 page = find_get_page(tree->mapping, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001242 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Masond1310b22008-01-24 16:13:08 -05001243 set_page_writeback(page);
1244 page_cache_release(page);
1245 index++;
1246 }
Chris Masond1310b22008-01-24 16:13:08 -05001247 return 0;
1248}
Chris Masond1310b22008-01-24 16:13:08 -05001249
Chris Masond352ac62008-09-29 15:18:18 -04001250/* find the first state struct with 'bits' set after 'start', and
1251 * return it. tree->lock must be held. NULL will returned if
1252 * nothing was found after 'start'
1253 */
Chris Masond7fc6402008-02-18 12:12:38 -05001254struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1255 u64 start, int bits)
1256{
1257 struct rb_node *node;
1258 struct extent_state *state;
1259
1260 /*
1261 * this search will find all the extents that end after
1262 * our range starts.
1263 */
1264 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001265 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001266 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001267
Chris Masond3977122009-01-05 21:25:51 -05001268 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001269 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001270 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001271 return state;
Chris Masond3977122009-01-05 21:25:51 -05001272
Chris Masond7fc6402008-02-18 12:12:38 -05001273 node = rb_next(node);
1274 if (!node)
1275 break;
1276 }
1277out:
1278 return NULL;
1279}
Chris Masond7fc6402008-02-18 12:12:38 -05001280
Chris Masond352ac62008-09-29 15:18:18 -04001281/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001282 * find the first offset in the io tree with 'bits' set. zero is
1283 * returned if we find something, and *start_ret and *end_ret are
1284 * set to reflect the state struct that was found.
1285 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001286 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001287 */
1288int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1289 u64 *start_ret, u64 *end_ret, int bits)
1290{
1291 struct extent_state *state;
1292 int ret = 1;
1293
1294 spin_lock(&tree->lock);
1295 state = find_first_extent_bit_state(tree, start, bits);
1296 if (state) {
1297 *start_ret = state->start;
1298 *end_ret = state->end;
1299 ret = 0;
1300 }
1301 spin_unlock(&tree->lock);
1302 return ret;
1303}
1304
1305/*
Chris Masond352ac62008-09-29 15:18:18 -04001306 * find a contiguous range of bytes in the file marked as delalloc, not
1307 * more than 'max_bytes'. start and end are used to return the range,
1308 *
1309 * 1 is returned if we find something, 0 if nothing was in the tree
1310 */
Chris Masonc8b97812008-10-29 14:49:59 -04001311static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001312 u64 *start, u64 *end, u64 max_bytes,
1313 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001314{
1315 struct rb_node *node;
1316 struct extent_state *state;
1317 u64 cur_start = *start;
1318 u64 found = 0;
1319 u64 total_bytes = 0;
1320
Chris Masoncad321a2008-12-17 14:51:42 -05001321 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001322
Chris Masond1310b22008-01-24 16:13:08 -05001323 /*
1324 * this search will find all the extents that end after
1325 * our range starts.
1326 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001327 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001328 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001329 if (!found)
1330 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001331 goto out;
1332 }
1333
Chris Masond3977122009-01-05 21:25:51 -05001334 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001335 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001336 if (found && (state->start != cur_start ||
1337 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001338 goto out;
1339 }
1340 if (!(state->state & EXTENT_DELALLOC)) {
1341 if (!found)
1342 *end = state->end;
1343 goto out;
1344 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001345 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001346 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001347 *cached_state = state;
1348 atomic_inc(&state->refs);
1349 }
Chris Masond1310b22008-01-24 16:13:08 -05001350 found++;
1351 *end = state->end;
1352 cur_start = state->end + 1;
1353 node = rb_next(node);
1354 if (!node)
1355 break;
1356 total_bytes += state->end - state->start + 1;
1357 if (total_bytes >= max_bytes)
1358 break;
1359 }
1360out:
Chris Masoncad321a2008-12-17 14:51:42 -05001361 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001362 return found;
1363}
1364
Jeff Mahoney143bede2012-03-01 14:56:26 +01001365static noinline void __unlock_for_delalloc(struct inode *inode,
1366 struct page *locked_page,
1367 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001368{
1369 int ret;
1370 struct page *pages[16];
1371 unsigned long index = start >> PAGE_CACHE_SHIFT;
1372 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1373 unsigned long nr_pages = end_index - index + 1;
1374 int i;
1375
1376 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001377 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001378
Chris Masond3977122009-01-05 21:25:51 -05001379 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001380 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001381 min_t(unsigned long, nr_pages,
1382 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001383 for (i = 0; i < ret; i++) {
1384 if (pages[i] != locked_page)
1385 unlock_page(pages[i]);
1386 page_cache_release(pages[i]);
1387 }
1388 nr_pages -= ret;
1389 index += ret;
1390 cond_resched();
1391 }
Chris Masonc8b97812008-10-29 14:49:59 -04001392}
1393
1394static noinline int lock_delalloc_pages(struct inode *inode,
1395 struct page *locked_page,
1396 u64 delalloc_start,
1397 u64 delalloc_end)
1398{
1399 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1400 unsigned long start_index = index;
1401 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1402 unsigned long pages_locked = 0;
1403 struct page *pages[16];
1404 unsigned long nrpages;
1405 int ret;
1406 int i;
1407
1408 /* the caller is responsible for locking the start index */
1409 if (index == locked_page->index && index == end_index)
1410 return 0;
1411
1412 /* skip the page at the start index */
1413 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001414 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001415 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001416 min_t(unsigned long,
1417 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001418 if (ret == 0) {
1419 ret = -EAGAIN;
1420 goto done;
1421 }
1422 /* now we have an array of pages, lock them all */
1423 for (i = 0; i < ret; i++) {
1424 /*
1425 * the caller is taking responsibility for
1426 * locked_page
1427 */
Chris Mason771ed682008-11-06 22:02:51 -05001428 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001429 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001430 if (!PageDirty(pages[i]) ||
1431 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001432 ret = -EAGAIN;
1433 unlock_page(pages[i]);
1434 page_cache_release(pages[i]);
1435 goto done;
1436 }
1437 }
Chris Masonc8b97812008-10-29 14:49:59 -04001438 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001439 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001440 }
Chris Masonc8b97812008-10-29 14:49:59 -04001441 nrpages -= ret;
1442 index += ret;
1443 cond_resched();
1444 }
1445 ret = 0;
1446done:
1447 if (ret && pages_locked) {
1448 __unlock_for_delalloc(inode, locked_page,
1449 delalloc_start,
1450 ((u64)(start_index + pages_locked - 1)) <<
1451 PAGE_CACHE_SHIFT);
1452 }
1453 return ret;
1454}
1455
1456/*
1457 * find a contiguous range of bytes in the file marked as delalloc, not
1458 * more than 'max_bytes'. start and end are used to return the range,
1459 *
1460 * 1 is returned if we find something, 0 if nothing was in the tree
1461 */
1462static noinline u64 find_lock_delalloc_range(struct inode *inode,
1463 struct extent_io_tree *tree,
1464 struct page *locked_page,
1465 u64 *start, u64 *end,
1466 u64 max_bytes)
1467{
1468 u64 delalloc_start;
1469 u64 delalloc_end;
1470 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001471 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001472 int ret;
1473 int loops = 0;
1474
1475again:
1476 /* step one, find a bunch of delalloc bytes starting at start */
1477 delalloc_start = *start;
1478 delalloc_end = 0;
1479 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001480 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001481 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001482 *start = delalloc_start;
1483 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001484 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001485 return found;
1486 }
1487
1488 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001489 * start comes from the offset of locked_page. We have to lock
1490 * pages in order, so we can't process delalloc bytes before
1491 * locked_page
1492 */
Chris Masond3977122009-01-05 21:25:51 -05001493 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001494 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001495
1496 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001497 * make sure to limit the number of pages we try to lock down
1498 * if we're looping.
1499 */
Chris Masond3977122009-01-05 21:25:51 -05001500 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001501 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001502
Chris Masonc8b97812008-10-29 14:49:59 -04001503 /* step two, lock all the pages after the page that has start */
1504 ret = lock_delalloc_pages(inode, locked_page,
1505 delalloc_start, delalloc_end);
1506 if (ret == -EAGAIN) {
1507 /* some of the pages are gone, lets avoid looping by
1508 * shortening the size of the delalloc range we're searching
1509 */
Chris Mason9655d292009-09-02 15:22:30 -04001510 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001511 if (!loops) {
1512 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1513 max_bytes = PAGE_CACHE_SIZE - offset;
1514 loops = 1;
1515 goto again;
1516 } else {
1517 found = 0;
1518 goto out_failed;
1519 }
1520 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001521 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
Chris Masonc8b97812008-10-29 14:49:59 -04001522
1523 /* step three, lock the state bits for the whole range */
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001524 lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001525
1526 /* then test to make sure it is all still delalloc */
1527 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001528 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001529 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001530 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1531 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001532 __unlock_for_delalloc(inode, locked_page,
1533 delalloc_start, delalloc_end);
1534 cond_resched();
1535 goto again;
1536 }
Chris Mason9655d292009-09-02 15:22:30 -04001537 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001538 *start = delalloc_start;
1539 *end = delalloc_end;
1540out_failed:
1541 return found;
1542}
1543
1544int extent_clear_unlock_delalloc(struct inode *inode,
1545 struct extent_io_tree *tree,
1546 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001547 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001548{
1549 int ret;
1550 struct page *pages[16];
1551 unsigned long index = start >> PAGE_CACHE_SHIFT;
1552 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1553 unsigned long nr_pages = end_index - index + 1;
1554 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001555 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001556
Chris Masona791e352009-10-08 11:27:10 -04001557 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001558 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001559 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001560 clear_bits |= EXTENT_DIRTY;
1561
Chris Masona791e352009-10-08 11:27:10 -04001562 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001563 clear_bits |= EXTENT_DELALLOC;
1564
Chris Mason2c64c532009-09-02 15:04:12 -04001565 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001566 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1567 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1568 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001569 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001570
Chris Masond3977122009-01-05 21:25:51 -05001571 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001572 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001573 min_t(unsigned long,
1574 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001575 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001576
Chris Masona791e352009-10-08 11:27:10 -04001577 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001578 SetPagePrivate2(pages[i]);
1579
Chris Masonc8b97812008-10-29 14:49:59 -04001580 if (pages[i] == locked_page) {
1581 page_cache_release(pages[i]);
1582 continue;
1583 }
Chris Masona791e352009-10-08 11:27:10 -04001584 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001585 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001586 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001587 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001588 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001589 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001590 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001591 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001592 page_cache_release(pages[i]);
1593 }
1594 nr_pages -= ret;
1595 index += ret;
1596 cond_resched();
1597 }
1598 return 0;
1599}
Chris Masonc8b97812008-10-29 14:49:59 -04001600
Chris Masond352ac62008-09-29 15:18:18 -04001601/*
1602 * count the number of bytes in the tree that have a given bit(s)
1603 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1604 * cached. The total number found is returned.
1605 */
Chris Masond1310b22008-01-24 16:13:08 -05001606u64 count_range_bits(struct extent_io_tree *tree,
1607 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001608 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001609{
1610 struct rb_node *node;
1611 struct extent_state *state;
1612 u64 cur_start = *start;
1613 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001614 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001615 int found = 0;
1616
1617 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001618 WARN_ON(1);
1619 return 0;
1620 }
1621
Chris Masoncad321a2008-12-17 14:51:42 -05001622 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001623 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1624 total_bytes = tree->dirty_bytes;
1625 goto out;
1626 }
1627 /*
1628 * this search will find all the extents that end after
1629 * our range starts.
1630 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001631 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001632 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001633 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001634
Chris Masond3977122009-01-05 21:25:51 -05001635 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001636 state = rb_entry(node, struct extent_state, rb_node);
1637 if (state->start > search_end)
1638 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001639 if (contig && found && state->start > last + 1)
1640 break;
1641 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001642 total_bytes += min(search_end, state->end) + 1 -
1643 max(cur_start, state->start);
1644 if (total_bytes >= max_bytes)
1645 break;
1646 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001647 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001648 found = 1;
1649 }
Chris Masonec29ed52011-02-23 16:23:20 -05001650 last = state->end;
1651 } else if (contig && found) {
1652 break;
Chris Masond1310b22008-01-24 16:13:08 -05001653 }
1654 node = rb_next(node);
1655 if (!node)
1656 break;
1657 }
1658out:
Chris Masoncad321a2008-12-17 14:51:42 -05001659 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001660 return total_bytes;
1661}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001662
Chris Masond352ac62008-09-29 15:18:18 -04001663/*
1664 * set the private field for a given byte offset in the tree. If there isn't
1665 * an extent_state there already, this does nothing.
1666 */
Chris Masond1310b22008-01-24 16:13:08 -05001667int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1668{
1669 struct rb_node *node;
1670 struct extent_state *state;
1671 int ret = 0;
1672
Chris Masoncad321a2008-12-17 14:51:42 -05001673 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001674 /*
1675 * this search will find all the extents that end after
1676 * our range starts.
1677 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001678 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001679 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001680 ret = -ENOENT;
1681 goto out;
1682 }
1683 state = rb_entry(node, struct extent_state, rb_node);
1684 if (state->start != start) {
1685 ret = -ENOENT;
1686 goto out;
1687 }
1688 state->private = private;
1689out:
Chris Masoncad321a2008-12-17 14:51:42 -05001690 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001691 return ret;
1692}
1693
1694int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1695{
1696 struct rb_node *node;
1697 struct extent_state *state;
1698 int ret = 0;
1699
Chris Masoncad321a2008-12-17 14:51:42 -05001700 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001701 /*
1702 * this search will find all the extents that end after
1703 * our range starts.
1704 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001705 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001706 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001707 ret = -ENOENT;
1708 goto out;
1709 }
1710 state = rb_entry(node, struct extent_state, rb_node);
1711 if (state->start != start) {
1712 ret = -ENOENT;
1713 goto out;
1714 }
1715 *private = state->private;
1716out:
Chris Masoncad321a2008-12-17 14:51:42 -05001717 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001718 return ret;
1719}
1720
1721/*
1722 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001723 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001724 * has the bits set. Otherwise, 1 is returned if any bit in the
1725 * range is found set.
1726 */
1727int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001728 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001729{
1730 struct extent_state *state = NULL;
1731 struct rb_node *node;
1732 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001733
Chris Masoncad321a2008-12-17 14:51:42 -05001734 spin_lock(&tree->lock);
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001735 if (cached && cached->tree && cached->start <= start &&
1736 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001737 node = &cached->rb_node;
1738 else
1739 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001740 while (node && start <= end) {
1741 state = rb_entry(node, struct extent_state, rb_node);
1742
1743 if (filled && state->start > start) {
1744 bitset = 0;
1745 break;
1746 }
1747
1748 if (state->start > end)
1749 break;
1750
1751 if (state->state & bits) {
1752 bitset = 1;
1753 if (!filled)
1754 break;
1755 } else if (filled) {
1756 bitset = 0;
1757 break;
1758 }
Chris Mason46562cec2009-09-23 20:23:16 -04001759
1760 if (state->end == (u64)-1)
1761 break;
1762
Chris Masond1310b22008-01-24 16:13:08 -05001763 start = state->end + 1;
1764 if (start > end)
1765 break;
1766 node = rb_next(node);
1767 if (!node) {
1768 if (filled)
1769 bitset = 0;
1770 break;
1771 }
1772 }
Chris Masoncad321a2008-12-17 14:51:42 -05001773 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001774 return bitset;
1775}
Chris Masond1310b22008-01-24 16:13:08 -05001776
1777/*
1778 * helper function to set a given page up to date if all the
1779 * extents in the tree for that page are up to date
1780 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001781static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001782{
1783 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1784 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001785 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001786 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001787}
1788
1789/*
1790 * helper function to unlock a page if all the extents in the tree
1791 * for that page are unlocked
1792 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001793static void check_page_locked(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001794{
1795 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1796 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001797 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001798 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05001799}
1800
1801/*
1802 * helper function to end page writeback if all the extents
1803 * in the tree for that page are done with writeback
1804 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001805static void check_page_writeback(struct extent_io_tree *tree,
1806 struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001807{
Chris Mason1edbb732009-09-02 13:24:36 -04001808 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001809}
1810
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001811/*
1812 * When IO fails, either with EIO or csum verification fails, we
1813 * try other mirrors that might have a good copy of the data. This
1814 * io_failure_record is used to record state as we go through all the
1815 * mirrors. If another mirror has good data, the page is set up to date
1816 * and things continue. If a good mirror can't be found, the original
1817 * bio end_io callback is called to indicate things have failed.
1818 */
1819struct io_failure_record {
1820 struct page *page;
1821 u64 start;
1822 u64 len;
1823 u64 logical;
1824 unsigned long bio_flags;
1825 int this_mirror;
1826 int failed_mirror;
1827 int in_validation;
1828};
1829
1830static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
1831 int did_repair)
1832{
1833 int ret;
1834 int err = 0;
1835 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1836
1837 set_state_private(failure_tree, rec->start, 0);
1838 ret = clear_extent_bits(failure_tree, rec->start,
1839 rec->start + rec->len - 1,
1840 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1841 if (ret)
1842 err = ret;
1843
1844 if (did_repair) {
1845 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1846 rec->start + rec->len - 1,
1847 EXTENT_DAMAGED, GFP_NOFS);
1848 if (ret && !err)
1849 err = ret;
1850 }
1851
1852 kfree(rec);
1853 return err;
1854}
1855
1856static void repair_io_failure_callback(struct bio *bio, int err)
1857{
1858 complete(bio->bi_private);
1859}
1860
1861/*
1862 * this bypasses the standard btrfs submit functions deliberately, as
1863 * the standard behavior is to write all copies in a raid setup. here we only
1864 * want to write the one bad copy. so we do the mapping for ourselves and issue
1865 * submit_bio directly.
1866 * to avoid any synchonization issues, wait for the data after writing, which
1867 * actually prevents the read that triggered the error from finishing.
1868 * currently, there can be no more than two copies of every data bit. thus,
1869 * exactly one rewrite is required.
1870 */
1871int repair_io_failure(struct btrfs_mapping_tree *map_tree, u64 start,
1872 u64 length, u64 logical, struct page *page,
1873 int mirror_num)
1874{
1875 struct bio *bio;
1876 struct btrfs_device *dev;
1877 DECLARE_COMPLETION_ONSTACK(compl);
1878 u64 map_length = 0;
1879 u64 sector;
1880 struct btrfs_bio *bbio = NULL;
1881 int ret;
1882
1883 BUG_ON(!mirror_num);
1884
1885 bio = bio_alloc(GFP_NOFS, 1);
1886 if (!bio)
1887 return -EIO;
1888 bio->bi_private = &compl;
1889 bio->bi_end_io = repair_io_failure_callback;
1890 bio->bi_size = 0;
1891 map_length = length;
1892
1893 ret = btrfs_map_block(map_tree, WRITE, logical,
1894 &map_length, &bbio, mirror_num);
1895 if (ret) {
1896 bio_put(bio);
1897 return -EIO;
1898 }
1899 BUG_ON(mirror_num != bbio->mirror_num);
1900 sector = bbio->stripes[mirror_num-1].physical >> 9;
1901 bio->bi_sector = sector;
1902 dev = bbio->stripes[mirror_num-1].dev;
1903 kfree(bbio);
1904 if (!dev || !dev->bdev || !dev->writeable) {
1905 bio_put(bio);
1906 return -EIO;
1907 }
1908 bio->bi_bdev = dev->bdev;
1909 bio_add_page(bio, page, length, start-page_offset(page));
Stefan Behrens21adbd52011-11-09 13:44:05 +01001910 btrfsic_submit_bio(WRITE_SYNC, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001911 wait_for_completion(&compl);
1912
1913 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
1914 /* try to remap that extent elsewhere? */
1915 bio_put(bio);
Stefan Behrens442a4f62012-05-25 16:06:08 +02001916 btrfs_dev_stat_inc_and_print(dev, BTRFS_DEV_STAT_WRITE_ERRS);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001917 return -EIO;
1918 }
1919
1920 printk(KERN_INFO "btrfs read error corrected: ino %lu off %llu (dev %s "
1921 "sector %llu)\n", page->mapping->host->i_ino, start,
1922 dev->name, sector);
1923
1924 bio_put(bio);
1925 return 0;
1926}
1927
Josef Bacikea466792012-03-26 21:57:36 -04001928int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
1929 int mirror_num)
1930{
1931 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1932 u64 start = eb->start;
1933 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond95603b2012-04-12 15:55:15 -04001934 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04001935
1936 for (i = 0; i < num_pages; i++) {
1937 struct page *p = extent_buffer_page(eb, i);
1938 ret = repair_io_failure(map_tree, start, PAGE_CACHE_SIZE,
1939 start, p, mirror_num);
1940 if (ret)
1941 break;
1942 start += PAGE_CACHE_SIZE;
1943 }
1944
1945 return ret;
1946}
1947
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001948/*
1949 * each time an IO finishes, we do a fast check in the IO failure tree
1950 * to see if we need to process or clean up an io_failure_record
1951 */
1952static int clean_io_failure(u64 start, struct page *page)
1953{
1954 u64 private;
1955 u64 private_failure;
1956 struct io_failure_record *failrec;
1957 struct btrfs_mapping_tree *map_tree;
1958 struct extent_state *state;
1959 int num_copies;
1960 int did_repair = 0;
1961 int ret;
1962 struct inode *inode = page->mapping->host;
1963
1964 private = 0;
1965 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1966 (u64)-1, 1, EXTENT_DIRTY, 0);
1967 if (!ret)
1968 return 0;
1969
1970 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
1971 &private_failure);
1972 if (ret)
1973 return 0;
1974
1975 failrec = (struct io_failure_record *)(unsigned long) private_failure;
1976 BUG_ON(!failrec->this_mirror);
1977
1978 if (failrec->in_validation) {
1979 /* there was no real error, just free the record */
1980 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
1981 failrec->start);
1982 did_repair = 1;
1983 goto out;
1984 }
1985
1986 spin_lock(&BTRFS_I(inode)->io_tree.lock);
1987 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1988 failrec->start,
1989 EXTENT_LOCKED);
1990 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
1991
1992 if (state && state->start == failrec->start) {
1993 map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree;
1994 num_copies = btrfs_num_copies(map_tree, failrec->logical,
1995 failrec->len);
1996 if (num_copies > 1) {
1997 ret = repair_io_failure(map_tree, start, failrec->len,
1998 failrec->logical, page,
1999 failrec->failed_mirror);
2000 did_repair = !ret;
2001 }
2002 }
2003
2004out:
2005 if (!ret)
2006 ret = free_io_failure(inode, failrec, did_repair);
2007
2008 return ret;
2009}
2010
2011/*
2012 * this is a generic handler for readpage errors (default
2013 * readpage_io_failed_hook). if other copies exist, read those and write back
2014 * good data to the failed position. does not investigate in remapping the
2015 * failed extent elsewhere, hoping the device will be smart enough to do this as
2016 * needed
2017 */
2018
2019static int bio_readpage_error(struct bio *failed_bio, struct page *page,
2020 u64 start, u64 end, int failed_mirror,
2021 struct extent_state *state)
2022{
2023 struct io_failure_record *failrec = NULL;
2024 u64 private;
2025 struct extent_map *em;
2026 struct inode *inode = page->mapping->host;
2027 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2028 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2029 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2030 struct bio *bio;
2031 int num_copies;
2032 int ret;
2033 int read_mode;
2034 u64 logical;
2035
2036 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2037
2038 ret = get_state_private(failure_tree, start, &private);
2039 if (ret) {
2040 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2041 if (!failrec)
2042 return -ENOMEM;
2043 failrec->start = start;
2044 failrec->len = end - start + 1;
2045 failrec->this_mirror = 0;
2046 failrec->bio_flags = 0;
2047 failrec->in_validation = 0;
2048
2049 read_lock(&em_tree->lock);
2050 em = lookup_extent_mapping(em_tree, start, failrec->len);
2051 if (!em) {
2052 read_unlock(&em_tree->lock);
2053 kfree(failrec);
2054 return -EIO;
2055 }
2056
2057 if (em->start > start || em->start + em->len < start) {
2058 free_extent_map(em);
2059 em = NULL;
2060 }
2061 read_unlock(&em_tree->lock);
2062
2063 if (!em || IS_ERR(em)) {
2064 kfree(failrec);
2065 return -EIO;
2066 }
2067 logical = start - em->start;
2068 logical = em->block_start + logical;
2069 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2070 logical = em->block_start;
2071 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2072 extent_set_compress_type(&failrec->bio_flags,
2073 em->compress_type);
2074 }
2075 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2076 "len=%llu\n", logical, start, failrec->len);
2077 failrec->logical = logical;
2078 free_extent_map(em);
2079
2080 /* set the bits in the private failure tree */
2081 ret = set_extent_bits(failure_tree, start, end,
2082 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2083 if (ret >= 0)
2084 ret = set_state_private(failure_tree, start,
2085 (u64)(unsigned long)failrec);
2086 /* set the bits in the inode's tree */
2087 if (ret >= 0)
2088 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2089 GFP_NOFS);
2090 if (ret < 0) {
2091 kfree(failrec);
2092 return ret;
2093 }
2094 } else {
2095 failrec = (struct io_failure_record *)(unsigned long)private;
2096 pr_debug("bio_readpage_error: (found) logical=%llu, "
2097 "start=%llu, len=%llu, validation=%d\n",
2098 failrec->logical, failrec->start, failrec->len,
2099 failrec->in_validation);
2100 /*
2101 * when data can be on disk more than twice, add to failrec here
2102 * (e.g. with a list for failed_mirror) to make
2103 * clean_io_failure() clean all those errors at once.
2104 */
2105 }
2106 num_copies = btrfs_num_copies(
2107 &BTRFS_I(inode)->root->fs_info->mapping_tree,
2108 failrec->logical, failrec->len);
2109 if (num_copies == 1) {
2110 /*
2111 * we only have a single copy of the data, so don't bother with
2112 * all the retry and error correction code that follows. no
2113 * matter what the error is, it is very likely to persist.
2114 */
2115 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2116 "state=%p, num_copies=%d, next_mirror %d, "
2117 "failed_mirror %d\n", state, num_copies,
2118 failrec->this_mirror, failed_mirror);
2119 free_io_failure(inode, failrec, 0);
2120 return -EIO;
2121 }
2122
2123 if (!state) {
2124 spin_lock(&tree->lock);
2125 state = find_first_extent_bit_state(tree, failrec->start,
2126 EXTENT_LOCKED);
2127 if (state && state->start != failrec->start)
2128 state = NULL;
2129 spin_unlock(&tree->lock);
2130 }
2131
2132 /*
2133 * there are two premises:
2134 * a) deliver good data to the caller
2135 * b) correct the bad sectors on disk
2136 */
2137 if (failed_bio->bi_vcnt > 1) {
2138 /*
2139 * to fulfill b), we need to know the exact failing sectors, as
2140 * we don't want to rewrite any more than the failed ones. thus,
2141 * we need separate read requests for the failed bio
2142 *
2143 * if the following BUG_ON triggers, our validation request got
2144 * merged. we need separate requests for our algorithm to work.
2145 */
2146 BUG_ON(failrec->in_validation);
2147 failrec->in_validation = 1;
2148 failrec->this_mirror = failed_mirror;
2149 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2150 } else {
2151 /*
2152 * we're ready to fulfill a) and b) alongside. get a good copy
2153 * of the failed sector and if we succeed, we have setup
2154 * everything for repair_io_failure to do the rest for us.
2155 */
2156 if (failrec->in_validation) {
2157 BUG_ON(failrec->this_mirror != failed_mirror);
2158 failrec->in_validation = 0;
2159 failrec->this_mirror = 0;
2160 }
2161 failrec->failed_mirror = failed_mirror;
2162 failrec->this_mirror++;
2163 if (failrec->this_mirror == failed_mirror)
2164 failrec->this_mirror++;
2165 read_mode = READ_SYNC;
2166 }
2167
2168 if (!state || failrec->this_mirror > num_copies) {
2169 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2170 "next_mirror %d, failed_mirror %d\n", state,
2171 num_copies, failrec->this_mirror, failed_mirror);
2172 free_io_failure(inode, failrec, 0);
2173 return -EIO;
2174 }
2175
2176 bio = bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04002177 if (!bio) {
2178 free_io_failure(inode, failrec, 0);
2179 return -EIO;
2180 }
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002181 bio->bi_private = state;
2182 bio->bi_end_io = failed_bio->bi_end_io;
2183 bio->bi_sector = failrec->logical >> 9;
2184 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2185 bio->bi_size = 0;
2186
2187 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2188
2189 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2190 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2191 failrec->this_mirror, num_copies, failrec->in_validation);
2192
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002193 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2194 failrec->this_mirror,
2195 failrec->bio_flags, 0);
2196 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002197}
2198
Chris Masond1310b22008-01-24 16:13:08 -05002199/* lots and lots of room for performance fixes in the end_bio funcs */
2200
Jeff Mahoney87826df2012-02-15 16:23:57 +01002201int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2202{
2203 int uptodate = (err == 0);
2204 struct extent_io_tree *tree;
2205 int ret;
2206
2207 tree = &BTRFS_I(page->mapping->host)->io_tree;
2208
2209 if (tree->ops && tree->ops->writepage_end_io_hook) {
2210 ret = tree->ops->writepage_end_io_hook(page, start,
2211 end, NULL, uptodate);
2212 if (ret)
2213 uptodate = 0;
2214 }
2215
Jeff Mahoney87826df2012-02-15 16:23:57 +01002216 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002217 ClearPageUptodate(page);
2218 SetPageError(page);
2219 }
2220 return 0;
2221}
2222
Chris Masond1310b22008-01-24 16:13:08 -05002223/*
2224 * after a writepage IO is done, we need to:
2225 * clear the uptodate bits on error
2226 * clear the writeback bits in the extent tree for this IO
2227 * end_page_writeback if the page has no more pending IO
2228 *
2229 * Scheduling is not allowed, so the extent state tree is expected
2230 * to have one and only one object corresponding to this IO.
2231 */
Chris Masond1310b22008-01-24 16:13:08 -05002232static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002233{
Chris Masond1310b22008-01-24 16:13:08 -05002234 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04002235 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002236 u64 start;
2237 u64 end;
2238 int whole_page;
2239
Chris Masond1310b22008-01-24 16:13:08 -05002240 do {
2241 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04002242 tree = &BTRFS_I(page->mapping->host)->io_tree;
2243
Chris Masond1310b22008-01-24 16:13:08 -05002244 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2245 bvec->bv_offset;
2246 end = start + bvec->bv_len - 1;
2247
2248 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2249 whole_page = 1;
2250 else
2251 whole_page = 0;
2252
2253 if (--bvec >= bio->bi_io_vec)
2254 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04002255
Jeff Mahoney87826df2012-02-15 16:23:57 +01002256 if (end_extent_writepage(page, err, start, end))
2257 continue;
Chris Mason70dec802008-01-29 09:59:12 -05002258
Chris Masond1310b22008-01-24 16:13:08 -05002259 if (whole_page)
2260 end_page_writeback(page);
2261 else
2262 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002263 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04002264
Chris Masond1310b22008-01-24 16:13:08 -05002265 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002266}
2267
2268/*
2269 * after a readpage IO is done, we need to:
2270 * clear the uptodate bits on error
2271 * set the uptodate bits if things worked
2272 * set the page up to date if all extents in the tree are uptodate
2273 * clear the lock bit in the extent tree
2274 * unlock the page if there are no other extents locked for it
2275 *
2276 * Scheduling is not allowed, so the extent state tree is expected
2277 * to have one and only one object corresponding to this IO.
2278 */
Chris Masond1310b22008-01-24 16:13:08 -05002279static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002280{
2281 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00002282 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2283 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04002284 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002285 u64 start;
2286 u64 end;
2287 int whole_page;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002288 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002289 int ret;
2290
Chris Masond20f7042008-12-08 16:58:54 -05002291 if (err)
2292 uptodate = 0;
2293
Chris Masond1310b22008-01-24 16:13:08 -05002294 do {
2295 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00002296 struct extent_state *cached = NULL;
2297 struct extent_state *state;
2298
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002299 pr_debug("end_bio_extent_readpage: bi_vcnt=%d, idx=%d, err=%d, "
2300 "mirror=%ld\n", bio->bi_vcnt, bio->bi_idx, err,
2301 (long int)bio->bi_bdev);
David Woodhouse902b22f2008-08-20 08:51:49 -04002302 tree = &BTRFS_I(page->mapping->host)->io_tree;
2303
Chris Masond1310b22008-01-24 16:13:08 -05002304 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2305 bvec->bv_offset;
2306 end = start + bvec->bv_len - 1;
2307
2308 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2309 whole_page = 1;
2310 else
2311 whole_page = 0;
2312
Chris Mason4125bf72010-02-03 18:18:45 +00002313 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05002314 prefetchw(&bvec->bv_page->flags);
2315
Arne Jansen507903b2011-04-06 10:02:20 +00002316 spin_lock(&tree->lock);
Chris Mason0d399202011-04-16 06:55:39 -04002317 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
Chris Mason109b36a2011-04-12 13:57:39 -04002318 if (state && state->start == start) {
Arne Jansen507903b2011-04-06 10:02:20 +00002319 /*
2320 * take a reference on the state, unlock will drop
2321 * the ref
2322 */
2323 cache_state(state, &cached);
2324 }
2325 spin_unlock(&tree->lock);
2326
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002327 mirror = (int)(unsigned long)bio->bi_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002328 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05002329 ret = tree->ops->readpage_end_io_hook(page, start, end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002330 state, mirror);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002331 if (ret) {
2332 /* no IO indicated but software detected errors
2333 * in the block, either checksum errors or
2334 * issues with the contents */
2335 struct btrfs_root *root =
2336 BTRFS_I(page->mapping->host)->root;
2337 struct btrfs_device *device;
2338
Chris Masond1310b22008-01-24 16:13:08 -05002339 uptodate = 0;
Stefan Behrens442a4f62012-05-25 16:06:08 +02002340 device = btrfs_find_device_for_logical(
2341 root, start, mirror);
2342 if (device)
2343 btrfs_dev_stat_inc_and_print(device,
2344 BTRFS_DEV_STAT_CORRUPTION_ERRS);
2345 } else {
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002346 clean_io_failure(start, page);
Stefan Behrens442a4f62012-05-25 16:06:08 +02002347 }
Chris Masond1310b22008-01-24 16:13:08 -05002348 }
Josef Bacikea466792012-03-26 21:57:36 -04002349
Josef Bacikea466792012-03-26 21:57:36 -04002350 if (!uptodate && tree->ops && tree->ops->readpage_io_failed_hook) {
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002351 ret = tree->ops->readpage_io_failed_hook(page, mirror);
Josef Bacikea466792012-03-26 21:57:36 -04002352 if (!ret && !err &&
2353 test_bit(BIO_UPTODATE, &bio->bi_flags))
2354 uptodate = 1;
2355 } else if (!uptodate) {
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002356 /*
2357 * The generic bio_readpage_error handles errors the
2358 * following way: If possible, new read requests are
2359 * created and submitted and will end up in
2360 * end_bio_extent_readpage as well (if we're lucky, not
2361 * in the !uptodate case). In that case it returns 0 and
2362 * we just go on with the next page in our bio. If it
2363 * can't handle the error it will return -EIO and we
2364 * remain responsible for that page.
2365 */
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002366 ret = bio_readpage_error(bio, page, start, end, mirror, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04002367 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04002368 uptodate =
2369 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05002370 if (err)
2371 uptodate = 0;
Arne Jansen507903b2011-04-06 10:02:20 +00002372 uncache_state(&cached);
Chris Mason7e383262008-04-09 16:28:12 -04002373 continue;
2374 }
2375 }
Chris Mason70dec802008-01-29 09:59:12 -05002376
Josef Bacik0b32f4b2012-03-13 09:38:00 -04002377 if (uptodate && tree->track_uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00002378 set_extent_uptodate(tree, start, end, &cached,
David Woodhouse902b22f2008-08-20 08:51:49 -04002379 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05002380 }
Arne Jansen507903b2011-04-06 10:02:20 +00002381 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05002382
Chris Mason70dec802008-01-29 09:59:12 -05002383 if (whole_page) {
2384 if (uptodate) {
2385 SetPageUptodate(page);
2386 } else {
2387 ClearPageUptodate(page);
2388 SetPageError(page);
2389 }
Chris Masond1310b22008-01-24 16:13:08 -05002390 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05002391 } else {
2392 if (uptodate) {
2393 check_page_uptodate(tree, page);
2394 } else {
2395 ClearPageUptodate(page);
2396 SetPageError(page);
2397 }
Chris Masond1310b22008-01-24 16:13:08 -05002398 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05002399 }
Chris Mason4125bf72010-02-03 18:18:45 +00002400 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05002401
2402 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002403}
2404
Miao Xie88f794e2010-11-22 03:02:55 +00002405struct bio *
2406btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2407 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002408{
2409 struct bio *bio;
2410
2411 bio = bio_alloc(gfp_flags, nr_vecs);
2412
2413 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2414 while (!bio && (nr_vecs /= 2))
2415 bio = bio_alloc(gfp_flags, nr_vecs);
2416 }
2417
2418 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04002419 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002420 bio->bi_bdev = bdev;
2421 bio->bi_sector = first_sector;
2422 }
2423 return bio;
2424}
2425
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002426/*
2427 * Since writes are async, they will only return -ENOMEM.
2428 * Reads can return the full range of I/O error conditions.
2429 */
Jeff Mahoney355808c2011-10-03 23:23:14 -04002430static int __must_check submit_one_bio(int rw, struct bio *bio,
2431 int mirror_num, unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002432{
Chris Masond1310b22008-01-24 16:13:08 -05002433 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002434 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2435 struct page *page = bvec->bv_page;
2436 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002437 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002438
2439 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002440
David Woodhouse902b22f2008-08-20 08:51:49 -04002441 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002442
2443 bio_get(bio);
2444
Chris Mason065631f2008-02-20 12:07:25 -05002445 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00002446 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002447 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002448 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01002449 btrfsic_submit_bio(rw, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002450
Chris Masond1310b22008-01-24 16:13:08 -05002451 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2452 ret = -EOPNOTSUPP;
2453 bio_put(bio);
2454 return ret;
2455}
2456
Jeff Mahoney3444a972011-10-03 23:23:13 -04002457static int merge_bio(struct extent_io_tree *tree, struct page *page,
2458 unsigned long offset, size_t size, struct bio *bio,
2459 unsigned long bio_flags)
2460{
2461 int ret = 0;
2462 if (tree->ops && tree->ops->merge_bio_hook)
2463 ret = tree->ops->merge_bio_hook(page, offset, size, bio,
2464 bio_flags);
2465 BUG_ON(ret < 0);
2466 return ret;
2467
2468}
2469
Chris Masond1310b22008-01-24 16:13:08 -05002470static int submit_extent_page(int rw, struct extent_io_tree *tree,
2471 struct page *page, sector_t sector,
2472 size_t size, unsigned long offset,
2473 struct block_device *bdev,
2474 struct bio **bio_ret,
2475 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04002476 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002477 int mirror_num,
2478 unsigned long prev_bio_flags,
2479 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002480{
2481 int ret = 0;
2482 struct bio *bio;
2483 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04002484 int contig = 0;
2485 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2486 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05002487 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002488
2489 if (bio_ret && *bio_ret) {
2490 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002491 if (old_compressed)
2492 contig = bio->bi_sector == sector;
2493 else
2494 contig = bio->bi_sector + (bio->bi_size >> 9) ==
2495 sector;
2496
2497 if (prev_bio_flags != bio_flags || !contig ||
Jeff Mahoney3444a972011-10-03 23:23:13 -04002498 merge_bio(tree, page, offset, page_size, bio, bio_flags) ||
Chris Masonc8b97812008-10-29 14:49:59 -04002499 bio_add_page(bio, page, page_size, offset) < page_size) {
2500 ret = submit_one_bio(rw, bio, mirror_num,
2501 prev_bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002502 if (ret < 0)
2503 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05002504 bio = NULL;
2505 } else {
2506 return 0;
2507 }
2508 }
Chris Masonc8b97812008-10-29 14:49:59 -04002509 if (this_compressed)
2510 nr = BIO_MAX_PAGES;
2511 else
2512 nr = bio_get_nr_vecs(bdev);
2513
Miao Xie88f794e2010-11-22 03:02:55 +00002514 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002515 if (!bio)
2516 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002517
Chris Masonc8b97812008-10-29 14:49:59 -04002518 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002519 bio->bi_end_io = end_io_func;
2520 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05002521
Chris Masond3977122009-01-05 21:25:51 -05002522 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002523 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002524 else
Chris Masonc8b97812008-10-29 14:49:59 -04002525 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002526
2527 return ret;
2528}
2529
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002530void attach_extent_buffer_page(struct extent_buffer *eb, struct page *page)
2531{
2532 if (!PagePrivate(page)) {
2533 SetPagePrivate(page);
2534 page_cache_get(page);
2535 set_page_private(page, (unsigned long)eb);
2536 } else {
2537 WARN_ON(page->private != (unsigned long)eb);
2538 }
2539}
2540
Chris Masond1310b22008-01-24 16:13:08 -05002541void set_page_extent_mapped(struct page *page)
2542{
2543 if (!PagePrivate(page)) {
2544 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002545 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002546 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002547 }
2548}
2549
Chris Masond1310b22008-01-24 16:13:08 -05002550/*
2551 * basic readpage implementation. Locked extent state structs are inserted
2552 * into the tree that are removed when the IO is done (by the end_io
2553 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002554 * XXX JDM: This needs looking at to ensure proper page locking
Chris Masond1310b22008-01-24 16:13:08 -05002555 */
2556static int __extent_read_full_page(struct extent_io_tree *tree,
2557 struct page *page,
2558 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002559 struct bio **bio, int mirror_num,
2560 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002561{
2562 struct inode *inode = page->mapping->host;
2563 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2564 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2565 u64 end;
2566 u64 cur = start;
2567 u64 extent_offset;
2568 u64 last_byte = i_size_read(inode);
2569 u64 block_start;
2570 u64 cur_end;
2571 sector_t sector;
2572 struct extent_map *em;
2573 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002574 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05002575 int ret;
2576 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002577 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002578 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002579 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002580 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002581 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002582
2583 set_page_extent_mapped(page);
2584
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002585 if (!PageUptodate(page)) {
2586 if (cleancache_get_page(page) == 0) {
2587 BUG_ON(blocksize != PAGE_SIZE);
2588 goto out;
2589 }
2590 }
2591
Chris Masond1310b22008-01-24 16:13:08 -05002592 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002593 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002594 lock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002595 ordered = btrfs_lookup_ordered_extent(inode, start);
2596 if (!ordered)
2597 break;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002598 unlock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002599 btrfs_start_ordered_extent(inode, ordered, 1);
2600 btrfs_put_ordered_extent(ordered);
2601 }
Chris Masond1310b22008-01-24 16:13:08 -05002602
Chris Masonc8b97812008-10-29 14:49:59 -04002603 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2604 char *userpage;
2605 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2606
2607 if (zero_offset) {
2608 iosize = PAGE_CACHE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002609 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002610 memset(userpage + zero_offset, 0, iosize);
2611 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002612 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002613 }
2614 }
Chris Masond1310b22008-01-24 16:13:08 -05002615 while (cur <= end) {
2616 if (cur >= last_byte) {
2617 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002618 struct extent_state *cached = NULL;
2619
David Sterba306e16c2011-04-19 14:29:38 +02002620 iosize = PAGE_CACHE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002621 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002622 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002623 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002624 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002625 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002626 &cached, GFP_NOFS);
2627 unlock_extent_cached(tree, cur, cur + iosize - 1,
2628 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002629 break;
2630 }
David Sterba306e16c2011-04-19 14:29:38 +02002631 em = get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002632 end - cur + 1, 0);
David Sterbac7040052011-04-19 18:00:01 +02002633 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002634 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002635 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002636 break;
2637 }
Chris Masond1310b22008-01-24 16:13:08 -05002638 extent_offset = cur - em->start;
2639 BUG_ON(extent_map_end(em) <= cur);
2640 BUG_ON(end < cur);
2641
Li Zefan261507a02010-12-17 14:21:50 +08002642 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002643 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002644 extent_set_compress_type(&this_bio_flag,
2645 em->compress_type);
2646 }
Chris Masonc8b97812008-10-29 14:49:59 -04002647
Chris Masond1310b22008-01-24 16:13:08 -05002648 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2649 cur_end = min(extent_map_end(em) - 1, end);
2650 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002651 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2652 disk_io_size = em->block_len;
2653 sector = em->block_start >> 9;
2654 } else {
2655 sector = (em->block_start + extent_offset) >> 9;
2656 disk_io_size = iosize;
2657 }
Chris Masond1310b22008-01-24 16:13:08 -05002658 bdev = em->bdev;
2659 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002660 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2661 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002662 free_extent_map(em);
2663 em = NULL;
2664
2665 /* we've found a hole, just zero and go on */
2666 if (block_start == EXTENT_MAP_HOLE) {
2667 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002668 struct extent_state *cached = NULL;
2669
Cong Wang7ac687d2011-11-25 23:14:28 +08002670 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002671 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002672 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002673 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002674
2675 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002676 &cached, GFP_NOFS);
2677 unlock_extent_cached(tree, cur, cur + iosize - 1,
2678 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002679 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002680 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002681 continue;
2682 }
2683 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002684 if (test_range_bit(tree, cur, cur_end,
2685 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002686 check_page_uptodate(tree, page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002687 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002688 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002689 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002690 continue;
2691 }
Chris Mason70dec802008-01-29 09:59:12 -05002692 /* we have an inline extent but it didn't get marked up
2693 * to date. Error out
2694 */
2695 if (block_start == EXTENT_MAP_INLINE) {
2696 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002697 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05002698 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002699 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05002700 continue;
2701 }
Chris Masond1310b22008-01-24 16:13:08 -05002702
2703 ret = 0;
2704 if (tree->ops && tree->ops->readpage_io_hook) {
2705 ret = tree->ops->readpage_io_hook(page, cur,
2706 cur + iosize - 1);
2707 }
2708 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002709 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2710 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002711 ret = submit_extent_page(READ, tree, page,
David Sterba306e16c2011-04-19 14:29:38 +02002712 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04002713 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002714 end_bio_extent_readpage, mirror_num,
2715 *bio_flags,
2716 this_bio_flag);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002717 BUG_ON(ret == -ENOMEM);
Chris Mason89642222008-07-24 09:41:53 -04002718 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002719 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002720 }
2721 if (ret)
2722 SetPageError(page);
2723 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002724 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002725 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002726out:
Chris Masond1310b22008-01-24 16:13:08 -05002727 if (!nr) {
2728 if (!PageError(page))
2729 SetPageUptodate(page);
2730 unlock_page(page);
2731 }
2732 return 0;
2733}
2734
2735int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002736 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05002737{
2738 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002739 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002740 int ret;
2741
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002742 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Chris Masonc8b97812008-10-29 14:49:59 -04002743 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002744 if (bio)
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002745 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002746 return ret;
2747}
Chris Masond1310b22008-01-24 16:13:08 -05002748
Chris Mason11c83492009-04-20 15:50:09 -04002749static noinline void update_nr_written(struct page *page,
2750 struct writeback_control *wbc,
2751 unsigned long nr_written)
2752{
2753 wbc->nr_to_write -= nr_written;
2754 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2755 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2756 page->mapping->writeback_index = page->index + nr_written;
2757}
2758
Chris Masond1310b22008-01-24 16:13:08 -05002759/*
2760 * the writepage semantics are similar to regular writepage. extent
2761 * records are inserted to lock ranges in the tree, and as dirty areas
2762 * are found, they are marked writeback. Then the lock bits are removed
2763 * and the end_io handler clears the writeback ranges
2764 */
2765static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2766 void *data)
2767{
2768 struct inode *inode = page->mapping->host;
2769 struct extent_page_data *epd = data;
2770 struct extent_io_tree *tree = epd->tree;
2771 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2772 u64 delalloc_start;
2773 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2774 u64 end;
2775 u64 cur = start;
2776 u64 extent_offset;
2777 u64 last_byte = i_size_read(inode);
2778 u64 block_start;
2779 u64 iosize;
2780 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002781 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002782 struct extent_map *em;
2783 struct block_device *bdev;
2784 int ret;
2785 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002786 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002787 size_t blocksize;
2788 loff_t i_size = i_size_read(inode);
2789 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2790 u64 nr_delalloc;
2791 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002792 int page_started;
2793 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002794 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002795 unsigned long nr_written = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002796 bool fill_delalloc = true;
Chris Masond1310b22008-01-24 16:13:08 -05002797
Chris Masonffbd5172009-04-20 15:50:09 -04002798 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002799 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002800 else
2801 write_flags = WRITE;
2802
liubo1abe9b82011-03-24 11:18:59 +00002803 trace___extent_writepage(page, inode, wbc);
2804
Chris Masond1310b22008-01-24 16:13:08 -05002805 WARN_ON(!PageLocked(page));
Chris Masonbf0da8c2011-11-04 12:29:37 -04002806
2807 ClearPageError(page);
2808
Chris Mason7f3c74f2008-07-18 12:01:11 -04002809 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002810 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002811 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002812 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002813 unlock_page(page);
2814 return 0;
2815 }
2816
2817 if (page->index == end_index) {
2818 char *userpage;
2819
Cong Wang7ac687d2011-11-25 23:14:28 +08002820 userpage = kmap_atomic(page);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002821 memset(userpage + pg_offset, 0,
2822 PAGE_CACHE_SIZE - pg_offset);
Cong Wang7ac687d2011-11-25 23:14:28 +08002823 kunmap_atomic(userpage);
Chris Mason211c17f2008-05-15 09:13:45 -04002824 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002825 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002826 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002827
2828 set_page_extent_mapped(page);
2829
Josef Bacik9e487102011-08-01 12:08:18 -04002830 if (!tree->ops || !tree->ops->fill_delalloc)
2831 fill_delalloc = false;
2832
Chris Masond1310b22008-01-24 16:13:08 -05002833 delalloc_start = start;
2834 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002835 page_started = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002836 if (!epd->extent_locked && fill_delalloc) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002837 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002838 /*
2839 * make sure the wbc mapping index is at least updated
2840 * to this page.
2841 */
2842 update_nr_written(page, wbc, 0);
2843
Chris Masond3977122009-01-05 21:25:51 -05002844 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002845 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002846 page,
2847 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002848 &delalloc_end,
2849 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002850 if (nr_delalloc == 0) {
2851 delalloc_start = delalloc_end + 1;
2852 continue;
2853 }
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002854 ret = tree->ops->fill_delalloc(inode, page,
2855 delalloc_start,
2856 delalloc_end,
2857 &page_started,
2858 &nr_written);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002859 /* File system has been set read-only */
2860 if (ret) {
2861 SetPageError(page);
2862 goto done;
2863 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002864 /*
2865 * delalloc_end is already one less than the total
2866 * length, so we don't subtract one from
2867 * PAGE_CACHE_SIZE
2868 */
2869 delalloc_to_write += (delalloc_end - delalloc_start +
2870 PAGE_CACHE_SIZE) >>
2871 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002872 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002873 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002874 if (wbc->nr_to_write < delalloc_to_write) {
2875 int thresh = 8192;
2876
2877 if (delalloc_to_write < thresh * 2)
2878 thresh = delalloc_to_write;
2879 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2880 thresh);
2881 }
Chris Masonc8b97812008-10-29 14:49:59 -04002882
Chris Mason771ed682008-11-06 22:02:51 -05002883 /* did the fill delalloc function already unlock and start
2884 * the IO?
2885 */
2886 if (page_started) {
2887 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002888 /*
2889 * we've unlocked the page, so we can't update
2890 * the mapping's writeback index, just update
2891 * nr_to_write.
2892 */
2893 wbc->nr_to_write -= nr_written;
2894 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002895 }
Chris Masonc8b97812008-10-29 14:49:59 -04002896 }
Chris Mason247e7432008-07-17 12:53:51 -04002897 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002898 ret = tree->ops->writepage_start_hook(page, start,
2899 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002900 if (ret) {
2901 /* Fixup worker will requeue */
2902 if (ret == -EBUSY)
2903 wbc->pages_skipped++;
2904 else
2905 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002906 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002907 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002908 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002909 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002910 }
2911 }
2912
Chris Mason11c83492009-04-20 15:50:09 -04002913 /*
2914 * we don't want to touch the inode after unlocking the page,
2915 * so we update the mapping writeback index now
2916 */
2917 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002918
Chris Masond1310b22008-01-24 16:13:08 -05002919 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002920 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002921 if (tree->ops && tree->ops->writepage_end_io_hook)
2922 tree->ops->writepage_end_io_hook(page, start,
2923 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002924 goto done;
2925 }
2926
Chris Masond1310b22008-01-24 16:13:08 -05002927 blocksize = inode->i_sb->s_blocksize;
2928
2929 while (cur <= end) {
2930 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002931 if (tree->ops && tree->ops->writepage_end_io_hook)
2932 tree->ops->writepage_end_io_hook(page, cur,
2933 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002934 break;
2935 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002936 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002937 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02002938 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002939 SetPageError(page);
2940 break;
2941 }
2942
2943 extent_offset = cur - em->start;
2944 BUG_ON(extent_map_end(em) <= cur);
2945 BUG_ON(end < cur);
2946 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2947 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2948 sector = (em->block_start + extent_offset) >> 9;
2949 bdev = em->bdev;
2950 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002951 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002952 free_extent_map(em);
2953 em = NULL;
2954
Chris Masonc8b97812008-10-29 14:49:59 -04002955 /*
2956 * compressed and inline extents are written through other
2957 * paths in the FS
2958 */
2959 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002960 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002961 /*
2962 * end_io notification does not happen here for
2963 * compressed extents
2964 */
2965 if (!compressed && tree->ops &&
2966 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002967 tree->ops->writepage_end_io_hook(page, cur,
2968 cur + iosize - 1,
2969 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002970 else if (compressed) {
2971 /* we don't want to end_page_writeback on
2972 * a compressed extent. this happens
2973 * elsewhere
2974 */
2975 nr++;
2976 }
2977
2978 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002979 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002980 continue;
2981 }
Chris Masond1310b22008-01-24 16:13:08 -05002982 /* leave this out until we have a page_mkwrite call */
2983 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04002984 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002985 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002986 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002987 continue;
2988 }
Chris Masonc8b97812008-10-29 14:49:59 -04002989
Chris Masond1310b22008-01-24 16:13:08 -05002990 if (tree->ops && tree->ops->writepage_io_hook) {
2991 ret = tree->ops->writepage_io_hook(page, cur,
2992 cur + iosize - 1);
2993 } else {
2994 ret = 0;
2995 }
Chris Mason1259ab72008-05-12 13:39:03 -04002996 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002997 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002998 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002999 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003000
Chris Masond1310b22008-01-24 16:13:08 -05003001 set_range_writeback(tree, cur, cur + iosize - 1);
3002 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05003003 printk(KERN_ERR "btrfs warning page %lu not "
3004 "writeback, cur %llu end %llu\n",
3005 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05003006 (unsigned long long)end);
3007 }
3008
Chris Masonffbd5172009-04-20 15:50:09 -04003009 ret = submit_extent_page(write_flags, tree, page,
3010 sector, iosize, pg_offset,
3011 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04003012 end_bio_extent_writepage,
3013 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003014 if (ret)
3015 SetPageError(page);
3016 }
3017 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003018 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003019 nr++;
3020 }
3021done:
3022 if (nr == 0) {
3023 /* make sure the mapping tag for page dirty gets cleared */
3024 set_page_writeback(page);
3025 end_page_writeback(page);
3026 }
Chris Masond1310b22008-01-24 16:13:08 -05003027 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05003028
Chris Mason11c83492009-04-20 15:50:09 -04003029done_unlocked:
3030
Chris Mason2c64c532009-09-02 15:04:12 -04003031 /* drop our reference on any cached states */
3032 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05003033 return 0;
3034}
3035
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003036static int eb_wait(void *word)
3037{
3038 io_schedule();
3039 return 0;
3040}
3041
3042static void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
3043{
3044 wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
3045 TASK_UNINTERRUPTIBLE);
3046}
3047
3048static int lock_extent_buffer_for_io(struct extent_buffer *eb,
3049 struct btrfs_fs_info *fs_info,
3050 struct extent_page_data *epd)
3051{
3052 unsigned long i, num_pages;
3053 int flush = 0;
3054 int ret = 0;
3055
3056 if (!btrfs_try_tree_write_lock(eb)) {
3057 flush = 1;
3058 flush_write_bio(epd);
3059 btrfs_tree_lock(eb);
3060 }
3061
3062 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3063 btrfs_tree_unlock(eb);
3064 if (!epd->sync_io)
3065 return 0;
3066 if (!flush) {
3067 flush_write_bio(epd);
3068 flush = 1;
3069 }
Chris Masona098d8e2012-03-21 12:09:56 -04003070 while (1) {
3071 wait_on_extent_buffer_writeback(eb);
3072 btrfs_tree_lock(eb);
3073 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3074 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003075 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003076 }
3077 }
3078
3079 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3080 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3081 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
3082 spin_lock(&fs_info->delalloc_lock);
3083 if (fs_info->dirty_metadata_bytes >= eb->len)
3084 fs_info->dirty_metadata_bytes -= eb->len;
3085 else
3086 WARN_ON(1);
3087 spin_unlock(&fs_info->delalloc_lock);
3088 ret = 1;
3089 }
3090
3091 btrfs_tree_unlock(eb);
3092
3093 if (!ret)
3094 return ret;
3095
3096 num_pages = num_extent_pages(eb->start, eb->len);
3097 for (i = 0; i < num_pages; i++) {
3098 struct page *p = extent_buffer_page(eb, i);
3099
3100 if (!trylock_page(p)) {
3101 if (!flush) {
3102 flush_write_bio(epd);
3103 flush = 1;
3104 }
3105 lock_page(p);
3106 }
3107 }
3108
3109 return ret;
3110}
3111
3112static void end_extent_buffer_writeback(struct extent_buffer *eb)
3113{
3114 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3115 smp_mb__after_clear_bit();
3116 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3117}
3118
3119static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
3120{
3121 int uptodate = err == 0;
3122 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
3123 struct extent_buffer *eb;
3124 int done;
3125
3126 do {
3127 struct page *page = bvec->bv_page;
3128
3129 bvec--;
3130 eb = (struct extent_buffer *)page->private;
3131 BUG_ON(!eb);
3132 done = atomic_dec_and_test(&eb->io_pages);
3133
3134 if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
3135 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3136 ClearPageUptodate(page);
3137 SetPageError(page);
3138 }
3139
3140 end_page_writeback(page);
3141
3142 if (!done)
3143 continue;
3144
3145 end_extent_buffer_writeback(eb);
3146 } while (bvec >= bio->bi_io_vec);
3147
3148 bio_put(bio);
3149
3150}
3151
3152static int write_one_eb(struct extent_buffer *eb,
3153 struct btrfs_fs_info *fs_info,
3154 struct writeback_control *wbc,
3155 struct extent_page_data *epd)
3156{
3157 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
3158 u64 offset = eb->start;
3159 unsigned long i, num_pages;
3160 int rw = (epd->sync_io ? WRITE_SYNC : WRITE);
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003161 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003162
3163 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3164 num_pages = num_extent_pages(eb->start, eb->len);
3165 atomic_set(&eb->io_pages, num_pages);
3166 for (i = 0; i < num_pages; i++) {
3167 struct page *p = extent_buffer_page(eb, i);
3168
3169 clear_page_dirty_for_io(p);
3170 set_page_writeback(p);
3171 ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
3172 PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
3173 -1, end_bio_extent_buffer_writepage,
3174 0, 0, 0);
3175 if (ret) {
3176 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3177 SetPageError(p);
3178 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3179 end_extent_buffer_writeback(eb);
3180 ret = -EIO;
3181 break;
3182 }
3183 offset += PAGE_CACHE_SIZE;
3184 update_nr_written(p, wbc, 1);
3185 unlock_page(p);
3186 }
3187
3188 if (unlikely(ret)) {
3189 for (; i < num_pages; i++) {
3190 struct page *p = extent_buffer_page(eb, i);
3191 unlock_page(p);
3192 }
3193 }
3194
3195 return ret;
3196}
3197
3198int btree_write_cache_pages(struct address_space *mapping,
3199 struct writeback_control *wbc)
3200{
3201 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3202 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3203 struct extent_buffer *eb, *prev_eb = NULL;
3204 struct extent_page_data epd = {
3205 .bio = NULL,
3206 .tree = tree,
3207 .extent_locked = 0,
3208 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3209 };
3210 int ret = 0;
3211 int done = 0;
3212 int nr_to_write_done = 0;
3213 struct pagevec pvec;
3214 int nr_pages;
3215 pgoff_t index;
3216 pgoff_t end; /* Inclusive */
3217 int scanned = 0;
3218 int tag;
3219
3220 pagevec_init(&pvec, 0);
3221 if (wbc->range_cyclic) {
3222 index = mapping->writeback_index; /* Start from prev offset */
3223 end = -1;
3224 } else {
3225 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3226 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3227 scanned = 1;
3228 }
3229 if (wbc->sync_mode == WB_SYNC_ALL)
3230 tag = PAGECACHE_TAG_TOWRITE;
3231 else
3232 tag = PAGECACHE_TAG_DIRTY;
3233retry:
3234 if (wbc->sync_mode == WB_SYNC_ALL)
3235 tag_pages_for_writeback(mapping, index, end);
3236 while (!done && !nr_to_write_done && (index <= end) &&
3237 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3238 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3239 unsigned i;
3240
3241 scanned = 1;
3242 for (i = 0; i < nr_pages; i++) {
3243 struct page *page = pvec.pages[i];
3244
3245 if (!PagePrivate(page))
3246 continue;
3247
3248 if (!wbc->range_cyclic && page->index > end) {
3249 done = 1;
3250 break;
3251 }
3252
3253 eb = (struct extent_buffer *)page->private;
3254 if (!eb) {
3255 WARN_ON(1);
3256 continue;
3257 }
3258
3259 if (eb == prev_eb)
3260 continue;
3261
3262 if (!atomic_inc_not_zero(&eb->refs)) {
3263 WARN_ON(1);
3264 continue;
3265 }
3266
3267 prev_eb = eb;
3268 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3269 if (!ret) {
3270 free_extent_buffer(eb);
3271 continue;
3272 }
3273
3274 ret = write_one_eb(eb, fs_info, wbc, &epd);
3275 if (ret) {
3276 done = 1;
3277 free_extent_buffer(eb);
3278 break;
3279 }
3280 free_extent_buffer(eb);
3281
3282 /*
3283 * the filesystem may choose to bump up nr_to_write.
3284 * We have to make sure to honor the new nr_to_write
3285 * at any time
3286 */
3287 nr_to_write_done = wbc->nr_to_write <= 0;
3288 }
3289 pagevec_release(&pvec);
3290 cond_resched();
3291 }
3292 if (!scanned && !done) {
3293 /*
3294 * We hit the last page and there is more work to be done: wrap
3295 * back to the start of the file
3296 */
3297 scanned = 1;
3298 index = 0;
3299 goto retry;
3300 }
3301 flush_write_bio(&epd);
3302 return ret;
3303}
3304
Chris Masond1310b22008-01-24 16:13:08 -05003305/**
Chris Mason4bef0842008-09-08 11:18:08 -04003306 * 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 -05003307 * @mapping: address space structure to write
3308 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3309 * @writepage: function called for each page
3310 * @data: data passed to writepage function
3311 *
3312 * If a page is already under I/O, write_cache_pages() skips it, even
3313 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3314 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3315 * and msync() need to guarantee that all the data which was dirty at the time
3316 * the call was made get new I/O started against them. If wbc->sync_mode is
3317 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3318 * existing IO to complete.
3319 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003320static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04003321 struct address_space *mapping,
3322 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003323 writepage_t writepage, void *data,
3324 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05003325{
Chris Masond1310b22008-01-24 16:13:08 -05003326 int ret = 0;
3327 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003328 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003329 struct pagevec pvec;
3330 int nr_pages;
3331 pgoff_t index;
3332 pgoff_t end; /* Inclusive */
3333 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003334 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003335
Chris Masond1310b22008-01-24 16:13:08 -05003336 pagevec_init(&pvec, 0);
3337 if (wbc->range_cyclic) {
3338 index = mapping->writeback_index; /* Start from prev offset */
3339 end = -1;
3340 } else {
3341 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3342 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003343 scanned = 1;
3344 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003345 if (wbc->sync_mode == WB_SYNC_ALL)
3346 tag = PAGECACHE_TAG_TOWRITE;
3347 else
3348 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003349retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003350 if (wbc->sync_mode == WB_SYNC_ALL)
3351 tag_pages_for_writeback(mapping, index, end);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003352 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003353 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3354 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003355 unsigned i;
3356
3357 scanned = 1;
3358 for (i = 0; i < nr_pages; i++) {
3359 struct page *page = pvec.pages[i];
3360
3361 /*
3362 * At this point we hold neither mapping->tree_lock nor
3363 * lock on the page itself: the page may be truncated or
3364 * invalidated (changing page->mapping to NULL), or even
3365 * swizzled back from swapper_space to tmpfs file
3366 * mapping
3367 */
Chris Mason01d658f2011-11-01 10:08:06 -04003368 if (tree->ops &&
3369 tree->ops->write_cache_pages_lock_hook) {
3370 tree->ops->write_cache_pages_lock_hook(page,
3371 data, flush_fn);
3372 } else {
3373 if (!trylock_page(page)) {
3374 flush_fn(data);
3375 lock_page(page);
3376 }
3377 }
Chris Masond1310b22008-01-24 16:13:08 -05003378
3379 if (unlikely(page->mapping != mapping)) {
3380 unlock_page(page);
3381 continue;
3382 }
3383
3384 if (!wbc->range_cyclic && page->index > end) {
3385 done = 1;
3386 unlock_page(page);
3387 continue;
3388 }
3389
Chris Masond2c3f4f2008-11-19 12:44:22 -05003390 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003391 if (PageWriteback(page))
3392 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05003393 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003394 }
Chris Masond1310b22008-01-24 16:13:08 -05003395
3396 if (PageWriteback(page) ||
3397 !clear_page_dirty_for_io(page)) {
3398 unlock_page(page);
3399 continue;
3400 }
3401
3402 ret = (*writepage)(page, wbc, data);
3403
3404 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3405 unlock_page(page);
3406 ret = 0;
3407 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003408 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05003409 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003410
3411 /*
3412 * the filesystem may choose to bump up nr_to_write.
3413 * We have to make sure to honor the new nr_to_write
3414 * at any time
3415 */
3416 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05003417 }
3418 pagevec_release(&pvec);
3419 cond_resched();
3420 }
3421 if (!scanned && !done) {
3422 /*
3423 * We hit the last page and there is more work to be done: wrap
3424 * back to the start of the file
3425 */
3426 scanned = 1;
3427 index = 0;
3428 goto retry;
3429 }
Chris Masond1310b22008-01-24 16:13:08 -05003430 return ret;
3431}
Chris Masond1310b22008-01-24 16:13:08 -05003432
Chris Masonffbd5172009-04-20 15:50:09 -04003433static void flush_epd_write_bio(struct extent_page_data *epd)
3434{
3435 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04003436 int rw = WRITE;
3437 int ret;
3438
Chris Masonffbd5172009-04-20 15:50:09 -04003439 if (epd->sync_io)
Jeff Mahoney355808c2011-10-03 23:23:14 -04003440 rw = WRITE_SYNC;
3441
3442 ret = submit_one_bio(rw, epd->bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003443 BUG_ON(ret < 0); /* -ENOMEM */
Chris Masonffbd5172009-04-20 15:50:09 -04003444 epd->bio = NULL;
3445 }
3446}
3447
Chris Masond2c3f4f2008-11-19 12:44:22 -05003448static noinline void flush_write_bio(void *data)
3449{
3450 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04003451 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003452}
3453
Chris Masond1310b22008-01-24 16:13:08 -05003454int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3455 get_extent_t *get_extent,
3456 struct writeback_control *wbc)
3457{
3458 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003459 struct extent_page_data epd = {
3460 .bio = NULL,
3461 .tree = tree,
3462 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003463 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003464 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05003465 };
Chris Masond1310b22008-01-24 16:13:08 -05003466
Chris Masond1310b22008-01-24 16:13:08 -05003467 ret = __extent_writepage(page, wbc, &epd);
3468
Chris Masonffbd5172009-04-20 15:50:09 -04003469 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003470 return ret;
3471}
Chris Masond1310b22008-01-24 16:13:08 -05003472
Chris Mason771ed682008-11-06 22:02:51 -05003473int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3474 u64 start, u64 end, get_extent_t *get_extent,
3475 int mode)
3476{
3477 int ret = 0;
3478 struct address_space *mapping = inode->i_mapping;
3479 struct page *page;
3480 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3481 PAGE_CACHE_SHIFT;
3482
3483 struct extent_page_data epd = {
3484 .bio = NULL,
3485 .tree = tree,
3486 .get_extent = get_extent,
3487 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04003488 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05003489 };
3490 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05003491 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05003492 .nr_to_write = nr_pages * 2,
3493 .range_start = start,
3494 .range_end = end + 1,
3495 };
3496
Chris Masond3977122009-01-05 21:25:51 -05003497 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05003498 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3499 if (clear_page_dirty_for_io(page))
3500 ret = __extent_writepage(page, &wbc_writepages, &epd);
3501 else {
3502 if (tree->ops && tree->ops->writepage_end_io_hook)
3503 tree->ops->writepage_end_io_hook(page, start,
3504 start + PAGE_CACHE_SIZE - 1,
3505 NULL, 1);
3506 unlock_page(page);
3507 }
3508 page_cache_release(page);
3509 start += PAGE_CACHE_SIZE;
3510 }
3511
Chris Masonffbd5172009-04-20 15:50:09 -04003512 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05003513 return ret;
3514}
Chris Masond1310b22008-01-24 16:13:08 -05003515
3516int extent_writepages(struct extent_io_tree *tree,
3517 struct address_space *mapping,
3518 get_extent_t *get_extent,
3519 struct writeback_control *wbc)
3520{
3521 int ret = 0;
3522 struct extent_page_data epd = {
3523 .bio = NULL,
3524 .tree = tree,
3525 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003526 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003527 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05003528 };
3529
Chris Mason4bef0842008-09-08 11:18:08 -04003530 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003531 __extent_writepage, &epd,
3532 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04003533 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003534 return ret;
3535}
Chris Masond1310b22008-01-24 16:13:08 -05003536
3537int extent_readpages(struct extent_io_tree *tree,
3538 struct address_space *mapping,
3539 struct list_head *pages, unsigned nr_pages,
3540 get_extent_t get_extent)
3541{
3542 struct bio *bio = NULL;
3543 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04003544 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003545
Chris Masond1310b22008-01-24 16:13:08 -05003546 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
3547 struct page *page = list_entry(pages->prev, struct page, lru);
3548
3549 prefetchw(&page->flags);
3550 list_del(&page->lru);
Nick Piggin28ecb6092010-03-17 13:31:04 +00003551 if (!add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04003552 page->index, GFP_NOFS)) {
Chris Masonf1885912008-04-09 16:28:12 -04003553 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04003554 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003555 }
3556 page_cache_release(page);
3557 }
Chris Masond1310b22008-01-24 16:13:08 -05003558 BUG_ON(!list_empty(pages));
3559 if (bio)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003560 return submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003561 return 0;
3562}
Chris Masond1310b22008-01-24 16:13:08 -05003563
3564/*
3565 * basic invalidatepage code, this waits on any locked or writeback
3566 * ranges corresponding to the page, and then deletes any extent state
3567 * records from the tree
3568 */
3569int extent_invalidatepage(struct extent_io_tree *tree,
3570 struct page *page, unsigned long offset)
3571{
Josef Bacik2ac55d42010-02-03 19:33:23 +00003572 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003573 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
3574 u64 end = start + PAGE_CACHE_SIZE - 1;
3575 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3576
Chris Masond3977122009-01-05 21:25:51 -05003577 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003578 if (start > end)
3579 return 0;
3580
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003581 lock_extent_bits(tree, start, end, 0, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04003582 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003583 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04003584 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3585 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003586 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003587 return 0;
3588}
Chris Masond1310b22008-01-24 16:13:08 -05003589
3590/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04003591 * a helper for releasepage, this tests for areas of the page that
3592 * are locked or under IO and drops the related state bits if it is safe
3593 * to drop the page.
3594 */
3595int try_release_extent_state(struct extent_map_tree *map,
3596 struct extent_io_tree *tree, struct page *page,
3597 gfp_t mask)
3598{
3599 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3600 u64 end = start + PAGE_CACHE_SIZE - 1;
3601 int ret = 1;
3602
Chris Mason211f90e2008-07-18 11:56:15 -04003603 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04003604 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04003605 ret = 0;
3606 else {
3607 if ((mask & GFP_NOFS) == GFP_NOFS)
3608 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04003609 /*
3610 * at this point we can safely clear everything except the
3611 * locked bit and the nodatasum bit
3612 */
Chris Masone3f24cc2011-02-14 12:52:08 -05003613 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04003614 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3615 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05003616
3617 /* if clear_extent_bit failed for enomem reasons,
3618 * we can't allow the release to continue.
3619 */
3620 if (ret < 0)
3621 ret = 0;
3622 else
3623 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003624 }
3625 return ret;
3626}
Chris Mason7b13b7b2008-04-18 10:29:50 -04003627
3628/*
Chris Masond1310b22008-01-24 16:13:08 -05003629 * a helper for releasepage. As long as there are no locked extents
3630 * in the range corresponding to the page, both state records and extent
3631 * map records are removed
3632 */
3633int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05003634 struct extent_io_tree *tree, struct page *page,
3635 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05003636{
3637 struct extent_map *em;
3638 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3639 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003640
Chris Mason70dec802008-01-29 09:59:12 -05003641 if ((mask & __GFP_WAIT) &&
3642 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05003643 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05003644 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05003645 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04003646 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05003647 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09003648 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04003649 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003650 break;
3651 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003652 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3653 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04003654 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003655 free_extent_map(em);
3656 break;
3657 }
3658 if (!test_range_bit(tree, em->start,
3659 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04003660 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04003661 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05003662 remove_extent_mapping(map, em);
3663 /* once for the rb tree */
3664 free_extent_map(em);
3665 }
3666 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04003667 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003668
3669 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05003670 free_extent_map(em);
3671 }
Chris Masond1310b22008-01-24 16:13:08 -05003672 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04003673 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003674}
Chris Masond1310b22008-01-24 16:13:08 -05003675
Chris Masonec29ed52011-02-23 16:23:20 -05003676/*
3677 * helper function for fiemap, which doesn't want to see any holes.
3678 * This maps until we find something past 'last'
3679 */
3680static struct extent_map *get_extent_skip_holes(struct inode *inode,
3681 u64 offset,
3682 u64 last,
3683 get_extent_t *get_extent)
3684{
3685 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3686 struct extent_map *em;
3687 u64 len;
3688
3689 if (offset >= last)
3690 return NULL;
3691
3692 while(1) {
3693 len = last - offset;
3694 if (len == 0)
3695 break;
3696 len = (len + sectorsize - 1) & ~(sectorsize - 1);
3697 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02003698 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05003699 return em;
3700
3701 /* if this isn't a hole return it */
3702 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3703 em->block_start != EXTENT_MAP_HOLE) {
3704 return em;
3705 }
3706
3707 /* this is a hole, advance to the next extent */
3708 offset = extent_map_end(em);
3709 free_extent_map(em);
3710 if (offset >= last)
3711 break;
3712 }
3713 return NULL;
3714}
3715
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003716int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3717 __u64 start, __u64 len, get_extent_t *get_extent)
3718{
Josef Bacik975f84f2010-11-23 19:36:57 +00003719 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003720 u64 off = start;
3721 u64 max = start + len;
3722 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00003723 u32 found_type;
3724 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05003725 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003726 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003727 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00003728 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003729 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003730 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00003731 struct btrfs_path *path;
3732 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003733 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003734 u64 em_start = 0;
3735 u64 em_len = 0;
3736 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003737 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003738
3739 if (len == 0)
3740 return -EINVAL;
3741
Josef Bacik975f84f2010-11-23 19:36:57 +00003742 path = btrfs_alloc_path();
3743 if (!path)
3744 return -ENOMEM;
3745 path->leave_spinning = 1;
3746
Josef Bacik4d479cf2011-11-17 11:34:31 -05003747 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3748 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3749
Chris Masonec29ed52011-02-23 16:23:20 -05003750 /*
3751 * lookup the last file extent. We're not using i_size here
3752 * because there might be preallocation past i_size
3753 */
Josef Bacik975f84f2010-11-23 19:36:57 +00003754 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
Li Zefan33345d012011-04-20 10:31:50 +08003755 path, btrfs_ino(inode), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00003756 if (ret < 0) {
3757 btrfs_free_path(path);
3758 return ret;
3759 }
3760 WARN_ON(!ret);
3761 path->slots[0]--;
3762 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3763 struct btrfs_file_extent_item);
3764 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3765 found_type = btrfs_key_type(&found_key);
3766
Chris Masonec29ed52011-02-23 16:23:20 -05003767 /* No extents, but there might be delalloc bits */
Li Zefan33345d012011-04-20 10:31:50 +08003768 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00003769 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05003770 /* have to trust i_size as the end */
3771 last = (u64)-1;
3772 last_for_get_extent = isize;
3773 } else {
3774 /*
3775 * remember the start of the last extent. There are a
3776 * bunch of different factors that go into the length of the
3777 * extent, so its much less complex to remember where it started
3778 */
3779 last = found_key.offset;
3780 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00003781 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003782 btrfs_free_path(path);
3783
Chris Masonec29ed52011-02-23 16:23:20 -05003784 /*
3785 * we might have some extents allocated but more delalloc past those
3786 * extents. so, we trust isize unless the start of the last extent is
3787 * beyond isize
3788 */
3789 if (last < isize) {
3790 last = (u64)-1;
3791 last_for_get_extent = isize;
3792 }
3793
Josef Bacik2ac55d42010-02-03 19:33:23 +00003794 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003795 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05003796
Josef Bacik4d479cf2011-11-17 11:34:31 -05003797 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05003798 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003799 if (!em)
3800 goto out;
3801 if (IS_ERR(em)) {
3802 ret = PTR_ERR(em);
3803 goto out;
3804 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003805
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003806 while (!end) {
Chris Masonea8efc72011-03-08 11:54:40 -05003807 u64 offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003808
Chris Masonea8efc72011-03-08 11:54:40 -05003809 /* break if the extent we found is outside the range */
3810 if (em->start >= max || extent_map_end(em) < off)
3811 break;
3812
3813 /*
3814 * get_extent may return an extent that starts before our
3815 * requested range. We have to make sure the ranges
3816 * we return to fiemap always move forward and don't
3817 * overlap, so adjust the offsets here
3818 */
3819 em_start = max(em->start, off);
3820
3821 /*
3822 * record the offset from the start of the extent
3823 * for adjusting the disk offset below
3824 */
3825 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05003826 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05003827 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05003828 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003829 disko = 0;
3830 flags = 0;
3831
Chris Masonea8efc72011-03-08 11:54:40 -05003832 /*
3833 * bump off for our next call to get_extent
3834 */
3835 off = extent_map_end(em);
3836 if (off >= max)
3837 end = 1;
3838
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003839 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003840 end = 1;
3841 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003842 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003843 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3844 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003845 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003846 flags |= (FIEMAP_EXTENT_DELALLOC |
3847 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003848 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05003849 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003850 }
3851 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3852 flags |= FIEMAP_EXTENT_ENCODED;
3853
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003854 free_extent_map(em);
3855 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05003856 if ((em_start >= last) || em_len == (u64)-1 ||
3857 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003858 flags |= FIEMAP_EXTENT_LAST;
3859 end = 1;
3860 }
3861
Chris Masonec29ed52011-02-23 16:23:20 -05003862 /* now scan forward to see if this is really the last extent. */
3863 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3864 get_extent);
3865 if (IS_ERR(em)) {
3866 ret = PTR_ERR(em);
3867 goto out;
3868 }
3869 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00003870 flags |= FIEMAP_EXTENT_LAST;
3871 end = 1;
3872 }
Chris Masonec29ed52011-02-23 16:23:20 -05003873 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3874 em_len, flags);
3875 if (ret)
3876 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003877 }
3878out_free:
3879 free_extent_map(em);
3880out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00003881 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3882 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003883 return ret;
3884}
3885
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003886inline struct page *extent_buffer_page(struct extent_buffer *eb,
Chris Masond1310b22008-01-24 16:13:08 -05003887 unsigned long i)
3888{
Chris Mason727011e2010-08-06 13:21:20 -04003889 return eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05003890}
3891
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003892inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04003893{
Chris Mason6af118ce2008-07-22 11:18:07 -04003894 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3895 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04003896}
3897
Chris Mason727011e2010-08-06 13:21:20 -04003898static void __free_extent_buffer(struct extent_buffer *eb)
3899{
3900#if LEAK_DEBUG
3901 unsigned long flags;
3902 spin_lock_irqsave(&leak_lock, flags);
3903 list_del(&eb->leak_list);
3904 spin_unlock_irqrestore(&leak_lock, flags);
3905#endif
3906 if (eb->pages && eb->pages != eb->inline_pages)
3907 kfree(eb->pages);
3908 kmem_cache_free(extent_buffer_cache, eb);
3909}
3910
Chris Masond1310b22008-01-24 16:13:08 -05003911static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3912 u64 start,
3913 unsigned long len,
3914 gfp_t mask)
3915{
3916 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003917#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003918 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003919#endif
Chris Masond1310b22008-01-24 16:13:08 -05003920
Chris Masond1310b22008-01-24 16:13:08 -05003921 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003922 if (eb == NULL)
3923 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003924 eb->start = start;
3925 eb->len = len;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05003926 eb->tree = tree;
Jan Schmidt815a51c2012-05-16 17:00:02 +02003927 eb->bflags = 0;
Chris Masonbd681512011-07-16 15:23:14 -04003928 rwlock_init(&eb->lock);
3929 atomic_set(&eb->write_locks, 0);
3930 atomic_set(&eb->read_locks, 0);
3931 atomic_set(&eb->blocking_readers, 0);
3932 atomic_set(&eb->blocking_writers, 0);
3933 atomic_set(&eb->spinning_readers, 0);
3934 atomic_set(&eb->spinning_writers, 0);
Arne Jansen5b25f702011-09-13 10:55:48 +02003935 eb->lock_nested = 0;
Chris Masonbd681512011-07-16 15:23:14 -04003936 init_waitqueue_head(&eb->write_lock_wq);
3937 init_waitqueue_head(&eb->read_lock_wq);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003938
Chris Mason39351272009-02-04 09:24:05 -05003939#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003940 spin_lock_irqsave(&leak_lock, flags);
3941 list_add(&eb->leak_list, &buffers);
3942 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003943#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05003944 spin_lock_init(&eb->refs_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003945 atomic_set(&eb->refs, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003946 atomic_set(&eb->io_pages, 0);
Chris Mason727011e2010-08-06 13:21:20 -04003947
3948 if (len > MAX_INLINE_EXTENT_BUFFER_SIZE) {
3949 struct page **pages;
3950 int num_pages = (len + PAGE_CACHE_SIZE - 1) >>
3951 PAGE_CACHE_SHIFT;
3952 pages = kzalloc(num_pages, mask);
3953 if (!pages) {
3954 __free_extent_buffer(eb);
3955 return NULL;
3956 }
3957 eb->pages = pages;
3958 } else {
3959 eb->pages = eb->inline_pages;
3960 }
Chris Masond1310b22008-01-24 16:13:08 -05003961
3962 return eb;
3963}
3964
Jan Schmidt815a51c2012-05-16 17:00:02 +02003965struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src)
3966{
3967 unsigned long i;
3968 struct page *p;
3969 struct extent_buffer *new;
3970 unsigned long num_pages = num_extent_pages(src->start, src->len);
3971
3972 new = __alloc_extent_buffer(NULL, src->start, src->len, GFP_ATOMIC);
3973 if (new == NULL)
3974 return NULL;
3975
3976 for (i = 0; i < num_pages; i++) {
3977 p = alloc_page(GFP_ATOMIC);
3978 BUG_ON(!p);
3979 attach_extent_buffer_page(new, p);
3980 WARN_ON(PageDirty(p));
3981 SetPageUptodate(p);
3982 new->pages[i] = p;
3983 }
3984
3985 copy_extent_buffer(new, src, 0, 0, src->len);
3986 set_bit(EXTENT_BUFFER_UPTODATE, &new->bflags);
3987 set_bit(EXTENT_BUFFER_DUMMY, &new->bflags);
3988
3989 return new;
3990}
3991
3992struct extent_buffer *alloc_dummy_extent_buffer(u64 start, unsigned long len)
3993{
3994 struct extent_buffer *eb;
3995 unsigned long num_pages = num_extent_pages(0, len);
3996 unsigned long i;
3997
3998 eb = __alloc_extent_buffer(NULL, start, len, GFP_ATOMIC);
3999 if (!eb)
4000 return NULL;
4001
4002 for (i = 0; i < num_pages; i++) {
4003 eb->pages[i] = alloc_page(GFP_ATOMIC);
4004 if (!eb->pages[i])
4005 goto err;
4006 }
4007 set_extent_buffer_uptodate(eb);
4008 btrfs_set_header_nritems(eb, 0);
4009 set_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
4010
4011 return eb;
4012err:
4013 for (i--; i > 0; i--)
4014 __free_page(eb->pages[i]);
4015 __free_extent_buffer(eb);
4016 return NULL;
4017}
4018
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004019static int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004020{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004021 return (atomic_read(&eb->io_pages) ||
4022 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
4023 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05004024}
4025
Miao Xie897ca6e2010-10-26 20:57:29 -04004026/*
4027 * Helper for releasing extent buffer page.
4028 */
4029static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
4030 unsigned long start_idx)
4031{
4032 unsigned long index;
Wang Sheng-Hui39bab872012-04-06 14:35:31 +08004033 unsigned long num_pages;
Miao Xie897ca6e2010-10-26 20:57:29 -04004034 struct page *page;
Jan Schmidt815a51c2012-05-16 17:00:02 +02004035 int mapped = !test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags);
Miao Xie897ca6e2010-10-26 20:57:29 -04004036
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004037 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e2010-10-26 20:57:29 -04004038
Wang Sheng-Hui39bab872012-04-06 14:35:31 +08004039 num_pages = num_extent_pages(eb->start, eb->len);
4040 index = start_idx + num_pages;
Miao Xie897ca6e2010-10-26 20:57:29 -04004041 if (start_idx >= index)
4042 return;
4043
4044 do {
4045 index--;
4046 page = extent_buffer_page(eb, index);
Jan Schmidt815a51c2012-05-16 17:00:02 +02004047 if (page && mapped) {
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004048 spin_lock(&page->mapping->private_lock);
4049 /*
4050 * We do this since we'll remove the pages after we've
4051 * removed the eb from the radix tree, so we could race
4052 * and have this page now attached to the new eb. So
4053 * only clear page_private if it's still connected to
4054 * this eb.
4055 */
4056 if (PagePrivate(page) &&
4057 page->private == (unsigned long)eb) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004058 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Josef Bacik3083ee22012-03-09 16:01:49 -05004059 BUG_ON(PageDirty(page));
4060 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004061 /*
4062 * We need to make sure we haven't be attached
4063 * to a new eb.
4064 */
4065 ClearPagePrivate(page);
4066 set_page_private(page, 0);
4067 /* One for the page private */
4068 page_cache_release(page);
4069 }
4070 spin_unlock(&page->mapping->private_lock);
4071
Jan Schmidt815a51c2012-05-16 17:00:02 +02004072 }
4073 if (page) {
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004074 /* One for when we alloced the page */
Miao Xie897ca6e2010-10-26 20:57:29 -04004075 page_cache_release(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004076 }
Miao Xie897ca6e2010-10-26 20:57:29 -04004077 } while (index != start_idx);
4078}
4079
4080/*
4081 * Helper for releasing the extent buffer.
4082 */
4083static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4084{
4085 btrfs_release_extent_buffer_page(eb, 0);
4086 __free_extent_buffer(eb);
4087}
4088
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004089static void check_buffer_tree_ref(struct extent_buffer *eb)
4090{
4091 /* the ref bit is tricky. We have to make sure it is set
4092 * if we have the buffer dirty. Otherwise the
4093 * code to free a buffer can end up dropping a dirty
4094 * page
4095 *
4096 * Once the ref bit is set, it won't go away while the
4097 * buffer is dirty or in writeback, and it also won't
4098 * go away while we have the reference count on the
4099 * eb bumped.
4100 *
4101 * We can't just set the ref bit without bumping the
4102 * ref on the eb because free_extent_buffer might
4103 * see the ref bit and try to clear it. If this happens
4104 * free_extent_buffer might end up dropping our original
4105 * ref by mistake and freeing the page before we are able
4106 * to add one more ref.
4107 *
4108 * So bump the ref count first, then set the bit. If someone
4109 * beat us to it, drop the ref we added.
4110 */
4111 if (!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
4112 atomic_inc(&eb->refs);
4113 if (test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4114 atomic_dec(&eb->refs);
4115 }
4116}
4117
Josef Bacik5df42352012-03-15 18:24:42 -04004118static void mark_extent_buffer_accessed(struct extent_buffer *eb)
4119{
4120 unsigned long num_pages, i;
4121
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004122 check_buffer_tree_ref(eb);
4123
Josef Bacik5df42352012-03-15 18:24:42 -04004124 num_pages = num_extent_pages(eb->start, eb->len);
4125 for (i = 0; i < num_pages; i++) {
4126 struct page *p = extent_buffer_page(eb, i);
4127 mark_page_accessed(p);
4128 }
4129}
4130
Chris Masond1310b22008-01-24 16:13:08 -05004131struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
Chris Mason727011e2010-08-06 13:21:20 -04004132 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004133{
4134 unsigned long num_pages = num_extent_pages(start, len);
4135 unsigned long i;
4136 unsigned long index = start >> PAGE_CACHE_SHIFT;
4137 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004138 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004139 struct page *p;
4140 struct address_space *mapping = tree->mapping;
4141 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004142 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004143
Miao Xie19fe0a82010-10-26 20:57:29 -04004144 rcu_read_lock();
4145 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4146 if (eb && atomic_inc_not_zero(&eb->refs)) {
4147 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004148 mark_extent_buffer_accessed(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04004149 return eb;
4150 }
Miao Xie19fe0a82010-10-26 20:57:29 -04004151 rcu_read_unlock();
Chris Mason6af118ce2008-07-22 11:18:07 -04004152
David Sterbaba144192011-04-21 01:12:06 +02004153 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
Peter2b114d12008-04-01 11:21:40 -04004154 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004155 return NULL;
4156
Chris Mason727011e2010-08-06 13:21:20 -04004157 for (i = 0; i < num_pages; i++, index++) {
Chris Masona6591712011-07-19 12:04:14 -04004158 p = find_or_create_page(mapping, index, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05004159 if (!p) {
4160 WARN_ON(1);
Chris Mason6af118ce2008-07-22 11:18:07 -04004161 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05004162 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004163
4164 spin_lock(&mapping->private_lock);
4165 if (PagePrivate(p)) {
4166 /*
4167 * We could have already allocated an eb for this page
4168 * and attached one so lets see if we can get a ref on
4169 * the existing eb, and if we can we know it's good and
4170 * we can just return that one, else we know we can just
4171 * overwrite page->private.
4172 */
4173 exists = (struct extent_buffer *)p->private;
4174 if (atomic_inc_not_zero(&exists->refs)) {
4175 spin_unlock(&mapping->private_lock);
4176 unlock_page(p);
Josef Bacik17de39a2012-05-04 15:16:06 -04004177 page_cache_release(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004178 mark_extent_buffer_accessed(exists);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004179 goto free_eb;
4180 }
4181
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004182 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004183 * Do this so attach doesn't complain and we need to
4184 * drop the ref the old guy had.
4185 */
4186 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004187 WARN_ON(PageDirty(p));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004188 page_cache_release(p);
Chris Masond1310b22008-01-24 16:13:08 -05004189 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004190 attach_extent_buffer_page(eb, p);
4191 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004192 WARN_ON(PageDirty(p));
Chris Masond1310b22008-01-24 16:13:08 -05004193 mark_page_accessed(p);
Chris Mason727011e2010-08-06 13:21:20 -04004194 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05004195 if (!PageUptodate(p))
4196 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05004197
4198 /*
4199 * see below about how we avoid a nasty race with release page
4200 * and why we unlock later
4201 */
Chris Masond1310b22008-01-24 16:13:08 -05004202 }
4203 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004204 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05004205again:
Miao Xie19fe0a82010-10-26 20:57:29 -04004206 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4207 if (ret)
4208 goto free_eb;
4209
Chris Mason6af118ce2008-07-22 11:18:07 -04004210 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004211 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
4212 if (ret == -EEXIST) {
4213 exists = radix_tree_lookup(&tree->buffer,
4214 start >> PAGE_CACHE_SHIFT);
Josef Bacik115391d2012-03-09 09:51:43 -05004215 if (!atomic_inc_not_zero(&exists->refs)) {
4216 spin_unlock(&tree->buffer_lock);
4217 radix_tree_preload_end();
Josef Bacik115391d2012-03-09 09:51:43 -05004218 exists = NULL;
4219 goto again;
4220 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004221 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004222 radix_tree_preload_end();
Josef Bacik5df42352012-03-15 18:24:42 -04004223 mark_extent_buffer_accessed(exists);
Chris Mason6af118ce2008-07-22 11:18:07 -04004224 goto free_eb;
4225 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004226 /* add one reference for the tree */
Josef Bacik3083ee22012-03-09 16:01:49 -05004227 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004228 check_buffer_tree_ref(eb);
Josef Bacik3083ee22012-03-09 16:01:49 -05004229 spin_unlock(&eb->refs_lock);
Yan, Zhengf044ba72010-02-04 08:46:56 +00004230 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004231 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05004232
4233 /*
4234 * there is a race where release page may have
4235 * tried to find this extent buffer in the radix
4236 * but failed. It will tell the VM it is safe to
4237 * reclaim the, and it will clear the page private bit.
4238 * We must make sure to set the page private bit properly
4239 * after the extent buffer is in the radix tree so
4240 * it doesn't get lost
4241 */
Chris Mason727011e2010-08-06 13:21:20 -04004242 SetPageChecked(eb->pages[0]);
4243 for (i = 1; i < num_pages; i++) {
4244 p = extent_buffer_page(eb, i);
Chris Mason727011e2010-08-06 13:21:20 -04004245 ClearPageChecked(p);
4246 unlock_page(p);
4247 }
4248 unlock_page(eb->pages[0]);
Chris Masond1310b22008-01-24 16:13:08 -05004249 return eb;
4250
Chris Mason6af118ce2008-07-22 11:18:07 -04004251free_eb:
Chris Mason727011e2010-08-06 13:21:20 -04004252 for (i = 0; i < num_pages; i++) {
4253 if (eb->pages[i])
4254 unlock_page(eb->pages[i]);
4255 }
Chris Masoneb14ab82011-02-10 12:35:00 -05004256
Josef Bacik17de39a2012-05-04 15:16:06 -04004257 WARN_ON(!atomic_dec_and_test(&eb->refs));
Miao Xie897ca6e2010-10-26 20:57:29 -04004258 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04004259 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05004260}
Chris Masond1310b22008-01-24 16:13:08 -05004261
4262struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
David Sterbaf09d1f62011-04-21 01:08:01 +02004263 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004264{
Chris Masond1310b22008-01-24 16:13:08 -05004265 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05004266
Miao Xie19fe0a82010-10-26 20:57:29 -04004267 rcu_read_lock();
4268 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4269 if (eb && atomic_inc_not_zero(&eb->refs)) {
4270 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004271 mark_extent_buffer_accessed(eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04004272 return eb;
4273 }
4274 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04004275
Miao Xie19fe0a82010-10-26 20:57:29 -04004276 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004277}
Chris Masond1310b22008-01-24 16:13:08 -05004278
Josef Bacik3083ee22012-03-09 16:01:49 -05004279static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4280{
4281 struct extent_buffer *eb =
4282 container_of(head, struct extent_buffer, rcu_head);
4283
4284 __free_extent_buffer(eb);
4285}
4286
Josef Bacik3083ee22012-03-09 16:01:49 -05004287/* Expects to have eb->eb_lock already held */
4288static void release_extent_buffer(struct extent_buffer *eb, gfp_t mask)
4289{
4290 WARN_ON(atomic_read(&eb->refs) == 0);
4291 if (atomic_dec_and_test(&eb->refs)) {
Jan Schmidt815a51c2012-05-16 17:00:02 +02004292 if (test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags)) {
4293 spin_unlock(&eb->refs_lock);
4294 } else {
4295 struct extent_io_tree *tree = eb->tree;
Josef Bacik3083ee22012-03-09 16:01:49 -05004296
Jan Schmidt815a51c2012-05-16 17:00:02 +02004297 spin_unlock(&eb->refs_lock);
Josef Bacik3083ee22012-03-09 16:01:49 -05004298
Jan Schmidt815a51c2012-05-16 17:00:02 +02004299 spin_lock(&tree->buffer_lock);
4300 radix_tree_delete(&tree->buffer,
4301 eb->start >> PAGE_CACHE_SHIFT);
4302 spin_unlock(&tree->buffer_lock);
4303 }
Josef Bacik3083ee22012-03-09 16:01:49 -05004304
4305 /* Should be safe to release our pages at this point */
4306 btrfs_release_extent_buffer_page(eb, 0);
4307
4308 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
4309 return;
4310 }
4311 spin_unlock(&eb->refs_lock);
4312}
4313
Chris Masond1310b22008-01-24 16:13:08 -05004314void free_extent_buffer(struct extent_buffer *eb)
4315{
Chris Masond1310b22008-01-24 16:13:08 -05004316 if (!eb)
4317 return;
4318
Josef Bacik3083ee22012-03-09 16:01:49 -05004319 spin_lock(&eb->refs_lock);
4320 if (atomic_read(&eb->refs) == 2 &&
Jan Schmidt815a51c2012-05-16 17:00:02 +02004321 test_bit(EXTENT_BUFFER_DUMMY, &eb->bflags))
4322 atomic_dec(&eb->refs);
4323
4324 if (atomic_read(&eb->refs) == 2 &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004325 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004326 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004327 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4328 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05004329
Josef Bacik3083ee22012-03-09 16:01:49 -05004330 /*
4331 * I know this is terrible, but it's temporary until we stop tracking
4332 * the uptodate bits and such for the extent buffers.
4333 */
4334 release_extent_buffer(eb, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05004335}
Chris Masond1310b22008-01-24 16:13:08 -05004336
Josef Bacik3083ee22012-03-09 16:01:49 -05004337void free_extent_buffer_stale(struct extent_buffer *eb)
4338{
4339 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004340 return;
4341
Josef Bacik3083ee22012-03-09 16:01:49 -05004342 spin_lock(&eb->refs_lock);
4343 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
4344
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004345 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004346 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4347 atomic_dec(&eb->refs);
4348 release_extent_buffer(eb, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05004349}
4350
Chris Mason1d4284b2012-03-28 20:31:37 -04004351void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004352{
Chris Masond1310b22008-01-24 16:13:08 -05004353 unsigned long i;
4354 unsigned long num_pages;
4355 struct page *page;
4356
Chris Masond1310b22008-01-24 16:13:08 -05004357 num_pages = num_extent_pages(eb->start, eb->len);
4358
4359 for (i = 0; i < num_pages; i++) {
4360 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04004361 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05004362 continue;
4363
Chris Masona61e6f22008-07-22 11:18:08 -04004364 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05004365 WARN_ON(!PagePrivate(page));
4366
Chris Masond1310b22008-01-24 16:13:08 -05004367 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004368 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05004369 if (!PageDirty(page)) {
4370 radix_tree_tag_clear(&page->mapping->page_tree,
4371 page_index(page),
4372 PAGECACHE_TAG_DIRTY);
4373 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004374 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04004375 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04004376 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05004377 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004378 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05004379}
Chris Masond1310b22008-01-24 16:13:08 -05004380
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004381int set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004382{
4383 unsigned long i;
4384 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04004385 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004386
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004387 check_buffer_tree_ref(eb);
4388
Chris Masonb9473432009-03-13 11:00:37 -04004389 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004390
Chris Masond1310b22008-01-24 16:13:08 -05004391 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik3083ee22012-03-09 16:01:49 -05004392 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004393 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
4394
Chris Masonb9473432009-03-13 11:00:37 -04004395 for (i = 0; i < num_pages; i++)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004396 set_page_dirty(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04004397 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05004398}
Chris Masond1310b22008-01-24 16:13:08 -05004399
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004400static int range_straddles_pages(u64 start, u64 len)
Chris Mason19b6caf2011-07-25 06:50:50 -04004401{
4402 if (len < PAGE_CACHE_SIZE)
4403 return 1;
4404 if (start & (PAGE_CACHE_SIZE - 1))
4405 return 1;
4406 if ((start + len) & (PAGE_CACHE_SIZE - 1))
4407 return 1;
4408 return 0;
4409}
4410
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004411int clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04004412{
4413 unsigned long i;
4414 struct page *page;
4415 unsigned long num_pages;
4416
Chris Masonb4ce94d2009-02-04 09:25:08 -05004417 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004418 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason1259ab72008-05-12 13:39:03 -04004419 for (i = 0; i < num_pages; i++) {
4420 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04004421 if (page)
4422 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04004423 }
4424 return 0;
4425}
4426
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004427int set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004428{
4429 unsigned long i;
4430 struct page *page;
4431 unsigned long num_pages;
4432
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004433 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004434 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05004435 for (i = 0; i < num_pages; i++) {
4436 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004437 SetPageUptodate(page);
4438 }
4439 return 0;
4440}
Chris Masond1310b22008-01-24 16:13:08 -05004441
Chris Masonce9adaa2008-04-09 16:28:12 -04004442int extent_range_uptodate(struct extent_io_tree *tree,
4443 u64 start, u64 end)
4444{
4445 struct page *page;
4446 int ret;
4447 int pg_uptodate = 1;
4448 int uptodate;
4449 unsigned long index;
4450
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004451 if (range_straddles_pages(start, end - start + 1)) {
Chris Mason19b6caf2011-07-25 06:50:50 -04004452 ret = test_range_bit(tree, start, end,
4453 EXTENT_UPTODATE, 1, NULL);
4454 if (ret)
4455 return 1;
4456 }
Chris Masond3977122009-01-05 21:25:51 -05004457 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004458 index = start >> PAGE_CACHE_SHIFT;
4459 page = find_get_page(tree->mapping, index);
Mitch Harder8bedd512012-01-26 15:01:11 -05004460 if (!page)
4461 return 1;
Chris Masonce9adaa2008-04-09 16:28:12 -04004462 uptodate = PageUptodate(page);
4463 page_cache_release(page);
4464 if (!uptodate) {
4465 pg_uptodate = 0;
4466 break;
4467 }
4468 start += PAGE_CACHE_SIZE;
4469 }
4470 return pg_uptodate;
4471}
4472
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004473int extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004474{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004475 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004476}
Chris Masond1310b22008-01-24 16:13:08 -05004477
4478int read_extent_buffer_pages(struct extent_io_tree *tree,
Arne Jansenbb82ab82011-06-10 14:06:53 +02004479 struct extent_buffer *eb, u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04004480 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05004481{
4482 unsigned long i;
4483 unsigned long start_i;
4484 struct page *page;
4485 int err;
4486 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04004487 int locked_pages = 0;
4488 int all_uptodate = 1;
Chris Masond1310b22008-01-24 16:13:08 -05004489 unsigned long num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04004490 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004491 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004492 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004493
Chris Masonb4ce94d2009-02-04 09:25:08 -05004494 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05004495 return 0;
4496
Chris Masond1310b22008-01-24 16:13:08 -05004497 if (start) {
4498 WARN_ON(start < eb->start);
4499 start_i = (start >> PAGE_CACHE_SHIFT) -
4500 (eb->start >> PAGE_CACHE_SHIFT);
4501 } else {
4502 start_i = 0;
4503 }
4504
4505 num_pages = num_extent_pages(eb->start, eb->len);
4506 for (i = start_i; i < num_pages; i++) {
4507 page = extent_buffer_page(eb, i);
Arne Jansenbb82ab82011-06-10 14:06:53 +02004508 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04004509 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04004510 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05004511 } else {
4512 lock_page(page);
4513 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004514 locked_pages++;
Chris Mason727011e2010-08-06 13:21:20 -04004515 if (!PageUptodate(page)) {
4516 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04004517 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04004518 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004519 }
4520 if (all_uptodate) {
4521 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004522 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04004523 goto unlock_exit;
4524 }
4525
Josef Bacikea466792012-03-26 21:57:36 -04004526 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04004527 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004528 atomic_set(&eb->io_pages, num_reads);
Chris Masonce9adaa2008-04-09 16:28:12 -04004529 for (i = start_i; i < num_pages; i++) {
4530 page = extent_buffer_page(eb, i);
Chris Masonce9adaa2008-04-09 16:28:12 -04004531 if (!PageUptodate(page)) {
Chris Masonf1885912008-04-09 16:28:12 -04004532 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05004533 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04004534 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04004535 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05004536 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05004537 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05004538 } else {
4539 unlock_page(page);
4540 }
4541 }
4542
Jeff Mahoney355808c2011-10-03 23:23:14 -04004543 if (bio) {
4544 err = submit_one_bio(READ, bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004545 if (err)
4546 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04004547 }
Chris Masona86c12c2008-02-07 10:50:54 -05004548
Arne Jansenbb82ab82011-06-10 14:06:53 +02004549 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05004550 return ret;
Chris Masond3977122009-01-05 21:25:51 -05004551
Chris Masond1310b22008-01-24 16:13:08 -05004552 for (i = start_i; i < num_pages; i++) {
4553 page = extent_buffer_page(eb, i);
4554 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05004555 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05004556 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05004557 }
Chris Masond3977122009-01-05 21:25:51 -05004558
Chris Masond1310b22008-01-24 16:13:08 -05004559 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04004560
4561unlock_exit:
4562 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05004563 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004564 page = extent_buffer_page(eb, i);
4565 i++;
4566 unlock_page(page);
4567 locked_pages--;
4568 }
4569 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004570}
Chris Masond1310b22008-01-24 16:13:08 -05004571
4572void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4573 unsigned long start,
4574 unsigned long len)
4575{
4576 size_t cur;
4577 size_t offset;
4578 struct page *page;
4579 char *kaddr;
4580 char *dst = (char *)dstv;
4581 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4582 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004583
4584 WARN_ON(start > eb->len);
4585 WARN_ON(start + len > eb->start + eb->len);
4586
4587 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4588
Chris Masond3977122009-01-05 21:25:51 -05004589 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004590 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004591
4592 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04004593 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004594 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004595
4596 dst += cur;
4597 len -= cur;
4598 offset = 0;
4599 i++;
4600 }
4601}
Chris Masond1310b22008-01-24 16:13:08 -05004602
4603int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04004604 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05004605 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04004606 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05004607{
4608 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4609 char *kaddr;
4610 struct page *p;
4611 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4612 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4613 unsigned long end_i = (start_offset + start + min_len - 1) >>
4614 PAGE_CACHE_SHIFT;
4615
4616 if (i != end_i)
4617 return -EINVAL;
4618
4619 if (i == 0) {
4620 offset = start_offset;
4621 *map_start = 0;
4622 } else {
4623 offset = 0;
4624 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4625 }
Chris Masond3977122009-01-05 21:25:51 -05004626
Chris Masond1310b22008-01-24 16:13:08 -05004627 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05004628 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
4629 "wanted %lu %lu\n", (unsigned long long)eb->start,
4630 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05004631 WARN_ON(1);
Josef Bacik850265332011-03-15 14:52:12 -04004632 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05004633 }
4634
4635 p = extent_buffer_page(eb, i);
Chris Masona6591712011-07-19 12:04:14 -04004636 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05004637 *map = kaddr + offset;
4638 *map_len = PAGE_CACHE_SIZE - offset;
4639 return 0;
4640}
Chris Masond1310b22008-01-24 16:13:08 -05004641
Chris Masond1310b22008-01-24 16:13:08 -05004642int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4643 unsigned long start,
4644 unsigned long len)
4645{
4646 size_t cur;
4647 size_t offset;
4648 struct page *page;
4649 char *kaddr;
4650 char *ptr = (char *)ptrv;
4651 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4652 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4653 int ret = 0;
4654
4655 WARN_ON(start > eb->len);
4656 WARN_ON(start + len > eb->start + eb->len);
4657
4658 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4659
Chris Masond3977122009-01-05 21:25:51 -05004660 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004661 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004662
4663 cur = min(len, (PAGE_CACHE_SIZE - offset));
4664
Chris Masona6591712011-07-19 12:04:14 -04004665 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004666 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004667 if (ret)
4668 break;
4669
4670 ptr += cur;
4671 len -= cur;
4672 offset = 0;
4673 i++;
4674 }
4675 return ret;
4676}
Chris Masond1310b22008-01-24 16:13:08 -05004677
4678void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4679 unsigned long start, unsigned long len)
4680{
4681 size_t cur;
4682 size_t offset;
4683 struct page *page;
4684 char *kaddr;
4685 char *src = (char *)srcv;
4686 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4687 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4688
4689 WARN_ON(start > eb->len);
4690 WARN_ON(start + len > eb->start + eb->len);
4691
4692 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4693
Chris Masond3977122009-01-05 21:25:51 -05004694 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004695 page = extent_buffer_page(eb, i);
4696 WARN_ON(!PageUptodate(page));
4697
4698 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004699 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004700 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004701
4702 src += cur;
4703 len -= cur;
4704 offset = 0;
4705 i++;
4706 }
4707}
Chris Masond1310b22008-01-24 16:13:08 -05004708
4709void memset_extent_buffer(struct extent_buffer *eb, char c,
4710 unsigned long start, unsigned long len)
4711{
4712 size_t cur;
4713 size_t offset;
4714 struct page *page;
4715 char *kaddr;
4716 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4717 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4718
4719 WARN_ON(start > eb->len);
4720 WARN_ON(start + len > eb->start + eb->len);
4721
4722 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4723
Chris Masond3977122009-01-05 21:25:51 -05004724 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004725 page = extent_buffer_page(eb, i);
4726 WARN_ON(!PageUptodate(page));
4727
4728 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004729 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004730 memset(kaddr + offset, c, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004731
4732 len -= cur;
4733 offset = 0;
4734 i++;
4735 }
4736}
Chris Masond1310b22008-01-24 16:13:08 -05004737
4738void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4739 unsigned long dst_offset, unsigned long src_offset,
4740 unsigned long len)
4741{
4742 u64 dst_len = dst->len;
4743 size_t cur;
4744 size_t offset;
4745 struct page *page;
4746 char *kaddr;
4747 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4748 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4749
4750 WARN_ON(src->len != dst_len);
4751
4752 offset = (start_offset + dst_offset) &
4753 ((unsigned long)PAGE_CACHE_SIZE - 1);
4754
Chris Masond3977122009-01-05 21:25:51 -05004755 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004756 page = extent_buffer_page(dst, i);
4757 WARN_ON(!PageUptodate(page));
4758
4759 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4760
Chris Masona6591712011-07-19 12:04:14 -04004761 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004762 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004763
4764 src_offset += cur;
4765 len -= cur;
4766 offset = 0;
4767 i++;
4768 }
4769}
Chris Masond1310b22008-01-24 16:13:08 -05004770
4771static void move_pages(struct page *dst_page, struct page *src_page,
4772 unsigned long dst_off, unsigned long src_off,
4773 unsigned long len)
4774{
Chris Masona6591712011-07-19 12:04:14 -04004775 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004776 if (dst_page == src_page) {
4777 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4778 } else {
Chris Masona6591712011-07-19 12:04:14 -04004779 char *src_kaddr = page_address(src_page);
Chris Masond1310b22008-01-24 16:13:08 -05004780 char *p = dst_kaddr + dst_off + len;
4781 char *s = src_kaddr + src_off + len;
4782
4783 while (len--)
4784 *--p = *--s;
Chris Masond1310b22008-01-24 16:13:08 -05004785 }
Chris Masond1310b22008-01-24 16:13:08 -05004786}
4787
Sergei Trofimovich33872062011-04-11 21:52:52 +00004788static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4789{
4790 unsigned long distance = (src > dst) ? src - dst : dst - src;
4791 return distance < len;
4792}
4793
Chris Masond1310b22008-01-24 16:13:08 -05004794static void copy_pages(struct page *dst_page, struct page *src_page,
4795 unsigned long dst_off, unsigned long src_off,
4796 unsigned long len)
4797{
Chris Masona6591712011-07-19 12:04:14 -04004798 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004799 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004800 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004801
Sergei Trofimovich33872062011-04-11 21:52:52 +00004802 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04004803 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00004804 } else {
Chris Masond1310b22008-01-24 16:13:08 -05004805 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004806 if (areas_overlap(src_off, dst_off, len))
4807 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00004808 }
Chris Masond1310b22008-01-24 16:13:08 -05004809
Chris Mason727011e2010-08-06 13:21:20 -04004810 if (must_memmove)
4811 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
4812 else
4813 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05004814}
4815
4816void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4817 unsigned long src_offset, unsigned long len)
4818{
4819 size_t cur;
4820 size_t dst_off_in_page;
4821 size_t src_off_in_page;
4822 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4823 unsigned long dst_i;
4824 unsigned long src_i;
4825
4826 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004827 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4828 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004829 BUG_ON(1);
4830 }
4831 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004832 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4833 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004834 BUG_ON(1);
4835 }
4836
Chris Masond3977122009-01-05 21:25:51 -05004837 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004838 dst_off_in_page = (start_offset + dst_offset) &
4839 ((unsigned long)PAGE_CACHE_SIZE - 1);
4840 src_off_in_page = (start_offset + src_offset) &
4841 ((unsigned long)PAGE_CACHE_SIZE - 1);
4842
4843 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4844 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4845
4846 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4847 src_off_in_page));
4848 cur = min_t(unsigned long, cur,
4849 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4850
4851 copy_pages(extent_buffer_page(dst, dst_i),
4852 extent_buffer_page(dst, src_i),
4853 dst_off_in_page, src_off_in_page, cur);
4854
4855 src_offset += cur;
4856 dst_offset += cur;
4857 len -= cur;
4858 }
4859}
Chris Masond1310b22008-01-24 16:13:08 -05004860
4861void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4862 unsigned long src_offset, unsigned long len)
4863{
4864 size_t cur;
4865 size_t dst_off_in_page;
4866 size_t src_off_in_page;
4867 unsigned long dst_end = dst_offset + len - 1;
4868 unsigned long src_end = src_offset + len - 1;
4869 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4870 unsigned long dst_i;
4871 unsigned long src_i;
4872
4873 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004874 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4875 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004876 BUG_ON(1);
4877 }
4878 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004879 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4880 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004881 BUG_ON(1);
4882 }
Chris Mason727011e2010-08-06 13:21:20 -04004883 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05004884 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4885 return;
4886 }
Chris Masond3977122009-01-05 21:25:51 -05004887 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004888 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
4889 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
4890
4891 dst_off_in_page = (start_offset + dst_end) &
4892 ((unsigned long)PAGE_CACHE_SIZE - 1);
4893 src_off_in_page = (start_offset + src_end) &
4894 ((unsigned long)PAGE_CACHE_SIZE - 1);
4895
4896 cur = min_t(unsigned long, len, src_off_in_page + 1);
4897 cur = min(cur, dst_off_in_page + 1);
4898 move_pages(extent_buffer_page(dst, dst_i),
4899 extent_buffer_page(dst, src_i),
4900 dst_off_in_page - cur + 1,
4901 src_off_in_page - cur + 1, cur);
4902
4903 dst_end -= cur;
4904 src_end -= cur;
4905 len -= cur;
4906 }
4907}
Chris Mason6af118ce2008-07-22 11:18:07 -04004908
Josef Bacik3083ee22012-03-09 16:01:49 -05004909int try_release_extent_buffer(struct page *page, gfp_t mask)
Miao Xie19fe0a82010-10-26 20:57:29 -04004910{
Chris Mason6af118ce2008-07-22 11:18:07 -04004911 struct extent_buffer *eb;
Miao Xie897ca6e2010-10-26 20:57:29 -04004912
Miao Xie19fe0a82010-10-26 20:57:29 -04004913 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05004914 * We need to make sure noboody is attaching this page to an eb right
4915 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04004916 */
Josef Bacik3083ee22012-03-09 16:01:49 -05004917 spin_lock(&page->mapping->private_lock);
4918 if (!PagePrivate(page)) {
4919 spin_unlock(&page->mapping->private_lock);
4920 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004921 }
4922
Josef Bacik3083ee22012-03-09 16:01:49 -05004923 eb = (struct extent_buffer *)page->private;
4924 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04004925
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004926 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05004927 * This is a little awful but should be ok, we need to make sure that
4928 * the eb doesn't disappear out from under us while we're looking at
4929 * this page.
4930 */
4931 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004932 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05004933 spin_unlock(&eb->refs_lock);
4934 spin_unlock(&page->mapping->private_lock);
4935 return 0;
4936 }
4937 spin_unlock(&page->mapping->private_lock);
4938
4939 if ((mask & GFP_NOFS) == GFP_NOFS)
4940 mask = GFP_NOFS;
4941
4942 /*
4943 * If tree ref isn't set then we know the ref on this eb is a real ref,
4944 * so just return, this page will likely be freed soon anyway.
4945 */
4946 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
4947 spin_unlock(&eb->refs_lock);
4948 return 0;
4949 }
4950 release_extent_buffer(eb, mask);
4951
4952 return 1;
Chris Mason6af118ce2008-07-22 11:18:07 -04004953}