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 find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
26 *orig_root, u64 num_blocks, u64 search_start,
27 u64 search_end, u64 hint_block,
28 struct btrfs_key *ins, u64 exclude_start,
29 u64 exclude_nr, int data);
30 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
31 btrfs_root *extent_root);
32 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
33 btrfs_root *extent_root);
35 static int cache_block_group(struct btrfs_root *root,
36 struct btrfs_block_group_cache *block_group)
38 struct btrfs_path *path;
41 struct btrfs_leaf *leaf;
42 struct radix_tree_root *extent_radix;
50 root = root->fs_info->extent_root;
51 extent_radix = &root->fs_info->extent_map_radix;
53 if (block_group->cached)
55 if (block_group->data)
57 path = btrfs_alloc_path();
61 key.objectid = block_group->key.objectid;
64 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
65 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
68 if (ret && path->slots[0] > 0)
70 limit = block_group->key.objectid + block_group->key.offset;
72 leaf = btrfs_buffer_leaf(path->nodes[0]);
73 slot = path->slots[0];
74 if (slot >= btrfs_header_nritems(&leaf->header)) {
75 ret = btrfs_next_leaf(root, path);
82 hole_size = block_group->key.objectid +
83 block_group->key.offset - last;
85 last = block_group->key.objectid;
86 hole_size = block_group->key.offset;
88 for (i = 0; i < hole_size; i++) {
89 set_radix_bit(extent_radix,
95 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
96 if (key.objectid >= block_group->key.objectid +
97 block_group->key.offset) {
99 hole_size = block_group->key.objectid +
100 block_group->key.offset - last;
102 last = block_group->key.objectid;
103 hole_size = block_group->key.offset;
105 for (i = 0; i < hole_size; i++) {
106 set_radix_bit(extent_radix, last + i);
110 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
112 last = key.objectid + key.offset;
115 hole_size = key.objectid - last;
116 for (i = 0; i < hole_size; i++) {
117 set_radix_bit(extent_radix, last + i);
119 last = key.objectid + key.offset;
125 block_group->cached = 1;
127 btrfs_free_path(path);
131 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
135 struct btrfs_block_group_cache *block_group;
138 ret = radix_tree_gang_lookup(&info->block_group_radix,
139 (void **)&block_group,
142 if (block_group->key.objectid <= blocknr && blocknr <=
143 block_group->key.objectid + block_group->key.offset)
146 ret = radix_tree_gang_lookup(&info->block_group_data_radix,
147 (void **)&block_group,
150 if (block_group->key.objectid <= blocknr && blocknr <=
151 block_group->key.objectid + block_group->key.offset)
157 static u64 leaf_range(struct btrfs_root *root)
159 u64 size = BTRFS_LEAF_DATA_SIZE(root);
160 do_div(size, sizeof(struct btrfs_extent_item) +
161 sizeof(struct btrfs_item));
165 static u64 find_search_start(struct btrfs_root *root,
166 struct btrfs_block_group_cache **cache_ret,
167 u64 search_start, int num)
169 unsigned long gang[8];
171 struct btrfs_block_group_cache *cache = *cache_ret;
172 u64 last = max(search_start, cache->key.objectid);
177 last = max(last, cache->last_prealloc);
180 ret = cache_block_group(root, cache);
184 ret = find_first_radix_bit(&root->fs_info->extent_map_radix,
185 gang, last, ARRAY_SIZE(gang));
188 last = gang[ret-1] + 1;
190 if (ret != ARRAY_SIZE(gang)) {
193 if (gang[ret-1] - gang[0] > leaf_range(root)) {
197 if (gang[0] >= cache->key.objectid + cache->key.offset) {
203 return max(cache->last_alloc, search_start);
206 cache = btrfs_lookup_block_group(root->fs_info,
207 last + cache->key.offset - 1);
209 return max((*cache_ret)->last_alloc, search_start);
211 cache = btrfs_find_block_group(root, cache,
212 last + cache->key.offset - 1, 0, 0);
217 static u64 div_factor(u64 num, int factor)
224 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
225 struct btrfs_block_group_cache
226 *hint, u64 search_start,
229 struct btrfs_block_group_cache *cache[8];
230 struct btrfs_block_group_cache *found_group = NULL;
231 struct btrfs_fs_info *info = root->fs_info;
232 struct radix_tree_root *radix;
233 struct radix_tree_root *swap_radix;
247 radix = &info->block_group_data_radix;
248 swap_radix = &info->block_group_radix;
250 radix = &info->block_group_radix;
251 swap_radix = &info->block_group_data_radix;
255 struct btrfs_block_group_cache *shint;
256 shint = btrfs_lookup_block_group(info, search_start);
257 if (shint->data == data) {
258 used = btrfs_block_group_used(&shint->item);
259 if (used + shint->pinned <
260 div_factor(shint->key.offset, factor)) {
265 if (hint && hint->data == data) {
266 used = btrfs_block_group_used(&hint->item);
267 if (used + hint->pinned <
268 div_factor(hint->key.offset, factor)) {
271 if (used >= div_factor(hint->key.offset, 8)) {
272 radix_tree_tag_clear(radix,
274 hint->key.offset - 1,
275 BTRFS_BLOCK_GROUP_AVAIL);
277 last = hint->key.offset * 3;
278 if (hint->key.objectid >= last)
279 last = max(search_start + hint->key.offset - 1,
280 hint->key.objectid - last);
282 last = hint->key.objectid + hint->key.offset;
286 hint_last = max(hint->key.objectid, search_start);
288 hint_last = search_start;
293 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
294 last, ARRAY_SIZE(cache),
295 BTRFS_BLOCK_GROUP_AVAIL);
298 for (i = 0; i < ret; i++) {
299 last = cache[i]->key.objectid +
300 cache[i]->key.offset;
301 used = btrfs_block_group_used(&cache[i]->item);
302 if (used + cache[i]->pinned <
303 div_factor(cache[i]->key.offset, factor)) {
304 found_group = cache[i];
307 if (used >= div_factor(cache[i]->key.offset, 8)) {
308 radix_tree_tag_clear(radix,
309 cache[i]->key.objectid +
310 cache[i]->key.offset - 1,
311 BTRFS_BLOCK_GROUP_AVAIL);
319 ret = radix_tree_gang_lookup(radix, (void **)cache,
320 last, ARRAY_SIZE(cache));
323 for (i = 0; i < ret; i++) {
324 last = cache[i]->key.objectid +
325 cache[i]->key.offset;
326 used = btrfs_block_group_used(&cache[i]->item);
327 if (used + cache[i]->pinned < cache[i]->key.offset) {
328 found_group = cache[i];
331 if (used >= cache[i]->key.offset) {
332 radix_tree_tag_clear(radix,
333 cache[i]->key.objectid +
334 cache[i]->key.offset - 1,
335 BTRFS_BLOCK_GROUP_AVAIL);
346 struct radix_tree_root *tmp = radix;
354 ret = radix_tree_gang_lookup(radix,
355 (void **)&found_group, 0, 1);
357 ret = radix_tree_gang_lookup(swap_radix,
358 (void **)&found_group,
367 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
368 struct btrfs_root *root,
369 u64 blocknr, u64 num_blocks)
371 struct btrfs_path *path;
373 struct btrfs_key key;
374 struct btrfs_leaf *l;
375 struct btrfs_extent_item *item;
376 struct btrfs_key ins;
379 path = btrfs_alloc_path();
382 ret = find_free_extent(trans, root->fs_info->extent_root, 0, 0,
383 (u64)-1, 0, &ins, 0, 0, 0);
385 btrfs_free_path(path);
388 key.objectid = blocknr;
390 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
391 key.offset = num_blocks;
392 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
400 l = btrfs_buffer_leaf(path->nodes[0]);
401 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
402 refs = btrfs_extent_refs(item);
403 btrfs_set_extent_refs(item, refs + 1);
404 btrfs_mark_buffer_dirty(path->nodes[0]);
406 btrfs_release_path(root->fs_info->extent_root, path);
407 btrfs_free_path(path);
408 finish_current_insert(trans, root->fs_info->extent_root);
409 del_pending_extents(trans, root->fs_info->extent_root);
413 static int lookup_extent_ref(struct btrfs_trans_handle *trans,
414 struct btrfs_root *root, u64 blocknr,
415 u64 num_blocks, u32 *refs)
417 struct btrfs_path *path;
419 struct btrfs_key key;
420 struct btrfs_leaf *l;
421 struct btrfs_extent_item *item;
423 path = btrfs_alloc_path();
424 key.objectid = blocknr;
425 key.offset = num_blocks;
427 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
428 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
434 l = btrfs_buffer_leaf(path->nodes[0]);
435 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
436 *refs = btrfs_extent_refs(item);
438 btrfs_free_path(path);
442 int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
443 struct btrfs_root *root)
445 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
448 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
449 struct buffer_head *buf)
452 struct btrfs_node *buf_node;
453 struct btrfs_leaf *buf_leaf;
454 struct btrfs_disk_key *key;
455 struct btrfs_file_extent_item *fi;
464 buf_node = btrfs_buffer_node(buf);
465 leaf = btrfs_is_leaf(buf_node);
466 buf_leaf = btrfs_buffer_leaf(buf);
467 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
470 key = &buf_leaf->items[i].key;
471 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
473 fi = btrfs_item_ptr(buf_leaf, i,
474 struct btrfs_file_extent_item);
475 if (btrfs_file_extent_type(fi) ==
476 BTRFS_FILE_EXTENT_INLINE)
478 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
479 if (disk_blocknr == 0)
481 ret = btrfs_inc_extent_ref(trans, root, disk_blocknr,
482 btrfs_file_extent_disk_num_blocks(fi));
488 blocknr = btrfs_node_blockptr(buf_node, i);
489 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
499 for (i =0; i < faili; i++) {
502 key = &buf_leaf->items[i].key;
503 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
505 fi = btrfs_item_ptr(buf_leaf, i,
506 struct btrfs_file_extent_item);
507 if (btrfs_file_extent_type(fi) ==
508 BTRFS_FILE_EXTENT_INLINE)
510 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
511 if (disk_blocknr == 0)
513 err = btrfs_free_extent(trans, root, disk_blocknr,
514 btrfs_file_extent_disk_num_blocks(fi), 0);
517 blocknr = btrfs_node_blockptr(buf_node, i);
518 err = btrfs_free_extent(trans, root, blocknr, 1, 0);
525 static int write_one_cache_group(struct btrfs_trans_handle *trans,
526 struct btrfs_root *root,
527 struct btrfs_path *path,
528 struct btrfs_block_group_cache *cache)
532 struct btrfs_root *extent_root = root->fs_info->extent_root;
533 struct btrfs_block_group_item *bi;
534 struct btrfs_key ins;
536 ret = find_free_extent(trans, extent_root, 0, 0, (u64)-1, 0, &ins,
538 /* FIXME, set bit to recalc cache groups on next mount */
541 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
545 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
546 struct btrfs_block_group_item);
547 memcpy(bi, &cache->item, sizeof(*bi));
548 btrfs_mark_buffer_dirty(path->nodes[0]);
549 btrfs_release_path(extent_root, path);
551 finish_current_insert(trans, extent_root);
552 pending_ret = del_pending_extents(trans, extent_root);
558 cache->last_alloc = cache->first_free;
563 static int write_dirty_block_radix(struct btrfs_trans_handle *trans,
564 struct btrfs_root *root,
565 struct radix_tree_root *radix)
567 struct btrfs_block_group_cache *cache[8];
572 struct btrfs_path *path;
573 unsigned long off = 0;
575 path = btrfs_alloc_path();
580 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
581 off, ARRAY_SIZE(cache),
582 BTRFS_BLOCK_GROUP_DIRTY);
585 for (i = 0; i < ret; i++) {
586 err = write_one_cache_group(trans, root,
589 * if we fail to write the cache group, we want
590 * to keep it marked dirty in hopes that a later
595 off = cache[i]->key.objectid +
596 cache[i]->key.offset;
600 radix_tree_tag_clear(radix, cache[i]->key.objectid +
601 cache[i]->key.offset - 1,
602 BTRFS_BLOCK_GROUP_DIRTY);
605 btrfs_free_path(path);
609 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
610 struct btrfs_root *root)
614 ret = write_dirty_block_radix(trans, root,
615 &root->fs_info->block_group_radix);
616 ret2 = write_dirty_block_radix(trans, root,
617 &root->fs_info->block_group_data_radix);
625 static int update_block_group(struct btrfs_trans_handle *trans,
626 struct btrfs_root *root,
627 u64 blocknr, u64 num, int alloc, int mark_free,
630 struct btrfs_block_group_cache *cache;
631 struct btrfs_fs_info *info = root->fs_info;
639 cache = btrfs_lookup_block_group(info, blocknr);
643 block_in_group = blocknr - cache->key.objectid;
644 WARN_ON(block_in_group > cache->key.offset);
645 radix_tree_tag_set(cache->radix, cache->key.objectid +
646 cache->key.offset - 1,
647 BTRFS_BLOCK_GROUP_DIRTY);
649 old_val = btrfs_block_group_used(&cache->item);
650 num = min(total, cache->key.offset - block_in_group);
652 if (blocknr > cache->last_alloc)
653 cache->last_alloc = blocknr;
655 for (i = 0; i < num; i++) {
656 clear_radix_bit(&info->extent_map_radix,
660 if (cache->data != data &&
661 old_val < (cache->key.offset >> 1)) {
663 radix_tree_delete(cache->radix,
664 cache->key.objectid +
665 cache->key.offset - 1);
669 &info->block_group_data_radix;
671 BTRFS_BLOCK_GROUP_DATA;
673 cache->radix = &info->block_group_radix;
675 ~BTRFS_BLOCK_GROUP_DATA;
677 ret = radix_tree_insert(cache->radix,
678 cache->key.objectid +
679 cache->key.offset - 1,
685 if (blocknr < cache->first_free)
686 cache->first_free = blocknr;
687 if (!cache->data && mark_free) {
688 for (i = 0; i < num; i++) {
689 set_radix_bit(&info->extent_map_radix,
693 if (old_val < (cache->key.offset >> 1) &&
694 old_val + num >= (cache->key.offset >> 1)) {
695 radix_tree_tag_set(cache->radix,
696 cache->key.objectid +
697 cache->key.offset - 1,
698 BTRFS_BLOCK_GROUP_AVAIL);
701 btrfs_set_block_group_used(&cache->item, old_val);
708 static int try_remove_page(struct address_space *mapping, unsigned long index)
711 ret = invalidate_mapping_pages(mapping, index, index);
715 int btrfs_copy_pinned(struct btrfs_root *root, struct radix_tree_root *copy)
717 unsigned long gang[8];
719 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
724 ret = find_first_radix_bit(pinned_radix, gang, last,
728 for (i = 0 ; i < ret; i++) {
729 set_radix_bit(copy, gang[i]);
736 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
737 struct btrfs_root *root,
738 struct radix_tree_root *unpin_radix)
740 unsigned long gang[8];
741 struct inode *btree_inode = root->fs_info->btree_inode;
742 struct btrfs_block_group_cache *block_group;
746 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
747 struct radix_tree_root *extent_radix = &root->fs_info->extent_map_radix;
750 ret = find_first_radix_bit(unpin_radix, gang, 0,
756 for (i = 0; i < ret; i++) {
757 clear_radix_bit(pinned_radix, gang[i]);
758 clear_radix_bit(unpin_radix, gang[i]);
759 block_group = btrfs_lookup_block_group(root->fs_info,
762 WARN_ON(block_group->pinned == 0);
763 block_group->pinned--;
764 if (gang[i] < block_group->last_alloc)
765 block_group->last_alloc = gang[i];
766 if (gang[i] < block_group->last_prealloc)
767 block_group->last_prealloc = gang[i];
768 if (!block_group->data)
769 set_radix_bit(extent_radix, gang[i]);
771 try_remove_page(btree_inode->i_mapping,
772 gang[i] << (PAGE_CACHE_SHIFT -
773 btree_inode->i_blkbits));
779 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
780 btrfs_root *extent_root)
782 struct btrfs_key ins;
783 struct btrfs_extent_item extent_item;
786 u64 super_blocks_used;
787 struct btrfs_fs_info *info = extent_root->fs_info;
789 btrfs_set_extent_refs(&extent_item, 1);
792 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
793 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
795 for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
796 ins.objectid = extent_root->fs_info->extent_tree_insert[i];
797 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
798 btrfs_set_super_blocks_used(&info->super_copy,
799 super_blocks_used + 1);
800 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
801 sizeof(extent_item));
804 extent_root->fs_info->extent_tree_insert_nr = 0;
808 static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
811 struct btrfs_header *header;
812 struct buffer_head *bh;
815 bh = btrfs_find_tree_block(root, blocknr);
817 if (buffer_uptodate(bh)) {
819 root->fs_info->running_transaction->transid;
820 header = btrfs_buffer_header(bh);
821 if (btrfs_header_generation(header) ==
823 btrfs_block_release(root, bh);
827 btrfs_block_release(root, bh);
829 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
831 struct btrfs_block_group_cache *cache;
832 cache = btrfs_lookup_block_group(root->fs_info,
838 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
845 * remove an extent from the root, returns 0 on success
847 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
848 *root, u64 blocknr, u64 num_blocks, int pin,
851 struct btrfs_path *path;
852 struct btrfs_key key;
853 struct btrfs_fs_info *info = root->fs_info;
854 struct btrfs_root *extent_root = info->extent_root;
856 struct btrfs_extent_item *ei;
857 struct btrfs_key ins;
860 key.objectid = blocknr;
862 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
863 key.offset = num_blocks;
865 path = btrfs_alloc_path();
869 ret = find_free_extent(trans, root, 0, 0, (u64)-1, 0, &ins, 0, 0, 0);
871 btrfs_free_path(path);
875 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
879 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
880 struct btrfs_extent_item);
881 BUG_ON(ei->refs == 0);
882 refs = btrfs_extent_refs(ei) - 1;
883 btrfs_set_extent_refs(ei, refs);
884 btrfs_mark_buffer_dirty(path->nodes[0]);
886 u64 super_blocks_used;
889 ret = pin_down_block(root, blocknr, 0);
893 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
894 btrfs_set_super_blocks_used(&info->super_copy,
895 super_blocks_used - num_blocks);
896 ret = btrfs_del_item(trans, extent_root, path);
900 ret = update_block_group(trans, root, blocknr, num_blocks, 0,
904 btrfs_free_path(path);
905 finish_current_insert(trans, extent_root);
910 * find all the blocks marked as pending in the radix tree and remove
911 * them from the extent map
913 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
914 btrfs_root *extent_root)
919 unsigned long gang[4];
921 struct radix_tree_root *pending_radix;
922 struct radix_tree_root *pinned_radix;
923 struct btrfs_block_group_cache *cache;
925 pending_radix = &extent_root->fs_info->pending_del_radix;
926 pinned_radix = &extent_root->fs_info->pinned_radix;
929 ret = find_first_radix_bit(pending_radix, gang, 0,
933 for (i = 0; i < ret; i++) {
934 wret = set_radix_bit(pinned_radix, gang[i]);
937 btrfs_lookup_block_group(extent_root->fs_info,
943 printk(KERN_CRIT "set_radix_bit, err %d\n",
947 wret = clear_radix_bit(pending_radix, gang[i]);
949 wret = __free_extent(trans, extent_root,
959 * remove an extent from the root, returns 0 on success
961 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
962 *root, u64 blocknr, u64 num_blocks, int pin)
964 struct btrfs_root *extent_root = root->fs_info->extent_root;
968 if (root == extent_root) {
969 pin_down_block(root, blocknr, 1);
972 ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
973 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
974 return ret ? ret : pending_ret;
978 * walks the btree of allocated extents and find a hole of a given size.
979 * The key ins is changed to record the hole:
980 * ins->objectid == block start
981 * ins->flags = BTRFS_EXTENT_ITEM_KEY
982 * ins->offset == number of blocks
983 * Any available blocks before search_start are skipped.
985 static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
986 *orig_root, u64 num_blocks, u64 search_start, u64
987 search_end, u64 hint_block,
988 struct btrfs_key *ins, u64 exclude_start,
989 u64 exclude_nr, int data)
991 struct btrfs_path *path;
992 struct btrfs_key key;
998 u64 orig_search_start = search_start;
1000 struct btrfs_leaf *l;
1001 struct btrfs_root * root = orig_root->fs_info->extent_root;
1002 struct btrfs_fs_info *info = root->fs_info;
1003 int total_needed = num_blocks;
1004 int total_found = 0;
1005 int fill_prealloc = 0;
1007 struct btrfs_block_group_cache *block_group;
1013 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1015 level = btrfs_header_level(btrfs_buffer_header(root->node));
1016 if (num_blocks == 0) {
1019 total_needed = (min(level + 1, BTRFS_MAX_LEVEL) + 2) * 3;
1021 if (fill_prealloc) {
1023 int nr = info->extent_tree_prealloc_nr;
1024 first = info->extent_tree_prealloc[nr - 1];
1025 if (info->extent_tree_prealloc_nr >= total_needed &&
1026 first >= search_start) {
1027 ins->objectid = info->extent_tree_prealloc[0];
1031 info->extent_tree_prealloc_nr = 0;
1033 if (search_end == (u64)-1)
1034 search_end = btrfs_super_total_blocks(&info->super_copy);
1036 block_group = btrfs_lookup_block_group(info, hint_block);
1037 block_group = btrfs_find_block_group(root, block_group,
1038 hint_block, data, 1);
1040 block_group = btrfs_find_block_group(root,
1041 trans->block_group, 0,
1045 path = btrfs_alloc_path();
1048 if (!block_group->data)
1049 search_start = find_search_start(root, &block_group,
1050 search_start, total_needed);
1051 else if (!full_scan)
1052 search_start = max(block_group->last_alloc, search_start);
1054 btrfs_init_path(path);
1055 ins->objectid = search_start;
1059 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1063 if (path->slots[0] > 0) {
1067 l = btrfs_buffer_leaf(path->nodes[0]);
1068 btrfs_disk_key_to_cpu(&key, &l->items[path->slots[0]].key);
1070 * a rare case, go back one key if we hit a block group item
1071 * instead of an extent item
1073 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
1074 key.objectid + key.offset >= search_start) {
1075 ins->objectid = key.objectid;
1076 ins->offset = key.offset - 1;
1077 btrfs_release_path(root, path);
1078 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1082 if (path->slots[0] > 0) {
1088 l = btrfs_buffer_leaf(path->nodes[0]);
1089 slot = path->slots[0];
1090 if (slot >= btrfs_header_nritems(&l->header)) {
1091 if (fill_prealloc) {
1092 info->extent_tree_prealloc_nr = 0;
1096 limit = last_block +
1097 (block_group->key.offset >> 1);
1099 limit = search_start +
1100 (block_group->key.offset >> 1);
1101 ret = btrfs_next_leaf(root, path);
1107 ins->objectid = search_start;
1108 ins->offset = search_end - search_start;
1112 ins->objectid = last_block > search_start ?
1113 last_block : search_start;
1114 ins->offset = search_end - ins->objectid;
1118 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
1119 if (key.objectid >= search_start && key.objectid > last_block &&
1121 if (last_block < search_start)
1122 last_block = search_start;
1123 hole_size = key.objectid - last_block;
1124 if (hole_size >= num_blocks) {
1125 ins->objectid = last_block;
1126 ins->offset = hole_size;
1131 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
1135 last_block = key.objectid + key.offset;
1136 if (!full_scan && last_block >= block_group->key.objectid +
1137 block_group->key.offset) {
1138 btrfs_release_path(root, path);
1139 search_start = block_group->key.objectid +
1140 block_group->key.offset * 2;
1148 /* we have to make sure we didn't find an extent that has already
1149 * been allocated by the map tree or the original allocation
1151 btrfs_release_path(root, path);
1152 BUG_ON(ins->objectid < search_start);
1154 if (ins->objectid + num_blocks >= search_end) {
1159 search_start = orig_search_start;
1166 for (test_block = ins->objectid;
1167 test_block < ins->objectid + num_blocks; test_block++) {
1168 if (test_radix_bit(&info->pinned_radix, test_block)) {
1169 search_start = test_block + 1;
1173 if (!fill_prealloc && info->extent_tree_insert_nr) {
1175 info->extent_tree_insert[info->extent_tree_insert_nr - 1];
1176 if (ins->objectid + num_blocks >
1177 info->extent_tree_insert[0] &&
1178 ins->objectid <= last) {
1179 search_start = last + 1;
1180 WARN_ON(!full_scan);
1184 if (!fill_prealloc && info->extent_tree_prealloc_nr) {
1186 info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
1187 if (ins->objectid + num_blocks > first &&
1188 ins->objectid <= info->extent_tree_prealloc[0]) {
1189 search_start = info->extent_tree_prealloc[0] + 1;
1193 if (exclude_nr > 0 && (ins->objectid + num_blocks > exclude_start &&
1194 ins->objectid < exclude_start + exclude_nr)) {
1195 search_start = exclude_start + exclude_nr;
1198 if (fill_prealloc) {
1200 test_block = ins->objectid;
1201 if (test_block - info->extent_tree_prealloc[total_needed - 1] >=
1204 info->extent_tree_prealloc_nr = total_found;
1206 while(test_block < ins->objectid + ins->offset &&
1207 total_found < total_needed) {
1208 nr = total_needed - total_found - 1;
1210 info->extent_tree_prealloc[nr] = test_block;
1214 if (total_found < total_needed) {
1215 search_start = test_block;
1218 info->extent_tree_prealloc_nr = total_found;
1221 block_group = btrfs_lookup_block_group(info, ins->objectid);
1224 block_group->last_prealloc =
1225 info->extent_tree_prealloc[total_needed-1];
1227 trans->block_group = block_group;
1230 ins->offset = num_blocks;
1231 btrfs_free_path(path);
1235 if (search_start + num_blocks >= search_end) {
1236 search_start = orig_search_start;
1246 block_group = btrfs_lookup_block_group(info, search_start);
1249 block_group = btrfs_find_block_group(root, block_group,
1250 search_start, data, 0);
1254 btrfs_release_path(root, path);
1255 btrfs_free_path(path);
1259 * finds a free extent and does all the dirty work required for allocation
1260 * returns the key for the extent through ins, and a tree buffer for
1261 * the first block of the extent through buf.
1263 * returns 0 if everything worked, non-zero otherwise.
1265 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1266 struct btrfs_root *root, u64 owner,
1267 u64 num_blocks, u64 hint_block,
1268 u64 search_end, struct btrfs_key *ins, int data)
1272 u64 super_blocks_used;
1273 u64 search_start = 0;
1274 u64 exclude_start = 0;
1276 struct btrfs_fs_info *info = root->fs_info;
1277 struct btrfs_root *extent_root = info->extent_root;
1278 struct btrfs_extent_item extent_item;
1279 struct btrfs_key prealloc_key;
1281 btrfs_set_extent_refs(&extent_item, 1);
1282 btrfs_set_extent_owner(&extent_item, owner);
1284 if (root == extent_root) {
1286 BUG_ON(info->extent_tree_prealloc_nr == 0);
1287 BUG_ON(num_blocks != 1);
1289 info->extent_tree_prealloc_nr--;
1290 nr = info->extent_tree_prealloc_nr;
1291 ins->objectid = info->extent_tree_prealloc[nr];
1292 info->extent_tree_insert[info->extent_tree_insert_nr++] =
1294 ret = update_block_group(trans, root,
1295 ins->objectid, ins->offset, 1, 0, 0);
1301 * if we're doing a data allocation, preallocate room in the
1302 * extent tree first. This way the extent tree blocks end up
1303 * in the correct block group.
1306 ret = find_free_extent(trans, root, 0, 0,
1307 search_end, 0, &prealloc_key, 0, 0, 0);
1311 exclude_nr = info->extent_tree_prealloc_nr;
1312 exclude_start = info->extent_tree_prealloc[exclude_nr - 1];
1315 /* do the real allocation */
1316 ret = find_free_extent(trans, root, num_blocks, search_start,
1317 search_end, hint_block, ins,
1318 exclude_start, exclude_nr, data);
1324 * if we're doing a metadata allocation, preallocate space in the
1325 * extent tree second. This way, we don't create a tiny hole
1326 * in the allocation map between any unused preallocation blocks
1327 * and the metadata block we're actually allocating. On disk,
1329 * [block we've allocated], [used prealloc 1], [ unused prealloc ]
1330 * The unused prealloc will get reused the next time around.
1333 exclude_start = ins->objectid;
1334 exclude_nr = ins->offset;
1335 hint_block = exclude_start + exclude_nr;
1336 ret = find_free_extent(trans, root, 0, search_start,
1337 search_end, hint_block,
1338 &prealloc_key, exclude_start,
1345 super_blocks_used = btrfs_super_blocks_used(&info->super_copy);
1346 btrfs_set_super_blocks_used(&info->super_copy, super_blocks_used +
1348 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1349 sizeof(extent_item));
1352 finish_current_insert(trans, extent_root);
1353 pending_ret = del_pending_extents(trans, extent_root);
1360 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1367 * helper function to allocate a block for a given tree
1368 * returns the tree buffer or NULL.
1370 struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1371 struct btrfs_root *root, u64 hint)
1373 struct btrfs_key ins;
1375 struct buffer_head *buf;
1377 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
1378 1, hint, (unsigned long)-1, &ins, 0);
1381 return ERR_PTR(ret);
1383 buf = btrfs_find_create_tree_block(root, ins.objectid);
1385 btrfs_free_extent(trans, root, ins.objectid, 1, 0);
1386 return ERR_PTR(-ENOMEM);
1388 set_buffer_uptodate(buf);
1389 set_buffer_checked(buf);
1390 set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
1394 static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1395 struct btrfs_root *root, struct buffer_head *cur)
1397 struct btrfs_disk_key *key;
1398 struct btrfs_leaf *leaf;
1399 struct btrfs_file_extent_item *fi;
1404 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
1405 leaf = btrfs_buffer_leaf(cur);
1406 nritems = btrfs_header_nritems(&leaf->header);
1407 for (i = 0; i < nritems; i++) {
1409 key = &leaf->items[i].key;
1410 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
1412 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1413 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
1416 * FIXME make sure to insert a trans record that
1417 * repeats the snapshot del on crash
1419 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
1420 if (disk_blocknr == 0)
1422 ret = btrfs_free_extent(trans, root, disk_blocknr,
1423 btrfs_file_extent_disk_num_blocks(fi),
1430 static void reada_walk_down(struct btrfs_root *root,
1431 struct btrfs_node *node)
1439 nritems = btrfs_header_nritems(&node->header);
1440 for (i = 0; i < nritems; i++) {
1441 blocknr = btrfs_node_blockptr(node, i);
1442 ret = lookup_extent_ref(NULL, root, blocknr, 1, &refs);
1446 ret = readahead_tree_block(root, blocknr);
1453 * helper function for drop_snapshot, this walks down the tree dropping ref
1454 * counts as it goes.
1456 static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1457 *root, struct btrfs_path *path, int *level)
1459 struct buffer_head *next;
1460 struct buffer_head *cur;
1465 WARN_ON(*level < 0);
1466 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1467 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
1474 * walk down to the last node level and free all the leaves
1476 while(*level >= 0) {
1477 WARN_ON(*level < 0);
1478 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1479 cur = path->nodes[*level];
1481 if (*level > 0 && path->slots[*level] == 0)
1482 reada_walk_down(root, btrfs_buffer_node(cur));
1484 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
1487 if (path->slots[*level] >=
1488 btrfs_header_nritems(btrfs_buffer_header(cur)))
1491 ret = drop_leaf_ref(trans, root, cur);
1495 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
1496 path->slots[*level]);
1497 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
1500 path->slots[*level]++;
1501 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
1505 next = read_tree_block(root, blocknr);
1506 WARN_ON(*level <= 0);
1507 if (path->nodes[*level-1])
1508 btrfs_block_release(root, path->nodes[*level-1]);
1509 path->nodes[*level-1] = next;
1510 *level = btrfs_header_level(btrfs_buffer_header(next));
1511 path->slots[*level] = 0;
1514 WARN_ON(*level < 0);
1515 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1516 ret = btrfs_free_extent(trans, root,
1517 bh_blocknr(path->nodes[*level]), 1, 1);
1518 btrfs_block_release(root, path->nodes[*level]);
1519 path->nodes[*level] = NULL;
1526 * helper for dropping snapshots. This walks back up the tree in the path
1527 * to find the first node higher up where we haven't yet gone through
1530 static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1531 *root, struct btrfs_path *path, int *level)
1536 struct btrfs_root_item *root_item = &root->root_item;
1538 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1539 slot = path->slots[i];
1540 if (slot < btrfs_header_nritems(
1541 btrfs_buffer_header(path->nodes[i])) - 1) {
1542 struct btrfs_node *node;
1543 node = btrfs_buffer_node(path->nodes[i]);
1546 WARN_ON(*level == 0);
1547 memcpy(&root_item->drop_progress,
1548 &node->ptrs[path->slots[i]].key,
1549 sizeof(root_item->drop_progress));
1550 root_item->drop_level = i;
1553 ret = btrfs_free_extent(trans, root,
1554 bh_blocknr(path->nodes[*level]),
1557 btrfs_block_release(root, path->nodes[*level]);
1558 path->nodes[*level] = NULL;
1566 * drop the reference count on the tree rooted at 'snap'. This traverses
1567 * the tree freeing any blocks that have a ref count of zero after being
1570 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
1576 struct btrfs_path *path;
1580 struct btrfs_root_item *root_item = &root->root_item;
1582 path = btrfs_alloc_path();
1585 level = btrfs_header_level(btrfs_buffer_header(root->node));
1587 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1588 path->nodes[level] = root->node;
1589 path->slots[level] = 0;
1591 struct btrfs_key key;
1592 struct btrfs_disk_key *found_key;
1593 struct btrfs_node *node;
1594 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1595 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1600 level = root_item->drop_level;
1601 node = btrfs_buffer_node(path->nodes[level]);
1602 found_key = &node->ptrs[path->slots[level]].key;
1603 WARN_ON(memcmp(found_key, &root_item->drop_progress,
1604 sizeof(*found_key)));
1607 wret = walk_down_tree(trans, root, path, &level);
1613 wret = walk_up_tree(trans, root, path, &level);
1619 if (num_walks > 10) {
1620 struct btrfs_key key;
1621 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1627 for (i = 0; i <= orig_level; i++) {
1628 if (path->nodes[i]) {
1629 btrfs_block_release(root, path->nodes[i]);
1633 btrfs_free_path(path);
1637 static int free_block_group_radix(struct radix_tree_root *radix)
1640 struct btrfs_block_group_cache *cache[8];
1644 ret = radix_tree_gang_lookup(radix, (void **)cache, 0,
1648 for (i = 0; i < ret; i++) {
1649 radix_tree_delete(radix, cache[i]->key.objectid +
1650 cache[i]->key.offset - 1);
1657 int btrfs_free_block_groups(struct btrfs_fs_info *info)
1661 unsigned long gang[16];
1664 ret = free_block_group_radix(&info->block_group_radix);
1665 ret2 = free_block_group_radix(&info->block_group_data_radix);
1672 ret = find_first_radix_bit(&info->extent_map_radix,
1673 gang, 0, ARRAY_SIZE(gang));
1676 for (i = 0; i < ret; i++) {
1677 clear_radix_bit(&info->extent_map_radix, gang[i]);
1683 int btrfs_read_block_groups(struct btrfs_root *root)
1685 struct btrfs_path *path;
1688 struct btrfs_block_group_item *bi;
1689 struct btrfs_block_group_cache *cache;
1690 struct btrfs_fs_info *info = root->fs_info;
1691 struct radix_tree_root *radix;
1692 struct btrfs_key key;
1693 struct btrfs_key found_key;
1694 struct btrfs_leaf *leaf;
1695 u64 group_size_blocks;
1698 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE >>
1699 root->fs_info->sb->s_blocksize_bits;
1700 root = info->extent_root;
1702 key.offset = group_size_blocks;
1704 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1706 path = btrfs_alloc_path();
1711 ret = btrfs_search_slot(NULL, info->extent_root,
1717 leaf = btrfs_buffer_leaf(path->nodes[0]);
1718 btrfs_disk_key_to_cpu(&found_key,
1719 &leaf->items[path->slots[0]].key);
1720 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1726 bi = btrfs_item_ptr(leaf, path->slots[0],
1727 struct btrfs_block_group_item);
1728 if (bi->flags & BTRFS_BLOCK_GROUP_DATA) {
1729 radix = &info->block_group_data_radix;
1732 radix = &info->block_group_radix;
1736 memcpy(&cache->item, bi, sizeof(*bi));
1737 memcpy(&cache->key, &found_key, sizeof(found_key));
1738 cache->last_alloc = cache->key.objectid;
1739 cache->first_free = cache->key.objectid;
1740 cache->last_prealloc = cache->key.objectid;
1744 cache->radix = radix;
1746 key.objectid = found_key.objectid + found_key.offset;
1747 btrfs_release_path(root, path);
1748 ret = radix_tree_insert(radix, found_key.objectid +
1749 found_key.offset - 1,
1752 used = btrfs_block_group_used(bi);
1753 if (used < div_factor(key.offset, 8)) {
1754 radix_tree_tag_set(radix, found_key.objectid +
1755 found_key.offset - 1,
1756 BTRFS_BLOCK_GROUP_AVAIL);
1759 btrfs_super_total_blocks(&info->super_copy))
1763 btrfs_free_path(path);