blob: 69a527c7a0b3d9e560d99a567b9e0ef4f6595ffe [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);
1916 return -EIO;
1917 }
1918
1919 printk(KERN_INFO "btrfs read error corrected: ino %lu off %llu (dev %s "
1920 "sector %llu)\n", page->mapping->host->i_ino, start,
1921 dev->name, sector);
1922
1923 bio_put(bio);
1924 return 0;
1925}
1926
Josef Bacikea466792012-03-26 21:57:36 -04001927int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
1928 int mirror_num)
1929{
1930 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1931 u64 start = eb->start;
1932 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond95603b2012-04-12 15:55:15 -04001933 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04001934
1935 for (i = 0; i < num_pages; i++) {
1936 struct page *p = extent_buffer_page(eb, i);
1937 ret = repair_io_failure(map_tree, start, PAGE_CACHE_SIZE,
1938 start, p, mirror_num);
1939 if (ret)
1940 break;
1941 start += PAGE_CACHE_SIZE;
1942 }
1943
1944 return ret;
1945}
1946
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001947/*
1948 * each time an IO finishes, we do a fast check in the IO failure tree
1949 * to see if we need to process or clean up an io_failure_record
1950 */
1951static int clean_io_failure(u64 start, struct page *page)
1952{
1953 u64 private;
1954 u64 private_failure;
1955 struct io_failure_record *failrec;
1956 struct btrfs_mapping_tree *map_tree;
1957 struct extent_state *state;
1958 int num_copies;
1959 int did_repair = 0;
1960 int ret;
1961 struct inode *inode = page->mapping->host;
1962
1963 private = 0;
1964 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1965 (u64)-1, 1, EXTENT_DIRTY, 0);
1966 if (!ret)
1967 return 0;
1968
1969 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
1970 &private_failure);
1971 if (ret)
1972 return 0;
1973
1974 failrec = (struct io_failure_record *)(unsigned long) private_failure;
1975 BUG_ON(!failrec->this_mirror);
1976
1977 if (failrec->in_validation) {
1978 /* there was no real error, just free the record */
1979 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
1980 failrec->start);
1981 did_repair = 1;
1982 goto out;
1983 }
1984
1985 spin_lock(&BTRFS_I(inode)->io_tree.lock);
1986 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1987 failrec->start,
1988 EXTENT_LOCKED);
1989 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
1990
1991 if (state && state->start == failrec->start) {
1992 map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree;
1993 num_copies = btrfs_num_copies(map_tree, failrec->logical,
1994 failrec->len);
1995 if (num_copies > 1) {
1996 ret = repair_io_failure(map_tree, start, failrec->len,
1997 failrec->logical, page,
1998 failrec->failed_mirror);
1999 did_repair = !ret;
2000 }
2001 }
2002
2003out:
2004 if (!ret)
2005 ret = free_io_failure(inode, failrec, did_repair);
2006
2007 return ret;
2008}
2009
2010/*
2011 * this is a generic handler for readpage errors (default
2012 * readpage_io_failed_hook). if other copies exist, read those and write back
2013 * good data to the failed position. does not investigate in remapping the
2014 * failed extent elsewhere, hoping the device will be smart enough to do this as
2015 * needed
2016 */
2017
2018static int bio_readpage_error(struct bio *failed_bio, struct page *page,
2019 u64 start, u64 end, int failed_mirror,
2020 struct extent_state *state)
2021{
2022 struct io_failure_record *failrec = NULL;
2023 u64 private;
2024 struct extent_map *em;
2025 struct inode *inode = page->mapping->host;
2026 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2027 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2028 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2029 struct bio *bio;
2030 int num_copies;
2031 int ret;
2032 int read_mode;
2033 u64 logical;
2034
2035 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2036
2037 ret = get_state_private(failure_tree, start, &private);
2038 if (ret) {
2039 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2040 if (!failrec)
2041 return -ENOMEM;
2042 failrec->start = start;
2043 failrec->len = end - start + 1;
2044 failrec->this_mirror = 0;
2045 failrec->bio_flags = 0;
2046 failrec->in_validation = 0;
2047
2048 read_lock(&em_tree->lock);
2049 em = lookup_extent_mapping(em_tree, start, failrec->len);
2050 if (!em) {
2051 read_unlock(&em_tree->lock);
2052 kfree(failrec);
2053 return -EIO;
2054 }
2055
2056 if (em->start > start || em->start + em->len < start) {
2057 free_extent_map(em);
2058 em = NULL;
2059 }
2060 read_unlock(&em_tree->lock);
2061
2062 if (!em || IS_ERR(em)) {
2063 kfree(failrec);
2064 return -EIO;
2065 }
2066 logical = start - em->start;
2067 logical = em->block_start + logical;
2068 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2069 logical = em->block_start;
2070 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2071 extent_set_compress_type(&failrec->bio_flags,
2072 em->compress_type);
2073 }
2074 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2075 "len=%llu\n", logical, start, failrec->len);
2076 failrec->logical = logical;
2077 free_extent_map(em);
2078
2079 /* set the bits in the private failure tree */
2080 ret = set_extent_bits(failure_tree, start, end,
2081 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2082 if (ret >= 0)
2083 ret = set_state_private(failure_tree, start,
2084 (u64)(unsigned long)failrec);
2085 /* set the bits in the inode's tree */
2086 if (ret >= 0)
2087 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2088 GFP_NOFS);
2089 if (ret < 0) {
2090 kfree(failrec);
2091 return ret;
2092 }
2093 } else {
2094 failrec = (struct io_failure_record *)(unsigned long)private;
2095 pr_debug("bio_readpage_error: (found) logical=%llu, "
2096 "start=%llu, len=%llu, validation=%d\n",
2097 failrec->logical, failrec->start, failrec->len,
2098 failrec->in_validation);
2099 /*
2100 * when data can be on disk more than twice, add to failrec here
2101 * (e.g. with a list for failed_mirror) to make
2102 * clean_io_failure() clean all those errors at once.
2103 */
2104 }
2105 num_copies = btrfs_num_copies(
2106 &BTRFS_I(inode)->root->fs_info->mapping_tree,
2107 failrec->logical, failrec->len);
2108 if (num_copies == 1) {
2109 /*
2110 * we only have a single copy of the data, so don't bother with
2111 * all the retry and error correction code that follows. no
2112 * matter what the error is, it is very likely to persist.
2113 */
2114 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2115 "state=%p, num_copies=%d, next_mirror %d, "
2116 "failed_mirror %d\n", state, num_copies,
2117 failrec->this_mirror, failed_mirror);
2118 free_io_failure(inode, failrec, 0);
2119 return -EIO;
2120 }
2121
2122 if (!state) {
2123 spin_lock(&tree->lock);
2124 state = find_first_extent_bit_state(tree, failrec->start,
2125 EXTENT_LOCKED);
2126 if (state && state->start != failrec->start)
2127 state = NULL;
2128 spin_unlock(&tree->lock);
2129 }
2130
2131 /*
2132 * there are two premises:
2133 * a) deliver good data to the caller
2134 * b) correct the bad sectors on disk
2135 */
2136 if (failed_bio->bi_vcnt > 1) {
2137 /*
2138 * to fulfill b), we need to know the exact failing sectors, as
2139 * we don't want to rewrite any more than the failed ones. thus,
2140 * we need separate read requests for the failed bio
2141 *
2142 * if the following BUG_ON triggers, our validation request got
2143 * merged. we need separate requests for our algorithm to work.
2144 */
2145 BUG_ON(failrec->in_validation);
2146 failrec->in_validation = 1;
2147 failrec->this_mirror = failed_mirror;
2148 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2149 } else {
2150 /*
2151 * we're ready to fulfill a) and b) alongside. get a good copy
2152 * of the failed sector and if we succeed, we have setup
2153 * everything for repair_io_failure to do the rest for us.
2154 */
2155 if (failrec->in_validation) {
2156 BUG_ON(failrec->this_mirror != failed_mirror);
2157 failrec->in_validation = 0;
2158 failrec->this_mirror = 0;
2159 }
2160 failrec->failed_mirror = failed_mirror;
2161 failrec->this_mirror++;
2162 if (failrec->this_mirror == failed_mirror)
2163 failrec->this_mirror++;
2164 read_mode = READ_SYNC;
2165 }
2166
2167 if (!state || failrec->this_mirror > num_copies) {
2168 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2169 "next_mirror %d, failed_mirror %d\n", state,
2170 num_copies, failrec->this_mirror, failed_mirror);
2171 free_io_failure(inode, failrec, 0);
2172 return -EIO;
2173 }
2174
2175 bio = bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04002176 if (!bio) {
2177 free_io_failure(inode, failrec, 0);
2178 return -EIO;
2179 }
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002180 bio->bi_private = state;
2181 bio->bi_end_io = failed_bio->bi_end_io;
2182 bio->bi_sector = failrec->logical >> 9;
2183 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2184 bio->bi_size = 0;
2185
2186 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2187
2188 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2189 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2190 failrec->this_mirror, num_copies, failrec->in_validation);
2191
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002192 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2193 failrec->this_mirror,
2194 failrec->bio_flags, 0);
2195 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002196}
2197
Chris Masond1310b22008-01-24 16:13:08 -05002198/* lots and lots of room for performance fixes in the end_bio funcs */
2199
Jeff Mahoney87826df2012-02-15 16:23:57 +01002200int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2201{
2202 int uptodate = (err == 0);
2203 struct extent_io_tree *tree;
2204 int ret;
2205
2206 tree = &BTRFS_I(page->mapping->host)->io_tree;
2207
2208 if (tree->ops && tree->ops->writepage_end_io_hook) {
2209 ret = tree->ops->writepage_end_io_hook(page, start,
2210 end, NULL, uptodate);
2211 if (ret)
2212 uptodate = 0;
2213 }
2214
Jeff Mahoney87826df2012-02-15 16:23:57 +01002215 if (!uptodate) {
Jeff Mahoney87826df2012-02-15 16:23:57 +01002216 ClearPageUptodate(page);
2217 SetPageError(page);
2218 }
2219 return 0;
2220}
2221
Chris Masond1310b22008-01-24 16:13:08 -05002222/*
2223 * after a writepage IO is done, we need to:
2224 * clear the uptodate bits on error
2225 * clear the writeback bits in the extent tree for this IO
2226 * end_page_writeback if the page has no more pending IO
2227 *
2228 * Scheduling is not allowed, so the extent state tree is expected
2229 * to have one and only one object corresponding to this IO.
2230 */
Chris Masond1310b22008-01-24 16:13:08 -05002231static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002232{
Chris Masond1310b22008-01-24 16:13:08 -05002233 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04002234 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002235 u64 start;
2236 u64 end;
2237 int whole_page;
2238
Chris Masond1310b22008-01-24 16:13:08 -05002239 do {
2240 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04002241 tree = &BTRFS_I(page->mapping->host)->io_tree;
2242
Chris Masond1310b22008-01-24 16:13:08 -05002243 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2244 bvec->bv_offset;
2245 end = start + bvec->bv_len - 1;
2246
2247 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2248 whole_page = 1;
2249 else
2250 whole_page = 0;
2251
2252 if (--bvec >= bio->bi_io_vec)
2253 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04002254
Jeff Mahoney87826df2012-02-15 16:23:57 +01002255 if (end_extent_writepage(page, err, start, end))
2256 continue;
Chris Mason70dec802008-01-29 09:59:12 -05002257
Chris Masond1310b22008-01-24 16:13:08 -05002258 if (whole_page)
2259 end_page_writeback(page);
2260 else
2261 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002262 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04002263
Chris Masond1310b22008-01-24 16:13:08 -05002264 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002265}
2266
2267/*
2268 * after a readpage IO is done, we need to:
2269 * clear the uptodate bits on error
2270 * set the uptodate bits if things worked
2271 * set the page up to date if all extents in the tree are uptodate
2272 * clear the lock bit in the extent tree
2273 * unlock the page if there are no other extents locked for it
2274 *
2275 * Scheduling is not allowed, so the extent state tree is expected
2276 * to have one and only one object corresponding to this IO.
2277 */
Chris Masond1310b22008-01-24 16:13:08 -05002278static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002279{
2280 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00002281 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2282 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04002283 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002284 u64 start;
2285 u64 end;
2286 int whole_page;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002287 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002288 int ret;
2289
Chris Masond20f7042008-12-08 16:58:54 -05002290 if (err)
2291 uptodate = 0;
2292
Chris Masond1310b22008-01-24 16:13:08 -05002293 do {
2294 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00002295 struct extent_state *cached = NULL;
2296 struct extent_state *state;
2297
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002298 pr_debug("end_bio_extent_readpage: bi_vcnt=%d, idx=%d, err=%d, "
2299 "mirror=%ld\n", bio->bi_vcnt, bio->bi_idx, err,
2300 (long int)bio->bi_bdev);
David Woodhouse902b22f2008-08-20 08:51:49 -04002301 tree = &BTRFS_I(page->mapping->host)->io_tree;
2302
Chris Masond1310b22008-01-24 16:13:08 -05002303 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2304 bvec->bv_offset;
2305 end = start + bvec->bv_len - 1;
2306
2307 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2308 whole_page = 1;
2309 else
2310 whole_page = 0;
2311
Chris Mason4125bf72010-02-03 18:18:45 +00002312 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05002313 prefetchw(&bvec->bv_page->flags);
2314
Arne Jansen507903b2011-04-06 10:02:20 +00002315 spin_lock(&tree->lock);
Chris Mason0d399202011-04-16 06:55:39 -04002316 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
Chris Mason109b36a2011-04-12 13:57:39 -04002317 if (state && state->start == start) {
Arne Jansen507903b2011-04-06 10:02:20 +00002318 /*
2319 * take a reference on the state, unlock will drop
2320 * the ref
2321 */
2322 cache_state(state, &cached);
2323 }
2324 spin_unlock(&tree->lock);
2325
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002326 mirror = (int)(unsigned long)bio->bi_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002327 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05002328 ret = tree->ops->readpage_end_io_hook(page, start, end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002329 state, mirror);
Chris Masond1310b22008-01-24 16:13:08 -05002330 if (ret)
2331 uptodate = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002332 else
2333 clean_io_failure(start, page);
Chris Masond1310b22008-01-24 16:13:08 -05002334 }
Josef Bacikea466792012-03-26 21:57:36 -04002335
Josef Bacikea466792012-03-26 21:57:36 -04002336 if (!uptodate && tree->ops && tree->ops->readpage_io_failed_hook) {
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002337 ret = tree->ops->readpage_io_failed_hook(page, mirror);
Josef Bacikea466792012-03-26 21:57:36 -04002338 if (!ret && !err &&
2339 test_bit(BIO_UPTODATE, &bio->bi_flags))
2340 uptodate = 1;
2341 } else if (!uptodate) {
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002342 /*
2343 * The generic bio_readpage_error handles errors the
2344 * following way: If possible, new read requests are
2345 * created and submitted and will end up in
2346 * end_bio_extent_readpage as well (if we're lucky, not
2347 * in the !uptodate case). In that case it returns 0 and
2348 * we just go on with the next page in our bio. If it
2349 * can't handle the error it will return -EIO and we
2350 * remain responsible for that page.
2351 */
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002352 ret = bio_readpage_error(bio, page, start, end, mirror, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04002353 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04002354 uptodate =
2355 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05002356 if (err)
2357 uptodate = 0;
Arne Jansen507903b2011-04-06 10:02:20 +00002358 uncache_state(&cached);
Chris Mason7e383262008-04-09 16:28:12 -04002359 continue;
2360 }
2361 }
Chris Mason70dec802008-01-29 09:59:12 -05002362
Josef Bacik0b32f4b2012-03-13 09:38:00 -04002363 if (uptodate && tree->track_uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00002364 set_extent_uptodate(tree, start, end, &cached,
David Woodhouse902b22f2008-08-20 08:51:49 -04002365 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05002366 }
Arne Jansen507903b2011-04-06 10:02:20 +00002367 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05002368
Chris Mason70dec802008-01-29 09:59:12 -05002369 if (whole_page) {
2370 if (uptodate) {
2371 SetPageUptodate(page);
2372 } else {
2373 ClearPageUptodate(page);
2374 SetPageError(page);
2375 }
Chris Masond1310b22008-01-24 16:13:08 -05002376 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05002377 } else {
2378 if (uptodate) {
2379 check_page_uptodate(tree, page);
2380 } else {
2381 ClearPageUptodate(page);
2382 SetPageError(page);
2383 }
Chris Masond1310b22008-01-24 16:13:08 -05002384 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05002385 }
Chris Mason4125bf72010-02-03 18:18:45 +00002386 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05002387
2388 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002389}
2390
Miao Xie88f794e2010-11-22 03:02:55 +00002391struct bio *
2392btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2393 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002394{
2395 struct bio *bio;
2396
2397 bio = bio_alloc(gfp_flags, nr_vecs);
2398
2399 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2400 while (!bio && (nr_vecs /= 2))
2401 bio = bio_alloc(gfp_flags, nr_vecs);
2402 }
2403
2404 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04002405 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002406 bio->bi_bdev = bdev;
2407 bio->bi_sector = first_sector;
2408 }
2409 return bio;
2410}
2411
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002412/*
2413 * Since writes are async, they will only return -ENOMEM.
2414 * Reads can return the full range of I/O error conditions.
2415 */
Jeff Mahoney355808c2011-10-03 23:23:14 -04002416static int __must_check submit_one_bio(int rw, struct bio *bio,
2417 int mirror_num, unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002418{
Chris Masond1310b22008-01-24 16:13:08 -05002419 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002420 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2421 struct page *page = bvec->bv_page;
2422 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002423 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002424
2425 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002426
David Woodhouse902b22f2008-08-20 08:51:49 -04002427 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002428
2429 bio_get(bio);
2430
Chris Mason065631f2008-02-20 12:07:25 -05002431 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00002432 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002433 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002434 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01002435 btrfsic_submit_bio(rw, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002436
Chris Masond1310b22008-01-24 16:13:08 -05002437 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2438 ret = -EOPNOTSUPP;
2439 bio_put(bio);
2440 return ret;
2441}
2442
Jeff Mahoney3444a972011-10-03 23:23:13 -04002443static int merge_bio(struct extent_io_tree *tree, struct page *page,
2444 unsigned long offset, size_t size, struct bio *bio,
2445 unsigned long bio_flags)
2446{
2447 int ret = 0;
2448 if (tree->ops && tree->ops->merge_bio_hook)
2449 ret = tree->ops->merge_bio_hook(page, offset, size, bio,
2450 bio_flags);
2451 BUG_ON(ret < 0);
2452 return ret;
2453
2454}
2455
Chris Masond1310b22008-01-24 16:13:08 -05002456static int submit_extent_page(int rw, struct extent_io_tree *tree,
2457 struct page *page, sector_t sector,
2458 size_t size, unsigned long offset,
2459 struct block_device *bdev,
2460 struct bio **bio_ret,
2461 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04002462 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002463 int mirror_num,
2464 unsigned long prev_bio_flags,
2465 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002466{
2467 int ret = 0;
2468 struct bio *bio;
2469 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04002470 int contig = 0;
2471 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2472 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05002473 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002474
2475 if (bio_ret && *bio_ret) {
2476 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002477 if (old_compressed)
2478 contig = bio->bi_sector == sector;
2479 else
2480 contig = bio->bi_sector + (bio->bi_size >> 9) ==
2481 sector;
2482
2483 if (prev_bio_flags != bio_flags || !contig ||
Jeff Mahoney3444a972011-10-03 23:23:13 -04002484 merge_bio(tree, page, offset, page_size, bio, bio_flags) ||
Chris Masonc8b97812008-10-29 14:49:59 -04002485 bio_add_page(bio, page, page_size, offset) < page_size) {
2486 ret = submit_one_bio(rw, bio, mirror_num,
2487 prev_bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002488 if (ret < 0)
2489 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05002490 bio = NULL;
2491 } else {
2492 return 0;
2493 }
2494 }
Chris Masonc8b97812008-10-29 14:49:59 -04002495 if (this_compressed)
2496 nr = BIO_MAX_PAGES;
2497 else
2498 nr = bio_get_nr_vecs(bdev);
2499
Miao Xie88f794e2010-11-22 03:02:55 +00002500 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002501 if (!bio)
2502 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002503
Chris Masonc8b97812008-10-29 14:49:59 -04002504 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002505 bio->bi_end_io = end_io_func;
2506 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05002507
Chris Masond3977122009-01-05 21:25:51 -05002508 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002509 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002510 else
Chris Masonc8b97812008-10-29 14:49:59 -04002511 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002512
2513 return ret;
2514}
2515
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002516void attach_extent_buffer_page(struct extent_buffer *eb, struct page *page)
2517{
2518 if (!PagePrivate(page)) {
2519 SetPagePrivate(page);
2520 page_cache_get(page);
2521 set_page_private(page, (unsigned long)eb);
2522 } else {
2523 WARN_ON(page->private != (unsigned long)eb);
2524 }
2525}
2526
Chris Masond1310b22008-01-24 16:13:08 -05002527void set_page_extent_mapped(struct page *page)
2528{
2529 if (!PagePrivate(page)) {
2530 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002531 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002532 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002533 }
2534}
2535
Chris Masond1310b22008-01-24 16:13:08 -05002536/*
2537 * basic readpage implementation. Locked extent state structs are inserted
2538 * into the tree that are removed when the IO is done (by the end_io
2539 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002540 * XXX JDM: This needs looking at to ensure proper page locking
Chris Masond1310b22008-01-24 16:13:08 -05002541 */
2542static int __extent_read_full_page(struct extent_io_tree *tree,
2543 struct page *page,
2544 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002545 struct bio **bio, int mirror_num,
2546 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002547{
2548 struct inode *inode = page->mapping->host;
2549 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2550 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2551 u64 end;
2552 u64 cur = start;
2553 u64 extent_offset;
2554 u64 last_byte = i_size_read(inode);
2555 u64 block_start;
2556 u64 cur_end;
2557 sector_t sector;
2558 struct extent_map *em;
2559 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002560 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05002561 int ret;
2562 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002563 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002564 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002565 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002566 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002567 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002568
2569 set_page_extent_mapped(page);
2570
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002571 if (!PageUptodate(page)) {
2572 if (cleancache_get_page(page) == 0) {
2573 BUG_ON(blocksize != PAGE_SIZE);
2574 goto out;
2575 }
2576 }
2577
Chris Masond1310b22008-01-24 16:13:08 -05002578 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002579 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002580 lock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002581 ordered = btrfs_lookup_ordered_extent(inode, start);
2582 if (!ordered)
2583 break;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002584 unlock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002585 btrfs_start_ordered_extent(inode, ordered, 1);
2586 btrfs_put_ordered_extent(ordered);
2587 }
Chris Masond1310b22008-01-24 16:13:08 -05002588
Chris Masonc8b97812008-10-29 14:49:59 -04002589 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2590 char *userpage;
2591 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2592
2593 if (zero_offset) {
2594 iosize = PAGE_CACHE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002595 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002596 memset(userpage + zero_offset, 0, iosize);
2597 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002598 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002599 }
2600 }
Chris Masond1310b22008-01-24 16:13:08 -05002601 while (cur <= end) {
2602 if (cur >= last_byte) {
2603 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002604 struct extent_state *cached = NULL;
2605
David Sterba306e16c2011-04-19 14:29:38 +02002606 iosize = PAGE_CACHE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002607 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002608 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002609 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002610 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002611 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002612 &cached, GFP_NOFS);
2613 unlock_extent_cached(tree, cur, cur + iosize - 1,
2614 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002615 break;
2616 }
David Sterba306e16c2011-04-19 14:29:38 +02002617 em = get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002618 end - cur + 1, 0);
David Sterbac7040052011-04-19 18:00:01 +02002619 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002620 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002621 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002622 break;
2623 }
Chris Masond1310b22008-01-24 16:13:08 -05002624 extent_offset = cur - em->start;
2625 BUG_ON(extent_map_end(em) <= cur);
2626 BUG_ON(end < cur);
2627
Li Zefan261507a02010-12-17 14:21:50 +08002628 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002629 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002630 extent_set_compress_type(&this_bio_flag,
2631 em->compress_type);
2632 }
Chris Masonc8b97812008-10-29 14:49:59 -04002633
Chris Masond1310b22008-01-24 16:13:08 -05002634 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2635 cur_end = min(extent_map_end(em) - 1, end);
2636 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002637 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2638 disk_io_size = em->block_len;
2639 sector = em->block_start >> 9;
2640 } else {
2641 sector = (em->block_start + extent_offset) >> 9;
2642 disk_io_size = iosize;
2643 }
Chris Masond1310b22008-01-24 16:13:08 -05002644 bdev = em->bdev;
2645 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002646 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2647 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002648 free_extent_map(em);
2649 em = NULL;
2650
2651 /* we've found a hole, just zero and go on */
2652 if (block_start == EXTENT_MAP_HOLE) {
2653 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002654 struct extent_state *cached = NULL;
2655
Cong Wang7ac687d2011-11-25 23:14:28 +08002656 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002657 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002658 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002659 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002660
2661 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002662 &cached, GFP_NOFS);
2663 unlock_extent_cached(tree, cur, cur + iosize - 1,
2664 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002665 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002666 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002667 continue;
2668 }
2669 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002670 if (test_range_bit(tree, cur, cur_end,
2671 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002672 check_page_uptodate(tree, page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002673 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002674 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002675 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002676 continue;
2677 }
Chris Mason70dec802008-01-29 09:59:12 -05002678 /* we have an inline extent but it didn't get marked up
2679 * to date. Error out
2680 */
2681 if (block_start == EXTENT_MAP_INLINE) {
2682 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002683 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05002684 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002685 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05002686 continue;
2687 }
Chris Masond1310b22008-01-24 16:13:08 -05002688
2689 ret = 0;
2690 if (tree->ops && tree->ops->readpage_io_hook) {
2691 ret = tree->ops->readpage_io_hook(page, cur,
2692 cur + iosize - 1);
2693 }
2694 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002695 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2696 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002697 ret = submit_extent_page(READ, tree, page,
David Sterba306e16c2011-04-19 14:29:38 +02002698 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04002699 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002700 end_bio_extent_readpage, mirror_num,
2701 *bio_flags,
2702 this_bio_flag);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002703 BUG_ON(ret == -ENOMEM);
Chris Mason89642222008-07-24 09:41:53 -04002704 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002705 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002706 }
2707 if (ret)
2708 SetPageError(page);
2709 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002710 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002711 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002712out:
Chris Masond1310b22008-01-24 16:13:08 -05002713 if (!nr) {
2714 if (!PageError(page))
2715 SetPageUptodate(page);
2716 unlock_page(page);
2717 }
2718 return 0;
2719}
2720
2721int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002722 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05002723{
2724 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002725 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002726 int ret;
2727
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002728 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Chris Masonc8b97812008-10-29 14:49:59 -04002729 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002730 if (bio)
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002731 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002732 return ret;
2733}
Chris Masond1310b22008-01-24 16:13:08 -05002734
Chris Mason11c83492009-04-20 15:50:09 -04002735static noinline void update_nr_written(struct page *page,
2736 struct writeback_control *wbc,
2737 unsigned long nr_written)
2738{
2739 wbc->nr_to_write -= nr_written;
2740 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2741 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2742 page->mapping->writeback_index = page->index + nr_written;
2743}
2744
Chris Masond1310b22008-01-24 16:13:08 -05002745/*
2746 * the writepage semantics are similar to regular writepage. extent
2747 * records are inserted to lock ranges in the tree, and as dirty areas
2748 * are found, they are marked writeback. Then the lock bits are removed
2749 * and the end_io handler clears the writeback ranges
2750 */
2751static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2752 void *data)
2753{
2754 struct inode *inode = page->mapping->host;
2755 struct extent_page_data *epd = data;
2756 struct extent_io_tree *tree = epd->tree;
2757 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2758 u64 delalloc_start;
2759 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2760 u64 end;
2761 u64 cur = start;
2762 u64 extent_offset;
2763 u64 last_byte = i_size_read(inode);
2764 u64 block_start;
2765 u64 iosize;
2766 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002767 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002768 struct extent_map *em;
2769 struct block_device *bdev;
2770 int ret;
2771 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002772 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002773 size_t blocksize;
2774 loff_t i_size = i_size_read(inode);
2775 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2776 u64 nr_delalloc;
2777 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002778 int page_started;
2779 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002780 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002781 unsigned long nr_written = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002782 bool fill_delalloc = true;
Chris Masond1310b22008-01-24 16:13:08 -05002783
Chris Masonffbd5172009-04-20 15:50:09 -04002784 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002785 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002786 else
2787 write_flags = WRITE;
2788
liubo1abe9b82011-03-24 11:18:59 +00002789 trace___extent_writepage(page, inode, wbc);
2790
Chris Masond1310b22008-01-24 16:13:08 -05002791 WARN_ON(!PageLocked(page));
Chris Masonbf0da8c2011-11-04 12:29:37 -04002792
2793 ClearPageError(page);
2794
Chris Mason7f3c74f2008-07-18 12:01:11 -04002795 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002796 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002797 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002798 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002799 unlock_page(page);
2800 return 0;
2801 }
2802
2803 if (page->index == end_index) {
2804 char *userpage;
2805
Cong Wang7ac687d2011-11-25 23:14:28 +08002806 userpage = kmap_atomic(page);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002807 memset(userpage + pg_offset, 0,
2808 PAGE_CACHE_SIZE - pg_offset);
Cong Wang7ac687d2011-11-25 23:14:28 +08002809 kunmap_atomic(userpage);
Chris Mason211c17f2008-05-15 09:13:45 -04002810 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002811 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002812 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002813
2814 set_page_extent_mapped(page);
2815
Josef Bacik9e487102011-08-01 12:08:18 -04002816 if (!tree->ops || !tree->ops->fill_delalloc)
2817 fill_delalloc = false;
2818
Chris Masond1310b22008-01-24 16:13:08 -05002819 delalloc_start = start;
2820 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002821 page_started = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002822 if (!epd->extent_locked && fill_delalloc) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002823 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002824 /*
2825 * make sure the wbc mapping index is at least updated
2826 * to this page.
2827 */
2828 update_nr_written(page, wbc, 0);
2829
Chris Masond3977122009-01-05 21:25:51 -05002830 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002831 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002832 page,
2833 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002834 &delalloc_end,
2835 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002836 if (nr_delalloc == 0) {
2837 delalloc_start = delalloc_end + 1;
2838 continue;
2839 }
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002840 ret = tree->ops->fill_delalloc(inode, page,
2841 delalloc_start,
2842 delalloc_end,
2843 &page_started,
2844 &nr_written);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002845 /* File system has been set read-only */
2846 if (ret) {
2847 SetPageError(page);
2848 goto done;
2849 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002850 /*
2851 * delalloc_end is already one less than the total
2852 * length, so we don't subtract one from
2853 * PAGE_CACHE_SIZE
2854 */
2855 delalloc_to_write += (delalloc_end - delalloc_start +
2856 PAGE_CACHE_SIZE) >>
2857 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002858 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002859 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002860 if (wbc->nr_to_write < delalloc_to_write) {
2861 int thresh = 8192;
2862
2863 if (delalloc_to_write < thresh * 2)
2864 thresh = delalloc_to_write;
2865 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2866 thresh);
2867 }
Chris Masonc8b97812008-10-29 14:49:59 -04002868
Chris Mason771ed682008-11-06 22:02:51 -05002869 /* did the fill delalloc function already unlock and start
2870 * the IO?
2871 */
2872 if (page_started) {
2873 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002874 /*
2875 * we've unlocked the page, so we can't update
2876 * the mapping's writeback index, just update
2877 * nr_to_write.
2878 */
2879 wbc->nr_to_write -= nr_written;
2880 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002881 }
Chris Masonc8b97812008-10-29 14:49:59 -04002882 }
Chris Mason247e7432008-07-17 12:53:51 -04002883 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002884 ret = tree->ops->writepage_start_hook(page, start,
2885 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002886 if (ret) {
2887 /* Fixup worker will requeue */
2888 if (ret == -EBUSY)
2889 wbc->pages_skipped++;
2890 else
2891 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002892 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002893 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002894 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002895 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002896 }
2897 }
2898
Chris Mason11c83492009-04-20 15:50:09 -04002899 /*
2900 * we don't want to touch the inode after unlocking the page,
2901 * so we update the mapping writeback index now
2902 */
2903 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002904
Chris Masond1310b22008-01-24 16:13:08 -05002905 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002906 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002907 if (tree->ops && tree->ops->writepage_end_io_hook)
2908 tree->ops->writepage_end_io_hook(page, start,
2909 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002910 goto done;
2911 }
2912
Chris Masond1310b22008-01-24 16:13:08 -05002913 blocksize = inode->i_sb->s_blocksize;
2914
2915 while (cur <= end) {
2916 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002917 if (tree->ops && tree->ops->writepage_end_io_hook)
2918 tree->ops->writepage_end_io_hook(page, cur,
2919 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002920 break;
2921 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002922 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002923 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02002924 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002925 SetPageError(page);
2926 break;
2927 }
2928
2929 extent_offset = cur - em->start;
2930 BUG_ON(extent_map_end(em) <= cur);
2931 BUG_ON(end < cur);
2932 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2933 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2934 sector = (em->block_start + extent_offset) >> 9;
2935 bdev = em->bdev;
2936 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002937 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002938 free_extent_map(em);
2939 em = NULL;
2940
Chris Masonc8b97812008-10-29 14:49:59 -04002941 /*
2942 * compressed and inline extents are written through other
2943 * paths in the FS
2944 */
2945 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002946 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002947 /*
2948 * end_io notification does not happen here for
2949 * compressed extents
2950 */
2951 if (!compressed && tree->ops &&
2952 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002953 tree->ops->writepage_end_io_hook(page, cur,
2954 cur + iosize - 1,
2955 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002956 else if (compressed) {
2957 /* we don't want to end_page_writeback on
2958 * a compressed extent. this happens
2959 * elsewhere
2960 */
2961 nr++;
2962 }
2963
2964 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002965 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002966 continue;
2967 }
Chris Masond1310b22008-01-24 16:13:08 -05002968 /* leave this out until we have a page_mkwrite call */
2969 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04002970 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002971 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002972 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002973 continue;
2974 }
Chris Masonc8b97812008-10-29 14:49:59 -04002975
Chris Masond1310b22008-01-24 16:13:08 -05002976 if (tree->ops && tree->ops->writepage_io_hook) {
2977 ret = tree->ops->writepage_io_hook(page, cur,
2978 cur + iosize - 1);
2979 } else {
2980 ret = 0;
2981 }
Chris Mason1259ab72008-05-12 13:39:03 -04002982 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002983 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002984 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002985 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002986
Chris Masond1310b22008-01-24 16:13:08 -05002987 set_range_writeback(tree, cur, cur + iosize - 1);
2988 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05002989 printk(KERN_ERR "btrfs warning page %lu not "
2990 "writeback, cur %llu end %llu\n",
2991 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05002992 (unsigned long long)end);
2993 }
2994
Chris Masonffbd5172009-04-20 15:50:09 -04002995 ret = submit_extent_page(write_flags, tree, page,
2996 sector, iosize, pg_offset,
2997 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04002998 end_bio_extent_writepage,
2999 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003000 if (ret)
3001 SetPageError(page);
3002 }
3003 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003004 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003005 nr++;
3006 }
3007done:
3008 if (nr == 0) {
3009 /* make sure the mapping tag for page dirty gets cleared */
3010 set_page_writeback(page);
3011 end_page_writeback(page);
3012 }
Chris Masond1310b22008-01-24 16:13:08 -05003013 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05003014
Chris Mason11c83492009-04-20 15:50:09 -04003015done_unlocked:
3016
Chris Mason2c64c532009-09-02 15:04:12 -04003017 /* drop our reference on any cached states */
3018 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05003019 return 0;
3020}
3021
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003022static int eb_wait(void *word)
3023{
3024 io_schedule();
3025 return 0;
3026}
3027
3028static void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
3029{
3030 wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
3031 TASK_UNINTERRUPTIBLE);
3032}
3033
3034static int lock_extent_buffer_for_io(struct extent_buffer *eb,
3035 struct btrfs_fs_info *fs_info,
3036 struct extent_page_data *epd)
3037{
3038 unsigned long i, num_pages;
3039 int flush = 0;
3040 int ret = 0;
3041
3042 if (!btrfs_try_tree_write_lock(eb)) {
3043 flush = 1;
3044 flush_write_bio(epd);
3045 btrfs_tree_lock(eb);
3046 }
3047
3048 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3049 btrfs_tree_unlock(eb);
3050 if (!epd->sync_io)
3051 return 0;
3052 if (!flush) {
3053 flush_write_bio(epd);
3054 flush = 1;
3055 }
Chris Masona098d8e2012-03-21 12:09:56 -04003056 while (1) {
3057 wait_on_extent_buffer_writeback(eb);
3058 btrfs_tree_lock(eb);
3059 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3060 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003061 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003062 }
3063 }
3064
3065 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3066 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3067 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
3068 spin_lock(&fs_info->delalloc_lock);
3069 if (fs_info->dirty_metadata_bytes >= eb->len)
3070 fs_info->dirty_metadata_bytes -= eb->len;
3071 else
3072 WARN_ON(1);
3073 spin_unlock(&fs_info->delalloc_lock);
3074 ret = 1;
3075 }
3076
3077 btrfs_tree_unlock(eb);
3078
3079 if (!ret)
3080 return ret;
3081
3082 num_pages = num_extent_pages(eb->start, eb->len);
3083 for (i = 0; i < num_pages; i++) {
3084 struct page *p = extent_buffer_page(eb, i);
3085
3086 if (!trylock_page(p)) {
3087 if (!flush) {
3088 flush_write_bio(epd);
3089 flush = 1;
3090 }
3091 lock_page(p);
3092 }
3093 }
3094
3095 return ret;
3096}
3097
3098static void end_extent_buffer_writeback(struct extent_buffer *eb)
3099{
3100 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3101 smp_mb__after_clear_bit();
3102 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3103}
3104
3105static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
3106{
3107 int uptodate = err == 0;
3108 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
3109 struct extent_buffer *eb;
3110 int done;
3111
3112 do {
3113 struct page *page = bvec->bv_page;
3114
3115 bvec--;
3116 eb = (struct extent_buffer *)page->private;
3117 BUG_ON(!eb);
3118 done = atomic_dec_and_test(&eb->io_pages);
3119
3120 if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
3121 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3122 ClearPageUptodate(page);
3123 SetPageError(page);
3124 }
3125
3126 end_page_writeback(page);
3127
3128 if (!done)
3129 continue;
3130
3131 end_extent_buffer_writeback(eb);
3132 } while (bvec >= bio->bi_io_vec);
3133
3134 bio_put(bio);
3135
3136}
3137
3138static int write_one_eb(struct extent_buffer *eb,
3139 struct btrfs_fs_info *fs_info,
3140 struct writeback_control *wbc,
3141 struct extent_page_data *epd)
3142{
3143 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
3144 u64 offset = eb->start;
3145 unsigned long i, num_pages;
3146 int rw = (epd->sync_io ? WRITE_SYNC : WRITE);
Josef Bacikd7dbe9e2012-04-23 14:00:51 -04003147 int ret = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003148
3149 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3150 num_pages = num_extent_pages(eb->start, eb->len);
3151 atomic_set(&eb->io_pages, num_pages);
3152 for (i = 0; i < num_pages; i++) {
3153 struct page *p = extent_buffer_page(eb, i);
3154
3155 clear_page_dirty_for_io(p);
3156 set_page_writeback(p);
3157 ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
3158 PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
3159 -1, end_bio_extent_buffer_writepage,
3160 0, 0, 0);
3161 if (ret) {
3162 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3163 SetPageError(p);
3164 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3165 end_extent_buffer_writeback(eb);
3166 ret = -EIO;
3167 break;
3168 }
3169 offset += PAGE_CACHE_SIZE;
3170 update_nr_written(p, wbc, 1);
3171 unlock_page(p);
3172 }
3173
3174 if (unlikely(ret)) {
3175 for (; i < num_pages; i++) {
3176 struct page *p = extent_buffer_page(eb, i);
3177 unlock_page(p);
3178 }
3179 }
3180
3181 return ret;
3182}
3183
3184int btree_write_cache_pages(struct address_space *mapping,
3185 struct writeback_control *wbc)
3186{
3187 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3188 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3189 struct extent_buffer *eb, *prev_eb = NULL;
3190 struct extent_page_data epd = {
3191 .bio = NULL,
3192 .tree = tree,
3193 .extent_locked = 0,
3194 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3195 };
3196 int ret = 0;
3197 int done = 0;
3198 int nr_to_write_done = 0;
3199 struct pagevec pvec;
3200 int nr_pages;
3201 pgoff_t index;
3202 pgoff_t end; /* Inclusive */
3203 int scanned = 0;
3204 int tag;
3205
3206 pagevec_init(&pvec, 0);
3207 if (wbc->range_cyclic) {
3208 index = mapping->writeback_index; /* Start from prev offset */
3209 end = -1;
3210 } else {
3211 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3212 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3213 scanned = 1;
3214 }
3215 if (wbc->sync_mode == WB_SYNC_ALL)
3216 tag = PAGECACHE_TAG_TOWRITE;
3217 else
3218 tag = PAGECACHE_TAG_DIRTY;
3219retry:
3220 if (wbc->sync_mode == WB_SYNC_ALL)
3221 tag_pages_for_writeback(mapping, index, end);
3222 while (!done && !nr_to_write_done && (index <= end) &&
3223 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3224 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3225 unsigned i;
3226
3227 scanned = 1;
3228 for (i = 0; i < nr_pages; i++) {
3229 struct page *page = pvec.pages[i];
3230
3231 if (!PagePrivate(page))
3232 continue;
3233
3234 if (!wbc->range_cyclic && page->index > end) {
3235 done = 1;
3236 break;
3237 }
3238
3239 eb = (struct extent_buffer *)page->private;
3240 if (!eb) {
3241 WARN_ON(1);
3242 continue;
3243 }
3244
3245 if (eb == prev_eb)
3246 continue;
3247
3248 if (!atomic_inc_not_zero(&eb->refs)) {
3249 WARN_ON(1);
3250 continue;
3251 }
3252
3253 prev_eb = eb;
3254 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3255 if (!ret) {
3256 free_extent_buffer(eb);
3257 continue;
3258 }
3259
3260 ret = write_one_eb(eb, fs_info, wbc, &epd);
3261 if (ret) {
3262 done = 1;
3263 free_extent_buffer(eb);
3264 break;
3265 }
3266 free_extent_buffer(eb);
3267
3268 /*
3269 * the filesystem may choose to bump up nr_to_write.
3270 * We have to make sure to honor the new nr_to_write
3271 * at any time
3272 */
3273 nr_to_write_done = wbc->nr_to_write <= 0;
3274 }
3275 pagevec_release(&pvec);
3276 cond_resched();
3277 }
3278 if (!scanned && !done) {
3279 /*
3280 * We hit the last page and there is more work to be done: wrap
3281 * back to the start of the file
3282 */
3283 scanned = 1;
3284 index = 0;
3285 goto retry;
3286 }
3287 flush_write_bio(&epd);
3288 return ret;
3289}
3290
Chris Masond1310b22008-01-24 16:13:08 -05003291/**
Chris Mason4bef0842008-09-08 11:18:08 -04003292 * 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 -05003293 * @mapping: address space structure to write
3294 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3295 * @writepage: function called for each page
3296 * @data: data passed to writepage function
3297 *
3298 * If a page is already under I/O, write_cache_pages() skips it, even
3299 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3300 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3301 * and msync() need to guarantee that all the data which was dirty at the time
3302 * the call was made get new I/O started against them. If wbc->sync_mode is
3303 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3304 * existing IO to complete.
3305 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003306static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04003307 struct address_space *mapping,
3308 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003309 writepage_t writepage, void *data,
3310 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05003311{
Chris Masond1310b22008-01-24 16:13:08 -05003312 int ret = 0;
3313 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003314 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003315 struct pagevec pvec;
3316 int nr_pages;
3317 pgoff_t index;
3318 pgoff_t end; /* Inclusive */
3319 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003320 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003321
Chris Masond1310b22008-01-24 16:13:08 -05003322 pagevec_init(&pvec, 0);
3323 if (wbc->range_cyclic) {
3324 index = mapping->writeback_index; /* Start from prev offset */
3325 end = -1;
3326 } else {
3327 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3328 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003329 scanned = 1;
3330 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003331 if (wbc->sync_mode == WB_SYNC_ALL)
3332 tag = PAGECACHE_TAG_TOWRITE;
3333 else
3334 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003335retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003336 if (wbc->sync_mode == WB_SYNC_ALL)
3337 tag_pages_for_writeback(mapping, index, end);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003338 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003339 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3340 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003341 unsigned i;
3342
3343 scanned = 1;
3344 for (i = 0; i < nr_pages; i++) {
3345 struct page *page = pvec.pages[i];
3346
3347 /*
3348 * At this point we hold neither mapping->tree_lock nor
3349 * lock on the page itself: the page may be truncated or
3350 * invalidated (changing page->mapping to NULL), or even
3351 * swizzled back from swapper_space to tmpfs file
3352 * mapping
3353 */
Chris Mason01d658f2011-11-01 10:08:06 -04003354 if (tree->ops &&
3355 tree->ops->write_cache_pages_lock_hook) {
3356 tree->ops->write_cache_pages_lock_hook(page,
3357 data, flush_fn);
3358 } else {
3359 if (!trylock_page(page)) {
3360 flush_fn(data);
3361 lock_page(page);
3362 }
3363 }
Chris Masond1310b22008-01-24 16:13:08 -05003364
3365 if (unlikely(page->mapping != mapping)) {
3366 unlock_page(page);
3367 continue;
3368 }
3369
3370 if (!wbc->range_cyclic && page->index > end) {
3371 done = 1;
3372 unlock_page(page);
3373 continue;
3374 }
3375
Chris Masond2c3f4f2008-11-19 12:44:22 -05003376 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003377 if (PageWriteback(page))
3378 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05003379 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003380 }
Chris Masond1310b22008-01-24 16:13:08 -05003381
3382 if (PageWriteback(page) ||
3383 !clear_page_dirty_for_io(page)) {
3384 unlock_page(page);
3385 continue;
3386 }
3387
3388 ret = (*writepage)(page, wbc, data);
3389
3390 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3391 unlock_page(page);
3392 ret = 0;
3393 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003394 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05003395 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003396
3397 /*
3398 * the filesystem may choose to bump up nr_to_write.
3399 * We have to make sure to honor the new nr_to_write
3400 * at any time
3401 */
3402 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05003403 }
3404 pagevec_release(&pvec);
3405 cond_resched();
3406 }
3407 if (!scanned && !done) {
3408 /*
3409 * We hit the last page and there is more work to be done: wrap
3410 * back to the start of the file
3411 */
3412 scanned = 1;
3413 index = 0;
3414 goto retry;
3415 }
Chris Masond1310b22008-01-24 16:13:08 -05003416 return ret;
3417}
Chris Masond1310b22008-01-24 16:13:08 -05003418
Chris Masonffbd5172009-04-20 15:50:09 -04003419static void flush_epd_write_bio(struct extent_page_data *epd)
3420{
3421 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04003422 int rw = WRITE;
3423 int ret;
3424
Chris Masonffbd5172009-04-20 15:50:09 -04003425 if (epd->sync_io)
Jeff Mahoney355808c2011-10-03 23:23:14 -04003426 rw = WRITE_SYNC;
3427
3428 ret = submit_one_bio(rw, epd->bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003429 BUG_ON(ret < 0); /* -ENOMEM */
Chris Masonffbd5172009-04-20 15:50:09 -04003430 epd->bio = NULL;
3431 }
3432}
3433
Chris Masond2c3f4f2008-11-19 12:44:22 -05003434static noinline void flush_write_bio(void *data)
3435{
3436 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04003437 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003438}
3439
Chris Masond1310b22008-01-24 16:13:08 -05003440int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3441 get_extent_t *get_extent,
3442 struct writeback_control *wbc)
3443{
3444 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003445 struct extent_page_data epd = {
3446 .bio = NULL,
3447 .tree = tree,
3448 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003449 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003450 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05003451 };
Chris Masond1310b22008-01-24 16:13:08 -05003452
Chris Masond1310b22008-01-24 16:13:08 -05003453 ret = __extent_writepage(page, wbc, &epd);
3454
Chris Masonffbd5172009-04-20 15:50:09 -04003455 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003456 return ret;
3457}
Chris Masond1310b22008-01-24 16:13:08 -05003458
Chris Mason771ed682008-11-06 22:02:51 -05003459int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3460 u64 start, u64 end, get_extent_t *get_extent,
3461 int mode)
3462{
3463 int ret = 0;
3464 struct address_space *mapping = inode->i_mapping;
3465 struct page *page;
3466 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3467 PAGE_CACHE_SHIFT;
3468
3469 struct extent_page_data epd = {
3470 .bio = NULL,
3471 .tree = tree,
3472 .get_extent = get_extent,
3473 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04003474 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05003475 };
3476 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05003477 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05003478 .nr_to_write = nr_pages * 2,
3479 .range_start = start,
3480 .range_end = end + 1,
3481 };
3482
Chris Masond3977122009-01-05 21:25:51 -05003483 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05003484 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3485 if (clear_page_dirty_for_io(page))
3486 ret = __extent_writepage(page, &wbc_writepages, &epd);
3487 else {
3488 if (tree->ops && tree->ops->writepage_end_io_hook)
3489 tree->ops->writepage_end_io_hook(page, start,
3490 start + PAGE_CACHE_SIZE - 1,
3491 NULL, 1);
3492 unlock_page(page);
3493 }
3494 page_cache_release(page);
3495 start += PAGE_CACHE_SIZE;
3496 }
3497
Chris Masonffbd5172009-04-20 15:50:09 -04003498 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05003499 return ret;
3500}
Chris Masond1310b22008-01-24 16:13:08 -05003501
3502int extent_writepages(struct extent_io_tree *tree,
3503 struct address_space *mapping,
3504 get_extent_t *get_extent,
3505 struct writeback_control *wbc)
3506{
3507 int ret = 0;
3508 struct extent_page_data epd = {
3509 .bio = NULL,
3510 .tree = tree,
3511 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003512 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003513 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05003514 };
3515
Chris Mason4bef0842008-09-08 11:18:08 -04003516 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003517 __extent_writepage, &epd,
3518 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04003519 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003520 return ret;
3521}
Chris Masond1310b22008-01-24 16:13:08 -05003522
3523int extent_readpages(struct extent_io_tree *tree,
3524 struct address_space *mapping,
3525 struct list_head *pages, unsigned nr_pages,
3526 get_extent_t get_extent)
3527{
3528 struct bio *bio = NULL;
3529 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04003530 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003531
Chris Masond1310b22008-01-24 16:13:08 -05003532 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
3533 struct page *page = list_entry(pages->prev, struct page, lru);
3534
3535 prefetchw(&page->flags);
3536 list_del(&page->lru);
Nick Piggin28ecb6092010-03-17 13:31:04 +00003537 if (!add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04003538 page->index, GFP_NOFS)) {
Chris Masonf1885912008-04-09 16:28:12 -04003539 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04003540 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003541 }
3542 page_cache_release(page);
3543 }
Chris Masond1310b22008-01-24 16:13:08 -05003544 BUG_ON(!list_empty(pages));
3545 if (bio)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003546 return submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003547 return 0;
3548}
Chris Masond1310b22008-01-24 16:13:08 -05003549
3550/*
3551 * basic invalidatepage code, this waits on any locked or writeback
3552 * ranges corresponding to the page, and then deletes any extent state
3553 * records from the tree
3554 */
3555int extent_invalidatepage(struct extent_io_tree *tree,
3556 struct page *page, unsigned long offset)
3557{
Josef Bacik2ac55d42010-02-03 19:33:23 +00003558 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003559 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
3560 u64 end = start + PAGE_CACHE_SIZE - 1;
3561 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3562
Chris Masond3977122009-01-05 21:25:51 -05003563 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003564 if (start > end)
3565 return 0;
3566
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003567 lock_extent_bits(tree, start, end, 0, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04003568 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003569 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04003570 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3571 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003572 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003573 return 0;
3574}
Chris Masond1310b22008-01-24 16:13:08 -05003575
3576/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04003577 * a helper for releasepage, this tests for areas of the page that
3578 * are locked or under IO and drops the related state bits if it is safe
3579 * to drop the page.
3580 */
3581int try_release_extent_state(struct extent_map_tree *map,
3582 struct extent_io_tree *tree, struct page *page,
3583 gfp_t mask)
3584{
3585 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3586 u64 end = start + PAGE_CACHE_SIZE - 1;
3587 int ret = 1;
3588
Chris Mason211f90e2008-07-18 11:56:15 -04003589 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04003590 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04003591 ret = 0;
3592 else {
3593 if ((mask & GFP_NOFS) == GFP_NOFS)
3594 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04003595 /*
3596 * at this point we can safely clear everything except the
3597 * locked bit and the nodatasum bit
3598 */
Chris Masone3f24cc2011-02-14 12:52:08 -05003599 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04003600 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3601 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05003602
3603 /* if clear_extent_bit failed for enomem reasons,
3604 * we can't allow the release to continue.
3605 */
3606 if (ret < 0)
3607 ret = 0;
3608 else
3609 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003610 }
3611 return ret;
3612}
Chris Mason7b13b7b2008-04-18 10:29:50 -04003613
3614/*
Chris Masond1310b22008-01-24 16:13:08 -05003615 * a helper for releasepage. As long as there are no locked extents
3616 * in the range corresponding to the page, both state records and extent
3617 * map records are removed
3618 */
3619int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05003620 struct extent_io_tree *tree, struct page *page,
3621 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05003622{
3623 struct extent_map *em;
3624 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3625 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003626
Chris Mason70dec802008-01-29 09:59:12 -05003627 if ((mask & __GFP_WAIT) &&
3628 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05003629 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05003630 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05003631 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04003632 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05003633 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09003634 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04003635 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003636 break;
3637 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003638 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3639 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04003640 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003641 free_extent_map(em);
3642 break;
3643 }
3644 if (!test_range_bit(tree, em->start,
3645 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04003646 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04003647 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05003648 remove_extent_mapping(map, em);
3649 /* once for the rb tree */
3650 free_extent_map(em);
3651 }
3652 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04003653 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003654
3655 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05003656 free_extent_map(em);
3657 }
Chris Masond1310b22008-01-24 16:13:08 -05003658 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04003659 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003660}
Chris Masond1310b22008-01-24 16:13:08 -05003661
Chris Masonec29ed52011-02-23 16:23:20 -05003662/*
3663 * helper function for fiemap, which doesn't want to see any holes.
3664 * This maps until we find something past 'last'
3665 */
3666static struct extent_map *get_extent_skip_holes(struct inode *inode,
3667 u64 offset,
3668 u64 last,
3669 get_extent_t *get_extent)
3670{
3671 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3672 struct extent_map *em;
3673 u64 len;
3674
3675 if (offset >= last)
3676 return NULL;
3677
3678 while(1) {
3679 len = last - offset;
3680 if (len == 0)
3681 break;
3682 len = (len + sectorsize - 1) & ~(sectorsize - 1);
3683 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02003684 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05003685 return em;
3686
3687 /* if this isn't a hole return it */
3688 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3689 em->block_start != EXTENT_MAP_HOLE) {
3690 return em;
3691 }
3692
3693 /* this is a hole, advance to the next extent */
3694 offset = extent_map_end(em);
3695 free_extent_map(em);
3696 if (offset >= last)
3697 break;
3698 }
3699 return NULL;
3700}
3701
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003702int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3703 __u64 start, __u64 len, get_extent_t *get_extent)
3704{
Josef Bacik975f84f2010-11-23 19:36:57 +00003705 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003706 u64 off = start;
3707 u64 max = start + len;
3708 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00003709 u32 found_type;
3710 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05003711 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003712 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003713 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00003714 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003715 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003716 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00003717 struct btrfs_path *path;
3718 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003719 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003720 u64 em_start = 0;
3721 u64 em_len = 0;
3722 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003723 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003724
3725 if (len == 0)
3726 return -EINVAL;
3727
Josef Bacik975f84f2010-11-23 19:36:57 +00003728 path = btrfs_alloc_path();
3729 if (!path)
3730 return -ENOMEM;
3731 path->leave_spinning = 1;
3732
Josef Bacik4d479cf2011-11-17 11:34:31 -05003733 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3734 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3735
Chris Masonec29ed52011-02-23 16:23:20 -05003736 /*
3737 * lookup the last file extent. We're not using i_size here
3738 * because there might be preallocation past i_size
3739 */
Josef Bacik975f84f2010-11-23 19:36:57 +00003740 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
Li Zefan33345d012011-04-20 10:31:50 +08003741 path, btrfs_ino(inode), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00003742 if (ret < 0) {
3743 btrfs_free_path(path);
3744 return ret;
3745 }
3746 WARN_ON(!ret);
3747 path->slots[0]--;
3748 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3749 struct btrfs_file_extent_item);
3750 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3751 found_type = btrfs_key_type(&found_key);
3752
Chris Masonec29ed52011-02-23 16:23:20 -05003753 /* No extents, but there might be delalloc bits */
Li Zefan33345d012011-04-20 10:31:50 +08003754 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00003755 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05003756 /* have to trust i_size as the end */
3757 last = (u64)-1;
3758 last_for_get_extent = isize;
3759 } else {
3760 /*
3761 * remember the start of the last extent. There are a
3762 * bunch of different factors that go into the length of the
3763 * extent, so its much less complex to remember where it started
3764 */
3765 last = found_key.offset;
3766 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00003767 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003768 btrfs_free_path(path);
3769
Chris Masonec29ed52011-02-23 16:23:20 -05003770 /*
3771 * we might have some extents allocated but more delalloc past those
3772 * extents. so, we trust isize unless the start of the last extent is
3773 * beyond isize
3774 */
3775 if (last < isize) {
3776 last = (u64)-1;
3777 last_for_get_extent = isize;
3778 }
3779
Josef Bacik2ac55d42010-02-03 19:33:23 +00003780 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003781 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05003782
Josef Bacik4d479cf2011-11-17 11:34:31 -05003783 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05003784 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003785 if (!em)
3786 goto out;
3787 if (IS_ERR(em)) {
3788 ret = PTR_ERR(em);
3789 goto out;
3790 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003791
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003792 while (!end) {
Chris Masonea8efc72011-03-08 11:54:40 -05003793 u64 offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003794
Chris Masonea8efc72011-03-08 11:54:40 -05003795 /* break if the extent we found is outside the range */
3796 if (em->start >= max || extent_map_end(em) < off)
3797 break;
3798
3799 /*
3800 * get_extent may return an extent that starts before our
3801 * requested range. We have to make sure the ranges
3802 * we return to fiemap always move forward and don't
3803 * overlap, so adjust the offsets here
3804 */
3805 em_start = max(em->start, off);
3806
3807 /*
3808 * record the offset from the start of the extent
3809 * for adjusting the disk offset below
3810 */
3811 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05003812 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05003813 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05003814 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003815 disko = 0;
3816 flags = 0;
3817
Chris Masonea8efc72011-03-08 11:54:40 -05003818 /*
3819 * bump off for our next call to get_extent
3820 */
3821 off = extent_map_end(em);
3822 if (off >= max)
3823 end = 1;
3824
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003825 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003826 end = 1;
3827 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003828 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003829 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3830 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003831 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003832 flags |= (FIEMAP_EXTENT_DELALLOC |
3833 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003834 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05003835 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003836 }
3837 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3838 flags |= FIEMAP_EXTENT_ENCODED;
3839
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003840 free_extent_map(em);
3841 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05003842 if ((em_start >= last) || em_len == (u64)-1 ||
3843 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003844 flags |= FIEMAP_EXTENT_LAST;
3845 end = 1;
3846 }
3847
Chris Masonec29ed52011-02-23 16:23:20 -05003848 /* now scan forward to see if this is really the last extent. */
3849 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3850 get_extent);
3851 if (IS_ERR(em)) {
3852 ret = PTR_ERR(em);
3853 goto out;
3854 }
3855 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00003856 flags |= FIEMAP_EXTENT_LAST;
3857 end = 1;
3858 }
Chris Masonec29ed52011-02-23 16:23:20 -05003859 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3860 em_len, flags);
3861 if (ret)
3862 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003863 }
3864out_free:
3865 free_extent_map(em);
3866out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00003867 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3868 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003869 return ret;
3870}
3871
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003872inline struct page *extent_buffer_page(struct extent_buffer *eb,
Chris Masond1310b22008-01-24 16:13:08 -05003873 unsigned long i)
3874{
Chris Mason727011e2010-08-06 13:21:20 -04003875 return eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05003876}
3877
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003878inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04003879{
Chris Mason6af118ce2008-07-22 11:18:07 -04003880 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3881 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04003882}
3883
Chris Mason727011e2010-08-06 13:21:20 -04003884static void __free_extent_buffer(struct extent_buffer *eb)
3885{
3886#if LEAK_DEBUG
3887 unsigned long flags;
3888 spin_lock_irqsave(&leak_lock, flags);
3889 list_del(&eb->leak_list);
3890 spin_unlock_irqrestore(&leak_lock, flags);
3891#endif
3892 if (eb->pages && eb->pages != eb->inline_pages)
3893 kfree(eb->pages);
3894 kmem_cache_free(extent_buffer_cache, eb);
3895}
3896
Chris Masond1310b22008-01-24 16:13:08 -05003897static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3898 u64 start,
3899 unsigned long len,
3900 gfp_t mask)
3901{
3902 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003903#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003904 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003905#endif
Chris Masond1310b22008-01-24 16:13:08 -05003906
Chris Masond1310b22008-01-24 16:13:08 -05003907 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003908 if (eb == NULL)
3909 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003910 eb->start = start;
3911 eb->len = len;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05003912 eb->tree = tree;
Chris Masonbd681512011-07-16 15:23:14 -04003913 rwlock_init(&eb->lock);
3914 atomic_set(&eb->write_locks, 0);
3915 atomic_set(&eb->read_locks, 0);
3916 atomic_set(&eb->blocking_readers, 0);
3917 atomic_set(&eb->blocking_writers, 0);
3918 atomic_set(&eb->spinning_readers, 0);
3919 atomic_set(&eb->spinning_writers, 0);
Arne Jansen5b25f702011-09-13 10:55:48 +02003920 eb->lock_nested = 0;
Chris Masonbd681512011-07-16 15:23:14 -04003921 init_waitqueue_head(&eb->write_lock_wq);
3922 init_waitqueue_head(&eb->read_lock_wq);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003923
Chris Mason39351272009-02-04 09:24:05 -05003924#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003925 spin_lock_irqsave(&leak_lock, flags);
3926 list_add(&eb->leak_list, &buffers);
3927 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003928#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05003929 spin_lock_init(&eb->refs_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003930 atomic_set(&eb->refs, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003931 atomic_set(&eb->io_pages, 0);
Chris Mason727011e2010-08-06 13:21:20 -04003932
3933 if (len > MAX_INLINE_EXTENT_BUFFER_SIZE) {
3934 struct page **pages;
3935 int num_pages = (len + PAGE_CACHE_SIZE - 1) >>
3936 PAGE_CACHE_SHIFT;
3937 pages = kzalloc(num_pages, mask);
3938 if (!pages) {
3939 __free_extent_buffer(eb);
3940 return NULL;
3941 }
3942 eb->pages = pages;
3943 } else {
3944 eb->pages = eb->inline_pages;
3945 }
Chris Masond1310b22008-01-24 16:13:08 -05003946
3947 return eb;
3948}
3949
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003950static int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05003951{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003952 return (atomic_read(&eb->io_pages) ||
3953 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
3954 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05003955}
3956
Miao Xie897ca6e2010-10-26 20:57:29 -04003957/*
3958 * Helper for releasing extent buffer page.
3959 */
3960static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
3961 unsigned long start_idx)
3962{
3963 unsigned long index;
Wang Sheng-Hui39bab872012-04-06 14:35:31 +08003964 unsigned long num_pages;
Miao Xie897ca6e2010-10-26 20:57:29 -04003965 struct page *page;
3966
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003967 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e2010-10-26 20:57:29 -04003968
Wang Sheng-Hui39bab872012-04-06 14:35:31 +08003969 num_pages = num_extent_pages(eb->start, eb->len);
3970 index = start_idx + num_pages;
Miao Xie897ca6e2010-10-26 20:57:29 -04003971 if (start_idx >= index)
3972 return;
3973
3974 do {
3975 index--;
3976 page = extent_buffer_page(eb, index);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05003977 if (page) {
3978 spin_lock(&page->mapping->private_lock);
3979 /*
3980 * We do this since we'll remove the pages after we've
3981 * removed the eb from the radix tree, so we could race
3982 * and have this page now attached to the new eb. So
3983 * only clear page_private if it's still connected to
3984 * this eb.
3985 */
3986 if (PagePrivate(page) &&
3987 page->private == (unsigned long)eb) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003988 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Josef Bacik3083ee22012-03-09 16:01:49 -05003989 BUG_ON(PageDirty(page));
3990 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05003991 /*
3992 * We need to make sure we haven't be attached
3993 * to a new eb.
3994 */
3995 ClearPagePrivate(page);
3996 set_page_private(page, 0);
3997 /* One for the page private */
3998 page_cache_release(page);
3999 }
4000 spin_unlock(&page->mapping->private_lock);
4001
4002 /* One for when we alloced the page */
Miao Xie897ca6e2010-10-26 20:57:29 -04004003 page_cache_release(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004004 }
Miao Xie897ca6e2010-10-26 20:57:29 -04004005 } while (index != start_idx);
4006}
4007
4008/*
4009 * Helper for releasing the extent buffer.
4010 */
4011static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4012{
4013 btrfs_release_extent_buffer_page(eb, 0);
4014 __free_extent_buffer(eb);
4015}
4016
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004017static void check_buffer_tree_ref(struct extent_buffer *eb)
4018{
4019 /* the ref bit is tricky. We have to make sure it is set
4020 * if we have the buffer dirty. Otherwise the
4021 * code to free a buffer can end up dropping a dirty
4022 * page
4023 *
4024 * Once the ref bit is set, it won't go away while the
4025 * buffer is dirty or in writeback, and it also won't
4026 * go away while we have the reference count on the
4027 * eb bumped.
4028 *
4029 * We can't just set the ref bit without bumping the
4030 * ref on the eb because free_extent_buffer might
4031 * see the ref bit and try to clear it. If this happens
4032 * free_extent_buffer might end up dropping our original
4033 * ref by mistake and freeing the page before we are able
4034 * to add one more ref.
4035 *
4036 * So bump the ref count first, then set the bit. If someone
4037 * beat us to it, drop the ref we added.
4038 */
4039 if (!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
4040 atomic_inc(&eb->refs);
4041 if (test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4042 atomic_dec(&eb->refs);
4043 }
4044}
4045
Josef Bacik5df42352012-03-15 18:24:42 -04004046static void mark_extent_buffer_accessed(struct extent_buffer *eb)
4047{
4048 unsigned long num_pages, i;
4049
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004050 check_buffer_tree_ref(eb);
4051
Josef Bacik5df42352012-03-15 18:24:42 -04004052 num_pages = num_extent_pages(eb->start, eb->len);
4053 for (i = 0; i < num_pages; i++) {
4054 struct page *p = extent_buffer_page(eb, i);
4055 mark_page_accessed(p);
4056 }
4057}
4058
Chris Masond1310b22008-01-24 16:13:08 -05004059struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
Chris Mason727011e2010-08-06 13:21:20 -04004060 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004061{
4062 unsigned long num_pages = num_extent_pages(start, len);
4063 unsigned long i;
4064 unsigned long index = start >> PAGE_CACHE_SHIFT;
4065 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004066 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004067 struct page *p;
4068 struct address_space *mapping = tree->mapping;
4069 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004070 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004071
Miao Xie19fe0a82010-10-26 20:57:29 -04004072 rcu_read_lock();
4073 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4074 if (eb && atomic_inc_not_zero(&eb->refs)) {
4075 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004076 mark_extent_buffer_accessed(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04004077 return eb;
4078 }
Miao Xie19fe0a82010-10-26 20:57:29 -04004079 rcu_read_unlock();
Chris Mason6af118ce2008-07-22 11:18:07 -04004080
David Sterbaba144192011-04-21 01:12:06 +02004081 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
Peter2b114d12008-04-01 11:21:40 -04004082 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004083 return NULL;
4084
Chris Mason727011e2010-08-06 13:21:20 -04004085 for (i = 0; i < num_pages; i++, index++) {
Chris Masona6591712011-07-19 12:04:14 -04004086 p = find_or_create_page(mapping, index, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05004087 if (!p) {
4088 WARN_ON(1);
Chris Mason6af118ce2008-07-22 11:18:07 -04004089 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05004090 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004091
4092 spin_lock(&mapping->private_lock);
4093 if (PagePrivate(p)) {
4094 /*
4095 * We could have already allocated an eb for this page
4096 * and attached one so lets see if we can get a ref on
4097 * the existing eb, and if we can we know it's good and
4098 * we can just return that one, else we know we can just
4099 * overwrite page->private.
4100 */
4101 exists = (struct extent_buffer *)p->private;
4102 if (atomic_inc_not_zero(&exists->refs)) {
4103 spin_unlock(&mapping->private_lock);
4104 unlock_page(p);
Josef Bacik17de39a2012-05-04 15:16:06 -04004105 page_cache_release(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004106 mark_extent_buffer_accessed(exists);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004107 goto free_eb;
4108 }
4109
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004110 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004111 * Do this so attach doesn't complain and we need to
4112 * drop the ref the old guy had.
4113 */
4114 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004115 WARN_ON(PageDirty(p));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004116 page_cache_release(p);
Chris Masond1310b22008-01-24 16:13:08 -05004117 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004118 attach_extent_buffer_page(eb, p);
4119 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004120 WARN_ON(PageDirty(p));
Chris Masond1310b22008-01-24 16:13:08 -05004121 mark_page_accessed(p);
Chris Mason727011e2010-08-06 13:21:20 -04004122 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05004123 if (!PageUptodate(p))
4124 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05004125
4126 /*
4127 * see below about how we avoid a nasty race with release page
4128 * and why we unlock later
4129 */
Chris Masond1310b22008-01-24 16:13:08 -05004130 }
4131 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004132 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05004133again:
Miao Xie19fe0a82010-10-26 20:57:29 -04004134 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4135 if (ret)
4136 goto free_eb;
4137
Chris Mason6af118ce2008-07-22 11:18:07 -04004138 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004139 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
4140 if (ret == -EEXIST) {
4141 exists = radix_tree_lookup(&tree->buffer,
4142 start >> PAGE_CACHE_SHIFT);
Josef Bacik115391d2012-03-09 09:51:43 -05004143 if (!atomic_inc_not_zero(&exists->refs)) {
4144 spin_unlock(&tree->buffer_lock);
4145 radix_tree_preload_end();
Josef Bacik115391d2012-03-09 09:51:43 -05004146 exists = NULL;
4147 goto again;
4148 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004149 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004150 radix_tree_preload_end();
Josef Bacik5df42352012-03-15 18:24:42 -04004151 mark_extent_buffer_accessed(exists);
Chris Mason6af118ce2008-07-22 11:18:07 -04004152 goto free_eb;
4153 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004154 /* add one reference for the tree */
Josef Bacik3083ee22012-03-09 16:01:49 -05004155 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004156 check_buffer_tree_ref(eb);
Josef Bacik3083ee22012-03-09 16:01:49 -05004157 spin_unlock(&eb->refs_lock);
Yan, Zhengf044ba72010-02-04 08:46:56 +00004158 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004159 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05004160
4161 /*
4162 * there is a race where release page may have
4163 * tried to find this extent buffer in the radix
4164 * but failed. It will tell the VM it is safe to
4165 * reclaim the, and it will clear the page private bit.
4166 * We must make sure to set the page private bit properly
4167 * after the extent buffer is in the radix tree so
4168 * it doesn't get lost
4169 */
Chris Mason727011e2010-08-06 13:21:20 -04004170 SetPageChecked(eb->pages[0]);
4171 for (i = 1; i < num_pages; i++) {
4172 p = extent_buffer_page(eb, i);
Chris Mason727011e2010-08-06 13:21:20 -04004173 ClearPageChecked(p);
4174 unlock_page(p);
4175 }
4176 unlock_page(eb->pages[0]);
Chris Masond1310b22008-01-24 16:13:08 -05004177 return eb;
4178
Chris Mason6af118ce2008-07-22 11:18:07 -04004179free_eb:
Chris Mason727011e2010-08-06 13:21:20 -04004180 for (i = 0; i < num_pages; i++) {
4181 if (eb->pages[i])
4182 unlock_page(eb->pages[i]);
4183 }
Chris Masoneb14ab82011-02-10 12:35:00 -05004184
Josef Bacik17de39a2012-05-04 15:16:06 -04004185 WARN_ON(!atomic_dec_and_test(&eb->refs));
Miao Xie897ca6e2010-10-26 20:57:29 -04004186 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04004187 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05004188}
Chris Masond1310b22008-01-24 16:13:08 -05004189
4190struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
David Sterbaf09d1f62011-04-21 01:08:01 +02004191 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004192{
Chris Masond1310b22008-01-24 16:13:08 -05004193 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05004194
Miao Xie19fe0a82010-10-26 20:57:29 -04004195 rcu_read_lock();
4196 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4197 if (eb && atomic_inc_not_zero(&eb->refs)) {
4198 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004199 mark_extent_buffer_accessed(eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04004200 return eb;
4201 }
4202 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04004203
Miao Xie19fe0a82010-10-26 20:57:29 -04004204 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004205}
Chris Masond1310b22008-01-24 16:13:08 -05004206
Josef Bacik3083ee22012-03-09 16:01:49 -05004207static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4208{
4209 struct extent_buffer *eb =
4210 container_of(head, struct extent_buffer, rcu_head);
4211
4212 __free_extent_buffer(eb);
4213}
4214
Josef Bacik3083ee22012-03-09 16:01:49 -05004215/* Expects to have eb->eb_lock already held */
4216static void release_extent_buffer(struct extent_buffer *eb, gfp_t mask)
4217{
4218 WARN_ON(atomic_read(&eb->refs) == 0);
4219 if (atomic_dec_and_test(&eb->refs)) {
4220 struct extent_io_tree *tree = eb->tree;
Josef Bacik3083ee22012-03-09 16:01:49 -05004221
4222 spin_unlock(&eb->refs_lock);
4223
Josef Bacik3083ee22012-03-09 16:01:49 -05004224 spin_lock(&tree->buffer_lock);
4225 radix_tree_delete(&tree->buffer,
4226 eb->start >> PAGE_CACHE_SHIFT);
4227 spin_unlock(&tree->buffer_lock);
4228
4229 /* Should be safe to release our pages at this point */
4230 btrfs_release_extent_buffer_page(eb, 0);
4231
4232 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
4233 return;
4234 }
4235 spin_unlock(&eb->refs_lock);
4236}
4237
Chris Masond1310b22008-01-24 16:13:08 -05004238void free_extent_buffer(struct extent_buffer *eb)
4239{
Chris Masond1310b22008-01-24 16:13:08 -05004240 if (!eb)
4241 return;
4242
Josef Bacik3083ee22012-03-09 16:01:49 -05004243 spin_lock(&eb->refs_lock);
4244 if (atomic_read(&eb->refs) == 2 &&
4245 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004246 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004247 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4248 atomic_dec(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05004249
Josef Bacik3083ee22012-03-09 16:01:49 -05004250 /*
4251 * I know this is terrible, but it's temporary until we stop tracking
4252 * the uptodate bits and such for the extent buffers.
4253 */
4254 release_extent_buffer(eb, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05004255}
Chris Masond1310b22008-01-24 16:13:08 -05004256
Josef Bacik3083ee22012-03-09 16:01:49 -05004257void free_extent_buffer_stale(struct extent_buffer *eb)
4258{
4259 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004260 return;
4261
Josef Bacik3083ee22012-03-09 16:01:49 -05004262 spin_lock(&eb->refs_lock);
4263 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
4264
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004265 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004266 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4267 atomic_dec(&eb->refs);
4268 release_extent_buffer(eb, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05004269}
4270
Chris Mason1d4284b2012-03-28 20:31:37 -04004271void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004272{
Chris Masond1310b22008-01-24 16:13:08 -05004273 unsigned long i;
4274 unsigned long num_pages;
4275 struct page *page;
4276
Chris Masond1310b22008-01-24 16:13:08 -05004277 num_pages = num_extent_pages(eb->start, eb->len);
4278
4279 for (i = 0; i < num_pages; i++) {
4280 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04004281 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05004282 continue;
4283
Chris Masona61e6f22008-07-22 11:18:08 -04004284 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05004285 WARN_ON(!PagePrivate(page));
4286
Chris Masond1310b22008-01-24 16:13:08 -05004287 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004288 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05004289 if (!PageDirty(page)) {
4290 radix_tree_tag_clear(&page->mapping->page_tree,
4291 page_index(page),
4292 PAGECACHE_TAG_DIRTY);
4293 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004294 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04004295 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04004296 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05004297 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004298 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05004299}
Chris Masond1310b22008-01-24 16:13:08 -05004300
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004301int set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004302{
4303 unsigned long i;
4304 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04004305 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004306
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004307 check_buffer_tree_ref(eb);
4308
Chris Masonb9473432009-03-13 11:00:37 -04004309 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004310
Chris Masond1310b22008-01-24 16:13:08 -05004311 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik3083ee22012-03-09 16:01:49 -05004312 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004313 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
4314
Chris Masonb9473432009-03-13 11:00:37 -04004315 for (i = 0; i < num_pages; i++)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004316 set_page_dirty(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04004317 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05004318}
Chris Masond1310b22008-01-24 16:13:08 -05004319
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004320static int range_straddles_pages(u64 start, u64 len)
Chris Mason19b6caf2011-07-25 06:50:50 -04004321{
4322 if (len < PAGE_CACHE_SIZE)
4323 return 1;
4324 if (start & (PAGE_CACHE_SIZE - 1))
4325 return 1;
4326 if ((start + len) & (PAGE_CACHE_SIZE - 1))
4327 return 1;
4328 return 0;
4329}
4330
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004331int clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04004332{
4333 unsigned long i;
4334 struct page *page;
4335 unsigned long num_pages;
4336
Chris Masonb4ce94d2009-02-04 09:25:08 -05004337 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004338 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason1259ab72008-05-12 13:39:03 -04004339 for (i = 0; i < num_pages; i++) {
4340 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04004341 if (page)
4342 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04004343 }
4344 return 0;
4345}
4346
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004347int set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004348{
4349 unsigned long i;
4350 struct page *page;
4351 unsigned long num_pages;
4352
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004353 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004354 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05004355 for (i = 0; i < num_pages; i++) {
4356 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004357 SetPageUptodate(page);
4358 }
4359 return 0;
4360}
Chris Masond1310b22008-01-24 16:13:08 -05004361
Chris Masonce9adaa2008-04-09 16:28:12 -04004362int extent_range_uptodate(struct extent_io_tree *tree,
4363 u64 start, u64 end)
4364{
4365 struct page *page;
4366 int ret;
4367 int pg_uptodate = 1;
4368 int uptodate;
4369 unsigned long index;
4370
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004371 if (range_straddles_pages(start, end - start + 1)) {
Chris Mason19b6caf2011-07-25 06:50:50 -04004372 ret = test_range_bit(tree, start, end,
4373 EXTENT_UPTODATE, 1, NULL);
4374 if (ret)
4375 return 1;
4376 }
Chris Masond3977122009-01-05 21:25:51 -05004377 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004378 index = start >> PAGE_CACHE_SHIFT;
4379 page = find_get_page(tree->mapping, index);
Mitch Harder8bedd512012-01-26 15:01:11 -05004380 if (!page)
4381 return 1;
Chris Masonce9adaa2008-04-09 16:28:12 -04004382 uptodate = PageUptodate(page);
4383 page_cache_release(page);
4384 if (!uptodate) {
4385 pg_uptodate = 0;
4386 break;
4387 }
4388 start += PAGE_CACHE_SIZE;
4389 }
4390 return pg_uptodate;
4391}
4392
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004393int extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004394{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004395 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004396}
Chris Masond1310b22008-01-24 16:13:08 -05004397
4398int read_extent_buffer_pages(struct extent_io_tree *tree,
Arne Jansenbb82ab82011-06-10 14:06:53 +02004399 struct extent_buffer *eb, u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04004400 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05004401{
4402 unsigned long i;
4403 unsigned long start_i;
4404 struct page *page;
4405 int err;
4406 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04004407 int locked_pages = 0;
4408 int all_uptodate = 1;
Chris Masond1310b22008-01-24 16:13:08 -05004409 unsigned long num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04004410 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004411 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004412 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004413
Chris Masonb4ce94d2009-02-04 09:25:08 -05004414 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05004415 return 0;
4416
Chris Masond1310b22008-01-24 16:13:08 -05004417 if (start) {
4418 WARN_ON(start < eb->start);
4419 start_i = (start >> PAGE_CACHE_SHIFT) -
4420 (eb->start >> PAGE_CACHE_SHIFT);
4421 } else {
4422 start_i = 0;
4423 }
4424
4425 num_pages = num_extent_pages(eb->start, eb->len);
4426 for (i = start_i; i < num_pages; i++) {
4427 page = extent_buffer_page(eb, i);
Arne Jansenbb82ab82011-06-10 14:06:53 +02004428 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04004429 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04004430 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05004431 } else {
4432 lock_page(page);
4433 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004434 locked_pages++;
Chris Mason727011e2010-08-06 13:21:20 -04004435 if (!PageUptodate(page)) {
4436 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04004437 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04004438 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004439 }
4440 if (all_uptodate) {
4441 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004442 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04004443 goto unlock_exit;
4444 }
4445
Josef Bacikea466792012-03-26 21:57:36 -04004446 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04004447 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004448 atomic_set(&eb->io_pages, num_reads);
Chris Masonce9adaa2008-04-09 16:28:12 -04004449 for (i = start_i; i < num_pages; i++) {
4450 page = extent_buffer_page(eb, i);
Chris Masonce9adaa2008-04-09 16:28:12 -04004451 if (!PageUptodate(page)) {
Chris Masonf1885912008-04-09 16:28:12 -04004452 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05004453 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04004454 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04004455 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05004456 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05004457 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05004458 } else {
4459 unlock_page(page);
4460 }
4461 }
4462
Jeff Mahoney355808c2011-10-03 23:23:14 -04004463 if (bio) {
4464 err = submit_one_bio(READ, bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004465 if (err)
4466 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04004467 }
Chris Masona86c12c2008-02-07 10:50:54 -05004468
Arne Jansenbb82ab82011-06-10 14:06:53 +02004469 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05004470 return ret;
Chris Masond3977122009-01-05 21:25:51 -05004471
Chris Masond1310b22008-01-24 16:13:08 -05004472 for (i = start_i; i < num_pages; i++) {
4473 page = extent_buffer_page(eb, i);
4474 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05004475 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05004476 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05004477 }
Chris Masond3977122009-01-05 21:25:51 -05004478
Chris Masond1310b22008-01-24 16:13:08 -05004479 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04004480
4481unlock_exit:
4482 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05004483 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004484 page = extent_buffer_page(eb, i);
4485 i++;
4486 unlock_page(page);
4487 locked_pages--;
4488 }
4489 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004490}
Chris Masond1310b22008-01-24 16:13:08 -05004491
4492void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4493 unsigned long start,
4494 unsigned long len)
4495{
4496 size_t cur;
4497 size_t offset;
4498 struct page *page;
4499 char *kaddr;
4500 char *dst = (char *)dstv;
4501 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4502 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004503
4504 WARN_ON(start > eb->len);
4505 WARN_ON(start + len > eb->start + eb->len);
4506
4507 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4508
Chris Masond3977122009-01-05 21:25:51 -05004509 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004510 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004511
4512 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04004513 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004514 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004515
4516 dst += cur;
4517 len -= cur;
4518 offset = 0;
4519 i++;
4520 }
4521}
Chris Masond1310b22008-01-24 16:13:08 -05004522
4523int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04004524 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05004525 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04004526 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05004527{
4528 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4529 char *kaddr;
4530 struct page *p;
4531 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4532 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4533 unsigned long end_i = (start_offset + start + min_len - 1) >>
4534 PAGE_CACHE_SHIFT;
4535
4536 if (i != end_i)
4537 return -EINVAL;
4538
4539 if (i == 0) {
4540 offset = start_offset;
4541 *map_start = 0;
4542 } else {
4543 offset = 0;
4544 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4545 }
Chris Masond3977122009-01-05 21:25:51 -05004546
Chris Masond1310b22008-01-24 16:13:08 -05004547 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05004548 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
4549 "wanted %lu %lu\n", (unsigned long long)eb->start,
4550 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05004551 WARN_ON(1);
Josef Bacik850265332011-03-15 14:52:12 -04004552 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05004553 }
4554
4555 p = extent_buffer_page(eb, i);
Chris Masona6591712011-07-19 12:04:14 -04004556 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05004557 *map = kaddr + offset;
4558 *map_len = PAGE_CACHE_SIZE - offset;
4559 return 0;
4560}
Chris Masond1310b22008-01-24 16:13:08 -05004561
Chris Masond1310b22008-01-24 16:13:08 -05004562int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4563 unsigned long start,
4564 unsigned long len)
4565{
4566 size_t cur;
4567 size_t offset;
4568 struct page *page;
4569 char *kaddr;
4570 char *ptr = (char *)ptrv;
4571 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4572 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4573 int ret = 0;
4574
4575 WARN_ON(start > eb->len);
4576 WARN_ON(start + len > eb->start + eb->len);
4577
4578 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4579
Chris Masond3977122009-01-05 21:25:51 -05004580 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004581 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004582
4583 cur = min(len, (PAGE_CACHE_SIZE - offset));
4584
Chris Masona6591712011-07-19 12:04:14 -04004585 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004586 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004587 if (ret)
4588 break;
4589
4590 ptr += cur;
4591 len -= cur;
4592 offset = 0;
4593 i++;
4594 }
4595 return ret;
4596}
Chris Masond1310b22008-01-24 16:13:08 -05004597
4598void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4599 unsigned long start, unsigned long len)
4600{
4601 size_t cur;
4602 size_t offset;
4603 struct page *page;
4604 char *kaddr;
4605 char *src = (char *)srcv;
4606 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4607 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4608
4609 WARN_ON(start > eb->len);
4610 WARN_ON(start + len > eb->start + eb->len);
4611
4612 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4613
Chris Masond3977122009-01-05 21:25:51 -05004614 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004615 page = extent_buffer_page(eb, i);
4616 WARN_ON(!PageUptodate(page));
4617
4618 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004619 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004620 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004621
4622 src += cur;
4623 len -= cur;
4624 offset = 0;
4625 i++;
4626 }
4627}
Chris Masond1310b22008-01-24 16:13:08 -05004628
4629void memset_extent_buffer(struct extent_buffer *eb, char c,
4630 unsigned long start, unsigned long len)
4631{
4632 size_t cur;
4633 size_t offset;
4634 struct page *page;
4635 char *kaddr;
4636 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4637 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4638
4639 WARN_ON(start > eb->len);
4640 WARN_ON(start + len > eb->start + eb->len);
4641
4642 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4643
Chris Masond3977122009-01-05 21:25:51 -05004644 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004645 page = extent_buffer_page(eb, i);
4646 WARN_ON(!PageUptodate(page));
4647
4648 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004649 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004650 memset(kaddr + offset, c, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004651
4652 len -= cur;
4653 offset = 0;
4654 i++;
4655 }
4656}
Chris Masond1310b22008-01-24 16:13:08 -05004657
4658void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4659 unsigned long dst_offset, unsigned long src_offset,
4660 unsigned long len)
4661{
4662 u64 dst_len = dst->len;
4663 size_t cur;
4664 size_t offset;
4665 struct page *page;
4666 char *kaddr;
4667 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4668 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4669
4670 WARN_ON(src->len != dst_len);
4671
4672 offset = (start_offset + dst_offset) &
4673 ((unsigned long)PAGE_CACHE_SIZE - 1);
4674
Chris Masond3977122009-01-05 21:25:51 -05004675 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004676 page = extent_buffer_page(dst, i);
4677 WARN_ON(!PageUptodate(page));
4678
4679 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4680
Chris Masona6591712011-07-19 12:04:14 -04004681 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004682 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004683
4684 src_offset += cur;
4685 len -= cur;
4686 offset = 0;
4687 i++;
4688 }
4689}
Chris Masond1310b22008-01-24 16:13:08 -05004690
4691static void move_pages(struct page *dst_page, struct page *src_page,
4692 unsigned long dst_off, unsigned long src_off,
4693 unsigned long len)
4694{
Chris Masona6591712011-07-19 12:04:14 -04004695 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004696 if (dst_page == src_page) {
4697 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4698 } else {
Chris Masona6591712011-07-19 12:04:14 -04004699 char *src_kaddr = page_address(src_page);
Chris Masond1310b22008-01-24 16:13:08 -05004700 char *p = dst_kaddr + dst_off + len;
4701 char *s = src_kaddr + src_off + len;
4702
4703 while (len--)
4704 *--p = *--s;
Chris Masond1310b22008-01-24 16:13:08 -05004705 }
Chris Masond1310b22008-01-24 16:13:08 -05004706}
4707
Sergei Trofimovich33872062011-04-11 21:52:52 +00004708static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4709{
4710 unsigned long distance = (src > dst) ? src - dst : dst - src;
4711 return distance < len;
4712}
4713
Chris Masond1310b22008-01-24 16:13:08 -05004714static void copy_pages(struct page *dst_page, struct page *src_page,
4715 unsigned long dst_off, unsigned long src_off,
4716 unsigned long len)
4717{
Chris Masona6591712011-07-19 12:04:14 -04004718 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004719 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004720 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004721
Sergei Trofimovich33872062011-04-11 21:52:52 +00004722 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04004723 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00004724 } else {
Chris Masond1310b22008-01-24 16:13:08 -05004725 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004726 if (areas_overlap(src_off, dst_off, len))
4727 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00004728 }
Chris Masond1310b22008-01-24 16:13:08 -05004729
Chris Mason727011e2010-08-06 13:21:20 -04004730 if (must_memmove)
4731 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
4732 else
4733 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05004734}
4735
4736void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4737 unsigned long src_offset, unsigned long len)
4738{
4739 size_t cur;
4740 size_t dst_off_in_page;
4741 size_t src_off_in_page;
4742 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4743 unsigned long dst_i;
4744 unsigned long src_i;
4745
4746 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004747 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4748 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004749 BUG_ON(1);
4750 }
4751 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004752 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4753 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004754 BUG_ON(1);
4755 }
4756
Chris Masond3977122009-01-05 21:25:51 -05004757 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004758 dst_off_in_page = (start_offset + dst_offset) &
4759 ((unsigned long)PAGE_CACHE_SIZE - 1);
4760 src_off_in_page = (start_offset + src_offset) &
4761 ((unsigned long)PAGE_CACHE_SIZE - 1);
4762
4763 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4764 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4765
4766 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4767 src_off_in_page));
4768 cur = min_t(unsigned long, cur,
4769 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4770
4771 copy_pages(extent_buffer_page(dst, dst_i),
4772 extent_buffer_page(dst, src_i),
4773 dst_off_in_page, src_off_in_page, cur);
4774
4775 src_offset += cur;
4776 dst_offset += cur;
4777 len -= cur;
4778 }
4779}
Chris Masond1310b22008-01-24 16:13:08 -05004780
4781void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4782 unsigned long src_offset, unsigned long len)
4783{
4784 size_t cur;
4785 size_t dst_off_in_page;
4786 size_t src_off_in_page;
4787 unsigned long dst_end = dst_offset + len - 1;
4788 unsigned long src_end = src_offset + len - 1;
4789 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4790 unsigned long dst_i;
4791 unsigned long src_i;
4792
4793 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004794 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4795 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004796 BUG_ON(1);
4797 }
4798 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004799 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4800 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004801 BUG_ON(1);
4802 }
Chris Mason727011e2010-08-06 13:21:20 -04004803 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05004804 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4805 return;
4806 }
Chris Masond3977122009-01-05 21:25:51 -05004807 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004808 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
4809 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
4810
4811 dst_off_in_page = (start_offset + dst_end) &
4812 ((unsigned long)PAGE_CACHE_SIZE - 1);
4813 src_off_in_page = (start_offset + src_end) &
4814 ((unsigned long)PAGE_CACHE_SIZE - 1);
4815
4816 cur = min_t(unsigned long, len, src_off_in_page + 1);
4817 cur = min(cur, dst_off_in_page + 1);
4818 move_pages(extent_buffer_page(dst, dst_i),
4819 extent_buffer_page(dst, src_i),
4820 dst_off_in_page - cur + 1,
4821 src_off_in_page - cur + 1, cur);
4822
4823 dst_end -= cur;
4824 src_end -= cur;
4825 len -= cur;
4826 }
4827}
Chris Mason6af118ce2008-07-22 11:18:07 -04004828
Josef Bacik3083ee22012-03-09 16:01:49 -05004829int try_release_extent_buffer(struct page *page, gfp_t mask)
Miao Xie19fe0a82010-10-26 20:57:29 -04004830{
Chris Mason6af118ce2008-07-22 11:18:07 -04004831 struct extent_buffer *eb;
Miao Xie897ca6e2010-10-26 20:57:29 -04004832
Miao Xie19fe0a82010-10-26 20:57:29 -04004833 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05004834 * We need to make sure noboody is attaching this page to an eb right
4835 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04004836 */
Josef Bacik3083ee22012-03-09 16:01:49 -05004837 spin_lock(&page->mapping->private_lock);
4838 if (!PagePrivate(page)) {
4839 spin_unlock(&page->mapping->private_lock);
4840 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004841 }
4842
Josef Bacik3083ee22012-03-09 16:01:49 -05004843 eb = (struct extent_buffer *)page->private;
4844 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04004845
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004846 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05004847 * This is a little awful but should be ok, we need to make sure that
4848 * the eb doesn't disappear out from under us while we're looking at
4849 * this page.
4850 */
4851 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004852 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05004853 spin_unlock(&eb->refs_lock);
4854 spin_unlock(&page->mapping->private_lock);
4855 return 0;
4856 }
4857 spin_unlock(&page->mapping->private_lock);
4858
4859 if ((mask & GFP_NOFS) == GFP_NOFS)
4860 mask = GFP_NOFS;
4861
4862 /*
4863 * If tree ref isn't set then we know the ref on this eb is a real ref,
4864 * so just return, this page will likely be freed soon anyway.
4865 */
4866 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
4867 spin_unlock(&eb->refs_lock);
4868 return 0;
4869 }
4870 release_extent_buffer(eb, mask);
4871
4872 return 1;
Chris Mason6af118ce2008-07-22 11:18:07 -04004873}