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.
21 #include "kerncompat.h"
22 #include "radix-tree.h"
25 #include "print-tree.h"
26 #include "transaction.h"
30 #define BLOCK_GROUP_DATA EXTENT_WRITEBACK
31 #define BLOCK_GROUP_METADATA EXTENT_UPTODATE
32 #define BLOCK_GROUP_SYSTEM EXTENT_NEW
34 #define BLOCK_GROUP_DIRTY EXTENT_DIRTY
36 #define PENDING_EXTENT_INSERT 0
37 #define PENDING_EXTENT_DELETE 1
38 #define PENDING_BACKREF_UPDATE 2
40 struct pending_extent_op {
45 struct btrfs_disk_key key;
49 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
50 struct btrfs_root *root,
51 u64 root_objectid, u64 generation,
52 u64 flags, struct btrfs_disk_key *key,
53 int level, struct btrfs_key *ins);
54 static int __free_extent(struct btrfs_trans_handle *trans,
55 struct btrfs_root *root,
56 u64 bytenr, u64 num_bytes, u64 parent,
57 u64 root_objectid, u64 owner_objectid,
58 u64 owner_offset, int refs_to_drop);
59 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
60 btrfs_root *extent_root);
61 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
62 btrfs_root *extent_root);
64 static int remove_sb_from_cache(struct btrfs_root *root,
65 struct btrfs_block_group_cache *cache)
71 struct extent_io_tree *free_space_cache;
73 free_space_cache = &root->fs_info->free_space_cache;
74 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
75 bytenr = btrfs_sb_offset(i);
76 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
77 cache->key.objectid, bytenr, 0,
78 &logical, &nr, &stripe_len);
81 clear_extent_dirty(free_space_cache, logical[nr],
82 logical[nr] + stripe_len - 1, GFP_NOFS);
89 static int cache_block_group(struct btrfs_root *root,
90 struct btrfs_block_group_cache *block_group)
92 struct btrfs_path *path;
95 struct extent_buffer *leaf;
96 struct extent_io_tree *free_space_cache;
104 root = root->fs_info->extent_root;
105 free_space_cache = &root->fs_info->free_space_cache;
107 if (block_group->cached)
110 path = btrfs_alloc_path();
115 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
118 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
119 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
124 leaf = path->nodes[0];
125 slot = path->slots[0];
126 if (slot >= btrfs_header_nritems(leaf)) {
127 ret = btrfs_next_leaf(root, path);
136 btrfs_item_key_to_cpu(leaf, &key, slot);
137 if (key.objectid < block_group->key.objectid) {
140 if (key.objectid >= block_group->key.objectid +
141 block_group->key.offset) {
145 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
146 if (key.objectid > last) {
147 hole_size = key.objectid - last;
148 set_extent_dirty(free_space_cache, last,
149 last + hole_size - 1,
152 last = key.objectid + key.offset;
158 if (block_group->key.objectid +
159 block_group->key.offset > last) {
160 hole_size = block_group->key.objectid +
161 block_group->key.offset - last;
162 set_extent_dirty(free_space_cache, last,
163 last + hole_size - 1, GFP_NOFS);
165 remove_sb_from_cache(root, block_group);
166 block_group->cached = 1;
168 btrfs_free_path(path);
172 struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
176 struct extent_io_tree *block_group_cache;
177 struct btrfs_block_group_cache *block_group = NULL;
183 bytenr = max_t(u64, bytenr,
184 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
185 block_group_cache = &info->block_group_cache;
186 ret = find_first_extent_bit(block_group_cache,
187 bytenr, &start, &end,
188 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
193 ret = get_state_private(block_group_cache, start, &ptr);
197 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
201 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
205 struct extent_io_tree *block_group_cache;
206 struct btrfs_block_group_cache *block_group = NULL;
212 block_group_cache = &info->block_group_cache;
213 ret = find_first_extent_bit(block_group_cache,
214 bytenr, &start, &end,
215 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
220 ret = get_state_private(block_group_cache, start, &ptr);
224 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
225 if (block_group->key.objectid <= bytenr && bytenr <
226 block_group->key.objectid + block_group->key.offset)
231 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
233 return (cache->flags & bits) == bits;
236 static int noinline find_search_start(struct btrfs_root *root,
237 struct btrfs_block_group_cache **cache_ret,
238 u64 *start_ret, int num, int data)
241 struct btrfs_block_group_cache *cache = *cache_ret;
245 u64 search_start = *start_ret;
252 ret = cache_block_group(root, cache);
256 last = max(search_start, cache->key.objectid);
257 if (cache->ro || !block_group_bits(cache, data)) {
262 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
263 last, &start, &end, EXTENT_DIRTY);
268 start = max(last, start);
270 if (last - start < num) {
273 if (start + num > cache->key.objectid + cache->key.offset) {
280 cache = btrfs_lookup_block_group(root->fs_info, search_start);
282 printk("Unable to find block group for %llu\n",
283 (unsigned long long)search_start);
289 last = cache->key.objectid + cache->key.offset;
291 cache = btrfs_lookup_first_block_group(root->fs_info, last);
301 cache = btrfs_find_block_group(root, cache, last, data, 0);
302 cache = btrfs_find_block_group(root, cache, last, data, 0);
310 static u64 div_factor(u64 num, int factor)
319 static int block_group_state_bits(u64 flags)
322 if (flags & BTRFS_BLOCK_GROUP_DATA)
323 bits |= BLOCK_GROUP_DATA;
324 if (flags & BTRFS_BLOCK_GROUP_METADATA)
325 bits |= BLOCK_GROUP_METADATA;
326 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
327 bits |= BLOCK_GROUP_SYSTEM;
331 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
332 struct btrfs_block_group_cache
333 *hint, u64 search_start,
336 struct btrfs_block_group_cache *cache;
337 struct extent_io_tree *block_group_cache;
338 struct btrfs_block_group_cache *found_group = NULL;
339 struct btrfs_fs_info *info = root->fs_info;
352 block_group_cache = &info->block_group_cache;
357 bit = block_group_state_bits(data);
360 struct btrfs_block_group_cache *shint;
361 shint = btrfs_lookup_block_group(info, search_start);
362 if (shint && !shint->ro && block_group_bits(shint, data)) {
363 used = btrfs_block_group_used(&shint->item);
364 if (used + shint->pinned <
365 div_factor(shint->key.offset, factor)) {
370 if (hint && !hint->ro && block_group_bits(hint, data)) {
371 used = btrfs_block_group_used(&hint->item);
372 if (used + hint->pinned <
373 div_factor(hint->key.offset, factor)) {
376 last = hint->key.objectid + hint->key.offset;
380 hint_last = max(hint->key.objectid, search_start);
382 hint_last = search_start;
388 ret = find_first_extent_bit(block_group_cache, last,
393 ret = get_state_private(block_group_cache, start, &ptr);
397 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
398 last = cache->key.objectid + cache->key.offset;
399 used = btrfs_block_group_used(&cache->item);
401 if (!cache->ro && block_group_bits(cache, data)) {
403 free_check = cache->key.offset;
405 free_check = div_factor(cache->key.offset,
408 if (used + cache->pinned < free_check) {
425 * Back reference rules. Back refs have three main goals:
427 * 1) differentiate between all holders of references to an extent so that
428 * when a reference is dropped we can make sure it was a valid reference
429 * before freeing the extent.
431 * 2) Provide enough information to quickly find the holders of an extent
432 * if we notice a given block is corrupted or bad.
434 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
435 * maintenance. This is actually the same as #2, but with a slightly
436 * different use case.
438 * There are two kinds of back refs. The implicit back refs is optimized
439 * for pointers in non-shared tree blocks. For a given pointer in a block,
440 * back refs of this kind provide information about the block's owner tree
441 * and the pointer's key. These information allow us to find the block by
442 * b-tree searching. The full back refs is for pointers in tree blocks not
443 * referenced by their owner trees. The location of tree block is recorded
444 * in the back refs. Actually the full back refs is generic, and can be
445 * used in all cases the implicit back refs is used. The major shortcoming
446 * of the full back refs is its overhead. Every time a tree block gets
447 * COWed, we have to update back refs entry for all pointers in it.
449 * For a newly allocated tree block, we use implicit back refs for
450 * pointers in it. This means most tree related operations only involve
451 * implicit back refs. For a tree block created in old transaction, the
452 * only way to drop a reference to it is COW it. So we can detect the
453 * event that tree block loses its owner tree's reference and do the
454 * back refs conversion.
456 * When a tree block is COW'd through a tree, there are four cases:
458 * The reference count of the block is one and the tree is the block's
459 * owner tree. Nothing to do in this case.
461 * The reference count of the block is one and the tree is not the
462 * block's owner tree. In this case, full back refs is used for pointers
463 * in the block. Remove these full back refs, add implicit back refs for
464 * every pointers in the new block.
466 * The reference count of the block is greater than one and the tree is
467 * the block's owner tree. In this case, implicit back refs is used for
468 * pointers in the block. Add full back refs for every pointers in the
469 * block, increase lower level extents' reference counts. The original
470 * implicit back refs are entailed to the new block.
472 * The reference count of the block is greater than one and the tree is
473 * not the block's owner tree. Add implicit back refs for every pointer in
474 * the new block, increase lower level extents' reference count.
476 * Back Reference Key composing:
478 * The key objectid corresponds to the first byte in the extent,
479 * The key type is used to differentiate between types of back refs.
480 * There are different meanings of the key offset for different types
483 * File extents can be referenced by:
485 * - multiple snapshots, subvolumes, or different generations in one subvol
486 * - different files inside a single subvolume
487 * - different offsets inside a file (bookend extents in file.c)
489 * The extent ref structure for the implicit back refs has fields for:
491 * - Objectid of the subvolume root
492 * - objectid of the file holding the reference
493 * - original offset in the file
494 * - how many bookend extents
496 * The key offset for the implicit back refs is hash of the first
499 * The extent ref structure for the full back refs has field for:
501 * - number of pointers in the tree leaf
503 * The key offset for the implicit back refs is the first byte of
506 * When a file extent is allocated, The implicit back refs is used.
507 * the fields are filled in:
509 * (root_key.objectid, inode objectid, offset in file, 1)
511 * When a file extent is removed file truncation, we find the
512 * corresponding implicit back refs and check the following fields:
514 * (btrfs_header_owner(leaf), inode objectid, offset in file)
516 * Btree extents can be referenced by:
518 * - Different subvolumes
520 * Both the implicit back refs and the full back refs for tree blocks
521 * only consist of key. The key offset for the implicit back refs is
522 * objectid of block's owner tree. The key offset for the full back refs
523 * is the first byte of parent block.
525 * When implicit back refs is used, information about the lowest key and
526 * level of the tree block are required. These information are stored in
527 * tree block info structure.
530 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
531 static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
532 struct btrfs_root *root,
533 struct btrfs_path *path,
534 u64 owner, u32 extra_size)
536 struct btrfs_extent_item *item;
537 struct btrfs_extent_item_v0 *ei0;
538 struct btrfs_extent_ref_v0 *ref0;
539 struct btrfs_tree_block_info *bi;
540 struct extent_buffer *leaf;
541 struct btrfs_key key;
542 struct btrfs_key found_key;
543 u32 new_size = sizeof(*item);
547 leaf = path->nodes[0];
548 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
550 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
551 ei0 = btrfs_item_ptr(leaf, path->slots[0],
552 struct btrfs_extent_item_v0);
553 refs = btrfs_extent_refs_v0(leaf, ei0);
555 if (owner == (u64)-1) {
557 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
558 ret = btrfs_next_leaf(root, path);
562 leaf = path->nodes[0];
564 btrfs_item_key_to_cpu(leaf, &found_key,
566 BUG_ON(key.objectid != found_key.objectid);
567 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
571 ref0 = btrfs_item_ptr(leaf, path->slots[0],
572 struct btrfs_extent_ref_v0);
573 owner = btrfs_ref_objectid_v0(leaf, ref0);
577 btrfs_release_path(root, path);
579 if (owner < BTRFS_FIRST_FREE_OBJECTID)
580 new_size += sizeof(*bi);
582 new_size -= sizeof(*ei0);
583 ret = btrfs_search_slot(trans, root, &key, path, new_size, 1);
588 ret = btrfs_extend_item(trans, root, path, new_size);
591 leaf = path->nodes[0];
592 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
593 btrfs_set_extent_refs(leaf, item, refs);
594 /* FIXME: get real generation */
595 btrfs_set_extent_generation(leaf, item, 0);
596 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
597 btrfs_set_extent_flags(leaf, item,
598 BTRFS_EXTENT_FLAG_TREE_BLOCK |
599 BTRFS_BLOCK_FLAG_FULL_BACKREF);
600 bi = (struct btrfs_tree_block_info *)(item + 1);
601 /* FIXME: get first key of the block */
602 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
603 btrfs_set_tree_block_level(leaf, bi, (int)owner);
605 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
607 btrfs_mark_buffer_dirty(leaf);
612 static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
614 u32 high_crc = ~(u32)0;
615 u32 low_crc = ~(u32)0;
618 lenum = cpu_to_le64(root_objectid);
619 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
620 lenum = cpu_to_le64(owner);
621 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
622 lenum = cpu_to_le64(offset);
623 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
625 return ((u64)high_crc << 31) ^ (u64)low_crc;
628 static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
629 struct btrfs_extent_data_ref *ref)
631 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
632 btrfs_extent_data_ref_objectid(leaf, ref),
633 btrfs_extent_data_ref_offset(leaf, ref));
636 static int match_extent_data_ref(struct extent_buffer *leaf,
637 struct btrfs_extent_data_ref *ref,
638 u64 root_objectid, u64 owner, u64 offset)
640 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
641 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
642 btrfs_extent_data_ref_offset(leaf, ref) != offset)
647 static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
648 struct btrfs_root *root,
649 struct btrfs_path *path,
650 u64 bytenr, u64 parent,
652 u64 owner, u64 offset)
654 struct btrfs_key key;
655 struct btrfs_extent_data_ref *ref;
656 struct extent_buffer *leaf;
662 key.objectid = bytenr;
664 key.type = BTRFS_SHARED_DATA_REF_KEY;
667 key.type = BTRFS_EXTENT_DATA_REF_KEY;
668 key.offset = hash_extent_data_ref(root_objectid,
673 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
682 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
683 key.type = BTRFS_EXTENT_REF_V0_KEY;
684 btrfs_release_path(root, path);
685 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
696 leaf = path->nodes[0];
697 nritems = btrfs_header_nritems(leaf);
699 if (path->slots[0] >= nritems) {
700 ret = btrfs_next_leaf(root, path);
706 leaf = path->nodes[0];
707 nritems = btrfs_header_nritems(leaf);
711 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
712 if (key.objectid != bytenr ||
713 key.type != BTRFS_EXTENT_DATA_REF_KEY)
716 ref = btrfs_item_ptr(leaf, path->slots[0],
717 struct btrfs_extent_data_ref);
719 if (match_extent_data_ref(leaf, ref, root_objectid,
722 btrfs_release_path(root, path);
734 static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
735 struct btrfs_root *root,
736 struct btrfs_path *path,
737 u64 bytenr, u64 parent,
738 u64 root_objectid, u64 owner,
739 u64 offset, int refs_to_add)
741 struct btrfs_key key;
742 struct extent_buffer *leaf;
747 key.objectid = bytenr;
749 key.type = BTRFS_SHARED_DATA_REF_KEY;
751 size = sizeof(struct btrfs_shared_data_ref);
753 key.type = BTRFS_EXTENT_DATA_REF_KEY;
754 key.offset = hash_extent_data_ref(root_objectid,
756 size = sizeof(struct btrfs_extent_data_ref);
759 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
760 if (ret && ret != -EEXIST)
763 leaf = path->nodes[0];
765 struct btrfs_shared_data_ref *ref;
766 ref = btrfs_item_ptr(leaf, path->slots[0],
767 struct btrfs_shared_data_ref);
769 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
771 num_refs = btrfs_shared_data_ref_count(leaf, ref);
772 num_refs += refs_to_add;
773 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
776 struct btrfs_extent_data_ref *ref;
777 while (ret == -EEXIST) {
778 ref = btrfs_item_ptr(leaf, path->slots[0],
779 struct btrfs_extent_data_ref);
780 if (match_extent_data_ref(leaf, ref, root_objectid,
783 btrfs_release_path(root, path);
786 ret = btrfs_insert_empty_item(trans, root, path, &key,
788 if (ret && ret != -EEXIST)
791 leaf = path->nodes[0];
793 ref = btrfs_item_ptr(leaf, path->slots[0],
794 struct btrfs_extent_data_ref);
796 btrfs_set_extent_data_ref_root(leaf, ref,
798 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
799 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
800 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
802 num_refs = btrfs_extent_data_ref_count(leaf, ref);
803 num_refs += refs_to_add;
804 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
807 btrfs_mark_buffer_dirty(leaf);
810 btrfs_release_path(root, path);
814 static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
815 struct btrfs_root *root,
816 struct btrfs_path *path,
819 struct btrfs_key key;
820 struct btrfs_extent_data_ref *ref1 = NULL;
821 struct btrfs_shared_data_ref *ref2 = NULL;
822 struct extent_buffer *leaf;
826 leaf = path->nodes[0];
827 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
829 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
830 ref1 = btrfs_item_ptr(leaf, path->slots[0],
831 struct btrfs_extent_data_ref);
832 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
833 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
834 ref2 = btrfs_item_ptr(leaf, path->slots[0],
835 struct btrfs_shared_data_ref);
836 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
837 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
838 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
839 struct btrfs_extent_ref_v0 *ref0;
840 ref0 = btrfs_item_ptr(leaf, path->slots[0],
841 struct btrfs_extent_ref_v0);
842 num_refs = btrfs_ref_count_v0(leaf, ref0);
848 BUG_ON(num_refs < refs_to_drop);
849 num_refs -= refs_to_drop;
852 ret = btrfs_del_item(trans, root, path);
854 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
855 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
856 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
857 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
858 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
860 struct btrfs_extent_ref_v0 *ref0;
861 ref0 = btrfs_item_ptr(leaf, path->slots[0],
862 struct btrfs_extent_ref_v0);
863 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
866 btrfs_mark_buffer_dirty(leaf);
871 static noinline u32 extent_data_ref_count(struct btrfs_root *root,
872 struct btrfs_path *path,
873 struct btrfs_extent_inline_ref *iref)
875 struct btrfs_key key;
876 struct extent_buffer *leaf;
877 struct btrfs_extent_data_ref *ref1;
878 struct btrfs_shared_data_ref *ref2;
881 leaf = path->nodes[0];
882 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
884 if (btrfs_extent_inline_ref_type(leaf, iref) ==
885 BTRFS_EXTENT_DATA_REF_KEY) {
886 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
887 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
889 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
890 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
892 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
893 ref1 = btrfs_item_ptr(leaf, path->slots[0],
894 struct btrfs_extent_data_ref);
895 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
896 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
897 ref2 = btrfs_item_ptr(leaf, path->slots[0],
898 struct btrfs_shared_data_ref);
899 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
900 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
901 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
902 struct btrfs_extent_ref_v0 *ref0;
903 ref0 = btrfs_item_ptr(leaf, path->slots[0],
904 struct btrfs_extent_ref_v0);
905 num_refs = btrfs_ref_count_v0(leaf, ref0);
913 static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
914 struct btrfs_root *root,
915 struct btrfs_path *path,
916 u64 bytenr, u64 parent,
919 struct btrfs_key key;
922 key.objectid = bytenr;
924 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
927 key.type = BTRFS_TREE_BLOCK_REF_KEY;
928 key.offset = root_objectid;
931 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
934 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
935 if (ret == -ENOENT && parent) {
936 btrfs_release_path(root, path);
937 key.type = BTRFS_EXTENT_REF_V0_KEY;
938 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
946 static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
947 struct btrfs_root *root,
948 struct btrfs_path *path,
949 u64 bytenr, u64 parent,
952 struct btrfs_key key;
955 key.objectid = bytenr;
957 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
960 key.type = BTRFS_TREE_BLOCK_REF_KEY;
961 key.offset = root_objectid;
964 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
966 btrfs_release_path(root, path);
970 static inline int extent_ref_type(u64 parent, u64 owner)
972 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
974 return BTRFS_SHARED_BLOCK_REF_KEY;
976 return BTRFS_TREE_BLOCK_REF_KEY;
979 return BTRFS_SHARED_DATA_REF_KEY;
981 return BTRFS_EXTENT_DATA_REF_KEY;
985 static int find_next_key(struct btrfs_path *path, struct btrfs_key *key)
989 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
990 if (!path->nodes[level])
992 if (path->slots[level] + 1 >=
993 btrfs_header_nritems(path->nodes[level]))
996 btrfs_item_key_to_cpu(path->nodes[level], key,
997 path->slots[level] + 1);
999 btrfs_node_key_to_cpu(path->nodes[level], key,
1000 path->slots[level] + 1);
1006 static int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1007 struct btrfs_root *root,
1008 struct btrfs_path *path,
1009 struct btrfs_extent_inline_ref **ref_ret,
1010 u64 bytenr, u64 num_bytes,
1011 u64 parent, u64 root_objectid,
1012 u64 owner, u64 offset, int insert)
1014 struct btrfs_key key;
1015 struct extent_buffer *leaf;
1016 struct btrfs_extent_item *ei;
1017 struct btrfs_extent_inline_ref *iref;
1028 key.objectid = bytenr;
1029 key.type = BTRFS_EXTENT_ITEM_KEY;
1030 key.offset = num_bytes;
1032 want = extent_ref_type(parent, owner);
1034 extra_size = btrfs_extent_inline_ref_size(want);
1037 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1044 leaf = path->nodes[0];
1045 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1046 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1047 if (item_size < sizeof(*ei)) {
1052 ret = convert_extent_item_v0(trans, root, path, owner,
1058 leaf = path->nodes[0];
1059 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1062 BUG_ON(item_size < sizeof(*ei));
1064 if (owner < BTRFS_FIRST_FREE_OBJECTID && insert &&
1065 item_size + extra_size >= BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1070 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1071 flags = btrfs_extent_flags(leaf, ei);
1073 ptr = (unsigned long)(ei + 1);
1074 end = (unsigned long)ei + item_size;
1076 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1077 ptr += sizeof(struct btrfs_tree_block_info);
1080 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1089 iref = (struct btrfs_extent_inline_ref *)ptr;
1090 type = btrfs_extent_inline_ref_type(leaf, iref);
1094 ptr += btrfs_extent_inline_ref_size(type);
1098 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1099 struct btrfs_extent_data_ref *dref;
1100 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1101 if (match_extent_data_ref(leaf, dref, root_objectid,
1106 if (hash_extent_data_ref_item(leaf, dref) <
1107 hash_extent_data_ref(root_objectid, owner, offset))
1111 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1113 if (parent == ref_offset) {
1117 if (ref_offset < parent)
1120 if (root_objectid == ref_offset) {
1124 if (ref_offset < root_objectid)
1128 ptr += btrfs_extent_inline_ref_size(type);
1130 if (err == -ENOENT && insert) {
1131 if (item_size + extra_size >=
1132 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1137 * To add new inline back ref, we have to make sure
1138 * there is no corresponding back ref item.
1139 * For simplicity, we just do not add new inline back
1140 * ref if there is any back ref item.
1142 if (owner >= BTRFS_FIRST_FREE_OBJECTID &&
1143 find_next_key(path, &key) == 0 && key.objectid == bytenr) {
1148 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1153 static int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1154 struct btrfs_root *root,
1155 struct btrfs_path *path,
1156 struct btrfs_extent_inline_ref *iref,
1157 u64 parent, u64 root_objectid,
1158 u64 owner, u64 offset, int refs_to_add)
1160 struct extent_buffer *leaf;
1161 struct btrfs_extent_item *ei;
1164 unsigned long item_offset;
1170 leaf = path->nodes[0];
1171 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1172 item_offset = (unsigned long)iref - (unsigned long)ei;
1174 type = extent_ref_type(parent, owner);
1175 size = btrfs_extent_inline_ref_size(type);
1177 ret = btrfs_extend_item(trans, root, path, size);
1180 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1181 refs = btrfs_extent_refs(leaf, ei);
1182 refs += refs_to_add;
1183 btrfs_set_extent_refs(leaf, ei, refs);
1185 ptr = (unsigned long)ei + item_offset;
1186 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1187 if (ptr < end - size)
1188 memmove_extent_buffer(leaf, ptr + size, ptr,
1191 iref = (struct btrfs_extent_inline_ref *)ptr;
1192 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1193 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1194 struct btrfs_extent_data_ref *dref;
1195 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1196 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1197 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1198 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1199 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1200 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1201 struct btrfs_shared_data_ref *sref;
1202 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1203 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1204 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1205 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1206 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1208 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1210 btrfs_mark_buffer_dirty(leaf);
1214 static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1215 struct btrfs_root *root,
1216 struct btrfs_path *path,
1217 struct btrfs_extent_inline_ref **ref_ret,
1218 u64 bytenr, u64 num_bytes, u64 parent,
1219 u64 root_objectid, u64 owner, u64 offset)
1223 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1224 bytenr, num_bytes, parent,
1225 root_objectid, owner, offset, 0);
1229 btrfs_release_path(root, path);
1232 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1233 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1236 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1237 root_objectid, owner, offset);
1242 static int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1243 struct btrfs_root *root,
1244 struct btrfs_path *path,
1245 struct btrfs_extent_inline_ref *iref,
1248 struct extent_buffer *leaf;
1249 struct btrfs_extent_item *ei;
1250 struct btrfs_extent_data_ref *dref = NULL;
1251 struct btrfs_shared_data_ref *sref = NULL;
1260 leaf = path->nodes[0];
1261 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1262 refs = btrfs_extent_refs(leaf, ei);
1263 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1264 refs += refs_to_mod;
1265 btrfs_set_extent_refs(leaf, ei, refs);
1267 type = btrfs_extent_inline_ref_type(leaf, iref);
1269 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1270 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1271 refs = btrfs_extent_data_ref_count(leaf, dref);
1272 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1273 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1274 refs = btrfs_shared_data_ref_count(leaf, sref);
1277 BUG_ON(refs_to_mod != -1);
1280 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1281 refs += refs_to_mod;
1284 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1285 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1287 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1289 size = btrfs_extent_inline_ref_size(type);
1290 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1291 ptr = (unsigned long)iref;
1292 end = (unsigned long)ei + item_size;
1293 if (ptr + size < end)
1294 memmove_extent_buffer(leaf, ptr, ptr + size,
1297 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1300 btrfs_mark_buffer_dirty(leaf);
1304 static int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1305 struct btrfs_root *root,
1306 struct btrfs_path *path,
1307 u64 bytenr, u64 num_bytes, u64 parent,
1308 u64 root_objectid, u64 owner,
1309 u64 offset, int refs_to_add)
1311 struct btrfs_extent_inline_ref *iref;
1314 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1315 bytenr, num_bytes, parent,
1316 root_objectid, owner, offset, 1);
1318 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1319 ret = update_inline_extent_backref(trans, root, path, iref,
1321 } else if (ret == -ENOENT) {
1322 ret = setup_inline_extent_backref(trans, root, path, iref,
1323 parent, root_objectid,
1324 owner, offset, refs_to_add);
1329 static int insert_extent_backref(struct btrfs_trans_handle *trans,
1330 struct btrfs_root *root,
1331 struct btrfs_path *path,
1332 u64 bytenr, u64 parent, u64 root_objectid,
1333 u64 owner, u64 offset, int refs_to_add)
1337 if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
1338 ret = insert_extent_data_ref(trans, root, path, bytenr,
1339 parent, root_objectid,
1340 owner, offset, refs_to_add);
1342 BUG_ON(refs_to_add != 1);
1343 ret = insert_tree_block_ref(trans, root, path, bytenr,
1344 parent, root_objectid);
1349 static int remove_extent_backref(struct btrfs_trans_handle *trans,
1350 struct btrfs_root *root,
1351 struct btrfs_path *path,
1352 struct btrfs_extent_inline_ref *iref,
1353 int refs_to_drop, int is_data)
1357 BUG_ON(!is_data && refs_to_drop != 1);
1359 ret = update_inline_extent_backref(trans, root, path, iref,
1361 } else if (is_data) {
1362 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1364 ret = btrfs_del_item(trans, root, path);
1369 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1370 struct btrfs_root *root,
1371 u64 bytenr, u64 num_bytes, u64 parent,
1372 u64 root_objectid, u64 owner, u64 offset)
1374 struct btrfs_path *path;
1375 struct extent_buffer *leaf;
1376 struct btrfs_extent_item *item;
1381 path = btrfs_alloc_path();
1386 path->leave_spinning = 1;
1388 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1389 path, bytenr, num_bytes, parent,
1390 root_objectid, owner, offset, 1);
1394 if (ret != -EAGAIN) {
1399 leaf = path->nodes[0];
1400 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1401 refs = btrfs_extent_refs(leaf, item);
1402 btrfs_set_extent_refs(leaf, item, refs + 1);
1404 btrfs_mark_buffer_dirty(leaf);
1405 btrfs_release_path(root->fs_info->extent_root, path);
1408 path->leave_spinning = 1;
1410 /* now insert the actual backref */
1411 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1412 path, bytenr, parent, root_objectid,
1417 btrfs_free_path(path);
1418 finish_current_insert(trans, root->fs_info->extent_root);
1419 del_pending_extents(trans, root->fs_info->extent_root);
1424 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1425 struct btrfs_root *root)
1427 finish_current_insert(trans, root->fs_info->extent_root);
1428 del_pending_extents(trans, root->fs_info->extent_root);
1432 int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
1433 struct btrfs_root *root, u64 bytenr,
1434 u64 num_bytes, u64 *refs, u64 *flags)
1436 struct btrfs_path *path;
1438 struct btrfs_key key;
1439 struct extent_buffer *l;
1440 struct btrfs_extent_item *item;
1445 WARN_ON(num_bytes < root->sectorsize);
1446 path = btrfs_alloc_path();
1448 key.objectid = bytenr;
1449 key.offset = num_bytes;
1450 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1451 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1456 btrfs_print_leaf(root, path->nodes[0]);
1457 printk("failed to find block number %Lu\n", bytenr);
1462 item_size = btrfs_item_size_nr(l, path->slots[0]);
1463 if (item_size >= sizeof(*item)) {
1464 item = btrfs_item_ptr(l, path->slots[0],
1465 struct btrfs_extent_item);
1466 num_refs = btrfs_extent_refs(l, item);
1467 extent_flags = btrfs_extent_flags(l, item);
1469 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1470 struct btrfs_extent_item_v0 *ei0;
1471 BUG_ON(item_size != sizeof(*ei0));
1472 ei0 = btrfs_item_ptr(l, path->slots[0],
1473 struct btrfs_extent_item_v0);
1474 num_refs = btrfs_extent_refs_v0(l, ei0);
1475 /* FIXME: this isn't correct for data */
1476 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
1481 BUG_ON(num_refs == 0);
1482 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1486 *flags = extent_flags;
1488 btrfs_free_path(path);
1492 int btrfs_set_block_flags(struct btrfs_trans_handle *trans,
1493 struct btrfs_root *root,
1494 u64 bytenr, u64 num_bytes, u64 flags)
1496 struct btrfs_path *path;
1498 struct btrfs_key key;
1499 struct extent_buffer *l;
1500 struct btrfs_extent_item *item;
1503 WARN_ON(num_bytes < root->sectorsize);
1504 path = btrfs_alloc_path();
1506 key.objectid = bytenr;
1507 key.offset = num_bytes;
1508 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1509 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1514 btrfs_print_leaf(root, path->nodes[0]);
1515 printk("failed to find block number %Lu\n",
1516 (unsigned long long)bytenr);
1520 item_size = btrfs_item_size_nr(l, path->slots[0]);
1521 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1522 if (item_size < sizeof(*item)) {
1523 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1529 item_size = btrfs_item_size_nr(l, path->slots[0]);
1532 BUG_ON(item_size < sizeof(*item));
1533 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1534 flags |= btrfs_extent_flags(l, item);
1535 btrfs_set_extent_flags(l, item, flags);
1537 btrfs_free_path(path);
1538 finish_current_insert(trans, root->fs_info->extent_root);
1539 del_pending_extents(trans, root->fs_info->extent_root);
1543 static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
1544 struct btrfs_root *root,
1545 struct extent_buffer *buf,
1546 int record_parent, int inc)
1553 struct btrfs_key key;
1554 struct btrfs_file_extent_item *fi;
1559 int (*process_func)(struct btrfs_trans_handle *trans,
1560 struct btrfs_root *root,
1561 u64, u64, u64, u64, u64, u64);
1563 ref_root = btrfs_header_owner(buf);
1564 nritems = btrfs_header_nritems(buf);
1565 level = btrfs_header_level(buf);
1567 if (!root->ref_cows && level == 0)
1571 process_func = btrfs_inc_extent_ref;
1573 process_func = btrfs_free_extent;
1576 parent = buf->start;
1580 for (i = 0; i < nritems; i++) {
1583 btrfs_item_key_to_cpu(buf, &key, i);
1584 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1586 fi = btrfs_item_ptr(buf, i,
1587 struct btrfs_file_extent_item);
1588 if (btrfs_file_extent_type(buf, fi) ==
1589 BTRFS_FILE_EXTENT_INLINE)
1591 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1595 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
1596 key.offset -= btrfs_file_extent_offset(buf, fi);
1597 ret = process_func(trans, root, bytenr, num_bytes,
1598 parent, ref_root, key.objectid,
1606 bytenr = btrfs_node_blockptr(buf, i);
1607 num_bytes = btrfs_level_size(root, level - 1);
1608 ret = process_func(trans, root, bytenr, num_bytes,
1609 parent, ref_root, level - 1, 0);
1621 for (i =0; i < faili; i++) {
1624 btrfs_item_key_to_cpu(buf, &key, i);
1625 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1627 fi = btrfs_item_ptr(buf, i,
1628 struct btrfs_file_extent_item);
1629 if (btrfs_file_extent_type(buf, fi) ==
1630 BTRFS_FILE_EXTENT_INLINE)
1632 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1633 if (disk_bytenr == 0)
1635 err = btrfs_free_extent(trans, root, disk_bytenr,
1636 btrfs_file_extent_disk_num_bytes(buf,
1640 bytenr = btrfs_node_blockptr(buf, i);
1641 err = btrfs_free_extent(trans, root, bytenr,
1642 btrfs_level_size(root, level - 1), 0);
1650 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1651 struct extent_buffer *buf, int record_parent)
1653 return __btrfs_mod_ref(trans, root, buf, record_parent, 1);
1656 int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1657 struct extent_buffer *buf, int record_parent)
1659 return __btrfs_mod_ref(trans, root, buf, record_parent, 0);
1662 static int write_one_cache_group(struct btrfs_trans_handle *trans,
1663 struct btrfs_root *root,
1664 struct btrfs_path *path,
1665 struct btrfs_block_group_cache *cache)
1669 struct btrfs_root *extent_root = root->fs_info->extent_root;
1671 struct extent_buffer *leaf;
1673 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
1678 leaf = path->nodes[0];
1679 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1680 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1681 btrfs_mark_buffer_dirty(leaf);
1682 btrfs_release_path(extent_root, path);
1684 finish_current_insert(trans, extent_root);
1685 pending_ret = del_pending_extents(trans, extent_root);
1694 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1695 struct btrfs_root *root)
1697 struct extent_io_tree *block_group_cache;
1698 struct btrfs_block_group_cache *cache;
1700 struct btrfs_path *path;
1706 block_group_cache = &root->fs_info->block_group_cache;
1707 path = btrfs_alloc_path();
1712 ret = find_first_extent_bit(block_group_cache, last,
1713 &start, &end, BLOCK_GROUP_DIRTY);
1722 ret = get_state_private(block_group_cache, start, &ptr);
1725 clear_extent_bits(block_group_cache, start, end,
1726 BLOCK_GROUP_DIRTY, GFP_NOFS);
1728 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
1729 ret = write_one_cache_group(trans, root, path, cache);
1732 btrfs_free_path(path);
1736 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
1739 struct list_head *head = &info->space_info;
1740 struct list_head *cur;
1741 struct btrfs_space_info *found;
1742 list_for_each(cur, head) {
1743 found = list_entry(cur, struct btrfs_space_info, list);
1744 if (found->flags == flags)
1751 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1752 u64 total_bytes, u64 bytes_used,
1753 struct btrfs_space_info **space_info)
1755 struct btrfs_space_info *found;
1757 found = __find_space_info(info, flags);
1759 found->total_bytes += total_bytes;
1760 found->bytes_used += bytes_used;
1761 WARN_ON(found->total_bytes < found->bytes_used);
1762 *space_info = found;
1765 found = kmalloc(sizeof(*found), GFP_NOFS);
1769 list_add(&found->list, &info->space_info);
1770 found->flags = flags;
1771 found->total_bytes = total_bytes;
1772 found->bytes_used = bytes_used;
1773 found->bytes_pinned = 0;
1775 *space_info = found;
1780 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1782 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
1783 BTRFS_BLOCK_GROUP_RAID1 |
1784 BTRFS_BLOCK_GROUP_DUP);
1786 if (flags & BTRFS_BLOCK_GROUP_DATA)
1787 fs_info->avail_data_alloc_bits |= extra_flags;
1788 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1789 fs_info->avail_metadata_alloc_bits |= extra_flags;
1790 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1791 fs_info->avail_system_alloc_bits |= extra_flags;
1795 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1796 struct btrfs_root *extent_root, u64 alloc_bytes,
1799 struct btrfs_space_info *space_info;
1805 space_info = __find_space_info(extent_root->fs_info, flags);
1807 ret = update_space_info(extent_root->fs_info, flags,
1811 BUG_ON(!space_info);
1813 if (space_info->full)
1816 thresh = div_factor(space_info->total_bytes, 7);
1817 if ((space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) <
1821 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
1822 if (ret == -ENOSPC) {
1823 space_info->full = 1;
1829 ret = btrfs_make_block_group(trans, extent_root, 0, flags,
1830 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
1835 static int update_block_group(struct btrfs_trans_handle *trans,
1836 struct btrfs_root *root,
1837 u64 bytenr, u64 num_bytes, int alloc,
1840 struct btrfs_block_group_cache *cache;
1841 struct btrfs_fs_info *info = root->fs_info;
1842 u64 total = num_bytes;
1848 /* block accounting for super block */
1849 old_val = btrfs_super_bytes_used(&info->super_copy);
1851 old_val += num_bytes;
1853 old_val -= num_bytes;
1854 btrfs_set_super_bytes_used(&info->super_copy, old_val);
1856 /* block accounting for root item */
1857 old_val = btrfs_root_used(&root->root_item);
1859 old_val += num_bytes;
1861 old_val -= num_bytes;
1862 btrfs_set_root_used(&root->root_item, old_val);
1865 cache = btrfs_lookup_block_group(info, bytenr);
1869 byte_in_group = bytenr - cache->key.objectid;
1870 WARN_ON(byte_in_group > cache->key.offset);
1871 start = cache->key.objectid;
1872 end = start + cache->key.offset - 1;
1873 set_extent_bits(&info->block_group_cache, start, end,
1874 BLOCK_GROUP_DIRTY, GFP_NOFS);
1876 old_val = btrfs_block_group_used(&cache->item);
1877 num_bytes = min(total, cache->key.offset - byte_in_group);
1879 old_val += num_bytes;
1880 cache->space_info->bytes_used += num_bytes;
1882 old_val -= num_bytes;
1883 cache->space_info->bytes_used -= num_bytes;
1885 set_extent_dirty(&info->free_space_cache,
1886 bytenr, bytenr + num_bytes - 1,
1890 btrfs_set_block_group_used(&cache->item, old_val);
1892 bytenr += num_bytes;
1897 static int update_pinned_extents(struct btrfs_root *root,
1898 u64 bytenr, u64 num, int pin)
1901 struct btrfs_block_group_cache *cache;
1902 struct btrfs_fs_info *fs_info = root->fs_info;
1905 set_extent_dirty(&fs_info->pinned_extents,
1906 bytenr, bytenr + num - 1, GFP_NOFS);
1908 clear_extent_dirty(&fs_info->pinned_extents,
1909 bytenr, bytenr + num - 1, GFP_NOFS);
1912 cache = btrfs_lookup_block_group(fs_info, bytenr);
1914 len = min(num, cache->key.offset -
1915 (bytenr - cache->key.objectid));
1917 cache->pinned += len;
1918 cache->space_info->bytes_pinned += len;
1919 fs_info->total_pinned += len;
1921 cache->pinned -= len;
1922 cache->space_info->bytes_pinned -= len;
1923 fs_info->total_pinned -= len;
1931 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
1936 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
1940 ret = find_first_extent_bit(pinned_extents, last,
1941 &start, &end, EXTENT_DIRTY);
1944 set_extent_dirty(copy, start, end, GFP_NOFS);
1950 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1951 struct btrfs_root *root,
1952 struct extent_io_tree *unpin)
1957 struct extent_io_tree *free_space_cache;
1958 free_space_cache = &root->fs_info->free_space_cache;
1961 ret = find_first_extent_bit(unpin, 0, &start, &end,
1965 update_pinned_extents(root, start, end + 1 - start, 0);
1966 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1967 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
1972 static int finish_current_insert(struct btrfs_trans_handle *trans,
1973 struct btrfs_root *extent_root)
1978 struct btrfs_fs_info *info = extent_root->fs_info;
1979 struct btrfs_path *path;
1980 struct pending_extent_op *extent_op;
1981 struct btrfs_key key;
1984 path = btrfs_alloc_path();
1987 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1988 &end, EXTENT_LOCKED);
1992 ret = get_state_private(&info->extent_ins, start, &priv);
1994 extent_op = (struct pending_extent_op *)(unsigned long)priv;
1996 if (extent_op->type == PENDING_EXTENT_INSERT) {
1997 key.objectid = start;
1998 key.offset = end + 1 - start;
1999 key.type = BTRFS_EXTENT_ITEM_KEY;
2000 ret = alloc_reserved_tree_block(trans, extent_root,
2001 extent_root->root_key.objectid,
2005 extent_op->level, &key);
2010 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
2014 btrfs_free_path(path);
2018 static int pin_down_bytes(struct btrfs_trans_handle *trans,
2019 struct btrfs_root *root,
2020 u64 bytenr, u64 num_bytes, int is_data)
2023 struct extent_buffer *buf;
2028 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2032 /* we can reuse a block if it hasn't been written
2033 * and it is from this transaction. We can't
2034 * reuse anything from the tree log root because
2035 * it has tiny sub-transactions.
2037 if (btrfs_buffer_uptodate(buf, 0)) {
2038 u64 header_owner = btrfs_header_owner(buf);
2039 u64 header_transid = btrfs_header_generation(buf);
2040 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
2041 header_transid == trans->transid &&
2042 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2043 clean_tree_block(NULL, root, buf);
2044 free_extent_buffer(buf);
2048 free_extent_buffer(buf);
2050 update_pinned_extents(root, bytenr, num_bytes, 1);
2057 * remove an extent from the root, returns 0 on success
2059 static int __free_extent(struct btrfs_trans_handle *trans,
2060 struct btrfs_root *root,
2061 u64 bytenr, u64 num_bytes, u64 parent,
2062 u64 root_objectid, u64 owner_objectid,
2063 u64 owner_offset, int refs_to_drop)
2066 struct btrfs_key key;
2067 struct btrfs_path *path;
2068 struct btrfs_extent_ops *ops = root->fs_info->extent_ops;
2069 struct btrfs_root *extent_root = root->fs_info->extent_root;
2070 struct extent_buffer *leaf;
2071 struct btrfs_extent_item *ei;
2072 struct btrfs_extent_inline_ref *iref;
2075 int extent_slot = 0;
2076 int found_extent = 0;
2081 path = btrfs_alloc_path();
2086 path->leave_spinning = 1;
2088 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
2089 BUG_ON(!is_data && refs_to_drop != 1);
2091 ret = lookup_extent_backref(trans, extent_root, path, &iref,
2092 bytenr, num_bytes, parent,
2093 root_objectid, owner_objectid,
2096 extent_slot = path->slots[0];
2097 while (extent_slot >= 0) {
2098 btrfs_item_key_to_cpu(path->nodes[0], &key,
2100 if (key.objectid != bytenr)
2102 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
2103 key.offset == num_bytes) {
2107 if (path->slots[0] - extent_slot > 5)
2111 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2112 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
2113 if (found_extent && item_size < sizeof(*ei))
2116 if (!found_extent) {
2118 ret = remove_extent_backref(trans, extent_root, path,
2122 btrfs_release_path(extent_root, path);
2123 path->leave_spinning = 1;
2125 key.objectid = bytenr;
2126 key.type = BTRFS_EXTENT_ITEM_KEY;
2127 key.offset = num_bytes;
2129 ret = btrfs_search_slot(trans, extent_root,
2132 printk(KERN_ERR "umm, got %d back from search"
2133 ", was looking for %llu\n", ret,
2134 (unsigned long long)bytenr);
2135 btrfs_print_leaf(extent_root, path->nodes[0]);
2138 extent_slot = path->slots[0];
2141 btrfs_print_leaf(extent_root, path->nodes[0]);
2143 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
2144 "parent %llu root %llu owner %llu offset %llu\n",
2145 (unsigned long long)bytenr,
2146 (unsigned long long)parent,
2147 (unsigned long long)root_objectid,
2148 (unsigned long long)owner_objectid,
2149 (unsigned long long)owner_offset);
2152 leaf = path->nodes[0];
2153 item_size = btrfs_item_size_nr(leaf, extent_slot);
2154 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2155 if (item_size < sizeof(*ei)) {
2156 BUG_ON(found_extent || extent_slot != path->slots[0]);
2157 ret = convert_extent_item_v0(trans, extent_root, path,
2161 btrfs_release_path(extent_root, path);
2162 path->leave_spinning = 1;
2164 key.objectid = bytenr;
2165 key.type = BTRFS_EXTENT_ITEM_KEY;
2166 key.offset = num_bytes;
2168 ret = btrfs_search_slot(trans, extent_root, &key, path,
2171 printk(KERN_ERR "umm, got %d back from search"
2172 ", was looking for %llu\n", ret,
2173 (unsigned long long)bytenr);
2174 btrfs_print_leaf(extent_root, path->nodes[0]);
2177 extent_slot = path->slots[0];
2178 leaf = path->nodes[0];
2179 item_size = btrfs_item_size_nr(leaf, extent_slot);
2182 BUG_ON(item_size < sizeof(*ei));
2183 ei = btrfs_item_ptr(leaf, extent_slot,
2184 struct btrfs_extent_item);
2185 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2186 struct btrfs_tree_block_info *bi;
2187 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
2188 bi = (struct btrfs_tree_block_info *)(ei + 1);
2189 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
2192 refs = btrfs_extent_refs(leaf, ei);
2193 BUG_ON(refs < refs_to_drop);
2194 refs -= refs_to_drop;
2198 * In the case of inline back ref, reference count will
2199 * be updated by remove_extent_backref
2202 BUG_ON(!found_extent);
2204 btrfs_set_extent_refs(leaf, ei, refs);
2205 btrfs_mark_buffer_dirty(leaf);
2208 ret = remove_extent_backref(trans, extent_root, path,
2218 BUG_ON(is_data && refs_to_drop !=
2219 extent_data_ref_count(root, path, iref));
2221 BUG_ON(path->slots[0] != extent_slot);
2223 BUG_ON(path->slots[0] != extent_slot + 1);
2224 path->slots[0] = extent_slot;
2229 if (ops && ops->free_extent) {
2230 ret = ops->free_extent(root, bytenr, num_bytes);
2238 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
2245 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2248 btrfs_release_path(extent_root, path);
2251 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
2255 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
2259 btrfs_free_path(path);
2260 finish_current_insert(trans, extent_root);
2265 * find all the blocks marked as pending in the radix tree and remove
2266 * them from the extent map
2268 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
2269 btrfs_root *extent_root)
2276 struct extent_io_tree *pending_del;
2277 struct extent_io_tree *extent_ins;
2278 struct pending_extent_op *extent_op;
2280 extent_ins = &extent_root->fs_info->extent_ins;
2281 pending_del = &extent_root->fs_info->pending_del;
2284 ret = find_first_extent_bit(pending_del, 0, &start, &end,
2289 ret = get_state_private(pending_del, start, &priv);
2291 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2293 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
2296 if (!test_range_bit(extent_ins, start, end,
2297 EXTENT_LOCKED, 0)) {
2298 ret = __free_extent(trans, extent_root,
2299 start, end + 1 - start, 0,
2300 extent_root->root_key.objectid,
2301 extent_op->level, 0, 1);
2305 ret = get_state_private(extent_ins, start, &priv);
2307 extent_op = (struct pending_extent_op *)
2308 (unsigned long)priv;
2310 clear_extent_bits(extent_ins, start, end,
2311 EXTENT_LOCKED, GFP_NOFS);
2313 if (extent_op->type == PENDING_BACKREF_UPDATE)
2325 * remove an extent from the root, returns 0 on success
2328 int btrfs_free_extent(struct btrfs_trans_handle *trans,
2329 struct btrfs_root *root,
2330 u64 bytenr, u64 num_bytes, u64 parent,
2331 u64 root_objectid, u64 owner, u64 offset)
2333 struct btrfs_root *extent_root = root->fs_info->extent_root;
2337 WARN_ON(num_bytes < root->sectorsize);
2338 if (root == extent_root) {
2339 struct pending_extent_op *extent_op;
2341 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2344 extent_op->type = PENDING_EXTENT_DELETE;
2345 extent_op->bytenr = bytenr;
2346 extent_op->num_bytes = num_bytes;
2347 extent_op->level = (int)owner;
2349 set_extent_bits(&root->fs_info->pending_del,
2350 bytenr, bytenr + num_bytes - 1,
2351 EXTENT_LOCKED, GFP_NOFS);
2352 set_state_private(&root->fs_info->pending_del,
2353 bytenr, (unsigned long)extent_op);
2356 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
2357 root_objectid, owner, offset, 1);
2358 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
2359 return ret ? ret : pending_ret;
2362 static u64 stripe_align(struct btrfs_root *root, u64 val)
2364 u64 mask = ((u64)root->stripesize - 1);
2365 u64 ret = (val + mask) & ~mask;
2370 * walks the btree of allocated extents and find a hole of a given size.
2371 * The key ins is changed to record the hole:
2372 * ins->objectid == block start
2373 * ins->flags = BTRFS_EXTENT_ITEM_KEY
2374 * ins->offset == number of blocks
2375 * Any available blocks before search_start are skipped.
2377 static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2378 struct btrfs_root *orig_root,
2379 u64 num_bytes, u64 empty_size,
2380 u64 search_start, u64 search_end,
2381 u64 hint_byte, struct btrfs_key *ins,
2382 u64 exclude_start, u64 exclude_nr,
2386 u64 orig_search_start = search_start;
2387 struct btrfs_root * root = orig_root->fs_info->extent_root;
2388 struct btrfs_fs_info *info = root->fs_info;
2389 u64 total_needed = num_bytes;
2390 struct btrfs_block_group_cache *block_group;
2394 WARN_ON(num_bytes < root->sectorsize);
2395 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
2398 block_group = btrfs_lookup_first_block_group(info, hint_byte);
2400 hint_byte = search_start;
2401 block_group = btrfs_find_block_group(root, block_group,
2402 hint_byte, data, 1);
2404 block_group = btrfs_find_block_group(root,
2406 search_start, data, 1);
2409 total_needed += empty_size;
2413 block_group = btrfs_lookup_first_block_group(info,
2416 block_group = btrfs_lookup_first_block_group(info,
2419 ret = find_search_start(root, &block_group, &search_start,
2420 total_needed, data);
2424 search_start = stripe_align(root, search_start);
2425 ins->objectid = search_start;
2426 ins->offset = num_bytes;
2428 if (ins->objectid + num_bytes >
2429 block_group->key.objectid + block_group->key.offset) {
2430 search_start = block_group->key.objectid +
2431 block_group->key.offset;
2435 if (test_range_bit(&info->extent_ins, ins->objectid,
2436 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
2437 search_start = ins->objectid + num_bytes;
2441 if (test_range_bit(&info->pinned_extents, ins->objectid,
2442 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
2443 search_start = ins->objectid + num_bytes;
2447 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
2448 ins->objectid < exclude_start + exclude_nr)) {
2449 search_start = exclude_start + exclude_nr;
2453 if (!(data & BTRFS_BLOCK_GROUP_DATA)) {
2454 block_group = btrfs_lookup_block_group(info, ins->objectid);
2456 trans->block_group = block_group;
2458 ins->offset = num_bytes;
2462 block_group = btrfs_lookup_first_block_group(info, search_start);
2464 search_start = orig_search_start;
2471 total_needed -= empty_size;
2477 block_group = btrfs_find_block_group(root, block_group,
2478 search_start, data, 0);
2485 static int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2486 struct btrfs_root *root,
2487 u64 num_bytes, u64 empty_size,
2488 u64 hint_byte, u64 search_end,
2489 struct btrfs_key *ins, int data)
2492 u64 search_start = 0;
2494 struct btrfs_fs_info *info = root->fs_info;
2496 if (info->extent_ops) {
2497 struct btrfs_extent_ops *ops = info->extent_ops;
2498 ret = ops->alloc_extent(root, num_bytes, hint_byte, ins);
2504 alloc_profile = info->avail_data_alloc_bits &
2505 info->data_alloc_profile;
2506 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
2507 } else if ((info->system_allocs > 0 || root == info->chunk_root) &&
2508 info->system_allocs >= 0) {
2509 alloc_profile = info->avail_system_alloc_bits &
2510 info->system_alloc_profile;
2511 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
2513 alloc_profile = info->avail_metadata_alloc_bits &
2514 info->metadata_alloc_profile;
2515 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
2518 if (root->ref_cows) {
2519 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
2520 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2522 BTRFS_BLOCK_GROUP_METADATA);
2525 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2526 num_bytes + 2 * 1024 * 1024, data);
2530 WARN_ON(num_bytes < root->sectorsize);
2531 ret = find_free_extent(trans, root, num_bytes, empty_size,
2532 search_start, search_end, hint_byte, ins,
2533 trans->alloc_exclude_start,
2534 trans->alloc_exclude_nr, data);
2537 clear_extent_dirty(&root->fs_info->free_space_cache,
2538 ins->objectid, ins->objectid + ins->offset - 1,
2543 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
2544 struct btrfs_root *root,
2545 u64 root_objectid, u64 generation,
2546 u64 flags, struct btrfs_disk_key *key,
2547 int level, struct btrfs_key *ins)
2550 struct btrfs_fs_info *fs_info = root->fs_info;
2551 struct btrfs_extent_item *extent_item;
2552 struct btrfs_tree_block_info *block_info;
2553 struct btrfs_extent_inline_ref *iref;
2554 struct btrfs_path *path;
2555 struct extent_buffer *leaf;
2556 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
2558 path = btrfs_alloc_path();
2561 path->leave_spinning = 1;
2562 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
2566 leaf = path->nodes[0];
2567 extent_item = btrfs_item_ptr(leaf, path->slots[0],
2568 struct btrfs_extent_item);
2569 btrfs_set_extent_refs(leaf, extent_item, 1);
2570 btrfs_set_extent_generation(leaf, extent_item, generation);
2571 btrfs_set_extent_flags(leaf, extent_item,
2572 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
2573 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
2575 btrfs_set_tree_block_key(leaf, block_info, key);
2576 btrfs_set_tree_block_level(leaf, block_info, level);
2578 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
2579 btrfs_set_extent_inline_ref_type(leaf, iref, BTRFS_TREE_BLOCK_REF_KEY);
2580 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
2582 btrfs_mark_buffer_dirty(leaf);
2583 btrfs_free_path(path);
2585 ret = update_block_group(trans, root, ins->objectid, ins->offset,
2588 printk(KERN_ERR "btrfs update block group failed for %llu "
2589 "%llu\n", (unsigned long long)ins->objectid,
2590 (unsigned long long)ins->offset);
2596 static int alloc_tree_block(struct btrfs_trans_handle *trans,
2597 struct btrfs_root *root, u64 num_bytes,
2598 u64 root_objectid, u64 generation,
2599 u64 flags, struct btrfs_disk_key *key,
2600 int level, u64 empty_size, u64 hint_byte,
2601 u64 search_end, struct btrfs_key *ins)
2604 ret = btrfs_reserve_extent(trans, root, num_bytes, empty_size,
2605 hint_byte, search_end, ins, 0);
2608 if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID) {
2609 struct pending_extent_op *extent_op;
2611 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2614 extent_op->type = PENDING_EXTENT_INSERT;
2615 extent_op->bytenr = ins->objectid;
2616 extent_op->num_bytes = ins->offset;
2617 extent_op->level = level;
2618 extent_op->flags = flags;
2619 memcpy(&extent_op->key, key, sizeof(*key));
2621 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
2622 ins->objectid + ins->offset - 1,
2623 EXTENT_LOCKED, GFP_NOFS);
2624 set_state_private(&root->fs_info->extent_ins,
2625 ins->objectid, (unsigned long)extent_op);
2627 ret = alloc_reserved_tree_block(trans, root, root_objectid,
2630 finish_current_insert(trans, root->fs_info->extent_root);
2631 del_pending_extents(trans, root->fs_info->extent_root);
2637 * helper function to allocate a block for a given tree
2638 * returns the tree buffer or NULL.
2640 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
2641 struct btrfs_root *root,
2642 u32 blocksize, u64 root_objectid,
2643 struct btrfs_disk_key *key, int level,
2644 u64 hint, u64 empty_size)
2646 struct btrfs_key ins;
2648 struct extent_buffer *buf;
2650 ret = alloc_tree_block(trans, root, blocksize, root_objectid,
2651 trans->transid, 0, key, level,
2652 empty_size, hint, (u64)-1, &ins);
2655 return ERR_PTR(ret);
2658 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
2660 btrfs_free_extent(trans, root, ins.objectid, ins.offset,
2661 0, root->root_key.objectid, level, 0);
2663 return ERR_PTR(-ENOMEM);
2665 btrfs_set_buffer_uptodate(buf);
2666 trans->blocks_used++;
2673 static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
2674 struct btrfs_root *root,
2675 struct extent_buffer *leaf)
2678 u64 leaf_generation;
2679 struct btrfs_key key;
2680 struct btrfs_file_extent_item *fi;
2685 BUG_ON(!btrfs_is_leaf(leaf));
2686 nritems = btrfs_header_nritems(leaf);
2687 leaf_owner = btrfs_header_owner(leaf);
2688 leaf_generation = btrfs_header_generation(leaf);
2690 for (i = 0; i < nritems; i++) {
2693 btrfs_item_key_to_cpu(leaf, &key, i);
2694 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2696 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
2697 if (btrfs_file_extent_type(leaf, fi) ==
2698 BTRFS_FILE_EXTENT_INLINE)
2701 * FIXME make sure to insert a trans record that
2702 * repeats the snapshot del on crash
2704 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2705 if (disk_bytenr == 0)
2707 ret = btrfs_free_extent(trans, root, disk_bytenr,
2708 btrfs_file_extent_disk_num_bytes(leaf, fi),
2709 leaf->start, leaf_owner, leaf_generation,
2716 static void noinline reada_walk_down(struct btrfs_root *root,
2717 struct extent_buffer *node,
2730 nritems = btrfs_header_nritems(node);
2731 level = btrfs_header_level(node);
2735 for (i = slot; i < nritems && skipped < 32; i++) {
2736 bytenr = btrfs_node_blockptr(node, i);
2737 if (last && ((bytenr > last && bytenr - last > 32 * 1024) ||
2738 (last > bytenr && last - bytenr > 32 * 1024))) {
2742 blocksize = btrfs_level_size(root, level - 1);
2744 ret = btrfs_lookup_extent_ref(NULL, root, bytenr,
2752 mutex_unlock(&root->fs_info->fs_mutex);
2753 ret = readahead_tree_block(root, bytenr, blocksize,
2754 btrfs_node_ptr_generation(node, i));
2755 last = bytenr + blocksize;
2757 mutex_lock(&root->fs_info->fs_mutex);
2764 * helper function for drop_snapshot, this walks down the tree dropping ref
2765 * counts as it goes.
2767 static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2768 struct btrfs_root *root,
2769 struct btrfs_path *path, int *level)
2775 struct extent_buffer *next;
2776 struct extent_buffer *cur;
2777 struct extent_buffer *parent;
2782 WARN_ON(*level < 0);
2783 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2784 ret = btrfs_lookup_extent_ref(trans, root,
2785 path->nodes[*level]->start,
2786 path->nodes[*level]->len, &refs);
2792 * walk down to the last node level and free all the leaves
2794 while(*level >= 0) {
2795 WARN_ON(*level < 0);
2796 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2797 cur = path->nodes[*level];
2799 if (btrfs_header_level(cur) != *level)
2802 if (path->slots[*level] >=
2803 btrfs_header_nritems(cur))
2806 ret = drop_leaf_ref(trans, root, cur);
2810 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2811 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2812 blocksize = btrfs_level_size(root, *level - 1);
2813 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
2817 parent = path->nodes[*level];
2818 root_owner = btrfs_header_owner(parent);
2819 root_gen = btrfs_header_generation(parent);
2820 path->slots[*level]++;
2821 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
2822 parent->start, root_owner,
2823 root_gen, *level - 1, 1);
2827 next = btrfs_find_tree_block(root, bytenr, blocksize);
2828 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
2829 free_extent_buffer(next);
2830 reada_walk_down(root, cur, path->slots[*level]);
2831 mutex_unlock(&root->fs_info->fs_mutex);
2832 next = read_tree_block(root, bytenr, blocksize,
2834 mutex_lock(&root->fs_info->fs_mutex);
2836 WARN_ON(*level <= 0);
2837 if (path->nodes[*level-1])
2838 free_extent_buffer(path->nodes[*level-1]);
2839 path->nodes[*level-1] = next;
2840 *level = btrfs_header_level(next);
2841 path->slots[*level] = 0;
2844 WARN_ON(*level < 0);
2845 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2847 if (path->nodes[*level] == root->node) {
2848 root_owner = root->root_key.objectid;
2849 parent = path->nodes[*level];
2851 parent = path->nodes[*level + 1];
2852 root_owner = btrfs_header_owner(parent);
2855 root_gen = btrfs_header_generation(parent);
2856 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
2857 path->nodes[*level]->len, parent->start,
2858 root_owner, root_gen, *level, 1);
2859 free_extent_buffer(path->nodes[*level]);
2860 path->nodes[*level] = NULL;
2867 * helper for dropping snapshots. This walks back up the tree in the path
2868 * to find the first node higher up where we haven't yet gone through
2871 static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
2872 struct btrfs_root *root,
2873 struct btrfs_path *path, int *level)
2877 struct btrfs_root_item *root_item = &root->root_item;
2882 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
2883 slot = path->slots[i];
2884 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
2885 struct extent_buffer *node;
2886 struct btrfs_disk_key disk_key;
2887 node = path->nodes[i];
2890 WARN_ON(*level == 0);
2891 btrfs_node_key(node, &disk_key, path->slots[i]);
2892 memcpy(&root_item->drop_progress,
2893 &disk_key, sizeof(disk_key));
2894 root_item->drop_level = i;
2897 struct extent_buffer *parent;
2898 if (path->nodes[*level] == root->node)
2899 parent = path->nodes[*level];
2901 parent = path->nodes[*level + 1];
2903 root_owner = btrfs_header_owner(parent);
2904 root_gen = btrfs_header_generation(parent);
2905 ret = btrfs_free_extent(trans, root,
2906 path->nodes[*level]->start,
2907 path->nodes[*level]->len,
2908 parent->start, root_owner,
2909 root_gen, *level, 1);
2911 free_extent_buffer(path->nodes[*level]);
2912 path->nodes[*level] = NULL;
2920 * drop the reference count on the tree rooted at 'snap'. This traverses
2921 * the tree freeing any blocks that have a ref count of zero after being
2924 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
2930 struct btrfs_path *path;
2933 struct btrfs_root_item *root_item = &root->root_item;
2935 path = btrfs_alloc_path();
2938 level = btrfs_header_level(root->node);
2940 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2941 path->nodes[level] = root->node;
2942 extent_buffer_get(root->node);
2943 path->slots[level] = 0;
2945 struct btrfs_key key;
2946 struct btrfs_disk_key found_key;
2947 struct extent_buffer *node;
2949 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
2950 level = root_item->drop_level;
2951 path->lowest_level = level;
2952 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2957 node = path->nodes[level];
2958 btrfs_node_key(node, &found_key, path->slots[level]);
2959 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2960 sizeof(found_key)));
2963 wret = walk_down_tree(trans, root, path, &level);
2969 wret = walk_up_tree(trans, root, path, &level);
2979 for (i = 0; i <= orig_level; i++) {
2980 if (path->nodes[i]) {
2981 free_extent_buffer(path->nodes[i]);
2982 path->nodes[i] = NULL;
2986 btrfs_free_path(path);
2992 int btrfs_free_block_groups(struct btrfs_fs_info *info)
2999 ret = find_first_extent_bit(&info->block_group_cache, 0,
3000 &start, &end, (unsigned int)-1);
3003 ret = get_state_private(&info->block_group_cache, start, &ptr);
3005 kfree((void *)(unsigned long)ptr);
3006 clear_extent_bits(&info->block_group_cache, start,
3007 end, (unsigned int)-1, GFP_NOFS);
3010 ret = find_first_extent_bit(&info->free_space_cache, 0,
3011 &start, &end, EXTENT_DIRTY);
3014 clear_extent_dirty(&info->free_space_cache, start,
3020 int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
3021 struct btrfs_key *key)
3024 struct btrfs_key found_key;
3025 struct extent_buffer *leaf;
3028 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
3032 slot = path->slots[0];
3033 leaf = path->nodes[0];
3034 if (slot >= btrfs_header_nritems(leaf)) {
3035 ret = btrfs_next_leaf(root, path);
3042 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3044 if (found_key.objectid >= key->objectid &&
3045 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
3054 int btrfs_read_block_groups(struct btrfs_root *root)
3056 struct btrfs_path *path;
3059 struct btrfs_block_group_cache *cache;
3060 struct btrfs_fs_info *info = root->fs_info;
3061 struct btrfs_space_info *space_info;
3062 struct extent_io_tree *block_group_cache;
3063 struct btrfs_key key;
3064 struct btrfs_key found_key;
3065 struct extent_buffer *leaf;
3067 block_group_cache = &info->block_group_cache;
3069 root = info->extent_root;
3072 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3073 path = btrfs_alloc_path();
3078 ret = find_first_block_group(root, path, &key);
3086 leaf = path->nodes[0];
3087 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3088 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3094 read_extent_buffer(leaf, &cache->item,
3095 btrfs_item_ptr_offset(leaf, path->slots[0]),
3096 sizeof(cache->item));
3097 memcpy(&cache->key, &found_key, sizeof(found_key));
3100 key.objectid = found_key.objectid + found_key.offset;
3101 btrfs_release_path(root, path);
3102 cache->flags = btrfs_block_group_flags(&cache->item);
3104 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
3105 bit = BLOCK_GROUP_DATA;
3106 } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
3107 bit = BLOCK_GROUP_SYSTEM;
3108 } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
3109 bit = BLOCK_GROUP_METADATA;
3111 set_avail_alloc_bits(info, cache->flags);
3112 if (btrfs_chunk_readonly(root, cache->key.objectid))
3115 ret = update_space_info(info, cache->flags, found_key.offset,
3116 btrfs_block_group_used(&cache->item),
3119 cache->space_info = space_info;
3121 /* use EXTENT_LOCKED to prevent merging */
3122 set_extent_bits(block_group_cache, found_key.objectid,
3123 found_key.objectid + found_key.offset - 1,
3124 bit | EXTENT_LOCKED, GFP_NOFS);
3125 set_state_private(block_group_cache, found_key.objectid,
3126 (unsigned long)cache);
3130 btrfs_free_path(path);
3134 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
3135 struct btrfs_root *root, u64 bytes_used,
3136 u64 type, u64 chunk_objectid, u64 chunk_offset,
3141 struct btrfs_root *extent_root;
3142 struct btrfs_block_group_cache *cache;
3143 struct extent_io_tree *block_group_cache;
3145 extent_root = root->fs_info->extent_root;
3146 block_group_cache = &root->fs_info->block_group_cache;
3148 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3150 cache->key.objectid = chunk_offset;
3151 cache->key.offset = size;
3153 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3154 btrfs_set_block_group_used(&cache->item, bytes_used);
3155 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
3156 cache->flags = type;
3157 btrfs_set_block_group_flags(&cache->item, type);
3159 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
3160 &cache->space_info);
3163 bit = block_group_state_bits(type);
3164 set_extent_bits(block_group_cache, chunk_offset,
3165 chunk_offset + size - 1,
3166 bit | EXTENT_LOCKED, GFP_NOFS);
3168 set_state_private(block_group_cache, chunk_offset,
3169 (unsigned long)cache);
3170 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3171 sizeof(cache->item));
3174 finish_current_insert(trans, extent_root);
3175 ret = del_pending_extents(trans, extent_root);
3177 set_avail_alloc_bits(extent_root->fs_info, type);
3182 * This is for converter use only.
3184 * In that case, we don't know where are free blocks located.
3185 * Therefore all block group cache entries must be setup properly
3186 * before doing any block allocation.
3188 int btrfs_make_block_groups(struct btrfs_trans_handle *trans,
3189 struct btrfs_root *root)
3197 u64 total_metadata = 0;
3201 struct btrfs_root *extent_root;
3202 struct btrfs_block_group_cache *cache;
3203 struct extent_io_tree *block_group_cache;
3205 extent_root = root->fs_info->extent_root;
3206 block_group_cache = &root->fs_info->block_group_cache;
3207 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3208 total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
3209 group_align = 64 * root->sectorsize;
3212 while (cur_start < total_bytes) {
3213 group_size = total_bytes / 12;
3214 group_size = min_t(u64, group_size, total_bytes - cur_start);
3215 if (cur_start == 0) {
3216 bit = BLOCK_GROUP_SYSTEM;
3217 group_type = BTRFS_BLOCK_GROUP_SYSTEM;
3219 group_size &= ~(group_align - 1);
3220 group_size = max_t(u64, group_size, 8 * 1024 * 1024);
3221 group_size = min_t(u64, group_size, 32 * 1024 * 1024);
3223 group_size &= ~(group_align - 1);
3224 if (total_data >= total_metadata * 2) {
3225 group_type = BTRFS_BLOCK_GROUP_METADATA;
3226 group_size = min_t(u64, group_size,
3227 1ULL * 1024 * 1024 * 1024);
3228 total_metadata += group_size;
3230 group_type = BTRFS_BLOCK_GROUP_DATA;
3231 group_size = min_t(u64, group_size,
3232 5ULL * 1024 * 1024 * 1024);
3233 total_data += group_size;
3235 if ((total_bytes - cur_start) * 4 < group_size * 5)
3236 group_size = total_bytes - cur_start;
3239 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3242 cache->key.objectid = cur_start;
3243 cache->key.offset = group_size;
3244 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3246 btrfs_set_block_group_used(&cache->item, 0);
3247 btrfs_set_block_group_chunk_objectid(&cache->item,
3249 btrfs_set_block_group_flags(&cache->item, group_type);
3251 cache->flags = group_type;
3253 ret = update_space_info(root->fs_info, group_type, group_size,
3254 0, &cache->space_info);
3256 set_avail_alloc_bits(extent_root->fs_info, group_type);
3258 set_extent_bits(block_group_cache, cur_start,
3259 cur_start + group_size - 1,
3260 bit | EXTENT_LOCKED, GFP_NOFS);
3261 set_state_private(block_group_cache, cur_start,
3262 (unsigned long)cache);
3263 cur_start += group_size;
3265 /* then insert all the items */
3267 while(cur_start < total_bytes) {
3268 cache = btrfs_lookup_block_group(root->fs_info, cur_start);
3271 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3272 sizeof(cache->item));
3275 finish_current_insert(trans, extent_root);
3276 ret = del_pending_extents(trans, extent_root);
3279 cur_start = cache->key.objectid + cache->key.offset;
3284 int btrfs_update_block_group(struct btrfs_trans_handle *trans,
3285 struct btrfs_root *root,
3286 u64 bytenr, u64 num_bytes, int alloc,
3289 return update_block_group(trans, root, bytenr, num_bytes,