3 * Copyright (C) 2007 Oracle. All rights reserved.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public
7 * License v2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 021110-1307, USA.
19 #define _XOPEN_SOURCE 600
23 #include <sys/types.h>
27 #include "kerncompat.h"
28 #include "extent_io.h"
31 u64 cache_max
= 1024 * 1024 * 32;
33 void extent_io_tree_init(struct extent_io_tree
*tree
)
35 cache_tree_init(&tree
->state
);
36 cache_tree_init(&tree
->cache
);
37 INIT_LIST_HEAD(&tree
->lru
);
41 static struct extent_state
*alloc_extent_state(void)
43 struct extent_state
*state
;
45 state
= malloc(sizeof(*state
));
54 static void free_extent_state(struct extent_state
*state
)
57 BUG_ON(state
->refs
< 0);
62 void extent_io_tree_cleanup(struct extent_io_tree
*tree
)
64 struct extent_state
*es
;
65 struct extent_buffer
*eb
;
66 struct cache_extent
*cache
;
68 while(!list_empty(&tree
->lru
)) {
69 eb
= list_entry(tree
->lru
.next
, struct extent_buffer
, lru
);
71 fprintf(stderr
, "extent buffer leak: "
72 "start %llu len %u\n",
73 (unsigned long long)eb
->start
, eb
->len
);
76 free_extent_buffer(eb
);
79 cache
= find_first_cache_extent(&tree
->state
, 0);
82 es
= container_of(cache
, struct extent_state
, cache_node
);
83 remove_cache_extent(&tree
->state
, &es
->cache_node
);
84 free_extent_state(es
);
88 static inline void update_extent_state(struct extent_state
*state
)
90 state
->cache_node
.start
= state
->start
;
91 state
->cache_node
.size
= state
->end
+ 1 - state
->start
;
95 * Utility function to look for merge candidates inside a given range.
96 * Any extents with matching state are merged together into a single
97 * extent in the tree. Extents with EXTENT_IO in their state field are
100 static int merge_state(struct extent_io_tree
*tree
,
101 struct extent_state
*state
)
103 struct extent_state
*other
;
104 struct cache_extent
*other_node
;
106 if (state
->state
& EXTENT_IOBITS
)
109 other_node
= prev_cache_extent(&state
->cache_node
);
111 other
= container_of(other_node
, struct extent_state
,
113 if (other
->end
== state
->start
- 1 &&
114 other
->state
== state
->state
) {
115 state
->start
= other
->start
;
116 update_extent_state(state
);
117 remove_cache_extent(&tree
->state
, &other
->cache_node
);
118 free_extent_state(other
);
121 other_node
= next_cache_extent(&state
->cache_node
);
123 other
= container_of(other_node
, struct extent_state
,
125 if (other
->start
== state
->end
+ 1 &&
126 other
->state
== state
->state
) {
127 other
->start
= state
->start
;
128 update_extent_state(other
);
129 remove_cache_extent(&tree
->state
, &state
->cache_node
);
130 free_extent_state(state
);
137 * insert an extent_state struct into the tree. 'bits' are set on the
138 * struct before it is inserted.
140 static int insert_state(struct extent_io_tree
*tree
,
141 struct extent_state
*state
, u64 start
, u64 end
,
147 state
->state
|= bits
;
148 state
->start
= start
;
150 update_extent_state(state
);
151 ret
= insert_existing_cache_extent(&tree
->state
, &state
->cache_node
);
153 merge_state(tree
, state
);
158 * split a given extent state struct in two, inserting the preallocated
159 * struct 'prealloc' as the newly created second half. 'split' indicates an
160 * offset inside 'orig' where it should be split.
162 static int split_state(struct extent_io_tree
*tree
, struct extent_state
*orig
,
163 struct extent_state
*prealloc
, u64 split
)
166 prealloc
->start
= orig
->start
;
167 prealloc
->end
= split
- 1;
168 prealloc
->state
= orig
->state
;
169 update_extent_state(prealloc
);
171 update_extent_state(orig
);
172 ret
= insert_existing_cache_extent(&tree
->state
,
173 &prealloc
->cache_node
);
179 * clear some bits on a range in the tree.
181 static int clear_state_bit(struct extent_io_tree
*tree
,
182 struct extent_state
*state
, int bits
)
184 int ret
= state
->state
& bits
;
186 state
->state
&= ~bits
;
187 if (state
->state
== 0) {
188 remove_cache_extent(&tree
->state
, &state
->cache_node
);
189 free_extent_state(state
);
191 merge_state(tree
, state
);
197 * set some bits on a range in the tree.
199 int clear_extent_bits(struct extent_io_tree
*tree
, u64 start
,
200 u64 end
, int bits
, gfp_t mask
)
202 struct extent_state
*state
;
203 struct extent_state
*prealloc
= NULL
;
204 struct cache_extent
*node
;
209 prealloc
= alloc_extent_state();
214 * this search will find the extents that end after
217 node
= find_first_cache_extent(&tree
->state
, start
);
220 state
= container_of(node
, struct extent_state
, cache_node
);
221 if (state
->start
> end
)
225 * | ---- desired range ---- |
227 * | ------------- state -------------- |
229 * We need to split the extent we found, and may flip
230 * bits on second half.
232 * If the extent we found extends past our range, we
233 * just split and search again. It'll get split again
234 * the next time though.
236 * If the extent we found is inside our range, we clear
237 * the desired bit on it.
239 if (state
->start
< start
) {
240 err
= split_state(tree
, state
, prealloc
, start
);
241 BUG_ON(err
== -EEXIST
);
245 if (state
->end
<= end
) {
246 start
= state
->end
+ 1;
247 set
|= clear_state_bit(tree
, state
, bits
);
249 start
= state
->start
;
254 * | ---- desired range ---- |
256 * We need to split the extent, and clear the bit
259 if (state
->start
<= end
&& state
->end
> end
) {
260 err
= split_state(tree
, state
, prealloc
, end
+ 1);
261 BUG_ON(err
== -EEXIST
);
263 set
|= clear_state_bit(tree
, prealloc
, bits
);
268 start
= state
->end
+ 1;
269 set
|= clear_state_bit(tree
, state
, bits
);
273 free_extent_state(prealloc
);
283 * set some bits on a range in the tree.
285 int set_extent_bits(struct extent_io_tree
*tree
, u64 start
,
286 u64 end
, int bits
, gfp_t mask
)
288 struct extent_state
*state
;
289 struct extent_state
*prealloc
= NULL
;
290 struct cache_extent
*node
;
296 prealloc
= alloc_extent_state();
301 * this search will find the extents that end after
304 node
= find_first_cache_extent(&tree
->state
, start
);
306 err
= insert_state(tree
, prealloc
, start
, end
, bits
);
307 BUG_ON(err
== -EEXIST
);
312 state
= container_of(node
, struct extent_state
, cache_node
);
313 last_start
= state
->start
;
314 last_end
= state
->end
;
317 * | ---- desired range ---- |
320 * Just lock what we found and keep going
322 if (state
->start
== start
&& state
->end
<= end
) {
323 set
= state
->state
& bits
;
324 state
->state
|= bits
;
325 start
= state
->end
+ 1;
326 merge_state(tree
, state
);
330 * | ---- desired range ---- |
333 * | ------------- state -------------- |
335 * We need to split the extent we found, and may flip bits on
338 * If the extent we found extends past our
339 * range, we just split and search again. It'll get split
340 * again the next time though.
342 * If the extent we found is inside our range, we set the
345 if (state
->start
< start
) {
346 set
= state
->state
& bits
;
347 err
= split_state(tree
, state
, prealloc
, start
);
348 BUG_ON(err
== -EEXIST
);
352 if (state
->end
<= end
) {
353 state
->state
|= bits
;
354 start
= state
->end
+ 1;
355 merge_state(tree
, state
);
357 start
= state
->start
;
362 * | ---- desired range ---- |
363 * | state | or | state |
365 * There's a hole, we need to insert something in it and
366 * ignore the extent we found.
368 if (state
->start
> start
) {
370 if (end
< last_start
)
373 this_end
= last_start
-1;
374 err
= insert_state(tree
, prealloc
, start
, this_end
,
376 BUG_ON(err
== -EEXIST
);
380 start
= this_end
+ 1;
384 * | ---- desired range ---- |
385 * | ---------- state ---------- |
386 * We need to split the extent, and set the bit
389 set
= state
->state
& bits
;
390 err
= split_state(tree
, state
, prealloc
, end
+ 1);
391 BUG_ON(err
== -EEXIST
);
393 state
->state
|= bits
;
394 merge_state(tree
, prealloc
);
398 free_extent_state(prealloc
);
406 int set_extent_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
,
409 return set_extent_bits(tree
, start
, end
, EXTENT_DIRTY
, mask
);
412 int clear_extent_dirty(struct extent_io_tree
*tree
, u64 start
, u64 end
,
415 return clear_extent_bits(tree
, start
, end
, EXTENT_DIRTY
, mask
);
418 int find_first_extent_bit(struct extent_io_tree
*tree
, u64 start
,
419 u64
*start_ret
, u64
*end_ret
, int bits
)
421 struct cache_extent
*node
;
422 struct extent_state
*state
;
426 * this search will find all the extents that end after
429 node
= find_first_cache_extent(&tree
->state
, start
);
434 state
= container_of(node
, struct extent_state
, cache_node
);
435 if (state
->end
>= start
&& (state
->state
& bits
)) {
436 *start_ret
= state
->start
;
437 *end_ret
= state
->end
;
441 node
= next_cache_extent(node
);
449 int test_range_bit(struct extent_io_tree
*tree
, u64 start
, u64 end
,
450 int bits
, int filled
)
452 struct extent_state
*state
= NULL
;
453 struct cache_extent
*node
;
456 node
= find_first_cache_extent(&tree
->state
, start
);
457 while (node
&& start
<= end
) {
458 state
= container_of(node
, struct extent_state
, cache_node
);
460 if (filled
&& state
->start
> start
) {
464 if (state
->start
> end
)
466 if (state
->state
& bits
) {
474 start
= state
->end
+ 1;
477 node
= next_cache_extent(node
);
487 int set_state_private(struct extent_io_tree
*tree
, u64 start
, u64
private)
489 struct cache_extent
*node
;
490 struct extent_state
*state
;
493 node
= find_first_cache_extent(&tree
->state
, start
);
498 state
= container_of(node
, struct extent_state
, cache_node
);
499 if (state
->start
!= start
) {
503 state
->private = private;
508 int get_state_private(struct extent_io_tree
*tree
, u64 start
, u64
*private)
510 struct cache_extent
*node
;
511 struct extent_state
*state
;
514 node
= find_first_cache_extent(&tree
->state
, start
);
519 state
= container_of(node
, struct extent_state
, cache_node
);
520 if (state
->start
!= start
) {
524 *private = state
->private;
529 static int free_some_buffers(struct extent_io_tree
*tree
)
532 struct extent_buffer
*eb
;
533 struct list_head
*node
, *next
;
535 if (tree
->cache_size
< cache_max
)
537 list_for_each_safe(node
, next
, &tree
->lru
) {
538 eb
= list_entry(node
, struct extent_buffer
, lru
);
540 free_extent_buffer(eb
);
541 if (tree
->cache_size
< cache_max
)
544 list_move_tail(&eb
->lru
, &tree
->lru
);
552 static struct extent_buffer
*__alloc_extent_buffer(struct extent_io_tree
*tree
,
553 u64 bytenr
, u32 blocksize
)
555 struct extent_buffer
*eb
;
558 eb
= malloc(sizeof(struct extent_buffer
) + blocksize
);
570 eb
->dev_bytenr
= (u64
)-1;
571 eb
->cache_node
.start
= bytenr
;
572 eb
->cache_node
.size
= blocksize
;
574 free_some_buffers(tree
);
575 ret
= insert_existing_cache_extent(&tree
->cache
, &eb
->cache_node
);
580 list_add_tail(&eb
->lru
, &tree
->lru
);
581 tree
->cache_size
+= blocksize
;
585 void free_extent_buffer(struct extent_buffer
*eb
)
591 BUG_ON(eb
->refs
< 0);
593 struct extent_io_tree
*tree
= eb
->tree
;
594 BUG_ON(eb
->flags
& EXTENT_DIRTY
);
595 list_del_init(&eb
->lru
);
596 remove_cache_extent(&tree
->cache
, &eb
->cache_node
);
597 BUG_ON(tree
->cache_size
< eb
->len
);
598 tree
->cache_size
-= eb
->len
;
603 struct extent_buffer
*find_extent_buffer(struct extent_io_tree
*tree
,
604 u64 bytenr
, u32 blocksize
)
606 struct extent_buffer
*eb
= NULL
;
607 struct cache_extent
*cache
;
609 cache
= find_cache_extent(&tree
->cache
, bytenr
, blocksize
);
610 if (cache
&& cache
->start
== bytenr
&& cache
->size
== blocksize
) {
611 eb
= container_of(cache
, struct extent_buffer
, cache_node
);
612 list_move_tail(&eb
->lru
, &tree
->lru
);
618 struct extent_buffer
*find_first_extent_buffer(struct extent_io_tree
*tree
,
621 struct extent_buffer
*eb
= NULL
;
622 struct cache_extent
*cache
;
624 cache
= find_first_cache_extent(&tree
->cache
, start
);
626 eb
= container_of(cache
, struct extent_buffer
, cache_node
);
627 list_move_tail(&eb
->lru
, &tree
->lru
);
633 struct extent_buffer
*alloc_extent_buffer(struct extent_io_tree
*tree
,
634 u64 bytenr
, u32 blocksize
)
636 struct extent_buffer
*eb
;
637 struct cache_extent
*cache
;
639 cache
= find_cache_extent(&tree
->cache
, bytenr
, blocksize
);
640 if (cache
&& cache
->start
== bytenr
&& cache
->size
== blocksize
) {
641 eb
= container_of(cache
, struct extent_buffer
, cache_node
);
642 list_move_tail(&eb
->lru
, &tree
->lru
);
646 eb
= container_of(cache
, struct extent_buffer
,
648 BUG_ON(eb
->refs
!= 1);
649 free_extent_buffer(eb
);
651 eb
= __alloc_extent_buffer(tree
, bytenr
, blocksize
);
656 int read_extent_from_disk(struct extent_buffer
*eb
)
659 ret
= pread(eb
->fd
, eb
->data
, eb
->len
, eb
->dev_bytenr
);
662 if (ret
!= eb
->len
) {
671 int write_extent_to_disk(struct extent_buffer
*eb
)
674 ret
= pwrite(eb
->fd
, eb
->data
, eb
->len
, eb
->dev_bytenr
);
677 if (ret
!= eb
->len
) {
686 int set_extent_buffer_uptodate(struct extent_buffer
*eb
)
688 eb
->flags
|= EXTENT_UPTODATE
;
692 int clear_extent_buffer_uptodate(struct extent_io_tree
*tree
,
693 struct extent_buffer
*eb
)
695 eb
->flags
&= ~EXTENT_UPTODATE
;
699 int extent_buffer_uptodate(struct extent_buffer
*eb
)
701 if (eb
->flags
& EXTENT_UPTODATE
)
706 int set_extent_buffer_dirty(struct extent_buffer
*eb
)
708 struct extent_io_tree
*tree
= eb
->tree
;
709 if (!(eb
->flags
& EXTENT_DIRTY
)) {
710 eb
->flags
|= EXTENT_DIRTY
;
711 set_extent_dirty(tree
, eb
->start
, eb
->start
+ eb
->len
- 1, 0);
712 extent_buffer_get(eb
);
717 int clear_extent_buffer_dirty(struct extent_buffer
*eb
)
719 struct extent_io_tree
*tree
= eb
->tree
;
720 if (eb
->flags
& EXTENT_DIRTY
) {
721 eb
->flags
&= ~EXTENT_DIRTY
;
722 clear_extent_dirty(tree
, eb
->start
, eb
->start
+ eb
->len
- 1, 0);
723 free_extent_buffer(eb
);
728 int memcmp_extent_buffer(struct extent_buffer
*eb
, const void *ptrv
,
729 unsigned long start
, unsigned long len
)
731 return memcmp(eb
->data
+ start
, ptrv
, len
);
734 void read_extent_buffer(struct extent_buffer
*eb
, void *dst
,
735 unsigned long start
, unsigned long len
)
737 memcpy(dst
, eb
->data
+ start
, len
);
740 void write_extent_buffer(struct extent_buffer
*eb
, const void *src
,
741 unsigned long start
, unsigned long len
)
743 memcpy(eb
->data
+ start
, src
, len
);
746 void copy_extent_buffer(struct extent_buffer
*dst
, struct extent_buffer
*src
,
747 unsigned long dst_offset
, unsigned long src_offset
,
750 memcpy(dst
->data
+ dst_offset
, src
->data
+ src_offset
, len
);
753 void memcpy_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
754 unsigned long src_offset
, unsigned long len
)
756 memcpy(dst
->data
+ dst_offset
, dst
->data
+ src_offset
, len
);
759 void memmove_extent_buffer(struct extent_buffer
*dst
, unsigned long dst_offset
,
760 unsigned long src_offset
, unsigned long len
)
762 memmove(dst
->data
+ dst_offset
, dst
->data
+ src_offset
, len
);
765 void memset_extent_buffer(struct extent_buffer
*eb
, char c
,
766 unsigned long start
, unsigned long len
)
768 memset(eb
->data
+ start
, c
, len
);