Fix mispatch of the experimental warnings
[btrfs-progs-unstable/devel.git] / extent_io.c
blob4920b291eb7917346eb81787cea666fde05fc026
2 /*
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
20 #define __USE_XOPEN2K
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include "kerncompat.h"
28 #include "extent_io.h"
29 #include "list.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);
38 tree->cache_size = 0;
41 static struct extent_state *alloc_extent_state(void)
43 struct extent_state *state;
45 state = malloc(sizeof(*state));
46 if (!state)
47 return NULL;
48 state->refs = 1;
49 state->state = 0;
50 state->private = 0;
51 return state;
54 static void free_extent_state(struct extent_state *state)
56 state->refs--;
57 BUG_ON(state->refs < 0);
58 if (state->refs == 0)
59 free(state);
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);
70 if (eb->refs != 1) {
71 fprintf(stderr, "extent buffer leak: "
72 "start %llu len %u\n",
73 (unsigned long long)eb->start, eb->len);
74 eb->refs = 1;
76 free_extent_buffer(eb);
78 while (1) {
79 cache = find_first_cache_extent(&tree->state, 0);
80 if (!cache)
81 break;
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
98 * not merged
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)
107 return 0;
109 other_node = prev_cache_extent(&state->cache_node);
110 if (other_node) {
111 other = container_of(other_node, struct extent_state,
112 cache_node);
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);
122 if (other_node) {
123 other = container_of(other_node, struct extent_state,
124 cache_node);
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);
133 return 0;
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,
142 int bits)
144 int ret;
146 BUG_ON(end < start);
147 state->state |= bits;
148 state->start = start;
149 state->end = end;
150 update_extent_state(state);
151 ret = insert_existing_cache_extent(&tree->state, &state->cache_node);
152 BUG_ON(ret);
153 merge_state(tree, state);
154 return 0;
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)
165 int ret;
166 prealloc->start = orig->start;
167 prealloc->end = split - 1;
168 prealloc->state = orig->state;
169 update_extent_state(prealloc);
170 orig->start = split;
171 update_extent_state(orig);
172 ret = insert_existing_cache_extent(&tree->state,
173 &prealloc->cache_node);
174 BUG_ON(ret);
175 return 0;
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);
190 } else {
191 merge_state(tree, state);
193 return ret;
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;
205 int err;
206 int set = 0;
208 again:
209 prealloc = alloc_extent_state();
210 if (!prealloc)
211 return -ENOMEM;
214 * this search will find the extents that end after
215 * our range starts
217 node = find_first_cache_extent(&tree->state, start);
218 if (!node)
219 goto out;
220 state = container_of(node, struct extent_state, cache_node);
221 if (state->start > end)
222 goto out;
225 * | ---- desired range ---- |
226 * | state | or
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);
242 prealloc = NULL;
243 if (err)
244 goto out;
245 if (state->end <= end) {
246 start = state->end + 1;
247 set |= clear_state_bit(tree, state, bits);
248 } else {
249 start = state->start;
251 goto search_again;
254 * | ---- desired range ---- |
255 * | state |
256 * We need to split the extent, and clear the bit
257 * on the first half
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);
264 prealloc = NULL;
265 goto out;
268 start = state->end + 1;
269 set |= clear_state_bit(tree, state, bits);
270 goto search_again;
271 out:
272 if (prealloc)
273 free_extent_state(prealloc);
274 return set;
276 search_again:
277 if (start > end)
278 goto out;
279 goto again;
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;
291 int err = 0;
292 int set;
293 u64 last_start;
294 u64 last_end;
295 again:
296 prealloc = alloc_extent_state();
297 if (!prealloc)
298 return -ENOMEM;
301 * this search will find the extents that end after
302 * our range starts
304 node = find_first_cache_extent(&tree->state, start);
305 if (!node) {
306 err = insert_state(tree, prealloc, start, end, bits);
307 BUG_ON(err == -EEXIST);
308 prealloc = NULL;
309 goto out;
312 state = container_of(node, struct extent_state, cache_node);
313 last_start = state->start;
314 last_end = state->end;
317 * | ---- desired range ---- |
318 * | state |
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);
327 goto search_again;
330 * | ---- desired range ---- |
331 * | state |
332 * or
333 * | ------------- state -------------- |
335 * We need to split the extent we found, and may flip bits on
336 * second half.
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
343 * desired bit on it.
345 if (state->start < start) {
346 set = state->state & bits;
347 err = split_state(tree, state, prealloc, start);
348 BUG_ON(err == -EEXIST);
349 prealloc = NULL;
350 if (err)
351 goto out;
352 if (state->end <= end) {
353 state->state |= bits;
354 start = state->end + 1;
355 merge_state(tree, state);
356 } else {
357 start = state->start;
359 goto search_again;
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) {
369 u64 this_end;
370 if (end < last_start)
371 this_end = end;
372 else
373 this_end = last_start -1;
374 err = insert_state(tree, prealloc, start, this_end,
375 bits);
376 BUG_ON(err == -EEXIST);
377 prealloc = NULL;
378 if (err)
379 goto out;
380 start = this_end + 1;
381 goto search_again;
384 * | ---- desired range ---- |
385 * | ---------- state ---------- |
386 * We need to split the extent, and set the bit
387 * on the first half
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);
395 prealloc = NULL;
396 out:
397 if (prealloc)
398 free_extent_state(prealloc);
399 return err;
400 search_again:
401 if (start > end)
402 goto out;
403 goto again;
406 int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end,
407 gfp_t mask)
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,
413 gfp_t mask)
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;
423 int ret = 1;
426 * this search will find all the extents that end after
427 * our range starts.
429 node = find_first_cache_extent(&tree->state, start);
430 if (!node)
431 goto out;
433 while(1) {
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;
438 ret = 0;
439 break;
441 node = next_cache_extent(node);
442 if (!node)
443 break;
445 out:
446 return ret;
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;
454 int bitset = 0;
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) {
461 bitset = 0;
462 break;
464 if (state->start > end)
465 break;
466 if (state->state & bits) {
467 bitset = 1;
468 if (!filled)
469 break;
470 } else if (filled) {
471 bitset = 0;
472 break;
474 start = state->end + 1;
475 if (start > end)
476 break;
477 node = next_cache_extent(node);
478 if (!node) {
479 if (filled)
480 bitset = 0;
481 break;
484 return bitset;
487 int set_state_private(struct extent_io_tree *tree, u64 start, u64 private)
489 struct cache_extent *node;
490 struct extent_state *state;
491 int ret = 0;
493 node = find_first_cache_extent(&tree->state, start);
494 if (!node) {
495 ret = -ENOENT;
496 goto out;
498 state = container_of(node, struct extent_state, cache_node);
499 if (state->start != start) {
500 ret = -ENOENT;
501 goto out;
503 state->private = private;
504 out:
505 return ret;
508 int get_state_private(struct extent_io_tree *tree, u64 start, u64 *private)
510 struct cache_extent *node;
511 struct extent_state *state;
512 int ret = 0;
514 node = find_first_cache_extent(&tree->state, start);
515 if (!node) {
516 ret = -ENOENT;
517 goto out;
519 state = container_of(node, struct extent_state, cache_node);
520 if (state->start != start) {
521 ret = -ENOENT;
522 goto out;
524 *private = state->private;
525 out:
526 return ret;
529 static int free_some_buffers(struct extent_io_tree *tree)
531 u32 nrscan = 0;
532 struct extent_buffer *eb;
533 struct list_head *node, *next;
535 if (tree->cache_size < cache_max)
536 return 0;
537 list_for_each_safe(node, next, &tree->lru) {
538 eb = list_entry(node, struct extent_buffer, lru);
539 if (eb->refs == 1) {
540 free_extent_buffer(eb);
541 if (tree->cache_size < cache_max)
542 break;
543 } else {
544 list_move_tail(&eb->lru, &tree->lru);
546 if (nrscan++ > 64)
547 break;
549 return 0;
552 static struct extent_buffer *__alloc_extent_buffer(struct extent_io_tree *tree,
553 u64 bytenr, u32 blocksize)
555 struct extent_buffer *eb;
556 int ret;
558 eb = malloc(sizeof(struct extent_buffer) + blocksize);
559 if (!eb) {
560 BUG();
561 return NULL;
564 eb->start = bytenr;
565 eb->len = blocksize;
566 eb->refs = 2;
567 eb->flags = 0;
568 eb->tree = tree;
569 eb->fd = -1;
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);
576 if (ret) {
577 free(eb);
578 return NULL;
580 list_add_tail(&eb->lru, &tree->lru);
581 tree->cache_size += blocksize;
582 return eb;
585 void free_extent_buffer(struct extent_buffer *eb)
587 if (!eb)
588 return;
590 eb->refs--;
591 BUG_ON(eb->refs < 0);
592 if (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;
599 free(eb);
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);
613 eb->refs++;
615 return eb;
618 struct extent_buffer *find_first_extent_buffer(struct extent_io_tree *tree,
619 u64 start)
621 struct extent_buffer *eb = NULL;
622 struct cache_extent *cache;
624 cache = find_first_cache_extent(&tree->cache, start);
625 if (cache) {
626 eb = container_of(cache, struct extent_buffer, cache_node);
627 list_move_tail(&eb->lru, &tree->lru);
628 eb->refs++;
630 return eb;
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);
643 eb->refs++;
644 } else {
645 if (cache) {
646 eb = container_of(cache, struct extent_buffer,
647 cache_node);
648 BUG_ON(eb->refs != 1);
649 free_extent_buffer(eb);
651 eb = __alloc_extent_buffer(tree, bytenr, blocksize);
653 return eb;
656 int read_extent_from_disk(struct extent_buffer *eb)
658 int ret;
659 ret = pread(eb->fd, eb->data, eb->len, eb->dev_bytenr);
660 if (ret < 0)
661 goto out;
662 if (ret != eb->len) {
663 ret = -EIO;
664 goto out;
666 ret = 0;
667 out:
668 return ret;
671 int write_extent_to_disk(struct extent_buffer *eb)
673 int ret;
674 ret = pwrite(eb->fd, eb->data, eb->len, eb->dev_bytenr);
675 if (ret < 0)
676 goto out;
677 if (ret != eb->len) {
678 ret = -EIO;
679 goto out;
681 ret = 0;
682 out:
683 return ret;
686 int set_extent_buffer_uptodate(struct extent_buffer *eb)
688 eb->flags |= EXTENT_UPTODATE;
689 return 0;
692 int clear_extent_buffer_uptodate(struct extent_io_tree *tree,
693 struct extent_buffer *eb)
695 eb->flags &= ~EXTENT_UPTODATE;
696 return 0;
699 int extent_buffer_uptodate(struct extent_buffer *eb)
701 if (eb->flags & EXTENT_UPTODATE)
702 return 1;
703 return 0;
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);
714 return 0;
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);
725 return 0;
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,
748 unsigned long len)
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);