blob: c1b898d590d70e957c20c4d594f8045340af2bd4 [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);
58
Chris Masond1310b22008-01-24 16:13:08 -050059int __init extent_io_init(void)
60{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020061 extent_state_cache = kmem_cache_create("extent_state",
62 sizeof(struct extent_state), 0,
63 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050064 if (!extent_state_cache)
65 return -ENOMEM;
66
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020067 extent_buffer_cache = kmem_cache_create("extent_buffers",
68 sizeof(struct extent_buffer), 0,
69 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050070 if (!extent_buffer_cache)
71 goto free_state_cache;
72 return 0;
73
74free_state_cache:
75 kmem_cache_destroy(extent_state_cache);
76 return -ENOMEM;
77}
78
79void extent_io_exit(void)
80{
81 struct extent_state *state;
Chris Mason2d2ae542008-03-26 16:24:23 -040082 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -050083
84 while (!list_empty(&states)) {
Chris Mason2d2ae542008-03-26 16:24:23 -040085 state = list_entry(states.next, struct extent_state, leak_list);
Chris Masond3977122009-01-05 21:25:51 -050086 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
87 "state %lu in tree %p refs %d\n",
88 (unsigned long long)state->start,
89 (unsigned long long)state->end,
90 state->state, state->tree, atomic_read(&state->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040091 list_del(&state->leak_list);
Chris Masond1310b22008-01-24 16:13:08 -050092 kmem_cache_free(extent_state_cache, state);
93
94 }
95
Chris Mason2d2ae542008-03-26 16:24:23 -040096 while (!list_empty(&buffers)) {
97 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
Chris Masond3977122009-01-05 21:25:51 -050098 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
99 "refs %d\n", (unsigned long long)eb->start,
100 eb->len, atomic_read(&eb->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -0400101 list_del(&eb->leak_list);
102 kmem_cache_free(extent_buffer_cache, eb);
103 }
Chris Masond1310b22008-01-24 16:13:08 -0500104 if (extent_state_cache)
105 kmem_cache_destroy(extent_state_cache);
106 if (extent_buffer_cache)
107 kmem_cache_destroy(extent_buffer_cache);
108}
109
110void extent_io_tree_init(struct extent_io_tree *tree,
David Sterbaf993c882011-04-20 23:35:57 +0200111 struct address_space *mapping)
Chris Masond1310b22008-01-24 16:13:08 -0500112{
Eric Paris6bef4d32010-02-23 19:43:04 +0000113 tree->state = RB_ROOT;
Miao Xie19fe0a82010-10-26 20:57:29 -0400114 INIT_RADIX_TREE(&tree->buffer, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500115 tree->ops = NULL;
116 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500117 spin_lock_init(&tree->lock);
Chris Mason6af118ce2008-07-22 11:18:07 -0400118 spin_lock_init(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -0500119 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500120}
Chris Masond1310b22008-01-24 16:13:08 -0500121
Christoph Hellwigb2950862008-12-02 09:54:17 -0500122static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500123{
124 struct extent_state *state;
Chris Mason39351272009-02-04 09:24:05 -0500125#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400126 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400127#endif
Chris Masond1310b22008-01-24 16:13:08 -0500128
129 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400130 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500131 return state;
132 state->state = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500133 state->private = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500134 state->tree = NULL;
Chris Mason39351272009-02-04 09:24:05 -0500135#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400136 spin_lock_irqsave(&leak_lock, flags);
137 list_add(&state->leak_list, &states);
138 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400139#endif
Chris Masond1310b22008-01-24 16:13:08 -0500140 atomic_set(&state->refs, 1);
141 init_waitqueue_head(&state->wq);
142 return state;
143}
Chris Masond1310b22008-01-24 16:13:08 -0500144
Chris Mason4845e442010-05-25 20:56:50 -0400145void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500146{
Chris Masond1310b22008-01-24 16:13:08 -0500147 if (!state)
148 return;
149 if (atomic_dec_and_test(&state->refs)) {
Chris Mason39351272009-02-04 09:24:05 -0500150#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400151 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400152#endif
Chris Mason70dec802008-01-29 09:59:12 -0500153 WARN_ON(state->tree);
Chris Mason39351272009-02-04 09:24:05 -0500154#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400155 spin_lock_irqsave(&leak_lock, flags);
156 list_del(&state->leak_list);
157 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400158#endif
Chris Masond1310b22008-01-24 16:13:08 -0500159 kmem_cache_free(extent_state_cache, state);
160 }
161}
Chris Masond1310b22008-01-24 16:13:08 -0500162
163static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
164 struct rb_node *node)
165{
Chris Masond3977122009-01-05 21:25:51 -0500166 struct rb_node **p = &root->rb_node;
167 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500168 struct tree_entry *entry;
169
Chris Masond3977122009-01-05 21:25:51 -0500170 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500171 parent = *p;
172 entry = rb_entry(parent, struct tree_entry, rb_node);
173
174 if (offset < entry->start)
175 p = &(*p)->rb_left;
176 else if (offset > entry->end)
177 p = &(*p)->rb_right;
178 else
179 return parent;
180 }
181
182 entry = rb_entry(node, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500183 rb_link_node(node, parent, p);
184 rb_insert_color(node, root);
185 return NULL;
186}
187
Chris Mason80ea96b2008-02-01 14:51:59 -0500188static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Chris Masond1310b22008-01-24 16:13:08 -0500189 struct rb_node **prev_ret,
190 struct rb_node **next_ret)
191{
Chris Mason80ea96b2008-02-01 14:51:59 -0500192 struct rb_root *root = &tree->state;
Chris Masond3977122009-01-05 21:25:51 -0500193 struct rb_node *n = root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500194 struct rb_node *prev = NULL;
195 struct rb_node *orig_prev = NULL;
196 struct tree_entry *entry;
197 struct tree_entry *prev_entry = NULL;
198
Chris Masond3977122009-01-05 21:25:51 -0500199 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500200 entry = rb_entry(n, struct tree_entry, rb_node);
201 prev = n;
202 prev_entry = entry;
203
204 if (offset < entry->start)
205 n = n->rb_left;
206 else if (offset > entry->end)
207 n = n->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500208 else
Chris Masond1310b22008-01-24 16:13:08 -0500209 return n;
210 }
211
212 if (prev_ret) {
213 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500214 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500215 prev = rb_next(prev);
216 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
217 }
218 *prev_ret = prev;
219 prev = orig_prev;
220 }
221
222 if (next_ret) {
223 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500224 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500225 prev = rb_prev(prev);
226 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
227 }
228 *next_ret = prev;
229 }
230 return NULL;
231}
232
Chris Mason80ea96b2008-02-01 14:51:59 -0500233static inline struct rb_node *tree_search(struct extent_io_tree *tree,
234 u64 offset)
Chris Masond1310b22008-01-24 16:13:08 -0500235{
Chris Mason70dec802008-01-29 09:59:12 -0500236 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500237 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500238
Chris Mason80ea96b2008-02-01 14:51:59 -0500239 ret = __etree_search(tree, offset, &prev, NULL);
Chris Masond3977122009-01-05 21:25:51 -0500240 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500241 return prev;
242 return ret;
243}
244
Josef Bacik9ed74f22009-09-11 16:12:44 -0400245static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
246 struct extent_state *other)
247{
248 if (tree->ops && tree->ops->merge_extent_hook)
249 tree->ops->merge_extent_hook(tree->mapping->host, new,
250 other);
251}
252
Chris Masond1310b22008-01-24 16:13:08 -0500253/*
254 * utility function to look for merge candidates inside a given range.
255 * Any extents with matching state are merged together into a single
256 * extent in the tree. Extents with EXTENT_IO in their state field
257 * are not merged because the end_io handlers need to be able to do
258 * operations on them without sleeping (or doing allocations/splits).
259 *
260 * This should be called with the tree lock held.
261 */
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000262static void merge_state(struct extent_io_tree *tree,
263 struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500264{
265 struct extent_state *other;
266 struct rb_node *other_node;
267
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400268 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000269 return;
Chris Masond1310b22008-01-24 16:13:08 -0500270
271 other_node = rb_prev(&state->rb_node);
272 if (other_node) {
273 other = rb_entry(other_node, struct extent_state, rb_node);
274 if (other->end == state->start - 1 &&
275 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400276 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500277 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500278 other->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500279 rb_erase(&other->rb_node, &tree->state);
280 free_extent_state(other);
281 }
282 }
283 other_node = rb_next(&state->rb_node);
284 if (other_node) {
285 other = rb_entry(other_node, struct extent_state, rb_node);
286 if (other->start == state->end + 1 &&
287 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400288 merge_cb(tree, state, other);
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400289 state->end = other->end;
290 other->tree = NULL;
291 rb_erase(&other->rb_node, &tree->state);
292 free_extent_state(other);
Chris Masond1310b22008-01-24 16:13:08 -0500293 }
294 }
Chris Masond1310b22008-01-24 16:13:08 -0500295}
296
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000297static void set_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400298 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500299{
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000300 if (tree->ops && tree->ops->set_bit_hook)
301 tree->ops->set_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500302}
303
304static void clear_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400305 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500306{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400307 if (tree->ops && tree->ops->clear_bit_hook)
308 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500309}
310
Xiao Guangrong3150b692011-07-14 03:19:08 +0000311static void set_state_bits(struct extent_io_tree *tree,
312 struct extent_state *state, int *bits);
313
Chris Masond1310b22008-01-24 16:13:08 -0500314/*
315 * insert an extent_state struct into the tree. 'bits' are set on the
316 * struct before it is inserted.
317 *
318 * This may return -EEXIST if the extent is already there, in which case the
319 * state struct is freed.
320 *
321 * The tree lock is not taken internally. This is a utility function and
322 * probably isn't what you want to call (see set/clear_extent_bit).
323 */
324static int insert_state(struct extent_io_tree *tree,
325 struct extent_state *state, u64 start, u64 end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400326 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500327{
328 struct rb_node *node;
329
330 if (end < start) {
Chris Masond3977122009-01-05 21:25:51 -0500331 printk(KERN_ERR "btrfs end < start %llu %llu\n",
332 (unsigned long long)end,
333 (unsigned long long)start);
Chris Masond1310b22008-01-24 16:13:08 -0500334 WARN_ON(1);
335 }
Chris Masond1310b22008-01-24 16:13:08 -0500336 state->start = start;
337 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400338
Xiao Guangrong3150b692011-07-14 03:19:08 +0000339 set_state_bits(tree, state, bits);
340
Chris Masond1310b22008-01-24 16:13:08 -0500341 node = tree_insert(&tree->state, end, &state->rb_node);
342 if (node) {
343 struct extent_state *found;
344 found = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500345 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
346 "%llu %llu\n", (unsigned long long)found->start,
347 (unsigned long long)found->end,
348 (unsigned long long)start, (unsigned long long)end);
Chris Masond1310b22008-01-24 16:13:08 -0500349 return -EEXIST;
350 }
Chris Mason70dec802008-01-29 09:59:12 -0500351 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500352 merge_state(tree, state);
353 return 0;
354}
355
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000356static void split_cb(struct extent_io_tree *tree, struct extent_state *orig,
Josef Bacik9ed74f22009-09-11 16:12:44 -0400357 u64 split)
358{
359 if (tree->ops && tree->ops->split_extent_hook)
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000360 tree->ops->split_extent_hook(tree->mapping->host, orig, split);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400361}
362
Chris Masond1310b22008-01-24 16:13:08 -0500363/*
364 * split a given extent state struct in two, inserting the preallocated
365 * struct 'prealloc' as the newly created second half. 'split' indicates an
366 * offset inside 'orig' where it should be split.
367 *
368 * Before calling,
369 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
370 * are two extent state structs in the tree:
371 * prealloc: [orig->start, split - 1]
372 * orig: [ split, orig->end ]
373 *
374 * The tree locks are not taken by this function. They need to be held
375 * by the caller.
376 */
377static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
378 struct extent_state *prealloc, u64 split)
379{
380 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400381
382 split_cb(tree, orig, split);
383
Chris Masond1310b22008-01-24 16:13:08 -0500384 prealloc->start = orig->start;
385 prealloc->end = split - 1;
386 prealloc->state = orig->state;
387 orig->start = split;
388
389 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
390 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500391 free_extent_state(prealloc);
392 return -EEXIST;
393 }
Chris Mason70dec802008-01-29 09:59:12 -0500394 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500395 return 0;
396}
397
398/*
399 * utility function to clear some bits in an extent state struct.
400 * it will optionally wake up any one waiting on this state (wake == 1), or
401 * forcibly remove the state from the tree (delete == 1).
402 *
403 * If no bits are set on the state struct after clearing things, the
404 * struct is freed and removed from the tree
405 */
406static int clear_state_bit(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400407 struct extent_state *state,
408 int *bits, int wake)
Chris Masond1310b22008-01-24 16:13:08 -0500409{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400410 int bits_to_clear = *bits & ~EXTENT_CTLBITS;
Josef Bacik32c00af2009-10-08 13:34:05 -0400411 int ret = state->state & bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500412
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400413 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500414 u64 range = state->end - state->start + 1;
415 WARN_ON(range > tree->dirty_bytes);
416 tree->dirty_bytes -= range;
417 }
Chris Mason291d6732008-01-29 15:55:23 -0500418 clear_state_cb(tree, state, bits);
Josef Bacik32c00af2009-10-08 13:34:05 -0400419 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500420 if (wake)
421 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400422 if (state->state == 0) {
Chris Mason70dec802008-01-29 09:59:12 -0500423 if (state->tree) {
Chris Masond1310b22008-01-24 16:13:08 -0500424 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500425 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500426 free_extent_state(state);
427 } else {
428 WARN_ON(1);
429 }
430 } else {
431 merge_state(tree, state);
432 }
433 return ret;
434}
435
Xiao Guangrong82337672011-04-20 06:44:57 +0000436static struct extent_state *
437alloc_extent_state_atomic(struct extent_state *prealloc)
438{
439 if (!prealloc)
440 prealloc = alloc_extent_state(GFP_ATOMIC);
441
442 return prealloc;
443}
444
Chris Masond1310b22008-01-24 16:13:08 -0500445/*
446 * clear some bits on a range in the tree. This may require splitting
447 * or inserting elements in the tree, so the gfp mask is used to
448 * indicate which allocations or sleeping are allowed.
449 *
450 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
451 * the given range from the tree regardless of state (ie for truncate).
452 *
453 * the range [start, end] is inclusive.
454 *
455 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
456 * bits were already set, or zero if none of the bits were already set.
457 */
458int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400459 int bits, int wake, int delete,
460 struct extent_state **cached_state,
461 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500462{
463 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400464 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500465 struct extent_state *prealloc = NULL;
Chris Mason2c64c532009-09-02 15:04:12 -0400466 struct rb_node *next_node;
Chris Masond1310b22008-01-24 16:13:08 -0500467 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400468 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500469 int err;
470 int set = 0;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000471 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500472
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400473 if (delete)
474 bits |= ~EXTENT_CTLBITS;
475 bits |= EXTENT_FIRST_DELALLOC;
476
Josef Bacik2ac55d42010-02-03 19:33:23 +0000477 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
478 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500479again:
480 if (!prealloc && (mask & __GFP_WAIT)) {
481 prealloc = alloc_extent_state(mask);
482 if (!prealloc)
483 return -ENOMEM;
484 }
485
Chris Masoncad321a2008-12-17 14:51:42 -0500486 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400487 if (cached_state) {
488 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000489
490 if (clear) {
491 *cached_state = NULL;
492 cached_state = NULL;
493 }
494
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400495 if (cached && cached->tree && cached->start <= start &&
496 cached->end > start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000497 if (clear)
498 atomic_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400499 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400500 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400501 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000502 if (clear)
503 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400504 }
Chris Masond1310b22008-01-24 16:13:08 -0500505 /*
506 * this search will find the extents that end after
507 * our range starts
508 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500509 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500510 if (!node)
511 goto out;
512 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400513hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500514 if (state->start > end)
515 goto out;
516 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400517 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500518
Liu Bo04493142012-02-16 18:34:37 +0800519 if (state->end < end && !need_resched())
520 next_node = rb_next(&state->rb_node);
521 else
522 next_node = NULL;
523
524 /* the state doesn't have the wanted bits, go ahead */
525 if (!(state->state & bits))
526 goto next;
527
Chris Masond1310b22008-01-24 16:13:08 -0500528 /*
529 * | ---- desired range ---- |
530 * | state | or
531 * | ------------- state -------------- |
532 *
533 * We need to split the extent we found, and may flip
534 * bits on second half.
535 *
536 * If the extent we found extends past our range, we
537 * just split and search again. It'll get split again
538 * the next time though.
539 *
540 * If the extent we found is inside our range, we clear
541 * the desired bit on it.
542 */
543
544 if (state->start < start) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000545 prealloc = alloc_extent_state_atomic(prealloc);
546 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500547 err = split_state(tree, state, prealloc, start);
548 BUG_ON(err == -EEXIST);
549 prealloc = NULL;
550 if (err)
551 goto out;
552 if (state->end <= end) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400553 set |= clear_state_bit(tree, state, &bits, wake);
Yan Zheng5c939df2009-05-27 09:16:03 -0400554 if (last_end == (u64)-1)
555 goto out;
556 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500557 }
558 goto search_again;
559 }
560 /*
561 * | ---- desired range ---- |
562 * | state |
563 * We need to split the extent, and clear the bit
564 * on the first half
565 */
566 if (state->start <= end && state->end > end) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000567 prealloc = alloc_extent_state_atomic(prealloc);
568 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500569 err = split_state(tree, state, prealloc, end + 1);
570 BUG_ON(err == -EEXIST);
Chris Masond1310b22008-01-24 16:13:08 -0500571 if (wake)
572 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400573
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400574 set |= clear_state_bit(tree, prealloc, &bits, wake);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400575
Chris Masond1310b22008-01-24 16:13:08 -0500576 prealloc = NULL;
577 goto out;
578 }
Chris Mason42daec22009-09-23 19:51:09 -0400579
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400580 set |= clear_state_bit(tree, state, &bits, wake);
Liu Bo04493142012-02-16 18:34:37 +0800581next:
Yan Zheng5c939df2009-05-27 09:16:03 -0400582 if (last_end == (u64)-1)
583 goto out;
584 start = last_end + 1;
Chris Mason2c64c532009-09-02 15:04:12 -0400585 if (start <= end && next_node) {
586 state = rb_entry(next_node, struct extent_state,
587 rb_node);
Liu Bo692e5752012-02-16 18:34:36 +0800588 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400589 }
Chris Masond1310b22008-01-24 16:13:08 -0500590 goto search_again;
591
592out:
Chris Masoncad321a2008-12-17 14:51:42 -0500593 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500594 if (prealloc)
595 free_extent_state(prealloc);
596
597 return set;
598
599search_again:
600 if (start > end)
601 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500602 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500603 if (mask & __GFP_WAIT)
604 cond_resched();
605 goto again;
606}
Chris Masond1310b22008-01-24 16:13:08 -0500607
608static int wait_on_state(struct extent_io_tree *tree,
609 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500610 __releases(tree->lock)
611 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500612{
613 DEFINE_WAIT(wait);
614 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500615 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500616 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500617 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500618 finish_wait(&state->wq, &wait);
619 return 0;
620}
621
622/*
623 * waits for one or more bits to clear on a range in the state tree.
624 * The range [start, end] is inclusive.
625 * The tree lock is taken by this function
626 */
627int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
628{
629 struct extent_state *state;
630 struct rb_node *node;
631
Chris Masoncad321a2008-12-17 14:51:42 -0500632 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500633again:
634 while (1) {
635 /*
636 * this search will find all the extents that end after
637 * our range starts
638 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500639 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500640 if (!node)
641 break;
642
643 state = rb_entry(node, struct extent_state, rb_node);
644
645 if (state->start > end)
646 goto out;
647
648 if (state->state & bits) {
649 start = state->start;
650 atomic_inc(&state->refs);
651 wait_on_state(tree, state);
652 free_extent_state(state);
653 goto again;
654 }
655 start = state->end + 1;
656
657 if (start > end)
658 break;
659
Xiao Guangrongded91f02011-07-14 03:19:27 +0000660 cond_resched_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500661 }
662out:
Chris Masoncad321a2008-12-17 14:51:42 -0500663 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500664 return 0;
665}
Chris Masond1310b22008-01-24 16:13:08 -0500666
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000667static void set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500668 struct extent_state *state,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400669 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500670{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400671 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400672
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000673 set_state_cb(tree, state, bits);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400674 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500675 u64 range = state->end - state->start + 1;
676 tree->dirty_bytes += range;
677 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400678 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500679}
680
Chris Mason2c64c532009-09-02 15:04:12 -0400681static void cache_state(struct extent_state *state,
682 struct extent_state **cached_ptr)
683{
684 if (cached_ptr && !(*cached_ptr)) {
685 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
686 *cached_ptr = state;
687 atomic_inc(&state->refs);
688 }
689 }
690}
691
Arne Jansen507903b2011-04-06 10:02:20 +0000692static void uncache_state(struct extent_state **cached_ptr)
693{
694 if (cached_ptr && (*cached_ptr)) {
695 struct extent_state *state = *cached_ptr;
Chris Mason109b36a2011-04-12 13:57:39 -0400696 *cached_ptr = NULL;
697 free_extent_state(state);
Arne Jansen507903b2011-04-06 10:02:20 +0000698 }
699}
700
Chris Masond1310b22008-01-24 16:13:08 -0500701/*
Chris Mason1edbb732009-09-02 13:24:36 -0400702 * set some bits on a range in the tree. This may require allocations or
703 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500704 *
Chris Mason1edbb732009-09-02 13:24:36 -0400705 * If any of the exclusive bits are set, this will fail with -EEXIST if some
706 * part of the range already has the desired bits set. The start of the
707 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500708 *
Chris Mason1edbb732009-09-02 13:24:36 -0400709 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500710 */
Chris Mason1edbb732009-09-02 13:24:36 -0400711
Chris Mason4845e442010-05-25 20:56:50 -0400712int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
713 int bits, int exclusive_bits, u64 *failed_start,
714 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500715{
716 struct extent_state *state;
717 struct extent_state *prealloc = NULL;
718 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500719 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500720 u64 last_start;
721 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400722
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400723 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500724again:
725 if (!prealloc && (mask & __GFP_WAIT)) {
726 prealloc = alloc_extent_state(mask);
Xiao Guangrong82337672011-04-20 06:44:57 +0000727 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500728 }
729
Chris Masoncad321a2008-12-17 14:51:42 -0500730 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400731 if (cached_state && *cached_state) {
732 state = *cached_state;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400733 if (state->start <= start && state->end > start &&
734 state->tree) {
Chris Mason9655d292009-09-02 15:22:30 -0400735 node = &state->rb_node;
736 goto hit_next;
737 }
738 }
Chris Masond1310b22008-01-24 16:13:08 -0500739 /*
740 * this search will find all the extents that end after
741 * our range starts.
742 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500743 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500744 if (!node) {
Xiao Guangrong82337672011-04-20 06:44:57 +0000745 prealloc = alloc_extent_state_atomic(prealloc);
746 BUG_ON(!prealloc);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400747 err = insert_state(tree, prealloc, start, end, &bits);
Chris Masond1310b22008-01-24 16:13:08 -0500748 prealloc = NULL;
749 BUG_ON(err == -EEXIST);
750 goto out;
751 }
Chris Masond1310b22008-01-24 16:13:08 -0500752 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400753hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500754 last_start = state->start;
755 last_end = state->end;
756
757 /*
758 * | ---- desired range ---- |
759 * | state |
760 *
761 * Just lock what we found and keep going
762 */
763 if (state->start == start && state->end <= end) {
Chris Mason40431d62009-08-05 12:57:59 -0400764 struct rb_node *next_node;
Chris Mason1edbb732009-09-02 13:24:36 -0400765 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500766 *failed_start = state->start;
767 err = -EEXIST;
768 goto out;
769 }
Chris Mason42daec22009-09-23 19:51:09 -0400770
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000771 set_state_bits(tree, state, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400772
Chris Mason2c64c532009-09-02 15:04:12 -0400773 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500774 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400775 if (last_end == (u64)-1)
776 goto out;
Chris Mason40431d62009-08-05 12:57:59 -0400777
Yan Zheng5c939df2009-05-27 09:16:03 -0400778 start = last_end + 1;
Josef Bacikdf98b6e2011-06-20 14:53:48 -0400779 next_node = rb_next(&state->rb_node);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000780 if (next_node && start < end && prealloc && !need_resched()) {
781 state = rb_entry(next_node, struct extent_state,
782 rb_node);
783 if (state->start == start)
784 goto hit_next;
Chris Mason40431d62009-08-05 12:57:59 -0400785 }
Chris Masond1310b22008-01-24 16:13:08 -0500786 goto search_again;
787 }
788
789 /*
790 * | ---- desired range ---- |
791 * | state |
792 * or
793 * | ------------- state -------------- |
794 *
795 * We need to split the extent we found, and may flip bits on
796 * second half.
797 *
798 * If the extent we found extends past our
799 * range, we just split and search again. It'll get split
800 * again the next time though.
801 *
802 * If the extent we found is inside our range, we set the
803 * desired bit on it.
804 */
805 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400806 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500807 *failed_start = start;
808 err = -EEXIST;
809 goto out;
810 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000811
812 prealloc = alloc_extent_state_atomic(prealloc);
813 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500814 err = split_state(tree, state, prealloc, start);
815 BUG_ON(err == -EEXIST);
816 prealloc = NULL;
817 if (err)
818 goto out;
819 if (state->end <= end) {
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000820 set_state_bits(tree, state, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400821 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500822 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400823 if (last_end == (u64)-1)
824 goto out;
825 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500826 }
827 goto search_again;
828 }
829 /*
830 * | ---- desired range ---- |
831 * | state | or | state |
832 *
833 * There's a hole, we need to insert something in it and
834 * ignore the extent we found.
835 */
836 if (state->start > start) {
837 u64 this_end;
838 if (end < last_start)
839 this_end = end;
840 else
Chris Masond3977122009-01-05 21:25:51 -0500841 this_end = last_start - 1;
Xiao Guangrong82337672011-04-20 06:44:57 +0000842
843 prealloc = alloc_extent_state_atomic(prealloc);
844 BUG_ON(!prealloc);
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000845
846 /*
847 * Avoid to free 'prealloc' if it can be merged with
848 * the later extent.
849 */
Chris Masond1310b22008-01-24 16:13:08 -0500850 err = insert_state(tree, prealloc, start, this_end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400851 &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400852 BUG_ON(err == -EEXIST);
853 if (err) {
Xiao Guangrongc7f895a2011-04-20 06:45:49 +0000854 free_extent_state(prealloc);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400855 prealloc = NULL;
856 goto out;
857 }
Chris Mason2c64c532009-09-02 15:04:12 -0400858 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500859 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500860 start = this_end + 1;
861 goto search_again;
862 }
863 /*
864 * | ---- desired range ---- |
865 * | state |
866 * We need to split the extent, and set the bit
867 * on the first half
868 */
869 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400870 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500871 *failed_start = start;
872 err = -EEXIST;
873 goto out;
874 }
Xiao Guangrong82337672011-04-20 06:44:57 +0000875
876 prealloc = alloc_extent_state_atomic(prealloc);
877 BUG_ON(!prealloc);
Chris Masond1310b22008-01-24 16:13:08 -0500878 err = split_state(tree, state, prealloc, end + 1);
879 BUG_ON(err == -EEXIST);
880
Jeff Mahoney1bf85042011-07-21 16:56:09 +0000881 set_state_bits(tree, prealloc, &bits);
Chris Mason2c64c532009-09-02 15:04:12 -0400882 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500883 merge_state(tree, prealloc);
884 prealloc = NULL;
885 goto out;
886 }
887
888 goto search_again;
889
890out:
Chris Masoncad321a2008-12-17 14:51:42 -0500891 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500892 if (prealloc)
893 free_extent_state(prealloc);
894
895 return err;
896
897search_again:
898 if (start > end)
899 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500900 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500901 if (mask & __GFP_WAIT)
902 cond_resched();
903 goto again;
904}
Chris Masond1310b22008-01-24 16:13:08 -0500905
Josef Bacik462d6fa2011-09-26 13:56:12 -0400906/**
907 * convert_extent - convert all bits in a given range from one bit to another
908 * @tree: the io tree to search
909 * @start: the start offset in bytes
910 * @end: the end offset in bytes (inclusive)
911 * @bits: the bits to set in this range
912 * @clear_bits: the bits to clear in this range
913 * @mask: the allocation mask
914 *
915 * This will go through and set bits for the given range. If any states exist
916 * already in this range they are set with the given bit and cleared of the
917 * clear_bits. This is only meant to be used by things that are mergeable, ie
918 * converting from say DELALLOC to DIRTY. This is not meant to be used with
919 * boundary bits like LOCK.
920 */
921int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
922 int bits, int clear_bits, gfp_t mask)
923{
924 struct extent_state *state;
925 struct extent_state *prealloc = NULL;
926 struct rb_node *node;
927 int err = 0;
928 u64 last_start;
929 u64 last_end;
930
931again:
932 if (!prealloc && (mask & __GFP_WAIT)) {
933 prealloc = alloc_extent_state(mask);
934 if (!prealloc)
935 return -ENOMEM;
936 }
937
938 spin_lock(&tree->lock);
939 /*
940 * this search will find all the extents that end after
941 * our range starts.
942 */
943 node = tree_search(tree, start);
944 if (!node) {
945 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -0500946 if (!prealloc) {
947 err = -ENOMEM;
948 goto out;
949 }
Josef Bacik462d6fa2011-09-26 13:56:12 -0400950 err = insert_state(tree, prealloc, start, end, &bits);
951 prealloc = NULL;
952 BUG_ON(err == -EEXIST);
953 goto out;
954 }
955 state = rb_entry(node, struct extent_state, rb_node);
956hit_next:
957 last_start = state->start;
958 last_end = state->end;
959
960 /*
961 * | ---- desired range ---- |
962 * | state |
963 *
964 * Just lock what we found and keep going
965 */
966 if (state->start == start && state->end <= end) {
967 struct rb_node *next_node;
968
969 set_state_bits(tree, state, &bits);
970 clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -0400971 if (last_end == (u64)-1)
972 goto out;
973
974 start = last_end + 1;
975 next_node = rb_next(&state->rb_node);
976 if (next_node && start < end && prealloc && !need_resched()) {
977 state = rb_entry(next_node, struct extent_state,
978 rb_node);
979 if (state->start == start)
980 goto hit_next;
981 }
982 goto search_again;
983 }
984
985 /*
986 * | ---- desired range ---- |
987 * | state |
988 * or
989 * | ------------- state -------------- |
990 *
991 * We need to split the extent we found, and may flip bits on
992 * second half.
993 *
994 * If the extent we found extends past our
995 * range, we just split and search again. It'll get split
996 * again the next time though.
997 *
998 * If the extent we found is inside our range, we set the
999 * desired bit on it.
1000 */
1001 if (state->start < start) {
1002 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001003 if (!prealloc) {
1004 err = -ENOMEM;
1005 goto out;
1006 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001007 err = split_state(tree, state, prealloc, start);
1008 BUG_ON(err == -EEXIST);
1009 prealloc = NULL;
1010 if (err)
1011 goto out;
1012 if (state->end <= end) {
1013 set_state_bits(tree, state, &bits);
1014 clear_state_bit(tree, state, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001015 if (last_end == (u64)-1)
1016 goto out;
1017 start = last_end + 1;
1018 }
1019 goto search_again;
1020 }
1021 /*
1022 * | ---- desired range ---- |
1023 * | state | or | state |
1024 *
1025 * There's a hole, we need to insert something in it and
1026 * ignore the extent we found.
1027 */
1028 if (state->start > start) {
1029 u64 this_end;
1030 if (end < last_start)
1031 this_end = end;
1032 else
1033 this_end = last_start - 1;
1034
1035 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001036 if (!prealloc) {
1037 err = -ENOMEM;
1038 goto out;
1039 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001040
1041 /*
1042 * Avoid to free 'prealloc' if it can be merged with
1043 * the later extent.
1044 */
1045 err = insert_state(tree, prealloc, start, this_end,
1046 &bits);
1047 BUG_ON(err == -EEXIST);
1048 if (err) {
1049 free_extent_state(prealloc);
1050 prealloc = NULL;
1051 goto out;
1052 }
1053 prealloc = NULL;
1054 start = this_end + 1;
1055 goto search_again;
1056 }
1057 /*
1058 * | ---- desired range ---- |
1059 * | state |
1060 * We need to split the extent, and set the bit
1061 * on the first half
1062 */
1063 if (state->start <= end && state->end > end) {
1064 prealloc = alloc_extent_state_atomic(prealloc);
Liu Bo1cf4ffd2011-12-07 20:08:40 -05001065 if (!prealloc) {
1066 err = -ENOMEM;
1067 goto out;
1068 }
Josef Bacik462d6fa2011-09-26 13:56:12 -04001069
1070 err = split_state(tree, state, prealloc, end + 1);
1071 BUG_ON(err == -EEXIST);
1072
1073 set_state_bits(tree, prealloc, &bits);
1074 clear_state_bit(tree, prealloc, &clear_bits, 0);
Josef Bacik462d6fa2011-09-26 13:56:12 -04001075 prealloc = NULL;
1076 goto out;
1077 }
1078
1079 goto search_again;
1080
1081out:
1082 spin_unlock(&tree->lock);
1083 if (prealloc)
1084 free_extent_state(prealloc);
1085
1086 return err;
1087
1088search_again:
1089 if (start > end)
1090 goto out;
1091 spin_unlock(&tree->lock);
1092 if (mask & __GFP_WAIT)
1093 cond_resched();
1094 goto again;
1095}
1096
Chris Masond1310b22008-01-24 16:13:08 -05001097/* wrappers around set/clear extent bit */
1098int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1099 gfp_t mask)
1100{
1101 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001102 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001103}
Chris Masond1310b22008-01-24 16:13:08 -05001104
1105int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1106 int bits, gfp_t mask)
1107{
1108 return set_extent_bit(tree, start, end, bits, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001109 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001110}
Chris Masond1310b22008-01-24 16:13:08 -05001111
1112int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1113 int bits, gfp_t mask)
1114{
Chris Mason2c64c532009-09-02 15:04:12 -04001115 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001116}
Chris Masond1310b22008-01-24 16:13:08 -05001117
1118int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001119 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001120{
1121 return set_extent_bit(tree, start, end,
Liu Bofee187d2011-09-29 15:55:28 +08001122 EXTENT_DELALLOC | EXTENT_UPTODATE,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001123 0, NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001124}
Chris Masond1310b22008-01-24 16:13:08 -05001125
1126int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
1127 gfp_t mask)
1128{
1129 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04001130 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001131 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001132}
Chris Masond1310b22008-01-24 16:13:08 -05001133
1134int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
1135 gfp_t mask)
1136{
1137 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -04001138 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001139}
Chris Masond1310b22008-01-24 16:13:08 -05001140
Chris Masond1310b22008-01-24 16:13:08 -05001141int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
Arne Jansen507903b2011-04-06 10:02:20 +00001142 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001143{
Arne Jansen507903b2011-04-06 10:02:20 +00001144 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0,
1145 NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001146}
Chris Masond1310b22008-01-24 16:13:08 -05001147
Chris Masond3977122009-01-05 21:25:51 -05001148static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001149 u64 end, struct extent_state **cached_state,
1150 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001151{
Chris Mason2c64c532009-09-02 15:04:12 -04001152 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +00001153 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001154}
Chris Masond1310b22008-01-24 16:13:08 -05001155
Chris Masond352ac62008-09-29 15:18:18 -04001156/*
1157 * either insert or lock state struct between start and end use mask to tell
1158 * us if waiting is desired.
1159 */
Chris Mason1edbb732009-09-02 13:24:36 -04001160int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -04001161 int bits, struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001162{
1163 int err;
1164 u64 failed_start;
1165 while (1) {
Chris Mason1edbb732009-09-02 13:24:36 -04001166 err = set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
Chris Mason2c64c532009-09-02 15:04:12 -04001167 EXTENT_LOCKED, &failed_start,
1168 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001169 if (err == -EEXIST && (mask & __GFP_WAIT)) {
1170 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1171 start = failed_start;
1172 } else {
1173 break;
1174 }
1175 WARN_ON(start > end);
1176 }
1177 return err;
1178}
Chris Masond1310b22008-01-24 16:13:08 -05001179
Chris Mason1edbb732009-09-02 13:24:36 -04001180int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
1181{
Chris Mason2c64c532009-09-02 15:04:12 -04001182 return lock_extent_bits(tree, start, end, 0, NULL, mask);
Chris Mason1edbb732009-09-02 13:24:36 -04001183}
1184
Josef Bacik25179202008-10-29 14:49:05 -04001185int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1186 gfp_t mask)
1187{
1188 int err;
1189 u64 failed_start;
1190
Chris Mason2c64c532009-09-02 15:04:12 -04001191 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1192 &failed_start, NULL, mask);
Yan Zheng66435582008-10-30 14:19:50 -04001193 if (err == -EEXIST) {
1194 if (failed_start > start)
1195 clear_extent_bit(tree, start, failed_start - 1,
Chris Mason2c64c532009-09-02 15:04:12 -04001196 EXTENT_LOCKED, 1, 0, NULL, mask);
Josef Bacik25179202008-10-29 14:49:05 -04001197 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001198 }
Josef Bacik25179202008-10-29 14:49:05 -04001199 return 1;
1200}
Josef Bacik25179202008-10-29 14:49:05 -04001201
Chris Mason2c64c532009-09-02 15:04:12 -04001202int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1203 struct extent_state **cached, gfp_t mask)
1204{
1205 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1206 mask);
1207}
1208
Arne Jansen507903b2011-04-06 10:02:20 +00001209int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001210{
Chris Mason2c64c532009-09-02 15:04:12 -04001211 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1212 mask);
Chris Masond1310b22008-01-24 16:13:08 -05001213}
Chris Masond1310b22008-01-24 16:13:08 -05001214
1215/*
Chris Masond1310b22008-01-24 16:13:08 -05001216 * helper function to set both pages and extents in the tree writeback
1217 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001218static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001219{
1220 unsigned long index = start >> PAGE_CACHE_SHIFT;
1221 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1222 struct page *page;
1223
1224 while (index <= end_index) {
1225 page = find_get_page(tree->mapping, index);
1226 BUG_ON(!page);
1227 set_page_writeback(page);
1228 page_cache_release(page);
1229 index++;
1230 }
Chris Masond1310b22008-01-24 16:13:08 -05001231 return 0;
1232}
Chris Masond1310b22008-01-24 16:13:08 -05001233
Chris Masond352ac62008-09-29 15:18:18 -04001234/* find the first state struct with 'bits' set after 'start', and
1235 * return it. tree->lock must be held. NULL will returned if
1236 * nothing was found after 'start'
1237 */
Chris Masond7fc6402008-02-18 12:12:38 -05001238struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1239 u64 start, int bits)
1240{
1241 struct rb_node *node;
1242 struct extent_state *state;
1243
1244 /*
1245 * this search will find all the extents that end after
1246 * our range starts.
1247 */
1248 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001249 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001250 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001251
Chris Masond3977122009-01-05 21:25:51 -05001252 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001253 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001254 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001255 return state;
Chris Masond3977122009-01-05 21:25:51 -05001256
Chris Masond7fc6402008-02-18 12:12:38 -05001257 node = rb_next(node);
1258 if (!node)
1259 break;
1260 }
1261out:
1262 return NULL;
1263}
Chris Masond7fc6402008-02-18 12:12:38 -05001264
Chris Masond352ac62008-09-29 15:18:18 -04001265/*
Xiao Guangrong69261c42011-07-14 03:19:45 +00001266 * find the first offset in the io tree with 'bits' set. zero is
1267 * returned if we find something, and *start_ret and *end_ret are
1268 * set to reflect the state struct that was found.
1269 *
1270 * If nothing was found, 1 is returned, < 0 on error
1271 */
1272int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1273 u64 *start_ret, u64 *end_ret, int bits)
1274{
1275 struct extent_state *state;
1276 int ret = 1;
1277
1278 spin_lock(&tree->lock);
1279 state = find_first_extent_bit_state(tree, start, bits);
1280 if (state) {
1281 *start_ret = state->start;
1282 *end_ret = state->end;
1283 ret = 0;
1284 }
1285 spin_unlock(&tree->lock);
1286 return ret;
1287}
1288
1289/*
Chris Masond352ac62008-09-29 15:18:18 -04001290 * find a contiguous range of bytes in the file marked as delalloc, not
1291 * more than 'max_bytes'. start and end are used to return the range,
1292 *
1293 * 1 is returned if we find something, 0 if nothing was in the tree
1294 */
Chris Masonc8b97812008-10-29 14:49:59 -04001295static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001296 u64 *start, u64 *end, u64 max_bytes,
1297 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001298{
1299 struct rb_node *node;
1300 struct extent_state *state;
1301 u64 cur_start = *start;
1302 u64 found = 0;
1303 u64 total_bytes = 0;
1304
Chris Masoncad321a2008-12-17 14:51:42 -05001305 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001306
Chris Masond1310b22008-01-24 16:13:08 -05001307 /*
1308 * this search will find all the extents that end after
1309 * our range starts.
1310 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001311 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001312 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001313 if (!found)
1314 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001315 goto out;
1316 }
1317
Chris Masond3977122009-01-05 21:25:51 -05001318 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001319 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001320 if (found && (state->start != cur_start ||
1321 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001322 goto out;
1323 }
1324 if (!(state->state & EXTENT_DELALLOC)) {
1325 if (!found)
1326 *end = state->end;
1327 goto out;
1328 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001329 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001330 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001331 *cached_state = state;
1332 atomic_inc(&state->refs);
1333 }
Chris Masond1310b22008-01-24 16:13:08 -05001334 found++;
1335 *end = state->end;
1336 cur_start = state->end + 1;
1337 node = rb_next(node);
1338 if (!node)
1339 break;
1340 total_bytes += state->end - state->start + 1;
1341 if (total_bytes >= max_bytes)
1342 break;
1343 }
1344out:
Chris Masoncad321a2008-12-17 14:51:42 -05001345 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001346 return found;
1347}
1348
Chris Masonc8b97812008-10-29 14:49:59 -04001349static noinline int __unlock_for_delalloc(struct inode *inode,
1350 struct page *locked_page,
1351 u64 start, u64 end)
1352{
1353 int ret;
1354 struct page *pages[16];
1355 unsigned long index = start >> PAGE_CACHE_SHIFT;
1356 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1357 unsigned long nr_pages = end_index - index + 1;
1358 int i;
1359
1360 if (index == locked_page->index && end_index == index)
1361 return 0;
1362
Chris Masond3977122009-01-05 21:25:51 -05001363 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001364 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001365 min_t(unsigned long, nr_pages,
1366 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001367 for (i = 0; i < ret; i++) {
1368 if (pages[i] != locked_page)
1369 unlock_page(pages[i]);
1370 page_cache_release(pages[i]);
1371 }
1372 nr_pages -= ret;
1373 index += ret;
1374 cond_resched();
1375 }
1376 return 0;
1377}
1378
1379static noinline int lock_delalloc_pages(struct inode *inode,
1380 struct page *locked_page,
1381 u64 delalloc_start,
1382 u64 delalloc_end)
1383{
1384 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1385 unsigned long start_index = index;
1386 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1387 unsigned long pages_locked = 0;
1388 struct page *pages[16];
1389 unsigned long nrpages;
1390 int ret;
1391 int i;
1392
1393 /* the caller is responsible for locking the start index */
1394 if (index == locked_page->index && index == end_index)
1395 return 0;
1396
1397 /* skip the page at the start index */
1398 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001399 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001400 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001401 min_t(unsigned long,
1402 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001403 if (ret == 0) {
1404 ret = -EAGAIN;
1405 goto done;
1406 }
1407 /* now we have an array of pages, lock them all */
1408 for (i = 0; i < ret; i++) {
1409 /*
1410 * the caller is taking responsibility for
1411 * locked_page
1412 */
Chris Mason771ed682008-11-06 22:02:51 -05001413 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001414 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001415 if (!PageDirty(pages[i]) ||
1416 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001417 ret = -EAGAIN;
1418 unlock_page(pages[i]);
1419 page_cache_release(pages[i]);
1420 goto done;
1421 }
1422 }
Chris Masonc8b97812008-10-29 14:49:59 -04001423 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001424 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001425 }
Chris Masonc8b97812008-10-29 14:49:59 -04001426 nrpages -= ret;
1427 index += ret;
1428 cond_resched();
1429 }
1430 ret = 0;
1431done:
1432 if (ret && pages_locked) {
1433 __unlock_for_delalloc(inode, locked_page,
1434 delalloc_start,
1435 ((u64)(start_index + pages_locked - 1)) <<
1436 PAGE_CACHE_SHIFT);
1437 }
1438 return ret;
1439}
1440
1441/*
1442 * find a contiguous range of bytes in the file marked as delalloc, not
1443 * more than 'max_bytes'. start and end are used to return the range,
1444 *
1445 * 1 is returned if we find something, 0 if nothing was in the tree
1446 */
1447static noinline u64 find_lock_delalloc_range(struct inode *inode,
1448 struct extent_io_tree *tree,
1449 struct page *locked_page,
1450 u64 *start, u64 *end,
1451 u64 max_bytes)
1452{
1453 u64 delalloc_start;
1454 u64 delalloc_end;
1455 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001456 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001457 int ret;
1458 int loops = 0;
1459
1460again:
1461 /* step one, find a bunch of delalloc bytes starting at start */
1462 delalloc_start = *start;
1463 delalloc_end = 0;
1464 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001465 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001466 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001467 *start = delalloc_start;
1468 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001469 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001470 return found;
1471 }
1472
1473 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001474 * start comes from the offset of locked_page. We have to lock
1475 * pages in order, so we can't process delalloc bytes before
1476 * locked_page
1477 */
Chris Masond3977122009-01-05 21:25:51 -05001478 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001479 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001480
1481 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001482 * make sure to limit the number of pages we try to lock down
1483 * if we're looping.
1484 */
Chris Masond3977122009-01-05 21:25:51 -05001485 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001486 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001487
Chris Masonc8b97812008-10-29 14:49:59 -04001488 /* step two, lock all the pages after the page that has start */
1489 ret = lock_delalloc_pages(inode, locked_page,
1490 delalloc_start, delalloc_end);
1491 if (ret == -EAGAIN) {
1492 /* some of the pages are gone, lets avoid looping by
1493 * shortening the size of the delalloc range we're searching
1494 */
Chris Mason9655d292009-09-02 15:22:30 -04001495 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001496 if (!loops) {
1497 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1498 max_bytes = PAGE_CACHE_SIZE - offset;
1499 loops = 1;
1500 goto again;
1501 } else {
1502 found = 0;
1503 goto out_failed;
1504 }
1505 }
1506 BUG_ON(ret);
1507
1508 /* step three, lock the state bits for the whole range */
Chris Mason9655d292009-09-02 15:22:30 -04001509 lock_extent_bits(tree, delalloc_start, delalloc_end,
1510 0, &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001511
1512 /* then test to make sure it is all still delalloc */
1513 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001514 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001515 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001516 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1517 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001518 __unlock_for_delalloc(inode, locked_page,
1519 delalloc_start, delalloc_end);
1520 cond_resched();
1521 goto again;
1522 }
Chris Mason9655d292009-09-02 15:22:30 -04001523 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001524 *start = delalloc_start;
1525 *end = delalloc_end;
1526out_failed:
1527 return found;
1528}
1529
1530int extent_clear_unlock_delalloc(struct inode *inode,
1531 struct extent_io_tree *tree,
1532 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001533 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001534{
1535 int ret;
1536 struct page *pages[16];
1537 unsigned long index = start >> PAGE_CACHE_SHIFT;
1538 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1539 unsigned long nr_pages = end_index - index + 1;
1540 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001541 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001542
Chris Masona791e352009-10-08 11:27:10 -04001543 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001544 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001545 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001546 clear_bits |= EXTENT_DIRTY;
1547
Chris Masona791e352009-10-08 11:27:10 -04001548 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001549 clear_bits |= EXTENT_DELALLOC;
1550
Chris Mason2c64c532009-09-02 15:04:12 -04001551 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001552 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1553 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1554 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001555 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001556
Chris Masond3977122009-01-05 21:25:51 -05001557 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001558 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001559 min_t(unsigned long,
1560 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001561 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001562
Chris Masona791e352009-10-08 11:27:10 -04001563 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001564 SetPagePrivate2(pages[i]);
1565
Chris Masonc8b97812008-10-29 14:49:59 -04001566 if (pages[i] == locked_page) {
1567 page_cache_release(pages[i]);
1568 continue;
1569 }
Chris Masona791e352009-10-08 11:27:10 -04001570 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001571 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001572 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001573 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001574 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001575 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001576 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001577 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001578 page_cache_release(pages[i]);
1579 }
1580 nr_pages -= ret;
1581 index += ret;
1582 cond_resched();
1583 }
1584 return 0;
1585}
Chris Masonc8b97812008-10-29 14:49:59 -04001586
Chris Masond352ac62008-09-29 15:18:18 -04001587/*
1588 * count the number of bytes in the tree that have a given bit(s)
1589 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1590 * cached. The total number found is returned.
1591 */
Chris Masond1310b22008-01-24 16:13:08 -05001592u64 count_range_bits(struct extent_io_tree *tree,
1593 u64 *start, u64 search_end, u64 max_bytes,
Chris Masonec29ed52011-02-23 16:23:20 -05001594 unsigned long bits, int contig)
Chris Masond1310b22008-01-24 16:13:08 -05001595{
1596 struct rb_node *node;
1597 struct extent_state *state;
1598 u64 cur_start = *start;
1599 u64 total_bytes = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05001600 u64 last = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001601 int found = 0;
1602
1603 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001604 WARN_ON(1);
1605 return 0;
1606 }
1607
Chris Masoncad321a2008-12-17 14:51:42 -05001608 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001609 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1610 total_bytes = tree->dirty_bytes;
1611 goto out;
1612 }
1613 /*
1614 * this search will find all the extents that end after
1615 * our range starts.
1616 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001617 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001618 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001619 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001620
Chris Masond3977122009-01-05 21:25:51 -05001621 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001622 state = rb_entry(node, struct extent_state, rb_node);
1623 if (state->start > search_end)
1624 break;
Chris Masonec29ed52011-02-23 16:23:20 -05001625 if (contig && found && state->start > last + 1)
1626 break;
1627 if (state->end >= cur_start && (state->state & bits) == bits) {
Chris Masond1310b22008-01-24 16:13:08 -05001628 total_bytes += min(search_end, state->end) + 1 -
1629 max(cur_start, state->start);
1630 if (total_bytes >= max_bytes)
1631 break;
1632 if (!found) {
Josef Bacikaf60bed2011-05-04 11:11:17 -04001633 *start = max(cur_start, state->start);
Chris Masond1310b22008-01-24 16:13:08 -05001634 found = 1;
1635 }
Chris Masonec29ed52011-02-23 16:23:20 -05001636 last = state->end;
1637 } else if (contig && found) {
1638 break;
Chris Masond1310b22008-01-24 16:13:08 -05001639 }
1640 node = rb_next(node);
1641 if (!node)
1642 break;
1643 }
1644out:
Chris Masoncad321a2008-12-17 14:51:42 -05001645 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001646 return total_bytes;
1647}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001648
Chris Masond352ac62008-09-29 15:18:18 -04001649/*
1650 * set the private field for a given byte offset in the tree. If there isn't
1651 * an extent_state there already, this does nothing.
1652 */
Chris Masond1310b22008-01-24 16:13:08 -05001653int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1654{
1655 struct rb_node *node;
1656 struct extent_state *state;
1657 int ret = 0;
1658
Chris Masoncad321a2008-12-17 14:51:42 -05001659 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001660 /*
1661 * this search will find all the extents that end after
1662 * our range starts.
1663 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001664 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001665 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001666 ret = -ENOENT;
1667 goto out;
1668 }
1669 state = rb_entry(node, struct extent_state, rb_node);
1670 if (state->start != start) {
1671 ret = -ENOENT;
1672 goto out;
1673 }
1674 state->private = private;
1675out:
Chris Masoncad321a2008-12-17 14:51:42 -05001676 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001677 return ret;
1678}
1679
1680int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1681{
1682 struct rb_node *node;
1683 struct extent_state *state;
1684 int ret = 0;
1685
Chris Masoncad321a2008-12-17 14:51:42 -05001686 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001687 /*
1688 * this search will find all the extents that end after
1689 * our range starts.
1690 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001691 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001692 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001693 ret = -ENOENT;
1694 goto out;
1695 }
1696 state = rb_entry(node, struct extent_state, rb_node);
1697 if (state->start != start) {
1698 ret = -ENOENT;
1699 goto out;
1700 }
1701 *private = state->private;
1702out:
Chris Masoncad321a2008-12-17 14:51:42 -05001703 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001704 return ret;
1705}
1706
1707/*
1708 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001709 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001710 * has the bits set. Otherwise, 1 is returned if any bit in the
1711 * range is found set.
1712 */
1713int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001714 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001715{
1716 struct extent_state *state = NULL;
1717 struct rb_node *node;
1718 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001719
Chris Masoncad321a2008-12-17 14:51:42 -05001720 spin_lock(&tree->lock);
Josef Bacikdf98b6e2011-06-20 14:53:48 -04001721 if (cached && cached->tree && cached->start <= start &&
1722 cached->end > start)
Chris Mason9655d292009-09-02 15:22:30 -04001723 node = &cached->rb_node;
1724 else
1725 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001726 while (node && start <= end) {
1727 state = rb_entry(node, struct extent_state, rb_node);
1728
1729 if (filled && state->start > start) {
1730 bitset = 0;
1731 break;
1732 }
1733
1734 if (state->start > end)
1735 break;
1736
1737 if (state->state & bits) {
1738 bitset = 1;
1739 if (!filled)
1740 break;
1741 } else if (filled) {
1742 bitset = 0;
1743 break;
1744 }
Chris Mason46562cec2009-09-23 20:23:16 -04001745
1746 if (state->end == (u64)-1)
1747 break;
1748
Chris Masond1310b22008-01-24 16:13:08 -05001749 start = state->end + 1;
1750 if (start > end)
1751 break;
1752 node = rb_next(node);
1753 if (!node) {
1754 if (filled)
1755 bitset = 0;
1756 break;
1757 }
1758 }
Chris Masoncad321a2008-12-17 14:51:42 -05001759 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001760 return bitset;
1761}
Chris Masond1310b22008-01-24 16:13:08 -05001762
1763/*
1764 * helper function to set a given page up to date if all the
1765 * extents in the tree for that page are up to date
1766 */
1767static int check_page_uptodate(struct extent_io_tree *tree,
1768 struct page *page)
1769{
1770 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1771 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001772 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001773 SetPageUptodate(page);
1774 return 0;
1775}
1776
1777/*
1778 * helper function to unlock a page if all the extents in the tree
1779 * for that page are unlocked
1780 */
1781static int check_page_locked(struct extent_io_tree *tree,
1782 struct page *page)
1783{
1784 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1785 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001786 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001787 unlock_page(page);
1788 return 0;
1789}
1790
1791/*
1792 * helper function to end page writeback if all the extents
1793 * in the tree for that page are done with writeback
1794 */
1795static int check_page_writeback(struct extent_io_tree *tree,
1796 struct page *page)
1797{
Chris Mason1edbb732009-09-02 13:24:36 -04001798 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001799 return 0;
1800}
1801
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001802/*
1803 * When IO fails, either with EIO or csum verification fails, we
1804 * try other mirrors that might have a good copy of the data. This
1805 * io_failure_record is used to record state as we go through all the
1806 * mirrors. If another mirror has good data, the page is set up to date
1807 * and things continue. If a good mirror can't be found, the original
1808 * bio end_io callback is called to indicate things have failed.
1809 */
1810struct io_failure_record {
1811 struct page *page;
1812 u64 start;
1813 u64 len;
1814 u64 logical;
1815 unsigned long bio_flags;
1816 int this_mirror;
1817 int failed_mirror;
1818 int in_validation;
1819};
1820
1821static int free_io_failure(struct inode *inode, struct io_failure_record *rec,
1822 int did_repair)
1823{
1824 int ret;
1825 int err = 0;
1826 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1827
1828 set_state_private(failure_tree, rec->start, 0);
1829 ret = clear_extent_bits(failure_tree, rec->start,
1830 rec->start + rec->len - 1,
1831 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1832 if (ret)
1833 err = ret;
1834
1835 if (did_repair) {
1836 ret = clear_extent_bits(&BTRFS_I(inode)->io_tree, rec->start,
1837 rec->start + rec->len - 1,
1838 EXTENT_DAMAGED, GFP_NOFS);
1839 if (ret && !err)
1840 err = ret;
1841 }
1842
1843 kfree(rec);
1844 return err;
1845}
1846
1847static void repair_io_failure_callback(struct bio *bio, int err)
1848{
1849 complete(bio->bi_private);
1850}
1851
1852/*
1853 * this bypasses the standard btrfs submit functions deliberately, as
1854 * the standard behavior is to write all copies in a raid setup. here we only
1855 * want to write the one bad copy. so we do the mapping for ourselves and issue
1856 * submit_bio directly.
1857 * to avoid any synchonization issues, wait for the data after writing, which
1858 * actually prevents the read that triggered the error from finishing.
1859 * currently, there can be no more than two copies of every data bit. thus,
1860 * exactly one rewrite is required.
1861 */
1862int repair_io_failure(struct btrfs_mapping_tree *map_tree, u64 start,
1863 u64 length, u64 logical, struct page *page,
1864 int mirror_num)
1865{
1866 struct bio *bio;
1867 struct btrfs_device *dev;
1868 DECLARE_COMPLETION_ONSTACK(compl);
1869 u64 map_length = 0;
1870 u64 sector;
1871 struct btrfs_bio *bbio = NULL;
1872 int ret;
1873
1874 BUG_ON(!mirror_num);
1875
1876 bio = bio_alloc(GFP_NOFS, 1);
1877 if (!bio)
1878 return -EIO;
1879 bio->bi_private = &compl;
1880 bio->bi_end_io = repair_io_failure_callback;
1881 bio->bi_size = 0;
1882 map_length = length;
1883
1884 ret = btrfs_map_block(map_tree, WRITE, logical,
1885 &map_length, &bbio, mirror_num);
1886 if (ret) {
1887 bio_put(bio);
1888 return -EIO;
1889 }
1890 BUG_ON(mirror_num != bbio->mirror_num);
1891 sector = bbio->stripes[mirror_num-1].physical >> 9;
1892 bio->bi_sector = sector;
1893 dev = bbio->stripes[mirror_num-1].dev;
1894 kfree(bbio);
1895 if (!dev || !dev->bdev || !dev->writeable) {
1896 bio_put(bio);
1897 return -EIO;
1898 }
1899 bio->bi_bdev = dev->bdev;
1900 bio_add_page(bio, page, length, start-page_offset(page));
Stefan Behrens21adbd52011-11-09 13:44:05 +01001901 btrfsic_submit_bio(WRITE_SYNC, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02001902 wait_for_completion(&compl);
1903
1904 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
1905 /* try to remap that extent elsewhere? */
1906 bio_put(bio);
1907 return -EIO;
1908 }
1909
1910 printk(KERN_INFO "btrfs read error corrected: ino %lu off %llu (dev %s "
1911 "sector %llu)\n", page->mapping->host->i_ino, start,
1912 dev->name, sector);
1913
1914 bio_put(bio);
1915 return 0;
1916}
1917
1918/*
1919 * each time an IO finishes, we do a fast check in the IO failure tree
1920 * to see if we need to process or clean up an io_failure_record
1921 */
1922static int clean_io_failure(u64 start, struct page *page)
1923{
1924 u64 private;
1925 u64 private_failure;
1926 struct io_failure_record *failrec;
1927 struct btrfs_mapping_tree *map_tree;
1928 struct extent_state *state;
1929 int num_copies;
1930 int did_repair = 0;
1931 int ret;
1932 struct inode *inode = page->mapping->host;
1933
1934 private = 0;
1935 ret = count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1936 (u64)-1, 1, EXTENT_DIRTY, 0);
1937 if (!ret)
1938 return 0;
1939
1940 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, start,
1941 &private_failure);
1942 if (ret)
1943 return 0;
1944
1945 failrec = (struct io_failure_record *)(unsigned long) private_failure;
1946 BUG_ON(!failrec->this_mirror);
1947
1948 if (failrec->in_validation) {
1949 /* there was no real error, just free the record */
1950 pr_debug("clean_io_failure: freeing dummy error at %llu\n",
1951 failrec->start);
1952 did_repair = 1;
1953 goto out;
1954 }
1955
1956 spin_lock(&BTRFS_I(inode)->io_tree.lock);
1957 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1958 failrec->start,
1959 EXTENT_LOCKED);
1960 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
1961
1962 if (state && state->start == failrec->start) {
1963 map_tree = &BTRFS_I(inode)->root->fs_info->mapping_tree;
1964 num_copies = btrfs_num_copies(map_tree, failrec->logical,
1965 failrec->len);
1966 if (num_copies > 1) {
1967 ret = repair_io_failure(map_tree, start, failrec->len,
1968 failrec->logical, page,
1969 failrec->failed_mirror);
1970 did_repair = !ret;
1971 }
1972 }
1973
1974out:
1975 if (!ret)
1976 ret = free_io_failure(inode, failrec, did_repair);
1977
1978 return ret;
1979}
1980
1981/*
1982 * this is a generic handler for readpage errors (default
1983 * readpage_io_failed_hook). if other copies exist, read those and write back
1984 * good data to the failed position. does not investigate in remapping the
1985 * failed extent elsewhere, hoping the device will be smart enough to do this as
1986 * needed
1987 */
1988
1989static int bio_readpage_error(struct bio *failed_bio, struct page *page,
1990 u64 start, u64 end, int failed_mirror,
1991 struct extent_state *state)
1992{
1993 struct io_failure_record *failrec = NULL;
1994 u64 private;
1995 struct extent_map *em;
1996 struct inode *inode = page->mapping->host;
1997 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1998 struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
1999 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2000 struct bio *bio;
2001 int num_copies;
2002 int ret;
2003 int read_mode;
2004 u64 logical;
2005
2006 BUG_ON(failed_bio->bi_rw & REQ_WRITE);
2007
2008 ret = get_state_private(failure_tree, start, &private);
2009 if (ret) {
2010 failrec = kzalloc(sizeof(*failrec), GFP_NOFS);
2011 if (!failrec)
2012 return -ENOMEM;
2013 failrec->start = start;
2014 failrec->len = end - start + 1;
2015 failrec->this_mirror = 0;
2016 failrec->bio_flags = 0;
2017 failrec->in_validation = 0;
2018
2019 read_lock(&em_tree->lock);
2020 em = lookup_extent_mapping(em_tree, start, failrec->len);
2021 if (!em) {
2022 read_unlock(&em_tree->lock);
2023 kfree(failrec);
2024 return -EIO;
2025 }
2026
2027 if (em->start > start || em->start + em->len < start) {
2028 free_extent_map(em);
2029 em = NULL;
2030 }
2031 read_unlock(&em_tree->lock);
2032
2033 if (!em || IS_ERR(em)) {
2034 kfree(failrec);
2035 return -EIO;
2036 }
2037 logical = start - em->start;
2038 logical = em->block_start + logical;
2039 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
2040 logical = em->block_start;
2041 failrec->bio_flags = EXTENT_BIO_COMPRESSED;
2042 extent_set_compress_type(&failrec->bio_flags,
2043 em->compress_type);
2044 }
2045 pr_debug("bio_readpage_error: (new) logical=%llu, start=%llu, "
2046 "len=%llu\n", logical, start, failrec->len);
2047 failrec->logical = logical;
2048 free_extent_map(em);
2049
2050 /* set the bits in the private failure tree */
2051 ret = set_extent_bits(failure_tree, start, end,
2052 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
2053 if (ret >= 0)
2054 ret = set_state_private(failure_tree, start,
2055 (u64)(unsigned long)failrec);
2056 /* set the bits in the inode's tree */
2057 if (ret >= 0)
2058 ret = set_extent_bits(tree, start, end, EXTENT_DAMAGED,
2059 GFP_NOFS);
2060 if (ret < 0) {
2061 kfree(failrec);
2062 return ret;
2063 }
2064 } else {
2065 failrec = (struct io_failure_record *)(unsigned long)private;
2066 pr_debug("bio_readpage_error: (found) logical=%llu, "
2067 "start=%llu, len=%llu, validation=%d\n",
2068 failrec->logical, failrec->start, failrec->len,
2069 failrec->in_validation);
2070 /*
2071 * when data can be on disk more than twice, add to failrec here
2072 * (e.g. with a list for failed_mirror) to make
2073 * clean_io_failure() clean all those errors at once.
2074 */
2075 }
2076 num_copies = btrfs_num_copies(
2077 &BTRFS_I(inode)->root->fs_info->mapping_tree,
2078 failrec->logical, failrec->len);
2079 if (num_copies == 1) {
2080 /*
2081 * we only have a single copy of the data, so don't bother with
2082 * all the retry and error correction code that follows. no
2083 * matter what the error is, it is very likely to persist.
2084 */
2085 pr_debug("bio_readpage_error: cannot repair, num_copies == 1. "
2086 "state=%p, num_copies=%d, next_mirror %d, "
2087 "failed_mirror %d\n", state, num_copies,
2088 failrec->this_mirror, failed_mirror);
2089 free_io_failure(inode, failrec, 0);
2090 return -EIO;
2091 }
2092
2093 if (!state) {
2094 spin_lock(&tree->lock);
2095 state = find_first_extent_bit_state(tree, failrec->start,
2096 EXTENT_LOCKED);
2097 if (state && state->start != failrec->start)
2098 state = NULL;
2099 spin_unlock(&tree->lock);
2100 }
2101
2102 /*
2103 * there are two premises:
2104 * a) deliver good data to the caller
2105 * b) correct the bad sectors on disk
2106 */
2107 if (failed_bio->bi_vcnt > 1) {
2108 /*
2109 * to fulfill b), we need to know the exact failing sectors, as
2110 * we don't want to rewrite any more than the failed ones. thus,
2111 * we need separate read requests for the failed bio
2112 *
2113 * if the following BUG_ON triggers, our validation request got
2114 * merged. we need separate requests for our algorithm to work.
2115 */
2116 BUG_ON(failrec->in_validation);
2117 failrec->in_validation = 1;
2118 failrec->this_mirror = failed_mirror;
2119 read_mode = READ_SYNC | REQ_FAILFAST_DEV;
2120 } else {
2121 /*
2122 * we're ready to fulfill a) and b) alongside. get a good copy
2123 * of the failed sector and if we succeed, we have setup
2124 * everything for repair_io_failure to do the rest for us.
2125 */
2126 if (failrec->in_validation) {
2127 BUG_ON(failrec->this_mirror != failed_mirror);
2128 failrec->in_validation = 0;
2129 failrec->this_mirror = 0;
2130 }
2131 failrec->failed_mirror = failed_mirror;
2132 failrec->this_mirror++;
2133 if (failrec->this_mirror == failed_mirror)
2134 failrec->this_mirror++;
2135 read_mode = READ_SYNC;
2136 }
2137
2138 if (!state || failrec->this_mirror > num_copies) {
2139 pr_debug("bio_readpage_error: (fail) state=%p, num_copies=%d, "
2140 "next_mirror %d, failed_mirror %d\n", state,
2141 num_copies, failrec->this_mirror, failed_mirror);
2142 free_io_failure(inode, failrec, 0);
2143 return -EIO;
2144 }
2145
2146 bio = bio_alloc(GFP_NOFS, 1);
2147 bio->bi_private = state;
2148 bio->bi_end_io = failed_bio->bi_end_io;
2149 bio->bi_sector = failrec->logical >> 9;
2150 bio->bi_bdev = BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev;
2151 bio->bi_size = 0;
2152
2153 bio_add_page(bio, page, failrec->len, start - page_offset(page));
2154
2155 pr_debug("bio_readpage_error: submitting new read[%#x] to "
2156 "this_mirror=%d, num_copies=%d, in_validation=%d\n", read_mode,
2157 failrec->this_mirror, num_copies, failrec->in_validation);
2158
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002159 ret = tree->ops->submit_bio_hook(inode, read_mode, bio,
2160 failrec->this_mirror,
2161 failrec->bio_flags, 0);
2162 return ret;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002163}
2164
Chris Masond1310b22008-01-24 16:13:08 -05002165/* lots and lots of room for performance fixes in the end_bio funcs */
2166
Jeff Mahoney87826df2012-02-15 16:23:57 +01002167int end_extent_writepage(struct page *page, int err, u64 start, u64 end)
2168{
2169 int uptodate = (err == 0);
2170 struct extent_io_tree *tree;
2171 int ret;
2172
2173 tree = &BTRFS_I(page->mapping->host)->io_tree;
2174
2175 if (tree->ops && tree->ops->writepage_end_io_hook) {
2176 ret = tree->ops->writepage_end_io_hook(page, start,
2177 end, NULL, uptodate);
2178 if (ret)
2179 uptodate = 0;
2180 }
2181
2182 if (!uptodate && tree->ops &&
2183 tree->ops->writepage_io_failed_hook) {
2184 ret = tree->ops->writepage_io_failed_hook(NULL, page,
2185 start, end, NULL);
2186 /* Writeback already completed */
2187 if (ret == 0)
2188 return 1;
2189 }
2190
2191 if (!uptodate) {
2192 clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
2193 ClearPageUptodate(page);
2194 SetPageError(page);
2195 }
2196 return 0;
2197}
2198
Chris Masond1310b22008-01-24 16:13:08 -05002199/*
2200 * after a writepage IO is done, we need to:
2201 * clear the uptodate bits on error
2202 * clear the writeback bits in the extent tree for this IO
2203 * end_page_writeback if the page has no more pending IO
2204 *
2205 * Scheduling is not allowed, so the extent state tree is expected
2206 * to have one and only one object corresponding to this IO.
2207 */
Chris Masond1310b22008-01-24 16:13:08 -05002208static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002209{
Chris Masond1310b22008-01-24 16:13:08 -05002210 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04002211 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002212 u64 start;
2213 u64 end;
2214 int whole_page;
2215
Chris Masond1310b22008-01-24 16:13:08 -05002216 do {
2217 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04002218 tree = &BTRFS_I(page->mapping->host)->io_tree;
2219
Chris Masond1310b22008-01-24 16:13:08 -05002220 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2221 bvec->bv_offset;
2222 end = start + bvec->bv_len - 1;
2223
2224 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2225 whole_page = 1;
2226 else
2227 whole_page = 0;
2228
2229 if (--bvec >= bio->bi_io_vec)
2230 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04002231
Jeff Mahoney87826df2012-02-15 16:23:57 +01002232 if (end_extent_writepage(page, err, start, end))
2233 continue;
Chris Mason70dec802008-01-29 09:59:12 -05002234
Chris Masond1310b22008-01-24 16:13:08 -05002235 if (whole_page)
2236 end_page_writeback(page);
2237 else
2238 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002239 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04002240
Chris Masond1310b22008-01-24 16:13:08 -05002241 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002242}
2243
2244/*
2245 * after a readpage IO is done, we need to:
2246 * clear the uptodate bits on error
2247 * set the uptodate bits if things worked
2248 * set the page up to date if all extents in the tree are uptodate
2249 * clear the lock bit in the extent tree
2250 * unlock the page if there are no other extents locked for it
2251 *
2252 * Scheduling is not allowed, so the extent state tree is expected
2253 * to have one and only one object corresponding to this IO.
2254 */
Chris Masond1310b22008-01-24 16:13:08 -05002255static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05002256{
2257 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00002258 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
2259 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04002260 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05002261 u64 start;
2262 u64 end;
2263 int whole_page;
2264 int ret;
2265
Chris Masond20f7042008-12-08 16:58:54 -05002266 if (err)
2267 uptodate = 0;
2268
Chris Masond1310b22008-01-24 16:13:08 -05002269 do {
2270 struct page *page = bvec->bv_page;
Arne Jansen507903b2011-04-06 10:02:20 +00002271 struct extent_state *cached = NULL;
2272 struct extent_state *state;
2273
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002274 pr_debug("end_bio_extent_readpage: bi_vcnt=%d, idx=%d, err=%d, "
2275 "mirror=%ld\n", bio->bi_vcnt, bio->bi_idx, err,
2276 (long int)bio->bi_bdev);
David Woodhouse902b22f2008-08-20 08:51:49 -04002277 tree = &BTRFS_I(page->mapping->host)->io_tree;
2278
Chris Masond1310b22008-01-24 16:13:08 -05002279 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
2280 bvec->bv_offset;
2281 end = start + bvec->bv_len - 1;
2282
2283 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
2284 whole_page = 1;
2285 else
2286 whole_page = 0;
2287
Chris Mason4125bf72010-02-03 18:18:45 +00002288 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05002289 prefetchw(&bvec->bv_page->flags);
2290
Arne Jansen507903b2011-04-06 10:02:20 +00002291 spin_lock(&tree->lock);
Chris Mason0d399202011-04-16 06:55:39 -04002292 state = find_first_extent_bit_state(tree, start, EXTENT_LOCKED);
Chris Mason109b36a2011-04-12 13:57:39 -04002293 if (state && state->start == start) {
Arne Jansen507903b2011-04-06 10:02:20 +00002294 /*
2295 * take a reference on the state, unlock will drop
2296 * the ref
2297 */
2298 cache_state(state, &cached);
2299 }
2300 spin_unlock(&tree->lock);
2301
Chris Masond1310b22008-01-24 16:13:08 -05002302 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05002303 ret = tree->ops->readpage_end_io_hook(page, start, end,
Arne Jansen507903b2011-04-06 10:02:20 +00002304 state);
Chris Masond1310b22008-01-24 16:13:08 -05002305 if (ret)
2306 uptodate = 0;
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002307 else
2308 clean_io_failure(start, page);
Chris Masond1310b22008-01-24 16:13:08 -05002309 }
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002310 if (!uptodate) {
Jan Schmidt32240a92011-11-20 07:33:38 -05002311 int failed_mirror;
2312 failed_mirror = (int)(unsigned long)bio->bi_bdev;
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002313 /*
2314 * The generic bio_readpage_error handles errors the
2315 * following way: If possible, new read requests are
2316 * created and submitted and will end up in
2317 * end_bio_extent_readpage as well (if we're lucky, not
2318 * in the !uptodate case). In that case it returns 0 and
2319 * we just go on with the next page in our bio. If it
2320 * can't handle the error it will return -EIO and we
2321 * remain responsible for that page.
2322 */
2323 ret = bio_readpage_error(bio, page, start, end,
2324 failed_mirror, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04002325 if (ret == 0) {
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002326error_handled:
Chris Mason3b951512008-04-17 11:29:12 -04002327 uptodate =
2328 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05002329 if (err)
2330 uptodate = 0;
Arne Jansen507903b2011-04-06 10:02:20 +00002331 uncache_state(&cached);
Chris Mason7e383262008-04-09 16:28:12 -04002332 continue;
2333 }
Jan Schmidtf4a8e652011-12-01 09:30:36 -05002334 if (tree->ops && tree->ops->readpage_io_failed_hook) {
2335 ret = tree->ops->readpage_io_failed_hook(
2336 bio, page, start, end,
2337 failed_mirror, state);
2338 if (ret == 0)
2339 goto error_handled;
2340 }
Chris Mason7e383262008-04-09 16:28:12 -04002341 }
Chris Mason70dec802008-01-29 09:59:12 -05002342
Josef Bacik0b32f4b2012-03-13 09:38:00 -04002343 if (uptodate && tree->track_uptodate) {
Arne Jansen507903b2011-04-06 10:02:20 +00002344 set_extent_uptodate(tree, start, end, &cached,
David Woodhouse902b22f2008-08-20 08:51:49 -04002345 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05002346 }
Arne Jansen507903b2011-04-06 10:02:20 +00002347 unlock_extent_cached(tree, start, end, &cached, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05002348
Chris Mason70dec802008-01-29 09:59:12 -05002349 if (whole_page) {
2350 if (uptodate) {
2351 SetPageUptodate(page);
2352 } else {
2353 ClearPageUptodate(page);
2354 SetPageError(page);
2355 }
Chris Masond1310b22008-01-24 16:13:08 -05002356 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05002357 } else {
2358 if (uptodate) {
2359 check_page_uptodate(tree, page);
2360 } else {
2361 ClearPageUptodate(page);
2362 SetPageError(page);
2363 }
Chris Masond1310b22008-01-24 16:13:08 -05002364 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05002365 }
Chris Mason4125bf72010-02-03 18:18:45 +00002366 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05002367
2368 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05002369}
2370
Miao Xie88f794e2010-11-22 03:02:55 +00002371struct bio *
2372btrfs_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
2373 gfp_t gfp_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002374{
2375 struct bio *bio;
2376
2377 bio = bio_alloc(gfp_flags, nr_vecs);
2378
2379 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
2380 while (!bio && (nr_vecs /= 2))
2381 bio = bio_alloc(gfp_flags, nr_vecs);
2382 }
2383
2384 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04002385 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002386 bio->bi_bdev = bdev;
2387 bio->bi_sector = first_sector;
2388 }
2389 return bio;
2390}
2391
Chris Masonc8b97812008-10-29 14:49:59 -04002392static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
2393 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002394{
Chris Masond1310b22008-01-24 16:13:08 -05002395 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05002396 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
2397 struct page *page = bvec->bv_page;
2398 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05002399 u64 start;
Chris Mason70dec802008-01-29 09:59:12 -05002400
2401 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002402
David Woodhouse902b22f2008-08-20 08:51:49 -04002403 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002404
2405 bio_get(bio);
2406
Chris Mason065631f2008-02-20 12:07:25 -05002407 if (tree->ops && tree->ops->submit_bio_hook)
liubo6b82ce82011-01-26 06:21:39 +00002408 ret = tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002409 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04002410 else
Stefan Behrens21adbd52011-11-09 13:44:05 +01002411 btrfsic_submit_bio(rw, bio);
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002412
Chris Masond1310b22008-01-24 16:13:08 -05002413 if (bio_flagged(bio, BIO_EOPNOTSUPP))
2414 ret = -EOPNOTSUPP;
2415 bio_put(bio);
2416 return ret;
2417}
2418
2419static int submit_extent_page(int rw, struct extent_io_tree *tree,
2420 struct page *page, sector_t sector,
2421 size_t size, unsigned long offset,
2422 struct block_device *bdev,
2423 struct bio **bio_ret,
2424 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04002425 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04002426 int mirror_num,
2427 unsigned long prev_bio_flags,
2428 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002429{
2430 int ret = 0;
2431 struct bio *bio;
2432 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04002433 int contig = 0;
2434 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
2435 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05002436 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05002437
2438 if (bio_ret && *bio_ret) {
2439 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04002440 if (old_compressed)
2441 contig = bio->bi_sector == sector;
2442 else
2443 contig = bio->bi_sector + (bio->bi_size >> 9) ==
2444 sector;
2445
2446 if (prev_bio_flags != bio_flags || !contig ||
Chris Mason239b14b2008-03-24 15:02:07 -04002447 (tree->ops && tree->ops->merge_bio_hook &&
Chris Masonc8b97812008-10-29 14:49:59 -04002448 tree->ops->merge_bio_hook(page, offset, page_size, bio,
2449 bio_flags)) ||
2450 bio_add_page(bio, page, page_size, offset) < page_size) {
2451 ret = submit_one_bio(rw, bio, mirror_num,
2452 prev_bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002453 bio = NULL;
2454 } else {
2455 return 0;
2456 }
2457 }
Chris Masonc8b97812008-10-29 14:49:59 -04002458 if (this_compressed)
2459 nr = BIO_MAX_PAGES;
2460 else
2461 nr = bio_get_nr_vecs(bdev);
2462
Miao Xie88f794e2010-11-22 03:02:55 +00002463 bio = btrfs_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Tsutomu Itoh5df67082011-02-01 09:17:35 +00002464 if (!bio)
2465 return -ENOMEM;
Chris Mason70dec802008-01-29 09:59:12 -05002466
Chris Masonc8b97812008-10-29 14:49:59 -04002467 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05002468 bio->bi_end_io = end_io_func;
2469 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05002470
Chris Masond3977122009-01-05 21:25:51 -05002471 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05002472 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05002473 else
Chris Masonc8b97812008-10-29 14:49:59 -04002474 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002475
2476 return ret;
2477}
2478
Josef Bacik4f2de97a2012-03-07 16:20:05 -05002479void attach_extent_buffer_page(struct extent_buffer *eb, struct page *page)
2480{
2481 if (!PagePrivate(page)) {
2482 SetPagePrivate(page);
2483 page_cache_get(page);
2484 set_page_private(page, (unsigned long)eb);
2485 } else {
2486 WARN_ON(page->private != (unsigned long)eb);
2487 }
2488}
2489
Chris Masond1310b22008-01-24 16:13:08 -05002490void set_page_extent_mapped(struct page *page)
2491{
2492 if (!PagePrivate(page)) {
2493 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05002494 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04002495 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05002496 }
2497}
2498
Chris Masond1310b22008-01-24 16:13:08 -05002499/*
2500 * basic readpage implementation. Locked extent state structs are inserted
2501 * into the tree that are removed when the IO is done (by the end_io
2502 * handlers)
2503 */
2504static int __extent_read_full_page(struct extent_io_tree *tree,
2505 struct page *page,
2506 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002507 struct bio **bio, int mirror_num,
2508 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002509{
2510 struct inode *inode = page->mapping->host;
2511 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2512 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2513 u64 end;
2514 u64 cur = start;
2515 u64 extent_offset;
2516 u64 last_byte = i_size_read(inode);
2517 u64 block_start;
2518 u64 cur_end;
2519 sector_t sector;
2520 struct extent_map *em;
2521 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002522 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05002523 int ret;
2524 int nr = 0;
David Sterba306e16c2011-04-19 14:29:38 +02002525 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002526 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002527 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002528 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002529 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002530
2531 set_page_extent_mapped(page);
2532
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002533 if (!PageUptodate(page)) {
2534 if (cleancache_get_page(page) == 0) {
2535 BUG_ON(blocksize != PAGE_SIZE);
2536 goto out;
2537 }
2538 }
2539
Chris Masond1310b22008-01-24 16:13:08 -05002540 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002541 while (1) {
2542 lock_extent(tree, start, end, GFP_NOFS);
2543 ordered = btrfs_lookup_ordered_extent(inode, start);
2544 if (!ordered)
2545 break;
2546 unlock_extent(tree, start, end, GFP_NOFS);
2547 btrfs_start_ordered_extent(inode, ordered, 1);
2548 btrfs_put_ordered_extent(ordered);
2549 }
Chris Masond1310b22008-01-24 16:13:08 -05002550
Chris Masonc8b97812008-10-29 14:49:59 -04002551 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2552 char *userpage;
2553 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2554
2555 if (zero_offset) {
2556 iosize = PAGE_CACHE_SIZE - zero_offset;
2557 userpage = kmap_atomic(page, KM_USER0);
2558 memset(userpage + zero_offset, 0, iosize);
2559 flush_dcache_page(page);
2560 kunmap_atomic(userpage, KM_USER0);
2561 }
2562 }
Chris Masond1310b22008-01-24 16:13:08 -05002563 while (cur <= end) {
2564 if (cur >= last_byte) {
2565 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002566 struct extent_state *cached = NULL;
2567
David Sterba306e16c2011-04-19 14:29:38 +02002568 iosize = PAGE_CACHE_SIZE - pg_offset;
Chris Masond1310b22008-01-24 16:13:08 -05002569 userpage = kmap_atomic(page, KM_USER0);
David Sterba306e16c2011-04-19 14:29:38 +02002570 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002571 flush_dcache_page(page);
2572 kunmap_atomic(userpage, KM_USER0);
2573 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002574 &cached, GFP_NOFS);
2575 unlock_extent_cached(tree, cur, cur + iosize - 1,
2576 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002577 break;
2578 }
David Sterba306e16c2011-04-19 14:29:38 +02002579 em = get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002580 end - cur + 1, 0);
David Sterbac7040052011-04-19 18:00:01 +02002581 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002582 SetPageError(page);
2583 unlock_extent(tree, cur, end, GFP_NOFS);
2584 break;
2585 }
Chris Masond1310b22008-01-24 16:13:08 -05002586 extent_offset = cur - em->start;
2587 BUG_ON(extent_map_end(em) <= cur);
2588 BUG_ON(end < cur);
2589
Li Zefan261507a02010-12-17 14:21:50 +08002590 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
Chris Masonc8b97812008-10-29 14:49:59 -04002591 this_bio_flag = EXTENT_BIO_COMPRESSED;
Li Zefan261507a02010-12-17 14:21:50 +08002592 extent_set_compress_type(&this_bio_flag,
2593 em->compress_type);
2594 }
Chris Masonc8b97812008-10-29 14:49:59 -04002595
Chris Masond1310b22008-01-24 16:13:08 -05002596 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2597 cur_end = min(extent_map_end(em) - 1, end);
2598 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002599 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2600 disk_io_size = em->block_len;
2601 sector = em->block_start >> 9;
2602 } else {
2603 sector = (em->block_start + extent_offset) >> 9;
2604 disk_io_size = iosize;
2605 }
Chris Masond1310b22008-01-24 16:13:08 -05002606 bdev = em->bdev;
2607 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002608 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2609 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002610 free_extent_map(em);
2611 em = NULL;
2612
2613 /* we've found a hole, just zero and go on */
2614 if (block_start == EXTENT_MAP_HOLE) {
2615 char *userpage;
Arne Jansen507903b2011-04-06 10:02:20 +00002616 struct extent_state *cached = NULL;
2617
Chris Masond1310b22008-01-24 16:13:08 -05002618 userpage = kmap_atomic(page, KM_USER0);
David Sterba306e16c2011-04-19 14:29:38 +02002619 memset(userpage + pg_offset, 0, iosize);
Chris Masond1310b22008-01-24 16:13:08 -05002620 flush_dcache_page(page);
2621 kunmap_atomic(userpage, KM_USER0);
2622
2623 set_extent_uptodate(tree, cur, cur + iosize - 1,
Arne Jansen507903b2011-04-06 10:02:20 +00002624 &cached, GFP_NOFS);
2625 unlock_extent_cached(tree, cur, cur + iosize - 1,
2626 &cached, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002627 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002628 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002629 continue;
2630 }
2631 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002632 if (test_range_bit(tree, cur, cur_end,
2633 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002634 check_page_uptodate(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002635 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2636 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002637 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002638 continue;
2639 }
Chris Mason70dec802008-01-29 09:59:12 -05002640 /* we have an inline extent but it didn't get marked up
2641 * to date. Error out
2642 */
2643 if (block_start == EXTENT_MAP_INLINE) {
2644 SetPageError(page);
2645 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2646 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002647 pg_offset += iosize;
Chris Mason70dec802008-01-29 09:59:12 -05002648 continue;
2649 }
Chris Masond1310b22008-01-24 16:13:08 -05002650
2651 ret = 0;
2652 if (tree->ops && tree->ops->readpage_io_hook) {
2653 ret = tree->ops->readpage_io_hook(page, cur,
2654 cur + iosize - 1);
2655 }
2656 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002657 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2658 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002659 ret = submit_extent_page(READ, tree, page,
David Sterba306e16c2011-04-19 14:29:38 +02002660 sector, disk_io_size, pg_offset,
Chris Mason89642222008-07-24 09:41:53 -04002661 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002662 end_bio_extent_readpage, mirror_num,
2663 *bio_flags,
2664 this_bio_flag);
Chris Mason89642222008-07-24 09:41:53 -04002665 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002666 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002667 }
2668 if (ret)
2669 SetPageError(page);
2670 cur = cur + iosize;
David Sterba306e16c2011-04-19 14:29:38 +02002671 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002672 }
Dan Magenheimer90a887c2011-05-26 10:01:56 -06002673out:
Chris Masond1310b22008-01-24 16:13:08 -05002674 if (!nr) {
2675 if (!PageError(page))
2676 SetPageUptodate(page);
2677 unlock_page(page);
2678 }
2679 return 0;
2680}
2681
2682int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002683 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05002684{
2685 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002686 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002687 int ret;
2688
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002689 ret = __extent_read_full_page(tree, page, get_extent, &bio, mirror_num,
Chris Masonc8b97812008-10-29 14:49:59 -04002690 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002691 if (bio)
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02002692 ret = submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002693 return ret;
2694}
Chris Masond1310b22008-01-24 16:13:08 -05002695
Chris Mason11c83492009-04-20 15:50:09 -04002696static noinline void update_nr_written(struct page *page,
2697 struct writeback_control *wbc,
2698 unsigned long nr_written)
2699{
2700 wbc->nr_to_write -= nr_written;
2701 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2702 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2703 page->mapping->writeback_index = page->index + nr_written;
2704}
2705
Chris Masond1310b22008-01-24 16:13:08 -05002706/*
2707 * the writepage semantics are similar to regular writepage. extent
2708 * records are inserted to lock ranges in the tree, and as dirty areas
2709 * are found, they are marked writeback. Then the lock bits are removed
2710 * and the end_io handler clears the writeback ranges
2711 */
2712static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2713 void *data)
2714{
2715 struct inode *inode = page->mapping->host;
2716 struct extent_page_data *epd = data;
2717 struct extent_io_tree *tree = epd->tree;
2718 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2719 u64 delalloc_start;
2720 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2721 u64 end;
2722 u64 cur = start;
2723 u64 extent_offset;
2724 u64 last_byte = i_size_read(inode);
2725 u64 block_start;
2726 u64 iosize;
2727 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002728 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002729 struct extent_map *em;
2730 struct block_device *bdev;
2731 int ret;
2732 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002733 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002734 size_t blocksize;
2735 loff_t i_size = i_size_read(inode);
2736 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2737 u64 nr_delalloc;
2738 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002739 int page_started;
2740 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002741 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002742 unsigned long nr_written = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002743 bool fill_delalloc = true;
Chris Masond1310b22008-01-24 16:13:08 -05002744
Chris Masonffbd5172009-04-20 15:50:09 -04002745 if (wbc->sync_mode == WB_SYNC_ALL)
Jens Axboe721a9602011-03-09 11:56:30 +01002746 write_flags = WRITE_SYNC;
Chris Masonffbd5172009-04-20 15:50:09 -04002747 else
2748 write_flags = WRITE;
2749
liubo1abe9b82011-03-24 11:18:59 +00002750 trace___extent_writepage(page, inode, wbc);
2751
Chris Masond1310b22008-01-24 16:13:08 -05002752 WARN_ON(!PageLocked(page));
Chris Masonbf0da8c2011-11-04 12:29:37 -04002753
2754 ClearPageError(page);
2755
Chris Mason7f3c74f2008-07-18 12:01:11 -04002756 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002757 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002758 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002759 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002760 unlock_page(page);
2761 return 0;
2762 }
2763
2764 if (page->index == end_index) {
2765 char *userpage;
2766
Chris Masond1310b22008-01-24 16:13:08 -05002767 userpage = kmap_atomic(page, KM_USER0);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002768 memset(userpage + pg_offset, 0,
2769 PAGE_CACHE_SIZE - pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002770 kunmap_atomic(userpage, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002771 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002772 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002773 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002774
2775 set_page_extent_mapped(page);
2776
Josef Bacik9e487102011-08-01 12:08:18 -04002777 if (!tree->ops || !tree->ops->fill_delalloc)
2778 fill_delalloc = false;
2779
Chris Masond1310b22008-01-24 16:13:08 -05002780 delalloc_start = start;
2781 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002782 page_started = 0;
Josef Bacik9e487102011-08-01 12:08:18 -04002783 if (!epd->extent_locked && fill_delalloc) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002784 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002785 /*
2786 * make sure the wbc mapping index is at least updated
2787 * to this page.
2788 */
2789 update_nr_written(page, wbc, 0);
2790
Chris Masond3977122009-01-05 21:25:51 -05002791 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002792 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002793 page,
2794 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002795 &delalloc_end,
2796 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002797 if (nr_delalloc == 0) {
2798 delalloc_start = delalloc_end + 1;
2799 continue;
2800 }
Tsutomu Itoh013bd4c2012-02-16 10:11:40 +09002801 ret = tree->ops->fill_delalloc(inode, page,
2802 delalloc_start,
2803 delalloc_end,
2804 &page_started,
2805 &nr_written);
2806 BUG_ON(ret);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002807 /*
2808 * delalloc_end is already one less than the total
2809 * length, so we don't subtract one from
2810 * PAGE_CACHE_SIZE
2811 */
2812 delalloc_to_write += (delalloc_end - delalloc_start +
2813 PAGE_CACHE_SIZE) >>
2814 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002815 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002816 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002817 if (wbc->nr_to_write < delalloc_to_write) {
2818 int thresh = 8192;
2819
2820 if (delalloc_to_write < thresh * 2)
2821 thresh = delalloc_to_write;
2822 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2823 thresh);
2824 }
Chris Masonc8b97812008-10-29 14:49:59 -04002825
Chris Mason771ed682008-11-06 22:02:51 -05002826 /* did the fill delalloc function already unlock and start
2827 * the IO?
2828 */
2829 if (page_started) {
2830 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002831 /*
2832 * we've unlocked the page, so we can't update
2833 * the mapping's writeback index, just update
2834 * nr_to_write.
2835 */
2836 wbc->nr_to_write -= nr_written;
2837 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002838 }
Chris Masonc8b97812008-10-29 14:49:59 -04002839 }
Chris Mason247e7432008-07-17 12:53:51 -04002840 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002841 ret = tree->ops->writepage_start_hook(page, start,
2842 page_end);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002843 if (ret) {
2844 /* Fixup worker will requeue */
2845 if (ret == -EBUSY)
2846 wbc->pages_skipped++;
2847 else
2848 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002849 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002850 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002851 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002852 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002853 }
2854 }
2855
Chris Mason11c83492009-04-20 15:50:09 -04002856 /*
2857 * we don't want to touch the inode after unlocking the page,
2858 * so we update the mapping writeback index now
2859 */
2860 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002861
Chris Masond1310b22008-01-24 16:13:08 -05002862 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002863 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002864 if (tree->ops && tree->ops->writepage_end_io_hook)
2865 tree->ops->writepage_end_io_hook(page, start,
2866 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002867 goto done;
2868 }
2869
Chris Masond1310b22008-01-24 16:13:08 -05002870 blocksize = inode->i_sb->s_blocksize;
2871
2872 while (cur <= end) {
2873 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002874 if (tree->ops && tree->ops->writepage_end_io_hook)
2875 tree->ops->writepage_end_io_hook(page, cur,
2876 page_end, NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002877 break;
2878 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002879 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002880 end - cur + 1, 1);
David Sterbac7040052011-04-19 18:00:01 +02002881 if (IS_ERR_OR_NULL(em)) {
Chris Masond1310b22008-01-24 16:13:08 -05002882 SetPageError(page);
2883 break;
2884 }
2885
2886 extent_offset = cur - em->start;
2887 BUG_ON(extent_map_end(em) <= cur);
2888 BUG_ON(end < cur);
2889 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2890 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2891 sector = (em->block_start + extent_offset) >> 9;
2892 bdev = em->bdev;
2893 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002894 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002895 free_extent_map(em);
2896 em = NULL;
2897
Chris Masonc8b97812008-10-29 14:49:59 -04002898 /*
2899 * compressed and inline extents are written through other
2900 * paths in the FS
2901 */
2902 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002903 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002904 /*
2905 * end_io notification does not happen here for
2906 * compressed extents
2907 */
2908 if (!compressed && tree->ops &&
2909 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002910 tree->ops->writepage_end_io_hook(page, cur,
2911 cur + iosize - 1,
2912 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002913 else if (compressed) {
2914 /* we don't want to end_page_writeback on
2915 * a compressed extent. this happens
2916 * elsewhere
2917 */
2918 nr++;
2919 }
2920
2921 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002922 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002923 continue;
2924 }
Chris Masond1310b22008-01-24 16:13:08 -05002925 /* leave this out until we have a page_mkwrite call */
2926 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04002927 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002928 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002929 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002930 continue;
2931 }
Chris Masonc8b97812008-10-29 14:49:59 -04002932
Chris Masond1310b22008-01-24 16:13:08 -05002933 if (tree->ops && tree->ops->writepage_io_hook) {
2934 ret = tree->ops->writepage_io_hook(page, cur,
2935 cur + iosize - 1);
2936 } else {
2937 ret = 0;
2938 }
Chris Mason1259ab72008-05-12 13:39:03 -04002939 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002940 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002941 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002942 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002943
Chris Masond1310b22008-01-24 16:13:08 -05002944 set_range_writeback(tree, cur, cur + iosize - 1);
2945 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05002946 printk(KERN_ERR "btrfs warning page %lu not "
2947 "writeback, cur %llu end %llu\n",
2948 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05002949 (unsigned long long)end);
2950 }
2951
Chris Masonffbd5172009-04-20 15:50:09 -04002952 ret = submit_extent_page(write_flags, tree, page,
2953 sector, iosize, pg_offset,
2954 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04002955 end_bio_extent_writepage,
2956 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002957 if (ret)
2958 SetPageError(page);
2959 }
2960 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002961 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002962 nr++;
2963 }
2964done:
2965 if (nr == 0) {
2966 /* make sure the mapping tag for page dirty gets cleared */
2967 set_page_writeback(page);
2968 end_page_writeback(page);
2969 }
Chris Masond1310b22008-01-24 16:13:08 -05002970 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002971
Chris Mason11c83492009-04-20 15:50:09 -04002972done_unlocked:
2973
Chris Mason2c64c532009-09-02 15:04:12 -04002974 /* drop our reference on any cached states */
2975 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05002976 return 0;
2977}
2978
Josef Bacik0b32f4b2012-03-13 09:38:00 -04002979static int eb_wait(void *word)
2980{
2981 io_schedule();
2982 return 0;
2983}
2984
2985static void wait_on_extent_buffer_writeback(struct extent_buffer *eb)
2986{
2987 wait_on_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK, eb_wait,
2988 TASK_UNINTERRUPTIBLE);
2989}
2990
2991static int lock_extent_buffer_for_io(struct extent_buffer *eb,
2992 struct btrfs_fs_info *fs_info,
2993 struct extent_page_data *epd)
2994{
2995 unsigned long i, num_pages;
2996 int flush = 0;
2997 int ret = 0;
2998
2999 if (!btrfs_try_tree_write_lock(eb)) {
3000 flush = 1;
3001 flush_write_bio(epd);
3002 btrfs_tree_lock(eb);
3003 }
3004
3005 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3006 btrfs_tree_unlock(eb);
3007 if (!epd->sync_io)
3008 return 0;
3009 if (!flush) {
3010 flush_write_bio(epd);
3011 flush = 1;
3012 }
3013 wait_on_extent_buffer_writeback(eb);
3014 btrfs_tree_lock(eb);
3015 if (test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags)) {
3016 printk(KERN_ERR "Um, ok?\n");
3017 btrfs_tree_unlock(eb);
3018 return 0;
3019 }
3020 }
3021
3022 if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3023 set_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3024 btrfs_set_header_flag(eb, BTRFS_HEADER_FLAG_WRITTEN);
3025 spin_lock(&fs_info->delalloc_lock);
3026 if (fs_info->dirty_metadata_bytes >= eb->len)
3027 fs_info->dirty_metadata_bytes -= eb->len;
3028 else
3029 WARN_ON(1);
3030 spin_unlock(&fs_info->delalloc_lock);
3031 ret = 1;
3032 }
3033
3034 btrfs_tree_unlock(eb);
3035
3036 if (!ret)
3037 return ret;
3038
3039 num_pages = num_extent_pages(eb->start, eb->len);
3040 for (i = 0; i < num_pages; i++) {
3041 struct page *p = extent_buffer_page(eb, i);
3042
3043 if (!trylock_page(p)) {
3044 if (!flush) {
3045 flush_write_bio(epd);
3046 flush = 1;
3047 }
3048 lock_page(p);
3049 }
3050 }
3051
3052 return ret;
3053}
3054
3055static void end_extent_buffer_writeback(struct extent_buffer *eb)
3056{
3057 clear_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags);
3058 smp_mb__after_clear_bit();
3059 wake_up_bit(&eb->bflags, EXTENT_BUFFER_WRITEBACK);
3060}
3061
3062static void end_bio_extent_buffer_writepage(struct bio *bio, int err)
3063{
3064 int uptodate = err == 0;
3065 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
3066 struct extent_buffer *eb;
3067 int done;
3068
3069 do {
3070 struct page *page = bvec->bv_page;
3071
3072 bvec--;
3073 eb = (struct extent_buffer *)page->private;
3074 BUG_ON(!eb);
3075 done = atomic_dec_and_test(&eb->io_pages);
3076
3077 if (!uptodate || test_bit(EXTENT_BUFFER_IOERR, &eb->bflags)) {
3078 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3079 ClearPageUptodate(page);
3080 SetPageError(page);
3081 }
3082
3083 end_page_writeback(page);
3084
3085 if (!done)
3086 continue;
3087
3088 end_extent_buffer_writeback(eb);
3089 } while (bvec >= bio->bi_io_vec);
3090
3091 bio_put(bio);
3092
3093}
3094
3095static int write_one_eb(struct extent_buffer *eb,
3096 struct btrfs_fs_info *fs_info,
3097 struct writeback_control *wbc,
3098 struct extent_page_data *epd)
3099{
3100 struct block_device *bdev = fs_info->fs_devices->latest_bdev;
3101 u64 offset = eb->start;
3102 unsigned long i, num_pages;
3103 int rw = (epd->sync_io ? WRITE_SYNC : WRITE);
3104 int ret;
3105
3106 clear_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3107 num_pages = num_extent_pages(eb->start, eb->len);
3108 atomic_set(&eb->io_pages, num_pages);
3109 for (i = 0; i < num_pages; i++) {
3110 struct page *p = extent_buffer_page(eb, i);
3111
3112 clear_page_dirty_for_io(p);
3113 set_page_writeback(p);
3114 ret = submit_extent_page(rw, eb->tree, p, offset >> 9,
3115 PAGE_CACHE_SIZE, 0, bdev, &epd->bio,
3116 -1, end_bio_extent_buffer_writepage,
3117 0, 0, 0);
3118 if (ret) {
3119 set_bit(EXTENT_BUFFER_IOERR, &eb->bflags);
3120 SetPageError(p);
3121 if (atomic_sub_and_test(num_pages - i, &eb->io_pages))
3122 end_extent_buffer_writeback(eb);
3123 ret = -EIO;
3124 break;
3125 }
3126 offset += PAGE_CACHE_SIZE;
3127 update_nr_written(p, wbc, 1);
3128 unlock_page(p);
3129 }
3130
3131 if (unlikely(ret)) {
3132 for (; i < num_pages; i++) {
3133 struct page *p = extent_buffer_page(eb, i);
3134 unlock_page(p);
3135 }
3136 }
3137
3138 return ret;
3139}
3140
3141int btree_write_cache_pages(struct address_space *mapping,
3142 struct writeback_control *wbc)
3143{
3144 struct extent_io_tree *tree = &BTRFS_I(mapping->host)->io_tree;
3145 struct btrfs_fs_info *fs_info = BTRFS_I(mapping->host)->root->fs_info;
3146 struct extent_buffer *eb, *prev_eb = NULL;
3147 struct extent_page_data epd = {
3148 .bio = NULL,
3149 .tree = tree,
3150 .extent_locked = 0,
3151 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
3152 };
3153 int ret = 0;
3154 int done = 0;
3155 int nr_to_write_done = 0;
3156 struct pagevec pvec;
3157 int nr_pages;
3158 pgoff_t index;
3159 pgoff_t end; /* Inclusive */
3160 int scanned = 0;
3161 int tag;
3162
3163 pagevec_init(&pvec, 0);
3164 if (wbc->range_cyclic) {
3165 index = mapping->writeback_index; /* Start from prev offset */
3166 end = -1;
3167 } else {
3168 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3169 end = wbc->range_end >> PAGE_CACHE_SHIFT;
3170 scanned = 1;
3171 }
3172 if (wbc->sync_mode == WB_SYNC_ALL)
3173 tag = PAGECACHE_TAG_TOWRITE;
3174 else
3175 tag = PAGECACHE_TAG_DIRTY;
3176retry:
3177 if (wbc->sync_mode == WB_SYNC_ALL)
3178 tag_pages_for_writeback(mapping, index, end);
3179 while (!done && !nr_to_write_done && (index <= end) &&
3180 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3181 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
3182 unsigned i;
3183
3184 scanned = 1;
3185 for (i = 0; i < nr_pages; i++) {
3186 struct page *page = pvec.pages[i];
3187
3188 if (!PagePrivate(page))
3189 continue;
3190
3191 if (!wbc->range_cyclic && page->index > end) {
3192 done = 1;
3193 break;
3194 }
3195
3196 eb = (struct extent_buffer *)page->private;
3197 if (!eb) {
3198 WARN_ON(1);
3199 continue;
3200 }
3201
3202 if (eb == prev_eb)
3203 continue;
3204
3205 if (!atomic_inc_not_zero(&eb->refs)) {
3206 WARN_ON(1);
3207 continue;
3208 }
3209
3210 prev_eb = eb;
3211 ret = lock_extent_buffer_for_io(eb, fs_info, &epd);
3212 if (!ret) {
3213 free_extent_buffer(eb);
3214 continue;
3215 }
3216
3217 ret = write_one_eb(eb, fs_info, wbc, &epd);
3218 if (ret) {
3219 done = 1;
3220 free_extent_buffer(eb);
3221 break;
3222 }
3223 free_extent_buffer(eb);
3224
3225 /*
3226 * the filesystem may choose to bump up nr_to_write.
3227 * We have to make sure to honor the new nr_to_write
3228 * at any time
3229 */
3230 nr_to_write_done = wbc->nr_to_write <= 0;
3231 }
3232 pagevec_release(&pvec);
3233 cond_resched();
3234 }
3235 if (!scanned && !done) {
3236 /*
3237 * We hit the last page and there is more work to be done: wrap
3238 * back to the start of the file
3239 */
3240 scanned = 1;
3241 index = 0;
3242 goto retry;
3243 }
3244 flush_write_bio(&epd);
3245 return ret;
3246}
3247
Chris Masond1310b22008-01-24 16:13:08 -05003248/**
Chris Mason4bef0842008-09-08 11:18:08 -04003249 * 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 -05003250 * @mapping: address space structure to write
3251 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
3252 * @writepage: function called for each page
3253 * @data: data passed to writepage function
3254 *
3255 * If a page is already under I/O, write_cache_pages() skips it, even
3256 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
3257 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
3258 * and msync() need to guarantee that all the data which was dirty at the time
3259 * the call was made get new I/O started against them. If wbc->sync_mode is
3260 * WB_SYNC_ALL then we were called for data integrity and we must wait for
3261 * existing IO to complete.
3262 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05003263static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04003264 struct address_space *mapping,
3265 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003266 writepage_t writepage, void *data,
3267 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05003268{
Chris Masond1310b22008-01-24 16:13:08 -05003269 int ret = 0;
3270 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003271 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003272 struct pagevec pvec;
3273 int nr_pages;
3274 pgoff_t index;
3275 pgoff_t end; /* Inclusive */
3276 int scanned = 0;
Josef Bacikf7aaa062011-07-15 21:26:38 +00003277 int tag;
Chris Masond1310b22008-01-24 16:13:08 -05003278
Chris Masond1310b22008-01-24 16:13:08 -05003279 pagevec_init(&pvec, 0);
3280 if (wbc->range_cyclic) {
3281 index = mapping->writeback_index; /* Start from prev offset */
3282 end = -1;
3283 } else {
3284 index = wbc->range_start >> PAGE_CACHE_SHIFT;
3285 end = wbc->range_end >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003286 scanned = 1;
3287 }
Josef Bacikf7aaa062011-07-15 21:26:38 +00003288 if (wbc->sync_mode == WB_SYNC_ALL)
3289 tag = PAGECACHE_TAG_TOWRITE;
3290 else
3291 tag = PAGECACHE_TAG_DIRTY;
Chris Masond1310b22008-01-24 16:13:08 -05003292retry:
Josef Bacikf7aaa062011-07-15 21:26:38 +00003293 if (wbc->sync_mode == WB_SYNC_ALL)
3294 tag_pages_for_writeback(mapping, index, end);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003295 while (!done && !nr_to_write_done && (index <= end) &&
Josef Bacikf7aaa062011-07-15 21:26:38 +00003296 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
3297 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05003298 unsigned i;
3299
3300 scanned = 1;
3301 for (i = 0; i < nr_pages; i++) {
3302 struct page *page = pvec.pages[i];
3303
3304 /*
3305 * At this point we hold neither mapping->tree_lock nor
3306 * lock on the page itself: the page may be truncated or
3307 * invalidated (changing page->mapping to NULL), or even
3308 * swizzled back from swapper_space to tmpfs file
3309 * mapping
3310 */
Chris Mason01d658f2011-11-01 10:08:06 -04003311 if (tree->ops &&
3312 tree->ops->write_cache_pages_lock_hook) {
3313 tree->ops->write_cache_pages_lock_hook(page,
3314 data, flush_fn);
3315 } else {
3316 if (!trylock_page(page)) {
3317 flush_fn(data);
3318 lock_page(page);
3319 }
3320 }
Chris Masond1310b22008-01-24 16:13:08 -05003321
3322 if (unlikely(page->mapping != mapping)) {
3323 unlock_page(page);
3324 continue;
3325 }
3326
3327 if (!wbc->range_cyclic && page->index > end) {
3328 done = 1;
3329 unlock_page(page);
3330 continue;
3331 }
3332
Chris Masond2c3f4f2008-11-19 12:44:22 -05003333 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05003334 if (PageWriteback(page))
3335 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05003336 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003337 }
Chris Masond1310b22008-01-24 16:13:08 -05003338
3339 if (PageWriteback(page) ||
3340 !clear_page_dirty_for_io(page)) {
3341 unlock_page(page);
3342 continue;
3343 }
3344
3345 ret = (*writepage)(page, wbc, data);
3346
3347 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
3348 unlock_page(page);
3349 ret = 0;
3350 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003351 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05003352 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04003353
3354 /*
3355 * the filesystem may choose to bump up nr_to_write.
3356 * We have to make sure to honor the new nr_to_write
3357 * at any time
3358 */
3359 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05003360 }
3361 pagevec_release(&pvec);
3362 cond_resched();
3363 }
3364 if (!scanned && !done) {
3365 /*
3366 * We hit the last page and there is more work to be done: wrap
3367 * back to the start of the file
3368 */
3369 scanned = 1;
3370 index = 0;
3371 goto retry;
3372 }
Chris Masond1310b22008-01-24 16:13:08 -05003373 return ret;
3374}
Chris Masond1310b22008-01-24 16:13:08 -05003375
Chris Masonffbd5172009-04-20 15:50:09 -04003376static void flush_epd_write_bio(struct extent_page_data *epd)
3377{
3378 if (epd->bio) {
3379 if (epd->sync_io)
3380 submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
3381 else
3382 submit_one_bio(WRITE, epd->bio, 0, 0);
3383 epd->bio = NULL;
3384 }
3385}
3386
Chris Masond2c3f4f2008-11-19 12:44:22 -05003387static noinline void flush_write_bio(void *data)
3388{
3389 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04003390 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05003391}
3392
Chris Masond1310b22008-01-24 16:13:08 -05003393int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
3394 get_extent_t *get_extent,
3395 struct writeback_control *wbc)
3396{
3397 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05003398 struct extent_page_data epd = {
3399 .bio = NULL,
3400 .tree = tree,
3401 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003402 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003403 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05003404 };
Chris Masond1310b22008-01-24 16:13:08 -05003405
Chris Masond1310b22008-01-24 16:13:08 -05003406 ret = __extent_writepage(page, wbc, &epd);
3407
Chris Masonffbd5172009-04-20 15:50:09 -04003408 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003409 return ret;
3410}
Chris Masond1310b22008-01-24 16:13:08 -05003411
Chris Mason771ed682008-11-06 22:02:51 -05003412int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
3413 u64 start, u64 end, get_extent_t *get_extent,
3414 int mode)
3415{
3416 int ret = 0;
3417 struct address_space *mapping = inode->i_mapping;
3418 struct page *page;
3419 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
3420 PAGE_CACHE_SHIFT;
3421
3422 struct extent_page_data epd = {
3423 .bio = NULL,
3424 .tree = tree,
3425 .get_extent = get_extent,
3426 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04003427 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05003428 };
3429 struct writeback_control wbc_writepages = {
Chris Mason771ed682008-11-06 22:02:51 -05003430 .sync_mode = mode,
Chris Mason771ed682008-11-06 22:02:51 -05003431 .nr_to_write = nr_pages * 2,
3432 .range_start = start,
3433 .range_end = end + 1,
3434 };
3435
Chris Masond3977122009-01-05 21:25:51 -05003436 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05003437 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
3438 if (clear_page_dirty_for_io(page))
3439 ret = __extent_writepage(page, &wbc_writepages, &epd);
3440 else {
3441 if (tree->ops && tree->ops->writepage_end_io_hook)
3442 tree->ops->writepage_end_io_hook(page, start,
3443 start + PAGE_CACHE_SIZE - 1,
3444 NULL, 1);
3445 unlock_page(page);
3446 }
3447 page_cache_release(page);
3448 start += PAGE_CACHE_SIZE;
3449 }
3450
Chris Masonffbd5172009-04-20 15:50:09 -04003451 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05003452 return ret;
3453}
Chris Masond1310b22008-01-24 16:13:08 -05003454
3455int extent_writepages(struct extent_io_tree *tree,
3456 struct address_space *mapping,
3457 get_extent_t *get_extent,
3458 struct writeback_control *wbc)
3459{
3460 int ret = 0;
3461 struct extent_page_data epd = {
3462 .bio = NULL,
3463 .tree = tree,
3464 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05003465 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04003466 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05003467 };
3468
Chris Mason4bef0842008-09-08 11:18:08 -04003469 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05003470 __extent_writepage, &epd,
3471 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04003472 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05003473 return ret;
3474}
Chris Masond1310b22008-01-24 16:13:08 -05003475
3476int extent_readpages(struct extent_io_tree *tree,
3477 struct address_space *mapping,
3478 struct list_head *pages, unsigned nr_pages,
3479 get_extent_t get_extent)
3480{
3481 struct bio *bio = NULL;
3482 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04003483 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003484
Chris Masond1310b22008-01-24 16:13:08 -05003485 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
3486 struct page *page = list_entry(pages->prev, struct page, lru);
3487
3488 prefetchw(&page->flags);
3489 list_del(&page->lru);
Nick Piggin28ecb6092010-03-17 13:31:04 +00003490 if (!add_to_page_cache_lru(page, mapping,
Itaru Kitayama43e817a2011-04-25 19:43:51 -04003491 page->index, GFP_NOFS)) {
Chris Masonf1885912008-04-09 16:28:12 -04003492 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04003493 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003494 }
3495 page_cache_release(page);
3496 }
Chris Masond1310b22008-01-24 16:13:08 -05003497 BUG_ON(!list_empty(pages));
3498 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04003499 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05003500 return 0;
3501}
Chris Masond1310b22008-01-24 16:13:08 -05003502
3503/*
3504 * basic invalidatepage code, this waits on any locked or writeback
3505 * ranges corresponding to the page, and then deletes any extent state
3506 * records from the tree
3507 */
3508int extent_invalidatepage(struct extent_io_tree *tree,
3509 struct page *page, unsigned long offset)
3510{
Josef Bacik2ac55d42010-02-03 19:33:23 +00003511 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003512 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
3513 u64 end = start + PAGE_CACHE_SIZE - 1;
3514 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
3515
Chris Masond3977122009-01-05 21:25:51 -05003516 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05003517 if (start > end)
3518 return 0;
3519
Josef Bacik2ac55d42010-02-03 19:33:23 +00003520 lock_extent_bits(tree, start, end, 0, &cached_state, GFP_NOFS);
Chris Mason1edbb732009-09-02 13:24:36 -04003521 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003522 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04003523 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3524 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003525 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05003526 return 0;
3527}
Chris Masond1310b22008-01-24 16:13:08 -05003528
3529/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04003530 * a helper for releasepage, this tests for areas of the page that
3531 * are locked or under IO and drops the related state bits if it is safe
3532 * to drop the page.
3533 */
3534int try_release_extent_state(struct extent_map_tree *map,
3535 struct extent_io_tree *tree, struct page *page,
3536 gfp_t mask)
3537{
3538 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3539 u64 end = start + PAGE_CACHE_SIZE - 1;
3540 int ret = 1;
3541
Chris Mason211f90e2008-07-18 11:56:15 -04003542 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04003543 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04003544 ret = 0;
3545 else {
3546 if ((mask & GFP_NOFS) == GFP_NOFS)
3547 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04003548 /*
3549 * at this point we can safely clear everything except the
3550 * locked bit and the nodatasum bit
3551 */
Chris Masone3f24cc2011-02-14 12:52:08 -05003552 ret = clear_extent_bit(tree, start, end,
Chris Mason11ef1602009-09-23 20:28:46 -04003553 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
3554 0, 0, NULL, mask);
Chris Masone3f24cc2011-02-14 12:52:08 -05003555
3556 /* if clear_extent_bit failed for enomem reasons,
3557 * we can't allow the release to continue.
3558 */
3559 if (ret < 0)
3560 ret = 0;
3561 else
3562 ret = 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003563 }
3564 return ret;
3565}
Chris Mason7b13b7b2008-04-18 10:29:50 -04003566
3567/*
Chris Masond1310b22008-01-24 16:13:08 -05003568 * a helper for releasepage. As long as there are no locked extents
3569 * in the range corresponding to the page, both state records and extent
3570 * map records are removed
3571 */
3572int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05003573 struct extent_io_tree *tree, struct page *page,
3574 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05003575{
3576 struct extent_map *em;
3577 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
3578 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04003579
Chris Mason70dec802008-01-29 09:59:12 -05003580 if ((mask & __GFP_WAIT) &&
3581 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05003582 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05003583 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05003584 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04003585 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05003586 em = lookup_extent_mapping(map, start, len);
Tsutomu Itoh285190d2012-02-16 16:23:58 +09003587 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -04003588 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003589 break;
3590 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04003591 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
3592 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04003593 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003594 free_extent_map(em);
3595 break;
3596 }
3597 if (!test_range_bit(tree, em->start,
3598 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04003599 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04003600 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05003601 remove_extent_mapping(map, em);
3602 /* once for the rb tree */
3603 free_extent_map(em);
3604 }
3605 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04003606 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05003607
3608 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05003609 free_extent_map(em);
3610 }
Chris Masond1310b22008-01-24 16:13:08 -05003611 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04003612 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003613}
Chris Masond1310b22008-01-24 16:13:08 -05003614
Chris Masonec29ed52011-02-23 16:23:20 -05003615/*
3616 * helper function for fiemap, which doesn't want to see any holes.
3617 * This maps until we find something past 'last'
3618 */
3619static struct extent_map *get_extent_skip_holes(struct inode *inode,
3620 u64 offset,
3621 u64 last,
3622 get_extent_t *get_extent)
3623{
3624 u64 sectorsize = BTRFS_I(inode)->root->sectorsize;
3625 struct extent_map *em;
3626 u64 len;
3627
3628 if (offset >= last)
3629 return NULL;
3630
3631 while(1) {
3632 len = last - offset;
3633 if (len == 0)
3634 break;
3635 len = (len + sectorsize - 1) & ~(sectorsize - 1);
3636 em = get_extent(inode, NULL, 0, offset, len, 0);
David Sterbac7040052011-04-19 18:00:01 +02003637 if (IS_ERR_OR_NULL(em))
Chris Masonec29ed52011-02-23 16:23:20 -05003638 return em;
3639
3640 /* if this isn't a hole return it */
3641 if (!test_bit(EXTENT_FLAG_VACANCY, &em->flags) &&
3642 em->block_start != EXTENT_MAP_HOLE) {
3643 return em;
3644 }
3645
3646 /* this is a hole, advance to the next extent */
3647 offset = extent_map_end(em);
3648 free_extent_map(em);
3649 if (offset >= last)
3650 break;
3651 }
3652 return NULL;
3653}
3654
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003655int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3656 __u64 start, __u64 len, get_extent_t *get_extent)
3657{
Josef Bacik975f84f2010-11-23 19:36:57 +00003658 int ret = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003659 u64 off = start;
3660 u64 max = start + len;
3661 u32 flags = 0;
Josef Bacik975f84f2010-11-23 19:36:57 +00003662 u32 found_type;
3663 u64 last;
Chris Masonec29ed52011-02-23 16:23:20 -05003664 u64 last_for_get_extent = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003665 u64 disko = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003666 u64 isize = i_size_read(inode);
Josef Bacik975f84f2010-11-23 19:36:57 +00003667 struct btrfs_key found_key;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003668 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00003669 struct extent_state *cached_state = NULL;
Josef Bacik975f84f2010-11-23 19:36:57 +00003670 struct btrfs_path *path;
3671 struct btrfs_file_extent_item *item;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003672 int end = 0;
Chris Masonec29ed52011-02-23 16:23:20 -05003673 u64 em_start = 0;
3674 u64 em_len = 0;
3675 u64 em_end = 0;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003676 unsigned long emflags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003677
3678 if (len == 0)
3679 return -EINVAL;
3680
Josef Bacik975f84f2010-11-23 19:36:57 +00003681 path = btrfs_alloc_path();
3682 if (!path)
3683 return -ENOMEM;
3684 path->leave_spinning = 1;
3685
Josef Bacik4d479cf2011-11-17 11:34:31 -05003686 start = ALIGN(start, BTRFS_I(inode)->root->sectorsize);
3687 len = ALIGN(len, BTRFS_I(inode)->root->sectorsize);
3688
Chris Masonec29ed52011-02-23 16:23:20 -05003689 /*
3690 * lookup the last file extent. We're not using i_size here
3691 * because there might be preallocation past i_size
3692 */
Josef Bacik975f84f2010-11-23 19:36:57 +00003693 ret = btrfs_lookup_file_extent(NULL, BTRFS_I(inode)->root,
Li Zefan33345d012011-04-20 10:31:50 +08003694 path, btrfs_ino(inode), -1, 0);
Josef Bacik975f84f2010-11-23 19:36:57 +00003695 if (ret < 0) {
3696 btrfs_free_path(path);
3697 return ret;
3698 }
3699 WARN_ON(!ret);
3700 path->slots[0]--;
3701 item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3702 struct btrfs_file_extent_item);
3703 btrfs_item_key_to_cpu(path->nodes[0], &found_key, path->slots[0]);
3704 found_type = btrfs_key_type(&found_key);
3705
Chris Masonec29ed52011-02-23 16:23:20 -05003706 /* No extents, but there might be delalloc bits */
Li Zefan33345d012011-04-20 10:31:50 +08003707 if (found_key.objectid != btrfs_ino(inode) ||
Josef Bacik975f84f2010-11-23 19:36:57 +00003708 found_type != BTRFS_EXTENT_DATA_KEY) {
Chris Masonec29ed52011-02-23 16:23:20 -05003709 /* have to trust i_size as the end */
3710 last = (u64)-1;
3711 last_for_get_extent = isize;
3712 } else {
3713 /*
3714 * remember the start of the last extent. There are a
3715 * bunch of different factors that go into the length of the
3716 * extent, so its much less complex to remember where it started
3717 */
3718 last = found_key.offset;
3719 last_for_get_extent = last + 1;
Josef Bacik975f84f2010-11-23 19:36:57 +00003720 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003721 btrfs_free_path(path);
3722
Chris Masonec29ed52011-02-23 16:23:20 -05003723 /*
3724 * we might have some extents allocated but more delalloc past those
3725 * extents. so, we trust isize unless the start of the last extent is
3726 * beyond isize
3727 */
3728 if (last < isize) {
3729 last = (u64)-1;
3730 last_for_get_extent = isize;
3731 }
3732
Josef Bacik2ac55d42010-02-03 19:33:23 +00003733 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
3734 &cached_state, GFP_NOFS);
Chris Masonec29ed52011-02-23 16:23:20 -05003735
Josef Bacik4d479cf2011-11-17 11:34:31 -05003736 em = get_extent_skip_holes(inode, start, last_for_get_extent,
Chris Masonec29ed52011-02-23 16:23:20 -05003737 get_extent);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003738 if (!em)
3739 goto out;
3740 if (IS_ERR(em)) {
3741 ret = PTR_ERR(em);
3742 goto out;
3743 }
Josef Bacik975f84f2010-11-23 19:36:57 +00003744
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003745 while (!end) {
Chris Masonea8efc72011-03-08 11:54:40 -05003746 u64 offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003747
Chris Masonea8efc72011-03-08 11:54:40 -05003748 /* break if the extent we found is outside the range */
3749 if (em->start >= max || extent_map_end(em) < off)
3750 break;
3751
3752 /*
3753 * get_extent may return an extent that starts before our
3754 * requested range. We have to make sure the ranges
3755 * we return to fiemap always move forward and don't
3756 * overlap, so adjust the offsets here
3757 */
3758 em_start = max(em->start, off);
3759
3760 /*
3761 * record the offset from the start of the extent
3762 * for adjusting the disk offset below
3763 */
3764 offset_in_extent = em_start - em->start;
Chris Masonec29ed52011-02-23 16:23:20 -05003765 em_end = extent_map_end(em);
Chris Masonea8efc72011-03-08 11:54:40 -05003766 em_len = em_end - em_start;
Chris Masonec29ed52011-02-23 16:23:20 -05003767 emflags = em->flags;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003768 disko = 0;
3769 flags = 0;
3770
Chris Masonea8efc72011-03-08 11:54:40 -05003771 /*
3772 * bump off for our next call to get_extent
3773 */
3774 off = extent_map_end(em);
3775 if (off >= max)
3776 end = 1;
3777
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003778 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003779 end = 1;
3780 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003781 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003782 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3783 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003784 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003785 flags |= (FIEMAP_EXTENT_DELALLOC |
3786 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003787 } else {
Chris Masonea8efc72011-03-08 11:54:40 -05003788 disko = em->block_start + offset_in_extent;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003789 }
3790 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3791 flags |= FIEMAP_EXTENT_ENCODED;
3792
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003793 free_extent_map(em);
3794 em = NULL;
Chris Masonec29ed52011-02-23 16:23:20 -05003795 if ((em_start >= last) || em_len == (u64)-1 ||
3796 (last == (u64)-1 && isize <= em_end)) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003797 flags |= FIEMAP_EXTENT_LAST;
3798 end = 1;
3799 }
3800
Chris Masonec29ed52011-02-23 16:23:20 -05003801 /* now scan forward to see if this is really the last extent. */
3802 em = get_extent_skip_holes(inode, off, last_for_get_extent,
3803 get_extent);
3804 if (IS_ERR(em)) {
3805 ret = PTR_ERR(em);
3806 goto out;
3807 }
3808 if (!em) {
Josef Bacik975f84f2010-11-23 19:36:57 +00003809 flags |= FIEMAP_EXTENT_LAST;
3810 end = 1;
3811 }
Chris Masonec29ed52011-02-23 16:23:20 -05003812 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3813 em_len, flags);
3814 if (ret)
3815 goto out_free;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003816 }
3817out_free:
3818 free_extent_map(em);
3819out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00003820 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3821 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003822 return ret;
3823}
3824
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003825inline struct page *extent_buffer_page(struct extent_buffer *eb,
Chris Masond1310b22008-01-24 16:13:08 -05003826 unsigned long i)
3827{
Chris Mason727011e2010-08-06 13:21:20 -04003828 return eb->pages[i];
Chris Masond1310b22008-01-24 16:13:08 -05003829}
3830
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02003831inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04003832{
Chris Mason6af118ce2008-07-22 11:18:07 -04003833 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3834 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04003835}
3836
Chris Mason727011e2010-08-06 13:21:20 -04003837static void __free_extent_buffer(struct extent_buffer *eb)
3838{
3839#if LEAK_DEBUG
3840 unsigned long flags;
3841 spin_lock_irqsave(&leak_lock, flags);
3842 list_del(&eb->leak_list);
3843 spin_unlock_irqrestore(&leak_lock, flags);
3844#endif
3845 if (eb->pages && eb->pages != eb->inline_pages)
3846 kfree(eb->pages);
3847 kmem_cache_free(extent_buffer_cache, eb);
3848}
3849
Chris Masond1310b22008-01-24 16:13:08 -05003850static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3851 u64 start,
3852 unsigned long len,
3853 gfp_t mask)
3854{
3855 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003856#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003857 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003858#endif
Chris Masond1310b22008-01-24 16:13:08 -05003859
Chris Masond1310b22008-01-24 16:13:08 -05003860 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Tsutomu Itoh91ca3382011-01-05 02:32:22 +00003861 if (eb == NULL)
3862 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003863 eb->start = start;
3864 eb->len = len;
Josef Bacik4f2de97a2012-03-07 16:20:05 -05003865 eb->tree = tree;
Chris Masonbd681512011-07-16 15:23:14 -04003866 rwlock_init(&eb->lock);
3867 atomic_set(&eb->write_locks, 0);
3868 atomic_set(&eb->read_locks, 0);
3869 atomic_set(&eb->blocking_readers, 0);
3870 atomic_set(&eb->blocking_writers, 0);
3871 atomic_set(&eb->spinning_readers, 0);
3872 atomic_set(&eb->spinning_writers, 0);
Arne Jansen5b25f702011-09-13 10:55:48 +02003873 eb->lock_nested = 0;
Chris Masonbd681512011-07-16 15:23:14 -04003874 init_waitqueue_head(&eb->write_lock_wq);
3875 init_waitqueue_head(&eb->read_lock_wq);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003876
Chris Mason39351272009-02-04 09:24:05 -05003877#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003878 spin_lock_irqsave(&leak_lock, flags);
3879 list_add(&eb->leak_list, &buffers);
3880 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003881#endif
Josef Bacik3083ee22012-03-09 16:01:49 -05003882 spin_lock_init(&eb->refs_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003883 atomic_set(&eb->refs, 1);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003884 atomic_set(&eb->io_pages, 0);
Chris Mason727011e2010-08-06 13:21:20 -04003885
3886 if (len > MAX_INLINE_EXTENT_BUFFER_SIZE) {
3887 struct page **pages;
3888 int num_pages = (len + PAGE_CACHE_SIZE - 1) >>
3889 PAGE_CACHE_SHIFT;
3890 pages = kzalloc(num_pages, mask);
3891 if (!pages) {
3892 __free_extent_buffer(eb);
3893 return NULL;
3894 }
3895 eb->pages = pages;
3896 } else {
3897 eb->pages = eb->inline_pages;
3898 }
Chris Masond1310b22008-01-24 16:13:08 -05003899
3900 return eb;
3901}
3902
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003903static int extent_buffer_under_io(struct extent_buffer *eb)
3904{
3905 return (atomic_read(&eb->io_pages) ||
3906 test_bit(EXTENT_BUFFER_WRITEBACK, &eb->bflags) ||
3907 test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
3908}
3909
Miao Xie897ca6e2010-10-26 20:57:29 -04003910/*
3911 * Helper for releasing extent buffer page.
3912 */
3913static void btrfs_release_extent_buffer_page(struct extent_buffer *eb,
3914 unsigned long start_idx)
3915{
3916 unsigned long index;
3917 struct page *page;
3918
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003919 BUG_ON(extent_buffer_under_io(eb));
3920
Miao Xie897ca6e2010-10-26 20:57:29 -04003921 index = num_extent_pages(eb->start, eb->len);
3922 if (start_idx >= index)
3923 return;
3924
3925 do {
3926 index--;
3927 page = extent_buffer_page(eb, index);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05003928 if (page) {
3929 spin_lock(&page->mapping->private_lock);
3930 /*
3931 * We do this since we'll remove the pages after we've
3932 * removed the eb from the radix tree, so we could race
3933 * and have this page now attached to the new eb. So
3934 * only clear page_private if it's still connected to
3935 * this eb.
3936 */
3937 if (PagePrivate(page) &&
3938 page->private == (unsigned long)eb) {
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003939 BUG_ON(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
Josef Bacik3083ee22012-03-09 16:01:49 -05003940 BUG_ON(PageDirty(page));
3941 BUG_ON(PageWriteback(page));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05003942 /*
3943 * We need to make sure we haven't be attached
3944 * to a new eb.
3945 */
3946 ClearPagePrivate(page);
3947 set_page_private(page, 0);
3948 /* One for the page private */
3949 page_cache_release(page);
3950 }
3951 spin_unlock(&page->mapping->private_lock);
3952
3953 /* One for when we alloced the page */
Miao Xie897ca6e2010-10-26 20:57:29 -04003954 page_cache_release(page);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05003955 }
Miao Xie897ca6e2010-10-26 20:57:29 -04003956 } while (index != start_idx);
3957}
3958
3959/*
3960 * Helper for releasing the extent buffer.
3961 */
3962static inline void btrfs_release_extent_buffer(struct extent_buffer *eb)
3963{
3964 btrfs_release_extent_buffer_page(eb, 0);
3965 __free_extent_buffer(eb);
3966}
3967
Josef Bacik0b32f4b2012-03-13 09:38:00 -04003968static void check_buffer_tree_ref(struct extent_buffer *eb)
3969{
3970 /* the ref bit is tricky. We have to make sure it is set
3971 * if we have the buffer dirty. Otherwise the
3972 * code to free a buffer can end up dropping a dirty
3973 * page
3974 *
3975 * Once the ref bit is set, it won't go away while the
3976 * buffer is dirty or in writeback, and it also won't
3977 * go away while we have the reference count on the
3978 * eb bumped.
3979 *
3980 * We can't just set the ref bit without bumping the
3981 * ref on the eb because free_extent_buffer might
3982 * see the ref bit and try to clear it. If this happens
3983 * free_extent_buffer might end up dropping our original
3984 * ref by mistake and freeing the page before we are able
3985 * to add one more ref.
3986 *
3987 * So bump the ref count first, then set the bit. If someone
3988 * beat us to it, drop the ref we added.
3989 */
3990 if (!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
3991 atomic_inc(&eb->refs);
3992 if (test_and_set_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
3993 atomic_dec(&eb->refs);
3994 }
3995}
3996
Josef Bacik5df42352012-03-15 18:24:42 -04003997static void mark_extent_buffer_accessed(struct extent_buffer *eb)
3998{
3999 unsigned long num_pages, i;
4000
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004001 check_buffer_tree_ref(eb);
4002
Josef Bacik5df42352012-03-15 18:24:42 -04004003 num_pages = num_extent_pages(eb->start, eb->len);
4004 for (i = 0; i < num_pages; i++) {
4005 struct page *p = extent_buffer_page(eb, i);
4006 mark_page_accessed(p);
4007 }
4008}
4009
Chris Masond1310b22008-01-24 16:13:08 -05004010struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
Chris Mason727011e2010-08-06 13:21:20 -04004011 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004012{
4013 unsigned long num_pages = num_extent_pages(start, len);
4014 unsigned long i;
4015 unsigned long index = start >> PAGE_CACHE_SHIFT;
4016 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04004017 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004018 struct page *p;
4019 struct address_space *mapping = tree->mapping;
4020 int uptodate = 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004021 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05004022
Miao Xie19fe0a82010-10-26 20:57:29 -04004023 rcu_read_lock();
4024 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4025 if (eb && atomic_inc_not_zero(&eb->refs)) {
4026 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004027 mark_extent_buffer_accessed(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04004028 return eb;
4029 }
Miao Xie19fe0a82010-10-26 20:57:29 -04004030 rcu_read_unlock();
Chris Mason6af118ce2008-07-22 11:18:07 -04004031
David Sterbaba144192011-04-21 01:12:06 +02004032 eb = __alloc_extent_buffer(tree, start, len, GFP_NOFS);
Peter2b114d12008-04-01 11:21:40 -04004033 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004034 return NULL;
4035
Chris Mason727011e2010-08-06 13:21:20 -04004036 for (i = 0; i < num_pages; i++, index++) {
Chris Masona6591712011-07-19 12:04:14 -04004037 p = find_or_create_page(mapping, index, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05004038 if (!p) {
4039 WARN_ON(1);
Chris Mason6af118ce2008-07-22 11:18:07 -04004040 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05004041 }
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004042
4043 spin_lock(&mapping->private_lock);
4044 if (PagePrivate(p)) {
4045 /*
4046 * We could have already allocated an eb for this page
4047 * and attached one so lets see if we can get a ref on
4048 * the existing eb, and if we can we know it's good and
4049 * we can just return that one, else we know we can just
4050 * overwrite page->private.
4051 */
4052 exists = (struct extent_buffer *)p->private;
4053 if (atomic_inc_not_zero(&exists->refs)) {
4054 spin_unlock(&mapping->private_lock);
4055 unlock_page(p);
Josef Bacik5df42352012-03-15 18:24:42 -04004056 mark_extent_buffer_accessed(exists);
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004057 goto free_eb;
4058 }
4059
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004060 /*
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004061 * Do this so attach doesn't complain and we need to
4062 * drop the ref the old guy had.
4063 */
4064 ClearPagePrivate(p);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004065 WARN_ON(PageDirty(p));
Josef Bacik4f2de97a2012-03-07 16:20:05 -05004066 page_cache_release(p);
4067 }
4068 attach_extent_buffer_page(eb, p);
4069 spin_unlock(&mapping->private_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004070 WARN_ON(PageDirty(p));
Chris Masond1310b22008-01-24 16:13:08 -05004071 mark_page_accessed(p);
Chris Mason727011e2010-08-06 13:21:20 -04004072 eb->pages[i] = p;
Chris Masond1310b22008-01-24 16:13:08 -05004073 if (!PageUptodate(p))
4074 uptodate = 0;
Chris Masoneb14ab82011-02-10 12:35:00 -05004075
4076 /*
4077 * see below about how we avoid a nasty race with release page
4078 * and why we unlock later
4079 */
Chris Masond1310b22008-01-24 16:13:08 -05004080 }
4081 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004082 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik115391d2012-03-09 09:51:43 -05004083again:
Miao Xie19fe0a82010-10-26 20:57:29 -04004084 ret = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
4085 if (ret)
4086 goto free_eb;
4087
Chris Mason6af118ce2008-07-22 11:18:07 -04004088 spin_lock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004089 ret = radix_tree_insert(&tree->buffer, start >> PAGE_CACHE_SHIFT, eb);
4090 if (ret == -EEXIST) {
4091 exists = radix_tree_lookup(&tree->buffer,
4092 start >> PAGE_CACHE_SHIFT);
Josef Bacik115391d2012-03-09 09:51:43 -05004093 if (!atomic_inc_not_zero(&exists->refs)) {
4094 spin_unlock(&tree->buffer_lock);
4095 radix_tree_preload_end();
Josef Bacik115391d2012-03-09 09:51:43 -05004096 exists = NULL;
4097 goto again;
4098 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004099 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004100 radix_tree_preload_end();
Josef Bacik5df42352012-03-15 18:24:42 -04004101 mark_extent_buffer_accessed(exists);
Chris Mason6af118ce2008-07-22 11:18:07 -04004102 goto free_eb;
4103 }
Chris Mason6af118ce2008-07-22 11:18:07 -04004104 /* add one reference for the tree */
Josef Bacik3083ee22012-03-09 16:01:49 -05004105 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004106 check_buffer_tree_ref(eb);
Josef Bacik3083ee22012-03-09 16:01:49 -05004107 spin_unlock(&eb->refs_lock);
Yan, Zhengf044ba72010-02-04 08:46:56 +00004108 spin_unlock(&tree->buffer_lock);
Miao Xie19fe0a82010-10-26 20:57:29 -04004109 radix_tree_preload_end();
Chris Masoneb14ab82011-02-10 12:35:00 -05004110
4111 /*
4112 * there is a race where release page may have
4113 * tried to find this extent buffer in the radix
4114 * but failed. It will tell the VM it is safe to
4115 * reclaim the, and it will clear the page private bit.
4116 * We must make sure to set the page private bit properly
4117 * after the extent buffer is in the radix tree so
4118 * it doesn't get lost
4119 */
Chris Mason727011e2010-08-06 13:21:20 -04004120 SetPageChecked(eb->pages[0]);
4121 for (i = 1; i < num_pages; i++) {
4122 p = extent_buffer_page(eb, i);
Chris Mason727011e2010-08-06 13:21:20 -04004123 ClearPageChecked(p);
4124 unlock_page(p);
4125 }
4126 unlock_page(eb->pages[0]);
Chris Masond1310b22008-01-24 16:13:08 -05004127 return eb;
4128
Chris Mason6af118ce2008-07-22 11:18:07 -04004129free_eb:
Chris Mason727011e2010-08-06 13:21:20 -04004130 for (i = 0; i < num_pages; i++) {
4131 if (eb->pages[i])
4132 unlock_page(eb->pages[i]);
4133 }
Chris Masoneb14ab82011-02-10 12:35:00 -05004134
Chris Masond1310b22008-01-24 16:13:08 -05004135 if (!atomic_dec_and_test(&eb->refs))
Chris Mason6af118ce2008-07-22 11:18:07 -04004136 return exists;
Miao Xie897ca6e2010-10-26 20:57:29 -04004137 btrfs_release_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04004138 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05004139}
Chris Masond1310b22008-01-24 16:13:08 -05004140
4141struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
David Sterbaf09d1f62011-04-21 01:08:01 +02004142 u64 start, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05004143{
Chris Masond1310b22008-01-24 16:13:08 -05004144 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05004145
Miao Xie19fe0a82010-10-26 20:57:29 -04004146 rcu_read_lock();
4147 eb = radix_tree_lookup(&tree->buffer, start >> PAGE_CACHE_SHIFT);
4148 if (eb && atomic_inc_not_zero(&eb->refs)) {
4149 rcu_read_unlock();
Josef Bacik5df42352012-03-15 18:24:42 -04004150 mark_extent_buffer_accessed(eb);
Miao Xie19fe0a82010-10-26 20:57:29 -04004151 return eb;
4152 }
4153 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -04004154
Miao Xie19fe0a82010-10-26 20:57:29 -04004155 return NULL;
Chris Masond1310b22008-01-24 16:13:08 -05004156}
Chris Masond1310b22008-01-24 16:13:08 -05004157
Josef Bacik3083ee22012-03-09 16:01:49 -05004158static inline void btrfs_release_extent_buffer_rcu(struct rcu_head *head)
4159{
4160 struct extent_buffer *eb =
4161 container_of(head, struct extent_buffer, rcu_head);
4162
4163 __free_extent_buffer(eb);
4164}
4165
Josef Bacik3083ee22012-03-09 16:01:49 -05004166/* Expects to have eb->eb_lock already held */
4167static void release_extent_buffer(struct extent_buffer *eb, gfp_t mask)
4168{
4169 WARN_ON(atomic_read(&eb->refs) == 0);
4170 if (atomic_dec_and_test(&eb->refs)) {
4171 struct extent_io_tree *tree = eb->tree;
Josef Bacik3083ee22012-03-09 16:01:49 -05004172
4173 spin_unlock(&eb->refs_lock);
4174
Josef Bacik3083ee22012-03-09 16:01:49 -05004175 spin_lock(&tree->buffer_lock);
4176 radix_tree_delete(&tree->buffer,
4177 eb->start >> PAGE_CACHE_SHIFT);
4178 spin_unlock(&tree->buffer_lock);
4179
4180 /* Should be safe to release our pages at this point */
4181 btrfs_release_extent_buffer_page(eb, 0);
4182
4183 call_rcu(&eb->rcu_head, btrfs_release_extent_buffer_rcu);
4184 return;
4185 }
4186 spin_unlock(&eb->refs_lock);
4187}
4188
Chris Masond1310b22008-01-24 16:13:08 -05004189void free_extent_buffer(struct extent_buffer *eb)
4190{
Chris Masond1310b22008-01-24 16:13:08 -05004191 if (!eb)
4192 return;
4193
Josef Bacik3083ee22012-03-09 16:01:49 -05004194 spin_lock(&eb->refs_lock);
4195 if (atomic_read(&eb->refs) == 2 &&
4196 test_bit(EXTENT_BUFFER_STALE, &eb->bflags) &&
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004197 !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004198 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4199 atomic_dec(&eb->refs);
4200
4201 /*
4202 * I know this is terrible, but it's temporary until we stop tracking
4203 * the uptodate bits and such for the extent buffers.
4204 */
4205 release_extent_buffer(eb, GFP_ATOMIC);
4206}
4207
4208void free_extent_buffer_stale(struct extent_buffer *eb)
4209{
4210 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05004211 return;
4212
Josef Bacik3083ee22012-03-09 16:01:49 -05004213 spin_lock(&eb->refs_lock);
4214 set_bit(EXTENT_BUFFER_STALE, &eb->bflags);
4215
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004216 if (atomic_read(&eb->refs) == 2 && !extent_buffer_under_io(eb) &&
Josef Bacik3083ee22012-03-09 16:01:49 -05004217 test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags))
4218 atomic_dec(&eb->refs);
4219 release_extent_buffer(eb, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05004220}
Chris Masond1310b22008-01-24 16:13:08 -05004221
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004222int clear_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004223{
Chris Masond1310b22008-01-24 16:13:08 -05004224 unsigned long i;
4225 unsigned long num_pages;
4226 struct page *page;
4227
Chris Masond1310b22008-01-24 16:13:08 -05004228 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004229 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05004230
4231 for (i = 0; i < num_pages; i++) {
4232 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04004233 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05004234 continue;
4235
Chris Masona61e6f22008-07-22 11:18:08 -04004236 lock_page(page);
Chris Masoneb14ab82011-02-10 12:35:00 -05004237 WARN_ON(!PagePrivate(page));
4238
Chris Masond1310b22008-01-24 16:13:08 -05004239 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004240 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05004241 if (!PageDirty(page)) {
4242 radix_tree_tag_clear(&page->mapping->page_tree,
4243 page_index(page),
4244 PAGECACHE_TAG_DIRTY);
4245 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04004246 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masonbf0da8c2011-11-04 12:29:37 -04004247 ClearPageError(page);
Chris Masona61e6f22008-07-22 11:18:08 -04004248 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05004249 }
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004250 WARN_ON(atomic_read(&eb->refs) == 0);
Chris Masond1310b22008-01-24 16:13:08 -05004251 return 0;
4252}
Chris Masond1310b22008-01-24 16:13:08 -05004253
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004254int set_extent_buffer_dirty(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004255{
4256 unsigned long i;
4257 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04004258 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004259
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004260 check_buffer_tree_ref(eb);
4261
Chris Masonb9473432009-03-13 11:00:37 -04004262 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004263
Chris Masond1310b22008-01-24 16:13:08 -05004264 num_pages = num_extent_pages(eb->start, eb->len);
Josef Bacik3083ee22012-03-09 16:01:49 -05004265 WARN_ON(atomic_read(&eb->refs) == 0);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004266 WARN_ON(!test_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags));
4267
Chris Masonb9473432009-03-13 11:00:37 -04004268 for (i = 0; i < num_pages; i++)
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004269 set_page_dirty(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04004270 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05004271}
Chris Masond1310b22008-01-24 16:13:08 -05004272
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004273static int range_straddles_pages(u64 start, u64 len)
Chris Mason19b6caf2011-07-25 06:50:50 -04004274{
4275 if (len < PAGE_CACHE_SIZE)
4276 return 1;
4277 if (start & (PAGE_CACHE_SIZE - 1))
4278 return 1;
4279 if ((start + len) & (PAGE_CACHE_SIZE - 1))
4280 return 1;
4281 return 0;
4282}
4283
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004284int clear_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Mason1259ab72008-05-12 13:39:03 -04004285{
4286 unsigned long i;
4287 struct page *page;
4288 unsigned long num_pages;
4289
Chris Masonb4ce94d2009-02-04 09:25:08 -05004290 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004291 num_pages = num_extent_pages(eb->start, eb->len);
Chris Mason1259ab72008-05-12 13:39:03 -04004292 for (i = 0; i < num_pages; i++) {
4293 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04004294 if (page)
4295 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04004296 }
4297 return 0;
4298}
4299
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004300int set_extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004301{
4302 unsigned long i;
4303 struct page *page;
4304 unsigned long num_pages;
4305
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004306 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004307 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masond1310b22008-01-24 16:13:08 -05004308 for (i = 0; i < num_pages; i++) {
4309 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004310 SetPageUptodate(page);
4311 }
4312 return 0;
4313}
Chris Masond1310b22008-01-24 16:13:08 -05004314
Chris Masonce9adaa2008-04-09 16:28:12 -04004315int extent_range_uptodate(struct extent_io_tree *tree,
4316 u64 start, u64 end)
4317{
4318 struct page *page;
4319 int ret;
4320 int pg_uptodate = 1;
4321 int uptodate;
4322 unsigned long index;
4323
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004324 if (range_straddles_pages(start, end - start + 1)) {
Chris Mason19b6caf2011-07-25 06:50:50 -04004325 ret = test_range_bit(tree, start, end,
4326 EXTENT_UPTODATE, 1, NULL);
4327 if (ret)
4328 return 1;
4329 }
Chris Masond3977122009-01-05 21:25:51 -05004330 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004331 index = start >> PAGE_CACHE_SHIFT;
4332 page = find_get_page(tree->mapping, index);
Mitch Harder8bedd512012-01-26 15:01:11 -05004333 if (!page)
4334 return 1;
Chris Masonce9adaa2008-04-09 16:28:12 -04004335 uptodate = PageUptodate(page);
4336 page_cache_release(page);
4337 if (!uptodate) {
4338 pg_uptodate = 0;
4339 break;
4340 }
4341 start += PAGE_CACHE_SIZE;
4342 }
4343 return pg_uptodate;
4344}
4345
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004346int extent_buffer_uptodate(struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05004347{
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004348 return test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05004349}
Chris Masond1310b22008-01-24 16:13:08 -05004350
4351int read_extent_buffer_pages(struct extent_io_tree *tree,
Arne Jansenbb82ab82011-06-10 14:06:53 +02004352 struct extent_buffer *eb, u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04004353 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05004354{
4355 unsigned long i;
4356 unsigned long start_i;
4357 struct page *page;
4358 int err;
4359 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04004360 int locked_pages = 0;
4361 int all_uptodate = 1;
Chris Masond1310b22008-01-24 16:13:08 -05004362 unsigned long num_pages;
Chris Mason727011e2010-08-06 13:21:20 -04004363 unsigned long num_reads = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004364 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04004365 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05004366
Chris Masonb4ce94d2009-02-04 09:25:08 -05004367 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05004368 return 0;
4369
Chris Masond1310b22008-01-24 16:13:08 -05004370 if (start) {
4371 WARN_ON(start < eb->start);
4372 start_i = (start >> PAGE_CACHE_SHIFT) -
4373 (eb->start >> PAGE_CACHE_SHIFT);
4374 } else {
4375 start_i = 0;
4376 }
4377
4378 num_pages = num_extent_pages(eb->start, eb->len);
4379 for (i = start_i; i < num_pages; i++) {
4380 page = extent_buffer_page(eb, i);
Arne Jansenbb82ab82011-06-10 14:06:53 +02004381 if (wait == WAIT_NONE) {
David Woodhouse2db04962008-08-07 11:19:43 -04004382 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04004383 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05004384 } else {
4385 lock_page(page);
4386 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004387 locked_pages++;
Chris Mason727011e2010-08-06 13:21:20 -04004388 if (!PageUptodate(page)) {
4389 num_reads++;
Chris Masonce9adaa2008-04-09 16:28:12 -04004390 all_uptodate = 0;
Chris Mason727011e2010-08-06 13:21:20 -04004391 }
Chris Masonce9adaa2008-04-09 16:28:12 -04004392 }
4393 if (all_uptodate) {
4394 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05004395 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04004396 goto unlock_exit;
4397 }
4398
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004399 atomic_set(&eb->io_pages, num_reads);
Chris Masonce9adaa2008-04-09 16:28:12 -04004400 for (i = start_i; i < num_pages; i++) {
4401 page = extent_buffer_page(eb, i);
Chris Masonce9adaa2008-04-09 16:28:12 -04004402 if (!PageUptodate(page)) {
Chris Masonf1885912008-04-09 16:28:12 -04004403 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05004404 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04004405 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04004406 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05004407 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05004408 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05004409 } else {
4410 unlock_page(page);
4411 }
4412 }
4413
Chris Masona86c12c2008-02-07 10:50:54 -05004414 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04004415 submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masona86c12c2008-02-07 10:50:54 -05004416
Arne Jansenbb82ab82011-06-10 14:06:53 +02004417 if (ret || wait != WAIT_COMPLETE)
Chris Masond1310b22008-01-24 16:13:08 -05004418 return ret;
Chris Masond3977122009-01-05 21:25:51 -05004419
Chris Masond1310b22008-01-24 16:13:08 -05004420 for (i = start_i; i < num_pages; i++) {
4421 page = extent_buffer_page(eb, i);
4422 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05004423 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05004424 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05004425 }
Chris Masond3977122009-01-05 21:25:51 -05004426
Chris Masond1310b22008-01-24 16:13:08 -05004427 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04004428
4429unlock_exit:
4430 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05004431 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04004432 page = extent_buffer_page(eb, i);
4433 i++;
4434 unlock_page(page);
4435 locked_pages--;
4436 }
4437 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05004438}
Chris Masond1310b22008-01-24 16:13:08 -05004439
4440void read_extent_buffer(struct extent_buffer *eb, void *dstv,
4441 unsigned long start,
4442 unsigned long len)
4443{
4444 size_t cur;
4445 size_t offset;
4446 struct page *page;
4447 char *kaddr;
4448 char *dst = (char *)dstv;
4449 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4450 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05004451
4452 WARN_ON(start > eb->len);
4453 WARN_ON(start + len > eb->start + eb->len);
4454
4455 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4456
Chris Masond3977122009-01-05 21:25:51 -05004457 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004458 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05004459
4460 cur = min(len, (PAGE_CACHE_SIZE - offset));
Chris Masona6591712011-07-19 12:04:14 -04004461 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004462 memcpy(dst, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004463
4464 dst += cur;
4465 len -= cur;
4466 offset = 0;
4467 i++;
4468 }
4469}
Chris Masond1310b22008-01-24 16:13:08 -05004470
4471int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
Chris Masona6591712011-07-19 12:04:14 -04004472 unsigned long min_len, char **map,
Chris Masond1310b22008-01-24 16:13:08 -05004473 unsigned long *map_start,
Chris Masona6591712011-07-19 12:04:14 -04004474 unsigned long *map_len)
Chris Masond1310b22008-01-24 16:13:08 -05004475{
4476 size_t offset = start & (PAGE_CACHE_SIZE - 1);
4477 char *kaddr;
4478 struct page *p;
4479 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4480 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4481 unsigned long end_i = (start_offset + start + min_len - 1) >>
4482 PAGE_CACHE_SHIFT;
4483
4484 if (i != end_i)
4485 return -EINVAL;
4486
4487 if (i == 0) {
4488 offset = start_offset;
4489 *map_start = 0;
4490 } else {
4491 offset = 0;
4492 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
4493 }
Chris Masond3977122009-01-05 21:25:51 -05004494
Chris Masond1310b22008-01-24 16:13:08 -05004495 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05004496 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
4497 "wanted %lu %lu\n", (unsigned long long)eb->start,
4498 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05004499 WARN_ON(1);
Josef Bacik850265332011-03-15 14:52:12 -04004500 return -EINVAL;
Chris Masond1310b22008-01-24 16:13:08 -05004501 }
4502
4503 p = extent_buffer_page(eb, i);
Chris Masona6591712011-07-19 12:04:14 -04004504 kaddr = page_address(p);
Chris Masond1310b22008-01-24 16:13:08 -05004505 *map = kaddr + offset;
4506 *map_len = PAGE_CACHE_SIZE - offset;
4507 return 0;
4508}
Chris Masond1310b22008-01-24 16:13:08 -05004509
Chris Masond1310b22008-01-24 16:13:08 -05004510int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
4511 unsigned long start,
4512 unsigned long len)
4513{
4514 size_t cur;
4515 size_t offset;
4516 struct page *page;
4517 char *kaddr;
4518 char *ptr = (char *)ptrv;
4519 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4520 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4521 int ret = 0;
4522
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));
4532
Chris Masona6591712011-07-19 12:04:14 -04004533 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004534 ret = memcmp(ptr, kaddr + offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004535 if (ret)
4536 break;
4537
4538 ptr += cur;
4539 len -= cur;
4540 offset = 0;
4541 i++;
4542 }
4543 return ret;
4544}
Chris Masond1310b22008-01-24 16:13:08 -05004545
4546void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
4547 unsigned long start, unsigned long len)
4548{
4549 size_t cur;
4550 size_t offset;
4551 struct page *page;
4552 char *kaddr;
4553 char *src = (char *)srcv;
4554 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4555 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4556
4557 WARN_ON(start > eb->len);
4558 WARN_ON(start + len > eb->start + eb->len);
4559
4560 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4561
Chris Masond3977122009-01-05 21:25:51 -05004562 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004563 page = extent_buffer_page(eb, i);
4564 WARN_ON(!PageUptodate(page));
4565
4566 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004567 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004568 memcpy(kaddr + offset, src, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004569
4570 src += cur;
4571 len -= cur;
4572 offset = 0;
4573 i++;
4574 }
4575}
Chris Masond1310b22008-01-24 16:13:08 -05004576
4577void memset_extent_buffer(struct extent_buffer *eb, char c,
4578 unsigned long start, unsigned long len)
4579{
4580 size_t cur;
4581 size_t offset;
4582 struct page *page;
4583 char *kaddr;
4584 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
4585 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
4586
4587 WARN_ON(start > eb->len);
4588 WARN_ON(start + len > eb->start + eb->len);
4589
4590 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
4591
Chris Masond3977122009-01-05 21:25:51 -05004592 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004593 page = extent_buffer_page(eb, i);
4594 WARN_ON(!PageUptodate(page));
4595
4596 cur = min(len, PAGE_CACHE_SIZE - offset);
Chris Masona6591712011-07-19 12:04:14 -04004597 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004598 memset(kaddr + offset, c, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004599
4600 len -= cur;
4601 offset = 0;
4602 i++;
4603 }
4604}
Chris Masond1310b22008-01-24 16:13:08 -05004605
4606void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
4607 unsigned long dst_offset, unsigned long src_offset,
4608 unsigned long len)
4609{
4610 u64 dst_len = dst->len;
4611 size_t cur;
4612 size_t offset;
4613 struct page *page;
4614 char *kaddr;
4615 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4616 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4617
4618 WARN_ON(src->len != dst_len);
4619
4620 offset = (start_offset + dst_offset) &
4621 ((unsigned long)PAGE_CACHE_SIZE - 1);
4622
Chris Masond3977122009-01-05 21:25:51 -05004623 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004624 page = extent_buffer_page(dst, i);
4625 WARN_ON(!PageUptodate(page));
4626
4627 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
4628
Chris Masona6591712011-07-19 12:04:14 -04004629 kaddr = page_address(page);
Chris Masond1310b22008-01-24 16:13:08 -05004630 read_extent_buffer(src, kaddr + offset, src_offset, cur);
Chris Masond1310b22008-01-24 16:13:08 -05004631
4632 src_offset += cur;
4633 len -= cur;
4634 offset = 0;
4635 i++;
4636 }
4637}
Chris Masond1310b22008-01-24 16:13:08 -05004638
4639static void move_pages(struct page *dst_page, struct page *src_page,
4640 unsigned long dst_off, unsigned long src_off,
4641 unsigned long len)
4642{
Chris Masona6591712011-07-19 12:04:14 -04004643 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004644 if (dst_page == src_page) {
4645 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
4646 } else {
Chris Masona6591712011-07-19 12:04:14 -04004647 char *src_kaddr = page_address(src_page);
Chris Masond1310b22008-01-24 16:13:08 -05004648 char *p = dst_kaddr + dst_off + len;
4649 char *s = src_kaddr + src_off + len;
4650
4651 while (len--)
4652 *--p = *--s;
Chris Masond1310b22008-01-24 16:13:08 -05004653 }
Chris Masond1310b22008-01-24 16:13:08 -05004654}
4655
Sergei Trofimovich33872062011-04-11 21:52:52 +00004656static inline bool areas_overlap(unsigned long src, unsigned long dst, unsigned long len)
4657{
4658 unsigned long distance = (src > dst) ? src - dst : dst - src;
4659 return distance < len;
4660}
4661
Chris Masond1310b22008-01-24 16:13:08 -05004662static void copy_pages(struct page *dst_page, struct page *src_page,
4663 unsigned long dst_off, unsigned long src_off,
4664 unsigned long len)
4665{
Chris Masona6591712011-07-19 12:04:14 -04004666 char *dst_kaddr = page_address(dst_page);
Chris Masond1310b22008-01-24 16:13:08 -05004667 char *src_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004668 int must_memmove = 0;
Chris Masond1310b22008-01-24 16:13:08 -05004669
Sergei Trofimovich33872062011-04-11 21:52:52 +00004670 if (dst_page != src_page) {
Chris Masona6591712011-07-19 12:04:14 -04004671 src_kaddr = page_address(src_page);
Sergei Trofimovich33872062011-04-11 21:52:52 +00004672 } else {
Chris Masond1310b22008-01-24 16:13:08 -05004673 src_kaddr = dst_kaddr;
Chris Mason727011e2010-08-06 13:21:20 -04004674 if (areas_overlap(src_off, dst_off, len))
4675 must_memmove = 1;
Sergei Trofimovich33872062011-04-11 21:52:52 +00004676 }
Chris Masond1310b22008-01-24 16:13:08 -05004677
Chris Mason727011e2010-08-06 13:21:20 -04004678 if (must_memmove)
4679 memmove(dst_kaddr + dst_off, src_kaddr + src_off, len);
4680 else
4681 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
Chris Masond1310b22008-01-24 16:13:08 -05004682}
4683
4684void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4685 unsigned long src_offset, unsigned long len)
4686{
4687 size_t cur;
4688 size_t dst_off_in_page;
4689 size_t src_off_in_page;
4690 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4691 unsigned long dst_i;
4692 unsigned long src_i;
4693
4694 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004695 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4696 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004697 BUG_ON(1);
4698 }
4699 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004700 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4701 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004702 BUG_ON(1);
4703 }
4704
Chris Masond3977122009-01-05 21:25:51 -05004705 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004706 dst_off_in_page = (start_offset + dst_offset) &
4707 ((unsigned long)PAGE_CACHE_SIZE - 1);
4708 src_off_in_page = (start_offset + src_offset) &
4709 ((unsigned long)PAGE_CACHE_SIZE - 1);
4710
4711 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
4712 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
4713
4714 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
4715 src_off_in_page));
4716 cur = min_t(unsigned long, cur,
4717 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
4718
4719 copy_pages(extent_buffer_page(dst, dst_i),
4720 extent_buffer_page(dst, src_i),
4721 dst_off_in_page, src_off_in_page, cur);
4722
4723 src_offset += cur;
4724 dst_offset += cur;
4725 len -= cur;
4726 }
4727}
Chris Masond1310b22008-01-24 16:13:08 -05004728
4729void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
4730 unsigned long src_offset, unsigned long len)
4731{
4732 size_t cur;
4733 size_t dst_off_in_page;
4734 size_t src_off_in_page;
4735 unsigned long dst_end = dst_offset + len - 1;
4736 unsigned long src_end = src_offset + len - 1;
4737 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
4738 unsigned long dst_i;
4739 unsigned long src_i;
4740
4741 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004742 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
4743 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004744 BUG_ON(1);
4745 }
4746 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05004747 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
4748 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05004749 BUG_ON(1);
4750 }
Chris Mason727011e2010-08-06 13:21:20 -04004751 if (dst_offset < src_offset) {
Chris Masond1310b22008-01-24 16:13:08 -05004752 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
4753 return;
4754 }
Chris Masond3977122009-01-05 21:25:51 -05004755 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05004756 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
4757 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
4758
4759 dst_off_in_page = (start_offset + dst_end) &
4760 ((unsigned long)PAGE_CACHE_SIZE - 1);
4761 src_off_in_page = (start_offset + src_end) &
4762 ((unsigned long)PAGE_CACHE_SIZE - 1);
4763
4764 cur = min_t(unsigned long, len, src_off_in_page + 1);
4765 cur = min(cur, dst_off_in_page + 1);
4766 move_pages(extent_buffer_page(dst, dst_i),
4767 extent_buffer_page(dst, src_i),
4768 dst_off_in_page - cur + 1,
4769 src_off_in_page - cur + 1, cur);
4770
4771 dst_end -= cur;
4772 src_end -= cur;
4773 len -= cur;
4774 }
4775}
Chris Mason6af118ce2008-07-22 11:18:07 -04004776
Josef Bacik3083ee22012-03-09 16:01:49 -05004777int try_release_extent_buffer(struct page *page, gfp_t mask)
Miao Xie19fe0a82010-10-26 20:57:29 -04004778{
Josef Bacik3083ee22012-03-09 16:01:49 -05004779 struct extent_buffer *eb;
Miao Xie897ca6e2010-10-26 20:57:29 -04004780
Miao Xie19fe0a82010-10-26 20:57:29 -04004781 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05004782 * We need to make sure noboody is attaching this page to an eb right
4783 * now.
Miao Xie19fe0a82010-10-26 20:57:29 -04004784 */
Josef Bacik3083ee22012-03-09 16:01:49 -05004785 spin_lock(&page->mapping->private_lock);
4786 if (!PagePrivate(page)) {
4787 spin_unlock(&page->mapping->private_lock);
4788 return 1;
Miao Xie19fe0a82010-10-26 20:57:29 -04004789 }
Miao Xie19fe0a82010-10-26 20:57:29 -04004790
Josef Bacik3083ee22012-03-09 16:01:49 -05004791 eb = (struct extent_buffer *)page->private;
4792 BUG_ON(!eb);
4793
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004794 /*
Josef Bacik3083ee22012-03-09 16:01:49 -05004795 * This is a little awful but should be ok, we need to make sure that
4796 * the eb doesn't disappear out from under us while we're looking at
4797 * this page.
4798 */
4799 spin_lock(&eb->refs_lock);
Josef Bacik0b32f4b2012-03-13 09:38:00 -04004800 if (atomic_read(&eb->refs) != 1 || extent_buffer_under_io(eb)) {
Josef Bacik3083ee22012-03-09 16:01:49 -05004801 spin_unlock(&eb->refs_lock);
4802 spin_unlock(&page->mapping->private_lock);
4803 return 0;
4804 }
4805 spin_unlock(&page->mapping->private_lock);
4806
4807 if ((mask & GFP_NOFS) == GFP_NOFS)
4808 mask = GFP_NOFS;
4809
4810 /*
4811 * If tree ref isn't set then we know the ref on this eb is a real ref,
4812 * so just return, this page will likely be freed soon anyway.
4813 */
4814 if (!test_and_clear_bit(EXTENT_BUFFER_TREE_REF, &eb->bflags)) {
4815 spin_unlock(&eb->refs_lock);
4816 return 0;
4817 }
4818 release_extent_buffer(eb, mask);
4819
4820 return 1;
Chris Mason6af118ce2008-07-22 11:18:07 -04004821}