1 #include <linux/bitops.h>
2 #include <linux/slab.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"
18 #include "btrfs_inode.h"
20 /* temporary define until extent_map moves out of btrfs */
21 struct kmem_cache
*btrfs_cache_create(const char *name
, size_t size
,
22 unsigned long extra_flags
,
23 void (*ctor
)(void *, struct kmem_cache
*,
26 static struct kmem_cache
*extent_state_cache
;
27 static struct kmem_cache
*extent_buffer_cache
;
29 static LIST_HEAD(buffers
);
30 static LIST_HEAD(states
);
34 static DEFINE_SPINLOCK(leak_lock
);
37 #define BUFFER_LRU_MAX 64
42 struct rb_node rb_node
;
45 struct extent_page_data
{
47 struct extent_io_tree
*tree
;
48 get_extent_t
*get_extent
;
50 /* tells writepage not to lock the state bits for this range
51 * it still does the unlocking
53 unsigned int extent_locked
:1;
55 /* tells the submit_bio code to use a WRITE_SYNC */
56 unsigned int sync_io
:1;
59 int __init
extent_io_init(void)
61 extent_state_cache
= btrfs_cache_create("extent_state",
62 sizeof(struct extent_state
), 0,
64 if (!extent_state_cache
)
67 extent_buffer_cache
= btrfs_cache_create("extent_buffers",
68 sizeof(struct extent_buffer
), 0,
70 if (!extent_buffer_cache
)
71 goto free_state_cache
;
75 kmem_cache_destroy(extent_state_cache
);
79 void extent_io_exit(void)
81 struct extent_state
*state
;
82 struct extent_buffer
*eb
;
84 while (!list_empty(&states
)) {
85 state
= list_entry(states
.next
, struct extent_state
, leak_list
);
86 printk(KERN_ERR
"btrfs state leak: start %llu end %llu "
87 "state %lu in tree %p refs %d\n",
88 (unsigned long long)state
->start
,
89 (unsigned long long)state
->end
,
90 state
->state
, state
->tree
, atomic_read(&state
->refs
));
91 list_del(&state
->leak_list
);
92 kmem_cache_free(extent_state_cache
, state
);
96 while (!list_empty(&buffers
)) {
97 eb
= list_entry(buffers
.next
, struct extent_buffer
, leak_list
);
98 printk(KERN_ERR
"btrfs buffer leak start %llu len %lu "
99 "refs %d\n", (unsigned long long)eb
->start
,
100 eb
->len
, atomic_read(&eb
->refs
));
101 list_del(&eb
->leak_list
);
102 kmem_cache_free(extent_buffer_cache
, eb
);
104 if (extent_state_cache
)
105 kmem_cache_destroy(extent_state_cache
);
106 if (extent_buffer_cache
)
107 kmem_cache_destroy(extent_buffer_cache
);
110 void extent_io_tree_init(struct extent_io_tree
*tree
,
111 struct address_space
*mapping
, gfp_t mask
)
113 tree
->state
.rb_node
= NULL
;
114 tree
->buffer
.rb_node
= NULL
;
116 tree
->dirty_bytes
= 0;
117 spin_lock_init(&tree
->lock
);
118 spin_lock_init(&tree
->buffer_lock
);
119 tree
->mapping
= mapping
;
122 static struct extent_state
*alloc_extent_state(gfp_t mask
)
124 struct extent_state
*state
;
129 state
= kmem_cache_alloc(extent_state_cache
, mask
);
136 spin_lock_irqsave(&leak_lock
, flags
);
137 list_add(&state
->leak_list
, &states
);
138 spin_unlock_irqrestore(&leak_lock
, flags
);
140 atomic_set(&state
->refs
, 1);
141 init_waitqueue_head(&state
->wq
);
145 static void free_extent_state(struct extent_state
*state
)
149 if (atomic_dec_and_test(&state
->refs
)) {
153 WARN_ON(state
->tree
);
155 spin_lock_irqsave(&leak_lock
, flags
);
156 list_del(&state
->leak_list
);
157 spin_unlock_irqrestore(&leak_lock
, flags
);
159 kmem_cache_free(extent_state_cache
, state
);
163 static struct rb_node
*tree_insert(struct rb_root
*root
, u64 offset
,
164 struct rb_node
*node
)
166 struct rb_node
**p
= &root
->rb_node
;
167 struct rb_node
*parent
= NULL
;
168 struct tree_entry
*entry
;
172 entry
= rb_entry(parent
, struct tree_entry
, rb_node
);
174 if (offset
< entry
->start
)
176 else if (offset
> entry
->end
)
182 entry
= rb_entry(node
, struct tree_entry
, rb_node
);
183 rb_link_node(node
, parent
, p
);
184 rb_insert_color(node
, root
);
188 static struct rb_node
*__etree_search(struct extent_io_tree
*tree
, u64 offset
,
189 struct rb_node
**prev_ret
,
190 struct rb_node
**next_ret
)
192 struct rb_root
*root
= &tree
->state
;
193 struct rb_node
*n
= root
->rb_node
;
194 struct rb_node
*prev
= NULL
;
195 struct rb_node
*orig_prev
= NULL
;
196 struct tree_entry
*entry
;
197 struct tree_entry
*prev_entry
= NULL
;
200 entry
= rb_entry(n
, struct tree_entry
, rb_node
);
204 if (offset
< entry
->start
)
206 else if (offset
> entry
->end
)
214 while (prev
&& offset
> prev_entry
->end
) {
215 prev
= rb_next(prev
);
216 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
223 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
224 while (prev
&& offset
< prev_entry
->start
) {
225 prev
= rb_prev(prev
);
226 prev_entry
= rb_entry(prev
, struct tree_entry
, rb_node
);
233 static inline struct rb_node
*tree_search(struct extent_io_tree
*tree
,
236 struct rb_node
*prev
= NULL
;
239 ret
= __etree_search(tree
, offset
, &prev
, NULL
);
245 static struct extent_buffer
*buffer_tree_insert(struct extent_io_tree
*tree
,
246 u64 offset
, struct rb_node
*node
)
248 struct rb_root
*root
= &tree
->buffer
;
249 struct rb_node
**p
= &root
->rb_node
;
250 struct rb_node
*parent
= NULL
;
251 struct extent_buffer
*eb
;
255 eb
= rb_entry(parent
, struct extent_buffer
, rb_node
);
257 if (offset
< eb
->start
)
259 else if (offset
> eb
->start
)
265 rb_link_node(node
, parent
, p
);
266 rb_insert_color(node
, root
);
270 static struct extent_buffer
*buffer_search(struct extent_io_tree
*tree
,
273 struct rb_root
*root
= &tree
->buffer
;
274 struct rb_node
*n
= root
->rb_node
;
275 struct extent_buffer
*eb
;
278 eb
= rb_entry(n
, struct extent_buffer
, rb_node
);
279 if (offset
< eb
->start
)
281 else if (offset
> eb
->start
)
290 * utility function to look for merge candidates inside a given range.
291 * Any extents with matching state are merged together into a single
292 * extent in the tree. Extents with EXTENT_IO in their state field
293 * are not merged because the end_io handlers need to be able to do
294 * operations on them without sleeping (or doing allocations/splits).
296 * This should be called with the tree lock held.
298 static int merge_state(struct extent_io_tree
*tree
,
299 struct extent_state
*state
)
301 struct extent_state
*other
;
302 struct rb_node
*other_node
;
304 if (state
->state
& (EXTENT_IOBITS
| EXTENT_BOUNDARY
))
307 other_node
= rb_prev(&state
->rb_node
);
309 other
= rb_entry(other_node
, struct extent_state
, rb_node
);
310 if (other
->end
== state
->start
- 1 &&
311 other
->state
== state
->state
) {
312 state
->start
= other
->start
;
314 rb_erase(&other
->rb_node
, &tree
->state
);
315 free_extent_state(other
);
318 other_node
= rb_next(&state
->rb_node
);
320 other
= rb_entry(other_node
, struct extent_state
, rb_node
);
321 if (other
->start
== state
->end
+ 1 &&
322 other
->state
== state
->state
) {
323 other
->start
= state
->start
;
325 rb_erase(&state
->rb_node
, &tree
->state
);
326 free_extent_state(state
);
332 static void set_state_cb(struct extent_io_tree
*tree
,
333 struct extent_state
*state
,
336 if (tree
->ops
&& tree
->ops
->set_bit_hook
) {
337 tree
->ops
->set_bit_hook(tree
->mapping
->host
, state
->start
,
338 state
->end
, state
->state
, bits
);
342 static void clear_state_cb(struct extent_io_tree
*tree
,
343 struct extent_state
*state
,
346 if (tree
->ops
&& tree
->ops
->clear_bit_hook
) {
347 tree
->ops
->clear_bit_hook(tree
->mapping
->host
, state
->start
,
348 state
->end
, state
->state
, bits
);
353 * insert an extent_state struct into the tree. 'bits' are set on the
354 * struct before it is inserted.
356 * This may return -EEXIST if the extent is already there, in which case the
357 * state struct is freed.
359 * The tree lock is not taken internally. This is a utility function and
360 * probably isn't what you want to call (see set/clear_extent_bit).
362 static int insert_state(struct extent_io_tree
*tree
,
363 struct extent_state
*state
, u64 start
, u64 end
,
366 struct rb_node
*node
;
369 printk(KERN_ERR
"btrfs end < start %llu %llu\n",
370 (unsigned long long)end
,
371 (unsigned long long)start
);
374 if (bits
& EXTENT_DIRTY
)
375 tree
->dirty_bytes
+= end
- start
+ 1;
376 set_state_cb(tree
, state
, bits
);
377 state
->state
|= bits
;
378 state
->start
= start
;
380 node
= tree_insert(&tree
->state
, end
, &state
->rb_node
);
382 struct extent_state
*found
;
383 found
= rb_entry(node
, struct extent_state
, rb_node
);
384 printk(KERN_ERR
"btrfs found node %llu %llu on insert of "
385 "%llu %llu\n", (unsigned long long)found
->start
,
386 (unsigned long long)found
->end
,
387 (unsigned long long)start
, (unsigned long long)end
);
388 free_extent_state(state
);
392 merge_state(tree
, state
);
397 * split a given extent state struct in two, inserting the preallocated
398 * struct 'prealloc' as the newly created second half. 'split' indicates an
399 * offset inside 'orig' where it should be split.
402 * the tree has 'orig' at [orig->start, orig->end]. After calling, there
403 * are two extent state structs in the tree:
404 * prealloc: [orig->start, split - 1]
405 * orig: [ split, orig->end ]
407 * The tree locks are not taken by this function. They need to be held
410 static int split_state(struct extent_io_tree
*tree
, struct extent_state
*orig
,
411 struct extent_state
*prealloc
, u64 split
)
413 struct rb_node
*node
;
414 prealloc
->start
= orig
->start
;
415 prealloc
->end
= split
- 1;
416 prealloc
->state
= orig
->state
;
419 node
= tree_insert(&tree
->state
, prealloc
->end
, &prealloc
->rb_node
);
421 free_extent_state(prealloc
);
424 prealloc
->tree
= tree
;
429 * utility function to clear some bits in an extent state struct.
430 * it will optionally wake up any one waiting on this state (wake == 1), or
431 * forcibly remove the state from the tree (delete == 1).
433 * If no bits are set on the state struct after clearing things, the
434 * struct is freed and removed from the tree
436 static int clear_state_bit(struct extent_io_tree
*tree
,
437 struct extent_state
*state
, int bits
, int wake
,
440 int ret
= state
->state
& bits
;
442 if ((bits
& EXTENT_DIRTY
) && (state
->state
& EXTENT_DIRTY
)) {
443 u64 range
= state
->end
- state
->start
+ 1;
444 WARN_ON(range
> tree
->dirty_bytes
);
445 tree
->dirty_bytes
-= range
;
447 clear_state_cb(tree
, state
, bits
);
448 state
->state
&= ~bits
;
451 if (delete || state
->state
== 0) {
453 clear_state_cb(tree
, state
, state
->state
);
454 rb_erase(&state
->rb_node
, &tree
->state
);
456 free_extent_state(state
);
461 merge_state(tree
, state
);
467 * clear some bits on a range in the tree. This may require splitting
468 * or inserting elements in the tree, so the gfp mask is used to
469 * indicate which allocations or sleeping are allowed.
471 * pass 'wake' == 1 to kick any sleepers, and 'delete' == 1 to remove
472 * the given range from the tree regardless of state (ie for truncate).
474 * the range [start, end] is inclusive.
476 * This takes the tree lock, and returns < 0 on error, > 0 if any of the
477 * bits were already set, or zero if none of the bits were already set.
479 int clear_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
480 int bits
, int wake
, int delete, gfp_t mask
)
482 struct extent_state
*state
;
483 struct extent_state
*prealloc
= NULL
;
484 struct rb_node
*node
;
489 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
490 prealloc
= alloc_extent_state(mask
);
495 spin_lock(&tree
->lock
);
497 * this search will find the extents that end after
500 node
= tree_search(tree
, start
);
503 state
= rb_entry(node
, struct extent_state
, rb_node
);
504 if (state
->start
> end
)
506 WARN_ON(state
->end
< start
);
509 * | ---- desired range ---- |
511 * | ------------- state -------------- |
513 * We need to split the extent we found, and may flip
514 * bits on second half.
516 * If the extent we found extends past our range, we
517 * just split and search again. It'll get split again
518 * the next time though.
520 * If the extent we found is inside our range, we clear
521 * the desired bit on it.
524 if (state
->start
< start
) {
526 prealloc
= alloc_extent_state(GFP_ATOMIC
);
527 err
= split_state(tree
, state
, prealloc
, start
);
528 BUG_ON(err
== -EEXIST
);
532 if (state
->end
<= end
) {
533 start
= state
->end
+ 1;
534 set
|= clear_state_bit(tree
, state
, bits
,
537 start
= state
->start
;
542 * | ---- desired range ---- |
544 * We need to split the extent, and clear the bit
547 if (state
->start
<= end
&& state
->end
> end
) {
549 prealloc
= alloc_extent_state(GFP_ATOMIC
);
550 err
= split_state(tree
, state
, prealloc
, end
+ 1);
551 BUG_ON(err
== -EEXIST
);
555 set
|= clear_state_bit(tree
, prealloc
, bits
,
561 start
= state
->end
+ 1;
562 set
|= clear_state_bit(tree
, state
, bits
, wake
, delete);
566 spin_unlock(&tree
->lock
);
568 free_extent_state(prealloc
);
575 spin_unlock(&tree
->lock
);
576 if (mask
& __GFP_WAIT
)
581 static int wait_on_state(struct extent_io_tree
*tree
,
582 struct extent_state
*state
)
583 __releases(tree
->lock
)
584 __acquires(tree
->lock
)
587 prepare_to_wait(&state
->wq
, &wait
, TASK_UNINTERRUPTIBLE
);
588 spin_unlock(&tree
->lock
);
590 spin_lock(&tree
->lock
);
591 finish_wait(&state
->wq
, &wait
);
596 * waits for one or more bits to clear on a range in the state tree.
597 * The range [start, end] is inclusive.
598 * The tree lock is taken by this function
600 int wait_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
, int bits
)
602 struct extent_state
*state
;
603 struct rb_node
*node
;
605 spin_lock(&tree
->lock
);
609 * this search will find all the extents that end after
612 node
= tree_search(tree
, start
);
616 state
= rb_entry(node
, struct extent_state
, rb_node
);
618 if (state
->start
> end
)
621 if (state
->state
& bits
) {
622 start
= state
->start
;
623 atomic_inc(&state
->refs
);
624 wait_on_state(tree
, state
);
625 free_extent_state(state
);
628 start
= state
->end
+ 1;
633 if (need_resched()) {
634 spin_unlock(&tree
->lock
);
636 spin_lock(&tree
->lock
);
640 spin_unlock(&tree
->lock
);
644 static void set_state_bits(struct extent_io_tree
*tree
,
645 struct extent_state
*state
,
648 if ((bits
& EXTENT_DIRTY
) && !(state
->state
& EXTENT_DIRTY
)) {
649 u64 range
= state
->end
- state
->start
+ 1;
650 tree
->dirty_bytes
+= range
;
652 set_state_cb(tree
, state
, bits
);
653 state
->state
|= bits
;
657 * set some bits on a range in the tree. This may require allocations
658 * or sleeping, so the gfp mask is used to indicate what is allowed.
660 * If 'exclusive' == 1, this will fail with -EEXIST if some part of the
661 * range already has the desired bits set. The start of the existing
662 * range is returned in failed_start in this case.
664 * [start, end] is inclusive
665 * This takes the tree lock.
667 static int set_extent_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
668 int bits
, int exclusive
, u64
*failed_start
,
671 struct extent_state
*state
;
672 struct extent_state
*prealloc
= NULL
;
673 struct rb_node
*node
;
679 if (!prealloc
&& (mask
& __GFP_WAIT
)) {
680 prealloc
= alloc_extent_state(mask
);
685 spin_lock(&tree
->lock
);
687 * this search will find all the extents that end after
690 node
= tree_search(tree
, start
);
692 err
= insert_state(tree
, prealloc
, start
, end
, bits
);
694 BUG_ON(err
== -EEXIST
);
698 state
= rb_entry(node
, struct extent_state
, rb_node
);
699 last_start
= state
->start
;
700 last_end
= state
->end
;
703 * | ---- desired range ---- |
706 * Just lock what we found and keep going
708 if (state
->start
== start
&& state
->end
<= end
) {
709 set
= state
->state
& bits
;
710 if (set
&& exclusive
) {
711 *failed_start
= state
->start
;
715 set_state_bits(tree
, state
, bits
);
716 start
= state
->end
+ 1;
717 merge_state(tree
, state
);
722 * | ---- desired range ---- |
725 * | ------------- state -------------- |
727 * We need to split the extent we found, and may flip bits on
730 * If the extent we found extends past our
731 * range, we just split and search again. It'll get split
732 * again the next time though.
734 * If the extent we found is inside our range, we set the
737 if (state
->start
< start
) {
738 set
= state
->state
& bits
;
739 if (exclusive
&& set
) {
740 *failed_start
= start
;
744 err
= split_state(tree
, state
, prealloc
, start
);
745 BUG_ON(err
== -EEXIST
);
749 if (state
->end
<= end
) {
750 set_state_bits(tree
, state
, bits
);
751 start
= state
->end
+ 1;
752 merge_state(tree
, state
);
754 start
= state
->start
;
759 * | ---- desired range ---- |
760 * | state | or | state |
762 * There's a hole, we need to insert something in it and
763 * ignore the extent we found.
765 if (state
->start
> start
) {
767 if (end
< last_start
)
770 this_end
= last_start
- 1;
771 err
= insert_state(tree
, prealloc
, start
, this_end
,
774 BUG_ON(err
== -EEXIST
);
777 start
= this_end
+ 1;
781 * | ---- desired range ---- |
783 * We need to split the extent, and set the bit
786 if (state
->start
<= end
&& state
->end
> end
) {
787 set
= state
->state
& bits
;
788 if (exclusive
&& set
) {
789 *failed_start
= start
;
793 err
= split_state(tree
, state
, prealloc
, end
+ 1);
794 BUG_ON(err
== -EEXIST
);
796 set_state_bits(tree
, prealloc
, bits
);
797 merge_state(tree
, prealloc
);
805 spin_unlock(&tree
->lock
);
807 free_extent_state(prealloc
);
814 spin_unlock(&tree
->lock
);
815 if (mask
& __GFP_WAIT
)
820 /* wrappers around set/clear extent bit */
821 int set_extent_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
,
824 return set_extent_bit(tree
, start
, end
, EXTENT_DIRTY
, 0, NULL
,
828 int set_extent_ordered(struct extent_io_tree
*tree
, u64 start
, u64 end
,
831 return set_extent_bit(tree
, start
, end
, EXTENT_ORDERED
, 0, NULL
, mask
);
834 int set_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
835 int bits
, gfp_t mask
)
837 return set_extent_bit(tree
, start
, end
, bits
, 0, NULL
,
841 int clear_extent_bits(struct extent_io_tree
*tree
, u64 start
, u64 end
,
842 int bits
, gfp_t mask
)
844 return clear_extent_bit(tree
, start
, end
, bits
, 0, 0, mask
);
847 int set_extent_delalloc(struct extent_io_tree
*tree
, u64 start
, u64 end
,
850 return set_extent_bit(tree
, start
, end
,
851 EXTENT_DELALLOC
| EXTENT_DIRTY
,
855 int clear_extent_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
,
858 return clear_extent_bit(tree
, start
, end
,
859 EXTENT_DIRTY
| EXTENT_DELALLOC
, 0, 0, mask
);
862 int clear_extent_ordered(struct extent_io_tree
*tree
, u64 start
, u64 end
,
865 return clear_extent_bit(tree
, start
, end
, EXTENT_ORDERED
, 1, 0, mask
);
868 int set_extent_new(struct extent_io_tree
*tree
, u64 start
, u64 end
,
871 return set_extent_bit(tree
, start
, end
, EXTENT_NEW
, 0, NULL
,
875 static int clear_extent_new(struct extent_io_tree
*tree
, u64 start
, u64 end
,
878 return clear_extent_bit(tree
, start
, end
, EXTENT_NEW
, 0, 0, mask
);
881 int set_extent_uptodate(struct extent_io_tree
*tree
, u64 start
, u64 end
,
884 return set_extent_bit(tree
, start
, end
, EXTENT_UPTODATE
, 0, NULL
,
888 static int clear_extent_uptodate(struct extent_io_tree
*tree
, u64 start
,
891 return clear_extent_bit(tree
, start
, end
, EXTENT_UPTODATE
, 0, 0, mask
);
894 static int set_extent_writeback(struct extent_io_tree
*tree
, u64 start
, u64 end
,
897 return set_extent_bit(tree
, start
, end
, EXTENT_WRITEBACK
,
901 static int clear_extent_writeback(struct extent_io_tree
*tree
, u64 start
,
904 return clear_extent_bit(tree
, start
, end
, EXTENT_WRITEBACK
, 1, 0, mask
);
907 int wait_on_extent_writeback(struct extent_io_tree
*tree
, u64 start
, u64 end
)
909 return wait_extent_bit(tree
, start
, end
, EXTENT_WRITEBACK
);
913 * either insert or lock state struct between start and end use mask to tell
914 * us if waiting is desired.
916 int lock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
, gfp_t mask
)
921 err
= set_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1,
922 &failed_start
, mask
);
923 if (err
== -EEXIST
&& (mask
& __GFP_WAIT
)) {
924 wait_extent_bit(tree
, failed_start
, end
, EXTENT_LOCKED
);
925 start
= failed_start
;
929 WARN_ON(start
> end
);
934 int try_lock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
,
940 err
= set_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1,
941 &failed_start
, mask
);
942 if (err
== -EEXIST
) {
943 if (failed_start
> start
)
944 clear_extent_bit(tree
, start
, failed_start
- 1,
945 EXTENT_LOCKED
, 1, 0, mask
);
951 int unlock_extent(struct extent_io_tree
*tree
, u64 start
, u64 end
,
954 return clear_extent_bit(tree
, start
, end
, EXTENT_LOCKED
, 1, 0, mask
);
958 * helper function to set pages and extents in the tree dirty
960 int set_range_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
)
962 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
963 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
966 while (index
<= end_index
) {
967 page
= find_get_page(tree
->mapping
, index
);
969 __set_page_dirty_nobuffers(page
);
970 page_cache_release(page
);
973 set_extent_dirty(tree
, start
, end
, GFP_NOFS
);
978 * helper function to set both pages and extents in the tree writeback
980 static int set_range_writeback(struct extent_io_tree
*tree
, u64 start
, u64 end
)
982 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
983 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
986 while (index
<= end_index
) {
987 page
= find_get_page(tree
->mapping
, index
);
989 set_page_writeback(page
);
990 page_cache_release(page
);
993 set_extent_writeback(tree
, start
, end
, GFP_NOFS
);
998 * find the first offset in the io tree with 'bits' set. zero is
999 * returned if we find something, and *start_ret and *end_ret are
1000 * set to reflect the state struct that was found.
1002 * If nothing was found, 1 is returned, < 0 on error
1004 int find_first_extent_bit(struct extent_io_tree
*tree
, u64 start
,
1005 u64
*start_ret
, u64
*end_ret
, int bits
)
1007 struct rb_node
*node
;
1008 struct extent_state
*state
;
1011 spin_lock(&tree
->lock
);
1013 * this search will find all the extents that end after
1016 node
= tree_search(tree
, start
);
1021 state
= rb_entry(node
, struct extent_state
, rb_node
);
1022 if (state
->end
>= start
&& (state
->state
& bits
)) {
1023 *start_ret
= state
->start
;
1024 *end_ret
= state
->end
;
1028 node
= rb_next(node
);
1033 spin_unlock(&tree
->lock
);
1037 /* find the first state struct with 'bits' set after 'start', and
1038 * return it. tree->lock must be held. NULL will returned if
1039 * nothing was found after 'start'
1041 struct extent_state
*find_first_extent_bit_state(struct extent_io_tree
*tree
,
1042 u64 start
, int bits
)
1044 struct rb_node
*node
;
1045 struct extent_state
*state
;
1048 * this search will find all the extents that end after
1051 node
= tree_search(tree
, start
);
1056 state
= rb_entry(node
, struct extent_state
, rb_node
);
1057 if (state
->end
>= start
&& (state
->state
& bits
))
1060 node
= rb_next(node
);
1069 * find a contiguous range of bytes in the file marked as delalloc, not
1070 * more than 'max_bytes'. start and end are used to return the range,
1072 * 1 is returned if we find something, 0 if nothing was in the tree
1074 static noinline u64
find_delalloc_range(struct extent_io_tree
*tree
,
1075 u64
*start
, u64
*end
, u64 max_bytes
)
1077 struct rb_node
*node
;
1078 struct extent_state
*state
;
1079 u64 cur_start
= *start
;
1081 u64 total_bytes
= 0;
1083 spin_lock(&tree
->lock
);
1086 * this search will find all the extents that end after
1089 node
= tree_search(tree
, cur_start
);
1097 state
= rb_entry(node
, struct extent_state
, rb_node
);
1098 if (found
&& (state
->start
!= cur_start
||
1099 (state
->state
& EXTENT_BOUNDARY
))) {
1102 if (!(state
->state
& EXTENT_DELALLOC
)) {
1108 *start
= state
->start
;
1111 cur_start
= state
->end
+ 1;
1112 node
= rb_next(node
);
1115 total_bytes
+= state
->end
- state
->start
+ 1;
1116 if (total_bytes
>= max_bytes
)
1120 spin_unlock(&tree
->lock
);
1124 static noinline
int __unlock_for_delalloc(struct inode
*inode
,
1125 struct page
*locked_page
,
1129 struct page
*pages
[16];
1130 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1131 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1132 unsigned long nr_pages
= end_index
- index
+ 1;
1135 if (index
== locked_page
->index
&& end_index
== index
)
1138 while (nr_pages
> 0) {
1139 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1140 min_t(unsigned long, nr_pages
,
1141 ARRAY_SIZE(pages
)), pages
);
1142 for (i
= 0; i
< ret
; i
++) {
1143 if (pages
[i
] != locked_page
)
1144 unlock_page(pages
[i
]);
1145 page_cache_release(pages
[i
]);
1154 static noinline
int lock_delalloc_pages(struct inode
*inode
,
1155 struct page
*locked_page
,
1159 unsigned long index
= delalloc_start
>> PAGE_CACHE_SHIFT
;
1160 unsigned long start_index
= index
;
1161 unsigned long end_index
= delalloc_end
>> PAGE_CACHE_SHIFT
;
1162 unsigned long pages_locked
= 0;
1163 struct page
*pages
[16];
1164 unsigned long nrpages
;
1168 /* the caller is responsible for locking the start index */
1169 if (index
== locked_page
->index
&& index
== end_index
)
1172 /* skip the page at the start index */
1173 nrpages
= end_index
- index
+ 1;
1174 while (nrpages
> 0) {
1175 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1176 min_t(unsigned long,
1177 nrpages
, ARRAY_SIZE(pages
)), pages
);
1182 /* now we have an array of pages, lock them all */
1183 for (i
= 0; i
< ret
; i
++) {
1185 * the caller is taking responsibility for
1188 if (pages
[i
] != locked_page
) {
1189 lock_page(pages
[i
]);
1190 if (!PageDirty(pages
[i
]) ||
1191 pages
[i
]->mapping
!= inode
->i_mapping
) {
1193 unlock_page(pages
[i
]);
1194 page_cache_release(pages
[i
]);
1198 page_cache_release(pages
[i
]);
1207 if (ret
&& pages_locked
) {
1208 __unlock_for_delalloc(inode
, locked_page
,
1210 ((u64
)(start_index
+ pages_locked
- 1)) <<
1217 * find a contiguous range of bytes in the file marked as delalloc, not
1218 * more than 'max_bytes'. start and end are used to return the range,
1220 * 1 is returned if we find something, 0 if nothing was in the tree
1222 static noinline u64
find_lock_delalloc_range(struct inode
*inode
,
1223 struct extent_io_tree
*tree
,
1224 struct page
*locked_page
,
1225 u64
*start
, u64
*end
,
1235 /* step one, find a bunch of delalloc bytes starting at start */
1236 delalloc_start
= *start
;
1238 found
= find_delalloc_range(tree
, &delalloc_start
, &delalloc_end
,
1240 if (!found
|| delalloc_end
<= *start
) {
1241 *start
= delalloc_start
;
1242 *end
= delalloc_end
;
1247 * start comes from the offset of locked_page. We have to lock
1248 * pages in order, so we can't process delalloc bytes before
1251 if (delalloc_start
< *start
)
1252 delalloc_start
= *start
;
1255 * make sure to limit the number of pages we try to lock down
1258 if (delalloc_end
+ 1 - delalloc_start
> max_bytes
&& loops
)
1259 delalloc_end
= delalloc_start
+ PAGE_CACHE_SIZE
- 1;
1261 /* step two, lock all the pages after the page that has start */
1262 ret
= lock_delalloc_pages(inode
, locked_page
,
1263 delalloc_start
, delalloc_end
);
1264 if (ret
== -EAGAIN
) {
1265 /* some of the pages are gone, lets avoid looping by
1266 * shortening the size of the delalloc range we're searching
1269 unsigned long offset
= (*start
) & (PAGE_CACHE_SIZE
- 1);
1270 max_bytes
= PAGE_CACHE_SIZE
- offset
;
1280 /* step three, lock the state bits for the whole range */
1281 lock_extent(tree
, delalloc_start
, delalloc_end
, GFP_NOFS
);
1283 /* then test to make sure it is all still delalloc */
1284 ret
= test_range_bit(tree
, delalloc_start
, delalloc_end
,
1285 EXTENT_DELALLOC
, 1);
1287 unlock_extent(tree
, delalloc_start
, delalloc_end
, GFP_NOFS
);
1288 __unlock_for_delalloc(inode
, locked_page
,
1289 delalloc_start
, delalloc_end
);
1293 *start
= delalloc_start
;
1294 *end
= delalloc_end
;
1299 int extent_clear_unlock_delalloc(struct inode
*inode
,
1300 struct extent_io_tree
*tree
,
1301 u64 start
, u64 end
, struct page
*locked_page
,
1304 int clear_delalloc
, int clear_dirty
,
1309 struct page
*pages
[16];
1310 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1311 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1312 unsigned long nr_pages
= end_index
- index
+ 1;
1317 clear_bits
|= EXTENT_LOCKED
;
1319 clear_bits
|= EXTENT_DIRTY
;
1322 clear_bits
|= EXTENT_DELALLOC
;
1324 clear_extent_bit(tree
, start
, end
, clear_bits
, 1, 0, GFP_NOFS
);
1325 if (!(unlock_pages
|| clear_dirty
|| set_writeback
|| end_writeback
))
1328 while (nr_pages
> 0) {
1329 ret
= find_get_pages_contig(inode
->i_mapping
, index
,
1330 min_t(unsigned long,
1331 nr_pages
, ARRAY_SIZE(pages
)), pages
);
1332 for (i
= 0; i
< ret
; i
++) {
1333 if (pages
[i
] == locked_page
) {
1334 page_cache_release(pages
[i
]);
1338 clear_page_dirty_for_io(pages
[i
]);
1340 set_page_writeback(pages
[i
]);
1342 end_page_writeback(pages
[i
]);
1344 unlock_page(pages
[i
]);
1345 page_cache_release(pages
[i
]);
1355 * count the number of bytes in the tree that have a given bit(s)
1356 * set. This can be fairly slow, except for EXTENT_DIRTY which is
1357 * cached. The total number found is returned.
1359 u64
count_range_bits(struct extent_io_tree
*tree
,
1360 u64
*start
, u64 search_end
, u64 max_bytes
,
1363 struct rb_node
*node
;
1364 struct extent_state
*state
;
1365 u64 cur_start
= *start
;
1366 u64 total_bytes
= 0;
1369 if (search_end
<= cur_start
) {
1374 spin_lock(&tree
->lock
);
1375 if (cur_start
== 0 && bits
== EXTENT_DIRTY
) {
1376 total_bytes
= tree
->dirty_bytes
;
1380 * this search will find all the extents that end after
1383 node
= tree_search(tree
, cur_start
);
1388 state
= rb_entry(node
, struct extent_state
, rb_node
);
1389 if (state
->start
> search_end
)
1391 if (state
->end
>= cur_start
&& (state
->state
& bits
)) {
1392 total_bytes
+= min(search_end
, state
->end
) + 1 -
1393 max(cur_start
, state
->start
);
1394 if (total_bytes
>= max_bytes
)
1397 *start
= state
->start
;
1401 node
= rb_next(node
);
1406 spin_unlock(&tree
->lock
);
1412 * helper function to lock both pages and extents in the tree.
1413 * pages must be locked first.
1415 static int lock_range(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1417 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1418 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1422 while (index
<= end_index
) {
1423 page
= grab_cache_page(tree
->mapping
, index
);
1429 err
= PTR_ERR(page
);
1434 lock_extent(tree
, start
, end
, GFP_NOFS
);
1439 * we failed above in getting the page at 'index', so we undo here
1440 * up to but not including the page at 'index'
1443 index
= start
>> PAGE_CACHE_SHIFT
;
1444 while (index
< end_index
) {
1445 page
= find_get_page(tree
->mapping
, index
);
1447 page_cache_release(page
);
1454 * helper function to unlock both pages and extents in the tree.
1456 static int unlock_range(struct extent_io_tree
*tree
, u64 start
, u64 end
)
1458 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
1459 unsigned long end_index
= end
>> PAGE_CACHE_SHIFT
;
1462 while (index
<= end_index
) {
1463 page
= find_get_page(tree
->mapping
, index
);
1465 page_cache_release(page
);
1468 unlock_extent(tree
, start
, end
, GFP_NOFS
);
1474 * set the private field for a given byte offset in the tree. If there isn't
1475 * an extent_state there already, this does nothing.
1477 int set_state_private(struct extent_io_tree
*tree
, u64 start
, u64
private)
1479 struct rb_node
*node
;
1480 struct extent_state
*state
;
1483 spin_lock(&tree
->lock
);
1485 * this search will find all the extents that end after
1488 node
= tree_search(tree
, start
);
1493 state
= rb_entry(node
, struct extent_state
, rb_node
);
1494 if (state
->start
!= start
) {
1498 state
->private = private;
1500 spin_unlock(&tree
->lock
);
1504 int get_state_private(struct extent_io_tree
*tree
, u64 start
, u64
*private)
1506 struct rb_node
*node
;
1507 struct extent_state
*state
;
1510 spin_lock(&tree
->lock
);
1512 * this search will find all the extents that end after
1515 node
= tree_search(tree
, start
);
1520 state
= rb_entry(node
, struct extent_state
, rb_node
);
1521 if (state
->start
!= start
) {
1525 *private = state
->private;
1527 spin_unlock(&tree
->lock
);
1532 * searches a range in the state tree for a given mask.
1533 * If 'filled' == 1, this returns 1 only if every extent in the tree
1534 * has the bits set. Otherwise, 1 is returned if any bit in the
1535 * range is found set.
1537 int test_range_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
1538 int bits
, int filled
)
1540 struct extent_state
*state
= NULL
;
1541 struct rb_node
*node
;
1544 spin_lock(&tree
->lock
);
1545 node
= tree_search(tree
, start
);
1546 while (node
&& start
<= end
) {
1547 state
= rb_entry(node
, struct extent_state
, rb_node
);
1549 if (filled
&& state
->start
> start
) {
1554 if (state
->start
> end
)
1557 if (state
->state
& bits
) {
1561 } else if (filled
) {
1565 start
= state
->end
+ 1;
1568 node
= rb_next(node
);
1575 spin_unlock(&tree
->lock
);
1580 * helper function to set a given page up to date if all the
1581 * extents in the tree for that page are up to date
1583 static int check_page_uptodate(struct extent_io_tree
*tree
,
1586 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1587 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1588 if (test_range_bit(tree
, start
, end
, EXTENT_UPTODATE
, 1))
1589 SetPageUptodate(page
);
1594 * helper function to unlock a page if all the extents in the tree
1595 * for that page are unlocked
1597 static int check_page_locked(struct extent_io_tree
*tree
,
1600 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1601 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1602 if (!test_range_bit(tree
, start
, end
, EXTENT_LOCKED
, 0))
1608 * helper function to end page writeback if all the extents
1609 * in the tree for that page are done with writeback
1611 static int check_page_writeback(struct extent_io_tree
*tree
,
1614 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1615 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
1616 if (!test_range_bit(tree
, start
, end
, EXTENT_WRITEBACK
, 0))
1617 end_page_writeback(page
);
1621 /* lots and lots of room for performance fixes in the end_bio funcs */
1624 * after a writepage IO is done, we need to:
1625 * clear the uptodate bits on error
1626 * clear the writeback bits in the extent tree for this IO
1627 * end_page_writeback if the page has no more pending IO
1629 * Scheduling is not allowed, so the extent state tree is expected
1630 * to have one and only one object corresponding to this IO.
1632 static void end_bio_extent_writepage(struct bio
*bio
, int err
)
1634 int uptodate
= err
== 0;
1635 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1636 struct extent_io_tree
*tree
;
1643 struct page
*page
= bvec
->bv_page
;
1644 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
1646 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
1648 end
= start
+ bvec
->bv_len
- 1;
1650 if (bvec
->bv_offset
== 0 && bvec
->bv_len
== PAGE_CACHE_SIZE
)
1655 if (--bvec
>= bio
->bi_io_vec
)
1656 prefetchw(&bvec
->bv_page
->flags
);
1657 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
) {
1658 ret
= tree
->ops
->writepage_end_io_hook(page
, start
,
1659 end
, NULL
, uptodate
);
1664 if (!uptodate
&& tree
->ops
&&
1665 tree
->ops
->writepage_io_failed_hook
) {
1666 ret
= tree
->ops
->writepage_io_failed_hook(bio
, page
,
1669 uptodate
= (err
== 0);
1675 clear_extent_uptodate(tree
, start
, end
, GFP_ATOMIC
);
1676 ClearPageUptodate(page
);
1680 clear_extent_writeback(tree
, start
, end
, GFP_ATOMIC
);
1683 end_page_writeback(page
);
1685 check_page_writeback(tree
, page
);
1686 } while (bvec
>= bio
->bi_io_vec
);
1692 * after a readpage IO is done, we need to:
1693 * clear the uptodate bits on error
1694 * set the uptodate bits if things worked
1695 * set the page up to date if all extents in the tree are uptodate
1696 * clear the lock bit in the extent tree
1697 * unlock the page if there are no other extents locked for it
1699 * Scheduling is not allowed, so the extent state tree is expected
1700 * to have one and only one object corresponding to this IO.
1702 static void end_bio_extent_readpage(struct bio
*bio
, int err
)
1704 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1705 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1706 struct extent_io_tree
*tree
;
1716 struct page
*page
= bvec
->bv_page
;
1717 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
1719 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
1721 end
= start
+ bvec
->bv_len
- 1;
1723 if (bvec
->bv_offset
== 0 && bvec
->bv_len
== PAGE_CACHE_SIZE
)
1728 if (--bvec
>= bio
->bi_io_vec
)
1729 prefetchw(&bvec
->bv_page
->flags
);
1731 if (uptodate
&& tree
->ops
&& tree
->ops
->readpage_end_io_hook
) {
1732 ret
= tree
->ops
->readpage_end_io_hook(page
, start
, end
,
1737 if (!uptodate
&& tree
->ops
&&
1738 tree
->ops
->readpage_io_failed_hook
) {
1739 ret
= tree
->ops
->readpage_io_failed_hook(bio
, page
,
1743 test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1751 set_extent_uptodate(tree
, start
, end
,
1754 unlock_extent(tree
, start
, end
, GFP_ATOMIC
);
1758 SetPageUptodate(page
);
1760 ClearPageUptodate(page
);
1766 check_page_uptodate(tree
, page
);
1768 ClearPageUptodate(page
);
1771 check_page_locked(tree
, page
);
1773 } while (bvec
>= bio
->bi_io_vec
);
1779 * IO done from prepare_write is pretty simple, we just unlock
1780 * the structs in the extent tree when done, and set the uptodate bits
1783 static void end_bio_extent_preparewrite(struct bio
*bio
, int err
)
1785 const int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1786 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1787 struct extent_io_tree
*tree
;
1792 struct page
*page
= bvec
->bv_page
;
1793 tree
= &BTRFS_I(page
->mapping
->host
)->io_tree
;
1795 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) +
1797 end
= start
+ bvec
->bv_len
- 1;
1799 if (--bvec
>= bio
->bi_io_vec
)
1800 prefetchw(&bvec
->bv_page
->flags
);
1803 set_extent_uptodate(tree
, start
, end
, GFP_ATOMIC
);
1805 ClearPageUptodate(page
);
1809 unlock_extent(tree
, start
, end
, GFP_ATOMIC
);
1811 } while (bvec
>= bio
->bi_io_vec
);
1817 extent_bio_alloc(struct block_device
*bdev
, u64 first_sector
, int nr_vecs
,
1822 bio
= bio_alloc(gfp_flags
, nr_vecs
);
1824 if (bio
== NULL
&& (current
->flags
& PF_MEMALLOC
)) {
1825 while (!bio
&& (nr_vecs
/= 2))
1826 bio
= bio_alloc(gfp_flags
, nr_vecs
);
1831 bio
->bi_bdev
= bdev
;
1832 bio
->bi_sector
= first_sector
;
1837 static int submit_one_bio(int rw
, struct bio
*bio
, int mirror_num
,
1838 unsigned long bio_flags
)
1841 struct bio_vec
*bvec
= bio
->bi_io_vec
+ bio
->bi_vcnt
- 1;
1842 struct page
*page
= bvec
->bv_page
;
1843 struct extent_io_tree
*tree
= bio
->bi_private
;
1847 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
) + bvec
->bv_offset
;
1848 end
= start
+ bvec
->bv_len
- 1;
1850 bio
->bi_private
= NULL
;
1854 if (tree
->ops
&& tree
->ops
->submit_bio_hook
)
1855 tree
->ops
->submit_bio_hook(page
->mapping
->host
, rw
, bio
,
1856 mirror_num
, bio_flags
);
1858 submit_bio(rw
, bio
);
1859 if (bio_flagged(bio
, BIO_EOPNOTSUPP
))
1865 static int submit_extent_page(int rw
, struct extent_io_tree
*tree
,
1866 struct page
*page
, sector_t sector
,
1867 size_t size
, unsigned long offset
,
1868 struct block_device
*bdev
,
1869 struct bio
**bio_ret
,
1870 unsigned long max_pages
,
1871 bio_end_io_t end_io_func
,
1873 unsigned long prev_bio_flags
,
1874 unsigned long bio_flags
)
1880 int this_compressed
= bio_flags
& EXTENT_BIO_COMPRESSED
;
1881 int old_compressed
= prev_bio_flags
& EXTENT_BIO_COMPRESSED
;
1882 size_t page_size
= min_t(size_t, size
, PAGE_CACHE_SIZE
);
1884 if (bio_ret
&& *bio_ret
) {
1887 contig
= bio
->bi_sector
== sector
;
1889 contig
= bio
->bi_sector
+ (bio
->bi_size
>> 9) ==
1892 if (prev_bio_flags
!= bio_flags
|| !contig
||
1893 (tree
->ops
&& tree
->ops
->merge_bio_hook
&&
1894 tree
->ops
->merge_bio_hook(page
, offset
, page_size
, bio
,
1896 bio_add_page(bio
, page
, page_size
, offset
) < page_size
) {
1897 ret
= submit_one_bio(rw
, bio
, mirror_num
,
1904 if (this_compressed
)
1907 nr
= bio_get_nr_vecs(bdev
);
1909 bio
= extent_bio_alloc(bdev
, sector
, nr
, GFP_NOFS
| __GFP_HIGH
);
1911 bio_add_page(bio
, page
, page_size
, offset
);
1912 bio
->bi_end_io
= end_io_func
;
1913 bio
->bi_private
= tree
;
1918 ret
= submit_one_bio(rw
, bio
, mirror_num
, bio_flags
);
1923 void set_page_extent_mapped(struct page
*page
)
1925 if (!PagePrivate(page
)) {
1926 SetPagePrivate(page
);
1927 page_cache_get(page
);
1928 set_page_private(page
, EXTENT_PAGE_PRIVATE
);
1932 static void set_page_extent_head(struct page
*page
, unsigned long len
)
1934 set_page_private(page
, EXTENT_PAGE_PRIVATE_FIRST_PAGE
| len
<< 2);
1938 * basic readpage implementation. Locked extent state structs are inserted
1939 * into the tree that are removed when the IO is done (by the end_io
1942 static int __extent_read_full_page(struct extent_io_tree
*tree
,
1944 get_extent_t
*get_extent
,
1945 struct bio
**bio
, int mirror_num
,
1946 unsigned long *bio_flags
)
1948 struct inode
*inode
= page
->mapping
->host
;
1949 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
1950 u64 page_end
= start
+ PAGE_CACHE_SIZE
- 1;
1954 u64 last_byte
= i_size_read(inode
);
1958 struct extent_map
*em
;
1959 struct block_device
*bdev
;
1962 size_t page_offset
= 0;
1964 size_t disk_io_size
;
1965 size_t blocksize
= inode
->i_sb
->s_blocksize
;
1966 unsigned long this_bio_flag
= 0;
1968 set_page_extent_mapped(page
);
1971 lock_extent(tree
, start
, end
, GFP_NOFS
);
1973 if (page
->index
== last_byte
>> PAGE_CACHE_SHIFT
) {
1975 size_t zero_offset
= last_byte
& (PAGE_CACHE_SIZE
- 1);
1978 iosize
= PAGE_CACHE_SIZE
- zero_offset
;
1979 userpage
= kmap_atomic(page
, KM_USER0
);
1980 memset(userpage
+ zero_offset
, 0, iosize
);
1981 flush_dcache_page(page
);
1982 kunmap_atomic(userpage
, KM_USER0
);
1985 while (cur
<= end
) {
1986 if (cur
>= last_byte
) {
1988 iosize
= PAGE_CACHE_SIZE
- page_offset
;
1989 userpage
= kmap_atomic(page
, KM_USER0
);
1990 memset(userpage
+ page_offset
, 0, iosize
);
1991 flush_dcache_page(page
);
1992 kunmap_atomic(userpage
, KM_USER0
);
1993 set_extent_uptodate(tree
, cur
, cur
+ iosize
- 1,
1995 unlock_extent(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
1998 em
= get_extent(inode
, page
, page_offset
, cur
,
2000 if (IS_ERR(em
) || !em
) {
2002 unlock_extent(tree
, cur
, end
, GFP_NOFS
);
2005 extent_offset
= cur
- em
->start
;
2006 BUG_ON(extent_map_end(em
) <= cur
);
2009 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
))
2010 this_bio_flag
= EXTENT_BIO_COMPRESSED
;
2012 iosize
= min(extent_map_end(em
) - cur
, end
- cur
+ 1);
2013 cur_end
= min(extent_map_end(em
) - 1, end
);
2014 iosize
= (iosize
+ blocksize
- 1) & ~((u64
)blocksize
- 1);
2015 if (this_bio_flag
& EXTENT_BIO_COMPRESSED
) {
2016 disk_io_size
= em
->block_len
;
2017 sector
= em
->block_start
>> 9;
2019 sector
= (em
->block_start
+ extent_offset
) >> 9;
2020 disk_io_size
= iosize
;
2023 block_start
= em
->block_start
;
2024 if (test_bit(EXTENT_FLAG_PREALLOC
, &em
->flags
))
2025 block_start
= EXTENT_MAP_HOLE
;
2026 free_extent_map(em
);
2029 /* we've found a hole, just zero and go on */
2030 if (block_start
== EXTENT_MAP_HOLE
) {
2032 userpage
= kmap_atomic(page
, KM_USER0
);
2033 memset(userpage
+ page_offset
, 0, iosize
);
2034 flush_dcache_page(page
);
2035 kunmap_atomic(userpage
, KM_USER0
);
2037 set_extent_uptodate(tree
, cur
, cur
+ iosize
- 1,
2039 unlock_extent(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
2041 page_offset
+= iosize
;
2044 /* the get_extent function already copied into the page */
2045 if (test_range_bit(tree
, cur
, cur_end
, EXTENT_UPTODATE
, 1)) {
2046 check_page_uptodate(tree
, page
);
2047 unlock_extent(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
2049 page_offset
+= iosize
;
2052 /* we have an inline extent but it didn't get marked up
2053 * to date. Error out
2055 if (block_start
== EXTENT_MAP_INLINE
) {
2057 unlock_extent(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
2059 page_offset
+= iosize
;
2064 if (tree
->ops
&& tree
->ops
->readpage_io_hook
) {
2065 ret
= tree
->ops
->readpage_io_hook(page
, cur
,
2069 unsigned long pnr
= (last_byte
>> PAGE_CACHE_SHIFT
) + 1;
2071 ret
= submit_extent_page(READ
, tree
, page
,
2072 sector
, disk_io_size
, page_offset
,
2074 end_bio_extent_readpage
, mirror_num
,
2078 *bio_flags
= this_bio_flag
;
2083 page_offset
+= iosize
;
2086 if (!PageError(page
))
2087 SetPageUptodate(page
);
2093 int extent_read_full_page(struct extent_io_tree
*tree
, struct page
*page
,
2094 get_extent_t
*get_extent
)
2096 struct bio
*bio
= NULL
;
2097 unsigned long bio_flags
= 0;
2100 ret
= __extent_read_full_page(tree
, page
, get_extent
, &bio
, 0,
2103 submit_one_bio(READ
, bio
, 0, bio_flags
);
2107 static noinline
void update_nr_written(struct page
*page
,
2108 struct writeback_control
*wbc
,
2109 unsigned long nr_written
)
2111 wbc
->nr_to_write
-= nr_written
;
2112 if (wbc
->range_cyclic
|| (wbc
->nr_to_write
> 0 &&
2113 wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
))
2114 page
->mapping
->writeback_index
= page
->index
+ nr_written
;
2118 * the writepage semantics are similar to regular writepage. extent
2119 * records are inserted to lock ranges in the tree, and as dirty areas
2120 * are found, they are marked writeback. Then the lock bits are removed
2121 * and the end_io handler clears the writeback ranges
2123 static int __extent_writepage(struct page
*page
, struct writeback_control
*wbc
,
2126 struct inode
*inode
= page
->mapping
->host
;
2127 struct extent_page_data
*epd
= data
;
2128 struct extent_io_tree
*tree
= epd
->tree
;
2129 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2131 u64 page_end
= start
+ PAGE_CACHE_SIZE
- 1;
2135 u64 last_byte
= i_size_read(inode
);
2140 struct extent_map
*em
;
2141 struct block_device
*bdev
;
2144 size_t pg_offset
= 0;
2146 loff_t i_size
= i_size_read(inode
);
2147 unsigned long end_index
= i_size
>> PAGE_CACHE_SHIFT
;
2153 unsigned long nr_written
= 0;
2155 if (wbc
->sync_mode
== WB_SYNC_ALL
)
2156 write_flags
= WRITE_SYNC_PLUG
;
2158 write_flags
= WRITE
;
2160 WARN_ON(!PageLocked(page
));
2161 pg_offset
= i_size
& (PAGE_CACHE_SIZE
- 1);
2162 if (page
->index
> end_index
||
2163 (page
->index
== end_index
&& !pg_offset
)) {
2164 page
->mapping
->a_ops
->invalidatepage(page
, 0);
2169 if (page
->index
== end_index
) {
2172 userpage
= kmap_atomic(page
, KM_USER0
);
2173 memset(userpage
+ pg_offset
, 0,
2174 PAGE_CACHE_SIZE
- pg_offset
);
2175 kunmap_atomic(userpage
, KM_USER0
);
2176 flush_dcache_page(page
);
2180 set_page_extent_mapped(page
);
2182 delalloc_start
= start
;
2185 if (!epd
->extent_locked
) {
2187 * make sure the wbc mapping index is at least updated
2190 update_nr_written(page
, wbc
, 0);
2192 while (delalloc_end
< page_end
) {
2193 nr_delalloc
= find_lock_delalloc_range(inode
, tree
,
2198 if (nr_delalloc
== 0) {
2199 delalloc_start
= delalloc_end
+ 1;
2202 tree
->ops
->fill_delalloc(inode
, page
, delalloc_start
,
2203 delalloc_end
, &page_started
,
2205 delalloc_start
= delalloc_end
+ 1;
2208 /* did the fill delalloc function already unlock and start
2214 * we've unlocked the page, so we can't update
2215 * the mapping's writeback index, just update
2218 wbc
->nr_to_write
-= nr_written
;
2222 lock_extent(tree
, start
, page_end
, GFP_NOFS
);
2224 unlock_start
= start
;
2226 if (tree
->ops
&& tree
->ops
->writepage_start_hook
) {
2227 ret
= tree
->ops
->writepage_start_hook(page
, start
,
2229 if (ret
== -EAGAIN
) {
2230 unlock_extent(tree
, start
, page_end
, GFP_NOFS
);
2231 redirty_page_for_writepage(wbc
, page
);
2232 update_nr_written(page
, wbc
, nr_written
);
2240 * we don't want to touch the inode after unlocking the page,
2241 * so we update the mapping writeback index now
2243 update_nr_written(page
, wbc
, nr_written
+ 1);
2246 if (test_range_bit(tree
, start
, page_end
, EXTENT_DELALLOC
, 0))
2247 printk(KERN_ERR
"btrfs delalloc bits after lock_extent\n");
2249 if (last_byte
<= start
) {
2250 clear_extent_dirty(tree
, start
, page_end
, GFP_NOFS
);
2251 unlock_extent(tree
, start
, page_end
, GFP_NOFS
);
2252 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2253 tree
->ops
->writepage_end_io_hook(page
, start
,
2255 unlock_start
= page_end
+ 1;
2259 set_extent_uptodate(tree
, start
, page_end
, GFP_NOFS
);
2260 blocksize
= inode
->i_sb
->s_blocksize
;
2262 while (cur
<= end
) {
2263 if (cur
>= last_byte
) {
2264 clear_extent_dirty(tree
, cur
, page_end
, GFP_NOFS
);
2265 unlock_extent(tree
, unlock_start
, page_end
, GFP_NOFS
);
2266 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2267 tree
->ops
->writepage_end_io_hook(page
, cur
,
2269 unlock_start
= page_end
+ 1;
2272 em
= epd
->get_extent(inode
, page
, pg_offset
, cur
,
2274 if (IS_ERR(em
) || !em
) {
2279 extent_offset
= cur
- em
->start
;
2280 BUG_ON(extent_map_end(em
) <= cur
);
2282 iosize
= min(extent_map_end(em
) - cur
, end
- cur
+ 1);
2283 iosize
= (iosize
+ blocksize
- 1) & ~((u64
)blocksize
- 1);
2284 sector
= (em
->block_start
+ extent_offset
) >> 9;
2286 block_start
= em
->block_start
;
2287 compressed
= test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
);
2288 free_extent_map(em
);
2292 * compressed and inline extents are written through other
2295 if (compressed
|| block_start
== EXTENT_MAP_HOLE
||
2296 block_start
== EXTENT_MAP_INLINE
) {
2297 clear_extent_dirty(tree
, cur
,
2298 cur
+ iosize
- 1, GFP_NOFS
);
2300 unlock_extent(tree
, unlock_start
, cur
+ iosize
- 1,
2304 * end_io notification does not happen here for
2305 * compressed extents
2307 if (!compressed
&& tree
->ops
&&
2308 tree
->ops
->writepage_end_io_hook
)
2309 tree
->ops
->writepage_end_io_hook(page
, cur
,
2312 else if (compressed
) {
2313 /* we don't want to end_page_writeback on
2314 * a compressed extent. this happens
2321 pg_offset
+= iosize
;
2325 /* leave this out until we have a page_mkwrite call */
2326 if (0 && !test_range_bit(tree
, cur
, cur
+ iosize
- 1,
2329 pg_offset
+= iosize
;
2333 clear_extent_dirty(tree
, cur
, cur
+ iosize
- 1, GFP_NOFS
);
2334 if (tree
->ops
&& tree
->ops
->writepage_io_hook
) {
2335 ret
= tree
->ops
->writepage_io_hook(page
, cur
,
2343 unsigned long max_nr
= end_index
+ 1;
2345 set_range_writeback(tree
, cur
, cur
+ iosize
- 1);
2346 if (!PageWriteback(page
)) {
2347 printk(KERN_ERR
"btrfs warning page %lu not "
2348 "writeback, cur %llu end %llu\n",
2349 page
->index
, (unsigned long long)cur
,
2350 (unsigned long long)end
);
2353 ret
= submit_extent_page(write_flags
, tree
, page
,
2354 sector
, iosize
, pg_offset
,
2355 bdev
, &epd
->bio
, max_nr
,
2356 end_bio_extent_writepage
,
2362 pg_offset
+= iosize
;
2367 /* make sure the mapping tag for page dirty gets cleared */
2368 set_page_writeback(page
);
2369 end_page_writeback(page
);
2371 if (unlock_start
<= page_end
)
2372 unlock_extent(tree
, unlock_start
, page_end
, GFP_NOFS
);
2381 * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
2382 * @mapping: address space structure to write
2383 * @wbc: subtract the number of written pages from *@wbc->nr_to_write
2384 * @writepage: function called for each page
2385 * @data: data passed to writepage function
2387 * If a page is already under I/O, write_cache_pages() skips it, even
2388 * if it's dirty. This is desirable behaviour for memory-cleaning writeback,
2389 * but it is INCORRECT for data-integrity system calls such as fsync(). fsync()
2390 * and msync() need to guarantee that all the data which was dirty at the time
2391 * the call was made get new I/O started against them. If wbc->sync_mode is
2392 * WB_SYNC_ALL then we were called for data integrity and we must wait for
2393 * existing IO to complete.
2395 static int extent_write_cache_pages(struct extent_io_tree
*tree
,
2396 struct address_space
*mapping
,
2397 struct writeback_control
*wbc
,
2398 writepage_t writepage
, void *data
,
2399 void (*flush_fn
)(void *))
2401 struct backing_dev_info
*bdi
= mapping
->backing_dev_info
;
2404 struct pagevec pvec
;
2407 pgoff_t end
; /* Inclusive */
2409 int range_whole
= 0;
2411 pagevec_init(&pvec
, 0);
2412 if (wbc
->range_cyclic
) {
2413 index
= mapping
->writeback_index
; /* Start from prev offset */
2416 index
= wbc
->range_start
>> PAGE_CACHE_SHIFT
;
2417 end
= wbc
->range_end
>> PAGE_CACHE_SHIFT
;
2418 if (wbc
->range_start
== 0 && wbc
->range_end
== LLONG_MAX
)
2423 while (!done
&& (index
<= end
) &&
2424 (nr_pages
= pagevec_lookup_tag(&pvec
, mapping
, &index
,
2425 PAGECACHE_TAG_DIRTY
, min(end
- index
,
2426 (pgoff_t
)PAGEVEC_SIZE
-1) + 1))) {
2430 for (i
= 0; i
< nr_pages
; i
++) {
2431 struct page
*page
= pvec
.pages
[i
];
2434 * At this point we hold neither mapping->tree_lock nor
2435 * lock on the page itself: the page may be truncated or
2436 * invalidated (changing page->mapping to NULL), or even
2437 * swizzled back from swapper_space to tmpfs file
2440 if (tree
->ops
&& tree
->ops
->write_cache_pages_lock_hook
)
2441 tree
->ops
->write_cache_pages_lock_hook(page
);
2445 if (unlikely(page
->mapping
!= mapping
)) {
2450 if (!wbc
->range_cyclic
&& page
->index
> end
) {
2456 if (wbc
->sync_mode
!= WB_SYNC_NONE
) {
2457 if (PageWriteback(page
))
2459 wait_on_page_writeback(page
);
2462 if (PageWriteback(page
) ||
2463 !clear_page_dirty_for_io(page
)) {
2468 ret
= (*writepage
)(page
, wbc
, data
);
2470 if (unlikely(ret
== AOP_WRITEPAGE_ACTIVATE
)) {
2474 if (ret
|| wbc
->nr_to_write
<= 0)
2476 if (wbc
->nonblocking
&& bdi_write_congested(bdi
)) {
2477 wbc
->encountered_congestion
= 1;
2481 pagevec_release(&pvec
);
2484 if (!scanned
&& !done
) {
2486 * We hit the last page and there is more work to be done: wrap
2487 * back to the start of the file
2496 static void flush_epd_write_bio(struct extent_page_data
*epd
)
2500 submit_one_bio(WRITE_SYNC
, epd
->bio
, 0, 0);
2502 submit_one_bio(WRITE
, epd
->bio
, 0, 0);
2507 static noinline
void flush_write_bio(void *data
)
2509 struct extent_page_data
*epd
= data
;
2510 flush_epd_write_bio(epd
);
2513 int extent_write_full_page(struct extent_io_tree
*tree
, struct page
*page
,
2514 get_extent_t
*get_extent
,
2515 struct writeback_control
*wbc
)
2518 struct address_space
*mapping
= page
->mapping
;
2519 struct extent_page_data epd
= {
2522 .get_extent
= get_extent
,
2524 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
2526 struct writeback_control wbc_writepages
= {
2528 .sync_mode
= wbc
->sync_mode
,
2529 .older_than_this
= NULL
,
2531 .range_start
= page_offset(page
) + PAGE_CACHE_SIZE
,
2532 .range_end
= (loff_t
)-1,
2535 ret
= __extent_writepage(page
, wbc
, &epd
);
2537 extent_write_cache_pages(tree
, mapping
, &wbc_writepages
,
2538 __extent_writepage
, &epd
, flush_write_bio
);
2539 flush_epd_write_bio(&epd
);
2543 int extent_write_locked_range(struct extent_io_tree
*tree
, struct inode
*inode
,
2544 u64 start
, u64 end
, get_extent_t
*get_extent
,
2548 struct address_space
*mapping
= inode
->i_mapping
;
2550 unsigned long nr_pages
= (end
- start
+ PAGE_CACHE_SIZE
) >>
2553 struct extent_page_data epd
= {
2556 .get_extent
= get_extent
,
2558 .sync_io
= mode
== WB_SYNC_ALL
,
2560 struct writeback_control wbc_writepages
= {
2561 .bdi
= inode
->i_mapping
->backing_dev_info
,
2563 .older_than_this
= NULL
,
2564 .nr_to_write
= nr_pages
* 2,
2565 .range_start
= start
,
2566 .range_end
= end
+ 1,
2569 while (start
<= end
) {
2570 page
= find_get_page(mapping
, start
>> PAGE_CACHE_SHIFT
);
2571 if (clear_page_dirty_for_io(page
))
2572 ret
= __extent_writepage(page
, &wbc_writepages
, &epd
);
2574 if (tree
->ops
&& tree
->ops
->writepage_end_io_hook
)
2575 tree
->ops
->writepage_end_io_hook(page
, start
,
2576 start
+ PAGE_CACHE_SIZE
- 1,
2580 page_cache_release(page
);
2581 start
+= PAGE_CACHE_SIZE
;
2584 flush_epd_write_bio(&epd
);
2588 int extent_writepages(struct extent_io_tree
*tree
,
2589 struct address_space
*mapping
,
2590 get_extent_t
*get_extent
,
2591 struct writeback_control
*wbc
)
2594 struct extent_page_data epd
= {
2597 .get_extent
= get_extent
,
2599 .sync_io
= wbc
->sync_mode
== WB_SYNC_ALL
,
2602 ret
= extent_write_cache_pages(tree
, mapping
, wbc
,
2603 __extent_writepage
, &epd
,
2605 flush_epd_write_bio(&epd
);
2609 int extent_readpages(struct extent_io_tree
*tree
,
2610 struct address_space
*mapping
,
2611 struct list_head
*pages
, unsigned nr_pages
,
2612 get_extent_t get_extent
)
2614 struct bio
*bio
= NULL
;
2616 struct pagevec pvec
;
2617 unsigned long bio_flags
= 0;
2619 pagevec_init(&pvec
, 0);
2620 for (page_idx
= 0; page_idx
< nr_pages
; page_idx
++) {
2621 struct page
*page
= list_entry(pages
->prev
, struct page
, lru
);
2623 prefetchw(&page
->flags
);
2624 list_del(&page
->lru
);
2626 * what we want to do here is call add_to_page_cache_lru,
2627 * but that isn't exported, so we reproduce it here
2629 if (!add_to_page_cache(page
, mapping
,
2630 page
->index
, GFP_KERNEL
)) {
2632 /* open coding of lru_cache_add, also not exported */
2633 page_cache_get(page
);
2634 if (!pagevec_add(&pvec
, page
))
2635 __pagevec_lru_add_file(&pvec
);
2636 __extent_read_full_page(tree
, page
, get_extent
,
2637 &bio
, 0, &bio_flags
);
2639 page_cache_release(page
);
2641 if (pagevec_count(&pvec
))
2642 __pagevec_lru_add_file(&pvec
);
2643 BUG_ON(!list_empty(pages
));
2645 submit_one_bio(READ
, bio
, 0, bio_flags
);
2650 * basic invalidatepage code, this waits on any locked or writeback
2651 * ranges corresponding to the page, and then deletes any extent state
2652 * records from the tree
2654 int extent_invalidatepage(struct extent_io_tree
*tree
,
2655 struct page
*page
, unsigned long offset
)
2657 u64 start
= ((u64
)page
->index
<< PAGE_CACHE_SHIFT
);
2658 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
2659 size_t blocksize
= page
->mapping
->host
->i_sb
->s_blocksize
;
2661 start
+= (offset
+ blocksize
- 1) & ~(blocksize
- 1);
2665 lock_extent(tree
, start
, end
, GFP_NOFS
);
2666 wait_on_extent_writeback(tree
, start
, end
);
2667 clear_extent_bit(tree
, start
, end
,
2668 EXTENT_LOCKED
| EXTENT_DIRTY
| EXTENT_DELALLOC
,
2674 * simple commit_write call, set_range_dirty is used to mark both
2675 * the pages and the extent records as dirty
2677 int extent_commit_write(struct extent_io_tree
*tree
,
2678 struct inode
*inode
, struct page
*page
,
2679 unsigned from
, unsigned to
)
2681 loff_t pos
= ((loff_t
)page
->index
<< PAGE_CACHE_SHIFT
) + to
;
2683 set_page_extent_mapped(page
);
2684 set_page_dirty(page
);
2686 if (pos
> inode
->i_size
) {
2687 i_size_write(inode
, pos
);
2688 mark_inode_dirty(inode
);
2693 int extent_prepare_write(struct extent_io_tree
*tree
,
2694 struct inode
*inode
, struct page
*page
,
2695 unsigned from
, unsigned to
, get_extent_t
*get_extent
)
2697 u64 page_start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2698 u64 page_end
= page_start
+ PAGE_CACHE_SIZE
- 1;
2700 u64 orig_block_start
;
2703 struct extent_map
*em
;
2704 unsigned blocksize
= 1 << inode
->i_blkbits
;
2705 size_t page_offset
= 0;
2706 size_t block_off_start
;
2707 size_t block_off_end
;
2713 set_page_extent_mapped(page
);
2715 block_start
= (page_start
+ from
) & ~((u64
)blocksize
- 1);
2716 block_end
= (page_start
+ to
- 1) | (blocksize
- 1);
2717 orig_block_start
= block_start
;
2719 lock_extent(tree
, page_start
, page_end
, GFP_NOFS
);
2720 while (block_start
<= block_end
) {
2721 em
= get_extent(inode
, page
, page_offset
, block_start
,
2722 block_end
- block_start
+ 1, 1);
2723 if (IS_ERR(em
) || !em
)
2726 cur_end
= min(block_end
, extent_map_end(em
) - 1);
2727 block_off_start
= block_start
& (PAGE_CACHE_SIZE
- 1);
2728 block_off_end
= block_off_start
+ blocksize
;
2729 isnew
= clear_extent_new(tree
, block_start
, cur_end
, GFP_NOFS
);
2731 if (!PageUptodate(page
) && isnew
&&
2732 (block_off_end
> to
|| block_off_start
< from
)) {
2735 kaddr
= kmap_atomic(page
, KM_USER0
);
2736 if (block_off_end
> to
)
2737 memset(kaddr
+ to
, 0, block_off_end
- to
);
2738 if (block_off_start
< from
)
2739 memset(kaddr
+ block_off_start
, 0,
2740 from
- block_off_start
);
2741 flush_dcache_page(page
);
2742 kunmap_atomic(kaddr
, KM_USER0
);
2744 if ((em
->block_start
!= EXTENT_MAP_HOLE
&&
2745 em
->block_start
!= EXTENT_MAP_INLINE
) &&
2746 !isnew
&& !PageUptodate(page
) &&
2747 (block_off_end
> to
|| block_off_start
< from
) &&
2748 !test_range_bit(tree
, block_start
, cur_end
,
2749 EXTENT_UPTODATE
, 1)) {
2751 u64 extent_offset
= block_start
- em
->start
;
2753 sector
= (em
->block_start
+ extent_offset
) >> 9;
2754 iosize
= (cur_end
- block_start
+ blocksize
) &
2755 ~((u64
)blocksize
- 1);
2757 * we've already got the extent locked, but we
2758 * need to split the state such that our end_bio
2759 * handler can clear the lock.
2761 set_extent_bit(tree
, block_start
,
2762 block_start
+ iosize
- 1,
2763 EXTENT_LOCKED
, 0, NULL
, GFP_NOFS
);
2764 ret
= submit_extent_page(READ
, tree
, page
,
2765 sector
, iosize
, page_offset
, em
->bdev
,
2767 end_bio_extent_preparewrite
, 0,
2770 block_start
= block_start
+ iosize
;
2772 set_extent_uptodate(tree
, block_start
, cur_end
,
2774 unlock_extent(tree
, block_start
, cur_end
, GFP_NOFS
);
2775 block_start
= cur_end
+ 1;
2777 page_offset
= block_start
& (PAGE_CACHE_SIZE
- 1);
2778 free_extent_map(em
);
2781 wait_extent_bit(tree
, orig_block_start
,
2782 block_end
, EXTENT_LOCKED
);
2784 check_page_uptodate(tree
, page
);
2786 /* FIXME, zero out newly allocated blocks on error */
2791 * a helper for releasepage, this tests for areas of the page that
2792 * are locked or under IO and drops the related state bits if it is safe
2795 int try_release_extent_state(struct extent_map_tree
*map
,
2796 struct extent_io_tree
*tree
, struct page
*page
,
2799 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2800 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
2803 if (test_range_bit(tree
, start
, end
,
2804 EXTENT_IOBITS
| EXTENT_ORDERED
, 0))
2807 if ((mask
& GFP_NOFS
) == GFP_NOFS
)
2809 clear_extent_bit(tree
, start
, end
, EXTENT_UPTODATE
,
2816 * a helper for releasepage. As long as there are no locked extents
2817 * in the range corresponding to the page, both state records and extent
2818 * map records are removed
2820 int try_release_extent_mapping(struct extent_map_tree
*map
,
2821 struct extent_io_tree
*tree
, struct page
*page
,
2824 struct extent_map
*em
;
2825 u64 start
= (u64
)page
->index
<< PAGE_CACHE_SHIFT
;
2826 u64 end
= start
+ PAGE_CACHE_SIZE
- 1;
2828 if ((mask
& __GFP_WAIT
) &&
2829 page
->mapping
->host
->i_size
> 16 * 1024 * 1024) {
2831 while (start
<= end
) {
2832 len
= end
- start
+ 1;
2833 spin_lock(&map
->lock
);
2834 em
= lookup_extent_mapping(map
, start
, len
);
2835 if (!em
|| IS_ERR(em
)) {
2836 spin_unlock(&map
->lock
);
2839 if (test_bit(EXTENT_FLAG_PINNED
, &em
->flags
) ||
2840 em
->start
!= start
) {
2841 spin_unlock(&map
->lock
);
2842 free_extent_map(em
);
2845 if (!test_range_bit(tree
, em
->start
,
2846 extent_map_end(em
) - 1,
2847 EXTENT_LOCKED
| EXTENT_WRITEBACK
|
2850 remove_extent_mapping(map
, em
);
2851 /* once for the rb tree */
2852 free_extent_map(em
);
2854 start
= extent_map_end(em
);
2855 spin_unlock(&map
->lock
);
2858 free_extent_map(em
);
2861 return try_release_extent_state(map
, tree
, page
, mask
);
2864 sector_t
extent_bmap(struct address_space
*mapping
, sector_t iblock
,
2865 get_extent_t
*get_extent
)
2867 struct inode
*inode
= mapping
->host
;
2868 u64 start
= iblock
<< inode
->i_blkbits
;
2869 sector_t sector
= 0;
2870 size_t blksize
= (1 << inode
->i_blkbits
);
2871 struct extent_map
*em
;
2873 lock_extent(&BTRFS_I(inode
)->io_tree
, start
, start
+ blksize
- 1,
2875 em
= get_extent(inode
, NULL
, 0, start
, blksize
, 0);
2876 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
, start
+ blksize
- 1,
2878 if (!em
|| IS_ERR(em
))
2881 if (em
->block_start
> EXTENT_MAP_LAST_BYTE
)
2884 sector
= (em
->block_start
+ start
- em
->start
) >> inode
->i_blkbits
;
2886 free_extent_map(em
);
2890 int extent_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fieinfo
,
2891 __u64 start
, __u64 len
, get_extent_t
*get_extent
)
2895 u64 max
= start
+ len
;
2898 struct extent_map
*em
= NULL
;
2900 u64 em_start
= 0, em_len
= 0;
2901 unsigned long emflags
;
2907 lock_extent(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
,
2909 em
= get_extent(inode
, NULL
, 0, off
, max
- off
, 0);
2917 off
= em
->start
+ em
->len
;
2921 em_start
= em
->start
;
2927 if (em
->block_start
== EXTENT_MAP_LAST_BYTE
) {
2929 flags
|= FIEMAP_EXTENT_LAST
;
2930 } else if (em
->block_start
== EXTENT_MAP_HOLE
) {
2931 flags
|= FIEMAP_EXTENT_UNWRITTEN
;
2932 } else if (em
->block_start
== EXTENT_MAP_INLINE
) {
2933 flags
|= (FIEMAP_EXTENT_DATA_INLINE
|
2934 FIEMAP_EXTENT_NOT_ALIGNED
);
2935 } else if (em
->block_start
== EXTENT_MAP_DELALLOC
) {
2936 flags
|= (FIEMAP_EXTENT_DELALLOC
|
2937 FIEMAP_EXTENT_UNKNOWN
);
2939 disko
= em
->block_start
;
2941 if (test_bit(EXTENT_FLAG_COMPRESSED
, &em
->flags
))
2942 flags
|= FIEMAP_EXTENT_ENCODED
;
2944 emflags
= em
->flags
;
2945 free_extent_map(em
);
2949 em
= get_extent(inode
, NULL
, 0, off
, max
- off
, 0);
2956 emflags
= em
->flags
;
2958 if (test_bit(EXTENT_FLAG_VACANCY
, &emflags
)) {
2959 flags
|= FIEMAP_EXTENT_LAST
;
2963 ret
= fiemap_fill_next_extent(fieinfo
, em_start
, disko
,
2969 free_extent_map(em
);
2971 unlock_extent(&BTRFS_I(inode
)->io_tree
, start
, start
+ len
,
2976 static inline struct page
*extent_buffer_page(struct extent_buffer
*eb
,
2980 struct address_space
*mapping
;
2983 return eb
->first_page
;
2984 i
+= eb
->start
>> PAGE_CACHE_SHIFT
;
2985 mapping
= eb
->first_page
->mapping
;
2990 * extent_buffer_page is only called after pinning the page
2991 * by increasing the reference count. So we know the page must
2992 * be in the radix tree.
2995 p
= radix_tree_lookup(&mapping
->page_tree
, i
);
3001 static inline unsigned long num_extent_pages(u64 start
, u64 len
)
3003 return ((start
+ len
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
) -
3004 (start
>> PAGE_CACHE_SHIFT
);
3007 static struct extent_buffer
*__alloc_extent_buffer(struct extent_io_tree
*tree
,
3012 struct extent_buffer
*eb
= NULL
;
3014 unsigned long flags
;
3017 eb
= kmem_cache_zalloc(extent_buffer_cache
, mask
);
3020 spin_lock_init(&eb
->lock
);
3021 init_waitqueue_head(&eb
->lock_wq
);
3024 spin_lock_irqsave(&leak_lock
, flags
);
3025 list_add(&eb
->leak_list
, &buffers
);
3026 spin_unlock_irqrestore(&leak_lock
, flags
);
3028 atomic_set(&eb
->refs
, 1);
3033 static void __free_extent_buffer(struct extent_buffer
*eb
)
3036 unsigned long flags
;
3037 spin_lock_irqsave(&leak_lock
, flags
);
3038 list_del(&eb
->leak_list
);
3039 spin_unlock_irqrestore(&leak_lock
, flags
);
3041 kmem_cache_free(extent_buffer_cache
, eb
);
3044 struct extent_buffer
*alloc_extent_buffer(struct extent_io_tree
*tree
,
3045 u64 start
, unsigned long len
,
3049 unsigned long num_pages
= num_extent_pages(start
, len
);
3051 unsigned long index
= start
>> PAGE_CACHE_SHIFT
;
3052 struct extent_buffer
*eb
;
3053 struct extent_buffer
*exists
= NULL
;
3055 struct address_space
*mapping
= tree
->mapping
;
3058 spin_lock(&tree
->buffer_lock
);
3059 eb
= buffer_search(tree
, start
);
3061 atomic_inc(&eb
->refs
);
3062 spin_unlock(&tree
->buffer_lock
);
3063 mark_page_accessed(eb
->first_page
);
3066 spin_unlock(&tree
->buffer_lock
);
3068 eb
= __alloc_extent_buffer(tree
, start
, len
, mask
);
3073 eb
->first_page
= page0
;
3076 page_cache_get(page0
);
3077 mark_page_accessed(page0
);
3078 set_page_extent_mapped(page0
);
3079 set_page_extent_head(page0
, len
);
3080 uptodate
= PageUptodate(page0
);
3084 for (; i
< num_pages
; i
++, index
++) {
3085 p
= find_or_create_page(mapping
, index
, mask
| __GFP_HIGHMEM
);
3090 set_page_extent_mapped(p
);
3091 mark_page_accessed(p
);
3094 set_page_extent_head(p
, len
);
3096 set_page_private(p
, EXTENT_PAGE_PRIVATE
);
3098 if (!PageUptodate(p
))
3103 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3105 spin_lock(&tree
->buffer_lock
);
3106 exists
= buffer_tree_insert(tree
, start
, &eb
->rb_node
);
3108 /* add one reference for the caller */
3109 atomic_inc(&exists
->refs
);
3110 spin_unlock(&tree
->buffer_lock
);
3113 spin_unlock(&tree
->buffer_lock
);
3115 /* add one reference for the tree */
3116 atomic_inc(&eb
->refs
);
3120 if (!atomic_dec_and_test(&eb
->refs
))
3122 for (index
= 1; index
< i
; index
++)
3123 page_cache_release(extent_buffer_page(eb
, index
));
3124 page_cache_release(extent_buffer_page(eb
, 0));
3125 __free_extent_buffer(eb
);
3129 struct extent_buffer
*find_extent_buffer(struct extent_io_tree
*tree
,
3130 u64 start
, unsigned long len
,
3133 struct extent_buffer
*eb
;
3135 spin_lock(&tree
->buffer_lock
);
3136 eb
= buffer_search(tree
, start
);
3138 atomic_inc(&eb
->refs
);
3139 spin_unlock(&tree
->buffer_lock
);
3142 mark_page_accessed(eb
->first_page
);
3147 void free_extent_buffer(struct extent_buffer
*eb
)
3152 if (!atomic_dec_and_test(&eb
->refs
))
3158 int clear_extent_buffer_dirty(struct extent_io_tree
*tree
,
3159 struct extent_buffer
*eb
)
3162 unsigned long num_pages
;
3165 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3167 for (i
= 0; i
< num_pages
; i
++) {
3168 page
= extent_buffer_page(eb
, i
);
3169 if (!PageDirty(page
))
3174 set_page_extent_head(page
, eb
->len
);
3176 set_page_private(page
, EXTENT_PAGE_PRIVATE
);
3178 clear_page_dirty_for_io(page
);
3179 spin_lock_irq(&page
->mapping
->tree_lock
);
3180 if (!PageDirty(page
)) {
3181 radix_tree_tag_clear(&page
->mapping
->page_tree
,
3183 PAGECACHE_TAG_DIRTY
);
3185 spin_unlock_irq(&page
->mapping
->tree_lock
);
3191 int wait_on_extent_buffer_writeback(struct extent_io_tree
*tree
,
3192 struct extent_buffer
*eb
)
3194 return wait_on_extent_writeback(tree
, eb
->start
,
3195 eb
->start
+ eb
->len
- 1);
3198 int set_extent_buffer_dirty(struct extent_io_tree
*tree
,
3199 struct extent_buffer
*eb
)
3202 unsigned long num_pages
;
3205 was_dirty
= test_and_set_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
);
3206 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3207 for (i
= 0; i
< num_pages
; i
++)
3208 __set_page_dirty_nobuffers(extent_buffer_page(eb
, i
));
3212 int clear_extent_buffer_uptodate(struct extent_io_tree
*tree
,
3213 struct extent_buffer
*eb
)
3217 unsigned long num_pages
;
3219 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3220 clear_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3222 clear_extent_uptodate(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3224 for (i
= 0; i
< num_pages
; i
++) {
3225 page
= extent_buffer_page(eb
, i
);
3227 ClearPageUptodate(page
);
3232 int set_extent_buffer_uptodate(struct extent_io_tree
*tree
,
3233 struct extent_buffer
*eb
)
3237 unsigned long num_pages
;
3239 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3241 set_extent_uptodate(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3243 for (i
= 0; i
< num_pages
; i
++) {
3244 page
= extent_buffer_page(eb
, i
);
3245 if ((i
== 0 && (eb
->start
& (PAGE_CACHE_SIZE
- 1))) ||
3246 ((i
== num_pages
- 1) &&
3247 ((eb
->start
+ eb
->len
) & (PAGE_CACHE_SIZE
- 1)))) {
3248 check_page_uptodate(tree
, page
);
3251 SetPageUptodate(page
);
3256 int extent_range_uptodate(struct extent_io_tree
*tree
,
3261 int pg_uptodate
= 1;
3263 unsigned long index
;
3265 ret
= test_range_bit(tree
, start
, end
, EXTENT_UPTODATE
, 1);
3268 while (start
<= end
) {
3269 index
= start
>> PAGE_CACHE_SHIFT
;
3270 page
= find_get_page(tree
->mapping
, index
);
3271 uptodate
= PageUptodate(page
);
3272 page_cache_release(page
);
3277 start
+= PAGE_CACHE_SIZE
;
3282 int extent_buffer_uptodate(struct extent_io_tree
*tree
,
3283 struct extent_buffer
*eb
)
3286 unsigned long num_pages
;
3289 int pg_uptodate
= 1;
3291 if (test_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
))
3294 ret
= test_range_bit(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3295 EXTENT_UPTODATE
, 1);
3299 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3300 for (i
= 0; i
< num_pages
; i
++) {
3301 page
= extent_buffer_page(eb
, i
);
3302 if (!PageUptodate(page
)) {
3310 int read_extent_buffer_pages(struct extent_io_tree
*tree
,
3311 struct extent_buffer
*eb
,
3312 u64 start
, int wait
,
3313 get_extent_t
*get_extent
, int mirror_num
)
3316 unsigned long start_i
;
3320 int locked_pages
= 0;
3321 int all_uptodate
= 1;
3322 int inc_all_pages
= 0;
3323 unsigned long num_pages
;
3324 struct bio
*bio
= NULL
;
3325 unsigned long bio_flags
= 0;
3327 if (test_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
))
3330 if (test_range_bit(tree
, eb
->start
, eb
->start
+ eb
->len
- 1,
3331 EXTENT_UPTODATE
, 1)) {
3336 WARN_ON(start
< eb
->start
);
3337 start_i
= (start
>> PAGE_CACHE_SHIFT
) -
3338 (eb
->start
>> PAGE_CACHE_SHIFT
);
3343 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3344 for (i
= start_i
; i
< num_pages
; i
++) {
3345 page
= extent_buffer_page(eb
, i
);
3347 if (!trylock_page(page
))
3353 if (!PageUptodate(page
))
3358 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3362 for (i
= start_i
; i
< num_pages
; i
++) {
3363 page
= extent_buffer_page(eb
, i
);
3365 page_cache_get(page
);
3366 if (!PageUptodate(page
)) {
3369 ClearPageError(page
);
3370 err
= __extent_read_full_page(tree
, page
,
3372 mirror_num
, &bio_flags
);
3381 submit_one_bio(READ
, bio
, mirror_num
, bio_flags
);
3386 for (i
= start_i
; i
< num_pages
; i
++) {
3387 page
= extent_buffer_page(eb
, i
);
3388 wait_on_page_locked(page
);
3389 if (!PageUptodate(page
))
3394 set_bit(EXTENT_BUFFER_UPTODATE
, &eb
->bflags
);
3399 while (locked_pages
> 0) {
3400 page
= extent_buffer_page(eb
, i
);
3408 void read_extent_buffer(struct extent_buffer
*eb
, void *dstv
,
3409 unsigned long start
,
3416 char *dst
= (char *)dstv
;
3417 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3418 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3420 WARN_ON(start
> eb
->len
);
3421 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3423 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3426 page
= extent_buffer_page(eb
, i
);
3428 cur
= min(len
, (PAGE_CACHE_SIZE
- offset
));
3429 kaddr
= kmap_atomic(page
, KM_USER1
);
3430 memcpy(dst
, kaddr
+ offset
, cur
);
3431 kunmap_atomic(kaddr
, KM_USER1
);
3440 int map_private_extent_buffer(struct extent_buffer
*eb
, unsigned long start
,
3441 unsigned long min_len
, char **token
, char **map
,
3442 unsigned long *map_start
,
3443 unsigned long *map_len
, int km
)
3445 size_t offset
= start
& (PAGE_CACHE_SIZE
- 1);
3448 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3449 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3450 unsigned long end_i
= (start_offset
+ start
+ min_len
- 1) >>
3457 offset
= start_offset
;
3461 *map_start
= ((u64
)i
<< PAGE_CACHE_SHIFT
) - start_offset
;
3464 if (start
+ min_len
> eb
->len
) {
3465 printk(KERN_ERR
"btrfs bad mapping eb start %llu len %lu, "
3466 "wanted %lu %lu\n", (unsigned long long)eb
->start
,
3467 eb
->len
, start
, min_len
);
3471 p
= extent_buffer_page(eb
, i
);
3472 kaddr
= kmap_atomic(p
, km
);
3474 *map
= kaddr
+ offset
;
3475 *map_len
= PAGE_CACHE_SIZE
- offset
;
3479 int map_extent_buffer(struct extent_buffer
*eb
, unsigned long start
,
3480 unsigned long min_len
,
3481 char **token
, char **map
,
3482 unsigned long *map_start
,
3483 unsigned long *map_len
, int km
)
3487 if (eb
->map_token
) {
3488 unmap_extent_buffer(eb
, eb
->map_token
, km
);
3489 eb
->map_token
= NULL
;
3492 err
= map_private_extent_buffer(eb
, start
, min_len
, token
, map
,
3493 map_start
, map_len
, km
);
3495 eb
->map_token
= *token
;
3497 eb
->map_start
= *map_start
;
3498 eb
->map_len
= *map_len
;
3503 void unmap_extent_buffer(struct extent_buffer
*eb
, char *token
, int km
)
3505 kunmap_atomic(token
, km
);
3508 int memcmp_extent_buffer(struct extent_buffer
*eb
, const void *ptrv
,
3509 unsigned long start
,
3516 char *ptr
= (char *)ptrv
;
3517 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3518 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3521 WARN_ON(start
> eb
->len
);
3522 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3524 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3527 page
= extent_buffer_page(eb
, i
);
3529 cur
= min(len
, (PAGE_CACHE_SIZE
- offset
));
3531 kaddr
= kmap_atomic(page
, KM_USER0
);
3532 ret
= memcmp(ptr
, kaddr
+ offset
, cur
);
3533 kunmap_atomic(kaddr
, KM_USER0
);
3545 void write_extent_buffer(struct extent_buffer
*eb
, const void *srcv
,
3546 unsigned long start
, unsigned long len
)
3552 char *src
= (char *)srcv
;
3553 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3554 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3556 WARN_ON(start
> eb
->len
);
3557 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3559 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3562 page
= extent_buffer_page(eb
, i
);
3563 WARN_ON(!PageUptodate(page
));
3565 cur
= min(len
, PAGE_CACHE_SIZE
- offset
);
3566 kaddr
= kmap_atomic(page
, KM_USER1
);
3567 memcpy(kaddr
+ offset
, src
, cur
);
3568 kunmap_atomic(kaddr
, KM_USER1
);
3577 void memset_extent_buffer(struct extent_buffer
*eb
, char c
,
3578 unsigned long start
, unsigned long len
)
3584 size_t start_offset
= eb
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3585 unsigned long i
= (start_offset
+ start
) >> PAGE_CACHE_SHIFT
;
3587 WARN_ON(start
> eb
->len
);
3588 WARN_ON(start
+ len
> eb
->start
+ eb
->len
);
3590 offset
= (start_offset
+ start
) & ((unsigned long)PAGE_CACHE_SIZE
- 1);
3593 page
= extent_buffer_page(eb
, i
);
3594 WARN_ON(!PageUptodate(page
));
3596 cur
= min(len
, PAGE_CACHE_SIZE
- offset
);
3597 kaddr
= kmap_atomic(page
, KM_USER0
);
3598 memset(kaddr
+ offset
, c
, cur
);
3599 kunmap_atomic(kaddr
, KM_USER0
);
3607 void copy_extent_buffer(struct extent_buffer
*dst
, struct extent_buffer
*src
,
3608 unsigned long dst_offset
, unsigned long src_offset
,
3611 u64 dst_len
= dst
->len
;
3616 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3617 unsigned long i
= (start_offset
+ dst_offset
) >> PAGE_CACHE_SHIFT
;
3619 WARN_ON(src
->len
!= dst_len
);
3621 offset
= (start_offset
+ dst_offset
) &
3622 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3625 page
= extent_buffer_page(dst
, i
);
3626 WARN_ON(!PageUptodate(page
));
3628 cur
= min(len
, (unsigned long)(PAGE_CACHE_SIZE
- offset
));
3630 kaddr
= kmap_atomic(page
, KM_USER0
);
3631 read_extent_buffer(src
, kaddr
+ offset
, src_offset
, cur
);
3632 kunmap_atomic(kaddr
, KM_USER0
);
3641 static void move_pages(struct page
*dst_page
, struct page
*src_page
,
3642 unsigned long dst_off
, unsigned long src_off
,
3645 char *dst_kaddr
= kmap_atomic(dst_page
, KM_USER0
);
3646 if (dst_page
== src_page
) {
3647 memmove(dst_kaddr
+ dst_off
, dst_kaddr
+ src_off
, len
);
3649 char *src_kaddr
= kmap_atomic(src_page
, KM_USER1
);
3650 char *p
= dst_kaddr
+ dst_off
+ len
;
3651 char *s
= src_kaddr
+ src_off
+ len
;
3656 kunmap_atomic(src_kaddr
, KM_USER1
);
3658 kunmap_atomic(dst_kaddr
, KM_USER0
);
3661 static void copy_pages(struct page
*dst_page
, struct page
*src_page
,
3662 unsigned long dst_off
, unsigned long src_off
,
3665 char *dst_kaddr
= kmap_atomic(dst_page
, KM_USER0
);
3668 if (dst_page
!= src_page
)
3669 src_kaddr
= kmap_atomic(src_page
, KM_USER1
);
3671 src_kaddr
= dst_kaddr
;
3673 memcpy(dst_kaddr
+ dst_off
, src_kaddr
+ src_off
, len
);
3674 kunmap_atomic(dst_kaddr
, KM_USER0
);
3675 if (dst_page
!= src_page
)
3676 kunmap_atomic(src_kaddr
, KM_USER1
);
3679 void memcpy_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
3680 unsigned long src_offset
, unsigned long len
)
3683 size_t dst_off_in_page
;
3684 size_t src_off_in_page
;
3685 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3686 unsigned long dst_i
;
3687 unsigned long src_i
;
3689 if (src_offset
+ len
> dst
->len
) {
3690 printk(KERN_ERR
"btrfs memmove bogus src_offset %lu move "
3691 "len %lu dst len %lu\n", src_offset
, len
, dst
->len
);
3694 if (dst_offset
+ len
> dst
->len
) {
3695 printk(KERN_ERR
"btrfs memmove bogus dst_offset %lu move "
3696 "len %lu dst len %lu\n", dst_offset
, len
, dst
->len
);
3701 dst_off_in_page
= (start_offset
+ dst_offset
) &
3702 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3703 src_off_in_page
= (start_offset
+ src_offset
) &
3704 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3706 dst_i
= (start_offset
+ dst_offset
) >> PAGE_CACHE_SHIFT
;
3707 src_i
= (start_offset
+ src_offset
) >> PAGE_CACHE_SHIFT
;
3709 cur
= min(len
, (unsigned long)(PAGE_CACHE_SIZE
-
3711 cur
= min_t(unsigned long, cur
,
3712 (unsigned long)(PAGE_CACHE_SIZE
- dst_off_in_page
));
3714 copy_pages(extent_buffer_page(dst
, dst_i
),
3715 extent_buffer_page(dst
, src_i
),
3716 dst_off_in_page
, src_off_in_page
, cur
);
3724 void memmove_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
3725 unsigned long src_offset
, unsigned long len
)
3728 size_t dst_off_in_page
;
3729 size_t src_off_in_page
;
3730 unsigned long dst_end
= dst_offset
+ len
- 1;
3731 unsigned long src_end
= src_offset
+ len
- 1;
3732 size_t start_offset
= dst
->start
& ((u64
)PAGE_CACHE_SIZE
- 1);
3733 unsigned long dst_i
;
3734 unsigned long src_i
;
3736 if (src_offset
+ len
> dst
->len
) {
3737 printk(KERN_ERR
"btrfs memmove bogus src_offset %lu move "
3738 "len %lu len %lu\n", src_offset
, len
, dst
->len
);
3741 if (dst_offset
+ len
> dst
->len
) {
3742 printk(KERN_ERR
"btrfs memmove bogus dst_offset %lu move "
3743 "len %lu len %lu\n", dst_offset
, len
, dst
->len
);
3746 if (dst_offset
< src_offset
) {
3747 memcpy_extent_buffer(dst
, dst_offset
, src_offset
, len
);
3751 dst_i
= (start_offset
+ dst_end
) >> PAGE_CACHE_SHIFT
;
3752 src_i
= (start_offset
+ src_end
) >> PAGE_CACHE_SHIFT
;
3754 dst_off_in_page
= (start_offset
+ dst_end
) &
3755 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3756 src_off_in_page
= (start_offset
+ src_end
) &
3757 ((unsigned long)PAGE_CACHE_SIZE
- 1);
3759 cur
= min_t(unsigned long, len
, src_off_in_page
+ 1);
3760 cur
= min(cur
, dst_off_in_page
+ 1);
3761 move_pages(extent_buffer_page(dst
, dst_i
),
3762 extent_buffer_page(dst
, src_i
),
3763 dst_off_in_page
- cur
+ 1,
3764 src_off_in_page
- cur
+ 1, cur
);
3772 int try_release_extent_buffer(struct extent_io_tree
*tree
, struct page
*page
)
3774 u64 start
= page_offset(page
);
3775 struct extent_buffer
*eb
;
3778 unsigned long num_pages
;
3780 spin_lock(&tree
->buffer_lock
);
3781 eb
= buffer_search(tree
, start
);
3785 if (atomic_read(&eb
->refs
) > 1) {
3789 if (test_bit(EXTENT_BUFFER_DIRTY
, &eb
->bflags
)) {
3793 /* at this point we can safely release the extent buffer */
3794 num_pages
= num_extent_pages(eb
->start
, eb
->len
);
3795 for (i
= 0; i
< num_pages
; i
++)
3796 page_cache_release(extent_buffer_page(eb
, i
));
3797 rb_erase(&eb
->rb_node
, &tree
->buffer
);
3798 __free_extent_buffer(eb
);
3800 spin_unlock(&tree
->buffer_lock
);