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"
29 #include "free-space-cache.h"
31 #define BLOCK_GROUP_DATA EXTENT_WRITEBACK
32 #define BLOCK_GROUP_METADATA EXTENT_UPTODATE
33 #define BLOCK_GROUP_SYSTEM EXTENT_NEW
35 #define BLOCK_GROUP_DIRTY EXTENT_DIRTY
37 #define PENDING_EXTENT_INSERT 0
38 #define PENDING_EXTENT_DELETE 1
39 #define PENDING_BACKREF_UPDATE 2
41 struct pending_extent_op {
46 struct btrfs_disk_key key;
50 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
51 struct btrfs_root *root,
52 u64 root_objectid, u64 generation,
53 u64 flags, struct btrfs_disk_key *key,
54 int level, struct btrfs_key *ins);
55 static int __free_extent(struct btrfs_trans_handle *trans,
56 struct btrfs_root *root,
57 u64 bytenr, u64 num_bytes, u64 parent,
58 u64 root_objectid, u64 owner_objectid,
59 u64 owner_offset, int refs_to_drop);
60 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
61 btrfs_root *extent_root);
62 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
63 btrfs_root *extent_root);
65 static int remove_sb_from_cache(struct btrfs_root *root,
66 struct btrfs_block_group_cache *cache)
72 struct extent_io_tree *free_space_cache;
74 free_space_cache = &root->fs_info->free_space_cache;
75 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
76 bytenr = btrfs_sb_offset(i);
77 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
78 cache->key.objectid, bytenr, 0,
79 &logical, &nr, &stripe_len);
82 clear_extent_dirty(free_space_cache, logical[nr],
83 logical[nr] + stripe_len - 1, GFP_NOFS);
90 static int cache_block_group(struct btrfs_root *root,
91 struct btrfs_block_group_cache *block_group)
93 struct btrfs_path *path;
96 struct extent_buffer *leaf;
97 struct extent_io_tree *free_space_cache;
105 root = root->fs_info->extent_root;
106 free_space_cache = &root->fs_info->free_space_cache;
108 if (block_group->cached)
111 path = btrfs_alloc_path();
116 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
121 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
126 leaf = path->nodes[0];
127 slot = path->slots[0];
128 if (slot >= btrfs_header_nritems(leaf)) {
129 ret = btrfs_next_leaf(root, path);
138 btrfs_item_key_to_cpu(leaf, &key, slot);
139 if (key.objectid < block_group->key.objectid) {
142 if (key.objectid >= block_group->key.objectid +
143 block_group->key.offset) {
147 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
148 key.type == BTRFS_METADATA_ITEM_KEY) {
149 if (key.objectid > last) {
150 hole_size = key.objectid - last;
151 set_extent_dirty(free_space_cache, last,
152 last + hole_size - 1,
155 if (key.type == BTRFS_METADATA_ITEM_KEY)
156 last = key.objectid + root->leafsize;
158 last = key.objectid + key.offset;
164 if (block_group->key.objectid +
165 block_group->key.offset > last) {
166 hole_size = block_group->key.objectid +
167 block_group->key.offset - last;
168 set_extent_dirty(free_space_cache, last,
169 last + hole_size - 1, GFP_NOFS);
171 remove_sb_from_cache(root, block_group);
172 block_group->cached = 1;
174 btrfs_free_path(path);
178 struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
182 struct extent_io_tree *block_group_cache;
183 struct btrfs_block_group_cache *block_group = NULL;
189 bytenr = max_t(u64, bytenr,
190 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
191 block_group_cache = &info->block_group_cache;
192 ret = find_first_extent_bit(block_group_cache,
193 bytenr, &start, &end,
194 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
199 ret = get_state_private(block_group_cache, start, &ptr);
203 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
207 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
211 struct extent_io_tree *block_group_cache;
212 struct btrfs_block_group_cache *block_group = NULL;
218 block_group_cache = &info->block_group_cache;
219 ret = find_first_extent_bit(block_group_cache,
220 bytenr, &start, &end,
221 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
226 ret = get_state_private(block_group_cache, start, &ptr);
230 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
231 if (block_group->key.objectid <= bytenr && bytenr <
232 block_group->key.objectid + block_group->key.offset)
237 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
239 return (cache->flags & bits) == bits;
242 static int noinline find_search_start(struct btrfs_root *root,
243 struct btrfs_block_group_cache **cache_ret,
244 u64 *start_ret, int num, int data)
247 struct btrfs_block_group_cache *cache = *cache_ret;
251 u64 search_start = *start_ret;
258 ret = cache_block_group(root, cache);
262 last = max(search_start, cache->key.objectid);
263 if (cache->ro || !block_group_bits(cache, data)) {
268 ret = find_first_extent_bit(&root->fs_info->free_space_cache,
269 last, &start, &end, EXTENT_DIRTY);
274 start = max(last, start);
276 if (last - start < num) {
279 if (start + num > cache->key.objectid + cache->key.offset) {
286 cache = btrfs_lookup_block_group(root->fs_info, search_start);
288 printk("Unable to find block group for %llu\n",
289 (unsigned long long)search_start);
295 last = cache->key.objectid + cache->key.offset;
297 cache = btrfs_lookup_first_block_group(root->fs_info, last);
307 cache = btrfs_find_block_group(root, cache, last, data, 0);
308 cache = btrfs_find_block_group(root, cache, last, data, 0);
316 static u64 div_factor(u64 num, int factor)
325 static int block_group_state_bits(u64 flags)
328 if (flags & BTRFS_BLOCK_GROUP_DATA)
329 bits |= BLOCK_GROUP_DATA;
330 if (flags & BTRFS_BLOCK_GROUP_METADATA)
331 bits |= BLOCK_GROUP_METADATA;
332 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
333 bits |= BLOCK_GROUP_SYSTEM;
337 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
338 struct btrfs_block_group_cache
339 *hint, u64 search_start,
342 struct btrfs_block_group_cache *cache;
343 struct extent_io_tree *block_group_cache;
344 struct btrfs_block_group_cache *found_group = NULL;
345 struct btrfs_fs_info *info = root->fs_info;
358 block_group_cache = &info->block_group_cache;
363 bit = block_group_state_bits(data);
366 struct btrfs_block_group_cache *shint;
367 shint = btrfs_lookup_block_group(info, search_start);
368 if (shint && !shint->ro && block_group_bits(shint, data)) {
369 used = btrfs_block_group_used(&shint->item);
370 if (used + shint->pinned <
371 div_factor(shint->key.offset, factor)) {
376 if (hint && !hint->ro && block_group_bits(hint, data)) {
377 used = btrfs_block_group_used(&hint->item);
378 if (used + hint->pinned <
379 div_factor(hint->key.offset, factor)) {
382 last = hint->key.objectid + hint->key.offset;
386 hint_last = max(hint->key.objectid, search_start);
388 hint_last = search_start;
394 ret = find_first_extent_bit(block_group_cache, last,
399 ret = get_state_private(block_group_cache, start, &ptr);
403 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
404 last = cache->key.objectid + cache->key.offset;
405 used = btrfs_block_group_used(&cache->item);
407 if (!cache->ro && block_group_bits(cache, data)) {
409 free_check = cache->key.offset;
411 free_check = div_factor(cache->key.offset,
414 if (used + cache->pinned < free_check) {
431 * Back reference rules. Back refs have three main goals:
433 * 1) differentiate between all holders of references to an extent so that
434 * when a reference is dropped we can make sure it was a valid reference
435 * before freeing the extent.
437 * 2) Provide enough information to quickly find the holders of an extent
438 * if we notice a given block is corrupted or bad.
440 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
441 * maintenance. This is actually the same as #2, but with a slightly
442 * different use case.
444 * There are two kinds of back refs. The implicit back refs is optimized
445 * for pointers in non-shared tree blocks. For a given pointer in a block,
446 * back refs of this kind provide information about the block's owner tree
447 * and the pointer's key. These information allow us to find the block by
448 * b-tree searching. The full back refs is for pointers in tree blocks not
449 * referenced by their owner trees. The location of tree block is recorded
450 * in the back refs. Actually the full back refs is generic, and can be
451 * used in all cases the implicit back refs is used. The major shortcoming
452 * of the full back refs is its overhead. Every time a tree block gets
453 * COWed, we have to update back refs entry for all pointers in it.
455 * For a newly allocated tree block, we use implicit back refs for
456 * pointers in it. This means most tree related operations only involve
457 * implicit back refs. For a tree block created in old transaction, the
458 * only way to drop a reference to it is COW it. So we can detect the
459 * event that tree block loses its owner tree's reference and do the
460 * back refs conversion.
462 * When a tree block is COW'd through a tree, there are four cases:
464 * The reference count of the block is one and the tree is the block's
465 * owner tree. Nothing to do in this case.
467 * The reference count of the block is one and the tree is not the
468 * block's owner tree. In this case, full back refs is used for pointers
469 * in the block. Remove these full back refs, add implicit back refs for
470 * every pointers in the new block.
472 * The reference count of the block is greater than one and the tree is
473 * the block's owner tree. In this case, implicit back refs is used for
474 * pointers in the block. Add full back refs for every pointers in the
475 * block, increase lower level extents' reference counts. The original
476 * implicit back refs are entailed to the new block.
478 * The reference count of the block is greater than one and the tree is
479 * not the block's owner tree. Add implicit back refs for every pointer in
480 * the new block, increase lower level extents' reference count.
482 * Back Reference Key composing:
484 * The key objectid corresponds to the first byte in the extent,
485 * The key type is used to differentiate between types of back refs.
486 * There are different meanings of the key offset for different types
489 * File extents can be referenced by:
491 * - multiple snapshots, subvolumes, or different generations in one subvol
492 * - different files inside a single subvolume
493 * - different offsets inside a file (bookend extents in file.c)
495 * The extent ref structure for the implicit back refs has fields for:
497 * - Objectid of the subvolume root
498 * - objectid of the file holding the reference
499 * - original offset in the file
500 * - how many bookend extents
502 * The key offset for the implicit back refs is hash of the first
505 * The extent ref structure for the full back refs has field for:
507 * - number of pointers in the tree leaf
509 * The key offset for the implicit back refs is the first byte of
512 * When a file extent is allocated, The implicit back refs is used.
513 * the fields are filled in:
515 * (root_key.objectid, inode objectid, offset in file, 1)
517 * When a file extent is removed file truncation, we find the
518 * corresponding implicit back refs and check the following fields:
520 * (btrfs_header_owner(leaf), inode objectid, offset in file)
522 * Btree extents can be referenced by:
524 * - Different subvolumes
526 * Both the implicit back refs and the full back refs for tree blocks
527 * only consist of key. The key offset for the implicit back refs is
528 * objectid of block's owner tree. The key offset for the full back refs
529 * is the first byte of parent block.
531 * When implicit back refs is used, information about the lowest key and
532 * level of the tree block are required. These information are stored in
533 * tree block info structure.
536 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
537 static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
538 struct btrfs_root *root,
539 struct btrfs_path *path,
540 u64 owner, u32 extra_size)
542 struct btrfs_extent_item *item;
543 struct btrfs_extent_item_v0 *ei0;
544 struct btrfs_extent_ref_v0 *ref0;
545 struct btrfs_tree_block_info *bi;
546 struct extent_buffer *leaf;
547 struct btrfs_key key;
548 struct btrfs_key found_key;
549 u32 new_size = sizeof(*item);
553 leaf = path->nodes[0];
554 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
556 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
557 ei0 = btrfs_item_ptr(leaf, path->slots[0],
558 struct btrfs_extent_item_v0);
559 refs = btrfs_extent_refs_v0(leaf, ei0);
561 if (owner == (u64)-1) {
563 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
564 ret = btrfs_next_leaf(root, path);
568 leaf = path->nodes[0];
570 btrfs_item_key_to_cpu(leaf, &found_key,
572 BUG_ON(key.objectid != found_key.objectid);
573 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
577 ref0 = btrfs_item_ptr(leaf, path->slots[0],
578 struct btrfs_extent_ref_v0);
579 owner = btrfs_ref_objectid_v0(leaf, ref0);
583 btrfs_release_path(root, path);
585 if (owner < BTRFS_FIRST_FREE_OBJECTID)
586 new_size += sizeof(*bi);
588 new_size -= sizeof(*ei0);
589 ret = btrfs_search_slot(trans, root, &key, path, new_size, 1);
594 ret = btrfs_extend_item(trans, root, path, new_size);
597 leaf = path->nodes[0];
598 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
599 btrfs_set_extent_refs(leaf, item, refs);
600 /* FIXME: get real generation */
601 btrfs_set_extent_generation(leaf, item, 0);
602 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
603 btrfs_set_extent_flags(leaf, item,
604 BTRFS_EXTENT_FLAG_TREE_BLOCK |
605 BTRFS_BLOCK_FLAG_FULL_BACKREF);
606 bi = (struct btrfs_tree_block_info *)(item + 1);
607 /* FIXME: get first key of the block */
608 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
609 btrfs_set_tree_block_level(leaf, bi, (int)owner);
611 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
613 btrfs_mark_buffer_dirty(leaf);
618 static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
620 u32 high_crc = ~(u32)0;
621 u32 low_crc = ~(u32)0;
624 lenum = cpu_to_le64(root_objectid);
625 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
626 lenum = cpu_to_le64(owner);
627 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
628 lenum = cpu_to_le64(offset);
629 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
631 return ((u64)high_crc << 31) ^ (u64)low_crc;
634 static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
635 struct btrfs_extent_data_ref *ref)
637 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
638 btrfs_extent_data_ref_objectid(leaf, ref),
639 btrfs_extent_data_ref_offset(leaf, ref));
642 static int match_extent_data_ref(struct extent_buffer *leaf,
643 struct btrfs_extent_data_ref *ref,
644 u64 root_objectid, u64 owner, u64 offset)
646 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
647 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
648 btrfs_extent_data_ref_offset(leaf, ref) != offset)
653 static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
654 struct btrfs_root *root,
655 struct btrfs_path *path,
656 u64 bytenr, u64 parent,
658 u64 owner, u64 offset)
660 struct btrfs_key key;
661 struct btrfs_extent_data_ref *ref;
662 struct extent_buffer *leaf;
668 key.objectid = bytenr;
670 key.type = BTRFS_SHARED_DATA_REF_KEY;
673 key.type = BTRFS_EXTENT_DATA_REF_KEY;
674 key.offset = hash_extent_data_ref(root_objectid,
679 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
688 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
689 key.type = BTRFS_EXTENT_REF_V0_KEY;
690 btrfs_release_path(root, path);
691 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
702 leaf = path->nodes[0];
703 nritems = btrfs_header_nritems(leaf);
705 if (path->slots[0] >= nritems) {
706 ret = btrfs_next_leaf(root, path);
712 leaf = path->nodes[0];
713 nritems = btrfs_header_nritems(leaf);
717 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
718 if (key.objectid != bytenr ||
719 key.type != BTRFS_EXTENT_DATA_REF_KEY)
722 ref = btrfs_item_ptr(leaf, path->slots[0],
723 struct btrfs_extent_data_ref);
725 if (match_extent_data_ref(leaf, ref, root_objectid,
728 btrfs_release_path(root, path);
740 static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
741 struct btrfs_root *root,
742 struct btrfs_path *path,
743 u64 bytenr, u64 parent,
744 u64 root_objectid, u64 owner,
745 u64 offset, int refs_to_add)
747 struct btrfs_key key;
748 struct extent_buffer *leaf;
753 key.objectid = bytenr;
755 key.type = BTRFS_SHARED_DATA_REF_KEY;
757 size = sizeof(struct btrfs_shared_data_ref);
759 key.type = BTRFS_EXTENT_DATA_REF_KEY;
760 key.offset = hash_extent_data_ref(root_objectid,
762 size = sizeof(struct btrfs_extent_data_ref);
765 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
766 if (ret && ret != -EEXIST)
769 leaf = path->nodes[0];
771 struct btrfs_shared_data_ref *ref;
772 ref = btrfs_item_ptr(leaf, path->slots[0],
773 struct btrfs_shared_data_ref);
775 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
777 num_refs = btrfs_shared_data_ref_count(leaf, ref);
778 num_refs += refs_to_add;
779 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
782 struct btrfs_extent_data_ref *ref;
783 while (ret == -EEXIST) {
784 ref = btrfs_item_ptr(leaf, path->slots[0],
785 struct btrfs_extent_data_ref);
786 if (match_extent_data_ref(leaf, ref, root_objectid,
789 btrfs_release_path(root, path);
792 ret = btrfs_insert_empty_item(trans, root, path, &key,
794 if (ret && ret != -EEXIST)
797 leaf = path->nodes[0];
799 ref = btrfs_item_ptr(leaf, path->slots[0],
800 struct btrfs_extent_data_ref);
802 btrfs_set_extent_data_ref_root(leaf, ref,
804 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
805 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
806 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
808 num_refs = btrfs_extent_data_ref_count(leaf, ref);
809 num_refs += refs_to_add;
810 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
813 btrfs_mark_buffer_dirty(leaf);
816 btrfs_release_path(root, path);
820 static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
821 struct btrfs_root *root,
822 struct btrfs_path *path,
825 struct btrfs_key key;
826 struct btrfs_extent_data_ref *ref1 = NULL;
827 struct btrfs_shared_data_ref *ref2 = NULL;
828 struct extent_buffer *leaf;
832 leaf = path->nodes[0];
833 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
835 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
836 ref1 = btrfs_item_ptr(leaf, path->slots[0],
837 struct btrfs_extent_data_ref);
838 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
839 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
840 ref2 = btrfs_item_ptr(leaf, path->slots[0],
841 struct btrfs_shared_data_ref);
842 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
843 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
844 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
845 struct btrfs_extent_ref_v0 *ref0;
846 ref0 = btrfs_item_ptr(leaf, path->slots[0],
847 struct btrfs_extent_ref_v0);
848 num_refs = btrfs_ref_count_v0(leaf, ref0);
854 BUG_ON(num_refs < refs_to_drop);
855 num_refs -= refs_to_drop;
858 ret = btrfs_del_item(trans, root, path);
860 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
861 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
862 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
863 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
864 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
866 struct btrfs_extent_ref_v0 *ref0;
867 ref0 = btrfs_item_ptr(leaf, path->slots[0],
868 struct btrfs_extent_ref_v0);
869 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
872 btrfs_mark_buffer_dirty(leaf);
877 static noinline u32 extent_data_ref_count(struct btrfs_root *root,
878 struct btrfs_path *path,
879 struct btrfs_extent_inline_ref *iref)
881 struct btrfs_key key;
882 struct extent_buffer *leaf;
883 struct btrfs_extent_data_ref *ref1;
884 struct btrfs_shared_data_ref *ref2;
887 leaf = path->nodes[0];
888 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
890 if (btrfs_extent_inline_ref_type(leaf, iref) ==
891 BTRFS_EXTENT_DATA_REF_KEY) {
892 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
893 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
895 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
896 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
898 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
899 ref1 = btrfs_item_ptr(leaf, path->slots[0],
900 struct btrfs_extent_data_ref);
901 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
902 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
903 ref2 = btrfs_item_ptr(leaf, path->slots[0],
904 struct btrfs_shared_data_ref);
905 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
906 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
907 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
908 struct btrfs_extent_ref_v0 *ref0;
909 ref0 = btrfs_item_ptr(leaf, path->slots[0],
910 struct btrfs_extent_ref_v0);
911 num_refs = btrfs_ref_count_v0(leaf, ref0);
919 static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
920 struct btrfs_root *root,
921 struct btrfs_path *path,
922 u64 bytenr, u64 parent,
925 struct btrfs_key key;
928 key.objectid = bytenr;
930 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
933 key.type = BTRFS_TREE_BLOCK_REF_KEY;
934 key.offset = root_objectid;
937 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
940 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
941 if (ret == -ENOENT && parent) {
942 btrfs_release_path(root, path);
943 key.type = BTRFS_EXTENT_REF_V0_KEY;
944 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
952 static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
953 struct btrfs_root *root,
954 struct btrfs_path *path,
955 u64 bytenr, u64 parent,
958 struct btrfs_key key;
961 key.objectid = bytenr;
963 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
966 key.type = BTRFS_TREE_BLOCK_REF_KEY;
967 key.offset = root_objectid;
970 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
972 btrfs_release_path(root, path);
976 static inline int extent_ref_type(u64 parent, u64 owner)
978 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
980 return BTRFS_SHARED_BLOCK_REF_KEY;
982 return BTRFS_TREE_BLOCK_REF_KEY;
985 return BTRFS_SHARED_DATA_REF_KEY;
987 return BTRFS_EXTENT_DATA_REF_KEY;
991 static int find_next_key(struct btrfs_path *path, struct btrfs_key *key)
995 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
996 if (!path->nodes[level])
998 if (path->slots[level] + 1 >=
999 btrfs_header_nritems(path->nodes[level]))
1002 btrfs_item_key_to_cpu(path->nodes[level], key,
1003 path->slots[level] + 1);
1005 btrfs_node_key_to_cpu(path->nodes[level], key,
1006 path->slots[level] + 1);
1012 static int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1013 struct btrfs_root *root,
1014 struct btrfs_path *path,
1015 struct btrfs_extent_inline_ref **ref_ret,
1016 u64 bytenr, u64 num_bytes,
1017 u64 parent, u64 root_objectid,
1018 u64 owner, u64 offset, int insert)
1020 struct btrfs_key key;
1021 struct extent_buffer *leaf;
1022 struct btrfs_extent_item *ei;
1023 struct btrfs_extent_inline_ref *iref;
1033 int skinny_metadata =
1034 btrfs_fs_incompat(root->fs_info,
1035 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
1037 key.objectid = bytenr;
1038 key.type = BTRFS_EXTENT_ITEM_KEY;
1039 key.offset = num_bytes;
1041 want = extent_ref_type(parent, owner);
1043 extra_size = btrfs_extent_inline_ref_size(want);
1047 if (owner < BTRFS_FIRST_FREE_OBJECTID && skinny_metadata) {
1048 skinny_metadata = 1;
1049 key.type = BTRFS_METADATA_ITEM_KEY;
1051 } else if (skinny_metadata) {
1052 skinny_metadata = 0;
1056 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1063 * We may be a newly converted file system which still has the old fat
1064 * extent entries for metadata, so try and see if we have one of those.
1066 if (ret > 0 && skinny_metadata) {
1067 skinny_metadata = 0;
1068 if (path->slots[0]) {
1070 btrfs_item_key_to_cpu(path->nodes[0], &key,
1072 if (key.objectid == bytenr &&
1073 key.type == BTRFS_EXTENT_ITEM_KEY &&
1074 key.offset == num_bytes)
1078 key.type = BTRFS_EXTENT_ITEM_KEY;
1079 key.offset = num_bytes;
1085 printf("Failed to find [%llu, %u, %llu]\n", key.objectid, key.type, key.offset);
1091 leaf = path->nodes[0];
1092 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1093 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1094 if (item_size < sizeof(*ei)) {
1099 ret = convert_extent_item_v0(trans, root, path, owner,
1105 leaf = path->nodes[0];
1106 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1109 if (item_size < sizeof(*ei)) {
1110 printf("Size is %u, needs to be %u, slot %d\n",
1111 (unsigned)item_size,
1112 (unsigned)sizeof(*ei), path->slots[0]);
1113 btrfs_print_leaf(root, leaf);
1116 BUG_ON(item_size < sizeof(*ei));
1118 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1119 flags = btrfs_extent_flags(leaf, ei);
1121 ptr = (unsigned long)(ei + 1);
1122 end = (unsigned long)ei + item_size;
1124 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
1125 ptr += sizeof(struct btrfs_tree_block_info);
1127 } else if (!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
1128 if (!(flags & BTRFS_EXTENT_FLAG_DATA)) {
1139 iref = (struct btrfs_extent_inline_ref *)ptr;
1140 type = btrfs_extent_inline_ref_type(leaf, iref);
1144 ptr += btrfs_extent_inline_ref_size(type);
1148 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1149 struct btrfs_extent_data_ref *dref;
1150 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1151 if (match_extent_data_ref(leaf, dref, root_objectid,
1156 if (hash_extent_data_ref_item(leaf, dref) <
1157 hash_extent_data_ref(root_objectid, owner, offset))
1161 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1163 if (parent == ref_offset) {
1167 if (ref_offset < parent)
1170 if (root_objectid == ref_offset) {
1174 if (ref_offset < root_objectid)
1178 ptr += btrfs_extent_inline_ref_size(type);
1180 if (err == -ENOENT && insert) {
1181 if (item_size + extra_size >=
1182 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1187 * To add new inline back ref, we have to make sure
1188 * there is no corresponding back ref item.
1189 * For simplicity, we just do not add new inline back
1190 * ref if there is any back ref item.
1192 if (find_next_key(path, &key) == 0 && key.objectid == bytenr &&
1193 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
1198 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1203 static int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1204 struct btrfs_root *root,
1205 struct btrfs_path *path,
1206 struct btrfs_extent_inline_ref *iref,
1207 u64 parent, u64 root_objectid,
1208 u64 owner, u64 offset, int refs_to_add)
1210 struct extent_buffer *leaf;
1211 struct btrfs_extent_item *ei;
1214 unsigned long item_offset;
1220 leaf = path->nodes[0];
1221 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1222 item_offset = (unsigned long)iref - (unsigned long)ei;
1224 type = extent_ref_type(parent, owner);
1225 size = btrfs_extent_inline_ref_size(type);
1227 ret = btrfs_extend_item(trans, root, path, size);
1230 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1231 refs = btrfs_extent_refs(leaf, ei);
1232 refs += refs_to_add;
1233 btrfs_set_extent_refs(leaf, ei, refs);
1235 ptr = (unsigned long)ei + item_offset;
1236 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1237 if (ptr < end - size)
1238 memmove_extent_buffer(leaf, ptr + size, ptr,
1241 iref = (struct btrfs_extent_inline_ref *)ptr;
1242 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1243 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1244 struct btrfs_extent_data_ref *dref;
1245 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1246 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1247 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1248 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1249 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1250 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1251 struct btrfs_shared_data_ref *sref;
1252 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1253 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1254 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1255 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1256 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1258 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1260 btrfs_mark_buffer_dirty(leaf);
1264 static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1265 struct btrfs_root *root,
1266 struct btrfs_path *path,
1267 struct btrfs_extent_inline_ref **ref_ret,
1268 u64 bytenr, u64 num_bytes, u64 parent,
1269 u64 root_objectid, u64 owner, u64 offset)
1273 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1274 bytenr, num_bytes, parent,
1275 root_objectid, owner, offset, 0);
1279 btrfs_release_path(root, path);
1282 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1283 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1286 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1287 root_objectid, owner, offset);
1292 static int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1293 struct btrfs_root *root,
1294 struct btrfs_path *path,
1295 struct btrfs_extent_inline_ref *iref,
1298 struct extent_buffer *leaf;
1299 struct btrfs_extent_item *ei;
1300 struct btrfs_extent_data_ref *dref = NULL;
1301 struct btrfs_shared_data_ref *sref = NULL;
1310 leaf = path->nodes[0];
1311 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1312 refs = btrfs_extent_refs(leaf, ei);
1313 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1314 refs += refs_to_mod;
1315 btrfs_set_extent_refs(leaf, ei, refs);
1317 type = btrfs_extent_inline_ref_type(leaf, iref);
1319 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1320 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1321 refs = btrfs_extent_data_ref_count(leaf, dref);
1322 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1323 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1324 refs = btrfs_shared_data_ref_count(leaf, sref);
1327 BUG_ON(refs_to_mod != -1);
1330 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1331 refs += refs_to_mod;
1334 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1335 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1337 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1339 size = btrfs_extent_inline_ref_size(type);
1340 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1341 ptr = (unsigned long)iref;
1342 end = (unsigned long)ei + item_size;
1343 if (ptr + size < end)
1344 memmove_extent_buffer(leaf, ptr, ptr + size,
1347 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1350 btrfs_mark_buffer_dirty(leaf);
1354 static int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1355 struct btrfs_root *root,
1356 struct btrfs_path *path,
1357 u64 bytenr, u64 num_bytes, u64 parent,
1358 u64 root_objectid, u64 owner,
1359 u64 offset, int refs_to_add)
1361 struct btrfs_extent_inline_ref *iref;
1364 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1365 bytenr, num_bytes, parent,
1366 root_objectid, owner, offset, 1);
1368 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1369 ret = update_inline_extent_backref(trans, root, path, iref,
1371 } else if (ret == -ENOENT) {
1372 ret = setup_inline_extent_backref(trans, root, path, iref,
1373 parent, root_objectid,
1374 owner, offset, refs_to_add);
1379 static int insert_extent_backref(struct btrfs_trans_handle *trans,
1380 struct btrfs_root *root,
1381 struct btrfs_path *path,
1382 u64 bytenr, u64 parent, u64 root_objectid,
1383 u64 owner, u64 offset, int refs_to_add)
1387 if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
1388 ret = insert_extent_data_ref(trans, root, path, bytenr,
1389 parent, root_objectid,
1390 owner, offset, refs_to_add);
1392 BUG_ON(refs_to_add != 1);
1393 ret = insert_tree_block_ref(trans, root, path, bytenr,
1394 parent, root_objectid);
1399 static int remove_extent_backref(struct btrfs_trans_handle *trans,
1400 struct btrfs_root *root,
1401 struct btrfs_path *path,
1402 struct btrfs_extent_inline_ref *iref,
1403 int refs_to_drop, int is_data)
1407 BUG_ON(!is_data && refs_to_drop != 1);
1409 ret = update_inline_extent_backref(trans, root, path, iref,
1411 } else if (is_data) {
1412 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1414 ret = btrfs_del_item(trans, root, path);
1419 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1420 struct btrfs_root *root,
1421 u64 bytenr, u64 num_bytes, u64 parent,
1422 u64 root_objectid, u64 owner, u64 offset)
1424 struct btrfs_path *path;
1425 struct extent_buffer *leaf;
1426 struct btrfs_extent_item *item;
1431 path = btrfs_alloc_path();
1436 path->leave_spinning = 1;
1438 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1439 path, bytenr, num_bytes, parent,
1440 root_objectid, owner, offset, 1);
1444 if (ret != -EAGAIN) {
1449 leaf = path->nodes[0];
1450 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1451 refs = btrfs_extent_refs(leaf, item);
1452 btrfs_set_extent_refs(leaf, item, refs + 1);
1454 btrfs_mark_buffer_dirty(leaf);
1455 btrfs_release_path(root->fs_info->extent_root, path);
1458 path->leave_spinning = 1;
1460 /* now insert the actual backref */
1461 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1462 path, bytenr, parent, root_objectid,
1467 btrfs_free_path(path);
1468 finish_current_insert(trans, root->fs_info->extent_root);
1469 del_pending_extents(trans, root->fs_info->extent_root);
1474 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1475 struct btrfs_root *root)
1477 finish_current_insert(trans, root->fs_info->extent_root);
1478 del_pending_extents(trans, root->fs_info->extent_root);
1482 int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
1483 struct btrfs_root *root, u64 bytenr,
1484 u64 offset, int metadata, u64 *refs, u64 *flags)
1486 struct btrfs_path *path;
1488 struct btrfs_key key;
1489 struct extent_buffer *l;
1490 struct btrfs_extent_item *item;
1496 !btrfs_fs_incompat(root->fs_info,
1497 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA)) {
1498 offset = root->leafsize;
1502 path = btrfs_alloc_path();
1505 key.objectid = bytenr;
1506 key.offset = offset;
1508 key.type = BTRFS_METADATA_ITEM_KEY;
1510 key.type = BTRFS_EXTENT_ITEM_KEY;
1513 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1519 * Deal with the fact that we may have mixed SKINNY and normal refs. If
1520 * we didn't find what we wanted check and see if we have a normal ref
1521 * right next to us, or re-search if we are on the edge of the leaf just
1524 if (ret > 0 && metadata) {
1527 btrfs_item_key_to_cpu(path->nodes[0], &key,
1529 if (key.objectid == bytenr &&
1530 key.type == BTRFS_METADATA_ITEM_KEY)
1535 btrfs_release_path(root, path);
1536 key.type = BTRFS_EXTENT_ITEM_KEY;
1537 key.offset = root->leafsize;
1549 item_size = btrfs_item_size_nr(l, path->slots[0]);
1550 if (item_size >= sizeof(*item)) {
1551 item = btrfs_item_ptr(l, path->slots[0],
1552 struct btrfs_extent_item);
1553 num_refs = btrfs_extent_refs(l, item);
1554 extent_flags = btrfs_extent_flags(l, item);
1556 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1557 struct btrfs_extent_item_v0 *ei0;
1558 BUG_ON(item_size != sizeof(*ei0));
1559 ei0 = btrfs_item_ptr(l, path->slots[0],
1560 struct btrfs_extent_item_v0);
1561 num_refs = btrfs_extent_refs_v0(l, ei0);
1562 /* FIXME: this isn't correct for data */
1563 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
1568 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1572 *flags = extent_flags;
1574 btrfs_free_path(path);
1578 int btrfs_set_block_flags(struct btrfs_trans_handle *trans,
1579 struct btrfs_root *root,
1580 u64 bytenr, int level, u64 flags)
1582 struct btrfs_path *path;
1584 struct btrfs_key key;
1585 struct extent_buffer *l;
1586 struct btrfs_extent_item *item;
1588 int skinny_metadata =
1589 btrfs_fs_incompat(root->fs_info,
1590 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
1592 path = btrfs_alloc_path();
1595 key.objectid = bytenr;
1596 if (skinny_metadata) {
1598 key.type = BTRFS_METADATA_ITEM_KEY;
1600 key.offset = root->leafsize;
1601 key.type = BTRFS_EXTENT_ITEM_KEY;
1605 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1610 if (ret > 0 && skinny_metadata) {
1611 skinny_metadata = 0;
1612 if (path->slots[0]--) {
1614 btrfs_item_key_to_cpu(path->nodes[0], &key,
1616 if (key.objectid == bytenr &&
1617 key.offset == root->leafsize &&
1618 key.type == BTRFS_EXTENT_ITEM_KEY)
1622 btrfs_release_path(root, path);
1623 key.offset = root->leafsize;
1624 key.type = BTRFS_EXTENT_ITEM_KEY;
1630 btrfs_print_leaf(root, path->nodes[0]);
1631 printk("failed to find block number %Lu\n",
1632 (unsigned long long)bytenr);
1636 item_size = btrfs_item_size_nr(l, path->slots[0]);
1637 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1638 if (item_size < sizeof(*item)) {
1639 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1645 item_size = btrfs_item_size_nr(l, path->slots[0]);
1648 BUG_ON(item_size < sizeof(*item));
1649 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1650 flags |= btrfs_extent_flags(l, item);
1651 btrfs_set_extent_flags(l, item, flags);
1653 btrfs_free_path(path);
1654 finish_current_insert(trans, root->fs_info->extent_root);
1655 del_pending_extents(trans, root->fs_info->extent_root);
1659 static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
1660 struct btrfs_root *root,
1661 struct extent_buffer *buf,
1662 int record_parent, int inc)
1669 struct btrfs_key key;
1670 struct btrfs_file_extent_item *fi;
1674 int (*process_func)(struct btrfs_trans_handle *trans,
1675 struct btrfs_root *root,
1676 u64, u64, u64, u64, u64, u64);
1678 ref_root = btrfs_header_owner(buf);
1679 nritems = btrfs_header_nritems(buf);
1680 level = btrfs_header_level(buf);
1682 if (!root->ref_cows && level == 0)
1686 process_func = btrfs_inc_extent_ref;
1688 process_func = btrfs_free_extent;
1691 parent = buf->start;
1695 for (i = 0; i < nritems; i++) {
1698 btrfs_item_key_to_cpu(buf, &key, i);
1699 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1701 fi = btrfs_item_ptr(buf, i,
1702 struct btrfs_file_extent_item);
1703 if (btrfs_file_extent_type(buf, fi) ==
1704 BTRFS_FILE_EXTENT_INLINE)
1706 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1710 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
1711 key.offset -= btrfs_file_extent_offset(buf, fi);
1712 ret = process_func(trans, root, bytenr, num_bytes,
1713 parent, ref_root, key.objectid,
1720 bytenr = btrfs_node_blockptr(buf, i);
1721 num_bytes = btrfs_level_size(root, level - 1);
1722 ret = process_func(trans, root, bytenr, num_bytes,
1723 parent, ref_root, level - 1, 0);
1736 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1737 struct extent_buffer *buf, int record_parent)
1739 return __btrfs_mod_ref(trans, root, buf, record_parent, 1);
1742 int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1743 struct extent_buffer *buf, int record_parent)
1745 return __btrfs_mod_ref(trans, root, buf, record_parent, 0);
1748 static int write_one_cache_group(struct btrfs_trans_handle *trans,
1749 struct btrfs_root *root,
1750 struct btrfs_path *path,
1751 struct btrfs_block_group_cache *cache)
1755 struct btrfs_root *extent_root = root->fs_info->extent_root;
1757 struct extent_buffer *leaf;
1759 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
1764 leaf = path->nodes[0];
1765 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1766 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1767 btrfs_mark_buffer_dirty(leaf);
1768 btrfs_release_path(extent_root, path);
1770 finish_current_insert(trans, extent_root);
1771 pending_ret = del_pending_extents(trans, extent_root);
1780 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1781 struct btrfs_root *root)
1783 struct extent_io_tree *block_group_cache;
1784 struct btrfs_block_group_cache *cache;
1786 struct btrfs_path *path;
1792 block_group_cache = &root->fs_info->block_group_cache;
1793 path = btrfs_alloc_path();
1798 ret = find_first_extent_bit(block_group_cache, last,
1799 &start, &end, BLOCK_GROUP_DIRTY);
1808 ret = get_state_private(block_group_cache, start, &ptr);
1811 clear_extent_bits(block_group_cache, start, end,
1812 BLOCK_GROUP_DIRTY, GFP_NOFS);
1814 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
1815 ret = write_one_cache_group(trans, root, path, cache);
1817 btrfs_free_path(path);
1821 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
1824 struct list_head *head = &info->space_info;
1825 struct list_head *cur;
1826 struct btrfs_space_info *found;
1827 list_for_each(cur, head) {
1828 found = list_entry(cur, struct btrfs_space_info, list);
1829 if (found->flags & flags)
1836 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1837 u64 total_bytes, u64 bytes_used,
1838 struct btrfs_space_info **space_info)
1840 struct btrfs_space_info *found;
1842 found = __find_space_info(info, flags);
1844 found->total_bytes += total_bytes;
1845 found->bytes_used += bytes_used;
1846 if (found->total_bytes < found->bytes_used) {
1847 fprintf(stderr, "warning, bad space info total_bytes "
1849 (unsigned long long)found->total_bytes,
1850 (unsigned long long)found->bytes_used);
1852 *space_info = found;
1855 found = kmalloc(sizeof(*found), GFP_NOFS);
1859 list_add(&found->list, &info->space_info);
1860 found->flags = flags;
1861 found->total_bytes = total_bytes;
1862 found->bytes_used = bytes_used;
1863 found->bytes_pinned = 0;
1865 *space_info = found;
1870 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1872 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
1873 BTRFS_BLOCK_GROUP_RAID1 |
1874 BTRFS_BLOCK_GROUP_RAID10 |
1875 BTRFS_BLOCK_GROUP_RAID5 |
1876 BTRFS_BLOCK_GROUP_RAID6 |
1877 BTRFS_BLOCK_GROUP_DUP);
1879 if (flags & BTRFS_BLOCK_GROUP_DATA)
1880 fs_info->avail_data_alloc_bits |= extra_flags;
1881 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1882 fs_info->avail_metadata_alloc_bits |= extra_flags;
1883 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1884 fs_info->avail_system_alloc_bits |= extra_flags;
1888 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1889 struct btrfs_root *extent_root, u64 alloc_bytes,
1892 struct btrfs_space_info *space_info;
1898 space_info = __find_space_info(extent_root->fs_info, flags);
1900 ret = update_space_info(extent_root->fs_info, flags,
1904 BUG_ON(!space_info);
1906 if (space_info->full)
1909 thresh = div_factor(space_info->total_bytes, 7);
1910 if ((space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) <
1914 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes,
1916 if (ret == -ENOSPC) {
1917 space_info->full = 1;
1923 ret = btrfs_make_block_group(trans, extent_root, 0, space_info->flags,
1924 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
1929 static int update_block_group(struct btrfs_trans_handle *trans,
1930 struct btrfs_root *root,
1931 u64 bytenr, u64 num_bytes, int alloc,
1934 struct btrfs_block_group_cache *cache;
1935 struct btrfs_fs_info *info = root->fs_info;
1936 u64 total = num_bytes;
1942 /* block accounting for super block */
1943 old_val = btrfs_super_bytes_used(info->super_copy);
1945 old_val += num_bytes;
1947 old_val -= num_bytes;
1948 btrfs_set_super_bytes_used(info->super_copy, old_val);
1950 /* block accounting for root item */
1951 old_val = btrfs_root_used(&root->root_item);
1953 old_val += num_bytes;
1955 old_val -= num_bytes;
1956 btrfs_set_root_used(&root->root_item, old_val);
1959 cache = btrfs_lookup_block_group(info, bytenr);
1963 byte_in_group = bytenr - cache->key.objectid;
1964 WARN_ON(byte_in_group > cache->key.offset);
1965 start = cache->key.objectid;
1966 end = start + cache->key.offset - 1;
1967 set_extent_bits(&info->block_group_cache, start, end,
1968 BLOCK_GROUP_DIRTY, GFP_NOFS);
1970 old_val = btrfs_block_group_used(&cache->item);
1971 num_bytes = min(total, cache->key.offset - byte_in_group);
1974 old_val += num_bytes;
1975 cache->space_info->bytes_used += num_bytes;
1977 old_val -= num_bytes;
1978 cache->space_info->bytes_used -= num_bytes;
1980 set_extent_dirty(&info->free_space_cache,
1981 bytenr, bytenr + num_bytes - 1,
1985 btrfs_set_block_group_used(&cache->item, old_val);
1987 bytenr += num_bytes;
1992 static int update_pinned_extents(struct btrfs_root *root,
1993 u64 bytenr, u64 num, int pin)
1996 struct btrfs_block_group_cache *cache;
1997 struct btrfs_fs_info *fs_info = root->fs_info;
2000 set_extent_dirty(&fs_info->pinned_extents,
2001 bytenr, bytenr + num - 1, GFP_NOFS);
2003 clear_extent_dirty(&fs_info->pinned_extents,
2004 bytenr, bytenr + num - 1, GFP_NOFS);
2007 cache = btrfs_lookup_block_group(fs_info, bytenr);
2009 len = min((u64)root->sectorsize, num);
2013 len = min(num, cache->key.offset -
2014 (bytenr - cache->key.objectid));
2016 cache->pinned += len;
2017 cache->space_info->bytes_pinned += len;
2018 fs_info->total_pinned += len;
2020 cache->pinned -= len;
2021 cache->space_info->bytes_pinned -= len;
2022 fs_info->total_pinned -= len;
2031 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
2036 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
2040 ret = find_first_extent_bit(pinned_extents, last,
2041 &start, &end, EXTENT_DIRTY);
2044 set_extent_dirty(copy, start, end, GFP_NOFS);
2050 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2051 struct btrfs_root *root,
2052 struct extent_io_tree *unpin)
2057 struct extent_io_tree *free_space_cache;
2058 free_space_cache = &root->fs_info->free_space_cache;
2061 ret = find_first_extent_bit(unpin, 0, &start, &end,
2065 update_pinned_extents(root, start, end + 1 - start, 0);
2066 clear_extent_dirty(unpin, start, end, GFP_NOFS);
2067 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
2072 static int extent_root_pending_ops(struct btrfs_fs_info *info)
2078 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
2079 &end, EXTENT_LOCKED);
2081 ret = find_first_extent_bit(&info->pending_del, 0, &start, &end,
2087 static int finish_current_insert(struct btrfs_trans_handle *trans,
2088 struct btrfs_root *extent_root)
2093 struct btrfs_fs_info *info = extent_root->fs_info;
2094 struct btrfs_path *path;
2095 struct pending_extent_op *extent_op;
2096 struct btrfs_key key;
2098 int skinny_metadata =
2099 btrfs_fs_incompat(extent_root->fs_info,
2100 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
2102 path = btrfs_alloc_path();
2105 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
2106 &end, EXTENT_LOCKED);
2110 ret = get_state_private(&info->extent_ins, start, &priv);
2112 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2114 if (extent_op->type == PENDING_EXTENT_INSERT) {
2115 key.objectid = start;
2116 if (skinny_metadata) {
2117 key.offset = extent_op->level;
2118 key.type = BTRFS_METADATA_ITEM_KEY;
2120 key.offset = extent_op->num_bytes;
2121 key.type = BTRFS_EXTENT_ITEM_KEY;
2123 ret = alloc_reserved_tree_block(trans, extent_root,
2124 extent_root->root_key.objectid,
2128 extent_op->level, &key);
2133 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
2137 btrfs_free_path(path);
2141 static int pin_down_bytes(struct btrfs_trans_handle *trans,
2142 struct btrfs_root *root,
2143 u64 bytenr, u64 num_bytes, int is_data)
2146 struct extent_buffer *buf;
2151 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2155 /* we can reuse a block if it hasn't been written
2156 * and it is from this transaction. We can't
2157 * reuse anything from the tree log root because
2158 * it has tiny sub-transactions.
2160 if (btrfs_buffer_uptodate(buf, 0)) {
2161 u64 header_owner = btrfs_header_owner(buf);
2162 u64 header_transid = btrfs_header_generation(buf);
2163 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
2164 header_transid == trans->transid &&
2165 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2166 clean_tree_block(NULL, root, buf);
2167 free_extent_buffer(buf);
2171 free_extent_buffer(buf);
2173 update_pinned_extents(root, bytenr, num_bytes, 1);
2179 void btrfs_pin_extent(struct btrfs_fs_info *fs_info,
2180 u64 bytenr, u64 num_bytes)
2182 update_pinned_extents(fs_info->extent_root, bytenr, num_bytes, 1);
2186 * remove an extent from the root, returns 0 on success
2188 static int __free_extent(struct btrfs_trans_handle *trans,
2189 struct btrfs_root *root,
2190 u64 bytenr, u64 num_bytes, u64 parent,
2191 u64 root_objectid, u64 owner_objectid,
2192 u64 owner_offset, int refs_to_drop)
2195 struct btrfs_key key;
2196 struct btrfs_path *path;
2197 struct btrfs_extent_ops *ops = root->fs_info->extent_ops;
2198 struct btrfs_root *extent_root = root->fs_info->extent_root;
2199 struct extent_buffer *leaf;
2200 struct btrfs_extent_item *ei;
2201 struct btrfs_extent_inline_ref *iref;
2204 int extent_slot = 0;
2205 int found_extent = 0;
2209 int skinny_metadata =
2210 btrfs_fs_incompat(extent_root->fs_info,
2211 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
2213 if (root->fs_info->free_extent_hook) {
2214 root->fs_info->free_extent_hook(trans, root, bytenr, num_bytes,
2215 parent, root_objectid, owner_objectid,
2216 owner_offset, refs_to_drop);
2219 path = btrfs_alloc_path();
2224 path->leave_spinning = 1;
2226 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
2228 skinny_metadata = 0;
2229 BUG_ON(!is_data && refs_to_drop != 1);
2231 ret = lookup_extent_backref(trans, extent_root, path, &iref,
2232 bytenr, num_bytes, parent,
2233 root_objectid, owner_objectid,
2236 extent_slot = path->slots[0];
2237 while (extent_slot >= 0) {
2238 btrfs_item_key_to_cpu(path->nodes[0], &key,
2240 if (key.objectid != bytenr)
2242 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
2243 key.offset == num_bytes) {
2247 if (key.type == BTRFS_METADATA_ITEM_KEY &&
2248 key.offset == owner_objectid) {
2252 if (path->slots[0] - extent_slot > 5)
2256 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2257 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
2258 if (found_extent && item_size < sizeof(*ei))
2261 if (!found_extent) {
2263 ret = remove_extent_backref(trans, extent_root, path,
2267 btrfs_release_path(extent_root, path);
2268 path->leave_spinning = 1;
2270 key.objectid = bytenr;
2272 if (skinny_metadata) {
2273 key.type = BTRFS_METADATA_ITEM_KEY;
2274 key.offset = owner_objectid;
2276 key.type = BTRFS_EXTENT_ITEM_KEY;
2277 key.offset = num_bytes;
2280 ret = btrfs_search_slot(trans, extent_root,
2282 if (ret > 0 && skinny_metadata && path->slots[0]) {
2284 btrfs_item_key_to_cpu(path->nodes[0],
2287 if (key.objectid == bytenr &&
2288 key.type == BTRFS_EXTENT_ITEM_KEY &&
2289 key.offset == num_bytes)
2293 if (ret > 0 && skinny_metadata) {
2294 skinny_metadata = 0;
2295 btrfs_release_path(extent_root, path);
2296 key.type = BTRFS_EXTENT_ITEM_KEY;
2297 key.offset = num_bytes;
2298 ret = btrfs_search_slot(trans, extent_root,
2303 printk(KERN_ERR "umm, got %d back from search"
2304 ", was looking for %llu\n", ret,
2305 (unsigned long long)bytenr);
2306 btrfs_print_leaf(extent_root, path->nodes[0]);
2309 extent_slot = path->slots[0];
2312 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
2313 "parent %llu root %llu owner %llu offset %llu\n",
2314 (unsigned long long)bytenr,
2315 (unsigned long long)parent,
2316 (unsigned long long)root_objectid,
2317 (unsigned long long)owner_objectid,
2318 (unsigned long long)owner_offset);
2323 leaf = path->nodes[0];
2324 item_size = btrfs_item_size_nr(leaf, extent_slot);
2325 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2326 if (item_size < sizeof(*ei)) {
2327 BUG_ON(found_extent || extent_slot != path->slots[0]);
2328 ret = convert_extent_item_v0(trans, extent_root, path,
2332 btrfs_release_path(extent_root, path);
2333 path->leave_spinning = 1;
2335 key.objectid = bytenr;
2336 key.type = BTRFS_EXTENT_ITEM_KEY;
2337 key.offset = num_bytes;
2339 ret = btrfs_search_slot(trans, extent_root, &key, path,
2342 printk(KERN_ERR "umm, got %d back from search"
2343 ", was looking for %llu\n", ret,
2344 (unsigned long long)bytenr);
2345 btrfs_print_leaf(extent_root, path->nodes[0]);
2348 extent_slot = path->slots[0];
2349 leaf = path->nodes[0];
2350 item_size = btrfs_item_size_nr(leaf, extent_slot);
2353 BUG_ON(item_size < sizeof(*ei));
2354 ei = btrfs_item_ptr(leaf, extent_slot,
2355 struct btrfs_extent_item);
2356 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
2357 key.type == BTRFS_EXTENT_ITEM_KEY) {
2358 struct btrfs_tree_block_info *bi;
2359 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
2360 bi = (struct btrfs_tree_block_info *)(ei + 1);
2361 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
2364 refs = btrfs_extent_refs(leaf, ei);
2365 BUG_ON(refs < refs_to_drop);
2366 refs -= refs_to_drop;
2370 * In the case of inline back ref, reference count will
2371 * be updated by remove_extent_backref
2374 BUG_ON(!found_extent);
2376 btrfs_set_extent_refs(leaf, ei, refs);
2377 btrfs_mark_buffer_dirty(leaf);
2380 ret = remove_extent_backref(trans, extent_root, path,
2390 BUG_ON(is_data && refs_to_drop !=
2391 extent_data_ref_count(root, path, iref));
2393 BUG_ON(path->slots[0] != extent_slot);
2395 BUG_ON(path->slots[0] != extent_slot + 1);
2396 path->slots[0] = extent_slot;
2401 if (ops && ops->free_extent) {
2402 ret = ops->free_extent(root, bytenr, num_bytes);
2410 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
2417 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2420 btrfs_release_path(extent_root, path);
2423 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
2427 update_block_group(trans, root, bytenr, num_bytes, 0, mark_free);
2430 btrfs_free_path(path);
2431 finish_current_insert(trans, extent_root);
2436 * find all the blocks marked as pending in the radix tree and remove
2437 * them from the extent map
2439 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
2440 btrfs_root *extent_root)
2447 struct extent_io_tree *pending_del;
2448 struct extent_io_tree *extent_ins;
2449 struct pending_extent_op *extent_op;
2451 extent_ins = &extent_root->fs_info->extent_ins;
2452 pending_del = &extent_root->fs_info->pending_del;
2455 ret = find_first_extent_bit(pending_del, 0, &start, &end,
2460 ret = get_state_private(pending_del, start, &priv);
2462 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2464 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
2467 if (!test_range_bit(extent_ins, start, end,
2468 EXTENT_LOCKED, 0)) {
2469 ret = __free_extent(trans, extent_root,
2470 start, end + 1 - start, 0,
2471 extent_root->root_key.objectid,
2472 extent_op->level, 0, 1);
2476 ret = get_state_private(extent_ins, start, &priv);
2478 extent_op = (struct pending_extent_op *)
2479 (unsigned long)priv;
2481 clear_extent_bits(extent_ins, start, end,
2482 EXTENT_LOCKED, GFP_NOFS);
2484 if (extent_op->type == PENDING_BACKREF_UPDATE)
2496 * remove an extent from the root, returns 0 on success
2499 int btrfs_free_extent(struct btrfs_trans_handle *trans,
2500 struct btrfs_root *root,
2501 u64 bytenr, u64 num_bytes, u64 parent,
2502 u64 root_objectid, u64 owner, u64 offset)
2504 struct btrfs_root *extent_root = root->fs_info->extent_root;
2508 WARN_ON(num_bytes < root->sectorsize);
2509 if (root == extent_root) {
2510 struct pending_extent_op *extent_op;
2512 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2515 extent_op->type = PENDING_EXTENT_DELETE;
2516 extent_op->bytenr = bytenr;
2517 extent_op->num_bytes = num_bytes;
2518 extent_op->level = (int)owner;
2520 set_extent_bits(&root->fs_info->pending_del,
2521 bytenr, bytenr + num_bytes - 1,
2522 EXTENT_LOCKED, GFP_NOFS);
2523 set_state_private(&root->fs_info->pending_del,
2524 bytenr, (unsigned long)extent_op);
2527 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
2528 root_objectid, owner, offset, 1);
2529 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
2530 return ret ? ret : pending_ret;
2533 static u64 stripe_align(struct btrfs_root *root, u64 val)
2535 u64 mask = ((u64)root->stripesize - 1);
2536 u64 ret = (val + mask) & ~mask;
2541 * walks the btree of allocated extents and find a hole of a given size.
2542 * The key ins is changed to record the hole:
2543 * ins->objectid == block start
2544 * ins->flags = BTRFS_EXTENT_ITEM_KEY
2545 * ins->offset == number of blocks
2546 * Any available blocks before search_start are skipped.
2548 static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2549 struct btrfs_root *orig_root,
2550 u64 num_bytes, u64 empty_size,
2551 u64 search_start, u64 search_end,
2552 u64 hint_byte, struct btrfs_key *ins,
2553 u64 exclude_start, u64 exclude_nr,
2557 u64 orig_search_start = search_start;
2558 struct btrfs_root * root = orig_root->fs_info->extent_root;
2559 struct btrfs_fs_info *info = root->fs_info;
2560 u64 total_needed = num_bytes;
2561 struct btrfs_block_group_cache *block_group;
2565 WARN_ON(num_bytes < root->sectorsize);
2566 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
2568 search_start = stripe_align(root, search_start);
2571 block_group = btrfs_lookup_first_block_group(info, hint_byte);
2573 hint_byte = search_start;
2574 block_group = btrfs_find_block_group(root, block_group,
2575 hint_byte, data, 1);
2577 block_group = btrfs_find_block_group(root,
2579 search_start, data, 1);
2582 total_needed += empty_size;
2585 search_start = stripe_align(root, search_start);
2587 block_group = btrfs_lookup_first_block_group(info,
2590 block_group = btrfs_lookup_first_block_group(info,
2593 ret = find_search_start(root, &block_group, &search_start,
2594 total_needed, data);
2598 ins->objectid = search_start;
2599 ins->offset = num_bytes;
2601 if (ins->objectid + num_bytes >
2602 block_group->key.objectid + block_group->key.offset) {
2603 search_start = block_group->key.objectid +
2604 block_group->key.offset;
2608 if (test_range_bit(&info->extent_ins, ins->objectid,
2609 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
2610 search_start = ins->objectid + num_bytes;
2614 if (test_range_bit(&info->pinned_extents, ins->objectid,
2615 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
2616 search_start = ins->objectid + num_bytes;
2620 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
2621 ins->objectid < exclude_start + exclude_nr)) {
2622 search_start = exclude_start + exclude_nr;
2626 if (!(data & BTRFS_BLOCK_GROUP_DATA)) {
2627 block_group = btrfs_lookup_block_group(info, ins->objectid);
2629 trans->block_group = block_group;
2631 ins->offset = num_bytes;
2635 block_group = btrfs_lookup_first_block_group(info, search_start);
2637 search_start = orig_search_start;
2644 total_needed -= empty_size;
2650 block_group = btrfs_find_block_group(root, block_group,
2651 search_start, data, 0);
2658 static int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2659 struct btrfs_root *root,
2660 u64 num_bytes, u64 empty_size,
2661 u64 hint_byte, u64 search_end,
2662 struct btrfs_key *ins, int data)
2665 u64 search_start = 0;
2667 struct btrfs_fs_info *info = root->fs_info;
2669 if (info->extent_ops) {
2670 struct btrfs_extent_ops *ops = info->extent_ops;
2671 ret = ops->alloc_extent(root, num_bytes, hint_byte, ins);
2677 alloc_profile = info->avail_data_alloc_bits &
2678 info->data_alloc_profile;
2679 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
2680 } else if ((info->system_allocs > 0 || root == info->chunk_root) &&
2681 info->system_allocs >= 0) {
2682 alloc_profile = info->avail_system_alloc_bits &
2683 info->system_alloc_profile;
2684 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
2686 alloc_profile = info->avail_metadata_alloc_bits &
2687 info->metadata_alloc_profile;
2688 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
2691 if (root->ref_cows) {
2692 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
2693 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2695 BTRFS_BLOCK_GROUP_METADATA);
2698 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2699 num_bytes + 2 * 1024 * 1024, data);
2703 WARN_ON(num_bytes < root->sectorsize);
2704 ret = find_free_extent(trans, root, num_bytes, empty_size,
2705 search_start, search_end, hint_byte, ins,
2706 trans->alloc_exclude_start,
2707 trans->alloc_exclude_nr, data);
2710 clear_extent_dirty(&root->fs_info->free_space_cache,
2711 ins->objectid, ins->objectid + ins->offset - 1,
2716 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
2717 struct btrfs_root *root,
2718 u64 root_objectid, u64 generation,
2719 u64 flags, struct btrfs_disk_key *key,
2720 int level, struct btrfs_key *ins)
2723 struct btrfs_fs_info *fs_info = root->fs_info;
2724 struct btrfs_extent_item *extent_item;
2725 struct btrfs_tree_block_info *block_info;
2726 struct btrfs_extent_inline_ref *iref;
2727 struct btrfs_path *path;
2728 struct extent_buffer *leaf;
2729 u32 size = sizeof(*extent_item) + sizeof(*iref);
2730 int skinny_metadata =
2731 btrfs_fs_incompat(fs_info,
2732 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
2734 if (!skinny_metadata)
2735 size += sizeof(*block_info);
2737 path = btrfs_alloc_path();
2740 path->leave_spinning = 1;
2741 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
2745 leaf = path->nodes[0];
2746 extent_item = btrfs_item_ptr(leaf, path->slots[0],
2747 struct btrfs_extent_item);
2748 btrfs_set_extent_refs(leaf, extent_item, 1);
2749 btrfs_set_extent_generation(leaf, extent_item, generation);
2750 btrfs_set_extent_flags(leaf, extent_item,
2751 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
2753 if (skinny_metadata) {
2754 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
2756 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
2757 btrfs_set_tree_block_key(leaf, block_info, key);
2758 btrfs_set_tree_block_level(leaf, block_info, level);
2759 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
2762 btrfs_set_extent_inline_ref_type(leaf, iref, BTRFS_TREE_BLOCK_REF_KEY);
2763 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
2765 btrfs_mark_buffer_dirty(leaf);
2766 btrfs_free_path(path);
2768 ret = update_block_group(trans, root, ins->objectid, root->leafsize,
2773 static int alloc_tree_block(struct btrfs_trans_handle *trans,
2774 struct btrfs_root *root, u64 num_bytes,
2775 u64 root_objectid, u64 generation,
2776 u64 flags, struct btrfs_disk_key *key,
2777 int level, u64 empty_size, u64 hint_byte,
2778 u64 search_end, struct btrfs_key *ins)
2781 ret = btrfs_reserve_extent(trans, root, num_bytes, empty_size,
2782 hint_byte, search_end, ins, 0);
2785 if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID) {
2786 struct pending_extent_op *extent_op;
2788 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2791 extent_op->type = PENDING_EXTENT_INSERT;
2792 extent_op->bytenr = ins->objectid;
2793 extent_op->num_bytes = ins->offset;
2794 extent_op->level = level;
2795 extent_op->flags = flags;
2796 memcpy(&extent_op->key, key, sizeof(*key));
2798 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
2799 ins->objectid + ins->offset - 1,
2800 EXTENT_LOCKED, GFP_NOFS);
2801 set_state_private(&root->fs_info->extent_ins,
2802 ins->objectid, (unsigned long)extent_op);
2804 if (btrfs_fs_incompat(root->fs_info,
2805 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA)) {
2806 ins->offset = level;
2807 ins->type = BTRFS_METADATA_ITEM_KEY;
2809 ret = alloc_reserved_tree_block(trans, root, root_objectid,
2812 finish_current_insert(trans, root->fs_info->extent_root);
2813 del_pending_extents(trans, root->fs_info->extent_root);
2819 * helper function to allocate a block for a given tree
2820 * returns the tree buffer or NULL.
2822 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
2823 struct btrfs_root *root,
2824 u32 blocksize, u64 root_objectid,
2825 struct btrfs_disk_key *key, int level,
2826 u64 hint, u64 empty_size)
2828 struct btrfs_key ins;
2830 struct extent_buffer *buf;
2832 ret = alloc_tree_block(trans, root, blocksize, root_objectid,
2833 trans->transid, 0, key, level,
2834 empty_size, hint, (u64)-1, &ins);
2837 return ERR_PTR(ret);
2840 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
2842 btrfs_free_extent(trans, root, ins.objectid, ins.offset,
2843 0, root->root_key.objectid, level, 0);
2845 return ERR_PTR(-ENOMEM);
2847 btrfs_set_buffer_uptodate(buf);
2848 trans->blocks_used++;
2855 static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
2856 struct btrfs_root *root,
2857 struct extent_buffer *leaf)
2860 u64 leaf_generation;
2861 struct btrfs_key key;
2862 struct btrfs_file_extent_item *fi;
2867 BUG_ON(!btrfs_is_leaf(leaf));
2868 nritems = btrfs_header_nritems(leaf);
2869 leaf_owner = btrfs_header_owner(leaf);
2870 leaf_generation = btrfs_header_generation(leaf);
2872 for (i = 0; i < nritems; i++) {
2875 btrfs_item_key_to_cpu(leaf, &key, i);
2876 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2878 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
2879 if (btrfs_file_extent_type(leaf, fi) ==
2880 BTRFS_FILE_EXTENT_INLINE)
2883 * FIXME make sure to insert a trans record that
2884 * repeats the snapshot del on crash
2886 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2887 if (disk_bytenr == 0)
2889 ret = btrfs_free_extent(trans, root, disk_bytenr,
2890 btrfs_file_extent_disk_num_bytes(leaf, fi),
2891 leaf->start, leaf_owner, leaf_generation,
2898 static void noinline reada_walk_down(struct btrfs_root *root,
2899 struct extent_buffer *node,
2912 nritems = btrfs_header_nritems(node);
2913 level = btrfs_header_level(node);
2917 for (i = slot; i < nritems && skipped < 32; i++) {
2918 bytenr = btrfs_node_blockptr(node, i);
2919 if (last && ((bytenr > last && bytenr - last > 32 * 1024) ||
2920 (last > bytenr && last - bytenr > 32 * 1024))) {
2924 blocksize = btrfs_level_size(root, level - 1);
2926 ret = btrfs_lookup_extent_ref(NULL, root, bytenr,
2934 mutex_unlock(&root->fs_info->fs_mutex);
2935 ret = readahead_tree_block(root, bytenr, blocksize,
2936 btrfs_node_ptr_generation(node, i));
2937 last = bytenr + blocksize;
2939 mutex_lock(&root->fs_info->fs_mutex);
2946 * helper function for drop_snapshot, this walks down the tree dropping ref
2947 * counts as it goes.
2949 static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2950 struct btrfs_root *root,
2951 struct btrfs_path *path, int *level)
2957 struct extent_buffer *next;
2958 struct extent_buffer *cur;
2959 struct extent_buffer *parent;
2964 WARN_ON(*level < 0);
2965 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2966 ret = btrfs_lookup_extent_ref(trans, root,
2967 path->nodes[*level]->start,
2968 path->nodes[*level]->len, &refs);
2974 * walk down to the last node level and free all the leaves
2976 while(*level >= 0) {
2977 WARN_ON(*level < 0);
2978 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2979 cur = path->nodes[*level];
2981 if (btrfs_header_level(cur) != *level)
2984 if (path->slots[*level] >=
2985 btrfs_header_nritems(cur))
2988 ret = drop_leaf_ref(trans, root, cur);
2992 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2993 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2994 blocksize = btrfs_level_size(root, *level - 1);
2995 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
2999 parent = path->nodes[*level];
3000 root_owner = btrfs_header_owner(parent);
3001 root_gen = btrfs_header_generation(parent);
3002 path->slots[*level]++;
3003 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
3004 parent->start, root_owner,
3005 root_gen, *level - 1, 1);
3009 next = btrfs_find_tree_block(root, bytenr, blocksize);
3010 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
3011 free_extent_buffer(next);
3012 reada_walk_down(root, cur, path->slots[*level]);
3013 mutex_unlock(&root->fs_info->fs_mutex);
3014 next = read_tree_block(root, bytenr, blocksize,
3016 mutex_lock(&root->fs_info->fs_mutex);
3018 WARN_ON(*level <= 0);
3019 if (path->nodes[*level-1])
3020 free_extent_buffer(path->nodes[*level-1]);
3021 path->nodes[*level-1] = next;
3022 *level = btrfs_header_level(next);
3023 path->slots[*level] = 0;
3026 WARN_ON(*level < 0);
3027 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3029 if (path->nodes[*level] == root->node) {
3030 root_owner = root->root_key.objectid;
3031 parent = path->nodes[*level];
3033 parent = path->nodes[*level + 1];
3034 root_owner = btrfs_header_owner(parent);
3037 root_gen = btrfs_header_generation(parent);
3038 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
3039 path->nodes[*level]->len, parent->start,
3040 root_owner, root_gen, *level, 1);
3041 free_extent_buffer(path->nodes[*level]);
3042 path->nodes[*level] = NULL;
3049 * helper for dropping snapshots. This walks back up the tree in the path
3050 * to find the first node higher up where we haven't yet gone through
3053 static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
3054 struct btrfs_root *root,
3055 struct btrfs_path *path, int *level)
3059 struct btrfs_root_item *root_item = &root->root_item;
3064 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
3065 slot = path->slots[i];
3066 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3067 struct extent_buffer *node;
3068 struct btrfs_disk_key disk_key;
3069 node = path->nodes[i];
3072 WARN_ON(*level == 0);
3073 btrfs_node_key(node, &disk_key, path->slots[i]);
3074 memcpy(&root_item->drop_progress,
3075 &disk_key, sizeof(disk_key));
3076 root_item->drop_level = i;
3079 struct extent_buffer *parent;
3080 if (path->nodes[*level] == root->node)
3081 parent = path->nodes[*level];
3083 parent = path->nodes[*level + 1];
3085 root_owner = btrfs_header_owner(parent);
3086 root_gen = btrfs_header_generation(parent);
3087 ret = btrfs_free_extent(trans, root,
3088 path->nodes[*level]->start,
3089 path->nodes[*level]->len,
3090 parent->start, root_owner,
3091 root_gen, *level, 1);
3093 free_extent_buffer(path->nodes[*level]);
3094 path->nodes[*level] = NULL;
3102 * drop the reference count on the tree rooted at 'snap'. This traverses
3103 * the tree freeing any blocks that have a ref count of zero after being
3106 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
3112 struct btrfs_path *path;
3115 struct btrfs_root_item *root_item = &root->root_item;
3117 path = btrfs_alloc_path();
3120 level = btrfs_header_level(root->node);
3122 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3123 path->nodes[level] = root->node;
3124 extent_buffer_get(root->node);
3125 path->slots[level] = 0;
3127 struct btrfs_key key;
3128 struct btrfs_disk_key found_key;
3129 struct extent_buffer *node;
3131 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
3132 level = root_item->drop_level;
3133 path->lowest_level = level;
3134 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3139 node = path->nodes[level];
3140 btrfs_node_key(node, &found_key, path->slots[level]);
3141 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3142 sizeof(found_key)));
3145 wret = walk_down_tree(trans, root, path, &level);
3151 wret = walk_up_tree(trans, root, path, &level);
3161 for (i = 0; i <= orig_level; i++) {
3162 if (path->nodes[i]) {
3163 free_extent_buffer(path->nodes[i]);
3164 path->nodes[i] = NULL;
3168 btrfs_free_path(path);
3174 int btrfs_free_block_groups(struct btrfs_fs_info *info)
3176 struct btrfs_space_info *sinfo;
3177 struct btrfs_block_group_cache *cache;
3184 ret = find_first_extent_bit(&info->block_group_cache, 0,
3185 &start, &end, (unsigned int)-1);
3188 ret = get_state_private(&info->block_group_cache, start, &ptr);
3190 cache = (struct btrfs_block_group_cache *)ptr;
3191 if (cache->free_space_ctl) {
3192 btrfs_remove_free_space_cache(cache);
3193 kfree(cache->free_space_ctl);
3197 clear_extent_bits(&info->block_group_cache, start,
3198 end, (unsigned int)-1, GFP_NOFS);
3201 ret = find_first_extent_bit(&info->free_space_cache, 0,
3202 &start, &end, EXTENT_DIRTY);
3205 clear_extent_dirty(&info->free_space_cache, start,
3209 while (!list_empty(&info->space_info)) {
3210 sinfo = list_entry(info->space_info.next,
3211 struct btrfs_space_info, list);
3212 list_del_init(&sinfo->list);
3218 int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
3219 struct btrfs_key *key)
3222 struct btrfs_key found_key;
3223 struct extent_buffer *leaf;
3226 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
3230 slot = path->slots[0];
3231 leaf = path->nodes[0];
3232 if (slot >= btrfs_header_nritems(leaf)) {
3233 ret = btrfs_next_leaf(root, path);
3240 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3242 if (found_key.objectid >= key->objectid &&
3243 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
3252 int btrfs_read_block_groups(struct btrfs_root *root)
3254 struct btrfs_path *path;
3257 struct btrfs_block_group_cache *cache;
3258 struct btrfs_fs_info *info = root->fs_info;
3259 struct btrfs_space_info *space_info;
3260 struct extent_io_tree *block_group_cache;
3261 struct btrfs_key key;
3262 struct btrfs_key found_key;
3263 struct extent_buffer *leaf;
3265 block_group_cache = &info->block_group_cache;
3267 root = info->extent_root;
3270 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3271 path = btrfs_alloc_path();
3276 ret = find_first_block_group(root, path, &key);
3284 leaf = path->nodes[0];
3285 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3286 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3292 read_extent_buffer(leaf, &cache->item,
3293 btrfs_item_ptr_offset(leaf, path->slots[0]),
3294 sizeof(cache->item));
3295 memcpy(&cache->key, &found_key, sizeof(found_key));
3298 key.objectid = found_key.objectid + found_key.offset;
3299 btrfs_release_path(root, path);
3300 cache->flags = btrfs_block_group_flags(&cache->item);
3302 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
3303 bit = BLOCK_GROUP_DATA;
3304 } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
3305 bit = BLOCK_GROUP_SYSTEM;
3306 } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
3307 bit = BLOCK_GROUP_METADATA;
3309 set_avail_alloc_bits(info, cache->flags);
3310 if (btrfs_chunk_readonly(root, cache->key.objectid))
3313 ret = update_space_info(info, cache->flags, found_key.offset,
3314 btrfs_block_group_used(&cache->item),
3317 cache->space_info = space_info;
3319 /* use EXTENT_LOCKED to prevent merging */
3320 set_extent_bits(block_group_cache, found_key.objectid,
3321 found_key.objectid + found_key.offset - 1,
3322 bit | EXTENT_LOCKED, GFP_NOFS);
3323 set_state_private(block_group_cache, found_key.objectid,
3324 (unsigned long)cache);
3328 btrfs_free_path(path);
3332 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
3333 struct btrfs_root *root, u64 bytes_used,
3334 u64 type, u64 chunk_objectid, u64 chunk_offset,
3339 struct btrfs_root *extent_root;
3340 struct btrfs_block_group_cache *cache;
3341 struct extent_io_tree *block_group_cache;
3343 extent_root = root->fs_info->extent_root;
3344 block_group_cache = &root->fs_info->block_group_cache;
3346 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3348 cache->key.objectid = chunk_offset;
3349 cache->key.offset = size;
3351 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3352 btrfs_set_block_group_used(&cache->item, bytes_used);
3353 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
3354 cache->flags = type;
3355 btrfs_set_block_group_flags(&cache->item, type);
3357 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
3358 &cache->space_info);
3361 bit = block_group_state_bits(type);
3362 set_extent_bits(block_group_cache, chunk_offset,
3363 chunk_offset + size - 1,
3364 bit | EXTENT_LOCKED, GFP_NOFS);
3366 set_state_private(block_group_cache, chunk_offset,
3367 (unsigned long)cache);
3368 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3369 sizeof(cache->item));
3372 finish_current_insert(trans, extent_root);
3373 ret = del_pending_extents(trans, extent_root);
3374 set_avail_alloc_bits(extent_root->fs_info, type);
3379 * This is for converter use only.
3381 * In that case, we don't know where are free blocks located.
3382 * Therefore all block group cache entries must be setup properly
3383 * before doing any block allocation.
3385 int btrfs_make_block_groups(struct btrfs_trans_handle *trans,
3386 struct btrfs_root *root)
3394 u64 total_metadata = 0;
3398 struct btrfs_root *extent_root;
3399 struct btrfs_block_group_cache *cache;
3400 struct extent_io_tree *block_group_cache;
3402 extent_root = root->fs_info->extent_root;
3403 block_group_cache = &root->fs_info->block_group_cache;
3404 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3405 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
3406 group_align = 64 * root->sectorsize;
3409 while (cur_start < total_bytes) {
3410 group_size = total_bytes / 12;
3411 group_size = min_t(u64, group_size, total_bytes - cur_start);
3412 if (cur_start == 0) {
3413 bit = BLOCK_GROUP_SYSTEM;
3414 group_type = BTRFS_BLOCK_GROUP_SYSTEM;
3416 group_size &= ~(group_align - 1);
3417 group_size = max_t(u64, group_size, 8 * 1024 * 1024);
3418 group_size = min_t(u64, group_size, 32 * 1024 * 1024);
3420 group_size &= ~(group_align - 1);
3421 if (total_data >= total_metadata * 2) {
3422 group_type = BTRFS_BLOCK_GROUP_METADATA;
3423 group_size = min_t(u64, group_size,
3424 1ULL * 1024 * 1024 * 1024);
3425 total_metadata += group_size;
3427 group_type = BTRFS_BLOCK_GROUP_DATA;
3428 group_size = min_t(u64, group_size,
3429 5ULL * 1024 * 1024 * 1024);
3430 total_data += group_size;
3432 if ((total_bytes - cur_start) * 4 < group_size * 5)
3433 group_size = total_bytes - cur_start;
3436 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3439 cache->key.objectid = cur_start;
3440 cache->key.offset = group_size;
3441 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3443 btrfs_set_block_group_used(&cache->item, 0);
3444 btrfs_set_block_group_chunk_objectid(&cache->item,
3446 btrfs_set_block_group_flags(&cache->item, group_type);
3448 cache->flags = group_type;
3450 ret = update_space_info(root->fs_info, group_type, group_size,
3451 0, &cache->space_info);
3453 set_avail_alloc_bits(extent_root->fs_info, group_type);
3455 set_extent_bits(block_group_cache, cur_start,
3456 cur_start + group_size - 1,
3457 bit | EXTENT_LOCKED, GFP_NOFS);
3458 set_state_private(block_group_cache, cur_start,
3459 (unsigned long)cache);
3460 cur_start += group_size;
3462 /* then insert all the items */
3464 while(cur_start < total_bytes) {
3465 cache = btrfs_lookup_block_group(root->fs_info, cur_start);
3468 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3469 sizeof(cache->item));
3472 finish_current_insert(trans, extent_root);
3473 ret = del_pending_extents(trans, extent_root);
3476 cur_start = cache->key.objectid + cache->key.offset;
3481 int btrfs_update_block_group(struct btrfs_trans_handle *trans,
3482 struct btrfs_root *root,
3483 u64 bytenr, u64 num_bytes, int alloc,
3486 return update_block_group(trans, root, bytenr, num_bytes,
3490 static int btrfs_count_extents_in_block_group(struct btrfs_root *root,
3491 struct btrfs_path *path, u64 start,
3495 struct btrfs_key key;
3496 struct extent_buffer *leaf;
3502 key.objectid = start;
3503 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
3504 ret = btrfs_search_slot(NULL, root->fs_info->extent_root,
3509 leaf = path->nodes[0];
3510 slot = path->slots[0];
3511 if (slot >= btrfs_header_nritems(leaf)) {
3512 ret = btrfs_next_leaf(root, path);
3517 leaf = path->nodes[0];
3518 slot = path->slots[0];
3520 btrfs_item_key_to_cpu(leaf, &key, slot);
3521 if (key.objectid > start + len)
3523 if (key.type == BTRFS_EXTENT_ITEM_KEY)
3524 bytes_used += key.offset;
3525 if (key.type == BTRFS_METADATA_ITEM_KEY)
3526 bytes_used += root->leafsize;
3529 *total = bytes_used;
3530 btrfs_release_path(root, path);
3534 int btrfs_check_block_accounting(struct btrfs_root *root)
3539 struct btrfs_path path;
3540 struct btrfs_block_group_cache *cache;
3541 struct btrfs_fs_info *fs_info = root->fs_info;
3543 btrfs_init_path(&path);
3546 cache = btrfs_lookup_block_group(fs_info, start);
3550 ret = btrfs_count_extents_in_block_group(root, &path,
3551 cache->key.objectid,
3556 u64 on_disk = btrfs_block_group_used(&cache->item);
3557 if (on_disk != bytes_used) {
3558 fprintf(stderr, "bad block group accounting found %llu "
3559 "expected %llu block group %llu\n",
3560 (unsigned long long)bytes_used,
3561 (unsigned long long)on_disk,
3562 (unsigned long long)cache->key.objectid);
3565 start = cache->key.objectid + cache->key.offset;
3567 cache->space_info->bytes_used = 0;
3573 * Fixup block accounting. The initial block accounting created by
3574 * make_block_groups isn't accuracy in this case.
3576 int btrfs_fix_block_accounting(struct btrfs_trans_handle *trans,
3577 struct btrfs_root *root)
3583 struct btrfs_path path;
3584 struct btrfs_key key;
3585 struct extent_buffer *leaf;
3586 struct btrfs_block_group_cache *cache;
3587 struct btrfs_fs_info *fs_info = root->fs_info;
3589 root = root->fs_info->extent_root;
3591 while(extent_root_pending_ops(fs_info)) {
3592 ret = finish_current_insert(trans, root);
3595 ret = del_pending_extents(trans, root);
3601 cache = btrfs_lookup_first_block_group(fs_info, start);
3604 start = cache->key.objectid + cache->key.offset;
3605 btrfs_set_block_group_used(&cache->item, 0);
3606 cache->space_info->bytes_used = 0;
3607 set_extent_bits(&root->fs_info->block_group_cache,
3608 cache->key.objectid,
3609 cache->key.objectid + cache->key.offset -1,
3610 BLOCK_GROUP_DIRTY, GFP_NOFS);
3613 btrfs_init_path(&path);
3616 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
3617 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
3622 leaf = path.nodes[0];
3623 slot = path.slots[0];
3624 if (slot >= btrfs_header_nritems(leaf)) {
3625 ret = btrfs_next_leaf(root, &path);
3630 leaf = path.nodes[0];
3631 slot = path.slots[0];
3633 btrfs_item_key_to_cpu(leaf, &key, slot);
3634 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
3635 bytes_used += key.offset;
3636 ret = btrfs_update_block_group(trans, root,
3637 key.objectid, key.offset, 1, 0);
3639 } else if (key.type == BTRFS_METADATA_ITEM_KEY) {
3640 bytes_used += root->leafsize;
3641 ret = btrfs_update_block_group(trans, root,
3642 key.objectid, root->leafsize, 1, 0);
3647 btrfs_set_super_bytes_used(root->fs_info->super_copy, bytes_used);
3648 btrfs_release_path(root, &path);