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;
178 ret = cache_block_group(root, cache);
182 last = max(search_start, cache->key.objectid);
185 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
186 last, &start, &end, EXTENT_DIRTY);
193 start = max(last, start);
195 if (last - start < num) {
196 if (last == cache->key.objectid + cache->key.offset)
200 if (data != BTRFS_BLOCK_GROUP_MIXED &&
201 start + num > cache->key.objectid + cache->key.offset)
206 cache = btrfs_lookup_block_group(root->fs_info, search_start);
208 printk("Unable to find block group for %Lu\n",
216 last = cache->key.objectid + cache->key.offset;
218 cache = btrfs_lookup_block_group(root->fs_info, last);
224 data = BTRFS_BLOCK_GROUP_MIXED;
229 if (cache_miss && !cache->cached) {
230 cache_block_group(root, cache);
232 cache = btrfs_lookup_block_group(root->fs_info, last);
234 cache = btrfs_find_block_group(root, cache, last, data, 0);
242 static u64 div_factor(u64 num, int factor)
251 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
252 struct btrfs_block_group_cache
253 *hint, u64 search_start,
256 struct btrfs_block_group_cache *cache;
257 struct extent_map_tree *block_group_cache;
258 struct btrfs_block_group_cache *found_group = NULL;
259 struct btrfs_fs_info *info = root->fs_info;
273 block_group_cache = &info->block_group_cache;
278 if (data == BTRFS_BLOCK_GROUP_MIXED) {
279 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
282 bit = BLOCK_GROUP_DATA;
284 bit = BLOCK_GROUP_METADATA;
287 struct btrfs_block_group_cache *shint;
288 shint = btrfs_lookup_block_group(info, search_start);
289 if (shint && (shint->data == data ||
290 shint->data == BTRFS_BLOCK_GROUP_MIXED)) {
291 used = btrfs_block_group_used(&shint->item);
292 if (used + shint->pinned <
293 div_factor(shint->key.offset, factor)) {
298 if (hint && (hint->data == data ||
299 hint->data == BTRFS_BLOCK_GROUP_MIXED)) {
300 used = btrfs_block_group_used(&hint->item);
301 if (used + hint->pinned <
302 div_factor(hint->key.offset, factor)) {
305 last = hint->key.objectid + hint->key.offset;
309 hint_last = max(hint->key.objectid, search_start);
311 hint_last = search_start;
317 ret = find_first_extent_bit(block_group_cache, last,
322 ret = get_state_private(block_group_cache, start, &ptr);
326 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
327 last = cache->key.objectid + cache->key.offset;
328 used = btrfs_block_group_used(&cache->item);
331 free_check = cache->key.offset;
333 free_check = div_factor(cache->key.offset, factor);
334 if (used + cache->pinned < free_check) {
347 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
355 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
356 struct btrfs_root *root,
357 u64 bytenr, u64 num_bytes)
359 struct btrfs_path *path;
361 struct btrfs_key key;
362 struct extent_buffer *l;
363 struct btrfs_extent_item *item;
366 WARN_ON(num_bytes < root->sectorsize);
367 path = btrfs_alloc_path();
371 key.objectid = bytenr;
372 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
373 key.offset = num_bytes;
374 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
383 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
384 refs = btrfs_extent_refs(l, item);
385 btrfs_set_extent_refs(l, item, refs + 1);
386 btrfs_mark_buffer_dirty(path->nodes[0]);
388 btrfs_release_path(root->fs_info->extent_root, path);
389 btrfs_free_path(path);
390 finish_current_insert(trans, root->fs_info->extent_root);
391 del_pending_extents(trans, root->fs_info->extent_root);
395 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
396 struct btrfs_root *root)
398 finish_current_insert(trans, root->fs_info->extent_root);
399 del_pending_extents(trans, root->fs_info->extent_root);
403 static int lookup_extent_ref(struct btrfs_trans_handle *trans,
404 struct btrfs_root *root, u64 bytenr,
405 u64 num_bytes, u32 *refs)
407 struct btrfs_path *path;
409 struct btrfs_key key;
410 struct extent_buffer *l;
411 struct btrfs_extent_item *item;
413 WARN_ON(num_bytes < root->sectorsize);
414 path = btrfs_alloc_path();
415 key.objectid = bytenr;
416 key.offset = num_bytes;
417 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
418 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
423 btrfs_print_leaf(root, path->nodes[0]);
424 printk("failed to find block number %Lu\n", bytenr);
428 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
429 *refs = btrfs_extent_refs(l, item);
431 btrfs_free_path(path);
435 int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
436 struct btrfs_root *root)
438 return btrfs_inc_extent_ref(trans, root, root->node->start,
442 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
443 struct extent_buffer *buf)
447 struct btrfs_key key;
448 struct btrfs_file_extent_item *fi;
458 level = btrfs_header_level(buf);
459 nritems = btrfs_header_nritems(buf);
460 for (i = 0; i < nritems; i++) {
463 btrfs_item_key_to_cpu(buf, &key, i);
464 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
466 fi = btrfs_item_ptr(buf, i,
467 struct btrfs_file_extent_item);
468 if (btrfs_file_extent_type(buf, fi) ==
469 BTRFS_FILE_EXTENT_INLINE)
471 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
472 if (disk_bytenr == 0)
474 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
475 btrfs_file_extent_disk_num_bytes(buf, fi));
481 bytenr = btrfs_node_blockptr(buf, i);
482 ret = btrfs_inc_extent_ref(trans, root, bytenr,
483 btrfs_level_size(root, level - 1));
493 for (i =0; i < faili; i++) {
496 btrfs_item_key_to_cpu(buf, &key, i);
497 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
499 fi = btrfs_item_ptr(buf, i,
500 struct btrfs_file_extent_item);
501 if (btrfs_file_extent_type(buf, fi) ==
502 BTRFS_FILE_EXTENT_INLINE)
504 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
505 if (disk_bytenr == 0)
507 err = btrfs_free_extent(trans, root, disk_bytenr,
508 btrfs_file_extent_disk_num_bytes(buf,
512 bytenr = btrfs_node_blockptr(buf, i);
513 err = btrfs_free_extent(trans, root, bytenr,
514 btrfs_level_size(root, level - 1), 0);
521 static int write_one_cache_group(struct btrfs_trans_handle *trans,
522 struct btrfs_root *root,
523 struct btrfs_path *path,
524 struct btrfs_block_group_cache *cache)
528 struct btrfs_root *extent_root = root->fs_info->extent_root;
530 struct extent_buffer *leaf;
532 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
537 leaf = path->nodes[0];
538 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
539 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
540 btrfs_mark_buffer_dirty(leaf);
541 btrfs_release_path(extent_root, path);
543 finish_current_insert(trans, extent_root);
544 pending_ret = del_pending_extents(trans, extent_root);
553 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
554 struct btrfs_root *root)
556 struct extent_map_tree *block_group_cache;
557 struct btrfs_block_group_cache *cache;
561 struct btrfs_path *path;
567 block_group_cache = &root->fs_info->block_group_cache;
568 path = btrfs_alloc_path();
573 ret = find_first_extent_bit(block_group_cache, last,
574 &start, &end, BLOCK_GROUP_DIRTY);
579 ret = get_state_private(block_group_cache, start, &ptr);
583 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
584 err = write_one_cache_group(trans, root,
587 * if we fail to write the cache group, we want
588 * to keep it marked dirty in hopes that a later
595 clear_extent_bits(block_group_cache, start, end,
596 BLOCK_GROUP_DIRTY, GFP_NOFS);
598 btrfs_free_path(path);
602 static int update_block_group(struct btrfs_trans_handle *trans,
603 struct btrfs_root *root,
604 u64 bytenr, u64 num_bytes, int alloc,
605 int mark_free, int data)
607 struct btrfs_block_group_cache *cache;
608 struct btrfs_fs_info *info = root->fs_info;
609 u64 total = num_bytes;
616 cache = btrfs_lookup_block_group(info, bytenr);
620 byte_in_group = bytenr - cache->key.objectid;
621 WARN_ON(byte_in_group > cache->key.offset);
622 start = cache->key.objectid;
623 end = start + cache->key.offset - 1;
624 set_extent_bits(&info->block_group_cache, start, end,
625 BLOCK_GROUP_DIRTY, GFP_NOFS);
627 old_val = btrfs_block_group_used(&cache->item);
628 num_bytes = min(total, cache->key.offset - byte_in_group);
630 if (cache->data != data &&
631 old_val < (cache->key.offset >> 1)) {
636 bit_to_clear = BLOCK_GROUP_METADATA;
637 bit_to_set = BLOCK_GROUP_DATA;
639 ~BTRFS_BLOCK_GROUP_MIXED;
641 BTRFS_BLOCK_GROUP_DATA;
643 bit_to_clear = BLOCK_GROUP_DATA;
644 bit_to_set = BLOCK_GROUP_METADATA;
646 ~BTRFS_BLOCK_GROUP_MIXED;
648 ~BTRFS_BLOCK_GROUP_DATA;
650 clear_extent_bits(&info->block_group_cache,
651 start, end, bit_to_clear,
653 set_extent_bits(&info->block_group_cache,
654 start, end, bit_to_set,
656 } else if (cache->data != data &&
657 cache->data != BTRFS_BLOCK_GROUP_MIXED) {
658 cache->data = BTRFS_BLOCK_GROUP_MIXED;
659 set_extent_bits(&info->block_group_cache,
662 BLOCK_GROUP_METADATA,
665 old_val += num_bytes;
667 old_val -= num_bytes;
669 set_extent_dirty(&info->free_space_cache,
670 bytenr, bytenr + num_bytes - 1,
674 btrfs_set_block_group_used(&cache->item, old_val);
680 static int update_pinned_extents(struct btrfs_root *root,
681 u64 bytenr, u64 num, int pin)
684 struct btrfs_block_group_cache *cache;
685 struct btrfs_fs_info *fs_info = root->fs_info;
688 set_extent_dirty(&fs_info->pinned_extents,
689 bytenr, bytenr + num - 1, GFP_NOFS);
691 clear_extent_dirty(&fs_info->pinned_extents,
692 bytenr, bytenr + num - 1, GFP_NOFS);
695 cache = btrfs_lookup_block_group(fs_info, bytenr);
697 len = min(num, cache->key.offset -
698 (bytenr - cache->key.objectid));
700 cache->pinned += len;
701 fs_info->total_pinned += len;
703 cache->pinned -= len;
704 fs_info->total_pinned -= len;
712 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
717 struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
721 ret = find_first_extent_bit(pinned_extents, last,
722 &start, &end, EXTENT_DIRTY);
725 set_extent_dirty(copy, start, end, GFP_NOFS);
731 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
732 struct btrfs_root *root,
733 struct extent_map_tree *unpin)
738 struct extent_map_tree *free_space_cache;
739 free_space_cache = &root->fs_info->free_space_cache;
742 ret = find_first_extent_bit(unpin, 0, &start, &end,
746 update_pinned_extents(root, start, end + 1 - start, 0);
747 clear_extent_dirty(unpin, start, end, GFP_NOFS);
748 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
753 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
754 btrfs_root *extent_root)
756 struct btrfs_key ins;
757 struct btrfs_extent_item extent_item;
762 struct btrfs_fs_info *info = extent_root->fs_info;
764 btrfs_set_stack_extent_refs(&extent_item, 1);
765 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
766 btrfs_set_stack_extent_owner(&extent_item,
767 extent_root->root_key.objectid);
770 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
771 &end, EXTENT_LOCKED);
775 ins.objectid = start;
776 ins.offset = end + 1 - start;
777 err = btrfs_insert_item(trans, extent_root, &ins,
778 &extent_item, sizeof(extent_item));
779 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
785 static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
789 struct extent_buffer *buf;
792 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
794 if (btrfs_buffer_uptodate(buf)) {
796 root->fs_info->running_transaction->transid;
797 if (btrfs_header_generation(buf) == transid) {
798 free_extent_buffer(buf);
802 free_extent_buffer(buf);
804 update_pinned_extents(root, bytenr, num_bytes, 1);
806 set_extent_bits(&root->fs_info->pending_del,
807 bytenr, bytenr + num_bytes - 1,
808 EXTENT_LOCKED, GFP_NOFS);
815 * remove an extent from the root, returns 0 on success
817 static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
818 *root, u64 bytenr, u64 num_bytes, int pin,
821 struct btrfs_path *path;
822 struct btrfs_key key;
823 struct btrfs_fs_info *info = root->fs_info;
824 struct btrfs_root *extent_root = info->extent_root;
825 struct extent_buffer *leaf;
827 struct btrfs_extent_item *ei;
830 key.objectid = bytenr;
831 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
832 key.offset = num_bytes;
834 path = btrfs_alloc_path();
838 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
843 leaf = path->nodes[0];
844 ei = btrfs_item_ptr(leaf, path->slots[0],
845 struct btrfs_extent_item);
846 refs = btrfs_extent_refs(leaf, ei);
849 btrfs_set_extent_refs(leaf, ei, refs);
850 btrfs_mark_buffer_dirty(leaf);
857 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
863 /* block accounting for super block */
864 super_used = btrfs_super_bytes_used(&info->super_copy);
865 btrfs_set_super_bytes_used(&info->super_copy,
866 super_used - num_bytes);
868 /* block accounting for root item */
869 root_used = btrfs_root_used(&root->root_item);
870 btrfs_set_root_used(&root->root_item,
871 root_used - num_bytes);
873 ret = btrfs_del_item(trans, extent_root, path);
877 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
881 btrfs_free_path(path);
882 finish_current_insert(trans, extent_root);
887 * find all the blocks marked as pending in the radix tree and remove
888 * them from the extent map
890 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
891 btrfs_root *extent_root)
897 struct extent_map_tree *pending_del;
898 struct extent_map_tree *pinned_extents;
900 pending_del = &extent_root->fs_info->pending_del;
901 pinned_extents = &extent_root->fs_info->pinned_extents;
904 ret = find_first_extent_bit(pending_del, 0, &start, &end,
908 update_pinned_extents(extent_root, start, end + 1 - start, 1);
909 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
911 ret = __free_extent(trans, extent_root,
912 start, end + 1 - start, 0, 0);
920 * remove an extent from the root, returns 0 on success
922 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
923 *root, u64 bytenr, u64 num_bytes, int pin)
925 struct btrfs_root *extent_root = root->fs_info->extent_root;
929 WARN_ON(num_bytes < root->sectorsize);
930 if (root == extent_root) {
931 pin_down_bytes(root, bytenr, num_bytes, 1);
934 ret = __free_extent(trans, root, bytenr, num_bytes, pin, pin == 0);
935 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
936 return ret ? ret : pending_ret;
939 static u64 stripe_align(struct btrfs_root *root, u64 val)
941 u64 mask = ((u64)root->stripesize - 1);
942 u64 ret = (val + mask) & ~mask;
947 * walks the btree of allocated extents and find a hole of a given size.
948 * The key ins is changed to record the hole:
949 * ins->objectid == block start
950 * ins->flags = BTRFS_EXTENT_ITEM_KEY
951 * ins->offset == number of blocks
952 * Any available blocks before search_start are skipped.
954 static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
955 *orig_root, u64 num_bytes, u64 empty_size,
956 u64 search_start, u64 search_end, u64 hint_byte,
957 struct btrfs_key *ins, u64 exclude_start,
958 u64 exclude_nr, int data)
960 struct btrfs_path *path;
961 struct btrfs_key key;
967 u64 orig_search_start = search_start;
969 struct extent_buffer *l;
970 struct btrfs_root * root = orig_root->fs_info->extent_root;
971 struct btrfs_fs_info *info = root->fs_info;
972 u64 total_needed = num_bytes;
974 struct btrfs_block_group_cache *block_group;
979 WARN_ON(num_bytes < root->sectorsize);
980 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
982 level = btrfs_header_level(root->node);
984 if (num_bytes >= 32 * 1024 * 1024 && hint_byte) {
985 data = BTRFS_BLOCK_GROUP_MIXED;
988 if (search_end == (u64)-1)
989 search_end = btrfs_super_total_bytes(&info->super_copy);
991 block_group = btrfs_lookup_block_group(info, hint_byte);
993 hint_byte = search_start;
994 block_group = btrfs_find_block_group(root, block_group,
997 block_group = btrfs_find_block_group(root,
999 search_start, data, 1);
1002 total_needed += empty_size;
1003 path = btrfs_alloc_path();
1005 search_start = find_search_start(root, &block_group, search_start,
1006 total_needed, data, full_scan);
1007 search_start = stripe_align(root, search_start);
1008 cached_start = search_start;
1009 btrfs_init_path(path);
1010 ins->objectid = search_start;
1015 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1019 if (path->slots[0] > 0) {
1024 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
1027 * a rare case, go back one key if we hit a block group item
1028 * instead of an extent item
1030 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
1031 key.objectid + key.offset >= search_start) {
1032 ins->objectid = key.objectid;
1033 ins->offset = key.offset - 1;
1034 btrfs_release_path(root, path);
1035 ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
1039 if (path->slots[0] > 0) {
1046 slot = path->slots[0];
1047 if (slot >= btrfs_header_nritems(l)) {
1048 ret = btrfs_next_leaf(root, path);
1054 search_start = max(search_start,
1055 block_group->key.objectid);
1057 aligned = stripe_align(root, search_start);
1058 ins->objectid = aligned;
1059 if (aligned >= search_end) {
1063 ins->offset = search_end - aligned;
1067 ins->objectid = stripe_align(root,
1068 last_byte > search_start ?
1069 last_byte : search_start);
1070 if (search_end <= ins->objectid) {
1074 ins->offset = search_end - ins->objectid;
1075 BUG_ON(ins->objectid >= search_end);
1078 btrfs_item_key_to_cpu(l, &key, slot);
1080 if (key.objectid >= search_start && key.objectid > last_byte &&
1082 if (last_byte < search_start)
1083 last_byte = search_start;
1084 aligned = stripe_align(root, last_byte);
1085 hole_size = key.objectid - aligned;
1086 if (key.objectid > aligned && hole_size >= num_bytes) {
1087 ins->objectid = aligned;
1088 ins->offset = hole_size;
1092 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
1094 last_byte = key.objectid;
1102 last_byte = key.objectid + key.offset;
1104 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
1105 last_byte >= block_group->key.objectid +
1106 block_group->key.offset) {
1107 btrfs_release_path(root, path);
1108 search_start = block_group->key.objectid +
1109 block_group->key.offset;
1117 /* we have to make sure we didn't find an extent that has already
1118 * been allocated by the map tree or the original allocation
1120 btrfs_release_path(root, path);
1121 BUG_ON(ins->objectid < search_start);
1123 if (ins->objectid + num_bytes >= search_end)
1125 if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
1126 ins->objectid + num_bytes > block_group->
1127 key.objectid + block_group->key.offset) {
1128 search_start = block_group->key.objectid +
1129 block_group->key.offset;
1132 if (test_range_bit(&info->extent_ins, ins->objectid,
1133 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1134 search_start = ins->objectid + num_bytes;
1137 if (test_range_bit(&info->pinned_extents, ins->objectid,
1138 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1139 search_start = ins->objectid + num_bytes;
1142 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
1143 ins->objectid < exclude_start + exclude_nr)) {
1144 search_start = exclude_start + exclude_nr;
1148 block_group = btrfs_lookup_block_group(info, ins->objectid);
1150 trans->block_group = block_group;
1152 ins->offset = num_bytes;
1153 btrfs_free_path(path);
1157 if (search_start + num_bytes >= search_end) {
1159 search_start = orig_search_start;
1166 total_needed -= empty_size;
1168 data = BTRFS_BLOCK_GROUP_MIXED;
1172 block_group = btrfs_lookup_block_group(info, search_start);
1174 block_group = btrfs_find_block_group(root, block_group,
1175 search_start, data, 0);
1179 btrfs_release_path(root, path);
1180 btrfs_free_path(path);
1184 * finds a free extent and does all the dirty work required for allocation
1185 * returns the key for the extent through ins, and a tree buffer for
1186 * the first block of the extent through buf.
1188 * returns 0 if everything worked, non-zero otherwise.
1190 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
1191 struct btrfs_root *root, u64 owner,
1192 u64 num_bytes, u64 empty_size, u64 hint_byte,
1193 u64 search_end, struct btrfs_key *ins, int data)
1197 u64 super_used, root_used;
1198 u64 search_start = 0;
1199 struct btrfs_fs_info *info = root->fs_info;
1200 struct btrfs_root *extent_root = info->extent_root;
1201 struct btrfs_extent_item extent_item;
1203 btrfs_set_stack_extent_refs(&extent_item, 1);
1204 btrfs_set_stack_extent_owner(&extent_item, owner);
1206 WARN_ON(num_bytes < root->sectorsize);
1207 ret = find_free_extent(trans, root, num_bytes, empty_size,
1208 search_start, search_end, hint_byte, ins,
1209 trans->alloc_exclude_start,
1210 trans->alloc_exclude_nr, data);
1215 /* block accounting for super block */
1216 super_used = btrfs_super_bytes_used(&info->super_copy);
1217 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
1219 /* block accounting for root item */
1220 root_used = btrfs_root_used(&root->root_item);
1221 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
1223 clear_extent_dirty(&root->fs_info->free_space_cache,
1224 ins->objectid, ins->objectid + ins->offset - 1,
1227 if (root == extent_root) {
1228 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1229 ins->objectid + ins->offset - 1,
1230 EXTENT_LOCKED, GFP_NOFS);
1235 WARN_ON(trans->alloc_exclude_nr);
1236 trans->alloc_exclude_start = ins->objectid;
1237 trans->alloc_exclude_nr = ins->offset;
1238 ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
1239 sizeof(extent_item));
1241 trans->alloc_exclude_start = 0;
1242 trans->alloc_exclude_nr = 0;
1245 finish_current_insert(trans, extent_root);
1246 pending_ret = del_pending_extents(trans, extent_root);
1256 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
1263 * helper function to allocate a block for a given tree
1264 * returns the tree buffer or NULL.
1266 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1267 struct btrfs_root *root,
1268 u32 blocksize, u64 hint,
1271 struct btrfs_key ins;
1273 struct extent_buffer *buf;
1275 ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
1276 blocksize, empty_size, hint,
1280 return ERR_PTR(ret);
1282 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
1284 btrfs_free_extent(trans, root, ins.objectid, blocksize, 0);
1285 return ERR_PTR(-ENOMEM);
1287 btrfs_set_buffer_uptodate(buf);
1288 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1289 buf->start + buf->len - 1, GFP_NOFS);
1290 set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,
1291 buf->start, buf->start + buf->len - 1,
1292 EXTENT_CSUM, GFP_NOFS);
1293 buf->flags |= EXTENT_CSUM;
1294 btrfs_set_buffer_defrag(buf);
1295 trans->blocks_used++;
1299 static int drop_leaf_ref(struct btrfs_trans_handle *trans,
1300 struct btrfs_root *root, struct extent_buffer *leaf)
1302 struct btrfs_key key;
1303 struct btrfs_file_extent_item *fi;
1308 BUG_ON(!btrfs_is_leaf(leaf));
1309 nritems = btrfs_header_nritems(leaf);
1310 for (i = 0; i < nritems; i++) {
1313 btrfs_item_key_to_cpu(leaf, &key, i);
1314 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1316 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
1317 if (btrfs_file_extent_type(leaf, fi) ==
1318 BTRFS_FILE_EXTENT_INLINE)
1321 * FIXME make sure to insert a trans record that
1322 * repeats the snapshot del on crash
1324 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1325 if (disk_bytenr == 0)
1327 ret = btrfs_free_extent(trans, root, disk_bytenr,
1328 btrfs_file_extent_disk_num_bytes(leaf, fi), 0);
1334 static void reada_walk_down(struct btrfs_root *root,
1335 struct extent_buffer *node)
1345 nritems = btrfs_header_nritems(node);
1346 level = btrfs_header_level(node);
1347 for (i = 0; i < nritems; i++) {
1348 bytenr = btrfs_node_blockptr(node, i);
1349 blocksize = btrfs_level_size(root, level - 1);
1350 ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
1354 mutex_unlock(&root->fs_info->fs_mutex);
1355 ret = readahead_tree_block(root, bytenr, blocksize);
1357 mutex_lock(&root->fs_info->fs_mutex);
1364 * helper function for drop_snapshot, this walks down the tree dropping ref
1365 * counts as it goes.
1367 static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1368 *root, struct btrfs_path *path, int *level)
1370 struct extent_buffer *next;
1371 struct extent_buffer *cur;
1377 WARN_ON(*level < 0);
1378 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1379 ret = lookup_extent_ref(trans, root,
1380 path->nodes[*level]->start,
1381 path->nodes[*level]->len, &refs);
1387 * walk down to the last node level and free all the leaves
1389 while(*level >= 0) {
1390 WARN_ON(*level < 0);
1391 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1392 cur = path->nodes[*level];
1394 if (*level > 0 && path->slots[*level] == 0)
1395 reada_walk_down(root, cur);
1397 if (btrfs_header_level(cur) != *level)
1400 if (path->slots[*level] >=
1401 btrfs_header_nritems(cur))
1404 ret = drop_leaf_ref(trans, root, cur);
1408 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
1409 blocksize = btrfs_level_size(root, *level - 1);
1410 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
1413 path->slots[*level]++;
1414 ret = btrfs_free_extent(trans, root, bytenr,
1419 next = btrfs_find_tree_block(root, bytenr, blocksize);
1420 if (!next || !btrfs_buffer_uptodate(next)) {
1421 free_extent_buffer(next);
1422 mutex_unlock(&root->fs_info->fs_mutex);
1423 next = read_tree_block(root, bytenr, blocksize);
1424 mutex_lock(&root->fs_info->fs_mutex);
1426 /* we dropped the lock, check one more time */
1427 ret = lookup_extent_ref(trans, root, bytenr,
1431 path->slots[*level]++;
1432 free_extent_buffer(next);
1433 ret = btrfs_free_extent(trans, root,
1434 bytenr, blocksize, 1);
1439 WARN_ON(*level <= 0);
1440 if (path->nodes[*level-1])
1441 free_extent_buffer(path->nodes[*level-1]);
1442 path->nodes[*level-1] = next;
1443 *level = btrfs_header_level(next);
1444 path->slots[*level] = 0;
1447 WARN_ON(*level < 0);
1448 WARN_ON(*level >= BTRFS_MAX_LEVEL);
1449 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
1450 path->nodes[*level]->len, 1);
1451 free_extent_buffer(path->nodes[*level]);
1452 path->nodes[*level] = NULL;
1459 * helper for dropping snapshots. This walks back up the tree in the path
1460 * to find the first node higher up where we haven't yet gone through
1463 static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
1464 *root, struct btrfs_path *path, int *level)
1469 struct btrfs_root_item *root_item = &root->root_item;
1471 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
1472 slot = path->slots[i];
1473 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
1474 struct extent_buffer *node;
1475 struct btrfs_disk_key disk_key;
1476 node = path->nodes[i];
1479 WARN_ON(*level == 0);
1480 btrfs_node_key(node, &disk_key, path->slots[i]);
1481 memcpy(&root_item->drop_progress,
1482 &disk_key, sizeof(disk_key));
1483 root_item->drop_level = i;
1486 ret = btrfs_free_extent(trans, root,
1487 path->nodes[*level]->start,
1488 path->nodes[*level]->len, 1);
1490 free_extent_buffer(path->nodes[*level]);
1491 path->nodes[*level] = NULL;
1499 * drop the reference count on the tree rooted at 'snap'. This traverses
1500 * the tree freeing any blocks that have a ref count of zero after being
1503 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
1509 struct btrfs_path *path;
1512 struct btrfs_root_item *root_item = &root->root_item;
1514 path = btrfs_alloc_path();
1517 level = btrfs_header_level(root->node);
1519 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
1520 path->nodes[level] = root->node;
1521 extent_buffer_get(root->node);
1522 path->slots[level] = 0;
1524 struct btrfs_key key;
1525 struct btrfs_disk_key found_key;
1526 struct extent_buffer *node;
1528 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
1529 level = root_item->drop_level;
1530 path->lowest_level = level;
1531 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1536 node = path->nodes[level];
1537 btrfs_node_key(node, &found_key, path->slots[level]);
1538 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
1539 sizeof(found_key)));
1542 wret = walk_down_tree(trans, root, path, &level);
1548 wret = walk_up_tree(trans, root, path, &level);
1556 for (i = 0; i <= orig_level; i++) {
1557 if (path->nodes[i]) {
1558 free_extent_buffer(path->nodes[i]);
1559 path->nodes[i] = NULL;
1563 btrfs_free_path(path);
1567 int btrfs_free_block_groups(struct btrfs_fs_info *info)
1574 ret = find_first_extent_bit(&info->block_group_cache, 0,
1575 &start, &end, (unsigned int)-1);
1578 ret = get_state_private(&info->block_group_cache, start, &ptr);
1580 kfree((void *)(unsigned long)ptr);
1581 clear_extent_bits(&info->block_group_cache, start,
1582 end, (unsigned int)-1, GFP_NOFS);
1585 ret = find_first_extent_bit(&info->free_space_cache, 0,
1586 &start, &end, EXTENT_DIRTY);
1589 clear_extent_dirty(&info->free_space_cache, start,
1595 int btrfs_read_block_groups(struct btrfs_root *root)
1597 struct btrfs_path *path;
1601 struct btrfs_block_group_cache *cache;
1602 struct btrfs_fs_info *info = root->fs_info;
1603 struct extent_map_tree *block_group_cache;
1604 struct btrfs_key key;
1605 struct btrfs_key found_key;
1606 struct extent_buffer *leaf;
1608 block_group_cache = &info->block_group_cache;
1610 root = info->extent_root;
1612 key.offset = BTRFS_BLOCK_GROUP_SIZE;
1613 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
1615 path = btrfs_alloc_path();
1620 ret = btrfs_search_slot(NULL, info->extent_root,
1626 leaf = path->nodes[0];
1627 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1628 cache = kmalloc(sizeof(*cache), GFP_NOFS);
1634 read_extent_buffer(leaf, &cache->item,
1635 btrfs_item_ptr_offset(leaf, path->slots[0]),
1636 sizeof(cache->item));
1637 memcpy(&cache->key, &found_key, sizeof(found_key));
1640 key.objectid = found_key.objectid + found_key.offset;
1641 btrfs_release_path(root, path);
1643 if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) {
1644 bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
1645 cache->data = BTRFS_BLOCK_GROUP_MIXED;
1646 } else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
1647 bit = BLOCK_GROUP_DATA;
1648 cache->data = BTRFS_BLOCK_GROUP_DATA;
1650 bit = BLOCK_GROUP_METADATA;
1654 /* use EXTENT_LOCKED to prevent merging */
1655 set_extent_bits(block_group_cache, found_key.objectid,
1656 found_key.objectid + found_key.offset - 1,
1657 bit | EXTENT_LOCKED, GFP_NOFS);
1658 set_state_private(block_group_cache, found_key.objectid,
1659 (unsigned long)cache);
1662 btrfs_super_total_bytes(&info->super_copy))
1666 btrfs_free_path(path);