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 #define BLOCK_GROUP_DATA EXTENT_WRITEBACK
26 #define BLOCK_GROUP_METADATA EXTENT_UPTODATE
27 #define BLOCK_GROUP_DIRTY EXTENT_DIRTY
29 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
30 btrfs_root *extent_root);
31 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
32 btrfs_root *extent_root);
34 static int cache_block_group(struct btrfs_root *root,
35 struct btrfs_block_group_cache *block_group)
37 struct btrfs_path *path;
40 struct extent_buffer *leaf;
41 struct extent_map_tree *free_space_cache;
48 root = root->fs_info->extent_root;
49 free_space_cache = &root->fs_info->free_space_cache;
51 if (block_group->cached)
54 path = btrfs_alloc_path();
59 first_free = block_group->key.objectid;
60 key.objectid = block_group->key.objectid;
63 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
64 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
69 if (ret && path->slots[0] > 0)
73 leaf = path->nodes[0];
74 slot = path->slots[0];
75 if (slot >= btrfs_header_nritems(leaf)) {
76 ret = btrfs_next_leaf(root, path);
86 btrfs_item_key_to_cpu(leaf, &key, slot);
87 if (key.objectid < block_group->key.objectid) {
88 if (key.objectid + key.offset > first_free)
89 first_free = key.objectid + key.offset;
93 if (key.objectid >= block_group->key.objectid +
94 block_group->key.offset) {
98 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
103 if (key.objectid > last) {
104 hole_size = key.objectid - last;
105 set_extent_dirty(free_space_cache, last,
106 last + hole_size - 1,
109 last = key.objectid + key.offset;
117 if (block_group->key.objectid +
118 block_group->key.offset > last) {
119 hole_size = block_group->key.objectid +
120 block_group->key.offset - last;
121 set_extent_dirty(free_space_cache, last,
122 last + hole_size - 1, GFP_NOFS);
124 block_group->cached = 1;
126 btrfs_free_path(path);
130 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
134 struct extent_map_tree *block_group_cache;
135 struct btrfs_block_group_cache *block_group = NULL;
141 block_group_cache = &info->block_group_cache;
142 ret = find_first_extent_bit(block_group_cache,
143 blocknr, &start, &end,
144 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA);
148 ret = get_state_private(block_group_cache, start, &ptr);
152 block_group = (struct btrfs_block_group_cache *)ptr;
155 if (block_group->key.objectid <= blocknr && blocknr <=
156 block_group->key.objectid + block_group->key.offset)
162 static u64 find_search_start(struct btrfs_root *root,
163 struct btrfs_block_group_cache **cache_ret,
164 u64 search_start, int num, int data)
167 struct btrfs_block_group_cache *cache = *cache_ret;
168 u64 last = max(search_start, cache->key.objectid);
173 ret = cache_block_group(root, cache);
177 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
178 last, &start, &end, EXTENT_DIRTY);
182 start = max(last, start);
184 if (end + 1 - start < num)
186 if (start + num >= cache->key.objectid + cache->key.offset)
191 return max(cache->last_alloc, search_start);
194 cache = btrfs_lookup_block_group(root->fs_info,
195 last + cache->key.offset - 1);
197 return max((*cache_ret)->last_alloc, search_start);
199 cache = btrfs_find_block_group(root, cache,
200 last + cache->key.offset - 1, data, 0);
202 last = min(cache->key.objectid, last);
206 static u64 div_factor(u64 num, int factor)
213 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
214 struct btrfs_block_group_cache
215 *hint, u64 search_start,
218 struct btrfs_block_group_cache *cache;
219 struct extent_map_tree *block_group_cache;
220 struct btrfs_block_group_cache *found_group = NULL;
221 struct btrfs_fs_info *info = root->fs_info;
235 block_group_cache = &info->block_group_cache;
241 bit = BLOCK_GROUP_DATA;
243 bit = BLOCK_GROUP_METADATA;
246 struct btrfs_block_group_cache *shint;
247 shint = btrfs_lookup_block_group(info, search_start);
248 if (shint && shint->data == data) {
249 used = btrfs_block_group_used(&shint->item);
250 if (used + shint->pinned <
251 div_factor(shint->key.offset, factor)) {
256 if (hint && hint->data == data) {
257 used = btrfs_block_group_used(&hint->item);
258 if (used + hint->pinned <
259 div_factor(hint->key.offset, factor)) {
262 last = hint->key.offset * 3;
263 if (hint->key.objectid >= last)
264 last = max(search_start + hint->key.offset - 1,
265 hint->key.objectid - last);
267 last = hint->key.objectid + hint->key.offset;
271 hint_last = max(hint->key.objectid, search_start);
273 hint_last = search_start;
279 ret = find_first_extent_bit(block_group_cache, last,
284 ret = get_state_private(block_group_cache, start, &ptr);
288 cache = (struct btrfs_block_group_cache *)ptr;
289 last = cache->key.objectid + cache->key.offset;
290 used = btrfs_block_group_used(&cache->item);
293 free_check = cache->key.offset;
295 free_check = div_factor(cache->key.offset, factor);
297 if (used + cache->pinned < free_check) {
310 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
318 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
319 struct btrfs_root *root,
320 u64 blocknr, u64 num_blocks)
322 struct btrfs_path *path;
324 struct btrfs_key key;
325 struct extent_buffer *l;
326 struct btrfs_extent_item *item;
329 path = btrfs_alloc_path();
333 key.objectid = blocknr;
334 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
335 key.offset = num_blocks;
336 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
345 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
346 refs = btrfs_extent_refs(l, item);
347 btrfs_set_extent_refs(l, item, refs + 1);
348 btrfs_mark_buffer_dirty(path->nodes[0]);
350 btrfs_release_path(root->fs_info->extent_root, path);
351 btrfs_free_path(path);
352 finish_current_insert(trans, root->fs_info->extent_root);
353 del_pending_extents(trans, root->fs_info->extent_root);
357 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
358 struct btrfs_root *root)
360 finish_current_insert(trans, root->fs_info->extent_root);
361 del_pending_extents(trans, root->fs_info->extent_root);
365 static int lookup_extent_ref(struct btrfs_trans_handle *trans,
366 struct btrfs_root *root, u64 blocknr,
367 u64 num_blocks, u32 *refs)
369 struct btrfs_path *path;
371 struct btrfs_key key;
372 struct extent_buffer *l;
373 struct btrfs_extent_item *item;
375 path = btrfs_alloc_path();
376 key.objectid = blocknr;
377 key.offset = num_blocks;
378 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
379 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
384 btrfs_print_leaf(root, path->nodes[0]);
385 printk("failed to find block number %Lu\n", blocknr);
389 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
390 *refs = btrfs_extent_refs(l, item);
392 btrfs_free_path(path);
396 int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
397 struct btrfs_root *root)
399 return btrfs_inc_extent_ref(trans, root,
400 extent_buffer_blocknr(root->node), 1);
403 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
404 struct extent_buffer *buf)
408 struct btrfs_key key;
409 struct btrfs_file_extent_item *fi;
419 leaf = btrfs_is_leaf(buf);
420 nritems = btrfs_header_nritems(buf);
421 for (i = 0; i < nritems; i++) {
424 btrfs_item_key_to_cpu(buf, &key, i);
425 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
427 fi = btrfs_item_ptr(buf, i,
428 struct btrfs_file_extent_item);
429 if (btrfs_file_extent_type(buf, fi) ==
430 BTRFS_FILE_EXTENT_INLINE)
432 disk_blocknr = btrfs_file_extent_disk_blocknr(buf, fi);
433 if (disk_blocknr == 0)
435 ret = btrfs_inc_extent_ref(trans, root, disk_blocknr,
436 btrfs_file_extent_disk_num_blocks(buf, fi));
442 blocknr = btrfs_node_blockptr(buf, i);
443 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
453 for (i =0; i < faili; i++) {
456 btrfs_item_key_to_cpu(buf, &key, i);
457 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
459 fi = btrfs_item_ptr(buf, i,
460 struct btrfs_file_extent_item);
461 if (btrfs_file_extent_type(buf, fi) ==
462 BTRFS_FILE_EXTENT_INLINE)
464 disk_blocknr = btrfs_file_extent_disk_blocknr(buf, fi);
465 if (disk_blocknr == 0)
467 err = btrfs_free_extent(trans, root, disk_blocknr,
468 btrfs_file_extent_disk_num_blocks(buf,
472 blocknr = btrfs_node_blockptr(buf, i);
473 err = btrfs_free_extent(trans, root, blocknr, 1, 0);
480 static int write_one_cache_group(struct btrfs_trans_handle *trans,
481 struct btrfs_root *root,
482 struct btrfs_path *path,
483 struct btrfs_block_group_cache *cache)
487 struct btrfs_root *extent_root = root->fs_info->extent_root;
489 struct extent_buffer *leaf;
491 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
496 leaf = path->nodes[0];
497 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
498 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
499 btrfs_mark_buffer_dirty(leaf);
500 btrfs_release_path(extent_root, path);
502 finish_current_insert(trans, extent_root);
503 pending_ret = del_pending_extents(trans, extent_root);
509 cache->last_alloc = cache->first_free;
514 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
515 struct btrfs_root *root)
517 struct extent_map_tree *block_group_cache;
518 struct btrfs_block_group_cache *cache;
522 struct btrfs_path *path;
528 block_group_cache = &root->fs_info->block_group_cache;
529 path = btrfs_alloc_path();
534 ret = find_first_extent_bit(block_group_cache, last,
535 &start, &end, BLOCK_GROUP_DIRTY);
540 ret = get_state_private(block_group_cache, start, &ptr);
544 cache = (struct btrfs_block_group_cache *)ptr;
545 err = write_one_cache_group(trans, root,
548 * if we fail to write the cache group, we want
549 * to keep it marked dirty in hopes that a later
556 clear_extent_bits(block_group_cache, start, end,
557 BLOCK_GROUP_DIRTY, GFP_NOFS);
559 btrfs_free_path(path);
563 static int update_block_group(struct btrfs_trans_handle *trans,
564 struct btrfs_root *root,
565 u64 blocknr, u64 num, int alloc, int mark_free,
568 struct btrfs_block_group_cache *cache;
569 struct btrfs_fs_info *info = root->fs_info;
577 cache = btrfs_lookup_block_group(info, blocknr);
581 block_in_group = blocknr - cache->key.objectid;
582 WARN_ON(block_in_group > cache->key.offset);
583 start = cache->key.objectid;
584 end = start + cache->key.offset - 1;
585 set_extent_bits(&info->block_group_cache, start, end,
586 BLOCK_GROUP_DIRTY, GFP_NOFS);
588 old_val = btrfs_block_group_used(&cache->item);
589 num = min(total, cache->key.offset - block_in_group);
591 if (blocknr > cache->last_alloc)
592 cache->last_alloc = blocknr;
593 if (cache->data != data &&
594 old_val < (cache->key.offset >> 1)) {
600 bit_to_clear = BLOCK_GROUP_DATA;
601 bit_to_set = BLOCK_GROUP_METADATA;
603 BTRFS_BLOCK_GROUP_DATA;
605 bit_to_clear = BLOCK_GROUP_METADATA;
606 bit_to_set = BLOCK_GROUP_DATA;
608 ~BTRFS_BLOCK_GROUP_DATA;
610 clear_extent_bits(&info->block_group_cache,
611 start, end, bit_to_clear,
613 set_extent_bits(&info->block_group_cache,
614 start, end, bit_to_set,
620 if (blocknr < cache->first_free)
621 cache->first_free = blocknr;
623 set_extent_dirty(&info->free_space_cache,
624 blocknr, blocknr + num - 1,
628 btrfs_set_block_group_used(&cache->item, old_val);
635 int btrfs_copy_pinned(struct btrfs_root *root, struct radix_tree_root *copy)
637 unsigned long gang[8];
639 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
644 ret = find_first_radix_bit(pinned_radix, gang, last,
648 for (i = 0 ; i < ret; i++) {
649 set_radix_bit(copy, gang[i]);
653 ret = find_first_radix_bit(&root->fs_info->extent_ins_radix, gang, 0,
659 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
660 struct btrfs_root *root,
661 struct radix_tree_root *unpin_radix)
663 unsigned long gang[8];
664 struct btrfs_block_group_cache *block_group;
668 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
669 struct extent_map_tree *free_space_cache;
671 free_space_cache = &root->fs_info->free_space_cache;
674 ret = find_first_radix_bit(unpin_radix, gang, 0,
680 for (i = 0; i < ret; i++) {
681 clear_radix_bit(pinned_radix, gang[i]);
682 clear_radix_bit(unpin_radix, gang[i]);
683 block_group = btrfs_lookup_block_group(root->fs_info,
686 WARN_ON(block_group->pinned == 0);
687 block_group->pinned--;
688 if (gang[i] < block_group->last_alloc)
689 block_group->last_alloc = gang[i];
690 set_extent_dirty(free_space_cache,
691 gang[i], gang[i], GFP_NOFS);
698 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
699 btrfs_root *extent_root)
701 struct btrfs_key ins;
702 struct btrfs_extent_item extent_item;
706 unsigned long gang[8];
707 struct btrfs_fs_info *info = extent_root->fs_info;
709 btrfs_set_stack_extent_refs(&extent_item, 1);
711 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
712 btrfs_set_stack_extent_owner(&extent_item,
713 extent_root->root_key.objectid);
716 ret = find_first_radix_bit(&info->extent_ins_radix, gang, 0,
721 for (i = 0; i < ret; i++) {
722 ins.objectid = gang[i];
723 err = btrfs_insert_item(trans, extent_root, &ins,
725 sizeof(extent_item));
726 clear_radix_bit(&info->extent_ins_radix, gang[i]);
733 static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
736 struct extent_buffer *buf;
739 buf = btrfs_find_tree_block(root, blocknr);
741 if (btrfs_buffer_uptodate(buf)) {
743 root->fs_info->running_transaction->transid;
744 if (btrfs_header_generation(buf) == transid) {
745 free_extent_buffer(buf);
749 free_extent_buffer(buf);
751 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
753 struct btrfs_block_group_cache *cache;
754 cache = btrfs_lookup_block_group(root->fs_info,
760 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
767 * remove an extent from the root, returns 0 on success
769 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
770 *root, u64 blocknr, u64 num_blocks, int pin,
773 struct btrfs_path *path;
774 struct btrfs_key key;
775 struct btrfs_fs_info *info = root->fs_info;
776 struct btrfs_root *extent_root = info->extent_root;
777 struct extent_buffer *leaf;
779 struct btrfs_extent_item *ei;
782 key.objectid = blocknr;
783 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
784 key.offset = num_blocks;
786 path = btrfs_alloc_path();
790 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
795 leaf = path->nodes[0];
796 ei = btrfs_item_ptr(leaf, path->slots[0],
797 struct btrfs_extent_item);
798 refs = btrfs_extent_refs(leaf, ei);
801 btrfs_set_extent_refs(leaf, ei, refs);
802 btrfs_mark_buffer_dirty(leaf);
805 u64 super_blocks_used, root_blocks_used;
808 ret = pin_down_block(root, blocknr, 0);
812 /* block accounting for super block */
813 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
814 btrfs_set_super_blocks_used(&info->super_copy,
815 super_blocks_used - num_blocks);
817 /* block accounting for root item */
818 root_blocks_used = btrfs_root_used(&root->root_item);
819 btrfs_set_root_used(&root->root_item,
820 root_blocks_used - num_blocks);
822 ret = btrfs_del_item(trans, extent_root, path);
826 ret = update_block_group(trans, root, blocknr, num_blocks, 0,
830 btrfs_free_path(path);
831 finish_current_insert(trans, extent_root);
836 * find all the blocks marked as pending in the radix tree and remove
837 * them from the extent map
839 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
840 btrfs_root *extent_root)
845 unsigned long gang[4];
847 struct radix_tree_root *pending_radix;
848 struct radix_tree_root *pinned_radix;
849 struct btrfs_block_group_cache *cache;
851 pending_radix = &extent_root->fs_info->pending_del_radix;
852 pinned_radix = &extent_root->fs_info->pinned_radix;
855 ret = find_first_radix_bit(pending_radix, gang, 0,
859 for (i = 0; i < ret; i++) {
860 wret = set_radix_bit(pinned_radix, gang[i]);
863 btrfs_lookup_block_group(extent_root->fs_info,
869 printk(KERN_CRIT "set_radix_bit, err %d\n",
873 wret = clear_radix_bit(pending_radix, gang[i]);
875 wret = __free_extent(trans, extent_root,
885 * remove an extent from the root, returns 0 on success
887 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
888 *root, u64 blocknr, u64 num_blocks, int pin)
890 struct btrfs_root *extent_root = root->fs_info->extent_root;
894 if (root == extent_root) {
895 pin_down_block(root, blocknr, 1);
898 ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
899 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
900 return ret ? ret : pending_ret;
904 * walks the btree of allocated extents and find a hole of a given size.
905 * The key ins is changed to record the hole:
906 * ins->objectid == block start
907 * ins->flags = BTRFS_EXTENT_ITEM_KEY
908 * ins->offset == number of blocks
909 * Any available blocks before search_start are skipped.
911 static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
912 *orig_root, u64 num_blocks, u64 empty_size,
913 u64 search_start, u64 search_end, u64 hint_block,
914 struct btrfs_key *ins, u64 exclude_start,
915 u64 exclude_nr, int data)
917 struct btrfs_path *path;
918 struct btrfs_key key;
924 u64 orig_search_start = search_start;
926 struct extent_buffer *l;
927 struct btrfs_root * root = orig_root->fs_info->extent_root;
928 struct btrfs_fs_info *info = root->fs_info;
929 int total_needed = num_blocks;
931 struct btrfs_block_group_cache *block_group;
934 u64 cached_search_start = 0;
936 WARN_ON(num_blocks < 1);
937 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
939 level = btrfs_header_level(root->node);
941 if (search_end == (u64)-1)
942 search_end = btrfs_super_total_blocks(&info->super_copy);
944 block_group = btrfs_lookup_block_group(info, hint_block);
945 block_group = btrfs_find_block_group(root, block_group,
946 hint_block, data, 1);
948 block_group = btrfs_find_block_group(root,
949 trans->block_group, 0,
953 total_needed += empty_size;
954 path = btrfs_alloc_path();
957 search_start = find_search_start(root, &block_group,
958 search_start, total_needed, data);
959 cached_search_start = search_start;
961 btrfs_init_path(path);
962 ins->objectid = search_start;
967 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
971 if (path->slots[0] > 0) {
976 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
979 * a rare case, go back one key if we hit a block group item
980 * instead of an extent item
982 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
983 key.objectid + key.offset >= search_start) {
984 ins->objectid = key.objectid;
985 ins->offset = key.offset - 1;
986 btrfs_release_path(root, path);
987 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
991 if (path->slots[0] > 0) {
998 slot = path->slots[0];
999 if (slot >= btrfs_header_nritems(l)) {
1000 ret = btrfs_next_leaf(root, path);
1006 ins->objectid = search_start;
1007 ins->offset = search_end - search_start;
1011 ins->objectid = last_block > search_start ?
1012 last_block : search_start;
1013 ins->offset = search_end - ins->objectid;
1016 btrfs_item_key_to_cpu(l, &key, slot);
1018 if (key.objectid >= search_start && key.objectid > last_block &&
1020 if (last_block < search_start)
1021 last_block = search_start;
1022 hole_size = key.objectid - last_block;
1023 if (hole_size >= num_blocks) {
1024 ins->objectid = last_block;
1025 ins->offset = hole_size;
1029 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
1031 last_block = key.objectid;
1039 last_block = key.objectid + key.offset;
1041 if (!full_scan && last_block >= block_group->key.objectid +
1042 block_group->key.offset) {
1043 btrfs_release_path(root, path);
1044 search_start = block_group->key.objectid +
1045 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)
1062 for (test_block = ins->objectid;
1063 test_block < ins->objectid + num_blocks; test_block++) {
1064 if (test_radix_bit(&info->pinned_radix, test_block) ||
1065 test_radix_bit(&info->extent_ins_radix, test_block)) {
1066 search_start = test_block + 1;
1070 if (exclude_nr > 0 && (ins->objectid + num_blocks > exclude_start &&
1071 ins->objectid < exclude_start + exclude_nr)) {
1072 search_start = exclude_start + exclude_nr;
1076 block_group = btrfs_lookup_block_group(info, ins->objectid);
1078 trans->block_group = block_group;
1080 ins->offset = num_blocks;
1081 btrfs_free_path(path);
1085 if (search_start + num_blocks >= search_end) {
1087 search_start = orig_search_start;
1094 total_needed -= empty_size;
1099 block_group = btrfs_lookup_block_group(info, search_start);
1102 block_group = btrfs_find_block_group(root, block_group,
1103 search_start, data, 0);
1107 btrfs_release_path(root, path);
1108 btrfs_free_path(path);
1112 * finds a free extent and does all the dirty work required for allocation
1113 * returns the key for the extent through ins, and a tree buffer for
1114 * the first block of the extent through buf.
1116 * returns 0 if everything worked, non-zero otherwise.
1118 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1119 struct btrfs_root *root, u64 owner,
1120 u64 num_blocks, u64 empty_size, u64 hint_block,
1121 u64 search_end, struct btrfs_key *ins, int data)
1125 u64 super_blocks_used, root_blocks_used;
1126 u64 search_start = 0;
1127 struct btrfs_fs_info *info = root->fs_info;
1128 struct btrfs_root *extent_root = info->extent_root;
1129 struct btrfs_extent_item extent_item;
1131 btrfs_set_stack_extent_refs(&extent_item, 1);
1132 btrfs_set_stack_extent_owner(&extent_item, owner);
1134 WARN_ON(num_blocks < 1);
1135 ret = find_free_extent(trans, root, num_blocks, empty_size,
1136 search_start, search_end, hint_block, ins,
1137 trans->alloc_exclude_start,
1138 trans->alloc_exclude_nr, data);
1143 /* block accounting for super block */
1144 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
1145 btrfs_set_super_blocks_used(&info->super_copy, super_blocks_used +
1148 /* block accounting for root item */
1149 root_blocks_used = btrfs_root_used(&root->root_item);
1150 btrfs_set_root_used(&root->root_item, root_blocks_used +
1153 clear_extent_dirty(&root->fs_info->free_space_cache,
1154 ins->objectid, ins->objectid + ins->offset - 1,
1157 if (root == extent_root) {
1158 BUG_ON(num_blocks != 1);
1159 set_radix_bit(&root->fs_info->extent_ins_radix, ins->objectid);
1163 WARN_ON(trans->alloc_exclude_nr);
1164 trans->alloc_exclude_start = ins->objectid;
1165 trans->alloc_exclude_nr = ins->offset;
1166 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1167 sizeof(extent_item));
1169 trans->alloc_exclude_start = 0;
1170 trans->alloc_exclude_nr = 0;
1173 finish_current_insert(trans, extent_root);
1174 pending_ret = del_pending_extents(trans, extent_root);
1184 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1191 * helper function to allocate a block for a given tree
1192 * returns the tree buffer or NULL.
1194 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1195 struct btrfs_root *root, u64 hint,
1198 struct btrfs_key ins;
1200 struct extent_buffer *buf;
1202 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
1203 1, empty_size, hint, (u64)-1, &ins, 0);
1206 return ERR_PTR(ret);
1208 buf = btrfs_find_create_tree_block(root, ins.objectid);
1210 btrfs_free_extent(trans, root, ins.objectid, 1, 0);
1211 return ERR_PTR(-ENOMEM);
1213 btrfs_set_buffer_uptodate(buf);
1214 buf->alloc_addr = (unsigned long)__builtin_return_address(0);
1215 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1216 buf->start + buf->len - 1, GFP_NOFS);
1218 set_buffer_checked(buf);
1219 set_buffer_defrag(buf);
1221 /* FIXME!!!!!!!!!!!!!!!!
1222 set_radix_bit(&trans->transaction->dirty_pages, buf->pages[0]->index);
1224 trans->blocks_used++;
1228 static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1229 struct btrfs_root *root, struct extent_buffer *leaf)
1231 struct btrfs_key key;
1232 struct btrfs_file_extent_item *fi;
1237 BUG_ON(!btrfs_is_leaf(leaf));
1238 nritems = btrfs_header_nritems(leaf);
1239 for (i = 0; i < nritems; i++) {
1242 btrfs_item_key_to_cpu(leaf, &key, i);
1243 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1245 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1246 if (btrfs_file_extent_type(leaf, fi) ==
1247 BTRFS_FILE_EXTENT_INLINE)
1250 * FIXME make sure to insert a trans record that
1251 * repeats the snapshot del on crash
1253 disk_blocknr = btrfs_file_extent_disk_blocknr(leaf, fi);
1254 if (disk_blocknr == 0)
1256 ret = btrfs_free_extent(trans, root, disk_blocknr,
1257 btrfs_file_extent_disk_num_blocks(leaf, fi), 0);
1263 static void reada_walk_down(struct btrfs_root *root,
1264 struct extent_buffer *node)
1272 nritems = btrfs_header_nritems(node);
1273 for (i = 0; i < nritems; i++) {
1274 blocknr = btrfs_node_blockptr(node, i);
1275 ret = lookup_extent_ref(NULL, root, blocknr, 1, &refs);
1279 mutex_unlock(&root->fs_info->fs_mutex);
1280 ret = readahead_tree_block(root, blocknr);
1282 mutex_lock(&root->fs_info->fs_mutex);
1289 * helper function for drop_snapshot, this walks down the tree dropping ref
1290 * counts as it goes.
1292 static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1293 *root, struct btrfs_path *path, int *level)
1295 struct extent_buffer *next;
1296 struct extent_buffer *cur;
1301 WARN_ON(*level < 0);
1302 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1303 ret = lookup_extent_ref(trans, root,
1304 extent_buffer_blocknr(path->nodes[*level]),
1311 * walk down to the last node level and free all the leaves
1313 while(*level >= 0) {
1314 WARN_ON(*level < 0);
1315 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1316 cur = path->nodes[*level];
1318 if (*level > 0 && path->slots[*level] == 0)
1319 reada_walk_down(root, cur);
1321 if (btrfs_header_level(cur) != *level)
1324 if (path->slots[*level] >=
1325 btrfs_header_nritems(cur))
1328 ret = drop_leaf_ref(trans, root, cur);
1332 blocknr = btrfs_node_blockptr(cur, path->slots[*level]);
1333 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
1336 path->slots[*level]++;
1337 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
1341 next = btrfs_find_tree_block(root, blocknr);
1342 if (!next || !btrfs_buffer_uptodate(next)) {
1343 free_extent_buffer(next);
1344 mutex_unlock(&root->fs_info->fs_mutex);
1345 next = read_tree_block(root, blocknr);
1346 mutex_lock(&root->fs_info->fs_mutex);
1348 /* we dropped the lock, check one more time */
1349 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
1352 path->slots[*level]++;
1353 free_extent_buffer(next);
1354 ret = btrfs_free_extent(trans, root,
1360 WARN_ON(*level <= 0);
1361 if (path->nodes[*level-1])
1362 free_extent_buffer(path->nodes[*level-1]);
1363 path->nodes[*level-1] = next;
1364 *level = btrfs_header_level(next);
1365 path->slots[*level] = 0;
1368 WARN_ON(*level < 0);
1369 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1370 ret = btrfs_free_extent(trans, root,
1371 extent_buffer_blocknr(path->nodes[*level]), 1, 1);
1372 free_extent_buffer(path->nodes[*level]);
1373 path->nodes[*level] = NULL;
1380 * helper for dropping snapshots. This walks back up the tree in the path
1381 * to find the first node higher up where we haven't yet gone through
1384 static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1385 *root, struct btrfs_path *path, int *level)
1390 struct btrfs_root_item *root_item = &root->root_item;
1392 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1393 slot = path->slots[i];
1394 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1395 struct extent_buffer *node;
1396 struct btrfs_disk_key disk_key;
1397 node = path->nodes[i];
1400 WARN_ON(*level == 0);
1401 btrfs_node_key(node, &disk_key, path->slots[i]);
1402 memcpy(&root_item->drop_progress,
1403 &disk_key, sizeof(disk_key));
1404 root_item->drop_level = i;
1407 ret = btrfs_free_extent(trans, root,
1408 extent_buffer_blocknr(path->nodes[*level]),
1411 free_extent_buffer(path->nodes[*level]);
1412 path->nodes[*level] = NULL;
1420 * drop the reference count on the tree rooted at 'snap'. This traverses
1421 * the tree freeing any blocks that have a ref count of zero after being
1424 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
1430 struct btrfs_path *path;
1433 struct btrfs_root_item *root_item = &root->root_item;
1435 path = btrfs_alloc_path();
1438 level = btrfs_header_level(root->node);
1440 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1441 path->nodes[level] = root->node;
1442 extent_buffer_get(root->node);
1443 path->slots[level] = 0;
1445 struct btrfs_key key;
1446 struct btrfs_disk_key found_key;
1447 struct extent_buffer *node;
1449 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1450 level = root_item->drop_level;
1451 path->lowest_level = level;
1452 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1457 node = path->nodes[level];
1458 btrfs_node_key(node, &found_key, path->slots[level]);
1459 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1460 sizeof(found_key)));
1463 wret = walk_down_tree(trans, root, path, &level);
1469 wret = walk_up_tree(trans, root, path, &level);
1477 for (i = 0; i <= orig_level; i++) {
1478 if (path->nodes[i]) {
1479 free_extent_buffer(path->nodes[i]);
1484 btrfs_free_path(path);
1488 int btrfs_free_block_groups(struct btrfs_fs_info *info)
1495 ret = find_first_extent_bit(&info->block_group_cache, 0,
1496 &start, &end, (unsigned int)-1);
1499 clear_extent_bits(&info->block_group_cache, start,
1500 end, (unsigned int)-1, GFP_NOFS);
1503 ret = find_first_extent_bit(&info->free_space_cache, 0,
1504 &start, &end, EXTENT_DIRTY);
1507 clear_extent_dirty(&info->free_space_cache, start,
1513 int btrfs_read_block_groups(struct btrfs_root *root)
1515 struct btrfs_path *path;
1519 struct btrfs_block_group_cache *cache;
1520 struct btrfs_fs_info *info = root->fs_info;
1521 struct extent_map_tree *block_group_cache;
1522 struct btrfs_key key;
1523 struct btrfs_key found_key;
1524 struct extent_buffer *leaf;
1525 u64 group_size_blocks;
1527 block_group_cache = &info->block_group_cache;
1529 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE >>
1530 info->sb->s_blocksize_bits;
1532 root = info->extent_root;
1534 key.offset = group_size_blocks;
1535 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1537 path = btrfs_alloc_path();
1542 ret = btrfs_search_slot(NULL, info->extent_root,
1548 leaf = path->nodes[0];
1549 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1550 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1556 read_extent_buffer(leaf, &cache->item,
1557 btrfs_item_ptr_offset(leaf, path->slots[0]),
1558 sizeof(cache->item));
1559 memcpy(&cache->key, &found_key, sizeof(found_key));
1560 cache->last_alloc = cache->key.objectid;
1561 cache->first_free = cache->key.objectid;
1565 key.objectid = found_key.objectid + found_key.offset;
1566 btrfs_release_path(root, path);
1568 if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
1569 bit = BLOCK_GROUP_DATA;
1572 bit = BLOCK_GROUP_METADATA;
1576 /* use EXTENT_LOCKED to prevent merging */
1577 set_extent_bits(block_group_cache, found_key.objectid,
1578 found_key.objectid + found_key.offset - 1,
1579 bit | EXTENT_LOCKED, GFP_NOFS);
1580 set_state_private(block_group_cache, found_key.objectid,
1584 btrfs_super_total_blocks(&info->super_copy))
1588 btrfs_free_path(path);