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,
9 u64 search_end, u64 hint_block,
10 struct btrfs_key *ins, int data);
11 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
12 btrfs_root *extent_root);
13 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
14 btrfs_root *extent_root);
16 static void reada_extent_leaves(struct btrfs_root *root,
17 struct btrfs_path *path, u64 limit)
19 struct btrfs_node *node;
29 node = btrfs_buffer_node(path->nodes[1]);
30 slot = path->slots[1] + 1;
31 nritems = btrfs_header_nritems(&node->header);
32 for (i = slot; i < nritems && i < slot + 8; i++) {
33 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
34 if (item_objectid > limit)
36 blocknr = btrfs_node_blockptr(node, i);
37 ret = readahead_tree_block(root, blocknr);
43 static int cache_block_group(struct btrfs_root *root,
44 struct btrfs_block_group_cache *block_group)
46 struct btrfs_path *path;
49 struct btrfs_leaf *leaf;
50 struct radix_tree_root *extent_radix;
58 root = root->fs_info->extent_root;
59 extent_radix = &root->fs_info->extent_map_radix;
61 if (block_group->cached)
63 if (block_group->data)
65 path = btrfs_alloc_path();
68 key.objectid = block_group->key.objectid;
71 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
72 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
75 if (ret && path->slots[0] > 0)
77 limit = block_group->key.objectid + block_group->key.offset;
78 reada_extent_leaves(root, path, limit);
80 leaf = btrfs_buffer_leaf(path->nodes[0]);
81 slot = path->slots[0];
82 if (slot >= btrfs_header_nritems(&leaf->header)) {
83 reada_extent_leaves(root, path, limit);
84 ret = btrfs_next_leaf(root, path);
89 hole_size = block_group->key.objectid +
90 block_group->key.offset - last;
92 last = block_group->key.objectid;
93 hole_size = block_group->key.offset;
95 for (i = 0; i < hole_size; i++) {
96 set_radix_bit(extent_radix,
102 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
103 if (key.objectid >= block_group->key.objectid +
104 block_group->key.offset) {
106 hole_size = block_group->key.objectid +
107 block_group->key.offset - last;
109 last = block_group->key.objectid;
110 hole_size = block_group->key.offset;
112 for (i = 0; i < hole_size; i++) {
113 set_radix_bit(extent_radix, last + i);
117 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
119 last = key.objectid + key.offset;
122 hole_size = key.objectid - last;
123 for (i = 0; i < hole_size; i++) {
124 set_radix_bit(extent_radix, last + i);
126 last = key.objectid + key.offset;
132 block_group->cached = 1;
133 btrfs_free_path(path);
137 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
141 struct btrfs_block_group_cache *block_group;
144 ret = radix_tree_gang_lookup(&info->block_group_radix,
145 (void **)&block_group,
148 if (block_group->key.objectid <= blocknr && blocknr <=
149 block_group->key.objectid + block_group->key.offset)
152 ret = radix_tree_gang_lookup(&info->block_group_data_radix,
153 (void **)&block_group,
156 if (block_group->key.objectid <= blocknr && blocknr <=
157 block_group->key.objectid + block_group->key.offset)
163 static u64 leaf_range(struct btrfs_root *root)
165 u64 size = BTRFS_LEAF_DATA_SIZE(root);
166 do_div(size, sizeof(struct btrfs_extent_item) +
167 sizeof(struct btrfs_item));
171 static u64 find_search_start(struct btrfs_root *root,
172 struct btrfs_block_group_cache **cache_ret,
173 u64 search_start, int num)
175 unsigned long gang[8];
177 struct btrfs_block_group_cache *cache = *cache_ret;
178 u64 last = max(search_start, cache->key.objectid);
183 last = max(last, cache->last_prealloc);
186 cache_block_group(root, cache);
188 ret = find_first_radix_bit(&root->fs_info->extent_map_radix,
189 gang, last, ARRAY_SIZE(gang));
192 last = gang[ret-1] + 1;
194 if (ret != ARRAY_SIZE(gang)) {
197 if (gang[ret-1] - gang[0] > leaf_range(root)) {
201 if (gang[0] >= cache->key.objectid + cache->key.offset) {
207 return max(cache->last_alloc, search_start);
210 cache = btrfs_lookup_block_group(root->fs_info,
211 last + cache->key.offset - 1);
213 return max((*cache_ret)->last_alloc, search_start);
215 cache = btrfs_find_block_group(root, cache,
216 last + cache->key.offset - 1, 0, 0);
221 static u64 div_factor(u64 num, int factor)
228 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
229 struct btrfs_block_group_cache
230 *hint, u64 search_start,
233 struct btrfs_block_group_cache *cache[8];
234 struct btrfs_block_group_cache *found_group = NULL;
235 struct btrfs_fs_info *info = root->fs_info;
236 struct radix_tree_root *radix;
237 struct radix_tree_root *swap_radix;
251 radix = &info->block_group_data_radix;
252 swap_radix = &info->block_group_radix;
254 radix = &info->block_group_radix;
255 swap_radix = &info->block_group_data_radix;
259 struct btrfs_block_group_cache *shint;
260 shint = btrfs_lookup_block_group(info, search_start);
261 if (shint->data == data) {
262 used = btrfs_block_group_used(&shint->item);
263 if (used + shint->pinned <
264 div_factor(shint->key.offset, factor)) {
269 if (hint && hint->data == data) {
270 used = btrfs_block_group_used(&hint->item);
271 if (used + hint->pinned <
272 div_factor(hint->key.offset, factor)) {
275 if (used >= div_factor(hint->key.offset, 8)) {
276 radix_tree_tag_clear(radix,
278 hint->key.offset - 1,
279 BTRFS_BLOCK_GROUP_AVAIL);
281 last = hint->key.offset * 3;
282 if (hint->key.objectid >= last)
283 last = max(search_start + hint->key.offset - 1,
284 hint->key.objectid - last);
286 last = hint->key.objectid + hint->key.offset;
290 hint_last = max(hint->key.objectid, search_start);
292 hint_last = search_start;
297 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
298 last, ARRAY_SIZE(cache),
299 BTRFS_BLOCK_GROUP_AVAIL);
302 for (i = 0; i < ret; i++) {
303 last = cache[i]->key.objectid +
304 cache[i]->key.offset;
305 used = btrfs_block_group_used(&cache[i]->item);
306 if (used + cache[i]->pinned <
307 div_factor(cache[i]->key.offset, factor)) {
308 found_group = cache[i];
311 if (used >= div_factor(cache[i]->key.offset, 8)) {
312 radix_tree_tag_clear(radix,
313 cache[i]->key.objectid +
314 cache[i]->key.offset - 1,
315 BTRFS_BLOCK_GROUP_AVAIL);
323 ret = radix_tree_gang_lookup(radix, (void **)cache,
324 last, ARRAY_SIZE(cache));
327 for (i = 0; i < ret; i++) {
328 last = cache[i]->key.objectid +
329 cache[i]->key.offset;
330 used = btrfs_block_group_used(&cache[i]->item);
331 if (used + cache[i]->pinned < cache[i]->key.offset) {
332 found_group = cache[i];
335 if (used >= cache[i]->key.offset) {
336 radix_tree_tag_clear(radix,
337 cache[i]->key.objectid +
338 cache[i]->key.offset - 1,
339 BTRFS_BLOCK_GROUP_AVAIL);
350 struct radix_tree_root *tmp = radix;
358 ret = radix_tree_gang_lookup(radix,
359 (void **)&found_group, 0, 1);
361 ret = radix_tree_gang_lookup(swap_radix,
362 (void **)&found_group,
371 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
372 struct btrfs_root *root,
373 u64 blocknr, u64 num_blocks)
375 struct btrfs_path *path;
377 struct btrfs_key key;
378 struct btrfs_leaf *l;
379 struct btrfs_extent_item *item;
380 struct btrfs_key ins;
383 find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1, 0,
385 path = btrfs_alloc_path();
387 btrfs_init_path(path);
388 key.objectid = blocknr;
390 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
391 key.offset = num_blocks;
392 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
398 l = btrfs_buffer_leaf(path->nodes[0]);
399 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
400 refs = btrfs_extent_refs(item);
401 btrfs_set_extent_refs(item, refs + 1);
402 btrfs_mark_buffer_dirty(path->nodes[0]);
404 btrfs_release_path(root->fs_info->extent_root, path);
405 btrfs_free_path(path);
406 finish_current_insert(trans, root->fs_info->extent_root);
407 del_pending_extents(trans, root->fs_info->extent_root);
411 static int lookup_extent_ref(struct btrfs_trans_handle *trans,
412 struct btrfs_root *root, u64 blocknr,
413 u64 num_blocks, u32 *refs)
415 struct btrfs_path *path;
417 struct btrfs_key key;
418 struct btrfs_leaf *l;
419 struct btrfs_extent_item *item;
421 path = btrfs_alloc_path();
422 btrfs_init_path(path);
423 key.objectid = blocknr;
424 key.offset = num_blocks;
426 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
427 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
431 l = btrfs_buffer_leaf(path->nodes[0]);
432 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
433 *refs = btrfs_extent_refs(item);
434 btrfs_release_path(root->fs_info->extent_root, path);
435 btrfs_free_path(path);
439 int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
440 struct btrfs_root *root)
442 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
445 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
446 struct buffer_head *buf)
449 struct btrfs_node *buf_node;
450 struct btrfs_leaf *buf_leaf;
451 struct btrfs_disk_key *key;
452 struct btrfs_file_extent_item *fi;
459 buf_node = btrfs_buffer_node(buf);
460 leaf = btrfs_is_leaf(buf_node);
461 buf_leaf = btrfs_buffer_leaf(buf);
462 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
465 key = &buf_leaf->items[i].key;
466 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
468 fi = btrfs_item_ptr(buf_leaf, i,
469 struct btrfs_file_extent_item);
470 if (btrfs_file_extent_type(fi) ==
471 BTRFS_FILE_EXTENT_INLINE)
473 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
474 if (disk_blocknr == 0)
476 ret = btrfs_inc_extent_ref(trans, root, disk_blocknr,
477 btrfs_file_extent_disk_num_blocks(fi));
480 blocknr = btrfs_node_blockptr(buf_node, i);
481 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
488 static int write_one_cache_group(struct btrfs_trans_handle *trans,
489 struct btrfs_root *root,
490 struct btrfs_path *path,
491 struct btrfs_block_group_cache *cache)
495 struct btrfs_root *extent_root = root->fs_info->extent_root;
496 struct btrfs_block_group_item *bi;
497 struct btrfs_key ins;
499 find_free_extent(trans, extent_root, 0, 0, (u64)-1, 0, &ins, 0);
500 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
502 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
503 struct btrfs_block_group_item);
504 memcpy(bi, &cache->item, sizeof(*bi));
505 mark_buffer_dirty(path->nodes[0]);
506 btrfs_release_path(extent_root, path);
508 finish_current_insert(trans, extent_root);
509 pending_ret = del_pending_extents(trans, extent_root);
515 cache->last_alloc = cache->first_free;
520 static int write_dirty_block_radix(struct btrfs_trans_handle *trans,
521 struct btrfs_root *root,
522 struct radix_tree_root *radix)
524 struct btrfs_block_group_cache *cache[8];
529 struct btrfs_path *path;
531 path = btrfs_alloc_path();
536 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
537 0, ARRAY_SIZE(cache),
538 BTRFS_BLOCK_GROUP_DIRTY);
541 for (i = 0; i < ret; i++) {
542 radix_tree_tag_clear(radix, cache[i]->key.objectid +
543 cache[i]->key.offset - 1,
544 BTRFS_BLOCK_GROUP_DIRTY);
545 err = write_one_cache_group(trans, root,
551 btrfs_free_path(path);
555 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
556 struct btrfs_root *root)
560 ret = write_dirty_block_radix(trans, root,
561 &root->fs_info->block_group_radix);
562 ret2 = write_dirty_block_radix(trans, root,
563 &root->fs_info->block_group_data_radix);
571 static int update_block_group(struct btrfs_trans_handle *trans,
572 struct btrfs_root *root,
573 u64 blocknr, u64 num, int alloc, int mark_free,
576 struct btrfs_block_group_cache *cache;
577 struct btrfs_fs_info *info = root->fs_info;
585 cache = btrfs_lookup_block_group(info, blocknr);
589 block_in_group = blocknr - cache->key.objectid;
590 WARN_ON(block_in_group > cache->key.offset);
591 radix_tree_tag_set(cache->radix, cache->key.objectid +
592 cache->key.offset - 1,
593 BTRFS_BLOCK_GROUP_DIRTY);
595 old_val = btrfs_block_group_used(&cache->item);
596 num = min(total, cache->key.offset - block_in_group);
598 if (blocknr > cache->last_alloc)
599 cache->last_alloc = blocknr;
601 for (i = 0; i < num; i++) {
602 clear_radix_bit(&info->extent_map_radix,
606 if (cache->data != data &&
607 old_val < (cache->key.offset >> 1)) {
609 radix_tree_delete(cache->radix,
610 cache->key.objectid +
611 cache->key.offset - 1);
615 &info->block_group_data_radix;
617 BTRFS_BLOCK_GROUP_DATA;
619 cache->radix = &info->block_group_radix;
621 ~BTRFS_BLOCK_GROUP_DATA;
623 ret = radix_tree_insert(cache->radix,
624 cache->key.objectid +
625 cache->key.offset - 1,
631 if (blocknr < cache->first_free)
632 cache->first_free = blocknr;
633 if (!cache->data && mark_free) {
634 for (i = 0; i < num; i++) {
635 set_radix_bit(&info->extent_map_radix,
639 if (old_val < (cache->key.offset >> 1) &&
640 old_val + num >= (cache->key.offset >> 1)) {
641 radix_tree_tag_set(cache->radix,
642 cache->key.objectid +
643 cache->key.offset - 1,
644 BTRFS_BLOCK_GROUP_AVAIL);
647 btrfs_set_block_group_used(&cache->item, old_val);
654 static int try_remove_page(struct address_space *mapping, unsigned long index)
657 ret = invalidate_mapping_pages(mapping, index, index);
661 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
664 unsigned long gang[8];
665 struct inode *btree_inode = root->fs_info->btree_inode;
666 struct btrfs_block_group_cache *block_group;
670 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
671 struct radix_tree_root *extent_radix = &root->fs_info->extent_map_radix;
674 ret = find_first_radix_bit(pinned_radix, gang, 0,
680 for (i = 0; i < ret; i++) {
681 clear_radix_bit(pinned_radix, gang[i]);
682 block_group = btrfs_lookup_block_group(root->fs_info,
685 WARN_ON(block_group->pinned == 0);
686 block_group->pinned--;
687 if (gang[i] < block_group->last_alloc)
688 block_group->last_alloc = gang[i];
689 if (gang[i] < block_group->last_prealloc)
690 block_group->last_prealloc = gang[i];
691 if (!block_group->data)
692 set_radix_bit(extent_radix, gang[i]);
694 try_remove_page(btree_inode->i_mapping,
695 gang[i] << (PAGE_CACHE_SHIFT -
696 btree_inode->i_blkbits));
702 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
703 btrfs_root *extent_root)
705 struct btrfs_key ins;
706 struct btrfs_extent_item extent_item;
709 u64 super_blocks_used;
710 struct btrfs_fs_info *info = extent_root->fs_info;
712 btrfs_set_extent_refs(&extent_item, 1);
715 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
716 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
718 for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
719 ins.objectid = extent_root->fs_info->extent_tree_insert[i];
720 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
721 btrfs_set_super_blocks_used(info->disk_super,
722 super_blocks_used + 1);
723 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
724 sizeof(extent_item));
727 extent_root->fs_info->extent_tree_insert_nr = 0;
728 extent_root->fs_info->extent_tree_prealloc_nr = 0;
732 static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
735 struct btrfs_header *header;
736 struct buffer_head *bh;
739 bh = btrfs_find_tree_block(root, blocknr);
741 if (buffer_uptodate(bh)) {
743 root->fs_info->running_transaction->transid;
744 header = btrfs_buffer_header(bh);
745 if (btrfs_header_generation(header) ==
747 btrfs_block_release(root, bh);
751 btrfs_block_release(root, bh);
753 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
755 struct btrfs_block_group_cache *cache;
756 cache = btrfs_lookup_block_group(root->fs_info,
762 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
769 * remove an extent from the root, returns 0 on success
771 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
772 *root, u64 blocknr, u64 num_blocks, int pin,
775 struct btrfs_path *path;
776 struct btrfs_key key;
777 struct btrfs_fs_info *info = root->fs_info;
778 struct btrfs_root *extent_root = info->extent_root;
780 struct btrfs_extent_item *ei;
781 struct btrfs_key ins;
784 key.objectid = blocknr;
786 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
787 key.offset = num_blocks;
789 find_free_extent(trans, root, 0, 0, (u64)-1, 0, &ins, 0);
790 path = btrfs_alloc_path();
792 btrfs_init_path(path);
794 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
798 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
799 struct btrfs_extent_item);
800 BUG_ON(ei->refs == 0);
801 refs = btrfs_extent_refs(ei) - 1;
802 btrfs_set_extent_refs(ei, refs);
803 btrfs_mark_buffer_dirty(path->nodes[0]);
805 u64 super_blocks_used;
808 ret = pin_down_block(root, blocknr, 0);
812 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
813 btrfs_set_super_blocks_used(info->disk_super,
814 super_blocks_used - num_blocks);
815 ret = btrfs_del_item(trans, extent_root, path);
818 ret = update_block_group(trans, root, blocknr, num_blocks, 0,
822 btrfs_free_path(path);
823 finish_current_insert(trans, extent_root);
828 * find all the blocks marked as pending in the radix tree and remove
829 * them from the extent map
831 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
832 btrfs_root *extent_root)
837 unsigned long gang[4];
839 struct radix_tree_root *pending_radix;
840 struct radix_tree_root *pinned_radix;
841 struct btrfs_block_group_cache *cache;
843 pending_radix = &extent_root->fs_info->pending_del_radix;
844 pinned_radix = &extent_root->fs_info->pinned_radix;
847 ret = find_first_radix_bit(pending_radix, gang, 0,
851 for (i = 0; i < ret; i++) {
852 wret = set_radix_bit(pinned_radix, gang[i]);
855 btrfs_lookup_block_group(extent_root->fs_info,
861 printk(KERN_CRIT "set_radix_bit, err %d\n",
865 wret = clear_radix_bit(pending_radix, gang[i]);
867 wret = __free_extent(trans, extent_root,
877 * remove an extent from the root, returns 0 on success
879 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
880 *root, u64 blocknr, u64 num_blocks, int pin)
882 struct btrfs_root *extent_root = root->fs_info->extent_root;
886 if (root == extent_root) {
887 pin_down_block(root, blocknr, 1);
890 ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
891 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
892 return ret ? ret : pending_ret;
896 * walks the btree of allocated extents and find a hole of a given size.
897 * The key ins is changed to record the hole:
898 * ins->objectid == block start
899 * ins->flags = BTRFS_EXTENT_ITEM_KEY
900 * ins->offset == number of blocks
901 * Any available blocks before search_start are skipped.
903 static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
904 *orig_root, u64 num_blocks, u64 search_start, u64
905 search_end, u64 hint_block,
906 struct btrfs_key *ins, int data)
908 struct btrfs_path *path;
909 struct btrfs_key key;
915 u64 orig_search_start = search_start;
917 struct btrfs_leaf *l;
918 struct btrfs_root * root = orig_root->fs_info->extent_root;
919 struct btrfs_fs_info *info = root->fs_info;
920 int total_needed = num_blocks;
922 int fill_prealloc = 0;
924 struct btrfs_block_group_cache *block_group;
929 path = btrfs_alloc_path();
931 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
933 level = btrfs_header_level(btrfs_buffer_header(root->node));
934 if (num_blocks == 0) {
937 total_needed = (min(level + 1, BTRFS_MAX_LEVEL) + 2) * 3;
939 if (search_end == (u64)-1)
940 search_end = btrfs_super_total_blocks(info->disk_super);
942 block_group = btrfs_lookup_block_group(info, hint_block);
943 block_group = btrfs_find_block_group(root, block_group,
944 hint_block, data, 1);
946 block_group = btrfs_find_block_group(root,
947 trans->block_group, 0,
952 if (!block_group->data)
953 search_start = find_search_start(root, &block_group,
954 search_start, total_needed);
956 search_start = max(block_group->last_alloc, search_start);
958 btrfs_init_path(path);
959 ins->objectid = search_start;
963 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
967 if (path->slots[0] > 0) {
971 l = btrfs_buffer_leaf(path->nodes[0]);
972 btrfs_disk_key_to_cpu(&key, &l->items[path->slots[0]].key);
974 * a rare case, go back one key if we hit a block group item
975 * instead of an extent item
977 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
978 key.objectid + key.offset >= search_start) {
979 ins->objectid = key.objectid;
980 ins->offset = key.offset - 1;
981 btrfs_release_path(root, path);
982 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
986 if (path->slots[0] > 0) {
992 l = btrfs_buffer_leaf(path->nodes[0]);
993 slot = path->slots[0];
994 if (slot >= btrfs_header_nritems(&l->header)) {
996 info->extent_tree_prealloc_nr = 0;
1000 limit = last_block +
1001 (block_group->key.offset >> 1);
1003 limit = search_start +
1004 (block_group->key.offset >> 1);
1005 ret = btrfs_next_leaf(root, path);
1011 ins->objectid = search_start;
1012 ins->offset = search_end - search_start;
1016 ins->objectid = last_block > search_start ?
1017 last_block : search_start;
1018 ins->offset = search_end - ins->objectid;
1022 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
1023 if (key.objectid >= search_start && key.objectid > last_block &&
1025 if (last_block < search_start)
1026 last_block = search_start;
1027 hole_size = key.objectid - last_block;
1028 if (hole_size >= num_blocks) {
1029 ins->objectid = last_block;
1030 ins->offset = hole_size;
1035 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
1039 last_block = key.objectid + key.offset;
1040 if (!full_scan && last_block >= block_group->key.objectid +
1041 block_group->key.offset) {
1042 btrfs_release_path(root, path);
1043 search_start = block_group->key.objectid +
1044 block_group->key.offset * 2;
1053 /* we have to make sure we didn't find an extent that has already
1054 * been allocated by the map tree or the original allocation
1056 btrfs_release_path(root, path);
1057 BUG_ON(ins->objectid < search_start);
1059 if (ins->objectid + num_blocks >= search_end) {
1064 search_start = orig_search_start;
1071 for (test_block = ins->objectid;
1072 test_block < ins->objectid + num_blocks; test_block++) {
1073 if (test_radix_bit(&info->pinned_radix, test_block)) {
1074 search_start = test_block + 1;
1078 if (!fill_prealloc && info->extent_tree_insert_nr) {
1080 info->extent_tree_insert[info->extent_tree_insert_nr - 1];
1081 if (ins->objectid + num_blocks >
1082 info->extent_tree_insert[0] &&
1083 ins->objectid <= last) {
1084 search_start = last + 1;
1085 WARN_ON(!full_scan);
1089 if (!fill_prealloc && info->extent_tree_prealloc_nr) {
1091 info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
1092 if (ins->objectid + num_blocks > first &&
1093 ins->objectid <= info->extent_tree_prealloc[0]) {
1094 search_start = info->extent_tree_prealloc[0] + 1;
1095 WARN_ON(!full_scan);
1099 if (fill_prealloc) {
1101 test_block = ins->objectid;
1102 if (test_block - info->extent_tree_prealloc[total_needed - 1] >=
1105 info->extent_tree_prealloc_nr = total_found;
1107 while(test_block < ins->objectid + ins->offset &&
1108 total_found < total_needed) {
1109 nr = total_needed - total_found - 1;
1111 info->extent_tree_prealloc[nr] = test_block;
1115 if (total_found < total_needed) {
1116 search_start = test_block;
1119 info->extent_tree_prealloc_nr = total_found;
1122 block_group = btrfs_lookup_block_group(info, ins->objectid);
1125 block_group->last_prealloc =
1126 info->extent_tree_prealloc[total_needed-1];
1128 trans->block_group = block_group;
1131 ins->offset = num_blocks;
1132 btrfs_free_path(path);
1136 if (search_start + num_blocks >= search_end) {
1137 search_start = orig_search_start;
1147 block_group = btrfs_lookup_block_group(info, search_start);
1150 block_group = btrfs_find_block_group(root, block_group,
1151 search_start, data, 0);
1155 btrfs_release_path(root, path);
1156 btrfs_free_path(path);
1160 * finds a free extent and does all the dirty work required for allocation
1161 * returns the key for the extent through ins, and a tree buffer for
1162 * the first block of the extent through buf.
1164 * returns 0 if everything worked, non-zero otherwise.
1166 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1167 struct btrfs_root *root, u64 owner,
1168 u64 num_blocks, u64 hint_block,
1169 u64 search_end, struct btrfs_key *ins, int data)
1173 u64 super_blocks_used;
1174 u64 search_start = 0;
1175 struct btrfs_fs_info *info = root->fs_info;
1176 struct btrfs_root *extent_root = info->extent_root;
1177 struct btrfs_extent_item extent_item;
1178 struct btrfs_key prealloc_key;
1180 btrfs_set_extent_refs(&extent_item, 1);
1181 btrfs_set_extent_owner(&extent_item, owner);
1183 if (root == extent_root) {
1185 BUG_ON(info->extent_tree_prealloc_nr == 0);
1186 BUG_ON(num_blocks != 1);
1188 info->extent_tree_prealloc_nr--;
1189 nr = info->extent_tree_prealloc_nr;
1190 ins->objectid = info->extent_tree_prealloc[nr];
1191 info->extent_tree_insert[info->extent_tree_insert_nr++] =
1193 ret = update_block_group(trans, root,
1194 ins->objectid, ins->offset, 1, 0, 0);
1200 * if we're doing a data allocation, preallocate room in the
1201 * extent tree first. This way the extent tree blocks end up
1202 * in the correct block group.
1205 ret = find_free_extent(trans, root, 0, 0,
1206 search_end, 0, &prealloc_key, 0);
1210 if (prealloc_key.objectid + prealloc_key.offset >= search_end) {
1211 int nr = info->extent_tree_prealloc_nr;
1212 search_end = info->extent_tree_prealloc[nr - 1] - 1;
1214 search_start = info->extent_tree_prealloc[0] + 1;
1217 if (hint_block < search_start)
1218 hint_block = search_start;
1219 /* do the real allocation */
1220 ret = find_free_extent(trans, root, num_blocks, search_start,
1221 search_end, hint_block, ins, data);
1227 * if we're doing a metadata allocation, preallocate space in the
1228 * extent tree second. This way, we don't create a tiny hole
1229 * in the allocation map between any unused preallocation blocks
1230 * and the metadata block we're actually allocating. On disk,
1232 * [block we've allocated], [used prealloc 1], [ unused prealloc ]
1233 * The unused prealloc will get reused the next time around.
1236 if (ins->objectid + ins->offset >= search_end)
1237 search_end = ins->objectid - 1;
1239 search_start = ins->objectid + ins->offset;
1241 if (hint_block < search_start)
1242 hint_block = search_start;
1244 ret = find_free_extent(trans, root, 0, search_start,
1245 search_end, hint_block,
1252 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
1253 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
1255 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1256 sizeof(extent_item));
1258 finish_current_insert(trans, extent_root);
1259 pending_ret = del_pending_extents(trans, extent_root);
1266 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1273 * helper function to allocate a block for a given tree
1274 * returns the tree buffer or NULL.
1276 struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1277 struct btrfs_root *root, u64 hint)
1279 struct btrfs_key ins;
1281 struct buffer_head *buf;
1283 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
1284 1, hint, (unsigned long)-1, &ins, 0);
1290 buf = btrfs_find_create_tree_block(root, ins.objectid);
1291 set_buffer_uptodate(buf);
1292 set_buffer_checked(buf);
1293 set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
1297 static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1298 struct btrfs_root *root, struct buffer_head *cur)
1300 struct btrfs_disk_key *key;
1301 struct btrfs_leaf *leaf;
1302 struct btrfs_file_extent_item *fi;
1307 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
1308 leaf = btrfs_buffer_leaf(cur);
1309 nritems = btrfs_header_nritems(&leaf->header);
1310 for (i = 0; i < nritems; i++) {
1312 key = &leaf->items[i].key;
1313 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
1315 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1316 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
1319 * FIXME make sure to insert a trans record that
1320 * repeats the snapshot del on crash
1322 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
1323 if (disk_blocknr == 0)
1325 ret = btrfs_free_extent(trans, root, disk_blocknr,
1326 btrfs_file_extent_disk_num_blocks(fi),
1334 * helper function for drop_snapshot, this walks down the tree dropping ref
1335 * counts as it goes.
1337 static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1338 *root, struct btrfs_path *path, int *level)
1340 struct buffer_head *next;
1341 struct buffer_head *cur;
1346 WARN_ON(*level < 0);
1347 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1348 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
1354 * walk down to the last node level and free all the leaves
1356 while(*level >= 0) {
1357 WARN_ON(*level < 0);
1358 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1359 cur = path->nodes[*level];
1360 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
1362 if (path->slots[*level] >=
1363 btrfs_header_nritems(btrfs_buffer_header(cur)))
1366 ret = drop_leaf_ref(trans, root, cur);
1370 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
1371 path->slots[*level]);
1372 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
1375 path->slots[*level]++;
1376 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
1380 next = read_tree_block(root, blocknr);
1381 WARN_ON(*level <= 0);
1382 if (path->nodes[*level-1])
1383 btrfs_block_release(root, path->nodes[*level-1]);
1384 path->nodes[*level-1] = next;
1385 *level = btrfs_header_level(btrfs_buffer_header(next));
1386 path->slots[*level] = 0;
1389 WARN_ON(*level < 0);
1390 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1391 ret = btrfs_free_extent(trans, root,
1392 bh_blocknr(path->nodes[*level]), 1, 1);
1393 btrfs_block_release(root, path->nodes[*level]);
1394 path->nodes[*level] = NULL;
1401 * helper for dropping snapshots. This walks back up the tree in the path
1402 * to find the first node higher up where we haven't yet gone through
1405 static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1406 *root, struct btrfs_path *path, int *level)
1411 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1412 slot = path->slots[i];
1413 if (slot < btrfs_header_nritems(
1414 btrfs_buffer_header(path->nodes[i])) - 1) {
1419 ret = btrfs_free_extent(trans, root,
1420 bh_blocknr(path->nodes[*level]),
1423 btrfs_block_release(root, path->nodes[*level]);
1424 path->nodes[*level] = NULL;
1432 * drop the reference count on the tree rooted at 'snap'. This traverses
1433 * the tree freeing any blocks that have a ref count of zero after being
1436 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
1437 *root, struct buffer_head *snap)
1442 struct btrfs_path *path;
1446 path = btrfs_alloc_path();
1448 btrfs_init_path(path);
1450 level = btrfs_header_level(btrfs_buffer_header(snap));
1452 path->nodes[level] = snap;
1453 path->slots[level] = 0;
1455 wret = walk_down_tree(trans, root, path, &level);
1461 wret = walk_up_tree(trans, root, path, &level);
1466 btrfs_btree_balance_dirty(root);
1468 for (i = 0; i <= orig_level; i++) {
1469 if (path->nodes[i]) {
1470 btrfs_block_release(root, path->nodes[i]);
1473 btrfs_free_path(path);
1477 static int free_block_group_radix(struct radix_tree_root *radix)
1480 struct btrfs_block_group_cache *cache[8];
1484 ret = radix_tree_gang_lookup(radix, (void **)cache, 0,
1488 for (i = 0; i < ret; i++) {
1489 radix_tree_delete(radix, cache[i]->key.objectid +
1490 cache[i]->key.offset - 1);
1497 int btrfs_free_block_groups(struct btrfs_fs_info *info)
1501 unsigned long gang[16];
1504 ret = free_block_group_radix(&info->block_group_radix);
1505 ret2 = free_block_group_radix(&info->block_group_data_radix);
1512 ret = find_first_radix_bit(&info->extent_map_radix,
1513 gang, 0, ARRAY_SIZE(gang));
1516 for (i = 0; i < ret; i++) {
1517 clear_radix_bit(&info->extent_map_radix, gang[i]);
1523 int btrfs_read_block_groups(struct btrfs_root *root)
1525 struct btrfs_path *path;
1528 struct btrfs_block_group_item *bi;
1529 struct btrfs_block_group_cache *cache;
1530 struct btrfs_fs_info *info = root->fs_info;
1531 struct radix_tree_root *radix;
1532 struct btrfs_key key;
1533 struct btrfs_key found_key;
1534 struct btrfs_leaf *leaf;
1535 u64 group_size_blocks;
1538 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE >>
1539 root->fs_info->sb->s_blocksize_bits;
1540 root = info->extent_root;
1542 key.offset = group_size_blocks;
1544 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1546 path = btrfs_alloc_path();
1551 ret = btrfs_search_slot(NULL, info->extent_root,
1557 leaf = btrfs_buffer_leaf(path->nodes[0]);
1558 btrfs_disk_key_to_cpu(&found_key,
1559 &leaf->items[path->slots[0]].key);
1560 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1566 bi = btrfs_item_ptr(leaf, path->slots[0],
1567 struct btrfs_block_group_item);
1568 if (bi->flags & BTRFS_BLOCK_GROUP_DATA) {
1569 radix = &info->block_group_data_radix;
1572 radix = &info->block_group_radix;
1576 memcpy(&cache->item, bi, sizeof(*bi));
1577 memcpy(&cache->key, &found_key, sizeof(found_key));
1578 cache->last_alloc = cache->key.objectid;
1579 cache->first_free = cache->key.objectid;
1580 cache->last_prealloc = cache->key.objectid;
1584 cache->radix = radix;
1586 key.objectid = found_key.objectid + found_key.offset;
1587 btrfs_release_path(root, path);
1588 ret = radix_tree_insert(radix, found_key.objectid +
1589 found_key.offset - 1,
1592 used = btrfs_block_group_used(bi);
1593 if (used < div_factor(key.offset, 8)) {
1594 radix_tree_tag_set(radix, found_key.objectid +
1595 found_key.offset - 1,
1596 BTRFS_BLOCK_GROUP_AVAIL);
1599 btrfs_super_total_blocks(info->disk_super))
1603 btrfs_free_path(path);