1 #include <linux/module.h>
4 #include "print-tree.h"
5 #include "transaction.h"
7 static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
8 *orig_root, u64 num_blocks, u64 search_start, u64
9 search_end, struct btrfs_key *ins);
10 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
11 btrfs_root *extent_root);
12 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
13 btrfs_root *extent_root);
15 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
16 struct btrfs_block_group_cache
19 struct btrfs_block_group_cache *cache[8];
20 struct btrfs_block_group_cache *found_group = NULL;
21 struct btrfs_fs_info *info = root->fs_info;
29 used = btrfs_block_group_used(&hint->item);
30 if (used < (hint->key.offset * 2) / 3) {
33 radix_tree_tag_clear(&info->block_group_radix,
34 hint->key.objectid + hint->key.offset - 1,
35 BTRFS_BLOCK_GROUP_AVAIL);
36 last = hint->key.objectid + hint->key.offset;
43 ret = radix_tree_gang_lookup_tag(&info->block_group_radix,
45 last, ARRAY_SIZE(cache),
46 BTRFS_BLOCK_GROUP_AVAIL);
49 for (i = 0; i < ret; i++) {
50 used = btrfs_block_group_used(&cache[i]->item);
51 if (used < (cache[i]->key.offset * 2) / 3) {
52 info->block_group_cache = cache[i];
53 found_group = cache[i];
56 radix_tree_tag_clear(&info->block_group_radix,
57 cache[i]->key.objectid +
58 cache[i]->key.offset - 1,
59 BTRFS_BLOCK_GROUP_AVAIL);
60 last = cache[i]->key.objectid +
67 ret = radix_tree_gang_lookup(&info->block_group_radix,
69 last, ARRAY_SIZE(cache));
72 for (i = 0; i < ret; i++) {
73 used = btrfs_block_group_used(&cache[i]->item);
74 if (used < cache[i]->key.offset) {
75 info->block_group_cache = cache[i];
76 found_group = cache[i];
79 radix_tree_tag_clear(&info->block_group_radix,
80 cache[i]->key.objectid +
81 cache[i]->key.offset - 1,
82 BTRFS_BLOCK_GROUP_AVAIL);
83 last = cache[i]->key.objectid +
87 info->block_group_cache = NULL;
95 ret = radix_tree_gang_lookup(&info->block_group_radix,
96 (void **)&found_group, 0, 1);
102 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
103 struct btrfs_root *root,
104 u64 blocknr, u64 num_blocks)
106 struct btrfs_path *path;
108 struct btrfs_key key;
109 struct btrfs_leaf *l;
110 struct btrfs_extent_item *item;
111 struct btrfs_key ins;
114 find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1,
116 path = btrfs_alloc_path();
118 btrfs_init_path(path);
119 key.objectid = blocknr;
121 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
122 key.offset = num_blocks;
123 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
126 printk("can't find block %Lu %Lu\n", blocknr, num_blocks);
130 l = btrfs_buffer_leaf(path->nodes[0]);
131 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
132 refs = btrfs_extent_refs(item);
133 btrfs_set_extent_refs(item, refs + 1);
134 btrfs_mark_buffer_dirty(path->nodes[0]);
136 btrfs_release_path(root->fs_info->extent_root, path);
137 btrfs_free_path(path);
138 finish_current_insert(trans, root->fs_info->extent_root);
139 del_pending_extents(trans, root->fs_info->extent_root);
143 static int lookup_extent_ref(struct btrfs_trans_handle *trans,
144 struct btrfs_root *root, u64 blocknr,
145 u64 num_blocks, u32 *refs)
147 struct btrfs_path *path;
149 struct btrfs_key key;
150 struct btrfs_leaf *l;
151 struct btrfs_extent_item *item;
153 path = btrfs_alloc_path();
154 btrfs_init_path(path);
155 key.objectid = blocknr;
156 key.offset = num_blocks;
158 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
159 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
163 l = btrfs_buffer_leaf(path->nodes[0]);
164 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
165 *refs = btrfs_extent_refs(item);
166 btrfs_release_path(root->fs_info->extent_root, path);
167 btrfs_free_path(path);
171 int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
172 struct btrfs_root *root)
174 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
177 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
178 struct buffer_head *buf)
181 struct btrfs_node *buf_node;
182 struct btrfs_leaf *buf_leaf;
183 struct btrfs_disk_key *key;
184 struct btrfs_file_extent_item *fi;
191 buf_node = btrfs_buffer_node(buf);
192 leaf = btrfs_is_leaf(buf_node);
193 buf_leaf = btrfs_buffer_leaf(buf);
194 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
196 key = &buf_leaf->items[i].key;
197 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
199 fi = btrfs_item_ptr(buf_leaf, i,
200 struct btrfs_file_extent_item);
201 if (btrfs_file_extent_type(fi) ==
202 BTRFS_FILE_EXTENT_INLINE)
204 ret = btrfs_inc_extent_ref(trans, root,
205 btrfs_file_extent_disk_blocknr(fi),
206 btrfs_file_extent_disk_num_blocks(fi));
209 blocknr = btrfs_node_blockptr(buf_node, i);
210 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
217 static int write_one_cache_group(struct btrfs_trans_handle *trans,
218 struct btrfs_root *root,
219 struct btrfs_path *path,
220 struct btrfs_block_group_cache *cache)
224 struct btrfs_root *extent_root = root->fs_info->extent_root;
225 struct btrfs_block_group_item *bi;
226 struct btrfs_key ins;
228 find_free_extent(trans, extent_root, 0, 0, (u64)-1, &ins);
229 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
231 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
232 struct btrfs_block_group_item);
233 memcpy(bi, &cache->item, sizeof(*bi));
234 mark_buffer_dirty(path->nodes[0]);
235 btrfs_release_path(extent_root, path);
237 finish_current_insert(trans, extent_root);
238 pending_ret = del_pending_extents(trans, extent_root);
247 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
248 struct btrfs_root *root)
250 struct btrfs_block_group_cache *cache[8];
254 struct radix_tree_root *radix = &root->fs_info->block_group_radix;
256 struct btrfs_path *path;
258 path = btrfs_alloc_path();
263 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
264 0, ARRAY_SIZE(cache),
265 BTRFS_BLOCK_GROUP_DIRTY);
268 for (i = 0; i < ret; i++) {
269 radix_tree_tag_clear(radix, cache[i]->key.objectid +
270 cache[i]->key.offset - 1,
271 BTRFS_BLOCK_GROUP_DIRTY);
272 err = write_one_cache_group(trans, root,
276 cache[i]->last_alloc = cache[i]->first_free;
279 btrfs_free_path(path);
283 static int update_block_group(struct btrfs_trans_handle *trans,
284 struct btrfs_root *root,
285 u64 blocknr, u64 num, int alloc)
287 struct btrfs_block_group_cache *cache;
288 struct btrfs_fs_info *info = root->fs_info;
294 ret = radix_tree_gang_lookup(&info->block_group_radix,
295 (void **)&cache, blocknr, 1);
297 printk(KERN_CRIT "blocknr %Lu lookup failed\n",
301 block_in_group = blocknr - cache->key.objectid;
302 WARN_ON(block_in_group > cache->key.offset);
303 radix_tree_tag_set(&info->block_group_radix,
304 cache->key.objectid + cache->key.offset - 1,
305 BTRFS_BLOCK_GROUP_DIRTY);
307 old_val = btrfs_block_group_used(&cache->item);
308 num = min(total, cache->key.offset - block_in_group);
313 if (blocknr > cache->last_alloc)
314 cache->last_alloc = blocknr;
317 if (blocknr < cache->first_free)
318 cache->first_free = blocknr;
320 btrfs_set_block_group_used(&cache->item, old_val);
325 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
328 unsigned long gang[8];
332 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
335 ret = find_first_radix_bit(pinned_radix, gang,
341 for (i = 0; i < ret; i++) {
342 clear_radix_bit(pinned_radix, gang[i]);
348 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
349 btrfs_root *extent_root)
351 struct btrfs_key ins;
352 struct btrfs_extent_item extent_item;
355 u64 super_blocks_used;
356 struct btrfs_fs_info *info = extent_root->fs_info;
358 btrfs_set_extent_refs(&extent_item, 1);
361 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
362 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
364 for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
365 ins.objectid = extent_root->fs_info->extent_tree_insert[i];
366 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
367 btrfs_set_super_blocks_used(info->disk_super,
368 super_blocks_used + 1);
369 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
370 sizeof(extent_item));
373 extent_root->fs_info->extent_tree_insert_nr = 0;
374 extent_root->fs_info->extent_tree_prealloc_nr = 0;
378 static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
381 struct btrfs_header *header;
382 struct buffer_head *bh;
385 bh = btrfs_find_tree_block(root, blocknr);
387 if (buffer_uptodate(bh)) {
389 root->fs_info->running_transaction->transid;
390 header = btrfs_buffer_header(bh);
391 if (btrfs_header_generation(header) ==
393 btrfs_block_release(root, bh);
397 btrfs_block_release(root, bh);
399 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
401 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
408 * remove an extent from the root, returns 0 on success
410 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
411 *root, u64 blocknr, u64 num_blocks, int pin)
413 struct btrfs_path *path;
414 struct btrfs_key key;
415 struct btrfs_fs_info *info = root->fs_info;
416 struct btrfs_root *extent_root = info->extent_root;
418 struct btrfs_extent_item *ei;
419 struct btrfs_key ins;
422 key.objectid = blocknr;
424 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
425 key.offset = num_blocks;
427 find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
428 path = btrfs_alloc_path();
430 btrfs_init_path(path);
432 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
434 printk("failed to find %Lu\n", key.objectid);
435 btrfs_print_tree(extent_root, extent_root->node);
436 printk("failed to find %Lu\n", key.objectid);
439 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
440 struct btrfs_extent_item);
441 BUG_ON(ei->refs == 0);
442 refs = btrfs_extent_refs(ei) - 1;
443 btrfs_set_extent_refs(ei, refs);
444 btrfs_mark_buffer_dirty(path->nodes[0]);
446 u64 super_blocks_used;
449 ret = pin_down_block(root, blocknr, 0);
453 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
454 btrfs_set_super_blocks_used(info->disk_super,
455 super_blocks_used - num_blocks);
456 ret = btrfs_del_item(trans, extent_root, path);
459 ret = update_block_group(trans, root, blocknr, num_blocks, 0);
462 btrfs_release_path(extent_root, path);
463 btrfs_free_path(path);
464 finish_current_insert(trans, extent_root);
469 * find all the blocks marked as pending in the radix tree and remove
470 * them from the extent map
472 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
473 btrfs_root *extent_root)
478 unsigned long gang[4];
480 struct radix_tree_root *pending_radix;
481 struct radix_tree_root *pinned_radix;
483 pending_radix = &extent_root->fs_info->pending_del_radix;
484 pinned_radix = &extent_root->fs_info->pinned_radix;
487 ret = find_first_radix_bit(pending_radix, gang,
491 for (i = 0; i < ret; i++) {
492 wret = set_radix_bit(pinned_radix, gang[i]);
494 wret = clear_radix_bit(pending_radix, gang[i]);
496 wret = __free_extent(trans, extent_root,
506 * remove an extent from the root, returns 0 on success
508 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
509 *root, u64 blocknr, u64 num_blocks, int pin)
511 struct btrfs_root *extent_root = root->fs_info->extent_root;
515 if (root == extent_root) {
516 pin_down_block(root, blocknr, 1);
519 ret = __free_extent(trans, root, blocknr, num_blocks, pin);
520 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
521 return ret ? ret : pending_ret;
525 * walks the btree of allocated extents and find a hole of a given size.
526 * The key ins is changed to record the hole:
527 * ins->objectid == block start
528 * ins->flags = BTRFS_EXTENT_ITEM_KEY
529 * ins->offset == number of blocks
530 * Any available blocks before search_start are skipped.
532 static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
533 *orig_root, u64 num_blocks, u64 search_start, u64
534 search_end, struct btrfs_key *ins)
536 struct btrfs_path *path;
537 struct btrfs_key key;
544 struct btrfs_leaf *l;
545 struct btrfs_root * root = orig_root->fs_info->extent_root;
546 struct btrfs_fs_info *info = root->fs_info;
547 int total_needed = num_blocks;
549 int fill_prealloc = 0;
551 int update_block_group = 0;
552 struct btrfs_block_group_cache *hint_block_group;
554 path = btrfs_alloc_path();
556 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
558 level = btrfs_header_level(btrfs_buffer_header(root->node));
559 /* find search start here */
560 if (0 && search_start && num_blocks) {
562 ret = radix_tree_gang_lookup(&info->block_group_radix,
563 (void **)&hint_block_group,
566 used = btrfs_block_group_used(&hint_block_group->item);
567 if (used > (hint_block_group->key.offset * 9) / 10)
569 else if (search_start < hint_block_group->last_alloc)
570 search_start = hint_block_group->last_alloc;
575 if (num_blocks == 0) {
578 total_needed = (min(level + 1, BTRFS_MAX_LEVEL) + 2) * 3;
580 if (1 || !search_start) {
581 trans->block_group = btrfs_find_block_group(root,
584 if (trans->block_group->last_alloc > search_start)
585 search_start = trans->block_group->last_alloc;
586 update_block_group = 1;
589 btrfs_init_path(path);
590 ins->objectid = search_start;
593 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
597 if (path->slots[0] > 0)
601 l = btrfs_buffer_leaf(path->nodes[0]);
602 slot = path->slots[0];
603 if (slot >= btrfs_header_nritems(&l->header)) {
605 info->extent_tree_prealloc_nr = 0;
608 ret = btrfs_next_leaf(root, path);
614 ins->objectid = search_start;
615 ins->offset = (u64)-1 - search_start;
619 ins->objectid = last_block > search_start ?
620 last_block : search_start;
621 ins->offset = (u64)-1 - ins->objectid;
624 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
625 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
627 if (key.objectid >= search_start) {
629 if (last_block < search_start)
630 last_block = search_start;
631 hole_size = key.objectid - last_block;
632 if (hole_size >= num_blocks) {
633 ins->objectid = last_block;
634 ins->offset = hole_size;
640 last_block = key.objectid + key.offset;
646 /* we have to make sure we didn't find an extent that has already
647 * been allocated by the map tree or the original allocation
649 btrfs_release_path(root, path);
650 BUG_ON(ins->objectid < search_start);
651 if (ins->objectid >= btrfs_super_total_blocks(info->disk_super)) {
652 if (search_start == 0)
657 for (test_block = ins->objectid;
658 test_block < ins->objectid + num_blocks; test_block++) {
659 if (test_radix_bit(&info->pinned_radix, test_block)) {
660 search_start = test_block + 1;
664 if (!fill_prealloc && info->extent_tree_insert_nr) {
666 info->extent_tree_insert[info->extent_tree_insert_nr - 1];
667 if (ins->objectid + num_blocks >
668 info->extent_tree_insert[0] &&
669 ins->objectid <= last) {
670 search_start = last + 1;
675 if (!fill_prealloc && info->extent_tree_prealloc_nr) {
677 info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
678 if (ins->objectid + num_blocks > first &&
679 ins->objectid <= info->extent_tree_prealloc[0]) {
680 search_start = info->extent_tree_prealloc[0] + 1;
687 test_block = ins->objectid;
688 while(test_block < ins->objectid + ins->offset &&
689 total_found < total_needed) {
690 nr = total_needed - total_found - 1;
692 info->extent_tree_prealloc[nr] = test_block;
696 if (total_found < total_needed) {
697 search_start = test_block;
700 info->extent_tree_prealloc_nr = total_found;
702 if (update_block_group) {
703 ret = radix_tree_gang_lookup(&info->block_group_radix,
704 (void **)&trans->block_group,
707 trans->block_group->last_alloc = ins->objectid;
710 ins->offset = num_blocks;
711 btrfs_free_path(path);
714 btrfs_release_path(root, path);
715 btrfs_free_path(path);
719 * finds a free extent and does all the dirty work required for allocation
720 * returns the key for the extent through ins, and a tree buffer for
721 * the first block of the extent through buf.
723 * returns 0 if everything worked, non-zero otherwise.
725 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
726 struct btrfs_root *root, u64 owner,
727 u64 num_blocks, u64 search_start,
728 u64 search_end, struct btrfs_key *ins)
732 u64 super_blocks_used;
733 struct btrfs_fs_info *info = root->fs_info;
734 struct btrfs_root *extent_root = info->extent_root;
735 struct btrfs_extent_item extent_item;
736 struct btrfs_key prealloc_key;
738 btrfs_set_extent_refs(&extent_item, 1);
739 btrfs_set_extent_owner(&extent_item, owner);
741 if (root == extent_root) {
743 BUG_ON(info->extent_tree_prealloc_nr == 0);
744 BUG_ON(num_blocks != 1);
746 info->extent_tree_prealloc_nr--;
747 nr = info->extent_tree_prealloc_nr;
748 ins->objectid = info->extent_tree_prealloc[nr];
749 info->extent_tree_insert[info->extent_tree_insert_nr++] =
751 ret = update_block_group(trans, root,
752 ins->objectid, ins->offset, 1);
756 /* do the real allocation */
757 ret = find_free_extent(trans, root, num_blocks, search_start,
762 /* then do prealloc for the extent tree */
763 ret = find_free_extent(trans, root, 0, ins->objectid + ins->offset,
764 search_end, &prealloc_key);
768 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
769 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
771 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
772 sizeof(extent_item));
774 finish_current_insert(trans, extent_root);
775 pending_ret = del_pending_extents(trans, extent_root);
780 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
785 * helper function to allocate a block for a given tree
786 * returns the tree buffer or NULL.
788 struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
789 struct btrfs_root *root, u64 hint)
791 struct btrfs_key ins;
793 struct buffer_head *buf;
795 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
796 1, hint, (unsigned long)-1, &ins);
802 buf = btrfs_find_create_tree_block(root, ins.objectid);
803 set_buffer_uptodate(buf);
804 set_buffer_checked(buf);
805 set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
809 static int drop_leaf_ref(struct btrfs_trans_handle *trans,
810 struct btrfs_root *root, struct buffer_head *cur)
812 struct btrfs_disk_key *key;
813 struct btrfs_leaf *leaf;
814 struct btrfs_file_extent_item *fi;
819 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
820 leaf = btrfs_buffer_leaf(cur);
821 nritems = btrfs_header_nritems(&leaf->header);
822 for (i = 0; i < nritems; i++) {
823 key = &leaf->items[i].key;
824 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
826 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
827 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
830 * FIXME make sure to insert a trans record that
831 * repeats the snapshot del on crash
833 ret = btrfs_free_extent(trans, root,
834 btrfs_file_extent_disk_blocknr(fi),
835 btrfs_file_extent_disk_num_blocks(fi),
843 * helper function for drop_snapshot, this walks down the tree dropping ref
846 static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
847 *root, struct btrfs_path *path, int *level)
849 struct buffer_head *next;
850 struct buffer_head *cur;
856 WARN_ON(*level >= BTRFS_MAX_LEVEL);
857 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
863 * walk down to the last node level and free all the leaves
867 WARN_ON(*level >= BTRFS_MAX_LEVEL);
868 cur = path->nodes[*level];
869 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
871 if (path->slots[*level] >=
872 btrfs_header_nritems(btrfs_buffer_header(cur)))
875 ret = drop_leaf_ref(trans, root, cur);
879 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
880 path->slots[*level]);
881 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
884 path->slots[*level]++;
885 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
889 next = read_tree_block(root, blocknr);
890 WARN_ON(*level <= 0);
891 if (path->nodes[*level-1])
892 btrfs_block_release(root, path->nodes[*level-1]);
893 path->nodes[*level-1] = next;
894 *level = btrfs_header_level(btrfs_buffer_header(next));
895 path->slots[*level] = 0;
899 WARN_ON(*level >= BTRFS_MAX_LEVEL);
900 ret = btrfs_free_extent(trans, root,
901 bh_blocknr(path->nodes[*level]), 1, 1);
902 btrfs_block_release(root, path->nodes[*level]);
903 path->nodes[*level] = NULL;
910 * helper for dropping snapshots. This walks back up the tree in the path
911 * to find the first node higher up where we haven't yet gone through
914 static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
915 *root, struct btrfs_path *path, int *level)
920 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
921 slot = path->slots[i];
922 if (slot < btrfs_header_nritems(
923 btrfs_buffer_header(path->nodes[i])) - 1) {
928 ret = btrfs_free_extent(trans, root,
929 bh_blocknr(path->nodes[*level]),
932 btrfs_block_release(root, path->nodes[*level]);
933 path->nodes[*level] = NULL;
941 * drop the reference count on the tree rooted at 'snap'. This traverses
942 * the tree freeing any blocks that have a ref count of zero after being
945 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
946 *root, struct buffer_head *snap)
951 struct btrfs_path *path;
955 path = btrfs_alloc_path();
957 btrfs_init_path(path);
959 level = btrfs_header_level(btrfs_buffer_header(snap));
961 path->nodes[level] = snap;
962 path->slots[level] = 0;
964 wret = walk_down_tree(trans, root, path, &level);
970 wret = walk_up_tree(trans, root, path, &level);
975 btrfs_btree_balance_dirty(root);
977 for (i = 0; i <= orig_level; i++) {
978 if (path->nodes[i]) {
979 btrfs_block_release(root, path->nodes[i]);
982 btrfs_free_path(path);
986 int btrfs_free_block_groups(struct btrfs_fs_info *info)
989 struct btrfs_block_group_cache *cache[8];
993 ret = radix_tree_gang_lookup(&info->block_group_radix,
998 for (i = 0; i < ret; i++) {
999 radix_tree_delete(&info->block_group_radix,
1000 cache[i]->key.objectid +
1001 cache[i]->key.offset - 1);
1008 int btrfs_read_block_groups(struct btrfs_root *root)
1010 struct btrfs_path *path;
1013 struct btrfs_block_group_item *bi;
1014 struct btrfs_block_group_cache *cache;
1015 struct btrfs_key key;
1016 struct btrfs_key found_key;
1017 struct btrfs_leaf *leaf;
1018 u64 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE / root->blocksize;
1021 root = root->fs_info->extent_root;
1023 key.offset = group_size_blocks;
1025 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1027 path = btrfs_alloc_path();
1032 ret = btrfs_search_slot(NULL, root->fs_info->extent_root,
1038 leaf = btrfs_buffer_leaf(path->nodes[0]);
1039 btrfs_disk_key_to_cpu(&found_key,
1040 &leaf->items[path->slots[0]].key);
1041 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1046 bi = btrfs_item_ptr(leaf, path->slots[0],
1047 struct btrfs_block_group_item);
1048 memcpy(&cache->item, bi, sizeof(*bi));
1049 memcpy(&cache->key, &found_key, sizeof(found_key));
1050 cache->last_alloc = cache->key.objectid;
1051 cache->first_free = cache->key.objectid;
1052 key.objectid = found_key.objectid + found_key.offset;
1053 btrfs_release_path(root, path);
1054 ret = radix_tree_insert(&root->fs_info->block_group_radix,
1055 found_key.objectid +
1056 found_key.offset - 1,
1059 used = btrfs_block_group_used(bi);
1060 if (used < (key.offset * 2) / 3) {
1061 radix_tree_tag_set(&root->fs_info->block_group_radix,
1062 found_key.objectid +
1063 found_key.offset - 1,
1064 BTRFS_BLOCK_GROUP_AVAIL);
1067 btrfs_super_total_blocks(root->fs_info->disk_super))
1071 btrfs_free_path(path);