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, int data);
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 struct btrfs_block_group_cache *lookup_block_group(struct
19 struct btrfs_block_group_cache *block_group;
22 ret = radix_tree_gang_lookup(&info->block_group_radix,
23 (void **)&block_group,
26 if (block_group->key.objectid <= blocknr && blocknr <=
27 block_group->key.objectid + block_group->key.offset)
30 ret = radix_tree_gang_lookup(&info->block_group_data_radix,
31 (void **)&block_group,
34 if (block_group->key.objectid <= blocknr && blocknr <=
35 block_group->key.objectid + block_group->key.offset)
39 printk("lookup_block_group fails for blocknr %Lu\n", blocknr);
40 printk("last ret was %d\n", ret);
42 printk("last block group was %Lu %Lu\n", block_group->key.objectid, block_group->key.offset);
47 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
48 struct btrfs_block_group_cache
49 *hint, u64 search_start,
52 struct btrfs_block_group_cache *cache[8];
53 struct btrfs_block_group_cache *found_group = NULL;
54 struct btrfs_fs_info *info = root->fs_info;
55 struct radix_tree_root *radix;
64 radix = &info->block_group_data_radix;
66 radix = &info->block_group_radix;
69 struct btrfs_block_group_cache *shint;
70 shint = lookup_block_group(info, search_start);
71 if (shint->data == data) {
72 used = btrfs_block_group_used(&shint->item);
73 if (used + shint->pinned <
74 (shint->key.offset * 8) / 10) {
79 if (hint && hint->data == data) {
80 used = btrfs_block_group_used(&hint->item);
81 if (used + hint->pinned < (hint->key.offset * 8) / 10) {
84 if (used >= (hint->key.offset * 8) / 10) {
85 radix_tree_tag_clear(radix,
88 BTRFS_BLOCK_GROUP_AVAIL);
90 last = hint->key.offset * 2;
91 if (hint->key.objectid >= last)
92 last = max(search_start, hint->key.objectid - last);
94 last = hint->key.objectid + hint->key.offset;
97 hint_last = search_start;
101 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
102 last, ARRAY_SIZE(cache),
103 BTRFS_BLOCK_GROUP_AVAIL);
106 for (i = 0; i < ret; i++) {
107 last = cache[i]->key.objectid +
108 cache[i]->key.offset;
109 used = btrfs_block_group_used(&cache[i]->item);
110 if (used + cache[i]->pinned <
111 (cache[i]->key.offset * 8) / 10) {
112 found_group = cache[i];
115 if (used >= (cache[i]->key.offset * 8) / 10) {
116 radix_tree_tag_clear(radix,
117 cache[i]->key.objectid +
118 cache[i]->key.offset - 1,
119 BTRFS_BLOCK_GROUP_AVAIL);
126 ret = radix_tree_gang_lookup(radix, (void **)cache,
127 last, ARRAY_SIZE(cache));
130 for (i = 0; i < ret; i++) {
131 last = cache[i]->key.objectid +
132 cache[i]->key.offset;
133 used = btrfs_block_group_used(&cache[i]->item);
134 if (used + cache[i]->pinned < cache[i]->key.offset) {
135 found_group = cache[i];
138 if (used >= cache[i]->key.offset) {
139 radix_tree_tag_clear(radix,
140 cache[i]->key.objectid +
141 cache[i]->key.offset - 1,
142 BTRFS_BLOCK_GROUP_AVAIL);
152 ret = radix_tree_gang_lookup(radix,
153 (void **)&found_group, 0, 1);
160 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
161 struct btrfs_root *root,
162 u64 blocknr, u64 num_blocks)
164 struct btrfs_path *path;
166 struct btrfs_key key;
167 struct btrfs_leaf *l;
168 struct btrfs_extent_item *item;
169 struct btrfs_key ins;
172 find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1,
174 path = btrfs_alloc_path();
176 btrfs_init_path(path);
177 key.objectid = blocknr;
179 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
180 key.offset = num_blocks;
181 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
184 printk("can't find block %Lu %Lu\n", blocknr, num_blocks);
188 l = btrfs_buffer_leaf(path->nodes[0]);
189 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
190 refs = btrfs_extent_refs(item);
191 btrfs_set_extent_refs(item, refs + 1);
192 btrfs_mark_buffer_dirty(path->nodes[0]);
194 btrfs_release_path(root->fs_info->extent_root, path);
195 btrfs_free_path(path);
196 finish_current_insert(trans, root->fs_info->extent_root);
197 del_pending_extents(trans, root->fs_info->extent_root);
201 static int lookup_extent_ref(struct btrfs_trans_handle *trans,
202 struct btrfs_root *root, u64 blocknr,
203 u64 num_blocks, u32 *refs)
205 struct btrfs_path *path;
207 struct btrfs_key key;
208 struct btrfs_leaf *l;
209 struct btrfs_extent_item *item;
211 path = btrfs_alloc_path();
212 btrfs_init_path(path);
213 key.objectid = blocknr;
214 key.offset = num_blocks;
216 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
217 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
221 l = btrfs_buffer_leaf(path->nodes[0]);
222 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
223 *refs = btrfs_extent_refs(item);
224 btrfs_release_path(root->fs_info->extent_root, path);
225 btrfs_free_path(path);
229 int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
230 struct btrfs_root *root)
232 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
235 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
236 struct buffer_head *buf)
239 struct btrfs_node *buf_node;
240 struct btrfs_leaf *buf_leaf;
241 struct btrfs_disk_key *key;
242 struct btrfs_file_extent_item *fi;
249 buf_node = btrfs_buffer_node(buf);
250 leaf = btrfs_is_leaf(buf_node);
251 buf_leaf = btrfs_buffer_leaf(buf);
252 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
254 key = &buf_leaf->items[i].key;
255 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
257 fi = btrfs_item_ptr(buf_leaf, i,
258 struct btrfs_file_extent_item);
259 if (btrfs_file_extent_type(fi) ==
260 BTRFS_FILE_EXTENT_INLINE)
262 ret = btrfs_inc_extent_ref(trans, root,
263 btrfs_file_extent_disk_blocknr(fi),
264 btrfs_file_extent_disk_num_blocks(fi));
267 blocknr = btrfs_node_blockptr(buf_node, i);
268 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
275 static int write_one_cache_group(struct btrfs_trans_handle *trans,
276 struct btrfs_root *root,
277 struct btrfs_path *path,
278 struct btrfs_block_group_cache *cache)
282 struct btrfs_root *extent_root = root->fs_info->extent_root;
283 struct btrfs_block_group_item *bi;
284 struct btrfs_key ins;
286 find_free_extent(trans, extent_root, 0, 0, (u64)-1, &ins, 0);
287 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
289 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
290 struct btrfs_block_group_item);
291 memcpy(bi, &cache->item, sizeof(*bi));
292 mark_buffer_dirty(path->nodes[0]);
293 btrfs_release_path(extent_root, path);
295 finish_current_insert(trans, extent_root);
296 pending_ret = del_pending_extents(trans, extent_root);
302 cache->last_alloc = cache->first_free;
307 static int write_dirty_block_radix(struct btrfs_trans_handle *trans,
308 struct btrfs_root *root,
309 struct radix_tree_root *radix)
311 struct btrfs_block_group_cache *cache[8];
316 struct btrfs_path *path;
318 path = btrfs_alloc_path();
323 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
324 0, ARRAY_SIZE(cache),
325 BTRFS_BLOCK_GROUP_DIRTY);
328 for (i = 0; i < ret; i++) {
329 radix_tree_tag_clear(radix, cache[i]->key.objectid +
330 cache[i]->key.offset - 1,
331 BTRFS_BLOCK_GROUP_DIRTY);
332 err = write_one_cache_group(trans, root,
338 btrfs_free_path(path);
342 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
343 struct btrfs_root *root)
347 ret = write_dirty_block_radix(trans, root,
348 &root->fs_info->block_group_radix);
349 ret2 = write_dirty_block_radix(trans, root,
350 &root->fs_info->block_group_data_radix);
358 static int update_block_group(struct btrfs_trans_handle *trans,
359 struct btrfs_root *root,
360 u64 blocknr, u64 num, int alloc)
362 struct btrfs_block_group_cache *cache;
363 struct btrfs_fs_info *info = root->fs_info;
369 cache = lookup_block_group(info, blocknr);
371 printk(KERN_CRIT "blocknr %Lu lookup failed\n",
375 block_in_group = blocknr - cache->key.objectid;
376 WARN_ON(block_in_group > cache->key.offset);
377 radix_tree_tag_set(cache->radix, cache->key.objectid +
378 cache->key.offset - 1,
379 BTRFS_BLOCK_GROUP_DIRTY);
381 old_val = btrfs_block_group_used(&cache->item);
382 num = min(total, cache->key.offset - block_in_group);
387 if (blocknr > cache->last_alloc)
388 cache->last_alloc = blocknr;
391 if (blocknr < cache->first_free)
392 cache->first_free = blocknr;
394 btrfs_set_block_group_used(&cache->item, old_val);
399 static int try_remove_page(struct address_space *mapping, unsigned long index)
402 ret = invalidate_mapping_pages(mapping, index, index);
406 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
409 unsigned long gang[8];
410 struct inode *btree_inode = root->fs_info->btree_inode;
411 struct btrfs_block_group_cache *block_group;
415 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
418 ret = find_first_radix_bit(pinned_radix, gang,
424 for (i = 0; i < ret; i++) {
425 clear_radix_bit(pinned_radix, gang[i]);
426 block_group = lookup_block_group(root->fs_info,
429 WARN_ON(block_group->pinned == 0);
430 block_group->pinned--;
431 if (gang[i] < block_group->last_alloc)
432 block_group->last_alloc = gang[i];
434 try_remove_page(btree_inode->i_mapping,
435 gang[i] << (PAGE_CACHE_SHIFT -
436 btree_inode->i_blkbits));
442 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
443 btrfs_root *extent_root)
445 struct btrfs_key ins;
446 struct btrfs_extent_item extent_item;
449 u64 super_blocks_used;
450 struct btrfs_fs_info *info = extent_root->fs_info;
452 btrfs_set_extent_refs(&extent_item, 1);
455 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
456 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
458 for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
459 ins.objectid = extent_root->fs_info->extent_tree_insert[i];
460 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
461 btrfs_set_super_blocks_used(info->disk_super,
462 super_blocks_used + 1);
463 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
464 sizeof(extent_item));
467 extent_root->fs_info->extent_tree_insert_nr = 0;
468 extent_root->fs_info->extent_tree_prealloc_nr = 0;
472 static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
475 struct btrfs_header *header;
476 struct buffer_head *bh;
479 bh = btrfs_find_tree_block(root, blocknr);
481 if (buffer_uptodate(bh)) {
483 root->fs_info->running_transaction->transid;
484 header = btrfs_buffer_header(bh);
485 if (btrfs_header_generation(header) ==
487 btrfs_block_release(root, bh);
491 btrfs_block_release(root, bh);
493 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
495 struct btrfs_block_group_cache *cache;
496 cache = lookup_block_group(root->fs_info, blocknr);
501 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
508 * remove an extent from the root, returns 0 on success
510 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
511 *root, u64 blocknr, u64 num_blocks, int pin)
513 struct btrfs_path *path;
514 struct btrfs_key key;
515 struct btrfs_fs_info *info = root->fs_info;
516 struct btrfs_root *extent_root = info->extent_root;
518 struct btrfs_extent_item *ei;
519 struct btrfs_key ins;
522 key.objectid = blocknr;
524 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
525 key.offset = num_blocks;
527 find_free_extent(trans, root, 0, 0, (u64)-1, &ins, 0);
528 path = btrfs_alloc_path();
530 btrfs_init_path(path);
532 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
534 printk("failed to find %Lu\n", key.objectid);
535 btrfs_print_tree(extent_root, extent_root->node);
536 printk("failed to find %Lu\n", key.objectid);
539 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
540 struct btrfs_extent_item);
541 BUG_ON(ei->refs == 0);
542 refs = btrfs_extent_refs(ei) - 1;
543 btrfs_set_extent_refs(ei, refs);
544 btrfs_mark_buffer_dirty(path->nodes[0]);
546 u64 super_blocks_used;
549 ret = pin_down_block(root, blocknr, 0);
553 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
554 btrfs_set_super_blocks_used(info->disk_super,
555 super_blocks_used - num_blocks);
556 ret = btrfs_del_item(trans, extent_root, path);
559 ret = update_block_group(trans, root, blocknr, num_blocks, 0);
562 btrfs_release_path(extent_root, path);
563 btrfs_free_path(path);
564 finish_current_insert(trans, extent_root);
569 * find all the blocks marked as pending in the radix tree and remove
570 * them from the extent map
572 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
573 btrfs_root *extent_root)
578 unsigned long gang[4];
580 struct radix_tree_root *pending_radix;
581 struct radix_tree_root *pinned_radix;
582 struct btrfs_block_group_cache *cache;
584 pending_radix = &extent_root->fs_info->pending_del_radix;
585 pinned_radix = &extent_root->fs_info->pinned_radix;
588 ret = find_first_radix_bit(pending_radix, gang,
592 for (i = 0; i < ret; i++) {
593 wret = set_radix_bit(pinned_radix, gang[i]);
595 cache = lookup_block_group(extent_root->fs_info,
601 printk(KERN_CRIT "set_radix_bit, err %d\n",
605 wret = clear_radix_bit(pending_radix, gang[i]);
607 wret = __free_extent(trans, extent_root,
617 * remove an extent from the root, returns 0 on success
619 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
620 *root, u64 blocknr, u64 num_blocks, int pin)
622 struct btrfs_root *extent_root = root->fs_info->extent_root;
626 if (root == extent_root) {
627 pin_down_block(root, blocknr, 1);
630 ret = __free_extent(trans, root, blocknr, num_blocks, pin);
631 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
632 return ret ? ret : pending_ret;
636 * walks the btree of allocated extents and find a hole of a given size.
637 * The key ins is changed to record the hole:
638 * ins->objectid == block start
639 * ins->flags = BTRFS_EXTENT_ITEM_KEY
640 * ins->offset == number of blocks
641 * Any available blocks before search_start are skipped.
643 static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
644 *orig_root, u64 num_blocks, u64 search_start, u64
645 search_end, struct btrfs_key *ins, int data)
647 struct btrfs_path *path;
648 struct btrfs_key key;
654 u64 orig_search_start = search_start;
656 struct btrfs_leaf *l;
657 struct btrfs_root * root = orig_root->fs_info->extent_root;
658 struct btrfs_fs_info *info = root->fs_info;
659 int total_needed = num_blocks;
661 int fill_prealloc = 0;
663 struct btrfs_block_group_cache *block_group;
666 path = btrfs_alloc_path();
668 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
670 level = btrfs_header_level(btrfs_buffer_header(root->node));
671 if (num_blocks == 0) {
674 total_needed = (min(level + 1, BTRFS_MAX_LEVEL) + 2) * 3;
676 if (search_end == (u64)-1)
677 search_end = btrfs_super_total_blocks(info->disk_super);
679 block_group = lookup_block_group(info, search_start);
680 block_group = btrfs_find_block_group(root, block_group,
683 block_group = btrfs_find_block_group(root,
684 trans->block_group, 0,
689 if (!full_scan && block_group->data != data)
691 if (block_group->last_alloc > search_start)
692 search_start = block_group->last_alloc;
693 btrfs_init_path(path);
694 ins->objectid = search_start;
697 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
701 if (path->slots[0] > 0)
705 l = btrfs_buffer_leaf(path->nodes[0]);
706 slot = path->slots[0];
707 if (slot >= btrfs_header_nritems(&l->header)) {
709 info->extent_tree_prealloc_nr = 0;
712 ret = btrfs_next_leaf(root, path);
718 ins->objectid = search_start;
719 ins->offset = search_end - search_start;
723 ins->objectid = last_block > search_start ?
724 last_block : search_start;
725 ins->offset = search_end - ins->objectid;
728 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
729 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
731 if (key.objectid >= search_start) {
733 if (last_block < search_start)
734 last_block = search_start;
735 hole_size = key.objectid - last_block;
736 if (hole_size >= num_blocks) {
737 ins->objectid = last_block;
738 ins->offset = hole_size;
744 last_block = key.objectid + key.offset;
745 if (last_block >= block_group->key.objectid +
746 block_group->key.offset) {
747 btrfs_release_path(root, path);
748 search_start = block_group->key.objectid +
749 block_group->key.offset * 2;
757 /* we have to make sure we didn't find an extent that has already
758 * been allocated by the map tree or the original allocation
760 btrfs_release_path(root, path);
761 BUG_ON(ins->objectid < search_start);
762 if (ins->objectid + num_blocks >= search_end) {
765 search_start = orig_search_start;
769 for (test_block = ins->objectid;
770 test_block < ins->objectid + num_blocks; test_block++) {
771 if (test_radix_bit(&info->pinned_radix, test_block)) {
772 search_start = test_block + 1;
776 if (!fill_prealloc && info->extent_tree_insert_nr) {
778 info->extent_tree_insert[info->extent_tree_insert_nr - 1];
779 if (ins->objectid + num_blocks >
780 info->extent_tree_insert[0] &&
781 ins->objectid <= last) {
782 search_start = last + 1;
787 if (!fill_prealloc && info->extent_tree_prealloc_nr) {
789 info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
790 if (ins->objectid + num_blocks > first &&
791 ins->objectid <= info->extent_tree_prealloc[0]) {
792 search_start = info->extent_tree_prealloc[0] + 1;
799 test_block = ins->objectid;
800 while(test_block < ins->objectid + ins->offset &&
801 total_found < total_needed) {
802 nr = total_needed - total_found - 1;
804 info->extent_tree_prealloc[nr] = test_block;
808 if (total_found < total_needed) {
809 search_start = test_block;
812 info->extent_tree_prealloc_nr = total_found;
814 block_group = lookup_block_group(info, ins->objectid);
816 block_group->last_alloc = ins->objectid;
818 trans->block_group = block_group;
820 ins->offset = num_blocks;
821 btrfs_free_path(path);
825 if (search_start + num_blocks >= search_end) {
826 search_start = orig_search_start;
829 block_group = lookup_block_group(info, search_start);
831 block_group = btrfs_find_block_group(root, block_group,
836 btrfs_release_path(root, path);
837 btrfs_free_path(path);
841 * finds a free extent and does all the dirty work required for allocation
842 * returns the key for the extent through ins, and a tree buffer for
843 * the first block of the extent through buf.
845 * returns 0 if everything worked, non-zero otherwise.
847 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
848 struct btrfs_root *root, u64 owner,
849 u64 num_blocks, u64 search_start,
850 u64 search_end, struct btrfs_key *ins, int data)
854 u64 super_blocks_used;
855 struct btrfs_fs_info *info = root->fs_info;
856 struct btrfs_root *extent_root = info->extent_root;
857 struct btrfs_extent_item extent_item;
858 struct btrfs_key prealloc_key;
860 btrfs_set_extent_refs(&extent_item, 1);
861 btrfs_set_extent_owner(&extent_item, owner);
863 if (root == extent_root) {
865 BUG_ON(info->extent_tree_prealloc_nr == 0);
866 BUG_ON(num_blocks != 1);
868 info->extent_tree_prealloc_nr--;
869 nr = info->extent_tree_prealloc_nr;
870 ins->objectid = info->extent_tree_prealloc[nr];
871 info->extent_tree_insert[info->extent_tree_insert_nr++] =
873 ret = update_block_group(trans, root,
874 ins->objectid, ins->offset, 1);
878 /* do the real allocation */
879 ret = find_free_extent(trans, root, num_blocks, search_start,
880 search_end, ins, data);
884 /* then do prealloc for the extent tree */
885 if (ins->objectid + ins->offset >= search_end)
886 search_end = ins->objectid - 1;
888 search_start = ins->objectid + ins->offset;
890 ret = find_free_extent(trans, root, 0, search_start,
891 search_end, &prealloc_key, 0);
895 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
896 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
898 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
899 sizeof(extent_item));
901 finish_current_insert(trans, extent_root);
902 pending_ret = del_pending_extents(trans, extent_root);
907 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
912 * helper function to allocate a block for a given tree
913 * returns the tree buffer or NULL.
915 struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
916 struct btrfs_root *root, u64 hint)
918 struct btrfs_key ins;
920 struct buffer_head *buf;
922 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
923 1, hint, (unsigned long)-1, &ins, 0);
929 buf = btrfs_find_create_tree_block(root, ins.objectid);
930 set_buffer_uptodate(buf);
931 set_buffer_checked(buf);
932 set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
936 static int drop_leaf_ref(struct btrfs_trans_handle *trans,
937 struct btrfs_root *root, struct buffer_head *cur)
939 struct btrfs_disk_key *key;
940 struct btrfs_leaf *leaf;
941 struct btrfs_file_extent_item *fi;
946 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
947 leaf = btrfs_buffer_leaf(cur);
948 nritems = btrfs_header_nritems(&leaf->header);
949 for (i = 0; i < nritems; i++) {
950 key = &leaf->items[i].key;
951 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
953 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
954 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
957 * FIXME make sure to insert a trans record that
958 * repeats the snapshot del on crash
960 ret = btrfs_free_extent(trans, root,
961 btrfs_file_extent_disk_blocknr(fi),
962 btrfs_file_extent_disk_num_blocks(fi),
970 * helper function for drop_snapshot, this walks down the tree dropping ref
973 static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
974 *root, struct btrfs_path *path, int *level)
976 struct buffer_head *next;
977 struct buffer_head *cur;
983 WARN_ON(*level >= BTRFS_MAX_LEVEL);
984 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
990 * walk down to the last node level and free all the leaves
994 WARN_ON(*level >= BTRFS_MAX_LEVEL);
995 cur = path->nodes[*level];
996 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
998 if (path->slots[*level] >=
999 btrfs_header_nritems(btrfs_buffer_header(cur)))
1002 ret = drop_leaf_ref(trans, root, cur);
1006 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
1007 path->slots[*level]);
1008 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
1011 path->slots[*level]++;
1012 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
1016 next = read_tree_block(root, blocknr);
1017 WARN_ON(*level <= 0);
1018 if (path->nodes[*level-1])
1019 btrfs_block_release(root, path->nodes[*level-1]);
1020 path->nodes[*level-1] = next;
1021 *level = btrfs_header_level(btrfs_buffer_header(next));
1022 path->slots[*level] = 0;
1025 WARN_ON(*level < 0);
1026 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1027 ret = btrfs_free_extent(trans, root,
1028 bh_blocknr(path->nodes[*level]), 1, 1);
1029 btrfs_block_release(root, path->nodes[*level]);
1030 path->nodes[*level] = NULL;
1037 * helper for dropping snapshots. This walks back up the tree in the path
1038 * to find the first node higher up where we haven't yet gone through
1041 static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1042 *root, struct btrfs_path *path, int *level)
1047 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1048 slot = path->slots[i];
1049 if (slot < btrfs_header_nritems(
1050 btrfs_buffer_header(path->nodes[i])) - 1) {
1055 ret = btrfs_free_extent(trans, root,
1056 bh_blocknr(path->nodes[*level]),
1059 btrfs_block_release(root, path->nodes[*level]);
1060 path->nodes[*level] = NULL;
1068 * drop the reference count on the tree rooted at 'snap'. This traverses
1069 * the tree freeing any blocks that have a ref count of zero after being
1072 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
1073 *root, struct buffer_head *snap)
1078 struct btrfs_path *path;
1082 path = btrfs_alloc_path();
1084 btrfs_init_path(path);
1086 level = btrfs_header_level(btrfs_buffer_header(snap));
1088 path->nodes[level] = snap;
1089 path->slots[level] = 0;
1091 wret = walk_down_tree(trans, root, path, &level);
1097 wret = walk_up_tree(trans, root, path, &level);
1102 btrfs_btree_balance_dirty(root);
1104 for (i = 0; i <= orig_level; i++) {
1105 if (path->nodes[i]) {
1106 btrfs_block_release(root, path->nodes[i]);
1109 btrfs_free_path(path);
1113 static int free_block_group_radix(struct radix_tree_root *radix)
1116 struct btrfs_block_group_cache *cache[8];
1120 ret = radix_tree_gang_lookup(radix, (void **)cache, 0,
1124 for (i = 0; i < ret; i++) {
1125 radix_tree_delete(radix, cache[i]->key.objectid +
1126 cache[i]->key.offset - 1);
1133 int btrfs_free_block_groups(struct btrfs_fs_info *info)
1138 ret = free_block_group_radix(&info->block_group_radix);
1139 ret2 = free_block_group_radix(&info->block_group_data_radix);
1147 int btrfs_read_block_groups(struct btrfs_root *root)
1149 struct btrfs_path *path;
1152 struct btrfs_block_group_item *bi;
1153 struct btrfs_block_group_cache *cache;
1154 struct btrfs_fs_info *info = root->fs_info;
1155 struct radix_tree_root *radix;
1156 struct btrfs_key key;
1157 struct btrfs_key found_key;
1158 struct btrfs_leaf *leaf;
1159 u64 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE / root->blocksize;
1163 root = info->extent_root;
1165 key.offset = group_size_blocks;
1167 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1169 path = btrfs_alloc_path();
1174 ret = btrfs_search_slot(NULL, info->extent_root,
1180 leaf = btrfs_buffer_leaf(path->nodes[0]);
1181 btrfs_disk_key_to_cpu(&found_key,
1182 &leaf->items[path->slots[0]].key);
1183 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1190 radix = &info->block_group_data_radix;
1192 radix = &info->block_group_radix;
1194 bi = btrfs_item_ptr(leaf, path->slots[0],
1195 struct btrfs_block_group_item);
1196 memcpy(&cache->item, bi, sizeof(*bi));
1197 memcpy(&cache->key, &found_key, sizeof(found_key));
1198 cache->last_alloc = cache->key.objectid;
1199 cache->first_free = cache->key.objectid;
1201 cache->data = (nr & 1);
1202 cache->radix = radix;
1204 key.objectid = found_key.objectid + found_key.offset;
1205 btrfs_release_path(root, path);
1206 ret = radix_tree_insert(radix, found_key.objectid +
1207 found_key.offset - 1,
1210 used = btrfs_block_group_used(bi);
1211 if (used < (key.offset * 8) / 10) {
1212 radix_tree_tag_set(radix, found_key.objectid +
1213 found_key.offset - 1,
1214 BTRFS_BLOCK_GROUP_AVAIL);
1217 btrfs_super_total_blocks(info->disk_super))
1222 btrfs_free_path(path);