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/module.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, int data);
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 void reada_extent_leaves(struct btrfs_root *root,
35 struct btrfs_path *path, u64 limit)
37 struct btrfs_node *node;
47 node = btrfs_buffer_node(path->nodes[1]);
48 slot = path->slots[1] + 1;
49 nritems = btrfs_header_nritems(&node->header);
50 for (i = slot; i < nritems && i < slot + 8; i++) {
51 item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
52 if (item_objectid > limit)
54 blocknr = btrfs_node_blockptr(node, i);
55 ret = readahead_tree_block(root, blocknr);
61 static int cache_block_group(struct btrfs_root *root,
62 struct btrfs_block_group_cache *block_group)
64 struct btrfs_path *path;
67 struct btrfs_leaf *leaf;
68 struct radix_tree_root *extent_radix;
76 root = root->fs_info->extent_root;
77 extent_radix = &root->fs_info->extent_map_radix;
79 if (block_group->cached)
81 if (block_group->data)
83 path = btrfs_alloc_path();
86 key.objectid = block_group->key.objectid;
89 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
90 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
93 if (ret && path->slots[0] > 0)
95 limit = block_group->key.objectid + block_group->key.offset;
96 reada_extent_leaves(root, path, limit);
98 leaf = btrfs_buffer_leaf(path->nodes[0]);
99 slot = path->slots[0];
100 if (slot >= btrfs_header_nritems(&leaf->header)) {
101 reada_extent_leaves(root, path, limit);
102 ret = btrfs_next_leaf(root, path);
107 hole_size = block_group->key.objectid +
108 block_group->key.offset - last;
110 last = block_group->key.objectid;
111 hole_size = block_group->key.offset;
113 for (i = 0; i < hole_size; i++) {
114 set_radix_bit(extent_radix,
120 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
121 if (key.objectid >= block_group->key.objectid +
122 block_group->key.offset) {
124 hole_size = block_group->key.objectid +
125 block_group->key.offset - last;
127 last = block_group->key.objectid;
128 hole_size = block_group->key.offset;
130 for (i = 0; i < hole_size; i++) {
131 set_radix_bit(extent_radix, last + i);
135 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
137 last = key.objectid + key.offset;
140 hole_size = key.objectid - last;
141 for (i = 0; i < hole_size; i++) {
142 set_radix_bit(extent_radix, last + i);
144 last = key.objectid + key.offset;
150 block_group->cached = 1;
151 btrfs_free_path(path);
155 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
159 struct btrfs_block_group_cache *block_group;
162 ret = radix_tree_gang_lookup(&info->block_group_radix,
163 (void **)&block_group,
166 if (block_group->key.objectid <= blocknr && blocknr <=
167 block_group->key.objectid + block_group->key.offset)
170 ret = radix_tree_gang_lookup(&info->block_group_data_radix,
171 (void **)&block_group,
174 if (block_group->key.objectid <= blocknr && blocknr <=
175 block_group->key.objectid + block_group->key.offset)
181 static u64 leaf_range(struct btrfs_root *root)
183 u64 size = BTRFS_LEAF_DATA_SIZE(root);
184 do_div(size, sizeof(struct btrfs_extent_item) +
185 sizeof(struct btrfs_item));
189 static u64 find_search_start(struct btrfs_root *root,
190 struct btrfs_block_group_cache **cache_ret,
191 u64 search_start, int num)
193 unsigned long gang[8];
195 struct btrfs_block_group_cache *cache = *cache_ret;
196 u64 last = max(search_start, cache->key.objectid);
201 last = max(last, cache->last_prealloc);
204 cache_block_group(root, cache);
206 ret = find_first_radix_bit(&root->fs_info->extent_map_radix,
207 gang, last, ARRAY_SIZE(gang));
210 last = gang[ret-1] + 1;
212 if (ret != ARRAY_SIZE(gang)) {
215 if (gang[ret-1] - gang[0] > leaf_range(root)) {
219 if (gang[0] >= cache->key.objectid + cache->key.offset) {
225 return max(cache->last_alloc, search_start);
228 cache = btrfs_lookup_block_group(root->fs_info,
229 last + cache->key.offset - 1);
231 return max((*cache_ret)->last_alloc, search_start);
233 cache = btrfs_find_block_group(root, cache,
234 last + cache->key.offset - 1, 0, 0);
239 static u64 div_factor(u64 num, int factor)
246 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
247 struct btrfs_block_group_cache
248 *hint, u64 search_start,
251 struct btrfs_block_group_cache *cache[8];
252 struct btrfs_block_group_cache *found_group = NULL;
253 struct btrfs_fs_info *info = root->fs_info;
254 struct radix_tree_root *radix;
255 struct radix_tree_root *swap_radix;
269 radix = &info->block_group_data_radix;
270 swap_radix = &info->block_group_radix;
272 radix = &info->block_group_radix;
273 swap_radix = &info->block_group_data_radix;
277 struct btrfs_block_group_cache *shint;
278 shint = btrfs_lookup_block_group(info, search_start);
279 if (shint->data == data) {
280 used = btrfs_block_group_used(&shint->item);
281 if (used + shint->pinned <
282 div_factor(shint->key.offset, factor)) {
287 if (hint && hint->data == data) {
288 used = btrfs_block_group_used(&hint->item);
289 if (used + hint->pinned <
290 div_factor(hint->key.offset, factor)) {
293 if (used >= div_factor(hint->key.offset, 8)) {
294 radix_tree_tag_clear(radix,
296 hint->key.offset - 1,
297 BTRFS_BLOCK_GROUP_AVAIL);
299 last = hint->key.offset * 3;
300 if (hint->key.objectid >= last)
301 last = max(search_start + hint->key.offset - 1,
302 hint->key.objectid - last);
304 last = hint->key.objectid + hint->key.offset;
308 hint_last = max(hint->key.objectid, search_start);
310 hint_last = search_start;
315 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
316 last, ARRAY_SIZE(cache),
317 BTRFS_BLOCK_GROUP_AVAIL);
320 for (i = 0; i < ret; i++) {
321 last = cache[i]->key.objectid +
322 cache[i]->key.offset;
323 used = btrfs_block_group_used(&cache[i]->item);
324 if (used + cache[i]->pinned <
325 div_factor(cache[i]->key.offset, factor)) {
326 found_group = cache[i];
329 if (used >= div_factor(cache[i]->key.offset, 8)) {
330 radix_tree_tag_clear(radix,
331 cache[i]->key.objectid +
332 cache[i]->key.offset - 1,
333 BTRFS_BLOCK_GROUP_AVAIL);
341 ret = radix_tree_gang_lookup(radix, (void **)cache,
342 last, ARRAY_SIZE(cache));
345 for (i = 0; i < ret; i++) {
346 last = cache[i]->key.objectid +
347 cache[i]->key.offset;
348 used = btrfs_block_group_used(&cache[i]->item);
349 if (used + cache[i]->pinned < cache[i]->key.offset) {
350 found_group = cache[i];
353 if (used >= cache[i]->key.offset) {
354 radix_tree_tag_clear(radix,
355 cache[i]->key.objectid +
356 cache[i]->key.offset - 1,
357 BTRFS_BLOCK_GROUP_AVAIL);
368 struct radix_tree_root *tmp = radix;
376 ret = radix_tree_gang_lookup(radix,
377 (void **)&found_group, 0, 1);
379 ret = radix_tree_gang_lookup(swap_radix,
380 (void **)&found_group,
389 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
390 struct btrfs_root *root,
391 u64 blocknr, u64 num_blocks)
393 struct btrfs_path *path;
395 struct btrfs_key key;
396 struct btrfs_leaf *l;
397 struct btrfs_extent_item *item;
398 struct btrfs_key ins;
401 find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1, 0,
403 path = btrfs_alloc_path();
405 btrfs_init_path(path);
406 key.objectid = blocknr;
408 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
409 key.offset = num_blocks;
410 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
416 l = btrfs_buffer_leaf(path->nodes[0]);
417 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
418 refs = btrfs_extent_refs(item);
419 btrfs_set_extent_refs(item, refs + 1);
420 btrfs_mark_buffer_dirty(path->nodes[0]);
422 btrfs_release_path(root->fs_info->extent_root, path);
423 btrfs_free_path(path);
424 finish_current_insert(trans, root->fs_info->extent_root);
425 del_pending_extents(trans, root->fs_info->extent_root);
429 static int lookup_extent_ref(struct btrfs_trans_handle *trans,
430 struct btrfs_root *root, u64 blocknr,
431 u64 num_blocks, u32 *refs)
433 struct btrfs_path *path;
435 struct btrfs_key key;
436 struct btrfs_leaf *l;
437 struct btrfs_extent_item *item;
439 path = btrfs_alloc_path();
440 btrfs_init_path(path);
441 key.objectid = blocknr;
442 key.offset = num_blocks;
444 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
445 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
449 l = btrfs_buffer_leaf(path->nodes[0]);
450 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
451 *refs = btrfs_extent_refs(item);
452 btrfs_release_path(root->fs_info->extent_root, path);
453 btrfs_free_path(path);
457 int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
458 struct btrfs_root *root)
460 return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
463 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
464 struct buffer_head *buf)
467 struct btrfs_node *buf_node;
468 struct btrfs_leaf *buf_leaf;
469 struct btrfs_disk_key *key;
470 struct btrfs_file_extent_item *fi;
477 buf_node = btrfs_buffer_node(buf);
478 leaf = btrfs_is_leaf(buf_node);
479 buf_leaf = btrfs_buffer_leaf(buf);
480 for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
483 key = &buf_leaf->items[i].key;
484 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
486 fi = btrfs_item_ptr(buf_leaf, i,
487 struct btrfs_file_extent_item);
488 if (btrfs_file_extent_type(fi) ==
489 BTRFS_FILE_EXTENT_INLINE)
491 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
492 if (disk_blocknr == 0)
494 ret = btrfs_inc_extent_ref(trans, root, disk_blocknr,
495 btrfs_file_extent_disk_num_blocks(fi));
498 blocknr = btrfs_node_blockptr(buf_node, i);
499 ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
506 static int write_one_cache_group(struct btrfs_trans_handle *trans,
507 struct btrfs_root *root,
508 struct btrfs_path *path,
509 struct btrfs_block_group_cache *cache)
513 struct btrfs_root *extent_root = root->fs_info->extent_root;
514 struct btrfs_block_group_item *bi;
515 struct btrfs_key ins;
517 find_free_extent(trans, extent_root, 0, 0, (u64)-1, 0, &ins, 0);
518 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
520 bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
521 struct btrfs_block_group_item);
522 memcpy(bi, &cache->item, sizeof(*bi));
523 mark_buffer_dirty(path->nodes[0]);
524 btrfs_release_path(extent_root, path);
526 finish_current_insert(trans, extent_root);
527 pending_ret = del_pending_extents(trans, extent_root);
533 cache->last_alloc = cache->first_free;
538 static int write_dirty_block_radix(struct btrfs_trans_handle *trans,
539 struct btrfs_root *root,
540 struct radix_tree_root *radix)
542 struct btrfs_block_group_cache *cache[8];
547 struct btrfs_path *path;
549 path = btrfs_alloc_path();
554 ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
555 0, ARRAY_SIZE(cache),
556 BTRFS_BLOCK_GROUP_DIRTY);
559 for (i = 0; i < ret; i++) {
560 radix_tree_tag_clear(radix, cache[i]->key.objectid +
561 cache[i]->key.offset - 1,
562 BTRFS_BLOCK_GROUP_DIRTY);
563 err = write_one_cache_group(trans, root,
569 btrfs_free_path(path);
573 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
574 struct btrfs_root *root)
578 ret = write_dirty_block_radix(trans, root,
579 &root->fs_info->block_group_radix);
580 ret2 = write_dirty_block_radix(trans, root,
581 &root->fs_info->block_group_data_radix);
589 static int update_block_group(struct btrfs_trans_handle *trans,
590 struct btrfs_root *root,
591 u64 blocknr, u64 num, int alloc, int mark_free,
594 struct btrfs_block_group_cache *cache;
595 struct btrfs_fs_info *info = root->fs_info;
603 cache = btrfs_lookup_block_group(info, blocknr);
607 block_in_group = blocknr - cache->key.objectid;
608 WARN_ON(block_in_group > cache->key.offset);
609 radix_tree_tag_set(cache->radix, cache->key.objectid +
610 cache->key.offset - 1,
611 BTRFS_BLOCK_GROUP_DIRTY);
613 old_val = btrfs_block_group_used(&cache->item);
614 num = min(total, cache->key.offset - block_in_group);
616 if (blocknr > cache->last_alloc)
617 cache->last_alloc = blocknr;
619 for (i = 0; i < num; i++) {
620 clear_radix_bit(&info->extent_map_radix,
624 if (cache->data != data &&
625 old_val < (cache->key.offset >> 1)) {
627 radix_tree_delete(cache->radix,
628 cache->key.objectid +
629 cache->key.offset - 1);
633 &info->block_group_data_radix;
635 BTRFS_BLOCK_GROUP_DATA;
637 cache->radix = &info->block_group_radix;
639 ~BTRFS_BLOCK_GROUP_DATA;
641 ret = radix_tree_insert(cache->radix,
642 cache->key.objectid +
643 cache->key.offset - 1,
649 if (blocknr < cache->first_free)
650 cache->first_free = blocknr;
651 if (!cache->data && mark_free) {
652 for (i = 0; i < num; i++) {
653 set_radix_bit(&info->extent_map_radix,
657 if (old_val < (cache->key.offset >> 1) &&
658 old_val + num >= (cache->key.offset >> 1)) {
659 radix_tree_tag_set(cache->radix,
660 cache->key.objectid +
661 cache->key.offset - 1,
662 BTRFS_BLOCK_GROUP_AVAIL);
665 btrfs_set_block_group_used(&cache->item, old_val);
672 static int try_remove_page(struct address_space *mapping, unsigned long index)
675 ret = invalidate_mapping_pages(mapping, index, index);
679 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
682 unsigned long gang[8];
683 struct inode *btree_inode = root->fs_info->btree_inode;
684 struct btrfs_block_group_cache *block_group;
688 struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
689 struct radix_tree_root *extent_radix = &root->fs_info->extent_map_radix;
692 ret = find_first_radix_bit(pinned_radix, gang, 0,
698 for (i = 0; i < ret; i++) {
699 clear_radix_bit(pinned_radix, gang[i]);
700 block_group = btrfs_lookup_block_group(root->fs_info,
703 WARN_ON(block_group->pinned == 0);
704 block_group->pinned--;
705 if (gang[i] < block_group->last_alloc)
706 block_group->last_alloc = gang[i];
707 if (gang[i] < block_group->last_prealloc)
708 block_group->last_prealloc = gang[i];
709 if (!block_group->data)
710 set_radix_bit(extent_radix, gang[i]);
712 try_remove_page(btree_inode->i_mapping,
713 gang[i] << (PAGE_CACHE_SHIFT -
714 btree_inode->i_blkbits));
720 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
721 btrfs_root *extent_root)
723 struct btrfs_key ins;
724 struct btrfs_extent_item extent_item;
727 u64 super_blocks_used;
728 struct btrfs_fs_info *info = extent_root->fs_info;
730 btrfs_set_extent_refs(&extent_item, 1);
733 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
734 btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
736 for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
737 ins.objectid = extent_root->fs_info->extent_tree_insert[i];
738 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
739 btrfs_set_super_blocks_used(info->disk_super,
740 super_blocks_used + 1);
741 ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
742 sizeof(extent_item));
745 extent_root->fs_info->extent_tree_insert_nr = 0;
746 extent_root->fs_info->extent_tree_prealloc_nr = 0;
750 static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
753 struct btrfs_header *header;
754 struct buffer_head *bh;
757 bh = btrfs_find_tree_block(root, blocknr);
759 if (buffer_uptodate(bh)) {
761 root->fs_info->running_transaction->transid;
762 header = btrfs_buffer_header(bh);
763 if (btrfs_header_generation(header) ==
765 btrfs_block_release(root, bh);
769 btrfs_block_release(root, bh);
771 err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
773 struct btrfs_block_group_cache *cache;
774 cache = btrfs_lookup_block_group(root->fs_info,
780 err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
787 * remove an extent from the root, returns 0 on success
789 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
790 *root, u64 blocknr, u64 num_blocks, int pin,
793 struct btrfs_path *path;
794 struct btrfs_key key;
795 struct btrfs_fs_info *info = root->fs_info;
796 struct btrfs_root *extent_root = info->extent_root;
798 struct btrfs_extent_item *ei;
799 struct btrfs_key ins;
802 key.objectid = blocknr;
804 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
805 key.offset = num_blocks;
807 find_free_extent(trans, root, 0, 0, (u64)-1, 0, &ins, 0);
808 path = btrfs_alloc_path();
810 btrfs_init_path(path);
812 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
816 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
817 struct btrfs_extent_item);
818 BUG_ON(ei->refs == 0);
819 refs = btrfs_extent_refs(ei) - 1;
820 btrfs_set_extent_refs(ei, refs);
821 btrfs_mark_buffer_dirty(path->nodes[0]);
823 u64 super_blocks_used;
826 ret = pin_down_block(root, blocknr, 0);
830 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
831 btrfs_set_super_blocks_used(info->disk_super,
832 super_blocks_used - num_blocks);
833 ret = btrfs_del_item(trans, extent_root, path);
836 ret = update_block_group(trans, root, blocknr, num_blocks, 0,
840 btrfs_free_path(path);
841 finish_current_insert(trans, extent_root);
846 * find all the blocks marked as pending in the radix tree and remove
847 * them from the extent map
849 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
850 btrfs_root *extent_root)
855 unsigned long gang[4];
857 struct radix_tree_root *pending_radix;
858 struct radix_tree_root *pinned_radix;
859 struct btrfs_block_group_cache *cache;
861 pending_radix = &extent_root->fs_info->pending_del_radix;
862 pinned_radix = &extent_root->fs_info->pinned_radix;
865 ret = find_first_radix_bit(pending_radix, gang, 0,
869 for (i = 0; i < ret; i++) {
870 wret = set_radix_bit(pinned_radix, gang[i]);
873 btrfs_lookup_block_group(extent_root->fs_info,
879 printk(KERN_CRIT "set_radix_bit, err %d\n",
883 wret = clear_radix_bit(pending_radix, gang[i]);
885 wret = __free_extent(trans, extent_root,
895 * remove an extent from the root, returns 0 on success
897 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
898 *root, u64 blocknr, u64 num_blocks, int pin)
900 struct btrfs_root *extent_root = root->fs_info->extent_root;
904 if (root == extent_root) {
905 pin_down_block(root, blocknr, 1);
908 ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
909 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
910 return ret ? ret : pending_ret;
914 * walks the btree of allocated extents and find a hole of a given size.
915 * The key ins is changed to record the hole:
916 * ins->objectid == block start
917 * ins->flags = BTRFS_EXTENT_ITEM_KEY
918 * ins->offset == number of blocks
919 * Any available blocks before search_start are skipped.
921 static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
922 *orig_root, u64 num_blocks, u64 search_start, u64
923 search_end, u64 hint_block,
924 struct btrfs_key *ins, int data)
926 struct btrfs_path *path;
927 struct btrfs_key key;
933 u64 orig_search_start = search_start;
935 struct btrfs_leaf *l;
936 struct btrfs_root * root = orig_root->fs_info->extent_root;
937 struct btrfs_fs_info *info = root->fs_info;
938 int total_needed = num_blocks;
940 int fill_prealloc = 0;
942 struct btrfs_block_group_cache *block_group;
947 path = btrfs_alloc_path();
949 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
951 level = btrfs_header_level(btrfs_buffer_header(root->node));
952 if (num_blocks == 0) {
955 total_needed = (min(level + 1, BTRFS_MAX_LEVEL) + 2) * 3;
957 if (search_end == (u64)-1)
958 search_end = btrfs_super_total_blocks(info->disk_super);
960 block_group = btrfs_lookup_block_group(info, hint_block);
961 block_group = btrfs_find_block_group(root, block_group,
962 hint_block, data, 1);
964 block_group = btrfs_find_block_group(root,
965 trans->block_group, 0,
970 if (!block_group->data)
971 search_start = find_search_start(root, &block_group,
972 search_start, total_needed);
974 search_start = max(block_group->last_alloc, search_start);
976 btrfs_init_path(path);
977 ins->objectid = search_start;
981 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
985 if (path->slots[0] > 0) {
989 l = btrfs_buffer_leaf(path->nodes[0]);
990 btrfs_disk_key_to_cpu(&key, &l->items[path->slots[0]].key);
992 * a rare case, go back one key if we hit a block group item
993 * instead of an extent item
995 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
996 key.objectid + key.offset >= search_start) {
997 ins->objectid = key.objectid;
998 ins->offset = key.offset - 1;
999 btrfs_release_path(root, path);
1000 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1004 if (path->slots[0] > 0) {
1010 l = btrfs_buffer_leaf(path->nodes[0]);
1011 slot = path->slots[0];
1012 if (slot >= btrfs_header_nritems(&l->header)) {
1013 if (fill_prealloc) {
1014 info->extent_tree_prealloc_nr = 0;
1018 limit = last_block +
1019 (block_group->key.offset >> 1);
1021 limit = search_start +
1022 (block_group->key.offset >> 1);
1023 ret = btrfs_next_leaf(root, path);
1029 ins->objectid = search_start;
1030 ins->offset = search_end - search_start;
1034 ins->objectid = last_block > search_start ?
1035 last_block : search_start;
1036 ins->offset = search_end - ins->objectid;
1040 btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
1041 if (key.objectid >= search_start && key.objectid > last_block &&
1043 if (last_block < search_start)
1044 last_block = search_start;
1045 hole_size = key.objectid - last_block;
1046 if (hole_size >= num_blocks) {
1047 ins->objectid = last_block;
1048 ins->offset = hole_size;
1053 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
1057 last_block = key.objectid + key.offset;
1058 if (!full_scan && last_block >= block_group->key.objectid +
1059 block_group->key.offset) {
1060 btrfs_release_path(root, path);
1061 search_start = block_group->key.objectid +
1062 block_group->key.offset * 2;
1071 /* we have to make sure we didn't find an extent that has already
1072 * been allocated by the map tree or the original allocation
1074 btrfs_release_path(root, path);
1075 BUG_ON(ins->objectid < search_start);
1077 if (ins->objectid + num_blocks >= search_end) {
1082 search_start = orig_search_start;
1089 for (test_block = ins->objectid;
1090 test_block < ins->objectid + num_blocks; test_block++) {
1091 if (test_radix_bit(&info->pinned_radix, test_block)) {
1092 search_start = test_block + 1;
1096 if (!fill_prealloc && info->extent_tree_insert_nr) {
1098 info->extent_tree_insert[info->extent_tree_insert_nr - 1];
1099 if (ins->objectid + num_blocks >
1100 info->extent_tree_insert[0] &&
1101 ins->objectid <= last) {
1102 search_start = last + 1;
1103 WARN_ON(!full_scan);
1107 if (!fill_prealloc && info->extent_tree_prealloc_nr) {
1109 info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
1110 if (ins->objectid + num_blocks > first &&
1111 ins->objectid <= info->extent_tree_prealloc[0]) {
1112 search_start = info->extent_tree_prealloc[0] + 1;
1113 WARN_ON(!full_scan);
1117 if (fill_prealloc) {
1119 test_block = ins->objectid;
1120 if (test_block - info->extent_tree_prealloc[total_needed - 1] >=
1123 info->extent_tree_prealloc_nr = total_found;
1125 while(test_block < ins->objectid + ins->offset &&
1126 total_found < total_needed) {
1127 nr = total_needed - total_found - 1;
1129 info->extent_tree_prealloc[nr] = test_block;
1133 if (total_found < total_needed) {
1134 search_start = test_block;
1137 info->extent_tree_prealloc_nr = total_found;
1140 block_group = btrfs_lookup_block_group(info, ins->objectid);
1143 block_group->last_prealloc =
1144 info->extent_tree_prealloc[total_needed-1];
1146 trans->block_group = block_group;
1149 ins->offset = num_blocks;
1150 btrfs_free_path(path);
1154 if (search_start + num_blocks >= search_end) {
1155 search_start = orig_search_start;
1165 block_group = btrfs_lookup_block_group(info, search_start);
1168 block_group = btrfs_find_block_group(root, block_group,
1169 search_start, data, 0);
1173 btrfs_release_path(root, path);
1174 btrfs_free_path(path);
1178 * finds a free extent and does all the dirty work required for allocation
1179 * returns the key for the extent through ins, and a tree buffer for
1180 * the first block of the extent through buf.
1182 * returns 0 if everything worked, non-zero otherwise.
1184 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1185 struct btrfs_root *root, u64 owner,
1186 u64 num_blocks, u64 hint_block,
1187 u64 search_end, struct btrfs_key *ins, int data)
1191 u64 super_blocks_used;
1192 u64 search_start = 0;
1193 struct btrfs_fs_info *info = root->fs_info;
1194 struct btrfs_root *extent_root = info->extent_root;
1195 struct btrfs_extent_item extent_item;
1196 struct btrfs_key prealloc_key;
1198 btrfs_set_extent_refs(&extent_item, 1);
1199 btrfs_set_extent_owner(&extent_item, owner);
1201 if (root == extent_root) {
1203 BUG_ON(info->extent_tree_prealloc_nr == 0);
1204 BUG_ON(num_blocks != 1);
1206 info->extent_tree_prealloc_nr--;
1207 nr = info->extent_tree_prealloc_nr;
1208 ins->objectid = info->extent_tree_prealloc[nr];
1209 info->extent_tree_insert[info->extent_tree_insert_nr++] =
1211 ret = update_block_group(trans, root,
1212 ins->objectid, ins->offset, 1, 0, 0);
1218 * if we're doing a data allocation, preallocate room in the
1219 * extent tree first. This way the extent tree blocks end up
1220 * in the correct block group.
1223 ret = find_free_extent(trans, root, 0, 0,
1224 search_end, 0, &prealloc_key, 0);
1228 if (prealloc_key.objectid + prealloc_key.offset >= search_end) {
1229 int nr = info->extent_tree_prealloc_nr;
1230 search_end = info->extent_tree_prealloc[nr - 1] - 1;
1232 search_start = info->extent_tree_prealloc[0] + 1;
1235 if (hint_block < search_start)
1236 hint_block = search_start;
1237 /* do the real allocation */
1238 ret = find_free_extent(trans, root, num_blocks, search_start,
1239 search_end, hint_block, ins, data);
1245 * if we're doing a metadata allocation, preallocate space in the
1246 * extent tree second. This way, we don't create a tiny hole
1247 * in the allocation map between any unused preallocation blocks
1248 * and the metadata block we're actually allocating. On disk,
1250 * [block we've allocated], [used prealloc 1], [ unused prealloc ]
1251 * The unused prealloc will get reused the next time around.
1254 if (ins->objectid + ins->offset >= search_end)
1255 search_end = ins->objectid - 1;
1257 search_start = ins->objectid + ins->offset;
1259 if (hint_block < search_start)
1260 hint_block = search_start;
1262 ret = find_free_extent(trans, root, 0, search_start,
1263 search_end, hint_block,
1270 super_blocks_used = btrfs_super_blocks_used(info->disk_super);
1271 btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
1273 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1274 sizeof(extent_item));
1276 finish_current_insert(trans, extent_root);
1277 pending_ret = del_pending_extents(trans, extent_root);
1284 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1291 * helper function to allocate a block for a given tree
1292 * returns the tree buffer or NULL.
1294 struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1295 struct btrfs_root *root, u64 hint)
1297 struct btrfs_key ins;
1299 struct buffer_head *buf;
1301 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
1302 1, hint, (unsigned long)-1, &ins, 0);
1308 buf = btrfs_find_create_tree_block(root, ins.objectid);
1309 set_buffer_uptodate(buf);
1310 set_buffer_checked(buf);
1311 set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
1315 static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1316 struct btrfs_root *root, struct buffer_head *cur)
1318 struct btrfs_disk_key *key;
1319 struct btrfs_leaf *leaf;
1320 struct btrfs_file_extent_item *fi;
1325 BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
1326 leaf = btrfs_buffer_leaf(cur);
1327 nritems = btrfs_header_nritems(&leaf->header);
1328 for (i = 0; i < nritems; i++) {
1330 key = &leaf->items[i].key;
1331 if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
1333 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1334 if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
1337 * FIXME make sure to insert a trans record that
1338 * repeats the snapshot del on crash
1340 disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
1341 if (disk_blocknr == 0)
1343 ret = btrfs_free_extent(trans, root, disk_blocknr,
1344 btrfs_file_extent_disk_num_blocks(fi),
1352 * helper function for drop_snapshot, this walks down the tree dropping ref
1353 * counts as it goes.
1355 static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1356 *root, struct btrfs_path *path, int *level)
1358 struct buffer_head *next;
1359 struct buffer_head *cur;
1364 WARN_ON(*level < 0);
1365 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1366 ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
1372 * walk down to the last node level and free all the leaves
1374 while(*level >= 0) {
1375 WARN_ON(*level < 0);
1376 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1377 cur = path->nodes[*level];
1378 if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
1380 if (path->slots[*level] >=
1381 btrfs_header_nritems(btrfs_buffer_header(cur)))
1384 ret = drop_leaf_ref(trans, root, cur);
1388 blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
1389 path->slots[*level]);
1390 ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
1393 path->slots[*level]++;
1394 ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
1398 next = read_tree_block(root, blocknr);
1399 WARN_ON(*level <= 0);
1400 if (path->nodes[*level-1])
1401 btrfs_block_release(root, path->nodes[*level-1]);
1402 path->nodes[*level-1] = next;
1403 *level = btrfs_header_level(btrfs_buffer_header(next));
1404 path->slots[*level] = 0;
1407 WARN_ON(*level < 0);
1408 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1409 ret = btrfs_free_extent(trans, root,
1410 bh_blocknr(path->nodes[*level]), 1, 1);
1411 btrfs_block_release(root, path->nodes[*level]);
1412 path->nodes[*level] = NULL;
1419 * helper for dropping snapshots. This walks back up the tree in the path
1420 * to find the first node higher up where we haven't yet gone through
1423 static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1424 *root, struct btrfs_path *path, int *level)
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) {
1437 ret = btrfs_free_extent(trans, root,
1438 bh_blocknr(path->nodes[*level]),
1441 btrfs_block_release(root, path->nodes[*level]);
1442 path->nodes[*level] = NULL;
1450 * drop the reference count on the tree rooted at 'snap'. This traverses
1451 * the tree freeing any blocks that have a ref count of zero after being
1454 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
1455 *root, struct buffer_head *snap)
1460 struct btrfs_path *path;
1464 path = btrfs_alloc_path();
1466 btrfs_init_path(path);
1468 level = btrfs_header_level(btrfs_buffer_header(snap));
1470 path->nodes[level] = snap;
1471 path->slots[level] = 0;
1473 wret = walk_down_tree(trans, root, path, &level);
1479 wret = walk_up_tree(trans, root, path, &level);
1484 btrfs_btree_balance_dirty(root);
1486 for (i = 0; i <= orig_level; i++) {
1487 if (path->nodes[i]) {
1488 btrfs_block_release(root, path->nodes[i]);
1491 btrfs_free_path(path);
1495 static int free_block_group_radix(struct radix_tree_root *radix)
1498 struct btrfs_block_group_cache *cache[8];
1502 ret = radix_tree_gang_lookup(radix, (void **)cache, 0,
1506 for (i = 0; i < ret; i++) {
1507 radix_tree_delete(radix, cache[i]->key.objectid +
1508 cache[i]->key.offset - 1);
1515 int btrfs_free_block_groups(struct btrfs_fs_info *info)
1519 unsigned long gang[16];
1522 ret = free_block_group_radix(&info->block_group_radix);
1523 ret2 = free_block_group_radix(&info->block_group_data_radix);
1530 ret = find_first_radix_bit(&info->extent_map_radix,
1531 gang, 0, ARRAY_SIZE(gang));
1534 for (i = 0; i < ret; i++) {
1535 clear_radix_bit(&info->extent_map_radix, gang[i]);
1541 int btrfs_read_block_groups(struct btrfs_root *root)
1543 struct btrfs_path *path;
1546 struct btrfs_block_group_item *bi;
1547 struct btrfs_block_group_cache *cache;
1548 struct btrfs_fs_info *info = root->fs_info;
1549 struct radix_tree_root *radix;
1550 struct btrfs_key key;
1551 struct btrfs_key found_key;
1552 struct btrfs_leaf *leaf;
1553 u64 group_size_blocks;
1556 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE >>
1557 root->fs_info->sb->s_blocksize_bits;
1558 root = info->extent_root;
1560 key.offset = group_size_blocks;
1562 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1564 path = btrfs_alloc_path();
1569 ret = btrfs_search_slot(NULL, info->extent_root,
1575 leaf = btrfs_buffer_leaf(path->nodes[0]);
1576 btrfs_disk_key_to_cpu(&found_key,
1577 &leaf->items[path->slots[0]].key);
1578 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1584 bi = btrfs_item_ptr(leaf, path->slots[0],
1585 struct btrfs_block_group_item);
1586 if (bi->flags & BTRFS_BLOCK_GROUP_DATA) {
1587 radix = &info->block_group_data_radix;
1590 radix = &info->block_group_radix;
1594 memcpy(&cache->item, bi, sizeof(*bi));
1595 memcpy(&cache->key, &found_key, sizeof(found_key));
1596 cache->last_alloc = cache->key.objectid;
1597 cache->first_free = cache->key.objectid;
1598 cache->last_prealloc = cache->key.objectid;
1602 cache->radix = radix;
1604 key.objectid = found_key.objectid + found_key.offset;
1605 btrfs_release_path(root, path);
1606 ret = radix_tree_insert(radix, found_key.objectid +
1607 found_key.offset - 1,
1610 used = btrfs_block_group_used(bi);
1611 if (used < div_factor(key.offset, 8)) {
1612 radix_tree_tag_set(radix, found_key.objectid +
1613 found_key.offset - 1,
1614 BTRFS_BLOCK_GROUP_AVAIL);
1617 btrfs_super_total_blocks(info->disk_super))
1621 btrfs_free_path(path);