blob: a4080c21ec55377b4e5e60fc5bc8fa59e032a1ee [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>
13#include "extent_io.h"
14#include "extent_map.h"
David Woodhouse2db04962008-08-07 11:19:43 -040015#include "compat.h"
David Woodhouse902b22f2008-08-20 08:51:49 -040016#include "ctree.h"
17#include "btrfs_inode.h"
Chris Masond1310b22008-01-24 16:13:08 -050018
Chris Masond1310b22008-01-24 16:13:08 -050019static struct kmem_cache *extent_state_cache;
20static struct kmem_cache *extent_buffer_cache;
21
22static LIST_HEAD(buffers);
23static LIST_HEAD(states);
Chris Mason4bef0842008-09-08 11:18:08 -040024
Chris Masonb47eda82008-11-10 12:34:40 -050025#define LEAK_DEBUG 0
Chris Mason39351272009-02-04 09:24:05 -050026#if LEAK_DEBUG
Chris Masond3977122009-01-05 21:25:51 -050027static DEFINE_SPINLOCK(leak_lock);
Chris Mason4bef0842008-09-08 11:18:08 -040028#endif
Chris Masond1310b22008-01-24 16:13:08 -050029
Chris Masond1310b22008-01-24 16:13:08 -050030#define BUFFER_LRU_MAX 64
31
32struct tree_entry {
33 u64 start;
34 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -050035 struct rb_node rb_node;
36};
37
38struct extent_page_data {
39 struct bio *bio;
40 struct extent_io_tree *tree;
41 get_extent_t *get_extent;
Chris Mason771ed682008-11-06 22:02:51 -050042
43 /* tells writepage not to lock the state bits for this range
44 * it still does the unlocking
45 */
Chris Masonffbd5172009-04-20 15:50:09 -040046 unsigned int extent_locked:1;
47
48 /* tells the submit_bio code to use a WRITE_SYNC */
49 unsigned int sync_io:1;
Chris Masond1310b22008-01-24 16:13:08 -050050};
51
52int __init extent_io_init(void)
53{
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020054 extent_state_cache = kmem_cache_create("extent_state",
55 sizeof(struct extent_state), 0,
56 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050057 if (!extent_state_cache)
58 return -ENOMEM;
59
Christoph Hellwig9601e3f2009-04-13 15:33:09 +020060 extent_buffer_cache = kmem_cache_create("extent_buffers",
61 sizeof(struct extent_buffer), 0,
62 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
Chris Masond1310b22008-01-24 16:13:08 -050063 if (!extent_buffer_cache)
64 goto free_state_cache;
65 return 0;
66
67free_state_cache:
68 kmem_cache_destroy(extent_state_cache);
69 return -ENOMEM;
70}
71
72void extent_io_exit(void)
73{
74 struct extent_state *state;
Chris Mason2d2ae542008-03-26 16:24:23 -040075 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -050076
77 while (!list_empty(&states)) {
Chris Mason2d2ae542008-03-26 16:24:23 -040078 state = list_entry(states.next, struct extent_state, leak_list);
Chris Masond3977122009-01-05 21:25:51 -050079 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
80 "state %lu in tree %p refs %d\n",
81 (unsigned long long)state->start,
82 (unsigned long long)state->end,
83 state->state, state->tree, atomic_read(&state->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040084 list_del(&state->leak_list);
Chris Masond1310b22008-01-24 16:13:08 -050085 kmem_cache_free(extent_state_cache, state);
86
87 }
88
Chris Mason2d2ae542008-03-26 16:24:23 -040089 while (!list_empty(&buffers)) {
90 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
Chris Masond3977122009-01-05 21:25:51 -050091 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
92 "refs %d\n", (unsigned long long)eb->start,
93 eb->len, atomic_read(&eb->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040094 list_del(&eb->leak_list);
95 kmem_cache_free(extent_buffer_cache, eb);
96 }
Chris Masond1310b22008-01-24 16:13:08 -050097 if (extent_state_cache)
98 kmem_cache_destroy(extent_state_cache);
99 if (extent_buffer_cache)
100 kmem_cache_destroy(extent_buffer_cache);
101}
102
103void extent_io_tree_init(struct extent_io_tree *tree,
104 struct address_space *mapping, gfp_t mask)
105{
Eric Paris6bef4d32010-02-23 19:43:04 +0000106 tree->state = RB_ROOT;
107 tree->buffer = RB_ROOT;
Chris Masond1310b22008-01-24 16:13:08 -0500108 tree->ops = NULL;
109 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500110 spin_lock_init(&tree->lock);
Chris Mason6af118ce2008-07-22 11:18:07 -0400111 spin_lock_init(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -0500112 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500113}
Chris Masond1310b22008-01-24 16:13:08 -0500114
Christoph Hellwigb2950862008-12-02 09:54:17 -0500115static struct extent_state *alloc_extent_state(gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500116{
117 struct extent_state *state;
Chris Mason39351272009-02-04 09:24:05 -0500118#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400119 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400120#endif
Chris Masond1310b22008-01-24 16:13:08 -0500121
122 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400123 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500124 return state;
125 state->state = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500126 state->private = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500127 state->tree = NULL;
Chris Mason39351272009-02-04 09:24:05 -0500128#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400129 spin_lock_irqsave(&leak_lock, flags);
130 list_add(&state->leak_list, &states);
131 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400132#endif
Chris Masond1310b22008-01-24 16:13:08 -0500133 atomic_set(&state->refs, 1);
134 init_waitqueue_head(&state->wq);
135 return state;
136}
Chris Masond1310b22008-01-24 16:13:08 -0500137
Chris Mason4845e442010-05-25 20:56:50 -0400138void free_extent_state(struct extent_state *state)
Chris Masond1310b22008-01-24 16:13:08 -0500139{
Chris Masond1310b22008-01-24 16:13:08 -0500140 if (!state)
141 return;
142 if (atomic_dec_and_test(&state->refs)) {
Chris Mason39351272009-02-04 09:24:05 -0500143#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400144 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -0400145#endif
Chris Mason70dec802008-01-29 09:59:12 -0500146 WARN_ON(state->tree);
Chris Mason39351272009-02-04 09:24:05 -0500147#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -0400148 spin_lock_irqsave(&leak_lock, flags);
149 list_del(&state->leak_list);
150 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -0400151#endif
Chris Masond1310b22008-01-24 16:13:08 -0500152 kmem_cache_free(extent_state_cache, state);
153 }
154}
Chris Masond1310b22008-01-24 16:13:08 -0500155
156static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
157 struct rb_node *node)
158{
Chris Masond3977122009-01-05 21:25:51 -0500159 struct rb_node **p = &root->rb_node;
160 struct rb_node *parent = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500161 struct tree_entry *entry;
162
Chris Masond3977122009-01-05 21:25:51 -0500163 while (*p) {
Chris Masond1310b22008-01-24 16:13:08 -0500164 parent = *p;
165 entry = rb_entry(parent, struct tree_entry, rb_node);
166
167 if (offset < entry->start)
168 p = &(*p)->rb_left;
169 else if (offset > entry->end)
170 p = &(*p)->rb_right;
171 else
172 return parent;
173 }
174
175 entry = rb_entry(node, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500176 rb_link_node(node, parent, p);
177 rb_insert_color(node, root);
178 return NULL;
179}
180
Chris Mason80ea96b2008-02-01 14:51:59 -0500181static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Chris Masond1310b22008-01-24 16:13:08 -0500182 struct rb_node **prev_ret,
183 struct rb_node **next_ret)
184{
Chris Mason80ea96b2008-02-01 14:51:59 -0500185 struct rb_root *root = &tree->state;
Chris Masond3977122009-01-05 21:25:51 -0500186 struct rb_node *n = root->rb_node;
Chris Masond1310b22008-01-24 16:13:08 -0500187 struct rb_node *prev = NULL;
188 struct rb_node *orig_prev = NULL;
189 struct tree_entry *entry;
190 struct tree_entry *prev_entry = NULL;
191
Chris Masond3977122009-01-05 21:25:51 -0500192 while (n) {
Chris Masond1310b22008-01-24 16:13:08 -0500193 entry = rb_entry(n, struct tree_entry, rb_node);
194 prev = n;
195 prev_entry = entry;
196
197 if (offset < entry->start)
198 n = n->rb_left;
199 else if (offset > entry->end)
200 n = n->rb_right;
Chris Masond3977122009-01-05 21:25:51 -0500201 else
Chris Masond1310b22008-01-24 16:13:08 -0500202 return n;
203 }
204
205 if (prev_ret) {
206 orig_prev = prev;
Chris Masond3977122009-01-05 21:25:51 -0500207 while (prev && offset > prev_entry->end) {
Chris Masond1310b22008-01-24 16:13:08 -0500208 prev = rb_next(prev);
209 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
210 }
211 *prev_ret = prev;
212 prev = orig_prev;
213 }
214
215 if (next_ret) {
216 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500217 while (prev && offset < prev_entry->start) {
Chris Masond1310b22008-01-24 16:13:08 -0500218 prev = rb_prev(prev);
219 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
220 }
221 *next_ret = prev;
222 }
223 return NULL;
224}
225
Chris Mason80ea96b2008-02-01 14:51:59 -0500226static inline struct rb_node *tree_search(struct extent_io_tree *tree,
227 u64 offset)
Chris Masond1310b22008-01-24 16:13:08 -0500228{
Chris Mason70dec802008-01-29 09:59:12 -0500229 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500230 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500231
Chris Mason80ea96b2008-02-01 14:51:59 -0500232 ret = __etree_search(tree, offset, &prev, NULL);
Chris Masond3977122009-01-05 21:25:51 -0500233 if (!ret)
Chris Masond1310b22008-01-24 16:13:08 -0500234 return prev;
235 return ret;
236}
237
Chris Mason6af118ce2008-07-22 11:18:07 -0400238static struct extent_buffer *buffer_tree_insert(struct extent_io_tree *tree,
239 u64 offset, struct rb_node *node)
240{
241 struct rb_root *root = &tree->buffer;
Chris Masond3977122009-01-05 21:25:51 -0500242 struct rb_node **p = &root->rb_node;
243 struct rb_node *parent = NULL;
Chris Mason6af118ce2008-07-22 11:18:07 -0400244 struct extent_buffer *eb;
245
Chris Masond3977122009-01-05 21:25:51 -0500246 while (*p) {
Chris Mason6af118ce2008-07-22 11:18:07 -0400247 parent = *p;
248 eb = rb_entry(parent, struct extent_buffer, rb_node);
249
250 if (offset < eb->start)
251 p = &(*p)->rb_left;
252 else if (offset > eb->start)
253 p = &(*p)->rb_right;
254 else
255 return eb;
256 }
257
258 rb_link_node(node, parent, p);
259 rb_insert_color(node, root);
260 return NULL;
261}
262
263static struct extent_buffer *buffer_search(struct extent_io_tree *tree,
264 u64 offset)
265{
266 struct rb_root *root = &tree->buffer;
Chris Masond3977122009-01-05 21:25:51 -0500267 struct rb_node *n = root->rb_node;
Chris Mason6af118ce2008-07-22 11:18:07 -0400268 struct extent_buffer *eb;
269
Chris Masond3977122009-01-05 21:25:51 -0500270 while (n) {
Chris Mason6af118ce2008-07-22 11:18:07 -0400271 eb = rb_entry(n, struct extent_buffer, rb_node);
272 if (offset < eb->start)
273 n = n->rb_left;
274 else if (offset > eb->start)
275 n = n->rb_right;
276 else
277 return eb;
278 }
279 return NULL;
280}
281
Josef Bacik9ed74f22009-09-11 16:12:44 -0400282static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
283 struct extent_state *other)
284{
285 if (tree->ops && tree->ops->merge_extent_hook)
286 tree->ops->merge_extent_hook(tree->mapping->host, new,
287 other);
288}
289
Chris Masond1310b22008-01-24 16:13:08 -0500290/*
291 * utility function to look for merge candidates inside a given range.
292 * Any extents with matching state are merged together into a single
293 * extent in the tree. Extents with EXTENT_IO in their state field
294 * are not merged because the end_io handlers need to be able to do
295 * operations on them without sleeping (or doing allocations/splits).
296 *
297 * This should be called with the tree lock held.
298 */
299static int merge_state(struct extent_io_tree *tree,
300 struct extent_state *state)
301{
302 struct extent_state *other;
303 struct rb_node *other_node;
304
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400305 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
Chris Masond1310b22008-01-24 16:13:08 -0500306 return 0;
307
308 other_node = rb_prev(&state->rb_node);
309 if (other_node) {
310 other = rb_entry(other_node, struct extent_state, rb_node);
311 if (other->end == state->start - 1 &&
312 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400313 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500314 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500315 other->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500316 rb_erase(&other->rb_node, &tree->state);
317 free_extent_state(other);
318 }
319 }
320 other_node = rb_next(&state->rb_node);
321 if (other_node) {
322 other = rb_entry(other_node, struct extent_state, rb_node);
323 if (other->start == state->end + 1 &&
324 other->state == state->state) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400325 merge_cb(tree, state, other);
Chris Masond1310b22008-01-24 16:13:08 -0500326 other->start = state->start;
Chris Mason70dec802008-01-29 09:59:12 -0500327 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500328 rb_erase(&state->rb_node, &tree->state);
329 free_extent_state(state);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400330 state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500331 }
332 }
Josef Bacik9ed74f22009-09-11 16:12:44 -0400333
Chris Masond1310b22008-01-24 16:13:08 -0500334 return 0;
335}
336
Josef Bacik9ed74f22009-09-11 16:12:44 -0400337static int set_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400338 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500339{
340 if (tree->ops && tree->ops->set_bit_hook) {
Josef Bacik9ed74f22009-09-11 16:12:44 -0400341 return tree->ops->set_bit_hook(tree->mapping->host,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400342 state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500343 }
Josef Bacik9ed74f22009-09-11 16:12:44 -0400344
345 return 0;
Chris Mason291d6732008-01-29 15:55:23 -0500346}
347
348static void clear_state_cb(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400349 struct extent_state *state, int *bits)
Chris Mason291d6732008-01-29 15:55:23 -0500350{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400351 if (tree->ops && tree->ops->clear_bit_hook)
352 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500353}
354
Chris Masond1310b22008-01-24 16:13:08 -0500355/*
356 * insert an extent_state struct into the tree. 'bits' are set on the
357 * struct before it is inserted.
358 *
359 * This may return -EEXIST if the extent is already there, in which case the
360 * state struct is freed.
361 *
362 * The tree lock is not taken internally. This is a utility function and
363 * probably isn't what you want to call (see set/clear_extent_bit).
364 */
365static int insert_state(struct extent_io_tree *tree,
366 struct extent_state *state, u64 start, u64 end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400367 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500368{
369 struct rb_node *node;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400370 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400371 int ret;
Chris Masond1310b22008-01-24 16:13:08 -0500372
373 if (end < start) {
Chris Masond3977122009-01-05 21:25:51 -0500374 printk(KERN_ERR "btrfs end < start %llu %llu\n",
375 (unsigned long long)end,
376 (unsigned long long)start);
Chris Masond1310b22008-01-24 16:13:08 -0500377 WARN_ON(1);
378 }
Chris Masond1310b22008-01-24 16:13:08 -0500379 state->start = start;
380 state->end = end;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400381 ret = set_state_cb(tree, state, bits);
382 if (ret)
383 return ret;
384
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400385 if (bits_to_set & EXTENT_DIRTY)
Josef Bacik9ed74f22009-09-11 16:12:44 -0400386 tree->dirty_bytes += end - start + 1;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400387 state->state |= bits_to_set;
Chris Masond1310b22008-01-24 16:13:08 -0500388 node = tree_insert(&tree->state, end, &state->rb_node);
389 if (node) {
390 struct extent_state *found;
391 found = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -0500392 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
393 "%llu %llu\n", (unsigned long long)found->start,
394 (unsigned long long)found->end,
395 (unsigned long long)start, (unsigned long long)end);
Chris Masond1310b22008-01-24 16:13:08 -0500396 free_extent_state(state);
397 return -EEXIST;
398 }
Chris Mason70dec802008-01-29 09:59:12 -0500399 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500400 merge_state(tree, state);
401 return 0;
402}
403
Josef Bacik9ed74f22009-09-11 16:12:44 -0400404static int split_cb(struct extent_io_tree *tree, struct extent_state *orig,
405 u64 split)
406{
407 if (tree->ops && tree->ops->split_extent_hook)
408 return tree->ops->split_extent_hook(tree->mapping->host,
409 orig, split);
410 return 0;
411}
412
Chris Masond1310b22008-01-24 16:13:08 -0500413/*
414 * split a given extent state struct in two, inserting the preallocated
415 * struct 'prealloc' as the newly created second half. 'split' indicates an
416 * offset inside 'orig' where it should be split.
417 *
418 * Before calling,
419 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
420 * are two extent state structs in the tree:
421 * prealloc: [orig->start, split - 1]
422 * orig: [ split, orig->end ]
423 *
424 * The tree locks are not taken by this function. They need to be held
425 * by the caller.
426 */
427static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
428 struct extent_state *prealloc, u64 split)
429{
430 struct rb_node *node;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400431
432 split_cb(tree, orig, split);
433
Chris Masond1310b22008-01-24 16:13:08 -0500434 prealloc->start = orig->start;
435 prealloc->end = split - 1;
436 prealloc->state = orig->state;
437 orig->start = split;
438
439 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
440 if (node) {
Chris Masond1310b22008-01-24 16:13:08 -0500441 free_extent_state(prealloc);
442 return -EEXIST;
443 }
Chris Mason70dec802008-01-29 09:59:12 -0500444 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500445 return 0;
446}
447
448/*
449 * utility function to clear some bits in an extent state struct.
450 * it will optionally wake up any one waiting on this state (wake == 1), or
451 * forcibly remove the state from the tree (delete == 1).
452 *
453 * If no bits are set on the state struct after clearing things, the
454 * struct is freed and removed from the tree
455 */
456static int clear_state_bit(struct extent_io_tree *tree,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400457 struct extent_state *state,
458 int *bits, int wake)
Chris Masond1310b22008-01-24 16:13:08 -0500459{
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400460 int bits_to_clear = *bits & ~EXTENT_CTLBITS;
Josef Bacik32c00af2009-10-08 13:34:05 -0400461 int ret = state->state & bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500462
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400463 if ((bits_to_clear & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500464 u64 range = state->end - state->start + 1;
465 WARN_ON(range > tree->dirty_bytes);
466 tree->dirty_bytes -= range;
467 }
Chris Mason291d6732008-01-29 15:55:23 -0500468 clear_state_cb(tree, state, bits);
Josef Bacik32c00af2009-10-08 13:34:05 -0400469 state->state &= ~bits_to_clear;
Chris Masond1310b22008-01-24 16:13:08 -0500470 if (wake)
471 wake_up(&state->wq);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400472 if (state->state == 0) {
Chris Mason70dec802008-01-29 09:59:12 -0500473 if (state->tree) {
Chris Masond1310b22008-01-24 16:13:08 -0500474 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500475 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500476 free_extent_state(state);
477 } else {
478 WARN_ON(1);
479 }
480 } else {
481 merge_state(tree, state);
482 }
483 return ret;
484}
485
486/*
487 * clear some bits on a range in the tree. This may require splitting
488 * or inserting elements in the tree, so the gfp mask is used to
489 * indicate which allocations or sleeping are allowed.
490 *
491 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
492 * the given range from the tree regardless of state (ie for truncate).
493 *
494 * the range [start, end] is inclusive.
495 *
496 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
497 * bits were already set, or zero if none of the bits were already set.
498 */
499int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -0400500 int bits, int wake, int delete,
501 struct extent_state **cached_state,
502 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500503{
504 struct extent_state *state;
Chris Mason2c64c532009-09-02 15:04:12 -0400505 struct extent_state *cached;
Chris Masond1310b22008-01-24 16:13:08 -0500506 struct extent_state *prealloc = NULL;
Chris Mason2c64c532009-09-02 15:04:12 -0400507 struct rb_node *next_node;
Chris Masond1310b22008-01-24 16:13:08 -0500508 struct rb_node *node;
Yan Zheng5c939df2009-05-27 09:16:03 -0400509 u64 last_end;
Chris Masond1310b22008-01-24 16:13:08 -0500510 int err;
511 int set = 0;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000512 int clear = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500513
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400514 if (delete)
515 bits |= ~EXTENT_CTLBITS;
516 bits |= EXTENT_FIRST_DELALLOC;
517
Josef Bacik2ac55d42010-02-03 19:33:23 +0000518 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
519 clear = 1;
Chris Masond1310b22008-01-24 16:13:08 -0500520again:
521 if (!prealloc && (mask & __GFP_WAIT)) {
522 prealloc = alloc_extent_state(mask);
523 if (!prealloc)
524 return -ENOMEM;
525 }
526
Chris Masoncad321a2008-12-17 14:51:42 -0500527 spin_lock(&tree->lock);
Chris Mason2c64c532009-09-02 15:04:12 -0400528 if (cached_state) {
529 cached = *cached_state;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000530
531 if (clear) {
532 *cached_state = NULL;
533 cached_state = NULL;
534 }
535
Chris Mason42daec22009-09-23 19:51:09 -0400536 if (cached && cached->tree && cached->start == start) {
Josef Bacik2ac55d42010-02-03 19:33:23 +0000537 if (clear)
538 atomic_dec(&cached->refs);
Chris Mason2c64c532009-09-02 15:04:12 -0400539 state = cached;
Chris Mason42daec22009-09-23 19:51:09 -0400540 goto hit_next;
Chris Mason2c64c532009-09-02 15:04:12 -0400541 }
Josef Bacik2ac55d42010-02-03 19:33:23 +0000542 if (clear)
543 free_extent_state(cached);
Chris Mason2c64c532009-09-02 15:04:12 -0400544 }
Chris Masond1310b22008-01-24 16:13:08 -0500545 /*
546 * this search will find the extents that end after
547 * our range starts
548 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500549 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500550 if (!node)
551 goto out;
552 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason2c64c532009-09-02 15:04:12 -0400553hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500554 if (state->start > end)
555 goto out;
556 WARN_ON(state->end < start);
Yan Zheng5c939df2009-05-27 09:16:03 -0400557 last_end = state->end;
Chris Masond1310b22008-01-24 16:13:08 -0500558
559 /*
560 * | ---- desired range ---- |
561 * | state | or
562 * | ------------- state -------------- |
563 *
564 * We need to split the extent we found, and may flip
565 * bits on second half.
566 *
567 * If the extent we found extends past our range, we
568 * just split and search again. It'll get split again
569 * the next time though.
570 *
571 * If the extent we found is inside our range, we clear
572 * the desired bit on it.
573 */
574
575 if (state->start < start) {
Chris Mason70dec802008-01-29 09:59:12 -0500576 if (!prealloc)
577 prealloc = alloc_extent_state(GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500578 err = split_state(tree, state, prealloc, start);
579 BUG_ON(err == -EEXIST);
580 prealloc = NULL;
581 if (err)
582 goto out;
583 if (state->end <= end) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400584 set |= clear_state_bit(tree, state, &bits, wake);
Yan Zheng5c939df2009-05-27 09:16:03 -0400585 if (last_end == (u64)-1)
586 goto out;
587 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500588 }
589 goto search_again;
590 }
591 /*
592 * | ---- desired range ---- |
593 * | state |
594 * We need to split the extent, and clear the bit
595 * on the first half
596 */
597 if (state->start <= end && state->end > end) {
Chris Mason70dec802008-01-29 09:59:12 -0500598 if (!prealloc)
599 prealloc = alloc_extent_state(GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500600 err = split_state(tree, state, prealloc, end + 1);
601 BUG_ON(err == -EEXIST);
Chris Masond1310b22008-01-24 16:13:08 -0500602 if (wake)
603 wake_up(&state->wq);
Chris Mason42daec22009-09-23 19:51:09 -0400604
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400605 set |= clear_state_bit(tree, prealloc, &bits, wake);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400606
Chris Masond1310b22008-01-24 16:13:08 -0500607 prealloc = NULL;
608 goto out;
609 }
Chris Mason42daec22009-09-23 19:51:09 -0400610
Chris Mason2c64c532009-09-02 15:04:12 -0400611 if (state->end < end && prealloc && !need_resched())
612 next_node = rb_next(&state->rb_node);
613 else
614 next_node = NULL;
Chris Mason42daec22009-09-23 19:51:09 -0400615
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400616 set |= clear_state_bit(tree, state, &bits, wake);
Yan Zheng5c939df2009-05-27 09:16:03 -0400617 if (last_end == (u64)-1)
618 goto out;
619 start = last_end + 1;
Chris Mason2c64c532009-09-02 15:04:12 -0400620 if (start <= end && next_node) {
621 state = rb_entry(next_node, struct extent_state,
622 rb_node);
623 if (state->start == start)
624 goto hit_next;
625 }
Chris Masond1310b22008-01-24 16:13:08 -0500626 goto search_again;
627
628out:
Chris Masoncad321a2008-12-17 14:51:42 -0500629 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500630 if (prealloc)
631 free_extent_state(prealloc);
632
633 return set;
634
635search_again:
636 if (start > end)
637 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500638 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500639 if (mask & __GFP_WAIT)
640 cond_resched();
641 goto again;
642}
Chris Masond1310b22008-01-24 16:13:08 -0500643
644static int wait_on_state(struct extent_io_tree *tree,
645 struct extent_state *state)
Christoph Hellwig641f5212008-12-02 06:36:10 -0500646 __releases(tree->lock)
647 __acquires(tree->lock)
Chris Masond1310b22008-01-24 16:13:08 -0500648{
649 DEFINE_WAIT(wait);
650 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Masoncad321a2008-12-17 14:51:42 -0500651 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500652 schedule();
Chris Masoncad321a2008-12-17 14:51:42 -0500653 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500654 finish_wait(&state->wq, &wait);
655 return 0;
656}
657
658/*
659 * waits for one or more bits to clear on a range in the state tree.
660 * The range [start, end] is inclusive.
661 * The tree lock is taken by this function
662 */
663int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
664{
665 struct extent_state *state;
666 struct rb_node *node;
667
Chris Masoncad321a2008-12-17 14:51:42 -0500668 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500669again:
670 while (1) {
671 /*
672 * this search will find all the extents that end after
673 * our range starts
674 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500675 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500676 if (!node)
677 break;
678
679 state = rb_entry(node, struct extent_state, rb_node);
680
681 if (state->start > end)
682 goto out;
683
684 if (state->state & bits) {
685 start = state->start;
686 atomic_inc(&state->refs);
687 wait_on_state(tree, state);
688 free_extent_state(state);
689 goto again;
690 }
691 start = state->end + 1;
692
693 if (start > end)
694 break;
695
696 if (need_resched()) {
Chris Masoncad321a2008-12-17 14:51:42 -0500697 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500698 cond_resched();
Chris Masoncad321a2008-12-17 14:51:42 -0500699 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500700 }
701 }
702out:
Chris Masoncad321a2008-12-17 14:51:42 -0500703 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500704 return 0;
705}
Chris Masond1310b22008-01-24 16:13:08 -0500706
Josef Bacik9ed74f22009-09-11 16:12:44 -0400707static int set_state_bits(struct extent_io_tree *tree,
Chris Masond1310b22008-01-24 16:13:08 -0500708 struct extent_state *state,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400709 int *bits)
Chris Masond1310b22008-01-24 16:13:08 -0500710{
Josef Bacik9ed74f22009-09-11 16:12:44 -0400711 int ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400712 int bits_to_set = *bits & ~EXTENT_CTLBITS;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400713
714 ret = set_state_cb(tree, state, bits);
715 if (ret)
716 return ret;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400717 if ((bits_to_set & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
Chris Masond1310b22008-01-24 16:13:08 -0500718 u64 range = state->end - state->start + 1;
719 tree->dirty_bytes += range;
720 }
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400721 state->state |= bits_to_set;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400722
723 return 0;
Chris Masond1310b22008-01-24 16:13:08 -0500724}
725
Chris Mason2c64c532009-09-02 15:04:12 -0400726static void cache_state(struct extent_state *state,
727 struct extent_state **cached_ptr)
728{
729 if (cached_ptr && !(*cached_ptr)) {
730 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
731 *cached_ptr = state;
732 atomic_inc(&state->refs);
733 }
734 }
735}
736
Chris Masond1310b22008-01-24 16:13:08 -0500737/*
Chris Mason1edbb732009-09-02 13:24:36 -0400738 * set some bits on a range in the tree. This may require allocations or
739 * sleeping, so the gfp mask is used to indicate what is allowed.
Chris Masond1310b22008-01-24 16:13:08 -0500740 *
Chris Mason1edbb732009-09-02 13:24:36 -0400741 * If any of the exclusive bits are set, this will fail with -EEXIST if some
742 * part of the range already has the desired bits set. The start of the
743 * existing range is returned in failed_start in this case.
Chris Masond1310b22008-01-24 16:13:08 -0500744 *
Chris Mason1edbb732009-09-02 13:24:36 -0400745 * [start, end] is inclusive This takes the tree lock.
Chris Masond1310b22008-01-24 16:13:08 -0500746 */
Chris Mason1edbb732009-09-02 13:24:36 -0400747
Chris Mason4845e442010-05-25 20:56:50 -0400748int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
749 int bits, int exclusive_bits, u64 *failed_start,
750 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500751{
752 struct extent_state *state;
753 struct extent_state *prealloc = NULL;
754 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -0500755 int err = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500756 u64 last_start;
757 u64 last_end;
Chris Mason42daec22009-09-23 19:51:09 -0400758
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400759 bits |= EXTENT_FIRST_DELALLOC;
Chris Masond1310b22008-01-24 16:13:08 -0500760again:
761 if (!prealloc && (mask & __GFP_WAIT)) {
762 prealloc = alloc_extent_state(mask);
763 if (!prealloc)
764 return -ENOMEM;
765 }
766
Chris Masoncad321a2008-12-17 14:51:42 -0500767 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -0400768 if (cached_state && *cached_state) {
769 state = *cached_state;
770 if (state->start == start && state->tree) {
771 node = &state->rb_node;
772 goto hit_next;
773 }
774 }
Chris Masond1310b22008-01-24 16:13:08 -0500775 /*
776 * this search will find all the extents that end after
777 * our range starts.
778 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500779 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500780 if (!node) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400781 err = insert_state(tree, prealloc, start, end, &bits);
Chris Masond1310b22008-01-24 16:13:08 -0500782 prealloc = NULL;
783 BUG_ON(err == -EEXIST);
784 goto out;
785 }
Chris Masond1310b22008-01-24 16:13:08 -0500786 state = rb_entry(node, struct extent_state, rb_node);
Chris Mason40431d62009-08-05 12:57:59 -0400787hit_next:
Chris Masond1310b22008-01-24 16:13:08 -0500788 last_start = state->start;
789 last_end = state->end;
790
791 /*
792 * | ---- desired range ---- |
793 * | state |
794 *
795 * Just lock what we found and keep going
796 */
797 if (state->start == start && state->end <= end) {
Chris Mason40431d62009-08-05 12:57:59 -0400798 struct rb_node *next_node;
Chris Mason1edbb732009-09-02 13:24:36 -0400799 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500800 *failed_start = state->start;
801 err = -EEXIST;
802 goto out;
803 }
Chris Mason42daec22009-09-23 19:51:09 -0400804
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400805 err = set_state_bits(tree, state, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400806 if (err)
807 goto out;
808
Chris Mason2c64c532009-09-02 15:04:12 -0400809 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500810 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400811 if (last_end == (u64)-1)
812 goto out;
Chris Mason40431d62009-08-05 12:57:59 -0400813
Yan Zheng5c939df2009-05-27 09:16:03 -0400814 start = last_end + 1;
Chris Mason40431d62009-08-05 12:57:59 -0400815 if (start < end && prealloc && !need_resched()) {
816 next_node = rb_next(node);
817 if (next_node) {
818 state = rb_entry(next_node, struct extent_state,
819 rb_node);
820 if (state->start == start)
821 goto hit_next;
822 }
823 }
Chris Masond1310b22008-01-24 16:13:08 -0500824 goto search_again;
825 }
826
827 /*
828 * | ---- desired range ---- |
829 * | state |
830 * or
831 * | ------------- state -------------- |
832 *
833 * We need to split the extent we found, and may flip bits on
834 * second half.
835 *
836 * If the extent we found extends past our
837 * range, we just split and search again. It'll get split
838 * again the next time though.
839 *
840 * If the extent we found is inside our range, we set the
841 * desired bit on it.
842 */
843 if (state->start < start) {
Chris Mason1edbb732009-09-02 13:24:36 -0400844 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500845 *failed_start = start;
846 err = -EEXIST;
847 goto out;
848 }
849 err = split_state(tree, state, prealloc, start);
850 BUG_ON(err == -EEXIST);
851 prealloc = NULL;
852 if (err)
853 goto out;
854 if (state->end <= end) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400855 err = set_state_bits(tree, state, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400856 if (err)
857 goto out;
Chris Mason2c64c532009-09-02 15:04:12 -0400858 cache_state(state, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500859 merge_state(tree, state);
Yan Zheng5c939df2009-05-27 09:16:03 -0400860 if (last_end == (u64)-1)
861 goto out;
862 start = last_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -0500863 }
864 goto search_again;
865 }
866 /*
867 * | ---- desired range ---- |
868 * | state | or | state |
869 *
870 * There's a hole, we need to insert something in it and
871 * ignore the extent we found.
872 */
873 if (state->start > start) {
874 u64 this_end;
875 if (end < last_start)
876 this_end = end;
877 else
Chris Masond3977122009-01-05 21:25:51 -0500878 this_end = last_start - 1;
Chris Masond1310b22008-01-24 16:13:08 -0500879 err = insert_state(tree, prealloc, start, this_end,
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400880 &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400881 BUG_ON(err == -EEXIST);
882 if (err) {
883 prealloc = NULL;
884 goto out;
885 }
Chris Mason2c64c532009-09-02 15:04:12 -0400886 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500887 prealloc = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500888 start = this_end + 1;
889 goto search_again;
890 }
891 /*
892 * | ---- desired range ---- |
893 * | state |
894 * We need to split the extent, and set the bit
895 * on the first half
896 */
897 if (state->start <= end && state->end > end) {
Chris Mason1edbb732009-09-02 13:24:36 -0400898 if (state->state & exclusive_bits) {
Chris Masond1310b22008-01-24 16:13:08 -0500899 *failed_start = start;
900 err = -EEXIST;
901 goto out;
902 }
903 err = split_state(tree, state, prealloc, end + 1);
904 BUG_ON(err == -EEXIST);
905
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400906 err = set_state_bits(tree, prealloc, &bits);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400907 if (err) {
908 prealloc = NULL;
909 goto out;
910 }
Chris Mason2c64c532009-09-02 15:04:12 -0400911 cache_state(prealloc, cached_state);
Chris Masond1310b22008-01-24 16:13:08 -0500912 merge_state(tree, prealloc);
913 prealloc = NULL;
914 goto out;
915 }
916
917 goto search_again;
918
919out:
Chris Masoncad321a2008-12-17 14:51:42 -0500920 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500921 if (prealloc)
922 free_extent_state(prealloc);
923
924 return err;
925
926search_again:
927 if (start > end)
928 goto out;
Chris Masoncad321a2008-12-17 14:51:42 -0500929 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500930 if (mask & __GFP_WAIT)
931 cond_resched();
932 goto again;
933}
Chris Masond1310b22008-01-24 16:13:08 -0500934
935/* wrappers around set/clear extent bit */
936int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
937 gfp_t mask)
938{
939 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400940 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500941}
Chris Masond1310b22008-01-24 16:13:08 -0500942
943int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
944 int bits, gfp_t mask)
945{
946 return set_extent_bit(tree, start, end, bits, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400947 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500948}
Chris Masond1310b22008-01-24 16:13:08 -0500949
950int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
951 int bits, gfp_t mask)
952{
Chris Mason2c64c532009-09-02 15:04:12 -0400953 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500954}
Chris Masond1310b22008-01-24 16:13:08 -0500955
956int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000957 struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500958{
959 return set_extent_bit(tree, start, end,
Chris Mason40431d62009-08-05 12:57:59 -0400960 EXTENT_DELALLOC | EXTENT_DIRTY | EXTENT_UPTODATE,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000961 0, NULL, cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500962}
Chris Masond1310b22008-01-24 16:13:08 -0500963
964int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
965 gfp_t mask)
966{
967 return clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -0400968 EXTENT_DIRTY | EXTENT_DELALLOC |
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400969 EXTENT_DO_ACCOUNTING, 0, 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500970}
Chris Masond1310b22008-01-24 16:13:08 -0500971
972int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
973 gfp_t mask)
974{
975 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400976 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500977}
Chris Masond1310b22008-01-24 16:13:08 -0500978
Christoph Hellwigb2950862008-12-02 09:54:17 -0500979static int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
Chris Masond1310b22008-01-24 16:13:08 -0500980 gfp_t mask)
981{
Chris Mason2c64c532009-09-02 15:04:12 -0400982 return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0,
983 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500984}
Chris Masond1310b22008-01-24 16:13:08 -0500985
986int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
987 gfp_t mask)
988{
989 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL,
Chris Mason2c64c532009-09-02 15:04:12 -0400990 NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500991}
Chris Masond1310b22008-01-24 16:13:08 -0500992
Chris Masond3977122009-01-05 21:25:51 -0500993static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000994 u64 end, struct extent_state **cached_state,
995 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -0500996{
Chris Mason2c64c532009-09-02 15:04:12 -0400997 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
Josef Bacik2ac55d42010-02-03 19:33:23 +0000998 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500999}
Chris Masond1310b22008-01-24 16:13:08 -05001000
Chris Masond1310b22008-01-24 16:13:08 -05001001int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end)
1002{
1003 return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK);
1004}
Chris Masond1310b22008-01-24 16:13:08 -05001005
Chris Masond352ac62008-09-29 15:18:18 -04001006/*
1007 * either insert or lock state struct between start and end use mask to tell
1008 * us if waiting is desired.
1009 */
Chris Mason1edbb732009-09-02 13:24:36 -04001010int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason2c64c532009-09-02 15:04:12 -04001011 int bits, struct extent_state **cached_state, gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05001012{
1013 int err;
1014 u64 failed_start;
1015 while (1) {
Chris Mason1edbb732009-09-02 13:24:36 -04001016 err = set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
Chris Mason2c64c532009-09-02 15:04:12 -04001017 EXTENT_LOCKED, &failed_start,
1018 cached_state, mask);
Chris Masond1310b22008-01-24 16:13:08 -05001019 if (err == -EEXIST && (mask & __GFP_WAIT)) {
1020 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1021 start = failed_start;
1022 } else {
1023 break;
1024 }
1025 WARN_ON(start > end);
1026 }
1027 return err;
1028}
Chris Masond1310b22008-01-24 16:13:08 -05001029
Chris Mason1edbb732009-09-02 13:24:36 -04001030int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
1031{
Chris Mason2c64c532009-09-02 15:04:12 -04001032 return lock_extent_bits(tree, start, end, 0, NULL, mask);
Chris Mason1edbb732009-09-02 13:24:36 -04001033}
1034
Josef Bacik25179202008-10-29 14:49:05 -04001035int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1036 gfp_t mask)
1037{
1038 int err;
1039 u64 failed_start;
1040
Chris Mason2c64c532009-09-02 15:04:12 -04001041 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1042 &failed_start, NULL, mask);
Yan Zheng66435582008-10-30 14:19:50 -04001043 if (err == -EEXIST) {
1044 if (failed_start > start)
1045 clear_extent_bit(tree, start, failed_start - 1,
Chris Mason2c64c532009-09-02 15:04:12 -04001046 EXTENT_LOCKED, 1, 0, NULL, mask);
Josef Bacik25179202008-10-29 14:49:05 -04001047 return 0;
Yan Zheng66435582008-10-30 14:19:50 -04001048 }
Josef Bacik25179202008-10-29 14:49:05 -04001049 return 1;
1050}
Josef Bacik25179202008-10-29 14:49:05 -04001051
Chris Mason2c64c532009-09-02 15:04:12 -04001052int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1053 struct extent_state **cached, gfp_t mask)
1054{
1055 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1056 mask);
1057}
1058
Chris Masond1310b22008-01-24 16:13:08 -05001059int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1060 gfp_t mask)
1061{
Chris Mason2c64c532009-09-02 15:04:12 -04001062 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1063 mask);
Chris Masond1310b22008-01-24 16:13:08 -05001064}
Chris Masond1310b22008-01-24 16:13:08 -05001065
1066/*
1067 * helper function to set pages and extents in the tree dirty
1068 */
1069int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end)
1070{
1071 unsigned long index = start >> PAGE_CACHE_SHIFT;
1072 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1073 struct page *page;
1074
1075 while (index <= end_index) {
1076 page = find_get_page(tree->mapping, index);
1077 BUG_ON(!page);
1078 __set_page_dirty_nobuffers(page);
1079 page_cache_release(page);
1080 index++;
1081 }
Chris Masond1310b22008-01-24 16:13:08 -05001082 return 0;
1083}
Chris Masond1310b22008-01-24 16:13:08 -05001084
1085/*
1086 * helper function to set both pages and extents in the tree writeback
1087 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05001088static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Chris Masond1310b22008-01-24 16:13:08 -05001089{
1090 unsigned long index = start >> PAGE_CACHE_SHIFT;
1091 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1092 struct page *page;
1093
1094 while (index <= end_index) {
1095 page = find_get_page(tree->mapping, index);
1096 BUG_ON(!page);
1097 set_page_writeback(page);
1098 page_cache_release(page);
1099 index++;
1100 }
Chris Masond1310b22008-01-24 16:13:08 -05001101 return 0;
1102}
Chris Masond1310b22008-01-24 16:13:08 -05001103
Chris Masond352ac62008-09-29 15:18:18 -04001104/*
1105 * find the first offset in the io tree with 'bits' set. zero is
1106 * returned if we find something, and *start_ret and *end_ret are
1107 * set to reflect the state struct that was found.
1108 *
1109 * If nothing was found, 1 is returned, < 0 on error
1110 */
Chris Masond1310b22008-01-24 16:13:08 -05001111int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1112 u64 *start_ret, u64 *end_ret, int bits)
1113{
1114 struct rb_node *node;
1115 struct extent_state *state;
1116 int ret = 1;
1117
Chris Masoncad321a2008-12-17 14:51:42 -05001118 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001119 /*
1120 * this search will find all the extents that end after
1121 * our range starts.
1122 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001123 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001124 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001125 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001126
Chris Masond3977122009-01-05 21:25:51 -05001127 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001128 state = rb_entry(node, struct extent_state, rb_node);
1129 if (state->end >= start && (state->state & bits)) {
1130 *start_ret = state->start;
1131 *end_ret = state->end;
1132 ret = 0;
1133 break;
1134 }
1135 node = rb_next(node);
1136 if (!node)
1137 break;
1138 }
1139out:
Chris Masoncad321a2008-12-17 14:51:42 -05001140 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001141 return ret;
1142}
Chris Masond1310b22008-01-24 16:13:08 -05001143
Chris Masond352ac62008-09-29 15:18:18 -04001144/* find the first state struct with 'bits' set after 'start', and
1145 * return it. tree->lock must be held. NULL will returned if
1146 * nothing was found after 'start'
1147 */
Chris Masond7fc6402008-02-18 12:12:38 -05001148struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1149 u64 start, int bits)
1150{
1151 struct rb_node *node;
1152 struct extent_state *state;
1153
1154 /*
1155 * this search will find all the extents that end after
1156 * our range starts.
1157 */
1158 node = tree_search(tree, start);
Chris Masond3977122009-01-05 21:25:51 -05001159 if (!node)
Chris Masond7fc6402008-02-18 12:12:38 -05001160 goto out;
Chris Masond7fc6402008-02-18 12:12:38 -05001161
Chris Masond3977122009-01-05 21:25:51 -05001162 while (1) {
Chris Masond7fc6402008-02-18 12:12:38 -05001163 state = rb_entry(node, struct extent_state, rb_node);
Chris Masond3977122009-01-05 21:25:51 -05001164 if (state->end >= start && (state->state & bits))
Chris Masond7fc6402008-02-18 12:12:38 -05001165 return state;
Chris Masond3977122009-01-05 21:25:51 -05001166
Chris Masond7fc6402008-02-18 12:12:38 -05001167 node = rb_next(node);
1168 if (!node)
1169 break;
1170 }
1171out:
1172 return NULL;
1173}
Chris Masond7fc6402008-02-18 12:12:38 -05001174
Chris Masond352ac62008-09-29 15:18:18 -04001175/*
1176 * find a contiguous range of bytes in the file marked as delalloc, not
1177 * more than 'max_bytes'. start and end are used to return the range,
1178 *
1179 * 1 is returned if we find something, 0 if nothing was in the tree
1180 */
Chris Masonc8b97812008-10-29 14:49:59 -04001181static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001182 u64 *start, u64 *end, u64 max_bytes,
1183 struct extent_state **cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05001184{
1185 struct rb_node *node;
1186 struct extent_state *state;
1187 u64 cur_start = *start;
1188 u64 found = 0;
1189 u64 total_bytes = 0;
1190
Chris Masoncad321a2008-12-17 14:51:42 -05001191 spin_lock(&tree->lock);
Chris Masonc8b97812008-10-29 14:49:59 -04001192
Chris Masond1310b22008-01-24 16:13:08 -05001193 /*
1194 * this search will find all the extents that end after
1195 * our range starts.
1196 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001197 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001198 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001199 if (!found)
1200 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001201 goto out;
1202 }
1203
Chris Masond3977122009-01-05 21:25:51 -05001204 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001205 state = rb_entry(node, struct extent_state, rb_node);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001206 if (found && (state->start != cur_start ||
1207 (state->state & EXTENT_BOUNDARY))) {
Chris Masond1310b22008-01-24 16:13:08 -05001208 goto out;
1209 }
1210 if (!(state->state & EXTENT_DELALLOC)) {
1211 if (!found)
1212 *end = state->end;
1213 goto out;
1214 }
Josef Bacikc2a128d2010-02-02 21:19:11 +00001215 if (!found) {
Chris Masond1310b22008-01-24 16:13:08 -05001216 *start = state->start;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001217 *cached_state = state;
1218 atomic_inc(&state->refs);
1219 }
Chris Masond1310b22008-01-24 16:13:08 -05001220 found++;
1221 *end = state->end;
1222 cur_start = state->end + 1;
1223 node = rb_next(node);
1224 if (!node)
1225 break;
1226 total_bytes += state->end - state->start + 1;
1227 if (total_bytes >= max_bytes)
1228 break;
1229 }
1230out:
Chris Masoncad321a2008-12-17 14:51:42 -05001231 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001232 return found;
1233}
1234
Chris Masonc8b97812008-10-29 14:49:59 -04001235static noinline int __unlock_for_delalloc(struct inode *inode,
1236 struct page *locked_page,
1237 u64 start, u64 end)
1238{
1239 int ret;
1240 struct page *pages[16];
1241 unsigned long index = start >> PAGE_CACHE_SHIFT;
1242 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1243 unsigned long nr_pages = end_index - index + 1;
1244 int i;
1245
1246 if (index == locked_page->index && end_index == index)
1247 return 0;
1248
Chris Masond3977122009-01-05 21:25:51 -05001249 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001250 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001251 min_t(unsigned long, nr_pages,
1252 ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001253 for (i = 0; i < ret; i++) {
1254 if (pages[i] != locked_page)
1255 unlock_page(pages[i]);
1256 page_cache_release(pages[i]);
1257 }
1258 nr_pages -= ret;
1259 index += ret;
1260 cond_resched();
1261 }
1262 return 0;
1263}
1264
1265static noinline int lock_delalloc_pages(struct inode *inode,
1266 struct page *locked_page,
1267 u64 delalloc_start,
1268 u64 delalloc_end)
1269{
1270 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1271 unsigned long start_index = index;
1272 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1273 unsigned long pages_locked = 0;
1274 struct page *pages[16];
1275 unsigned long nrpages;
1276 int ret;
1277 int i;
1278
1279 /* the caller is responsible for locking the start index */
1280 if (index == locked_page->index && index == end_index)
1281 return 0;
1282
1283 /* skip the page at the start index */
1284 nrpages = end_index - index + 1;
Chris Masond3977122009-01-05 21:25:51 -05001285 while (nrpages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001286 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001287 min_t(unsigned long,
1288 nrpages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001289 if (ret == 0) {
1290 ret = -EAGAIN;
1291 goto done;
1292 }
1293 /* now we have an array of pages, lock them all */
1294 for (i = 0; i < ret; i++) {
1295 /*
1296 * the caller is taking responsibility for
1297 * locked_page
1298 */
Chris Mason771ed682008-11-06 22:02:51 -05001299 if (pages[i] != locked_page) {
Chris Masonc8b97812008-10-29 14:49:59 -04001300 lock_page(pages[i]);
Chris Masonf2b1c412008-11-10 07:31:30 -05001301 if (!PageDirty(pages[i]) ||
1302 pages[i]->mapping != inode->i_mapping) {
Chris Mason771ed682008-11-06 22:02:51 -05001303 ret = -EAGAIN;
1304 unlock_page(pages[i]);
1305 page_cache_release(pages[i]);
1306 goto done;
1307 }
1308 }
Chris Masonc8b97812008-10-29 14:49:59 -04001309 page_cache_release(pages[i]);
Chris Mason771ed682008-11-06 22:02:51 -05001310 pages_locked++;
Chris Masonc8b97812008-10-29 14:49:59 -04001311 }
Chris Masonc8b97812008-10-29 14:49:59 -04001312 nrpages -= ret;
1313 index += ret;
1314 cond_resched();
1315 }
1316 ret = 0;
1317done:
1318 if (ret && pages_locked) {
1319 __unlock_for_delalloc(inode, locked_page,
1320 delalloc_start,
1321 ((u64)(start_index + pages_locked - 1)) <<
1322 PAGE_CACHE_SHIFT);
1323 }
1324 return ret;
1325}
1326
1327/*
1328 * find a contiguous range of bytes in the file marked as delalloc, not
1329 * more than 'max_bytes'. start and end are used to return the range,
1330 *
1331 * 1 is returned if we find something, 0 if nothing was in the tree
1332 */
1333static noinline u64 find_lock_delalloc_range(struct inode *inode,
1334 struct extent_io_tree *tree,
1335 struct page *locked_page,
1336 u64 *start, u64 *end,
1337 u64 max_bytes)
1338{
1339 u64 delalloc_start;
1340 u64 delalloc_end;
1341 u64 found;
Chris Mason9655d292009-09-02 15:22:30 -04001342 struct extent_state *cached_state = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04001343 int ret;
1344 int loops = 0;
1345
1346again:
1347 /* step one, find a bunch of delalloc bytes starting at start */
1348 delalloc_start = *start;
1349 delalloc_end = 0;
1350 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
Josef Bacikc2a128d2010-02-02 21:19:11 +00001351 max_bytes, &cached_state);
Chris Mason70b99e62008-10-31 12:46:39 -04001352 if (!found || delalloc_end <= *start) {
Chris Masonc8b97812008-10-29 14:49:59 -04001353 *start = delalloc_start;
1354 *end = delalloc_end;
Josef Bacikc2a128d2010-02-02 21:19:11 +00001355 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001356 return found;
1357 }
1358
1359 /*
Chris Mason70b99e62008-10-31 12:46:39 -04001360 * start comes from the offset of locked_page. We have to lock
1361 * pages in order, so we can't process delalloc bytes before
1362 * locked_page
1363 */
Chris Masond3977122009-01-05 21:25:51 -05001364 if (delalloc_start < *start)
Chris Mason70b99e62008-10-31 12:46:39 -04001365 delalloc_start = *start;
Chris Mason70b99e62008-10-31 12:46:39 -04001366
1367 /*
Chris Masonc8b97812008-10-29 14:49:59 -04001368 * make sure to limit the number of pages we try to lock down
1369 * if we're looping.
1370 */
Chris Masond3977122009-01-05 21:25:51 -05001371 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
Chris Mason771ed682008-11-06 22:02:51 -05001372 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
Chris Masond3977122009-01-05 21:25:51 -05001373
Chris Masonc8b97812008-10-29 14:49:59 -04001374 /* step two, lock all the pages after the page that has start */
1375 ret = lock_delalloc_pages(inode, locked_page,
1376 delalloc_start, delalloc_end);
1377 if (ret == -EAGAIN) {
1378 /* some of the pages are gone, lets avoid looping by
1379 * shortening the size of the delalloc range we're searching
1380 */
Chris Mason9655d292009-09-02 15:22:30 -04001381 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001382 if (!loops) {
1383 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1384 max_bytes = PAGE_CACHE_SIZE - offset;
1385 loops = 1;
1386 goto again;
1387 } else {
1388 found = 0;
1389 goto out_failed;
1390 }
1391 }
1392 BUG_ON(ret);
1393
1394 /* step three, lock the state bits for the whole range */
Chris Mason9655d292009-09-02 15:22:30 -04001395 lock_extent_bits(tree, delalloc_start, delalloc_end,
1396 0, &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001397
1398 /* then test to make sure it is all still delalloc */
1399 ret = test_range_bit(tree, delalloc_start, delalloc_end,
Chris Mason9655d292009-09-02 15:22:30 -04001400 EXTENT_DELALLOC, 1, cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001401 if (!ret) {
Chris Mason9655d292009-09-02 15:22:30 -04001402 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1403 &cached_state, GFP_NOFS);
Chris Masonc8b97812008-10-29 14:49:59 -04001404 __unlock_for_delalloc(inode, locked_page,
1405 delalloc_start, delalloc_end);
1406 cond_resched();
1407 goto again;
1408 }
Chris Mason9655d292009-09-02 15:22:30 -04001409 free_extent_state(cached_state);
Chris Masonc8b97812008-10-29 14:49:59 -04001410 *start = delalloc_start;
1411 *end = delalloc_end;
1412out_failed:
1413 return found;
1414}
1415
1416int extent_clear_unlock_delalloc(struct inode *inode,
1417 struct extent_io_tree *tree,
1418 u64 start, u64 end, struct page *locked_page,
Chris Masona791e352009-10-08 11:27:10 -04001419 unsigned long op)
Chris Masonc8b97812008-10-29 14:49:59 -04001420{
1421 int ret;
1422 struct page *pages[16];
1423 unsigned long index = start >> PAGE_CACHE_SHIFT;
1424 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1425 unsigned long nr_pages = end_index - index + 1;
1426 int i;
Chris Mason771ed682008-11-06 22:02:51 -05001427 int clear_bits = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001428
Chris Masona791e352009-10-08 11:27:10 -04001429 if (op & EXTENT_CLEAR_UNLOCK)
Chris Mason771ed682008-11-06 22:02:51 -05001430 clear_bits |= EXTENT_LOCKED;
Chris Masona791e352009-10-08 11:27:10 -04001431 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001432 clear_bits |= EXTENT_DIRTY;
1433
Chris Masona791e352009-10-08 11:27:10 -04001434 if (op & EXTENT_CLEAR_DELALLOC)
Chris Mason771ed682008-11-06 22:02:51 -05001435 clear_bits |= EXTENT_DELALLOC;
1436
Chris Mason2c64c532009-09-02 15:04:12 -04001437 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
Josef Bacik32c00af2009-10-08 13:34:05 -04001438 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1439 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1440 EXTENT_SET_PRIVATE2)))
Chris Mason771ed682008-11-06 22:02:51 -05001441 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -04001442
Chris Masond3977122009-01-05 21:25:51 -05001443 while (nr_pages > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -04001444 ret = find_get_pages_contig(inode->i_mapping, index,
Chris Mason5b050f02008-11-11 09:34:41 -05001445 min_t(unsigned long,
1446 nr_pages, ARRAY_SIZE(pages)), pages);
Chris Masonc8b97812008-10-29 14:49:59 -04001447 for (i = 0; i < ret; i++) {
Chris Mason8b62b722009-09-02 16:53:46 -04001448
Chris Masona791e352009-10-08 11:27:10 -04001449 if (op & EXTENT_SET_PRIVATE2)
Chris Mason8b62b722009-09-02 16:53:46 -04001450 SetPagePrivate2(pages[i]);
1451
Chris Masonc8b97812008-10-29 14:49:59 -04001452 if (pages[i] == locked_page) {
1453 page_cache_release(pages[i]);
1454 continue;
1455 }
Chris Masona791e352009-10-08 11:27:10 -04001456 if (op & EXTENT_CLEAR_DIRTY)
Chris Masonc8b97812008-10-29 14:49:59 -04001457 clear_page_dirty_for_io(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001458 if (op & EXTENT_SET_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001459 set_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001460 if (op & EXTENT_END_WRITEBACK)
Chris Masonc8b97812008-10-29 14:49:59 -04001461 end_page_writeback(pages[i]);
Chris Masona791e352009-10-08 11:27:10 -04001462 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
Chris Mason771ed682008-11-06 22:02:51 -05001463 unlock_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -04001464 page_cache_release(pages[i]);
1465 }
1466 nr_pages -= ret;
1467 index += ret;
1468 cond_resched();
1469 }
1470 return 0;
1471}
Chris Masonc8b97812008-10-29 14:49:59 -04001472
Chris Masond352ac62008-09-29 15:18:18 -04001473/*
1474 * count the number of bytes in the tree that have a given bit(s)
1475 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1476 * cached. The total number found is returned.
1477 */
Chris Masond1310b22008-01-24 16:13:08 -05001478u64 count_range_bits(struct extent_io_tree *tree,
1479 u64 *start, u64 search_end, u64 max_bytes,
1480 unsigned long bits)
1481{
1482 struct rb_node *node;
1483 struct extent_state *state;
1484 u64 cur_start = *start;
1485 u64 total_bytes = 0;
1486 int found = 0;
1487
1488 if (search_end <= cur_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001489 WARN_ON(1);
1490 return 0;
1491 }
1492
Chris Masoncad321a2008-12-17 14:51:42 -05001493 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001494 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1495 total_bytes = tree->dirty_bytes;
1496 goto out;
1497 }
1498 /*
1499 * this search will find all the extents that end after
1500 * our range starts.
1501 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001502 node = tree_search(tree, cur_start);
Chris Masond3977122009-01-05 21:25:51 -05001503 if (!node)
Chris Masond1310b22008-01-24 16:13:08 -05001504 goto out;
Chris Masond1310b22008-01-24 16:13:08 -05001505
Chris Masond3977122009-01-05 21:25:51 -05001506 while (1) {
Chris Masond1310b22008-01-24 16:13:08 -05001507 state = rb_entry(node, struct extent_state, rb_node);
1508 if (state->start > search_end)
1509 break;
1510 if (state->end >= cur_start && (state->state & bits)) {
1511 total_bytes += min(search_end, state->end) + 1 -
1512 max(cur_start, state->start);
1513 if (total_bytes >= max_bytes)
1514 break;
1515 if (!found) {
1516 *start = state->start;
1517 found = 1;
1518 }
1519 }
1520 node = rb_next(node);
1521 if (!node)
1522 break;
1523 }
1524out:
Chris Masoncad321a2008-12-17 14:51:42 -05001525 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001526 return total_bytes;
1527}
Christoph Hellwigb2950862008-12-02 09:54:17 -05001528
Chris Masond352ac62008-09-29 15:18:18 -04001529/*
1530 * set the private field for a given byte offset in the tree. If there isn't
1531 * an extent_state there already, this does nothing.
1532 */
Chris Masond1310b22008-01-24 16:13:08 -05001533int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1534{
1535 struct rb_node *node;
1536 struct extent_state *state;
1537 int ret = 0;
1538
Chris Masoncad321a2008-12-17 14:51:42 -05001539 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001540 /*
1541 * this search will find all the extents that end after
1542 * our range starts.
1543 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001544 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001545 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001546 ret = -ENOENT;
1547 goto out;
1548 }
1549 state = rb_entry(node, struct extent_state, rb_node);
1550 if (state->start != start) {
1551 ret = -ENOENT;
1552 goto out;
1553 }
1554 state->private = private;
1555out:
Chris Masoncad321a2008-12-17 14:51:42 -05001556 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001557 return ret;
1558}
1559
1560int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1561{
1562 struct rb_node *node;
1563 struct extent_state *state;
1564 int ret = 0;
1565
Chris Masoncad321a2008-12-17 14:51:42 -05001566 spin_lock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001567 /*
1568 * this search will find all the extents that end after
1569 * our range starts.
1570 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001571 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001572 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001573 ret = -ENOENT;
1574 goto out;
1575 }
1576 state = rb_entry(node, struct extent_state, rb_node);
1577 if (state->start != start) {
1578 ret = -ENOENT;
1579 goto out;
1580 }
1581 *private = state->private;
1582out:
Chris Masoncad321a2008-12-17 14:51:42 -05001583 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001584 return ret;
1585}
1586
1587/*
1588 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001589 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001590 * has the bits set. Otherwise, 1 is returned if any bit in the
1591 * range is found set.
1592 */
1593int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
Chris Mason9655d292009-09-02 15:22:30 -04001594 int bits, int filled, struct extent_state *cached)
Chris Masond1310b22008-01-24 16:13:08 -05001595{
1596 struct extent_state *state = NULL;
1597 struct rb_node *node;
1598 int bitset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001599
Chris Masoncad321a2008-12-17 14:51:42 -05001600 spin_lock(&tree->lock);
Chris Mason9655d292009-09-02 15:22:30 -04001601 if (cached && cached->tree && cached->start == start)
1602 node = &cached->rb_node;
1603 else
1604 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001605 while (node && start <= end) {
1606 state = rb_entry(node, struct extent_state, rb_node);
1607
1608 if (filled && state->start > start) {
1609 bitset = 0;
1610 break;
1611 }
1612
1613 if (state->start > end)
1614 break;
1615
1616 if (state->state & bits) {
1617 bitset = 1;
1618 if (!filled)
1619 break;
1620 } else if (filled) {
1621 bitset = 0;
1622 break;
1623 }
Chris Mason46562cec2009-09-23 20:23:16 -04001624
1625 if (state->end == (u64)-1)
1626 break;
1627
Chris Masond1310b22008-01-24 16:13:08 -05001628 start = state->end + 1;
1629 if (start > end)
1630 break;
1631 node = rb_next(node);
1632 if (!node) {
1633 if (filled)
1634 bitset = 0;
1635 break;
1636 }
1637 }
Chris Masoncad321a2008-12-17 14:51:42 -05001638 spin_unlock(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001639 return bitset;
1640}
Chris Masond1310b22008-01-24 16:13:08 -05001641
1642/*
1643 * helper function to set a given page up to date if all the
1644 * extents in the tree for that page are up to date
1645 */
1646static int check_page_uptodate(struct extent_io_tree *tree,
1647 struct page *page)
1648{
1649 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1650 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001651 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001652 SetPageUptodate(page);
1653 return 0;
1654}
1655
1656/*
1657 * helper function to unlock a page if all the extents in the tree
1658 * for that page are unlocked
1659 */
1660static int check_page_locked(struct extent_io_tree *tree,
1661 struct page *page)
1662{
1663 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1664 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason9655d292009-09-02 15:22:30 -04001665 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
Chris Masond1310b22008-01-24 16:13:08 -05001666 unlock_page(page);
1667 return 0;
1668}
1669
1670/*
1671 * helper function to end page writeback if all the extents
1672 * in the tree for that page are done with writeback
1673 */
1674static int check_page_writeback(struct extent_io_tree *tree,
1675 struct page *page)
1676{
Chris Mason1edbb732009-09-02 13:24:36 -04001677 end_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05001678 return 0;
1679}
1680
1681/* lots and lots of room for performance fixes in the end_bio funcs */
1682
1683/*
1684 * after a writepage IO is done, we need to:
1685 * clear the uptodate bits on error
1686 * clear the writeback bits in the extent tree for this IO
1687 * end_page_writeback if the page has no more pending IO
1688 *
1689 * Scheduling is not allowed, so the extent state tree is expected
1690 * to have one and only one object corresponding to this IO.
1691 */
Chris Masond1310b22008-01-24 16:13:08 -05001692static void end_bio_extent_writepage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001693{
Chris Mason1259ab72008-05-12 13:39:03 -04001694 int uptodate = err == 0;
Chris Masond1310b22008-01-24 16:13:08 -05001695 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001696 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001697 u64 start;
1698 u64 end;
1699 int whole_page;
Chris Mason1259ab72008-05-12 13:39:03 -04001700 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05001701
Chris Masond1310b22008-01-24 16:13:08 -05001702 do {
1703 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001704 tree = &BTRFS_I(page->mapping->host)->io_tree;
1705
Chris Masond1310b22008-01-24 16:13:08 -05001706 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1707 bvec->bv_offset;
1708 end = start + bvec->bv_len - 1;
1709
1710 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1711 whole_page = 1;
1712 else
1713 whole_page = 0;
1714
1715 if (--bvec >= bio->bi_io_vec)
1716 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001717 if (tree->ops && tree->ops->writepage_end_io_hook) {
1718 ret = tree->ops->writepage_end_io_hook(page, start,
David Woodhouse902b22f2008-08-20 08:51:49 -04001719 end, NULL, uptodate);
Chris Mason1259ab72008-05-12 13:39:03 -04001720 if (ret)
1721 uptodate = 0;
1722 }
1723
1724 if (!uptodate && tree->ops &&
1725 tree->ops->writepage_io_failed_hook) {
1726 ret = tree->ops->writepage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001727 start, end, NULL);
Chris Mason1259ab72008-05-12 13:39:03 -04001728 if (ret == 0) {
Chris Mason1259ab72008-05-12 13:39:03 -04001729 uptodate = (err == 0);
1730 continue;
1731 }
1732 }
1733
Chris Masond1310b22008-01-24 16:13:08 -05001734 if (!uptodate) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00001735 clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001736 ClearPageUptodate(page);
1737 SetPageError(page);
1738 }
Chris Mason70dec802008-01-29 09:59:12 -05001739
Chris Masond1310b22008-01-24 16:13:08 -05001740 if (whole_page)
1741 end_page_writeback(page);
1742 else
1743 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05001744 } while (bvec >= bio->bi_io_vec);
Chris Mason2b1f55b2008-09-24 11:48:04 -04001745
Chris Masond1310b22008-01-24 16:13:08 -05001746 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001747}
1748
1749/*
1750 * after a readpage IO is done, we need to:
1751 * clear the uptodate bits on error
1752 * set the uptodate bits if things worked
1753 * set the page up to date if all extents in the tree are uptodate
1754 * clear the lock bit in the extent tree
1755 * unlock the page if there are no other extents locked for it
1756 *
1757 * Scheduling is not allowed, so the extent state tree is expected
1758 * to have one and only one object corresponding to this IO.
1759 */
Chris Masond1310b22008-01-24 16:13:08 -05001760static void end_bio_extent_readpage(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001761{
1762 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason4125bf72010-02-03 18:18:45 +00001763 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
1764 struct bio_vec *bvec = bio->bi_io_vec;
David Woodhouse902b22f2008-08-20 08:51:49 -04001765 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001766 u64 start;
1767 u64 end;
1768 int whole_page;
1769 int ret;
1770
Chris Masond20f7042008-12-08 16:58:54 -05001771 if (err)
1772 uptodate = 0;
1773
Chris Masond1310b22008-01-24 16:13:08 -05001774 do {
1775 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001776 tree = &BTRFS_I(page->mapping->host)->io_tree;
1777
Chris Masond1310b22008-01-24 16:13:08 -05001778 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1779 bvec->bv_offset;
1780 end = start + bvec->bv_len - 1;
1781
1782 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1783 whole_page = 1;
1784 else
1785 whole_page = 0;
1786
Chris Mason4125bf72010-02-03 18:18:45 +00001787 if (++bvec <= bvec_end)
Chris Masond1310b22008-01-24 16:13:08 -05001788 prefetchw(&bvec->bv_page->flags);
1789
1790 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05001791 ret = tree->ops->readpage_end_io_hook(page, start, end,
David Woodhouse902b22f2008-08-20 08:51:49 -04001792 NULL);
Chris Masond1310b22008-01-24 16:13:08 -05001793 if (ret)
1794 uptodate = 0;
1795 }
Chris Mason7e383262008-04-09 16:28:12 -04001796 if (!uptodate && tree->ops &&
1797 tree->ops->readpage_io_failed_hook) {
1798 ret = tree->ops->readpage_io_failed_hook(bio, page,
David Woodhouse902b22f2008-08-20 08:51:49 -04001799 start, end, NULL);
Chris Mason7e383262008-04-09 16:28:12 -04001800 if (ret == 0) {
Chris Mason3b951512008-04-17 11:29:12 -04001801 uptodate =
1802 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Masond20f7042008-12-08 16:58:54 -05001803 if (err)
1804 uptodate = 0;
Chris Mason7e383262008-04-09 16:28:12 -04001805 continue;
1806 }
1807 }
Chris Mason70dec802008-01-29 09:59:12 -05001808
Chris Mason771ed682008-11-06 22:02:51 -05001809 if (uptodate) {
David Woodhouse902b22f2008-08-20 08:51:49 -04001810 set_extent_uptodate(tree, start, end,
1811 GFP_ATOMIC);
Chris Mason771ed682008-11-06 22:02:51 -05001812 }
David Woodhouse902b22f2008-08-20 08:51:49 -04001813 unlock_extent(tree, start, end, GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -05001814
Chris Mason70dec802008-01-29 09:59:12 -05001815 if (whole_page) {
1816 if (uptodate) {
1817 SetPageUptodate(page);
1818 } else {
1819 ClearPageUptodate(page);
1820 SetPageError(page);
1821 }
Chris Masond1310b22008-01-24 16:13:08 -05001822 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05001823 } else {
1824 if (uptodate) {
1825 check_page_uptodate(tree, page);
1826 } else {
1827 ClearPageUptodate(page);
1828 SetPageError(page);
1829 }
Chris Masond1310b22008-01-24 16:13:08 -05001830 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05001831 }
Chris Mason4125bf72010-02-03 18:18:45 +00001832 } while (bvec <= bvec_end);
Chris Masond1310b22008-01-24 16:13:08 -05001833
1834 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001835}
1836
1837/*
1838 * IO done from prepare_write is pretty simple, we just unlock
1839 * the structs in the extent tree when done, and set the uptodate bits
1840 * as appropriate.
1841 */
Chris Masond1310b22008-01-24 16:13:08 -05001842static void end_bio_extent_preparewrite(struct bio *bio, int err)
Chris Masond1310b22008-01-24 16:13:08 -05001843{
1844 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1845 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
David Woodhouse902b22f2008-08-20 08:51:49 -04001846 struct extent_io_tree *tree;
Chris Masond1310b22008-01-24 16:13:08 -05001847 u64 start;
1848 u64 end;
1849
Chris Masond1310b22008-01-24 16:13:08 -05001850 do {
1851 struct page *page = bvec->bv_page;
David Woodhouse902b22f2008-08-20 08:51:49 -04001852 tree = &BTRFS_I(page->mapping->host)->io_tree;
1853
Chris Masond1310b22008-01-24 16:13:08 -05001854 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1855 bvec->bv_offset;
1856 end = start + bvec->bv_len - 1;
1857
1858 if (--bvec >= bio->bi_io_vec)
1859 prefetchw(&bvec->bv_page->flags);
1860
1861 if (uptodate) {
1862 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1863 } else {
1864 ClearPageUptodate(page);
1865 SetPageError(page);
1866 }
1867
1868 unlock_extent(tree, start, end, GFP_ATOMIC);
1869
1870 } while (bvec >= bio->bi_io_vec);
1871
1872 bio_put(bio);
Chris Masond1310b22008-01-24 16:13:08 -05001873}
1874
1875static struct bio *
1876extent_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1877 gfp_t gfp_flags)
1878{
1879 struct bio *bio;
1880
1881 bio = bio_alloc(gfp_flags, nr_vecs);
1882
1883 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1884 while (!bio && (nr_vecs /= 2))
1885 bio = bio_alloc(gfp_flags, nr_vecs);
1886 }
1887
1888 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04001889 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001890 bio->bi_bdev = bdev;
1891 bio->bi_sector = first_sector;
1892 }
1893 return bio;
1894}
1895
Chris Masonc8b97812008-10-29 14:49:59 -04001896static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
1897 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001898{
Chris Masond1310b22008-01-24 16:13:08 -05001899 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05001900 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1901 struct page *page = bvec->bv_page;
1902 struct extent_io_tree *tree = bio->bi_private;
Chris Mason70dec802008-01-29 09:59:12 -05001903 u64 start;
1904 u64 end;
1905
1906 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
1907 end = start + bvec->bv_len - 1;
1908
David Woodhouse902b22f2008-08-20 08:51:49 -04001909 bio->bi_private = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05001910
1911 bio_get(bio);
1912
Chris Mason065631f2008-02-20 12:07:25 -05001913 if (tree->ops && tree->ops->submit_bio_hook)
Chris Masonf1885912008-04-09 16:28:12 -04001914 tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04001915 mirror_num, bio_flags, start);
Chris Mason0b86a832008-03-24 15:01:56 -04001916 else
1917 submit_bio(rw, bio);
Chris Masond1310b22008-01-24 16:13:08 -05001918 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1919 ret = -EOPNOTSUPP;
1920 bio_put(bio);
1921 return ret;
1922}
1923
1924static int submit_extent_page(int rw, struct extent_io_tree *tree,
1925 struct page *page, sector_t sector,
1926 size_t size, unsigned long offset,
1927 struct block_device *bdev,
1928 struct bio **bio_ret,
1929 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04001930 bio_end_io_t end_io_func,
Chris Masonc8b97812008-10-29 14:49:59 -04001931 int mirror_num,
1932 unsigned long prev_bio_flags,
1933 unsigned long bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05001934{
1935 int ret = 0;
1936 struct bio *bio;
1937 int nr;
Chris Masonc8b97812008-10-29 14:49:59 -04001938 int contig = 0;
1939 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
1940 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
Chris Mason5b050f02008-11-11 09:34:41 -05001941 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -05001942
1943 if (bio_ret && *bio_ret) {
1944 bio = *bio_ret;
Chris Masonc8b97812008-10-29 14:49:59 -04001945 if (old_compressed)
1946 contig = bio->bi_sector == sector;
1947 else
1948 contig = bio->bi_sector + (bio->bi_size >> 9) ==
1949 sector;
1950
1951 if (prev_bio_flags != bio_flags || !contig ||
Chris Mason239b14b2008-03-24 15:02:07 -04001952 (tree->ops && tree->ops->merge_bio_hook &&
Chris Masonc8b97812008-10-29 14:49:59 -04001953 tree->ops->merge_bio_hook(page, offset, page_size, bio,
1954 bio_flags)) ||
1955 bio_add_page(bio, page, page_size, offset) < page_size) {
1956 ret = submit_one_bio(rw, bio, mirror_num,
1957 prev_bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001958 bio = NULL;
1959 } else {
1960 return 0;
1961 }
1962 }
Chris Masonc8b97812008-10-29 14:49:59 -04001963 if (this_compressed)
1964 nr = BIO_MAX_PAGES;
1965 else
1966 nr = bio_get_nr_vecs(bdev);
1967
Chris Masond1310b22008-01-24 16:13:08 -05001968 bio = extent_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
Chris Mason70dec802008-01-29 09:59:12 -05001969
Chris Masonc8b97812008-10-29 14:49:59 -04001970 bio_add_page(bio, page, page_size, offset);
Chris Masond1310b22008-01-24 16:13:08 -05001971 bio->bi_end_io = end_io_func;
1972 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05001973
Chris Masond3977122009-01-05 21:25:51 -05001974 if (bio_ret)
Chris Masond1310b22008-01-24 16:13:08 -05001975 *bio_ret = bio;
Chris Masond3977122009-01-05 21:25:51 -05001976 else
Chris Masonc8b97812008-10-29 14:49:59 -04001977 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05001978
1979 return ret;
1980}
1981
1982void set_page_extent_mapped(struct page *page)
1983{
1984 if (!PagePrivate(page)) {
1985 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001986 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04001987 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05001988 }
1989}
1990
Christoph Hellwigb2950862008-12-02 09:54:17 -05001991static void set_page_extent_head(struct page *page, unsigned long len)
Chris Masond1310b22008-01-24 16:13:08 -05001992{
1993 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
1994}
1995
1996/*
1997 * basic readpage implementation. Locked extent state structs are inserted
1998 * into the tree that are removed when the IO is done (by the end_io
1999 * handlers)
2000 */
2001static int __extent_read_full_page(struct extent_io_tree *tree,
2002 struct page *page,
2003 get_extent_t *get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002004 struct bio **bio, int mirror_num,
2005 unsigned long *bio_flags)
Chris Masond1310b22008-01-24 16:13:08 -05002006{
2007 struct inode *inode = page->mapping->host;
2008 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2009 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2010 u64 end;
2011 u64 cur = start;
2012 u64 extent_offset;
2013 u64 last_byte = i_size_read(inode);
2014 u64 block_start;
2015 u64 cur_end;
2016 sector_t sector;
2017 struct extent_map *em;
2018 struct block_device *bdev;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002019 struct btrfs_ordered_extent *ordered;
Chris Masond1310b22008-01-24 16:13:08 -05002020 int ret;
2021 int nr = 0;
2022 size_t page_offset = 0;
2023 size_t iosize;
Chris Masonc8b97812008-10-29 14:49:59 -04002024 size_t disk_io_size;
Chris Masond1310b22008-01-24 16:13:08 -05002025 size_t blocksize = inode->i_sb->s_blocksize;
Chris Masonc8b97812008-10-29 14:49:59 -04002026 unsigned long this_bio_flag = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002027
2028 set_page_extent_mapped(page);
2029
2030 end = page_end;
Josef Bacik11c65dc2010-05-23 11:07:21 -04002031 while (1) {
2032 lock_extent(tree, start, end, GFP_NOFS);
2033 ordered = btrfs_lookup_ordered_extent(inode, start);
2034 if (!ordered)
2035 break;
2036 unlock_extent(tree, start, end, GFP_NOFS);
2037 btrfs_start_ordered_extent(inode, ordered, 1);
2038 btrfs_put_ordered_extent(ordered);
2039 }
Chris Masond1310b22008-01-24 16:13:08 -05002040
Chris Masonc8b97812008-10-29 14:49:59 -04002041 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2042 char *userpage;
2043 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2044
2045 if (zero_offset) {
2046 iosize = PAGE_CACHE_SIZE - zero_offset;
2047 userpage = kmap_atomic(page, KM_USER0);
2048 memset(userpage + zero_offset, 0, iosize);
2049 flush_dcache_page(page);
2050 kunmap_atomic(userpage, KM_USER0);
2051 }
2052 }
Chris Masond1310b22008-01-24 16:13:08 -05002053 while (cur <= end) {
2054 if (cur >= last_byte) {
2055 char *userpage;
2056 iosize = PAGE_CACHE_SIZE - page_offset;
2057 userpage = kmap_atomic(page, KM_USER0);
2058 memset(userpage + page_offset, 0, iosize);
2059 flush_dcache_page(page);
2060 kunmap_atomic(userpage, KM_USER0);
2061 set_extent_uptodate(tree, cur, cur + iosize - 1,
2062 GFP_NOFS);
2063 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2064 break;
2065 }
2066 em = get_extent(inode, page, page_offset, cur,
2067 end - cur + 1, 0);
2068 if (IS_ERR(em) || !em) {
2069 SetPageError(page);
2070 unlock_extent(tree, cur, end, GFP_NOFS);
2071 break;
2072 }
Chris Masond1310b22008-01-24 16:13:08 -05002073 extent_offset = cur - em->start;
2074 BUG_ON(extent_map_end(em) <= cur);
2075 BUG_ON(end < cur);
2076
Chris Masonc8b97812008-10-29 14:49:59 -04002077 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
2078 this_bio_flag = EXTENT_BIO_COMPRESSED;
2079
Chris Masond1310b22008-01-24 16:13:08 -05002080 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2081 cur_end = min(extent_map_end(em) - 1, end);
2082 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002083 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2084 disk_io_size = em->block_len;
2085 sector = em->block_start >> 9;
2086 } else {
2087 sector = (em->block_start + extent_offset) >> 9;
2088 disk_io_size = iosize;
2089 }
Chris Masond1310b22008-01-24 16:13:08 -05002090 bdev = em->bdev;
2091 block_start = em->block_start;
Yan Zhengd899e052008-10-30 14:25:28 -04002092 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2093 block_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05002094 free_extent_map(em);
2095 em = NULL;
2096
2097 /* we've found a hole, just zero and go on */
2098 if (block_start == EXTENT_MAP_HOLE) {
2099 char *userpage;
2100 userpage = kmap_atomic(page, KM_USER0);
2101 memset(userpage + page_offset, 0, iosize);
2102 flush_dcache_page(page);
2103 kunmap_atomic(userpage, KM_USER0);
2104
2105 set_extent_uptodate(tree, cur, cur + iosize - 1,
2106 GFP_NOFS);
2107 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2108 cur = cur + iosize;
2109 page_offset += iosize;
2110 continue;
2111 }
2112 /* the get_extent function already copied into the page */
Chris Mason9655d292009-09-02 15:22:30 -04002113 if (test_range_bit(tree, cur, cur_end,
2114 EXTENT_UPTODATE, 1, NULL)) {
Chris Masona1b32a52008-09-05 16:09:51 -04002115 check_page_uptodate(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05002116 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2117 cur = cur + iosize;
2118 page_offset += iosize;
2119 continue;
2120 }
Chris Mason70dec802008-01-29 09:59:12 -05002121 /* we have an inline extent but it didn't get marked up
2122 * to date. Error out
2123 */
2124 if (block_start == EXTENT_MAP_INLINE) {
2125 SetPageError(page);
2126 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2127 cur = cur + iosize;
2128 page_offset += iosize;
2129 continue;
2130 }
Chris Masond1310b22008-01-24 16:13:08 -05002131
2132 ret = 0;
2133 if (tree->ops && tree->ops->readpage_io_hook) {
2134 ret = tree->ops->readpage_io_hook(page, cur,
2135 cur + iosize - 1);
2136 }
2137 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04002138 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2139 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05002140 ret = submit_extent_page(READ, tree, page,
Chris Masonc8b97812008-10-29 14:49:59 -04002141 sector, disk_io_size, page_offset,
Chris Mason89642222008-07-24 09:41:53 -04002142 bdev, bio, pnr,
Chris Masonc8b97812008-10-29 14:49:59 -04002143 end_bio_extent_readpage, mirror_num,
2144 *bio_flags,
2145 this_bio_flag);
Chris Mason89642222008-07-24 09:41:53 -04002146 nr++;
Chris Masonc8b97812008-10-29 14:49:59 -04002147 *bio_flags = this_bio_flag;
Chris Masond1310b22008-01-24 16:13:08 -05002148 }
2149 if (ret)
2150 SetPageError(page);
2151 cur = cur + iosize;
2152 page_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002153 }
2154 if (!nr) {
2155 if (!PageError(page))
2156 SetPageUptodate(page);
2157 unlock_page(page);
2158 }
2159 return 0;
2160}
2161
2162int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2163 get_extent_t *get_extent)
2164{
2165 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04002166 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002167 int ret;
2168
Chris Masonc8b97812008-10-29 14:49:59 -04002169 ret = __extent_read_full_page(tree, page, get_extent, &bio, 0,
2170 &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002171 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002172 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002173 return ret;
2174}
Chris Masond1310b22008-01-24 16:13:08 -05002175
Chris Mason11c83492009-04-20 15:50:09 -04002176static noinline void update_nr_written(struct page *page,
2177 struct writeback_control *wbc,
2178 unsigned long nr_written)
2179{
2180 wbc->nr_to_write -= nr_written;
2181 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2182 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2183 page->mapping->writeback_index = page->index + nr_written;
2184}
2185
Chris Masond1310b22008-01-24 16:13:08 -05002186/*
2187 * the writepage semantics are similar to regular writepage. extent
2188 * records are inserted to lock ranges in the tree, and as dirty areas
2189 * are found, they are marked writeback. Then the lock bits are removed
2190 * and the end_io handler clears the writeback ranges
2191 */
2192static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2193 void *data)
2194{
2195 struct inode *inode = page->mapping->host;
2196 struct extent_page_data *epd = data;
2197 struct extent_io_tree *tree = epd->tree;
2198 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2199 u64 delalloc_start;
2200 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2201 u64 end;
2202 u64 cur = start;
2203 u64 extent_offset;
2204 u64 last_byte = i_size_read(inode);
2205 u64 block_start;
2206 u64 iosize;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002207 u64 unlock_start;
Chris Masond1310b22008-01-24 16:13:08 -05002208 sector_t sector;
Chris Mason2c64c532009-09-02 15:04:12 -04002209 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002210 struct extent_map *em;
2211 struct block_device *bdev;
2212 int ret;
2213 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002214 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002215 size_t blocksize;
2216 loff_t i_size = i_size_read(inode);
2217 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2218 u64 nr_delalloc;
2219 u64 delalloc_end;
Chris Masonc8b97812008-10-29 14:49:59 -04002220 int page_started;
2221 int compressed;
Chris Masonffbd5172009-04-20 15:50:09 -04002222 int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -05002223 unsigned long nr_written = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002224
Chris Masonffbd5172009-04-20 15:50:09 -04002225 if (wbc->sync_mode == WB_SYNC_ALL)
2226 write_flags = WRITE_SYNC_PLUG;
2227 else
2228 write_flags = WRITE;
2229
Chris Masond1310b22008-01-24 16:13:08 -05002230 WARN_ON(!PageLocked(page));
Chris Mason7f3c74f2008-07-18 12:01:11 -04002231 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002232 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002233 (page->index == end_index && !pg_offset)) {
Chris Mason39be25c2008-11-10 11:50:50 -05002234 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002235 unlock_page(page);
2236 return 0;
2237 }
2238
2239 if (page->index == end_index) {
2240 char *userpage;
2241
Chris Masond1310b22008-01-24 16:13:08 -05002242 userpage = kmap_atomic(page, KM_USER0);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002243 memset(userpage + pg_offset, 0,
2244 PAGE_CACHE_SIZE - pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002245 kunmap_atomic(userpage, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002246 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002247 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002248 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002249
2250 set_page_extent_mapped(page);
2251
2252 delalloc_start = start;
2253 delalloc_end = 0;
Chris Masonc8b97812008-10-29 14:49:59 -04002254 page_started = 0;
Chris Mason771ed682008-11-06 22:02:51 -05002255 if (!epd->extent_locked) {
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002256 u64 delalloc_to_write = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002257 /*
2258 * make sure the wbc mapping index is at least updated
2259 * to this page.
2260 */
2261 update_nr_written(page, wbc, 0);
2262
Chris Masond3977122009-01-05 21:25:51 -05002263 while (delalloc_end < page_end) {
Chris Mason771ed682008-11-06 22:02:51 -05002264 nr_delalloc = find_lock_delalloc_range(inode, tree,
Chris Masonc8b97812008-10-29 14:49:59 -04002265 page,
2266 &delalloc_start,
Chris Masond1310b22008-01-24 16:13:08 -05002267 &delalloc_end,
2268 128 * 1024 * 1024);
Chris Mason771ed682008-11-06 22:02:51 -05002269 if (nr_delalloc == 0) {
2270 delalloc_start = delalloc_end + 1;
2271 continue;
2272 }
2273 tree->ops->fill_delalloc(inode, page, delalloc_start,
2274 delalloc_end, &page_started,
2275 &nr_written);
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002276 /*
2277 * delalloc_end is already one less than the total
2278 * length, so we don't subtract one from
2279 * PAGE_CACHE_SIZE
2280 */
2281 delalloc_to_write += (delalloc_end - delalloc_start +
2282 PAGE_CACHE_SIZE) >>
2283 PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05002284 delalloc_start = delalloc_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002285 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002286 if (wbc->nr_to_write < delalloc_to_write) {
2287 int thresh = 8192;
2288
2289 if (delalloc_to_write < thresh * 2)
2290 thresh = delalloc_to_write;
2291 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2292 thresh);
2293 }
Chris Masonc8b97812008-10-29 14:49:59 -04002294
Chris Mason771ed682008-11-06 22:02:51 -05002295 /* did the fill delalloc function already unlock and start
2296 * the IO?
2297 */
2298 if (page_started) {
2299 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002300 /*
2301 * we've unlocked the page, so we can't update
2302 * the mapping's writeback index, just update
2303 * nr_to_write.
2304 */
2305 wbc->nr_to_write -= nr_written;
2306 goto done_unlocked;
Chris Mason771ed682008-11-06 22:02:51 -05002307 }
Chris Masonc8b97812008-10-29 14:49:59 -04002308 }
Chris Mason247e7432008-07-17 12:53:51 -04002309 if (tree->ops && tree->ops->writepage_start_hook) {
Chris Masonc8b97812008-10-29 14:49:59 -04002310 ret = tree->ops->writepage_start_hook(page, start,
2311 page_end);
Chris Mason247e7432008-07-17 12:53:51 -04002312 if (ret == -EAGAIN) {
Chris Mason247e7432008-07-17 12:53:51 -04002313 redirty_page_for_writepage(wbc, page);
Chris Mason11c83492009-04-20 15:50:09 -04002314 update_nr_written(page, wbc, nr_written);
Chris Mason247e7432008-07-17 12:53:51 -04002315 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002316 ret = 0;
Chris Mason11c83492009-04-20 15:50:09 -04002317 goto done_unlocked;
Chris Mason247e7432008-07-17 12:53:51 -04002318 }
2319 }
2320
Chris Mason11c83492009-04-20 15:50:09 -04002321 /*
2322 * we don't want to touch the inode after unlocking the page,
2323 * so we update the mapping writeback index now
2324 */
2325 update_nr_written(page, wbc, nr_written + 1);
Chris Mason771ed682008-11-06 22:02:51 -05002326
Chris Masond1310b22008-01-24 16:13:08 -05002327 end = page_end;
Chris Masond1310b22008-01-24 16:13:08 -05002328 if (last_byte <= start) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002329 if (tree->ops && tree->ops->writepage_end_io_hook)
2330 tree->ops->writepage_end_io_hook(page, start,
2331 page_end, NULL, 1);
2332 unlock_start = page_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002333 goto done;
2334 }
2335
Chris Masond1310b22008-01-24 16:13:08 -05002336 blocksize = inode->i_sb->s_blocksize;
2337
2338 while (cur <= end) {
2339 if (cur >= last_byte) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002340 if (tree->ops && tree->ops->writepage_end_io_hook)
2341 tree->ops->writepage_end_io_hook(page, cur,
2342 page_end, NULL, 1);
2343 unlock_start = page_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002344 break;
2345 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002346 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002347 end - cur + 1, 1);
2348 if (IS_ERR(em) || !em) {
2349 SetPageError(page);
2350 break;
2351 }
2352
2353 extent_offset = cur - em->start;
2354 BUG_ON(extent_map_end(em) <= cur);
2355 BUG_ON(end < cur);
2356 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2357 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2358 sector = (em->block_start + extent_offset) >> 9;
2359 bdev = em->bdev;
2360 block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -04002361 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Masond1310b22008-01-24 16:13:08 -05002362 free_extent_map(em);
2363 em = NULL;
2364
Chris Masonc8b97812008-10-29 14:49:59 -04002365 /*
2366 * compressed and inline extents are written through other
2367 * paths in the FS
2368 */
2369 if (compressed || block_start == EXTENT_MAP_HOLE ||
Chris Masond1310b22008-01-24 16:13:08 -05002370 block_start == EXTENT_MAP_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04002371 /*
2372 * end_io notification does not happen here for
2373 * compressed extents
2374 */
2375 if (!compressed && tree->ops &&
2376 tree->ops->writepage_end_io_hook)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002377 tree->ops->writepage_end_io_hook(page, cur,
2378 cur + iosize - 1,
2379 NULL, 1);
Chris Masonc8b97812008-10-29 14:49:59 -04002380 else if (compressed) {
2381 /* we don't want to end_page_writeback on
2382 * a compressed extent. this happens
2383 * elsewhere
2384 */
2385 nr++;
2386 }
2387
2388 cur += iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002389 pg_offset += iosize;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002390 unlock_start = cur;
Chris Masond1310b22008-01-24 16:13:08 -05002391 continue;
2392 }
Chris Masond1310b22008-01-24 16:13:08 -05002393 /* leave this out until we have a page_mkwrite call */
2394 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
Chris Mason9655d292009-09-02 15:22:30 -04002395 EXTENT_DIRTY, 0, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002396 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002397 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002398 continue;
2399 }
Chris Masonc8b97812008-10-29 14:49:59 -04002400
Chris Masond1310b22008-01-24 16:13:08 -05002401 if (tree->ops && tree->ops->writepage_io_hook) {
2402 ret = tree->ops->writepage_io_hook(page, cur,
2403 cur + iosize - 1);
2404 } else {
2405 ret = 0;
2406 }
Chris Mason1259ab72008-05-12 13:39:03 -04002407 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002408 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002409 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002410 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002411
Chris Masond1310b22008-01-24 16:13:08 -05002412 set_range_writeback(tree, cur, cur + iosize - 1);
2413 if (!PageWriteback(page)) {
Chris Masond3977122009-01-05 21:25:51 -05002414 printk(KERN_ERR "btrfs warning page %lu not "
2415 "writeback, cur %llu end %llu\n",
2416 page->index, (unsigned long long)cur,
Chris Masond1310b22008-01-24 16:13:08 -05002417 (unsigned long long)end);
2418 }
2419
Chris Masonffbd5172009-04-20 15:50:09 -04002420 ret = submit_extent_page(write_flags, tree, page,
2421 sector, iosize, pg_offset,
2422 bdev, &epd->bio, max_nr,
Chris Masonc8b97812008-10-29 14:49:59 -04002423 end_bio_extent_writepage,
2424 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002425 if (ret)
2426 SetPageError(page);
2427 }
2428 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002429 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002430 nr++;
2431 }
2432done:
2433 if (nr == 0) {
2434 /* make sure the mapping tag for page dirty gets cleared */
2435 set_page_writeback(page);
2436 end_page_writeback(page);
2437 }
Chris Masond1310b22008-01-24 16:13:08 -05002438 unlock_page(page);
Chris Mason771ed682008-11-06 22:02:51 -05002439
Chris Mason11c83492009-04-20 15:50:09 -04002440done_unlocked:
2441
Chris Mason2c64c532009-09-02 15:04:12 -04002442 /* drop our reference on any cached states */
2443 free_extent_state(cached_state);
Chris Masond1310b22008-01-24 16:13:08 -05002444 return 0;
2445}
2446
Chris Masond1310b22008-01-24 16:13:08 -05002447/**
Chris Mason4bef0842008-09-08 11:18:08 -04002448 * 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 -05002449 * @mapping: address space structure to write
2450 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2451 * @writepage: function called for each page
2452 * @data: data passed to writepage function
2453 *
2454 * If a page is already under I/O, write_cache_pages() skips it, even
2455 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2456 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2457 * and msync() need to guarantee that all the data which was dirty at the time
2458 * the call was made get new I/O started against them. If wbc->sync_mode is
2459 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2460 * existing IO to complete.
2461 */
Christoph Hellwigb2950862008-12-02 09:54:17 -05002462static int extent_write_cache_pages(struct extent_io_tree *tree,
Chris Mason4bef0842008-09-08 11:18:08 -04002463 struct address_space *mapping,
2464 struct writeback_control *wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002465 writepage_t writepage, void *data,
2466 void (*flush_fn)(void *))
Chris Masond1310b22008-01-24 16:13:08 -05002467{
Chris Masond1310b22008-01-24 16:13:08 -05002468 int ret = 0;
2469 int done = 0;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002470 int nr_to_write_done = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002471 struct pagevec pvec;
2472 int nr_pages;
2473 pgoff_t index;
2474 pgoff_t end; /* Inclusive */
2475 int scanned = 0;
2476 int range_whole = 0;
2477
Chris Masond1310b22008-01-24 16:13:08 -05002478 pagevec_init(&pvec, 0);
2479 if (wbc->range_cyclic) {
2480 index = mapping->writeback_index; /* Start from prev offset */
2481 end = -1;
2482 } else {
2483 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2484 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2485 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2486 range_whole = 1;
2487 scanned = 1;
2488 }
2489retry:
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002490 while (!done && !nr_to_write_done && (index <= end) &&
Chris Masond1310b22008-01-24 16:13:08 -05002491 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
Chris Masond3977122009-01-05 21:25:51 -05002492 PAGECACHE_TAG_DIRTY, min(end - index,
2493 (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
Chris Masond1310b22008-01-24 16:13:08 -05002494 unsigned i;
2495
2496 scanned = 1;
2497 for (i = 0; i < nr_pages; i++) {
2498 struct page *page = pvec.pages[i];
2499
2500 /*
2501 * At this point we hold neither mapping->tree_lock nor
2502 * lock on the page itself: the page may be truncated or
2503 * invalidated (changing page->mapping to NULL), or even
2504 * swizzled back from swapper_space to tmpfs file
2505 * mapping
2506 */
Chris Mason4bef0842008-09-08 11:18:08 -04002507 if (tree->ops && tree->ops->write_cache_pages_lock_hook)
2508 tree->ops->write_cache_pages_lock_hook(page);
2509 else
2510 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002511
2512 if (unlikely(page->mapping != mapping)) {
2513 unlock_page(page);
2514 continue;
2515 }
2516
2517 if (!wbc->range_cyclic && page->index > end) {
2518 done = 1;
2519 unlock_page(page);
2520 continue;
2521 }
2522
Chris Masond2c3f4f2008-11-19 12:44:22 -05002523 if (wbc->sync_mode != WB_SYNC_NONE) {
Chris Mason0e6bd952008-11-20 10:46:35 -05002524 if (PageWriteback(page))
2525 flush_fn(data);
Chris Masond1310b22008-01-24 16:13:08 -05002526 wait_on_page_writeback(page);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002527 }
Chris Masond1310b22008-01-24 16:13:08 -05002528
2529 if (PageWriteback(page) ||
2530 !clear_page_dirty_for_io(page)) {
2531 unlock_page(page);
2532 continue;
2533 }
2534
2535 ret = (*writepage)(page, wbc, data);
2536
2537 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2538 unlock_page(page);
2539 ret = 0;
2540 }
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002541 if (ret)
Chris Masond1310b22008-01-24 16:13:08 -05002542 done = 1;
Chris Masonf85d7d6c2009-09-18 16:03:16 -04002543
2544 /*
2545 * the filesystem may choose to bump up nr_to_write.
2546 * We have to make sure to honor the new nr_to_write
2547 * at any time
2548 */
2549 nr_to_write_done = wbc->nr_to_write <= 0;
Chris Masond1310b22008-01-24 16:13:08 -05002550 }
2551 pagevec_release(&pvec);
2552 cond_resched();
2553 }
2554 if (!scanned && !done) {
2555 /*
2556 * We hit the last page and there is more work to be done: wrap
2557 * back to the start of the file
2558 */
2559 scanned = 1;
2560 index = 0;
2561 goto retry;
2562 }
Chris Masond1310b22008-01-24 16:13:08 -05002563 return ret;
2564}
Chris Masond1310b22008-01-24 16:13:08 -05002565
Chris Masonffbd5172009-04-20 15:50:09 -04002566static void flush_epd_write_bio(struct extent_page_data *epd)
2567{
2568 if (epd->bio) {
2569 if (epd->sync_io)
2570 submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
2571 else
2572 submit_one_bio(WRITE, epd->bio, 0, 0);
2573 epd->bio = NULL;
2574 }
2575}
2576
Chris Masond2c3f4f2008-11-19 12:44:22 -05002577static noinline void flush_write_bio(void *data)
2578{
2579 struct extent_page_data *epd = data;
Chris Masonffbd5172009-04-20 15:50:09 -04002580 flush_epd_write_bio(epd);
Chris Masond2c3f4f2008-11-19 12:44:22 -05002581}
2582
Chris Masond1310b22008-01-24 16:13:08 -05002583int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2584 get_extent_t *get_extent,
2585 struct writeback_control *wbc)
2586{
2587 int ret;
2588 struct address_space *mapping = page->mapping;
2589 struct extent_page_data epd = {
2590 .bio = NULL,
2591 .tree = tree,
2592 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002593 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002594 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002595 };
2596 struct writeback_control wbc_writepages = {
2597 .bdi = wbc->bdi,
Chris Masond313d7a2009-04-20 15:50:09 -04002598 .sync_mode = wbc->sync_mode,
Chris Masond1310b22008-01-24 16:13:08 -05002599 .older_than_this = NULL,
2600 .nr_to_write = 64,
2601 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2602 .range_end = (loff_t)-1,
2603 };
2604
Chris Masond1310b22008-01-24 16:13:08 -05002605 ret = __extent_writepage(page, wbc, &epd);
2606
Chris Mason4bef0842008-09-08 11:18:08 -04002607 extent_write_cache_pages(tree, mapping, &wbc_writepages,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002608 __extent_writepage, &epd, flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002609 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002610 return ret;
2611}
Chris Masond1310b22008-01-24 16:13:08 -05002612
Chris Mason771ed682008-11-06 22:02:51 -05002613int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
2614 u64 start, u64 end, get_extent_t *get_extent,
2615 int mode)
2616{
2617 int ret = 0;
2618 struct address_space *mapping = inode->i_mapping;
2619 struct page *page;
2620 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
2621 PAGE_CACHE_SHIFT;
2622
2623 struct extent_page_data epd = {
2624 .bio = NULL,
2625 .tree = tree,
2626 .get_extent = get_extent,
2627 .extent_locked = 1,
Chris Masonffbd5172009-04-20 15:50:09 -04002628 .sync_io = mode == WB_SYNC_ALL,
Chris Mason771ed682008-11-06 22:02:51 -05002629 };
2630 struct writeback_control wbc_writepages = {
2631 .bdi = inode->i_mapping->backing_dev_info,
2632 .sync_mode = mode,
2633 .older_than_this = NULL,
2634 .nr_to_write = nr_pages * 2,
2635 .range_start = start,
2636 .range_end = end + 1,
2637 };
2638
Chris Masond3977122009-01-05 21:25:51 -05002639 while (start <= end) {
Chris Mason771ed682008-11-06 22:02:51 -05002640 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
2641 if (clear_page_dirty_for_io(page))
2642 ret = __extent_writepage(page, &wbc_writepages, &epd);
2643 else {
2644 if (tree->ops && tree->ops->writepage_end_io_hook)
2645 tree->ops->writepage_end_io_hook(page, start,
2646 start + PAGE_CACHE_SIZE - 1,
2647 NULL, 1);
2648 unlock_page(page);
2649 }
2650 page_cache_release(page);
2651 start += PAGE_CACHE_SIZE;
2652 }
2653
Chris Masonffbd5172009-04-20 15:50:09 -04002654 flush_epd_write_bio(&epd);
Chris Mason771ed682008-11-06 22:02:51 -05002655 return ret;
2656}
Chris Masond1310b22008-01-24 16:13:08 -05002657
2658int extent_writepages(struct extent_io_tree *tree,
2659 struct address_space *mapping,
2660 get_extent_t *get_extent,
2661 struct writeback_control *wbc)
2662{
2663 int ret = 0;
2664 struct extent_page_data epd = {
2665 .bio = NULL,
2666 .tree = tree,
2667 .get_extent = get_extent,
Chris Mason771ed682008-11-06 22:02:51 -05002668 .extent_locked = 0,
Chris Masonffbd5172009-04-20 15:50:09 -04002669 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
Chris Masond1310b22008-01-24 16:13:08 -05002670 };
2671
Chris Mason4bef0842008-09-08 11:18:08 -04002672 ret = extent_write_cache_pages(tree, mapping, wbc,
Chris Masond2c3f4f2008-11-19 12:44:22 -05002673 __extent_writepage, &epd,
2674 flush_write_bio);
Chris Masonffbd5172009-04-20 15:50:09 -04002675 flush_epd_write_bio(&epd);
Chris Masond1310b22008-01-24 16:13:08 -05002676 return ret;
2677}
Chris Masond1310b22008-01-24 16:13:08 -05002678
2679int extent_readpages(struct extent_io_tree *tree,
2680 struct address_space *mapping,
2681 struct list_head *pages, unsigned nr_pages,
2682 get_extent_t get_extent)
2683{
2684 struct bio *bio = NULL;
2685 unsigned page_idx;
Chris Masonc8b97812008-10-29 14:49:59 -04002686 unsigned long bio_flags = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002687
Chris Masond1310b22008-01-24 16:13:08 -05002688 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2689 struct page *page = list_entry(pages->prev, struct page, lru);
2690
2691 prefetchw(&page->flags);
2692 list_del(&page->lru);
Nick Piggin28ecb6092010-03-17 13:31:04 +00002693 if (!add_to_page_cache_lru(page, mapping,
Chris Masond1310b22008-01-24 16:13:08 -05002694 page->index, GFP_KERNEL)) {
Chris Masonf1885912008-04-09 16:28:12 -04002695 __extent_read_full_page(tree, page, get_extent,
Chris Masonc8b97812008-10-29 14:49:59 -04002696 &bio, 0, &bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002697 }
2698 page_cache_release(page);
2699 }
Chris Masond1310b22008-01-24 16:13:08 -05002700 BUG_ON(!list_empty(pages));
2701 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04002702 submit_one_bio(READ, bio, 0, bio_flags);
Chris Masond1310b22008-01-24 16:13:08 -05002703 return 0;
2704}
Chris Masond1310b22008-01-24 16:13:08 -05002705
2706/*
2707 * basic invalidatepage code, this waits on any locked or writeback
2708 * ranges corresponding to the page, and then deletes any extent state
2709 * records from the tree
2710 */
2711int extent_invalidatepage(struct extent_io_tree *tree,
2712 struct page *page, unsigned long offset)
2713{
Josef Bacik2ac55d42010-02-03 19:33:23 +00002714 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002715 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2716 u64 end = start + PAGE_CACHE_SIZE - 1;
2717 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2718
Chris Masond3977122009-01-05 21:25:51 -05002719 start += (offset + blocksize - 1) & ~(blocksize - 1);
Chris Masond1310b22008-01-24 16:13:08 -05002720 if (start > end)
2721 return 0;
2722
Josef Bacik2ac55d42010-02-03 19:33:23 +00002723 lock_extent_bits(tree, start, end, 0, &cached_state, GFP_NOFS);
Chris Mason1edbb732009-09-02 13:24:36 -04002724 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002725 clear_extent_bit(tree, start, end,
Josef Bacik32c00af2009-10-08 13:34:05 -04002726 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2727 EXTENT_DO_ACCOUNTING,
Josef Bacik2ac55d42010-02-03 19:33:23 +00002728 1, 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002729 return 0;
2730}
Chris Masond1310b22008-01-24 16:13:08 -05002731
2732/*
2733 * simple commit_write call, set_range_dirty is used to mark both
2734 * the pages and the extent records as dirty
2735 */
2736int extent_commit_write(struct extent_io_tree *tree,
2737 struct inode *inode, struct page *page,
2738 unsigned from, unsigned to)
2739{
2740 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2741
2742 set_page_extent_mapped(page);
2743 set_page_dirty(page);
2744
2745 if (pos > inode->i_size) {
2746 i_size_write(inode, pos);
2747 mark_inode_dirty(inode);
2748 }
2749 return 0;
2750}
Chris Masond1310b22008-01-24 16:13:08 -05002751
2752int extent_prepare_write(struct extent_io_tree *tree,
2753 struct inode *inode, struct page *page,
2754 unsigned from, unsigned to, get_extent_t *get_extent)
2755{
2756 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2757 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2758 u64 block_start;
2759 u64 orig_block_start;
2760 u64 block_end;
2761 u64 cur_end;
2762 struct extent_map *em;
2763 unsigned blocksize = 1 << inode->i_blkbits;
2764 size_t page_offset = 0;
2765 size_t block_off_start;
2766 size_t block_off_end;
2767 int err = 0;
2768 int iocount = 0;
2769 int ret = 0;
2770 int isnew;
2771
2772 set_page_extent_mapped(page);
2773
2774 block_start = (page_start + from) & ~((u64)blocksize - 1);
2775 block_end = (page_start + to - 1) | (blocksize - 1);
2776 orig_block_start = block_start;
2777
2778 lock_extent(tree, page_start, page_end, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -05002779 while (block_start <= block_end) {
Chris Masond1310b22008-01-24 16:13:08 -05002780 em = get_extent(inode, page, page_offset, block_start,
2781 block_end - block_start + 1, 1);
Chris Masond3977122009-01-05 21:25:51 -05002782 if (IS_ERR(em) || !em)
Chris Masond1310b22008-01-24 16:13:08 -05002783 goto err;
Chris Masond3977122009-01-05 21:25:51 -05002784
Chris Masond1310b22008-01-24 16:13:08 -05002785 cur_end = min(block_end, extent_map_end(em) - 1);
2786 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2787 block_off_end = block_off_start + blocksize;
2788 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2789
2790 if (!PageUptodate(page) && isnew &&
2791 (block_off_end > to || block_off_start < from)) {
2792 void *kaddr;
2793
2794 kaddr = kmap_atomic(page, KM_USER0);
2795 if (block_off_end > to)
2796 memset(kaddr + to, 0, block_off_end - to);
2797 if (block_off_start < from)
2798 memset(kaddr + block_off_start, 0,
2799 from - block_off_start);
2800 flush_dcache_page(page);
2801 kunmap_atomic(kaddr, KM_USER0);
2802 }
2803 if ((em->block_start != EXTENT_MAP_HOLE &&
2804 em->block_start != EXTENT_MAP_INLINE) &&
2805 !isnew && !PageUptodate(page) &&
2806 (block_off_end > to || block_off_start < from) &&
2807 !test_range_bit(tree, block_start, cur_end,
Chris Mason9655d292009-09-02 15:22:30 -04002808 EXTENT_UPTODATE, 1, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05002809 u64 sector;
2810 u64 extent_offset = block_start - em->start;
2811 size_t iosize;
2812 sector = (em->block_start + extent_offset) >> 9;
2813 iosize = (cur_end - block_start + blocksize) &
2814 ~((u64)blocksize - 1);
2815 /*
2816 * we've already got the extent locked, but we
2817 * need to split the state such that our end_bio
2818 * handler can clear the lock.
2819 */
2820 set_extent_bit(tree, block_start,
2821 block_start + iosize - 1,
Chris Mason2c64c532009-09-02 15:04:12 -04002822 EXTENT_LOCKED, 0, NULL, NULL, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002823 ret = submit_extent_page(READ, tree, page,
2824 sector, iosize, page_offset, em->bdev,
2825 NULL, 1,
Chris Masonc8b97812008-10-29 14:49:59 -04002826 end_bio_extent_preparewrite, 0,
2827 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002828 iocount++;
2829 block_start = block_start + iosize;
2830 } else {
2831 set_extent_uptodate(tree, block_start, cur_end,
2832 GFP_NOFS);
2833 unlock_extent(tree, block_start, cur_end, GFP_NOFS);
2834 block_start = cur_end + 1;
2835 }
2836 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2837 free_extent_map(em);
2838 }
2839 if (iocount) {
2840 wait_extent_bit(tree, orig_block_start,
2841 block_end, EXTENT_LOCKED);
2842 }
2843 check_page_uptodate(tree, page);
2844err:
2845 /* FIXME, zero out newly allocated blocks on error */
2846 return err;
2847}
Chris Masond1310b22008-01-24 16:13:08 -05002848
2849/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04002850 * a helper for releasepage, this tests for areas of the page that
2851 * are locked or under IO and drops the related state bits if it is safe
2852 * to drop the page.
2853 */
2854int try_release_extent_state(struct extent_map_tree *map,
2855 struct extent_io_tree *tree, struct page *page,
2856 gfp_t mask)
2857{
2858 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2859 u64 end = start + PAGE_CACHE_SIZE - 1;
2860 int ret = 1;
2861
Chris Mason211f90e2008-07-18 11:56:15 -04002862 if (test_range_bit(tree, start, end,
Chris Mason8b62b722009-09-02 16:53:46 -04002863 EXTENT_IOBITS, 0, NULL))
Chris Mason7b13b7b2008-04-18 10:29:50 -04002864 ret = 0;
2865 else {
2866 if ((mask & GFP_NOFS) == GFP_NOFS)
2867 mask = GFP_NOFS;
Chris Mason11ef1602009-09-23 20:28:46 -04002868 /*
2869 * at this point we can safely clear everything except the
2870 * locked bit and the nodatasum bit
2871 */
2872 clear_extent_bit(tree, start, end,
2873 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
2874 0, 0, NULL, mask);
Chris Mason7b13b7b2008-04-18 10:29:50 -04002875 }
2876 return ret;
2877}
Chris Mason7b13b7b2008-04-18 10:29:50 -04002878
2879/*
Chris Masond1310b22008-01-24 16:13:08 -05002880 * a helper for releasepage. As long as there are no locked extents
2881 * in the range corresponding to the page, both state records and extent
2882 * map records are removed
2883 */
2884int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05002885 struct extent_io_tree *tree, struct page *page,
2886 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05002887{
2888 struct extent_map *em;
2889 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2890 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04002891
Chris Mason70dec802008-01-29 09:59:12 -05002892 if ((mask & __GFP_WAIT) &&
2893 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05002894 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05002895 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05002896 len = end - start + 1;
Chris Mason890871b2009-09-02 16:24:52 -04002897 write_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05002898 em = lookup_extent_mapping(map, start, len);
Chris Mason70dec802008-01-29 09:59:12 -05002899 if (!em || IS_ERR(em)) {
Chris Mason890871b2009-09-02 16:24:52 -04002900 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002901 break;
2902 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002903 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
2904 em->start != start) {
Chris Mason890871b2009-09-02 16:24:52 -04002905 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002906 free_extent_map(em);
2907 break;
2908 }
2909 if (!test_range_bit(tree, em->start,
2910 extent_map_end(em) - 1,
Chris Mason8b62b722009-09-02 16:53:46 -04002911 EXTENT_LOCKED | EXTENT_WRITEBACK,
Chris Mason9655d292009-09-02 15:22:30 -04002912 0, NULL)) {
Chris Mason70dec802008-01-29 09:59:12 -05002913 remove_extent_mapping(map, em);
2914 /* once for the rb tree */
2915 free_extent_map(em);
2916 }
2917 start = extent_map_end(em);
Chris Mason890871b2009-09-02 16:24:52 -04002918 write_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002919
2920 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05002921 free_extent_map(em);
2922 }
Chris Masond1310b22008-01-24 16:13:08 -05002923 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04002924 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05002925}
Chris Masond1310b22008-01-24 16:13:08 -05002926
2927sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2928 get_extent_t *get_extent)
2929{
2930 struct inode *inode = mapping->host;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002931 struct extent_state *cached_state = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002932 u64 start = iblock << inode->i_blkbits;
2933 sector_t sector = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002934 size_t blksize = (1 << inode->i_blkbits);
Chris Masond1310b22008-01-24 16:13:08 -05002935 struct extent_map *em;
2936
Josef Bacik2ac55d42010-02-03 19:33:23 +00002937 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2938 0, &cached_state, GFP_NOFS);
Yan Zhengd899e052008-10-30 14:25:28 -04002939 em = get_extent(inode, NULL, 0, start, blksize, 0);
Josef Bacik2ac55d42010-02-03 19:33:23 +00002940 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start,
2941 start + blksize - 1, &cached_state, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002942 if (!em || IS_ERR(em))
2943 return 0;
2944
Yan Zhengd899e052008-10-30 14:25:28 -04002945 if (em->block_start > EXTENT_MAP_LAST_BYTE)
Chris Masond1310b22008-01-24 16:13:08 -05002946 goto out;
2947
2948 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
Chris Masond1310b22008-01-24 16:13:08 -05002949out:
2950 free_extent_map(em);
2951 return sector;
2952}
2953
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002954int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2955 __u64 start, __u64 len, get_extent_t *get_extent)
2956{
2957 int ret;
2958 u64 off = start;
2959 u64 max = start + len;
2960 u32 flags = 0;
2961 u64 disko = 0;
2962 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002963 struct extent_state *cached_state = NULL;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002964 int end = 0;
2965 u64 em_start = 0, em_len = 0;
2966 unsigned long emflags;
2967 ret = 0;
2968
2969 if (len == 0)
2970 return -EINVAL;
2971
Josef Bacik2ac55d42010-02-03 19:33:23 +00002972 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
2973 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002974 em = get_extent(inode, NULL, 0, off, max - off, 0);
2975 if (!em)
2976 goto out;
2977 if (IS_ERR(em)) {
2978 ret = PTR_ERR(em);
2979 goto out;
2980 }
2981 while (!end) {
2982 off = em->start + em->len;
2983 if (off >= max)
2984 end = 1;
2985
2986 em_start = em->start;
2987 em_len = em->len;
2988
2989 disko = 0;
2990 flags = 0;
2991
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002992 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002993 end = 1;
2994 flags |= FIEMAP_EXTENT_LAST;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002995 } else if (em->block_start == EXTENT_MAP_HOLE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002996 flags |= FIEMAP_EXTENT_UNWRITTEN;
Heiko Carstens93dbfad2009-04-03 10:33:45 -04002997 } else if (em->block_start == EXTENT_MAP_INLINE) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05002998 flags |= (FIEMAP_EXTENT_DATA_INLINE |
2999 FIEMAP_EXTENT_NOT_ALIGNED);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003000 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003001 flags |= (FIEMAP_EXTENT_DELALLOC |
3002 FIEMAP_EXTENT_UNKNOWN);
Heiko Carstens93dbfad2009-04-03 10:33:45 -04003003 } else {
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003004 disko = em->block_start;
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003005 }
3006 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3007 flags |= FIEMAP_EXTENT_ENCODED;
3008
3009 emflags = em->flags;
3010 free_extent_map(em);
3011 em = NULL;
3012
3013 if (!end) {
3014 em = get_extent(inode, NULL, 0, off, max - off, 0);
3015 if (!em)
3016 goto out;
3017 if (IS_ERR(em)) {
3018 ret = PTR_ERR(em);
3019 goto out;
3020 }
3021 emflags = em->flags;
3022 }
3023 if (test_bit(EXTENT_FLAG_VACANCY, &emflags)) {
3024 flags |= FIEMAP_EXTENT_LAST;
3025 end = 1;
3026 }
3027
3028 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3029 em_len, flags);
3030 if (ret)
3031 goto out_free;
3032 }
3033out_free:
3034 free_extent_map(em);
3035out:
Josef Bacik2ac55d42010-02-03 19:33:23 +00003036 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3037 &cached_state, GFP_NOFS);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05003038 return ret;
3039}
3040
Chris Masond1310b22008-01-24 16:13:08 -05003041static inline struct page *extent_buffer_page(struct extent_buffer *eb,
3042 unsigned long i)
3043{
3044 struct page *p;
3045 struct address_space *mapping;
3046
3047 if (i == 0)
3048 return eb->first_page;
3049 i += eb->start >> PAGE_CACHE_SHIFT;
3050 mapping = eb->first_page->mapping;
Chris Mason33958dc2008-07-30 10:29:12 -04003051 if (!mapping)
3052 return NULL;
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003053
3054 /*
3055 * extent_buffer_page is only called after pinning the page
3056 * by increasing the reference count. So we know the page must
3057 * be in the radix tree.
3058 */
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003059 rcu_read_lock();
Chris Masond1310b22008-01-24 16:13:08 -05003060 p = radix_tree_lookup(&mapping->page_tree, i);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003061 rcu_read_unlock();
Chris Mason2b1f55b2008-09-24 11:48:04 -04003062
Chris Masond1310b22008-01-24 16:13:08 -05003063 return p;
3064}
3065
Chris Mason6af118ce2008-07-22 11:18:07 -04003066static inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04003067{
Chris Mason6af118ce2008-07-22 11:18:07 -04003068 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3069 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04003070}
3071
Chris Masond1310b22008-01-24 16:13:08 -05003072static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3073 u64 start,
3074 unsigned long len,
3075 gfp_t mask)
3076{
3077 struct extent_buffer *eb = NULL;
Chris Mason39351272009-02-04 09:24:05 -05003078#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003079 unsigned long flags;
Chris Mason4bef0842008-09-08 11:18:08 -04003080#endif
Chris Masond1310b22008-01-24 16:13:08 -05003081
Chris Masond1310b22008-01-24 16:13:08 -05003082 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Chris Masond1310b22008-01-24 16:13:08 -05003083 eb->start = start;
3084 eb->len = len;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003085 spin_lock_init(&eb->lock);
3086 init_waitqueue_head(&eb->lock_wq);
3087
Chris Mason39351272009-02-04 09:24:05 -05003088#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003089 spin_lock_irqsave(&leak_lock, flags);
3090 list_add(&eb->leak_list, &buffers);
3091 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003092#endif
Chris Masond1310b22008-01-24 16:13:08 -05003093 atomic_set(&eb->refs, 1);
3094
3095 return eb;
3096}
3097
3098static void __free_extent_buffer(struct extent_buffer *eb)
3099{
Chris Mason39351272009-02-04 09:24:05 -05003100#if LEAK_DEBUG
Chris Mason2d2ae542008-03-26 16:24:23 -04003101 unsigned long flags;
3102 spin_lock_irqsave(&leak_lock, flags);
3103 list_del(&eb->leak_list);
3104 spin_unlock_irqrestore(&leak_lock, flags);
Chris Mason4bef0842008-09-08 11:18:08 -04003105#endif
Chris Masond1310b22008-01-24 16:13:08 -05003106 kmem_cache_free(extent_buffer_cache, eb);
3107}
3108
3109struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3110 u64 start, unsigned long len,
3111 struct page *page0,
3112 gfp_t mask)
3113{
3114 unsigned long num_pages = num_extent_pages(start, len);
3115 unsigned long i;
3116 unsigned long index = start >> PAGE_CACHE_SHIFT;
3117 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04003118 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05003119 struct page *p;
3120 struct address_space *mapping = tree->mapping;
3121 int uptodate = 1;
3122
Chris Mason6af118ce2008-07-22 11:18:07 -04003123 spin_lock(&tree->buffer_lock);
3124 eb = buffer_search(tree, start);
3125 if (eb) {
3126 atomic_inc(&eb->refs);
3127 spin_unlock(&tree->buffer_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003128 mark_page_accessed(eb->first_page);
Chris Mason6af118ce2008-07-22 11:18:07 -04003129 return eb;
3130 }
3131 spin_unlock(&tree->buffer_lock);
3132
Chris Masond1310b22008-01-24 16:13:08 -05003133 eb = __alloc_extent_buffer(tree, start, len, mask);
Peter2b114d12008-04-01 11:21:40 -04003134 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05003135 return NULL;
3136
Chris Masond1310b22008-01-24 16:13:08 -05003137 if (page0) {
3138 eb->first_page = page0;
3139 i = 1;
3140 index++;
3141 page_cache_get(page0);
3142 mark_page_accessed(page0);
3143 set_page_extent_mapped(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003144 set_page_extent_head(page0, len);
Chris Masonf1885912008-04-09 16:28:12 -04003145 uptodate = PageUptodate(page0);
Chris Masond1310b22008-01-24 16:13:08 -05003146 } else {
3147 i = 0;
3148 }
3149 for (; i < num_pages; i++, index++) {
3150 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
3151 if (!p) {
3152 WARN_ON(1);
Chris Mason6af118ce2008-07-22 11:18:07 -04003153 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05003154 }
3155 set_page_extent_mapped(p);
3156 mark_page_accessed(p);
3157 if (i == 0) {
3158 eb->first_page = p;
3159 set_page_extent_head(p, len);
3160 } else {
3161 set_page_private(p, EXTENT_PAGE_PRIVATE);
3162 }
3163 if (!PageUptodate(p))
3164 uptodate = 0;
3165 unlock_page(p);
3166 }
3167 if (uptodate)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003168 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003169
Chris Mason6af118ce2008-07-22 11:18:07 -04003170 spin_lock(&tree->buffer_lock);
3171 exists = buffer_tree_insert(tree, start, &eb->rb_node);
3172 if (exists) {
3173 /* add one reference for the caller */
3174 atomic_inc(&exists->refs);
3175 spin_unlock(&tree->buffer_lock);
3176 goto free_eb;
3177 }
Chris Mason6af118ce2008-07-22 11:18:07 -04003178 /* add one reference for the tree */
3179 atomic_inc(&eb->refs);
Yan, Zhengf044ba72010-02-04 08:46:56 +00003180 spin_unlock(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003181 return eb;
3182
Chris Mason6af118ce2008-07-22 11:18:07 -04003183free_eb:
Chris Masond1310b22008-01-24 16:13:08 -05003184 if (!atomic_dec_and_test(&eb->refs))
Chris Mason6af118ce2008-07-22 11:18:07 -04003185 return exists;
3186 for (index = 1; index < i; index++)
Chris Masond1310b22008-01-24 16:13:08 -05003187 page_cache_release(extent_buffer_page(eb, index));
Chris Mason6af118ce2008-07-22 11:18:07 -04003188 page_cache_release(extent_buffer_page(eb, 0));
Chris Masond1310b22008-01-24 16:13:08 -05003189 __free_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04003190 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05003191}
Chris Masond1310b22008-01-24 16:13:08 -05003192
3193struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
3194 u64 start, unsigned long len,
3195 gfp_t mask)
3196{
Chris Masond1310b22008-01-24 16:13:08 -05003197 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05003198
Chris Mason6af118ce2008-07-22 11:18:07 -04003199 spin_lock(&tree->buffer_lock);
3200 eb = buffer_search(tree, start);
3201 if (eb)
3202 atomic_inc(&eb->refs);
3203 spin_unlock(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003204
Josef Bacik0f9dd462008-09-23 13:14:11 -04003205 if (eb)
3206 mark_page_accessed(eb->first_page);
3207
Chris Masond1310b22008-01-24 16:13:08 -05003208 return eb;
Chris Masond1310b22008-01-24 16:13:08 -05003209}
Chris Masond1310b22008-01-24 16:13:08 -05003210
3211void free_extent_buffer(struct extent_buffer *eb)
3212{
Chris Masond1310b22008-01-24 16:13:08 -05003213 if (!eb)
3214 return;
3215
3216 if (!atomic_dec_and_test(&eb->refs))
3217 return;
3218
Chris Mason6af118ce2008-07-22 11:18:07 -04003219 WARN_ON(1);
Chris Masond1310b22008-01-24 16:13:08 -05003220}
Chris Masond1310b22008-01-24 16:13:08 -05003221
3222int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3223 struct extent_buffer *eb)
3224{
Chris Masond1310b22008-01-24 16:13:08 -05003225 unsigned long i;
3226 unsigned long num_pages;
3227 struct page *page;
3228
Chris Masond1310b22008-01-24 16:13:08 -05003229 num_pages = num_extent_pages(eb->start, eb->len);
3230
3231 for (i = 0; i < num_pages; i++) {
3232 page = extent_buffer_page(eb, i);
Chris Masonb9473432009-03-13 11:00:37 -04003233 if (!PageDirty(page))
Chris Masond2c3f4f2008-11-19 12:44:22 -05003234 continue;
3235
Chris Masona61e6f22008-07-22 11:18:08 -04003236 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003237 if (i == 0)
3238 set_page_extent_head(page, eb->len);
3239 else
3240 set_page_private(page, EXTENT_PAGE_PRIVATE);
3241
Chris Masond1310b22008-01-24 16:13:08 -05003242 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003243 spin_lock_irq(&page->mapping->tree_lock);
Chris Masond1310b22008-01-24 16:13:08 -05003244 if (!PageDirty(page)) {
3245 radix_tree_tag_clear(&page->mapping->page_tree,
3246 page_index(page),
3247 PAGECACHE_TAG_DIRTY);
3248 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003249 spin_unlock_irq(&page->mapping->tree_lock);
Chris Masona61e6f22008-07-22 11:18:08 -04003250 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05003251 }
3252 return 0;
3253}
Chris Masond1310b22008-01-24 16:13:08 -05003254
3255int wait_on_extent_buffer_writeback(struct extent_io_tree *tree,
3256 struct extent_buffer *eb)
3257{
3258 return wait_on_extent_writeback(tree, eb->start,
3259 eb->start + eb->len - 1);
3260}
Chris Masond1310b22008-01-24 16:13:08 -05003261
3262int set_extent_buffer_dirty(struct extent_io_tree *tree,
3263 struct extent_buffer *eb)
3264{
3265 unsigned long i;
3266 unsigned long num_pages;
Chris Masonb9473432009-03-13 11:00:37 -04003267 int was_dirty = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003268
Chris Masonb9473432009-03-13 11:00:37 -04003269 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003270 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb9473432009-03-13 11:00:37 -04003271 for (i = 0; i < num_pages; i++)
Chris Masond1310b22008-01-24 16:13:08 -05003272 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Masonb9473432009-03-13 11:00:37 -04003273 return was_dirty;
Chris Masond1310b22008-01-24 16:13:08 -05003274}
Chris Masond1310b22008-01-24 16:13:08 -05003275
Chris Mason1259ab72008-05-12 13:39:03 -04003276int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003277 struct extent_buffer *eb,
3278 struct extent_state **cached_state)
Chris Mason1259ab72008-05-12 13:39:03 -04003279{
3280 unsigned long i;
3281 struct page *page;
3282 unsigned long num_pages;
3283
3284 num_pages = num_extent_pages(eb->start, eb->len);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003285 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Mason1259ab72008-05-12 13:39:03 -04003286
3287 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003288 cached_state, GFP_NOFS);
Chris Mason1259ab72008-05-12 13:39:03 -04003289 for (i = 0; i < num_pages; i++) {
3290 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04003291 if (page)
3292 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04003293 }
3294 return 0;
3295}
3296
Chris Masond1310b22008-01-24 16:13:08 -05003297int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3298 struct extent_buffer *eb)
3299{
3300 unsigned long i;
3301 struct page *page;
3302 unsigned long num_pages;
3303
3304 num_pages = num_extent_pages(eb->start, eb->len);
3305
3306 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3307 GFP_NOFS);
3308 for (i = 0; i < num_pages; i++) {
3309 page = extent_buffer_page(eb, i);
3310 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3311 ((i == num_pages - 1) &&
3312 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3313 check_page_uptodate(tree, page);
3314 continue;
3315 }
3316 SetPageUptodate(page);
3317 }
3318 return 0;
3319}
Chris Masond1310b22008-01-24 16:13:08 -05003320
Chris Masonce9adaa2008-04-09 16:28:12 -04003321int extent_range_uptodate(struct extent_io_tree *tree,
3322 u64 start, u64 end)
3323{
3324 struct page *page;
3325 int ret;
3326 int pg_uptodate = 1;
3327 int uptodate;
3328 unsigned long index;
3329
Chris Mason9655d292009-09-02 15:22:30 -04003330 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL);
Chris Masonce9adaa2008-04-09 16:28:12 -04003331 if (ret)
3332 return 1;
Chris Masond3977122009-01-05 21:25:51 -05003333 while (start <= end) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003334 index = start >> PAGE_CACHE_SHIFT;
3335 page = find_get_page(tree->mapping, index);
3336 uptodate = PageUptodate(page);
3337 page_cache_release(page);
3338 if (!uptodate) {
3339 pg_uptodate = 0;
3340 break;
3341 }
3342 start += PAGE_CACHE_SIZE;
3343 }
3344 return pg_uptodate;
3345}
3346
Chris Masond1310b22008-01-24 16:13:08 -05003347int extent_buffer_uptodate(struct extent_io_tree *tree,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003348 struct extent_buffer *eb,
3349 struct extent_state *cached_state)
Chris Masond1310b22008-01-24 16:13:08 -05003350{
Chris Mason728131d2008-04-09 16:28:12 -04003351 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003352 unsigned long num_pages;
3353 unsigned long i;
Chris Mason728131d2008-04-09 16:28:12 -04003354 struct page *page;
3355 int pg_uptodate = 1;
3356
Chris Masonb4ce94d2009-02-04 09:25:08 -05003357 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Mason42352982008-04-28 16:40:52 -04003358 return 1;
Chris Mason728131d2008-04-09 16:28:12 -04003359
Chris Mason42352982008-04-28 16:40:52 -04003360 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Josef Bacik2ac55d42010-02-03 19:33:23 +00003361 EXTENT_UPTODATE, 1, cached_state);
Chris Mason42352982008-04-28 16:40:52 -04003362 if (ret)
3363 return ret;
Chris Mason728131d2008-04-09 16:28:12 -04003364
3365 num_pages = num_extent_pages(eb->start, eb->len);
3366 for (i = 0; i < num_pages; i++) {
3367 page = extent_buffer_page(eb, i);
3368 if (!PageUptodate(page)) {
3369 pg_uptodate = 0;
3370 break;
3371 }
3372 }
Chris Mason42352982008-04-28 16:40:52 -04003373 return pg_uptodate;
Chris Masond1310b22008-01-24 16:13:08 -05003374}
Chris Masond1310b22008-01-24 16:13:08 -05003375
3376int read_extent_buffer_pages(struct extent_io_tree *tree,
3377 struct extent_buffer *eb,
Chris Masona86c12c2008-02-07 10:50:54 -05003378 u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04003379 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003380{
3381 unsigned long i;
3382 unsigned long start_i;
3383 struct page *page;
3384 int err;
3385 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003386 int locked_pages = 0;
3387 int all_uptodate = 1;
3388 int inc_all_pages = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003389 unsigned long num_pages;
Chris Masona86c12c2008-02-07 10:50:54 -05003390 struct bio *bio = NULL;
Chris Masonc8b97812008-10-29 14:49:59 -04003391 unsigned long bio_flags = 0;
Chris Masona86c12c2008-02-07 10:50:54 -05003392
Chris Masonb4ce94d2009-02-04 09:25:08 -05003393 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
Chris Masond1310b22008-01-24 16:13:08 -05003394 return 0;
3395
Chris Masonce9adaa2008-04-09 16:28:12 -04003396 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Mason9655d292009-09-02 15:22:30 -04003397 EXTENT_UPTODATE, 1, NULL)) {
Chris Masond1310b22008-01-24 16:13:08 -05003398 return 0;
3399 }
3400
3401 if (start) {
3402 WARN_ON(start < eb->start);
3403 start_i = (start >> PAGE_CACHE_SHIFT) -
3404 (eb->start >> PAGE_CACHE_SHIFT);
3405 } else {
3406 start_i = 0;
3407 }
3408
3409 num_pages = num_extent_pages(eb->start, eb->len);
3410 for (i = start_i; i < num_pages; i++) {
3411 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003412 if (!wait) {
David Woodhouse2db04962008-08-07 11:19:43 -04003413 if (!trylock_page(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003414 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05003415 } else {
3416 lock_page(page);
3417 }
Chris Masonce9adaa2008-04-09 16:28:12 -04003418 locked_pages++;
Chris Masond3977122009-01-05 21:25:51 -05003419 if (!PageUptodate(page))
Chris Masonce9adaa2008-04-09 16:28:12 -04003420 all_uptodate = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003421 }
3422 if (all_uptodate) {
3423 if (start_i == 0)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003424 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masonce9adaa2008-04-09 16:28:12 -04003425 goto unlock_exit;
3426 }
3427
3428 for (i = start_i; i < num_pages; i++) {
3429 page = extent_buffer_page(eb, i);
3430 if (inc_all_pages)
3431 page_cache_get(page);
3432 if (!PageUptodate(page)) {
3433 if (start_i == 0)
3434 inc_all_pages = 1;
Chris Masonf1885912008-04-09 16:28:12 -04003435 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05003436 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04003437 get_extent, &bio,
Chris Masonc8b97812008-10-29 14:49:59 -04003438 mirror_num, &bio_flags);
Chris Masond3977122009-01-05 21:25:51 -05003439 if (err)
Chris Masond1310b22008-01-24 16:13:08 -05003440 ret = err;
Chris Masond1310b22008-01-24 16:13:08 -05003441 } else {
3442 unlock_page(page);
3443 }
3444 }
3445
Chris Masona86c12c2008-02-07 10:50:54 -05003446 if (bio)
Chris Masonc8b97812008-10-29 14:49:59 -04003447 submit_one_bio(READ, bio, mirror_num, bio_flags);
Chris Masona86c12c2008-02-07 10:50:54 -05003448
Chris Masond3977122009-01-05 21:25:51 -05003449 if (ret || !wait)
Chris Masond1310b22008-01-24 16:13:08 -05003450 return ret;
Chris Masond3977122009-01-05 21:25:51 -05003451
Chris Masond1310b22008-01-24 16:13:08 -05003452 for (i = start_i; i < num_pages; i++) {
3453 page = extent_buffer_page(eb, i);
3454 wait_on_page_locked(page);
Chris Masond3977122009-01-05 21:25:51 -05003455 if (!PageUptodate(page))
Chris Masond1310b22008-01-24 16:13:08 -05003456 ret = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05003457 }
Chris Masond3977122009-01-05 21:25:51 -05003458
Chris Masond1310b22008-01-24 16:13:08 -05003459 if (!ret)
Chris Masonb4ce94d2009-02-04 09:25:08 -05003460 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
Chris Masond1310b22008-01-24 16:13:08 -05003461 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04003462
3463unlock_exit:
3464 i = start_i;
Chris Masond3977122009-01-05 21:25:51 -05003465 while (locked_pages > 0) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003466 page = extent_buffer_page(eb, i);
3467 i++;
3468 unlock_page(page);
3469 locked_pages--;
3470 }
3471 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003472}
Chris Masond1310b22008-01-24 16:13:08 -05003473
3474void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3475 unsigned long start,
3476 unsigned long len)
3477{
3478 size_t cur;
3479 size_t offset;
3480 struct page *page;
3481 char *kaddr;
3482 char *dst = (char *)dstv;
3483 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3484 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003485
3486 WARN_ON(start > eb->len);
3487 WARN_ON(start + len > eb->start + eb->len);
3488
3489 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3490
Chris Masond3977122009-01-05 21:25:51 -05003491 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003492 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003493
3494 cur = min(len, (PAGE_CACHE_SIZE - offset));
3495 kaddr = kmap_atomic(page, KM_USER1);
3496 memcpy(dst, kaddr + offset, cur);
3497 kunmap_atomic(kaddr, KM_USER1);
3498
3499 dst += cur;
3500 len -= cur;
3501 offset = 0;
3502 i++;
3503 }
3504}
Chris Masond1310b22008-01-24 16:13:08 -05003505
3506int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3507 unsigned long min_len, char **token, char **map,
3508 unsigned long *map_start,
3509 unsigned long *map_len, int km)
3510{
3511 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3512 char *kaddr;
3513 struct page *p;
3514 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3515 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3516 unsigned long end_i = (start_offset + start + min_len - 1) >>
3517 PAGE_CACHE_SHIFT;
3518
3519 if (i != end_i)
3520 return -EINVAL;
3521
3522 if (i == 0) {
3523 offset = start_offset;
3524 *map_start = 0;
3525 } else {
3526 offset = 0;
3527 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3528 }
Chris Masond3977122009-01-05 21:25:51 -05003529
Chris Masond1310b22008-01-24 16:13:08 -05003530 if (start + min_len > eb->len) {
Chris Masond3977122009-01-05 21:25:51 -05003531 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
3532 "wanted %lu %lu\n", (unsigned long long)eb->start,
3533 eb->len, start, min_len);
Chris Masond1310b22008-01-24 16:13:08 -05003534 WARN_ON(1);
3535 }
3536
3537 p = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003538 kaddr = kmap_atomic(p, km);
3539 *token = kaddr;
3540 *map = kaddr + offset;
3541 *map_len = PAGE_CACHE_SIZE - offset;
3542 return 0;
3543}
Chris Masond1310b22008-01-24 16:13:08 -05003544
3545int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3546 unsigned long min_len,
3547 char **token, char **map,
3548 unsigned long *map_start,
3549 unsigned long *map_len, int km)
3550{
3551 int err;
3552 int save = 0;
3553 if (eb->map_token) {
3554 unmap_extent_buffer(eb, eb->map_token, km);
3555 eb->map_token = NULL;
3556 save = 1;
3557 }
3558 err = map_private_extent_buffer(eb, start, min_len, token, map,
3559 map_start, map_len, km);
3560 if (!err && save) {
3561 eb->map_token = *token;
3562 eb->kaddr = *map;
3563 eb->map_start = *map_start;
3564 eb->map_len = *map_len;
3565 }
3566 return err;
3567}
Chris Masond1310b22008-01-24 16:13:08 -05003568
3569void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3570{
3571 kunmap_atomic(token, km);
3572}
Chris Masond1310b22008-01-24 16:13:08 -05003573
3574int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3575 unsigned long start,
3576 unsigned long len)
3577{
3578 size_t cur;
3579 size_t offset;
3580 struct page *page;
3581 char *kaddr;
3582 char *ptr = (char *)ptrv;
3583 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3584 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3585 int ret = 0;
3586
3587 WARN_ON(start > eb->len);
3588 WARN_ON(start + len > eb->start + eb->len);
3589
3590 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3591
Chris Masond3977122009-01-05 21:25:51 -05003592 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003593 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003594
3595 cur = min(len, (PAGE_CACHE_SIZE - offset));
3596
3597 kaddr = kmap_atomic(page, KM_USER0);
3598 ret = memcmp(ptr, kaddr + offset, cur);
3599 kunmap_atomic(kaddr, KM_USER0);
3600 if (ret)
3601 break;
3602
3603 ptr += cur;
3604 len -= cur;
3605 offset = 0;
3606 i++;
3607 }
3608 return ret;
3609}
Chris Masond1310b22008-01-24 16:13:08 -05003610
3611void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3612 unsigned long start, unsigned long len)
3613{
3614 size_t cur;
3615 size_t offset;
3616 struct page *page;
3617 char *kaddr;
3618 char *src = (char *)srcv;
3619 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3620 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3621
3622 WARN_ON(start > eb->len);
3623 WARN_ON(start + len > eb->start + eb->len);
3624
3625 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3626
Chris Masond3977122009-01-05 21:25:51 -05003627 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003628 page = extent_buffer_page(eb, i);
3629 WARN_ON(!PageUptodate(page));
3630
3631 cur = min(len, PAGE_CACHE_SIZE - offset);
3632 kaddr = kmap_atomic(page, KM_USER1);
3633 memcpy(kaddr + offset, src, cur);
3634 kunmap_atomic(kaddr, KM_USER1);
3635
3636 src += cur;
3637 len -= cur;
3638 offset = 0;
3639 i++;
3640 }
3641}
Chris Masond1310b22008-01-24 16:13:08 -05003642
3643void memset_extent_buffer(struct extent_buffer *eb, char c,
3644 unsigned long start, unsigned long len)
3645{
3646 size_t cur;
3647 size_t offset;
3648 struct page *page;
3649 char *kaddr;
3650 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3651 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3652
3653 WARN_ON(start > eb->len);
3654 WARN_ON(start + len > eb->start + eb->len);
3655
3656 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3657
Chris Masond3977122009-01-05 21:25:51 -05003658 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003659 page = extent_buffer_page(eb, i);
3660 WARN_ON(!PageUptodate(page));
3661
3662 cur = min(len, PAGE_CACHE_SIZE - offset);
3663 kaddr = kmap_atomic(page, KM_USER0);
3664 memset(kaddr + offset, c, cur);
3665 kunmap_atomic(kaddr, KM_USER0);
3666
3667 len -= cur;
3668 offset = 0;
3669 i++;
3670 }
3671}
Chris Masond1310b22008-01-24 16:13:08 -05003672
3673void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3674 unsigned long dst_offset, unsigned long src_offset,
3675 unsigned long len)
3676{
3677 u64 dst_len = dst->len;
3678 size_t cur;
3679 size_t offset;
3680 struct page *page;
3681 char *kaddr;
3682 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3683 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3684
3685 WARN_ON(src->len != dst_len);
3686
3687 offset = (start_offset + dst_offset) &
3688 ((unsigned long)PAGE_CACHE_SIZE - 1);
3689
Chris Masond3977122009-01-05 21:25:51 -05003690 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003691 page = extent_buffer_page(dst, i);
3692 WARN_ON(!PageUptodate(page));
3693
3694 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3695
3696 kaddr = kmap_atomic(page, KM_USER0);
3697 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3698 kunmap_atomic(kaddr, KM_USER0);
3699
3700 src_offset += cur;
3701 len -= cur;
3702 offset = 0;
3703 i++;
3704 }
3705}
Chris Masond1310b22008-01-24 16:13:08 -05003706
3707static void move_pages(struct page *dst_page, struct page *src_page,
3708 unsigned long dst_off, unsigned long src_off,
3709 unsigned long len)
3710{
3711 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3712 if (dst_page == src_page) {
3713 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3714 } else {
3715 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3716 char *p = dst_kaddr + dst_off + len;
3717 char *s = src_kaddr + src_off + len;
3718
3719 while (len--)
3720 *--p = *--s;
3721
3722 kunmap_atomic(src_kaddr, KM_USER1);
3723 }
3724 kunmap_atomic(dst_kaddr, KM_USER0);
3725}
3726
3727static void copy_pages(struct page *dst_page, struct page *src_page,
3728 unsigned long dst_off, unsigned long src_off,
3729 unsigned long len)
3730{
3731 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3732 char *src_kaddr;
3733
3734 if (dst_page != src_page)
3735 src_kaddr = kmap_atomic(src_page, KM_USER1);
3736 else
3737 src_kaddr = dst_kaddr;
3738
3739 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3740 kunmap_atomic(dst_kaddr, KM_USER0);
3741 if (dst_page != src_page)
3742 kunmap_atomic(src_kaddr, KM_USER1);
3743}
3744
3745void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3746 unsigned long src_offset, unsigned long len)
3747{
3748 size_t cur;
3749 size_t dst_off_in_page;
3750 size_t src_off_in_page;
3751 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3752 unsigned long dst_i;
3753 unsigned long src_i;
3754
3755 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003756 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3757 "len %lu dst len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003758 BUG_ON(1);
3759 }
3760 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003761 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3762 "len %lu dst len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003763 BUG_ON(1);
3764 }
3765
Chris Masond3977122009-01-05 21:25:51 -05003766 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003767 dst_off_in_page = (start_offset + dst_offset) &
3768 ((unsigned long)PAGE_CACHE_SIZE - 1);
3769 src_off_in_page = (start_offset + src_offset) &
3770 ((unsigned long)PAGE_CACHE_SIZE - 1);
3771
3772 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3773 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3774
3775 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3776 src_off_in_page));
3777 cur = min_t(unsigned long, cur,
3778 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3779
3780 copy_pages(extent_buffer_page(dst, dst_i),
3781 extent_buffer_page(dst, src_i),
3782 dst_off_in_page, src_off_in_page, cur);
3783
3784 src_offset += cur;
3785 dst_offset += cur;
3786 len -= cur;
3787 }
3788}
Chris Masond1310b22008-01-24 16:13:08 -05003789
3790void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3791 unsigned long src_offset, unsigned long len)
3792{
3793 size_t cur;
3794 size_t dst_off_in_page;
3795 size_t src_off_in_page;
3796 unsigned long dst_end = dst_offset + len - 1;
3797 unsigned long src_end = src_offset + len - 1;
3798 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3799 unsigned long dst_i;
3800 unsigned long src_i;
3801
3802 if (src_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003803 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3804 "len %lu len %lu\n", src_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003805 BUG_ON(1);
3806 }
3807 if (dst_offset + len > dst->len) {
Chris Masond3977122009-01-05 21:25:51 -05003808 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3809 "len %lu len %lu\n", dst_offset, len, dst->len);
Chris Masond1310b22008-01-24 16:13:08 -05003810 BUG_ON(1);
3811 }
3812 if (dst_offset < src_offset) {
3813 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
3814 return;
3815 }
Chris Masond3977122009-01-05 21:25:51 -05003816 while (len > 0) {
Chris Masond1310b22008-01-24 16:13:08 -05003817 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
3818 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
3819
3820 dst_off_in_page = (start_offset + dst_end) &
3821 ((unsigned long)PAGE_CACHE_SIZE - 1);
3822 src_off_in_page = (start_offset + src_end) &
3823 ((unsigned long)PAGE_CACHE_SIZE - 1);
3824
3825 cur = min_t(unsigned long, len, src_off_in_page + 1);
3826 cur = min(cur, dst_off_in_page + 1);
3827 move_pages(extent_buffer_page(dst, dst_i),
3828 extent_buffer_page(dst, src_i),
3829 dst_off_in_page - cur + 1,
3830 src_off_in_page - cur + 1, cur);
3831
3832 dst_end -= cur;
3833 src_end -= cur;
3834 len -= cur;
3835 }
3836}
Chris Mason6af118ce2008-07-22 11:18:07 -04003837
3838int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
3839{
3840 u64 start = page_offset(page);
3841 struct extent_buffer *eb;
3842 int ret = 1;
3843 unsigned long i;
3844 unsigned long num_pages;
3845
3846 spin_lock(&tree->buffer_lock);
3847 eb = buffer_search(tree, start);
3848 if (!eb)
3849 goto out;
3850
3851 if (atomic_read(&eb->refs) > 1) {
3852 ret = 0;
3853 goto out;
3854 }
Chris Masonb9473432009-03-13 11:00:37 -04003855 if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3856 ret = 0;
3857 goto out;
3858 }
Chris Mason6af118ce2008-07-22 11:18:07 -04003859 /* at this point we can safely release the extent buffer */
3860 num_pages = num_extent_pages(eb->start, eb->len);
Christoph Hellwigb2141072008-09-05 16:43:31 -04003861 for (i = 0; i < num_pages; i++)
3862 page_cache_release(extent_buffer_page(eb, i));
Chris Mason6af118ce2008-07-22 11:18:07 -04003863 rb_erase(&eb->rb_node, &tree->buffer);
3864 __free_extent_buffer(eb);
3865out:
3866 spin_unlock(&tree->buffer_lock);
3867 return ret;
3868}