2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
19 #include <linux/sched.h>
22 #include "print-tree.h"
23 #include "transaction.h"
25 #define BLOCK_GROUP_DATA EXTENT_WRITEBACK
26 #define BLOCK_GROUP_METADATA EXTENT_UPTODATE
27 #define BLOCK_GROUP_DIRTY EXTENT_DIRTY
29 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
30 btrfs_root *extent_root);
31 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
32 btrfs_root *extent_root);
34 static int cache_block_group(struct btrfs_root *root,
35 struct btrfs_block_group_cache *block_group)
37 struct btrfs_path *path;
40 struct extent_buffer *leaf;
41 struct extent_map_tree *free_space_cache;
48 root = root->fs_info->extent_root;
49 free_space_cache = &root->fs_info->free_space_cache;
51 if (block_group->cached)
54 path = btrfs_alloc_path();
59 first_free = block_group->key.objectid;
60 key.objectid = block_group->key.objectid;
63 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
64 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
69 if (ret && path->slots[0] > 0)
73 leaf = path->nodes[0];
74 slot = path->slots[0];
75 if (slot >= btrfs_header_nritems(leaf)) {
76 ret = btrfs_next_leaf(root, path);
86 btrfs_item_key_to_cpu(leaf, &key, slot);
87 if (key.objectid < block_group->key.objectid) {
88 if (key.objectid + key.offset > first_free)
89 first_free = key.objectid + key.offset;
93 if (key.objectid >= block_group->key.objectid +
94 block_group->key.offset) {
98 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
103 if (key.objectid > last) {
104 hole_size = key.objectid - last;
105 set_extent_dirty(free_space_cache, last,
106 last + hole_size - 1,
109 last = key.objectid + key.offset;
117 if (block_group->key.objectid +
118 block_group->key.offset > last) {
119 hole_size = block_group->key.objectid +
120 block_group->key.offset - last;
121 set_extent_dirty(free_space_cache, last,
122 last + hole_size - 1, GFP_NOFS);
124 block_group->cached = 1;
126 btrfs_free_path(path);
130 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
134 struct extent_map_tree *block_group_cache;
135 struct btrfs_block_group_cache *block_group = NULL;
141 block_group_cache = &info->block_group_cache;
142 ret = find_first_extent_bit(block_group_cache,
143 bytenr, &start, &end,
144 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA);
148 ret = get_state_private(block_group_cache, start, &ptr);
152 block_group = (struct btrfs_block_group_cache *)ptr;
155 if (block_group->key.objectid <= bytenr && bytenr <=
156 block_group->key.objectid + block_group->key.offset)
162 static u64 find_search_start(struct btrfs_root *root,
163 struct btrfs_block_group_cache **cache_ret,
164 u64 search_start, int num, int data)
167 struct btrfs_block_group_cache *cache = *cache_ret;
168 u64 last = max(search_start, cache->key.objectid);
173 ret = cache_block_group(root, cache);
177 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
178 last, &start, &end, EXTENT_DIRTY);
182 start = max(last, start);
184 if (end + 1 - start < num)
186 if (start + num >= cache->key.objectid + cache->key.offset)
194 cache = btrfs_lookup_block_group(root->fs_info,
195 last + cache->key.offset - 1);
199 cache = btrfs_find_block_group(root, cache,
200 last + cache->key.offset - 1, data, 0);
202 last = min(cache->key.objectid, last);
206 static u64 div_factor(u64 num, int factor)
213 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
214 struct btrfs_block_group_cache
215 *hint, u64 search_start,
218 struct btrfs_block_group_cache *cache;
219 struct extent_map_tree *block_group_cache;
220 struct btrfs_block_group_cache *found_group = NULL;
221 struct btrfs_fs_info *info = root->fs_info;
235 block_group_cache = &info->block_group_cache;
241 bit = BLOCK_GROUP_DATA;
243 bit = BLOCK_GROUP_METADATA;
246 struct btrfs_block_group_cache *shint;
247 shint = btrfs_lookup_block_group(info, search_start);
248 if (shint && shint->data == data) {
249 used = btrfs_block_group_used(&shint->item);
250 if (used < div_factor(shint->key.offset, factor)) {
255 if (hint && hint->data == data) {
256 used = btrfs_block_group_used(&hint->item);
257 if (used < div_factor(hint->key.offset, factor)) {
260 last = hint->key.offset * 3;
261 if (hint->key.objectid >= last)
262 last = max(search_start + hint->key.offset - 1,
263 hint->key.objectid - last);
265 last = hint->key.objectid + hint->key.offset;
269 hint_last = max(hint->key.objectid, search_start);
271 hint_last = search_start;
277 ret = find_first_extent_bit(block_group_cache, last,
282 ret = get_state_private(block_group_cache, start, &ptr);
286 cache = (struct btrfs_block_group_cache *)ptr;
287 last = cache->key.objectid + cache->key.offset;
288 used = btrfs_block_group_used(&cache->item);
291 free_check = cache->key.offset;
293 free_check = div_factor(cache->key.offset, factor);
295 if (used < free_check) {
308 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
316 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
317 struct btrfs_root *root,
318 u64 bytenr, u64 num_bytes)
320 struct btrfs_path *path;
322 struct btrfs_key key;
323 struct extent_buffer *l;
324 struct btrfs_extent_item *item;
327 WARN_ON(num_bytes < root->sectorsize);
328 path = btrfs_alloc_path();
332 key.objectid = bytenr;
333 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
334 key.offset = num_bytes;
335 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
344 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
345 refs = btrfs_extent_refs(l, item);
346 btrfs_set_extent_refs(l, item, refs + 1);
347 btrfs_mark_buffer_dirty(path->nodes[0]);
349 btrfs_release_path(root->fs_info->extent_root, path);
350 btrfs_free_path(path);
351 finish_current_insert(trans, root->fs_info->extent_root);
352 del_pending_extents(trans, root->fs_info->extent_root);
356 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
357 struct btrfs_root *root)
359 finish_current_insert(trans, root->fs_info->extent_root);
360 del_pending_extents(trans, root->fs_info->extent_root);
364 static int lookup_extent_ref(struct btrfs_trans_handle *trans,
365 struct btrfs_root *root, u64 bytenr,
366 u64 num_bytes, u32 *refs)
368 struct btrfs_path *path;
370 struct btrfs_key key;
371 struct extent_buffer *l;
372 struct btrfs_extent_item *item;
374 WARN_ON(num_bytes < root->sectorsize);
375 path = btrfs_alloc_path();
376 key.objectid = bytenr;
377 key.offset = num_bytes;
378 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
379 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
384 btrfs_print_leaf(root, path->nodes[0]);
385 printk("failed to find block number %Lu\n", bytenr);
389 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
390 *refs = btrfs_extent_refs(l, item);
392 btrfs_free_path(path);
396 int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
397 struct btrfs_root *root)
399 return btrfs_inc_extent_ref(trans, root, root->node->start,
403 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
404 struct extent_buffer *buf)
408 struct btrfs_key key;
409 struct btrfs_file_extent_item *fi;
419 level = btrfs_header_level(buf);
420 nritems = btrfs_header_nritems(buf);
421 for (i = 0; i < nritems; i++) {
424 btrfs_item_key_to_cpu(buf, &key, i);
425 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
427 fi = btrfs_item_ptr(buf, i,
428 struct btrfs_file_extent_item);
429 if (btrfs_file_extent_type(buf, fi) ==
430 BTRFS_FILE_EXTENT_INLINE)
432 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
433 if (disk_bytenr == 0)
435 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
436 btrfs_file_extent_disk_num_bytes(buf, fi));
442 bytenr = btrfs_node_blockptr(buf, i);
443 ret = btrfs_inc_extent_ref(trans, root, bytenr,
444 btrfs_level_size(root, level - 1));
454 for (i =0; i < faili; i++) {
457 btrfs_item_key_to_cpu(buf, &key, i);
458 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
460 fi = btrfs_item_ptr(buf, i,
461 struct btrfs_file_extent_item);
462 if (btrfs_file_extent_type(buf, fi) ==
463 BTRFS_FILE_EXTENT_INLINE)
465 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
466 if (disk_bytenr == 0)
468 err = btrfs_free_extent(trans, root, disk_bytenr,
469 btrfs_file_extent_disk_num_bytes(buf,
473 bytenr = btrfs_node_blockptr(buf, i);
474 err = btrfs_free_extent(trans, root, bytenr,
475 btrfs_level_size(root, level - 1), 0);
482 static int write_one_cache_group(struct btrfs_trans_handle *trans,
483 struct btrfs_root *root,
484 struct btrfs_path *path,
485 struct btrfs_block_group_cache *cache)
489 struct btrfs_root *extent_root = root->fs_info->extent_root;
491 struct extent_buffer *leaf;
493 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
498 leaf = path->nodes[0];
499 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
500 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
501 btrfs_mark_buffer_dirty(leaf);
502 btrfs_release_path(extent_root, path);
504 finish_current_insert(trans, extent_root);
505 pending_ret = del_pending_extents(trans, extent_root);
514 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
515 struct btrfs_root *root)
517 struct extent_map_tree *block_group_cache;
518 struct btrfs_block_group_cache *cache;
522 struct btrfs_path *path;
528 block_group_cache = &root->fs_info->block_group_cache;
529 path = btrfs_alloc_path();
534 ret = find_first_extent_bit(block_group_cache, last,
535 &start, &end, BLOCK_GROUP_DIRTY);
540 ret = get_state_private(block_group_cache, start, &ptr);
544 cache = (struct btrfs_block_group_cache *)ptr;
545 err = write_one_cache_group(trans, root,
548 * if we fail to write the cache group, we want
549 * to keep it marked dirty in hopes that a later
556 clear_extent_bits(block_group_cache, start, end,
557 BLOCK_GROUP_DIRTY, GFP_NOFS);
559 btrfs_free_path(path);
563 static int update_block_group(struct btrfs_trans_handle *trans,
564 struct btrfs_root *root,
565 u64 bytenr, u64 num_bytes, int alloc,
566 int mark_free, int data)
568 struct btrfs_block_group_cache *cache;
569 struct btrfs_fs_info *info = root->fs_info;
570 u64 total = num_bytes;
577 cache = btrfs_lookup_block_group(info, bytenr);
581 byte_in_group = bytenr - cache->key.objectid;
582 WARN_ON(byte_in_group > cache->key.offset);
583 start = cache->key.objectid;
584 end = start + cache->key.offset - 1;
585 set_extent_bits(&info->block_group_cache, start, end,
586 BLOCK_GROUP_DIRTY, GFP_NOFS);
588 old_val = btrfs_block_group_used(&cache->item);
589 num_bytes = min(total, cache->key.offset - byte_in_group);
591 if (cache->data != data &&
592 old_val < (cache->key.offset >> 1)) {
598 bit_to_clear = BLOCK_GROUP_DATA;
599 bit_to_set = BLOCK_GROUP_METADATA;
601 BTRFS_BLOCK_GROUP_DATA;
603 bit_to_clear = BLOCK_GROUP_METADATA;
604 bit_to_set = BLOCK_GROUP_DATA;
606 ~BTRFS_BLOCK_GROUP_DATA;
608 clear_extent_bits(&info->block_group_cache,
609 start, end, bit_to_clear,
611 set_extent_bits(&info->block_group_cache,
612 start, end, bit_to_set,
615 old_val += num_bytes;
617 old_val -= num_bytes;
619 set_extent_dirty(&info->free_space_cache,
620 bytenr, bytenr + num_bytes - 1,
624 btrfs_set_block_group_used(&cache->item, old_val);
631 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
636 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
640 ret = find_first_extent_bit(pinned_extents, last,
641 &start, &end, EXTENT_DIRTY);
644 set_extent_dirty(copy, start, end, GFP_NOFS);
650 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
651 struct btrfs_root *root,
652 struct extent_map_tree *unpin)
657 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
658 struct extent_map_tree *free_space_cache;
660 free_space_cache = &root->fs_info->free_space_cache;
663 ret = find_first_extent_bit(unpin, 0, &start, &end,
668 clear_extent_dirty(pinned_extents, start, end,
670 clear_extent_dirty(unpin, start, end, GFP_NOFS);
671 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
676 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
677 btrfs_root *extent_root)
679 struct btrfs_key ins;
680 struct btrfs_extent_item extent_item;
685 struct btrfs_fs_info *info = extent_root->fs_info;
687 btrfs_set_stack_extent_refs(&extent_item, 1);
688 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
689 btrfs_set_stack_extent_owner(&extent_item,
690 extent_root->root_key.objectid);
693 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
694 &end, EXTENT_LOCKED);
698 ins.objectid = start;
699 ins.offset = end + 1 - start;
700 err = btrfs_insert_item(trans, extent_root, &ins,
701 &extent_item, sizeof(extent_item));
702 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
708 static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
712 struct extent_buffer *buf;
715 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
717 if (btrfs_buffer_uptodate(buf)) {
719 root->fs_info->running_transaction->transid;
720 if (btrfs_header_generation(buf) == transid) {
721 free_extent_buffer(buf);
725 free_extent_buffer(buf);
727 set_extent_dirty(&root->fs_info->pinned_extents,
728 bytenr, bytenr + num_bytes - 1, GFP_NOFS);
730 set_extent_bits(&root->fs_info->pending_del,
731 bytenr, bytenr + num_bytes - 1,
732 EXTENT_LOCKED, GFP_NOFS);
739 * remove an extent from the root, returns 0 on success
741 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
742 *root, u64 bytenr, u64 num_bytes, int pin,
745 struct btrfs_path *path;
746 struct btrfs_key key;
747 struct btrfs_fs_info *info = root->fs_info;
748 struct btrfs_root *extent_root = info->extent_root;
749 struct extent_buffer *leaf;
751 struct btrfs_extent_item *ei;
754 key.objectid = bytenr;
755 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
756 key.offset = num_bytes;
758 path = btrfs_alloc_path();
762 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
767 leaf = path->nodes[0];
768 ei = btrfs_item_ptr(leaf, path->slots[0],
769 struct btrfs_extent_item);
770 refs = btrfs_extent_refs(leaf, ei);
773 btrfs_set_extent_refs(leaf, ei, refs);
774 btrfs_mark_buffer_dirty(leaf);
781 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
785 /* block accounting for super block */
786 super_used = btrfs_super_bytes_used(&info->super_copy);
787 btrfs_set_super_bytes_used(&info->super_copy,
788 super_used - num_bytes);
790 /* block accounting for root item */
791 root_used = btrfs_root_used(&root->root_item);
792 btrfs_set_root_used(&root->root_item,
793 root_used - num_bytes);
795 ret = btrfs_del_item(trans, extent_root, path);
799 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
803 btrfs_free_path(path);
804 finish_current_insert(trans, extent_root);
809 * find all the blocks marked as pending in the radix tree and remove
810 * them from the extent map
812 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
813 btrfs_root *extent_root)
819 struct extent_map_tree *pending_del;
820 struct extent_map_tree *pinned_extents;
822 pending_del = &extent_root->fs_info->pending_del;
823 pinned_extents = &extent_root->fs_info->pinned_extents;
826 ret = find_first_extent_bit(pending_del, 0, &start, &end,
831 set_extent_dirty(pinned_extents, start, end, GFP_NOFS);
832 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
834 ret = __free_extent(trans, extent_root,
835 start, end + 1 - start, 0, 0);
843 * remove an extent from the root, returns 0 on success
845 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
846 *root, u64 bytenr, u64 num_bytes, int pin)
848 struct btrfs_root *extent_root = root->fs_info->extent_root;
852 WARN_ON(num_bytes < root->sectorsize);
853 if (root == extent_root) {
854 pin_down_bytes(root, bytenr, num_bytes, 1);
857 ret = __free_extent(trans, root, bytenr, num_bytes, pin, pin == 0);
858 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
859 return ret ? ret : pending_ret;
863 * walks the btree of allocated extents and find a hole of a given size.
864 * The key ins is changed to record the hole:
865 * ins->objectid == block start
866 * ins->flags = BTRFS_EXTENT_ITEM_KEY
867 * ins->offset == number of blocks
868 * Any available blocks before search_start are skipped.
870 static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
871 *orig_root, u64 num_bytes, u64 empty_size,
872 u64 search_start, u64 search_end, u64 hint_byte,
873 struct btrfs_key *ins, u64 exclude_start,
874 u64 exclude_nr, int data)
876 struct btrfs_path *path;
877 struct btrfs_key key;
882 u64 orig_search_start = search_start;
884 struct extent_buffer *l;
885 struct btrfs_root * root = orig_root->fs_info->extent_root;
886 struct btrfs_fs_info *info = root->fs_info;
887 u64 total_needed = num_bytes;
889 struct btrfs_block_group_cache *block_group;
893 WARN_ON(num_bytes < root->sectorsize);
894 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
896 level = btrfs_header_level(root->node);
898 if (search_end == (u64)-1)
899 search_end = btrfs_super_total_bytes(&info->super_copy);
901 block_group = btrfs_lookup_block_group(info, hint_byte);
902 block_group = btrfs_find_block_group(root, block_group,
905 block_group = btrfs_find_block_group(root,
906 trans->block_group, 0,
910 total_needed += empty_size;
911 path = btrfs_alloc_path();
914 search_start = find_search_start(root, &block_group,
915 search_start, total_needed, data);
917 btrfs_init_path(path);
918 ins->objectid = search_start;
923 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
927 if (path->slots[0] > 0) {
932 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
935 * a rare case, go back one key if we hit a block group item
936 * instead of an extent item
938 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
939 key.objectid + key.offset >= search_start) {
940 ins->objectid = key.objectid;
941 ins->offset = key.offset - 1;
942 btrfs_release_path(root, path);
943 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
947 if (path->slots[0] > 0) {
954 slot = path->slots[0];
955 if (slot >= btrfs_header_nritems(l)) {
956 ret = btrfs_next_leaf(root, path);
962 ins->objectid = search_start;
963 ins->offset = search_end - search_start;
967 ins->objectid = last_byte > search_start ?
968 last_byte : search_start;
969 ins->offset = search_end - ins->objectid;
972 btrfs_item_key_to_cpu(l, &key, slot);
974 if (key.objectid >= search_start && key.objectid > last_byte &&
976 if (last_byte < search_start)
977 last_byte = search_start;
978 hole_size = key.objectid - last_byte;
979 if (hole_size >= num_bytes) {
980 ins->objectid = last_byte;
981 ins->offset = hole_size;
985 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
987 last_byte = key.objectid;
995 last_byte = key.objectid + key.offset;
997 if (!full_scan && last_byte >= block_group->key.objectid +
998 block_group->key.offset) {
999 btrfs_release_path(root, path);
1000 search_start = block_group->key.objectid +
1001 block_group->key.offset * 2;
1009 /* we have to make sure we didn't find an extent that has already
1010 * been allocated by the map tree or the original allocation
1012 btrfs_release_path(root, path);
1013 BUG_ON(ins->objectid < search_start);
1015 if (ins->objectid + num_bytes >= search_end)
1018 if (test_range_bit(&info->extent_ins, ins->objectid,
1019 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1020 search_start = ins->objectid + num_bytes;
1023 if (test_range_bit(&info->pinned_extents, ins->objectid,
1024 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1025 search_start = ins->objectid + num_bytes;
1028 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
1029 ins->objectid < exclude_start + exclude_nr)) {
1030 search_start = exclude_start + exclude_nr;
1034 block_group = btrfs_lookup_block_group(info, ins->objectid);
1036 trans->block_group = block_group;
1038 ins->offset = num_bytes;
1039 btrfs_free_path(path);
1043 if (search_start + num_bytes >= search_end) {
1045 search_start = orig_search_start;
1052 total_needed -= empty_size;
1057 block_group = btrfs_lookup_block_group(info, search_start);
1060 block_group = btrfs_find_block_group(root, block_group,
1061 search_start, data, 0);
1065 btrfs_release_path(root, path);
1066 btrfs_free_path(path);
1070 * finds a free extent and does all the dirty work required for allocation
1071 * returns the key for the extent through ins, and a tree buffer for
1072 * the first block of the extent through buf.
1074 * returns 0 if everything worked, non-zero otherwise.
1076 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1077 struct btrfs_root *root, u64 owner,
1078 u64 num_bytes, u64 empty_size, u64 hint_byte,
1079 u64 search_end, struct btrfs_key *ins, int data)
1083 u64 super_used, root_used;
1084 u64 search_start = 0;
1085 struct btrfs_fs_info *info = root->fs_info;
1086 struct btrfs_root *extent_root = info->extent_root;
1087 struct btrfs_extent_item extent_item;
1089 btrfs_set_stack_extent_refs(&extent_item, 1);
1090 btrfs_set_stack_extent_owner(&extent_item, owner);
1092 WARN_ON(num_bytes < root->sectorsize);
1093 ret = find_free_extent(trans, root, num_bytes, empty_size,
1094 search_start, search_end, hint_byte, ins,
1095 trans->alloc_exclude_start,
1096 trans->alloc_exclude_nr, data);
1101 /* block accounting for super block */
1102 super_used = btrfs_super_bytes_used(&info->super_copy);
1103 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
1105 /* block accounting for root item */
1106 root_used = btrfs_root_used(&root->root_item);
1107 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
1109 clear_extent_dirty(&root->fs_info->free_space_cache,
1110 ins->objectid, ins->objectid + ins->offset - 1,
1113 if (root == extent_root) {
1114 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1115 ins->objectid + ins->offset - 1,
1116 EXTENT_LOCKED, GFP_NOFS);
1120 WARN_ON(trans->alloc_exclude_nr);
1121 trans->alloc_exclude_start = ins->objectid;
1122 trans->alloc_exclude_nr = ins->offset;
1123 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1124 sizeof(extent_item));
1126 trans->alloc_exclude_start = 0;
1127 trans->alloc_exclude_nr = 0;
1130 finish_current_insert(trans, extent_root);
1131 pending_ret = del_pending_extents(trans, extent_root);
1141 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1148 * helper function to allocate a block for a given tree
1149 * returns the tree buffer or NULL.
1151 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1152 struct btrfs_root *root,
1153 u32 blocksize, u64 hint,
1156 struct btrfs_key ins;
1158 struct extent_buffer *buf;
1160 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
1161 blocksize, empty_size, hint,
1165 return ERR_PTR(ret);
1167 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
1169 btrfs_free_extent(trans, root, ins.objectid, blocksize, 0);
1170 return ERR_PTR(-ENOMEM);
1172 btrfs_set_buffer_uptodate(buf);
1173 buf->alloc_addr = (unsigned long)__builtin_return_address(0);
1174 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1175 buf->start + buf->len - 1, GFP_NOFS);
1176 btrfs_set_buffer_defrag(buf);
1177 trans->blocks_used++;
1181 static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1182 struct btrfs_root *root, struct extent_buffer *leaf)
1184 struct btrfs_key key;
1185 struct btrfs_file_extent_item *fi;
1190 BUG_ON(!btrfs_is_leaf(leaf));
1191 nritems = btrfs_header_nritems(leaf);
1192 for (i = 0; i < nritems; i++) {
1195 btrfs_item_key_to_cpu(leaf, &key, i);
1196 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1198 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1199 if (btrfs_file_extent_type(leaf, fi) ==
1200 BTRFS_FILE_EXTENT_INLINE)
1203 * FIXME make sure to insert a trans record that
1204 * repeats the snapshot del on crash
1206 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1207 if (disk_bytenr == 0)
1209 ret = btrfs_free_extent(trans, root, disk_bytenr,
1210 btrfs_file_extent_disk_num_bytes(leaf, fi), 0);
1216 static void reada_walk_down(struct btrfs_root *root,
1217 struct extent_buffer *node)
1227 nritems = btrfs_header_nritems(node);
1228 level = btrfs_header_level(node);
1229 for (i = 0; i < nritems; i++) {
1230 bytenr = btrfs_node_blockptr(node, i);
1231 blocksize = btrfs_level_size(root, level - 1);
1232 ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
1236 mutex_unlock(&root->fs_info->fs_mutex);
1237 ret = readahead_tree_block(root, bytenr, blocksize);
1239 mutex_lock(&root->fs_info->fs_mutex);
1246 * helper function for drop_snapshot, this walks down the tree dropping ref
1247 * counts as it goes.
1249 static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1250 *root, struct btrfs_path *path, int *level)
1252 struct extent_buffer *next;
1253 struct extent_buffer *cur;
1259 WARN_ON(*level < 0);
1260 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1261 ret = lookup_extent_ref(trans, root,
1262 path->nodes[*level]->start,
1263 path->nodes[*level]->len, &refs);
1269 * walk down to the last node level and free all the leaves
1271 while(*level >= 0) {
1272 WARN_ON(*level < 0);
1273 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1274 cur = path->nodes[*level];
1276 if (*level > 0 && path->slots[*level] == 0)
1277 reada_walk_down(root, cur);
1279 if (btrfs_header_level(cur) != *level)
1282 if (path->slots[*level] >=
1283 btrfs_header_nritems(cur))
1286 ret = drop_leaf_ref(trans, root, cur);
1290 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1291 blocksize = btrfs_level_size(root, *level - 1);
1292 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
1295 path->slots[*level]++;
1296 ret = btrfs_free_extent(trans, root, bytenr,
1301 next = btrfs_find_tree_block(root, bytenr, blocksize);
1302 if (!next || !btrfs_buffer_uptodate(next)) {
1303 free_extent_buffer(next);
1304 mutex_unlock(&root->fs_info->fs_mutex);
1305 next = read_tree_block(root, bytenr, blocksize);
1306 mutex_lock(&root->fs_info->fs_mutex);
1308 /* we dropped the lock, check one more time */
1309 ret = lookup_extent_ref(trans, root, bytenr,
1313 path->slots[*level]++;
1314 free_extent_buffer(next);
1315 ret = btrfs_free_extent(trans, root,
1316 bytenr, blocksize, 1);
1321 WARN_ON(*level <= 0);
1322 if (path->nodes[*level-1])
1323 free_extent_buffer(path->nodes[*level-1]);
1324 path->nodes[*level-1] = next;
1325 *level = btrfs_header_level(next);
1326 path->slots[*level] = 0;
1329 WARN_ON(*level < 0);
1330 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1331 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
1332 path->nodes[*level]->len, 1);
1333 free_extent_buffer(path->nodes[*level]);
1334 path->nodes[*level] = NULL;
1341 * helper for dropping snapshots. This walks back up the tree in the path
1342 * to find the first node higher up where we haven't yet gone through
1345 static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1346 *root, struct btrfs_path *path, int *level)
1351 struct btrfs_root_item *root_item = &root->root_item;
1353 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1354 slot = path->slots[i];
1355 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1356 struct extent_buffer *node;
1357 struct btrfs_disk_key disk_key;
1358 node = path->nodes[i];
1361 WARN_ON(*level == 0);
1362 btrfs_node_key(node, &disk_key, path->slots[i]);
1363 memcpy(&root_item->drop_progress,
1364 &disk_key, sizeof(disk_key));
1365 root_item->drop_level = i;
1368 ret = btrfs_free_extent(trans, root,
1369 path->nodes[*level]->start,
1370 path->nodes[*level]->len, 1);
1372 free_extent_buffer(path->nodes[*level]);
1373 path->nodes[*level] = NULL;
1381 * drop the reference count on the tree rooted at 'snap'. This traverses
1382 * the tree freeing any blocks that have a ref count of zero after being
1385 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
1391 struct btrfs_path *path;
1394 struct btrfs_root_item *root_item = &root->root_item;
1396 path = btrfs_alloc_path();
1399 level = btrfs_header_level(root->node);
1401 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1402 path->nodes[level] = root->node;
1403 extent_buffer_get(root->node);
1404 path->slots[level] = 0;
1406 struct btrfs_key key;
1407 struct btrfs_disk_key found_key;
1408 struct extent_buffer *node;
1410 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1411 level = root_item->drop_level;
1412 path->lowest_level = level;
1413 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1418 node = path->nodes[level];
1419 btrfs_node_key(node, &found_key, path->slots[level]);
1420 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1421 sizeof(found_key)));
1424 wret = walk_down_tree(trans, root, path, &level);
1430 wret = walk_up_tree(trans, root, path, &level);
1438 for (i = 0; i <= orig_level; i++) {
1439 if (path->nodes[i]) {
1440 free_extent_buffer(path->nodes[i]);
1445 btrfs_free_path(path);
1449 int btrfs_free_block_groups(struct btrfs_fs_info *info)
1456 ret = find_first_extent_bit(&info->block_group_cache, 0,
1457 &start, &end, (unsigned int)-1);
1460 clear_extent_bits(&info->block_group_cache, start,
1461 end, (unsigned int)-1, GFP_NOFS);
1464 ret = find_first_extent_bit(&info->free_space_cache, 0,
1465 &start, &end, EXTENT_DIRTY);
1468 clear_extent_dirty(&info->free_space_cache, start,
1474 int btrfs_read_block_groups(struct btrfs_root *root)
1476 struct btrfs_path *path;
1480 struct btrfs_block_group_cache *cache;
1481 struct btrfs_fs_info *info = root->fs_info;
1482 struct extent_map_tree *block_group_cache;
1483 struct btrfs_key key;
1484 struct btrfs_key found_key;
1485 struct extent_buffer *leaf;
1487 block_group_cache = &info->block_group_cache;
1489 root = info->extent_root;
1491 key.offset = BTRFS_BLOCK_GROUP_SIZE;
1492 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1494 path = btrfs_alloc_path();
1499 ret = btrfs_search_slot(NULL, info->extent_root,
1505 leaf = path->nodes[0];
1506 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1507 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1513 read_extent_buffer(leaf, &cache->item,
1514 btrfs_item_ptr_offset(leaf, path->slots[0]),
1515 sizeof(cache->item));
1516 memcpy(&cache->key, &found_key, sizeof(found_key));
1519 key.objectid = found_key.objectid + found_key.offset;
1520 btrfs_release_path(root, path);
1522 if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
1523 bit = BLOCK_GROUP_DATA;
1526 bit = BLOCK_GROUP_METADATA;
1530 /* use EXTENT_LOCKED to prevent merging */
1531 set_extent_bits(block_group_cache, found_key.objectid,
1532 found_key.objectid + found_key.offset - 1,
1533 bit | EXTENT_LOCKED, GFP_NOFS);
1534 set_state_private(block_group_cache, found_key.objectid,
1538 btrfs_super_total_bytes(&info->super_copy))
1542 btrfs_free_path(path);