2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/sched.h>
22 #include "print-tree.h"
23 #include "transaction.h"
25 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
26 btrfs_root *extent_root);
27 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
28 btrfs_root *extent_root);
30 static int cache_block_group(struct btrfs_root *root,
31 struct btrfs_block_group_cache *block_group)
33 struct btrfs_path *path;
36 struct btrfs_leaf *leaf;
37 struct radix_tree_root *extent_radix;
45 root = root->fs_info->extent_root;
46 extent_radix = &root->fs_info->extent_map_radix;
48 if (block_group->cached)
50 if (block_group->data)
52 path = btrfs_alloc_path();
56 key.objectid = block_group->key.objectid;
59 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
60 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
63 if (ret && path->slots[0] > 0)
65 limit = block_group->key.objectid + block_group->key.offset;
67 leaf = btrfs_buffer_leaf(path->nodes[0]);
68 slot = path->slots[0];
69 if (slot >= btrfs_header_nritems(&leaf->header)) {
70 ret = btrfs_next_leaf(root, path);
77 hole_size = block_group->key.objectid +
78 block_group->key.offset - last;
80 last = block_group->key.objectid;
81 hole_size = block_group->key.offset;
83 for (i = 0; i < hole_size; i++) {
84 set_radix_bit(extent_radix,
90 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
91 if (key.objectid >= block_group->key.objectid +
92 block_group->key.offset) {
94 hole_size = block_group->key.objectid +
95 block_group->key.offset - last;
97 last = block_group->key.objectid;
98 hole_size = block_group->key.offset;
100 for (i = 0; i < hole_size; i++) {
101 set_radix_bit(extent_radix, last + i);
105 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
107 last = key.objectid + key.offset;
110 hole_size = key.objectid - last;
111 for (i = 0; i < hole_size; i++) {
112 set_radix_bit(extent_radix, last + i);
114 last = key.objectid + key.offset;
120 block_group->cached = 1;
122 btrfs_free_path(path);
126 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
130 struct btrfs_block_group_cache *block_group;
133 ret = radix_tree_gang_lookup(&info->block_group_radix,
134 (void **)&block_group,
137 if (block_group->key.objectid <= blocknr && blocknr <=
138 block_group->key.objectid + block_group->key.offset)
141 ret = radix_tree_gang_lookup(&info->block_group_data_radix,
142 (void **)&block_group,
145 if (block_group->key.objectid <= blocknr && blocknr <=
146 block_group->key.objectid + block_group->key.offset)
152 static u64 leaf_range(struct btrfs_root *root)
154 u64 size = BTRFS_LEAF_DATA_SIZE(root);
155 do_div(size, sizeof(struct btrfs_extent_item) +
156 sizeof(struct btrfs_item));
160 static u64 find_search_start(struct btrfs_root *root,
161 struct btrfs_block_group_cache **cache_ret,
162 u64 search_start, int num)
164 unsigned long gang[8];
166 struct btrfs_block_group_cache *cache = *cache_ret;
167 u64 last = max(search_start, cache->key.objectid);
172 ret = cache_block_group(root, cache);
176 ret = find_first_radix_bit(&root->fs_info->extent_map_radix,
177 gang, last, ARRAY_SIZE(gang));
180 last = gang[ret-1] + 1;
182 if (ret != ARRAY_SIZE(gang)) {
185 if (gang[ret-1] - gang[0] > leaf_range(root)) {
189 if (gang[0] >= cache->key.objectid + cache->key.offset) {
195 return max(cache->last_alloc, search_start);
198 cache = btrfs_lookup_block_group(root->fs_info,
199 last + cache->key.offset - 1);
201 return max((*cache_ret)->last_alloc, search_start);
203 cache = btrfs_find_block_group(root, cache,
204 last + cache->key.offset - 1, 0, 0);
209 static u64 div_factor(u64 num, int factor)
216 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
217 struct btrfs_block_group_cache
218 *hint, u64 search_start,
221 struct btrfs_block_group_cache *cache[8];
222 struct btrfs_block_group_cache *found_group = NULL;
223 struct btrfs_fs_info *info = root->fs_info;
224 struct radix_tree_root *radix;
225 struct radix_tree_root *swap_radix;
239 radix = &info->block_group_data_radix;
240 swap_radix = &info->block_group_radix;
242 radix = &info->block_group_radix;
243 swap_radix = &info->block_group_data_radix;
247 struct btrfs_block_group_cache *shint;
248 shint = btrfs_lookup_block_group(info, search_start);
249 if (shint->data == data) {
250 used = btrfs_block_group_used(&shint->item);
251 if (used + shint->pinned <
252 div_factor(shint->key.offset, factor)) {
257 if (hint && hint->data == data) {
258 used = btrfs_block_group_used(&hint->item);
259 if (used + hint->pinned <
260 div_factor(hint->key.offset, factor)) {
263 if (used >= div_factor(hint->key.offset, 8)) {
264 radix_tree_tag_clear(radix,
266 hint->key.offset - 1,
267 BTRFS_BLOCK_GROUP_AVAIL);
269 last = hint->key.offset * 3;
270 if (hint->key.objectid >= last)
271 last = max(search_start + hint->key.offset - 1,
272 hint->key.objectid - last);
274 last = hint->key.objectid + hint->key.offset;
278 hint_last = max(hint->key.objectid, search_start);
280 hint_last = search_start;
285 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
286 last, ARRAY_SIZE(cache),
287 BTRFS_BLOCK_GROUP_AVAIL);
290 for (i = 0; i < ret; i++) {
291 last = cache[i]->key.objectid +
292 cache[i]->key.offset;
293 used = btrfs_block_group_used(&cache[i]->item);
294 if (used + cache[i]->pinned <
295 div_factor(cache[i]->key.offset, factor)) {
296 found_group = cache[i];
299 if (used >= div_factor(cache[i]->key.offset, 8)) {
300 radix_tree_tag_clear(radix,
301 cache[i]->key.objectid +
302 cache[i]->key.offset - 1,
303 BTRFS_BLOCK_GROUP_AVAIL);
311 ret = radix_tree_gang_lookup(radix, (void **)cache,
312 last, ARRAY_SIZE(cache));
315 for (i = 0; i < ret; i++) {
316 last = cache[i]->key.objectid +
317 cache[i]->key.offset;
318 used = btrfs_block_group_used(&cache[i]->item);
319 if (used + cache[i]->pinned < cache[i]->key.offset) {
320 found_group = cache[i];
323 if (used >= cache[i]->key.offset) {
324 radix_tree_tag_clear(radix,
325 cache[i]->key.objectid +
326 cache[i]->key.offset - 1,
327 BTRFS_BLOCK_GROUP_AVAIL);
338 struct radix_tree_root *tmp = radix;
346 ret = radix_tree_gang_lookup(radix,
347 (void **)&found_group, 0, 1);
349 ret = radix_tree_gang_lookup(swap_radix,
350 (void **)&found_group,
359 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
360 struct btrfs_root *root,
361 u64 blocknr, u64 num_blocks)
363 struct btrfs_path *path;
365 struct btrfs_key key;
366 struct btrfs_leaf *l;
367 struct btrfs_extent_item *item;
370 path = btrfs_alloc_path();
374 key.objectid = blocknr;
376 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
377 key.offset = num_blocks;
378 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
386 l = btrfs_buffer_leaf(path->nodes[0]);
387 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
388 refs = btrfs_extent_refs(item);
389 btrfs_set_extent_refs(item, refs + 1);
390 btrfs_mark_buffer_dirty(path->nodes[0]);
392 btrfs_release_path(root->fs_info->extent_root, path);
393 btrfs_free_path(path);
394 finish_current_insert(trans, root->fs_info->extent_root);
395 del_pending_extents(trans, root->fs_info->extent_root);
399 static int lookup_extent_ref(struct btrfs_trans_handle *trans,
400 struct btrfs_root *root, u64 blocknr,
401 u64 num_blocks, u32 *refs)
403 struct btrfs_path *path;
405 struct btrfs_key key;
406 struct btrfs_leaf *l;
407 struct btrfs_extent_item *item;
409 path = btrfs_alloc_path();
410 key.objectid = blocknr;
411 key.offset = num_blocks;
413 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
414 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
420 l = btrfs_buffer_leaf(path->nodes[0]);
421 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
422 *refs = btrfs_extent_refs(item);
424 btrfs_free_path(path);
428 int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
429 struct btrfs_root *root)
431 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
434 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
435 struct buffer_head *buf)
438 struct btrfs_node *buf_node;
439 struct btrfs_leaf *buf_leaf;
440 struct btrfs_disk_key *key;
441 struct btrfs_file_extent_item *fi;
450 buf_node = btrfs_buffer_node(buf);
451 leaf = btrfs_is_leaf(buf_node);
452 buf_leaf = btrfs_buffer_leaf(buf);
453 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
456 key = &buf_leaf->items[i].key;
457 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
459 fi = btrfs_item_ptr(buf_leaf, i,
460 struct btrfs_file_extent_item);
461 if (btrfs_file_extent_type(fi) ==
462 BTRFS_FILE_EXTENT_INLINE)
464 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
465 if (disk_blocknr == 0)
467 ret = btrfs_inc_extent_ref(trans, root, disk_blocknr,
468 btrfs_file_extent_disk_num_blocks(fi));
474 blocknr = btrfs_node_blockptr(buf_node, i);
475 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
485 for (i =0; i < faili; i++) {
488 key = &buf_leaf->items[i].key;
489 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
491 fi = btrfs_item_ptr(buf_leaf, i,
492 struct btrfs_file_extent_item);
493 if (btrfs_file_extent_type(fi) ==
494 BTRFS_FILE_EXTENT_INLINE)
496 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
497 if (disk_blocknr == 0)
499 err = btrfs_free_extent(trans, root, disk_blocknr,
500 btrfs_file_extent_disk_num_blocks(fi), 0);
503 blocknr = btrfs_node_blockptr(buf_node, i);
504 err = btrfs_free_extent(trans, root, blocknr, 1, 0);
511 static int write_one_cache_group(struct btrfs_trans_handle *trans,
512 struct btrfs_root *root,
513 struct btrfs_path *path,
514 struct btrfs_block_group_cache *cache)
518 struct btrfs_root *extent_root = root->fs_info->extent_root;
519 struct btrfs_block_group_item *bi;
521 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
525 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
526 struct btrfs_block_group_item);
527 memcpy(bi, &cache->item, sizeof(*bi));
528 btrfs_mark_buffer_dirty(path->nodes[0]);
529 btrfs_release_path(extent_root, path);
531 finish_current_insert(trans, extent_root);
532 pending_ret = del_pending_extents(trans, extent_root);
538 cache->last_alloc = cache->first_free;
543 static int write_dirty_block_radix(struct btrfs_trans_handle *trans,
544 struct btrfs_root *root,
545 struct radix_tree_root *radix)
547 struct btrfs_block_group_cache *cache[8];
552 struct btrfs_path *path;
553 unsigned long off = 0;
555 path = btrfs_alloc_path();
560 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
561 off, ARRAY_SIZE(cache),
562 BTRFS_BLOCK_GROUP_DIRTY);
565 for (i = 0; i < ret; i++) {
566 err = write_one_cache_group(trans, root,
569 * if we fail to write the cache group, we want
570 * to keep it marked dirty in hopes that a later
575 off = cache[i]->key.objectid +
576 cache[i]->key.offset;
580 radix_tree_tag_clear(radix, cache[i]->key.objectid +
581 cache[i]->key.offset - 1,
582 BTRFS_BLOCK_GROUP_DIRTY);
585 btrfs_free_path(path);
589 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
590 struct btrfs_root *root)
594 ret = write_dirty_block_radix(trans, root,
595 &root->fs_info->block_group_radix);
596 ret2 = write_dirty_block_radix(trans, root,
597 &root->fs_info->block_group_data_radix);
605 static int update_block_group(struct btrfs_trans_handle *trans,
606 struct btrfs_root *root,
607 u64 blocknr, u64 num, int alloc, int mark_free,
610 struct btrfs_block_group_cache *cache;
611 struct btrfs_fs_info *info = root->fs_info;
619 cache = btrfs_lookup_block_group(info, blocknr);
623 block_in_group = blocknr - cache->key.objectid;
624 WARN_ON(block_in_group > cache->key.offset);
625 radix_tree_tag_set(cache->radix, cache->key.objectid +
626 cache->key.offset - 1,
627 BTRFS_BLOCK_GROUP_DIRTY);
629 old_val = btrfs_block_group_used(&cache->item);
630 num = min(total, cache->key.offset - block_in_group);
632 if (blocknr > cache->last_alloc)
633 cache->last_alloc = blocknr;
635 for (i = 0; i < num; i++) {
636 clear_radix_bit(&info->extent_map_radix,
640 if (cache->data != data &&
641 old_val < (cache->key.offset >> 1)) {
643 radix_tree_delete(cache->radix,
644 cache->key.objectid +
645 cache->key.offset - 1);
649 &info->block_group_data_radix;
651 BTRFS_BLOCK_GROUP_DATA;
653 cache->radix = &info->block_group_radix;
655 ~BTRFS_BLOCK_GROUP_DATA;
657 ret = radix_tree_insert(cache->radix,
658 cache->key.objectid +
659 cache->key.offset - 1,
665 if (blocknr < cache->first_free)
666 cache->first_free = blocknr;
667 if (!cache->data && mark_free) {
668 for (i = 0; i < num; i++) {
669 set_radix_bit(&info->extent_map_radix,
673 if (old_val < (cache->key.offset >> 1) &&
674 old_val + num >= (cache->key.offset >> 1)) {
675 radix_tree_tag_set(cache->radix,
676 cache->key.objectid +
677 cache->key.offset - 1,
678 BTRFS_BLOCK_GROUP_AVAIL);
681 btrfs_set_block_group_used(&cache->item, old_val);
688 int btrfs_copy_pinned(struct btrfs_root *root, struct radix_tree_root *copy)
690 unsigned long gang[8];
692 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
697 ret = find_first_radix_bit(pinned_radix, gang, last,
701 for (i = 0 ; i < ret; i++) {
702 set_radix_bit(copy, gang[i]);
706 ret = find_first_radix_bit(&root->fs_info->extent_ins_radix, gang, 0,
712 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
713 struct btrfs_root *root,
714 struct radix_tree_root *unpin_radix)
716 unsigned long gang[8];
717 struct btrfs_block_group_cache *block_group;
721 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
722 struct radix_tree_root *extent_radix = &root->fs_info->extent_map_radix;
725 ret = find_first_radix_bit(unpin_radix, gang, 0,
731 for (i = 0; i < ret; i++) {
732 clear_radix_bit(pinned_radix, gang[i]);
733 clear_radix_bit(unpin_radix, gang[i]);
734 block_group = btrfs_lookup_block_group(root->fs_info,
737 WARN_ON(block_group->pinned == 0);
738 block_group->pinned--;
739 if (gang[i] < block_group->last_alloc)
740 block_group->last_alloc = gang[i];
741 if (!block_group->data)
742 set_radix_bit(extent_radix, gang[i]);
749 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
750 btrfs_root *extent_root)
752 struct btrfs_key ins;
753 struct btrfs_extent_item extent_item;
757 unsigned long gang[8];
758 struct btrfs_fs_info *info = extent_root->fs_info;
760 btrfs_set_extent_refs(&extent_item, 1);
763 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
764 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
767 ret = find_first_radix_bit(&info->extent_ins_radix, gang, 0,
772 for (i = 0; i < ret; i++) {
773 ins.objectid = gang[i];
774 err = btrfs_insert_item(trans, extent_root, &ins,
776 sizeof(extent_item));
777 clear_radix_bit(&info->extent_ins_radix, gang[i]);
784 static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
787 struct btrfs_header *header;
788 struct buffer_head *bh;
791 bh = btrfs_find_tree_block(root, blocknr);
793 if (buffer_uptodate(bh)) {
795 root->fs_info->running_transaction->transid;
796 header = btrfs_buffer_header(bh);
797 if (btrfs_header_generation(header) ==
799 btrfs_block_release(root, bh);
803 btrfs_block_release(root, bh);
805 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
807 struct btrfs_block_group_cache *cache;
808 cache = btrfs_lookup_block_group(root->fs_info,
814 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
821 * remove an extent from the root, returns 0 on success
823 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
824 *root, u64 blocknr, u64 num_blocks, int pin,
827 struct btrfs_path *path;
828 struct btrfs_key key;
829 struct btrfs_fs_info *info = root->fs_info;
830 struct btrfs_root *extent_root = info->extent_root;
832 struct btrfs_extent_item *ei;
835 key.objectid = blocknr;
837 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
838 key.offset = num_blocks;
840 path = btrfs_alloc_path();
844 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
848 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
849 struct btrfs_extent_item);
850 BUG_ON(ei->refs == 0);
851 refs = btrfs_extent_refs(ei) - 1;
852 btrfs_set_extent_refs(ei, refs);
853 btrfs_mark_buffer_dirty(path->nodes[0]);
855 u64 super_blocks_used;
858 ret = pin_down_block(root, blocknr, 0);
862 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
863 btrfs_set_super_blocks_used(&info->super_copy,
864 super_blocks_used - num_blocks);
865 ret = btrfs_del_item(trans, extent_root, path);
869 ret = update_block_group(trans, root, blocknr, num_blocks, 0,
873 btrfs_free_path(path);
874 finish_current_insert(trans, extent_root);
879 * find all the blocks marked as pending in the radix tree and remove
880 * them from the extent map
882 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
883 btrfs_root *extent_root)
888 unsigned long gang[4];
890 struct radix_tree_root *pending_radix;
891 struct radix_tree_root *pinned_radix;
892 struct btrfs_block_group_cache *cache;
894 pending_radix = &extent_root->fs_info->pending_del_radix;
895 pinned_radix = &extent_root->fs_info->pinned_radix;
898 ret = find_first_radix_bit(pending_radix, gang, 0,
902 for (i = 0; i < ret; i++) {
903 wret = set_radix_bit(pinned_radix, gang[i]);
906 btrfs_lookup_block_group(extent_root->fs_info,
912 printk(KERN_CRIT "set_radix_bit, err %d\n",
916 wret = clear_radix_bit(pending_radix, gang[i]);
918 wret = __free_extent(trans, extent_root,
928 * remove an extent from the root, returns 0 on success
930 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
931 *root, u64 blocknr, u64 num_blocks, int pin)
933 struct btrfs_root *extent_root = root->fs_info->extent_root;
937 if (root == extent_root) {
938 pin_down_block(root, blocknr, 1);
941 ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
942 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
943 return ret ? ret : pending_ret;
947 * walks the btree of allocated extents and find a hole of a given size.
948 * The key ins is changed to record the hole:
949 * ins->objectid == block start
950 * ins->flags = BTRFS_EXTENT_ITEM_KEY
951 * ins->offset == number of blocks
952 * Any available blocks before search_start are skipped.
954 static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
955 *orig_root, u64 num_blocks, u64 empty_size,
956 u64 search_start, u64 search_end, u64 hint_block,
957 struct btrfs_key *ins, u64 exclude_start,
958 u64 exclude_nr, int data)
960 struct btrfs_path *path;
961 struct btrfs_key key;
967 u64 orig_search_start = search_start;
969 struct btrfs_leaf *l;
970 struct btrfs_root * root = orig_root->fs_info->extent_root;
971 struct btrfs_fs_info *info = root->fs_info;
972 int total_needed = num_blocks;
974 struct btrfs_block_group_cache *block_group;
979 WARN_ON(num_blocks < 1);
981 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
983 level = btrfs_header_level(btrfs_buffer_header(root->node));
984 if (search_end == (u64)-1)
985 search_end = btrfs_super_total_blocks(&info->super_copy);
987 block_group = btrfs_lookup_block_group(info, hint_block);
988 block_group = btrfs_find_block_group(root, block_group,
989 hint_block, data, 1);
991 block_group = btrfs_find_block_group(root,
992 trans->block_group, 0,
996 total_needed += empty_size;
997 path = btrfs_alloc_path();
1000 if (!block_group->data)
1001 search_start = find_search_start(root, &block_group,
1002 search_start, total_needed);
1003 else if (!full_scan)
1004 search_start = max(block_group->last_alloc, search_start);
1006 btrfs_init_path(path);
1007 ins->objectid = search_start;
1011 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1015 if (path->slots[0] > 0) {
1019 l = btrfs_buffer_leaf(path->nodes[0]);
1020 btrfs_disk_key_to_cpu(&key, &l->items[path->slots[0]].key);
1022 * a rare case, go back one key if we hit a block group item
1023 * instead of an extent item
1025 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
1026 key.objectid + key.offset >= search_start) {
1027 ins->objectid = key.objectid;
1028 ins->offset = key.offset - 1;
1029 btrfs_release_path(root, path);
1030 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1034 if (path->slots[0] > 0) {
1040 l = btrfs_buffer_leaf(path->nodes[0]);
1041 slot = path->slots[0];
1042 if (slot >= btrfs_header_nritems(&l->header)) {
1044 limit = last_block +
1045 (block_group->key.offset >> 1);
1047 limit = search_start +
1048 (block_group->key.offset >> 1);
1049 ret = btrfs_next_leaf(root, path);
1055 ins->objectid = search_start;
1056 ins->offset = search_end - search_start;
1060 ins->objectid = last_block > search_start ?
1061 last_block : search_start;
1062 ins->offset = search_end - ins->objectid;
1066 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
1067 if (key.objectid >= search_start && key.objectid > last_block &&
1069 if (last_block < search_start)
1070 last_block = search_start;
1071 hole_size = key.objectid - last_block;
1072 if (hole_size >= num_blocks) {
1073 ins->objectid = last_block;
1074 ins->offset = hole_size;
1079 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
1083 last_block = key.objectid + key.offset;
1084 if (!full_scan && last_block >= block_group->key.objectid +
1085 block_group->key.offset) {
1086 btrfs_release_path(root, path);
1087 search_start = block_group->key.objectid +
1088 block_group->key.offset * 2;
1096 /* we have to make sure we didn't find an extent that has already
1097 * been allocated by the map tree or the original allocation
1099 btrfs_release_path(root, path);
1100 BUG_ON(ins->objectid < search_start);
1102 if (ins->objectid + num_blocks >= search_end) {
1107 search_start = orig_search_start;
1110 total_needed -= empty_size;
1116 for (test_block = ins->objectid;
1117 test_block < ins->objectid + num_blocks; test_block++) {
1118 if (test_radix_bit(&info->pinned_radix, test_block) ||
1119 test_radix_bit(&info->extent_ins_radix, test_block)) {
1120 search_start = test_block + 1;
1124 if (exclude_nr > 0 && (ins->objectid + num_blocks > exclude_start &&
1125 ins->objectid < exclude_start + exclude_nr)) {
1126 search_start = exclude_start + exclude_nr;
1130 block_group = btrfs_lookup_block_group(info, ins->objectid);
1132 trans->block_group = block_group;
1134 ins->offset = num_blocks;
1135 btrfs_free_path(path);
1139 if (search_start + num_blocks >= search_end) {
1140 search_start = orig_search_start;
1147 total_needed -= empty_size;
1152 block_group = btrfs_lookup_block_group(info, search_start);
1155 block_group = btrfs_find_block_group(root, block_group,
1156 search_start, data, 0);
1160 btrfs_release_path(root, path);
1161 btrfs_free_path(path);
1165 * finds a free extent and does all the dirty work required for allocation
1166 * returns the key for the extent through ins, and a tree buffer for
1167 * the first block of the extent through buf.
1169 * returns 0 if everything worked, non-zero otherwise.
1171 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1172 struct btrfs_root *root, u64 owner,
1173 u64 num_blocks, u64 empty_size, u64 hint_block,
1174 u64 search_end, struct btrfs_key *ins, int data)
1178 u64 super_blocks_used;
1179 u64 search_start = 0;
1180 struct btrfs_fs_info *info = root->fs_info;
1181 struct btrfs_root *extent_root = info->extent_root;
1182 struct btrfs_extent_item extent_item;
1184 btrfs_set_extent_refs(&extent_item, 1);
1185 btrfs_set_extent_owner(&extent_item, owner);
1187 WARN_ON(num_blocks < 1);
1188 ret = find_free_extent(trans, root, num_blocks, empty_size,
1189 search_start, search_end, hint_block, ins,
1190 trans->alloc_exclude_start,
1191 trans->alloc_exclude_nr, data);
1196 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
1197 btrfs_set_super_blocks_used(&info->super_copy, super_blocks_used +
1200 if (root == extent_root) {
1201 BUG_ON(num_blocks != 1);
1202 set_radix_bit(&root->fs_info->extent_ins_radix, ins->objectid);
1206 WARN_ON(trans->alloc_exclude_nr);
1207 trans->alloc_exclude_start = ins->objectid;
1208 trans->alloc_exclude_nr = ins->offset;
1209 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1210 sizeof(extent_item));
1212 trans->alloc_exclude_start = 0;
1213 trans->alloc_exclude_nr = 0;
1216 finish_current_insert(trans, extent_root);
1217 pending_ret = del_pending_extents(trans, extent_root);
1226 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1233 * helper function to allocate a block for a given tree
1234 * returns the tree buffer or NULL.
1236 struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1237 struct btrfs_root *root, u64 hint,
1240 struct btrfs_key ins;
1242 struct buffer_head *buf;
1244 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
1245 1, empty_size, hint,
1246 (unsigned long)-1, &ins, 0);
1249 return ERR_PTR(ret);
1251 buf = btrfs_find_create_tree_block(root, ins.objectid);
1253 btrfs_free_extent(trans, root, ins.objectid, 1, 0);
1254 return ERR_PTR(-ENOMEM);
1256 WARN_ON(buffer_dirty(buf));
1257 set_buffer_uptodate(buf);
1258 set_buffer_checked(buf);
1259 set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
1263 static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1264 struct btrfs_root *root, struct buffer_head *cur)
1266 struct btrfs_disk_key *key;
1267 struct btrfs_leaf *leaf;
1268 struct btrfs_file_extent_item *fi;
1273 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
1274 leaf = btrfs_buffer_leaf(cur);
1275 nritems = btrfs_header_nritems(&leaf->header);
1276 for (i = 0; i < nritems; i++) {
1278 key = &leaf->items[i].key;
1279 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
1281 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1282 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
1285 * FIXME make sure to insert a trans record that
1286 * repeats the snapshot del on crash
1288 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
1289 if (disk_blocknr == 0)
1291 ret = btrfs_free_extent(trans, root, disk_blocknr,
1292 btrfs_file_extent_disk_num_blocks(fi),
1299 static void reada_walk_down(struct btrfs_root *root,
1300 struct btrfs_node *node)
1308 nritems = btrfs_header_nritems(&node->header);
1309 for (i = 0; i < nritems; i++) {
1310 blocknr = btrfs_node_blockptr(node, i);
1311 ret = lookup_extent_ref(NULL, root, blocknr, 1, &refs);
1315 ret = readahead_tree_block(root, blocknr);
1322 * helper function for drop_snapshot, this walks down the tree dropping ref
1323 * counts as it goes.
1325 static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1326 *root, struct btrfs_path *path, int *level)
1328 struct buffer_head *next;
1329 struct buffer_head *cur;
1334 WARN_ON(*level < 0);
1335 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1336 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
1343 * walk down to the last node level and free all the leaves
1345 while(*level >= 0) {
1346 WARN_ON(*level < 0);
1347 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1348 cur = path->nodes[*level];
1350 if (*level > 0 && path->slots[*level] == 0)
1351 reada_walk_down(root, btrfs_buffer_node(cur));
1353 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
1356 if (path->slots[*level] >=
1357 btrfs_header_nritems(btrfs_buffer_header(cur)))
1360 ret = drop_leaf_ref(trans, root, cur);
1364 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
1365 path->slots[*level]);
1366 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
1369 path->slots[*level]++;
1370 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
1374 next = read_tree_block(root, blocknr);
1375 WARN_ON(*level <= 0);
1376 if (path->nodes[*level-1])
1377 btrfs_block_release(root, path->nodes[*level-1]);
1378 path->nodes[*level-1] = next;
1379 *level = btrfs_header_level(btrfs_buffer_header(next));
1380 path->slots[*level] = 0;
1383 WARN_ON(*level < 0);
1384 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1385 ret = btrfs_free_extent(trans, root,
1386 bh_blocknr(path->nodes[*level]), 1, 1);
1387 btrfs_block_release(root, path->nodes[*level]);
1388 path->nodes[*level] = NULL;
1395 * helper for dropping snapshots. This walks back up the tree in the path
1396 * to find the first node higher up where we haven't yet gone through
1399 static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1400 *root, struct btrfs_path *path, int *level)
1405 struct btrfs_root_item *root_item = &root->root_item;
1407 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1408 slot = path->slots[i];
1409 if (slot < btrfs_header_nritems(
1410 btrfs_buffer_header(path->nodes[i])) - 1) {
1411 struct btrfs_node *node;
1412 node = btrfs_buffer_node(path->nodes[i]);
1415 WARN_ON(*level == 0);
1416 memcpy(&root_item->drop_progress,
1417 &node->ptrs[path->slots[i]].key,
1418 sizeof(root_item->drop_progress));
1419 root_item->drop_level = i;
1422 ret = btrfs_free_extent(trans, root,
1423 bh_blocknr(path->nodes[*level]),
1426 btrfs_block_release(root, path->nodes[*level]);
1427 path->nodes[*level] = NULL;
1435 * drop the reference count on the tree rooted at 'snap'. This traverses
1436 * the tree freeing any blocks that have a ref count of zero after being
1439 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
1445 struct btrfs_path *path;
1449 struct btrfs_root_item *root_item = &root->root_item;
1451 path = btrfs_alloc_path();
1454 level = btrfs_header_level(btrfs_buffer_header(root->node));
1456 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1457 path->nodes[level] = root->node;
1458 path->slots[level] = 0;
1460 struct btrfs_key key;
1461 struct btrfs_disk_key *found_key;
1462 struct btrfs_node *node;
1464 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1465 level = root_item->drop_level;
1466 path->lowest_level = level;
1467 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1472 node = btrfs_buffer_node(path->nodes[level]);
1473 found_key = &node->ptrs[path->slots[level]].key;
1474 WARN_ON(memcmp(found_key, &root_item->drop_progress,
1475 sizeof(*found_key)));
1478 wret = walk_down_tree(trans, root, path, &level);
1484 wret = walk_up_tree(trans, root, path, &level);
1490 if (num_walks > 2) {
1496 for (i = 0; i <= orig_level; i++) {
1497 if (path->nodes[i]) {
1498 btrfs_block_release(root, path->nodes[i]);
1503 btrfs_free_path(path);
1507 static int free_block_group_radix(struct radix_tree_root *radix)
1510 struct btrfs_block_group_cache *cache[8];
1514 ret = radix_tree_gang_lookup(radix, (void **)cache, 0,
1518 for (i = 0; i < ret; i++) {
1519 radix_tree_delete(radix, cache[i]->key.objectid +
1520 cache[i]->key.offset - 1);
1527 int btrfs_free_block_groups(struct btrfs_fs_info *info)
1531 unsigned long gang[16];
1534 ret = free_block_group_radix(&info->block_group_radix);
1535 ret2 = free_block_group_radix(&info->block_group_data_radix);
1542 ret = find_first_radix_bit(&info->extent_map_radix,
1543 gang, 0, ARRAY_SIZE(gang));
1546 for (i = 0; i < ret; i++) {
1547 clear_radix_bit(&info->extent_map_radix, gang[i]);
1553 int btrfs_read_block_groups(struct btrfs_root *root)
1555 struct btrfs_path *path;
1558 struct btrfs_block_group_item *bi;
1559 struct btrfs_block_group_cache *cache;
1560 struct btrfs_fs_info *info = root->fs_info;
1561 struct radix_tree_root *radix;
1562 struct btrfs_key key;
1563 struct btrfs_key found_key;
1564 struct btrfs_leaf *leaf;
1565 u64 group_size_blocks;
1568 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE >>
1569 root->fs_info->sb->s_blocksize_bits;
1570 root = info->extent_root;
1572 key.offset = group_size_blocks;
1574 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1576 path = btrfs_alloc_path();
1581 ret = btrfs_search_slot(NULL, info->extent_root,
1587 leaf = btrfs_buffer_leaf(path->nodes[0]);
1588 btrfs_disk_key_to_cpu(&found_key,
1589 &leaf->items[path->slots[0]].key);
1590 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1596 bi = btrfs_item_ptr(leaf, path->slots[0],
1597 struct btrfs_block_group_item);
1598 if (bi->flags & BTRFS_BLOCK_GROUP_DATA) {
1599 radix = &info->block_group_data_radix;
1602 radix = &info->block_group_radix;
1606 memcpy(&cache->item, bi, sizeof(*bi));
1607 memcpy(&cache->key, &found_key, sizeof(found_key));
1608 cache->last_alloc = cache->key.objectid;
1609 cache->first_free = cache->key.objectid;
1613 cache->radix = radix;
1615 key.objectid = found_key.objectid + found_key.offset;
1616 btrfs_release_path(root, path);
1617 ret = radix_tree_insert(radix, found_key.objectid +
1618 found_key.offset - 1,
1621 used = btrfs_block_group_used(bi);
1622 if (used < div_factor(key.offset, 8)) {
1623 radix_tree_tag_set(radix, found_key.objectid +
1624 found_key.offset - 1,
1625 BTRFS_BLOCK_GROUP_AVAIL);
1628 btrfs_super_total_blocks(&info->super_copy))
1632 btrfs_free_path(path);