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;
44 root = root->fs_info->extent_root;
45 extent_radix = &root->fs_info->extent_map_radix;
47 if (block_group->cached)
49 if (block_group->data)
51 path = btrfs_alloc_path();
55 key.objectid = block_group->key.objectid;
58 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
59 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
62 if (ret && path->slots[0] > 0)
65 leaf = btrfs_buffer_leaf(path->nodes[0]);
66 slot = path->slots[0];
67 if (slot >= btrfs_header_nritems(&leaf->header)) {
68 ret = btrfs_next_leaf(root, path);
75 hole_size = block_group->key.objectid +
76 block_group->key.offset - last;
78 last = block_group->key.objectid;
79 hole_size = block_group->key.offset;
81 for (i = 0; i < hole_size; i++) {
82 set_radix_bit(extent_radix,
88 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
89 if (key.objectid >= block_group->key.objectid +
90 block_group->key.offset) {
92 hole_size = block_group->key.objectid +
93 block_group->key.offset - last;
95 last = block_group->key.objectid;
96 hole_size = block_group->key.offset;
98 for (i = 0; i < hole_size; i++) {
99 set_radix_bit(extent_radix, last + i);
103 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
105 last = key.objectid + key.offset;
108 hole_size = key.objectid - last;
109 for (i = 0; i < hole_size; i++) {
110 set_radix_bit(extent_radix, last + i);
112 last = key.objectid + key.offset;
118 block_group->cached = 1;
120 btrfs_free_path(path);
124 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
128 struct btrfs_block_group_cache *block_group;
131 ret = radix_tree_gang_lookup(&info->block_group_radix,
132 (void **)&block_group,
135 if (block_group->key.objectid <= blocknr && blocknr <=
136 block_group->key.objectid + block_group->key.offset)
139 ret = radix_tree_gang_lookup(&info->block_group_data_radix,
140 (void **)&block_group,
143 if (block_group->key.objectid <= blocknr && blocknr <=
144 block_group->key.objectid + block_group->key.offset)
150 static u64 leaf_range(struct btrfs_root *root)
152 u64 size = BTRFS_LEAF_DATA_SIZE(root);
153 do_div(size, sizeof(struct btrfs_extent_item) +
154 sizeof(struct btrfs_item));
158 static u64 find_search_start(struct btrfs_root *root,
159 struct btrfs_block_group_cache **cache_ret,
160 u64 search_start, int num)
162 unsigned long gang[8];
164 struct btrfs_block_group_cache *cache = *cache_ret;
165 u64 last = max(search_start, cache->key.objectid);
170 ret = cache_block_group(root, cache);
174 ret = find_first_radix_bit(&root->fs_info->extent_map_radix,
175 gang, last, ARRAY_SIZE(gang));
178 last = gang[ret-1] + 1;
180 if (ret != ARRAY_SIZE(gang)) {
183 if (gang[ret-1] - gang[0] > leaf_range(root)) {
187 if (gang[0] >= cache->key.objectid + cache->key.offset) {
193 return max(cache->last_alloc, search_start);
196 cache = btrfs_lookup_block_group(root->fs_info,
197 last + cache->key.offset - 1);
199 return max((*cache_ret)->last_alloc, search_start);
201 cache = btrfs_find_block_group(root, cache,
202 last + cache->key.offset - 1, 0, 0);
207 static u64 div_factor(u64 num, int factor)
214 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
215 struct btrfs_block_group_cache
216 *hint, u64 search_start,
219 struct btrfs_block_group_cache *cache[8];
220 struct btrfs_block_group_cache *found_group = NULL;
221 struct btrfs_fs_info *info = root->fs_info;
222 struct radix_tree_root *radix;
223 struct radix_tree_root *swap_radix;
237 radix = &info->block_group_data_radix;
238 swap_radix = &info->block_group_radix;
240 radix = &info->block_group_radix;
241 swap_radix = &info->block_group_data_radix;
245 struct btrfs_block_group_cache *shint;
246 shint = btrfs_lookup_block_group(info, search_start);
247 if (shint->data == data) {
248 used = btrfs_block_group_used(&shint->item);
249 if (used + shint->pinned <
250 div_factor(shint->key.offset, factor)) {
255 if (hint && hint->data == data) {
256 used = btrfs_block_group_used(&hint->item);
257 if (used + hint->pinned <
258 div_factor(hint->key.offset, factor)) {
261 if (used >= div_factor(hint->key.offset, 8)) {
262 radix_tree_tag_clear(radix,
264 hint->key.offset - 1,
265 BTRFS_BLOCK_GROUP_AVAIL);
267 last = hint->key.offset * 3;
268 if (hint->key.objectid >= last)
269 last = max(search_start + hint->key.offset - 1,
270 hint->key.objectid - last);
272 last = hint->key.objectid + hint->key.offset;
276 hint_last = max(hint->key.objectid, search_start);
278 hint_last = search_start;
283 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
284 last, ARRAY_SIZE(cache),
285 BTRFS_BLOCK_GROUP_AVAIL);
288 for (i = 0; i < ret; i++) {
289 last = cache[i]->key.objectid +
290 cache[i]->key.offset;
291 used = btrfs_block_group_used(&cache[i]->item);
292 if (used + cache[i]->pinned <
293 div_factor(cache[i]->key.offset, factor)) {
294 found_group = cache[i];
297 if (used >= div_factor(cache[i]->key.offset, 8)) {
298 radix_tree_tag_clear(radix,
299 cache[i]->key.objectid +
300 cache[i]->key.offset - 1,
301 BTRFS_BLOCK_GROUP_AVAIL);
309 ret = radix_tree_gang_lookup(radix, (void **)cache,
310 last, ARRAY_SIZE(cache));
313 for (i = 0; i < ret; i++) {
314 last = cache[i]->key.objectid +
315 cache[i]->key.offset;
316 used = btrfs_block_group_used(&cache[i]->item);
317 if (used + cache[i]->pinned < cache[i]->key.offset) {
318 found_group = cache[i];
321 if (used >= cache[i]->key.offset) {
322 radix_tree_tag_clear(radix,
323 cache[i]->key.objectid +
324 cache[i]->key.offset - 1,
325 BTRFS_BLOCK_GROUP_AVAIL);
336 struct radix_tree_root *tmp = radix;
344 ret = radix_tree_gang_lookup(radix,
345 (void **)&found_group, 0, 1);
347 ret = radix_tree_gang_lookup(swap_radix,
348 (void **)&found_group,
357 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
358 struct btrfs_root *root,
359 u64 blocknr, u64 num_blocks)
361 struct btrfs_path *path;
363 struct btrfs_key key;
364 struct btrfs_leaf *l;
365 struct btrfs_extent_item *item;
368 path = btrfs_alloc_path();
372 key.objectid = blocknr;
374 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
375 key.offset = num_blocks;
376 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
384 l = btrfs_buffer_leaf(path->nodes[0]);
385 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
386 refs = btrfs_extent_refs(item);
387 btrfs_set_extent_refs(item, refs + 1);
388 btrfs_mark_buffer_dirty(path->nodes[0]);
390 btrfs_release_path(root->fs_info->extent_root, path);
391 btrfs_free_path(path);
392 finish_current_insert(trans, root->fs_info->extent_root);
393 del_pending_extents(trans, root->fs_info->extent_root);
397 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
398 struct btrfs_root *root)
400 finish_current_insert(trans, root->fs_info->extent_root);
401 del_pending_extents(trans, root->fs_info->extent_root);
405 static int lookup_extent_ref(struct btrfs_trans_handle *trans,
406 struct btrfs_root *root, u64 blocknr,
407 u64 num_blocks, u32 *refs)
409 struct btrfs_path *path;
411 struct btrfs_key key;
412 struct btrfs_leaf *l;
413 struct btrfs_extent_item *item;
415 path = btrfs_alloc_path();
416 key.objectid = blocknr;
417 key.offset = num_blocks;
419 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
420 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
426 l = btrfs_buffer_leaf(path->nodes[0]);
427 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
428 *refs = btrfs_extent_refs(item);
430 btrfs_free_path(path);
434 int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
435 struct btrfs_root *root)
437 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
440 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
441 struct buffer_head *buf)
444 struct btrfs_node *buf_node;
445 struct btrfs_leaf *buf_leaf;
446 struct btrfs_disk_key *key;
447 struct btrfs_file_extent_item *fi;
456 buf_node = btrfs_buffer_node(buf);
457 leaf = btrfs_is_leaf(buf_node);
458 buf_leaf = btrfs_buffer_leaf(buf);
459 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
462 key = &buf_leaf->items[i].key;
463 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
465 fi = btrfs_item_ptr(buf_leaf, i,
466 struct btrfs_file_extent_item);
467 if (btrfs_file_extent_type(fi) ==
468 BTRFS_FILE_EXTENT_INLINE)
470 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
471 if (disk_blocknr == 0)
473 ret = btrfs_inc_extent_ref(trans, root, disk_blocknr,
474 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);
491 for (i =0; i < faili; i++) {
494 key = &buf_leaf->items[i].key;
495 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
497 fi = btrfs_item_ptr(buf_leaf, i,
498 struct btrfs_file_extent_item);
499 if (btrfs_file_extent_type(fi) ==
500 BTRFS_FILE_EXTENT_INLINE)
502 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
503 if (disk_blocknr == 0)
505 err = btrfs_free_extent(trans, root, disk_blocknr,
506 btrfs_file_extent_disk_num_blocks(fi), 0);
509 blocknr = btrfs_node_blockptr(buf_node, i);
510 err = btrfs_free_extent(trans, root, blocknr, 1, 0);
517 static int write_one_cache_group(struct btrfs_trans_handle *trans,
518 struct btrfs_root *root,
519 struct btrfs_path *path,
520 struct btrfs_block_group_cache *cache)
524 struct btrfs_root *extent_root = root->fs_info->extent_root;
525 struct btrfs_block_group_item *bi;
527 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
531 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
532 struct btrfs_block_group_item);
533 memcpy(bi, &cache->item, sizeof(*bi));
534 btrfs_mark_buffer_dirty(path->nodes[0]);
535 btrfs_release_path(extent_root, path);
537 finish_current_insert(trans, extent_root);
538 pending_ret = del_pending_extents(trans, extent_root);
544 cache->last_alloc = cache->first_free;
549 static int write_dirty_block_radix(struct btrfs_trans_handle *trans,
550 struct btrfs_root *root,
551 struct radix_tree_root *radix)
553 struct btrfs_block_group_cache *cache[8];
558 struct btrfs_path *path;
559 unsigned long off = 0;
561 path = btrfs_alloc_path();
566 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
567 off, ARRAY_SIZE(cache),
568 BTRFS_BLOCK_GROUP_DIRTY);
571 for (i = 0; i < ret; i++) {
572 err = write_one_cache_group(trans, root,
575 * if we fail to write the cache group, we want
576 * to keep it marked dirty in hopes that a later
581 off = cache[i]->key.objectid +
582 cache[i]->key.offset;
586 radix_tree_tag_clear(radix, cache[i]->key.objectid +
587 cache[i]->key.offset - 1,
588 BTRFS_BLOCK_GROUP_DIRTY);
591 btrfs_free_path(path);
595 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
596 struct btrfs_root *root)
600 ret = write_dirty_block_radix(trans, root,
601 &root->fs_info->block_group_radix);
602 ret2 = write_dirty_block_radix(trans, root,
603 &root->fs_info->block_group_data_radix);
611 static int update_block_group(struct btrfs_trans_handle *trans,
612 struct btrfs_root *root,
613 u64 blocknr, u64 num, int alloc, int mark_free,
616 struct btrfs_block_group_cache *cache;
617 struct btrfs_fs_info *info = root->fs_info;
625 cache = btrfs_lookup_block_group(info, blocknr);
629 block_in_group = blocknr - cache->key.objectid;
630 WARN_ON(block_in_group > cache->key.offset);
631 radix_tree_tag_set(cache->radix, cache->key.objectid +
632 cache->key.offset - 1,
633 BTRFS_BLOCK_GROUP_DIRTY);
635 old_val = btrfs_block_group_used(&cache->item);
636 num = min(total, cache->key.offset - block_in_group);
638 if (blocknr > cache->last_alloc)
639 cache->last_alloc = blocknr;
641 for (i = 0; i < num; i++) {
642 clear_radix_bit(&info->extent_map_radix,
646 if (cache->data != data &&
647 old_val < (cache->key.offset >> 1)) {
649 radix_tree_delete(cache->radix,
650 cache->key.objectid +
651 cache->key.offset - 1);
655 &info->block_group_data_radix;
657 BTRFS_BLOCK_GROUP_DATA;
659 cache->radix = &info->block_group_radix;
661 ~BTRFS_BLOCK_GROUP_DATA;
663 ret = radix_tree_insert(cache->radix,
664 cache->key.objectid +
665 cache->key.offset - 1,
671 if (blocknr < cache->first_free)
672 cache->first_free = blocknr;
673 if (!cache->data && mark_free) {
674 for (i = 0; i < num; i++) {
675 set_radix_bit(&info->extent_map_radix,
679 if (old_val < (cache->key.offset >> 1) &&
680 old_val + num >= (cache->key.offset >> 1)) {
681 radix_tree_tag_set(cache->radix,
682 cache->key.objectid +
683 cache->key.offset - 1,
684 BTRFS_BLOCK_GROUP_AVAIL);
687 btrfs_set_block_group_used(&cache->item, old_val);
694 int btrfs_copy_pinned(struct btrfs_root *root, struct radix_tree_root *copy)
696 unsigned long gang[8];
698 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
703 ret = find_first_radix_bit(pinned_radix, gang, last,
707 for (i = 0 ; i < ret; i++) {
708 set_radix_bit(copy, gang[i]);
712 ret = find_first_radix_bit(&root->fs_info->extent_ins_radix, gang, 0,
718 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
719 struct btrfs_root *root,
720 struct radix_tree_root *unpin_radix)
722 unsigned long gang[8];
723 struct btrfs_block_group_cache *block_group;
727 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
728 struct radix_tree_root *extent_radix = &root->fs_info->extent_map_radix;
731 ret = find_first_radix_bit(unpin_radix, gang, 0,
737 for (i = 0; i < ret; i++) {
738 clear_radix_bit(pinned_radix, gang[i]);
739 clear_radix_bit(unpin_radix, gang[i]);
740 block_group = btrfs_lookup_block_group(root->fs_info,
743 WARN_ON(block_group->pinned == 0);
744 block_group->pinned--;
745 if (gang[i] < block_group->last_alloc)
746 block_group->last_alloc = gang[i];
747 if (!block_group->data)
748 set_radix_bit(extent_radix, gang[i]);
755 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
756 btrfs_root *extent_root)
758 struct btrfs_key ins;
759 struct btrfs_extent_item extent_item;
763 unsigned long gang[8];
764 struct btrfs_fs_info *info = extent_root->fs_info;
766 btrfs_set_extent_refs(&extent_item, 1);
769 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
770 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
773 ret = find_first_radix_bit(&info->extent_ins_radix, gang, 0,
778 for (i = 0; i < ret; i++) {
779 ins.objectid = gang[i];
780 err = btrfs_insert_item(trans, extent_root, &ins,
782 sizeof(extent_item));
783 clear_radix_bit(&info->extent_ins_radix, gang[i]);
790 static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
793 struct btrfs_header *header;
794 struct buffer_head *bh;
797 bh = btrfs_find_tree_block(root, blocknr);
799 if (buffer_uptodate(bh)) {
801 root->fs_info->running_transaction->transid;
802 header = btrfs_buffer_header(bh);
803 if (btrfs_header_generation(header) ==
805 btrfs_block_release(root, bh);
809 btrfs_block_release(root, bh);
811 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
813 struct btrfs_block_group_cache *cache;
814 cache = btrfs_lookup_block_group(root->fs_info,
820 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
827 * remove an extent from the root, returns 0 on success
829 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
830 *root, u64 blocknr, u64 num_blocks, int pin,
833 struct btrfs_path *path;
834 struct btrfs_key key;
835 struct btrfs_fs_info *info = root->fs_info;
836 struct btrfs_root *extent_root = info->extent_root;
838 struct btrfs_extent_item *ei;
841 key.objectid = blocknr;
843 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
844 key.offset = num_blocks;
846 path = btrfs_alloc_path();
850 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
854 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
855 struct btrfs_extent_item);
856 BUG_ON(ei->refs == 0);
857 refs = btrfs_extent_refs(ei) - 1;
858 btrfs_set_extent_refs(ei, refs);
859 btrfs_mark_buffer_dirty(path->nodes[0]);
861 u64 super_blocks_used;
864 ret = pin_down_block(root, blocknr, 0);
868 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
869 btrfs_set_super_blocks_used(&info->super_copy,
870 super_blocks_used - num_blocks);
871 ret = btrfs_del_item(trans, extent_root, path);
875 ret = update_block_group(trans, root, blocknr, num_blocks, 0,
879 btrfs_free_path(path);
880 finish_current_insert(trans, extent_root);
885 * find all the blocks marked as pending in the radix tree and remove
886 * them from the extent map
888 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
889 btrfs_root *extent_root)
894 unsigned long gang[4];
896 struct radix_tree_root *pending_radix;
897 struct radix_tree_root *pinned_radix;
898 struct btrfs_block_group_cache *cache;
900 pending_radix = &extent_root->fs_info->pending_del_radix;
901 pinned_radix = &extent_root->fs_info->pinned_radix;
904 ret = find_first_radix_bit(pending_radix, gang, 0,
908 for (i = 0; i < ret; i++) {
909 wret = set_radix_bit(pinned_radix, gang[i]);
912 btrfs_lookup_block_group(extent_root->fs_info,
918 printk(KERN_CRIT "set_radix_bit, err %d\n",
922 wret = clear_radix_bit(pending_radix, gang[i]);
924 wret = __free_extent(trans, extent_root,
934 * remove an extent from the root, returns 0 on success
936 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
937 *root, u64 blocknr, u64 num_blocks, int pin)
939 struct btrfs_root *extent_root = root->fs_info->extent_root;
943 if (root == extent_root) {
944 pin_down_block(root, blocknr, 1);
947 ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
948 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
949 return ret ? ret : pending_ret;
953 * walks the btree of allocated extents and find a hole of a given size.
954 * The key ins is changed to record the hole:
955 * ins->objectid == block start
956 * ins->flags = BTRFS_EXTENT_ITEM_KEY
957 * ins->offset == number of blocks
958 * Any available blocks before search_start are skipped.
960 static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
961 *orig_root, u64 num_blocks, u64 empty_size,
962 u64 search_start, u64 search_end, u64 hint_block,
963 struct btrfs_key *ins, u64 exclude_start,
964 u64 exclude_nr, int data)
966 struct btrfs_path *path;
967 struct btrfs_key key;
973 u64 orig_search_start = search_start;
975 struct btrfs_leaf *l;
976 struct btrfs_root * root = orig_root->fs_info->extent_root;
977 struct btrfs_fs_info *info = root->fs_info;
978 int total_needed = num_blocks;
980 struct btrfs_block_group_cache *block_group;
984 WARN_ON(num_blocks < 1);
986 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
988 level = btrfs_header_level(btrfs_buffer_header(root->node));
989 if (search_end == (u64)-1)
990 search_end = btrfs_super_total_blocks(&info->super_copy);
992 block_group = btrfs_lookup_block_group(info, hint_block);
993 block_group = btrfs_find_block_group(root, block_group,
994 hint_block, data, 1);
996 block_group = btrfs_find_block_group(root,
997 trans->block_group, 0,
1001 total_needed += empty_size;
1002 path = btrfs_alloc_path();
1005 if (!block_group->data)
1006 search_start = find_search_start(root, &block_group,
1007 search_start, total_needed);
1008 else if (!full_scan)
1009 search_start = max(block_group->last_alloc, search_start);
1011 btrfs_init_path(path);
1012 ins->objectid = search_start;
1017 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1021 if (path->slots[0] > 0) {
1025 l = btrfs_buffer_leaf(path->nodes[0]);
1026 btrfs_disk_key_to_cpu(&key, &l->items[path->slots[0]].key);
1028 * a rare case, go back one key if we hit a block group item
1029 * instead of an extent item
1031 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
1032 key.objectid + key.offset >= search_start) {
1033 ins->objectid = key.objectid;
1034 ins->offset = key.offset - 1;
1035 btrfs_release_path(root, path);
1036 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1040 if (path->slots[0] > 0) {
1046 l = btrfs_buffer_leaf(path->nodes[0]);
1047 slot = path->slots[0];
1048 if (slot >= btrfs_header_nritems(&l->header)) {
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_buffer_defrag(buf);
1260 set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
1264 static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1265 struct btrfs_root *root, struct buffer_head *cur)
1267 struct btrfs_disk_key *key;
1268 struct btrfs_leaf *leaf;
1269 struct btrfs_file_extent_item *fi;
1274 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
1275 leaf = btrfs_buffer_leaf(cur);
1276 nritems = btrfs_header_nritems(&leaf->header);
1277 for (i = 0; i < nritems; i++) {
1279 key = &leaf->items[i].key;
1280 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
1282 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1283 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
1286 * FIXME make sure to insert a trans record that
1287 * repeats the snapshot del on crash
1289 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
1290 if (disk_blocknr == 0)
1292 ret = btrfs_free_extent(trans, root, disk_blocknr,
1293 btrfs_file_extent_disk_num_blocks(fi),
1300 static void reada_walk_down(struct btrfs_root *root,
1301 struct btrfs_node *node)
1309 nritems = btrfs_header_nritems(&node->header);
1310 for (i = 0; i < nritems; i++) {
1311 blocknr = btrfs_node_blockptr(node, i);
1312 ret = lookup_extent_ref(NULL, root, blocknr, 1, &refs);
1316 mutex_unlock(&root->fs_info->fs_mutex);
1317 ret = readahead_tree_block(root, blocknr);
1319 mutex_lock(&root->fs_info->fs_mutex);
1326 * helper function for drop_snapshot, this walks down the tree dropping ref
1327 * counts as it goes.
1329 static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1330 *root, struct btrfs_path *path, int *level)
1332 struct buffer_head *next;
1333 struct buffer_head *cur;
1338 WARN_ON(*level < 0);
1339 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1340 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
1347 * walk down to the last node level and free all the leaves
1349 while(*level >= 0) {
1350 WARN_ON(*level < 0);
1351 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1352 cur = path->nodes[*level];
1354 if (*level > 0 && path->slots[*level] == 0)
1355 reada_walk_down(root, btrfs_buffer_node(cur));
1357 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
1360 if (path->slots[*level] >=
1361 btrfs_header_nritems(btrfs_buffer_header(cur)))
1364 ret = drop_leaf_ref(trans, root, cur);
1368 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
1369 path->slots[*level]);
1370 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
1373 path->slots[*level]++;
1374 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
1378 next = btrfs_find_tree_block(root, blocknr);
1379 if (!next || !buffer_uptodate(next)) {
1381 mutex_unlock(&root->fs_info->fs_mutex);
1382 next = read_tree_block(root, blocknr);
1383 mutex_lock(&root->fs_info->fs_mutex);
1385 /* we dropped the lock, check one more time */
1386 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
1389 path->slots[*level]++;
1391 ret = btrfs_free_extent(trans, root,
1397 WARN_ON(*level <= 0);
1398 if (path->nodes[*level-1])
1399 btrfs_block_release(root, path->nodes[*level-1]);
1400 path->nodes[*level-1] = next;
1401 *level = btrfs_header_level(btrfs_buffer_header(next));
1402 path->slots[*level] = 0;
1405 WARN_ON(*level < 0);
1406 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1407 ret = btrfs_free_extent(trans, root,
1408 bh_blocknr(path->nodes[*level]), 1, 1);
1409 btrfs_block_release(root, path->nodes[*level]);
1410 path->nodes[*level] = NULL;
1417 * helper for dropping snapshots. This walks back up the tree in the path
1418 * to find the first node higher up where we haven't yet gone through
1421 static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1422 *root, struct btrfs_path *path, int *level)
1427 struct btrfs_root_item *root_item = &root->root_item;
1429 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1430 slot = path->slots[i];
1431 if (slot < btrfs_header_nritems(
1432 btrfs_buffer_header(path->nodes[i])) - 1) {
1433 struct btrfs_node *node;
1434 node = btrfs_buffer_node(path->nodes[i]);
1437 WARN_ON(*level == 0);
1438 memcpy(&root_item->drop_progress,
1439 &node->ptrs[path->slots[i]].key,
1440 sizeof(root_item->drop_progress));
1441 root_item->drop_level = i;
1444 ret = btrfs_free_extent(trans, root,
1445 bh_blocknr(path->nodes[*level]),
1448 btrfs_block_release(root, path->nodes[*level]);
1449 path->nodes[*level] = NULL;
1457 * drop the reference count on the tree rooted at 'snap'. This traverses
1458 * the tree freeing any blocks that have a ref count of zero after being
1461 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
1467 struct btrfs_path *path;
1470 struct btrfs_root_item *root_item = &root->root_item;
1472 path = btrfs_alloc_path();
1475 level = btrfs_header_level(btrfs_buffer_header(root->node));
1477 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1478 path->nodes[level] = root->node;
1479 path->slots[level] = 0;
1481 struct btrfs_key key;
1482 struct btrfs_disk_key *found_key;
1483 struct btrfs_node *node;
1485 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1486 level = root_item->drop_level;
1487 path->lowest_level = level;
1488 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1493 node = btrfs_buffer_node(path->nodes[level]);
1494 found_key = &node->ptrs[path->slots[level]].key;
1495 WARN_ON(memcmp(found_key, &root_item->drop_progress,
1496 sizeof(*found_key)));
1499 wret = walk_down_tree(trans, root, path, &level);
1505 wret = walk_up_tree(trans, root, path, &level);
1514 for (i = 0; i <= orig_level; i++) {
1515 if (path->nodes[i]) {
1516 btrfs_block_release(root, path->nodes[i]);
1521 btrfs_free_path(path);
1525 static int free_block_group_radix(struct radix_tree_root *radix)
1528 struct btrfs_block_group_cache *cache[8];
1532 ret = radix_tree_gang_lookup(radix, (void **)cache, 0,
1536 for (i = 0; i < ret; i++) {
1537 radix_tree_delete(radix, cache[i]->key.objectid +
1538 cache[i]->key.offset - 1);
1545 int btrfs_free_block_groups(struct btrfs_fs_info *info)
1549 unsigned long gang[16];
1552 ret = free_block_group_radix(&info->block_group_radix);
1553 ret2 = free_block_group_radix(&info->block_group_data_radix);
1560 ret = find_first_radix_bit(&info->extent_map_radix,
1561 gang, 0, ARRAY_SIZE(gang));
1564 for (i = 0; i < ret; i++) {
1565 clear_radix_bit(&info->extent_map_radix, gang[i]);
1571 int btrfs_read_block_groups(struct btrfs_root *root)
1573 struct btrfs_path *path;
1576 struct btrfs_block_group_item *bi;
1577 struct btrfs_block_group_cache *cache;
1578 struct btrfs_fs_info *info = root->fs_info;
1579 struct radix_tree_root *radix;
1580 struct btrfs_key key;
1581 struct btrfs_key found_key;
1582 struct btrfs_leaf *leaf;
1583 u64 group_size_blocks;
1586 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE >>
1587 root->fs_info->sb->s_blocksize_bits;
1588 root = info->extent_root;
1590 key.offset = group_size_blocks;
1592 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1594 path = btrfs_alloc_path();
1599 ret = btrfs_search_slot(NULL, info->extent_root,
1605 leaf = btrfs_buffer_leaf(path->nodes[0]);
1606 btrfs_disk_key_to_cpu(&found_key,
1607 &leaf->items[path->slots[0]].key);
1608 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1614 bi = btrfs_item_ptr(leaf, path->slots[0],
1615 struct btrfs_block_group_item);
1616 if (bi->flags & BTRFS_BLOCK_GROUP_DATA) {
1617 radix = &info->block_group_data_radix;
1620 radix = &info->block_group_radix;
1624 memcpy(&cache->item, bi, sizeof(*bi));
1625 memcpy(&cache->key, &found_key, sizeof(found_key));
1626 cache->last_alloc = cache->key.objectid;
1627 cache->first_free = cache->key.objectid;
1631 cache->radix = radix;
1633 key.objectid = found_key.objectid + found_key.offset;
1634 btrfs_release_path(root, path);
1635 ret = radix_tree_insert(radix, found_key.objectid +
1636 found_key.offset - 1,
1639 used = btrfs_block_group_used(bi);
1640 if (used < div_factor(key.offset, 8)) {
1641 radix_tree_tag_set(radix, found_key.objectid +
1642 found_key.offset - 1,
1643 BTRFS_BLOCK_GROUP_AVAIL);
1646 btrfs_super_total_blocks(&info->super_copy))
1650 btrfs_free_path(path);