blob: 1cf4baba4342a2e5419424da9f0f0fc7cd699e62 [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>
5#include <linux/gfp.h>
6#include <linux/pagemap.h>
7#include <linux/page-flags.h>
8#include <linux/module.h>
9#include <linux/spinlock.h>
10#include <linux/blkdev.h>
11#include <linux/swap.h>
12#include <linux/version.h>
13#include <linux/writeback.h>
14#include <linux/pagevec.h>
15#include "extent_io.h"
16#include "extent_map.h"
17
18/* temporary define until extent_map moves out of btrfs */
19struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
20 unsigned long extra_flags,
21 void (*ctor)(void *, struct kmem_cache *,
22 unsigned long));
23
24static struct kmem_cache *extent_state_cache;
25static struct kmem_cache *extent_buffer_cache;
26
27static LIST_HEAD(buffers);
28static LIST_HEAD(states);
Chris Mason2d2ae542008-03-26 16:24:23 -040029static spinlock_t leak_lock = SPIN_LOCK_UNLOCKED;
Chris Masond1310b22008-01-24 16:13:08 -050030
Chris Masond1310b22008-01-24 16:13:08 -050031#define BUFFER_LRU_MAX 64
32
33struct tree_entry {
34 u64 start;
35 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -050036 struct rb_node rb_node;
37};
38
39struct extent_page_data {
40 struct bio *bio;
41 struct extent_io_tree *tree;
42 get_extent_t *get_extent;
43};
44
45int __init extent_io_init(void)
46{
47 extent_state_cache = btrfs_cache_create("extent_state",
48 sizeof(struct extent_state), 0,
49 NULL);
50 if (!extent_state_cache)
51 return -ENOMEM;
52
53 extent_buffer_cache = btrfs_cache_create("extent_buffers",
54 sizeof(struct extent_buffer), 0,
55 NULL);
56 if (!extent_buffer_cache)
57 goto free_state_cache;
58 return 0;
59
60free_state_cache:
61 kmem_cache_destroy(extent_state_cache);
62 return -ENOMEM;
63}
64
65void extent_io_exit(void)
66{
67 struct extent_state *state;
Chris Mason2d2ae542008-03-26 16:24:23 -040068 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -050069
70 while (!list_empty(&states)) {
Chris Mason2d2ae542008-03-26 16:24:23 -040071 state = list_entry(states.next, struct extent_state, leak_list);
Chris Mason70dec802008-01-29 09:59:12 -050072 printk("state leak: start %Lu end %Lu state %lu in tree %p refs %d\n", state->start, state->end, state->state, state->tree, atomic_read(&state->refs));
Chris Mason2d2ae542008-03-26 16:24:23 -040073 list_del(&state->leak_list);
Chris Masond1310b22008-01-24 16:13:08 -050074 kmem_cache_free(extent_state_cache, state);
75
76 }
77
Chris Mason2d2ae542008-03-26 16:24:23 -040078 while (!list_empty(&buffers)) {
79 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
80 printk("buffer leak start %Lu len %lu refs %d\n", eb->start, eb->len, atomic_read(&eb->refs));
81 list_del(&eb->leak_list);
82 kmem_cache_free(extent_buffer_cache, eb);
83 }
Chris Masond1310b22008-01-24 16:13:08 -050084 if (extent_state_cache)
85 kmem_cache_destroy(extent_state_cache);
86 if (extent_buffer_cache)
87 kmem_cache_destroy(extent_buffer_cache);
88}
89
90void extent_io_tree_init(struct extent_io_tree *tree,
91 struct address_space *mapping, gfp_t mask)
92{
93 tree->state.rb_node = NULL;
Chris Mason6af118ce2008-07-22 11:18:07 -040094 tree->buffer.rb_node = NULL;
Chris Masond1310b22008-01-24 16:13:08 -050095 tree->ops = NULL;
96 tree->dirty_bytes = 0;
Chris Mason70dec802008-01-29 09:59:12 -050097 spin_lock_init(&tree->lock);
Chris Mason6af118ce2008-07-22 11:18:07 -040098 spin_lock_init(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -050099 tree->mapping = mapping;
Chris Masond1310b22008-01-24 16:13:08 -0500100}
101EXPORT_SYMBOL(extent_io_tree_init);
102
Chris Masond1310b22008-01-24 16:13:08 -0500103struct extent_state *alloc_extent_state(gfp_t mask)
104{
105 struct extent_state *state;
Chris Mason2d2ae542008-03-26 16:24:23 -0400106 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500107
108 state = kmem_cache_alloc(extent_state_cache, mask);
Peter2b114d12008-04-01 11:21:40 -0400109 if (!state)
Chris Masond1310b22008-01-24 16:13:08 -0500110 return state;
111 state->state = 0;
Chris Masond1310b22008-01-24 16:13:08 -0500112 state->private = 0;
Chris Mason70dec802008-01-29 09:59:12 -0500113 state->tree = NULL;
Chris Mason2d2ae542008-03-26 16:24:23 -0400114 spin_lock_irqsave(&leak_lock, flags);
115 list_add(&state->leak_list, &states);
116 spin_unlock_irqrestore(&leak_lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -0500117
118 atomic_set(&state->refs, 1);
119 init_waitqueue_head(&state->wq);
120 return state;
121}
122EXPORT_SYMBOL(alloc_extent_state);
123
124void free_extent_state(struct extent_state *state)
125{
Chris Masond1310b22008-01-24 16:13:08 -0500126 if (!state)
127 return;
128 if (atomic_dec_and_test(&state->refs)) {
Chris Mason2d2ae542008-03-26 16:24:23 -0400129 unsigned long flags;
Chris Mason70dec802008-01-29 09:59:12 -0500130 WARN_ON(state->tree);
Chris Mason2d2ae542008-03-26 16:24:23 -0400131 spin_lock_irqsave(&leak_lock, flags);
132 list_del(&state->leak_list);
133 spin_unlock_irqrestore(&leak_lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -0500134 kmem_cache_free(extent_state_cache, state);
135 }
136}
137EXPORT_SYMBOL(free_extent_state);
138
139static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
140 struct rb_node *node)
141{
142 struct rb_node ** p = &root->rb_node;
143 struct rb_node * parent = NULL;
144 struct tree_entry *entry;
145
146 while(*p) {
147 parent = *p;
148 entry = rb_entry(parent, struct tree_entry, rb_node);
149
150 if (offset < entry->start)
151 p = &(*p)->rb_left;
152 else if (offset > entry->end)
153 p = &(*p)->rb_right;
154 else
155 return parent;
156 }
157
158 entry = rb_entry(node, struct tree_entry, rb_node);
Chris Masond1310b22008-01-24 16:13:08 -0500159 rb_link_node(node, parent, p);
160 rb_insert_color(node, root);
161 return NULL;
162}
163
Chris Mason80ea96b2008-02-01 14:51:59 -0500164static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
Chris Masond1310b22008-01-24 16:13:08 -0500165 struct rb_node **prev_ret,
166 struct rb_node **next_ret)
167{
Chris Mason80ea96b2008-02-01 14:51:59 -0500168 struct rb_root *root = &tree->state;
Chris Masond1310b22008-01-24 16:13:08 -0500169 struct rb_node * n = root->rb_node;
170 struct rb_node *prev = NULL;
171 struct rb_node *orig_prev = NULL;
172 struct tree_entry *entry;
173 struct tree_entry *prev_entry = NULL;
174
175 while(n) {
176 entry = rb_entry(n, struct tree_entry, rb_node);
177 prev = n;
178 prev_entry = entry;
179
180 if (offset < entry->start)
181 n = n->rb_left;
182 else if (offset > entry->end)
183 n = n->rb_right;
Chris Mason80ea96b2008-02-01 14:51:59 -0500184 else {
Chris Masond1310b22008-01-24 16:13:08 -0500185 return n;
Chris Mason80ea96b2008-02-01 14:51:59 -0500186 }
Chris Masond1310b22008-01-24 16:13:08 -0500187 }
188
189 if (prev_ret) {
190 orig_prev = prev;
191 while(prev && offset > prev_entry->end) {
192 prev = rb_next(prev);
193 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
194 }
195 *prev_ret = prev;
196 prev = orig_prev;
197 }
198
199 if (next_ret) {
200 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
201 while(prev && offset < prev_entry->start) {
202 prev = rb_prev(prev);
203 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
204 }
205 *next_ret = prev;
206 }
207 return NULL;
208}
209
Chris Mason80ea96b2008-02-01 14:51:59 -0500210static inline struct rb_node *tree_search(struct extent_io_tree *tree,
211 u64 offset)
Chris Masond1310b22008-01-24 16:13:08 -0500212{
Chris Mason70dec802008-01-29 09:59:12 -0500213 struct rb_node *prev = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500214 struct rb_node *ret;
Chris Mason70dec802008-01-29 09:59:12 -0500215
Chris Mason80ea96b2008-02-01 14:51:59 -0500216 ret = __etree_search(tree, offset, &prev, NULL);
217 if (!ret) {
Chris Masond1310b22008-01-24 16:13:08 -0500218 return prev;
Chris Mason80ea96b2008-02-01 14:51:59 -0500219 }
Chris Masond1310b22008-01-24 16:13:08 -0500220 return ret;
221}
222
Chris Mason6af118ce2008-07-22 11:18:07 -0400223static struct extent_buffer *buffer_tree_insert(struct extent_io_tree *tree,
224 u64 offset, struct rb_node *node)
225{
226 struct rb_root *root = &tree->buffer;
227 struct rb_node ** p = &root->rb_node;
228 struct rb_node * parent = NULL;
229 struct extent_buffer *eb;
230
231 while(*p) {
232 parent = *p;
233 eb = rb_entry(parent, struct extent_buffer, rb_node);
234
235 if (offset < eb->start)
236 p = &(*p)->rb_left;
237 else if (offset > eb->start)
238 p = &(*p)->rb_right;
239 else
240 return eb;
241 }
242
243 rb_link_node(node, parent, p);
244 rb_insert_color(node, root);
245 return NULL;
246}
247
248static struct extent_buffer *buffer_search(struct extent_io_tree *tree,
249 u64 offset)
250{
251 struct rb_root *root = &tree->buffer;
252 struct rb_node * n = root->rb_node;
253 struct extent_buffer *eb;
254
255 while(n) {
256 eb = rb_entry(n, struct extent_buffer, rb_node);
257 if (offset < eb->start)
258 n = n->rb_left;
259 else if (offset > eb->start)
260 n = n->rb_right;
261 else
262 return eb;
263 }
264 return NULL;
265}
266
Chris Masond1310b22008-01-24 16:13:08 -0500267/*
268 * utility function to look for merge candidates inside a given range.
269 * Any extents with matching state are merged together into a single
270 * extent in the tree. Extents with EXTENT_IO in their state field
271 * are not merged because the end_io handlers need to be able to do
272 * operations on them without sleeping (or doing allocations/splits).
273 *
274 * This should be called with the tree lock held.
275 */
276static int merge_state(struct extent_io_tree *tree,
277 struct extent_state *state)
278{
279 struct extent_state *other;
280 struct rb_node *other_node;
281
282 if (state->state & EXTENT_IOBITS)
283 return 0;
284
285 other_node = rb_prev(&state->rb_node);
286 if (other_node) {
287 other = rb_entry(other_node, struct extent_state, rb_node);
288 if (other->end == state->start - 1 &&
289 other->state == state->state) {
290 state->start = other->start;
Chris Mason70dec802008-01-29 09:59:12 -0500291 other->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500292 rb_erase(&other->rb_node, &tree->state);
293 free_extent_state(other);
294 }
295 }
296 other_node = rb_next(&state->rb_node);
297 if (other_node) {
298 other = rb_entry(other_node, struct extent_state, rb_node);
299 if (other->start == state->end + 1 &&
300 other->state == state->state) {
301 other->start = state->start;
Chris Mason70dec802008-01-29 09:59:12 -0500302 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500303 rb_erase(&state->rb_node, &tree->state);
304 free_extent_state(state);
305 }
306 }
307 return 0;
308}
309
Chris Mason291d6732008-01-29 15:55:23 -0500310static void set_state_cb(struct extent_io_tree *tree,
311 struct extent_state *state,
312 unsigned long bits)
313{
314 if (tree->ops && tree->ops->set_bit_hook) {
315 tree->ops->set_bit_hook(tree->mapping->host, state->start,
Chris Masonb0c68f82008-01-31 11:05:37 -0500316 state->end, state->state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500317 }
318}
319
320static void clear_state_cb(struct extent_io_tree *tree,
321 struct extent_state *state,
322 unsigned long bits)
323{
324 if (tree->ops && tree->ops->set_bit_hook) {
325 tree->ops->clear_bit_hook(tree->mapping->host, state->start,
Chris Masonb0c68f82008-01-31 11:05:37 -0500326 state->end, state->state, bits);
Chris Mason291d6732008-01-29 15:55:23 -0500327 }
328}
329
Chris Masond1310b22008-01-24 16:13:08 -0500330/*
331 * insert an extent_state struct into the tree. 'bits' are set on the
332 * struct before it is inserted.
333 *
334 * This may return -EEXIST if the extent is already there, in which case the
335 * state struct is freed.
336 *
337 * The tree lock is not taken internally. This is a utility function and
338 * probably isn't what you want to call (see set/clear_extent_bit).
339 */
340static int insert_state(struct extent_io_tree *tree,
341 struct extent_state *state, u64 start, u64 end,
342 int bits)
343{
344 struct rb_node *node;
345
346 if (end < start) {
347 printk("end < start %Lu %Lu\n", end, start);
348 WARN_ON(1);
349 }
350 if (bits & EXTENT_DIRTY)
351 tree->dirty_bytes += end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500352 set_state_cb(tree, state, bits);
Chris Masond1310b22008-01-24 16:13:08 -0500353 state->state |= bits;
354 state->start = start;
355 state->end = end;
356 node = tree_insert(&tree->state, end, &state->rb_node);
357 if (node) {
358 struct extent_state *found;
359 found = rb_entry(node, struct extent_state, rb_node);
360 printk("found node %Lu %Lu on insert of %Lu %Lu\n", found->start, found->end, start, end);
361 free_extent_state(state);
362 return -EEXIST;
363 }
Chris Mason70dec802008-01-29 09:59:12 -0500364 state->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500365 merge_state(tree, state);
366 return 0;
367}
368
369/*
370 * split a given extent state struct in two, inserting the preallocated
371 * struct 'prealloc' as the newly created second half. 'split' indicates an
372 * offset inside 'orig' where it should be split.
373 *
374 * Before calling,
375 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
376 * are two extent state structs in the tree:
377 * prealloc: [orig->start, split - 1]
378 * orig: [ split, orig->end ]
379 *
380 * The tree locks are not taken by this function. They need to be held
381 * by the caller.
382 */
383static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
384 struct extent_state *prealloc, u64 split)
385{
386 struct rb_node *node;
387 prealloc->start = orig->start;
388 prealloc->end = split - 1;
389 prealloc->state = orig->state;
390 orig->start = split;
391
392 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
393 if (node) {
394 struct extent_state *found;
395 found = rb_entry(node, struct extent_state, rb_node);
396 printk("found node %Lu %Lu on insert of %Lu %Lu\n", found->start, found->end, prealloc->start, prealloc->end);
397 free_extent_state(prealloc);
398 return -EEXIST;
399 }
Chris Mason70dec802008-01-29 09:59:12 -0500400 prealloc->tree = tree;
Chris Masond1310b22008-01-24 16:13:08 -0500401 return 0;
402}
403
404/*
405 * utility function to clear some bits in an extent state struct.
406 * it will optionally wake up any one waiting on this state (wake == 1), or
407 * forcibly remove the state from the tree (delete == 1).
408 *
409 * If no bits are set on the state struct after clearing things, the
410 * struct is freed and removed from the tree
411 */
412static int clear_state_bit(struct extent_io_tree *tree,
413 struct extent_state *state, int bits, int wake,
414 int delete)
415{
416 int ret = state->state & bits;
417
418 if ((bits & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
419 u64 range = state->end - state->start + 1;
420 WARN_ON(range > tree->dirty_bytes);
421 tree->dirty_bytes -= range;
422 }
Chris Mason291d6732008-01-29 15:55:23 -0500423 clear_state_cb(tree, state, bits);
Chris Masonb0c68f82008-01-31 11:05:37 -0500424 state->state &= ~bits;
Chris Masond1310b22008-01-24 16:13:08 -0500425 if (wake)
426 wake_up(&state->wq);
427 if (delete || state->state == 0) {
Chris Mason70dec802008-01-29 09:59:12 -0500428 if (state->tree) {
Chris Masonae9d1282008-02-01 15:42:15 -0500429 clear_state_cb(tree, state, state->state);
Chris Masond1310b22008-01-24 16:13:08 -0500430 rb_erase(&state->rb_node, &tree->state);
Chris Mason70dec802008-01-29 09:59:12 -0500431 state->tree = NULL;
Chris Masond1310b22008-01-24 16:13:08 -0500432 free_extent_state(state);
433 } else {
434 WARN_ON(1);
435 }
436 } else {
437 merge_state(tree, state);
438 }
439 return ret;
440}
441
442/*
443 * clear some bits on a range in the tree. This may require splitting
444 * or inserting elements in the tree, so the gfp mask is used to
445 * indicate which allocations or sleeping are allowed.
446 *
447 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
448 * the given range from the tree regardless of state (ie for truncate).
449 *
450 * the range [start, end] is inclusive.
451 *
452 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
453 * bits were already set, or zero if none of the bits were already set.
454 */
455int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
456 int bits, int wake, int delete, gfp_t mask)
457{
458 struct extent_state *state;
459 struct extent_state *prealloc = NULL;
460 struct rb_node *node;
461 unsigned long flags;
462 int err;
463 int set = 0;
464
465again:
466 if (!prealloc && (mask & __GFP_WAIT)) {
467 prealloc = alloc_extent_state(mask);
468 if (!prealloc)
469 return -ENOMEM;
470 }
471
Chris Mason70dec802008-01-29 09:59:12 -0500472 spin_lock_irqsave(&tree->lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -0500473 /*
474 * this search will find the extents that end after
475 * our range starts
476 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500477 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500478 if (!node)
479 goto out;
480 state = rb_entry(node, struct extent_state, rb_node);
481 if (state->start > end)
482 goto out;
483 WARN_ON(state->end < start);
484
485 /*
486 * | ---- desired range ---- |
487 * | state | or
488 * | ------------- state -------------- |
489 *
490 * We need to split the extent we found, and may flip
491 * bits on second half.
492 *
493 * If the extent we found extends past our range, we
494 * just split and search again. It'll get split again
495 * the next time though.
496 *
497 * If the extent we found is inside our range, we clear
498 * the desired bit on it.
499 */
500
501 if (state->start < start) {
Chris Mason70dec802008-01-29 09:59:12 -0500502 if (!prealloc)
503 prealloc = alloc_extent_state(GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500504 err = split_state(tree, state, prealloc, start);
505 BUG_ON(err == -EEXIST);
506 prealloc = NULL;
507 if (err)
508 goto out;
509 if (state->end <= end) {
510 start = state->end + 1;
511 set |= clear_state_bit(tree, state, bits,
512 wake, delete);
513 } else {
514 start = state->start;
515 }
516 goto search_again;
517 }
518 /*
519 * | ---- desired range ---- |
520 * | state |
521 * We need to split the extent, and clear the bit
522 * on the first half
523 */
524 if (state->start <= end && state->end > end) {
Chris Mason70dec802008-01-29 09:59:12 -0500525 if (!prealloc)
526 prealloc = alloc_extent_state(GFP_ATOMIC);
Chris Masond1310b22008-01-24 16:13:08 -0500527 err = split_state(tree, state, prealloc, end + 1);
528 BUG_ON(err == -EEXIST);
529
530 if (wake)
531 wake_up(&state->wq);
532 set |= clear_state_bit(tree, prealloc, bits,
533 wake, delete);
534 prealloc = NULL;
535 goto out;
536 }
537
538 start = state->end + 1;
539 set |= clear_state_bit(tree, state, bits, wake, delete);
540 goto search_again;
541
542out:
Chris Mason70dec802008-01-29 09:59:12 -0500543 spin_unlock_irqrestore(&tree->lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -0500544 if (prealloc)
545 free_extent_state(prealloc);
546
547 return set;
548
549search_again:
550 if (start > end)
551 goto out;
Chris Mason70dec802008-01-29 09:59:12 -0500552 spin_unlock_irqrestore(&tree->lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -0500553 if (mask & __GFP_WAIT)
554 cond_resched();
555 goto again;
556}
557EXPORT_SYMBOL(clear_extent_bit);
558
559static int wait_on_state(struct extent_io_tree *tree,
560 struct extent_state *state)
561{
562 DEFINE_WAIT(wait);
563 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
Chris Mason70dec802008-01-29 09:59:12 -0500564 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500565 schedule();
Chris Mason70dec802008-01-29 09:59:12 -0500566 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500567 finish_wait(&state->wq, &wait);
568 return 0;
569}
570
571/*
572 * waits for one or more bits to clear on a range in the state tree.
573 * The range [start, end] is inclusive.
574 * The tree lock is taken by this function
575 */
576int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
577{
578 struct extent_state *state;
579 struct rb_node *node;
580
Chris Mason70dec802008-01-29 09:59:12 -0500581 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500582again:
583 while (1) {
584 /*
585 * this search will find all the extents that end after
586 * our range starts
587 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500588 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500589 if (!node)
590 break;
591
592 state = rb_entry(node, struct extent_state, rb_node);
593
594 if (state->start > end)
595 goto out;
596
597 if (state->state & bits) {
598 start = state->start;
599 atomic_inc(&state->refs);
600 wait_on_state(tree, state);
601 free_extent_state(state);
602 goto again;
603 }
604 start = state->end + 1;
605
606 if (start > end)
607 break;
608
609 if (need_resched()) {
Chris Mason70dec802008-01-29 09:59:12 -0500610 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500611 cond_resched();
Chris Mason70dec802008-01-29 09:59:12 -0500612 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500613 }
614 }
615out:
Chris Mason70dec802008-01-29 09:59:12 -0500616 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500617 return 0;
618}
619EXPORT_SYMBOL(wait_extent_bit);
620
621static void set_state_bits(struct extent_io_tree *tree,
622 struct extent_state *state,
623 int bits)
624{
625 if ((bits & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
626 u64 range = state->end - state->start + 1;
627 tree->dirty_bytes += range;
628 }
Chris Mason291d6732008-01-29 15:55:23 -0500629 set_state_cb(tree, state, bits);
Chris Masonb0c68f82008-01-31 11:05:37 -0500630 state->state |= bits;
Chris Masond1310b22008-01-24 16:13:08 -0500631}
632
633/*
634 * set some bits on a range in the tree. This may require allocations
635 * or sleeping, so the gfp mask is used to indicate what is allowed.
636 *
637 * If 'exclusive' == 1, this will fail with -EEXIST if some part of the
638 * range already has the desired bits set. The start of the existing
639 * range is returned in failed_start in this case.
640 *
641 * [start, end] is inclusive
642 * This takes the tree lock.
643 */
644int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits,
645 int exclusive, u64 *failed_start, gfp_t mask)
646{
647 struct extent_state *state;
648 struct extent_state *prealloc = NULL;
649 struct rb_node *node;
650 unsigned long flags;
651 int err = 0;
652 int set;
653 u64 last_start;
654 u64 last_end;
655again:
656 if (!prealloc && (mask & __GFP_WAIT)) {
657 prealloc = alloc_extent_state(mask);
658 if (!prealloc)
659 return -ENOMEM;
660 }
661
Chris Mason70dec802008-01-29 09:59:12 -0500662 spin_lock_irqsave(&tree->lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -0500663 /*
664 * this search will find all the extents that end after
665 * our range starts.
666 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500667 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -0500668 if (!node) {
669 err = insert_state(tree, prealloc, start, end, bits);
670 prealloc = NULL;
671 BUG_ON(err == -EEXIST);
672 goto out;
673 }
674
675 state = rb_entry(node, struct extent_state, rb_node);
676 last_start = state->start;
677 last_end = state->end;
678
679 /*
680 * | ---- desired range ---- |
681 * | state |
682 *
683 * Just lock what we found and keep going
684 */
685 if (state->start == start && state->end <= end) {
686 set = state->state & bits;
687 if (set && exclusive) {
688 *failed_start = state->start;
689 err = -EEXIST;
690 goto out;
691 }
692 set_state_bits(tree, state, bits);
693 start = state->end + 1;
694 merge_state(tree, state);
695 goto search_again;
696 }
697
698 /*
699 * | ---- desired range ---- |
700 * | state |
701 * or
702 * | ------------- state -------------- |
703 *
704 * We need to split the extent we found, and may flip bits on
705 * second half.
706 *
707 * If the extent we found extends past our
708 * range, we just split and search again. It'll get split
709 * again the next time though.
710 *
711 * If the extent we found is inside our range, we set the
712 * desired bit on it.
713 */
714 if (state->start < start) {
715 set = state->state & bits;
716 if (exclusive && set) {
717 *failed_start = start;
718 err = -EEXIST;
719 goto out;
720 }
721 err = split_state(tree, state, prealloc, start);
722 BUG_ON(err == -EEXIST);
723 prealloc = NULL;
724 if (err)
725 goto out;
726 if (state->end <= end) {
727 set_state_bits(tree, state, bits);
728 start = state->end + 1;
729 merge_state(tree, state);
730 } else {
731 start = state->start;
732 }
733 goto search_again;
734 }
735 /*
736 * | ---- desired range ---- |
737 * | state | or | state |
738 *
739 * There's a hole, we need to insert something in it and
740 * ignore the extent we found.
741 */
742 if (state->start > start) {
743 u64 this_end;
744 if (end < last_start)
745 this_end = end;
746 else
747 this_end = last_start -1;
748 err = insert_state(tree, prealloc, start, this_end,
749 bits);
750 prealloc = NULL;
751 BUG_ON(err == -EEXIST);
752 if (err)
753 goto out;
754 start = this_end + 1;
755 goto search_again;
756 }
757 /*
758 * | ---- desired range ---- |
759 * | state |
760 * We need to split the extent, and set the bit
761 * on the first half
762 */
763 if (state->start <= end && state->end > end) {
764 set = state->state & bits;
765 if (exclusive && set) {
766 *failed_start = start;
767 err = -EEXIST;
768 goto out;
769 }
770 err = split_state(tree, state, prealloc, end + 1);
771 BUG_ON(err == -EEXIST);
772
773 set_state_bits(tree, prealloc, bits);
774 merge_state(tree, prealloc);
775 prealloc = NULL;
776 goto out;
777 }
778
779 goto search_again;
780
781out:
Chris Mason70dec802008-01-29 09:59:12 -0500782 spin_unlock_irqrestore(&tree->lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -0500783 if (prealloc)
784 free_extent_state(prealloc);
785
786 return err;
787
788search_again:
789 if (start > end)
790 goto out;
Chris Mason70dec802008-01-29 09:59:12 -0500791 spin_unlock_irqrestore(&tree->lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -0500792 if (mask & __GFP_WAIT)
793 cond_resched();
794 goto again;
795}
796EXPORT_SYMBOL(set_extent_bit);
797
798/* wrappers around set/clear extent bit */
799int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
800 gfp_t mask)
801{
802 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
803 mask);
804}
805EXPORT_SYMBOL(set_extent_dirty);
806
Chris Masone6dcd2d2008-07-17 12:53:50 -0400807int set_extent_ordered(struct extent_io_tree *tree, u64 start, u64 end,
808 gfp_t mask)
809{
810 return set_extent_bit(tree, start, end, EXTENT_ORDERED, 0, NULL, mask);
811}
812EXPORT_SYMBOL(set_extent_ordered);
813
Chris Masond1310b22008-01-24 16:13:08 -0500814int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
815 int bits, gfp_t mask)
816{
817 return set_extent_bit(tree, start, end, bits, 0, NULL,
818 mask);
819}
820EXPORT_SYMBOL(set_extent_bits);
821
822int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
823 int bits, gfp_t mask)
824{
825 return clear_extent_bit(tree, start, end, bits, 0, 0, mask);
826}
827EXPORT_SYMBOL(clear_extent_bits);
828
829int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
830 gfp_t mask)
831{
832 return set_extent_bit(tree, start, end,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400833 EXTENT_DELALLOC | EXTENT_DIRTY,
834 0, NULL, mask);
Chris Masond1310b22008-01-24 16:13:08 -0500835}
836EXPORT_SYMBOL(set_extent_delalloc);
837
838int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
839 gfp_t mask)
840{
841 return clear_extent_bit(tree, start, end,
842 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, mask);
843}
844EXPORT_SYMBOL(clear_extent_dirty);
845
Chris Masone6dcd2d2008-07-17 12:53:50 -0400846int clear_extent_ordered(struct extent_io_tree *tree, u64 start, u64 end,
847 gfp_t mask)
848{
849 return clear_extent_bit(tree, start, end, EXTENT_ORDERED, 1, 0, mask);
850}
851EXPORT_SYMBOL(clear_extent_ordered);
852
Chris Masond1310b22008-01-24 16:13:08 -0500853int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
854 gfp_t mask)
855{
856 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
857 mask);
858}
859EXPORT_SYMBOL(set_extent_new);
860
861int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
862 gfp_t mask)
863{
864 return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0, mask);
865}
866EXPORT_SYMBOL(clear_extent_new);
867
868int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
869 gfp_t mask)
870{
871 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL,
872 mask);
873}
874EXPORT_SYMBOL(set_extent_uptodate);
875
876int clear_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
877 gfp_t mask)
878{
879 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0, mask);
880}
881EXPORT_SYMBOL(clear_extent_uptodate);
882
883int set_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end,
884 gfp_t mask)
885{
886 return set_extent_bit(tree, start, end, EXTENT_WRITEBACK,
887 0, NULL, mask);
888}
889EXPORT_SYMBOL(set_extent_writeback);
890
891int clear_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end,
892 gfp_t mask)
893{
894 return clear_extent_bit(tree, start, end, EXTENT_WRITEBACK, 1, 0, mask);
895}
896EXPORT_SYMBOL(clear_extent_writeback);
897
898int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end)
899{
900 return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK);
901}
902EXPORT_SYMBOL(wait_on_extent_writeback);
903
Chris Masond1310b22008-01-24 16:13:08 -0500904int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
905{
906 int err;
907 u64 failed_start;
908 while (1) {
909 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, 1,
910 &failed_start, mask);
911 if (err == -EEXIST && (mask & __GFP_WAIT)) {
912 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
913 start = failed_start;
914 } else {
915 break;
916 }
917 WARN_ON(start > end);
918 }
919 return err;
920}
921EXPORT_SYMBOL(lock_extent);
922
923int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end,
924 gfp_t mask)
925{
926 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, mask);
927}
928EXPORT_SYMBOL(unlock_extent);
929
930/*
931 * helper function to set pages and extents in the tree dirty
932 */
933int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end)
934{
935 unsigned long index = start >> PAGE_CACHE_SHIFT;
936 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
937 struct page *page;
938
939 while (index <= end_index) {
940 page = find_get_page(tree->mapping, index);
941 BUG_ON(!page);
942 __set_page_dirty_nobuffers(page);
943 page_cache_release(page);
944 index++;
945 }
946 set_extent_dirty(tree, start, end, GFP_NOFS);
947 return 0;
948}
949EXPORT_SYMBOL(set_range_dirty);
950
951/*
952 * helper function to set both pages and extents in the tree writeback
953 */
954int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
955{
956 unsigned long index = start >> PAGE_CACHE_SHIFT;
957 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
958 struct page *page;
959
960 while (index <= end_index) {
961 page = find_get_page(tree->mapping, index);
962 BUG_ON(!page);
963 set_page_writeback(page);
964 page_cache_release(page);
965 index++;
966 }
967 set_extent_writeback(tree, start, end, GFP_NOFS);
968 return 0;
969}
970EXPORT_SYMBOL(set_range_writeback);
971
972int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
973 u64 *start_ret, u64 *end_ret, int bits)
974{
975 struct rb_node *node;
976 struct extent_state *state;
977 int ret = 1;
978
Chris Mason70dec802008-01-29 09:59:12 -0500979 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500980 /*
981 * this search will find all the extents that end after
982 * our range starts.
983 */
Chris Mason80ea96b2008-02-01 14:51:59 -0500984 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -0400985 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -0500986 goto out;
987 }
988
989 while(1) {
990 state = rb_entry(node, struct extent_state, rb_node);
991 if (state->end >= start && (state->state & bits)) {
992 *start_ret = state->start;
993 *end_ret = state->end;
994 ret = 0;
995 break;
996 }
997 node = rb_next(node);
998 if (!node)
999 break;
1000 }
1001out:
Chris Mason70dec802008-01-29 09:59:12 -05001002 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001003 return ret;
1004}
1005EXPORT_SYMBOL(find_first_extent_bit);
1006
Chris Masond7fc6402008-02-18 12:12:38 -05001007struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1008 u64 start, int bits)
1009{
1010 struct rb_node *node;
1011 struct extent_state *state;
1012
1013 /*
1014 * this search will find all the extents that end after
1015 * our range starts.
1016 */
1017 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001018 if (!node) {
Chris Masond7fc6402008-02-18 12:12:38 -05001019 goto out;
1020 }
1021
1022 while(1) {
1023 state = rb_entry(node, struct extent_state, rb_node);
1024 if (state->end >= start && (state->state & bits)) {
1025 return state;
1026 }
1027 node = rb_next(node);
1028 if (!node)
1029 break;
1030 }
1031out:
1032 return NULL;
1033}
1034EXPORT_SYMBOL(find_first_extent_bit_state);
1035
Chris Masond1310b22008-01-24 16:13:08 -05001036u64 find_lock_delalloc_range(struct extent_io_tree *tree,
1037 u64 *start, u64 *end, u64 max_bytes)
1038{
1039 struct rb_node *node;
1040 struct extent_state *state;
1041 u64 cur_start = *start;
1042 u64 found = 0;
1043 u64 total_bytes = 0;
1044
Chris Mason70dec802008-01-29 09:59:12 -05001045 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001046 /*
1047 * this search will find all the extents that end after
1048 * our range starts.
1049 */
1050search_again:
Chris Mason80ea96b2008-02-01 14:51:59 -05001051 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001052 if (!node) {
Chris Mason3b951512008-04-17 11:29:12 -04001053 if (!found)
1054 *end = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001055 goto out;
1056 }
1057
1058 while(1) {
1059 state = rb_entry(node, struct extent_state, rb_node);
1060 if (found && state->start != cur_start) {
1061 goto out;
1062 }
1063 if (!(state->state & EXTENT_DELALLOC)) {
1064 if (!found)
1065 *end = state->end;
1066 goto out;
1067 }
1068 if (!found) {
1069 struct extent_state *prev_state;
1070 struct rb_node *prev_node = node;
1071 while(1) {
1072 prev_node = rb_prev(prev_node);
1073 if (!prev_node)
1074 break;
1075 prev_state = rb_entry(prev_node,
1076 struct extent_state,
1077 rb_node);
1078 if (!(prev_state->state & EXTENT_DELALLOC))
1079 break;
1080 state = prev_state;
1081 node = prev_node;
1082 }
1083 }
1084 if (state->state & EXTENT_LOCKED) {
1085 DEFINE_WAIT(wait);
1086 atomic_inc(&state->refs);
1087 prepare_to_wait(&state->wq, &wait,
1088 TASK_UNINTERRUPTIBLE);
Chris Mason70dec802008-01-29 09:59:12 -05001089 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001090 schedule();
Chris Mason70dec802008-01-29 09:59:12 -05001091 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001092 finish_wait(&state->wq, &wait);
1093 free_extent_state(state);
1094 goto search_again;
1095 }
Chris Mason291d6732008-01-29 15:55:23 -05001096 set_state_cb(tree, state, EXTENT_LOCKED);
Chris Masonb0c68f82008-01-31 11:05:37 -05001097 state->state |= EXTENT_LOCKED;
Chris Masond1310b22008-01-24 16:13:08 -05001098 if (!found)
1099 *start = state->start;
1100 found++;
1101 *end = state->end;
1102 cur_start = state->end + 1;
1103 node = rb_next(node);
1104 if (!node)
1105 break;
1106 total_bytes += state->end - state->start + 1;
1107 if (total_bytes >= max_bytes)
1108 break;
1109 }
1110out:
Chris Mason70dec802008-01-29 09:59:12 -05001111 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001112 return found;
1113}
1114
1115u64 count_range_bits(struct extent_io_tree *tree,
1116 u64 *start, u64 search_end, u64 max_bytes,
1117 unsigned long bits)
1118{
1119 struct rb_node *node;
1120 struct extent_state *state;
1121 u64 cur_start = *start;
1122 u64 total_bytes = 0;
1123 int found = 0;
1124
1125 if (search_end <= cur_start) {
1126 printk("search_end %Lu start %Lu\n", search_end, cur_start);
1127 WARN_ON(1);
1128 return 0;
1129 }
1130
Chris Mason70dec802008-01-29 09:59:12 -05001131 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001132 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1133 total_bytes = tree->dirty_bytes;
1134 goto out;
1135 }
1136 /*
1137 * this search will find all the extents that end after
1138 * our range starts.
1139 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001140 node = tree_search(tree, cur_start);
Peter2b114d12008-04-01 11:21:40 -04001141 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001142 goto out;
1143 }
1144
1145 while(1) {
1146 state = rb_entry(node, struct extent_state, rb_node);
1147 if (state->start > search_end)
1148 break;
1149 if (state->end >= cur_start && (state->state & bits)) {
1150 total_bytes += min(search_end, state->end) + 1 -
1151 max(cur_start, state->start);
1152 if (total_bytes >= max_bytes)
1153 break;
1154 if (!found) {
1155 *start = state->start;
1156 found = 1;
1157 }
1158 }
1159 node = rb_next(node);
1160 if (!node)
1161 break;
1162 }
1163out:
Chris Mason70dec802008-01-29 09:59:12 -05001164 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001165 return total_bytes;
1166}
1167/*
1168 * helper function to lock both pages and extents in the tree.
1169 * pages must be locked first.
1170 */
1171int lock_range(struct extent_io_tree *tree, u64 start, u64 end)
1172{
1173 unsigned long index = start >> PAGE_CACHE_SHIFT;
1174 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1175 struct page *page;
1176 int err;
1177
1178 while (index <= end_index) {
1179 page = grab_cache_page(tree->mapping, index);
1180 if (!page) {
1181 err = -ENOMEM;
1182 goto failed;
1183 }
1184 if (IS_ERR(page)) {
1185 err = PTR_ERR(page);
1186 goto failed;
1187 }
1188 index++;
1189 }
1190 lock_extent(tree, start, end, GFP_NOFS);
1191 return 0;
1192
1193failed:
1194 /*
1195 * we failed above in getting the page at 'index', so we undo here
1196 * up to but not including the page at 'index'
1197 */
1198 end_index = index;
1199 index = start >> PAGE_CACHE_SHIFT;
1200 while (index < end_index) {
1201 page = find_get_page(tree->mapping, index);
1202 unlock_page(page);
1203 page_cache_release(page);
1204 index++;
1205 }
1206 return err;
1207}
1208EXPORT_SYMBOL(lock_range);
1209
1210/*
1211 * helper function to unlock both pages and extents in the tree.
1212 */
1213int unlock_range(struct extent_io_tree *tree, u64 start, u64 end)
1214{
1215 unsigned long index = start >> PAGE_CACHE_SHIFT;
1216 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1217 struct page *page;
1218
1219 while (index <= end_index) {
1220 page = find_get_page(tree->mapping, index);
1221 unlock_page(page);
1222 page_cache_release(page);
1223 index++;
1224 }
1225 unlock_extent(tree, start, end, GFP_NOFS);
1226 return 0;
1227}
1228EXPORT_SYMBOL(unlock_range);
1229
1230int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1231{
1232 struct rb_node *node;
1233 struct extent_state *state;
1234 int ret = 0;
1235
Chris Mason70dec802008-01-29 09:59:12 -05001236 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001237 /*
1238 * this search will find all the extents that end after
1239 * our range starts.
1240 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001241 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001242 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001243 ret = -ENOENT;
1244 goto out;
1245 }
1246 state = rb_entry(node, struct extent_state, rb_node);
1247 if (state->start != start) {
1248 ret = -ENOENT;
1249 goto out;
1250 }
1251 state->private = private;
1252out:
Chris Mason70dec802008-01-29 09:59:12 -05001253 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001254 return ret;
1255}
1256
1257int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1258{
1259 struct rb_node *node;
1260 struct extent_state *state;
1261 int ret = 0;
1262
Chris Mason70dec802008-01-29 09:59:12 -05001263 spin_lock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001264 /*
1265 * this search will find all the extents that end after
1266 * our range starts.
1267 */
Chris Mason80ea96b2008-02-01 14:51:59 -05001268 node = tree_search(tree, start);
Peter2b114d12008-04-01 11:21:40 -04001269 if (!node) {
Chris Masond1310b22008-01-24 16:13:08 -05001270 ret = -ENOENT;
1271 goto out;
1272 }
1273 state = rb_entry(node, struct extent_state, rb_node);
1274 if (state->start != start) {
1275 ret = -ENOENT;
1276 goto out;
1277 }
1278 *private = state->private;
1279out:
Chris Mason70dec802008-01-29 09:59:12 -05001280 spin_unlock_irq(&tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05001281 return ret;
1282}
1283
1284/*
1285 * searches a range in the state tree for a given mask.
Chris Mason70dec802008-01-29 09:59:12 -05001286 * If 'filled' == 1, this returns 1 only if every extent in the tree
Chris Masond1310b22008-01-24 16:13:08 -05001287 * has the bits set. Otherwise, 1 is returned if any bit in the
1288 * range is found set.
1289 */
1290int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1291 int bits, int filled)
1292{
1293 struct extent_state *state = NULL;
1294 struct rb_node *node;
1295 int bitset = 0;
1296 unsigned long flags;
1297
Chris Mason70dec802008-01-29 09:59:12 -05001298 spin_lock_irqsave(&tree->lock, flags);
Chris Mason80ea96b2008-02-01 14:51:59 -05001299 node = tree_search(tree, start);
Chris Masond1310b22008-01-24 16:13:08 -05001300 while (node && start <= end) {
1301 state = rb_entry(node, struct extent_state, rb_node);
1302
1303 if (filled && state->start > start) {
1304 bitset = 0;
1305 break;
1306 }
1307
1308 if (state->start > end)
1309 break;
1310
1311 if (state->state & bits) {
1312 bitset = 1;
1313 if (!filled)
1314 break;
1315 } else if (filled) {
1316 bitset = 0;
1317 break;
1318 }
1319 start = state->end + 1;
1320 if (start > end)
1321 break;
1322 node = rb_next(node);
1323 if (!node) {
1324 if (filled)
1325 bitset = 0;
1326 break;
1327 }
1328 }
Chris Mason70dec802008-01-29 09:59:12 -05001329 spin_unlock_irqrestore(&tree->lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -05001330 return bitset;
1331}
1332EXPORT_SYMBOL(test_range_bit);
1333
1334/*
1335 * helper function to set a given page up to date if all the
1336 * extents in the tree for that page are up to date
1337 */
1338static int check_page_uptodate(struct extent_io_tree *tree,
1339 struct page *page)
1340{
1341 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1342 u64 end = start + PAGE_CACHE_SIZE - 1;
1343 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1))
1344 SetPageUptodate(page);
1345 return 0;
1346}
1347
1348/*
1349 * helper function to unlock a page if all the extents in the tree
1350 * for that page are unlocked
1351 */
1352static int check_page_locked(struct extent_io_tree *tree,
1353 struct page *page)
1354{
1355 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1356 u64 end = start + PAGE_CACHE_SIZE - 1;
1357 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0))
1358 unlock_page(page);
1359 return 0;
1360}
1361
1362/*
1363 * helper function to end page writeback if all the extents
1364 * in the tree for that page are done with writeback
1365 */
1366static int check_page_writeback(struct extent_io_tree *tree,
1367 struct page *page)
1368{
1369 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1370 u64 end = start + PAGE_CACHE_SIZE - 1;
1371 if (!test_range_bit(tree, start, end, EXTENT_WRITEBACK, 0))
1372 end_page_writeback(page);
1373 return 0;
1374}
1375
1376/* lots and lots of room for performance fixes in the end_bio funcs */
1377
1378/*
1379 * after a writepage IO is done, we need to:
1380 * clear the uptodate bits on error
1381 * clear the writeback bits in the extent tree for this IO
1382 * end_page_writeback if the page has no more pending IO
1383 *
1384 * Scheduling is not allowed, so the extent state tree is expected
1385 * to have one and only one object corresponding to this IO.
1386 */
1387#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1388static void end_bio_extent_writepage(struct bio *bio, int err)
1389#else
1390static int end_bio_extent_writepage(struct bio *bio,
1391 unsigned int bytes_done, int err)
1392#endif
1393{
Chris Mason1259ab72008-05-12 13:39:03 -04001394 int uptodate = err == 0;
Chris Masond1310b22008-01-24 16:13:08 -05001395 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
Chris Mason70dec802008-01-29 09:59:12 -05001396 struct extent_state *state = bio->bi_private;
1397 struct extent_io_tree *tree = state->tree;
1398 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -05001399 u64 start;
1400 u64 end;
Chris Mason70dec802008-01-29 09:59:12 -05001401 u64 cur;
Chris Masond1310b22008-01-24 16:13:08 -05001402 int whole_page;
Chris Mason1259ab72008-05-12 13:39:03 -04001403 int ret;
Chris Mason70dec802008-01-29 09:59:12 -05001404 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -05001405
1406#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1407 if (bio->bi_size)
1408 return 1;
1409#endif
Chris Masond1310b22008-01-24 16:13:08 -05001410 do {
1411 struct page *page = bvec->bv_page;
1412 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1413 bvec->bv_offset;
1414 end = start + bvec->bv_len - 1;
1415
1416 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1417 whole_page = 1;
1418 else
1419 whole_page = 0;
1420
1421 if (--bvec >= bio->bi_io_vec)
1422 prefetchw(&bvec->bv_page->flags);
Chris Mason1259ab72008-05-12 13:39:03 -04001423 if (tree->ops && tree->ops->writepage_end_io_hook) {
1424 ret = tree->ops->writepage_end_io_hook(page, start,
Chris Masone6dcd2d2008-07-17 12:53:50 -04001425 end, state, uptodate);
Chris Mason1259ab72008-05-12 13:39:03 -04001426 if (ret)
1427 uptodate = 0;
1428 }
1429
1430 if (!uptodate && tree->ops &&
1431 tree->ops->writepage_io_failed_hook) {
1432 ret = tree->ops->writepage_io_failed_hook(bio, page,
1433 start, end, state);
1434 if (ret == 0) {
1435 state = NULL;
1436 uptodate = (err == 0);
1437 continue;
1438 }
1439 }
1440
Chris Masond1310b22008-01-24 16:13:08 -05001441 if (!uptodate) {
1442 clear_extent_uptodate(tree, start, end, GFP_ATOMIC);
1443 ClearPageUptodate(page);
1444 SetPageError(page);
1445 }
Chris Mason70dec802008-01-29 09:59:12 -05001446
Chris Mason70dec802008-01-29 09:59:12 -05001447 /*
1448 * bios can get merged in funny ways, and so we need to
1449 * be careful with the state variable. We know the
1450 * state won't be merged with others because it has
1451 * WRITEBACK set, but we can't be sure each biovec is
1452 * sequential in the file. So, if our cached state
1453 * doesn't match the expected end, search the tree
1454 * for the correct one.
1455 */
1456
1457 spin_lock_irqsave(&tree->lock, flags);
1458 if (!state || state->end != end) {
1459 state = NULL;
Chris Mason80ea96b2008-02-01 14:51:59 -05001460 node = __etree_search(tree, start, NULL, NULL);
Chris Mason70dec802008-01-29 09:59:12 -05001461 if (node) {
1462 state = rb_entry(node, struct extent_state,
1463 rb_node);
1464 if (state->end != end ||
1465 !(state->state & EXTENT_WRITEBACK))
1466 state = NULL;
1467 }
1468 if (!state) {
1469 spin_unlock_irqrestore(&tree->lock, flags);
1470 clear_extent_writeback(tree, start,
1471 end, GFP_ATOMIC);
1472 goto next_io;
1473 }
1474 }
1475 cur = end;
1476 while(1) {
1477 struct extent_state *clear = state;
1478 cur = state->start;
1479 node = rb_prev(&state->rb_node);
1480 if (node) {
1481 state = rb_entry(node,
1482 struct extent_state,
1483 rb_node);
1484 } else {
1485 state = NULL;
1486 }
1487
1488 clear_state_bit(tree, clear, EXTENT_WRITEBACK,
1489 1, 0);
1490 if (cur == start)
1491 break;
1492 if (cur < start) {
1493 WARN_ON(1);
1494 break;
1495 }
1496 if (!node)
1497 break;
1498 }
1499 /* before releasing the lock, make sure the next state
1500 * variable has the expected bits set and corresponds
1501 * to the correct offsets in the file
1502 */
1503 if (state && (state->end + 1 != start ||
Yanc2e639f2008-02-04 08:57:25 -05001504 !(state->state & EXTENT_WRITEBACK))) {
Chris Mason70dec802008-01-29 09:59:12 -05001505 state = NULL;
1506 }
1507 spin_unlock_irqrestore(&tree->lock, flags);
1508next_io:
Chris Masond1310b22008-01-24 16:13:08 -05001509
1510 if (whole_page)
1511 end_page_writeback(page);
1512 else
1513 check_page_writeback(tree, page);
Chris Masond1310b22008-01-24 16:13:08 -05001514 } while (bvec >= bio->bi_io_vec);
Chris Masond1310b22008-01-24 16:13:08 -05001515 bio_put(bio);
1516#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1517 return 0;
1518#endif
1519}
1520
1521/*
1522 * after a readpage IO is done, we need to:
1523 * clear the uptodate bits on error
1524 * set the uptodate bits if things worked
1525 * set the page up to date if all extents in the tree are uptodate
1526 * clear the lock bit in the extent tree
1527 * unlock the page if there are no other extents locked for it
1528 *
1529 * Scheduling is not allowed, so the extent state tree is expected
1530 * to have one and only one object corresponding to this IO.
1531 */
1532#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1533static void end_bio_extent_readpage(struct bio *bio, int err)
1534#else
1535static int end_bio_extent_readpage(struct bio *bio,
1536 unsigned int bytes_done, int err)
1537#endif
1538{
1539 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1540 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
Chris Mason70dec802008-01-29 09:59:12 -05001541 struct extent_state *state = bio->bi_private;
1542 struct extent_io_tree *tree = state->tree;
1543 struct rb_node *node;
Chris Masond1310b22008-01-24 16:13:08 -05001544 u64 start;
1545 u64 end;
Chris Mason70dec802008-01-29 09:59:12 -05001546 u64 cur;
1547 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -05001548 int whole_page;
1549 int ret;
1550
1551#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1552 if (bio->bi_size)
1553 return 1;
1554#endif
1555
1556 do {
1557 struct page *page = bvec->bv_page;
1558 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1559 bvec->bv_offset;
1560 end = start + bvec->bv_len - 1;
1561
1562 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1563 whole_page = 1;
1564 else
1565 whole_page = 0;
1566
1567 if (--bvec >= bio->bi_io_vec)
1568 prefetchw(&bvec->bv_page->flags);
1569
1570 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
Chris Mason70dec802008-01-29 09:59:12 -05001571 ret = tree->ops->readpage_end_io_hook(page, start, end,
1572 state);
Chris Masond1310b22008-01-24 16:13:08 -05001573 if (ret)
1574 uptodate = 0;
1575 }
Chris Mason7e383262008-04-09 16:28:12 -04001576 if (!uptodate && tree->ops &&
1577 tree->ops->readpage_io_failed_hook) {
1578 ret = tree->ops->readpage_io_failed_hook(bio, page,
1579 start, end, state);
1580 if (ret == 0) {
1581 state = NULL;
Chris Mason3b951512008-04-17 11:29:12 -04001582 uptodate =
1583 test_bit(BIO_UPTODATE, &bio->bi_flags);
Chris Mason7e383262008-04-09 16:28:12 -04001584 continue;
1585 }
1586 }
Chris Mason70dec802008-01-29 09:59:12 -05001587
1588 spin_lock_irqsave(&tree->lock, flags);
1589 if (!state || state->end != end) {
1590 state = NULL;
Chris Mason80ea96b2008-02-01 14:51:59 -05001591 node = __etree_search(tree, start, NULL, NULL);
Chris Mason70dec802008-01-29 09:59:12 -05001592 if (node) {
1593 state = rb_entry(node, struct extent_state,
1594 rb_node);
1595 if (state->end != end ||
1596 !(state->state & EXTENT_LOCKED))
1597 state = NULL;
1598 }
Chris Mason3b951512008-04-17 11:29:12 -04001599 if (!state) {
Chris Mason70dec802008-01-29 09:59:12 -05001600 spin_unlock_irqrestore(&tree->lock, flags);
Chris Mason3b951512008-04-17 11:29:12 -04001601 if (uptodate)
1602 set_extent_uptodate(tree, start, end,
1603 GFP_ATOMIC);
Chris Mason70dec802008-01-29 09:59:12 -05001604 unlock_extent(tree, start, end, GFP_ATOMIC);
1605 goto next_io;
1606 }
Chris Masond1310b22008-01-24 16:13:08 -05001607 }
1608
Chris Mason70dec802008-01-29 09:59:12 -05001609 cur = end;
1610 while(1) {
1611 struct extent_state *clear = state;
1612 cur = state->start;
1613 node = rb_prev(&state->rb_node);
1614 if (node) {
1615 state = rb_entry(node,
1616 struct extent_state,
1617 rb_node);
1618 } else {
1619 state = NULL;
1620 }
Chris Masonf1885912008-04-09 16:28:12 -04001621 if (uptodate) {
1622 set_state_cb(tree, clear, EXTENT_UPTODATE);
1623 clear->state |= EXTENT_UPTODATE;
1624 }
Chris Mason70dec802008-01-29 09:59:12 -05001625 clear_state_bit(tree, clear, EXTENT_LOCKED,
1626 1, 0);
1627 if (cur == start)
1628 break;
1629 if (cur < start) {
1630 WARN_ON(1);
1631 break;
1632 }
1633 if (!node)
1634 break;
1635 }
1636 /* before releasing the lock, make sure the next state
1637 * variable has the expected bits set and corresponds
1638 * to the correct offsets in the file
1639 */
1640 if (state && (state->end + 1 != start ||
Yanc2e639f2008-02-04 08:57:25 -05001641 !(state->state & EXTENT_LOCKED))) {
Chris Mason70dec802008-01-29 09:59:12 -05001642 state = NULL;
1643 }
1644 spin_unlock_irqrestore(&tree->lock, flags);
1645next_io:
1646 if (whole_page) {
1647 if (uptodate) {
1648 SetPageUptodate(page);
1649 } else {
1650 ClearPageUptodate(page);
1651 SetPageError(page);
1652 }
Chris Masond1310b22008-01-24 16:13:08 -05001653 unlock_page(page);
Chris Mason70dec802008-01-29 09:59:12 -05001654 } else {
1655 if (uptodate) {
1656 check_page_uptodate(tree, page);
1657 } else {
1658 ClearPageUptodate(page);
1659 SetPageError(page);
1660 }
Chris Masond1310b22008-01-24 16:13:08 -05001661 check_page_locked(tree, page);
Chris Mason70dec802008-01-29 09:59:12 -05001662 }
Chris Masond1310b22008-01-24 16:13:08 -05001663 } while (bvec >= bio->bi_io_vec);
1664
1665 bio_put(bio);
1666#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1667 return 0;
1668#endif
1669}
1670
1671/*
1672 * IO done from prepare_write is pretty simple, we just unlock
1673 * the structs in the extent tree when done, and set the uptodate bits
1674 * as appropriate.
1675 */
1676#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
1677static void end_bio_extent_preparewrite(struct bio *bio, int err)
1678#else
1679static int end_bio_extent_preparewrite(struct bio *bio,
1680 unsigned int bytes_done, int err)
1681#endif
1682{
1683 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1684 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
Chris Mason70dec802008-01-29 09:59:12 -05001685 struct extent_state *state = bio->bi_private;
1686 struct extent_io_tree *tree = state->tree;
Chris Masond1310b22008-01-24 16:13:08 -05001687 u64 start;
1688 u64 end;
1689
1690#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1691 if (bio->bi_size)
1692 return 1;
1693#endif
1694
1695 do {
1696 struct page *page = bvec->bv_page;
1697 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1698 bvec->bv_offset;
1699 end = start + bvec->bv_len - 1;
1700
1701 if (--bvec >= bio->bi_io_vec)
1702 prefetchw(&bvec->bv_page->flags);
1703
1704 if (uptodate) {
1705 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1706 } else {
1707 ClearPageUptodate(page);
1708 SetPageError(page);
1709 }
1710
1711 unlock_extent(tree, start, end, GFP_ATOMIC);
1712
1713 } while (bvec >= bio->bi_io_vec);
1714
1715 bio_put(bio);
1716#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
1717 return 0;
1718#endif
1719}
1720
1721static struct bio *
1722extent_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1723 gfp_t gfp_flags)
1724{
1725 struct bio *bio;
1726
1727 bio = bio_alloc(gfp_flags, nr_vecs);
1728
1729 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1730 while (!bio && (nr_vecs /= 2))
1731 bio = bio_alloc(gfp_flags, nr_vecs);
1732 }
1733
1734 if (bio) {
Chris Masone1c4b742008-04-22 13:26:46 -04001735 bio->bi_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001736 bio->bi_bdev = bdev;
1737 bio->bi_sector = first_sector;
1738 }
1739 return bio;
1740}
1741
Chris Masonf1885912008-04-09 16:28:12 -04001742static int submit_one_bio(int rw, struct bio *bio, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05001743{
Chris Masond1310b22008-01-24 16:13:08 -05001744 int ret = 0;
Chris Mason70dec802008-01-29 09:59:12 -05001745 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1746 struct page *page = bvec->bv_page;
1747 struct extent_io_tree *tree = bio->bi_private;
1748 struct rb_node *node;
1749 struct extent_state *state;
1750 u64 start;
1751 u64 end;
1752
1753 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
1754 end = start + bvec->bv_len - 1;
1755
1756 spin_lock_irq(&tree->lock);
Chris Mason80ea96b2008-02-01 14:51:59 -05001757 node = __etree_search(tree, start, NULL, NULL);
Chris Mason70dec802008-01-29 09:59:12 -05001758 BUG_ON(!node);
1759 state = rb_entry(node, struct extent_state, rb_node);
1760 while(state->end < end) {
1761 node = rb_next(node);
1762 state = rb_entry(node, struct extent_state, rb_node);
1763 }
1764 BUG_ON(state->end != end);
1765 spin_unlock_irq(&tree->lock);
1766
1767 bio->bi_private = state;
Chris Masond1310b22008-01-24 16:13:08 -05001768
1769 bio_get(bio);
1770
Chris Mason065631f2008-02-20 12:07:25 -05001771 if (tree->ops && tree->ops->submit_bio_hook)
Chris Masonf1885912008-04-09 16:28:12 -04001772 tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
1773 mirror_num);
Chris Mason0b86a832008-03-24 15:01:56 -04001774 else
1775 submit_bio(rw, bio);
Chris Masond1310b22008-01-24 16:13:08 -05001776 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1777 ret = -EOPNOTSUPP;
1778 bio_put(bio);
1779 return ret;
1780}
1781
1782static int submit_extent_page(int rw, struct extent_io_tree *tree,
1783 struct page *page, sector_t sector,
1784 size_t size, unsigned long offset,
1785 struct block_device *bdev,
1786 struct bio **bio_ret,
1787 unsigned long max_pages,
Chris Masonf1885912008-04-09 16:28:12 -04001788 bio_end_io_t end_io_func,
1789 int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05001790{
1791 int ret = 0;
1792 struct bio *bio;
1793 int nr;
1794
1795 if (bio_ret && *bio_ret) {
1796 bio = *bio_ret;
1797 if (bio->bi_sector + (bio->bi_size >> 9) != sector ||
Chris Mason239b14b2008-03-24 15:02:07 -04001798 (tree->ops && tree->ops->merge_bio_hook &&
1799 tree->ops->merge_bio_hook(page, offset, size, bio)) ||
Chris Masond1310b22008-01-24 16:13:08 -05001800 bio_add_page(bio, page, size, offset) < size) {
Chris Masonf1885912008-04-09 16:28:12 -04001801 ret = submit_one_bio(rw, bio, mirror_num);
Chris Masond1310b22008-01-24 16:13:08 -05001802 bio = NULL;
1803 } else {
1804 return 0;
1805 }
1806 }
Chris Mason961d0232008-02-06 11:01:42 -05001807 nr = bio_get_nr_vecs(bdev);
Chris Masond1310b22008-01-24 16:13:08 -05001808 bio = extent_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
1809 if (!bio) {
1810 printk("failed to allocate bio nr %d\n", nr);
1811 }
Chris Mason70dec802008-01-29 09:59:12 -05001812
1813
Chris Masond1310b22008-01-24 16:13:08 -05001814 bio_add_page(bio, page, size, offset);
1815 bio->bi_end_io = end_io_func;
1816 bio->bi_private = tree;
Chris Mason70dec802008-01-29 09:59:12 -05001817
Chris Masond1310b22008-01-24 16:13:08 -05001818 if (bio_ret) {
1819 *bio_ret = bio;
1820 } else {
Chris Masonf1885912008-04-09 16:28:12 -04001821 ret = submit_one_bio(rw, bio, mirror_num);
Chris Masond1310b22008-01-24 16:13:08 -05001822 }
1823
1824 return ret;
1825}
1826
1827void set_page_extent_mapped(struct page *page)
1828{
1829 if (!PagePrivate(page)) {
1830 SetPagePrivate(page);
Chris Masond1310b22008-01-24 16:13:08 -05001831 page_cache_get(page);
Chris Mason6af118ce2008-07-22 11:18:07 -04001832 set_page_private(page, EXTENT_PAGE_PRIVATE);
Chris Masond1310b22008-01-24 16:13:08 -05001833 }
1834}
1835
1836void set_page_extent_head(struct page *page, unsigned long len)
1837{
1838 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
1839}
1840
1841/*
1842 * basic readpage implementation. Locked extent state structs are inserted
1843 * into the tree that are removed when the IO is done (by the end_io
1844 * handlers)
1845 */
1846static int __extent_read_full_page(struct extent_io_tree *tree,
1847 struct page *page,
1848 get_extent_t *get_extent,
Chris Masonf1885912008-04-09 16:28:12 -04001849 struct bio **bio, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05001850{
1851 struct inode *inode = page->mapping->host;
1852 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1853 u64 page_end = start + PAGE_CACHE_SIZE - 1;
1854 u64 end;
1855 u64 cur = start;
1856 u64 extent_offset;
1857 u64 last_byte = i_size_read(inode);
1858 u64 block_start;
1859 u64 cur_end;
1860 sector_t sector;
1861 struct extent_map *em;
1862 struct block_device *bdev;
1863 int ret;
1864 int nr = 0;
1865 size_t page_offset = 0;
1866 size_t iosize;
1867 size_t blocksize = inode->i_sb->s_blocksize;
1868
1869 set_page_extent_mapped(page);
1870
1871 end = page_end;
1872 lock_extent(tree, start, end, GFP_NOFS);
1873
1874 while (cur <= end) {
1875 if (cur >= last_byte) {
1876 char *userpage;
1877 iosize = PAGE_CACHE_SIZE - page_offset;
1878 userpage = kmap_atomic(page, KM_USER0);
1879 memset(userpage + page_offset, 0, iosize);
1880 flush_dcache_page(page);
1881 kunmap_atomic(userpage, KM_USER0);
1882 set_extent_uptodate(tree, cur, cur + iosize - 1,
1883 GFP_NOFS);
1884 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1885 break;
1886 }
1887 em = get_extent(inode, page, page_offset, cur,
1888 end - cur + 1, 0);
1889 if (IS_ERR(em) || !em) {
1890 SetPageError(page);
1891 unlock_extent(tree, cur, end, GFP_NOFS);
1892 break;
1893 }
Chris Masond1310b22008-01-24 16:13:08 -05001894 extent_offset = cur - em->start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001895 if (extent_map_end(em) <= cur) {
1896printk("bad mapping em [%Lu %Lu] cur %Lu\n", em->start, extent_map_end(em), cur);
1897 }
Chris Masond1310b22008-01-24 16:13:08 -05001898 BUG_ON(extent_map_end(em) <= cur);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001899 if (end < cur) {
1900printk("2bad mapping end %Lu cur %Lu\n", end, cur);
1901 }
Chris Masond1310b22008-01-24 16:13:08 -05001902 BUG_ON(end < cur);
1903
1904 iosize = min(extent_map_end(em) - cur, end - cur + 1);
1905 cur_end = min(extent_map_end(em) - 1, end);
1906 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
1907 sector = (em->block_start + extent_offset) >> 9;
1908 bdev = em->bdev;
1909 block_start = em->block_start;
1910 free_extent_map(em);
1911 em = NULL;
1912
1913 /* we've found a hole, just zero and go on */
1914 if (block_start == EXTENT_MAP_HOLE) {
1915 char *userpage;
1916 userpage = kmap_atomic(page, KM_USER0);
1917 memset(userpage + page_offset, 0, iosize);
1918 flush_dcache_page(page);
1919 kunmap_atomic(userpage, KM_USER0);
1920
1921 set_extent_uptodate(tree, cur, cur + iosize - 1,
1922 GFP_NOFS);
1923 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1924 cur = cur + iosize;
1925 page_offset += iosize;
1926 continue;
1927 }
1928 /* the get_extent function already copied into the page */
1929 if (test_range_bit(tree, cur, cur_end, EXTENT_UPTODATE, 1)) {
1930 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1931 cur = cur + iosize;
1932 page_offset += iosize;
1933 continue;
1934 }
Chris Mason70dec802008-01-29 09:59:12 -05001935 /* we have an inline extent but it didn't get marked up
1936 * to date. Error out
1937 */
1938 if (block_start == EXTENT_MAP_INLINE) {
1939 SetPageError(page);
1940 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
1941 cur = cur + iosize;
1942 page_offset += iosize;
1943 continue;
1944 }
Chris Masond1310b22008-01-24 16:13:08 -05001945
1946 ret = 0;
1947 if (tree->ops && tree->ops->readpage_io_hook) {
1948 ret = tree->ops->readpage_io_hook(page, cur,
1949 cur + iosize - 1);
1950 }
1951 if (!ret) {
Chris Mason89642222008-07-24 09:41:53 -04001952 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
1953 pnr -= page->index;
Chris Masond1310b22008-01-24 16:13:08 -05001954 ret = submit_extent_page(READ, tree, page,
1955 sector, iosize, page_offset,
Chris Mason89642222008-07-24 09:41:53 -04001956 bdev, bio, pnr,
Chris Masonf1885912008-04-09 16:28:12 -04001957 end_bio_extent_readpage, mirror_num);
Chris Mason89642222008-07-24 09:41:53 -04001958 nr++;
Chris Masond1310b22008-01-24 16:13:08 -05001959 }
1960 if (ret)
1961 SetPageError(page);
1962 cur = cur + iosize;
1963 page_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05001964 }
1965 if (!nr) {
1966 if (!PageError(page))
1967 SetPageUptodate(page);
1968 unlock_page(page);
1969 }
1970 return 0;
1971}
1972
1973int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
1974 get_extent_t *get_extent)
1975{
1976 struct bio *bio = NULL;
1977 int ret;
1978
Chris Masonf1885912008-04-09 16:28:12 -04001979 ret = __extent_read_full_page(tree, page, get_extent, &bio, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001980 if (bio)
Chris Masonf1885912008-04-09 16:28:12 -04001981 submit_one_bio(READ, bio, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001982 return ret;
1983}
1984EXPORT_SYMBOL(extent_read_full_page);
1985
1986/*
1987 * the writepage semantics are similar to regular writepage. extent
1988 * records are inserted to lock ranges in the tree, and as dirty areas
1989 * are found, they are marked writeback. Then the lock bits are removed
1990 * and the end_io handler clears the writeback ranges
1991 */
1992static int __extent_writepage(struct page *page, struct writeback_control *wbc,
1993 void *data)
1994{
1995 struct inode *inode = page->mapping->host;
1996 struct extent_page_data *epd = data;
1997 struct extent_io_tree *tree = epd->tree;
1998 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1999 u64 delalloc_start;
2000 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2001 u64 end;
2002 u64 cur = start;
2003 u64 extent_offset;
2004 u64 last_byte = i_size_read(inode);
2005 u64 block_start;
2006 u64 iosize;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002007 u64 unlock_start;
Chris Masond1310b22008-01-24 16:13:08 -05002008 sector_t sector;
2009 struct extent_map *em;
2010 struct block_device *bdev;
2011 int ret;
2012 int nr = 0;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002013 size_t pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002014 size_t blocksize;
2015 loff_t i_size = i_size_read(inode);
2016 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2017 u64 nr_delalloc;
2018 u64 delalloc_end;
2019
2020 WARN_ON(!PageLocked(page));
Chris Mason7f3c74f2008-07-18 12:01:11 -04002021 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
Chris Mason211c17f2008-05-15 09:13:45 -04002022 if (page->index > end_index ||
Chris Mason7f3c74f2008-07-18 12:01:11 -04002023 (page->index == end_index && !pg_offset)) {
Chris Mason211c17f2008-05-15 09:13:45 -04002024 page->mapping->a_ops->invalidatepage(page, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002025 unlock_page(page);
2026 return 0;
2027 }
2028
2029 if (page->index == end_index) {
2030 char *userpage;
2031
Chris Masond1310b22008-01-24 16:13:08 -05002032 userpage = kmap_atomic(page, KM_USER0);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002033 memset(userpage + pg_offset, 0,
2034 PAGE_CACHE_SIZE - pg_offset);
Chris Masond1310b22008-01-24 16:13:08 -05002035 kunmap_atomic(userpage, KM_USER0);
Chris Mason211c17f2008-05-15 09:13:45 -04002036 flush_dcache_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002037 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002038 pg_offset = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002039
2040 set_page_extent_mapped(page);
2041
2042 delalloc_start = start;
2043 delalloc_end = 0;
2044 while(delalloc_end < page_end) {
2045 nr_delalloc = find_lock_delalloc_range(tree, &delalloc_start,
2046 &delalloc_end,
2047 128 * 1024 * 1024);
2048 if (nr_delalloc == 0) {
2049 delalloc_start = delalloc_end + 1;
2050 continue;
2051 }
2052 tree->ops->fill_delalloc(inode, delalloc_start,
2053 delalloc_end);
2054 clear_extent_bit(tree, delalloc_start,
2055 delalloc_end,
2056 EXTENT_LOCKED | EXTENT_DELALLOC,
2057 1, 0, GFP_NOFS);
2058 delalloc_start = delalloc_end + 1;
2059 }
2060 lock_extent(tree, start, page_end, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002061 unlock_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002062
Chris Mason247e7432008-07-17 12:53:51 -04002063 if (tree->ops && tree->ops->writepage_start_hook) {
2064 ret = tree->ops->writepage_start_hook(page, start, page_end);
2065 if (ret == -EAGAIN) {
2066 unlock_extent(tree, start, page_end, GFP_NOFS);
2067 redirty_page_for_writepage(wbc, page);
2068 unlock_page(page);
2069 return 0;
2070 }
2071 }
2072
Chris Masond1310b22008-01-24 16:13:08 -05002073 end = page_end;
2074 if (test_range_bit(tree, start, page_end, EXTENT_DELALLOC, 0)) {
2075 printk("found delalloc bits after lock_extent\n");
2076 }
2077
2078 if (last_byte <= start) {
2079 clear_extent_dirty(tree, start, page_end, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002080 unlock_extent(tree, start, page_end, GFP_NOFS);
2081 if (tree->ops && tree->ops->writepage_end_io_hook)
2082 tree->ops->writepage_end_io_hook(page, start,
2083 page_end, NULL, 1);
2084 unlock_start = page_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002085 goto done;
2086 }
2087
2088 set_extent_uptodate(tree, start, page_end, GFP_NOFS);
2089 blocksize = inode->i_sb->s_blocksize;
2090
2091 while (cur <= end) {
2092 if (cur >= last_byte) {
2093 clear_extent_dirty(tree, cur, page_end, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002094 unlock_extent(tree, unlock_start, page_end, GFP_NOFS);
2095 if (tree->ops && tree->ops->writepage_end_io_hook)
2096 tree->ops->writepage_end_io_hook(page, cur,
2097 page_end, NULL, 1);
2098 unlock_start = page_end + 1;
Chris Masond1310b22008-01-24 16:13:08 -05002099 break;
2100 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002101 em = epd->get_extent(inode, page, pg_offset, cur,
Chris Masond1310b22008-01-24 16:13:08 -05002102 end - cur + 1, 1);
2103 if (IS_ERR(em) || !em) {
2104 SetPageError(page);
2105 break;
2106 }
2107
2108 extent_offset = cur - em->start;
2109 BUG_ON(extent_map_end(em) <= cur);
2110 BUG_ON(end < cur);
2111 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2112 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2113 sector = (em->block_start + extent_offset) >> 9;
2114 bdev = em->bdev;
2115 block_start = em->block_start;
2116 free_extent_map(em);
2117 em = NULL;
2118
2119 if (block_start == EXTENT_MAP_HOLE ||
2120 block_start == EXTENT_MAP_INLINE) {
2121 clear_extent_dirty(tree, cur,
2122 cur + iosize - 1, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002123
2124 unlock_extent(tree, unlock_start, cur + iosize -1,
2125 GFP_NOFS);
Chris Mason7f3c74f2008-07-18 12:01:11 -04002126
Chris Masone6dcd2d2008-07-17 12:53:50 -04002127 if (tree->ops && tree->ops->writepage_end_io_hook)
2128 tree->ops->writepage_end_io_hook(page, cur,
2129 cur + iosize - 1,
2130 NULL, 1);
Chris Masond1310b22008-01-24 16:13:08 -05002131 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002132 pg_offset += iosize;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002133 unlock_start = cur;
Chris Masond1310b22008-01-24 16:13:08 -05002134 continue;
2135 }
2136
2137 /* leave this out until we have a page_mkwrite call */
2138 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
2139 EXTENT_DIRTY, 0)) {
2140 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002141 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002142 continue;
2143 }
2144 clear_extent_dirty(tree, cur, cur + iosize - 1, GFP_NOFS);
2145 if (tree->ops && tree->ops->writepage_io_hook) {
2146 ret = tree->ops->writepage_io_hook(page, cur,
2147 cur + iosize - 1);
2148 } else {
2149 ret = 0;
2150 }
Chris Mason1259ab72008-05-12 13:39:03 -04002151 if (ret) {
Chris Masond1310b22008-01-24 16:13:08 -05002152 SetPageError(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002153 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002154 unsigned long max_nr = end_index + 1;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002155
Chris Masond1310b22008-01-24 16:13:08 -05002156 set_range_writeback(tree, cur, cur + iosize - 1);
2157 if (!PageWriteback(page)) {
2158 printk("warning page %lu not writeback, "
2159 "cur %llu end %llu\n", page->index,
2160 (unsigned long long)cur,
2161 (unsigned long long)end);
2162 }
2163
2164 ret = submit_extent_page(WRITE, tree, page, sector,
Chris Mason7f3c74f2008-07-18 12:01:11 -04002165 iosize, pg_offset, bdev,
Chris Masond1310b22008-01-24 16:13:08 -05002166 &epd->bio, max_nr,
Chris Masonf1885912008-04-09 16:28:12 -04002167 end_bio_extent_writepage, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002168 if (ret)
2169 SetPageError(page);
2170 }
2171 cur = cur + iosize;
Chris Mason7f3c74f2008-07-18 12:01:11 -04002172 pg_offset += iosize;
Chris Masond1310b22008-01-24 16:13:08 -05002173 nr++;
2174 }
2175done:
2176 if (nr == 0) {
2177 /* make sure the mapping tag for page dirty gets cleared */
2178 set_page_writeback(page);
2179 end_page_writeback(page);
2180 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002181 if (unlock_start <= page_end)
2182 unlock_extent(tree, unlock_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002183 unlock_page(page);
2184 return 0;
2185}
2186
Chris Mason5e478dc2008-04-25 09:10:45 -04002187#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
Chris Masond1310b22008-01-24 16:13:08 -05002188/* Taken directly from 2.6.23 for 2.6.18 back port */
2189typedef int (*writepage_t)(struct page *page, struct writeback_control *wbc,
2190 void *data);
2191
2192/**
2193 * write_cache_pages - walk the list of dirty pages of the given address space
2194 * and write all of them.
2195 * @mapping: address space structure to write
2196 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2197 * @writepage: function called for each page
2198 * @data: data passed to writepage function
2199 *
2200 * If a page is already under I/O, write_cache_pages() skips it, even
2201 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2202 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2203 * and msync() need to guarantee that all the data which was dirty at the time
2204 * the call was made get new I/O started against them. If wbc->sync_mode is
2205 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2206 * existing IO to complete.
2207 */
2208static int write_cache_pages(struct address_space *mapping,
2209 struct writeback_control *wbc, writepage_t writepage,
2210 void *data)
2211{
2212 struct backing_dev_info *bdi = mapping->backing_dev_info;
2213 int ret = 0;
2214 int done = 0;
2215 struct pagevec pvec;
2216 int nr_pages;
2217 pgoff_t index;
2218 pgoff_t end; /* Inclusive */
2219 int scanned = 0;
2220 int range_whole = 0;
2221
2222 if (wbc->nonblocking && bdi_write_congested(bdi)) {
2223 wbc->encountered_congestion = 1;
2224 return 0;
2225 }
2226
2227 pagevec_init(&pvec, 0);
2228 if (wbc->range_cyclic) {
2229 index = mapping->writeback_index; /* Start from prev offset */
2230 end = -1;
2231 } else {
2232 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2233 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2234 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2235 range_whole = 1;
2236 scanned = 1;
2237 }
2238retry:
2239 while (!done && (index <= end) &&
2240 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
2241 PAGECACHE_TAG_DIRTY,
2242 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
2243 unsigned i;
2244
2245 scanned = 1;
2246 for (i = 0; i < nr_pages; i++) {
2247 struct page *page = pvec.pages[i];
2248
2249 /*
2250 * At this point we hold neither mapping->tree_lock nor
2251 * lock on the page itself: the page may be truncated or
2252 * invalidated (changing page->mapping to NULL), or even
2253 * swizzled back from swapper_space to tmpfs file
2254 * mapping
2255 */
2256 lock_page(page);
2257
2258 if (unlikely(page->mapping != mapping)) {
2259 unlock_page(page);
2260 continue;
2261 }
2262
2263 if (!wbc->range_cyclic && page->index > end) {
2264 done = 1;
2265 unlock_page(page);
2266 continue;
2267 }
2268
2269 if (wbc->sync_mode != WB_SYNC_NONE)
2270 wait_on_page_writeback(page);
2271
2272 if (PageWriteback(page) ||
2273 !clear_page_dirty_for_io(page)) {
2274 unlock_page(page);
2275 continue;
2276 }
2277
2278 ret = (*writepage)(page, wbc, data);
2279
2280 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2281 unlock_page(page);
2282 ret = 0;
2283 }
2284 if (ret || (--(wbc->nr_to_write) <= 0))
2285 done = 1;
2286 if (wbc->nonblocking && bdi_write_congested(bdi)) {
2287 wbc->encountered_congestion = 1;
2288 done = 1;
2289 }
2290 }
2291 pagevec_release(&pvec);
2292 cond_resched();
2293 }
2294 if (!scanned && !done) {
2295 /*
2296 * We hit the last page and there is more work to be done: wrap
2297 * back to the start of the file
2298 */
2299 scanned = 1;
2300 index = 0;
2301 goto retry;
2302 }
2303 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2304 mapping->writeback_index = index;
2305 return ret;
2306}
2307#endif
2308
2309int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2310 get_extent_t *get_extent,
2311 struct writeback_control *wbc)
2312{
2313 int ret;
2314 struct address_space *mapping = page->mapping;
2315 struct extent_page_data epd = {
2316 .bio = NULL,
2317 .tree = tree,
2318 .get_extent = get_extent,
2319 };
2320 struct writeback_control wbc_writepages = {
2321 .bdi = wbc->bdi,
2322 .sync_mode = WB_SYNC_NONE,
2323 .older_than_this = NULL,
2324 .nr_to_write = 64,
2325 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2326 .range_end = (loff_t)-1,
2327 };
2328
2329
2330 ret = __extent_writepage(page, wbc, &epd);
2331
2332 write_cache_pages(mapping, &wbc_writepages, __extent_writepage, &epd);
2333 if (epd.bio) {
Chris Masonf1885912008-04-09 16:28:12 -04002334 submit_one_bio(WRITE, epd.bio, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002335 }
2336 return ret;
2337}
2338EXPORT_SYMBOL(extent_write_full_page);
2339
2340
2341int extent_writepages(struct extent_io_tree *tree,
2342 struct address_space *mapping,
2343 get_extent_t *get_extent,
2344 struct writeback_control *wbc)
2345{
2346 int ret = 0;
2347 struct extent_page_data epd = {
2348 .bio = NULL,
2349 .tree = tree,
2350 .get_extent = get_extent,
2351 };
2352
2353 ret = write_cache_pages(mapping, wbc, __extent_writepage, &epd);
2354 if (epd.bio) {
Chris Masonf1885912008-04-09 16:28:12 -04002355 submit_one_bio(WRITE, epd.bio, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002356 }
2357 return ret;
2358}
2359EXPORT_SYMBOL(extent_writepages);
2360
2361int extent_readpages(struct extent_io_tree *tree,
2362 struct address_space *mapping,
2363 struct list_head *pages, unsigned nr_pages,
2364 get_extent_t get_extent)
2365{
2366 struct bio *bio = NULL;
2367 unsigned page_idx;
2368 struct pagevec pvec;
2369
2370 pagevec_init(&pvec, 0);
2371 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2372 struct page *page = list_entry(pages->prev, struct page, lru);
2373
2374 prefetchw(&page->flags);
2375 list_del(&page->lru);
2376 /*
2377 * what we want to do here is call add_to_page_cache_lru,
2378 * but that isn't exported, so we reproduce it here
2379 */
2380 if (!add_to_page_cache(page, mapping,
2381 page->index, GFP_KERNEL)) {
2382
2383 /* open coding of lru_cache_add, also not exported */
2384 page_cache_get(page);
2385 if (!pagevec_add(&pvec, page))
2386 __pagevec_lru_add(&pvec);
Chris Masonf1885912008-04-09 16:28:12 -04002387 __extent_read_full_page(tree, page, get_extent,
2388 &bio, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002389 }
2390 page_cache_release(page);
2391 }
2392 if (pagevec_count(&pvec))
2393 __pagevec_lru_add(&pvec);
2394 BUG_ON(!list_empty(pages));
2395 if (bio)
Chris Masonf1885912008-04-09 16:28:12 -04002396 submit_one_bio(READ, bio, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002397 return 0;
2398}
2399EXPORT_SYMBOL(extent_readpages);
2400
2401/*
2402 * basic invalidatepage code, this waits on any locked or writeback
2403 * ranges corresponding to the page, and then deletes any extent state
2404 * records from the tree
2405 */
2406int extent_invalidatepage(struct extent_io_tree *tree,
2407 struct page *page, unsigned long offset)
2408{
2409 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2410 u64 end = start + PAGE_CACHE_SIZE - 1;
2411 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2412
2413 start += (offset + blocksize -1) & ~(blocksize - 1);
2414 if (start > end)
2415 return 0;
2416
2417 lock_extent(tree, start, end, GFP_NOFS);
2418 wait_on_extent_writeback(tree, start, end);
2419 clear_extent_bit(tree, start, end,
2420 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC,
2421 1, 1, GFP_NOFS);
2422 return 0;
2423}
2424EXPORT_SYMBOL(extent_invalidatepage);
2425
2426/*
2427 * simple commit_write call, set_range_dirty is used to mark both
2428 * the pages and the extent records as dirty
2429 */
2430int extent_commit_write(struct extent_io_tree *tree,
2431 struct inode *inode, struct page *page,
2432 unsigned from, unsigned to)
2433{
2434 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2435
2436 set_page_extent_mapped(page);
2437 set_page_dirty(page);
2438
2439 if (pos > inode->i_size) {
2440 i_size_write(inode, pos);
2441 mark_inode_dirty(inode);
2442 }
2443 return 0;
2444}
2445EXPORT_SYMBOL(extent_commit_write);
2446
2447int extent_prepare_write(struct extent_io_tree *tree,
2448 struct inode *inode, struct page *page,
2449 unsigned from, unsigned to, get_extent_t *get_extent)
2450{
2451 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2452 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2453 u64 block_start;
2454 u64 orig_block_start;
2455 u64 block_end;
2456 u64 cur_end;
2457 struct extent_map *em;
2458 unsigned blocksize = 1 << inode->i_blkbits;
2459 size_t page_offset = 0;
2460 size_t block_off_start;
2461 size_t block_off_end;
2462 int err = 0;
2463 int iocount = 0;
2464 int ret = 0;
2465 int isnew;
2466
2467 set_page_extent_mapped(page);
2468
2469 block_start = (page_start + from) & ~((u64)blocksize - 1);
2470 block_end = (page_start + to - 1) | (blocksize - 1);
2471 orig_block_start = block_start;
2472
2473 lock_extent(tree, page_start, page_end, GFP_NOFS);
2474 while(block_start <= block_end) {
2475 em = get_extent(inode, page, page_offset, block_start,
2476 block_end - block_start + 1, 1);
2477 if (IS_ERR(em) || !em) {
2478 goto err;
2479 }
2480 cur_end = min(block_end, extent_map_end(em) - 1);
2481 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2482 block_off_end = block_off_start + blocksize;
2483 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2484
2485 if (!PageUptodate(page) && isnew &&
2486 (block_off_end > to || block_off_start < from)) {
2487 void *kaddr;
2488
2489 kaddr = kmap_atomic(page, KM_USER0);
2490 if (block_off_end > to)
2491 memset(kaddr + to, 0, block_off_end - to);
2492 if (block_off_start < from)
2493 memset(kaddr + block_off_start, 0,
2494 from - block_off_start);
2495 flush_dcache_page(page);
2496 kunmap_atomic(kaddr, KM_USER0);
2497 }
2498 if ((em->block_start != EXTENT_MAP_HOLE &&
2499 em->block_start != EXTENT_MAP_INLINE) &&
2500 !isnew && !PageUptodate(page) &&
2501 (block_off_end > to || block_off_start < from) &&
2502 !test_range_bit(tree, block_start, cur_end,
2503 EXTENT_UPTODATE, 1)) {
2504 u64 sector;
2505 u64 extent_offset = block_start - em->start;
2506 size_t iosize;
2507 sector = (em->block_start + extent_offset) >> 9;
2508 iosize = (cur_end - block_start + blocksize) &
2509 ~((u64)blocksize - 1);
2510 /*
2511 * we've already got the extent locked, but we
2512 * need to split the state such that our end_bio
2513 * handler can clear the lock.
2514 */
2515 set_extent_bit(tree, block_start,
2516 block_start + iosize - 1,
2517 EXTENT_LOCKED, 0, NULL, GFP_NOFS);
2518 ret = submit_extent_page(READ, tree, page,
2519 sector, iosize, page_offset, em->bdev,
2520 NULL, 1,
Chris Masonf1885912008-04-09 16:28:12 -04002521 end_bio_extent_preparewrite, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002522 iocount++;
2523 block_start = block_start + iosize;
2524 } else {
2525 set_extent_uptodate(tree, block_start, cur_end,
2526 GFP_NOFS);
2527 unlock_extent(tree, block_start, cur_end, GFP_NOFS);
2528 block_start = cur_end + 1;
2529 }
2530 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2531 free_extent_map(em);
2532 }
2533 if (iocount) {
2534 wait_extent_bit(tree, orig_block_start,
2535 block_end, EXTENT_LOCKED);
2536 }
2537 check_page_uptodate(tree, page);
2538err:
2539 /* FIXME, zero out newly allocated blocks on error */
2540 return err;
2541}
2542EXPORT_SYMBOL(extent_prepare_write);
2543
2544/*
Chris Mason7b13b7b2008-04-18 10:29:50 -04002545 * a helper for releasepage, this tests for areas of the page that
2546 * are locked or under IO and drops the related state bits if it is safe
2547 * to drop the page.
2548 */
2549int try_release_extent_state(struct extent_map_tree *map,
2550 struct extent_io_tree *tree, struct page *page,
2551 gfp_t mask)
2552{
2553 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2554 u64 end = start + PAGE_CACHE_SIZE - 1;
2555 int ret = 1;
2556
Chris Mason211f90e2008-07-18 11:56:15 -04002557 if (test_range_bit(tree, start, end,
2558 EXTENT_IOBITS | EXTENT_ORDERED, 0))
Chris Mason7b13b7b2008-04-18 10:29:50 -04002559 ret = 0;
2560 else {
2561 if ((mask & GFP_NOFS) == GFP_NOFS)
2562 mask = GFP_NOFS;
2563 clear_extent_bit(tree, start, end, EXTENT_UPTODATE,
2564 1, 1, mask);
2565 }
2566 return ret;
2567}
2568EXPORT_SYMBOL(try_release_extent_state);
2569
2570/*
Chris Masond1310b22008-01-24 16:13:08 -05002571 * a helper for releasepage. As long as there are no locked extents
2572 * in the range corresponding to the page, both state records and extent
2573 * map records are removed
2574 */
2575int try_release_extent_mapping(struct extent_map_tree *map,
Chris Mason70dec802008-01-29 09:59:12 -05002576 struct extent_io_tree *tree, struct page *page,
2577 gfp_t mask)
Chris Masond1310b22008-01-24 16:13:08 -05002578{
2579 struct extent_map *em;
2580 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2581 u64 end = start + PAGE_CACHE_SIZE - 1;
Chris Mason7b13b7b2008-04-18 10:29:50 -04002582
Chris Mason70dec802008-01-29 09:59:12 -05002583 if ((mask & __GFP_WAIT) &&
2584 page->mapping->host->i_size > 16 * 1024 * 1024) {
Yan39b56372008-02-15 10:40:50 -05002585 u64 len;
Chris Mason70dec802008-01-29 09:59:12 -05002586 while (start <= end) {
Yan39b56372008-02-15 10:40:50 -05002587 len = end - start + 1;
Chris Mason70dec802008-01-29 09:59:12 -05002588 spin_lock(&map->lock);
Yan39b56372008-02-15 10:40:50 -05002589 em = lookup_extent_mapping(map, start, len);
Chris Mason70dec802008-01-29 09:59:12 -05002590 if (!em || IS_ERR(em)) {
2591 spin_unlock(&map->lock);
2592 break;
2593 }
Chris Mason7f3c74f2008-07-18 12:01:11 -04002594 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
2595 em->start != start) {
Chris Mason70dec802008-01-29 09:59:12 -05002596 spin_unlock(&map->lock);
2597 free_extent_map(em);
2598 break;
2599 }
2600 if (!test_range_bit(tree, em->start,
2601 extent_map_end(em) - 1,
2602 EXTENT_LOCKED, 0)) {
2603 remove_extent_mapping(map, em);
2604 /* once for the rb tree */
2605 free_extent_map(em);
2606 }
2607 start = extent_map_end(em);
Chris Masond1310b22008-01-24 16:13:08 -05002608 spin_unlock(&map->lock);
Chris Mason70dec802008-01-29 09:59:12 -05002609
2610 /* once for us */
Chris Masond1310b22008-01-24 16:13:08 -05002611 free_extent_map(em);
2612 }
Chris Masond1310b22008-01-24 16:13:08 -05002613 }
Chris Mason7b13b7b2008-04-18 10:29:50 -04002614 return try_release_extent_state(map, tree, page, mask);
Chris Masond1310b22008-01-24 16:13:08 -05002615}
2616EXPORT_SYMBOL(try_release_extent_mapping);
2617
2618sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2619 get_extent_t *get_extent)
2620{
2621 struct inode *inode = mapping->host;
2622 u64 start = iblock << inode->i_blkbits;
2623 sector_t sector = 0;
2624 struct extent_map *em;
2625
2626 em = get_extent(inode, NULL, 0, start, (1 << inode->i_blkbits), 0);
2627 if (!em || IS_ERR(em))
2628 return 0;
2629
2630 if (em->block_start == EXTENT_MAP_INLINE ||
2631 em->block_start == EXTENT_MAP_HOLE)
2632 goto out;
2633
2634 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
Chris Masond1310b22008-01-24 16:13:08 -05002635out:
2636 free_extent_map(em);
2637 return sector;
2638}
2639
Chris Masond1310b22008-01-24 16:13:08 -05002640static inline struct page *extent_buffer_page(struct extent_buffer *eb,
2641 unsigned long i)
2642{
2643 struct page *p;
2644 struct address_space *mapping;
2645
2646 if (i == 0)
2647 return eb->first_page;
2648 i += eb->start >> PAGE_CACHE_SHIFT;
2649 mapping = eb->first_page->mapping;
Chris Mason33958dc2008-07-30 10:29:12 -04002650 if (!mapping)
2651 return NULL;
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002652
2653 /*
2654 * extent_buffer_page is only called after pinning the page
2655 * by increasing the reference count. So we know the page must
2656 * be in the radix tree.
2657 */
2658#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
2659 rcu_read_lock();
2660#else
Chris Masond1310b22008-01-24 16:13:08 -05002661 read_lock_irq(&mapping->tree_lock);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002662#endif
Chris Masond1310b22008-01-24 16:13:08 -05002663 p = radix_tree_lookup(&mapping->page_tree, i);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002664
2665#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
2666 rcu_read_unlock();
2667#else
Chris Masond1310b22008-01-24 16:13:08 -05002668 read_unlock_irq(&mapping->tree_lock);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002669#endif
Chris Masond1310b22008-01-24 16:13:08 -05002670 return p;
2671}
2672
Chris Mason6af118ce2008-07-22 11:18:07 -04002673static inline unsigned long num_extent_pages(u64 start, u64 len)
Chris Masonce9adaa2008-04-09 16:28:12 -04002674{
Chris Mason6af118ce2008-07-22 11:18:07 -04002675 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
2676 (start >> PAGE_CACHE_SHIFT);
Chris Mason728131d2008-04-09 16:28:12 -04002677}
2678
Chris Masond1310b22008-01-24 16:13:08 -05002679static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
2680 u64 start,
2681 unsigned long len,
2682 gfp_t mask)
2683{
2684 struct extent_buffer *eb = NULL;
Chris Mason2d2ae542008-03-26 16:24:23 -04002685 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -05002686
Chris Masond1310b22008-01-24 16:13:08 -05002687 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
Chris Masond1310b22008-01-24 16:13:08 -05002688 eb->start = start;
2689 eb->len = len;
Chris Masona61e6f22008-07-22 11:18:08 -04002690 mutex_init(&eb->mutex);
Chris Mason2d2ae542008-03-26 16:24:23 -04002691 spin_lock_irqsave(&leak_lock, flags);
2692 list_add(&eb->leak_list, &buffers);
2693 spin_unlock_irqrestore(&leak_lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -05002694 atomic_set(&eb->refs, 1);
2695
2696 return eb;
2697}
2698
2699static void __free_extent_buffer(struct extent_buffer *eb)
2700{
Chris Mason2d2ae542008-03-26 16:24:23 -04002701 unsigned long flags;
2702 spin_lock_irqsave(&leak_lock, flags);
2703 list_del(&eb->leak_list);
2704 spin_unlock_irqrestore(&leak_lock, flags);
Chris Masond1310b22008-01-24 16:13:08 -05002705 kmem_cache_free(extent_buffer_cache, eb);
2706}
2707
2708struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
2709 u64 start, unsigned long len,
2710 struct page *page0,
2711 gfp_t mask)
2712{
2713 unsigned long num_pages = num_extent_pages(start, len);
2714 unsigned long i;
2715 unsigned long index = start >> PAGE_CACHE_SHIFT;
2716 struct extent_buffer *eb;
Chris Mason6af118ce2008-07-22 11:18:07 -04002717 struct extent_buffer *exists = NULL;
Chris Masond1310b22008-01-24 16:13:08 -05002718 struct page *p;
2719 struct address_space *mapping = tree->mapping;
2720 int uptodate = 1;
2721
Chris Mason6af118ce2008-07-22 11:18:07 -04002722 spin_lock(&tree->buffer_lock);
2723 eb = buffer_search(tree, start);
2724 if (eb) {
2725 atomic_inc(&eb->refs);
2726 spin_unlock(&tree->buffer_lock);
2727 return eb;
2728 }
2729 spin_unlock(&tree->buffer_lock);
2730
Chris Masond1310b22008-01-24 16:13:08 -05002731 eb = __alloc_extent_buffer(tree, start, len, mask);
Peter2b114d12008-04-01 11:21:40 -04002732 if (!eb)
Chris Masond1310b22008-01-24 16:13:08 -05002733 return NULL;
2734
Chris Masond1310b22008-01-24 16:13:08 -05002735 if (page0) {
2736 eb->first_page = page0;
2737 i = 1;
2738 index++;
2739 page_cache_get(page0);
2740 mark_page_accessed(page0);
2741 set_page_extent_mapped(page0);
Chris Masond1310b22008-01-24 16:13:08 -05002742 set_page_extent_head(page0, len);
Chris Masonf1885912008-04-09 16:28:12 -04002743 uptodate = PageUptodate(page0);
Chris Masond1310b22008-01-24 16:13:08 -05002744 } else {
2745 i = 0;
2746 }
2747 for (; i < num_pages; i++, index++) {
2748 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
2749 if (!p) {
2750 WARN_ON(1);
Chris Mason6af118ce2008-07-22 11:18:07 -04002751 goto free_eb;
Chris Masond1310b22008-01-24 16:13:08 -05002752 }
2753 set_page_extent_mapped(p);
2754 mark_page_accessed(p);
2755 if (i == 0) {
2756 eb->first_page = p;
2757 set_page_extent_head(p, len);
2758 } else {
2759 set_page_private(p, EXTENT_PAGE_PRIVATE);
2760 }
2761 if (!PageUptodate(p))
2762 uptodate = 0;
2763 unlock_page(p);
2764 }
2765 if (uptodate)
2766 eb->flags |= EXTENT_UPTODATE;
2767 eb->flags |= EXTENT_BUFFER_FILLED;
2768
Chris Mason6af118ce2008-07-22 11:18:07 -04002769 spin_lock(&tree->buffer_lock);
2770 exists = buffer_tree_insert(tree, start, &eb->rb_node);
2771 if (exists) {
2772 /* add one reference for the caller */
2773 atomic_inc(&exists->refs);
2774 spin_unlock(&tree->buffer_lock);
2775 goto free_eb;
2776 }
2777 spin_unlock(&tree->buffer_lock);
2778
2779 /* add one reference for the tree */
2780 atomic_inc(&eb->refs);
Chris Masond1310b22008-01-24 16:13:08 -05002781 return eb;
2782
Chris Mason6af118ce2008-07-22 11:18:07 -04002783free_eb:
Chris Masond1310b22008-01-24 16:13:08 -05002784 if (!atomic_dec_and_test(&eb->refs))
Chris Mason6af118ce2008-07-22 11:18:07 -04002785 return exists;
2786 for (index = 1; index < i; index++)
Chris Masond1310b22008-01-24 16:13:08 -05002787 page_cache_release(extent_buffer_page(eb, index));
Chris Mason6af118ce2008-07-22 11:18:07 -04002788 page_cache_release(extent_buffer_page(eb, 0));
Chris Masond1310b22008-01-24 16:13:08 -05002789 __free_extent_buffer(eb);
Chris Mason6af118ce2008-07-22 11:18:07 -04002790 return exists;
Chris Masond1310b22008-01-24 16:13:08 -05002791}
2792EXPORT_SYMBOL(alloc_extent_buffer);
2793
2794struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
2795 u64 start, unsigned long len,
2796 gfp_t mask)
2797{
Chris Masond1310b22008-01-24 16:13:08 -05002798 struct extent_buffer *eb;
Chris Masond1310b22008-01-24 16:13:08 -05002799
Chris Mason6af118ce2008-07-22 11:18:07 -04002800 spin_lock(&tree->buffer_lock);
2801 eb = buffer_search(tree, start);
2802 if (eb)
2803 atomic_inc(&eb->refs);
2804 spin_unlock(&tree->buffer_lock);
Chris Masond1310b22008-01-24 16:13:08 -05002805
Chris Masond1310b22008-01-24 16:13:08 -05002806 return eb;
Chris Masond1310b22008-01-24 16:13:08 -05002807}
2808EXPORT_SYMBOL(find_extent_buffer);
2809
2810void free_extent_buffer(struct extent_buffer *eb)
2811{
Chris Masond1310b22008-01-24 16:13:08 -05002812 if (!eb)
2813 return;
2814
2815 if (!atomic_dec_and_test(&eb->refs))
2816 return;
2817
Chris Mason6af118ce2008-07-22 11:18:07 -04002818 WARN_ON(1);
Chris Masond1310b22008-01-24 16:13:08 -05002819}
2820EXPORT_SYMBOL(free_extent_buffer);
2821
2822int clear_extent_buffer_dirty(struct extent_io_tree *tree,
2823 struct extent_buffer *eb)
2824{
2825 int set;
2826 unsigned long i;
2827 unsigned long num_pages;
2828 struct page *page;
2829
2830 u64 start = eb->start;
2831 u64 end = start + eb->len - 1;
2832
2833 set = clear_extent_dirty(tree, start, end, GFP_NOFS);
2834 num_pages = num_extent_pages(eb->start, eb->len);
2835
2836 for (i = 0; i < num_pages; i++) {
2837 page = extent_buffer_page(eb, i);
Chris Masona61e6f22008-07-22 11:18:08 -04002838 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002839 if (i == 0)
2840 set_page_extent_head(page, eb->len);
2841 else
2842 set_page_private(page, EXTENT_PAGE_PRIVATE);
2843
2844 /*
2845 * if we're on the last page or the first page and the
2846 * block isn't aligned on a page boundary, do extra checks
2847 * to make sure we don't clean page that is partially dirty
2848 */
2849 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
2850 ((i == num_pages - 1) &&
2851 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
2852 start = (u64)page->index << PAGE_CACHE_SHIFT;
2853 end = start + PAGE_CACHE_SIZE - 1;
2854 if (test_range_bit(tree, start, end,
2855 EXTENT_DIRTY, 0)) {
Chris Masona61e6f22008-07-22 11:18:08 -04002856 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002857 continue;
2858 }
2859 }
2860 clear_page_dirty_for_io(page);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002861#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
2862 spin_lock_irq(&page->mapping->tree_lock);
2863#else
Chris Mason70dec802008-01-29 09:59:12 -05002864 read_lock_irq(&page->mapping->tree_lock);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002865#endif
Chris Masond1310b22008-01-24 16:13:08 -05002866 if (!PageDirty(page)) {
2867 radix_tree_tag_clear(&page->mapping->page_tree,
2868 page_index(page),
2869 PAGECACHE_TAG_DIRTY);
2870 }
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002871#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
2872 spin_unlock_irq(&page->mapping->tree_lock);
2873#else
Chris Mason70dec802008-01-29 09:59:12 -05002874 read_unlock_irq(&page->mapping->tree_lock);
Sven Wegener0ee0fda2008-07-30 16:54:26 -04002875#endif
Chris Masona61e6f22008-07-22 11:18:08 -04002876 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002877 }
2878 return 0;
2879}
2880EXPORT_SYMBOL(clear_extent_buffer_dirty);
2881
2882int wait_on_extent_buffer_writeback(struct extent_io_tree *tree,
2883 struct extent_buffer *eb)
2884{
2885 return wait_on_extent_writeback(tree, eb->start,
2886 eb->start + eb->len - 1);
2887}
2888EXPORT_SYMBOL(wait_on_extent_buffer_writeback);
2889
2890int set_extent_buffer_dirty(struct extent_io_tree *tree,
2891 struct extent_buffer *eb)
2892{
2893 unsigned long i;
2894 unsigned long num_pages;
2895
2896 num_pages = num_extent_pages(eb->start, eb->len);
2897 for (i = 0; i < num_pages; i++) {
2898 struct page *page = extent_buffer_page(eb, i);
2899 /* writepage may need to do something special for the
2900 * first page, we have to make sure page->private is
2901 * properly set. releasepage may drop page->private
2902 * on us if the page isn't already dirty.
2903 */
2904 if (i == 0) {
Chris Masona61e6f22008-07-22 11:18:08 -04002905 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002906 set_page_extent_head(page, eb->len);
2907 } else if (PagePrivate(page) &&
2908 page->private != EXTENT_PAGE_PRIVATE) {
Chris Masona61e6f22008-07-22 11:18:08 -04002909 lock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002910 set_page_extent_mapped(page);
Chris Masona61e6f22008-07-22 11:18:08 -04002911 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002912 }
2913 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
Chris Masona61e6f22008-07-22 11:18:08 -04002914 if (i == 0)
2915 unlock_page(page);
Chris Masond1310b22008-01-24 16:13:08 -05002916 }
2917 return set_extent_dirty(tree, eb->start,
2918 eb->start + eb->len - 1, GFP_NOFS);
2919}
2920EXPORT_SYMBOL(set_extent_buffer_dirty);
2921
Chris Mason1259ab72008-05-12 13:39:03 -04002922int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
2923 struct extent_buffer *eb)
2924{
2925 unsigned long i;
2926 struct page *page;
2927 unsigned long num_pages;
2928
2929 num_pages = num_extent_pages(eb->start, eb->len);
2930 eb->flags &= ~EXTENT_UPTODATE;
2931
2932 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
2933 GFP_NOFS);
2934 for (i = 0; i < num_pages; i++) {
2935 page = extent_buffer_page(eb, i);
Chris Mason33958dc2008-07-30 10:29:12 -04002936 if (page)
2937 ClearPageUptodate(page);
Chris Mason1259ab72008-05-12 13:39:03 -04002938 }
2939 return 0;
2940}
2941
Chris Masond1310b22008-01-24 16:13:08 -05002942int set_extent_buffer_uptodate(struct extent_io_tree *tree,
2943 struct extent_buffer *eb)
2944{
2945 unsigned long i;
2946 struct page *page;
2947 unsigned long num_pages;
2948
2949 num_pages = num_extent_pages(eb->start, eb->len);
2950
2951 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
2952 GFP_NOFS);
2953 for (i = 0; i < num_pages; i++) {
2954 page = extent_buffer_page(eb, i);
2955 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
2956 ((i == num_pages - 1) &&
2957 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
2958 check_page_uptodate(tree, page);
2959 continue;
2960 }
2961 SetPageUptodate(page);
2962 }
2963 return 0;
2964}
2965EXPORT_SYMBOL(set_extent_buffer_uptodate);
2966
Chris Masonce9adaa2008-04-09 16:28:12 -04002967int extent_range_uptodate(struct extent_io_tree *tree,
2968 u64 start, u64 end)
2969{
2970 struct page *page;
2971 int ret;
2972 int pg_uptodate = 1;
2973 int uptodate;
2974 unsigned long index;
2975
2976 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1);
2977 if (ret)
2978 return 1;
2979 while(start <= end) {
2980 index = start >> PAGE_CACHE_SHIFT;
2981 page = find_get_page(tree->mapping, index);
2982 uptodate = PageUptodate(page);
2983 page_cache_release(page);
2984 if (!uptodate) {
2985 pg_uptodate = 0;
2986 break;
2987 }
2988 start += PAGE_CACHE_SIZE;
2989 }
2990 return pg_uptodate;
2991}
2992
Chris Masond1310b22008-01-24 16:13:08 -05002993int extent_buffer_uptodate(struct extent_io_tree *tree,
Chris Masonce9adaa2008-04-09 16:28:12 -04002994 struct extent_buffer *eb)
Chris Masond1310b22008-01-24 16:13:08 -05002995{
Chris Mason728131d2008-04-09 16:28:12 -04002996 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04002997 unsigned long num_pages;
2998 unsigned long i;
Chris Mason728131d2008-04-09 16:28:12 -04002999 struct page *page;
3000 int pg_uptodate = 1;
3001
Chris Masond1310b22008-01-24 16:13:08 -05003002 if (eb->flags & EXTENT_UPTODATE)
Chris Mason42352982008-04-28 16:40:52 -04003003 return 1;
Chris Mason728131d2008-04-09 16:28:12 -04003004
Chris Mason42352982008-04-28 16:40:52 -04003005 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Masond1310b22008-01-24 16:13:08 -05003006 EXTENT_UPTODATE, 1);
Chris Mason42352982008-04-28 16:40:52 -04003007 if (ret)
3008 return ret;
Chris Mason728131d2008-04-09 16:28:12 -04003009
3010 num_pages = num_extent_pages(eb->start, eb->len);
3011 for (i = 0; i < num_pages; i++) {
3012 page = extent_buffer_page(eb, i);
3013 if (!PageUptodate(page)) {
3014 pg_uptodate = 0;
3015 break;
3016 }
3017 }
Chris Mason42352982008-04-28 16:40:52 -04003018 return pg_uptodate;
Chris Masond1310b22008-01-24 16:13:08 -05003019}
3020EXPORT_SYMBOL(extent_buffer_uptodate);
3021
3022int read_extent_buffer_pages(struct extent_io_tree *tree,
3023 struct extent_buffer *eb,
Chris Masona86c12c2008-02-07 10:50:54 -05003024 u64 start, int wait,
Chris Masonf1885912008-04-09 16:28:12 -04003025 get_extent_t *get_extent, int mirror_num)
Chris Masond1310b22008-01-24 16:13:08 -05003026{
3027 unsigned long i;
3028 unsigned long start_i;
3029 struct page *page;
3030 int err;
3031 int ret = 0;
Chris Masonce9adaa2008-04-09 16:28:12 -04003032 int locked_pages = 0;
3033 int all_uptodate = 1;
3034 int inc_all_pages = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003035 unsigned long num_pages;
Chris Masona86c12c2008-02-07 10:50:54 -05003036 struct bio *bio = NULL;
3037
Chris Masond1310b22008-01-24 16:13:08 -05003038 if (eb->flags & EXTENT_UPTODATE)
3039 return 0;
3040
Chris Masonce9adaa2008-04-09 16:28:12 -04003041 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
Chris Masond1310b22008-01-24 16:13:08 -05003042 EXTENT_UPTODATE, 1)) {
3043 return 0;
3044 }
3045
3046 if (start) {
3047 WARN_ON(start < eb->start);
3048 start_i = (start >> PAGE_CACHE_SHIFT) -
3049 (eb->start >> PAGE_CACHE_SHIFT);
3050 } else {
3051 start_i = 0;
3052 }
3053
3054 num_pages = num_extent_pages(eb->start, eb->len);
3055 for (i = start_i; i < num_pages; i++) {
3056 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003057 if (!wait) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003058 if (TestSetPageLocked(page))
3059 goto unlock_exit;
Chris Masond1310b22008-01-24 16:13:08 -05003060 } else {
3061 lock_page(page);
3062 }
Chris Masonce9adaa2008-04-09 16:28:12 -04003063 locked_pages++;
Chris Masond1310b22008-01-24 16:13:08 -05003064 if (!PageUptodate(page)) {
Chris Masonce9adaa2008-04-09 16:28:12 -04003065 all_uptodate = 0;
3066 }
3067 }
3068 if (all_uptodate) {
3069 if (start_i == 0)
3070 eb->flags |= EXTENT_UPTODATE;
3071 goto unlock_exit;
3072 }
3073
3074 for (i = start_i; i < num_pages; i++) {
3075 page = extent_buffer_page(eb, i);
3076 if (inc_all_pages)
3077 page_cache_get(page);
3078 if (!PageUptodate(page)) {
3079 if (start_i == 0)
3080 inc_all_pages = 1;
Chris Masonf1885912008-04-09 16:28:12 -04003081 ClearPageError(page);
Chris Masona86c12c2008-02-07 10:50:54 -05003082 err = __extent_read_full_page(tree, page,
Chris Masonf1885912008-04-09 16:28:12 -04003083 get_extent, &bio,
3084 mirror_num);
Chris Masond1310b22008-01-24 16:13:08 -05003085 if (err) {
3086 ret = err;
3087 }
3088 } else {
3089 unlock_page(page);
3090 }
3091 }
3092
Chris Masona86c12c2008-02-07 10:50:54 -05003093 if (bio)
Chris Masonf1885912008-04-09 16:28:12 -04003094 submit_one_bio(READ, bio, mirror_num);
Chris Masona86c12c2008-02-07 10:50:54 -05003095
Chris Masond1310b22008-01-24 16:13:08 -05003096 if (ret || !wait) {
3097 return ret;
3098 }
Chris Masond1310b22008-01-24 16:13:08 -05003099 for (i = start_i; i < num_pages; i++) {
3100 page = extent_buffer_page(eb, i);
3101 wait_on_page_locked(page);
3102 if (!PageUptodate(page)) {
3103 ret = -EIO;
3104 }
3105 }
3106 if (!ret)
3107 eb->flags |= EXTENT_UPTODATE;
3108 return ret;
Chris Masonce9adaa2008-04-09 16:28:12 -04003109
3110unlock_exit:
3111 i = start_i;
3112 while(locked_pages > 0) {
3113 page = extent_buffer_page(eb, i);
3114 i++;
3115 unlock_page(page);
3116 locked_pages--;
3117 }
3118 return ret;
Chris Masond1310b22008-01-24 16:13:08 -05003119}
3120EXPORT_SYMBOL(read_extent_buffer_pages);
3121
3122void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3123 unsigned long start,
3124 unsigned long len)
3125{
3126 size_t cur;
3127 size_t offset;
3128 struct page *page;
3129 char *kaddr;
3130 char *dst = (char *)dstv;
3131 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3132 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
Chris Masond1310b22008-01-24 16:13:08 -05003133
3134 WARN_ON(start > eb->len);
3135 WARN_ON(start + len > eb->start + eb->len);
3136
3137 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3138
3139 while(len > 0) {
3140 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003141
3142 cur = min(len, (PAGE_CACHE_SIZE - offset));
3143 kaddr = kmap_atomic(page, KM_USER1);
3144 memcpy(dst, kaddr + offset, cur);
3145 kunmap_atomic(kaddr, KM_USER1);
3146
3147 dst += cur;
3148 len -= cur;
3149 offset = 0;
3150 i++;
3151 }
3152}
3153EXPORT_SYMBOL(read_extent_buffer);
3154
3155int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3156 unsigned long min_len, char **token, char **map,
3157 unsigned long *map_start,
3158 unsigned long *map_len, int km)
3159{
3160 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3161 char *kaddr;
3162 struct page *p;
3163 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3164 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3165 unsigned long end_i = (start_offset + start + min_len - 1) >>
3166 PAGE_CACHE_SHIFT;
3167
3168 if (i != end_i)
3169 return -EINVAL;
3170
3171 if (i == 0) {
3172 offset = start_offset;
3173 *map_start = 0;
3174 } else {
3175 offset = 0;
3176 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3177 }
3178 if (start + min_len > eb->len) {
3179printk("bad mapping eb start %Lu len %lu, wanted %lu %lu\n", eb->start, eb->len, start, min_len);
3180 WARN_ON(1);
3181 }
3182
3183 p = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003184 kaddr = kmap_atomic(p, km);
3185 *token = kaddr;
3186 *map = kaddr + offset;
3187 *map_len = PAGE_CACHE_SIZE - offset;
3188 return 0;
3189}
3190EXPORT_SYMBOL(map_private_extent_buffer);
3191
3192int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3193 unsigned long min_len,
3194 char **token, char **map,
3195 unsigned long *map_start,
3196 unsigned long *map_len, int km)
3197{
3198 int err;
3199 int save = 0;
3200 if (eb->map_token) {
3201 unmap_extent_buffer(eb, eb->map_token, km);
3202 eb->map_token = NULL;
3203 save = 1;
3204 }
3205 err = map_private_extent_buffer(eb, start, min_len, token, map,
3206 map_start, map_len, km);
3207 if (!err && save) {
3208 eb->map_token = *token;
3209 eb->kaddr = *map;
3210 eb->map_start = *map_start;
3211 eb->map_len = *map_len;
3212 }
3213 return err;
3214}
3215EXPORT_SYMBOL(map_extent_buffer);
3216
3217void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3218{
3219 kunmap_atomic(token, km);
3220}
3221EXPORT_SYMBOL(unmap_extent_buffer);
3222
3223int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3224 unsigned long start,
3225 unsigned long len)
3226{
3227 size_t cur;
3228 size_t offset;
3229 struct page *page;
3230 char *kaddr;
3231 char *ptr = (char *)ptrv;
3232 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3233 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3234 int ret = 0;
3235
3236 WARN_ON(start > eb->len);
3237 WARN_ON(start + len > eb->start + eb->len);
3238
3239 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3240
3241 while(len > 0) {
3242 page = extent_buffer_page(eb, i);
Chris Masond1310b22008-01-24 16:13:08 -05003243
3244 cur = min(len, (PAGE_CACHE_SIZE - offset));
3245
3246 kaddr = kmap_atomic(page, KM_USER0);
3247 ret = memcmp(ptr, kaddr + offset, cur);
3248 kunmap_atomic(kaddr, KM_USER0);
3249 if (ret)
3250 break;
3251
3252 ptr += cur;
3253 len -= cur;
3254 offset = 0;
3255 i++;
3256 }
3257 return ret;
3258}
3259EXPORT_SYMBOL(memcmp_extent_buffer);
3260
3261void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3262 unsigned long start, unsigned long len)
3263{
3264 size_t cur;
3265 size_t offset;
3266 struct page *page;
3267 char *kaddr;
3268 char *src = (char *)srcv;
3269 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3270 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3271
3272 WARN_ON(start > eb->len);
3273 WARN_ON(start + len > eb->start + eb->len);
3274
3275 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3276
3277 while(len > 0) {
3278 page = extent_buffer_page(eb, i);
3279 WARN_ON(!PageUptodate(page));
3280
3281 cur = min(len, PAGE_CACHE_SIZE - offset);
3282 kaddr = kmap_atomic(page, KM_USER1);
3283 memcpy(kaddr + offset, src, cur);
3284 kunmap_atomic(kaddr, KM_USER1);
3285
3286 src += cur;
3287 len -= cur;
3288 offset = 0;
3289 i++;
3290 }
3291}
3292EXPORT_SYMBOL(write_extent_buffer);
3293
3294void memset_extent_buffer(struct extent_buffer *eb, char c,
3295 unsigned long start, unsigned long len)
3296{
3297 size_t cur;
3298 size_t offset;
3299 struct page *page;
3300 char *kaddr;
3301 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3302 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3303
3304 WARN_ON(start > eb->len);
3305 WARN_ON(start + len > eb->start + eb->len);
3306
3307 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3308
3309 while(len > 0) {
3310 page = extent_buffer_page(eb, i);
3311 WARN_ON(!PageUptodate(page));
3312
3313 cur = min(len, PAGE_CACHE_SIZE - offset);
3314 kaddr = kmap_atomic(page, KM_USER0);
3315 memset(kaddr + offset, c, cur);
3316 kunmap_atomic(kaddr, KM_USER0);
3317
3318 len -= cur;
3319 offset = 0;
3320 i++;
3321 }
3322}
3323EXPORT_SYMBOL(memset_extent_buffer);
3324
3325void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3326 unsigned long dst_offset, unsigned long src_offset,
3327 unsigned long len)
3328{
3329 u64 dst_len = dst->len;
3330 size_t cur;
3331 size_t offset;
3332 struct page *page;
3333 char *kaddr;
3334 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3335 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3336
3337 WARN_ON(src->len != dst_len);
3338
3339 offset = (start_offset + dst_offset) &
3340 ((unsigned long)PAGE_CACHE_SIZE - 1);
3341
3342 while(len > 0) {
3343 page = extent_buffer_page(dst, i);
3344 WARN_ON(!PageUptodate(page));
3345
3346 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3347
3348 kaddr = kmap_atomic(page, KM_USER0);
3349 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3350 kunmap_atomic(kaddr, KM_USER0);
3351
3352 src_offset += cur;
3353 len -= cur;
3354 offset = 0;
3355 i++;
3356 }
3357}
3358EXPORT_SYMBOL(copy_extent_buffer);
3359
3360static void move_pages(struct page *dst_page, struct page *src_page,
3361 unsigned long dst_off, unsigned long src_off,
3362 unsigned long len)
3363{
3364 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3365 if (dst_page == src_page) {
3366 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3367 } else {
3368 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3369 char *p = dst_kaddr + dst_off + len;
3370 char *s = src_kaddr + src_off + len;
3371
3372 while (len--)
3373 *--p = *--s;
3374
3375 kunmap_atomic(src_kaddr, KM_USER1);
3376 }
3377 kunmap_atomic(dst_kaddr, KM_USER0);
3378}
3379
3380static void copy_pages(struct page *dst_page, struct page *src_page,
3381 unsigned long dst_off, unsigned long src_off,
3382 unsigned long len)
3383{
3384 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3385 char *src_kaddr;
3386
3387 if (dst_page != src_page)
3388 src_kaddr = kmap_atomic(src_page, KM_USER1);
3389 else
3390 src_kaddr = dst_kaddr;
3391
3392 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3393 kunmap_atomic(dst_kaddr, KM_USER0);
3394 if (dst_page != src_page)
3395 kunmap_atomic(src_kaddr, KM_USER1);
3396}
3397
3398void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3399 unsigned long src_offset, unsigned long len)
3400{
3401 size_t cur;
3402 size_t dst_off_in_page;
3403 size_t src_off_in_page;
3404 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3405 unsigned long dst_i;
3406 unsigned long src_i;
3407
3408 if (src_offset + len > dst->len) {
3409 printk("memmove bogus src_offset %lu move len %lu len %lu\n",
3410 src_offset, len, dst->len);
3411 BUG_ON(1);
3412 }
3413 if (dst_offset + len > dst->len) {
3414 printk("memmove bogus dst_offset %lu move len %lu len %lu\n",
3415 dst_offset, len, dst->len);
3416 BUG_ON(1);
3417 }
3418
3419 while(len > 0) {
3420 dst_off_in_page = (start_offset + dst_offset) &
3421 ((unsigned long)PAGE_CACHE_SIZE - 1);
3422 src_off_in_page = (start_offset + src_offset) &
3423 ((unsigned long)PAGE_CACHE_SIZE - 1);
3424
3425 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3426 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3427
3428 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3429 src_off_in_page));
3430 cur = min_t(unsigned long, cur,
3431 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3432
3433 copy_pages(extent_buffer_page(dst, dst_i),
3434 extent_buffer_page(dst, src_i),
3435 dst_off_in_page, src_off_in_page, cur);
3436
3437 src_offset += cur;
3438 dst_offset += cur;
3439 len -= cur;
3440 }
3441}
3442EXPORT_SYMBOL(memcpy_extent_buffer);
3443
3444void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3445 unsigned long src_offset, unsigned long len)
3446{
3447 size_t cur;
3448 size_t dst_off_in_page;
3449 size_t src_off_in_page;
3450 unsigned long dst_end = dst_offset + len - 1;
3451 unsigned long src_end = src_offset + len - 1;
3452 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3453 unsigned long dst_i;
3454 unsigned long src_i;
3455
3456 if (src_offset + len > dst->len) {
3457 printk("memmove bogus src_offset %lu move len %lu len %lu\n",
3458 src_offset, len, dst->len);
3459 BUG_ON(1);
3460 }
3461 if (dst_offset + len > dst->len) {
3462 printk("memmove bogus dst_offset %lu move len %lu len %lu\n",
3463 dst_offset, len, dst->len);
3464 BUG_ON(1);
3465 }
3466 if (dst_offset < src_offset) {
3467 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
3468 return;
3469 }
3470 while(len > 0) {
3471 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
3472 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
3473
3474 dst_off_in_page = (start_offset + dst_end) &
3475 ((unsigned long)PAGE_CACHE_SIZE - 1);
3476 src_off_in_page = (start_offset + src_end) &
3477 ((unsigned long)PAGE_CACHE_SIZE - 1);
3478
3479 cur = min_t(unsigned long, len, src_off_in_page + 1);
3480 cur = min(cur, dst_off_in_page + 1);
3481 move_pages(extent_buffer_page(dst, dst_i),
3482 extent_buffer_page(dst, src_i),
3483 dst_off_in_page - cur + 1,
3484 src_off_in_page - cur + 1, cur);
3485
3486 dst_end -= cur;
3487 src_end -= cur;
3488 len -= cur;
3489 }
3490}
3491EXPORT_SYMBOL(memmove_extent_buffer);
Chris Mason6af118ce2008-07-22 11:18:07 -04003492
3493int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
3494{
3495 u64 start = page_offset(page);
3496 struct extent_buffer *eb;
3497 int ret = 1;
3498 unsigned long i;
3499 unsigned long num_pages;
3500
3501 spin_lock(&tree->buffer_lock);
3502 eb = buffer_search(tree, start);
3503 if (!eb)
3504 goto out;
3505
3506 if (atomic_read(&eb->refs) > 1) {
3507 ret = 0;
3508 goto out;
3509 }
3510 /* at this point we can safely release the extent buffer */
3511 num_pages = num_extent_pages(eb->start, eb->len);
3512 for (i = 0; i < num_pages; i++) {
3513 struct page *page = extent_buffer_page(eb, i);
3514 page_cache_release(page);
3515 }
3516 rb_erase(&eb->rb_node, &tree->buffer);
3517 __free_extent_buffer(eb);
3518out:
3519 spin_unlock(&tree->buffer_lock);
3520 return ret;
3521}
3522EXPORT_SYMBOL(try_release_extent_buffer);