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;
51 root = root->fs_info->extent_root;
52 free_space_cache = &root->fs_info->free_space_cache;
54 if (block_group->cached)
57 path = btrfs_alloc_path();
62 first_free = block_group->key.objectid;
63 key.objectid = block_group->key.objectid;
66 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
67 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
72 if (ret && path->slots[0] > 0)
76 leaf = path->nodes[0];
77 slot = path->slots[0];
78 if (slot >= btrfs_header_nritems(leaf)) {
79 ret = btrfs_next_leaf(root, path);
89 btrfs_item_key_to_cpu(leaf, &key, slot);
90 if (key.objectid < block_group->key.objectid) {
91 if (key.objectid + key.offset > first_free)
92 first_free = key.objectid + key.offset;
96 if (key.objectid >= block_group->key.objectid +
97 block_group->key.offset) {
101 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
106 if (key.objectid > last) {
107 hole_size = key.objectid - last;
108 set_extent_dirty(free_space_cache, last,
109 last + hole_size - 1,
112 last = key.objectid + key.offset;
120 if (block_group->key.objectid +
121 block_group->key.offset > last) {
122 hole_size = block_group->key.objectid +
123 block_group->key.offset - last;
124 set_extent_dirty(free_space_cache, last,
125 last + hole_size - 1, GFP_NOFS);
127 block_group->cached = 1;
129 btrfs_free_path(path);
133 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
137 struct extent_map_tree *block_group_cache;
138 struct btrfs_block_group_cache *block_group = NULL;
144 block_group_cache = &info->block_group_cache;
145 ret = find_first_extent_bit(block_group_cache,
146 bytenr, &start, &end,
147 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA);
151 ret = get_state_private(block_group_cache, start, &ptr);
155 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
156 if (block_group->key.objectid <= bytenr && bytenr <
157 block_group->key.objectid + block_group->key.offset)
161 static u64 find_search_start(struct btrfs_root *root,
162 struct btrfs_block_group_cache **cache_ret,
163 u64 search_start, int num,
164 int data, int full_scan)
167 struct btrfs_block_group_cache *cache = *cache_ret;
175 cache = btrfs_lookup_block_group(root->fs_info, search_start);
180 ret = cache_block_group(root, cache);
184 last = max(search_start, cache->key.objectid);
187 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
188 last, &start, &end, EXTENT_DIRTY);
195 start = max(last, start);
197 if (last - start < num) {
198 if (last == cache->key.objectid + cache->key.offset)
202 if (data != BTRFS_BLOCK_GROUP_MIXED &&
203 start + num > cache->key.objectid + cache->key.offset)
211 last = cache->key.objectid + cache->key.offset;
213 cache = btrfs_lookup_block_group(root->fs_info, last);
219 data = BTRFS_BLOCK_GROUP_MIXED;
224 if (cache_miss && !cache->cached) {
225 cache_block_group(root, cache);
227 cache = btrfs_lookup_block_group(root->fs_info, last);
230 cache = btrfs_find_block_group(root, cache, last, data, 0);
238 static u64 div_factor(u64 num, int factor)
247 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
248 struct btrfs_block_group_cache
249 *hint, u64 search_start,
252 struct btrfs_block_group_cache *cache;
253 struct extent_map_tree *block_group_cache;
254 struct btrfs_block_group_cache *found_group = NULL;
255 struct btrfs_fs_info *info = root->fs_info;
269 block_group_cache = &info->block_group_cache;
274 if (data == BTRFS_BLOCK_GROUP_MIXED) {
275 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
278 bit = BLOCK_GROUP_DATA;
280 bit = BLOCK_GROUP_METADATA;
283 struct btrfs_block_group_cache *shint;
284 shint = btrfs_lookup_block_group(info, search_start);
285 if (shint && (shint->data == data ||
286 shint->data == BTRFS_BLOCK_GROUP_MIXED)) {
287 used = btrfs_block_group_used(&shint->item);
288 if (used + shint->pinned <
289 div_factor(shint->key.offset, factor)) {
294 if (hint && (hint->data == data ||
295 hint->data == BTRFS_BLOCK_GROUP_MIXED)) {
296 used = btrfs_block_group_used(&hint->item);
297 if (used + hint->pinned <
298 div_factor(hint->key.offset, factor)) {
301 last = hint->key.objectid + hint->key.offset;
305 hint_last = max(hint->key.objectid, search_start);
307 hint_last = search_start;
313 ret = find_first_extent_bit(block_group_cache, last,
318 ret = get_state_private(block_group_cache, start, &ptr);
322 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
323 last = cache->key.objectid + cache->key.offset;
324 used = btrfs_block_group_used(&cache->item);
327 free_check = cache->key.offset;
329 free_check = div_factor(cache->key.offset, factor);
330 if (used + cache->pinned < free_check) {
343 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
351 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
352 struct btrfs_root *root,
353 u64 bytenr, u64 num_bytes)
355 struct btrfs_path *path;
357 struct btrfs_key key;
358 struct extent_buffer *l;
359 struct btrfs_extent_item *item;
362 WARN_ON(num_bytes < root->sectorsize);
363 path = btrfs_alloc_path();
367 key.objectid = bytenr;
368 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
369 key.offset = num_bytes;
370 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
379 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
380 refs = btrfs_extent_refs(l, item);
381 btrfs_set_extent_refs(l, item, refs + 1);
382 btrfs_mark_buffer_dirty(path->nodes[0]);
384 btrfs_release_path(root->fs_info->extent_root, path);
385 btrfs_free_path(path);
386 finish_current_insert(trans, root->fs_info->extent_root);
387 del_pending_extents(trans, root->fs_info->extent_root);
391 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
392 struct btrfs_root *root)
394 finish_current_insert(trans, root->fs_info->extent_root);
395 del_pending_extents(trans, root->fs_info->extent_root);
399 static int lookup_extent_ref(struct btrfs_trans_handle *trans,
400 struct btrfs_root *root, u64 bytenr,
401 u64 num_bytes, u32 *refs)
403 struct btrfs_path *path;
405 struct btrfs_key key;
406 struct extent_buffer *l;
407 struct btrfs_extent_item *item;
409 WARN_ON(num_bytes < root->sectorsize);
410 path = btrfs_alloc_path();
411 key.objectid = bytenr;
412 key.offset = num_bytes;
413 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
414 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
419 btrfs_print_leaf(root, path->nodes[0]);
420 printk("failed to find block number %Lu\n", bytenr);
424 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
425 *refs = btrfs_extent_refs(l, item);
427 btrfs_free_path(path);
431 int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
432 struct btrfs_root *root)
434 return btrfs_inc_extent_ref(trans, root, root->node->start,
438 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
439 struct extent_buffer *buf)
443 struct btrfs_key key;
444 struct btrfs_file_extent_item *fi;
454 level = btrfs_header_level(buf);
455 nritems = btrfs_header_nritems(buf);
456 for (i = 0; i < nritems; i++) {
459 btrfs_item_key_to_cpu(buf, &key, i);
460 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
462 fi = btrfs_item_ptr(buf, i,
463 struct btrfs_file_extent_item);
464 if (btrfs_file_extent_type(buf, fi) ==
465 BTRFS_FILE_EXTENT_INLINE)
467 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
468 if (disk_bytenr == 0)
470 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
471 btrfs_file_extent_disk_num_bytes(buf, fi));
477 bytenr = btrfs_node_blockptr(buf, i);
478 ret = btrfs_inc_extent_ref(trans, root, bytenr,
479 btrfs_level_size(root, level - 1));
489 for (i =0; i < faili; i++) {
492 btrfs_item_key_to_cpu(buf, &key, i);
493 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
495 fi = btrfs_item_ptr(buf, i,
496 struct btrfs_file_extent_item);
497 if (btrfs_file_extent_type(buf, fi) ==
498 BTRFS_FILE_EXTENT_INLINE)
500 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
501 if (disk_bytenr == 0)
503 err = btrfs_free_extent(trans, root, disk_bytenr,
504 btrfs_file_extent_disk_num_bytes(buf,
508 bytenr = btrfs_node_blockptr(buf, i);
509 err = btrfs_free_extent(trans, root, bytenr,
510 btrfs_level_size(root, level - 1), 0);
517 static int write_one_cache_group(struct btrfs_trans_handle *trans,
518 struct btrfs_root *root,
519 struct btrfs_path *path,
520 struct btrfs_block_group_cache *cache)
524 struct btrfs_root *extent_root = root->fs_info->extent_root;
526 struct extent_buffer *leaf;
528 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
533 leaf = path->nodes[0];
534 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
535 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
536 btrfs_mark_buffer_dirty(leaf);
537 btrfs_release_path(extent_root, path);
539 finish_current_insert(trans, extent_root);
540 pending_ret = del_pending_extents(trans, extent_root);
549 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
550 struct btrfs_root *root)
552 struct extent_map_tree *block_group_cache;
553 struct btrfs_block_group_cache *cache;
557 struct btrfs_path *path;
563 block_group_cache = &root->fs_info->block_group_cache;
564 path = btrfs_alloc_path();
569 ret = find_first_extent_bit(block_group_cache, last,
570 &start, &end, BLOCK_GROUP_DIRTY);
575 ret = get_state_private(block_group_cache, start, &ptr);
579 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
580 err = write_one_cache_group(trans, root,
583 * if we fail to write the cache group, we want
584 * to keep it marked dirty in hopes that a later
591 clear_extent_bits(block_group_cache, start, end,
592 BLOCK_GROUP_DIRTY, GFP_NOFS);
594 btrfs_free_path(path);
598 static int update_block_group(struct btrfs_trans_handle *trans,
599 struct btrfs_root *root,
600 u64 bytenr, u64 num_bytes, int alloc,
601 int mark_free, int data)
603 struct btrfs_block_group_cache *cache;
604 struct btrfs_fs_info *info = root->fs_info;
605 u64 total = num_bytes;
612 cache = btrfs_lookup_block_group(info, bytenr);
616 byte_in_group = bytenr - cache->key.objectid;
617 WARN_ON(byte_in_group > cache->key.offset);
618 start = cache->key.objectid;
619 end = start + cache->key.offset - 1;
620 set_extent_bits(&info->block_group_cache, start, end,
621 BLOCK_GROUP_DIRTY, GFP_NOFS);
623 old_val = btrfs_block_group_used(&cache->item);
624 num_bytes = min(total, cache->key.offset - byte_in_group);
626 if (cache->data != data &&
627 old_val < (cache->key.offset >> 1)) {
632 bit_to_clear = BLOCK_GROUP_METADATA;
633 bit_to_set = BLOCK_GROUP_DATA;
635 ~BTRFS_BLOCK_GROUP_MIXED;
637 BTRFS_BLOCK_GROUP_DATA;
639 bit_to_clear = BLOCK_GROUP_DATA;
640 bit_to_set = BLOCK_GROUP_METADATA;
642 ~BTRFS_BLOCK_GROUP_MIXED;
644 ~BTRFS_BLOCK_GROUP_DATA;
646 clear_extent_bits(&info->block_group_cache,
647 start, end, bit_to_clear,
649 set_extent_bits(&info->block_group_cache,
650 start, end, bit_to_set,
652 } else if (cache->data != data &&
653 cache->data != BTRFS_BLOCK_GROUP_MIXED) {
654 cache->data = BTRFS_BLOCK_GROUP_MIXED;
655 set_extent_bits(&info->block_group_cache,
658 BLOCK_GROUP_METADATA,
661 old_val += num_bytes;
663 old_val -= num_bytes;
665 set_extent_dirty(&info->free_space_cache,
666 bytenr, bytenr + num_bytes - 1,
670 btrfs_set_block_group_used(&cache->item, old_val);
676 static int update_pinned_extents(struct btrfs_root *root,
677 u64 bytenr, u64 num, int pin)
680 struct btrfs_block_group_cache *cache;
681 struct btrfs_fs_info *fs_info = root->fs_info;
684 set_extent_dirty(&fs_info->pinned_extents,
685 bytenr, bytenr + num - 1, GFP_NOFS);
687 clear_extent_dirty(&fs_info->pinned_extents,
688 bytenr, bytenr + num - 1, GFP_NOFS);
691 cache = btrfs_lookup_block_group(fs_info, bytenr);
693 len = min(num, cache->key.offset -
694 (bytenr - cache->key.objectid));
696 cache->pinned += len;
697 fs_info->total_pinned += len;
699 cache->pinned -= len;
700 fs_info->total_pinned -= len;
708 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
713 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
717 ret = find_first_extent_bit(pinned_extents, last,
718 &start, &end, EXTENT_DIRTY);
721 set_extent_dirty(copy, start, end, GFP_NOFS);
727 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
728 struct btrfs_root *root,
729 struct extent_map_tree *unpin)
734 struct extent_map_tree *free_space_cache;
735 free_space_cache = &root->fs_info->free_space_cache;
738 ret = find_first_extent_bit(unpin, 0, &start, &end,
742 update_pinned_extents(root, start, end + 1 - start, 0);
743 clear_extent_dirty(unpin, start, end, GFP_NOFS);
744 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
749 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
750 btrfs_root *extent_root)
752 struct btrfs_key ins;
753 struct btrfs_extent_item extent_item;
758 struct btrfs_fs_info *info = extent_root->fs_info;
760 btrfs_set_stack_extent_refs(&extent_item, 1);
761 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
762 btrfs_set_stack_extent_owner(&extent_item,
763 extent_root->root_key.objectid);
766 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
767 &end, EXTENT_LOCKED);
771 ins.objectid = start;
772 ins.offset = end + 1 - start;
773 err = btrfs_insert_item(trans, extent_root, &ins,
774 &extent_item, sizeof(extent_item));
775 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
781 static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
785 struct extent_buffer *buf;
788 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
790 if (btrfs_buffer_uptodate(buf)) {
792 root->fs_info->running_transaction->transid;
793 if (btrfs_header_generation(buf) == transid) {
794 free_extent_buffer(buf);
798 free_extent_buffer(buf);
800 update_pinned_extents(root, bytenr, num_bytes, 1);
802 set_extent_bits(&root->fs_info->pending_del,
803 bytenr, bytenr + num_bytes - 1,
804 EXTENT_LOCKED, GFP_NOFS);
811 * remove an extent from the root, returns 0 on success
813 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
814 *root, u64 bytenr, u64 num_bytes, int pin,
817 struct btrfs_path *path;
818 struct btrfs_key key;
819 struct btrfs_fs_info *info = root->fs_info;
820 struct btrfs_root *extent_root = info->extent_root;
821 struct extent_buffer *leaf;
823 struct btrfs_extent_item *ei;
826 key.objectid = bytenr;
827 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
828 key.offset = num_bytes;
830 path = btrfs_alloc_path();
834 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
839 leaf = path->nodes[0];
840 ei = btrfs_item_ptr(leaf, path->slots[0],
841 struct btrfs_extent_item);
842 refs = btrfs_extent_refs(leaf, ei);
845 btrfs_set_extent_refs(leaf, ei, refs);
846 btrfs_mark_buffer_dirty(leaf);
853 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
859 /* block accounting for super block */
860 super_used = btrfs_super_bytes_used(&info->super_copy);
861 btrfs_set_super_bytes_used(&info->super_copy,
862 super_used - num_bytes);
864 /* block accounting for root item */
865 root_used = btrfs_root_used(&root->root_item);
866 btrfs_set_root_used(&root->root_item,
867 root_used - num_bytes);
869 ret = btrfs_del_item(trans, extent_root, path);
873 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
877 btrfs_free_path(path);
878 finish_current_insert(trans, extent_root);
883 * find all the blocks marked as pending in the radix tree and remove
884 * them from the extent map
886 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
887 btrfs_root *extent_root)
893 struct extent_map_tree *pending_del;
894 struct extent_map_tree *pinned_extents;
896 pending_del = &extent_root->fs_info->pending_del;
897 pinned_extents = &extent_root->fs_info->pinned_extents;
900 ret = find_first_extent_bit(pending_del, 0, &start, &end,
904 update_pinned_extents(extent_root, start, end + 1 - start, 1);
905 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
907 ret = __free_extent(trans, extent_root,
908 start, end + 1 - start, 0, 0);
916 * remove an extent from the root, returns 0 on success
918 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
919 *root, u64 bytenr, u64 num_bytes, int pin)
921 struct btrfs_root *extent_root = root->fs_info->extent_root;
925 WARN_ON(num_bytes < root->sectorsize);
926 if (root == extent_root) {
927 pin_down_bytes(root, bytenr, num_bytes, 1);
930 ret = __free_extent(trans, root, bytenr, num_bytes, pin, pin == 0);
931 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
932 return ret ? ret : pending_ret;
936 * walks the btree of allocated extents and find a hole of a given size.
937 * The key ins is changed to record the hole:
938 * ins->objectid == block start
939 * ins->flags = BTRFS_EXTENT_ITEM_KEY
940 * ins->offset == number of blocks
941 * Any available blocks before search_start are skipped.
943 static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
944 *orig_root, u64 num_bytes, u64 empty_size,
945 u64 search_start, u64 search_end, u64 hint_byte,
946 struct btrfs_key *ins, u64 exclude_start,
947 u64 exclude_nr, int data)
949 struct btrfs_path *path;
950 struct btrfs_key key;
955 u64 orig_search_start = search_start;
957 struct extent_buffer *l;
958 struct btrfs_root * root = orig_root->fs_info->extent_root;
959 struct btrfs_fs_info *info = root->fs_info;
960 u64 total_needed = num_bytes;
962 struct btrfs_block_group_cache *block_group;
967 WARN_ON(num_bytes < root->sectorsize);
968 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
970 level = btrfs_header_level(root->node);
972 if (num_bytes >= 32 * 1024 * 1024 && hint_byte) {
973 data = BTRFS_BLOCK_GROUP_MIXED;
976 if (search_end == (u64)-1)
977 search_end = btrfs_super_total_bytes(&info->super_copy);
979 block_group = btrfs_lookup_block_group(info, hint_byte);
980 block_group = btrfs_find_block_group(root, block_group,
983 block_group = btrfs_find_block_group(root,
984 trans->block_group, 0,
988 total_needed += empty_size;
989 path = btrfs_alloc_path();
991 search_start = find_search_start(root, &block_group, search_start,
992 total_needed, data, full_scan);
993 cached_start = search_start;
994 btrfs_init_path(path);
995 ins->objectid = search_start;
1000 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1004 if (path->slots[0] > 0) {
1009 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1012 * a rare case, go back one key if we hit a block group item
1013 * instead of an extent item
1015 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
1016 key.objectid + key.offset >= search_start) {
1017 ins->objectid = key.objectid;
1018 ins->offset = key.offset - 1;
1019 btrfs_release_path(root, path);
1020 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1024 if (path->slots[0] > 0) {
1031 slot = path->slots[0];
1032 if (slot >= btrfs_header_nritems(l)) {
1033 ret = btrfs_next_leaf(root, path);
1039 search_start = max(search_start,
1040 block_group->key.objectid);
1042 ins->objectid = search_start;
1043 ins->offset = search_end - search_start;
1047 ins->objectid = last_byte > search_start ?
1048 last_byte : search_start;
1049 ins->offset = search_end - ins->objectid;
1050 BUG_ON(ins->objectid >= search_end);
1053 btrfs_item_key_to_cpu(l, &key, slot);
1055 if (key.objectid >= search_start && key.objectid > last_byte &&
1057 if (last_byte < search_start)
1058 last_byte = search_start;
1059 hole_size = key.objectid - last_byte;
1060 if (hole_size >= num_bytes) {
1061 ins->objectid = last_byte;
1062 ins->offset = hole_size;
1066 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
1068 last_byte = key.objectid;
1076 last_byte = key.objectid + key.offset;
1078 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
1079 last_byte >= block_group->key.objectid +
1080 block_group->key.offset) {
1081 btrfs_release_path(root, path);
1082 search_start = block_group->key.objectid +
1083 block_group->key.offset;
1091 /* we have to make sure we didn't find an extent that has already
1092 * been allocated by the map tree or the original allocation
1094 btrfs_release_path(root, path);
1095 BUG_ON(ins->objectid < search_start);
1097 if (ins->objectid + num_bytes >= search_end)
1099 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
1100 ins->objectid + num_bytes > block_group->
1101 key.objectid + block_group->key.offset) {
1102 search_start = block_group->key.objectid +
1103 block_group->key.offset;
1106 if (test_range_bit(&info->extent_ins, ins->objectid,
1107 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1108 search_start = ins->objectid + num_bytes;
1111 if (test_range_bit(&info->pinned_extents, ins->objectid,
1112 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1113 search_start = ins->objectid + num_bytes;
1116 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
1117 ins->objectid < exclude_start + exclude_nr)) {
1118 search_start = exclude_start + exclude_nr;
1122 block_group = btrfs_lookup_block_group(info, ins->objectid);
1124 trans->block_group = block_group;
1126 ins->offset = num_bytes;
1127 btrfs_free_path(path);
1131 if (search_start + num_bytes >= search_end) {
1133 search_start = orig_search_start;
1140 total_needed -= empty_size;
1145 block_group = btrfs_lookup_block_group(info, search_start);
1148 block_group = btrfs_find_block_group(root, block_group,
1149 search_start, data, 0);
1153 btrfs_release_path(root, path);
1154 btrfs_free_path(path);
1158 * finds a free extent and does all the dirty work required for allocation
1159 * returns the key for the extent through ins, and a tree buffer for
1160 * the first block of the extent through buf.
1162 * returns 0 if everything worked, non-zero otherwise.
1164 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1165 struct btrfs_root *root, u64 owner,
1166 u64 num_bytes, u64 empty_size, u64 hint_byte,
1167 u64 search_end, struct btrfs_key *ins, int data)
1171 u64 super_used, root_used;
1172 u64 search_start = 0;
1173 struct btrfs_fs_info *info = root->fs_info;
1174 struct btrfs_root *extent_root = info->extent_root;
1175 struct btrfs_extent_item extent_item;
1177 btrfs_set_stack_extent_refs(&extent_item, 1);
1178 btrfs_set_stack_extent_owner(&extent_item, owner);
1180 WARN_ON(num_bytes < root->sectorsize);
1181 ret = find_free_extent(trans, root, num_bytes, empty_size,
1182 search_start, search_end, hint_byte, ins,
1183 trans->alloc_exclude_start,
1184 trans->alloc_exclude_nr, data);
1189 /* block accounting for super block */
1190 super_used = btrfs_super_bytes_used(&info->super_copy);
1191 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
1193 /* block accounting for root item */
1194 root_used = btrfs_root_used(&root->root_item);
1195 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
1197 clear_extent_dirty(&root->fs_info->free_space_cache,
1198 ins->objectid, ins->objectid + ins->offset - 1,
1201 if (root == extent_root) {
1202 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1203 ins->objectid + ins->offset - 1,
1204 EXTENT_LOCKED, GFP_NOFS);
1209 WARN_ON(trans->alloc_exclude_nr);
1210 trans->alloc_exclude_start = ins->objectid;
1211 trans->alloc_exclude_nr = ins->offset;
1212 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1213 sizeof(extent_item));
1215 trans->alloc_exclude_start = 0;
1216 trans->alloc_exclude_nr = 0;
1219 finish_current_insert(trans, extent_root);
1220 pending_ret = del_pending_extents(trans, extent_root);
1230 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1237 * helper function to allocate a block for a given tree
1238 * returns the tree buffer or NULL.
1240 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1241 struct btrfs_root *root,
1242 u32 blocksize, u64 hint,
1245 struct btrfs_key ins;
1247 struct extent_buffer *buf;
1249 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
1250 blocksize, empty_size, hint,
1254 return ERR_PTR(ret);
1256 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
1258 btrfs_free_extent(trans, root, ins.objectid, blocksize, 0);
1259 return ERR_PTR(-ENOMEM);
1261 btrfs_set_buffer_uptodate(buf);
1262 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1263 buf->start + buf->len - 1, GFP_NOFS);
1264 set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,
1265 buf->start, buf->start + buf->len - 1,
1266 EXTENT_CSUM, GFP_NOFS);
1267 buf->flags |= EXTENT_CSUM;
1268 btrfs_set_buffer_defrag(buf);
1269 trans->blocks_used++;
1273 static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1274 struct btrfs_root *root, struct extent_buffer *leaf)
1276 struct btrfs_key key;
1277 struct btrfs_file_extent_item *fi;
1282 BUG_ON(!btrfs_is_leaf(leaf));
1283 nritems = btrfs_header_nritems(leaf);
1284 for (i = 0; i < nritems; i++) {
1287 btrfs_item_key_to_cpu(leaf, &key, i);
1288 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1290 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1291 if (btrfs_file_extent_type(leaf, fi) ==
1292 BTRFS_FILE_EXTENT_INLINE)
1295 * FIXME make sure to insert a trans record that
1296 * repeats the snapshot del on crash
1298 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1299 if (disk_bytenr == 0)
1301 ret = btrfs_free_extent(trans, root, disk_bytenr,
1302 btrfs_file_extent_disk_num_bytes(leaf, fi), 0);
1308 static void reada_walk_down(struct btrfs_root *root,
1309 struct extent_buffer *node)
1319 nritems = btrfs_header_nritems(node);
1320 level = btrfs_header_level(node);
1321 for (i = 0; i < nritems; i++) {
1322 bytenr = btrfs_node_blockptr(node, i);
1323 blocksize = btrfs_level_size(root, level - 1);
1324 ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
1328 mutex_unlock(&root->fs_info->fs_mutex);
1329 ret = readahead_tree_block(root, bytenr, blocksize);
1331 mutex_lock(&root->fs_info->fs_mutex);
1338 * helper function for drop_snapshot, this walks down the tree dropping ref
1339 * counts as it goes.
1341 static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1342 *root, struct btrfs_path *path, int *level)
1344 struct extent_buffer *next;
1345 struct extent_buffer *cur;
1351 WARN_ON(*level < 0);
1352 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1353 ret = lookup_extent_ref(trans, root,
1354 path->nodes[*level]->start,
1355 path->nodes[*level]->len, &refs);
1361 * walk down to the last node level and free all the leaves
1363 while(*level >= 0) {
1364 WARN_ON(*level < 0);
1365 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1366 cur = path->nodes[*level];
1368 if (*level > 0 && path->slots[*level] == 0)
1369 reada_walk_down(root, cur);
1371 if (btrfs_header_level(cur) != *level)
1374 if (path->slots[*level] >=
1375 btrfs_header_nritems(cur))
1378 ret = drop_leaf_ref(trans, root, cur);
1382 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1383 blocksize = btrfs_level_size(root, *level - 1);
1384 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
1387 path->slots[*level]++;
1388 ret = btrfs_free_extent(trans, root, bytenr,
1393 next = btrfs_find_tree_block(root, bytenr, blocksize);
1394 if (!next || !btrfs_buffer_uptodate(next)) {
1395 free_extent_buffer(next);
1396 mutex_unlock(&root->fs_info->fs_mutex);
1397 next = read_tree_block(root, bytenr, blocksize);
1398 mutex_lock(&root->fs_info->fs_mutex);
1400 /* we dropped the lock, check one more time */
1401 ret = lookup_extent_ref(trans, root, bytenr,
1405 path->slots[*level]++;
1406 free_extent_buffer(next);
1407 ret = btrfs_free_extent(trans, root,
1408 bytenr, blocksize, 1);
1413 WARN_ON(*level <= 0);
1414 if (path->nodes[*level-1])
1415 free_extent_buffer(path->nodes[*level-1]);
1416 path->nodes[*level-1] = next;
1417 *level = btrfs_header_level(next);
1418 path->slots[*level] = 0;
1421 WARN_ON(*level < 0);
1422 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1423 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
1424 path->nodes[*level]->len, 1);
1425 free_extent_buffer(path->nodes[*level]);
1426 path->nodes[*level] = NULL;
1433 * helper for dropping snapshots. This walks back up the tree in the path
1434 * to find the first node higher up where we haven't yet gone through
1437 static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1438 *root, struct btrfs_path *path, int *level)
1443 struct btrfs_root_item *root_item = &root->root_item;
1445 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1446 slot = path->slots[i];
1447 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1448 struct extent_buffer *node;
1449 struct btrfs_disk_key disk_key;
1450 node = path->nodes[i];
1453 WARN_ON(*level == 0);
1454 btrfs_node_key(node, &disk_key, path->slots[i]);
1455 memcpy(&root_item->drop_progress,
1456 &disk_key, sizeof(disk_key));
1457 root_item->drop_level = i;
1460 ret = btrfs_free_extent(trans, root,
1461 path->nodes[*level]->start,
1462 path->nodes[*level]->len, 1);
1464 free_extent_buffer(path->nodes[*level]);
1465 path->nodes[*level] = NULL;
1473 * drop the reference count on the tree rooted at 'snap'. This traverses
1474 * the tree freeing any blocks that have a ref count of zero after being
1477 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
1483 struct btrfs_path *path;
1486 struct btrfs_root_item *root_item = &root->root_item;
1488 path = btrfs_alloc_path();
1491 level = btrfs_header_level(root->node);
1493 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1494 path->nodes[level] = root->node;
1495 extent_buffer_get(root->node);
1496 path->slots[level] = 0;
1498 struct btrfs_key key;
1499 struct btrfs_disk_key found_key;
1500 struct extent_buffer *node;
1502 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1503 level = root_item->drop_level;
1504 path->lowest_level = level;
1505 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1510 node = path->nodes[level];
1511 btrfs_node_key(node, &found_key, path->slots[level]);
1512 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1513 sizeof(found_key)));
1516 wret = walk_down_tree(trans, root, path, &level);
1522 wret = walk_up_tree(trans, root, path, &level);
1530 for (i = 0; i <= orig_level; i++) {
1531 if (path->nodes[i]) {
1532 free_extent_buffer(path->nodes[i]);
1533 path->nodes[i] = NULL;
1537 btrfs_free_path(path);
1541 int btrfs_free_block_groups(struct btrfs_fs_info *info)
1548 ret = find_first_extent_bit(&info->block_group_cache, 0,
1549 &start, &end, (unsigned int)-1);
1552 ret = get_state_private(&info->block_group_cache, start, &ptr);
1554 kfree((void *)(unsigned long)ptr);
1555 clear_extent_bits(&info->block_group_cache, start,
1556 end, (unsigned int)-1, GFP_NOFS);
1559 ret = find_first_extent_bit(&info->free_space_cache, 0,
1560 &start, &end, EXTENT_DIRTY);
1563 clear_extent_dirty(&info->free_space_cache, start,
1569 int btrfs_read_block_groups(struct btrfs_root *root)
1571 struct btrfs_path *path;
1575 struct btrfs_block_group_cache *cache;
1576 struct btrfs_fs_info *info = root->fs_info;
1577 struct extent_map_tree *block_group_cache;
1578 struct btrfs_key key;
1579 struct btrfs_key found_key;
1580 struct extent_buffer *leaf;
1582 block_group_cache = &info->block_group_cache;
1584 root = info->extent_root;
1586 key.offset = BTRFS_BLOCK_GROUP_SIZE;
1587 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1589 path = btrfs_alloc_path();
1594 ret = btrfs_search_slot(NULL, info->extent_root,
1600 leaf = path->nodes[0];
1601 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1602 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1608 read_extent_buffer(leaf, &cache->item,
1609 btrfs_item_ptr_offset(leaf, path->slots[0]),
1610 sizeof(cache->item));
1611 memcpy(&cache->key, &found_key, sizeof(found_key));
1614 key.objectid = found_key.objectid + found_key.offset;
1615 btrfs_release_path(root, path);
1617 if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) {
1618 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
1619 cache->data = BTRFS_BLOCK_GROUP_MIXED;
1620 } else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
1621 bit = BLOCK_GROUP_DATA;
1622 cache->data = BTRFS_BLOCK_GROUP_DATA;
1624 bit = BLOCK_GROUP_METADATA;
1628 /* use EXTENT_LOCKED to prevent merging */
1629 set_extent_bits(block_group_cache, found_key.objectid,
1630 found_key.objectid + found_key.offset - 1,
1631 bit | EXTENT_LOCKED, GFP_NOFS);
1632 set_state_private(block_group_cache, found_key.objectid,
1633 (unsigned long)cache);
1636 btrfs_super_total_bytes(&info->super_copy))
1640 btrfs_free_path(path);