blob: 82b4829f00b8202e23226708e795e1cdc172ed24 [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) {
Jeff Mahoney6763af82012-03-01 14:56:29 +0100572 clear_state_bit(tree, state, &bits, wake);
Yan Zheng5c939df2009-05-27 09:16:03 -0400573 if (last_end == (u64)-1)
574 goto out;
575 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500576 }
577 goto search_again;
578 }
579 /*
580 * | ---- desired range ---- |
581 * | state |
582 * We need to split the extent, and clear the bit
583 * on the first half
584 */
585 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000586 prealloc = alloc_extent_state_atomic(prealloc);
587 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500588 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400589 if (err)
590 extent_io_tree_panic(tree, err);
591
Chris Masond1310b22008-01-24 16:13:08 -0500592 if (wake)
593 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400594
Jeff Mahoney6763af82012-03-01 14:56:29 +0100595 clear_state_bit(tree, prealloc, &bits, wake);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400596
Chris Masond1310b22008-01-24 16:13:08 -0500597 prealloc = NULL;
598 goto out;
599 }
Chris Mason42daec22009-09-23 19:51:09 -0400600
Li Zefancdc6a392012-03-12 16:39:48 +0800601 state = clear_state_bit(tree, state, &bits, wake);
Liu Bo04493142012-02-16 18:34:37 +0800602next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400603 if (last_end == (u64)-1)
604 goto out;
605 start = last_end + 1;
Li Zefancdc6a392012-03-12 16:39:48 +0800606 if (start <= end && state && !need_resched())
Liu Bo692e5752012-02-16 18:34:36 +0800607 goto hit_next;
Chris Masond1310b22008-01-24 16:13:08 -0500608 goto search_again;
609
610out:
Chris Masoncad321a2008-12-17 14:51:42 -0500611 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500612 if (prealloc)
613 free_extent_state(prealloc);
614
Jeff Mahoney6763af82012-03-01 14:56:29 +0100615 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500616
617search_again:
618 if (start > end)
619 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500620 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500621 if (mask & __GFP_WAIT)
622 cond_resched();
623 goto again;
624}
Chris Masond1310b22008-01-24 16:13:08 -0500625
Jeff Mahoney143bede2012-03-01 14:56:26 +0100626static void wait_on_state(struct extent_io_tree *tree,
627 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500628 __releases(tree->lock)
629 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500630{
631 DEFINE_WAIT(wait);
632 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500633 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500634 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500635 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500636 finish_wait(&state->wq, &wait);
Chris Masond1310b22008-01-24 16:13:08 -0500637}
638
639/*
640 * waits for one or more bits to clear on a range in the state tree.
641 * The range [start, end] is inclusive.
642 * The tree lock is taken by this function
643 */
Jeff Mahoney143bede2012-03-01 14:56:26 +0100644void wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
Chris Masond1310b22008-01-24 16:13:08 -0500645{
646 struct extent_state *state;
647 struct rb_node *node;
648
Chris Masoncad321a2008-12-17 14:51:42 -0500649 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500650again:
651 while (1) {
652 /*
653 * this search will find all the extents that end after
654 * our range starts
655 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500656 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500657 if (!node)
658 break;
659
660 state = rb_entry(node, struct extent_state, rb_node);
661
662 if (state->start > end)
663 goto out;
664
665 if (state->state & bits) {
666 start = state->start;
667 atomic_inc(&state->refs);
668 wait_on_state(tree, state);
669 free_extent_state(state);
670 goto again;
671 }
672 start = state->end + 1;
673
674 if (start > end)
675 break;
676
Xiao Guangrongded91f02011-07-14 03:19:27 +0000677 cond_resched_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500678 }
679out:
Chris Masoncad321a2008-12-17 14:51:42 -0500680 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500681}
Chris Masond1310b22008-01-24 16:13:08 -0500682
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000683static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500684 struct extent_state *state,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400685 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500686{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400687 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400688
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000689 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400690 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500691 u64 range = state->end - state->start + 1;
692 tree->dirty_bytes += range;
693 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400694 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500695}
696
Chris Mason2c64c532009-09-02 15:04:12 -0400697static void cache_state(struct extent_state *state,
698 struct extent_state **cached_ptr)
699{
700 if (cached_ptr && !(*cached_ptr)) {
701 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
702 *cached_ptr = state;
703 atomic_inc(&state->refs);
704 }
705 }
706}
707
Arne Jansen507903b2011-04-06 10:02:20 +0000708static void uncache_state(struct extent_state **cached_ptr)
709{
710 if (cached_ptr && (*cached_ptr)) {
711 struct extent_state *state = *cached_ptr;
Chris Mason109b36a2011-04-12 13:57:39 -0400712 *cached_ptr = NULL;
713 free_extent_state(state);
Arne Jansen507903b2011-04-06 10:02:20 +0000714 }
715}
716
Chris Masond1310b22008-01-24 16:13:08 -0500717/*
Chris Mason1edbb732009-09-02 13:24:36 -0400718 * set some bits on a range in the tree. This may require allocations or
719 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500720 *
Chris Mason1edbb732009-09-02 13:24:36 -0400721 * If any of the exclusive bits are set, this will fail with -EEXIST if some
722 * part of the range already has the desired bits set. The start of the
723 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500724 *
Chris Mason1edbb732009-09-02 13:24:36 -0400725 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500726 */
Chris Mason1edbb732009-09-02 13:24:36 -0400727
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100728static int __must_check
729__set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
730 int bits, int exclusive_bits, u64 *failed_start,
731 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500732{
733 struct extent_state *state;
734 struct extent_state *prealloc = NULL;
735 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500736 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500737 u64 last_start;
738 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400739
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400740 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500741again:
742 if (!prealloc && (mask & __GFP_WAIT)) {
743 prealloc = alloc_extent_state(mask);
Xiao Guangrong82337672011-04-20 06:44:57 +0000744 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500745 }
746
Chris Masoncad321a2008-12-17 14:51:42 -0500747 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400748 if (cached_state && *cached_state) {
749 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400750 if (state->start <= start && state->end > start &&
751 state->tree) {
Chris Mason9655d292009-09-02 15:22:30 -0400752 node = &state->rb_node;
753 goto hit_next;
754 }
755 }
Chris Masond1310b22008-01-24 16:13:08 -0500756 /*
757 * this search will find all the extents that end after
758 * our range starts.
759 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500760 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500761 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000762 prealloc = alloc_extent_state_atomic(prealloc);
763 BUG_ON(!prealloc);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400764 err = insert_state(tree, prealloc, start, end, &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400765 if (err)
766 extent_io_tree_panic(tree, err);
767
Chris Masond1310b22008-01-24 16:13:08 -0500768 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500769 goto out;
770 }
Chris Masond1310b22008-01-24 16:13:08 -0500771 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400772hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500773 last_start = state->start;
774 last_end = state->end;
775
776 /*
777 * | ---- desired range ---- |
778 * | state |
779 *
780 * Just lock what we found and keep going
781 */
782 if (state->start == start && state->end <= end) {
Chris Mason40431d62009-08-05 12:57:59 -0400783 struct rb_node *next_node;
Chris Mason1edbb732009-09-02 13:24:36 -0400784 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500785 *failed_start = state->start;
786 err = -EEXIST;
787 goto out;
788 }
Chris Mason42daec22009-09-23 19:51:09 -0400789
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000790 set_state_bits(tree, state, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400791
Chris Mason2c64c532009-09-02 15:04:12 -0400792 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500793 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400794 if (last_end == (u64)-1)
795 goto out;
Chris Mason40431d62009-08-05 12:57:59 -0400796
Yan Zheng5c939df2009-05-27 09:16:03 -0400797 start = last_end + 1;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400798 next_node = rb_next(&state->rb_node);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000799 if (next_node && start < end && prealloc && !need_resched()) {
800 state = rb_entry(next_node, struct extent_state,
801 rb_node);
802 if (state->start == start)
803 goto hit_next;
Chris Mason40431d62009-08-05 12:57:59 -0400804 }
Chris Masond1310b22008-01-24 16:13:08 -0500805 goto search_again;
806 }
807
808 /*
809 * | ---- desired range ---- |
810 * | state |
811 * or
812 * | ------------- state -------------- |
813 *
814 * We need to split the extent we found, and may flip bits on
815 * second half.
816 *
817 * If the extent we found extends past our
818 * range, we just split and search again. It'll get split
819 * again the next time though.
820 *
821 * If the extent we found is inside our range, we set the
822 * desired bit on it.
823 */
824 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400825 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500826 *failed_start = start;
827 err = -EEXIST;
828 goto out;
829 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000830
831 prealloc = alloc_extent_state_atomic(prealloc);
832 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500833 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400834 if (err)
835 extent_io_tree_panic(tree, err);
836
Chris Masond1310b22008-01-24 16:13:08 -0500837 prealloc = NULL;
838 if (err)
839 goto out;
840 if (state->end <= end) {
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000841 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400842 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500843 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400844 if (last_end == (u64)-1)
845 goto out;
846 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500847 }
848 goto search_again;
849 }
850 /*
851 * | ---- desired range ---- |
852 * | state | or | state |
853 *
854 * There's a hole, we need to insert something in it and
855 * ignore the extent we found.
856 */
857 if (state->start > start) {
858 u64 this_end;
859 if (end < last_start)
860 this_end = end;
861 else
Chris Masond3977122009-01-05 21:25:51 -0500862 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000863
864 prealloc = alloc_extent_state_atomic(prealloc);
865 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000866
867 /*
868 * Avoid to free 'prealloc' if it can be merged with
869 * the later extent.
870 */
Chris Masond1310b22008-01-24 16:13:08 -0500871 err = insert_state(tree, prealloc, start, this_end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400872 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400873 if (err)
874 extent_io_tree_panic(tree, err);
875
Chris Mason2c64c532009-09-02 15:04:12 -0400876 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500877 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500878 start = this_end + 1;
879 goto search_again;
880 }
881 /*
882 * | ---- desired range ---- |
883 * | state |
884 * We need to split the extent, and set the bit
885 * on the first half
886 */
887 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400888 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500889 *failed_start = start;
890 err = -EEXIST;
891 goto out;
892 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000893
894 prealloc = alloc_extent_state_atomic(prealloc);
895 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500896 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400897 if (err)
898 extent_io_tree_panic(tree, err);
Chris Masond1310b22008-01-24 16:13:08 -0500899
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000900 set_state_bits(tree, prealloc, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400901 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500902 merge_state(tree, prealloc);
903 prealloc = NULL;
904 goto out;
905 }
906
907 goto search_again;
908
909out:
Chris Masoncad321a2008-12-17 14:51:42 -0500910 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500911 if (prealloc)
912 free_extent_state(prealloc);
913
914 return err;
915
916search_again:
917 if (start > end)
918 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500919 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500920 if (mask & __GFP_WAIT)
921 cond_resched();
922 goto again;
923}
Chris Masond1310b22008-01-24 16:13:08 -0500924
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +0100925int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits,
926 u64 *failed_start, struct extent_state **cached_state,
927 gfp_t mask)
928{
929 return __set_extent_bit(tree, start, end, bits, 0, failed_start,
930 cached_state, mask);
931}
932
933
Josef Bacik462d6fa2011-09-26 13:56:12 -0400934/**
935 * convert_extent - convert all bits in a given range from one bit to another
936 * @tree: the io tree to search
937 * @start: the start offset in bytes
938 * @end: the end offset in bytes (inclusive)
939 * @bits: the bits to set in this range
940 * @clear_bits: the bits to clear in this range
941 * @mask: the allocation mask
942 *
943 * This will go through and set bits for the given range. If any states exist
944 * already in this range they are set with the given bit and cleared of the
945 * clear_bits. This is only meant to be used by things that are mergeable, ie
946 * converting from say DELALLOC to DIRTY. This is not meant to be used with
947 * boundary bits like LOCK.
948 */
949int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
950 int bits, int clear_bits, gfp_t mask)
951{
952 struct extent_state *state;
953 struct extent_state *prealloc = NULL;
954 struct rb_node *node;
955 int err = 0;
956 u64 last_start;
957 u64 last_end;
958
959again:
960 if (!prealloc && (mask & __GFP_WAIT)) {
961 prealloc = alloc_extent_state(mask);
962 if (!prealloc)
963 return -ENOMEM;
964 }
965
966 spin_lock(&tree->lock);
967 /*
968 * this search will find all the extents that end after
969 * our range starts.
970 */
971 node = tree_search(tree, start);
972 if (!node) {
973 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -0500974 if (!prealloc) {
975 err = -ENOMEM;
976 goto out;
977 }
Josef Bacik462d6fa2011-09-26 13:56:12 -0400978 err = insert_state(tree, prealloc, start, end, &bits);
979 prealloc = NULL;
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -0400980 if (err)
981 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -0400982 goto out;
983 }
984 state = rb_entry(node, struct extent_state, rb_node);
985hit_next:
986 last_start = state->start;
987 last_end = state->end;
988
989 /*
990 * | ---- desired range ---- |
991 * | state |
992 *
993 * Just lock what we found and keep going
994 */
995 if (state->start == start && state->end <= end) {
996 struct rb_node *next_node;
997
998 set_state_bits(tree, state, &bits);
999 clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001000 if (last_end == (u64)-1)
1001 goto out;
1002
1003 start = last_end + 1;
1004 next_node = rb_next(&state->rb_node);
1005 if (next_node && start < end && prealloc && !need_resched()) {
1006 state = rb_entry(next_node, struct extent_state,
1007 rb_node);
1008 if (state->start == start)
1009 goto hit_next;
1010 }
1011 goto search_again;
1012 }
1013
1014 /*
1015 * | ---- desired range ---- |
1016 * | state |
1017 * or
1018 * | ------------- state -------------- |
1019 *
1020 * We need to split the extent we found, and may flip bits on
1021 * second half.
1022 *
1023 * If the extent we found extends past our
1024 * range, we just split and search again. It'll get split
1025 * again the next time though.
1026 *
1027 * If the extent we found is inside our range, we set the
1028 * desired bit on it.
1029 */
1030 if (state->start < start) {
1031 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001032 if (!prealloc) {
1033 err = -ENOMEM;
1034 goto out;
1035 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001036 err = split_state(tree, state, prealloc, start);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001037 if (err)
1038 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001039 prealloc = NULL;
1040 if (err)
1041 goto out;
1042 if (state->end <= end) {
1043 set_state_bits(tree, state, &bits);
1044 clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001045 if (last_end == (u64)-1)
1046 goto out;
1047 start = last_end + 1;
1048 }
1049 goto search_again;
1050 }
1051 /*
1052 * | ---- desired range ---- |
1053 * | state | or | state |
1054 *
1055 * There's a hole, we need to insert something in it and
1056 * ignore the extent we found.
1057 */
1058 if (state->start > start) {
1059 u64 this_end;
1060 if (end < last_start)
1061 this_end = end;
1062 else
1063 this_end = last_start - 1;
1064
1065 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001066 if (!prealloc) {
1067 err = -ENOMEM;
1068 goto out;
1069 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001070
1071 /*
1072 * Avoid to free 'prealloc' if it can be merged with
1073 * the later extent.
1074 */
1075 err = insert_state(tree, prealloc, start, this_end,
1076 &bits);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001077 if (err)
1078 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001079 prealloc = NULL;
1080 start = this_end + 1;
1081 goto search_again;
1082 }
1083 /*
1084 * | ---- desired range ---- |
1085 * | state |
1086 * We need to split the extent, and set the bit
1087 * on the first half
1088 */
1089 if (state->start <= end && state->end > end) {
1090 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001091 if (!prealloc) {
1092 err = -ENOMEM;
1093 goto out;
1094 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001095
1096 err = split_state(tree, state, prealloc, end + 1);
Jeff Mahoneyc2d904e2011-10-03 23:22:32 -04001097 if (err)
1098 extent_io_tree_panic(tree, err);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001099
1100 set_state_bits(tree, prealloc, &bits);
1101 clear_state_bit(tree, prealloc, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001102 prealloc = NULL;
1103 goto out;
1104 }
1105
1106 goto search_again;
1107
1108out:
1109 spin_unlock(&tree->lock);
1110 if (prealloc)
1111 free_extent_state(prealloc);
1112
1113 return err;
1114
1115search_again:
1116 if (start > end)
1117 goto out;
1118 spin_unlock(&tree->lock);
1119 if (mask & __GFP_WAIT)
1120 cond_resched();
1121 goto again;
1122}
1123
Chris Masond1310b22008-01-24 16:13:08 -05001124/* wrappers around set/clear extent bit */
1125int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1126 gfp_t mask)
1127{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001128 return set_extent_bit(tree, start, end, EXTENT_DIRTY, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001129 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001130}
Chris Masond1310b22008-01-24 16:13:08 -05001131
1132int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1133 int bits, gfp_t mask)
1134{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001135 return set_extent_bit(tree, start, end, bits, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001136 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001137}
Chris Masond1310b22008-01-24 16:13:08 -05001138
1139int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1140 int bits, gfp_t mask)
1141{
Chris Mason2c64c532009-09-02 15:04:12 -04001142 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001143}
Chris Masond1310b22008-01-24 16:13:08 -05001144
1145int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001146 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001147{
1148 return set_extent_bit(tree, start, end,
Liu Bofee187d2011-09-29 15:55:28 +08001149 EXTENT_DELALLOC | EXTENT_UPTODATE,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001150 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001151}
Chris Masond1310b22008-01-24 16:13:08 -05001152
1153int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1154 gfp_t mask)
1155{
1156 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04001157 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001158 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001159}
Chris Masond1310b22008-01-24 16:13:08 -05001160
1161int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1162 gfp_t mask)
1163{
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001164 return set_extent_bit(tree, start, end, EXTENT_NEW, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001165 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001166}
Chris Masond1310b22008-01-24 16:13:08 -05001167
Chris Masond1310b22008-01-24 16:13:08 -05001168int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +00001169 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001170{
Arne Jansen507903b2011-04-06 10:02:20 +00001171 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001172 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001173}
Chris Masond1310b22008-01-24 16:13:08 -05001174
Chris Masond3977122009-01-05 21:25:51 -05001175static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001176 u64 end, struct extent_state **cached_state,
1177 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001178{
Chris Mason2c64c532009-09-02 15:04:12 -04001179 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001180 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001181}
Chris Masond1310b22008-01-24 16:13:08 -05001182
Chris Masond352ac62008-09-29 15:18:18 -04001183/*
1184 * either insert or lock state struct between start and end use mask to tell
1185 * us if waiting is desired.
1186 */
Chris Mason1edbb732009-09-02 13:24:36 -04001187int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001188 int bits, struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001189{
1190 int err;
1191 u64 failed_start;
1192 while (1) {
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001193 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
1194 EXTENT_LOCKED, &failed_start,
1195 cached_state, GFP_NOFS);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001196 if (err == -EEXIST) {
Chris Masond1310b22008-01-24 16:13:08 -05001197 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1198 start = failed_start;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001199 } else
Chris Masond1310b22008-01-24 16:13:08 -05001200 break;
Chris Masond1310b22008-01-24 16:13:08 -05001201 WARN_ON(start > end);
1202 }
1203 return err;
1204}
Chris Masond1310b22008-01-24 16:13:08 -05001205
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001206int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Mason1edbb732009-09-02 13:24:36 -04001207{
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001208 return lock_extent_bits(tree, start, end, 0, NULL);
Chris Mason1edbb732009-09-02 13:24:36 -04001209}
1210
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001211int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacik25179202008-10-29 14:49:05 -04001212{
1213 int err;
1214 u64 failed_start;
1215
Jeff Mahoney3fbe5c02012-03-01 14:57:19 +01001216 err = __set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1217 &failed_start, NULL, GFP_NOFS);
Yan Zheng66435582008-10-30 14:19:50 -04001218 if (err == -EEXIST) {
1219 if (failed_start > start)
1220 clear_extent_bit(tree, start, failed_start - 1,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001221 EXTENT_LOCKED, 1, 0, NULL, GFP_NOFS);
Josef Bacik25179202008-10-29 14:49:05 -04001222 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001223 }
Josef Bacik25179202008-10-29 14:49:05 -04001224 return 1;
1225}
Josef Bacik25179202008-10-29 14:49:05 -04001226
Chris Mason2c64c532009-09-02 15:04:12 -04001227int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1228 struct extent_state **cached, gfp_t mask)
1229{
1230 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1231 mask);
1232}
1233
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001234int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001235{
Chris Mason2c64c532009-09-02 15:04:12 -04001236 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001237 GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001238}
Chris Masond1310b22008-01-24 16:13:08 -05001239
1240/*
Chris Masond1310b22008-01-24 16:13:08 -05001241 * helper function to set both pages and extents in the tree writeback
1242 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001243static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001244{
1245 unsigned long index = start >> PAGE_CACHE_SHIFT;
1246 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1247 struct page *page;
1248
1249 while (index <= end_index) {
1250 page = find_get_page(tree->mapping, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001251 BUG_ON(!page); /* Pages should be in the extent_io_tree */
Chris Masond1310b22008-01-24 16:13:08 -05001252 set_page_writeback(page);
1253 page_cache_release(page);
1254 index++;
1255 }
Chris Masond1310b22008-01-24 16:13:08 -05001256 return 0;
1257}
Chris Masond1310b22008-01-24 16:13:08 -05001258
Chris Masond352ac62008-09-29 15:18:18 -04001259/* find the first state struct with 'bits' set after 'start', and
1260 * return it. tree->lock must be held. NULL will returned if
1261 * nothing was found after 'start'
1262 */
Chris Masond7fc6402008-02-18 12:12:38 -05001263struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1264 u64 start, int bits)
1265{
1266 struct rb_node *node;
1267 struct extent_state *state;
1268
1269 /*
1270 * this search will find all the extents that end after
1271 * our range starts.
1272 */
1273 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001274 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001275 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001276
Chris Masond3977122009-01-05 21:25:51 -05001277 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001278 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001279 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001280 return state;
Chris Masond3977122009-01-05 21:25:51 -05001281
Chris Masond7fc6402008-02-18 12:12:38 -05001282 node = rb_next(node);
1283 if (!node)
1284 break;
1285 }
1286out:
1287 return NULL;
1288}
Chris Masond7fc6402008-02-18 12:12:38 -05001289
Chris Masond352ac62008-09-29 15:18:18 -04001290/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001291 * find the first offset in the io tree with 'bits' set. zero is
1292 * returned if we find something, and *start_ret and *end_ret are
1293 * set to reflect the state struct that was found.
1294 *
Wang Sheng-Hui477d7ea2012-04-06 14:35:47 +08001295 * If nothing was found, 1 is returned. If found something, return 0.
Xiao Guangrong69261c42011-07-14 03:19:45 +00001296 */
1297int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1298 u64 *start_ret, u64 *end_ret, int bits)
1299{
1300 struct extent_state *state;
1301 int ret = 1;
1302
1303 spin_lock(&tree->lock);
1304 state = find_first_extent_bit_state(tree, start, bits);
1305 if (state) {
1306 *start_ret = state->start;
1307 *end_ret = state->end;
1308 ret = 0;
1309 }
1310 spin_unlock(&tree->lock);
1311 return ret;
1312}
1313
1314/*
Chris Masond352ac62008-09-29 15:18:18 -04001315 * find a contiguous range of bytes in the file marked as delalloc, not
1316 * more than 'max_bytes'. start and end are used to return the range,
1317 *
1318 * 1 is returned if we find something, 0 if nothing was in the tree
1319 */
Chris Masonc8b97812008-10-29 14:49:59 -04001320static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001321 u64 *start, u64 *end, u64 max_bytes,
1322 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001323{
1324 struct rb_node *node;
1325 struct extent_state *state;
1326 u64 cur_start = *start;
1327 u64 found = 0;
1328 u64 total_bytes = 0;
1329
Chris Masoncad321a2008-12-17 14:51:42 -05001330 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001331
Chris Masond1310b22008-01-24 16:13:08 -05001332 /*
1333 * this search will find all the extents that end after
1334 * our range starts.
1335 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001336 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001337 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001338 if (!found)
1339 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001340 goto out;
1341 }
1342
Chris Masond3977122009-01-05 21:25:51 -05001343 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001344 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001345 if (found && (state->start != cur_start ||
1346 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001347 goto out;
1348 }
1349 if (!(state->state & EXTENT_DELALLOC)) {
1350 if (!found)
1351 *end = state->end;
1352 goto out;
1353 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001354 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001355 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001356 *cached_state = state;
1357 atomic_inc(&state->refs);
1358 }
Chris Masond1310b22008-01-24 16:13:08 -05001359 found++;
1360 *end = state->end;
1361 cur_start = state->end + 1;
1362 node = rb_next(node);
1363 if (!node)
1364 break;
1365 total_bytes += state->end - state->start + 1;
1366 if (total_bytes >= max_bytes)
1367 break;
1368 }
1369out:
Chris Masoncad321a2008-12-17 14:51:42 -05001370 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001371 return found;
1372}
1373
Jeff Mahoney143bede2012-03-01 14:56:26 +01001374static noinline void __unlock_for_delalloc(struct inode *inode,
1375 struct page *locked_page,
1376 u64 start, u64 end)
Chris Masonc8b97812008-10-29 14:49:59 -04001377{
1378 int ret;
1379 struct page *pages[16];
1380 unsigned long index = start >> PAGE_CACHE_SHIFT;
1381 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1382 unsigned long nr_pages = end_index - index + 1;
1383 int i;
1384
1385 if (index == locked_page->index && end_index == index)
Jeff Mahoney143bede2012-03-01 14:56:26 +01001386 return;
Chris Masonc8b97812008-10-29 14:49:59 -04001387
Chris Masond3977122009-01-05 21:25:51 -05001388 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001389 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001390 min_t(unsigned long, nr_pages,
1391 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001392 for (i = 0; i < ret; i++) {
1393 if (pages[i] != locked_page)
1394 unlock_page(pages[i]);
1395 page_cache_release(pages[i]);
1396 }
1397 nr_pages -= ret;
1398 index += ret;
1399 cond_resched();
1400 }
Chris Masonc8b97812008-10-29 14:49:59 -04001401}
1402
1403static noinline int lock_delalloc_pages(struct inode *inode,
1404 struct page *locked_page,
1405 u64 delalloc_start,
1406 u64 delalloc_end)
1407{
1408 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1409 unsigned long start_index = index;
1410 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1411 unsigned long pages_locked = 0;
1412 struct page *pages[16];
1413 unsigned long nrpages;
1414 int ret;
1415 int i;
1416
1417 /* the caller is responsible for locking the start index */
1418 if (index == locked_page->index && index == end_index)
1419 return 0;
1420
1421 /* skip the page at the start index */
1422 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001423 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001424 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001425 min_t(unsigned long,
1426 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001427 if (ret == 0) {
1428 ret = -EAGAIN;
1429 goto done;
1430 }
1431 /* now we have an array of pages, lock them all */
1432 for (i = 0; i < ret; i++) {
1433 /*
1434 * the caller is taking responsibility for
1435 * locked_page
1436 */
Chris Mason771ed682008-11-06 22:02:51 -05001437 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001438 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001439 if (!PageDirty(pages[i]) ||
1440 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001441 ret = -EAGAIN;
1442 unlock_page(pages[i]);
1443 page_cache_release(pages[i]);
1444 goto done;
1445 }
1446 }
Chris Masonc8b97812008-10-29 14:49:59 -04001447 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001448 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001449 }
Chris Masonc8b97812008-10-29 14:49:59 -04001450 nrpages -= ret;
1451 index += ret;
1452 cond_resched();
1453 }
1454 ret = 0;
1455done:
1456 if (ret && pages_locked) {
1457 __unlock_for_delalloc(inode, locked_page,
1458 delalloc_start,
1459 ((u64)(start_index + pages_locked - 1)) <<
1460 PAGE_CACHE_SHIFT);
1461 }
1462 return ret;
1463}
1464
1465/*
1466 * find a contiguous range of bytes in the file marked as delalloc, not
1467 * more than 'max_bytes'. start and end are used to return the range,
1468 *
1469 * 1 is returned if we find something, 0 if nothing was in the tree
1470 */
1471static noinline u64 find_lock_delalloc_range(struct inode *inode,
1472 struct extent_io_tree *tree,
1473 struct page *locked_page,
1474 u64 *start, u64 *end,
1475 u64 max_bytes)
1476{
1477 u64 delalloc_start;
1478 u64 delalloc_end;
1479 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001480 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001481 int ret;
1482 int loops = 0;
1483
1484again:
1485 /* step one, find a bunch of delalloc bytes starting at start */
1486 delalloc_start = *start;
1487 delalloc_end = 0;
1488 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001489 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001490 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001491 *start = delalloc_start;
1492 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001493 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001494 return found;
1495 }
1496
1497 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001498 * start comes from the offset of locked_page. We have to lock
1499 * pages in order, so we can't process delalloc bytes before
1500 * locked_page
1501 */
Chris Masond3977122009-01-05 21:25:51 -05001502 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001503 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001504
1505 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001506 * make sure to limit the number of pages we try to lock down
1507 * if we're looping.
1508 */
Chris Masond3977122009-01-05 21:25:51 -05001509 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001510 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001511
Chris Masonc8b97812008-10-29 14:49:59 -04001512 /* step two, lock all the pages after the page that has start */
1513 ret = lock_delalloc_pages(inode, locked_page,
1514 delalloc_start, delalloc_end);
1515 if (ret == -EAGAIN) {
1516 /* some of the pages are gone, lets avoid looping by
1517 * shortening the size of the delalloc range we're searching
1518 */
Chris Mason9655d292009-09-02 15:22:30 -04001519 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001520 if (!loops) {
1521 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1522 max_bytes = PAGE_CACHE_SIZE - offset;
1523 loops = 1;
1524 goto again;
1525 } else {
1526 found = 0;
1527 goto out_failed;
1528 }
1529 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001530 BUG_ON(ret); /* Only valid values are 0 and -EAGAIN */
Chris Masonc8b97812008-10-29 14:49:59 -04001531
1532 /* step three, lock the state bits for the whole range */
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001533 lock_extent_bits(tree, delalloc_start, delalloc_end, 0, &cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001534
1535 /* then test to make sure it is all still delalloc */
1536 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001537 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001538 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001539 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1540 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001541 __unlock_for_delalloc(inode, locked_page,
1542 delalloc_start, delalloc_end);
1543 cond_resched();
1544 goto again;
1545 }
Chris Mason9655d292009-09-02 15:22:30 -04001546 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001547 *start = delalloc_start;
1548 *end = delalloc_end;
1549out_failed:
1550 return found;
1551}
1552
1553int extent_clear_unlock_delalloc(struct inode *inode,
1554 struct extent_io_tree *tree,
1555 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001556 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001557{
1558 int ret;
1559 struct page *pages[16];
1560 unsigned long index = start >> PAGE_CACHE_SHIFT;
1561 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1562 unsigned long nr_pages = end_index - index + 1;
1563 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001564 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001565
Chris Masona791e352009-10-08 11:27:10 -04001566 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001567 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001568 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001569 clear_bits |= EXTENT_DIRTY;
1570
Chris Masona791e352009-10-08 11:27:10 -04001571 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001572 clear_bits |= EXTENT_DELALLOC;
1573
Chris Mason2c64c532009-09-02 15:04:12 -04001574 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001575 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1576 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1577 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001578 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001579
Chris Masond3977122009-01-05 21:25:51 -05001580 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001581 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001582 min_t(unsigned long,
1583 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001584 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001585
Chris Masona791e352009-10-08 11:27:10 -04001586 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001587 SetPagePrivate2(pages[i]);
1588
Chris Masonc8b97812008-10-29 14:49:59 -04001589 if (pages[i] == locked_page) {
1590 page_cache_release(pages[i]);
1591 continue;
1592 }
Chris Masona791e352009-10-08 11:27:10 -04001593 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001594 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001595 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001596 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001597 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001598 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001599 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001600 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001601 page_cache_release(pages[i]);
1602 }
1603 nr_pages -= ret;
1604 index += ret;
1605 cond_resched();
1606 }
1607 return 0;
1608}
Chris Masonc8b97812008-10-29 14:49:59 -04001609
Chris Masond352ac62008-09-29 15:18:18 -04001610/*
1611 * count the number of bytes in the tree that have a given bit(s)
1612 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1613 * cached. The total number found is returned.
1614 */
Chris Masond1310b22008-01-24 16:13:08 -05001615u64 count_range_bits(struct extent_io_tree *tree,
1616 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001617 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001618{
1619 struct rb_node *node;
1620 struct extent_state *state;
1621 u64 cur_start = *start;
1622 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001623 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001624 int found = 0;
1625
1626 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001627 WARN_ON(1);
1628 return 0;
1629 }
1630
Chris Masoncad321a2008-12-17 14:51:42 -05001631 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001632 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1633 total_bytes = tree->dirty_bytes;
1634 goto out;
1635 }
1636 /*
1637 * this search will find all the extents that end after
1638 * our range starts.
1639 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001640 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001641 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001642 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001643
Chris Masond3977122009-01-05 21:25:51 -05001644 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001645 state = rb_entry(node, struct extent_state, rb_node);
1646 if (state->start > search_end)
1647 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001648 if (contig && found && state->start > last + 1)
1649 break;
1650 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001651 total_bytes += min(search_end, state->end) + 1 -
1652 max(cur_start, state->start);
1653 if (total_bytes >= max_bytes)
1654 break;
1655 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001656 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001657 found = 1;
1658 }
Chris Masonec29ed52011-02-23 16:23:20 -05001659 last = state->end;
1660 } else if (contig && found) {
1661 break;
Chris Masond1310b22008-01-24 16:13:08 -05001662 }
1663 node = rb_next(node);
1664 if (!node)
1665 break;
1666 }
1667out:
Chris Masoncad321a2008-12-17 14:51:42 -05001668 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001669 return total_bytes;
1670}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001671
Chris Masond352ac62008-09-29 15:18:18 -04001672/*
1673 * set the private field for a given byte offset in the tree. If there isn't
1674 * an extent_state there already, this does nothing.
1675 */
Chris Masond1310b22008-01-24 16:13:08 -05001676int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1677{
1678 struct rb_node *node;
1679 struct extent_state *state;
1680 int ret = 0;
1681
Chris Masoncad321a2008-12-17 14:51:42 -05001682 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001683 /*
1684 * this search will find all the extents that end after
1685 * our range starts.
1686 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001687 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001688 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001689 ret = -ENOENT;
1690 goto out;
1691 }
1692 state = rb_entry(node, struct extent_state, rb_node);
1693 if (state->start != start) {
1694 ret = -ENOENT;
1695 goto out;
1696 }
1697 state->private = private;
1698out:
Chris Masoncad321a2008-12-17 14:51:42 -05001699 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001700 return ret;
1701}
1702
1703int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1704{
1705 struct rb_node *node;
1706 struct extent_state *state;
1707 int ret = 0;
1708
Chris Masoncad321a2008-12-17 14:51:42 -05001709 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001710 /*
1711 * this search will find all the extents that end after
1712 * our range starts.
1713 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001714 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001715 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001716 ret = -ENOENT;
1717 goto out;
1718 }
1719 state = rb_entry(node, struct extent_state, rb_node);
1720 if (state->start != start) {
1721 ret = -ENOENT;
1722 goto out;
1723 }
1724 *private = state->private;
1725out:
Chris Masoncad321a2008-12-17 14:51:42 -05001726 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001727 return ret;
1728}
1729
1730/*
1731 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001732 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001733 * has the bits set. Otherwise, 1 is returned if any bit in the
1734 * range is found set.
1735 */
1736int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001737 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001738{
1739 struct extent_state *state = NULL;
1740 struct rb_node *node;
1741 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001742
Chris Masoncad321a2008-12-17 14:51:42 -05001743 spin_lock(&tree->lock);
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001744 if (cached && cached->tree && cached->start <= start &&
1745 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001746 node = &cached->rb_node;
1747 else
1748 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001749 while (node && start <= end) {
1750 state = rb_entry(node, struct extent_state, rb_node);
1751
1752 if (filled && state->start > start) {
1753 bitset = 0;
1754 break;
1755 }
1756
1757 if (state->start > end)
1758 break;
1759
1760 if (state->state & bits) {
1761 bitset = 1;
1762 if (!filled)
1763 break;
1764 } else if (filled) {
1765 bitset = 0;
1766 break;
1767 }
Chris Mason46562cec2009-09-23 20:23:16 -04001768
1769 if (state->end == (u64)-1)
1770 break;
1771
Chris Masond1310b22008-01-24 16:13:08 -05001772 start = state->end + 1;
1773 if (start > end)
1774 break;
1775 node = rb_next(node);
1776 if (!node) {
1777 if (filled)
1778 bitset = 0;
1779 break;
1780 }
1781 }
Chris Masoncad321a2008-12-17 14:51:42 -05001782 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001783 return bitset;
1784}
Chris Masond1310b22008-01-24 16:13:08 -05001785
1786/*
1787 * helper function to set a given page up to date if all the
1788 * extents in the tree for that page are up to date
1789 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001790static void check_page_uptodate(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001791{
1792 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1793 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001794 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001795 SetPageUptodate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001796}
1797
1798/*
1799 * helper function to unlock a page if all the extents in the tree
1800 * for that page are unlocked
1801 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001802static void check_page_locked(struct extent_io_tree *tree, struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001803{
1804 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1805 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001806 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001807 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05001808}
1809
1810/*
1811 * helper function to end page writeback if all the extents
1812 * in the tree for that page are done with writeback
1813 */
Jeff Mahoney143bede2012-03-01 14:56:26 +01001814static void check_page_writeback(struct extent_io_tree *tree,
1815 struct page *page)
Chris Masond1310b22008-01-24 16:13:08 -05001816{
Chris Mason1edbb732009-09-02 13:24:36 -04001817 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001818}
1819
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001820/*
1821 * When IO fails, either with EIO or csum verification fails, we
1822 * try other mirrors that might have a good copy of the data. This
1823 * io_failure_record is used to record state as we go through all the
1824 * mirrors. If another mirror has good data, the page is set up to date
1825 * and things continue. If a good mirror can't be found, the original
1826 * bio end_io callback is called to indicate things have failed.
1827 */
1828struct io_failure_record {
1829 struct page *page;
1830 u64 start;
1831 u64 len;
1832 u64 logical;
1833 unsigned long bio_flags;
1834 int this_mirror;
1835 int failed_mirror;
1836 int in_validation;
1837};
1838
1839static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
1840 int did_repair)
1841{
1842 int ret;
1843 int err = 0;
1844 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1845
1846 set_state_private(failure_tree, rec->start, 0);
1847 ret = clear_extent_bits(failure_tree, rec->start,
1848 rec->start + rec->len - 1,
1849 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1850 if (ret)
1851 err = ret;
1852
1853 if (did_repair) {
1854 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1855 rec->start + rec->len - 1,
1856 EXTENT_DAMAGED, GFP_NOFS);
1857 if (ret && !err)
1858 err = ret;
1859 }
1860
1861 kfree(rec);
1862 return err;
1863}
1864
1865static void repair_io_failure_callback(struct bio *bio, int err)
1866{
1867 complete(bio->bi_private);
1868}
1869
1870/*
1871 * this bypasses the standard btrfs submit functions deliberately, as
1872 * the standard behavior is to write all copies in a raid setup. here we only
1873 * want to write the one bad copy. so we do the mapping for ourselves and issue
1874 * submit_bio directly.
1875 * to avoid any synchonization issues, wait for the data after writing, which
1876 * actually prevents the read that triggered the error from finishing.
1877 * currently, there can be no more than two copies of every data bit. thus,
1878 * exactly one rewrite is required.
1879 */
1880int repair_io_failure(struct btrfs_mapping_tree *map_tree, u64 start,
1881 u64 length, u64 logical, struct page *page,
1882 int mirror_num)
1883{
1884 struct bio *bio;
1885 struct btrfs_device *dev;
1886 DECLARE_COMPLETION_ONSTACK(compl);
1887 u64 map_length = 0;
1888 u64 sector;
1889 struct btrfs_bio *bbio = NULL;
1890 int ret;
1891
1892 BUG_ON(!mirror_num);
1893
1894 bio = bio_alloc(GFP_NOFS, 1);
1895 if (!bio)
1896 return -EIO;
1897 bio->bi_private = &compl;
1898 bio->bi_end_io = repair_io_failure_callback;
1899 bio->bi_size = 0;
1900 map_length = length;
1901
1902 ret = btrfs_map_block(map_tree, WRITE, logical,
1903 &map_length, &bbio, mirror_num);
1904 if (ret) {
1905 bio_put(bio);
1906 return -EIO;
1907 }
1908 BUG_ON(mirror_num != bbio->mirror_num);
1909 sector = bbio->stripes[mirror_num-1].physical >> 9;
1910 bio->bi_sector = sector;
1911 dev = bbio->stripes[mirror_num-1].dev;
1912 kfree(bbio);
1913 if (!dev || !dev->bdev || !dev->writeable) {
1914 bio_put(bio);
1915 return -EIO;
1916 }
1917 bio->bi_bdev = dev->bdev;
1918 bio_add_page(bio, page, length, start-page_offset(page));
Stefan Behrens21adbd52011-11-09 13:44:05 +01001919 btrfsic_submit_bio(WRITE_SYNC, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001920 wait_for_completion(&compl);
1921
1922 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
1923 /* try to remap that extent elsewhere? */
1924 bio_put(bio);
1925 return -EIO;
1926 }
1927
1928 printk(KERN_INFO "btrfs read error corrected: ino %lu off %llu (dev %s "
1929 "sector %llu)\n", page->mapping->host->i_ino, start,
1930 dev->name, sector);
1931
1932 bio_put(bio);
1933 return 0;
1934}
1935
Josef Bacikea466792012-03-26 21:57:36 -04001936int repair_eb_io_failure(struct btrfs_root *root, struct extent_buffer *eb,
1937 int mirror_num)
1938{
1939 struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
1940 u64 start = eb->start;
1941 unsigned long i, num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond95603b2012-04-12 15:55:15 -04001942 int ret = 0;
Josef Bacikea466792012-03-26 21:57:36 -04001943
1944 for (i = 0; i < num_pages; i++) {
1945 struct page *p = extent_buffer_page(eb, i);
1946 ret = repair_io_failure(map_tree, start, PAGE_CACHE_SIZE,
1947 start, p, mirror_num);
1948 if (ret)
1949 break;
1950 start += PAGE_CACHE_SIZE;
1951 }
1952
1953 return ret;
1954}
1955
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001956/*
1957 * each time an IO finishes, we do a fast check in the IO failure tree
1958 * to see if we need to process or clean up an io_failure_record
1959 */
1960static int clean_io_failure(u64 start, struct page *page)
1961{
1962 u64 private;
1963 u64 private_failure;
1964 struct io_failure_record *failrec;
1965 struct btrfs_mapping_tree *map_tree;
1966 struct extent_state *state;
1967 int num_copies;
1968 int did_repair = 0;
1969 int ret;
1970 struct inode *inode = page->mapping->host;
1971
1972 private = 0;
1973 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1974 (u64)-1, 1, EXTENT_DIRTY, 0);
1975 if (!ret)
1976 return 0;
1977
1978 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
1979 &private_failure);
1980 if (ret)
1981 return 0;
1982
1983 failrec = (struct io_failure_record *)(unsigned long) private_failure;
1984 BUG_ON(!failrec->this_mirror);
1985
1986 if (failrec->in_validation) {
1987 /* there was no real error, just free the record */
1988 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
1989 failrec->start);
1990 did_repair = 1;
1991 goto out;
1992 }
1993
1994 spin_lock(&BTRFS_I(inode)->io_tree.lock);
1995 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1996 failrec->start,
1997 EXTENT_LOCKED);
1998 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
1999
2000 if (state && state->start == failrec->start) {
2001 map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree;
2002 num_copies = btrfs_num_copies(map_tree, failrec->logical,
2003 failrec->len);
2004 if (num_copies > 1) {
2005 ret = repair_io_failure(map_tree, start, failrec->len,
2006 failrec->logical, page,
2007 failrec->failed_mirror);
2008 did_repair = !ret;
2009 }
2010 }
2011
2012out:
2013 if (!ret)
2014 ret = free_io_failure(inode, failrec, did_repair);
2015
2016 return ret;
2017}
2018
2019/*
2020 * this is a generic handler for readpage errors (default
2021 * readpage_io_failed_hook). if other copies exist, read those and write back
2022 * good data to the failed position. does not investigate in remapping the
2023 * failed extent elsewhere, hoping the device will be smart enough to do this as
2024 * needed
2025 */
2026
2027static int bio_readpage_error(struct bio *failed_bio, struct page *page,
2028 u64 start, u64 end, int failed_mirror,
2029 struct extent_state *state)
2030{
2031 struct io_failure_record *failrec = NULL;
2032 u64 private;
2033 struct extent_map *em;
2034 struct inode *inode = page->mapping->host;
2035 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
2036 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
2037 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2038 struct bio *bio;
2039 int num_copies;
2040 int ret;
2041 int read_mode;
2042 u64 logical;
2043
2044 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2045
2046 ret = get_state_private(failure_tree, start, &private);
2047 if (ret) {
2048 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2049 if (!failrec)
2050 return -ENOMEM;
2051 failrec->start = start;
2052 failrec->len = end - start + 1;
2053 failrec->this_mirror = 0;
2054 failrec->bio_flags = 0;
2055 failrec->in_validation = 0;
2056
2057 read_lock(&em_tree->lock);
2058 em = lookup_extent_mapping(em_tree, start, failrec->len);
2059 if (!em) {
2060 read_unlock(&em_tree->lock);
2061 kfree(failrec);
2062 return -EIO;
2063 }
2064
2065 if (em->start > start || em->start + em->len < start) {
2066 free_extent_map(em);
2067 em = NULL;
2068 }
2069 read_unlock(&em_tree->lock);
2070
2071 if (!em || IS_ERR(em)) {
2072 kfree(failrec);
2073 return -EIO;
2074 }
2075 logical = start - em->start;
2076 logical = em->block_start + logical;
2077 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2078 logical = em->block_start;
2079 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2080 extent_set_compress_type(&failrec->bio_flags,
2081 em->compress_type);
2082 }
2083 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2084 "len=%llu\n", logical, start, failrec->len);
2085 failrec->logical = logical;
2086 free_extent_map(em);
2087
2088 /* set the bits in the private failure tree */
2089 ret = set_extent_bits(failure_tree, start, end,
2090 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2091 if (ret >= 0)
2092 ret = set_state_private(failure_tree, start,
2093 (u64)(unsigned long)failrec);
2094 /* set the bits in the inode's tree */
2095 if (ret >= 0)
2096 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2097 GFP_NOFS);
2098 if (ret < 0) {
2099 kfree(failrec);
2100 return ret;
2101 }
2102 } else {
2103 failrec = (struct io_failure_record *)(unsigned long)private;
2104 pr_debug("bio_readpage_error: (found) logical=%llu, "
2105 "start=%llu, len=%llu, validation=%d\n",
2106 failrec->logical, failrec->start, failrec->len,
2107 failrec->in_validation);
2108 /*
2109 * when data can be on disk more than twice, add to failrec here
2110 * (e.g. with a list for failed_mirror) to make
2111 * clean_io_failure() clean all those errors at once.
2112 */
2113 }
2114 num_copies = btrfs_num_copies(
2115 &BTRFS_I(inode)->root->fs_info->mapping_tree,
2116 failrec->logical, failrec->len);
2117 if (num_copies == 1) {
2118 /*
2119 * we only have a single copy of the data, so don't bother with
2120 * all the retry and error correction code that follows. no
2121 * matter what the error is, it is very likely to persist.
2122 */
2123 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2124 "state=%p, num_copies=%d, next_mirror %d, "
2125 "failed_mirror %d\n", state, num_copies,
2126 failrec->this_mirror, failed_mirror);
2127 free_io_failure(inode, failrec, 0);
2128 return -EIO;
2129 }
2130
2131 if (!state) {
2132 spin_lock(&tree->lock);
2133 state = find_first_extent_bit_state(tree, failrec->start,
2134 EXTENT_LOCKED);
2135 if (state && state->start != failrec->start)
2136 state = NULL;
2137 spin_unlock(&tree->lock);
2138 }
2139
2140 /*
2141 * there are two premises:
2142 * a) deliver good data to the caller
2143 * b) correct the bad sectors on disk
2144 */
2145 if (failed_bio->bi_vcnt > 1) {
2146 /*
2147 * to fulfill b), we need to know the exact failing sectors, as
2148 * we don't want to rewrite any more than the failed ones. thus,
2149 * we need separate read requests for the failed bio
2150 *
2151 * if the following BUG_ON triggers, our validation request got
2152 * merged. we need separate requests for our algorithm to work.
2153 */
2154 BUG_ON(failrec->in_validation);
2155 failrec->in_validation = 1;
2156 failrec->this_mirror = failed_mirror;
2157 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2158 } else {
2159 /*
2160 * we're ready to fulfill a) and b) alongside. get a good copy
2161 * of the failed sector and if we succeed, we have setup
2162 * everything for repair_io_failure to do the rest for us.
2163 */
2164 if (failrec->in_validation) {
2165 BUG_ON(failrec->this_mirror != failed_mirror);
2166 failrec->in_validation = 0;
2167 failrec->this_mirror = 0;
2168 }
2169 failrec->failed_mirror = failed_mirror;
2170 failrec->this_mirror++;
2171 if (failrec->this_mirror == failed_mirror)
2172 failrec->this_mirror++;
2173 read_mode = READ_SYNC;
2174 }
2175
2176 if (!state || failrec->this_mirror > num_copies) {
2177 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2178 "next_mirror %d, failed_mirror %d\n", state,
2179 num_copies, failrec->this_mirror, failed_mirror);
2180 free_io_failure(inode, failrec, 0);
2181 return -EIO;
2182 }
2183
2184 bio = bio_alloc(GFP_NOFS, 1);
Tsutomu Itohe627ee72012-04-12 16:03:56 -04002185 if (!bio) {
2186 free_io_failure(inode, failrec, 0);
2187 return -EIO;
2188 }
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002189 bio->bi_private = state;
2190 bio->bi_end_io = failed_bio->bi_end_io;
2191 bio->bi_sector = failrec->logical >> 9;
2192 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2193 bio->bi_size = 0;
2194
2195 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2196
2197 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2198 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2199 failrec->this_mirror, num_copies, failrec->in_validation);
2200
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002201 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2202 failrec->this_mirror,
2203 failrec->bio_flags, 0);
2204 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002205}
2206
Chris Masond1310b22008-01-24 16:13:08 -05002207/* lots and lots of room for performance fixes in the end_bio funcs */
2208
Jeff Mahoney87826df2012-02-15 16:23:57 +01002209int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2210{
2211 int uptodate = (err == 0);
2212 struct extent_io_tree *tree;
2213 int ret;
2214
2215 tree = &BTRFS_I(page->mapping->host)->io_tree;
2216
2217 if (tree->ops && tree->ops->writepage_end_io_hook) {
2218 ret = tree->ops->writepage_end_io_hook(page, start,
2219 end, NULL, uptodate);
2220 if (ret)
2221 uptodate = 0;
2222 }
2223
2224 if (!uptodate && tree->ops &&
2225 tree->ops->writepage_io_failed_hook) {
2226 ret = tree->ops->writepage_io_failed_hook(NULL, page,
2227 start, end, NULL);
2228 /* Writeback already completed */
2229 if (ret == 0)
2230 return 1;
2231 }
2232
2233 if (!uptodate) {
2234 clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
2235 ClearPageUptodate(page);
2236 SetPageError(page);
2237 }
2238 return 0;
2239}
2240
Chris Masond1310b22008-01-24 16:13:08 -05002241/*
2242 * after a writepage IO is done, we need to:
2243 * clear the uptodate bits on error
2244 * clear the writeback bits in the extent tree for this IO
2245 * end_page_writeback if the page has no more pending IO
2246 *
2247 * Scheduling is not allowed, so the extent state tree is expected
2248 * to have one and only one object corresponding to this IO.
2249 */
Chris Masond1310b22008-01-24 16:13:08 -05002250static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002251{
Chris Masond1310b22008-01-24 16:13:08 -05002252 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04002253 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002254 u64 start;
2255 u64 end;
2256 int whole_page;
2257
Chris Masond1310b22008-01-24 16:13:08 -05002258 do {
2259 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04002260 tree = &BTRFS_I(page->mapping->host)->io_tree;
2261
Chris Masond1310b22008-01-24 16:13:08 -05002262 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2263 bvec->bv_offset;
2264 end = start + bvec->bv_len - 1;
2265
2266 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2267 whole_page = 1;
2268 else
2269 whole_page = 0;
2270
2271 if (--bvec >= bio->bi_io_vec)
2272 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04002273
Jeff Mahoney87826df2012-02-15 16:23:57 +01002274 if (end_extent_writepage(page, err, start, end))
2275 continue;
Chris Mason70dec802008-01-29 09:59:12 -05002276
Chris Masond1310b22008-01-24 16:13:08 -05002277 if (whole_page)
2278 end_page_writeback(page);
2279 else
2280 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002281 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04002282
Chris Masond1310b22008-01-24 16:13:08 -05002283 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002284}
2285
2286/*
2287 * after a readpage IO is done, we need to:
2288 * clear the uptodate bits on error
2289 * set the uptodate bits if things worked
2290 * set the page up to date if all extents in the tree are uptodate
2291 * clear the lock bit in the extent tree
2292 * unlock the page if there are no other extents locked for it
2293 *
2294 * Scheduling is not allowed, so the extent state tree is expected
2295 * to have one and only one object corresponding to this IO.
2296 */
Chris Masond1310b22008-01-24 16:13:08 -05002297static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002298{
2299 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00002300 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2301 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04002302 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002303 u64 start;
2304 u64 end;
2305 int whole_page;
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002306 int mirror;
Chris Masond1310b22008-01-24 16:13:08 -05002307 int ret;
2308
Chris Masond20f7042008-12-08 16:58:54 -05002309 if (err)
2310 uptodate = 0;
2311
Chris Masond1310b22008-01-24 16:13:08 -05002312 do {
2313 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00002314 struct extent_state *cached = NULL;
2315 struct extent_state *state;
2316
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002317 pr_debug("end_bio_extent_readpage: bi_vcnt=%d, idx=%d, err=%d, "
2318 "mirror=%ld\n", bio->bi_vcnt, bio->bi_idx, err,
2319 (long int)bio->bi_bdev);
David Woodhouse902b22f2008-08-20 08:51:49 -04002320 tree = &BTRFS_I(page->mapping->host)->io_tree;
2321
Chris Masond1310b22008-01-24 16:13:08 -05002322 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2323 bvec->bv_offset;
2324 end = start + bvec->bv_len - 1;
2325
2326 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2327 whole_page = 1;
2328 else
2329 whole_page = 0;
2330
Chris Mason4125bf72010-02-03 18:18:45 +00002331 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05002332 prefetchw(&bvec->bv_page->flags);
2333
Arne Jansen507903b2011-04-06 10:02:20 +00002334 spin_lock(&tree->lock);
Chris Mason0d399202011-04-16 06:55:39 -04002335 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
Chris Mason109b36a2011-04-12 13:57:39 -04002336 if (state && state->start == start) {
Arne Jansen507903b2011-04-06 10:02:20 +00002337 /*
2338 * take a reference on the state, unlock will drop
2339 * the ref
2340 */
2341 cache_state(state, &cached);
2342 }
2343 spin_unlock(&tree->lock);
2344
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002345 mirror = (int)(unsigned long)bio->bi_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002346 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05002347 ret = tree->ops->readpage_end_io_hook(page, start, end,
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002348 state, mirror);
Chris Masond1310b22008-01-24 16:13:08 -05002349 if (ret)
2350 uptodate = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002351 else
2352 clean_io_failure(start, page);
Chris Masond1310b22008-01-24 16:13:08 -05002353 }
Josef Bacikea466792012-03-26 21:57:36 -04002354
Josef Bacikea466792012-03-26 21:57:36 -04002355 if (!uptodate && tree->ops && tree->ops->readpage_io_failed_hook) {
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002356 ret = tree->ops->readpage_io_failed_hook(page, mirror);
Josef Bacikea466792012-03-26 21:57:36 -04002357 if (!ret && !err &&
2358 test_bit(BIO_UPTODATE, &bio->bi_flags))
2359 uptodate = 1;
2360 } else if (!uptodate) {
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002361 /*
2362 * The generic bio_readpage_error handles errors the
2363 * following way: If possible, new read requests are
2364 * created and submitted and will end up in
2365 * end_bio_extent_readpage as well (if we're lucky, not
2366 * in the !uptodate case). In that case it returns 0 and
2367 * we just go on with the next page in our bio. If it
2368 * can't handle the error it will return -EIO and we
2369 * remain responsible for that page.
2370 */
Josef Bacik5cf1ab52012-04-16 09:42:26 -04002371 ret = bio_readpage_error(bio, page, start, end, mirror, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04002372 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04002373 uptodate =
2374 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05002375 if (err)
2376 uptodate = 0;
Arne Jansen507903b2011-04-06 10:02:20 +00002377 uncache_state(&cached);
Chris Mason7e383262008-04-09 16:28:12 -04002378 continue;
2379 }
2380 }
Chris Mason70dec802008-01-29 09:59:12 -05002381
Josef Bacik0b32f4b2012-03-13 09:38:00 -04002382 if (uptodate && tree->track_uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00002383 set_extent_uptodate(tree, start, end, &cached,
David Woodhouse902b22f2008-08-20 08:51:49 -04002384 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05002385 }
Arne Jansen507903b2011-04-06 10:02:20 +00002386 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05002387
Chris Mason70dec802008-01-29 09:59:12 -05002388 if (whole_page) {
2389 if (uptodate) {
2390 SetPageUptodate(page);
2391 } else {
2392 ClearPageUptodate(page);
2393 SetPageError(page);
2394 }
Chris Masond1310b22008-01-24 16:13:08 -05002395 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05002396 } else {
2397 if (uptodate) {
2398 check_page_uptodate(tree, page);
2399 } else {
2400 ClearPageUptodate(page);
2401 SetPageError(page);
2402 }
Chris Masond1310b22008-01-24 16:13:08 -05002403 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05002404 }
Chris Mason4125bf72010-02-03 18:18:45 +00002405 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05002406
2407 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002408}
2409
Miao Xie88f794e2010-11-22 03:02:55 +00002410struct bio *
2411btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2412 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002413{
2414 struct bio *bio;
2415
2416 bio = bio_alloc(gfp_flags, nr_vecs);
2417
2418 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2419 while (!bio && (nr_vecs /= 2))
2420 bio = bio_alloc(gfp_flags, nr_vecs);
2421 }
2422
2423 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04002424 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002425 bio->bi_bdev = bdev;
2426 bio->bi_sector = first_sector;
2427 }
2428 return bio;
2429}
2430
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002431/*
2432 * Since writes are async, they will only return -ENOMEM.
2433 * Reads can return the full range of I/O error conditions.
2434 */
Jeff Mahoney355808c2011-10-03 23:23:14 -04002435static int __must_check submit_one_bio(int rw, struct bio *bio,
2436 int mirror_num, unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002437{
Chris Masond1310b22008-01-24 16:13:08 -05002438 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002439 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2440 struct page *page = bvec->bv_page;
2441 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002442 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002443
2444 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002445
David Woodhouse902b22f2008-08-20 08:51:49 -04002446 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002447
2448 bio_get(bio);
2449
Chris Mason065631f2008-02-20 12:07:25 -05002450 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00002451 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002452 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002453 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01002454 btrfsic_submit_bio(rw, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002455
Chris Masond1310b22008-01-24 16:13:08 -05002456 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2457 ret = -EOPNOTSUPP;
2458 bio_put(bio);
2459 return ret;
2460}
2461
Jeff Mahoney3444a972011-10-03 23:23:13 -04002462static int merge_bio(struct extent_io_tree *tree, struct page *page,
2463 unsigned long offset, size_t size, struct bio *bio,
2464 unsigned long bio_flags)
2465{
2466 int ret = 0;
2467 if (tree->ops && tree->ops->merge_bio_hook)
2468 ret = tree->ops->merge_bio_hook(page, offset, size, bio,
2469 bio_flags);
2470 BUG_ON(ret < 0);
2471 return ret;
2472
2473}
2474
Chris Masond1310b22008-01-24 16:13:08 -05002475static int submit_extent_page(int rw, struct extent_io_tree *tree,
2476 struct page *page, sector_t sector,
2477 size_t size, unsigned long offset,
2478 struct block_device *bdev,
2479 struct bio **bio_ret,
2480 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04002481 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002482 int mirror_num,
2483 unsigned long prev_bio_flags,
2484 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002485{
2486 int ret = 0;
2487 struct bio *bio;
2488 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04002489 int contig = 0;
2490 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2491 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05002492 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002493
2494 if (bio_ret && *bio_ret) {
2495 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002496 if (old_compressed)
2497 contig = bio->bi_sector == sector;
2498 else
2499 contig = bio->bi_sector + (bio->bi_size >> 9) ==
2500 sector;
2501
2502 if (prev_bio_flags != bio_flags || !contig ||
Jeff Mahoney3444a972011-10-03 23:23:13 -04002503 merge_bio(tree, page, offset, page_size, bio, bio_flags) ||
Chris Masonc8b97812008-10-29 14:49:59 -04002504 bio_add_page(bio, page, page_size, offset) < page_size) {
2505 ret = submit_one_bio(rw, bio, mirror_num,
2506 prev_bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002507 if (ret < 0)
2508 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05002509 bio = NULL;
2510 } else {
2511 return 0;
2512 }
2513 }
Chris Masonc8b97812008-10-29 14:49:59 -04002514 if (this_compressed)
2515 nr = BIO_MAX_PAGES;
2516 else
2517 nr = bio_get_nr_vecs(bdev);
2518
Miao Xie88f794e2010-11-22 03:02:55 +00002519 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002520 if (!bio)
2521 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002522
Chris Masonc8b97812008-10-29 14:49:59 -04002523 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002524 bio->bi_end_io = end_io_func;
2525 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05002526
Chris Masond3977122009-01-05 21:25:51 -05002527 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002528 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002529 else
Chris Masonc8b97812008-10-29 14:49:59 -04002530 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002531
2532 return ret;
2533}
2534
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002535void attach_extent_buffer_page(struct extent_buffer *eb, struct page *page)
2536{
2537 if (!PagePrivate(page)) {
2538 SetPagePrivate(page);
2539 page_cache_get(page);
2540 set_page_private(page, (unsigned long)eb);
2541 } else {
2542 WARN_ON(page->private != (unsigned long)eb);
2543 }
2544}
2545
Chris Masond1310b22008-01-24 16:13:08 -05002546void set_page_extent_mapped(struct page *page)
2547{
2548 if (!PagePrivate(page)) {
2549 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002550 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002551 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002552 }
2553}
2554
Chris Masond1310b22008-01-24 16:13:08 -05002555/*
2556 * basic readpage implementation. Locked extent state structs are inserted
2557 * into the tree that are removed when the IO is done (by the end_io
2558 * handlers)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002559 * XXX JDM: This needs looking at to ensure proper page locking
Chris Masond1310b22008-01-24 16:13:08 -05002560 */
2561static int __extent_read_full_page(struct extent_io_tree *tree,
2562 struct page *page,
2563 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002564 struct bio **bio, int mirror_num,
2565 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002566{
2567 struct inode *inode = page->mapping->host;
2568 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2569 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2570 u64 end;
2571 u64 cur = start;
2572 u64 extent_offset;
2573 u64 last_byte = i_size_read(inode);
2574 u64 block_start;
2575 u64 cur_end;
2576 sector_t sector;
2577 struct extent_map *em;
2578 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002579 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05002580 int ret;
2581 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002582 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002583 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002584 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002585 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002586 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002587
2588 set_page_extent_mapped(page);
2589
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002590 if (!PageUptodate(page)) {
2591 if (cleancache_get_page(page) == 0) {
2592 BUG_ON(blocksize != PAGE_SIZE);
2593 goto out;
2594 }
2595 }
2596
Chris Masond1310b22008-01-24 16:13:08 -05002597 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002598 while (1) {
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002599 lock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002600 ordered = btrfs_lookup_ordered_extent(inode, start);
2601 if (!ordered)
2602 break;
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002603 unlock_extent(tree, start, end);
Josef Bacik11c65dc2010-05-23 11:07:21 -04002604 btrfs_start_ordered_extent(inode, ordered, 1);
2605 btrfs_put_ordered_extent(ordered);
2606 }
Chris Masond1310b22008-01-24 16:13:08 -05002607
Chris Masonc8b97812008-10-29 14:49:59 -04002608 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2609 char *userpage;
2610 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2611
2612 if (zero_offset) {
2613 iosize = PAGE_CACHE_SIZE - zero_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002614 userpage = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -04002615 memset(userpage + zero_offset, 0, iosize);
2616 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002617 kunmap_atomic(userpage);
Chris Masonc8b97812008-10-29 14:49:59 -04002618 }
2619 }
Chris Masond1310b22008-01-24 16:13:08 -05002620 while (cur <= end) {
2621 if (cur >= last_byte) {
2622 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002623 struct extent_state *cached = NULL;
2624
David Sterba306e16c2011-04-19 14:29:38 +02002625 iosize = PAGE_CACHE_SIZE - pg_offset;
Cong Wang7ac687d2011-11-25 23:14:28 +08002626 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002627 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002628 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002629 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002630 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002631 &cached, GFP_NOFS);
2632 unlock_extent_cached(tree, cur, cur + iosize - 1,
2633 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002634 break;
2635 }
David Sterba306e16c2011-04-19 14:29:38 +02002636 em = get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002637 end - cur + 1, 0);
David Sterbac7040052011-04-19 18:00:01 +02002638 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002639 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002640 unlock_extent(tree, cur, end);
Chris Masond1310b22008-01-24 16:13:08 -05002641 break;
2642 }
Chris Masond1310b22008-01-24 16:13:08 -05002643 extent_offset = cur - em->start;
2644 BUG_ON(extent_map_end(em) <= cur);
2645 BUG_ON(end < cur);
2646
Li Zefan261507a02010-12-17 14:21:50 +08002647 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002648 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002649 extent_set_compress_type(&this_bio_flag,
2650 em->compress_type);
2651 }
Chris Masonc8b97812008-10-29 14:49:59 -04002652
Chris Masond1310b22008-01-24 16:13:08 -05002653 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2654 cur_end = min(extent_map_end(em) - 1, end);
2655 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002656 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2657 disk_io_size = em->block_len;
2658 sector = em->block_start >> 9;
2659 } else {
2660 sector = (em->block_start + extent_offset) >> 9;
2661 disk_io_size = iosize;
2662 }
Chris Masond1310b22008-01-24 16:13:08 -05002663 bdev = em->bdev;
2664 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002665 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2666 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002667 free_extent_map(em);
2668 em = NULL;
2669
2670 /* we've found a hole, just zero and go on */
2671 if (block_start == EXTENT_MAP_HOLE) {
2672 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002673 struct extent_state *cached = NULL;
2674
Cong Wang7ac687d2011-11-25 23:14:28 +08002675 userpage = kmap_atomic(page);
David Sterba306e16c2011-04-19 14:29:38 +02002676 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002677 flush_dcache_page(page);
Cong Wang7ac687d2011-11-25 23:14:28 +08002678 kunmap_atomic(userpage);
Chris Masond1310b22008-01-24 16:13:08 -05002679
2680 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002681 &cached, GFP_NOFS);
2682 unlock_extent_cached(tree, cur, cur + iosize - 1,
2683 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002684 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002685 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002686 continue;
2687 }
2688 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002689 if (test_range_bit(tree, cur, cur_end,
2690 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002691 check_page_uptodate(tree, page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002692 unlock_extent(tree, cur, cur + iosize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002693 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002694 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002695 continue;
2696 }
Chris Mason70dec802008-01-29 09:59:12 -05002697 /* we have an inline extent but it didn't get marked up
2698 * to date. Error out
2699 */
2700 if (block_start == EXTENT_MAP_INLINE) {
2701 SetPageError(page);
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002702 unlock_extent(tree, cur, cur + iosize - 1);
Chris Mason70dec802008-01-29 09:59:12 -05002703 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002704 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05002705 continue;
2706 }
Chris Masond1310b22008-01-24 16:13:08 -05002707
2708 ret = 0;
2709 if (tree->ops && tree->ops->readpage_io_hook) {
2710 ret = tree->ops->readpage_io_hook(page, cur,
2711 cur + iosize - 1);
2712 }
2713 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002714 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2715 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002716 ret = submit_extent_page(READ, tree, page,
David Sterba306e16c2011-04-19 14:29:38 +02002717 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04002718 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002719 end_bio_extent_readpage, mirror_num,
2720 *bio_flags,
2721 this_bio_flag);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002722 BUG_ON(ret == -ENOMEM);
Chris Mason89642222008-07-24 09:41:53 -04002723 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002724 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002725 }
2726 if (ret)
2727 SetPageError(page);
2728 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002729 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002730 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002731out:
Chris Masond1310b22008-01-24 16:13:08 -05002732 if (!nr) {
2733 if (!PageError(page))
2734 SetPageUptodate(page);
2735 unlock_page(page);
2736 }
2737 return 0;
2738}
2739
2740int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002741 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05002742{
2743 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002744 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002745 int ret;
2746
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002747 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Chris Masonc8b97812008-10-29 14:49:59 -04002748 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002749 if (bio)
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002750 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002751 return ret;
2752}
Chris Masond1310b22008-01-24 16:13:08 -05002753
Chris Mason11c83492009-04-20 15:50:09 -04002754static noinline void update_nr_written(struct page *page,
2755 struct writeback_control *wbc,
2756 unsigned long nr_written)
2757{
2758 wbc->nr_to_write -= nr_written;
2759 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2760 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2761 page->mapping->writeback_index = page->index + nr_written;
2762}
2763
Chris Masond1310b22008-01-24 16:13:08 -05002764/*
2765 * the writepage semantics are similar to regular writepage. extent
2766 * records are inserted to lock ranges in the tree, and as dirty areas
2767 * are found, they are marked writeback. Then the lock bits are removed
2768 * and the end_io handler clears the writeback ranges
2769 */
2770static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2771 void *data)
2772{
2773 struct inode *inode = page->mapping->host;
2774 struct extent_page_data *epd = data;
2775 struct extent_io_tree *tree = epd->tree;
2776 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2777 u64 delalloc_start;
2778 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2779 u64 end;
2780 u64 cur = start;
2781 u64 extent_offset;
2782 u64 last_byte = i_size_read(inode);
2783 u64 block_start;
2784 u64 iosize;
2785 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002786 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002787 struct extent_map *em;
2788 struct block_device *bdev;
2789 int ret;
2790 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002791 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002792 size_t blocksize;
2793 loff_t i_size = i_size_read(inode);
2794 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2795 u64 nr_delalloc;
2796 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002797 int page_started;
2798 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002799 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002800 unsigned long nr_written = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002801 bool fill_delalloc = true;
Chris Masond1310b22008-01-24 16:13:08 -05002802
Chris Masonffbd5172009-04-20 15:50:09 -04002803 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002804 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002805 else
2806 write_flags = WRITE;
2807
liubo1abe9b82011-03-24 11:18:59 +00002808 trace___extent_writepage(page, inode, wbc);
2809
Chris Masond1310b22008-01-24 16:13:08 -05002810 WARN_ON(!PageLocked(page));
Chris Masonbf0da8c2011-11-04 12:29:37 -04002811
2812 ClearPageError(page);
2813
Chris Mason7f3c74f2008-07-18 12:01:11 -04002814 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002815 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002816 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002817 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002818 unlock_page(page);
2819 return 0;
2820 }
2821
2822 if (page->index == end_index) {
2823 char *userpage;
2824
Cong Wang7ac687d2011-11-25 23:14:28 +08002825 userpage = kmap_atomic(page);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002826 memset(userpage + pg_offset, 0,
2827 PAGE_CACHE_SIZE - pg_offset);
Cong Wang7ac687d2011-11-25 23:14:28 +08002828 kunmap_atomic(userpage);
Chris Mason211c17f2008-05-15 09:13:45 -04002829 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002830 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002831 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002832
2833 set_page_extent_mapped(page);
2834
Josef Bacik9e487102011-08-01 12:08:18 -04002835 if (!tree->ops || !tree->ops->fill_delalloc)
2836 fill_delalloc = false;
2837
Chris Masond1310b22008-01-24 16:13:08 -05002838 delalloc_start = start;
2839 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002840 page_started = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002841 if (!epd->extent_locked && fill_delalloc) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002842 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002843 /*
2844 * make sure the wbc mapping index is at least updated
2845 * to this page.
2846 */
2847 update_nr_written(page, wbc, 0);
2848
Chris Masond3977122009-01-05 21:25:51 -05002849 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002850 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002851 page,
2852 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002853 &delalloc_end,
2854 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002855 if (nr_delalloc == 0) {
2856 delalloc_start = delalloc_end + 1;
2857 continue;
2858 }
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002859 ret = tree->ops->fill_delalloc(inode, page,
2860 delalloc_start,
2861 delalloc_end,
2862 &page_started,
2863 &nr_written);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002864 /* File system has been set read-only */
2865 if (ret) {
2866 SetPageError(page);
2867 goto done;
2868 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002869 /*
2870 * delalloc_end is already one less than the total
2871 * length, so we don't subtract one from
2872 * PAGE_CACHE_SIZE
2873 */
2874 delalloc_to_write += (delalloc_end - delalloc_start +
2875 PAGE_CACHE_SIZE) >>
2876 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002877 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002878 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002879 if (wbc->nr_to_write < delalloc_to_write) {
2880 int thresh = 8192;
2881
2882 if (delalloc_to_write < thresh * 2)
2883 thresh = delalloc_to_write;
2884 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2885 thresh);
2886 }
Chris Masonc8b97812008-10-29 14:49:59 -04002887
Chris Mason771ed682008-11-06 22:02:51 -05002888 /* did the fill delalloc function already unlock and start
2889 * the IO?
2890 */
2891 if (page_started) {
2892 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002893 /*
2894 * we've unlocked the page, so we can't update
2895 * the mapping's writeback index, just update
2896 * nr_to_write.
2897 */
2898 wbc->nr_to_write -= nr_written;
2899 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002900 }
Chris Masonc8b97812008-10-29 14:49:59 -04002901 }
Chris Mason247e7432008-07-17 12:53:51 -04002902 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002903 ret = tree->ops->writepage_start_hook(page, start,
2904 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002905 if (ret) {
2906 /* Fixup worker will requeue */
2907 if (ret == -EBUSY)
2908 wbc->pages_skipped++;
2909 else
2910 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002911 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002912 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002913 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002914 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002915 }
2916 }
2917
Chris Mason11c83492009-04-20 15:50:09 -04002918 /*
2919 * we don't want to touch the inode after unlocking the page,
2920 * so we update the mapping writeback index now
2921 */
2922 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002923
Chris Masond1310b22008-01-24 16:13:08 -05002924 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002925 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002926 if (tree->ops && tree->ops->writepage_end_io_hook)
2927 tree->ops->writepage_end_io_hook(page, start,
2928 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002929 goto done;
2930 }
2931
Chris Masond1310b22008-01-24 16:13:08 -05002932 blocksize = inode->i_sb->s_blocksize;
2933
2934 while (cur <= end) {
2935 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002936 if (tree->ops && tree->ops->writepage_end_io_hook)
2937 tree->ops->writepage_end_io_hook(page, cur,
2938 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002939 break;
2940 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002941 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002942 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02002943 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002944 SetPageError(page);
2945 break;
2946 }
2947
2948 extent_offset = cur - em->start;
2949 BUG_ON(extent_map_end(em) <= cur);
2950 BUG_ON(end < cur);
2951 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2952 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2953 sector = (em->block_start + extent_offset) >> 9;
2954 bdev = em->bdev;
2955 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002956 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002957 free_extent_map(em);
2958 em = NULL;
2959
Chris Masonc8b97812008-10-29 14:49:59 -04002960 /*
2961 * compressed and inline extents are written through other
2962 * paths in the FS
2963 */
2964 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002965 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002966 /*
2967 * end_io notification does not happen here for
2968 * compressed extents
2969 */
2970 if (!compressed && tree->ops &&
2971 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002972 tree->ops->writepage_end_io_hook(page, cur,
2973 cur + iosize - 1,
2974 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002975 else if (compressed) {
2976 /* we don't want to end_page_writeback on
2977 * a compressed extent. this happens
2978 * elsewhere
2979 */
2980 nr++;
2981 }
2982
2983 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002984 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002985 continue;
2986 }
Chris Masond1310b22008-01-24 16:13:08 -05002987 /* leave this out until we have a page_mkwrite call */
2988 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04002989 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002990 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002991 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002992 continue;
2993 }
Chris Masonc8b97812008-10-29 14:49:59 -04002994
Chris Masond1310b22008-01-24 16:13:08 -05002995 if (tree->ops && tree->ops->writepage_io_hook) {
2996 ret = tree->ops->writepage_io_hook(page, cur,
2997 cur + iosize - 1);
2998 } else {
2999 ret = 0;
3000 }
Chris Mason1259ab72008-05-12 13:39:03 -04003001 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05003002 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003003 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003004 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003005
Chris Masond1310b22008-01-24 16:13:08 -05003006 set_range_writeback(tree, cur, cur + iosize - 1);
3007 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05003008 printk(KERN_ERR "btrfs warning page %lu not "
3009 "writeback, cur %llu end %llu\n",
3010 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05003011 (unsigned long long)end);
3012 }
3013
Chris Masonffbd5172009-04-20 15:50:09 -04003014 ret = submit_extent_page(write_flags, tree, page,
3015 sector, iosize, pg_offset,
3016 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04003017 end_bio_extent_writepage,
3018 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003019 if (ret)
3020 SetPageError(page);
3021 }
3022 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04003023 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05003024 nr++;
3025 }
3026done:
3027 if (nr == 0) {
3028 /* make sure the mapping tag for page dirty gets cleared */
3029 set_page_writeback(page);
3030 end_page_writeback(page);
3031 }
Chris Masond1310b22008-01-24 16:13:08 -05003032 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05003033
Chris Mason11c83492009-04-20 15:50:09 -04003034done_unlocked:
3035
Chris Mason2c64c532009-09-02 15:04:12 -04003036 /* drop our reference on any cached states */
3037 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05003038 return 0;
3039}
3040
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003041static int eb_wait(void *word)
3042{
3043 io_schedule();
3044 return 0;
3045}
3046
3047static void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
3048{
3049 wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
3050 TASK_UNINTERRUPTIBLE);
3051}
3052
3053static int lock_extent_buffer_for_io(struct extent_buffer *eb,
3054 struct btrfs_fs_info *fs_info,
3055 struct extent_page_data *epd)
3056{
3057 unsigned long i, num_pages;
3058 int flush = 0;
3059 int ret = 0;
3060
3061 if (!btrfs_try_tree_write_lock(eb)) {
3062 flush = 1;
3063 flush_write_bio(epd);
3064 btrfs_tree_lock(eb);
3065 }
3066
3067 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3068 btrfs_tree_unlock(eb);
3069 if (!epd->sync_io)
3070 return 0;
3071 if (!flush) {
3072 flush_write_bio(epd);
3073 flush = 1;
3074 }
Chris Masona098d8e2012-03-21 12:09:56 -04003075 while (1) {
3076 wait_on_extent_buffer_writeback(eb);
3077 btrfs_tree_lock(eb);
3078 if (!test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags))
3079 break;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003080 btrfs_tree_unlock(eb);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003081 }
3082 }
3083
3084 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3085 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3086 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
3087 spin_lock(&fs_info->delalloc_lock);
3088 if (fs_info->dirty_metadata_bytes >= eb->len)
3089 fs_info->dirty_metadata_bytes -= eb->len;
3090 else
3091 WARN_ON(1);
3092 spin_unlock(&fs_info->delalloc_lock);
3093 ret = 1;
3094 }
3095
3096 btrfs_tree_unlock(eb);
3097
3098 if (!ret)
3099 return ret;
3100
3101 num_pages = num_extent_pages(eb->start, eb->len);
3102 for (i = 0; i < num_pages; i++) {
3103 struct page *p = extent_buffer_page(eb, i);
3104
3105 if (!trylock_page(p)) {
3106 if (!flush) {
3107 flush_write_bio(epd);
3108 flush = 1;
3109 }
3110 lock_page(p);
3111 }
3112 }
3113
3114 return ret;
3115}
3116
3117static void end_extent_buffer_writeback(struct extent_buffer *eb)
3118{
3119 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3120 smp_mb__after_clear_bit();
3121 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3122}
3123
3124static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
3125{
3126 int uptodate = err == 0;
3127 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
3128 struct extent_buffer *eb;
3129 int done;
3130
3131 do {
3132 struct page *page = bvec->bv_page;
3133
3134 bvec--;
3135 eb = (struct extent_buffer *)page->private;
3136 BUG_ON(!eb);
3137 done = atomic_dec_and_test(&eb->io_pages);
3138
3139 if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
3140 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3141 ClearPageUptodate(page);
3142 SetPageError(page);
3143 }
3144
3145 end_page_writeback(page);
3146
3147 if (!done)
3148 continue;
3149
3150 end_extent_buffer_writeback(eb);
3151 } while (bvec >= bio->bi_io_vec);
3152
3153 bio_put(bio);
3154
3155}
3156
3157static int write_one_eb(struct extent_buffer *eb,
3158 struct btrfs_fs_info *fs_info,
3159 struct writeback_control *wbc,
3160 struct extent_page_data *epd)
3161{
3162 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
3163 u64 offset = eb->start;
3164 unsigned long i, num_pages;
3165 int rw = (epd->sync_io ? WRITE_SYNC : WRITE);
3166 int ret;
3167
3168 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3169 num_pages = num_extent_pages(eb->start, eb->len);
3170 atomic_set(&eb->io_pages, num_pages);
3171 for (i = 0; i < num_pages; i++) {
3172 struct page *p = extent_buffer_page(eb, i);
3173
3174 clear_page_dirty_for_io(p);
3175 set_page_writeback(p);
3176 ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
3177 PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
3178 -1, end_bio_extent_buffer_writepage,
3179 0, 0, 0);
3180 if (ret) {
3181 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3182 SetPageError(p);
3183 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3184 end_extent_buffer_writeback(eb);
3185 ret = -EIO;
3186 break;
3187 }
3188 offset += PAGE_CACHE_SIZE;
3189 update_nr_written(p, wbc, 1);
3190 unlock_page(p);
3191 }
3192
3193 if (unlikely(ret)) {
3194 for (; i < num_pages; i++) {
3195 struct page *p = extent_buffer_page(eb, i);
3196 unlock_page(p);
3197 }
3198 }
3199
3200 return ret;
3201}
3202
3203int btree_write_cache_pages(struct address_space *mapping,
3204 struct writeback_control *wbc)
3205{
3206 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3207 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3208 struct extent_buffer *eb, *prev_eb = NULL;
3209 struct extent_page_data epd = {
3210 .bio = NULL,
3211 .tree = tree,
3212 .extent_locked = 0,
3213 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3214 };
3215 int ret = 0;
3216 int done = 0;
3217 int nr_to_write_done = 0;
3218 struct pagevec pvec;
3219 int nr_pages;
3220 pgoff_t index;
3221 pgoff_t end; /* Inclusive */
3222 int scanned = 0;
3223 int tag;
3224
3225 pagevec_init(&pvec, 0);
3226 if (wbc->range_cyclic) {
3227 index = mapping->writeback_index; /* Start from prev offset */
3228 end = -1;
3229 } else {
3230 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3231 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3232 scanned = 1;
3233 }
3234 if (wbc->sync_mode == WB_SYNC_ALL)
3235 tag = PAGECACHE_TAG_TOWRITE;
3236 else
3237 tag = PAGECACHE_TAG_DIRTY;
3238retry:
3239 if (wbc->sync_mode == WB_SYNC_ALL)
3240 tag_pages_for_writeback(mapping, index, end);
3241 while (!done && !nr_to_write_done && (index <= end) &&
3242 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3243 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3244 unsigned i;
3245
3246 scanned = 1;
3247 for (i = 0; i < nr_pages; i++) {
3248 struct page *page = pvec.pages[i];
3249
3250 if (!PagePrivate(page))
3251 continue;
3252
3253 if (!wbc->range_cyclic && page->index > end) {
3254 done = 1;
3255 break;
3256 }
3257
3258 eb = (struct extent_buffer *)page->private;
3259 if (!eb) {
3260 WARN_ON(1);
3261 continue;
3262 }
3263
3264 if (eb == prev_eb)
3265 continue;
3266
3267 if (!atomic_inc_not_zero(&eb->refs)) {
3268 WARN_ON(1);
3269 continue;
3270 }
3271
3272 prev_eb = eb;
3273 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3274 if (!ret) {
3275 free_extent_buffer(eb);
3276 continue;
3277 }
3278
3279 ret = write_one_eb(eb, fs_info, wbc, &epd);
3280 if (ret) {
3281 done = 1;
3282 free_extent_buffer(eb);
3283 break;
3284 }
3285 free_extent_buffer(eb);
3286
3287 /*
3288 * the filesystem may choose to bump up nr_to_write.
3289 * We have to make sure to honor the new nr_to_write
3290 * at any time
3291 */
3292 nr_to_write_done = wbc->nr_to_write <= 0;
3293 }
3294 pagevec_release(&pvec);
3295 cond_resched();
3296 }
3297 if (!scanned && !done) {
3298 /*
3299 * We hit the last page and there is more work to be done: wrap
3300 * back to the start of the file
3301 */
3302 scanned = 1;
3303 index = 0;
3304 goto retry;
3305 }
3306 flush_write_bio(&epd);
3307 return ret;
3308}
3309
Chris Masond1310b22008-01-24 16:13:08 -05003310/**
Chris Mason4bef0842008-09-08 11:18:08 -04003311 * 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 -05003312 * @mapping: address space structure to write
3313 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3314 * @writepage: function called for each page
3315 * @data: data passed to writepage function
3316 *
3317 * If a page is already under I/O, write_cache_pages() skips it, even
3318 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3319 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3320 * and msync() need to guarantee that all the data which was dirty at the time
3321 * the call was made get new I/O started against them. If wbc->sync_mode is
3322 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3323 * existing IO to complete.
3324 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003325static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04003326 struct address_space *mapping,
3327 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003328 writepage_t writepage, void *data,
3329 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05003330{
Chris Masond1310b22008-01-24 16:13:08 -05003331 int ret = 0;
3332 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003333 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003334 struct pagevec pvec;
3335 int nr_pages;
3336 pgoff_t index;
3337 pgoff_t end; /* Inclusive */
3338 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003339 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003340
Chris Masond1310b22008-01-24 16:13:08 -05003341 pagevec_init(&pvec, 0);
3342 if (wbc->range_cyclic) {
3343 index = mapping->writeback_index; /* Start from prev offset */
3344 end = -1;
3345 } else {
3346 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3347 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003348 scanned = 1;
3349 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003350 if (wbc->sync_mode == WB_SYNC_ALL)
3351 tag = PAGECACHE_TAG_TOWRITE;
3352 else
3353 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003354retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003355 if (wbc->sync_mode == WB_SYNC_ALL)
3356 tag_pages_for_writeback(mapping, index, end);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003357 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003358 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3359 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003360 unsigned i;
3361
3362 scanned = 1;
3363 for (i = 0; i < nr_pages; i++) {
3364 struct page *page = pvec.pages[i];
3365
3366 /*
3367 * At this point we hold neither mapping->tree_lock nor
3368 * lock on the page itself: the page may be truncated or
3369 * invalidated (changing page->mapping to NULL), or even
3370 * swizzled back from swapper_space to tmpfs file
3371 * mapping
3372 */
Chris Mason01d658f2011-11-01 10:08:06 -04003373 if (tree->ops &&
3374 tree->ops->write_cache_pages_lock_hook) {
3375 tree->ops->write_cache_pages_lock_hook(page,
3376 data, flush_fn);
3377 } else {
3378 if (!trylock_page(page)) {
3379 flush_fn(data);
3380 lock_page(page);
3381 }
3382 }
Chris Masond1310b22008-01-24 16:13:08 -05003383
3384 if (unlikely(page->mapping != mapping)) {
3385 unlock_page(page);
3386 continue;
3387 }
3388
3389 if (!wbc->range_cyclic && page->index > end) {
3390 done = 1;
3391 unlock_page(page);
3392 continue;
3393 }
3394
Chris Masond2c3f4f2008-11-19 12:44:22 -05003395 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003396 if (PageWriteback(page))
3397 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05003398 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003399 }
Chris Masond1310b22008-01-24 16:13:08 -05003400
3401 if (PageWriteback(page) ||
3402 !clear_page_dirty_for_io(page)) {
3403 unlock_page(page);
3404 continue;
3405 }
3406
3407 ret = (*writepage)(page, wbc, data);
3408
3409 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3410 unlock_page(page);
3411 ret = 0;
3412 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003413 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05003414 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003415
3416 /*
3417 * the filesystem may choose to bump up nr_to_write.
3418 * We have to make sure to honor the new nr_to_write
3419 * at any time
3420 */
3421 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05003422 }
3423 pagevec_release(&pvec);
3424 cond_resched();
3425 }
3426 if (!scanned && !done) {
3427 /*
3428 * We hit the last page and there is more work to be done: wrap
3429 * back to the start of the file
3430 */
3431 scanned = 1;
3432 index = 0;
3433 goto retry;
3434 }
Chris Masond1310b22008-01-24 16:13:08 -05003435 return ret;
3436}
Chris Masond1310b22008-01-24 16:13:08 -05003437
Chris Masonffbd5172009-04-20 15:50:09 -04003438static void flush_epd_write_bio(struct extent_page_data *epd)
3439{
3440 if (epd->bio) {
Jeff Mahoney355808c2011-10-03 23:23:14 -04003441 int rw = WRITE;
3442 int ret;
3443
Chris Masonffbd5172009-04-20 15:50:09 -04003444 if (epd->sync_io)
Jeff Mahoney355808c2011-10-03 23:23:14 -04003445 rw = WRITE_SYNC;
3446
3447 ret = submit_one_bio(rw, epd->bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003448 BUG_ON(ret < 0); /* -ENOMEM */
Chris Masonffbd5172009-04-20 15:50:09 -04003449 epd->bio = NULL;
3450 }
3451}
3452
Chris Masond2c3f4f2008-11-19 12:44:22 -05003453static noinline void flush_write_bio(void *data)
3454{
3455 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04003456 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003457}
3458
Chris Masond1310b22008-01-24 16:13:08 -05003459int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3460 get_extent_t *get_extent,
3461 struct writeback_control *wbc)
3462{
3463 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003464 struct extent_page_data epd = {
3465 .bio = NULL,
3466 .tree = tree,
3467 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003468 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003469 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05003470 };
Chris Masond1310b22008-01-24 16:13:08 -05003471
Chris Masond1310b22008-01-24 16:13:08 -05003472 ret = __extent_writepage(page, wbc, &epd);
3473
Chris Masonffbd5172009-04-20 15:50:09 -04003474 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003475 return ret;
3476}
Chris Masond1310b22008-01-24 16:13:08 -05003477
Chris Mason771ed682008-11-06 22:02:51 -05003478int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3479 u64 start, u64 end, get_extent_t *get_extent,
3480 int mode)
3481{
3482 int ret = 0;
3483 struct address_space *mapping = inode->i_mapping;
3484 struct page *page;
3485 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3486 PAGE_CACHE_SHIFT;
3487
3488 struct extent_page_data epd = {
3489 .bio = NULL,
3490 .tree = tree,
3491 .get_extent = get_extent,
3492 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04003493 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05003494 };
3495 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05003496 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05003497 .nr_to_write = nr_pages * 2,
3498 .range_start = start,
3499 .range_end = end + 1,
3500 };
3501
Chris Masond3977122009-01-05 21:25:51 -05003502 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05003503 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3504 if (clear_page_dirty_for_io(page))
3505 ret = __extent_writepage(page, &wbc_writepages, &epd);
3506 else {
3507 if (tree->ops && tree->ops->writepage_end_io_hook)
3508 tree->ops->writepage_end_io_hook(page, start,
3509 start + PAGE_CACHE_SIZE - 1,
3510 NULL, 1);
3511 unlock_page(page);
3512 }
3513 page_cache_release(page);
3514 start += PAGE_CACHE_SIZE;
3515 }
3516
Chris Masonffbd5172009-04-20 15:50:09 -04003517 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05003518 return ret;
3519}
Chris Masond1310b22008-01-24 16:13:08 -05003520
3521int extent_writepages(struct extent_io_tree *tree,
3522 struct address_space *mapping,
3523 get_extent_t *get_extent,
3524 struct writeback_control *wbc)
3525{
3526 int ret = 0;
3527 struct extent_page_data epd = {
3528 .bio = NULL,
3529 .tree = tree,
3530 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003531 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003532 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05003533 };
3534
Chris Mason4bef0842008-09-08 11:18:08 -04003535 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003536 __extent_writepage, &epd,
3537 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04003538 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003539 return ret;
3540}
Chris Masond1310b22008-01-24 16:13:08 -05003541
3542int extent_readpages(struct extent_io_tree *tree,
3543 struct address_space *mapping,
3544 struct list_head *pages, unsigned nr_pages,
3545 get_extent_t get_extent)
3546{
3547 struct bio *bio = NULL;
3548 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04003549 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003550
Chris Masond1310b22008-01-24 16:13:08 -05003551 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
3552 struct page *page = list_entry(pages->prev, struct page, lru);
3553
3554 prefetchw(&page->flags);
3555 list_del(&page->lru);
Nick Piggin28ecb6092010-03-17 13:31:04 +00003556 if (!add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04003557 page->index, GFP_NOFS)) {
Chris Masonf1885912008-04-09 16:28:12 -04003558 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04003559 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003560 }
3561 page_cache_release(page);
3562 }
Chris Masond1310b22008-01-24 16:13:08 -05003563 BUG_ON(!list_empty(pages));
3564 if (bio)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003565 return submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003566 return 0;
3567}
Chris Masond1310b22008-01-24 16:13:08 -05003568
3569/*
3570 * basic invalidatepage code, this waits on any locked or writeback
3571 * ranges corresponding to the page, and then deletes any extent state
3572 * records from the tree
3573 */
3574int extent_invalidatepage(struct extent_io_tree *tree,
3575 struct page *page, unsigned long offset)
3576{
Josef Bacik2ac55d42010-02-03 19:33:23 +00003577 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003578 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
3579 u64 end = start + PAGE_CACHE_SIZE - 1;
3580 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3581
Chris Masond3977122009-01-05 21:25:51 -05003582 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003583 if (start > end)
3584 return 0;
3585
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003586 lock_extent_bits(tree, start, end, 0, &cached_state);
Chris Mason1edbb732009-09-02 13:24:36 -04003587 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003588 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04003589 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3590 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003591 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003592 return 0;
3593}
Chris Masond1310b22008-01-24 16:13:08 -05003594
3595/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04003596 * a helper for releasepage, this tests for areas of the page that
3597 * are locked or under IO and drops the related state bits if it is safe
3598 * to drop the page.
3599 */
3600int try_release_extent_state(struct extent_map_tree *map,
3601 struct extent_io_tree *tree, struct page *page,
3602 gfp_t mask)
3603{
3604 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3605 u64 end = start + PAGE_CACHE_SIZE - 1;
3606 int ret = 1;
3607
Chris Mason211f90e2008-07-18 11:56:15 -04003608 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04003609 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04003610 ret = 0;
3611 else {
3612 if ((mask & GFP_NOFS) == GFP_NOFS)
3613 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04003614 /*
3615 * at this point we can safely clear everything except the
3616 * locked bit and the nodatasum bit
3617 */
Chris Masone3f24cc2011-02-14 12:52:08 -05003618 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04003619 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3620 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05003621
3622 /* if clear_extent_bit failed for enomem reasons,
3623 * we can't allow the release to continue.
3624 */
3625 if (ret < 0)
3626 ret = 0;
3627 else
3628 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003629 }
3630 return ret;
3631}
Chris Mason7b13b7b2008-04-18 10:29:50 -04003632
3633/*
Chris Masond1310b22008-01-24 16:13:08 -05003634 * a helper for releasepage. As long as there are no locked extents
3635 * in the range corresponding to the page, both state records and extent
3636 * map records are removed
3637 */
3638int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05003639 struct extent_io_tree *tree, struct page *page,
3640 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05003641{
3642 struct extent_map *em;
3643 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3644 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003645
Chris Mason70dec802008-01-29 09:59:12 -05003646 if ((mask & __GFP_WAIT) &&
3647 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05003648 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05003649 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05003650 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04003651 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05003652 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09003653 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04003654 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003655 break;
3656 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003657 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3658 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04003659 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003660 free_extent_map(em);
3661 break;
3662 }
3663 if (!test_range_bit(tree, em->start,
3664 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04003665 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04003666 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05003667 remove_extent_mapping(map, em);
3668 /* once for the rb tree */
3669 free_extent_map(em);
3670 }
3671 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04003672 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003673
3674 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05003675 free_extent_map(em);
3676 }
Chris Masond1310b22008-01-24 16:13:08 -05003677 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04003678 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003679}
Chris Masond1310b22008-01-24 16:13:08 -05003680
Chris Masonec29ed52011-02-23 16:23:20 -05003681/*
3682 * helper function for fiemap, which doesn't want to see any holes.
3683 * This maps until we find something past 'last'
3684 */
3685static struct extent_map *get_extent_skip_holes(struct inode *inode,
3686 u64 offset,
3687 u64 last,
3688 get_extent_t *get_extent)
3689{
3690 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3691 struct extent_map *em;
3692 u64 len;
3693
3694 if (offset >= last)
3695 return NULL;
3696
3697 while(1) {
3698 len = last - offset;
3699 if (len == 0)
3700 break;
3701 len = (len + sectorsize - 1) & ~(sectorsize - 1);
3702 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02003703 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05003704 return em;
3705
3706 /* if this isn't a hole return it */
3707 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3708 em->block_start != EXTENT_MAP_HOLE) {
3709 return em;
3710 }
3711
3712 /* this is a hole, advance to the next extent */
3713 offset = extent_map_end(em);
3714 free_extent_map(em);
3715 if (offset >= last)
3716 break;
3717 }
3718 return NULL;
3719}
3720
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003721int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3722 __u64 start, __u64 len, get_extent_t *get_extent)
3723{
Josef Bacik975f84f2010-11-23 19:36:57 +00003724 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003725 u64 off = start;
3726 u64 max = start + len;
3727 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00003728 u32 found_type;
3729 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05003730 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003731 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003732 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00003733 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003734 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003735 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00003736 struct btrfs_path *path;
3737 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003738 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003739 u64 em_start = 0;
3740 u64 em_len = 0;
3741 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003742 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003743
3744 if (len == 0)
3745 return -EINVAL;
3746
Josef Bacik975f84f2010-11-23 19:36:57 +00003747 path = btrfs_alloc_path();
3748 if (!path)
3749 return -ENOMEM;
3750 path->leave_spinning = 1;
3751
Josef Bacik4d479cf2011-11-17 11:34:31 -05003752 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3753 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3754
Chris Masonec29ed52011-02-23 16:23:20 -05003755 /*
3756 * lookup the last file extent. We're not using i_size here
3757 * because there might be preallocation past i_size
3758 */
Josef Bacik975f84f2010-11-23 19:36:57 +00003759 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
Li Zefan33345d012011-04-20 10:31:50 +08003760 path, btrfs_ino(inode), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00003761 if (ret < 0) {
3762 btrfs_free_path(path);
3763 return ret;
3764 }
3765 WARN_ON(!ret);
3766 path->slots[0]--;
3767 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3768 struct btrfs_file_extent_item);
3769 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3770 found_type = btrfs_key_type(&found_key);
3771
Chris Masonec29ed52011-02-23 16:23:20 -05003772 /* No extents, but there might be delalloc bits */
Li Zefan33345d012011-04-20 10:31:50 +08003773 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00003774 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05003775 /* have to trust i_size as the end */
3776 last = (u64)-1;
3777 last_for_get_extent = isize;
3778 } else {
3779 /*
3780 * remember the start of the last extent. There are a
3781 * bunch of different factors that go into the length of the
3782 * extent, so its much less complex to remember where it started
3783 */
3784 last = found_key.offset;
3785 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00003786 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003787 btrfs_free_path(path);
3788
Chris Masonec29ed52011-02-23 16:23:20 -05003789 /*
3790 * we might have some extents allocated but more delalloc past those
3791 * extents. so, we trust isize unless the start of the last extent is
3792 * beyond isize
3793 */
3794 if (last < isize) {
3795 last = (u64)-1;
3796 last_for_get_extent = isize;
3797 }
3798
Josef Bacik2ac55d42010-02-03 19:33:23 +00003799 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01003800 &cached_state);
Chris Masonec29ed52011-02-23 16:23:20 -05003801
Josef Bacik4d479cf2011-11-17 11:34:31 -05003802 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05003803 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003804 if (!em)
3805 goto out;
3806 if (IS_ERR(em)) {
3807 ret = PTR_ERR(em);
3808 goto out;
3809 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003810
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003811 while (!end) {
Chris Masonea8efc72011-03-08 11:54:40 -05003812 u64 offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003813
Chris Masonea8efc72011-03-08 11:54:40 -05003814 /* break if the extent we found is outside the range */
3815 if (em->start >= max || extent_map_end(em) < off)
3816 break;
3817
3818 /*
3819 * get_extent may return an extent that starts before our
3820 * requested range. We have to make sure the ranges
3821 * we return to fiemap always move forward and don't
3822 * overlap, so adjust the offsets here
3823 */
3824 em_start = max(em->start, off);
3825
3826 /*
3827 * record the offset from the start of the extent
3828 * for adjusting the disk offset below
3829 */
3830 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05003831 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05003832 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05003833 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003834 disko = 0;
3835 flags = 0;
3836
Chris Masonea8efc72011-03-08 11:54:40 -05003837 /*
3838 * bump off for our next call to get_extent
3839 */
3840 off = extent_map_end(em);
3841 if (off >= max)
3842 end = 1;
3843
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003844 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003845 end = 1;
3846 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003847 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003848 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3849 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003850 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003851 flags |= (FIEMAP_EXTENT_DELALLOC |
3852 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003853 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05003854 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003855 }
3856 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3857 flags |= FIEMAP_EXTENT_ENCODED;
3858
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003859 free_extent_map(em);
3860 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05003861 if ((em_start >= last) || em_len == (u64)-1 ||
3862 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003863 flags |= FIEMAP_EXTENT_LAST;
3864 end = 1;
3865 }
3866
Chris Masonec29ed52011-02-23 16:23:20 -05003867 /* now scan forward to see if this is really the last extent. */
3868 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3869 get_extent);
3870 if (IS_ERR(em)) {
3871 ret = PTR_ERR(em);
3872 goto out;
3873 }
3874 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00003875 flags |= FIEMAP_EXTENT_LAST;
3876 end = 1;
3877 }
Chris Masonec29ed52011-02-23 16:23:20 -05003878 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3879 em_len, flags);
3880 if (ret)
3881 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003882 }
3883out_free:
3884 free_extent_map(em);
3885out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00003886 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3887 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003888 return ret;
3889}
3890
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003891inline struct page *extent_buffer_page(struct extent_buffer *eb,
Chris Masond1310b22008-01-24 16:13:08 -05003892 unsigned long i)
3893{
Chris Mason727011e2010-08-06 13:21:20 -04003894 return eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05003895}
3896
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003897inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04003898{
Chris Mason6af118ce2008-07-22 11:18:07 -04003899 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3900 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04003901}
3902
Chris Mason727011e2010-08-06 13:21:20 -04003903static void __free_extent_buffer(struct extent_buffer *eb)
3904{
3905#if LEAK_DEBUG
3906 unsigned long flags;
3907 spin_lock_irqsave(&leak_lock, flags);
3908 list_del(&eb->leak_list);
3909 spin_unlock_irqrestore(&leak_lock, flags);
3910#endif
3911 if (eb->pages && eb->pages != eb->inline_pages)
3912 kfree(eb->pages);
3913 kmem_cache_free(extent_buffer_cache, eb);
3914}
3915
Chris Masond1310b22008-01-24 16:13:08 -05003916static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3917 u64 start,
3918 unsigned long len,
3919 gfp_t mask)
3920{
3921 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003922#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003923 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003924#endif
Chris Masond1310b22008-01-24 16:13:08 -05003925
Chris Masond1310b22008-01-24 16:13:08 -05003926 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003927 if (eb == NULL)
3928 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003929 eb->start = start;
3930 eb->len = len;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05003931 eb->tree = tree;
Chris Masonbd681512011-07-16 15:23:14 -04003932 rwlock_init(&eb->lock);
3933 atomic_set(&eb->write_locks, 0);
3934 atomic_set(&eb->read_locks, 0);
3935 atomic_set(&eb->blocking_readers, 0);
3936 atomic_set(&eb->blocking_writers, 0);
3937 atomic_set(&eb->spinning_readers, 0);
3938 atomic_set(&eb->spinning_writers, 0);
Arne Jansen5b25f702011-09-13 10:55:48 +02003939 eb->lock_nested = 0;
Chris Masonbd681512011-07-16 15:23:14 -04003940 init_waitqueue_head(&eb->write_lock_wq);
3941 init_waitqueue_head(&eb->read_lock_wq);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003942
Chris Mason39351272009-02-04 09:24:05 -05003943#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003944 spin_lock_irqsave(&leak_lock, flags);
3945 list_add(&eb->leak_list, &buffers);
3946 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003947#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05003948 spin_lock_init(&eb->refs_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003949 atomic_set(&eb->refs, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003950 atomic_set(&eb->io_pages, 0);
Chris Mason727011e2010-08-06 13:21:20 -04003951
3952 if (len > MAX_INLINE_EXTENT_BUFFER_SIZE) {
3953 struct page **pages;
3954 int num_pages = (len + PAGE_CACHE_SIZE - 1) >>
3955 PAGE_CACHE_SHIFT;
3956 pages = kzalloc(num_pages, mask);
3957 if (!pages) {
3958 __free_extent_buffer(eb);
3959 return NULL;
3960 }
3961 eb->pages = pages;
3962 } else {
3963 eb->pages = eb->inline_pages;
3964 }
Chris Masond1310b22008-01-24 16:13:08 -05003965
3966 return eb;
3967}
3968
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003969static int extent_buffer_under_io(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05003970{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003971 return (atomic_read(&eb->io_pages) ||
3972 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
3973 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Chris Masond1310b22008-01-24 16:13:08 -05003974}
3975
Miao Xie897ca6e2010-10-26 20:57:29 -04003976/*
3977 * Helper for releasing extent buffer page.
3978 */
3979static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
3980 unsigned long start_idx)
3981{
3982 unsigned long index;
Wang Sheng-Hui39bab872012-04-06 14:35:31 +08003983 unsigned long num_pages;
Miao Xie897ca6e2010-10-26 20:57:29 -04003984 struct page *page;
3985
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003986 BUG_ON(extent_buffer_under_io(eb));
Miao Xie897ca6e2010-10-26 20:57:29 -04003987
Wang Sheng-Hui39bab872012-04-06 14:35:31 +08003988 num_pages = num_extent_pages(eb->start, eb->len);
3989 index = start_idx + num_pages;
Miao Xie897ca6e2010-10-26 20:57:29 -04003990 if (start_idx >= index)
3991 return;
3992
3993 do {
3994 index--;
3995 page = extent_buffer_page(eb, index);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05003996 if (page) {
3997 spin_lock(&page->mapping->private_lock);
3998 /*
3999 * We do this since we'll remove the pages after we've
4000 * removed the eb from the radix tree, so we could race
4001 * and have this page now attached to the new eb. So
4002 * only clear page_private if it's still connected to
4003 * this eb.
4004 */
4005 if (PagePrivate(page) &&
4006 page->private == (unsigned long)eb) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004007 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Josef Bacik3083ee22012-03-09 16:01:49 -05004008 BUG_ON(PageDirty(page));
4009 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004010 /*
4011 * We need to make sure we haven't be attached
4012 * to a new eb.
4013 */
4014 ClearPagePrivate(page);
4015 set_page_private(page, 0);
4016 /* One for the page private */
4017 page_cache_release(page);
4018 }
4019 spin_unlock(&page->mapping->private_lock);
4020
4021 /* One for when we alloced the page */
Miao Xie897ca6e2010-10-26 20:57:29 -04004022 page_cache_release(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004023 }
Miao Xie897ca6e2010-10-26 20:57:29 -04004024 } while (index != start_idx);
4025}
4026
4027/*
4028 * Helper for releasing the extent buffer.
4029 */
4030static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
4031{
4032 btrfs_release_extent_buffer_page(eb, 0);
4033 __free_extent_buffer(eb);
4034}
4035
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004036static void check_buffer_tree_ref(struct extent_buffer *eb)
4037{
4038 /* the ref bit is tricky. We have to make sure it is set
4039 * if we have the buffer dirty. Otherwise the
4040 * code to free a buffer can end up dropping a dirty
4041 * page
4042 *
4043 * Once the ref bit is set, it won't go away while the
4044 * buffer is dirty or in writeback, and it also won't
4045 * go away while we have the reference count on the
4046 * eb bumped.
4047 *
4048 * We can't just set the ref bit without bumping the
4049 * ref on the eb because free_extent_buffer might
4050 * see the ref bit and try to clear it. If this happens
4051 * free_extent_buffer might end up dropping our original
4052 * ref by mistake and freeing the page before we are able
4053 * to add one more ref.
4054 *
4055 * So bump the ref count first, then set the bit. If someone
4056 * beat us to it, drop the ref we added.
4057 */
4058 if (!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
4059 atomic_inc(&eb->refs);
4060 if (test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4061 atomic_dec(&eb->refs);
4062 }
4063}
4064
Josef Bacik5df42352012-03-15 18:24:42 -04004065static void mark_extent_buffer_accessed(struct extent_buffer *eb)
4066{
4067 unsigned long num_pages, i;
4068
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004069 check_buffer_tree_ref(eb);
4070
Josef Bacik5df42352012-03-15 18:24:42 -04004071 num_pages = num_extent_pages(eb->start, eb->len);
4072 for (i = 0; i < num_pages; i++) {
4073 struct page *p = extent_buffer_page(eb, i);
4074 mark_page_accessed(p);
4075 }
4076}
4077
Chris Masond1310b22008-01-24 16:13:08 -05004078struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
Chris Mason727011e2010-08-06 13:21:20 -04004079 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004080{
4081 unsigned long num_pages = num_extent_pages(start, len);
4082 unsigned long i;
4083 unsigned long index = start >> PAGE_CACHE_SHIFT;
4084 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004085 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004086 struct page *p;
4087 struct address_space *mapping = tree->mapping;
4088 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004089 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004090
Miao Xie19fe0a82010-10-26 20:57:29 -04004091 rcu_read_lock();
4092 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4093 if (eb && atomic_inc_not_zero(&eb->refs)) {
4094 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004095 mark_extent_buffer_accessed(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04004096 return eb;
4097 }
Miao Xie19fe0a82010-10-26 20:57:29 -04004098 rcu_read_unlock();
Chris Mason6af118ce2008-07-22 11:18:07 -04004099
David Sterbaba144192011-04-21 01:12:06 +02004100 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
Peter2b114d12008-04-01 11:21:40 -04004101 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004102 return NULL;
4103
Chris Mason727011e2010-08-06 13:21:20 -04004104 for (i = 0; i < num_pages; i++, index++) {
Chris Masona6591712011-07-19 12:04:14 -04004105 p = find_or_create_page(mapping, index, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05004106 if (!p) {
4107 WARN_ON(1);
Chris Mason6af118ce2008-07-22 11:18:07 -04004108 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05004109 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004110
4111 spin_lock(&mapping->private_lock);
4112 if (PagePrivate(p)) {
4113 /*
4114 * We could have already allocated an eb for this page
4115 * and attached one so lets see if we can get a ref on
4116 * the existing eb, and if we can we know it's good and
4117 * we can just return that one, else we know we can just
4118 * overwrite page->private.
4119 */
4120 exists = (struct extent_buffer *)p->private;
4121 if (atomic_inc_not_zero(&exists->refs)) {
4122 spin_unlock(&mapping->private_lock);
4123 unlock_page(p);
Josef Bacik17de39a2012-05-04 15:16:06 -04004124 page_cache_release(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004125 mark_extent_buffer_accessed(exists);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004126 goto free_eb;
4127 }
4128
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004129 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004130 * Do this so attach doesn't complain and we need to
4131 * drop the ref the old guy had.
4132 */
4133 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004134 WARN_ON(PageDirty(p));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004135 page_cache_release(p);
Chris Masond1310b22008-01-24 16:13:08 -05004136 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004137 attach_extent_buffer_page(eb, p);
4138 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004139 WARN_ON(PageDirty(p));
Chris Masond1310b22008-01-24 16:13:08 -05004140 mark_page_accessed(p);
Chris Mason727011e2010-08-06 13:21:20 -04004141 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05004142 if (!PageUptodate(p))
4143 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05004144
4145 /*
4146 * see below about how we avoid a nasty race with release page
4147 * and why we unlock later
4148 */
Chris Masond1310b22008-01-24 16:13:08 -05004149 }
4150 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004151 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05004152again:
Miao Xie19fe0a82010-10-26 20:57:29 -04004153 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4154 if (ret)
4155 goto free_eb;
4156
Chris Mason6af118ce2008-07-22 11:18:07 -04004157 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004158 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
4159 if (ret == -EEXIST) {
4160 exists = radix_tree_lookup(&tree->buffer,
4161 start >> PAGE_CACHE_SHIFT);
Josef Bacik115391d2012-03-09 09:51:43 -05004162 if (!atomic_inc_not_zero(&exists->refs)) {
4163 spin_unlock(&tree->buffer_lock);
4164 radix_tree_preload_end();
Josef Bacik115391d2012-03-09 09:51:43 -05004165 exists = NULL;
4166 goto again;
4167 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004168 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004169 radix_tree_preload_end();
Josef Bacik5df42352012-03-15 18:24:42 -04004170 mark_extent_buffer_accessed(exists);
Chris Mason6af118ce2008-07-22 11:18:07 -04004171 goto free_eb;
4172 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004173 /* add one reference for the tree */
Josef Bacik3083ee22012-03-09 16:01:49 -05004174 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004175 check_buffer_tree_ref(eb);
Josef Bacik3083ee22012-03-09 16:01:49 -05004176 spin_unlock(&eb->refs_lock);
Yan, Zhengf044ba72010-02-04 08:46:56 +00004177 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004178 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05004179
4180 /*
4181 * there is a race where release page may have
4182 * tried to find this extent buffer in the radix
4183 * but failed. It will tell the VM it is safe to
4184 * reclaim the, and it will clear the page private bit.
4185 * We must make sure to set the page private bit properly
4186 * after the extent buffer is in the radix tree so
4187 * it doesn't get lost
4188 */
Chris Mason727011e2010-08-06 13:21:20 -04004189 SetPageChecked(eb->pages[0]);
4190 for (i = 1; i < num_pages; i++) {
4191 p = extent_buffer_page(eb, i);
Chris Mason727011e2010-08-06 13:21:20 -04004192 ClearPageChecked(p);
4193 unlock_page(p);
4194 }
4195 unlock_page(eb->pages[0]);
Chris Masond1310b22008-01-24 16:13:08 -05004196 return eb;
4197
Chris Mason6af118ce2008-07-22 11:18:07 -04004198free_eb:
Chris Mason727011e2010-08-06 13:21:20 -04004199 for (i = 0; i < num_pages; i++) {
4200 if (eb->pages[i])
4201 unlock_page(eb->pages[i]);
4202 }
Chris Masoneb14ab82011-02-10 12:35:00 -05004203
Josef Bacik17de39a2012-05-04 15:16:06 -04004204 WARN_ON(!atomic_dec_and_test(&eb->refs));
Miao Xie897ca6e2010-10-26 20:57:29 -04004205 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04004206 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05004207}
Chris Masond1310b22008-01-24 16:13:08 -05004208
4209struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
David Sterbaf09d1f62011-04-21 01:08:01 +02004210 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004211{
Chris Masond1310b22008-01-24 16:13:08 -05004212 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05004213
Miao Xie19fe0a82010-10-26 20:57:29 -04004214 rcu_read_lock();
4215 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4216 if (eb && atomic_inc_not_zero(&eb->refs)) {
4217 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004218 mark_extent_buffer_accessed(eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04004219 return eb;
4220 }
4221 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04004222
Miao Xie19fe0a82010-10-26 20:57:29 -04004223 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004224}
Chris Masond1310b22008-01-24 16:13:08 -05004225
Josef Bacik3083ee22012-03-09 16:01:49 -05004226static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4227{
4228 struct extent_buffer *eb =
4229 container_of(head, struct extent_buffer, rcu_head);
4230
4231 __free_extent_buffer(eb);
4232}
4233
Josef Bacik3083ee22012-03-09 16:01:49 -05004234/* Expects to have eb->eb_lock already held */
4235static void release_extent_buffer(struct extent_buffer *eb, gfp_t mask)
4236{
4237 WARN_ON(atomic_read(&eb->refs) == 0);
4238 if (atomic_dec_and_test(&eb->refs)) {
4239 struct extent_io_tree *tree = eb->tree;
Josef Bacik3083ee22012-03-09 16:01:49 -05004240
4241 spin_unlock(&eb->refs_lock);
4242
Josef Bacik3083ee22012-03-09 16:01:49 -05004243 spin_lock(&tree->buffer_lock);
4244 radix_tree_delete(&tree->buffer,
4245 eb->start >> PAGE_CACHE_SHIFT);
4246 spin_unlock(&tree->buffer_lock);
4247
4248 /* Should be safe to release our pages at this point */
4249 btrfs_release_extent_buffer_page(eb, 0);
4250
4251 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
4252 return;
4253 }
4254 spin_unlock(&eb->refs_lock);
4255}
4256
Chris Masond1310b22008-01-24 16:13:08 -05004257void free_extent_buffer(struct extent_buffer *eb)
4258{
Chris Masond1310b22008-01-24 16:13:08 -05004259 if (!eb)
4260 return;
4261
Josef Bacik3083ee22012-03-09 16:01:49 -05004262 spin_lock(&eb->refs_lock);
4263 if (atomic_read(&eb->refs) == 2 &&
4264 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004265 !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);
Chris Masond1310b22008-01-24 16:13:08 -05004268
Josef Bacik3083ee22012-03-09 16:01:49 -05004269 /*
4270 * I know this is terrible, but it's temporary until we stop tracking
4271 * the uptodate bits and such for the extent buffers.
4272 */
4273 release_extent_buffer(eb, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05004274}
Chris Masond1310b22008-01-24 16:13:08 -05004275
Josef Bacik3083ee22012-03-09 16:01:49 -05004276void free_extent_buffer_stale(struct extent_buffer *eb)
4277{
4278 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004279 return;
4280
Josef Bacik3083ee22012-03-09 16:01:49 -05004281 spin_lock(&eb->refs_lock);
4282 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
4283
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004284 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004285 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4286 atomic_dec(&eb->refs);
4287 release_extent_buffer(eb, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05004288}
4289
Chris Mason1d4284b2012-03-28 20:31:37 -04004290void clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004291{
Chris Masond1310b22008-01-24 16:13:08 -05004292 unsigned long i;
4293 unsigned long num_pages;
4294 struct page *page;
4295
Chris Masond1310b22008-01-24 16:13:08 -05004296 num_pages = num_extent_pages(eb->start, eb->len);
4297
4298 for (i = 0; i < num_pages; i++) {
4299 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04004300 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05004301 continue;
4302
Chris Masona61e6f22008-07-22 11:18:08 -04004303 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05004304 WARN_ON(!PagePrivate(page));
4305
Chris Masond1310b22008-01-24 16:13:08 -05004306 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004307 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05004308 if (!PageDirty(page)) {
4309 radix_tree_tag_clear(&page->mapping->page_tree,
4310 page_index(page),
4311 PAGECACHE_TAG_DIRTY);
4312 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004313 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04004314 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04004315 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05004316 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004317 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05004318}
Chris Masond1310b22008-01-24 16:13:08 -05004319
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004320int set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004321{
4322 unsigned long i;
4323 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04004324 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004325
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004326 check_buffer_tree_ref(eb);
4327
Chris Masonb9473432009-03-13 11:00:37 -04004328 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004329
Chris Masond1310b22008-01-24 16:13:08 -05004330 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik3083ee22012-03-09 16:01:49 -05004331 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004332 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
4333
Chris Masonb9473432009-03-13 11:00:37 -04004334 for (i = 0; i < num_pages; i++)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004335 set_page_dirty(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04004336 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05004337}
Chris Masond1310b22008-01-24 16:13:08 -05004338
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004339static int range_straddles_pages(u64 start, u64 len)
Chris Mason19b6caf2011-07-25 06:50:50 -04004340{
4341 if (len < PAGE_CACHE_SIZE)
4342 return 1;
4343 if (start & (PAGE_CACHE_SIZE - 1))
4344 return 1;
4345 if ((start + len) & (PAGE_CACHE_SIZE - 1))
4346 return 1;
4347 return 0;
4348}
4349
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004350int clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04004351{
4352 unsigned long i;
4353 struct page *page;
4354 unsigned long num_pages;
4355
Chris Masonb4ce94d2009-02-04 09:25:08 -05004356 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004357 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason1259ab72008-05-12 13:39:03 -04004358 for (i = 0; i < num_pages; i++) {
4359 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04004360 if (page)
4361 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04004362 }
4363 return 0;
4364}
4365
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004366int set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004367{
4368 unsigned long i;
4369 struct page *page;
4370 unsigned long num_pages;
4371
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004372 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004373 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05004374 for (i = 0; i < num_pages; i++) {
4375 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004376 SetPageUptodate(page);
4377 }
4378 return 0;
4379}
Chris Masond1310b22008-01-24 16:13:08 -05004380
Chris Masonce9adaa2008-04-09 16:28:12 -04004381int extent_range_uptodate(struct extent_io_tree *tree,
4382 u64 start, u64 end)
4383{
4384 struct page *page;
4385 int ret;
4386 int pg_uptodate = 1;
4387 int uptodate;
4388 unsigned long index;
4389
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004390 if (range_straddles_pages(start, end - start + 1)) {
Chris Mason19b6caf2011-07-25 06:50:50 -04004391 ret = test_range_bit(tree, start, end,
4392 EXTENT_UPTODATE, 1, NULL);
4393 if (ret)
4394 return 1;
4395 }
Chris Masond3977122009-01-05 21:25:51 -05004396 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004397 index = start >> PAGE_CACHE_SHIFT;
4398 page = find_get_page(tree->mapping, index);
Mitch Harder8bedd512012-01-26 15:01:11 -05004399 if (!page)
4400 return 1;
Chris Masonce9adaa2008-04-09 16:28:12 -04004401 uptodate = PageUptodate(page);
4402 page_cache_release(page);
4403 if (!uptodate) {
4404 pg_uptodate = 0;
4405 break;
4406 }
4407 start += PAGE_CACHE_SIZE;
4408 }
4409 return pg_uptodate;
4410}
4411
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004412int extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004413{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004414 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004415}
Chris Masond1310b22008-01-24 16:13:08 -05004416
4417int read_extent_buffer_pages(struct extent_io_tree *tree,
Arne Jansenbb82ab82011-06-10 14:06:53 +02004418 struct extent_buffer *eb, u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04004419 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05004420{
4421 unsigned long i;
4422 unsigned long start_i;
4423 struct page *page;
4424 int err;
4425 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04004426 int locked_pages = 0;
4427 int all_uptodate = 1;
Chris Masond1310b22008-01-24 16:13:08 -05004428 unsigned long num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04004429 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004430 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004431 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004432
Chris Masonb4ce94d2009-02-04 09:25:08 -05004433 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05004434 return 0;
4435
Chris Masond1310b22008-01-24 16:13:08 -05004436 if (start) {
4437 WARN_ON(start < eb->start);
4438 start_i = (start >> PAGE_CACHE_SHIFT) -
4439 (eb->start >> PAGE_CACHE_SHIFT);
4440 } else {
4441 start_i = 0;
4442 }
4443
4444 num_pages = num_extent_pages(eb->start, eb->len);
4445 for (i = start_i; i < num_pages; i++) {
4446 page = extent_buffer_page(eb, i);
Arne Jansenbb82ab82011-06-10 14:06:53 +02004447 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04004448 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04004449 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05004450 } else {
4451 lock_page(page);
4452 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004453 locked_pages++;
Chris Mason727011e2010-08-06 13:21:20 -04004454 if (!PageUptodate(page)) {
4455 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04004456 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04004457 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004458 }
4459 if (all_uptodate) {
4460 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004461 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04004462 goto unlock_exit;
4463 }
4464
Josef Bacikea466792012-03-26 21:57:36 -04004465 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
Josef Bacik5cf1ab52012-04-16 09:42:26 -04004466 eb->read_mirror = 0;
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004467 atomic_set(&eb->io_pages, num_reads);
Chris Masonce9adaa2008-04-09 16:28:12 -04004468 for (i = start_i; i < num_pages; i++) {
4469 page = extent_buffer_page(eb, i);
Chris Masonce9adaa2008-04-09 16:28:12 -04004470 if (!PageUptodate(page)) {
Chris Masonf1885912008-04-09 16:28:12 -04004471 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05004472 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04004473 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04004474 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05004475 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05004476 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05004477 } else {
4478 unlock_page(page);
4479 }
4480 }
4481
Jeff Mahoney355808c2011-10-03 23:23:14 -04004482 if (bio) {
4483 err = submit_one_bio(READ, bio, mirror_num, bio_flags);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004484 if (err)
4485 return err;
Jeff Mahoney355808c2011-10-03 23:23:14 -04004486 }
Chris Masona86c12c2008-02-07 10:50:54 -05004487
Arne Jansenbb82ab82011-06-10 14:06:53 +02004488 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05004489 return ret;
Chris Masond3977122009-01-05 21:25:51 -05004490
Chris Masond1310b22008-01-24 16:13:08 -05004491 for (i = start_i; i < num_pages; i++) {
4492 page = extent_buffer_page(eb, i);
4493 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05004494 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05004495 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05004496 }
Chris Masond3977122009-01-05 21:25:51 -05004497
Chris Masond1310b22008-01-24 16:13:08 -05004498 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04004499
4500unlock_exit:
4501 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05004502 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004503 page = extent_buffer_page(eb, i);
4504 i++;
4505 unlock_page(page);
4506 locked_pages--;
4507 }
4508 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004509}
Chris Masond1310b22008-01-24 16:13:08 -05004510
4511void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4512 unsigned long start,
4513 unsigned long len)
4514{
4515 size_t cur;
4516 size_t offset;
4517 struct page *page;
4518 char *kaddr;
4519 char *dst = (char *)dstv;
4520 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4521 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004522
4523 WARN_ON(start > eb->len);
4524 WARN_ON(start + len > eb->start + eb->len);
4525
4526 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4527
Chris Masond3977122009-01-05 21:25:51 -05004528 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004529 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004530
4531 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04004532 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004533 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004534
4535 dst += cur;
4536 len -= cur;
4537 offset = 0;
4538 i++;
4539 }
4540}
Chris Masond1310b22008-01-24 16:13:08 -05004541
4542int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04004543 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05004544 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04004545 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05004546{
4547 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4548 char *kaddr;
4549 struct page *p;
4550 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4551 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4552 unsigned long end_i = (start_offset + start + min_len - 1) >>
4553 PAGE_CACHE_SHIFT;
4554
4555 if (i != end_i)
4556 return -EINVAL;
4557
4558 if (i == 0) {
4559 offset = start_offset;
4560 *map_start = 0;
4561 } else {
4562 offset = 0;
4563 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4564 }
Chris Masond3977122009-01-05 21:25:51 -05004565
Chris Masond1310b22008-01-24 16:13:08 -05004566 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05004567 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
4568 "wanted %lu %lu\n", (unsigned long long)eb->start,
4569 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05004570 WARN_ON(1);
Josef Bacik850265332011-03-15 14:52:12 -04004571 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05004572 }
4573
4574 p = extent_buffer_page(eb, i);
Chris Masona6591712011-07-19 12:04:14 -04004575 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05004576 *map = kaddr + offset;
4577 *map_len = PAGE_CACHE_SIZE - offset;
4578 return 0;
4579}
Chris Masond1310b22008-01-24 16:13:08 -05004580
Chris Masond1310b22008-01-24 16:13:08 -05004581int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4582 unsigned long start,
4583 unsigned long len)
4584{
4585 size_t cur;
4586 size_t offset;
4587 struct page *page;
4588 char *kaddr;
4589 char *ptr = (char *)ptrv;
4590 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4591 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4592 int ret = 0;
4593
4594 WARN_ON(start > eb->len);
4595 WARN_ON(start + len > eb->start + eb->len);
4596
4597 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4598
Chris Masond3977122009-01-05 21:25:51 -05004599 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004600 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004601
4602 cur = min(len, (PAGE_CACHE_SIZE - offset));
4603
Chris Masona6591712011-07-19 12:04:14 -04004604 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004605 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004606 if (ret)
4607 break;
4608
4609 ptr += cur;
4610 len -= cur;
4611 offset = 0;
4612 i++;
4613 }
4614 return ret;
4615}
Chris Masond1310b22008-01-24 16:13:08 -05004616
4617void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4618 unsigned long start, unsigned long len)
4619{
4620 size_t cur;
4621 size_t offset;
4622 struct page *page;
4623 char *kaddr;
4624 char *src = (char *)srcv;
4625 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4626 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4627
4628 WARN_ON(start > eb->len);
4629 WARN_ON(start + len > eb->start + eb->len);
4630
4631 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4632
Chris Masond3977122009-01-05 21:25:51 -05004633 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004634 page = extent_buffer_page(eb, i);
4635 WARN_ON(!PageUptodate(page));
4636
4637 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004638 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004639 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004640
4641 src += cur;
4642 len -= cur;
4643 offset = 0;
4644 i++;
4645 }
4646}
Chris Masond1310b22008-01-24 16:13:08 -05004647
4648void memset_extent_buffer(struct extent_buffer *eb, char c,
4649 unsigned long start, unsigned long len)
4650{
4651 size_t cur;
4652 size_t offset;
4653 struct page *page;
4654 char *kaddr;
4655 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4656 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4657
4658 WARN_ON(start > eb->len);
4659 WARN_ON(start + len > eb->start + eb->len);
4660
4661 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4662
Chris Masond3977122009-01-05 21:25:51 -05004663 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004664 page = extent_buffer_page(eb, i);
4665 WARN_ON(!PageUptodate(page));
4666
4667 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004668 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004669 memset(kaddr + offset, c, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004670
4671 len -= cur;
4672 offset = 0;
4673 i++;
4674 }
4675}
Chris Masond1310b22008-01-24 16:13:08 -05004676
4677void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4678 unsigned long dst_offset, unsigned long src_offset,
4679 unsigned long len)
4680{
4681 u64 dst_len = dst->len;
4682 size_t cur;
4683 size_t offset;
4684 struct page *page;
4685 char *kaddr;
4686 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4687 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4688
4689 WARN_ON(src->len != dst_len);
4690
4691 offset = (start_offset + dst_offset) &
4692 ((unsigned long)PAGE_CACHE_SIZE - 1);
4693
Chris Masond3977122009-01-05 21:25:51 -05004694 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004695 page = extent_buffer_page(dst, i);
4696 WARN_ON(!PageUptodate(page));
4697
4698 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4699
Chris Masona6591712011-07-19 12:04:14 -04004700 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004701 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004702
4703 src_offset += cur;
4704 len -= cur;
4705 offset = 0;
4706 i++;
4707 }
4708}
Chris Masond1310b22008-01-24 16:13:08 -05004709
4710static void move_pages(struct page *dst_page, struct page *src_page,
4711 unsigned long dst_off, unsigned long src_off,
4712 unsigned long len)
4713{
Chris Masona6591712011-07-19 12:04:14 -04004714 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004715 if (dst_page == src_page) {
4716 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4717 } else {
Chris Masona6591712011-07-19 12:04:14 -04004718 char *src_kaddr = page_address(src_page);
Chris Masond1310b22008-01-24 16:13:08 -05004719 char *p = dst_kaddr + dst_off + len;
4720 char *s = src_kaddr + src_off + len;
4721
4722 while (len--)
4723 *--p = *--s;
Chris Masond1310b22008-01-24 16:13:08 -05004724 }
Chris Masond1310b22008-01-24 16:13:08 -05004725}
4726
Sergei Trofimovich33872062011-04-11 21:52:52 +00004727static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4728{
4729 unsigned long distance = (src > dst) ? src - dst : dst - src;
4730 return distance < len;
4731}
4732
Chris Masond1310b22008-01-24 16:13:08 -05004733static void copy_pages(struct page *dst_page, struct page *src_page,
4734 unsigned long dst_off, unsigned long src_off,
4735 unsigned long len)
4736{
Chris Masona6591712011-07-19 12:04:14 -04004737 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004738 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004739 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004740
Sergei Trofimovich33872062011-04-11 21:52:52 +00004741 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04004742 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00004743 } else {
Chris Masond1310b22008-01-24 16:13:08 -05004744 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004745 if (areas_overlap(src_off, dst_off, len))
4746 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00004747 }
Chris Masond1310b22008-01-24 16:13:08 -05004748
Chris Mason727011e2010-08-06 13:21:20 -04004749 if (must_memmove)
4750 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
4751 else
4752 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05004753}
4754
4755void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4756 unsigned long src_offset, unsigned long len)
4757{
4758 size_t cur;
4759 size_t dst_off_in_page;
4760 size_t src_off_in_page;
4761 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4762 unsigned long dst_i;
4763 unsigned long src_i;
4764
4765 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004766 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4767 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004768 BUG_ON(1);
4769 }
4770 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004771 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4772 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004773 BUG_ON(1);
4774 }
4775
Chris Masond3977122009-01-05 21:25:51 -05004776 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004777 dst_off_in_page = (start_offset + dst_offset) &
4778 ((unsigned long)PAGE_CACHE_SIZE - 1);
4779 src_off_in_page = (start_offset + src_offset) &
4780 ((unsigned long)PAGE_CACHE_SIZE - 1);
4781
4782 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4783 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4784
4785 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4786 src_off_in_page));
4787 cur = min_t(unsigned long, cur,
4788 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4789
4790 copy_pages(extent_buffer_page(dst, dst_i),
4791 extent_buffer_page(dst, src_i),
4792 dst_off_in_page, src_off_in_page, cur);
4793
4794 src_offset += cur;
4795 dst_offset += cur;
4796 len -= cur;
4797 }
4798}
Chris Masond1310b22008-01-24 16:13:08 -05004799
4800void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4801 unsigned long src_offset, unsigned long len)
4802{
4803 size_t cur;
4804 size_t dst_off_in_page;
4805 size_t src_off_in_page;
4806 unsigned long dst_end = dst_offset + len - 1;
4807 unsigned long src_end = src_offset + len - 1;
4808 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4809 unsigned long dst_i;
4810 unsigned long src_i;
4811
4812 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004813 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4814 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004815 BUG_ON(1);
4816 }
4817 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004818 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4819 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004820 BUG_ON(1);
4821 }
Chris Mason727011e2010-08-06 13:21:20 -04004822 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05004823 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4824 return;
4825 }
Chris Masond3977122009-01-05 21:25:51 -05004826 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004827 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
4828 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
4829
4830 dst_off_in_page = (start_offset + dst_end) &
4831 ((unsigned long)PAGE_CACHE_SIZE - 1);
4832 src_off_in_page = (start_offset + src_end) &
4833 ((unsigned long)PAGE_CACHE_SIZE - 1);
4834
4835 cur = min_t(unsigned long, len, src_off_in_page + 1);
4836 cur = min(cur, dst_off_in_page + 1);
4837 move_pages(extent_buffer_page(dst, dst_i),
4838 extent_buffer_page(dst, src_i),
4839 dst_off_in_page - cur + 1,
4840 src_off_in_page - cur + 1, cur);
4841
4842 dst_end -= cur;
4843 src_end -= cur;
4844 len -= cur;
4845 }
4846}
Chris Mason6af118ce2008-07-22 11:18:07 -04004847
Josef Bacik3083ee22012-03-09 16:01:49 -05004848int try_release_extent_buffer(struct page *page, gfp_t mask)
Miao Xie19fe0a82010-10-26 20:57:29 -04004849{
Chris Mason6af118ce2008-07-22 11:18:07 -04004850 struct extent_buffer *eb;
Miao Xie897ca6e2010-10-26 20:57:29 -04004851
Miao Xie19fe0a82010-10-26 20:57:29 -04004852 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05004853 * We need to make sure noboody is attaching this page to an eb right
4854 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04004855 */
Josef Bacik3083ee22012-03-09 16:01:49 -05004856 spin_lock(&page->mapping->private_lock);
4857 if (!PagePrivate(page)) {
4858 spin_unlock(&page->mapping->private_lock);
4859 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004860 }
4861
Josef Bacik3083ee22012-03-09 16:01:49 -05004862 eb = (struct extent_buffer *)page->private;
4863 BUG_ON(!eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04004864
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004865 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05004866 * This is a little awful but should be ok, we need to make sure that
4867 * the eb doesn't disappear out from under us while we're looking at
4868 * this page.
4869 */
4870 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004871 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05004872 spin_unlock(&eb->refs_lock);
4873 spin_unlock(&page->mapping->private_lock);
4874 return 0;
4875 }
4876 spin_unlock(&page->mapping->private_lock);
4877
4878 if ((mask & GFP_NOFS) == GFP_NOFS)
4879 mask = GFP_NOFS;
4880
4881 /*
4882 * If tree ref isn't set then we know the ref on this eb is a real ref,
4883 * so just return, this page will likely be freed soon anyway.
4884 */
4885 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
4886 spin_unlock(&eb->refs_lock);
4887 return 0;
4888 }
4889 release_extent_buffer(eb, mask);
4890
4891 return 1;
Chris Mason6af118ce2008-07-22 11:18:07 -04004892}