Update find_free_extent for the ext3 conversion tool
[btrfs-progs-unstable.git] / extent_map.c
blob8a6b071d30d4a41354469eeeb3014e1d591e811f
1 /*
2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
18 #define _XOPEN_SOURCE 600
19 #define __USE_XOPEN2K
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include "kerncompat.h"
27 #include "extent_map.h"
28 #include "list.h"
30 u64 cache_max = 1024 * 1024 * 32;
32 void extent_map_tree_init(struct extent_map_tree *tree)
34 cache_tree_init(&tree->state);
35 cache_tree_init(&tree->cache);
36 INIT_LIST_HEAD(&tree->lru);
37 tree->cache_size = 0;
40 static struct extent_state *alloc_extent_state(void)
42 struct extent_state *state;
44 state = malloc(sizeof(*state));
45 if (!state)
46 return NULL;
47 state->refs = 1;
48 state->state = 0;
49 state->private = 0;
50 return state;
53 static void free_extent_state(struct extent_state *state)
55 state->refs--;
56 BUG_ON(state->refs < 0);
57 if (state->refs == 0)
58 free(state);
61 void extent_map_tree_cleanup(struct extent_map_tree *tree)
63 struct extent_state *es;
64 struct extent_buffer *eb;
65 struct cache_extent *cache;
67 while(!list_empty(&tree->lru)) {
68 eb = list_entry(tree->lru.next, struct extent_buffer, lru);
69 if (eb->refs != 1) {
70 fprintf(stderr, "extent buffer leak: "
71 "start %Lu len %u\n", eb->start, eb->len);
72 eb->refs = 1;
74 free_extent_buffer(eb);
76 while (1) {
77 cache = find_first_cache_extent(&tree->state, 0);
78 if (!cache)
79 break;
80 es = container_of(cache, struct extent_state, cache_node);
81 remove_cache_extent(&tree->state, &es->cache_node);
82 free_extent_state(es);
86 static inline void update_extent_state(struct extent_state *state)
88 state->cache_node.start = state->start;
89 state->cache_node.size = state->end + 1 - state->start;
93 * Utility function to look for merge candidates inside a given range.
94 * Any extents with matching state are merged together into a single
95 * extent in the tree. Extents with EXTENT_IO in their state field are
96 * not merged
98 static int merge_state(struct extent_map_tree *tree,
99 struct extent_state *state)
101 struct extent_state *other;
102 struct cache_extent *other_node;
104 if (state->state & EXTENT_IOBITS)
105 return 0;
107 other_node = prev_cache_extent(&state->cache_node);
108 if (other_node) {
109 other = container_of(other_node, struct extent_state,
110 cache_node);
111 if (other->end == state->start - 1 &&
112 other->state == state->state) {
113 state->start = other->start;
114 update_extent_state(state);
115 remove_cache_extent(&tree->state, &other->cache_node);
116 free_extent_state(other);
119 other_node = next_cache_extent(&state->cache_node);
120 if (other_node) {
121 other = container_of(other_node, struct extent_state,
122 cache_node);
123 if (other->start == state->end + 1 &&
124 other->state == state->state) {
125 other->start = state->start;
126 update_extent_state(state);
127 remove_cache_extent(&tree->state, &state->cache_node);
128 free_extent_state(state);
131 return 0;
135 * insert an extent_state struct into the tree. 'bits' are set on the
136 * struct before it is inserted.
138 static int insert_state(struct extent_map_tree *tree,
139 struct extent_state *state, u64 start, u64 end,
140 int bits)
142 int ret;
144 BUG_ON(end < start);
145 state->state |= bits;
146 state->start = start;
147 state->end = end;
148 update_extent_state(state);
149 ret = insert_existing_cache_extent(&tree->state, &state->cache_node);
150 BUG_ON(ret);
151 merge_state(tree, state);
152 return 0;
156 * split a given extent state struct in two, inserting the preallocated
157 * struct 'prealloc' as the newly created second half. 'split' indicates an
158 * offset inside 'orig' where it should be split.
160 static int split_state(struct extent_map_tree *tree, struct extent_state *orig,
161 struct extent_state *prealloc, u64 split)
163 int ret;
164 prealloc->start = orig->start;
165 prealloc->end = split - 1;
166 prealloc->state = orig->state;
167 update_extent_state(prealloc);
168 orig->start = split;
169 update_extent_state(orig);
170 ret = insert_existing_cache_extent(&tree->state,
171 &prealloc->cache_node);
172 BUG_ON(ret);
173 return 0;
177 * clear some bits on a range in the tree.
179 static int clear_state_bit(struct extent_map_tree *tree,
180 struct extent_state *state, int bits)
182 int ret = state->state & bits;
184 state->state &= ~bits;
185 if (state->state == 0) {
186 remove_cache_extent(&tree->state, &state->cache_node);
187 free_extent_state(state);
188 } else {
189 merge_state(tree, state);
191 return ret;
195 * set some bits on a range in the tree.
197 int clear_extent_bits(struct extent_map_tree *tree, u64 start,
198 u64 end, int bits, gfp_t mask)
200 struct extent_state *state;
201 struct extent_state *prealloc = NULL;
202 struct cache_extent *node;
203 int err;
204 int set = 0;
206 again:
207 prealloc = alloc_extent_state();
208 if (!prealloc)
209 return -ENOMEM;
212 * this search will find the extents that end after
213 * our range starts
215 node = find_first_cache_extent(&tree->state, start);
216 if (!node)
217 goto out;
218 state = container_of(node, struct extent_state, cache_node);
219 if (state->start > end)
220 goto out;
223 * | ---- desired range ---- |
224 * | state | or
225 * | ------------- state -------------- |
227 * We need to split the extent we found, and may flip
228 * bits on second half.
230 * If the extent we found extends past our range, we
231 * just split and search again. It'll get split again
232 * the next time though.
234 * If the extent we found is inside our range, we clear
235 * the desired bit on it.
237 if (state->start < start) {
238 err = split_state(tree, state, prealloc, start);
239 BUG_ON(err == -EEXIST);
240 prealloc = NULL;
241 if (err)
242 goto out;
243 if (state->end <= end) {
244 start = state->end + 1;
245 set |= clear_state_bit(tree, state, bits);
246 } else {
247 start = state->start;
249 goto search_again;
252 * | ---- desired range ---- |
253 * | state |
254 * We need to split the extent, and clear the bit
255 * on the first half
257 if (state->start <= end && state->end > end) {
258 err = split_state(tree, state, prealloc, end + 1);
259 BUG_ON(err == -EEXIST);
261 set |= clear_state_bit(tree, prealloc, bits);
262 prealloc = NULL;
263 goto out;
266 start = state->end + 1;
267 set |= clear_state_bit(tree, state, bits);
268 goto search_again;
269 out:
270 if (prealloc)
271 free_extent_state(prealloc);
272 return set;
274 search_again:
275 if (start > end)
276 goto out;
277 goto again;
281 * set some bits on a range in the tree.
283 int set_extent_bits(struct extent_map_tree *tree, u64 start,
284 u64 end, int bits, gfp_t mask)
286 struct extent_state *state;
287 struct extent_state *prealloc = NULL;
288 struct cache_extent *node;
289 int err = 0;
290 int set;
291 u64 last_start;
292 u64 last_end;
293 again:
294 prealloc = alloc_extent_state();
295 if (!prealloc)
296 return -ENOMEM;
299 * this search will find the extents that end after
300 * our range starts
302 node = find_first_cache_extent(&tree->state, start);
303 if (!node) {
304 err = insert_state(tree, prealloc, start, end, bits);
305 BUG_ON(err == -EEXIST);
306 prealloc = NULL;
307 goto out;
310 state = container_of(node, struct extent_state, cache_node);
311 last_start = state->start;
312 last_end = state->end;
315 * | ---- desired range ---- |
316 * | state |
318 * Just lock what we found and keep going
320 if (state->start == start && state->end <= end) {
321 set = state->state & bits;
322 state->state |= bits;
323 start = state->end + 1;
324 merge_state(tree, state);
325 goto search_again;
328 * | ---- desired range ---- |
329 * | state |
330 * or
331 * | ------------- state -------------- |
333 * We need to split the extent we found, and may flip bits on
334 * second half.
336 * If the extent we found extends past our
337 * range, we just split and search again. It'll get split
338 * again the next time though.
340 * If the extent we found is inside our range, we set the
341 * desired bit on it.
343 if (state->start < start) {
344 set = state->state & bits;
345 err = split_state(tree, state, prealloc, start);
346 BUG_ON(err == -EEXIST);
347 prealloc = NULL;
348 if (err)
349 goto out;
350 if (state->end <= end) {
351 state->state |= bits;
352 start = state->end + 1;
353 merge_state(tree, state);
354 } else {
355 start = state->start;
357 goto search_again;
360 * | ---- desired range ---- |
361 * | state | or | state |
363 * There's a hole, we need to insert something in it and
364 * ignore the extent we found.
366 if (state->start > start) {
367 u64 this_end;
368 if (end < last_start)
369 this_end = end;
370 else
371 this_end = last_start -1;
372 err = insert_state(tree, prealloc, start, this_end,
373 bits);
374 BUG_ON(err == -EEXIST);
375 prealloc = NULL;
376 if (err)
377 goto out;
378 start = this_end + 1;
379 goto search_again;
382 * | ---- desired range ---- |
383 * | ---------- state ---------- |
384 * We need to split the extent, and set the bit
385 * on the first half
387 set = state->state & bits;
388 err = split_state(tree, state, prealloc, end + 1);
389 BUG_ON(err == -EEXIST);
391 state->state |= bits;
392 merge_state(tree, prealloc);
393 prealloc = NULL;
394 out:
395 if (prealloc)
396 free_extent_state(prealloc);
397 return err;
398 search_again:
399 if (start > end)
400 goto out;
401 goto again;
404 int set_extent_dirty(struct extent_map_tree *tree, u64 start, u64 end,
405 gfp_t mask)
407 return set_extent_bits(tree, start, end, EXTENT_DIRTY, mask);
410 int clear_extent_dirty(struct extent_map_tree *tree, u64 start, u64 end,
411 gfp_t mask)
413 return clear_extent_bits(tree, start, end, EXTENT_DIRTY, mask);
416 int find_first_extent_bit(struct extent_map_tree *tree, u64 start,
417 u64 *start_ret, u64 *end_ret, int bits)
419 struct cache_extent *node;
420 struct extent_state *state;
421 int ret = 1;
424 * this search will find all the extents that end after
425 * our range starts.
427 node = find_first_cache_extent(&tree->state, start);
428 if (!node)
429 goto out;
431 while(1) {
432 state = container_of(node, struct extent_state, cache_node);
433 if (state->end >= start && (state->state & bits)) {
434 *start_ret = state->start;
435 *end_ret = state->end;
436 ret = 0;
437 break;
439 node = next_cache_extent(node);
440 if (!node)
441 break;
443 out:
444 return ret;
447 int test_range_bit(struct extent_map_tree *tree, u64 start, u64 end,
448 int bits, int filled)
450 struct extent_state *state = NULL;
451 struct cache_extent *node;
452 int bitset = 0;
454 node = find_first_cache_extent(&tree->state, start);
455 while (node && start <= end) {
456 state = container_of(node, struct extent_state, cache_node);
458 if (filled && state->start > start) {
459 bitset = 0;
460 break;
462 if (state->start > end)
463 break;
464 if (state->state & bits) {
465 bitset = 1;
466 if (!filled)
467 break;
468 } else if (filled) {
469 bitset = 0;
470 break;
472 start = state->end + 1;
473 if (start > end)
474 break;
475 node = next_cache_extent(node);
477 return bitset;
480 int set_state_private(struct extent_map_tree *tree, u64 start, u64 private)
482 struct cache_extent *node;
483 struct extent_state *state;
484 int ret = 0;
486 node = find_first_cache_extent(&tree->state, start);
487 if (!node) {
488 ret = -ENOENT;
489 goto out;
491 state = container_of(node, struct extent_state, cache_node);
492 if (state->start != start) {
493 ret = -ENOENT;
494 goto out;
496 state->private = private;
497 out:
498 return ret;
501 int get_state_private(struct extent_map_tree *tree, u64 start, u64 *private)
503 struct cache_extent *node;
504 struct extent_state *state;
505 int ret = 0;
507 node = find_first_cache_extent(&tree->state, start);
508 if (!node) {
509 ret = -ENOENT;
510 goto out;
512 state = container_of(node, struct extent_state, cache_node);
513 if (state->start != start) {
514 ret = -ENOENT;
515 goto out;
517 *private = state->private;
518 out:
519 return ret;
522 static int free_some_buffers(struct extent_map_tree *tree)
524 u32 nrscan = 0;
525 struct extent_buffer *eb;
526 struct list_head *node, *next;
528 if (tree->cache_size < cache_max)
529 return 0;
530 list_for_each_safe(node, next, &tree->lru) {
531 eb = list_entry(node, struct extent_buffer, lru);
532 if (eb->refs == 1) {
533 free_extent_buffer(eb);
534 if (tree->cache_size < cache_max)
535 break;
537 if (nrscan++ > 64)
538 break;
540 return 0;
543 static struct extent_buffer *__alloc_extent_buffer(struct extent_map_tree *tree,
544 u64 bytenr, u32 blocksize)
546 struct extent_buffer *eb;
547 int ret;
549 eb = malloc(sizeof(struct extent_buffer) + blocksize);
550 if (!eb)
551 return NULL;
553 eb->start = bytenr;
554 eb->len = blocksize;
555 eb->refs = 2;
556 eb->flags = 0;
557 eb->tree = tree;
558 eb->fd = -1;
559 eb->dev_bytenr = (u64)-1;
560 eb->cache_node.start = bytenr;
561 eb->cache_node.size = blocksize;
563 free_some_buffers(tree);
564 ret = insert_existing_cache_extent(&tree->cache, &eb->cache_node);
565 if (ret) {
566 free(eb);
567 return NULL;
569 list_add_tail(&eb->lru, &tree->lru);
570 tree->cache_size += blocksize;
571 return eb;
574 void free_extent_buffer(struct extent_buffer *eb)
576 if (!eb)
577 return;
579 eb->refs--;
580 BUG_ON(eb->refs < 0);
581 if (eb->refs == 0) {
582 struct extent_map_tree *tree = eb->tree;
583 BUG_ON(eb->flags & EXTENT_DIRTY);
584 list_del_init(&eb->lru);
585 remove_cache_extent(&tree->cache, &eb->cache_node);
586 BUG_ON(tree->cache_size < eb->len);
587 tree->cache_size -= eb->len;
588 free(eb);
592 struct extent_buffer *find_extent_buffer(struct extent_map_tree *tree,
593 u64 bytenr, u32 blocksize)
595 struct extent_buffer *eb = NULL;
596 struct cache_extent *cache;
598 cache = find_cache_extent(&tree->cache, bytenr, blocksize);
599 if (cache && cache->start == bytenr && cache->size == blocksize) {
600 eb = container_of(cache, struct extent_buffer, cache_node);
601 list_move_tail(&eb->lru, &tree->lru);
602 eb->refs++;
604 return eb;
607 struct extent_buffer *find_first_extent_buffer(struct extent_map_tree *tree,
608 u64 start)
610 struct extent_buffer *eb = NULL;
611 struct cache_extent *cache;
613 cache = find_first_cache_extent(&tree->cache, start);
614 if (cache) {
615 eb = container_of(cache, struct extent_buffer, cache_node);
616 list_move_tail(&eb->lru, &tree->lru);
617 eb->refs++;
619 return eb;
622 struct extent_buffer *alloc_extent_buffer(struct extent_map_tree *tree,
623 u64 bytenr, u32 blocksize)
625 struct extent_buffer *eb;
626 struct cache_extent *cache;
628 cache = find_cache_extent(&tree->cache, bytenr, blocksize);
629 if (cache && cache->start == bytenr && cache->size == blocksize) {
630 eb = container_of(cache, struct extent_buffer, cache_node);
631 list_move_tail(&eb->lru, &tree->lru);
632 eb->refs++;
633 } else {
634 if (cache) {
635 eb = container_of(cache, struct extent_buffer,
636 cache_node);
637 BUG_ON(eb->refs != 1);
638 free_extent_buffer(eb);
640 eb = __alloc_extent_buffer(tree, bytenr, blocksize);
642 return eb;
645 int read_extent_from_disk(struct extent_buffer *eb)
647 int ret;
648 ret = pread(eb->fd, eb->data, eb->len, eb->dev_bytenr);
649 if (ret < 0)
650 goto out;
651 if (ret != eb->len) {
652 ret = -EIO;
653 goto out;
655 ret = 0;
656 out:
657 return ret;
660 int write_extent_to_disk(struct extent_buffer *eb)
662 int ret;
663 ret = pwrite(eb->fd, eb->data, eb->len, eb->dev_bytenr);
664 if (ret < 0)
665 goto out;
666 if (ret != eb->len) {
667 ret = -EIO;
668 goto out;
670 ret = 0;
671 out:
672 return ret;
675 int set_extent_buffer_uptodate(struct extent_buffer *eb)
677 eb->flags |= EXTENT_UPTODATE;
678 return 0;
681 int extent_buffer_uptodate(struct extent_buffer *eb)
683 if (eb->flags & EXTENT_UPTODATE)
684 return 1;
685 return 0;
688 int set_extent_buffer_dirty(struct extent_buffer *eb)
690 struct extent_map_tree *tree = eb->tree;
691 if (!(eb->flags & EXTENT_DIRTY)) {
692 eb->flags |= EXTENT_DIRTY;
693 set_extent_dirty(tree, eb->start, eb->start + eb->len - 1, 0);
694 extent_buffer_get(eb);
696 return 0;
699 int clear_extent_buffer_dirty(struct extent_buffer *eb)
701 struct extent_map_tree *tree = eb->tree;
702 if (eb->flags & EXTENT_DIRTY) {
703 eb->flags &= ~EXTENT_DIRTY;
704 clear_extent_dirty(tree, eb->start, eb->start + eb->len - 1, 0);
705 free_extent_buffer(eb);
707 return 0;
710 int memcmp_extent_buffer(struct extent_buffer *eb, const void *ptrv,
711 unsigned long start, unsigned long len)
713 return memcmp(eb->data + start, ptrv, len);
716 void read_extent_buffer(struct extent_buffer *eb, void *dst,
717 unsigned long start, unsigned long len)
719 memcpy(dst, eb->data + start, len);
722 void write_extent_buffer(struct extent_buffer *eb, const void *src,
723 unsigned long start, unsigned long len)
725 memcpy(eb->data + start, src, len);
728 void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
729 unsigned long dst_offset, unsigned long src_offset,
730 unsigned long len)
732 memcpy(dst->data + dst_offset, src->data + src_offset, len);
735 void memcpy_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
736 unsigned long src_offset, unsigned long len)
738 memcpy(dst->data + dst_offset, dst->data + src_offset, len);
741 void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
742 unsigned long src_offset, unsigned long len)
744 memmove(dst->data + dst_offset, dst->data + src_offset, len);
747 void memset_extent_buffer(struct extent_buffer *eb, char c,
748 unsigned long start, unsigned long len)
750 memset(eb->data + start, c, len);