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.
22 #include "kerncompat.h"
23 #include "radix-tree.h"
26 #include "print-tree.h"
27 #include "transaction.h"
30 #include "free-space-cache.h"
33 #define PENDING_EXTENT_INSERT 0
34 #define PENDING_EXTENT_DELETE 1
35 #define PENDING_BACKREF_UPDATE 2
37 struct pending_extent_op {
42 struct btrfs_disk_key key;
46 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
47 struct btrfs_root *root,
48 u64 root_objectid, u64 generation,
49 u64 flags, struct btrfs_disk_key *key,
50 int level, struct btrfs_key *ins);
51 static int __free_extent(struct btrfs_trans_handle *trans,
52 struct btrfs_root *root,
53 u64 bytenr, u64 num_bytes, u64 parent,
54 u64 root_objectid, u64 owner_objectid,
55 u64 owner_offset, int refs_to_drop);
56 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
57 btrfs_root *extent_root);
58 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
59 btrfs_root *extent_root);
61 static int remove_sb_from_cache(struct btrfs_root *root,
62 struct btrfs_block_group_cache *cache)
68 struct extent_io_tree *free_space_cache;
70 free_space_cache = &root->fs_info->free_space_cache;
71 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
72 bytenr = btrfs_sb_offset(i);
73 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
74 cache->key.objectid, bytenr, 0,
75 &logical, &nr, &stripe_len);
78 clear_extent_dirty(free_space_cache, logical[nr],
79 logical[nr] + stripe_len - 1, GFP_NOFS);
86 static int cache_block_group(struct btrfs_root *root,
87 struct btrfs_block_group_cache *block_group)
89 struct btrfs_path *path;
92 struct extent_buffer *leaf;
93 struct extent_io_tree *free_space_cache;
101 root = root->fs_info->extent_root;
102 free_space_cache = &root->fs_info->free_space_cache;
104 if (block_group->cached)
107 path = btrfs_alloc_path();
112 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
117 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
122 leaf = path->nodes[0];
123 slot = path->slots[0];
124 if (slot >= btrfs_header_nritems(leaf)) {
125 ret = btrfs_next_leaf(root, path);
134 btrfs_item_key_to_cpu(leaf, &key, slot);
135 if (key.objectid < block_group->key.objectid) {
138 if (key.objectid >= block_group->key.objectid +
139 block_group->key.offset) {
143 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
144 key.type == BTRFS_METADATA_ITEM_KEY) {
145 if (key.objectid > last) {
146 hole_size = key.objectid - last;
147 set_extent_dirty(free_space_cache, last,
148 last + hole_size - 1,
151 if (key.type == BTRFS_METADATA_ITEM_KEY)
152 last = key.objectid + root->leafsize;
154 last = key.objectid + key.offset;
160 if (block_group->key.objectid +
161 block_group->key.offset > last) {
162 hole_size = block_group->key.objectid +
163 block_group->key.offset - last;
164 set_extent_dirty(free_space_cache, last,
165 last + hole_size - 1, GFP_NOFS);
167 remove_sb_from_cache(root, block_group);
168 block_group->cached = 1;
170 btrfs_free_path(path);
174 struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
178 struct extent_io_tree *block_group_cache;
179 struct btrfs_block_group_cache *block_group = NULL;
185 bytenr = max_t(u64, bytenr,
186 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
187 block_group_cache = &info->block_group_cache;
188 ret = find_first_extent_bit(block_group_cache,
189 bytenr, &start, &end,
190 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
195 ret = get_state_private(block_group_cache, start, &ptr);
199 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
203 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
207 struct extent_io_tree *block_group_cache;
208 struct btrfs_block_group_cache *block_group = NULL;
214 block_group_cache = &info->block_group_cache;
215 ret = find_first_extent_bit(block_group_cache,
216 bytenr, &start, &end,
217 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
222 ret = get_state_private(block_group_cache, start, &ptr);
226 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
227 if (block_group->key.objectid <= bytenr && bytenr <
228 block_group->key.objectid + block_group->key.offset)
233 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
235 return (cache->flags & bits) == bits;
238 static int noinline find_search_start(struct btrfs_root *root,
239 struct btrfs_block_group_cache **cache_ret,
240 u64 *start_ret, int num, int data)
243 struct btrfs_block_group_cache *cache = *cache_ret;
244 u64 last = *start_ret;
247 u64 search_start = *start_ret;
253 ret = cache_block_group(root, cache);
257 last = max(search_start, cache->key.objectid);
258 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) {
281 cache = btrfs_lookup_block_group(root->fs_info, search_start);
283 printk("Unable to find block group for %llu\n",
284 (unsigned long long)search_start);
290 last = cache->key.objectid + cache->key.offset;
292 cache = btrfs_lookup_first_block_group(root->fs_info, last);
305 static int block_group_state_bits(u64 flags)
308 if (flags & BTRFS_BLOCK_GROUP_DATA)
309 bits |= BLOCK_GROUP_DATA;
310 if (flags & BTRFS_BLOCK_GROUP_METADATA)
311 bits |= BLOCK_GROUP_METADATA;
312 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
313 bits |= BLOCK_GROUP_SYSTEM;
317 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
318 struct btrfs_block_group_cache
319 *hint, u64 search_start,
322 struct btrfs_block_group_cache *cache;
323 struct extent_io_tree *block_group_cache;
324 struct btrfs_block_group_cache *found_group = NULL;
325 struct btrfs_fs_info *info = root->fs_info;
338 block_group_cache = &info->block_group_cache;
343 bit = block_group_state_bits(data);
346 struct btrfs_block_group_cache *shint;
347 shint = btrfs_lookup_block_group(info, search_start);
348 if (shint && !shint->ro && block_group_bits(shint, data)) {
349 used = btrfs_block_group_used(&shint->item);
350 if (used + shint->pinned <
351 div_factor(shint->key.offset, factor)) {
356 if (hint && !hint->ro && block_group_bits(hint, data)) {
357 used = btrfs_block_group_used(&hint->item);
358 if (used + hint->pinned <
359 div_factor(hint->key.offset, factor)) {
362 last = hint->key.objectid + hint->key.offset;
366 hint_last = max(hint->key.objectid, search_start);
368 hint_last = search_start;
374 ret = find_first_extent_bit(block_group_cache, last,
379 ret = get_state_private(block_group_cache, start, &ptr);
383 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
384 last = cache->key.objectid + cache->key.offset;
385 used = btrfs_block_group_used(&cache->item);
387 if (!cache->ro && block_group_bits(cache, data)) {
389 free_check = cache->key.offset;
391 free_check = div_factor(cache->key.offset,
394 if (used + cache->pinned < free_check) {
411 * Back reference rules. Back refs have three main goals:
413 * 1) differentiate between all holders of references to an extent so that
414 * when a reference is dropped we can make sure it was a valid reference
415 * before freeing the extent.
417 * 2) Provide enough information to quickly find the holders of an extent
418 * if we notice a given block is corrupted or bad.
420 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
421 * maintenance. This is actually the same as #2, but with a slightly
422 * different use case.
424 * There are two kinds of back refs. The implicit back refs is optimized
425 * for pointers in non-shared tree blocks. For a given pointer in a block,
426 * back refs of this kind provide information about the block's owner tree
427 * and the pointer's key. These information allow us to find the block by
428 * b-tree searching. The full back refs is for pointers in tree blocks not
429 * referenced by their owner trees. The location of tree block is recorded
430 * in the back refs. Actually the full back refs is generic, and can be
431 * used in all cases the implicit back refs is used. The major shortcoming
432 * of the full back refs is its overhead. Every time a tree block gets
433 * COWed, we have to update back refs entry for all pointers in it.
435 * For a newly allocated tree block, we use implicit back refs for
436 * pointers in it. This means most tree related operations only involve
437 * implicit back refs. For a tree block created in old transaction, the
438 * only way to drop a reference to it is COW it. So we can detect the
439 * event that tree block loses its owner tree's reference and do the
440 * back refs conversion.
442 * When a tree block is COW'd through a tree, there are four cases:
444 * The reference count of the block is one and the tree is the block's
445 * owner tree. Nothing to do in this case.
447 * The reference count of the block is one and the tree is not the
448 * block's owner tree. In this case, full back refs is used for pointers
449 * in the block. Remove these full back refs, add implicit back refs for
450 * every pointers in the new block.
452 * The reference count of the block is greater than one and the tree is
453 * the block's owner tree. In this case, implicit back refs is used for
454 * pointers in the block. Add full back refs for every pointers in the
455 * block, increase lower level extents' reference counts. The original
456 * implicit back refs are entailed to the new block.
458 * The reference count of the block is greater than one and the tree is
459 * not the block's owner tree. Add implicit back refs for every pointer in
460 * the new block, increase lower level extents' reference count.
462 * Back Reference Key composing:
464 * The key objectid corresponds to the first byte in the extent,
465 * The key type is used to differentiate between types of back refs.
466 * There are different meanings of the key offset for different types
469 * File extents can be referenced by:
471 * - multiple snapshots, subvolumes, or different generations in one subvol
472 * - different files inside a single subvolume
473 * - different offsets inside a file (bookend extents in file.c)
475 * The extent ref structure for the implicit back refs has fields for:
477 * - Objectid of the subvolume root
478 * - objectid of the file holding the reference
479 * - original offset in the file
480 * - how many bookend extents
482 * The key offset for the implicit back refs is hash of the first
485 * The extent ref structure for the full back refs has field for:
487 * - number of pointers in the tree leaf
489 * The key offset for the implicit back refs is the first byte of
492 * When a file extent is allocated, The implicit back refs is used.
493 * the fields are filled in:
495 * (root_key.objectid, inode objectid, offset in file, 1)
497 * When a file extent is removed file truncation, we find the
498 * corresponding implicit back refs and check the following fields:
500 * (btrfs_header_owner(leaf), inode objectid, offset in file)
502 * Btree extents can be referenced by:
504 * - Different subvolumes
506 * Both the implicit back refs and the full back refs for tree blocks
507 * only consist of key. The key offset for the implicit back refs is
508 * objectid of block's owner tree. The key offset for the full back refs
509 * is the first byte of parent block.
511 * When implicit back refs is used, information about the lowest key and
512 * level of the tree block are required. These information are stored in
513 * tree block info structure.
516 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
517 static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
518 struct btrfs_root *root,
519 struct btrfs_path *path,
520 u64 owner, u32 extra_size)
522 struct btrfs_extent_item *item;
523 struct btrfs_extent_item_v0 *ei0;
524 struct btrfs_extent_ref_v0 *ref0;
525 struct btrfs_tree_block_info *bi;
526 struct extent_buffer *leaf;
527 struct btrfs_key key;
528 struct btrfs_key found_key;
529 u32 new_size = sizeof(*item);
533 leaf = path->nodes[0];
534 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
536 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
537 ei0 = btrfs_item_ptr(leaf, path->slots[0],
538 struct btrfs_extent_item_v0);
539 refs = btrfs_extent_refs_v0(leaf, ei0);
541 if (owner == (u64)-1) {
543 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
544 ret = btrfs_next_leaf(root, path);
548 leaf = path->nodes[0];
550 btrfs_item_key_to_cpu(leaf, &found_key,
552 BUG_ON(key.objectid != found_key.objectid);
553 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
557 ref0 = btrfs_item_ptr(leaf, path->slots[0],
558 struct btrfs_extent_ref_v0);
559 owner = btrfs_ref_objectid_v0(leaf, ref0);
563 btrfs_release_path(root, path);
565 if (owner < BTRFS_FIRST_FREE_OBJECTID)
566 new_size += sizeof(*bi);
568 new_size -= sizeof(*ei0);
569 ret = btrfs_search_slot(trans, root, &key, path, new_size, 1);
574 ret = btrfs_extend_item(trans, root, path, new_size);
577 leaf = path->nodes[0];
578 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
579 btrfs_set_extent_refs(leaf, item, refs);
580 /* FIXME: get real generation */
581 btrfs_set_extent_generation(leaf, item, 0);
582 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
583 btrfs_set_extent_flags(leaf, item,
584 BTRFS_EXTENT_FLAG_TREE_BLOCK |
585 BTRFS_BLOCK_FLAG_FULL_BACKREF);
586 bi = (struct btrfs_tree_block_info *)(item + 1);
587 /* FIXME: get first key of the block */
588 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
589 btrfs_set_tree_block_level(leaf, bi, (int)owner);
591 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
593 btrfs_mark_buffer_dirty(leaf);
598 static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
600 u32 high_crc = ~(u32)0;
601 u32 low_crc = ~(u32)0;
604 lenum = cpu_to_le64(root_objectid);
605 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
606 lenum = cpu_to_le64(owner);
607 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
608 lenum = cpu_to_le64(offset);
609 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
611 return ((u64)high_crc << 31) ^ (u64)low_crc;
614 static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
615 struct btrfs_extent_data_ref *ref)
617 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
618 btrfs_extent_data_ref_objectid(leaf, ref),
619 btrfs_extent_data_ref_offset(leaf, ref));
622 static int match_extent_data_ref(struct extent_buffer *leaf,
623 struct btrfs_extent_data_ref *ref,
624 u64 root_objectid, u64 owner, u64 offset)
626 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
627 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
628 btrfs_extent_data_ref_offset(leaf, ref) != offset)
633 static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
634 struct btrfs_root *root,
635 struct btrfs_path *path,
636 u64 bytenr, u64 parent,
638 u64 owner, u64 offset)
640 struct btrfs_key key;
641 struct btrfs_extent_data_ref *ref;
642 struct extent_buffer *leaf;
648 key.objectid = bytenr;
650 key.type = BTRFS_SHARED_DATA_REF_KEY;
653 key.type = BTRFS_EXTENT_DATA_REF_KEY;
654 key.offset = hash_extent_data_ref(root_objectid,
659 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
668 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
669 key.type = BTRFS_EXTENT_REF_V0_KEY;
670 btrfs_release_path(root, path);
671 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
682 leaf = path->nodes[0];
683 nritems = btrfs_header_nritems(leaf);
685 if (path->slots[0] >= nritems) {
686 ret = btrfs_next_leaf(root, path);
692 leaf = path->nodes[0];
693 nritems = btrfs_header_nritems(leaf);
697 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
698 if (key.objectid != bytenr ||
699 key.type != BTRFS_EXTENT_DATA_REF_KEY)
702 ref = btrfs_item_ptr(leaf, path->slots[0],
703 struct btrfs_extent_data_ref);
705 if (match_extent_data_ref(leaf, ref, root_objectid,
708 btrfs_release_path(root, path);
720 static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
721 struct btrfs_root *root,
722 struct btrfs_path *path,
723 u64 bytenr, u64 parent,
724 u64 root_objectid, u64 owner,
725 u64 offset, int refs_to_add)
727 struct btrfs_key key;
728 struct extent_buffer *leaf;
733 key.objectid = bytenr;
735 key.type = BTRFS_SHARED_DATA_REF_KEY;
737 size = sizeof(struct btrfs_shared_data_ref);
739 key.type = BTRFS_EXTENT_DATA_REF_KEY;
740 key.offset = hash_extent_data_ref(root_objectid,
742 size = sizeof(struct btrfs_extent_data_ref);
745 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
746 if (ret && ret != -EEXIST)
749 leaf = path->nodes[0];
751 struct btrfs_shared_data_ref *ref;
752 ref = btrfs_item_ptr(leaf, path->slots[0],
753 struct btrfs_shared_data_ref);
755 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
757 num_refs = btrfs_shared_data_ref_count(leaf, ref);
758 num_refs += refs_to_add;
759 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
762 struct btrfs_extent_data_ref *ref;
763 while (ret == -EEXIST) {
764 ref = btrfs_item_ptr(leaf, path->slots[0],
765 struct btrfs_extent_data_ref);
766 if (match_extent_data_ref(leaf, ref, root_objectid,
769 btrfs_release_path(root, path);
772 ret = btrfs_insert_empty_item(trans, root, path, &key,
774 if (ret && ret != -EEXIST)
777 leaf = path->nodes[0];
779 ref = btrfs_item_ptr(leaf, path->slots[0],
780 struct btrfs_extent_data_ref);
782 btrfs_set_extent_data_ref_root(leaf, ref,
784 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
785 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
786 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
788 num_refs = btrfs_extent_data_ref_count(leaf, ref);
789 num_refs += refs_to_add;
790 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
793 btrfs_mark_buffer_dirty(leaf);
796 btrfs_release_path(root, path);
800 static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
801 struct btrfs_root *root,
802 struct btrfs_path *path,
805 struct btrfs_key key;
806 struct btrfs_extent_data_ref *ref1 = NULL;
807 struct btrfs_shared_data_ref *ref2 = NULL;
808 struct extent_buffer *leaf;
812 leaf = path->nodes[0];
813 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
815 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
816 ref1 = btrfs_item_ptr(leaf, path->slots[0],
817 struct btrfs_extent_data_ref);
818 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
819 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
820 ref2 = btrfs_item_ptr(leaf, path->slots[0],
821 struct btrfs_shared_data_ref);
822 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
823 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
824 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
825 struct btrfs_extent_ref_v0 *ref0;
826 ref0 = btrfs_item_ptr(leaf, path->slots[0],
827 struct btrfs_extent_ref_v0);
828 num_refs = btrfs_ref_count_v0(leaf, ref0);
834 BUG_ON(num_refs < refs_to_drop);
835 num_refs -= refs_to_drop;
838 ret = btrfs_del_item(trans, root, path);
840 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
841 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
842 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
843 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
844 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
846 struct btrfs_extent_ref_v0 *ref0;
847 ref0 = btrfs_item_ptr(leaf, path->slots[0],
848 struct btrfs_extent_ref_v0);
849 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
852 btrfs_mark_buffer_dirty(leaf);
857 static noinline u32 extent_data_ref_count(struct btrfs_root *root,
858 struct btrfs_path *path,
859 struct btrfs_extent_inline_ref *iref)
861 struct btrfs_key key;
862 struct extent_buffer *leaf;
863 struct btrfs_extent_data_ref *ref1;
864 struct btrfs_shared_data_ref *ref2;
867 leaf = path->nodes[0];
868 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
870 if (btrfs_extent_inline_ref_type(leaf, iref) ==
871 BTRFS_EXTENT_DATA_REF_KEY) {
872 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
873 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
875 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
876 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
878 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
879 ref1 = btrfs_item_ptr(leaf, path->slots[0],
880 struct btrfs_extent_data_ref);
881 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
882 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
883 ref2 = btrfs_item_ptr(leaf, path->slots[0],
884 struct btrfs_shared_data_ref);
885 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
886 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
887 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
888 struct btrfs_extent_ref_v0 *ref0;
889 ref0 = btrfs_item_ptr(leaf, path->slots[0],
890 struct btrfs_extent_ref_v0);
891 num_refs = btrfs_ref_count_v0(leaf, ref0);
899 static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
900 struct btrfs_root *root,
901 struct btrfs_path *path,
902 u64 bytenr, u64 parent,
905 struct btrfs_key key;
908 key.objectid = bytenr;
910 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
913 key.type = BTRFS_TREE_BLOCK_REF_KEY;
914 key.offset = root_objectid;
917 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
920 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
921 if (ret == -ENOENT && parent) {
922 btrfs_release_path(root, path);
923 key.type = BTRFS_EXTENT_REF_V0_KEY;
924 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
932 static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
933 struct btrfs_root *root,
934 struct btrfs_path *path,
935 u64 bytenr, u64 parent,
938 struct btrfs_key key;
941 key.objectid = bytenr;
943 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
946 key.type = BTRFS_TREE_BLOCK_REF_KEY;
947 key.offset = root_objectid;
950 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
952 btrfs_release_path(root, path);
956 static inline int extent_ref_type(u64 parent, u64 owner)
959 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
961 type = BTRFS_SHARED_BLOCK_REF_KEY;
963 type = BTRFS_TREE_BLOCK_REF_KEY;
966 type = BTRFS_SHARED_DATA_REF_KEY;
968 type = BTRFS_EXTENT_DATA_REF_KEY;
973 static int find_next_key(struct btrfs_path *path, struct btrfs_key *key)
977 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
978 if (!path->nodes[level])
980 if (path->slots[level] + 1 >=
981 btrfs_header_nritems(path->nodes[level]))
984 btrfs_item_key_to_cpu(path->nodes[level], key,
985 path->slots[level] + 1);
987 btrfs_node_key_to_cpu(path->nodes[level], key,
988 path->slots[level] + 1);
994 static int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
995 struct btrfs_root *root,
996 struct btrfs_path *path,
997 struct btrfs_extent_inline_ref **ref_ret,
998 u64 bytenr, u64 num_bytes,
999 u64 parent, u64 root_objectid,
1000 u64 owner, u64 offset, int insert)
1002 struct btrfs_key key;
1003 struct extent_buffer *leaf;
1004 struct btrfs_extent_item *ei;
1005 struct btrfs_extent_inline_ref *iref;
1015 int skinny_metadata =
1016 btrfs_fs_incompat(root->fs_info,
1017 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
1019 key.objectid = bytenr;
1020 key.type = BTRFS_EXTENT_ITEM_KEY;
1021 key.offset = num_bytes;
1023 want = extent_ref_type(parent, owner);
1025 extra_size = btrfs_extent_inline_ref_size(want);
1029 if (owner < BTRFS_FIRST_FREE_OBJECTID && skinny_metadata) {
1030 skinny_metadata = 1;
1031 key.type = BTRFS_METADATA_ITEM_KEY;
1033 } else if (skinny_metadata) {
1034 skinny_metadata = 0;
1038 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1045 * We may be a newly converted file system which still has the old fat
1046 * extent entries for metadata, so try and see if we have one of those.
1048 if (ret > 0 && skinny_metadata) {
1049 skinny_metadata = 0;
1050 if (path->slots[0]) {
1052 btrfs_item_key_to_cpu(path->nodes[0], &key,
1054 if (key.objectid == bytenr &&
1055 key.type == BTRFS_EXTENT_ITEM_KEY &&
1056 key.offset == num_bytes)
1060 key.type = BTRFS_EXTENT_ITEM_KEY;
1061 key.offset = num_bytes;
1067 printf("Failed to find [%llu, %u, %llu]\n", key.objectid, key.type, key.offset);
1073 leaf = path->nodes[0];
1074 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1075 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1076 if (item_size < sizeof(*ei)) {
1081 ret = convert_extent_item_v0(trans, root, path, owner,
1087 leaf = path->nodes[0];
1088 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1091 if (item_size < sizeof(*ei)) {
1092 printf("Size is %u, needs to be %u, slot %d\n",
1093 (unsigned)item_size,
1094 (unsigned)sizeof(*ei), path->slots[0]);
1095 btrfs_print_leaf(root, leaf);
1098 BUG_ON(item_size < sizeof(*ei));
1100 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1101 flags = btrfs_extent_flags(leaf, ei);
1103 ptr = (unsigned long)(ei + 1);
1104 end = (unsigned long)ei + item_size;
1106 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
1107 ptr += sizeof(struct btrfs_tree_block_info);
1109 } else if (!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)) {
1110 if (!(flags & BTRFS_EXTENT_FLAG_DATA)) {
1121 iref = (struct btrfs_extent_inline_ref *)ptr;
1122 type = btrfs_extent_inline_ref_type(leaf, iref);
1126 ptr += btrfs_extent_inline_ref_size(type);
1130 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1131 struct btrfs_extent_data_ref *dref;
1132 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1133 if (match_extent_data_ref(leaf, dref, root_objectid,
1138 if (hash_extent_data_ref_item(leaf, dref) <
1139 hash_extent_data_ref(root_objectid, owner, offset))
1143 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1145 if (parent == ref_offset) {
1149 if (ref_offset < parent)
1152 if (root_objectid == ref_offset) {
1156 if (ref_offset < root_objectid)
1160 ptr += btrfs_extent_inline_ref_size(type);
1162 if (err == -ENOENT && insert) {
1163 if (item_size + extra_size >=
1164 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1169 * To add new inline back ref, we have to make sure
1170 * there is no corresponding back ref item.
1171 * For simplicity, we just do not add new inline back
1172 * ref if there is any back ref item.
1174 if (find_next_key(path, &key) == 0 && key.objectid == bytenr &&
1175 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
1180 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1185 static int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1186 struct btrfs_root *root,
1187 struct btrfs_path *path,
1188 struct btrfs_extent_inline_ref *iref,
1189 u64 parent, u64 root_objectid,
1190 u64 owner, u64 offset, int refs_to_add)
1192 struct extent_buffer *leaf;
1193 struct btrfs_extent_item *ei;
1196 unsigned long item_offset;
1202 leaf = path->nodes[0];
1203 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1204 item_offset = (unsigned long)iref - (unsigned long)ei;
1206 type = extent_ref_type(parent, owner);
1207 size = btrfs_extent_inline_ref_size(type);
1209 ret = btrfs_extend_item(trans, root, path, size);
1212 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1213 refs = btrfs_extent_refs(leaf, ei);
1214 refs += refs_to_add;
1215 btrfs_set_extent_refs(leaf, ei, refs);
1217 ptr = (unsigned long)ei + item_offset;
1218 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1219 if (ptr < end - size)
1220 memmove_extent_buffer(leaf, ptr + size, ptr,
1223 iref = (struct btrfs_extent_inline_ref *)ptr;
1224 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1225 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1226 struct btrfs_extent_data_ref *dref;
1227 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1228 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1229 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1230 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1231 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1232 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1233 struct btrfs_shared_data_ref *sref;
1234 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1235 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1236 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1237 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1238 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1240 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1242 btrfs_mark_buffer_dirty(leaf);
1246 static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1247 struct btrfs_root *root,
1248 struct btrfs_path *path,
1249 struct btrfs_extent_inline_ref **ref_ret,
1250 u64 bytenr, u64 num_bytes, u64 parent,
1251 u64 root_objectid, u64 owner, u64 offset)
1255 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1256 bytenr, num_bytes, parent,
1257 root_objectid, owner, offset, 0);
1261 btrfs_release_path(root, path);
1264 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1265 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1268 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1269 root_objectid, owner, offset);
1274 static int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1275 struct btrfs_root *root,
1276 struct btrfs_path *path,
1277 struct btrfs_extent_inline_ref *iref,
1280 struct extent_buffer *leaf;
1281 struct btrfs_extent_item *ei;
1282 struct btrfs_extent_data_ref *dref = NULL;
1283 struct btrfs_shared_data_ref *sref = NULL;
1292 leaf = path->nodes[0];
1293 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1294 refs = btrfs_extent_refs(leaf, ei);
1295 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1296 refs += refs_to_mod;
1297 btrfs_set_extent_refs(leaf, ei, refs);
1299 type = btrfs_extent_inline_ref_type(leaf, iref);
1301 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1302 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1303 refs = btrfs_extent_data_ref_count(leaf, dref);
1304 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1305 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1306 refs = btrfs_shared_data_ref_count(leaf, sref);
1309 BUG_ON(refs_to_mod != -1);
1312 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1313 refs += refs_to_mod;
1316 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1317 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1319 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1321 size = btrfs_extent_inline_ref_size(type);
1322 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1323 ptr = (unsigned long)iref;
1324 end = (unsigned long)ei + item_size;
1325 if (ptr + size < end)
1326 memmove_extent_buffer(leaf, ptr, ptr + size,
1329 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1332 btrfs_mark_buffer_dirty(leaf);
1336 static int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1337 struct btrfs_root *root,
1338 struct btrfs_path *path,
1339 u64 bytenr, u64 num_bytes, u64 parent,
1340 u64 root_objectid, u64 owner,
1341 u64 offset, int refs_to_add)
1343 struct btrfs_extent_inline_ref *iref;
1346 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1347 bytenr, num_bytes, parent,
1348 root_objectid, owner, offset, 1);
1350 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1351 ret = update_inline_extent_backref(trans, root, path, iref,
1353 } else if (ret == -ENOENT) {
1354 ret = setup_inline_extent_backref(trans, root, path, iref,
1355 parent, root_objectid,
1356 owner, offset, refs_to_add);
1361 static int insert_extent_backref(struct btrfs_trans_handle *trans,
1362 struct btrfs_root *root,
1363 struct btrfs_path *path,
1364 u64 bytenr, u64 parent, u64 root_objectid,
1365 u64 owner, u64 offset, int refs_to_add)
1369 if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
1370 ret = insert_extent_data_ref(trans, root, path, bytenr,
1371 parent, root_objectid,
1372 owner, offset, refs_to_add);
1374 BUG_ON(refs_to_add != 1);
1375 ret = insert_tree_block_ref(trans, root, path, bytenr,
1376 parent, root_objectid);
1381 static int remove_extent_backref(struct btrfs_trans_handle *trans,
1382 struct btrfs_root *root,
1383 struct btrfs_path *path,
1384 struct btrfs_extent_inline_ref *iref,
1385 int refs_to_drop, int is_data)
1389 BUG_ON(!is_data && refs_to_drop != 1);
1391 ret = update_inline_extent_backref(trans, root, path, iref,
1393 } else if (is_data) {
1394 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1396 ret = btrfs_del_item(trans, root, path);
1401 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1402 struct btrfs_root *root,
1403 u64 bytenr, u64 num_bytes, u64 parent,
1404 u64 root_objectid, u64 owner, u64 offset)
1406 struct btrfs_path *path;
1407 struct extent_buffer *leaf;
1408 struct btrfs_extent_item *item;
1413 path = btrfs_alloc_path();
1418 path->leave_spinning = 1;
1420 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1421 path, bytenr, num_bytes, parent,
1422 root_objectid, owner, offset, 1);
1426 if (ret != -EAGAIN) {
1431 leaf = path->nodes[0];
1432 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1433 refs = btrfs_extent_refs(leaf, item);
1434 btrfs_set_extent_refs(leaf, item, refs + 1);
1436 btrfs_mark_buffer_dirty(leaf);
1437 btrfs_release_path(root->fs_info->extent_root, path);
1440 path->leave_spinning = 1;
1442 /* now insert the actual backref */
1443 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1444 path, bytenr, parent, root_objectid,
1449 btrfs_free_path(path);
1450 finish_current_insert(trans, root->fs_info->extent_root);
1451 del_pending_extents(trans, root->fs_info->extent_root);
1456 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1457 struct btrfs_root *root)
1459 finish_current_insert(trans, root->fs_info->extent_root);
1460 del_pending_extents(trans, root->fs_info->extent_root);
1464 int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
1465 struct btrfs_root *root, u64 bytenr,
1466 u64 offset, int metadata, u64 *refs, u64 *flags)
1468 struct btrfs_path *path;
1470 struct btrfs_key key;
1471 struct extent_buffer *l;
1472 struct btrfs_extent_item *item;
1478 !btrfs_fs_incompat(root->fs_info,
1479 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA)) {
1480 offset = root->leafsize;
1484 path = btrfs_alloc_path();
1487 key.objectid = bytenr;
1488 key.offset = offset;
1490 key.type = BTRFS_METADATA_ITEM_KEY;
1492 key.type = BTRFS_EXTENT_ITEM_KEY;
1495 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1501 * Deal with the fact that we may have mixed SKINNY and normal refs. If
1502 * we didn't find what we wanted check and see if we have a normal ref
1503 * right next to us, or re-search if we are on the edge of the leaf just
1506 if (ret > 0 && metadata) {
1509 btrfs_item_key_to_cpu(path->nodes[0], &key,
1511 if (key.objectid == bytenr &&
1512 key.type == BTRFS_METADATA_ITEM_KEY)
1517 btrfs_release_path(root, path);
1518 key.type = BTRFS_EXTENT_ITEM_KEY;
1519 key.offset = root->leafsize;
1531 item_size = btrfs_item_size_nr(l, path->slots[0]);
1532 if (item_size >= sizeof(*item)) {
1533 item = btrfs_item_ptr(l, path->slots[0],
1534 struct btrfs_extent_item);
1535 num_refs = btrfs_extent_refs(l, item);
1536 extent_flags = btrfs_extent_flags(l, item);
1538 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1539 struct btrfs_extent_item_v0 *ei0;
1540 BUG_ON(item_size != sizeof(*ei0));
1541 ei0 = btrfs_item_ptr(l, path->slots[0],
1542 struct btrfs_extent_item_v0);
1543 num_refs = btrfs_extent_refs_v0(l, ei0);
1544 /* FIXME: this isn't correct for data */
1545 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
1550 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1554 *flags = extent_flags;
1556 btrfs_free_path(path);
1560 int btrfs_set_block_flags(struct btrfs_trans_handle *trans,
1561 struct btrfs_root *root,
1562 u64 bytenr, int level, u64 flags)
1564 struct btrfs_path *path;
1566 struct btrfs_key key;
1567 struct extent_buffer *l;
1568 struct btrfs_extent_item *item;
1570 int skinny_metadata =
1571 btrfs_fs_incompat(root->fs_info,
1572 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
1574 path = btrfs_alloc_path();
1577 key.objectid = bytenr;
1578 if (skinny_metadata) {
1580 key.type = BTRFS_METADATA_ITEM_KEY;
1582 key.offset = root->leafsize;
1583 key.type = BTRFS_EXTENT_ITEM_KEY;
1587 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
1592 if (ret > 0 && skinny_metadata) {
1593 skinny_metadata = 0;
1594 if (path->slots[0]--) {
1596 btrfs_item_key_to_cpu(path->nodes[0], &key,
1598 if (key.objectid == bytenr &&
1599 key.offset == root->leafsize &&
1600 key.type == BTRFS_EXTENT_ITEM_KEY)
1604 btrfs_release_path(root, path);
1605 key.offset = root->leafsize;
1606 key.type = BTRFS_EXTENT_ITEM_KEY;
1612 btrfs_print_leaf(root, path->nodes[0]);
1613 printk("failed to find block number %Lu\n",
1614 (unsigned long long)bytenr);
1618 item_size = btrfs_item_size_nr(l, path->slots[0]);
1619 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1620 if (item_size < sizeof(*item)) {
1621 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1627 item_size = btrfs_item_size_nr(l, path->slots[0]);
1630 BUG_ON(item_size < sizeof(*item));
1631 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
1632 flags |= btrfs_extent_flags(l, item);
1633 btrfs_set_extent_flags(l, item, flags);
1635 btrfs_free_path(path);
1636 finish_current_insert(trans, root->fs_info->extent_root);
1637 del_pending_extents(trans, root->fs_info->extent_root);
1641 static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
1642 struct btrfs_root *root,
1643 struct extent_buffer *buf,
1644 int record_parent, int inc)
1651 struct btrfs_key key;
1652 struct btrfs_file_extent_item *fi;
1656 int (*process_func)(struct btrfs_trans_handle *trans,
1657 struct btrfs_root *root,
1658 u64, u64, u64, u64, u64, u64);
1660 ref_root = btrfs_header_owner(buf);
1661 nritems = btrfs_header_nritems(buf);
1662 level = btrfs_header_level(buf);
1664 if (!root->ref_cows && level == 0)
1668 process_func = btrfs_inc_extent_ref;
1670 process_func = btrfs_free_extent;
1673 parent = buf->start;
1677 for (i = 0; i < nritems; i++) {
1680 btrfs_item_key_to_cpu(buf, &key, i);
1681 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1683 fi = btrfs_item_ptr(buf, i,
1684 struct btrfs_file_extent_item);
1685 if (btrfs_file_extent_type(buf, fi) ==
1686 BTRFS_FILE_EXTENT_INLINE)
1688 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1692 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
1693 key.offset -= btrfs_file_extent_offset(buf, fi);
1694 ret = process_func(trans, root, bytenr, num_bytes,
1695 parent, ref_root, key.objectid,
1702 bytenr = btrfs_node_blockptr(buf, i);
1703 num_bytes = btrfs_level_size(root, level - 1);
1704 ret = process_func(trans, root, bytenr, num_bytes,
1705 parent, ref_root, level - 1, 0);
1718 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1719 struct extent_buffer *buf, int record_parent)
1721 return __btrfs_mod_ref(trans, root, buf, record_parent, 1);
1724 int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1725 struct extent_buffer *buf, int record_parent)
1727 return __btrfs_mod_ref(trans, root, buf, record_parent, 0);
1730 static int write_one_cache_group(struct btrfs_trans_handle *trans,
1731 struct btrfs_root *root,
1732 struct btrfs_path *path,
1733 struct btrfs_block_group_cache *cache)
1737 struct btrfs_root *extent_root = root->fs_info->extent_root;
1739 struct extent_buffer *leaf;
1741 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
1746 leaf = path->nodes[0];
1747 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1748 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1749 btrfs_mark_buffer_dirty(leaf);
1750 btrfs_release_path(extent_root, path);
1752 finish_current_insert(trans, extent_root);
1753 pending_ret = del_pending_extents(trans, extent_root);
1762 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1763 struct btrfs_root *root)
1765 struct extent_io_tree *block_group_cache;
1766 struct btrfs_block_group_cache *cache;
1768 struct btrfs_path *path;
1774 block_group_cache = &root->fs_info->block_group_cache;
1775 path = btrfs_alloc_path();
1780 ret = find_first_extent_bit(block_group_cache, last,
1781 &start, &end, BLOCK_GROUP_DIRTY);
1790 ret = get_state_private(block_group_cache, start, &ptr);
1793 clear_extent_bits(block_group_cache, start, end,
1794 BLOCK_GROUP_DIRTY, GFP_NOFS);
1796 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
1797 ret = write_one_cache_group(trans, root, path, cache);
1799 btrfs_free_path(path);
1803 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
1806 struct list_head *head = &info->space_info;
1807 struct list_head *cur;
1808 struct btrfs_space_info *found;
1809 list_for_each(cur, head) {
1810 found = list_entry(cur, struct btrfs_space_info, list);
1811 if (found->flags & flags)
1818 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1819 u64 total_bytes, u64 bytes_used,
1820 struct btrfs_space_info **space_info)
1822 struct btrfs_space_info *found;
1824 found = __find_space_info(info, flags);
1826 found->total_bytes += total_bytes;
1827 found->bytes_used += bytes_used;
1828 if (found->total_bytes < found->bytes_used) {
1829 fprintf(stderr, "warning, bad space info total_bytes "
1831 (unsigned long long)found->total_bytes,
1832 (unsigned long long)found->bytes_used);
1834 *space_info = found;
1837 found = kmalloc(sizeof(*found), GFP_NOFS);
1841 list_add(&found->list, &info->space_info);
1842 found->flags = flags;
1843 found->total_bytes = total_bytes;
1844 found->bytes_used = bytes_used;
1845 found->bytes_pinned = 0;
1847 *space_info = found;
1852 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1854 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
1855 BTRFS_BLOCK_GROUP_RAID1 |
1856 BTRFS_BLOCK_GROUP_RAID10 |
1857 BTRFS_BLOCK_GROUP_RAID5 |
1858 BTRFS_BLOCK_GROUP_RAID6 |
1859 BTRFS_BLOCK_GROUP_DUP);
1861 if (flags & BTRFS_BLOCK_GROUP_DATA)
1862 fs_info->avail_data_alloc_bits |= extra_flags;
1863 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1864 fs_info->avail_metadata_alloc_bits |= extra_flags;
1865 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1866 fs_info->avail_system_alloc_bits |= extra_flags;
1870 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1871 struct btrfs_root *extent_root, u64 alloc_bytes,
1874 struct btrfs_space_info *space_info;
1880 space_info = __find_space_info(extent_root->fs_info, flags);
1882 ret = update_space_info(extent_root->fs_info, flags,
1886 BUG_ON(!space_info);
1888 if (space_info->full)
1891 thresh = div_factor(space_info->total_bytes, 7);
1892 if ((space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) <
1896 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes,
1898 if (ret == -ENOSPC) {
1899 space_info->full = 1;
1905 ret = btrfs_make_block_group(trans, extent_root, 0, space_info->flags,
1906 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
1911 static int update_block_group(struct btrfs_trans_handle *trans,
1912 struct btrfs_root *root,
1913 u64 bytenr, u64 num_bytes, int alloc,
1916 struct btrfs_block_group_cache *cache;
1917 struct btrfs_fs_info *info = root->fs_info;
1918 u64 total = num_bytes;
1924 /* block accounting for super block */
1925 old_val = btrfs_super_bytes_used(info->super_copy);
1927 old_val += num_bytes;
1929 old_val -= num_bytes;
1930 btrfs_set_super_bytes_used(info->super_copy, old_val);
1932 /* block accounting for root item */
1933 old_val = btrfs_root_used(&root->root_item);
1935 old_val += num_bytes;
1937 old_val -= num_bytes;
1938 btrfs_set_root_used(&root->root_item, old_val);
1941 cache = btrfs_lookup_block_group(info, bytenr);
1945 byte_in_group = bytenr - cache->key.objectid;
1946 WARN_ON(byte_in_group > cache->key.offset);
1947 start = cache->key.objectid;
1948 end = start + cache->key.offset - 1;
1949 set_extent_bits(&info->block_group_cache, start, end,
1950 BLOCK_GROUP_DIRTY, GFP_NOFS);
1952 old_val = btrfs_block_group_used(&cache->item);
1953 num_bytes = min(total, cache->key.offset - byte_in_group);
1956 old_val += num_bytes;
1957 cache->space_info->bytes_used += num_bytes;
1959 old_val -= num_bytes;
1960 cache->space_info->bytes_used -= num_bytes;
1962 set_extent_dirty(&info->free_space_cache,
1963 bytenr, bytenr + num_bytes - 1,
1967 btrfs_set_block_group_used(&cache->item, old_val);
1969 bytenr += num_bytes;
1974 static int update_pinned_extents(struct btrfs_root *root,
1975 u64 bytenr, u64 num, int pin)
1978 struct btrfs_block_group_cache *cache;
1979 struct btrfs_fs_info *fs_info = root->fs_info;
1982 set_extent_dirty(&fs_info->pinned_extents,
1983 bytenr, bytenr + num - 1, GFP_NOFS);
1985 clear_extent_dirty(&fs_info->pinned_extents,
1986 bytenr, bytenr + num - 1, GFP_NOFS);
1989 cache = btrfs_lookup_block_group(fs_info, bytenr);
1991 len = min((u64)root->sectorsize, num);
1995 len = min(num, cache->key.offset -
1996 (bytenr - cache->key.objectid));
1998 cache->pinned += len;
1999 cache->space_info->bytes_pinned += len;
2000 fs_info->total_pinned += len;
2002 cache->pinned -= len;
2003 cache->space_info->bytes_pinned -= len;
2004 fs_info->total_pinned -= len;
2013 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
2018 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
2022 ret = find_first_extent_bit(pinned_extents, last,
2023 &start, &end, EXTENT_DIRTY);
2026 set_extent_dirty(copy, start, end, GFP_NOFS);
2032 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2033 struct btrfs_root *root,
2034 struct extent_io_tree *unpin)
2039 struct extent_io_tree *free_space_cache;
2040 free_space_cache = &root->fs_info->free_space_cache;
2043 ret = find_first_extent_bit(unpin, 0, &start, &end,
2047 update_pinned_extents(root, start, end + 1 - start, 0);
2048 clear_extent_dirty(unpin, start, end, GFP_NOFS);
2049 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
2054 static int extent_root_pending_ops(struct btrfs_fs_info *info)
2060 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
2061 &end, EXTENT_LOCKED);
2063 ret = find_first_extent_bit(&info->pending_del, 0, &start, &end,
2069 static int finish_current_insert(struct btrfs_trans_handle *trans,
2070 struct btrfs_root *extent_root)
2075 struct btrfs_fs_info *info = extent_root->fs_info;
2076 struct btrfs_path *path;
2077 struct pending_extent_op *extent_op;
2078 struct btrfs_key key;
2080 int skinny_metadata =
2081 btrfs_fs_incompat(extent_root->fs_info,
2082 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
2084 path = btrfs_alloc_path();
2087 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
2088 &end, EXTENT_LOCKED);
2092 ret = get_state_private(&info->extent_ins, start, &priv);
2094 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2096 if (extent_op->type == PENDING_EXTENT_INSERT) {
2097 key.objectid = start;
2098 if (skinny_metadata) {
2099 key.offset = extent_op->level;
2100 key.type = BTRFS_METADATA_ITEM_KEY;
2102 key.offset = extent_op->num_bytes;
2103 key.type = BTRFS_EXTENT_ITEM_KEY;
2105 ret = alloc_reserved_tree_block(trans, extent_root,
2106 extent_root->root_key.objectid,
2110 extent_op->level, &key);
2116 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
2120 btrfs_free_path(path);
2124 static int pin_down_bytes(struct btrfs_trans_handle *trans,
2125 struct btrfs_root *root,
2126 u64 bytenr, u64 num_bytes, int is_data)
2129 struct extent_buffer *buf;
2134 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2138 /* we can reuse a block if it hasn't been written
2139 * and it is from this transaction. We can't
2140 * reuse anything from the tree log root because
2141 * it has tiny sub-transactions.
2143 if (btrfs_buffer_uptodate(buf, 0)) {
2144 u64 header_owner = btrfs_header_owner(buf);
2145 u64 header_transid = btrfs_header_generation(buf);
2146 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
2147 header_transid == trans->transid &&
2148 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2149 clean_tree_block(NULL, root, buf);
2150 free_extent_buffer(buf);
2154 free_extent_buffer(buf);
2156 update_pinned_extents(root, bytenr, num_bytes, 1);
2162 void btrfs_pin_extent(struct btrfs_fs_info *fs_info,
2163 u64 bytenr, u64 num_bytes)
2165 update_pinned_extents(fs_info->extent_root, bytenr, num_bytes, 1);
2168 void btrfs_unpin_extent(struct btrfs_fs_info *fs_info,
2169 u64 bytenr, u64 num_bytes)
2171 update_pinned_extents(fs_info->extent_root, bytenr, num_bytes, 0);
2175 * remove an extent from the root, returns 0 on success
2177 static int __free_extent(struct btrfs_trans_handle *trans,
2178 struct btrfs_root *root,
2179 u64 bytenr, u64 num_bytes, u64 parent,
2180 u64 root_objectid, u64 owner_objectid,
2181 u64 owner_offset, int refs_to_drop)
2184 struct btrfs_key key;
2185 struct btrfs_path *path;
2186 struct btrfs_extent_ops *ops = root->fs_info->extent_ops;
2187 struct btrfs_root *extent_root = root->fs_info->extent_root;
2188 struct extent_buffer *leaf;
2189 struct btrfs_extent_item *ei;
2190 struct btrfs_extent_inline_ref *iref;
2193 int extent_slot = 0;
2194 int found_extent = 0;
2198 int skinny_metadata =
2199 btrfs_fs_incompat(extent_root->fs_info,
2200 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
2202 if (root->fs_info->free_extent_hook) {
2203 root->fs_info->free_extent_hook(trans, root, bytenr, num_bytes,
2204 parent, root_objectid, owner_objectid,
2205 owner_offset, refs_to_drop);
2208 path = btrfs_alloc_path();
2213 path->leave_spinning = 1;
2215 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
2217 skinny_metadata = 0;
2218 BUG_ON(!is_data && refs_to_drop != 1);
2220 ret = lookup_extent_backref(trans, extent_root, path, &iref,
2221 bytenr, num_bytes, parent,
2222 root_objectid, owner_objectid,
2225 extent_slot = path->slots[0];
2226 while (extent_slot >= 0) {
2227 btrfs_item_key_to_cpu(path->nodes[0], &key,
2229 if (key.objectid != bytenr)
2231 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
2232 key.offset == num_bytes) {
2236 if (key.type == BTRFS_METADATA_ITEM_KEY &&
2237 key.offset == owner_objectid) {
2241 if (path->slots[0] - extent_slot > 5)
2245 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2246 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
2247 if (found_extent && item_size < sizeof(*ei))
2250 if (!found_extent) {
2252 ret = remove_extent_backref(trans, extent_root, path,
2256 btrfs_release_path(extent_root, path);
2257 path->leave_spinning = 1;
2259 key.objectid = bytenr;
2261 if (skinny_metadata) {
2262 key.type = BTRFS_METADATA_ITEM_KEY;
2263 key.offset = owner_objectid;
2265 key.type = BTRFS_EXTENT_ITEM_KEY;
2266 key.offset = num_bytes;
2269 ret = btrfs_search_slot(trans, extent_root,
2271 if (ret > 0 && skinny_metadata && path->slots[0]) {
2273 btrfs_item_key_to_cpu(path->nodes[0],
2276 if (key.objectid == bytenr &&
2277 key.type == BTRFS_EXTENT_ITEM_KEY &&
2278 key.offset == num_bytes)
2282 if (ret > 0 && skinny_metadata) {
2283 skinny_metadata = 0;
2284 btrfs_release_path(extent_root, path);
2285 key.type = BTRFS_EXTENT_ITEM_KEY;
2286 key.offset = num_bytes;
2287 ret = btrfs_search_slot(trans, extent_root,
2292 printk(KERN_ERR "umm, got %d back from search"
2293 ", was looking for %llu\n", ret,
2294 (unsigned long long)bytenr);
2295 btrfs_print_leaf(extent_root, path->nodes[0]);
2298 extent_slot = path->slots[0];
2301 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
2302 "parent %llu root %llu owner %llu offset %llu\n",
2303 (unsigned long long)bytenr,
2304 (unsigned long long)parent,
2305 (unsigned long long)root_objectid,
2306 (unsigned long long)owner_objectid,
2307 (unsigned long long)owner_offset);
2312 leaf = path->nodes[0];
2313 item_size = btrfs_item_size_nr(leaf, extent_slot);
2314 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2315 if (item_size < sizeof(*ei)) {
2316 BUG_ON(found_extent || extent_slot != path->slots[0]);
2317 ret = convert_extent_item_v0(trans, extent_root, path,
2321 btrfs_release_path(extent_root, path);
2322 path->leave_spinning = 1;
2324 key.objectid = bytenr;
2325 key.type = BTRFS_EXTENT_ITEM_KEY;
2326 key.offset = num_bytes;
2328 ret = btrfs_search_slot(trans, extent_root, &key, path,
2331 printk(KERN_ERR "umm, got %d back from search"
2332 ", was looking for %llu\n", ret,
2333 (unsigned long long)bytenr);
2334 btrfs_print_leaf(extent_root, path->nodes[0]);
2337 extent_slot = path->slots[0];
2338 leaf = path->nodes[0];
2339 item_size = btrfs_item_size_nr(leaf, extent_slot);
2342 BUG_ON(item_size < sizeof(*ei));
2343 ei = btrfs_item_ptr(leaf, extent_slot,
2344 struct btrfs_extent_item);
2345 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
2346 key.type == BTRFS_EXTENT_ITEM_KEY) {
2347 struct btrfs_tree_block_info *bi;
2348 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
2349 bi = (struct btrfs_tree_block_info *)(ei + 1);
2350 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
2353 refs = btrfs_extent_refs(leaf, ei);
2354 BUG_ON(refs < refs_to_drop);
2355 refs -= refs_to_drop;
2359 * In the case of inline back ref, reference count will
2360 * be updated by remove_extent_backref
2363 BUG_ON(!found_extent);
2365 btrfs_set_extent_refs(leaf, ei, refs);
2366 btrfs_mark_buffer_dirty(leaf);
2369 ret = remove_extent_backref(trans, extent_root, path,
2379 BUG_ON(is_data && refs_to_drop !=
2380 extent_data_ref_count(root, path, iref));
2382 BUG_ON(path->slots[0] != extent_slot);
2384 BUG_ON(path->slots[0] != extent_slot + 1);
2385 path->slots[0] = extent_slot;
2390 if (ops && ops->free_extent) {
2391 ret = ops->free_extent(root, bytenr, num_bytes);
2399 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
2406 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2409 btrfs_release_path(extent_root, path);
2412 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
2416 update_block_group(trans, root, bytenr, num_bytes, 0, mark_free);
2419 btrfs_free_path(path);
2420 finish_current_insert(trans, extent_root);
2425 * find all the blocks marked as pending in the radix tree and remove
2426 * them from the extent map
2428 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
2429 btrfs_root *extent_root)
2436 struct extent_io_tree *pending_del;
2437 struct extent_io_tree *extent_ins;
2438 struct pending_extent_op *extent_op;
2440 extent_ins = &extent_root->fs_info->extent_ins;
2441 pending_del = &extent_root->fs_info->pending_del;
2444 ret = find_first_extent_bit(pending_del, 0, &start, &end,
2449 ret = get_state_private(pending_del, start, &priv);
2451 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2453 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
2456 if (!test_range_bit(extent_ins, start, end,
2457 EXTENT_LOCKED, 0)) {
2458 ret = __free_extent(trans, extent_root,
2459 start, end + 1 - start, 0,
2460 extent_root->root_key.objectid,
2461 extent_op->level, 0, 1);
2465 ret = get_state_private(extent_ins, start, &priv);
2467 extent_op = (struct pending_extent_op *)
2468 (unsigned long)priv;
2470 clear_extent_bits(extent_ins, start, end,
2471 EXTENT_LOCKED, GFP_NOFS);
2473 if (extent_op->type == PENDING_BACKREF_UPDATE)
2485 * remove an extent from the root, returns 0 on success
2488 int btrfs_free_extent(struct btrfs_trans_handle *trans,
2489 struct btrfs_root *root,
2490 u64 bytenr, u64 num_bytes, u64 parent,
2491 u64 root_objectid, u64 owner, u64 offset)
2493 struct btrfs_root *extent_root = root->fs_info->extent_root;
2497 WARN_ON(num_bytes < root->sectorsize);
2498 if (root == extent_root) {
2499 struct pending_extent_op *extent_op;
2501 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2504 extent_op->type = PENDING_EXTENT_DELETE;
2505 extent_op->bytenr = bytenr;
2506 extent_op->num_bytes = num_bytes;
2507 extent_op->level = (int)owner;
2509 set_extent_bits(&root->fs_info->pending_del,
2510 bytenr, bytenr + num_bytes - 1,
2511 EXTENT_LOCKED, GFP_NOFS);
2512 set_state_private(&root->fs_info->pending_del,
2513 bytenr, (unsigned long)extent_op);
2516 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
2517 root_objectid, owner, offset, 1);
2518 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
2519 return ret ? ret : pending_ret;
2522 static u64 stripe_align(struct btrfs_root *root, u64 val)
2524 u64 mask = ((u64)root->stripesize - 1);
2525 u64 ret = (val + mask) & ~mask;
2530 * walks the btree of allocated extents and find a hole of a given size.
2531 * The key ins is changed to record the hole:
2532 * ins->objectid == block start
2533 * ins->flags = BTRFS_EXTENT_ITEM_KEY
2534 * ins->offset == number of blocks
2535 * Any available blocks before search_start are skipped.
2537 static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2538 struct btrfs_root *orig_root,
2539 u64 num_bytes, u64 empty_size,
2540 u64 search_start, u64 search_end,
2541 u64 hint_byte, struct btrfs_key *ins,
2542 u64 exclude_start, u64 exclude_nr,
2546 u64 orig_search_start = search_start;
2547 struct btrfs_root * root = orig_root->fs_info->extent_root;
2548 struct btrfs_fs_info *info = root->fs_info;
2549 u64 total_needed = num_bytes;
2550 struct btrfs_block_group_cache *block_group;
2554 WARN_ON(num_bytes < root->sectorsize);
2555 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
2557 search_start = stripe_align(root, search_start);
2560 block_group = btrfs_lookup_first_block_group(info, hint_byte);
2562 hint_byte = search_start;
2563 block_group = btrfs_find_block_group(root, block_group,
2564 hint_byte, data, 1);
2566 block_group = btrfs_find_block_group(root,
2568 search_start, data, 1);
2571 total_needed += empty_size;
2574 search_start = stripe_align(root, search_start);
2576 block_group = btrfs_lookup_first_block_group(info,
2579 block_group = btrfs_lookup_first_block_group(info,
2582 ret = find_search_start(root, &block_group, &search_start,
2583 total_needed, data);
2587 ins->objectid = search_start;
2588 ins->offset = num_bytes;
2590 if (ins->objectid + num_bytes >
2591 block_group->key.objectid + block_group->key.offset) {
2592 search_start = block_group->key.objectid +
2593 block_group->key.offset;
2597 if (test_range_bit(&info->extent_ins, ins->objectid,
2598 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
2599 search_start = ins->objectid + num_bytes;
2603 if (test_range_bit(&info->pinned_extents, ins->objectid,
2604 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
2605 search_start = ins->objectid + num_bytes;
2609 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
2610 ins->objectid < exclude_start + exclude_nr)) {
2611 search_start = exclude_start + exclude_nr;
2615 if (!(data & BTRFS_BLOCK_GROUP_DATA)) {
2616 block_group = btrfs_lookup_block_group(info, ins->objectid);
2618 trans->block_group = block_group;
2620 ins->offset = num_bytes;
2624 block_group = btrfs_lookup_first_block_group(info, search_start);
2626 search_start = orig_search_start;
2633 total_needed -= empty_size;
2639 block_group = btrfs_find_block_group(root, block_group,
2640 search_start, data, 0);
2647 static int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2648 struct btrfs_root *root,
2649 u64 num_bytes, u64 empty_size,
2650 u64 hint_byte, u64 search_end,
2651 struct btrfs_key *ins, int data)
2654 u64 search_start = 0;
2656 struct btrfs_fs_info *info = root->fs_info;
2658 if (info->extent_ops) {
2659 struct btrfs_extent_ops *ops = info->extent_ops;
2660 ret = ops->alloc_extent(root, num_bytes, hint_byte, ins);
2666 alloc_profile = info->avail_data_alloc_bits &
2667 info->data_alloc_profile;
2668 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
2669 } else if ((info->system_allocs > 0 || root == info->chunk_root) &&
2670 info->system_allocs >= 0) {
2671 alloc_profile = info->avail_system_alloc_bits &
2672 info->system_alloc_profile;
2673 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
2675 alloc_profile = info->avail_metadata_alloc_bits &
2676 info->metadata_alloc_profile;
2677 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
2680 if (root->ref_cows) {
2681 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
2682 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2684 BTRFS_BLOCK_GROUP_METADATA);
2687 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2688 num_bytes + 2 * 1024 * 1024, data);
2692 WARN_ON(num_bytes < root->sectorsize);
2693 ret = find_free_extent(trans, root, num_bytes, empty_size,
2694 search_start, search_end, hint_byte, ins,
2695 trans->alloc_exclude_start,
2696 trans->alloc_exclude_nr, data);
2699 clear_extent_dirty(&root->fs_info->free_space_cache,
2700 ins->objectid, ins->objectid + ins->offset - 1,
2705 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
2706 struct btrfs_root *root,
2707 u64 root_objectid, u64 generation,
2708 u64 flags, struct btrfs_disk_key *key,
2709 int level, struct btrfs_key *ins)
2712 struct btrfs_fs_info *fs_info = root->fs_info;
2713 struct btrfs_extent_item *extent_item;
2714 struct btrfs_tree_block_info *block_info;
2715 struct btrfs_extent_inline_ref *iref;
2716 struct btrfs_path *path;
2717 struct extent_buffer *leaf;
2718 u32 size = sizeof(*extent_item) + sizeof(*iref);
2719 int skinny_metadata =
2720 btrfs_fs_incompat(fs_info,
2721 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA);
2723 if (!skinny_metadata)
2724 size += sizeof(*block_info);
2726 path = btrfs_alloc_path();
2729 path->leave_spinning = 1;
2730 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
2734 leaf = path->nodes[0];
2735 extent_item = btrfs_item_ptr(leaf, path->slots[0],
2736 struct btrfs_extent_item);
2737 btrfs_set_extent_refs(leaf, extent_item, 1);
2738 btrfs_set_extent_generation(leaf, extent_item, generation);
2739 btrfs_set_extent_flags(leaf, extent_item,
2740 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
2742 if (skinny_metadata) {
2743 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
2745 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
2746 btrfs_set_tree_block_key(leaf, block_info, key);
2747 btrfs_set_tree_block_level(leaf, block_info, level);
2748 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
2751 btrfs_set_extent_inline_ref_type(leaf, iref, BTRFS_TREE_BLOCK_REF_KEY);
2752 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
2754 btrfs_mark_buffer_dirty(leaf);
2755 btrfs_free_path(path);
2757 ret = update_block_group(trans, root, ins->objectid, root->leafsize,
2762 static int alloc_tree_block(struct btrfs_trans_handle *trans,
2763 struct btrfs_root *root, u64 num_bytes,
2764 u64 root_objectid, u64 generation,
2765 u64 flags, struct btrfs_disk_key *key,
2766 int level, u64 empty_size, u64 hint_byte,
2767 u64 search_end, struct btrfs_key *ins)
2770 ret = btrfs_reserve_extent(trans, root, num_bytes, empty_size,
2771 hint_byte, search_end, ins, 0);
2774 if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID) {
2775 struct pending_extent_op *extent_op;
2777 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2780 extent_op->type = PENDING_EXTENT_INSERT;
2781 extent_op->bytenr = ins->objectid;
2782 extent_op->num_bytes = ins->offset;
2783 extent_op->level = level;
2784 extent_op->flags = flags;
2785 memcpy(&extent_op->key, key, sizeof(*key));
2787 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
2788 ins->objectid + ins->offset - 1,
2789 EXTENT_LOCKED, GFP_NOFS);
2790 set_state_private(&root->fs_info->extent_ins,
2791 ins->objectid, (unsigned long)extent_op);
2793 if (btrfs_fs_incompat(root->fs_info,
2794 BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA)) {
2795 ins->offset = level;
2796 ins->type = BTRFS_METADATA_ITEM_KEY;
2798 ret = alloc_reserved_tree_block(trans, root, root_objectid,
2801 finish_current_insert(trans, root->fs_info->extent_root);
2802 del_pending_extents(trans, root->fs_info->extent_root);
2808 * helper function to allocate a block for a given tree
2809 * returns the tree buffer or NULL.
2811 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
2812 struct btrfs_root *root,
2813 u32 blocksize, u64 root_objectid,
2814 struct btrfs_disk_key *key, int level,
2815 u64 hint, u64 empty_size)
2817 struct btrfs_key ins;
2819 struct extent_buffer *buf;
2821 ret = alloc_tree_block(trans, root, blocksize, root_objectid,
2822 trans->transid, 0, key, level,
2823 empty_size, hint, (u64)-1, &ins);
2826 return ERR_PTR(ret);
2829 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
2831 btrfs_free_extent(trans, root, ins.objectid, ins.offset,
2832 0, root->root_key.objectid, level, 0);
2834 return ERR_PTR(-ENOMEM);
2836 btrfs_set_buffer_uptodate(buf);
2837 trans->blocks_used++;
2844 static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
2845 struct btrfs_root *root,
2846 struct extent_buffer *leaf)
2849 u64 leaf_generation;
2850 struct btrfs_key key;
2851 struct btrfs_file_extent_item *fi;
2856 BUG_ON(!btrfs_is_leaf(leaf));
2857 nritems = btrfs_header_nritems(leaf);
2858 leaf_owner = btrfs_header_owner(leaf);
2859 leaf_generation = btrfs_header_generation(leaf);
2861 for (i = 0; i < nritems; i++) {
2864 btrfs_item_key_to_cpu(leaf, &key, i);
2865 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2867 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
2868 if (btrfs_file_extent_type(leaf, fi) ==
2869 BTRFS_FILE_EXTENT_INLINE)
2872 * FIXME make sure to insert a trans record that
2873 * repeats the snapshot del on crash
2875 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2876 if (disk_bytenr == 0)
2878 ret = btrfs_free_extent(trans, root, disk_bytenr,
2879 btrfs_file_extent_disk_num_bytes(leaf, fi),
2880 leaf->start, leaf_owner, leaf_generation,
2887 static void noinline reada_walk_down(struct btrfs_root *root,
2888 struct extent_buffer *node,
2901 nritems = btrfs_header_nritems(node);
2902 level = btrfs_header_level(node);
2906 for (i = slot; i < nritems && skipped < 32; i++) {
2907 bytenr = btrfs_node_blockptr(node, i);
2908 if (last && ((bytenr > last && bytenr - last > 32 * 1024) ||
2909 (last > bytenr && last - bytenr > 32 * 1024))) {
2913 blocksize = btrfs_level_size(root, level - 1);
2915 ret = btrfs_lookup_extent_ref(NULL, root, bytenr,
2923 mutex_unlock(&root->fs_info->fs_mutex);
2924 ret = readahead_tree_block(root, bytenr, blocksize,
2925 btrfs_node_ptr_generation(node, i));
2926 last = bytenr + blocksize;
2928 mutex_lock(&root->fs_info->fs_mutex);
2935 * helper function for drop_snapshot, this walks down the tree dropping ref
2936 * counts as it goes.
2938 static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2939 struct btrfs_root *root,
2940 struct btrfs_path *path, int *level)
2946 struct extent_buffer *next;
2947 struct extent_buffer *cur;
2948 struct extent_buffer *parent;
2953 WARN_ON(*level < 0);
2954 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2955 ret = btrfs_lookup_extent_ref(trans, root,
2956 path->nodes[*level]->start,
2957 path->nodes[*level]->len, &refs);
2963 * walk down to the last node level and free all the leaves
2965 while(*level >= 0) {
2966 WARN_ON(*level < 0);
2967 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2968 cur = path->nodes[*level];
2970 if (btrfs_header_level(cur) != *level)
2973 if (path->slots[*level] >=
2974 btrfs_header_nritems(cur))
2977 ret = drop_leaf_ref(trans, root, cur);
2981 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2982 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2983 blocksize = btrfs_level_size(root, *level - 1);
2984 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
2988 parent = path->nodes[*level];
2989 root_owner = btrfs_header_owner(parent);
2990 root_gen = btrfs_header_generation(parent);
2991 path->slots[*level]++;
2992 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
2993 parent->start, root_owner,
2994 root_gen, *level - 1, 1);
2998 next = btrfs_find_tree_block(root, bytenr, blocksize);
2999 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
3000 free_extent_buffer(next);
3001 reada_walk_down(root, cur, path->slots[*level]);
3002 mutex_unlock(&root->fs_info->fs_mutex);
3003 next = read_tree_block(root, bytenr, blocksize,
3005 mutex_lock(&root->fs_info->fs_mutex);
3007 WARN_ON(*level <= 0);
3008 if (path->nodes[*level-1])
3009 free_extent_buffer(path->nodes[*level-1]);
3010 path->nodes[*level-1] = next;
3011 *level = btrfs_header_level(next);
3012 path->slots[*level] = 0;
3015 WARN_ON(*level < 0);
3016 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3018 if (path->nodes[*level] == root->node) {
3019 root_owner = root->root_key.objectid;
3020 parent = path->nodes[*level];
3022 parent = path->nodes[*level + 1];
3023 root_owner = btrfs_header_owner(parent);
3026 root_gen = btrfs_header_generation(parent);
3027 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
3028 path->nodes[*level]->len, parent->start,
3029 root_owner, root_gen, *level, 1);
3030 free_extent_buffer(path->nodes[*level]);
3031 path->nodes[*level] = NULL;
3038 * helper for dropping snapshots. This walks back up the tree in the path
3039 * to find the first node higher up where we haven't yet gone through
3042 static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
3043 struct btrfs_root *root,
3044 struct btrfs_path *path, int *level)
3048 struct btrfs_root_item *root_item = &root->root_item;
3053 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
3054 slot = path->slots[i];
3055 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3056 struct extent_buffer *node;
3057 struct btrfs_disk_key disk_key;
3058 node = path->nodes[i];
3061 WARN_ON(*level == 0);
3062 btrfs_node_key(node, &disk_key, path->slots[i]);
3063 memcpy(&root_item->drop_progress,
3064 &disk_key, sizeof(disk_key));
3065 root_item->drop_level = i;
3068 struct extent_buffer *parent;
3069 if (path->nodes[*level] == root->node)
3070 parent = path->nodes[*level];
3072 parent = path->nodes[*level + 1];
3074 root_owner = btrfs_header_owner(parent);
3075 root_gen = btrfs_header_generation(parent);
3076 ret = btrfs_free_extent(trans, root,
3077 path->nodes[*level]->start,
3078 path->nodes[*level]->len,
3079 parent->start, root_owner,
3080 root_gen, *level, 1);
3082 free_extent_buffer(path->nodes[*level]);
3083 path->nodes[*level] = NULL;
3092 int btrfs_free_block_groups(struct btrfs_fs_info *info)
3094 struct btrfs_space_info *sinfo;
3095 struct btrfs_block_group_cache *cache;
3102 ret = find_first_extent_bit(&info->block_group_cache, 0,
3103 &start, &end, (unsigned int)-1);
3106 ret = get_state_private(&info->block_group_cache, start, &ptr);
3108 cache = (struct btrfs_block_group_cache *)
3110 if (cache->free_space_ctl) {
3111 btrfs_remove_free_space_cache(cache);
3112 kfree(cache->free_space_ctl);
3116 clear_extent_bits(&info->block_group_cache, start,
3117 end, (unsigned int)-1, GFP_NOFS);
3120 ret = find_first_extent_bit(&info->free_space_cache, 0,
3121 &start, &end, EXTENT_DIRTY);
3124 clear_extent_dirty(&info->free_space_cache, start,
3128 while (!list_empty(&info->space_info)) {
3129 sinfo = list_entry(info->space_info.next,
3130 struct btrfs_space_info, list);
3131 list_del_init(&sinfo->list);
3137 int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
3138 struct btrfs_key *key)
3141 struct btrfs_key found_key;
3142 struct extent_buffer *leaf;
3145 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
3149 slot = path->slots[0];
3150 leaf = path->nodes[0];
3151 if (slot >= btrfs_header_nritems(leaf)) {
3152 ret = btrfs_next_leaf(root, path);
3159 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3161 if (found_key.objectid >= key->objectid &&
3162 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
3171 int btrfs_read_block_groups(struct btrfs_root *root)
3173 struct btrfs_path *path;
3176 struct btrfs_block_group_cache *cache;
3177 struct btrfs_fs_info *info = root->fs_info;
3178 struct btrfs_space_info *space_info;
3179 struct extent_io_tree *block_group_cache;
3180 struct btrfs_key key;
3181 struct btrfs_key found_key;
3182 struct extent_buffer *leaf;
3184 block_group_cache = &info->block_group_cache;
3186 root = info->extent_root;
3189 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3190 path = btrfs_alloc_path();
3195 ret = find_first_block_group(root, path, &key);
3203 leaf = path->nodes[0];
3204 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3205 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3211 read_extent_buffer(leaf, &cache->item,
3212 btrfs_item_ptr_offset(leaf, path->slots[0]),
3213 sizeof(cache->item));
3214 memcpy(&cache->key, &found_key, sizeof(found_key));
3217 key.objectid = found_key.objectid + found_key.offset;
3218 btrfs_release_path(root, path);
3219 cache->flags = btrfs_block_group_flags(&cache->item);
3221 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
3222 bit = BLOCK_GROUP_DATA;
3223 } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
3224 bit = BLOCK_GROUP_SYSTEM;
3225 } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
3226 bit = BLOCK_GROUP_METADATA;
3228 set_avail_alloc_bits(info, cache->flags);
3229 if (btrfs_chunk_readonly(root, cache->key.objectid))
3232 ret = update_space_info(info, cache->flags, found_key.offset,
3233 btrfs_block_group_used(&cache->item),
3236 cache->space_info = space_info;
3238 /* use EXTENT_LOCKED to prevent merging */
3239 set_extent_bits(block_group_cache, found_key.objectid,
3240 found_key.objectid + found_key.offset - 1,
3241 bit | EXTENT_LOCKED, GFP_NOFS);
3242 set_state_private(block_group_cache, found_key.objectid,
3243 (unsigned long)cache);
3247 btrfs_free_path(path);
3251 struct btrfs_block_group_cache *
3252 btrfs_add_block_group(struct btrfs_fs_info *fs_info, u64 bytes_used, u64 type,
3253 u64 chunk_objectid, u64 chunk_offset, u64 size)
3257 struct btrfs_block_group_cache *cache;
3258 struct extent_io_tree *block_group_cache;
3260 block_group_cache = &fs_info->block_group_cache;
3262 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3264 cache->key.objectid = chunk_offset;
3265 cache->key.offset = size;
3267 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3268 btrfs_set_block_group_used(&cache->item, bytes_used);
3269 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
3270 cache->flags = type;
3271 btrfs_set_block_group_flags(&cache->item, type);
3273 ret = update_space_info(fs_info, cache->flags, size, bytes_used,
3274 &cache->space_info);
3277 bit = block_group_state_bits(type);
3278 ret = set_extent_bits(block_group_cache, chunk_offset,
3279 chunk_offset + size - 1,
3280 bit | EXTENT_LOCKED, GFP_NOFS);
3283 ret = set_state_private(block_group_cache, chunk_offset,
3284 (unsigned long)cache);
3286 set_avail_alloc_bits(fs_info, type);
3291 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
3292 struct btrfs_root *root, u64 bytes_used,
3293 u64 type, u64 chunk_objectid, u64 chunk_offset,
3297 struct btrfs_root *extent_root;
3298 struct btrfs_block_group_cache *cache;
3300 cache = btrfs_add_block_group(root->fs_info, bytes_used, type,
3301 chunk_objectid, chunk_offset, size);
3302 extent_root = root->fs_info->extent_root;
3303 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3304 sizeof(cache->item));
3307 ret = finish_current_insert(trans, extent_root);
3309 ret = del_pending_extents(trans, extent_root);
3316 * This is for converter use only.
3318 * In that case, we don't know where are free blocks located.
3319 * Therefore all block group cache entries must be setup properly
3320 * before doing any block allocation.
3322 int btrfs_make_block_groups(struct btrfs_trans_handle *trans,
3323 struct btrfs_root *root)
3331 u64 total_metadata = 0;
3335 struct btrfs_root *extent_root;
3336 struct btrfs_block_group_cache *cache;
3337 struct extent_io_tree *block_group_cache;
3339 extent_root = root->fs_info->extent_root;
3340 block_group_cache = &root->fs_info->block_group_cache;
3341 chunk_objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
3342 total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
3343 group_align = 64 * root->sectorsize;
3346 while (cur_start < total_bytes) {
3347 group_size = total_bytes / 12;
3348 group_size = min_t(u64, group_size, total_bytes - cur_start);
3349 if (cur_start == 0) {
3350 bit = BLOCK_GROUP_SYSTEM;
3351 group_type = BTRFS_BLOCK_GROUP_SYSTEM;
3353 group_size &= ~(group_align - 1);
3354 group_size = max_t(u64, group_size, 8 * 1024 * 1024);
3355 group_size = min_t(u64, group_size, 32 * 1024 * 1024);
3357 group_size &= ~(group_align - 1);
3358 if (total_data >= total_metadata * 2) {
3359 group_type = BTRFS_BLOCK_GROUP_METADATA;
3360 group_size = min_t(u64, group_size,
3361 1ULL * 1024 * 1024 * 1024);
3362 total_metadata += group_size;
3364 group_type = BTRFS_BLOCK_GROUP_DATA;
3365 group_size = min_t(u64, group_size,
3366 5ULL * 1024 * 1024 * 1024);
3367 total_data += group_size;
3369 if ((total_bytes - cur_start) * 4 < group_size * 5)
3370 group_size = total_bytes - cur_start;
3373 cache = kzalloc(sizeof(*cache), GFP_NOFS);
3376 cache->key.objectid = cur_start;
3377 cache->key.offset = group_size;
3378 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
3380 btrfs_set_block_group_used(&cache->item, 0);
3381 btrfs_set_block_group_chunk_objectid(&cache->item,
3383 btrfs_set_block_group_flags(&cache->item, group_type);
3385 cache->flags = group_type;
3387 ret = update_space_info(root->fs_info, group_type, group_size,
3388 0, &cache->space_info);
3390 set_avail_alloc_bits(extent_root->fs_info, group_type);
3392 set_extent_bits(block_group_cache, cur_start,
3393 cur_start + group_size - 1,
3394 bit | EXTENT_LOCKED, GFP_NOFS);
3395 set_state_private(block_group_cache, cur_start,
3396 (unsigned long)cache);
3397 cur_start += group_size;
3399 /* then insert all the items */
3401 while(cur_start < total_bytes) {
3402 cache = btrfs_lookup_block_group(root->fs_info, cur_start);
3405 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3406 sizeof(cache->item));
3409 finish_current_insert(trans, extent_root);
3410 ret = del_pending_extents(trans, extent_root);
3413 cur_start = cache->key.objectid + cache->key.offset;
3418 int btrfs_update_block_group(struct btrfs_trans_handle *trans,
3419 struct btrfs_root *root,
3420 u64 bytenr, u64 num_bytes, int alloc,
3423 return update_block_group(trans, root, bytenr, num_bytes,
3427 static int btrfs_count_extents_in_block_group(struct btrfs_root *root,
3428 struct btrfs_path *path, u64 start,
3432 struct btrfs_key key;
3433 struct extent_buffer *leaf;
3439 key.objectid = start;
3440 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
3441 ret = btrfs_search_slot(NULL, root->fs_info->extent_root,
3446 leaf = path->nodes[0];
3447 slot = path->slots[0];
3448 if (slot >= btrfs_header_nritems(leaf)) {
3449 ret = btrfs_next_leaf(root, path);
3454 leaf = path->nodes[0];
3455 slot = path->slots[0];
3457 btrfs_item_key_to_cpu(leaf, &key, slot);
3458 if (key.objectid > start + len)
3460 if (key.type == BTRFS_EXTENT_ITEM_KEY)
3461 bytes_used += key.offset;
3462 if (key.type == BTRFS_METADATA_ITEM_KEY)
3463 bytes_used += root->leafsize;
3466 *total = bytes_used;
3467 btrfs_release_path(root, path);
3471 int btrfs_check_block_accounting(struct btrfs_root *root)
3476 struct btrfs_path path;
3477 struct btrfs_block_group_cache *cache;
3478 struct btrfs_fs_info *fs_info = root->fs_info;
3480 btrfs_init_path(&path);
3483 cache = btrfs_lookup_block_group(fs_info, start);
3487 ret = btrfs_count_extents_in_block_group(root, &path,
3488 cache->key.objectid,
3493 u64 on_disk = btrfs_block_group_used(&cache->item);
3494 if (on_disk != bytes_used) {
3495 fprintf(stderr, "bad block group accounting found %llu "
3496 "expected %llu block group %llu\n",
3497 (unsigned long long)bytes_used,
3498 (unsigned long long)on_disk,
3499 (unsigned long long)cache->key.objectid);
3502 start = cache->key.objectid + cache->key.offset;
3504 cache->space_info->bytes_used = 0;
3510 * Fixup block accounting. The initial block accounting created by
3511 * make_block_groups isn't accuracy in this case.
3513 int btrfs_fix_block_accounting(struct btrfs_trans_handle *trans,
3514 struct btrfs_root *root)
3520 struct btrfs_path path;
3521 struct btrfs_key key;
3522 struct extent_buffer *leaf;
3523 struct btrfs_block_group_cache *cache;
3524 struct btrfs_fs_info *fs_info = root->fs_info;
3526 root = root->fs_info->extent_root;
3528 while(extent_root_pending_ops(fs_info)) {
3529 ret = finish_current_insert(trans, root);
3532 ret = del_pending_extents(trans, root);
3538 cache = btrfs_lookup_first_block_group(fs_info, start);
3541 start = cache->key.objectid + cache->key.offset;
3542 btrfs_set_block_group_used(&cache->item, 0);
3543 cache->space_info->bytes_used = 0;
3544 set_extent_bits(&root->fs_info->block_group_cache,
3545 cache->key.objectid,
3546 cache->key.objectid + cache->key.offset -1,
3547 BLOCK_GROUP_DIRTY, GFP_NOFS);
3550 btrfs_init_path(&path);
3553 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
3554 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
3559 leaf = path.nodes[0];
3560 slot = path.slots[0];
3561 if (slot >= btrfs_header_nritems(leaf)) {
3562 ret = btrfs_next_leaf(root, &path);
3567 leaf = path.nodes[0];
3568 slot = path.slots[0];
3570 btrfs_item_key_to_cpu(leaf, &key, slot);
3571 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
3572 bytes_used += key.offset;
3573 ret = btrfs_update_block_group(trans, root,
3574 key.objectid, key.offset, 1, 0);
3576 } else if (key.type == BTRFS_METADATA_ITEM_KEY) {
3577 bytes_used += root->leafsize;
3578 ret = btrfs_update_block_group(trans, root,
3579 key.objectid, root->leafsize, 1, 0);
3584 btrfs_set_super_bytes_used(root->fs_info->super_copy, bytes_used);
3585 btrfs_release_path(root, &path);