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 static int find_search_start(struct btrfs_root *root, int data)
17 struct btrfs_block_group_cache *cache[8];
18 struct btrfs_fs_info *info = root->fs_info;
24 cache[0] = info->block_group_cache;
27 used = btrfs_block_group_used(&cache[0]->item);
28 if (used < (cache[0]->key.offset * 3 / 2))
33 ret = radix_tree_gang_lookup_tag(&info->block_group_radix,
35 last, ARRAY_SIZE(cache),
36 BTRFS_BLOCK_GROUP_DIRTY);
39 for (i = 0; i < ret; i++) {
40 used = btrfs_block_group_used(&cache[i]->item);
41 if (used < (cache[i]->key.offset * 3 / 2)) {
42 info->block_group_cache = cache[i];
43 cache[i]->last_alloc = cache[i]->first_free;
46 last = cache[i]->key.objectid +
47 cache[i]->key.offset - 1;
52 ret = radix_tree_gang_lookup(&info->block_group_radix,
54 last, ARRAY_SIZE(cache));
57 for (i = 0; i < ret; i++) {
58 used = btrfs_block_group_used(&cache[i]->item);
59 if (used < (cache[i]->key.offset * 3 / 2)) {
60 info->block_group_cache = cache[i];
61 cache[i]->last_alloc = cache[i]->first_free;
64 last = cache[i]->key.objectid +
65 cache[i]->key.offset - 1;
68 info->block_group_cache = NULL;
72 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
73 struct btrfs_root *root,
74 u64 blocknr, u64 num_blocks)
76 struct btrfs_path *path;
80 struct btrfs_extent_item *item;
84 find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1,
86 path = btrfs_alloc_path();
88 btrfs_init_path(path);
89 key.objectid = blocknr;
91 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
92 key.offset = num_blocks;
93 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
96 printk("can't find block %Lu %Lu\n", blocknr, num_blocks);
100 l = btrfs_buffer_leaf(path->nodes[0]);
101 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
102 refs = btrfs_extent_refs(item);
103 btrfs_set_extent_refs(item, refs + 1);
104 btrfs_mark_buffer_dirty(path->nodes[0]);
106 btrfs_release_path(root->fs_info->extent_root, path);
107 btrfs_free_path(path);
108 finish_current_insert(trans, root->fs_info->extent_root);
109 del_pending_extents(trans, root->fs_info->extent_root);
113 static int lookup_extent_ref(struct btrfs_trans_handle *trans,
114 struct btrfs_root *root, u64 blocknr,
115 u64 num_blocks, u32 *refs)
117 struct btrfs_path *path;
119 struct btrfs_key key;
120 struct btrfs_leaf *l;
121 struct btrfs_extent_item *item;
123 path = btrfs_alloc_path();
124 btrfs_init_path(path);
125 key.objectid = blocknr;
126 key.offset = num_blocks;
128 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
129 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
133 l = btrfs_buffer_leaf(path->nodes[0]);
134 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
135 *refs = btrfs_extent_refs(item);
136 btrfs_release_path(root->fs_info->extent_root, path);
137 btrfs_free_path(path);
141 int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
142 struct btrfs_root *root)
144 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
147 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
148 struct buffer_head *buf)
151 struct btrfs_node *buf_node;
152 struct btrfs_leaf *buf_leaf;
153 struct btrfs_disk_key *key;
154 struct btrfs_file_extent_item *fi;
161 buf_node = btrfs_buffer_node(buf);
162 leaf = btrfs_is_leaf(buf_node);
163 buf_leaf = btrfs_buffer_leaf(buf);
164 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
166 key = &buf_leaf->items[i].key;
167 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
169 fi = btrfs_item_ptr(buf_leaf, i,
170 struct btrfs_file_extent_item);
171 if (btrfs_file_extent_type(fi) ==
172 BTRFS_FILE_EXTENT_INLINE)
174 ret = btrfs_inc_extent_ref(trans, root,
175 btrfs_file_extent_disk_blocknr(fi),
176 btrfs_file_extent_disk_num_blocks(fi));
179 blocknr = btrfs_node_blockptr(buf_node, i);
180 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
187 static int write_one_cache_group(struct btrfs_trans_handle *trans,
188 struct btrfs_root *root,
189 struct btrfs_path *path,
190 struct btrfs_block_group_cache *cache)
194 struct btrfs_root *extent_root = root->fs_info->extent_root;
195 struct btrfs_block_group_item *bi;
196 struct btrfs_key ins;
198 find_free_extent(trans, extent_root, 0, 0, (u64)-1, &ins);
199 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
201 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
202 struct btrfs_block_group_item);
203 memcpy(bi, &cache->item, sizeof(*bi));
204 mark_buffer_dirty(path->nodes[0]);
205 btrfs_release_path(extent_root, path);
207 finish_current_insert(trans, extent_root);
208 pending_ret = del_pending_extents(trans, extent_root);
217 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
218 struct btrfs_root *root)
220 struct btrfs_block_group_cache *cache[8];
224 struct radix_tree_root *radix = &root->fs_info->block_group_radix;
226 struct btrfs_path *path;
228 path = btrfs_alloc_path();
233 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
234 0, ARRAY_SIZE(cache),
235 BTRFS_BLOCK_GROUP_DIRTY);
238 for (i = 0; i < ret; i++) {
239 radix_tree_tag_clear(radix, cache[i]->key.objectid +
240 cache[i]->key.offset - 1,
241 BTRFS_BLOCK_GROUP_DIRTY);
242 err = write_one_cache_group(trans, root,
248 btrfs_free_path(path);
252 static int update_block_group(struct btrfs_trans_handle *trans,
253 struct btrfs_root *root,
254 u64 blocknr, u64 num, int alloc)
256 struct btrfs_block_group_cache *cache;
257 struct btrfs_fs_info *info = root->fs_info;
263 ret = radix_tree_gang_lookup(&info->block_group_radix,
264 (void **)&cache, blocknr, 1);
266 printk(KERN_CRIT "blocknr %Lu lookup failed\n",
270 block_in_group = blocknr - cache->key.objectid;
271 WARN_ON(block_in_group > cache->key.offset);
272 radix_tree_tag_set(&info->block_group_radix,
273 cache->key.objectid + cache->key.offset - 1,
274 BTRFS_BLOCK_GROUP_DIRTY);
276 old_val = btrfs_block_group_used(&cache->item);
277 num = min(total, cache->key.offset - block_in_group);
282 if (blocknr > cache->last_alloc)
283 cache->last_alloc = blocknr;
286 if (blocknr < cache->first_free)
287 cache->first_free = blocknr;
289 btrfs_set_block_group_used(&cache->item, old_val);
294 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
297 unsigned long gang[8];
301 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
304 ret = find_first_radix_bit(pinned_radix, gang,
310 for (i = 0; i < ret; i++) {
311 clear_radix_bit(pinned_radix, gang[i]);
314 root->fs_info->block_group_cache = NULL;
318 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
319 btrfs_root *extent_root)
321 struct btrfs_key ins;
322 struct btrfs_extent_item extent_item;
325 u64 super_blocks_used;
326 struct btrfs_fs_info *info = extent_root->fs_info;
328 btrfs_set_extent_refs(&extent_item, 1);
331 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
332 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
334 for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
335 ins.objectid = extent_root->fs_info->extent_tree_insert[i];
336 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
337 btrfs_set_super_blocks_used(info->disk_super,
338 super_blocks_used + 1);
339 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
340 sizeof(extent_item));
343 extent_root->fs_info->extent_tree_insert_nr = 0;
344 extent_root->fs_info->extent_tree_prealloc_nr = 0;
348 static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
351 struct btrfs_header *header;
352 struct buffer_head *bh;
355 bh = btrfs_find_tree_block(root, blocknr);
357 if (buffer_uptodate(bh)) {
359 root->fs_info->running_transaction->transid;
360 header = btrfs_buffer_header(bh);
361 if (btrfs_header_generation(header) ==
363 btrfs_block_release(root, bh);
367 btrfs_block_release(root, bh);
369 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
371 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
378 * remove an extent from the root, returns 0 on success
380 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
381 *root, u64 blocknr, u64 num_blocks, int pin)
383 struct btrfs_path *path;
384 struct btrfs_key key;
385 struct btrfs_fs_info *info = root->fs_info;
386 struct btrfs_root *extent_root = info->extent_root;
388 struct btrfs_extent_item *ei;
389 struct btrfs_key ins;
392 key.objectid = blocknr;
394 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
395 key.offset = num_blocks;
397 find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
398 path = btrfs_alloc_path();
400 btrfs_init_path(path);
402 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
404 printk("failed to find %Lu\n", key.objectid);
405 btrfs_print_tree(extent_root, extent_root->node);
406 printk("failed to find %Lu\n", key.objectid);
409 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
410 struct btrfs_extent_item);
411 BUG_ON(ei->refs == 0);
412 refs = btrfs_extent_refs(ei) - 1;
413 btrfs_set_extent_refs(ei, refs);
414 btrfs_mark_buffer_dirty(path->nodes[0]);
416 u64 super_blocks_used;
419 ret = pin_down_block(root, blocknr, 0);
423 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
424 btrfs_set_super_blocks_used(info->disk_super,
425 super_blocks_used - num_blocks);
426 ret = btrfs_del_item(trans, extent_root, path);
429 ret = update_block_group(trans, root, blocknr, num_blocks, 0);
432 btrfs_release_path(extent_root, path);
433 btrfs_free_path(path);
434 finish_current_insert(trans, extent_root);
439 * find all the blocks marked as pending in the radix tree and remove
440 * them from the extent map
442 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
443 btrfs_root *extent_root)
448 unsigned long gang[4];
450 struct radix_tree_root *pending_radix;
451 struct radix_tree_root *pinned_radix;
453 pending_radix = &extent_root->fs_info->pending_del_radix;
454 pinned_radix = &extent_root->fs_info->pinned_radix;
457 ret = find_first_radix_bit(pending_radix, gang,
461 for (i = 0; i < ret; i++) {
462 wret = set_radix_bit(pinned_radix, gang[i]);
464 wret = clear_radix_bit(pending_radix, gang[i]);
466 wret = __free_extent(trans, extent_root,
476 * remove an extent from the root, returns 0 on success
478 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
479 *root, u64 blocknr, u64 num_blocks, int pin)
481 struct btrfs_root *extent_root = root->fs_info->extent_root;
485 if (root == extent_root) {
486 pin_down_block(root, blocknr, 1);
489 ret = __free_extent(trans, root, blocknr, num_blocks, pin);
490 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
491 return ret ? ret : pending_ret;
495 * walks the btree of allocated extents and find a hole of a given size.
496 * The key ins is changed to record the hole:
497 * ins->objectid == block start
498 * ins->flags = BTRFS_EXTENT_ITEM_KEY
499 * ins->offset == number of blocks
500 * Any available blocks before search_start are skipped.
502 static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
503 *orig_root, u64 num_blocks, u64 search_start, u64
504 search_end, struct btrfs_key *ins)
506 struct btrfs_path *path;
507 struct btrfs_key key;
514 struct btrfs_leaf *l;
515 struct btrfs_root * root = orig_root->fs_info->extent_root;
516 struct btrfs_fs_info *info = root->fs_info;
517 int total_needed = num_blocks;
519 int fill_prealloc = 0;
522 path = btrfs_alloc_path();
524 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
526 level = btrfs_header_level(btrfs_buffer_header(root->node));
527 if (num_blocks == 0) {
530 total_needed = min(level + 2, BTRFS_MAX_LEVEL) * 3;
532 find_search_start(root, 0);
533 if (info->block_group_cache &&
534 info->block_group_cache->last_alloc > search_start)
535 search_start = info->block_group_cache->last_alloc;
538 btrfs_init_path(path);
539 ins->objectid = search_start;
542 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
546 if (path->slots[0] > 0)
550 l = btrfs_buffer_leaf(path->nodes[0]);
551 slot = path->slots[0];
552 if (slot >= btrfs_header_nritems(&l->header)) {
554 info->extent_tree_prealloc_nr = 0;
557 ret = btrfs_next_leaf(root, path);
563 ins->objectid = search_start;
564 ins->offset = (u64)-1 - search_start;
568 ins->objectid = last_block > search_start ?
569 last_block : search_start;
570 ins->offset = (u64)-1 - ins->objectid;
573 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
574 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
576 if (key.objectid >= search_start) {
578 if (last_block < search_start)
579 last_block = search_start;
580 hole_size = key.objectid - last_block;
581 if (hole_size > num_blocks) {
582 ins->objectid = last_block;
583 ins->offset = hole_size;
589 last_block = key.objectid + key.offset;
595 /* we have to make sure we didn't find an extent that has already
596 * been allocated by the map tree or the original allocation
598 btrfs_release_path(root, path);
599 BUG_ON(ins->objectid < search_start);
600 for (test_block = ins->objectid;
601 test_block < ins->objectid + num_blocks; test_block++) {
602 if (test_radix_bit(&info->pinned_radix, test_block)) {
603 search_start = test_block + 1;
607 if (!fill_prealloc && info->extent_tree_insert_nr) {
609 info->extent_tree_insert[info->extent_tree_insert_nr - 1];
610 if (ins->objectid + num_blocks >
611 info->extent_tree_insert[0] &&
612 ins->objectid <= last) {
613 search_start = last + 1;
618 if (!fill_prealloc && info->extent_tree_prealloc_nr) {
620 info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
621 if (ins->objectid + num_blocks > first &&
622 ins->objectid <= info->extent_tree_prealloc[0]) {
623 search_start = info->extent_tree_prealloc[0] + 1;
630 test_block = ins->objectid;
631 while(test_block < ins->objectid + ins->offset &&
632 total_found < total_needed) {
633 nr = total_needed - total_found - 1;
635 info->extent_tree_prealloc[nr] = test_block;
639 if (total_found < total_needed) {
640 search_start = test_block;
643 info->extent_tree_prealloc_nr = total_found;
645 ret = radix_tree_gang_lookup(&info->block_group_radix,
646 (void **)&info->block_group_cache,
649 info->block_group_cache->last_alloc = ins->objectid;
651 ins->offset = num_blocks;
652 btrfs_free_path(path);
655 btrfs_release_path(root, path);
656 btrfs_free_path(path);
660 * finds a free extent and does all the dirty work required for allocation
661 * returns the key for the extent through ins, and a tree buffer for
662 * the first block of the extent through buf.
664 * returns 0 if everything worked, non-zero otherwise.
666 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
667 struct btrfs_root *root, u64 owner,
668 u64 num_blocks, u64 search_start,
669 u64 search_end, struct btrfs_key *ins)
673 u64 super_blocks_used;
674 struct btrfs_fs_info *info = root->fs_info;
675 struct btrfs_root *extent_root = info->extent_root;
676 struct btrfs_extent_item extent_item;
677 struct btrfs_key prealloc_key;
679 btrfs_set_extent_refs(&extent_item, 1);
680 btrfs_set_extent_owner(&extent_item, owner);
682 if (root == extent_root) {
684 BUG_ON(info->extent_tree_prealloc_nr == 0);
685 BUG_ON(num_blocks != 1);
687 info->extent_tree_prealloc_nr--;
688 nr = info->extent_tree_prealloc_nr;
689 ins->objectid = info->extent_tree_prealloc[nr];
690 info->extent_tree_insert[info->extent_tree_insert_nr++] =
692 ret = update_block_group(trans, root,
693 ins->objectid, ins->offset, 1);
697 /* do the real allocation */
698 ret = find_free_extent(trans, root, num_blocks, search_start,
703 /* then do prealloc for the extent tree */
704 ret = find_free_extent(trans, root, 0, ins->objectid + ins->offset,
705 search_end, &prealloc_key);
709 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
710 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
712 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
713 sizeof(extent_item));
715 finish_current_insert(trans, extent_root);
716 pending_ret = del_pending_extents(trans, extent_root);
721 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
726 * helper function to allocate a block for a given tree
727 * returns the tree buffer or NULL.
729 struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
730 struct btrfs_root *root)
732 struct btrfs_key ins;
734 struct buffer_head *buf;
736 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
737 1, 0, (unsigned long)-1, &ins);
743 buf = btrfs_find_create_tree_block(root, ins.objectid);
744 set_buffer_uptodate(buf);
748 static int drop_leaf_ref(struct btrfs_trans_handle *trans,
749 struct btrfs_root *root, struct buffer_head *cur)
751 struct btrfs_disk_key *key;
752 struct btrfs_leaf *leaf;
753 struct btrfs_file_extent_item *fi;
758 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
759 leaf = btrfs_buffer_leaf(cur);
760 nritems = btrfs_header_nritems(&leaf->header);
761 for (i = 0; i < nritems; i++) {
762 key = &leaf->items[i].key;
763 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
765 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
766 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
769 * FIXME make sure to insert a trans record that
770 * repeats the snapshot del on crash
772 ret = btrfs_free_extent(trans, root,
773 btrfs_file_extent_disk_blocknr(fi),
774 btrfs_file_extent_disk_num_blocks(fi),
782 * helper function for drop_snapshot, this walks down the tree dropping ref
785 static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
786 *root, struct btrfs_path *path, int *level)
788 struct buffer_head *next;
789 struct buffer_head *cur;
795 WARN_ON(*level >= BTRFS_MAX_LEVEL);
796 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
802 * walk down to the last node level and free all the leaves
806 WARN_ON(*level >= BTRFS_MAX_LEVEL);
807 cur = path->nodes[*level];
808 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
810 if (path->slots[*level] >=
811 btrfs_header_nritems(btrfs_buffer_header(cur)))
814 ret = drop_leaf_ref(trans, root, cur);
818 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
819 path->slots[*level]);
820 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
823 path->slots[*level]++;
824 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
828 next = read_tree_block(root, blocknr);
829 WARN_ON(*level <= 0);
830 if (path->nodes[*level-1])
831 btrfs_block_release(root, path->nodes[*level-1]);
832 path->nodes[*level-1] = next;
833 *level = btrfs_header_level(btrfs_buffer_header(next));
834 path->slots[*level] = 0;
838 WARN_ON(*level >= BTRFS_MAX_LEVEL);
839 ret = btrfs_free_extent(trans, root,
840 bh_blocknr(path->nodes[*level]), 1, 1);
841 btrfs_block_release(root, path->nodes[*level]);
842 path->nodes[*level] = NULL;
849 * helper for dropping snapshots. This walks back up the tree in the path
850 * to find the first node higher up where we haven't yet gone through
853 static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
854 *root, struct btrfs_path *path, int *level)
859 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
860 slot = path->slots[i];
861 if (slot < btrfs_header_nritems(
862 btrfs_buffer_header(path->nodes[i])) - 1) {
867 ret = btrfs_free_extent(trans, root,
868 bh_blocknr(path->nodes[*level]),
871 btrfs_block_release(root, path->nodes[*level]);
872 path->nodes[*level] = NULL;
880 * drop the reference count on the tree rooted at 'snap'. This traverses
881 * the tree freeing any blocks that have a ref count of zero after being
884 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
885 *root, struct buffer_head *snap)
890 struct btrfs_path *path;
894 path = btrfs_alloc_path();
896 btrfs_init_path(path);
898 level = btrfs_header_level(btrfs_buffer_header(snap));
900 path->nodes[level] = snap;
901 path->slots[level] = 0;
903 wret = walk_down_tree(trans, root, path, &level);
909 wret = walk_up_tree(trans, root, path, &level);
915 for (i = 0; i <= orig_level; i++) {
916 if (path->nodes[i]) {
917 btrfs_block_release(root, path->nodes[i]);
920 btrfs_free_path(path);
924 int btrfs_free_block_groups(struct btrfs_fs_info *info)
927 struct btrfs_block_group_cache *cache[8];
931 ret = radix_tree_gang_lookup(&info->block_group_radix,
936 for (i = 0; i < ret; i++) {
937 radix_tree_delete(&info->block_group_radix,
938 cache[i]->key.objectid +
939 cache[i]->key.offset - 1);
946 int btrfs_read_block_groups(struct btrfs_root *root)
948 struct btrfs_path *path;
951 struct btrfs_block_group_item *bi;
952 struct btrfs_block_group_cache *cache;
953 struct btrfs_key key;
954 struct btrfs_key found_key;
955 struct btrfs_leaf *leaf;
956 u64 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE / root->blocksize;
958 root = root->fs_info->extent_root;
960 key.offset = group_size_blocks;
962 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
964 path = btrfs_alloc_path();
969 ret = btrfs_search_slot(NULL, root->fs_info->extent_root,
975 leaf = btrfs_buffer_leaf(path->nodes[0]);
976 btrfs_disk_key_to_cpu(&found_key,
977 &leaf->items[path->slots[0]].key);
978 cache = kmalloc(sizeof(*cache), GFP_NOFS);
983 bi = btrfs_item_ptr(leaf, path->slots[0],
984 struct btrfs_block_group_item);
985 memcpy(&cache->item, bi, sizeof(*bi));
986 memcpy(&cache->key, &found_key, sizeof(found_key));
987 cache->last_alloc = 0;
988 cache->first_free = 0;
989 key.objectid = found_key.objectid + found_key.offset;
990 btrfs_release_path(root, path);
991 ret = radix_tree_insert(&root->fs_info->block_group_radix,
993 found_key.offset - 1,
997 btrfs_super_total_blocks(root->fs_info->disk_super))
1001 btrfs_free_path(path);