1 #include <linux/bitops.h>
2 #include <linux/slab.h>
5 #include <linux/pagemap.h>
6 #include <linux/page-flags.h>
7 #include <linux/module.h>
8 #include <linux/spinlock.h>
9 #include <linux/blkdev.h>
10 #include <linux/swap.h>
11 #include <linux/writeback.h>
12 #include <linux/pagevec.h>
13 #include "extent_io.h"
14 #include "extent_map.h"
17 #include "btrfs_inode.h"
19 static struct kmem_cache
*extent_state_cache
;
20 static struct kmem_cache
*extent_buffer_cache
;
22 static LIST_HEAD(buffers
);
23 static LIST_HEAD(states
);
27 static DEFINE_SPINLOCK(leak_lock
);
30 #define BUFFER_LRU_MAX 64
35 struct rb_node rb_node
;
38 struct extent_page_data
{
40 struct extent_io_tree
*tree
;
41 get_extent_t
*get_extent
;
43 /* tells writepage not to lock the state bits for this range
44 * it still does the unlocking
46 unsigned int extent_locked
:1;
48 /* tells the submit_bio code to use a WRITE_SYNC */
49 unsigned int sync_io
:1;
52 int __init
extent_io_init(void)
54 extent_state_cache
= kmem_cache_create("extent_state",
55 sizeof(struct extent_state
), 0,
56 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
57 if (!extent_state_cache
)
60 extent_buffer_cache
= kmem_cache_create("extent_buffers",
61 sizeof(struct extent_buffer
), 0,
62 SLAB_RECLAIM_ACCOUNT
| SLAB_MEM_SPREAD
, NULL
);
63 if (!extent_buffer_cache
)
64 goto free_state_cache
;
68 kmem_cache_destroy(extent_state_cache
);
72 void extent_io_exit(void)
74 struct extent_state
*state
;
75 struct extent_buffer
*eb
;
77 while (!list_empty(&states
)) {
78 state
= list_entry(states
.next
, struct extent_state
, leak_list
);
79 printk(KERN_ERR
"btrfs state leak: start %llu end %llu "
80 "state %lu in tree %p refs %d\n",
81 (unsigned long long)state
->start
,
82 (unsigned long long)state
->end
,
83 state
->state
, state
->tree
, atomic_read(&state
->refs
));
84 list_del(&state
->leak_list
);
85 kmem_cache_free(extent_state_cache
, state
);
89 while (!list_empty(&buffers
)) {
90 eb
= list_entry(buffers
.next
, struct extent_buffer
, leak_list
);
91 printk(KERN_ERR
"btrfs buffer leak start %llu len %lu "
92 "refs %d\n", (unsigned long long)eb
->start
,
93 eb
->len
, atomic_read(&eb
->refs
));
94 list_del(&eb
->leak_list
);
95 kmem_cache_free(extent_buffer_cache
, eb
);
97 if (extent_state_cache
)
98 kmem_cache_destroy(extent_state_cache
);
99 if (extent_buffer_cache
)
100 kmem_cache_destroy(extent_buffer_cache
);
103 void extent_io_tree_init(struct extent_io_tree
*tree
,
104 struct address_space
*mapping
, gfp_t mask
)
106 tree
->state
= RB_ROOT
;
107 tree
->buffer
= RB_ROOT
;
109 tree
->dirty_bytes
= 0;
110 spin_lock_init(&tree
->lock
);
111 spin_lock_init(&tree
->buffer_lock
);
112 tree
->mapping
= mapping
;
115 static struct extent_state
*alloc_extent_state(gfp_t mask
)
117 struct extent_state
*state
;
122 state
= kmem_cache_alloc(extent_state_cache
, mask
);
129 spin_lock_irqsave(&leak_lock
, flags
);
130 list_add(&state
->leak_list
, &states
);
131 spin_unlock_irqrestore(&leak_lock
, flags
);
133 atomic_set(&state
->refs
, 1);
134 init_waitqueue_head(&state
->wq
);
138 static void free_extent_state(struct extent_state
*state
)
142 if (atomic_dec_and_test(&state
->refs
)) {
146 WARN_ON(state
->tree
);
148 spin_lock_irqsave(&leak_lock
, flags
);
149 list_del(&state
->leak_list
);
150 spin_unlock_irqrestore(&leak_lock
, flags
);
152 kmem_cache_free(extent_state_cache
, state
);
156 static struct rb_node
*tree_insert(struct rb_root
*root
, u64 offset
,
157 struct rb_node
*node
)
159 struct rb_node
**p
= &root
->rb_node
;
160 struct rb_node
*parent
= NULL
;
161 struct tree_entry
*entry
;
165 entry
= rb_entry(parent
, struct tree_entry
, rb_node
);
167 if (offset
< entry
->start
)
169 else if (offset
> entry
->end
)
175 entry
= rb_entry(node
, struct tree_entry
, rb_node
);
176 rb_link_node(node
, parent
, p
);
177 rb_insert_color(node
, root
);
181 static struct rb_node
*__etree_search(struct extent_io_tree
*tree
, u64 offset
,
182 struct rb_node
**prev_ret
,
183 struct rb_node
**next_ret
)
185 struct rb_root
*root
= &tree
->state
;
186 struct rb_node
*n
= root
->rb_node
;
187 struct rb_node
*prev
= NULL
;
188 struct rb_node
*orig_prev
= NULL
;
189 struct tree_entry
*entry
;
190 struct tree_entry
*prev_entry
= NULL
;
193 entry
= rb_entry(n
, struct tree_entry
, rb_node
);
197 if (offset
< entry
->start
)
199 else if (offset
> entry
->end
)
207 while (prev
&& offset
> prev_entry
->end
) {
208 prev
= rb_next(prev
);
209 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
216 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
217 while (prev
&& offset
< prev_entry
->start
) {
218 prev
= rb_prev(prev
);
219 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
226 static inline struct rb_node
*tree_search(struct extent_io_tree
*tree
,
229 struct rb_node
*prev
= NULL
;
232 ret
= __etree_search(tree
, offset
, &prev
, NULL
);
238 static struct extent_buffer
*buffer_tree_insert(struct extent_io_tree
*tree
,
239 u64 offset
, struct rb_node
*node
)
241 struct rb_root
*root
= &tree
->buffer
;
242 struct rb_node
**p
= &root
->rb_node
;
243 struct rb_node
*parent
= NULL
;
244 struct extent_buffer
*eb
;
248 eb
= rb_entry(parent
, struct extent_buffer
, rb_node
);
250 if (offset
< eb
->start
)
252 else if (offset
> eb
->start
)
258 rb_link_node(node
, parent
, p
);
259 rb_insert_color(node
, root
);
263 static struct extent_buffer
*buffer_search(struct extent_io_tree
*tree
,
266 struct rb_root
*root
= &tree
->buffer
;
267 struct rb_node
*n
= root
->rb_node
;
268 struct extent_buffer
*eb
;
271 eb
= rb_entry(n
, struct extent_buffer
, rb_node
);
272 if (offset
< eb
->start
)
274 else if (offset
> eb
->start
)
282 static void merge_cb(struct extent_io_tree
*tree
, struct extent_state
*new,
283 struct extent_state
*other
)
285 if (tree
->ops
&& tree
->ops
->merge_extent_hook
)
286 tree
->ops
->merge_extent_hook(tree
->mapping
->host
, new,
291 * utility function to look for merge candidates inside a given range.
292 * Any extents with matching state are merged together into a single
293 * extent in the tree. Extents with EXTENT_IO in their state field
294 * are not merged because the end_io handlers need to be able to do
295 * operations on them without sleeping (or doing allocations/splits).
297 * This should be called with the tree lock held.
299 static int merge_state(struct extent_io_tree
*tree
,
300 struct extent_state
*state
)
302 struct extent_state
*other
;
303 struct rb_node
*other_node
;
305 if (state
->state
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
))
308 other_node
= rb_prev(&state
->rb_node
);
310 other
= rb_entry(other_node
, struct extent_state
, rb_node
);
311 if (other
->end
== state
->start
- 1 &&
312 other
->state
== state
->state
) {
313 merge_cb(tree
, state
, other
);
314 state
->start
= other
->start
;
316 rb_erase(&other
->rb_node
, &tree
->state
);
317 free_extent_state(other
);
320 other_node
= rb_next(&state
->rb_node
);
322 other
= rb_entry(other_node
, struct extent_state
, rb_node
);
323 if (other
->start
== state
->end
+ 1 &&
324 other
->state
== state
->state
) {
325 merge_cb(tree
, state
, other
);
326 other
->start
= state
->start
;
328 rb_erase(&state
->rb_node
, &tree
->state
);
329 free_extent_state(state
);
337 static int set_state_cb(struct extent_io_tree
*tree
,
338 struct extent_state
*state
,
341 if (tree
->ops
&& tree
->ops
->set_bit_hook
) {
342 return tree
->ops
->set_bit_hook(tree
->mapping
->host
,
343 state
->start
, state
->end
,
350 static void clear_state_cb(struct extent_io_tree
*tree
,
351 struct extent_state
*state
,
354 if (tree
->ops
&& tree
->ops
->clear_bit_hook
)
355 tree
->ops
->clear_bit_hook(tree
->mapping
->host
, state
, bits
);
359 * insert an extent_state struct into the tree. 'bits' are set on the
360 * struct before it is inserted.
362 * This may return -EEXIST if the extent is already there, in which case the
363 * state struct is freed.
365 * The tree lock is not taken internally. This is a utility function and
366 * probably isn't what you want to call (see set/clear_extent_bit).
368 static int insert_state(struct extent_io_tree
*tree
,
369 struct extent_state
*state
, u64 start
, u64 end
,
372 struct rb_node
*node
;
376 printk(KERN_ERR
"btrfs end < start %llu %llu\n",
377 (unsigned long long)end
,
378 (unsigned long long)start
);
381 state
->start
= start
;
383 ret
= set_state_cb(tree
, state
, bits
);
387 if (bits
& EXTENT_DIRTY
)
388 tree
->dirty_bytes
+= end
- start
+ 1;
389 state
->state
|= bits
;
390 node
= tree_insert(&tree
->state
, end
, &state
->rb_node
);
392 struct extent_state
*found
;
393 found
= rb_entry(node
, struct extent_state
, rb_node
);
394 printk(KERN_ERR
"btrfs found node %llu %llu on insert of "
395 "%llu %llu\n", (unsigned long long)found
->start
,
396 (unsigned long long)found
->end
,
397 (unsigned long long)start
, (unsigned long long)end
);
398 free_extent_state(state
);
402 merge_state(tree
, state
);
406 static int split_cb(struct extent_io_tree
*tree
, struct extent_state
*orig
,
409 if (tree
->ops
&& tree
->ops
->split_extent_hook
)
410 return tree
->ops
->split_extent_hook(tree
->mapping
->host
,
416 * split a given extent state struct in two, inserting the preallocated
417 * struct 'prealloc' as the newly created second half. 'split' indicates an
418 * offset inside 'orig' where it should be split.
421 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
422 * are two extent state structs in the tree:
423 * prealloc: [orig->start, split - 1]
424 * orig: [ split, orig->end ]
426 * The tree locks are not taken by this function. They need to be held
429 static int split_state(struct extent_io_tree
*tree
, struct extent_state
*orig
,
430 struct extent_state
*prealloc
, u64 split
)
432 struct rb_node
*node
;
434 split_cb(tree
, orig
, split
);
436 prealloc
->start
= orig
->start
;
437 prealloc
->end
= split
- 1;
438 prealloc
->state
= orig
->state
;
441 node
= tree_insert(&tree
->state
, prealloc
->end
, &prealloc
->rb_node
);
443 free_extent_state(prealloc
);
446 prealloc
->tree
= tree
;
451 * utility function to clear some bits in an extent state struct.
452 * it will optionally wake up any one waiting on this state (wake == 1), or
453 * forcibly remove the state from the tree (delete == 1).
455 * If no bits are set on the state struct after clearing things, the
456 * struct is freed and removed from the tree
458 static int clear_state_bit(struct extent_io_tree
*tree
,
459 struct extent_state
*state
, int bits
, int wake
,
462 int bits_to_clear
= bits
& ~EXTENT_DO_ACCOUNTING
;
463 int ret
= state
->state
& bits_to_clear
;
465 if ((bits
& EXTENT_DIRTY
) && (state
->state
& EXTENT_DIRTY
)) {
466 u64 range
= state
->end
- state
->start
+ 1;
467 WARN_ON(range
> tree
->dirty_bytes
);
468 tree
->dirty_bytes
-= range
;
470 clear_state_cb(tree
, state
, bits
);
471 state
->state
&= ~bits_to_clear
;
474 if (delete || state
->state
== 0) {
476 clear_state_cb(tree
, state
, state
->state
);
477 rb_erase(&state
->rb_node
, &tree
->state
);
479 free_extent_state(state
);
484 merge_state(tree
, state
);
490 * clear some bits on a range in the tree. This may require splitting
491 * or inserting elements in the tree, so the gfp mask is used to
492 * indicate which allocations or sleeping are allowed.
494 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
495 * the given range from the tree regardless of state (ie for truncate).
497 * the range [start, end] is inclusive.
499 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
500 * bits were already set, or zero if none of the bits were already set.
502 int clear_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
503 int bits
, int wake
, int delete,
504 struct extent_state
**cached_state
,
507 struct extent_state
*state
;
508 struct extent_state
*cached
;
509 struct extent_state
*prealloc
= NULL
;
510 struct rb_node
*next_node
;
511 struct rb_node
*node
;
517 if (bits
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
))
520 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
521 prealloc
= alloc_extent_state(mask
);
526 spin_lock(&tree
->lock
);
528 cached
= *cached_state
;
531 *cached_state
= NULL
;
535 if (cached
&& cached
->tree
&& cached
->start
== start
) {
537 atomic_dec(&cached
->refs
);
542 free_extent_state(cached
);
545 * this search will find the extents that end after
548 node
= tree_search(tree
, start
);
551 state
= rb_entry(node
, struct extent_state
, rb_node
);
553 if (state
->start
> end
)
555 WARN_ON(state
->end
< start
);
556 last_end
= state
->end
;
559 * | ---- desired range ---- |
561 * | ------------- state -------------- |
563 * We need to split the extent we found, and may flip
564 * bits on second half.
566 * If the extent we found extends past our range, we
567 * just split and search again. It'll get split again
568 * the next time though.
570 * If the extent we found is inside our range, we clear
571 * the desired bit on it.
574 if (state
->start
< start
) {
576 prealloc
= alloc_extent_state(GFP_ATOMIC
);
577 err
= split_state(tree
, state
, prealloc
, start
);
578 BUG_ON(err
== -EEXIST
);
582 if (state
->end
<= end
) {
583 set
|= clear_state_bit(tree
, state
, bits
, wake
,
585 if (last_end
== (u64
)-1)
587 start
= last_end
+ 1;
592 * | ---- desired range ---- |
594 * We need to split the extent, and clear the bit
597 if (state
->start
<= end
&& state
->end
> end
) {
599 prealloc
= alloc_extent_state(GFP_ATOMIC
);
600 err
= split_state(tree
, state
, prealloc
, end
+ 1);
601 BUG_ON(err
== -EEXIST
);
605 set
|= clear_state_bit(tree
, prealloc
, bits
, wake
, delete);
611 if (state
->end
< end
&& prealloc
&& !need_resched())
612 next_node
= rb_next(&state
->rb_node
);
616 set
|= clear_state_bit(tree
, state
, bits
, wake
, delete);
617 if (last_end
== (u64
)-1)
619 start
= last_end
+ 1;
620 if (start
<= end
&& next_node
) {
621 state
= rb_entry(next_node
, struct extent_state
,
623 if (state
->start
== start
)
629 spin_unlock(&tree
->lock
);
631 free_extent_state(prealloc
);
638 spin_unlock(&tree
->lock
);
639 if (mask
& __GFP_WAIT
)
644 static int wait_on_state(struct extent_io_tree
*tree
,
645 struct extent_state
*state
)
646 __releases(tree
->lock
)
647 __acquires(tree
->lock
)
650 prepare_to_wait(&state
->wq
, &wait
, TASK_UNINTERRUPTIBLE
);
651 spin_unlock(&tree
->lock
);
653 spin_lock(&tree
->lock
);
654 finish_wait(&state
->wq
, &wait
);
659 * waits for one or more bits to clear on a range in the state tree.
660 * The range [start, end] is inclusive.
661 * The tree lock is taken by this function
663 int wait_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
, int bits
)
665 struct extent_state
*state
;
666 struct rb_node
*node
;
668 spin_lock(&tree
->lock
);
672 * this search will find all the extents that end after
675 node
= tree_search(tree
, start
);
679 state
= rb_entry(node
, struct extent_state
, rb_node
);
681 if (state
->start
> end
)
684 if (state
->state
& bits
) {
685 start
= state
->start
;
686 atomic_inc(&state
->refs
);
687 wait_on_state(tree
, state
);
688 free_extent_state(state
);
691 start
= state
->end
+ 1;
696 if (need_resched()) {
697 spin_unlock(&tree
->lock
);
699 spin_lock(&tree
->lock
);
703 spin_unlock(&tree
->lock
);
707 static int set_state_bits(struct extent_io_tree
*tree
,
708 struct extent_state
*state
,
713 ret
= set_state_cb(tree
, state
, bits
);
717 if ((bits
& EXTENT_DIRTY
) && !(state
->state
& EXTENT_DIRTY
)) {
718 u64 range
= state
->end
- state
->start
+ 1;
719 tree
->dirty_bytes
+= range
;
721 state
->state
|= bits
;
726 static void cache_state(struct extent_state
*state
,
727 struct extent_state
**cached_ptr
)
729 if (cached_ptr
&& !(*cached_ptr
)) {
730 if (state
->state
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
)) {
732 atomic_inc(&state
->refs
);
738 * set some bits on a range in the tree. This may require allocations or
739 * sleeping, so the gfp mask is used to indicate what is allowed.
741 * If any of the exclusive bits are set, this will fail with -EEXIST if some
742 * part of the range already has the desired bits set. The start of the
743 * existing range is returned in failed_start in this case.
745 * [start, end] is inclusive This takes the tree lock.
748 static int set_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
749 int bits
, int exclusive_bits
, u64
*failed_start
,
750 struct extent_state
**cached_state
,
753 struct extent_state
*state
;
754 struct extent_state
*prealloc
= NULL
;
755 struct rb_node
*node
;
761 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
762 prealloc
= alloc_extent_state(mask
);
767 spin_lock(&tree
->lock
);
768 if (cached_state
&& *cached_state
) {
769 state
= *cached_state
;
770 if (state
->start
== start
&& state
->tree
) {
771 node
= &state
->rb_node
;
776 * this search will find all the extents that end after
779 node
= tree_search(tree
, start
);
781 err
= insert_state(tree
, prealloc
, start
, end
, bits
);
783 BUG_ON(err
== -EEXIST
);
786 state
= rb_entry(node
, struct extent_state
, rb_node
);
788 last_start
= state
->start
;
789 last_end
= state
->end
;
792 * | ---- desired range ---- |
795 * Just lock what we found and keep going
797 if (state
->start
== start
&& state
->end
<= end
) {
798 struct rb_node
*next_node
;
799 if (state
->state
& exclusive_bits
) {
800 *failed_start
= state
->start
;
805 err
= set_state_bits(tree
, state
, bits
);
809 cache_state(state
, cached_state
);
810 merge_state(tree
, state
);
811 if (last_end
== (u64
)-1)
814 start
= last_end
+ 1;
815 if (start
< end
&& prealloc
&& !need_resched()) {
816 next_node
= rb_next(node
);
818 state
= rb_entry(next_node
, struct extent_state
,
820 if (state
->start
== start
)
828 * | ---- desired range ---- |
831 * | ------------- state -------------- |
833 * We need to split the extent we found, and may flip bits on
836 * If the extent we found extends past our
837 * range, we just split and search again. It'll get split
838 * again the next time though.
840 * If the extent we found is inside our range, we set the
843 if (state
->start
< start
) {
844 if (state
->state
& exclusive_bits
) {
845 *failed_start
= start
;
849 err
= split_state(tree
, state
, prealloc
, start
);
850 BUG_ON(err
== -EEXIST
);
854 if (state
->end
<= end
) {
855 err
= set_state_bits(tree
, state
, bits
);
858 cache_state(state
, cached_state
);
859 merge_state(tree
, state
);
860 if (last_end
== (u64
)-1)
862 start
= last_end
+ 1;
867 * | ---- desired range ---- |
868 * | state | or | state |
870 * There's a hole, we need to insert something in it and
871 * ignore the extent we found.
873 if (state
->start
> start
) {
875 if (end
< last_start
)
878 this_end
= last_start
- 1;
879 err
= insert_state(tree
, prealloc
, start
, this_end
,
881 BUG_ON(err
== -EEXIST
);
886 cache_state(prealloc
, cached_state
);
888 start
= this_end
+ 1;
892 * | ---- desired range ---- |
894 * We need to split the extent, and set the bit
897 if (state
->start
<= end
&& state
->end
> end
) {
898 if (state
->state
& exclusive_bits
) {
899 *failed_start
= start
;
903 err
= split_state(tree
, state
, prealloc
, end
+ 1);
904 BUG_ON(err
== -EEXIST
);
906 err
= set_state_bits(tree
, prealloc
, bits
);
911 cache_state(prealloc
, cached_state
);
912 merge_state(tree
, prealloc
);
920 spin_unlock(&tree
->lock
);
922 free_extent_state(prealloc
);
929 spin_unlock(&tree
->lock
);
930 if (mask
& __GFP_WAIT
)
935 /* wrappers around set/clear extent bit */
936 int set_extent_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
,
939 return set_extent_bit(tree
, start
, end
, EXTENT_DIRTY
, 0, NULL
,
943 int set_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
944 int bits
, gfp_t mask
)
946 return set_extent_bit(tree
, start
, end
, bits
, 0, NULL
,
950 int clear_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
951 int bits
, gfp_t mask
)
953 return clear_extent_bit(tree
, start
, end
, bits
, 0, 0, NULL
, mask
);
956 int set_extent_delalloc(struct extent_io_tree
*tree
, u64 start
, u64 end
,
957 struct extent_state
**cached_state
, gfp_t mask
)
959 return set_extent_bit(tree
, start
, end
,
960 EXTENT_DELALLOC
| EXTENT_DIRTY
| EXTENT_UPTODATE
,
961 0, NULL
, cached_state
, mask
);
964 int clear_extent_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
,
967 return clear_extent_bit(tree
, start
, end
,
968 EXTENT_DIRTY
| EXTENT_DELALLOC
|
969 EXTENT_DO_ACCOUNTING
, 0, 0,
973 int set_extent_new(struct extent_io_tree
*tree
, u64 start
, u64 end
,
976 return set_extent_bit(tree
, start
, end
, EXTENT_NEW
, 0, NULL
,
980 static int clear_extent_new(struct extent_io_tree
*tree
, u64 start
, u64 end
,
983 return clear_extent_bit(tree
, start
, end
, EXTENT_NEW
, 0, 0,
987 int set_extent_uptodate(struct extent_io_tree
*tree
, u64 start
, u64 end
,
990 return set_extent_bit(tree
, start
, end
, EXTENT_UPTODATE
, 0, NULL
,
994 static int clear_extent_uptodate(struct extent_io_tree
*tree
, u64 start
,
995 u64 end
, struct extent_state
**cached_state
,
998 return clear_extent_bit(tree
, start
, end
, EXTENT_UPTODATE
, 0, 0,
1002 int wait_on_extent_writeback(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1004 return wait_extent_bit(tree
, start
, end
, EXTENT_WRITEBACK
);
1008 * either insert or lock state struct between start and end use mask to tell
1009 * us if waiting is desired.
1011 int lock_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1012 int bits
, struct extent_state
**cached_state
, gfp_t mask
)
1017 err
= set_extent_bit(tree
, start
, end
, EXTENT_LOCKED
| bits
,
1018 EXTENT_LOCKED
, &failed_start
,
1019 cached_state
, mask
);
1020 if (err
== -EEXIST
&& (mask
& __GFP_WAIT
)) {
1021 wait_extent_bit(tree
, failed_start
, end
, EXTENT_LOCKED
);
1022 start
= failed_start
;
1026 WARN_ON(start
> end
);
1031 int lock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
, gfp_t mask
)
1033 return lock_extent_bits(tree
, start
, end
, 0, NULL
, mask
);
1036 int try_lock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1042 err
= set_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, EXTENT_LOCKED
,
1043 &failed_start
, NULL
, mask
);
1044 if (err
== -EEXIST
) {
1045 if (failed_start
> start
)
1046 clear_extent_bit(tree
, start
, failed_start
- 1,
1047 EXTENT_LOCKED
, 1, 0, NULL
, mask
);
1053 int unlock_extent_cached(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1054 struct extent_state
**cached
, gfp_t mask
)
1056 return clear_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1, 0, cached
,
1060 int unlock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1063 return clear_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1, 0, NULL
,
1068 * helper function to set pages and extents in the tree dirty
1070 int set_range_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1072 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1073 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1076 while (index
<= end_index
) {
1077 page
= find_get_page(tree
->mapping
, index
);
1079 __set_page_dirty_nobuffers(page
);
1080 page_cache_release(page
);
1087 * helper function to set both pages and extents in the tree writeback
1089 static int set_range_writeback(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1091 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1092 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1095 while (index
<= end_index
) {
1096 page
= find_get_page(tree
->mapping
, index
);
1098 set_page_writeback(page
);
1099 page_cache_release(page
);
1106 * find the first offset in the io tree with 'bits' set. zero is
1107 * returned if we find something, and *start_ret and *end_ret are
1108 * set to reflect the state struct that was found.
1110 * If nothing was found, 1 is returned, < 0 on error
1112 int find_first_extent_bit(struct extent_io_tree
*tree
, u64 start
,
1113 u64
*start_ret
, u64
*end_ret
, int bits
)
1115 struct rb_node
*node
;
1116 struct extent_state
*state
;
1119 spin_lock(&tree
->lock
);
1121 * this search will find all the extents that end after
1124 node
= tree_search(tree
, start
);
1129 state
= rb_entry(node
, struct extent_state
, rb_node
);
1130 if (state
->end
>= start
&& (state
->state
& bits
)) {
1131 *start_ret
= state
->start
;
1132 *end_ret
= state
->end
;
1136 node
= rb_next(node
);
1141 spin_unlock(&tree
->lock
);
1145 /* find the first state struct with 'bits' set after 'start', and
1146 * return it. tree->lock must be held. NULL will returned if
1147 * nothing was found after 'start'
1149 struct extent_state
*find_first_extent_bit_state(struct extent_io_tree
*tree
,
1150 u64 start
, int bits
)
1152 struct rb_node
*node
;
1153 struct extent_state
*state
;
1156 * this search will find all the extents that end after
1159 node
= tree_search(tree
, start
);
1164 state
= rb_entry(node
, struct extent_state
, rb_node
);
1165 if (state
->end
>= start
&& (state
->state
& bits
))
1168 node
= rb_next(node
);
1177 * find a contiguous range of bytes in the file marked as delalloc, not
1178 * more than 'max_bytes'. start and end are used to return the range,
1180 * 1 is returned if we find something, 0 if nothing was in the tree
1182 static noinline u64
find_delalloc_range(struct extent_io_tree
*tree
,
1183 u64
*start
, u64
*end
, u64 max_bytes
,
1184 struct extent_state
**cached_state
)
1186 struct rb_node
*node
;
1187 struct extent_state
*state
;
1188 u64 cur_start
= *start
;
1190 u64 total_bytes
= 0;
1192 spin_lock(&tree
->lock
);
1195 * this search will find all the extents that end after
1198 node
= tree_search(tree
, cur_start
);
1206 state
= rb_entry(node
, struct extent_state
, rb_node
);
1207 if (found
&& (state
->start
!= cur_start
||
1208 (state
->state
& EXTENT_BOUNDARY
))) {
1211 if (!(state
->state
& EXTENT_DELALLOC
)) {
1217 *start
= state
->start
;
1218 *cached_state
= state
;
1219 atomic_inc(&state
->refs
);
1223 cur_start
= state
->end
+ 1;
1224 node
= rb_next(node
);
1227 total_bytes
+= state
->end
- state
->start
+ 1;
1228 if (total_bytes
>= max_bytes
)
1232 spin_unlock(&tree
->lock
);
1236 static noinline
int __unlock_for_delalloc(struct inode
*inode
,
1237 struct page
*locked_page
,
1241 struct page
*pages
[16];
1242 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1243 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1244 unsigned long nr_pages
= end_index
- index
+ 1;
1247 if (index
== locked_page
->index
&& end_index
== index
)
1250 while (nr_pages
> 0) {
1251 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1252 min_t(unsigned long, nr_pages
,
1253 ARRAY_SIZE(pages
)), pages
);
1254 for (i
= 0; i
< ret
; i
++) {
1255 if (pages
[i
] != locked_page
)
1256 unlock_page(pages
[i
]);
1257 page_cache_release(pages
[i
]);
1266 static noinline
int lock_delalloc_pages(struct inode
*inode
,
1267 struct page
*locked_page
,
1271 unsigned long index
= delalloc_start
>> PAGE_CACHE_SHIFT
;
1272 unsigned long start_index
= index
;
1273 unsigned long end_index
= delalloc_end
>> PAGE_CACHE_SHIFT
;
1274 unsigned long pages_locked
= 0;
1275 struct page
*pages
[16];
1276 unsigned long nrpages
;
1280 /* the caller is responsible for locking the start index */
1281 if (index
== locked_page
->index
&& index
== end_index
)
1284 /* skip the page at the start index */
1285 nrpages
= end_index
- index
+ 1;
1286 while (nrpages
> 0) {
1287 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1288 min_t(unsigned long,
1289 nrpages
, ARRAY_SIZE(pages
)), pages
);
1294 /* now we have an array of pages, lock them all */
1295 for (i
= 0; i
< ret
; i
++) {
1297 * the caller is taking responsibility for
1300 if (pages
[i
] != locked_page
) {
1301 lock_page(pages
[i
]);
1302 if (!PageDirty(pages
[i
]) ||
1303 pages
[i
]->mapping
!= inode
->i_mapping
) {
1305 unlock_page(pages
[i
]);
1306 page_cache_release(pages
[i
]);
1310 page_cache_release(pages
[i
]);
1319 if (ret
&& pages_locked
) {
1320 __unlock_for_delalloc(inode
, locked_page
,
1322 ((u64
)(start_index
+ pages_locked
- 1)) <<
1329 * find a contiguous range of bytes in the file marked as delalloc, not
1330 * more than 'max_bytes'. start and end are used to return the range,
1332 * 1 is returned if we find something, 0 if nothing was in the tree
1334 static noinline u64
find_lock_delalloc_range(struct inode
*inode
,
1335 struct extent_io_tree
*tree
,
1336 struct page
*locked_page
,
1337 u64
*start
, u64
*end
,
1343 struct extent_state
*cached_state
= NULL
;
1348 /* step one, find a bunch of delalloc bytes starting at start */
1349 delalloc_start
= *start
;
1351 found
= find_delalloc_range(tree
, &delalloc_start
, &delalloc_end
,
1352 max_bytes
, &cached_state
);
1353 if (!found
|| delalloc_end
<= *start
) {
1354 *start
= delalloc_start
;
1355 *end
= delalloc_end
;
1356 free_extent_state(cached_state
);
1361 * start comes from the offset of locked_page. We have to lock
1362 * pages in order, so we can't process delalloc bytes before
1365 if (delalloc_start
< *start
)
1366 delalloc_start
= *start
;
1369 * make sure to limit the number of pages we try to lock down
1372 if (delalloc_end
+ 1 - delalloc_start
> max_bytes
&& loops
)
1373 delalloc_end
= delalloc_start
+ PAGE_CACHE_SIZE
- 1;
1375 /* step two, lock all the pages after the page that has start */
1376 ret
= lock_delalloc_pages(inode
, locked_page
,
1377 delalloc_start
, delalloc_end
);
1378 if (ret
== -EAGAIN
) {
1379 /* some of the pages are gone, lets avoid looping by
1380 * shortening the size of the delalloc range we're searching
1382 free_extent_state(cached_state
);
1384 unsigned long offset
= (*start
) & (PAGE_CACHE_SIZE
- 1);
1385 max_bytes
= PAGE_CACHE_SIZE
- offset
;
1395 /* step three, lock the state bits for the whole range */
1396 lock_extent_bits(tree
, delalloc_start
, delalloc_end
,
1397 0, &cached_state
, GFP_NOFS
);
1399 /* then test to make sure it is all still delalloc */
1400 ret
= test_range_bit(tree
, delalloc_start
, delalloc_end
,
1401 EXTENT_DELALLOC
, 1, cached_state
);
1403 unlock_extent_cached(tree
, delalloc_start
, delalloc_end
,
1404 &cached_state
, GFP_NOFS
);
1405 __unlock_for_delalloc(inode
, locked_page
,
1406 delalloc_start
, delalloc_end
);
1410 free_extent_state(cached_state
);
1411 *start
= delalloc_start
;
1412 *end
= delalloc_end
;
1417 int extent_clear_unlock_delalloc(struct inode
*inode
,
1418 struct extent_io_tree
*tree
,
1419 u64 start
, u64 end
, struct page
*locked_page
,
1423 struct page
*pages
[16];
1424 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1425 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1426 unsigned long nr_pages
= end_index
- index
+ 1;
1430 if (op
& EXTENT_CLEAR_UNLOCK
)
1431 clear_bits
|= EXTENT_LOCKED
;
1432 if (op
& EXTENT_CLEAR_DIRTY
)
1433 clear_bits
|= EXTENT_DIRTY
;
1435 if (op
& EXTENT_CLEAR_DELALLOC
)
1436 clear_bits
|= EXTENT_DELALLOC
;
1438 if (op
& EXTENT_CLEAR_ACCOUNTING
)
1439 clear_bits
|= EXTENT_DO_ACCOUNTING
;
1441 clear_extent_bit(tree
, start
, end
, clear_bits
, 1, 0, NULL
, GFP_NOFS
);
1442 if (!(op
& (EXTENT_CLEAR_UNLOCK_PAGE
| EXTENT_CLEAR_DIRTY
|
1443 EXTENT_SET_WRITEBACK
| EXTENT_END_WRITEBACK
|
1444 EXTENT_SET_PRIVATE2
)))
1447 while (nr_pages
> 0) {
1448 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1449 min_t(unsigned long,
1450 nr_pages
, ARRAY_SIZE(pages
)), pages
);
1451 for (i
= 0; i
< ret
; i
++) {
1453 if (op
& EXTENT_SET_PRIVATE2
)
1454 SetPagePrivate2(pages
[i
]);
1456 if (pages
[i
] == locked_page
) {
1457 page_cache_release(pages
[i
]);
1460 if (op
& EXTENT_CLEAR_DIRTY
)
1461 clear_page_dirty_for_io(pages
[i
]);
1462 if (op
& EXTENT_SET_WRITEBACK
)
1463 set_page_writeback(pages
[i
]);
1464 if (op
& EXTENT_END_WRITEBACK
)
1465 end_page_writeback(pages
[i
]);
1466 if (op
& EXTENT_CLEAR_UNLOCK_PAGE
)
1467 unlock_page(pages
[i
]);
1468 page_cache_release(pages
[i
]);
1478 * count the number of bytes in the tree that have a given bit(s)
1479 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1480 * cached. The total number found is returned.
1482 u64
count_range_bits(struct extent_io_tree
*tree
,
1483 u64
*start
, u64 search_end
, u64 max_bytes
,
1486 struct rb_node
*node
;
1487 struct extent_state
*state
;
1488 u64 cur_start
= *start
;
1489 u64 total_bytes
= 0;
1492 if (search_end
<= cur_start
) {
1497 spin_lock(&tree
->lock
);
1498 if (cur_start
== 0 && bits
== EXTENT_DIRTY
) {
1499 total_bytes
= tree
->dirty_bytes
;
1503 * this search will find all the extents that end after
1506 node
= tree_search(tree
, cur_start
);
1511 state
= rb_entry(node
, struct extent_state
, rb_node
);
1512 if (state
->start
> search_end
)
1514 if (state
->end
>= cur_start
&& (state
->state
& bits
)) {
1515 total_bytes
+= min(search_end
, state
->end
) + 1 -
1516 max(cur_start
, state
->start
);
1517 if (total_bytes
>= max_bytes
)
1520 *start
= state
->start
;
1524 node
= rb_next(node
);
1529 spin_unlock(&tree
->lock
);
1534 * set the private field for a given byte offset in the tree. If there isn't
1535 * an extent_state there already, this does nothing.
1537 int set_state_private(struct extent_io_tree
*tree
, u64 start
, u64
private)
1539 struct rb_node
*node
;
1540 struct extent_state
*state
;
1543 spin_lock(&tree
->lock
);
1545 * this search will find all the extents that end after
1548 node
= tree_search(tree
, start
);
1553 state
= rb_entry(node
, struct extent_state
, rb_node
);
1554 if (state
->start
!= start
) {
1558 state
->private = private;
1560 spin_unlock(&tree
->lock
);
1564 int get_state_private(struct extent_io_tree
*tree
, u64 start
, u64
*private)
1566 struct rb_node
*node
;
1567 struct extent_state
*state
;
1570 spin_lock(&tree
->lock
);
1572 * this search will find all the extents that end after
1575 node
= tree_search(tree
, start
);
1580 state
= rb_entry(node
, struct extent_state
, rb_node
);
1581 if (state
->start
!= start
) {
1585 *private = state
->private;
1587 spin_unlock(&tree
->lock
);
1592 * searches a range in the state tree for a given mask.
1593 * If 'filled' == 1, this returns 1 only if every extent in the tree
1594 * has the bits set. Otherwise, 1 is returned if any bit in the
1595 * range is found set.
1597 int test_range_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1598 int bits
, int filled
, struct extent_state
*cached
)
1600 struct extent_state
*state
= NULL
;
1601 struct rb_node
*node
;
1604 spin_lock(&tree
->lock
);
1605 if (cached
&& cached
->tree
&& cached
->start
== start
)
1606 node
= &cached
->rb_node
;
1608 node
= tree_search(tree
, start
);
1609 while (node
&& start
<= end
) {
1610 state
= rb_entry(node
, struct extent_state
, rb_node
);
1612 if (filled
&& state
->start
> start
) {
1617 if (state
->start
> end
)
1620 if (state
->state
& bits
) {
1624 } else if (filled
) {
1629 if (state
->end
== (u64
)-1)
1632 start
= state
->end
+ 1;
1635 node
= rb_next(node
);
1642 spin_unlock(&tree
->lock
);
1647 * helper function to set a given page up to date if all the
1648 * extents in the tree for that page are up to date
1650 static int check_page_uptodate(struct extent_io_tree
*tree
,
1653 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1654 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1655 if (test_range_bit(tree
, start
, end
, EXTENT_UPTODATE
, 1, NULL
))
1656 SetPageUptodate(page
);
1661 * helper function to unlock a page if all the extents in the tree
1662 * for that page are unlocked
1664 static int check_page_locked(struct extent_io_tree
*tree
,
1667 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1668 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1669 if (!test_range_bit(tree
, start
, end
, EXTENT_LOCKED
, 0, NULL
))
1675 * helper function to end page writeback if all the extents
1676 * in the tree for that page are done with writeback
1678 static int check_page_writeback(struct extent_io_tree
*tree
,
1681 end_page_writeback(page
);
1685 /* lots and lots of room for performance fixes in the end_bio funcs */
1688 * after a writepage IO is done, we need to:
1689 * clear the uptodate bits on error
1690 * clear the writeback bits in the extent tree for this IO
1691 * end_page_writeback if the page has no more pending IO
1693 * Scheduling is not allowed, so the extent state tree is expected
1694 * to have one and only one object corresponding to this IO.
1696 static void end_bio_extent_writepage(struct bio
*bio
, int err
)
1698 int uptodate
= err
== 0;
1699 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1700 struct extent_io_tree
*tree
;
1707 struct page
*page
= bvec
->bv_page
;
1708 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
1710 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
1712 end
= start
+ bvec
->bv_len
- 1;
1714 if (bvec
->bv_offset
== 0 && bvec
->bv_len
== PAGE_CACHE_SIZE
)
1719 if (--bvec
>= bio
->bi_io_vec
)
1720 prefetchw(&bvec
->bv_page
->flags
);
1721 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
) {
1722 ret
= tree
->ops
->writepage_end_io_hook(page
, start
,
1723 end
, NULL
, uptodate
);
1728 if (!uptodate
&& tree
->ops
&&
1729 tree
->ops
->writepage_io_failed_hook
) {
1730 ret
= tree
->ops
->writepage_io_failed_hook(bio
, page
,
1733 uptodate
= (err
== 0);
1739 clear_extent_uptodate(tree
, start
, end
, NULL
, GFP_NOFS
);
1740 ClearPageUptodate(page
);
1745 end_page_writeback(page
);
1747 check_page_writeback(tree
, page
);
1748 } while (bvec
>= bio
->bi_io_vec
);
1754 * after a readpage IO is done, we need to:
1755 * clear the uptodate bits on error
1756 * set the uptodate bits if things worked
1757 * set the page up to date if all extents in the tree are uptodate
1758 * clear the lock bit in the extent tree
1759 * unlock the page if there are no other extents locked for it
1761 * Scheduling is not allowed, so the extent state tree is expected
1762 * to have one and only one object corresponding to this IO.
1764 static void end_bio_extent_readpage(struct bio
*bio
, int err
)
1766 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1767 struct bio_vec
*bvec_end
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1768 struct bio_vec
*bvec
= bio
->bi_io_vec
;
1769 struct extent_io_tree
*tree
;
1779 struct page
*page
= bvec
->bv_page
;
1780 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
1782 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
1784 end
= start
+ bvec
->bv_len
- 1;
1786 if (bvec
->bv_offset
== 0 && bvec
->bv_len
== PAGE_CACHE_SIZE
)
1791 if (++bvec
<= bvec_end
)
1792 prefetchw(&bvec
->bv_page
->flags
);
1794 if (uptodate
&& tree
->ops
&& tree
->ops
->readpage_end_io_hook
) {
1795 ret
= tree
->ops
->readpage_end_io_hook(page
, start
, end
,
1800 if (!uptodate
&& tree
->ops
&&
1801 tree
->ops
->readpage_io_failed_hook
) {
1802 ret
= tree
->ops
->readpage_io_failed_hook(bio
, page
,
1806 test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1814 set_extent_uptodate(tree
, start
, end
,
1817 unlock_extent(tree
, start
, end
, GFP_ATOMIC
);
1821 SetPageUptodate(page
);
1823 ClearPageUptodate(page
);
1829 check_page_uptodate(tree
, page
);
1831 ClearPageUptodate(page
);
1834 check_page_locked(tree
, page
);
1836 } while (bvec
<= bvec_end
);
1842 * IO done from prepare_write is pretty simple, we just unlock
1843 * the structs in the extent tree when done, and set the uptodate bits
1846 static void end_bio_extent_preparewrite(struct bio
*bio
, int err
)
1848 const int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1849 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1850 struct extent_io_tree
*tree
;
1855 struct page
*page
= bvec
->bv_page
;
1856 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
1858 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
1860 end
= start
+ bvec
->bv_len
- 1;
1862 if (--bvec
>= bio
->bi_io_vec
)
1863 prefetchw(&bvec
->bv_page
->flags
);
1866 set_extent_uptodate(tree
, start
, end
, GFP_ATOMIC
);
1868 ClearPageUptodate(page
);
1872 unlock_extent(tree
, start
, end
, GFP_ATOMIC
);
1874 } while (bvec
>= bio
->bi_io_vec
);
1880 extent_bio_alloc(struct block_device
*bdev
, u64 first_sector
, int nr_vecs
,
1885 bio
= bio_alloc(gfp_flags
, nr_vecs
);
1887 if (bio
== NULL
&& (current
->flags
& PF_MEMALLOC
)) {
1888 while (!bio
&& (nr_vecs
/= 2))
1889 bio
= bio_alloc(gfp_flags
, nr_vecs
);
1894 bio
->bi_bdev
= bdev
;
1895 bio
->bi_sector
= first_sector
;
1900 static int submit_one_bio(int rw
, struct bio
*bio
, int mirror_num
,
1901 unsigned long bio_flags
)
1904 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1905 struct page
*page
= bvec
->bv_page
;
1906 struct extent_io_tree
*tree
= bio
->bi_private
;
1910 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) + bvec
->bv_offset
;
1911 end
= start
+ bvec
->bv_len
- 1;
1913 bio
->bi_private
= NULL
;
1917 if (tree
->ops
&& tree
->ops
->submit_bio_hook
)
1918 tree
->ops
->submit_bio_hook(page
->mapping
->host
, rw
, bio
,
1919 mirror_num
, bio_flags
);
1921 submit_bio(rw
, bio
);
1922 if (bio_flagged(bio
, BIO_EOPNOTSUPP
))
1928 static int submit_extent_page(int rw
, struct extent_io_tree
*tree
,
1929 struct page
*page
, sector_t sector
,
1930 size_t size
, unsigned long offset
,
1931 struct block_device
*bdev
,
1932 struct bio
**bio_ret
,
1933 unsigned long max_pages
,
1934 bio_end_io_t end_io_func
,
1936 unsigned long prev_bio_flags
,
1937 unsigned long bio_flags
)
1943 int this_compressed
= bio_flags
& EXTENT_BIO_COMPRESSED
;
1944 int old_compressed
= prev_bio_flags
& EXTENT_BIO_COMPRESSED
;
1945 size_t page_size
= min_t(size_t, size
, PAGE_CACHE_SIZE
);
1947 if (bio_ret
&& *bio_ret
) {
1950 contig
= bio
->bi_sector
== sector
;
1952 contig
= bio
->bi_sector
+ (bio
->bi_size
>> 9) ==
1955 if (prev_bio_flags
!= bio_flags
|| !contig
||
1956 (tree
->ops
&& tree
->ops
->merge_bio_hook
&&
1957 tree
->ops
->merge_bio_hook(page
, offset
, page_size
, bio
,
1959 bio_add_page(bio
, page
, page_size
, offset
) < page_size
) {
1960 ret
= submit_one_bio(rw
, bio
, mirror_num
,
1967 if (this_compressed
)
1970 nr
= bio_get_nr_vecs(bdev
);
1972 bio
= extent_bio_alloc(bdev
, sector
, nr
, GFP_NOFS
| __GFP_HIGH
);
1974 bio_add_page(bio
, page
, page_size
, offset
);
1975 bio
->bi_end_io
= end_io_func
;
1976 bio
->bi_private
= tree
;
1981 ret
= submit_one_bio(rw
, bio
, mirror_num
, bio_flags
);
1986 void set_page_extent_mapped(struct page
*page
)
1988 if (!PagePrivate(page
)) {
1989 SetPagePrivate(page
);
1990 page_cache_get(page
);
1991 set_page_private(page
, EXTENT_PAGE_PRIVATE
);
1995 static void set_page_extent_head(struct page
*page
, unsigned long len
)
1997 set_page_private(page
, EXTENT_PAGE_PRIVATE_FIRST_PAGE
| len
<< 2);
2001 * basic readpage implementation. Locked extent state structs are inserted
2002 * into the tree that are removed when the IO is done (by the end_io
2005 static int __extent_read_full_page(struct extent_io_tree
*tree
,
2007 get_extent_t
*get_extent
,
2008 struct bio
**bio
, int mirror_num
,
2009 unsigned long *bio_flags
)
2011 struct inode
*inode
= page
->mapping
->host
;
2012 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2013 u64 page_end
= start
+ PAGE_CACHE_SIZE
- 1;
2017 u64 last_byte
= i_size_read(inode
);
2021 struct extent_map
*em
;
2022 struct block_device
*bdev
;
2025 size_t page_offset
= 0;
2027 size_t disk_io_size
;
2028 size_t blocksize
= inode
->i_sb
->s_blocksize
;
2029 unsigned long this_bio_flag
= 0;
2031 set_page_extent_mapped(page
);
2034 lock_extent(tree
, start
, end
, GFP_NOFS
);
2036 if (page
->index
== last_byte
>> PAGE_CACHE_SHIFT
) {
2038 size_t zero_offset
= last_byte
& (PAGE_CACHE_SIZE
- 1);
2041 iosize
= PAGE_CACHE_SIZE
- zero_offset
;
2042 userpage
= kmap_atomic(page
, KM_USER0
);
2043 memset(userpage
+ zero_offset
, 0, iosize
);
2044 flush_dcache_page(page
);
2045 kunmap_atomic(userpage
, KM_USER0
);
2048 while (cur
<= end
) {
2049 if (cur
>= last_byte
) {
2051 iosize
= PAGE_CACHE_SIZE
- page_offset
;
2052 userpage
= kmap_atomic(page
, KM_USER0
);
2053 memset(userpage
+ page_offset
, 0, iosize
);
2054 flush_dcache_page(page
);
2055 kunmap_atomic(userpage
, KM_USER0
);
2056 set_extent_uptodate(tree
, cur
, cur
+ iosize
- 1,
2058 unlock_extent(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
2061 em
= get_extent(inode
, page
, page_offset
, cur
,
2063 if (IS_ERR(em
) || !em
) {
2065 unlock_extent(tree
, cur
, end
, GFP_NOFS
);
2068 extent_offset
= cur
- em
->start
;
2069 BUG_ON(extent_map_end(em
) <= cur
);
2072 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
))
2073 this_bio_flag
= EXTENT_BIO_COMPRESSED
;
2075 iosize
= min(extent_map_end(em
) - cur
, end
- cur
+ 1);
2076 cur_end
= min(extent_map_end(em
) - 1, end
);
2077 iosize
= (iosize
+ blocksize
- 1) & ~((u64
)blocksize
- 1);
2078 if (this_bio_flag
& EXTENT_BIO_COMPRESSED
) {
2079 disk_io_size
= em
->block_len
;
2080 sector
= em
->block_start
>> 9;
2082 sector
= (em
->block_start
+ extent_offset
) >> 9;
2083 disk_io_size
= iosize
;
2086 block_start
= em
->block_start
;
2087 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
2088 block_start
= EXTENT_MAP_HOLE
;
2089 free_extent_map(em
);
2092 /* we've found a hole, just zero and go on */
2093 if (block_start
== EXTENT_MAP_HOLE
) {
2095 userpage
= kmap_atomic(page
, KM_USER0
);
2096 memset(userpage
+ page_offset
, 0, iosize
);
2097 flush_dcache_page(page
);
2098 kunmap_atomic(userpage
, KM_USER0
);
2100 set_extent_uptodate(tree
, cur
, cur
+ iosize
- 1,
2102 unlock_extent(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
2104 page_offset
+= iosize
;
2107 /* the get_extent function already copied into the page */
2108 if (test_range_bit(tree
, cur
, cur_end
,
2109 EXTENT_UPTODATE
, 1, NULL
)) {
2110 check_page_uptodate(tree
, page
);
2111 unlock_extent(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
2113 page_offset
+= iosize
;
2116 /* we have an inline extent but it didn't get marked up
2117 * to date. Error out
2119 if (block_start
== EXTENT_MAP_INLINE
) {
2121 unlock_extent(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
2123 page_offset
+= iosize
;
2128 if (tree
->ops
&& tree
->ops
->readpage_io_hook
) {
2129 ret
= tree
->ops
->readpage_io_hook(page
, cur
,
2133 unsigned long pnr
= (last_byte
>> PAGE_CACHE_SHIFT
) + 1;
2135 ret
= submit_extent_page(READ
, tree
, page
,
2136 sector
, disk_io_size
, page_offset
,
2138 end_bio_extent_readpage
, mirror_num
,
2142 *bio_flags
= this_bio_flag
;
2147 page_offset
+= iosize
;
2150 if (!PageError(page
))
2151 SetPageUptodate(page
);
2157 int extent_read_full_page(struct extent_io_tree
*tree
, struct page
*page
,
2158 get_extent_t
*get_extent
)
2160 struct bio
*bio
= NULL
;
2161 unsigned long bio_flags
= 0;
2164 ret
= __extent_read_full_page(tree
, page
, get_extent
, &bio
, 0,
2167 submit_one_bio(READ
, bio
, 0, bio_flags
);
2171 static noinline
void update_nr_written(struct page
*page
,
2172 struct writeback_control
*wbc
,
2173 unsigned long nr_written
)
2175 wbc
->nr_to_write
-= nr_written
;
2176 if (wbc
->range_cyclic
|| (wbc
->nr_to_write
> 0 &&
2177 wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
))
2178 page
->mapping
->writeback_index
= page
->index
+ nr_written
;
2182 * the writepage semantics are similar to regular writepage. extent
2183 * records are inserted to lock ranges in the tree, and as dirty areas
2184 * are found, they are marked writeback. Then the lock bits are removed
2185 * and the end_io handler clears the writeback ranges
2187 static int __extent_writepage(struct page
*page
, struct writeback_control
*wbc
,
2190 struct inode
*inode
= page
->mapping
->host
;
2191 struct extent_page_data
*epd
= data
;
2192 struct extent_io_tree
*tree
= epd
->tree
;
2193 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2195 u64 page_end
= start
+ PAGE_CACHE_SIZE
- 1;
2199 u64 last_byte
= i_size_read(inode
);
2204 struct extent_state
*cached_state
= NULL
;
2205 struct extent_map
*em
;
2206 struct block_device
*bdev
;
2209 size_t pg_offset
= 0;
2211 loff_t i_size
= i_size_read(inode
);
2212 unsigned long end_index
= i_size
>> PAGE_CACHE_SHIFT
;
2218 unsigned long nr_written
= 0;
2220 if (wbc
->sync_mode
== WB_SYNC_ALL
)
2221 write_flags
= WRITE_SYNC_PLUG
;
2223 write_flags
= WRITE
;
2225 WARN_ON(!PageLocked(page
));
2226 pg_offset
= i_size
& (PAGE_CACHE_SIZE
- 1);
2227 if (page
->index
> end_index
||
2228 (page
->index
== end_index
&& !pg_offset
)) {
2229 page
->mapping
->a_ops
->invalidatepage(page
, 0);
2234 if (page
->index
== end_index
) {
2237 userpage
= kmap_atomic(page
, KM_USER0
);
2238 memset(userpage
+ pg_offset
, 0,
2239 PAGE_CACHE_SIZE
- pg_offset
);
2240 kunmap_atomic(userpage
, KM_USER0
);
2241 flush_dcache_page(page
);
2245 set_page_extent_mapped(page
);
2247 delalloc_start
= start
;
2250 if (!epd
->extent_locked
) {
2251 u64 delalloc_to_write
= 0;
2253 * make sure the wbc mapping index is at least updated
2256 update_nr_written(page
, wbc
, 0);
2258 while (delalloc_end
< page_end
) {
2259 nr_delalloc
= find_lock_delalloc_range(inode
, tree
,
2264 if (nr_delalloc
== 0) {
2265 delalloc_start
= delalloc_end
+ 1;
2268 tree
->ops
->fill_delalloc(inode
, page
, delalloc_start
,
2269 delalloc_end
, &page_started
,
2272 * delalloc_end is already one less than the total
2273 * length, so we don't subtract one from
2276 delalloc_to_write
+= (delalloc_end
- delalloc_start
+
2279 delalloc_start
= delalloc_end
+ 1;
2281 if (wbc
->nr_to_write
< delalloc_to_write
) {
2284 if (delalloc_to_write
< thresh
* 2)
2285 thresh
= delalloc_to_write
;
2286 wbc
->nr_to_write
= min_t(u64
, delalloc_to_write
,
2290 /* did the fill delalloc function already unlock and start
2296 * we've unlocked the page, so we can't update
2297 * the mapping's writeback index, just update
2300 wbc
->nr_to_write
-= nr_written
;
2304 if (tree
->ops
&& tree
->ops
->writepage_start_hook
) {
2305 ret
= tree
->ops
->writepage_start_hook(page
, start
,
2307 if (ret
== -EAGAIN
) {
2308 redirty_page_for_writepage(wbc
, page
);
2309 update_nr_written(page
, wbc
, nr_written
);
2317 * we don't want to touch the inode after unlocking the page,
2318 * so we update the mapping writeback index now
2320 update_nr_written(page
, wbc
, nr_written
+ 1);
2323 if (last_byte
<= start
) {
2324 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2325 tree
->ops
->writepage_end_io_hook(page
, start
,
2327 unlock_start
= page_end
+ 1;
2331 blocksize
= inode
->i_sb
->s_blocksize
;
2333 while (cur
<= end
) {
2334 if (cur
>= last_byte
) {
2335 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2336 tree
->ops
->writepage_end_io_hook(page
, cur
,
2338 unlock_start
= page_end
+ 1;
2341 em
= epd
->get_extent(inode
, page
, pg_offset
, cur
,
2343 if (IS_ERR(em
) || !em
) {
2348 extent_offset
= cur
- em
->start
;
2349 BUG_ON(extent_map_end(em
) <= cur
);
2351 iosize
= min(extent_map_end(em
) - cur
, end
- cur
+ 1);
2352 iosize
= (iosize
+ blocksize
- 1) & ~((u64
)blocksize
- 1);
2353 sector
= (em
->block_start
+ extent_offset
) >> 9;
2355 block_start
= em
->block_start
;
2356 compressed
= test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
2357 free_extent_map(em
);
2361 * compressed and inline extents are written through other
2364 if (compressed
|| block_start
== EXTENT_MAP_HOLE
||
2365 block_start
== EXTENT_MAP_INLINE
) {
2367 * end_io notification does not happen here for
2368 * compressed extents
2370 if (!compressed
&& tree
->ops
&&
2371 tree
->ops
->writepage_end_io_hook
)
2372 tree
->ops
->writepage_end_io_hook(page
, cur
,
2375 else if (compressed
) {
2376 /* we don't want to end_page_writeback on
2377 * a compressed extent. this happens
2384 pg_offset
+= iosize
;
2388 /* leave this out until we have a page_mkwrite call */
2389 if (0 && !test_range_bit(tree
, cur
, cur
+ iosize
- 1,
2390 EXTENT_DIRTY
, 0, NULL
)) {
2392 pg_offset
+= iosize
;
2396 if (tree
->ops
&& tree
->ops
->writepage_io_hook
) {
2397 ret
= tree
->ops
->writepage_io_hook(page
, cur
,
2405 unsigned long max_nr
= end_index
+ 1;
2407 set_range_writeback(tree
, cur
, cur
+ iosize
- 1);
2408 if (!PageWriteback(page
)) {
2409 printk(KERN_ERR
"btrfs warning page %lu not "
2410 "writeback, cur %llu end %llu\n",
2411 page
->index
, (unsigned long long)cur
,
2412 (unsigned long long)end
);
2415 ret
= submit_extent_page(write_flags
, tree
, page
,
2416 sector
, iosize
, pg_offset
,
2417 bdev
, &epd
->bio
, max_nr
,
2418 end_bio_extent_writepage
,
2424 pg_offset
+= iosize
;
2429 /* make sure the mapping tag for page dirty gets cleared */
2430 set_page_writeback(page
);
2431 end_page_writeback(page
);
2437 /* drop our reference on any cached states */
2438 free_extent_state(cached_state
);
2443 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
2444 * @mapping: address space structure to write
2445 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2446 * @writepage: function called for each page
2447 * @data: data passed to writepage function
2449 * If a page is already under I/O, write_cache_pages() skips it, even
2450 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2451 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2452 * and msync() need to guarantee that all the data which was dirty at the time
2453 * the call was made get new I/O started against them. If wbc->sync_mode is
2454 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2455 * existing IO to complete.
2457 static int extent_write_cache_pages(struct extent_io_tree
*tree
,
2458 struct address_space
*mapping
,
2459 struct writeback_control
*wbc
,
2460 writepage_t writepage
, void *data
,
2461 void (*flush_fn
)(void *))
2465 int nr_to_write_done
= 0;
2466 struct pagevec pvec
;
2469 pgoff_t end
; /* Inclusive */
2471 int range_whole
= 0;
2473 pagevec_init(&pvec
, 0);
2474 if (wbc
->range_cyclic
) {
2475 index
= mapping
->writeback_index
; /* Start from prev offset */
2478 index
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
2479 end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
2480 if (wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
)
2485 while (!done
&& !nr_to_write_done
&& (index
<= end
) &&
2486 (nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
,
2487 PAGECACHE_TAG_DIRTY
, min(end
- index
,
2488 (pgoff_t
)PAGEVEC_SIZE
-1) + 1))) {
2492 for (i
= 0; i
< nr_pages
; i
++) {
2493 struct page
*page
= pvec
.pages
[i
];
2496 * At this point we hold neither mapping->tree_lock nor
2497 * lock on the page itself: the page may be truncated or
2498 * invalidated (changing page->mapping to NULL), or even
2499 * swizzled back from swapper_space to tmpfs file
2502 if (tree
->ops
&& tree
->ops
->write_cache_pages_lock_hook
)
2503 tree
->ops
->write_cache_pages_lock_hook(page
);
2507 if (unlikely(page
->mapping
!= mapping
)) {
2512 if (!wbc
->range_cyclic
&& page
->index
> end
) {
2518 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
2519 if (PageWriteback(page
))
2521 wait_on_page_writeback(page
);
2524 if (PageWriteback(page
) ||
2525 !clear_page_dirty_for_io(page
)) {
2530 ret
= (*writepage
)(page
, wbc
, data
);
2532 if (unlikely(ret
== AOP_WRITEPAGE_ACTIVATE
)) {
2540 * the filesystem may choose to bump up nr_to_write.
2541 * We have to make sure to honor the new nr_to_write
2544 nr_to_write_done
= wbc
->nr_to_write
<= 0;
2546 pagevec_release(&pvec
);
2549 if (!scanned
&& !done
) {
2551 * We hit the last page and there is more work to be done: wrap
2552 * back to the start of the file
2561 static void flush_epd_write_bio(struct extent_page_data
*epd
)
2565 submit_one_bio(WRITE_SYNC
, epd
->bio
, 0, 0);
2567 submit_one_bio(WRITE
, epd
->bio
, 0, 0);
2572 static noinline
void flush_write_bio(void *data
)
2574 struct extent_page_data
*epd
= data
;
2575 flush_epd_write_bio(epd
);
2578 int extent_write_full_page(struct extent_io_tree
*tree
, struct page
*page
,
2579 get_extent_t
*get_extent
,
2580 struct writeback_control
*wbc
)
2583 struct address_space
*mapping
= page
->mapping
;
2584 struct extent_page_data epd
= {
2587 .get_extent
= get_extent
,
2589 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
2591 struct writeback_control wbc_writepages
= {
2593 .sync_mode
= wbc
->sync_mode
,
2594 .older_than_this
= NULL
,
2596 .range_start
= page_offset(page
) + PAGE_CACHE_SIZE
,
2597 .range_end
= (loff_t
)-1,
2600 ret
= __extent_writepage(page
, wbc
, &epd
);
2602 extent_write_cache_pages(tree
, mapping
, &wbc_writepages
,
2603 __extent_writepage
, &epd
, flush_write_bio
);
2604 flush_epd_write_bio(&epd
);
2608 int extent_write_locked_range(struct extent_io_tree
*tree
, struct inode
*inode
,
2609 u64 start
, u64 end
, get_extent_t
*get_extent
,
2613 struct address_space
*mapping
= inode
->i_mapping
;
2615 unsigned long nr_pages
= (end
- start
+ PAGE_CACHE_SIZE
) >>
2618 struct extent_page_data epd
= {
2621 .get_extent
= get_extent
,
2623 .sync_io
= mode
== WB_SYNC_ALL
,
2625 struct writeback_control wbc_writepages
= {
2626 .bdi
= inode
->i_mapping
->backing_dev_info
,
2628 .older_than_this
= NULL
,
2629 .nr_to_write
= nr_pages
* 2,
2630 .range_start
= start
,
2631 .range_end
= end
+ 1,
2634 while (start
<= end
) {
2635 page
= find_get_page(mapping
, start
>> PAGE_CACHE_SHIFT
);
2636 if (clear_page_dirty_for_io(page
))
2637 ret
= __extent_writepage(page
, &wbc_writepages
, &epd
);
2639 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2640 tree
->ops
->writepage_end_io_hook(page
, start
,
2641 start
+ PAGE_CACHE_SIZE
- 1,
2645 page_cache_release(page
);
2646 start
+= PAGE_CACHE_SIZE
;
2649 flush_epd_write_bio(&epd
);
2653 int extent_writepages(struct extent_io_tree
*tree
,
2654 struct address_space
*mapping
,
2655 get_extent_t
*get_extent
,
2656 struct writeback_control
*wbc
)
2659 struct extent_page_data epd
= {
2662 .get_extent
= get_extent
,
2664 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
2667 ret
= extent_write_cache_pages(tree
, mapping
, wbc
,
2668 __extent_writepage
, &epd
,
2670 flush_epd_write_bio(&epd
);
2674 int extent_readpages(struct extent_io_tree
*tree
,
2675 struct address_space
*mapping
,
2676 struct list_head
*pages
, unsigned nr_pages
,
2677 get_extent_t get_extent
)
2679 struct bio
*bio
= NULL
;
2681 unsigned long bio_flags
= 0;
2683 for (page_idx
= 0; page_idx
< nr_pages
; page_idx
++) {
2684 struct page
*page
= list_entry(pages
->prev
, struct page
, lru
);
2686 prefetchw(&page
->flags
);
2687 list_del(&page
->lru
);
2688 if (!add_to_page_cache_lru(page
, mapping
,
2689 page
->index
, GFP_KERNEL
)) {
2690 __extent_read_full_page(tree
, page
, get_extent
,
2691 &bio
, 0, &bio_flags
);
2693 page_cache_release(page
);
2695 BUG_ON(!list_empty(pages
));
2697 submit_one_bio(READ
, bio
, 0, bio_flags
);
2702 * basic invalidatepage code, this waits on any locked or writeback
2703 * ranges corresponding to the page, and then deletes any extent state
2704 * records from the tree
2706 int extent_invalidatepage(struct extent_io_tree
*tree
,
2707 struct page
*page
, unsigned long offset
)
2709 struct extent_state
*cached_state
= NULL
;
2710 u64 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
2711 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
2712 size_t blocksize
= page
->mapping
->host
->i_sb
->s_blocksize
;
2714 start
+= (offset
+ blocksize
- 1) & ~(blocksize
- 1);
2718 lock_extent_bits(tree
, start
, end
, 0, &cached_state
, GFP_NOFS
);
2719 wait_on_page_writeback(page
);
2720 clear_extent_bit(tree
, start
, end
,
2721 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
|
2722 EXTENT_DO_ACCOUNTING
,
2723 1, 1, &cached_state
, GFP_NOFS
);
2728 * simple commit_write call, set_range_dirty is used to mark both
2729 * the pages and the extent records as dirty
2731 int extent_commit_write(struct extent_io_tree
*tree
,
2732 struct inode
*inode
, struct page
*page
,
2733 unsigned from
, unsigned to
)
2735 loff_t pos
= ((loff_t
)page
->index
<< PAGE_CACHE_SHIFT
) + to
;
2737 set_page_extent_mapped(page
);
2738 set_page_dirty(page
);
2740 if (pos
> inode
->i_size
) {
2741 i_size_write(inode
, pos
);
2742 mark_inode_dirty(inode
);
2747 int extent_prepare_write(struct extent_io_tree
*tree
,
2748 struct inode
*inode
, struct page
*page
,
2749 unsigned from
, unsigned to
, get_extent_t
*get_extent
)
2751 u64 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2752 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2754 u64 orig_block_start
;
2757 struct extent_map
*em
;
2758 unsigned blocksize
= 1 << inode
->i_blkbits
;
2759 size_t page_offset
= 0;
2760 size_t block_off_start
;
2761 size_t block_off_end
;
2767 set_page_extent_mapped(page
);
2769 block_start
= (page_start
+ from
) & ~((u64
)blocksize
- 1);
2770 block_end
= (page_start
+ to
- 1) | (blocksize
- 1);
2771 orig_block_start
= block_start
;
2773 lock_extent(tree
, page_start
, page_end
, GFP_NOFS
);
2774 while (block_start
<= block_end
) {
2775 em
= get_extent(inode
, page
, page_offset
, block_start
,
2776 block_end
- block_start
+ 1, 1);
2777 if (IS_ERR(em
) || !em
)
2780 cur_end
= min(block_end
, extent_map_end(em
) - 1);
2781 block_off_start
= block_start
& (PAGE_CACHE_SIZE
- 1);
2782 block_off_end
= block_off_start
+ blocksize
;
2783 isnew
= clear_extent_new(tree
, block_start
, cur_end
, GFP_NOFS
);
2785 if (!PageUptodate(page
) && isnew
&&
2786 (block_off_end
> to
|| block_off_start
< from
)) {
2789 kaddr
= kmap_atomic(page
, KM_USER0
);
2790 if (block_off_end
> to
)
2791 memset(kaddr
+ to
, 0, block_off_end
- to
);
2792 if (block_off_start
< from
)
2793 memset(kaddr
+ block_off_start
, 0,
2794 from
- block_off_start
);
2795 flush_dcache_page(page
);
2796 kunmap_atomic(kaddr
, KM_USER0
);
2798 if ((em
->block_start
!= EXTENT_MAP_HOLE
&&
2799 em
->block_start
!= EXTENT_MAP_INLINE
) &&
2800 !isnew
&& !PageUptodate(page
) &&
2801 (block_off_end
> to
|| block_off_start
< from
) &&
2802 !test_range_bit(tree
, block_start
, cur_end
,
2803 EXTENT_UPTODATE
, 1, NULL
)) {
2805 u64 extent_offset
= block_start
- em
->start
;
2807 sector
= (em
->block_start
+ extent_offset
) >> 9;
2808 iosize
= (cur_end
- block_start
+ blocksize
) &
2809 ~((u64
)blocksize
- 1);
2811 * we've already got the extent locked, but we
2812 * need to split the state such that our end_bio
2813 * handler can clear the lock.
2815 set_extent_bit(tree
, block_start
,
2816 block_start
+ iosize
- 1,
2817 EXTENT_LOCKED
, 0, NULL
, NULL
, GFP_NOFS
);
2818 ret
= submit_extent_page(READ
, tree
, page
,
2819 sector
, iosize
, page_offset
, em
->bdev
,
2821 end_bio_extent_preparewrite
, 0,
2824 block_start
= block_start
+ iosize
;
2826 set_extent_uptodate(tree
, block_start
, cur_end
,
2828 unlock_extent(tree
, block_start
, cur_end
, GFP_NOFS
);
2829 block_start
= cur_end
+ 1;
2831 page_offset
= block_start
& (PAGE_CACHE_SIZE
- 1);
2832 free_extent_map(em
);
2835 wait_extent_bit(tree
, orig_block_start
,
2836 block_end
, EXTENT_LOCKED
);
2838 check_page_uptodate(tree
, page
);
2840 /* FIXME, zero out newly allocated blocks on error */
2845 * a helper for releasepage, this tests for areas of the page that
2846 * are locked or under IO and drops the related state bits if it is safe
2849 int try_release_extent_state(struct extent_map_tree
*map
,
2850 struct extent_io_tree
*tree
, struct page
*page
,
2853 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2854 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
2857 if (test_range_bit(tree
, start
, end
,
2858 EXTENT_IOBITS
, 0, NULL
))
2861 if ((mask
& GFP_NOFS
) == GFP_NOFS
)
2864 * at this point we can safely clear everything except the
2865 * locked bit and the nodatasum bit
2867 clear_extent_bit(tree
, start
, end
,
2868 ~(EXTENT_LOCKED
| EXTENT_NODATASUM
),
2875 * a helper for releasepage. As long as there are no locked extents
2876 * in the range corresponding to the page, both state records and extent
2877 * map records are removed
2879 int try_release_extent_mapping(struct extent_map_tree
*map
,
2880 struct extent_io_tree
*tree
, struct page
*page
,
2883 struct extent_map
*em
;
2884 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2885 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
2887 if ((mask
& __GFP_WAIT
) &&
2888 page
->mapping
->host
->i_size
> 16 * 1024 * 1024) {
2890 while (start
<= end
) {
2891 len
= end
- start
+ 1;
2892 write_lock(&map
->lock
);
2893 em
= lookup_extent_mapping(map
, start
, len
);
2894 if (!em
|| IS_ERR(em
)) {
2895 write_unlock(&map
->lock
);
2898 if (test_bit(EXTENT_FLAG_PINNED
, &em
->flags
) ||
2899 em
->start
!= start
) {
2900 write_unlock(&map
->lock
);
2901 free_extent_map(em
);
2904 if (!test_range_bit(tree
, em
->start
,
2905 extent_map_end(em
) - 1,
2906 EXTENT_LOCKED
| EXTENT_WRITEBACK
,
2908 remove_extent_mapping(map
, em
);
2909 /* once for the rb tree */
2910 free_extent_map(em
);
2912 start
= extent_map_end(em
);
2913 write_unlock(&map
->lock
);
2916 free_extent_map(em
);
2919 return try_release_extent_state(map
, tree
, page
, mask
);
2922 sector_t
extent_bmap(struct address_space
*mapping
, sector_t iblock
,
2923 get_extent_t
*get_extent
)
2925 struct inode
*inode
= mapping
->host
;
2926 struct extent_state
*cached_state
= NULL
;
2927 u64 start
= iblock
<< inode
->i_blkbits
;
2928 sector_t sector
= 0;
2929 size_t blksize
= (1 << inode
->i_blkbits
);
2930 struct extent_map
*em
;
2932 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, start
, start
+ blksize
- 1,
2933 0, &cached_state
, GFP_NOFS
);
2934 em
= get_extent(inode
, NULL
, 0, start
, blksize
, 0);
2935 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, start
,
2936 start
+ blksize
- 1, &cached_state
, GFP_NOFS
);
2937 if (!em
|| IS_ERR(em
))
2940 if (em
->block_start
> EXTENT_MAP_LAST_BYTE
)
2943 sector
= (em
->block_start
+ start
- em
->start
) >> inode
->i_blkbits
;
2945 free_extent_map(em
);
2949 int extent_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
2950 __u64 start
, __u64 len
, get_extent_t
*get_extent
)
2954 u64 max
= start
+ len
;
2957 struct extent_map
*em
= NULL
;
2958 struct extent_state
*cached_state
= NULL
;
2960 u64 em_start
= 0, em_len
= 0;
2961 unsigned long emflags
;
2967 lock_extent_bits(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
, 0,
2968 &cached_state
, GFP_NOFS
);
2969 em
= get_extent(inode
, NULL
, 0, off
, max
- off
, 0);
2977 off
= em
->start
+ em
->len
;
2981 em_start
= em
->start
;
2987 if (em
->block_start
== EXTENT_MAP_LAST_BYTE
) {
2989 flags
|= FIEMAP_EXTENT_LAST
;
2990 } else if (em
->block_start
== EXTENT_MAP_HOLE
) {
2991 flags
|= FIEMAP_EXTENT_UNWRITTEN
;
2992 } else if (em
->block_start
== EXTENT_MAP_INLINE
) {
2993 flags
|= (FIEMAP_EXTENT_DATA_INLINE
|
2994 FIEMAP_EXTENT_NOT_ALIGNED
);
2995 } else if (em
->block_start
== EXTENT_MAP_DELALLOC
) {
2996 flags
|= (FIEMAP_EXTENT_DELALLOC
|
2997 FIEMAP_EXTENT_UNKNOWN
);
2999 disko
= em
->block_start
;
3001 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
))
3002 flags
|= FIEMAP_EXTENT_ENCODED
;
3004 emflags
= em
->flags
;
3005 free_extent_map(em
);
3009 em
= get_extent(inode
, NULL
, 0, off
, max
- off
, 0);
3016 emflags
= em
->flags
;
3018 if (test_bit(EXTENT_FLAG_VACANCY
, &emflags
)) {
3019 flags
|= FIEMAP_EXTENT_LAST
;
3023 ret
= fiemap_fill_next_extent(fieinfo
, em_start
, disko
,
3029 free_extent_map(em
);
3031 unlock_extent_cached(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
,
3032 &cached_state
, GFP_NOFS
);
3036 static inline struct page
*extent_buffer_page(struct extent_buffer
*eb
,
3040 struct address_space
*mapping
;
3043 return eb
->first_page
;
3044 i
+= eb
->start
>> PAGE_CACHE_SHIFT
;
3045 mapping
= eb
->first_page
->mapping
;
3050 * extent_buffer_page is only called after pinning the page
3051 * by increasing the reference count. So we know the page must
3052 * be in the radix tree.
3055 p
= radix_tree_lookup(&mapping
->page_tree
, i
);
3061 static inline unsigned long num_extent_pages(u64 start
, u64 len
)
3063 return ((start
+ len
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
) -
3064 (start
>> PAGE_CACHE_SHIFT
);
3067 static struct extent_buffer
*__alloc_extent_buffer(struct extent_io_tree
*tree
,
3072 struct extent_buffer
*eb
= NULL
;
3074 unsigned long flags
;
3077 eb
= kmem_cache_zalloc(extent_buffer_cache
, mask
);
3080 spin_lock_init(&eb
->lock
);
3081 init_waitqueue_head(&eb
->lock_wq
);
3084 spin_lock_irqsave(&leak_lock
, flags
);
3085 list_add(&eb
->leak_list
, &buffers
);
3086 spin_unlock_irqrestore(&leak_lock
, flags
);
3088 atomic_set(&eb
->refs
, 1);
3093 static void __free_extent_buffer(struct extent_buffer
*eb
)
3096 unsigned long flags
;
3097 spin_lock_irqsave(&leak_lock
, flags
);
3098 list_del(&eb
->leak_list
);
3099 spin_unlock_irqrestore(&leak_lock
, flags
);
3101 kmem_cache_free(extent_buffer_cache
, eb
);
3104 struct extent_buffer
*alloc_extent_buffer(struct extent_io_tree
*tree
,
3105 u64 start
, unsigned long len
,
3109 unsigned long num_pages
= num_extent_pages(start
, len
);
3111 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
3112 struct extent_buffer
*eb
;
3113 struct extent_buffer
*exists
= NULL
;
3115 struct address_space
*mapping
= tree
->mapping
;
3118 spin_lock(&tree
->buffer_lock
);
3119 eb
= buffer_search(tree
, start
);
3121 atomic_inc(&eb
->refs
);
3122 spin_unlock(&tree
->buffer_lock
);
3123 mark_page_accessed(eb
->first_page
);
3126 spin_unlock(&tree
->buffer_lock
);
3128 eb
= __alloc_extent_buffer(tree
, start
, len
, mask
);
3133 eb
->first_page
= page0
;
3136 page_cache_get(page0
);
3137 mark_page_accessed(page0
);
3138 set_page_extent_mapped(page0
);
3139 set_page_extent_head(page0
, len
);
3140 uptodate
= PageUptodate(page0
);
3144 for (; i
< num_pages
; i
++, index
++) {
3145 p
= find_or_create_page(mapping
, index
, mask
| __GFP_HIGHMEM
);
3150 set_page_extent_mapped(p
);
3151 mark_page_accessed(p
);
3154 set_page_extent_head(p
, len
);
3156 set_page_private(p
, EXTENT_PAGE_PRIVATE
);
3158 if (!PageUptodate(p
))
3163 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3165 spin_lock(&tree
->buffer_lock
);
3166 exists
= buffer_tree_insert(tree
, start
, &eb
->rb_node
);
3168 /* add one reference for the caller */
3169 atomic_inc(&exists
->refs
);
3170 spin_unlock(&tree
->buffer_lock
);
3173 /* add one reference for the tree */
3174 atomic_inc(&eb
->refs
);
3175 spin_unlock(&tree
->buffer_lock
);
3179 if (!atomic_dec_and_test(&eb
->refs
))
3181 for (index
= 1; index
< i
; index
++)
3182 page_cache_release(extent_buffer_page(eb
, index
));
3183 page_cache_release(extent_buffer_page(eb
, 0));
3184 __free_extent_buffer(eb
);
3188 struct extent_buffer
*find_extent_buffer(struct extent_io_tree
*tree
,
3189 u64 start
, unsigned long len
,
3192 struct extent_buffer
*eb
;
3194 spin_lock(&tree
->buffer_lock
);
3195 eb
= buffer_search(tree
, start
);
3197 atomic_inc(&eb
->refs
);
3198 spin_unlock(&tree
->buffer_lock
);
3201 mark_page_accessed(eb
->first_page
);
3206 void free_extent_buffer(struct extent_buffer
*eb
)
3211 if (!atomic_dec_and_test(&eb
->refs
))
3217 int clear_extent_buffer_dirty(struct extent_io_tree
*tree
,
3218 struct extent_buffer
*eb
)
3221 unsigned long num_pages
;
3224 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3226 for (i
= 0; i
< num_pages
; i
++) {
3227 page
= extent_buffer_page(eb
, i
);
3228 if (!PageDirty(page
))
3233 set_page_extent_head(page
, eb
->len
);
3235 set_page_private(page
, EXTENT_PAGE_PRIVATE
);
3237 clear_page_dirty_for_io(page
);
3238 spin_lock_irq(&page
->mapping
->tree_lock
);
3239 if (!PageDirty(page
)) {
3240 radix_tree_tag_clear(&page
->mapping
->page_tree
,
3242 PAGECACHE_TAG_DIRTY
);
3244 spin_unlock_irq(&page
->mapping
->tree_lock
);
3250 int wait_on_extent_buffer_writeback(struct extent_io_tree
*tree
,
3251 struct extent_buffer
*eb
)
3253 return wait_on_extent_writeback(tree
, eb
->start
,
3254 eb
->start
+ eb
->len
- 1);
3257 int set_extent_buffer_dirty(struct extent_io_tree
*tree
,
3258 struct extent_buffer
*eb
)
3261 unsigned long num_pages
;
3264 was_dirty
= test_and_set_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
);
3265 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3266 for (i
= 0; i
< num_pages
; i
++)
3267 __set_page_dirty_nobuffers(extent_buffer_page(eb
, i
));
3271 int clear_extent_buffer_uptodate(struct extent_io_tree
*tree
,
3272 struct extent_buffer
*eb
,
3273 struct extent_state
**cached_state
)
3277 unsigned long num_pages
;
3279 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3280 clear_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3282 clear_extent_uptodate(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3283 cached_state
, GFP_NOFS
);
3284 for (i
= 0; i
< num_pages
; i
++) {
3285 page
= extent_buffer_page(eb
, i
);
3287 ClearPageUptodate(page
);
3292 int set_extent_buffer_uptodate(struct extent_io_tree
*tree
,
3293 struct extent_buffer
*eb
)
3297 unsigned long num_pages
;
3299 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3301 set_extent_uptodate(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3303 for (i
= 0; i
< num_pages
; i
++) {
3304 page
= extent_buffer_page(eb
, i
);
3305 if ((i
== 0 && (eb
->start
& (PAGE_CACHE_SIZE
- 1))) ||
3306 ((i
== num_pages
- 1) &&
3307 ((eb
->start
+ eb
->len
) & (PAGE_CACHE_SIZE
- 1)))) {
3308 check_page_uptodate(tree
, page
);
3311 SetPageUptodate(page
);
3316 int extent_range_uptodate(struct extent_io_tree
*tree
,
3321 int pg_uptodate
= 1;
3323 unsigned long index
;
3325 ret
= test_range_bit(tree
, start
, end
, EXTENT_UPTODATE
, 1, NULL
);
3328 while (start
<= end
) {
3329 index
= start
>> PAGE_CACHE_SHIFT
;
3330 page
= find_get_page(tree
->mapping
, index
);
3331 uptodate
= PageUptodate(page
);
3332 page_cache_release(page
);
3337 start
+= PAGE_CACHE_SIZE
;
3342 int extent_buffer_uptodate(struct extent_io_tree
*tree
,
3343 struct extent_buffer
*eb
,
3344 struct extent_state
*cached_state
)
3347 unsigned long num_pages
;
3350 int pg_uptodate
= 1;
3352 if (test_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
))
3355 ret
= test_range_bit(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3356 EXTENT_UPTODATE
, 1, cached_state
);
3360 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3361 for (i
= 0; i
< num_pages
; i
++) {
3362 page
= extent_buffer_page(eb
, i
);
3363 if (!PageUptodate(page
)) {
3371 int read_extent_buffer_pages(struct extent_io_tree
*tree
,
3372 struct extent_buffer
*eb
,
3373 u64 start
, int wait
,
3374 get_extent_t
*get_extent
, int mirror_num
)
3377 unsigned long start_i
;
3381 int locked_pages
= 0;
3382 int all_uptodate
= 1;
3383 int inc_all_pages
= 0;
3384 unsigned long num_pages
;
3385 struct bio
*bio
= NULL
;
3386 unsigned long bio_flags
= 0;
3388 if (test_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
))
3391 if (test_range_bit(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3392 EXTENT_UPTODATE
, 1, NULL
)) {
3397 WARN_ON(start
< eb
->start
);
3398 start_i
= (start
>> PAGE_CACHE_SHIFT
) -
3399 (eb
->start
>> PAGE_CACHE_SHIFT
);
3404 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3405 for (i
= start_i
; i
< num_pages
; i
++) {
3406 page
= extent_buffer_page(eb
, i
);
3408 if (!trylock_page(page
))
3414 if (!PageUptodate(page
))
3419 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3423 for (i
= start_i
; i
< num_pages
; i
++) {
3424 page
= extent_buffer_page(eb
, i
);
3426 page_cache_get(page
);
3427 if (!PageUptodate(page
)) {
3430 ClearPageError(page
);
3431 err
= __extent_read_full_page(tree
, page
,
3433 mirror_num
, &bio_flags
);
3442 submit_one_bio(READ
, bio
, mirror_num
, bio_flags
);
3447 for (i
= start_i
; i
< num_pages
; i
++) {
3448 page
= extent_buffer_page(eb
, i
);
3449 wait_on_page_locked(page
);
3450 if (!PageUptodate(page
))
3455 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3460 while (locked_pages
> 0) {
3461 page
= extent_buffer_page(eb
, i
);
3469 void read_extent_buffer(struct extent_buffer
*eb
, void *dstv
,
3470 unsigned long start
,
3477 char *dst
= (char *)dstv
;
3478 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3479 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3481 WARN_ON(start
> eb
->len
);
3482 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3484 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3487 page
= extent_buffer_page(eb
, i
);
3489 cur
= min(len
, (PAGE_CACHE_SIZE
- offset
));
3490 kaddr
= kmap_atomic(page
, KM_USER1
);
3491 memcpy(dst
, kaddr
+ offset
, cur
);
3492 kunmap_atomic(kaddr
, KM_USER1
);
3501 int map_private_extent_buffer(struct extent_buffer
*eb
, unsigned long start
,
3502 unsigned long min_len
, char **token
, char **map
,
3503 unsigned long *map_start
,
3504 unsigned long *map_len
, int km
)
3506 size_t offset
= start
& (PAGE_CACHE_SIZE
- 1);
3509 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3510 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3511 unsigned long end_i
= (start_offset
+ start
+ min_len
- 1) >>
3518 offset
= start_offset
;
3522 *map_start
= ((u64
)i
<< PAGE_CACHE_SHIFT
) - start_offset
;
3525 if (start
+ min_len
> eb
->len
) {
3526 printk(KERN_ERR
"btrfs bad mapping eb start %llu len %lu, "
3527 "wanted %lu %lu\n", (unsigned long long)eb
->start
,
3528 eb
->len
, start
, min_len
);
3532 p
= extent_buffer_page(eb
, i
);
3533 kaddr
= kmap_atomic(p
, km
);
3535 *map
= kaddr
+ offset
;
3536 *map_len
= PAGE_CACHE_SIZE
- offset
;
3540 int map_extent_buffer(struct extent_buffer
*eb
, unsigned long start
,
3541 unsigned long min_len
,
3542 char **token
, char **map
,
3543 unsigned long *map_start
,
3544 unsigned long *map_len
, int km
)
3548 if (eb
->map_token
) {
3549 unmap_extent_buffer(eb
, eb
->map_token
, km
);
3550 eb
->map_token
= NULL
;
3553 err
= map_private_extent_buffer(eb
, start
, min_len
, token
, map
,
3554 map_start
, map_len
, km
);
3556 eb
->map_token
= *token
;
3558 eb
->map_start
= *map_start
;
3559 eb
->map_len
= *map_len
;
3564 void unmap_extent_buffer(struct extent_buffer
*eb
, char *token
, int km
)
3566 kunmap_atomic(token
, km
);
3569 int memcmp_extent_buffer(struct extent_buffer
*eb
, const void *ptrv
,
3570 unsigned long start
,
3577 char *ptr
= (char *)ptrv
;
3578 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3579 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3582 WARN_ON(start
> eb
->len
);
3583 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3585 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3588 page
= extent_buffer_page(eb
, i
);
3590 cur
= min(len
, (PAGE_CACHE_SIZE
- offset
));
3592 kaddr
= kmap_atomic(page
, KM_USER0
);
3593 ret
= memcmp(ptr
, kaddr
+ offset
, cur
);
3594 kunmap_atomic(kaddr
, KM_USER0
);
3606 void write_extent_buffer(struct extent_buffer
*eb
, const void *srcv
,
3607 unsigned long start
, unsigned long len
)
3613 char *src
= (char *)srcv
;
3614 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3615 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3617 WARN_ON(start
> eb
->len
);
3618 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3620 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3623 page
= extent_buffer_page(eb
, i
);
3624 WARN_ON(!PageUptodate(page
));
3626 cur
= min(len
, PAGE_CACHE_SIZE
- offset
);
3627 kaddr
= kmap_atomic(page
, KM_USER1
);
3628 memcpy(kaddr
+ offset
, src
, cur
);
3629 kunmap_atomic(kaddr
, KM_USER1
);
3638 void memset_extent_buffer(struct extent_buffer
*eb
, char c
,
3639 unsigned long start
, unsigned long len
)
3645 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3646 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3648 WARN_ON(start
> eb
->len
);
3649 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3651 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3654 page
= extent_buffer_page(eb
, i
);
3655 WARN_ON(!PageUptodate(page
));
3657 cur
= min(len
, PAGE_CACHE_SIZE
- offset
);
3658 kaddr
= kmap_atomic(page
, KM_USER0
);
3659 memset(kaddr
+ offset
, c
, cur
);
3660 kunmap_atomic(kaddr
, KM_USER0
);
3668 void copy_extent_buffer(struct extent_buffer
*dst
, struct extent_buffer
*src
,
3669 unsigned long dst_offset
, unsigned long src_offset
,
3672 u64 dst_len
= dst
->len
;
3677 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3678 unsigned long i
= (start_offset
+ dst_offset
) >> PAGE_CACHE_SHIFT
;
3680 WARN_ON(src
->len
!= dst_len
);
3682 offset
= (start_offset
+ dst_offset
) &
3683 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3686 page
= extent_buffer_page(dst
, i
);
3687 WARN_ON(!PageUptodate(page
));
3689 cur
= min(len
, (unsigned long)(PAGE_CACHE_SIZE
- offset
));
3691 kaddr
= kmap_atomic(page
, KM_USER0
);
3692 read_extent_buffer(src
, kaddr
+ offset
, src_offset
, cur
);
3693 kunmap_atomic(kaddr
, KM_USER0
);
3702 static void move_pages(struct page
*dst_page
, struct page
*src_page
,
3703 unsigned long dst_off
, unsigned long src_off
,
3706 char *dst_kaddr
= kmap_atomic(dst_page
, KM_USER0
);
3707 if (dst_page
== src_page
) {
3708 memmove(dst_kaddr
+ dst_off
, dst_kaddr
+ src_off
, len
);
3710 char *src_kaddr
= kmap_atomic(src_page
, KM_USER1
);
3711 char *p
= dst_kaddr
+ dst_off
+ len
;
3712 char *s
= src_kaddr
+ src_off
+ len
;
3717 kunmap_atomic(src_kaddr
, KM_USER1
);
3719 kunmap_atomic(dst_kaddr
, KM_USER0
);
3722 static void copy_pages(struct page
*dst_page
, struct page
*src_page
,
3723 unsigned long dst_off
, unsigned long src_off
,
3726 char *dst_kaddr
= kmap_atomic(dst_page
, KM_USER0
);
3729 if (dst_page
!= src_page
)
3730 src_kaddr
= kmap_atomic(src_page
, KM_USER1
);
3732 src_kaddr
= dst_kaddr
;
3734 memcpy(dst_kaddr
+ dst_off
, src_kaddr
+ src_off
, len
);
3735 kunmap_atomic(dst_kaddr
, KM_USER0
);
3736 if (dst_page
!= src_page
)
3737 kunmap_atomic(src_kaddr
, KM_USER1
);
3740 void memcpy_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
3741 unsigned long src_offset
, unsigned long len
)
3744 size_t dst_off_in_page
;
3745 size_t src_off_in_page
;
3746 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3747 unsigned long dst_i
;
3748 unsigned long src_i
;
3750 if (src_offset
+ len
> dst
->len
) {
3751 printk(KERN_ERR
"btrfs memmove bogus src_offset %lu move "
3752 "len %lu dst len %lu\n", src_offset
, len
, dst
->len
);
3755 if (dst_offset
+ len
> dst
->len
) {
3756 printk(KERN_ERR
"btrfs memmove bogus dst_offset %lu move "
3757 "len %lu dst len %lu\n", dst_offset
, len
, dst
->len
);
3762 dst_off_in_page
= (start_offset
+ dst_offset
) &
3763 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3764 src_off_in_page
= (start_offset
+ src_offset
) &
3765 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3767 dst_i
= (start_offset
+ dst_offset
) >> PAGE_CACHE_SHIFT
;
3768 src_i
= (start_offset
+ src_offset
) >> PAGE_CACHE_SHIFT
;
3770 cur
= min(len
, (unsigned long)(PAGE_CACHE_SIZE
-
3772 cur
= min_t(unsigned long, cur
,
3773 (unsigned long)(PAGE_CACHE_SIZE
- dst_off_in_page
));
3775 copy_pages(extent_buffer_page(dst
, dst_i
),
3776 extent_buffer_page(dst
, src_i
),
3777 dst_off_in_page
, src_off_in_page
, cur
);
3785 void memmove_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
3786 unsigned long src_offset
, unsigned long len
)
3789 size_t dst_off_in_page
;
3790 size_t src_off_in_page
;
3791 unsigned long dst_end
= dst_offset
+ len
- 1;
3792 unsigned long src_end
= src_offset
+ len
- 1;
3793 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3794 unsigned long dst_i
;
3795 unsigned long src_i
;
3797 if (src_offset
+ len
> dst
->len
) {
3798 printk(KERN_ERR
"btrfs memmove bogus src_offset %lu move "
3799 "len %lu len %lu\n", src_offset
, len
, dst
->len
);
3802 if (dst_offset
+ len
> dst
->len
) {
3803 printk(KERN_ERR
"btrfs memmove bogus dst_offset %lu move "
3804 "len %lu len %lu\n", dst_offset
, len
, dst
->len
);
3807 if (dst_offset
< src_offset
) {
3808 memcpy_extent_buffer(dst
, dst_offset
, src_offset
, len
);
3812 dst_i
= (start_offset
+ dst_end
) >> PAGE_CACHE_SHIFT
;
3813 src_i
= (start_offset
+ src_end
) >> PAGE_CACHE_SHIFT
;
3815 dst_off_in_page
= (start_offset
+ dst_end
) &
3816 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3817 src_off_in_page
= (start_offset
+ src_end
) &
3818 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3820 cur
= min_t(unsigned long, len
, src_off_in_page
+ 1);
3821 cur
= min(cur
, dst_off_in_page
+ 1);
3822 move_pages(extent_buffer_page(dst
, dst_i
),
3823 extent_buffer_page(dst
, src_i
),
3824 dst_off_in_page
- cur
+ 1,
3825 src_off_in_page
- cur
+ 1, cur
);
3833 int try_release_extent_buffer(struct extent_io_tree
*tree
, struct page
*page
)
3835 u64 start
= page_offset(page
);
3836 struct extent_buffer
*eb
;
3839 unsigned long num_pages
;
3841 spin_lock(&tree
->buffer_lock
);
3842 eb
= buffer_search(tree
, start
);
3846 if (atomic_read(&eb
->refs
) > 1) {
3850 if (test_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
)) {
3854 /* at this point we can safely release the extent buffer */
3855 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3856 for (i
= 0; i
< num_pages
; i
++)
3857 page_cache_release(extent_buffer_page(eb
, i
));
3858 rb_erase(&eb
->rb_node
, &tree
->buffer
);
3859 __free_extent_buffer(eb
);
3861 spin_unlock(&tree
->buffer_lock
);