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 int cache_block_group(struct btrfs_root *root,
16 struct btrfs_block_group_cache *block_group)
18 struct btrfs_path *path;
21 struct btrfs_leaf *leaf;
22 struct radix_tree_root *extent_radix;
29 root = root->fs_info->extent_root;
30 extent_radix = &root->fs_info->extent_map_radix;
32 if (block_group->cached)
34 if (block_group->data)
36 path = btrfs_alloc_path();
39 printk("cache block group %Lu\n", block_group->key.objectid);
40 key.objectid = block_group->key.objectid;
43 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
44 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
47 if (ret && path->slots[0] > 0)
50 leaf = btrfs_buffer_leaf(path->nodes[0]);
51 slot = path->slots[0];
52 if (slot >= btrfs_header_nritems(&leaf->header)) {
53 ret = btrfs_next_leaf(root, path);
58 hole_size = block_group->key.objectid +
59 block_group->key.offset - last;
61 last = block_group->key.objectid;
62 hole_size = block_group->key.offset;
64 for (i = 0; i < hole_size; i++) {
65 set_radix_bit(extent_radix,
71 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
72 if (key.objectid >= block_group->key.objectid +
73 block_group->key.offset) {
75 hole_size = block_group->key.objectid +
76 block_group->key.offset - last;
78 last = block_group->key.objectid;
79 hole_size = block_group->key.offset;
81 for (i = 0; i < hole_size; i++) {
82 set_radix_bit(extent_radix, last + i);
86 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
88 last = key.objectid + key.offset;
91 hole_size = key.objectid - last;
92 for (i = 0; i < hole_size; i++) {
93 set_radix_bit(extent_radix, last + i);
95 last = key.objectid + key.offset;
101 block_group->cached = 1;
102 btrfs_free_path(path);
106 static struct btrfs_block_group_cache *lookup_block_group(struct
110 struct btrfs_block_group_cache *block_group;
113 ret = radix_tree_gang_lookup(&info->block_group_radix,
114 (void **)&block_group,
117 if (block_group->key.objectid <= blocknr && blocknr <=
118 block_group->key.objectid + block_group->key.offset)
121 ret = radix_tree_gang_lookup(&info->block_group_data_radix,
122 (void **)&block_group,
125 if (block_group->key.objectid <= blocknr && blocknr <=
126 block_group->key.objectid + block_group->key.offset)
130 printk("lookup_block_group fails for blocknr %Lu\n", blocknr);
131 printk("last ret was %d\n", ret);
133 printk("last block group was %Lu %Lu\n", block_group->key.objectid, block_group->key.offset);
138 static u64 leaf_range(struct btrfs_root *root)
140 u64 size = BTRFS_LEAF_DATA_SIZE(root);
141 size = size / (sizeof(struct btrfs_extent_item) +
142 sizeof(struct btrfs_item));
146 static u64 find_search_start(struct btrfs_root *root,
147 struct btrfs_block_group_cache **cache_ret,
148 u64 search_start, int num)
150 unsigned long gang[8];
152 struct btrfs_block_group_cache *cache = *cache_ret;
153 u64 last = max(search_start, cache->key.objectid);
158 last = max(last, cache->last_prealloc);
161 cache_block_group(root, cache);
163 ret = find_first_radix_bit(&root->fs_info->extent_map_radix,
164 gang, last, ARRAY_SIZE(gang));
167 last = gang[ret-1] + 1;
169 if (ret != ARRAY_SIZE(gang)) {
172 if (gang[ret-1] - gang[0] > leaf_range(root)) {
176 if (gang[0] >= cache->key.objectid + cache->key.offset) {
182 return max(cache->last_alloc, search_start);
185 cache = lookup_block_group(root->fs_info, last + cache->key.offset - 1);
187 return max((*cache_ret)->last_alloc, search_start);
189 cache = btrfs_find_block_group(root, cache,
190 last + cache->key.offset - 1, 0);
195 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
196 struct btrfs_block_group_cache
197 *hint, u64 search_start,
200 struct btrfs_block_group_cache *cache[8];
201 struct btrfs_block_group_cache *found_group = NULL;
202 struct btrfs_fs_info *info = root->fs_info;
203 struct radix_tree_root *radix;
212 radix = &info->block_group_data_radix;
214 radix = &info->block_group_radix;
217 struct btrfs_block_group_cache *shint;
218 shint = lookup_block_group(info, search_start);
219 if (shint->data == data) {
220 used = btrfs_block_group_used(&shint->item);
221 if (used + shint->pinned <
222 (shint->key.offset * 8) / 10) {
227 if (hint && hint->data == data) {
228 used = btrfs_block_group_used(&hint->item);
229 if (used + hint->pinned < (hint->key.offset * 8) / 10) {
232 if (used >= (hint->key.offset * 8) / 10) {
233 radix_tree_tag_clear(radix,
235 hint->key.offset - 1,
236 BTRFS_BLOCK_GROUP_AVAIL);
238 last = hint->key.offset * 3;
239 if (hint->key.objectid >= last)
240 last = max(search_start + hint->key.offset - 1,
241 hint->key.objectid - last);
243 last = hint->key.objectid + hint->key.offset;
247 hint_last = max(hint->key.objectid, search_start);
249 hint_last = search_start;
254 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
255 last, ARRAY_SIZE(cache),
256 BTRFS_BLOCK_GROUP_AVAIL);
259 for (i = 0; i < ret; i++) {
260 last = cache[i]->key.objectid +
261 cache[i]->key.offset;
262 used = btrfs_block_group_used(&cache[i]->item);
263 if (used + cache[i]->pinned <
264 (cache[i]->key.offset * 8) / 10) {
265 found_group = cache[i];
268 if (used >= (cache[i]->key.offset * 8) / 10) {
269 radix_tree_tag_clear(radix,
270 cache[i]->key.objectid +
271 cache[i]->key.offset - 1,
272 BTRFS_BLOCK_GROUP_AVAIL);
279 ret = radix_tree_gang_lookup(radix, (void **)cache,
280 last, ARRAY_SIZE(cache));
283 for (i = 0; i < ret; i++) {
284 last = cache[i]->key.objectid +
285 cache[i]->key.offset;
286 used = btrfs_block_group_used(&cache[i]->item);
287 if (used + cache[i]->pinned < cache[i]->key.offset) {
288 found_group = cache[i];
291 if (used >= cache[i]->key.offset) {
292 radix_tree_tag_clear(radix,
293 cache[i]->key.objectid +
294 cache[i]->key.offset - 1,
295 BTRFS_BLOCK_GROUP_AVAIL);
305 ret = radix_tree_gang_lookup(radix,
306 (void **)&found_group, 0, 1);
313 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
314 struct btrfs_root *root,
315 u64 blocknr, u64 num_blocks)
317 struct btrfs_path *path;
319 struct btrfs_key key;
320 struct btrfs_leaf *l;
321 struct btrfs_extent_item *item;
322 struct btrfs_key ins;
325 find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1,
327 path = btrfs_alloc_path();
329 btrfs_init_path(path);
330 key.objectid = blocknr;
332 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
333 key.offset = num_blocks;
334 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
337 printk("can't find block %Lu %Lu\n", blocknr, num_blocks);
341 l = btrfs_buffer_leaf(path->nodes[0]);
342 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
343 refs = btrfs_extent_refs(item);
344 btrfs_set_extent_refs(item, refs + 1);
345 btrfs_mark_buffer_dirty(path->nodes[0]);
347 btrfs_release_path(root->fs_info->extent_root, path);
348 btrfs_free_path(path);
349 finish_current_insert(trans, root->fs_info->extent_root);
350 del_pending_extents(trans, root->fs_info->extent_root);
354 static int lookup_extent_ref(struct btrfs_trans_handle *trans,
355 struct btrfs_root *root, u64 blocknr,
356 u64 num_blocks, u32 *refs)
358 struct btrfs_path *path;
360 struct btrfs_key key;
361 struct btrfs_leaf *l;
362 struct btrfs_extent_item *item;
364 path = btrfs_alloc_path();
365 btrfs_init_path(path);
366 key.objectid = blocknr;
367 key.offset = num_blocks;
369 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
370 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
374 l = btrfs_buffer_leaf(path->nodes[0]);
375 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
376 *refs = btrfs_extent_refs(item);
377 btrfs_release_path(root->fs_info->extent_root, path);
378 btrfs_free_path(path);
382 int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
383 struct btrfs_root *root)
385 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
388 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
389 struct buffer_head *buf)
392 struct btrfs_node *buf_node;
393 struct btrfs_leaf *buf_leaf;
394 struct btrfs_disk_key *key;
395 struct btrfs_file_extent_item *fi;
402 buf_node = btrfs_buffer_node(buf);
403 leaf = btrfs_is_leaf(buf_node);
404 buf_leaf = btrfs_buffer_leaf(buf);
405 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
407 key = &buf_leaf->items[i].key;
408 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
410 fi = btrfs_item_ptr(buf_leaf, i,
411 struct btrfs_file_extent_item);
412 if (btrfs_file_extent_type(fi) ==
413 BTRFS_FILE_EXTENT_INLINE)
415 ret = btrfs_inc_extent_ref(trans, root,
416 btrfs_file_extent_disk_blocknr(fi),
417 btrfs_file_extent_disk_num_blocks(fi));
420 blocknr = btrfs_node_blockptr(buf_node, i);
421 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
428 static int write_one_cache_group(struct btrfs_trans_handle *trans,
429 struct btrfs_root *root,
430 struct btrfs_path *path,
431 struct btrfs_block_group_cache *cache)
435 struct btrfs_root *extent_root = root->fs_info->extent_root;
436 struct btrfs_block_group_item *bi;
437 struct btrfs_key ins;
439 find_free_extent(trans, extent_root, 0, 0, (u64)-1, &ins, 0);
440 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
442 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
443 struct btrfs_block_group_item);
444 memcpy(bi, &cache->item, sizeof(*bi));
445 mark_buffer_dirty(path->nodes[0]);
446 btrfs_release_path(extent_root, path);
448 finish_current_insert(trans, extent_root);
449 pending_ret = del_pending_extents(trans, extent_root);
455 cache->last_alloc = cache->first_free;
460 static int write_dirty_block_radix(struct btrfs_trans_handle *trans,
461 struct btrfs_root *root,
462 struct radix_tree_root *radix)
464 struct btrfs_block_group_cache *cache[8];
469 struct btrfs_path *path;
471 path = btrfs_alloc_path();
476 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
477 0, ARRAY_SIZE(cache),
478 BTRFS_BLOCK_GROUP_DIRTY);
481 for (i = 0; i < ret; i++) {
482 radix_tree_tag_clear(radix, cache[i]->key.objectid +
483 cache[i]->key.offset - 1,
484 BTRFS_BLOCK_GROUP_DIRTY);
485 err = write_one_cache_group(trans, root,
491 btrfs_free_path(path);
495 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
496 struct btrfs_root *root)
500 ret = write_dirty_block_radix(trans, root,
501 &root->fs_info->block_group_radix);
502 ret2 = write_dirty_block_radix(trans, root,
503 &root->fs_info->block_group_data_radix);
511 static int update_block_group(struct btrfs_trans_handle *trans,
512 struct btrfs_root *root,
513 u64 blocknr, u64 num, int alloc, int mark_free)
515 struct btrfs_block_group_cache *cache;
516 struct btrfs_fs_info *info = root->fs_info;
523 cache = lookup_block_group(info, blocknr);
525 printk(KERN_CRIT "blocknr %Lu lookup failed\n",
529 block_in_group = blocknr - cache->key.objectid;
530 WARN_ON(block_in_group > cache->key.offset);
531 radix_tree_tag_set(cache->radix, cache->key.objectid +
532 cache->key.offset - 1,
533 BTRFS_BLOCK_GROUP_DIRTY);
535 old_val = btrfs_block_group_used(&cache->item);
536 num = min(total, cache->key.offset - block_in_group);
539 if (blocknr > cache->last_alloc)
540 cache->last_alloc = blocknr;
542 for (i = 0; i < num; i++) {
543 clear_radix_bit(&info->extent_map_radix,
549 if (blocknr < cache->first_free)
550 cache->first_free = blocknr;
551 if (!cache->data && mark_free) {
552 for (i = 0; i < num; i++) {
553 set_radix_bit(&info->extent_map_radix,
557 if (old_val < (cache->key.offset * 6) / 10 &&
558 old_val + num >= (cache->key.offset * 6) / 10) {
559 printk("group %Lu now available\n", cache->key.objectid);
560 radix_tree_tag_set(cache->radix,
561 cache->key.objectid +
562 cache->key.offset - 1,
563 BTRFS_BLOCK_GROUP_AVAIL);
566 btrfs_set_block_group_used(&cache->item, old_val);
573 static int try_remove_page(struct address_space *mapping, unsigned long index)
576 ret = invalidate_mapping_pages(mapping, index, index);
580 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
583 unsigned long gang[8];
584 struct inode *btree_inode = root->fs_info->btree_inode;
585 struct btrfs_block_group_cache *block_group;
589 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
590 struct radix_tree_root *extent_radix = &root->fs_info->extent_map_radix;
593 ret = find_first_radix_bit(pinned_radix, gang, 0,
599 for (i = 0; i < ret; i++) {
600 clear_radix_bit(pinned_radix, gang[i]);
601 block_group = lookup_block_group(root->fs_info,
604 WARN_ON(block_group->pinned == 0);
605 block_group->pinned--;
606 if (gang[i] < block_group->last_alloc)
607 block_group->last_alloc = gang[i];
608 if (gang[i] < block_group->last_prealloc)
609 block_group->last_prealloc = gang[i];
610 if (!block_group->data)
611 set_radix_bit(extent_radix, gang[i]);
613 try_remove_page(btree_inode->i_mapping,
614 gang[i] << (PAGE_CACHE_SHIFT -
615 btree_inode->i_blkbits));
621 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
622 btrfs_root *extent_root)
624 struct btrfs_key ins;
625 struct btrfs_extent_item extent_item;
628 u64 super_blocks_used;
629 struct btrfs_fs_info *info = extent_root->fs_info;
631 btrfs_set_extent_refs(&extent_item, 1);
634 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
635 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
637 for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
638 ins.objectid = extent_root->fs_info->extent_tree_insert[i];
639 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
640 btrfs_set_super_blocks_used(info->disk_super,
641 super_blocks_used + 1);
642 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
643 sizeof(extent_item));
646 extent_root->fs_info->extent_tree_insert_nr = 0;
647 extent_root->fs_info->extent_tree_prealloc_nr = 0;
651 static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
654 struct btrfs_header *header;
655 struct buffer_head *bh;
658 bh = btrfs_find_tree_block(root, blocknr);
660 if (buffer_uptodate(bh)) {
662 root->fs_info->running_transaction->transid;
663 header = btrfs_buffer_header(bh);
664 if (btrfs_header_generation(header) ==
666 btrfs_block_release(root, bh);
670 btrfs_block_release(root, bh);
672 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
674 struct btrfs_block_group_cache *cache;
675 cache = lookup_block_group(root->fs_info, blocknr);
680 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
687 * remove an extent from the root, returns 0 on success
689 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
690 *root, u64 blocknr, u64 num_blocks, int pin,
693 struct btrfs_path *path;
694 struct btrfs_key key;
695 struct btrfs_fs_info *info = root->fs_info;
696 struct btrfs_root *extent_root = info->extent_root;
698 struct btrfs_extent_item *ei;
699 struct btrfs_key ins;
702 key.objectid = blocknr;
704 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
705 key.offset = num_blocks;
707 find_free_extent(trans, root, 0, 0, (u64)-1, &ins, 0);
708 path = btrfs_alloc_path();
710 btrfs_init_path(path);
712 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
714 printk("failed to find %Lu\n", key.objectid);
715 btrfs_print_tree(extent_root, extent_root->node);
716 printk("failed to find %Lu\n", key.objectid);
719 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
720 struct btrfs_extent_item);
721 BUG_ON(ei->refs == 0);
722 refs = btrfs_extent_refs(ei) - 1;
723 btrfs_set_extent_refs(ei, refs);
724 btrfs_mark_buffer_dirty(path->nodes[0]);
726 u64 super_blocks_used;
729 ret = pin_down_block(root, blocknr, 0);
733 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
734 btrfs_set_super_blocks_used(info->disk_super,
735 super_blocks_used - num_blocks);
736 ret = btrfs_del_item(trans, extent_root, path);
739 ret = update_block_group(trans, root, blocknr, num_blocks, 0,
743 btrfs_free_path(path);
744 finish_current_insert(trans, extent_root);
749 * find all the blocks marked as pending in the radix tree and remove
750 * them from the extent map
752 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
753 btrfs_root *extent_root)
758 unsigned long gang[4];
760 struct radix_tree_root *pending_radix;
761 struct radix_tree_root *pinned_radix;
762 struct btrfs_block_group_cache *cache;
764 pending_radix = &extent_root->fs_info->pending_del_radix;
765 pinned_radix = &extent_root->fs_info->pinned_radix;
768 ret = find_first_radix_bit(pending_radix, gang, 0,
772 for (i = 0; i < ret; i++) {
773 wret = set_radix_bit(pinned_radix, gang[i]);
775 cache = lookup_block_group(extent_root->fs_info,
781 printk(KERN_CRIT "set_radix_bit, err %d\n",
785 wret = clear_radix_bit(pending_radix, gang[i]);
787 wret = __free_extent(trans, extent_root,
797 * remove an extent from the root, returns 0 on success
799 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
800 *root, u64 blocknr, u64 num_blocks, int pin)
802 struct btrfs_root *extent_root = root->fs_info->extent_root;
806 if (root == extent_root) {
807 pin_down_block(root, blocknr, 1);
810 ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
811 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
812 return ret ? ret : pending_ret;
816 * walks the btree of allocated extents and find a hole of a given size.
817 * The key ins is changed to record the hole:
818 * ins->objectid == block start
819 * ins->flags = BTRFS_EXTENT_ITEM_KEY
820 * ins->offset == number of blocks
821 * Any available blocks before search_start are skipped.
823 static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
824 *orig_root, u64 num_blocks, u64 search_start, u64
825 search_end, struct btrfs_key *ins, int data)
827 struct btrfs_path *path;
828 struct btrfs_key key;
834 u64 orig_search_start = search_start;
836 struct btrfs_leaf *l;
837 struct btrfs_root * root = orig_root->fs_info->extent_root;
838 struct btrfs_fs_info *info = root->fs_info;
839 int total_needed = num_blocks;
841 int fill_prealloc = 0;
843 struct btrfs_block_group_cache *block_group;
846 path = btrfs_alloc_path();
848 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
850 level = btrfs_header_level(btrfs_buffer_header(root->node));
851 if (num_blocks == 0) {
854 total_needed = (min(level + 1, BTRFS_MAX_LEVEL) + 2) * 3;
856 if (search_end == (u64)-1)
857 search_end = btrfs_super_total_blocks(info->disk_super);
859 block_group = lookup_block_group(info, search_start);
860 block_group = btrfs_find_block_group(root, block_group,
863 block_group = btrfs_find_block_group(root,
864 trans->block_group, 0,
869 if (!full_scan && block_group->data != data)
873 search_start = find_search_start(root, &block_group,
874 search_start, total_needed);
876 search_start = max(block_group->last_alloc, search_start);
878 btrfs_init_path(path);
879 ins->objectid = search_start;
883 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
887 if (path->slots[0] > 0) {
891 l = btrfs_buffer_leaf(path->nodes[0]);
892 btrfs_disk_key_to_cpu(&key, &l->items[path->slots[0]].key);
894 * a rare case, go back one key if we hit a block group item
895 * instead of an extent item
897 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
898 key.objectid + key.offset >= search_start) {
899 ins->objectid = key.objectid;
900 ins->offset = key.offset - 1;
901 btrfs_release_path(root, path);
902 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
906 if (path->slots[0] > 0) {
912 l = btrfs_buffer_leaf(path->nodes[0]);
913 slot = path->slots[0];
914 if (slot >= btrfs_header_nritems(&l->header)) {
916 info->extent_tree_prealloc_nr = 0;
919 ret = btrfs_next_leaf(root, path);
925 ins->objectid = search_start;
926 ins->offset = search_end - search_start;
930 ins->objectid = last_block > search_start ?
931 last_block : search_start;
932 ins->offset = search_end - ins->objectid;
936 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
937 if (key.objectid >= search_start && key.objectid > last_block &&
939 if (last_block < search_start)
940 last_block = search_start;
941 hole_size = key.objectid - last_block;
942 if (hole_size >= num_blocks) {
943 ins->objectid = last_block;
944 ins->offset = hole_size;
949 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
953 last_block = key.objectid + key.offset;
954 if (last_block >= block_group->key.objectid +
955 block_group->key.offset) {
956 btrfs_release_path(root, path);
957 search_start = block_group->key.objectid +
958 block_group->key.offset * 2;
966 /* we have to make sure we didn't find an extent that has already
967 * been allocated by the map tree or the original allocation
969 btrfs_release_path(root, path);
970 BUG_ON(ins->objectid < search_start);
972 if (ins->objectid + num_blocks >= search_end) {
975 search_start = orig_search_start;
979 for (test_block = ins->objectid;
980 test_block < ins->objectid + num_blocks; test_block++) {
981 if (test_radix_bit(&info->pinned_radix, test_block)) {
982 search_start = test_block + 1;
986 if (!fill_prealloc && info->extent_tree_insert_nr) {
988 info->extent_tree_insert[info->extent_tree_insert_nr - 1];
989 if (ins->objectid + num_blocks >
990 info->extent_tree_insert[0] &&
991 ins->objectid <= last) {
992 search_start = last + 1;
997 if (!fill_prealloc && info->extent_tree_prealloc_nr) {
999 info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
1000 if (ins->objectid + num_blocks > first &&
1001 ins->objectid <= info->extent_tree_prealloc[0]) {
1002 search_start = info->extent_tree_prealloc[0] + 1;
1003 WARN_ON(!full_scan);
1007 if (fill_prealloc) {
1009 test_block = ins->objectid;
1010 if (test_block - info->extent_tree_prealloc[total_needed - 1] >=
1013 info->extent_tree_prealloc_nr = total_found;
1015 while(test_block < ins->objectid + ins->offset &&
1016 total_found < total_needed) {
1017 nr = total_needed - total_found - 1;
1019 info->extent_tree_prealloc[nr] = test_block;
1023 if (total_found < total_needed) {
1024 search_start = test_block;
1027 info->extent_tree_prealloc_nr = total_found;
1030 block_group = lookup_block_group(info, ins->objectid);
1033 block_group->last_prealloc =
1034 info->extent_tree_prealloc[total_needed-1];
1036 trans->block_group = block_group;
1039 ins->offset = num_blocks;
1040 btrfs_free_path(path);
1044 if (search_start + num_blocks >= search_end) {
1045 search_start = orig_search_start;
1046 printk("doing full scan!\n");
1049 block_group = lookup_block_group(info, search_start);
1051 block_group = btrfs_find_block_group(root, block_group,
1052 search_start, data);
1056 btrfs_release_path(root, path);
1057 btrfs_free_path(path);
1061 * finds a free extent and does all the dirty work required for allocation
1062 * returns the key for the extent through ins, and a tree buffer for
1063 * the first block of the extent through buf.
1065 * returns 0 if everything worked, non-zero otherwise.
1067 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1068 struct btrfs_root *root, u64 owner,
1069 u64 num_blocks, u64 search_start,
1070 u64 search_end, struct btrfs_key *ins, int data)
1074 u64 super_blocks_used;
1075 struct btrfs_fs_info *info = root->fs_info;
1076 struct btrfs_root *extent_root = info->extent_root;
1077 struct btrfs_extent_item extent_item;
1078 struct btrfs_key prealloc_key;
1080 btrfs_set_extent_refs(&extent_item, 1);
1081 btrfs_set_extent_owner(&extent_item, owner);
1083 if (root == extent_root) {
1085 BUG_ON(info->extent_tree_prealloc_nr == 0);
1086 BUG_ON(num_blocks != 1);
1088 info->extent_tree_prealloc_nr--;
1089 nr = info->extent_tree_prealloc_nr;
1090 ins->objectid = info->extent_tree_prealloc[nr];
1091 info->extent_tree_insert[info->extent_tree_insert_nr++] =
1093 ret = update_block_group(trans, root,
1094 ins->objectid, ins->offset, 1, 0);
1100 * if we're doing a data allocation, preallocate room in the
1101 * extent tree first. This way the extent tree blocks end up
1102 * in the correct block group.
1105 ret = find_free_extent(trans, root, 0, search_start,
1106 search_end, &prealloc_key, 0);
1110 if (prealloc_key.objectid + prealloc_key.offset >= search_end) {
1111 int nr = info->extent_tree_prealloc_nr;
1112 search_end = info->extent_tree_prealloc[nr - 1] - 1;
1114 search_start = info->extent_tree_prealloc[0] + 1;
1117 /* do the real allocation */
1118 ret = find_free_extent(trans, root, num_blocks, search_start,
1119 search_end, ins, data);
1125 * if we're doing a metadata allocation, preallocate space in the
1126 * extent tree second. This way, we don't create a tiny hole
1127 * in the allocation map between any unused preallocation blocks
1128 * and the metadata block we're actually allocating. On disk,
1130 * [block we've allocated], [used prealloc 1], [ unused prealloc ]
1131 * The unused prealloc will get reused the next time around.
1134 if (ins->objectid + ins->offset >= search_end)
1135 search_end = ins->objectid - 1;
1137 search_start = ins->objectid + ins->offset;
1139 ret = find_free_extent(trans, root, 0, search_start,
1140 search_end, &prealloc_key, 0);
1146 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
1147 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
1149 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1150 sizeof(extent_item));
1152 finish_current_insert(trans, extent_root);
1153 pending_ret = del_pending_extents(trans, extent_root);
1160 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
1165 * helper function to allocate a block for a given tree
1166 * returns the tree buffer or NULL.
1168 struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1169 struct btrfs_root *root, u64 hint)
1171 struct btrfs_key ins;
1173 struct buffer_head *buf;
1175 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
1176 1, 0, (unsigned long)-1, &ins, 0);
1182 buf = btrfs_find_create_tree_block(root, ins.objectid);
1183 set_buffer_uptodate(buf);
1184 set_buffer_checked(buf);
1185 set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
1189 static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1190 struct btrfs_root *root, struct buffer_head *cur)
1192 struct btrfs_disk_key *key;
1193 struct btrfs_leaf *leaf;
1194 struct btrfs_file_extent_item *fi;
1199 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
1200 leaf = btrfs_buffer_leaf(cur);
1201 nritems = btrfs_header_nritems(&leaf->header);
1202 for (i = 0; i < nritems; i++) {
1203 key = &leaf->items[i].key;
1204 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
1206 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1207 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
1210 * FIXME make sure to insert a trans record that
1211 * repeats the snapshot del on crash
1213 ret = btrfs_free_extent(trans, root,
1214 btrfs_file_extent_disk_blocknr(fi),
1215 btrfs_file_extent_disk_num_blocks(fi),
1223 * helper function for drop_snapshot, this walks down the tree dropping ref
1224 * counts as it goes.
1226 static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1227 *root, struct btrfs_path *path, int *level)
1229 struct buffer_head *next;
1230 struct buffer_head *cur;
1235 WARN_ON(*level < 0);
1236 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1237 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
1243 * walk down to the last node level and free all the leaves
1245 while(*level >= 0) {
1246 WARN_ON(*level < 0);
1247 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1248 cur = path->nodes[*level];
1249 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
1251 if (path->slots[*level] >=
1252 btrfs_header_nritems(btrfs_buffer_header(cur)))
1255 ret = drop_leaf_ref(trans, root, cur);
1259 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
1260 path->slots[*level]);
1261 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
1264 path->slots[*level]++;
1265 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
1269 next = read_tree_block(root, blocknr);
1270 WARN_ON(*level <= 0);
1271 if (path->nodes[*level-1])
1272 btrfs_block_release(root, path->nodes[*level-1]);
1273 path->nodes[*level-1] = next;
1274 *level = btrfs_header_level(btrfs_buffer_header(next));
1275 path->slots[*level] = 0;
1278 WARN_ON(*level < 0);
1279 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1280 ret = btrfs_free_extent(trans, root,
1281 bh_blocknr(path->nodes[*level]), 1, 1);
1282 btrfs_block_release(root, path->nodes[*level]);
1283 path->nodes[*level] = NULL;
1290 * helper for dropping snapshots. This walks back up the tree in the path
1291 * to find the first node higher up where we haven't yet gone through
1294 static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1295 *root, struct btrfs_path *path, int *level)
1300 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1301 slot = path->slots[i];
1302 if (slot < btrfs_header_nritems(
1303 btrfs_buffer_header(path->nodes[i])) - 1) {
1308 ret = btrfs_free_extent(trans, root,
1309 bh_blocknr(path->nodes[*level]),
1312 btrfs_block_release(root, path->nodes[*level]);
1313 path->nodes[*level] = NULL;
1321 * drop the reference count on the tree rooted at 'snap'. This traverses
1322 * the tree freeing any blocks that have a ref count of zero after being
1325 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
1326 *root, struct buffer_head *snap)
1331 struct btrfs_path *path;
1335 path = btrfs_alloc_path();
1337 btrfs_init_path(path);
1339 level = btrfs_header_level(btrfs_buffer_header(snap));
1341 path->nodes[level] = snap;
1342 path->slots[level] = 0;
1344 wret = walk_down_tree(trans, root, path, &level);
1350 wret = walk_up_tree(trans, root, path, &level);
1355 btrfs_btree_balance_dirty(root);
1357 for (i = 0; i <= orig_level; i++) {
1358 if (path->nodes[i]) {
1359 btrfs_block_release(root, path->nodes[i]);
1362 btrfs_free_path(path);
1366 static int free_block_group_radix(struct radix_tree_root *radix)
1369 struct btrfs_block_group_cache *cache[8];
1373 ret = radix_tree_gang_lookup(radix, (void **)cache, 0,
1377 for (i = 0; i < ret; i++) {
1378 radix_tree_delete(radix, cache[i]->key.objectid +
1379 cache[i]->key.offset - 1);
1386 int btrfs_free_block_groups(struct btrfs_fs_info *info)
1390 unsigned long gang[16];
1393 ret = free_block_group_radix(&info->block_group_radix);
1394 ret2 = free_block_group_radix(&info->block_group_data_radix);
1401 ret = find_first_radix_bit(&info->extent_map_radix,
1402 gang, 0, ARRAY_SIZE(gang));
1405 for (i = 0; i < ret; i++) {
1406 clear_radix_bit(&info->extent_map_radix, gang[i]);
1412 int btrfs_read_block_groups(struct btrfs_root *root)
1414 struct btrfs_path *path;
1417 struct btrfs_block_group_item *bi;
1418 struct btrfs_block_group_cache *cache;
1419 struct btrfs_fs_info *info = root->fs_info;
1420 struct radix_tree_root *radix;
1421 struct btrfs_key key;
1422 struct btrfs_key found_key;
1423 struct btrfs_leaf *leaf;
1424 u64 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE / root->blocksize;
1428 root = info->extent_root;
1430 key.offset = group_size_blocks;
1432 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1434 path = btrfs_alloc_path();
1439 ret = btrfs_search_slot(NULL, info->extent_root,
1445 leaf = btrfs_buffer_leaf(path->nodes[0]);
1446 btrfs_disk_key_to_cpu(&found_key,
1447 &leaf->items[path->slots[0]].key);
1448 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1455 radix = &info->block_group_data_radix;
1457 radix = &info->block_group_radix;
1459 bi = btrfs_item_ptr(leaf, path->slots[0],
1460 struct btrfs_block_group_item);
1461 memcpy(&cache->item, bi, sizeof(*bi));
1462 memcpy(&cache->key, &found_key, sizeof(found_key));
1463 cache->last_alloc = cache->key.objectid;
1464 cache->first_free = cache->key.objectid;
1465 cache->last_prealloc = cache->key.objectid;
1473 cache->radix = radix;
1475 key.objectid = found_key.objectid + found_key.offset;
1476 btrfs_release_path(root, path);
1477 ret = radix_tree_insert(radix, found_key.objectid +
1478 found_key.offset - 1,
1481 used = btrfs_block_group_used(bi);
1482 if (used < (key.offset * 8) / 10) {
1483 radix_tree_tag_set(radix, found_key.objectid +
1484 found_key.offset - 1,
1485 BTRFS_BLOCK_GROUP_AVAIL);
1488 btrfs_super_total_blocks(info->disk_super))
1493 btrfs_free_path(path);