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 *)(unsigned long)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;
173 ret = cache_block_group(root, cache);
176 last = max(search_start, cache->key.objectid);
179 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
180 last, &start, &end, EXTENT_DIRTY);
185 start = max(last, start);
187 if (end + 1 - start < num)
189 if (start + num >= cache->key.objectid + cache->key.offset)
197 last = cache->key.objectid + cache->key.offset;
198 cache = btrfs_lookup_block_group(root->fs_info, last);
202 cache = btrfs_find_block_group(root, cache, last, data, 0);
207 static u64 div_factor(u64 num, int factor)
214 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
215 struct btrfs_block_group_cache
216 *hint, u64 search_start,
219 struct btrfs_block_group_cache *cache;
220 struct extent_map_tree *block_group_cache;
221 struct btrfs_block_group_cache *found_group = NULL;
222 struct btrfs_fs_info *info = root->fs_info;
236 block_group_cache = &info->block_group_cache;
242 bit = BLOCK_GROUP_DATA;
244 bit = BLOCK_GROUP_METADATA;
247 struct btrfs_block_group_cache *shint;
248 shint = btrfs_lookup_block_group(info, search_start);
249 if (shint && shint->data == data) {
250 used = btrfs_block_group_used(&shint->item);
251 if (used < div_factor(shint->key.offset, factor)) {
256 if (hint && hint->data == data) {
257 used = btrfs_block_group_used(&hint->item);
258 if (used < div_factor(hint->key.offset, factor)) {
261 last = hint->key.objectid + hint->key.offset;
265 hint_last = max(hint->key.objectid, search_start);
267 hint_last = search_start;
273 ret = find_first_extent_bit(block_group_cache, last,
278 ret = get_state_private(block_group_cache, start, &ptr);
282 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
283 last = cache->key.objectid + cache->key.offset;
284 used = btrfs_block_group_used(&cache->item);
287 free_check = cache->key.offset;
289 free_check = div_factor(cache->key.offset, factor);
291 if (used < free_check) {
304 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
312 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
313 struct btrfs_root *root,
314 u64 bytenr, u64 num_bytes)
316 struct btrfs_path *path;
318 struct btrfs_key key;
319 struct extent_buffer *l;
320 struct btrfs_extent_item *item;
323 WARN_ON(num_bytes < root->sectorsize);
324 path = btrfs_alloc_path();
328 key.objectid = bytenr;
329 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
330 key.offset = num_bytes;
331 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
340 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
341 refs = btrfs_extent_refs(l, item);
342 btrfs_set_extent_refs(l, item, refs + 1);
343 btrfs_mark_buffer_dirty(path->nodes[0]);
345 btrfs_release_path(root->fs_info->extent_root, path);
346 btrfs_free_path(path);
347 finish_current_insert(trans, root->fs_info->extent_root);
348 del_pending_extents(trans, root->fs_info->extent_root);
352 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
353 struct btrfs_root *root)
355 finish_current_insert(trans, root->fs_info->extent_root);
356 del_pending_extents(trans, root->fs_info->extent_root);
360 static int lookup_extent_ref(struct btrfs_trans_handle *trans,
361 struct btrfs_root *root, u64 bytenr,
362 u64 num_bytes, u32 *refs)
364 struct btrfs_path *path;
366 struct btrfs_key key;
367 struct extent_buffer *l;
368 struct btrfs_extent_item *item;
370 WARN_ON(num_bytes < root->sectorsize);
371 path = btrfs_alloc_path();
372 key.objectid = bytenr;
373 key.offset = num_bytes;
374 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
375 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
380 btrfs_print_leaf(root, path->nodes[0]);
381 printk("failed to find block number %Lu\n", bytenr);
385 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
386 *refs = btrfs_extent_refs(l, item);
388 btrfs_free_path(path);
392 int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
393 struct btrfs_root *root)
395 return btrfs_inc_extent_ref(trans, root, root->node->start,
399 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
400 struct extent_buffer *buf)
404 struct btrfs_key key;
405 struct btrfs_file_extent_item *fi;
415 level = btrfs_header_level(buf);
416 nritems = btrfs_header_nritems(buf);
417 for (i = 0; i < nritems; i++) {
420 btrfs_item_key_to_cpu(buf, &key, i);
421 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
423 fi = btrfs_item_ptr(buf, i,
424 struct btrfs_file_extent_item);
425 if (btrfs_file_extent_type(buf, fi) ==
426 BTRFS_FILE_EXTENT_INLINE)
428 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
429 if (disk_bytenr == 0)
431 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
432 btrfs_file_extent_disk_num_bytes(buf, fi));
438 bytenr = btrfs_node_blockptr(buf, i);
439 ret = btrfs_inc_extent_ref(trans, root, bytenr,
440 btrfs_level_size(root, level - 1));
450 for (i =0; i < faili; i++) {
453 btrfs_item_key_to_cpu(buf, &key, i);
454 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
456 fi = btrfs_item_ptr(buf, i,
457 struct btrfs_file_extent_item);
458 if (btrfs_file_extent_type(buf, fi) ==
459 BTRFS_FILE_EXTENT_INLINE)
461 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
462 if (disk_bytenr == 0)
464 err = btrfs_free_extent(trans, root, disk_bytenr,
465 btrfs_file_extent_disk_num_bytes(buf,
469 bytenr = btrfs_node_blockptr(buf, i);
470 err = btrfs_free_extent(trans, root, bytenr,
471 btrfs_level_size(root, level - 1), 0);
478 static int write_one_cache_group(struct btrfs_trans_handle *trans,
479 struct btrfs_root *root,
480 struct btrfs_path *path,
481 struct btrfs_block_group_cache *cache)
485 struct btrfs_root *extent_root = root->fs_info->extent_root;
487 struct extent_buffer *leaf;
489 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
494 leaf = path->nodes[0];
495 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
496 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
497 btrfs_mark_buffer_dirty(leaf);
498 btrfs_release_path(extent_root, path);
500 finish_current_insert(trans, extent_root);
501 pending_ret = del_pending_extents(trans, extent_root);
510 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
511 struct btrfs_root *root)
513 struct extent_map_tree *block_group_cache;
514 struct btrfs_block_group_cache *cache;
518 struct btrfs_path *path;
524 block_group_cache = &root->fs_info->block_group_cache;
525 path = btrfs_alloc_path();
530 ret = find_first_extent_bit(block_group_cache, last,
531 &start, &end, BLOCK_GROUP_DIRTY);
536 ret = get_state_private(block_group_cache, start, &ptr);
540 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
541 err = write_one_cache_group(trans, root,
544 * if we fail to write the cache group, we want
545 * to keep it marked dirty in hopes that a later
552 clear_extent_bits(block_group_cache, start, end,
553 BLOCK_GROUP_DIRTY, GFP_NOFS);
555 btrfs_free_path(path);
559 static int update_block_group(struct btrfs_trans_handle *trans,
560 struct btrfs_root *root,
561 u64 bytenr, u64 num_bytes, int alloc,
562 int mark_free, int data)
564 struct btrfs_block_group_cache *cache;
565 struct btrfs_fs_info *info = root->fs_info;
566 u64 total = num_bytes;
573 cache = btrfs_lookup_block_group(info, bytenr);
577 byte_in_group = bytenr - cache->key.objectid;
578 WARN_ON(byte_in_group > cache->key.offset);
579 start = cache->key.objectid;
580 end = start + cache->key.offset - 1;
581 set_extent_bits(&info->block_group_cache, start, end,
582 BLOCK_GROUP_DIRTY, GFP_NOFS);
584 old_val = btrfs_block_group_used(&cache->item);
585 num_bytes = min(total, cache->key.offset - byte_in_group);
587 if (cache->data != data &&
588 old_val < (cache->key.offset >> 1)) {
593 bit_to_clear = BLOCK_GROUP_METADATA;
594 bit_to_set = BLOCK_GROUP_DATA;
596 BTRFS_BLOCK_GROUP_DATA;
598 bit_to_clear = BLOCK_GROUP_DATA;
599 bit_to_set = BLOCK_GROUP_METADATA;
601 ~BTRFS_BLOCK_GROUP_DATA;
603 clear_extent_bits(&info->block_group_cache,
604 start, end, bit_to_clear,
606 set_extent_bits(&info->block_group_cache,
607 start, end, bit_to_set,
610 old_val += num_bytes;
612 old_val -= num_bytes;
614 set_extent_dirty(&info->free_space_cache,
615 bytenr, bytenr + num_bytes - 1,
619 btrfs_set_block_group_used(&cache->item, old_val);
626 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
631 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
635 ret = find_first_extent_bit(pinned_extents, last,
636 &start, &end, EXTENT_DIRTY);
639 set_extent_dirty(copy, start, end, GFP_NOFS);
645 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
646 struct btrfs_root *root,
647 struct extent_map_tree *unpin)
652 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
653 struct extent_map_tree *free_space_cache;
655 free_space_cache = &root->fs_info->free_space_cache;
658 ret = find_first_extent_bit(unpin, 0, &start, &end,
663 clear_extent_dirty(pinned_extents, start, end,
665 clear_extent_dirty(unpin, start, end, GFP_NOFS);
666 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
671 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
672 btrfs_root *extent_root)
674 struct btrfs_key ins;
675 struct btrfs_extent_item extent_item;
680 struct btrfs_fs_info *info = extent_root->fs_info;
682 btrfs_set_stack_extent_refs(&extent_item, 1);
683 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
684 btrfs_set_stack_extent_owner(&extent_item,
685 extent_root->root_key.objectid);
688 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
689 &end, EXTENT_LOCKED);
693 ins.objectid = start;
694 ins.offset = end + 1 - start;
695 err = btrfs_insert_item(trans, extent_root, &ins,
696 &extent_item, sizeof(extent_item));
697 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
703 static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
707 struct extent_buffer *buf;
710 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
712 if (btrfs_buffer_uptodate(buf)) {
714 root->fs_info->running_transaction->transid;
715 if (btrfs_header_generation(buf) == transid) {
716 free_extent_buffer(buf);
720 free_extent_buffer(buf);
722 set_extent_dirty(&root->fs_info->pinned_extents,
723 bytenr, bytenr + num_bytes - 1, GFP_NOFS);
725 set_extent_bits(&root->fs_info->pending_del,
726 bytenr, bytenr + num_bytes - 1,
727 EXTENT_LOCKED, GFP_NOFS);
734 * remove an extent from the root, returns 0 on success
736 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
737 *root, u64 bytenr, u64 num_bytes, int pin,
740 struct btrfs_path *path;
741 struct btrfs_key key;
742 struct btrfs_fs_info *info = root->fs_info;
743 struct btrfs_root *extent_root = info->extent_root;
744 struct extent_buffer *leaf;
746 struct btrfs_extent_item *ei;
749 key.objectid = bytenr;
750 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
751 key.offset = num_bytes;
753 path = btrfs_alloc_path();
757 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
762 leaf = path->nodes[0];
763 ei = btrfs_item_ptr(leaf, path->slots[0],
764 struct btrfs_extent_item);
765 refs = btrfs_extent_refs(leaf, ei);
768 btrfs_set_extent_refs(leaf, ei, refs);
769 btrfs_mark_buffer_dirty(leaf);
776 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
780 /* block accounting for super block */
781 super_used = btrfs_super_bytes_used(&info->super_copy);
782 btrfs_set_super_bytes_used(&info->super_copy,
783 super_used - num_bytes);
785 /* block accounting for root item */
786 root_used = btrfs_root_used(&root->root_item);
787 btrfs_set_root_used(&root->root_item,
788 root_used - num_bytes);
790 ret = btrfs_del_item(trans, extent_root, path);
794 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
798 btrfs_free_path(path);
799 finish_current_insert(trans, extent_root);
804 * find all the blocks marked as pending in the radix tree and remove
805 * them from the extent map
807 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
808 btrfs_root *extent_root)
814 struct extent_map_tree *pending_del;
815 struct extent_map_tree *pinned_extents;
817 pending_del = &extent_root->fs_info->pending_del;
818 pinned_extents = &extent_root->fs_info->pinned_extents;
821 ret = find_first_extent_bit(pending_del, 0, &start, &end,
826 set_extent_dirty(pinned_extents, start, end, GFP_NOFS);
827 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
829 ret = __free_extent(trans, extent_root,
830 start, end + 1 - start, 0, 0);
838 * remove an extent from the root, returns 0 on success
840 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
841 *root, u64 bytenr, u64 num_bytes, int pin)
843 struct btrfs_root *extent_root = root->fs_info->extent_root;
847 WARN_ON(num_bytes < root->sectorsize);
848 if (root == extent_root) {
849 pin_down_bytes(root, bytenr, num_bytes, 1);
852 ret = __free_extent(trans, root, bytenr, num_bytes, pin, pin == 0);
853 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
854 return ret ? ret : pending_ret;
858 * walks the btree of allocated extents and find a hole of a given size.
859 * The key ins is changed to record the hole:
860 * ins->objectid == block start
861 * ins->flags = BTRFS_EXTENT_ITEM_KEY
862 * ins->offset == number of blocks
863 * Any available blocks before search_start are skipped.
865 static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
866 *orig_root, u64 num_bytes, u64 empty_size,
867 u64 search_start, u64 search_end, u64 hint_byte,
868 struct btrfs_key *ins, u64 exclude_start,
869 u64 exclude_nr, int data)
871 struct btrfs_path *path;
872 struct btrfs_key key;
877 u64 orig_search_start = search_start;
879 struct extent_buffer *l;
880 struct btrfs_root * root = orig_root->fs_info->extent_root;
881 struct btrfs_fs_info *info = root->fs_info;
882 u64 total_needed = num_bytes;
884 struct btrfs_block_group_cache *block_group;
888 WARN_ON(num_bytes < root->sectorsize);
889 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
891 level = btrfs_header_level(root->node);
893 if (search_end == (u64)-1)
894 search_end = btrfs_super_total_bytes(&info->super_copy);
896 block_group = btrfs_lookup_block_group(info, hint_byte);
897 block_group = btrfs_find_block_group(root, block_group,
900 block_group = btrfs_find_block_group(root,
901 trans->block_group, 0,
905 total_needed += empty_size;
906 path = btrfs_alloc_path();
909 search_start = find_search_start(root, &block_group,
910 search_start, total_needed, data);
911 btrfs_init_path(path);
912 ins->objectid = search_start;
917 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
921 if (path->slots[0] > 0) {
926 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
929 * a rare case, go back one key if we hit a block group item
930 * instead of an extent item
932 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
933 key.objectid + key.offset >= search_start) {
934 ins->objectid = key.objectid;
935 ins->offset = key.offset - 1;
936 btrfs_release_path(root, path);
937 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
941 if (path->slots[0] > 0) {
948 slot = path->slots[0];
949 if (slot >= btrfs_header_nritems(l)) {
950 ret = btrfs_next_leaf(root, path);
956 search_start = max(search_start,
957 block_group->key.objectid);
959 ins->objectid = search_start;
960 ins->offset = search_end - search_start;
964 ins->objectid = last_byte > search_start ?
965 last_byte : search_start;
966 ins->offset = search_end - ins->objectid;
967 BUG_ON(ins->objectid >= search_end);
970 btrfs_item_key_to_cpu(l, &key, slot);
972 if (key.objectid >= search_start && key.objectid > last_byte &&
974 if (last_byte < search_start)
975 last_byte = search_start;
976 hole_size = key.objectid - last_byte;
977 if (hole_size >= num_bytes) {
978 ins->objectid = last_byte;
979 ins->offset = hole_size;
983 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
985 last_byte = key.objectid;
993 last_byte = key.objectid + key.offset;
995 if (!full_scan && last_byte >= block_group->key.objectid +
996 block_group->key.offset) {
997 btrfs_release_path(root, path);
998 search_start = block_group->key.objectid +
999 block_group->key.offset;
1007 /* we have to make sure we didn't find an extent that has already
1008 * been allocated by the map tree or the original allocation
1010 btrfs_release_path(root, path);
1011 BUG_ON(ins->objectid < search_start);
1013 if (ins->objectid + num_bytes >= search_end)
1016 if (!full_scan && ins->objectid + num_bytes >= block_group->
1017 key.objectid + block_group->key.offset) {
1018 search_start = block_group->key.objectid +
1019 block_group->key.offset;
1022 if (test_range_bit(&info->extent_ins, ins->objectid,
1023 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1024 search_start = ins->objectid + num_bytes;
1027 if (test_range_bit(&info->pinned_extents, ins->objectid,
1028 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1029 search_start = ins->objectid + num_bytes;
1032 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
1033 ins->objectid < exclude_start + exclude_nr)) {
1034 search_start = exclude_start + exclude_nr;
1038 block_group = btrfs_lookup_block_group(info, ins->objectid);
1040 trans->block_group = block_group;
1042 ins->offset = num_bytes;
1043 btrfs_free_path(path);
1047 if (search_start + num_bytes >= search_end) {
1049 search_start = orig_search_start;
1056 total_needed -= empty_size;
1061 block_group = btrfs_lookup_block_group(info, search_start);
1064 block_group = btrfs_find_block_group(root, block_group,
1065 search_start, data, 0);
1069 btrfs_release_path(root, path);
1070 btrfs_free_path(path);
1074 * finds a free extent and does all the dirty work required for allocation
1075 * returns the key for the extent through ins, and a tree buffer for
1076 * the first block of the extent through buf.
1078 * returns 0 if everything worked, non-zero otherwise.
1080 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1081 struct btrfs_root *root, u64 owner,
1082 u64 num_bytes, u64 empty_size, u64 hint_byte,
1083 u64 search_end, struct btrfs_key *ins, int data)
1087 u64 super_used, root_used;
1088 u64 search_start = 0;
1089 struct btrfs_fs_info *info = root->fs_info;
1090 struct btrfs_root *extent_root = info->extent_root;
1091 struct btrfs_extent_item extent_item;
1093 btrfs_set_stack_extent_refs(&extent_item, 1);
1094 btrfs_set_stack_extent_owner(&extent_item, owner);
1096 WARN_ON(num_bytes < root->sectorsize);
1097 ret = find_free_extent(trans, root, num_bytes, empty_size,
1098 search_start, search_end, hint_byte, ins,
1099 trans->alloc_exclude_start,
1100 trans->alloc_exclude_nr, data);
1105 /* block accounting for super block */
1106 super_used = btrfs_super_bytes_used(&info->super_copy);
1107 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
1109 /* block accounting for root item */
1110 root_used = btrfs_root_used(&root->root_item);
1111 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
1113 clear_extent_dirty(&root->fs_info->free_space_cache,
1114 ins->objectid, ins->objectid + ins->offset - 1,
1117 if (root == extent_root) {
1118 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1119 ins->objectid + ins->offset - 1,
1120 EXTENT_LOCKED, GFP_NOFS);
1125 WARN_ON(trans->alloc_exclude_nr);
1126 trans->alloc_exclude_start = ins->objectid;
1127 trans->alloc_exclude_nr = ins->offset;
1128 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1129 sizeof(extent_item));
1131 trans->alloc_exclude_start = 0;
1132 trans->alloc_exclude_nr = 0;
1135 finish_current_insert(trans, extent_root);
1136 pending_ret = del_pending_extents(trans, extent_root);
1146 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1153 * helper function to allocate a block for a given tree
1154 * returns the tree buffer or NULL.
1156 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1157 struct btrfs_root *root,
1158 u32 blocksize, u64 hint,
1161 struct btrfs_key ins;
1163 struct extent_buffer *buf;
1165 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
1166 blocksize, empty_size, hint,
1170 return ERR_PTR(ret);
1172 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
1174 btrfs_free_extent(trans, root, ins.objectid, blocksize, 0);
1175 return ERR_PTR(-ENOMEM);
1177 btrfs_set_buffer_uptodate(buf);
1178 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1179 buf->start + buf->len - 1, GFP_NOFS);
1180 set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,
1181 buf->start, buf->start + buf->len - 1,
1182 EXTENT_CSUM, GFP_NOFS);
1183 buf->flags |= EXTENT_CSUM;
1184 btrfs_set_buffer_defrag(buf);
1185 trans->blocks_used++;
1189 static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1190 struct btrfs_root *root, struct extent_buffer *leaf)
1192 struct btrfs_key key;
1193 struct btrfs_file_extent_item *fi;
1198 BUG_ON(!btrfs_is_leaf(leaf));
1199 nritems = btrfs_header_nritems(leaf);
1200 for (i = 0; i < nritems; i++) {
1203 btrfs_item_key_to_cpu(leaf, &key, i);
1204 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1206 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1207 if (btrfs_file_extent_type(leaf, fi) ==
1208 BTRFS_FILE_EXTENT_INLINE)
1211 * FIXME make sure to insert a trans record that
1212 * repeats the snapshot del on crash
1214 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1215 if (disk_bytenr == 0)
1217 ret = btrfs_free_extent(trans, root, disk_bytenr,
1218 btrfs_file_extent_disk_num_bytes(leaf, fi), 0);
1224 static void reada_walk_down(struct btrfs_root *root,
1225 struct extent_buffer *node)
1235 nritems = btrfs_header_nritems(node);
1236 level = btrfs_header_level(node);
1237 for (i = 0; i < nritems; i++) {
1238 bytenr = btrfs_node_blockptr(node, i);
1239 blocksize = btrfs_level_size(root, level - 1);
1240 ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
1244 mutex_unlock(&root->fs_info->fs_mutex);
1245 ret = readahead_tree_block(root, bytenr, blocksize);
1247 mutex_lock(&root->fs_info->fs_mutex);
1254 * helper function for drop_snapshot, this walks down the tree dropping ref
1255 * counts as it goes.
1257 static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1258 *root, struct btrfs_path *path, int *level)
1260 struct extent_buffer *next;
1261 struct extent_buffer *cur;
1267 WARN_ON(*level < 0);
1268 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1269 ret = lookup_extent_ref(trans, root,
1270 path->nodes[*level]->start,
1271 path->nodes[*level]->len, &refs);
1277 * walk down to the last node level and free all the leaves
1279 while(*level >= 0) {
1280 WARN_ON(*level < 0);
1281 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1282 cur = path->nodes[*level];
1284 if (*level > 0 && path->slots[*level] == 0)
1285 reada_walk_down(root, cur);
1287 if (btrfs_header_level(cur) != *level)
1290 if (path->slots[*level] >=
1291 btrfs_header_nritems(cur))
1294 ret = drop_leaf_ref(trans, root, cur);
1298 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1299 blocksize = btrfs_level_size(root, *level - 1);
1300 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
1303 path->slots[*level]++;
1304 ret = btrfs_free_extent(trans, root, bytenr,
1309 next = btrfs_find_tree_block(root, bytenr, blocksize);
1310 if (!next || !btrfs_buffer_uptodate(next)) {
1311 free_extent_buffer(next);
1312 mutex_unlock(&root->fs_info->fs_mutex);
1313 next = read_tree_block(root, bytenr, blocksize);
1314 mutex_lock(&root->fs_info->fs_mutex);
1316 /* we dropped the lock, check one more time */
1317 ret = lookup_extent_ref(trans, root, bytenr,
1321 path->slots[*level]++;
1322 free_extent_buffer(next);
1323 ret = btrfs_free_extent(trans, root,
1324 bytenr, blocksize, 1);
1329 WARN_ON(*level <= 0);
1330 if (path->nodes[*level-1])
1331 free_extent_buffer(path->nodes[*level-1]);
1332 path->nodes[*level-1] = next;
1333 *level = btrfs_header_level(next);
1334 path->slots[*level] = 0;
1337 WARN_ON(*level < 0);
1338 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1339 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
1340 path->nodes[*level]->len, 1);
1341 free_extent_buffer(path->nodes[*level]);
1342 path->nodes[*level] = NULL;
1349 * helper for dropping snapshots. This walks back up the tree in the path
1350 * to find the first node higher up where we haven't yet gone through
1353 static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1354 *root, struct btrfs_path *path, int *level)
1359 struct btrfs_root_item *root_item = &root->root_item;
1361 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1362 slot = path->slots[i];
1363 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1364 struct extent_buffer *node;
1365 struct btrfs_disk_key disk_key;
1366 node = path->nodes[i];
1369 WARN_ON(*level == 0);
1370 btrfs_node_key(node, &disk_key, path->slots[i]);
1371 memcpy(&root_item->drop_progress,
1372 &disk_key, sizeof(disk_key));
1373 root_item->drop_level = i;
1376 ret = btrfs_free_extent(trans, root,
1377 path->nodes[*level]->start,
1378 path->nodes[*level]->len, 1);
1380 free_extent_buffer(path->nodes[*level]);
1381 path->nodes[*level] = NULL;
1389 * drop the reference count on the tree rooted at 'snap'. This traverses
1390 * the tree freeing any blocks that have a ref count of zero after being
1393 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
1399 struct btrfs_path *path;
1402 struct btrfs_root_item *root_item = &root->root_item;
1404 path = btrfs_alloc_path();
1407 level = btrfs_header_level(root->node);
1409 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1410 path->nodes[level] = root->node;
1411 extent_buffer_get(root->node);
1412 path->slots[level] = 0;
1414 struct btrfs_key key;
1415 struct btrfs_disk_key found_key;
1416 struct extent_buffer *node;
1418 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1419 level = root_item->drop_level;
1420 path->lowest_level = level;
1421 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1426 node = path->nodes[level];
1427 btrfs_node_key(node, &found_key, path->slots[level]);
1428 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1429 sizeof(found_key)));
1432 wret = walk_down_tree(trans, root, path, &level);
1438 wret = walk_up_tree(trans, root, path, &level);
1446 for (i = 0; i <= orig_level; i++) {
1447 if (path->nodes[i]) {
1448 free_extent_buffer(path->nodes[i]);
1449 path->nodes[i] = NULL;
1453 btrfs_free_path(path);
1457 int btrfs_free_block_groups(struct btrfs_fs_info *info)
1464 ret = find_first_extent_bit(&info->block_group_cache, 0,
1465 &start, &end, (unsigned int)-1);
1468 ret = get_state_private(&info->block_group_cache, start, &ptr);
1470 kfree((void *)(unsigned long)ptr);
1471 clear_extent_bits(&info->block_group_cache, start,
1472 end, (unsigned int)-1, GFP_NOFS);
1475 ret = find_first_extent_bit(&info->free_space_cache, 0,
1476 &start, &end, EXTENT_DIRTY);
1479 clear_extent_dirty(&info->free_space_cache, start,
1485 int btrfs_read_block_groups(struct btrfs_root *root)
1487 struct btrfs_path *path;
1491 struct btrfs_block_group_cache *cache;
1492 struct btrfs_fs_info *info = root->fs_info;
1493 struct extent_map_tree *block_group_cache;
1494 struct btrfs_key key;
1495 struct btrfs_key found_key;
1496 struct extent_buffer *leaf;
1498 block_group_cache = &info->block_group_cache;
1500 root = info->extent_root;
1502 key.offset = BTRFS_BLOCK_GROUP_SIZE;
1503 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1505 path = btrfs_alloc_path();
1510 ret = btrfs_search_slot(NULL, info->extent_root,
1516 leaf = path->nodes[0];
1517 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1518 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1524 read_extent_buffer(leaf, &cache->item,
1525 btrfs_item_ptr_offset(leaf, path->slots[0]),
1526 sizeof(cache->item));
1527 memcpy(&cache->key, &found_key, sizeof(found_key));
1530 key.objectid = found_key.objectid + found_key.offset;
1531 btrfs_release_path(root, path);
1533 if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
1534 bit = BLOCK_GROUP_DATA;
1537 bit = BLOCK_GROUP_METADATA;
1541 /* use EXTENT_LOCKED to prevent merging */
1542 set_extent_bits(block_group_cache, found_key.objectid,
1543 found_key.objectid + found_key.offset - 1,
1544 bit | EXTENT_LOCKED, GFP_NOFS);
1545 set_state_private(block_group_cache, found_key.objectid,
1546 (unsigned long)cache);
1549 btrfs_super_total_bytes(&info->super_copy))
1553 btrfs_free_path(path);