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;
175 ret = cache_block_group(root, cache);
179 last = max(search_start, cache->key.objectid);
182 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
183 last, &start, &end, EXTENT_DIRTY);
190 start = max(last, start);
192 if (last - start < num) {
193 if (last == cache->key.objectid + cache->key.offset)
197 if (data != BTRFS_BLOCK_GROUP_MIXED &&
198 start + num >= cache->key.objectid + cache->key.offset)
206 last = cache->key.objectid + cache->key.offset;
208 cache = btrfs_lookup_block_group(root->fs_info, last);
213 data = BTRFS_BLOCK_GROUP_MIXED;
218 if (cache_miss && !cache->cached) {
219 cache_block_group(root, cache);
222 cache = btrfs_lookup_block_group(root->fs_info, last);
224 cache = btrfs_find_block_group(root, cache, last, data, 0);
230 static u64 div_factor(u64 num, int factor)
239 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
240 struct btrfs_block_group_cache
241 *hint, u64 search_start,
244 struct btrfs_block_group_cache *cache;
245 struct extent_map_tree *block_group_cache;
246 struct btrfs_block_group_cache *found_group = NULL;
247 struct btrfs_fs_info *info = root->fs_info;
261 block_group_cache = &info->block_group_cache;
266 if (data == BTRFS_BLOCK_GROUP_MIXED) {
267 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
270 bit = BLOCK_GROUP_DATA;
272 bit = BLOCK_GROUP_METADATA;
275 struct btrfs_block_group_cache *shint;
276 shint = btrfs_lookup_block_group(info, search_start);
277 if (shint && (shint->data == data ||
278 shint->data == BTRFS_BLOCK_GROUP_MIXED)) {
279 used = btrfs_block_group_used(&shint->item);
280 if (used < div_factor(shint->key.offset, factor)) {
285 if (hint && (hint->data == data ||
286 hint->data == BTRFS_BLOCK_GROUP_MIXED)) {
287 used = btrfs_block_group_used(&hint->item);
288 if (used < div_factor(hint->key.offset, factor)) {
291 last = hint->key.objectid + hint->key.offset;
295 hint_last = max(hint->key.objectid, search_start);
297 hint_last = search_start;
303 ret = find_first_extent_bit(block_group_cache, last,
308 ret = get_state_private(block_group_cache, start, &ptr);
312 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
313 last = cache->key.objectid + cache->key.offset;
314 used = btrfs_block_group_used(&cache->item);
317 free_check = cache->key.offset;
319 free_check = div_factor(cache->key.offset, factor);
321 if (used < free_check) {
334 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
342 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
343 struct btrfs_root *root,
344 u64 bytenr, u64 num_bytes)
346 struct btrfs_path *path;
348 struct btrfs_key key;
349 struct extent_buffer *l;
350 struct btrfs_extent_item *item;
353 WARN_ON(num_bytes < root->sectorsize);
354 path = btrfs_alloc_path();
358 key.objectid = bytenr;
359 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
360 key.offset = num_bytes;
361 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
370 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
371 refs = btrfs_extent_refs(l, item);
372 btrfs_set_extent_refs(l, item, refs + 1);
373 btrfs_mark_buffer_dirty(path->nodes[0]);
375 btrfs_release_path(root->fs_info->extent_root, path);
376 btrfs_free_path(path);
377 finish_current_insert(trans, root->fs_info->extent_root);
378 del_pending_extents(trans, root->fs_info->extent_root);
382 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
383 struct btrfs_root *root)
385 finish_current_insert(trans, root->fs_info->extent_root);
386 del_pending_extents(trans, root->fs_info->extent_root);
390 static int lookup_extent_ref(struct btrfs_trans_handle *trans,
391 struct btrfs_root *root, u64 bytenr,
392 u64 num_bytes, u32 *refs)
394 struct btrfs_path *path;
396 struct btrfs_key key;
397 struct extent_buffer *l;
398 struct btrfs_extent_item *item;
400 WARN_ON(num_bytes < root->sectorsize);
401 path = btrfs_alloc_path();
402 key.objectid = bytenr;
403 key.offset = num_bytes;
404 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
405 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
410 btrfs_print_leaf(root, path->nodes[0]);
411 printk("failed to find block number %Lu\n", bytenr);
415 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
416 *refs = btrfs_extent_refs(l, item);
418 btrfs_free_path(path);
422 int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
423 struct btrfs_root *root)
425 return btrfs_inc_extent_ref(trans, root, root->node->start,
429 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
430 struct extent_buffer *buf)
434 struct btrfs_key key;
435 struct btrfs_file_extent_item *fi;
445 level = btrfs_header_level(buf);
446 nritems = btrfs_header_nritems(buf);
447 for (i = 0; i < nritems; i++) {
450 btrfs_item_key_to_cpu(buf, &key, i);
451 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
453 fi = btrfs_item_ptr(buf, i,
454 struct btrfs_file_extent_item);
455 if (btrfs_file_extent_type(buf, fi) ==
456 BTRFS_FILE_EXTENT_INLINE)
458 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
459 if (disk_bytenr == 0)
461 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
462 btrfs_file_extent_disk_num_bytes(buf, fi));
468 bytenr = btrfs_node_blockptr(buf, i);
469 ret = btrfs_inc_extent_ref(trans, root, bytenr,
470 btrfs_level_size(root, level - 1));
480 for (i =0; i < faili; i++) {
483 btrfs_item_key_to_cpu(buf, &key, i);
484 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
486 fi = btrfs_item_ptr(buf, i,
487 struct btrfs_file_extent_item);
488 if (btrfs_file_extent_type(buf, fi) ==
489 BTRFS_FILE_EXTENT_INLINE)
491 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
492 if (disk_bytenr == 0)
494 err = btrfs_free_extent(trans, root, disk_bytenr,
495 btrfs_file_extent_disk_num_bytes(buf,
499 bytenr = btrfs_node_blockptr(buf, i);
500 err = btrfs_free_extent(trans, root, bytenr,
501 btrfs_level_size(root, level - 1), 0);
508 static int write_one_cache_group(struct btrfs_trans_handle *trans,
509 struct btrfs_root *root,
510 struct btrfs_path *path,
511 struct btrfs_block_group_cache *cache)
515 struct btrfs_root *extent_root = root->fs_info->extent_root;
517 struct extent_buffer *leaf;
519 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
524 leaf = path->nodes[0];
525 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
526 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
527 btrfs_mark_buffer_dirty(leaf);
528 btrfs_release_path(extent_root, path);
530 finish_current_insert(trans, extent_root);
531 pending_ret = del_pending_extents(trans, extent_root);
540 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
541 struct btrfs_root *root)
543 struct extent_map_tree *block_group_cache;
544 struct btrfs_block_group_cache *cache;
548 struct btrfs_path *path;
554 block_group_cache = &root->fs_info->block_group_cache;
555 path = btrfs_alloc_path();
560 ret = find_first_extent_bit(block_group_cache, last,
561 &start, &end, BLOCK_GROUP_DIRTY);
566 ret = get_state_private(block_group_cache, start, &ptr);
570 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
571 err = write_one_cache_group(trans, root,
574 * if we fail to write the cache group, we want
575 * to keep it marked dirty in hopes that a later
582 clear_extent_bits(block_group_cache, start, end,
583 BLOCK_GROUP_DIRTY, GFP_NOFS);
585 btrfs_free_path(path);
589 static int update_block_group(struct btrfs_trans_handle *trans,
590 struct btrfs_root *root,
591 u64 bytenr, u64 num_bytes, int alloc,
592 int mark_free, int data)
594 struct btrfs_block_group_cache *cache;
595 struct btrfs_fs_info *info = root->fs_info;
596 u64 total = num_bytes;
603 cache = btrfs_lookup_block_group(info, bytenr);
607 byte_in_group = bytenr - cache->key.objectid;
608 WARN_ON(byte_in_group > cache->key.offset);
609 start = cache->key.objectid;
610 end = start + cache->key.offset - 1;
611 set_extent_bits(&info->block_group_cache, start, end,
612 BLOCK_GROUP_DIRTY, GFP_NOFS);
614 old_val = btrfs_block_group_used(&cache->item);
615 num_bytes = min(total, cache->key.offset - byte_in_group);
617 if (cache->data != data &&
618 old_val < (cache->key.offset >> 1)) {
623 bit_to_clear = BLOCK_GROUP_METADATA;
624 bit_to_set = BLOCK_GROUP_DATA;
626 ~BTRFS_BLOCK_GROUP_MIXED;
628 BTRFS_BLOCK_GROUP_DATA;
630 bit_to_clear = BLOCK_GROUP_DATA;
631 bit_to_set = BLOCK_GROUP_METADATA;
633 ~BTRFS_BLOCK_GROUP_MIXED;
635 ~BTRFS_BLOCK_GROUP_DATA;
637 clear_extent_bits(&info->block_group_cache,
638 start, end, bit_to_clear,
640 set_extent_bits(&info->block_group_cache,
641 start, end, bit_to_set,
643 } else if (cache->data != data &&
644 cache->data != BTRFS_BLOCK_GROUP_MIXED) {
645 cache->data = BTRFS_BLOCK_GROUP_MIXED;
646 set_extent_bits(&info->block_group_cache,
649 BLOCK_GROUP_METADATA,
652 old_val += num_bytes;
654 old_val -= num_bytes;
656 set_extent_dirty(&info->free_space_cache,
657 bytenr, bytenr + num_bytes - 1,
661 btrfs_set_block_group_used(&cache->item, old_val);
668 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
673 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
677 ret = find_first_extent_bit(pinned_extents, last,
678 &start, &end, EXTENT_DIRTY);
681 set_extent_dirty(copy, start, end, GFP_NOFS);
687 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
688 struct btrfs_root *root,
689 struct extent_map_tree *unpin)
694 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
695 struct extent_map_tree *free_space_cache;
697 free_space_cache = &root->fs_info->free_space_cache;
700 ret = find_first_extent_bit(unpin, 0, &start, &end,
705 clear_extent_dirty(pinned_extents, start, end,
707 clear_extent_dirty(unpin, start, end, GFP_NOFS);
708 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
713 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
714 btrfs_root *extent_root)
716 struct btrfs_key ins;
717 struct btrfs_extent_item extent_item;
722 struct btrfs_fs_info *info = extent_root->fs_info;
724 btrfs_set_stack_extent_refs(&extent_item, 1);
725 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
726 btrfs_set_stack_extent_owner(&extent_item,
727 extent_root->root_key.objectid);
730 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
731 &end, EXTENT_LOCKED);
735 ins.objectid = start;
736 ins.offset = end + 1 - start;
737 err = btrfs_insert_item(trans, extent_root, &ins,
738 &extent_item, sizeof(extent_item));
739 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
745 static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
749 struct extent_buffer *buf;
752 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
754 if (btrfs_buffer_uptodate(buf)) {
756 root->fs_info->running_transaction->transid;
757 if (btrfs_header_generation(buf) == transid) {
758 free_extent_buffer(buf);
762 free_extent_buffer(buf);
764 set_extent_dirty(&root->fs_info->pinned_extents,
765 bytenr, bytenr + num_bytes - 1, GFP_NOFS);
767 set_extent_bits(&root->fs_info->pending_del,
768 bytenr, bytenr + num_bytes - 1,
769 EXTENT_LOCKED, GFP_NOFS);
776 * remove an extent from the root, returns 0 on success
778 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
779 *root, u64 bytenr, u64 num_bytes, int pin,
782 struct btrfs_path *path;
783 struct btrfs_key key;
784 struct btrfs_fs_info *info = root->fs_info;
785 struct btrfs_root *extent_root = info->extent_root;
786 struct extent_buffer *leaf;
788 struct btrfs_extent_item *ei;
791 key.objectid = bytenr;
792 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
793 key.offset = num_bytes;
795 path = btrfs_alloc_path();
799 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
804 leaf = path->nodes[0];
805 ei = btrfs_item_ptr(leaf, path->slots[0],
806 struct btrfs_extent_item);
807 refs = btrfs_extent_refs(leaf, ei);
810 btrfs_set_extent_refs(leaf, ei, refs);
811 btrfs_mark_buffer_dirty(leaf);
818 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
824 /* block accounting for super block */
825 super_used = btrfs_super_bytes_used(&info->super_copy);
826 btrfs_set_super_bytes_used(&info->super_copy,
827 super_used - num_bytes);
829 /* block accounting for root item */
830 root_used = btrfs_root_used(&root->root_item);
831 btrfs_set_root_used(&root->root_item,
832 root_used - num_bytes);
834 ret = btrfs_del_item(trans, extent_root, path);
838 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
842 btrfs_free_path(path);
843 finish_current_insert(trans, extent_root);
848 * find all the blocks marked as pending in the radix tree and remove
849 * them from the extent map
851 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
852 btrfs_root *extent_root)
858 struct extent_map_tree *pending_del;
859 struct extent_map_tree *pinned_extents;
861 pending_del = &extent_root->fs_info->pending_del;
862 pinned_extents = &extent_root->fs_info->pinned_extents;
865 ret = find_first_extent_bit(pending_del, 0, &start, &end,
870 set_extent_dirty(pinned_extents, start, end, GFP_NOFS);
871 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
873 ret = __free_extent(trans, extent_root,
874 start, end + 1 - start, 0, 0);
882 * remove an extent from the root, returns 0 on success
884 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
885 *root, u64 bytenr, u64 num_bytes, int pin)
887 struct btrfs_root *extent_root = root->fs_info->extent_root;
891 WARN_ON(num_bytes < root->sectorsize);
892 if (root == extent_root) {
893 pin_down_bytes(root, bytenr, num_bytes, 1);
896 ret = __free_extent(trans, root, bytenr, num_bytes, pin, pin == 0);
897 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
898 return ret ? ret : pending_ret;
902 * walks the btree of allocated extents and find a hole of a given size.
903 * The key ins is changed to record the hole:
904 * ins->objectid == block start
905 * ins->flags = BTRFS_EXTENT_ITEM_KEY
906 * ins->offset == number of blocks
907 * Any available blocks before search_start are skipped.
909 static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
910 *orig_root, u64 num_bytes, u64 empty_size,
911 u64 search_start, u64 search_end, u64 hint_byte,
912 struct btrfs_key *ins, u64 exclude_start,
913 u64 exclude_nr, int data)
915 struct btrfs_path *path;
916 struct btrfs_key key;
921 u64 orig_search_start = search_start;
923 struct extent_buffer *l;
924 struct btrfs_root * root = orig_root->fs_info->extent_root;
925 struct btrfs_fs_info *info = root->fs_info;
926 u64 total_needed = num_bytes;
928 struct btrfs_block_group_cache *block_group;
933 WARN_ON(num_bytes < root->sectorsize);
934 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
936 level = btrfs_header_level(root->node);
938 if (num_bytes >= 96 * 1024 * 1024 && hint_byte) {
939 data = BTRFS_BLOCK_GROUP_MIXED;
942 if (search_end == (u64)-1)
943 search_end = btrfs_super_total_bytes(&info->super_copy);
945 block_group = btrfs_lookup_block_group(info, hint_byte);
946 block_group = btrfs_find_block_group(root, block_group,
949 block_group = btrfs_find_block_group(root,
950 trans->block_group, 0,
954 total_needed += empty_size;
955 path = btrfs_alloc_path();
958 search_start = find_search_start(root, &block_group,
959 search_start, total_needed, data);
960 cached_start = search_start;
962 btrfs_init_path(path);
963 ins->objectid = search_start;
968 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
972 if (path->slots[0] > 0) {
977 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
980 * a rare case, go back one key if we hit a block group item
981 * instead of an extent item
983 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
984 key.objectid + key.offset >= search_start) {
985 ins->objectid = key.objectid;
986 ins->offset = key.offset - 1;
987 btrfs_release_path(root, path);
988 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
992 if (path->slots[0] > 0) {
999 slot = path->slots[0];
1000 if (slot >= btrfs_header_nritems(l)) {
1001 ret = btrfs_next_leaf(root, path);
1007 search_start = max(search_start,
1008 block_group->key.objectid);
1010 ins->objectid = search_start;
1011 ins->offset = search_end - search_start;
1015 ins->objectid = last_byte > search_start ?
1016 last_byte : search_start;
1017 ins->offset = search_end - ins->objectid;
1018 BUG_ON(ins->objectid >= search_end);
1021 btrfs_item_key_to_cpu(l, &key, slot);
1023 if (key.objectid >= search_start && key.objectid > last_byte &&
1025 if (last_byte < search_start)
1026 last_byte = search_start;
1027 hole_size = key.objectid - last_byte;
1028 if (hole_size >= num_bytes) {
1029 ins->objectid = last_byte;
1030 ins->offset = hole_size;
1034 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
1036 last_byte = key.objectid;
1044 last_byte = key.objectid + key.offset;
1046 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
1047 last_byte >= block_group->key.objectid +
1048 block_group->key.offset) {
1049 btrfs_release_path(root, path);
1050 search_start = block_group->key.objectid +
1051 block_group->key.offset;
1059 /* we have to make sure we didn't find an extent that has already
1060 * been allocated by the map tree or the original allocation
1062 btrfs_release_path(root, path);
1063 BUG_ON(ins->objectid < search_start);
1065 if (ins->objectid + num_bytes >= search_end)
1068 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
1069 ins->objectid + num_bytes >= block_group->
1070 key.objectid + block_group->key.offset) {
1071 search_start = block_group->key.objectid +
1072 block_group->key.offset;
1075 if (test_range_bit(&info->extent_ins, ins->objectid,
1076 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1077 search_start = ins->objectid + num_bytes;
1080 if (test_range_bit(&info->pinned_extents, ins->objectid,
1081 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1082 search_start = ins->objectid + num_bytes;
1085 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
1086 ins->objectid < exclude_start + exclude_nr)) {
1087 search_start = exclude_start + exclude_nr;
1091 block_group = btrfs_lookup_block_group(info, ins->objectid);
1093 trans->block_group = block_group;
1095 ins->offset = num_bytes;
1096 btrfs_free_path(path);
1100 if (search_start + num_bytes >= search_end) {
1102 search_start = orig_search_start;
1109 total_needed -= empty_size;
1114 block_group = btrfs_lookup_block_group(info, search_start);
1117 block_group = btrfs_find_block_group(root, block_group,
1118 search_start, data, 0);
1122 btrfs_release_path(root, path);
1123 btrfs_free_path(path);
1127 * finds a free extent and does all the dirty work required for allocation
1128 * returns the key for the extent through ins, and a tree buffer for
1129 * the first block of the extent through buf.
1131 * returns 0 if everything worked, non-zero otherwise.
1133 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1134 struct btrfs_root *root, u64 owner,
1135 u64 num_bytes, u64 empty_size, u64 hint_byte,
1136 u64 search_end, struct btrfs_key *ins, int data)
1140 u64 super_used, root_used;
1141 u64 search_start = 0;
1142 struct btrfs_fs_info *info = root->fs_info;
1143 struct btrfs_root *extent_root = info->extent_root;
1144 struct btrfs_extent_item extent_item;
1146 btrfs_set_stack_extent_refs(&extent_item, 1);
1147 btrfs_set_stack_extent_owner(&extent_item, owner);
1149 WARN_ON(num_bytes < root->sectorsize);
1150 ret = find_free_extent(trans, root, num_bytes, empty_size,
1151 search_start, search_end, hint_byte, ins,
1152 trans->alloc_exclude_start,
1153 trans->alloc_exclude_nr, data);
1158 /* block accounting for super block */
1159 super_used = btrfs_super_bytes_used(&info->super_copy);
1160 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
1162 /* block accounting for root item */
1163 root_used = btrfs_root_used(&root->root_item);
1164 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
1166 clear_extent_dirty(&root->fs_info->free_space_cache,
1167 ins->objectid, ins->objectid + ins->offset - 1,
1170 if (root == extent_root) {
1171 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1172 ins->objectid + ins->offset - 1,
1173 EXTENT_LOCKED, GFP_NOFS);
1178 WARN_ON(trans->alloc_exclude_nr);
1179 trans->alloc_exclude_start = ins->objectid;
1180 trans->alloc_exclude_nr = ins->offset;
1181 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1182 sizeof(extent_item));
1184 trans->alloc_exclude_start = 0;
1185 trans->alloc_exclude_nr = 0;
1188 finish_current_insert(trans, extent_root);
1189 pending_ret = del_pending_extents(trans, extent_root);
1199 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1206 * helper function to allocate a block for a given tree
1207 * returns the tree buffer or NULL.
1209 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1210 struct btrfs_root *root,
1211 u32 blocksize, u64 hint,
1214 struct btrfs_key ins;
1216 struct extent_buffer *buf;
1218 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
1219 blocksize, empty_size, hint,
1223 return ERR_PTR(ret);
1225 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
1227 btrfs_free_extent(trans, root, ins.objectid, blocksize, 0);
1228 return ERR_PTR(-ENOMEM);
1230 btrfs_set_buffer_uptodate(buf);
1231 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1232 buf->start + buf->len - 1, GFP_NOFS);
1233 set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,
1234 buf->start, buf->start + buf->len - 1,
1235 EXTENT_CSUM, GFP_NOFS);
1236 buf->flags |= EXTENT_CSUM;
1237 btrfs_set_buffer_defrag(buf);
1238 trans->blocks_used++;
1242 static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1243 struct btrfs_root *root, struct extent_buffer *leaf)
1245 struct btrfs_key key;
1246 struct btrfs_file_extent_item *fi;
1251 BUG_ON(!btrfs_is_leaf(leaf));
1252 nritems = btrfs_header_nritems(leaf);
1253 for (i = 0; i < nritems; i++) {
1256 btrfs_item_key_to_cpu(leaf, &key, i);
1257 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1259 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1260 if (btrfs_file_extent_type(leaf, fi) ==
1261 BTRFS_FILE_EXTENT_INLINE)
1264 * FIXME make sure to insert a trans record that
1265 * repeats the snapshot del on crash
1267 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1268 if (disk_bytenr == 0)
1270 ret = btrfs_free_extent(trans, root, disk_bytenr,
1271 btrfs_file_extent_disk_num_bytes(leaf, fi), 0);
1277 static void reada_walk_down(struct btrfs_root *root,
1278 struct extent_buffer *node)
1288 nritems = btrfs_header_nritems(node);
1289 level = btrfs_header_level(node);
1290 for (i = 0; i < nritems; i++) {
1291 bytenr = btrfs_node_blockptr(node, i);
1292 blocksize = btrfs_level_size(root, level - 1);
1293 ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
1297 mutex_unlock(&root->fs_info->fs_mutex);
1298 ret = readahead_tree_block(root, bytenr, blocksize);
1300 mutex_lock(&root->fs_info->fs_mutex);
1307 * helper function for drop_snapshot, this walks down the tree dropping ref
1308 * counts as it goes.
1310 static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1311 *root, struct btrfs_path *path, int *level)
1313 struct extent_buffer *next;
1314 struct extent_buffer *cur;
1320 WARN_ON(*level < 0);
1321 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1322 ret = lookup_extent_ref(trans, root,
1323 path->nodes[*level]->start,
1324 path->nodes[*level]->len, &refs);
1330 * walk down to the last node level and free all the leaves
1332 while(*level >= 0) {
1333 WARN_ON(*level < 0);
1334 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1335 cur = path->nodes[*level];
1337 if (*level > 0 && path->slots[*level] == 0)
1338 reada_walk_down(root, cur);
1340 if (btrfs_header_level(cur) != *level)
1343 if (path->slots[*level] >=
1344 btrfs_header_nritems(cur))
1347 ret = drop_leaf_ref(trans, root, cur);
1351 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1352 blocksize = btrfs_level_size(root, *level - 1);
1353 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
1356 path->slots[*level]++;
1357 ret = btrfs_free_extent(trans, root, bytenr,
1362 next = btrfs_find_tree_block(root, bytenr, blocksize);
1363 if (!next || !btrfs_buffer_uptodate(next)) {
1364 free_extent_buffer(next);
1365 mutex_unlock(&root->fs_info->fs_mutex);
1366 next = read_tree_block(root, bytenr, blocksize);
1367 mutex_lock(&root->fs_info->fs_mutex);
1369 /* we dropped the lock, check one more time */
1370 ret = lookup_extent_ref(trans, root, bytenr,
1374 path->slots[*level]++;
1375 free_extent_buffer(next);
1376 ret = btrfs_free_extent(trans, root,
1377 bytenr, blocksize, 1);
1382 WARN_ON(*level <= 0);
1383 if (path->nodes[*level-1])
1384 free_extent_buffer(path->nodes[*level-1]);
1385 path->nodes[*level-1] = next;
1386 *level = btrfs_header_level(next);
1387 path->slots[*level] = 0;
1390 WARN_ON(*level < 0);
1391 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1392 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
1393 path->nodes[*level]->len, 1);
1394 free_extent_buffer(path->nodes[*level]);
1395 path->nodes[*level] = NULL;
1402 * helper for dropping snapshots. This walks back up the tree in the path
1403 * to find the first node higher up where we haven't yet gone through
1406 static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1407 *root, struct btrfs_path *path, int *level)
1412 struct btrfs_root_item *root_item = &root->root_item;
1414 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1415 slot = path->slots[i];
1416 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1417 struct extent_buffer *node;
1418 struct btrfs_disk_key disk_key;
1419 node = path->nodes[i];
1422 WARN_ON(*level == 0);
1423 btrfs_node_key(node, &disk_key, path->slots[i]);
1424 memcpy(&root_item->drop_progress,
1425 &disk_key, sizeof(disk_key));
1426 root_item->drop_level = i;
1429 ret = btrfs_free_extent(trans, root,
1430 path->nodes[*level]->start,
1431 path->nodes[*level]->len, 1);
1433 free_extent_buffer(path->nodes[*level]);
1434 path->nodes[*level] = NULL;
1442 * drop the reference count on the tree rooted at 'snap'. This traverses
1443 * the tree freeing any blocks that have a ref count of zero after being
1446 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
1452 struct btrfs_path *path;
1455 struct btrfs_root_item *root_item = &root->root_item;
1457 path = btrfs_alloc_path();
1460 level = btrfs_header_level(root->node);
1462 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1463 path->nodes[level] = root->node;
1464 extent_buffer_get(root->node);
1465 path->slots[level] = 0;
1467 struct btrfs_key key;
1468 struct btrfs_disk_key found_key;
1469 struct extent_buffer *node;
1471 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1472 level = root_item->drop_level;
1473 path->lowest_level = level;
1474 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1479 node = path->nodes[level];
1480 btrfs_node_key(node, &found_key, path->slots[level]);
1481 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1482 sizeof(found_key)));
1485 wret = walk_down_tree(trans, root, path, &level);
1491 wret = walk_up_tree(trans, root, path, &level);
1499 for (i = 0; i <= orig_level; i++) {
1500 if (path->nodes[i]) {
1501 free_extent_buffer(path->nodes[i]);
1502 path->nodes[i] = NULL;
1506 btrfs_free_path(path);
1510 int btrfs_free_block_groups(struct btrfs_fs_info *info)
1517 ret = find_first_extent_bit(&info->block_group_cache, 0,
1518 &start, &end, (unsigned int)-1);
1521 ret = get_state_private(&info->block_group_cache, start, &ptr);
1523 kfree((void *)(unsigned long)ptr);
1524 clear_extent_bits(&info->block_group_cache, start,
1525 end, (unsigned int)-1, GFP_NOFS);
1528 ret = find_first_extent_bit(&info->free_space_cache, 0,
1529 &start, &end, EXTENT_DIRTY);
1532 clear_extent_dirty(&info->free_space_cache, start,
1538 int btrfs_read_block_groups(struct btrfs_root *root)
1540 struct btrfs_path *path;
1544 struct btrfs_block_group_cache *cache;
1545 struct btrfs_fs_info *info = root->fs_info;
1546 struct extent_map_tree *block_group_cache;
1547 struct btrfs_key key;
1548 struct btrfs_key found_key;
1549 struct extent_buffer *leaf;
1551 block_group_cache = &info->block_group_cache;
1553 root = info->extent_root;
1555 key.offset = BTRFS_BLOCK_GROUP_SIZE;
1556 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1558 path = btrfs_alloc_path();
1563 ret = btrfs_search_slot(NULL, info->extent_root,
1569 leaf = path->nodes[0];
1570 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1571 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1577 read_extent_buffer(leaf, &cache->item,
1578 btrfs_item_ptr_offset(leaf, path->slots[0]),
1579 sizeof(cache->item));
1580 memcpy(&cache->key, &found_key, sizeof(found_key));
1583 key.objectid = found_key.objectid + found_key.offset;
1584 btrfs_release_path(root, path);
1586 if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) {
1587 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
1588 cache->data = BTRFS_BLOCK_GROUP_MIXED;
1589 } else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
1590 bit = BLOCK_GROUP_DATA;
1591 cache->data = BTRFS_BLOCK_GROUP_DATA;
1593 bit = BLOCK_GROUP_METADATA;
1597 /* use EXTENT_LOCKED to prevent merging */
1598 set_extent_bits(block_group_cache, found_key.objectid,
1599 found_key.objectid + found_key.offset - 1,
1600 bit | EXTENT_LOCKED, GFP_NOFS);
1601 set_state_private(block_group_cache, found_key.objectid,
1602 (unsigned long)cache);
1605 btrfs_super_total_bytes(&info->super_copy))
1609 btrfs_free_path(path);