ARM: 6071/1: perf-events: allow modules to query the number of hardware counters
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / btrfs / extent_io.c
blobc99121ac5d6b7f1f9f3b05b2c4b1e47e1ac06cbf
1 #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/writeback.h>
13 #include <linux/pagevec.h>
14 #include "extent_io.h"
15 #include "extent_map.h"
16 #include "compat.h"
17 #include "ctree.h"
18 #include "btrfs_inode.h"
20 static struct kmem_cache *extent_state_cache;
21 static struct kmem_cache *extent_buffer_cache;
23 static LIST_HEAD(buffers);
24 static LIST_HEAD(states);
26 #define LEAK_DEBUG 0
27 #if LEAK_DEBUG
28 static DEFINE_SPINLOCK(leak_lock);
29 #endif
31 #define BUFFER_LRU_MAX 64
33 struct tree_entry {
34 u64 start;
35 u64 end;
36 struct rb_node rb_node;
39 struct extent_page_data {
40 struct bio *bio;
41 struct extent_io_tree *tree;
42 get_extent_t *get_extent;
44 /* tells writepage not to lock the state bits for this range
45 * it still does the unlocking
47 unsigned int extent_locked:1;
49 /* tells the submit_bio code to use a WRITE_SYNC */
50 unsigned int sync_io:1;
53 int __init extent_io_init(void)
55 extent_state_cache = kmem_cache_create("extent_state",
56 sizeof(struct extent_state), 0,
57 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
58 if (!extent_state_cache)
59 return -ENOMEM;
61 extent_buffer_cache = kmem_cache_create("extent_buffers",
62 sizeof(struct extent_buffer), 0,
63 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
64 if (!extent_buffer_cache)
65 goto free_state_cache;
66 return 0;
68 free_state_cache:
69 kmem_cache_destroy(extent_state_cache);
70 return -ENOMEM;
73 void extent_io_exit(void)
75 struct extent_state *state;
76 struct extent_buffer *eb;
78 while (!list_empty(&states)) {
79 state = list_entry(states.next, struct extent_state, leak_list);
80 printk(KERN_ERR "btrfs state leak: start %llu end %llu "
81 "state %lu in tree %p refs %d\n",
82 (unsigned long long)state->start,
83 (unsigned long long)state->end,
84 state->state, state->tree, atomic_read(&state->refs));
85 list_del(&state->leak_list);
86 kmem_cache_free(extent_state_cache, state);
90 while (!list_empty(&buffers)) {
91 eb = list_entry(buffers.next, struct extent_buffer, leak_list);
92 printk(KERN_ERR "btrfs buffer leak start %llu len %lu "
93 "refs %d\n", (unsigned long long)eb->start,
94 eb->len, atomic_read(&eb->refs));
95 list_del(&eb->leak_list);
96 kmem_cache_free(extent_buffer_cache, eb);
98 if (extent_state_cache)
99 kmem_cache_destroy(extent_state_cache);
100 if (extent_buffer_cache)
101 kmem_cache_destroy(extent_buffer_cache);
104 void extent_io_tree_init(struct extent_io_tree *tree,
105 struct address_space *mapping, gfp_t mask)
107 tree->state = RB_ROOT;
108 tree->buffer = RB_ROOT;
109 tree->ops = NULL;
110 tree->dirty_bytes = 0;
111 spin_lock_init(&tree->lock);
112 spin_lock_init(&tree->buffer_lock);
113 tree->mapping = mapping;
116 static struct extent_state *alloc_extent_state(gfp_t mask)
118 struct extent_state *state;
119 #if LEAK_DEBUG
120 unsigned long flags;
121 #endif
123 state = kmem_cache_alloc(extent_state_cache, mask);
124 if (!state)
125 return state;
126 state->state = 0;
127 state->private = 0;
128 state->tree = NULL;
129 #if LEAK_DEBUG
130 spin_lock_irqsave(&leak_lock, flags);
131 list_add(&state->leak_list, &states);
132 spin_unlock_irqrestore(&leak_lock, flags);
133 #endif
134 atomic_set(&state->refs, 1);
135 init_waitqueue_head(&state->wq);
136 return state;
139 static void free_extent_state(struct extent_state *state)
141 if (!state)
142 return;
143 if (atomic_dec_and_test(&state->refs)) {
144 #if LEAK_DEBUG
145 unsigned long flags;
146 #endif
147 WARN_ON(state->tree);
148 #if LEAK_DEBUG
149 spin_lock_irqsave(&leak_lock, flags);
150 list_del(&state->leak_list);
151 spin_unlock_irqrestore(&leak_lock, flags);
152 #endif
153 kmem_cache_free(extent_state_cache, state);
157 static struct rb_node *tree_insert(struct rb_root *root, u64 offset,
158 struct rb_node *node)
160 struct rb_node **p = &root->rb_node;
161 struct rb_node *parent = NULL;
162 struct tree_entry *entry;
164 while (*p) {
165 parent = *p;
166 entry = rb_entry(parent, struct tree_entry, rb_node);
168 if (offset < entry->start)
169 p = &(*p)->rb_left;
170 else if (offset > entry->end)
171 p = &(*p)->rb_right;
172 else
173 return parent;
176 entry = rb_entry(node, struct tree_entry, rb_node);
177 rb_link_node(node, parent, p);
178 rb_insert_color(node, root);
179 return NULL;
182 static struct rb_node *__etree_search(struct extent_io_tree *tree, u64 offset,
183 struct rb_node **prev_ret,
184 struct rb_node **next_ret)
186 struct rb_root *root = &tree->state;
187 struct rb_node *n = root->rb_node;
188 struct rb_node *prev = NULL;
189 struct rb_node *orig_prev = NULL;
190 struct tree_entry *entry;
191 struct tree_entry *prev_entry = NULL;
193 while (n) {
194 entry = rb_entry(n, struct tree_entry, rb_node);
195 prev = n;
196 prev_entry = entry;
198 if (offset < entry->start)
199 n = n->rb_left;
200 else if (offset > entry->end)
201 n = n->rb_right;
202 else
203 return n;
206 if (prev_ret) {
207 orig_prev = prev;
208 while (prev && offset > prev_entry->end) {
209 prev = rb_next(prev);
210 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
212 *prev_ret = prev;
213 prev = orig_prev;
216 if (next_ret) {
217 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
218 while (prev && offset < prev_entry->start) {
219 prev = rb_prev(prev);
220 prev_entry = rb_entry(prev, struct tree_entry, rb_node);
222 *next_ret = prev;
224 return NULL;
227 static inline struct rb_node *tree_search(struct extent_io_tree *tree,
228 u64 offset)
230 struct rb_node *prev = NULL;
231 struct rb_node *ret;
233 ret = __etree_search(tree, offset, &prev, NULL);
234 if (!ret)
235 return prev;
236 return ret;
239 static struct extent_buffer *buffer_tree_insert(struct extent_io_tree *tree,
240 u64 offset, struct rb_node *node)
242 struct rb_root *root = &tree->buffer;
243 struct rb_node **p = &root->rb_node;
244 struct rb_node *parent = NULL;
245 struct extent_buffer *eb;
247 while (*p) {
248 parent = *p;
249 eb = rb_entry(parent, struct extent_buffer, rb_node);
251 if (offset < eb->start)
252 p = &(*p)->rb_left;
253 else if (offset > eb->start)
254 p = &(*p)->rb_right;
255 else
256 return eb;
259 rb_link_node(node, parent, p);
260 rb_insert_color(node, root);
261 return NULL;
264 static struct extent_buffer *buffer_search(struct extent_io_tree *tree,
265 u64 offset)
267 struct rb_root *root = &tree->buffer;
268 struct rb_node *n = root->rb_node;
269 struct extent_buffer *eb;
271 while (n) {
272 eb = rb_entry(n, struct extent_buffer, rb_node);
273 if (offset < eb->start)
274 n = n->rb_left;
275 else if (offset > eb->start)
276 n = n->rb_right;
277 else
278 return eb;
280 return NULL;
283 static void merge_cb(struct extent_io_tree *tree, struct extent_state *new,
284 struct extent_state *other)
286 if (tree->ops && tree->ops->merge_extent_hook)
287 tree->ops->merge_extent_hook(tree->mapping->host, new,
288 other);
292 * utility function to look for merge candidates inside a given range.
293 * Any extents with matching state are merged together into a single
294 * extent in the tree. Extents with EXTENT_IO in their state field
295 * are not merged because the end_io handlers need to be able to do
296 * operations on them without sleeping (or doing allocations/splits).
298 * This should be called with the tree lock held.
300 static int merge_state(struct extent_io_tree *tree,
301 struct extent_state *state)
303 struct extent_state *other;
304 struct rb_node *other_node;
306 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY))
307 return 0;
309 other_node = rb_prev(&state->rb_node);
310 if (other_node) {
311 other = rb_entry(other_node, struct extent_state, rb_node);
312 if (other->end == state->start - 1 &&
313 other->state == state->state) {
314 merge_cb(tree, state, other);
315 state->start = other->start;
316 other->tree = NULL;
317 rb_erase(&other->rb_node, &tree->state);
318 free_extent_state(other);
321 other_node = rb_next(&state->rb_node);
322 if (other_node) {
323 other = rb_entry(other_node, struct extent_state, rb_node);
324 if (other->start == state->end + 1 &&
325 other->state == state->state) {
326 merge_cb(tree, state, other);
327 other->start = state->start;
328 state->tree = NULL;
329 rb_erase(&state->rb_node, &tree->state);
330 free_extent_state(state);
331 state = NULL;
335 return 0;
338 static int set_state_cb(struct extent_io_tree *tree,
339 struct extent_state *state,
340 unsigned long bits)
342 if (tree->ops && tree->ops->set_bit_hook) {
343 return tree->ops->set_bit_hook(tree->mapping->host,
344 state->start, state->end,
345 state->state, bits);
348 return 0;
351 static void clear_state_cb(struct extent_io_tree *tree,
352 struct extent_state *state,
353 unsigned long bits)
355 if (tree->ops && tree->ops->clear_bit_hook)
356 tree->ops->clear_bit_hook(tree->mapping->host, state, bits);
360 * insert an extent_state struct into the tree. 'bits' are set on the
361 * struct before it is inserted.
363 * This may return -EEXIST if the extent is already there, in which case the
364 * state struct is freed.
366 * The tree lock is not taken internally. This is a utility function and
367 * probably isn't what you want to call (see set/clear_extent_bit).
369 static int insert_state(struct extent_io_tree *tree,
370 struct extent_state *state, u64 start, u64 end,
371 int bits)
373 struct rb_node *node;
374 int ret;
376 if (end < start) {
377 printk(KERN_ERR "btrfs end < start %llu %llu\n",
378 (unsigned long long)end,
379 (unsigned long long)start);
380 WARN_ON(1);
382 state->start = start;
383 state->end = end;
384 ret = set_state_cb(tree, state, bits);
385 if (ret)
386 return ret;
388 if (bits & EXTENT_DIRTY)
389 tree->dirty_bytes += end - start + 1;
390 state->state |= bits;
391 node = tree_insert(&tree->state, end, &state->rb_node);
392 if (node) {
393 struct extent_state *found;
394 found = rb_entry(node, struct extent_state, rb_node);
395 printk(KERN_ERR "btrfs found node %llu %llu on insert of "
396 "%llu %llu\n", (unsigned long long)found->start,
397 (unsigned long long)found->end,
398 (unsigned long long)start, (unsigned long long)end);
399 free_extent_state(state);
400 return -EEXIST;
402 state->tree = tree;
403 merge_state(tree, state);
404 return 0;
407 static int split_cb(struct extent_io_tree *tree, struct extent_state *orig,
408 u64 split)
410 if (tree->ops && tree->ops->split_extent_hook)
411 return tree->ops->split_extent_hook(tree->mapping->host,
412 orig, split);
413 return 0;
417 * split a given extent state struct in two, inserting the preallocated
418 * struct 'prealloc' as the newly created second half. 'split' indicates an
419 * offset inside 'orig' where it should be split.
421 * Before calling,
422 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
423 * are two extent state structs in the tree:
424 * prealloc: [orig->start, split - 1]
425 * orig: [ split, orig->end ]
427 * The tree locks are not taken by this function. They need to be held
428 * by the caller.
430 static int split_state(struct extent_io_tree *tree, struct extent_state *orig,
431 struct extent_state *prealloc, u64 split)
433 struct rb_node *node;
435 split_cb(tree, orig, split);
437 prealloc->start = orig->start;
438 prealloc->end = split - 1;
439 prealloc->state = orig->state;
440 orig->start = split;
442 node = tree_insert(&tree->state, prealloc->end, &prealloc->rb_node);
443 if (node) {
444 free_extent_state(prealloc);
445 return -EEXIST;
447 prealloc->tree = tree;
448 return 0;
452 * utility function to clear some bits in an extent state struct.
453 * it will optionally wake up any one waiting on this state (wake == 1), or
454 * forcibly remove the state from the tree (delete == 1).
456 * If no bits are set on the state struct after clearing things, the
457 * struct is freed and removed from the tree
459 static int clear_state_bit(struct extent_io_tree *tree,
460 struct extent_state *state, int bits, int wake,
461 int delete)
463 int bits_to_clear = bits & ~EXTENT_DO_ACCOUNTING;
464 int ret = state->state & bits_to_clear;
466 if ((bits & EXTENT_DIRTY) && (state->state & EXTENT_DIRTY)) {
467 u64 range = state->end - state->start + 1;
468 WARN_ON(range > tree->dirty_bytes);
469 tree->dirty_bytes -= range;
471 clear_state_cb(tree, state, bits);
472 state->state &= ~bits_to_clear;
473 if (wake)
474 wake_up(&state->wq);
475 if (delete || state->state == 0) {
476 if (state->tree) {
477 clear_state_cb(tree, state, state->state);
478 rb_erase(&state->rb_node, &tree->state);
479 state->tree = NULL;
480 free_extent_state(state);
481 } else {
482 WARN_ON(1);
484 } else {
485 merge_state(tree, state);
487 return ret;
491 * clear some bits on a range in the tree. This may require splitting
492 * or inserting elements in the tree, so the gfp mask is used to
493 * indicate which allocations or sleeping are allowed.
495 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
496 * the given range from the tree regardless of state (ie for truncate).
498 * the range [start, end] is inclusive.
500 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
501 * bits were already set, or zero if none of the bits were already set.
503 int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
504 int bits, int wake, int delete,
505 struct extent_state **cached_state,
506 gfp_t mask)
508 struct extent_state *state;
509 struct extent_state *cached;
510 struct extent_state *prealloc = NULL;
511 struct rb_node *next_node;
512 struct rb_node *node;
513 u64 last_end;
514 int err;
515 int set = 0;
516 int clear = 0;
518 if (bits & (EXTENT_IOBITS | EXTENT_BOUNDARY))
519 clear = 1;
520 again:
521 if (!prealloc && (mask & __GFP_WAIT)) {
522 prealloc = alloc_extent_state(mask);
523 if (!prealloc)
524 return -ENOMEM;
527 spin_lock(&tree->lock);
528 if (cached_state) {
529 cached = *cached_state;
531 if (clear) {
532 *cached_state = NULL;
533 cached_state = NULL;
536 if (cached && cached->tree && cached->start == start) {
537 if (clear)
538 atomic_dec(&cached->refs);
539 state = cached;
540 goto hit_next;
542 if (clear)
543 free_extent_state(cached);
546 * this search will find the extents that end after
547 * our range starts
549 node = tree_search(tree, start);
550 if (!node)
551 goto out;
552 state = rb_entry(node, struct extent_state, rb_node);
553 hit_next:
554 if (state->start > end)
555 goto out;
556 WARN_ON(state->end < start);
557 last_end = state->end;
560 * | ---- desired range ---- |
561 * | state | or
562 * | ------------- state -------------- |
564 * We need to split the extent we found, and may flip
565 * bits on second half.
567 * If the extent we found extends past our range, we
568 * just split and search again. It'll get split again
569 * the next time though.
571 * If the extent we found is inside our range, we clear
572 * the desired bit on it.
575 if (state->start < start) {
576 if (!prealloc)
577 prealloc = alloc_extent_state(GFP_ATOMIC);
578 err = split_state(tree, state, prealloc, start);
579 BUG_ON(err == -EEXIST);
580 prealloc = NULL;
581 if (err)
582 goto out;
583 if (state->end <= end) {
584 set |= clear_state_bit(tree, state, bits, wake,
585 delete);
586 if (last_end == (u64)-1)
587 goto out;
588 start = last_end + 1;
590 goto search_again;
593 * | ---- desired range ---- |
594 * | state |
595 * We need to split the extent, and clear the bit
596 * on the first half
598 if (state->start <= end && state->end > end) {
599 if (!prealloc)
600 prealloc = alloc_extent_state(GFP_ATOMIC);
601 err = split_state(tree, state, prealloc, end + 1);
602 BUG_ON(err == -EEXIST);
603 if (wake)
604 wake_up(&state->wq);
606 set |= clear_state_bit(tree, prealloc, bits, wake, delete);
608 prealloc = NULL;
609 goto out;
612 if (state->end < end && prealloc && !need_resched())
613 next_node = rb_next(&state->rb_node);
614 else
615 next_node = NULL;
617 set |= clear_state_bit(tree, state, bits, wake, delete);
618 if (last_end == (u64)-1)
619 goto out;
620 start = last_end + 1;
621 if (start <= end && next_node) {
622 state = rb_entry(next_node, struct extent_state,
623 rb_node);
624 if (state->start == start)
625 goto hit_next;
627 goto search_again;
629 out:
630 spin_unlock(&tree->lock);
631 if (prealloc)
632 free_extent_state(prealloc);
634 return set;
636 search_again:
637 if (start > end)
638 goto out;
639 spin_unlock(&tree->lock);
640 if (mask & __GFP_WAIT)
641 cond_resched();
642 goto again;
645 static int wait_on_state(struct extent_io_tree *tree,
646 struct extent_state *state)
647 __releases(tree->lock)
648 __acquires(tree->lock)
650 DEFINE_WAIT(wait);
651 prepare_to_wait(&state->wq, &wait, TASK_UNINTERRUPTIBLE);
652 spin_unlock(&tree->lock);
653 schedule();
654 spin_lock(&tree->lock);
655 finish_wait(&state->wq, &wait);
656 return 0;
660 * waits for one or more bits to clear on a range in the state tree.
661 * The range [start, end] is inclusive.
662 * The tree lock is taken by this function
664 int wait_extent_bit(struct extent_io_tree *tree, u64 start, u64 end, int bits)
666 struct extent_state *state;
667 struct rb_node *node;
669 spin_lock(&tree->lock);
670 again:
671 while (1) {
673 * this search will find all the extents that end after
674 * our range starts
676 node = tree_search(tree, start);
677 if (!node)
678 break;
680 state = rb_entry(node, struct extent_state, rb_node);
682 if (state->start > end)
683 goto out;
685 if (state->state & bits) {
686 start = state->start;
687 atomic_inc(&state->refs);
688 wait_on_state(tree, state);
689 free_extent_state(state);
690 goto again;
692 start = state->end + 1;
694 if (start > end)
695 break;
697 if (need_resched()) {
698 spin_unlock(&tree->lock);
699 cond_resched();
700 spin_lock(&tree->lock);
703 out:
704 spin_unlock(&tree->lock);
705 return 0;
708 static int set_state_bits(struct extent_io_tree *tree,
709 struct extent_state *state,
710 int bits)
712 int ret;
714 ret = set_state_cb(tree, state, bits);
715 if (ret)
716 return ret;
718 if ((bits & EXTENT_DIRTY) && !(state->state & EXTENT_DIRTY)) {
719 u64 range = state->end - state->start + 1;
720 tree->dirty_bytes += range;
722 state->state |= bits;
724 return 0;
727 static void cache_state(struct extent_state *state,
728 struct extent_state **cached_ptr)
730 if (cached_ptr && !(*cached_ptr)) {
731 if (state->state & (EXTENT_IOBITS | EXTENT_BOUNDARY)) {
732 *cached_ptr = state;
733 atomic_inc(&state->refs);
739 * set some bits on a range in the tree. This may require allocations or
740 * sleeping, so the gfp mask is used to indicate what is allowed.
742 * If any of the exclusive bits are set, this will fail with -EEXIST if some
743 * part of the range already has the desired bits set. The start of the
744 * existing range is returned in failed_start in this case.
746 * [start, end] is inclusive This takes the tree lock.
749 static int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
750 int bits, int exclusive_bits, u64 *failed_start,
751 struct extent_state **cached_state,
752 gfp_t mask)
754 struct extent_state *state;
755 struct extent_state *prealloc = NULL;
756 struct rb_node *node;
757 int err = 0;
758 u64 last_start;
759 u64 last_end;
761 again:
762 if (!prealloc && (mask & __GFP_WAIT)) {
763 prealloc = alloc_extent_state(mask);
764 if (!prealloc)
765 return -ENOMEM;
768 spin_lock(&tree->lock);
769 if (cached_state && *cached_state) {
770 state = *cached_state;
771 if (state->start == start && state->tree) {
772 node = &state->rb_node;
773 goto hit_next;
777 * this search will find all the extents that end after
778 * our range starts.
780 node = tree_search(tree, start);
781 if (!node) {
782 err = insert_state(tree, prealloc, start, end, bits);
783 prealloc = NULL;
784 BUG_ON(err == -EEXIST);
785 goto out;
787 state = rb_entry(node, struct extent_state, rb_node);
788 hit_next:
789 last_start = state->start;
790 last_end = state->end;
793 * | ---- desired range ---- |
794 * | state |
796 * Just lock what we found and keep going
798 if (state->start == start && state->end <= end) {
799 struct rb_node *next_node;
800 if (state->state & exclusive_bits) {
801 *failed_start = state->start;
802 err = -EEXIST;
803 goto out;
806 err = set_state_bits(tree, state, bits);
807 if (err)
808 goto out;
810 cache_state(state, cached_state);
811 merge_state(tree, state);
812 if (last_end == (u64)-1)
813 goto out;
815 start = last_end + 1;
816 if (start < end && prealloc && !need_resched()) {
817 next_node = rb_next(node);
818 if (next_node) {
819 state = rb_entry(next_node, struct extent_state,
820 rb_node);
821 if (state->start == start)
822 goto hit_next;
825 goto search_again;
829 * | ---- desired range ---- |
830 * | state |
831 * or
832 * | ------------- state -------------- |
834 * We need to split the extent we found, and may flip bits on
835 * second half.
837 * If the extent we found extends past our
838 * range, we just split and search again. It'll get split
839 * again the next time though.
841 * If the extent we found is inside our range, we set the
842 * desired bit on it.
844 if (state->start < start) {
845 if (state->state & exclusive_bits) {
846 *failed_start = start;
847 err = -EEXIST;
848 goto out;
850 err = split_state(tree, state, prealloc, start);
851 BUG_ON(err == -EEXIST);
852 prealloc = NULL;
853 if (err)
854 goto out;
855 if (state->end <= end) {
856 err = set_state_bits(tree, state, bits);
857 if (err)
858 goto out;
859 cache_state(state, cached_state);
860 merge_state(tree, state);
861 if (last_end == (u64)-1)
862 goto out;
863 start = last_end + 1;
865 goto search_again;
868 * | ---- desired range ---- |
869 * | state | or | state |
871 * There's a hole, we need to insert something in it and
872 * ignore the extent we found.
874 if (state->start > start) {
875 u64 this_end;
876 if (end < last_start)
877 this_end = end;
878 else
879 this_end = last_start - 1;
880 err = insert_state(tree, prealloc, start, this_end,
881 bits);
882 BUG_ON(err == -EEXIST);
883 if (err) {
884 prealloc = NULL;
885 goto out;
887 cache_state(prealloc, cached_state);
888 prealloc = NULL;
889 start = this_end + 1;
890 goto search_again;
893 * | ---- desired range ---- |
894 * | state |
895 * We need to split the extent, and set the bit
896 * on the first half
898 if (state->start <= end && state->end > end) {
899 if (state->state & exclusive_bits) {
900 *failed_start = start;
901 err = -EEXIST;
902 goto out;
904 err = split_state(tree, state, prealloc, end + 1);
905 BUG_ON(err == -EEXIST);
907 err = set_state_bits(tree, prealloc, bits);
908 if (err) {
909 prealloc = NULL;
910 goto out;
912 cache_state(prealloc, cached_state);
913 merge_state(tree, prealloc);
914 prealloc = NULL;
915 goto out;
918 goto search_again;
920 out:
921 spin_unlock(&tree->lock);
922 if (prealloc)
923 free_extent_state(prealloc);
925 return err;
927 search_again:
928 if (start > end)
929 goto out;
930 spin_unlock(&tree->lock);
931 if (mask & __GFP_WAIT)
932 cond_resched();
933 goto again;
936 /* wrappers around set/clear extent bit */
937 int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
938 gfp_t mask)
940 return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL,
941 NULL, mask);
944 int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
945 int bits, gfp_t mask)
947 return set_extent_bit(tree, start, end, bits, 0, NULL,
948 NULL, mask);
951 int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
952 int bits, gfp_t mask)
954 return clear_extent_bit(tree, start, end, bits, 0, 0, NULL, mask);
957 int set_extent_delalloc(struct extent_io_tree *tree, u64 start, u64 end,
958 struct extent_state **cached_state, gfp_t mask)
960 return set_extent_bit(tree, start, end,
961 EXTENT_DELALLOC | EXTENT_DIRTY | EXTENT_UPTODATE,
962 0, NULL, cached_state, mask);
965 int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
966 gfp_t mask)
968 return clear_extent_bit(tree, start, end,
969 EXTENT_DIRTY | EXTENT_DELALLOC |
970 EXTENT_DO_ACCOUNTING, 0, 0,
971 NULL, mask);
974 int set_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
975 gfp_t mask)
977 return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL,
978 NULL, mask);
981 static int clear_extent_new(struct extent_io_tree *tree, u64 start, u64 end,
982 gfp_t mask)
984 return clear_extent_bit(tree, start, end, EXTENT_NEW, 0, 0,
985 NULL, mask);
988 int set_extent_uptodate(struct extent_io_tree *tree, u64 start, u64 end,
989 gfp_t mask)
991 return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL,
992 NULL, mask);
995 static int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
996 u64 end, struct extent_state **cached_state,
997 gfp_t mask)
999 return clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
1000 cached_state, mask);
1003 int wait_on_extent_writeback(struct extent_io_tree *tree, u64 start, u64 end)
1005 return wait_extent_bit(tree, start, end, EXTENT_WRITEBACK);
1009 * either insert or lock state struct between start and end use mask to tell
1010 * us if waiting is desired.
1012 int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
1013 int bits, struct extent_state **cached_state, gfp_t mask)
1015 int err;
1016 u64 failed_start;
1017 while (1) {
1018 err = set_extent_bit(tree, start, end, EXTENT_LOCKED | bits,
1019 EXTENT_LOCKED, &failed_start,
1020 cached_state, mask);
1021 if (err == -EEXIST && (mask & __GFP_WAIT)) {
1022 wait_extent_bit(tree, failed_start, end, EXTENT_LOCKED);
1023 start = failed_start;
1024 } else {
1025 break;
1027 WARN_ON(start > end);
1029 return err;
1032 int lock_extent(struct extent_io_tree *tree, u64 start, u64 end, gfp_t mask)
1034 return lock_extent_bits(tree, start, end, 0, NULL, mask);
1037 int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1038 gfp_t mask)
1040 int err;
1041 u64 failed_start;
1043 err = set_extent_bit(tree, start, end, EXTENT_LOCKED, EXTENT_LOCKED,
1044 &failed_start, NULL, mask);
1045 if (err == -EEXIST) {
1046 if (failed_start > start)
1047 clear_extent_bit(tree, start, failed_start - 1,
1048 EXTENT_LOCKED, 1, 0, NULL, mask);
1049 return 0;
1051 return 1;
1054 int unlock_extent_cached(struct extent_io_tree *tree, u64 start, u64 end,
1055 struct extent_state **cached, gfp_t mask)
1057 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
1058 mask);
1061 int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end,
1062 gfp_t mask)
1064 return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL,
1065 mask);
1069 * helper function to set pages and extents in the tree dirty
1071 int set_range_dirty(struct extent_io_tree *tree, u64 start, u64 end)
1073 unsigned long index = start >> PAGE_CACHE_SHIFT;
1074 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1075 struct page *page;
1077 while (index <= end_index) {
1078 page = find_get_page(tree->mapping, index);
1079 BUG_ON(!page);
1080 __set_page_dirty_nobuffers(page);
1081 page_cache_release(page);
1082 index++;
1084 return 0;
1088 * helper function to set both pages and extents in the tree writeback
1090 static int set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
1092 unsigned long index = start >> PAGE_CACHE_SHIFT;
1093 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1094 struct page *page;
1096 while (index <= end_index) {
1097 page = find_get_page(tree->mapping, index);
1098 BUG_ON(!page);
1099 set_page_writeback(page);
1100 page_cache_release(page);
1101 index++;
1103 return 0;
1107 * find the first offset in the io tree with 'bits' set. zero is
1108 * returned if we find something, and *start_ret and *end_ret are
1109 * set to reflect the state struct that was found.
1111 * If nothing was found, 1 is returned, < 0 on error
1113 int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
1114 u64 *start_ret, u64 *end_ret, int bits)
1116 struct rb_node *node;
1117 struct extent_state *state;
1118 int ret = 1;
1120 spin_lock(&tree->lock);
1122 * this search will find all the extents that end after
1123 * our range starts.
1125 node = tree_search(tree, start);
1126 if (!node)
1127 goto out;
1129 while (1) {
1130 state = rb_entry(node, struct extent_state, rb_node);
1131 if (state->end >= start && (state->state & bits)) {
1132 *start_ret = state->start;
1133 *end_ret = state->end;
1134 ret = 0;
1135 break;
1137 node = rb_next(node);
1138 if (!node)
1139 break;
1141 out:
1142 spin_unlock(&tree->lock);
1143 return ret;
1146 /* find the first state struct with 'bits' set after 'start', and
1147 * return it. tree->lock must be held. NULL will returned if
1148 * nothing was found after 'start'
1150 struct extent_state *find_first_extent_bit_state(struct extent_io_tree *tree,
1151 u64 start, int bits)
1153 struct rb_node *node;
1154 struct extent_state *state;
1157 * this search will find all the extents that end after
1158 * our range starts.
1160 node = tree_search(tree, start);
1161 if (!node)
1162 goto out;
1164 while (1) {
1165 state = rb_entry(node, struct extent_state, rb_node);
1166 if (state->end >= start && (state->state & bits))
1167 return state;
1169 node = rb_next(node);
1170 if (!node)
1171 break;
1173 out:
1174 return NULL;
1178 * find a contiguous range of bytes in the file marked as delalloc, not
1179 * more than 'max_bytes'. start and end are used to return the range,
1181 * 1 is returned if we find something, 0 if nothing was in the tree
1183 static noinline u64 find_delalloc_range(struct extent_io_tree *tree,
1184 u64 *start, u64 *end, u64 max_bytes,
1185 struct extent_state **cached_state)
1187 struct rb_node *node;
1188 struct extent_state *state;
1189 u64 cur_start = *start;
1190 u64 found = 0;
1191 u64 total_bytes = 0;
1193 spin_lock(&tree->lock);
1196 * this search will find all the extents that end after
1197 * our range starts.
1199 node = tree_search(tree, cur_start);
1200 if (!node) {
1201 if (!found)
1202 *end = (u64)-1;
1203 goto out;
1206 while (1) {
1207 state = rb_entry(node, struct extent_state, rb_node);
1208 if (found && (state->start != cur_start ||
1209 (state->state & EXTENT_BOUNDARY))) {
1210 goto out;
1212 if (!(state->state & EXTENT_DELALLOC)) {
1213 if (!found)
1214 *end = state->end;
1215 goto out;
1217 if (!found) {
1218 *start = state->start;
1219 *cached_state = state;
1220 atomic_inc(&state->refs);
1222 found++;
1223 *end = state->end;
1224 cur_start = state->end + 1;
1225 node = rb_next(node);
1226 if (!node)
1227 break;
1228 total_bytes += state->end - state->start + 1;
1229 if (total_bytes >= max_bytes)
1230 break;
1232 out:
1233 spin_unlock(&tree->lock);
1234 return found;
1237 static noinline int __unlock_for_delalloc(struct inode *inode,
1238 struct page *locked_page,
1239 u64 start, u64 end)
1241 int ret;
1242 struct page *pages[16];
1243 unsigned long index = start >> PAGE_CACHE_SHIFT;
1244 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1245 unsigned long nr_pages = end_index - index + 1;
1246 int i;
1248 if (index == locked_page->index && end_index == index)
1249 return 0;
1251 while (nr_pages > 0) {
1252 ret = find_get_pages_contig(inode->i_mapping, index,
1253 min_t(unsigned long, nr_pages,
1254 ARRAY_SIZE(pages)), pages);
1255 for (i = 0; i < ret; i++) {
1256 if (pages[i] != locked_page)
1257 unlock_page(pages[i]);
1258 page_cache_release(pages[i]);
1260 nr_pages -= ret;
1261 index += ret;
1262 cond_resched();
1264 return 0;
1267 static noinline int lock_delalloc_pages(struct inode *inode,
1268 struct page *locked_page,
1269 u64 delalloc_start,
1270 u64 delalloc_end)
1272 unsigned long index = delalloc_start >> PAGE_CACHE_SHIFT;
1273 unsigned long start_index = index;
1274 unsigned long end_index = delalloc_end >> PAGE_CACHE_SHIFT;
1275 unsigned long pages_locked = 0;
1276 struct page *pages[16];
1277 unsigned long nrpages;
1278 int ret;
1279 int i;
1281 /* the caller is responsible for locking the start index */
1282 if (index == locked_page->index && index == end_index)
1283 return 0;
1285 /* skip the page at the start index */
1286 nrpages = end_index - index + 1;
1287 while (nrpages > 0) {
1288 ret = find_get_pages_contig(inode->i_mapping, index,
1289 min_t(unsigned long,
1290 nrpages, ARRAY_SIZE(pages)), pages);
1291 if (ret == 0) {
1292 ret = -EAGAIN;
1293 goto done;
1295 /* now we have an array of pages, lock them all */
1296 for (i = 0; i < ret; i++) {
1298 * the caller is taking responsibility for
1299 * locked_page
1301 if (pages[i] != locked_page) {
1302 lock_page(pages[i]);
1303 if (!PageDirty(pages[i]) ||
1304 pages[i]->mapping != inode->i_mapping) {
1305 ret = -EAGAIN;
1306 unlock_page(pages[i]);
1307 page_cache_release(pages[i]);
1308 goto done;
1311 page_cache_release(pages[i]);
1312 pages_locked++;
1314 nrpages -= ret;
1315 index += ret;
1316 cond_resched();
1318 ret = 0;
1319 done:
1320 if (ret && pages_locked) {
1321 __unlock_for_delalloc(inode, locked_page,
1322 delalloc_start,
1323 ((u64)(start_index + pages_locked - 1)) <<
1324 PAGE_CACHE_SHIFT);
1326 return ret;
1330 * find a contiguous range of bytes in the file marked as delalloc, not
1331 * more than 'max_bytes'. start and end are used to return the range,
1333 * 1 is returned if we find something, 0 if nothing was in the tree
1335 static noinline u64 find_lock_delalloc_range(struct inode *inode,
1336 struct extent_io_tree *tree,
1337 struct page *locked_page,
1338 u64 *start, u64 *end,
1339 u64 max_bytes)
1341 u64 delalloc_start;
1342 u64 delalloc_end;
1343 u64 found;
1344 struct extent_state *cached_state = NULL;
1345 int ret;
1346 int loops = 0;
1348 again:
1349 /* step one, find a bunch of delalloc bytes starting at start */
1350 delalloc_start = *start;
1351 delalloc_end = 0;
1352 found = find_delalloc_range(tree, &delalloc_start, &delalloc_end,
1353 max_bytes, &cached_state);
1354 if (!found || delalloc_end <= *start) {
1355 *start = delalloc_start;
1356 *end = delalloc_end;
1357 free_extent_state(cached_state);
1358 return found;
1362 * start comes from the offset of locked_page. We have to lock
1363 * pages in order, so we can't process delalloc bytes before
1364 * locked_page
1366 if (delalloc_start < *start)
1367 delalloc_start = *start;
1370 * make sure to limit the number of pages we try to lock down
1371 * if we're looping.
1373 if (delalloc_end + 1 - delalloc_start > max_bytes && loops)
1374 delalloc_end = delalloc_start + PAGE_CACHE_SIZE - 1;
1376 /* step two, lock all the pages after the page that has start */
1377 ret = lock_delalloc_pages(inode, locked_page,
1378 delalloc_start, delalloc_end);
1379 if (ret == -EAGAIN) {
1380 /* some of the pages are gone, lets avoid looping by
1381 * shortening the size of the delalloc range we're searching
1383 free_extent_state(cached_state);
1384 if (!loops) {
1385 unsigned long offset = (*start) & (PAGE_CACHE_SIZE - 1);
1386 max_bytes = PAGE_CACHE_SIZE - offset;
1387 loops = 1;
1388 goto again;
1389 } else {
1390 found = 0;
1391 goto out_failed;
1394 BUG_ON(ret);
1396 /* step three, lock the state bits for the whole range */
1397 lock_extent_bits(tree, delalloc_start, delalloc_end,
1398 0, &cached_state, GFP_NOFS);
1400 /* then test to make sure it is all still delalloc */
1401 ret = test_range_bit(tree, delalloc_start, delalloc_end,
1402 EXTENT_DELALLOC, 1, cached_state);
1403 if (!ret) {
1404 unlock_extent_cached(tree, delalloc_start, delalloc_end,
1405 &cached_state, GFP_NOFS);
1406 __unlock_for_delalloc(inode, locked_page,
1407 delalloc_start, delalloc_end);
1408 cond_resched();
1409 goto again;
1411 free_extent_state(cached_state);
1412 *start = delalloc_start;
1413 *end = delalloc_end;
1414 out_failed:
1415 return found;
1418 int extent_clear_unlock_delalloc(struct inode *inode,
1419 struct extent_io_tree *tree,
1420 u64 start, u64 end, struct page *locked_page,
1421 unsigned long op)
1423 int ret;
1424 struct page *pages[16];
1425 unsigned long index = start >> PAGE_CACHE_SHIFT;
1426 unsigned long end_index = end >> PAGE_CACHE_SHIFT;
1427 unsigned long nr_pages = end_index - index + 1;
1428 int i;
1429 int clear_bits = 0;
1431 if (op & EXTENT_CLEAR_UNLOCK)
1432 clear_bits |= EXTENT_LOCKED;
1433 if (op & EXTENT_CLEAR_DIRTY)
1434 clear_bits |= EXTENT_DIRTY;
1436 if (op & EXTENT_CLEAR_DELALLOC)
1437 clear_bits |= EXTENT_DELALLOC;
1439 if (op & EXTENT_CLEAR_ACCOUNTING)
1440 clear_bits |= EXTENT_DO_ACCOUNTING;
1442 clear_extent_bit(tree, start, end, clear_bits, 1, 0, NULL, GFP_NOFS);
1443 if (!(op & (EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
1444 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK |
1445 EXTENT_SET_PRIVATE2)))
1446 return 0;
1448 while (nr_pages > 0) {
1449 ret = find_get_pages_contig(inode->i_mapping, index,
1450 min_t(unsigned long,
1451 nr_pages, ARRAY_SIZE(pages)), pages);
1452 for (i = 0; i < ret; i++) {
1454 if (op & EXTENT_SET_PRIVATE2)
1455 SetPagePrivate2(pages[i]);
1457 if (pages[i] == locked_page) {
1458 page_cache_release(pages[i]);
1459 continue;
1461 if (op & EXTENT_CLEAR_DIRTY)
1462 clear_page_dirty_for_io(pages[i]);
1463 if (op & EXTENT_SET_WRITEBACK)
1464 set_page_writeback(pages[i]);
1465 if (op & EXTENT_END_WRITEBACK)
1466 end_page_writeback(pages[i]);
1467 if (op & EXTENT_CLEAR_UNLOCK_PAGE)
1468 unlock_page(pages[i]);
1469 page_cache_release(pages[i]);
1471 nr_pages -= ret;
1472 index += ret;
1473 cond_resched();
1475 return 0;
1479 * count the number of bytes in the tree that have a given bit(s)
1480 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1481 * cached. The total number found is returned.
1483 u64 count_range_bits(struct extent_io_tree *tree,
1484 u64 *start, u64 search_end, u64 max_bytes,
1485 unsigned long bits)
1487 struct rb_node *node;
1488 struct extent_state *state;
1489 u64 cur_start = *start;
1490 u64 total_bytes = 0;
1491 int found = 0;
1493 if (search_end <= cur_start) {
1494 WARN_ON(1);
1495 return 0;
1498 spin_lock(&tree->lock);
1499 if (cur_start == 0 && bits == EXTENT_DIRTY) {
1500 total_bytes = tree->dirty_bytes;
1501 goto out;
1504 * this search will find all the extents that end after
1505 * our range starts.
1507 node = tree_search(tree, cur_start);
1508 if (!node)
1509 goto out;
1511 while (1) {
1512 state = rb_entry(node, struct extent_state, rb_node);
1513 if (state->start > search_end)
1514 break;
1515 if (state->end >= cur_start && (state->state & bits)) {
1516 total_bytes += min(search_end, state->end) + 1 -
1517 max(cur_start, state->start);
1518 if (total_bytes >= max_bytes)
1519 break;
1520 if (!found) {
1521 *start = state->start;
1522 found = 1;
1525 node = rb_next(node);
1526 if (!node)
1527 break;
1529 out:
1530 spin_unlock(&tree->lock);
1531 return total_bytes;
1535 * set the private field for a given byte offset in the tree. If there isn't
1536 * an extent_state there already, this does nothing.
1538 int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
1540 struct rb_node *node;
1541 struct extent_state *state;
1542 int ret = 0;
1544 spin_lock(&tree->lock);
1546 * this search will find all the extents that end after
1547 * our range starts.
1549 node = tree_search(tree, start);
1550 if (!node) {
1551 ret = -ENOENT;
1552 goto out;
1554 state = rb_entry(node, struct extent_state, rb_node);
1555 if (state->start != start) {
1556 ret = -ENOENT;
1557 goto out;
1559 state->private = private;
1560 out:
1561 spin_unlock(&tree->lock);
1562 return ret;
1565 int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
1567 struct rb_node *node;
1568 struct extent_state *state;
1569 int ret = 0;
1571 spin_lock(&tree->lock);
1573 * this search will find all the extents that end after
1574 * our range starts.
1576 node = tree_search(tree, start);
1577 if (!node) {
1578 ret = -ENOENT;
1579 goto out;
1581 state = rb_entry(node, struct extent_state, rb_node);
1582 if (state->start != start) {
1583 ret = -ENOENT;
1584 goto out;
1586 *private = state->private;
1587 out:
1588 spin_unlock(&tree->lock);
1589 return ret;
1593 * searches a range in the state tree for a given mask.
1594 * If 'filled' == 1, this returns 1 only if every extent in the tree
1595 * has the bits set. Otherwise, 1 is returned if any bit in the
1596 * range is found set.
1598 int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
1599 int bits, int filled, struct extent_state *cached)
1601 struct extent_state *state = NULL;
1602 struct rb_node *node;
1603 int bitset = 0;
1605 spin_lock(&tree->lock);
1606 if (cached && cached->tree && cached->start == start)
1607 node = &cached->rb_node;
1608 else
1609 node = tree_search(tree, start);
1610 while (node && start <= end) {
1611 state = rb_entry(node, struct extent_state, rb_node);
1613 if (filled && state->start > start) {
1614 bitset = 0;
1615 break;
1618 if (state->start > end)
1619 break;
1621 if (state->state & bits) {
1622 bitset = 1;
1623 if (!filled)
1624 break;
1625 } else if (filled) {
1626 bitset = 0;
1627 break;
1630 if (state->end == (u64)-1)
1631 break;
1633 start = state->end + 1;
1634 if (start > end)
1635 break;
1636 node = rb_next(node);
1637 if (!node) {
1638 if (filled)
1639 bitset = 0;
1640 break;
1643 spin_unlock(&tree->lock);
1644 return bitset;
1648 * helper function to set a given page up to date if all the
1649 * extents in the tree for that page are up to date
1651 static int check_page_uptodate(struct extent_io_tree *tree,
1652 struct page *page)
1654 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1655 u64 end = start + PAGE_CACHE_SIZE - 1;
1656 if (test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL))
1657 SetPageUptodate(page);
1658 return 0;
1662 * helper function to unlock a page if all the extents in the tree
1663 * for that page are unlocked
1665 static int check_page_locked(struct extent_io_tree *tree,
1666 struct page *page)
1668 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
1669 u64 end = start + PAGE_CACHE_SIZE - 1;
1670 if (!test_range_bit(tree, start, end, EXTENT_LOCKED, 0, NULL))
1671 unlock_page(page);
1672 return 0;
1676 * helper function to end page writeback if all the extents
1677 * in the tree for that page are done with writeback
1679 static int check_page_writeback(struct extent_io_tree *tree,
1680 struct page *page)
1682 end_page_writeback(page);
1683 return 0;
1686 /* lots and lots of room for performance fixes in the end_bio funcs */
1689 * after a writepage IO is done, we need to:
1690 * clear the uptodate bits on error
1691 * clear the writeback bits in the extent tree for this IO
1692 * end_page_writeback if the page has no more pending IO
1694 * Scheduling is not allowed, so the extent state tree is expected
1695 * to have one and only one object corresponding to this IO.
1697 static void end_bio_extent_writepage(struct bio *bio, int err)
1699 int uptodate = err == 0;
1700 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1701 struct extent_io_tree *tree;
1702 u64 start;
1703 u64 end;
1704 int whole_page;
1705 int ret;
1707 do {
1708 struct page *page = bvec->bv_page;
1709 tree = &BTRFS_I(page->mapping->host)->io_tree;
1711 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1712 bvec->bv_offset;
1713 end = start + bvec->bv_len - 1;
1715 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1716 whole_page = 1;
1717 else
1718 whole_page = 0;
1720 if (--bvec >= bio->bi_io_vec)
1721 prefetchw(&bvec->bv_page->flags);
1722 if (tree->ops && tree->ops->writepage_end_io_hook) {
1723 ret = tree->ops->writepage_end_io_hook(page, start,
1724 end, NULL, uptodate);
1725 if (ret)
1726 uptodate = 0;
1729 if (!uptodate && tree->ops &&
1730 tree->ops->writepage_io_failed_hook) {
1731 ret = tree->ops->writepage_io_failed_hook(bio, page,
1732 start, end, NULL);
1733 if (ret == 0) {
1734 uptodate = (err == 0);
1735 continue;
1739 if (!uptodate) {
1740 clear_extent_uptodate(tree, start, end, NULL, GFP_NOFS);
1741 ClearPageUptodate(page);
1742 SetPageError(page);
1745 if (whole_page)
1746 end_page_writeback(page);
1747 else
1748 check_page_writeback(tree, page);
1749 } while (bvec >= bio->bi_io_vec);
1751 bio_put(bio);
1755 * after a readpage IO is done, we need to:
1756 * clear the uptodate bits on error
1757 * set the uptodate bits if things worked
1758 * set the page up to date if all extents in the tree are uptodate
1759 * clear the lock bit in the extent tree
1760 * unlock the page if there are no other extents locked for it
1762 * Scheduling is not allowed, so the extent state tree is expected
1763 * to have one and only one object corresponding to this IO.
1765 static void end_bio_extent_readpage(struct bio *bio, int err)
1767 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1768 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
1769 struct bio_vec *bvec = bio->bi_io_vec;
1770 struct extent_io_tree *tree;
1771 u64 start;
1772 u64 end;
1773 int whole_page;
1774 int ret;
1776 if (err)
1777 uptodate = 0;
1779 do {
1780 struct page *page = bvec->bv_page;
1781 tree = &BTRFS_I(page->mapping->host)->io_tree;
1783 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1784 bvec->bv_offset;
1785 end = start + bvec->bv_len - 1;
1787 if (bvec->bv_offset == 0 && bvec->bv_len == PAGE_CACHE_SIZE)
1788 whole_page = 1;
1789 else
1790 whole_page = 0;
1792 if (++bvec <= bvec_end)
1793 prefetchw(&bvec->bv_page->flags);
1795 if (uptodate && tree->ops && tree->ops->readpage_end_io_hook) {
1796 ret = tree->ops->readpage_end_io_hook(page, start, end,
1797 NULL);
1798 if (ret)
1799 uptodate = 0;
1801 if (!uptodate && tree->ops &&
1802 tree->ops->readpage_io_failed_hook) {
1803 ret = tree->ops->readpage_io_failed_hook(bio, page,
1804 start, end, NULL);
1805 if (ret == 0) {
1806 uptodate =
1807 test_bit(BIO_UPTODATE, &bio->bi_flags);
1808 if (err)
1809 uptodate = 0;
1810 continue;
1814 if (uptodate) {
1815 set_extent_uptodate(tree, start, end,
1816 GFP_ATOMIC);
1818 unlock_extent(tree, start, end, GFP_ATOMIC);
1820 if (whole_page) {
1821 if (uptodate) {
1822 SetPageUptodate(page);
1823 } else {
1824 ClearPageUptodate(page);
1825 SetPageError(page);
1827 unlock_page(page);
1828 } else {
1829 if (uptodate) {
1830 check_page_uptodate(tree, page);
1831 } else {
1832 ClearPageUptodate(page);
1833 SetPageError(page);
1835 check_page_locked(tree, page);
1837 } while (bvec <= bvec_end);
1839 bio_put(bio);
1843 * IO done from prepare_write is pretty simple, we just unlock
1844 * the structs in the extent tree when done, and set the uptodate bits
1845 * as appropriate.
1847 static void end_bio_extent_preparewrite(struct bio *bio, int err)
1849 const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1850 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1851 struct extent_io_tree *tree;
1852 u64 start;
1853 u64 end;
1855 do {
1856 struct page *page = bvec->bv_page;
1857 tree = &BTRFS_I(page->mapping->host)->io_tree;
1859 start = ((u64)page->index << PAGE_CACHE_SHIFT) +
1860 bvec->bv_offset;
1861 end = start + bvec->bv_len - 1;
1863 if (--bvec >= bio->bi_io_vec)
1864 prefetchw(&bvec->bv_page->flags);
1866 if (uptodate) {
1867 set_extent_uptodate(tree, start, end, GFP_ATOMIC);
1868 } else {
1869 ClearPageUptodate(page);
1870 SetPageError(page);
1873 unlock_extent(tree, start, end, GFP_ATOMIC);
1875 } while (bvec >= bio->bi_io_vec);
1877 bio_put(bio);
1880 static struct bio *
1881 extent_bio_alloc(struct block_device *bdev, u64 first_sector, int nr_vecs,
1882 gfp_t gfp_flags)
1884 struct bio *bio;
1886 bio = bio_alloc(gfp_flags, nr_vecs);
1888 if (bio == NULL && (current->flags & PF_MEMALLOC)) {
1889 while (!bio && (nr_vecs /= 2))
1890 bio = bio_alloc(gfp_flags, nr_vecs);
1893 if (bio) {
1894 bio->bi_size = 0;
1895 bio->bi_bdev = bdev;
1896 bio->bi_sector = first_sector;
1898 return bio;
1901 static int submit_one_bio(int rw, struct bio *bio, int mirror_num,
1902 unsigned long bio_flags)
1904 int ret = 0;
1905 struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
1906 struct page *page = bvec->bv_page;
1907 struct extent_io_tree *tree = bio->bi_private;
1908 u64 start;
1909 u64 end;
1911 start = ((u64)page->index << PAGE_CACHE_SHIFT) + bvec->bv_offset;
1912 end = start + bvec->bv_len - 1;
1914 bio->bi_private = NULL;
1916 bio_get(bio);
1918 if (tree->ops && tree->ops->submit_bio_hook)
1919 tree->ops->submit_bio_hook(page->mapping->host, rw, bio,
1920 mirror_num, bio_flags);
1921 else
1922 submit_bio(rw, bio);
1923 if (bio_flagged(bio, BIO_EOPNOTSUPP))
1924 ret = -EOPNOTSUPP;
1925 bio_put(bio);
1926 return ret;
1929 static int submit_extent_page(int rw, struct extent_io_tree *tree,
1930 struct page *page, sector_t sector,
1931 size_t size, unsigned long offset,
1932 struct block_device *bdev,
1933 struct bio **bio_ret,
1934 unsigned long max_pages,
1935 bio_end_io_t end_io_func,
1936 int mirror_num,
1937 unsigned long prev_bio_flags,
1938 unsigned long bio_flags)
1940 int ret = 0;
1941 struct bio *bio;
1942 int nr;
1943 int contig = 0;
1944 int this_compressed = bio_flags & EXTENT_BIO_COMPRESSED;
1945 int old_compressed = prev_bio_flags & EXTENT_BIO_COMPRESSED;
1946 size_t page_size = min_t(size_t, size, PAGE_CACHE_SIZE);
1948 if (bio_ret && *bio_ret) {
1949 bio = *bio_ret;
1950 if (old_compressed)
1951 contig = bio->bi_sector == sector;
1952 else
1953 contig = bio->bi_sector + (bio->bi_size >> 9) ==
1954 sector;
1956 if (prev_bio_flags != bio_flags || !contig ||
1957 (tree->ops && tree->ops->merge_bio_hook &&
1958 tree->ops->merge_bio_hook(page, offset, page_size, bio,
1959 bio_flags)) ||
1960 bio_add_page(bio, page, page_size, offset) < page_size) {
1961 ret = submit_one_bio(rw, bio, mirror_num,
1962 prev_bio_flags);
1963 bio = NULL;
1964 } else {
1965 return 0;
1968 if (this_compressed)
1969 nr = BIO_MAX_PAGES;
1970 else
1971 nr = bio_get_nr_vecs(bdev);
1973 bio = extent_bio_alloc(bdev, sector, nr, GFP_NOFS | __GFP_HIGH);
1975 bio_add_page(bio, page, page_size, offset);
1976 bio->bi_end_io = end_io_func;
1977 bio->bi_private = tree;
1979 if (bio_ret)
1980 *bio_ret = bio;
1981 else
1982 ret = submit_one_bio(rw, bio, mirror_num, bio_flags);
1984 return ret;
1987 void set_page_extent_mapped(struct page *page)
1989 if (!PagePrivate(page)) {
1990 SetPagePrivate(page);
1991 page_cache_get(page);
1992 set_page_private(page, EXTENT_PAGE_PRIVATE);
1996 static void set_page_extent_head(struct page *page, unsigned long len)
1998 set_page_private(page, EXTENT_PAGE_PRIVATE_FIRST_PAGE | len << 2);
2002 * basic readpage implementation. Locked extent state structs are inserted
2003 * into the tree that are removed when the IO is done (by the end_io
2004 * handlers)
2006 static int __extent_read_full_page(struct extent_io_tree *tree,
2007 struct page *page,
2008 get_extent_t *get_extent,
2009 struct bio **bio, int mirror_num,
2010 unsigned long *bio_flags)
2012 struct inode *inode = page->mapping->host;
2013 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2014 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2015 u64 end;
2016 u64 cur = start;
2017 u64 extent_offset;
2018 u64 last_byte = i_size_read(inode);
2019 u64 block_start;
2020 u64 cur_end;
2021 sector_t sector;
2022 struct extent_map *em;
2023 struct block_device *bdev;
2024 int ret;
2025 int nr = 0;
2026 size_t page_offset = 0;
2027 size_t iosize;
2028 size_t disk_io_size;
2029 size_t blocksize = inode->i_sb->s_blocksize;
2030 unsigned long this_bio_flag = 0;
2032 set_page_extent_mapped(page);
2034 end = page_end;
2035 lock_extent(tree, start, end, GFP_NOFS);
2037 if (page->index == last_byte >> PAGE_CACHE_SHIFT) {
2038 char *userpage;
2039 size_t zero_offset = last_byte & (PAGE_CACHE_SIZE - 1);
2041 if (zero_offset) {
2042 iosize = PAGE_CACHE_SIZE - zero_offset;
2043 userpage = kmap_atomic(page, KM_USER0);
2044 memset(userpage + zero_offset, 0, iosize);
2045 flush_dcache_page(page);
2046 kunmap_atomic(userpage, KM_USER0);
2049 while (cur <= end) {
2050 if (cur >= last_byte) {
2051 char *userpage;
2052 iosize = PAGE_CACHE_SIZE - page_offset;
2053 userpage = kmap_atomic(page, KM_USER0);
2054 memset(userpage + page_offset, 0, iosize);
2055 flush_dcache_page(page);
2056 kunmap_atomic(userpage, KM_USER0);
2057 set_extent_uptodate(tree, cur, cur + iosize - 1,
2058 GFP_NOFS);
2059 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2060 break;
2062 em = get_extent(inode, page, page_offset, cur,
2063 end - cur + 1, 0);
2064 if (IS_ERR(em) || !em) {
2065 SetPageError(page);
2066 unlock_extent(tree, cur, end, GFP_NOFS);
2067 break;
2069 extent_offset = cur - em->start;
2070 BUG_ON(extent_map_end(em) <= cur);
2071 BUG_ON(end < cur);
2073 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
2074 this_bio_flag = EXTENT_BIO_COMPRESSED;
2076 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2077 cur_end = min(extent_map_end(em) - 1, end);
2078 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2079 if (this_bio_flag & EXTENT_BIO_COMPRESSED) {
2080 disk_io_size = em->block_len;
2081 sector = em->block_start >> 9;
2082 } else {
2083 sector = (em->block_start + extent_offset) >> 9;
2084 disk_io_size = iosize;
2086 bdev = em->bdev;
2087 block_start = em->block_start;
2088 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
2089 block_start = EXTENT_MAP_HOLE;
2090 free_extent_map(em);
2091 em = NULL;
2093 /* we've found a hole, just zero and go on */
2094 if (block_start == EXTENT_MAP_HOLE) {
2095 char *userpage;
2096 userpage = kmap_atomic(page, KM_USER0);
2097 memset(userpage + page_offset, 0, iosize);
2098 flush_dcache_page(page);
2099 kunmap_atomic(userpage, KM_USER0);
2101 set_extent_uptodate(tree, cur, cur + iosize - 1,
2102 GFP_NOFS);
2103 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2104 cur = cur + iosize;
2105 page_offset += iosize;
2106 continue;
2108 /* the get_extent function already copied into the page */
2109 if (test_range_bit(tree, cur, cur_end,
2110 EXTENT_UPTODATE, 1, NULL)) {
2111 check_page_uptodate(tree, page);
2112 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2113 cur = cur + iosize;
2114 page_offset += iosize;
2115 continue;
2117 /* we have an inline extent but it didn't get marked up
2118 * to date. Error out
2120 if (block_start == EXTENT_MAP_INLINE) {
2121 SetPageError(page);
2122 unlock_extent(tree, cur, cur + iosize - 1, GFP_NOFS);
2123 cur = cur + iosize;
2124 page_offset += iosize;
2125 continue;
2128 ret = 0;
2129 if (tree->ops && tree->ops->readpage_io_hook) {
2130 ret = tree->ops->readpage_io_hook(page, cur,
2131 cur + iosize - 1);
2133 if (!ret) {
2134 unsigned long pnr = (last_byte >> PAGE_CACHE_SHIFT) + 1;
2135 pnr -= page->index;
2136 ret = submit_extent_page(READ, tree, page,
2137 sector, disk_io_size, page_offset,
2138 bdev, bio, pnr,
2139 end_bio_extent_readpage, mirror_num,
2140 *bio_flags,
2141 this_bio_flag);
2142 nr++;
2143 *bio_flags = this_bio_flag;
2145 if (ret)
2146 SetPageError(page);
2147 cur = cur + iosize;
2148 page_offset += iosize;
2150 if (!nr) {
2151 if (!PageError(page))
2152 SetPageUptodate(page);
2153 unlock_page(page);
2155 return 0;
2158 int extent_read_full_page(struct extent_io_tree *tree, struct page *page,
2159 get_extent_t *get_extent)
2161 struct bio *bio = NULL;
2162 unsigned long bio_flags = 0;
2163 int ret;
2165 ret = __extent_read_full_page(tree, page, get_extent, &bio, 0,
2166 &bio_flags);
2167 if (bio)
2168 submit_one_bio(READ, bio, 0, bio_flags);
2169 return ret;
2172 static noinline void update_nr_written(struct page *page,
2173 struct writeback_control *wbc,
2174 unsigned long nr_written)
2176 wbc->nr_to_write -= nr_written;
2177 if (wbc->range_cyclic || (wbc->nr_to_write > 0 &&
2178 wbc->range_start == 0 && wbc->range_end == LLONG_MAX))
2179 page->mapping->writeback_index = page->index + nr_written;
2183 * the writepage semantics are similar to regular writepage. extent
2184 * records are inserted to lock ranges in the tree, and as dirty areas
2185 * are found, they are marked writeback. Then the lock bits are removed
2186 * and the end_io handler clears the writeback ranges
2188 static int __extent_writepage(struct page *page, struct writeback_control *wbc,
2189 void *data)
2191 struct inode *inode = page->mapping->host;
2192 struct extent_page_data *epd = data;
2193 struct extent_io_tree *tree = epd->tree;
2194 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2195 u64 delalloc_start;
2196 u64 page_end = start + PAGE_CACHE_SIZE - 1;
2197 u64 end;
2198 u64 cur = start;
2199 u64 extent_offset;
2200 u64 last_byte = i_size_read(inode);
2201 u64 block_start;
2202 u64 iosize;
2203 u64 unlock_start;
2204 sector_t sector;
2205 struct extent_state *cached_state = NULL;
2206 struct extent_map *em;
2207 struct block_device *bdev;
2208 int ret;
2209 int nr = 0;
2210 size_t pg_offset = 0;
2211 size_t blocksize;
2212 loff_t i_size = i_size_read(inode);
2213 unsigned long end_index = i_size >> PAGE_CACHE_SHIFT;
2214 u64 nr_delalloc;
2215 u64 delalloc_end;
2216 int page_started;
2217 int compressed;
2218 int write_flags;
2219 unsigned long nr_written = 0;
2221 if (wbc->sync_mode == WB_SYNC_ALL)
2222 write_flags = WRITE_SYNC_PLUG;
2223 else
2224 write_flags = WRITE;
2226 WARN_ON(!PageLocked(page));
2227 pg_offset = i_size & (PAGE_CACHE_SIZE - 1);
2228 if (page->index > end_index ||
2229 (page->index == end_index && !pg_offset)) {
2230 page->mapping->a_ops->invalidatepage(page, 0);
2231 unlock_page(page);
2232 return 0;
2235 if (page->index == end_index) {
2236 char *userpage;
2238 userpage = kmap_atomic(page, KM_USER0);
2239 memset(userpage + pg_offset, 0,
2240 PAGE_CACHE_SIZE - pg_offset);
2241 kunmap_atomic(userpage, KM_USER0);
2242 flush_dcache_page(page);
2244 pg_offset = 0;
2246 set_page_extent_mapped(page);
2248 delalloc_start = start;
2249 delalloc_end = 0;
2250 page_started = 0;
2251 if (!epd->extent_locked) {
2252 u64 delalloc_to_write = 0;
2254 * make sure the wbc mapping index is at least updated
2255 * to this page.
2257 update_nr_written(page, wbc, 0);
2259 while (delalloc_end < page_end) {
2260 nr_delalloc = find_lock_delalloc_range(inode, tree,
2261 page,
2262 &delalloc_start,
2263 &delalloc_end,
2264 128 * 1024 * 1024);
2265 if (nr_delalloc == 0) {
2266 delalloc_start = delalloc_end + 1;
2267 continue;
2269 tree->ops->fill_delalloc(inode, page, delalloc_start,
2270 delalloc_end, &page_started,
2271 &nr_written);
2273 * delalloc_end is already one less than the total
2274 * length, so we don't subtract one from
2275 * PAGE_CACHE_SIZE
2277 delalloc_to_write += (delalloc_end - delalloc_start +
2278 PAGE_CACHE_SIZE) >>
2279 PAGE_CACHE_SHIFT;
2280 delalloc_start = delalloc_end + 1;
2282 if (wbc->nr_to_write < delalloc_to_write) {
2283 int thresh = 8192;
2285 if (delalloc_to_write < thresh * 2)
2286 thresh = delalloc_to_write;
2287 wbc->nr_to_write = min_t(u64, delalloc_to_write,
2288 thresh);
2291 /* did the fill delalloc function already unlock and start
2292 * the IO?
2294 if (page_started) {
2295 ret = 0;
2297 * we've unlocked the page, so we can't update
2298 * the mapping's writeback index, just update
2299 * nr_to_write.
2301 wbc->nr_to_write -= nr_written;
2302 goto done_unlocked;
2305 if (tree->ops && tree->ops->writepage_start_hook) {
2306 ret = tree->ops->writepage_start_hook(page, start,
2307 page_end);
2308 if (ret == -EAGAIN) {
2309 redirty_page_for_writepage(wbc, page);
2310 update_nr_written(page, wbc, nr_written);
2311 unlock_page(page);
2312 ret = 0;
2313 goto done_unlocked;
2318 * we don't want to touch the inode after unlocking the page,
2319 * so we update the mapping writeback index now
2321 update_nr_written(page, wbc, nr_written + 1);
2323 end = page_end;
2324 if (last_byte <= start) {
2325 if (tree->ops && tree->ops->writepage_end_io_hook)
2326 tree->ops->writepage_end_io_hook(page, start,
2327 page_end, NULL, 1);
2328 unlock_start = page_end + 1;
2329 goto done;
2332 blocksize = inode->i_sb->s_blocksize;
2334 while (cur <= end) {
2335 if (cur >= last_byte) {
2336 if (tree->ops && tree->ops->writepage_end_io_hook)
2337 tree->ops->writepage_end_io_hook(page, cur,
2338 page_end, NULL, 1);
2339 unlock_start = page_end + 1;
2340 break;
2342 em = epd->get_extent(inode, page, pg_offset, cur,
2343 end - cur + 1, 1);
2344 if (IS_ERR(em) || !em) {
2345 SetPageError(page);
2346 break;
2349 extent_offset = cur - em->start;
2350 BUG_ON(extent_map_end(em) <= cur);
2351 BUG_ON(end < cur);
2352 iosize = min(extent_map_end(em) - cur, end - cur + 1);
2353 iosize = (iosize + blocksize - 1) & ~((u64)blocksize - 1);
2354 sector = (em->block_start + extent_offset) >> 9;
2355 bdev = em->bdev;
2356 block_start = em->block_start;
2357 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
2358 free_extent_map(em);
2359 em = NULL;
2362 * compressed and inline extents are written through other
2363 * paths in the FS
2365 if (compressed || block_start == EXTENT_MAP_HOLE ||
2366 block_start == EXTENT_MAP_INLINE) {
2368 * end_io notification does not happen here for
2369 * compressed extents
2371 if (!compressed && tree->ops &&
2372 tree->ops->writepage_end_io_hook)
2373 tree->ops->writepage_end_io_hook(page, cur,
2374 cur + iosize - 1,
2375 NULL, 1);
2376 else if (compressed) {
2377 /* we don't want to end_page_writeback on
2378 * a compressed extent. this happens
2379 * elsewhere
2381 nr++;
2384 cur += iosize;
2385 pg_offset += iosize;
2386 unlock_start = cur;
2387 continue;
2389 /* leave this out until we have a page_mkwrite call */
2390 if (0 && !test_range_bit(tree, cur, cur + iosize - 1,
2391 EXTENT_DIRTY, 0, NULL)) {
2392 cur = cur + iosize;
2393 pg_offset += iosize;
2394 continue;
2397 if (tree->ops && tree->ops->writepage_io_hook) {
2398 ret = tree->ops->writepage_io_hook(page, cur,
2399 cur + iosize - 1);
2400 } else {
2401 ret = 0;
2403 if (ret) {
2404 SetPageError(page);
2405 } else {
2406 unsigned long max_nr = end_index + 1;
2408 set_range_writeback(tree, cur, cur + iosize - 1);
2409 if (!PageWriteback(page)) {
2410 printk(KERN_ERR "btrfs warning page %lu not "
2411 "writeback, cur %llu end %llu\n",
2412 page->index, (unsigned long long)cur,
2413 (unsigned long long)end);
2416 ret = submit_extent_page(write_flags, tree, page,
2417 sector, iosize, pg_offset,
2418 bdev, &epd->bio, max_nr,
2419 end_bio_extent_writepage,
2420 0, 0, 0);
2421 if (ret)
2422 SetPageError(page);
2424 cur = cur + iosize;
2425 pg_offset += iosize;
2426 nr++;
2428 done:
2429 if (nr == 0) {
2430 /* make sure the mapping tag for page dirty gets cleared */
2431 set_page_writeback(page);
2432 end_page_writeback(page);
2434 unlock_page(page);
2436 done_unlocked:
2438 /* drop our reference on any cached states */
2439 free_extent_state(cached_state);
2440 return 0;
2444 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
2445 * @mapping: address space structure to write
2446 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2447 * @writepage: function called for each page
2448 * @data: data passed to writepage function
2450 * If a page is already under I/O, write_cache_pages() skips it, even
2451 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2452 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2453 * and msync() need to guarantee that all the data which was dirty at the time
2454 * the call was made get new I/O started against them. If wbc->sync_mode is
2455 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2456 * existing IO to complete.
2458 static int extent_write_cache_pages(struct extent_io_tree *tree,
2459 struct address_space *mapping,
2460 struct writeback_control *wbc,
2461 writepage_t writepage, void *data,
2462 void (*flush_fn)(void *))
2464 int ret = 0;
2465 int done = 0;
2466 int nr_to_write_done = 0;
2467 struct pagevec pvec;
2468 int nr_pages;
2469 pgoff_t index;
2470 pgoff_t end; /* Inclusive */
2471 int scanned = 0;
2472 int range_whole = 0;
2474 pagevec_init(&pvec, 0);
2475 if (wbc->range_cyclic) {
2476 index = mapping->writeback_index; /* Start from prev offset */
2477 end = -1;
2478 } else {
2479 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2480 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2481 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2482 range_whole = 1;
2483 scanned = 1;
2485 retry:
2486 while (!done && !nr_to_write_done && (index <= end) &&
2487 (nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
2488 PAGECACHE_TAG_DIRTY, min(end - index,
2489 (pgoff_t)PAGEVEC_SIZE-1) + 1))) {
2490 unsigned i;
2492 scanned = 1;
2493 for (i = 0; i < nr_pages; i++) {
2494 struct page *page = pvec.pages[i];
2497 * At this point we hold neither mapping->tree_lock nor
2498 * lock on the page itself: the page may be truncated or
2499 * invalidated (changing page->mapping to NULL), or even
2500 * swizzled back from swapper_space to tmpfs file
2501 * mapping
2503 if (tree->ops && tree->ops->write_cache_pages_lock_hook)
2504 tree->ops->write_cache_pages_lock_hook(page);
2505 else
2506 lock_page(page);
2508 if (unlikely(page->mapping != mapping)) {
2509 unlock_page(page);
2510 continue;
2513 if (!wbc->range_cyclic && page->index > end) {
2514 done = 1;
2515 unlock_page(page);
2516 continue;
2519 if (wbc->sync_mode != WB_SYNC_NONE) {
2520 if (PageWriteback(page))
2521 flush_fn(data);
2522 wait_on_page_writeback(page);
2525 if (PageWriteback(page) ||
2526 !clear_page_dirty_for_io(page)) {
2527 unlock_page(page);
2528 continue;
2531 ret = (*writepage)(page, wbc, data);
2533 if (unlikely(ret == AOP_WRITEPAGE_ACTIVATE)) {
2534 unlock_page(page);
2535 ret = 0;
2537 if (ret)
2538 done = 1;
2541 * the filesystem may choose to bump up nr_to_write.
2542 * We have to make sure to honor the new nr_to_write
2543 * at any time
2545 nr_to_write_done = wbc->nr_to_write <= 0;
2547 pagevec_release(&pvec);
2548 cond_resched();
2550 if (!scanned && !done) {
2552 * We hit the last page and there is more work to be done: wrap
2553 * back to the start of the file
2555 scanned = 1;
2556 index = 0;
2557 goto retry;
2559 return ret;
2562 static void flush_epd_write_bio(struct extent_page_data *epd)
2564 if (epd->bio) {
2565 if (epd->sync_io)
2566 submit_one_bio(WRITE_SYNC, epd->bio, 0, 0);
2567 else
2568 submit_one_bio(WRITE, epd->bio, 0, 0);
2569 epd->bio = NULL;
2573 static noinline void flush_write_bio(void *data)
2575 struct extent_page_data *epd = data;
2576 flush_epd_write_bio(epd);
2579 int extent_write_full_page(struct extent_io_tree *tree, struct page *page,
2580 get_extent_t *get_extent,
2581 struct writeback_control *wbc)
2583 int ret;
2584 struct address_space *mapping = page->mapping;
2585 struct extent_page_data epd = {
2586 .bio = NULL,
2587 .tree = tree,
2588 .get_extent = get_extent,
2589 .extent_locked = 0,
2590 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
2592 struct writeback_control wbc_writepages = {
2593 .bdi = wbc->bdi,
2594 .sync_mode = wbc->sync_mode,
2595 .older_than_this = NULL,
2596 .nr_to_write = 64,
2597 .range_start = page_offset(page) + PAGE_CACHE_SIZE,
2598 .range_end = (loff_t)-1,
2601 ret = __extent_writepage(page, wbc, &epd);
2603 extent_write_cache_pages(tree, mapping, &wbc_writepages,
2604 __extent_writepage, &epd, flush_write_bio);
2605 flush_epd_write_bio(&epd);
2606 return ret;
2609 int extent_write_locked_range(struct extent_io_tree *tree, struct inode *inode,
2610 u64 start, u64 end, get_extent_t *get_extent,
2611 int mode)
2613 int ret = 0;
2614 struct address_space *mapping = inode->i_mapping;
2615 struct page *page;
2616 unsigned long nr_pages = (end - start + PAGE_CACHE_SIZE) >>
2617 PAGE_CACHE_SHIFT;
2619 struct extent_page_data epd = {
2620 .bio = NULL,
2621 .tree = tree,
2622 .get_extent = get_extent,
2623 .extent_locked = 1,
2624 .sync_io = mode == WB_SYNC_ALL,
2626 struct writeback_control wbc_writepages = {
2627 .bdi = inode->i_mapping->backing_dev_info,
2628 .sync_mode = mode,
2629 .older_than_this = NULL,
2630 .nr_to_write = nr_pages * 2,
2631 .range_start = start,
2632 .range_end = end + 1,
2635 while (start <= end) {
2636 page = find_get_page(mapping, start >> PAGE_CACHE_SHIFT);
2637 if (clear_page_dirty_for_io(page))
2638 ret = __extent_writepage(page, &wbc_writepages, &epd);
2639 else {
2640 if (tree->ops && tree->ops->writepage_end_io_hook)
2641 tree->ops->writepage_end_io_hook(page, start,
2642 start + PAGE_CACHE_SIZE - 1,
2643 NULL, 1);
2644 unlock_page(page);
2646 page_cache_release(page);
2647 start += PAGE_CACHE_SIZE;
2650 flush_epd_write_bio(&epd);
2651 return ret;
2654 int extent_writepages(struct extent_io_tree *tree,
2655 struct address_space *mapping,
2656 get_extent_t *get_extent,
2657 struct writeback_control *wbc)
2659 int ret = 0;
2660 struct extent_page_data epd = {
2661 .bio = NULL,
2662 .tree = tree,
2663 .get_extent = get_extent,
2664 .extent_locked = 0,
2665 .sync_io = wbc->sync_mode == WB_SYNC_ALL,
2668 ret = extent_write_cache_pages(tree, mapping, wbc,
2669 __extent_writepage, &epd,
2670 flush_write_bio);
2671 flush_epd_write_bio(&epd);
2672 return ret;
2675 int extent_readpages(struct extent_io_tree *tree,
2676 struct address_space *mapping,
2677 struct list_head *pages, unsigned nr_pages,
2678 get_extent_t get_extent)
2680 struct bio *bio = NULL;
2681 unsigned page_idx;
2682 struct pagevec pvec;
2683 unsigned long bio_flags = 0;
2685 pagevec_init(&pvec, 0);
2686 for (page_idx = 0; page_idx < nr_pages; page_idx++) {
2687 struct page *page = list_entry(pages->prev, struct page, lru);
2689 prefetchw(&page->flags);
2690 list_del(&page->lru);
2692 * what we want to do here is call add_to_page_cache_lru,
2693 * but that isn't exported, so we reproduce it here
2695 if (!add_to_page_cache(page, mapping,
2696 page->index, GFP_KERNEL)) {
2698 /* open coding of lru_cache_add, also not exported */
2699 page_cache_get(page);
2700 if (!pagevec_add(&pvec, page))
2701 __pagevec_lru_add_file(&pvec);
2702 __extent_read_full_page(tree, page, get_extent,
2703 &bio, 0, &bio_flags);
2705 page_cache_release(page);
2707 if (pagevec_count(&pvec))
2708 __pagevec_lru_add_file(&pvec);
2709 BUG_ON(!list_empty(pages));
2710 if (bio)
2711 submit_one_bio(READ, bio, 0, bio_flags);
2712 return 0;
2716 * basic invalidatepage code, this waits on any locked or writeback
2717 * ranges corresponding to the page, and then deletes any extent state
2718 * records from the tree
2720 int extent_invalidatepage(struct extent_io_tree *tree,
2721 struct page *page, unsigned long offset)
2723 struct extent_state *cached_state = NULL;
2724 u64 start = ((u64)page->index << PAGE_CACHE_SHIFT);
2725 u64 end = start + PAGE_CACHE_SIZE - 1;
2726 size_t blocksize = page->mapping->host->i_sb->s_blocksize;
2728 start += (offset + blocksize - 1) & ~(blocksize - 1);
2729 if (start > end)
2730 return 0;
2732 lock_extent_bits(tree, start, end, 0, &cached_state, GFP_NOFS);
2733 wait_on_page_writeback(page);
2734 clear_extent_bit(tree, start, end,
2735 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2736 EXTENT_DO_ACCOUNTING,
2737 1, 1, &cached_state, GFP_NOFS);
2738 return 0;
2742 * simple commit_write call, set_range_dirty is used to mark both
2743 * the pages and the extent records as dirty
2745 int extent_commit_write(struct extent_io_tree *tree,
2746 struct inode *inode, struct page *page,
2747 unsigned from, unsigned to)
2749 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2751 set_page_extent_mapped(page);
2752 set_page_dirty(page);
2754 if (pos > inode->i_size) {
2755 i_size_write(inode, pos);
2756 mark_inode_dirty(inode);
2758 return 0;
2761 int extent_prepare_write(struct extent_io_tree *tree,
2762 struct inode *inode, struct page *page,
2763 unsigned from, unsigned to, get_extent_t *get_extent)
2765 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2766 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
2767 u64 block_start;
2768 u64 orig_block_start;
2769 u64 block_end;
2770 u64 cur_end;
2771 struct extent_map *em;
2772 unsigned blocksize = 1 << inode->i_blkbits;
2773 size_t page_offset = 0;
2774 size_t block_off_start;
2775 size_t block_off_end;
2776 int err = 0;
2777 int iocount = 0;
2778 int ret = 0;
2779 int isnew;
2781 set_page_extent_mapped(page);
2783 block_start = (page_start + from) & ~((u64)blocksize - 1);
2784 block_end = (page_start + to - 1) | (blocksize - 1);
2785 orig_block_start = block_start;
2787 lock_extent(tree, page_start, page_end, GFP_NOFS);
2788 while (block_start <= block_end) {
2789 em = get_extent(inode, page, page_offset, block_start,
2790 block_end - block_start + 1, 1);
2791 if (IS_ERR(em) || !em)
2792 goto err;
2794 cur_end = min(block_end, extent_map_end(em) - 1);
2795 block_off_start = block_start & (PAGE_CACHE_SIZE - 1);
2796 block_off_end = block_off_start + blocksize;
2797 isnew = clear_extent_new(tree, block_start, cur_end, GFP_NOFS);
2799 if (!PageUptodate(page) && isnew &&
2800 (block_off_end > to || block_off_start < from)) {
2801 void *kaddr;
2803 kaddr = kmap_atomic(page, KM_USER0);
2804 if (block_off_end > to)
2805 memset(kaddr + to, 0, block_off_end - to);
2806 if (block_off_start < from)
2807 memset(kaddr + block_off_start, 0,
2808 from - block_off_start);
2809 flush_dcache_page(page);
2810 kunmap_atomic(kaddr, KM_USER0);
2812 if ((em->block_start != EXTENT_MAP_HOLE &&
2813 em->block_start != EXTENT_MAP_INLINE) &&
2814 !isnew && !PageUptodate(page) &&
2815 (block_off_end > to || block_off_start < from) &&
2816 !test_range_bit(tree, block_start, cur_end,
2817 EXTENT_UPTODATE, 1, NULL)) {
2818 u64 sector;
2819 u64 extent_offset = block_start - em->start;
2820 size_t iosize;
2821 sector = (em->block_start + extent_offset) >> 9;
2822 iosize = (cur_end - block_start + blocksize) &
2823 ~((u64)blocksize - 1);
2825 * we've already got the extent locked, but we
2826 * need to split the state such that our end_bio
2827 * handler can clear the lock.
2829 set_extent_bit(tree, block_start,
2830 block_start + iosize - 1,
2831 EXTENT_LOCKED, 0, NULL, NULL, GFP_NOFS);
2832 ret = submit_extent_page(READ, tree, page,
2833 sector, iosize, page_offset, em->bdev,
2834 NULL, 1,
2835 end_bio_extent_preparewrite, 0,
2836 0, 0);
2837 iocount++;
2838 block_start = block_start + iosize;
2839 } else {
2840 set_extent_uptodate(tree, block_start, cur_end,
2841 GFP_NOFS);
2842 unlock_extent(tree, block_start, cur_end, GFP_NOFS);
2843 block_start = cur_end + 1;
2845 page_offset = block_start & (PAGE_CACHE_SIZE - 1);
2846 free_extent_map(em);
2848 if (iocount) {
2849 wait_extent_bit(tree, orig_block_start,
2850 block_end, EXTENT_LOCKED);
2852 check_page_uptodate(tree, page);
2853 err:
2854 /* FIXME, zero out newly allocated blocks on error */
2855 return err;
2859 * a helper for releasepage, this tests for areas of the page that
2860 * are locked or under IO and drops the related state bits if it is safe
2861 * to drop the page.
2863 int try_release_extent_state(struct extent_map_tree *map,
2864 struct extent_io_tree *tree, struct page *page,
2865 gfp_t mask)
2867 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2868 u64 end = start + PAGE_CACHE_SIZE - 1;
2869 int ret = 1;
2871 if (test_range_bit(tree, start, end,
2872 EXTENT_IOBITS, 0, NULL))
2873 ret = 0;
2874 else {
2875 if ((mask & GFP_NOFS) == GFP_NOFS)
2876 mask = GFP_NOFS;
2878 * at this point we can safely clear everything except the
2879 * locked bit and the nodatasum bit
2881 clear_extent_bit(tree, start, end,
2882 ~(EXTENT_LOCKED | EXTENT_NODATASUM),
2883 0, 0, NULL, mask);
2885 return ret;
2889 * a helper for releasepage. As long as there are no locked extents
2890 * in the range corresponding to the page, both state records and extent
2891 * map records are removed
2893 int try_release_extent_mapping(struct extent_map_tree *map,
2894 struct extent_io_tree *tree, struct page *page,
2895 gfp_t mask)
2897 struct extent_map *em;
2898 u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
2899 u64 end = start + PAGE_CACHE_SIZE - 1;
2901 if ((mask & __GFP_WAIT) &&
2902 page->mapping->host->i_size > 16 * 1024 * 1024) {
2903 u64 len;
2904 while (start <= end) {
2905 len = end - start + 1;
2906 write_lock(&map->lock);
2907 em = lookup_extent_mapping(map, start, len);
2908 if (!em || IS_ERR(em)) {
2909 write_unlock(&map->lock);
2910 break;
2912 if (test_bit(EXTENT_FLAG_PINNED, &em->flags) ||
2913 em->start != start) {
2914 write_unlock(&map->lock);
2915 free_extent_map(em);
2916 break;
2918 if (!test_range_bit(tree, em->start,
2919 extent_map_end(em) - 1,
2920 EXTENT_LOCKED | EXTENT_WRITEBACK,
2921 0, NULL)) {
2922 remove_extent_mapping(map, em);
2923 /* once for the rb tree */
2924 free_extent_map(em);
2926 start = extent_map_end(em);
2927 write_unlock(&map->lock);
2929 /* once for us */
2930 free_extent_map(em);
2933 return try_release_extent_state(map, tree, page, mask);
2936 sector_t extent_bmap(struct address_space *mapping, sector_t iblock,
2937 get_extent_t *get_extent)
2939 struct inode *inode = mapping->host;
2940 struct extent_state *cached_state = NULL;
2941 u64 start = iblock << inode->i_blkbits;
2942 sector_t sector = 0;
2943 size_t blksize = (1 << inode->i_blkbits);
2944 struct extent_map *em;
2946 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + blksize - 1,
2947 0, &cached_state, GFP_NOFS);
2948 em = get_extent(inode, NULL, 0, start, blksize, 0);
2949 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start,
2950 start + blksize - 1, &cached_state, GFP_NOFS);
2951 if (!em || IS_ERR(em))
2952 return 0;
2954 if (em->block_start > EXTENT_MAP_LAST_BYTE)
2955 goto out;
2957 sector = (em->block_start + start - em->start) >> inode->i_blkbits;
2958 out:
2959 free_extent_map(em);
2960 return sector;
2963 int extent_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
2964 __u64 start, __u64 len, get_extent_t *get_extent)
2966 int ret;
2967 u64 off = start;
2968 u64 max = start + len;
2969 u32 flags = 0;
2970 u64 disko = 0;
2971 struct extent_map *em = NULL;
2972 struct extent_state *cached_state = NULL;
2973 int end = 0;
2974 u64 em_start = 0, em_len = 0;
2975 unsigned long emflags;
2976 ret = 0;
2978 if (len == 0)
2979 return -EINVAL;
2981 lock_extent_bits(&BTRFS_I(inode)->io_tree, start, start + len, 0,
2982 &cached_state, GFP_NOFS);
2983 em = get_extent(inode, NULL, 0, off, max - off, 0);
2984 if (!em)
2985 goto out;
2986 if (IS_ERR(em)) {
2987 ret = PTR_ERR(em);
2988 goto out;
2990 while (!end) {
2991 off = em->start + em->len;
2992 if (off >= max)
2993 end = 1;
2995 em_start = em->start;
2996 em_len = em->len;
2998 disko = 0;
2999 flags = 0;
3001 if (em->block_start == EXTENT_MAP_LAST_BYTE) {
3002 end = 1;
3003 flags |= FIEMAP_EXTENT_LAST;
3004 } else if (em->block_start == EXTENT_MAP_HOLE) {
3005 flags |= FIEMAP_EXTENT_UNWRITTEN;
3006 } else if (em->block_start == EXTENT_MAP_INLINE) {
3007 flags |= (FIEMAP_EXTENT_DATA_INLINE |
3008 FIEMAP_EXTENT_NOT_ALIGNED);
3009 } else if (em->block_start == EXTENT_MAP_DELALLOC) {
3010 flags |= (FIEMAP_EXTENT_DELALLOC |
3011 FIEMAP_EXTENT_UNKNOWN);
3012 } else {
3013 disko = em->block_start;
3015 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
3016 flags |= FIEMAP_EXTENT_ENCODED;
3018 emflags = em->flags;
3019 free_extent_map(em);
3020 em = NULL;
3022 if (!end) {
3023 em = get_extent(inode, NULL, 0, off, max - off, 0);
3024 if (!em)
3025 goto out;
3026 if (IS_ERR(em)) {
3027 ret = PTR_ERR(em);
3028 goto out;
3030 emflags = em->flags;
3032 if (test_bit(EXTENT_FLAG_VACANCY, &emflags)) {
3033 flags |= FIEMAP_EXTENT_LAST;
3034 end = 1;
3037 ret = fiemap_fill_next_extent(fieinfo, em_start, disko,
3038 em_len, flags);
3039 if (ret)
3040 goto out_free;
3042 out_free:
3043 free_extent_map(em);
3044 out:
3045 unlock_extent_cached(&BTRFS_I(inode)->io_tree, start, start + len,
3046 &cached_state, GFP_NOFS);
3047 return ret;
3050 static inline struct page *extent_buffer_page(struct extent_buffer *eb,
3051 unsigned long i)
3053 struct page *p;
3054 struct address_space *mapping;
3056 if (i == 0)
3057 return eb->first_page;
3058 i += eb->start >> PAGE_CACHE_SHIFT;
3059 mapping = eb->first_page->mapping;
3060 if (!mapping)
3061 return NULL;
3064 * extent_buffer_page is only called after pinning the page
3065 * by increasing the reference count. So we know the page must
3066 * be in the radix tree.
3068 rcu_read_lock();
3069 p = radix_tree_lookup(&mapping->page_tree, i);
3070 rcu_read_unlock();
3072 return p;
3075 static inline unsigned long num_extent_pages(u64 start, u64 len)
3077 return ((start + len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) -
3078 (start >> PAGE_CACHE_SHIFT);
3081 static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
3082 u64 start,
3083 unsigned long len,
3084 gfp_t mask)
3086 struct extent_buffer *eb = NULL;
3087 #if LEAK_DEBUG
3088 unsigned long flags;
3089 #endif
3091 eb = kmem_cache_zalloc(extent_buffer_cache, mask);
3092 eb->start = start;
3093 eb->len = len;
3094 spin_lock_init(&eb->lock);
3095 init_waitqueue_head(&eb->lock_wq);
3097 #if LEAK_DEBUG
3098 spin_lock_irqsave(&leak_lock, flags);
3099 list_add(&eb->leak_list, &buffers);
3100 spin_unlock_irqrestore(&leak_lock, flags);
3101 #endif
3102 atomic_set(&eb->refs, 1);
3104 return eb;
3107 static void __free_extent_buffer(struct extent_buffer *eb)
3109 #if LEAK_DEBUG
3110 unsigned long flags;
3111 spin_lock_irqsave(&leak_lock, flags);
3112 list_del(&eb->leak_list);
3113 spin_unlock_irqrestore(&leak_lock, flags);
3114 #endif
3115 kmem_cache_free(extent_buffer_cache, eb);
3118 struct extent_buffer *alloc_extent_buffer(struct extent_io_tree *tree,
3119 u64 start, unsigned long len,
3120 struct page *page0,
3121 gfp_t mask)
3123 unsigned long num_pages = num_extent_pages(start, len);
3124 unsigned long i;
3125 unsigned long index = start >> PAGE_CACHE_SHIFT;
3126 struct extent_buffer *eb;
3127 struct extent_buffer *exists = NULL;
3128 struct page *p;
3129 struct address_space *mapping = tree->mapping;
3130 int uptodate = 1;
3132 spin_lock(&tree->buffer_lock);
3133 eb = buffer_search(tree, start);
3134 if (eb) {
3135 atomic_inc(&eb->refs);
3136 spin_unlock(&tree->buffer_lock);
3137 mark_page_accessed(eb->first_page);
3138 return eb;
3140 spin_unlock(&tree->buffer_lock);
3142 eb = __alloc_extent_buffer(tree, start, len, mask);
3143 if (!eb)
3144 return NULL;
3146 if (page0) {
3147 eb->first_page = page0;
3148 i = 1;
3149 index++;
3150 page_cache_get(page0);
3151 mark_page_accessed(page0);
3152 set_page_extent_mapped(page0);
3153 set_page_extent_head(page0, len);
3154 uptodate = PageUptodate(page0);
3155 } else {
3156 i = 0;
3158 for (; i < num_pages; i++, index++) {
3159 p = find_or_create_page(mapping, index, mask | __GFP_HIGHMEM);
3160 if (!p) {
3161 WARN_ON(1);
3162 goto free_eb;
3164 set_page_extent_mapped(p);
3165 mark_page_accessed(p);
3166 if (i == 0) {
3167 eb->first_page = p;
3168 set_page_extent_head(p, len);
3169 } else {
3170 set_page_private(p, EXTENT_PAGE_PRIVATE);
3172 if (!PageUptodate(p))
3173 uptodate = 0;
3174 unlock_page(p);
3176 if (uptodate)
3177 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3179 spin_lock(&tree->buffer_lock);
3180 exists = buffer_tree_insert(tree, start, &eb->rb_node);
3181 if (exists) {
3182 /* add one reference for the caller */
3183 atomic_inc(&exists->refs);
3184 spin_unlock(&tree->buffer_lock);
3185 goto free_eb;
3187 /* add one reference for the tree */
3188 atomic_inc(&eb->refs);
3189 spin_unlock(&tree->buffer_lock);
3190 return eb;
3192 free_eb:
3193 if (!atomic_dec_and_test(&eb->refs))
3194 return exists;
3195 for (index = 1; index < i; index++)
3196 page_cache_release(extent_buffer_page(eb, index));
3197 page_cache_release(extent_buffer_page(eb, 0));
3198 __free_extent_buffer(eb);
3199 return exists;
3202 struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
3203 u64 start, unsigned long len,
3204 gfp_t mask)
3206 struct extent_buffer *eb;
3208 spin_lock(&tree->buffer_lock);
3209 eb = buffer_search(tree, start);
3210 if (eb)
3211 atomic_inc(&eb->refs);
3212 spin_unlock(&tree->buffer_lock);
3214 if (eb)
3215 mark_page_accessed(eb->first_page);
3217 return eb;
3220 void free_extent_buffer(struct extent_buffer *eb)
3222 if (!eb)
3223 return;
3225 if (!atomic_dec_and_test(&eb->refs))
3226 return;
3228 WARN_ON(1);
3231 int clear_extent_buffer_dirty(struct extent_io_tree *tree,
3232 struct extent_buffer *eb)
3234 unsigned long i;
3235 unsigned long num_pages;
3236 struct page *page;
3238 num_pages = num_extent_pages(eb->start, eb->len);
3240 for (i = 0; i < num_pages; i++) {
3241 page = extent_buffer_page(eb, i);
3242 if (!PageDirty(page))
3243 continue;
3245 lock_page(page);
3246 if (i == 0)
3247 set_page_extent_head(page, eb->len);
3248 else
3249 set_page_private(page, EXTENT_PAGE_PRIVATE);
3251 clear_page_dirty_for_io(page);
3252 spin_lock_irq(&page->mapping->tree_lock);
3253 if (!PageDirty(page)) {
3254 radix_tree_tag_clear(&page->mapping->page_tree,
3255 page_index(page),
3256 PAGECACHE_TAG_DIRTY);
3258 spin_unlock_irq(&page->mapping->tree_lock);
3259 unlock_page(page);
3261 return 0;
3264 int wait_on_extent_buffer_writeback(struct extent_io_tree *tree,
3265 struct extent_buffer *eb)
3267 return wait_on_extent_writeback(tree, eb->start,
3268 eb->start + eb->len - 1);
3271 int set_extent_buffer_dirty(struct extent_io_tree *tree,
3272 struct extent_buffer *eb)
3274 unsigned long i;
3275 unsigned long num_pages;
3276 int was_dirty = 0;
3278 was_dirty = test_and_set_bit(EXTENT_BUFFER_DIRTY, &eb->bflags);
3279 num_pages = num_extent_pages(eb->start, eb->len);
3280 for (i = 0; i < num_pages; i++)
3281 __set_page_dirty_nobuffers(extent_buffer_page(eb, i));
3282 return was_dirty;
3285 int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
3286 struct extent_buffer *eb,
3287 struct extent_state **cached_state)
3289 unsigned long i;
3290 struct page *page;
3291 unsigned long num_pages;
3293 num_pages = num_extent_pages(eb->start, eb->len);
3294 clear_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3296 clear_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3297 cached_state, GFP_NOFS);
3298 for (i = 0; i < num_pages; i++) {
3299 page = extent_buffer_page(eb, i);
3300 if (page)
3301 ClearPageUptodate(page);
3303 return 0;
3306 int set_extent_buffer_uptodate(struct extent_io_tree *tree,
3307 struct extent_buffer *eb)
3309 unsigned long i;
3310 struct page *page;
3311 unsigned long num_pages;
3313 num_pages = num_extent_pages(eb->start, eb->len);
3315 set_extent_uptodate(tree, eb->start, eb->start + eb->len - 1,
3316 GFP_NOFS);
3317 for (i = 0; i < num_pages; i++) {
3318 page = extent_buffer_page(eb, i);
3319 if ((i == 0 && (eb->start & (PAGE_CACHE_SIZE - 1))) ||
3320 ((i == num_pages - 1) &&
3321 ((eb->start + eb->len) & (PAGE_CACHE_SIZE - 1)))) {
3322 check_page_uptodate(tree, page);
3323 continue;
3325 SetPageUptodate(page);
3327 return 0;
3330 int extent_range_uptodate(struct extent_io_tree *tree,
3331 u64 start, u64 end)
3333 struct page *page;
3334 int ret;
3335 int pg_uptodate = 1;
3336 int uptodate;
3337 unsigned long index;
3339 ret = test_range_bit(tree, start, end, EXTENT_UPTODATE, 1, NULL);
3340 if (ret)
3341 return 1;
3342 while (start <= end) {
3343 index = start >> PAGE_CACHE_SHIFT;
3344 page = find_get_page(tree->mapping, index);
3345 uptodate = PageUptodate(page);
3346 page_cache_release(page);
3347 if (!uptodate) {
3348 pg_uptodate = 0;
3349 break;
3351 start += PAGE_CACHE_SIZE;
3353 return pg_uptodate;
3356 int extent_buffer_uptodate(struct extent_io_tree *tree,
3357 struct extent_buffer *eb,
3358 struct extent_state *cached_state)
3360 int ret = 0;
3361 unsigned long num_pages;
3362 unsigned long i;
3363 struct page *page;
3364 int pg_uptodate = 1;
3366 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
3367 return 1;
3369 ret = test_range_bit(tree, eb->start, eb->start + eb->len - 1,
3370 EXTENT_UPTODATE, 1, cached_state);
3371 if (ret)
3372 return ret;
3374 num_pages = num_extent_pages(eb->start, eb->len);
3375 for (i = 0; i < num_pages; i++) {
3376 page = extent_buffer_page(eb, i);
3377 if (!PageUptodate(page)) {
3378 pg_uptodate = 0;
3379 break;
3382 return pg_uptodate;
3385 int read_extent_buffer_pages(struct extent_io_tree *tree,
3386 struct extent_buffer *eb,
3387 u64 start, int wait,
3388 get_extent_t *get_extent, int mirror_num)
3390 unsigned long i;
3391 unsigned long start_i;
3392 struct page *page;
3393 int err;
3394 int ret = 0;
3395 int locked_pages = 0;
3396 int all_uptodate = 1;
3397 int inc_all_pages = 0;
3398 unsigned long num_pages;
3399 struct bio *bio = NULL;
3400 unsigned long bio_flags = 0;
3402 if (test_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags))
3403 return 0;
3405 if (test_range_bit(tree, eb->start, eb->start + eb->len - 1,
3406 EXTENT_UPTODATE, 1, NULL)) {
3407 return 0;
3410 if (start) {
3411 WARN_ON(start < eb->start);
3412 start_i = (start >> PAGE_CACHE_SHIFT) -
3413 (eb->start >> PAGE_CACHE_SHIFT);
3414 } else {
3415 start_i = 0;
3418 num_pages = num_extent_pages(eb->start, eb->len);
3419 for (i = start_i; i < num_pages; i++) {
3420 page = extent_buffer_page(eb, i);
3421 if (!wait) {
3422 if (!trylock_page(page))
3423 goto unlock_exit;
3424 } else {
3425 lock_page(page);
3427 locked_pages++;
3428 if (!PageUptodate(page))
3429 all_uptodate = 0;
3431 if (all_uptodate) {
3432 if (start_i == 0)
3433 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3434 goto unlock_exit;
3437 for (i = start_i; i < num_pages; i++) {
3438 page = extent_buffer_page(eb, i);
3439 if (inc_all_pages)
3440 page_cache_get(page);
3441 if (!PageUptodate(page)) {
3442 if (start_i == 0)
3443 inc_all_pages = 1;
3444 ClearPageError(page);
3445 err = __extent_read_full_page(tree, page,
3446 get_extent, &bio,
3447 mirror_num, &bio_flags);
3448 if (err)
3449 ret = err;
3450 } else {
3451 unlock_page(page);
3455 if (bio)
3456 submit_one_bio(READ, bio, mirror_num, bio_flags);
3458 if (ret || !wait)
3459 return ret;
3461 for (i = start_i; i < num_pages; i++) {
3462 page = extent_buffer_page(eb, i);
3463 wait_on_page_locked(page);
3464 if (!PageUptodate(page))
3465 ret = -EIO;
3468 if (!ret)
3469 set_bit(EXTENT_BUFFER_UPTODATE, &eb->bflags);
3470 return ret;
3472 unlock_exit:
3473 i = start_i;
3474 while (locked_pages > 0) {
3475 page = extent_buffer_page(eb, i);
3476 i++;
3477 unlock_page(page);
3478 locked_pages--;
3480 return ret;
3483 void read_extent_buffer(struct extent_buffer *eb, void *dstv,
3484 unsigned long start,
3485 unsigned long len)
3487 size_t cur;
3488 size_t offset;
3489 struct page *page;
3490 char *kaddr;
3491 char *dst = (char *)dstv;
3492 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3493 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3495 WARN_ON(start > eb->len);
3496 WARN_ON(start + len > eb->start + eb->len);
3498 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3500 while (len > 0) {
3501 page = extent_buffer_page(eb, i);
3503 cur = min(len, (PAGE_CACHE_SIZE - offset));
3504 kaddr = kmap_atomic(page, KM_USER1);
3505 memcpy(dst, kaddr + offset, cur);
3506 kunmap_atomic(kaddr, KM_USER1);
3508 dst += cur;
3509 len -= cur;
3510 offset = 0;
3511 i++;
3515 int map_private_extent_buffer(struct extent_buffer *eb, unsigned long start,
3516 unsigned long min_len, char **token, char **map,
3517 unsigned long *map_start,
3518 unsigned long *map_len, int km)
3520 size_t offset = start & (PAGE_CACHE_SIZE - 1);
3521 char *kaddr;
3522 struct page *p;
3523 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3524 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3525 unsigned long end_i = (start_offset + start + min_len - 1) >>
3526 PAGE_CACHE_SHIFT;
3528 if (i != end_i)
3529 return -EINVAL;
3531 if (i == 0) {
3532 offset = start_offset;
3533 *map_start = 0;
3534 } else {
3535 offset = 0;
3536 *map_start = ((u64)i << PAGE_CACHE_SHIFT) - start_offset;
3539 if (start + min_len > eb->len) {
3540 printk(KERN_ERR "btrfs bad mapping eb start %llu len %lu, "
3541 "wanted %lu %lu\n", (unsigned long long)eb->start,
3542 eb->len, start, min_len);
3543 WARN_ON(1);
3546 p = extent_buffer_page(eb, i);
3547 kaddr = kmap_atomic(p, km);
3548 *token = kaddr;
3549 *map = kaddr + offset;
3550 *map_len = PAGE_CACHE_SIZE - offset;
3551 return 0;
3554 int map_extent_buffer(struct extent_buffer *eb, unsigned long start,
3555 unsigned long min_len,
3556 char **token, char **map,
3557 unsigned long *map_start,
3558 unsigned long *map_len, int km)
3560 int err;
3561 int save = 0;
3562 if (eb->map_token) {
3563 unmap_extent_buffer(eb, eb->map_token, km);
3564 eb->map_token = NULL;
3565 save = 1;
3567 err = map_private_extent_buffer(eb, start, min_len, token, map,
3568 map_start, map_len, km);
3569 if (!err && save) {
3570 eb->map_token = *token;
3571 eb->kaddr = *map;
3572 eb->map_start = *map_start;
3573 eb->map_len = *map_len;
3575 return err;
3578 void unmap_extent_buffer(struct extent_buffer *eb, char *token, int km)
3580 kunmap_atomic(token, km);
3583 int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
3584 unsigned long start,
3585 unsigned long len)
3587 size_t cur;
3588 size_t offset;
3589 struct page *page;
3590 char *kaddr;
3591 char *ptr = (char *)ptrv;
3592 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3593 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3594 int ret = 0;
3596 WARN_ON(start > eb->len);
3597 WARN_ON(start + len > eb->start + eb->len);
3599 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3601 while (len > 0) {
3602 page = extent_buffer_page(eb, i);
3604 cur = min(len, (PAGE_CACHE_SIZE - offset));
3606 kaddr = kmap_atomic(page, KM_USER0);
3607 ret = memcmp(ptr, kaddr + offset, cur);
3608 kunmap_atomic(kaddr, KM_USER0);
3609 if (ret)
3610 break;
3612 ptr += cur;
3613 len -= cur;
3614 offset = 0;
3615 i++;
3617 return ret;
3620 void write_extent_buffer(struct extent_buffer *eb, const void *srcv,
3621 unsigned long start, unsigned long len)
3623 size_t cur;
3624 size_t offset;
3625 struct page *page;
3626 char *kaddr;
3627 char *src = (char *)srcv;
3628 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3629 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3631 WARN_ON(start > eb->len);
3632 WARN_ON(start + len > eb->start + eb->len);
3634 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3636 while (len > 0) {
3637 page = extent_buffer_page(eb, i);
3638 WARN_ON(!PageUptodate(page));
3640 cur = min(len, PAGE_CACHE_SIZE - offset);
3641 kaddr = kmap_atomic(page, KM_USER1);
3642 memcpy(kaddr + offset, src, cur);
3643 kunmap_atomic(kaddr, KM_USER1);
3645 src += cur;
3646 len -= cur;
3647 offset = 0;
3648 i++;
3652 void memset_extent_buffer(struct extent_buffer *eb, char c,
3653 unsigned long start, unsigned long len)
3655 size_t cur;
3656 size_t offset;
3657 struct page *page;
3658 char *kaddr;
3659 size_t start_offset = eb->start & ((u64)PAGE_CACHE_SIZE - 1);
3660 unsigned long i = (start_offset + start) >> PAGE_CACHE_SHIFT;
3662 WARN_ON(start > eb->len);
3663 WARN_ON(start + len > eb->start + eb->len);
3665 offset = (start_offset + start) & ((unsigned long)PAGE_CACHE_SIZE - 1);
3667 while (len > 0) {
3668 page = extent_buffer_page(eb, i);
3669 WARN_ON(!PageUptodate(page));
3671 cur = min(len, PAGE_CACHE_SIZE - offset);
3672 kaddr = kmap_atomic(page, KM_USER0);
3673 memset(kaddr + offset, c, cur);
3674 kunmap_atomic(kaddr, KM_USER0);
3676 len -= cur;
3677 offset = 0;
3678 i++;
3682 void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
3683 unsigned long dst_offset, unsigned long src_offset,
3684 unsigned long len)
3686 u64 dst_len = dst->len;
3687 size_t cur;
3688 size_t offset;
3689 struct page *page;
3690 char *kaddr;
3691 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3692 unsigned long i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3694 WARN_ON(src->len != dst_len);
3696 offset = (start_offset + dst_offset) &
3697 ((unsigned long)PAGE_CACHE_SIZE - 1);
3699 while (len > 0) {
3700 page = extent_buffer_page(dst, i);
3701 WARN_ON(!PageUptodate(page));
3703 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE - offset));
3705 kaddr = kmap_atomic(page, KM_USER0);
3706 read_extent_buffer(src, kaddr + offset, src_offset, cur);
3707 kunmap_atomic(kaddr, KM_USER0);
3709 src_offset += cur;
3710 len -= cur;
3711 offset = 0;
3712 i++;
3716 static void move_pages(struct page *dst_page, struct page *src_page,
3717 unsigned long dst_off, unsigned long src_off,
3718 unsigned long len)
3720 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3721 if (dst_page == src_page) {
3722 memmove(dst_kaddr + dst_off, dst_kaddr + src_off, len);
3723 } else {
3724 char *src_kaddr = kmap_atomic(src_page, KM_USER1);
3725 char *p = dst_kaddr + dst_off + len;
3726 char *s = src_kaddr + src_off + len;
3728 while (len--)
3729 *--p = *--s;
3731 kunmap_atomic(src_kaddr, KM_USER1);
3733 kunmap_atomic(dst_kaddr, KM_USER0);
3736 static void copy_pages(struct page *dst_page, struct page *src_page,
3737 unsigned long dst_off, unsigned long src_off,
3738 unsigned long len)
3740 char *dst_kaddr = kmap_atomic(dst_page, KM_USER0);
3741 char *src_kaddr;
3743 if (dst_page != src_page)
3744 src_kaddr = kmap_atomic(src_page, KM_USER1);
3745 else
3746 src_kaddr = dst_kaddr;
3748 memcpy(dst_kaddr + dst_off, src_kaddr + src_off, len);
3749 kunmap_atomic(dst_kaddr, KM_USER0);
3750 if (dst_page != src_page)
3751 kunmap_atomic(src_kaddr, KM_USER1);
3754 void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3755 unsigned long src_offset, unsigned long len)
3757 size_t cur;
3758 size_t dst_off_in_page;
3759 size_t src_off_in_page;
3760 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3761 unsigned long dst_i;
3762 unsigned long src_i;
3764 if (src_offset + len > dst->len) {
3765 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3766 "len %lu dst len %lu\n", src_offset, len, dst->len);
3767 BUG_ON(1);
3769 if (dst_offset + len > dst->len) {
3770 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3771 "len %lu dst len %lu\n", dst_offset, len, dst->len);
3772 BUG_ON(1);
3775 while (len > 0) {
3776 dst_off_in_page = (start_offset + dst_offset) &
3777 ((unsigned long)PAGE_CACHE_SIZE - 1);
3778 src_off_in_page = (start_offset + src_offset) &
3779 ((unsigned long)PAGE_CACHE_SIZE - 1);
3781 dst_i = (start_offset + dst_offset) >> PAGE_CACHE_SHIFT;
3782 src_i = (start_offset + src_offset) >> PAGE_CACHE_SHIFT;
3784 cur = min(len, (unsigned long)(PAGE_CACHE_SIZE -
3785 src_off_in_page));
3786 cur = min_t(unsigned long, cur,
3787 (unsigned long)(PAGE_CACHE_SIZE - dst_off_in_page));
3789 copy_pages(extent_buffer_page(dst, dst_i),
3790 extent_buffer_page(dst, src_i),
3791 dst_off_in_page, src_off_in_page, cur);
3793 src_offset += cur;
3794 dst_offset += cur;
3795 len -= cur;
3799 void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
3800 unsigned long src_offset, unsigned long len)
3802 size_t cur;
3803 size_t dst_off_in_page;
3804 size_t src_off_in_page;
3805 unsigned long dst_end = dst_offset + len - 1;
3806 unsigned long src_end = src_offset + len - 1;
3807 size_t start_offset = dst->start & ((u64)PAGE_CACHE_SIZE - 1);
3808 unsigned long dst_i;
3809 unsigned long src_i;
3811 if (src_offset + len > dst->len) {
3812 printk(KERN_ERR "btrfs memmove bogus src_offset %lu move "
3813 "len %lu len %lu\n", src_offset, len, dst->len);
3814 BUG_ON(1);
3816 if (dst_offset + len > dst->len) {
3817 printk(KERN_ERR "btrfs memmove bogus dst_offset %lu move "
3818 "len %lu len %lu\n", dst_offset, len, dst->len);
3819 BUG_ON(1);
3821 if (dst_offset < src_offset) {
3822 memcpy_extent_buffer(dst, dst_offset, src_offset, len);
3823 return;
3825 while (len > 0) {
3826 dst_i = (start_offset + dst_end) >> PAGE_CACHE_SHIFT;
3827 src_i = (start_offset + src_end) >> PAGE_CACHE_SHIFT;
3829 dst_off_in_page = (start_offset + dst_end) &
3830 ((unsigned long)PAGE_CACHE_SIZE - 1);
3831 src_off_in_page = (start_offset + src_end) &
3832 ((unsigned long)PAGE_CACHE_SIZE - 1);
3834 cur = min_t(unsigned long, len, src_off_in_page + 1);
3835 cur = min(cur, dst_off_in_page + 1);
3836 move_pages(extent_buffer_page(dst, dst_i),
3837 extent_buffer_page(dst, src_i),
3838 dst_off_in_page - cur + 1,
3839 src_off_in_page - cur + 1, cur);
3841 dst_end -= cur;
3842 src_end -= cur;
3843 len -= cur;
3847 int try_release_extent_buffer(struct extent_io_tree *tree, struct page *page)
3849 u64 start = page_offset(page);
3850 struct extent_buffer *eb;
3851 int ret = 1;
3852 unsigned long i;
3853 unsigned long num_pages;
3855 spin_lock(&tree->buffer_lock);
3856 eb = buffer_search(tree, start);
3857 if (!eb)
3858 goto out;
3860 if (atomic_read(&eb->refs) > 1) {
3861 ret = 0;
3862 goto out;
3864 if (test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags)) {
3865 ret = 0;
3866 goto out;
3868 /* at this point we can safely release the extent buffer */
3869 num_pages = num_extent_pages(eb->start, eb->len);
3870 for (i = 0; i < num_pages; i++)
3871 page_cache_release(extent_buffer_page(eb, i));
3872 rb_erase(&eb->rb_node, &tree->buffer);
3873 __free_extent_buffer(eb);
3874 out:
3875 spin_unlock(&tree->buffer_lock);
3876 return ret;